--- libaitcrc/src/hash.c 2011/04/28 20:28:20 1.2 +++ libaitcrc/src/hash.c 2012/08/29 09:00:44 1.4 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: hash.c,v 1.2 2011/04/28 20:28:20 misho Exp $ +* $Id: hash.c,v 1.4 2012/08/29 09:00:44 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -47,7 +47,8 @@ SUCH DAMAGE. /* - * hash_varchar() Compute index hash by variable length string + * hash_varchar() - Compute index hash by variable length string + * * @csStr = Input data buffer * @nStrLen = Length of data buffer * @nVer = Version of algorythm; 0 - original, 1 - AITNET variant @@ -65,10 +66,10 @@ hash_varchar(const char *csStr, int nStrLen, int nVer) n = 2 * hash + csStr[i]; if (!nVer) { if (hash & 0x80000000) - n ^= 0xC0A0A0D5; // Polynom CRC-32 aviation + n ^= POLY_CRC32; /* Polynom CRC-32 aviation */ } else { - if (n & 1) // Misho ;) patch for better avalanche!!! - n ^= 0xC0A0A0D5; // Polynom CRC-32 aviation + if (n & 1) /* Misho ;) patch for better avalanche!!! */ + n ^= POLY_CRC32; /* Polynom CRC-32 aviation */ } hash = n; } @@ -77,7 +78,8 @@ hash_varchar(const char *csStr, int nStrLen, int nVer) } /* - * hash_bernstein() Compute index hash by Bernstein + * hash_bernstein() - Compute index hash by Bernstein + * * @csStr = Input data buffer * @nStrLen = Length of data buffer * @nVer = Version of algorythm; 0 - Bernstein, 1 - DJBX33A variant @@ -86,7 +88,7 @@ hash_varchar(const char *csStr, int nStrLen, int nVer) inline u_int hash_bernstein(const char *csStr, int nStrLen, int nVer) { - register u_int hash = 5381; + register u_int hash = INIT_BERNSTEIN; register int i; assert(csStr); @@ -95,13 +97,14 @@ hash_bernstein(const char *csStr, int nStrLen, int nVe if (!nVer) hash = ((hash << 5) + hash) + csStr[i]; else - hash = hash * 33 + csStr[i]; // DJBX33A + hash = hash * 33 + csStr[i]; /* DJBX33A */ return hash; } /* - * hash_fnv1() Compute index hash by FNV-1 + * hash_fnv1() - Compute index hash by FNV-1 + * * @csStr = Input data buffer * @nStrLen = Length of data buffer * @nVer = Version of algorythm; 0 - FNV-1, 1 - FNV-1a (best avalanche) @@ -110,25 +113,26 @@ hash_bernstein(const char *csStr, int nStrLen, int nVe inline u_int hash_fnv1(const char *csStr, int nStrLen, int nVer) { - register u_int hash = 0x811c9dc5; // 2166136261 + register u_int hash = INIT_FNV1; register int i; assert(csStr); for (i = 0; i < nStrLen; i++) - if (!nVer) { // FNV-1 - hash *= 16777619; + if (!nVer) { /* FNV-1 */ + hash *= POLY_FNV1; hash ^= csStr[i]; - } else { // FNV-1a + } else { /* FNV-1a */ hash ^= csStr[i]; - hash *= 16777619; + hash *= POLY_FNV1; } return hash; } /* - * hash_jenkins() Compute index hash by Jenkins (one-at-a-time) + * hash_jenkins() - Compute index hash by Jenkins (one-at-a-time) + * * @csStr = Input data buffer * @nStrLen = Length of data buffer * return: Hash value @@ -154,7 +158,8 @@ hash_jenkins(const char *csStr, int nStrLen) } /* - * hash_reddragon() Compute index hash by Red Dragon + * hash_reddragon() - Compute index hash by Red Dragon + * * @csStr = Input data buffer * @nStrLen = Length of data buffer * return: Hash value @@ -176,4 +181,45 @@ hash_reddragon(const char *csStr, int nStrLen) } return hash; +} + +/* + * hash_jenkins32() - Fast Jenkins hash function + * + * @buf = Input buffer + * @len = Length of buffer + * @prevhash = Previous hash, if participate in continuing hash + * return: Hash value + */ +u_int +hash_jenkins32(const u_int *buf, int len, u_int prevhash) +{ + register u_int a, b, c; + + assert(buf); + + a = b = c = INIT_JENKINS32 + (((u_int) len) << 2) + prevhash; + + while (len > 3) { + a += buf[0]; + b += buf[1]; + c += buf[2]; + MIX32(a, b, c); + len -= 3; + buf += 3; + } + + switch (len) { + case 3: + c += buf[2]; + case 2: + b += buf[1]; + case 1: + a += buf[0]; + FINAL32(a, b, c); + case 0: + break; + } + + return c; }