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