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.
29 #include <sys/cdefs.h>
32 * xdr_mem.h, XDR implementation using memory buffers.
34 * Copyright (C) 1984, Sun Microsystems, Inc.
36 * If you have some data to be interpreted as external data representation
37 * or to be converted to external data representation in a memory buffer,
38 * then this is the package for you.
42 #include <sys/types.h>
44 #include <netinet/in.h>
48 #include <rpc/types.h>
50 #include "un-namespace.h"
52 static void xdrmem_destroy(XDR *);
53 static bool_t xdrmem_getlong_aligned(XDR *, long *);
54 static bool_t xdrmem_putlong_aligned(XDR *, const long *);
55 static bool_t xdrmem_getlong_unaligned(XDR *, long *);
56 static bool_t xdrmem_putlong_unaligned(XDR *, const long *);
57 static bool_t xdrmem_getbytes(XDR *, char *, u_int);
58 static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
59 /* XXX: w/64-bit pointers, u_int not enough! */
60 static u_int xdrmem_getpos(XDR *);
61 static bool_t xdrmem_setpos(XDR *, u_int);
62 static int32_t *xdrmem_inline_aligned(XDR *, u_int);
63 static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
65 static const struct xdr_ops xdrmem_ops_aligned = {
66 xdrmem_getlong_aligned,
67 xdrmem_putlong_aligned,
72 xdrmem_inline_aligned,
76 static const struct xdr_ops xdrmem_ops_unaligned = {
77 xdrmem_getlong_unaligned,
78 xdrmem_putlong_unaligned,
83 xdrmem_inline_unaligned,
88 * The procedure xdrmem_create initializes a stream descriptor for a
92 xdrmem_create(xdrs, addr, size, op)
100 xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
101 ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
102 xdrs->x_private = xdrs->x_base = addr;
103 xdrs->x_handy = size;
115 xdrmem_getlong_aligned(xdrs, lp)
120 if (xdrs->x_handy < sizeof(int32_t))
122 xdrs->x_handy -= sizeof(int32_t);
123 *lp = ntohl(*(u_int32_t *)xdrs->x_private);
124 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
129 xdrmem_putlong_aligned(xdrs, lp)
134 if (xdrs->x_handy < sizeof(int32_t))
136 xdrs->x_handy -= sizeof(int32_t);
137 *(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp);
138 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
143 xdrmem_getlong_unaligned(xdrs, lp)
149 if (xdrs->x_handy < sizeof(int32_t))
151 xdrs->x_handy -= sizeof(int32_t);
152 memmove(&l, xdrs->x_private, sizeof(int32_t));
154 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
159 xdrmem_putlong_unaligned(xdrs, lp)
165 if (xdrs->x_handy < sizeof(int32_t))
167 xdrs->x_handy -= sizeof(int32_t);
168 l = htonl((u_int32_t)*lp);
169 memmove(xdrs->x_private, &l, sizeof(int32_t));
170 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
175 xdrmem_getbytes(xdrs, addr, len)
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(xdrs, addr, len)
196 if (xdrs->x_handy < len)
198 xdrs->x_handy -= len;
199 memmove(xdrs->x_private, addr, len);
200 xdrs->x_private = (char *)xdrs->x_private + len;
209 /* XXX w/64-bit pointers, u_int not enough! */
210 return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
214 xdrmem_setpos(xdrs, pos)
218 char *newaddr = xdrs->x_base + pos;
219 char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
221 if (newaddr > lastaddr)
223 xdrs->x_private = newaddr;
224 xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
229 xdrmem_inline_aligned(xdrs, len)
235 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;
245 xdrmem_inline_unaligned(xdrs, len)