Whitespace
[amule.git] / src / utils / plasmamule / qt-emc.cpp
blobb71114eb4ac15da399799a138a3e74df8b4f1217
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2010-2011 Werner Mahr (Vollstrecker) <amule@vollstreckernet.de>
5 //
6 // Any parts of this program contributed by third-party developers are copyrighted
7 // by their respective authors.
8 //
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "qt-emc.h"
26 qtEmc::qtEmc(const QString &filename)
29 QStringList files = filename.split("file://");
31 for (QStringList::const_iterator constFilesIterator = files.constBegin(); constFilesIterator != files.constEnd(); ++constFilesIterator)
33 if (!QString(*constFilesIterator).remove("\n").trimmed().isEmpty())
35 QFile collection(QString(*constFilesIterator).trimmed());
37 if (collection.open (QIODevice::ReadOnly))
39 valid = readBinary(collection);
40 } else {
41 valid = FALSE;
42 errorCode = BadFileFormat;
45 collection.close();
50 const int qtEmc::getError()
52 return errorCode;
55 const QString qtEmc::getErrorMessage()
58 switch (errorCode)
60 case BadFileFormat:
62 return QString("File format wasn't recognised");
65 case BadTagFormat:
67 return QString("Tag format -%1- not recognized.").arg(lastTag);
70 case UnknownTag:
72 return QString("An unknown Tag (%1 in type %2) was read from collection file").arg(lastTag).arg(lastTagType);
75 case UnknownTagType:
77 return QString("An unknown type of tag (%1) was read from file").arg(lastTagType);
80 case WrongTagCount:
82 return QString("Tag count claims to be %1").arg(lastTag);
85 case CorruptFile:
87 return QString("Your collection file was corrupted and ends to Early");
91 return QString("Unknown Error-Code");
94 const QStringList qtEmc::getLinks()
96 return list;
99 const bool qtEmc::isValid()
101 return valid;
104 bool qtEmc::readBinary(QFile &collection)
106 int fileSize;
107 QString fileHash, fileName;
108 quint8 rating, sFileSize, tag, tagType;
109 quint16 length, mFileSize;
110 quint32 lFileSize, llength, tagCount;
111 quint64 xlFileSize;
113 QDataStream in(&collection);
114 in.setByteOrder(QDataStream::LittleEndian);
116 in >> emcVersion >> headerTagCount;
118 if (in.atEnd())
120 errorCode = CorruptFile;
121 return FALSE;
122 } else if (emcVersion > 0x02)
124 return readText(collection);
127 for (int i=1; i<=headerTagCount; i++)
129 in >> tagType >> tagFormat;
131 if (in.atEnd())
133 errorCode = CorruptFile;
134 return FALSE;
137 if (tagFormat != 1)
139 lastTag = tag;
140 errorCode = BadTagFormat;
141 return FALSE;
144 in >> tag;
146 if (in.atEnd())
148 errorCode = CorruptFile;
149 return FALSE;
152 switch (tag)
154 case 0x01: //FT_FILENAME
156 in >> length;
158 if (in.atEnd())
160 errorCode = CorruptFile;
161 return FALSE;
164 char* buffer = new char[length];
165 in.readRawData(buffer, length);
167 name = QString(buffer);
169 delete [] buffer;
170 break;
173 case 0x31: //FT_COLLECTIONAUTHOR
175 in >> length;
177 if (in.atEnd())
179 errorCode = CorruptFile;
180 return FALSE;
183 char* buffer = new char[length];
184 in.readRawData(buffer, length);
186 author = QString(buffer);
188 delete [] buffer;
189 break;
192 case 0x32: //FT_COLLECTIONAUTHORKEY
194 in >> llength;
196 if (in.atEnd())
198 errorCode = CorruptFile;
199 return FALSE;
202 char* buffer = new char[llength];
203 in.readRawData(buffer, llength);
204 authorKey = QString(buffer);
206 delete [] buffer;
207 break;
210 default:
212 lastTag = tag;
213 errorCode = UnknownTag;
214 return FALSE;
217 break;
221 in >> fileCount;
223 if (in.atEnd())
225 errorCode = CorruptFile;
226 return FALSE;
229 for (int i=1; i<=fileCount; i++)
231 fileHash.clear();
232 fileName.clear();
234 in >> tagCount;
236 if (in.atEnd())
238 errorCode = CorruptFile;
239 return FALSE;
242 if (tagCount > 5)
244 lastTag = tagCount;
245 errorCode = WrongTagCount;
246 return FALSE;
249 for (int j=1; j<=tagCount; j++)
251 in >> tag;
253 if (in.atEnd())
255 errorCode = CorruptFile;
256 return FALSE;
259 in >> tagType;
261 if (in.atEnd())
263 errorCode = CorruptFile;
264 return FALSE;
267 switch (tagType)
269 case 0x01: // FT_FILENAME
271 switch (tag)
273 case 0x82:
275 in >> length;
277 if (in.atEnd())
279 errorCode = CorruptFile;
280 return FALSE;
283 char* buffer = new char[length];
284 in.readRawData(buffer, length);
285 fileName = QString().fromLocal8Bit(buffer, length).trimmed();
287 if (fileName.length() > length)
289 fileName.chop(fileName.length() - length);
290 } else if (fileName.length() == length)
292 int pos=0;
294 for (int k=0; k<length; k++)
296 if (fileName.at(k) != buffer[k+pos])
298 pos++;
302 fileName.chop(pos);
305 delete [] buffer;
307 if (in.atEnd() && (j != tagCount) && (i =! fileCount))
309 errorCode = CorruptFile;
310 return FALSE;
313 break;
316 default:
318 if (((tag^0x80) >= 0x11) && ((tag^0x80) <= 0x20))
321 length = (tag^0x80) - 0x10;
323 char* buffer = new char[length];
324 in.readRawData(buffer, length);
325 fileName = QString().fromLocal8Bit(buffer, length).trimmed();
327 if (fileName.length() > length)
329 fileName.chop(fileName.length() - length);
330 } else if (fileName.length() == length)
332 int pos=0;
334 for (int k=0; k<length; k++)
336 if (fileName.at(k) != buffer[k+pos])
338 pos++;
342 fileName.chop(pos);
345 delete [] buffer;
347 if (in.atEnd() && (j != tagCount) && (i =! fileCount))
349 errorCode = CorruptFile;
350 return FALSE;
352 } else {
353 lastTag = tag;
354 lastTagType = tagType;
355 errorCode = UnknownTag;
356 return FALSE;
361 break;
364 case 0x02: //FT_FILESIZE
366 switch (tag)
368 case 0x83:
370 in >> lFileSize;
371 fileSize = lFileSize;
373 if (in.atEnd() && (j != tagCount) && (i =! fileCount))
375 errorCode = CorruptFile;
376 return FALSE;
379 break;
382 case 0x88:
384 in >> mFileSize;
385 fileSize = mFileSize;
387 if (in.atEnd() && (j != tagCount) && (i =! fileCount))
389 errorCode = CorruptFile;
390 return FALSE;
393 break;
396 case 0x89:
398 in >> sFileSize;
399 fileSize = sFileSize;
401 if (in.atEnd() && (j != tagCount) && (i =! fileCount))
403 errorCode = CorruptFile;
404 return FALSE;
407 break;
410 case 0x8b:
412 in >> xlFileSize;
413 fileSize = xlFileSize;
415 if (in.atEnd() && (j != tagCount) && (i =! fileCount))
417 errorCode = CorruptFile;
418 return FALSE;
421 break;
424 default:
426 lastTag = tag;
427 lastTagType = tagType;
428 errorCode = UnknownTag;
429 return FALSE;
433 break;
436 case 0x28: //FT_FILEHASH
438 switch (tag)
440 case 0x81: //FT_FILEHASH
442 char* buffer = new char[16];
443 in.readRawData(buffer, 16);
445 if (in.atEnd() && (j != tagCount) && (i =! fileCount))
447 errorCode = CorruptFile;
448 return FALSE;
451 for (int pos = 0; pos < 16; pos++)
453 fileHash.append(QString().setNum(((buffer[pos] >> 4) & 0xF), 16));
454 fileHash.append(QString().setNum((buffer[pos] & 0x0F), 16));
457 delete [] buffer;
458 break;
461 default:
463 lastTag = tag;
464 lastTagType = tagType;
465 errorCode = UnknownTag;
466 return FALSE;
470 break;
472 // The comment is read correctly. It will be stored when I know what to do with it.
473 case 0xf6: //FT_FILECOMMENT
475 if ((tag^0x80) == 0x02)
477 in >> length;
479 if (in.atEnd())
481 errorCode = CorruptFile;
482 return FALSE;
484 } else {
485 length = ((tag^0x80)-0x10);
488 char* buffer = new char[length];
489 in.readRawData(buffer, length);
491 delete [] buffer;
492 break;
494 // The file-rating is read correctly. It will be stored when I know what to do with it.
495 case 0xf7: //FT_FILERATING
497 in >> rating;
499 break;
502 default:
504 lastTagType = tagType;
505 errorCode = UnknownTagType;
506 return FALSE;
511 list.append(QString("ed2k://|file|%1|%2|%3|/").arg(fileName).arg(fileSize).arg(fileHash));
514 return TRUE;
517 bool qtEmc::readText(QFile &collection)
519 quint8 character;
520 QString tmp;
522 collection.seek(0);
523 QDataStream in(&collection);
524 in.setByteOrder(QDataStream::LittleEndian);
526 for (int i=0; i<=6; i++)
528 in >> character;
529 tmp.append(character);
532 if (tmp == "ed2k://")
534 while (!in.atEnd())
536 in >> character;
538 if (character == 0x0d)
540 list.append(tmp);
541 tmp.clear();
542 } else if (character != 0x0a)
544 tmp.append(character);
547 } else {
548 errorCode = BadFileFormat;
549 return FALSE;
552 return TRUE;