2 * Copyright (c) 2009, Sun Microsystems, Inc.
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.
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.
30 * xdr_mem.h, XDR implementation using memory buffers.
32 * Copyright (C) 1984, Sun Microsystems, Inc.
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.
40 #include <sys/types.h>
44 #include <rpc/types.h>
47 #include "xdr_private.h"
50 # define ntohl(x) xdr_ntohl(x)
53 # define htonl(x) xdr_htonl(x)
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 *);
73 static const struct xdr_ops xdrmem_ops_aligned
= {
74 xdrmem_getlong_aligned
,
75 xdrmem_putlong_aligned
,
80 xdrmem_inline_aligned
,
82 xdrmem_getint32_aligned
,
83 xdrmem_putint32_aligned
86 static const struct xdr_ops xdrmem_ops_unaligned
= {
87 xdrmem_getlong_unaligned
,
88 xdrmem_putlong_unaligned
,
93 xdrmem_inline_unaligned
,
95 xdrmem_getint32_unaligned
,
96 xdrmem_putint32_unaligned
100 * The procedure xdrmem_create initializes a stream descriptor for a
104 xdrmem_create (XDR
* xdrs
,
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
;
118 xdrmem_destroy (XDR
* xdrs
)
123 xdrmem_getlong_aligned (XDR
* xdrs
,
126 if (xdrs
->x_handy
< sizeof (int32_t))
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);
135 xdrmem_putlong_aligned (XDR
* xdrs
,
138 if (xdrs
->x_handy
< sizeof (int32_t))
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);
147 xdrmem_getlong_unaligned (XDR
* xdrs
,
152 if (xdrs
->x_handy
< sizeof (int32_t))
154 xdrs
->x_handy
-= sizeof (int32_t);
155 memmove (&l
, xdrs
->x_private
, sizeof (int32_t));
157 xdrs
->x_private
= (char *) xdrs
->x_private
+ sizeof (int32_t);
162 xdrmem_putlong_unaligned (XDR
* xdrs
,
167 if (xdrs
->x_handy
< sizeof (int32_t))
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);
177 xdrmem_getbytes (XDR
* xdrs
,
181 if (xdrs
->x_handy
< len
)
183 xdrs
->x_handy
-= len
;
184 memmove (addr
, xdrs
->x_private
, len
);
185 xdrs
->x_private
= (char *) xdrs
->x_private
+ len
;
190 xdrmem_putbytes (XDR
* xdrs
,
194 if (xdrs
->x_handy
< len
)
196 xdrs
->x_handy
-= len
;
197 memmove (xdrs
->x_private
, addr
, len
);
198 xdrs
->x_private
= (char *) xdrs
->x_private
+ len
;
203 xdrmem_getpos (XDR
* xdrs
)
205 /* XXX w/64-bit pointers, u_int not enough! */
206 return (u_int
) ((u_long
) xdrs
->x_private
- (u_long
) xdrs
->x_base
);
210 xdrmem_setpos (XDR
* xdrs
,
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
;
217 if (newaddr
> lastaddr
218 || newaddr
< xdrs
->x_base
219 || handy
!= (u_int
) handy
)
222 xdrs
->x_private
= newaddr
;
223 xdrs
->x_handy
= (u_int
) handy
;
224 /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
229 xdrmem_inline_aligned (XDR
* xdrs
,
234 if (xdrs
->x_handy
>= len
)
236 xdrs
->x_handy
-= len
;
237 buf
= (int32_t *) xdrs
->x_private
;
238 xdrs
->x_private
= (char *) xdrs
->x_private
+ len
;
244 xdrmem_inline_unaligned (XDR
* xdrs
,
251 xdrmem_getint32_aligned (XDR
*xdrs
,
254 if (xdrs
->x_handy
< sizeof(int32_t))
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);
263 xdrmem_putint32_aligned (XDR
*xdrs
,
266 if (xdrs
->x_handy
< sizeof(int32_t))
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);
275 xdrmem_getint32_unaligned (XDR
*xdrs
,
280 if (xdrs
->x_handy
< sizeof(int32_t))
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);
290 xdrmem_putint32_unaligned (XDR
*xdrs
,
295 if (xdrs
->x_handy
< sizeof(int32_t))
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);