diff --git a/src/Makefile.am b/src/Makefile.am index 77ba0ff0..7648df82 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -454,7 +454,7 @@ mod_wstunnel_la_LIBADD = $(common_libadd) $(CRYPTO_LIB) hdr = base64.h buffer.h burl.h network.h log.h http_kv.h keyvalue.h \ response.h request.h reqpool.h fastcgi.h chunk.h h2.h \ first.h http_chunk.h \ - algo_md5.h algo_sha1.h \ + algo_md.h algo_md5.h algo_sha1.h \ http_auth.h http_header.h http_vhostdb.h stream.h \ fdevent.h gw_backend.h connections.h base.h base_decls.h stat_cache.h \ plugin.h plugin_config.h \ diff --git a/src/algo_md.h b/src/algo_md.h new file mode 100644 index 00000000..f3fac6eb --- /dev/null +++ b/src/algo_md.h @@ -0,0 +1,42 @@ +/* algo_md.h - message digest (MD) wrapper (non-cryptographic) + * + * Copyright(c) 2020 Glenn Strauss gstrauss()gluelogic.com All rights reserved + * License: BSD 3-clause (same as lighttpd) + */ +#ifndef INCLUDED_ALGO_MD_H +#define INCLUDED_ALGO_MD_H +#include "first.h" + + +/* + * not cryptographically secure (and never intended to be) + */ + + +/* DJB hash function for strings (djb2a) */ +#define DJBHASH_INIT 5381 +__attribute_pure__ +static inline uint32_t djbhash(const char *str, const uint32_t len, uint32_t hash); +static inline uint32_t djbhash(const char *str, const uint32_t len, uint32_t hash) +{ + const unsigned char * const s = (const unsigned char *)str; + for (uint32_t i = 0; i < len; ++i) hash = ((hash << 5) + hash) ^ s[i]; + return hash; +} + + +/* Donald E. Knuth + * The Art Of Computer Programming Volume 3 + * Chapter 6.4, Topic: Sorting and Search */ +__attribute_pure__ +static inline uint32_t dekhash (const char *str, const uint32_t len); +static inline uint32_t dekhash (const char *str, const uint32_t len) +{ + const unsigned char * const s = (const unsigned char *)str; + uint32_t h = len; + for (uint32_t i = 0; i < len; ++i) h = (h << 5) ^ (h >> 27) ^ s[i]; + return h; +} + + +#endif /* INCLUDED_ALGO_MD_H */ diff --git a/src/etag.c b/src/etag.c index 7f7b7726..9d1ff713 100644 --- a/src/etag.c +++ b/src/etag.c @@ -1,10 +1,10 @@ #include "first.h" +#include "algo_md.h" #include "buffer.h" #include "etag.h" #include -#include int etag_is_equal(const buffer *etag, const char *line, int weak_ok) { enum { @@ -173,20 +173,6 @@ int etag_create(buffer *etag, const struct stat *st, int flags) { } -/* Donald E. Knuth - * The Art Of Computer Programming Volume 3 - * Chapter 6.4, Topic: Sorting and Search */ -__attribute_pure__ -static inline uint32_t -dekhash (const char *str, const uint32_t len) -{ - const unsigned char * const s = (const unsigned char *)str; - uint32_t h = len; - for (uint32_t i = 0; i < len; ++i) h = (h << 5) ^ (h >> 27) ^ s[i]; - return h; -} - - void etag_mutate (buffer * const mut, const buffer * const etag) { /* mut and etag may be the same, so calculate hash before modifying mut */ diff --git a/src/gw_backend.c b/src/gw_backend.c index b1d796aa..b09de522 100644 --- a/src/gw_backend.c +++ b/src/gw_backend.c @@ -28,13 +28,13 @@ #include #include "base.h" +#include "algo_md.h" #include "array.h" #include "buffer.h" #include "chunk.h" #include "fdevent.h" #include "log.h" #include "sock_addr.h" -#include "splaytree.h" diff --git a/src/splaytree.h b/src/splaytree.h index c4074b65..e15a8472 100644 --- a/src/splaytree.h +++ b/src/splaytree.h @@ -22,23 +22,10 @@ splay_tree * splaytree_size(splay_tree *t); /* a special version of NULL which was a real node with size 0. */ -__attribute_pure__ -static inline uint32_t djbhash(const char *str, const uint32_t len, uint32_t hash); +#include "algo_md.h" __attribute_pure__ static inline int32_t splaytree_djbhash(const char *str, const uint32_t len); - - -/* the famous DJB hash function for strings */ -#define DJBHASH_INIT 5381 -static inline uint32_t djbhash(const char *str, const uint32_t len, uint32_t hash) -{ - const unsigned char * const s = (const unsigned char *)str; - for (uint32_t i = 0; i < len; ++i) hash = ((hash << 5) + hash) ^ s[i]; - return hash; -} - - static inline int32_t splaytree_djbhash(const char *str, const uint32_t len) { /* strip highest bit of hash value for splaytree */