3 * Copyright (c) 2009, Sun Microsystems, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * - Neither the name of Sun Microsystems, Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 //#include <sys/cdefs.h>
33 * xdr_array.c, Generic XDR routines impelmentation.
35 * Copyright (C) 1984, Sun Microsystems, Inc.
37 * These are the "non-trivial" xdr primitives used to serialize and de-serialize
38 * arrays. See xdr.h for more info on the interface to xdr.
42 #include "namespace.h"
49 #include <rpc/types.h>
51 #include "un-namespace.h"
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.
61 xdr_array(xdrs
, addrp
, sizep
, maxsize
, elsize
, elproc
)
63 caddr_t
*addrp
; /* array pointer */
64 u_int
*sizep
; /* number of elements */
65 u_int maxsize
; /* max numberof elements */
66 u_int elsize
; /* size in bytes of each element */
67 xdrproc_t elproc
; /* xdr routine to handle each element */
70 caddr_t target
= *addrp
;
71 u_int c
; /* the actual element count */
75 /* like strings, arrays are really counted arrays */
76 if (!xdr_u_int(xdrs
, sizep
)) {
80 if ((c
> maxsize
|| UINT_MAX
/elsize
< c
) &&
81 (xdrs
->x_op
!= XDR_FREE
)) {
84 nodesize
= c
* elsize
;
87 * if we are deserializing, we may need to allocate an array.
88 * We also save time by checking for a null array if we are freeing.
95 *addrp
= target
= mem_alloc(nodesize
);
97 //warnx("xdr_array: out of memory");
100 memset(target
, 0, nodesize
);
111 * now we xdr each element of array
113 for (i
= 0; (i
< c
) && stat
; i
++) {
114 stat
= (*elproc
)(xdrs
, target
);
119 * the array may need freeing
121 if (xdrs
->x_op
== XDR_FREE
) {
122 mem_free(*addrp
, nodesize
);
131 * XDR a fixed length array. Unlike variable-length arrays,
132 * the storage of fixed length arrays is static and unfreeable.
133 * > basep: base of the array
134 * > size: size of the array
135 * > elemsize: size of each element
136 * > xdr_elem: routine to XDR each element
139 xdr_vector(xdrs
, basep
, nelem
, elemsize
, xdr_elem
)
150 for (i
= 0; i
< nelem
; i
++) {
151 if (!(*xdr_elem
)(xdrs
, elptr
)) {