2014-10-01 09:21:11 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# bencode.py python3 compatable bencode / bdecode
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2016-08-30 08:16:18 +00:00
|
|
|
class Decoder(object):
|
|
|
|
|
|
|
|
def _decode_int(self):
|
|
|
|
"""
|
|
|
|
decode integer from bytearray
|
|
|
|
return int
|
|
|
|
"""
|
|
|
|
self.idx += 1
|
|
|
|
start = self.idx
|
|
|
|
end = self.data.index(b'e', self.idx)
|
|
|
|
self.idx = end + 1
|
|
|
|
return int(self.data[start:end])
|
|
|
|
|
|
|
|
def _decode_str(self):
|
|
|
|
"""
|
|
|
|
decode string from bytearray
|
|
|
|
return string
|
|
|
|
"""
|
|
|
|
start = self.data.index(b':', self.idx)
|
|
|
|
l = int(self.data[self.idx:start].decode(), 10)
|
2016-10-26 21:38:00 +00:00
|
|
|
if l < 0:
|
2016-08-30 08:16:18 +00:00
|
|
|
raise Exception('invalid string size: %d' % l)
|
|
|
|
start += 1
|
|
|
|
ret = self.data[start:start+l]
|
|
|
|
try:
|
|
|
|
ret = ret.decode('utf-8')
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
self.idx = start + l
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def _decode_list(self):
|
|
|
|
"""
|
|
|
|
decode list from bytearray
|
|
|
|
return list
|
|
|
|
"""
|
|
|
|
ls = []
|
|
|
|
self.idx += 1
|
|
|
|
while self.data[self.idx] != ord(b'e'):
|
|
|
|
ls.append(self._decode())
|
|
|
|
self.idx += 1
|
|
|
|
return ls
|
|
|
|
|
|
|
|
def _decode_dict(self):
|
|
|
|
"""
|
|
|
|
decode dict from bytearray
|
|
|
|
return dict
|
|
|
|
"""
|
2016-08-30 09:16:02 +00:00
|
|
|
d = {}
|
2016-08-30 08:16:18 +00:00
|
|
|
self.idx += 1
|
|
|
|
while self.data[self.idx] != ord(b'e'):
|
|
|
|
k = self._decode_str()
|
|
|
|
v = self._decode()
|
|
|
|
d[k] = v
|
|
|
|
self.idx += 1
|
|
|
|
return d
|
|
|
|
|
|
|
|
def _decode(self):
|
|
|
|
ch = chr(self.data[self.idx])
|
|
|
|
if ch == 'l':
|
|
|
|
return self._decode_list()
|
|
|
|
elif ch == 'i':
|
|
|
|
return self._decode_int()
|
|
|
|
elif ch == 'd':
|
|
|
|
return self._decode_dict()
|
|
|
|
elif ch.isdigit():
|
|
|
|
return self._decode_str()
|
|
|
|
else:
|
|
|
|
raise Exception('could not decode data: %s' % data)
|
|
|
|
|
|
|
|
def decode(self, data):
|
|
|
|
self.idx = 0
|
|
|
|
self.data = data
|
|
|
|
obj = self._decode()
|
|
|
|
if len(data) != self.idx:
|
|
|
|
raise Exception('failed to decode, extra data: %s' % data)
|
|
|
|
return obj
|
2014-10-01 09:21:11 +00:00
|
|
|
|
|
|
|
def bdecode(data):
|
|
|
|
"""
|
|
|
|
decode a bytearray
|
2016-08-30 08:16:18 +00:00
|
|
|
return decoded object
|
2014-10-01 09:21:11 +00:00
|
|
|
"""
|
2016-08-30 08:16:18 +00:00
|
|
|
return Decoder().decode(data)
|
2014-10-01 09:21:11 +00:00
|
|
|
|
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-08-30 08:16:18 +00:00
|
|
|
|
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')
|
2016-08-30 08:16:18 +00:00
|
|
|
for k in sorted(d):
|
2016-09-17 15:20:17 +00:00
|
|
|
if not isinstance(k, (bytes, str)):
|
2016-08-30 08:16:18 +00:00
|
|
|
k = str(k)
|
|
|
|
_encode(k, buff)
|
2016-06-08 13:32:46 +00:00
|
|
|
_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)
|
2016-10-26 21:38:00 +00:00
|
|
|
elif isinstance(obj, int):
|
|
|
|
_encode_int(obj, buff)
|
2016-06-08 13:32:46 +00:00
|
|
|
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-10-26 21:38:00 +00:00
|
|
|
raise Exception('non serializable object: %s [%s]' % (obj, type(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)
|