From e5cc417e96d37741b0d22edda63b73a87195e0a6 Mon Sep 17 00:00:00 2001 From: Julien BLACHE Date: Thu, 30 Apr 2009 14:51:36 +0200 Subject: [PATCH] Add DJB hash function to misc.[ch] and use it --- src/httpd_daap.c | 6 ++---- src/main.c | 5 +++-- src/misc.c | 16 ++++++++++++++++ src/misc.h | 5 +++++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/httpd_daap.c b/src/httpd_daap.c index c572a7e8..5ccf4fa3 100644 --- a/src/httpd_daap.c +++ b/src/httpd_daap.c @@ -47,8 +47,6 @@ #include "httpd.h" #include "httpd_daap.h" -#include "util.h" - struct uri_map { regex_t preg; @@ -691,7 +689,7 @@ parse_meta(struct evhttp_request *req, char *tag, const char *param, uint32_t ** meta = strtok_r(metastr, ",", &ptr); for (i = 0; i < nmeta; i++) { - hashes[i] = util_djb_hash_str(meta); + hashes[i] = djb_hash(meta, strlen(meta)); meta = strtok_r(NULL, ",", &ptr); if (!meta) @@ -1939,7 +1937,7 @@ daap_init(void) for (i = 0; dmap_fields[i].type != 0; i++) { - dmap_fields[i].hash = util_djb_hash_str(dmap_fields[i].desc); + dmap_fields[i].hash = djb_hash(dmap_fields[i].desc, strlen(dmap_fields[i].desc)); node = avl_insert(dmap_fields_hash, &dmap_fields[i]); if (!node) diff --git a/src/main.c b/src/main.c index 3a0a0979..9c506df2 100644 --- a/src/main.c +++ b/src/main.c @@ -86,6 +86,7 @@ #include "conf.h" #include "configfile.h" #include "err.h" +#include "misc.h" #include "filescanner.h" #include "httpd.h" #include "webserver.h" @@ -702,8 +703,8 @@ int main(int argc, char *argv[]) { } snprintf(txtrecord[0], 128, "txtvers=1"); - snprintf(txtrecord[1], 128, "Database ID=%0X", util_djb_hash_str(servername)); - snprintf(txtrecord[2], 128, "Machine ID=%0X", util_djb_hash_str(servername)); + snprintf(txtrecord[1], 128, "Database ID=%0X", djb_hash(servername, strlen(servername))); + snprintf(txtrecord[2], 128, "Machine ID=%0X", djb_hash(servername, strlen(servername))); snprintf(txtrecord[3], 128, "Machine Name=%s", servername); snprintf(txtrecord[4], 128, "mtd-version=%s", VERSION); snprintf(txtrecord[5], 128, "iTSh Version=131073"); /* iTunes 6.0.4 */ diff --git a/src/misc.c b/src/misc.c index b5c01069..87f6dbaf 100644 --- a/src/misc.c +++ b/src/misc.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include "daapd.h" @@ -94,3 +95,18 @@ safe_atol(const char *str, long *val) return 0; } + +uint32_t +djb_hash(void *data, size_t len) +{ + unsigned char *bytes = data; + uint32_t hash = 5381; + + while (len--) + { + hash = ((hash << 5) + hash) + *bytes; + bytes++; + } + + return hash; +} diff --git a/src/misc.h b/src/misc.h index 06ef6646..4f4f2748 100644 --- a/src/misc.h +++ b/src/misc.h @@ -2,10 +2,15 @@ #ifndef __MISC_H__ #define __MISC_H__ +#include + int safe_atoi(const char *str, int *val); int safe_atol(const char *str, long *val); +uint32_t +djb_hash(void *data, size_t len); + #endif /* !__MISC_H__ */