2 // This file is part of the aMule Project.
4 // Copyright (c) 2008 Dévai Tamás ( gonosztopi@amule.org )
5 // Copyright (c) 2004-2008 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>
41 static wxString
VersionInfo(uint8_t version
, uint8_t type
)
43 wxString verStr
= wxT("??");
44 if (type
== PrefFile
) {
45 if (version
== PREFFILE_VERSION
) {
46 verStr
= wxT("PREFFILE_VERSION");
48 } else if (type
== PartMetFile
) {
49 if (version
== PARTFILE_VERSION
) {
50 verStr
= wxT("PARTFILE_VERSION");
51 } else if (version
== PARTFILE_SPLITTEDVERSION
) {
52 verStr
= wxT("PARTFILE_SPLITTEDVERSION");
53 } else if (version
== PARTFILE_VERSION_LARGEFILE
) {
54 verStr
= wxT("PARTFILE_VERSION_LARGEFILE");
56 } else if (type
== CreditFile
) {
57 if (version
== CREDITFILE_VERSION
) {
58 verStr
= wxT("CREDITFILE_VERSION");
60 } else if (type
== KnownFileList
) {
61 if (version
== MET_HEADER
) {
62 verStr
= wxT("MET_HEADER");
63 } else if (version
== MET_HEADER_WITH_LARGEFILES
) {
64 verStr
= wxT("MET_HEADER_WITH_LARGEFILES");
67 return hex(version
) + wxT(' ') + verStr
;
70 void DecodePreferencesDat(const CFileDataIO
& file
)
72 cout
<< "(Version): " << VersionInfo(file
.ReadUInt8(), PrefFile
);
73 cout
<< "\nUserHash : " << file
.ReadHash() << '\n';
76 void DecodeFriendList(const CFileDataIO
& file
)
78 uint8_t version
= file
.ReadUInt8();
79 cout
<< "Version : " << VersionInfo(version
, KnownFileList
) << endl
;
80 if (version
!= MET_HEADER
) {
81 cerr
<< "File seems to be corrupt, invalid version!" << endl
;
84 uint32_t records
= file
.ReadUInt32();
85 cout
<< "Records : " << records
<< '\n';
86 for (uint32_t i
= 0; i
< records
; i
++) {
87 cout
<< "\tUserHash : " << file
.ReadHash();
88 cout
<< "\n\tLasUsedIP : " << CeD2kIP(file
.ReadUInt32());
89 cout
<< "\n\tLasUsedPort: " << file
.ReadUInt16();
90 cout
<< "\n\tLastSeen : " << CTimeT(file
.ReadUInt32());
91 cout
<< "\n\tLastChatted: " << CTimeT(file
.ReadUInt32());
92 uint32_t tagCount
= file
.ReadUInt32();
93 cout
<< "\n\ttagCount : " << tagCount
<< '\n';
94 for (; tagCount
> 0; --tagCount
) {
96 cout
<< "\t\t" << CFriendTag(tag
) << '\n';
101 void DecodeServerMet(const CFileDataIO
& file
)
103 uint8_t version
= file
.ReadUInt8();
104 cout
<< "Version : " << VersionInfo(version
, KnownFileList
) << endl
;
105 if (version
!= 0xE0 && version
!= MET_HEADER
) {
106 cerr
<< "File seems to be corrupt, invalid version!" << endl
;
109 uint32_t ServerCount
= file
.ReadUInt32();
110 cout
<< "Count : " << ServerCount
<< '\n';
111 for (; ServerCount
> 0; --ServerCount
) {
112 cout
<< "\tIP : " << CeD2kIP(file
.ReadUInt32()) << '\n';
113 cout
<< "\tPort : " << file
.ReadUInt16() << '\n';
114 uint32_t tagCount
= file
.ReadUInt32();
115 cout
<< "\ttagCount : " << tagCount
<< '\n';
116 for (; tagCount
> 0; --tagCount
) {
117 CTag
tag(file
, true);
118 cout
<< "\t\t" << CServerTag(tag
) << '\n';
123 void DecodeClientsMet(const CFileDataIO
& file
)
125 uint8_t version
= file
.ReadUInt8();
126 cout
<< "Version : " << VersionInfo(version
, CreditFile
) << endl
;
127 if (version
!= CREDITFILE_VERSION
) {
128 cerr
<< "File seems to be corrupt, invalid version!" << endl
;
131 uint32_t count
= file
.ReadUInt32();
132 cout
<< "Count : " << count
<< '\n';
133 for (uint32_t i
= 0; i
< count
; i
++) {
134 cout
<< wxString::Format(wxT("#%u\tKey : "), i
) << file
.ReadHash();
135 uint32_t uploaded
= file
.ReadUInt32();
136 uint32_t downloaded
= file
.ReadUInt32();
137 cout
<< "\n\tUploaded : " << uploaded
;
138 cout
<< "\n\tDownloaded : " << downloaded
;
139 cout
<< "\n\tLast Seen : " << CTimeT(file
.ReadUInt32());
140 uint32_t uphi
= file
.ReadUInt32();
141 uint32_t downhi
= file
.ReadUInt32();
142 uint64_t totalup
= (static_cast<uint64_t>(uphi
) << 32) + uploaded
;
143 uint64_t totaldown
= (static_cast<uint64_t>(downhi
) << 32) + downloaded
;
144 cout
<< "\n\tUploadedHI : " << uphi
<< " Total : " << totalup
<< " (" << CastItoXBytes(totalup
) << ')';
145 cout
<< "\n\tDownloadedHI : " << downhi
<< " Total : " << totaldown
<< " (" << CastItoXBytes(totaldown
) << ')';
146 cout
<< "\n\t(Reserved) : " << file
.ReadUInt16();
147 uint8_t keysize
= file
.ReadUInt8();
148 cout
<< "\n\tKeySize : " << (unsigned)keysize
;
149 cout
<< "\n\tKey Data : ";
150 char buf
[MAXPUBKEYSIZE
];
151 file
.Read(buf
, MAXPUBKEYSIZE
);
152 PrintByteArray(buf
, MAXPUBKEYSIZE
);
154 if (keysize
> MAXPUBKEYSIZE
) {
155 cerr
<< "\n*** Corruption found while reading credit file! ***\n" << endl
;
161 static inline void PrintDateFromFile(const CFileDataIO
& file
, const wxString
& prefix
= wxEmptyString
)
163 cout
<< prefix
<< "LastChanged : " << CTimeT(file
.ReadUInt32()) << '\n';
166 static void PrintHashsetFromFile(const CFileDataIO
& file
, const wxString
& prefix
= wxEmptyString
)
168 cout
<< prefix
<< "FileHash : " << file
.ReadHash() << '\n';
170 uint16_t parts
= file
.ReadUInt16();
171 cout
<< prefix
<< "Parts : " << parts
<< '\n';
173 cout
<< prefix
<< "HashSet : ";
174 for (uint16_t i
= 0; i
< parts
; i
++)
179 cout
<< file
.ReadHash();
184 void DecodeKnownMet(const CFileDataIO
& file
)
186 uint8_t version
= file
.ReadUInt8();
187 cout
<< "Version : " << VersionInfo(version
, KnownFileList
) << endl
;
188 if (version
!= MET_HEADER
&& version
!= MET_HEADER_WITH_LARGEFILES
) {
189 cerr
<< "File seems to be corrupt, invalid version!" << endl
;
192 uint32_t records
= file
.ReadUInt32();
193 cout
<< "Records : " << records
<< '\n';
194 for (uint32_t i
= 0; i
< records
; i
++) {
195 PrintDateFromFile(file
, wxString::Format(wxT("#%u\t"), i
));
196 PrintHashsetFromFile(file
, wxT("\t"));
197 uint32_t tagCount
= file
.ReadUInt32();
198 cout
<< "\tTagCount : " << tagCount
<< '\n';
199 for (; tagCount
> 0; tagCount
--) {
200 CTag
tag(file
, true);
201 cout
<< "\t\t" << tag
<< '\n';
206 void DecodePartMetFile(const CFileDataIO
& file
)
208 uint16_t PartCount
= 0;
209 uint8_t version
= file
.ReadUInt8();
210 cout
<< "Version : " << VersionInfo(version
, PartMetFile
) << endl
;
211 if (version
!= PARTFILE_VERSION
&& version
!= PARTFILE_SPLITTEDVERSION
&& version
!= PARTFILE_VERSION_LARGEFILE
) {
212 cerr
<< "File seems to be corrupt, invalid version!" << endl
;
215 bool isnewstyle
= (version
== PARTFILE_SPLITTEDVERSION
);
218 file
.Seek(24, wxFromStart
);
220 file
.Seek(1, wxFromStart
);
221 if (test
[0] == 0 && test
[1] == 0 && test
[2] == 2 && test
[3] == 1) {
226 uint32_t temp
= file
.ReadUInt32();
228 PrintHashsetFromFile(file
);
230 file
.Seek(2, wxFromStart
);
231 PrintDateFromFile(file
);
232 cout
<< "FileHash : " << file
.ReadHash() << '\n';
235 PrintDateFromFile(file
);
236 PrintHashsetFromFile(file
);
238 uint32_t tagCount
= file
.ReadUInt32();
239 cout
<< "TagCount : " << tagCount
<< '\n';
240 for (; tagCount
> 0; tagCount
--) {
241 CTag
tag(file
, true);
242 if (tag
.GetNameID() == FT_FILESIZE
) {
243 PartCount
= (tag
.GetInt() + (PARTSIZE
- 1)) / PARTSIZE
;
245 cout
<< '\t' << tag
<< '\n';
247 if (isnewstyle
&& (file
.GetPosition() < file
.GetLength())) {
248 file
.Seek(1, wxFromCurrent
);
249 cout
<< "HashSet : ";
250 for (uint16_t i
= 0; i
< PartCount
&& (file
.GetPosition() + 16 < file
.GetLength()); i
++) {
254 cout
<< file
.ReadHash();