summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2016-08-28 00:30:02 -0400
committerPaul Duncan <pabs@pablotron.org>2016-08-28 00:30:02 -0400
commit9315bbfd0f9e51da5438d29681cab6f9a6533d89 (patch)
treef02b70f56196e57be7086333b321e9cf012ac9ef /hash.c
parent1bc717dc54b9964e7c62082b34d2d74e3daaa6a6 (diff)
downloadlibfhp-9315bbfd0f9e51da5438d29681cab6f9a6533d89.tar.bz2
libfhp-9315bbfd0f9e51da5438d29681cab6f9a6533d89.zip
split up fhp.c
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c50
1 files changed, 50 insertions, 0 deletions
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 <string.h>
+#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));
+}