xref: /freebsd/lib/libc/xdr/xdr_mem.c (revision eae561b30ec984ce171d99b1fd182575acc2c639)
1eae561b3SGarrett Wollman /*
2eae561b3SGarrett Wollman  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3eae561b3SGarrett Wollman  * unrestricted use provided that this legend is included on all tape
4eae561b3SGarrett Wollman  * media and as a part of the software program in whole or part.  Users
5eae561b3SGarrett Wollman  * may copy or modify Sun RPC without charge, but are not authorized
6eae561b3SGarrett Wollman  * to license or distribute it to anyone else except as part of a product or
7eae561b3SGarrett Wollman  * program developed by the user.
8eae561b3SGarrett Wollman  *
9eae561b3SGarrett Wollman  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10eae561b3SGarrett Wollman  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11eae561b3SGarrett Wollman  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12eae561b3SGarrett Wollman  *
13eae561b3SGarrett Wollman  * Sun RPC is provided with no support and without any obligation on the
14eae561b3SGarrett Wollman  * part of Sun Microsystems, Inc. to assist in its use, correction,
15eae561b3SGarrett Wollman  * modification or enhancement.
16eae561b3SGarrett Wollman  *
17eae561b3SGarrett Wollman  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18eae561b3SGarrett Wollman  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19eae561b3SGarrett Wollman  * OR ANY PART THEREOF.
20eae561b3SGarrett Wollman  *
21eae561b3SGarrett Wollman  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22eae561b3SGarrett Wollman  * or profits or other special, indirect and consequential damages, even if
23eae561b3SGarrett Wollman  * Sun has been advised of the possibility of such damages.
24eae561b3SGarrett Wollman  *
25eae561b3SGarrett Wollman  * Sun Microsystems, Inc.
26eae561b3SGarrett Wollman  * 2550 Garcia Avenue
27eae561b3SGarrett Wollman  * Mountain View, California  94043
28eae561b3SGarrett Wollman  */
29eae561b3SGarrett Wollman 
30eae561b3SGarrett Wollman #if defined(LIBC_SCCS) && !defined(lint)
31eae561b3SGarrett Wollman /*static char *sccsid = "from: @(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";*/
32eae561b3SGarrett Wollman /*static char *sccsid = "from: @(#)xdr_mem.c	2.1 88/07/29 4.0 RPCSRC";*/
33eae561b3SGarrett Wollman static char *rcsid = "$Id: xdr_mem.c,v 1.1 1993/10/27 05:41:11 paul Exp $";
34eae561b3SGarrett Wollman #endif
35eae561b3SGarrett Wollman 
36eae561b3SGarrett Wollman /*
37eae561b3SGarrett Wollman  * xdr_mem.h, XDR implementation using memory buffers.
38eae561b3SGarrett Wollman  *
39eae561b3SGarrett Wollman  * Copyright (C) 1984, Sun Microsystems, Inc.
40eae561b3SGarrett Wollman  *
41eae561b3SGarrett Wollman  * If you have some data to be interpreted as external data representation
42eae561b3SGarrett Wollman  * or to be converted to external data representation in a memory buffer,
43eae561b3SGarrett Wollman  * then this is the package for you.
44eae561b3SGarrett Wollman  *
45eae561b3SGarrett Wollman  */
46eae561b3SGarrett Wollman 
47eae561b3SGarrett Wollman 
48eae561b3SGarrett Wollman #include <rpc/types.h>
49eae561b3SGarrett Wollman #include <rpc/xdr.h>
50eae561b3SGarrett Wollman #include <netinet/in.h>
51eae561b3SGarrett Wollman 
52eae561b3SGarrett Wollman static bool_t	xdrmem_getlong();
53eae561b3SGarrett Wollman static bool_t	xdrmem_putlong();
54eae561b3SGarrett Wollman static bool_t	xdrmem_getbytes();
55eae561b3SGarrett Wollman static bool_t	xdrmem_putbytes();
56eae561b3SGarrett Wollman static u_int	xdrmem_getpos();
57eae561b3SGarrett Wollman static bool_t	xdrmem_setpos();
58eae561b3SGarrett Wollman static long *	xdrmem_inline();
59eae561b3SGarrett Wollman static void	xdrmem_destroy();
60eae561b3SGarrett Wollman 
61eae561b3SGarrett Wollman static struct	xdr_ops xdrmem_ops = {
62eae561b3SGarrett Wollman 	xdrmem_getlong,
63eae561b3SGarrett Wollman 	xdrmem_putlong,
64eae561b3SGarrett Wollman 	xdrmem_getbytes,
65eae561b3SGarrett Wollman 	xdrmem_putbytes,
66eae561b3SGarrett Wollman 	xdrmem_getpos,
67eae561b3SGarrett Wollman 	xdrmem_setpos,
68eae561b3SGarrett Wollman 	xdrmem_inline,
69eae561b3SGarrett Wollman 	xdrmem_destroy
70eae561b3SGarrett Wollman };
71eae561b3SGarrett Wollman 
72eae561b3SGarrett Wollman /*
73eae561b3SGarrett Wollman  * The procedure xdrmem_create initializes a stream descriptor for a
74eae561b3SGarrett Wollman  * memory buffer.
75eae561b3SGarrett Wollman  */
76eae561b3SGarrett Wollman void
77eae561b3SGarrett Wollman xdrmem_create(xdrs, addr, size, op)
78eae561b3SGarrett Wollman 	register XDR *xdrs;
79eae561b3SGarrett Wollman 	caddr_t addr;
80eae561b3SGarrett Wollman 	u_int size;
81eae561b3SGarrett Wollman 	enum xdr_op op;
82eae561b3SGarrett Wollman {
83eae561b3SGarrett Wollman 
84eae561b3SGarrett Wollman 	xdrs->x_op = op;
85eae561b3SGarrett Wollman 	xdrs->x_ops = &xdrmem_ops;
86eae561b3SGarrett Wollman 	xdrs->x_private = xdrs->x_base = addr;
87eae561b3SGarrett Wollman 	xdrs->x_handy = size;
88eae561b3SGarrett Wollman }
89eae561b3SGarrett Wollman 
90eae561b3SGarrett Wollman static void
91eae561b3SGarrett Wollman xdrmem_destroy(/*xdrs*/)
92eae561b3SGarrett Wollman 	/*XDR *xdrs;*/
93eae561b3SGarrett Wollman {
94eae561b3SGarrett Wollman }
95eae561b3SGarrett Wollman 
96eae561b3SGarrett Wollman static bool_t
97eae561b3SGarrett Wollman xdrmem_getlong(xdrs, lp)
98eae561b3SGarrett Wollman 	register XDR *xdrs;
99eae561b3SGarrett Wollman 	long *lp;
100eae561b3SGarrett Wollman {
101eae561b3SGarrett Wollman 
102eae561b3SGarrett Wollman 	if ((xdrs->x_handy -= sizeof(long)) < 0)
103eae561b3SGarrett Wollman 		return (FALSE);
104eae561b3SGarrett Wollman 	*lp = (long)ntohl((u_long)(*((long *)(xdrs->x_private))));
105eae561b3SGarrett Wollman 	xdrs->x_private += sizeof(long);
106eae561b3SGarrett Wollman 	return (TRUE);
107eae561b3SGarrett Wollman }
108eae561b3SGarrett Wollman 
109eae561b3SGarrett Wollman static bool_t
110eae561b3SGarrett Wollman xdrmem_putlong(xdrs, lp)
111eae561b3SGarrett Wollman 	register XDR *xdrs;
112eae561b3SGarrett Wollman 	long *lp;
113eae561b3SGarrett Wollman {
114eae561b3SGarrett Wollman 
115eae561b3SGarrett Wollman 	if ((xdrs->x_handy -= sizeof(long)) < 0)
116eae561b3SGarrett Wollman 		return (FALSE);
117eae561b3SGarrett Wollman 	*(long *)xdrs->x_private = (long)htonl((u_long)(*lp));
118eae561b3SGarrett Wollman 	xdrs->x_private += sizeof(long);
119eae561b3SGarrett Wollman 	return (TRUE);
120eae561b3SGarrett Wollman }
121eae561b3SGarrett Wollman 
122eae561b3SGarrett Wollman static bool_t
123eae561b3SGarrett Wollman xdrmem_getbytes(xdrs, addr, len)
124eae561b3SGarrett Wollman 	register XDR *xdrs;
125eae561b3SGarrett Wollman 	caddr_t addr;
126eae561b3SGarrett Wollman 	register u_int len;
127eae561b3SGarrett Wollman {
128eae561b3SGarrett Wollman 
129eae561b3SGarrett Wollman 	if ((xdrs->x_handy -= len) < 0)
130eae561b3SGarrett Wollman 		return (FALSE);
131eae561b3SGarrett Wollman 	bcopy(xdrs->x_private, addr, len);
132eae561b3SGarrett Wollman 	xdrs->x_private += len;
133eae561b3SGarrett Wollman 	return (TRUE);
134eae561b3SGarrett Wollman }
135eae561b3SGarrett Wollman 
136eae561b3SGarrett Wollman static bool_t
137eae561b3SGarrett Wollman xdrmem_putbytes(xdrs, addr, len)
138eae561b3SGarrett Wollman 	register XDR *xdrs;
139eae561b3SGarrett Wollman 	caddr_t addr;
140eae561b3SGarrett Wollman 	register u_int len;
141eae561b3SGarrett Wollman {
142eae561b3SGarrett Wollman 
143eae561b3SGarrett Wollman 	if ((xdrs->x_handy -= len) < 0)
144eae561b3SGarrett Wollman 		return (FALSE);
145eae561b3SGarrett Wollman 	bcopy(addr, xdrs->x_private, len);
146eae561b3SGarrett Wollman 	xdrs->x_private += len;
147eae561b3SGarrett Wollman 	return (TRUE);
148eae561b3SGarrett Wollman }
149eae561b3SGarrett Wollman 
150eae561b3SGarrett Wollman static u_int
151eae561b3SGarrett Wollman xdrmem_getpos(xdrs)
152eae561b3SGarrett Wollman 	register XDR *xdrs;
153eae561b3SGarrett Wollman {
154eae561b3SGarrett Wollman 
155eae561b3SGarrett Wollman 	return ((u_int)xdrs->x_private - (u_int)xdrs->x_base);
156eae561b3SGarrett Wollman }
157eae561b3SGarrett Wollman 
158eae561b3SGarrett Wollman static bool_t
159eae561b3SGarrett Wollman xdrmem_setpos(xdrs, pos)
160eae561b3SGarrett Wollman 	register XDR *xdrs;
161eae561b3SGarrett Wollman 	u_int pos;
162eae561b3SGarrett Wollman {
163eae561b3SGarrett Wollman 	register caddr_t newaddr = xdrs->x_base + pos;
164eae561b3SGarrett Wollman 	register caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
165eae561b3SGarrett Wollman 
166eae561b3SGarrett Wollman 	if ((long)newaddr > (long)lastaddr)
167eae561b3SGarrett Wollman 		return (FALSE);
168eae561b3SGarrett Wollman 	xdrs->x_private = newaddr;
169eae561b3SGarrett Wollman 	xdrs->x_handy = (int)lastaddr - (int)newaddr;
170eae561b3SGarrett Wollman 	return (TRUE);
171eae561b3SGarrett Wollman }
172eae561b3SGarrett Wollman 
173eae561b3SGarrett Wollman static long *
174eae561b3SGarrett Wollman xdrmem_inline(xdrs, len)
175eae561b3SGarrett Wollman 	register XDR *xdrs;
176eae561b3SGarrett Wollman 	int len;
177eae561b3SGarrett Wollman {
178eae561b3SGarrett Wollman 	long *buf = 0;
179eae561b3SGarrett Wollman 
180eae561b3SGarrett Wollman 	if (xdrs->x_handy >= len) {
181eae561b3SGarrett Wollman 		xdrs->x_handy -= len;
182eae561b3SGarrett Wollman 		buf = (long *) xdrs->x_private;
183eae561b3SGarrett Wollman 		xdrs->x_private += len;
184eae561b3SGarrett Wollman 	}
185eae561b3SGarrett Wollman 	return (buf);
186eae561b3SGarrett Wollman }
187