xref: /titanic_50/usr/src/uts/common/rpc/xdr_mem.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*fa9e4066Sahrens  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * xdr_mem.c, XDR implementation using memory buffers.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * If you have some data to be interpreted as external data representation
417c478bd9Sstevel@tonic-gate  * or to be converted to external data representation in a memory buffer,
427c478bd9Sstevel@tonic-gate  * then this is the package for you.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <sys/param.h>
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <sys/systm.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <rpc/types.h>
507c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static struct xdr_ops *xdrmem_ops(void);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * The procedure xdrmem_create initializes a stream descriptor for a
567c478bd9Sstevel@tonic-gate  * memory buffer.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate void
xdrmem_create(XDR * xdrs,caddr_t addr,uint_t size,enum xdr_op op)59*fa9e4066Sahrens xdrmem_create(XDR *xdrs, caddr_t addr, uint_t size, enum xdr_op op)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	xdrs->x_op = op;
627c478bd9Sstevel@tonic-gate 	xdrs->x_ops = xdrmem_ops();
637c478bd9Sstevel@tonic-gate 	xdrs->x_private = xdrs->x_base = addr;
647c478bd9Sstevel@tonic-gate 	xdrs->x_handy = size;
657c478bd9Sstevel@tonic-gate 	xdrs->x_public = NULL;
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /* ARGSUSED */
697c478bd9Sstevel@tonic-gate static void
xdrmem_destroy(XDR * xdrs)707c478bd9Sstevel@tonic-gate xdrmem_destroy(XDR *xdrs)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static bool_t
xdrmem_getint32(XDR * xdrs,int32_t * int32p)757c478bd9Sstevel@tonic-gate xdrmem_getint32(XDR *xdrs, int32_t *int32p)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	if ((xdrs->x_handy -= (int)sizeof (int32_t)) < 0)
787c478bd9Sstevel@tonic-gate 		return (FALSE);
797c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
807c478bd9Sstevel@tonic-gate 	*int32p = (int32_t)ntohl((uint32_t)(*((int32_t *)(xdrs->x_private))));
817c478bd9Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
827c478bd9Sstevel@tonic-gate 	return (TRUE);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static bool_t
xdrmem_putint32(XDR * xdrs,int32_t * int32p)867c478bd9Sstevel@tonic-gate xdrmem_putint32(XDR *xdrs, int32_t *int32p)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	if ((xdrs->x_handy -= (int)sizeof (int32_t)) < 0)
897c478bd9Sstevel@tonic-gate 		return (FALSE);
907c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
917c478bd9Sstevel@tonic-gate 	*(int32_t *)xdrs->x_private = (int32_t)htonl((uint32_t)(*int32p));
927c478bd9Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
937c478bd9Sstevel@tonic-gate 	return (TRUE);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static bool_t
xdrmem_getbytes(XDR * xdrs,caddr_t addr,int len)977c478bd9Sstevel@tonic-gate xdrmem_getbytes(XDR *xdrs, caddr_t addr, int len)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	if ((xdrs->x_handy -= len) < 0)
1007c478bd9Sstevel@tonic-gate 		return (FALSE);
1017c478bd9Sstevel@tonic-gate 	bcopy(xdrs->x_private, addr, len);
1027c478bd9Sstevel@tonic-gate 	xdrs->x_private += len;
1037c478bd9Sstevel@tonic-gate 	return (TRUE);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static bool_t
xdrmem_putbytes(XDR * xdrs,caddr_t addr,int len)1077c478bd9Sstevel@tonic-gate xdrmem_putbytes(XDR *xdrs, caddr_t addr, int len)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	if ((xdrs->x_handy -= len) < 0)
1107c478bd9Sstevel@tonic-gate 		return (FALSE);
1117c478bd9Sstevel@tonic-gate 	bcopy(addr, xdrs->x_private, len);
1127c478bd9Sstevel@tonic-gate 	xdrs->x_private += len;
1137c478bd9Sstevel@tonic-gate 	return (TRUE);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
116*fa9e4066Sahrens static uint_t
xdrmem_getpos(XDR * xdrs)1177c478bd9Sstevel@tonic-gate xdrmem_getpos(XDR *xdrs)
1187c478bd9Sstevel@tonic-gate {
119*fa9e4066Sahrens 	return ((uint_t)((uintptr_t)xdrs->x_private - (uintptr_t)xdrs->x_base));
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate static bool_t
xdrmem_setpos(XDR * xdrs,uint_t pos)123*fa9e4066Sahrens xdrmem_setpos(XDR *xdrs, uint_t pos)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	caddr_t newaddr = xdrs->x_base + pos;
1267c478bd9Sstevel@tonic-gate 	caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
1277c478bd9Sstevel@tonic-gate 	ptrdiff_t diff;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if (newaddr > lastaddr)
1307c478bd9Sstevel@tonic-gate 		return (FALSE);
1317c478bd9Sstevel@tonic-gate 	xdrs->x_private = newaddr;
1327c478bd9Sstevel@tonic-gate 	diff = lastaddr - newaddr;
1337c478bd9Sstevel@tonic-gate 	xdrs->x_handy = (int)diff;
1347c478bd9Sstevel@tonic-gate 	return (TRUE);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate static rpc_inline_t *
xdrmem_inline(XDR * xdrs,int len)1387c478bd9Sstevel@tonic-gate xdrmem_inline(XDR *xdrs, int len)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	rpc_inline_t *buf = NULL;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if (xdrs->x_handy >= len) {
1437c478bd9Sstevel@tonic-gate 		xdrs->x_handy -= len;
1447c478bd9Sstevel@tonic-gate 		/* LINTED pointer alignment */
1457c478bd9Sstevel@tonic-gate 		buf = (rpc_inline_t *)xdrs->x_private;
1467c478bd9Sstevel@tonic-gate 		xdrs->x_private += len;
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 	return (buf);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static bool_t
xdrmem_control(XDR * xdrs,int request,void * info)1527c478bd9Sstevel@tonic-gate xdrmem_control(XDR *xdrs, int request, void *info)
1537c478bd9Sstevel@tonic-gate {
154*fa9e4066Sahrens 	xdr_bytesrec *xptr;
1557c478bd9Sstevel@tonic-gate 	int32_t *int32p;
1567c478bd9Sstevel@tonic-gate 	int len;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	switch (request) {
159*fa9e4066Sahrens 
160*fa9e4066Sahrens 	case XDR_GET_BYTES_AVAIL:
161*fa9e4066Sahrens 		xptr = (xdr_bytesrec *)info;
162*fa9e4066Sahrens 		xptr->xc_is_last_record = TRUE;
163*fa9e4066Sahrens 		xptr->xc_num_avail = xdrs->x_handy;
164*fa9e4066Sahrens 		return (TRUE);
165*fa9e4066Sahrens 
1667c478bd9Sstevel@tonic-gate 	case XDR_PEEK:
1677c478bd9Sstevel@tonic-gate 		/*
1687c478bd9Sstevel@tonic-gate 		 * Return the next 4 byte unit in the XDR stream.
1697c478bd9Sstevel@tonic-gate 		 */
1707c478bd9Sstevel@tonic-gate 		if (xdrs->x_handy < sizeof (int32_t))
1717c478bd9Sstevel@tonic-gate 			return (FALSE);
1727c478bd9Sstevel@tonic-gate 		int32p = (int32_t *)info;
1737c478bd9Sstevel@tonic-gate 		*int32p = (int32_t)ntohl((uint32_t)
1747c478bd9Sstevel@tonic-gate 		    (*((int32_t *)(xdrs->x_private))));
1757c478bd9Sstevel@tonic-gate 		return (TRUE);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	case XDR_SKIPBYTES:
1787c478bd9Sstevel@tonic-gate 		/*
1797c478bd9Sstevel@tonic-gate 		 * Skip the next N bytes in the XDR stream.
1807c478bd9Sstevel@tonic-gate 		 */
1817c478bd9Sstevel@tonic-gate 		int32p = (int32_t *)info;
1827c478bd9Sstevel@tonic-gate 		len = RNDUP((int)(*int32p));
1837c478bd9Sstevel@tonic-gate 		if ((xdrs->x_handy -= len) < 0)
1847c478bd9Sstevel@tonic-gate 			return (FALSE);
1857c478bd9Sstevel@tonic-gate 		xdrs->x_private += len;
1867c478bd9Sstevel@tonic-gate 		return (TRUE);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	}
189*fa9e4066Sahrens 	return (FALSE);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate static struct xdr_ops *
xdrmem_ops(void)1937c478bd9Sstevel@tonic-gate xdrmem_ops(void)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	static struct xdr_ops ops;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (ops.x_getint32 == NULL) {
1987c478bd9Sstevel@tonic-gate 		ops.x_getbytes = xdrmem_getbytes;
1997c478bd9Sstevel@tonic-gate 		ops.x_putbytes = xdrmem_putbytes;
2007c478bd9Sstevel@tonic-gate 		ops.x_getpostn = xdrmem_getpos;
2017c478bd9Sstevel@tonic-gate 		ops.x_setpostn = xdrmem_setpos;
2027c478bd9Sstevel@tonic-gate 		ops.x_inline = xdrmem_inline;
2037c478bd9Sstevel@tonic-gate 		ops.x_destroy = xdrmem_destroy;
2047c478bd9Sstevel@tonic-gate 		ops.x_control = xdrmem_control;
2057c478bd9Sstevel@tonic-gate 		ops.x_getint32 = xdrmem_getint32;
2067c478bd9Sstevel@tonic-gate 		ops.x_putint32 = xdrmem_putint32;
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	return (&ops);
2097c478bd9Sstevel@tonic-gate }
210