2008-05-24 16:05:49 +08:00
|
|
|
/* tinyproxy - A fast light-weight HTTP proxy
|
|
|
|
* Copyright (C) 2002 Robert James Kaes <rjkaes@users.sourceforge.net>
|
2002-04-08 05:30:02 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
2008-05-24 16:05:49 +08:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* A hashmap implementation. The keys are case-insensitive NULL terminated
|
|
|
|
* strings, and the data is arbitrary lumps of data. Copies of both the
|
|
|
|
* key and the data in the hashmap itself, so you must free the original
|
|
|
|
* key and data to avoid a memory leak. The hashmap returns a pointer
|
|
|
|
* to the data when a key is searched for, so take care in modifying the
|
|
|
|
* data as it's modifying the data stored in the hashmap. (In other words,
|
|
|
|
* don't try to free the data, or realloc the memory. :)
|
2002-04-08 05:30:02 +08:00
|
|
|
*/
|
|
|
|
|
2009-08-07 06:12:53 +08:00
|
|
|
#include "main.h"
|
2002-04-08 05:30:02 +08:00
|
|
|
|
|
|
|
#include "hashmap.h"
|
2002-05-24 02:20:27 +08:00
|
|
|
#include "heap.h"
|
2002-04-08 05:30:02 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* These structures are the storage for the hashmap. Entries are stored in
|
|
|
|
* struct hashentry_s (the key, data, and length), and all the "buckets" are
|
|
|
|
* grouped together in hashmap_s. The hashmap_s.size member is for
|
|
|
|
* internal use. It stores the number of buckets the hashmap was created
|
|
|
|
* with.
|
|
|
|
*/
|
2008-12-01 23:01:11 +08:00
|
|
|
struct hashentry_s
|
|
|
|
{
|
|
|
|
char *key;
|
|
|
|
void *data;
|
|
|
|
size_t len;
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
struct hashentry_s *prev, *next;
|
2002-04-08 05:30:02 +08:00
|
|
|
};
|
2005-05-04 04:34:54 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
struct hashbucket_s
|
|
|
|
{
|
|
|
|
struct hashentry_s *head, *tail;
|
2005-05-04 04:34:54 +08:00
|
|
|
};
|
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
struct hashmap_s
|
|
|
|
{
|
|
|
|
unsigned int size;
|
|
|
|
hashmap_iter end_iterator;
|
2002-04-26 02:55:56 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
struct hashbucket_s *buckets;
|
2002-04-08 05:30:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A NULL terminated string is passed to this function and a "hash" value
|
|
|
|
* is produced within the range of [0 .. size) (In other words, 0 to one
|
|
|
|
* less than size.)
|
|
|
|
* The contents of the key are converted to lowercase, so this function
|
|
|
|
* is not case-sensitive.
|
|
|
|
*
|
|
|
|
* If any of the arguments are invalid a negative number is returned.
|
|
|
|
*/
|
|
|
|
static int
|
2008-12-01 23:01:11 +08:00
|
|
|
hashfunc (const char *key, unsigned int size)
|
2002-04-08 05:30:02 +08:00
|
|
|
{
|
2008-12-01 23:01:11 +08:00
|
|
|
uint32_t hash;
|
2005-08-15 11:54:31 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
if (key == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
if (size == 0)
|
|
|
|
return -ERANGE;
|
2005-08-15 11:54:31 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
for (hash = tolower (*key++); *key != '\0'; key++)
|
|
|
|
{
|
|
|
|
uint32_t bit = (hash & 1) ? (1 << (sizeof (uint32_t) - 1)) : 0;
|
2005-08-15 11:54:31 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
hash >>= 1;
|
2005-08-15 11:54:31 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
hash += tolower (*key) + bit;
|
|
|
|
}
|
2005-08-15 11:54:31 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
/* Keep the hash within the table limits */
|
|
|
|
return hash % size;
|
2002-04-08 05:30:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a hashmap with the requested number of buckets. If "nbuckets" is
|
|
|
|
* not greater than zero a NULL is returned; otherwise, a _token_ to the
|
|
|
|
* hashmap is returned.
|
|
|
|
*
|
|
|
|
* NULLs are also returned if memory could not be allocated for hashmap.
|
|
|
|
*/
|
|
|
|
hashmap_t
|
2008-12-01 23:01:11 +08:00
|
|
|
hashmap_create (unsigned int nbuckets)
|
2002-04-08 05:30:02 +08:00
|
|
|
{
|
2008-12-01 23:01:11 +08:00
|
|
|
struct hashmap_s *ptr;
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
if (nbuckets == 0)
|
|
|
|
return NULL;
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2009-08-07 15:14:38 +08:00
|
|
|
ptr = (struct hashmap_s *)safecalloc (1, sizeof (struct hashmap_s));
|
2008-12-01 23:01:11 +08:00
|
|
|
if (!ptr)
|
|
|
|
return NULL;
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
ptr->size = nbuckets;
|
2009-08-07 15:14:38 +08:00
|
|
|
ptr->buckets = (struct hashbucket_s *)safecalloc (nbuckets,
|
|
|
|
sizeof (struct hashbucket_s));
|
2008-12-01 23:01:11 +08:00
|
|
|
if (!ptr->buckets)
|
|
|
|
{
|
|
|
|
safefree (ptr);
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
/* This points to "one" past the end of the hashmap. */
|
|
|
|
ptr->end_iterator = 0;
|
2002-04-26 02:55:56 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
return ptr;
|
2002-04-08 05:30:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Follow the chain of hashentries and delete them (including the data and
|
|
|
|
* the key.)
|
|
|
|
*
|
|
|
|
* Returns: 0 if the function completed successfully
|
|
|
|
* negative number is returned if "entry" was NULL
|
|
|
|
*/
|
|
|
|
static inline int
|
2008-12-01 23:01:11 +08:00
|
|
|
delete_hashbucket (struct hashbucket_s *bucket)
|
2002-04-08 05:30:02 +08:00
|
|
|
{
|
2008-12-01 23:01:11 +08:00
|
|
|
struct hashentry_s *nextptr;
|
|
|
|
struct hashentry_s *ptr;
|
2005-08-15 11:54:31 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
if (bucket == NULL || bucket->head == NULL)
|
|
|
|
return -EINVAL;
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
ptr = bucket->head;
|
|
|
|
while (ptr)
|
|
|
|
{
|
|
|
|
nextptr = ptr->next;
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
safefree (ptr->key);
|
|
|
|
safefree (ptr->data);
|
|
|
|
safefree (ptr);
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
ptr = nextptr;
|
|
|
|
}
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
return 0;
|
2002-04-08 05:30:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deletes a hashmap. All the key/data pairs are also deleted.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success
|
|
|
|
* negative if a NULL "map" was supplied
|
|
|
|
*/
|
|
|
|
int
|
2008-12-01 23:01:11 +08:00
|
|
|
hashmap_delete (hashmap_t map)
|
2002-04-08 05:30:02 +08:00
|
|
|
{
|
2008-12-01 23:01:11 +08:00
|
|
|
unsigned int i;
|
2005-08-15 11:54:31 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
if (map == NULL)
|
|
|
|
return -EINVAL;
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
for (i = 0; i != map->size; i++)
|
|
|
|
{
|
|
|
|
if (map->buckets[i].head != NULL)
|
2008-12-08 21:39:44 +08:00
|
|
|
{
|
|
|
|
delete_hashbucket (&map->buckets[i]);
|
|
|
|
}
|
2008-12-01 23:01:11 +08:00
|
|
|
}
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
safefree (map->buckets);
|
|
|
|
safefree (map);
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
return 0;
|
2002-04-08 05:30:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Inserts a NULL terminated string (as the key), plus any arbitrary "data"
|
|
|
|
* of "len" bytes. Both the key and the data are copied, so the original
|
|
|
|
* key/data must be freed to avoid a memory leak.
|
|
|
|
* The "data" must be non-NULL and "len" must be greater than zero. You
|
|
|
|
* cannot insert NULL data in association with the key.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success
|
|
|
|
* negative number if there are errors
|
|
|
|
*/
|
|
|
|
int
|
2008-12-01 23:01:11 +08:00
|
|
|
hashmap_insert (hashmap_t map, const char *key, const void *data, size_t len)
|
2002-04-08 05:30:02 +08:00
|
|
|
{
|
2008-12-01 23:01:11 +08:00
|
|
|
struct hashentry_s *ptr;
|
|
|
|
int hash;
|
|
|
|
char *key_copy;
|
|
|
|
void *data_copy;
|
|
|
|
|
|
|
|
assert (map != NULL);
|
|
|
|
assert (key != NULL);
|
|
|
|
assert (data != NULL);
|
|
|
|
assert (len > 0);
|
|
|
|
|
|
|
|
if (map == NULL || key == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
if (!data || len < 1)
|
|
|
|
return -ERANGE;
|
|
|
|
|
|
|
|
hash = hashfunc (key, map->size);
|
|
|
|
if (hash < 0)
|
|
|
|
return hash;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First make copies of the key and data in case there is a memory
|
|
|
|
* problem later.
|
|
|
|
*/
|
|
|
|
key_copy = safestrdup (key);
|
|
|
|
if (!key_copy)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
data_copy = safemalloc (len);
|
|
|
|
if (!data_copy)
|
|
|
|
{
|
|
|
|
safefree (key_copy);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
memcpy (data_copy, data, len);
|
|
|
|
|
2009-08-07 15:14:38 +08:00
|
|
|
ptr = (struct hashentry_s *)safemalloc (sizeof (struct hashentry_s));
|
2008-12-01 23:01:11 +08:00
|
|
|
if (!ptr)
|
|
|
|
{
|
|
|
|
safefree (key_copy);
|
|
|
|
safefree (data_copy);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr->key = key_copy;
|
|
|
|
ptr->data = data_copy;
|
|
|
|
ptr->len = len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now add the entry to the end of the bucket chain.
|
|
|
|
*/
|
|
|
|
ptr->next = NULL;
|
|
|
|
ptr->prev = map->buckets[hash].tail;
|
|
|
|
if (map->buckets[hash].tail)
|
|
|
|
map->buckets[hash].tail->next = ptr;
|
|
|
|
|
|
|
|
map->buckets[hash].tail = ptr;
|
|
|
|
if (!map->buckets[hash].head)
|
|
|
|
map->buckets[hash].head = ptr;
|
|
|
|
|
|
|
|
map->end_iterator++;
|
|
|
|
return 0;
|
2002-04-08 05:30:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-04-26 02:55:56 +08:00
|
|
|
* Get an iterator to the first entry.
|
|
|
|
*
|
|
|
|
* Returns: an negative value upon error.
|
|
|
|
*/
|
|
|
|
hashmap_iter
|
2008-12-01 23:01:11 +08:00
|
|
|
hashmap_first (hashmap_t map)
|
2002-04-26 02:55:56 +08:00
|
|
|
{
|
2008-12-01 23:01:11 +08:00
|
|
|
assert (map != NULL);
|
2002-04-27 00:51:29 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
if (!map)
|
|
|
|
return -EINVAL;
|
2002-04-26 02:55:56 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
if (map->end_iterator == 0)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
2002-04-26 02:55:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks to see if the iterator is pointing at the "end" of the entries.
|
|
|
|
*
|
|
|
|
* Returns: 1 if it is the end
|
|
|
|
* 0 otherwise
|
|
|
|
*/
|
|
|
|
int
|
2008-12-01 23:01:11 +08:00
|
|
|
hashmap_is_end (hashmap_t map, hashmap_iter iter)
|
2002-04-26 02:55:56 +08:00
|
|
|
{
|
2008-12-01 23:01:11 +08:00
|
|
|
assert (map != NULL);
|
|
|
|
assert (iter >= 0);
|
2002-04-26 02:55:56 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
if (!map || iter < 0)
|
|
|
|
return -EINVAL;
|
2002-04-26 02:55:56 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
if (iter == map->end_iterator)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
2002-04-26 02:55:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a "pointer" to the first instance of the particular key. It can
|
|
|
|
* be tested against hashmap_is_end() to see if the key was not found.
|
2002-04-08 05:30:02 +08:00
|
|
|
*
|
|
|
|
* Returns: negative upon an error
|
2002-04-26 02:55:56 +08:00
|
|
|
* an "iterator" pointing at the first key
|
|
|
|
* an "end-iterator" if the key wasn't found
|
|
|
|
*/
|
|
|
|
hashmap_iter
|
2008-12-01 23:01:11 +08:00
|
|
|
hashmap_find (hashmap_t map, const char *key)
|
2002-04-26 02:55:56 +08:00
|
|
|
{
|
2008-12-01 23:01:11 +08:00
|
|
|
unsigned int i;
|
|
|
|
hashmap_iter iter = 0;
|
|
|
|
struct hashentry_s *ptr;
|
|
|
|
|
|
|
|
assert (map != NULL);
|
|
|
|
assert (key != NULL);
|
|
|
|
|
|
|
|
if (!map || !key)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop through all the keys and look for the first occurrence
|
|
|
|
* of a particular key.
|
|
|
|
*/
|
|
|
|
for (i = 0; i != map->size; i++)
|
|
|
|
{
|
|
|
|
ptr = map->buckets[i].head;
|
|
|
|
|
|
|
|
while (ptr)
|
2008-12-08 21:39:44 +08:00
|
|
|
{
|
|
|
|
if (strcasecmp (ptr->key, key) == 0)
|
|
|
|
{
|
|
|
|
/* Found it, so return the current count */
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter++;
|
|
|
|
ptr = ptr->next;
|
|
|
|
}
|
2008-12-01 23:01:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return iter;
|
2002-04-26 02:55:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieve the data associated with a particular iterator.
|
|
|
|
*
|
|
|
|
* Returns: the length of the data block upon success
|
|
|
|
* negative upon error
|
|
|
|
*/
|
|
|
|
ssize_t
|
2008-12-01 23:01:11 +08:00
|
|
|
hashmap_return_entry (hashmap_t map, hashmap_iter iter, char **key,
|
2008-12-08 21:39:44 +08:00
|
|
|
void **data)
|
2002-04-26 02:55:56 +08:00
|
|
|
{
|
2008-12-01 23:01:11 +08:00
|
|
|
unsigned int i;
|
|
|
|
struct hashentry_s *ptr;
|
|
|
|
hashmap_iter count = 0;
|
|
|
|
|
|
|
|
assert (map != NULL);
|
|
|
|
assert (iter >= 0);
|
|
|
|
assert (iter != map->end_iterator);
|
|
|
|
assert (key != NULL);
|
|
|
|
assert (data != NULL);
|
|
|
|
|
|
|
|
if (!map || iter < 0 || !key || !data)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
for (i = 0; i != map->size; i++)
|
|
|
|
{
|
|
|
|
ptr = map->buckets[i].head;
|
|
|
|
while (ptr)
|
2008-12-08 21:39:44 +08:00
|
|
|
{
|
|
|
|
if (count == iter)
|
|
|
|
{
|
|
|
|
/* This is the data so return it */
|
|
|
|
*key = ptr->key;
|
|
|
|
*data = ptr->data;
|
|
|
|
return ptr->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = ptr->next;
|
|
|
|
count++;
|
|
|
|
}
|
2008-12-01 23:01:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return -EFAULT;
|
2002-04-26 02:55:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-05-24 12:45:32 +08:00
|
|
|
* Searches for _any_ occurrences of "key" within the hashmap.
|
2008-05-24 16:05:49 +08:00
|
|
|
*
|
2002-04-26 02:55:56 +08:00
|
|
|
* Returns: negative upon an error
|
2002-04-08 05:30:02 +08:00
|
|
|
* zero if no key is found
|
2002-04-26 02:55:56 +08:00
|
|
|
* count found
|
2002-04-08 05:30:02 +08:00
|
|
|
*/
|
|
|
|
ssize_t
|
2008-12-01 23:01:11 +08:00
|
|
|
hashmap_search (hashmap_t map, const char *key)
|
2002-04-08 05:30:02 +08:00
|
|
|
{
|
2008-12-01 23:01:11 +08:00
|
|
|
int hash;
|
|
|
|
struct hashentry_s *ptr;
|
|
|
|
ssize_t count = 0;
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
if (map == NULL || key == NULL)
|
|
|
|
return -EINVAL;
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
hash = hashfunc (key, map->size);
|
|
|
|
if (hash < 0)
|
|
|
|
return hash;
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
ptr = map->buckets[hash].head;
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
/* All right, there is an entry here, now see if it's the one we want */
|
|
|
|
while (ptr)
|
|
|
|
{
|
|
|
|
if (strcasecmp (ptr->key, key) == 0)
|
2008-12-08 21:39:44 +08:00
|
|
|
++count;
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
/* This entry didn't contain the key; move to the next one */
|
|
|
|
ptr = ptr->next;
|
|
|
|
}
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
return count;
|
2002-04-08 05:30:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-04-26 02:55:56 +08:00
|
|
|
* Get the first entry (assuming there is more than one) for a particular
|
|
|
|
* key. The data MUST be non-NULL.
|
2002-04-08 05:30:02 +08:00
|
|
|
*
|
2002-04-26 02:55:56 +08:00
|
|
|
* Returns: negative upon error
|
|
|
|
* zero if no entry is found
|
|
|
|
* length of data for the entry
|
2002-04-08 05:30:02 +08:00
|
|
|
*/
|
2002-04-26 02:55:56 +08:00
|
|
|
ssize_t
|
2008-12-01 23:01:11 +08:00
|
|
|
hashmap_entry_by_key (hashmap_t map, const char *key, void **data)
|
2002-04-08 05:30:02 +08:00
|
|
|
{
|
2008-12-01 23:01:11 +08:00
|
|
|
int hash;
|
|
|
|
struct hashentry_s *ptr;
|
2005-08-15 11:54:31 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
if (!map || !key || !data)
|
|
|
|
return -EINVAL;
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
hash = hashfunc (key, map->size);
|
|
|
|
if (hash < 0)
|
|
|
|
return hash;
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
ptr = map->buckets[hash].head;
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
while (ptr)
|
|
|
|
{
|
|
|
|
if (strcasecmp (ptr->key, key) == 0)
|
2008-12-08 21:39:44 +08:00
|
|
|
{
|
|
|
|
*data = ptr->data;
|
|
|
|
return ptr->len;
|
|
|
|
}
|
2002-04-26 02:55:56 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
ptr = ptr->next;
|
|
|
|
}
|
2002-04-08 05:30:02 +08:00
|
|
|
|
2008-12-01 23:01:11 +08:00
|
|
|
return 0;
|
2002-04-08 05:30:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Go through the hashmap and remove the particular key.
|
2002-04-26 02:55:56 +08:00
|
|
|
* NOTE: This will invalidate any iterators which have been created.
|
2002-04-08 05:30:02 +08:00
|
|
|
*
|
|
|
|
* Remove: negative upon error
|
|
|
|
* 0 if the key was not found
|
2002-04-26 02:55:56 +08:00
|
|
|
* positive count of entries deleted
|
2002-04-08 05:30:02 +08:00
|
|
|
*/
|
|
|
|
ssize_t
|
2008-12-01 23:01:11 +08:00
|
|
|
hashmap_remove (hashmap_t map, const char *key)
|
2002-04-08 05:30:02 +08:00
|
|
|
{
|
2008-12-01 23:01:11 +08:00
|
|
|
int hash;
|
|
|
|
struct hashentry_s *ptr, *next;
|
|
|
|
short int deleted = 0;
|
|
|
|
|
|
|
|
if (map == NULL || key == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
hash = hashfunc (key, map->size);
|
|
|
|
if (hash < 0)
|
|
|
|
return hash;
|
|
|
|
|
|
|
|
ptr = map->buckets[hash].head;
|
|
|
|
while (ptr)
|
|
|
|
{
|
|
|
|
if (strcasecmp (ptr->key, key) == 0)
|
2008-12-08 21:39:44 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Found the data, now need to remove everything
|
|
|
|
* and update the hashmap.
|
|
|
|
*/
|
|
|
|
next = ptr->next;
|
|
|
|
|
|
|
|
if (ptr->prev)
|
|
|
|
ptr->prev->next = ptr->next;
|
|
|
|
if (ptr->next)
|
|
|
|
ptr->next->prev = ptr->prev;
|
|
|
|
|
|
|
|
if (map->buckets[hash].head == ptr)
|
|
|
|
map->buckets[hash].head = ptr->next;
|
|
|
|
if (map->buckets[hash].tail == ptr)
|
|
|
|
map->buckets[hash].tail = ptr->prev;
|
|
|
|
|
|
|
|
safefree (ptr->key);
|
|
|
|
safefree (ptr->data);
|
|
|
|
safefree (ptr);
|
|
|
|
|
|
|
|
++deleted;
|
|
|
|
--map->end_iterator;
|
|
|
|
|
|
|
|
ptr = next;
|
|
|
|
continue;
|
|
|
|
}
|
2008-12-01 23:01:11 +08:00
|
|
|
|
|
|
|
/* This entry didn't contain the key; move to the next one */
|
|
|
|
ptr = ptr->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The key was not found, so return 0 */
|
|
|
|
return deleted;
|
2002-04-08 05:30:02 +08:00
|
|
|
}
|