-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbencode.cc
More file actions
243 lines (215 loc) · 4.47 KB
/
bencode.cc
File metadata and controls
243 lines (215 loc) · 4.47 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "bencode.h"
#include <cctype>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdarg.h>
const int MAX_DEPTH = 64;
/*
* COMPABILITY NOTE
*
* Please note that the original decoder in bencode.py accepts integers with
* leading spaces and zeroes. To enable compability with the original decoder,
* comment out the following lines.
*/
#define NO_LEADING_SPACES
#define NO_LEADING_ZEROES
namespace ben {
int encode_string(std::string *out, const std::string &s)
{
char length[16];
sprintf(length, "%zd:", s.size());
*out += length + s;
return 0;
}
int encode(std::string *out, const variant &value)
{
variant_map dict;
variant_list list;
int i;
long long ll;
bool b;
std::string s;
if (value.get(&dict) == 0) {
*out += 'd';
for (map_const_iter<std::string, variant> i(dict); i.valid();
i.next()) {
encode_string(out, i.key());
if (encode(out, *i))
return -1;
}
*out += 'e';
} else if (value.get(&list) == 0) {
*out += 'l';
for (list_const_iter<variant> i(list); i.valid(); i.next()) {
if (encode(out, *i))
return -1;
}
*out += 'e';
} else if (value.get(&i) == 0) {
char number[16];
sprintf(number, "i%de", i);
*out += number;
} else if (value.get(&ll) == 0) {
char number[22];
sprintf(number, "i%llde", ll);
*out += number;
} else if (value.get(&b) == 0) {
*out += b ? "b1" : "b0";
} else if (value.get(&s) == 0) {
encode_string(out, s);
} else {
fprintf(stderr, "Unable to bencode\n");
return -1;
}
return 0;
}
int decoder::decode_string(std::string *s)
{
size_t end = m_buf.find(':', m_pos);
if (end == std::string::npos || end >= m_len) {
set_error("Unterminated string length");
return -1;
}
std::string length = m_buf.substr(m_pos, end - m_pos);
if (!validate_int(length))
return -1;
char *endp = NULL;
size_t strlen = strtoul(length.c_str(), &endp, 10);
if (size_t(endp - length.c_str()) != length.size()) {
set_error("Invalid string length (%s)", length.c_str());
return -1;
}
m_pos = end + 1;
if (strlen + m_pos > m_len) {
set_error("Input overrun");
return -1;
}
*s = m_buf.substr(m_pos, strlen);
m_pos += strlen;
return 0;
}
decoder::decoder(const std::string &buf) :
m_buf(buf), m_pos(0), m_len(buf.size()), m_depth(0), m_error(NULL)
{
}
decoder::~decoder()
{
free(m_error);
}
int decoder::decode(variant *value)
{
if (m_pos >= m_len) {
set_error("Input overrun");
return -1;
}
if (m_depth >= MAX_DEPTH) {
set_error("Recursion limit exceeded");
return -1;
}
m_depth++;
switch (m_buf[m_pos]) {
case 'd':{
m_pos++;
variant_map dict;
while (m_buf[m_pos] != 'e') {
std::string key;
variant item;
if (decode_string(&key) ||
decode(&item))
return -1;
dict.insert(key, item);
}
m_pos++;
*value = dict;
}
break;
case 'l':{
m_pos++;
variant_list list;
while (m_buf[m_pos] != 'e') {
variant item;
if (decode(&item))
return -1;
list.push_back(item);
}
m_pos++;
*value = list;
}
break;
case 'i':{
m_pos++;
size_t end = m_buf.find('e', m_pos);
if (end == std::string::npos || end >= m_len) {
set_error("Unterminated integer");
return -1;
}
std::string number = m_buf.substr(m_pos, end - m_pos);
if (!validate_int(number))
return -1;
char *endp = NULL;
long long ll = strtoll(number.c_str(), &endp, 10);
if (size_t(endp - number.c_str()) != number.size()) {
set_error("Invalid integer (%s)",
number.c_str());
return -1;
}
if (ll >= INT_MIN && ll <= INT_MAX)
*value = int(ll);
else
*value = ll;
m_pos = end + 1;
}
break;
case 'b':{
m_pos++;
switch (m_buf[m_pos]) {
case '1':
*value = true;
break;
case '0':
*value = false;
break;
default:
set_error("Invalid boolean");
return -1;
}
m_pos++;
}
break;
default: {
std::string s;
if (decode_string(&s))
return -1;
*value = s;
}
break;
}
assert(m_depth > 0);
m_depth--;
return 0;
}
void decoder::set_error(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vasprintf(&m_error, fmt, ap);
va_end(ap);
}
bool decoder::validate_int(const std::string &s)
{
#ifdef NO_LEADING_SPACES
if (s.size() >= 1 && isspace(s[0])) {
set_error("Integer with leading spaces: %s", s.c_str());
return false;
}
#endif
#ifdef NO_LEADING_ZEROES
if (s.size() >= 2 && (s[0] == '0' || (s[0] == '-' && s[1] == '0'))) {
set_error("Integer with leading zeroes: %s", s.c_str());
return false;
}
#endif
return true;
}
}