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