Coverage Report

Created: 2022-09-28 06:21

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