xref: /titanic_51/usr/src/lib/libnsl/rpc/xdr_array.c (revision 1a7c1b724419d3cb5fa6eea75123c6b2060ba31b)
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 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 /*
30  * Portions of this source code were derived from Berkeley
31  * 4.3 BSD under license from the Regents of the University of
32  * California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 /*
38  * xdr_array.c, Generic XDR routines impelmentation.
39  *
40  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
41  * arrays.  See xdr.h for more info on the interface to xdr.
42  */
43 
44 #include <sys/types.h>
45 #include <syslog.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 
49 #include <rpc/types.h>
50 #include <rpc/xdr.h>
51 #include <memory.h>
52 
53 #define	LASTUNSIGNED	((uint_t)0-1)
54 
55 char mem_err_msg_arr[] = "xdr_array: out of memory";
56 
57 /*
58  * XDR an array of arbitrary elements
59  * *addrp is a pointer to the array, *sizep is the number of elements.
60  * If *addrp is NULL (*sizep * elsize) bytes are allocated.
61  * elsize is the size (in bytes) of each element, and elproc is the
62  * xdr procedure to call to handle each element of the array.
63  */
64 bool_t
65 xdr_array(XDR *xdrs, caddr_t *addrp, uint_t *sizep, const uint_t maxsize,
66 	const uint_t elsize, const xdrproc_t elproc)
67 {
68 	uint_t i;
69 	caddr_t target = *addrp;
70 	uint_t c;  /* the actual element count */
71 	bool_t stat = TRUE;
72 	uint_t nodesize;
73 
74 	/* like strings, arrays are really counted arrays */
75 	if (!xdr_u_int(xdrs, sizep))
76 		return (FALSE);
77 	c = *sizep;
78 	if ((c > maxsize || LASTUNSIGNED / elsize < c) &&
79 	    xdrs->x_op != XDR_FREE)
80 		return (FALSE);
81 	nodesize = c * elsize;
82 
83 	/*
84 	 * if we are deserializing, we may need to allocate an array.
85 	 * We also save time by checking for a null array if we are freeing.
86 	 */
87 	if (target == NULL)
88 		switch (xdrs->x_op) {
89 		case XDR_DECODE:
90 			if (c == 0)
91 				return (TRUE);
92 			*addrp = target = malloc(nodesize);
93 			if (target == NULL) {
94 				(void) syslog(LOG_ERR, mem_err_msg_arr);
95 				return (FALSE);
96 			}
97 			(void) memset(target, 0, nodesize);
98 			break;
99 
100 		case XDR_FREE:
101 			return (TRUE);
102 	}
103 
104 	/*
105 	 * now we xdr each element of array
106 	 */
107 	for (i = 0; (i < c) && stat; i++) {
108 		stat = (*elproc)(xdrs, target);
109 		target += elsize;
110 	}
111 
112 	/*
113 	 * the array may need freeing
114 	 */
115 	if (xdrs->x_op == XDR_FREE) {
116 		free(*addrp);
117 		*addrp = NULL;
118 	}
119 	return (stat);
120 }
121 
122 /*
123  * xdr_vector():
124  *
125  * XDR a fixed length array. Unlike variable-length arrays,
126  * the storage of fixed length arrays is static and unfreeable.
127  * > basep: base of the array
128  * > size: size of the array
129  * > elemsize: size of each element
130  * > xdr_elem: routine to XDR each element
131  */
132 bool_t
133 xdr_vector(XDR *xdrs, char *basep, const uint_t nelem,
134 	const uint_t elemsize, const xdrproc_t xdr_elem)
135 {
136 	uint_t i;
137 	char *elptr;
138 
139 	elptr = basep;
140 	for (i = 0; i < nelem; i++) {
141 		if (!(*xdr_elem)(xdrs, elptr, LASTUNSIGNED))
142 			return (FALSE);
143 		elptr += elemsize;
144 	}
145 	return (TRUE);
146 }
147