Debian Codegraph @

crt/aws-c-common/include/aws/common/private/lookup3.inl

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