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