xref: /freebsd/crypto/heimdal/lib/asn1/hash.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov  * Copyright (c) 1997 Kungliga Tekniska Högskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray /*
35b528cefcSMark Murray  * Hash table functions
36b528cefcSMark Murray  */
37b528cefcSMark Murray 
38b528cefcSMark Murray #include "gen_locl.h"
39b528cefcSMark Murray 
40*ae771770SStanislav Sedov RCSID("$Id$");
41b528cefcSMark Murray 
42b528cefcSMark Murray static Hashentry *_search(Hashtab * htab,	/* The hash table */
43b528cefcSMark Murray 			  void *ptr);	/* And key */
44b528cefcSMark Murray 
45b528cefcSMark Murray Hashtab *
hashtabnew(int sz,int (* cmp)(void *,void *),unsigned (* hash)(void *))46b528cefcSMark Murray hashtabnew(int sz,
47b528cefcSMark Murray 	   int (*cmp) (void *, void *),
48b528cefcSMark Murray 	   unsigned (*hash) (void *))
49b528cefcSMark Murray {
50b528cefcSMark Murray     Hashtab *htab;
51b528cefcSMark Murray     int i;
52b528cefcSMark Murray 
53b528cefcSMark Murray     assert(sz > 0);
54b528cefcSMark Murray 
55b528cefcSMark Murray     htab = (Hashtab *) malloc(sizeof(Hashtab) + (sz - 1) * sizeof(Hashentry *));
56c19800e8SDoug Rabson     if (htab == NULL)
57c19800e8SDoug Rabson 	return NULL;
58c19800e8SDoug Rabson 
59b528cefcSMark Murray     for (i = 0; i < sz; ++i)
60b528cefcSMark Murray 	htab->tab[i] = NULL;
61b528cefcSMark Murray 
62b528cefcSMark Murray     htab->cmp = cmp;
63b528cefcSMark Murray     htab->hash = hash;
64b528cefcSMark Murray     htab->sz = sz;
65b528cefcSMark Murray     return htab;
66b528cefcSMark Murray }
67b528cefcSMark Murray 
68b528cefcSMark Murray /* Intern search function */
69b528cefcSMark Murray 
70b528cefcSMark Murray static Hashentry *
_search(Hashtab * htab,void * ptr)71b528cefcSMark Murray _search(Hashtab * htab, void *ptr)
72b528cefcSMark Murray {
73b528cefcSMark Murray     Hashentry *hptr;
74b528cefcSMark Murray 
75b528cefcSMark Murray     assert(htab && ptr);
76b528cefcSMark Murray 
77b528cefcSMark Murray     for (hptr = htab->tab[(*htab->hash) (ptr) % htab->sz];
78b528cefcSMark Murray 	 hptr;
79b528cefcSMark Murray 	 hptr = hptr->next)
80b528cefcSMark Murray 	if ((*htab->cmp) (ptr, hptr->ptr) == 0)
81b528cefcSMark Murray 	    break;
82b528cefcSMark Murray     return hptr;
83b528cefcSMark Murray }
84b528cefcSMark Murray 
85b528cefcSMark Murray /* Search for element in hash table */
86b528cefcSMark Murray 
87b528cefcSMark Murray void *
hashtabsearch(Hashtab * htab,void * ptr)88b528cefcSMark Murray hashtabsearch(Hashtab * htab, void *ptr)
89b528cefcSMark Murray {
90b528cefcSMark Murray     Hashentry *tmp;
91b528cefcSMark Murray 
92b528cefcSMark Murray     tmp = _search(htab, ptr);
93b528cefcSMark Murray     return tmp ? tmp->ptr : tmp;
94b528cefcSMark Murray }
95b528cefcSMark Murray 
96b528cefcSMark Murray /* add element to hash table */
97b528cefcSMark Murray /* if already there, set new value */
98b528cefcSMark Murray /* !NULL if succesful */
99b528cefcSMark Murray 
100b528cefcSMark Murray void *
hashtabadd(Hashtab * htab,void * ptr)101b528cefcSMark Murray hashtabadd(Hashtab * htab, void *ptr)
102b528cefcSMark Murray {
103b528cefcSMark Murray     Hashentry *h = _search(htab, ptr);
104b528cefcSMark Murray     Hashentry **tabptr;
105b528cefcSMark Murray 
106b528cefcSMark Murray     assert(htab && ptr);
107b528cefcSMark Murray 
108b528cefcSMark Murray     if (h)
109b528cefcSMark Murray 	free((void *) h->ptr);
110b528cefcSMark Murray     else {
111b528cefcSMark Murray 	h = (Hashentry *) malloc(sizeof(Hashentry));
112b528cefcSMark Murray 	if (h == NULL) {
113b528cefcSMark Murray 	    return NULL;
114b528cefcSMark Murray 	}
115b528cefcSMark Murray 	tabptr = &htab->tab[(*htab->hash) (ptr) % htab->sz];
116b528cefcSMark Murray 	h->next = *tabptr;
117b528cefcSMark Murray 	*tabptr = h;
118b528cefcSMark Murray 	h->prev = tabptr;
119b528cefcSMark Murray 	if (h->next)
120b528cefcSMark Murray 	    h->next->prev = &h->next;
121b528cefcSMark Murray     }
122b528cefcSMark Murray     h->ptr = ptr;
123b528cefcSMark Murray     return h;
124b528cefcSMark Murray }
125b528cefcSMark Murray 
126b528cefcSMark Murray /* delete element with key key. Iff freep, free Hashentry->ptr */
127b528cefcSMark Murray 
128b528cefcSMark Murray int
_hashtabdel(Hashtab * htab,void * ptr,int freep)129b528cefcSMark Murray _hashtabdel(Hashtab * htab, void *ptr, int freep)
130b528cefcSMark Murray {
131b528cefcSMark Murray     Hashentry *h;
132b528cefcSMark Murray 
133b528cefcSMark Murray     assert(htab && ptr);
134b528cefcSMark Murray 
135b528cefcSMark Murray     h = _search(htab, ptr);
136b528cefcSMark Murray     if (h) {
137b528cefcSMark Murray 	if (freep)
138b528cefcSMark Murray 	    free(h->ptr);
139b528cefcSMark Murray 	if ((*(h->prev) = h->next))
140b528cefcSMark Murray 	    h->next->prev = h->prev;
141b528cefcSMark Murray 	free(h);
142b528cefcSMark Murray 	return 0;
143b528cefcSMark Murray     } else
144b528cefcSMark Murray 	return -1;
145b528cefcSMark Murray }
146b528cefcSMark Murray 
147b528cefcSMark Murray /* Do something for each element */
148b528cefcSMark Murray 
149b528cefcSMark Murray void
hashtabforeach(Hashtab * htab,int (* func)(void * ptr,void * arg),void * arg)150b528cefcSMark Murray hashtabforeach(Hashtab * htab, int (*func) (void *ptr, void *arg),
151b528cefcSMark Murray 	       void *arg)
152b528cefcSMark Murray {
153b528cefcSMark Murray     Hashentry **h, *g;
154b528cefcSMark Murray 
155b528cefcSMark Murray     assert(htab);
156b528cefcSMark Murray 
157b528cefcSMark Murray     for (h = htab->tab; h < &htab->tab[htab->sz]; ++h)
158b528cefcSMark Murray 	for (g = *h; g; g = g->next)
159b528cefcSMark Murray 	    if ((*func) (g->ptr, arg))
160b528cefcSMark Murray 		return;
161b528cefcSMark Murray }
162b528cefcSMark Murray 
163b528cefcSMark Murray /* standard hash-functions for strings */
164b528cefcSMark Murray 
165b528cefcSMark Murray unsigned
hashadd(const char * s)166b528cefcSMark Murray hashadd(const char *s)
167b528cefcSMark Murray {				/* Standard hash function */
168b528cefcSMark Murray     unsigned i;
169b528cefcSMark Murray 
170b528cefcSMark Murray     assert(s);
171b528cefcSMark Murray 
172b528cefcSMark Murray     for (i = 0; *s; ++s)
173b528cefcSMark Murray 	i += *s;
174b528cefcSMark Murray     return i;
175b528cefcSMark Murray }
176b528cefcSMark Murray 
177b528cefcSMark Murray unsigned
hashcaseadd(const char * s)178b528cefcSMark Murray hashcaseadd(const char *s)
179b528cefcSMark Murray {				/* Standard hash function */
180b528cefcSMark Murray     unsigned i;
181b528cefcSMark Murray 
182b528cefcSMark Murray     assert(s);
183b528cefcSMark Murray 
184b528cefcSMark Murray     for (i = 0; *s; ++s)
185c19800e8SDoug Rabson 	i += toupper((unsigned char)*s);
186b528cefcSMark Murray     return i;
187b528cefcSMark Murray }
188b528cefcSMark Murray 
189b528cefcSMark Murray #define TWELVE (sizeof(unsigned))
190b528cefcSMark Murray #define SEVENTYFIVE (6*sizeof(unsigned))
191b528cefcSMark Murray #define HIGH_BITS (~((unsigned)(~0) >> TWELVE))
192b528cefcSMark Murray 
193b528cefcSMark Murray unsigned
hashjpw(const char * ss)194b528cefcSMark Murray hashjpw(const char *ss)
195b528cefcSMark Murray {				/* another hash function */
196b528cefcSMark Murray     unsigned h = 0;
197b528cefcSMark Murray     unsigned g;
198b528cefcSMark Murray     const unsigned char *s = (const unsigned char *)ss;
199b528cefcSMark Murray 
200b528cefcSMark Murray     for (; *s; ++s) {
201b528cefcSMark Murray 	h = (h << TWELVE) + *s;
202b528cefcSMark Murray 	if ((g = h & HIGH_BITS))
203b528cefcSMark Murray 	    h = (h ^ (g >> SEVENTYFIVE)) & ~HIGH_BITS;
204b528cefcSMark Murray     }
205b528cefcSMark Murray     return h;
206b528cefcSMark Murray }
207