-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathTag.cpp
471 lines (400 loc) · 11.1 KB
/
Tag.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//
// This file is part of the aMule Project.
//
// Copyright (c) 2003-2011 aMule Team ( [email protected] / http://www.amule.org )
// Copyright (c) 2002-2011 Merkur ( [email protected] / http://www.emule-project.net )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
#include "Tag.h" // Interface declarations
#include <common/Format.h> // Needed for WXLONGLONGFMTSPEC
#include "SafeFile.h" // Needed for CFileDataIO
#include "MD4Hash.h" // Needed for CMD4Hash
#include "CompilerSpecific.h" // Needed for __FUNCTION__
///////////////////////////////////////////////////////////////////////////////
// CTag
CTag::CTag(const wxString& Name)
{
m_uType = 0;
m_uName = 0;
m_Name = Name;
m_uVal = 0;
m_nSize = 0;
}
CTag::CTag(uint8 uName)
{
m_uType = 0;
m_uName = uName;
m_uVal = 0;
m_nSize = 0;
}
CTag::CTag(const CTag& rTag)
{
m_uType = rTag.m_uType;
m_uName = rTag.m_uName;
m_Name = rTag.m_Name;
m_nSize = 0;
if (rTag.IsStr()) {
m_pstrVal = new wxString(rTag.GetStr());
} else if (rTag.IsInt()) {
m_uVal = rTag.GetInt();
} else if (rTag.IsFloat()) {
m_fVal = rTag.GetFloat();
} else if (rTag.IsHash()) {
m_hashVal = new CMD4Hash(rTag.GetHash());
} else if (rTag.IsBlob()) {
m_nSize = rTag.GetBlobSize();
m_pData = new unsigned char[rTag.GetBlobSize()];
memcpy(m_pData, rTag.GetBlob(), rTag.GetBlobSize());
} else if (rTag.IsBsob()) {
m_nSize = rTag.GetBsobSize();
m_pData = new unsigned char[rTag.GetBsobSize()];
memcpy(m_pData, rTag.GetBsob(), rTag.GetBsobSize());
} else {
wxFAIL;
m_uVal = 0;
}
}
CTag::CTag(const CFileDataIO& data, bool bOptUTF8)
{
// Zero variables to allow for safe deletion
m_uType = m_uName = m_nSize = m_uVal = 0;
m_pData = NULL;
try {
m_uType = data.ReadUInt8();
if (m_uType & 0x80) {
m_uType &= 0x7F;
m_uName = data.ReadUInt8();
} else {
uint16 length = data.ReadUInt16();
if (length == 1) {
m_uName = data.ReadUInt8();
} else {
m_uName = 0;
m_Name = data.ReadOnlyString(utf8strNone,length);
}
}
// NOTE: It's very important that we read the *entire* packet data,
// even if we do not use each tag. Otherwise we will get in trouble
// when the packets are returned in a list - like the search results
// from a server. If we cannot do this, then we throw an exception.
switch (m_uType) {
case TAGTYPE_STRING:
m_pstrVal = new wxString(data.ReadString(bOptUTF8));
break;
case TAGTYPE_UINT32:
m_uVal = data.ReadUInt32();
break;
case TAGTYPE_UINT64:
m_uVal = data.ReadUInt64();
break;
case TAGTYPE_UINT16:
m_uVal = data.ReadUInt16();
m_uType = TAGTYPE_UINT32;
break;
case TAGTYPE_UINT8:
m_uVal = data.ReadUInt8();
m_uType = TAGTYPE_UINT32;
break;
case TAGTYPE_FLOAT32:
//#warning Endianness problem?
data.Read(&m_fVal, 4);
break;
case TAGTYPE_HASH16:
m_hashVal = new CMD4Hash(data.ReadHash());
break;
case TAGTYPE_BOOL:
printf("***NOTE: %s; Reading BOOL tag\n", __FUNCTION__);
data.ReadUInt8();
break;
case TAGTYPE_BOOLARRAY: {
printf("***NOTE: %s; Reading BOOL Array tag\n", __FUNCTION__);
uint16 len = data.ReadUInt16();
// 07-Apr-2004: eMule versions prior to 0.42e.29 used the formula "(len+7)/8"!
//#warning This seems to be off by one! 8 / 8 + 1 == 2, etc.
data.Seek((len / 8) + 1, wxFromCurrent);
break;
}
case TAGTYPE_BLOB:
// 07-Apr-2004: eMule versions prior to 0.42e.29 handled the "len" as int16!
m_nSize = data.ReadUInt32();
// Since the length is 32b, this check is needed to avoid
// huge allocations in case of bad tags.
if (m_nSize > data.GetLength() - data.GetPosition()) {
throw CInvalidPacket(wxT("Malformed tag"));
}
m_pData = new unsigned char[m_nSize];
data.Read(m_pData, m_nSize);
break;
default:
if (m_uType >= TAGTYPE_STR1 && m_uType <= TAGTYPE_STR16) {
uint8 length = m_uType - TAGTYPE_STR1 + 1;
m_pstrVal = new wxString(data.ReadOnlyString(bOptUTF8, length));
m_uType = TAGTYPE_STRING;
} else {
// Since we cannot determine the length of this tag, we
// simply have to abort reading the file.
throw CInvalidPacket(CFormat(wxT("Unknown tag type encountered %x, cannot proceed!")) % m_uType);
}
}
} catch (...) {
if (m_uType == TAGTYPE_BLOB) {
delete[] m_pData;
}
throw;
}
}
CTag::~CTag()
{
if (IsStr()) {
delete m_pstrVal;
} else if (IsHash()) {
delete m_hashVal;
} else if (IsBlob() || IsBsob()) {
delete[] m_pData;
}
}
CTag &CTag::operator=(const CTag &rhs)
{
if (&rhs != this) {
m_uType = rhs.m_uType;
m_uName = rhs.m_uName;
m_Name = rhs.m_Name;
m_nSize = 0;
if (rhs.IsStr()) {
wxString *p = new wxString(rhs.GetStr());
delete m_pstrVal;
m_pstrVal = p;
} else if (rhs.IsInt()) {
m_uVal = rhs.GetInt();
} else if (rhs.IsFloat()) {
m_fVal = rhs.GetFloat();
} else if (rhs.IsHash()) {
CMD4Hash *p = new CMD4Hash(rhs.GetHash());
delete m_hashVal;
m_hashVal = p;
} else if (rhs.IsBlob()) {
m_nSize = rhs.GetBlobSize();
unsigned char *p = new unsigned char[rhs.GetBlobSize()];
delete [] m_pData;
m_pData = p;
memcpy(m_pData, rhs.GetBlob(), rhs.GetBlobSize());
} else if (rhs.IsBsob()) {
m_nSize = rhs.GetBsobSize();
unsigned char *p = new unsigned char[rhs.GetBsobSize()];
delete [] m_pData;
m_pData = p;
memcpy(m_pData, rhs.GetBsob(), rhs.GetBsobSize());
} else {
wxFAIL;
m_uVal = 0;
}
}
return *this;
}
#define CHECK_TAG_TYPE(check, expected) \
if (!(check)) { \
throw CInvalidPacket(wxT(#expected) wxT(" tag expected, but found ") + GetFullInfo()); \
}
uint64 CTag::GetInt() const
{
CHECK_TAG_TYPE(IsInt(), Integer);
return m_uVal;
}
const wxString& CTag::GetStr() const
{
CHECK_TAG_TYPE(IsStr(), String);
return *m_pstrVal;
}
float CTag::GetFloat() const
{
CHECK_TAG_TYPE(IsFloat(), Float);
return m_fVal;
}
const CMD4Hash& CTag::GetHash() const
{
CHECK_TAG_TYPE(IsHash(), Hash);
return *m_hashVal;
}
uint32 CTag::GetBlobSize() const
{
CHECK_TAG_TYPE(IsBlob(), Blob);
return m_nSize;
}
const uint8_t* CTag::GetBlob() const
{
CHECK_TAG_TYPE(IsBlob(), Blob);
return m_pData;
}
uint32 CTag::GetBsobSize() const
{
CHECK_TAG_TYPE(IsBsob(), Bsob);
return m_nSize;
}
const uint8_t* CTag::GetBsob() const
{
CHECK_TAG_TYPE(IsBsob(), Bsob);
return m_pData;
}
bool CTag::WriteNewEd2kTag(CFileDataIO* data, EUtf8Str eStrEncode) const
{
// Write tag type
uint8 uType;
if (IsInt()) {
if (m_uVal <= 0xFF) {
uType = TAGTYPE_UINT8;
} else if (m_uVal <= 0xFFFF) {
uType = TAGTYPE_UINT16;
} else if (m_uVal <= 0xFFFFFFFF) {
uType = TAGTYPE_UINT32;
} else {
uType = TAGTYPE_UINT64;
}
} else if (IsStr()) {
uint16 uStrValLen = GetRawSize(*m_pstrVal, eStrEncode);
if (uStrValLen >= 1 && uStrValLen <= 16) {
uType = TAGTYPE_STR1 + uStrValLen - 1;
} else {
uType = TAGTYPE_STRING;
}
} else {
uType = m_uType;
}
// Write tag name
if (!m_Name.IsEmpty()) {
data->WriteUInt8(uType);
data->WriteString(m_Name,utf8strNone);
} else {
wxASSERT( m_uName != 0 );
data->WriteUInt8(uType | 0x80);
data->WriteUInt8(m_uName);
}
// Write tag data
switch (uType) {
case TAGTYPE_STRING:
data->WriteString(*m_pstrVal,eStrEncode);
break;
case TAGTYPE_UINT64:
data->WriteUInt64(m_uVal);
break;
case TAGTYPE_UINT32:
data->WriteUInt32(m_uVal);
break;
case TAGTYPE_UINT16:
data->WriteUInt16(m_uVal);
break;
case TAGTYPE_UINT8:
data->WriteUInt8(m_uVal);
break;
case TAGTYPE_FLOAT32:
//#warning Endianness problem?
data->Write(&m_fVal, 4);
break;
case TAGTYPE_HASH16:
data->WriteHash(*m_hashVal);
break;
case TAGTYPE_BLOB:
data->WriteUInt32(m_nSize);
data->Write(m_pData, m_nSize);
break;
default:
// See comment on the default: of CTag::CTag(const CFileDataIO& data, bool bOptUTF8)
if (uType >= TAGTYPE_STR1 && uType <= TAGTYPE_STR16) {
// Sending '0' as len size makes it not write the len on the IO file.
// This is because this tag types send the len as their type.
data->WriteString(*m_pstrVal,eStrEncode,0);
} else {
printf("%s; Unknown tag: type=0x%02X\n", __FUNCTION__, uType);
wxFAIL;
return false;
}
break;
}
return true;
}
bool CTag::WriteTagToFile(CFileDataIO* file, EUtf8Str WXUNUSED(eStrEncode), bool restrictive) const
{
// Don't write tags of unknown types, we wouldn't be able to read them in again
// and the met file would be corrupted
if (!restrictive || (IsStr() || IsInt() || IsFloat() || IsBlob())) {
// If this fails, it'll throw.
file->WriteTag(*this);
return true;
} else {
printf("%s; Ignored tag with unknown type=0x%02X\n", __FUNCTION__, m_uType);
return false;
}
}
wxString CTag::GetFullInfo() const
{
wxString strTag;
if (!m_Name.IsEmpty()) {
// Special case: Kad tags, and some ED2k tags ...
if (m_Name.Length() == 1) {
strTag = CFormat(wxT("0x%02X")) % (unsigned)m_Name[0];
} else {
strTag = wxT('\"');
strTag += m_Name;
strTag += wxT('\"');
}
} else {
strTag = CFormat(wxT("0x%02X")) % m_uName;
}
strTag += wxT("=");
if (m_uType == TAGTYPE_STRING) {
strTag += wxT("\"");
strTag += *m_pstrVal;
strTag += wxT("\"");
} else if (m_uType >= TAGTYPE_STR1 && m_uType <= TAGTYPE_STR16) {
strTag += CFormat(wxT("(Str%u)\"")) % (m_uType - TAGTYPE_STR1 + 1)
+ *m_pstrVal + wxT("\"");
} else if (m_uType == TAGTYPE_UINT64) {
strTag += CFormat(wxT("(Int64)%u")) % m_uVal;
} else if (m_uType == TAGTYPE_UINT32) {
strTag += CFormat(wxT("(Int32)%u")) % m_uVal;
} else if (m_uType == TAGTYPE_UINT16) {
strTag += CFormat(wxT("(Int16)%u")) % m_uVal;
} else if (m_uType == TAGTYPE_UINT8) {
strTag += CFormat(wxT("(Int8)%u")) % m_uVal;
} else if (m_uType == TAGTYPE_FLOAT32) {
strTag += CFormat(wxT("(Float32)%f")) % m_fVal;
} else if (m_uType == TAGTYPE_BLOB) {
strTag += CFormat(wxT("(Blob)%u")) % m_nSize;
} else if (m_uType == TAGTYPE_BSOB) {
strTag += CFormat(wxT("(Bsob)%u")) % m_nSize;
} else {
strTag += CFormat(wxT("Type=%u")) % m_uType;
}
return strTag;
}
CTagHash::CTagHash(const wxString& name, const CMD4Hash& value)
: CTag(name) {
m_hashVal = new CMD4Hash(value);
m_uType = TAGTYPE_HASH16;
}
CTagHash::CTagHash(uint8 name, const CMD4Hash& value)
: CTag(name) {
m_hashVal = new CMD4Hash(value);
m_uType = TAGTYPE_HASH16;
}
void deleteTagPtrListEntries(TagPtrList* taglist)
{
DeleteContents(*taglist);
}
// File_checked_for_headers