lbry-sdk/lbrynet/dht/encoding.py

142 lines
5 KiB
Python
Raw Normal View History

2017-10-10 19:08:22 +02:00
from error import DecodeError
2015-08-20 17:27:15 +02:00
2017-03-31 19:32:43 +02:00
2015-08-20 17:27:15 +02:00
class Encoding(object):
""" Interface for RPC message encoders/decoders
2016-12-14 00:08:29 +01:00
2015-08-20 17:27:15 +02:00
All encoding implementations used with this library should inherit and
implement this.
"""
2017-03-31 19:32:43 +02:00
2015-08-20 17:27:15 +02:00
def encode(self, data):
""" Encode the specified data
2016-12-14 00:08:29 +01:00
2015-08-20 17:27:15 +02:00
@param data: The data to encode
This method has to support encoding of the following
types: C{str}, C{int} and C{long}
Any additional data types may be supported as long as the
implementing class's C{decode()} method can successfully
decode them.
2016-12-14 00:08:29 +01:00
2015-08-20 17:27:15 +02:00
@return: The encoded data
@rtype: str
"""
2017-03-31 19:32:43 +02:00
2015-08-20 17:27:15 +02:00
def decode(self, data):
""" Decode the specified data string
2016-12-14 00:08:29 +01:00
2015-08-20 17:27:15 +02:00
@param data: The data (byte string) to decode.
@type data: str
2016-12-14 00:08:29 +01:00
2015-08-20 17:27:15 +02:00
@return: The decoded data (in its correct type)
"""
2017-03-31 19:32:43 +02:00
2015-08-20 17:27:15 +02:00
class Bencode(Encoding):
""" Implementation of a Bencode-based algorithm (Bencode is the encoding
algorithm used by Bittorrent).
2016-12-14 00:08:29 +01:00
2015-08-20 17:27:15 +02:00
@note: This algorithm differs from the "official" Bencode algorithm in
that it can encode/decode floating point values in addition to
integers.
"""
2016-12-14 00:08:29 +01:00
2015-08-20 17:27:15 +02:00
def encode(self, data):
""" Encoder implementation of the Bencode algorithm
2016-12-14 00:08:29 +01:00
2015-08-20 17:27:15 +02:00
@param data: The data to encode
@type data: int, long, tuple, list, dict or str
2016-12-14 00:08:29 +01:00
2015-08-20 17:27:15 +02:00
@return: The encoded data
@rtype: str
"""
2017-04-25 20:31:05 +02:00
if isinstance(data, (int, long)):
2015-08-20 17:27:15 +02:00
return 'i%de' % data
2017-04-25 20:31:05 +02:00
elif isinstance(data, str):
2015-08-20 17:27:15 +02:00
return '%d:%s' % (len(data), data)
2017-04-25 20:31:05 +02:00
elif isinstance(data, (list, tuple)):
2015-08-20 17:27:15 +02:00
encodedListItems = ''
for item in data:
encodedListItems += self.encode(item)
return 'l%se' % encodedListItems
2017-04-25 20:31:05 +02:00
elif isinstance(data, dict):
2015-08-20 17:27:15 +02:00
encodedDictItems = ''
keys = data.keys()
keys.sort()
for key in keys:
encodedDictItems += self.encode(key)
encodedDictItems += self.encode(data[key])
return 'd%se' % encodedDictItems
2017-04-25 20:31:05 +02:00
elif isinstance(data, float):
2016-12-14 00:08:29 +01:00
# This (float data type) is a non-standard extension to the original Bencode algorithm
2015-08-20 17:27:15 +02:00
return 'f%fe' % data
2017-04-25 20:21:13 +02:00
elif data is None:
2016-11-30 21:20:45 +01:00
# This (None/NULL data type) is a non-standard extension
# to the original Bencode algorithm
2015-08-20 17:27:15 +02:00
return 'n'
else:
print data
2017-04-25 20:31:05 +02:00
raise TypeError("Cannot bencode '%s' object" % type(data))
2016-12-14 00:08:29 +01:00
2015-08-20 17:27:15 +02:00
def decode(self, data):
2016-12-14 00:08:29 +01:00
""" Decoder implementation of the Bencode algorithm
2015-08-20 17:27:15 +02:00
@param data: The encoded data
@type data: str
2016-12-14 00:08:29 +01:00
2015-08-20 17:27:15 +02:00
@note: This is a convenience wrapper for the recursive decoding
algorithm, C{_decodeRecursive}
2016-12-14 00:08:29 +01:00
2015-08-20 17:27:15 +02:00
@return: The decoded data, as a native Python type
@rtype: int, list, dict or str
"""
if len(data) == 0:
2017-04-25 20:31:05 +02:00
raise DecodeError('Cannot decode empty string')
try:
return self._decodeRecursive(data)[0]
except ValueError as e:
2017-04-25 20:31:05 +02:00
raise DecodeError(e.message)
2016-12-14 00:08:29 +01:00
2015-08-20 17:27:15 +02:00
@staticmethod
def _decodeRecursive(data, startIndex=0):
""" Actual implementation of the recursive Bencode algorithm
2016-12-14 00:08:29 +01:00
2015-08-20 17:27:15 +02:00
Do not call this; use C{decode()} instead
"""
if data[startIndex] == 'i':
2017-03-31 19:32:43 +02:00
endPos = data[startIndex:].find('e') + startIndex
2017-04-25 20:31:05 +02:00
return int(data[startIndex + 1:endPos]), endPos + 1
2015-08-20 17:27:15 +02:00
elif data[startIndex] == 'l':
startIndex += 1
decodedList = []
while data[startIndex] != 'e':
listData, startIndex = Bencode._decodeRecursive(data, startIndex)
decodedList.append(listData)
2017-04-25 20:31:05 +02:00
return decodedList, startIndex + 1
2015-08-20 17:27:15 +02:00
elif data[startIndex] == 'd':
startIndex += 1
decodedDict = {}
while data[startIndex] != 'e':
key, startIndex = Bencode._decodeRecursive(data, startIndex)
value, startIndex = Bencode._decodeRecursive(data, startIndex)
decodedDict[key] = value
2017-04-25 20:31:05 +02:00
return decodedDict, startIndex
2015-08-20 17:27:15 +02:00
elif data[startIndex] == 'f':
# This (float data type) is a non-standard extension to the original Bencode algorithm
2017-03-31 19:32:43 +02:00
endPos = data[startIndex:].find('e') + startIndex
2017-04-25 20:31:05 +02:00
return float(data[startIndex + 1:endPos]), endPos + 1
2015-08-20 17:27:15 +02:00
elif data[startIndex] == 'n':
2016-11-30 21:20:45 +01:00
# This (None/NULL data type) is a non-standard extension
# to the original Bencode algorithm
2017-04-25 20:31:05 +02:00
return None, startIndex + 1
2015-08-20 17:27:15 +02:00
else:
2017-03-31 19:32:43 +02:00
splitPos = data[startIndex:].find(':') + startIndex
2015-08-20 17:27:15 +02:00
try:
length = int(data[startIndex:splitPos])
except ValueError, e:
raise DecodeError, e
2017-03-31 19:32:43 +02:00
startIndex = splitPos + 1
endPos = startIndex + length
2015-08-20 17:27:15 +02:00
bytes = data[startIndex:endPos]
2017-04-25 20:31:05 +02:00
return bytes, endPos