Debian Codegraph @

src/libsystemd/sd-journal/lookup3.c

raw
1 /* SPDX-License-Identifier: LicenseRef-lookup3-public-domain */
2 /* Slightly modified by Lennart Poettering, to avoid name clashes, and
3 * unexport a few functions. */
4
5 #include "lookup3.h"
6
7 #if HAVE_VALGRIND_VALGRIND_H
8 # include <valgrind/valgrind.h>
9 #else
10 # define RUNNING_ON_VALGRIND 0
11 #endif
12
13 /*
14 -------------------------------------------------------------------------------
15 lookup3.c, by Bob Jenkins, May 2006, Public Domain.
16
17 These are functions for producing 32-bit hashes for hash table lookup.
18 hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
19 are externally useful functions. Routines to test the hash are included
20 if SELF_TEST is defined. You can use this free for any purpose. It's in
21 the public domain. It has no warranty.
22
23 You probably want to use hashlittle(). hashlittle() and hashbig()
24 hash byte arrays. hashlittle() is faster than hashbig() on
25 little-endian machines. Intel and AMD are little-endian machines.
26 On second thought, you probably want hashlittle2(), which is identical to
27 hashlittle() except it returns two 32-bit hashes for the price of one.
28 You could implement hashbig2() if you wanted but I haven't bothered here.
29
30 If you want to find a hash of, say, exactly 7 integers, do
31 a = i1; b = i2; c = i3;
32 mix(a,b,c);
33 a += i4; b += i5; c += i6;
34 mix(a,b,c);
35 a += i7;
36 final(a,b,c);
37 then use c as the hash value. If you have a variable length array of
38 4-byte integers to hash, use hashword(). If you have a byte array (like
39 a character string), use hashlittle(). If you have several byte arrays, or
40 a mix of things, see the comments above hashlittle().
41
42 Why is this so big? I read 12 bytes at a time into 3 4-byte integers,
43 then mix those integers. This is fast (you can do a lot more thorough
44 mixing with 12*3 instructions on 3 integers than you can with 3 instructions
45 on 1 byte), but shoehorning those bytes into integers efficiently is messy.
46 -------------------------------------------------------------------------------
47 */
48 /* #define SELF_TEST 1 */
49
50 #include <endian.h> /* attempt to define endianness */
51 #include <stdint.h> /* defines uint32_t etc */
52 #include <stdio.h> /* defines printf for tests */
53 #include <time.h> /* defines time_t for timings in the test */
54
55 _Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"")
56
57 /*
58 * My best guess at if you are big-endian or little-endian. This may
59 * need adjustment.
60 */
61 #if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
62 __BYTE_ORDER == __LITTLE_ENDIAN) || \
63 (defined(i386) || defined(__i386__) || defined(__i486__) || \
64 defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL))
65 # define HASH_LITTLE_ENDIAN 1
66 # define HASH_BIG_ENDIAN 0
67 #elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
68 __BYTE_ORDER == __BIG_ENDIAN) || \
69 (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel))
70 # define HASH_LITTLE_ENDIAN 0
71 # define HASH_BIG_ENDIAN 1
72 #else
73 # define HASH_LITTLE_ENDIAN 0
74 # define HASH_BIG_ENDIAN 0
75 #endif
76
77 #define hashsize(n) ((uint32_t)1<<(n))
78 #define hashmask(n) (hashsize(n)-1)
79 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
80
81 /*
82 -------------------------------------------------------------------------------
83 mix -- mix 3 32-bit values reversibly.
84
85 This is reversible, so any information in (a,b,c) before mix() is
86 still in (a,b,c) after mix().
87
88 If four pairs of (a,b,c) inputs are run through mix(), or through
89 mix() in reverse, there are at least 32 bits of the output that
90 are sometimes the same for one pair and different for another pair.
91 This was tested for:
92 * pairs that differed by one bit, by two bits, in any combination
93 of top bits of (a,b,c), or in any combination of bottom bits of
94 (a,b,c).
95 * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
96 the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
97 is commonly produced by subtraction) look like a single 1-bit
98 difference.
99 * the base values were pseudorandom, all zero but one bit set, or
100 all zero plus a counter that starts at zero.
101
102 Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
103 satisfy this are
104 4 6 8 16 19 4
105 9 15 3 18 27 15
106 14 9 3 7 17 3
107 Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
108 for "differ" defined as + with a one-bit base and a two-bit delta. I
109 used http://burtleburtle.net/bob/hash/avalanche.html to choose
110 the operations, constants, and arrangements of the variables.
111
112 This does not achieve avalanche. There are input bits of (a,b,c)
113 that fail to affect some output bits of (a,b,c), especially of a. The
114 most thoroughly mixed value is c, but it doesn't really even achieve
115 avalanche in c.
116
117 This allows some parallelism. Read-after-writes are good at doubling
118 the number of bits affected, so the goal of mixing pulls in the opposite
119 direction as the goal of parallelism. I did what I could. Rotates
120 seem to cost as much as shifts on every machine I could lay my hands
121 on, and rotates are much kinder to the top and bottom bits, so I used
122 rotates.
123 -------------------------------------------------------------------------------
124 */
125 #define mix(a,b,c) \
126 { \
127 a -= c; a ^= rot(c, 4); c += b; \
128 b -= a; b ^= rot(a, 6); a += c; \
129 c -= b; c ^= rot(b, 8); b += a; \
130 a -= c; a ^= rot(c,16); c += b; \
131 b -= a; b ^= rot(a,19); a += c; \
132 c -= b; c ^= rot(b, 4); b += a; \
133 }
134
135 /*
136 -------------------------------------------------------------------------------
137 final -- final mixing of 3 32-bit values (a,b,c) into c
138
139 Pairs of (a,b,c) values differing in only a few bits will usually
140 produce values of c that look totally different. This was tested for
141 * pairs that differed by one bit, by two bits, in any combination
142 of top bits of (a,b,c), or in any combination of bottom bits of
143 (a,b,c).
144 * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
145 the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
146 is commonly produced by subtraction) look like a single 1-bit
147 difference.
148 * the base values were pseudorandom, all zero but one bit set, or
149 all zero plus a counter that starts at zero.
150
151 These constants passed:
152 14 11 25 16 4 14 24
153 12 14 25 16 4 14 24
154 and these came close:
155 4 8 15 26 3 22 24
156 10 8 15 26 3 22 24
157 11 8 15 26 3 22 24
158 -------------------------------------------------------------------------------
159 */
160 #define final(a,b,c) \
161 { \
162 c ^= b; c -= rot(b,14); \
163 a ^= c; a -= rot(c,11); \
164 b ^= a; b -= rot(a,25); \
165 c ^= b; c -= rot(b,16); \
166 a ^= c; a -= rot(c,4); \
167 b ^= a; b -= rot(a,14); \
168 c ^= b; c -= rot(b,24); \
169 }
170
171 /*
172 --------------------------------------------------------------------
173 This works on all machines. To be useful, it requires
174 -- that the key be an array of uint32_t's, and
175 -- that the length be the number of uint32_t's in the key
176
177 The function hashword() is identical to hashlittle() on little-endian
178 machines, and identical to hashbig() on big-endian machines,
179 except that the length has to be measured in uint32_ts rather than in
180 bytes. hashlittle() is more complicated than hashword() only because
181 hashlittle() has to dance around fitting the key bytes into registers.
182 --------------------------------------------------------------------
183 */
184 uint32_t jenkins_hashword(
185 const uint32_t *k, /* the key, an array of uint32_t values */
186 size_t length, /* the length of the key, in uint32_ts */
187 uint32_t initval) /* the previous hash, or an arbitrary value */
188 {
189 uint32_t a,b,c;
190
191 /* Set up the internal state */
192 a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval;
193
194 /*------------------------------------------------- handle most of the key */
195 while (length > 3)
196 {
197 a += k[0];
198 b += k[1];
199 c += k[2];
200 mix(a,b,c);
201 length -= 3;
202 k += 3;
203 }
204
205 /*------------------------------------------- handle the last 3 uint32_t's */
206 switch(length) /* all the case statements fall through */
207 {
208 case 3 : c+=k[2];
209 case 2 : b+=k[1];
210 case 1 : a+=k[0];
211 final(a,b,c);
212 case 0: /* case 0: nothing left to add */
213 break;
214 }
215 /*------------------------------------------------------ report the result */
216 return c;
217 }
218
219 /*
220 --------------------------------------------------------------------
221 hashword2() -- same as hashword(), but take two seeds and return two
222 32-bit values. pc and pb must both be nonnull, and *pc and *pb must
223 both be initialized with seeds. If you pass in (*pb)==0, the output
224 (*pc) will be the same as the return value from hashword().
225 --------------------------------------------------------------------
226 */
227 void jenkins_hashword2 (
228 const uint32_t *k, /* the key, an array of uint32_t values */
229 size_t length, /* the length of the key, in uint32_ts */
230 uint32_t *pc, /* IN: seed OUT: primary hash value */
231 uint32_t *pb) /* IN: more seed OUT: secondary hash value */
232 {
233 uint32_t a,b,c;
234
235 /* Set up the internal state */
236 a = b = c = 0xdeadbeef + ((uint32_t)(length<<2)) + *pc;
237 c += *pb;
238
239 /*------------------------------------------------- handle most of the key */
240 while (length > 3)
241 {
242 a += k[0];
243 b += k[1];
244 c += k[2];
245 mix(a,b,c);
246 length -= 3;
247 k += 3;
248 }
249
250 /*------------------------------------------- handle the last 3 uint32_t's */
251 switch(length) /* all the case statements fall through */
252 {
253 case 3 : c+=k[2];
254 case 2 : b+=k[1];
255 case 1 : a+=k[0];
256 final(a,b,c);
257 case 0: /* case 0: nothing left to add */
258 break;
259 }
260 /*------------------------------------------------------ report the result */
261 *pc=c; *pb=b;
262 }
263
264 /*
265 -------------------------------------------------------------------------------
266 hashlittle() -- hash a variable-length key into a 32-bit value
267 k : the key (the unaligned variable-length array of bytes)
268 length : the length of the key, counting by bytes
269 initval : can be any 4-byte value
270 Returns a 32-bit value. Every bit of the key affects every bit of
271 the return value. Two keys differing by one or two bits will have
272 totally different hash values.
273
274 The best hash table sizes are powers of 2. There is no need to do
275 mod a prime (mod is sooo slow!). If you need less than 32 bits,
276 use a bitmask. For example, if you need only 10 bits, do
277 h = (h & hashmask(10));
278 In which case, the hash table should have hashsize(10) elements.
279
280 If you are hashing n strings (uint8_t **)k, do it like this:
281 for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
282
283 By Bob Jenkins, 2006. [email protected]. You may use this
284 code any way you wish, private, educational, or commercial. It's free.
285
286 Use for hash table lookup, or anything where one collision in 2^^32 is
287 acceptable. Do NOT use for cryptographic purposes.
288 -------------------------------------------------------------------------------
289 */
290
291 uint32_t jenkins_hashlittle( const void *key, size_t length, uint32_t initval)
292 {
293 uint32_t a,b,c; /* internal state */
294 union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
295
296 /* Set up the internal state */
297 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
298
299 u.ptr = key;
300 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
301 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
302
303 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
304 while (length > 12)
305 {
306 a += k[0];
307 b += k[1];
308 c += k[2];
309 mix(a,b,c);
310 length -= 12;
311 k += 3;
312 }
313
314 /*----------------------------- handle the last (probably partial) block */
315 /*
316 * "k[2]&0xffffff" actually reads beyond the end of the string, but
317 * then masks off the part it's not allowed to read. Because the
318 * string is aligned, the masked-off tail is in the same word as the
319 * rest of the string. Every machine with memory protection I've seen
320 * does it on word boundaries, so is OK with this. But valgrind will
321 * still catch it and complain. The masking trick does make the hash
322 * noticeably faster for short strings (like English words).
323 */
324 #define VALGRIND_LIKE (_unlikely_(HAS_FEATURE_ADDRESS_SANITIZER || \
325 HAS_FEATURE_MEMORY_SANITIZER || \
326 RUNNING_ON_VALGRIND))
327
328 if (!VALGRIND_LIKE) {
329 switch(length)
330 {
331 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
332 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
333 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
334 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
335 case 8 : b+=k[1]; a+=k[0]; break;
336 case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
337 case 6 : b+=k[1]&0xffff; a+=k[0]; break;
338 case 5 : b+=k[1]&0xff; a+=k[0]; break;
339 case 4 : a+=k[0]; break;
340 case 3 : a+=k[0]&0xffffff; break;
341 case 2 : a+=k[0]&0xffff; break;
342 case 1 : a+=k[0]&0xff; break;
343 case 0 : return c; /* zero length strings require no mixing */
344 }
345 } else {
346 const uint8_t *k8 = (const uint8_t *) k;
347
348 switch(length)
349 {
350 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
351 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
352 case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
353 case 9 : c+=k8[8]; /* fall through */
354 case 8 : b+=k[1]; a+=k[0]; break;
355 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
356 case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
357 case 5 : b+=k8[4]; /* fall through */
358 case 4 : a+=k[0]; break;
359 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
360 case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
361 case 1 : a+=k8[0]; break;
362 case 0 : return c;
363 }
364 }
365
366 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
367 const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
368 const uint8_t *k8;
369
370 /*--------------- all but last block: aligned reads and different mixing */
371 while (length > 12)
372 {
373 a += k[0] + (((uint32_t)k[1])<<16);
374 b += k[2] + (((uint32_t)k[3])<<16);
375 c += k[4] + (((uint32_t)k[5])<<16);
376 mix(a,b,c);
377 length -= 12;
378 k += 6;
379 }
380
381 /*----------------------------- handle the last (probably partial) block */
382 k8 = (const uint8_t *)k;
383 switch(length)
384 {
385 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
386 b+=k[2]+(((uint32_t)k[3])<<16);
387 a+=k[0]+(((uint32_t)k[1])<<16);
388 break;
389 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
390 case 10: c+=k[4];
391 b+=k[2]+(((uint32_t)k[3])<<16);
392 a+=k[0]+(((uint32_t)k[1])<<16);
393 break;
394 case 9 : c+=k8[8]; /* fall through */
395 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
396 a+=k[0]+(((uint32_t)k[1])<<16);
397 break;
398 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
399 case 6 : b+=k[2];
400 a+=k[0]+(((uint32_t)k[1])<<16);
401 break;
402 case 5 : b+=k8[4]; /* fall through */
403 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
404 break;
405 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
406 case 2 : a+=k[0];
407 break;
408 case 1 : a+=k8[0];
409 break;
410 case 0 : return c; /* zero length requires no mixing */
411 }
412
413 } else { /* need to read the key one byte at a time */
414 const uint8_t *k = (const uint8_t *)key;
415
416 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
417 while (length > 12)
418 {
419 a += k[0];
420 a += ((uint32_t)k[1])<<8;
421 a += ((uint32_t)k[2])<<16;
422 a += ((uint32_t)k[3])<<24;
423 b += k[4];
424 b += ((uint32_t)k[5])<<8;
425 b += ((uint32_t)k[6])<<16;
426 b += ((uint32_t)k[7])<<24;
427 c += k[8];
428 c += ((uint32_t)k[9])<<8;
429 c += ((uint32_t)k[10])<<16;
430 c += ((uint32_t)k[11])<<24;
431 mix(a,b,c);
432 length -= 12;
433 k += 12;
434 }
435
436 /*-------------------------------- last block: affect all 32 bits of (c) */
437 switch(length) /* all the case statements fall through */
438 {
439 case 12: c+=((uint32_t)k[11])<<24;
440 case 11: c+=((uint32_t)k[10])<<16;
441 case 10: c+=((uint32_t)k[9])<<8;
442 case 9 : c+=k[8];
443 case 8 : b+=((uint32_t)k[7])<<24;
444 case 7 : b+=((uint32_t)k[6])<<16;
445 case 6 : b+=((uint32_t)k[5])<<8;
446 case 5 : b+=k[4];
447 case 4 : a+=((uint32_t)k[3])<<24;
448 case 3 : a+=((uint32_t)k[2])<<16;
449 case 2 : a+=((uint32_t)k[1])<<8;
450 case 1 : a+=k[0];
451 break;
452 case 0 : return c;
453 }
454 }
455
456 final(a,b,c);
457 return c;
458 }
459
460 /*
461 * hashlittle2: return 2 32-bit hash values
462 *
463 * This is identical to hashlittle(), except it returns two 32-bit hash
464 * values instead of just one. This is good enough for hash table
465 * lookup with 2^^64 buckets, or if you want a second hash if you're not
466 * happy with the first, or if you want a probably-unique 64-bit ID for
467 * the key. *pc is better mixed than *pb, so use *pc first. If you want
468 * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
469 */
470 void jenkins_hashlittle2(
471 const void *key, /* the key to hash */
472 size_t length, /* length of the key */
473 uint32_t *pc, /* IN: primary initval, OUT: primary hash */
474 uint32_t *pb) /* IN: secondary initval, OUT: secondary hash */
475 {
476 uint32_t a,b,c; /* internal state */
477 union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
478
479 /* Set up the internal state */
480 a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
481 c += *pb;
482
483 u.ptr = key;
484 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
485 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
486
487 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
488 while (length > 12)
489 {
490 a += k[0];
491 b += k[1];
492 c += k[2];
493 mix(a,b,c);
494 length -= 12;
495 k += 3;
496 }
497
498 /*----------------------------- handle the last (probably partial) block */
499 /*
500 * "k[2]&0xffffff" actually reads beyond the end of the string, but
501 * then masks off the part it's not allowed to read. Because the
502 * string is aligned, the masked-off tail is in the same word as the
503 * rest of the string. Every machine with memory protection I've seen
504 * does it on word boundaries, so is OK with this. But valgrind will
505 * still catch it and complain. The masking trick does make the hash
506 * noticeably faster for short strings (like English words).
507 */
508 if (!VALGRIND_LIKE) {
509 switch(length)
510 {
511 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
512 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
513 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
514 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
515 case 8 : b+=k[1]; a+=k[0]; break;
516 case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
517 case 6 : b+=k[1]&0xffff; a+=k[0]; break;
518 case 5 : b+=k[1]&0xff; a+=k[0]; break;
519 case 4 : a+=k[0]; break;
520 case 3 : a+=k[0]&0xffffff; break;
521 case 2 : a+=k[0]&0xffff; break;
522 case 1 : a+=k[0]&0xff; break;
523 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
524 }
525 } else {
526 const uint8_t *k8 = (const uint8_t *)k;
527
528 switch(length)
529 {
530 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
531 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
532 case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
533 case 9 : c+=k8[8]; /* fall through */
534 case 8 : b+=k[1]; a+=k[0]; break;
535 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
536 case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
537 case 5 : b+=k8[4]; /* fall through */
538 case 4 : a+=k[0]; break;
539 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
540 case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
541 case 1 : a+=k8[0]; break;
542 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
543 }
544 }
545
546 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
547 const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
548 const uint8_t *k8;
549
550 /*--------------- all but last block: aligned reads and different mixing */
551 while (length > 12)
552 {
553 a += k[0] + (((uint32_t)k[1])<<16);
554 b += k[2] + (((uint32_t)k[3])<<16);
555 c += k[4] + (((uint32_t)k[5])<<16);
556 mix(a,b,c);
557 length -= 12;
558 k += 6;
559 }
560
561 /*----------------------------- handle the last (probably partial) block */
562 k8 = (const uint8_t *)k;
563 switch(length)
564 {
565 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
566 b+=k[2]+(((uint32_t)k[3])<<16);
567 a+=k[0]+(((uint32_t)k[1])<<16);
568 break;
569 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
570 case 10: c+=k[4];
571 b+=k[2]+(((uint32_t)k[3])<<16);
572 a+=k[0]+(((uint32_t)k[1])<<16);
573 break;
574 case 9 : c+=k8[8]; /* fall through */
575 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
576 a+=k[0]+(((uint32_t)k[1])<<16);
577 break;
578 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
579 case 6 : b+=k[2];
580 a+=k[0]+(((uint32_t)k[1])<<16);
581 break;
582 case 5 : b+=k8[4]; /* fall through */
583 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
584 break;
585 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
586 case 2 : a+=k[0];
587 break;
588 case 1 : a+=k8[0];
589 break;
590 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
591 }
592
593 } else { /* need to read the key one byte at a time */
594 const uint8_t *k = (const uint8_t *)key;
595
596 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
597 while (length > 12)
598 {
599 a += k[0];
600 a += ((uint32_t)k[1])<<8;
601 a += ((uint32_t)k[2])<<16;
602 a += ((uint32_t)k[3])<<24;
603 b += k[4];
604 b += ((uint32_t)k[5])<<8;
605 b += ((uint32_t)k[6])<<16;
606 b += ((uint32_t)k[7])<<24;
607 c += k[8];
608 c += ((uint32_t)k[9])<<8;
609 c += ((uint32_t)k[10])<<16;
610 c += ((uint32_t)k[11])<<24;
611 mix(a,b,c);
612 length -= 12;
613 k += 12;
614 }
615
616 /*-------------------------------- last block: affect all 32 bits of (c) */
617 switch(length) /* all the case statements fall through */
618 {
619 case 12: c+=((uint32_t)k[11])<<24;
620 case 11: c+=((uint32_t)k[10])<<16;
621 case 10: c+=((uint32_t)k[9])<<8;
622 case 9 : c+=k[8];
623 case 8 : b+=((uint32_t)k[7])<<24;
624 case 7 : b+=((uint32_t)k[6])<<16;
625 case 6 : b+=((uint32_t)k[5])<<8;
626 case 5 : b+=k[4];
627 case 4 : a+=((uint32_t)k[3])<<24;
628 case 3 : a+=((uint32_t)k[2])<<16;
629 case 2 : a+=((uint32_t)k[1])<<8;
630 case 1 : a+=k[0];
631 break;
632 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
633 }
634 }
635
636 final(a,b,c);
637 *pc=c; *pb=b;
638 }
639
640 /*
641 * hashbig():
642 * This is the same as hashword() on big-endian machines. It is different
643 * from hashlittle() on all machines. hashbig() takes advantage of
644 * big-endian byte ordering.
645 */
646 uint32_t jenkins_hashbig( const void *key, size_t length, uint32_t initval)
647 {
648 uint32_t a,b,c;
649 union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
650
651 /* Set up the internal state */
652 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
653
654 u.ptr = key;
655 if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
656 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
657
658 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
659 while (length > 12)
660 {
661 a += k[0];
662 b += k[1];
663 c += k[2];
664 mix(a,b,c);
665 length -= 12;
666 k += 3;
667 }
668
669 /*----------------------------- handle the last (probably partial) block */
670 /*
671 * "k[2]<<8" actually reads beyond the end of the string, but
672 * then shifts out the part it's not allowed to read. Because the
673 * string is aligned, the illegal read is in the same word as the
674 * rest of the string. Every machine with memory protection I've seen
675 * does it on word boundaries, so is OK with this. But valgrind will
676 * still catch it and complain. The masking trick does make the hash
677 * noticeably faster for short strings (like English words).
678 */
679
680 if (!VALGRIND_LIKE) {
681 switch(length)
682 {
683 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
684 case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
685 case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
686 case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
687 case 8 : b+=k[1]; a+=k[0]; break;
688 case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
689 case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
690 case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
691 case 4 : a+=k[0]; break;
692 case 3 : a+=k[0]&0xffffff00; break;
693 case 2 : a+=k[0]&0xffff0000; break;
694 case 1 : a+=k[0]&0xff000000; break;
695 case 0 : return c; /* zero length strings require no mixing */
696 }
697 } else {
698 const uint8_t *k8 = (const uint8_t *)k;
699
700 switch(length) /* all the case statements fall through */
701 {
702 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
703 case 11: c+=((uint32_t)k8[10])<<8; /* fall through */
704 case 10: c+=((uint32_t)k8[9])<<16; /* fall through */
705 case 9 : c+=((uint32_t)k8[8])<<24; /* fall through */
706 case 8 : b+=k[1]; a+=k[0]; break;
707 case 7 : b+=((uint32_t)k8[6])<<8; /* fall through */
708 case 6 : b+=((uint32_t)k8[5])<<16; /* fall through */
709 case 5 : b+=((uint32_t)k8[4])<<24; /* fall through */
710 case 4 : a+=k[0]; break;
711 case 3 : a+=((uint32_t)k8[2])<<8; /* fall through */
712 case 2 : a+=((uint32_t)k8[1])<<16; /* fall through */
713 case 1 : a+=((uint32_t)k8[0])<<24; break;
714 case 0 : return c;
715 }
716 }
717
718 } else { /* need to read the key one byte at a time */
719 const uint8_t *k = (const uint8_t *)key;
720
721 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
722 while (length > 12)
723 {
724 a += ((uint32_t)k[0])<<24;
725 a += ((uint32_t)k[1])<<16;
726 a += ((uint32_t)k[2])<<8;
727 a += ((uint32_t)k[3]);
728 b += ((uint32_t)k[4])<<24;
729 b += ((uint32_t)k[5])<<16;
730 b += ((uint32_t)k[6])<<8;
731 b += ((uint32_t)k[7]);
732 c += ((uint32_t)k[8])<<24;
733 c += ((uint32_t)k[9])<<16;
734 c += ((uint32_t)k[10])<<8;
735 c += ((uint32_t)k[11]);
736 mix(a,b,c);
737 length -= 12;
738 k += 12;
739 }
740
741 /*-------------------------------- last block: affect all 32 bits of (c) */
742 switch(length) /* all the case statements fall through */
743 {
744 case 12: c+=k[11];
745 case 11: c+=((uint32_t)k[10])<<8;
746 case 10: c+=((uint32_t)k[9])<<16;
747 case 9 : c+=((uint32_t)k[8])<<24;
748 case 8 : b+=k[7];
749 case 7 : b+=((uint32_t)k[6])<<8;
750 case 6 : b+=((uint32_t)k[5])<<16;
751 case 5 : b+=((uint32_t)k[4])<<24;
752 case 4 : a+=k[3];
753 case 3 : a+=((uint32_t)k[2])<<8;
754 case 2 : a+=((uint32_t)k[1])<<16;
755 case 1 : a+=((uint32_t)k[0])<<24;
756 break;
757 case 0 : return c;
758 }
759 }
760
761 final(a,b,c);
762 return c;
763 }
764
765 #ifdef SELF_TEST
766
767 /* used for timings */
768 void driver1()
769 {
770 uint8_t buf[256];
771 uint32_t i;
772 uint32_t h=0;
773 time_t a,z;
774
775 time(&a);
776 for (i=0; i<256; ++i) buf[i] = 'x';
777 for (i=0; i<1; ++i)
778 {
779 h = hashlittle(&buf[0],1,h);
780 }
781 time(&z);
782 if (z-a > 0) printf("time %d %.8x\n", z-a, h);
783 }
784
785 /* check that every input bit changes every output bit half the time */
786 #define HASHSTATE 1
787 #define HASHLEN 1
788 #define MAXPAIR 60
789 #define MAXLEN 70
790 void driver2()
791 {
792 uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1];
793 uint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l, m=0, z;
794 uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE];
795 uint32_t x[HASHSTATE],y[HASHSTATE];
796 uint32_t hlen;
797
798 printf("No more than %d trials should ever be needed \n",MAXPAIR/2);
799 for (hlen=0; hlen < MAXLEN; ++hlen)
800 {
801 z=0;
802 for (i=0; i<hlen; ++i) /*----------------------- for each input byte, */
803 {
804 for (j=0; j<8; ++j) /*------------------------ for each input bit, */
805 {
806 for (m=1; m<8; ++m) /*------------- for several possible initvals, */
807 {
808 for (l=0; l<HASHSTATE; ++l)
809 e[l]=f[l]=g[l]=h[l]=x[l]=y[l]=~((uint32_t)0);
810
811 /*---- check that every output bit is affected by that input bit */
812 for (k=0; k<MAXPAIR; k+=2)
813 {
814 uint32_t finished=1;
815 /* keys have one bit different */
816 for (l=0; l<hlen+1; ++l) {a[l] = b[l] = (uint8_t)0;}
817 /* have a and b be two keys differing in only one bit */
818 a[i] ^= (k<<j);
819 a[i] ^= (k>>(8-j));
820 c[0] = hashlittle(a, hlen, m);
821 b[i] ^= ((k+1)<<j);
822 b[i] ^= ((k+1)>>(8-j));
823 d[0] = hashlittle(b, hlen, m);
824 /* check every bit is 1, 0, set, and not set at least once */
825 for (l=0; l<HASHSTATE; ++l)
826 {
827 e[l] &= (c[l]^d[l]);
828 f[l] &= ~(c[l]^d[l]);
829 g[l] &= c[l];
830 h[l] &= ~c[l];
831 x[l] &= d[l];
832 y[l] &= ~d[l];
833 if (e[l]|f[l]|g[l]|h[l]|x[l]|y[l]) finished=0;
834 }
835 if (finished) break;
836 }
837 if (k>z) z=k;
838 if (k==MAXPAIR)
839 {
840 printf("Some bit didn't change: ");
841 printf("%.8x %.8x %.8x %.8x %.8x %.8x ",
842 e[0],f[0],g[0],h[0],x[0],y[0]);
843 printf("i %d j %d m %d len %d\n", i, j, m, hlen);
844 }
845 if (z==MAXPAIR) goto done;
846 }
847 }
848 }
849 done:
850 if (z < MAXPAIR)
851 {
852 printf("Mix success %2d bytes %2d initvals ",i,m);
853 printf("required %d trials\n", z/2);
854 }
855 }
856 printf("\n");
857 }
858
859 /* Check for reading beyond the end of the buffer and alignment problems */
860 void driver3()
861 {
862 uint8_t buf[MAXLEN+20], *b;
863 uint32_t len;
864 uint8_t q[] = "This is the time for all good men to come to the aid of their country...";
865 uint32_t h;
866 uint8_t qq[] = "xThis is the time for all good men to come to the aid of their country...";
867 uint32_t i;
868 uint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country...";
869 uint32_t j;
870 uint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country...";
871 uint32_t ref,x,y;
872 uint8_t *p;
873
874 printf("Endianness. These lines should all be the same (for values filled in):\n");
875 printf("%.8x %.8x %.8x\n",
876 hashword((const uint32_t *)q, (sizeof(q)-1)/4, 13),
877 hashword((const uint32_t *)q, (sizeof(q)-5)/4, 13),
878 hashword((const uint32_t *)q, (sizeof(q)-9)/4, 13));
879 p = q;
880 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
881 hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
882 hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
883 hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
884 hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
885 hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
886 hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
887 p = &qq[1];
888 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
889 hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
890 hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
891 hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
892 hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
893 hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
894 hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
895 p = &qqq[2];
896 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
897 hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
898 hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
899 hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
900 hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
901 hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
902 hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
903 p = &qqqq[3];
904 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
905 hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
906 hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
907 hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
908 hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
909 hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
910 hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
911 printf("\n");
912
913 /* check that hashlittle2 and hashlittle produce the same results */
914 i=47; j=0;
915 hashlittle2(q, sizeof(q), &i, &j);
916 if (hashlittle(q, sizeof(q), 47) != i)
917 printf("hashlittle2 and hashlittle mismatch\n");
918
919 /* check that hashword2 and hashword produce the same results */
920 len = 0xdeadbeef;
921 i=47, j=0;
922 hashword2(&len, 1, &i, &j);
923 if (hashword(&len, 1, 47) != i)
924 printf("hashword2 and hashword mismatch %x %x\n",
925 i, hashword(&len, 1, 47));
926
927 /* check hashlittle doesn't read before or after the ends of the string */
928 for (h=0, b=buf+1; h<8; ++h, ++b)
929 {
930 for (i=0; i<MAXLEN; ++i)
931 {
932 len = i;
933 for (j=0; j<i; ++j) *(b+j)=0;
934
935 /* these should all be equal */
936 ref = hashlittle(b, len, (uint32_t)1);
937 *(b+i)=(uint8_t)~0;
938 *(b-1)=(uint8_t)~0;
939 x = hashlittle(b, len, (uint32_t)1);
940 y = hashlittle(b, len, (uint32_t)1);
941 if ((ref != x) || (ref != y))
942 {
943 printf("alignment error: %.8x %.8x %.8x %d %d\n",ref,x,y,
944 h, i);
945 }
946 }
947 }
948 }
949
950 /* check for problems with nulls */
951 void driver4()
952 {
953 uint8_t buf[1];
954 uint32_t h,i,state[HASHSTATE];
955
956 buf[0] = ~0;
957 for (i=0; i<HASHSTATE; ++i) state[i] = 1;
958 printf("These should all be different\n");
959 for (i=0, h=0; i<8; ++i)
960 {
961 h = hashlittle(buf, 0, h);
962 printf("%2ld 0-byte strings, hash is %.8x\n", i, h);
963 }
964 }
965
966 void driver5()
967 {
968 uint32_t b,c;
969 b=0, c=0, hashlittle2("", 0, &c, &b);
970 printf("hash is %.8lx %.8lx\n", c, b); /* deadbeef deadbeef */
971 b=0xdeadbeef, c=0, hashlittle2("", 0, &c, &b);
972 printf("hash is %.8lx %.8lx\n", c, b); /* bd5b7dde deadbeef */
973 b=0xdeadbeef, c=0xdeadbeef, hashlittle2("", 0, &c, &b);
974 printf("hash is %.8lx %.8lx\n", c, b); /* 9c093ccd bd5b7dde */
975 b=0, c=0, hashlittle2("Four score and seven years ago", 30, &c, &b);
976 printf("hash is %.8lx %.8lx\n", c, b); /* 17770551 ce7226e6 */
977 b=1, c=0, hashlittle2("Four score and seven years ago", 30, &c, &b);
978 printf("hash is %.8lx %.8lx\n", c, b); /* e3607cae bd371de4 */
979 b=0, c=1, hashlittle2("Four score and seven years ago", 30, &c, &b);
980 printf("hash is %.8lx %.8lx\n", c, b); /* cd628161 6cbea4b3 */
981 c = hashlittle("Four score and seven years ago", 30, 0);
982 printf("hash is %.8lx\n", c); /* 17770551 */
983 c = hashlittle("Four score and seven years ago", 30, 1);
984 printf("hash is %.8lx\n", c); /* cd628161 */
985 }
986
987 int main()
988 {
989 driver1(); /* test that the key is hashed: used for timings */
990 driver2(); /* test that whole key is hashed thoroughly */
991 driver3(); /* test that nothing but the key is hashed */
992 driver4(); /* test hashing multiple buffers (all buffers are null) */
993 driver5(); /* test the hash against known vectors */
994 return 1;
995 }
996
997 #endif /* SELF_TEST */