1 | /* |
---|
2 | * Copyright (c) 2009, Sun Microsystems, Inc. |
---|
3 | * All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions are met: |
---|
7 | * - Redistributions of source code must retain the above copyright notice, |
---|
8 | * this list of conditions and the following disclaimer. |
---|
9 | * - Redistributions in binary form must reproduce the above copyright notice, |
---|
10 | * this list of conditions and the following disclaimer in the documentation |
---|
11 | * and/or other materials provided with the distribution. |
---|
12 | * - Neither the name of Sun Microsystems, Inc. nor the names of its |
---|
13 | * contributors may be used to endorse or promote products derived |
---|
14 | * from this software without specific prior written permission. |
---|
15 | * |
---|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
26 | * POSSIBILITY OF SUCH DAMAGE. |
---|
27 | */ |
---|
28 | |
---|
29 | /* |
---|
30 | * xdr_mem.h, XDR implementation using memory buffers. |
---|
31 | * |
---|
32 | * Copyright (C) 1984, Sun Microsystems, Inc. |
---|
33 | * |
---|
34 | * If you have some data to be interpreted as external data representation |
---|
35 | * or to be converted to external data representation in a memory buffer, |
---|
36 | * then this is the package for you. |
---|
37 | * |
---|
38 | */ |
---|
39 | |
---|
40 | #include <sys/types.h> |
---|
41 | #include <string.h> |
---|
42 | #include <limits.h> |
---|
43 | |
---|
44 | #include <rpc/types.h> |
---|
45 | #include <rpc/xdr.h> |
---|
46 | |
---|
47 | #include "xdr_private.h" |
---|
48 | |
---|
49 | #ifndef ntohl |
---|
50 | # define ntohl(x) xdr_ntohl(x) |
---|
51 | #endif |
---|
52 | #ifndef htonl |
---|
53 | # define htonl(x) xdr_htonl(x) |
---|
54 | #endif |
---|
55 | |
---|
56 | static void xdrmem_destroy (XDR *); |
---|
57 | static bool_t xdrmem_getlong_aligned (XDR *, long *); |
---|
58 | static bool_t xdrmem_putlong_aligned (XDR *, const long *); |
---|
59 | static bool_t xdrmem_getlong_unaligned (XDR *, long *); |
---|
60 | static bool_t xdrmem_putlong_unaligned (XDR *, const long *); |
---|
61 | static bool_t xdrmem_getbytes (XDR *, char *, u_int); |
---|
62 | static bool_t xdrmem_putbytes (XDR *, const char *, u_int); |
---|
63 | /* XXX: w/64-bit pointers, u_int not enough! */ |
---|
64 | static u_int xdrmem_getpos (XDR *); |
---|
65 | static bool_t xdrmem_setpos (XDR *, u_int); |
---|
66 | static int32_t * xdrmem_inline_aligned (XDR *, u_int); |
---|
67 | static int32_t * xdrmem_inline_unaligned (XDR *, u_int); |
---|
68 | static bool_t xdrmem_getint32_aligned (XDR *, int32_t *); |
---|
69 | static bool_t xdrmem_putint32_aligned (XDR *, const int32_t *); |
---|
70 | static bool_t xdrmem_getint32_unaligned (XDR *, int32_t *); |
---|
71 | static bool_t xdrmem_putint32_unaligned (XDR *, const int32_t *); |
---|
72 | |
---|
73 | static const struct xdr_ops xdrmem_ops_aligned = { |
---|
74 | xdrmem_getlong_aligned, |
---|
75 | xdrmem_putlong_aligned, |
---|
76 | xdrmem_getbytes, |
---|
77 | xdrmem_putbytes, |
---|
78 | xdrmem_getpos, |
---|
79 | xdrmem_setpos, |
---|
80 | xdrmem_inline_aligned, |
---|
81 | xdrmem_destroy, |
---|
82 | xdrmem_getint32_aligned, |
---|
83 | xdrmem_putint32_aligned |
---|
84 | }; |
---|
85 | |
---|
86 | static const struct xdr_ops xdrmem_ops_unaligned = { |
---|
87 | xdrmem_getlong_unaligned, |
---|
88 | xdrmem_putlong_unaligned, |
---|
89 | xdrmem_getbytes, |
---|
90 | xdrmem_putbytes, |
---|
91 | xdrmem_getpos, |
---|
92 | xdrmem_setpos, |
---|
93 | xdrmem_inline_unaligned, |
---|
94 | xdrmem_destroy, |
---|
95 | xdrmem_getint32_unaligned, |
---|
96 | xdrmem_putint32_unaligned |
---|
97 | }; |
---|
98 | |
---|
99 | /* |
---|
100 | * The procedure xdrmem_create initializes a stream descriptor for a |
---|
101 | * memory buffer. |
---|
102 | */ |
---|
103 | void |
---|
104 | xdrmem_create (XDR * xdrs, |
---|
105 | caddr_t addr, |
---|
106 | u_int size, |
---|
107 | enum xdr_op op) |
---|
108 | { |
---|
109 | xdrs->x_op = op; |
---|
110 | xdrs->x_ops = ((unsigned long)addr & (sizeof (int32_t) - 1)) |
---|
111 | ? (struct xdr_ops *)&xdrmem_ops_unaligned |
---|
112 | : (struct xdr_ops *)&xdrmem_ops_aligned; |
---|
113 | xdrs->x_private = xdrs->x_base = addr; |
---|
114 | xdrs->x_handy = size; |
---|
115 | } |
---|
116 | |
---|
117 | static void |
---|
118 | xdrmem_destroy (XDR * xdrs) |
---|
119 | { |
---|
120 | } |
---|
121 | |
---|
122 | static bool_t |
---|
123 | xdrmem_getlong_aligned (XDR * xdrs, |
---|
124 | long *lp) |
---|
125 | { |
---|
126 | if (xdrs->x_handy < sizeof (int32_t)) |
---|
127 | return FALSE; |
---|
128 | xdrs->x_handy -= sizeof (int32_t); |
---|
129 | *lp = (int32_t) ntohl (*(u_int32_t *) xdrs->x_private); |
---|
130 | xdrs->x_private = (char *) xdrs->x_private + sizeof (int32_t); |
---|
131 | return TRUE; |
---|
132 | } |
---|
133 | |
---|
134 | static bool_t |
---|
135 | xdrmem_putlong_aligned (XDR * xdrs, |
---|
136 | const long *lp) |
---|
137 | { |
---|
138 | if (xdrs->x_handy < sizeof (int32_t)) |
---|
139 | return FALSE; |
---|
140 | xdrs->x_handy -= sizeof (int32_t); |
---|
141 | *(u_int32_t *) xdrs->x_private = htonl ((u_int32_t) * lp); |
---|
142 | xdrs->x_private = (char *) xdrs->x_private + sizeof (int32_t); |
---|
143 | return TRUE; |
---|
144 | } |
---|
145 | |
---|
146 | static bool_t |
---|
147 | xdrmem_getlong_unaligned (XDR * xdrs, |
---|
148 | long *lp) |
---|
149 | { |
---|
150 | u_int32_t l; |
---|
151 | |
---|
152 | if (xdrs->x_handy < sizeof (int32_t)) |
---|
153 | return FALSE; |
---|
154 | xdrs->x_handy -= sizeof (int32_t); |
---|
155 | memmove (&l, xdrs->x_private, sizeof (int32_t)); |
---|
156 | *lp = ntohl (l); |
---|
157 | xdrs->x_private = (char *) xdrs->x_private + sizeof (int32_t); |
---|
158 | return TRUE; |
---|
159 | } |
---|
160 | |
---|
161 | static bool_t |
---|
162 | xdrmem_putlong_unaligned (XDR * xdrs, |
---|
163 | const long *lp) |
---|
164 | { |
---|
165 | u_int32_t l; |
---|
166 | |
---|
167 | if (xdrs->x_handy < sizeof (int32_t)) |
---|
168 | return FALSE; |
---|
169 | xdrs->x_handy -= sizeof (int32_t); |
---|
170 | l = htonl ((u_int32_t) * lp); |
---|
171 | memmove (xdrs->x_private, &l, sizeof (int32_t)); |
---|
172 | xdrs->x_private = (char *) xdrs->x_private + sizeof (int32_t); |
---|
173 | return TRUE; |
---|
174 | } |
---|
175 | |
---|
176 | static bool_t |
---|
177 | xdrmem_getbytes (XDR * xdrs, |
---|
178 | char *addr, |
---|
179 | u_int len) |
---|
180 | { |
---|
181 | if (xdrs->x_handy < len) |
---|
182 | return FALSE; |
---|
183 | xdrs->x_handy -= len; |
---|
184 | memmove (addr, xdrs->x_private, len); |
---|
185 | xdrs->x_private = (char *) xdrs->x_private + len; |
---|
186 | return TRUE; |
---|
187 | } |
---|
188 | |
---|
189 | static bool_t |
---|
190 | xdrmem_putbytes (XDR * xdrs, |
---|
191 | const char *addr, |
---|
192 | u_int len) |
---|
193 | { |
---|
194 | if (xdrs->x_handy < len) |
---|
195 | return FALSE; |
---|
196 | xdrs->x_handy -= len; |
---|
197 | memmove (xdrs->x_private, addr, len); |
---|
198 | xdrs->x_private = (char *) xdrs->x_private + len; |
---|
199 | return TRUE; |
---|
200 | } |
---|
201 | |
---|
202 | static u_int |
---|
203 | xdrmem_getpos (XDR * xdrs) |
---|
204 | { |
---|
205 | /* XXX w/64-bit pointers, u_int not enough! */ |
---|
206 | return (u_int) ((u_long) xdrs->x_private - (u_long) xdrs->x_base); |
---|
207 | } |
---|
208 | |
---|
209 | static bool_t |
---|
210 | xdrmem_setpos (XDR * xdrs, |
---|
211 | u_int pos) |
---|
212 | { |
---|
213 | caddr_t newaddr = xdrs->x_base + pos; |
---|
214 | caddr_t lastaddr = (caddr_t) xdrs->x_private + xdrs->x_handy; |
---|
215 | size_t handy = lastaddr - newaddr; |
---|
216 | |
---|
217 | if (newaddr > lastaddr |
---|
218 | || newaddr < xdrs->x_base |
---|
219 | || handy != (u_int) handy) |
---|
220 | return FALSE; |
---|
221 | |
---|
222 | xdrs->x_private = newaddr; |
---|
223 | xdrs->x_handy = (u_int) handy; |
---|
224 | /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */ |
---|
225 | return TRUE; |
---|
226 | } |
---|
227 | |
---|
228 | static int32_t * |
---|
229 | xdrmem_inline_aligned (XDR * xdrs, |
---|
230 | u_int len) |
---|
231 | { |
---|
232 | int32_t *buf = 0; |
---|
233 | |
---|
234 | if (xdrs->x_handy >= len) |
---|
235 | { |
---|
236 | xdrs->x_handy -= len; |
---|
237 | buf = (int32_t *) xdrs->x_private; |
---|
238 | xdrs->x_private = (char *) xdrs->x_private + len; |
---|
239 | } |
---|
240 | return (buf); |
---|
241 | } |
---|
242 | |
---|
243 | static int32_t * |
---|
244 | xdrmem_inline_unaligned (XDR * xdrs, |
---|
245 | u_int len) |
---|
246 | { |
---|
247 | return (0); |
---|
248 | } |
---|
249 | |
---|
250 | static bool_t |
---|
251 | xdrmem_getint32_aligned (XDR *xdrs, |
---|
252 | int32_t *ip) |
---|
253 | { |
---|
254 | if (xdrs->x_handy < sizeof(int32_t)) |
---|
255 | return FALSE; |
---|
256 | xdrs->x_handy -= sizeof(int32_t); |
---|
257 | *ip = (int32_t) ntohl (*(u_int32_t *) xdrs->x_private); |
---|
258 | xdrs->x_private = (char *) xdrs->x_private + sizeof (int32_t); |
---|
259 | return TRUE; |
---|
260 | } |
---|
261 | |
---|
262 | static bool_t |
---|
263 | xdrmem_putint32_aligned (XDR *xdrs, |
---|
264 | const int32_t *ip) |
---|
265 | { |
---|
266 | if (xdrs->x_handy < sizeof(int32_t)) |
---|
267 | return FALSE; |
---|
268 | xdrs->x_handy -= sizeof(int32_t); |
---|
269 | *(u_int32_t *) xdrs->x_private = htonl ((u_int32_t) * ip); |
---|
270 | xdrs->x_private = (char *) xdrs->x_private + sizeof (int32_t); |
---|
271 | return TRUE; |
---|
272 | } |
---|
273 | |
---|
274 | static bool_t |
---|
275 | xdrmem_getint32_unaligned (XDR *xdrs, |
---|
276 | int32_t *ip) |
---|
277 | { |
---|
278 | u_int32_t l; |
---|
279 | |
---|
280 | if (xdrs->x_handy < sizeof(int32_t)) |
---|
281 | return FALSE; |
---|
282 | xdrs->x_handy -= sizeof(int32_t); |
---|
283 | memmove (&l, xdrs->x_private, sizeof (int32_t)); |
---|
284 | *ip = (int32_t) ntohl (l); |
---|
285 | xdrs->x_private = (char *) xdrs->x_private + sizeof (int32_t); |
---|
286 | return TRUE; |
---|
287 | } |
---|
288 | |
---|
289 | static bool_t |
---|
290 | xdrmem_putint32_unaligned (XDR *xdrs, |
---|
291 | const int32_t *ip) |
---|
292 | { |
---|
293 | u_int32_t l; |
---|
294 | |
---|
295 | if (xdrs->x_handy < sizeof(int32_t)) |
---|
296 | return FALSE; |
---|
297 | xdrs->x_handy -= sizeof(int32_t); |
---|
298 | l = htonl ((u_int32_t) * ip); |
---|
299 | memmove (xdrs->x_private, &l, sizeof (int32_t)); |
---|
300 | xdrs->x_private = (char *) xdrs->x_private + sizeof (int32_t); |
---|
301 | return TRUE; |
---|
302 | } |
---|
303 | |
---|