]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/hash.c
- Implementation einer Hash-Funktion begonnen.
[ngircd-alex.git] / src / ngircd / hash.c
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.1 2002/03/14 15:31:22 alex Exp $
13  *
14  * hash.c: Hash-Werte berechnen
15  */
16
17
18 #include "portab.h"
19
20 #include "imp.h"
21 #include <assert.h>
22
23 #include "exp.h"
24 #include "hash.h"
25
26
27 /*
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.
37  */
38
39
40 typedef unsigned long int ub4;
41 typedef unsigned char ub1;
42
43
44 #define hashsize(n) ((ub4)1<<(n))
45 #define hashmask(n) (hashsize(n)-1)
46
47 #define mix(a,b,c) \
48 { \
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); \
58 }
59
60
61 ub4 hash( register ub1 *k, register ub4 length, register ub4 initval)
62 {
63         /* k: the key
64          * length: length of the key
65          * initval: the previous hash, or an arbitrary value
66          */
67
68         register ub4 a,b,c,len;
69
70         /* Set up the internal state */
71         len = length;
72         a = b = 0x9e3779b9;     /* the golden ratio; an arbitrary value */
73         c = initval;            /* the previous hash value */
74
75         /* handle most of the key */
76         while (len >= 12)
77         {
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));
81                 mix(a,b,c);
82                 k += 12; len -= 12;
83         }
84
85         /* handle the last 11 bytes */
86         c += length;
87         switch(len)             /* all the case statements fall through */
88         {
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);
96                 case 5 : b+=k[4];
97                 case 4 : a+=((ub4)k[3]<<24);
98                 case 3 : a+=((ub4)k[2]<<16);
99                 case 2 : a+=((ub4)k[1]<<8);
100                 case 1 : a+=k[0];
101                 /* case 0: nothing left to add */
102         }
103         mix(a,b,c);
104
105         /* report the result */
106         return c;
107 } /* hash */
108
109
110 /* -eof- */