2 // This file is part of the aMule Project.
4 // Copyright (c) 2008-2011 Dévai Tamás ( gonosztopi@amule.org )
5 // Copyright (c) 2004-2011 aMule Team ( admin@amule.org / http://www.amule.org )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "eD2kFiles.h"
28 #include "../../SafeFile.h"
29 #include "../../ClientCredits.h" // Needed for MAXPUBKEYSIZE
30 #include "../../OtherFunctions.h" // Needed for CastItoXBytes
31 #include <protocol/ed2k/Constants.h>
32 #include <tags/FileTags.h>
42 static wxString
VersionInfo(uint8_t version
, uint8_t type
)
44 wxString verStr
= wxT("??");
45 if (type
== PrefFile
) {
46 if (version
== PREFFILE_VERSION
) {
47 verStr
= wxT("PREFFILE_VERSION");
49 } else if (type
== PartMetFile
) {
50 if (version
== PARTFILE_VERSION
) {
51 verStr
= wxT("PARTFILE_VERSION");
52 } else if (version
== PARTFILE_SPLITTEDVERSION
) {
53 verStr
= wxT("PARTFILE_SPLITTEDVERSION");
54 } else if (version
== PARTFILE_VERSION_LARGEFILE
) {
55 verStr
= wxT("PARTFILE_VERSION_LARGEFILE");
57 } else if (type
== CreditFile
) {
58 if (version
== CREDITFILE_VERSION
) {
59 verStr
= wxT("CREDITFILE_VERSION");
61 } else if (type
== KnownFileList
) {
62 if (version
== MET_HEADER
) {
63 verStr
= wxT("MET_HEADER");
64 } else if (version
== MET_HEADER_WITH_LARGEFILES
) {
65 verStr
= wxT("MET_HEADER_WITH_LARGEFILES");
67 } else if (type
== CanceledFileList
) {
68 if (version
== CANCELEDFILE_VERSION
) {
69 verStr
= wxT("CANCELEDFILE_VERSION");
72 return hex(version
) + wxT(' ') + verStr
;
75 void DecodePreferencesDat(const CFileDataIO
& file
)
77 cout
<< "(Version): " << VersionInfo(file
.ReadUInt8(), PrefFile
);
78 cout
<< "\nUserHash : " << file
.ReadHash() << '\n';
81 void DecodeFriendList(const CFileDataIO
& file
)
83 uint8_t version
= file
.ReadUInt8();
84 cout
<< "Version : " << VersionInfo(version
, KnownFileList
) << endl
;
85 if (version
!= MET_HEADER
) {
86 cerr
<< "File seems to be corrupt, invalid version!" << endl
;
89 uint32_t records
= file
.ReadUInt32();
90 cout
<< "Records : " << records
<< '\n';
91 for (uint32_t i
= 0; i
< records
; i
++) {
92 cout
<< "\tUserHash : " << file
.ReadHash();
93 cout
<< "\n\tLasUsedIP : " << CeD2kIP(file
.ReadUInt32());
94 cout
<< "\n\tLasUsedPort: " << file
.ReadUInt16();
95 cout
<< "\n\tLastSeen : " << CTimeT(file
.ReadUInt32());
96 cout
<< "\n\tLastChatted: " << CTimeT(file
.ReadUInt32());
97 uint32_t tagCount
= file
.ReadUInt32();
98 cout
<< "\n\ttagCount : " << tagCount
<< '\n';
99 for (; tagCount
> 0; --tagCount
) {
100 CTag
tag(file
, true);
101 cout
<< "\t\t" << CFriendTag(tag
) << '\n';
106 void DecodeServerMet(const CFileDataIO
& file
)
108 uint8_t version
= file
.ReadUInt8();
109 cout
<< "Version : " << VersionInfo(version
, KnownFileList
) << endl
;
110 if (version
!= 0xE0 && version
!= MET_HEADER
) {
111 cerr
<< "File seems to be corrupt, invalid version!" << endl
;
114 uint32_t ServerCount
= file
.ReadUInt32();
115 cout
<< "Count : " << ServerCount
<< '\n';
116 for (; ServerCount
> 0; --ServerCount
) {
117 cout
<< "\tIP : " << CeD2kIP(file
.ReadUInt32()) << '\n';
118 cout
<< "\tPort : " << file
.ReadUInt16() << '\n';
119 uint32_t tagCount
= file
.ReadUInt32();
120 cout
<< "\ttagCount : " << tagCount
<< '\n';
121 for (; tagCount
> 0; --tagCount
) {
122 CTag
tag(file
, true);
123 cout
<< "\t\t" << CServerTag(tag
) << '\n';
128 void DecodeClientsMet(const CFileDataIO
& file
)
130 uint8_t version
= file
.ReadUInt8();
131 cout
<< "Version : " << VersionInfo(version
, CreditFile
) << endl
;
132 if (version
!= CREDITFILE_VERSION
) {
133 cerr
<< "File seems to be corrupt, invalid version!" << endl
;
136 uint32_t count
= file
.ReadUInt32();
137 cout
<< "Count : " << count
<< '\n';
138 for (uint32_t i
= 0; i
< count
; i
++) {
139 cout
<< wxString::Format(wxT("#%u\tKey : "), i
) << file
.ReadHash();
140 uint32_t uploaded
= file
.ReadUInt32();
141 uint32_t downloaded
= file
.ReadUInt32();
142 cout
<< "\n\tUploaded : " << uploaded
;
143 cout
<< "\n\tDownloaded : " << downloaded
;
144 cout
<< "\n\tLast Seen : " << CTimeT(file
.ReadUInt32());
145 uint32_t uphi
= file
.ReadUInt32();
146 uint32_t downhi
= file
.ReadUInt32();
147 uint64_t totalup
= (static_cast<uint64_t>(uphi
) << 32) + uploaded
;
148 uint64_t totaldown
= (static_cast<uint64_t>(downhi
) << 32) + downloaded
;
149 cout
<< "\n\tUploadedHI : " << uphi
<< " Total : " << totalup
<< " (" << CastItoXBytes(totalup
) << ')';
150 cout
<< "\n\tDownloadedHI : " << downhi
<< " Total : " << totaldown
<< " (" << CastItoXBytes(totaldown
) << ')';
151 cout
<< "\n\t(Reserved) : " << file
.ReadUInt16();
152 uint8_t keysize
= file
.ReadUInt8();
153 cout
<< "\n\tKeySize : " << (unsigned)keysize
;
154 cout
<< "\n\tKey Data : ";
155 char buf
[MAXPUBKEYSIZE
];
156 file
.Read(buf
, MAXPUBKEYSIZE
);
157 PrintByteArray(buf
, MAXPUBKEYSIZE
);
159 if (keysize
> MAXPUBKEYSIZE
) {
160 cerr
<< "\n*** Corruption found while reading credit file! ***\n" << endl
;
166 static inline void PrintDateFromFile(const CFileDataIO
& file
, const wxString
& prefix
= wxEmptyString
)
168 cout
<< prefix
<< "LastChanged : " << CTimeT(file
.ReadUInt32()) << '\n';
171 static void PrintHashsetFromFile(const CFileDataIO
& file
, const wxString
& prefix
= wxEmptyString
)
173 cout
<< prefix
<< "FileHash : " << file
.ReadHash() << '\n';
175 uint16_t parts
= file
.ReadUInt16();
176 cout
<< prefix
<< "Parts : " << parts
<< '\n';
178 cout
<< prefix
<< "HashSet : ";
179 for (uint16_t i
= 0; i
< parts
; i
++)
184 cout
<< file
.ReadHash();
189 void DecodeKnownMet(const CFileDataIO
& file
)
191 uint8_t version
= file
.ReadUInt8();
192 cout
<< "Version : " << VersionInfo(version
, KnownFileList
) << endl
;
193 if (version
!= MET_HEADER
&& version
!= MET_HEADER_WITH_LARGEFILES
) {
194 cerr
<< "File seems to be corrupt, invalid version!" << endl
;
197 uint32_t records
= file
.ReadUInt32();
198 cout
<< "Records : " << records
<< '\n';
199 for (uint32_t i
= 0; i
< records
; i
++) {
200 PrintDateFromFile(file
, wxString::Format(wxT("#%u\t"), i
));
201 PrintHashsetFromFile(file
, wxT("\t"));
202 uint32_t tagCount
= file
.ReadUInt32();
203 cout
<< "\tTagCount : " << tagCount
<< '\n';
204 for (; tagCount
> 0; tagCount
--) {
205 CTag
tag(file
, true);
206 cout
<< "\t\t" << tag
<< '\n';
211 void DecodePartMetFile(const CFileDataIO
& file
)
213 uint16_t PartCount
= 0;
214 uint8_t version
= file
.ReadUInt8();
215 cout
<< "Version : " << VersionInfo(version
, PartMetFile
) << endl
;
216 if (version
!= PARTFILE_VERSION
&& version
!= PARTFILE_SPLITTEDVERSION
&& version
!= PARTFILE_VERSION_LARGEFILE
) {
217 cerr
<< "File seems to be corrupt, invalid version!" << endl
;
220 bool isnewstyle
= (version
== PARTFILE_SPLITTEDVERSION
);
223 file
.Seek(24, wxFromStart
);
225 file
.Seek(1, wxFromStart
);
226 if (test
[0] == 0 && test
[1] == 0 && test
[2] == 2 && test
[3] == 1) {
231 uint32_t temp
= file
.ReadUInt32();
233 PrintHashsetFromFile(file
);
235 file
.Seek(2, wxFromStart
);
236 PrintDateFromFile(file
);
237 cout
<< "FileHash : " << file
.ReadHash() << '\n';
240 PrintDateFromFile(file
);
241 PrintHashsetFromFile(file
);
243 uint32_t tagCount
= file
.ReadUInt32();
244 cout
<< "TagCount : " << tagCount
<< '\n';
245 for (; tagCount
> 0; tagCount
--) {
246 CTag
tag(file
, true);
247 if (tag
.GetNameID() == FT_FILESIZE
) {
248 PartCount
= (tag
.GetInt() + (PARTSIZE
- 1)) / PARTSIZE
;
250 cout
<< '\t' << tag
<< '\n';
252 if (isnewstyle
&& (file
.GetPosition() < file
.GetLength())) {
253 file
.Seek(1, wxFromCurrent
);
254 cout
<< "HashSet : ";
255 for (uint16_t i
= 0; i
< PartCount
&& (file
.GetPosition() + 16 < file
.GetLength()); i
++) {
259 cout
<< file
.ReadHash();
265 void DecodeStatisticsDat(const CFileDataIO
& file
)
267 uint8_t version
= file
.ReadUInt8();
268 cout
<< "Version : " << (unsigned)version
<< '\n';
270 uint64_t tmp
= file
.ReadUInt64();
271 cout
<< "Total sent bytes : " << tmp
<< " (" << CastItoXBytes(tmp
) << ")\n";
272 tmp
= file
.ReadUInt64();
273 cout
<< "Total received bytes : " << tmp
<< " (" << CastItoXBytes(tmp
) << ")\n";
277 void DecodeCanceledMet(const CFileDataIO
& file
)
279 uint8_t version
= file
.ReadUInt8();
280 cout
<< "Version : " << VersionInfo(version
, CanceledFileList
) << endl
;
281 if (version
!= CANCELEDFILE_VERSION
) {
282 cerr
<< "File seems to be corrupt, invalid version!" << endl
;
285 uint32_t records
= file
.ReadUInt32();
286 cout
<< "Records : " << records
<< '\n';
287 for (uint32_t i
= 0; i
< records
; i
++) {
288 cout
<< "\tFile hash : " << file
.ReadHash() << '\n';