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