1
2 /*
3 * Copyright (c) 2009, Sun Microsystems, Inc.
4 * All rights reserved.
5 *
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.
16 *
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.
28 */
29
30 /*
31 * xdr_array.c, Generic XDR routines impelmentation.
32 *
33 * Copyright (C) 1984, Sun Microsystems, Inc.
34 *
35 * These are the "non-trivial" xdr primitives used to serialize and de-serialize
36 * arrays. See xdr.h for more info on the interface to xdr.
37 */
38
39 #define _DEFAULT_SOURCE
40 #include <limits.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <errno.h>
44
45 #include <rpc/types.h>
46 #include <rpc/xdr.h>
47
48 #include "xdr_private.h"
49
50 /*
51 * XDR an array of arbitrary elements
52 * *addrp is a pointer to the array, *sizep is the number of elements.
53 * If addrp is NULL (*sizep * elsize) bytes are allocated.
54 * elsize is the size (in bytes) of each element, and elproc is the
55 * xdr procedure to call to handle each element of the array.
56 */
57 bool_t
xdr_array(XDR * xdrs,caddr_t * addrp,u_int * sizep,u_int maxsize,u_int elsize,xdrproc_t elproc)58 xdr_array (XDR * xdrs,
59 caddr_t * addrp,
60 u_int * sizep,
61 u_int maxsize,
62 u_int elsize,
63 xdrproc_t elproc)
64 {
65 u_int i;
66 caddr_t target = *addrp;
67 u_int c; /* the actual element count */
68 bool_t stat = TRUE;
69 u_int nodesize;
70
71 /* like strings, arrays are really counted arrays */
72 if (!xdr_u_int (xdrs, sizep))
73 {
74 return FALSE;
75 }
76 c = *sizep;
77 if ((c > maxsize || UINT_MAX / elsize < c) && (xdrs->x_op != XDR_FREE))
78 {
79 return FALSE;
80 }
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 {
90 case XDR_DECODE:
91 if (c == 0)
92 return TRUE;
93 *addrp = target = mem_alloc (nodesize);
94 if (target == NULL)
95 {
96 xdr_warnx ("xdr_array: out of memory");
97 errno = ENOMEM;
98 return FALSE;
99 }
100 memset (target, 0, nodesize);
101 break;
102
103 case XDR_FREE:
104 return TRUE;
105
106 case XDR_ENCODE:
107 break;
108 }
109
110 /*
111 * now we xdr each element of array
112 */
113 for (i = 0; (i < c) && stat; i++)
114 {
115 stat = (*elproc) (xdrs, target);
116 target += elsize;
117 }
118
119 /*
120 * the array may need freeing
121 */
122 if (xdrs->x_op == XDR_FREE)
123 {
124 mem_free (*addrp, nodesize);
125 *addrp = NULL;
126 }
127 return (stat);
128 }
129
130 /*
131 * xdr_vector():
132 *
133 * XDR a fixed length array. Unlike variable-length arrays,
134 * the storage of fixed length arrays is static and unfreeable.
135 * > basep: base of the array
136 * > size: size of the array
137 * > elemsize: size of each element
138 * > xdr_elem: routine to XDR each element
139 */
140 bool_t
xdr_vector(XDR * xdrs,char * basep,u_int nelem,u_int elemsize,xdrproc_t xdr_elem)141 xdr_vector (XDR * xdrs,
142 char *basep,
143 u_int nelem,
144 u_int elemsize,
145 xdrproc_t xdr_elem)
146 {
147 u_int i;
148 char *elptr;
149
150 elptr = basep;
151 for (i = 0; i < nelem; i++)
152 {
153 if (!(*xdr_elem) (xdrs, elptr))
154 {
155 return FALSE;
156 }
157 elptr += elemsize;
158 }
159 return TRUE;
160 }
161