2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
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.
17 static char UNUSED id[] = "$Id: hash.c,v 1.13 2006/10/06 21:23:47 fw Exp $";
30 static UINT32 jenkins_hash PARAMS(( register UINT8 *k, register UINT32 length, register UINT32 initval ));
34 Hash( const char *String )
36 /* Hash-Wert ueber String berechnen */
38 char buffer[LINE_LEN];
40 strlcpy( buffer, String, sizeof( buffer ));
41 return jenkins_hash( (UINT8 *)ngt_LowerStr( buffer ), strlen( buffer ), 42 );
46 * Die hier verwendete Hash-Funktion stammt aus lookup2.c von Bob Jenkins
47 * (URL: <http://burtleburtle.net/bob/c/lookup2.c>). Aus dem Header:
48 * --------------------------------------------------------------------
49 * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
50 * hash(), hash2(), hash3, and mix() are externally useful functions.
51 * Routines to test the hash are included if SELF_TEST is defined.
52 * You can use this free for any purpose. It has no warranty.
53 * --------------------------------------------------------------------
54 * nicht alle seiner Funktionen werden hier genutzt.
58 #define hashsize(n) ((UINT32)1<<(n))
59 #define hashmask(n) (hashsize(n)-1)
63 a -= b; a -= c; a ^= (c>>13); \
64 b -= c; b -= a; b ^= (a<<8); \
65 c -= a; c -= b; c ^= (b>>13); \
66 a -= b; a -= c; a ^= (c>>12); \
67 b -= c; b -= a; b ^= (a<<16); \
68 c -= a; c -= b; c ^= (b>>5); \
69 a -= b; a -= c; a ^= (c>>3); \
70 b -= c; b -= a; b ^= (a<<10); \
71 c -= a; c -= b; c ^= (b>>15); \
76 jenkins_hash( register UINT8 *k, register UINT32 length, register UINT32 initval )
79 * length: length of the key
80 * initval: the previous hash, or an arbitrary value
83 register UINT32 a,b,c,len;
85 /* Set up the internal state */
87 a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
88 c = initval; /* the previous hash value */
90 /* handle most of the key */
93 a += (k[0] +((UINT32)k[1]<<8) +((UINT32)k[2]<<16) +((UINT32)k[3]<<24));
94 b += (k[4] +((UINT32)k[5]<<8) +((UINT32)k[6]<<16) +((UINT32)k[7]<<24));
95 c += (k[8] +((UINT32)k[9]<<8) +((UINT32)k[10]<<16)+((UINT32)k[11]<<24));
100 /* handle the last 11 bytes */
102 switch( (int)len ) /* all the case statements fall through */
104 case 11: c+=((UINT32)k[10]<<24);
105 case 10: c+=((UINT32)k[9]<<16);
106 case 9 : c+=((UINT32)k[8]<<8);
107 /* the first byte of c is reserved for the length */
108 case 8 : b+=((UINT32)k[7]<<24);
109 case 7 : b+=((UINT32)k[6]<<16);
110 case 6 : b+=((UINT32)k[5]<<8);
112 case 4 : a+=((UINT32)k[3]<<24);
113 case 3 : a+=((UINT32)k[2]<<16);
114 case 2 : a+=((UINT32)k[1]<<8);
116 /* case 0: nothing left to add */
120 /* report the result */