c: Implement the C implementation of the bhash module
This commit is contained in:
46
c/bhash/inc/bhash.h
Normal file
46
c/bhash/inc/bhash.h
Normal file
@ -0,0 +1,46 @@
|
||||
#ifndef BHASH_H_INCLUDED
|
||||
#define BHASH_H_INCLUDED
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define BHASH_MOD32_ERR UINT32_MAX
|
||||
|
||||
typedef struct bhash bhash_t;
|
||||
|
||||
struct bhash
|
||||
{
|
||||
uint8_t *bh_data; /* Current data buffer */
|
||||
size_t bh_len; /* Data length */
|
||||
double bh_bits_avail; /* Bits available */
|
||||
double bh_bits_used; /* Bits used */
|
||||
};
|
||||
|
||||
/*
|
||||
* Initialize new instance of Bhash
|
||||
*/
|
||||
void bhash_init(bhash_t *bh, const void *data, size_t data_len);
|
||||
|
||||
/*
|
||||
* Destroy a bhash object and its associated data
|
||||
*/
|
||||
void bhash_fini(bhash_t *bh);
|
||||
|
||||
/*
|
||||
* Perform a divison of the whole bhash data using the 32-bit `mod` variable
|
||||
* and return the 32-bit reminder.
|
||||
*
|
||||
* This function returns BHASH_MOD32_ERR on error.
|
||||
*/
|
||||
uint32_t bhash_mod32(bhash_t *bh, uint32_t mod);
|
||||
|
||||
/*
|
||||
* Return the number of total bits available in hash
|
||||
*/
|
||||
double bhash_bits_avail(bhash_t *bh);
|
||||
|
||||
/*
|
||||
* Return the number of bits used in bhash
|
||||
*/
|
||||
double bhash_bits_used(bhash_t *bh);
|
||||
|
||||
#endif /* BHASH_H_INCLUDED */
|
||||
12
c/bhash/meson.build
Normal file
12
c/bhash/meson.build
Normal file
@ -0,0 +1,12 @@
|
||||
|
||||
cc = meson.get_compiler('c')
|
||||
|
||||
bhash_inc = include_directories('inc')
|
||||
|
||||
bhash_lib = static_library(
|
||||
'bhash',
|
||||
['src/bhash.c'],
|
||||
include_directories : bhash_inc,
|
||||
dependencies: [ cc.find_library('m', required: false) ])
|
||||
|
||||
bhash_dep = declare_dependency(link_with : bhash_lib, include_directories : bhash_inc)
|
||||
67
c/bhash/src/bhash.c
Normal file
67
c/bhash/src/bhash.c
Normal file
@ -0,0 +1,67 @@
|
||||
#include <arpa/inet.h> /* For htonl() */
|
||||
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "bhash.h"
|
||||
|
||||
void bhash_init(bhash_t *bh, const void *data, size_t data_len)
|
||||
{
|
||||
memset(bh, 0, sizeof(*bh));
|
||||
|
||||
bh->bh_data = malloc(data_len);
|
||||
memcpy(bh->bh_data, data, data_len);
|
||||
bh->bh_len = data_len;
|
||||
|
||||
bh->bh_bits_avail = data_len * 8.0;
|
||||
}
|
||||
|
||||
void bhash_fini(bhash_t *bh)
|
||||
{
|
||||
free(bh->bh_data);
|
||||
}
|
||||
|
||||
uint32_t bhash_mod32(bhash_t *bh, uint32_t mod)
|
||||
{
|
||||
uint32_t *pi;
|
||||
uint32_t *pend;
|
||||
uint64_t n;
|
||||
|
||||
uint32_t rv = 0;
|
||||
|
||||
if ((bh->bh_len % sizeof(uint32_t)) != 0)
|
||||
{
|
||||
return BHASH_MOD32_ERR;
|
||||
}
|
||||
|
||||
pend = (uint32_t *)(bh->bh_data + bh->bh_len);
|
||||
|
||||
for (pi = (uint32_t *)bh->bh_data; pi < pend; pi++)
|
||||
{
|
||||
n = (uint64_t)rv << (sizeof(uint32_t) * 8);
|
||||
n |= htonl(*pi);
|
||||
|
||||
*pi = ntohl(n / mod);
|
||||
rv = n % mod;
|
||||
}
|
||||
|
||||
bh->bh_bits_used += log(mod) / log(2);
|
||||
if (bh->bh_bits_used > bh->bh_bits_avail)
|
||||
{
|
||||
return BHASH_MOD32_ERR;
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
double bhash_bits_avail(bhash_t *bh)
|
||||
{
|
||||
return bh->bh_bits_avail;
|
||||
}
|
||||
double bhash_bits_used(bhash_t *bh)
|
||||
{
|
||||
return bh->bh_bits_used;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user