/* $OpenBSD: hash.c,v 1.9 2004/05/10 15:30:47 deraadt Exp $ */12/* Routines for manipulating hash tables... */34/*-5* SPDX-License-Identifier: BSD-3-Clause6*7* Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium.8* All rights reserved.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13*14* 1. Redistributions of source code must retain the above copyright15* notice, this list of conditions and the following disclaimer.16* 2. Redistributions in binary form must reproduce the above copyright17* notice, this list of conditions and the following disclaimer in the18* documentation and/or other materials provided with the distribution.19* 3. Neither the name of The Internet Software Consortium nor the names20* of its contributors may be used to endorse or promote products derived21* from this software without specific prior written permission.22*23* THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND24* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,25* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF26* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE27* DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR28* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,29* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF31* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND32* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,33* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT34* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF35* SUCH DAMAGE.36*37* This software has been written for the Internet Software Consortium38* by Ted Lemon <[email protected]> in cooperation with Vixie39* Enterprises. To learn more about the Internet Software Consortium,40* see ``http://www.vix.com/isc''. To learn more about Vixie41* Enterprises, see ``http://www.vix.com''.42*/4344#include <sys/cdefs.h>45#include "dhcpd.h"4647static int do_hash(const unsigned char *, int, int);4849struct hash_table *50new_hash(void)51{52struct hash_table *rv = new_hash_table(DEFAULT_HASH_SIZE);5354if (!rv)55return (rv);56memset(&rv->buckets[0], 0,57DEFAULT_HASH_SIZE * sizeof(struct hash_bucket *));58return (rv);59}6061static int62do_hash(const unsigned char *name, int len, int size)63{64const unsigned char *s = name;65int accum = 0, i = len;6667while (i--) {68/* Add the character in... */69accum += *s++;70/* Add carry back in... */71while (accum > 255)72accum = (accum & 255) + (accum >> 8);73}74return (accum % size);75}7677void add_hash(struct hash_table *table, const unsigned char *name, int len,78unsigned char *pointer)79{80struct hash_bucket *bp;81int hashno;8283if (!table)84return;85if (!len)86len = strlen((const char *)name);8788hashno = do_hash(name, len, table->hash_count);89bp = new_hash_bucket();9091if (!bp) {92warning("Can't add %s to hash table.", name);93return;94}95bp->name = name;96bp->value = pointer;97bp->next = table->buckets[hashno];98bp->len = len;99table->buckets[hashno] = bp;100}101102void *103hash_lookup(struct hash_table *table, unsigned char *name, int len)104{105struct hash_bucket *bp;106int hashno;107108if (!table)109return (NULL);110111if (!len)112len = strlen((char *)name);113114hashno = do_hash(name, len, table->hash_count);115116for (bp = table->buckets[hashno]; bp; bp = bp->next)117if (len == bp->len && !memcmp(bp->name, name, len))118return (bp->value);119120return (NULL);121}122123124