summaryrefslogtreecommitdiff
path: root/polux/application/boa/src/hash.c
blob: dc5be8ab66f8cfee40696eda2dbe84d4c0740c7b (plain)
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
 *  Boa, an http server
 *  Copyright (C) 1995 Paul Phillips <paulp@go2net.com>
 *  Some changes Copyright (C) 1996 Larry Doolittle <ldoolitt@boa.org>
 *  Some changes Copyright (C) 1997 Jon Nelson <jnelson@boa.org>
 *
 *  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.1.1.1 2006/06/16 09:48:03 fleury Exp $*/

#include "boa.h"

#ifndef MAX_HASH_LENGTH
#define MAX_HASH_LENGTH 4
#endif

/*
 * 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];
void add_mime_type(const char *extension, const char *type);
static unsigned get_homedir_hash_value(const char *name);

#ifdef WANT_ICKY_HASH
static unsigned four_char_hash(const char *buf);
#define _boa_hash four_char_hash
#else
#ifdef WANT_SDBM_HASH
static unsigned sdbm_hash(const char *str);
#define _boa_hash sdbm_hash
#else
#ifdef WANT_DJB2_HASH
static unsigned djb2_hash(const char *str);
#define _boa_hash djb2_hash
#else
static unsigned fnv1a_hash(const char *str);
#define _boa_hash fnv1a_hash
#endif
#endif
#endif

static unsigned boa_hash(const char *str)
{
    if (str == NULL || str[0] == '\0') {
        log_error_time();
        fprintf(stderr,
                "Attempt to hash NULL or empty string! [boa_hash]!\n");
        return 0;
    }
    return _boa_hash(str);
}

#ifdef WANT_ICKY_HASH
static unsigned four_char_hash(const char *buf)
{
    unsigned int hash = (buf[0] +
                         (buf[1] ? buf[1] : 241 +
                          (buf[2] ? buf[2] : 251 +
                           (buf[3] ? buf[3] : 257))));
    DEBUG(DEBUG_HASH) {
        log_error_time();
        fprintf(stderr, "four_char_hash(%s) = %u\n", buf, hash);
    }
    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
#ifdef WANT_SDBM_HASH
static unsigned sdbm_hash(const char *str)
{
    unsigned hash = 0;
    int c;
    short count = MAX_HASH_LENGTH;

    DEBUG(DEBUG_HASH) {
        log_error_time();
        fprintf(stderr, "sdbm_hash(%s) = ", str);
    }

    while ((c = *str++) && count--)
        hash = c + (hash << 6) + (hash << 16) - hash;

    DEBUG(DEBUG_HASH) {
        fprintf(stderr, "%u\n", hash);
    }
    return hash;
}
#else
#ifdef WANT_DJB2_HASH
static unsigned djb2_hash(const char *str)
{
    unsigned hash = 5381;
    int c;
    short count = MAX_HASH_LENGTH;

    DEBUG(DEBUG_HASH) {
        log_error_time();
        fprintf(stderr, "djb2_hash(%s) = ", str);
    }

    while ((c = *(str++)) && count--)
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

    DEBUG(DEBUG_HASH) {
        fprintf(stderr, "%u\n", hash);
    }
    return hash;
}
#else
/*
 * magic 32 bit FNV1a hash constants
 *
 * See: http://www.isthe.com/chongo/tech/comp/fnv/index.html
 */
#define OFFSET_BASIS 2166136261U
#define FNV_PRIME 16777619U
/*
 *
 * hash.c:152: warning: width of integer constant may change on other systems with -traditional
 */

static unsigned fnv1a_hash(const char *str)
{
    unsigned int hash = OFFSET_BASIS;
    short count = MAX_HASH_LENGTH;

    /*
     * compute FNV1a hash of the first file component
     */
    do {
        hash ^= *str++;
        hash *= FNV_PRIME;
    } while (*str != '\0' && count--);

    return hash;
}

#endif
#endif
#endif

/*
 * Name: hash_insert
 * Description: Adds a key/value pair to the provided hashtable
 */

