forked from irwir/eMule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collection.cpp
446 lines (401 loc) · 11.4 KB
/
Collection.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
//this file is part of eMule
//Copyright (C)2002-2005 Merkur ( devs@emule-project.net / http://www.emule-project.net )
//
//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., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "StdAfx.h"
#include "collection.h"
#include "KnownFile.h"
#include "CollectionFile.h"
#include "SafeFile.h"
#include "Packets.h"
#include "Preferences.h"
#include "SharedFilelist.h"
#include "emule.h"
#include "Log.h"
#include "md5sum.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define COLLECTION_FILE_VERSION1_INITIAL 0x01
#define COLLECTION_FILE_VERSION2_LARGEFILES 0x02
CCollection::CCollection(void)
: m_sCollectionName(_T(""))
, m_sCollectionAuthorName(_T(""))
, m_bTextFormat(false)
{
m_CollectionFilesMap.InitHashTable(1031);
m_sCollectionName.Format(_T("New Collection-%u"), ::GetTickCount());
m_pabyCollectionAuthorKey = NULL;
m_nKeySize = 0;
}
CCollection::CCollection(const CCollection* pCollection)
{
m_sCollectionName = pCollection->m_sCollectionName;
if (pCollection->m_pabyCollectionAuthorKey != NULL){
m_nKeySize = pCollection->m_nKeySize;
m_pabyCollectionAuthorKey = new BYTE[m_nKeySize];
memcpy(m_pabyCollectionAuthorKey, pCollection->m_pabyCollectionAuthorKey, m_nKeySize);
m_sCollectionAuthorName = pCollection->m_sCollectionAuthorName;
}
else{
m_nKeySize = 0;
m_pabyCollectionAuthorKey = NULL;
}
m_bTextFormat = pCollection->m_bTextFormat;
m_CollectionFilesMap.InitHashTable(1031);
POSITION pos = pCollection->m_CollectionFilesMap.GetStartPosition();
CCollectionFile* pCollectionFile;
CSKey key;
while( pos != NULL )
{
pCollection->m_CollectionFilesMap.GetNextAssoc( pos, key, pCollectionFile );
AddFileToCollection(pCollectionFile, true);
}
}
CCollection::~CCollection(void)
{
delete[] m_pabyCollectionAuthorKey;
POSITION pos = m_CollectionFilesMap.GetStartPosition();
CCollectionFile* pCollectionFile;
CSKey key;
while( pos != NULL )
{
m_CollectionFilesMap.GetNextAssoc( pos, key, pCollectionFile );
delete pCollectionFile;
}
m_CollectionFilesMap.RemoveAll();
}
CCollectionFile* CCollection::AddFileToCollection(CAbstractFile* pAbstractFile, bool bCreateClone)
{
CSKey key(pAbstractFile->GetFileHash());
CCollectionFile* pCollectionFile;
if (m_CollectionFilesMap.Lookup(key, pCollectionFile))
{
ASSERT(0);
return pCollectionFile;
}
pCollectionFile = NULL;
if(bCreateClone)
pCollectionFile = new CCollectionFile(pAbstractFile);
else if(pAbstractFile->IsKindOf(RUNTIME_CLASS(CCollectionFile)))
pCollectionFile = (CCollectionFile*)pAbstractFile;
if(pCollectionFile)
m_CollectionFilesMap.SetAt(key, pCollectionFile);
return pCollectionFile;
}
void CCollection::RemoveFileFromCollection(CAbstractFile* pAbstractFile)
{
CSKey key(pAbstractFile->GetFileHash());
CCollectionFile* pCollectionFile;
if (m_CollectionFilesMap.Lookup(key, pCollectionFile))
{
m_CollectionFilesMap.RemoveKey(key);
delete pCollectionFile;
}
else
ASSERT(0);
}
void CCollection::SetCollectionAuthorKey(const byte* abyCollectionAuthorKey, uint32 nSize)
{
delete[] m_pabyCollectionAuthorKey;
m_pabyCollectionAuthorKey = NULL;
m_nKeySize = 0;
if (abyCollectionAuthorKey != NULL){
m_pabyCollectionAuthorKey = new BYTE[nSize];
memcpy(m_pabyCollectionAuthorKey, abyCollectionAuthorKey, nSize);
m_nKeySize = nSize;
}
}
bool CCollection::InitCollectionFromFile(const CString& sFilePath, CString sFileName)
{
DEBUG_ONLY( sFileName.Replace(COLLECTION_FILEEXTENSION, _T("")) );
bool bCollectionLoaded = false;
CSafeFile data;
if(data.Open(sFilePath, CFile::modeRead | CFile::shareDenyWrite | CFile::typeBinary))
{
try
{
uint32 nVersion = data.ReadUInt32();
if(nVersion == COLLECTION_FILE_VERSION1_INITIAL || nVersion == COLLECTION_FILE_VERSION2_LARGEFILES)
{
uint32 headerTagCount = data.ReadUInt32();
while(headerTagCount)
{
CTag tag(&data, true);
switch(tag.GetNameID())
{
case FT_FILENAME:
{
if(tag.IsStr())
m_sCollectionName = tag.GetStr();
break;
}
case FT_COLLECTIONAUTHOR:
{
if(tag.IsStr())
m_sCollectionAuthorName = tag.GetStr();
break;
}
case FT_COLLECTIONAUTHORKEY:
{
if(tag.IsBlob())
{
SetCollectionAuthorKey(tag.GetBlob(), tag.GetBlobSize());
}
break;
}
}
headerTagCount--;
}
uint32 fileCount = data.ReadUInt32();
while(fileCount)
{
CCollectionFile* pCollectionFile = new CCollectionFile(&data);
if(pCollectionFile)
AddFileToCollection(pCollectionFile, false);
fileCount--;
}
bCollectionLoaded = true;
}
if (m_pabyCollectionAuthorKey != NULL){
bool bResult = false;
if (data.GetLength() > data.GetPosition()){
using namespace CryptoPP;
uint32 nPos = (uint32)data.GetPosition();
data.SeekToBegin();
BYTE* pMessage = new BYTE[nPos];
VERIFY( data.Read(pMessage, nPos) == nPos);
StringSource ss_Pubkey(m_pabyCollectionAuthorKey, m_nKeySize, true, 0);
RSASSA_PKCS1v15_SHA_Verifier pubkey(ss_Pubkey);
int nSignLen = (int)(data.GetLength() - data.GetPosition());
BYTE* pSignature = new BYTE[nSignLen ];
VERIFY( data.Read(pSignature, nSignLen) == (UINT)nSignLen);
bResult = pubkey.VerifyMessage(pMessage, nPos, pSignature, nSignLen);
delete[] pMessage;
delete[] pSignature;
}
if (!bResult){
DebugLogWarning(_T("Collection %s: Verifying of public key failed!"), m_sCollectionName);
delete[] m_pabyCollectionAuthorKey;
m_pabyCollectionAuthorKey = NULL;
m_nKeySize = 0;
m_sCollectionAuthorName = _T("");
}
else
DebugLog(_T("Collection %s: Public key verified"), m_sCollectionName);
}
else
m_sCollectionAuthorName = _T("");
data.Close();
}
catch(CFileException* error)
{
error->Delete();
return false;
}
catch(...)
{
ASSERT( false );
data.Close();
return false;
}
}
else
return false;
if(!bCollectionLoaded)
{
CStdioFile data;
if(data.Open(sFilePath, CFile::modeRead | CFile::shareDenyWrite | CFile::typeText))
{
try
{
CString sLink;
while(data.ReadString(sLink))
{
//Ignore all lines that start with #.
//These lines can be used for future features..
if(sLink.Find(_T("#")) != 0)
{
try
{
CCollectionFile* pCollectionFile = new CCollectionFile();
if (pCollectionFile->InitFromLink(sLink))
AddFileToCollection(pCollectionFile, false);
else
delete pCollectionFile;
}
catch(...)
{
ASSERT( false );
data.Close();
return false;
}
}
}
data.Close();
m_sCollectionName = sFileName;
bCollectionLoaded = true;
m_bTextFormat = true;
}
catch(CFileException* error)
{
error->Delete();
return false;
}
catch(...)
{
ASSERT( false );
data.Close();
return false;
}
}
}
return bCollectionLoaded;
}
void CCollection::WriteToFileAddShared(CryptoPP::RSASSA_PKCS1v15_SHA_Signer* pSignKey)
{
using namespace CryptoPP;
CString sFileName;
sFileName.Format(_T("%s%s"), m_sCollectionName, COLLECTION_FILEEXTENSION);
CString sFilePath;
sFilePath.Format(_T("%s\\%s"), thePrefs.GetMuleDirectory(EMULE_INCOMINGDIR), sFileName);
if(m_bTextFormat)
{
CStdioFile data;
if(data.Open(sFilePath, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite | CFile::typeText))
{
try
{
POSITION pos = m_CollectionFilesMap.GetStartPosition();
CCollectionFile* pCollectionFile;
CSKey key;
while( pos != NULL )
{
m_CollectionFilesMap.GetNextAssoc( pos, key, pCollectionFile );
CString sLink;
sLink.Format(_T("%s\n"), pCollectionFile->GetED2kLink());
data.WriteString(sLink);
}
data.Close();
}
catch(CFileException* error)
{
error->Delete();
return;
}
catch(...)
{
ASSERT( false );
data.Close();
return;
}
}
}
else
{
CSafeFile data;
if(data.Open(sFilePath, CFile::modeCreate | CFile::modeReadWrite | CFile::shareDenyWrite | CFile::typeBinary))
{
try
{
//Version
// check first if we have any large files in the map - write use lowest version possible
uint32 dwVersion = COLLECTION_FILE_VERSION1_INITIAL;
POSITION pos = m_CollectionFilesMap.GetStartPosition();
CCollectionFile* pCollectionFile;
CSKey key;
while( pos != NULL ) {
m_CollectionFilesMap.GetNextAssoc( pos, key, pCollectionFile );
if (pCollectionFile->IsLargeFile()){
dwVersion = COLLECTION_FILE_VERSION2_LARGEFILES;
break;
}
}
data.WriteUInt32(dwVersion);
uint32 uTagCount = 1;
//NumberHeaderTags
if(m_pabyCollectionAuthorKey != NULL)
uTagCount += 2;
data.WriteUInt32(uTagCount);
CTag collectionName(FT_FILENAME, m_sCollectionName);
collectionName.WriteTagToFile(&data, utf8strRaw);
if(m_pabyCollectionAuthorKey != NULL){
CTag collectionAuthor(FT_COLLECTIONAUTHOR, m_sCollectionAuthorName);
collectionAuthor.WriteTagToFile(&data, utf8strRaw);
CTag collectionAuthorKey(FT_COLLECTIONAUTHORKEY, m_nKeySize, m_pabyCollectionAuthorKey);
collectionAuthorKey.WriteTagToFile(&data, utf8strRaw);
}
//Total Files
data.WriteUInt32(m_CollectionFilesMap.GetSize());
pos = m_CollectionFilesMap.GetStartPosition();
while( pos != NULL ) {
m_CollectionFilesMap.GetNextAssoc( pos, key, pCollectionFile );
pCollectionFile->WriteCollectionInfo(&data);
}
if (pSignKey != NULL){
uint32 nPos = (uint32)data.GetPosition();
data.SeekToBegin();
BYTE* pBuffer = new BYTE[nPos];
VERIFY( data.Read(pBuffer, nPos) == nPos);
SecByteBlock sbbSignature(pSignKey->SignatureLength());
AutoSeededRandomPool rng;
pSignKey->SignMessage(rng, pBuffer ,nPos , sbbSignature.begin());
BYTE abyBuffer2[500];
ArraySink asink(abyBuffer2, 500);
asink.Put(sbbSignature.begin(), sbbSignature.size());
int nResult = (uint8)asink.TotalPutLength();
data.Write(abyBuffer2, nResult);
delete[] pBuffer;
}
data.Close();
}
catch(CFileException* error)
{
error->Delete();
return;
}
catch(...)
{
ASSERT( false );
data.Close();
return;
}
}
}
theApp.sharedfiles->AddFileFromNewlyCreatedCollection(sFilePath);
}
bool CCollection::HasCollectionExtention(const CString& sFileName)
{
if(sFileName.Find(COLLECTION_FILEEXTENSION) == -1)
return false;
return true;
}
CString CCollection::GetCollectionAuthorKeyString(){
if (m_pabyCollectionAuthorKey != NULL)
return EncodeBase16(m_pabyCollectionAuthorKey, m_nKeySize);
else
return CString(_T(""));
}
CString CCollection::GetAuthorKeyHashString(){
if (m_pabyCollectionAuthorKey != NULL){
MD5Sum md5(m_pabyCollectionAuthorKey, m_nKeySize);
CString strResult = md5.GetHash();
strResult.MakeUpper();
return strResult;
}
return CString(_T(""));
}