aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/suricata/src/util-bloomfilter.c
blob: 5718fb1002c63270fa73e764259922250854be1b (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
278
279
280
281
282
283
284
285
286
287
288
289
290
/* Copyright (C) 2007-2010 Open Information Security Foundation
 *
 * You can copy, redistribute or modify this Program under the terms of
 * the GNU General Public License version 2 as published by the Free
 * Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * version 2 along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

/**
 * \file
 *
 * \author Victor Julien <victor@inliniac.net>
 *
 * Bitwise bloom filter implementation
 */

#include "suricata-common.h"
#include "util-bloomfilter.h"
#include "util-unittest.h"

BloomFilter *BloomFilterInit(uint32_t size, uint8_t iter, uint32_t (*Hash)(void *, uint16_t, uint8_t, uint32_t)) {
    BloomFilter *bf = NULL;

    if (size == 0 || iter == 0)
        goto error;

    if (Hash == NULL) {
        //printf("ERROR: BloomFilterInit no Hash function\n");
        goto error;
    }

    /* setup the filter */
    bf = SCMalloc(sizeof(BloomFilter));
    if (unlikely(bf == NULL))
        goto error;
    memset(bf,0,sizeof(BloomFilter));
    bf->bitarray_size = size;
    bf->hash_iterations = iter;
    bf->Hash = Hash;

    /* setup the bitarray */
    bf->bitarray = SCMalloc((bf->bitarray_size/8)+1);
    if (bf->bitarray == NULL)
        goto error;
    memset(bf->bitarray,0,(bf->bitarray_size/8)+1);

    return bf;

error:
    if (bf != NULL) {
        if (bf->bitarray != NULL)
            SCFree(bf->bitarray);

        SCFree(bf);
    }
    return NULL;
}

void BloomFilterFree(BloomFilter *bf)
{
    if (bf != NULL) {
        if (bf->bitarray != NULL)
            SCFree(bf->bitarray);

        SCFree(bf);
    }
}

void BloomFilterPrint(BloomFilter *bf)
{
    printf("\n---------- Bloom Filter Stats -----------\n");
    printf("Buckets:               %" PRIu32 "\n", bf->bitarray_size);
    printf("Memory size:           %" PRIu32 " bytes\n", bf->bitarray_size/8 + 1);
    printf("Hash function pointer: %p\n", bf->Hash);
    printf("Hash functions:        %" PRIu32 "\n", bf->hash_iterations);
    printf("-----------------------------------------\n");
}

int BloomFilterAdd(BloomFilter *bf, void *data, uint16_t datalen)
{
    uint8_t iter = 0;
    uint32_t hash = 0;

    if (bf == NULL || data == NULL || datalen == 0)
        return -1;

    for (iter = 0; iter < bf->hash_iterations; iter++) {
        hash = bf->Hash(data, datalen, iter, bf->bitarray_size);
        bf->bitarray[hash/8] |= (1<<hash%8);
    }

    return 0;
}

uint32_t BloomFilterMemoryCnt(BloomFilter *bf)
{
     if (bf == NULL)
         return 0;

     return 2;
}

uint32_t BloomFilterMemorySize(BloomFilter *bf)
{
     if (bf == NULL)
         return 0;

     return (sizeof(BloomFilter) + (bf->bitarray_size/8) + 1);
}

/*
 * ONLY TESTS BELOW THIS COMMENT
 */

#ifdef UNITTESTS
static uint32_t BloomFilterTestHash(void *data, uint16_t datalen, uint8_t iter, uint32_t hash_size)
{
     uint8_t *d = (uint8_t *)data;
     uint32_t i;
     uint32_t hash = 0;

     for (i = 0; i < datalen; i++) {
         if (i == 0)      hash += (((uint32_t)*d++));
         else if (i == 1) hash += (((uint32_t)*d++) * datalen);
         else             hash *= (((uint32_t)*d++) * i);
     }

     hash *= (iter + datalen);
     hash %= hash_size;
     return hash;
}

static int BloomFilterTestInit01 (void)
{
    BloomFilter *bf = BloomFilterInit(1024, 4, BloomFilterTestHash);
    if (bf == NULL)
        return 0;

    BloomFilterFree(bf);
    return 1;
}

/* no hash function, so it should fail */
static int BloomFilterTestInit02 (void)
{
    BloomFilter *bf = BloomFilterInit(1024, 4, NULL);
    if (bf == NULL)
        return 1;

    BloomFilterFree(bf);
    return 0;
}

static int BloomFilterTestInit03 (void)
{
    int result = 0;
    BloomFilter *bf = BloomFilterInit(1024, 4, BloomFilterTestHash);
    if (bf == NULL)
        return 0;

    if (bf->Hash == BloomFilterTestHash)
        result = 1;

    BloomFilterFree(bf);
    return result;
}

static int BloomFilterTestInit04 (void)
{
    BloomFilter *bf = BloomFilterInit(1024, 0, BloomFilterTestHash);
    if (bf == NULL)
        return 1;

    BloomFilterFree(bf);
    return 0;
}

static int BloomFilterTestInit05 (void)
{
    BloomFilter *bf = BloomFilterInit(0, 4, BloomFilterTestHash);
    if (bf == NULL)
        return 1;

    BloomFilterFree(bf);
    return 0;
}

static int BloomFilterTestAdd01 (void)
{
    int result = 0;
    BloomFilter *bf = BloomFilterInit(1024, 4, BloomFilterTestHash);
    if (bf == NULL)
        return 0;

    int r = BloomFilterAdd(bf, "test", 0);
    if (r == 0)
        goto end;

    /* all is good! */
    result = 1;
end:
    if (bf != NULL) BloomFilterFree(bf);
    return result;
}

static int BloomFilterTestAdd02 (void)
{
    int result = 0;
    BloomFilter *bf = BloomFilterInit(1024, 4, BloomFilterTestHash);
    if (bf == NULL)
        return 0;

    int r = BloomFilterAdd(bf, NULL, 4);
    if (r == 0)
        goto end;

    /* all is good! */
    result = 1;
end:
    if (bf != NULL) BloomFilterFree(bf);
    return result;
}

static int BloomFilterTestFull01 (void)
{
    int result = 0;
    BloomFilter *bf = BloomFilterInit(32, 4, BloomFilterTestHash);
    if (bf == NULL)
        goto end;

    int r = BloomFilterAdd(bf, "test", 4);
    if (r != 0)
        goto end;

    r = BloomFilterTest(bf, "test", 4);
    if (r != 1)
        goto end;

    /* all is good! */
    result = 1;
end:
    if (bf != NULL) BloomFilterFree(bf);
    return result;
}

static int BloomFilterTestFull02 (void)
{
    int result = 0;
    BloomFilter *bf = BloomFilterInit(32, 4, BloomFilterTestHash);
    if (bf == NULL)
        goto end;

    int r = BloomFilterTest(bf, "test", 4);
    if (r != 0)
        goto end;

    /* all is good! */
    result = 1;
end:
    if (bf != NULL) BloomFilterFree(bf);
    return result;
}
#endif /* UNITTESTS */

void BloomFilterRegisterTests(void)
{
#ifdef UNITTESTS
    UtRegisterTest("BloomFilterTestInit01", BloomFilterTestInit01, 1);
    UtRegisterTest("BloomFilterTestInit02", BloomFilterTestInit02, 1);
    UtRegisterTest("BloomFilterTestInit03", BloomFilterTestInit03, 1);
    UtRegisterTest("BloomFilterTestInit04", BloomFilterTestInit04, 1);
    UtRegisterTest("BloomFilterTestInit05", BloomFilterTestInit05, 1);

    UtRegisterTest("BloomFilterTestAdd01", BloomFilterTestAdd01, 1);
    UtRegisterTest("BloomFilterTestAdd02", BloomFilterTestAdd02, 1);

    UtRegisterTest("BloomFilterTestFull01", BloomFilterTestFull01, 1);
    UtRegisterTest("BloomFilterTestFull02", BloomFilterTestFull02, 1);
#endif /* UNITTESTS */
}