static
hash_struct *hash_insert(hash_struct * table[], const unsigned int hash,
                         const char *key, const char *value)
{
    hash_struct *current, *trailer;

    if (!key) {
        log_error_time();
        fprintf(stderr, "Yipes! Null value sent as key! [hash_insert]!\n");
        return NULL;
    } else if (key[0] == '\0') {
        log_error_time();
        fprintf(stderr,
                "Attempt to hash NULL or empty key! [hash_insert]!\n");
        return NULL;
    }

    if (!value) {
        log_error_time();
        fprintf(stderr, "Yipes! Null value sent as value! [hash_insert]!\n");
        return NULL;
    } else if (value[0] == '\0') {
        log_error_time();
        fprintf(stderr,
                "Attempt to hash NULL or empty value! [hash_insert]!\n");
        return NULL;
    }

    /* should we bother to check table, hash, and key for NULL/0 ? */

    DEBUG(DEBUG_HASH) {
        fprintf(stderr,
                "Adding key \"%s\" for value \"%s\" (hash=%d)\n",
                key, value, hash);
    }

    current = table[hash];
    while (current) {
        if (!strcmp(current->key, key))
            return current;     /* don't add extension twice */
        if (current->next)
            current = current->next;
        else
            break;
    }

    /* not found */
    trailer = (hash_struct *) malloc(sizeof (hash_struct));
    if (trailer == NULL) {
        WARN("unable to allocate memory for hash_insert");
        return NULL;
    }

    trailer->key = strdup(key);
    trailer->value = strdup(value);
    trailer->next = NULL;

    if (trailer->key == NULL || trailer->value == NULL) {
        int errno_save = errno;

        free(trailer);
        if (trailer->key)
            free(trailer->key);
        if (trailer->value)
            free(trailer->value);
        errno = errno_save;
        WARN("allocated key or value is NULL");
        return NULL;
    }

    /* successfully allocated and built new hash structure */
    if (!current) {
        /* no entries for this hash value */
        table[hash] = trailer;
    } else {
        current->next = trailer;
    }

    return trailer;
}

static
hash_struct *hash_find(hash_struct * table[], const char *key,
                          const unsigned int hash)
{
    hash_struct *current;

    current = table[hash];

    if (!key) {
        log_error_time();
        fprintf(stderr, "Yipes! Null value sent as key! [hash_find]!\n");
        return NULL;
    } else if (key[0] == '\0') {
        log_error_time();
        fprintf(stderr,
                "Attempt to locate empty string in hash! [hash_find]!\n");
        return NULL;
    }

    while (current) {
        if (!strcmp(current->key, key)) /* hit */
            return current;
        current = current->next;
    }

    return NULL;
}

static
void hash_clear(hash_struct * table[], int size)
{
    int i;
    hash_struct *temp;
    for (i = 0; i < size; ++i) { /* these limits OK? */
        temp = table[i];
        while (temp) {
            hash_struct *temp_next;

            temp_next = temp->next;
            free(temp->key);
            free(temp->value);
            free(temp);

            temp = temp_next;
        }
        table[i] = NULL;
    }
}

void hash_show_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);

}


/*******************************************************************/
/*******************************************************************/
/******* Generic Hash Functions Above, Specfic Below ***************/
/*******************************************************************/
/*******************************************************************/

/*
 * Name: add_mime_type
 * Description: Adds a key/value pair to the mime_hashtable
 */

void add_mime_type(const char *extension, const char *type)
{
    unsigned int hash;

    hash = get_mime_hash_value(extension);
    hash_insert(mime_hashtable, hash, extension, type);
}

/*
 * 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(const char *extension)
{
    return boa_hash(extension) % MIME_HASHTABLE_SIZE;
}

/*
 * Name: get_mime_type
 *
 * Description: Returns the mime type for a supplied filename.
 * Returns default type if not found.
 */

char *get_mime_type(const char *filename)
{
    char *extension;
    hash_struct *current;

    unsigned int hash;

    if (filename == NULL) {
        log_error_time();
        fprintf(stderr,
                "Attempt to hash NULL string! [get_mime_type]\n");
        return default_type;
    } else if (filename[0] == '\0') {
        log_error_time();
        fprintf(stderr,
                "Attempt to hash empty string! [get_mime_type]\n");
        return default_type;
    }

    extension = strrchr(filename, '.');

    /* remember, extension points to the *last* '.' in the filename,
     * which may be NULL or look like:
     *  foo.bar
     *  foo. (in which case extension[1] == '\0')
     */
    /* extension[0] *can't* be NIL */
    if (!extension || extension[1] == '\0')
        return default_type;

    /* make sure we hash on the 'bar' not the '.bar' */
    ++extension;

    hash = get_mime_hash_value(extension);
    current = hash_find(mime_hashtable, extension, hash);
    return (current ? current->value : 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
 */

static unsigned get_homedir_hash_value(const char *name)
{
    return boa_hash(name) % PASSWD_HASHTABLE_SIZE;
}


/*
 * 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(const char *name)
{
    hash_struct *current;

    unsigned int hash;

    hash = get_homedir_hash_value(name);

    current = hash_find(passwd_hashtable, name, hash);

    if (!current) {
        /* not found */
        struct passwd *passwdbuf;

        passwdbuf = getpwnam(name);

        if (!passwdbuf)         /* does not exist */
            return NULL;

        current =
            hash_insert(passwd_hashtable, hash, name, passwdbuf->pw_dir);
    }

    return (current ? current->value : NULL);
}

void dump_mime(void)
{
    hash_clear(mime_hashtable, MIME_HASHTABLE_SIZE);
}

void dump_passwd(void)
{
    hash_clear(passwd_hashtable, PASSWD_HASHTABLE_SIZE);
}