]> git.sur5r.net Git - openldap/commitdiff
Replace indexing hash with 32-bit FNV-1... should be much faster than MD5
authorKurt Zeilenga <kurt@openldap.org>
Fri, 22 Sep 2000 22:19:46 +0000 (22:19 +0000)
committerKurt Zeilenga <kurt@openldap.org>
Fri, 22 Sep 2000 22:19:46 +0000 (22:19 +0000)
include/lutil_hash.h [new file with mode: 0644]
libraries/liblutil/Makefile.in
libraries/liblutil/hash.c [new file with mode: 0644]
servers/slapd/schema_init.c

diff --git a/include/lutil_hash.h b/include/lutil_hash.h
new file mode 100644 (file)
index 0000000..c382a7a
--- /dev/null
@@ -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 <lber_types.h>
+
+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_ */
index bb85c616c9f77de6076f56e81d899563c72d3560..f40e04c54c58c874b76d8cfadc7a94e98ee6b8d7 100644 (file)
@@ -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 (file)
index 0000000..f801ffe
--- /dev/null
@@ -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 <ac/string.h>
+
+/* include socket.h to get sys/types.h and/or winsock2.h */
+#include <ac/socket.h>
+
+#include <lutil_hash.h>
+
+/* 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;
+}
index 3e2bad5fed59bfe8105e5063c91f7bbcb5a3c7ac..0de00b9264da24de06d405d8bb567bee5190245b 100644 (file)
 
 #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