From: Kurt Zeilenga Date: Fri, 22 Sep 2000 22:19:46 +0000 (+0000) Subject: Replace indexing hash with 32-bit FNV-1... should be much faster than MD5 X-Git-Tag: LDBM_PRE_GIANT_RWLOCK~1916 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=6949ab9d151d296ad54a68ff58cd018f149e706e;p=openldap Replace indexing hash with 32-bit FNV-1... should be much faster than MD5 --- diff --git a/include/lutil_hash.h b/include/lutil_hash.h new file mode 100644 index 0000000000..c382a7a654 --- /dev/null +++ b/include/lutil_hash.h @@ -0,0 +1,46 @@ +/* $OpenLDAP$ */ +/* + * Copyright 1998-2000 The OpenLDAP Foundation, Redwood City, California, USA + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted only + * as authorized by the OpenLDAP Public License. A copy of this + * license is available at http://www.OpenLDAP.org/license.html or + * in file LICENSE in the top-level directory of the distribution. + */ + +/* See hash.c for explanation and copyright information. */ + +#ifndef _LUTIL_HASH_H_ +#define _LUTIL_HASH_H_ + +#include + +LDAP_BEGIN_DECL + +#define LUTIL_HASH_BYTES 4 + +struct lutil_HASHContext { + ber_uint_t hash; +}; + +LDAP_LUTIL_F( void ) +lutil_HASHInit LDAP_P(( + struct lutil_HASHContext *context)); + +LDAP_LUTIL_F( void ) +lutil_HASHUpdate LDAP_P(( + struct lutil_HASHContext *context, + unsigned char const *buf, + ber_len_t len)); + +LDAP_LUTIL_F( void ) +lutil_HASHFinal LDAP_P(( + unsigned char digest[LUTIL_HASH_BYTES], + struct lutil_HASHContext *context)); + +typedef struct lutil_HASHContext lutil_HASH_CTX; + +LDAP_END_DECL + +#endif /* _LUTIL_HASH_H_ */ diff --git a/libraries/liblutil/Makefile.in b/libraries/liblutil/Makefile.in index bb85c616c9..f40e04c54c 100644 --- a/libraries/liblutil/Makefile.in +++ b/libraries/liblutil/Makefile.in @@ -13,11 +13,11 @@ NT_OBJS = ntservice.o slapdmsg.res UNIX_SRCS = detach.c UNIX_OBJS = detach.o -SRCS = base64.c debug.c entropy.c sasl.c signal.c \ +SRCS = base64.c debug.c entropy.c sasl.c signal.c hash.c \ md5.c passwd.c sha1.c getpass.c lockf.c utils.c sockpair.c \ @LIBSRCS@ $(@PLAT@_SRCS) -OBJS = base64.o debug.o entropy.o sasl.o signal.o \ +OBJS = base64.o debug.o entropy.o sasl.o signal.o hash.o \ md5.o passwd.o sha1.o getpass.o lockf.o utils.o sockpair.o \ @LIBOBJS@ $(@PLAT@_OBJS) diff --git a/libraries/liblutil/hash.c b/libraries/liblutil/hash.c new file mode 100644 index 0000000000..f801ffe317 --- /dev/null +++ b/libraries/liblutil/hash.c @@ -0,0 +1,67 @@ +/* $OpenLDAP$ */ +/* This implements the Fowler / Noll / Vo (FNV-1) hash algorithm. + * A summary of the algorithm can be found at: + * http://www.isthe.com/chongo/tech/comp/fnv/index.html + */ + +#include "portable.h" +#include + +/* include socket.h to get sys/types.h and/or winsock2.h */ +#include + +#include + +/* offset and prime for 32-bit FNV-1 */ +#define HASH_OFFSET 0x811c9dc5 +#define HASH_PRIME 16777619 + + +/* + * Initialize context + */ +void +lutil_HASHInit( struct lutil_HASHContext *ctx ) +{ + ctx->hash = HASH_OFFSET; +} + +/* + * Update hash + */ +void +lutil_HASHUpdate( + struct lutil_HASHContext *ctx, + const unsigned char *buf, + ber_len_t len +) +{ + const unsigned char *p, *e; + ber_uint_t h; + + p = buf; + e = &buf[len]; + + h = ctx->hash; + + while( p < e ) { + h *= HASH_PRIME; + h ^= *p++; + } + + ctx->hash = h; +} + +/* + * Save hash + */ +void +lutil_HASHFinal( unsigned char *digest, struct lutil_HASHContext *ctx ) +{ + ber_uint_t h = ctx->hash; + + digest[0] = h & 0xff; + digest[1] = (h>>8) & 0xff; + digest[2] = (h>>16) & 0xff; + digest[3] = (h>>24) & 0xff; +} diff --git a/servers/slapd/schema_init.c b/servers/slapd/schema_init.c index 3e2bad5fed..0de00b9264 100644 --- a/servers/slapd/schema_init.c +++ b/servers/slapd/schema_init.c @@ -15,14 +15,24 @@ #include "slap.h" #include "ldap_pvt.h" -#include "lutil_md5.h" +#ifdef USE_MD5 +#include "lutil_md5.h" /* We should replace MD5 with a faster hash */ #define HASH_BYTES LUTIL_MD5_BYTES #define HASH_CONTEXT lutil_MD5_CTX #define HASH_Init(c) lutil_MD5Init(c) #define HASH_Update(c,buf,len) lutil_MD5Update(c,buf,len) #define HASH_Final(d,c) lutil_MD5Final(d,c) +#else +#include "lutil_hash.h" +/* We should replace MD5 with a faster hash */ +#define HASH_BYTES LUTIL_HASH_BYTES +#define HASH_CONTEXT lutil_HASH_CTX +#define HASH_Init(c) lutil_HASHInit(c) +#define HASH_Update(c,buf,len) lutil_HASHUpdate(c,buf,len) +#define HASH_Final(d,c) lutil_HASHFinal(d,c) +#endif /* recycled validatation routines */ #define berValidate blobValidate