]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/hash.c
- fixed some warnings that showed up with -Wtraditional
[ngircd-alex.git] / src / ngircd / hash.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001,2002 by 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  * Hash calculation
12  */
13
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: hash.c,v 1.7 2002/12/25 13:22:43 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <string.h>
22
23 #include "defines.h"
24 #include "log.h"
25 #include "tool.h"
26
27 #include "exp.h"
28 #include "hash.h"
29
30
31 LOCAL UINT32 jenkins_hash PARAMS(( register UINT8 *k, register UINT32 length, register UINT32 initval ));
32
33
34 GLOBAL UINT32
35 Hash( CHAR *String )
36 {
37         /* Hash-Wert ueber String berechnen */
38
39         CHAR buffer[LINE_LEN];
40
41         strncpy( buffer, String, LINE_LEN - 1 );
42         buffer[LINE_LEN - 1] = '\0';
43         
44         return jenkins_hash( (UINT8 *)ngt_LowerStr( buffer ), strlen( buffer ), 42 );
45 } /* Hash */
46
47
48 /*
49  * Die hier verwendete Hash-Funktion stammt aus lookup2.c von Bob Jenkins
50  * (URL: <http://burtleburtle.net/bob/c/lookup2.c>). Aus dem Header:
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  * nicht alle seiner Funktionen werden hier genutzt.
58  */
59
60
61 #define hashsize(n) ((UINT32)1<<(n))
62 #define hashmask(n) (hashsize(n)-1)
63
64 #define mix(a,b,c) \
65 { \
66         a -= b; a -= c; a ^= (c>>13); \
67         b -= c; b -= a; b ^= (a<<8);  \
68         c -= a; c -= b; c ^= (b>>13); \
69         a -= b; a -= c; a ^= (c>>12); \
70         b -= c; b -= a; b ^= (a<<16); \
71         c -= a; c -= b; c ^= (b>>5);  \
72         a -= b; a -= c; a ^= (c>>3);  \
73         b -= c; b -= a; b ^= (a<<10); \
74         c -= a; c -= b; c ^= (b>>15); \
75 } /* mix */
76
77
78 LOCAL UINT32
79 jenkins_hash( register UINT8 *k, register UINT32 length, register UINT32 initval )
80 {
81         /* k: the key
82          * length: length of the key
83          * initval: the previous hash, or an arbitrary value
84          */
85
86         register UINT32 a,b,c,len;
87
88         /* Set up the internal state */
89         len = length;
90         a = b = 0x9e3779b9u;    /* the golden ratio; an arbitrary value */
91         c = initval;            /* the previous hash value */
92
93         /* handle most of the key */
94         while (len >= 12)
95         {
96                 a += (k[0] +((UINT32)k[1]<<8) +((UINT32)k[2]<<16) +((UINT32)k[3]<<24));
97                 b += (k[4] +((UINT32)k[5]<<8) +((UINT32)k[6]<<16) +((UINT32)k[7]<<24));
98                 c += (k[8] +((UINT32)k[9]<<8) +((UINT32)k[10]<<16)+((UINT32)k[11]<<24));
99                 mix(a,b,c);
100                 k += 12; len -= 12;
101         }
102
103         /* handle the last 11 bytes */
104         c += length;
105         switch( (INT)len )      /* all the case statements fall through */
106         {
107                 case 11: c+=((UINT32)k[10]<<24);
108                 case 10: c+=((UINT32)k[9]<<16);
109                 case 9 : c+=((UINT32)k[8]<<8);
110                 /* the first byte of c is reserved for the length */
111                 case 8 : b+=((UINT32)k[7]<<24);
112                 case 7 : b+=((UINT32)k[6]<<16);
113                 case 6 : b+=((UINT32)k[5]<<8);
114                 case 5 : b+=k[4];
115                 case 4 : a+=((UINT32)k[3]<<24);
116                 case 3 : a+=((UINT32)k[2]<<16);
117                 case 2 : a+=((UINT32)k[1]<<8);
118                 case 1 : a+=k[0];
119                 /* case 0: nothing left to add */
120         }
121         mix(a,b,c);
122
123         /* report the result */
124         return c;
125 } /* jenkins_hash */
126
127
128 /* -eof- */