forked from amule-project/amule
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSHAHashSet.cpp
More file actions
1267 lines (1168 loc) · 40.7 KB
/
Copy pathSHAHashSet.cpp
File metadata and controls
1267 lines (1168 loc) · 40.7 KB
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// This file is part of the aMule Project.
//
// Copyright (c) 2004-2011 Angel Vidal ( [email protected] )
// Copyright (c) 2003-2026 aMule Team ( https://amule-org.github.io )
// 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 <wx/file.h>
#include "SHAHashSet.h"
#include "amule.h"
#include "MemFile.h"
#include "Preferences.h"
#include "SHA.h"
#include "updownclient.h"
#include "DownloadQueue.h"
#include "PartFile.h"
#include "Logger.h"
#include <common/Format.h>
// for this version the limits are set very high, they might be lowered later
// to make a hash trustworthy, at least 10 unique Ips (255.255.128.0) must have sent it
// and if we have received more than one hash for the file, one hash has to be sent by more than 95% of all
// unique IPs
#define MINUNIQUEIPS_TOTRUST 10 // how many unique IPs have to send us a hash to make it trustworthy
#define MINPERCENTAGE_TOTRUST \
92 // how many percentage of clients have to send the same hash to make it trustworthy
CAICHRequestedDataList CAICHHashSet::m_liRequestedData;
// Lazily-built index mapping root hash → file offset in known2.met.
// See SaveHashSet (dedup-on-append) and LoadHashSet (O(1) lookup for
// incoming AICH requests) for usage.
wxMutex CAICHHashSet::s_rootHashCacheMutex;
std::unordered_map<CAICHHash, uint64> CAICHHashSet::s_rootHashCache;
bool CAICHHashSet::s_rootHashCacheLoaded = false;
/////////////////////////////////////////////////////////////////////////////////////////
/// CAICHHash
wxString CAICHHash::GetString() const
{
return EncodeBase32(m_abyBuffer, HASHSIZE);
}
void CAICHHash::Read(CFileDataIO *file)
{
file->Read(m_abyBuffer, HASHSIZE);
}
void CAICHHash::Write(CFileDataIO *file) const
{
file->Write(m_abyBuffer, HASHSIZE);
}
unsigned int CAICHHash::DecodeBase32(const wxString &base32)
{
return ::DecodeBase32(base32, HASHSIZE, m_abyBuffer);
}
/////////////////////////////////////////////////////////////////////////////////////////
/// CAICHHashTree
CAICHHashTree::CAICHHashTree(uint64 nDataSize, bool bLeftBranch, uint64 nBaseSize)
{
m_nDataSize = nDataSize;
m_nBaseSize = nBaseSize;
m_bIsLeftBranch = bLeftBranch;
m_pLeftTree = NULL;
m_pRightTree = NULL;
m_bHashValid = false;
}
CAICHHashTree::~CAICHHashTree()
{
delete m_pLeftTree;
delete m_pRightTree;
}
// recursive
CAICHHashTree *CAICHHashTree::FindHash(uint64 nStartPos, uint64 nSize, uint8 *nLevel)
{
(*nLevel)++;
wxCHECK(*nLevel <= 22, NULL);
wxCHECK(nStartPos + nSize <= m_nDataSize, NULL);
wxCHECK(nSize <= m_nDataSize, NULL);
if (nStartPos == 0 && nSize == m_nDataSize) {
// this is the searched hash
return this;
} else if (m_nDataSize <= m_nBaseSize) { // sanity
// this is already the last level, can't go deeper
wxFAIL;
return NULL;
} else {
uint64 nBlocks = m_nDataSize / m_nBaseSize + ((m_nDataSize % m_nBaseSize != 0) ? 1 : 0);
uint64 nLeft = (((m_bIsLeftBranch) ? nBlocks + 1 : nBlocks) / 2) * m_nBaseSize;
uint64 nRight = m_nDataSize - nLeft;
if (nStartPos < nLeft) {
if (nStartPos + nSize > nLeft) { // sanity
wxFAIL;
return NULL;
}
if (m_pLeftTree == NULL) {
m_pLeftTree = new CAICHHashTree(
nLeft, true, (nLeft <= PARTSIZE) ? EMBLOCKSIZE : PARTSIZE);
} else {
wxASSERT(m_pLeftTree->m_nDataSize == nLeft);
}
return m_pLeftTree->FindHash(nStartPos, nSize, nLevel);
} else {
nStartPos -= nLeft;
if (nStartPos + nSize > nRight) { // sanity
wxFAIL;
return NULL;
}
if (m_pRightTree == NULL) {
m_pRightTree = new CAICHHashTree(
nRight, false, (nRight <= PARTSIZE) ? EMBLOCKSIZE : PARTSIZE);
} else {
wxASSERT(m_pRightTree->m_nDataSize == nRight);
}
return m_pRightTree->FindHash(nStartPos, nSize, nLevel);
}
}
}
// recursive
// calculates missing hash from the existing ones
// overwrites existing hashs
// fails if no hash is found for any branch
bool CAICHHashTree::ReCalculateHash(CAICHHashAlgo *hashalg, bool bDontReplace)
{
if (m_pLeftTree && m_pRightTree) {
if (!m_pLeftTree->ReCalculateHash(hashalg, bDontReplace) ||
!m_pRightTree->ReCalculateHash(hashalg, bDontReplace)) {
return false;
}
if (bDontReplace && m_bHashValid) {
return true;
}
if (m_pRightTree->m_bHashValid && m_pLeftTree->m_bHashValid) {
hashalg->Reset();
hashalg->Add(m_pLeftTree->m_Hash.GetRawHash(), HASHSIZE);
hashalg->Add(m_pRightTree->m_Hash.GetRawHash(), HASHSIZE);
hashalg->Finish(m_Hash);
m_bHashValid = true;
return true;
} else {
return m_bHashValid;
}
} else if (m_pLeftTree == NULL && m_pRightTree == NULL) {
return true;
} else {
AddDebugLogLineN(logSHAHashSet, "ReCalculateHash failed - Hashtree incomplete");
return false;
}
}
bool CAICHHashTree::VerifyHashTree(CAICHHashAlgo *hashalg, bool bDeleteBadTrees)
{
if (!m_bHashValid) {
wxFAIL;
if (bDeleteBadTrees) {
if (m_pLeftTree) {
delete m_pLeftTree;
m_pLeftTree = NULL;
}
if (m_pRightTree) {
delete m_pRightTree;
m_pRightTree = NULL;
}
}
AddDebugLogLineN(logSHAHashSet, "VerifyHashTree - No masterhash available");
return false;
}
// calculated missing hashs without overwriting anything
if (m_pLeftTree && !m_pLeftTree->m_bHashValid) {
m_pLeftTree->ReCalculateHash(hashalg, true);
}
if (m_pRightTree && !m_pRightTree->m_bHashValid) {
m_pRightTree->ReCalculateHash(hashalg, true);
}
if ((m_pRightTree && m_pRightTree->m_bHashValid) ^ (m_pLeftTree && m_pLeftTree->m_bHashValid)) {
// one branch can never be verified
if (bDeleteBadTrees) {
if (m_pLeftTree) {
delete m_pLeftTree;
m_pLeftTree = NULL;
}
if (m_pRightTree) {
delete m_pRightTree;
m_pRightTree = NULL;
}
}
AddDebugLogLineN(logSHAHashSet, "VerifyHashSet failed - Hashtree incomplete");
return false;
}
if ((m_pRightTree && m_pRightTree->m_bHashValid) && (m_pLeftTree && m_pLeftTree->m_bHashValid)) {
// check verify the hashs of both child nodes against my hash
CAICHHash CmpHash;
hashalg->Reset();
hashalg->Add(m_pLeftTree->m_Hash.GetRawHash(), HASHSIZE);
hashalg->Add(m_pRightTree->m_Hash.GetRawHash(), HASHSIZE);
hashalg->Finish(CmpHash);
if (m_Hash != CmpHash) {
if (bDeleteBadTrees) {
if (m_pLeftTree) {
delete m_pLeftTree;
m_pLeftTree = NULL;
}
if (m_pRightTree) {
delete m_pRightTree;
m_pRightTree = NULL;
}
}
return false;
}
return m_pLeftTree->VerifyHashTree(hashalg, bDeleteBadTrees) &&
m_pRightTree->VerifyHashTree(hashalg, bDeleteBadTrees);
} else {
// last hash in branch - nothing below to verify
return true;
}
}
void CAICHHashTree::SetBlockHash(uint64 nSize, uint64 nStartPos, CAICHHashAlgo *pHashAlg)
{
wxASSERT(nSize <= EMBLOCKSIZE);
CAICHHashTree *pToInsert = FindHash(nStartPos, nSize);
if (pToInsert == NULL) { // sanity
wxFAIL;
AddDebugLogLineN(
logSHAHashSet, "Critical Error: Failed to Insert SHA-HashBlock, FindHash() failed!");
return;
}
// sanity
if (pToInsert->m_nBaseSize != EMBLOCKSIZE || pToInsert->m_nDataSize != nSize) {
wxFAIL;
AddDebugLogLineN(
logSHAHashSet, "Critical Error: Logical error on values in SetBlockHashFromData");
return;
}
pHashAlg->Finish(pToInsert->m_Hash);
pToInsert->m_bHashValid = true;
}
bool CAICHHashTree::CreatePartRecoveryData(
uint64 nStartPos, uint64 nSize, CFileDataIO *fileDataOut, uint32 wHashIdent, bool b32BitIdent)
{
wxCHECK(nStartPos + nSize <= m_nDataSize, false);
wxCHECK(nSize <= m_nDataSize, false);
if (nStartPos == 0 && nSize == m_nDataSize) {
// this is the searched part, now write all blocks of this part
// hashident for this level will be adjusted by WriteLowestLevelHash
return WriteLowestLevelHashs(fileDataOut, wHashIdent, false, b32BitIdent);
} else if (m_nDataSize <= m_nBaseSize) { // sanity
// this is already the last level, can't go deeper
wxFAIL;
return false;
} else {
wHashIdent <<= 1;
wHashIdent |= (m_bIsLeftBranch) ? 1 : 0;
uint64 nBlocks = m_nDataSize / m_nBaseSize + ((m_nDataSize % m_nBaseSize != 0) ? 1 : 0);
uint64 nLeft = (((m_bIsLeftBranch) ? nBlocks + 1 : nBlocks) / 2) * m_nBaseSize;
uint64 nRight = m_nDataSize - nLeft;
if (m_pLeftTree == NULL || m_pRightTree == NULL) {
wxFAIL;
return false;
}
if (nStartPos < nLeft) {
if (nStartPos + nSize > nLeft || !m_pRightTree->m_bHashValid) { // sanity
wxFAIL;
return false;
}
m_pRightTree->WriteHash(fileDataOut, wHashIdent, b32BitIdent);
return m_pLeftTree->CreatePartRecoveryData(
nStartPos, nSize, fileDataOut, wHashIdent, b32BitIdent);
} else {
nStartPos -= nLeft;
if (nStartPos + nSize > nRight || !m_pLeftTree->m_bHashValid) { // sanity
wxFAIL;
return false;
}
m_pLeftTree->WriteHash(fileDataOut, wHashIdent, b32BitIdent);
return m_pRightTree->CreatePartRecoveryData(
nStartPos, nSize, fileDataOut, wHashIdent, b32BitIdent);
}
}
}
void CAICHHashTree::WriteHash(CFileDataIO *fileDataOut, uint32 wHashIdent, bool b32BitIdent) const
{
wxASSERT(m_bHashValid);
wHashIdent <<= 1;
wHashIdent |= (m_bIsLeftBranch) ? 1 : 0;
if (!b32BitIdent) {
wxASSERT(wHashIdent <= 0xFFFF);
fileDataOut->WriteUInt16((uint16)wHashIdent);
} else {
fileDataOut->WriteUInt32(wHashIdent);
}
m_Hash.Write(fileDataOut);
}
// write lowest level hashs into file, ordered from left to right optional without identifier
bool CAICHHashTree::WriteLowestLevelHashs(
CFileDataIO *fileDataOut, uint32 wHashIdent, bool bNoIdent, bool b32BitIdent) const
{
wHashIdent <<= 1;
wHashIdent |= (m_bIsLeftBranch) ? 1 : 0;
if (m_pLeftTree == NULL && m_pRightTree == NULL) {
if (m_nDataSize <= m_nBaseSize && m_bHashValid) {
if (!bNoIdent && !b32BitIdent) {
wxASSERT(wHashIdent <= 0xFFFF);
fileDataOut->WriteUInt16((uint16)wHashIdent);
} else if (!bNoIdent && b32BitIdent) {
fileDataOut->WriteUInt32(wHashIdent);
}
m_Hash.Write(fileDataOut);
return true;
} else {
wxFAIL;
return false;
}
} else if (m_pLeftTree == NULL || m_pRightTree == NULL) {
wxFAIL;
return false;
} else {
return m_pLeftTree->WriteLowestLevelHashs(fileDataOut, wHashIdent, bNoIdent, b32BitIdent) &&
m_pRightTree->WriteLowestLevelHashs(fileDataOut, wHashIdent, bNoIdent, b32BitIdent);
}
}
// recover all low level hashs from given data. hashs are assumed to be ordered in left to right - no
// identifier used
bool CAICHHashTree::LoadLowestLevelHashs(CFileDataIO *fileInput)
{
if (m_nDataSize <= m_nBaseSize) { // sanity
// lowest level, read hash
m_Hash.Read(fileInput);
m_bHashValid = true;
return true;
} else {
uint64 nBlocks = m_nDataSize / m_nBaseSize + ((m_nDataSize % m_nBaseSize != 0) ? 1 : 0);
uint64 nLeft = (((m_bIsLeftBranch) ? nBlocks + 1 : nBlocks) / 2) * m_nBaseSize;
uint64 nRight = m_nDataSize - nLeft;
if (m_pLeftTree == NULL) {
m_pLeftTree =
new CAICHHashTree(nLeft, true, (nLeft <= PARTSIZE) ? EMBLOCKSIZE : PARTSIZE);
} else {
wxASSERT(m_pLeftTree->m_nDataSize == nLeft);
}
if (m_pRightTree == NULL) {
m_pRightTree = new CAICHHashTree(
nRight, false, (nRight <= PARTSIZE) ? EMBLOCKSIZE : PARTSIZE);
} else {
wxASSERT(m_pRightTree->m_nDataSize == nRight);
}
return m_pLeftTree->LoadLowestLevelHashs(fileInput) &&
m_pRightTree->LoadLowestLevelHashs(fileInput);
}
}
// write the hash, specified by wHashIdent, with Data from fileInput.
bool CAICHHashTree::SetHash(CFileDataIO *fileInput, uint32 wHashIdent, sint8 nLevel, bool bAllowOverwrite)
{
if (nLevel == (-1)) {
// first call, check how many level we need to go
uint8 i = 0;
for (; i != 32 && (wHashIdent & 0x80000000) == 0; ++i) {
wHashIdent <<= 1;
}
if (i > 31) {
AddDebugLogLineN(
logSHAHashSet, "CAICHHashTree::SetHash - found invalid HashIdent (0)");
return false;
} else {
nLevel = 31 - i;
}
}
if (nLevel == 0) {
// this is the searched hash
if (m_bHashValid && !bAllowOverwrite) {
// not allowed to overwrite this hash, however move the filepointer as if we read a
// hash
fileInput->Seek(HASHSIZE, wxFromCurrent);
return true;
}
m_Hash.Read(fileInput);
m_bHashValid = true;
return true;
} else if (m_nDataSize <= m_nBaseSize) { // sanity
// this is already the last level, can't go deeper
wxFAIL;
return false;
} else {
// adjust ident to point the path to the next node
wHashIdent <<= 1;
nLevel--;
uint64 nBlocks = m_nDataSize / m_nBaseSize + ((m_nDataSize % m_nBaseSize != 0) ? 1 : 0);
uint64 nLeft = (((m_bIsLeftBranch) ? nBlocks + 1 : nBlocks) / 2) * m_nBaseSize;
uint64 nRight = m_nDataSize - nLeft;
if ((wHashIdent & 0x80000000) > 0) {
if (m_pLeftTree == NULL) {
m_pLeftTree = new CAICHHashTree(
nLeft, true, (nLeft <= PARTSIZE) ? EMBLOCKSIZE : PARTSIZE);
} else {
wxASSERT(m_pLeftTree->m_nDataSize == nLeft);
}
return m_pLeftTree->SetHash(fileInput, wHashIdent, nLevel);
} else {
if (m_pRightTree == NULL) {
m_pRightTree = new CAICHHashTree(
nRight, false, (nRight <= PARTSIZE) ? EMBLOCKSIZE : PARTSIZE);
} else {
wxASSERT(m_pRightTree->m_nDataSize == nRight);
}
return m_pRightTree->SetHash(fileInput, wHashIdent, nLevel);
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////
/// CAICHUntrustedHash
bool CAICHUntrustedHash::AddSigningIP(uint32 dwIP)
{
dwIP &= 0x00F0FFFF; // we use only the 20 most significant bytes for unique IPs
return m_adwIpsSigning.insert(dwIP).second;
}
/////////////////////////////////////////////////////////////////////////////////////////
/// CAICHHashSet
CAICHHashSet::CAICHHashSet(CKnownFile *pOwner)
: m_pHashTree(0, true, PARTSIZE)
{
m_eStatus = AICH_EMPTY;
m_pOwner = pOwner;
}
CAICHHashSet::~CAICHHashSet(void)
{
FreeHashSet();
}
bool CAICHHashSet::CreatePartRecoveryData(uint64 nPartStartPos, CFileDataIO *fileDataOut, bool bDbgDontLoad)
{
wxASSERT(m_pOwner);
if (m_pOwner->IsPartFile() || m_eStatus != AICH_HASHSETCOMPLETE) {
wxFAIL;
return false;
}
if (m_pHashTree.m_nDataSize <= EMBLOCKSIZE) {
wxFAIL;
return false;
}
if (!bDbgDontLoad) {
if (!LoadHashSet()) {
AddDebugLogLineN(logSHAHashSet,
CFormat("Created RecoveryData error: failed to load hashset. File: %s") %
m_pOwner->GetFileName());
SetStatus(AICH_ERROR);
return false;
}
}
bool bResult;
uint8 nLevel = 0;
uint32 nPartSize = min<uint64>(PARTSIZE, m_pOwner->GetFileSize() - nPartStartPos);
m_pHashTree.FindHash(nPartStartPos, nPartSize, &nLevel);
uint16 nHashsToWrite =
(nLevel - 1) + nPartSize / EMBLOCKSIZE + ((nPartSize % EMBLOCKSIZE != 0) ? 1 : 0);
const bool bUse32BitIdentifier = m_pOwner->IsLargeFile();
if (bUse32BitIdentifier) {
fileDataOut->WriteUInt16(0); // no 16bit hashs to write
}
fileDataOut->WriteUInt16(nHashsToWrite);
uint64 nCheckFilePos = fileDataOut->GetPosition();
if (m_pHashTree.CreatePartRecoveryData(
nPartStartPos, nPartSize, fileDataOut, 0, bUse32BitIdentifier)) {
if (nHashsToWrite * (HASHSIZE + (bUse32BitIdentifier ? 4u : 2u)) !=
fileDataOut->GetPosition() - nCheckFilePos) {
wxFAIL;
AddDebugLogLineN(logSHAHashSet,
CFormat("Created RecoveryData has wrong length. File: %s") %
m_pOwner->GetFileName());
bResult = false;
SetStatus(AICH_ERROR);
} else {
bResult = true;
}
} else {
AddDebugLogLineN(logSHAHashSet,
CFormat("Failed to create RecoveryData for '%s'") % m_pOwner->GetFileName());
bResult = false;
SetStatus(AICH_ERROR);
}
if (!bUse32BitIdentifier) {
fileDataOut->WriteUInt16(0); // no 32bit hashs to write
}
if (!bDbgDontLoad) {
FreeHashSet();
}
return bResult;
}
bool CAICHHashSet::ReadRecoveryData(uint64 nPartStartPos, CMemFile *fileDataIn)
{
if (/*eMule TODO !m_pOwner->IsPartFile() ||*/ !(
m_eStatus == AICH_VERIFIED || m_eStatus == AICH_TRUSTED)) {
wxFAIL;
return false;
}
/* V2 AICH Hash Packet:
<count1 uint16>
16bit-hashs-to-read
(<identifier uint16><hash HASHSIZE>)[count1] AICH hashs
<count2 uint16>
32bit-hashs-to-read
(<identifier uint32><hash HASHSIZE>)[count2] AICH hashs
*/
// at this time we check the recoverydata for the correct ammounts of hashs only
// all hash are then taken into the tree, depending on there hashidentifier (except the masterhash)
uint8 nLevel = 0;
uint32 nPartSize = min<uint64>(PARTSIZE, m_pOwner->GetFileSize() - nPartStartPos);
m_pHashTree.FindHash(nPartStartPos, nPartSize, &nLevel);
uint16 nHashsToRead =
(nLevel - 1) + nPartSize / EMBLOCKSIZE + ((nPartSize % EMBLOCKSIZE != 0) ? 1 : 0);
// read hashs with 16 bit identifier
uint16 nHashsAvailable = fileDataIn->ReadUInt16();
if (fileDataIn->GetLength() - fileDataIn->GetPosition() < nHashsToRead * (HASHSIZE + 2u) ||
(nHashsToRead != nHashsAvailable && nHashsAvailable != 0)) {
// this check is redundant, CSafememfile would catch such an error too
AddDebugLogLineN(logSHAHashSet,
CFormat("Failed to read RecoveryData for '%s' - Received datasize/amounts of hashs "
"was invalid") %
m_pOwner->GetFileName());
return false;
}
for (uint32 i = 0; i != nHashsAvailable; i++) {
uint16 wHashIdent = fileDataIn->ReadUInt16();
if (wHashIdent == 1 /*never allow masterhash to be overwritten*/
|| !m_pHashTree.SetHash(fileDataIn, wHashIdent, (-1), false)) {
AddDebugLogLineN(logSHAHashSet,
CFormat("Failed to read RecoveryData for '%s' - Error when trying to read "
"hash into tree") %
m_pOwner->GetFileName());
VerifyHashTree(true); // remove invalid hashes which we have already written
return false;
}
}
// read hashs with 32bit identifier
if (nHashsAvailable == 0 && fileDataIn->GetLength() - fileDataIn->GetPosition() >= 2) {
nHashsAvailable = fileDataIn->ReadUInt16();
if (fileDataIn->GetLength() - fileDataIn->GetPosition() < nHashsToRead * (HASHSIZE + 4u) ||
(nHashsToRead != nHashsAvailable && nHashsAvailable != 0)) {
// this check is redundant, CSafememfile would catch such an error too
// TODO: theApp->QueueDebugLogLine(/*DLP_VERYHIGH,*/ false,
// _T("Failed to read RecoveryData for %s - Received datasize/amounts of hashs was
// invalid (2)"), m_pOwner->GetFileName() );
return false;
}
// TODO: DEBUG_ONLY( theApp->QueueDebugLogLine(/*DLP_VERYHIGH,*/ false, _T("read RecoveryData
// for %s - Received packet with %u 32bit hash identifiers)"), m_pOwner->GetFileName(),
// nHashsAvailable ) );
for (uint32 i = 0; i != nHashsToRead; i++) {
uint32 wHashIdent = fileDataIn->ReadUInt32();
if (wHashIdent == 1 /*never allow masterhash to be overwritten*/
|| wHashIdent > 0x400000 ||
!m_pHashTree.SetHash(fileDataIn, wHashIdent, (-1), false)) {
// TODO: theApp->QueueDebugLogLine(/*DLP_VERYHIGH,*/ false,
// _T("Failed to read RecoveryData for %s - Error when trying to read hash
// into tree (2)"), m_pOwner->GetFileName() );
VerifyHashTree(true); // remove invalid hashes which we have already written
return false;
}
}
}
if (nHashsAvailable == 0) {
// TODO: theApp->QueueDebugLogLine(/*DLP_VERYHIGH,*/ false, _T("Failed to read
// RecoveryData for %s - Packet didn't contained any hashs"), m_pOwner->GetFileName() );
return false;
}
if (VerifyHashTree(true)) {
// some final check if all hashs we wanted are there
for (uint32 nPartPos = 0; nPartPos < nPartSize; nPartPos += EMBLOCKSIZE) {
CAICHHashTree *phtToCheck = m_pHashTree.FindHash(
nPartStartPos + nPartPos, min<uint64>(EMBLOCKSIZE, nPartSize - nPartPos));
if (phtToCheck == NULL || !phtToCheck->m_bHashValid) {
AddDebugLogLineN(logSHAHashSet,
CFormat("Failed to read RecoveryData for '%s' - Error while "
"verifying presence of all lowest level hashes") %
m_pOwner->GetFileName());
return false;
}
}
// all done
return true;
} else {
AddDebugLogLineN(logSHAHashSet,
CFormat("Failed to read RecoveryData for '%s' - Verifying received hashtree failed") %
m_pOwner->GetFileName());
return false;
}
}
// this function is only allowed to be called right after successfully calculating the hashset (!)
void CAICHHashSet::InvalidateRootHashCache()
{
wxMutexLocker lock(s_rootHashCacheMutex);
s_rootHashCache.clear();
s_rootHashCacheLoaded = false;
}
void CAICHHashSet::LoadRootHashCacheLocked()
{
// Walk known2.met once, collecting every root hash. Replaces the
// per-call in-file linear scan that turned bulk-hashing N files into
// O(N^2) on-disk work (issue #579).
s_rootHashCache.clear();
s_rootHashCacheLoaded = true; // marked early so a partial read still ends the loop
const wxString fullpath = thePrefs::GetConfigDir() + KNOWN2_MET_FILENAME;
if (!wxFile::Exists(fullpath)) {
return;
}
CFile file(fullpath, CFile::read);
if (!file.IsOpened()) {
// Couldn't open: don't claim the cache is valid; SaveHashSet
// will retry on the next invocation.
s_rootHashCacheLoaded = false;
return;
}
try {
const uint64 nFileSize = file.GetLength();
if (nFileSize == 0) {
return;
}
const uint8 header = file.ReadUInt8();
if (header != KNOWN2_MET_VERSION) {
AddDebugLogLineC(logSHAHashSet,
"AICH cache load: unexpected known2.met version, leaving cache empty");
return;
}
while (file.GetPosition() < nFileSize) {
// Remember the byte offset of the root-hash position so
// LoadHashSet can seek straight here later instead of
// rewalking the file.
const uint64 entryOffset = file.GetPosition();
CAICHHash rootHash;
rootHash.Read(&file);
const uint32 nHashCount = file.ReadUInt32();
const uint64 skipBytes = static_cast<uint64>(nHashCount) * HASHSIZE;
if (file.GetPosition() + skipBytes > nFileSize) {
// known2.met is truncated past this entry; stop here.
// CAICHSyncTask will handle the actual truncation/recovery.
AddDebugLogLineC(
logSHAHashSet, "AICH cache load: known2.met truncated mid-entry");
break;
}
file.Seek(static_cast<wxFileOffset>(skipBytes), wxFromCurrent);
s_rootHashCache.emplace(rootHash, entryOffset);
}
} catch (const CSafeIOException &e) {
AddDebugLogLineC(logSHAHashSet, "IO error walking known2.met for AICH cache: " + e.what());
// Keep whatever we collected; stay marked loaded so we don't
// re-scan on every SaveHashSet call (and risk the same error).
}
}
bool CAICHHashSet::SaveHashSet()
{
if (m_eStatus != AICH_HASHSETCOMPLETE) {
wxFAIL;
return false;
}
if (!m_pHashTree.m_bHashValid || m_pHashTree.m_nDataSize != m_pOwner->GetFileSize()) {
wxFAIL;
return false;
}
wxMutexLocker cacheLock(s_rootHashCacheMutex);
if (!s_rootHashCacheLoaded) {
LoadRootHashCacheLocked();
}
// O(1) dedup — replaces the linear file walk that used to make this
// O(N) per call and O(N^2) over a bulk-hashing batch.
if (s_rootHashCache.find(m_pHashTree.m_Hash) != s_rootHashCache.end()) {
return true;
}
// Byte offset at which the new entry's root hash will be appended.
// Captured inside the try block and used after it to update the
// cache once the write has succeeded.
uint64 newEntryOffset = 0;
try {
const wxString fullpath = thePrefs::GetConfigDir() + KNOWN2_MET_FILENAME;
const bool exists = wxFile::Exists(fullpath);
CFile file(fullpath, exists ? CFile::read_write : CFile::write);
if (!file.IsOpened()) {
AddDebugLogLineC(logSHAHashSet, "Failed to save HashSet: opening met file failed!");
return false;
}
uint64 nExistingSize = file.GetLength();
if (nExistingSize) {
uint8 header = file.ReadUInt8();
if (header != KNOWN2_MET_VERSION) {
AddDebugLogLineC(
logSHAHashSet, "Saving failed: Current file is not a met-file!");
return false;
}
// Skip the in-file dedup walk; the cache already confirmed
// our root hash isn't present.
file.Seek(static_cast<wxFileOffset>(nExistingSize), wxFromStart);
} else {
file.WriteUInt8(KNOWN2_MET_VERSION);
// Update the recorded size, in order for the sanity check below to work.
nExistingSize += 1;
}
// This is the byte offset at which the new entry's root hash
// will land — capture it for the cache so LoadHashSet can
// seek straight here later.
newEntryOffset = nExistingSize;
// write hashset
m_pHashTree.m_Hash.Write(&file);
uint32 nHashCount = (PARTSIZE / EMBLOCKSIZE + ((PARTSIZE % EMBLOCKSIZE != 0) ? 1 : 0)) *
(m_pHashTree.m_nDataSize / PARTSIZE);
if (m_pHashTree.m_nDataSize % PARTSIZE != 0) {
nHashCount += (m_pHashTree.m_nDataSize % PARTSIZE) / EMBLOCKSIZE +
(((m_pHashTree.m_nDataSize % PARTSIZE) % EMBLOCKSIZE != 0) ? 1 : 0);
}
file.WriteUInt32(nHashCount);
if (!m_pHashTree.WriteLowestLevelHashs(&file, 0, true, true)) {
// that's bad... really
file.SetLength(nExistingSize);
AddDebugLogLineC(
logSHAHashSet, "Failed to save HashSet: WriteLowestLevelHashs() failed!");
return false;
}
if (file.GetLength() != nExistingSize + (nHashCount + 1) * HASHSIZE + 4) {
// that's even worse
file.SetLength(nExistingSize);
AddDebugLogLineC(logSHAHashSet,
"Failed to save HashSet: Calculated and real size of hashset differ!");
return false;
}
AddDebugLogLineN(logSHAHashSet,
CFormat("Successfully saved eMuleAC Hashset, %u Hashs + 1 Masterhash written") %
nHashCount);
} catch (const CSafeIOException &e) {
AddDebugLogLineC(logSHAHashSet, "IO error while saving AICH HashSet: " + e.what());
return false;
}
// Append succeeded — record offset in the cache so the next
// SaveHashSet for this root hash dedups in O(1), and LoadHashSet
// can seek straight to it.
s_rootHashCache.emplace(m_pHashTree.m_Hash, newEntryOffset);
return true;
}
bool CAICHHashSet::LoadHashSet()
{
if (m_eStatus != AICH_HASHSETCOMPLETE) {
wxFAIL;
return false;
}
if (!m_pHashTree.m_bHashValid || m_pHashTree.m_nDataSize != m_pOwner->GetFileSize() ||
m_pHashTree.m_nDataSize == 0) {
wxFAIL;
return false;
}
// O(1) cache lookup: ask the offset index where this root hash
// lives in known2.met. The cache was the dedup-on-write index
// before; here we reuse it to skip the linear scan that used to
// happen on every incoming OP_AICHREQUEST (issue #166). If the
// cache miss-and-cold-load path is taken we still pay the one-shot
// walk, but only once across all subsequent calls.
uint64 cachedOffset = 0;
bool haveCachedOffset = false;
{
wxMutexLocker lock(s_rootHashCacheMutex);
if (!s_rootHashCacheLoaded) {
LoadRootHashCacheLocked();
}
auto it = s_rootHashCache.find(m_pHashTree.m_Hash);
if (it != s_rootHashCache.end()) {
cachedOffset = it->second;
haveCachedOffset = true;
} else if (s_rootHashCacheLoaded) {
// Cache is authoritative and the hash isn't there.
// known2.met genuinely doesn't contain it; skip the I/O.
return false;
}
}
wxString fullpath = thePrefs::GetConfigDir() + KNOWN2_MET_FILENAME;
CFile file(fullpath, CFile::read);
if (!file.IsOpened()) {
if (wxFileExists(fullpath)) {
wxString strError("Failed to load " KNOWN2_MET_FILENAME " file");
AddDebugLogLineC(logSHAHashSet, strError);
}
return false;
}
try {
uint8 header = file.ReadUInt8();
if (header != KNOWN2_MET_VERSION) {
AddDebugLogLineC(logSHAHashSet, "Loading failed: Current file is not a met-file!");
return false;
}
uint64 nExistingSize = file.GetLength();
// Fast path: seek straight to the cached entry. If the offset
// turns out to be stale -- past EOF, or first read at that
// position doesn't match our root hash -- rewind once to just
// past the version header and fall through to a true linear
// scan from the top as defensive recovery against external
// modification of known2.met between cache load and now.
if (haveCachedOffset) {
if (cachedOffset >= nExistingSize) {
haveCachedOffset = false;
} else {
file.Seek(static_cast<wxFileOffset>(cachedOffset), wxFromStart);
}
}
CAICHHash CurrentHash;
uint32 nHashCount;
bool cacheFallbackTriggered = false;
while (file.GetPosition() < nExistingSize) {
// Position of the root-hash at the start of the entry we're
// about to examine — captured pre-read so we can stamp it
// back into the cache when a stale-cache rewind succeeds.
const uint64 entryStartPos = file.GetPosition();
CurrentHash.Read(&file);
if (m_pHashTree.m_Hash == CurrentHash) {
// found Hashset
uint32 nExpectedCount =
(PARTSIZE / EMBLOCKSIZE + ((PARTSIZE % EMBLOCKSIZE != 0) ? 1 : 0)) *
(m_pHashTree.m_nDataSize / PARTSIZE);
if (m_pHashTree.m_nDataSize % PARTSIZE != 0) {
nExpectedCount +=
(m_pHashTree.m_nDataSize % PARTSIZE) / EMBLOCKSIZE +
(((m_pHashTree.m_nDataSize % PARTSIZE) % EMBLOCKSIZE != 0)
? 1
: 0);
}
nHashCount = file.ReadUInt32();
if (nHashCount != nExpectedCount) {
AddDebugLogLineC(logSHAHashSet,
"Failed to load HashSet: Available Hashs and expected "
"hashcount differ!");
return false;
}
if (!m_pHashTree.LoadLowestLevelHashs(&file)) {
AddDebugLogLineC(logSHAHashSet,
"Failed to load HashSet: LoadLowestLevelHashs failed!");
return false;
}
if (!ReCalculateHash(false)) {
AddDebugLogLineC(logSHAHashSet,
"Failed to load HashSet: Calculating loaded hashs failed!");
return false;
}
if (CurrentHash != m_pHashTree.m_Hash) {
AddDebugLogLineC(logSHAHashSet,
"Failed to load HashSet: Calculated Masterhash differs from "
"given Masterhash - hashset corrupt!");
return false;
}
// Self-heal: if we got here via the stale-cache rewind,
// update the cache so future lookups for this root hash
// go straight to the new correct offset instead of
// paying the linear-scan penalty every time.
if (cacheFallbackTriggered) {
wxMutexLocker lock(s_rootHashCacheMutex);
s_rootHashCache[m_pHashTree.m_Hash] = entryStartPos;
}
return true;
}
// First read after seeking to the cached offset didn't match
// our root hash. The cache must be stale -- known2.met was
// modified externally between cache load and now. Rewind once
// to just past the version header and restart as a true linear
// scan from the top.
if (haveCachedOffset && !cacheFallbackTriggered) {
cacheFallbackTriggered = true;
haveCachedOffset = false;
file.Seek(1, wxFromStart);
continue;
}
nHashCount = file.ReadUInt32();
if (file.GetPosition() + nHashCount * HASHSIZE > nExistingSize) {
AddDebugLogLineC(logSHAHashSet,
"Saving failed: File contains fewer entries than specified!");
return false;
}
// skip the rest of this hashset
file.Seek(nHashCount * HASHSIZE, wxFromCurrent);
}
AddDebugLogLineC(logSHAHashSet, "Failed to load HashSet: HashSet not found!");
} catch (const CSafeIOException &e) {
AddDebugLogLineC(logSHAHashSet, "IO error while loading AICH HashSet: " + e.what());
}
return false;
}
// delete the hashset except the masterhash (we dont keep aich hashsets in memory to save resources)
void CAICHHashSet::FreeHashSet()
{
if (m_pHashTree.m_pLeftTree) {
delete m_pHashTree.m_pLeftTree;
m_pHashTree.m_pLeftTree = NULL;
}
if (m_pHashTree.m_pRightTree) {
delete m_pHashTree.m_pRightTree;
m_pHashTree.m_pRightTree = NULL;
}
}
void CAICHHashSet::SetMasterHash(const CAICHHash &Hash, EAICHStatus eNewStatus)
{
m_pHashTree.m_Hash = Hash;
m_pHashTree.m_bHashValid = true;
SetStatus(eNewStatus);
}
CAICHHashAlgo *CAICHHashSet::GetNewHashAlgo()
{
return new CSHA();
}
bool CAICHHashSet::ReCalculateHash(bool bDontReplace)
{
CAICHHashAlgo *hashalg = GetNewHashAlgo();
bool bResult = m_pHashTree.ReCalculateHash(hashalg, bDontReplace);
delete hashalg;
return bResult;
}
bool CAICHHashSet::VerifyHashTree(bool bDeleteBadTrees)
{