174462Salfred/*	$NetBSD: xdr_array.c,v 1.12 2000/01/22 22:19:18 mycroft Exp $	*/
274462Salfred
3273188Shrs/*-
4273188Shrs * Copyright (c) 2010, Oracle America, Inc.
58870Srgrimes *
6273188Shrs * Redistribution and use in source and binary forms, with or without
7273188Shrs * modification, are permitted provided that the following conditions are
8273188Shrs * met:
98870Srgrimes *
10273188Shrs *     * Redistributions of source code must retain the above copyright
11273188Shrs *       notice, this list of conditions and the following disclaimer.
12273188Shrs *     * Redistributions in binary form must reproduce the above
13273188Shrs *       copyright notice, this list of conditions and the following
14273188Shrs *       disclaimer in the documentation and/or other materials
15273188Shrs *       provided with the distribution.
16273188Shrs *     * Neither the name of the "Oracle America, Inc." nor the names of its
17273188Shrs *       contributors may be used to endorse or promote products derived
18273188Shrs *       from this software without specific prior written permission.
198870Srgrimes *
20273188Shrs *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21273188Shrs *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22273188Shrs *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23273188Shrs *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24273188Shrs *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25273188Shrs *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26273188Shrs *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27273188Shrs *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28273188Shrs *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29273188Shrs *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30273188Shrs *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31273188Shrs *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
321902Swollman */
331902Swollman
341902Swollman#if defined(LIBC_SCCS) && !defined(lint)
35136582Sobrienstatic char *sccsid2 = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";
3692986Sobrienstatic char *sccsid = "@(#)xdr_array.c	2.1 88/07/29 4.0 RPCSRC";
371902Swollman#endif
3892986Sobrien#include <sys/cdefs.h>
3992986Sobrien__FBSDID("$FreeBSD$");
401902Swollman
411902Swollman/*
421902Swollman * xdr_array.c, Generic XDR routines impelmentation.
431902Swollman *
441902Swollman * These are the "non-trivial" xdr primitives used to serialize and de-serialize
451902Swollman * arrays.  See xdr.h for more info on the interface to xdr.
461902Swollman */
471902Swollman
4874462Salfred#include "namespace.h"
4974462Salfred#include <err.h>
50101067Snectar#include <limits.h>
511902Swollman#include <stdio.h>
521902Swollman#include <stdlib.h>
5311669Sphk#include <string.h>
5474462Salfred
551902Swollman#include <rpc/types.h>
561902Swollman#include <rpc/xdr.h>
5774462Salfred#include "un-namespace.h"
581902Swollman
591902Swollman/*
601902Swollman * XDR an array of arbitrary elements
611902Swollman * *addrp is a pointer to the array, *sizep is the number of elements.
621902Swollman * If addrp is NULL (*sizep * elsize) bytes are allocated.
631902Swollman * elsize is the size (in bytes) of each element, and elproc is the
641902Swollman * xdr procedure to call to handle each element of the array.
651902Swollman */
661902Swollmanbool_t
671902Swollmanxdr_array(xdrs, addrp, sizep, maxsize, elsize, elproc)
6874462Salfred	XDR *xdrs;
691902Swollman	caddr_t *addrp;		/* array pointer */
701902Swollman	u_int *sizep;		/* number of elements */
711902Swollman	u_int maxsize;		/* max numberof elements */
721902Swollman	u_int elsize;		/* size in bytes of each element */
731902Swollman	xdrproc_t elproc;	/* xdr routine to handle each element */
741902Swollman{
7574462Salfred	u_int i;
7674462Salfred	caddr_t target = *addrp;
7774462Salfred	u_int c;  /* the actual element count */
7874462Salfred	bool_t stat = TRUE;
7974462Salfred	u_int nodesize;
801902Swollman
811902Swollman	/* like strings, arrays are really counted arrays */
82101045Sdarrenr	if (!xdr_u_int(xdrs, sizep)) {
831902Swollman		return (FALSE);
841902Swollman	}
851902Swollman	c = *sizep;
86101147Snectar	if ((c > maxsize || UINT_MAX/elsize < c) &&
87101045Sdarrenr	    (xdrs->x_op != XDR_FREE)) {
881902Swollman		return (FALSE);
891902Swollman	}
901902Swollman	nodesize = c * elsize;
911902Swollman
921902Swollman	/*
931902Swollman	 * if we are deserializing, we may need to allocate an array.
941902Swollman	 * We also save time by checking for a null array if we are freeing.
951902Swollman	 */
961902Swollman	if (target == NULL)
971902Swollman		switch (xdrs->x_op) {
981902Swollman		case XDR_DECODE:
991902Swollman			if (c == 0)
1001902Swollman				return (TRUE);
1011902Swollman			*addrp = target = mem_alloc(nodesize);
1021902Swollman			if (target == NULL) {
10374462Salfred				warnx("xdr_array: out of memory");
1041902Swollman				return (FALSE);
1051902Swollman			}
10621062Speter			memset(target, 0, nodesize);
1071902Swollman			break;
1081902Swollman
1091902Swollman		case XDR_FREE:
1101902Swollman			return (TRUE);
11174462Salfred
11274462Salfred		case XDR_ENCODE:
11374462Salfred			break;
1141902Swollman	}
11574462Salfred
1161902Swollman	/*
1171902Swollman	 * now we xdr each element of array
1181902Swollman	 */
1191902Swollman	for (i = 0; (i < c) && stat; i++) {
12074462Salfred		stat = (*elproc)(xdrs, target);
1211902Swollman		target += elsize;
1221902Swollman	}
1231902Swollman
1241902Swollman	/*
1251902Swollman	 * the array may need freeing
1261902Swollman	 */
1271902Swollman	if (xdrs->x_op == XDR_FREE) {
1281902Swollman		mem_free(*addrp, nodesize);
1291902Swollman		*addrp = NULL;
1301902Swollman	}
1311902Swollman	return (stat);
1321902Swollman}
1331902Swollman
1341902Swollman/*
1351902Swollman * xdr_vector():
1361902Swollman *
1371902Swollman * XDR a fixed length array. Unlike variable-length arrays,
1381902Swollman * the storage of fixed length arrays is static and unfreeable.
1391902Swollman * > basep: base of the array
1401902Swollman * > size: size of the array
1411902Swollman * > elemsize: size of each element
1421902Swollman * > xdr_elem: routine to XDR each element
1431902Swollman */
1441902Swollmanbool_t
1451902Swollmanxdr_vector(xdrs, basep, nelem, elemsize, xdr_elem)
14674462Salfred	XDR *xdrs;
14774462Salfred	char *basep;
14874462Salfred	u_int nelem;
14974462Salfred	u_int elemsize;
15074462Salfred	xdrproc_t xdr_elem;
1511902Swollman{
15274462Salfred	u_int i;
15374462Salfred	char *elptr;
1541902Swollman
1551902Swollman	elptr = basep;
1561902Swollman	for (i = 0; i < nelem; i++) {
157101045Sdarrenr		if (!(*xdr_elem)(xdrs, elptr)) {
1581902Swollman			return(FALSE);
1591902Swollman		}
1601902Swollman		elptr += elemsize;
1611902Swollman	}
16274462Salfred	return(TRUE);
1631902Swollman}
164