Debian Codegraph @

hashtable.c

raw symbols SCIP
1 /*
2 * Routines to provide a memory-efficient hashtable.
3 *
4 * Copyright (C) 2007-2022 Wayne Davison
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, visit the http://fsf.org website.
18 */
19
20 #include "rsync.h"
21
22 #define HASH_LOAD_LIMIT(size) ((size)/4*3) /* /4 first: never overflows int */
23
24 struct hashtable *hashtable_create(int size, int key64)
25 {
26 int req = size;
27 struct hashtable *tbl;
28 int node_size = key64 ? sizeof (struct ht_int64_node)
29 : sizeof (struct ht_int32_node);
30
31 /* Pick a power of 2 that can hold the requested size. Test size < 16 first
32 * so a negative/zero req short-circuits before the size-1 (INT_MIN is UB). */
33 if (size < 16 || (size & (size-1))) {
34 size = 16;
35 while (size < req) {
36 if (size > INT_MAX/2) { /* the next doubling would overflow int */
37 rprintf(FERROR, "[%s] hashtable_create: requested size %d is too large\n",
38 who_am_i(), req);
39 exit_cleanup(RERR_MALLOC);
40 }
41 size *= 2;
42 }
43 }
44
45 tbl = new(struct hashtable);
46 /* Pass size and node_size as SEPARATE factors so my_alloc's overflow /
47 * --max-alloc guard sees both; computing size*node_size as int would wrap to
48 * a tiny count and under-allocate (heap overflow on later node access). */
49 tbl->nodes = my_alloc(do_calloc, size, node_size, __FILE__, __LINE__);
50 tbl->size = size;
51 tbl->entries = 0;
52 tbl->node_size = node_size;
53 tbl->key64 = key64 ? 1 : 0;
54
55 if (DEBUG_GTE(HASH, 1)) {
56 char buf[32];
57 if (req != size)
58 snprintf(buf, sizeof buf, "req: %d, ", req);
59 else
60 *buf = '\0';
61 rprintf(FINFO, "[%s] created hashtable %lx (%ssize: %d, keys: %d-bit)\n",
62 who_am_i(), (long)tbl, buf, size, key64 ? 64 : 32);
63 }
64
65 return tbl;
66 }
67
68 void hashtable_destroy(struct hashtable *tbl)
69 {
70 if (DEBUG_GTE(HASH, 1)) {
71 rprintf(FINFO, "[%s] destroyed hashtable %lx (size: %d, keys: %d-bit)\n",
72 who_am_i(), (long)tbl, tbl->size, tbl->key64 ? 64 : 32);
73 }
74 free(tbl->nodes);
75 free(tbl);
76 }
77
78 /* Returns the node that holds the indicated key if it exists. When it does not
79 * exist, it returns either NULL (when data_when_new is NULL), or it returns a
80 * new node with its node->data set to the indicated value.
81 *
82 * If your code doesn't know the data value for a new node in advance (usually
83 * because it doesn't know if a node is new or not) you should pass in a unique
84 * (non-0) value that you can use to check if the returned node is new. You can
85 * then overwrite the data with any value you want (even 0) since it only needs
86 * to be different than whatever data_when_new value you use later on.
87 *
88 * This return is a void* just because it might be pointing at a ht_int32_node
89 * or a ht_int64_node, and that makes the caller's assignment a little easier. */
90 void *hashtable_find(struct hashtable *tbl, int64 key, void *data_when_new)
91 {
92 int key64 = tbl->key64;
93 struct ht_int32_node *node;
94 uint32 ndx;
95
96 if (key64 ? key == 0 : (int32)key == 0) {
97 rprintf(FERROR, "Internal hashtable error: illegal key supplied!\n");
98 exit_cleanup(RERR_MESSAGEIO);
99 }
100
101 if (data_when_new && tbl->entries > HASH_LOAD_LIMIT(tbl->size)) {
102 void *old_nodes = tbl->nodes;
103 int size, i;
104
105 if (tbl->size > INT_MAX/2) { /* doubling would overflow int */
106 rprintf(FERROR, "[%s] hashtable grow: size overflow\n", who_am_i());
107 exit_cleanup(RERR_MALLOC);
108 }
109 size = tbl->size * 2;
110 /* Separate factors so my_alloc's guard sees both (see hashtable_create). */
111 tbl->nodes = my_alloc(do_calloc, size, tbl->node_size, __FILE__, __LINE__);
112 tbl->size = size;
113 tbl->entries = 0;
114
115 if (DEBUG_GTE(HASH, 1)) {
116 rprintf(FINFO, "[%s] growing hashtable %lx (size: %d, keys: %d-bit)\n",
117 who_am_i(), (long)tbl, size, key64 ? 64 : 32);
118 }
119
120 for (i = size / 2; i-- > 0; ) {
121 struct ht_int32_node *move_node = HT_NODE(tbl, old_nodes, i);
122 int64 move_key = HT_KEY(move_node, key64);
123 if (move_key == 0)
124 continue;
125 if (move_node->data)
126 hashtable_find(tbl, move_key, move_node->data);
127 else {
128 node = hashtable_find(tbl, move_key, "");
129 node->data = 0;
130 }
131 }
132
133 free(old_nodes);
134 }
135
136 if (!key64) {
137 /* Based on Jenkins One-at-a-time hash. */
138 uchar buf[4] = {0}, *keyp = buf; /* {0} only to satisfy the analyzer (SIVALu fills buf) */
139 int i;
140
141 SIVALu(buf, 0, key);
142 for (ndx = 0, i = 0; i < 4; i++) {
143 ndx += keyp[i];
144 ndx += (ndx << 10);
145 ndx ^= (ndx >> 6);
146 }
147 ndx += (ndx << 3);
148 ndx ^= (ndx >> 11);
149 ndx += (ndx << 15);
150 } else {
151 /* Based on Jenkins hashword() from lookup3.c. */
152 uint32 a, b, c;
153
154 /* Set up the internal state */
155 a = b = c = 0xdeadbeef + (8 << 2);
156
157 #define rot(x,k) (((x)<<(k)) ^ ((x)>>(32-(k))))
158 #if SIZEOF_INT64 >= 8
159 b += (uint32)(key >> 32);
160 #endif
161 a += (uint32)key;
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 #undef rot
170 ndx = c;
171 }
172
173 /* If it already exists, return the node. If we're not
174 * allocating, return NULL if the key is not found. */
175 while (1) {
176 int64 nkey;
177
178 ndx &= tbl->size - 1;
179 node = HT_NODE(tbl, tbl->nodes, ndx);
180 nkey = HT_KEY(node, key64);
181
182 if (nkey == key)
183 return node;
184 if (nkey == 0) {
185 if (!data_when_new)
186 return NULL;
187 break;
188 }
189 ndx++;
190 }
191
192 /* Take over this empty spot and then return the node. */
193 if (key64)
194 ((struct ht_int64_node*)node)->key = key;
195 else
196 node->key = (int32)key;
197 node->data = data_when_new;
198 tbl->entries++;
199 return node;
200 }
201
202 #ifndef WORDS_BIGENDIAN
203 # define HASH_LITTLE_ENDIAN 1
204 # define HASH_BIG_ENDIAN 0
205 #else
206 # define HASH_LITTLE_ENDIAN 0
207 # define HASH_BIG_ENDIAN 1
208 #endif
209
210 /*
211 -------------------------------------------------------------------------------
212 lookup3.c, by Bob Jenkins, May 2006, Public Domain.
213
214 These are functions for producing 32-bit hashes for hash table lookup.
215 hash_word(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
216 are externally useful functions. Routines to test the hash are included
217 if SELF_TEST is defined. You can use this free for any purpose. It's in
218 the public domain. It has no warranty.
219
220 You probably want to use hashlittle(). hashlittle() and hashbig()
221 hash byte arrays. hashlittle() is is faster than hashbig() on
222 little-endian machines. Intel and AMD are little-endian machines.
223 On second thought, you probably want hashlittle2(), which is identical to
224 hashlittle() except it returns two 32-bit hashes for the price of one.
225 You could implement hashbig2() if you wanted but I haven't bothered here.
226
227 If you want to find a hash of, say, exactly 7 integers, do
228 a = i1; b = i2; c = i3;
229 mix(a,b,c);
230 a += i4; b += i5; c += i6;
231 mix(a,b,c);
232 a += i7;
233 final(a,b,c);
234 then use c as the hash value. If you have a variable length array of
235 4-byte integers to hash, use hash_word(). If you have a byte array (like
236 a character string), use hashlittle(). If you have several byte arrays, or
237 a mix of things, see the comments above hashlittle().
238
239 Why is this so big? I read 12 bytes at a time into 3 4-byte integers,
240 then mix those integers. This is fast (you can do a lot more thorough
241 mixing with 12*3 instructions on 3 integers than you can with 3 instructions
242 on 1 byte), but shoehorning those bytes into integers efficiently is messy.
243 */
244
245 #define hashsize(n) ((uint32_t)1<<(n))
246 #define hashmask(n) (hashsize(n)-1)
247 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
248
249 /*
250 -------------------------------------------------------------------------------
251 mix -- mix 3 32-bit values reversibly.
252
253 This is reversible, so any information in (a,b,c) before mix() is
254 still in (a,b,c) after mix().
255
256 If four pairs of (a,b,c) inputs are run through mix(), or through
257 mix() in reverse, there are at least 32 bits of the output that
258 are sometimes the same for one pair and different for another pair.
259 This was tested for:
260 * pairs that differed by one bit, by two bits, in any combination
261 of top bits of (a,b,c), or in any combination of bottom bits of
262 (a,b,c).
263 * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
264 the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
265 is commonly produced by subtraction) look like a single 1-bit
266 difference.
267 * the base values were pseudorandom, all zero but one bit set, or
268 all zero plus a counter that starts at zero.
269
270 Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
271 satisfy this are
272 4 6 8 16 19 4
273 9 15 3 18 27 15
274 14 9 3 7 17 3
275 Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
276 for "differ" defined as + with a one-bit base and a two-bit delta. I
277 used http://burtleburtle.net/bob/hash/avalanche.html to choose
278 the operations, constants, and arrangements of the variables.
279
280 This does not achieve avalanche. There are input bits of (a,b,c)
281 that fail to affect some output bits of (a,b,c), especially of a. The
282 most thoroughly mixed value is c, but it doesn't really even achieve
283 avalanche in c.
284
285 This allows some parallelism. Read-after-writes are good at doubling
286 the number of bits affected, so the goal of mixing pulls in the opposite
287 direction as the goal of parallelism. I did what I could. Rotates
288 seem to cost as much as shifts on every machine I could lay my hands
289 on, and rotates are much kinder to the top and bottom bits, so I used
290 rotates.
291 -------------------------------------------------------------------------------
292 */
293 #define mix(a,b,c) \
294 { \
295 a -= c; a ^= rot(c, 4); c += b; \
296 b -= a; b ^= rot(a, 6); a += c; \
297 c -= b; c ^= rot(b, 8); b += a; \
298 a -= c; a ^= rot(c,16); c += b; \
299 b -= a; b ^= rot(a,19); a += c; \
300 c -= b; c ^= rot(b, 4); b += a; \
301 }
302
303 /*
304 -------------------------------------------------------------------------------
305 final -- final mixing of 3 32-bit values (a,b,c) into c
306
307 Pairs of (a,b,c) values differing in only a few bits will usually
308 produce values of c that look totally different. This was tested for
309 * pairs that differed by one bit, by two bits, in any combination
310 of top bits of (a,b,c), or in any combination of bottom bits of
311 (a,b,c).
312 * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
313 the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
314 is commonly produced by subtraction) look like a single 1-bit
315 difference.
316 * the base values were pseudorandom, all zero but one bit set, or
317 all zero plus a counter that starts at zero.
318
319 These constants passed:
320 14 11 25 16 4 14 24
321 12 14 25 16 4 14 24
322 and these came close:
323 4 8 15 26 3 22 24
324 10 8 15 26 3 22 24
325 11 8 15 26 3 22 24
326 -------------------------------------------------------------------------------
327 */
328 #define final(a,b,c) \
329 { \
330 c ^= b; c -= rot(b,14); \
331 a ^= c; a -= rot(c,11); \
332 b ^= a; b -= rot(a,25); \
333 c ^= b; c -= rot(b,16); \
334 a ^= c; a -= rot(c,4); \
335 b ^= a; b -= rot(a,14); \
336 c ^= b; c -= rot(b,24); \
337 }
338
339
340 /*
341 -------------------------------------------------------------------------------
342 hashlittle() -- hash a variable-length key into a 32-bit value
343 k : the key (the unaligned variable-length array of bytes)
344 length : the length of the key, counting by bytes
345 val2 : IN: can be any 4-byte value OUT: second 32 bit hash.
346 Returns a 32-bit value. Every bit of the key affects every bit of
347 the return value. Two keys differing by one or two bits will have
348 totally different hash values. Note that the return value is better
349 mixed than val2, so use that first.
350
351 The best hash table sizes are powers of 2. There is no need to do
352 mod a prime (mod is sooo slow!). If you need less than 32 bits,
353 use a bitmask. For example, if you need only 10 bits, do
354 h = (h & hashmask(10));
355 In which case, the hash table should have hashsize(10) elements.
356
357 If you are hashing n strings (uint8_t **)k, do it like this:
358 for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
359
360 By Bob Jenkins, 2006. [email protected]. You may use this
361 code any way you wish, private, educational, or commercial. It's free.
362
363 Use for hash table lookup, or anything where one collision in 2^^32 is
364 acceptable. Do NOT use for cryptographic purposes.
365 -------------------------------------------------------------------------------
366 */
367
368 #define NON_ZERO_32(x) ((x) ? (x) : (uint32_t)1)
369 #define NON_ZERO_64(x, y) ((x) || (y) ? (y) | (uint64_t)(x) << 32 | (y) : (int64)1)
370
371 uint32_t hashlittle(const void *key, size_t length)
372 {
373 uint32_t a,b,c; /* internal state */
374 union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
375
376 /* Set up the internal state */
377 a = b = c = 0xdeadbeef + ((uint32_t)length);
378
379 u.ptr = key;
380 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
381 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
382 const uint8_t *k8;
383
384 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
385 while (length > 12)
386 {
387 a += k[0];
388 b += k[1];
389 c += k[2];
390 mix(a,b,c);
391 length -= 12;
392 k += 3;
393 }
394
395 /*----------------------------- handle the last (probably partial) block */
396 k8 = (const uint8_t *)k;
397 switch(length)
398 {
399 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
400 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
401 case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
402 case 9 : c+=k8[8]; /* fall through */
403 case 8 : b+=k[1]; a+=k[0]; break;
404 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
405 case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
406 case 5 : b+=k8[4]; /* fall through */
407 case 4 : a+=k[0]; break;
408 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
409 case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
410 case 1 : a+=k8[0]; break;
411 case 0 : return NON_ZERO_32(c);
412 }
413 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
414 const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
415 const uint8_t *k8;
416
417 /*--------------- all but last block: aligned reads and different mixing */
418 while (length > 12)
419 {
420 a += k[0] + (((uint32_t)k[1])<<16);
421 b += k[2] + (((uint32_t)k[3])<<16);
422 c += k[4] + (((uint32_t)k[5])<<16);
423 mix(a,b,c);
424 length -= 12;
425 k += 6;
426 }
427
428 /*----------------------------- handle the last (probably partial) block */
429 k8 = (const uint8_t *)k;
430 switch(length)
431 {
432 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
433 b+=k[2]+(((uint32_t)k[3])<<16);
434 a+=k[0]+(((uint32_t)k[1])<<16);
435 break;
436 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
437 case 10: c+=k[4];
438 b+=k[2]+(((uint32_t)k[3])<<16);
439 a+=k[0]+(((uint32_t)k[1])<<16);
440 break;
441 case 9 : c+=k8[8]; /* fall through */
442 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
443 a+=k[0]+(((uint32_t)k[1])<<16);
444 break;
445 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
446 case 6 : b+=k[2];
447 a+=k[0]+(((uint32_t)k[1])<<16);
448 break;
449 case 5 : b+=k8[4]; /* fall through */
450 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
451 break;
452 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
453 case 2 : a+=k[0];
454 break;
455 case 1 : a+=k8[0];
456 break;
457 case 0 : return NON_ZERO_32(c); /* zero length requires no mixing */
458 }
459
460 } else { /* need to read the key one byte at a time */
461 const uint8_t *k = (const uint8_t *)key;
462
463 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
464 while (length > 12)
465 {
466 a += k[0];
467 a += ((uint32_t)k[1])<<8;
468 a += ((uint32_t)k[2])<<16;
469 a += ((uint32_t)k[3])<<24;
470 b += k[4];
471 b += ((uint32_t)k[5])<<8;
472 b += ((uint32_t)k[6])<<16;
473 b += ((uint32_t)k[7])<<24;
474 c += k[8];
475 c += ((uint32_t)k[9])<<8;
476 c += ((uint32_t)k[10])<<16;
477 c += ((uint32_t)k[11])<<24;
478 mix(a,b,c);
479 length -= 12;
480 k += 12;
481 }
482
483 /*-------------------------------- last block: affect all 32 bits of (c) */
484 switch(length) /* all the case statements fall through */
485 {
486 case 12: c+=((uint32_t)k[11])<<24;
487 /* FALLTHROUGH */
488 case 11: c+=((uint32_t)k[10])<<16;
489 /* FALLTHROUGH */
490 case 10: c+=((uint32_t)k[9])<<8;
491 /* FALLTHROUGH */
492 case 9 : c+=k[8];
493 /* FALLTHROUGH */
494 case 8 : b+=((uint32_t)k[7])<<24;
495 /* FALLTHROUGH */
496 case 7 : b+=((uint32_t)k[6])<<16;
497 /* FALLTHROUGH */
498 case 6 : b+=((uint32_t)k[5])<<8;
499 /* FALLTHROUGH */
500 case 5 : b+=k[4];
501 /* FALLTHROUGH */
502 case 4 : a+=((uint32_t)k[3])<<24;
503 /* FALLTHROUGH */
504 case 3 : a+=((uint32_t)k[2])<<16;
505 /* FALLTHROUGH */
506 case 2 : a+=((uint32_t)k[1])<<8;
507 /* FALLTHROUGH */
508 case 1 : a+=k[0];
509 break;
510 case 0 : return NON_ZERO_32(c);
511 }
512 }
513
514 final(a,b,c);
515 return NON_ZERO_32(c);
516 }
517
518 #if SIZEOF_INT64 >= 8
519 /*
520 * hashlittle2: return 2 32-bit hash values joined into an int64.
521 *
522 * This is identical to hashlittle(), except it returns two 32-bit hash
523 * values instead of just one. This is good enough for hash table
524 * lookup with 2^^64 buckets, or if you want a second hash if you're not
525 * happy with the first, or if you want a probably-unique 64-bit ID for
526 * the key. *pc is better mixed than *pb, so use *pc first. If you want
527 * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
528 */
529 int64 hashlittle2(const void *key, size_t length)
530 {
531 uint32_t a,b,c; /* internal state */
532 union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
533
534 /* Set up the internal state */
535 a = b = c = 0xdeadbeef + ((uint32_t)length);
536
537 u.ptr = key;
538 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
539 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
540 const uint8_t *k8;
541
542 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
543 while (length > 12)
544 {
545 a += k[0];
546 b += k[1];
547 c += k[2];
548 mix(a,b,c);
549 length -= 12;
550 k += 3;
551 }
552
553 /*----------------------------- handle the last (probably partial) block */
554 k8 = (const uint8_t *)k;
555 switch(length)
556 {
557 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
558 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
559 case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
560 case 9 : c+=k8[8]; /* fall through */
561 case 8 : b+=k[1]; a+=k[0]; break;
562 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
563 case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
564 case 5 : b+=k8[4]; /* fall through */
565 case 4 : a+=k[0]; break;
566 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
567 case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
568 case 1 : a+=k8[0]; break;
569 case 0 : return NON_ZERO_64(b, c);
570 }
571 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
572 const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
573 const uint8_t *k8;
574
575 /*--------------- all but last block: aligned reads and different mixing */
576 while (length > 12)
577 {
578 a += k[0] + (((uint32_t)k[1])<<16);
579 b += k[2] + (((uint32_t)k[3])<<16);
580 c += k[4] + (((uint32_t)k[5])<<16);
581 mix(a,b,c);
582 length -= 12;
583 k += 6;
584 }
585
586 /*----------------------------- handle the last (probably partial) block */
587 k8 = (const uint8_t *)k;
588 switch(length)
589 {
590 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
591 b+=k[2]+(((uint32_t)k[3])<<16);
592 a+=k[0]+(((uint32_t)k[1])<<16);
593 break;
594 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
595 case 10: c+=k[4];
596 b+=k[2]+(((uint32_t)k[3])<<16);
597 a+=k[0]+(((uint32_t)k[1])<<16);
598 break;
599 case 9 : c+=k8[8]; /* fall through */
600 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
601 a+=k[0]+(((uint32_t)k[1])<<16);
602 break;
603 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
604 case 6 : b+=k[2];
605 a+=k[0]+(((uint32_t)k[1])<<16);
606 break;
607 case 5 : b+=k8[4]; /* fall through */
608 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
609 break;
610 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
611 case 2 : a+=k[0];
612 break;
613 case 1 : a+=k8[0];
614 break;
615 case 0 : return NON_ZERO_64(b, c); /* zero length strings require no mixing */
616 }
617
618 } else { /* need to read the key one byte at a time */
619 const uint8_t *k = (const uint8_t *)key;
620
621 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
622 while (length > 12)
623 {
624 a += k[0];
625 a += ((uint32_t)k[1])<<8;
626 a += ((uint32_t)k[2])<<16;
627 a += ((uint32_t)k[3])<<24;
628 b += k[4];
629 b += ((uint32_t)k[5])<<8;
630 b += ((uint32_t)k[6])<<16;
631 b += ((uint32_t)k[7])<<24;
632 c += k[8];
633 c += ((uint32_t)k[9])<<8;
634 c += ((uint32_t)k[10])<<16;
635 c += ((uint32_t)k[11])<<24;
636 mix(a,b,c);
637 length -= 12;
638 k += 12;
639 }
640
641 /*-------------------------------- last block: affect all 32 bits of (c) */
642 switch(length) /* all the case statements fall through */
643 {
644 case 12: c+=((uint32_t)k[11])<<24;
645 /* FALLTHROUGH */
646 case 11: c+=((uint32_t)k[10])<<16;
647 /* FALLTHROUGH */
648 case 10: c+=((uint32_t)k[9])<<8;
649 /* FALLTHROUGH */
650 case 9 : c+=k[8];
651 /* FALLTHROUGH */
652 case 8 : b+=((uint32_t)k[7])<<24;
653 /* FALLTHROUGH */
654 case 7 : b+=((uint32_t)k[6])<<16;
655 /* FALLTHROUGH */
656 case 6 : b+=((uint32_t)k[5])<<8;
657 /* FALLTHROUGH */
658 case 5 : b+=k[4];
659 /* FALLTHROUGH */
660 case 4 : a+=((uint32_t)k[3])<<24;
661 /* FALLTHROUGH */
662 case 3 : a+=((uint32_t)k[2])<<16;
663 /* FALLTHROUGH */
664 case 2 : a+=((uint32_t)k[1])<<8;
665 /* FALLTHROUGH */
666 case 1 : a+=k[0];
667 break;
668 case 0 : return NON_ZERO_64(b, c);
669 }
670 }
671
672 final(a,b,c);
673 return NON_ZERO_64(b, c);
674 }
675 #else
676 #define hashlittle2(key, len) hashlittle(key, len)
677 #endif