2004-12-23 15:16:30 +02:00
|
|
|
#include "djb2_hash.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2005-01-21 22:42:33 +02:00
|
|
|
djb2_state_t *djb2_state_new()
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
2005-01-21 22:42:33 +02:00
|
|
|
djb2_state_t *state = (djb2_state_t *)malloc(sizeof(djb2_state_t));
|
2011-12-26 23:35:30 +02:00
|
|
|
if (!djb2_state) return NULL;
|
2005-01-18 23:06:08 +02:00
|
|
|
state->hashfunc = CMPH_HASH_DJB2;
|
2004-12-23 15:16:30 +02:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2005-01-21 22:42:33 +02:00
|
|
|
void djb2_state_destroy(djb2_state_t *state)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
|
|
|
free(state);
|
|
|
|
}
|
|
|
|
|
2005-01-21 22:42:33 +02:00
|
|
|
cmph_uint32 djb2_hash(djb2_state_t *state, const char *k, cmph_uint32 keylen)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
2005-01-18 23:06:08 +02:00
|
|
|
register cmph_uint32 hash = 5381;
|
2006-04-27 20:30:19 +03:00
|
|
|
const unsigned char *ptr = (unsigned char *)k;
|
2005-01-18 23:06:08 +02:00
|
|
|
cmph_uint32 i = 0;
|
2011-12-26 23:35:30 +02:00
|
|
|
while (i < keylen)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
|
|
|
hash = hash*33 ^ *ptr;
|
|
|
|
++ptr, ++i;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-21 22:42:33 +02:00
|
|
|
void djb2_state_dump(djb2_state_t *state, char **buf, cmph_uint32 *buflen)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
|
|
|
*buf = NULL;
|
|
|
|
*buflen = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-07-29 06:09:31 +03:00
|
|
|
djb2_state_t *djb2_state_copy(djb2_state_t *src_state)
|
|
|
|
{
|
|
|
|
djb2_state_t *dest_state = (djb2_state_t *)malloc(sizeof(djb2_state_t));
|
|
|
|
dest_state->hashfunc = src_state->hashfunc;
|
|
|
|
return dest_state;
|
|
|
|
}
|
|
|
|
|
2005-01-21 22:42:33 +02:00
|
|
|
djb2_state_t *djb2_state_load(const char *buf, cmph_uint32 buflen)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
2005-01-21 22:42:33 +02:00
|
|
|
djb2_state_t *state = (djb2_state_t *)malloc(sizeof(djb2_state_t));
|
2005-01-18 23:06:08 +02:00
|
|
|
state->hashfunc = CMPH_HASH_DJB2;
|
2004-12-23 15:16:30 +02:00
|
|
|
return state;
|
|
|
|
}
|