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