xref: /netbsd-src/common/lib/libc/rpc/xdr_array.c (revision bf1be5afae38ea5e3fa10cf6bfd64cdb5c69e3ba)
1*bf1be5afSchristos /*	$NetBSD: xdr_array.c,v 1.3 2019/06/16 16:01:44 christos Exp $	*/
26f60c4f9Shannken 
36f60c4f9Shannken /*
46f60c4f9Shannken  * Copyright (c) 2010, Oracle America, Inc.
56f60c4f9Shannken  *
66f60c4f9Shannken  * Redistribution and use in source and binary forms, with or without
76f60c4f9Shannken  * modification, are permitted provided that the following conditions are
86f60c4f9Shannken  * met:
96f60c4f9Shannken  *
106f60c4f9Shannken  *     * Redistributions of source code must retain the above copyright
116f60c4f9Shannken  *       notice, this list of conditions and the following disclaimer.
126f60c4f9Shannken  *     * Redistributions in binary form must reproduce the above
136f60c4f9Shannken  *       copyright notice, this list of conditions and the following
146f60c4f9Shannken  *       disclaimer in the documentation and/or other materials
156f60c4f9Shannken  *       provided with the distribution.
166f60c4f9Shannken  *     * Neither the name of the "Oracle America, Inc." nor the names of its
176f60c4f9Shannken  *       contributors may be used to endorse or promote products derived
186f60c4f9Shannken  *       from this software without specific prior written permission.
196f60c4f9Shannken  *
206f60c4f9Shannken  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
216f60c4f9Shannken  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
226f60c4f9Shannken  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
236f60c4f9Shannken  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
246f60c4f9Shannken  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
256f60c4f9Shannken  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
266f60c4f9Shannken  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
276f60c4f9Shannken  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
286f60c4f9Shannken  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
296f60c4f9Shannken  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
306f60c4f9Shannken  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
316f60c4f9Shannken  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
326f60c4f9Shannken  */
336f60c4f9Shannken 
346f60c4f9Shannken #include <sys/cdefs.h>
356f60c4f9Shannken #if defined(LIBC_SCCS) && !defined(lint)
366f60c4f9Shannken #if 0
376f60c4f9Shannken static char *sccsid = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";
386f60c4f9Shannken static char *sccsid = "@(#)xdr_array.c	2.1 88/07/29 4.0 RPCSRC";
396f60c4f9Shannken #else
40*bf1be5afSchristos __RCSID("$NetBSD: xdr_array.c,v 1.3 2019/06/16 16:01:44 christos Exp $");
416f60c4f9Shannken #endif
426f60c4f9Shannken #endif
436f60c4f9Shannken 
446f60c4f9Shannken /*
456f60c4f9Shannken  * xdr_array.c, Generic XDR routines implementation.
466f60c4f9Shannken  *
476f60c4f9Shannken  * Copyright (C) 1984, Sun Microsystems, Inc.
486f60c4f9Shannken  *
496f60c4f9Shannken  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
506f60c4f9Shannken  * arrays.  See xdr.h for more info on the interface to xdr.
516f60c4f9Shannken  */
526f60c4f9Shannken 
53c7814b62Shannken #if defined(_KERNEL) || defined(_STANDALONE)
54c7814b62Shannken 
55c7814b62Shannken #include <sys/param.h>
56c7814b62Shannken #include <lib/libkern/libkern.h>
57c7814b62Shannken #include <rpc/types.h>
58c7814b62Shannken #include <rpc/xdr.h>
59c7814b62Shannken 
60c7814b62Shannken #else /* _KERNEL || _STANDALONE */
61c7814b62Shannken 
626f60c4f9Shannken #include "namespace.h"
636f60c4f9Shannken 
646f60c4f9Shannken #include <err.h>
656f60c4f9Shannken #include <stdio.h>
666f60c4f9Shannken #include <stdlib.h>
676f60c4f9Shannken #include <string.h>
686f60c4f9Shannken #include <limits.h>
696f60c4f9Shannken 
706f60c4f9Shannken #include <rpc/types.h>
716f60c4f9Shannken #include <rpc/xdr.h>
726f60c4f9Shannken 
736f60c4f9Shannken #ifdef __weak_alias
__weak_alias(xdr_array,_xdr_array)746f60c4f9Shannken __weak_alias(xdr_array,_xdr_array)
756f60c4f9Shannken __weak_alias(xdr_vector,_xdr_vector)
766f60c4f9Shannken #endif
776f60c4f9Shannken 
78c7814b62Shannken #endif /* _KERNEL || _STANDALONE */
79c7814b62Shannken 
806f60c4f9Shannken /*
816f60c4f9Shannken  * XDR an array of arbitrary elements
826f60c4f9Shannken  * *addrp is a pointer to the array, *sizep is the number of elements.
836f60c4f9Shannken  * If addrp is NULL (*sizep * elsize) bytes are allocated.
846f60c4f9Shannken  * elsize is the size (in bytes) of each element, and elproc is the
856f60c4f9Shannken  * xdr procedure to call to handle each element of the array.
866f60c4f9Shannken  */
876f60c4f9Shannken bool_t
88*bf1be5afSchristos xdr_array(XDR *xdrs, char **addrp, u_int *sizep, u_int maxsize, u_int elsize,
896f60c4f9Shannken     xdrproc_t elproc)
906f60c4f9Shannken {
916f60c4f9Shannken 	u_int i;
92*bf1be5afSchristos 	char *target = *addrp;
936f60c4f9Shannken 	u_int c;  /* the actual element count */
946f60c4f9Shannken 	bool_t stat = TRUE;
956f60c4f9Shannken 	u_int nodesize;
966f60c4f9Shannken 
976f60c4f9Shannken 	/* like strings, arrays are really counted arrays */
986f60c4f9Shannken 	if (!xdr_u_int(xdrs, sizep))
996f60c4f9Shannken 		return (FALSE);
1006f60c4f9Shannken 
1016f60c4f9Shannken 	c = *sizep;
1026f60c4f9Shannken 	if ((c > maxsize || UINT_MAX/elsize < c) &&
1036f60c4f9Shannken 	    (xdrs->x_op != XDR_FREE))
1046f60c4f9Shannken 		return (FALSE);
1056f60c4f9Shannken 	nodesize = c * elsize;
1066f60c4f9Shannken 
1076f60c4f9Shannken 	/*
1086f60c4f9Shannken 	 * if we are deserializing, we may need to allocate an array.
1096f60c4f9Shannken 	 * We also save time by checking for a null array if we are freeing.
1106f60c4f9Shannken 	 */
1116f60c4f9Shannken 	if (target == NULL)
1126f60c4f9Shannken 		switch (xdrs->x_op) {
1136f60c4f9Shannken 		case XDR_DECODE:
1146f60c4f9Shannken 			if (c == 0)
1156f60c4f9Shannken 				return (TRUE);
1166f60c4f9Shannken 			*addrp = target = mem_alloc(nodesize);
1176f60c4f9Shannken 			if (target == NULL) {
1186f60c4f9Shannken 				warn("%s: out of memory", __func__);
1196f60c4f9Shannken 				return (FALSE);
1206f60c4f9Shannken 			}
1216f60c4f9Shannken 			memset(target, 0, nodesize);
1226f60c4f9Shannken 			break;
1236f60c4f9Shannken 
1246f60c4f9Shannken 		case XDR_FREE:
1256f60c4f9Shannken 			return (TRUE);
1266f60c4f9Shannken 
1276f60c4f9Shannken 		case XDR_ENCODE:
1286f60c4f9Shannken 			break;
1296f60c4f9Shannken 	}
1306f60c4f9Shannken 
1316f60c4f9Shannken 	/*
1326f60c4f9Shannken 	 * now we xdr each element of array
1336f60c4f9Shannken 	 */
1346f60c4f9Shannken 	for (i = 0; (i < c) && stat; i++) {
1356f60c4f9Shannken 		stat = (*elproc)(xdrs, target);
1366f60c4f9Shannken 		target += elsize;
1376f60c4f9Shannken 	}
1386f60c4f9Shannken 
1396f60c4f9Shannken 	/*
1406f60c4f9Shannken 	 * the array may need freeing
1416f60c4f9Shannken 	 */
1426f60c4f9Shannken 	if (xdrs->x_op == XDR_FREE) {
1436f60c4f9Shannken 		mem_free(*addrp, nodesize);
1446f60c4f9Shannken 		*addrp = NULL;
1456f60c4f9Shannken 	}
1466f60c4f9Shannken 	return (stat);
1476f60c4f9Shannken }
1486f60c4f9Shannken 
1496f60c4f9Shannken /*
1506f60c4f9Shannken  * xdr_vector():
1516f60c4f9Shannken  *
1526f60c4f9Shannken  * XDR a fixed length array. Unlike variable-length arrays,
1536f60c4f9Shannken  * the storage of fixed length arrays is static and unfreeable.
1546f60c4f9Shannken  * > basep: base of the array
1556f60c4f9Shannken  * > size: size of the array
1566f60c4f9Shannken  * > elemsize: size of each element
1576f60c4f9Shannken  * > xdr_elem: routine to XDR each element
1586f60c4f9Shannken  */
1596f60c4f9Shannken bool_t
xdr_vector(XDR * xdrs,char * basep,u_int nelem,u_int elemsize,xdrproc_t xdr_elem)1606f60c4f9Shannken xdr_vector(XDR *xdrs, char *basep, u_int nelem, u_int elemsize,
1616f60c4f9Shannken     xdrproc_t xdr_elem)
1626f60c4f9Shannken {
1636f60c4f9Shannken 	u_int i;
1646f60c4f9Shannken 	char *elptr;
1656f60c4f9Shannken 
1666f60c4f9Shannken 	elptr = basep;
1676f60c4f9Shannken 	for (i = 0; i < nelem; i++) {
1686f60c4f9Shannken 		if (!(*xdr_elem)(xdrs, elptr)) {
1696f60c4f9Shannken 			return(FALSE);
1706f60c4f9Shannken 		}
1716f60c4f9Shannken 		elptr += elemsize;
1726f60c4f9Shannken 	}
1736f60c4f9Shannken 	return(TRUE);
1746f60c4f9Shannken }
175