174462Salfred/* $NetBSD: xdr_array.c,v 1.12 2000/01/22 22:19:18 mycroft Exp $ */ 274462Salfred 3259118Shrs/*- 4259118Shrs * Copyright (c) 2010, Oracle America, Inc. 58870Srgrimes * 6259118Shrs * Redistribution and use in source and binary forms, with or without 7259118Shrs * modification, are permitted provided that the following conditions are 8259118Shrs * met: 98870Srgrimes * 10259118Shrs * * Redistributions of source code must retain the above copyright 11259118Shrs * notice, this list of conditions and the following disclaimer. 12259118Shrs * * Redistributions in binary form must reproduce the above 13259118Shrs * copyright notice, this list of conditions and the following 14259118Shrs * disclaimer in the documentation and/or other materials 15259118Shrs * provided with the distribution. 16259118Shrs * * Neither the name of the "Oracle America, Inc." nor the names of its 17259118Shrs * contributors may be used to endorse or promote products derived 18259118Shrs * from this software without specific prior written permission. 198870Srgrimes * 20259118Shrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21259118Shrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22259118Shrs * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23259118Shrs * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24259118Shrs * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 25259118Shrs * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26259118Shrs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 27259118Shrs * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28259118Shrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29259118Shrs * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30259118Shrs * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31259118Shrs * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 321902Swollman */ 331902Swollman 341902Swollman#if defined(LIBC_SCCS) && !defined(lint) 35136582Sobrienstatic char *sccsid2 = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro"; 3692986Sobrienstatic char *sccsid = "@(#)xdr_array.c 2.1 88/07/29 4.0 RPCSRC"; 371902Swollman#endif 3892986Sobrien#include <sys/cdefs.h> 3992986Sobrien__FBSDID("$FreeBSD$"); 401902Swollman 411902Swollman/* 421902Swollman * xdr_array.c, Generic XDR routines impelmentation. 431902Swollman * 441902Swollman * These are the "non-trivial" xdr primitives used to serialize and de-serialize 451902Swollman * arrays. See xdr.h for more info on the interface to xdr. 461902Swollman */ 471902Swollman 4874462Salfred#include "namespace.h" 4974462Salfred#include <err.h> 50101067Snectar#include <limits.h> 511902Swollman#include <stdio.h> 521902Swollman#include <stdlib.h> 5311669Sphk#include <string.h> 5474462Salfred 551902Swollman#include <rpc/types.h> 561902Swollman#include <rpc/xdr.h> 5774462Salfred#include "un-namespace.h" 581902Swollman 591902Swollman/* 601902Swollman * XDR an array of arbitrary elements 611902Swollman * *addrp is a pointer to the array, *sizep is the number of elements. 621902Swollman * If addrp is NULL (*sizep * elsize) bytes are allocated. 631902Swollman * elsize is the size (in bytes) of each element, and elproc is the 641902Swollman * xdr procedure to call to handle each element of the array. 651902Swollman */ 661902Swollmanbool_t 67283833Srodrigcxdr_array(XDR *xdrs, caddr_t *addrp, u_int *sizep, u_int maxsize, u_int elsize, xdrproc_t elproc) 68283833Srodrigc/* 69283833Srodrigc * XDR *xdrs; 70283833Srodrigc * caddr_t *addrp; // array pointer 71283833Srodrigc * u_int *sizep; // number of elements 72283833Srodrigc * u_int maxsize; // max numberof elements 73283833Srodrigc * u_int elsize; // size in bytes of each element 74283833Srodrigc * xdrproc_t elproc; // xdr routine to handle each element 75283833Srodrigc */ 761902Swollman{ 7774462Salfred u_int i; 7874462Salfred caddr_t target = *addrp; 7974462Salfred u_int c; /* the actual element count */ 8074462Salfred bool_t stat = TRUE; 8174462Salfred u_int nodesize; 821902Swollman 831902Swollman /* like strings, arrays are really counted arrays */ 84101045Sdarrenr if (!xdr_u_int(xdrs, sizep)) { 851902Swollman return (FALSE); 861902Swollman } 871902Swollman c = *sizep; 88101147Snectar if ((c > maxsize || UINT_MAX/elsize < c) && 89101045Sdarrenr (xdrs->x_op != XDR_FREE)) { 901902Swollman return (FALSE); 911902Swollman } 921902Swollman nodesize = c * elsize; 931902Swollman 941902Swollman /* 951902Swollman * if we are deserializing, we may need to allocate an array. 961902Swollman * We also save time by checking for a null array if we are freeing. 971902Swollman */ 981902Swollman if (target == NULL) 991902Swollman switch (xdrs->x_op) { 1001902Swollman case XDR_DECODE: 1011902Swollman if (c == 0) 1021902Swollman return (TRUE); 1031902Swollman *addrp = target = mem_alloc(nodesize); 1041902Swollman if (target == NULL) { 10574462Salfred warnx("xdr_array: out of memory"); 1061902Swollman return (FALSE); 1071902Swollman } 10821062Speter memset(target, 0, nodesize); 1091902Swollman break; 1101902Swollman 1111902Swollman case XDR_FREE: 1121902Swollman return (TRUE); 11374462Salfred 11474462Salfred case XDR_ENCODE: 11574462Salfred break; 1161902Swollman } 11774462Salfred 1181902Swollman /* 1191902Swollman * now we xdr each element of array 1201902Swollman */ 1211902Swollman for (i = 0; (i < c) && stat; i++) { 12274462Salfred stat = (*elproc)(xdrs, target); 1231902Swollman target += elsize; 1241902Swollman } 1251902Swollman 1261902Swollman /* 1271902Swollman * the array may need freeing 1281902Swollman */ 1291902Swollman if (xdrs->x_op == XDR_FREE) { 1301902Swollman mem_free(*addrp, nodesize); 1311902Swollman *addrp = NULL; 1321902Swollman } 1331902Swollman return (stat); 1341902Swollman} 1351902Swollman 1361902Swollman/* 1371902Swollman * xdr_vector(): 1381902Swollman * 1391902Swollman * XDR a fixed length array. Unlike variable-length arrays, 1401902Swollman * the storage of fixed length arrays is static and unfreeable. 1411902Swollman * > basep: base of the array 1421902Swollman * > size: size of the array 1431902Swollman * > elemsize: size of each element 1441902Swollman * > xdr_elem: routine to XDR each element 1451902Swollman */ 1461902Swollmanbool_t 147283833Srodrigcxdr_vector(XDR *xdrs, char *basep, u_int nelem, u_int elemsize, xdrproc_t xdr_elem) 1481902Swollman{ 14974462Salfred u_int i; 15074462Salfred char *elptr; 1511902Swollman 1521902Swollman elptr = basep; 1531902Swollman for (i = 0; i < nelem; i++) { 154101045Sdarrenr if (!(*xdr_elem)(xdrs, elptr)) { 1551902Swollman return(FALSE); 1561902Swollman } 1571902Swollman elptr += elemsize; 1581902Swollman } 15974462Salfred return(TRUE); 1601902Swollman} 161