xref: /freebsd/libexec/mknetid/hash.c (revision 71233f4f)
1ca09eb42SBill Paul /*
2ca09eb42SBill Paul  * Copyright (c) 1995
3ca09eb42SBill Paul  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4ca09eb42SBill Paul  *
5ca09eb42SBill Paul  * Redistribution and use in source and binary forms, with or without
6ca09eb42SBill Paul  * modification, are permitted provided that the following conditions
7ca09eb42SBill Paul  * are met:
8ca09eb42SBill Paul  * 1. Redistributions of source code must retain the above copyright
9ca09eb42SBill Paul  *    notice, this list of conditions and the following disclaimer.
10ca09eb42SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
11ca09eb42SBill Paul  *    notice, this list of conditions and the following disclaimer in the
12ca09eb42SBill Paul  *    documentation and/or other materials provided with the distribution.
13ca09eb42SBill Paul  * 3. All advertising materials mentioning features or use of this software
14ca09eb42SBill Paul  *    must display the following acknowledgement:
15ca09eb42SBill Paul  *	This product includes software developed by Bill Paul.
16ca09eb42SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
17ca09eb42SBill Paul  *    may be used to endorse or promote products derived from this software
18ca09eb42SBill Paul  *    without specific prior written permission.
19ca09eb42SBill Paul  *
20ca09eb42SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21ca09eb42SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22ca09eb42SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23ca09eb42SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24ca09eb42SBill Paul  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25ca09eb42SBill Paul  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26ca09eb42SBill Paul  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27ca09eb42SBill Paul  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28ca09eb42SBill Paul  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29ca09eb42SBill Paul  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30ca09eb42SBill Paul  * SUCH DAMAGE.
31ca09eb42SBill Paul  */
32ca09eb42SBill Paul 
33ca09eb42SBill Paul #include <stdio.h>
34ca09eb42SBill Paul #include <stdlib.h>
35ca09eb42SBill Paul #include <string.h>
36ca09eb42SBill Paul #include <sys/types.h>
37ca09eb42SBill Paul #include "hash.h"
38ca09eb42SBill Paul 
39ca09eb42SBill Paul #ifndef lint
40ad17ca10SPhilippe Charnier static const char rcsid[] =
417f3dea24SPeter Wemm   "$FreeBSD$";
42ad17ca10SPhilippe Charnier #endif /* not lint */
43ca09eb42SBill Paul 
44ca09eb42SBill Paul /*
45ca09eb42SBill Paul  * This hash function is stolen directly from the
46ca09eb42SBill Paul  * Berkeley DB package. It already exists inside libc, but
47ca09eb42SBill Paul  * it's declared static which prevents us from calling it
48ca09eb42SBill Paul  * from here.
49ca09eb42SBill Paul  */
50ca09eb42SBill Paul /*
51ca09eb42SBill Paul  * OZ's original sdbm hash
52ca09eb42SBill Paul  */
53ca09eb42SBill Paul u_int32_t
5471233f4fSWarner Losh hash(const void *keyarg, size_t len)
55ca09eb42SBill Paul {
5671233f4fSWarner Losh 	const u_char *key;
5771233f4fSWarner Losh 	size_t loop;
5871233f4fSWarner Losh 	u_int32_t h;
59ca09eb42SBill Paul 
60ca09eb42SBill Paul #define HASHC   h = *key++ + 65599 * h
61ca09eb42SBill Paul 
62ca09eb42SBill Paul 	h = 0;
63ca09eb42SBill Paul 	key = keyarg;
64ca09eb42SBill Paul 	if (len > 0) {
65ca09eb42SBill Paul 		loop = (len + 8 - 1) >> 3;
66ca09eb42SBill Paul 
67ca09eb42SBill Paul 		switch (len & (8 - 1)) {
68ca09eb42SBill Paul 		case 0:
69ca09eb42SBill Paul 			do {
70ca09eb42SBill Paul 				HASHC;
71ca09eb42SBill Paul 				/* FALLTHROUGH */
72ca09eb42SBill Paul 		case 7:
73ca09eb42SBill Paul 				HASHC;
74ca09eb42SBill Paul 				/* FALLTHROUGH */
75ca09eb42SBill Paul 		case 6:
76ca09eb42SBill Paul 				HASHC;
77ca09eb42SBill Paul 				/* FALLTHROUGH */
78ca09eb42SBill Paul 		case 5:
79ca09eb42SBill Paul 				HASHC;
80ca09eb42SBill Paul 				/* FALLTHROUGH */
81ca09eb42SBill Paul 		case 4:
82ca09eb42SBill Paul 				HASHC;
83ca09eb42SBill Paul 				/* FALLTHROUGH */
84ca09eb42SBill Paul 		case 3:
85ca09eb42SBill Paul 				HASHC;
86ca09eb42SBill Paul 				/* FALLTHROUGH */
87ca09eb42SBill Paul 		case 2:
88ca09eb42SBill Paul 				HASHC;
89ca09eb42SBill Paul 				/* FALLTHROUGH */
90ca09eb42SBill Paul 		case 1:
91ca09eb42SBill Paul 				HASHC;
92ca09eb42SBill Paul 			} while (--loop);
93ca09eb42SBill Paul 		}
94ca09eb42SBill Paul 	}
95ca09eb42SBill Paul 	return (h);
96ca09eb42SBill Paul }
97ca09eb42SBill Paul 
98ca09eb42SBill Paul /*
99ca09eb42SBill Paul  * Generate a hash value for a given key (character string).
100ca09eb42SBill Paul  * We mask off all but the lower 8 bits since our table array
101ca09eb42SBill Paul  * can only hole 256 elements.
102ca09eb42SBill Paul  */
10371233f4fSWarner Losh u_int32_t hashkey(char *key)
104ca09eb42SBill Paul {
105ca09eb42SBill Paul 
106ca09eb42SBill Paul 	if (key == NULL)
107ca09eb42SBill Paul 		return (-1);
108ca09eb42SBill Paul 	return(hash((void *)key, strlen(key)) & HASH_MASK);
109ca09eb42SBill Paul }
110ca09eb42SBill Paul 
111ca09eb42SBill Paul /* Find an entry in the hash table (may be hanging off a linked list). */
11271233f4fSWarner Losh struct grouplist *lookup(struct member_entry *table[], char *key)
113ca09eb42SBill Paul {
114ca09eb42SBill Paul 	struct member_entry *cur;
115ca09eb42SBill Paul 
116ca09eb42SBill Paul 	cur = table[hashkey(key)];
117ca09eb42SBill Paul 
118ca09eb42SBill Paul 	while (cur) {
119ca09eb42SBill Paul 		if (!strcmp(cur->key, key))
120ca09eb42SBill Paul 			return(cur->groups);
121ca09eb42SBill Paul 		cur = cur->next;
122ca09eb42SBill Paul 	}
123ca09eb42SBill Paul 
124ca09eb42SBill Paul 	return(NULL);
125ca09eb42SBill Paul }
126ca09eb42SBill Paul 
127ca09eb42SBill Paul struct grouplist dummy = { 99999, NULL };
128ca09eb42SBill Paul 
129ca09eb42SBill Paul /*
130ca09eb42SBill Paul  * Store an group member entry and/or update its grouplist.
131ca09eb42SBill Paul  */
13271233f4fSWarner Losh void mstore (struct member_entry *table[], char *key, int gid, int dup)
133ca09eb42SBill Paul {
134ca09eb42SBill Paul 	struct member_entry *cur, *new;
135ca09eb42SBill Paul 	struct grouplist *tmp;
136ca09eb42SBill Paul 	u_int32_t i;
137ca09eb42SBill Paul 
138ca09eb42SBill Paul 	i = hashkey(key);
139ca09eb42SBill Paul 	cur = table[i];
140ca09eb42SBill Paul 
141ca09eb42SBill Paul 	if (!dup) {
142ca09eb42SBill Paul 		tmp = (struct grouplist *)malloc(sizeof(struct grouplist));
143ca09eb42SBill Paul 		tmp->groupid = gid;
144ca09eb42SBill Paul 		tmp->next = NULL;
145ca09eb42SBill Paul 	}
146ca09eb42SBill Paul 
147ca09eb42SBill Paul 		/* Check if all we have to do is insert a new groupname. */
148ca09eb42SBill Paul 	while (cur) {
149ca09eb42SBill Paul 		if (!dup && !strcmp(cur->key, key)) {
150ca09eb42SBill Paul 			tmp->next = cur->groups;
151ca09eb42SBill Paul 			cur->groups = tmp;
152ca09eb42SBill Paul 			return;
153ca09eb42SBill Paul 		}
154ca09eb42SBill Paul 		cur = cur->next;
155ca09eb42SBill Paul 	}
156ca09eb42SBill Paul 
157ca09eb42SBill Paul 	/* Didn't find a match -- add the whole mess to the table. */
158ca09eb42SBill Paul 	new = (struct member_entry *)malloc(sizeof(struct member_entry));
159ca09eb42SBill Paul 	new->key = strdup(key);
160ca09eb42SBill Paul 	if (!dup)
161ca09eb42SBill Paul 		new->groups = tmp;
162ca09eb42SBill Paul 	else
163ca09eb42SBill Paul 		new->groups = (struct grouplist *)&dummy;
164ca09eb42SBill Paul 	new->next = table[i];
165ca09eb42SBill Paul 	table[i] = new;
166ca09eb42SBill Paul 
167ca09eb42SBill Paul 	return;
168ca09eb42SBill Paul }
169