From c00aa8fd5eb72bbeff2a87082669d3863fa7d199 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Fri, 29 May 2009 14:07:50 +0200 Subject: [PATCH] util: import a_strhash() Signed-off-by: Julien Danjou --- common/util.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/common/util.h b/common/util.h index d8f42028..6d2c23cd 100644 --- a/common/util.h +++ b/common/util.h @@ -317,6 +317,21 @@ a_strncat(char *dst, ssize_t n, const char *src, ssize_t l) return dlen + a_strncpy(dst + dlen, n - dlen, src, l); } +/** Compute a hash for a string. + * This is based on 'djb2' algorithm. + */ +static inline unsigned long +a_strhash(const unsigned char *str) +{ + unsigned long hash = 5381; + int c; + + while((c = *str++)) + hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + + return hash; +} + #define fatal(string, ...) _fatal(__LINE__, \ __FUNCTION__, \ string, ## __VA_ARGS__)