xref: /onnv-gate/usr/src/lib/libnsl/rpc/xdr_mem.c (revision 132:e3f7eaf7dde4)
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
21*132Srobinson  */
22*132Srobinson 
23*132Srobinson /*
24*132Srobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
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  * Portions of this source code were derived from Berkeley
310Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
320Sstevel@tonic-gate  * 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.h, 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 "mt.h"
460Sstevel@tonic-gate #include "rpc_mt.h"
470Sstevel@tonic-gate #include <sys/types.h>
480Sstevel@tonic-gate #include <rpc/types.h>
490Sstevel@tonic-gate #include <rpc/xdr.h>
500Sstevel@tonic-gate #include <memory.h>
510Sstevel@tonic-gate #include <inttypes.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static struct xdr_ops *xdrmem_ops(void);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * Meaning of the private areas of the xdr struct for xdr_mem
570Sstevel@tonic-gate  * 	x_base : Base from where the xdr stream starts
580Sstevel@tonic-gate  * 	x_private : The current position of the stream.
590Sstevel@tonic-gate  * 	x_handy : The size of the stream buffer.
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * The procedure xdrmem_create initializes a stream descriptor for a
640Sstevel@tonic-gate  * memory buffer.
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate void
xdrmem_create(XDR * xdrs,const caddr_t addr,const uint_t size,const enum xdr_op op)67*132Srobinson xdrmem_create(XDR *xdrs, const caddr_t addr, const uint_t size,
68*132Srobinson 							const enum xdr_op op)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	caddr_t eaddr = addr;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	xdrs->x_op = op;
730Sstevel@tonic-gate 	xdrs->x_ops = xdrmem_ops();
740Sstevel@tonic-gate 	xdrs->x_private = xdrs->x_base = 0;
750Sstevel@tonic-gate 	/*
760Sstevel@tonic-gate 	 * We check here that the size is with in the range of the
770Sstevel@tonic-gate 	 * address space. If not we set x_handy to zero. This will cause
780Sstevel@tonic-gate 	 * all xdrmem entry points to fail.
790Sstevel@tonic-gate 	 */
800Sstevel@tonic-gate 	eaddr = addr + size;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	if (eaddr < addr)
830Sstevel@tonic-gate 		xdrs->x_handy = 0;
840Sstevel@tonic-gate 	else {
850Sstevel@tonic-gate 		xdrs->x_handy = size;
860Sstevel@tonic-gate 		xdrs->x_private = xdrs->x_base = addr;
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
90*132Srobinson /* ARGSUSED */
910Sstevel@tonic-gate static void
xdrmem_destroy(XDR * xdrs)920Sstevel@tonic-gate xdrmem_destroy(XDR *xdrs)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static bool_t
xdrmem_getlong(XDR * xdrs,long * lp)970Sstevel@tonic-gate xdrmem_getlong(XDR *xdrs, long *lp)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	if (sizeof (int32_t) > (uint32_t)xdrs->x_handy) {
1000Sstevel@tonic-gate 		xdrs->x_private += (uint_t)xdrs->x_handy;
1010Sstevel@tonic-gate 		xdrs->x_handy = 0;
1020Sstevel@tonic-gate 		return (FALSE);
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 	xdrs->x_handy -= sizeof (int32_t);
105*132Srobinson 	/* LINTED pointer cast */
1060Sstevel@tonic-gate 	*lp = (int32_t)ntohl((uint32_t)(*((int32_t *)(xdrs->x_private))));
1070Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
1080Sstevel@tonic-gate 	return (TRUE);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static bool_t
xdrmem_putlong(XDR * xdrs,long * lp)1120Sstevel@tonic-gate xdrmem_putlong(XDR *xdrs, long *lp)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate #if defined(_LP64)
115*132Srobinson 	if ((*lp > INT32_MAX) || (*lp < INT32_MIN))
1160Sstevel@tonic-gate 		return (FALSE);
1170Sstevel@tonic-gate #endif
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if ((sizeof (int32_t) > (uint32_t)xdrs->x_handy)) {
1200Sstevel@tonic-gate 		xdrs->x_private += (uint_t)xdrs->x_handy;
1210Sstevel@tonic-gate 		xdrs->x_handy = 0;
1220Sstevel@tonic-gate 		return (FALSE);
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate 	xdrs->x_handy -= sizeof (int32_t);
125*132Srobinson 	/* LINTED pointer cast */
1260Sstevel@tonic-gate 	*(int32_t *)xdrs->x_private = (int32_t)htonl((uint32_t)(*lp));
1270Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
1280Sstevel@tonic-gate 	return (TRUE);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate #if defined(_LP64)
1320Sstevel@tonic-gate static bool_t
xdrmem_getint32(XDR * xdrs,int32_t * ip)1330Sstevel@tonic-gate xdrmem_getint32(XDR *xdrs, int32_t *ip)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 	if (sizeof (int32_t) > (uint_t)xdrs->x_handy) {
1360Sstevel@tonic-gate 		xdrs->x_private += (uint_t)xdrs->x_handy;
1370Sstevel@tonic-gate 		xdrs->x_handy = 0;
1380Sstevel@tonic-gate 		return (FALSE);
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 	xdrs->x_handy -= sizeof (int32_t);
141*132Srobinson 	/* LINTED pointer cast */
1420Sstevel@tonic-gate 	*ip = (int32_t)ntohl((uint32_t)(*((int32_t *)(xdrs->x_private))));
1430Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
1440Sstevel@tonic-gate 	return (TRUE);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate static bool_t
xdrmem_putint32(XDR * xdrs,int32_t * ip)1480Sstevel@tonic-gate xdrmem_putint32(XDR *xdrs, int32_t *ip)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	if (sizeof (int32_t) > (uint32_t)xdrs->x_handy) {
1510Sstevel@tonic-gate 		xdrs->x_private += (uint_t)xdrs->x_handy;
1520Sstevel@tonic-gate 		xdrs->x_handy = 0;
1530Sstevel@tonic-gate 		return (FALSE);
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 	xdrs->x_handy -= sizeof (int32_t);
156*132Srobinson 	/* LINTED pointer cast */
1570Sstevel@tonic-gate 	*(int32_t *)xdrs->x_private = (int32_t)htonl((uint32_t)(*ip));
1580Sstevel@tonic-gate 	xdrs->x_private += sizeof (int32_t);
1590Sstevel@tonic-gate 	return (TRUE);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate #endif /* _LP64 */
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate static bool_t
xdrmem_getbytes(XDR * xdrs,caddr_t addr,int len)1640Sstevel@tonic-gate xdrmem_getbytes(XDR *xdrs, caddr_t addr, int len)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate 	if ((uint32_t)len > (uint32_t)xdrs->x_handy) {
1670Sstevel@tonic-gate 		xdrs->x_private += (uint_t)xdrs->x_handy;
1680Sstevel@tonic-gate 		xdrs->x_handy = 0;
1690Sstevel@tonic-gate 		return (FALSE);
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 	xdrs->x_handy -= len;
1720Sstevel@tonic-gate 	(void) memcpy(addr, xdrs->x_private, (uint_t)len);
1730Sstevel@tonic-gate 	xdrs->x_private += (uint_t)len;
1740Sstevel@tonic-gate 	return (TRUE);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate static bool_t
xdrmem_putbytes(XDR * xdrs,caddr_t addr,int len)1780Sstevel@tonic-gate xdrmem_putbytes(XDR *xdrs, caddr_t addr, int len)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	if ((uint32_t)len > (uint32_t)xdrs->x_handy) {
1810Sstevel@tonic-gate 		xdrs->x_private += (uint_t)xdrs->x_handy;
1820Sstevel@tonic-gate 		xdrs->x_handy = 0;
1830Sstevel@tonic-gate 		return (FALSE);
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 	xdrs->x_handy -= len;
1860Sstevel@tonic-gate 	(void) memcpy(xdrs->x_private, addr, (uint_t)len);
1870Sstevel@tonic-gate 	xdrs->x_private += (uint_t)len;
1880Sstevel@tonic-gate 	return (TRUE);
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate static uint_t
xdrmem_getpos(XDR * xdrs)192*132Srobinson xdrmem_getpos(XDR *xdrs)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate 	return (uint_t)((uintptr_t)xdrs->x_private - (uintptr_t)xdrs->x_base);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate static bool_t
xdrmem_setpos(XDR * xdrs,uint_t pos)198*132Srobinson xdrmem_setpos(XDR *xdrs, uint_t pos)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate 	caddr_t newaddr = xdrs->x_base + pos;
2010Sstevel@tonic-gate 	caddr_t lastaddr = xdrs->x_private + (uint_t)xdrs->x_handy;
2020Sstevel@tonic-gate 
203*132Srobinson 	if ((long)newaddr > (long)lastaddr)
2040Sstevel@tonic-gate 		return (FALSE);
2050Sstevel@tonic-gate 	xdrs->x_private = newaddr;
2060Sstevel@tonic-gate 	xdrs->x_handy = (int)((uintptr_t)lastaddr - (uintptr_t)newaddr);
2070Sstevel@tonic-gate 	return (TRUE);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate static rpc_inline_t *
xdrmem_inline(XDR * xdrs,int len)2110Sstevel@tonic-gate xdrmem_inline(XDR *xdrs, int len)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate 	rpc_inline_t *buf = 0;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	if ((uint32_t)xdrs->x_handy >= (uint32_t)len) {
2160Sstevel@tonic-gate 		xdrs->x_handy -= len;
217*132Srobinson 		/* LINTED pointer cast */
2180Sstevel@tonic-gate 		buf = (rpc_inline_t *)xdrs->x_private;
2190Sstevel@tonic-gate 		xdrs->x_private += (uint_t)len;
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 	return (buf);
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate static bool_t
xdrmem_control(XDR * xdrs,int request,void * info)2250Sstevel@tonic-gate xdrmem_control(XDR *xdrs, int request, void *info)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	xdr_bytesrec *xptr;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	switch (request) {
2300Sstevel@tonic-gate 	case XDR_GET_BYTES_AVAIL:
2310Sstevel@tonic-gate 		xptr = (xdr_bytesrec *) info;
2320Sstevel@tonic-gate 		xptr->xc_is_last_record = TRUE;
2330Sstevel@tonic-gate 		xptr->xc_num_avail = xdrs->x_handy;
2340Sstevel@tonic-gate 		return (TRUE);
2350Sstevel@tonic-gate 	default:
2360Sstevel@tonic-gate 		return (FALSE);
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate static struct xdr_ops *
xdrmem_ops(void)243*132Srobinson xdrmem_ops(void)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate 	static struct xdr_ops ops;
2460Sstevel@tonic-gate 	extern mutex_t	ops_lock;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate /* VARIABLES PROTECTED BY ops_lock: ops */
249*132Srobinson 	(void) mutex_lock(&ops_lock);
2500Sstevel@tonic-gate 	if (ops.x_getlong == NULL) {
2510Sstevel@tonic-gate 		ops.x_getlong = xdrmem_getlong;
2520Sstevel@tonic-gate 		ops.x_putlong = xdrmem_putlong;
2530Sstevel@tonic-gate 		ops.x_getbytes = xdrmem_getbytes;
2540Sstevel@tonic-gate 		ops.x_putbytes = xdrmem_putbytes;
2550Sstevel@tonic-gate 		ops.x_getpostn = xdrmem_getpos;
2560Sstevel@tonic-gate 		ops.x_setpostn = xdrmem_setpos;
2570Sstevel@tonic-gate 		ops.x_inline = xdrmem_inline;
2580Sstevel@tonic-gate 		ops.x_destroy = xdrmem_destroy;
2590Sstevel@tonic-gate 		ops.x_control = xdrmem_control;
2600Sstevel@tonic-gate #if defined(_LP64)
2610Sstevel@tonic-gate 		ops.x_getint32 = xdrmem_getint32;
2620Sstevel@tonic-gate 		ops.x_putint32 = xdrmem_putint32;
2630Sstevel@tonic-gate #endif
2640Sstevel@tonic-gate 	}
265*132Srobinson 	(void) mutex_unlock(&ops_lock);
2660Sstevel@tonic-gate 	return (&ops);
2670Sstevel@tonic-gate }
268