1998-12-18 19:06:16 +00:00
|
|
|
/*
|
1999-01-06 05:33:05 +00:00
|
|
|
* Copyright (C) 1996, 1997, 1998, 1999 Internet Software Consortium.
|
1998-12-18 19:06:16 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
|
|
|
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
|
|
|
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
|
|
|
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
1998-12-18 22:20:02 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
1998-12-18 19:06:16 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <isc/assertions.h>
|
|
|
|
#include <isc/list.h>
|
|
|
|
#include <isc/symtab.h>
|
|
|
|
|
1998-12-18 21:17:37 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
1998-12-18 19:06:16 +00:00
|
|
|
typedef struct elt {
|
|
|
|
char * key;
|
|
|
|
unsigned int type;
|
1998-12-18 21:17:37 +00:00
|
|
|
isc_symvalue_t value;
|
1998-12-18 19:06:16 +00:00
|
|
|
LINK(struct elt) link;
|
|
|
|
} elt_t;
|
|
|
|
|
|
|
|
typedef LIST(elt_t) eltlist_t;
|
|
|
|
|
1998-12-18 21:17:37 +00:00
|
|
|
#define SYMTAB_MAGIC 0x53796D54U /* SymT. */
|
|
|
|
#define VALID_SYMTAB(st) ((st) != NULL && \
|
|
|
|
(st)->magic == SYMTAB_MAGIC)
|
|
|
|
|
1998-12-18 19:06:16 +00:00
|
|
|
struct isc_symtab {
|
1998-12-18 21:17:37 +00:00
|
|
|
/* Unlocked. */
|
|
|
|
unsigned int magic;
|
1998-12-18 19:06:16 +00:00
|
|
|
isc_mem_t * mctx;
|
|
|
|
unsigned int size;
|
|
|
|
eltlist_t * table;
|
1998-12-18 21:17:37 +00:00
|
|
|
isc_symtabaction_t undefine_action;
|
1999-06-08 13:02:10 +00:00
|
|
|
void * undefine_arg;
|
1999-01-04 22:30:28 +00:00
|
|
|
isc_boolean_t case_sensitive;
|
1998-12-18 19:06:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_symtab_create(isc_mem_t *mctx, unsigned int size,
|
1998-12-18 21:17:37 +00:00
|
|
|
isc_symtabaction_t undefine_action,
|
1999-06-08 13:02:10 +00:00
|
|
|
void *undefine_arg,
|
1999-01-04 22:30:28 +00:00
|
|
|
isc_boolean_t case_sensitive,
|
1998-12-18 19:06:16 +00:00
|
|
|
isc_symtab_t **symtabp)
|
|
|
|
{
|
|
|
|
isc_symtab_t *symtab;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
REQUIRE(mctx != NULL);
|
|
|
|
REQUIRE(symtabp != NULL && *symtabp == NULL);
|
|
|
|
REQUIRE(size > 0); /* Should be prime. */
|
|
|
|
|
|
|
|
symtab = (isc_symtab_t *)isc_mem_get(mctx, sizeof *symtab);
|
|
|
|
if (symtab == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
symtab->table = (eltlist_t *)isc_mem_get(mctx,
|
|
|
|
size * sizeof (eltlist_t));
|
|
|
|
if (symtab->table == NULL) {
|
|
|
|
isc_mem_put(mctx, symtab, sizeof *symtab);
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
INIT_LIST(symtab->table[i]);
|
|
|
|
symtab->mctx = mctx;
|
|
|
|
symtab->size = size;
|
1998-12-18 21:17:37 +00:00
|
|
|
symtab->undefine_action = undefine_action;
|
1999-06-08 13:02:10 +00:00
|
|
|
symtab->undefine_arg = undefine_arg;
|
1999-01-04 22:30:28 +00:00
|
|
|
symtab->case_sensitive = case_sensitive;
|
1998-12-18 21:17:37 +00:00
|
|
|
symtab->magic = SYMTAB_MAGIC;
|
1998-12-18 19:06:16 +00:00
|
|
|
|
|
|
|
*symtabp = symtab;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_symtab_destroy(isc_symtab_t **symtabp) {
|
|
|
|
isc_symtab_t *symtab;
|
|
|
|
unsigned int i;
|
|
|
|
elt_t *elt, *nelt;
|
|
|
|
|
1998-12-18 21:17:37 +00:00
|
|
|
REQUIRE(symtabp != NULL);
|
1998-12-18 19:06:16 +00:00
|
|
|
symtab = *symtabp;
|
1998-12-18 21:17:37 +00:00
|
|
|
REQUIRE(VALID_SYMTAB(symtab));
|
1998-12-18 19:06:16 +00:00
|
|
|
|
|
|
|
for (i = 0; i < symtab->size; i++) {
|
|
|
|
for (elt = HEAD(symtab->table[i]); elt != NULL; elt = nelt) {
|
|
|
|
nelt = NEXT(elt, link);
|
1998-12-18 21:17:37 +00:00
|
|
|
if (symtab->undefine_action != NULL)
|
|
|
|
(symtab->undefine_action)(elt->key,
|
|
|
|
elt->type,
|
1999-06-08 13:02:10 +00:00
|
|
|
elt->value,
|
|
|
|
symtab->undefine_arg);
|
1998-12-18 19:06:16 +00:00
|
|
|
isc_mem_put(symtab->mctx, elt, sizeof *elt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
isc_mem_put(symtab->mctx, symtab->table,
|
|
|
|
symtab->size * sizeof (eltlist_t));
|
1998-12-18 21:17:37 +00:00
|
|
|
symtab->magic = 0;
|
1998-12-18 19:06:16 +00:00
|
|
|
isc_mem_put(symtab->mctx, symtab, sizeof *symtab);
|
|
|
|
|
|
|
|
*symtabp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int
|
1999-01-04 22:30:28 +00:00
|
|
|
hash(const char *key, isc_boolean_t case_sensitive) {
|
1998-12-18 19:06:16 +00:00
|
|
|
const char *s;
|
|
|
|
unsigned int h = 0;
|
|
|
|
unsigned int g;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* P. J. Weinberger's hash function, adapted from p. 436 of
|
|
|
|
* _Compilers: Principles, Techniques, and Tools_, Aho, Sethi
|
|
|
|
* and Ullman, Addison-Wesley, 1986, ISBN 0-201-10088-6.
|
|
|
|
*/
|
|
|
|
|
1999-01-04 22:30:28 +00:00
|
|
|
if (case_sensitive) {
|
|
|
|
for (s = key; *s != '\0'; s++) {
|
|
|
|
h = ( h << 4 ) + *s;
|
|
|
|
if ((g = ( h & 0xf0000000 )) != 0) {
|
|
|
|
h = h ^ (g >> 24);
|
|
|
|
h = h ^ g;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (s = key; *s != '\0'; s++) {
|
|
|
|
c = *s;
|
|
|
|
if (isascii(c) && isupper(c))
|
|
|
|
c = tolower(c);
|
|
|
|
h = ( h << 4 ) + c;
|
|
|
|
if ((g = ( h & 0xf0000000 )) != 0) {
|
|
|
|
h = h ^ (g >> 24);
|
|
|
|
h = h ^ g;
|
|
|
|
}
|
1998-12-18 19:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (h);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define FIND(s, k, t, b, e) \
|
1999-01-04 22:30:28 +00:00
|
|
|
b = hash((k), (s)->case_sensitive) % (s)->size; \
|
|
|
|
if ((s)->case_sensitive) { \
|
|
|
|
for (e = HEAD((s)->table[b]); e != NULL; e = NEXT(e, link)) { \
|
|
|
|
if (((t) == 0 || e->type == (t)) && \
|
|
|
|
strcmp(e->key, (k)) == 0) \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} else { \
|
|
|
|
for (e = HEAD((s)->table[b]); e != NULL; e = NEXT(e, link)) { \
|
|
|
|
if (((t) == 0 || e->type == (t)) && \
|
|
|
|
strcasecmp(e->key, (k)) == 0) \
|
|
|
|
break; \
|
|
|
|
} \
|
1998-12-18 19:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_symtab_lookup(isc_symtab_t *symtab, const char *key, unsigned int type,
|
|
|
|
isc_symvalue_t *value)
|
|
|
|
{
|
|
|
|
unsigned int bucket;
|
|
|
|
elt_t *elt;
|
|
|
|
|
1998-12-18 21:17:37 +00:00
|
|
|
REQUIRE(VALID_SYMTAB(symtab));
|
|
|
|
REQUIRE(key != NULL);
|
|
|
|
|
1998-12-18 19:06:16 +00:00
|
|
|
FIND(symtab, key, type, bucket, elt);
|
|
|
|
|
|
|
|
if (elt == NULL)
|
|
|
|
return (ISC_R_NOTFOUND);
|
|
|
|
|
|
|
|
if (value != NULL)
|
|
|
|
*value = elt->value;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_symtab_define(isc_symtab_t *symtab, char *key, unsigned int type,
|
1998-12-19 00:13:59 +00:00
|
|
|
isc_symvalue_t value, isc_symexists_t exists_policy)
|
1998-12-18 19:06:16 +00:00
|
|
|
{
|
|
|
|
unsigned int bucket;
|
|
|
|
elt_t *elt;
|
|
|
|
|
1998-12-18 21:17:37 +00:00
|
|
|
REQUIRE(VALID_SYMTAB(symtab));
|
|
|
|
REQUIRE(key != NULL);
|
1998-12-19 00:15:19 +00:00
|
|
|
REQUIRE(type != 0);
|
1998-12-18 21:17:37 +00:00
|
|
|
|
1998-12-18 19:06:16 +00:00
|
|
|
FIND(symtab, key, type, bucket, elt);
|
|
|
|
|
1998-12-19 00:13:59 +00:00
|
|
|
if (exists_policy != isc_symexists_add && elt != NULL) {
|
|
|
|
if (exists_policy == isc_symexists_reject)
|
|
|
|
return (ISC_R_EXISTS);
|
|
|
|
INSIST(exists_policy == isc_symexists_replace);
|
|
|
|
UNLINK(symtab->table[bucket], elt, link);
|
|
|
|
if (symtab->undefine_action != NULL)
|
|
|
|
(symtab->undefine_action)(elt->key, elt->type,
|
1999-06-08 13:02:10 +00:00
|
|
|
elt->value,
|
|
|
|
symtab->undefine_arg);
|
1998-12-19 00:13:59 +00:00
|
|
|
} else {
|
|
|
|
elt = (elt_t *)isc_mem_get(symtab->mctx, sizeof *elt);
|
|
|
|
if (elt == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
1998-12-18 19:06:16 +00:00
|
|
|
elt->key = key;
|
|
|
|
elt->type = type;
|
|
|
|
elt->value = value;
|
1998-12-18 21:17:37 +00:00
|
|
|
|
1998-12-19 00:13:59 +00:00
|
|
|
/*
|
1998-12-19 00:15:50 +00:00
|
|
|
* We prepend so that the most recent definition will be found.
|
1998-12-19 00:13:59 +00:00
|
|
|
*/
|
|
|
|
PREPEND(symtab->table[bucket], elt, link);
|
1998-12-18 19:06:16 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_symtab_undefine(isc_symtab_t *symtab, char *key, unsigned int type) {
|
|
|
|
unsigned int bucket;
|
|
|
|
elt_t *elt;
|
|
|
|
|
1998-12-18 21:17:37 +00:00
|
|
|
REQUIRE(VALID_SYMTAB(symtab));
|
|
|
|
REQUIRE(key != NULL);
|
|
|
|
|
1998-12-18 19:06:16 +00:00
|
|
|
FIND(symtab, key, type, bucket, elt);
|
|
|
|
|
|
|
|
if (elt == NULL)
|
|
|
|
return (ISC_R_NOTFOUND);
|
|
|
|
|
1998-12-18 21:17:37 +00:00
|
|
|
if (symtab->undefine_action != NULL)
|
1999-06-08 13:02:10 +00:00
|
|
|
(symtab->undefine_action)(elt->key, elt->type,
|
|
|
|
elt->value, symtab->undefine_arg);
|
1998-12-18 19:06:16 +00:00
|
|
|
UNLINK(symtab->table[bucket], elt, link);
|
|
|
|
isc_mem_put(symtab->mctx, elt, sizeof *elt);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|