xref: /onnv-gate/usr/src/lib/libnsl/rpc/xdr.c (revision 1219:f89f56c2d9ac)
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
21132Srobinson  */
22132Srobinson 
23132Srobinson /*
24*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
27*1219Sraf 
280Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
290Sstevel@tonic-gate /* All Rights Reserved */
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
320Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
330Sstevel@tonic-gate  * California.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /*
39*1219Sraf  * Generic XDR routines implementation.
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * These are the "generic" xdr routines used to serialize and de-serialize
420Sstevel@tonic-gate  * most common data items.  See xdr.h for more info on the interface to
430Sstevel@tonic-gate  * xdr.
440Sstevel@tonic-gate  */
45*1219Sraf #include "mt.h"
46132Srobinson #include <stdlib.h>
470Sstevel@tonic-gate #include <sys/types.h>
480Sstevel@tonic-gate #include <sys/isa_defs.h>
490Sstevel@tonic-gate #include <syslog.h>
500Sstevel@tonic-gate #include <stdio.h>
510Sstevel@tonic-gate #include <stdlib.h>
520Sstevel@tonic-gate #include <string.h>
530Sstevel@tonic-gate #include <limits.h>
540Sstevel@tonic-gate #include <rpc/types.h>
550Sstevel@tonic-gate #include <rpc/xdr.h>
560Sstevel@tonic-gate #include <inttypes.h>
570Sstevel@tonic-gate #include <sys/sysmacros.h>
58132Srobinson #include <assert.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #pragma weak xdr_int64_t = xdr_hyper
610Sstevel@tonic-gate #pragma weak xdr_uint64_t = xdr_u_hyper
620Sstevel@tonic-gate #pragma weak xdr_int32_t = xdr_int
630Sstevel@tonic-gate #pragma weak xdr_uint32_t = xdr_u_int
640Sstevel@tonic-gate #pragma weak xdr_int16_t = xdr_short
650Sstevel@tonic-gate #pragma weak xdr_uint16_t = xdr_u_short
660Sstevel@tonic-gate #pragma weak xdr_int8_t = xdr_char
670Sstevel@tonic-gate #pragma weak xdr_uint8_t = xdr_u_char
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
70132Srobinson  * The following routine was part of a workaround for an rpcgen
71132Srobinson  * that was fixed, this routine should be removed sometime.
72132Srobinson  */
73132Srobinson #pragma weak xdr_ulonglong_t = xdr_u_longlong_t
74132Srobinson 
75132Srobinson /*
760Sstevel@tonic-gate  * constants specific to the xdr "protocol"
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate #define	XDR_FALSE	((uint_t)0)
790Sstevel@tonic-gate #define	XDR_TRUE	((uint_t)1)
800Sstevel@tonic-gate #define	LASTUNSIGNED	((uint_t)0-1)
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /* fragment size to use when doing an xdr_string() */
830Sstevel@tonic-gate #define	FRAGMENT	65536
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate  * for unit alignment
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate static const char xdr_zero[BYTES_PER_XDR_UNIT]	= { 0 };
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate  * Free a data structure using XDR
920Sstevel@tonic-gate  * Not a filter, but a convenient utility nonetheless
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate void
xdr_free(xdrproc_t proc,char * objp)950Sstevel@tonic-gate xdr_free(xdrproc_t proc, char *objp)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	XDR x;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	x.x_op = XDR_FREE;
1000Sstevel@tonic-gate 	(*proc)(&x, objp);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * XDR nothing
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate bool_t
xdr_void(void)107132Srobinson xdr_void(void)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	return (TRUE);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate  * xdr_time_t  sends time_t value over the wire.
1140Sstevel@tonic-gate  * Due to RPC Protocol limitation, it can only send
1150Sstevel@tonic-gate  * up to 32-bit integer quantity over the wire.
1160Sstevel@tonic-gate  *
1170Sstevel@tonic-gate  */
1180Sstevel@tonic-gate bool_t
xdr_time_t(XDR * xdrs,time_t * tp)1190Sstevel@tonic-gate xdr_time_t(XDR *xdrs, time_t *tp)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	int32_t i;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	switch (xdrs->x_op) {
1240Sstevel@tonic-gate 	case XDR_ENCODE:
1250Sstevel@tonic-gate 	/*
1260Sstevel@tonic-gate 	 * Check for the time overflow, when encoding it.
1270Sstevel@tonic-gate 	 * Don't want to send OTW the time value too large to
1280Sstevel@tonic-gate 	 * handle by the protocol.
1290Sstevel@tonic-gate 	 */
1300Sstevel@tonic-gate #if defined(_LP64)
1310Sstevel@tonic-gate 	if (*tp > INT32_MAX)
1320Sstevel@tonic-gate 		*tp = INT32_MAX;
1330Sstevel@tonic-gate 	else if (*tp < INT32_MIN)
1340Sstevel@tonic-gate 		*tp = INT32_MIN;
1350Sstevel@tonic-gate #endif
1360Sstevel@tonic-gate 		i =  (int32_t)*tp;
137132Srobinson 		return (XDR_PUTINT32(xdrs, &i));
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	case XDR_DECODE:
140132Srobinson 		if (!XDR_GETINT32(xdrs, &i))
1410Sstevel@tonic-gate 			return (FALSE);
1420Sstevel@tonic-gate 		*tp = (time_t)i;
1430Sstevel@tonic-gate 		return (TRUE);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	case XDR_FREE:
1460Sstevel@tonic-gate 		return (TRUE);
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 	return (FALSE);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate /*
1520Sstevel@tonic-gate  * XDR integers
1530Sstevel@tonic-gate  */
1540Sstevel@tonic-gate bool_t
xdr_int(XDR * xdrs,int * ip)1550Sstevel@tonic-gate xdr_int(XDR *xdrs, int *ip)
1560Sstevel@tonic-gate {
157132Srobinson 	switch (xdrs->x_op) {
158132Srobinson 	case XDR_ENCODE:
1590Sstevel@tonic-gate 		return (XDR_PUTINT32(xdrs, ip));
160132Srobinson 	case XDR_DECODE:
1610Sstevel@tonic-gate 		return (XDR_GETINT32(xdrs, ip));
162132Srobinson 	case XDR_FREE:
1630Sstevel@tonic-gate 		return (TRUE);
164132Srobinson 	}
1650Sstevel@tonic-gate 	return (FALSE);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate  * XDR unsigned integers
1700Sstevel@tonic-gate  */
1710Sstevel@tonic-gate bool_t
xdr_u_int(XDR * xdrs,uint_t * up)1720Sstevel@tonic-gate xdr_u_int(XDR *xdrs, uint_t *up)
1730Sstevel@tonic-gate {
174132Srobinson 	switch (xdrs->x_op) {
175132Srobinson 	case XDR_ENCODE:
1760Sstevel@tonic-gate 		return (XDR_PUTINT32(xdrs, (int *)up));
177132Srobinson 	case XDR_DECODE:
1780Sstevel@tonic-gate 		return (XDR_GETINT32(xdrs, (int *)up));
179132Srobinson 	case XDR_FREE:
1800Sstevel@tonic-gate 		return (TRUE);
181132Srobinson 	}
1820Sstevel@tonic-gate 	return (FALSE);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate  * The definition of xdr_long()/xdr_u_long() is kept for backward
1870Sstevel@tonic-gate  * compatibitlity.
1880Sstevel@tonic-gate  * XDR long integers, same as xdr_u_long
1890Sstevel@tonic-gate  */
1900Sstevel@tonic-gate bool_t
xdr_long(XDR * xdrs,long * lp)1910Sstevel@tonic-gate xdr_long(XDR *xdrs, long *lp)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	int32_t i;
1940Sstevel@tonic-gate 
195132Srobinson 	switch (xdrs->x_op) {
196132Srobinson 	case XDR_ENCODE:
1970Sstevel@tonic-gate #if defined(_LP64)
198132Srobinson 		if ((*lp > INT32_MAX) || (*lp < INT32_MIN))
1990Sstevel@tonic-gate 			return (FALSE);
2000Sstevel@tonic-gate #endif
2010Sstevel@tonic-gate 		i = (int32_t)*lp;
202132Srobinson 		return (XDR_PUTINT32(xdrs, &i));
203132Srobinson 	case XDR_DECODE:
204132Srobinson 		if (!XDR_GETINT32(xdrs, &i))
205132Srobinson 			return (FALSE);
2060Sstevel@tonic-gate 		*lp = (long)i;
207132Srobinson 		return (TRUE);
208132Srobinson 	case XDR_FREE:
209132Srobinson 		return (TRUE);
210132Srobinson 	}
211132Srobinson 	return (FALSE);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate  * XDR unsigned long integers
2160Sstevel@tonic-gate  * same as xdr_long
2170Sstevel@tonic-gate  */
2180Sstevel@tonic-gate bool_t
xdr_u_long(XDR * xdrs,ulong_t * ulp)2190Sstevel@tonic-gate xdr_u_long(XDR *xdrs, ulong_t *ulp)
2200Sstevel@tonic-gate {
2210Sstevel@tonic-gate 	uint32_t ui;
2220Sstevel@tonic-gate 
223132Srobinson 	switch (xdrs->x_op) {
224132Srobinson 	case XDR_ENCODE:
2250Sstevel@tonic-gate #if defined(_LP64)
226132Srobinson 		if (*ulp > UINT32_MAX)
2270Sstevel@tonic-gate 			return (FALSE);
2280Sstevel@tonic-gate #endif
2290Sstevel@tonic-gate 		ui = (uint32_t)*ulp;
230132Srobinson 		return (XDR_PUTINT32(xdrs, (int32_t *)&ui));
231132Srobinson 	case XDR_DECODE:
232132Srobinson 		if (!XDR_GETINT32(xdrs, (int32_t *)&ui))
233132Srobinson 			return (FALSE);
2340Sstevel@tonic-gate 		*ulp = (ulong_t)ui;
235132Srobinson 		return (TRUE);
236132Srobinson 	case XDR_FREE:
237132Srobinson 		return (TRUE);
238132Srobinson 	}
239132Srobinson 	return (FALSE);
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate /*
2430Sstevel@tonic-gate  * XDR short integers
2440Sstevel@tonic-gate  */
2450Sstevel@tonic-gate bool_t
xdr_short(XDR * xdrs,short * sp)2460Sstevel@tonic-gate xdr_short(XDR *xdrs, short *sp)
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate 	int32_t l;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	switch (xdrs->x_op) {
2510Sstevel@tonic-gate 	case XDR_ENCODE:
2520Sstevel@tonic-gate 		l = (int32_t)*sp;
253132Srobinson 		return (XDR_PUTINT32(xdrs, &l));
2540Sstevel@tonic-gate 	case XDR_DECODE:
255132Srobinson 		if (!XDR_GETINT32(xdrs, &l))
2560Sstevel@tonic-gate 			return (FALSE);
2570Sstevel@tonic-gate 		*sp = (short)l;
2580Sstevel@tonic-gate 		return (TRUE);
2590Sstevel@tonic-gate 	case XDR_FREE:
2600Sstevel@tonic-gate 		return (TRUE);
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 	return (FALSE);
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate /*
2660Sstevel@tonic-gate  * XDR unsigned short integers
2670Sstevel@tonic-gate  */
2680Sstevel@tonic-gate bool_t
xdr_u_short(XDR * xdrs,ushort_t * usp)2690Sstevel@tonic-gate xdr_u_short(XDR *xdrs, ushort_t *usp)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate 	uint_t i;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	switch (xdrs->x_op) {
2740Sstevel@tonic-gate 	case XDR_ENCODE:
2750Sstevel@tonic-gate 		i = (uint_t)*usp;
276132Srobinson 		return (XDR_PUTINT32(xdrs, (int *)&i));
2770Sstevel@tonic-gate 	case XDR_DECODE:
278132Srobinson 		if (!XDR_GETINT32(xdrs, (int *)&i))
2790Sstevel@tonic-gate 			return (FALSE);
2800Sstevel@tonic-gate 		*usp = (ushort_t)i;
2810Sstevel@tonic-gate 		return (TRUE);
2820Sstevel@tonic-gate 	case XDR_FREE:
2830Sstevel@tonic-gate 		return (TRUE);
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 	return (FALSE);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate /*
2900Sstevel@tonic-gate  * XDR a char
2910Sstevel@tonic-gate  */
2920Sstevel@tonic-gate bool_t
xdr_char(XDR * xdrs,char * cp)2930Sstevel@tonic-gate xdr_char(XDR *xdrs, char *cp)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate 	int i;
2960Sstevel@tonic-gate 
297132Srobinson 	switch (xdrs->x_op) {
298132Srobinson 	case XDR_ENCODE:
2990Sstevel@tonic-gate 		i = (*cp);
300132Srobinson 		return (XDR_PUTINT32(xdrs, &i));
301132Srobinson 	case XDR_DECODE:
302132Srobinson 		if (!XDR_GETINT32(xdrs, &i))
303132Srobinson 			return (FALSE);
304132Srobinson 		*cp = (char)i;
305132Srobinson 		return (TRUE);
306132Srobinson 	case XDR_FREE:
307132Srobinson 		return (TRUE);
3080Sstevel@tonic-gate 	}
309132Srobinson 	return (FALSE);
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate  * XDR an unsigned char
3140Sstevel@tonic-gate  */
3150Sstevel@tonic-gate bool_t
xdr_u_char(XDR * xdrs,uchar_t * cp)3160Sstevel@tonic-gate xdr_u_char(XDR *xdrs, uchar_t *cp)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate 	int i;
3190Sstevel@tonic-gate 
320132Srobinson 	switch (xdrs->x_op) {
321132Srobinson 	case XDR_ENCODE:
3220Sstevel@tonic-gate 		i = (*cp);
323132Srobinson 		return (XDR_PUTINT32(xdrs, &i));
324132Srobinson 	case XDR_DECODE:
325132Srobinson 		if (!XDR_GETINT32(xdrs, &i))
326132Srobinson 			return (FALSE);
327132Srobinson 		*cp = (uchar_t)i;
328132Srobinson 		return (TRUE);
329132Srobinson 	case XDR_FREE:
330132Srobinson 		return (TRUE);
3310Sstevel@tonic-gate 	}
332132Srobinson 	return (FALSE);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate /*
3360Sstevel@tonic-gate  * XDR booleans
3370Sstevel@tonic-gate  */
3380Sstevel@tonic-gate bool_t
xdr_bool(XDR * xdrs,bool_t * bp)3390Sstevel@tonic-gate xdr_bool(XDR *xdrs, bool_t *bp)
3400Sstevel@tonic-gate {
3410Sstevel@tonic-gate 	int i;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	switch (xdrs->x_op) {
3440Sstevel@tonic-gate 	case XDR_ENCODE:
3450Sstevel@tonic-gate 		i = *bp ? XDR_TRUE : XDR_FALSE;
346132Srobinson 		return (XDR_PUTINT32(xdrs, &i));
3470Sstevel@tonic-gate 	case XDR_DECODE:
348132Srobinson 		if (!XDR_GETINT32(xdrs, &i))
3490Sstevel@tonic-gate 			return (FALSE);
3500Sstevel@tonic-gate 		*bp = (i == XDR_FALSE) ? FALSE : TRUE;
3510Sstevel@tonic-gate 		return (TRUE);
3520Sstevel@tonic-gate 	case XDR_FREE:
3530Sstevel@tonic-gate 		return (TRUE);
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 	return (FALSE);
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate  * XDR enumerations
3600Sstevel@tonic-gate  */
3610Sstevel@tonic-gate bool_t
xdr_enum(XDR * xdrs,enum_t * ep)3620Sstevel@tonic-gate xdr_enum(XDR *xdrs, enum_t *ep)
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate 	enum sizecheck { SIZEVAL };	/* used to find the size of an enum */
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	/*
3670Sstevel@tonic-gate 	 * enums are treated as ints
3680Sstevel@tonic-gate 	 */
369132Srobinson 	/* CONSTCOND */
370132Srobinson 	assert(sizeof (enum sizecheck) == sizeof (int32_t));
371132Srobinson 	return (xdr_int(xdrs, (int *)ep));
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate /*
3750Sstevel@tonic-gate  * XDR opaque data
3760Sstevel@tonic-gate  * Allows the specification of a fixed size sequence of opaque bytes.
3770Sstevel@tonic-gate  * cp points to the opaque object and cnt gives the byte length.
3780Sstevel@tonic-gate  */
3790Sstevel@tonic-gate bool_t
xdr_opaque(XDR * xdrs,caddr_t cp,const uint_t cnt)380132Srobinson xdr_opaque(XDR *xdrs, caddr_t cp, const uint_t cnt)
3810Sstevel@tonic-gate {
382132Srobinson 	uint_t rndup;
3830Sstevel@tonic-gate 	char crud[BYTES_PER_XDR_UNIT];
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	/*
3860Sstevel@tonic-gate 	 * if no data we are done
3870Sstevel@tonic-gate 	 */
388132Srobinson 	if (cnt == 0)
3890Sstevel@tonic-gate 		return (TRUE);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	/*
3920Sstevel@tonic-gate 	 * round byte count to full xdr units
3930Sstevel@tonic-gate 	 */
3940Sstevel@tonic-gate 	rndup = cnt % BYTES_PER_XDR_UNIT;
3950Sstevel@tonic-gate 	if ((int)rndup > 0)
3960Sstevel@tonic-gate 		rndup = BYTES_PER_XDR_UNIT - rndup;
3970Sstevel@tonic-gate 
398132Srobinson 	switch (xdrs->x_op) {
399132Srobinson 	case XDR_DECODE:
400132Srobinson 		if (!XDR_GETBYTES(xdrs, cp, cnt))
4010Sstevel@tonic-gate 			return (FALSE);
402132Srobinson 		if (rndup == 0)
4030Sstevel@tonic-gate 			return (TRUE);
404132Srobinson 		return (XDR_GETBYTES(xdrs, crud, rndup));
405132Srobinson 	case XDR_ENCODE:
406132Srobinson 		if (!XDR_PUTBYTES(xdrs, cp, cnt))
4070Sstevel@tonic-gate 			return (FALSE);
408132Srobinson 		if (rndup == 0)
4090Sstevel@tonic-gate 			return (TRUE);
410132Srobinson 		return (XDR_PUTBYTES(xdrs, (caddr_t)&xdr_zero[0], rndup));
411132Srobinson 	case XDR_FREE:
4120Sstevel@tonic-gate 		return (TRUE);
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 	return (FALSE);
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate /*
4180Sstevel@tonic-gate  * XDR counted bytes
4190Sstevel@tonic-gate  * *cpp is a pointer to the bytes, *sizep is the count.
4200Sstevel@tonic-gate  * If *cpp is NULL maxsize bytes are allocated
4210Sstevel@tonic-gate  */
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate static const char xdr_err[] = "xdr_%s: out of memory";
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate bool_t
xdr_bytes(XDR * xdrs,char ** cpp,uint_t * sizep,const uint_t maxsize)426132Srobinson xdr_bytes(XDR *xdrs, char **cpp, uint_t *sizep, const uint_t maxsize)
4270Sstevel@tonic-gate {
428132Srobinson 	char *sp = *cpp;  /* sp is the actual string pointer */
429132Srobinson 	uint_t nodesize;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/*
4320Sstevel@tonic-gate 	 * first deal with the length since xdr bytes are counted
4330Sstevel@tonic-gate 	 * We decided not to use MACRO XDR_U_INT here, because the
4340Sstevel@tonic-gate 	 * advantages here will be miniscule compared to xdr_bytes.
4350Sstevel@tonic-gate 	 * This saved us 100 bytes in the library size.
4360Sstevel@tonic-gate 	 */
437132Srobinson 	if (!xdr_u_int(xdrs, sizep))
4380Sstevel@tonic-gate 		return (FALSE);
4390Sstevel@tonic-gate 	nodesize = *sizep;
440132Srobinson 	if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
4410Sstevel@tonic-gate 		return (FALSE);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	/*
4440Sstevel@tonic-gate 	 * now deal with the actual bytes
4450Sstevel@tonic-gate 	 */
4460Sstevel@tonic-gate 	switch (xdrs->x_op) {
4470Sstevel@tonic-gate 	case XDR_DECODE:
448132Srobinson 		if (nodesize == 0)
4490Sstevel@tonic-gate 			return (TRUE);
450132Srobinson 		if (sp == NULL)
451132Srobinson 			*cpp = sp = malloc(nodesize);
4520Sstevel@tonic-gate 		if (sp == NULL) {
4530Sstevel@tonic-gate 			(void) syslog(LOG_ERR, xdr_err, (const char *)"bytes");
4540Sstevel@tonic-gate 			return (FALSE);
4550Sstevel@tonic-gate 		}
4560Sstevel@tonic-gate 		/*FALLTHROUGH*/
4570Sstevel@tonic-gate 	case XDR_ENCODE:
458132Srobinson 		return (xdr_opaque(xdrs, sp, nodesize));
4590Sstevel@tonic-gate 	case XDR_FREE:
4600Sstevel@tonic-gate 		if (sp != NULL) {
461132Srobinson 			free(sp);
4620Sstevel@tonic-gate 			*cpp = NULL;
4630Sstevel@tonic-gate 		}
4640Sstevel@tonic-gate 		return (TRUE);
4650Sstevel@tonic-gate 	}
4660Sstevel@tonic-gate 	return (FALSE);
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate /*
4700Sstevel@tonic-gate  * Implemented here due to commonality of the object.
4710Sstevel@tonic-gate  */
4720Sstevel@tonic-gate bool_t
xdr_netobj(XDR * xdrs,struct netobj * np)4730Sstevel@tonic-gate xdr_netobj(XDR *xdrs, struct netobj *np)
4740Sstevel@tonic-gate {
475132Srobinson 	return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate /*
4790Sstevel@tonic-gate  * XDR a descriminated union
4800Sstevel@tonic-gate  * Support routine for discriminated unions.
4810Sstevel@tonic-gate  * You create an array of xdrdiscrim structures, terminated with
4820Sstevel@tonic-gate  * an entry with a null procedure pointer.  The routine gets
4830Sstevel@tonic-gate  * the discriminant value and then searches the array of xdrdiscrims
4840Sstevel@tonic-gate  * looking for that value.  It calls the procedure given in the xdrdiscrim
4850Sstevel@tonic-gate  * to handle the discriminant.  If there is no specific routine a default
4860Sstevel@tonic-gate  * routine may be called.
4870Sstevel@tonic-gate  * If there is no specific or default routine an error is returned.
4880Sstevel@tonic-gate  */
4890Sstevel@tonic-gate bool_t
xdr_union(XDR * xdrs,enum_t * dscmp,char * unp,const struct xdr_discrim * choices,const xdrproc_t dfault)4900Sstevel@tonic-gate xdr_union(XDR *xdrs, enum_t *dscmp, char *unp,
491132Srobinson 		const struct xdr_discrim *choices, const xdrproc_t dfault)
4920Sstevel@tonic-gate {
493132Srobinson 	enum_t dscm;
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	/*
4960Sstevel@tonic-gate 	 * we deal with the discriminator;  it's an enum
4970Sstevel@tonic-gate 	 */
498132Srobinson 	if (!xdr_enum(xdrs, dscmp))
4990Sstevel@tonic-gate 		return (FALSE);
5000Sstevel@tonic-gate 	dscm = *dscmp;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	/*
5030Sstevel@tonic-gate 	 * search choices for a value that matches the discriminator.
5040Sstevel@tonic-gate 	 * if we find one, execute the xdr routine for that value.
5050Sstevel@tonic-gate 	 */
5060Sstevel@tonic-gate 	for (; choices->proc != NULL_xdrproc_t; choices++) {
507132Srobinson 		if (choices->value == dscm)
508132Srobinson 			return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	/*
5120Sstevel@tonic-gate 	 * no match - execute the default xdr routine if there is one
5130Sstevel@tonic-gate 	 */
514132Srobinson 	return ((dfault == NULL_xdrproc_t) ? FALSE :
515132Srobinson 	    (*dfault)(xdrs, unp, LASTUNSIGNED));
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate /*
5200Sstevel@tonic-gate  * Non-portable xdr primitives.
5210Sstevel@tonic-gate  * Care should be taken when moving these routines to new architectures.
5220Sstevel@tonic-gate  */
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate /*
5260Sstevel@tonic-gate  * XDR null terminated ASCII strings
5270Sstevel@tonic-gate  * xdr_string deals with "C strings" - arrays of bytes that are
5280Sstevel@tonic-gate  * terminated by a NULL character.  The parameter cpp references a
5290Sstevel@tonic-gate  * pointer to storage; If the pointer is null, then the necessary
5300Sstevel@tonic-gate  * storage is allocated.  The last parameter is the max allowed length
5310Sstevel@tonic-gate  * of the string as specified by a protocol.
5320Sstevel@tonic-gate  */
5330Sstevel@tonic-gate bool_t
xdr_string(XDR * xdrs,char ** cpp,const uint_t maxsize)534132Srobinson xdr_string(XDR *xdrs, char **cpp, const uint_t maxsize)
5350Sstevel@tonic-gate {
536132Srobinson 	char *newsp, *sp = *cpp;  /* sp is the actual string pointer */
5370Sstevel@tonic-gate 	uint_t size, block;
5380Sstevel@tonic-gate 	uint64_t bytesread;
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	/*
5410Sstevel@tonic-gate 	 * first deal with the length since xdr strings are counted-strings
5420Sstevel@tonic-gate 	 */
5430Sstevel@tonic-gate 	switch (xdrs->x_op) {
5440Sstevel@tonic-gate 	case XDR_FREE:
545132Srobinson 		if (sp == NULL)
5460Sstevel@tonic-gate 			return (TRUE);	/* already free */
5470Sstevel@tonic-gate 		/*FALLTHROUGH*/
5480Sstevel@tonic-gate 	case XDR_ENCODE:
5490Sstevel@tonic-gate 		size = (sp != NULL) ? (uint_t)strlen(sp) : 0;
5500Sstevel@tonic-gate 		break;
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 	/*
5530Sstevel@tonic-gate 	 * We decided not to use MACRO XDR_U_INT here, because the
5540Sstevel@tonic-gate 	 * advantages here will be miniscule compared to xdr_string.
5550Sstevel@tonic-gate 	 * This saved us 100 bytes in the library size.
5560Sstevel@tonic-gate 	 */
557132Srobinson 	if (!xdr_u_int(xdrs, &size))
5580Sstevel@tonic-gate 		return (FALSE);
559132Srobinson 	if (size > maxsize)
5600Sstevel@tonic-gate 		return (FALSE);
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	/*
5630Sstevel@tonic-gate 	 * now deal with the actual bytes
5640Sstevel@tonic-gate 	 */
5650Sstevel@tonic-gate 	switch (xdrs->x_op) {
5660Sstevel@tonic-gate 	case XDR_DECODE:
5670Sstevel@tonic-gate 		/* if buffer is already given, call xdr_opaque() directly */
5680Sstevel@tonic-gate 		if (sp != NULL) {
569132Srobinson 			if (!xdr_opaque(xdrs, sp, size))
570132Srobinson 				return (FALSE);
5710Sstevel@tonic-gate 			sp[size] = 0;
572132Srobinson 			return (TRUE);
5730Sstevel@tonic-gate 		}
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 		/*
5760Sstevel@tonic-gate 		 * We have to allocate a buffer of size 'size'. To avoid
5770Sstevel@tonic-gate 		 * malloc()ing one huge chunk, we'll read the bytes in max
5780Sstevel@tonic-gate 		 * FRAGMENT size blocks and keep realloc()ing. 'block' is
5790Sstevel@tonic-gate 		 * the number of bytes to read in each xdr_opaque() and
5800Sstevel@tonic-gate 		 * 'bytesread' is what we have already read. sp is NULL
5810Sstevel@tonic-gate 		 * when we are in the loop for the first time.
5820Sstevel@tonic-gate 		 */
5830Sstevel@tonic-gate 		bytesread = 0;
5840Sstevel@tonic-gate 		do {
5850Sstevel@tonic-gate 			block = MIN(size - bytesread, FRAGMENT);
5860Sstevel@tonic-gate 			/*
5870Sstevel@tonic-gate 			 * allocate enough for 'bytesread + block' bytes and
5880Sstevel@tonic-gate 			 * one extra for the terminating NULL.
5890Sstevel@tonic-gate 			 */
5900Sstevel@tonic-gate 			newsp = realloc(sp, bytesread + block + 1);
5910Sstevel@tonic-gate 			if (newsp == NULL) {
5920Sstevel@tonic-gate 				if (sp != NULL)
5930Sstevel@tonic-gate 					free(sp);
5940Sstevel@tonic-gate 				return (FALSE);
5950Sstevel@tonic-gate 			}
5960Sstevel@tonic-gate 			sp = newsp;
5970Sstevel@tonic-gate 			if (!xdr_opaque(xdrs, &sp[bytesread], block)) {
5980Sstevel@tonic-gate 				free(sp);
5990Sstevel@tonic-gate 				return (FALSE);
6000Sstevel@tonic-gate 			}
6010Sstevel@tonic-gate 			bytesread += block;
6020Sstevel@tonic-gate 		} while (bytesread < size);
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 		sp[bytesread] = 0; /* terminate the string with a NULL */
6050Sstevel@tonic-gate 		*cpp = sp;
6060Sstevel@tonic-gate 		return (TRUE);
6070Sstevel@tonic-gate 	case XDR_ENCODE:
608132Srobinson 		return (xdr_opaque(xdrs, sp, size));
6090Sstevel@tonic-gate 	case XDR_FREE:
6100Sstevel@tonic-gate 		free(sp);
6110Sstevel@tonic-gate 		*cpp = NULL;
6120Sstevel@tonic-gate 		return (TRUE);
6130Sstevel@tonic-gate 	}
6140Sstevel@tonic-gate 	return (FALSE);
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate bool_t
xdr_hyper(XDR * xdrs,longlong_t * hp)6180Sstevel@tonic-gate xdr_hyper(XDR *xdrs, longlong_t *hp)
6190Sstevel@tonic-gate {
6200Sstevel@tonic-gate 	if (xdrs->x_op == XDR_ENCODE) {
6210Sstevel@tonic-gate #if defined(_LONG_LONG_HTOL)
622132Srobinson 		if (XDR_PUTINT32(xdrs, (int *)hp) == TRUE)
623132Srobinson 			/* LINTED pointer cast */
624132Srobinson 			return (XDR_PUTINT32(xdrs, (int *)((char *)hp +
625132Srobinson 				BYTES_PER_XDR_UNIT)));
6260Sstevel@tonic-gate #else
627132Srobinson 		/* LINTED pointer cast */
6280Sstevel@tonic-gate 		if (XDR_PUTINT32(xdrs, (int *)((char *)hp +
629132Srobinson 				BYTES_PER_XDR_UNIT)) == TRUE)
630132Srobinson 			return (XDR_PUTINT32(xdrs, (int32_t *)hp));
631132Srobinson #endif
632132Srobinson 		return (FALSE);
633132Srobinson 	}
6340Sstevel@tonic-gate 
635132Srobinson 	if (xdrs->x_op == XDR_DECODE) {
6360Sstevel@tonic-gate #if defined(_LONG_LONG_HTOL)
6370Sstevel@tonic-gate 		if (XDR_GETINT32(xdrs, (int *)hp) == FALSE ||
638132Srobinson 		    /* LINTED pointer cast */
6390Sstevel@tonic-gate 		    (XDR_GETINT32(xdrs, (int *)((char *)hp +
640132Srobinson 				BYTES_PER_XDR_UNIT)) == FALSE))
6410Sstevel@tonic-gate 			return (FALSE);
6420Sstevel@tonic-gate #else
643132Srobinson 		/* LINTED pointer cast */
6440Sstevel@tonic-gate 		if ((XDR_GETINT32(xdrs, (int *)((char *)hp +
6450Sstevel@tonic-gate 				BYTES_PER_XDR_UNIT)) == FALSE) ||
646132Srobinson 				(XDR_GETINT32(xdrs, (int *)hp) == FALSE))
6470Sstevel@tonic-gate 			return (FALSE);
6480Sstevel@tonic-gate #endif
6490Sstevel@tonic-gate 		return (TRUE);
6500Sstevel@tonic-gate 	}
6510Sstevel@tonic-gate 	return (TRUE);
6520Sstevel@tonic-gate }
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate bool_t
xdr_u_hyper(XDR * xdrs,u_longlong_t * hp)6550Sstevel@tonic-gate xdr_u_hyper(XDR *xdrs, u_longlong_t *hp)
6560Sstevel@tonic-gate {
657132Srobinson 	return (xdr_hyper(xdrs, (longlong_t *)hp));
6580Sstevel@tonic-gate }
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate bool_t
xdr_longlong_t(XDR * xdrs,longlong_t * hp)6610Sstevel@tonic-gate xdr_longlong_t(XDR *xdrs, longlong_t *hp)
6620Sstevel@tonic-gate {
663132Srobinson 	return (xdr_hyper(xdrs, hp));
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate bool_t
xdr_u_longlong_t(XDR * xdrs,u_longlong_t * hp)6670Sstevel@tonic-gate xdr_u_longlong_t(XDR *xdrs, u_longlong_t *hp)
6680Sstevel@tonic-gate {
669132Srobinson 	return (xdr_hyper(xdrs, (longlong_t *)hp));
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate /*
6730Sstevel@tonic-gate  * Wrapper for xdr_string that can be called directly from
6740Sstevel@tonic-gate  * routines like clnt_call
6750Sstevel@tonic-gate  */
6760Sstevel@tonic-gate bool_t
xdr_wrapstring(XDR * xdrs,char ** cpp)6770Sstevel@tonic-gate xdr_wrapstring(XDR *xdrs, char **cpp)
6780Sstevel@tonic-gate {
679132Srobinson 	return (xdr_string(xdrs, cpp, LASTUNSIGNED));
6800Sstevel@tonic-gate }
681