LCOV - code coverage report
Current view: top level - journal - lookup3.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 64 273 23.4 %
Date: 2019-08-22 15:41:25 Functions: 1 5 20.0 %

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

Generated by: LCOV version 1.14