suricata
util-bloomfilter.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  */
23 
24 #ifndef __BLOOMFILTER_H__
25 #define __BLOOMFILTER_H__
26 
27 /* Bloom Filter structure */
28 typedef struct BloomFilter_ {
29  uint8_t hash_iterations;
30  uint32_t (*Hash)(const void *, uint16_t, uint8_t, uint32_t);
31  uint32_t bitarray_size;
32  uint8_t *bitarray;
33 } BloomFilter;
34 
35 /* prototypes */
36 BloomFilter *BloomFilterInit(uint32_t, uint8_t, uint32_t (*Hash)(const void *, uint16_t, uint8_t, uint32_t));
39 int BloomFilterAdd(BloomFilter *, const void *, uint16_t);
42 
43 void BloomFilterRegisterTests(void);
44 
45 /** ----- Inline functions ---- */
46 
47 static inline int BloomFilterTest(const BloomFilter *, const void *, uint16_t);
48 
49 static inline int BloomFilterTest(const BloomFilter *bf, const void *data, uint16_t datalen)
50 {
51  uint8_t iter = 0;
52  uint32_t hash = 0;
53  int hit = 1;
54 
55  for (iter = 0; iter < bf->hash_iterations; iter++) {
56  hash = bf->Hash(data, datalen, iter, bf->bitarray_size);
57  if (!(bf->bitarray[hash/8] & (1<<hash%8))) {
58  hit = 0;
59  break;
60  }
61  }
62 
63  return hit;
64 }
65 
66 #endif /* __BLOOMFILTER_H__ */
67 
void BloomFilterRegisterTests(void)
void BloomFilterPrint(BloomFilter *)
uint32_t bitarray_size
int BloomFilterAdd(BloomFilter *, const void *, uint16_t)
BloomFilter * BloomFilterInit(uint32_t, uint8_t, uint32_t(*Hash)(const void *, uint16_t, uint8_t, uint32_t))
uint8_t hash_iterations
uint32_t(* Hash)(const void *, uint16_t, uint8_t, uint32_t)
uint32_t BloomFilterMemorySize(BloomFilter *)
uint8_t * bitarray
struct BloomFilter_ BloomFilter
void BloomFilterFree(BloomFilter *)
uint32_t BloomFilterMemoryCnt(BloomFilter *)