z85.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """Python implementation of Z85 85-bit encoding
  2. Z85 encoding is a plaintext encoding for a bytestring interpreted as 32bit integers.
  3. Since the chunks are 32bit, a bytestring must be a multiple of 4 bytes.
  4. See ZMQ RFC 32 for details.
  5. """
  6. # Copyright (C) PyZMQ Developers
  7. # Distributed under the terms of the Modified BSD License.
  8. from __future__ import annotations
  9. import struct
  10. # Z85CHARS is the base 85 symbol table
  11. Z85CHARS = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#"
  12. # Z85MAP maps integers in [0,84] to the appropriate character in Z85CHARS
  13. Z85MAP = {c: idx for idx, c in enumerate(Z85CHARS)}
  14. _85s = [85**i for i in range(5)][::-1]
  15. def encode(rawbytes):
  16. """encode raw bytes into Z85"""
  17. # Accepts only byte arrays bounded to 4 bytes
  18. if len(rawbytes) % 4:
  19. raise ValueError(f"length must be multiple of 4, not {len(rawbytes)}")
  20. nvalues = len(rawbytes) // 4
  21. values = struct.unpack(f'>{nvalues:d}I', rawbytes)
  22. encoded = []
  23. for v in values:
  24. for offset in _85s:
  25. encoded.append(Z85CHARS[(v // offset) % 85])
  26. return bytes(encoded)
  27. def decode(z85bytes):
  28. """decode Z85 bytes to raw bytes, accepts ASCII string"""
  29. if isinstance(z85bytes, str):
  30. try:
  31. z85bytes = z85bytes.encode('ascii')
  32. except UnicodeEncodeError:
  33. raise ValueError('string argument should contain only ASCII characters')
  34. if len(z85bytes) % 5:
  35. raise ValueError(f"Z85 length must be multiple of 5, not {len(z85bytes)}")
  36. nvalues = len(z85bytes) // 5
  37. values = []
  38. for i in range(0, len(z85bytes), 5):
  39. value = 0
  40. for j, offset in enumerate(_85s):
  41. value += Z85MAP[z85bytes[i + j]] * offset
  42. values.append(value)
  43. return struct.pack(f'>{nvalues:d}I', *values)