suricata
util-hash-lookup3.h
Go to the documentation of this file.
1/*
2-------------------------------------------------------------------------------
3lookup3.c, by Bob Jenkins, May 2006, Public Domain.
4
5These are functions for producing 32-bit hashes for hash table lookup.
6hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
7are externally useful functions. Routines to test the hash are included
8if SELF_TEST is defined. You can use this free for any purpose. It's in
9the public domain. It has no warranty.
10
11You probably want to use hashlittle(). hashlittle() and hashbig()
12hash byte arrays. hashlittle() is is faster than hashbig() on
13little-endian machines. Intel and AMD are little-endian machines.
14On second thought, you probably want hashlittle2(), which is identical to
15hashlittle() except it returns two 32-bit hashes for the price of one.
16You could implement hashbig2() if you wanted but I haven't bothered here.
17
18If you want to find a hash of, say, exactly 7 integers, do
19 a = i1; b = i2; c = i3;
20 mix(a,b,c);
21 a += i4; b += i5; c += i6;
22 mix(a,b,c);
23 a += i7;
24 final(a,b,c);
25then use c as the hash value. If you have a variable length array of
264-byte integers to hash, use hashword(). If you have a byte array (like
27a character string), use hashlittle(). If you have several byte arrays, or
28a mix of things, see the comments above hashlittle().
29
30Why is this so big? I read 12 bytes at a time into 3 4-byte integers,
31then mix those integers. This is fast (you can do a lot more thorough
32mixing with 12*3 instructions on 3 integers than you can with 3 instructions
33on 1 byte), but shoehorning those bytes into integers efficiently is messy.
34-------------------------------------------------------------------------------
35*/
36
37#ifndef SURICATA_UTIL_HASH_LOOKUP3_H
38#define SURICATA_UTIL_HASH_LOOKUP3_H
39
40#define hashsize(n) ((uint32_t)1<<(n))
41#define hashmask(n) (hashsize(n)-1)
42
43uint32_t hashword(const uint32_t *k, /* the key, an array of uint32_t values */
44 size_t length, /* the length of the key, in uint32_ts */
45 uint32_t initval); /* the previous hash, or an arbitrary value */
46
47
48void hashword2 (const uint32_t *k, /* the key, an array of uint32_t values */
49 size_t length, /* the length of the key, in uint32_ts */
50 uint32_t *pc, /* IN: seed OUT: primary hash value */
51 uint32_t *pb); /* IN: more seed OUT: secondary hash value */
52
53uint32_t hashlittle( const void *key, size_t length, uint32_t initval);
54
55/* A variant of hashlittle() that ensures avoids accesses beyond the last byte
56 * of the string, which will cause warnings from tools like Valgrind or Address
57 * Sanitizer. */
58uint32_t hashlittle_safe(const void *key, size_t length, uint32_t initval);
59
60void hashlittle2(const void *key, /* the key to hash */
61 size_t length, /* length of the key */
62 uint32_t *pc, /* IN: primary initval, OUT: primary hash */
63 uint32_t *pb); /* IN: secondary initval, OUT: secondary hash */
64
65/* A variant of hashlittle2() that ensures avoids accesses beyond the last byte
66 * of the string, which will cause warnings from tools like Valgrind or Address
67 * Sanitizer. */
68void hashlittle2_safe(const void *key, size_t length, uint32_t *pc, uint32_t *pb);
69
70uint32_t hashbig( const void *key, size_t length, uint32_t initval);
71
72#endif /* SURICATA_UTIL_HASH_LOOKUP3_H */
uint32_t hashbig(const void *key, size_t length, uint32_t initval)
uint32_t hashlittle_safe(const void *key, size_t length, uint32_t initval)
void hashlittle2(const void *key, size_t length, uint32_t *pc, uint32_t *pb)
uint32_t hashword(const uint32_t *k, size_t length, uint32_t initval)
uint32_t hashlittle(const void *key, size_t length, uint32_t initval)
void hashlittle2_safe(const void *key, size_t length, uint32_t *pc, uint32_t *pb)
void hashword2(const uint32_t *k, size_t length, uint32_t *pc, uint32_t *pb)