1 /*
2 
3 Slightly adapted from Source by Bob Jenkins. The original comments
4 of the file have been retained. Some unused code has been removed,
5 the code was converted to ANSI C and the type system is adapted to
6 that of my software. MdD Jan 2003.
7 
8 --------------------------------------------------------------------
9 lookupa.c, by Bob Jenkins, December 1996.  Same as lookup2.c
10 You may use this code in any way you wish.  It has no warranty.
11 Source is http://burtleburtle.net/bob/c/lookupa.c
12 --------------------------------------------------------------------
13 
14 */
15 
16 #   include	"appUtilConfig.h"
17 #   include	"utilJenkinsHash.h"
18 
19 #   define	ub4	UtilUint32
20 #   define	ub1	unsigned char
21 
22 /*
23 --------------------------------------------------------------------
24 mix -- mix 3 32-bit values reversibly.
25 For every delta with one or two bit set, and the deltas of all three
26   high bits or all three low bits, whether the original value of a,b,c
27   is almost all zero or is uniformly distributed,
28 * If mix() is run forward or backward, at least 32 bits in a,b,c
29   have at least 1/4 probability of changing.
30 * If mix() is run forward, every bit of c will change between 1/3 and
31   2/3 of the time.  (Well, 22/100 and 78/100 for some 2-bit deltas.)
32 mix() takes 36 machine instructions, but only 18 cycles on a superscalar
33   machine.  (Pentiums and Sparcs do not appear to be superscalar machines,
34   despite claims to the contrary.)  No faster mixer seems to work,
35   that's the result of my brute-force search.  There were about 2^^68
36   hashes to choose from.  I only tested about a billion of those.
37 --------------------------------------------------------------------
38 */
39 #define mix(a,b,c) \
40 { \
41   a -= b; a -= c; a ^= (c>>13); \
42   b -= c; b -= a; b ^= (a<<8); \
43   c -= a; c -= b; c ^= (b>>13); \
44   a -= b; a -= c; a ^= (c>>12);  \
45   b -= c; b -= a; b ^= (a<<16); \
46   c -= a; c -= b; c ^= (b>>5); \
47   a -= b; a -= c; a ^= (c>>3);  \
48   b -= c; b -= a; b ^= (a<<10); \
49   c -= a; c -= b; c ^= (b>>15); \
50 }
51 
52 /*
53 --------------------------------------------------------------------
54 lookup() -- hash a variable-length key into a 32-bit value
55   k     : the key (the unaligned variable-length array of bytes)
56   len   : the length of the key, counting by bytes
57   level : can be any 4-byte value
58 Returns a 32-bit value.  Every bit of the key affects every bit of
59 the return value.  Every 1-bit and 2-bit delta achieves avalanche.
60 About 6len+35 instructions.
61 
62 The best hash table sizes are powers of 2.  There is no need to do
63 mod a prime (mod is sooo slow!).  If you need less than 32 bits,
64 use a bitmask.  For example, if you need only 10 bits, do
65   h = (h & hashmask(10));
66 In which case, the hash table should have hashsize(10) elements.
67 
68 If you are hashing n strings (ub1 **)k, do it like this:
69   for (i=0, h=0; i<n; ++i) h = lookup( k[i], len[i], h);
70 
71 By Bob Jenkins, 1996.  bob_jenkins@burtleburtle.net.  You may use this
72 code any way you wish, private, educational, or commercial.
73 
74 See http://burtleburtle.net/bob/hash/evahash.html
75 Use for hash table lookup, or anything where one collision in 2^32 is
76 acceptable.  Do NOT use for cryptographic purposes.
77 --------------------------------------------------------------------
78 
79 k:        the key
80 length:   the length of the key
81 level:    the previous hash, or an arbitrary value
82 
83 */
84 
utilJenkinsHash(const unsigned char * k,int l,unsigned long lev)85 unsigned long utilJenkinsHash(	const unsigned char *	k,
86 				int			l,
87 				unsigned long		lev )
88 {
89    ub4  length= l;
90    ub4  level= lev;
91 
92    register ub4 a,b,c,len;
93 
94    /* Set up the internal state */
95    len = length;
96    a = b = 0x9e3779b9;  /* the golden ratio; an arbitrary value */
97    c = level;           /* the previous hash value */
98 
99    /*---------------------------------------- handle most of the key */
100    while (len >= 12)
101    {
102       a += (k[0] +((ub4)k[1]<<8) +((ub4)k[2]<<16) +((ub4)k[3]<<24));
103       b += (k[4] +((ub4)k[5]<<8) +((ub4)k[6]<<16) +((ub4)k[7]<<24));
104       c += (k[8] +((ub4)k[9]<<8) +((ub4)k[10]<<16)+((ub4)k[11]<<24));
105       mix(a,b,c);
106       k += 12; len -= 12;
107    }
108 
109    /*------------------------------------- handle the last 11 bytes */
110    c += length;
111    switch(len)              /* all the case statements fall through */
112    {
113    case 11: c+=((ub4)k[10]<<24);
114    case 10: c+=((ub4)k[9]<<16);
115    case 9 : c+=((ub4)k[8]<<8);
116       /* the first byte of c is reserved for the length */
117    case 8 : b+=((ub4)k[7]<<24);
118    case 7 : b+=((ub4)k[6]<<16);
119    case 6 : b+=((ub4)k[5]<<8);
120    case 5 : b+=k[4];
121    case 4 : a+=((ub4)k[3]<<24);
122    case 3 : a+=((ub4)k[2]<<16);
123    case 2 : a+=((ub4)k[1]<<8);
124    case 1 : a+=k[0];
125      /* case 0: nothing left to add */
126    }
127    mix(a,b,c);
128    /*-------------------------------------------- report the result */
129    return c;
130 }
131 
132 /*
133 --------------------------------------------------------------------
134 mixc -- mixc 8 4-bit values as quickly and thoroughly as possible.
135 Repeating mix() three times achieves avalanche.
136 Repeating mix() four times eliminates all funnels and all
137   characteristics stronger than 2^{-11}.
138 --------------------------------------------------------------------
139 */
140 #define mixc(a,b,c,d,e,f,g,h) \
141 { \
142    a^=b<<11; d+=a; b+=c; \
143    b^=c>>2;  e+=b; c+=d; \
144    c^=d<<8;  f+=c; d+=e; \
145    d^=e>>16; g+=d; e+=f; \
146    e^=f<<10; h+=e; f+=g; \
147    f^=g>>4;  a+=f; g+=h; \
148    g^=h<<8;  b+=g; h+=a; \
149    h^=a>>9;  c+=h; a+=b; \
150 }
151 
152 /*
153 --------------------------------------------------------------------
154 checksum() -- hash a variable-length key into a 256-bit value
155   k     : the key (the unaligned variable-length array of bytes)
156   len   : the length of the key, counting by bytes
157   state : an array of CHECKSTATE 4-byte values (256 bits)
158 The state is the checksum.  Every bit of the key affects every bit of
159 the state.  There are no funnels.  About 112+6.875len instructions.
160 
161 If you are hashing n strings (ub1 **)k, do it like this:
162   for (i=0; i<8; ++i) state[i] = 0x9e3779b9;
163   for (i=0, h=0; i<n; ++i) checksum( k[i], len[i], state);
164 
165 (c) Bob Jenkins, 1996.  bob_jenkins@burtleburtle.net.  You may use this
166 code any way you wish, private, educational, or commercial, as long
167 as this whole comment accompanies it.
168 
169 See http://burtleburtle.net/bob/hash/evahash.html
170 Use to detect changes between revisions of documents, assuming nobody
171 is trying to cause collisions.  Do NOT use for cryptography.
172 --------------------------------------------------------------------
173 */
174 
utilJenkinsHash2(const unsigned char * k,int l,unsigned long * state)175 void  utilJenkinsHash2(	const unsigned char *	k,
176 			int			l,
177 			unsigned long *		state )
178 {
179    register ub4  len= l;
180 
181    register ub4 a,b,c,d,e,f,g,h,length;
182 
183    /* Use the length and level; add in the golden ratio. */
184    length = len;
185    a=state[0]; b=state[1]; c=state[2]; d=state[3];
186    e=state[4]; f=state[5]; g=state[6]; h=state[7];
187 
188    /*---------------------------------------- handle most of the key */
189    while (len >= 32)
190    {
191       a += (k[0] +(k[1]<<8) +(k[2]<<16) +(k[3]<<24));
192       b += (k[4] +(k[5]<<8) +(k[6]<<16) +(k[7]<<24));
193       c += (k[8] +(k[9]<<8) +(k[10]<<16)+(k[11]<<24));
194       d += (k[12]+(k[13]<<8)+(k[14]<<16)+(k[15]<<24));
195       e += (k[16]+(k[17]<<8)+(k[18]<<16)+(k[19]<<24));
196       f += (k[20]+(k[21]<<8)+(k[22]<<16)+(k[23]<<24));
197       g += (k[24]+(k[25]<<8)+(k[26]<<16)+(k[27]<<24));
198       h += (k[28]+(k[29]<<8)+(k[30]<<16)+(k[31]<<24));
199       mixc(a,b,c,d,e,f,g,h);
200       mixc(a,b,c,d,e,f,g,h);
201       mixc(a,b,c,d,e,f,g,h);
202       mixc(a,b,c,d,e,f,g,h);
203       k += 32; len -= 32;
204    }
205 
206    /*------------------------------------- handle the last 31 bytes */
207    h += length;
208    switch(len)
209    {
210    case 31: h+=(k[30]<<24);
211    case 30: h+=(k[29]<<16);
212    case 29: h+=(k[28]<<8);
213    case 28: g+=(k[27]<<24);
214    case 27: g+=(k[26]<<16);
215    case 26: g+=(k[25]<<8);
216    case 25: g+=k[24];
217    case 24: f+=(k[23]<<24);
218    case 23: f+=(k[22]<<16);
219    case 22: f+=(k[21]<<8);
220    case 21: f+=k[20];
221    case 20: e+=(k[19]<<24);
222    case 19: e+=(k[18]<<16);
223    case 18: e+=(k[17]<<8);
224    case 17: e+=k[16];
225    case 16: d+=(k[15]<<24);
226    case 15: d+=(k[14]<<16);
227    case 14: d+=(k[13]<<8);
228    case 13: d+=k[12];
229    case 12: c+=(k[11]<<24);
230    case 11: c+=(k[10]<<16);
231    case 10: c+=(k[9]<<8);
232    case 9 : c+=k[8];
233    case 8 : b+=(k[7]<<24);
234    case 7 : b+=(k[6]<<16);
235    case 6 : b+=(k[5]<<8);
236    case 5 : b+=k[4];
237    case 4 : a+=(k[3]<<24);
238    case 3 : a+=(k[2]<<16);
239    case 2 : a+=(k[1]<<8);
240    case 1 : a+=k[0];
241    }
242    mixc(a,b,c,d,e,f,g,h);
243    mixc(a,b,c,d,e,f,g,h);
244    mixc(a,b,c,d,e,f,g,h);
245    mixc(a,b,c,d,e,f,g,h);
246 
247    /*-------------------------------------------- report the result */
248    state[0]=a; state[1]=b; state[2]=c; state[3]=d;
249    state[4]=e; state[5]=f; state[6]=g; state[7]=h;
250 }
251