1226586Sdim/* $NetBSD: xdr_array.c,v 1.12 2000/01/22 22:19:18 mycroft Exp $ */ 2226586Sdim 3226586Sdim/*- 4226586Sdim * Copyright (c) 2010, Oracle America, Inc. 5226586Sdim * 6226586Sdim * Redistribution and use in source and binary forms, with or without 7226586Sdim * modification, are permitted provided that the following conditions are 8226586Sdim * met: 9226586Sdim * 10226586Sdim * * Redistributions of source code must retain the above copyright 11226586Sdim * notice, this list of conditions and the following disclaimer. 12226586Sdim * * Redistributions in binary form must reproduce the above 13226586Sdim * copyright notice, this list of conditions and the following 14226586Sdim * disclaimer in the documentation and/or other materials 15239462Sdim * provided with the distribution. 16226586Sdim * * Neither the name of the "Oracle America, Inc." nor the names of its 17226586Sdim * contributors may be used to endorse or promote products derived 18226586Sdim * from this software without specific prior written permission. 19234353Sdim * 20226586Sdim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21226586Sdim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22226586Sdim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23226586Sdim * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24226586Sdim * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 25234353Sdim * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26234353Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 27234353Sdim * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28234353Sdim * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29234353Sdim * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30234353Sdim * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31234353Sdim * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32234353Sdim */ 33234353Sdim 34234353Sdim#if defined(LIBC_SCCS) && !defined(lint) 35234353Sdimstatic char *sccsid2 = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro"; 36234353Sdimstatic char *sccsid = "@(#)xdr_array.c 2.1 88/07/29 4.0 RPCSRC"; 37234353Sdim#endif 38234353Sdim#include <sys/cdefs.h> 39234353Sdim__FBSDID("$FreeBSD$"); 40234353Sdim 41234353Sdim/* 42234353Sdim * xdr_array.c, Generic XDR routines impelmentation. 43234353Sdim * 44234353Sdim * These are the "non-trivial" xdr primitives used to serialize and de-serialize 45226586Sdim * arrays. See xdr.h for more info on the interface to xdr. 46226586Sdim */ 47226586Sdim 48226586Sdim#include "namespace.h" 49226586Sdim#include <err.h> 50226586Sdim#include <limits.h> 51226586Sdim#include <stdio.h> 52226586Sdim#include <stdlib.h> 53226586Sdim#include <string.h> 54226586Sdim 55226586Sdim#include <rpc/types.h> 56226586Sdim#include <rpc/xdr.h> 57226586Sdim#include "un-namespace.h" 58226586Sdim 59226586Sdim/* 60226586Sdim * XDR an array of arbitrary elements 61226586Sdim * *addrp is a pointer to the array, *sizep is the number of elements. 62226586Sdim * If addrp is NULL (*sizep * elsize) bytes are allocated. 63226586Sdim * elsize is the size (in bytes) of each element, and elproc is the 64226586Sdim * xdr procedure to call to handle each element of the array. 65226586Sdim */ 66226586Sdimbool_t 67226586Sdimxdr_array(xdrs, addrp, sizep, maxsize, elsize, elproc) 68226586Sdim XDR *xdrs; 69226586Sdim caddr_t *addrp; /* array pointer */ 70239462Sdim u_int *sizep; /* number of elements */ 71239462Sdim u_int maxsize; /* max numberof elements */ 72239462Sdim u_int elsize; /* size in bytes of each element */ 73239462Sdim xdrproc_t elproc; /* xdr routine to handle each element */ 74243830Sdim{ 75243830Sdim u_int i; 76239462Sdim caddr_t target = *addrp; 77239462Sdim u_int c; /* the actual element count */ 78239462Sdim bool_t stat = TRUE; 79239462Sdim u_int nodesize; 80239462Sdim 81239462Sdim /* like strings, arrays are really counted arrays */ 82239462Sdim if (!xdr_u_int(xdrs, sizep)) { 83226586Sdim return (FALSE); 84226586Sdim } 85226586Sdim c = *sizep; 86226586Sdim if ((c > maxsize || UINT_MAX/elsize < c) && 87226586Sdim (xdrs->x_op != XDR_FREE)) { 88226586Sdim return (FALSE); 89234353Sdim } 90234353Sdim nodesize = c * elsize; 91226586Sdim 92226586Sdim /* 93226586Sdim * if we are deserializing, we may need to allocate an array. 94226586Sdim * We also save time by checking for a null array if we are freeing. 95226586Sdim */ 96226586Sdim if (target == NULL) 97226586Sdim switch (xdrs->x_op) { 98226586Sdim case XDR_DECODE: 99226586Sdim if (c == 0) 100226586Sdim return (TRUE); 101226586Sdim *addrp = target = mem_alloc(nodesize); 102226586Sdim if (target == NULL) { 103226586Sdim warnx("xdr_array: out of memory"); 104226586Sdim return (FALSE); 105226586Sdim } 106226586Sdim memset(target, 0, nodesize); 107226586Sdim break; 108226586Sdim 109226586Sdim case XDR_FREE: 110243830Sdim return (TRUE); 111243830Sdim 112226586Sdim case XDR_ENCODE: 113226586Sdim break; 114234353Sdim } 115226586Sdim 116226586Sdim /* 117226586Sdim * now we xdr each element of array 118243830Sdim */ 119226586Sdim for (i = 0; (i < c) && stat; i++) { 120226586Sdim stat = (*elproc)(xdrs, target); 121226586Sdim target += elsize; 122243830Sdim } 123226586Sdim 124234353Sdim /* 125226586Sdim * the array may need freeing 126226586Sdim */ 127243830Sdim if (xdrs->x_op == XDR_FREE) { 128226586Sdim mem_free(*addrp, nodesize); 129226586Sdim *addrp = NULL; 130226586Sdim } 131226586Sdim return (stat); 132226586Sdim} 133234353Sdim 134226586Sdim/* 135226586Sdim * xdr_vector(): 136226586Sdim * 137234353Sdim * XDR a fixed length array. Unlike variable-length arrays, 138226586Sdim * the storage of fixed length arrays is static and unfreeable. 139226586Sdim * > basep: base of the array 140226586Sdim * > size: size of the array 141226586Sdim * > elemsize: size of each element 142226586Sdim * > xdr_elem: routine to XDR each element 143234353Sdim */ 144226586Sdimbool_t 145226586Sdimxdr_vector(xdrs, basep, nelem, elemsize, xdr_elem) 146234353Sdim XDR *xdrs; 147226586Sdim char *basep; 148239462Sdim u_int nelem; 149226586Sdim u_int elemsize; 150226586Sdim xdrproc_t xdr_elem; 151234353Sdim{ 152234353Sdim u_int i; 153226586Sdim char *elptr; 154234353Sdim 155226586Sdim elptr = basep; 156226586Sdim for (i = 0; i < nelem; i++) { 157234353Sdim if (!(*xdr_elem)(xdrs, elptr)) { 158226586Sdim return(FALSE); 159226586Sdim } 160234353Sdim elptr += elemsize; 161226586Sdim } 162239462Sdim return(TRUE); 163226586Sdim} 164226586Sdim