diff --git a/src/util.c b/src/util.c new file mode 100644 index 00000000..242991c6 --- /dev/null +++ b/src/util.c @@ -0,0 +1,35 @@ +/* + * simple utility functions + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_STDINT_H +# include +#endif + +#include +#include +#include + +uint32_t util_djb_hash_block(unsigned char *data, uint32_t len) { + uint32_t hash = 5381; + unsigned char *pstr = data; + + while(len--) { + hash = ((hash << 5) + hash) + *pstr; + pstr++; + } + return hash; +} + + +uint32_t util_djb_hash_str(char *str) { + uint32_t len; + + len = strlen(str); + return util_djb_hash_block((unsigned char *)str,len); +} + diff --git a/src/util.h b/src/util.h new file mode 100644 index 00000000..74972b38 --- /dev/null +++ b/src/util.h @@ -0,0 +1,24 @@ +/* + * simple utility functions + */ + +#ifndef _UTIL_H_ +#define _UTIL_H_ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_STDINT_H +# include +#endif + +#include + +/* simple hashing functions */ +extern uint32_t util_djb_hash_block(unsigned char *data, uint32_t len); +extern uint32_t util_djb_hash_str(char *str); + + +#endif /* _UTIL_H_ */ +