2 // This file is part of the aMule Project.
4 // Copyright (c) 2010-2011 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");
91 return QString("Unknown Error-Code");
94 const QStringList
qtEmc::getLinks()
99 const bool qtEmc::isValid()
104 bool qtEmc::readBinary(QFile
&collection
)
107 QString fileHash
, fileName
;
108 quint8 rating
, sFileSize
, tag
, tagType
;
109 quint16 length
, mFileSize
;
110 quint32 lFileSize
, llength
, tagCount
;
113 QDataStream
in(&collection
);
114 in
.setByteOrder(QDataStream::LittleEndian
);
116 in
>> emcVersion
>> headerTagCount
;
120 errorCode
= CorruptFile
;
122 } else if (emcVersion
> 0x02)
124 return readText(collection
);
127 for (int i
=1; i
<=headerTagCount
; i
++)
129 in
>> tagType
>> tagFormat
;
133 errorCode
= CorruptFile
;
140 errorCode
= BadTagFormat
;
148 errorCode
= CorruptFile
;
154 case 0x01: //FT_FILENAME
160 errorCode
= CorruptFile
;
164 char* buffer
= new char[length
];
165 in
.readRawData(buffer
, length
);
167 name
= QString(buffer
);
173 case 0x31: //FT_COLLECTIONAUTHOR
179 errorCode
= CorruptFile
;
183 char* buffer
= new char[length
];
184 in
.readRawData(buffer
, length
);
186 author
= QString(buffer
);
192 case 0x32: //FT_COLLECTIONAUTHORKEY
198 errorCode
= CorruptFile
;
202 char* buffer
= new char[llength
];
203 in
.readRawData(buffer
, llength
);
204 authorKey
= QString(buffer
);
213 errorCode
= UnknownTag
;
225 errorCode
= CorruptFile
;
229 for (int i
=1; i
<=fileCount
; i
++)
238 errorCode
= CorruptFile
;
245 errorCode
= WrongTagCount
;
249 for (int j
=1; j
<=tagCount
; j
++)
255 errorCode
= CorruptFile
;
263 errorCode
= CorruptFile
;
269 case 0x01: // FT_FILENAME
279 errorCode
= CorruptFile
;
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
)
294 for (int k
=0; k
<length
; k
++)
296 if (fileName
.at(k
) != buffer
[k
+pos
])
307 if (in
.atEnd() && (j
!= tagCount
) && (i
=! fileCount
))
309 errorCode
= CorruptFile
;
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
)
334 for (int k
=0; k
<length
; k
++)
336 if (fileName
.at(k
) != buffer
[k
+pos
])
347 if (in
.atEnd() && (j
!= tagCount
) && (i
=! fileCount
))
349 errorCode
= CorruptFile
;
354 lastTagType
= tagType
;
355 errorCode
= UnknownTag
;
364 case 0x02: //FT_FILESIZE
371 fileSize
= lFileSize
;
373 if (in
.atEnd() && (j
!= tagCount
) && (i
=! fileCount
))
375 errorCode
= CorruptFile
;
385 fileSize
= mFileSize
;
387 if (in
.atEnd() && (j
!= tagCount
) && (i
=! fileCount
))
389 errorCode
= CorruptFile
;
399 fileSize
= sFileSize
;
401 if (in
.atEnd() && (j
!= tagCount
) && (i
=! fileCount
))
403 errorCode
= CorruptFile
;
413 fileSize
= xlFileSize
;
415 if (in
.atEnd() && (j
!= tagCount
) && (i
=! fileCount
))
417 errorCode
= CorruptFile
;
427 lastTagType
= tagType
;
428 errorCode
= UnknownTag
;
436 case 0x28: //FT_FILEHASH
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
;
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));
464 lastTagType
= tagType
;
465 errorCode
= UnknownTag
;
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)
481 errorCode
= CorruptFile
;
485 length
= ((tag
^0x80)-0x10);
488 char* buffer
= new char[length
];
489 in
.readRawData(buffer
, length
);
494 // The file-rating is read correctly. It will be stored when I know what to do with it.
495 case 0xf7: //FT_FILERATING
504 lastTagType
= tagType
;
505 errorCode
= UnknownTagType
;
511 list
.append(QString("ed2k://|file|%1|%2|%3|/").arg(fileName
).arg(fileSize
).arg(fileHash
));
517 bool qtEmc::readText(QFile
&collection
)
523 QDataStream
in(&collection
);
524 in
.setByteOrder(QDataStream::LittleEndian
);
526 for (int i
=0; i
<=6; i
++)
529 tmp
.append(character
);
532 if (tmp
== "ed2k://")
538 if (character
== 0x0d)
542 } else if (character
!= 0x0a)
544 tmp
.append(character
);
548 errorCode
= BadFileFormat
;