1*beb15867Skrw /* $OpenBSD: xdr_array.c,v 1.14 2017/01/21 08:29:13 krw Exp $ */
2cb7760d1Smillert
3df930be7Sderaadt /*
4cb7760d1Smillert * Copyright (c) 2010, Oracle America, Inc.
5df930be7Sderaadt *
6cb7760d1Smillert * Redistribution and use in source and binary forms, with or without
7cb7760d1Smillert * modification, are permitted provided that the following conditions are
8cb7760d1Smillert * met:
9df930be7Sderaadt *
10cb7760d1Smillert * * Redistributions of source code must retain the above copyright
11cb7760d1Smillert * notice, this list of conditions and the following disclaimer.
12cb7760d1Smillert * * Redistributions in binary form must reproduce the above
13cb7760d1Smillert * copyright notice, this list of conditions and the following
14cb7760d1Smillert * disclaimer in the documentation and/or other materials
15cb7760d1Smillert * provided with the distribution.
16cb7760d1Smillert * * Neither the name of the "Oracle America, Inc." nor the names of its
17cb7760d1Smillert * contributors may be used to endorse or promote products derived
18cb7760d1Smillert * from this software without specific prior written permission.
19df930be7Sderaadt *
20cb7760d1Smillert * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21cb7760d1Smillert * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22cb7760d1Smillert * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23cb7760d1Smillert * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24cb7760d1Smillert * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25cb7760d1Smillert * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26cb7760d1Smillert * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27cb7760d1Smillert * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28cb7760d1Smillert * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29cb7760d1Smillert * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30cb7760d1Smillert * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31cb7760d1Smillert * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32df930be7Sderaadt */
33df930be7Sderaadt
34df930be7Sderaadt /*
35c8ba362dSespie * xdr_array.c, Generic XDR routines implementation.
36df930be7Sderaadt *
37df930be7Sderaadt * These are the "non-trivial" xdr primitives used to serialize and de-serialize
38df930be7Sderaadt * arrays. See xdr.h for more info on the interface to xdr.
39df930be7Sderaadt */
40df930be7Sderaadt
41df930be7Sderaadt
42df930be7Sderaadt
43df930be7Sderaadt #include <stdio.h>
44df930be7Sderaadt #include <stdlib.h>
45df930be7Sderaadt #include <string.h>
46de1fda84Sderaadt #include <limits.h>
47df930be7Sderaadt #include <rpc/types.h>
48df930be7Sderaadt #include <rpc/xdr.h>
49df930be7Sderaadt
50df930be7Sderaadt /*
51df930be7Sderaadt * XDR an array of arbitrary elements
52df930be7Sderaadt * *addrp is a pointer to the array, *sizep is the number of elements.
53df930be7Sderaadt * If addrp is NULL (*sizep * elsize) bytes are allocated.
54df930be7Sderaadt * elsize is the size (in bytes) of each element, and elproc is the
55df930be7Sderaadt * xdr procedure to call to handle each element of the array.
56df930be7Sderaadt */
57df930be7Sderaadt bool_t
xdr_array(XDR * xdrs,caddr_t * addrp,u_int * sizep,u_int maxsize,u_int elsize,xdrproc_t elproc)58384ec30aSotto xdr_array(XDR *xdrs,
59384ec30aSotto caddr_t *addrp, /* array pointer */
60384ec30aSotto u_int *sizep, /* number of elements */
61384ec30aSotto u_int maxsize, /* max numberof elements */
62384ec30aSotto u_int elsize, /* size in bytes of each element */
63384ec30aSotto xdrproc_t elproc) /* xdr routine to handle each element */
64df930be7Sderaadt {
6541aa8645Sderaadt caddr_t target = *addrp;
66162b7f13Sderaadt u_int nodesize, c, i;
6741aa8645Sderaadt bool_t stat = TRUE;
68df930be7Sderaadt
69df930be7Sderaadt /* like strings, arrays are really counted arrays */
70162b7f13Sderaadt if (!xdr_u_int(xdrs, sizep))
71df930be7Sderaadt return (FALSE);
72162b7f13Sderaadt
73df930be7Sderaadt c = *sizep;
74162b7f13Sderaadt if ((c > maxsize || c > UINT_MAX/elsize) &&
75162b7f13Sderaadt xdrs->x_op != XDR_FREE)
76df930be7Sderaadt return (FALSE);
77df930be7Sderaadt nodesize = c * elsize;
78df930be7Sderaadt
79df930be7Sderaadt /*
80df930be7Sderaadt * if we are deserializing, we may need to allocate an array.
81df930be7Sderaadt * We also save time by checking for a null array if we are freeing.
82df930be7Sderaadt */
83162b7f13Sderaadt if (target == NULL) {
84df930be7Sderaadt switch (xdrs->x_op) {
85df930be7Sderaadt case XDR_DECODE:
86df930be7Sderaadt if (c == 0)
87df930be7Sderaadt return (TRUE);
88df930be7Sderaadt *addrp = target = mem_alloc(nodesize);
89137e52c2Sderaadt if (target == NULL)
90df930be7Sderaadt return (FALSE);
91df930be7Sderaadt memset(target, 0, nodesize);
92df930be7Sderaadt break;
93df930be7Sderaadt case XDR_FREE:
94df930be7Sderaadt return (TRUE);
959313a659Skrw default:
969313a659Skrw break;
97df930be7Sderaadt }
98162b7f13Sderaadt }
99df930be7Sderaadt
100df930be7Sderaadt /*
101df930be7Sderaadt * now we xdr each element of array
102df930be7Sderaadt */
103df930be7Sderaadt for (i = 0; (i < c) && stat; i++) {
104df930be7Sderaadt stat = (*elproc)(xdrs, target);
105df930be7Sderaadt target += elsize;
106df930be7Sderaadt }
107df930be7Sderaadt
108df930be7Sderaadt /*
109df930be7Sderaadt * the array may need freeing
110df930be7Sderaadt */
111df930be7Sderaadt if (xdrs->x_op == XDR_FREE) {
112df930be7Sderaadt mem_free(*addrp, nodesize);
113df930be7Sderaadt *addrp = NULL;
114df930be7Sderaadt }
115df930be7Sderaadt return (stat);
116df930be7Sderaadt }
11785858ec2Sguenther DEF_WEAK(xdr_array);
118df930be7Sderaadt
119df930be7Sderaadt /*
120df930be7Sderaadt * xdr_vector():
121df930be7Sderaadt *
122df930be7Sderaadt * XDR a fixed length array. Unlike variable-length arrays,
123df930be7Sderaadt * the storage of fixed length arrays is static and unfreeable.
124df930be7Sderaadt * > basep: base of the array
125df930be7Sderaadt * > size: size of the array
126df930be7Sderaadt * > elemsize: size of each element
127df930be7Sderaadt * > xdr_elem: routine to XDR each element
128df930be7Sderaadt */
129df930be7Sderaadt bool_t
xdr_vector(XDR * xdrs,char * basep,u_int nelem,u_int elemsize,xdrproc_t xdr_elem)130384ec30aSotto xdr_vector(XDR *xdrs, char *basep, u_int nelem, u_int elemsize,
131384ec30aSotto xdrproc_t xdr_elem)
132df930be7Sderaadt {
13341aa8645Sderaadt char *elptr;
134162b7f13Sderaadt u_int i;
135df930be7Sderaadt
136df930be7Sderaadt elptr = basep;
137df930be7Sderaadt for (i = 0; i < nelem; i++) {
138162b7f13Sderaadt if (!(*xdr_elem)(xdrs, elptr))
139df930be7Sderaadt return(FALSE);
140df930be7Sderaadt elptr += elemsize;
141df930be7Sderaadt }
142df930be7Sderaadt return(TRUE);
143df930be7Sderaadt }
144