xref: /illumos-gate/usr/src/lib/libnsl/rpc/xdr_mem.c (revision 1da57d55)
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 /*
367c478bd9Sstevel@tonic-gate  * xdr_mem.h, 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 "mt.h"
447c478bd9Sstevel@tonic-gate #include "rpc_mt.h"
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <rpc/types.h>
477c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
487c478bd9Sstevel@tonic-gate #include <memory.h>
497c478bd9Sstevel@tonic-gate #include <inttypes.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static struct xdr_ops *xdrmem_ops(void);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * Meaning of the private areas of the xdr struct for xdr_mem
557c478bd9Sstevel@tonic-gate  * 	x_base : Base from where the xdr stream starts
567c478bd9Sstevel@tonic-gate  * 	x_private : The current position of the stream.
577c478bd9Sstevel@tonic-gate  * 	x_handy : The size of the stream buffer.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * The procedure xdrmem_create initializes a stream descriptor for a
627c478bd9Sstevel@tonic-gate  * memory buffer.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate void
xdrmem_create(XDR * xdrs,const caddr_t addr,const uint_t size,const enum xdr_op op)65*61961e0fSrobinson xdrmem_create(XDR *xdrs, const caddr_t addr, const uint_t size,
66*61961e0fSrobinson 							const enum xdr_op op)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	caddr_t eaddr = addr;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	xdrs->x_op = op;
717c478bd9Sstevel@tonic-gate 	xdrs->x_ops = xdrmem_ops();
727c478bd9Sstevel@tonic-gate 	xdrs->x_private = xdrs->x_base = 0;
737c478bd9Sstevel@tonic-gate 	/*
747c478bd9Sstevel@tonic-gate 	 * We check here that the size is with in the range of the
757c478bd9Sstevel@tonic-gate 	 * address space. If not we set x_handy to zero. This will cause
767c478bd9Sstevel@tonic-gate 	 * all xdrmem entry points to fail.
777c478bd9Sstevel@tonic-gate 	 */
787c478bd9Sstevel@tonic-gate 	eaddr = addr + size;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	if (eaddr < addr)
817c478bd9Sstevel@tonic-gate 		xdrs->x_handy = 0;
827c478bd9Sstevel@tonic-gate 	else {
837c478bd9Sstevel@tonic-gate 		xdrs->x_handy = size;
847c478bd9Sstevel@tonic-gate 		xdrs->x_private = xdrs->x_base = addr;
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
88*61961e0fSrobinson /* ARGSUSED */
897c478bd9Sstevel@tonic-gate static void
xdrmem_destroy(XDR * xdrs)907c478bd9Sstevel@tonic-gate xdrmem_destroy(XDR *xdrs)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static bool_t
xdrmem_getlong(XDR * xdrs,long * lp)957c478bd9Sstevel@tonic-gate xdrmem_getlong(XDR *xdrs, long *lp)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	if (sizeof (int32_t) > (uint32_t)xdrs->x_handy) {
987c478bd9Sstevel@tonic-gate 		xdrs->x_private += (uint_t)xdrs->x_handy;
997c478bd9Sstevel@tonic-gate 		xdrs->x_handy = 0;
1007c478bd9Sstevel@tonic-gate 		return (FALSE);
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 	xdrs->x_handy -= sizeof (int32_t);
103*61961e0fSrobinson 	/* LINTED pointer cast */
1047c478bd9Sstevel@tonic-gate 	*lp = (int32_t)ntohl((uint32_t)(*((int32_t *)(xdrs->x_private))));
1057c478bd9Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
1067c478bd9Sstevel@tonic-gate 	return (TRUE);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate static bool_t
xdrmem_putlong(XDR * xdrs,long * lp)1107c478bd9Sstevel@tonic-gate xdrmem_putlong(XDR *xdrs, long *lp)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate #if defined(_LP64)
113*61961e0fSrobinson 	if ((*lp > INT32_MAX) || (*lp < INT32_MIN))
1147c478bd9Sstevel@tonic-gate 		return (FALSE);
1157c478bd9Sstevel@tonic-gate #endif
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if ((sizeof (int32_t) > (uint32_t)xdrs->x_handy)) {
1187c478bd9Sstevel@tonic-gate 		xdrs->x_private += (uint_t)xdrs->x_handy;
1197c478bd9Sstevel@tonic-gate 		xdrs->x_handy = 0;
1207c478bd9Sstevel@tonic-gate 		return (FALSE);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 	xdrs->x_handy -= sizeof (int32_t);
123*61961e0fSrobinson 	/* LINTED pointer cast */
1247c478bd9Sstevel@tonic-gate 	*(int32_t *)xdrs->x_private = (int32_t)htonl((uint32_t)(*lp));
1257c478bd9Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
1267c478bd9Sstevel@tonic-gate 	return (TRUE);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #if defined(_LP64)
1307c478bd9Sstevel@tonic-gate static bool_t
xdrmem_getint32(XDR * xdrs,int32_t * ip)1317c478bd9Sstevel@tonic-gate xdrmem_getint32(XDR *xdrs, int32_t *ip)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	if (sizeof (int32_t) > (uint_t)xdrs->x_handy) {
1347c478bd9Sstevel@tonic-gate 		xdrs->x_private += (uint_t)xdrs->x_handy;
1357c478bd9Sstevel@tonic-gate 		xdrs->x_handy = 0;
1367c478bd9Sstevel@tonic-gate 		return (FALSE);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 	xdrs->x_handy -= sizeof (int32_t);
139*61961e0fSrobinson 	/* LINTED pointer cast */
1407c478bd9Sstevel@tonic-gate 	*ip = (int32_t)ntohl((uint32_t)(*((int32_t *)(xdrs->x_private))));
1417c478bd9Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
1427c478bd9Sstevel@tonic-gate 	return (TRUE);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate static bool_t
xdrmem_putint32(XDR * xdrs,int32_t * ip)1467c478bd9Sstevel@tonic-gate xdrmem_putint32(XDR *xdrs, int32_t *ip)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	if (sizeof (int32_t) > (uint32_t)xdrs->x_handy) {
1497c478bd9Sstevel@tonic-gate 		xdrs->x_private += (uint_t)xdrs->x_handy;
1507c478bd9Sstevel@tonic-gate 		xdrs->x_handy = 0;
1517c478bd9Sstevel@tonic-gate 		return (FALSE);
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 	xdrs->x_handy -= sizeof (int32_t);
154*61961e0fSrobinson 	/* LINTED pointer cast */
1557c478bd9Sstevel@tonic-gate 	*(int32_t *)xdrs->x_private = (int32_t)htonl((uint32_t)(*ip));
1567c478bd9Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
1577c478bd9Sstevel@tonic-gate 	return (TRUE);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate #endif /* _LP64 */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate static bool_t
xdrmem_getbytes(XDR * xdrs,caddr_t addr,int len)1627c478bd9Sstevel@tonic-gate xdrmem_getbytes(XDR *xdrs, caddr_t addr, int len)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	if ((uint32_t)len > (uint32_t)xdrs->x_handy) {
1657c478bd9Sstevel@tonic-gate 		xdrs->x_private += (uint_t)xdrs->x_handy;
1667c478bd9Sstevel@tonic-gate 		xdrs->x_handy = 0;
1677c478bd9Sstevel@tonic-gate 		return (FALSE);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 	xdrs->x_handy -= len;
1707c478bd9Sstevel@tonic-gate 	(void) memcpy(addr, xdrs->x_private, (uint_t)len);
1717c478bd9Sstevel@tonic-gate 	xdrs->x_private += (uint_t)len;
1727c478bd9Sstevel@tonic-gate 	return (TRUE);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate static bool_t
xdrmem_putbytes(XDR * xdrs,caddr_t addr,int len)1767c478bd9Sstevel@tonic-gate xdrmem_putbytes(XDR *xdrs, caddr_t addr, int len)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	if ((uint32_t)len > (uint32_t)xdrs->x_handy) {
1797c478bd9Sstevel@tonic-gate 		xdrs->x_private += (uint_t)xdrs->x_handy;
1807c478bd9Sstevel@tonic-gate 		xdrs->x_handy = 0;
1817c478bd9Sstevel@tonic-gate 		return (FALSE);
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 	xdrs->x_handy -= len;
1847c478bd9Sstevel@tonic-gate 	(void) memcpy(xdrs->x_private, addr, (uint_t)len);
1857c478bd9Sstevel@tonic-gate 	xdrs->x_private += (uint_t)len;
1867c478bd9Sstevel@tonic-gate 	return (TRUE);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate static uint_t
xdrmem_getpos(XDR * xdrs)190*61961e0fSrobinson xdrmem_getpos(XDR *xdrs)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	return (uint_t)((uintptr_t)xdrs->x_private - (uintptr_t)xdrs->x_base);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate static bool_t
xdrmem_setpos(XDR * xdrs,uint_t pos)196*61961e0fSrobinson xdrmem_setpos(XDR *xdrs, uint_t pos)
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	caddr_t newaddr = xdrs->x_base + pos;
1997c478bd9Sstevel@tonic-gate 	caddr_t lastaddr = xdrs->x_private + (uint_t)xdrs->x_handy;
2007c478bd9Sstevel@tonic-gate 
201*61961e0fSrobinson 	if ((long)newaddr > (long)lastaddr)
2027c478bd9Sstevel@tonic-gate 		return (FALSE);
2037c478bd9Sstevel@tonic-gate 	xdrs->x_private = newaddr;
2047c478bd9Sstevel@tonic-gate 	xdrs->x_handy = (int)((uintptr_t)lastaddr - (uintptr_t)newaddr);
2057c478bd9Sstevel@tonic-gate 	return (TRUE);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate static rpc_inline_t *
xdrmem_inline(XDR * xdrs,int len)2097c478bd9Sstevel@tonic-gate xdrmem_inline(XDR *xdrs, int len)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	rpc_inline_t *buf = 0;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if ((uint32_t)xdrs->x_handy >= (uint32_t)len) {
2147c478bd9Sstevel@tonic-gate 		xdrs->x_handy -= len;
215*61961e0fSrobinson 		/* LINTED pointer cast */
2167c478bd9Sstevel@tonic-gate 		buf = (rpc_inline_t *)xdrs->x_private;
2177c478bd9Sstevel@tonic-gate 		xdrs->x_private += (uint_t)len;
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 	return (buf);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate static bool_t
xdrmem_control(XDR * xdrs,int request,void * info)2237c478bd9Sstevel@tonic-gate xdrmem_control(XDR *xdrs, int request, void *info)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	xdr_bytesrec *xptr;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	switch (request) {
2287c478bd9Sstevel@tonic-gate 	case XDR_GET_BYTES_AVAIL:
2297c478bd9Sstevel@tonic-gate 		xptr = (xdr_bytesrec *) info;
2307c478bd9Sstevel@tonic-gate 		xptr->xc_is_last_record = TRUE;
2317c478bd9Sstevel@tonic-gate 		xptr->xc_num_avail = xdrs->x_handy;
2327c478bd9Sstevel@tonic-gate 		return (TRUE);
2337c478bd9Sstevel@tonic-gate 	default:
2347c478bd9Sstevel@tonic-gate 		return (FALSE);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate static struct xdr_ops *
xdrmem_ops(void)241*61961e0fSrobinson xdrmem_ops(void)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	static struct xdr_ops ops;
2447c478bd9Sstevel@tonic-gate 	extern mutex_t	ops_lock;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY ops_lock: ops */
247*61961e0fSrobinson 	(void) mutex_lock(&ops_lock);
2487c478bd9Sstevel@tonic-gate 	if (ops.x_getlong == NULL) {
2497c478bd9Sstevel@tonic-gate 		ops.x_getlong = xdrmem_getlong;
2507c478bd9Sstevel@tonic-gate 		ops.x_putlong = xdrmem_putlong;
2517c478bd9Sstevel@tonic-gate 		ops.x_getbytes = xdrmem_getbytes;
2527c478bd9Sstevel@tonic-gate 		ops.x_putbytes = xdrmem_putbytes;
2537c478bd9Sstevel@tonic-gate 		ops.x_getpostn = xdrmem_getpos;
2547c478bd9Sstevel@tonic-gate 		ops.x_setpostn = xdrmem_setpos;
2557c478bd9Sstevel@tonic-gate 		ops.x_inline = xdrmem_inline;
2567c478bd9Sstevel@tonic-gate 		ops.x_destroy = xdrmem_destroy;
2577c478bd9Sstevel@tonic-gate 		ops.x_control = xdrmem_control;
2587c478bd9Sstevel@tonic-gate #if defined(_LP64)
2597c478bd9Sstevel@tonic-gate 		ops.x_getint32 = xdrmem_getint32;
2607c478bd9Sstevel@tonic-gate 		ops.x_putint32 = xdrmem_putint32;
2617c478bd9Sstevel@tonic-gate #endif
2627c478bd9Sstevel@tonic-gate 	}
263*61961e0fSrobinson 	(void) mutex_unlock(&ops_lock);
2647c478bd9Sstevel@tonic-gate 	return (&ops);
2657c478bd9Sstevel@tonic-gate }
266