Exact eigenfunctions for square-wave gratings: Application to diffraction and surface-plasmon calculations