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