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