base91_1.py 671 B

1234567891011121314151617181920212223242526
  1. def decode(encoded_str):
  2. ''' Decode Base91 string to a bytearray '''
  3. v = -1
  4. b = 0
  5. n = 0
  6. out = bytearray()
  7. for strletter in encoded_str:
  8. if not strletter in decode_table:
  9. continue
  10. c = decode_table[strletter]
  11. if(v < 0):
  12. v = c
  13. else:
  14. v += c*91
  15. b |= v << n
  16. n += 13 if (v & 8191)>88 else 14
  17. while True:
  18. out += struct.pack('B', b&255)
  19. b >>= 8
  20. n -= 8
  21. if not n>7:
  22. break
  23. v = -1
  24. if v+1:
  25. out += struct.pack('B', (b | v << n) & 255 )
  26. return out