diff options
author | Uros Majstorovic <majstor@majstor.org> | 2017-05-03 21:10:08 +0200 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2017-05-03 21:10:08 +0200 |
commit | 25de5e761daab8b897a4f09ff8503e6f43c299f9 (patch) | |
tree | a9a1c185c7f89d67f9250e42b2aa53eefc9a6770 /code/htable/htable.c |
initial commit
Diffstat (limited to 'code/htable/htable.c')
-rw-r--r-- | code/htable/htable.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/code/htable/htable.c b/code/htable/htable.c new file mode 100644 index 0000000..4a9ee11 --- /dev/null +++ b/code/htable/htable.c @@ -0,0 +1,57 @@ +#include <core.h> + +#include <string.h> + +#include "hashtable.h" +#include "hashtable_private.h" + +static unsigned int hash_fn(void *k) { + return *((unsigned int *)k); +} + +static int eq_fn(void *k1, void *k2) { + return !memcmp(k1, k2, ECP_ECDH_SIZE_KEY); +} + +static void *h_create(ECPContext *ctx) { + int rv; + struct hashtable *h = malloc(sizeof(struct hashtable)); + if (h == NULL) return NULL; + + rv = create_hashtable(h, 1000, (unsigned int (*)(void *))ctx->cr.dh_pub_hash_fn, (int (*)(void *, void *))ctx->cr.dh_pub_hash_eq, NULL, NULL, NULL); + if (!rv) { + free(h); + return NULL; + } + + return h; +} + +static void h_destroy(void *h) { + hashtable_destroy(h); + free(h); +} + +static int h_insert(void *h, unsigned char *k, ECPConnection *v) { + int rv = hashtable_insert(h, k, v); + if (!rv) return ECP_ERR; + return ECP_OK; +} + +static ECPConnection *h_remove(void *h, unsigned char *k) { + return hashtable_remove(h, k); +} + +static ECPConnection *h_search(void *h, unsigned char *k) { + return hashtable_search(h, k); +} + +int ecp_htable_init(ECPHTableIface *h) { + h->init = 1; + h->create = h_create; + h->destroy = h_destroy; + h->insert = h_insert; + h->remove = h_remove; + h->search = h_search; + return 0; +} |