xref: /onnv-gate/usr/src/uts/common/rpc/xdr_array.c (revision 7387:0b3a92e31fd8)
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
5*7387SRobert.Gordon@Sun.COM  * Common Development and Distribution License (the "License").
6*7387SRobert.Gordon@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7387SRobert.Gordon@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
310Sstevel@tonic-gate  * under license from the Regents of the University of California.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate  * xdr_array.c, Generic XDR routines impelmentation.
360Sstevel@tonic-gate  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
370Sstevel@tonic-gate  * arrays.  See xdr.h for more info on the interface to xdr.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <sys/param.h>
410Sstevel@tonic-gate #include <sys/cmn_err.h>
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include <sys/systm.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <rpc/types.h>
460Sstevel@tonic-gate #include <rpc/xdr.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #define	LASTUNSIGNED	((uint_t)0-1)
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * XDR an array of arbitrary elements
520Sstevel@tonic-gate  * *addrp is a pointer to the array, *sizep is the number of elements.
530Sstevel@tonic-gate  * If addrp is NULL (*sizep * elsize) bytes are allocated.
540Sstevel@tonic-gate  * elsize is the size (in bytes) of each element, and elproc is the
550Sstevel@tonic-gate  * xdr procedure to call to handle each element of the array.
560Sstevel@tonic-gate  */
570Sstevel@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)580Sstevel@tonic-gate xdr_array(XDR *xdrs, caddr_t *addrp, uint_t *sizep, const uint_t maxsize,
590Sstevel@tonic-gate 	const uint_t elsize, const xdrproc_t elproc)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	uint_t i;
620Sstevel@tonic-gate 	caddr_t target = *addrp;
630Sstevel@tonic-gate 	uint_t c;  /* the actual element count */
640Sstevel@tonic-gate 	bool_t stat = TRUE;
650Sstevel@tonic-gate 	uint_t nodesize;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	/* like strings, arrays are really counted arrays */
680Sstevel@tonic-gate 	if (!xdr_u_int(xdrs, sizep)) {
690Sstevel@tonic-gate 		return (FALSE);
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate 	c = *sizep;
720Sstevel@tonic-gate 	if ((c > maxsize || LASTUNSIGNED / elsize < c) &&
730Sstevel@tonic-gate 	    xdrs->x_op != XDR_FREE) {
740Sstevel@tonic-gate 		return (FALSE);
750Sstevel@tonic-gate 	}
760Sstevel@tonic-gate 	nodesize = c * elsize;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	/*
790Sstevel@tonic-gate 	 * if we are deserializing, we may need to allocate an array.
800Sstevel@tonic-gate 	 * We also save time by checking for a null array if we are freeing.
810Sstevel@tonic-gate 	 */
820Sstevel@tonic-gate 	if (target == NULL)
830Sstevel@tonic-gate 		switch (xdrs->x_op) {
840Sstevel@tonic-gate 		case XDR_DECODE:
850Sstevel@tonic-gate 			if (c == 0)
860Sstevel@tonic-gate 				return (TRUE);
870Sstevel@tonic-gate 			*addrp = target = (char *)mem_alloc(nodesize);
880Sstevel@tonic-gate 			bzero(target, nodesize);
890Sstevel@tonic-gate 			break;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 		case XDR_FREE:
920Sstevel@tonic-gate 			return (TRUE);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 		case XDR_ENCODE:
950Sstevel@tonic-gate 			break;
960Sstevel@tonic-gate 		}
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	/*
990Sstevel@tonic-gate 	 * now we xdr each element of array
1000Sstevel@tonic-gate 	 */
1010Sstevel@tonic-gate 	for (i = 0; (i < c) && stat; i++) {
1020Sstevel@tonic-gate 		stat = (*elproc)(xdrs, target, LASTUNSIGNED);
1030Sstevel@tonic-gate 		target += elsize;
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	/*
1070Sstevel@tonic-gate 	 * the array may need freeing
1080Sstevel@tonic-gate 	 */
1090Sstevel@tonic-gate 	if (xdrs->x_op == XDR_FREE) {
1100Sstevel@tonic-gate 		mem_free(*addrp, nodesize);
1110Sstevel@tonic-gate 		*addrp = NULL;
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 	return (stat);
1140Sstevel@tonic-gate }
115