| 1 |
#include "fnv_hash.h" |
| 2 |
#include <stdlib.h> |
| 3 |
|
| 4 |
fnv_state_t *fnv_state_new() |
| 5 |
{ |
| 6 |
fnv_state_t *state = (fnv_state_t *)malloc(sizeof(fnv_state_t)); |
| 7 |
state->hashfunc = CMPH_HASH_FNV; |
| 8 |
return state; |
| 9 |
} |
| 10 |
|
| 11 |
void fnv_state_destroy(fnv_state_t *state) |
| 12 |
{ |
| 13 |
free(state); |
| 14 |
} |
| 15 |
|
| 16 |
cmph_uint32 fnv_hash(fnv_state_t *state, const char *k, cmph_uint32 keylen) |
| 17 |
{ |
| 18 |
const unsigned char *bp = (const unsigned char *)k; |
| 19 |
const unsigned char *be = bp + keylen; |
| 20 |
static unsigned int hval = 0; |
| 21 |
|
| 22 |
while (bp < be) |
| 23 |
{ |
| 24 |
|
| 25 |
//hval *= 0x01000193; good for non-gcc compiler |
| 26 |
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24); //good for gcc |
| 27 |
|
| 28 |
hval ^= *bp++; |
| 29 |
} |
| 30 |
return hval; |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
void fnv_state_dump(fnv_state_t *state, char **buf, cmph_uint32 *buflen) |
| 35 |
{ |
| 36 |
*buf = NULL; |
| 37 |
*buflen = 0; |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
fnv_state_t * fnv_state_copy(fnv_state_t *src_state) |
| 42 |
{ |
| 43 |
fnv_state_t *dest_state = (fnv_state_t *)malloc(sizeof(fnv_state_t)); |
| 44 |
dest_state->hashfunc = src_state->hashfunc; |
| 45 |
return dest_state; |
| 46 |
} |
| 47 |
|
| 48 |
fnv_state_t *fnv_state_load(const char *buf, cmph_uint32 buflen) |
| 49 |
{ |
| 50 |
fnv_state_t *state = (fnv_state_t *)malloc(sizeof(fnv_state_t)); |
| 51 |
state->hashfunc = CMPH_HASH_FNV; |
| 52 |
return state; |
| 53 |
} |