aboutsummaryrefslogtreecommitdiff
path: root/src/lib/ndpi_bitmap.c
blob: 6caf174bf237fa012f90e92ad0747700f14fae7e (plain)
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
/*
 * ndpi_bitmap.c
 *
 * Copyright (C) 2011-24 - ntop.org and contributors
 *
 * This file is part of nDPI, an open source deep packet inspection
 * library based on the OpenDPI and PACE technology by ipoque GmbH
 *
 * nDPI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * nDPI 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with nDPI.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <sys/types.h>


#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_UNKNOWN

#include "ndpi_config.h"
#include "ndpi_api.h"
#include "ndpi_includes.h"
#include "ndpi_encryption.h"

#ifdef USE_ROARING_V2 
#include "third_party/include/roaring_v2.h"
#else
#include "third_party/include/roaring.h"
#endif

/* ******************************************* */

ndpi_bitmap* ndpi_bitmap_alloc() {
#ifdef USE_ROARING_V2
  return((ndpi_bitmap*)roaring_bitmap_create());
#else
  return((ndpi_bitmap*)roaring64_bitmap_create());
#endif
}

/* ******************************************* */

void ndpi_bitmap_free(ndpi_bitmap* b) {
#ifdef USE_ROARING_V2
  roaring_bitmap_free((const roaring_bitmap_t *)b);
#else
  roaring64_bitmap_free((roaring64_bitmap_t *)b);
#endif
}

/* ******************************************* */

ndpi_bitmap* ndpi_bitmap_copy(ndpi_bitmap* b) {
#ifdef USE_ROARING_V2
  return(roaring_bitmap_copy(b));
#else
  return(roaring64_bitmap_copy(b));
#endif
}

/* ******************************************* */

u_int64_t ndpi_bitmap_cardinality(ndpi_bitmap* b) {
#ifdef USE_ROARING_V2
  return(roaring_bitmap_get_cardinality((const roaring_bitmap_t *)b));
#else
  return(roaring64_bitmap_get_cardinality((roaring64_bitmap_t *)b));
#endif
}

/* ******************************************* */

void ndpi_bitmap_set(ndpi_bitmap* b, u_int64_t value) {
#ifdef USE_ROARING_V2
  roaring_bitmap_add((roaring_bitmap_t *)b, value);
#else
  roaring64_bitmap_add((roaring64_bitmap_t *)b, value);
#endif
}

/* ******************************************* */

void ndpi_bitmap_unset(ndpi_bitmap* b, u_int64_t value) {
#ifdef USE_ROARING_V2
  roaring_bitmap_remove((roaring_bitmap_t *)b, value);
#else
  roaring64_bitmap_remove((roaring64_bitmap_t *)b, value);
#endif
}

/* ******************************************* */

bool ndpi_bitmap_isset(ndpi_bitmap* b, u_int64_t value) {
  bool ret;
  
#ifdef USE_ROARING_V2
  ret = roaring_bitmap_contains((const roaring_bitmap_t *)b, value);
#else
  ret = roaring64_bitmap_contains((const roaring64_bitmap_t *)b, value);
#endif

  return(ret);
}

/* ******************************************* */

size_t ndpi_bitmap_serialize(ndpi_bitmap* b, char **buf) {
  size_t s;

#ifdef USE_ROARING_V2
  const roaring_bitmap_t *r = (const roaring_bitmap_t *)b;
  
  s = roaring_bitmap_portable_size_in_bytes(r);
#else
  const roaring64_bitmap_t *r = (const roaring64_bitmap_t *)b;
  
  s = roaring64_bitmap_portable_size_in_bytes(r);
#endif
  
  *buf = (char*)ndpi_malloc(s);

  if((*buf) == NULL) return(0);

#ifdef USE_ROARING_V2
  return(roaring_bitmap_portable_serialize(r, *buf));
#else
  return(roaring64_bitmap_portable_serialize(r, *buf));
#endif
}

/* ******************************************* */

ndpi_bitmap* ndpi_bitmap_deserialize(char *buf, size_t buf_len) {
#ifdef USE_ROARING_V2
  return((ndpi_bitmap*)roaring_bitmap_portable_deserialize_safe(buf, buf_len));
#else
  return((ndpi_bitmap*)roaring64_bitmap_portable_deserialize_safe(buf, buf_len));
#endif
}

/* ******************************************* */

/* b = b & b_and */
void ndpi_bitmap_and(ndpi_bitmap* a, ndpi_bitmap* b_and) {
#ifdef USE_ROARING_V2
  roaring_bitmap_and_inplace((roaring_bitmap_t*)a, (roaring_bitmap_t*)b_and);
#else
  roaring64_bitmap_and_inplace((roaring64_bitmap_t*)a, (roaring64_bitmap_t*)b_and);
#endif
}

