10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao""" Python 'base64_codec' Codec - base64 content transfer encoding 20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Unlike most of the other codecs which target Unicode, this codec 40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao will return Python string objects for both encode and decode. 50a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Written by Marc-Andre Lemburg (mal@lemburg.com). 70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao""" 90a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport codecs, base64 100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao### Codec APIs 120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 130a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef base64_encode(input,errors='strict'): 140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ Encodes the object input and returns a tuple (output 160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao object, length consumed). 170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao errors defines the error handling to apply. It defaults to 190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'strict' handling which is the only currently supported 200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao error handling for this codec. 210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao assert errors == 'strict' 240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao output = base64.encodestring(input) 250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (output, len(input)) 260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 270a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef base64_decode(input,errors='strict'): 280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ Decodes the object input and returns a tuple (output 300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao object, length consumed). 310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao input must be an object which provides the bf_getreadbuf 330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao buffer slot. Python strings, buffer objects and memory 340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao mapped files are examples of objects providing this slot. 350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao errors defines the error handling to apply. It defaults to 370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'strict' handling which is the only currently supported 380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao error handling for this codec. 390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao assert errors == 'strict' 420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao output = base64.decodestring(input) 430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (output, len(input)) 440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 450a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Codec(codecs.Codec): 460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def encode(self, input,errors='strict'): 480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return base64_encode(input,errors) 490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def decode(self, input,errors='strict'): 500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return base64_decode(input,errors) 510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 520a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass IncrementalEncoder(codecs.IncrementalEncoder): 530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def encode(self, input, final=False): 540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao assert self.errors == 'strict' 550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return base64.encodestring(input) 560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 570a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass IncrementalDecoder(codecs.IncrementalDecoder): 580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def decode(self, input, final=False): 590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao assert self.errors == 'strict' 600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return base64.decodestring(input) 610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 620a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass StreamWriter(Codec,codecs.StreamWriter): 630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 650a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass StreamReader(Codec,codecs.StreamReader): 660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao### encodings module API 690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 700a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef getregentry(): 710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return codecs.CodecInfo( 720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao name='base64', 730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao encode=base64_encode, 740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao decode=base64_decode, 750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao incrementalencoder=IncrementalEncoder, 760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao incrementaldecoder=IncrementalDecoder, 770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao streamwriter=StreamWriter, 780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao streamreader=StreamReader, 790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ) 80