xref: /titanic_50/usr/src/lib/libnsl/rpc/xdr_mem.c (revision 61961e0f20c7637a3846bb39786bb9dffa91dfb9)
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
21*61961e0fSrobinson  */
22*61961e0fSrobinson 
23*61961e0fSrobinson /*
24*61961e0fSrobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
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  * Portions of this source code were derived from Berkeley
317c478bd9Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
327c478bd9Sstevel@tonic-gate  * 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.h, 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 "mt.h"
467c478bd9Sstevel@tonic-gate #include "rpc_mt.h"
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate #include <rpc/types.h>
497c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
507c478bd9Sstevel@tonic-gate #include <memory.h>
517c478bd9Sstevel@tonic-gate #include <inttypes.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static struct xdr_ops *xdrmem_ops(void);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * Meaning of the private areas of the xdr struct for xdr_mem
577c478bd9Sstevel@tonic-gate  * 	x_base : Base from where the xdr stream starts
587c478bd9Sstevel@tonic-gate  * 	x_private : The current position of the stream.
597c478bd9Sstevel@tonic-gate  * 	x_handy : The size of the stream buffer.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * The procedure xdrmem_create initializes a stream descriptor for a
647c478bd9Sstevel@tonic-gate  * memory buffer.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate void
xdrmem_create(XDR * xdrs,const caddr_t addr,const uint_t size,const enum xdr_op op)67*61961e0fSrobinson xdrmem_create(XDR *xdrs, const caddr_t addr, const uint_t size,
68*61961e0fSrobinson 							const enum xdr_op op)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	caddr_t eaddr = addr;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	xdrs->x_op = op;
737c478bd9Sstevel@tonic-gate 	xdrs->x_ops = xdrmem_ops();
747c478bd9Sstevel@tonic-gate 	xdrs->x_private = xdrs->x_base = 0;
757c478bd9Sstevel@tonic-gate 	/*
767c478bd9Sstevel@tonic-gate 	 * We check here that the size is with in the range of the
777c478bd9Sstevel@tonic-gate 	 * address space. If not we set x_handy to zero. This will cause
787c478bd9Sstevel@tonic-gate 	 * all xdrmem entry points to fail.
797c478bd9Sstevel@tonic-gate 	 */
807c478bd9Sstevel@tonic-gate 	eaddr = addr + size;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	if (eaddr < addr)
837c478bd9Sstevel@tonic-gate 		xdrs->x_handy = 0;
847c478bd9Sstevel@tonic-gate 	else {
857c478bd9Sstevel@tonic-gate 		xdrs->x_handy = size;
867c478bd9Sstevel@tonic-gate 		xdrs->x_private = xdrs->x_base = addr;
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
90*61961e0fSrobinson /* ARGSUSED */
917c478bd9Sstevel@tonic-gate static void
xdrmem_destroy(XDR * xdrs)927c478bd9Sstevel@tonic-gate xdrmem_destroy(XDR *xdrs)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static bool_t
xdrmem_getlong(XDR * xdrs,long * lp)977c478bd9Sstevel@tonic-gate xdrmem_getlong(XDR *xdrs, long *lp)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	if (sizeof (int32_t) > (uint32_t)xdrs->x_handy) {
1007c478bd9Sstevel@tonic-gate 		xdrs->x_private += (uint_t)xdrs->x_handy;
1017c478bd9Sstevel@tonic-gate 		xdrs->x_handy = 0;
1027c478bd9Sstevel@tonic-gate 		return (FALSE);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 	xdrs->x_handy -= sizeof (int32_t);
105*61961e0fSrobinson 	/* LINTED pointer cast */
1067c478bd9Sstevel@tonic-gate 	*lp = (int32_t)ntohl((uint32_t)(*((int32_t *)(xdrs->x_private))));
1077c478bd9Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
1087c478bd9Sstevel@tonic-gate 	return (TRUE);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static bool_t
xdrmem_putlong(XDR * xdrs,long * lp)1127c478bd9Sstevel@tonic-gate xdrmem_putlong(XDR *xdrs, long *lp)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate #if defined(_LP64)
115*61961e0fSrobinson 	if ((*lp > INT32_MAX) || (*lp < INT32_MIN))
1167c478bd9Sstevel@tonic-gate 		return (FALSE);
1177c478bd9Sstevel@tonic-gate #endif
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if ((sizeof (int32_t) > (uint32_t)xdrs->x_handy)) {
1207c478bd9Sstevel@tonic-gate 		xdrs->x_private += (uint_t)xdrs->x_handy;
1217c478bd9Sstevel@tonic-gate 		xdrs->x_handy = 0;
1227c478bd9Sstevel@tonic-gate 		return (FALSE);
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 	xdrs->x_handy -= sizeof (int32_t);
125*61961e0fSrobinson 	/* LINTED pointer cast */
1267c478bd9Sstevel@tonic-gate 	*(int32_t *)xdrs->x_private = (int32_t)htonl((uint32_t)(*lp));
1277c478bd9Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
1287c478bd9Sstevel@tonic-gate 	return (TRUE);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #if defined(_LP64)
1327c478bd9Sstevel@tonic-gate static bool_t
xdrmem_getint32(XDR * xdrs,int32_t * ip)1337c478bd9Sstevel@tonic-gate xdrmem_getint32(XDR *xdrs, int32_t *ip)
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate 	if (sizeof (int32_t) > (uint_t)xdrs->x_handy) {
1367c478bd9Sstevel@tonic-gate 		xdrs->x_private += (uint_t)xdrs->x_handy;
1377c478bd9Sstevel@tonic-gate 		xdrs->x_handy = 0;
1387c478bd9Sstevel@tonic-gate 		return (FALSE);
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 	xdrs->x_handy -= sizeof (int32_t);
141*61961e0fSrobinson 	/* LINTED pointer cast */
1427c478bd9Sstevel@tonic-gate 	*ip = (int32_t)ntohl((uint32_t)(*((int32_t *)(xdrs->x_private))));
1437c478bd9Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
1447c478bd9Sstevel@tonic-gate 	return (TRUE);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static bool_t
xdrmem_putint32(XDR * xdrs,int32_t * ip)1487c478bd9Sstevel@tonic-gate xdrmem_putint32(XDR *xdrs, int32_t *ip)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	if (sizeof (int32_t) > (uint32_t)xdrs->x_handy) {
1517c478bd9Sstevel@tonic-gate 		xdrs->x_private += (uint_t)xdrs->x_handy;
1527c478bd9Sstevel@tonic-gate 		xdrs->x_handy = 0;
1537c478bd9Sstevel@tonic-gate 		return (FALSE);
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 	xdrs->x_handy -= sizeof (int32_t);
156*61961e0fSrobinson 	/* LINTED pointer cast */
1577c478bd9Sstevel@tonic-gate 	*(int32_t *)xdrs->x_private = (int32_t)htonl((uint32_t)(*ip));
1587c478bd9Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
1597c478bd9Sstevel@tonic-gate 	return (TRUE);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate #endif /* _LP64 */
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate static bool_t
xdrmem_getbytes(XDR * xdrs,caddr_t addr,int len)1647c478bd9Sstevel@tonic-gate xdrmem_getbytes(XDR *xdrs, caddr_t addr, int len)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	if ((uint32_t)len > (uint32_t)xdrs->x_handy) {
1677c478bd9Sstevel@tonic-gate 		xdrs->x_private += (uint_t)xdrs->x_handy;
1687c478bd9Sstevel@tonic-gate 		xdrs->x_handy = 0;
1697c478bd9Sstevel@tonic-gate 		return (FALSE);
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 	xdrs->x_handy -= len;
1727c478bd9Sstevel@tonic-gate 	(void) memcpy(addr, xdrs->x_private, (uint_t)len);
1737c478bd9Sstevel@tonic-gate 	xdrs->x_private += (uint_t)len;
1747c478bd9Sstevel@tonic-gate 	return (TRUE);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate static bool_t
xdrmem_putbytes(XDR * xdrs,caddr_t addr,int len)1787c478bd9Sstevel@tonic-gate xdrmem_putbytes(XDR *xdrs, caddr_t addr, int len)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	if ((uint32_t)len > (uint32_t)xdrs->x_handy) {
1817c478bd9Sstevel@tonic-gate 		xdrs->x_private += (uint_t)xdrs->x_handy;
1827c478bd9Sstevel@tonic-gate 		xdrs->x_handy = 0;
1837c478bd9Sstevel@tonic-gate 		return (FALSE);
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 	xdrs->x_handy -= len;
1867c478bd9Sstevel@tonic-gate 	(void) memcpy(xdrs->x_private, addr, (uint_t)len);
1877c478bd9Sstevel@tonic-gate 	xdrs->x_private += (uint_t)len;
1887c478bd9Sstevel@tonic-gate 	return (TRUE);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate static uint_t
xdrmem_getpos(XDR * xdrs)192*61961e0fSrobinson xdrmem_getpos(XDR *xdrs)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	return (uint_t)((uintptr_t)xdrs->x_private - (uintptr_t)xdrs->x_base);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate static bool_t
xdrmem_setpos(XDR * xdrs,uint_t pos)198*61961e0fSrobinson xdrmem_setpos(XDR *xdrs, uint_t pos)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	caddr_t newaddr = xdrs->x_base + pos;
2017c478bd9Sstevel@tonic-gate 	caddr_t lastaddr = xdrs->x_private + (uint_t)xdrs->x_handy;
2027c478bd9Sstevel@tonic-gate 
203*61961e0fSrobinson 	if ((long)newaddr > (long)lastaddr)
2047c478bd9Sstevel@tonic-gate 		return (FALSE);
2057c478bd9Sstevel@tonic-gate 	xdrs->x_private = newaddr;
2067c478bd9Sstevel@tonic-gate 	xdrs->x_handy = (int)((uintptr_t)lastaddr - (uintptr_t)newaddr);
2077c478bd9Sstevel@tonic-gate 	return (TRUE);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate static rpc_inline_t *
xdrmem_inline(XDR * xdrs,int len)2117c478bd9Sstevel@tonic-gate xdrmem_inline(XDR *xdrs, int len)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	rpc_inline_t *buf = 0;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if ((uint32_t)xdrs->x_handy >= (uint32_t)len) {
2167c478bd9Sstevel@tonic-gate 		xdrs->x_handy -= len;
217*61961e0fSrobinson 		/* LINTED pointer cast */
2187c478bd9Sstevel@tonic-gate 		buf = (rpc_inline_t *)xdrs->x_private;
2197c478bd9Sstevel@tonic-gate 		xdrs->x_private += (uint_t)len;
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 	return (buf);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate static bool_t
xdrmem_control(XDR * xdrs,int request,void * info)2257c478bd9Sstevel@tonic-gate xdrmem_control(XDR *xdrs, int request, void *info)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	xdr_bytesrec *xptr;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	switch (request) {
2307c478bd9Sstevel@tonic-gate 	case XDR_GET_BYTES_AVAIL:
2317c478bd9Sstevel@tonic-gate 		xptr = (xdr_bytesrec *) info;
2327c478bd9Sstevel@tonic-gate 		xptr->xc_is_last_record = TRUE;
2337c478bd9Sstevel@tonic-gate 		xptr->xc_num_avail = xdrs->x_handy;
2347c478bd9Sstevel@tonic-gate 		return (TRUE);
2357c478bd9Sstevel@tonic-gate 	default:
2367c478bd9Sstevel@tonic-gate 		return (FALSE);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate static struct xdr_ops *
xdrmem_ops(void)243*61961e0fSrobinson xdrmem_ops(void)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	static struct xdr_ops ops;
2467c478bd9Sstevel@tonic-gate 	extern mutex_t	ops_lock;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY ops_lock: ops */
249*61961e0fSrobinson 	(void) mutex_lock(&ops_lock);
2507c478bd9Sstevel@tonic-gate 	if (ops.x_getlong == NULL) {
2517c478bd9Sstevel@tonic-gate 		ops.x_getlong = xdrmem_getlong;
2527c478bd9Sstevel@tonic-gate 		ops.x_putlong = xdrmem_putlong;
2537c478bd9Sstevel@tonic-gate 		ops.x_getbytes = xdrmem_getbytes;
2547c478bd9Sstevel@tonic-gate 		ops.x_putbytes = xdrmem_putbytes;
2557c478bd9Sstevel@tonic-gate 		ops.x_getpostn = xdrmem_getpos;
2567c478bd9Sstevel@tonic-gate 		ops.x_setpostn = xdrmem_setpos;
2577c478bd9Sstevel@tonic-gate 		ops.x_inline = xdrmem_inline;
2587c478bd9Sstevel@tonic-gate 		ops.x_destroy = xdrmem_destroy;
2597c478bd9Sstevel@tonic-gate 		ops.x_control = xdrmem_control;
2607c478bd9Sstevel@tonic-gate #if defined(_LP64)
2617c478bd9Sstevel@tonic-gate 		ops.x_getint32 = xdrmem_getint32;
2627c478bd9Sstevel@tonic-gate 		ops.x_putint32 = xdrmem_putint32;
2637c478bd9Sstevel@tonic-gate #endif
2647c478bd9Sstevel@tonic-gate 	}
265*61961e0fSrobinson 	(void) mutex_unlock(&ops_lock);
2667c478bd9Sstevel@tonic-gate 	return (&ops);
2677c478bd9Sstevel@tonic-gate }
268