Sync up nameser with upstream(2014.03.05)

Bug: 13219633

Change-Id: I8ee4ddf270710e9eea415fa3db984af7719a8dd4
diff --git a/libc/dns/nameser/ns_name.c b/libc/dns/nameser/ns_name.c
index da36425..12bf029 100644
--- a/libc/dns/nameser/ns_name.c
+++ b/libc/dns/nameser/ns_name.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: ns_name.c,v 1.3 2004/11/07 02:19:49 christos Exp $	*/
+/*	$NetBSD: ns_name.c,v 1.9 2012/03/13 21:13:39 christos Exp $	*/
 
 /*
  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
@@ -20,9 +20,9 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #ifdef notdef
-static const char rcsid[] = "Id: ns_name.c,v 1.3.2.4.4.2 2004/05/04 03:27:47 marka Exp";
+static const char rcsid[] = "Id: ns_name.c,v 1.11 2009/01/23 19:59:16 each Exp";
 #else
-__RCSID("$NetBSD: ns_name.c,v 1.3 2004/11/07 02:19:49 christos Exp $");
+__RCSID("$NetBSD: ns_name.c,v 1.9 2012/03/13 21:13:39 christos Exp $");
 #endif
 #endif
 
@@ -31,6 +31,7 @@
 #include <netinet/in.h>
 #include <arpa/nameser.h>
 
+#include <assert.h>
 #include <errno.h>
 #ifdef ANDROID_CHANGES
 #include "resolv_private.h"
@@ -43,9 +44,9 @@
 #include <limits.h>
 
 #ifdef SPRINTF_CHAR
-# define SPRINTF(x) strlen(sprintf/**/x)
+# define SPRINTF(x) ((int)strlen(sprintf/**/x))
 #else
-# define SPRINTF(x) ((size_t)sprintf x)
+# define SPRINTF(x) (sprintf x)
 #endif
 
 #define NS_TYPE_ELT			0x40 /* EDNS0 extended label type */
@@ -91,10 +92,10 @@
 /* Public. */
 
 /*
- * ns_name_ntop(src, dst, dstsiz)
  *	Convert an encoded domain name to printable ascii as per RFC1035.
  * return:
  *	Number of bytes written to buffer, or -1 (with errno set)
+ *
  * notes:
  *	The root is returned as "."
  *	All other domains are returned in non absolute form
@@ -188,23 +189,42 @@
 		return (-1);
 	}
 	*dn++ = '\0';
-	return (dn - dst);
+	_DIAGASSERT(__type_fit(int, dn - dst));
+	return (int)(dn - dst);
 }
 
 /*
- * ns_name_pton(src, dst, dstsiz)
+ *	Convert a ascii string into an encoded domain name as per RFC1035.
+ *
+ * return:
+ *
+ *	-1 if it fails
+ *	1 if string was fully qualified
+ *	0 is string was not fully qualified
+ *
+ * notes:
+ *	Enforces label and domain length limits.
+ */
+int
+ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
+	return (ns_name_pton2(src, dst, dstsiz, NULL));
+}
+
+/*
+ * ns_name_pton2(src, dst, dstsiz, *dstlen)
  *	Convert a ascii string into an encoded domain name as per RFC1035.
  * return:
  *	-1 if it fails
  *	1 if string was fully qualified
  *	0 is string was not fully qualified
+ * side effects:
+ *	fills in *dstlen (if non-NULL)
  * notes:
  *	Enforces label and domain length limits.
  */
 
 int
-ns_name_pton(const char *src, u_char *dst, size_t dstsiz)
-{
+ns_name_pton2(const char *src, u_char *dst, size_t dstsiz, size_t *dstlen) {
 	u_char *label, *bp, *eom;
 	int c, n, escaped, e = 0;
 	char *cp;
@@ -238,19 +258,19 @@
 				continue;
 			}
 			else if ((cp = strchr(digits, c)) != NULL) {
-				n = (cp - digits) * 100;
+				n = (int)(cp - digits) * 100;
 				if ((c = *src++) == 0 ||
 				    (cp = strchr(digits, c)) == NULL) {
 					errno = EMSGSIZE;
 					return (-1);
 				}
-				n += (cp - digits) * 10;
+				n += (int)(cp - digits) * 10;
 				if ((c = *src++) == 0 ||
 				    (cp = strchr(digits, c)) == NULL) {
 					errno = EMSGSIZE;
 					return (-1);
 				}
-				n += (cp - digits);
+				n += (int)(cp - digits);
 				if (n > 255) {
 					errno = EMSGSIZE;
 					return (-1);
@@ -262,7 +282,7 @@
 			escaped = 1;
 			continue;
 		} else if (c == '.') {
-			c = (bp - label - 1);
+			c = (int)(bp - label - 1);
 			if ((c & NS_CMPRSFLGS) != 0) {	/* Label too big. */
 				errno = EMSGSIZE;
 				return (-1);
@@ -285,6 +305,8 @@
 					errno = EMSGSIZE;
 					return (-1);
 				}
+				if (dstlen != NULL)
+					*dstlen = (bp - dst);
 				return (1);
 			}
 			if (c == 0 || *src == '.') {
@@ -300,7 +322,7 @@
 		}
 		*bp++ = (u_char)c;
 	}
-	c = (bp - label - 1);
+	c = (int)(bp - label - 1);
 	if ((c & NS_CMPRSFLGS) != 0) {		/* Label too big. */
 		errno = EMSGSIZE;
 		return (-1);
@@ -322,14 +344,17 @@
 		errno = EMSGSIZE;
 		return (-1);
 	}
+	if (dstlen != NULL)
+		*dstlen = (bp - dst);
 	return (0);
 }
 
 /*
- * ns_name_ntol(src, dst, dstsiz)
  *	Convert a network strings labels into all lowercase.
+ *
  * return:
  *	Number of bytes written to buffer, or -1 (with errno set)
+ *
  * notes:
  *	Enforces label and domain length limits.
  */
@@ -368,19 +393,20 @@
 		}
 		for (; l > 0; l--) {
 			c = *cp++;
-			if (isupper(c))
+			if (isascii(c) && isupper(c))
 				*dn++ = tolower(c);
 			else
 				*dn++ = c;
 		}
 	}
 	*dn++ = '\0';
-	return (dn - dst);
+	_DIAGASSERT(__type_fit(int, dn - dst));
+	return (int)(dn - dst);
 }
 
 /*
- * ns_name_unpack(msg, eom, src, dst, dstsiz)
  *	Unpack a domain name from a message, source may be compressed.
+ *
  * return:
  *	-1 if it fails, or consumed octets if it succeeds.
  */
