Implement "locale" function in PE module and improve buffer bounds checking