Theory of plasma injection into a magnetic field