15185a700Sflorian /*
25185a700Sflorian * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
35185a700Sflorian *
45185a700Sflorian * Permission to use, copy, modify, and/or distribute this software for any
55185a700Sflorian * purpose with or without fee is hereby granted, provided that the above
65185a700Sflorian * copyright notice and this permission notice appear in all copies.
75185a700Sflorian *
85185a700Sflorian * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
95185a700Sflorian * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
105185a700Sflorian * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
115185a700Sflorian * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
125185a700Sflorian * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
135185a700Sflorian * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
145185a700Sflorian * PERFORMANCE OF THIS SOFTWARE.
155185a700Sflorian */
165185a700Sflorian
17*1fb015a8Sflorian /* $Id: symtab.c,v 1.6 2020/09/14 08:40:44 florian Exp $ */
185185a700Sflorian
195185a700Sflorian /*! \file */
205185a700Sflorian
215185a700Sflorian #include <ctype.h>
225185a700Sflorian #include <stdlib.h>
235185a700Sflorian #include <string.h>
245185a700Sflorian #include <isc/symtab.h>
255185a700Sflorian #include <isc/util.h>
265185a700Sflorian
275185a700Sflorian typedef struct elt {
285185a700Sflorian char * key;
295185a700Sflorian unsigned int type;
305185a700Sflorian isc_symvalue_t value;
315185a700Sflorian LINK(struct elt) link;
325185a700Sflorian } elt_t;
335185a700Sflorian
345185a700Sflorian typedef LIST(elt_t) eltlist_t;
355185a700Sflorian
365185a700Sflorian struct isc_symtab {
375185a700Sflorian /* Unlocked. */
385185a700Sflorian unsigned int size;
395185a700Sflorian unsigned int count;
405185a700Sflorian unsigned int maxload;
415185a700Sflorian eltlist_t * table;
425185a700Sflorian isc_symtabaction_t undefine_action;
435185a700Sflorian void * undefine_arg;
44*1fb015a8Sflorian int case_sensitive;
455185a700Sflorian };
465185a700Sflorian
475185a700Sflorian isc_result_t
isc_symtab_create(unsigned int size,isc_symtabaction_t undefine_action,void * undefine_arg,int case_sensitive,isc_symtab_t ** symtabp)485185a700Sflorian isc_symtab_create(unsigned int size,
495185a700Sflorian isc_symtabaction_t undefine_action,
505185a700Sflorian void *undefine_arg,
51*1fb015a8Sflorian int case_sensitive,
525185a700Sflorian isc_symtab_t **symtabp)
535185a700Sflorian {
545185a700Sflorian isc_symtab_t *symtab;
555185a700Sflorian unsigned int i;
565185a700Sflorian
575185a700Sflorian REQUIRE(symtabp != NULL && *symtabp == NULL);
585185a700Sflorian REQUIRE(size > 0); /* Should be prime. */
595185a700Sflorian
605185a700Sflorian symtab = (isc_symtab_t *)malloc(sizeof(*symtab));
615185a700Sflorian if (symtab == NULL)
625185a700Sflorian return (ISC_R_NOMEMORY);
635185a700Sflorian
645148cc0dSderaadt symtab->table = (eltlist_t *)reallocarray(NULL, size, sizeof(eltlist_t));
655185a700Sflorian if (symtab->table == NULL) {
665185a700Sflorian free(symtab);
675185a700Sflorian return (ISC_R_NOMEMORY);
685185a700Sflorian }
695185a700Sflorian for (i = 0; i < size; i++)
705185a700Sflorian INIT_LIST(symtab->table[i]);
715185a700Sflorian symtab->size = size;
725185a700Sflorian symtab->count = 0;
735185a700Sflorian symtab->maxload = size * 3 / 4;
745185a700Sflorian symtab->undefine_action = undefine_action;
755185a700Sflorian symtab->undefine_arg = undefine_arg;
765185a700Sflorian symtab->case_sensitive = case_sensitive;
775185a700Sflorian *symtabp = symtab;
785185a700Sflorian return (ISC_R_SUCCESS);
795185a700Sflorian }
805185a700Sflorian
815185a700Sflorian void
isc_symtab_destroy(isc_symtab_t ** symtabp)825185a700Sflorian isc_symtab_destroy(isc_symtab_t **symtabp) {
835185a700Sflorian isc_symtab_t *symtab;
845185a700Sflorian unsigned int i;
855185a700Sflorian elt_t *elt, *nelt;
865185a700Sflorian
875185a700Sflorian REQUIRE(symtabp != NULL);
885185a700Sflorian symtab = *symtabp;
895185a700Sflorian
905185a700Sflorian for (i = 0; i < symtab->size; i++) {
915185a700Sflorian for (elt = HEAD(symtab->table[i]); elt != NULL; elt = nelt) {
925185a700Sflorian nelt = NEXT(elt, link);
935185a700Sflorian if (symtab->undefine_action != NULL)
945185a700Sflorian (symtab->undefine_action)(elt->key,
955185a700Sflorian elt->type,
965185a700Sflorian elt->value,
975185a700Sflorian symtab->undefine_arg);
985185a700Sflorian free(elt);
995185a700Sflorian }
1005185a700Sflorian }
1015185a700Sflorian free(symtab->table);
1025185a700Sflorian free(symtab);
1035185a700Sflorian *symtabp = NULL;
1045185a700Sflorian }
1055185a700Sflorian
1065185a700Sflorian static inline unsigned int
hash(const char * key,int case_sensitive)107*1fb015a8Sflorian hash(const char *key, int case_sensitive) {
1085185a700Sflorian const char *s;
1095185a700Sflorian unsigned int h = 0;
1105185a700Sflorian int c;
1115185a700Sflorian
1125185a700Sflorian /*
1135185a700Sflorian * This hash function is similar to the one Ousterhout
1145185a700Sflorian * uses in Tcl.
1155185a700Sflorian */
1165185a700Sflorian
1175185a700Sflorian if (case_sensitive) {
1185185a700Sflorian for (s = key; *s != '\0'; s++) {
1195185a700Sflorian h += (h << 3) + *s;
1205185a700Sflorian }
1215185a700Sflorian } else {
1225185a700Sflorian for (s = key; *s != '\0'; s++) {
1235185a700Sflorian c = *s;
1245185a700Sflorian c = tolower((unsigned char)c);
1255185a700Sflorian h += (h << 3) + c;
1265185a700Sflorian }
1275185a700Sflorian }
1285185a700Sflorian
1295185a700Sflorian return (h);
1305185a700Sflorian }
1315185a700Sflorian
1325185a700Sflorian #define FIND(s, k, t, b, e) \
1335185a700Sflorian b = hash((k), (s)->case_sensitive) % (s)->size; \
1345185a700Sflorian if ((s)->case_sensitive) { \
1355185a700Sflorian for (e = HEAD((s)->table[b]); e != NULL; e = NEXT(e, link)) { \
1365185a700Sflorian if (((t) == 0 || e->type == (t)) && \
1375185a700Sflorian strcmp(e->key, (k)) == 0) \
1385185a700Sflorian break; \
1395185a700Sflorian } \
1405185a700Sflorian } else { \
1415185a700Sflorian for (e = HEAD((s)->table[b]); e != NULL; e = NEXT(e, link)) { \
1425185a700Sflorian if (((t) == 0 || e->type == (t)) && \
1435185a700Sflorian strcasecmp(e->key, (k)) == 0) \
1445185a700Sflorian break; \
1455185a700Sflorian } \
1465185a700Sflorian }
1475185a700Sflorian
1485185a700Sflorian isc_result_t
isc_symtab_lookup(isc_symtab_t * symtab,const char * key,unsigned int type,isc_symvalue_t * value)1495185a700Sflorian isc_symtab_lookup(isc_symtab_t *symtab, const char *key, unsigned int type,
1505185a700Sflorian isc_symvalue_t *value)
1515185a700Sflorian {
1525185a700Sflorian unsigned int bucket;
1535185a700Sflorian elt_t *elt;
1545185a700Sflorian
1555185a700Sflorian REQUIRE(key != NULL);
1565185a700Sflorian
1575185a700Sflorian FIND(symtab, key, type, bucket, elt);
1585185a700Sflorian
1595185a700Sflorian if (elt == NULL)
1605185a700Sflorian return (ISC_R_NOTFOUND);
1615185a700Sflorian
1625185a700Sflorian if (value != NULL)
1635185a700Sflorian *value = elt->value;
1645185a700Sflorian
1655185a700Sflorian return (ISC_R_SUCCESS);
1665185a700Sflorian }
1675185a700Sflorian
1685185a700Sflorian static void
grow_table(isc_symtab_t * symtab)1695185a700Sflorian grow_table(isc_symtab_t *symtab) {
1705185a700Sflorian eltlist_t *newtable;
1715185a700Sflorian unsigned int i, newsize, newmax;
1725185a700Sflorian
1735185a700Sflorian REQUIRE(symtab != NULL);
1745185a700Sflorian
1755185a700Sflorian newsize = symtab->size * 2;
1765185a700Sflorian newmax = newsize * 3 / 4;
1775185a700Sflorian INSIST(newsize > 0U && newmax > 0U);
1785185a700Sflorian
1795148cc0dSderaadt newtable = reallocarray(NULL, newsize, sizeof(eltlist_t));
1805185a700Sflorian if (newtable == NULL)
1815185a700Sflorian return;
1825185a700Sflorian
1835185a700Sflorian for (i = 0; i < newsize; i++)
1845185a700Sflorian INIT_LIST(newtable[i]);
1855185a700Sflorian
1865185a700Sflorian for (i = 0; i < symtab->size; i++) {
1875185a700Sflorian elt_t *elt, *nelt;
1885185a700Sflorian
1895185a700Sflorian for (elt = HEAD(symtab->table[i]); elt != NULL; elt = nelt) {
1905185a700Sflorian unsigned int hv;
1915185a700Sflorian
1925185a700Sflorian nelt = NEXT(elt, link);
1935185a700Sflorian
1945185a700Sflorian UNLINK(symtab->table[i], elt, link);
1955185a700Sflorian hv = hash(elt->key, symtab->case_sensitive);
1965185a700Sflorian APPEND(newtable[hv % newsize], elt, link);
1975185a700Sflorian }
1985185a700Sflorian }
1995185a700Sflorian
2005185a700Sflorian free(symtab->table);
2015185a700Sflorian
2025185a700Sflorian symtab->table = newtable;
2035185a700Sflorian symtab->size = newsize;
2045185a700Sflorian symtab->maxload = newmax;
2055185a700Sflorian }
2065185a700Sflorian
2075185a700Sflorian isc_result_t
isc_symtab_define(isc_symtab_t * symtab,const char * key,unsigned int type,isc_symvalue_t value,isc_symexists_t exists_policy)2085185a700Sflorian isc_symtab_define(isc_symtab_t *symtab, const char *key, unsigned int type,
2095185a700Sflorian isc_symvalue_t value, isc_symexists_t exists_policy)
2105185a700Sflorian {
2115185a700Sflorian unsigned int bucket;
2125185a700Sflorian elt_t *elt;
2135185a700Sflorian
2145185a700Sflorian REQUIRE(key != NULL);
2155185a700Sflorian REQUIRE(type != 0);
2165185a700Sflorian
2175185a700Sflorian FIND(symtab, key, type, bucket, elt);
2185185a700Sflorian
2195185a700Sflorian if (exists_policy != isc_symexists_add && elt != NULL) {
2205185a700Sflorian if (exists_policy == isc_symexists_reject)
2215185a700Sflorian return (ISC_R_EXISTS);
2225185a700Sflorian INSIST(exists_policy == isc_symexists_replace);
2235185a700Sflorian UNLINK(symtab->table[bucket], elt, link);
2245185a700Sflorian if (symtab->undefine_action != NULL)
2255185a700Sflorian (symtab->undefine_action)(elt->key, elt->type,
2265185a700Sflorian elt->value,
2275185a700Sflorian symtab->undefine_arg);
2285185a700Sflorian } else {
2295185a700Sflorian elt = (elt_t *)malloc(sizeof(*elt));
2305185a700Sflorian if (elt == NULL)
2315185a700Sflorian return (ISC_R_NOMEMORY);
2325185a700Sflorian ISC_LINK_INIT(elt, link);
2335185a700Sflorian symtab->count++;
2345185a700Sflorian }
2355185a700Sflorian
2365185a700Sflorian /*
2375185a700Sflorian * Though the "key" can be const coming in, it is not stored as const
2385185a700Sflorian * so that the calling program can easily have writable access to
2395185a700Sflorian * it in its undefine_action function. In the event that it *was*
2405185a700Sflorian * truly const coming in and then the caller modified it anyway ...
2415185a700Sflorian * well, don't do that!
2425185a700Sflorian */
2435185a700Sflorian DE_CONST(key, elt->key);
2445185a700Sflorian elt->type = type;
2455185a700Sflorian elt->value = value;
2465185a700Sflorian
2475185a700Sflorian /*
2485185a700Sflorian * We prepend so that the most recent definition will be found.
2495185a700Sflorian */
2505185a700Sflorian PREPEND(symtab->table[bucket], elt, link);
2515185a700Sflorian
2525185a700Sflorian if (symtab->count > symtab->maxload)
2535185a700Sflorian grow_table(symtab);
2545185a700Sflorian
2555185a700Sflorian return (ISC_R_SUCCESS);
2565185a700Sflorian }
257