xref: /onnv-gate/usr/src/uts/common/rpc/xdr_mem.c (revision 789:b348f31ed315)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*789Sahrens  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
320Sstevel@tonic-gate  * under license from the Regents of the University of California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * xdr_mem.c, XDR implementation using memory buffers.
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  * If you have some data to be interpreted as external data representation
410Sstevel@tonic-gate  * or to be converted to external data representation in a memory buffer,
420Sstevel@tonic-gate  * then this is the package for you.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <sys/param.h>
460Sstevel@tonic-gate #include <sys/types.h>
470Sstevel@tonic-gate #include <sys/systm.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include <rpc/types.h>
500Sstevel@tonic-gate #include <rpc/xdr.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static struct xdr_ops *xdrmem_ops(void);
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * The procedure xdrmem_create initializes a stream descriptor for a
560Sstevel@tonic-gate  * memory buffer.
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate void
xdrmem_create(XDR * xdrs,caddr_t addr,uint_t size,enum xdr_op op)59*789Sahrens xdrmem_create(XDR *xdrs, caddr_t addr, uint_t size, enum xdr_op op)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	xdrs->x_op = op;
620Sstevel@tonic-gate 	xdrs->x_ops = xdrmem_ops();
630Sstevel@tonic-gate 	xdrs->x_private = xdrs->x_base = addr;
640Sstevel@tonic-gate 	xdrs->x_handy = size;
650Sstevel@tonic-gate 	xdrs->x_public = NULL;
660Sstevel@tonic-gate }
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /* ARGSUSED */
690Sstevel@tonic-gate static void
xdrmem_destroy(XDR * xdrs)700Sstevel@tonic-gate xdrmem_destroy(XDR *xdrs)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate }
730Sstevel@tonic-gate 
740Sstevel@tonic-gate static bool_t
xdrmem_getint32(XDR * xdrs,int32_t * int32p)750Sstevel@tonic-gate xdrmem_getint32(XDR *xdrs, int32_t *int32p)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	if ((xdrs->x_handy -= (int)sizeof (int32_t)) < 0)
780Sstevel@tonic-gate 		return (FALSE);
790Sstevel@tonic-gate 	/* LINTED pointer alignment */
800Sstevel@tonic-gate 	*int32p = (int32_t)ntohl((uint32_t)(*((int32_t *)(xdrs->x_private))));
810Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
820Sstevel@tonic-gate 	return (TRUE);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static bool_t
xdrmem_putint32(XDR * xdrs,int32_t * int32p)860Sstevel@tonic-gate xdrmem_putint32(XDR *xdrs, int32_t *int32p)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	if ((xdrs->x_handy -= (int)sizeof (int32_t)) < 0)
890Sstevel@tonic-gate 		return (FALSE);
900Sstevel@tonic-gate 	/* LINTED pointer alignment */
910Sstevel@tonic-gate 	*(int32_t *)xdrs->x_private = (int32_t)htonl((uint32_t)(*int32p));
920Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
930Sstevel@tonic-gate 	return (TRUE);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static bool_t
xdrmem_getbytes(XDR * xdrs,caddr_t addr,int len)970Sstevel@tonic-gate xdrmem_getbytes(XDR *xdrs, caddr_t addr, int len)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	if ((xdrs->x_handy -= len) < 0)
1000Sstevel@tonic-gate 		return (FALSE);
1010Sstevel@tonic-gate 	bcopy(xdrs->x_private, addr, len);
1020Sstevel@tonic-gate 	xdrs->x_private += len;
1030Sstevel@tonic-gate 	return (TRUE);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate static bool_t
xdrmem_putbytes(XDR * xdrs,caddr_t addr,int len)1070Sstevel@tonic-gate xdrmem_putbytes(XDR *xdrs, caddr_t addr, int len)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	if ((xdrs->x_handy -= len) < 0)
1100Sstevel@tonic-gate 		return (FALSE);
1110Sstevel@tonic-gate 	bcopy(addr, xdrs->x_private, len);
1120Sstevel@tonic-gate 	xdrs->x_private += len;
1130Sstevel@tonic-gate 	return (TRUE);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate 
116*789Sahrens static uint_t
xdrmem_getpos(XDR * xdrs)1170Sstevel@tonic-gate xdrmem_getpos(XDR *xdrs)
1180Sstevel@tonic-gate {
119*789Sahrens 	return ((uint_t)((uintptr_t)xdrs->x_private - (uintptr_t)xdrs->x_base));
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate static bool_t
xdrmem_setpos(XDR * xdrs,uint_t pos)123*789Sahrens xdrmem_setpos(XDR *xdrs, uint_t pos)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	caddr_t newaddr = xdrs->x_base + pos;
1260Sstevel@tonic-gate 	caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
1270Sstevel@tonic-gate 	ptrdiff_t diff;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	if (newaddr > lastaddr)
1300Sstevel@tonic-gate 		return (FALSE);
1310Sstevel@tonic-gate 	xdrs->x_private = newaddr;
1320Sstevel@tonic-gate 	diff = lastaddr - newaddr;
1330Sstevel@tonic-gate 	xdrs->x_handy = (int)diff;
1340Sstevel@tonic-gate 	return (TRUE);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate static rpc_inline_t *
xdrmem_inline(XDR * xdrs,int len)1380Sstevel@tonic-gate xdrmem_inline(XDR *xdrs, int len)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	rpc_inline_t *buf = NULL;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	if (xdrs->x_handy >= len) {
1430Sstevel@tonic-gate 		xdrs->x_handy -= len;
1440Sstevel@tonic-gate 		/* LINTED pointer alignment */
145*789Sahrens 		buf = (rpc_inline_t *)xdrs->x_private;
1460Sstevel@tonic-gate 		xdrs->x_private += len;
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 	return (buf);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate static bool_t
xdrmem_control(XDR * xdrs,int request,void * info)1520Sstevel@tonic-gate xdrmem_control(XDR *xdrs, int request, void *info)
1530Sstevel@tonic-gate {
154*789Sahrens 	xdr_bytesrec *xptr;
1550Sstevel@tonic-gate 	int32_t *int32p;
1560Sstevel@tonic-gate 	int len;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	switch (request) {
159*789Sahrens 
160*789Sahrens 	case XDR_GET_BYTES_AVAIL:
161*789Sahrens 		xptr = (xdr_bytesrec *)info;
162*789Sahrens 		xptr->xc_is_last_record = TRUE;
163*789Sahrens 		xptr->xc_num_avail = xdrs->x_handy;
164*789Sahrens 		return (TRUE);
165*789Sahrens 
1660Sstevel@tonic-gate 	case XDR_PEEK:
1670Sstevel@tonic-gate 		/*
1680Sstevel@tonic-gate 		 * Return the next 4 byte unit in the XDR stream.
1690Sstevel@tonic-gate 		 */
1700Sstevel@tonic-gate 		if (xdrs->x_handy < sizeof (int32_t))
1710Sstevel@tonic-gate 			return (FALSE);
1720Sstevel@tonic-gate 		int32p = (int32_t *)info;
1730Sstevel@tonic-gate 		*int32p = (int32_t)ntohl((uint32_t)
1740Sstevel@tonic-gate 		    (*((int32_t *)(xdrs->x_private))));
1750Sstevel@tonic-gate 		return (TRUE);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	case XDR_SKIPBYTES:
1780Sstevel@tonic-gate 		/*
1790Sstevel@tonic-gate 		 * Skip the next N bytes in the XDR stream.
1800Sstevel@tonic-gate 		 */
1810Sstevel@tonic-gate 		int32p = (int32_t *)info;
1820Sstevel@tonic-gate 		len = RNDUP((int)(*int32p));
1830Sstevel@tonic-gate 		if ((xdrs->x_handy -= len) < 0)
1840Sstevel@tonic-gate 			return (FALSE);
1850Sstevel@tonic-gate 		xdrs->x_private += len;
1860Sstevel@tonic-gate 		return (TRUE);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	}
189*789Sahrens 	return (FALSE);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate static struct xdr_ops *
xdrmem_ops(void)1930Sstevel@tonic-gate xdrmem_ops(void)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate 	static struct xdr_ops ops;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (ops.x_getint32 == NULL) {
1980Sstevel@tonic-gate 		ops.x_getbytes = xdrmem_getbytes;
1990Sstevel@tonic-gate 		ops.x_putbytes = xdrmem_putbytes;
2000Sstevel@tonic-gate 		ops.x_getpostn = xdrmem_getpos;
2010Sstevel@tonic-gate 		ops.x_setpostn = xdrmem_setpos;
2020Sstevel@tonic-gate 		ops.x_inline = xdrmem_inline;
2030Sstevel@tonic-gate 		ops.x_destroy = xdrmem_destroy;
2040Sstevel@tonic-gate 		ops.x_control = xdrmem_control;
2050Sstevel@tonic-gate 		ops.x_getint32 = xdrmem_getint32;
2060Sstevel@tonic-gate 		ops.x_putint32 = xdrmem_putint32;
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 	return (&ops);
2090Sstevel@tonic-gate }
210