forked from iceland2k14/KeyHunt-Cuda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBloom.cpp
353 lines (280 loc) · 7.66 KB
/
Bloom.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
#include "Bloom.h"
#include "xxhash.h"
#include <iostream>
#include <math.h>
#include <string.h>
//#include <unistd.h>
#define MAKESTRING(n) STRING(n)
#define STRING(n) #n
#define BLOOM_MAGIC "libbloom2"
#define BLOOM_VERSION_MAJOR 2
#define BLOOM_VERSION_MINOR 1
Bloom::Bloom(unsigned long long entries, double error) : _ready(0)
{
if (entries < 2 || error <= 0 || error >= 1) {
printf("Bloom init error, minimum 2 entries required\n");
return;
}
_entries = entries;
_error = error;
long double num = -log(_error);
long double denom = 0.480453013918201; // ln(2)^2
_bpe = (num / denom);
long double dentries = (long double)_entries;
long double allbits = dentries * _bpe;
_bits = (unsigned long long int)allbits;
if (_bits % 8) {
_bytes = (unsigned long long int)(_bits / 8) + 1;
} else {
_bytes = (unsigned long long int) _bits / 8;
}
_hashes = (unsigned char)ceil(0.693147180559945 * _bpe); // ln(2)
_bf = (unsigned char *)calloc((unsigned long long int)_bytes, sizeof(unsigned char));
if (_bf == NULL) { // LCOV_EXCL_START
printf("Bloom init error\n");
return;
} // LCOV_EXCL_STOP
_ready = 1;
_major = BLOOM_VERSION_MAJOR;
_minor = BLOOM_VERSION_MINOR;
}
Bloom::~Bloom()
{
if (_ready)
free(_bf);
}
int Bloom::check(const void *buffer, int len)
{
return bloom_check_add(buffer, len, 0);
}
int Bloom::add(const void *buffer, int len)
{
return bloom_check_add(buffer, len, 1);
}
void Bloom::print()
{
printf("Bloom at %p\n", (void *)this);
if (!_ready) {
printf(" *** NOT READY ***\n");
}
printf(" Version : %d.%d\n", _major, _minor);
printf(" Entries : %llu\n", _entries);
printf(" Error : %1.10f\n", _error);
printf(" Bits : %llu\n", _bits);
printf(" Bits/Elem : %f\n", _bpe);
printf(" Bytes : %llu", _bytes);
unsigned int KB = _bytes / 1024;
unsigned int MB = KB / 1024;
//printf(" (%u KB, %u MB)\n", KB, MB);
printf(" (%u MB)\n", MB);
printf(" Hash funcs : %d\n", _hashes);
}
int Bloom::reset()
{
if (!_ready)
return 1;
memset(_bf, 0, _bytes);
return 0;
}
int Bloom::save(const char *filename)
{
// if (filename == NULL || filename[0] == 0) {
// return 1;
// }
// int fd = open(filename, O_WRONLY | O_CREAT, 0644);
// if (fd < 0) {
// return 1;
// }
// ssize_t out = write(fd, BLOOM_MAGIC, strlen(BLOOM_MAGIC));
// if (out != strlen(BLOOM_MAGIC)) {
// goto save_error; // LCOV_EXCL_LINE
// }
// uint16_t size = sizeof(struct bloom);
// out = write(fd, &size, sizeof(uint16_t));
// if (out != sizeof(uint16_t)) {
// goto save_error; // LCOV_EXCL_LINE
// }
// out = write(fd, bloom, sizeof(struct bloom));
// if (out != sizeof(struct bloom)) {
// goto save_error; // LCOV_EXCL_LINE
// }
// out = write(fd, _bf, _bytes);
// if (out != _bytes) {
// goto save_error; // LCOV_EXCL_LINE
// }
// close(fd);
// return 0;
// // LCOV_EXCL_START
//save_error:
// close(fd);
// return 1;
// // LCOV_EXCL_STOP
return 0;
}
int Bloom::load(const char *filename)
{
// int rv = 0;
// if (filename == NULL || filename[0] == 0) {
// return 1;
// }
// if (bloom == NULL) {
// return 2;
// }
// memset(bloom, 0, sizeof(struct bloom));
// int fd = open(filename, O_RDONLY);
// if (fd < 0) {
// return 3;
// }
// char line[30];
// memset(line, 0, 30);
// ssize_t in = read(fd, line, strlen(BLOOM_MAGIC));
// if (in != strlen(BLOOM_MAGIC)) {
// rv = 4;
// goto load_error;
// }
// if (strncmp(line, BLOOM_MAGIC, strlen(BLOOM_MAGIC))) {
// rv = 5;
// goto load_error;
// }
// uint16_t size;
// in = read(fd, &size, sizeof(uint16_t));
// if (in != sizeof(uint16_t)) {
// rv = 6;
// goto load_error;
// }
// if (size != sizeof(struct bloom)) {
// rv = 7;
// goto load_error;
// }
// in = read(fd, bloom, sizeof(struct bloom));
// if (in != sizeof(struct bloom)) {
// rv = 8;
// goto load_error;
// }
// _bf = NULL;
// if (_major != BLOOM_VERSION_MAJOR) {
// rv = 9;
// goto load_error;
// }
// _bf = (unsigned char *)malloc(_bytes);
// if (_bf == NULL) {
// rv = 10; // LCOV_EXCL_LINE
// goto load_error;
// }
// in = read(fd, _bf, _bytes);
// if (in != _bytes) {
// rv = 11;
// free(_bf);
// _bf = NULL;
// goto load_error;
// }
// close(fd);
// return rv;
//load_error:
// close(fd);
// _ready = 0;
// return rv;
return 0;
}
unsigned char Bloom::get_hashes()
{
return _hashes;
}
unsigned long long int Bloom::get_bits()
{
return _bits;
}
unsigned long long int Bloom::get_bytes()
{
return _bytes;
}
const unsigned char *Bloom::get_bf()
{
return _bf;
}
int Bloom::test_bit_set_bit(unsigned char *buf, unsigned long long bit, int set_bit)
{
unsigned long long byte = bit >> 3;
unsigned char c = buf[byte]; // expensive memory access
unsigned char mask = 1 << (bit % 8);
if (c & mask) {
return 1;
} else {
if (set_bit) {
buf[byte] = c | mask;
}
return 0;
}
}
int Bloom::bloom_check_add(const void *buffer, int len, int add)
{
if (_ready == 0) {
printf("bloom not initialized!\n");
return -1;
}
unsigned char hits = 0;
// unsigned int a = murmurhash2(buffer, len, 0x9747b28c);
// unsigned int b = murmurhash2(buffer, len, a);
unsigned long long a = XXH64(buffer, len, 0x59f2815b16f81798);
unsigned long long b = XXH64(buffer, len, a);
unsigned long long x;
unsigned char i;
for (i = 0; i < _hashes; i++) {
x = (a + b * i) % _bits;
if (test_bit_set_bit(_bf, x, add)) {
hits++;
} else if (!add) {
// Don't care about the presence of all the bits. Just our own.
return 0;
}
}
if (hits == _hashes) {
return 1; // 1 == element already in (or collision)
}
return 0;
}
// MurmurHash2, by Austin Appleby
// Note - This code makes a few assumptions about how your machine behaves -
// 1. We can read a 4-byte value from any address without crashing
// 2. sizeof(int) == 4
// And it has a few limitations -
// 1. It will not work incrementally.
// 2. It will not produce the same results on little-endian and big-endian
// machines.
unsigned int Bloom::murmurhash2(const void *key, int len, const unsigned int seed)
{
// 'm' and 'r' are mixing constants generated offline.
// They're not really 'magic', they just happen to work well.
const unsigned int m = 0x5bd1e995;
const int r = 24;
// Initialize the hash to a 'random' value
unsigned int h = seed ^ len;
// Mix 4 bytes at a time into the hash
const unsigned char *data = (const unsigned char *)key;
while (len >= 4) {
unsigned int k = *(unsigned int *)data;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
len -= 4;
}
// Handle the last few bytes of the input array
switch (len) {
case 3: h ^= data[2] << 16;
break;
case 2: h ^= data[1] << 8;
break;
case 1: h ^= data[0];
h *= m;
break;
};
// Do a few final mixes of the hash to ensure the last few
// bytes are well-incorporated.
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}