Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / xdr / xdr_array.c
blob77e7164d26720bb659b9eb09bfec4a65ad7f60f6
2 /*
3 * Copyright (c) 2009, Sun Microsystems, Inc.
4 * All rights reserved.
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.
31 * xdr_array.c, Generic XDR routines impelmentation.
33 * Copyright (C) 1984, Sun Microsystems, Inc.
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.
39 #include <limits.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <errno.h>
44 #include <rpc/types.h>
45 #include <rpc/xdr.h>
47 #include "xdr_private.h"
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.
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)
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;
70 /* like strings, arrays are really counted arrays */
71 if (!xdr_u_int (xdrs, sizep))
73 return FALSE;
75 c = *sizep;
76 if ((c > maxsize || UINT_MAX / elsize < c) && (xdrs->x_op != XDR_FREE))
78 return FALSE;
80 nodesize = c * elsize;
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.
86 if (target == NULL)
87 switch (xdrs->x_op)
89 case XDR_DECODE:
90 if (c == 0)
91 return TRUE;
92 *addrp = target = mem_alloc (nodesize);
93 if (target == NULL)
95 xdr_warnx ("xdr_array: out of memory");
96 errno = ENOMEM;
97 return FALSE;
99 memset (target, 0, nodesize);
100 break;
102 case XDR_FREE:
103 return TRUE;
105 case XDR_ENCODE:
106 break;
110 * now we xdr each element of array
112 for (i = 0; (i < c) && stat; i++)
114 stat = (*elproc) (xdrs, target);
115 target += elsize;
119 * the array may need freeing
121 if (xdrs->x_op == XDR_FREE)
123 mem_free (*addrp, nodesize);
124 *addrp = NULL;
126 return (stat);
130 * xdr_vector():
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
139 bool_t
140 xdr_vector (XDR * xdrs,
141 char *basep,
142 u_int nelem,
143 u_int elemsize,
144 xdrproc_t xdr_elem)
146 u_int i;
147 char *elptr;
149 elptr = basep;
150 for (i = 0; i < nelem; i++)
152 if (!(*xdr_elem) (xdrs, elptr))
154 return FALSE;
156 elptr += elemsize;
158 return TRUE;