X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=ngircd-alex.git;a=blobdiff_plain;f=src%2Fngircd%2Fhash.c;h=eb980d17e2e618b62431bd8f99be95b53ab46f5d;hp=511dba15274c17edfd335f50562f6b59866208d0;hb=2a7dd06ebd9cc72d45a6a4becdbef5213d7b7800;hpb=8a45b177ce478d466d42d59767c4e73405b616b0 diff --git a/src/ngircd/hash.c b/src/ngircd/hash.c index 511dba15..eb980d17 100644 --- a/src/ngircd/hash.c +++ b/src/ngircd/hash.c @@ -1,29 +1,47 @@ /* * ngIRCd -- The Next Generation IRC Daemon - * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de) + * Copyright (c)2001-2010 Alexander Barton (alex@barton.de) * - * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen - * der GNU General Public License (GPL), wie von der Free Software Foundation - * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2 - * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version. - * 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 $ - * - * hash.c: Hash-Werte berechnen + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * Please read the file COPYING, README and AUTHORS for more information. */ - #include "portab.h" +/** + * @file + * Hash calculation + */ + #include "imp.h" #include +#include + +#include "defines.h" +#include "tool.h" #include "exp.h" #include "hash.h" +static UINT32 jenkins_hash PARAMS(( register UINT8 *k, register UINT32 length, register UINT32 initval )); + + +GLOBAL UINT32 +Hash( const char *String ) +{ + /* Hash-Wert ueber String berechnen */ + + char buffer[LINE_LEN]; + + strlcpy(buffer, String, sizeof(buffer)); + return jenkins_hash((UINT8 *)ngt_LowerStr(buffer), + (UINT32)strlen(buffer), 42); +} /* Hash */ + /* * Die hier verwendete Hash-Funktion stammt aus lookup2.c von Bob Jenkins * (URL: ). Aus dem Header: @@ -36,12 +54,7 @@ * nicht alle seiner Funktionen werden hier genutzt. */ - -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 +68,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) +static 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,28 +88,28 @@ 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; } /* handle the last 11 bytes */ c += length; - switch(len) /* all the case statements fall through */ + switch( (int)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 +117,6 @@ ub4 hash( register ub1 *k, register ub4 length, register ub4 initval) /* report the result */ return c; -} /* hash */ - +} /* jenkins_hash */ /* -eof- */