-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
372 lines (292 loc) · 7.41 KB
/
Copy pathparser.cpp
File metadata and controls
372 lines (292 loc) · 7.41 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <stdexcept>
#include <map>
#include <fstream>
// sha -1
#include <openssl/sha.h>
#include <sstream>
#include <iomanip>
using namespace std;
size_t g_info_start = 0;
size_t g_info_end = 0;
bool g_info_found = false;
struct BencodeValue
{
enum class Type
{
Int,
String,
List,
Dict
};
Type type;
int64_t integer;
string str;
vector<BencodeValue> list;
map<string, BencodeValue> dict;
};
BencodeValue parseInt(const vector<uint8_t> &data, size_t &pos)
{
// skip i
pos++;
bool negative = false;
if (data[pos] == '-')
{
negative = true;
pos++;
}
int64_t val = 0;
while (data[pos] != 'e')
{
if (!isdigit(data[pos]))
{
throw runtime_error("invalid integer in bencode");
}
val = val * 10 + (data[pos] - '0');
pos++;
}
// skip e
pos++;
if (negative)
val = -val;
BencodeValue v;
v.type = BencodeValue::Type::Int;
v.integer = val;
return v;
}
BencodeValue parseString(const vector<uint8_t> &data, size_t &pos)
{
size_t length = 0;
while (data[pos] != ':')
{
if ((!isdigit(data[pos])))
throw runtime_error("Invalid character in Bencode string");
length = length * 10 + (data[pos] - '0');
pos++;
}
// skip :
pos++;
if (pos + length > data.size())
throw runtime_error("Invalid Bencode String");
string s;
s.reserve(length);
for (size_t i = 0; i < length; i++)
{
s.push_back(static_cast<char>(data[pos]));
pos++;
}
BencodeValue v;
v.type = BencodeValue::Type::String;
v.str = s;
return v;
}
BencodeValue parse(const vector<uint8_t> &data, size_t &pos);
BencodeValue parseList(const vector<uint8_t> &data, size_t &pos)
{
// skip l;
pos++;
BencodeValue v;
v.type = BencodeValue::Type::List;
while (data[pos] != 'e')
{
v.list.push_back(parse(data, pos));
}
// skip e;
pos++;
return v;
}
BencodeValue parseDict(const vector<uint8_t> &data, size_t &pos)
{
// skip d;
pos++;
BencodeValue v;
v.type = BencodeValue::Type::Dict;
while (data[pos] != 'e')
{
BencodeValue key = parseString(data, pos);
if (key.str == "info" && !g_info_found)
{
g_info_found = true;
g_info_start = pos;
BencodeValue value = parse(data, pos);
g_info_end = pos;
v.dict[key.str] = value;
}
else
{
BencodeValue value = parse(data, pos);
v.dict[key.str] = value;
}
}
pos++;
return v;
}
BencodeValue parse(const vector<uint8_t> &data, size_t &pos)
{
if (pos > data.size())
throw runtime_error("Unexpected end of data");
if (data[pos] == 'i')
{
return parseInt(data, pos);
}
if (isdigit(data[pos]))
{
return parseString(data, pos);
}
if (data[pos] == 'l')
{
return parseList(data, pos);
}
if (data[pos] == 'd')
{
return parseDict(data, pos);
}
throw runtime_error("Unsupported bencode type (yet)");
}
vector<uint8_t> readFile(const string &path)
{
ifstream file(path, ios::binary);
if (!file)
throw runtime_error("failed to open file");
return vector<uint8_t>((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
}
void printJson(const BencodeValue &v, int indent = 0)
{
const auto pad = [&]()
{
for (size_t i = 0; i < indent; i++)
cout << " ";
};
switch (v.type)
{
case BencodeValue::Type::Int:
std::cout << v.integer;
break;
case BencodeValue::Type::String:
std::cout << "\"" << v.str << "\"";
break;
case BencodeValue::Type::List:
cout << "[\n";
for (size_t i = 0; i < v.list.size(); i++)
{
pad();
cout << " ";
printJson(v.list[i], indent + 1);
// 4. The Comma Check
if (i + 1 < v.list.size())
std::cout << ",";
std::cout << "\n";
}
pad();
cout << "\n]";
break;
case BencodeValue::Type::Dict:
std::cout << "{\n";
size_t count = 0;
for (const auto &[k, val] : v.dict)
{
pad();
std::cout << " \"" << k << "\": ";
printJson(val, indent + 1);
if (++count < v.dict.size())
std::cout << ",";
std::cout << "\n";
}
pad();
std::cout << "}";
break;
}
}
std::string sha1Hex(const std::vector<uint8_t> &data,
size_t start,
size_t end)
{
unsigned char hash[SHA_DIGEST_LENGTH];
SHA1(&data[start], end - start, hash);
std::ostringstream oss;
for (int i = 0; i < SHA_DIGEST_LENGTH; i++)
{
oss << std::hex << std::setw(2)
<< std::setfill('0')
<< static_cast<int>(hash[i]);
}
return oss.str();
}
vector<string> extractTrackers(const BencodeValue &root)
{
if (root.type != BencodeValue::Type::Dict)
{
throw runtime_error("torrent type is not dict");
}
vector<string> trackers;
auto it = root.dict.find("announce-list");
if (it != root.dict.end())
{
if (it->second.type != BencodeValue::Type::List)
{
throw runtime_error("announce List is not list");
}
for (const auto &tier : it->second.list)
{
if (tier.type != BencodeValue::Type::List)
{
throw runtime_error("announce-list tier is not a list");
}
for (const auto &tracker : tier.list)
{
if (tracker.type != BencodeValue::Type::String)
{
throw std::runtime_error("tracker URL is not a string");
}
if (tracker.str.rfind("http://") || tracker.str.rfind("https://"))
{
trackers.push_back(tracker.str);
}
}
}
if (trackers.empty())
{
auto it2 = root.dict.find("announce");
if (it2 == root.dict.end())
{
throw runtime_error("no announce or announce-list found");
}
if (it2->second.type == BencodeValue::Type::String)
{
throw runtime_error("anounce is not a string");
}
trackers.push_back(it2->second.str);
}
}
return trackers;
}
int main()
{
try
{
auto data = readFile("sample.torrent");
size_t pos = 0;
BencodeValue root = parse(data, pos);
// printJson(root);
if (!g_info_found)
{
throw std::runtime_error("info dictionary not found");
}
std::string infoHash = sha1Hex(data, g_info_start, g_info_end);
std::cout << "Info-hash: " << infoHash << "\n";
auto trackers = extractTrackers(root);
std::cout << "Trackers:\n";
for (const auto &t : trackers)
{
std::cout << " " << t << "\n";
}
// std::cout << "Torrent parsed successfully\n";
}
catch (const std::exception &ex)
{
std::cerr << "Error: " << ex.what() << "\n";
}
}