Annotation of ircnowd/src/ngircd/hash.c, Revision 1.1.1.1
1.1 tomglok 1: /*
2: * ngIRCd -- The Next Generation IRC Daemon
3: * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
4: *
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.
10: */
11:
12: #include "portab.h"
13:
14: /**
15: * @file
16: * Hash calculation
17: */
18:
19: #include <assert.h>
20: #include <string.h>
21:
22: #include "defines.h"
23: #include "tool.h"
24:
25: #include "hash.h"
26:
27: static UINT32 jenkins_hash PARAMS((UINT8 *k, UINT32 length, UINT32 initval));
28:
29: /**
30: * Calculate hash value for a given string.
31: *
32: * @param String Input string
33: * @return 32 bit hash value
34: */
35: GLOBAL UINT32
36: Hash( const char *String )
37: {
38: char buffer[COMMAND_LEN];
39:
40: strlcpy(buffer, String, sizeof(buffer));
41: return jenkins_hash((UINT8 *)ngt_LowerStr(buffer),
42: (UINT32)strlen(buffer), 42);
43: } /* Hash */
44:
45: /*
46: * This hash function originates from lookup3.c of Bob Jenkins
47: * (URL: <http://burtleburtle.net/bob/c/lookup3.c>):
48: * --------------------------------------------------------------------
49: * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
50: * These are functions for producing 32-bit hashes for hash table lookup.
51: * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
52: * are externally useful functions. Routines to test the hash are included
53: * if SELF_TEST is defined. You can use this free for any purpose. It's in
54: * the public domain. It has no warranty.
55: * --------------------------------------------------------------------
56: * Not all of his functions are used here.
57: */
58:
59: #define hashsize(n) ((UINT32)1<<(n))
60: #define hashmask(n) (hashsize(n)-1)
61: #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
62:
63: #define mix(a,b,c) \
64: { \
65: a -= c; a ^= rot(c, 4); c += b; \
66: b -= a; b ^= rot(a, 6); a += c; \
67: c -= b; c ^= rot(b, 8); b += a; \
68: a -= c; a ^= rot(c,16); c += b; \
69: b -= a; b ^= rot(a,19); a += c; \
70: c -= b; c ^= rot(b, 4); b += a; \
71: } /* mix */
72:
73: #define final(a,b,c) \
74: { \
75: c ^= b; c -= rot(b,14); \
76: a ^= c; a -= rot(c,11); \
77: b ^= a; b -= rot(a,25); \
78: c ^= b; c -= rot(b,16); \
79: a ^= c; a -= rot(c,4); \
80: b ^= a; b -= rot(a,14); \
81: c ^= b; c -= rot(b,24); \
82: }
83:
84: static UINT32
85: jenkins_hash(UINT8 *k, UINT32 length, UINT32 initval)
86: {
87: /* k: the key
88: * length: length of the key
89: * initval: the previous hash, or an arbitrary value
90: */
91: UINT32 a,b,c;
92:
93: /* Set up the internal state */
94: a = b = c = 0xdeadbeef + length + initval;
95:
96: /* handle most of the key */
97: while (length > 12) {
98: a += (k[0] +((UINT32)k[1]<<8) +((UINT32)k[2]<<16) +((UINT32)k[3]<<24));
99: b += (k[4] +((UINT32)k[5]<<8) +((UINT32)k[6]<<16) +((UINT32)k[7]<<24));
100: c += (k[8] +((UINT32)k[9]<<8) +((UINT32)k[10]<<16)+((UINT32)k[11]<<24));
101: mix(a,b,c);
102: length -= 12;
103: k += 12;
104: }
105:
106: /*-------------------------------- last block: affect all 32 bits of (c) */
107: switch(length) /* all the case statements fall through */
108:
109: {
110: case 12: c+=((UINT32)k[11])<<24;
111: case 11: c+=((UINT32)k[10]<<16);
112: case 10: c+=((UINT32)k[9]<<8);
113: case 9 : c+=k[8];
114: case 8 : b+=((UINT32)k[7]<<24);
115: case 7 : b+=((UINT32)k[6]<<16);
116: case 6 : b+=((UINT32)k[5]<<8);
117: case 5 : b+=k[4];
118: case 4 : a+=((UINT32)k[3]<<24);
119: case 3 : a+=((UINT32)k[2]<<16);
120: case 2 : a+=((UINT32)k[1]<<8);
121: case 1 : a+=k[0];
122: break;
123: case 0 : return c;
124: }
125: final(a,b,c);
126: return c;
127: } /* jenkins_hash */
128:
129: /* -eof- */
CVSweb