jjako | c49cb30 | 2004-12-30 16:40:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Hash lookup function. |
| 3 | * Copyright (C) 2003, 2004 Mondru AB. |
| 4 | * |
| 5 | * The contents of this file may be used under the terms of the GNU |
| 6 | * General Public License Version 2, provided that the above copyright |
| 7 | * notice and this permission notice is included in all copies or |
| 8 | * substantial portions of the software. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * lookup() |
| 14 | * Generates a 32 bit hash. |
| 15 | * Based on public domain code by Bob Jenkins |
| 16 | * It should be one of the best hash functions around in terms of both |
| 17 | * statistical properties and speed. It is NOT recommended for cryptographic |
| 18 | * purposes. |
| 19 | **/ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 20 | unsigned long int lookup(k, length, level) |
| 21 | register unsigned char *k; /* the key */ |
| 22 | register unsigned long int length; /* the length of the key */ |
| 23 | register unsigned long int level; /* the previous hash, or an arbitrary value */ |
jjako | c49cb30 | 2004-12-30 16:40:17 +0000 | [diff] [blame] | 24 | { |
| 25 | |
| 26 | #define mix(a,b,c) \ |
| 27 | { \ |
| 28 | a -= b; a -= c; a ^= (c>>13); \ |
| 29 | b -= c; b -= a; b ^= (a<<8); \ |
| 30 | c -= a; c -= b; c ^= (b>>13); \ |
| 31 | a -= b; a -= c; a ^= (c>>12); \ |
| 32 | b -= c; b -= a; b ^= (a<<16); \ |
| 33 | c -= a; c -= b; c ^= (b>>5); \ |
| 34 | a -= b; a -= c; a ^= (c>>3); \ |
| 35 | b -= c; b -= a; b ^= (a<<10); \ |
| 36 | c -= a; c -= b; c ^= (b>>15); \ |
| 37 | } |
| 38 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 39 | typedef unsigned long int ub4; /* unsigned 4-byte quantities */ |
| 40 | typedef unsigned char ub1; /* unsigned 1-byte quantities */ |
| 41 | register unsigned long int a, b, c, len; |
jjako | c49cb30 | 2004-12-30 16:40:17 +0000 | [diff] [blame] | 42 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 43 | /* Set up the internal state */ |
| 44 | len = length; |
| 45 | a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */ |
| 46 | c = level; /* the previous hash value */ |
| 47 | |
| 48 | /*---------------------------------------- handle most of the key */ |
| 49 | while (len >= 12) { |
| 50 | a += (k[0] + ((ub4) k[1] << 8) + ((ub4) k[2] << 16) + |
| 51 | ((ub4) k[3] << 24)); |
| 52 | b += (k[4] + ((ub4) k[5] << 8) + ((ub4) k[6] << 16) + |
| 53 | ((ub4) k[7] << 24)); |
| 54 | c += (k[8] + ((ub4) k[9] << 8) + ((ub4) k[10] << 16) + |
| 55 | ((ub4) k[11] << 24)); |
| 56 | mix(a, b, c); |
| 57 | k += 12; |
| 58 | len -= 12; |
| 59 | } |
| 60 | |
| 61 | /*------------------------------------- handle the last 11 bytes */ |
| 62 | c += length; |
| 63 | switch (len) { /* all the case statements fall through */ |
| 64 | case 11: |
| 65 | c += ((ub4) k[10] << 24); |
| 66 | case 10: |
| 67 | c += ((ub4) k[9] << 16); |
| 68 | case 9: |
| 69 | c += ((ub4) k[8] << 8); |
| 70 | /* the first byte of c is reserved for the length */ |
| 71 | case 8: |
| 72 | b += ((ub4) k[7] << 24); |
| 73 | case 7: |
| 74 | b += ((ub4) k[6] << 16); |
| 75 | case 6: |
| 76 | b += ((ub4) k[5] << 8); |
| 77 | case 5: |
| 78 | b += k[4]; |
| 79 | case 4: |
| 80 | a += ((ub4) k[3] << 24); |
| 81 | case 3: |
| 82 | a += ((ub4) k[2] << 16); |
| 83 | case 2: |
| 84 | a += ((ub4) k[1] << 8); |
| 85 | case 1: |
| 86 | a += k[0]; |
| 87 | /* case 0: nothing left to add */ |
| 88 | } |
| 89 | mix(a, b, c); |
| 90 | /*-------------------------------------------- report the result */ |
| 91 | return c; |
| 92 | } |