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