From dc80f4fd584243689f42784fb049fe712b1ed8a7 Mon Sep 17 00:00:00 2001 From: slush0 Date: Tue, 17 Feb 2015 23:13:18 +0100 Subject: [PATCH] base64: Ported to MicroPython. --- base64/base64.py | 38 +++++++++++++++++++++----------------- base64/test_base64.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 base64/test_base64.py diff --git a/base64/base64.py b/base64/base64.py index b6e82b69..be01ba97 100644 --- a/base64/base64.py +++ b/base64/base64.py @@ -33,7 +33,8 @@ def _bytes_from_decode_data(s): if isinstance(s, str): try: return s.encode('ascii') - except UnicodeEncodeError: +# except UnicodeEncodeError: + except: raise ValueError('string argument should contain only ASCII characters') elif isinstance(s, bytes_types): return s @@ -109,8 +110,8 @@ def standard_b64decode(s): return b64decode(s) -_urlsafe_encode_translation = bytes.maketrans(b'+/', b'-_') -_urlsafe_decode_translation = bytes.maketrans(b'-_', b'+/') +#_urlsafe_encode_translation = bytes.maketrans(b'+/', b'-_') +#_urlsafe_decode_translation = bytes.maketrans(b'-_', b'+/') def urlsafe_b64encode(s): """Encode a byte string using a url-safe Base64 alphabet. @@ -119,7 +120,8 @@ def urlsafe_b64encode(s): returned. The alphabet uses '-' instead of '+' and '_' instead of '/'. """ - return b64encode(s).translate(_urlsafe_encode_translation) +# return b64encode(s).translate(_urlsafe_encode_translation) + raise NotImplementedError() def urlsafe_b64decode(s): """Decode a byte string encoded with the standard Base64 alphabet. @@ -131,9 +133,10 @@ def urlsafe_b64decode(s): The alphabet uses '-' instead of '+' and '_' instead of '/'. """ - s = _bytes_from_decode_data(s) - s = s.translate(_urlsafe_decode_translation) - return b64decode(s) +# s = _bytes_from_decode_data(s) +# s = s.translate(_urlsafe_decode_translation) +# return b64decode(s) + raise NotImplementedError() @@ -187,13 +190,13 @@ def b32encode(s): ]) # Adjust for any leftover partial quanta if leftover == 1: - encoded[-6:] = b'======' + encoded = encoded[:-6] + b'======' elif leftover == 2: - encoded[-4:] = b'====' + encoded = encoded[:-4] + b'====' elif leftover == 3: - encoded[-3:] = b'===' + encoded = encoded[:-3] + b'===' elif leftover == 4: - encoded[-1:] = b'=' + encoded = encoded[:-1] + b'=' return bytes(encoded) @@ -232,12 +235,13 @@ def b32decode(s, casefold=False, map01=None): # Strip off pad characters from the right. We need to count the pad # characters because this will tell us how many null bytes to remove from # the end of the decoded string. - padchars = 0 - mo = re.search(b'(?P[=]*)$', s) - if mo: - padchars = len(mo.group('pad')) - if padchars > 0: - s = s[:-padchars] + padchars = s.find(b'=') + if padchars > 0: + padchars = len(s) - padchars + s = s[:-padchars] + else: + padchars = 0 + # Now decode the full quanta parts = [] acc = 0 diff --git a/base64/test_base64.py b/base64/test_base64.py new file mode 100644 index 00000000..36abfbfc --- /dev/null +++ b/base64/test_base64.py @@ -0,0 +1,36 @@ +import base64 + +b = base64.b64encode(b'zlutoucky kun upel dabelske ody') +print(b) + +if b != b'emx1dG91Y2t5IGt1biB1cGVsIGRhYmVsc2tlIG9keQ==': + raise Exception("Error") + +d = base64.b64decode(b) +print(d) + +if d != b'zlutoucky kun upel dabelske ody': + raise Exception("Error") + +base64.test() + +binary = b'\x99\x10\xaa' +b = base64.b64encode(binary) +if b != b'mRCq': + raise Exception("Error") + +d = base64.b64decode(b) +print(d) +if d != binary: + raise Exception("Error") + +d = base64.b32encode(b'zlutoucky kun upel dabelske ody') +if d != b'PJWHK5DPOVRWW6JANN2W4IDVOBSWYIDEMFRGK3DTNNSSA33EPE======': + raise Exception("Error") + +print(d) +b = base64.b32decode(d) +if b != b'zlutoucky kun upel dabelske ody': + raise Exception("Error") + +print("OK")