-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbencode.py
More file actions
165 lines (141 loc) · 5.08 KB
/
bencode.py
File metadata and controls
165 lines (141 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Author: Bilal Qandeel
# Date: 2020-02-07
class UnsupportedDataTypeException(Exception):
def __init__(self, *args):
if args:
self.message = args[0]
else:
self.message = None
def __str__(self, *args):
return "Unsupported data type " + self.message
class InvalidBencode(Exception):
def __init__(self, *args):
if args:
self.message = args[0]
else:
self.message = None
def __str__(self):
return "Unable to resolve invalid Bencode [" + self.message + "]"
def encode(element):
"""
:param element: the element required to be bencoded
:return: the bencoded string
"""
return_code = ""
if type(element) is int:
return_code += "i" + str(int(element)) + "e"
elif type(element) is str:
return_code += str(len(element)) + ":" + str(element)
elif type(element) is list:
for value in element:
return_code += encode(value)
return "l" + return_code + "e"
elif type(element) is dict:
for key, value in element.items():
return_code += encode(key)
return_code += encode(value)
return "d" + return_code + "e"
else:
raise UnsupportedDataTypeException(str(type(element)))
return return_code
def process_dict(content):
if content.strip() == "":
return ""
original_content = content
exclusion_zone = []
end = content.find("}")
begin = content.rfind("{", 0, end)
while end != -1:
content = content[0:begin] + content[begin + 1: end].replace("{", "<").replace("}", ">") + content[end + 1:]
exclusion_zone.extend(list(range(begin, end)))
end = content.find("}")
begin = content.find("{", 0, end)
end = content.find("]")
begin = content.rfind("[", 0, end)
while end != -1:
content = content[0:begin] + content[begin + 1: end].replace("[", "<").replace("]", ">") + content[end + 1:]
exclusion_zone.extend(list(range(begin, end)))
end = content.find("]")
begin = content.find("[", 0, end)
content = original_content
at = 0
token = content[at]
token_order = 0
while at < len(content)-1:
if token == "," and at not in exclusion_zone:
if (token_order % 2) == 0:
content = content[0:at] + ":" + content[at + 1:]
token_order += 1
at += 1
token = content[at]
else:
at += 1
token = content[at]
return content
def decode(code):
try:
if code.strip() == "":
return ""
result = code
# # Process texts # #
placeholder = []
placeholder_counter = -1
token = result.find(":")
while token != -1:
left_to_token = token-1
while result[left_to_token:token].isdigit():
left_to_token -= 1
length = int(result[left_to_token + 1:token])
text = result[token+1:token+1+length]
# Encode texts so they won't interfere with `d`, `l`, or `i`
placeholder.append(text)
placeholder_counter += 1
result =\
result[0:left_to_token+1] + "\"" +\
"TEXTPLACEHOLDER" + f"{placeholder_counter:04d}" +\
"\"," + result[token+length+1:]
token = result.find(":")
# # Process integers # #
begin = result.find("i")
end = result.find("e", begin)
while begin != -1:
result = result[0:begin] + result[begin + 1:end] + "," + result[end + 1:]
begin = result.find("i")
end = result.find("e", begin)
# # Process dicts and lists # #
end = result.find("e")
while end != -1:
begin_l = result.rfind("l", 0, end)
begin_d = result.rfind("d", 0, end)
if begin_d > begin_l:
result = result[0:begin_d] + "{" + process_dict(result[begin_d + 1:end]) + "}," + result[end + 1:]
elif begin_l > begin_d:
result = result[0:begin_l] + "[" + result[begin_l + 1:end] + "]," + result[end + 1:]
else:
raise InvalidBencode(result)
end = result.find("e")
# Cleanup for extraneous commas
# between square brackets
fix = result.find(",]")
while fix != -1:
result = result.replace(",]", "]")
fix = result.find(",]")
# between curly braces
fix = result.find(",}")
while fix != -1:
result = result.replace(",}", "}")
fix = result.find(",}")
# between braces
fix = result.find(",)")
while fix != -1:
result = result.replace(",)", ")")
fix = result.find(",)")
# and trailing ones
if result[-1] == ",":
result = result[0:-1]
# Decode texts
for text_id in range(0, len(placeholder)):
result = result.replace("TEXTPLACEHOLDER" + f"{text_id:04d}", placeholder[text_id])
return eval(result)
except Exception:
raise InvalidBencode