]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/hash.c
Code cleanup: mostly removing empty lines
[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
30 static UINT32 jenkins_hash PARAMS(( register UINT8 *k, register UINT32 length, register UINT32 initval ));
31
32
33 GLOBAL UINT32
34 Hash( const char *String )
35 {
36         /* Hash-Wert ueber String berechnen */
37
38         char buffer[LINE_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  * Die hier verwendete Hash-Funktion stammt aus lookup2.c von Bob Jenkins
47  * (URL: <http://burtleburtle.net/bob/c/lookup2.c>). Aus dem Header:
48  * --------------------------------------------------------------------
49  * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
50  * hash(), hash2(), hash3, and mix() are externally useful functions.
51  * Routines to test the hash are included if SELF_TEST is defined.
52  * You can use this free for any purpose.  It has no warranty.
53  * --------------------------------------------------------------------
54  * nicht alle seiner Funktionen werden hier genutzt.
55  */
56
57 #define hashsize(n) ((UINT32)1<<(n))
58 #define hashmask(n) (hashsize(n)-1)
59
60 #define mix(a,b,c) \
61 { \
62         a -= b; a -= c; a ^= (c>>13); \
63         b -= c; b -= a; b ^= (a<<8);  \
64         c -= a; c -= b; c ^= (b>>13); \
65         a -= b; a -= c; a ^= (c>>12); \
66         b -= c; b -= a; b ^= (a<<16); \
67         c -= a; c -= b; c ^= (b>>5);  \
68         a -= b; a -= c; a ^= (c>>3);  \
69         b -= c; b -= a; b ^= (a<<10); \
70         c -= a; c -= b; c ^= (b>>15); \
71 } /* mix */
72
73 static UINT32
74 jenkins_hash( register UINT8 *k, register UINT32 length, register UINT32 initval )
75 {
76         /* k: the key
77          * length: length of the key
78          * initval: the previous hash, or an arbitrary value
79          */
80
81         register UINT32 a,b,c,len;
82
83         /* Set up the internal state */
84         len = length;
85         a = b = 0x9e3779b9;     /* the golden ratio; an arbitrary value */
86         c = initval;            /* the previous hash value */
87
88         /* handle most of the key */
89         while (len >= 12)
90         {
91                 a += (k[0] +((UINT32)k[1]<<8) +((UINT32)k[2]<<16) +((UINT32)k[3]<<24));
92                 b += (k[4] +((UINT32)k[5]<<8) +((UINT32)k[6]<<16) +((UINT32)k[7]<<24));
93                 c += (k[8] +((UINT32)k[9]<<8) +((UINT32)k[10]<<16)+((UINT32)k[11]<<24));
94                 mix(a,b,c);
95                 k += 12; len -= 12;
96         }
97
98         /* handle the last 11 bytes */
99         c += length;
100         switch( (int)len )      /* all the case statements fall through */
101         {
102                 case 11: c+=((UINT32)k[10]<<24);
103                 case 10: c+=((UINT32)k[9]<<16);
104                 case 9 : c+=((UINT32)k[8]<<8);
105                 /* the first byte of c is reserved for the length */
106                 case 8 : b+=((UINT32)k[7]<<24);
107                 case 7 : b+=((UINT32)k[6]<<16);
108                 case 6 : b+=((UINT32)k[5]<<8);
109                 case 5 : b+=k[4];
110                 case 4 : a+=((UINT32)k[3]<<24);
111                 case 3 : a+=((UINT32)k[2]<<16);
112                 case 2 : a+=((UINT32)k[1]<<8);
113                 case 1 : a+=k[0];
114                 /* case 0: nothing left to add */
115         }
116         mix(a,b,c);
117
118         /* report the result */
119         return c;
120 } /* jenkins_hash */
121
122 /* -eof- */