xref: /freebsd/lib/libc/xdr/xdr_array.c (revision bdcbfde31e8e9b343f113a1956384bdf30d1ed62)
1 /*	$NetBSD: xdr_array.c,v 1.12 2000/01/22 22:19:18 mycroft Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2010, Oracle America, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above
15  *       copyright notice, this list of conditions and the following
16  *       disclaimer in the documentation and/or other materials
17  *       provided with the distribution.
18  *     * Neither the name of the "Oracle America, Inc." nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /*
37  * xdr_array.c, Generic XDR routines implementation.
38  *
39  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
40  * arrays.  See xdr.h for more info on the interface to xdr.
41  */
42 
43 #include "namespace.h"
44 #include <err.h>
45 #include <limits.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 
50 #include <rpc/types.h>
51 #include <rpc/xdr.h>
52 #include "un-namespace.h"
53 
54 /*
55  * XDR an array of arbitrary elements
56  * *addrp is a pointer to the array, *sizep is the number of elements.
57  * If addrp is NULL (*sizep * elsize) bytes are allocated.
58  * elsize is the size (in bytes) of each element, and elproc is the
59  * xdr procedure to call to handle each element of the array.
60  */
61 bool_t
62 xdr_array(XDR *xdrs, caddr_t *addrp, u_int *sizep, u_int maxsize, u_int elsize, xdrproc_t elproc)
63 /*
64  *	XDR *xdrs;
65  *	caddr_t *addrp;		// array pointer
66  *	u_int *sizep;		// number of elements
67  *	u_int maxsize;		// max numberof elements
68  *	u_int elsize;		// size in bytes of each element
69  *	xdrproc_t elproc;	// xdr routine to handle each element
70  */
71 {
72 	u_int i;
73 	caddr_t target = *addrp;
74 	u_int c;  /* the actual element count */
75 	bool_t stat = TRUE;
76 	u_int nodesize;
77 
78 	/* like strings, arrays are really counted arrays */
79 	if (!xdr_u_int(xdrs, sizep)) {
80 		return (FALSE);
81 	}
82 	c = *sizep;
83 	if ((c > maxsize || UINT_MAX/elsize < c) &&
84 	    (xdrs->x_op != XDR_FREE)) {
85 		return (FALSE);
86 	}
87 	nodesize = c * elsize;
88 
89 	/*
90 	 * if we are deserializing, we may need to allocate an array.
91 	 * We also save time by checking for a null array if we are freeing.
92 	 */
93 	if (target == NULL)
94 		switch (xdrs->x_op) {
95 		case XDR_DECODE:
96 			if (c == 0)
97 				return (TRUE);
98 			*addrp = target = mem_alloc(nodesize);
99 			if (target == NULL) {
100 				warnx("xdr_array: out of memory");
101 				return (FALSE);
102 			}
103 			memset(target, 0, nodesize);
104 			break;
105 
106 		case XDR_FREE:
107 			return (TRUE);
108 
109 		case XDR_ENCODE:
110 			break;
111 	}
112 
113 	/*
114 	 * now we xdr each element of array
115 	 */
116 	for (i = 0; (i < c) && stat; i++) {
117 		stat = (*elproc)(xdrs, target);
118 		target += elsize;
119 	}
120 
121 	/*
122 	 * the array may need freeing
123 	 */
124 	if (xdrs->x_op == XDR_FREE) {
125 		mem_free(*addrp, nodesize);
126 		*addrp = NULL;
127 	}
128 	return (stat);
129 }
130 
131 /*
132  * xdr_vector():
133  *
134  * XDR a fixed length array. Unlike variable-length arrays,
135  * the storage of fixed length arrays is static and unfreeable.
136  * > basep: base of the array
137  * > size: size of the array
138  * > elemsize: size of each element
139  * > xdr_elem: routine to XDR each element
140  */
141 bool_t
142 xdr_vector(XDR *xdrs, char *basep, u_int nelem, u_int elemsize, xdrproc_t xdr_elem)
143 {
144 	u_int i;
145 	char *elptr;
146 
147 	elptr = basep;
148 	for (i = 0; i < nelem; i++) {
149 		if (!(*xdr_elem)(xdrs, elptr)) {
150 			return(FALSE);
151 		}
152 		elptr += elemsize;
153 	}
154 	return(TRUE);
155 }
156