blob: 8e7703b3b6072d644f9ea000b1a2737f990d6f95 [file] [log] [blame]
Georg Brandl02524622010-12-02 18:06:51 +00001"""Python 'base64_codec' Codec - base64 content transfer encoding.
2
R David Murraye5cb8362014-03-13 20:54:30 -04003This codec de/encodes from bytes to bytes.
Georg Brandl02524622010-12-02 18:06:51 +00004
5Written by Marc-Andre Lemburg (mal@lemburg.com).
6"""
7
8import codecs
9import base64
10
11### Codec APIs
12
13def base64_encode(input, errors='strict'):
14 assert errors == 'strict'
Georg Brandl7c23ea22010-12-06 22:25:25 +000015 return (base64.encodebytes(input), len(input))
Georg Brandl02524622010-12-02 18:06:51 +000016
17def base64_decode(input, errors='strict'):
18 assert errors == 'strict'
Georg Brandl7c23ea22010-12-06 22:25:25 +000019 return (base64.decodebytes(input), len(input))
Georg Brandl02524622010-12-02 18:06:51 +000020
21class Codec(codecs.Codec):
22 def encode(self, input, errors='strict'):
23 return base64_encode(input, errors)
24 def decode(self, input, errors='strict'):
25 return base64_decode(input, errors)
26
27class IncrementalEncoder(codecs.IncrementalEncoder):
28 def encode(self, input, final=False):
29 assert self.errors == 'strict'
Georg Brandl7c23ea22010-12-06 22:25:25 +000030 return base64.encodebytes(input)
Georg Brandl02524622010-12-02 18:06:51 +000031
32class IncrementalDecoder(codecs.IncrementalDecoder):
33 def decode(self, input, final=False):
34 assert self.errors == 'strict'
Georg Brandl7c23ea22010-12-06 22:25:25 +000035 return base64.decodebytes(input)
Georg Brandl02524622010-12-02 18:06:51 +000036
37class StreamWriter(Codec, codecs.StreamWriter):
38 charbuffertype = bytes
39
40class StreamReader(Codec, codecs.StreamReader):
41 charbuffertype = bytes
42
43### encodings module API
44
45def getregentry():
46 return codecs.CodecInfo(
47 name='base64',
48 encode=base64_encode,
49 decode=base64_decode,
50 incrementalencoder=IncrementalEncoder,
51 incrementaldecoder=IncrementalDecoder,
52 streamwriter=StreamWriter,
53 streamreader=StreamReader,
Serhiy Storchaka94ee3892014-02-24 14:43:03 +020054 _is_text_encoding=False,
Georg Brandl02524622010-12-02 18:06:51 +000055 )