Preparation of esters of carbonic or haloformic acids