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.
37 * xdr_mem.h, XDR implementation using memory buffers.
39 * If you have some data to be interpreted as external data representation
40 * or to be converted to external data representation in a memory buffer,
41 * then this is the package for you.
45 #include "namespace.h"
46 #include <sys/types.h>
48 #include <netinet/in.h>
52 #include <rpc/types.h>
54 #include "un-namespace.h"
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);
69 static const struct xdr_ops xdrmem_ops_aligned = {
70 xdrmem_getlong_aligned,
71 xdrmem_putlong_aligned,
76 xdrmem_inline_aligned,
80 static const struct xdr_ops xdrmem_ops_unaligned = {
81 xdrmem_getlong_unaligned,
82 xdrmem_putlong_unaligned,
87 xdrmem_inline_unaligned,
92 * The procedure xdrmem_create initializes a stream descriptor for a
96 xdrmem_create(XDR *xdrs, char *addr, u_int size, enum xdr_op 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;
108 xdrmem_destroy(XDR *xdrs)
114 xdrmem_getlong_aligned(XDR *xdrs, long *lp)
117 if (xdrs->x_handy < sizeof(int32_t))
119 xdrs->x_handy -= sizeof(int32_t);
120 *lp = ntohl(*(u_int32_t *)xdrs->x_private);
121 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
126 xdrmem_putlong_aligned(XDR *xdrs, const long *lp)
129 if (xdrs->x_handy < sizeof(int32_t))
131 xdrs->x_handy -= sizeof(int32_t);
132 *(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp);
133 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
138 xdrmem_getlong_unaligned(XDR *xdrs, long *lp)
142 if (xdrs->x_handy < sizeof(int32_t))
144 xdrs->x_handy -= sizeof(int32_t);
145 memmove(&l, xdrs->x_private, sizeof(int32_t));
147 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
152 xdrmem_putlong_unaligned(XDR *xdrs, const long *lp)
156 if (xdrs->x_handy < sizeof(int32_t))
158 xdrs->x_handy -= sizeof(int32_t);
159 l = htonl((u_int32_t)*lp);
160 memmove(xdrs->x_private, &l, sizeof(int32_t));
161 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
166 xdrmem_getbytes(XDR *xdrs, char *addr, u_int len)
169 if (xdrs->x_handy < len)
171 xdrs->x_handy -= len;
172 memmove(addr, xdrs->x_private, len);
173 xdrs->x_private = (char *)xdrs->x_private + len;
178 xdrmem_putbytes(XDR *xdrs, const char *addr, u_int len)
181 if (xdrs->x_handy < len)
183 xdrs->x_handy -= len;
184 memmove(xdrs->x_private, addr, len);
185 xdrs->x_private = (char *)xdrs->x_private + len;
190 xdrmem_getpos(XDR *xdrs)
193 /* XXX w/64-bit pointers, u_int not enough! */
194 return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
198 xdrmem_setpos(XDR *xdrs, u_int pos)
200 char *newaddr = xdrs->x_base + pos;
201 char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
203 if (newaddr > lastaddr)
205 xdrs->x_private = newaddr;
206 xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
211 xdrmem_inline_aligned(XDR *xdrs, u_int len)
215 if (xdrs->x_handy >= len) {
216 xdrs->x_handy -= len;
217 buf = (int32_t *)xdrs->x_private;
218 xdrs->x_private = (char *)xdrs->x_private + len;
225 xdrmem_inline_unaligned(XDR *xdrs, u_int len)