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 | 151k | #define HASHINIT 0x7017e781 |
33 | 39.9k | #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 | 88.3k | { |
60 | 88.3k | const char *key_str = (const char *)key; |
61 | 88.3k | const char *end = key_str + key_length; |
62 | 88.3k | size_t h = 5381; |
63 | | |
64 | 9.44M | while(key_str < end) { |
65 | 9.35M | size_t j = (size_t)*key_str++; |
66 | 9.35M | h += h << 5; |
67 | 9.35M | h ^= j; |
68 | 9.35M | } |
69 | | |
70 | 88.3k | return (h % slots_num); |
71 | 88.3k | } |
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 | 85.2k | { |
76 | 85.2k | return (char *)he + offsetof(struct Curl_hash_element, key); |
77 | 85.2k | } |
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 | 46.2k | { |
107 | 46.2k | return (key1_len == key2_len) && !memcmp(key1, key2, key1_len); |
108 | 46.2k | } |
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 | 88.3k | { |
117 | 88.3k | DEBUGASSERT(CURL_ARRAYSIZE(hash_functions) == CURL_HASH_TYPE_LAST); |
118 | 88.3k | if((unsigned int)type >= CURL_HASH_TYPE_LAST) |
119 | 0 | type = CURL_HASH_TYPE_BYTES; |
120 | 88.3k | return &hash_functions[type]; |
121 | 88.3k | } |
122 | | |
123 | | static bool hash_key_is_valid(uint8_t type, size_t key_len) |
124 | 190k | { |
125 | 190k | return (type == CURL_HASH_TYPE_BYTES) || |
126 | 21.0k | ((type == CURL_HASH_TYPE_SOCKET) && |
127 | 21.0k | (key_len == sizeof(curl_socket_t))); |
128 | 190k | } |
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 | 151k | { |
177 | 151k | DEBUGASSERT(h); |
178 | 151k | DEBUGASSERT(slots); |
179 | 151k | DEBUGASSERT((unsigned int)type < CURL_HASH_TYPE_LAST); |
180 | 151k | DEBUGASSERT(dtor); |
181 | | |
182 | 151k | h->table = NULL; |
183 | 151k | h->dtor = dtor; |
184 | 151k | h->size = 0; |
185 | 151k | h->slots = slots; |
186 | 151k | h->type = (uint8_t)type; |
187 | 151k | #ifdef DEBUGBUILD |
188 | 151k | h->init = HASHINIT; |
189 | 151k | #endif |
190 | 151k | } |
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 | 39.0k | { |
197 | 39.0k | struct Curl_hash_element *he; |
198 | | |
199 | | /* allocate the struct plus memory after it to store the key */ |
200 | 39.0k | he = curlx_malloc(sizeof(struct Curl_hash_element) + key_len); |
201 | 39.0k | if(he) { |
202 | 39.0k | he->next = NULL; |
203 | | /* copy the key */ |
204 | 39.0k | memcpy(hash_elem_key(he), key, key_len); |
205 | 39.0k | he->key_len = key_len; |
206 | 39.0k | he->ptr = p; |
207 | 39.0k | he->dtor = dtor; |
208 | 39.0k | } |
209 | 39.0k | return he; |
210 | 39.0k | } |
211 | | |
212 | | static void hash_elem_clear_ptr(struct Curl_hash *h, |
213 | | struct Curl_hash_element *he) |
214 | 39.0k | { |
215 | 39.0k | DEBUGASSERT(h); |
216 | 39.0k | DEBUGASSERT(he); |
217 | 39.0k | if(he->ptr) { |
218 | 39.0k | if(he->dtor) |
219 | 4 | he->dtor(hash_elem_key(he), he->key_len, he->ptr); |
220 | 39.0k | else |
221 | 39.0k | h->dtor(he->ptr); |
222 | 39.0k | he->ptr = NULL; |
223 | 39.0k | } |
224 | 39.0k | } |
225 | | |
226 | | static void hash_elem_destroy(struct Curl_hash *h, |
227 | | struct Curl_hash_element *he) |
228 | 39.0k | { |
229 | 39.0k | hash_elem_clear_ptr(h, he); |
230 | 39.0k | curlx_free(he); |
231 | 39.0k | } |
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 | 39.0k | { |
237 | 39.0k | *he_anchor = he->next; |
238 | 39.0k | --h->size; |
239 | 39.0k | } |
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 | 39.0k | { |
245 | 39.0k | he->next = *he_anchor; |
246 | 39.0k | *he_anchor = he; |
247 | 39.0k | ++h->size; |
248 | 39.0k | } |
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 | 39.0k | { |
254 | 39.0k | const struct hash_functions *functions; |
255 | 39.0k | struct Curl_hash_element *he, **slot; |
256 | | |
257 | 39.0k | DEBUGASSERT(h); |
258 | 39.0k | DEBUGASSERT(h->slots); |
259 | 39.0k | DEBUGASSERT(h->init == HASHINIT); |
260 | 39.0k | if(!hash_key_is_valid(h->type, key_len)) |
261 | 0 | return NULL; |
262 | 39.0k | if(!h->table) { |
263 | 35.8k | h->table = curlx_calloc(h->slots, sizeof(struct Curl_hash_element *)); |
264 | 35.8k | if(!h->table) |
265 | 0 | return NULL; /* OOM */ |
266 | 35.8k | } |
267 | | |
268 | 39.0k | functions = hash_get_functions(h->type); |
269 | 39.0k | slot = &h->table[functions->hash(key, key_len, h->slots)]; |
270 | 39.0k | for(he = *slot; he; he = he->next) { |
271 | 7 | 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 | 7 | } |
279 | | |
280 | 39.0k | he = hash_elem_create(key, key_len, p, dtor); |
281 | 39.0k | if(!he) |
282 | 0 | return NULL; /* OOM */ |
283 | | |
284 | 39.0k | hash_elem_link(h, slot, he); |
285 | 39.0k | return p; /* return the new entry */ |
286 | 39.0k | } |
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 | 39.0k | { |
299 | 39.0k | return Curl_hash_add2(h, key, key_len, p, NULL); |
300 | 39.0k | } |
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 | 63.8k | { |
309 | 63.8k | const struct hash_functions *functions; |
310 | | |
311 | 63.8k | DEBUGASSERT(h); |
312 | 63.8k | DEBUGASSERT(h->slots); |
313 | 63.8k | DEBUGASSERT(h->init == HASHINIT); |
314 | 63.8k | if(!hash_key_is_valid(h->type, key_len)) |
315 | 0 | return 1; |
316 | 63.8k | if(h->table) { |
317 | 20.9k | struct Curl_hash_element *he, **he_anchor; |
318 | | |
319 | 20.9k | functions = hash_get_functions(h->type); |
320 | 20.9k | he_anchor = &h->table[functions->hash(key, key_len, h->slots)]; |
321 | 20.9k | while(*he_anchor) { |
322 | 20.9k | he = *he_anchor; |
323 | 20.9k | if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) { |
324 | 20.9k | hash_elem_unlink(h, he_anchor, he); |
325 | 20.9k | hash_elem_destroy(h, he); |
326 | 20.9k | return 0; |
327 | 20.9k | } |
328 | 0 | he_anchor = &he->next; |
329 | 0 | } |
330 | 20.9k | } |
331 | 42.9k | return 1; |
332 | 63.8k | } |
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 | 87.3k | { |
340 | 87.3k | const struct hash_functions *functions; |
341 | | |
342 | 87.3k | DEBUGASSERT(h); |
343 | 87.3k | DEBUGASSERT(h->init == HASHINIT); |
344 | 87.3k | if(!hash_key_is_valid(h->type, key_len)) |
345 | 0 | return NULL; |
346 | 87.3k | if(h->table) { |
347 | 28.3k | struct Curl_hash_element *he; |
348 | 28.3k | DEBUGASSERT(h->slots); |
349 | 28.3k | functions = hash_get_functions(h->type); |
350 | 28.3k | he = h->table[functions->hash(key, key_len, h->slots)]; |
351 | 28.3k | while(he) { |
352 | 25.2k | if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) { |
353 | 25.2k | return he->ptr; |
354 | 25.2k | } |
355 | 10 | he = he->next; |
356 | 10 | } |
357 | 28.3k | } |
358 | 62.0k | return NULL; |
359 | 87.3k | } |
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 | 151k | { |
370 | 151k | DEBUGASSERT(h->init == HASHINIT); |
371 | 151k | if(h->table) { |
372 | 35.8k | Curl_hash_clean(h); |
373 | 35.8k | curlx_safefree(h->table); |
374 | 35.8k | } |
375 | 151k | DEBUGASSERT(h->size == 0); |
376 | 151k | h->slots = 0; |
377 | 151k | } |
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 | 35.8k | { |
385 | 35.8k | if(h && h->table) { |
386 | 35.8k | struct Curl_hash_element *he, **he_anchor; |
387 | 35.8k | size_t i; |
388 | 35.8k | DEBUGASSERT(h->init == HASHINIT); |
389 | 3.05M | for(i = 0; i < h->slots; ++i) { |
390 | 3.01M | he_anchor = &h->table[i]; |
391 | 3.03M | while(*he_anchor) { |
392 | 18.0k | he = *he_anchor; |
393 | 18.0k | hash_elem_unlink(h, he_anchor, he); |
394 | 18.0k | hash_elem_destroy(h, he); |
395 | 18.0k | } |
396 | 3.01M | } |
397 | 35.8k | } |
398 | 35.8k | } |
399 | | |
400 | | size_t Curl_hash_count(struct Curl_hash *h) |
401 | 21.4k | { |
402 | 21.4k | DEBUGASSERT(h->init == HASHINIT); |
403 | 21.4k | return h->size; |
404 | 21.4k | } |
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 | 21.4k | { |
410 | 21.4k | size_t i; |
411 | | |
412 | 21.4k | if(!h || !h->table) |
413 | 189 | return; |
414 | | |
415 | 21.2k | DEBUGASSERT(h->init == HASHINIT); |
416 | 1.52M | for(i = 0; i < h->slots; ++i) { |
417 | 1.50M | struct Curl_hash_element *he, **he_anchor = &h->table[i]; |
418 | 1.52M | while(*he_anchor) { |
419 | | /* ask the callback function if we shall remove this entry or not */ |
420 | 21.4k | 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 | 21.3k | else |
426 | 21.3k | he_anchor = &(*he_anchor)->next; |
427 | 21.4k | } |
428 | 1.50M | } |
429 | 21.2k | } |
430 | | |
431 | | void Curl_hash_start_iterate(struct Curl_hash *hash, |
432 | | struct Curl_hash_iterator *iter) |
433 | 39.9k | { |
434 | 39.9k | DEBUGASSERT(hash->init == HASHINIT); |
435 | 39.9k | iter->hash = hash; |
436 | 39.9k | iter->slot_index = 0; |
437 | 39.9k | iter->current = NULL; |
438 | 39.9k | #ifdef DEBUGBUILD |
439 | 39.9k | iter->init = ITERINIT; |
440 | 39.9k | #endif |
441 | 39.9k | } |
442 | | |
443 | | struct Curl_hash_element *Curl_hash_next_element( |
444 | | struct Curl_hash_iterator *iter) |
445 | 43.7k | { |
446 | 43.7k | struct Curl_hash *h; |
447 | 43.7k | DEBUGASSERT(iter->init == ITERINIT); |
448 | 43.7k | h = iter->hash; |
449 | 43.7k | if(!h->table) |
450 | 3.45k | return NULL; /* empty hash, nothing to return */ |
451 | | |
452 | | /* Get the next element in the current list, if any */ |
453 | 40.3k | if(iter->current) |
454 | 3.83k | iter->current = iter->current->next; |
455 | | |
456 | | /* If we have reached the end of the list, find the next one */ |
457 | 40.3k | if(!iter->current) { |
458 | 40.3k | size_t i; |
459 | 3.56M | for(i = iter->slot_index; i < h->slots; i++) { |
460 | 3.52M | if(h->table[i]) { |
461 | 4.29k | iter->current = h->table[i]; |
462 | 4.29k | iter->slot_index = i + 1; |
463 | 4.29k | break; |
464 | 4.29k | } |
465 | 3.52M | } |
466 | 40.3k | } |
467 | | |
468 | 40.3k | return iter->current; |
469 | 43.7k | } |