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