Add DJB hash function to misc.[ch] and use it

This commit is contained in:
Julien BLACHE 2009-04-30 14:51:36 +02:00
parent 0a1c4545dc
commit e5cc417e96
4 changed files with 26 additions and 6 deletions

View File

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

View File

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

View File

@ -23,6 +23,7 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <limits.h>
#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;
}

View File

@ -2,10 +2,15 @@
#ifndef __MISC_H__
#define __MISC_H__
#include <stdint.h>
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__ */