2 // This file is part of the aMule Project.
4 // Copyright (c) 2010 Werner Mahr (Vollstrecker) <amule@vollstreckernet.de>
6 // Any parts of this program contributed by third-party developers are copyrighted
7 // by their respective authors.
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
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
);
42 errorCode
= BadFileFormat
;
50 const int qtEmc::getError()
55 const QString
qtEmc::getErrorMessage()
62 return QString("File format wasn't recognised");
67 return QString("Tag format -%1- not recognized.").arg(lastTag
);
72 return QString("An unknown Tag (%1 in type %2) was read from collection file").arg(lastTag
).arg(lastTagType
);
77 return QString("An unknown type of tag (%1) was read from file").arg(lastTagType
);
82 return QString("Tag count claims to be %1").arg(lastTag
);
87 return QString("Your collection file was corrupted and ends to Early");
92 const QStringList
qtEmc::getLinks()
97 const bool qtEmc::isValid()
102 bool qtEmc::readBinary(QFile
&collection
)
105 QString fileHash
, fileName
;
106 quint8 rating
, sFileSize
, tag
, tagType
;
107 quint16 length
, mFileSize
;
108 quint32 lFileSize
, llength
, tagCount
;
111 QDataStream
in(&collection
);
112 in
.setByteOrder(QDataStream::LittleEndian
);
114 in
>> emcVersion
>> headerTagCount
;
118 errorCode
= CorruptFile
;
120 } else if (emcVersion
> 0x02)
122 return readText(collection
);
125 for (int i
=1; i
<=headerTagCount
; i
++)
127 in
>> tagType
>> tagFormat
;
131 errorCode
= CorruptFile
;
138 errorCode
= BadTagFormat
;
146 errorCode
= CorruptFile
;
152 case 0x01: //FT_FILENAME
158 errorCode
= CorruptFile
;
162 char* buffer
= new char[length
];
163 in
.readRawData(buffer
, length
);
165 name
= QString(buffer
);
171 case 0x31: //FT_COLLECTIONAUTHOR
177 errorCode
= CorruptFile
;
181 char* buffer
= new char[length
];
182 in
.readRawData(buffer
, length
);
184 author
= QString(buffer
);
190 case 0x32: //FT_COLLECTIONAUTHORKEY
196 errorCode
= CorruptFile
;
200 char* buffer
= new char[llength
];
201 in
.readRawData(buffer
, llength
);
202 authorKey
= QString(buffer
);
211 errorCode
= UnknownTag
;
223 errorCode
= CorruptFile
;
227 for (int i
=1; i
<=fileCount
; i
++)
236 errorCode
= CorruptFile
;
243 errorCode
= WrongTagCount
;
247 for (int j
=1; j
<=tagCount
; j
++)
253 errorCode
= CorruptFile
;
261 errorCode
= CorruptFile
;
267 case 0x01: // FT_FILENAME
277 errorCode
= CorruptFile
;
281 char* buffer
= new char[length
];
282 in
.readRawData(buffer
, length
);
283 fileName
= QString().fromLocal8Bit(buffer
, length
).trimmed();
285 if (fileName
.length() > length
)
287 fileName
.chop(fileName
.length() - length
);
288 } else if (fileName
.length() == length
)
292 for (int k
=0; k
<length
; k
++)
294 if (fileName
.at(k
) != buffer
[k
+pos
])
305 if (in
.atEnd() && (j
!= tagCount
) && (i
=! fileCount
))
307 errorCode
= CorruptFile
;
316 if (((tag
^0x80) >= 0x11) && ((tag
^0x80) <= 0x20))
319 length
= (tag
^0x80) - 0x10;
321 char* buffer
= new char[length
];
322 in
.readRawData(buffer
, length
);
323 fileName
= QString().fromLocal8Bit(buffer
, length
).trimmed();
325 if (fileName
.length() > length
)
327 fileName
.chop(fileName
.length() - length
);
328 } else if (fileName
.length() == length
)
332 for (int k
=0; k
<length
; k
++)
334 if (fileName
.at(k
) != buffer
[k
+pos
])
345 if (in
.atEnd() && (j
!= tagCount
) && (i
=! fileCount
))
347 errorCode
= CorruptFile
;
352 lastTagType
= tagType
;
353 errorCode
= UnknownTag
;
362 case 0x02: //FT_FILESIZE
369 fileSize
= lFileSize
;
371 if (in
.atEnd() && (j
!= tagCount
) && (i
=! fileCount
))
373 errorCode
= CorruptFile
;
383 fileSize
= mFileSize
;
385 if (in
.atEnd() && (j
!= tagCount
) && (i
=! fileCount
))
387 errorCode
= CorruptFile
;
397 fileSize
= sFileSize
;
399 if (in
.atEnd() && (j
!= tagCount
) && (i
=! fileCount
))
401 errorCode
= CorruptFile
;
411 fileSize
= xlFileSize
;
413 if (in
.atEnd() && (j
!= tagCount
) && (i
=! fileCount
))
415 errorCode
= CorruptFile
;
425 lastTagType
= tagType
;
426 errorCode
= UnknownTag
;
434 case 0x28: //FT_FILEHASH
438 case 0x81: //FT_FILEHASH
440 char* buffer
= new char[16];
441 in
.readRawData(buffer
, 16);
443 if (in
.atEnd() && (j
!= tagCount
) && (i
=! fileCount
))
445 errorCode
= CorruptFile
;
449 for (int pos
= 0; pos
< 16; pos
++)
451 fileHash
.append(QString().setNum(((buffer
[pos
] >> 4) & 0xF), 16));
452 fileHash
.append(QString().setNum((buffer
[pos
] & 0x0F), 16));
462 lastTagType
= tagType
;
463 errorCode
= UnknownTag
;
470 // The comment is read correctly. It will be stored when I know what to do with it.
471 case 0xf6: //FT_FILECOMMENT
473 if ((tag
^0x80) == 0x02)
479 errorCode
= CorruptFile
;
483 length
= ((tag
^0x80)-0x10);
486 char* buffer
= new char[length
];
487 in
.readRawData(buffer
, length
);
492 // The file-rating is read correctly. It will be stored when I know what to do with it.
493 case 0xf7: //FT_FILERATING
502 lastTagType
= tagType
;
503 errorCode
= UnknownTagType
;
509 list
.append(QString("ed2k://|file|%1|%2|%3|/").arg(fileName
).arg(fileSize
).arg(fileHash
));
515 bool qtEmc::readText(QFile
&collection
)
521 QDataStream
in(&collection
);
522 in
.setByteOrder(QDataStream::LittleEndian
);
524 for (int i
=0; i
<=6; i
++)
527 tmp
.append(character
);
530 if (tmp
== "ed2k://")
536 if (character
== 0x0d)
540 } else if (character
!= 0x0a)
542 tmp
.append(character
);
546 errorCode
= BadFileFormat
;