mlang/tests: Win64 printf format warning fixes.
[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 #ifdef HAVE_RESOLV
21
22 #include <sys/types.h>
23
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
30
31 #include <errno.h>
32 #ifdef HAVE_RESOLV_H
33 # include <resolv.h>
34 #endif
35 #include <string.h>
36
37 /* Forward. */
38
39 static void     setsection(ns_msg *msg, ns_sect sect);
40
41 /* Macros. */
42
43 #define RETERR(err) do { return (-1); } while (0)
44
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
50
51 #define DNS_NS_GET16(s, cp) do { \
52     register const u_char *t_cp = (const u_char *)(cp); \
53     (s) = ((u_int16_t)t_cp[0] << 8) \
54         | ((u_int16_t)t_cp[1]) \
55         ; \
56     (cp) += NS_INT16SZ; \
57 } while (0)
58
59 #define DNS_NS_GET32(l, cp) do { \
60     register const u_char *t_cp = (const u_char *)(cp); \
61     (l) = ((u_int32_t)t_cp[0] << 24) \
62         | ((u_int32_t)t_cp[1] << 16) \
63         | ((u_int32_t)t_cp[2] << 8) \
64         | ((u_int32_t)t_cp[3]) \
65         ; \
66     (cp) += NS_INT32SZ; \
67 } while (0)
68
69 /* Public. */
70
71 static int
72 dns_ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
73         const u_char *optr = ptr;
74
75         for ((void)NULL; count > 0; count--) {
76                 int b, rdlength;
77
78                 b = dn_skipname(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) {
83                         if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
84                                 RETERR(EMSGSIZE);
85                         ptr += NS_INT32SZ/*TTL*/;
86                         DNS_NS_GET16(rdlength, ptr);
87                         ptr += rdlength/*RData*/;
88                 }
89         }
90         if (ptr > eom)
91                 RETERR(EMSGSIZE);
92         return (ptr - optr);
93 }
94
95 int
96 dns_ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
97         const u_char *eom = msg + msglen;
98         int i;
99
100         memset(handle, 0x5e, sizeof *handle);
101         handle->_msg = msg;
102         handle->_eom = eom;
103         if (msg + NS_INT16SZ > eom)
104                 RETERR(EMSGSIZE);
105         DNS_NS_GET16(handle->_id, msg);
106         if (msg + NS_INT16SZ > eom)
107                 RETERR(EMSGSIZE);
108         DNS_NS_GET16(handle->_flags, msg);
109         for (i = 0; i < ns_s_max; i++) {
110                 if (msg + NS_INT16SZ > eom)
111                         RETERR(EMSGSIZE);
112                 DNS_NS_GET16(handle->_counts[i], msg);
113         }
114         for (i = 0; i < ns_s_max; i++)
115                 if (handle->_counts[i] == 0)
116                         handle->_sections[i] = NULL;
117                 else {
118                         int b = dns_ns_skiprr(msg, eom, (ns_sect)i,
119                                           handle->_counts[i]);
120
121                         if (b < 0)
122                                 return (-1);
123                         handle->_sections[i] = msg;
124                         msg += b;
125                 }
126         if (msg != eom)
127                 RETERR(EMSGSIZE);
128         setsection(handle, ns_s_max);
129         return (0);
130 }
131
132 int
133 dns_ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
134         int b;
135
136         /* Make section right. */
137         if (section < 0 || section >= ns_s_max)
138                 RETERR(ENODEV);
139         if (section != handle->_sect)
140                 setsection(handle, section);
141
142         /* Make rrnum right. */
143         if (rrnum == -1)
144                 rrnum = handle->_rrnum;
145         if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
146                 RETERR(ENODEV);
147         if (rrnum < handle->_rrnum)
148                 setsection(handle, section);
149         if (rrnum > handle->_rrnum) {
150                 b = dns_ns_skiprr(NS_PTR(handle), handle->_eom, section,
151                               rrnum - handle->_rrnum);
152
153                 if (b < 0)
154                         return (-1);
155                 NS_PTR(handle) += b;
156                 handle->_rrnum = rrnum;
157         }
158
159         /* Do the parse. */
160         b = dn_expand(handle->_msg, handle->_eom,
161                       NS_PTR(handle), rr->name, NS_MAXDNAME);
162         if (b < 0)
163                 return (-1);
164         NS_PTR(handle) += b;
165         if (NS_PTR(handle) + NS_INT16SZ + NS_INT16SZ > handle->_eom)
166                 RETERR(EMSGSIZE);
167         DNS_NS_GET16(rr->type, NS_PTR(handle));
168         DNS_NS_GET16(rr->rr_class, NS_PTR(handle));
169         if (section == ns_s_qd) {
170                 rr->ttl = 0;
171                 rr->rdlength = 0;
172                 rr->rdata = NULL;
173         } else {
174                 if (NS_PTR(handle) + NS_INT32SZ + NS_INT16SZ > handle->_eom)
175                         RETERR(EMSGSIZE);
176                 DNS_NS_GET32(rr->ttl, NS_PTR(handle));
177                 DNS_NS_GET16(rr->rdlength, NS_PTR(handle));
178                 if (NS_PTR(handle) + rr->rdlength > handle->_eom)
179                         RETERR(EMSGSIZE);
180                 rr->rdata = NS_PTR(handle);
181                 NS_PTR(handle) += rr->rdlength;
182         }
183         if (++handle->_rrnum > handle->_counts[(int)section])
184                 setsection(handle, (ns_sect)((int)section + 1));
185
186         /* All done. */
187         return (0);
188 }
189
190 /* Private. */
191
192 static void
193 setsection(ns_msg *msg, ns_sect sect) {
194         msg->_sect = sect;
195         if (sect == ns_s_max) {
196                 msg->_rrnum = -1;
197                 NS_PTR(msg) = NULL;
198         } else {
199                 msg->_rrnum = 0;
200                 NS_PTR(msg) = msg->_sections[(int)sect];
201         }
202 }
203
204 #endif