LCOV - code coverage report
Current view: top level - src/plugins/ni/nickel-1.1.0/src - hash.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 102 117 87.2 %
Date: 2019-09-12 12:28:41 Functions: 2 2 100.0 %

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

Generated by: LCOV version 1.13