2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
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.
12 * $Id: hash.c,v 1.1 2002/03/14 15:31:22 alex Exp $
14 * hash.c: Hash-Werte berechnen
28 * Die hier verwendete Hash-Funktion stammt aus lookup2.c von Bob Jenkins
29 * (URL: <http://burtleburtle.net/bob/c/lookup2.c>). Aus dem Header:
30 * --------------------------------------------------------------------
31 * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
32 * hash(), hash2(), hash3, and mix() are externally useful functions.
33 * Routines to test the hash are included if SELF_TEST is defined.
34 * You can use this free for any purpose. It has no warranty.
35 * --------------------------------------------------------------------
36 * nicht alle seiner Funktionen werden hier genutzt.
40 typedef unsigned long int ub4;
41 typedef unsigned char ub1;
44 #define hashsize(n) ((ub4)1<<(n))
45 #define hashmask(n) (hashsize(n)-1)
49 a -= b; a -= c; a ^= (c>>13); \
50 b -= c; b -= a; b ^= (a<<8); \
51 c -= a; c -= b; c ^= (b>>13); \
52 a -= b; a -= c; a ^= (c>>12); \
53 b -= c; b -= a; b ^= (a<<16); \
54 c -= a; c -= b; c ^= (b>>5); \
55 a -= b; a -= c; a ^= (c>>3); \
56 b -= c; b -= a; b ^= (a<<10); \
57 c -= a; c -= b; c ^= (b>>15); \
61 ub4 hash( register ub1 *k, register ub4 length, register ub4 initval)
64 * length: length of the key
65 * initval: the previous hash, or an arbitrary value
68 register ub4 a,b,c,len;
70 /* Set up the internal state */
72 a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
73 c = initval; /* the previous hash value */
75 /* handle most of the key */
78 a += (k[0] +((ub4)k[1]<<8) +((ub4)k[2]<<16) +((ub4)k[3]<<24));
79 b += (k[4] +((ub4)k[5]<<8) +((ub4)k[6]<<16) +((ub4)k[7]<<24));
80 c += (k[8] +((ub4)k[9]<<8) +((ub4)k[10]<<16)+((ub4)k[11]<<24));
85 /* handle the last 11 bytes */
87 switch(len) /* all the case statements fall through */
89 case 11: c+=((ub4)k[10]<<24);
90 case 10: c+=((ub4)k[9]<<16);
91 case 9 : c+=((ub4)k[8]<<8);
92 /* the first byte of c is reserved for the length */
93 case 8 : b+=((ub4)k[7]<<24);
94 case 7 : b+=((ub4)k[6]<<16);
95 case 6 : b+=((ub4)k[5]<<8);
97 case 4 : a+=((ub4)k[3]<<24);
98 case 3 : a+=((ub4)k[2]<<16);
99 case 2 : a+=((ub4)k[1]<<8);
101 /* case 0: nothing left to add */
105 /* report the result */