xref: /minix3/lib/libc/rpc/xdr_array.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: xdr_array.c,v 1.19 2013/03/11 20:19:29 tron Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
4*84d9c625SLionel Sambuc  * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras  *
6*84d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
7*84d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions are
8*84d9c625SLionel Sambuc  * met:
92fe8fb19SBen Gras  *
10*84d9c625SLionel Sambuc  *     * Redistributions of source code must retain the above copyright
11*84d9c625SLionel Sambuc  *       notice, this list of conditions and the following disclaimer.
12*84d9c625SLionel Sambuc  *     * Redistributions in binary form must reproduce the above
13*84d9c625SLionel Sambuc  *       copyright notice, this list of conditions and the following
14*84d9c625SLionel Sambuc  *       disclaimer in the documentation and/or other materials
15*84d9c625SLionel Sambuc  *       provided with the distribution.
16*84d9c625SLionel Sambuc  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17*84d9c625SLionel Sambuc  *       contributors may be used to endorse or promote products derived
18*84d9c625SLionel Sambuc  *       from this software without specific prior written permission.
192fe8fb19SBen Gras  *
20*84d9c625SLionel Sambuc  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*84d9c625SLionel Sambuc  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*84d9c625SLionel Sambuc  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*84d9c625SLionel Sambuc  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*84d9c625SLionel Sambuc  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*84d9c625SLionel Sambuc  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*84d9c625SLionel Sambuc  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*84d9c625SLionel Sambuc  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*84d9c625SLionel Sambuc  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*84d9c625SLionel Sambuc  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*84d9c625SLionel Sambuc  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*84d9c625SLionel Sambuc  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras  */
332fe8fb19SBen Gras 
342fe8fb19SBen Gras #include <sys/cdefs.h>
352fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
362fe8fb19SBen Gras #if 0
372fe8fb19SBen Gras static char *sccsid = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";
382fe8fb19SBen Gras static char *sccsid = "@(#)xdr_array.c	2.1 88/07/29 4.0 RPCSRC";
392fe8fb19SBen Gras #else
40*84d9c625SLionel Sambuc __RCSID("$NetBSD: xdr_array.c,v 1.19 2013/03/11 20:19:29 tron Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras 
442fe8fb19SBen Gras /*
452fe8fb19SBen Gras  * xdr_array.c, Generic XDR routines implementation.
462fe8fb19SBen Gras  *
472fe8fb19SBen Gras  * Copyright (C) 1984, Sun Microsystems, Inc.
482fe8fb19SBen Gras  *
492fe8fb19SBen Gras  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
502fe8fb19SBen Gras  * arrays.  See xdr.h for more info on the interface to xdr.
512fe8fb19SBen Gras  */
522fe8fb19SBen Gras 
532fe8fb19SBen Gras #include "namespace.h"
542fe8fb19SBen Gras 
552fe8fb19SBen Gras #include <err.h>
562fe8fb19SBen Gras #include <stdio.h>
572fe8fb19SBen Gras #include <stdlib.h>
582fe8fb19SBen Gras #include <string.h>
592fe8fb19SBen Gras #include <limits.h>
602fe8fb19SBen Gras 
612fe8fb19SBen Gras #include <rpc/types.h>
622fe8fb19SBen Gras #include <rpc/xdr.h>
632fe8fb19SBen Gras 
642fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(xdr_array,_xdr_array)652fe8fb19SBen Gras __weak_alias(xdr_array,_xdr_array)
662fe8fb19SBen Gras __weak_alias(xdr_vector,_xdr_vector)
672fe8fb19SBen Gras #endif
682fe8fb19SBen Gras 
692fe8fb19SBen Gras /*
702fe8fb19SBen Gras  * XDR an array of arbitrary elements
712fe8fb19SBen Gras  * *addrp is a pointer to the array, *sizep is the number of elements.
722fe8fb19SBen Gras  * If addrp is NULL (*sizep * elsize) bytes are allocated.
732fe8fb19SBen Gras  * elsize is the size (in bytes) of each element, and elproc is the
742fe8fb19SBen Gras  * xdr procedure to call to handle each element of the array.
752fe8fb19SBen Gras  */
762fe8fb19SBen Gras bool_t
77f14fb602SLionel Sambuc xdr_array(XDR *xdrs, caddr_t *addrp, u_int *sizep, u_int maxsize, u_int elsize,
78f14fb602SLionel Sambuc     xdrproc_t elproc)
792fe8fb19SBen Gras {
802fe8fb19SBen Gras 	u_int i;
812fe8fb19SBen Gras 	caddr_t target = *addrp;
822fe8fb19SBen Gras 	u_int c;  /* the actual element count */
832fe8fb19SBen Gras 	bool_t stat = TRUE;
842fe8fb19SBen Gras 	u_int nodesize;
852fe8fb19SBen Gras 
862fe8fb19SBen Gras 	/* like strings, arrays are really counted arrays */
872fe8fb19SBen Gras 	if (!xdr_u_int(xdrs, sizep))
882fe8fb19SBen Gras 		return (FALSE);
892fe8fb19SBen Gras 
902fe8fb19SBen Gras 	c = *sizep;
912fe8fb19SBen Gras 	if ((c > maxsize || UINT_MAX/elsize < c) &&
922fe8fb19SBen Gras 	    (xdrs->x_op != XDR_FREE))
932fe8fb19SBen Gras 		return (FALSE);
942fe8fb19SBen Gras 	nodesize = c * elsize;
952fe8fb19SBen Gras 
962fe8fb19SBen Gras 	/*
972fe8fb19SBen Gras 	 * if we are deserializing, we may need to allocate an array.
982fe8fb19SBen Gras 	 * We also save time by checking for a null array if we are freeing.
992fe8fb19SBen Gras 	 */
1002fe8fb19SBen Gras 	if (target == NULL)
1012fe8fb19SBen Gras 		switch (xdrs->x_op) {
1022fe8fb19SBen Gras 		case XDR_DECODE:
1032fe8fb19SBen Gras 			if (c == 0)
1042fe8fb19SBen Gras 				return (TRUE);
1052fe8fb19SBen Gras 			*addrp = target = mem_alloc(nodesize);
1062fe8fb19SBen Gras 			if (target == NULL) {
107*84d9c625SLionel Sambuc 				warn("%s: out of memory", __func__);
1082fe8fb19SBen Gras 				return (FALSE);
1092fe8fb19SBen Gras 			}
1102fe8fb19SBen Gras 			memset(target, 0, nodesize);
1112fe8fb19SBen Gras 			break;
1122fe8fb19SBen Gras 
1132fe8fb19SBen Gras 		case XDR_FREE:
1142fe8fb19SBen Gras 			return (TRUE);
1152fe8fb19SBen Gras 
1162fe8fb19SBen Gras 		case XDR_ENCODE:
1172fe8fb19SBen Gras 			break;
1182fe8fb19SBen Gras 	}
1192fe8fb19SBen Gras 
1202fe8fb19SBen Gras 	/*
1212fe8fb19SBen Gras 	 * now we xdr each element of array
1222fe8fb19SBen Gras 	 */
1232fe8fb19SBen Gras 	for (i = 0; (i < c) && stat; i++) {
1242fe8fb19SBen Gras 		stat = (*elproc)(xdrs, target);
1252fe8fb19SBen Gras 		target += elsize;
1262fe8fb19SBen Gras 	}
1272fe8fb19SBen Gras 
1282fe8fb19SBen Gras 	/*
1292fe8fb19SBen Gras 	 * the array may need freeing
1302fe8fb19SBen Gras 	 */
1312fe8fb19SBen Gras 	if (xdrs->x_op == XDR_FREE) {
1322fe8fb19SBen Gras 		mem_free(*addrp, nodesize);
1332fe8fb19SBen Gras 		*addrp = NULL;
1342fe8fb19SBen Gras 	}
1352fe8fb19SBen Gras 	return (stat);
1362fe8fb19SBen Gras }
1372fe8fb19SBen Gras 
1382fe8fb19SBen Gras /*
1392fe8fb19SBen Gras  * xdr_vector():
1402fe8fb19SBen Gras  *
1412fe8fb19SBen Gras  * XDR a fixed length array. Unlike variable-length arrays,
1422fe8fb19SBen Gras  * the storage of fixed length arrays is static and unfreeable.
1432fe8fb19SBen Gras  * > basep: base of the array
1442fe8fb19SBen Gras  * > size: size of the array
1452fe8fb19SBen Gras  * > elemsize: size of each element
1462fe8fb19SBen Gras  * > xdr_elem: routine to XDR each element
1472fe8fb19SBen Gras  */
1482fe8fb19SBen Gras bool_t
xdr_vector(XDR * xdrs,char * basep,u_int nelem,u_int elemsize,xdrproc_t xdr_elem)149f14fb602SLionel Sambuc xdr_vector(XDR *xdrs, char *basep, u_int nelem, u_int elemsize,
150f14fb602SLionel Sambuc     xdrproc_t xdr_elem)
1512fe8fb19SBen Gras {
1522fe8fb19SBen Gras 	u_int i;
1532fe8fb19SBen Gras 	char *elptr;
1542fe8fb19SBen Gras 
1552fe8fb19SBen Gras 	elptr = basep;
1562fe8fb19SBen Gras 	for (i = 0; i < nelem; i++) {
1572fe8fb19SBen Gras 		if (!(*xdr_elem)(xdrs, elptr)) {
1582fe8fb19SBen Gras 			return(FALSE);
1592fe8fb19SBen Gras 		}
1602fe8fb19SBen Gras 		elptr += elemsize;
1612fe8fb19SBen Gras 	}
1622fe8fb19SBen Gras 	return(TRUE);
1632fe8fb19SBen Gras }
164