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