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