modified: myjupyterlab.sh
[GalaxyCodeBases.git] / etc / Windows / vlmcsd_old_vancepym / ns_parse.c
blobb0874c1da0d50a9de06bebe4fc44e924bd51a206
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 /* Import. */
32 #include <sys/types.h>
33 #include <errno.h>
34 #include <string.h>
36 #include "types.h"
37 #include "endian.h"
38 #include "ns_name.h"
39 #include "ns_parse.h"
41 /* Macros. */
43 #define NS_GET16_VLMCSD(s, cp) do { \
44 (s) = GET_UA16BE(cp); \
45 (cp) += NS_INT16SZ; \
46 } while (0)
48 #define NS_GET32_VLMCSD(l, cp) do { \
49 (l) = GET_UA32BE(cp); \
50 (cp) += NS_INT32SZ; \
51 } while (0)
53 #define RETERR(err) do { errno = (err); return (-1); } while (0)
55 /* Forward. */
57 static void setsection_vlmcsd(ns_msg_vlmcsd *msg, ns_sect_vlmcsd sect);
60 static int dn_skipname_vlmcsd(const unsigned char *s, const unsigned char *end)
62 const unsigned char *p;
63 for (p=s; p<end; p++)
64 if (!*p) return p-s+1;
65 else if (*p>=192)
66 {if (p+1<end) return p-s+2;
67 else break;}
68 return -1;
71 static int
72 ns_skiprr_vlmcsd(const uint8_t *ptr, const uint8_t *eom, ns_sect_vlmcsd section, int count) {
73 const uint8_t *optr = ptr;
75 for ((void)NULL; count > 0; count--) {
76 int b, rdlength;
78 b = dn_skipname_vlmcsd(ptr, eom);
79 if (b < 0)
80 RETERR(EMSGSIZE);
81 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
82 if (section != ns_s_qd_vlmcsd) {
83 if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
84 RETERR(EMSGSIZE);
85 ptr += NS_INT32SZ/*TTL*/;
86 NS_GET16_VLMCSD(rdlength, ptr);
87 ptr += rdlength/*RData*/;
90 if (ptr > eom)
91 RETERR(EMSGSIZE);
92 return (ptr - optr);
95 int
96 ns_initparse_vlmcsd(const uint8_t *msg, int msglen, ns_msg_vlmcsd *handle) {
97 const uint8_t *eom = msg + msglen;
98 int i;
100 memset(handle, 0x5e, sizeof *handle);
101 handle->_msg = msg;
102 handle->_eom = eom;
103 if (msg + NS_INT16SZ > eom)
104 RETERR(EMSGSIZE);
105 NS_GET16_VLMCSD(handle->_id, msg);
106 if (msg + NS_INT16SZ > eom)
107 RETERR(EMSGSIZE);
108 NS_GET16_VLMCSD(handle->_flags, msg);
109 for (i = 0; i < ns_s_max_vlmcsd; i++) {
110 if (msg + NS_INT16SZ > eom)
111 RETERR(EMSGSIZE);
112 NS_GET16_VLMCSD(handle->_counts[i], msg);
114 for (i = 0; i < ns_s_max_vlmcsd; i++)
115 if (handle->_counts[i] == 0)
116 handle->_sections[i] = NULL;
117 else {
118 int b = ns_skiprr_vlmcsd(msg, eom, (ns_sect_vlmcsd)i,
119 handle->_counts[i]);
121 if (b < 0)
122 return (-1);
123 handle->_sections[i] = msg;
124 msg += b;
126 if (msg > eom)
127 RETERR(EMSGSIZE);
128 handle->_eom = msg;
129 setsection_vlmcsd(handle, ns_s_max_vlmcsd);
130 return (0);
134 ns_parserr_vlmcsd(ns_msg_vlmcsd *handle, ns_sect_vlmcsd section, int rrnum, ns_rr_vlmcsd *rr) {
135 int b;
137 /* Make section right. */
138 if (section >= ns_s_max_vlmcsd)
139 RETERR(ENODEV);
140 if (section != handle->_sect)
141 setsection_vlmcsd(handle, section);
143 /* Make rrnum right. */
144 if (rrnum == -1)
145 rrnum = handle->_rrnum;
146 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
147 RETERR(ENODEV);
148 if (rrnum < handle->_rrnum)
149 setsection_vlmcsd(handle, section);
150 if (rrnum > handle->_rrnum) {
151 b = ns_skiprr_vlmcsd(handle->_msg_ptr, handle->_eom, section,
152 rrnum - handle->_rrnum);
154 if (b < 0)
155 return (-1);
156 handle->_msg_ptr += b;
157 handle->_rrnum = rrnum;
160 /* Do the parse. */
161 b = ns_name_uncompress_vlmcsd(handle->_msg, handle->_eom,
162 handle->_msg_ptr, rr->name, NS_MAXDNAME);
163 if (b < 0)
164 return (-1);
165 handle->_msg_ptr += b;
166 if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
167 RETERR(EMSGSIZE);
168 NS_GET16_VLMCSD(rr->type, handle->_msg_ptr);
169 NS_GET16_VLMCSD(rr->rr_class, handle->_msg_ptr);
170 if (section == ns_s_qd_vlmcsd) {
171 rr->ttl = 0;
172 rr->rdlength = 0;
173 rr->rdata = NULL;
174 } else {
175 if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
176 RETERR(EMSGSIZE);
177 NS_GET32_VLMCSD(rr->ttl, handle->_msg_ptr);
178 NS_GET16_VLMCSD(rr->rdlength, handle->_msg_ptr);
179 if (handle->_msg_ptr + rr->rdlength > handle->_eom)
180 RETERR(EMSGSIZE);
181 rr->rdata = handle->_msg_ptr;
182 handle->_msg_ptr += rr->rdlength;
184 if (++handle->_rrnum > handle->_counts[(int)section])
185 setsection_vlmcsd(handle, (ns_sect_vlmcsd)((int)section + 1));
187 /* All done. */
188 return (0);
191 /* Private. */
193 static void
194 setsection_vlmcsd(ns_msg_vlmcsd *msg, ns_sect_vlmcsd sect) {
195 msg->_sect = sect;
196 if (sect == ns_s_max_vlmcsd) {
197 msg->_rrnum = -1;
198 msg->_msg_ptr = NULL;
199 } else {
200 msg->_rrnum = 0;
201 msg->_msg_ptr = msg->_sections[(int)sect];
205 #endif // !NO_DNS
206 #endif // DNS_PARSER_INTERNAL