xref: /illumos-gate/usr/src/uts/common/rpc/xdr_array.c (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 /*
38  * xdr_array.c, Generic XDR routines impelmentation.
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 <sys/param.h>
44 #include <sys/cmn_err.h>
45 #include <sys/types.h>
46 #include <sys/systm.h>
47 
48 #include <rpc/types.h>
49 #include <rpc/xdr.h>
50 
51 #define	LASTUNSIGNED	((uint_t)0-1)
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
61 xdr_array(XDR *xdrs, caddr_t *addrp, uint_t *sizep, const uint_t maxsize,
62 	const uint_t elsize, const xdrproc_t elproc)
63 {
64 	uint_t i;
65 	caddr_t target = *addrp;
66 	uint_t c;  /* the actual element count */
67 	bool_t stat = TRUE;
68 	uint_t nodesize;
69 
70 	/* like strings, arrays are really counted arrays */
71 	if (!xdr_u_int(xdrs, sizep)) {
72 #ifdef DEBUG
73 		printf("xdr_array: size FAILED\n");
74 #endif
75 		return (FALSE);
76 	}
77 	c = *sizep;
78 	if ((c > maxsize || LASTUNSIGNED / elsize < c) &&
79 	    xdrs->x_op != XDR_FREE) {
80 #ifdef DEBUG
81 		printf("xdr_array: bad size FAILED\n");
82 #endif
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 = (char *)mem_alloc(nodesize);
97 			bzero(target, nodesize);
98 			break;
99 
100 		case XDR_FREE:
101 			return (TRUE);
102 
103 		case XDR_ENCODE:
104 			break;
105 		}
106 
107 	/*
108 	 * now we xdr each element of array
109 	 */
110 	for (i = 0; (i < c) && stat; i++) {
111 		stat = (*elproc)(xdrs, target, LASTUNSIGNED);
112 		target += elsize;
113 	}
114 
115 	/*
116 	 * the array may need freeing
117 	 */
118 	if (xdrs->x_op == XDR_FREE) {
119 		mem_free(*addrp, nodesize);
120 		*addrp = NULL;
121 	}
122 	return (stat);
123 }
124