d3d9: Add a test for GetContainer (Volumes & Surfaces).
[wine/wine64.git] / dlls / dnsapi / ns_parse.c
blob9c386bc6d575cd6bb272cf9aa360fe9f811b469b
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.
18 #include "config.h"
20 #ifdef HAVE_RESOLV
22 #include <sys/types.h>
24 #ifdef HAVE_NETINET_IN_H
25 # include <netinet/in.h>
26 #endif
27 #ifdef HAVE_ARPA_NAMESER_H
28 # include <arpa/nameser.h>
29 #endif
31 #include <errno.h>
32 #ifdef HAVE_RESOLV_H
33 # include <resolv.h>
34 #endif
35 #include <string.h>
37 /* Forward. */
39 static void setsection(ns_msg *msg, ns_sect sect);
41 /* Macros. */
43 #define RETERR(err) do { return (-1); } while (0)
45 #ifdef HAVE_NS_MSG__MSG_PTR
46 # define NS_PTR(ns_msg) ((ns_msg)->_msg_ptr)
47 #else
48 # define NS_PTR(ns_msg) ((ns_msg)->_ptr)
49 #endif
51 /* Public. */
53 static int
54 dns_ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
55 const u_char *optr = ptr;
57 for ((void)NULL; count > 0; count--) {
58 int b, rdlength;
60 b = dn_skipname(ptr, eom);
61 if (b < 0)
62 RETERR(EMSGSIZE);
63 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
64 if (section != ns_s_qd) {
65 if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
66 RETERR(EMSGSIZE);
67 ptr += NS_INT32SZ/*TTL*/;
68 NS_GET16(rdlength, ptr);
69 ptr += rdlength/*RData*/;
72 if (ptr > eom)
73 RETERR(EMSGSIZE);
74 return (ptr - optr);
77 int
78 dns_ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
79 const u_char *eom = msg + msglen;
80 int i;
82 memset(handle, 0x5e, sizeof *handle);
83 handle->_msg = msg;
84 handle->_eom = eom;
85 if (msg + NS_INT16SZ > eom)
86 RETERR(EMSGSIZE);
87 NS_GET16(handle->_id, msg);
88 if (msg + NS_INT16SZ > eom)
89 RETERR(EMSGSIZE);
90 NS_GET16(handle->_flags, msg);
91 for (i = 0; i < ns_s_max; i++) {
92 if (msg + NS_INT16SZ > eom)
93 RETERR(EMSGSIZE);
94 NS_GET16(handle->_counts[i], msg);
96 for (i = 0; i < ns_s_max; i++)
97 if (handle->_counts[i] == 0)
98 handle->_sections[i] = NULL;
99 else {
100 int b = dns_ns_skiprr(msg, eom, (ns_sect)i,
101 handle->_counts[i]);
103 if (b < 0)
104 return (-1);
105 handle->_sections[i] = msg;
106 msg += b;
108 if (msg != eom)
109 RETERR(EMSGSIZE);
110 setsection(handle, ns_s_max);
111 return (0);
115 dns_ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
116 int b;
118 /* Make section right. */
119 if (section < 0 || section >= ns_s_max)
120 RETERR(ENODEV);
121 if (section != handle->_sect)
122 setsection(handle, section);
124 /* Make rrnum right. */
125 if (rrnum == -1)
126 rrnum = handle->_rrnum;
127 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
128 RETERR(ENODEV);
129 if (rrnum < handle->_rrnum)
130 setsection(handle, section);
131 if (rrnum > handle->_rrnum) {
132 b = dns_ns_skiprr(NS_PTR(handle), handle->_eom, section,
133 rrnum - handle->_rrnum);
135 if (b < 0)
136 return (-1);
137 NS_PTR(handle) += b;
138 handle->_rrnum = rrnum;
141 /* Do the parse. */
142 b = dn_expand(handle->_msg, handle->_eom,
143 NS_PTR(handle), rr->name, NS_MAXDNAME);
144 if (b < 0)
145 return (-1);
146 NS_PTR(handle) += b;
147 if (NS_PTR(handle) + NS_INT16SZ + NS_INT16SZ > handle->_eom)
148 RETERR(EMSGSIZE);
149 NS_GET16(rr->type, NS_PTR(handle));
150 NS_GET16(rr->rr_class, NS_PTR(handle));
151 if (section == ns_s_qd) {
152 rr->ttl = 0;
153 rr->rdlength = 0;
154 rr->rdata = NULL;
155 } else {
156 if (NS_PTR(handle) + NS_INT32SZ + NS_INT16SZ > handle->_eom)
157 RETERR(EMSGSIZE);
158 NS_GET32(rr->ttl, NS_PTR(handle));
159 NS_GET16(rr->rdlength, NS_PTR(handle));
160 if (NS_PTR(handle) + rr->rdlength > handle->_eom)
161 RETERR(EMSGSIZE);
162 rr->rdata = NS_PTR(handle);
163 NS_PTR(handle) += rr->rdlength;
165 if (++handle->_rrnum > handle->_counts[(int)section])
166 setsection(handle, (ns_sect)((int)section + 1));
168 /* All done. */
169 return (0);
172 /* Private. */
174 static void
175 setsection(ns_msg *msg, ns_sect sect) {
176 msg->_sect = sect;
177 if (sect == ns_s_max) {
178 msg->_rrnum = -1;
179 NS_PTR(msg) = NULL;
180 } else {
181 msg->_rrnum = 0;
182 NS_PTR(msg) = msg->_sections[(int)sect];
186 #endif