-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsdbf_class.cc
513 lines (475 loc) · 18.5 KB
/
sdbf_class.cc
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
// sdbf_class.cc
// Authors: Candice Quates, Vassil Roussev
// implementation of sdbf object
#include "sdbf_class.h"
#include "sdbf_defines.h"
#define SDBF_VERSION 3
#include <stdint.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
/**
\internal
Initialize static configuration object with sensible defaults.
*/
sdbf_conf *sdbf::config = new sdbf_conf(1, FLAG_OFF, _MAX_ELEM_COUNT, _MAX_ELEM_COUNT_DD);
/**
Create new sdbf from file. dd_block_size turns on "block" mode.
\param filename file to hash
\param dd_block_size size of block to process file with. 0 is off.
*/
sdbf::sdbf(const char *filename, uint32_t dd_block_size) {
processed_file_t *mfile = process_file( filename, MIN_FILE_SIZE, config->warnings);
if( !mfile)
throw -1 ; // cannot process file
sdbf_create(filename);
this->info=NULL;
this->orig_file_size=mfile->size;
if (!dd_block_size) { // stream mode
this->max_elem = config->max_elem;
gen_chunk_sdbf( mfile->buffer,mfile->size, 32*MB);
} else { // block mode
this->max_elem = config->max_elem_dd;
uint64_t dd_block_cnt = mfile->size/dd_block_size;
if( mfile->size % dd_block_size >= MIN_FILE_SIZE)
dd_block_cnt++;
this->bf_count = dd_block_cnt;
this->dd_block_size = dd_block_size;
this->buffer = (uint8_t *)alloc_check( ALLOC_ZERO, dd_block_cnt*config->bf_size, "sdbf_hash_dd", "this->buffer", ERROR_EXIT);
this->elem_counts = (uint16_t *)alloc_check( ALLOC_ZERO, sizeof( uint16_t)*dd_block_cnt, "sdbf_hash_dd", "this->elem_counts", ERROR_EXIT);
gen_block_sdbf_mt( mfile->buffer, mfile->size, dd_block_size, config->thread_cnt);
}
free( mfile->buffer);
compute_hamming();
free(mfile);
}
/**
Generates a new sdbf, with a maximum size read from an open stream.
dd_block_size enables block mode.
\param name name of stream
\param ifs open istream to read raw data from
\param dd_block_size size of block to divide data with. 0 is off.
\param msize amount of data to read and process
\param info block of information about indexes
*/
sdbf::sdbf(const char *name, std::istream *ifs, uint32_t dd_block_size, uint64_t msize,index_info *info) {
uint64_t chunk_size;
uint8_t *bufferinput;
bufferinput = (uint8_t*)alloc_check(ALLOC_ZERO, sizeof(uint8_t)*msize,"sdbf_hash_stream",
"buffer input", ERROR_EXIT);
ifs->read((char*)bufferinput,msize);
chunk_size = ifs->gcount();
if (chunk_size < MIN_FILE_SIZE) {
free(bufferinput);
throw -3; // too small
}
sdbf_create(name); // change to filename+offset / size
this->info=info;
this->orig_file_size=chunk_size;
if (!dd_block_size) { // single stream mode should not be used but we'll support it anyway
this->max_elem = config->max_elem;
gen_chunk_sdbf(bufferinput,msize, 32*MB);
} else { // block mode
this->max_elem = config->max_elem_dd;
uint64_t dd_block_cnt = msize/dd_block_size;
if( msize % dd_block_size >= MIN_FILE_SIZE)
dd_block_cnt++;
this->bf_count = dd_block_cnt;
this->dd_block_size = dd_block_size;
this->buffer = (uint8_t *)alloc_check( ALLOC_ZERO, dd_block_cnt*config->bf_size, "sdbf_hash_dd", "this->buffer", ERROR_EXIT);
this->elem_counts = (uint16_t *)alloc_check( ALLOC_ZERO, sizeof( uint16_t)*dd_block_cnt, "sdbf_hash_dd", "this->elem_counts", ERROR_EXIT);
gen_block_sdbf_mt( bufferinput, msize, dd_block_size, config->thread_cnt);
}
compute_hamming();
free(bufferinput);
}
/**
Generates a new sdbf, from a char *string
dd_block_size enables block mode.
\param name name of stream
\param str input to be hashed
\param dd_block_size size of block to divide data with. 0 is off.
\param length length of str to be hashed
\param info block of information about indexes
*/
sdbf::sdbf(const char *name, char *str, uint32_t dd_block_size, uint64_t length,index_info *info) {
if (length < MIN_FILE_SIZE)
throw -3; // too small
sdbf_create(name);
this->info=info;
this->orig_file_size=length;
if (!dd_block_size) { // single stream mode should not be used but we'll support it anyway
this->max_elem = config->max_elem;
gen_chunk_sdbf((uint8_t*)str,length, 32*MB);
} else { // block mode
this->max_elem = config->max_elem_dd;
uint64_t dd_block_cnt = length/dd_block_size;
if( length % dd_block_size >= MIN_FILE_SIZE)
dd_block_cnt++;
this->bf_count = dd_block_cnt;
this->dd_block_size = dd_block_size;
this->buffer = (uint8_t *)alloc_check( ALLOC_ZERO, dd_block_cnt*config->bf_size, "sdbf_hash_dd", "this->buffer", ERROR_EXIT);
this->elem_counts = (uint16_t *)alloc_check( ALLOC_ZERO, sizeof( uint16_t)*dd_block_cnt, "sdbf_hash_dd", "this->elem_counts", ERROR_EXIT);
gen_block_sdbf_mt( (uint8_t*)str, length, dd_block_size, config->thread_cnt);
}
compute_hamming();
}
/// Andrei - this is a dirty HACK over "sdhash-3.3"
/**
Reads an already generated sdbf from open file.
Throws exceptions in case of bad formatting.
\param in FILE* open formatted as list of sdbfs
*/
sdbf::sdbf(char *str) {
char *b64, fmt[64];
uint8_t buffer[16*KB];
char sdbf_magic[16], hash_magic[8];
uint32_t colon_cnt, read_cnt, hash_cnt, b64_len;
int d_len;
uint32_t version, name_len;
uint64_t i;
// http://stackoverflow.com/a/13503155 - sscanf(expression, "%lf%n", &value, &pos);
char fmt_n[66];
int pos;
char *in = str;
if( !(*in) )
throw -1; //end of file - quit
sdbf_create(NULL);
for( i=0, colon_cnt=3; i<MAX_MAGIC_HEADER && (*in); i++) {
buffer[i] = *in;
in++;
if (i==4 && strncmp((char*)buffer,"sdbf",4) )
throw -2;
if( buffer[i] == DELIM_CHAR) {
buffer[i] = 0x20;
colon_cnt--;
if( !colon_cnt)
break;
}
}
if( !(*in) )
throw -3 ; // end of file prematurely
buffer[i] = 0;
sscanf( (char*)buffer, "%s %d %d", sdbf_magic, &version, &name_len);
if( (strcmp( sdbf_magic, MAGIC_STREAM) && strcmp( sdbf_magic, MAGIC_DD)) || version != 3) {
if (config->warnings)
fprintf( stderr, "ERROR: Unsupported format '%s:%02d'. Expecting '%s:03' or '%s:03'\n", sdbf_magic, version, MAGIC_STREAM, MAGIC_DD);
throw -2 ; // unsupported format - caller should exit
}
fmt_n[0] = '%';
fmt[0] = '%';
sprintf( fmt_n+1, "%dc%%n", name_len);
sprintf( fmt+1, "%dc", name_len);
this->filenamealloc=true;
this->hashname = (char*)alloc_check( ALLOC_ZERO, name_len+2, "sdbf_from_stream", "this->hashname", ERROR_EXIT);
read_cnt = sscanf( in, fmt_n, this->hashname, &pos);
read_cnt = sscanf( in, fmt, this->hashname);
in += pos;
read_cnt = sscanf( in, ":%ld:%4s:%d:%d:%x:%d:%d%n", &(this->orig_file_size), hash_magic, &(this->bf_size), &(this->hash_count), &(this->mask), &(this->max_elem), &(this->bf_count), &pos);
read_cnt = sscanf( in, ":%ld:%4s:%d:%d:%x:%d:%d", &(this->orig_file_size), hash_magic, &(this->bf_size), &(this->hash_count), &(this->mask), &(this->max_elem), &(this->bf_count));
in += pos;
this->buffer = (uint8_t *)alloc_check( ALLOC_ZERO, this->bf_count*this->bf_size, "sdbf_from_stream", "this->buffer", ERROR_EXIT);
// DD fork
if( !strcmp( sdbf_magic, MAGIC_DD)) {
read_cnt = sscanf( in, ":%d%n", &(this->dd_block_size), &pos);
read_cnt = sscanf( in, ":%d", &(this->dd_block_size));
in += pos;
this->elem_counts = (uint16_t *)alloc_check( ALLOC_ZERO, this->bf_count*sizeof(uint16_t), "sdbf_from_stream", "this->elem_counts", ERROR_EXIT);
for( i=0; i<this->bf_count; i++) {
read_cnt = sscanf( in, ":%2x:%344s%n", &hash_cnt, buffer, &pos);
read_cnt = sscanf( in, ":%2x:%344s", &hash_cnt, buffer);
in += pos;
this->elem_counts[i] = (uint16_t)hash_cnt;
d_len = b64decode_into( buffer, 344, this->buffer + i*this->bf_size);
if( d_len != 256) {
if (config->warnings)
fprintf( stderr, "ERROR: Unexpected decoded length for BF: %d. name: %s, BF#: %d\n", d_len, this->hashname, (int)i);
throw -2; // unsupported format - caller should exit
}
}
// Stream fork
} else {
read_cnt = sscanf( in, ":%d:%n", &(this->last_count), &pos);
read_cnt = sscanf( in, ":%d:", &(this->last_count));
in += pos;
b64_len = this->bf_count*this->bf_size;
b64_len = 4*(b64_len/3 +1*(b64_len % 3 > 0 ? 1 : 0));
sprintf( &fmt_n[1], "%ds%%n", b64_len);
sprintf( &fmt[1], "%ds", b64_len);
b64 = (char*)alloc_check( ALLOC_ZERO, b64_len+2, "sdbf_from_stream", "b64", ERROR_EXIT);
read_cnt = sscanf( in, fmt_n, b64, &pos);
read_cnt = sscanf( in, fmt, b64);
in += pos;
free(this->buffer);
this->buffer =(uint8_t*) b64decode( (char*)b64, (int)b64_len, &d_len);
if( (uint32_t)d_len != this->bf_count*this->bf_size) {
if (config->warnings)
fprintf( stderr, "ERROR: Incorrect base64 decoding length. Expected: %d, actual: %d\n", this->bf_count*this->bf_size, d_len);
free (b64); // cleanup in case of wanting to go on
throw -2; // unsupported format, caller should exit
}
free( b64);
}
compute_hamming();
if (read_cnt) read_cnt++; // making compiler warnings shut up.
this->info=NULL;
}
/**
Reads an already generated sdbf from open file.
Throws exceptions in case of bad formatting.
\param in FILE* open formatted as list of sdbfs
*/
sdbf::sdbf(FILE *in) {
char *b64, fmt[64];
uint8_t buffer[16*KB];
char sdbf_magic[16], hash_magic[8];
uint32_t colon_cnt, read_cnt, hash_cnt, b64_len;
int d_len;
uint32_t version, name_len;
uint64_t i;
if( feof( in))
throw -1; //end of file - quit
sdbf_create(NULL);
for( i=0, colon_cnt=3; i<MAX_MAGIC_HEADER && !feof(in); i++) {
buffer[i] = fgetc( in);
if (i==4 && strncmp((char*)buffer,"sdbf",4) )
throw -2;
if( buffer[i] == DELIM_CHAR) {
buffer[i] = 0x20;
colon_cnt--;
if( !colon_cnt)
break;
}
}
if( feof( in))
throw -3 ; // end of file prematurely
buffer[i] = 0;
sscanf( (char*)buffer, "%s %d %d", sdbf_magic, &version, &name_len);
if( (strcmp( sdbf_magic, MAGIC_STREAM) && strcmp( sdbf_magic, MAGIC_DD)) || version != 3) {
if (config->warnings)
fprintf( stderr, "ERROR: Unsupported format '%s:%02d'. Expecting '%s:03' or '%s:03'\n", sdbf_magic, version, MAGIC_STREAM, MAGIC_DD);
throw -2 ; // unsupported format - caller should exit
}
fmt[0] = '%';
sprintf( fmt+1, "%dc", name_len);
this->filenamealloc=true;
this->hashname = (char*)alloc_check( ALLOC_ZERO, name_len+2, "sdbf_from_stream", "this->hashname", ERROR_EXIT);
read_cnt = fscanf( in, fmt, this->hashname);
read_cnt = fscanf( in, ":%ld:%4s:%d:%d:%x:%d:%d", &(this->orig_file_size), hash_magic, &(this->bf_size), &(this->hash_count), &(this->mask), &(this->max_elem), &(this->bf_count));
this->buffer = (uint8_t *)alloc_check( ALLOC_ZERO, this->bf_count*this->bf_size, "sdbf_from_stream", "this->buffer", ERROR_EXIT);
// DD fork
if( !strcmp( sdbf_magic, MAGIC_DD)) {
read_cnt = fscanf( in, ":%d", &(this->dd_block_size));
this->elem_counts = (uint16_t *)alloc_check( ALLOC_ZERO, this->bf_count*sizeof(uint16_t), "sdbf_from_stream", "this->elem_counts", ERROR_EXIT);
for( i=0; i<this->bf_count; i++) {
read_cnt = fscanf( in, ":%2x:%344s", &hash_cnt, buffer);
this->elem_counts[i] = (uint16_t)hash_cnt;
d_len = b64decode_into( buffer, 344, this->buffer + i*this->bf_size);
if( d_len != 256) {
if (config->warnings)
fprintf( stderr, "ERROR: Unexpected decoded length for BF: %d. name: %s, BF#: %d\n", d_len, this->hashname, (int)i);
throw -2; // unsupported format - caller should exit
}
}
// Stream fork
} else {
read_cnt = fscanf( in, ":%d:", &(this->last_count));
b64_len = this->bf_count*this->bf_size;
b64_len = 4*(b64_len/3 +1*(b64_len % 3 > 0 ? 1 : 0));
sprintf( &fmt[1], "%ds", b64_len);
b64 = (char*)alloc_check( ALLOC_ZERO, b64_len+2, "sdbf_from_stream", "b64", ERROR_EXIT);
read_cnt = fscanf( in, fmt, b64);
free(this->buffer);
this->buffer =(uint8_t*) b64decode( (char*)b64, (int)b64_len, &d_len);
if( (uint32_t)d_len != this->bf_count*this->bf_size) {
if (config->warnings)
fprintf( stderr, "ERROR: Incorrect base64 decoding length. Expected: %d, actual: %d\n", this->bf_count*this->bf_size, d_len);
free (b64); // cleanup in case of wanting to go on
throw -2; // unsupported format, caller should exit
}
free( b64);
}
compute_hamming();
if (read_cnt) read_cnt++; // making compiler warnings shut up.
this->info=NULL;
}
/**
Destroys this sdbf
*/
sdbf::~sdbf() {
if (buffer)
free(buffer);
if (hamming)
free(hamming);
if (elem_counts)
free(elem_counts);
if (filenamealloc)
free(hashname);
}
/**
Returns the name of the file or data this sdbf represents.
\returns char* of file name
*/
const char *
sdbf::name() {
return (char*)this->hashname;
}
/**
Returns the size of the hash data for this sdbf
\returns uint64_t length value
*/
uint64_t
sdbf::size() {
return (this->bf_size)*(this->bf_count);
}
/**
Returns the size of the data that the hash was generated from.
\returns uint64_t length value
*/
uint64_t
sdbf::input_size() {
return this->orig_file_size;
}
/**
* Compares this sdbf to other passed sdbf, returns a confidence score
\param other sdbf* to compare to self
\param sample sets the number of BFs to sample - 0 uses all
\returns int32_t confidence score
*/
int32_t
sdbf::compare( sdbf *other, uint32_t sample) {
if (config->warnings)
cerr << this->name() << " vs " << other->name() << endl;
return sdbf_score( this, other, sample);
}
/**
Write this sdbf to stream
*/
ostream& operator<<(ostream& os, const sdbf& s) {
os << s.to_string();
return os;
}
/**
Write sdbf to stream
*/
ostream& operator<<(ostream& os, const sdbf *s) {
os << s->to_string();
return os;
}
/**
Encode this sdbf and return it as a string.
\returns std::string containing sdbf suitable for display or writing to file
*/
string
sdbf::to_string () const { // write self to stream
std::stringstream hash;
// Stream version
if( !this->elem_counts) {
hash.fill('0');
hash << MAGIC_STREAM << ":" << setw (2) << SDBF_VERSION << ":";
hash << (int)strlen((char*)this->hashname) << ":" << this->hashname << ":" << this->orig_file_size << ":sha1:";
hash << this->bf_size << ":" << this->hash_count<< ":" << hex << this->mask << ":" << dec;
hash << this->max_elem << ":" << this->bf_count << ":" << this->last_count << ":";
uint64_t qt = this->bf_count/6, rem = this->bf_count % 6;
uint64_t i, pos=0, b64_block = 6*this->bf_size;
for( i=0,pos=0; i<qt; i++,pos+=b64_block) {
char *b64 = b64encode( (char*)this->buffer + pos, b64_block);
hash << b64;
free( b64);
}
if( rem>0) {
char *b64 = b64encode( (char*)this->buffer + pos, rem*this->bf_size);
hash << b64;
free( b64);
}
} else { // block version
hash.fill('0');
hash << MAGIC_DD << ":" << setw (2) << SDBF_VERSION << ":";
hash << (int)strlen((char*)this->hashname) << ":" << this->hashname << ":" << this->orig_file_size << ":sha1:";
hash << this->bf_size << ":" << this->hash_count<< ":" << hex << this->mask << ":" << dec;
hash << this->max_elem << ":" << this->bf_count << ":" << this->dd_block_size ;
uint32_t i;
for( i=0; i<this->bf_count; i++) {
char *b64 = b64encode( (char*)this->buffer+i*this->bf_size, this->bf_size);
hash << ":" << setw (2) << hex << this->elem_counts[i];
hash << ":" << b64;
free(b64);
}
}
hash << endl;
return hash.str();
}
string
sdbf::get_index_results() const{
return index_results;
}
/**
Clones a copy of a single bloom filter in
this sdbf.
Warning: 256-bytes long, not terminated, may contain nulls.
\param position index of bloom filter
\returns uint8_t* pointer to 256-byte long bloom filter
*/
uint8_t*
sdbf::clone_filter(uint32_t position) {
if (position < this->bf_count) {
uint8_t *filter=(uint8_t*)alloc_check(ALLOC_ZERO,bf_size*sizeof(uint8_t),"single_bloom_filter","return buffer",ERROR_EXIT);
memcpy(filter,this->buffer + position*bf_size,bf_size);
return filter;
} else {
return NULL;
}
}
/** \internal
* Create and initialize an sdbf structure ready for stream mode.
*/
void
sdbf::sdbf_create(const char *name) {
this->hashname = (char*)name;
this->bf_size = config->bf_size;
this->hash_count = 5;
this->mask = config->BF_CLASS_MASKS[0];
this->bf_count = 1;
this->last_count = 0;
this->elem_counts= 0;
this->orig_file_size = 0;
this->hamming = NULL;
this->buffer = NULL;
this->info=NULL;
this->filenamealloc=false;
}
/** \internal
* Pre-compute Hamming weights for each BF and adds them to the SDBF descriptor.
*/
int
sdbf::compute_hamming() {
uint32_t pos, bf_count = this->bf_count;
this->hamming = (uint16_t *) alloc_check( ALLOC_ZERO, bf_count*sizeof( uint16_t), "compute_hamming", "this->hamming", ERROR_EXIT);
uint64_t i, j;
uint16_t *buffer16 = (uint16_t *)this->buffer;
for( i=0,pos=0; i<bf_count; i++) {
for( j=0; j<BF_SIZE/2; j++,pos++) {
this->hamming[i] += config->bit_count_16[buffer16[pos]];
}
}
return 0;
}
/** \internal
get element count for comparisons
*/
int32_t
sdbf::get_elem_count( sdbf *mine,uint64_t index) {
if( !mine->elem_counts) {
return (index < mine->bf_count-1) ? mine->max_elem : mine->last_count;
// DD fork
} else {
return mine->elem_counts[index];
}
}
uint32_t
sdbf::filter_count() {
return bf_count;
}