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