Mechanism of proton doping in polyaniline