-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbloom-js-export.c
111 lines (84 loc) · 2.88 KB
/
bloom-js-export.c
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
/* bloom-js-export.c
*
* Wrap bloom.h library functions for emcc JavaScript/wasm export. Main
* function that does nothing. Function comments can be found in bloom.c and
* bloom.h.
*
* This file is useful to have around if it becomes desirable to change the
* JavaScript interface without changing the underlying library interface.
*
* Created by Jacob Strieb
* January 2021
*/
#include <stdio.h>
#include <stdlib.h>
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#else /* __EMSCRIPTEN__ */
#define EMSCRIPTEN_KEEPALIVE
#endif /* __EMSCRIPTEN__ */
#include "bloom.h"
/*******************************************************************************
* Types and Structures
******************************************************************************/
struct decompressed_s {
byte *bloom;
size_t size;
};
/*******************************************************************************
* Wrappers around library functions
******************************************************************************/
EMSCRIPTEN_KEEPALIVE
byte *js_new_bloom(uint8_t num_bits) {
return new_bloom(num_bits);
}
EMSCRIPTEN_KEEPALIVE
void js_free_bloom(byte *bloom) {
free_bloom(bloom);
}
/***
* Return a pointer to a heap-allocated structure containing a pointer to the
* heap-allocated decompressed Bloom filter and its size. Use the helpful
* wrappers below to return the address and size individually from the structure.
*
* Implemented this way to facilitate returning a size and address with only
* one call to the underlying decompression function – decompressing twice is
* wasteful.
*
* NOTE: Both the structure and returned Bloom filter must be individually and
* manually freed.
*/
EMSCRIPTEN_KEEPALIVE
struct decompressed_s *js_decompress_bloom(byte *compressed, size_t size) {
struct decompressed_s *decompressed = malloc(sizeof(struct decompressed_s));
byte *bloom;
decompressed->size = decompress_bloom(compressed, size, &bloom);
decompressed->bloom = bloom;
return decompressed;
}
EMSCRIPTEN_KEEPALIVE
size_t js_get_decompressed_size(struct decompressed_s *decompressed) {
return decompressed->size;
}
EMSCRIPTEN_KEEPALIVE
byte *js_get_decompressed_bloom(struct decompressed_s *decompressed) {
return decompressed->bloom;
}
EMSCRIPTEN_KEEPALIVE
void js_add_bloom(byte *bloom, uint8_t num_bits, byte *data, uint32_t length) {
add_bloom(bloom, num_bits, data, length);
}
EMSCRIPTEN_KEEPALIVE
int js_in_bloom(byte *bloom, uint8_t num_bits, byte *data, uint32_t length) {
return in_bloom(bloom, num_bits, data, length);
}
EMSCRIPTEN_KEEPALIVE
void js_combine_bloom(byte *bloom, byte *new, uint8_t num_bits) {
combine_bloom(bloom, new, num_bits);
}
/*******************************************************************************
* (Empty) main function
******************************************************************************/
int main() {
return 0;
}