chiark / gitweb /
Reformat
authorSunDwarf <eyesismine@gmail.com>
Fri, 15 Jan 2016 16:58:49 +0000 (16:58 +0000)
committerSunDwarf <eyesismine@gmail.com>
Fri, 15 Jan 2016 16:58:49 +0000 (16:58 +0000)
base91.py

index cd279bc18faba3ca9754795ec06b95c65188ede5..d6541314f61ff97009b5ba8b88a5180603dba39a 100644 (file)
--- a/base91.py
+++ b/base91.py
 import struct
 
 base91_alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
-       'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
-       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
-       'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
-       '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '#', '$',
-       '%', '&', '(', ')', '*', '+', ',', '.', '/', ':', ';', '<', '=',
-       '>', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~', '"']
+                   'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+                   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
+                   'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+                   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '#', '$',
+                   '%', '&', '(', ')', '*', '+', ',', '.', '/', ':', ';', '<', '=',
+                   '>', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~', '"']
+
+decode_table = dict((v, k) for k, v in enumerate(base91_alphabet))
 
-decode_table = dict((v,k) for k,v in enumerate(base91_alphabet))
 
 def decode(encoded_str):
     ''' Decode Base91 string to a bytearray '''
@@ -49,33 +50,34 @@ def decode(encoded_str):
         if not strletter in decode_table:
             continue
         c = decode_table[strletter]
-        if(v < 0):
+        if (v < 0):
             v = c
         else:
-            v += c*91
+            v += c * 91
             b |= v << n
-            n += 13 if (v & 8191)>88 else 14
+            n += 13 if (v & 8191) > 88 else 14
             while True:
-                out += struct.pack('B', b&255)
+                out += struct.pack('B', b & 255)
                 b >>= 8
                 n -= 8
-                if not n>7:
+                if not n > 7:
                     break
             v = -1
-    if v+1:
-        out += struct.pack('B', (b | v << n) & 255 )
+    if v + 1:
+        out += struct.pack('B', (b | v << n) & 255)
     return out
 
+
 def encode(bindata):
     ''' Encode a bytearray to a Base91 string '''
     b = 0
     n = 0
     out = ''
     for count in range(len(bindata)):
-        byte = bindata[count:count+1]
+        byte = bindata[count:count + 1]
         b |= struct.unpack('B', byte)[0] << n
         n += 8
-        if n>13:
+        if n > 13:
             v = b & 8191
             if v > 88:
                 b >>= 13
@@ -87,6 +89,6 @@ def encode(bindata):
             out += base91_alphabet[v % 91] + base91_alphabet[v // 91]
     if n:
         out += base91_alphabet[b % 91]
-        if n>7 or b>90:
+        if n > 7 or b > 90:
             out += base91_alphabet[b // 91]
     return out