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