1 /* $NetBSD: xdr_mem.c,v 1.15 2000/01/22 22:19:18 mycroft Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2010, Oracle America, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials
17 * provided with the distribution.
18 * * Neither the name of the "Oracle America, Inc." nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static char *sccsid2 = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
38 static char *sccsid = "@(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 * xdr_mem.h, XDR implementation using memory buffers.
46 * If you have some data to be interpreted as external data representation
47 * or to be converted to external data representation in a memory buffer,
48 * then this is the package for you.
52 #include "namespace.h"
53 #include <sys/types.h>
55 #include <netinet/in.h>
59 #include <rpc/types.h>
61 #include "un-namespace.h"
63 static void xdrmem_destroy(XDR *);
64 static bool_t xdrmem_getlong_aligned(XDR *, long *);
65 static bool_t xdrmem_putlong_aligned(XDR *, const long *);
66 static bool_t xdrmem_getlong_unaligned(XDR *, long *);
67 static bool_t xdrmem_putlong_unaligned(XDR *, const long *);
68 static bool_t xdrmem_getbytes(XDR *, char *, u_int);
69 static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
70 /* XXX: w/64-bit pointers, u_int not enough! */
71 static u_int xdrmem_getpos(XDR *);
72 static bool_t xdrmem_setpos(XDR *, u_int);
73 static int32_t *xdrmem_inline_aligned(XDR *, u_int);
74 static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
76 static const struct xdr_ops xdrmem_ops_aligned = {
77 xdrmem_getlong_aligned,
78 xdrmem_putlong_aligned,
83 xdrmem_inline_aligned,
87 static const struct xdr_ops xdrmem_ops_unaligned = {
88 xdrmem_getlong_unaligned,
89 xdrmem_putlong_unaligned,
94 xdrmem_inline_unaligned,
99 * The procedure xdrmem_create initializes a stream descriptor for a
103 xdrmem_create(XDR *xdrs, char *addr, u_int size, enum xdr_op op)
107 xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
108 ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
109 xdrs->x_private = xdrs->x_base = addr;
110 xdrs->x_handy = size;
115 xdrmem_destroy(XDR *xdrs)
121 xdrmem_getlong_aligned(XDR *xdrs, long *lp)
124 if (xdrs->x_handy < sizeof(int32_t))
126 xdrs->x_handy -= sizeof(int32_t);
127 *lp = ntohl(*(u_int32_t *)xdrs->x_private);
128 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
133 xdrmem_putlong_aligned(XDR *xdrs, const long *lp)
136 if (xdrs->x_handy < sizeof(int32_t))
138 xdrs->x_handy -= sizeof(int32_t);
139 *(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp);
140 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
145 xdrmem_getlong_unaligned(XDR *xdrs, long *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(XDR *xdrs, const long *lp)
163 if (xdrs->x_handy < sizeof(int32_t))
165 xdrs->x_handy -= sizeof(int32_t);
166 l = htonl((u_int32_t)*lp);
167 memmove(xdrs->x_private, &l, sizeof(int32_t));
168 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
173 xdrmem_getbytes(XDR *xdrs, char *addr, u_int len)
176 if (xdrs->x_handy < len)
178 xdrs->x_handy -= len;
179 memmove(addr, xdrs->x_private, len);
180 xdrs->x_private = (char *)xdrs->x_private + len;
185 xdrmem_putbytes(XDR *xdrs, const char *addr, u_int len)
188 if (xdrs->x_handy < len)
190 xdrs->x_handy -= len;
191 memmove(xdrs->x_private, addr, len);
192 xdrs->x_private = (char *)xdrs->x_private + len;
197 xdrmem_getpos(XDR *xdrs)
200 /* XXX w/64-bit pointers, u_int not enough! */
201 return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
205 xdrmem_setpos(XDR *xdrs, u_int pos)
207 char *newaddr = xdrs->x_base + pos;
208 char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
210 if (newaddr > lastaddr)
212 xdrs->x_private = newaddr;
213 xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
218 xdrmem_inline_aligned(XDR *xdrs, u_int len)
222 if (xdrs->x_handy >= len) {
223 xdrs->x_handy -= len;
224 buf = (int32_t *)xdrs->x_private;
225 xdrs->x_private = (char *)xdrs->x_private + len;
232 xdrmem_inline_unaligned(XDR *xdrs, u_int len)