modified: myjupyterlab.sh
[GalaxyCodeBases.git] / etc / Windows / vlmcsd_old_vancepym / ns_name.c
blob6bdff565c416e0aecf54e8e7e30cd06121ecbd04
1 /*
2 * Copyright (c) 1996,1999 by Internet Software Consortium.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15 * SOFTWARE.
19 * Modified by Hotbird64 for use with vlmcs.
22 #ifndef CONFIG
23 #define CONFIG "config.h"
24 #endif // CONFIG
25 #include CONFIG
27 #ifdef DNS_PARSER_INTERNAL
28 #ifndef NO_DNS
30 #include <sys/types.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <limits.h>
38 #include "types.h"
39 #include "ns_name.h"
41 #ifdef SPRINTF_CHAR
42 # define SPRINTF(x) strlen(sprintf/**/x)
43 #else
44 # define SPRINTF(x) ((size_t)sprintf x)
45 #endif
47 #define NS_TYPE_ELT 0x40 /* EDNS0 extended label type */
48 #define DNS_LABELTYPE_BITSTRING 0x41
50 #define NS_MAXCDNAME 255
51 #define NS_CMPRSFLGS 0xc0
53 /* Data. */
55 static char digits[] = "0123456789";
58 /* Forward. */
60 static int special_vlmcsd(int);
61 static int printable_vlmcsd(int);
62 static int labellen_vlmcsd(const uint8_t *);
63 static int decode_bitstring_vlmcsd(const char **, char *, const char *);
66 * ns_name_ntop(src, dst, dstsiz)
67 * Convert an encoded domain name to printable ascii as per RFC1035.
68 * return:
69 * Number of bytes written to buffer, or -1 (with errno set)
70 * notes:
71 * The root is returned as "."
72 * All other domains are returned in non absolute form
74 static int
75 ns_name_ntop_vlmcsd(const uint8_t *src, char *dst, size_t dstsiz)
77 const uint8_t *cp;
78 char *dn, *eom;
79 uint8_t c;
80 uint32_t n;
81 int l;
83 cp = src;
84 dn = dst;
85 eom = dst + dstsiz;
87 while ((n = *cp++) != 0) {
88 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
89 /* Some kind of compression pointer. */
90 errno = EMSGSIZE;
91 return (-1);
93 if (dn != dst) {
94 if (dn >= eom) {
95 errno = EMSGSIZE;
96 return (-1);
98 *dn++ = '.';
100 if ((l = labellen_vlmcsd(cp - 1)) < 0) {
101 errno = EMSGSIZE; /* XXX */
102 return(-1);
104 if (dn + l >= eom) {
105 errno = EMSGSIZE;
106 return (-1);
108 if ((n & NS_CMPRSFLGS) == NS_TYPE_ELT) {
109 int m;
111 if (n != DNS_LABELTYPE_BITSTRING) {
112 /* XXX: labellen should reject this case */
113 errno = EINVAL;
114 return(-1);
116 if ((m = decode_bitstring_vlmcsd((const char **)&cp, dn, eom)) < 0)
118 errno = EMSGSIZE;
119 return(-1);
121 dn += m;
122 continue;
124 for ((void)NULL; l > 0; l--) {
125 c = *cp++;
126 if (special_vlmcsd(c)) {
127 if (dn + 1 >= eom) {
128 errno = EMSGSIZE;
129 return (-1);
131 *dn++ = '\\';
132 *dn++ = (char)c;
133 } else if (!printable_vlmcsd(c)) {
134 if (dn + 3 >= eom) {
135 errno = EMSGSIZE;
136 return (-1);
138 *dn++ = '\\';
139 *dn++ = digits[c / 100];
140 *dn++ = digits[(c % 100) / 10];
141 *dn++ = digits[c % 10];
142 } else {
143 if (dn >= eom) {
144 errno = EMSGSIZE;
145 return (-1);
147 *dn++ = (char)c;
151 if (dn == dst) {
152 if (dn >= eom) {
153 errno = EMSGSIZE;
154 return (-1);
156 *dn++ = '.';
158 if (dn >= eom) {
159 errno = EMSGSIZE;
160 return (-1);
162 *dn++ = '\0';
163 return (dn - dst);
166 static int
167 ns_name_unpack_vlmcsd(const uint8_t *msg, const uint8_t *eom, const uint8_t *src,
168 uint8_t *dst, size_t dstsiz)
170 const uint8_t *srcp, *dstlim;
171 uint8_t *dstp;
172 int n, len, checked, l;
174 len = -1;
175 checked = 0;
176 dstp = dst;
177 srcp = src;
178 dstlim = dst + dstsiz;
179 if (srcp < msg || srcp >= eom) {
180 errno = EMSGSIZE;
181 return (-1);
183 /* Fetch next label in domain name. */
184 while ((n = *srcp++) != 0) {
185 /* Check for indirection. */
186 switch (n & NS_CMPRSFLGS) {
187 case 0:
188 case NS_TYPE_ELT:
189 /* Limit checks. */
190 if ((l = labellen_vlmcsd(srcp - 1)) < 0) {
191 errno = EMSGSIZE;
192 return(-1);
194 if (dstp + l + 1 >= dstlim || srcp + l >= eom) {
195 errno = EMSGSIZE;
196 return (-1);
198 checked += l + 1;
199 *dstp++ = n;
200 memcpy(dstp, srcp, l);
201 dstp += l;
202 srcp += l;
203 break;
205 case NS_CMPRSFLGS:
206 if (srcp >= eom) {
207 errno = EMSGSIZE;
208 return (-1);
210 if (len < 0)
211 len = srcp - src + 1;
212 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
213 if (srcp < msg || srcp >= eom) { /* Out of range. */
214 errno = EMSGSIZE;
215 return (-1);
217 checked += 2;
219 * Check for loops in the compressed name;
220 * if we've looked at the whole message,
221 * there must be a loop.
223 if (checked >= eom - msg) {
224 errno = EMSGSIZE;
225 return (-1);
227 break;
229 default:
230 errno = EMSGSIZE;
231 return (-1); /* flag error */
234 *dstp = '\0';
235 if (len < 0)
236 len = srcp - src;
237 return (len);
242 * ns_name_uncompress_vlmcsd(msg, eom, src, dst, dstsiz)
243 * Expand compressed domain name to presentation format.
244 * return:
245 * Number of bytes read out of `src', or -1 (with errno set).
246 * note:
247 * Root domain returns as "." not "".
250 ns_name_uncompress_vlmcsd(const uint8_t *msg, const uint8_t *eom, const uint8_t *src,
251 char *dst, size_t dstsiz)
253 uint8_t tmp[NS_MAXCDNAME];
254 int n;
256 if ((n = ns_name_unpack_vlmcsd(msg, eom, src, tmp, sizeof tmp)) == -1)
257 return (-1);
258 if (ns_name_ntop_vlmcsd(tmp, dst, dstsiz) == -1)
259 return (-1);
260 return (n);
264 * special(ch)
265 * Thinking in noninternationalized USASCII (per the DNS spec),
266 * is this characted special ("in need of quoting") ?
267 * return:
268 * boolean.
270 static int
271 special_vlmcsd(int ch) {
272 switch (ch) {
273 case 0x22: /* '"' */
274 case 0x2E: /* '.' */
275 case 0x3B: /* ';' */
276 case 0x5C: /* '\\' */
277 case 0x28: /* '(' */
278 case 0x29: /* ')' */
279 /* Special modifiers in zone files. */
280 case 0x40: /* '@' */
281 case 0x24: /* '$' */
282 return (1);
283 default:
284 return (0);
289 * printable(ch)
290 * Thinking in noninternationalized USASCII (per the DNS spec),
291 * is this character visible and not a space when printed ?
292 * return:
293 * boolean.
295 static int
296 printable_vlmcsd(int ch) {
297 return (ch > 0x20 && ch < 0x7f);
300 static int
301 decode_bitstring_vlmcsd(const char **cpp, char *dn, const char *eom)
303 const char *cp = *cpp;
304 char *beg = dn, tc;
305 int b, blen, plen;
307 if ((blen = (*cp & 0xff)) == 0)
308 blen = 256;
309 plen = (blen + 3) / 4;
310 plen += sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1);
311 if (dn + plen >= eom)
312 return(-1);
314 cp++;
315 dn += SPRINTF((dn, "\\[x"));
316 for (b = blen; b > 7; b -= 8, cp++)
317 dn += SPRINTF((dn, "%02x", *cp & 0xff));
318 if (b > 4) {
319 tc = *cp++;
320 dn += SPRINTF((dn, "%02x", tc & (0xff << (8 - b))));
321 } else if (b > 0) {
322 tc = *cp++;
323 dn += SPRINTF((dn, "%1x",
324 ((tc >> 4) & 0x0f) & (0x0f << (4 - b))));
326 dn += SPRINTF((dn, "/%d]", blen));
328 *cpp = cp;
329 return(dn - beg);
332 static int
333 labellen_vlmcsd(const uint8_t *lp)
335 int bitlen;
336 uint8_t l = *lp;
338 if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
339 /* should be avoided by the caller */
340 return(-1);
343 if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT) {
344 if (l == DNS_LABELTYPE_BITSTRING) {
345 if ((bitlen = *(lp + 1)) == 0)
346 bitlen = 256;
347 return((bitlen + 7 ) / 8 + 1);
349 return(-1); /* unknwon ELT */
351 return(l);
354 #endif // !NO_DNS
355 #endif // DNS_PARSER_INTERNAL