Blob


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