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