1 /* MIT License
2 *
3 * Copyright (c) 2023 Brad House
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * SPDX-License-Identifier: MIT
25 */
26 #include "ares_setup.h"
27 #include "ares.h"
28 #include "ares_private.h"
29 #include "ares__htable.h"
30 #include "ares__htable_strvp.h"
31
32 struct ares__htable_strvp {
33 ares__htable_strvp_val_free_t free_val;
34 ares__htable_t *hash;
35 };
36
37 typedef struct {
38 char *key;
39 void *val;
40 ares__htable_strvp_t *parent;
41 } ares__htable_strvp_bucket_t;
42
ares__htable_strvp_destroy(ares__htable_strvp_t * htable)43 void ares__htable_strvp_destroy(ares__htable_strvp_t *htable)
44 {
45 if (htable == NULL) {
46 return;
47 }
48
49 ares__htable_destroy(htable->hash);
50 ares_free(htable);
51 }
52
hash_func(const void * key,unsigned int seed)53 static unsigned int hash_func(const void *key, unsigned int seed)
54 {
55 const char *arg = key;
56 return ares__htable_hash_FNV1a_casecmp((const unsigned char *)arg,
57 ares_strlen(arg), seed);
58 }
59
bucket_key(const void * bucket)60 static const void *bucket_key(const void *bucket)
61 {
62 const ares__htable_strvp_bucket_t *arg = bucket;
63 return arg->key;
64 }
65
bucket_free(void * bucket)66 static void bucket_free(void *bucket)
67 {
68 ares__htable_strvp_bucket_t *arg = bucket;
69
70 if (arg->parent->free_val) {
71 arg->parent->free_val(arg->val);
72 }
73 ares_free(arg->key);
74 ares_free(arg);
75 }
76
key_eq(const void * key1,const void * key2)77 static ares_bool_t key_eq(const void *key1, const void *key2)
78 {
79 const char *k1 = key1;
80 const char *k2 = key2;
81
82 if (strcasecmp(k1, k2) == 0) {
83 return ARES_TRUE;
84 }
85
86 return ARES_FALSE;
87 }
88
89 ares__htable_strvp_t *
ares__htable_strvp_create(ares__htable_strvp_val_free_t val_free)90 ares__htable_strvp_create(ares__htable_strvp_val_free_t val_free)
91 {
92 ares__htable_strvp_t *htable = ares_malloc(sizeof(*htable));
93 if (htable == NULL) {
94 goto fail;
95 }
96
97 htable->hash =
98 ares__htable_create(hash_func, bucket_key, bucket_free, key_eq);
99 if (htable->hash == NULL) {
100 goto fail;
101 }
102
103 htable->free_val = val_free;
104
105 return htable;
106
107 fail:
108 if (htable) {
109 ares__htable_destroy(htable->hash);
110 ares_free(htable);
111 }
112 return NULL;
113 }
114
ares__htable_strvp_insert(ares__htable_strvp_t * htable,const char * key,void * val)115 ares_bool_t ares__htable_strvp_insert(ares__htable_strvp_t *htable,
116 const char *key, void *val)
117 {
118 ares__htable_strvp_bucket_t *bucket = NULL;
119
120 if (htable == NULL || key == NULL) {
121 goto fail;
122 }
123
124 bucket = ares_malloc(sizeof(*bucket));
125 if (bucket == NULL) {
126 goto fail;
127 }
128
129 bucket->parent = htable;
130 bucket->key = ares_strdup(key);
131 if (bucket->key == NULL) {
132 goto fail;
133 }
134 bucket->val = val;
135
136 if (!ares__htable_insert(htable->hash, bucket)) {
137 goto fail;
138 }
139
140 return ARES_TRUE;
141
142 fail:
143 if (bucket) {
144 ares_free(bucket->key);
145 ares_free(bucket);
146 }
147 return ARES_FALSE;
148 }
149
ares__htable_strvp_get(const ares__htable_strvp_t * htable,const char * key,void ** val)150 ares_bool_t ares__htable_strvp_get(const ares__htable_strvp_t *htable,
151 const char *key, void **val)
152 {
153 ares__htable_strvp_bucket_t *bucket = NULL;
154
155 if (val) {
156 *val = NULL;
157 }
158
159 if (htable == NULL || key == NULL) {
160 return ARES_FALSE;
161 }
162
163 bucket = ares__htable_get(htable->hash, key);
164 if (bucket == NULL) {
165 return ARES_FALSE;
166 }
167
168 if (val) {
169 *val = bucket->val;
170 }
171 return ARES_TRUE;
172 }
173
ares__htable_strvp_get_direct(const ares__htable_strvp_t * htable,const char * key)174 void *ares__htable_strvp_get_direct(const ares__htable_strvp_t *htable,
175 const char *key)
176 {
177 void *val = NULL;
178 ares__htable_strvp_get(htable, key, &val);
179 return val;
180 }
181
ares__htable_strvp_remove(ares__htable_strvp_t * htable,const char * key)182 ares_bool_t ares__htable_strvp_remove(ares__htable_strvp_t *htable,
183 const char *key)
184 {
185 if (htable == NULL) {
186 return ARES_FALSE;
187 }
188
189 return ares__htable_remove(htable->hash, key);
190 }
191
ares__htable_strvp_num_keys(const ares__htable_strvp_t * htable)192 size_t ares__htable_strvp_num_keys(const ares__htable_strvp_t *htable)
193 {
194 if (htable == NULL) {
195 return 0;
196 }
197 return ares__htable_num_keys(htable->hash);
198 }
199