1 /* $NetBSD: xdr_mem.c,v 1.15 2000/01/22 22:19:18 mycroft Exp $ */
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char *sccsid2 = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
36 static char *sccsid = "@(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * xdr_mem.h, XDR implementation using memory buffers.
44 * If you have some data to be interpreted as external data representation
45 * or to be converted to external data representation in a memory buffer,
46 * then this is the package for you.
50 #include "namespace.h"
51 #include <sys/types.h>
53 #include <netinet/in.h>
57 #include <rpc/types.h>
59 #include "un-namespace.h"
61 static void xdrmem_destroy(XDR *);
62 static bool_t xdrmem_getlong_aligned(XDR *, long *);
63 static bool_t xdrmem_putlong_aligned(XDR *, const long *);
64 static bool_t xdrmem_getlong_unaligned(XDR *, long *);
65 static bool_t xdrmem_putlong_unaligned(XDR *, const long *);
66 static bool_t xdrmem_getbytes(XDR *, char *, u_int);
67 static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
68 /* XXX: w/64-bit pointers, u_int not enough! */
69 static u_int xdrmem_getpos(XDR *);
70 static bool_t xdrmem_setpos(XDR *, u_int);
71 static int32_t *xdrmem_inline_aligned(XDR *, u_int);
72 static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
74 static const struct xdr_ops xdrmem_ops_aligned = {
75 xdrmem_getlong_aligned,
76 xdrmem_putlong_aligned,
81 xdrmem_inline_aligned,
85 static const struct xdr_ops xdrmem_ops_unaligned = {
86 xdrmem_getlong_unaligned,
87 xdrmem_putlong_unaligned,
92 xdrmem_inline_unaligned,
97 * The procedure xdrmem_create initializes a stream descriptor for a
101 xdrmem_create(xdrs, addr, size, op)
109 xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
110 ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
111 xdrs->x_private = xdrs->x_base = addr;
112 xdrs->x_handy = size;
124 xdrmem_getlong_aligned(xdrs, lp)
129 if (xdrs->x_handy < sizeof(int32_t))
131 xdrs->x_handy -= sizeof(int32_t);
132 *lp = ntohl(*(u_int32_t *)xdrs->x_private);
133 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
138 xdrmem_putlong_aligned(xdrs, lp)
143 if (xdrs->x_handy < sizeof(int32_t))
145 xdrs->x_handy -= sizeof(int32_t);
146 *(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp);
147 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
152 xdrmem_getlong_unaligned(xdrs, lp)
158 if (xdrs->x_handy < sizeof(int32_t))
160 xdrs->x_handy -= sizeof(int32_t);
161 memmove(&l, xdrs->x_private, sizeof(int32_t));
163 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
168 xdrmem_putlong_unaligned(xdrs, lp)
174 if (xdrs->x_handy < sizeof(int32_t))
176 xdrs->x_handy -= sizeof(int32_t);
177 l = htonl((u_int32_t)*lp);
178 memmove(xdrs->x_private, &l, sizeof(int32_t));
179 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
184 xdrmem_getbytes(xdrs, addr, len)
190 if (xdrs->x_handy < len)
192 xdrs->x_handy -= len;
193 memmove(addr, xdrs->x_private, len);
194 xdrs->x_private = (char *)xdrs->x_private + len;
199 xdrmem_putbytes(xdrs, addr, len)
205 if (xdrs->x_handy < len)
207 xdrs->x_handy -= len;
208 memmove(xdrs->x_private, addr, len);
209 xdrs->x_private = (char *)xdrs->x_private + len;
218 /* XXX w/64-bit pointers, u_int not enough! */
219 return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
223 xdrmem_setpos(xdrs, pos)
227 char *newaddr = xdrs->x_base + pos;
228 char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
230 if (newaddr > lastaddr)
232 xdrs->x_private = newaddr;
233 xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
238 xdrmem_inline_aligned(xdrs, len)
244 if (xdrs->x_handy >= len) {
245 xdrs->x_handy -= len;
246 buf = (int32_t *)xdrs->x_private;
247 xdrs->x_private = (char *)xdrs->x_private + len;
254 xdrmem_inline_unaligned(xdrs, len)