suricata
util-hash-string.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2017 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 #include "suricata-common.h"
19 #include "util-hash-string.h"
20 
21 /* djb2 string hashing */
22 uint32_t StringHashDjb2(const uint8_t *data, uint32_t datalen)
23 {
24  uint32_t hash = 5381;
25  for (uint32_t i = 0; i < datalen; i++) {
26  uint32_t c = data[i];
27  hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
28  }
29  return hash;
30 }
31 
32 /* djb2 string hashing */
33 uint32_t StringHashFunc(HashTable *ht, void *data, uint16_t datalen)
34 {
35  return StringHashDjb2(data, datalen) % ht->array_size;
36 }
37 
38 char StringHashCompareFunc(void *data1, uint16_t datalen1,
39  void *data2, uint16_t datalen2)
40 {
41  int len1 = strlen((char *)data1);
42  int len2 = strlen((char *)data2);
43 
44  if (len1 == len2 && memcmp(data1, data2, len1) == 0) {
45  return 1;
46  }
47 
48  return 0;
49 }
50 
51 void StringHashFreeFunc(void *data)
52 {
53  SCFree(data);
54 }
util-hash-string.h
HashTable_
Definition: util-hash.h:35
StringHashCompareFunc
char StringHashCompareFunc(void *data1, uint16_t datalen1, void *data2, uint16_t datalen2)
Definition: util-hash-string.c:38
StringHashFunc
uint32_t StringHashFunc(HashTable *ht, void *data, uint16_t datalen)
Definition: util-hash-string.c:33
HashTable_::array_size
uint32_t array_size
Definition: util-hash.h:37
StringHashFreeFunc
void StringHashFreeFunc(void *data)
Definition: util-hash-string.c:51
suricata-common.h
SCFree
#define SCFree(p)
Definition: util-mem.h:61
StringHashDjb2
uint32_t StringHashDjb2(const uint8_t *data, uint32_t datalen)
Definition: util-hash-string.c:22