From bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4 Mon Sep 17 00:00:00 2001 From: Bob Halley Date: Thu, 28 Jan 1999 23:52:00 +0000 Subject: [PATCH] add dns_name_hash --- lib/dns/include/dns/name.h | 10 ++++++++++ lib/dns/name.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/lib/dns/include/dns/name.h b/lib/dns/include/dns/name.h index 532e58dcbb..8018b79ea1 100644 --- a/lib/dns/include/dns/name.h +++ b/lib/dns/include/dns/name.h @@ -243,6 +243,16 @@ isc_boolean_t dns_name_isabsolute(dns_name_t *name); * FALSE The last label in 'name' is not the root label. */ +unsigned int dns_name_hash(dns_name_t *name); +/* + * Provide a hash value for 'name'. + * + * Requires: + * 'name' is a valid name + * + * Returns: + * A hash value + */ /*** *** Comparisons diff --git a/lib/dns/name.c b/lib/dns/name.c index 4ac7448321..94d3b35ddb 100644 --- a/lib/dns/name.c +++ b/lib/dns/name.c @@ -294,6 +294,44 @@ dns_name_isabsolute(dns_name_t *name) { return (ISC_FALSE); } +unsigned int +dns_name_hash(dns_name_t *name) { + unsigned int length; + const char *s; + unsigned int h = 0; + unsigned int g; + + /* + * Provide a hash value for 'name'. + */ + REQUIRE(VALID_NAME(name)); + + if (name->labels == 0) + return (0); + length = name->length; + if (length > 16) + length = 16; + + /* + * P. J. Weinberger's hash function, adapted from p. 436 of + * _Compilers: Principles, Techniques, and Tools_, Aho, Sethi + * and Ullman, Addison-Wesley, 1986, ISBN 0-201-10088-6. + */ + + s = name->ndata; + while (length > 0) { + h = ( h << 4 ) + *s; + if ((g = ( h & 0xf0000000 )) != 0) { + h = h ^ (g >> 24); + h = h ^ g; + } + s++; + length--; + } + + return (h); +} + int dns_name_compare(dns_name_t *name1, dns_name_t *name2) { unsigned int l1, l2, l, count1, count2, count;