xref: /onnv-gate/usr/src/lib/libnsl/rpc/xdr_array.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 impelmentation.
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
420Sstevel@tonic-gate  * arrays.  See xdr.h for more info on the interface to xdr.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate 
45*1219Sraf #include "mt.h"
460Sstevel@tonic-gate #include <sys/types.h>
470Sstevel@tonic-gate #include <syslog.h>
480Sstevel@tonic-gate #include <stdio.h>
490Sstevel@tonic-gate #include <stdlib.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #include <rpc/types.h>
520Sstevel@tonic-gate #include <rpc/xdr.h>
530Sstevel@tonic-gate #include <memory.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #define	LASTUNSIGNED	((uint_t)0-1)
560Sstevel@tonic-gate 
570Sstevel@tonic-gate char mem_err_msg_arr[] = "xdr_array: out of memory";
58132Srobinson 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * XDR an array of arbitrary elements
610Sstevel@tonic-gate  * *addrp is a pointer to the array, *sizep is the number of elements.
620Sstevel@tonic-gate  * If *addrp is NULL (*sizep * elsize) bytes are allocated.
630Sstevel@tonic-gate  * elsize is the size (in bytes) of each element, and elproc is the
640Sstevel@tonic-gate  * xdr procedure to call to handle each element of the array.
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate bool_t
xdr_array(XDR * xdrs,caddr_t * addrp,uint_t * sizep,const uint_t maxsize,const uint_t elsize,const xdrproc_t elproc)67132Srobinson xdr_array(XDR *xdrs, caddr_t *addrp, uint_t *sizep, const uint_t maxsize,
68132Srobinson 	const uint_t elsize, const xdrproc_t elproc)
690Sstevel@tonic-gate {
70132Srobinson 	uint_t i;
71132Srobinson 	caddr_t target = *addrp;
72132Srobinson 	uint_t c;  /* the actual element count */
73132Srobinson 	bool_t stat = TRUE;
74132Srobinson 	uint_t nodesize;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	/* like strings, arrays are really counted arrays */
77132Srobinson 	if (!xdr_u_int(xdrs, sizep))
780Sstevel@tonic-gate 		return (FALSE);
790Sstevel@tonic-gate 	c = *sizep;
800Sstevel@tonic-gate 	if ((c > maxsize || LASTUNSIGNED / elsize < c) &&
81132Srobinson 	    xdrs->x_op != XDR_FREE)
820Sstevel@tonic-gate 		return (FALSE);
830Sstevel@tonic-gate 	nodesize = c * elsize;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	/*
860Sstevel@tonic-gate 	 * if we are deserializing, we may need to allocate an array.
870Sstevel@tonic-gate 	 * We also save time by checking for a null array if we are freeing.
880Sstevel@tonic-gate 	 */
890Sstevel@tonic-gate 	if (target == NULL)
900Sstevel@tonic-gate 		switch (xdrs->x_op) {
910Sstevel@tonic-gate 		case XDR_DECODE:
92132Srobinson 			if (c == 0)
930Sstevel@tonic-gate 				return (TRUE);
94132Srobinson 			*addrp = target = malloc(nodesize);
950Sstevel@tonic-gate 			if (target == NULL) {
960Sstevel@tonic-gate 				(void) syslog(LOG_ERR, mem_err_msg_arr);
970Sstevel@tonic-gate 				return (FALSE);
980Sstevel@tonic-gate 			}
990Sstevel@tonic-gate 			(void) memset(target, 0, nodesize);
1000Sstevel@tonic-gate 			break;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 		case XDR_FREE:
1030Sstevel@tonic-gate 			return (TRUE);
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	/*
1070Sstevel@tonic-gate 	 * now we xdr each element of array
1080Sstevel@tonic-gate 	 */
1090Sstevel@tonic-gate 	for (i = 0; (i < c) && stat; i++) {
1100Sstevel@tonic-gate 		stat = (*elproc)(xdrs, target);
1110Sstevel@tonic-gate 		target += elsize;
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	/*
1150Sstevel@tonic-gate 	 * the array may need freeing
1160Sstevel@tonic-gate 	 */
1170Sstevel@tonic-gate 	if (xdrs->x_op == XDR_FREE) {
118132Srobinson 		free(*addrp);
1190Sstevel@tonic-gate 		*addrp = NULL;
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 	return (stat);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate  * xdr_vector():
1260Sstevel@tonic-gate  *
1270Sstevel@tonic-gate  * XDR a fixed length array. Unlike variable-length arrays,
1280Sstevel@tonic-gate  * the storage of fixed length arrays is static and unfreeable.
1290Sstevel@tonic-gate  * > basep: base of the array
1300Sstevel@tonic-gate  * > size: size of the array
1310Sstevel@tonic-gate  * > elemsize: size of each element
1320Sstevel@tonic-gate  * > xdr_elem: routine to XDR each element
1330Sstevel@tonic-gate  */
1340Sstevel@tonic-gate bool_t
xdr_vector(XDR * xdrs,char * basep,const uint_t nelem,const uint_t elemsize,const xdrproc_t xdr_elem)135132Srobinson xdr_vector(XDR *xdrs, char *basep, const uint_t nelem,
136132Srobinson 	const uint_t elemsize, const xdrproc_t xdr_elem)
1370Sstevel@tonic-gate {
138132Srobinson 	uint_t i;
139132Srobinson 	char *elptr;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	elptr = basep;
1420Sstevel@tonic-gate 	for (i = 0; i < nelem; i++) {
143132Srobinson 		if (!(*xdr_elem)(xdrs, elptr, LASTUNSIGNED))
1440Sstevel@tonic-gate 			return (FALSE);
1450Sstevel@tonic-gate 		elptr += elemsize;
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 	return (TRUE);
1480Sstevel@tonic-gate }
149