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 19342576 2002-05-27 alex * $Id: hash.c,v 1.5 2002/05/27 12:54:07 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 19342576 2002-05-27 alex LOCAL UINT32 jenkins_hash PARAMS(( register UINT8 *k, register UINT32 length, register UINT32 initval ));
33 b422b118 2002-03-14 alex
34 b422b118 2002-03-14 alex
35 19342576 2002-05-27 alex GLOBAL UINT32
36 19342576 2002-05-27 alex Hash( CHAR *String )
37 b422b118 2002-03-14 alex {
38 b422b118 2002-03-14 alex /* Hash-Wert ueber String berechnen */
39 0df6a761 2002-03-22 alex
40 0df6a761 2002-03-22 alex CHAR buffer[LINE_LEN];
41 0df6a761 2002-03-22 alex
42 0df6a761 2002-03-22 alex strncpy( buffer, String, LINE_LEN - 1 );
43 0df6a761 2002-03-22 alex buffer[LINE_LEN - 1] = '\0';
44 0df6a761 2002-03-22 alex
45 95a4b1b1 2002-03-25 alex return jenkins_hash( (UINT8 *)ngt_LowerStr( buffer ), strlen( buffer ), 42 );
46 b422b118 2002-03-14 alex } /* Hash */
47 b422b118 2002-03-14 alex
48 b422b118 2002-03-14 alex
49 8a45b177 2002-03-14 alex /*
50 8a45b177 2002-03-14 alex * Die hier verwendete Hash-Funktion stammt aus lookup2.c von Bob Jenkins
51 8a45b177 2002-03-14 alex * (URL: <http://burtleburtle.net/bob/c/lookup2.c>). Aus dem Header:
52 8a45b177 2002-03-14 alex * --------------------------------------------------------------------
53 8a45b177 2002-03-14 alex * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
54 8a45b177 2002-03-14 alex * hash(), hash2(), hash3, and mix() are externally useful functions.
55 8a45b177 2002-03-14 alex * Routines to test the hash are included if SELF_TEST is defined.
56 8a45b177 2002-03-14 alex * You can use this free for any purpose. It has no warranty.
57 8a45b177 2002-03-14 alex * --------------------------------------------------------------------
58 8a45b177 2002-03-14 alex * nicht alle seiner Funktionen werden hier genutzt.
59 8a45b177 2002-03-14 alex */
60 8a45b177 2002-03-14 alex
61 8a45b177 2002-03-14 alex
62 b422b118 2002-03-14 alex #define hashsize(n) ((UINT32)1<<(n))
63 8a45b177 2002-03-14 alex #define hashmask(n) (hashsize(n)-1)
64 8a45b177 2002-03-14 alex
65 8a45b177 2002-03-14 alex #define mix(a,b,c) \
66 8a45b177 2002-03-14 alex { \
67 8a45b177 2002-03-14 alex a -= b; a -= c; a ^= (c>>13); \
68 8a45b177 2002-03-14 alex b -= c; b -= a; b ^= (a<<8); \
69 8a45b177 2002-03-14 alex c -= a; c -= b; c ^= (b>>13); \
70 8a45b177 2002-03-14 alex a -= b; a -= c; a ^= (c>>12); \
71 8a45b177 2002-03-14 alex b -= c; b -= a; b ^= (a<<16); \
72 8a45b177 2002-03-14 alex c -= a; c -= b; c ^= (b>>5); \
73 8a45b177 2002-03-14 alex a -= b; a -= c; a ^= (c>>3); \
74 8a45b177 2002-03-14 alex b -= c; b -= a; b ^= (a<<10); \
75 8a45b177 2002-03-14 alex c -= a; c -= b; c ^= (b>>15); \
76 b422b118 2002-03-14 alex } /* mix */
77 8a45b177 2002-03-14 alex
78 8a45b177 2002-03-14 alex
79 19342576 2002-05-27 alex LOCAL UINT32
80 19342576 2002-05-27 alex jenkins_hash( register UINT8 *k, register UINT32 length, register UINT32 initval )
81 8a45b177 2002-03-14 alex {
82 8a45b177 2002-03-14 alex /* k: the key
83 8a45b177 2002-03-14 alex * length: length of the key
84 8a45b177 2002-03-14 alex * initval: the previous hash, or an arbitrary value
85 8a45b177 2002-03-14 alex */
86 8a45b177 2002-03-14 alex
87 b422b118 2002-03-14 alex register UINT32 a,b,c,len;
88 8a45b177 2002-03-14 alex
89 8a45b177 2002-03-14 alex /* Set up the internal state */
90 8a45b177 2002-03-14 alex len = length;
91 8a45b177 2002-03-14 alex a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
92 8a45b177 2002-03-14 alex c = initval; /* the previous hash value */
93 8a45b177 2002-03-14 alex
94 8a45b177 2002-03-14 alex /* handle most of the key */
95 8a45b177 2002-03-14 alex while (len >= 12)
96 8a45b177 2002-03-14 alex {
97 b422b118 2002-03-14 alex a += (k[0] +((UINT32)k[1]<<8) +((UINT32)k[2]<<16) +((UINT32)k[3]<<24));
98 b422b118 2002-03-14 alex b += (k[4] +((UINT32)k[5]<<8) +((UINT32)k[6]<<16) +((UINT32)k[7]<<24));
99 b422b118 2002-03-14 alex c += (k[8] +((UINT32)k[9]<<8) +((UINT32)k[10]<<16)+((UINT32)k[11]<<24));
100 8a45b177 2002-03-14 alex mix(a,b,c);
101 8a45b177 2002-03-14 alex k += 12; len -= 12;
102 8a45b177 2002-03-14 alex }
103 8a45b177 2002-03-14 alex
104 8a45b177 2002-03-14 alex /* handle the last 11 bytes */
105 8a45b177 2002-03-14 alex c += length;
106 8a45b177 2002-03-14 alex switch(len) /* all the case statements fall through */
107 8a45b177 2002-03-14 alex {
108 b422b118 2002-03-14 alex case 11: c+=((UINT32)k[10]<<24);
109 b422b118 2002-03-14 alex case 10: c+=((UINT32)k[9]<<16);
110 b422b118 2002-03-14 alex case 9 : c+=((UINT32)k[8]<<8);
111 8a45b177 2002-03-14 alex /* the first byte of c is reserved for the length */
112 b422b118 2002-03-14 alex case 8 : b+=((UINT32)k[7]<<24);
113 b422b118 2002-03-14 alex case 7 : b+=((UINT32)k[6]<<16);
114 b422b118 2002-03-14 alex case 6 : b+=((UINT32)k[5]<<8);
115 8a45b177 2002-03-14 alex case 5 : b+=k[4];
116 b422b118 2002-03-14 alex case 4 : a+=((UINT32)k[3]<<24);
117 b422b118 2002-03-14 alex case 3 : a+=((UINT32)k[2]<<16);
118 b422b118 2002-03-14 alex case 2 : a+=((UINT32)k[1]<<8);
119 8a45b177 2002-03-14 alex case 1 : a+=k[0];
120 8a45b177 2002-03-14 alex /* case 0: nothing left to add */
121 8a45b177 2002-03-14 alex }
122 8a45b177 2002-03-14 alex mix(a,b,c);
123 8a45b177 2002-03-14 alex
124 8a45b177 2002-03-14 alex /* report the result */
125 8a45b177 2002-03-14 alex return c;
126 b422b118 2002-03-14 alex } /* jenkins_hash */
127 8a45b177 2002-03-14 alex
128 8a45b177 2002-03-14 alex
129 8a45b177 2002-03-14 alex /* -eof- */