xref: /freebsd/libexec/revnetgroup/hash.c (revision d7b71c676e8144df99554c15e86e2e6575171913)
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  *
32d7b71c67SBill Paul  *	$Id: hash.c,v 1.1.1.1 1995/10/26 16:25:29 wpaul Exp $
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 
41d7b71c67SBill Paul #ifndef lint
42d7b71c67SBill Paul static const char rcsid[] = "$Id$";
43d7b71c67SBill Paul #endif
44d7b71c67SBill Paul 
45a44e4d14SBill Paul /*
46a44e4d14SBill Paul  * This hash function is stolen directly from the
47a44e4d14SBill Paul  * Berkeley DB package. It already exists inside libc, but
48a44e4d14SBill Paul  * it's declared static which prevents us from calling it
49a44e4d14SBill Paul  * from here.
50a44e4d14SBill Paul  */
51a44e4d14SBill Paul /*
52a44e4d14SBill Paul  * OZ's original sdbm hash
53a44e4d14SBill Paul  */
54a44e4d14SBill Paul u_int32_t
55a44e4d14SBill Paul hash(keyarg, len)
56a44e4d14SBill Paul 	const void *keyarg;
57a44e4d14SBill Paul 	register size_t len;
58a44e4d14SBill Paul {
59a44e4d14SBill Paul 	register const u_char *key;
60a44e4d14SBill Paul 	register size_t loop;
61a44e4d14SBill Paul 	register u_int32_t h;
62a44e4d14SBill Paul 
63a44e4d14SBill Paul #define HASHC   h = *key++ + 65599 * h
64a44e4d14SBill Paul 
65a44e4d14SBill Paul 	h = 0;
66a44e4d14SBill Paul 	key = keyarg;
67a44e4d14SBill Paul 	if (len > 0) {
68a44e4d14SBill Paul 		loop = (len + 8 - 1) >> 3;
69a44e4d14SBill Paul 
70a44e4d14SBill Paul 		switch (len & (8 - 1)) {
71a44e4d14SBill Paul 		case 0:
72a44e4d14SBill Paul 			do {
73a44e4d14SBill Paul 				HASHC;
74a44e4d14SBill Paul 				/* FALLTHROUGH */
75a44e4d14SBill Paul 		case 7:
76a44e4d14SBill Paul 				HASHC;
77a44e4d14SBill Paul 				/* FALLTHROUGH */
78a44e4d14SBill Paul 		case 6:
79a44e4d14SBill Paul 				HASHC;
80a44e4d14SBill Paul 				/* FALLTHROUGH */
81a44e4d14SBill Paul 		case 5:
82a44e4d14SBill Paul 				HASHC;
83a44e4d14SBill Paul 				/* FALLTHROUGH */
84a44e4d14SBill Paul 		case 4:
85a44e4d14SBill Paul 				HASHC;
86a44e4d14SBill Paul 				/* FALLTHROUGH */
87a44e4d14SBill Paul 		case 3:
88a44e4d14SBill Paul 				HASHC;
89a44e4d14SBill Paul 				/* FALLTHROUGH */
90a44e4d14SBill Paul 		case 2:
91a44e4d14SBill Paul 				HASHC;
92a44e4d14SBill Paul 				/* FALLTHROUGH */
93a44e4d14SBill Paul 		case 1:
94a44e4d14SBill Paul 				HASHC;
95a44e4d14SBill Paul 			} while (--loop);
96a44e4d14SBill Paul 		}
97a44e4d14SBill Paul 	}
98a44e4d14SBill Paul 	return (h);
99a44e4d14SBill Paul }
100a44e4d14SBill Paul 
101a44e4d14SBill Paul /*
102a44e4d14SBill Paul  * Generate a hash value for a given key (character string).
103a44e4d14SBill Paul  * We mask off all but the lower 8 bits since our table array
104d7b71c67SBill Paul  * can only hold 256 elements.
105a44e4d14SBill Paul  */
106a44e4d14SBill Paul u_int32_t hashkey(key)
107a44e4d14SBill Paul 	char *key;
108a44e4d14SBill Paul {
109a44e4d14SBill Paul 
110a44e4d14SBill Paul 	if (key == NULL)
111a44e4d14SBill Paul 		return (-1);
112a44e4d14SBill Paul 	return(hash((void *)key, strlen(key)) & HASH_MASK);
113a44e4d14SBill Paul }
114a44e4d14SBill Paul 
115a44e4d14SBill Paul /* Find an entry in the hash table (may be hanging off a linked list). */
116a44e4d14SBill Paul char *lookup(table, key)
117a44e4d14SBill Paul 	struct group_entry *table[];
118a44e4d14SBill Paul 	char *key;
119a44e4d14SBill Paul {
120a44e4d14SBill Paul 	struct group_entry *cur;
121a44e4d14SBill Paul 
122a44e4d14SBill Paul 	cur = table[hashkey(key)];
123a44e4d14SBill Paul 
124a44e4d14SBill Paul 	while (cur) {
125a44e4d14SBill Paul 		if (!strcmp(cur->key, key))
126a44e4d14SBill Paul 			return(cur->data);
127a44e4d14SBill Paul 		cur = cur->next;
128a44e4d14SBill Paul 	}
129a44e4d14SBill Paul 
130a44e4d14SBill Paul 	return(NULL);
131a44e4d14SBill Paul }
132a44e4d14SBill Paul 
133a44e4d14SBill Paul /*
134a44e4d14SBill Paul  * Store an entry in the main netgroup hash table. Here's how this
135a44e4d14SBill Paul  * works: the table can only be so big when we initialize it (TABLESIZE)
136a44e4d14SBill Paul  * but the number of netgroups in the /etc/netgroup file could easily be
137a44e4d14SBill Paul  * much larger than the table. Since our hash values are adjusted to
138a44e4d14SBill Paul  * never be greater than TABLESIZE too, this means it won't be long before
139a44e4d14SBill Paul  * we find ourselves with two keys that hash to the same value.
140a44e4d14SBill Paul  *
141a44e4d14SBill Paul  * One way to deal with this is to malloc(2) a second table and start
142a44e4d14SBill Paul  * doing indirection, but this is a pain in the butt and it's not worth
143d7b71c67SBill Paul  * going to all that trouble for a dinky little program like this. Instead,
144a44e4d14SBill Paul  * we turn each table entry into a linked list and simply link keys
145a44e4d14SBill Paul  * with the same hash value together at the same index location within
146a44e4d14SBill Paul  * the table.
147a44e4d14SBill Paul  *
148a44e4d14SBill Paul  * That's a lot of comment for such a small piece of code, isn't it.
149a44e4d14SBill Paul  */
150a44e4d14SBill Paul void store (table, key, data)
151a44e4d14SBill Paul 	struct group_entry *table[];
152a44e4d14SBill Paul 	char *key, *data;
153a44e4d14SBill Paul {
154a44e4d14SBill Paul 	struct group_entry *new;
155a44e4d14SBill Paul 	u_int32_t i;
156a44e4d14SBill Paul 
157a44e4d14SBill Paul 	i = hashkey(key);
158a44e4d14SBill Paul 
159a44e4d14SBill Paul 	new = (struct group_entry *)malloc(sizeof(struct group_entry));
160a44e4d14SBill Paul 	new->key = strdup(key);
161a44e4d14SBill Paul 	new->data = strdup(data);
162a44e4d14SBill Paul 	new->next = table[i];
163a44e4d14SBill Paul 	table[i] = new;
164a44e4d14SBill Paul 
165a44e4d14SBill Paul 	return;
166a44e4d14SBill Paul }
167a44e4d14SBill Paul 
168a44e4d14SBill Paul /*
169d7b71c67SBill Paul  * Store a group member entry and/or update its grouplist. This is
170a44e4d14SBill Paul  * a bit more complicated than the previous function since we have to
171a44e4d14SBill Paul  * maintain not only the hash table of group members, each group member
172a44e4d14SBill Paul  * structure also has a linked list of groups hung off it. If handed
173a44e4d14SBill Paul  * a member name that we haven't encountered before, we have to do
174a44e4d14SBill Paul  * two things: add that member to the table (possibly hanging them
175a44e4d14SBill Paul  * off the end of a linked list, as above), and add a group name to
176a44e4d14SBill Paul  * the member's grouplist list. If we're handed a name that already has
177a44e4d14SBill Paul  * an entry in the table, then we just have to do one thing, which is
178a44e4d14SBill Paul  * to update its grouplist.
179a44e4d14SBill Paul  */
180a44e4d14SBill Paul void mstore (table, key, data, domain)
181a44e4d14SBill Paul 	struct member_entry *table[];
182a44e4d14SBill Paul 	char *key, *data, *domain;
183a44e4d14SBill Paul {
184a44e4d14SBill Paul 	struct member_entry *cur, *new;
185a44e4d14SBill Paul 	struct grouplist *tmp;
186a44e4d14SBill Paul 	u_int32_t i;
187a44e4d14SBill Paul 
188a44e4d14SBill Paul 	i = hashkey(key);
189a44e4d14SBill Paul 	cur = table[i];
190a44e4d14SBill Paul 
191a44e4d14SBill Paul 	tmp = (struct grouplist *)malloc(sizeof(struct grouplist));
192a44e4d14SBill Paul 	tmp->groupname = strdup(data);
193a44e4d14SBill Paul 	tmp->next = NULL;
194a44e4d14SBill Paul 
195a44e4d14SBill Paul 	/* Check if all we have to do is insert a new groupname. */
196a44e4d14SBill Paul 	while (cur) {
197a44e4d14SBill Paul 		if (!strcmp(cur->key, key)) {
198a44e4d14SBill Paul 			tmp->next = cur->groups;
199a44e4d14SBill Paul 			cur->groups = tmp;
200a44e4d14SBill Paul 			return;
201a44e4d14SBill Paul 		}
202a44e4d14SBill Paul 		cur = cur->next;
203a44e4d14SBill Paul 	}
204a44e4d14SBill Paul 
205a44e4d14SBill Paul 	/* Didn't find a match -- add the whole mess to the table. */
206a44e4d14SBill Paul 	new = (struct member_entry *)malloc(sizeof(struct member_entry));
207a44e4d14SBill Paul 	new->key = strdup(key);
208a44e4d14SBill Paul 	new->domain = domain ? strdup(domain) : "*";
209a44e4d14SBill Paul 	new->groups = tmp;
210a44e4d14SBill Paul 	new->next = table[i];
211a44e4d14SBill Paul 	table[i] = new;
212a44e4d14SBill Paul 
213a44e4d14SBill Paul 	return;
214a44e4d14SBill Paul }
215