xref: /csrg-svn/lib/libc/net/res_comp.c (revision 30441)
118546Sralph /*
221384Sdist  * Copyright (c) 1985 Regents of the University of California.
321384Sdist  * All rights reserved.  The Berkeley software License Agreement
421384Sdist  * specifies the terms and conditions for redistribution.
518546Sralph  */
618546Sralph 
726631Sdonn #if defined(LIBC_SCCS) && !defined(lint)
8*30441Skjd static char sccsid[] = "@(#)res_comp.c	6.8 (Berkeley) 01/31/87";
926631Sdonn #endif LIBC_SCCS and not lint
1021384Sdist 
1118140Sralph #include <sys/types.h>
1218140Sralph #include <stdio.h>
1324079Skjd #include <arpa/nameser.h>
1418140Sralph 
1523870Skjd 
1618140Sralph /*
1718342Sralph  * Expand compressed domain name 'comp_dn' to full domain name.
1818342Sralph  * 'msg' is a pointer to the begining of the message,
1926112Skarels  * 'eomorig' points to the first location after the message,
2018342Sralph  * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
2118140Sralph  * Return size of compressed name or -1 if there was an error.
2218140Sralph  */
2326112Skarels dn_expand(msg, eomorig, comp_dn, exp_dn, length)
2426112Skarels 	char *msg, *eomorig, *comp_dn, *exp_dn;
2526112Skarels 	int length;
2618140Sralph {
2718140Sralph 	register char *cp, *dn;
2818140Sralph 	register int n, c;
2926112Skarels 	char *eom;
3018342Sralph 	int len = -1;
3118140Sralph 
3218140Sralph 	dn = exp_dn;
3318140Sralph 	cp = comp_dn;
3418140Sralph 	eom = exp_dn + length - 1;
3518140Sralph 	/*
3618140Sralph 	 * fetch next label in domain name
3718140Sralph 	 */
3818140Sralph 	while (n = *cp++) {
3918140Sralph 		/*
4018140Sralph 		 * Check for indirection
4118140Sralph 		 */
4218140Sralph 		switch (n & INDIR_MASK) {
4318140Sralph 		case 0:
4418342Sralph 			if (dn != exp_dn) {
4518342Sralph 				if (dn >= eom)
4618342Sralph 					return (-1);
4718140Sralph 				*dn++ = '.';
4818342Sralph 			}
4918140Sralph 			if (dn+n >= eom)
5018140Sralph 				return (-1);
5126064Skjd 			while (--n >= 0) {
5226812Sbloom 				if ((c = *cp++) == '.') {
5326812Sbloom 					if (dn+n+1 >= eom)
5426812Sbloom 						return (-1);
5526812Sbloom 					*dn++ = '\\';
5618342Sralph 				}
5726812Sbloom 				*dn++ = c;
5826112Skarels 				if (cp >= eomorig)	/* out of range */
5926064Skjd 					return(-1);
6026064Skjd 			}
6118140Sralph 			break;
6218140Sralph 
6318140Sralph 		case INDIR_MASK:
6418342Sralph 			if (len < 0)
6518140Sralph 				len = cp - comp_dn + 1;
6618140Sralph 			cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
6726112Skarels 			if (cp < msg || cp >= eomorig)	/* out of range */
6826064Skjd 				return(-1);
6918140Sralph 			break;
7018140Sralph 
7118140Sralph 		default:
7218140Sralph 			return (-1);			/* flag error */
7318140Sralph 		}
7418140Sralph 	}
7518140Sralph 	*dn = '\0';
7618342Sralph 	if (len < 0)
7718140Sralph 		len = cp - comp_dn;
7818140Sralph 	return (len);
7918140Sralph }
8018140Sralph 
8118140Sralph /*
8218342Sralph  * Compress domain name 'exp_dn' into 'comp_dn'.
8318342Sralph  * Return the size of the compressed name or -1.
8418342Sralph  * 'length' is the size of the array pointed to by 'comp_dn'.
8518342Sralph  * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
8618140Sralph  * is a pointer to the beginning of the message. The list ends with NULL.
8718342Sralph  * 'lastdnptr' is a pointer to the end of the arrary pointed to
8818342Sralph  * by 'dnptrs'. Side effect is to update the list of pointers for
8918342Sralph  * labels inserted into the message as we compress the name.
9018342Sralph  * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
9118342Sralph  * is NULL, we don't update the list.
9218140Sralph  */
9318140Sralph dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
9418140Sralph 	char *exp_dn, *comp_dn;
9518140Sralph 	int length;
9618140Sralph 	char **dnptrs, **lastdnptr;
9718140Sralph {
9818140Sralph 	register char *cp, *dn;
9918140Sralph 	register int c, l;
10018140Sralph 	char **cpp, **lpp, *sp, *eob;
10118140Sralph 	char *msg;
10218140Sralph 
10318140Sralph 	dn = exp_dn;
10418140Sralph 	cp = comp_dn;
10525360Sbloom 	eob = cp + length;
10618140Sralph 	if (dnptrs != NULL) {
10718140Sralph 		if ((msg = *dnptrs++) != NULL) {
10818140Sralph 			for (cpp = dnptrs; *cpp != NULL; cpp++)
10918140Sralph 				;
11018140Sralph 			lpp = cpp;	/* end of list to search */
11118140Sralph 		}
11218140Sralph 	} else
11318140Sralph 		msg = NULL;
11418140Sralph 	for (c = *dn++; c != '\0'; ) {
11518140Sralph 		/* look to see if we can use pointers */
11618140Sralph 		if (msg != NULL) {
11718140Sralph 			if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
11818140Sralph 				if (cp+1 >= eob)
11918140Sralph 					return (-1);
12018140Sralph 				*cp++ = (l >> 8) | INDIR_MASK;
12126240Skjd 				*cp++ = l % 256;
12218140Sralph 				return (cp - comp_dn);
12318140Sralph 			}
12418140Sralph 			/* not found, save it */
12518140Sralph 			if (lastdnptr != NULL && cpp < lastdnptr-1) {
12618140Sralph 				*cpp++ = cp;
12718140Sralph 				*cpp = NULL;
12818140Sralph 			}
12918140Sralph 		}
13018140Sralph 		sp = cp++;	/* save ptr to length byte */
13118140Sralph 		do {
13218140Sralph 			if (c == '.') {
13318140Sralph 				c = *dn++;
13418140Sralph 				break;
13518140Sralph 			}
13618342Sralph 			if (c == '\\') {
13718342Sralph 				if ((c = *dn++) == '\0')
13818342Sralph 					break;
13918342Sralph 			}
14018140Sralph 			if (cp >= eob)
14118140Sralph 				return (-1);
14218140Sralph 			*cp++ = c;
14318140Sralph 		} while ((c = *dn++) != '\0');
14418342Sralph 		/* catch trailing '.'s but not '..' */
14518342Sralph 		if ((l = cp - sp - 1) == 0 && c == '\0') {
14618342Sralph 			cp--;
14718342Sralph 			break;
14818342Sralph 		}
14918342Sralph 		if (l <= 0 || l > MAXLABEL)
15018140Sralph 			return (-1);
15118140Sralph 		*sp = l;
15218140Sralph 	}
15318140Sralph 	if (cp >= eob)
15418140Sralph 		return (-1);
15518140Sralph 	*cp++ = '\0';
15618140Sralph 	return (cp - comp_dn);
15718140Sralph }
15818140Sralph 
15918140Sralph /*
16018342Sralph  * Skip over a compressed domain name. Return the size or -1.
16118140Sralph  */
16218342Sralph dn_skip(comp_dn)
16318342Sralph 	char *comp_dn;
16418140Sralph {
16518140Sralph 	register char *cp;
16618140Sralph 	register int n;
16718140Sralph 
16818342Sralph 	cp = comp_dn;
16918140Sralph 	while (n = *cp++) {
17018140Sralph 		/*
17118140Sralph 		 * check for indirection
17218140Sralph 		 */
17318140Sralph 		switch (n & INDIR_MASK) {
17418140Sralph 		case 0:		/* normal case, n == len */
17518140Sralph 			cp += n;
17618140Sralph 			continue;
17718140Sralph 		default:	/* illegal type */
17818140Sralph 			return (-1);
17918140Sralph 		case INDIR_MASK:	/* indirection */
18018140Sralph 			cp++;
18118140Sralph 		}
18218140Sralph 		break;
18318140Sralph 	}
18418342Sralph 	return (cp - comp_dn);
18518140Sralph }
18618140Sralph 
18718140Sralph /*
18818140Sralph  * Search for expanded name from a list of previously compressed names.
18918140Sralph  * Return the offset from msg if found or -1.
19018140Sralph  */
19118140Sralph dn_find(exp_dn, msg, dnptrs, lastdnptr)
19218140Sralph 	char *exp_dn, *msg;
19318140Sralph 	char **dnptrs, **lastdnptr;
19418140Sralph {
19518140Sralph 	register char *dn, *cp, **cpp;
19618140Sralph 	register int n;
19718140Sralph 	char *sp;
19818140Sralph 
19926240Skjd 	for (cpp = dnptrs + 1; cpp < lastdnptr; cpp++) {
20018140Sralph 		dn = exp_dn;
20118140Sralph 		sp = cp = *cpp;
20218140Sralph 		while (n = *cp++) {
20318140Sralph 			/*
20418140Sralph 			 * check for indirection
20518140Sralph 			 */
20618140Sralph 			switch (n & INDIR_MASK) {
20718140Sralph 			case 0:		/* normal case, n == len */
20818342Sralph 				while (--n >= 0) {
20918342Sralph 					if (*dn == '\\')
21018342Sralph 						dn++;
21118140Sralph 					if (*dn++ != *cp++)
21218140Sralph 						goto next;
21318342Sralph 				}
21418140Sralph 				if ((n = *dn++) == '\0' && *cp == '\0')
21518140Sralph 					return (sp - msg);
21618140Sralph 				if (n == '.')
21718140Sralph 					continue;
21818140Sralph 				goto next;
21918140Sralph 
22018140Sralph 			default:	/* illegal type */
22118140Sralph 				return (-1);
22218140Sralph 
22318140Sralph 			case INDIR_MASK:	/* indirection */
22418140Sralph 				cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
22518140Sralph 			}
22618140Sralph 		}
22718140Sralph 		if (*dn == '\0')
22818140Sralph 			return (sp - msg);
22918140Sralph 	next:	;
23018140Sralph 	}
23118140Sralph 	return (-1);
23218140Sralph }
23318528Sralph 
23418528Sralph /*
23518528Sralph  * Routines to insert/extract short/long's. Must account for byte
23618528Sralph  * order and non-alignment problems. This code at least has the
23718528Sralph  * advantage of being portable.
23818528Sralph  */
23918528Sralph 
24018528Sralph u_short
241*30441Skjd _getshort(msgp)
24218528Sralph 	char *msgp;
24318528Sralph {
24418528Sralph 	register u_char *p = (u_char *) msgp;
24525360Sbloom #ifdef vax
24625360Sbloom 	/*
24725360Sbloom 	 * vax compiler doesn't put shorts in registers
24825360Sbloom 	 */
24925360Sbloom 	register u_long u;
25025360Sbloom #else
25124735Sbloom 	register u_short u;
25225360Sbloom #endif
25318528Sralph 
25424735Sbloom 	u = *p++ << 8;
25525360Sbloom 	return ((u_short)(u | *p));
25618528Sralph }
25718528Sralph 
25818528Sralph u_long
259*30441Skjd _getlong(msgp)
26018528Sralph 	char *msgp;
26118528Sralph {
26218528Sralph 	register u_char *p = (u_char *) msgp;
26323870Skjd 	register u_long u;
26418528Sralph 
26523870Skjd 	u = *p++; u <<= 8;
26623870Skjd 	u |= *p++; u <<= 8;
26723870Skjd 	u |= *p++; u <<= 8;
26823870Skjd 	return (u | *p);
26918528Sralph }
27018528Sralph 
27123870Skjd 
27218528Sralph putshort(s, msgp)
27318528Sralph 	register u_short s;
27418528Sralph 	register char *msgp;
27518528Sralph {
27618528Sralph 
27718528Sralph 	msgp[1] = s;
27818528Sralph 	msgp[0] = s >> 8;
27918528Sralph }
28018528Sralph 
28118528Sralph putlong(l, msgp)
28218528Sralph 	register u_long l;
28318528Sralph 	register char *msgp;
28418528Sralph {
28518528Sralph 
28618528Sralph 	msgp[3] = l;
28718528Sralph 	msgp[2] = (l >>= 8);
28818528Sralph 	msgp[1] = (l >>= 8);
28918528Sralph 	msgp[0] = l >> 8;
29018528Sralph }
291