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 490f28ff 2002-12-12 alex * This program is free software; you can redistribute it and/or modify
6 490f28ff 2002-12-12 alex * it under the terms of the GNU General Public License as published by
7 490f28ff 2002-12-12 alex * the Free Software Foundation; either version 2 of the License, or
8 490f28ff 2002-12-12 alex * (at your option) any later version.
9 490f28ff 2002-12-12 alex * Please read the file COPYING, README and AUTHORS for more information.
10 8a45b177 2002-03-14 alex *
11 490f28ff 2002-12-12 alex * Hash calculation
12 8a45b177 2002-03-14 alex */
13 8a45b177 2002-03-14 alex
14 8a45b177 2002-03-14 alex
15 8a45b177 2002-03-14 alex #include "portab.h"
16 8a45b177 2002-03-14 alex
17 0ced4181 2002-12-26 alex static char UNUSED id[] = "$Id: hash.c,v 1.9 2002/12/26 16:25:43 alex Exp $";
18 490f28ff 2002-12-12 alex
19 8a45b177 2002-03-14 alex #include "imp.h"
20 8a45b177 2002-03-14 alex #include <assert.h>
21 0df6a761 2002-03-22 alex #include <string.h>
22 8a45b177 2002-03-14 alex
23 0df6a761 2002-03-22 alex #include "defines.h"
24 0df6a761 2002-03-22 alex #include "log.h"
25 0df6a761 2002-03-22 alex #include "tool.h"
26 0df6a761 2002-03-22 alex
27 8a45b177 2002-03-14 alex #include "exp.h"
28 8a45b177 2002-03-14 alex #include "hash.h"
29 8a45b177 2002-03-14 alex
30 8a45b177 2002-03-14 alex
31 19342576 2002-05-27 alex LOCAL UINT32 jenkins_hash PARAMS(( register UINT8 *k, register UINT32 length, register UINT32 initval ));
32 b422b118 2002-03-14 alex
33 b422b118 2002-03-14 alex
34 19342576 2002-05-27 alex GLOBAL UINT32
35 19342576 2002-05-27 alex 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 0ced4181 2002-12-26 alex strlcpy( buffer, String, sizeof( buffer ));
42 95a4b1b1 2002-03-25 alex return jenkins_hash( (UINT8 *)ngt_LowerStr( buffer ), strlen( buffer ), 42 );
43 b422b118 2002-03-14 alex } /* Hash */
44 b422b118 2002-03-14 alex
45 b422b118 2002-03-14 alex
46 8a45b177 2002-03-14 alex /*
47 8a45b177 2002-03-14 alex * Die hier verwendete Hash-Funktion stammt aus lookup2.c von Bob Jenkins
48 8a45b177 2002-03-14 alex * (URL: <http://burtleburtle.net/bob/c/lookup2.c>). Aus dem Header:
49 8a45b177 2002-03-14 alex * --------------------------------------------------------------------
50 8a45b177 2002-03-14 alex * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
51 8a45b177 2002-03-14 alex * hash(), hash2(), hash3, and mix() are externally useful functions.
52 8a45b177 2002-03-14 alex * Routines to test the hash are included if SELF_TEST is defined.
53 8a45b177 2002-03-14 alex * You can use this free for any purpose. It has no warranty.
54 8a45b177 2002-03-14 alex * --------------------------------------------------------------------
55 8a45b177 2002-03-14 alex * nicht alle seiner Funktionen werden hier genutzt.
56 8a45b177 2002-03-14 alex */
57 8a45b177 2002-03-14 alex
58 8a45b177 2002-03-14 alex
59 b422b118 2002-03-14 alex #define hashsize(n) ((UINT32)1<<(n))
60 8a45b177 2002-03-14 alex #define hashmask(n) (hashsize(n)-1)
61 8a45b177 2002-03-14 alex
62 8a45b177 2002-03-14 alex #define mix(a,b,c) \
63 8a45b177 2002-03-14 alex { \
64 8a45b177 2002-03-14 alex a -= b; a -= c; a ^= (c>>13); \
65 8a45b177 2002-03-14 alex b -= c; b -= a; b ^= (a<<8); \
66 8a45b177 2002-03-14 alex c -= a; c -= b; c ^= (b>>13); \
67 8a45b177 2002-03-14 alex a -= b; a -= c; a ^= (c>>12); \
68 8a45b177 2002-03-14 alex b -= c; b -= a; b ^= (a<<16); \
69 8a45b177 2002-03-14 alex c -= a; c -= b; c ^= (b>>5); \
70 8a45b177 2002-03-14 alex a -= b; a -= c; a ^= (c>>3); \
71 8a45b177 2002-03-14 alex b -= c; b -= a; b ^= (a<<10); \
72 8a45b177 2002-03-14 alex c -= a; c -= b; c ^= (b>>15); \
73 b422b118 2002-03-14 alex } /* mix */
74 8a45b177 2002-03-14 alex
75 8a45b177 2002-03-14 alex
76 19342576 2002-05-27 alex LOCAL UINT32
77 19342576 2002-05-27 alex jenkins_hash( register UINT8 *k, register UINT32 length, register UINT32 initval )
78 8a45b177 2002-03-14 alex {
79 8a45b177 2002-03-14 alex /* k: the key
80 8a45b177 2002-03-14 alex * length: length of the key
81 8a45b177 2002-03-14 alex * initval: the previous hash, or an arbitrary value
82 8a45b177 2002-03-14 alex */
83 8a45b177 2002-03-14 alex
84 b422b118 2002-03-14 alex register UINT32 a,b,c,len;
85 8a45b177 2002-03-14 alex
86 8a45b177 2002-03-14 alex /* Set up the internal state */
87 8a45b177 2002-03-14 alex len = length;
88 9353a4a9 2002-12-26 alex a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
89 8a45b177 2002-03-14 alex c = initval; /* the previous hash value */
90 8a45b177 2002-03-14 alex
91 8a45b177 2002-03-14 alex /* handle most of the key */
92 8a45b177 2002-03-14 alex while (len >= 12)
93 8a45b177 2002-03-14 alex {
94 b422b118 2002-03-14 alex a += (k[0] +((UINT32)k[1]<<8) +((UINT32)k[2]<<16) +((UINT32)k[3]<<24));
95 b422b118 2002-03-14 alex b += (k[4] +((UINT32)k[5]<<8) +((UINT32)k[6]<<16) +((UINT32)k[7]<<24));
96 b422b118 2002-03-14 alex c += (k[8] +((UINT32)k[9]<<8) +((UINT32)k[10]<<16)+((UINT32)k[11]<<24));
97 8a45b177 2002-03-14 alex mix(a,b,c);
98 8a45b177 2002-03-14 alex k += 12; len -= 12;
99 8a45b177 2002-03-14 alex }
100 8a45b177 2002-03-14 alex
101 8a45b177 2002-03-14 alex /* handle the last 11 bytes */
102 8a45b177 2002-03-14 alex c += length;
103 d9a13b31 2002-12-25 alex switch( (INT)len ) /* all the case statements fall through */
104 8a45b177 2002-03-14 alex {
105 b422b118 2002-03-14 alex case 11: c+=((UINT32)k[10]<<24);
106 b422b118 2002-03-14 alex case 10: c+=((UINT32)k[9]<<16);
107 b422b118 2002-03-14 alex case 9 : c+=((UINT32)k[8]<<8);
108 8a45b177 2002-03-14 alex /* the first byte of c is reserved for the length */
109 b422b118 2002-03-14 alex case 8 : b+=((UINT32)k[7]<<24);
110 b422b118 2002-03-14 alex case 7 : b+=((UINT32)k[6]<<16);
111 b422b118 2002-03-14 alex case 6 : b+=((UINT32)k[5]<<8);
112 8a45b177 2002-03-14 alex case 5 : b+=k[4];
113 b422b118 2002-03-14 alex case 4 : a+=((UINT32)k[3]<<24);
114 b422b118 2002-03-14 alex case 3 : a+=((UINT32)k[2]<<16);
115 b422b118 2002-03-14 alex case 2 : a+=((UINT32)k[1]<<8);
116 8a45b177 2002-03-14 alex case 1 : a+=k[0];
117 8a45b177 2002-03-14 alex /* case 0: nothing left to add */
118 8a45b177 2002-03-14 alex }
119 8a45b177 2002-03-14 alex mix(a,b,c);
120 8a45b177 2002-03-14 alex
121 8a45b177 2002-03-14 alex /* report the result */
122 8a45b177 2002-03-14 alex return c;
123 b422b118 2002-03-14 alex } /* jenkins_hash */
124 8a45b177 2002-03-14 alex
125 8a45b177 2002-03-14 alex
126 8a45b177 2002-03-14 alex /* -eof- */