Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "compat.h"
19 #include <stddef.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "utils.h"
25 #include "kamid.h"
27 static void *hash_alloc(size_t, void *);
28 static void *hash_calloc(size_t, size_t, void *);
29 static void hash_free(void *, void *);
31 static int table_static_open(struct table *);
32 static int table_static_add(struct table *, const char *, const char *);
33 static int table_static_lookup(struct table *, const char *, char **);
34 static void table_static_close(struct table *);
36 struct table_backend table_static = {
37 "static",
38 table_static_open,
39 table_static_add,
40 table_static_lookup,
41 table_static_close,
42 };
44 struct kp {
45 char *val;
46 char key[];
47 };
49 static void *
50 hash_alloc(size_t len, void *d)
51 {
52 return xmalloc(len);
53 }
55 static void *
56 hash_calloc(size_t nmemb, size_t size, void *d)
57 {
58 return xcalloc(nmemb, size);
59 }
61 static void
62 hash_free(void *ptr, void *d)
63 {
64 free(ptr);
65 }
67 static int
68 table_static_open(struct table *t)
69 {
70 struct ohash_info info = {
71 .key_offset = offsetof(struct kp, key),
72 .calloc = hash_calloc,
73 .free = hash_free,
74 .alloc = hash_alloc,
75 };
77 t->t_handle = xmalloc(sizeof(struct ohash));
78 ohash_init(t->t_handle, 5, &info);
79 return 0;
80 }
82 int
83 table_static_add(struct table *t, const char *key, const char *val)
84 {
85 struct kp *kp;
86 unsigned int slot;
88 if (key == NULL)
89 return -1;
91 kp = xcalloc(1, sizeof(*kp) + strlen(key) + 1);
92 strcpy(kp->key, key);
93 if (val != NULL)
94 kp->val = xstrdup(val);
96 slot = ohash_qlookup(t->t_handle, kp->key);
97 ohash_insert(t->t_handle, slot, kp);
99 return 0;
102 int
103 table_static_lookup(struct table *t, const char *key, char **ret_val)
105 struct kp *kp;
106 unsigned int slot;
108 slot = ohash_qlookup(t->t_handle, key);
109 if ((kp = ohash_find(t->t_handle, slot)) == NULL)
110 return -1;
112 *ret_val = xstrdup(kp->val);
113 return 0;
116 static void
117 table_static_close(struct table *t)
119 struct kp *kp;
120 unsigned int i;
122 for (kp = ohash_first(t->t_handle, &i);
123 kp != NULL;
124 kp = ohash_next(t->t_handle, &i)) {
125 ohash_remove(t->t_handle, i);
126 free(kp->key);
127 free(kp->val);
128 free(kp);
131 free(t->t_handle);