2004-12-23 15:16:30 +02:00
|
|
|
#include "hash_state.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
//#define DEBUG
|
|
|
|
#include "debug.h"
|
|
|
|
|
2005-01-27 16:12:28 +02:00
|
|
|
const char *cmph_hash_names[] = { "djb2", "fnv", "jenkins", "sdbm", NULL };
|
2004-12-23 15:16:30 +02:00
|
|
|
|
2005-01-21 22:42:33 +02:00
|
|
|
hash_state_t *hash_state_new(CMPH_HASH hashfunc, cmph_uint32 hashsize)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
2005-01-21 22:42:33 +02:00
|
|
|
hash_state_t *state = NULL;
|
2004-12-23 15:16:30 +02:00
|
|
|
switch (hashfunc)
|
|
|
|
{
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_JENKINS:
|
2004-12-23 15:16:30 +02:00
|
|
|
DEBUGP("Jenkins function - %u\n", hashsize);
|
2005-01-21 22:42:33 +02:00
|
|
|
state = (hash_state_t *)jenkins_state_new(hashsize);
|
2004-12-23 15:16:30 +02:00
|
|
|
DEBUGP("Jenkins function created\n");
|
|
|
|
break;
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_DJB2:
|
2005-01-21 22:42:33 +02:00
|
|
|
state = (hash_state_t *)djb2_state_new();
|
2004-12-23 15:16:30 +02:00
|
|
|
break;
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_SDBM:
|
2005-01-21 22:42:33 +02:00
|
|
|
state = (hash_state_t *)sdbm_state_new();
|
2004-12-23 15:16:30 +02:00
|
|
|
break;
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_FNV:
|
2005-01-21 22:42:33 +02:00
|
|
|
state = (hash_state_t *)fnv_state_new();
|
2004-12-23 15:16:30 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
state->hashfunc = hashfunc;
|
|
|
|
return state;
|
|
|
|
}
|
2005-01-21 22:42:33 +02:00
|
|
|
cmph_uint32 hash(hash_state_t *state, const char *key, cmph_uint32 keylen)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
|
|
|
switch (state->hashfunc)
|
|
|
|
{
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_JENKINS:
|
2005-01-21 22:42:33 +02:00
|
|
|
return jenkins_hash((jenkins_state_t *)state, key, keylen);
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_DJB2:
|
2005-01-21 22:42:33 +02:00
|
|
|
return djb2_hash((djb2_state_t *)state, key, keylen);
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_SDBM:
|
2005-01-21 22:42:33 +02:00
|
|
|
return sdbm_hash((sdbm_state_t *)state, key, keylen);
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_FNV:
|
2005-01-21 22:42:33 +02:00
|
|
|
return fnv_hash((fnv_state_t *)state, key, keylen);
|
2004-12-23 15:16:30 +02:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-21 22:42:33 +02:00
|
|
|
void hash_state_dump(hash_state_t *state, char **buf, cmph_uint32 *buflen)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
|
|
|
char *algobuf;
|
|
|
|
switch (state->hashfunc)
|
|
|
|
{
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_JENKINS:
|
2005-01-21 22:42:33 +02:00
|
|
|
jenkins_state_dump((jenkins_state_t *)state, &algobuf, buflen);
|
2004-12-23 15:16:30 +02:00
|
|
|
if (*buflen == UINT_MAX) return;
|
|
|
|
break;
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_DJB2:
|
2005-01-21 22:42:33 +02:00
|
|
|
djb2_state_dump((djb2_state_t *)state, &algobuf, buflen);
|
2004-12-23 15:16:30 +02:00
|
|
|
if (*buflen == UINT_MAX) return;
|
|
|
|
break;
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_SDBM:
|
2005-01-21 22:42:33 +02:00
|
|
|
sdbm_state_dump((sdbm_state_t *)state, &algobuf, buflen);
|
2004-12-23 15:16:30 +02:00
|
|
|
if (*buflen == UINT_MAX) return;
|
|
|
|
break;
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_FNV:
|
2005-01-21 22:42:33 +02:00
|
|
|
fnv_state_dump((fnv_state_t *)state, &algobuf, buflen);
|
2004-12-23 15:16:30 +02:00
|
|
|
if (*buflen == UINT_MAX) return;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2006-07-25 18:30:46 +03:00
|
|
|
*buf = (char *)malloc(strlen(cmph_hash_names[state->hashfunc]) + 1 + *buflen);
|
2005-01-18 23:06:08 +02:00
|
|
|
memcpy(*buf, cmph_hash_names[state->hashfunc], strlen(cmph_hash_names[state->hashfunc]) + 1);
|
|
|
|
DEBUGP("Algobuf is %u\n", *(cmph_uint32 *)algobuf);
|
|
|
|
memcpy(*buf + strlen(cmph_hash_names[state->hashfunc]) + 1, algobuf, *buflen);
|
|
|
|
*buflen = (cmph_uint32)strlen(cmph_hash_names[state->hashfunc]) + 1 + *buflen;
|
2004-12-23 15:16:30 +02:00
|
|
|
free(algobuf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-07-29 06:09:31 +03:00
|
|
|
hash_state_t * hash_state_copy(hash_state_t *src_state)
|
|
|
|
{
|
|
|
|
hash_state_t *dest_state = NULL;
|
|
|
|
switch (src_state->hashfunc)
|
|
|
|
{
|
|
|
|
case CMPH_HASH_JENKINS:
|
|
|
|
dest_state = (hash_state_t *)jenkins_state_copy((jenkins_state_t *)src_state);
|
|
|
|
break;
|
|
|
|
case CMPH_HASH_DJB2:
|
|
|
|
dest_state = (hash_state_t *)djb2_state_copy((djb2_state_t *)src_state);
|
|
|
|
break;
|
|
|
|
case CMPH_HASH_SDBM:
|
|
|
|
dest_state = (hash_state_t *)sdbm_state_copy((sdbm_state_t *)src_state);
|
|
|
|
break;
|
|
|
|
case CMPH_HASH_FNV:
|
|
|
|
dest_state = (hash_state_t *)fnv_state_copy((fnv_state_t *)src_state);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
dest_state->hashfunc = src_state->hashfunc;
|
|
|
|
return dest_state;
|
|
|
|
}
|
|
|
|
|
2005-01-21 22:42:33 +02:00
|
|
|
hash_state_t *hash_state_load(const char *buf, cmph_uint32 buflen)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
2005-01-18 23:06:08 +02:00
|
|
|
cmph_uint32 i;
|
|
|
|
cmph_uint32 offset;
|
|
|
|
CMPH_HASH hashfunc = CMPH_HASH_COUNT;
|
|
|
|
for (i = 0; i < CMPH_HASH_COUNT; ++i)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
2005-01-18 23:06:08 +02:00
|
|
|
if (strcmp(buf, cmph_hash_names[i]) == 0)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
|
|
|
hashfunc = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-01-18 23:06:08 +02:00
|
|
|
if (hashfunc == CMPH_HASH_COUNT) return NULL;
|
|
|
|
offset = (cmph_uint32)strlen(cmph_hash_names[hashfunc]) + 1;
|
2004-12-23 15:16:30 +02:00
|
|
|
switch (hashfunc)
|
|
|
|
{
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_JENKINS:
|
2005-01-21 22:42:33 +02:00
|
|
|
return (hash_state_t *)jenkins_state_load(buf + offset, buflen - offset);
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_DJB2:
|
2005-01-21 22:42:33 +02:00
|
|
|
return (hash_state_t *)djb2_state_load(buf + offset, buflen - offset);
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_SDBM:
|
2005-01-21 22:42:33 +02:00
|
|
|
return (hash_state_t *)sdbm_state_load(buf + offset, buflen - offset);
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_FNV:
|
2005-01-21 22:42:33 +02:00
|
|
|
return (hash_state_t *)fnv_state_load(buf + offset, buflen - offset);
|
2004-12-23 15:16:30 +02:00
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-01-21 22:42:33 +02:00
|
|
|
void hash_state_destroy(hash_state_t *state)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
|
|
|
switch (state->hashfunc)
|
|
|
|
{
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_JENKINS:
|
2005-01-21 22:42:33 +02:00
|
|
|
jenkins_state_destroy((jenkins_state_t *)state);
|
2004-12-23 15:16:30 +02:00
|
|
|
break;
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_DJB2:
|
2005-01-21 22:42:33 +02:00
|
|
|
djb2_state_destroy((djb2_state_t *)state);
|
2004-12-23 15:16:30 +02:00
|
|
|
break;
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_SDBM:
|
2005-01-21 22:42:33 +02:00
|
|
|
sdbm_state_destroy((sdbm_state_t *)state);
|
2004-12-23 15:16:30 +02:00
|
|
|
break;
|
2005-01-18 23:06:08 +02:00
|
|
|
case CMPH_HASH_FNV:
|
2005-01-21 22:42:33 +02:00
|
|
|
fnv_state_destroy((fnv_state_t *)state);
|
2004-12-23 15:16:30 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|