kernel32/tests: Added more FoldString tests.
[wine/multimedia.git] / dlls / dnsapi / ns_parse.c
blobdf4933a82ad8929006b546023af433e24d827a88
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 <stdarg.h>
36 #include <string.h>
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winnls.h"
42 #include "dnsapi.h"
44 /* Forward. */
46 static void setsection(ns_msg *msg, ns_sect sect);
48 /* Macros. */
50 #define RETERR(err) do { return (-1); } while (0)
52 #ifdef HAVE_NS_MSG__MSG_PTR
53 # define NS_PTR(ns_msg) ((ns_msg)->_msg_ptr)
54 #else
55 # define NS_PTR(ns_msg) ((ns_msg)->_ptr)
56 #endif
58 #define DNS_NS_GET16(s, cp) do { \
59 register const u_char *t_cp = (cp); \
60 (s) = ((WORD)t_cp[0] << 8) \
61 | ((WORD)t_cp[1]) \
62 ; \
63 (cp) += NS_INT16SZ; \
64 } while (0)
66 #define DNS_NS_GET32(l, cp) do { \
67 register const u_char *t_cp = (cp); \
68 (l) = ((DWORD)t_cp[0] << 24) \
69 | ((DWORD)t_cp[1] << 16) \
70 | ((DWORD)t_cp[2] << 8) \
71 | ((DWORD)t_cp[3]) \
72 ; \
73 (cp) += NS_INT32SZ; \
74 } while (0)
76 /* Public. */
78 static int
79 dns_ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
80 const u_char *optr = ptr;
82 while (count-- > 0) {
83 int rdlength;
85 if (dns_ns_name_skip(&ptr, eom) < 0)
86 RETERR(EMSGSIZE);
87 ptr += NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
88 if (section != ns_s_qd) {
89 if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
90 RETERR(EMSGSIZE);
91 ptr += NS_INT32SZ/*TTL*/;
92 DNS_NS_GET16(rdlength, ptr);
93 ptr += rdlength/*RData*/;
96 if (ptr > eom)
97 RETERR(EMSGSIZE);
98 return (ptr - optr);
102 dns_ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
103 const u_char *eom = msg + msglen;
104 int i;
106 memset(handle, 0x5e, sizeof *handle);
107 handle->_msg = msg;
108 handle->_eom = eom;
109 if (msg + NS_INT16SZ > eom)
110 RETERR(EMSGSIZE);
111 DNS_NS_GET16(handle->_id, msg);
112 if (msg + NS_INT16SZ > eom)
113 RETERR(EMSGSIZE);
114 DNS_NS_GET16(handle->_flags, msg);
115 for (i = 0; i < ns_s_max; i++) {
116 if (msg + NS_INT16SZ > eom)
117 RETERR(EMSGSIZE);
118 DNS_NS_GET16(handle->_counts[i], msg);
120 for (i = 0; i < ns_s_max; i++)
121 if (handle->_counts[i] == 0)
122 handle->_sections[i] = NULL;
123 else {
124 int b = dns_ns_skiprr(msg, eom, (ns_sect)i,
125 handle->_counts[i]);
127 if (b < 0)
128 return (-1);
129 handle->_sections[i] = msg;
130 msg += b;
132 if (msg != eom)
133 RETERR(EMSGSIZE);
134 setsection(handle, ns_s_max);
135 return (0);
139 dns_ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
140 int b;
142 /* Make section right. */
143 if (section >= ns_s_max)
144 RETERR(ENODEV);
145 if (section != handle->_sect)
146 setsection(handle, section);
148 /* Make rrnum right. */
149 if (rrnum == -1)
150 rrnum = handle->_rrnum;
151 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
152 RETERR(ENODEV);
153 if (rrnum < handle->_rrnum)
154 setsection(handle, section);
155 if (rrnum > handle->_rrnum) {
156 b = dns_ns_skiprr(NS_PTR(handle), handle->_eom, section,
157 rrnum - handle->_rrnum);
159 if (b < 0)
160 return (-1);
161 NS_PTR(handle) += b;
162 handle->_rrnum = rrnum;
165 /* Do the parse. */
166 b = dn_expand(handle->_msg, handle->_eom,
167 NS_PTR(handle), rr->name, NS_MAXDNAME);
168 if (b < 0)
169 return (-1);
170 NS_PTR(handle) += b;
171 if (NS_PTR(handle) + NS_INT16SZ + NS_INT16SZ > handle->_eom)
172 RETERR(EMSGSIZE);
173 DNS_NS_GET16(rr->type, NS_PTR(handle));
174 DNS_NS_GET16(rr->rr_class, NS_PTR(handle));
175 if (section == ns_s_qd) {
176 rr->ttl = 0;
177 rr->rdlength = 0;
178 rr->rdata = NULL;
179 } else {
180 if (NS_PTR(handle) + NS_INT32SZ + NS_INT16SZ > handle->_eom)
181 RETERR(EMSGSIZE);
182 DNS_NS_GET32(rr->ttl, NS_PTR(handle));
183 DNS_NS_GET16(rr->rdlength, NS_PTR(handle));
184 if (NS_PTR(handle) + rr->rdlength > handle->_eom)
185 RETERR(EMSGSIZE);
186 rr->rdata = NS_PTR(handle);
187 NS_PTR(handle) += rr->rdlength;
189 if (++handle->_rrnum > handle->_counts[(int)section])
190 setsection(handle, (ns_sect)((int)section + 1));
192 /* All done. */
193 return (0);
196 /* Private. */
198 static void
199 setsection(ns_msg *msg, ns_sect sect) {
200 msg->_sect = sect;
201 if (sect == ns_s_max) {
202 msg->_rrnum = -1;
203 NS_PTR(msg) = NULL;
204 } else {
205 msg->_rrnum = 0;
206 NS_PTR(msg) = msg->_sections[(int)sect];
210 #endif