xref: /onnv-gate/usr/src/uts/common/rpc/xdr_mem.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1989 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*0Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * xdr_mem.c, XDR implementation using memory buffers.
39*0Sstevel@tonic-gate  *
40*0Sstevel@tonic-gate  * If you have some data to be interpreted as external data representation
41*0Sstevel@tonic-gate  * or to be converted to external data representation in a memory buffer,
42*0Sstevel@tonic-gate  * then this is the package for you.
43*0Sstevel@tonic-gate  */
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #include <sys/param.h>
46*0Sstevel@tonic-gate #include <sys/types.h>
47*0Sstevel@tonic-gate #include <sys/systm.h>
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #include <rpc/types.h>
50*0Sstevel@tonic-gate #include <rpc/xdr.h>
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate static struct xdr_ops *xdrmem_ops(void);
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate /*
55*0Sstevel@tonic-gate  * The procedure xdrmem_create initializes a stream descriptor for a
56*0Sstevel@tonic-gate  * memory buffer.
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate void
59*0Sstevel@tonic-gate xdrmem_create(XDR *xdrs, caddr_t addr, u_int size, enum xdr_op op)
60*0Sstevel@tonic-gate {
61*0Sstevel@tonic-gate 	xdrs->x_op = op;
62*0Sstevel@tonic-gate 	xdrs->x_ops = xdrmem_ops();
63*0Sstevel@tonic-gate 	xdrs->x_private = xdrs->x_base = addr;
64*0Sstevel@tonic-gate 	xdrs->x_handy = size;
65*0Sstevel@tonic-gate 	xdrs->x_public = NULL;
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /* ARGSUSED */
69*0Sstevel@tonic-gate static void
70*0Sstevel@tonic-gate xdrmem_destroy(XDR *xdrs)
71*0Sstevel@tonic-gate {
72*0Sstevel@tonic-gate }
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate static bool_t
75*0Sstevel@tonic-gate xdrmem_getint32(XDR *xdrs, int32_t *int32p)
76*0Sstevel@tonic-gate {
77*0Sstevel@tonic-gate 	if ((xdrs->x_handy -= (int)sizeof (int32_t)) < 0)
78*0Sstevel@tonic-gate 		return (FALSE);
79*0Sstevel@tonic-gate 	/* LINTED pointer alignment */
80*0Sstevel@tonic-gate 	*int32p = (int32_t)ntohl((uint32_t)(*((int32_t *)(xdrs->x_private))));
81*0Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
82*0Sstevel@tonic-gate 	return (TRUE);
83*0Sstevel@tonic-gate }
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate static bool_t
86*0Sstevel@tonic-gate xdrmem_putint32(XDR *xdrs, int32_t *int32p)
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate 	if ((xdrs->x_handy -= (int)sizeof (int32_t)) < 0)
89*0Sstevel@tonic-gate 		return (FALSE);
90*0Sstevel@tonic-gate 	/* LINTED pointer alignment */
91*0Sstevel@tonic-gate 	*(int32_t *)xdrs->x_private = (int32_t)htonl((uint32_t)(*int32p));
92*0Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
93*0Sstevel@tonic-gate 	return (TRUE);
94*0Sstevel@tonic-gate }
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate static bool_t
97*0Sstevel@tonic-gate xdrmem_getbytes(XDR *xdrs, caddr_t addr, int len)
98*0Sstevel@tonic-gate {
99*0Sstevel@tonic-gate 	if ((xdrs->x_handy -= len) < 0)
100*0Sstevel@tonic-gate 		return (FALSE);
101*0Sstevel@tonic-gate 	bcopy(xdrs->x_private, addr, len);
102*0Sstevel@tonic-gate 	xdrs->x_private += len;
103*0Sstevel@tonic-gate 	return (TRUE);
104*0Sstevel@tonic-gate }
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate static bool_t
107*0Sstevel@tonic-gate xdrmem_putbytes(XDR *xdrs, caddr_t addr, int len)
108*0Sstevel@tonic-gate {
109*0Sstevel@tonic-gate 	if ((xdrs->x_handy -= len) < 0)
110*0Sstevel@tonic-gate 		return (FALSE);
111*0Sstevel@tonic-gate 	bcopy(addr, xdrs->x_private, len);
112*0Sstevel@tonic-gate 	xdrs->x_private += len;
113*0Sstevel@tonic-gate 	return (TRUE);
114*0Sstevel@tonic-gate }
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate static u_int
117*0Sstevel@tonic-gate xdrmem_getpos(XDR *xdrs)
118*0Sstevel@tonic-gate {
119*0Sstevel@tonic-gate 	return ((u_int)((uintptr_t)xdrs->x_private - (uintptr_t)xdrs->x_base));
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate static bool_t
123*0Sstevel@tonic-gate xdrmem_setpos(XDR *xdrs, u_int pos)
124*0Sstevel@tonic-gate {
125*0Sstevel@tonic-gate 	caddr_t newaddr = xdrs->x_base + pos;
126*0Sstevel@tonic-gate 	caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
127*0Sstevel@tonic-gate 	ptrdiff_t diff;
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	if (newaddr > lastaddr)
130*0Sstevel@tonic-gate 		return (FALSE);
131*0Sstevel@tonic-gate 	xdrs->x_private = newaddr;
132*0Sstevel@tonic-gate 	diff = lastaddr - newaddr;
133*0Sstevel@tonic-gate 	xdrs->x_handy = (int)diff;
134*0Sstevel@tonic-gate 	return (TRUE);
135*0Sstevel@tonic-gate }
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate static rpc_inline_t *
138*0Sstevel@tonic-gate xdrmem_inline(XDR *xdrs, int len)
139*0Sstevel@tonic-gate {
140*0Sstevel@tonic-gate 	rpc_inline_t *buf = NULL;
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 	if (xdrs->x_handy >= len) {
143*0Sstevel@tonic-gate 		xdrs->x_handy -= len;
144*0Sstevel@tonic-gate 		/* LINTED pointer alignment */
145*0Sstevel@tonic-gate 		buf = (rpc_inline_t *) xdrs->x_private;
146*0Sstevel@tonic-gate 		xdrs->x_private += len;
147*0Sstevel@tonic-gate 	}
148*0Sstevel@tonic-gate 	return (buf);
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate static bool_t
152*0Sstevel@tonic-gate xdrmem_control(XDR *xdrs, int request, void *info)
153*0Sstevel@tonic-gate {
154*0Sstevel@tonic-gate 	int32_t *int32p;
155*0Sstevel@tonic-gate 	int len;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	switch (request) {
158*0Sstevel@tonic-gate 	case XDR_PEEK:
159*0Sstevel@tonic-gate 		/*
160*0Sstevel@tonic-gate 		 * Return the next 4 byte unit in the XDR stream.
161*0Sstevel@tonic-gate 		 */
162*0Sstevel@tonic-gate 		if (xdrs->x_handy < sizeof (int32_t))
163*0Sstevel@tonic-gate 			return (FALSE);
164*0Sstevel@tonic-gate 		int32p = (int32_t *)info;
165*0Sstevel@tonic-gate 		*int32p = (int32_t)ntohl((uint32_t)
166*0Sstevel@tonic-gate 		    (*((int32_t *)(xdrs->x_private))));
167*0Sstevel@tonic-gate 		return (TRUE);
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	case XDR_SKIPBYTES:
170*0Sstevel@tonic-gate 		/*
171*0Sstevel@tonic-gate 		 * Skip the next N bytes in the XDR stream.
172*0Sstevel@tonic-gate 		 */
173*0Sstevel@tonic-gate 		int32p = (int32_t *)info;
174*0Sstevel@tonic-gate 		len = RNDUP((int)(*int32p));
175*0Sstevel@tonic-gate 		if ((xdrs->x_handy -= len) < 0)
176*0Sstevel@tonic-gate 			return (FALSE);
177*0Sstevel@tonic-gate 		xdrs->x_private += len;
178*0Sstevel@tonic-gate 		return (TRUE);
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	default:
181*0Sstevel@tonic-gate 		return (FALSE);
182*0Sstevel@tonic-gate 	}
183*0Sstevel@tonic-gate }
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate static struct xdr_ops *
186*0Sstevel@tonic-gate xdrmem_ops(void)
187*0Sstevel@tonic-gate {
188*0Sstevel@tonic-gate 	static struct xdr_ops ops;
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	if (ops.x_getint32 == NULL) {
191*0Sstevel@tonic-gate 		ops.x_getbytes = xdrmem_getbytes;
192*0Sstevel@tonic-gate 		ops.x_putbytes = xdrmem_putbytes;
193*0Sstevel@tonic-gate 		ops.x_getpostn = xdrmem_getpos;
194*0Sstevel@tonic-gate 		ops.x_setpostn = xdrmem_setpos;
195*0Sstevel@tonic-gate 		ops.x_inline = xdrmem_inline;
196*0Sstevel@tonic-gate 		ops.x_destroy = xdrmem_destroy;
197*0Sstevel@tonic-gate 		ops.x_control = xdrmem_control;
198*0Sstevel@tonic-gate 		ops.x_getint32 = xdrmem_getint32;
199*0Sstevel@tonic-gate 		ops.x_putint32 = xdrmem_putint32;
200*0Sstevel@tonic-gate 	}
201*0Sstevel@tonic-gate 	return (&ops);
202*0Sstevel@tonic-gate }
203