-
Notifications
You must be signed in to change notification settings - Fork 30
/
HashTable.cpp
366 lines (288 loc) · 7.89 KB
/
HashTable.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
/*
* This file is part of the BTCCollider distribution (https://github.com/JeanLucPons/BTCCollider).
* Copyright (c) 2020 Jean Luc PONS.
*
* 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, version 3.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "HashTable.h"
#include <stdio.h>
#include "Int.h"
#ifndef WIN64
#include <string.h>
#endif
#define safe_free(x) if(x) {free(x);x=NULL;}
#define GET(hash,id) E[hash].items[id]
HashTable::HashTable() {
totalItem = 0;
memset(E,0,sizeof(E));
}
void HashTable::Reset() {
for (uint32_t h = 0; h < HASH_SIZE; h++) {
for(uint32_t i=0;i<E[h].nbItem;i++)
free(E[h].items[i]);
safe_free(E[h].items);
E[h].maxItem = 0;
E[h].nbItem = 0;
}
totalItem = 0;
}
uint16_t HashTable::getHash(hash160_t *h) {
uint16_t s = 0;
int i;
for(i=0;i<nbFull;i++)
s+=h->i16[i];
if(colMask)
s+=h->i16[i] & colMask;
return s % HASH_SIZE;
}
void HashTable::SetParam(int n,int nbFull,uint16_t colMask) {
this->cSize = n;
this->nbFull = nbFull;
this->colMask = colMask;
}
int HashTable::GetNbItem() {
return totalItem;
}
ENTRY *HashTable::CreateEntry(hash160_t *h1, hash160_t *h2) {
ENTRY *e = (ENTRY *)malloc(sizeof(ENTRY));
e->start = *h1;
e->end = *h2;
return e;
}
void HashTable::getCollision(hash160_t *a, hash160_t *b, hash160_t *e) {
*a = this->a;
*b = this->b;
if(e) *e = this->e;
}
#define ADD_ENTRY(entry) { \
/* Shift the end of the index table */ \
for (int i = E[h].nbItem; i > st; i--) \
E[h].items[i] = E[h].items[i - 1]; \
E[h].items[st] = entry; \
E[h].nbItem++; \
totalItem++;}
int HashTable::AddHash(hash160_t *start, hash160_t *end) {
uint16_t h = getHash(end);
if (E[h].maxItem == 0) {
E[h].maxItem = 16;
E[h].items = (ENTRY **)malloc(sizeof(ENTRY *) * E[h].maxItem);
}
if(E[h].nbItem==0) {
E[h].items[0] = CreateEntry(start, end);
E[h].nbItem = 1;
totalItem++;
return false;
}
if (E[h].nbItem >= E[h].maxItem - 1) {
// We need to reallocate
E[h].maxItem += 16;
ENTRY **nitems = (ENTRY **)malloc(sizeof(ENTRY *) * E[h].maxItem);
memcpy(nitems, E[h].items, sizeof(ENTRY *) * E[h].nbItem);
free(E[h].items);
E[h].items = nitems;
}
// Search insertion position
int st, ed, mi;
st=0; ed = E[h].nbItem-1;
while(st<=ed) {
mi = (st+ed)/2;
int comp = compareHash(end,&GET(h,mi)->end);
if(comp<0) {
ed = mi - 1;
} else {
st = mi + 1;
}
}
// Collision check
if (st > 0) {
if (hashCollide(&GET(h, st - 1)->end, end)) {
if (hashCollide(&GET(h, st - 1)->start, start)) {
printf("\nFalse collision\n");
return FALSE_COLLISION;
}
ENTRY *entry = CreateEntry(start,end);
ADD_ENTRY(entry);
a = GET(h, st-1)->start;
b = GET(h, st)->start;
e = GET(h, st)->end;
return COLLISION;
}
}
if (st < (int)E[h].nbItem ) {
if (hashCollide(&GET(h, st)->end, end)) {
if (hashCollide(&GET(h, st)->start, start)) {
printf("\nFalse collision\n");
return FALSE_COLLISION;
}
ENTRY *entry = CreateEntry(start, end);
ADD_ENTRY(entry);
a = GET(h, st)->start;
b = GET(h, st+1)->start;
e = GET(h, st)->end;
return COLLISION;
}
}
ENTRY *entry = CreateEntry(start, end);
ADD_ENTRY(entry);
return NO_COLLISION;
}
double HashTable::GetSizeMB() {
uint64_t byte = 0;
for (int h = 0; h < HASH_SIZE; h++) {
if (E[h].nbItem > 0) {
byte += sizeof(ENTRY *) * E[h].maxItem;
byte += sizeof(ENTRY) * E[h].nbItem;
}
}
return (double)byte / (1024.0*1024.0);
}
void HashTable::SaveTable(FILE *f) {
for (uint32_t h = 0; h < HASH_SIZE; h++) {
fwrite(&E[h].nbItem, sizeof(uint32_t), 1, f);
fwrite(&E[h].maxItem, sizeof(uint32_t), 1, f);
for (uint32_t i = 0; i < E[h].nbItem; i++) {
fwrite(&E[h].items[i]->start, 20, 1, f);
fwrite(&E[h].items[i]->end, 20, 1, f);
}
}
}
void HashTable::LoadTable(FILE *f) {
Reset();
for (uint32_t h = 0; h < HASH_SIZE; h++) {
fread(&E[h].nbItem, sizeof(uint32_t), 1, f);
fread(&E[h].maxItem, sizeof(uint32_t), 1, f);
if (E[h].maxItem > 0) {
// Allocate indexes
E[h].items = (ENTRY **)malloc(sizeof(ENTRY *) * E[h].maxItem);
}
for (uint32_t i = 0; i < E[h].nbItem; i++) {
ENTRY *e = (ENTRY *)malloc(sizeof(ENTRY));
fread(&e->start, 20, 1, f);
fread(&e->end, 20, 1, f);
E[h].items[i] = e;
}
totalItem += E[h].nbItem;
}
printf("HashTable::LoadTable(): %d items loaded\n",totalItem);
}
std::string HashTable::GetHashStr(hash160_t *h1) {
char tmp2[3];
char tmp[256];
tmp[0] = 0;
for (int i = 0; i < 20; i++) {
#ifdef WIN64
sprintf_s(tmp2,3,"%02X",h1->i8[i]);
#else
sprintf(tmp2,"%02X",h1->i8[i]);
#endif
strcat(tmp,tmp2);
}
return std::string(tmp);
}
void HashTable::PrintTable(int limit) {
int curItem=0;
if(limit<=0) limit = totalItem;
for (int h = 0; h < HASH_SIZE; h++) {
if (E[h].nbItem > 0) {
printf("ENTRY: %04X\n",h);
for (uint32_t i = 0; i < E[h].nbItem && curItem<limit; i++) {
//printf("%02d: S:%s\n", i, GetHashStr(&GET(h,i).start).c_str());
//printf(" E:%s\n", GetHashStr(&GET(h, i).end).c_str());
printf("%02d: E:%s\n", i, GetHashStr(&GET(h,i)->start).c_str());
}
}
}
}
bool HashTable::compare(HashTable *ht) {
if (ht->GetNbItem() != GetNbItem()) {
printf("Item number not equal !\n");
return false;
}
for (int h = 0; h < HASH_SIZE; h++) {
if (ht->E[h].nbItem != E[h].nbItem) {
printf("[H%04X] Item number not equal !\n",h);
return false;
}
bool equal = true;
uint32_t i = 0;
while (equal && i < E[h].nbItem) {
equal = (compareHash(&GET(h,i)->start , &ht->GET(h,i)->start)==0) &&
(compareHash(&GET(h, i)->end, &ht->GET(h, i)->end) == 0);
if(equal) i++;
}
if (!equal) {
printf("Unexpected hash found at [%04X][%d] !\n", h,i);
printf("H1 H %s\n", GetHashStr(&GET(h, i)->start).c_str());
printf("H1 E %s\n", GetHashStr(&GET(h, i)->end).c_str());
printf("H2 H %s\n", GetHashStr(&ht->GET(h, i)->start).c_str());
printf("H2 E %s\n", GetHashStr(&ht->GET(h, i)->end).c_str());
return false;
}
}
return true;
}
bool HashTable::hashCollide(hash160_t *h1, hash160_t *h2) {
int i=0;
for(i=0;i<nbFull;i++)
if(h1->i16[i]!=h2->i16[i])
return false;
if( colMask )
if( (h1->i16[i] & colMask) != (h2->i16[i] & colMask) )
return false;
return true;
}
int HashTable::getCollisionSize(hash160_t *h1, hash160_t *h2) {
int i;
int size = 0;
for (i = 0; i < 20; i++) {
if (h1->i8[i] != h2->i8[i])
break;
size += 8;
}
if (i < 20) {
uint8_t m = 0x80;
uint8_t b1=0;
uint8_t b2=0;
while (b1 == b2) {
b1 = h1->i8[i] & m;
b2 = h2->i8[i] & m;
if (b1 == b2) {
size++;
m = m >> 1;
}
}
}
return size;
}
int HashTable::compareHash(hash160_t *h1, hash160_t *h2) {
uint32_t *a = h1->i32;
uint32_t *b = h2->i32;
int i;
for(i=0;i<5;i++) {
if( a[i]!=b[i] )
break;
}
if(i<5) {
#ifdef WIN64
uint32_t ai = _byteswap_ulong(a[i]);
uint32_t bi = _byteswap_ulong(b[i]);
#else
uint32_t ai = __builtin_bswap32(a[i]);
uint32_t bi = __builtin_bswap32(b[i]);
#endif
if( ai>bi ) return 1;
else return -1;
} else {
return 0;
}
}