2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
30 #if defined(LIBC_SCCS) && !defined(lint)
31 /*static char *sccsid = "from: @(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";*/
32 /*static char *sccsid = "from: @(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC";*/
33 static char *rcsid = "$FreeBSD: src/lib/libc/xdr/xdr_mem.c,v 1.8.2.1 2003/03/20 12:59:55 jedgar Exp $";
37 * xdr_mem.h, XDR implementation using memory buffers.
39 * Copyright (C) 1984, Sun Microsystems, Inc.
41 * If you have some data to be interpreted as external data representation
42 * or to be converted to external data representation in a memory buffer,
43 * then this is the package for you.
48 #include <rpc/types.h>
50 #include <netinet/in.h>
52 static bool_t xdrmem_getlong_aligned();
53 static bool_t xdrmem_putlong_aligned();
54 static bool_t xdrmem_getlong_unaligned();
55 static bool_t xdrmem_putlong_unaligned();
56 static bool_t xdrmem_getbytes();
57 static bool_t xdrmem_putbytes();
58 static u_int xdrmem_getpos(); /* XXX w/64-bit pointers, u_int not enough! */
59 static bool_t xdrmem_setpos();
60 static int32_t *xdrmem_inline_aligned();
61 static int32_t *xdrmem_inline_unaligned();
62 static void xdrmem_destroy();
64 static struct xdr_ops xdrmem_ops_aligned = {
65 xdrmem_getlong_aligned,
66 xdrmem_putlong_aligned,
71 xdrmem_inline_aligned,
75 static struct xdr_ops xdrmem_ops_unaligned = {
76 xdrmem_getlong_unaligned,
77 xdrmem_putlong_unaligned,
82 xdrmem_inline_unaligned,
87 * The procedure xdrmem_create initializes a stream descriptor for a
91 xdrmem_create(xdrs, addr, size, op)
99 xdrs->x_ops = ((size_t)addr & (sizeof(int32_t) - 1))
100 ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
101 xdrs->x_private = xdrs->x_base = addr;
102 xdrs->x_handy = size;
106 xdrmem_destroy(/*xdrs*/)
113 xdrmem_getlong_aligned(xdrs, lp)
118 if (xdrs->x_handy < sizeof(int32_t))
120 xdrs->x_handy -= sizeof(int32_t);
121 *lp = ntohl(*(int32_t *)(xdrs->x_private));
122 xdrs->x_private += sizeof(int32_t);
127 xdrmem_putlong_aligned(xdrs, lp)
132 if (xdrs->x_handy < sizeof(int32_t))
134 xdrs->x_handy -= sizeof(int32_t);
135 *(int32_t *)xdrs->x_private = htonl(*lp);
136 xdrs->x_private += sizeof(int32_t);
141 xdrmem_getlong_unaligned(xdrs, lp)
147 if (xdrs->x_handy < sizeof(int32_t))
149 xdrs->x_handy -= sizeof(int32_t);
150 memcpy(&l, xdrs->x_private, sizeof(int32_t));
152 xdrs->x_private += sizeof(int32_t);
157 xdrmem_putlong_unaligned(xdrs, lp)
163 if (xdrs->x_handy < sizeof(int32_t))
165 xdrs->x_handy -= sizeof(int32_t);
167 memcpy(xdrs->x_private, &l, sizeof(int32_t));
168 xdrs->x_private += sizeof(int32_t);
173 xdrmem_getbytes(xdrs, addr, len)
179 if (xdrs->x_handy < len)
181 xdrs->x_handy -= len;
182 memcpy(addr, xdrs->x_private, len);
183 xdrs->x_private += len;
188 xdrmem_putbytes(xdrs, addr, len)
194 if (xdrs->x_handy < len)
196 xdrs->x_handy -= len;
197 memcpy(xdrs->x_private, addr, len);
198 xdrs->x_private += len;
207 /* XXX w/64-bit pointers, u_int not enough! */
208 return ((u_long)xdrs->x_private - (u_long)xdrs->x_base);
212 xdrmem_setpos(xdrs, pos)
216 register caddr_t newaddr = xdrs->x_base + pos;
217 register caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
219 if (newaddr > lastaddr)
221 xdrs->x_private = newaddr;
222 xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
227 xdrmem_inline_aligned(xdrs, len)
233 if (xdrs->x_handy >= len) {
234 xdrs->x_handy -= len;
235 buf = (int32_t *) xdrs->x_private;
236 xdrs->x_private += len;
242 xdrmem_inline_unaligned(xdrs, len)