[core] move djbhash(), dekhash() to algo_md.h

This commit is contained in:
Glenn Strauss 2020-09-04 22:22:25 -04:00
parent 6c8160e5a4
commit 3ffb195532
5 changed files with 46 additions and 31 deletions

View File

@ -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 \

42
src/algo_md.h Normal file
View File

@ -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 */

View File

@ -1,10 +1,10 @@
#include "first.h"
#include "algo_md.h"
#include "buffer.h"
#include "etag.h"
#include <sys/stat.h>
#include <string.h>
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 */

View File

@ -28,13 +28,13 @@
#include <unistd.h>
#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"

View File

@ -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 */