/* ******************************************* */

/* b = b & b_and */
ndpi_bitmap* ndpi_bitmap_and_alloc(ndpi_bitmap* a, ndpi_bitmap* b_and) {
#ifdef USE_ROARING_V2
  return((ndpi_bitmap*)roaring_bitmap_and((roaring_bitmap_t*)a, (roaring_bitmap_t*)b_and));
#else
  return((ndpi_bitmap*)roaring64_bitmap_and((roaring64_bitmap_t*)a, (roaring64_bitmap_t*)b_and));
#endif
}

/* ******************************************* */

/* b = b & !b_and */
void ndpi_bitmap_andnot(ndpi_bitmap* a, ndpi_bitmap* b_and) {
#ifdef USE_ROARING_V2
  roaring_bitmap_andnot_inplace((roaring_bitmap_t*)a, (roaring_bitmap_t*)b_and);
#else
  roaring64_bitmap_andnot_inplace((roaring64_bitmap_t*)a, (roaring64_bitmap_t*)b_and);
#endif
}

/* ******************************************* */

/* b = b | b_or */
void ndpi_bitmap_or(ndpi_bitmap* a, ndpi_bitmap* b_or) {
#ifdef USE_ROARING_V2
  roaring_bitmap_or_inplace((roaring_bitmap_t*)a, (roaring_bitmap_t*)b_or);
#else
  roaring64_bitmap_or_inplace((roaring64_bitmap_t*)a, (roaring64_bitmap_t*)b_or);
#endif
}

/* ******************************************* */

/* b = b | b_or */
ndpi_bitmap* ndpi_bitmap_or_alloc(ndpi_bitmap* a, ndpi_bitmap* b_or) {
#ifdef USE_ROARING_V2
  return((ndpi_bitmap*)roaring_bitmap_or((roaring_bitmap_t*)a, (roaring_bitmap_t*)b_or));
#else
  return((ndpi_bitmap*)roaring64_bitmap_or((roaring64_bitmap_t*)a, (roaring64_bitmap_t*)b_or));
#endif
}

/* ******************************************* */

/* b = b ^ b_xor */
void ndpi_bitmap_xor(ndpi_bitmap* a, ndpi_bitmap* b_xor) {
#ifdef USE_ROARING_V2
  roaring_bitmap_xor_inplace((roaring_bitmap_t*)a, (roaring_bitmap_t*)b_xor);
#else
  roaring64_bitmap_xor_inplace((roaring64_bitmap_t*)a, (roaring64_bitmap_t*)b_xor);
#endif
}

/* ******************************************* */

void ndpi_bitmap_optimize(ndpi_bitmap* a) {
#ifdef USE_ROARING_V2
  roaring_bitmap_run_optimize(a);
#else
  roaring64_bitmap_run_optimize(a);
#endif
}

/* ******************************************* */

ndpi_bitmap_iterator* ndpi_bitmap_iterator_alloc(ndpi_bitmap* b) {
#ifdef USE_ROARING_V2 
  return((ndpi_bitmap_iterator*)roaring_create_iterator((roaring_bitmap_t*)b));
#else
  return((ndpi_bitmap_iterator*)roaring64_iterator_create((const roaring64_bitmap_t*)b));
#endif
}

/* ******************************************* */

void ndpi_bitmap_iterator_free(ndpi_bitmap* b) {
#ifdef USE_ROARING_V2
  roaring_free_uint32_iterator((roaring_uint32_iterator_t*)b);
#else
  roaring64_iterator_free((roaring64_iterator_t*)b);
#endif
}

/* ******************************************* */

bool ndpi_bitmap_is_empty(ndpi_bitmap* b) {
#ifdef USE_ROARING_V2
  return(roaring_bitmap_is_empty((roaring_bitmap_t*)b));
#else
  return(roaring64_bitmap_is_empty((roaring64_bitmap_t*)b));
#endif
}

/* ******************************************* */

/* Return the next value in the bitmap iterator
   
   true is returned when a value is present, false when we reached the end 
*/
bool ndpi_bitmap_iterator_next(ndpi_bitmap_iterator* i, u_int64_t *value) {
#ifdef USE_ROARING_V2
  uint32_t ret;
  uint32_t num = roaring_read_uint32_iterator((roaring_uint32_iterator_t*)i, &ret, 1);

  *value = (uint32_t)ret;
#else
  uint64_t num = roaring64_iterator_read((roaring64_iterator_t*)i, value, 1);
#endif
  
  return((num == 1) ? true /* found */ : false /* not found */);  
}