You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.4 KiB

  1. # the decoder works off a reader
  2. # the encoder returns bytearray
  3. def hex2bytes(h):
  4. return bytearray(h.decode('hex'))
  5. def bytes2hex(b):
  6. if type(b) in (str, unicode):
  7. return "".join([hex(ord(c))[2:].zfill(2) for c in b])
  8. else:
  9. return bytes2hex(b.decode())
  10. # expects uvarint64 (no crazy big nums!)
  11. def uvarint_size(i):
  12. if i == 0:
  13. return 0
  14. for j in xrange(1, 8):
  15. if i < 1 << j * 8:
  16. return j
  17. return 8
  18. # expects i < 2**size
  19. def encode_big_endian(i, size):
  20. if size == 0:
  21. return bytearray()
  22. return encode_big_endian(i / 256, size - 1) + bytearray([i % 256])
  23. def decode_big_endian(reader, size):
  24. if size == 0:
  25. return 0
  26. firstByte = reader.read(1)[0]
  27. return firstByte * (256 ** (size - 1)) + decode_big_endian(reader, size - 1)
  28. # ints are max 16 bytes long
  29. def encode_varint(i):
  30. negate = False
  31. if i < 0:
  32. negate = True
  33. i = -i
  34. size = uvarint_size(i)
  35. if size == 0:
  36. return bytearray([0])
  37. big_end = encode_big_endian(i, size)
  38. if negate:
  39. size += 0xF0
  40. return bytearray([size]) + big_end
  41. # returns the int and whats left of the byte array
  42. def decode_varint(reader):
  43. size = reader.read(1)[0]
  44. if size == 0:
  45. return 0
  46. negate = True if size > int(0xF0) else False
  47. if negate:
  48. size = size - 0xF0
  49. i = decode_big_endian(reader, size)
  50. if negate:
  51. i = i * (-1)
  52. return i
  53. def encode_string(s):
  54. size = encode_varint(len(s))
  55. return size + bytearray(s)
  56. def decode_string(reader):
  57. length = decode_varint(reader)
  58. return str(reader.read(length))
  59. def encode_list(s):
  60. b = bytearray()
  61. map(b.extend, map(encode, s))
  62. return encode_varint(len(s)) + b
  63. def encode(s):
  64. if s is None:
  65. return bytearray()
  66. if isinstance(s, int):
  67. return encode_varint(s)
  68. elif isinstance(s, str):
  69. return encode_string(s)
  70. elif isinstance(s, list):
  71. return encode_list(s)
  72. else:
  73. print "UNSUPPORTED TYPE!", type(s), s
  74. if __name__ == '__main__':
  75. ns = [100, 100, 1000, 256]
  76. ss = [2, 5, 5, 2]
  77. bs = map(encode_big_endian, ns, ss)
  78. ds = map(decode_big_endian, bs, ss)
  79. print ns
  80. print [i[0] for i in ds]
  81. ss = ["abc", "hi there jim", "ok now what"]
  82. e = map(encode_string, ss)
  83. d = map(decode_string, e)
  84. print ss
  85. print [i[0] for i in d]