Home | History | Annotate | Line # | Download | only in nameser
ns_name.c revision 1.3.10.1
      1  1.3.10.1       jdc /*	$NetBSD: ns_name.c,v 1.3.10.1 2007/05/17 00:43:50 jdc Exp $	*/
      2       1.1  christos 
      3       1.1  christos /*
      4       1.1  christos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5       1.1  christos  * Copyright (c) 1996,1999 by Internet Software Consortium.
      6       1.1  christos  *
      7       1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      8       1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      9       1.1  christos  * copyright notice and this permission notice appear in all copies.
     10       1.1  christos  *
     11       1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     12       1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13       1.1  christos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14       1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15       1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16       1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17       1.1  christos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18       1.1  christos  */
     19       1.1  christos 
     20       1.2  christos #include <sys/cdefs.h>
     21       1.1  christos #ifndef lint
     22       1.2  christos #ifdef notdef
     23  1.3.10.1       jdc static const char rcsid[] = "Id: ns_name.c,v 1.8.18.2 2005/04/27 05:01:08 sra Exp";
     24       1.2  christos #else
     25  1.3.10.1       jdc __RCSID("$NetBSD: ns_name.c,v 1.3.10.1 2007/05/17 00:43:50 jdc Exp $");
     26       1.2  christos #endif
     27       1.1  christos #endif
     28       1.1  christos 
     29       1.1  christos #include "port_before.h"
     30       1.1  christos 
     31       1.1  christos #include <sys/types.h>
     32       1.1  christos 
     33       1.1  christos #include <netinet/in.h>
     34       1.1  christos #include <arpa/nameser.h>
     35       1.1  christos 
     36       1.1  christos #include <errno.h>
     37       1.1  christos #include <resolv.h>
     38       1.1  christos #include <string.h>
     39       1.1  christos #include <ctype.h>
     40       1.1  christos #include <stdlib.h>
     41       1.1  christos #include <limits.h>
     42       1.1  christos 
     43       1.1  christos #include "port_after.h"
     44       1.1  christos 
     45       1.1  christos #ifdef SPRINTF_CHAR
     46       1.1  christos # define SPRINTF(x) strlen(sprintf/**/x)
     47       1.1  christos #else
     48       1.1  christos # define SPRINTF(x) ((size_t)sprintf x)
     49       1.1  christos #endif
     50       1.1  christos 
     51  1.3.10.1       jdc #define NS_TYPE_ELT			0x40 /*%< EDNS0 extended label type */
     52       1.1  christos #define DNS_LABELTYPE_BITSTRING		0x41
     53       1.1  christos 
     54       1.1  christos /* Data. */
     55       1.1  christos 
     56       1.1  christos static const char	digits[] = "0123456789";
     57       1.1  christos 
     58       1.1  christos static const char digitvalue[256] = {
     59       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,	/*16*/
     60       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
     61       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
     62       1.1  christos 	 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1, /*64*/
     63       1.1  christos 	-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
     64       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
     65       1.1  christos 	-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
     66       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
     67       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     68       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     69       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     70       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     71       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     72       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     73       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     74       1.1  christos 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/
     75       1.1  christos };
     76       1.1  christos 
     77       1.1  christos /* Forward. */
     78       1.1  christos 
     79       1.1  christos static int		special(int);
     80       1.1  christos static int		printable(int);
     81       1.1  christos static int		dn_find(const u_char *, const u_char *,
     82       1.1  christos 				const u_char * const *,
     83       1.1  christos 				const u_char * const *);
     84       1.1  christos static int		encode_bitsring(const char **, const char *,
     85       1.3  christos 					unsigned char **, unsigned char **,
     86       1.3  christos 					unsigned const char *);
     87       1.1  christos static int		labellen(const u_char *);
     88       1.3  christos static int		decode_bitstring(const unsigned char **,
     89       1.3  christos 					 char *, const char *);
     90       1.1  christos 
     91       1.1  christos /* Public. */
     92       1.1  christos 
     93  1.3.10.1       jdc /*%
     94       1.1  christos  *	Convert an encoded domain name to printable ascii as per RFC1035.
     95  1.3.10.1       jdc 
     96       1.1  christos  * return:
     97  1.3.10.1       jdc  *\li	Number of bytes written to buffer, or -1 (with errno set)
     98  1.3.10.1       jdc  *
     99       1.1  christos  * notes:
    100  1.3.10.1       jdc  *\li	The root is returned as "."
    101  1.3.10.1       jdc  *\li	All other domains are returned in non absolute form
    102       1.1  christos  */
    103       1.1  christos int
    104       1.1  christos ns_name_ntop(const u_char *src, char *dst, size_t dstsiz)
    105       1.1  christos {
    106       1.1  christos 	const u_char *cp;
    107       1.1  christos 	char *dn, *eom;
    108       1.1  christos 	u_char c;
    109       1.1  christos 	u_int n;
    110       1.1  christos 	int l;
    111       1.1  christos 
    112       1.1  christos 	cp = src;
    113       1.1  christos 	dn = dst;
    114       1.1  christos 	eom = dst + dstsiz;
    115       1.1  christos 
    116       1.1  christos 	while ((n = *cp++) != 0) {
    117       1.1  christos 		if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
    118       1.1  christos 			/* Some kind of compression pointer. */
    119       1.1  christos 			errno = EMSGSIZE;
    120       1.1  christos 			return (-1);
    121       1.1  christos 		}
    122       1.1  christos 		if (dn != dst) {
    123       1.1  christos 			if (dn >= eom) {
    124       1.1  christos 				errno = EMSGSIZE;
    125       1.1  christos 				return (-1);
    126       1.1  christos 			}
    127       1.1  christos 			*dn++ = '.';
    128       1.1  christos 		}
    129       1.1  christos 		if ((l = labellen(cp - 1)) < 0) {
    130  1.3.10.1       jdc 			errno = EMSGSIZE; /*%< XXX */
    131       1.1  christos 			return(-1);
    132       1.1  christos 		}
    133       1.1  christos 		if (dn + l >= eom) {
    134       1.1  christos 			errno = EMSGSIZE;
    135       1.1  christos 			return (-1);
    136       1.1  christos 		}
    137       1.1  christos 		if ((n & NS_CMPRSFLGS) == NS_TYPE_ELT) {
    138       1.1  christos 			int m;
    139       1.1  christos 
    140       1.1  christos 			if (n != DNS_LABELTYPE_BITSTRING) {
    141       1.1  christos 				/* XXX: labellen should reject this case */
    142       1.1  christos 				errno = EINVAL;
    143       1.1  christos 				return(-1);
    144       1.1  christos 			}
    145       1.3  christos 			if ((m = decode_bitstring(&cp, dn, eom)) < 0)
    146       1.1  christos 			{
    147       1.1  christos 				errno = EMSGSIZE;
    148       1.1  christos 				return(-1);
    149       1.1  christos 			}
    150       1.1  christos 			dn += m;
    151       1.1  christos 			continue;
    152       1.1  christos 		}
    153       1.2  christos 		for (; l > 0; l--) {
    154       1.1  christos 			c = *cp++;
    155       1.1  christos 			if (special(c)) {
    156       1.1  christos 				if (dn + 1 >= eom) {
    157       1.1  christos 					errno = EMSGSIZE;
    158       1.1  christos 					return (-1);
    159       1.1  christos 				}
    160       1.1  christos 				*dn++ = '\\';
    161       1.1  christos 				*dn++ = (char)c;
    162       1.1  christos 			} else if (!printable(c)) {
    163       1.1  christos 				if (dn + 3 >= eom) {
    164       1.1  christos 					errno = EMSGSIZE;
    165       1.1  christos 					return (-1);
    166       1.1  christos 				}
    167       1.1  christos 				*dn++ = '\\';
    168       1.1  christos 				*dn++ = digits[c / 100];
    169       1.1  christos 				*dn++ = digits[(c % 100) / 10];
    170       1.1  christos 				*dn++ = digits[c % 10];
    171       1.1  christos 			} else {
    172       1.1  christos 				if (dn >= eom) {
    173       1.1  christos 					errno = EMSGSIZE;
    174       1.1  christos 					return (-1);
    175       1.1  christos 				}
    176       1.1  christos 				*dn++ = (char)c;
    177       1.1  christos 			}
    178       1.1  christos 		}
    179       1.1  christos 	}
    180       1.1  christos 	if (dn == dst) {
    181       1.1  christos 		if (dn >= eom) {
    182       1.1  christos 			errno = EMSGSIZE;
    183       1.1  christos 			return (-1);
    184       1.1  christos 		}
    185       1.1  christos 		*dn++ = '.';
    186       1.1  christos 	}
    187       1.1  christos 	if (dn >= eom) {
    188       1.1  christos 		errno = EMSGSIZE;
    189       1.1  christos 		return (-1);
    190       1.1  christos 	}
    191       1.1  christos 	*dn++ = '\0';
    192       1.1  christos 	return (dn - dst);
    193       1.1  christos }
    194       1.1  christos 
    195  1.3.10.1       jdc /*%
    196       1.1  christos  *	Convert a ascii string into an encoded domain name as per RFC1035.
    197  1.3.10.1       jdc  *
    198       1.1  christos  * return:
    199  1.3.10.1       jdc  *
    200  1.3.10.1       jdc  *\li	-1 if it fails
    201  1.3.10.1       jdc  *\li	1 if string was fully qualified
    202  1.3.10.1       jdc  *\li	0 is string was not fully qualified
    203  1.3.10.1       jdc  *
    204       1.1  christos  * notes:
    205  1.3.10.1       jdc  *\li	Enforces label and domain length limits.
    206       1.1  christos  */
    207       1.1  christos 
    208       1.1  christos int
    209       1.1  christos ns_name_pton(const char *src, u_char *dst, size_t dstsiz)
    210       1.1  christos {
    211       1.1  christos 	u_char *label, *bp, *eom;
    212       1.1  christos 	int c, n, escaped, e = 0;
    213       1.1  christos 	char *cp;
    214       1.1  christos 
    215       1.1  christos 	escaped = 0;
    216       1.1  christos 	bp = dst;
    217       1.1  christos 	eom = dst + dstsiz;
    218       1.1  christos 	label = bp++;
    219       1.1  christos 
    220       1.1  christos 	while ((c = *src++) != 0) {
    221       1.1  christos 		if (escaped) {
    222  1.3.10.1       jdc 			if (c == '[') { /*%< start a bit string label */
    223       1.1  christos 				if ((cp = strchr(src, ']')) == NULL) {
    224  1.3.10.1       jdc 					errno = EINVAL; /*%< ??? */
    225       1.1  christos 					return(-1);
    226       1.1  christos 				}
    227       1.3  christos 				if ((e = encode_bitsring(&src, cp + 2,
    228       1.3  christos 							 &label, &bp, eom))
    229       1.1  christos 				    != 0) {
    230       1.1  christos 					errno = e;
    231       1.1  christos 					return(-1);
    232       1.1  christos 				}
    233       1.1  christos 				escaped = 0;
    234       1.1  christos 				label = bp++;
    235       1.1  christos 				if ((c = *src++) == 0)
    236       1.1  christos 					goto done;
    237       1.1  christos 				else if (c != '.') {
    238       1.1  christos 					errno = EINVAL;
    239       1.1  christos 					return(-1);
    240       1.1  christos 				}
    241       1.1  christos 				continue;
    242       1.1  christos 			}
    243       1.1  christos 			else if ((cp = strchr(digits, c)) != NULL) {
    244       1.1  christos 				n = (cp - digits) * 100;
    245       1.1  christos 				if ((c = *src++) == 0 ||
    246       1.1  christos 				    (cp = strchr(digits, c)) == NULL) {
    247       1.1  christos 					errno = EMSGSIZE;
    248       1.1  christos 					return (-1);
    249       1.1  christos 				}
    250       1.1  christos 				n += (cp - digits) * 10;
    251       1.1  christos 				if ((c = *src++) == 0 ||
    252       1.1  christos 				    (cp = strchr(digits, c)) == NULL) {
    253       1.1  christos 					errno = EMSGSIZE;
    254       1.1  christos 					return (-1);
    255       1.1  christos 				}
    256       1.1  christos 				n += (cp - digits);
    257       1.1  christos 				if (n > 255) {
    258       1.1  christos 					errno = EMSGSIZE;
    259       1.1  christos 					return (-1);
    260       1.1  christos 				}
    261       1.1  christos 				c = n;
    262       1.1  christos 			}
    263       1.1  christos 			escaped = 0;
    264       1.1  christos 		} else if (c == '\\') {
    265       1.1  christos 			escaped = 1;
    266       1.1  christos 			continue;
    267       1.1  christos 		} else if (c == '.') {
    268       1.1  christos 			c = (bp - label - 1);
    269  1.3.10.1       jdc 			if ((c & NS_CMPRSFLGS) != 0) {	/*%< Label too big. */
    270       1.1  christos 				errno = EMSGSIZE;
    271       1.1  christos 				return (-1);
    272       1.1  christos 			}
    273       1.1  christos 			if (label >= eom) {
    274       1.1  christos 				errno = EMSGSIZE;
    275       1.1  christos 				return (-1);
    276       1.1  christos 			}
    277       1.1  christos 			*label = c;
    278       1.1  christos 			/* Fully qualified ? */
    279       1.1  christos 			if (*src == '\0') {
    280       1.1  christos 				if (c != 0) {
    281       1.1  christos 					if (bp >= eom) {
    282       1.1  christos 						errno = EMSGSIZE;
    283       1.1  christos 						return (-1);
    284       1.1  christos 					}
    285       1.1  christos 					*bp++ = '\0';
    286       1.1  christos 				}
    287       1.1  christos 				if ((bp - dst) > MAXCDNAME) {
    288       1.1  christos 					errno = EMSGSIZE;
    289       1.1  christos 					return (-1);
    290       1.1  christos 				}
    291       1.1  christos 				return (1);
    292       1.1  christos 			}
    293       1.1  christos 			if (c == 0 || *src == '.') {
    294       1.1  christos 				errno = EMSGSIZE;
    295       1.1  christos 				return (-1);
    296       1.1  christos 			}
    297       1.1  christos 			label = bp++;
    298       1.1  christos 			continue;
    299       1.1  christos 		}
    300       1.1  christos 		if (bp >= eom) {
    301       1.1  christos 			errno = EMSGSIZE;
    302       1.1  christos 			return (-1);
    303       1.1  christos 		}
    304       1.1  christos 		*bp++ = (u_char)c;
    305       1.1  christos 	}
    306       1.1  christos 	c = (bp - label - 1);
    307  1.3.10.1       jdc 	if ((c & NS_CMPRSFLGS) != 0) {		/*%< Label too big. */
    308       1.1  christos 		errno = EMSGSIZE;
    309       1.1  christos 		return (-1);
    310       1.1  christos 	}
    311       1.1  christos   done:
    312       1.1  christos 	if (label >= eom) {
    313       1.1  christos 		errno = EMSGSIZE;
    314       1.1  christos 		return (-1);
    315       1.1  christos 	}
    316       1.1  christos 	*label = c;
    317       1.1  christos 	if (c != 0) {
    318       1.1  christos 		if (bp >= eom) {
    319       1.1  christos 			errno = EMSGSIZE;
    320       1.1  christos 			return (-1);
    321       1.1  christos 		}
    322       1.1  christos 		*bp++ = 0;
    323       1.1  christos 	}
    324  1.3.10.1       jdc 	if ((bp - dst) > MAXCDNAME) {	/*%< src too big */
    325       1.1  christos 		errno = EMSGSIZE;
    326       1.1  christos 		return (-1);
    327       1.1  christos 	}
    328       1.1  christos 	return (0);
    329       1.1  christos }
    330       1.1  christos 
    331  1.3.10.1       jdc /*%
    332       1.1  christos  *	Convert a network strings labels into all lowercase.
    333  1.3.10.1       jdc  *
    334       1.1  christos  * return:
    335  1.3.10.1       jdc  *\li	Number of bytes written to buffer, or -1 (with errno set)
    336  1.3.10.1       jdc  *
    337       1.1  christos  * notes:
    338  1.3.10.1       jdc  *\li	Enforces label and domain length limits.
    339       1.1  christos  */
    340       1.1  christos 
    341       1.1  christos int
    342       1.1  christos ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz)
    343       1.1  christos {
    344       1.1  christos 	const u_char *cp;
    345       1.1  christos 	u_char *dn, *eom;
    346       1.1  christos 	u_char c;
    347       1.1  christos 	u_int n;
    348       1.1  christos 	int l;
    349       1.1  christos 
    350       1.1  christos 	cp = src;
    351       1.1  christos 	dn = dst;
    352       1.1  christos 	eom = dst + dstsiz;
    353       1.1  christos 
    354       1.1  christos 	if (dn >= eom) {
    355       1.1  christos 		errno = EMSGSIZE;
    356       1.1  christos 		return (-1);
    357       1.1  christos 	}
    358       1.1  christos 	while ((n = *cp++) != 0) {
    359       1.1  christos 		if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
    360       1.1  christos 			/* Some kind of compression pointer. */
    361       1.1  christos 			errno = EMSGSIZE;
    362       1.1  christos 			return (-1);
    363       1.1  christos 		}
    364       1.1  christos 		*dn++ = n;
    365       1.1  christos 		if ((l = labellen(cp - 1)) < 0) {
    366       1.1  christos 			errno = EMSGSIZE;
    367       1.1  christos 			return (-1);
    368       1.1  christos 		}
    369       1.1  christos 		if (dn + l >= eom) {
    370       1.1  christos 			errno = EMSGSIZE;
    371       1.1  christos 			return (-1);
    372       1.1  christos 		}
    373       1.2  christos 		for (; l > 0; l--) {
    374       1.1  christos 			c = *cp++;
    375       1.1  christos 			if (isupper(c))
    376       1.1  christos 				*dn++ = tolower(c);
    377       1.1  christos 			else
    378       1.1  christos 				*dn++ = c;
    379       1.1  christos 		}
    380       1.1  christos 	}
    381       1.1  christos 	*dn++ = '\0';
    382       1.1  christos 	return (dn - dst);
    383       1.1  christos }
    384       1.1  christos 
    385  1.3.10.1       jdc /*%
    386       1.1  christos  *	Unpack a domain name from a message, source may be compressed.
    387  1.3.10.1       jdc  *
    388       1.1  christos  * return:
    389  1.3.10.1       jdc  *\li	-1 if it fails, or consumed octets if it succeeds.
    390       1.1  christos  */
    391       1.1  christos int
    392       1.1  christos ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
    393       1.1  christos 	       u_char *dst, size_t dstsiz)
    394       1.1  christos {
    395       1.1  christos 	const u_char *srcp, *dstlim;
    396       1.1  christos 	u_char *dstp;
    397       1.1  christos 	int n, len, checked, l;
    398       1.1  christos 
    399       1.1  christos 	len = -1;
    400       1.1  christos 	checked = 0;
    401       1.1  christos 	dstp = dst;
    402       1.1  christos 	srcp = src;
    403       1.1  christos 	dstlim = dst + dstsiz;
    404       1.1  christos 	if (srcp < msg || srcp >= eom) {
    405       1.1  christos 		errno = EMSGSIZE;
    406       1.1  christos 		return (-1);
    407       1.1  christos 	}
    408       1.1  christos 	/* Fetch next label in domain name. */
    409       1.1  christos 	while ((n = *srcp++) != 0) {
    410       1.1  christos 		/* Check for indirection. */
    411       1.1  christos 		switch (n & NS_CMPRSFLGS) {
    412       1.1  christos 		case 0:
    413       1.1  christos 		case NS_TYPE_ELT:
    414       1.1  christos 			/* Limit checks. */
    415       1.1  christos 			if ((l = labellen(srcp - 1)) < 0) {
    416       1.1  christos 				errno = EMSGSIZE;
    417       1.1  christos 				return(-1);
    418       1.1  christos 			}
    419       1.1  christos 			if (dstp + l + 1 >= dstlim || srcp + l >= eom) {
    420       1.1  christos 				errno = EMSGSIZE;
    421       1.1  christos 				return (-1);
    422       1.1  christos 			}
    423       1.1  christos 			checked += l + 1;
    424       1.1  christos 			*dstp++ = n;
    425       1.2  christos 			memcpy(dstp, srcp, (size_t)l);
    426       1.1  christos 			dstp += l;
    427       1.1  christos 			srcp += l;
    428       1.1  christos 			break;
    429       1.1  christos 
    430       1.1  christos 		case NS_CMPRSFLGS:
    431       1.1  christos 			if (srcp >= eom) {
    432       1.1  christos 				errno = EMSGSIZE;
    433       1.1  christos 				return (-1);
    434       1.1  christos 			}
    435       1.1  christos 			if (len < 0)
    436       1.1  christos 				len = srcp - src + 1;
    437       1.1  christos 			srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
    438  1.3.10.1       jdc 			if (srcp < msg || srcp >= eom) {  /*%< Out of range. */
    439       1.1  christos 				errno = EMSGSIZE;
    440       1.1  christos 				return (-1);
    441       1.1  christos 			}
    442       1.1  christos 			checked += 2;
    443       1.1  christos 			/*
    444       1.1  christos 			 * Check for loops in the compressed name;
    445       1.1  christos 			 * if we've looked at the whole message,
    446       1.1  christos 			 * there must be a loop.
    447       1.1  christos 			 */
    448       1.1  christos 			if (checked >= eom - msg) {
    449       1.1  christos 				errno = EMSGSIZE;
    450       1.1  christos 				return (-1);
    451       1.1  christos 			}
    452       1.1  christos 			break;
    453       1.1  christos 
    454       1.1  christos 		default:
    455       1.1  christos 			errno = EMSGSIZE;
    456  1.3.10.1       jdc 			return (-1);			/*%< flag error */
    457       1.1  christos 		}
    458       1.1  christos 	}
    459       1.1  christos 	*dstp = '\0';
    460       1.1  christos 	if (len < 0)
    461       1.1  christos 		len = srcp - src;
    462       1.1  christos 	return (len);
    463       1.1  christos }
    464       1.1  christos 
    465  1.3.10.1       jdc /*%
    466       1.1  christos  *	Pack domain name 'domain' into 'comp_dn'.
    467  1.3.10.1       jdc  *
    468       1.1  christos  * return:
    469  1.3.10.1       jdc  *\li	Size of the compressed name, or -1.
    470  1.3.10.1       jdc  *
    471       1.1  christos  * notes:
    472  1.3.10.1       jdc  *\li	'dnptrs' is an array of pointers to previous compressed names.
    473  1.3.10.1       jdc  *\li	dnptrs[0] is a pointer to the beginning of the message. The array
    474       1.1  christos  *	ends with NULL.
    475  1.3.10.1       jdc  *\li	'lastdnptr' is a pointer to the end of the array pointed to
    476       1.1  christos  *	by 'dnptrs'.
    477  1.3.10.1       jdc  *
    478       1.1  christos  * Side effects:
    479  1.3.10.1       jdc  *\li	The list of pointers in dnptrs is updated for labels inserted into
    480       1.1  christos  *	the message as we compress the name.  If 'dnptr' is NULL, we don't
    481       1.1  christos  *	try to compress names. If 'lastdnptr' is NULL, we don't update the
    482       1.1  christos  *	list.
    483       1.1  christos  */
    484       1.1  christos int
    485       1.1  christos ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
    486       1.1  christos 	     const u_char **dnptrs, const u_char **lastdnptr)
    487       1.1  christos {
    488       1.1  christos 	u_char *dstp;
    489       1.1  christos 	const u_char **cpp, **lpp, *eob, *msg;
    490       1.1  christos 	const u_char *srcp;
    491       1.1  christos 	int n, l, first = 1;
    492       1.1  christos 
    493       1.1  christos 	srcp = src;
    494       1.1  christos 	dstp = dst;
    495       1.1  christos 	eob = dstp + dstsiz;
    496       1.1  christos 	lpp = cpp = NULL;
    497       1.1  christos 	if (dnptrs != NULL) {
    498       1.1  christos 		if ((msg = *dnptrs++) != NULL) {
    499       1.1  christos 			for (cpp = dnptrs; *cpp != NULL; cpp++)
    500  1.3.10.1       jdc 				continue;
    501  1.3.10.1       jdc 			lpp = cpp;	/*%< end of list to search */
    502       1.1  christos 		}
    503       1.1  christos 	} else
    504       1.1  christos 		msg = NULL;
    505       1.1  christos 
    506       1.1  christos 	/* make sure the domain we are about to add is legal */
    507       1.1  christos 	l = 0;
    508       1.1  christos 	do {
    509       1.1  christos 		int l0;
    510       1.1  christos 
    511       1.1  christos 		n = *srcp;
    512       1.1  christos 		if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
    513       1.1  christos 			errno = EMSGSIZE;
    514       1.1  christos 			return (-1);
    515       1.1  christos 		}
    516       1.1  christos 		if ((l0 = labellen(srcp)) < 0) {
    517       1.1  christos 			errno = EINVAL;
    518       1.1  christos 			return(-1);
    519       1.1  christos 		}
    520       1.1  christos 		l += l0 + 1;
    521       1.1  christos 		if (l > MAXCDNAME) {
    522       1.1  christos 			errno = EMSGSIZE;
    523       1.1  christos 			return (-1);
    524       1.1  christos 		}
    525       1.1  christos 		srcp += l0 + 1;
    526       1.1  christos 	} while (n != 0);
    527       1.1  christos 
    528       1.1  christos 	/* from here on we need to reset compression pointer array on error */
    529       1.1  christos 	srcp = src;
    530       1.1  christos 	do {
    531       1.1  christos 		/* Look to see if we can use pointers. */
    532       1.1  christos 		n = *srcp;
    533       1.1  christos 		if (n != 0 && msg != NULL) {
    534       1.1  christos 			l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
    535       1.1  christos 				    (const u_char * const *)lpp);
    536       1.1  christos 			if (l >= 0) {
    537       1.1  christos 				if (dstp + 1 >= eob) {
    538       1.1  christos 					goto cleanup;
    539       1.1  christos 				}
    540       1.2  christos 				*dstp++ = ((u_int32_t)l >> 8) | NS_CMPRSFLGS;
    541       1.1  christos 				*dstp++ = l % 256;
    542       1.1  christos 				return (dstp - dst);
    543       1.1  christos 			}
    544       1.1  christos 			/* Not found, save it. */
    545       1.1  christos 			if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
    546       1.1  christos 			    (dstp - msg) < 0x4000 && first) {
    547       1.1  christos 				*cpp++ = dstp;
    548       1.1  christos 				*cpp = NULL;
    549       1.1  christos 				first = 0;
    550       1.1  christos 			}
    551       1.1  christos 		}
    552       1.1  christos 		/* copy label to buffer */
    553       1.1  christos 		if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
    554       1.1  christos 			/* Should not happen. */
    555       1.1  christos 			goto cleanup;
    556       1.1  christos 		}
    557       1.1  christos 		n = labellen(srcp);
    558       1.1  christos 		if (dstp + 1 + n >= eob) {
    559       1.1  christos 			goto cleanup;
    560       1.1  christos 		}
    561       1.2  christos 		memcpy(dstp, srcp, (size_t)(n + 1));
    562       1.1  christos 		srcp += n + 1;
    563       1.1  christos 		dstp += n + 1;
    564       1.1  christos 	} while (n != 0);
    565       1.1  christos 
    566       1.1  christos 	if (dstp > eob) {
    567       1.1  christos cleanup:
    568       1.1  christos 		if (msg != NULL)
    569       1.1  christos 			*lpp = NULL;
    570       1.1  christos 		errno = EMSGSIZE;
    571       1.1  christos 		return (-1);
    572       1.1  christos 	}
    573       1.1  christos 	return (dstp - dst);
    574       1.1  christos }
    575       1.1  christos 
    576  1.3.10.1       jdc /*%
    577       1.1  christos  *	Expand compressed domain name to presentation format.
    578  1.3.10.1       jdc  *
    579       1.1  christos  * return:
    580  1.3.10.1       jdc  *\li	Number of bytes read out of `src', or -1 (with errno set).
    581  1.3.10.1       jdc  *
    582       1.1  christos  * note:
    583  1.3.10.1       jdc  *\li	Root domain returns as "." not "".
    584       1.1  christos  */
    585       1.1  christos int
    586       1.1  christos ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
    587       1.1  christos 		   char *dst, size_t dstsiz)
    588       1.1  christos {
    589       1.1  christos 	u_char tmp[NS_MAXCDNAME];
    590       1.1  christos 	int n;
    591       1.1  christos 
    592       1.1  christos 	if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
    593       1.1  christos 		return (-1);
    594       1.1  christos 	if (ns_name_ntop(tmp, dst, dstsiz) == -1)
    595       1.1  christos 		return (-1);
    596       1.1  christos 	return (n);
    597       1.1  christos }
    598       1.1  christos 
    599  1.3.10.1       jdc /*%
    600       1.1  christos  *	Compress a domain name into wire format, using compression pointers.
    601  1.3.10.1       jdc  *
    602       1.1  christos  * return:
    603  1.3.10.1       jdc  *\li	Number of bytes consumed in `dst' or -1 (with errno set).
    604  1.3.10.1       jdc  *
    605       1.1  christos  * notes:
    606  1.3.10.1       jdc  *\li	'dnptrs' is an array of pointers to previous compressed names.
    607  1.3.10.1       jdc  *\li	dnptrs[0] is a pointer to the beginning of the message.
    608  1.3.10.1       jdc  *\li	The list ends with NULL.  'lastdnptr' is a pointer to the end of the
    609       1.1  christos  *	array pointed to by 'dnptrs'. Side effect is to update the list of
    610       1.1  christos  *	pointers for labels inserted into the message as we compress the name.
    611  1.3.10.1       jdc  *\li	If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
    612       1.1  christos  *	is NULL, we don't update the list.
    613       1.1  christos  */
    614       1.1  christos int
    615       1.1  christos ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
    616       1.1  christos 		 const u_char **dnptrs, const u_char **lastdnptr)
    617       1.1  christos {
    618       1.1  christos 	u_char tmp[NS_MAXCDNAME];
    619       1.1  christos 
    620       1.1  christos 	if (ns_name_pton(src, tmp, sizeof tmp) == -1)
    621       1.1  christos 		return (-1);
    622       1.2  christos 	return (ns_name_pack(tmp, dst, (int)dstsiz, dnptrs, lastdnptr));
    623       1.1  christos }
    624       1.1  christos 
    625  1.3.10.1       jdc /*%
    626       1.1  christos  * Reset dnptrs so that there are no active references to pointers at or
    627       1.1  christos  * after src.
    628       1.1  christos  */
    629       1.1  christos void
    630       1.1  christos ns_name_rollback(const u_char *src, const u_char **dnptrs,
    631       1.1  christos 		 const u_char **lastdnptr)
    632       1.1  christos {
    633       1.1  christos 	while (dnptrs < lastdnptr && *dnptrs != NULL) {
    634       1.1  christos 		if (*dnptrs >= src) {
    635       1.1  christos 			*dnptrs = NULL;
    636       1.1  christos 			break;
    637       1.1  christos 		}
    638       1.1  christos 		dnptrs++;
    639       1.1  christos 	}
    640       1.1  christos }
    641       1.1  christos 
    642  1.3.10.1       jdc /*%
    643       1.1  christos  *	Advance *ptrptr to skip over the compressed name it points at.
    644  1.3.10.1       jdc  *
    645       1.1  christos  * return:
    646  1.3.10.1       jdc  *\li	0 on success, -1 (with errno set) on failure.
    647       1.1  christos  */
    648       1.1  christos int
    649       1.1  christos ns_name_skip(const u_char **ptrptr, const u_char *eom)
    650       1.1  christos {
    651       1.1  christos 	const u_char *cp;
    652       1.1  christos 	u_int n;
    653       1.1  christos 	int l;
    654       1.1  christos 
    655       1.1  christos 	cp = *ptrptr;
    656       1.1  christos 	while (cp < eom && (n = *cp++) != 0) {
    657       1.1  christos 		/* Check for indirection. */
    658       1.1  christos 		switch (n & NS_CMPRSFLGS) {
    659  1.3.10.1       jdc 		case 0:			/*%< normal case, n == len */
    660       1.1  christos 			cp += n;
    661       1.1  christos 			continue;
    662  1.3.10.1       jdc 		case NS_TYPE_ELT: /*%< EDNS0 extended label */
    663       1.1  christos 			if ((l = labellen(cp - 1)) < 0) {
    664  1.3.10.1       jdc 				errno = EMSGSIZE; /*%< XXX */
    665       1.1  christos 				return(-1);
    666       1.1  christos 			}
    667       1.1  christos 			cp += l;
    668       1.1  christos 			continue;
    669  1.3.10.1       jdc 		case NS_CMPRSFLGS:	/*%< indirection */
    670       1.1  christos 			cp++;
    671       1.1  christos 			break;
    672  1.3.10.1       jdc 		default:		/*%< illegal type */
    673       1.1  christos 			errno = EMSGSIZE;
    674       1.1  christos 			return (-1);
    675       1.1  christos 		}
    676       1.1  christos 		break;
    677       1.1  christos 	}
    678       1.1  christos 	if (cp > eom) {
    679       1.1  christos 		errno = EMSGSIZE;
    680       1.1  christos 		return (-1);
    681       1.1  christos 	}
    682       1.1  christos 	*ptrptr = cp;
    683       1.1  christos 	return (0);
    684       1.1  christos }
    685       1.1  christos 
    686       1.1  christos /* Private. */
    687       1.1  christos 
    688  1.3.10.1       jdc /*%
    689       1.1  christos  *	Thinking in noninternationalized USASCII (per the DNS spec),
    690       1.1  christos  *	is this characted special ("in need of quoting") ?
    691  1.3.10.1       jdc  *
    692       1.1  christos  * return:
    693  1.3.10.1       jdc  *\li	boolean.
    694       1.1  christos  */
    695       1.1  christos static int
    696       1.1  christos special(int ch) {
    697       1.1  christos 	switch (ch) {
    698  1.3.10.1       jdc 	case 0x22: /*%< '"' */
    699  1.3.10.1       jdc 	case 0x2E: /*%< '.' */
    700  1.3.10.1       jdc 	case 0x3B: /*%< ';' */
    701  1.3.10.1       jdc 	case 0x5C: /*%< '\\' */
    702  1.3.10.1       jdc 	case 0x28: /*%< '(' */
    703  1.3.10.1       jdc 	case 0x29: /*%< ')' */
    704       1.1  christos 	/* Special modifiers in zone files. */
    705  1.3.10.1       jdc 	case 0x40: /*%< '@' */
    706  1.3.10.1       jdc 	case 0x24: /*%< '$' */
    707       1.1  christos 		return (1);
    708       1.1  christos 	default:
    709       1.1  christos 		return (0);
    710       1.1  christos 	}
    711       1.1  christos }
    712       1.1  christos 
    713  1.3.10.1       jdc /*%
    714       1.1  christos  *	Thinking in noninternationalized USASCII (per the DNS spec),
    715       1.1  christos  *	is this character visible and not a space when printed ?
    716  1.3.10.1       jdc  *
    717       1.1  christos  * return:
    718  1.3.10.1       jdc  *\li	boolean.
    719       1.1  christos  */
    720       1.1  christos static int
    721       1.1  christos printable(int ch) {
    722       1.1  christos 	return (ch > 0x20 && ch < 0x7f);
    723       1.1  christos }
    724       1.1  christos 
    725  1.3.10.1       jdc /*%
    726       1.1  christos  *	Thinking in noninternationalized USASCII (per the DNS spec),
    727       1.1  christos  *	convert this character to lower case if it's upper case.
    728       1.1  christos  */
    729       1.1  christos static int
    730       1.1  christos mklower(int ch) {
    731       1.1  christos 	if (ch >= 0x41 && ch <= 0x5A)
    732       1.1  christos 		return (ch + 0x20);
    733       1.1  christos 	return (ch);
    734       1.1  christos }
    735       1.1  christos 
    736  1.3.10.1       jdc /*%
    737       1.1  christos  *	Search for the counted-label name in an array of compressed names.
    738  1.3.10.1       jdc  *
    739       1.1  christos  * return:
    740  1.3.10.1       jdc  *\li	offset from msg if found, or -1.
    741  1.3.10.1       jdc  *
    742       1.1  christos  * notes:
    743  1.3.10.1       jdc  *\li	dnptrs is the pointer to the first name on the list,
    744  1.3.10.1       jdc  *\li	not the pointer to the start of the message.
    745       1.1  christos  */
    746       1.1  christos static int
    747       1.1  christos dn_find(const u_char *domain, const u_char *msg,
    748       1.1  christos 	const u_char * const *dnptrs,
    749       1.1  christos 	const u_char * const *lastdnptr)
    750       1.1  christos {
    751       1.1  christos 	const u_char *dn, *cp, *sp;
    752       1.1  christos 	const u_char * const *cpp;
    753       1.1  christos 	u_int n;
    754       1.1  christos 
    755       1.1  christos 	for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
    756       1.1  christos 		sp = *cpp;
    757       1.1  christos 		/*
    758       1.1  christos 		 * terminate search on:
    759       1.1  christos 		 * root label
    760       1.1  christos 		 * compression pointer
    761       1.1  christos 		 * unusable offset
    762       1.1  christos 		 */
    763       1.1  christos 		while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 &&
    764       1.1  christos 		       (sp - msg) < 0x4000) {
    765       1.1  christos 			dn = domain;
    766       1.1  christos 			cp = sp;
    767       1.1  christos 			while ((n = *cp++) != 0) {
    768       1.1  christos 				/*
    769       1.1  christos 				 * check for indirection
    770       1.1  christos 				 */
    771       1.1  christos 				switch (n & NS_CMPRSFLGS) {
    772  1.3.10.1       jdc 				case 0:		/*%< normal case, n == len */
    773  1.3.10.1       jdc 					n = labellen(cp - 1); /*%< XXX */
    774       1.1  christos 					if (n != *dn++)
    775       1.1  christos 						goto next;
    776       1.1  christos 
    777       1.2  christos 					for (; n > 0; n--)
    778       1.1  christos 						if (mklower(*dn++) !=
    779       1.1  christos 						    mklower(*cp++))
    780       1.1  christos 							goto next;
    781       1.1  christos 					/* Is next root for both ? */
    782       1.1  christos 					if (*dn == '\0' && *cp == '\0')
    783       1.1  christos 						return (sp - msg);
    784       1.1  christos 					if (*dn)
    785       1.1  christos 						continue;
    786       1.1  christos 					goto next;
    787  1.3.10.1       jdc 				case NS_CMPRSFLGS:	/*%< indirection */
    788       1.1  christos 					cp = msg + (((n & 0x3f) << 8) | *cp);
    789       1.1  christos 					break;
    790       1.1  christos 
    791  1.3.10.1       jdc 				default:	/*%< illegal type */
    792       1.1  christos 					errno = EMSGSIZE;
    793       1.1  christos 					return (-1);
    794       1.1  christos 				}
    795       1.1  christos 			}
    796       1.1  christos  next: ;
    797       1.1  christos 			sp += *sp + 1;
    798       1.1  christos 		}
    799       1.1  christos 	}
    800       1.1  christos 	errno = ENOENT;
    801       1.1  christos 	return (-1);
    802       1.1  christos }
    803       1.1  christos 
    804       1.1  christos static int
    805       1.3  christos decode_bitstring(const unsigned char **cpp, char *dn, const char *eom)
    806       1.1  christos {
    807       1.3  christos 	const unsigned char *cp = *cpp;
    808       1.1  christos 	char *beg = dn, tc;
    809       1.1  christos 	int b, blen, plen, i;
    810       1.1  christos 
    811       1.1  christos 	if ((blen = (*cp & 0xff)) == 0)
    812       1.1  christos 		blen = 256;
    813       1.1  christos 	plen = (blen + 3) / 4;
    814       1.1  christos 	plen += sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1);
    815       1.1  christos 	if (dn + plen >= eom)
    816       1.1  christos 		return(-1);
    817       1.1  christos 
    818       1.1  christos 	cp++;
    819       1.1  christos 	i = SPRINTF((dn, "\\[x"));
    820       1.1  christos 	if (i < 0)
    821       1.1  christos 		return (-1);
    822       1.1  christos 	dn += i;
    823       1.1  christos 	for (b = blen; b > 7; b -= 8, cp++) {
    824       1.1  christos 		i = SPRINTF((dn, "%02x", *cp & 0xff));
    825       1.1  christos 		if (i < 0)
    826       1.1  christos 			return (-1);
    827       1.1  christos 		dn += i;
    828       1.1  christos 	}
    829       1.1  christos 	if (b > 4) {
    830       1.1  christos 		tc = *cp++;
    831       1.1  christos 		i = SPRINTF((dn, "%02x", tc & (0xff << (8 - b))));
    832       1.1  christos 		if (i < 0)
    833       1.1  christos 			return (-1);
    834       1.1  christos 		dn += i;
    835       1.1  christos 	} else if (b > 0) {
    836       1.1  christos 		tc = *cp++;
    837       1.1  christos 		i = SPRINTF((dn, "%1x",
    838       1.2  christos 			       (((u_int32_t)tc >> 4) & 0x0f) & (0x0f << (4 - b))));
    839       1.1  christos 		if (i < 0)
    840       1.1  christos 			return (-1);
    841       1.1  christos 		dn += i;
    842       1.1  christos 	}
    843       1.1  christos 	i = SPRINTF((dn, "/%d]", blen));
    844       1.1  christos 	if (i < 0)
    845       1.1  christos 		return (-1);
    846       1.1  christos 	dn += i;
    847       1.1  christos 
    848       1.1  christos 	*cpp = cp;
    849       1.1  christos 	return(dn - beg);
    850       1.1  christos }
    851       1.1  christos 
    852       1.1  christos static int
    853       1.3  christos encode_bitsring(const char **bp, const char *end, unsigned char **labelp,
    854       1.3  christos 	        unsigned char ** dst, unsigned const char *eom)
    855       1.1  christos {
    856       1.1  christos 	int afterslash = 0;
    857       1.1  christos 	const char *cp = *bp;
    858       1.3  christos 	unsigned char *tp;
    859       1.3  christos 	char c;
    860       1.1  christos 	const char *beg_blen;
    861       1.1  christos 	char *end_blen = NULL;
    862       1.1  christos 	int value = 0, count = 0, tbcount = 0, blen = 0;
    863       1.1  christos 
    864       1.1  christos 	beg_blen = end_blen = NULL;
    865       1.1  christos 
    866       1.1  christos 	/* a bitstring must contain at least 2 characters */
    867       1.1  christos 	if (end - cp < 2)
    868       1.1  christos 		return(EINVAL);
    869       1.1  christos 
    870       1.1  christos 	/* XXX: currently, only hex strings are supported */
    871       1.1  christos 	if (*cp++ != 'x')
    872       1.1  christos 		return(EINVAL);
    873  1.3.10.1       jdc 	if (!isxdigit((*cp) & 0xff)) /*%< reject '\[x/BLEN]' */
    874       1.1  christos 		return(EINVAL);
    875       1.1  christos 
    876       1.1  christos 	for (tp = *dst + 1; cp < end && tp < eom; cp++) {
    877       1.1  christos 		switch((c = *cp)) {
    878  1.3.10.1       jdc 		case ']':	/*%< end of the bitstring */
    879       1.1  christos 			if (afterslash) {
    880       1.1  christos 				if (beg_blen == NULL)
    881       1.1  christos 					return(EINVAL);
    882       1.1  christos 				blen = (int)strtol(beg_blen, &end_blen, 10);
    883       1.1  christos 				if (*end_blen != ']')
    884       1.1  christos 					return(EINVAL);
    885       1.1  christos 			}
    886       1.1  christos 			if (count)
    887       1.1  christos 				*tp++ = ((value << 4) & 0xff);
    888  1.3.10.1       jdc 			cp++;	/*%< skip ']' */
    889       1.1  christos 			goto done;
    890       1.1  christos 		case '/':
    891       1.1  christos 			afterslash = 1;
    892       1.1  christos 			break;
    893       1.1  christos 		default:
    894       1.1  christos 			if (afterslash) {
    895       1.1  christos 				if (!isdigit(c&0xff))
    896       1.1  christos 					return(EINVAL);
    897       1.1  christos 				if (beg_blen == NULL) {
    898       1.1  christos 
    899       1.1  christos 					if (c == '0') {
    900       1.1  christos 						/* blen never begings with 0 */
    901       1.1  christos 						return(EINVAL);
    902       1.1  christos 					}
    903       1.1  christos 					beg_blen = cp;
    904       1.1  christos 				}
    905       1.1  christos 			} else {
    906       1.1  christos 				if (!isxdigit(c&0xff))
    907       1.1  christos 					return(EINVAL);
    908       1.1  christos 				value <<= 4;
    909       1.1  christos 				value += digitvalue[(int)c];
    910       1.1  christos 				count += 4;
    911       1.1  christos 				tbcount += 4;
    912       1.1  christos 				if (tbcount > 256)
    913       1.1  christos 					return(EINVAL);
    914       1.1  christos 				if (count == 8) {
    915       1.1  christos 					*tp++ = value;
    916       1.1  christos 					count = 0;
    917       1.1  christos 				}
    918       1.1  christos 			}
    919       1.1  christos 			break;
    920       1.1  christos 		}
    921       1.1  christos 	}
    922       1.1  christos   done:
    923       1.1  christos 	if (cp >= end || tp >= eom)
    924       1.1  christos 		return(EMSGSIZE);
    925       1.1  christos 
    926       1.1  christos 	/*
    927       1.1  christos 	 * bit length validation:
    928       1.1  christos 	 * If a <length> is present, the number of digits in the <bit-data>
    929       1.1  christos 	 * MUST be just sufficient to contain the number of bits specified
    930       1.1  christos 	 * by the <length>. If there are insignificant bits in a final
    931       1.1  christos 	 * hexadecimal or octal digit, they MUST be zero.
    932  1.3.10.1       jdc 	 * RFC2673, Section 3.2.
    933       1.1  christos 	 */
    934       1.1  christos 	if (blen > 0) {
    935       1.1  christos 		int traillen;
    936       1.1  christos 
    937       1.1  christos 		if (((blen + 3) & ~3) != tbcount)
    938       1.1  christos 			return(EINVAL);
    939  1.3.10.1       jdc 		traillen = tbcount - blen; /*%< between 0 and 3 */
    940       1.1  christos 		if (((value << (8 - traillen)) & 0xff) != 0)
    941       1.1  christos 			return(EINVAL);
    942       1.1  christos 	}
    943       1.1  christos 	else
    944       1.1  christos 		blen = tbcount;
    945       1.1  christos 	if (blen == 256)
    946       1.1  christos 		blen = 0;
    947       1.1  christos 
    948       1.1  christos 	/* encode the type and the significant bit fields */
    949       1.1  christos 	**labelp = DNS_LABELTYPE_BITSTRING;
    950       1.1  christos 	**dst = blen;
    951       1.1  christos 
    952       1.1  christos 	*bp = cp;
    953       1.1  christos 	*dst = tp;
    954       1.1  christos 
    955       1.1  christos 	return(0);
    956       1.1  christos }
    957       1.1  christos 
    958       1.1  christos static int
    959       1.1  christos labellen(const u_char *lp)
    960       1.1  christos {
    961       1.1  christos 	int bitlen;
    962       1.1  christos 	u_char l = *lp;
    963       1.1  christos 
    964       1.1  christos 	if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
    965       1.1  christos 		/* should be avoided by the caller */
    966       1.1  christos 		return(-1);
    967       1.1  christos 	}
    968       1.1  christos 
    969       1.1  christos 	if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT) {
    970       1.1  christos 		if (l == DNS_LABELTYPE_BITSTRING) {
    971       1.1  christos 			if ((bitlen = *(lp + 1)) == 0)
    972       1.1  christos 				bitlen = 256;
    973       1.1  christos 			return((bitlen + 7 ) / 8 + 1);
    974       1.1  christos 		}
    975  1.3.10.1       jdc 		return(-1);	/*%< unknwon ELT */
    976       1.1  christos 	}
    977       1.1  christos 	return(l);
    978       1.1  christos }
    979  1.3.10.1       jdc 
    980  1.3.10.1       jdc /*! \file */
    981