Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | #include "curl_setup.h" |
25 | | |
26 | | #include <stddef.h> /* for offsetof() */ |
27 | | |
28 | | #include "hash.h" |
29 | | |
30 | | /* random patterns for API verification */ |
31 | | #ifdef DEBUGBUILD |
32 | 173k | #define HASHINIT 0x7017e781 |
33 | 43.6k | #define ITERINIT 0x5FEDCBA9 |
34 | | #endif |
35 | | |
36 | | |
37 | | typedef size_t (*hash_function)(const void *key, |
38 | | size_t key_length, |
39 | | size_t slots_num); |
40 | | |
41 | | typedef size_t (*comp_function)(const void *key1, |
42 | | size_t key1_len, |
43 | | const void *key2, |
44 | | size_t key2_len); |
45 | | |
46 | | /* Curl_hash stores its type in a uint8_t. */ |
47 | | typedef char hash_types_fit_uint8[ |
48 | | ((CURL_HASH_TYPE_LAST - 1) <= UINT8_MAX) ? 1 : -1]; |
49 | | |
50 | | struct hash_functions { |
51 | | hash_function hash; |
52 | | comp_function compare; |
53 | | }; |
54 | | |
55 | | /* @unittest 1603 |
56 | | */ |
57 | | UNITTEST size_t hash_str(const void *key, size_t key_length, size_t slots_num); |
58 | | UNITTEST size_t hash_str(const void *key, size_t key_length, size_t slots_num) |
59 | 117k | { |
60 | 117k | const char *key_str = (const char *)key; |
61 | 117k | const char *end = key_str + key_length; |
62 | 117k | size_t h = 5381; |
63 | | |
64 | 5.78M | while(key_str < end) { |
65 | 5.66M | size_t j = (size_t)*key_str++; |
66 | 5.66M | h += h << 5; |
67 | 5.66M | h ^= j; |
68 | 5.66M | } |
69 | | |
70 | 117k | return (h % slots_num); |
71 | 117k | } |
72 | | |
73 | | /* Avoid treating the variable-length key[1] as a one-byte object. */ |
74 | | static char *hash_elem_key(struct Curl_hash_element *he) |
75 | 107k | { |
76 | 107k | return (char *)he + offsetof(struct Curl_hash_element, key); |
77 | 107k | } |
78 | | |
79 | | static size_t hash_socket(const void *key, size_t key_length, |
80 | | size_t slots_num) |
81 | 0 | { |
82 | 0 | curl_socket_t socket; |
83 | |
|
84 | 0 | DEBUGASSERT(key_length == sizeof(socket)); |
85 | 0 | if(key_length != sizeof(socket)) |
86 | 0 | return 0; |
87 | 0 | memcpy(&socket, key, sizeof(socket)); |
88 | 0 | return (size_t)(socket % (curl_socket_t)slots_num); |
89 | 0 | } |
90 | | |
91 | | static size_t compare_socket(const void *key1, size_t key1_len, |
92 | | const void *key2, size_t key2_len) |
93 | 0 | { |
94 | 0 | curl_socket_t socket1; |
95 | 0 | curl_socket_t socket2; |
96 | |
|
97 | 0 | if((key1_len != sizeof(socket1)) || (key2_len != sizeof(socket2))) |
98 | 0 | return 0; |
99 | 0 | memcpy(&socket1, key1, sizeof(socket1)); |
100 | 0 | memcpy(&socket2, key2, sizeof(socket2)); |
101 | 0 | return socket1 == socket2; |
102 | 0 | } |
103 | | |
104 | | static size_t compare_bytes(const void *key1, size_t key1_len, |
105 | | const void *key2, size_t key2_len) |
106 | 63.8k | { |
107 | 63.8k | return (key1_len == key2_len) && !memcmp(key1, key2, key1_len); |
108 | 63.8k | } |
109 | | |
110 | | static const struct hash_functions hash_functions[] = { |
111 | | { hash_str, compare_bytes }, |
112 | | { hash_socket, compare_socket } |
113 | | }; |
114 | | |
115 | | static const struct hash_functions *hash_get_functions(uint8_t type) |
116 | 117k | { |
117 | 117k | DEBUGASSERT(CURL_ARRAYSIZE(hash_functions) == CURL_HASH_TYPE_LAST); |
118 | 117k | if((unsigned int)type >= CURL_HASH_TYPE_LAST) |
119 | 0 | type = CURL_HASH_TYPE_BYTES; |
120 | 117k | return &hash_functions[type]; |
121 | 117k | } |
122 | | |
123 | | static bool hash_key_is_valid(uint8_t type, size_t key_len) |
124 | 245k | { |
125 | 245k | return (type == CURL_HASH_TYPE_BYTES) || |
126 | 23.3k | ((type == CURL_HASH_TYPE_SOCKET) && |
127 | 23.3k | (key_len == sizeof(curl_socket_t))); |
128 | 245k | } |
129 | | |
130 | | #if 0 /* useful function for debugging hashes and their contents */ |
131 | | void Curl_hash_print(struct Curl_hash *h, void (*func)(void *)) |
132 | | { |
133 | | struct Curl_hash_iterator iter; |
134 | | struct Curl_hash_element *he; |
135 | | size_t last_index = UINT_MAX; |
136 | | |
137 | | if(!h) |
138 | | return; |
139 | | |
140 | | curl_mfprintf(stderr, "=Hash dump=\n"); |
141 | | |
142 | | Curl_hash_start_iterate(h, &iter); |
143 | | |
144 | | he = Curl_hash_next_element(&iter); |
145 | | while(he) { |
146 | | if(iter.slot_index != last_index) { |
147 | | curl_mfprintf(stderr, "index %d:", (int)iter.slot_index); |
148 | | if(last_index != UINT_MAX) { |
149 | | curl_mfprintf(stderr, "\n"); |
150 | | } |
151 | | last_index = iter.slot_index; |
152 | | } |
153 | | |
154 | | if(func) |
155 | | func(he->ptr); |
156 | | else |
157 | | curl_mfprintf(stderr, " [key=%.*s, he=%p, ptr=%p]", |
158 | | (int)he->key_len, hash_elem_key(he), |
159 | | (void *)he, (void *)he->ptr); |
160 | | |
161 | | he = Curl_hash_next_element(&iter); |
162 | | } |
163 | | curl_mfprintf(stderr, "\n"); |
164 | | } |
165 | | #endif |
166 | | |
167 | | /* Initializes a hash structure. |
168 | | * |
169 | | * @unittest: 1602 |
170 | | * @unittest: 1603 |
171 | | */ |
172 | | void Curl_hash_init(struct Curl_hash *h, |
173 | | size_t slots, |
174 | | Curl_hash_type type, |
175 | | Curl_hash_dtor dtor) |
176 | 173k | { |
177 | 173k | DEBUGASSERT(h); |
178 | 173k | DEBUGASSERT(slots); |
179 | 173k | DEBUGASSERT((unsigned int)type < CURL_HASH_TYPE_LAST); |
180 | 173k | DEBUGASSERT(dtor); |
181 | | |
182 | 173k | h->table = NULL; |
183 | 173k | h->dtor = dtor; |
184 | 173k | h->size = 0; |
185 | 173k | h->slots = slots; |
186 | 173k | h->type = (uint8_t)type; |
187 | 173k | #ifdef DEBUGBUILD |
188 | 173k | h->init = HASHINIT; |
189 | 173k | #endif |
190 | 173k | } |
191 | | |
192 | | static struct Curl_hash_element *hash_elem_create(const void *key, |
193 | | size_t key_len, |
194 | | void *p, |
195 | | Curl_hash_elem_dtor dtor) |
196 | 43.2k | { |
197 | 43.2k | struct Curl_hash_element *he; |
198 | | |
199 | | /* allocate the struct plus memory after it to store the key */ |
200 | 43.2k | he = curlx_malloc(sizeof(struct Curl_hash_element) + key_len); |
201 | 43.2k | if(he) { |
202 | 43.2k | he->next = NULL; |
203 | | /* copy the key */ |
204 | 43.2k | memcpy(hash_elem_key(he), key, key_len); |
205 | 43.2k | he->key_len = key_len; |
206 | 43.2k | he->ptr = p; |
207 | 43.2k | he->dtor = dtor; |
208 | 43.2k | } |
209 | 43.2k | return he; |
210 | 43.2k | } |
211 | | |
212 | | static void hash_elem_clear_ptr(struct Curl_hash *h, |
213 | | struct Curl_hash_element *he) |
214 | 43.2k | { |
215 | 43.2k | DEBUGASSERT(h); |
216 | 43.2k | DEBUGASSERT(he); |
217 | 43.2k | if(he->ptr) { |
218 | 43.2k | if(he->dtor) |
219 | 0 | he->dtor(hash_elem_key(he), he->key_len, he->ptr); |
220 | 43.2k | else |
221 | 43.2k | h->dtor(he->ptr); |
222 | 43.2k | he->ptr = NULL; |
223 | 43.2k | } |
224 | 43.2k | } |
225 | | |
226 | | static void hash_elem_destroy(struct Curl_hash *h, |
227 | | struct Curl_hash_element *he) |
228 | 43.2k | { |
229 | 43.2k | hash_elem_clear_ptr(h, he); |
230 | 43.2k | curlx_free(he); |
231 | 43.2k | } |
232 | | |
233 | | static void hash_elem_unlink(struct Curl_hash *h, |
234 | | struct Curl_hash_element **he_anchor, |
235 | | struct Curl_hash_element *he) |
236 | 43.2k | { |
237 | 43.2k | *he_anchor = he->next; |
238 | 43.2k | --h->size; |
239 | 43.2k | } |
240 | | |
241 | | static void hash_elem_link(struct Curl_hash *h, |
242 | | struct Curl_hash_element **he_anchor, |
243 | | struct Curl_hash_element *he) |
244 | 43.2k | { |
245 | 43.2k | he->next = *he_anchor; |
246 | 43.2k | *he_anchor = he; |
247 | 43.2k | ++h->size; |
248 | 43.2k | } |
249 | | |
250 | | void *Curl_hash_add2(struct Curl_hash *h, |
251 | | const void *key, size_t key_len, void *p, |
252 | | Curl_hash_elem_dtor dtor) |
253 | 43.2k | { |
254 | 43.2k | const struct hash_functions *functions; |
255 | 43.2k | struct Curl_hash_element *he, **slot; |
256 | | |
257 | 43.2k | DEBUGASSERT(h); |
258 | 43.2k | DEBUGASSERT(h->slots); |
259 | 43.2k | DEBUGASSERT(h->init == HASHINIT); |
260 | 43.2k | if(!hash_key_is_valid(h->type, key_len)) |
261 | 0 | return NULL; |
262 | 43.2k | if(!h->table) { |
263 | 37.7k | h->table = curlx_calloc(h->slots, sizeof(struct Curl_hash_element *)); |
264 | 37.7k | if(!h->table) |
265 | 0 | return NULL; /* OOM */ |
266 | 37.7k | } |
267 | | |
268 | 43.2k | functions = hash_get_functions(h->type); |
269 | 43.2k | slot = &h->table[functions->hash(key, key_len, h->slots)]; |
270 | 43.2k | for(he = *slot; he; he = he->next) { |
271 | 17 | if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) { |
272 | | /* existing key entry, overwrite by clearing old pointer */ |
273 | 0 | hash_elem_clear_ptr(h, he); |
274 | 0 | he->ptr = p; |
275 | 0 | he->dtor = dtor; |
276 | 0 | return p; |
277 | 0 | } |
278 | 17 | } |
279 | | |
280 | 43.2k | he = hash_elem_create(key, key_len, p, dtor); |
281 | 43.2k | if(!he) |
282 | 0 | return NULL; /* OOM */ |
283 | | |
284 | 43.2k | hash_elem_link(h, slot, he); |
285 | 43.2k | return p; /* return the new entry */ |
286 | 43.2k | } |
287 | | |
288 | | /* Insert the data in the hash. If there already was a match in the hash, that |
289 | | * data is replaced. This function also "lazily" allocates the table if |
290 | | * needed, as it is not done in the _init function (anymore). |
291 | | * |
292 | | * @unittest: 1305 |
293 | | * @unittest: 1602 |
294 | | * @unittest: 1603 |
295 | | */ |
296 | | void *Curl_hash_add(struct Curl_hash *h, |
297 | | const void *key, size_t key_len, void *p) |
298 | 43.2k | { |
299 | 43.2k | return Curl_hash_add2(h, key, key_len, p, NULL); |
300 | 43.2k | } |
301 | | |
302 | | /* Remove the identified hash entry. |
303 | | * Returns non-zero on failure. |
304 | | * |
305 | | * @unittest: 1603 |
306 | | */ |
307 | | int Curl_hash_delete(struct Curl_hash *h, const void *key, size_t key_len) |
308 | 72.0k | { |
309 | 72.0k | const struct hash_functions *functions; |
310 | | |
311 | 72.0k | DEBUGASSERT(h); |
312 | 72.0k | DEBUGASSERT(h->slots); |
313 | 72.0k | DEBUGASSERT(h->init == HASHINIT); |
314 | 72.0k | if(!hash_key_is_valid(h->type, key_len)) |
315 | 0 | return 1; |
316 | 72.0k | if(h->table) { |
317 | 24.3k | struct Curl_hash_element *he, **he_anchor; |
318 | | |
319 | 24.3k | functions = hash_get_functions(h->type); |
320 | 24.3k | he_anchor = &h->table[functions->hash(key, key_len, h->slots)]; |
321 | 24.3k | while(*he_anchor) { |
322 | 24.3k | he = *he_anchor; |
323 | 24.3k | if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) { |
324 | 24.3k | hash_elem_unlink(h, he_anchor, he); |
325 | 24.3k | hash_elem_destroy(h, he); |
326 | 24.3k | return 0; |
327 | 24.3k | } |
328 | 1 | he_anchor = &he->next; |
329 | 1 | } |
330 | 24.3k | } |
331 | 47.7k | return 1; |
332 | 72.0k | } |
333 | | |
334 | | /* Retrieves a hash element. |
335 | | * |
336 | | * @unittest: 1603 |
337 | | */ |
338 | | void *Curl_hash_pick(struct Curl_hash *h, const void *key, size_t key_len) |
339 | 130k | { |
340 | 130k | const struct hash_functions *functions; |
341 | | |
342 | 130k | DEBUGASSERT(h); |
343 | 130k | DEBUGASSERT(h->init == HASHINIT); |
344 | 130k | if(!hash_key_is_valid(h->type, key_len)) |
345 | 0 | return NULL; |
346 | 130k | if(h->table) { |
347 | 50.3k | struct Curl_hash_element *he; |
348 | 50.3k | DEBUGASSERT(h->slots); |
349 | 50.3k | functions = hash_get_functions(h->type); |
350 | 50.3k | he = h->table[functions->hash(key, key_len, h->slots)]; |
351 | 50.3k | while(he) { |
352 | 39.5k | if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) { |
353 | 39.4k | return he->ptr; |
354 | 39.4k | } |
355 | 30 | he = he->next; |
356 | 30 | } |
357 | 50.3k | } |
358 | 90.8k | return NULL; |
359 | 130k | } |
360 | | |
361 | | /* Destroys all the entries in the given hash and resets its attributes, |
362 | | * prepping the given hash for [static|dynamic] deallocation. |
363 | | * |
364 | | * @unittest: 1305 |
365 | | * @unittest: 1602 |
366 | | * @unittest: 1603 |
367 | | */ |
368 | | void Curl_hash_destroy(struct Curl_hash *h) |
369 | 173k | { |
370 | 173k | DEBUGASSERT(h->init == HASHINIT); |
371 | 173k | if(h->table) { |
372 | 37.7k | Curl_hash_clean(h); |
373 | 37.7k | curlx_safefree(h->table); |
374 | 37.7k | } |
375 | 173k | DEBUGASSERT(h->size == 0); |
376 | 173k | h->slots = 0; |
377 | 173k | } |
378 | | |
379 | | /* Removes all the entries in the given hash. |
380 | | * |
381 | | * @unittest: 1602 |
382 | | */ |
383 | | void Curl_hash_clean(struct Curl_hash *h) |
384 | 37.7k | { |
385 | 37.7k | if(h && h->table) { |
386 | 37.7k | struct Curl_hash_element *he, **he_anchor; |
387 | 37.7k | size_t i; |
388 | 37.7k | DEBUGASSERT(h->init == HASHINIT); |
389 | 3.21M | for(i = 0; i < h->slots; ++i) { |
390 | 3.17M | he_anchor = &h->table[i]; |
391 | 3.19M | while(*he_anchor) { |
392 | 18.9k | he = *he_anchor; |
393 | 18.9k | hash_elem_unlink(h, he_anchor, he); |
394 | 18.9k | hash_elem_destroy(h, he); |
395 | 18.9k | } |
396 | 3.17M | } |
397 | 37.7k | } |
398 | 37.7k | } |
399 | | |
400 | | size_t Curl_hash_count(struct Curl_hash *h) |
401 | 33.3k | { |
402 | 33.3k | DEBUGASSERT(h->init == HASHINIT); |
403 | 33.3k | return h->size; |
404 | 33.3k | } |
405 | | |
406 | | /* Cleans all entries that pass the comp function criteria. */ |
407 | | void Curl_hash_clean_with_criterium(struct Curl_hash *h, void *user, |
408 | | int (*comp)(void *, void *)) |
409 | 33.3k | { |
410 | 33.3k | size_t i; |
411 | | |
412 | 33.3k | if(!h || !h->table) |
413 | 762 | return; |
414 | | |
415 | 32.5k | DEBUGASSERT(h->init == HASHINIT); |
416 | 2.34M | for(i = 0; i < h->slots; ++i) { |
417 | 2.31M | struct Curl_hash_element *he, **he_anchor = &h->table[i]; |
418 | 2.34M | while(*he_anchor) { |
419 | | /* ask the callback function if we shall remove this entry or not */ |
420 | 32.8k | if(!comp || comp(user, (*he_anchor)->ptr)) { |
421 | 26 | he = *he_anchor; |
422 | 26 | hash_elem_unlink(h, he_anchor, he); |
423 | 26 | hash_elem_destroy(h, he); |
424 | 26 | } |
425 | 32.8k | else |
426 | 32.8k | he_anchor = &(*he_anchor)->next; |
427 | 32.8k | } |
428 | 2.31M | } |
429 | 32.5k | } |
430 | | |
431 | | void Curl_hash_start_iterate(struct Curl_hash *hash, |
432 | | struct Curl_hash_iterator *iter) |
433 | 43.6k | { |
434 | 43.6k | DEBUGASSERT(hash->init == HASHINIT); |
435 | 43.6k | iter->hash = hash; |
436 | 43.6k | iter->slot_index = 0; |
437 | 43.6k | iter->current = NULL; |
438 | 43.6k | #ifdef DEBUGBUILD |
439 | 43.6k | iter->init = ITERINIT; |
440 | 43.6k | #endif |
441 | 43.6k | } |
442 | | |
443 | | struct Curl_hash_element *Curl_hash_next_element( |
444 | | struct Curl_hash_iterator *iter) |
445 | 43.6k | { |
446 | 43.6k | struct Curl_hash *h; |
447 | 43.6k | DEBUGASSERT(iter->init == ITERINIT); |
448 | 43.6k | h = iter->hash; |
449 | 43.6k | if(!h->table) |
450 | 22.9k | return NULL; /* empty hash, nothing to return */ |
451 | | |
452 | | /* Get the next element in the current list, if any */ |
453 | 20.6k | if(iter->current) |
454 | 53 | iter->current = iter->current->next; |
455 | | |
456 | | /* If we have reached the end of the list, find the next one */ |
457 | 20.6k | if(!iter->current) { |
458 | 20.6k | size_t i; |
459 | 1.97M | for(i = iter->slot_index; i < h->slots; i++) { |
460 | 1.95M | if(h->table[i]) { |
461 | 1.62k | iter->current = h->table[i]; |
462 | 1.62k | iter->slot_index = i + 1; |
463 | 1.62k | break; |
464 | 1.62k | } |
465 | 1.95M | } |
466 | 20.6k | } |
467 | | |
468 | 20.6k | return iter->current; |
469 | 43.6k | } |