xref: /illumos-gate/usr/src/uts/common/rpc/xdr_mem.c (revision 2d6eb4a5e0a47d30189497241345dc5466bb68ab)
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 /*
367c478bd9Sstevel@tonic-gate  * xdr_mem.c, XDR implementation using memory buffers.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * If you have some data to be interpreted as external data representation
397c478bd9Sstevel@tonic-gate  * or to be converted to external data representation in a memory buffer,
407c478bd9Sstevel@tonic-gate  * then this is the package for you.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <sys/param.h>
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/systm.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include <rpc/types.h>
487c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static struct xdr_ops *xdrmem_ops(void);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * The procedure xdrmem_create initializes a stream descriptor for a
547c478bd9Sstevel@tonic-gate  * memory buffer.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate void
xdrmem_create(XDR * xdrs,caddr_t addr,uint_t size,enum xdr_op op)57*fa9e4066Sahrens xdrmem_create(XDR *xdrs, caddr_t addr, uint_t size, enum xdr_op op)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	xdrs->x_op = op;
607c478bd9Sstevel@tonic-gate 	xdrs->x_ops = xdrmem_ops();
617c478bd9Sstevel@tonic-gate 	xdrs->x_private = xdrs->x_base = addr;
627c478bd9Sstevel@tonic-gate 	xdrs->x_handy = size;
637c478bd9Sstevel@tonic-gate 	xdrs->x_public = NULL;
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /* ARGSUSED */
677c478bd9Sstevel@tonic-gate static void
xdrmem_destroy(XDR * xdrs)687c478bd9Sstevel@tonic-gate xdrmem_destroy(XDR *xdrs)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static bool_t
xdrmem_getint32(XDR * xdrs,int32_t * int32p)737c478bd9Sstevel@tonic-gate xdrmem_getint32(XDR *xdrs, int32_t *int32p)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	if ((xdrs->x_handy -= (int)sizeof (int32_t)) < 0)
767c478bd9Sstevel@tonic-gate 		return (FALSE);
777c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
787c478bd9Sstevel@tonic-gate 	*int32p = (int32_t)ntohl((uint32_t)(*((int32_t *)(xdrs->x_private))));
797c478bd9Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
807c478bd9Sstevel@tonic-gate 	return (TRUE);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static bool_t
xdrmem_putint32(XDR * xdrs,int32_t * int32p)847c478bd9Sstevel@tonic-gate xdrmem_putint32(XDR *xdrs, int32_t *int32p)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	if ((xdrs->x_handy -= (int)sizeof (int32_t)) < 0)
877c478bd9Sstevel@tonic-gate 		return (FALSE);
887c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
897c478bd9Sstevel@tonic-gate 	*(int32_t *)xdrs->x_private = (int32_t)htonl((uint32_t)(*int32p));
907c478bd9Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
917c478bd9Sstevel@tonic-gate 	return (TRUE);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static bool_t
xdrmem_getbytes(XDR * xdrs,caddr_t addr,int len)957c478bd9Sstevel@tonic-gate xdrmem_getbytes(XDR *xdrs, caddr_t addr, int len)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	if ((xdrs->x_handy -= len) < 0)
987c478bd9Sstevel@tonic-gate 		return (FALSE);
997c478bd9Sstevel@tonic-gate 	bcopy(xdrs->x_private, addr, len);
1007c478bd9Sstevel@tonic-gate 	xdrs->x_private += len;
1017c478bd9Sstevel@tonic-gate 	return (TRUE);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static bool_t
xdrmem_putbytes(XDR * xdrs,caddr_t addr,int len)1057c478bd9Sstevel@tonic-gate xdrmem_putbytes(XDR *xdrs, caddr_t addr, int len)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	if ((xdrs->x_handy -= len) < 0)
1087c478bd9Sstevel@tonic-gate 		return (FALSE);
1097c478bd9Sstevel@tonic-gate 	bcopy(addr, xdrs->x_private, len);
1107c478bd9Sstevel@tonic-gate 	xdrs->x_private += len;
1117c478bd9Sstevel@tonic-gate 	return (TRUE);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
114*fa9e4066Sahrens static uint_t
xdrmem_getpos(XDR * xdrs)1157c478bd9Sstevel@tonic-gate xdrmem_getpos(XDR *xdrs)
1167c478bd9Sstevel@tonic-gate {
117*fa9e4066Sahrens 	return ((uint_t)((uintptr_t)xdrs->x_private - (uintptr_t)xdrs->x_base));
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate static bool_t
xdrmem_setpos(XDR * xdrs,uint_t pos)121*fa9e4066Sahrens xdrmem_setpos(XDR *xdrs, uint_t pos)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate 	caddr_t newaddr = xdrs->x_base + pos;
1247c478bd9Sstevel@tonic-gate 	caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
1257c478bd9Sstevel@tonic-gate 	ptrdiff_t diff;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (newaddr > lastaddr)
1287c478bd9Sstevel@tonic-gate 		return (FALSE);
1297c478bd9Sstevel@tonic-gate 	xdrs->x_private = newaddr;
1307c478bd9Sstevel@tonic-gate 	diff = lastaddr - newaddr;
1317c478bd9Sstevel@tonic-gate 	xdrs->x_handy = (int)diff;
1327c478bd9Sstevel@tonic-gate 	return (TRUE);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static rpc_inline_t *
xdrmem_inline(XDR * xdrs,int len)1367c478bd9Sstevel@tonic-gate xdrmem_inline(XDR *xdrs, int len)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	rpc_inline_t *buf = NULL;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if (xdrs->x_handy >= len) {
1417c478bd9Sstevel@tonic-gate 		xdrs->x_handy -= len;
1427c478bd9Sstevel@tonic-gate 		/* LINTED pointer alignment */
1437c478bd9Sstevel@tonic-gate 		buf = (rpc_inline_t *)xdrs->x_private;
1447c478bd9Sstevel@tonic-gate 		xdrs->x_private += len;
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 	return (buf);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate static bool_t
xdrmem_control(XDR * xdrs,int request,void * info)1507c478bd9Sstevel@tonic-gate xdrmem_control(XDR *xdrs, int request, void *info)
1517c478bd9Sstevel@tonic-gate {
152*fa9e4066Sahrens 	xdr_bytesrec *xptr;
1537c478bd9Sstevel@tonic-gate 	int32_t *int32p;
1547c478bd9Sstevel@tonic-gate 	int len;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	switch (request) {
157*fa9e4066Sahrens 
158*fa9e4066Sahrens 	case XDR_GET_BYTES_AVAIL:
159*fa9e4066Sahrens 		xptr = (xdr_bytesrec *)info;
160*fa9e4066Sahrens 		xptr->xc_is_last_record = TRUE;
161*fa9e4066Sahrens 		xptr->xc_num_avail = xdrs->x_handy;
162*fa9e4066Sahrens 		return (TRUE);
163*fa9e4066Sahrens 
1647c478bd9Sstevel@tonic-gate 	case XDR_PEEK:
1657c478bd9Sstevel@tonic-gate 		/*
1667c478bd9Sstevel@tonic-gate 		 * Return the next 4 byte unit in the XDR stream.
1677c478bd9Sstevel@tonic-gate 		 */
1687c478bd9Sstevel@tonic-gate 		if (xdrs->x_handy < sizeof (int32_t))
1697c478bd9Sstevel@tonic-gate 			return (FALSE);
1707c478bd9Sstevel@tonic-gate 		int32p = (int32_t *)info;
1717c478bd9Sstevel@tonic-gate 		*int32p = (int32_t)ntohl((uint32_t)
1727c478bd9Sstevel@tonic-gate 		    (*((int32_t *)(xdrs->x_private))));
1737c478bd9Sstevel@tonic-gate 		return (TRUE);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	case XDR_SKIPBYTES:
1767c478bd9Sstevel@tonic-gate 		/*
1777c478bd9Sstevel@tonic-gate 		 * Skip the next N bytes in the XDR stream.
1787c478bd9Sstevel@tonic-gate 		 */
1797c478bd9Sstevel@tonic-gate 		int32p = (int32_t *)info;
1807c478bd9Sstevel@tonic-gate 		len = RNDUP((int)(*int32p));
1817c478bd9Sstevel@tonic-gate 		if ((xdrs->x_handy -= len) < 0)
1827c478bd9Sstevel@tonic-gate 			return (FALSE);
1837c478bd9Sstevel@tonic-gate 		xdrs->x_private += len;
1847c478bd9Sstevel@tonic-gate 		return (TRUE);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	}
187*fa9e4066Sahrens 	return (FALSE);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate static struct xdr_ops *
xdrmem_ops(void)1917c478bd9Sstevel@tonic-gate xdrmem_ops(void)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	static struct xdr_ops ops;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	if (ops.x_getint32 == NULL) {
1967c478bd9Sstevel@tonic-gate 		ops.x_getbytes = xdrmem_getbytes;
1977c478bd9Sstevel@tonic-gate 		ops.x_putbytes = xdrmem_putbytes;
1987c478bd9Sstevel@tonic-gate 		ops.x_getpostn = xdrmem_getpos;
1997c478bd9Sstevel@tonic-gate 		ops.x_setpostn = xdrmem_setpos;
2007c478bd9Sstevel@tonic-gate 		ops.x_inline = xdrmem_inline;
2017c478bd9Sstevel@tonic-gate 		ops.x_destroy = xdrmem_destroy;
2027c478bd9Sstevel@tonic-gate 		ops.x_control = xdrmem_control;
2037c478bd9Sstevel@tonic-gate 		ops.x_getint32 = xdrmem_getint32;
2047c478bd9Sstevel@tonic-gate 		ops.x_putint32 = xdrmem_putint32;
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 	return (&ops);
2077c478bd9Sstevel@tonic-gate }
208