2 * Hash lookup function.
3 * Copyright (C) 2003, 2004 Mondru AB.
5 * The contents of this file may be used under the terms of the GNU
6 * General Public License Version 2, provided that the above copyright
7 * notice and this permission notice is included in all copies or
8 * substantial portions of the software.
14 * Generates a 32 bit hash.
15 * Based on public domain code by Bob Jenkins
16 * It should be one of the best hash functions around in terms of both
17 * statistical properties and speed. It is NOT recommended for cryptographic
20 unsigned long int lookup(k
, length
, level
)
21 register unsigned char *k
; /* the key */
22 register unsigned long int length
; /* the length of the key */
23 register unsigned long int level
; /* the previous hash, or an arbitrary value */
28 a -= b; a -= c; a ^= (c>>13); \
29 b -= c; b -= a; b ^= (a<<8); \
30 c -= a; c -= b; c ^= (b>>13); \
31 a -= b; a -= c; a ^= (c>>12); \
32 b -= c; b -= a; b ^= (a<<16); \
33 c -= a; c -= b; c ^= (b>>5); \
34 a -= b; a -= c; a ^= (c>>3); \
35 b -= c; b -= a; b ^= (a<<10); \
36 c -= a; c -= b; c ^= (b>>15); \
39 typedef unsigned long int ub4
; /* unsigned 4-byte quantities */
40 register unsigned long int a
, b
, c
, len
;
42 /* Set up the internal state */
44 a
= b
= 0x9e3779b9; /* the golden ratio; an arbitrary value */
45 c
= level
; /* the previous hash value */
47 /*---------------------------------------- handle most of the key */
49 a
+= (k
[0] + ((ub4
) k
[1] << 8) + ((ub4
) k
[2] << 16) +
51 b
+= (k
[4] + ((ub4
) k
[5] << 8) + ((ub4
) k
[6] << 16) +
53 c
+= (k
[8] + ((ub4
) k
[9] << 8) + ((ub4
) k
[10] << 16) +
60 /*------------------------------------- handle the last 11 bytes */
62 switch (len
) { /* all the case statements fall through */
64 c
+= ((ub4
) k
[10] << 24);
66 c
+= ((ub4
) k
[9] << 16);
68 c
+= ((ub4
) k
[8] << 8);
69 /* the first byte of c is reserved for the length */
71 b
+= ((ub4
) k
[7] << 24);
73 b
+= ((ub4
) k
[6] << 16);
75 b
+= ((ub4
) k
[5] << 8);
79 a
+= ((ub4
) k
[3] << 24);
81 a
+= ((ub4
) k
[2] << 16);
83 a
+= ((ub4
) k
[1] << 8);
86 /* case 0: nothing left to add */
89 /*-------------------------------------------- report the result */