explorerframe/tests: Initialize hit test point before first use (Valgrind).
[wine.git] / dlls / dnsapi / ns_parse.c
blobbfb83c249e931101240c3320a2ce2495f2893a63
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 #include "windef.h"
39 /* Forward. */
41 static void setsection(ns_msg *msg, ns_sect sect);
43 /* Macros. */
45 #define RETERR(err) do { return (-1); } while (0)
47 #ifdef HAVE_NS_MSG__MSG_PTR
48 # define NS_PTR(ns_msg) ((ns_msg)->_msg_ptr)
49 #else
50 # define NS_PTR(ns_msg) ((ns_msg)->_ptr)
51 #endif
53 #define DNS_NS_GET16(s, cp) do { \
54 register const u_char *t_cp = (cp); \
55 (s) = ((WORD)t_cp[0] << 8) \
56 | ((WORD)t_cp[1]) \
57 ; \
58 (cp) += NS_INT16SZ; \
59 } while (0)
61 #define DNS_NS_GET32(l, cp) do { \
62 register const u_char *t_cp = (cp); \
63 (l) = ((DWORD)t_cp[0] << 24) \
64 | ((DWORD)t_cp[1] << 16) \
65 | ((DWORD)t_cp[2] << 8) \
66 | ((DWORD)t_cp[3]) \
67 ; \
68 (cp) += NS_INT32SZ; \
69 } while (0)
71 /* Public. */
73 static int
74 dns_ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
75 const u_char *optr = ptr;
77 while (count-- > 0) {
78 int b, rdlength;
80 b = dn_skipname(ptr, eom);
81 if (b < 0)
82 RETERR(EMSGSIZE);
83 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
84 if (section != ns_s_qd) {
85 if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
86 RETERR(EMSGSIZE);
87 ptr += NS_INT32SZ/*TTL*/;
88 DNS_NS_GET16(rdlength, ptr);
89 ptr += rdlength/*RData*/;
92 if (ptr > eom)
93 RETERR(EMSGSIZE);
94 return (ptr - optr);
97 int
98 dns_ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
99 const u_char *eom = msg + msglen;
100 int i;
102 memset(handle, 0x5e, sizeof *handle);
103 handle->_msg = msg;
104 handle->_eom = eom;
105 if (msg + NS_INT16SZ > eom)
106 RETERR(EMSGSIZE);
107 DNS_NS_GET16(handle->_id, msg);
108 if (msg + NS_INT16SZ > eom)
109 RETERR(EMSGSIZE);
110 DNS_NS_GET16(handle->_flags, msg);
111 for (i = 0; i < ns_s_max; i++) {
112 if (msg + NS_INT16SZ > eom)
113 RETERR(EMSGSIZE);
114 DNS_NS_GET16(handle->_counts[i], msg);
116 for (i = 0; i < ns_s_max; i++)
117 if (handle->_counts[i] == 0)
118 handle->_sections[i] = NULL;
119 else {
120 int b = dns_ns_skiprr(msg, eom, (ns_sect)i,
121 handle->_counts[i]);
123 if (b < 0)
124 return (-1);
125 handle->_sections[i] = msg;
126 msg += b;
128 if (msg != eom)
129 RETERR(EMSGSIZE);
130 setsection(handle, ns_s_max);
131 return (0);
135 dns_ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
136 int b;
138 /* Make section right. */
139 if (section >= ns_s_max)
140 RETERR(ENODEV);
141 if (section != handle->_sect)
142 setsection(handle, section);
144 /* Make rrnum right. */
145 if (rrnum == -1)
146 rrnum = handle->_rrnum;
147 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
148 RETERR(ENODEV);
149 if (rrnum < handle->_rrnum)
150 setsection(handle, section);
151 if (rrnum > handle->_rrnum) {
152 b = dns_ns_skiprr(NS_PTR(handle), handle->_eom, section,
153 rrnum - handle->_rrnum);
155 if (b < 0)
156 return (-1);
157 NS_PTR(handle) += b;
158 handle->_rrnum = rrnum;
161 /* Do the parse. */
162 b = dn_expand(handle->_msg, handle->_eom,
163 NS_PTR(handle), rr->name, NS_MAXDNAME);
164 if (b < 0)
165 return (-1);
166 NS_PTR(handle) += b;
167 if (NS_PTR(handle) + NS_INT16SZ + NS_INT16SZ > handle->_eom)
168 RETERR(EMSGSIZE);
169 DNS_NS_GET16(rr->type, NS_PTR(handle));
170 DNS_NS_GET16(rr->rr_class, NS_PTR(handle));
171 if (section == ns_s_qd) {
172 rr->ttl = 0;
173 rr->rdlength = 0;
174 rr->rdata = NULL;
175 } else {
176 if (NS_PTR(handle) + NS_INT32SZ + NS_INT16SZ > handle->_eom)
177 RETERR(EMSGSIZE);
178 DNS_NS_GET32(rr->ttl, NS_PTR(handle));
179 DNS_NS_GET16(rr->rdlength, NS_PTR(handle));
180 if (NS_PTR(handle) + rr->rdlength > handle->_eom)
181 RETERR(EMSGSIZE);
182 rr->rdata = NS_PTR(handle);
183 NS_PTR(handle) += rr->rdlength;
185 if (++handle->_rrnum > handle->_counts[(int)section])
186 setsection(handle, (ns_sect)((int)section + 1));
188 /* All done. */
189 return (0);
192 /* Private. */
194 static void
195 setsection(ns_msg *msg, ns_sect sect) {
196 msg->_sect = sect;
197 if (sect == ns_s_max) {
198 msg->_rrnum = -1;
199 NS_PTR(msg) = NULL;
200 } else {
201 msg->_rrnum = 0;
202 NS_PTR(msg) = msg->_sections[(int)sect];
206 #endif