2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
25 static UINT32 jenkins_hash PARAMS((UINT8 *k, UINT32 length, UINT32 initval));
28 * Calculate hash value for a given string.
30 * @param String Input string
31 * @return 32 bit hash value
34 Hash( const char *String )
36 char buffer[COMMAND_LEN];
38 strlcpy(buffer, String, sizeof(buffer));
39 return jenkins_hash((UINT8 *)ngt_LowerStr(buffer),
40 (UINT32)strlen(buffer), 42);
44 * This hash function originates from lookup3.c of Bob Jenkins
45 * (URL: <http://burtleburtle.net/bob/c/lookup3.c>):
46 * --------------------------------------------------------------------
47 * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
48 * These are functions for producing 32-bit hashes for hash table lookup.
49 * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
50 * are externally useful functions. Routines to test the hash are included
51 * if SELF_TEST is defined. You can use this free for any purpose. It's in
52 * the public domain. It has no warranty.
53 * --------------------------------------------------------------------
54 * Not all of his functions are used here.
57 #define hashsize(n) ((UINT32)1<<(n))
58 #define hashmask(n) (hashsize(n)-1)
59 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
63 a -= c; a ^= rot(c, 4); c += b; \
64 b -= a; b ^= rot(a, 6); a += c; \
65 c -= b; c ^= rot(b, 8); b += a; \
66 a -= c; a ^= rot(c,16); c += b; \
67 b -= a; b ^= rot(a,19); a += c; \
68 c -= b; c ^= rot(b, 4); b += a; \
71 #define final(a,b,c) \
73 c ^= b; c -= rot(b,14); \
74 a ^= c; a -= rot(c,11); \
75 b ^= a; b -= rot(a,25); \
76 c ^= b; c -= rot(b,16); \
77 a ^= c; a -= rot(c,4); \
78 b ^= a; b -= rot(a,14); \
79 c ^= b; c -= rot(b,24); \
83 jenkins_hash(UINT8 *k, UINT32 length, UINT32 initval)
86 * length: length of the key
87 * initval: the previous hash, or an arbitrary value
91 /* Set up the internal state */
92 a = b = c = 0xdeadbeef + length + initval;
94 /* handle most of the key */
96 a += (k[0] +((UINT32)k[1]<<8) +((UINT32)k[2]<<16) +((UINT32)k[3]<<24));
97 b += (k[4] +((UINT32)k[5]<<8) +((UINT32)k[6]<<16) +((UINT32)k[7]<<24));
98 c += (k[8] +((UINT32)k[9]<<8) +((UINT32)k[10]<<16)+((UINT32)k[11]<<24));
104 /*-------------------------------- last block: affect all 32 bits of (c) */
105 switch(length) /* all the case statements fall through */
108 case 12: c+=((UINT32)k[11])<<24;
109 case 11: c+=((UINT32)k[10]<<16);
110 case 10: c+=((UINT32)k[9]<<8);
112 case 8 : b+=((UINT32)k[7]<<24);
113 case 7 : b+=((UINT32)k[6]<<16);
114 case 6 : b+=((UINT32)k[5]<<8);
116 case 4 : a+=((UINT32)k[3]<<24);
117 case 3 : a+=((UINT32)k[2]<<16);
118 case 2 : a+=((UINT32)k[1]<<8);