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"
26 #include "table.h"
28 static void *hash_alloc(size_t, void *);
29 static void *hash_calloc(size_t, size_t, void *);
30 static void hash_free(void *, void *);
32 static int table_static_open(struct table *);
33 static int table_static_add(struct table *, const char *, const char *);
34 static int table_static_lookup(struct table *, const char *, char **);
35 static void table_static_close(struct table *);
37 struct table_backend table_static = {
38 "static",
39 table_static_open,
40 table_static_add,
41 table_static_lookup,
42 table_static_close,
43 };
45 struct kp {
46 char *val;
47 char key[];
48 };
50 static void *
51 hash_alloc(size_t len, void *d)
52 {
53 return xmalloc(len);
54 }
56 static void *
57 hash_calloc(size_t nmemb, size_t size, void *d)
58 {
59 return xcalloc(nmemb, size);
60 }
62 static void
63 hash_free(void *ptr, void *d)
64 {
65 free(ptr);
66 }
68 static int
69 table_static_open(struct table *t)
70 {
71 struct ohash_info info = {
72 .key_offset = offsetof(struct kp, key),
73 .calloc = hash_calloc,
74 .free = hash_free,
75 .alloc = hash_alloc,
76 };
78 t->t_handle = xmalloc(sizeof(struct ohash));
79 ohash_init(t->t_handle, 5, &info);
80 return 0;
81 }
83 int
84 table_static_add(struct table *t, const char *key, const char *val)
85 {
86 struct kp *kp;
87 unsigned int slot;
89 if (key == NULL)
90 return -1;
92 kp = xcalloc(1, sizeof(*kp) + strlen(key) + 1);
93 strcpy(kp->key, key);
94 if (val != NULL)
95 kp->val = xstrdup(val);
97 slot = ohash_qlookup(t->t_handle, kp->key);
98 ohash_insert(t->t_handle, slot, kp);
100 return 0;
103 int
104 table_static_lookup(struct table *t, const char *key, char **ret_val)
106 struct kp *kp;
107 unsigned int slot;
109 slot = ohash_qlookup(t->t_handle, key);
110 if ((kp = ohash_find(t->t_handle, slot)) == NULL)
111 return -1;
113 *ret_val = xstrdup(kp->val);
114 return 0;
117 static void
118 table_static_close(struct table *t)
120 struct kp *kp;
121 unsigned int i;
123 for (kp = ohash_first(t->t_handle, &i);
124 kp != NULL;
125 kp = ohash_next(t->t_handle, &i)) {
126 ohash_remove(t->t_handle, i);
127 free(kp->val);
128 free(kp);
131 free(t->t_handle);