1 | n/a | """Python 'base64_codec' Codec - base64 content transfer encoding. |
---|
2 | n/a | |
---|
3 | n/a | This codec de/encodes from bytes to bytes. |
---|
4 | n/a | |
---|
5 | n/a | Written by Marc-Andre Lemburg (mal@lemburg.com). |
---|
6 | n/a | """ |
---|
7 | n/a | |
---|
8 | n/a | import codecs |
---|
9 | n/a | import base64 |
---|
10 | n/a | |
---|
11 | n/a | ### Codec APIs |
---|
12 | n/a | |
---|
13 | n/a | def base64_encode(input, errors='strict'): |
---|
14 | n/a | assert errors == 'strict' |
---|
15 | n/a | return (base64.encodebytes(input), len(input)) |
---|
16 | n/a | |
---|
17 | n/a | def base64_decode(input, errors='strict'): |
---|
18 | n/a | assert errors == 'strict' |
---|
19 | n/a | return (base64.decodebytes(input), len(input)) |
---|
20 | n/a | |
---|
21 | n/a | class Codec(codecs.Codec): |
---|
22 | n/a | def encode(self, input, errors='strict'): |
---|
23 | n/a | return base64_encode(input, errors) |
---|
24 | n/a | def decode(self, input, errors='strict'): |
---|
25 | n/a | return base64_decode(input, errors) |
---|
26 | n/a | |
---|
27 | n/a | class IncrementalEncoder(codecs.IncrementalEncoder): |
---|
28 | n/a | def encode(self, input, final=False): |
---|
29 | n/a | assert self.errors == 'strict' |
---|
30 | n/a | return base64.encodebytes(input) |
---|
31 | n/a | |
---|
32 | n/a | class IncrementalDecoder(codecs.IncrementalDecoder): |
---|
33 | n/a | def decode(self, input, final=False): |
---|
34 | n/a | assert self.errors == 'strict' |
---|
35 | n/a | return base64.decodebytes(input) |
---|
36 | n/a | |
---|
37 | n/a | class StreamWriter(Codec, codecs.StreamWriter): |
---|
38 | n/a | charbuffertype = bytes |
---|
39 | n/a | |
---|
40 | n/a | class StreamReader(Codec, codecs.StreamReader): |
---|
41 | n/a | charbuffertype = bytes |
---|
42 | n/a | |
---|
43 | n/a | ### encodings module API |
---|
44 | n/a | |
---|
45 | n/a | def getregentry(): |
---|
46 | n/a | return codecs.CodecInfo( |
---|
47 | n/a | name='base64', |
---|
48 | n/a | encode=base64_encode, |
---|
49 | n/a | decode=base64_decode, |
---|
50 | n/a | incrementalencoder=IncrementalEncoder, |
---|
51 | n/a | incrementaldecoder=IncrementalDecoder, |
---|
52 | n/a | streamwriter=StreamWriter, |
---|
53 | n/a | streamreader=StreamReader, |
---|
54 | n/a | _is_text_encoding=False, |
---|
55 | n/a | ) |
---|