Blob Blame History Raw
From d1ed27e8f776140846b591ee7d09cc95a3fa7386 Mon Sep 17 00:00:00 2001
From: Dan Callaghan <dcallagh@redhat.com>
Date: Mon, 27 Aug 2018 11:30:44 +1000
Subject: [PATCH 2/2] expect binary data to be bytes not str

---
 zbase32/test/test_zbase32.py | 10 +++++-----
 zbase32/zbase32.py           | 17 +++++++++--------
 2 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/zbase32/test/test_zbase32.py b/zbase32/test/test_zbase32.py
index 31dead4..e14cfdb 100644
--- a/zbase32/test/test_zbase32.py
+++ b/zbase32/test/test_zbase32.py
@@ -15,7 +15,7 @@
 from zbase32 import b2a
 
 def insecurerandstr(n):
-    return ''.join(map(chr, map(random.randrange, [0]*n, [256]*n)))
+    return bytes(map(random.randrange, [0]*n, [256]*n))
 
 class base32TestCase(unittest.TestCase):
     def test_ende(self):
@@ -77,10 +77,10 @@ def test_odd_sizes(self):
             bs = insecurerandstr((lib+7)//8)
             # zero-out unused least-sig bits
             if lib%8:
-                b=ord(bs[-1])
+                b = bs[-1]
                 b = b >> (8 - (lib%8))
                 b = b << (8 - (lib%8))
-                bs = bs[:-1] + chr(b)
+                bs = bs[:-1] + bytes([b])
             asciis = b2a_l(bs, lib)
             assert len(asciis) == (lib+4)//5 # the size of the base-32 encoding must be just right
             asciisl = b2a_l_long(bs, lib)
@@ -109,10 +109,10 @@ def test_could_be(self):
             bs = insecurerandstr((bitl+7)//8)
             # zero-out unused least-sig bits
             if bitl%8:
-                b=ord(bs[-1])
+                b = bs[-1]
                 b = b >> (8 - (bitl%8))
                 b = b << (8 - (bitl%8))
-                bs = bs[:-1] + chr(b)
+                bs = bs[:-1] + bytes([b])
             assert could_be_base32_encoded_l(b2a_l(bs, bitl), bitl)
 
         # anything with a bogus character couldn't be
diff --git a/zbase32/zbase32.py b/zbase32/zbase32.py
index c840882..00329d0 100644
--- a/zbase32/zbase32.py
+++ b/zbase32/zbase32.py
@@ -94,7 +94,7 @@ def add_upcase(s, upcasetranstable=upcasetranstable):
 
 def b2a(os):
     """
-    @param os the data to be encoded (a string)
+    @param os the data to be encoded (bytes)
 
     @return the contents of os in base-32 encoded form
     """
@@ -102,7 +102,7 @@ def b2a(os):
 
 def b2a_l(os, lengthinbits):
     """
-    @param os the data to be encoded (a string)
+    @param os the data to be encoded (bytes)
     @param lengthinbits the number of bits of data in os to be encoded
 
     b2a_l() will generate a base-32 encoded string big enough to encode
@@ -124,11 +124,12 @@ def b2a_l(os, lengthinbits):
 
     @return the contents of os in base-32 encoded form
     """
+    precondition(not isinstance(os, str), "os is required to be bytes-like")
     precondition(isinstance(lengthinbits, int), "lengthinbits is required to be an integer.", lengthinbits=lengthinbits)
     precondition(div_ceil(lengthinbits, 8) == len(os), "lengthinbits is required to specify a number of bits storable in exactly len(os) octets.", lengthinbits=lengthinbits, lenos=len(os))
     # precondition((lengthinbits % 8==0) or ((ord(os[-1]) % (2**(8-(lengthinbits%8))))==0), "Any unused least-significant bits in os are required to be zero bits.", ord(os[-1]), lengthinbits=lengthinbits) # removing this precondition, because I like to use it with random os, like this: base32.b2a_l(file("/dev/urandom", "r").read(9), 65)
 
-    os = [ord(o) for o in os]
+    os = bytearray(os)
 
     numquintets = div_ceil(lengthinbits, 5)
     numoctetsofdata = div_ceil(lengthinbits, 8)
@@ -238,7 +239,7 @@ def a2b_l(cs, lengthinbits):
         num = num * 256
         pos = pos * 256
     assert len(octets) == numoctets, "len(octets): %s, numoctets: %s, octets: %s" % (len(octets), numoctets, octets,)
-    res = ''.join([chr(o) for o in octets])
+    res = bytes(octets)
     precondition(b2a_l(res, lengthinbits) == cs, "cs is required to be the canonical base-32 encoding of some data.", b2a(res), res=res, cs=cs)
     return res
 
@@ -308,7 +309,7 @@ def b2a_long(os):
 def b2a_l_long(os, lengthinbits):
     origos = os # for postcondition assertions
 
-    os = [ord(o) for o in os]
+    os = bytearray(os)
 
     numquintets = (lengthinbits+4)//5
     numoctetsofdata = (lengthinbits+7)//8
@@ -382,7 +383,7 @@ def a2b_l_long(cs, lengthinbits):
             num = num - (octet * CUTOFF)
             num = num * 256
     octets = octets[:numoctets]
-    res = ''.join([chr(o) for o in octets])
+    res = bytes(octets)
     precondition(b2a_l(res, lengthinbits) == cs, "cs is required to be the canonical base-32 encoding of some data.", b2a(res), res=res, cs=cs)
     return res
 
@@ -390,7 +391,7 @@ def trimnpad(os, lengthinbits):
     """
     @return a string derived from os but containing exactly lengthinbits data bits -- if lengthinbits is less than the number of bits contained in os then the trailing unused bits will be zeroed out, and if lengthinbits is greater than the number of bits contained in os then extra zero bytes will be appended
     """
-    os = [ord(o) for o in os]
+    os = bytearray(os)
     mod8 = lengthinbits % 8
     div8 = lengthinbits // 8
     numos = div8 + (mod8 != 0)
@@ -404,7 +405,7 @@ def trimnpad(os, lengthinbits):
     else:
         # append zero octets for padding
         os.extend([0]*(numos-len(os)))
-    return ''.join([chr(o) for o in os])
+    return bytes(os)
 
 
 # Now we'll override the Python functions with the compiled functions if they are not None.
-- 
2.17.1