Changeset View
Changeset View
Standalone View
Standalone View
sbin/dhclient/hash.c
Show First 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | |||||
* Enterprises, see ``http://www.vix.com''. | * Enterprises, see ``http://www.vix.com''. | ||||
*/ | */ | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include "dhcpd.h" | #include "dhcpd.h" | ||||
static int do_hash(unsigned char *, int, int); | static int do_hash(const unsigned char *, int, int); | ||||
struct hash_table * | struct hash_table * | ||||
new_hash(void) | new_hash(void) | ||||
{ | { | ||||
struct hash_table *rv = new_hash_table(DEFAULT_HASH_SIZE); | struct hash_table *rv = new_hash_table(DEFAULT_HASH_SIZE); | ||||
if (!rv) | if (!rv) | ||||
return (rv); | return (rv); | ||||
memset(&rv->buckets[0], 0, | memset(&rv->buckets[0], 0, | ||||
DEFAULT_HASH_SIZE * sizeof(struct hash_bucket *)); | DEFAULT_HASH_SIZE * sizeof(struct hash_bucket *)); | ||||
return (rv); | return (rv); | ||||
} | } | ||||
static int | static int | ||||
do_hash(unsigned char *name, int len, int size) | do_hash(const unsigned char *name, int len, int size) | ||||
{ | { | ||||
unsigned char *s = name; | const unsigned char *s = name; | ||||
int accum = 0, i = len; | int accum = 0, i = len; | ||||
while (i--) { | while (i--) { | ||||
/* Add the character in... */ | /* Add the character in... */ | ||||
accum += *s++; | accum += *s++; | ||||
/* Add carry back in... */ | /* Add carry back in... */ | ||||
while (accum > 255) | while (accum > 255) | ||||
accum = (accum & 255) + (accum >> 8); | accum = (accum & 255) + (accum >> 8); | ||||
} | } | ||||
return (accum % size); | return (accum % size); | ||||
} | } | ||||
void add_hash(struct hash_table *table, unsigned char *name, int len, | void add_hash(struct hash_table *table, const unsigned char *name, int len, | ||||
unsigned char *pointer) | unsigned char *pointer) | ||||
{ | { | ||||
struct hash_bucket *bp; | struct hash_bucket *bp; | ||||
int hashno; | int hashno; | ||||
if (!table) | if (!table) | ||||
return; | return; | ||||
if (!len) | if (!len) | ||||
len = strlen((char *)name); | len = strlen((const char *)name); | ||||
hashno = do_hash(name, len, table->hash_count); | hashno = do_hash(name, len, table->hash_count); | ||||
bp = new_hash_bucket(); | bp = new_hash_bucket(); | ||||
if (!bp) { | if (!bp) { | ||||
warning("Can't add %s to hash table.", name); | warning("Can't add %s to hash table.", name); | ||||
return; | return; | ||||
} | } | ||||
bp->name = name; | bp->name = name; | ||||
bp->value = pointer; | bp->value = pointer; | ||||
bp->next = table->buckets[hashno]; | bp->next = table->buckets[hashno]; | ||||
bp->len = len; | bp->len = len; | ||||
table->buckets[hashno] = bp; | table->buckets[hashno] = bp; | ||||
} | } | ||||
unsigned char * | void * | ||||
hash_lookup(struct hash_table *table, unsigned char *name, int len) | hash_lookup(struct hash_table *table, unsigned char *name, int len) | ||||
{ | { | ||||
struct hash_bucket *bp; | struct hash_bucket *bp; | ||||
int hashno; | int hashno; | ||||
if (!table) | if (!table) | ||||
return (NULL); | return (NULL); | ||||
Show All 11 Lines |