1*c2c66affSColin Finck /* hash.c
2*c2c66affSColin Finck
3*c2c66affSColin Finck Routines for manipulating hash tables... */
4*c2c66affSColin Finck
5*c2c66affSColin Finck /*
6*c2c66affSColin Finck * Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium.
7*c2c66affSColin Finck * All rights reserved.
8*c2c66affSColin Finck *
9*c2c66affSColin Finck * Redistribution and use in source and binary forms, with or without
10*c2c66affSColin Finck * modification, are permitted provided that the following conditions
11*c2c66affSColin Finck * are met:
12*c2c66affSColin Finck *
13*c2c66affSColin Finck * 1. Redistributions of source code must retain the above copyright
14*c2c66affSColin Finck * notice, this list of conditions and the following disclaimer.
15*c2c66affSColin Finck * 2. Redistributions in binary form must reproduce the above copyright
16*c2c66affSColin Finck * notice, this list of conditions and the following disclaimer in the
17*c2c66affSColin Finck * documentation and/or other materials provided with the distribution.
18*c2c66affSColin Finck * 3. Neither the name of The Internet Software Consortium nor the names
19*c2c66affSColin Finck * of its contributors may be used to endorse or promote products derived
20*c2c66affSColin Finck * from this software without specific prior written permission.
21*c2c66affSColin Finck *
22*c2c66affSColin Finck * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23*c2c66affSColin Finck * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24*c2c66affSColin Finck * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25*c2c66affSColin Finck * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26*c2c66affSColin Finck * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27*c2c66affSColin Finck * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28*c2c66affSColin Finck * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29*c2c66affSColin Finck * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30*c2c66affSColin Finck * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31*c2c66affSColin Finck * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32*c2c66affSColin Finck * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33*c2c66affSColin Finck * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34*c2c66affSColin Finck * SUCH DAMAGE.
35*c2c66affSColin Finck *
36*c2c66affSColin Finck * This software has been written for the Internet Software Consortium
37*c2c66affSColin Finck * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
38*c2c66affSColin Finck * Enterprises. To learn more about the Internet Software Consortium,
39*c2c66affSColin Finck * see ``http://www.vix.com/isc''. To learn more about Vixie
40*c2c66affSColin Finck * Enterprises, see ``http://www.vix.com''.
41*c2c66affSColin Finck */
42*c2c66affSColin Finck
43*c2c66affSColin Finck #define lint
44*c2c66affSColin Finck #ifndef lint
45*c2c66affSColin Finck static char copyright[] =
46*c2c66affSColin Finck "$Id: hash.c,v 1.9.2.3 1999/04/09 17:39:41 mellon Exp $ Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium. All rights reserved.\n";
47*c2c66affSColin Finck #endif /* not lint */
48*c2c66affSColin Finck
49*c2c66affSColin Finck #include <rosdhcp.h>
50*c2c66affSColin Finck
51*c2c66affSColin Finck static __inline int do_hash PROTO ((unsigned char *, int, int));
52*c2c66affSColin Finck
new_hash()53*c2c66affSColin Finck struct hash_table *new_hash ()
54*c2c66affSColin Finck {
55*c2c66affSColin Finck struct hash_table *rv = new_hash_table (DEFAULT_HASH_SIZE);
56*c2c66affSColin Finck if (!rv)
57*c2c66affSColin Finck return rv;
58*c2c66affSColin Finck memset (&rv -> buckets [0], 0,
59*c2c66affSColin Finck DEFAULT_HASH_SIZE * sizeof (struct hash_bucket *));
60*c2c66affSColin Finck return rv;
61*c2c66affSColin Finck }
62*c2c66affSColin Finck
do_hash(name,len,size)63*c2c66affSColin Finck static __inline int do_hash (name, len, size)
64*c2c66affSColin Finck unsigned char *name;
65*c2c66affSColin Finck int len;
66*c2c66affSColin Finck int size;
67*c2c66affSColin Finck {
68*c2c66affSColin Finck register int accum = 0;
69*c2c66affSColin Finck register unsigned char *s = name;
70*c2c66affSColin Finck int i = len;
71*c2c66affSColin Finck while (i--) {
72*c2c66affSColin Finck /* Add the character in... */
73*c2c66affSColin Finck accum += *s++;
74*c2c66affSColin Finck /* Add carry back in... */
75*c2c66affSColin Finck while (accum > 255) {
76*c2c66affSColin Finck accum = (accum & 255) + (accum >> 8);
77*c2c66affSColin Finck }
78*c2c66affSColin Finck }
79*c2c66affSColin Finck return accum % size;
80*c2c66affSColin Finck }
81*c2c66affSColin Finck
add_hash(table,name,len,pointer)82*c2c66affSColin Finck void add_hash (table, name, len, pointer)
83*c2c66affSColin Finck struct hash_table *table;
84*c2c66affSColin Finck int len;
85*c2c66affSColin Finck unsigned char *name;
86*c2c66affSColin Finck unsigned char *pointer;
87*c2c66affSColin Finck {
88*c2c66affSColin Finck int hashno;
89*c2c66affSColin Finck struct hash_bucket *bp;
90*c2c66affSColin Finck
91*c2c66affSColin Finck if (!table)
92*c2c66affSColin Finck return;
93*c2c66affSColin Finck if (!len)
94*c2c66affSColin Finck len = strlen ((char *)name);
95*c2c66affSColin Finck
96*c2c66affSColin Finck hashno = do_hash (name, len, table -> hash_count);
97*c2c66affSColin Finck bp = new_hash_bucket ();
98*c2c66affSColin Finck
99*c2c66affSColin Finck if (!bp) {
100*c2c66affSColin Finck warn ("Can't add %s to hash table.", name);
101*c2c66affSColin Finck return;
102*c2c66affSColin Finck }
103*c2c66affSColin Finck bp -> name = name;
104*c2c66affSColin Finck bp -> value = pointer;
105*c2c66affSColin Finck bp -> next = table -> buckets [hashno];
106*c2c66affSColin Finck bp -> len = len;
107*c2c66affSColin Finck table -> buckets [hashno] = bp;
108*c2c66affSColin Finck }
109*c2c66affSColin Finck
delete_hash_entry(table,name,len)110*c2c66affSColin Finck void delete_hash_entry (table, name, len)
111*c2c66affSColin Finck struct hash_table *table;
112*c2c66affSColin Finck int len;
113*c2c66affSColin Finck unsigned char *name;
114*c2c66affSColin Finck {
115*c2c66affSColin Finck int hashno;
116*c2c66affSColin Finck struct hash_bucket *bp, *pbp = (struct hash_bucket *)0;
117*c2c66affSColin Finck
118*c2c66affSColin Finck if (!table)
119*c2c66affSColin Finck return;
120*c2c66affSColin Finck if (!len)
121*c2c66affSColin Finck len = strlen ((char *)name);
122*c2c66affSColin Finck
123*c2c66affSColin Finck hashno = do_hash (name, len, table -> hash_count);
124*c2c66affSColin Finck
125*c2c66affSColin Finck /* Go through the list looking for an entry that matches;
126*c2c66affSColin Finck if we find it, delete it. */
127*c2c66affSColin Finck for (bp = table -> buckets [hashno]; bp; bp = bp -> next) {
128*c2c66affSColin Finck if ((!bp -> len &&
129*c2c66affSColin Finck !strcmp ((char *)bp -> name, (char *)name)) ||
130*c2c66affSColin Finck (bp -> len == len &&
131*c2c66affSColin Finck !memcmp (bp -> name, name, len))) {
132*c2c66affSColin Finck if (pbp) {
133*c2c66affSColin Finck pbp -> next = bp -> next;
134*c2c66affSColin Finck } else {
135*c2c66affSColin Finck table -> buckets [hashno] = bp -> next;
136*c2c66affSColin Finck }
137*c2c66affSColin Finck free_hash_bucket (bp, "delete_hash_entry");
138*c2c66affSColin Finck break;
139*c2c66affSColin Finck }
140*c2c66affSColin Finck pbp = bp; /* jwg, 9/6/96 - nice catch! */
141*c2c66affSColin Finck }
142*c2c66affSColin Finck }
143*c2c66affSColin Finck
hash_lookup(table,name,len)144*c2c66affSColin Finck unsigned char *hash_lookup (table, name, len)
145*c2c66affSColin Finck struct hash_table *table;
146*c2c66affSColin Finck unsigned char *name;
147*c2c66affSColin Finck int len;
148*c2c66affSColin Finck {
149*c2c66affSColin Finck int hashno;
150*c2c66affSColin Finck struct hash_bucket *bp;
151*c2c66affSColin Finck
152*c2c66affSColin Finck if (!table)
153*c2c66affSColin Finck return (unsigned char *)0;
154*c2c66affSColin Finck
155*c2c66affSColin Finck if (!len)
156*c2c66affSColin Finck len = strlen ((char *)name);
157*c2c66affSColin Finck
158*c2c66affSColin Finck hashno = do_hash (name, len, table -> hash_count);
159*c2c66affSColin Finck
160*c2c66affSColin Finck for (bp = table -> buckets [hashno]; bp; bp = bp -> next) {
161*c2c66affSColin Finck if (len == bp -> len && !memcmp (bp -> name, name, len))
162*c2c66affSColin Finck return bp -> value;
163*c2c66affSColin Finck }
164*c2c66affSColin Finck return (unsigned char *)0;
165*c2c66affSColin Finck }
166