2002-09-03 07:52:59 -04:00
|
|
|
/***************************************************************************
|
2004-06-24 03:43:48 -04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2002-01-03 05:22:59 -05:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2020-09-02 06:06:20 -04:00
|
|
|
* Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2002-01-03 05:22:59 -05:00
|
|
|
*
|
2002-09-03 07:52:59 -04:00
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 08:02:01 -05:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2004-06-24 03:43:48 -04:00
|
|
|
*
|
2002-01-03 05:22:59 -05:00
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
2002-09-03 07:52:59 -04:00
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
2002-01-03 05:22:59 -05:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
2002-09-03 07:52:59 -04:00
|
|
|
***************************************************************************/
|
2002-01-03 05:22:59 -05:00
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2002-01-03 05:22:59 -05:00
|
|
|
|
2016-04-01 08:21:12 -04:00
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "hash.h"
|
|
|
|
#include "llist.h"
|
2009-04-21 07:46:16 -04:00
|
|
|
#include "curl_memory.h"
|
2016-04-01 08:21:12 -04:00
|
|
|
|
2008-10-25 12:15:21 -04:00
|
|
|
/* The last #include file should be: */
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2002-01-03 05:22:59 -05:00
|
|
|
|
2004-06-24 03:43:48 -04:00
|
|
|
static void
|
2003-09-05 08:44:35 -04:00
|
|
|
hash_element_dtor(void *user, void *element)
|
2002-01-03 05:22:59 -05:00
|
|
|
{
|
2020-09-02 06:07:44 -04:00
|
|
|
struct Curl_hash *h = (struct Curl_hash *) user;
|
|
|
|
struct Curl_hash_element *e = (struct Curl_hash_element *) element;
|
2002-04-12 19:40:19 -04:00
|
|
|
|
2011-10-11 13:41:30 -04:00
|
|
|
if(e->ptr) {
|
2008-10-27 01:29:17 -04:00
|
|
|
h->dtor(e->ptr);
|
2011-10-11 13:41:30 -04:00
|
|
|
e->ptr = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
e->key_len = 0;
|
2002-01-03 05:22:59 -05:00
|
|
|
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:40:26 -05:00
|
|
|
/* Initializes a hash structure.
|
|
|
|
* Return 1 on error, 0 is fine.
|
|
|
|
*
|
|
|
|
* @unittest: 1602
|
|
|
|
* @unittest: 1603
|
|
|
|
*/
|
2003-08-14 11:05:13 -04:00
|
|
|
int
|
2020-09-02 06:07:44 -04:00
|
|
|
Curl_hash_init(struct Curl_hash *h,
|
2007-06-26 17:09:28 -04:00
|
|
|
int slots,
|
|
|
|
hash_function hfunc,
|
|
|
|
comp_function comparator,
|
2020-09-02 06:07:44 -04:00
|
|
|
Curl_hash_dtor dtor)
|
2002-01-03 05:22:59 -05:00
|
|
|
{
|
2007-11-07 04:21:35 -05:00
|
|
|
if(!slots || !hfunc || !comparator ||!dtor) {
|
2007-06-26 17:09:28 -04:00
|
|
|
return 1; /* failure */
|
|
|
|
}
|
|
|
|
|
|
|
|
h->hash_func = hfunc;
|
|
|
|
h->comp_func = comparator;
|
2002-01-03 05:22:59 -05:00
|
|
|
h->dtor = dtor;
|
|
|
|
h->size = 0;
|
2004-06-24 03:43:48 -04:00
|
|
|
h->slots = slots;
|
2002-01-03 05:22:59 -05:00
|
|
|
|
2020-09-02 06:06:20 -04:00
|
|
|
h->table = malloc(slots * sizeof(struct Curl_llist));
|
2003-08-14 11:05:13 -04:00
|
|
|
if(h->table) {
|
2018-06-02 16:52:56 -04:00
|
|
|
int i;
|
2017-04-03 04:32:43 -04:00
|
|
|
for(i = 0; i < slots; ++i)
|
2020-09-02 06:06:20 -04:00
|
|
|
Curl_llist_init(&h->table[i], (Curl_llist_dtor) hash_element_dtor);
|
2003-08-14 11:05:13 -04:00
|
|
|
return 0; /* fine */
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
h->slots = 0;
|
|
|
|
return 1; /* failure */
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
|
|
|
|
2020-09-02 06:07:44 -04:00
|
|
|
static struct Curl_hash_element *
|
2007-08-29 01:36:53 -04:00
|
|
|
mk_hash_element(const void *key, size_t key_len, const void *p)
|
2002-04-12 19:40:19 -04:00
|
|
|
{
|
2017-03-31 19:12:32 -04:00
|
|
|
/* allocate the struct plus memory after it to store the key */
|
2020-09-02 06:07:44 -04:00
|
|
|
struct Curl_hash_element *he = malloc(sizeof(struct Curl_hash_element) +
|
2017-03-31 19:12:32 -04:00
|
|
|
key_len);
|
2003-09-05 08:44:35 -04:00
|
|
|
if(he) {
|
2017-03-31 19:12:32 -04:00
|
|
|
/* copy the key */
|
|
|
|
memcpy(he->key, key, key_len);
|
|
|
|
he->key_len = key_len;
|
|
|
|
he->ptr = (void *) p;
|
2003-09-05 08:44:35 -04:00
|
|
|
}
|
|
|
|
return he;
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
2002-04-12 19:40:19 -04:00
|
|
|
|
2017-04-03 04:32:43 -04:00
|
|
|
#define FETCH_LIST(x,y,z) &x->table[x->hash_func(y, z, x->slots)]
|
2002-04-12 19:40:19 -04:00
|
|
|
|
2009-11-11 04:31:37 -05:00
|
|
|
/* Insert the data in the hash. If there already was a match in the hash,
|
2011-06-10 08:40:46 -04:00
|
|
|
* that data is replaced.
|
|
|
|
*
|
|
|
|
* @unittest: 1305
|
2015-11-12 14:40:26 -05:00
|
|
|
* @unittest: 1602
|
|
|
|
* @unittest: 1603
|
2011-06-10 08:40:46 -04:00
|
|
|
*/
|
2003-09-14 17:17:54 -04:00
|
|
|
void *
|
2020-09-02 06:07:44 -04:00
|
|
|
Curl_hash_add(struct Curl_hash *h, void *key, size_t key_len, void *p)
|
2002-01-03 05:22:59 -05:00
|
|
|
{
|
2020-09-02 06:07:44 -04:00
|
|
|
struct Curl_hash_element *he;
|
2020-09-02 06:06:20 -04:00
|
|
|
struct Curl_llist_element *le;
|
|
|
|
struct Curl_llist *l = FETCH_LIST(h, key, key_len);
|
2002-04-12 19:40:19 -04:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
for(le = l->head; le; le = le->next) {
|
2020-09-02 06:07:44 -04:00
|
|
|
he = (struct Curl_hash_element *) le->ptr;
|
2007-11-07 04:21:35 -05:00
|
|
|
if(h->comp_func(he->key, he->key_len, key, key_len)) {
|
2009-11-11 04:31:37 -05:00
|
|
|
Curl_llist_remove(l, le, (void *)h);
|
|
|
|
--h->size;
|
|
|
|
break;
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-05 08:44:35 -04:00
|
|
|
he = mk_hash_element(key, key_len, p);
|
2007-11-07 04:21:35 -05:00
|
|
|
if(he) {
|
2017-04-20 09:10:04 -04:00
|
|
|
Curl_llist_insert_next(l, l->tail, he, &he->list);
|
|
|
|
++h->size;
|
|
|
|
return p; /* return the new entry */
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
2004-05-04 09:40:30 -04:00
|
|
|
|
2003-09-14 17:17:54 -04:00
|
|
|
return NULL; /* failure */
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:40:26 -05:00
|
|
|
/* Remove the identified hash entry.
|
|
|
|
* Returns non-zero on failure.
|
|
|
|
*
|
|
|
|
* @unittest: 1603
|
|
|
|
*/
|
2020-09-02 06:07:44 -04:00
|
|
|
int Curl_hash_delete(struct Curl_hash *h, void *key, size_t key_len)
|
2006-04-10 11:00:53 -04:00
|
|
|
{
|
2020-09-02 06:06:20 -04:00
|
|
|
struct Curl_llist_element *le;
|
|
|
|
struct Curl_llist *l = FETCH_LIST(h, key, key_len);
|
2006-04-10 11:00:53 -04:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
for(le = l->head; le; le = le->next) {
|
2020-09-02 06:07:44 -04:00
|
|
|
struct Curl_hash_element *he = le->ptr;
|
2007-11-07 04:21:35 -05:00
|
|
|
if(h->comp_func(he->key, he->key_len, key, key_len)) {
|
2006-04-10 11:00:53 -04:00
|
|
|
Curl_llist_remove(l, le, (void *) h);
|
2011-10-11 13:41:30 -04:00
|
|
|
--h->size;
|
2006-04-10 11:00:53 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:40:26 -05:00
|
|
|
/* Retrieves a hash element.
|
|
|
|
*
|
|
|
|
* @unittest: 1603
|
|
|
|
*/
|
2002-11-05 05:51:41 -05:00
|
|
|
void *
|
2020-09-02 06:07:44 -04:00
|
|
|
Curl_hash_pick(struct Curl_hash *h, void *key, size_t key_len)
|
2002-01-03 05:22:59 -05:00
|
|
|
{
|
2020-09-02 06:06:20 -04:00
|
|
|
struct Curl_llist_element *le;
|
|
|
|
struct Curl_llist *l;
|
2002-04-12 19:40:19 -04:00
|
|
|
|
2012-12-26 00:00:59 -05:00
|
|
|
if(h) {
|
|
|
|
l = FETCH_LIST(h, key, key_len);
|
|
|
|
for(le = l->head; le; le = le->next) {
|
2020-09-02 06:07:44 -04:00
|
|
|
struct Curl_hash_element *he = le->ptr;
|
2012-12-26 00:00:59 -05:00
|
|
|
if(h->comp_func(he->key, he->key_len, key, key_len)) {
|
|
|
|
return he->ptr;
|
|
|
|
}
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-05 05:51:41 -05:00
|
|
|
return NULL;
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
|
|
|
|
2009-06-09 22:49:42 -04:00
|
|
|
#if defined(DEBUGBUILD) && defined(AGGRESIVE_TEST)
|
2004-06-24 03:43:48 -04:00
|
|
|
void
|
2020-09-02 06:07:44 -04:00
|
|
|
Curl_hash_apply(Curl_hash *h, void *user,
|
2002-11-05 05:51:41 -05:00
|
|
|
void (*cb)(void *user, void *ptr))
|
2002-01-03 05:22:59 -05:00
|
|
|
{
|
2020-09-02 06:06:20 -04:00
|
|
|
struct Curl_llist_element *le;
|
2002-01-03 05:22:59 -05:00
|
|
|
int i;
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
for(i = 0; i < h->slots; ++i) {
|
|
|
|
for(le = (h->table[i])->head;
|
|
|
|
le;
|
|
|
|
le = le->next) {
|
2020-09-02 06:07:44 -04:00
|
|
|
Curl_hash_element *el = le->ptr;
|
2002-11-05 05:51:41 -05:00
|
|
|
cb(user, el->ptr);
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-09-05 08:44:35 -04:00
|
|
|
#endif
|
2002-01-03 05:22:59 -05:00
|
|
|
|
2015-05-02 14:49:55 -04:00
|
|
|
/* Destroys all the entries in the given hash and resets its attributes,
|
|
|
|
* prepping the given hash for [static|dynamic] deallocation.
|
2015-11-12 14:40:26 -05:00
|
|
|
*
|
|
|
|
* @unittest: 1305
|
|
|
|
* @unittest: 1602
|
|
|
|
* @unittest: 1603
|
2015-05-02 14:49:55 -04:00
|
|
|
*/
|
2002-01-03 05:22:59 -05:00
|
|
|
void
|
2020-09-02 06:07:44 -04:00
|
|
|
Curl_hash_destroy(struct Curl_hash *h)
|
2002-01-03 05:22:59 -05:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
for(i = 0; i < h->slots; ++i) {
|
2017-04-03 04:32:43 -04:00
|
|
|
Curl_llist_destroy(&h->table[i], (void *) h);
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
|
|
|
|
2012-12-25 07:30:41 -05:00
|
|
|
Curl_safefree(h->table);
|
2011-10-11 13:41:30 -04:00
|
|
|
h->size = 0;
|
|
|
|
h->slots = 0;
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
|
|
|
|
2015-05-02 14:49:55 -04:00
|
|
|
/* Removes all the entries in the given hash.
|
|
|
|
*
|
|
|
|
* @unittest: 1602
|
|
|
|
*/
|
|
|
|
void
|
2020-09-02 06:07:44 -04:00
|
|
|
Curl_hash_clean(struct Curl_hash *h)
|
2015-05-02 14:49:55 -04:00
|
|
|
{
|
|
|
|
Curl_hash_clean_with_criterium(h, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cleans all entries that pass the comp function criteria. */
|
2002-04-17 16:13:55 -04:00
|
|
|
void
|
2020-09-02 06:07:44 -04:00
|
|
|
Curl_hash_clean_with_criterium(struct Curl_hash *h, void *user,
|
2002-04-27 09:06:40 -04:00
|
|
|
int (*comp)(void *, void *))
|
2002-04-17 16:13:55 -04:00
|
|
|
{
|
2020-09-02 06:06:20 -04:00
|
|
|
struct Curl_llist_element *le;
|
|
|
|
struct Curl_llist_element *lnext;
|
|
|
|
struct Curl_llist *list;
|
2002-04-17 16:13:55 -04:00
|
|
|
int i;
|
|
|
|
|
2011-12-25 05:35:45 -05:00
|
|
|
if(!h)
|
|
|
|
return;
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
for(i = 0; i < h->slots; ++i) {
|
2017-04-03 04:32:43 -04:00
|
|
|
list = &h->table[i];
|
2003-09-05 08:44:35 -04:00
|
|
|
le = list->head; /* get first list entry */
|
|
|
|
while(le) {
|
2020-09-02 06:07:44 -04:00
|
|
|
struct Curl_hash_element *he = le->ptr;
|
2003-09-05 08:44:35 -04:00
|
|
|
lnext = le->next;
|
|
|
|
/* ask the callback function if we shall remove this entry or not */
|
2015-05-02 14:49:55 -04:00
|
|
|
if(comp == NULL || comp(user, he->ptr)) {
|
2003-09-05 08:44:35 -04:00
|
|
|
Curl_llist_remove(list, le, (void *) h);
|
|
|
|
--h->size; /* one less entry in the hash now */
|
2002-04-17 16:13:55 -04:00
|
|
|
}
|
2003-09-05 08:44:35 -04:00
|
|
|
le = lnext;
|
|
|
|
}
|
2002-04-17 16:13:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 01:53:24 -05:00
|
|
|
size_t Curl_hash_str(void *key, size_t key_length, size_t slots_num)
|
2007-06-26 17:09:28 -04:00
|
|
|
{
|
2016-11-23 01:53:24 -05:00
|
|
|
const char *key_str = (const char *) key;
|
2007-08-29 01:36:53 -04:00
|
|
|
const char *end = key_str + key_length;
|
2018-04-06 08:19:39 -04:00
|
|
|
size_t h = 5381;
|
2007-06-26 17:09:28 -04:00
|
|
|
|
2007-11-07 04:21:35 -05:00
|
|
|
while(key_str < end) {
|
2007-06-26 17:09:28 -04:00
|
|
|
h += h << 5;
|
2018-04-06 08:19:39 -04:00
|
|
|
h ^= *key_str++;
|
2007-06-26 17:09:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return (h % slots_num);
|
|
|
|
}
|
|
|
|
|
2015-04-28 07:09:39 -04:00
|
|
|
size_t Curl_str_key_compare(void *k1, size_t key1_len,
|
|
|
|
void *k2, size_t key2_len)
|
2007-06-26 17:09:28 -04:00
|
|
|
{
|
2015-04-28 07:09:39 -04:00
|
|
|
if((key1_len == key2_len) && !memcmp(k1, k2, key1_len))
|
2007-06-26 17:09:28 -04:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-02 06:07:44 -04:00
|
|
|
void Curl_hash_start_iterate(struct Curl_hash *hash,
|
|
|
|
struct Curl_hash_iterator *iter)
|
2012-12-06 06:12:04 -05:00
|
|
|
{
|
|
|
|
iter->hash = hash;
|
|
|
|
iter->slot_index = 0;
|
|
|
|
iter->current_element = NULL;
|
|
|
|
}
|
|
|
|
|
2020-09-02 06:07:44 -04:00
|
|
|
struct Curl_hash_element *
|
|
|
|
Curl_hash_next_element(struct Curl_hash_iterator *iter)
|
2012-12-06 06:12:04 -05:00
|
|
|
{
|
2020-09-02 06:07:44 -04:00
|
|
|
struct Curl_hash *h = iter->hash;
|
2012-12-06 06:12:04 -05:00
|
|
|
|
|
|
|
/* Get the next element in the current list, if any */
|
|
|
|
if(iter->current_element)
|
|
|
|
iter->current_element = iter->current_element->next;
|
|
|
|
|
|
|
|
/* If we have reached the end of the list, find the next one */
|
|
|
|
if(!iter->current_element) {
|
2018-06-02 16:52:56 -04:00
|
|
|
int i;
|
2017-09-11 03:27:06 -04:00
|
|
|
for(i = iter->slot_index; i < h->slots; i++) {
|
2017-04-03 04:32:43 -04:00
|
|
|
if(h->table[i].head) {
|
|
|
|
iter->current_element = h->table[i].head;
|
2017-09-09 17:55:08 -04:00
|
|
|
iter->slot_index = i + 1;
|
2012-12-06 06:12:04 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(iter->current_element) {
|
2020-09-02 06:07:44 -04:00
|
|
|
struct Curl_hash_element *he = iter->current_element->ptr;
|
2012-12-06 06:12:04 -05:00
|
|
|
return he;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
iter->current_element = NULL;
|
|
|
|
return NULL;
|
2012-12-06 06:12:04 -05:00
|
|
|
}
|
|
|
|
|
2006-09-10 18:12:24 -04:00
|
|
|
#if 0 /* useful function for debugging hashes and their contents */
|
2020-09-02 06:07:44 -04:00
|
|
|
void Curl_hash_print(struct Curl_hash *h,
|
2006-09-10 18:12:24 -04:00
|
|
|
void (*func)(void *))
|
|
|
|
{
|
2020-09-02 06:07:44 -04:00
|
|
|
struct Curl_hash_iterator iter;
|
|
|
|
struct Curl_hash_element *he;
|
2012-12-06 06:12:04 -05:00
|
|
|
int last_index = -1;
|
|
|
|
|
2007-11-07 04:21:35 -05:00
|
|
|
if(!h)
|
2006-09-10 18:12:24 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
fprintf(stderr, "=Hash dump=\n");
|
|
|
|
|
2012-12-06 06:12:04 -05:00
|
|
|
Curl_hash_start_iterate(h, &iter);
|
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
|
|
|
while(he) {
|
|
|
|
if(iter.slot_index != last_index) {
|
|
|
|
fprintf(stderr, "index %d:", iter.slot_index);
|
|
|
|
if(last_index >= 0) {
|
|
|
|
fprintf(stderr, "\n");
|
2006-09-10 18:12:24 -04:00
|
|
|
}
|
2012-12-06 06:12:04 -05:00
|
|
|
last_index = iter.slot_index;
|
2006-09-10 18:12:24 -04:00
|
|
|
}
|
2012-12-06 06:12:04 -05:00
|
|
|
|
|
|
|
if(func)
|
|
|
|
func(he->ptr);
|
|
|
|
else
|
2013-07-23 09:59:07 -04:00
|
|
|
fprintf(stderr, " [%p]", (void *)he->ptr);
|
2012-12-06 06:12:04 -05:00
|
|
|
|
|
|
|
he = Curl_hash_next_element(&iter);
|
2006-09-10 18:12:24 -04:00
|
|
|
}
|
2012-12-06 06:12:04 -05:00
|
|
|
fprintf(stderr, "\n");
|
2006-09-10 18:12:24 -04:00
|
|
|
}
|
|
|
|
#endif
|