-
Notifications
You must be signed in to change notification settings - Fork 1
/
metainfo.cpp
360 lines (307 loc) · 7.76 KB
/
metainfo.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
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
#include "bencodeparser.h"
#include "metainfo.h"
#include <QDateTime>
#include <QMetaType>
#include <QString>
#include <iostream>
#include <QDebug>
#include <QStringList>
#include <math.h>
MetaInfo::MetaInfo()
{
clear();
}
void MetaInfo::clear()
{
errString = "Unknown error";
content.clear();
infoData.clear();
metaInfoMultiFiles.clear();
metaInfoAnnounce.clear();
metaInfoAnnounceList.clear();
metaInfoCreationDate = QDateTime();
metaInfoComment.clear();
metaInfoCreatedBy.clear();
metaInfoName.clear();
metaInfoPieceLength = 0;
metaInfoSha1Sums.clear();
multiFilesLengths.clear();
}
bool MetaInfo::parse(const QByteArray &data)
{
clear();
content = data;
BencodeParser parser;
if (!parser.parse(content)) {
errString = parser.errorString();
return false;
}
infoData = parser.infoSection();
QMap<QByteArray, QVariant> dict = parser.dictionary();
if (!dict.contains("info"))
return false;
QMap<QByteArray, QVariant> info = qvariant_cast<Dictionary>(dict.value("info"));
if (info.contains("files")) {
metaInfoFileForm = MultiFileForm;
QList<QVariant> files = info.value("files").toList();
for (int i = 0; i < files.size(); ++i) {
QMap<QByteArray, QVariant> file = qvariant_cast<Dictionary>(files.at(i));
QList<QVariant> pathElements = file.value("path").toList();
QByteArray path;
foreach (QVariant p, pathElements) {
if (!path.isEmpty())
path += "/";
path += p.toByteArray();
}
MetaInfoMultiFile multiFile;
multiFile.length = file.value("length").toLongLong();
multiFile.path = QString::fromUtf8(path);
multiFile.md5sum = file.value("md5sum").toByteArray();
metaInfoMultiFiles << multiFile;
//additional map
multiFilesLengths.insert(multiFile.path, multiFile.length);
}
metaInfoName = QString::fromUtf8(info.value("name").toByteArray());
metaInfoPieceLength = info.value("piece length").toInt();
QByteArray pieces = info.value("pieces").toByteArray();
for (int i = 0; i < pieces.size(); i += 20)
metaInfoSha1Sums << pieces.mid(i, 20);
}
else if (info.contains("length"))
{
metaInfoFileForm = SingleFileForm;
metaInfoSingleFile.length = info.value("length").toLongLong();
metaInfoSingleFile.md5sum = info.value("md5sum").toByteArray();
metaInfoSingleFile.name = QString::fromUtf8(info.value("name").toByteArray());
metaInfoSingleFile.pieceLength = info.value("piece length").toInt();
QByteArray pieces = info.value("pieces").toByteArray();
for (int i = 0; i < pieces.size(); i += 20)
{
metaInfoSingleFile.sha1Sums << pieces.mid(i, 20);
}
}
metaInfoAnnounce = QString::fromUtf8(dict.value("announce").toByteArray());
if (dict.contains("announce-list")) {
// ### unimplemented
}
if (dict.contains("creation date"))
metaInfoCreationDate.setTime_t(dict.value("creation date").toInt());
if (dict.contains("comment"))
metaInfoComment = QString::fromUtf8(dict.value("comment").toByteArray());
if (dict.contains("created by"))
metaInfoCreatedBy = QString::fromUtf8(dict.value("created by").toByteArray());
return true;
}
//*********MY METHODS USE THIS***************************************
/** Return the main tracker Url*/
QString MetaInfo::getMainTrackerUrl() const
{
return this->announceUrl();
}
/** Return the filename or root directory name*/
QString MetaInfo::getName() const
{
if(this->fileForm()==SingleFileForm)
{
return this->getSingleFileName();
}
else if( this->fileForm()==MultiFileForm)
{
return this->name();
}
else
{
return QString();
}
}
/** Return total length of a file */
qint64 MetaInfo::getTotalLength() const
{
return this->totalSize();
}
/** Return length of piece */
int MetaInfo::getPieceLength() const
{
if(this->fileForm()==SingleFileForm)
{
return this->getSingleFilePieceLength();
}
else if( this->fileForm()==MultiFileForm)
{
return this->pieceLength();
}
else
{
return -1;
}
}
/** Return number of pieces */
double MetaInfo::getNumberOfPieces() const
{
double temp=(double)getTotalLength();
double chunks = ceil(temp/getPieceLength());
return chunks;
}
/** Return list of SHA-1 checksums */
QList<QByteArray> MetaInfo::getSha1Sums() const
{
if(this->fileForm()==SingleFileForm)
{
return this->getSingleFileSha1Sums();
}
else if( this->fileForm()==MultiFileForm)
{
return this->sha1Sums();
}
else
{
return QList<QByteArray>();
}
}
/** Return a list of paths of files for multifiles */
QList<QString> MetaInfo::getMultiFilePaths() const
{
if(this->fileForm()==MultiFileForm)
{
QList<QString> list;
foreach (MetaInfoMultiFile file, multiFiles())
{
list.push_back(file.path);
}
return list;
}
else
{
return QList<QString>();
}
}
/** Return creation date */
QDateTime MetaInfo::getCreationDate() const
{
return this->creationDate();
}
/** Return comment */
QString MetaInfo::getComment() const
{
return this->comment();
}
/** Return created by */
QString MetaInfo::getCreatedBy() const
{
return this->createdBy();
}
/** Return length of the specified file */
qint64 MetaInfo::getFileLengthByPath(QString path) const
{
if(multiFilesLengths.contains(path))
{
return multiFilesLengths.value(path);
}
else
{
return 0;
}
}
/** Return true is this file is a multifile */
bool MetaInfo::isMultiFile() const
{
if(this->fileForm()==MultiFileForm)
{
return true;
}
else
{
return false;
}
}
//***************END OF MY METHODS***********************************
qint64 MetaInfo::getMetaLength() const
{
return singleFile().length;
}
QByteArray MetaInfo::getMetaMD5() const
{
return singleFile().md5sum;
}
QString MetaInfo:: getSingleFileName() const
{
return singleFile().name;
}
int MetaInfo::getSingleFilePieceLength() const
{
return singleFile().pieceLength;
}
QList<QByteArray> MetaInfo::getSingleFileSha1Sums() const
{
return singleFile().sha1Sums;
}
QList<qint64> MetaInfo::getMultiFileLengths() const
{
QList<qint64> list;
foreach (MetaInfoMultiFile file, multiFiles())
{
list.push_back(file.length);
}
return list;
}
QByteArray MetaInfo::infoValue() const
{
return infoData;
}
QString MetaInfo::errorString() const
{
return errString;
}
MetaInfo::FileForm MetaInfo::fileForm() const
{
return metaInfoFileForm;
}
QString MetaInfo::announceUrl() const
{
return metaInfoAnnounce;
}
QStringList MetaInfo::announceList() const
{
return metaInfoAnnounceList;
}
QDateTime MetaInfo::creationDate() const
{
return metaInfoCreationDate;
}
QString MetaInfo::comment() const
{
return metaInfoComment;
}
QString MetaInfo::createdBy() const
{
return metaInfoCreatedBy;
}
MetaInfoSingleFile MetaInfo::singleFile() const
{
return metaInfoSingleFile;
}
QList<MetaInfoMultiFile> MetaInfo::multiFiles() const
{
return metaInfoMultiFiles;
}
QString MetaInfo::name() const
{
return metaInfoName;
}
int MetaInfo::pieceLength() const
{
return metaInfoPieceLength;
}
QList<QByteArray> MetaInfo::sha1Sums() const
{
return metaInfoSha1Sums;
}
qint64 MetaInfo::totalSize() const
{
if (fileForm() == SingleFileForm)
return singleFile().length;
qint64 size = 0;
foreach (MetaInfoMultiFile file, multiFiles())
size += file.length;
return size;
}