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