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