Blame


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