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 | #include <limits.h> |
---|
40 | #include <stdlib.h> |
---|
41 | #include <string.h> |
---|
42 | #include <errno.h> |
---|
43 | |
---|
44 | #include <rpc/types.h> |
---|
45 | #include <rpc/xdr.h> |
---|
46 | |
---|
47 | #include "xdr_private.h" |
---|
48 | |
---|
49 | /* |
---|
50 | * XDR an array of arbitrary elements |
---|
51 | * *addrp is a pointer to the array, *sizep is the number of elements. |
---|
52 | * If addrp is NULL (*sizep * elsize) bytes are allocated. |
---|
53 | * elsize is the size (in bytes) of each element, and elproc is the |
---|
54 | * xdr procedure to call to handle each element of the array. |
---|
55 | */ |
---|
56 | bool_t |
---|
57 | xdr_array (XDR * xdrs, |
---|
58 | caddr_t * addrp, |
---|
59 | u_int * sizep, |
---|
60 | u_int maxsize, |
---|
61 | u_int elsize, |
---|
62 | xdrproc_t elproc) |
---|
63 | { |
---|
64 | u_int i; |
---|
65 | caddr_t target = *addrp; |
---|
66 | u_int c; /* the actual element count */ |
---|
67 | bool_t stat = TRUE; |
---|
68 | u_int nodesize; |
---|
69 | |
---|
70 | /* like strings, arrays are really counted arrays */ |
---|
71 | if (!xdr_u_int (xdrs, sizep)) |
---|
72 | { |
---|
73 | return FALSE; |
---|
74 | } |
---|
75 | c = *sizep; |
---|
76 | if ((c > maxsize || UINT_MAX / elsize < c) && (xdrs->x_op != XDR_FREE)) |
---|
77 | { |
---|
78 | return FALSE; |
---|
79 | } |
---|
80 | nodesize = c * elsize; |
---|
81 | |
---|
82 | /* |
---|
83 | * if we are deserializing, we may need to allocate an array. |
---|
84 | * We also save time by checking for a null array if we are freeing. |
---|
85 | */ |
---|
86 | if (target == NULL) |
---|
87 | switch (xdrs->x_op) |
---|
88 | { |
---|
89 | case XDR_DECODE: |
---|
90 | if (c == 0) |
---|
91 | return TRUE; |
---|
92 | *addrp = target = mem_alloc (nodesize); |
---|
93 | if (target == NULL) |
---|
94 | { |
---|
95 | xdr_warnx ("xdr_array: out of memory"); |
---|
96 | errno = ENOMEM; |
---|
97 | return FALSE; |
---|
98 | } |
---|
99 | memset (target, 0, nodesize); |
---|
100 | break; |
---|
101 | |
---|
102 | case XDR_FREE: |
---|
103 | return TRUE; |
---|
104 | |
---|
105 | case XDR_ENCODE: |
---|
106 | break; |
---|
107 | } |
---|
108 | |
---|
109 | /* |
---|
110 | * now we xdr each element of array |
---|
111 | */ |
---|
112 | for (i = 0; (i < c) && stat; i++) |
---|
113 | { |
---|
114 | stat = (*elproc) (xdrs, target); |
---|
115 | target += elsize; |
---|
116 | } |
---|
117 | |
---|
118 | /* |
---|
119 | * the array may need freeing |
---|
120 | */ |
---|
121 | if (xdrs->x_op == XDR_FREE) |
---|
122 | { |
---|
123 | mem_free (*addrp, nodesize); |
---|
124 | *addrp = NULL; |
---|
125 | } |
---|
126 | return (stat); |
---|
127 | } |
---|
128 | |
---|
129 | /* |
---|
130 | * xdr_vector(): |
---|
131 | * |
---|
132 | * XDR a fixed length array. Unlike variable-length arrays, |
---|
133 | * the storage of fixed length arrays is static and unfreeable. |
---|
134 | * > basep: base of the array |
---|
135 | * > size: size of the array |
---|
136 | * > elemsize: size of each element |
---|
137 | * > xdr_elem: routine to XDR each element |
---|
138 | */ |
---|
139 | bool_t |
---|
140 | xdr_vector (XDR * xdrs, |
---|
141 | char *basep, |
---|
142 | u_int nelem, |
---|
143 | u_int elemsize, |
---|
144 | xdrproc_t xdr_elem) |
---|
145 | { |
---|
146 | u_int i; |
---|
147 | char *elptr; |
---|
148 | |
---|
149 | elptr = basep; |
---|
150 | for (i = 0; i < nelem; i++) |
---|
151 | { |
---|
152 | if (!(*xdr_elem) (xdrs, elptr)) |
---|
153 | { |
---|
154 | return FALSE; |
---|
155 | } |
---|
156 | elptr += elemsize; |
---|
157 | } |
---|
158 | return TRUE; |
---|
159 | } |
---|