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