Libinfo-517.200.9.tar.gz
[apple/libinfo.git] / dns.subproj / res_comp.c
1 /*
2 * Copyright (c) 2018 Apple Inc. All rights reserved.
3 */
4 /*
5 * ++Copyright++ 1985, 1993
6 * -
7 * Copyright (c) 1985, 1993
8 * The Regents of the University of California. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 * -
38 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
39 *
40 * Permission to use, copy, modify, and distribute this software for any
41 * purpose with or without fee is hereby granted, provided that the above
42 * copyright notice and this permission notice appear in all copies, and that
43 * the name of Digital Equipment Corporation not be used in advertising or
44 * publicity pertaining to distribution of the document or software without
45 * specific, written prior permission.
46 *
47 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
48 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
50 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
51 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
52 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
53 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
54 * SOFTWARE.
55 * -
56 * --Copyright--
57 */
58
59 #if defined(LIBC_SCCS) && !defined(lint)
60 static char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93";
61 static char rcsid[] = "$Id: res_comp.c,v 1.4 2003/02/18 17:29:24 majka Exp $";
62 #endif /* LIBC_SCCS and not lint */
63
64 #include "libinfo_common.h"
65
66 #include <sys/param.h>
67 #include <netinet/in.h>
68
69 #include <stdio.h>
70 #include <ctype.h>
71
72 #include <arpa/nameser_compat.h>
73 #include <nameser.h>
74
75 /*
76 * Expand compressed domain name 'comp_dn' to full domain name.
77 * 'msg' is a pointer to the begining of the message,
78 * 'eomorig' points to the first location after the message,
79 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
80 * Return size of compressed name or -1 if there was an error.
81 */
82 LIBINFO_EXPORT
83 int
84 dn_expand(const u_char *msg, const u_char *eomorig, const u_char *comp_dn, char *exp_dn, int length)
85 {
86 register const u_char *cp;
87 register char *dn;
88 register int n, c;
89 char *eom;
90 int len = -1, checked = 0;
91
92 dn = exp_dn;
93 cp = comp_dn;
94 eom = exp_dn + length;
95 /*
96 * fetch next label in domain name
97 */
98 while ((n = *cp++)) {
99 /*
100 * Check for indirection
101 */
102 switch (n & INDIR_MASK) {
103 case 0:
104 if (dn != exp_dn) {
105 if (dn >= eom)
106 return (-1);
107 *dn++ = '.';
108 }
109 if (dn+n >= eom)
110 return (-1);
111 checked += n + 1;
112 while (--n >= 0) {
113 if ((c = *cp++) == '.') {
114 if (dn + n + 2 >= eom)
115 return (-1);
116 *dn++ = '\\';
117 }
118 *dn++ = c;
119 if (cp >= eomorig) /* out of range */
120 return (-1);
121 }
122 break;
123
124 case INDIR_MASK:
125 if (len < 0)
126 len = cp - comp_dn + 1;
127 cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
128 if (cp < msg || cp >= eomorig) /* out of range */
129 return (-1);
130 checked += 2;
131 /*
132 * Check for loops in the compressed name;
133 * if we've looked at the whole message,
134 * there must be a loop.
135 */
136 if (checked >= eomorig - msg)
137 return (-1);
138 break;
139
140 default:
141 return (-1); /* flag error */
142 }
143 }
144 *dn = '\0';
145 for (dn = exp_dn; (c = *dn) != '\0'; dn++)
146 if (isascii(c) && isspace(c))
147 return (-1);
148 if (len < 0)
149 len = cp - comp_dn;
150 return (len);
151 }
152
153 /*
154 * Skip over a compressed domain name. Return the size or -1.
155 */
156 LIBINFO_EXPORT
157 int
158 __dn_skipname(const u_char *comp_dn, const u_char *eom)
159 {
160 register const u_char *cp;
161 register int n;
162
163 cp = comp_dn;
164 while (cp < eom && (n = *cp++)) {
165 /*
166 * check for indirection
167 */
168 switch (n & INDIR_MASK) {
169 case 0: /* normal case, n == len */
170 cp += n;
171 continue;
172 case INDIR_MASK: /* indirection */
173 cp++;
174 break;
175 default: /* illegal type */
176 return (-1);
177 }
178 break;
179 }
180 if (cp > eom)
181 return (-1);
182 return (cp - comp_dn);
183 }
184
185 /*
186 * Routines to insert/extract short/long's.
187 */
188
189 LIBINFO_EXPORT
190 u_int16_t
191 _getshort(const u_char *msgp)
192 {
193 u_int16_t u;
194 GETSHORT(u, msgp);
195 return u;
196 }
197
198 LIBINFO_EXPORT
199 u_int32_t
200 _getlong(const u_char *msgp)
201 {
202 u_int32_t u;
203 GETLONG(u, msgp);
204 return u;
205 }