[77] | 1 | /* |
---|
| 2 | ------------------------------------------------------------------------------- |
---|
| 3 | lookup3.c, by Bob Jenkins, May 2006, Public Domain. |
---|
| 4 | These are functions for producing 32-bit hashes for hash table lookup. |
---|
| 5 | hashword(), hashlittle(), hashbig(), mix(), and final() are externally |
---|
| 6 | useful functions. Routines to test the hash are included if SELF_TEST |
---|
| 7 | is defined. You can use this free for any purpose. It has no warranty. |
---|
| 8 | |
---|
| 9 | You probably want to use hashlittle(). hashlittle() and hashbig() |
---|
| 10 | hash byte arrays. hashlittle() is is faster than hashbig() on |
---|
| 11 | little-endian machines. Intel and AMD are little-endian machines. |
---|
| 12 | |
---|
| 13 | If you want to find a hash of, say, exactly 7 integers, do |
---|
| 14 | a = i1; b = i2; c = i3; |
---|
| 15 | mix(a,b,c); |
---|
| 16 | a += i4; b += i5; c += i6; |
---|
| 17 | mix(a,b,c); |
---|
| 18 | a += i7; |
---|
| 19 | final(a,b,c); |
---|
| 20 | then use c as the hash value. If you have a variable length array of |
---|
| 21 | 4-byte integers to hash, use hashword(). If you have a byte array (like |
---|
| 22 | a character string), use hashlittle(). If you have several byte arrays, or |
---|
| 23 | a mix of things, see the comments above hashlittle(). |
---|
| 24 | |
---|
| 25 | Code changed for ANSI C compilers and autotools by Åukasz CieÅnik. |
---|
| 26 | ------------------------------------------------------------------------------- |
---|
| 27 | */ |
---|
| 28 | #ifdef HAVE_CONFIG_H |
---|
| 29 | # include <config.h> |
---|
| 30 | #endif |
---|
| 31 | |
---|
| 32 | #include <drmaa_utils/lookup3.h> |
---|
| 33 | |
---|
| 34 | #ifdef SELF_TEST |
---|
| 35 | #include <stdio.h> |
---|
| 36 | #include <time.h> |
---|
| 37 | #include <stdlib.h> |
---|
| 38 | #endif |
---|
| 39 | |
---|
| 40 | #ifndef lint |
---|
| 41 | static char rcsid[] |
---|
| 42 | # ifdef __GNUC__ |
---|
| 43 | __attribute__ ((unused)) |
---|
| 44 | # endif |
---|
| 45 | = "$Id: lookup3.c 13 2011-04-20 15:41:43Z mmamonski $"; |
---|
| 46 | #endif |
---|
| 47 | |
---|
| 48 | typedef uint32_t uint32; /* unsigned 4-byte quantities */ |
---|
| 49 | typedef uint16_t uint16; /* unsigned 2-byte quantities */ |
---|
| 50 | typedef uint8_t uint8; /* unsigned 1-byte quantities */ |
---|
| 51 | |
---|
| 52 | /* do not make any assumptions about the endianess - safer and prevents from reading not initialized memory */ |
---|
| 53 | # define HASH_LITTLE_ENDIAN 0 |
---|
| 54 | # define HASH_BIG_ENDIAN 0 |
---|
| 55 | |
---|
| 56 | #define hashsize(n) ((uint32)1<<(n)) |
---|
| 57 | #define hashmask(n) (hashsize(n)-1) |
---|
| 58 | #define rot(x,k) (((x)<<(k)) ^ ((x)>>(32-(k)))) |
---|
| 59 | |
---|
| 60 | /* |
---|
| 61 | ------------------------------------------------------------------------------- |
---|
| 62 | mix -- mix 3 32-bit values reversibly. |
---|
| 63 | |
---|
| 64 | This is reversible, so any information in (a,b,c) before mix() is |
---|
| 65 | still in (a,b,c) after mix(). |
---|
| 66 | |
---|
| 67 | If four pairs of (a,b,c) inputs are run through mix(), or through |
---|
| 68 | mix() in reverse, there are at least 32 bits of the output that |
---|
| 69 | are sometimes the same for one pair and different for another pair. |
---|
| 70 | This was tested for: |
---|
| 71 | * pairs that differed by one bit, by two bits, in any combination |
---|
| 72 | of top bits of (a,b,c), or in any combination of bottom bits of |
---|
| 73 | (a,b,c). |
---|
| 74 | * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed |
---|
| 75 | the output delta to a Gray code (a^(a>>1)) so a string of 1's (as |
---|
| 76 | is commonly produced by subtraction) look like a single 1-bit |
---|
| 77 | difference. |
---|
| 78 | * the base values were pseudorandom, all zero but one bit set, or |
---|
| 79 | all zero plus a counter that starts at zero. |
---|
| 80 | |
---|
| 81 | Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that |
---|
| 82 | satisfy this are |
---|
| 83 | 4 6 8 16 19 4 |
---|
| 84 | 9 15 3 18 27 15 |
---|
| 85 | 14 9 3 7 17 3 |
---|
| 86 | Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing |
---|
| 87 | for "differ" defined as + with a one-bit base and a two-bit delta. I |
---|
| 88 | used http://burtleburtle.net/bob/hash/avalanche.html to choose |
---|
| 89 | the operations, constants, and arrangements of the variables. |
---|
| 90 | |
---|
| 91 | This does not achieve avalanche. There are input bits of (a,b,c) |
---|
| 92 | that fail to affect some output bits of (a,b,c), especially of a. The |
---|
| 93 | most thoroughly mixed value is c, but it doesn't really even achieve |
---|
| 94 | avalanche in c. |
---|
| 95 | |
---|
| 96 | This allows some parallelism. Read-after-writes are good at doubling |
---|
| 97 | the number of bits affected, so the goal of mixing pulls in the opposite |
---|
| 98 | direction as the goal of parallelism. I did what I could. Rotates |
---|
| 99 | seem to cost as much as shifts on every machine I could lay my hands |
---|
| 100 | on, and rotates are much kinder to the top and bottom bits, so I used |
---|
| 101 | rotates. |
---|
| 102 | ------------------------------------------------------------------------------- |
---|
| 103 | */ |
---|
| 104 | #define mix(a,b,c) \ |
---|
| 105 | { \ |
---|
| 106 | a -= c; a ^= rot(c, 4); c += b; \ |
---|
| 107 | b -= a; b ^= rot(a, 6); a += c; \ |
---|
| 108 | c -= b; c ^= rot(b, 8); b += a; \ |
---|
| 109 | a -= c; a ^= rot(c,16); c += b; \ |
---|
| 110 | b -= a; b ^= rot(a,19); a += c; \ |
---|
| 111 | c -= b; c ^= rot(b, 4); b += a; \ |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | /* |
---|
| 115 | ------------------------------------------------------------------------------- |
---|
| 116 | final -- final mixing of 3 32-bit values (a,b,c) into c |
---|
| 117 | |
---|
| 118 | Pairs of (a,b,c) values differing in only a few bits will usually |
---|
| 119 | produce values of c that look totally different. This was tested for |
---|
| 120 | * pairs that differed by one bit, by two bits, in any combination |
---|
| 121 | of top bits of (a,b,c), or in any combination of bottom bits of |
---|
| 122 | (a,b,c). |
---|
| 123 | * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed |
---|
| 124 | the output delta to a Gray code (a^(a>>1)) so a string of 1's (as |
---|
| 125 | is commonly produced by subtraction) look like a single 1-bit |
---|
| 126 | difference. |
---|
| 127 | * the base values were pseudorandom, all zero but one bit set, or |
---|
| 128 | all zero plus a counter that starts at zero. |
---|
| 129 | |
---|
| 130 | These constants passed: |
---|
| 131 | 14 11 25 16 4 14 24 |
---|
| 132 | 12 14 25 16 4 14 24 |
---|
| 133 | and these came close: |
---|
| 134 | 4 8 15 26 3 22 24 |
---|
| 135 | 10 8 15 26 3 22 24 |
---|
| 136 | 11 8 15 26 3 22 24 |
---|
| 137 | ------------------------------------------------------------------------------- |
---|
| 138 | */ |
---|
| 139 | #define final(a,b,c) \ |
---|
| 140 | { \ |
---|
| 141 | c ^= b; c -= rot(b,14); \ |
---|
| 142 | a ^= c; a -= rot(c,11); \ |
---|
| 143 | b ^= a; b -= rot(a,25); \ |
---|
| 144 | c ^= b; c -= rot(b,16); \ |
---|
| 145 | a ^= c; a -= rot(c,4); \ |
---|
| 146 | b ^= a; b -= rot(a,14); \ |
---|
| 147 | c ^= b; c -= rot(b,24); \ |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /** |
---|
| 151 | * This works on all machines. To be useful, it requires |
---|
| 152 | * -- that the key be an array of uint32's, and |
---|
| 153 | * -- that all your machines have the same endianness, and |
---|
| 154 | * -- that the length be the number of uint32's in the key |
---|
| 155 | |
---|
| 156 | * The function hashword() is identical to hashlittle() on little-endian |
---|
| 157 | * machines, and identical to hashbig() on big-endian machines, |
---|
| 158 | * except that the length has to be measured in uint32s rather than in |
---|
| 159 | * bytes. hashlittle() is more complicated than hashword() only because |
---|
| 160 | * hashlittle() has to dance around fitting the key bytes into registers. |
---|
| 161 | * |
---|
| 162 | * @param k the key, an array of uint32 values |
---|
| 163 | * @param length the length of the key, in uint32s |
---|
| 164 | * @param initval the previous hash, or an arbitrary value |
---|
| 165 | */ |
---|
| 166 | uint32 hashword( const uint32 *k, size_t length, uint32 initval ) |
---|
| 167 | { |
---|
| 168 | uint32 a,b,c; |
---|
| 169 | |
---|
| 170 | /* Set up the internal state */ |
---|
| 171 | a = b = c = 0xdeadbeef + (((uint32)length)<<2) + initval; |
---|
| 172 | |
---|
| 173 | /*------------------------------------------------- handle most of the key */ |
---|
| 174 | while (length > 3) |
---|
| 175 | { |
---|
| 176 | a += k[0]; |
---|
| 177 | b += k[1]; |
---|
| 178 | c += k[2]; |
---|
| 179 | mix(a,b,c); |
---|
| 180 | length -= 3; |
---|
| 181 | k += 3; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | /*--------------------------------------------- handle the last 3 uint32's */ |
---|
| 185 | switch(length) /* all the case statements fall through */ |
---|
| 186 | { |
---|
| 187 | case 3 : c+=k[2]; |
---|
| 188 | case 2 : b+=k[1]; |
---|
| 189 | case 1 : a+=k[0]; |
---|
| 190 | final(a,b,c); |
---|
| 191 | case 0: /* case 0: nothing left to add */ |
---|
| 192 | break; |
---|
| 193 | } |
---|
| 194 | /*------------------------------------------------------ report the result */ |
---|
| 195 | return c; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | |
---|
| 199 | /** |
---|
| 200 | * hashlittle() -- hash a variable-length key into a 32-bit value |
---|
| 201 | * @param k the key (the unaligned variable-length array of bytes) |
---|
| 202 | * @param length the length of the key, counting by bytes |
---|
| 203 | * @param initval can be any 4-byte value |
---|
| 204 | * |
---|
| 205 | * Returns a 32-bit value. Every bit of the key affects every bit of |
---|
| 206 | * the return value. Two keys differing by one or two bits will have |
---|
| 207 | * totally different hash values. |
---|
| 208 | * |
---|
| 209 | * The best hash table sizes are powers of 2. There is no need to do |
---|
| 210 | * mod a prime (mod is sooo slow!). If you need less than 32 bits, |
---|
| 211 | * use a bitmask. For example, if you need only 10 bits, do |
---|
| 212 | * h = (h & hashmask(10)); |
---|
| 213 | * In which case, the hash table should have hashsize(10) elements. |
---|
| 214 | * |
---|
| 215 | * If you are hashing n strings (uint8 **)k, do it like this: |
---|
| 216 | * for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h); |
---|
| 217 | * |
---|
| 218 | * By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this |
---|
| 219 | * code any way you wish, private, educational, or commercial. It's free. |
---|
| 220 | * |
---|
| 221 | * Use for hash table lookup, or anything where one collision in 2^^32 is |
---|
| 222 | * acceptable. Do NOT use for cryptographic purposes. |
---|
| 223 | */ |
---|
| 224 | uint32 hashlittle( const void *key, size_t length, uint32 initval ) |
---|
| 225 | { |
---|
| 226 | uint32 a,b,c; |
---|
| 227 | |
---|
| 228 | /* Set up the internal state */ |
---|
| 229 | a = b = c = 0xdeadbeef + ((uint32)length) + initval; |
---|
| 230 | |
---|
| 231 | if (HASH_LITTLE_ENDIAN && !((((uint8 *)key)-(uint8 *)0) & 0x3)) { |
---|
| 232 | const uint32 *k = key; /* read 32-bit chunks */ |
---|
| 233 | |
---|
| 234 | /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */ |
---|
| 235 | while (length > 12) |
---|
| 236 | { |
---|
| 237 | a += k[0]; |
---|
| 238 | b += k[1]; |
---|
| 239 | c += k[2]; |
---|
| 240 | mix(a,b,c); |
---|
| 241 | length -= 12; |
---|
| 242 | k += 3; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | /*----------------------------- handle the last (probably partial) block */ |
---|
| 246 | switch(length) |
---|
| 247 | { |
---|
| 248 | case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; |
---|
| 249 | case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break; |
---|
| 250 | case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break; |
---|
| 251 | case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break; |
---|
| 252 | case 8 : b+=k[1]; a+=k[0]; break; |
---|
| 253 | case 7 : b+=k[1]&0xffffff; a+=k[0]; break; |
---|
| 254 | case 6 : b+=k[1]&0xffff; a+=k[0]; break; |
---|
| 255 | case 5 : b+=k[1]&0xff; a+=k[0]; break; |
---|
| 256 | case 4 : a+=k[0]; break; |
---|
| 257 | case 3 : a+=k[0]&0xffffff; break; |
---|
| 258 | case 2 : a+=k[0]&0xffff; break; |
---|
| 259 | case 1 : a+=k[0]&0xff; break; |
---|
| 260 | case 0 : return c; /* zero length strings require no mixing */ |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | } else if (HASH_LITTLE_ENDIAN && !((((uint8 *)key)-(uint8 *)0) & 0x1)) { |
---|
| 264 | const uint16 *k = key; /* read 16-bit chunks */ |
---|
| 265 | |
---|
| 266 | /*--------------- all but last block: aligned reads and different mixing */ |
---|
| 267 | while (length > 12) |
---|
| 268 | { |
---|
| 269 | a += k[0] + (((uint32)k[1])<<16); |
---|
| 270 | b += k[2] + (((uint32)k[3])<<16); |
---|
| 271 | c += k[4] + (((uint32)k[5])<<16); |
---|
| 272 | mix(a,b,c); |
---|
| 273 | length -= 12; |
---|
| 274 | k += 6; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | /*----------------------------- handle the last (probably partial) block */ |
---|
| 278 | switch(length) |
---|
| 279 | { |
---|
| 280 | case 12: c+=k[4]+(((uint32)k[5])<<16); |
---|
| 281 | b+=k[2]+(((uint32)k[3])<<16); |
---|
| 282 | a+=k[0]+(((uint32)k[1])<<16); |
---|
| 283 | break; |
---|
| 284 | case 11: c+=((uint32)(k[5]&0xff))<<16;/* fall through */ |
---|
| 285 | case 10: c+=k[4]; |
---|
| 286 | b+=k[2]+(((uint32)k[3])<<16); |
---|
| 287 | a+=k[0]+(((uint32)k[1])<<16); |
---|
| 288 | break; |
---|
| 289 | case 9 : c+=k[4]&0xff; /* fall through */ |
---|
| 290 | case 8 : b+=k[2]+(((uint32)k[3])<<16); |
---|
| 291 | a+=k[0]+(((uint32)k[1])<<16); |
---|
| 292 | break; |
---|
| 293 | case 7 : b+=((uint32)(k[3]&0xff))<<16;/* fall through */ |
---|
| 294 | case 6 : b+=k[2]; |
---|
| 295 | a+=k[0]+(((uint32)k[1])<<16); |
---|
| 296 | break; |
---|
| 297 | case 5 : b+=k[2]&0xff; /* fall through */ |
---|
| 298 | case 4 : a+=k[0]+(((uint32)k[1])<<16); |
---|
| 299 | break; |
---|
| 300 | case 3 : a+=((uint32)(k[1]&0xff))<<16;/* fall through */ |
---|
| 301 | case 2 : a+=k[0]; |
---|
| 302 | break; |
---|
| 303 | case 1 : a+=k[0]&0xff; |
---|
| 304 | break; |
---|
| 305 | case 0 : return c; /* zero length requires no mixing */ |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | } else { /* need to read the key one byte at a time */ |
---|
| 309 | const uint8 *k = key; |
---|
| 310 | |
---|
| 311 | /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ |
---|
| 312 | while (length > 12) |
---|
| 313 | { |
---|
| 314 | a += k[0]; |
---|
| 315 | a += ((uint32)k[1])<<8; |
---|
| 316 | a += ((uint32)k[2])<<16; |
---|
| 317 | a += ((uint32)k[3])<<24; |
---|
| 318 | b += k[4]; |
---|
| 319 | b += ((uint32)k[5])<<8; |
---|
| 320 | b += ((uint32)k[6])<<16; |
---|
| 321 | b += ((uint32)k[7])<<24; |
---|
| 322 | c += k[8]; |
---|
| 323 | c += ((uint32)k[9])<<8; |
---|
| 324 | c += ((uint32)k[10])<<16; |
---|
| 325 | c += ((uint32)k[11])<<24; |
---|
| 326 | mix(a,b,c); |
---|
| 327 | length -= 12; |
---|
| 328 | k += 12; |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | /*-------------------------------- last block: affect all 32 bits of (c) */ |
---|
| 332 | switch(length) /* all the case statements fall through */ |
---|
| 333 | { |
---|
| 334 | case 12: c+=((uint32)k[11])<<24; |
---|
| 335 | case 11: c+=((uint32)k[10])<<16; |
---|
| 336 | case 10: c+=((uint32)k[9])<<8; |
---|
| 337 | case 9 : c+=k[8]; |
---|
| 338 | case 8 : b+=((uint32)k[7])<<24; |
---|
| 339 | case 7 : b+=((uint32)k[6])<<16; |
---|
| 340 | case 6 : b+=((uint32)k[5])<<8; |
---|
| 341 | case 5 : b+=k[4]; |
---|
| 342 | case 4 : a+=((uint32)k[3])<<24; |
---|
| 343 | case 3 : a+=((uint32)k[2])<<16; |
---|
| 344 | case 2 : a+=((uint32)k[1])<<8; |
---|
| 345 | case 1 : a+=k[0]; |
---|
| 346 | break; |
---|
| 347 | case 0 : return c; |
---|
| 348 | } |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | final(a,b,c); |
---|
| 352 | return c; |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | |
---|
| 356 | |
---|
| 357 | /** |
---|
| 358 | * hashbig(): |
---|
| 359 | * This is the same as hashword() on big-endian machines. It is different |
---|
| 360 | * from hashlittle() on all machines. hashbig() takes advantage of |
---|
| 361 | * big-endian byte ordering. |
---|
| 362 | */ |
---|
| 363 | uint32 hashbig( const void *key, size_t length, uint32 initval ) |
---|
| 364 | { |
---|
| 365 | uint32 a,b,c; |
---|
| 366 | |
---|
| 367 | /* Set up the internal state */ |
---|
| 368 | a = b = c = 0xdeadbeef + ((uint32)length) + initval; |
---|
| 369 | |
---|
| 370 | if (HASH_BIG_ENDIAN && !((((uint8 *)key)-(uint8 *)0) & 0x3)) { |
---|
| 371 | const uint32 *k = key; /* read 32-bit chunks */ |
---|
| 372 | |
---|
| 373 | /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */ |
---|
| 374 | while (length > 12) |
---|
| 375 | { |
---|
| 376 | a += k[0]; |
---|
| 377 | b += k[1]; |
---|
| 378 | c += k[2]; |
---|
| 379 | mix(a,b,c); |
---|
| 380 | length -= 12; |
---|
| 381 | k += 3; |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | /*----------------------------- handle the last (probably partial) block */ |
---|
| 385 | switch(length) |
---|
| 386 | { |
---|
| 387 | case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; |
---|
| 388 | case 11: c+=k[2]<<8; b+=k[1]; a+=k[0]; break; |
---|
| 389 | case 10: c+=k[2]<<16; b+=k[1]; a+=k[0]; break; |
---|
| 390 | case 9 : c+=k[2]<<24; b+=k[1]; a+=k[0]; break; |
---|
| 391 | case 8 : b+=k[1]; a+=k[0]; break; |
---|
| 392 | case 7 : b+=k[1]<<8; a+=k[0]; break; |
---|
| 393 | case 6 : b+=k[1]<<16; a+=k[0]; break; |
---|
| 394 | case 5 : b+=k[1]<<24; a+=k[0]; break; |
---|
| 395 | case 4 : a+=k[0]; break; |
---|
| 396 | case 3 : a+=k[0]<<8; break; |
---|
| 397 | case 2 : a+=k[0]<<16; break; |
---|
| 398 | case 1 : a+=k[0]<<24; break; |
---|
| 399 | case 0 : return c; /* zero length strings require no mixing */ |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | } else { /* need to read the key one byte at a time */ |
---|
| 403 | const uint8 *k = key; |
---|
| 404 | |
---|
| 405 | /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ |
---|
| 406 | while (length > 12) |
---|
| 407 | { |
---|
| 408 | a += ((uint32)k[0])<<24; |
---|
| 409 | a += ((uint32)k[1])<<16; |
---|
| 410 | a += ((uint32)k[2])<<8; |
---|
| 411 | a += ((uint32)k[3]); |
---|
| 412 | b += ((uint32)k[4])<<24; |
---|
| 413 | b += ((uint32)k[5])<<16; |
---|
| 414 | b += ((uint32)k[6])<<8; |
---|
| 415 | b += ((uint32)k[7]); |
---|
| 416 | c += ((uint32)k[8])<<24; |
---|
| 417 | c += ((uint32)k[9])<<16; |
---|
| 418 | c += ((uint32)k[10])<<8; |
---|
| 419 | c += ((uint32)k[11]); |
---|
| 420 | mix(a,b,c); |
---|
| 421 | length -= 12; |
---|
| 422 | k += 12; |
---|
| 423 | } |
---|
| 424 | |
---|
| 425 | /*-------------------------------- last block: affect all 32 bits of (c) */ |
---|
| 426 | switch(length) /* all the case statements fall through */ |
---|
| 427 | { |
---|
| 428 | case 12: c+=((uint32)k[11])<<24; |
---|
| 429 | case 11: c+=((uint32)k[10])<<16; |
---|
| 430 | case 10: c+=((uint32)k[9])<<8; |
---|
| 431 | case 9 : c+=k[8]; |
---|
| 432 | case 8 : b+=((uint32)k[7])<<24; |
---|
| 433 | case 7 : b+=((uint32)k[6])<<16; |
---|
| 434 | case 6 : b+=((uint32)k[5])<<8; |
---|
| 435 | case 5 : b+=k[4]; |
---|
| 436 | case 4 : a+=((uint32)k[3])<<24; |
---|
| 437 | case 3 : a+=((uint32)k[2])<<16; |
---|
| 438 | case 2 : a+=((uint32)k[1])<<8; |
---|
| 439 | case 1 : a+=k[0]; |
---|
| 440 | break; |
---|
| 441 | case 0 : return c; |
---|
| 442 | } |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | final(a,b,c); |
---|
| 446 | return c; |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | |
---|
| 450 | #ifdef SELF_TEST |
---|
| 451 | |
---|
| 452 | /* used for timings */ |
---|
| 453 | void driver1(void) |
---|
| 454 | { |
---|
| 455 | uint8 buf[256]; |
---|
| 456 | uint32 i; |
---|
| 457 | uint32 h=0; |
---|
| 458 | time_t a,z; |
---|
| 459 | |
---|
| 460 | time(&a); |
---|
| 461 | for (i=0; i<256; ++i) buf[i] = 'x'; |
---|
| 462 | for (i=0; i<1; ++i) |
---|
| 463 | { |
---|
| 464 | h = hashlittle(&buf[0],1,h); |
---|
| 465 | } |
---|
| 466 | time(&z); |
---|
| 467 | if (z-a > 0) printf("time %ld %.8lx\n", z-a, h); |
---|
| 468 | } |
---|
| 469 | |
---|
| 470 | /* check that every input bit changes every output bit half the time */ |
---|
| 471 | #define HASHSTATE 1 |
---|
| 472 | #define HASHLEN 1 |
---|
| 473 | #define MAXPAIR 60 |
---|
| 474 | #define MAXLEN 70 |
---|
| 475 | void driver2(void) |
---|
| 476 | { |
---|
| 477 | uint8 qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1]; |
---|
| 478 | uint32 c[HASHSTATE], d[HASHSTATE], i, j=0, k, l, m, z; |
---|
| 479 | uint32 e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE]; |
---|
| 480 | uint32 x[HASHSTATE],y[HASHSTATE]; |
---|
| 481 | uint32 hlen; |
---|
| 482 | |
---|
| 483 | printf("No more than %d trials should ever be needed \n",MAXPAIR/2); |
---|
| 484 | for (hlen=0; hlen < MAXLEN; ++hlen) |
---|
| 485 | { |
---|
| 486 | z=0; |
---|
| 487 | for (i=0; i<hlen; ++i) /*----------------------- for each input byte, */ |
---|
| 488 | { |
---|
| 489 | for (j=0; j<8; ++j) /*------------------------ for each input bit, */ |
---|
| 490 | { |
---|
| 491 | for (m=1; m<8; ++m) /*------------ for serveral possible initvals, */ |
---|
| 492 | { |
---|
| 493 | for (l=0; l<HASHSTATE; ++l) e[l]=f[l]=g[l]=h[l]=x[l]=y[l]=~((uint32)0); |
---|
| 494 | |
---|
| 495 | /*---- check that every output bit is affected by that input bit */ |
---|
| 496 | for (k=0; k<MAXPAIR; k+=2) |
---|
| 497 | { |
---|
| 498 | uint32 finished=1; |
---|
| 499 | /* keys have one bit different */ |
---|
| 500 | for (l=0; l<hlen+1; ++l) {a[l] = b[l] = (uint8)0;} |
---|
| 501 | /* have a and b be two keys differing in only one bit */ |
---|
| 502 | a[i] ^= (k<<j); |
---|
| 503 | a[i] ^= (k>>(8-j)); |
---|
| 504 | c[0] = hashlittle(a, hlen, m); |
---|
| 505 | b[i] ^= ((k+1)<<j); |
---|
| 506 | b[i] ^= ((k+1)>>(8-j)); |
---|
| 507 | d[0] = hashlittle(b, hlen, m); |
---|
| 508 | /* check every bit is 1, 0, set, and not set at least once */ |
---|
| 509 | for (l=0; l<HASHSTATE; ++l) |
---|
| 510 | { |
---|
| 511 | e[l] &= (c[l]^d[l]); |
---|
| 512 | f[l] &= ~(c[l]^d[l]); |
---|
| 513 | g[l] &= c[l]; |
---|
| 514 | h[l] &= ~c[l]; |
---|
| 515 | x[l] &= d[l]; |
---|
| 516 | y[l] &= ~d[l]; |
---|
| 517 | if (e[l]|f[l]|g[l]|h[l]|x[l]|y[l]) finished=0; |
---|
| 518 | } |
---|
| 519 | if (finished) break; |
---|
| 520 | } |
---|
| 521 | if (k>z) z=k; |
---|
| 522 | if (k==MAXPAIR) |
---|
| 523 | { |
---|
| 524 | printf("Some bit didn't change: "); |
---|
| 525 | printf("%.8lx %.8lx %.8lx %.8lx %.8lx %.8lx ", |
---|
| 526 | e[0],f[0],g[0],h[0],x[0],y[0]); |
---|
| 527 | printf("i %ld j %ld m %ld len %ld\n",i,j,m,hlen); |
---|
| 528 | } |
---|
| 529 | if (z==MAXPAIR) goto done; |
---|
| 530 | } |
---|
| 531 | } |
---|
| 532 | } |
---|
| 533 | done: |
---|
| 534 | if (z < MAXPAIR) |
---|
| 535 | { |
---|
| 536 | printf("Mix success %2ld bytes %2ld initvals ",i,m); |
---|
| 537 | printf("required %ld trials\n",z/2); |
---|
| 538 | } |
---|
| 539 | } |
---|
| 540 | printf("\n"); |
---|
| 541 | } |
---|
| 542 | |
---|
| 543 | /* Check for reading beyond the end of the buffer and alignment problems */ |
---|
| 544 | void driver3(void) |
---|
| 545 | { |
---|
| 546 | uint8 buf[MAXLEN+20], *b; |
---|
| 547 | uint32 len; |
---|
| 548 | uint8 q[] = "This is the time for all good men to come to the aid of their country..."; |
---|
| 549 | uint32 dummy1; |
---|
| 550 | uint8 qq[] = "xThis is the time for all good men to come to the aid of their country..."; |
---|
| 551 | uint32 dummy2; |
---|
| 552 | uint8 qqq[] = "xxThis is the time for all good men to come to the aid of their country..."; |
---|
| 553 | uint32 dummy3; |
---|
| 554 | uint8 qqqq[] = "xxxThis is the time for all good men to come to the aid of their country..."; |
---|
| 555 | uint32 h,i,j,ref,x,y; |
---|
| 556 | uint8 *p; |
---|
| 557 | |
---|
| 558 | printf("Endianness. These lines should all be the same (for values filled in):\n"); |
---|
| 559 | printf("%.8lx %.8lx %.8lx\n", |
---|
| 560 | hashword((uint32 *)q, (sizeof(q)-1)/4, 13), |
---|
| 561 | hashword((uint32 *)q, (sizeof(q)-5)/4, 13), |
---|
| 562 | hashword((uint32 *)q, (sizeof(q)-9)/4, 13)); |
---|
| 563 | p = q; |
---|
| 564 | printf("%.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx\n", |
---|
| 565 | hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13), |
---|
| 566 | hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13), |
---|
| 567 | hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13), |
---|
| 568 | hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13), |
---|
| 569 | hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13), |
---|
| 570 | hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13)); |
---|
| 571 | p = &qq[1]; |
---|
| 572 | printf("%.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx\n", |
---|
| 573 | hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13), |
---|
| 574 | hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13), |
---|
| 575 | hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13), |
---|
| 576 | hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13), |
---|
| 577 | hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13), |
---|
| 578 | hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13)); |
---|
| 579 | p = &qqq[2]; |
---|
| 580 | printf("%.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx\n", |
---|
| 581 | hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13), |
---|
| 582 | hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13), |
---|
| 583 | hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13), |
---|
| 584 | hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13), |
---|
| 585 | hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13), |
---|
| 586 | hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13)); |
---|
| 587 | p = &qqqq[3]; |
---|
| 588 | printf("%.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx %.8lx\n", |
---|
| 589 | hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13), |
---|
| 590 | hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13), |
---|
| 591 | hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13), |
---|
| 592 | hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13), |
---|
| 593 | hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13), |
---|
| 594 | hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13)); |
---|
| 595 | printf("\n"); |
---|
| 596 | for (h=0, b=buf+1; h<8; ++h, ++b) |
---|
| 597 | { |
---|
| 598 | for (i=0; i<MAXLEN; ++i) |
---|
| 599 | { |
---|
| 600 | len = i; |
---|
| 601 | for (j=0; j<i; ++j) *(b+j)=0; |
---|
| 602 | |
---|
| 603 | /* these should all be equal */ |
---|
| 604 | ref = hashlittle(b, len, (uint32)1); |
---|
| 605 | *(b+i)=(uint8)~0; |
---|
| 606 | *(b-1)=(uint8)~0; |
---|
| 607 | x = hashlittle(b, len, (uint32)1); |
---|
| 608 | y = hashlittle(b, len, (uint32)1); |
---|
| 609 | if ((ref != x) || (ref != y)) |
---|
| 610 | { |
---|
| 611 | printf("alignment error: %.8lx %.8lx %.8lx %ld %ld\n",ref,x,y,h,i); |
---|
| 612 | } |
---|
| 613 | } |
---|
| 614 | } |
---|
| 615 | } |
---|
| 616 | |
---|
| 617 | /* check for problems with nulls */ |
---|
| 618 | void driver4(void) |
---|
| 619 | { |
---|
| 620 | uint8 buf[1]; |
---|
| 621 | uint32 h,i,state[HASHSTATE]; |
---|
| 622 | |
---|
| 623 | |
---|
| 624 | buf[0] = ~0; |
---|
| 625 | for (i=0; i<HASHSTATE; ++i) state[i] = 1; |
---|
| 626 | printf("These should all be different\n"); |
---|
| 627 | for (i=0, h=0; i<8; ++i) |
---|
| 628 | { |
---|
| 629 | h = hashlittle(buf, (uint32)0, h); |
---|
| 630 | printf("%2ld 0-byte strings, hash is %.8lx\n", i, h); |
---|
| 631 | } |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | |
---|
| 635 | int main() |
---|
| 636 | { |
---|
| 637 | driver1(); /* test that the key is hashed: used for timings */ |
---|
| 638 | driver2(); /* test that whole key is hashed thoroughly */ |
---|
| 639 | driver3(); /* test that nothing but the key is hashed */ |
---|
| 640 | driver4(); /* test hashing multiple buffers (all buffers are null) */ |
---|
| 641 | return 1; |
---|
| 642 | } |
---|
| 643 | |
---|
| 644 | #endif /* SELF_TEST */ |
---|