2 # the decoder works off a reader
3 # the encoder returns bytearray
7 return bytearray(h.decode('hex'))
11 if type(b) in (str, str):
12 return "".join([hex(ord(c))[2:].zfill(2) for c in b])
14 return bytes2hex(b.decode())
17 # expects uvarint64 (no crazy big nums!)
29 def encode_big_endian(i, size):
32 return encode_big_endian(i // 256, size - 1) + bytearray([i % 256])
35 def decode_big_endian(reader, size):
38 firstByte = reader.read(1)[0]
39 return firstByte * (256 ** (size - 1)) + decode_big_endian(reader, size - 1)
41 # ints are max 16 bytes long
49 size = uvarint_size(i)
52 big_end = encode_big_endian(i, size)
55 return bytearray([size]) + big_end
57 # returns the int and whats left of the byte array
60 def decode_varint(reader):
61 size = reader.read(1)[0]
65 negate = True if size > int(0xF0) else False
68 i = decode_big_endian(reader, size)
75 size = encode_varint(len(s))
76 return size + bytearray(s, 'utf8')
79 def decode_string(reader):
80 length = decode_varint(reader)
81 raw_data = reader.read(length)
82 return raw_data.decode()
87 list(map(b.extend, list(map(encode, s))))
88 return encode_varint(len(s)) + b
92 print('encoding', repr(s))
95 if isinstance(s, int):
96 return encode_varint(s)
97 elif isinstance(s, str):
98 return encode_string(s)
99 elif isinstance(s, list):
100 return encode_list(s)
101 elif isinstance(s, bytearray):
102 return encode_string(s)
104 print("UNSUPPORTED TYPE!", type(s), s)
107 if __name__ == '__main__':
108 ns = [100, 100, 1000, 256]
110 bs = list(map(encode_big_endian, ns, ss))
111 ds = list(map(decode_big_endian, bs, ss))
113 print([i[0] for i in ds])
115 ss = ["abc", "hi there jim", "ok now what"]
116 e = list(map(encode_string, ss))
117 d = list(map(decode_string, e))
119 print([i[0] for i in d])