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