Blob


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