1 #include "jenkins_hash.h"
2 #include <stdlib.h>
3 #ifdef WIN32
4 #define _USE_MATH_DEFINES //For M_LOG2E
5 #endif
6 #include <math.h>
7 #include <limits.h>
8 #include <string.h>
9 
10 //#define DEBUG
11 #include "debug.h"
12 
13 #define hashsize(n) ((cmph_uint32)1<<(n))
14 #define hashmask(n) (hashsize(n)-1)
15 
16 
17 
18 //#define NM2 /* Define this if you do not want power of 2 table sizes*/
19 
20 
21 /*
22    --------------------------------------------------------------------
23    mix -- mix 3 32-bit values reversibly.
24    For every delta with one or two bits set, and the deltas of all three
25    high bits or all three low bits, whether the original value of a,b,c
26    is almost all zero or is uniformly distributed,
27  * If mix() is run forward or backward, at least 32 bits in a,b,c
28  have at least 1/4 probability of changing.
29  * If mix() is run forward, every bit of c will change between 1/3 and
30  2/3 of the time.  (Well, 22/100 and 78/100 for some 2-bit deltas.)
31  mix() was built out of 36 single-cycle latency instructions in a
32  structure that could supported 2x parallelism, like so:
33  a -= b;
34  a -= c; x = (c>>13);
35  b -= c; a ^= x;
36  b -= a; x = (a<<8);
37  c -= a; b ^= x;
38  c -= b; x = (b>>13);
39  ...
40  Unfortunately, superscalar Pentiums and Sparcs can't take advantage
41  of that parallelism.  They've also turned some of those single-cycle
42  latency instructions into multi-cycle latency instructions.  Still,
43  this is the fastest good hash I could find.  There were about 2^^68
44  to choose from.  I only looked at a billion or so.
45  --------------------------------------------------------------------
46  */
47 #define mix(a,b,c) \
48 { \
49 	a -= b; a -= c; a ^= (c>>13); \
50 	b -= c; b -= a; b ^= (a<<8); \
51 	c -= a; c -= b; c ^= (b>>13); \
52 	a -= b; a -= c; a ^= (c>>12);  \
53 	b -= c; b -= a; b ^= (a<<16); \
54 	c -= a; c -= b; c ^= (b>>5); \
55 	a -= b; a -= c; a ^= (c>>3);  \
56 	b -= c; b -= a; b ^= (a<<10); \
57 	c -= a; c -= b; c ^= (b>>15); \
58 }
59 
60 /*
61    --------------------------------------------------------------------
62    hash() -- hash a variable-length key into a 32-bit value
63 k       : the key (the unaligned variable-length array of bytes)
64 len     : the length of the key, counting by bytes
65 initval : can be any 4-byte value
66 Returns a 32-bit value.  Every bit of the key affects every bit of
67 the return value.  Every 1-bit and 2-bit delta achieves avalanche.
68 About 6*len+35 instructions.
69 
70 The best hash table sizes are powers of 2.  There is no need to do
71 mod a prime (mod is sooo slow!).  If you need less than 32 bits,
72 use a bitmask.  For example, if you need only 10 bits, do
73 h = (h & hashmask(10));
74 In which case, the hash table should have hashsize(10) elements.
75 
76 If you are hashing n strings (cmph_uint8 **)k, do it like this:
77 for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);
78 
79 By Bob Jenkins, 1996.  bob_jenkins@burtleburtle.net.  You may use this
80 code any way you wish, private, educational, or commercial.  It's free.
81 
82 See http://burtleburtle.net/bob/hash/evahash.html
83 Use for hash table lookup, or anything where one collision in 2^^32 is
84 acceptable.  Do NOT use for cryptographic purposes.
85 --------------------------------------------------------------------
86  */
jenkins_state_new(cmph_uint32 size)87 jenkins_state_t *jenkins_state_new(cmph_uint32 size) //size of hash table
88 {
89 	jenkins_state_t *state = (jenkins_state_t *)malloc(sizeof(jenkins_state_t));
90         if (!state) return NULL;
91 	DEBUGP("Initializing jenkins hash\n");
92 	state->seed = ((cmph_uint32)rand() % size);
93 	return state;
94 }
jenkins_state_destroy(jenkins_state_t * state)95 void jenkins_state_destroy(jenkins_state_t *state)
96 {
97 	free(state);
98 }
99 
100 
__jenkins_hash_vector(cmph_uint32 seed,const char * k,cmph_uint32 keylen,cmph_uint32 * hashes)101 static inline void __jenkins_hash_vector(cmph_uint32 seed, const char *k, cmph_uint32 keylen, cmph_uint32 * hashes)
102 {
103 	register cmph_uint32 len, length;
104 
105 	/* Set up the internal state */
106 	length = keylen;
107 	len = length;
108 	hashes[0] = hashes[1] = 0x9e3779b9;  /* the golden ratio; an arbitrary value */
109 	hashes[2] = seed;   /* the previous hash value - seed in our case */
110 
111 	/*---------------------------------------- handle most of the key */
112 	while (len >= 12)
113 	{
114 		hashes[0] += ((cmph_uint32)k[0] +((cmph_uint32)k[1]<<8) +((cmph_uint32)k[2]<<16) +((cmph_uint32)k[3]<<24));
115 		hashes[1] += ((cmph_uint32)k[4] +((cmph_uint32)k[5]<<8) +((cmph_uint32)k[6]<<16) +((cmph_uint32)k[7]<<24));
116 		hashes[2] += ((cmph_uint32)k[8] +((cmph_uint32)k[9]<<8) +((cmph_uint32)k[10]<<16)+((cmph_uint32)k[11]<<24));
117 		mix(hashes[0],hashes[1],hashes[2]);
118 		k += 12; len -= 12;
119 	}
120 
121 	/*------------------------------------- handle the last 11 bytes */
122 	hashes[2]  += length;
123 	switch(len)              /* all the case statements fall through */
124 	{
125 		case 11:
126 			hashes[2] +=((cmph_uint32)k[10]<<24);
127 		case 10:
128 			hashes[2] +=((cmph_uint32)k[9]<<16);
129 		case 9 :
130 			hashes[2] +=((cmph_uint32)k[8]<<8);
131 			/* the first byte of hashes[2] is reserved for the length */
132 		case 8 :
133 			hashes[1] +=((cmph_uint32)k[7]<<24);
134 		case 7 :
135 			hashes[1] +=((cmph_uint32)k[6]<<16);
136 		case 6 :
137 			hashes[1] +=((cmph_uint32)k[5]<<8);
138 		case 5 :
139 			hashes[1] +=(cmph_uint8) k[4];
140 		case 4 :
141 			hashes[0] +=((cmph_uint32)k[3]<<24);
142 		case 3 :
143 			hashes[0] +=((cmph_uint32)k[2]<<16);
144 		case 2 :
145 			hashes[0] +=((cmph_uint32)k[1]<<8);
146 		case 1 :
147 			hashes[0] +=(cmph_uint8)k[0];
148 			/* case 0: nothing left to add */
149 	}
150 
151 	mix(hashes[0],hashes[1],hashes[2]);
152 }
153 
jenkins_hash(jenkins_state_t * state,const char * k,cmph_uint32 keylen)154 cmph_uint32 jenkins_hash(jenkins_state_t *state, const char *k, cmph_uint32 keylen)
155 {
156 	cmph_uint32 hashes[3];
157 	__jenkins_hash_vector(state->seed, k, keylen, hashes);
158 	return hashes[2];
159 /*	cmph_uint32 a, b, c;
160 	cmph_uint32 len, length;
161 
162 	// Set up the internal state
163 	length = keylen;
164 	len = length;
165 	a = b = 0x9e3779b9;  // the golden ratio; an arbitrary value
166 	c = state->seed;   // the previous hash value - seed in our case
167 
168 	// handle most of the key
169 	while (len >= 12)
170 	{
171 		a += (k[0] +((cmph_uint32)k[1]<<8) +((cmph_uint32)k[2]<<16) +((cmph_uint32)k[3]<<24));
172 		b += (k[4] +((cmph_uint32)k[5]<<8) +((cmph_uint32)k[6]<<16) +((cmph_uint32)k[7]<<24));
173 		c += (k[8] +((cmph_uint32)k[9]<<8) +((cmph_uint32)k[10]<<16)+((cmph_uint32)k[11]<<24));
174 		mix(a,b,c);
175 		k += 12; len -= 12;
176 	}
177 
178 	// handle the last 11 bytes
179 	c  += length;
180 	switch(len)              /// all the case statements fall through
181 	{
182 		case 11:
183 			c +=((cmph_uint32)k[10]<<24);
184 		case 10:
185 			c +=((cmph_uint32)k[9]<<16);
186 		case 9 :
187 			c +=((cmph_uint32)k[8]<<8);
188 			// the first byte of c is reserved for the length
189 		case 8 :
190 			b +=((cmph_uint32)k[7]<<24);
191 		case 7 :
192 			b +=((cmph_uint32)k[6]<<16);
193 		case 6 :
194 			b +=((cmph_uint32)k[5]<<8);
195 		case 5 :
196 			b +=k[4];
197 		case 4 :
198 			a +=((cmph_uint32)k[3]<<24);
199 		case 3 :
200 			a +=((cmph_uint32)k[2]<<16);
201 		case 2 :
202 			a +=((cmph_uint32)k[1]<<8);
203 		case 1 :
204 			a +=k[0];
205 		// case 0: nothing left to add
206 	}
207 
208 	mix(a,b,c);
209 
210 	/// report the result
211 
212 	return c;
213 	*/
214 }
215 
jenkins_hash_vector_(jenkins_state_t * state,const char * k,cmph_uint32 keylen,cmph_uint32 * hashes)216 void jenkins_hash_vector_(jenkins_state_t *state, const char *k, cmph_uint32 keylen, cmph_uint32 * hashes)
217 {
218 	__jenkins_hash_vector(state->seed, k, keylen, hashes);
219 }
220 
jenkins_state_dump(jenkins_state_t * state,char ** buf,cmph_uint32 * buflen)221 void jenkins_state_dump(jenkins_state_t *state, char **buf, cmph_uint32 *buflen)
222 {
223 	*buflen = sizeof(cmph_uint32);
224 	*buf = (char *)malloc(sizeof(cmph_uint32));
225 	if (!*buf)
226 	{
227 		*buflen = UINT_MAX;
228 		return;
229 	}
230 	memcpy(*buf, &(state->seed), sizeof(cmph_uint32));
231 	DEBUGP("Dumped jenkins state with seed %u\n", state->seed);
232 	return;
233 }
234 
jenkins_state_copy(jenkins_state_t * src_state)235 jenkins_state_t *jenkins_state_copy(jenkins_state_t *src_state)
236 {
237 	jenkins_state_t *dest_state = (jenkins_state_t *)malloc(sizeof(jenkins_state_t));
238 	dest_state->hashfunc = src_state->hashfunc;
239 	dest_state->seed = src_state->seed;
240 	return dest_state;
241 }
242 
jenkins_state_load(const char * buf,cmph_uint32 buflen)243 jenkins_state_t *jenkins_state_load(const char *buf, cmph_uint32 buflen)
244 {
245 	jenkins_state_t *state = (jenkins_state_t *)malloc(sizeof(jenkins_state_t));
246 	state->seed = *(cmph_uint32 *)buf;
247 	state->hashfunc = CMPH_HASH_JENKINS;
248 	DEBUGP("Loaded jenkins state with seed %u\n", state->seed);
249 	return state;
250 }
251 
252 
253 /** \fn void jenkins_state_pack(jenkins_state_t *state, void *jenkins_packed);
254  *  \brief Support the ability to pack a jenkins function into a preallocated contiguous memory space pointed by jenkins_packed.
255  *  \param state points to the jenkins function
256  *  \param jenkins_packed pointer to the contiguous memory area used to store the jenkins function. The size of jenkins_packed must be at least jenkins_state_packed_size()
257  */
jenkins_state_pack(jenkins_state_t * state,void * jenkins_packed)258 void jenkins_state_pack(jenkins_state_t *state, void *jenkins_packed)
259 {
260 	if (state && jenkins_packed)
261 	{
262 		memcpy(jenkins_packed, &(state->seed), sizeof(cmph_uint32));
263 	}
264 }
265 
266 /** \fn cmph_uint32 jenkins_state_packed_size(jenkins_state_t *state);
267  *  \brief Return the amount of space needed to pack a jenkins function.
268  *  \return the size of the packed function or zero for failures
269  */
jenkins_state_packed_size(void)270 cmph_uint32 jenkins_state_packed_size(void)
271 {
272 	return sizeof(cmph_uint32);
273 }
274 
275 
276 /** \fn cmph_uint32 jenkins_hash_packed(void *jenkins_packed, const char *k, cmph_uint32 keylen);
277  *  \param jenkins_packed is a pointer to a contiguous memory area
278  *  \param key is a pointer to a key
279  *  \param keylen is the key length
280  *  \return an integer that represents a hash value of 32 bits.
281  */
jenkins_hash_packed(void * jenkins_packed,const char * k,cmph_uint32 keylen)282 cmph_uint32 jenkins_hash_packed(void *jenkins_packed, const char *k, cmph_uint32 keylen)
283 {
284 	cmph_uint32 hashes[3];
285 	__jenkins_hash_vector(*((cmph_uint32 *)jenkins_packed), k, keylen, hashes);
286 	return hashes[2];
287 }
288 
289 /** \fn jenkins_hash_vector_packed(void *jenkins_packed, const char *k, cmph_uint32 keylen, cmph_uint32 * hashes);
290  *  \param jenkins_packed is a pointer to a contiguous memory area
291  *  \param key is a pointer to a key
292  *  \param keylen is the key length
293  *  \param hashes is a pointer to a memory large enough to fit three 32-bit integers.
294  */
jenkins_hash_vector_packed(void * jenkins_packed,const char * k,cmph_uint32 keylen,cmph_uint32 * hashes)295 void jenkins_hash_vector_packed(void *jenkins_packed, const char *k, cmph_uint32 keylen, cmph_uint32 * hashes)
296 {
297 	__jenkins_hash_vector(*((cmph_uint32 *)jenkins_packed), k, keylen, hashes);
298 }
299