Dynamical theory of x-ray diffraction