Skip to content

Commit 44e2d86

Browse files
admin-slushpfalcon
authored andcommitted
base64: Ported to MicroPython.
1 parent a07073b commit 44e2d86

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

base64/base64.py

+21-17
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def _bytes_from_decode_data(s):
3333
if isinstance(s, str):
3434
try:
3535
return s.encode('ascii')
36-
except UnicodeEncodeError:
36+
# except UnicodeEncodeError:
37+
except:
3738
raise ValueError('string argument should contain only ASCII characters')
3839
elif isinstance(s, bytes_types):
3940
return s
@@ -109,8 +110,8 @@ def standard_b64decode(s):
109110
return b64decode(s)
110111

111112

112-
_urlsafe_encode_translation = bytes.maketrans(b'+/', b'-_')
113-
_urlsafe_decode_translation = bytes.maketrans(b'-_', b'+/')
113+
#_urlsafe_encode_translation = bytes.maketrans(b'+/', b'-_')
114+
#_urlsafe_decode_translation = bytes.maketrans(b'-_', b'+/')
114115

115116
def urlsafe_b64encode(s):
116117
"""Encode a byte string using a url-safe Base64 alphabet.
@@ -119,7 +120,8 @@ def urlsafe_b64encode(s):
119120
returned. The alphabet uses '-' instead of '+' and '_' instead of
120121
'/'.
121122
"""
122-
return b64encode(s).translate(_urlsafe_encode_translation)
123+
# return b64encode(s).translate(_urlsafe_encode_translation)
124+
raise NotImplementedError()
123125

124126
def urlsafe_b64decode(s):
125127
"""Decode a byte string encoded with the standard Base64 alphabet.
@@ -131,9 +133,10 @@ def urlsafe_b64decode(s):
131133
132134
The alphabet uses '-' instead of '+' and '_' instead of '/'.
133135
"""
134-
s = _bytes_from_decode_data(s)
135-
s = s.translate(_urlsafe_decode_translation)
136-
return b64decode(s)
136+
# s = _bytes_from_decode_data(s)
137+
# s = s.translate(_urlsafe_decode_translation)
138+
# return b64decode(s)
139+
raise NotImplementedError()
137140

138141

139142

@@ -187,13 +190,13 @@ def b32encode(s):
187190
])
188191
# Adjust for any leftover partial quanta
189192
if leftover == 1:
190-
encoded[-6:] = b'======'
193+
encoded = encoded[:-6] + b'======'
191194
elif leftover == 2:
192-
encoded[-4:] = b'===='
195+
encoded = encoded[:-4] + b'===='
193196
elif leftover == 3:
194-
encoded[-3:] = b'==='
197+
encoded = encoded[:-3] + b'==='
195198
elif leftover == 4:
196-
encoded[-1:] = b'='
199+
encoded = encoded[:-1] + b'='
197200
return bytes(encoded)
198201

199202

@@ -232,12 +235,13 @@ def b32decode(s, casefold=False, map01=None):
232235
# Strip off pad characters from the right. We need to count the pad
233236
# characters because this will tell us how many null bytes to remove from
234237
# the end of the decoded string.
235-
padchars = 0
236-
mo = re.search(b'(?P<pad>[=]*)$', s)
237-
if mo:
238-
padchars = len(mo.group('pad'))
239-
if padchars > 0:
240-
s = s[:-padchars]
238+
padchars = s.find(b'=')
239+
if padchars > 0:
240+
padchars = len(s) - padchars
241+
s = s[:-padchars]
242+
else:
243+
padchars = 0
244+
241245
# Now decode the full quanta
242246
parts = []
243247
acc = 0

base64/test_base64.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import base64
2+
3+
b = base64.b64encode(b'zlutoucky kun upel dabelske ody')
4+
print(b)
5+
6+
if b != b'emx1dG91Y2t5IGt1biB1cGVsIGRhYmVsc2tlIG9keQ==':
7+
raise Exception("Error")
8+
9+
d = base64.b64decode(b)
10+
print(d)
11+
12+
if d != b'zlutoucky kun upel dabelske ody':
13+
raise Exception("Error")
14+
15+
base64.test()
16+
17+
binary = b'\x99\x10\xaa'
18+
b = base64.b64encode(binary)
19+
if b != b'mRCq':
20+
raise Exception("Error")
21+
22+
d = base64.b64decode(b)
23+
print(d)
24+
if d != binary:
25+
raise Exception("Error")
26+
27+
d = base64.b32encode(b'zlutoucky kun upel dabelske ody')
28+
if d != b'PJWHK5DPOVRWW6JANN2W4IDVOBSWYIDEMFRGK3DTNNSSA33EPE======':
29+
raise Exception("Error")
30+
31+
print(d)
32+
b = base64.b32decode(d)
33+
if b != b'zlutoucky kun upel dabelske ody':
34+
raise Exception("Error")
35+
36+
print("OK")

0 commit comments

Comments
 (0)