int mode_filter(int argc, char** argv) { uint argo = 0; uint negative = FALSE; if (strcmp(argv[2 + argo], "-n") == 0) { negative = TRUE; argo++; } uint update = FALSE; if (strcmp(argv[2 + argo], "-u") == 0) { update = TRUE; argo++; } char* bloom_file = argv[2 + argo]; bf_t* filter = bf_load(bloom_file, BLOOM_HASH_FUNC); if (!filter) { printf("File %s has wrong format or doesn't exist.\n", bloom_file); return 2; } char input_buffer[BF_KEY_BUFFER_SIZE] = { 0 }; uint actual_length = 0; while (gets_skip(input_buffer, BF_KEY_BUFFER_SIZE, &actual_length)) { uint has = bf_has(filter, (const char*)input_buffer, actual_length); if (negative) { if (!has) { printf("%s\n", input_buffer); fflush(stdout); if (update) { bf_add(filter, input_buffer, actual_length); } } } else { if (has) { printf("%s\n", input_buffer); fflush(stdout); } else { if (update) { bf_add(filter, input_buffer, actual_length); } } } } if (update) { bf_save(filter, bloom_file); } bf_destroy(filter); return 0; }
int mode_merge(int argc, char** argv) { if (argc < 4) { show_help(argv[0]); return 1; } char* bloom_file = argv[3]; bf_t* filter = bf_load(bloom_file, BLOOM_HASH_FUNC); if (!filter) { printf("File %s has wrong format or doesn't exist.\n", bloom_file); return 2; } bf_t* merge_with; uint file_index; for (file_index = 4; file_index < argc; file_index++) { merge_with = bf_load(argv[file_index], BLOOM_HASH_FUNC); if (!merge_with) { printf("File %s has wrong format or doesn't exist.\n", argv[file_index]); return 2; } bf_merge(filter, merge_with); bf_destroy(merge_with); } char* merged_bloom_file = argv[2]; bf_save(filter, merged_bloom_file); return 0; }
int mode_info(int argc, char** argv) { char* bloom_file = argv[2]; bf_t* filter = bf_load(bloom_file, BLOOM_HASH_FUNC); if (!filter) { printf("File %s has wrong format or doesn't exist.\n", bloom_file); return 2; } printf("capacity: %ld\n", filter->bits_count); printf("used: %ld\n", filter->bits_used); printf("error rate: %.3f%%\n", exp(- filter->bits_count / (float)filter->bits_used * BF_LOG2_2)); printf("hash/item: %d\n", filter->hashes_count); bf_destroy(filter); }
static PX_Cipher * bf_cbc_load(void) { return bf_load(MODE_CBC); }
static PX_Cipher * bf_ecb_load(void) { return bf_load(MODE_ECB); }