-
Notifications
You must be signed in to change notification settings - Fork 7
/
hash.c
411 lines (344 loc) · 9.32 KB
/
hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* Boa, an http server
* Copyright (C) 1995 Paul Phillips <[email protected]>
* Some changes Copyright (C) 1996 Larry Doolittle <[email protected]>
* Some changes Copyright (C) 1997 Jon Nelson <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/* $Id: hash.c,v 1.14.4.4 2002/07/30 03:59:26 jnelson Exp $*/
#include "boa.h"
#include "parse.h"
/*
* There are two hash tables used, each with a key/value pair
* stored in a hash_struct. They are:
*
* mime_hashtable:
* key = file extension
* value = mime type
*
* passwd_hashtable:
* key = username
* value = home directory
*
*/
struct _hash_struct_ {
char *key;
char *value;
struct _hash_struct_ *next;
};
typedef struct _hash_struct_ hash_struct;
static hash_struct *mime_hashtable[MIME_HASHTABLE_SIZE];
static hash_struct *passwd_hashtable[PASSWD_HASHTABLE_SIZE];
#ifdef WANT_ICKY_HASH
static unsigned four_char_hash(char *buf);
#define boa_hash four_char_hash
#else
#ifdef WANT_SDBM_HASH
static unsigned sdbm_hash(char *str);
#define boa_hash sdbm_hash
#else
static unsigned djb2_hash(char *str);
#define boa_hash djb2_hash
#endif
#endif
#ifdef WANT_ICKY_HASH
static unsigned four_char_hash(char *buf)
{
unsigned int hash = (buf[0] +
(buf[1] ? buf[1] : 241 +
(buf[2] ? buf[2] : 251 +
(buf[3] ? buf[3] : 257))));
#ifdef DEBUG_HASH
log_error_time();
fprintf(stderr, "four_char_hash(%s) = %u\n", buf, hash);
#endif
return hash;
}
/* The next two hashes taken from
* http://www.cs.yorku.ca/~oz/hash.html
*
* In my (admittedly) very brief testing, djb2_hash performed
* very slightly better than sdbm_hash.
*/
#else
#define MAX_HASH_LENGTH 4
#ifdef WANT_SDBM_HASH
static unsigned sdbm_hash(char *str)
{
unsigned hash = 0;
int c;
short count = MAX_HASH_LENGTH;
#ifdef DEBUG_HASH
log_error_time();
fprintf(stderr, "sdbm_hash(%s) = ", str);
#endif
while ((c = *str++) && count--)
hash = c + (hash << 6) + (hash << 16) - hash;
#ifdef DEBUG_HASH
fprintf(stderr, "%u\n", hash);
#endif
return hash;
}
#else
static unsigned djb2_hash(char *str)
{
unsigned hash = 5381;
int c;
short count = MAX_HASH_LENGTH;
#ifdef DEBUG_HASH
log_error_time();
fprintf(stderr, "djb2_hash(%s) = ", str);
#endif
while ((c = *(str++)) && count--)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
#ifdef DEBUG_HASH
fprintf(stderr, "%u\n", hash);
#endif
return hash;
}
#endif
#endif
/*
* Name: add_mime_type
* Description: Adds a key/value pair to the mime_hashtable
*/
void add_mime_type(char *extension, char *type)
{
unsigned int hash;
hash_struct *current, *next;
if (!extension)
return;
hash = get_mime_hash_value(extension);
current = mime_hashtable[hash];
while (current) {
if (!strcmp(current->key, extension))
return; /* don't add extension twice */
if (current->next)
current = current->next;
else
break;
}
/* if here, we need to add a new one */
next = (hash_struct *) malloc(sizeof (hash_struct));
if (!next) {
DIE("malloc of hash_struct failed!");
}
next->key = strdup(extension);
if (!next->key)
DIE("malloc of hash_struct->key failed!");
next->value = strdup(type);
if (!next->value)
DIE("malloc of hash_struct->value failed!");
next->next = NULL;
if (!current) {
mime_hashtable[hash] = next;
} else {
current->next = next;
}
}
/*
* Name: get_mime_hash_value
*
* Description: adds the ASCII values of the file extension letters
* and mods by the hashtable size to get the hash value
*/
unsigned get_mime_hash_value(char *extension)
{
unsigned int hash = 0;
if (extension == NULL || extension[0] == '\0') {
/* FIXME */
log_error_time();
fprintf(stderr, "Attempt to hash NULL or empty string!\n");
return 0;
}
hash = boa_hash(extension);
hash %= MIME_HASHTABLE_SIZE;
return hash;
}
/*
* Name: get_mime_type
*
* Description: Returns the mime type for a supplied filename.
* Returns default type if not found.
*/
char *get_mime_type(char *filename)
{
char *extension;
hash_struct *current;
unsigned int hash;
extension = strrchr(filename, '.');
if (!extension || *extension++ == '\0')
return default_type;
hash = get_mime_hash_value(extension);
current = mime_hashtable[hash];
while (current) {
if (!strcmp(current->key, extension)) /* hit */
return current->value;
current = current->next;
}
return default_type;
}
/*
* Name: get_homedir_hash_value
*
* Description: adds the ASCII values of the username letters
* and mods by the hashtable size to get the hash value
*/
unsigned get_homedir_hash_value(char *name)
{
unsigned int hash = 0;
if (name == NULL || name[0] == '\0') {
/* FIXME */
log_error_time();
fprintf(stderr, "Attempt to hash NULL or empty string!\n");
return 0;
}
hash = boa_hash(name);
hash %= PASSWD_HASHTABLE_SIZE;
return hash;
}
/*
* Name: get_home_dir
*
* Description: Returns a point to the supplied user's home directory.
* Adds to the hashtable if it's not already present.
*
*/
char *get_home_dir(char *name)
{
struct passwd *passwdbuf;
hash_struct *current, *next;
unsigned int hash;
/* first check hash table -- if username is less than four characters,
just hash to zero (this should be very rare) */
hash = get_homedir_hash_value(name);
for(current = passwd_hashtable[hash];current;current = current->next) {
if (!strcmp(current->key, name)) /* hit */
return current->value;
if (!current->next)
break;
}
/* if here, we have to add a new one */
passwdbuf = getpwnam(name);
if (!passwdbuf) /* does not exist */
return NULL;
next = (hash_struct *) malloc(sizeof (hash_struct));
if (!next) {
WARN("malloc of hash_struct for passwd_hashtable failed!");
return NULL;
}
next->key = strdup(name);
if (!next->key) {
WARN("malloc of passwd_hashtable[hash]->key failed!");
free(next);
return NULL;
}
next->value = strdup(passwdbuf->pw_dir);
if (!next->value) {
WARN("malloc of passwd_hashtable[hash]->value failed!");
free(next->key);
free(next);
return NULL;
}
next->next = NULL;
if (!current) {
passwd_hashtable[hash] = next;
} else {
current->next = next;
}
return next->value;
}
void dump_mime(void)
{
int i;
hash_struct *temp;
for (i = 0; i < MIME_HASHTABLE_SIZE; ++i) { /* these limits OK? */
temp = mime_hashtable[i];
while (temp) {
hash_struct *temp_next;
temp_next = temp->next;
free(temp->key);
free(temp->value);
free(temp);
temp = temp_next;
}
mime_hashtable[i] = NULL;
}
}
void dump_passwd(void)
{
int i;
hash_struct *temp;
for (i = 0; i < PASSWD_HASHTABLE_SIZE; ++i) { /* these limits OK? */
temp = passwd_hashtable[i];
while (temp) {
hash_struct *temp_next;
temp_next = temp->next;
free(temp->key);
free(temp->value);
free(temp);
temp = temp_next;
}
passwd_hashtable[i] = NULL;
}
}
void show_hash_stats(void)
{
int i;
hash_struct *temp;
int total = 0;
int count;
for (i = 0; i < MIME_HASHTABLE_SIZE; ++i) { /* these limits OK? */
if (mime_hashtable[i]) {
count = 0;
temp = mime_hashtable[i];
while (temp) {
temp = temp->next;
++count;
}
#ifdef NOISY_SIGALRM
log_error_time();
fprintf(stderr, "mime_hashtable[%d] has %d entries\n",
i, count);
#endif
total += count;
}
}
log_error_time();
fprintf(stderr, "mime_hashtable has %d total entries\n",
total);
total = 0;
for (i = 0; i < PASSWD_HASHTABLE_SIZE; ++i) { /* these limits OK? */
if (passwd_hashtable[i]) {
temp = passwd_hashtable[i];
count = 0;
while (temp) {
temp = temp->next;
++count;
}
#ifdef NOISY_SIGALRM
log_error_time();
fprintf(stderr, "passwd_hashtable[%d] has %d entries\n",
i, count);
#endif
total += count;
}
}
log_error_time();
fprintf(stderr, "passwd_hashtable has %d total entries\n",
total);
}