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