xdr_array.c revision 1.1 1 1.1 hannken /* $NetBSD: xdr_array.c,v 1.1 2019/06/04 15:07:55 hannken Exp $ */
2 1.1 hannken
3 1.1 hannken /*
4 1.1 hannken * Copyright (c) 2010, Oracle America, Inc.
5 1.1 hannken *
6 1.1 hannken * Redistribution and use in source and binary forms, with or without
7 1.1 hannken * modification, are permitted provided that the following conditions are
8 1.1 hannken * met:
9 1.1 hannken *
10 1.1 hannken * * Redistributions of source code must retain the above copyright
11 1.1 hannken * notice, this list of conditions and the following disclaimer.
12 1.1 hannken * * Redistributions in binary form must reproduce the above
13 1.1 hannken * copyright notice, this list of conditions and the following
14 1.1 hannken * disclaimer in the documentation and/or other materials
15 1.1 hannken * provided with the distribution.
16 1.1 hannken * * Neither the name of the "Oracle America, Inc." nor the names of its
17 1.1 hannken * contributors may be used to endorse or promote products derived
18 1.1 hannken * from this software without specific prior written permission.
19 1.1 hannken *
20 1.1 hannken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 1.1 hannken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 1.1 hannken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 1.1 hannken * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 1.1 hannken * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 1.1 hannken * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 hannken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 1.1 hannken * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 hannken * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.1 hannken * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 1.1 hannken * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 1.1 hannken * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 hannken */
33 1.1 hannken
34 1.1 hannken #include <sys/cdefs.h>
35 1.1 hannken #if defined(LIBC_SCCS) && !defined(lint)
36 1.1 hannken #if 0
37 1.1 hannken static char *sccsid = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";
38 1.1 hannken static char *sccsid = "@(#)xdr_array.c 2.1 88/07/29 4.0 RPCSRC";
39 1.1 hannken #else
40 1.1 hannken __RCSID("$NetBSD: xdr_array.c,v 1.1 2019/06/04 15:07:55 hannken Exp $");
41 1.1 hannken #endif
42 1.1 hannken #endif
43 1.1 hannken
44 1.1 hannken /*
45 1.1 hannken * xdr_array.c, Generic XDR routines implementation.
46 1.1 hannken *
47 1.1 hannken * Copyright (C) 1984, Sun Microsystems, Inc.
48 1.1 hannken *
49 1.1 hannken * These are the "non-trivial" xdr primitives used to serialize and de-serialize
50 1.1 hannken * arrays. See xdr.h for more info on the interface to xdr.
51 1.1 hannken */
52 1.1 hannken
53 1.1 hannken #include "namespace.h"
54 1.1 hannken
55 1.1 hannken #include <err.h>
56 1.1 hannken #include <stdio.h>
57 1.1 hannken #include <stdlib.h>
58 1.1 hannken #include <string.h>
59 1.1 hannken #include <limits.h>
60 1.1 hannken
61 1.1 hannken #include <rpc/types.h>
62 1.1 hannken #include <rpc/xdr.h>
63 1.1 hannken
64 1.1 hannken #ifdef __weak_alias
65 1.1 hannken __weak_alias(xdr_array,_xdr_array)
66 1.1 hannken __weak_alias(xdr_vector,_xdr_vector)
67 1.1 hannken #endif
68 1.1 hannken
69 1.1 hannken /*
70 1.1 hannken * XDR an array of arbitrary elements
71 1.1 hannken * *addrp is a pointer to the array, *sizep is the number of elements.
72 1.1 hannken * If addrp is NULL (*sizep * elsize) bytes are allocated.
73 1.1 hannken * elsize is the size (in bytes) of each element, and elproc is the
74 1.1 hannken * xdr procedure to call to handle each element of the array.
75 1.1 hannken */
76 1.1 hannken bool_t
77 1.1 hannken xdr_array(XDR *xdrs, caddr_t *addrp, u_int *sizep, u_int maxsize, u_int elsize,
78 1.1 hannken xdrproc_t elproc)
79 1.1 hannken {
80 1.1 hannken u_int i;
81 1.1 hannken caddr_t target = *addrp;
82 1.1 hannken u_int c; /* the actual element count */
83 1.1 hannken bool_t stat = TRUE;
84 1.1 hannken u_int nodesize;
85 1.1 hannken
86 1.1 hannken /* like strings, arrays are really counted arrays */
87 1.1 hannken if (!xdr_u_int(xdrs, sizep))
88 1.1 hannken return (FALSE);
89 1.1 hannken
90 1.1 hannken c = *sizep;
91 1.1 hannken if ((c > maxsize || UINT_MAX/elsize < c) &&
92 1.1 hannken (xdrs->x_op != XDR_FREE))
93 1.1 hannken return (FALSE);
94 1.1 hannken nodesize = c * elsize;
95 1.1 hannken
96 1.1 hannken /*
97 1.1 hannken * if we are deserializing, we may need to allocate an array.
98 1.1 hannken * We also save time by checking for a null array if we are freeing.
99 1.1 hannken */
100 1.1 hannken if (target == NULL)
101 1.1 hannken switch (xdrs->x_op) {
102 1.1 hannken case XDR_DECODE:
103 1.1 hannken if (c == 0)
104 1.1 hannken return (TRUE);
105 1.1 hannken *addrp = target = mem_alloc(nodesize);
106 1.1 hannken if (target == NULL) {
107 1.1 hannken warn("%s: out of memory", __func__);
108 1.1 hannken return (FALSE);
109 1.1 hannken }
110 1.1 hannken memset(target, 0, nodesize);
111 1.1 hannken break;
112 1.1 hannken
113 1.1 hannken case XDR_FREE:
114 1.1 hannken return (TRUE);
115 1.1 hannken
116 1.1 hannken case XDR_ENCODE:
117 1.1 hannken break;
118 1.1 hannken }
119 1.1 hannken
120 1.1 hannken /*
121 1.1 hannken * now we xdr each element of array
122 1.1 hannken */
123 1.1 hannken for (i = 0; (i < c) && stat; i++) {
124 1.1 hannken stat = (*elproc)(xdrs, target);
125 1.1 hannken target += elsize;
126 1.1 hannken }
127 1.1 hannken
128 1.1 hannken /*
129 1.1 hannken * the array may need freeing
130 1.1 hannken */
131 1.1 hannken if (xdrs->x_op == XDR_FREE) {
132 1.1 hannken mem_free(*addrp, nodesize);
133 1.1 hannken *addrp = NULL;
134 1.1 hannken }
135 1.1 hannken return (stat);
136 1.1 hannken }
137 1.1 hannken
138 1.1 hannken /*
139 1.1 hannken * xdr_vector():
140 1.1 hannken *
141 1.1 hannken * XDR a fixed length array. Unlike variable-length arrays,
142 1.1 hannken * the storage of fixed length arrays is static and unfreeable.
143 1.1 hannken * > basep: base of the array
144 1.1 hannken * > size: size of the array
145 1.1 hannken * > elemsize: size of each element
146 1.1 hannken * > xdr_elem: routine to XDR each element
147 1.1 hannken */
148 1.1 hannken bool_t
149 1.1 hannken xdr_vector(XDR *xdrs, char *basep, u_int nelem, u_int elemsize,
150 1.1 hannken xdrproc_t xdr_elem)
151 1.1 hannken {
152 1.1 hannken u_int i;
153 1.1 hannken char *elptr;
154 1.1 hannken
155 1.1 hannken elptr = basep;
156 1.1 hannken for (i = 0; i < nelem; i++) {
157 1.1 hannken if (!(*xdr_elem)(xdrs, elptr)) {
158 1.1 hannken return(FALSE);
159 1.1 hannken }
160 1.1 hannken elptr += elemsize;
161 1.1 hannken }
162 1.1 hannken return(TRUE);
163 1.1 hannken }
164