xref: /freebsd/lib/libc/xdr/xdr_array.c (revision 2512630061f710cdbf448756c7c67774ecdce565)
18360efbdSAlfred Perlstein /*	$NetBSD: xdr_array.c,v 1.12 2000/01/22 22:19:18 mycroft Exp $	*/
28360efbdSAlfred Perlstein 
3a204967aSHiroki Sato /*-
48a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
58a16b7a1SPedro F. Giffuni  *
6a204967aSHiroki Sato  * Copyright (c) 2010, Oracle America, Inc.
7eae561b3SGarrett Wollman  *
8a204967aSHiroki Sato  * Redistribution and use in source and binary forms, with or without
9a204967aSHiroki Sato  * modification, are permitted provided that the following conditions are
10a204967aSHiroki Sato  * met:
11eae561b3SGarrett Wollman  *
12a204967aSHiroki Sato  *     * Redistributions of source code must retain the above copyright
13a204967aSHiroki Sato  *       notice, this list of conditions and the following disclaimer.
14a204967aSHiroki Sato  *     * Redistributions in binary form must reproduce the above
15a204967aSHiroki Sato  *       copyright notice, this list of conditions and the following
16a204967aSHiroki Sato  *       disclaimer in the documentation and/or other materials
17a204967aSHiroki Sato  *       provided with the distribution.
18a204967aSHiroki Sato  *     * Neither the name of the "Oracle America, Inc." nor the names of its
19a204967aSHiroki Sato  *       contributors may be used to endorse or promote products derived
20a204967aSHiroki Sato  *       from this software without specific prior written permission.
21eae561b3SGarrett Wollman  *
22a204967aSHiroki Sato  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23a204967aSHiroki Sato  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24a204967aSHiroki Sato  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25a204967aSHiroki Sato  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26a204967aSHiroki Sato  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27a204967aSHiroki Sato  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28a204967aSHiroki Sato  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29a204967aSHiroki Sato  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30a204967aSHiroki Sato  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31a204967aSHiroki Sato  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32a204967aSHiroki Sato  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33a204967aSHiroki Sato  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34eae561b3SGarrett Wollman  */
35eae561b3SGarrett Wollman 
36eae561b3SGarrett Wollman #if defined(LIBC_SCCS) && !defined(lint)
37a9bdcd37SDavid E. O'Brien static char *sccsid2 = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";
38333fc21eSDavid E. O'Brien static char *sccsid = "@(#)xdr_array.c	2.1 88/07/29 4.0 RPCSRC";
39eae561b3SGarrett Wollman #endif
40333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
41333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
42eae561b3SGarrett Wollman 
43eae561b3SGarrett Wollman /*
44*25126300SPedro F. Giffuni  * xdr_array.c, Generic XDR routines implementation.
45eae561b3SGarrett Wollman  *
46eae561b3SGarrett Wollman  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
47eae561b3SGarrett Wollman  * arrays.  See xdr.h for more info on the interface to xdr.
48eae561b3SGarrett Wollman  */
49eae561b3SGarrett Wollman 
508360efbdSAlfred Perlstein #include "namespace.h"
518360efbdSAlfred Perlstein #include <err.h>
52b113cd80SJacques Vidrine #include <limits.h>
53eae561b3SGarrett Wollman #include <stdio.h>
54eae561b3SGarrett Wollman #include <stdlib.h>
5529285d6cSPoul-Henning Kamp #include <string.h>
568360efbdSAlfred Perlstein 
57eae561b3SGarrett Wollman #include <rpc/types.h>
58eae561b3SGarrett Wollman #include <rpc/xdr.h>
598360efbdSAlfred Perlstein #include "un-namespace.h"
60eae561b3SGarrett Wollman 
61eae561b3SGarrett Wollman /*
62eae561b3SGarrett Wollman  * XDR an array of arbitrary elements
63eae561b3SGarrett Wollman  * *addrp is a pointer to the array, *sizep is the number of elements.
64eae561b3SGarrett Wollman  * If addrp is NULL (*sizep * elsize) bytes are allocated.
65eae561b3SGarrett Wollman  * elsize is the size (in bytes) of each element, and elproc is the
66eae561b3SGarrett Wollman  * xdr procedure to call to handle each element of the array.
67eae561b3SGarrett Wollman  */
68eae561b3SGarrett Wollman bool_t
69d660d38dSCraig Rodrigues xdr_array(XDR *xdrs, caddr_t *addrp, u_int *sizep, u_int maxsize, u_int elsize, xdrproc_t elproc)
70d660d38dSCraig Rodrigues /*
71d660d38dSCraig Rodrigues  *	XDR *xdrs;
72d660d38dSCraig Rodrigues  *	caddr_t *addrp;		// array pointer
73d660d38dSCraig Rodrigues  *	u_int *sizep;		// number of elements
74d660d38dSCraig Rodrigues  *	u_int maxsize;		// max numberof elements
75d660d38dSCraig Rodrigues  *	u_int elsize;		// size in bytes of each element
76d660d38dSCraig Rodrigues  *	xdrproc_t elproc;	// xdr routine to handle each element
77d660d38dSCraig Rodrigues  */
78eae561b3SGarrett Wollman {
798360efbdSAlfred Perlstein 	u_int i;
808360efbdSAlfred Perlstein 	caddr_t target = *addrp;
818360efbdSAlfred Perlstein 	u_int c;  /* the actual element count */
828360efbdSAlfred Perlstein 	bool_t stat = TRUE;
838360efbdSAlfred Perlstein 	u_int nodesize;
84eae561b3SGarrett Wollman 
85eae561b3SGarrett Wollman 	/* like strings, arrays are really counted arrays */
86eae561b3SGarrett Wollman 	if (!xdr_u_int(xdrs, sizep)) {
87eae561b3SGarrett Wollman 		return (FALSE);
88eae561b3SGarrett Wollman 	}
89eae561b3SGarrett Wollman 	c = *sizep;
908f3e3652SJacques Vidrine 	if ((c > maxsize || UINT_MAX/elsize < c) &&
917fc37b7cSDarren Reed 	    (xdrs->x_op != XDR_FREE)) {
92eae561b3SGarrett Wollman 		return (FALSE);
93eae561b3SGarrett Wollman 	}
94eae561b3SGarrett Wollman 	nodesize = c * elsize;
95eae561b3SGarrett Wollman 
96eae561b3SGarrett Wollman 	/*
97eae561b3SGarrett Wollman 	 * if we are deserializing, we may need to allocate an array.
98eae561b3SGarrett Wollman 	 * We also save time by checking for a null array if we are freeing.
99eae561b3SGarrett Wollman 	 */
100eae561b3SGarrett Wollman 	if (target == NULL)
101eae561b3SGarrett Wollman 		switch (xdrs->x_op) {
102eae561b3SGarrett Wollman 		case XDR_DECODE:
103eae561b3SGarrett Wollman 			if (c == 0)
104eae561b3SGarrett Wollman 				return (TRUE);
105eae561b3SGarrett Wollman 			*addrp = target = mem_alloc(nodesize);
106eae561b3SGarrett Wollman 			if (target == NULL) {
1078360efbdSAlfred Perlstein 				warnx("xdr_array: out of memory");
108eae561b3SGarrett Wollman 				return (FALSE);
109eae561b3SGarrett Wollman 			}
1101ad08a09SPeter Wemm 			memset(target, 0, nodesize);
111eae561b3SGarrett Wollman 			break;
112eae561b3SGarrett Wollman 
113eae561b3SGarrett Wollman 		case XDR_FREE:
114eae561b3SGarrett Wollman 			return (TRUE);
1158360efbdSAlfred Perlstein 
1168360efbdSAlfred Perlstein 		case XDR_ENCODE:
1178360efbdSAlfred Perlstein 			break;
118eae561b3SGarrett Wollman 	}
119eae561b3SGarrett Wollman 
120eae561b3SGarrett Wollman 	/*
121eae561b3SGarrett Wollman 	 * now we xdr each element of array
122eae561b3SGarrett Wollman 	 */
123eae561b3SGarrett Wollman 	for (i = 0; (i < c) && stat; i++) {
1248360efbdSAlfred Perlstein 		stat = (*elproc)(xdrs, target);
125eae561b3SGarrett Wollman 		target += elsize;
126eae561b3SGarrett Wollman 	}
127eae561b3SGarrett Wollman 
128eae561b3SGarrett Wollman 	/*
129eae561b3SGarrett Wollman 	 * the array may need freeing
130eae561b3SGarrett Wollman 	 */
131eae561b3SGarrett Wollman 	if (xdrs->x_op == XDR_FREE) {
132eae561b3SGarrett Wollman 		mem_free(*addrp, nodesize);
133eae561b3SGarrett Wollman 		*addrp = NULL;
134eae561b3SGarrett Wollman 	}
135eae561b3SGarrett Wollman 	return (stat);
136eae561b3SGarrett Wollman }
137eae561b3SGarrett Wollman 
138eae561b3SGarrett Wollman /*
139eae561b3SGarrett Wollman  * xdr_vector():
140eae561b3SGarrett Wollman  *
141eae561b3SGarrett Wollman  * XDR a fixed length array. Unlike variable-length arrays,
142eae561b3SGarrett Wollman  * the storage of fixed length arrays is static and unfreeable.
143eae561b3SGarrett Wollman  * > basep: base of the array
144eae561b3SGarrett Wollman  * > size: size of the array
145eae561b3SGarrett Wollman  * > elemsize: size of each element
146eae561b3SGarrett Wollman  * > xdr_elem: routine to XDR each element
147eae561b3SGarrett Wollman  */
148eae561b3SGarrett Wollman bool_t
149d660d38dSCraig Rodrigues xdr_vector(XDR *xdrs, char *basep, u_int nelem, u_int elemsize, xdrproc_t xdr_elem)
150eae561b3SGarrett Wollman {
1518360efbdSAlfred Perlstein 	u_int i;
1528360efbdSAlfred Perlstein 	char *elptr;
153eae561b3SGarrett Wollman 
154eae561b3SGarrett Wollman 	elptr = basep;
155eae561b3SGarrett Wollman 	for (i = 0; i < nelem; i++) {
1568360efbdSAlfred Perlstein 		if (!(*xdr_elem)(xdrs, elptr)) {
157eae561b3SGarrett Wollman 			return(FALSE);
158eae561b3SGarrett Wollman 		}
159eae561b3SGarrett Wollman 		elptr += elemsize;
160eae561b3SGarrett Wollman 	}
161eae561b3SGarrett Wollman 	return(TRUE);
162eae561b3SGarrett Wollman }
163