87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/* Slightly modified by Lennart Poettering, to avoid name clashes, and
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * unexport a few functions. */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include "lookup3.h"
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/*
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering-------------------------------------------------------------------------------
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringlookup3.c, by Bob Jenkins, May 2006, Public Domain.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringThese are functions for producing 32-bit hashes for hash table lookup.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringhashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringare externally useful functions. Routines to test the hash are included
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringif SELF_TEST is defined. You can use this free for any purpose. It's in
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringthe public domain. It has no warranty.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringYou probably want to use hashlittle(). hashlittle() and hashbig()
c53158818d8cdaf46b3f1b5299b9bda118a1043fThomas Hindoe Paaboel Andersenhash byte arrays. hashlittle() is faster than hashbig() on
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringlittle-endian machines. Intel and AMD are little-endian machines.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringOn second thought, you probably want hashlittle2(), which is identical to
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringhashlittle() except it returns two 32-bit hashes for the price of one.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringYou could implement hashbig2() if you wanted but I haven't bothered here.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringIf you want to find a hash of, say, exactly 7 integers, do
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a = i1; b = i2; c = i3;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering mix(a,b,c);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += i4; b += i5; c += i6;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering mix(a,b,c);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += i7;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering final(a,b,c);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringthen use c as the hash value. If you have a variable length array of
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering4-byte integers to hash, use hashword(). If you have a byte array (like
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringa character string), use hashlittle(). If you have several byte arrays, or
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringa mix of things, see the comments above hashlittle().
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringWhy is this so big? I read 12 bytes at a time into 3 4-byte integers,
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringthen mix those integers. This is fast (you can do a lot more thorough
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringmixing with 12*3 instructions on 3 integers than you can with 3 instructions
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringon 1 byte), but shoehorning those bytes into integers efficiently is messy.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering-------------------------------------------------------------------------------
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering*/
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/* #define SELF_TEST 1 */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include <stdint.h> /* defines uint32_t etc */
cf0fbc49e67b55f8d346fc94de28c90113505297Thomas Hindoe Paaboel Andersen#include <stdio.h> /* defines printf for tests */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include <sys/param.h> /* attempt to define endianness */
cf0fbc49e67b55f8d346fc94de28c90113505297Thomas Hindoe Paaboel Andersen#include <time.h> /* defines time_t for timings in the test */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#ifdef linux
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering# include <endian.h> /* attempt to define endianness */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#endif
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/*
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * My best guess at if you are big-endian or little-endian. This may
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * need adjustment.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering __BYTE_ORDER == __LITTLE_ENDIAN) || \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering (defined(i386) || defined(__i386__) || defined(__i486__) || \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL))
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering# define HASH_LITTLE_ENDIAN 1
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering# define HASH_BIG_ENDIAN 0
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering __BYTE_ORDER == __BIG_ENDIAN) || \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel))
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering# define HASH_LITTLE_ENDIAN 0
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering# define HASH_BIG_ENDIAN 1
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#else
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering# define HASH_LITTLE_ENDIAN 0
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering# define HASH_BIG_ENDIAN 0
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#endif
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#define hashsize(n) ((uint32_t)1<<(n))
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#define hashmask(n) (hashsize(n)-1)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/*
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering-------------------------------------------------------------------------------
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringmix -- mix 3 32-bit values reversibly.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringThis is reversible, so any information in (a,b,c) before mix() is
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringstill in (a,b,c) after mix().
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringIf four pairs of (a,b,c) inputs are run through mix(), or through
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringmix() in reverse, there are at least 32 bits of the output that
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringare sometimes the same for one pair and different for another pair.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringThis was tested for:
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering* pairs that differed by one bit, by two bits, in any combination
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering of top bits of (a,b,c), or in any combination of bottom bits of
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering (a,b,c).
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering is commonly produced by subtraction) look like a single 1-bit
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering difference.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering* the base values were pseudorandom, all zero but one bit set, or
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering all zero plus a counter that starts at zero.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringSome k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringsatisfy this are
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering 4 6 8 16 19 4
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering 9 15 3 18 27 15
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering 14 9 3 7 17 3
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringWell, "9 15 3 18 27 15" didn't quite get 32 bits diffing
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringfor "differ" defined as + with a one-bit base and a two-bit delta. I
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringused http://burtleburtle.net/bob/hash/avalanche.html to choose
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringthe operations, constants, and arrangements of the variables.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringThis does not achieve avalanche. There are input bits of (a,b,c)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringthat fail to affect some output bits of (a,b,c), especially of a. The
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringmost thoroughly mixed value is c, but it doesn't really even achieve
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringavalanche in c.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringThis allows some parallelism. Read-after-writes are good at doubling
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringthe number of bits affected, so the goal of mixing pulls in the opposite
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringdirection as the goal of parallelism. I did what I could. Rotates
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringseem to cost as much as shifts on every machine I could lay my hands
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringon, and rotates are much kinder to the top and bottom bits, so I used
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringrotates.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering-------------------------------------------------------------------------------
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering*/
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#define mix(a,b,c) \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering{ \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a -= c; a ^= rot(c, 4); c += b; \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b -= a; b ^= rot(a, 6); a += c; \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c -= b; c ^= rot(b, 8); b += a; \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a -= c; a ^= rot(c,16); c += b; \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b -= a; b ^= rot(a,19); a += c; \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c -= b; c ^= rot(b, 4); b += a; \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/*
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering-------------------------------------------------------------------------------
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringfinal -- final mixing of 3 32-bit values (a,b,c) into c
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringPairs of (a,b,c) values differing in only a few bits will usually
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringproduce values of c that look totally different. This was tested for
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering* pairs that differed by one bit, by two bits, in any combination
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering of top bits of (a,b,c), or in any combination of bottom bits of
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering (a,b,c).
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering is commonly produced by subtraction) look like a single 1-bit
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering difference.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering* the base values were pseudorandom, all zero but one bit set, or
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering all zero plus a counter that starts at zero.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringThese constants passed:
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering 14 11 25 16 4 14 24
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering 12 14 25 16 4 14 24
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringand these came close:
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering 4 8 15 26 3 22 24
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering 10 8 15 26 3 22 24
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering 11 8 15 26 3 22 24
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering-------------------------------------------------------------------------------
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering*/
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#define final(a,b,c) \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering{ \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c ^= b; c -= rot(b,14); \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a ^= c; a -= rot(c,11); \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b ^= a; b -= rot(a,25); \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c ^= b; c -= rot(b,16); \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a ^= c; a -= rot(c,4); \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b ^= a; b -= rot(a,14); \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c ^= b; c -= rot(b,24); \
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/*
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering--------------------------------------------------------------------
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering This works on all machines. To be useful, it requires
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering -- that the key be an array of uint32_t's, and
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering -- that the length be the number of uint32_t's in the key
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering The function hashword() is identical to hashlittle() on little-endian
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering machines, and identical to hashbig() on big-endian machines,
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering except that the length has to be measured in uint32_ts rather than in
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering bytes. hashlittle() is more complicated than hashword() only because
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle() has to dance around fitting the key bytes into registers.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering--------------------------------------------------------------------
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering*/
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringuint32_t jenkins_hashword(
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringconst uint32_t *k, /* the key, an array of uint32_t values */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringsize_t length, /* the length of the key, in uint32_ts */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringuint32_t initval) /* the previous hash, or an arbitrary value */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering{
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t a,b,c;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* Set up the internal state */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*------------------------------------------------- handle most of the key */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering while (length > 3)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += k[0];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += k[1];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += k[2];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering mix(a,b,c);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering length -= 3;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering k += 3;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*------------------------------------------- handle the last 3 uint32_t's */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering switch(length) /* all the case statements fall through */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 3 : c+=k[2];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 2 : b+=k[1];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 1 : a+=k[0];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering final(a,b,c);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 0: /* case 0: nothing left to add */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*------------------------------------------------------ report the result */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return c;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/*
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering--------------------------------------------------------------------
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringhashword2() -- same as hashword(), but take two seeds and return two
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering32-bit values. pc and pb must both be nonnull, and *pc and *pb must
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringboth be initialized with seeds. If you pass in (*pb)==0, the output
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering(*pc) will be the same as the return value from hashword().
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering--------------------------------------------------------------------
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering*/
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringvoid jenkins_hashword2 (
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringconst uint32_t *k, /* the key, an array of uint32_t values */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringsize_t length, /* the length of the key, in uint32_ts */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringuint32_t *pc, /* IN: seed OUT: primary hash value */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringuint32_t *pb) /* IN: more seed OUT: secondary hash value */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering{
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t a,b,c;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* Set up the internal state */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a = b = c = 0xdeadbeef + ((uint32_t)(length<<2)) + *pc;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += *pb;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*------------------------------------------------- handle most of the key */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering while (length > 3)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += k[0];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += k[1];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += k[2];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering mix(a,b,c);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering length -= 3;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering k += 3;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*------------------------------------------- handle the last 3 uint32_t's */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering switch(length) /* all the case statements fall through */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 3 : c+=k[2];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 2 : b+=k[1];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 1 : a+=k[0];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering final(a,b,c);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 0: /* case 0: nothing left to add */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*------------------------------------------------------ report the result */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering *pc=c; *pb=b;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/*
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering-------------------------------------------------------------------------------
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringhashlittle() -- hash a variable-length key into a 32-bit value
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering k : the key (the unaligned variable-length array of bytes)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering length : the length of the key, counting by bytes
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering initval : can be any 4-byte value
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringReturns a 32-bit value. Every bit of the key affects every bit of
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringthe return value. Two keys differing by one or two bits will have
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringtotally different hash values.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringThe best hash table sizes are powers of 2. There is no need to do
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringmod a prime (mod is sooo slow!). If you need less than 32 bits,
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringuse a bitmask. For example, if you need only 10 bits, do
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering h = (h & hashmask(10));
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringIn which case, the hash table should have hashsize(10) elements.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringIf you are hashing n strings (uint8_t **)k, do it like this:
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringBy Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringcode any way you wish, private, educational, or commercial. It's free.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart PoetteringUse for hash table lookup, or anything where one collision in 2^^32 is
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringacceptable. Do NOT use for cryptographic purposes.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering-------------------------------------------------------------------------------
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering*/
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringuint32_t jenkins_hashlittle( const void *key, size_t length, uint32_t initval)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering{
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t a,b,c; /* internal state */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* Set up the internal state */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering u.ptr = key;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering while (length > 12)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += k[0];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += k[1];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += k[2];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering mix(a,b,c);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering length -= 12;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering k += 3;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*----------------------------- handle the last (probably partial) block */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * "k[2]&0xffffff" actually reads beyond the end of the string, but
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * then masks off the part it's not allowed to read. Because the
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * string is aligned, the masked-off tail is in the same word as the
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * rest of the string. Every machine with memory protection I've seen
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * does it on word boundaries, so is OK with this. But VALGRIND will
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * still catch it and complain. The masking trick does make the hash
49f43d5f91a99b23f745726aa351d8f159774357Ville Skyttä * noticeably faster for short strings (like English words).
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#ifndef VALGRIND
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering switch(length)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 8 : b+=k[1]; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 6 : b+=k[1]&0xffff; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 5 : b+=k[1]&0xff; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 4 : a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 3 : a+=k[0]&0xffffff; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 2 : a+=k[0]&0xffff; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 1 : a+=k[0]&0xff; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 0 : return c; /* zero length strings require no mixing */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#else /* make valgrind happy */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek const uint8_t *k8 = (const uint8_t *) k;
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek switch(length)
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek {
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 9 : c+=k8[8]; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 8 : b+=k[1]; a+=k[0]; break;
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 5 : b+=k8[4]; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 4 : a+=k[0]; break;
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 1 : a+=k8[0]; break;
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 0 : return c;
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#endif /* !valgrind */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering const uint8_t *k8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*--------------- all but last block: aligned reads and different mixing */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering while (length > 12)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += k[0] + (((uint32_t)k[1])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += k[2] + (((uint32_t)k[3])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += k[4] + (((uint32_t)k[5])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering mix(a,b,c);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering length -= 12;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering k += 6;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*----------------------------- handle the last (probably partial) block */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering k8 = (const uint8_t *)k;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering switch(length)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 12: c+=k[4]+(((uint32_t)k[5])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b+=k[2]+(((uint32_t)k[3])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a+=k[0]+(((uint32_t)k[1])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 10: c+=k[4];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b+=k[2]+(((uint32_t)k[3])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a+=k[0]+(((uint32_t)k[1])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 9 : c+=k8[8]; /* fall through */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a+=k[0]+(((uint32_t)k[1])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 6 : b+=k[2];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a+=k[0]+(((uint32_t)k[1])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 5 : b+=k8[4]; /* fall through */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 2 : a+=k[0];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 1 : a+=k8[0];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 0 : return c; /* zero length requires no mixing */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering } else { /* need to read the key one byte at a time */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering const uint8_t *k = (const uint8_t *)key;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering while (length > 12)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += k[0];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += ((uint32_t)k[1])<<8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += ((uint32_t)k[2])<<16;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += ((uint32_t)k[3])<<24;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += k[4];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += ((uint32_t)k[5])<<8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += ((uint32_t)k[6])<<16;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += ((uint32_t)k[7])<<24;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += k[8];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += ((uint32_t)k[9])<<8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += ((uint32_t)k[10])<<16;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += ((uint32_t)k[11])<<24;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering mix(a,b,c);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering length -= 12;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering k += 12;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*-------------------------------- last block: affect all 32 bits of (c) */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering switch(length) /* all the case statements fall through */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 12: c+=((uint32_t)k[11])<<24;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 11: c+=((uint32_t)k[10])<<16;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 10: c+=((uint32_t)k[9])<<8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 9 : c+=k[8];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 8 : b+=((uint32_t)k[7])<<24;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 7 : b+=((uint32_t)k[6])<<16;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 6 : b+=((uint32_t)k[5])<<8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 5 : b+=k[4];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 4 : a+=((uint32_t)k[3])<<24;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 3 : a+=((uint32_t)k[2])<<16;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 2 : a+=((uint32_t)k[1])<<8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 1 : a+=k[0];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 0 : return c;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering final(a,b,c);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return c;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/*
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * hashlittle2: return 2 32-bit hash values
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering *
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * This is identical to hashlittle(), except it returns two 32-bit hash
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * values instead of just one. This is good enough for hash table
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * lookup with 2^^64 buckets, or if you want a second hash if you're not
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * happy with the first, or if you want a probably-unique 64-bit ID for
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * the key. *pc is better mixed than *pb, so use *pc first. If you want
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringvoid jenkins_hashlittle2(
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering const void *key, /* the key to hash */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering size_t length, /* length of the key */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t *pc, /* IN: primary initval, OUT: primary hash */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t *pb) /* IN: secondary initval, OUT: secondary hash */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering{
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t a,b,c; /* internal state */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* Set up the internal state */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += *pb;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering u.ptr = key;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering while (length > 12)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += k[0];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += k[1];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += k[2];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering mix(a,b,c);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering length -= 12;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering k += 3;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*----------------------------- handle the last (probably partial) block */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * "k[2]&0xffffff" actually reads beyond the end of the string, but
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * then masks off the part it's not allowed to read. Because the
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * string is aligned, the masked-off tail is in the same word as the
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * rest of the string. Every machine with memory protection I've seen
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * does it on word boundaries, so is OK with this. But VALGRIND will
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * still catch it and complain. The masking trick does make the hash
49f43d5f91a99b23f745726aa351d8f159774357Ville Skyttä * noticeably faster for short strings (like English words).
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#ifndef VALGRIND
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering switch(length)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 8 : b+=k[1]; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 6 : b+=k[1]&0xffff; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 5 : b+=k[1]&0xff; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 4 : a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 3 : a+=k[0]&0xffffff; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 2 : a+=k[0]&0xffff; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 1 : a+=k[0]&0xff; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#else /* make valgrind happy */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek const uint8_t *k8 = (const uint8_t *)k;
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek switch(length)
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek {
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 9 : c+=k8[8]; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 8 : b+=k[1]; a+=k[0]; break;
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 5 : b+=k8[4]; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 4 : a+=k[0]; break;
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 1 : a+=k8[0]; break;
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#endif /* !valgrind */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering const uint8_t *k8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*--------------- all but last block: aligned reads and different mixing */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering while (length > 12)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += k[0] + (((uint32_t)k[1])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += k[2] + (((uint32_t)k[3])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += k[4] + (((uint32_t)k[5])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering mix(a,b,c);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering length -= 12;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering k += 6;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*----------------------------- handle the last (probably partial) block */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering k8 = (const uint8_t *)k;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering switch(length)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 12: c+=k[4]+(((uint32_t)k[5])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b+=k[2]+(((uint32_t)k[3])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a+=k[0]+(((uint32_t)k[1])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 10: c+=k[4];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b+=k[2]+(((uint32_t)k[3])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a+=k[0]+(((uint32_t)k[1])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 9 : c+=k8[8]; /* fall through */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a+=k[0]+(((uint32_t)k[1])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 6 : b+=k[2];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a+=k[0]+(((uint32_t)k[1])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 5 : b+=k8[4]; /* fall through */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 2 : a+=k[0];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 1 : a+=k8[0];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering } else { /* need to read the key one byte at a time */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering const uint8_t *k = (const uint8_t *)key;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering while (length > 12)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += k[0];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += ((uint32_t)k[1])<<8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += ((uint32_t)k[2])<<16;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += ((uint32_t)k[3])<<24;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += k[4];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += ((uint32_t)k[5])<<8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += ((uint32_t)k[6])<<16;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += ((uint32_t)k[7])<<24;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += k[8];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += ((uint32_t)k[9])<<8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += ((uint32_t)k[10])<<16;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += ((uint32_t)k[11])<<24;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering mix(a,b,c);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering length -= 12;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering k += 12;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*-------------------------------- last block: affect all 32 bits of (c) */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering switch(length) /* all the case statements fall through */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 12: c+=((uint32_t)k[11])<<24;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 11: c+=((uint32_t)k[10])<<16;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 10: c+=((uint32_t)k[9])<<8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 9 : c+=k[8];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 8 : b+=((uint32_t)k[7])<<24;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 7 : b+=((uint32_t)k[6])<<16;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 6 : b+=((uint32_t)k[5])<<8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 5 : b+=k[4];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 4 : a+=((uint32_t)k[3])<<24;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 3 : a+=((uint32_t)k[2])<<16;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 2 : a+=((uint32_t)k[1])<<8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 1 : a+=k[0];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering final(a,b,c);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering *pc=c; *pb=b;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/*
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * hashbig():
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * This is the same as hashword() on big-endian machines. It is different
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * from hashlittle() on all machines. hashbig() takes advantage of
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * big-endian byte ordering.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringuint32_t jenkins_hashbig( const void *key, size_t length, uint32_t initval)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering{
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t a,b,c;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* Set up the internal state */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering u.ptr = key;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering while (length > 12)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += k[0];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += k[1];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += k[2];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering mix(a,b,c);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering length -= 12;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering k += 3;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*----------------------------- handle the last (probably partial) block */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * "k[2]<<8" actually reads beyond the end of the string, but
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * then shifts out the part it's not allowed to read. Because the
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * string is aligned, the illegal read is in the same word as the
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * rest of the string. Every machine with memory protection I've seen
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * does it on word boundaries, so is OK with this. But VALGRIND will
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering * still catch it and complain. The masking trick does make the hash
49f43d5f91a99b23f745726aa351d8f159774357Ville Skyttä * noticeably faster for short strings (like English words).
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#ifndef VALGRIND
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering switch(length)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 8 : b+=k[1]; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 4 : a+=k[0]; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 3 : a+=k[0]&0xffffff00; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 2 : a+=k[0]&0xffff0000; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 1 : a+=k[0]&0xff000000; break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 0 : return c; /* zero length strings require no mixing */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#else /* make valgrind happy */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek const uint8_t *k8 = (const uint8_t *)k;
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek switch(length) /* all the case statements fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek {
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 11: c+=((uint32_t)k8[10])<<8; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 10: c+=((uint32_t)k8[9])<<16; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 9 : c+=((uint32_t)k8[8])<<24; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 8 : b+=k[1]; a+=k[0]; break;
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 7 : b+=((uint32_t)k8[6])<<8; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 6 : b+=((uint32_t)k8[5])<<16; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 5 : b+=((uint32_t)k8[4])<<24; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 4 : a+=k[0]; break;
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 3 : a+=((uint32_t)k8[2])<<8; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 2 : a+=((uint32_t)k8[1])<<16; /* fall through */
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 1 : a+=((uint32_t)k8[0])<<24; break;
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek case 0 : return c;
1b4bb4fdac4dce4e658aa3743153d77c04d1a331Zbigniew Jędrzejewski-Szmek }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#endif /* !VALGRIND */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering } else { /* need to read the key one byte at a time */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering const uint8_t *k = (const uint8_t *)key;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering while (length > 12)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += ((uint32_t)k[0])<<24;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += ((uint32_t)k[1])<<16;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += ((uint32_t)k[2])<<8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a += ((uint32_t)k[3]);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += ((uint32_t)k[4])<<24;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += ((uint32_t)k[5])<<16;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += ((uint32_t)k[6])<<8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b += ((uint32_t)k[7]);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += ((uint32_t)k[8])<<24;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += ((uint32_t)k[9])<<16;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += ((uint32_t)k[10])<<8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c += ((uint32_t)k[11]);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering mix(a,b,c);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering length -= 12;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering k += 12;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*-------------------------------- last block: affect all 32 bits of (c) */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering switch(length) /* all the case statements fall through */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 12: c+=k[11];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 11: c+=((uint32_t)k[10])<<8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 10: c+=((uint32_t)k[9])<<16;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 9 : c+=((uint32_t)k[8])<<24;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 8 : b+=k[7];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 7 : b+=((uint32_t)k[6])<<8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 6 : b+=((uint32_t)k[5])<<16;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 5 : b+=((uint32_t)k[4])<<24;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 4 : a+=k[3];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 3 : a+=((uint32_t)k[2])<<8;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 2 : a+=((uint32_t)k[1])<<16;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 1 : a+=((uint32_t)k[0])<<24;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering case 0 : return c;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering final(a,b,c);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return c;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#ifdef SELF_TEST
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/* used for timings */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringvoid driver1()
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering{
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint8_t buf[256];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t i;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t h=0;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering time_t a,z;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering time(&a);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (i=0; i<256; ++i) buf[i] = 'x';
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (i=0; i<1; ++i)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering h = hashlittle(&buf[0],1,h);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering time(&z);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (z-a > 0) printf("time %d %.8x\n", z-a, h);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/* check that every input bit changes every output bit half the time */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#define HASHSTATE 1
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#define HASHLEN 1
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#define MAXPAIR 60
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#define MAXLEN 70
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringvoid driver2()
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering{
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l, m=0, z;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t x[HASHSTATE],y[HASHSTATE];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t hlen;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("No more than %d trials should ever be needed \n",MAXPAIR/2);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (hlen=0; hlen < MAXLEN; ++hlen)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering z=0;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (i=0; i<hlen; ++i) /*----------------------- for each input byte, */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (j=0; j<8; ++j) /*------------------------ for each input bit, */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (m=1; m<8; ++m) /*------------ for serveral possible initvals, */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (l=0; l<HASHSTATE; ++l)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering e[l]=f[l]=g[l]=h[l]=x[l]=y[l]=~((uint32_t)0);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /*---- check that every output bit is affected by that input bit */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (k=0; k<MAXPAIR; k+=2)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t finished=1;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* keys have one bit different */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (l=0; l<hlen+1; ++l) {a[l] = b[l] = (uint8_t)0;}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* have a and b be two keys differing in only one bit */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a[i] ^= (k<<j);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering a[i] ^= (k>>(8-j));
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c[0] = hashlittle(a, hlen, m);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b[i] ^= ((k+1)<<j);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b[i] ^= ((k+1)>>(8-j));
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering d[0] = hashlittle(b, hlen, m);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* check every bit is 1, 0, set, and not set at least once */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (l=0; l<HASHSTATE; ++l)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering e[l] &= (c[l]^d[l]);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering f[l] &= ~(c[l]^d[l]);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering g[l] &= c[l];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering h[l] &= ~c[l];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering x[l] &= d[l];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering y[l] &= ~d[l];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (e[l]|f[l]|g[l]|h[l]|x[l]|y[l]) finished=0;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (finished) break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (k>z) z=k;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (k==MAXPAIR)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("Some bit didn't change: ");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("%.8x %.8x %.8x %.8x %.8x %.8x ",
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering e[0],f[0],g[0],h[0],x[0],y[0]);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("i %d j %d m %d len %d\n", i, j, m, hlen);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (z==MAXPAIR) goto done;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering done:
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (z < MAXPAIR)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("Mix success %2d bytes %2d initvals ",i,m);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("required %d trials\n", z/2);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("\n");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/* Check for reading beyond the end of the buffer and alignment problems */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringvoid driver3()
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering{
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint8_t buf[MAXLEN+20], *b;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t len;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint8_t q[] = "This is the time for all good men to come to the aid of their country...";
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t h;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint8_t qq[] = "xThis is the time for all good men to come to the aid of their country...";
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t i;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country...";
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t j;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country...";
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t ref,x,y;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint8_t *p;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("Endianness. These lines should all be the same (for values filled in):\n");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("%.8x %.8x %.8x\n",
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashword((const uint32_t *)q, (sizeof(q)-1)/4, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashword((const uint32_t *)q, (sizeof(q)-5)/4, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashword((const uint32_t *)q, (sizeof(q)-9)/4, 13));
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering p = q;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering p = &qq[1];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering p = &qqq[2];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering p = &qqqq[3];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("\n");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* check that hashlittle2 and hashlittle produce the same results */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering i=47; j=0;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashlittle2(q, sizeof(q), &i, &j);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (hashlittle(q, sizeof(q), 47) != i)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("hashlittle2 and hashlittle mismatch\n");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* check that hashword2 and hashword produce the same results */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering len = 0xdeadbeef;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering i=47, j=0;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashword2(&len, 1, &i, &j);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (hashword(&len, 1, 47) != i)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("hashword2 and hashword mismatch %x %x\n",
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering i, hashword(&len, 1, 47));
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* check hashlittle doesn't read before or after the ends of the string */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (h=0, b=buf+1; h<8; ++h, ++b)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (i=0; i<MAXLEN; ++i)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering len = i;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (j=0; j<i; ++j) *(b+j)=0;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* these should all be equal */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering ref = hashlittle(b, len, (uint32_t)1);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering *(b+i)=(uint8_t)~0;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering *(b-1)=(uint8_t)~0;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering x = hashlittle(b, len, (uint32_t)1);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering y = hashlittle(b, len, (uint32_t)1);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if ((ref != x) || (ref != y))
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("alignment error: %.8x %.8x %.8x %d %d\n",ref,x,y,
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering h, i);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/* check for problems with nulls */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering void driver4()
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering{
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint8_t buf[1];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t h,i,state[HASHSTATE];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering buf[0] = ~0;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (i=0; i<HASHSTATE; ++i) state[i] = 1;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("These should all be different\n");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (i=0, h=0; i<8; ++i)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering h = hashlittle(buf, 0, h);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("%2ld 0-byte strings, hash is %.8x\n", i, h);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringvoid driver5()
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering{
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint32_t b,c;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b=0, c=0, hashlittle2("", 0, &c, &b);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("hash is %.8lx %.8lx\n", c, b); /* deadbeef deadbeef */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b=0xdeadbeef, c=0, hashlittle2("", 0, &c, &b);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("hash is %.8lx %.8lx\n", c, b); /* bd5b7dde deadbeef */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b=0xdeadbeef, c=0xdeadbeef, hashlittle2("", 0, &c, &b);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("hash is %.8lx %.8lx\n", c, b); /* 9c093ccd bd5b7dde */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b=0, c=0, hashlittle2("Four score and seven years ago", 30, &c, &b);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("hash is %.8lx %.8lx\n", c, b); /* 17770551 ce7226e6 */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b=1, c=0, hashlittle2("Four score and seven years ago", 30, &c, &b);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("hash is %.8lx %.8lx\n", c, b); /* e3607cae bd371de4 */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering b=0, c=1, hashlittle2("Four score and seven years ago", 30, &c, &b);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("hash is %.8lx %.8lx\n", c, b); /* cd628161 6cbea4b3 */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c = hashlittle("Four score and seven years ago", 30, 0);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("hash is %.8lx\n", c); /* 17770551 */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering c = hashlittle("Four score and seven years ago", 30, 1);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering printf("hash is %.8lx\n", c); /* cd628161 */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringint main()
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering{
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering driver1(); /* test that the key is hashed: used for timings */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering driver2(); /* test that whole key is hashed thoroughly */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering driver3(); /* test that nothing but the key is hashed */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering driver4(); /* test hashing multiple buffers (all buffers are null) */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering driver5(); /* test the hash against known vectors */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return 1;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#endif /* SELF_TEST */