-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseTorrent.cpp
326 lines (254 loc) · 6.37 KB
/
ParseTorrent.cpp
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
/*
* ParseTorrent.cpp
*
* Created on: Sep 28, 2014
* Author: jay
*/
#include "ParseTorrent.h"
ParseTorrent::ParseTorrent() {
// TODO Auto-generated constructor stub
}
ParseTorrent::~ParseTorrent() {
// TODO Auto-generated destructor stub
}
string* ParseTorrent::decodeDict(char* str1, int index) {
index++;
if (str1[index] == 'e') {
array[0] = index;
array[1] = "\0";
return array;
}
if (str1[index] == 'l') {
return decodeList(str1, index++);
} else if (str1[index] == 'd') {
return decodeDict(str1, index++);
} else if (str1[index] == 'i') {
return decodeInt(str1, index++);
} else {
return decodeString(str1, index);
}
}
string* ParseTorrent::decodeString(char* str1, int index) {
int startIndex = index; //first position of string
stringstream ss;
int colon;
// to find first index of colon
for (int i = startIndex; i < strlen(str1); i++) {
if (str1[i] == ':') {
colon = i;
break;
}
}
for (int i = startIndex; i < colon; i++) // to get the number
{
ss << str1[i];
}
int number;
ss >> number; //stringstream to int
colon++;
//get each string
stringstream value;
for (int j = (colon); j < (colon + number); j++) {
value << str1[j];
}
ostringstream convert;
convert << colon + number;
array[0] = convert.str();
array[1] = value.str();
ss.str(""); //clear string stream
value.str("");
return array;
}
string* ParseTorrent::decodeInt(char* str1, int index) {
int Eindex;
int startIndex = index; // to find first index of 'e'
for (int i = startIndex; i < strlen(str1); i++) {
if (str1[i] == 'e') {
Eindex = i;
break;
}
}
index++;
stringstream number;
// to store number
for (int i = index; i < Eindex; i++) {
number << str1[i];
}
ostringstream convert;
convert << Eindex + 1;
array[0] = convert.str();
array[1] = number.str();
number.str(""); // clear stringstream
return array;
}
string* ParseTorrent::decodeList(char* str1, int index) {
index++;
if (str1[index] == 'e') {
array[0] = index;
array[1] = "\0";
return array;
}
if (str1[index] == 'l') {
return decodeList(str1, index++);
} else if (str1[index] == 'd') {
return decodeDict(str1, index++);
} else if (str1[index] == 'i') {
return decodeInt(str1, index++);
} else {
return decodeString(str1, index++);
}
}
map<string, string> ParseTorrent::parseFile(string fileName) {
map<string, string> infoValues;
int a = 1; // iterator to store values in map.
string mapKey, mapValue;
ifstream infile(fileName.c_str(), ifstream::binary);
// get size of file
infile.seekg(0, infile.end);
long fileLength = infile.tellg();
infile.seekg(0, infile.beg);
// allocate memory for file content
char ch[fileLength];
ch[fileLength + 1] = '\0';
// read content of infile
infile.read(ch, fileLength);
infile.close();
//cout.write(ch, fileLength) << endl;
int index = 0;
string * output;
do {
switch (ch[index]) {
case 'l':
output = decodeList(ch, index);
break;
case 'd':
output = decodeDict(ch, index);
break;
case 'i':
output = decodeInt(ch, index);
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
output = decodeString(ch, index);
break;
default:
cout << "Invalid BenCoded Meta Info. Please Check File" << endl;
exit(1);
}
index = atoi(output[0].c_str());
if (a % 2 == 1 && output[1] != "info") {
mapKey = output[1];
}
if (a % 2 == 0) {
mapValue = output[1];
infoValues[mapKey] = mapValue;
}
if (output[1] == "info") {
a--;
}
a++;
} while (ch[index] != 'e' && index < fileLength);
return infoValues;
}
/*stringstream ParseTorrent::getTorrentFileStream(char* str) {
return stringstream;
}*/
bt_info_t ParseTorrent::getInfoStructure(map<string, string> m) {
//char **a;
bt_info_t btStructure;
map<int, string> p_hashes;
map<string, string>::iterator it;
for (it = m.begin(); it != m.end(); it++) {
btStructure.name = ((m.find("name"))->second);
btStructure.piece_length = std::strtol(
((m.find("piece length"))->second).c_str(), NULL, 10);
btStructure.length = std::strtol(((m.find("length"))->second).c_str(),
NULL, 10);
btStructure.num_pieces = ceil(
(double) btStructure.length
/ (double) btStructure.piece_length);
//char* p = new char[20];
char** intermediate = new char*[btStructure.num_pieces];
stringstream value;
value << ((m.find("pieces"))->second);
cout << value.str() << endl;
for (int i = 0; i < btStructure.num_pieces; i++) {
char* p = new char[120];
memset(p, '\0', 20);
value.read(p, 20);
intermediate[i] = &p[i];
}
btStructure.piece_hashes = (char**) intermediate;
return btStructure;
}
}
unsigned char* ParseTorrent::calInfoHash(char * filename) {
ifstream infile(filename, ifstream::binary);
char * sp;
// get size of file
infile.seekg(0, infile.end);
long fileLength = infile.tellg();
infile.seekg(0, infile.beg);
// allocate memory for file content
char ch[fileLength];
ch[fileLength + 1] = '\0';
// read content of infile
infile.read(ch, fileLength);
infile.close();
stringstream s;
for (int j = 0; j < fileLength; j++) {
s << ch[j];
}
size_t index = s.str().find("info", 0);
index = index + 4;
size_t lastIndexOfE = s.str().find_last_of('e', fileLength);
lastIndexOfE = lastIndexOfE - 1;
string info_part = s.str().substr(index, lastIndexOfE - index + 1);
int len = info_part.length();
cout << "Before" << len << endl;
char * infoToChar = new char[len];
infoToChar[len] = '\0';
int len1;
len1 = info_part.copy(infoToChar, len, 0);
SHA1((unsigned char *) infoToChar, strlen(infoToChar),
(unsigned char*) hashOP);
//return 'i';
cout << "hash value returned." << endl;
return hashOP;
}
char* ParseTorrent::getHandshakeMessage(unsigned char * hash, peer_t * peer) {
bt_lib bt;
stringstream sample;
char * finalHash = new char[68];
char peer_id_hash[20];
for (int i = 0; i < 28; i++) {
sample << handshakeMessage[i];
}
for (int i = 0; i < 28; i++) {
printf("%02x\n", handshakeMessage[i]);
}
sample.seekg(0, sample.end);
for (int i = 0; i < 20; i++) {
sample << hash[i];
}
for (int i = 0; i < 48; i++) {
printf("%02x\n", sample.str().c_str()[i]);
}
memcpy(&peer_id_hash, &peer->id, 20);
sample.seekg(0, sample.end);
for (int j = 0; j < 20; j++) {
sample << peer_id_hash[j];
}
sample.seekg(0, sample.end);
sample.seekg(0, sample.beg);
sample.read(finalHash, 68);
return finalHash;
}