@@ -388,6 +414,21 @@
 ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
 	       u_char *dst, size_t dstsiz)
 {
+	return (ns_name_unpack2(msg, eom, src, dst, dstsiz, NULL));
+}
+
+/*
+ * ns_name_unpack2(msg, eom, src, dst, dstsiz, *dstlen)
+ *	Unpack a domain name from a message, source may be compressed.
+ * return:
+ *	-1 if it fails, or consumed octets if it succeeds.
+ * side effect:
+ *	fills in *dstlen (if non-NULL).
+ */
+int
+ns_name_unpack2(const u_char *msg, const u_char *eom, const u_char *src,
+		u_char *dst, size_t dstsiz, size_t *dstlen)
+{
 	const u_char *srcp, *dstlim;
 	u_char *dstp;
 	int n, len, checked, l;
@@ -428,8 +469,10 @@
 				errno = EMSGSIZE;
 				return (-1);
 			}
-			if (len < 0)
-				len = srcp - src + 1;
+			if (len < 0) {
+				_DIAGASSERT(__type_fit(int, srcp - src + 1));
+				len = (int)(srcp - src + 1);
+			}
 			srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
 			if (srcp < msg || srcp >= eom) {  /* Out of range. */
 				errno = EMSGSIZE;
@@ -452,23 +495,29 @@
 			return (-1);			/* flag error */
 		}
 	}
-	*dstp = '\0';
-	if (len < 0)
-		len = srcp - src;
-	return (len);
+	*dstp++ = 0;
+	if (dstlen != NULL)
+		*dstlen = dstp - dst;
+	if (len < 0) {
+		_DIAGASSERT(__type_fit(int, srcp - src));
+		len = (int)(srcp - src);
+	}
+	return len;
 }
 
 /*
- * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
  *	Pack domain name 'domain' into 'comp_dn'.
+ *
  * return:
  *	Size of the compressed name, or -1.
+ *
  * notes:
  *	'dnptrs' is an array of pointers to previous compressed names.
  *	dnptrs[0] is a pointer to the beginning of the message. The array
  *	ends with NULL.
  *	'lastdnptr' is a pointer to the end of the array pointed to
  *	by 'dnptrs'.
+ *
  * Side effects:
  *	The list of pointers in dnptrs is updated for labels inserted into
  *	the message as we compress the name.  If 'dnptr' is NULL, we don't
@@ -491,7 +540,7 @@
 	if (dnptrs != NULL) {
 		if ((msg = *dnptrs++) != NULL) {
 			for (cpp = dnptrs; *cpp != NULL; cpp++)
-				;
+				continue;
 			lpp = cpp;	/* end of list to search */
 		}
 	} else
@@ -533,7 +582,8 @@
 				}
 				*dstp++ = ((u_int32_t)l >> 8) | NS_CMPRSFLGS;
 				*dstp++ = l % 256;
-				return (dstp - dst);
+				_DIAGASSERT(__type_fit(int, dstp - dst));
+				return (int)(dstp - dst);
 			}
 			/* Not found, save it. */
 			if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
@@ -564,14 +614,16 @@
 		errno = EMSGSIZE;
 		return (-1);
 	}
-	return (dstp - dst);
+	_DIAGASSERT(__type_fit(int, dstp - dst));
+	return (int)(dstp - dst);
 }
 
 /*
- * ns_name_uncompress(msg, eom, src, dst, dstsiz)
  *	Expand compressed domain name to presentation format.
+ *
  * return:
  *	Number of bytes read out of `src', or -1 (with errno set).
+ *
  * note:
  *	Root domain returns as "." not "".
  */
@@ -590,10 +642,11 @@
 }
 
 /*
- * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
  *	Compress a domain name into wire format, using compression pointers.
+ *
  * return:
  *	Number of bytes consumed in `dst' or -1 (with errno set).
+ *
  * notes:
  *	'dnptrs' is an array of pointers to previous compressed names.
  *	dnptrs[0] is a pointer to the beginning of the message.
@@ -632,8 +685,8 @@
 }
 
 /*
- * ns_name_skip(ptrptr, eom)
  *	Advance *ptrptr to skip over the compressed name it points at.
+ *
  * return:
  *	0 on success, -1 (with errno set) on failure.
  */
@@ -675,12 +728,156 @@
 	return (0);
 }
 
+/* Find the number of octets an nname takes up, including the root label.
+ * (This is basically ns_name_skip() without compression-pointer support.)
+ * ((NOTE: can only return zero if passed-in namesiz argument is zero.))
+ */
+ssize_t
+ns_name_length(ns_nname_ct nname, size_t namesiz) {
+	ns_nname_ct orig = nname;
+	u_int n;
+
+	while (namesiz-- > 0 && (n = *nname++) != 0) {
+		if ((n & NS_CMPRSFLGS) != 0) {
+			errno = EISDIR;
+			return (-1);
+		}
+		if (n > namesiz) {
+			errno = EMSGSIZE;
+			return (-1);
+		}
+		nname += n;
+		namesiz -= n;
+	}
+	return (nname - orig);
+}
+
+/* Compare two nname's for equality.  Return -1 on error (setting errno).
+ */
+int
+ns_name_eq(ns_nname_ct a, size_t as, ns_nname_ct b, size_t bs) {
+	ns_nname_ct ae = a + as, be = b + bs;
+	int ac, bc;
+
+	while (ac = *a, bc = *b, ac != 0 && bc != 0) {
+		if ((ac & NS_CMPRSFLGS) != 0 || (bc & NS_CMPRSFLGS) != 0) {
+			errno = EISDIR;
+			return (-1);
+		}
+		if (a + ac >= ae || b + bc >= be) {
+			errno = EMSGSIZE;
+			return (-1);
+		}
+		if (ac != bc || strncasecmp((const char *) ++a,
+					    (const char *) ++b,
+					    (size_t)ac) != 0)
+			return (0);
+		a += ac, b += bc;
+	}
+	return (ac == 0 && bc == 0);
+}
+
+/* Is domain "A" owned by (at or below) domain "B"?
+ */
+int
+ns_name_owned(ns_namemap_ct a, int an, ns_namemap_ct b, int bn) {
+	/* If A is shorter, it cannot be owned by B. */
+	if (an < bn)
+		return (0);
+
+	/* If they are unequal before the length of the shorter, A cannot... */
+	while (bn > 0) {
+		if (a->len != b->len ||
+		    strncasecmp((const char *) a->base,
+				(const char *) b->base, (size_t)a->len) != 0)
+			return (0);
+		a++, an--;
+		b++, bn--;
+	}
+
+	/* A might be longer or not, but either way, B owns it. */
+	return (1);
+}
+
+/* Build an array of <base,len> tuples from an nname, top-down order.
+ * Return the number of tuples (labels) thus discovered.
+ */
+int
+ns_name_map(ns_nname_ct nname, size_t namelen, ns_namemap_t map, int mapsize) {
+	u_int n;
+	int l;
+
+	n = *nname++;
+	namelen--;
+
+	/* Root zone? */
+	if (n == 0) {
+		/* Extra data follows name? */
+		if (namelen > 0) {
+			errno = EMSGSIZE;
+			return (-1);
+		}
+		return (0);
+	}
+
+	/* Compression pointer? */
+	if ((n & NS_CMPRSFLGS) != 0) {
+		errno = EISDIR;
+		return (-1);
+	}
+
+	/* Label too long? */
+	if (n > namelen) {
+		errno = EMSGSIZE;
+		return (-1);
+	}
+
+	/* Recurse to get rest of name done first. */
+	l = ns_name_map(nname + n, namelen - n, map, mapsize);
+	if (l < 0)
+		return (-1);
+
+	/* Too many labels? */
+	if (l >= mapsize) {
+		errno = ENAMETOOLONG;
+		return (-1);
+	}
+
+	/* We're on our way back up-stack, store current map data. */
+	map[l].base = nname;
+	map[l].len = n;
+	return (l + 1);
+}
+
+/* Count the labels in a domain name.  Root counts, so COM. has two.  This
+ * is to make the result comparable to the result of ns_name_map().
+ */
+int
+ns_name_labels(ns_nname_ct nname, size_t namesiz) {
+	int ret = 0;
+	u_int n;
+
+	while (namesiz-- > 0 && (n = *nname++) != 0) {
+		if ((n & NS_CMPRSFLGS) != 0) {
+			errno = EISDIR;
+			return (-1);
+		}
+		if (n > namesiz) {
+			errno = EMSGSIZE;
+			return (-1);
+		}
+		nname += n;
+		namesiz -= n;
+		ret++;
+	}
+	return (ret + 1);
+}
 /* Private. */
 
 /*
- * special(ch)
  *	Thinking in noninternationalized USASCII (per the DNS spec),
  *	is this characted special ("in need of quoting") ?
+ *
  * return:
  *	boolean.
  */
