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 <sys/queue.h>
19 #include <event.h>
20 #include <stddef.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ohash.h>
25 #include <imsg.h>
27 #include "utils.h"
28 #include "kamid.h"
30 static void *hash_alloc(size_t, void *);
31 static void *hash_calloc(size_t, size_t, void *);
32 static void hash_free(void *, void *);
34 static int table_static_open(struct table *);
35 static int table_static_add(struct table *, const char *, const char *);
36 static int table_static_lookup(struct table *, const char *, char **);
37 static void table_static_close(struct table *);
39 struct table_backend table_static = {
40 "static",
41 table_static_open,
42 table_static_add,
43 table_static_lookup,
44 table_static_close,
45 };
47 struct kp {
48 char *val;
49 char key[];
50 };
52 static void *
53 hash_alloc(size_t len, void *d)
54 {
55 return xmalloc(len);
56 }
58 static void *
59 hash_calloc(size_t nmemb, size_t size, void *d)
60 {
61 return xcalloc(nmemb, size);
62 }
64 static void
65 hash_free(void *ptr, void *d)
66 {
67 free(ptr);
68 }
70 static int
71 table_static_open(struct table *t)
72 {
73 struct ohash_info info = {
74 .key_offset = offsetof(struct kp, key),
75 .calloc = hash_calloc,
76 .free = hash_free,
77 .alloc = hash_alloc,
78 };
80 t->t_handle = xmalloc(sizeof(struct ohash));
81 ohash_init(t->t_handle, 5, &info);
82 return 0;
83 }
85 int
86 table_static_add(struct table *t, const char *key, const char *val)
87 {
88 struct kp *kp;
89 unsigned int slot;
91 if (key == NULL)
92 return -1;
94 kp = xcalloc(1, sizeof(*kp) + strlen(key) + 1);
95 strcpy(kp->key, key);
96 if (val != NULL)
97 kp->val = xstrdup(val);
99 slot = ohash_qlookup(t->t_handle, kp->key);
100 ohash_insert(t->t_handle, slot, kp);
102 return 0;
105 int
106 table_static_lookup(struct table *t, const char *key, char **ret_val)
108 struct kp *kp;
109 unsigned int slot;
111 slot = ohash_qlookup(t->t_handle, key);
112 if ((kp = ohash_find(t->t_handle, slot)) == NULL)
113 return -1;
115 *ret_val = xstrdup(kp->val);
116 return 0;
119 static void
120 table_static_close(struct table *t)
122 struct kp *kp;
123 unsigned int i;
125 for (kp = ohash_first(t->t_handle, &i);
126 kp != NULL;
127 kp = ohash_next(t->t_handle, &i)) {
128 ohash_remove(t->t_handle, i);
129 free(kp->key);
130 free(kp->val);
131 free(kp);
134 free(t->t_handle);