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