@@ -703,9 +900,9 @@
 }
 
 /*
- * printable(ch)
  *	Thinking in noninternationalized USASCII (per the DNS spec),
  *	is this character visible and not a space when printed ?
+ *
  * return:
  *	boolean.
  */
@@ -726,10 +923,11 @@
 }
 
 /*
- * dn_find(domain, msg, dnptrs, lastdnptr)
  *	Search for the counted-label name in an array of compressed names.
+ *
  * return:
  *	offset from msg if found, or -1.
+ *
  * notes:
  *	dnptrs is the pointer to the first name on the list,
  *	not the pointer to the start of the message.
@@ -771,8 +969,11 @@
 						    mklower(*cp++))
 							goto next;
 					/* Is next root for both ? */
-					if (*dn == '\0' && *cp == '\0')
-						return (sp - msg);
+					if (*dn == '\0' && *cp == '\0') {
+						_DIAGASSERT(__type_fit(int,
+						    sp - msg));
+						return (int)(sp - msg);
+					}
 					if (*dn)
 						continue;
 					goto next;
@@ -803,7 +1004,7 @@
 	if ((blen = (*cp & 0xff)) == 0)
 		blen = 256;
 	plen = (blen + 3) / 4;
-	plen += sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1);
+	plen += (int)sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1);
 	if (dn + plen >= eom)
 		return(-1);
 
@@ -838,7 +1039,8 @@
 	dn += i;
 
 	*cpp = cp;
-	return(dn - beg);
+	_DIAGASSERT(__type_fit(int, dn - beg));
+	return (int)(dn - beg);
 }
 
 static int
diff --git a/libc/dns/nameser/ns_netint.c b/libc/dns/nameser/ns_netint.c
index 33e6090..8b546f8 100644
--- a/libc/dns/nameser/ns_netint.c
+++ b/libc/dns/nameser/ns_netint.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: ns_netint.c,v 1.2 2004/05/20 20:19:00 christos Exp $	*/
+/*	$NetBSD: ns_netint.c,v 1.7 2012/03/13 21:13:39 christos Exp $	*/
 
 /*
  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
@@ -20,9 +20,9 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #ifdef notdef
-static const char rcsid[] = "Id: ns_netint.c,v 1.1.206.1 2004/03/09 08:33:44 marka Exp";
+static const char rcsid[] = "Id: ns_netint.c,v 1.3 2005/04/27 04:56:40 sra Exp";
 #else
-__RCSID("$NetBSD: ns_netint.c,v 1.2 2004/05/20 20:19:00 christos Exp $");
+__RCSID("$NetBSD: ns_netint.c,v 1.7 2012/03/13 21:13:39 christos Exp $");
 #endif
 #endif
 
@@ -32,28 +32,28 @@
 
 /* Public. */
 
-u_int16_t
+uint16_t
 ns_get16(const u_char *src) {
-	u_int dst;
+	uint16_t dst;
 
 	NS_GET16(dst, src);
-	return (dst);
+	return dst;
 }
 
-u_int32_t
+uint32_t
 ns_get32(const u_char *src) {
-	u_long dst;
+	u_int32_t dst;
 
 	NS_GET32(dst, src);
-	return (dst);
+	return dst;
 }
 
 void
