2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
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.
29 static UINT32 jenkins_hash PARAMS((register UINT8 *k, register UINT32 length,
30 register UINT32 initval));
33 * Calculate hash value for a given string.
35 * @param String Input string
36 * @return 32 bit hash value
39 Hash( const char *String )
41 char buffer[LINE_LEN];
43 strlcpy(buffer, String, sizeof(buffer));
44 return jenkins_hash((UINT8 *)ngt_LowerStr(buffer),
45 (UINT32)strlen(buffer), 42);
49 * This hash function originates from lookup2.c of Bob Jenkins
50 * (URL: <http://burtleburtle.net/bob/c/lookup2.c>):
51 * --------------------------------------------------------------------
52 * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
53 * hash(), hash2(), hash3, and mix() are externally useful functions.
54 * Routines to test the hash are included if SELF_TEST is defined.
55 * You can use this free for any purpose. It has no warranty.
56 * --------------------------------------------------------------------
57 * Not all of his functions are used here.
60 #define hashsize(n) ((UINT32)1<<(n))
61 #define hashmask(n) (hashsize(n)-1)
65 a -= b; a -= c; a ^= (c>>13); \
66 b -= c; b -= a; b ^= (a<<8); \
67 c -= a; c -= b; c ^= (b>>13); \
68 a -= b; a -= c; a ^= (c>>12); \
69 b -= c; b -= a; b ^= (a<<16); \
70 c -= a; c -= b; c ^= (b>>5); \
71 a -= b; a -= c; a ^= (c>>3); \
72 b -= c; b -= a; b ^= (a<<10); \
73 c -= a; c -= b; c ^= (b>>15); \
77 jenkins_hash( register UINT8 *k, register UINT32 length, register UINT32 initval )
80 * length: length of the key
81 * initval: the previous hash, or an arbitrary value
84 register UINT32 a,b,c,len;
86 /* Set up the internal state */
88 a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
89 c = initval; /* the previous hash value */
91 /* handle most of the key */
94 a += (k[0] +((UINT32)k[1]<<8) +((UINT32)k[2]<<16) +((UINT32)k[3]<<24));
95 b += (k[4] +((UINT32)k[5]<<8) +((UINT32)k[6]<<16) +((UINT32)k[7]<<24));
96 c += (k[8] +((UINT32)k[9]<<8) +((UINT32)k[10]<<16)+((UINT32)k[11]<<24));
101 /* handle the last 11 bytes */
103 switch( (int)len ) /* all the case statements fall through */
105 case 11: c+=((UINT32)k[10]<<24);
106 case 10: c+=((UINT32)k[9]<<16);
107 case 9 : c+=((UINT32)k[8]<<8);
108 /* the first byte of c is reserved for the length */
109 case 8 : b+=((UINT32)k[7]<<24);
110 case 7 : b+=((UINT32)k[6]<<16);
111 case 6 : b+=((UINT32)k[5]<<8);
113 case 4 : a+=((UINT32)k[3]<<24);
114 case 3 : a+=((UINT32)k[2]<<16);
115 case 2 : a+=((UINT32)k[1]<<8);
117 /* case 0: nothing left to add */
121 /* report the result */