• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 #include "curl_setup.h"
26 
27 #include <curl/curl.h>
28 
29 #include "hash.h"
30 #include "llist.h"
31 #include "curl_memory.h"
32 
33 /* The last #include file should be: */
34 #include "memdebug.h"
35 
36 /* random patterns for API verification */
37 #define HASHINIT 0x7017e781
38 #define ITERINIT 0x5FEDCBA9
39 
40 static void
hash_element_dtor(void * user,void * element)41 hash_element_dtor(void *user, void *element)
42 {
43   struct Curl_hash *h = (struct Curl_hash *) user;
44   struct Curl_hash_element *e = (struct Curl_hash_element *) element;
45   DEBUGASSERT(h);
46   DEBUGASSERT(e);
47 
48   if(e->ptr) {
49     if(e->dtor)
50       e->dtor(e->key, e->key_len, e->ptr);
51     else
52       h->dtor(e->ptr);
53     e->ptr = NULL;
54   }
55 
56   e->key_len = 0;
57 
58   free(e);
59 }
60 
61 /* Initializes a hash structure.
62  * Return 1 on error, 0 is fine.
63  *
64  * @unittest: 1602
65  * @unittest: 1603
66  */
67 void
Curl_hash_init(struct Curl_hash * h,size_t slots,hash_function hfunc,comp_function comparator,Curl_hash_dtor dtor)68 Curl_hash_init(struct Curl_hash *h,
69                size_t slots,
70                hash_function hfunc,
71                comp_function comparator,
72                Curl_hash_dtor dtor)
73 {
74   DEBUGASSERT(h);
75   DEBUGASSERT(slots);
76   DEBUGASSERT(hfunc);
77   DEBUGASSERT(comparator);
78   DEBUGASSERT(dtor);
79 
80   h->table = NULL;
81   h->hash_func = hfunc;
82   h->comp_func = comparator;
83   h->dtor = dtor;
84   h->size = 0;
85   h->slots = slots;
86 #ifdef DEBUGBUILD
87   h->init = HASHINIT;
88 #endif
89 }
90 
91 static struct Curl_hash_element *
mk_hash_element(const void * key,size_t key_len,const void * p,Curl_hash_elem_dtor dtor)92 mk_hash_element(const void *key, size_t key_len, const void *p,
93                 Curl_hash_elem_dtor dtor)
94 {
95   /* allocate the struct plus memory after it to store the key */
96   struct Curl_hash_element *he = malloc(sizeof(struct Curl_hash_element) +
97                                         key_len);
98   if(he) {
99     /* copy the key */
100     memcpy(he->key, key, key_len);
101     he->key_len = key_len;
102     he->ptr = (void *) p;
103     he->dtor = dtor;
104   }
105   return he;
106 }
107 
108 #define FETCH_LIST(x,y,z) &x->table[x->hash_func(y, z, x->slots)]
109 
Curl_hash_add2(struct Curl_hash * h,void * key,size_t key_len,void * p,Curl_hash_elem_dtor dtor)110 void *Curl_hash_add2(struct Curl_hash *h, void *key, size_t key_len, void *p,
111                      Curl_hash_elem_dtor dtor)
112 {
113   struct Curl_hash_element  *he;
114   struct Curl_llist_node *le;
115   struct Curl_llist *l;
116 
117   DEBUGASSERT(h);
118   DEBUGASSERT(h->slots);
119   DEBUGASSERT(h->init == HASHINIT);
120   if(!h->table) {
121     size_t i;
122     h->table = malloc(h->slots * sizeof(struct Curl_llist));
123     if(!h->table)
124       return NULL; /* OOM */
125     for(i = 0; i < h->slots; ++i)
126       Curl_llist_init(&h->table[i], hash_element_dtor);
127   }
128 
129   l = FETCH_LIST(h, key, key_len);
130 
131   for(le = Curl_llist_head(l); le; le = Curl_node_next(le)) {
132     he = (struct Curl_hash_element *) Curl_node_elem(le);
133     if(h->comp_func(he->key, he->key_len, key, key_len)) {
134       Curl_node_uremove(le, (void *)h);
135       --h->size;
136       break;
137     }
138   }
139 
140   he = mk_hash_element(key, key_len, p, dtor);
141   if(he) {
142     Curl_llist_append(l, he, &he->list);
143     ++h->size;
144     return p; /* return the new entry */
145   }
146 
147   return NULL; /* failure */
148 }
149 
150 /* Insert the data in the hash. If there already was a match in the hash, that
151  * data is replaced. This function also "lazily" allocates the table if
152  * needed, as it is not done in the _init function (anymore).
153  *
154  * @unittest: 1305
155  * @unittest: 1602
156  * @unittest: 1603
157  */
158 void *
Curl_hash_add(struct Curl_hash * h,void * key,size_t key_len,void * p)159 Curl_hash_add(struct Curl_hash *h, void *key, size_t key_len, void *p)
160 {
161   return Curl_hash_add2(h, key, key_len, p, NULL);
162 }
163 
164 /* Remove the identified hash entry.
165  * Returns non-zero on failure.
166  *
167  * @unittest: 1603
168  */
Curl_hash_delete(struct Curl_hash * h,void * key,size_t key_len)169 int Curl_hash_delete(struct Curl_hash *h, void *key, size_t key_len)
170 {
171   DEBUGASSERT(h);
172   DEBUGASSERT(h->slots);
173   DEBUGASSERT(h->init == HASHINIT);
174   if(h->table) {
175     struct Curl_llist_node *le;
176     struct Curl_llist *l = FETCH_LIST(h, key, key_len);
177 
178     for(le = Curl_llist_head(l); le; le = Curl_node_next(le)) {
179       struct Curl_hash_element *he = Curl_node_elem(le);
180       if(h->comp_func(he->key, he->key_len, key, key_len)) {
181         Curl_node_uremove(le, (void *) h);
182         --h->size;
183         return 0;
184       }
185     }
186   }
187   return 1;
188 }
189 
190 /* Retrieves a hash element.
191  *
192  * @unittest: 1603
193  */
194 void *
Curl_hash_pick(struct Curl_hash * h,void * key,size_t key_len)195 Curl_hash_pick(struct Curl_hash *h, void *key, size_t key_len)
196 {
197   DEBUGASSERT(h);
198   DEBUGASSERT(h->init == HASHINIT);
199   if(h->table) {
200     struct Curl_llist_node *le;
201     struct Curl_llist *l;
202     DEBUGASSERT(h->slots);
203     l = FETCH_LIST(h, key, key_len);
204     for(le = Curl_llist_head(l); le; le = Curl_node_next(le)) {
205       struct Curl_hash_element *he = Curl_node_elem(le);
206       if(h->comp_func(he->key, he->key_len, key, key_len)) {
207         return he->ptr;
208       }
209     }
210   }
211 
212   return NULL;
213 }
214 
215 /* Destroys all the entries in the given hash and resets its attributes,
216  * prepping the given hash for [static|dynamic] deallocation.
217  *
218  * @unittest: 1305
219  * @unittest: 1602
220  * @unittest: 1603
221  */
222 void
Curl_hash_destroy(struct Curl_hash * h)223 Curl_hash_destroy(struct Curl_hash *h)
224 {
225   DEBUGASSERT(h->init == HASHINIT);
226   if(h->table) {
227     size_t i;
228     for(i = 0; i < h->slots; ++i) {
229       Curl_llist_destroy(&h->table[i], (void *) h);
230     }
231     Curl_safefree(h->table);
232   }
233   h->size = 0;
234   h->slots = 0;
235 }
236 
237 /* Removes all the entries in the given hash.
238  *
239  * @unittest: 1602
240  */
241 void
Curl_hash_clean(struct Curl_hash * h)242 Curl_hash_clean(struct Curl_hash *h)
243 {
244   Curl_hash_clean_with_criterium(h, NULL, NULL);
245 }
246 
Curl_hash_count(struct Curl_hash * h)247 size_t Curl_hash_count(struct Curl_hash *h)
248 {
249   DEBUGASSERT(h->init == HASHINIT);
250   return h->size;
251 }
252 
253 /* Cleans all entries that pass the comp function criteria. */
254 void
Curl_hash_clean_with_criterium(struct Curl_hash * h,void * user,int (* comp)(void *,void *))255 Curl_hash_clean_with_criterium(struct Curl_hash *h, void *user,
256                                int (*comp)(void *, void *))
257 {
258   size_t i;
259 
260   if(!h || !h->table)
261     return;
262 
263   DEBUGASSERT(h->init == HASHINIT);
264   for(i = 0; i < h->slots; ++i) {
265     struct Curl_llist *list = &h->table[i];
266     struct Curl_llist_node *le =
267       Curl_llist_head(list); /* get first list entry */
268     while(le) {
269       struct Curl_hash_element *he = Curl_node_elem(le);
270       struct Curl_llist_node *lnext = Curl_node_next(le);
271       /* ask the callback function if we shall remove this entry or not */
272       if(!comp || comp(user, he->ptr)) {
273         Curl_node_uremove(le, (void *) h);
274         --h->size; /* one less entry in the hash now */
275       }
276       le = lnext;
277     }
278   }
279 }
280 
Curl_hash_str(void * key,size_t key_length,size_t slots_num)281 size_t Curl_hash_str(void *key, size_t key_length, size_t slots_num)
282 {
283   const char *key_str = (const char *) key;
284   const char *end = key_str + key_length;
285   size_t h = 5381;
286 
287   while(key_str < end) {
288     size_t j = (size_t)*key_str++;
289     h += h << 5;
290     h ^= j;
291   }
292 
293   return (h % slots_num);
294 }
295 
Curl_str_key_compare(void * k1,size_t key1_len,void * k2,size_t key2_len)296 size_t Curl_str_key_compare(void *k1, size_t key1_len,
297                             void *k2, size_t key2_len)
298 {
299   if((key1_len == key2_len) && !memcmp(k1, k2, key1_len))
300     return 1;
301 
302   return 0;
303 }
304 
Curl_hash_start_iterate(struct Curl_hash * hash,struct Curl_hash_iterator * iter)305 void Curl_hash_start_iterate(struct Curl_hash *hash,
306                              struct Curl_hash_iterator *iter)
307 {
308   DEBUGASSERT(hash->init == HASHINIT);
309   iter->hash = hash;
310   iter->slot_index = 0;
311   iter->current_element = NULL;
312 #ifdef DEBUGBUILD
313   iter->init = ITERINIT;
314 #endif
315 }
316 
317 struct Curl_hash_element *
Curl_hash_next_element(struct Curl_hash_iterator * iter)318 Curl_hash_next_element(struct Curl_hash_iterator *iter)
319 {
320   struct Curl_hash *h;
321   DEBUGASSERT(iter->init == ITERINIT);
322   h = iter->hash;
323   if(!h->table)
324     return NULL; /* empty hash, nothing to return */
325 
326   /* Get the next element in the current list, if any */
327   if(iter->current_element)
328     iter->current_element = Curl_node_next(iter->current_element);
329 
330   /* If we have reached the end of the list, find the next one */
331   if(!iter->current_element) {
332     size_t i;
333     for(i = iter->slot_index; i < h->slots; i++) {
334       if(Curl_llist_head(&h->table[i])) {
335         iter->current_element = Curl_llist_head(&h->table[i]);
336         iter->slot_index = i + 1;
337         break;
338       }
339     }
340   }
341 
342   if(iter->current_element) {
343     struct Curl_hash_element *he = Curl_node_elem(iter->current_element);
344     return he;
345   }
346   return NULL;
347 }
348 
349 #if 0 /* useful function for debugging hashes and their contents */
350 void Curl_hash_print(struct Curl_hash *h,
351                      void (*func)(void *))
352 {
353   struct Curl_hash_iterator iter;
354   struct Curl_hash_element *he;
355   size_t last_index = ~0;
356 
357   if(!h)
358     return;
359 
360   fprintf(stderr, "=Hash dump=\n");
361 
362   Curl_hash_start_iterate(h, &iter);
363 
364   he = Curl_hash_next_element(&iter);
365   while(he) {
366     if(iter.slot_index != last_index) {
367       fprintf(stderr, "index %d:", iter.slot_index);
368       if(last_index != ~0) {
369         fprintf(stderr, "\n");
370       }
371       last_index = iter.slot_index;
372     }
373 
374     if(func)
375       func(he->ptr);
376     else
377       fprintf(stderr, " [%p]", (void *)he->ptr);
378 
379     he = Curl_hash_next_element(&iter);
380   }
381   fprintf(stderr, "\n");
382 }
383 #endif
384 
Curl_hash_offt_init(struct Curl_hash * h,size_t slots,Curl_hash_dtor dtor)385 void Curl_hash_offt_init(struct Curl_hash *h,
386                          size_t slots,
387                          Curl_hash_dtor dtor)
388 {
389   Curl_hash_init(h, slots, Curl_hash_str, Curl_str_key_compare, dtor);
390 }
391 
Curl_hash_offt_set(struct Curl_hash * h,curl_off_t id,void * elem)392 void *Curl_hash_offt_set(struct Curl_hash *h, curl_off_t id, void *elem)
393 {
394   return Curl_hash_add(h, &id, sizeof(id), elem);
395 }
396 
Curl_hash_offt_remove(struct Curl_hash * h,curl_off_t id)397 int Curl_hash_offt_remove(struct Curl_hash *h, curl_off_t id)
398 {
399   return Curl_hash_delete(h, &id, sizeof(id));
400 }
401 
Curl_hash_offt_get(struct Curl_hash * h,curl_off_t id)402 void *Curl_hash_offt_get(struct Curl_hash *h, curl_off_t id)
403 {
404   return Curl_hash_pick(h, &id, sizeof(id));
405 }
406