-ns_put16(u_int16_t src, u_char *dst) {
+ns_put16(uint16_t src, u_char *dst) {
 	NS_PUT16(src, dst);
 }
 
 void
-ns_put32(u_int32_t src, u_char *dst) {
+ns_put32(uint32_t src, u_char *dst) {
 	NS_PUT32(src, dst);
 }
diff --git a/libc/dns/nameser/ns_parse.c b/libc/dns/nameser/ns_parse.c
index 899ff81..2d6d530 100644
--- a/libc/dns/nameser/ns_parse.c
+++ b/libc/dns/nameser/ns_parse.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: ns_parse.c,v 1.2 2004/05/20 20:35:05 christos Exp $	*/
+/*	$NetBSD: ns_parse.c,v 1.9 2012/03/13 21:13:39 christos Exp $	*/
 
 /*
  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
@@ -20,9 +20,9 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #ifdef notdef
-static const char rcsid[] = "Id: ns_parse.c,v 1.3.2.1.4.1 2004/03/09 08:33:44 marka Exp";
+static const char rcsid[] = "Id: ns_parse.c,v 1.10 2009/01/23 19:59:16 each Exp";
 #else
-__RCSID("$NetBSD: ns_parse.c,v 1.2 2004/05/20 20:35:05 christos Exp $");
+__RCSID("$NetBSD: ns_parse.c,v 1.9 2012/03/13 21:13:39 christos Exp $");
 #endif
 #endif
 
@@ -33,6 +33,7 @@
 #include <netinet/in.h>
 #include <arpa/nameser.h>
 
+#include <assert.h>
 #include <errno.h>
 #ifdef ANDROID_CHANGES
 #include "resolv_private.h"
@@ -96,7 +97,8 @@
 	}
 	if (ptr > eom)
 		RETERR(EMSGSIZE);
-	return (ptr - optr);
+	_DIAGASSERT(__type_fit(int, ptr - optr));
+	return (int)(ptr - optr);
 }
 
 int
@@ -104,7 +106,6 @@
 	const u_char *eom = msg + msglen;
 	int i;
 
-	memset(handle, 0x5e, sizeof *handle);
 	handle->_msg = msg;
 	handle->_eom = eom;
 	if (msg + NS_INT16SZ > eom)
@@ -139,9 +140,11 @@
 int
 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
 	int b;
+	int tmp;
 
 	/* Make section right. */
-	if ((unsigned)section >= (unsigned)ns_s_max)
+	tmp = section;
+	if (tmp < 0 || section >= ns_s_max)
 		RETERR(ENODEV);
 	if (section != handle->_sect)
 		setsection(handle, section);
@@ -194,6 +197,69 @@
 	return (0);
 }
 
+/*
+ * This is identical to the above but uses network-format (uncompressed) names.
+ */
+int
+ns_parserr2(ns_msg *handle, ns_sect section, int rrnum, ns_rr2 *rr) {
+	int b;
+	int tmp;
+
+	/* Make section right. */
+	tmp = section;
+	if (tmp < 0 || section >= ns_s_max)
+		RETERR(ENODEV);
+	if (section != handle->_sect)
+		setsection(handle, section);
+
+	/* Make rrnum right. */
+	if (rrnum == -1)
+		rrnum = handle->_rrnum;
+	if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
+		RETERR(ENODEV);
+	if (rrnum < handle->_rrnum)
+		setsection(handle, section);
+	if (rrnum > handle->_rrnum) {
+		b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
+			      rrnum - handle->_rrnum);
+
+		if (b < 0)
+			return (-1);
+		handle->_msg_ptr += b;
+		handle->_rrnum = rrnum;
+	}
+
+	/* Do the parse. */
+	b = ns_name_unpack2(handle->_msg, handle->_eom, handle->_msg_ptr,
+			    rr->nname, NS_MAXNNAME, &rr->nnamel);
+	if (b < 0)
+		return (-1);
+	handle->_msg_ptr += b;
+	if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
+		RETERR(EMSGSIZE);
+	NS_GET16(rr->type, handle->_msg_ptr);
+	NS_GET16(rr->rr_class, handle->_msg_ptr);
+	if (section == ns_s_qd) {
+		rr->ttl = 0;
+		rr->rdlength = 0;
+		rr->rdata = NULL;
+	} else {
+		if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
+			RETERR(EMSGSIZE);
+		NS_GET32(rr->ttl, handle->_msg_ptr);
+		NS_GET16(rr->rdlength, handle->_msg_ptr);
+		if (handle->_msg_ptr + rr->rdlength > handle->_eom)
+			RETERR(EMSGSIZE);
+		rr->rdata = handle->_msg_ptr;
+		handle->_msg_ptr += rr->rdlength;
+	}
+	if (++handle->_rrnum > handle->_counts[(int)section])
+		setsection(handle, (ns_sect)((int)section + 1));
+
+	/* All done. */
+	return (0);
+}
+
 /* Private. */
 
 static void
diff --git a/libc/dns/nameser/ns_print.c b/libc/dns/nameser/ns_print.c
index 0b3c068..0a6a1d6 100644
--- a/libc/dns/nameser/ns_print.c
+++ b/libc/dns/nameser/ns_print.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: ns_print.c,v 1.5 2004/11/07 02:19:49 christos Exp $	*/
+/*	$NetBSD: ns_print.c,v 1.11 2012/03/13 21:13:39 christos Exp $	*/
 
 /*
  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
@@ -20,9 +20,9 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #ifdef notdef
-static const char rcsid[] = "Id: ns_print.c,v 1.3.2.1.4.5 2004/07/28 20:16:45 marka Exp";
+static const char rcsid[] = "Id: ns_print.c,v 1.12 2009/03/03 05:29:58 each Exp";
 #else
-__RCSID("$NetBSD: ns_print.c,v 1.5 2004/11/07 02:19:49 christos Exp $");
+__RCSID("$NetBSD: ns_print.c,v 1.11 2012/03/13 21:13:39 christos Exp $");
 #endif
 #endif
 
@@ -37,20 +37,21 @@
 
 #include <isc/assertions.h>
 #include <isc/dst.h>
+#include <assert.h>
 #include <errno.h>
 #ifdef ANDROID_CHANGES
 #include "resolv_private.h"
 #else
 #include <resolv.h>
 #endif
+#include <stddef.h>
 #include <string.h>
 #include <ctype.h>
-#include <assert.h>
 
 #ifdef SPRINTF_CHAR
-# define SPRINTF(x) strlen(sprintf/**/x)
+# define SPRINTF(x) ((int)strlen(sprintf/**/x))
 #else
-# define SPRINTF(x) ((size_t)sprintf x)
+# define SPRINTF(x) (sprintf x)
 #endif
 
 #ifndef MIN
@@ -79,12 +80,13 @@
 			return (-1); \
 	} while (/*CONSTCOND*/0)
 
+static const char base32hex[] =
+        "0123456789ABCDEFGHIJKLMNOPQRSTUV=0123456789abcdefghijklmnopqrstuv";
 /* Public. */
 
 /*
- * int
- * ns_sprintrr(handle, rr, name_ctx, origin, buf, buflen)
  *	Convert an RR to presentation format.
+ *
  * return:
  *	Number of characters written to buf, or -1 (check errno).
  */
@@ -103,10 +105,8 @@
 }
 
 /*
- * int
- * ns_sprintrrf(msg, msglen, name, class, type, ttl, rdata, rdlen,
- *	       name_ctx, origin, buf, buflen)
  *	Convert the fields of an RR into presentation format.
+ *
  * return:
  *	Number of characters written to buf, or -1 (check errno).
  */
