xref: /freebsd/libexec/revnetgroup/hash.c (revision a44e4d140757134340807cb35dac2213cfc54b6c)
1a44e4d14SBill Paul /*
2a44e4d14SBill Paul  * Copyright (c) 1995
3a44e4d14SBill Paul  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4a44e4d14SBill Paul  *
5a44e4d14SBill Paul  * Redistribution and use in source and binary forms, with or without
6a44e4d14SBill Paul  * modification, are permitted provided that the following conditions
7a44e4d14SBill Paul  * are met:
8a44e4d14SBill Paul  * 1. Redistributions of source code must retain the above copyright
9a44e4d14SBill Paul  *    notice, this list of conditions and the following disclaimer.
10a44e4d14SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
11a44e4d14SBill Paul  *    notice, this list of conditions and the following disclaimer in the
12a44e4d14SBill Paul  *    documentation and/or other materials provided with the distribution.
13a44e4d14SBill Paul  * 3. All advertising materials mentioning features or use of this software
14a44e4d14SBill Paul  *    must display the following acknowledgement:
15a44e4d14SBill Paul  *	This product includes software developed by Bill Paul.
16a44e4d14SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
17a44e4d14SBill Paul  *    may be used to endorse or promote products derived from this software
18a44e4d14SBill Paul  *    without specific prior written permission.
19a44e4d14SBill Paul  *
20a44e4d14SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21a44e4d14SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22a44e4d14SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23a44e4d14SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24a44e4d14SBill Paul  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25a44e4d14SBill Paul  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26a44e4d14SBill Paul  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27a44e4d14SBill Paul  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28a44e4d14SBill Paul  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29a44e4d14SBill Paul  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30a44e4d14SBill Paul  * SUCH DAMAGE.
31a44e4d14SBill Paul  *
32a44e4d14SBill Paul  *	$Id$
33a44e4d14SBill Paul  */
34a44e4d14SBill Paul 
35a44e4d14SBill Paul #include <stdio.h>
36a44e4d14SBill Paul #include <stdlib.h>
37a44e4d14SBill Paul #include <string.h>
38a44e4d14SBill Paul #include <sys/types.h>
39a44e4d14SBill Paul #include "hash.h"
40a44e4d14SBill Paul 
41a44e4d14SBill Paul /*
42a44e4d14SBill Paul  * This hash function is stolen directly from the
43a44e4d14SBill Paul  * Berkeley DB package. It already exists inside libc, but
44a44e4d14SBill Paul  * it's declared static which prevents us from calling it
45a44e4d14SBill Paul  * from here.
46a44e4d14SBill Paul  */
47a44e4d14SBill Paul /*
48a44e4d14SBill Paul  * OZ's original sdbm hash
49a44e4d14SBill Paul  */
50a44e4d14SBill Paul u_int32_t
51a44e4d14SBill Paul hash(keyarg, len)
52a44e4d14SBill Paul 	const void *keyarg;
53a44e4d14SBill Paul 	register size_t len;
54a44e4d14SBill Paul {
55a44e4d14SBill Paul 	register const u_char *key;
56a44e4d14SBill Paul 	register size_t loop;
57a44e4d14SBill Paul 	register u_int32_t h;
58a44e4d14SBill Paul 
59a44e4d14SBill Paul #define HASHC   h = *key++ + 65599 * h
60a44e4d14SBill Paul 
61a44e4d14SBill Paul 	h = 0;
62a44e4d14SBill Paul 	key = keyarg;
63a44e4d14SBill Paul 	if (len > 0) {
64a44e4d14SBill Paul 		loop = (len + 8 - 1) >> 3;
65a44e4d14SBill Paul 
66a44e4d14SBill Paul 		switch (len & (8 - 1)) {
67a44e4d14SBill Paul 		case 0:
68a44e4d14SBill Paul 			do {
69a44e4d14SBill Paul 				HASHC;
70a44e4d14SBill Paul 				/* FALLTHROUGH */
71a44e4d14SBill Paul 		case 7:
72a44e4d14SBill Paul 				HASHC;
73a44e4d14SBill Paul 				/* FALLTHROUGH */
74a44e4d14SBill Paul 		case 6:
75a44e4d14SBill Paul 				HASHC;
76a44e4d14SBill Paul 				/* FALLTHROUGH */
77a44e4d14SBill Paul 		case 5:
78a44e4d14SBill Paul 				HASHC;
79a44e4d14SBill Paul 				/* FALLTHROUGH */
80a44e4d14SBill Paul 		case 4:
81a44e4d14SBill Paul 				HASHC;
82a44e4d14SBill Paul 				/* FALLTHROUGH */
83a44e4d14SBill Paul 		case 3:
84a44e4d14SBill Paul 				HASHC;
85a44e4d14SBill Paul 				/* FALLTHROUGH */
86a44e4d14SBill Paul 		case 2:
87a44e4d14SBill Paul 				HASHC;
88a44e4d14SBill Paul 				/* FALLTHROUGH */
89a44e4d14SBill Paul 		case 1:
90a44e4d14SBill Paul 				HASHC;
91a44e4d14SBill Paul 			} while (--loop);
92a44e4d14SBill Paul 		}
93a44e4d14SBill Paul 	}
94a44e4d14SBill Paul 	return (h);
95a44e4d14SBill Paul }
96a44e4d14SBill Paul 
97a44e4d14SBill Paul /*
98a44e4d14SBill Paul  * Generate a hash value for a given key (character string).
99a44e4d14SBill Paul  * We mask off all but the lower 8 bits since our table array
100a44e4d14SBill Paul  * can only hole 256 elements.
101a44e4d14SBill Paul  */
102a44e4d14SBill Paul u_int32_t hashkey(key)
103a44e4d14SBill Paul 	char *key;
104a44e4d14SBill Paul {
105a44e4d14SBill Paul 
106a44e4d14SBill Paul 	if (key == NULL)
107a44e4d14SBill Paul 		return (-1);
108a44e4d14SBill Paul 	return(hash((void *)key, strlen(key)) & HASH_MASK);
109a44e4d14SBill Paul }
110a44e4d14SBill Paul 
111a44e4d14SBill Paul /* Find an entry in the hash table (may be hanging off a linked list). */
112a44e4d14SBill Paul char *lookup(table, key)
113a44e4d14SBill Paul 	struct group_entry *table[];
114a44e4d14SBill Paul 	char *key;
115a44e4d14SBill Paul {
116a44e4d14SBill Paul 	struct group_entry *cur;
117a44e4d14SBill Paul 
118a44e4d14SBill Paul 	cur = table[hashkey(key)];
119a44e4d14SBill Paul 
120a44e4d14SBill Paul 	while (cur) {
121a44e4d14SBill Paul 		if (!strcmp(cur->key, key))
122a44e4d14SBill Paul 			return(cur->data);
123a44e4d14SBill Paul 		cur = cur->next;
124a44e4d14SBill Paul 	}
125a44e4d14SBill Paul 
126a44e4d14SBill Paul 	return(NULL);
127a44e4d14SBill Paul }
128a44e4d14SBill Paul 
129a44e4d14SBill Paul /*
130a44e4d14SBill Paul  * Store an entry in the main netgroup hash table. Here's how this
131a44e4d14SBill Paul  * works: the table can only be so big when we initialize it (TABLESIZE)
132a44e4d14SBill Paul  * but the number of netgroups in the /etc/netgroup file could easily be
133a44e4d14SBill Paul  * much larger than the table. Since our hash values are adjusted to
134a44e4d14SBill Paul  * never be greater than TABLESIZE too, this means it won't be long before
135a44e4d14SBill Paul  * we find ourselves with two keys that hash to the same value.
136a44e4d14SBill Paul  *
137a44e4d14SBill Paul  * One way to deal with this is to malloc(2) a second table and start
138a44e4d14SBill Paul  * doing indirection, but this is a pain in the butt and it's not worth
139a44e4d14SBill Paul  * going to all that trouble for a dinky littke program like this. Instead,
140a44e4d14SBill Paul  * we turn each table entry into a linked list and simply link keys
141a44e4d14SBill Paul  * with the same hash value together at the same index location within
142a44e4d14SBill Paul  * the table.
143a44e4d14SBill Paul  *
144a44e4d14SBill Paul  * That's a lot of comment for such a small piece of code, isn't it.
145a44e4d14SBill Paul  */
146a44e4d14SBill Paul void store (table, key, data)
147a44e4d14SBill Paul 	struct group_entry *table[];
148a44e4d14SBill Paul 	char *key, *data;
149a44e4d14SBill Paul {
150a44e4d14SBill Paul 	struct group_entry *new;
151a44e4d14SBill Paul 	u_int32_t i;
152a44e4d14SBill Paul 
153a44e4d14SBill Paul 	i = hashkey(key);
154a44e4d14SBill Paul 
155a44e4d14SBill Paul 	new = (struct group_entry *)malloc(sizeof(struct group_entry));
156a44e4d14SBill Paul 	new->key = strdup(key);
157a44e4d14SBill Paul 	new->data = strdup(data);
158a44e4d14SBill Paul 	new->next = table[i];
159a44e4d14SBill Paul 	table[i] = new;
160a44e4d14SBill Paul 
161a44e4d14SBill Paul 	return;
162a44e4d14SBill Paul }
163a44e4d14SBill Paul 
164a44e4d14SBill Paul /*
165a44e4d14SBill Paul  * Store an group member entry and/or update its grouplist. This is
166a44e4d14SBill Paul  * a bit more complicated than the previous function since we have to
167a44e4d14SBill Paul  * maintain not only the hash table of group members, each group member
168a44e4d14SBill Paul  * structure also has a linked list of groups hung off it. If handed
169a44e4d14SBill Paul  * a member name that we haven't encountered before, we have to do
170a44e4d14SBill Paul  * two things: add that member to the table (possibly hanging them
171a44e4d14SBill Paul  * off the end of a linked list, as above), and add a group name to
172a44e4d14SBill Paul  * the member's grouplist list. If we're handed a name that already has
173a44e4d14SBill Paul  * an entry in the table, then we just have to do one thing, which is
174a44e4d14SBill Paul  * to update its grouplist.
175a44e4d14SBill Paul  */
176a44e4d14SBill Paul void mstore (table, key, data, domain)
177a44e4d14SBill Paul 	struct member_entry *table[];
178a44e4d14SBill Paul 	char *key, *data, *domain;
179a44e4d14SBill Paul {
180a44e4d14SBill Paul 	struct member_entry *cur, *new;
181a44e4d14SBill Paul 	struct grouplist *tmp;
182a44e4d14SBill Paul 	u_int32_t i;
183a44e4d14SBill Paul 
184a44e4d14SBill Paul 	i = hashkey(key);
185a44e4d14SBill Paul 	cur = table[i];
186a44e4d14SBill Paul 
187a44e4d14SBill Paul 	tmp = (struct grouplist *)malloc(sizeof(struct grouplist));
188a44e4d14SBill Paul 	tmp->groupname = strdup(data);
189a44e4d14SBill Paul 	tmp->next = NULL;
190a44e4d14SBill Paul 
191a44e4d14SBill Paul 	/* Check if all we have to do is insert a new groupname. */
192a44e4d14SBill Paul 	while (cur) {
193a44e4d14SBill Paul 		if (!strcmp(cur->key, key)) {
194a44e4d14SBill Paul 			tmp->next = cur->groups;
195a44e4d14SBill Paul 			cur->groups = tmp;
196a44e4d14SBill Paul 			return;
197a44e4d14SBill Paul 		}
198a44e4d14SBill Paul 		cur = cur->next;
199a44e4d14SBill Paul 	}
200a44e4d14SBill Paul 
201a44e4d14SBill Paul 	/* Didn't find a match -- add the whole mess to the table. */
202a44e4d14SBill Paul 	new = (struct member_entry *)malloc(sizeof(struct member_entry));
203a44e4d14SBill Paul 	new->key = strdup(key);
204a44e4d14SBill Paul 	new->domain = domain ? strdup(domain) : "*";
205a44e4d14SBill Paul 	new->groups = tmp;
206a44e4d14SBill Paul 	new->next = table[i];
207a44e4d14SBill Paul 	table[i] = new;
208a44e4d14SBill Paul 
209a44e4d14SBill Paul 	return;
210a44e4d14SBill Paul }
211