mirror of
https://github.com/osmarks/ngircd.git
synced 2024-12-14 02:40:27 +00:00
- API fuer Hash() definiert.
This commit is contained in:
parent
8a45b177ce
commit
b422b11865
@ -9,7 +9,7 @@
|
||||
* Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
|
||||
* der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
|
||||
*
|
||||
* $Id: hash.c,v 1.1 2002/03/14 15:31:22 alex Exp $
|
||||
* $Id: hash.c,v 1.2 2002/03/14 15:49:36 alex Exp $
|
||||
*
|
||||
* hash.c: Hash-Werte berechnen
|
||||
*/
|
||||
@ -24,6 +24,16 @@
|
||||
#include "hash.h"
|
||||
|
||||
|
||||
LOCAL UINT32 jenkins_hash( register UINT8 *k, register UINT32 length, register UINT32 initval);
|
||||
|
||||
|
||||
GLOBAL UINT32 Hash( CHAR *String )
|
||||
{
|
||||
/* Hash-Wert ueber String berechnen */
|
||||
return jenkins_hash( String, strlen( String ), 42 );
|
||||
} /* Hash */
|
||||
|
||||
|
||||
/*
|
||||
* Die hier verwendete Hash-Funktion stammt aus lookup2.c von Bob Jenkins
|
||||
* (URL: <http://burtleburtle.net/bob/c/lookup2.c>). Aus dem Header:
|
||||
@ -37,11 +47,7 @@
|
||||
*/
|
||||
|
||||
|
||||
typedef unsigned long int ub4;
|
||||
typedef unsigned char ub1;
|
||||
|
||||
|
||||
#define hashsize(n) ((ub4)1<<(n))
|
||||
#define hashsize(n) ((UINT32)1<<(n))
|
||||
#define hashmask(n) (hashsize(n)-1)
|
||||
|
||||
#define mix(a,b,c) \
|
||||
@ -55,17 +61,17 @@ typedef unsigned char ub1;
|
||||
a -= b; a -= c; a ^= (c>>3); \
|
||||
b -= c; b -= a; b ^= (a<<10); \
|
||||
c -= a; c -= b; c ^= (b>>15); \
|
||||
}
|
||||
} /* mix */
|
||||
|
||||
|
||||
ub4 hash( register ub1 *k, register ub4 length, register ub4 initval)
|
||||
LOCAL UINT32 jenkins_hash( register UINT8 *k, register UINT32 length, register UINT32 initval)
|
||||
{
|
||||
/* k: the key
|
||||
* length: length of the key
|
||||
* initval: the previous hash, or an arbitrary value
|
||||
*/
|
||||
|
||||
register ub4 a,b,c,len;
|
||||
register UINT32 a,b,c,len;
|
||||
|
||||
/* Set up the internal state */
|
||||
len = length;
|
||||
@ -75,9 +81,9 @@ ub4 hash( register ub1 *k, register ub4 length, register ub4 initval)
|
||||
/* handle most of the key */
|
||||
while (len >= 12)
|
||||
{
|
||||
a += (k[0] +((ub4)k[1]<<8) +((ub4)k[2]<<16) +((ub4)k[3]<<24));
|
||||
b += (k[4] +((ub4)k[5]<<8) +((ub4)k[6]<<16) +((ub4)k[7]<<24));
|
||||
c += (k[8] +((ub4)k[9]<<8) +((ub4)k[10]<<16)+((ub4)k[11]<<24));
|
||||
a += (k[0] +((UINT32)k[1]<<8) +((UINT32)k[2]<<16) +((UINT32)k[3]<<24));
|
||||
b += (k[4] +((UINT32)k[5]<<8) +((UINT32)k[6]<<16) +((UINT32)k[7]<<24));
|
||||
c += (k[8] +((UINT32)k[9]<<8) +((UINT32)k[10]<<16)+((UINT32)k[11]<<24));
|
||||
mix(a,b,c);
|
||||
k += 12; len -= 12;
|
||||
}
|
||||
@ -86,17 +92,17 @@ ub4 hash( register ub1 *k, register ub4 length, register ub4 initval)
|
||||
c += length;
|
||||
switch(len) /* all the case statements fall through */
|
||||
{
|
||||
case 11: c+=((ub4)k[10]<<24);
|
||||
case 10: c+=((ub4)k[9]<<16);
|
||||
case 9 : c+=((ub4)k[8]<<8);
|
||||
case 11: c+=((UINT32)k[10]<<24);
|
||||
case 10: c+=((UINT32)k[9]<<16);
|
||||
case 9 : c+=((UINT32)k[8]<<8);
|
||||
/* the first byte of c is reserved for the length */
|
||||
case 8 : b+=((ub4)k[7]<<24);
|
||||
case 7 : b+=((ub4)k[6]<<16);
|
||||
case 6 : b+=((ub4)k[5]<<8);
|
||||
case 8 : b+=((UINT32)k[7]<<24);
|
||||
case 7 : b+=((UINT32)k[6]<<16);
|
||||
case 6 : b+=((UINT32)k[5]<<8);
|
||||
case 5 : b+=k[4];
|
||||
case 4 : a+=((ub4)k[3]<<24);
|
||||
case 3 : a+=((ub4)k[2]<<16);
|
||||
case 2 : a+=((ub4)k[1]<<8);
|
||||
case 4 : a+=((UINT32)k[3]<<24);
|
||||
case 3 : a+=((UINT32)k[2]<<16);
|
||||
case 2 : a+=((UINT32)k[1]<<8);
|
||||
case 1 : a+=k[0];
|
||||
/* case 0: nothing left to add */
|
||||
}
|
||||
@ -104,7 +110,7 @@ ub4 hash( register ub1 *k, register ub4 length, register ub4 initval)
|
||||
|
||||
/* report the result */
|
||||
return c;
|
||||
} /* hash */
|
||||
} /* jenkins_hash */
|
||||
|
||||
|
||||
/* -eof- */
|
||||
|
@ -9,7 +9,7 @@
|
||||
* Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
|
||||
* der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
|
||||
*
|
||||
* $Id: hash.h,v 1.1 2002/03/14 15:31:22 alex Exp $
|
||||
* $Id: hash.h,v 1.2 2002/03/14 15:49:36 alex Exp $
|
||||
*
|
||||
* hash.h: Hash-Werte berechnen (Header)
|
||||
*/
|
||||
@ -19,6 +19,9 @@
|
||||
#define __hash_h__
|
||||
|
||||
|
||||
GLOBAL UINT32 Hash( CHAR *String );
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user