@@ -131,7 +131,7 @@
 	if (name_ctx != NULL && ns_samename(name_ctx, name) == 1) {
 		T(addstr("\t\t\t", (size_t)3, &buf, &buflen));
 	} else {
-		len = prune_origin(name, origin);
+		len = (int)prune_origin(name, origin);
 		if (*name == '\0') {
 			goto root;
 		} else if (len == 0) {
@@ -166,7 +166,7 @@
 	case ns_t_a:
 		if (rdlen != (size_t)NS_INADDRSZ)
 			goto formerr;
-		(void) inet_ntop(AF_INET, rdata, buf, buflen);
+		(void) inet_ntop(AF_INET, rdata, buf, (socklen_t)buflen);
 		addlen(strlen(buf), &buf, &buflen);
 		break;
 
@@ -265,7 +265,8 @@
 
 	case ns_t_mx:
 	case ns_t_afsdb:
-	case ns_t_rt: {
+	case ns_t_rt:
+	case ns_t_kx: {
 		u_int t;
 
 		if (rdlen < (size_t)NS_INT16SZ)
@@ -313,6 +314,7 @@
 		break;
 
 	case ns_t_txt:
+	case ns_t_spf:
 		while (rdata < edata) {
 			T(len = charstr(rdata, edata, &buf, &buflen));
 			if (len == 0)
@@ -334,7 +336,7 @@
 	case ns_t_aaaa:
 		if (rdlen != (size_t)NS_IN6ADDRSZ)
 			goto formerr;
-		(void) inet_ntop(AF_INET6, rdata, buf, buflen);
+		(void) inet_ntop(AF_INET6, rdata, buf, (socklen_t)buflen);
 		addlen(strlen(buf), &buf, &buflen);
 		break;
 
@@ -425,7 +427,7 @@
 			goto formerr;
 
 		/* Address. */
-		(void) inet_ntop(AF_INET, rdata, buf, buflen);
+		(void) inet_ntop(AF_INET, rdata, buf, (socklen_t)buflen);
 		addlen(strlen(buf), &buf, &buflen);
 		rdata += NS_INADDRSZ;
 
@@ -459,7 +461,8 @@
 		break;
 	    }
 
-	case ns_t_key: {
+	case ns_t_key:
+	case ns_t_dnskey: {
 		char base64_key[NS_MD5RSA_MAX_BASE64];
 		u_int keyflags, protocol, algorithm, key_id;
 		const char *leader;
@@ -505,7 +508,8 @@
 		break;
 	    }
 
-	case ns_t_sig: {
+	case ns_t_sig:
+	case ns_t_rrsig: {
 		char base64_key[NS_MD5RSA_MAX_BASE64];
 		u_int typ, algorithm, labels, footprint;
 		const char *leader;
@@ -566,7 +570,7 @@
 	    }
 
 	case ns_t_nxt: {
-		int n, c;
+		ptrdiff_t n, c;
 
 		/* Next domain name. */
 		T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
@@ -575,7 +579,7 @@
 		n = edata - rdata;
 		for (c = 0; c < n*8; c++)
 			if (NS_NXT_BIT_ISSET(c, rdata)) {
-				len = SPRINTF((tmp, " %s", p_type(c)));
+				len = SPRINTF((tmp, " %s", p_type((int)c)));
 				T(addstr(tmp, (size_t)len, &buf, &buflen));
 			}
 		break;
@@ -584,7 +588,7 @@
 	case ns_t_cert: {
 		u_int c_type, key_tag, alg;
 		int n;
-		unsigned int siz;
+		size_t siz;
 		char base64_cert[8192], tmp1[40];
 		const char *leader;
 
@@ -690,7 +694,7 @@
 			if (rdata + pbyte >= edata) goto formerr;
 			memset(&a, 0, sizeof(a));
 			memcpy(&a.s6_addr[pbyte], rdata, sizeof(a) - pbyte);
-			(void) inet_ntop(AF_INET6, &a, buf, buflen);
+			(void) inet_ntop(AF_INET6, &a, buf, (socklen_t)buflen);
 			addlen(strlen(buf), &buf, &buflen);
 			rdata += sizeof(a) - pbyte;
 		}
@@ -711,25 +715,368 @@
 		break;
 	    }
 
+	case ns_t_ds:
+	case ns_t_dlv:
+	case ns_t_sshfp: {
+		u_int t;
+
+		if (type == ns_t_ds || type == ns_t_dlv) {
+			if (rdlen < 4U) goto formerr;
+			t = ns_get16(rdata);
+			rdata += NS_INT16SZ;
+			len = SPRINTF((tmp, "%u ", t));
+			T(addstr(tmp, (size_t)len, &buf, &buflen));
+		} else
+			if (rdlen < 2U) goto formerr;
+
+		len = SPRINTF((tmp, "%u ", *rdata));
+		T(addstr(tmp, (size_t)len, &buf, &buflen));
+		rdata++;
+
+		len = SPRINTF((tmp, "%u ", *rdata));
+		T(addstr(tmp, (size_t)len, &buf, &buflen));
+		rdata++;
+
+		while (rdata < edata) {
+			len = SPRINTF((tmp, "%02X", *rdata));
+			T(addstr(tmp, (size_t)len, &buf, &buflen));
+			rdata++;
+		}
+		break;
+	    }
+
+	case ns_t_nsec3:
+	case ns_t_nsec3param: {
+		u_int t, w, l, j, k, c;
+
+		len = SPRINTF((tmp, "%u ", *rdata));
+		T(addstr(tmp, (size_t)len, &buf, &buflen));
+		rdata++;
+
+		len = SPRINTF((tmp, "%u ", *rdata));
+		T(addstr(tmp, (size_t)len, &buf, &buflen));
+		rdata++;
+
+		t = ns_get16(rdata);
+		rdata += NS_INT16SZ;
+		len = SPRINTF((tmp, "%u ", t));
+		T(addstr(tmp, (size_t)len, &buf, &buflen));
+
+		t = *rdata++;
+		if (t == 0) {
+			T(addstr("-", 1, &buf, &buflen));
+		} else {
+			while (t-- > 0) {
+				len = SPRINTF((tmp, "%02X", *rdata));
+				T(addstr(tmp, (size_t)len, &buf, &buflen));
+				rdata++;
+			}
+		}
+		if (type == ns_t_nsec3param)
+			break;
+		T(addstr(" ", 1, &buf, &buflen));
+
+		t = *rdata++;
+		while (t > 0) {
+			switch (t) {
+			case 1:
+				tmp[0] = base32hex[(((uint32_t)rdata[0]>>3)&0x1f)];
+				tmp[1] = base32hex[(((uint32_t)rdata[0]<<2)&0x1c)];
+				tmp[2] = tmp[3] = tmp[4] = '=';
+				tmp[5] = tmp[6] = tmp[7] = '=';
+				break;
+			case 2:
+				tmp[0] = base32hex[(((uint32_t)rdata[0]>>3)&0x1f)];
+				tmp[1] = base32hex[(((uint32_t)rdata[0]<<2)&0x1c)|
+						   (((uint32_t)rdata[1]>>6)&0x03)];
+				tmp[2] = base32hex[(((uint32_t)rdata[1]>>1)&0x1f)];
+				tmp[3] = base32hex[(((uint32_t)rdata[1]<<4)&0x10)];
+				tmp[4] = tmp[5] = tmp[6] = tmp[7] = '=';
+				break;
+			case 3:
+				tmp[0] = base32hex[(((uint32_t)rdata[0]>>3)&0x1f)];
+				tmp[1] = base32hex[(((uint32_t)rdata[0]<<2)&0x1c)|
+						   (((uint32_t)rdata[1]>>6)&0x03)];
+				tmp[2] = base32hex[(((uint32_t)rdata[1]>>1)&0x1f)];
+				tmp[3] = base32hex[(((uint32_t)rdata[1]<<4)&0x10)|
+						   (((uint32_t)rdata[2]>>4)&0x0f)];
+				tmp[4] = base32hex[(((uint32_t)rdata[2]<<1)&0x1e)];
+				tmp[5] = tmp[6] = tmp[7] = '=';
+				break;
+			case 4:
+				tmp[0] = base32hex[(((uint32_t)rdata[0]>>3)&0x1f)];
+				tmp[1] = base32hex[(((uint32_t)rdata[0]<<2)&0x1c)|
+						   (((uint32_t)rdata[1]>>6)&0x03)];
+				tmp[2] = base32hex[(((uint32_t)rdata[1]>>1)&0x1f)];
+				tmp[3] = base32hex[(((uint32_t)rdata[1]<<4)&0x10)|
+						   (((uint32_t)rdata[2]>>4)&0x0f)];
+				tmp[4] = base32hex[(((uint32_t)rdata[2]<<1)&0x1e)|
+						   (((uint32_t)rdata[3]>>7)&0x01)];
+				tmp[5] = base32hex[(((uint32_t)rdata[3]>>2)&0x1f)];
+				tmp[6] = base32hex[((uint32_t)rdata[3]<<3)&0x18];
+				tmp[7] = '=';
+				break;
+			default:
+				tmp[0] = base32hex[(((uint32_t)rdata[0]>>3)&0x1f)];
+				tmp[1] = base32hex[(((uint32_t)rdata[0]<<2)&0x1c)|
+						   (((uint32_t)rdata[1]>>6)&0x03)];
+				tmp[2] = base32hex[(((uint32_t)rdata[1]>>1)&0x1f)];
+				tmp[3] = base32hex[(((uint32_t)rdata[1]<<4)&0x10)|
+						   (((uint32_t)rdata[2]>>4)&0x0f)];
+				tmp[4] = base32hex[(((uint32_t)rdata[2]<<1)&0x1e)|
+						   (((uint32_t)rdata[3]>>7)&0x01)];
+				tmp[5] = base32hex[(((uint32_t)rdata[3]>>2)&0x1f)];
+				tmp[6] = base32hex[(((uint32_t)rdata[3]<<3)&0x18)|
+						   (((uint32_t)rdata[4]>>5)&0x07)];
+				tmp[7] = base32hex[(rdata[4]&0x1f)];
+				break;
+			}
+			T(addstr(tmp, 8, &buf, &buflen));
+			if (t >= 5) {
+				rdata += 5;
+				t -= 5;
+			} else {
+				rdata += t;
+				t -= t;
+			}
+		}
+
+		while (rdata < edata) {
+			w = *rdata++;
+			l = *rdata++;
+			for (j = 0; j < l; j++) {
+				if (rdata[j] == 0)
+					continue;
+				for (k = 0; k < 8; k++) {
+					if ((rdata[j] & (0x80 >> k)) == 0)
+						continue;
+					c = w * 256 + j * 8 + k;
+					len = SPRINTF((tmp, " %s", p_type((ns_type)c)));
+					T(addstr(tmp, (size_t)len, &buf, &buflen));
+				}
+			}
+			rdata += l;
+		}
+		break;
+	    }
+
+	case ns_t_nsec: {
+		u_int w, l, j, k, c;
+
+		T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
+
+		while (rdata < edata) {
+			w = *rdata++;
+			l = *rdata++;
+			for (j = 0; j < l; j++) {
+				if (rdata[j] == 0)
+					continue;
+				for (k = 0; k < 8; k++) {
+					if ((rdata[j] & (0x80 >> k)) == 0)
+						continue;
+					c = w * 256 + j * 8 + k;
+					len = SPRINTF((tmp, " %s", p_type((ns_type)c)));
+					T(addstr(tmp, (size_t)len, &buf, &buflen));
+				}
+			}
+			rdata += l;
+		}
+		break;
+	    }
+
+	case ns_t_dhcid: {
+		int n;
+		unsigned int siz;
+		char base64_dhcid[8192];
+		const char *leader;
+
+		siz = (int)(edata-rdata)*4/3 + 4; /* "+4" accounts for trailing \0 */
+		if (siz > sizeof(base64_dhcid) * 3/4) {
+			const char *str = "record too long to print";
+			T(addstr(str, strlen(str), &buf, &buflen));
+		} else {
+			len = b64_ntop(rdata, (size_t)(edata-rdata),
+			    base64_dhcid, siz);
+
+			if (len < 0)
+				goto formerr;
+
+			else if (len > 15) {
+				T(addstr(" (", 2, &buf, &buflen));
+				leader = "\n\t\t";
+				spaced = 0;
+			}
+			else
+				leader = " ";
+
+			for (n = 0; n < len; n += 48) {
+				T(addstr(leader, strlen(leader),
+					 &buf, &buflen));
+				T(addstr(base64_dhcid + n,
+				    (size_t)MIN(len - n, 48), &buf, &buflen));
+			}
+			if (len > 15)
+				T(addstr(" )", 2, &buf, &buflen));
+		}
+		break;
+	}
+
+	case ns_t_ipseckey: {
+		int n;
+		unsigned int siz;
+		char base64_key[8192];
+		const char *leader;
+
+		if (rdlen < 2)
+			goto formerr;
+
+		switch (rdata[1]) {
+		case 0:
+		case 3:
+			if (rdlen < 3)
+				goto formerr;
+			break;
+		case 1:
+			if (rdlen < 7)
+				goto formerr;
+			break;
+		case 2:
+			if (rdlen < 19)
+				goto formerr;
+			break;
+		default:
+			comment = "unknown IPSECKEY gateway type";
+			goto hexify;
+		}
+
+		len = SPRINTF((tmp, "%u ", *rdata));
+		T(addstr(tmp, (size_t)len, &buf, &buflen));
+		rdata++;
+
+		len = SPRINTF((tmp, "%u ", *rdata));
+		T(addstr(tmp, (size_t)len, &buf, &buflen));
+		rdata++;
+
+		len = SPRINTF((tmp, "%u ", *rdata));
+		T(addstr(tmp, (size_t)len, &buf, &buflen));
+		rdata++;
+
+		switch (rdata[-2]) {
+		case 0:
+			T(addstr(".", 1, &buf, &buflen));
+			break;
+		case 1:
+			(void) inet_ntop(AF_INET, rdata, buf, (socklen_t)buflen);
+			addlen(strlen(buf), &buf, &buflen);
+			rdata += 4;
+			break;
+		case 2:
+			(void) inet_ntop(AF_INET6, rdata, buf, (socklen_t)buflen);
+			addlen(strlen(buf), &buf, &buflen);
+			rdata += 16;
+			break;
+		case 3:
+			T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
+			break;
+		}
+
+		if (rdata >= edata)
+			break;
+
+		siz = (int)(edata-rdata)*4/3 + 4; /* "+4" accounts for trailing \0 */
+		if (siz > sizeof(base64_key) * 3/4) {
+			const char *str = "record too long to print";
+			T(addstr(str, strlen(str), &buf, &buflen));
+		} else {
+			len = b64_ntop(rdata, (size_t)(edata-rdata),
+			    base64_key, siz);
+
+			if (len < 0)
+				goto formerr;
+
+			else if (len > 15) {
+				T(addstr(" (", 2, &buf, &buflen));
+				leader = "\n\t\t";
+				spaced = 0;
+			}
+			else
+				leader = " ";
+
+			for (n = 0; n < len; n += 48) {
+				T(addstr(leader, strlen(leader),
+					 &buf, &buflen));
+				T(addstr(base64_key + n,
+				    (size_t)MIN(len - n, 48), &buf, &buflen));
+			}
+			if (len > 15)
+				T(addstr(" )", 2, &buf, &buflen));
+		}
+		break;
+	}
+
+	case ns_t_hip: {
+		unsigned int i, hip_len, algorithm, key_len;
+		char base64_key[NS_MD5RSA_MAX_BASE64];
+		unsigned int siz;
+		const char *leader = "\n\t\t\t\t\t";
+
+		hip_len = *rdata++;
+		algorithm = *rdata++;
+		key_len = ns_get16(rdata);
+		rdata += NS_INT16SZ;
+
+		siz = key_len*4/3 + 4; /* "+4" accounts for trailing \0 */
+		if (siz > sizeof(base64_key) * 3/4) {
+			const char *str = "record too long to print";
+			T(addstr(str, strlen(str), &buf, &buflen));
+		} else {
+			len = sprintf(tmp, "( %u ", algorithm);
+			T(addstr(tmp, (size_t)len, &buf, &buflen));
+
+			for (i = 0; i < hip_len; i++) {
+				len = sprintf(tmp, "%02X", *rdata);
+				T(addstr(tmp, (size_t)len, &buf, &buflen));
+				rdata++;
+			}
+			T(addstr(leader, strlen(leader), &buf, &buflen));
+
+			len = b64_ntop(rdata, key_len, base64_key, siz);
+			if (len < 0)
+				goto formerr;
+
+			T(addstr(base64_key, (size_t)len, &buf, &buflen));
+
+			rdata += key_len;
+			while (rdata < edata) {
+				T(addstr(leader, strlen(leader), &buf, &buflen));
+				T(addname(msg, msglen, &rdata, origin,
+					  &buf, &buflen));
+			}
+			T(addstr(" )", 2, &buf, &buflen));
+		}
+		break;
+	    }
+
 	default:
 		comment = "unknown RR type";
 		goto hexify;
 	}
-	return (buf - obuf);
+	_DIAGASSERT(__type_fit(int, buf - obuf));
+	return (int)(buf - obuf);
  formerr:
 	comment = "RR format error";
  hexify: {
 	int n, m;
 	char *p;
 
-	len = SPRINTF((tmp, "\\# %tu%s\t; %s", edata - rdata,
-		       rdlen != 0 ? " (" : "", comment));
+	len = SPRINTF((tmp, "\\# %u%s\t; %s", (unsigned)(edata - rdata),
+		       rdlen != 0U ? " (" : "", comment));
 	T(addstr(tmp, (size_t)len, &buf, &buflen));
 	while (rdata < edata) {
 		p = tmp;
 		p += SPRINTF((p, "\n\t"));
 		spaced = 0;
-		n = MIN(16, edata - rdata);
+		n = MIN(16, (int)(edata - rdata));
 		for (m = 0; m < n; m++)
 			p += SPRINTF((p, "%02x ", rdata[m]));
 		T(addstr(tmp, (size_t)(p - tmp), &buf, &buflen));
@@ -746,7 +1093,8 @@
 		T(addstr(tmp, (size_t)(p - tmp), &buf, &buflen));
 		rdata += n;
 	}
-	return (buf - obuf);
+	_DIAGASSERT(__type_fit(int, buf - obuf));
+	return (int)(buf - obuf);
     }
 }
 
@@ -822,7 +1170,8 @@
 	}
 	if (addstr("\"", (size_t)1, buf, buflen) < 0)
 		goto enospc;
-	return (rdata - odata);
+	_DIAGASSERT(__type_fit(int, rdata - odata));
+	return (int)(rdata - odata);
  enospc:
 	errno = ENOSPC;
 	*buf = save_buf;
@@ -866,7 +1215,8 @@
 	*pp += n;
 	addlen(newlen, buf, buflen);
 	**buf = '\0';
-	return (newlen);
+	_DIAGASSERT(__type_fit(int, newlen));
+	return (int)newlen;
  enospc:
 	errno = ENOSPC;
 	*buf = save_buf;
@@ -897,7 +1247,7 @@
 addtab(size_t len, size_t target, int spaced, char **buf, size_t *buflen) {
 	size_t save_buflen = *buflen;
 	char *save_buf = *buf;
-	int t;
+	ptrdiff_t t;
 
 	if (spaced || len >= target - 1) {
 		T(addstr("  ", (size_t)2, buf, buflen));
diff --git a/libc/dns/nameser/ns_samedomain.c b/libc/dns/nameser/ns_samedomain.c
index d35dfe1..0be0c28 100644
--- a/libc/dns/nameser/ns_samedomain.c
+++ b/libc/dns/nameser/ns_samedomain.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: ns_samedomain.c,v 1.2 2004/05/20 20:35:05 christos Exp $	*/
+/*	$NetBSD: ns_samedomain.c,v 1.8 2012/11/22 20:22:31 christos Exp $	*/
 
 /*
  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
@@ -20,9 +20,9 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #ifdef notdef
-static const char rcsid[] = "Id: ns_samedomain.c,v 1.1.2.2.4.2 2004/03/16 12:34:17 marka Exp";
+static const char rcsid[] = "Id: ns_samedomain.c,v 1.6 2005/04/27 04:56:40 sra Exp";
 #else
-__RCSID("$NetBSD: ns_samedomain.c,v 1.2 2004/05/20 20:35:05 christos Exp $");
+__RCSID("$NetBSD: ns_samedomain.c,v 1.8 2012/11/22 20:22:31 christos Exp $");
 #endif
 #endif
 
@@ -31,16 +31,17 @@
 #include <errno.h>
 #include <string.h>
 
-#ifndef _LIBC
+#ifdef _LIBRESOLV
 /*
- * int
- * ns_samedomain(a, b)
  *	Check whether a name belongs to a domain.
+ *
  * Inputs:
  *	a - the domain whose ancestory is being verified
  *	b - the potential ancestor we're checking against
+ *
  * Return:
  *	boolean - is a at or below b?
+ *
  * Notes:
  *	Trailing dots are first removed from name and domain.
  *	Always compare complete subdomains, not only whether the
@@ -52,8 +53,8 @@
 
 int
 ns_samedomain(const char *a, const char *b) {
-	size_t la, lb;
-	int diff, i, escaped;
+	size_t la, lb, i;
+	int diff, escaped;
 	const char *cp;
 
 	la = strlen(a);
@@ -63,8 +64,8 @@
 	if (la != 0U && a[la - 1] == '.') {
 		escaped = 0;
 		/* Note this loop doesn't get executed if la==1. */
-		for (i = la - 2; i >= 0; i--)
-			if (a[i] == '\\') {
+		for (i = la - 1; i > 0; i--)
+			if (a[i - 1] == '\\') {
 				if (escaped)
 					escaped = 0;
 				else
@@ -79,8 +80,8 @@
 	if (lb != 0U && b[lb - 1] == '.') {
 		escaped = 0;
 		/* note this loop doesn't get executed if lb==1 */
-		for (i = lb - 2; i >= 0; i--)
-			if (b[i] == '\\') {
+		for (i = lb - 1; i > 0; i--)
+			if (b[i - 1] == '\\') {
 				if (escaped)
 					escaped = 0;
 				else
@@ -105,7 +106,7 @@
 
 	/* Ok, we know la > lb. */
 
-	diff = la - lb;
+	diff = (int)(la - lb);
 
 	/*
 	 * If 'a' is only 1 character longer than 'b', then it can't be
@@ -128,8 +129,8 @@
          * and thus not a really a label separator.
 	 */
 	escaped = 0;
-	for (i = diff - 2; i >= 0; i--)
-		if (a[i] == '\\') {
+	for (i = diff - 1; i > 0; i--)
+		if (a[i - 1] == '\\') {
 			if (escaped)
 				escaped = 0;
 			else
@@ -145,8 +146,6 @@
 }
 
 /*
- * int
- * ns_subdomain(a, b)
  *	is "a" a subdomain of "b"?
  */
 int
@@ -155,10 +154,10 @@
 }
 #endif
 
+#ifdef _LIBC
 /*
- * int
- * ns_makecanon(src, dst, dstsize)
  *	make a canonical copy of domain name "src"
+ *
  * notes:
  *	foo -> foo.
  *	foo. -> foo.
@@ -188,9 +187,8 @@
 }
 
 /*
- * int
- * ns_samename(a, b)
  *	determine whether domain name "a" is the same as domain name "b"
+ *
  * return:
  *	-1 on error
  *	0 if names differ
@@ -209,3 +207,4 @@
 	else
 		return (0);
 }
+#endif
diff --git a/libc/dns/nameser/ns_ttl.c b/libc/dns/nameser/ns_ttl.c
index 1ad3b49..2395b99 100644
--- a/libc/dns/nameser/ns_ttl.c
+++ b/libc/dns/nameser/ns_ttl.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: ns_ttl.c,v 1.2 2004/05/20 20:35:05 christos Exp $	*/
+/*	$NetBSD: ns_ttl.c,v 1.8 2012/03/13 21:13:39 christos Exp $	*/
 
 /*
  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
@@ -20,9 +20,9 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #ifdef notdef
-static const char rcsid[] = "Id: ns_ttl.c,v 1.1.206.1 2004/03/09 08:33:45 marka Exp";
+static const char rcsid[] = "Id: ns_ttl.c,v 1.4 2005/07/28 06:51:49 marka Exp";
 #else
-__RCSID("$NetBSD: ns_ttl.c,v 1.2 2004/05/20 20:35:05 christos Exp $");
+__RCSID("$NetBSD: ns_ttl.c,v 1.8 2012/03/13 21:13:39 christos Exp $");
 #endif
 #endif
 
@@ -30,6 +30,7 @@
 
 #include <arpa/nameser.h>
 
+#include <assert.h>
 #include <ctype.h>
 #include <errno.h>
 #include <stdio.h>
@@ -57,11 +58,11 @@
 	int secs, mins, hours, days, weeks, x;
 	char *p;
 
-	secs = src % 60;   src /= 60;
-	mins = src % 60;   src /= 60;
-	hours = src % 24;  src /= 24;
-	days = src % 7;    src /= 7;
-	weeks = src;       src = 0;
+	secs = (int)(src % 60);   src /= 60;
+	mins = (int)(src % 60);   src /= 60;
+	hours = (int)(src % 24);  src /= 24;
+	days = (int)(src % 7);    src /= 7;
+	weeks = (int)src;       src = 0;
 
 	x = 0;
 	if (weeks) {
@@ -93,7 +94,8 @@
 				*p = tolower(ch);
 	}
 
-	return (dst - odst);
+	_DIAGASSERT(__type_fit(int, dst - odst));
+	return (int)(dst - odst);
 }
 
 #ifndef _LIBC
@@ -137,7 +139,8 @@
 			goto einval;
 		else
 			ttl += tmp;
-	}
+	} else if (!dirty)
+		goto einval;
 	*dst = ttl;
 	return (0);