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