Blame


1 8a45b177 2002-03-14 alex /*
2 8a45b177 2002-03-14 alex * ngIRCd -- The Next Generation IRC Daemon
3 8a45b177 2002-03-14 alex * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4 8a45b177 2002-03-14 alex *
5 8a45b177 2002-03-14 alex * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6 8a45b177 2002-03-14 alex * der GNU General Public License (GPL), wie von der Free Software Foundation
7 8a45b177 2002-03-14 alex * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8 8a45b177 2002-03-14 alex * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9 8a45b177 2002-03-14 alex * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10 8a45b177 2002-03-14 alex * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
11 8a45b177 2002-03-14 alex *
12 95a4b1b1 2002-03-25 alex * $Id: hash.c,v 1.4 2002/03/25 19:11:01 alex Exp $
13 8a45b177 2002-03-14 alex *
14 8a45b177 2002-03-14 alex * hash.c: Hash-Werte berechnen
15 8a45b177 2002-03-14 alex */
16 8a45b177 2002-03-14 alex
17 8a45b177 2002-03-14 alex
18 8a45b177 2002-03-14 alex #include "portab.h"
19 8a45b177 2002-03-14 alex
20 8a45b177 2002-03-14 alex #include "imp.h"
21 8a45b177 2002-03-14 alex #include <assert.h>
22 0df6a761 2002-03-22 alex #include <string.h>
23 8a45b177 2002-03-14 alex
24 0df6a761 2002-03-22 alex #include "defines.h"
25 0df6a761 2002-03-22 alex #include "log.h"
26 0df6a761 2002-03-22 alex #include "tool.h"
27 0df6a761 2002-03-22 alex
28 8a45b177 2002-03-14 alex #include "exp.h"
29 8a45b177 2002-03-14 alex #include "hash.h"
30 8a45b177 2002-03-14 alex
31 8a45b177 2002-03-14 alex
32 b422b118 2002-03-14 alex LOCAL UINT32 jenkins_hash( register UINT8 *k, register UINT32 length, register UINT32 initval);
33 b422b118 2002-03-14 alex
34 b422b118 2002-03-14 alex
35 b422b118 2002-03-14 alex GLOBAL UINT32 Hash( CHAR *String )
36 b422b118 2002-03-14 alex {
37 b422b118 2002-03-14 alex /* Hash-Wert ueber String berechnen */
38 0df6a761 2002-03-22 alex
39 0df6a761 2002-03-22 alex CHAR buffer[LINE_LEN];
40 0df6a761 2002-03-22 alex
41 0df6a761 2002-03-22 alex strncpy( buffer, String, LINE_LEN - 1 );
42 0df6a761 2002-03-22 alex buffer[LINE_LEN - 1] = '\0';
43 0df6a761 2002-03-22 alex
44 95a4b1b1 2002-03-25 alex return jenkins_hash( (UINT8 *)ngt_LowerStr( buffer ), strlen( buffer ), 42 );
45 b422b118 2002-03-14 alex } /* Hash */
46 b422b118 2002-03-14 alex
47 b422b118 2002-03-14 alex
48 8a45b177 2002-03-14 alex /*
49 8a45b177 2002-03-14 alex * Die hier verwendete Hash-Funktion stammt aus lookup2.c von Bob Jenkins
50 8a45b177 2002-03-14 alex * (URL: <http://burtleburtle.net/bob/c/lookup2.c>). Aus dem Header:
51 8a45b177 2002-03-14 alex * --------------------------------------------------------------------
52 8a45b177 2002-03-14 alex * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
53 8a45b177 2002-03-14 alex * hash(), hash2(), hash3, and mix() are externally useful functions.
54 8a45b177 2002-03-14 alex * Routines to test the hash are included if SELF_TEST is defined.
55 8a45b177 2002-03-14 alex * You can use this free for any purpose. It has no warranty.
56 8a45b177 2002-03-14 alex * --------------------------------------------------------------------
57 8a45b177 2002-03-14 alex * nicht alle seiner Funktionen werden hier genutzt.
58 8a45b177 2002-03-14 alex */
59 8a45b177 2002-03-14 alex
60 8a45b177 2002-03-14 alex
61 b422b118 2002-03-14 alex #define hashsize(n) ((UINT32)1<<(n))
62 8a45b177 2002-03-14 alex #define hashmask(n) (hashsize(n)-1)
63 8a45b177 2002-03-14 alex
64 8a45b177 2002-03-14 alex #define mix(a,b,c) \
65 8a45b177 2002-03-14 alex { \
66 8a45b177 2002-03-14 alex a -= b; a -= c; a ^= (c>>13); \
67 8a45b177 2002-03-14 alex b -= c; b -= a; b ^= (a<<8); \
68 8a45b177 2002-03-14 alex c -= a; c -= b; c ^= (b>>13); \
69 8a45b177 2002-03-14 alex a -= b; a -= c; a ^= (c>>12); \
70 8a45b177 2002-03-14 alex b -= c; b -= a; b ^= (a<<16); \
71 8a45b177 2002-03-14 alex c -= a; c -= b; c ^= (b>>5); \
72 8a45b177 2002-03-14 alex a -= b; a -= c; a ^= (c>>3); \
73 8a45b177 2002-03-14 alex b -= c; b -= a; b ^= (a<<10); \
74 8a45b177 2002-03-14 alex c -= a; c -= b; c ^= (b>>15); \
75 b422b118 2002-03-14 alex } /* mix */
76 8a45b177 2002-03-14 alex
77 8a45b177 2002-03-14 alex
78 b422b118 2002-03-14 alex LOCAL UINT32 jenkins_hash( register UINT8 *k, register UINT32 length, register UINT32 initval)
79 8a45b177 2002-03-14 alex {
80 8a45b177 2002-03-14 alex /* k: the key
81 8a45b177 2002-03-14 alex * length: length of the key
82 8a45b177 2002-03-14 alex * initval: the previous hash, or an arbitrary value
83 8a45b177 2002-03-14 alex */
84 8a45b177 2002-03-14 alex
85 b422b118 2002-03-14 alex register UINT32 a,b,c,len;
86 8a45b177 2002-03-14 alex
87 8a45b177 2002-03-14 alex /* Set up the internal state */
88 8a45b177 2002-03-14 alex len = length;
89 8a45b177 2002-03-14 alex a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
90 8a45b177 2002-03-14 alex c = initval; /* the previous hash value */
91 8a45b177 2002-03-14 alex
92 8a45b177 2002-03-14 alex /* handle most of the key */
93 8a45b177 2002-03-14 alex while (len >= 12)
94 8a45b177 2002-03-14 alex {
95 b422b118 2002-03-14 alex a += (k[0] +((UINT32)k[1]<<8) +((UINT32)k[2]<<16) +((UINT32)k[3]<<24));
96 b422b118 2002-03-14 alex b += (k[4] +((UINT32)k[5]<<8) +((UINT32)k[6]<<16) +((UINT32)k[7]<<24));
97 b422b118 2002-03-14 alex c += (k[8] +((UINT32)k[9]<<8) +((UINT32)k[10]<<16)+((UINT32)k[11]<<24));
98 8a45b177 2002-03-14 alex mix(a,b,c);
99 8a45b177 2002-03-14 alex k += 12; len -= 12;
100 8a45b177 2002-03-14 alex }
101 8a45b177 2002-03-14 alex
102 8a45b177 2002-03-14 alex /* handle the last 11 bytes */
103 8a45b177 2002-03-14 alex c += length;
104 8a45b177 2002-03-14 alex switch(len) /* all the case statements fall through */
105 8a45b177 2002-03-14 alex {
106 b422b118 2002-03-14 alex case 11: c+=((UINT32)k[10]<<24);
107 b422b118 2002-03-14 alex case 10: c+=((UINT32)k[9]<<16);
108 b422b118 2002-03-14 alex case 9 : c+=((UINT32)k[8]<<8);
109 8a45b177 2002-03-14 alex /* the first byte of c is reserved for the length */
110 b422b118 2002-03-14 alex case 8 : b+=((UINT32)k[7]<<24);
111 b422b118 2002-03-14 alex case 7 : b+=((UINT32)k[6]<<16);
112 b422b118 2002-03-14 alex case 6 : b+=((UINT32)k[5]<<8);
113 8a45b177 2002-03-14 alex case 5 : b+=k[4];
114 b422b118 2002-03-14 alex case 4 : a+=((UINT32)k[3]<<24);
115 b422b118 2002-03-14 alex case 3 : a+=((UINT32)k[2]<<16);
116 b422b118 2002-03-14 alex case 2 : a+=((UINT32)k[1]<<8);
117 8a45b177 2002-03-14 alex case 1 : a+=k[0];
118 8a45b177 2002-03-14 alex /* case 0: nothing left to add */
119 8a45b177 2002-03-14 alex }
120 8a45b177 2002-03-14 alex mix(a,b,c);
121 8a45b177 2002-03-14 alex
122 8a45b177 2002-03-14 alex /* report the result */
123 8a45b177 2002-03-14 alex return c;
124 b422b118 2002-03-14 alex } /* jenkins_hash */
125 8a45b177 2002-03-14 alex
126 8a45b177 2002-03-14 alex
127 8a45b177 2002-03-14 alex /* -eof- */