]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/hash.c
Add Doxygen @file documentation to each source and header file
[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
13 #include "portab.h"
14
15 /**
16  * @file
17  * Hash calculation
18  */
19
20 #include "imp.h"
21 #include <assert.h>
22 #include <string.h>
23
24 #include "defines.h"
25 #include "tool.h"
26
27 #include "exp.h"
28 #include "hash.h"
29
30
31 static UINT32 jenkins_hash PARAMS(( register UINT8 *k, register UINT32 length, register UINT32 initval ));
32
33
34 GLOBAL UINT32
35 Hash( const char *String )
36 {
37         /* Hash-Wert ueber String berechnen */
38
39         char buffer[LINE_LEN];
40
41         strlcpy(buffer, String, sizeof(buffer));
42         return jenkins_hash((UINT8 *)ngt_LowerStr(buffer),
43                             (UINT32)strlen(buffer), 42);
44 } /* Hash */
45
46
47 /*
48  * Die hier verwendete Hash-Funktion stammt aus lookup2.c von Bob Jenkins
49  * (URL: <http://burtleburtle.net/bob/c/lookup2.c>). Aus dem Header:
50  * --------------------------------------------------------------------
51  * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
52  * hash(), hash2(), hash3, and mix() are externally useful functions.
53  * Routines to test the hash are included if SELF_TEST is defined.
54  * You can use this free for any purpose.  It has no warranty.
55  * --------------------------------------------------------------------
56  * nicht alle seiner Funktionen werden hier genutzt.
57  */
58
59
60 #define hashsize(n) ((UINT32)1<<(n))
61 #define hashmask(n) (hashsize(n)-1)
62
63 #define mix(a,b,c) \
64 { \
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); \
74 } /* mix */
75
76
77 static UINT32
78 jenkins_hash( register UINT8 *k, register UINT32 length, register UINT32 initval )
79 {
80         /* k: the key
81          * length: length of the key
82          * initval: the previous hash, or an arbitrary value
83          */
84
85         register UINT32 a,b,c,len;
86
87         /* Set up the internal state */
88         len = length;
89         a = b = 0x9e3779b9;     /* the golden ratio; an arbitrary value */
90         c = initval;            /* the previous hash value */
91
92         /* handle most of the key */
93         while (len >= 12)
94         {
95                 a += (k[0] +((UINT32)k[1]<<8) +((UINT32)k[2]<<16) +((UINT32)k[3]<<24));
96                 b += (k[4] +((UINT32)k[5]<<8) +((UINT32)k[6]<<16) +((UINT32)k[7]<<24));
97                 c += (k[8] +((UINT32)k[9]<<8) +((UINT32)k[10]<<16)+((UINT32)k[11]<<24));
98                 mix(a,b,c);
99                 k += 12; len -= 12;
100         }
101
102         /* handle the last 11 bytes */
103         c += length;
104         switch( (int)len )      /* all the case statements fall through */
105         {
106                 case 11: c+=((UINT32)k[10]<<24);
107                 case 10: c+=((UINT32)k[9]<<16);
108                 case 9 : c+=((UINT32)k[8]<<8);
109                 /* the first byte of c is reserved for the length */
110                 case 8 : b+=((UINT32)k[7]<<24);
111                 case 7 : b+=((UINT32)k[6]<<16);
112                 case 6 : b+=((UINT32)k[5]<<8);
113                 case 5 : b+=k[4];
114                 case 4 : a+=((UINT32)k[3]<<24);
115                 case 3 : a+=((UINT32)k[2]<<16);
116                 case 2 : a+=((UINT32)k[1]<<8);
117                 case 1 : a+=k[0];
118                 /* case 0: nothing left to add */
119         }
120         mix(a,b,c);
121
122         /* report the result */
123         return c;
124 } /* jenkins_hash */
125
126
127 /* -eof- */