#include "apr_private.h"
#include "apr_general.h"
#include "apr_pools.h"
#include "apr_time.h"
#include "apr_hash.h"
#if APR_HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if APR_HAVE_STRING_H
#include <string.h>
#endif
#if APR_POOL_DEBUG && APR_HAVE_STDIO_H
#include <stdio.h>
#endif
typedef struct apr_hash_entry_t apr_hash_entry_t;
struct apr_hash_entry_t {
apr_hash_entry_t *next;
unsigned int hash;
const void *key;
apr_ssize_t klen;
const void *val;
};
struct apr_hash_index_t {
apr_hash_t *ht;
apr_hash_entry_t *this, *next;
unsigned int index;
};
struct apr_hash_t {
apr_pool_t *pool;
apr_hash_entry_t **array;
apr_hash_index_t iterator;
unsigned int count, max, seed;
apr_hashfunc_t hash_func;
apr_hash_entry_t *free;
};
#define INITIAL_MAX 15
static apr_hash_entry_t **alloc_array(apr_hash_t *ht, unsigned int max)
{
return apr_pcalloc(ht->pool, sizeof(*ht->array) * (max + 1));
}
APR_DECLARE(apr_hash_t *) apr_hash_make(apr_pool_t *pool)
{
apr_hash_t *ht;
apr_time_t now = apr_time_now();
ht = apr_palloc(pool, sizeof(apr_hash_t));
ht->pool = pool;
ht->free = NULL;
ht->count = 0;
ht->max = INITIAL_MAX;
ht->seed = (unsigned int)((now >> 32) ^ now ^ (apr_uintptr_t)pool ^
(apr_uintptr_t)ht ^ (apr_uintptr_t)&now) - 1;
ht->array = alloc_array(ht, ht->max);
ht->hash_func = NULL;
return ht;
}
APR_DECLARE(apr_hash_t *) apr_hash_make_custom(apr_pool_t *pool,
apr_hashfunc_t hash_func)
{
apr_hash_t *ht = apr_hash_make(pool);
ht->hash_func = hash_func;
return ht;
}
APR_DECLARE(apr_hash_index_t *) apr_hash_next(apr_hash_index_t *hi)
{
hi->this = hi->next;
while (!hi->this) {
if (hi->index > hi->ht->max)
return NULL;
hi->this = hi->ht->array[hi->index++];
}
hi->next = hi->this->next;
return hi;
}
APR_DECLARE(apr_hash_index_t *) apr_hash_first(apr_pool_t *p, apr_hash_t *ht)
{
apr_hash_index_t *hi;
if (p)
hi = apr_palloc(p, sizeof(*hi));
else
hi = &ht->iterator;
hi->ht = ht;
hi->index = 0;
hi->this = NULL;
hi->next = NULL;
return apr_hash_next(hi);
}
APR_DECLARE(void) apr_hash_this(apr_hash_index_t *hi,
const void **key,
apr_ssize_t *klen,
void **val)
{
if (key) *key = hi->this->key;
if (klen) *klen = hi->this->klen;
if (val) *val = (void *)hi->this->val;
}
static void expand_array(apr_hash_t *ht)
{
apr_hash_index_t *hi;
apr_hash_entry_t **new_array;
unsigned int new_max;
new_max = ht->max * 2 + 1;
new_array = alloc_array(ht, new_max);
for (hi = apr_hash_first(NULL, ht); hi; hi = apr_hash_next(hi)) {
unsigned int i = hi->this->hash & new_max;
hi->this->next = new_array[i];
new_array[i] = hi->this;
}
ht->array = new_array;
ht->max = new_max;
}
static unsigned int hashfunc_default(const char *char_key, apr_ssize_t *klen,
unsigned int hash)
{
const unsigned char *key = (const unsigned char *)char_key;
const unsigned char *p;
apr_ssize_t i;
if (*klen == APR_HASH_KEY_STRING) {
for (p = key; *p; p++) {
hash = hash * 33 + *p;
}
*klen = p - key;
}
else {
for (p = key, i = *klen; i; i--, p++) {
hash = hash * 33 + *p;
}
}
return hash;
}
APR_DECLARE_NONSTD(unsigned int) apr_hashfunc_default(const char *char_key,
apr_ssize_t *klen)
{
return hashfunc_default(char_key, klen, 0);
}
static apr_hash_entry_t **find_entry(apr_hash_t *ht,
const void *key,
apr_ssize_t klen,
const void *val)
{
apr_hash_entry_t **hep, *he;
unsigned int hash;
if (ht->hash_func)
hash = ht->hash_func(key, &klen);
else
hash = hashfunc_default(key, &klen, ht->seed);
for (hep = &ht->array[hash & ht->max], he = *hep;
he; hep = &he->next, he = *hep) {
if (he->hash == hash
&& he->klen == klen
&& memcmp(he->key, key, klen) == 0)
break;
}
if (he || !val)
return hep;
if ((he = ht->free) != NULL)
ht->free = he->next;
else
he = apr_palloc(ht->pool, sizeof(*he));
he->next = NULL;
he->hash = hash;
he->key = key;
he->klen = klen;
he->val = val;
*hep = he;
ht->count++;
return hep;
}
APR_DECLARE(apr_hash_t *) apr_hash_copy(apr_pool_t *pool,
const apr_hash_t *orig)
{
apr_hash_t *ht;
apr_hash_entry_t *new_vals;
unsigned int i, j;
ht = apr_palloc(pool, sizeof(apr_hash_t) +
sizeof(*ht->array) * (orig->max + 1) +
sizeof(apr_hash_entry_t) * orig->count);
ht->pool = pool;
ht->free = NULL;
ht->count = orig->count;
ht->max = orig->max;
ht->seed = orig->seed;
ht->hash_func = orig->hash_func;
ht->array = (apr_hash_entry_t **)((char *)ht + sizeof(apr_hash_t));
new_vals = (apr_hash_entry_t *)((char *)(ht) + sizeof(apr_hash_t) +
sizeof(*ht->array) * (orig->max + 1));
j = 0;
for (i = 0; i <= ht->max; i++) {
apr_hash_entry_t **new_entry = &(ht->array[i]);
apr_hash_entry_t *orig_entry = orig->array[i];
while (orig_entry) {
*new_entry = &new_vals[j++];
(*new_entry)->hash = orig_entry->hash;
(*new_entry)->key = orig_entry->key;
(*new_entry)->klen = orig_entry->klen;
(*new_entry)->val = orig_entry->val;
new_entry = &((*new_entry)->next);
orig_entry = orig_entry->next;
}
*new_entry = NULL;
}
return ht;
}
APR_DECLARE(void *) apr_hash_get(apr_hash_t *ht,
const void *key,
apr_ssize_t klen)
{
apr_hash_entry_t *he;
he = *find_entry(ht, key, klen, NULL);
if (he)
return (void *)he->val;
else
return NULL;
}
APR_DECLARE(void) apr_hash_set(apr_hash_t *ht,
const void *key,
apr_ssize_t klen,
const void *val)
{
apr_hash_entry_t **hep;
hep = find_entry(ht, key, klen, val);
if (*hep) {
if (!val) {
apr_hash_entry_t *old = *hep;
*hep = (*hep)->next;
old->next = ht->free;
ht->free = old;
--ht->count;
}
else {
(*hep)->val = val;
if (ht->count > ht->max) {
expand_array(ht);
}
}
}
}
APR_DECLARE(unsigned int) apr_hash_count(apr_hash_t *ht)
{
return ht->count;
}
APR_DECLARE(void) apr_hash_clear(apr_hash_t *ht)
{
apr_hash_index_t *hi;
for (hi = apr_hash_first(NULL, ht); hi; hi = apr_hash_next(hi))
apr_hash_set(ht, hi->this->key, hi->this->klen, NULL);
}
APR_DECLARE(apr_hash_t*) apr_hash_overlay(apr_pool_t *p,
const apr_hash_t *overlay,
const apr_hash_t *base)
{
return apr_hash_merge(p, overlay, base, NULL, NULL);
}
APR_DECLARE(apr_hash_t *) apr_hash_merge(apr_pool_t *p,
const apr_hash_t *overlay,
const apr_hash_t *base,
void * (*merger)(apr_pool_t *p,
const void *key,
apr_ssize_t klen,
const void *h1_val,
const void *h2_val,
const void *data),
const void *data)
{
apr_hash_t *res;
apr_hash_entry_t *new_vals = NULL;
apr_hash_entry_t *iter;
apr_hash_entry_t *ent;
unsigned int i, j, k, hash;
#if APR_POOL_DEBUG
if (!apr_pool_is_ancestor(overlay->pool, p)) {
fprintf(stderr,
"apr_hash_merge: overlay's pool is not an ancestor of p\n");
abort();
}
if (!apr_pool_is_ancestor(base->pool, p)) {
fprintf(stderr,
"apr_hash_merge: base's pool is not an ancestor of p\n");
abort();
}
#endif
res = apr_palloc(p, sizeof(apr_hash_t));
res->pool = p;
res->free = NULL;
res->hash_func = base->hash_func;
res->count = base->count;
res->max = (overlay->max > base->max) ? overlay->max : base->max;
if (base->count + overlay->count > res->max) {
res->max = res->max * 2 + 1;
}
res->seed = base->seed;
res->array = alloc_array(res, res->max);
if (base->count + overlay->count) {
new_vals = apr_palloc(p, sizeof(apr_hash_entry_t) *
(base->count + overlay->count));
}
j = 0;
for (k = 0; k <= base->max; k++) {
for (iter = base->array[k]; iter; iter = iter->next) {
i = iter->hash & res->max;
new_vals[j].klen = iter->klen;
new_vals[j].key = iter->key;
new_vals[j].val = iter->val;
new_vals[j].hash = iter->hash;
new_vals[j].next = res->array[i];
res->array[i] = &new_vals[j];
j++;
}
}
for (k = 0; k <= overlay->max; k++) {
for (iter = overlay->array[k]; iter; iter = iter->next) {
if (res->hash_func)
hash = res->hash_func(iter->key, &iter->klen);
else
hash = hashfunc_default(iter->key, &iter->klen, res->seed);
i = hash & res->max;
for (ent = res->array[i]; ent; ent = ent->next) {
if ((ent->klen == iter->klen) &&
(memcmp(ent->key, iter->key, iter->klen) == 0)) {
if (merger) {
ent->val = (*merger)(p, iter->key, iter->klen,
iter->val, ent->val, data);
}
else {
ent->val = iter->val;
}
break;
}
}
if (!ent) {
new_vals[j].klen = iter->klen;
new_vals[j].key = iter->key;
new_vals[j].val = iter->val;
new_vals[j].hash = hash;
new_vals[j].next = res->array[i];
res->array[i] = &new_vals[j];
res->count++;
j++;
}
}
}
return res;
}
APR_DECLARE(int) apr_hash_do(apr_hash_do_callback_fn_t *comp,
void *rec, const apr_hash_t *ht)
{
apr_hash_index_t hix;
apr_hash_index_t *hi;
int rv, dorv = 1;
hix.ht = (apr_hash_t *)ht;
hix.index = 0;
hix.this = NULL;
hix.next = NULL;
if ((hi = apr_hash_next(&hix))) {
do {
rv = (*comp)(rec, hi->this->key, hi->this->klen, hi->this->val);
} while (rv && (hi = apr_hash_next(hi)));
if (rv == 0) {
dorv = 0;
}
}
return dorv;
}
APR_POOL_IMPLEMENT_ACCESSOR(hash)