2014-10-01 09:21:11 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# bencode.py python3 compatable bencode / bdecode
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
def _decode_int(data):
|
|
|
|
"""
|
|
|
|
decode integer from bytearray
|
|
|
|
return int, remaining data
|
|
|
|
"""
|
|
|
|
data = data[1:]
|
|
|
|
end = data.index(b'e')
|
2016-06-08 13:32:46 +00:00
|
|
|
return int(data[:end], 10), data[end+1:]
|
2014-10-01 09:21:11 +00:00
|
|
|
|
|
|
|
def _decode_str(data):
|
|
|
|
"""
|
|
|
|
decode string from bytearray
|
|
|
|
return string, remaining data
|
|
|
|
"""
|
|
|
|
start = data.index(b':')
|
2016-06-08 13:32:46 +00:00
|
|
|
l = int(data[:start].decode(), 10)
|
2014-10-01 09:21:11 +00:00
|
|
|
if l <= 0:
|
2016-06-08 13:32:46 +00:00
|
|
|
raise Exception('invalid string size: %d' % l)
|
2014-10-01 09:21:11 +00:00
|
|
|
start += 1
|
|
|
|
ret = bytes(data[start:start+l])
|
|
|
|
data = data[start+l:]
|
|
|
|
return ret, data
|
|
|
|
|
|
|
|
def _decode_list(data):
|
|
|
|
"""
|
|
|
|
decode list from bytearray
|
|
|
|
return list, remaining data
|
|
|
|
"""
|
|
|
|
ls = []
|
|
|
|
data = data[1:]
|
|
|
|
while data[0] != ord(b'e'):
|
|
|
|
elem, data = _decode(data)
|
|
|
|
ls.append(elem)
|
|
|
|
return ls, data[1:]
|
|
|
|
|
|
|
|
def _decode_dict(data):
|
|
|
|
"""
|
|
|
|
decode dict from bytearray
|
|
|
|
return dict, remaining data
|
|
|
|
"""
|
|
|
|
d = {}
|
|
|
|
data = data[1:]
|
|
|
|
while data[0] != ord(b'e'):
|
|
|
|
k, data = _decode_str(data)
|
|
|
|
v, data = _decode(data)
|
|
|
|
d[k.decode()] = v
|
|
|
|
return d, data[1:]
|
|
|
|
|
|
|
|
def _decode(data):
|
|
|
|
"""
|
|
|
|
decode a bytearray
|
|
|
|
return deserialized object, remaining data
|
|
|
|
"""
|
|
|
|
ch = chr(data[0])
|
|
|
|
if ch == 'l':
|
|
|
|
return _decode_list(data)
|
|
|
|
elif ch == 'i':
|
|
|
|
return _decode_int(data)
|
|
|
|
elif ch == 'd':
|
|
|
|
return _decode_dict(data)
|
|
|
|
elif ch.isdigit():
|
|
|
|
return _decode_str(data)
|
|
|
|
else:
|
2016-06-08 13:32:46 +00:00
|
|
|
raise Exception('could not deserialize data: %s' % data)
|
2014-10-01 09:21:11 +00:00
|
|
|
|
|
|
|
def bdecode(data):
|
|
|
|
"""
|
|
|
|
decode a bytearray
|
|
|
|
return deserialized object
|
|
|
|
"""
|
2016-06-08 13:32:46 +00:00
|
|
|
obj, data = _decode(data)
|
2014-10-01 09:21:11 +00:00
|
|
|
if len(data) > 0:
|
2016-06-08 13:32:46 +00:00
|
|
|
raise Exception('failed to deserialize, extra data: %s' % data)
|
2014-10-01 09:21:11 +00:00
|
|
|
return obj
|
|
|
|
|
2016-06-08 13:32:46 +00:00
|
|
|
def _encode_str(s, buff):
|
2014-10-01 09:21:11 +00:00
|
|
|
"""
|
|
|
|
encode string to a buffer
|
|
|
|
"""
|
|
|
|
s = bytearray(s)
|
|
|
|
l = len(s)
|
2016-06-08 13:32:46 +00:00
|
|
|
buff.append(bytearray(str(l)+':', 'utf-8'))
|
2014-10-01 09:21:11 +00:00
|
|
|
buff.append(s)
|
|
|
|
|
2016-06-08 13:32:46 +00:00
|
|
|
def _encode_int(i, buff):
|
2014-10-01 09:21:11 +00:00
|
|
|
"""
|
|
|
|
encode integer to a buffer
|
|
|
|
"""
|
|
|
|
buff.append(b'i')
|
2016-06-08 13:32:46 +00:00
|
|
|
buff.append(bytearray(str(i), 'ascii'))
|
2014-10-01 09:21:11 +00:00
|
|
|
buff.append(b'e')
|
|
|
|
|
2016-06-08 13:32:46 +00:00
|
|
|
def _encode_list(l, buff):
|
2014-10-01 09:21:11 +00:00
|
|
|
"""
|
|
|
|
encode list of elements to a buffer
|
|
|
|
"""
|
|
|
|
buff.append(b'l')
|
|
|
|
for i in l:
|
2016-06-08 13:32:46 +00:00
|
|
|
_encode(i, buff)
|
2014-10-01 09:21:11 +00:00
|
|
|
buff.append(b'e')
|
|
|
|
|
2016-06-08 13:32:46 +00:00
|
|
|
def _encode_dict(d, buff):
|
2014-10-01 09:21:11 +00:00
|
|
|
"""
|
|
|
|
encode dict
|
|
|
|
"""
|
|
|
|
buff.append(b'd')
|
|
|
|
l = list(d.keys())
|
|
|
|
l.sort()
|
|
|
|
for k in l:
|
2016-06-08 13:32:46 +00:00
|
|
|
_encode(str(k), buff)
|
|
|
|
_encode(d[k], buff)
|
2014-10-01 09:21:11 +00:00
|
|
|
buff.append(b'e')
|
|
|
|
|
2016-06-08 13:32:46 +00:00
|
|
|
def _encode(obj, buff):
|
2014-10-01 09:21:11 +00:00
|
|
|
"""
|
|
|
|
encode element obj to a buffer buff
|
|
|
|
"""
|
2016-06-08 13:32:46 +00:00
|
|
|
if isinstance(obj, str):
|
|
|
|
_encode_str(bytearray(obj, 'utf-8'), buff)
|
|
|
|
elif isinstance(obj, bytes):
|
|
|
|
_encode_str(bytearray(obj), buff)
|
|
|
|
elif isinstance(obj, bytearray):
|
|
|
|
_encode_str(obj, buff)
|
2014-10-01 09:21:11 +00:00
|
|
|
elif str(obj).isdigit():
|
2016-06-08 13:32:46 +00:00
|
|
|
_encode_int(obj, buff)
|
|
|
|
elif isinstance(obj, list):
|
|
|
|
_encode_list(obj, buff)
|
|
|
|
elif hasattr(obj, 'keys') and hasattr(obj, 'values'):
|
|
|
|
_encode_dict(obj, buff)
|
|
|
|
elif str(obj) in ['True', 'False']:
|
|
|
|
_encode_int(int(obj and '1' or '0'), buff)
|
2014-10-01 09:21:11 +00:00
|
|
|
else:
|
2016-06-08 13:32:46 +00:00
|
|
|
raise Exception('non serializable object: %s' % obj)
|
2014-10-01 09:21:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def bencode(obj):
|
|
|
|
"""
|
|
|
|
bencode element, return bytearray
|
|
|
|
"""
|
|
|
|
buff = []
|
2016-06-08 13:32:46 +00:00
|
|
|
_encode(obj, buff)
|
|
|
|
ret = bytearray()
|
2014-10-01 09:21:11 +00:00
|
|
|
for ba in buff:
|
2016-06-08 13:32:46 +00:00
|
|
|
ret += ba
|
2014-10-01 09:21:11 +00:00
|
|
|
return bytes(ret)
|