suricata
util-hash-lookup3.h
Go to the documentation of this file.
1 /*
2 -------------------------------------------------------------------------------
3 lookup3.c, by Bob Jenkins, May 2006, Public Domain.
4 
5 These are functions for producing 32-bit hashes for hash table lookup.
6 hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
7 are externally useful functions. Routines to test the hash are included
8 if SELF_TEST is defined. You can use this free for any purpose. It's in
9 the public domain. It has no warranty.
10 
11 You probably want to use hashlittle(). hashlittle() and hashbig()
12 hash byte arrays. hashlittle() is is faster than hashbig() on
13 little-endian machines. Intel and AMD are little-endian machines.
14 On second thought, you probably want hashlittle2(), which is identical to
15 hashlittle() except it returns two 32-bit hashes for the price of one.
16 You could implement hashbig2() if you wanted but I haven't bothered here.
17 
18 If 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);
25 then use c as the hash value. If you have a variable length array of
26 4-byte integers to hash, use hashword(). If you have a byte array (like
27 a character string), use hashlittle(). If you have several byte arrays, or
28 a mix of things, see the comments above hashlittle().
29 
30 Why is this so big? I read 12 bytes at a time into 3 4-byte integers,
31 then mix those integers. This is fast (you can do a lot more thorough
32 mixing with 12*3 instructions on 3 integers than you can with 3 instructions
33 on 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 
43 uint32_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 
48 void 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 
53 uint32_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. */
58 uint32_t hashlittle_safe(const void *key, size_t length, uint32_t initval);
59 
60 void 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 uint32_t hashbig( const void *key, size_t length, uint32_t initval);
66 
67 #endif /* SURICATA_UTIL_HASH_LOOKUP3_H */
hashlittle
uint32_t hashlittle(const void *key, size_t length, uint32_t initval)
Definition: util-hash-lookup3.c:281
hashlittle_safe
uint32_t hashlittle_safe(const void *key, size_t length, uint32_t initval)
Definition: util-hash-lookup3.c:482
hashword
uint32_t hashword(const uint32_t *k, size_t length, uint32_t initval)
Definition: util-hash-lookup3.c:172
hashlittle2
void hashlittle2(const void *key, size_t length, uint32_t *pc, uint32_t *pb)
Definition: util-hash-lookup3.c:635
hashword2
void hashword2(const uint32_t *k, size_t length, uint32_t *pc, uint32_t *pb)
Definition: util-hash-lookup3.c:216
hashbig
uint32_t hashbig(const void *key, size_t length, uint32_t initval)
Definition: util-hash-lookup3.c:816