34 from gettext
import gettext
as _
36 BASE64 =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
39 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
40 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
41 255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
42 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255,255,255,255,
43 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
44 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255,
45 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
46 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255,
47 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
48 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
49 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
50 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
51 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
52 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
53 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
54 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
68 def encode(f, src, columns = 72, delim = None):
70 blen = len(src) - len(src) % 3
73 for pos
in xrange(0, blen, 3):
81 i0, i1, i2 = [ord(ch)
for ch
in src[pos:pos + 3]]
84 f.write(BASE64[i0 >> 2] +
85 BASE64[((i0 & 0x03) << 4) + (i1 >> 4)] +
86 BASE64[((i1 & 0x0f) << 2) + (i2 >> 6)] +
89 if columns
is not None:
91 if ocnt % (columns / 4) == 0
and pos != len(src) - 3:
97 if blen == len(src) - 1:
100 i1 = ord(src[blen + 1])
103 f.write(BASE64[i0 >> 2] +
104 BASE64[((i0 & 0x03) << 4) + (i1 >> 4)])
105 if blen == len(src) - 1:
108 f.write(BASE64[((i1 & 0x0f) << 2) + (i2 >> 6)])
114 if delim
is not None:
115 f.write(delim +
'\n')
148 except StopIteration:
149 if delim
is not None:
150 raise DecodingError, _(
"Unexpected end-of-file")
153 if delim
is not None and line == delim +
'\n':
172 res = (pos & 0x0f) << 4
175 dst += [res | (pos >> 2)]
176 res = (pos & 0x03) << 6
203 if state == 2
or state == 3:
215 return ''.join(chr(b)
for b
in dst)
def encode
Write a binary string to a file in base64 representation.
Raised when reading invalid or unterminated base64-encoded data.
def decode
Read a string in base64 representation from a file.