Skip to content

Commit 17b499c

Browse files
committed
Needed until next beta with binascii C module
1 parent f450eb9 commit 17b499c

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
PAD = '='
2+
3+
table_a2b_base64 = [
4+
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
5+
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
6+
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
7+
52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1, # Note PAD->-1 here
8+
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
9+
15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
10+
-1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
11+
41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1,
12+
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
13+
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
14+
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
15+
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
16+
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
17+
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
18+
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
19+
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
20+
]
21+
def _transform(n):
22+
if n == -1:
23+
return '\xff'
24+
else:
25+
return chr(n)
26+
table_a2b_base64 = ''.join(map(_transform, table_a2b_base64))
27+
assert len(table_a2b_base64) == 256
28+
29+
def a2b_base64(ascii):
30+
"Decode a line of base64 data."
31+
32+
res = []
33+
quad_pos = 0
34+
leftchar = 0
35+
leftbits = 0
36+
last_char_was_a_pad = False
37+
38+
for c in ascii:
39+
c = chr(c)
40+
if c == PAD:
41+
if quad_pos > 2 or (quad_pos == 2 and last_char_was_a_pad):
42+
break # stop on 'xxx=' or on 'xx=='
43+
last_char_was_a_pad = True
44+
else:
45+
n = ord(table_a2b_base64[ord(c)])
46+
if n == 0xff:
47+
continue # ignore strange characters
48+
#
49+
# Shift it in on the low end, and see if there's
50+
# a byte ready for output.
51+
quad_pos = (quad_pos + 1) & 3
52+
leftchar = (leftchar << 6) | n
53+
leftbits += 6
54+
#
55+
if leftbits >= 8:
56+
leftbits -= 8
57+
res.append((leftchar >> leftbits).to_bytes(1, 'big'))
58+
leftchar &= ((1 << leftbits) - 1)
59+
#
60+
last_char_was_a_pad = False
61+
else:
62+
if leftbits != 0:
63+
raise Exception("Incorrect padding")
64+
65+
return b''.join(res)
66+
67+
# ____________________________________________________________
68+
69+
table_b2a_base64 = (
70+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
71+
72+
def b2a_base64(bindata):
73+
"Base64-code line of data."
74+
75+
newlength = (len(bindata) + 2) // 3
76+
newlength = newlength * 4 + 1
77+
res = []
78+
79+
leftchar = 0
80+
leftbits = 0
81+
for c in bindata:
82+
# Shift into our buffer, and output any 6bits ready
83+
leftchar = (leftchar << 8) | c
84+
leftbits += 8
85+
res.append(table_b2a_base64[(leftchar >> (leftbits-6)) & 0x3f])
86+
leftbits -= 6
87+
if leftbits >= 6:
88+
res.append(table_b2a_base64[(leftchar >> (leftbits-6)) & 0x3f])
89+
leftbits -= 6
90+
#
91+
if leftbits == 2:
92+
res.append(table_b2a_base64[(leftchar & 3) << 4])
93+
res.append(PAD)
94+
res.append(PAD)
95+
elif leftbits == 4:
96+
res.append(table_b2a_base64[(leftchar & 0xf) << 2])
97+
res.append(PAD)
98+
res.append('\n')
99+
return ''.join(res).encode('ascii')

0 commit comments

Comments
 (0)