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