Single-photon scattering and quantum-state transformations in cavity QED