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
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.
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]
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
31 * Portions of this source code were derived from Berkeley
32 * 4.3 BSD under license from the Regents of the University of
37 * 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.
44 #include <sys/types.h>
49 #include <rpc/types.h>
53 #define LASTUNSIGNED ((uint_t)0-1)
55 char mem_err_msg_arr
[] = "xdr_array: out of memory";
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.
65 xdr_array(XDR
*xdrs
, caddr_t
*addrp
, uint_t
*sizep
, const uint_t maxsize
,
66 const uint_t elsize
, const xdrproc_t elproc
)
69 caddr_t target
= *addrp
;
70 uint_t c
; /* the actual element count */
74 /* like strings, arrays are really counted arrays */
75 if (!xdr_u_int(xdrs
, sizep
))
78 if ((c
> maxsize
|| LASTUNSIGNED
/ elsize
< c
) &&
79 xdrs
->x_op
!= XDR_FREE
)
81 nodesize
= c
* elsize
;
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.
92 *addrp
= target
= malloc(nodesize
);
94 (void) syslog(LOG_ERR
, mem_err_msg_arr
);
97 (void) memset(target
, 0, nodesize
);
105 * now we xdr each element of array
107 for (i
= 0; (i
< c
) && stat
; i
++) {
108 stat
= (*elproc
)(xdrs
, target
);
113 * the array may need freeing
115 if (xdrs
->x_op
== XDR_FREE
) {
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
133 xdr_vector(XDR
*xdrs
, char *basep
, const uint_t nelem
,
134 const uint_t elemsize
, const xdrproc_t xdr_elem
)
139 /* Make sure x_op contains a valid value */
140 if (xdrs
->x_op
!= XDR_ENCODE
&&
141 xdrs
->x_op
!= XDR_DECODE
&&
142 xdrs
->x_op
!= XDR_FREE
)
146 for (i
= 0; i
< nelem
; i
++) {
147 if (!(*xdr_elem
)(xdrs
, elptr
, LASTUNSIGNED
))