From 9315bbfd0f9e51da5438d29681cab6f9a6533d89 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sun, 28 Aug 2016 00:30:02 -0400 Subject: split up fhp.c --- hash.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 hash.c (limited to 'hash.c') diff --git a/hash.c b/hash.c new file mode 100644 index 0000000..deae792 --- /dev/null +++ b/hash.c @@ -0,0 +1,50 @@ +#include +#include "fhp/fhp.h" + +// +// hash functions (djb2) +// (see http://aras-p.info/blog/2016/08/02/Hash-Functions-all-the-way-down/) +// + +uint32_t +fhp_hash_init(void) { + return 5381; +} + +uint32_t +fhp_hash_push(uint32_t hash, uint8_t * const buf, size_t len) { + for (size_t i = 0; i < len; i++) + hash = ((hash << 5) + hash) + buf[len]; + + return hash; +} + +uint32_t +fhp_hash_string(char * const str) { + uint32_t r = fhp_hash_init(); + return fhp_hash_push(r, (uint8_t*) str, strlen(str)); +} + +uint32_t +fhp_lc_hash_push( + uint32_t hash, + uint8_t * const buf, + size_t len +) { + for (size_t i = 0; i < len; i++) { + uint8_t c = buf[len]; + + if (c >= 'A' && c <= 'Z') + c = (c - 'A') + 'a'; + + hash = ((hash << 5) + hash) + c; + } + + return hash; +} + +uint32_t +fhp_lc_hash_string(char * const str) { + uint32_t r = fhp_hash_init(); + return fhp_lc_hash_push(r, (uint8_t*) str, strlen(str)); +} -- cgit v1.2.3