xdr_mem.c revision 92905
1/*	$NetBSD: xdr_mem.c,v 1.15 2000/01/22 22:19:18 mycroft Exp $	*/
2
3/*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part.  Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California  94043
30 */
31
32#include <sys/cdefs.h>
33#if defined(LIBC_SCCS) && !defined(lint)
34/*static char *sccsid = "from: @(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";*/
35/*static char *sccsid = "from: @(#)xdr_mem.c	2.1 88/07/29 4.0 RPCSRC";*/
36static char *rcsid = "$FreeBSD: head/lib/libc/xdr/xdr_mem.c 92905 2002-03-21 22:49:10Z obrien $";
37#endif
38
39/*
40 * xdr_mem.h, XDR implementation using memory buffers.
41 *
42 * Copyright (C) 1984, Sun Microsystems, Inc.
43 *
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.
47 *
48 */
49
50#include "namespace.h"
51#include <sys/types.h>
52
53#include <netinet/in.h>
54
55#include <string.h>
56
57#include <rpc/types.h>
58#include <rpc/xdr.h>
59#include "un-namespace.h"
60
61static void xdrmem_destroy(XDR *);
62static bool_t xdrmem_getlong_aligned(XDR *, long *);
63static bool_t xdrmem_putlong_aligned(XDR *, const long *);
64static bool_t xdrmem_getlong_unaligned(XDR *, long *);
65static bool_t xdrmem_putlong_unaligned(XDR *, const long *);
66static bool_t xdrmem_getbytes(XDR *, char *, u_int);
67static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
68/* XXX: w/64-bit pointers, u_int not enough! */
69static u_int xdrmem_getpos(XDR *);
70static bool_t xdrmem_setpos(XDR *, u_int);
71static int32_t *xdrmem_inline_aligned(XDR *, u_int);
72static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
73
74static const struct	xdr_ops xdrmem_ops_aligned = {
75	xdrmem_getlong_aligned,
76	xdrmem_putlong_aligned,
77	xdrmem_getbytes,
78	xdrmem_putbytes,
79	xdrmem_getpos,
80	xdrmem_setpos,
81	xdrmem_inline_aligned,
82	xdrmem_destroy
83};
84
85static const struct	xdr_ops xdrmem_ops_unaligned = {
86	xdrmem_getlong_unaligned,
87	xdrmem_putlong_unaligned,
88	xdrmem_getbytes,
89	xdrmem_putbytes,
90	xdrmem_getpos,
91	xdrmem_setpos,
92	xdrmem_inline_unaligned,
93	xdrmem_destroy
94};
95
96/*
97 * The procedure xdrmem_create initializes a stream descriptor for a
98 * memory buffer.
99 */
100void
101xdrmem_create(xdrs, addr, size, op)
102	XDR *xdrs;
103	char *addr;
104	u_int size;
105	enum xdr_op op;
106{
107
108	xdrs->x_op = 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;
113}
114
115/*ARGSUSED*/
116static void
117xdrmem_destroy(xdrs)
118	XDR *xdrs;
119{
120
121}
122
123static bool_t
124xdrmem_getlong_aligned(xdrs, lp)
125	XDR *xdrs;
126	long *lp;
127{
128
129	if ((xdrs->x_handy -= sizeof(int32_t)) < 0)
130		return (FALSE);
131	*lp = ntohl(*(u_int32_t *)xdrs->x_private);
132	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
133	return (TRUE);
134}
135
136static bool_t
137xdrmem_putlong_aligned(xdrs, lp)
138	XDR *xdrs;
139	const long *lp;
140{
141
142	if ((xdrs->x_handy -= sizeof(int32_t)) < 0)
143		return (FALSE);
144	*(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp);
145	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
146	return (TRUE);
147}
148
149static bool_t
150xdrmem_getlong_unaligned(xdrs, lp)
151	XDR *xdrs;
152	long *lp;
153{
154	u_int32_t l;
155
156	if ((xdrs->x_handy -= sizeof(int32_t)) < 0)
157		return (FALSE);
158	memmove(&l, xdrs->x_private, sizeof(int32_t));
159	*lp = ntohl(l);
160	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
161	return (TRUE);
162}
163
164static bool_t
165xdrmem_putlong_unaligned(xdrs, lp)
166	XDR *xdrs;
167	const long *lp;
168{
169	u_int32_t l;
170
171	if ((xdrs->x_handy -= sizeof(int32_t)) < 0)
172		return (FALSE);
173	l = htonl((u_int32_t)*lp);
174	memmove(xdrs->x_private, &l, sizeof(int32_t));
175	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
176	return (TRUE);
177}
178
179static bool_t
180xdrmem_getbytes(xdrs, addr, len)
181	XDR *xdrs;
182	char *addr;
183	u_int len;
184{
185
186	if ((xdrs->x_handy -= len) < 0)
187		return (FALSE);
188	memmove(addr, xdrs->x_private, len);
189	xdrs->x_private = (char *)xdrs->x_private + len;
190	return (TRUE);
191}
192
193static bool_t
194xdrmem_putbytes(xdrs, addr, len)
195	XDR *xdrs;
196	const char *addr;
197	u_int len;
198{
199
200	if ((xdrs->x_handy -= len) < 0)
201		return (FALSE);
202	memmove(xdrs->x_private, addr, len);
203	xdrs->x_private = (char *)xdrs->x_private + len;
204	return (TRUE);
205}
206
207static u_int
208xdrmem_getpos(xdrs)
209	XDR *xdrs;
210{
211
212	/* XXX w/64-bit pointers, u_int not enough! */
213	return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
214}
215
216static bool_t
217xdrmem_setpos(xdrs, pos)
218	XDR *xdrs;
219	u_int pos;
220{
221	char *newaddr = xdrs->x_base + pos;
222	char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
223
224	if ((long)newaddr > (long)lastaddr)
225		return (FALSE);
226	xdrs->x_private = newaddr;
227	xdrs->x_handy = (int)((long)lastaddr - (long)newaddr);
228	return (TRUE);
229}
230
231static int32_t *
232xdrmem_inline_aligned(xdrs, len)
233	XDR *xdrs;
234	u_int len;
235{
236	int32_t *buf = 0;
237
238	if (xdrs->x_handy >= len) {
239		xdrs->x_handy -= len;
240		buf = (int32_t *)xdrs->x_private;
241		xdrs->x_private = (char *)xdrs->x_private + len;
242	}
243	return (buf);
244}
245
246/* ARGSUSED */
247static int32_t *
248xdrmem_inline_unaligned(xdrs, len)
249	XDR *xdrs;
250	u_int len;
251{
252
253	return (0);
254}
255