xdr_array.c revision 101147
174462Salfred/* $NetBSD: xdr_array.c,v 1.12 2000/01/22 22:19:18 mycroft Exp $ */ 274462Salfred 31902Swollman/* 41902Swollman * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 51902Swollman * unrestricted use provided that this legend is included on all tape 61902Swollman * media and as a part of the software program in whole or part. Users 71902Swollman * may copy or modify Sun RPC without charge, but are not authorized 81902Swollman * to license or distribute it to anyone else except as part of a product or 91902Swollman * program developed by the user. 108870Srgrimes * 111902Swollman * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 121902Swollman * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 131902Swollman * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 148870Srgrimes * 151902Swollman * Sun RPC is provided with no support and without any obligation on the 161902Swollman * part of Sun Microsystems, Inc. to assist in its use, correction, 171902Swollman * modification or enhancement. 188870Srgrimes * 191902Swollman * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 201902Swollman * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 211902Swollman * OR ANY PART THEREOF. 228870Srgrimes * 231902Swollman * In no event will Sun Microsystems, Inc. be liable for any lost revenue 241902Swollman * or profits or other special, indirect and consequential damages, even if 251902Swollman * Sun has been advised of the possibility of such damages. 268870Srgrimes * 271902Swollman * Sun Microsystems, Inc. 281902Swollman * 2550 Garcia Avenue 291902Swollman * Mountain View, California 94043 301902Swollman */ 311902Swollman 321902Swollman#if defined(LIBC_SCCS) && !defined(lint) 3392986Sobrienstatic char *sccsid = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro"; 3492986Sobrienstatic char *sccsid = "@(#)xdr_array.c 2.1 88/07/29 4.0 RPCSRC"; 351902Swollman#endif 3692986Sobrien#include <sys/cdefs.h> 3792986Sobrien__FBSDID("$FreeBSD: head/lib/libc/xdr/xdr_array.c 101147 2002-08-01 12:23:04Z nectar $"); 381902Swollman 391902Swollman/* 401902Swollman * xdr_array.c, Generic XDR routines impelmentation. 411902Swollman * 421902Swollman * Copyright (C) 1984, Sun Microsystems, Inc. 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 671902Swollmanxdr_array(xdrs, addrp, sizep, maxsize, elsize, elproc) 6874462Salfred XDR *xdrs; 691902Swollman caddr_t *addrp; /* array pointer */ 701902Swollman u_int *sizep; /* number of elements */ 711902Swollman u_int maxsize; /* max numberof elements */ 721902Swollman u_int elsize; /* size in bytes of each element */ 731902Swollman xdrproc_t elproc; /* xdr routine to handle each element */ 741902Swollman{ 7574462Salfred u_int i; 7674462Salfred caddr_t target = *addrp; 7774462Salfred u_int c; /* the actual element count */ 7874462Salfred bool_t stat = TRUE; 7974462Salfred u_int nodesize; 801902Swollman 811902Swollman /* like strings, arrays are really counted arrays */ 82101045Sdarrenr if (!xdr_u_int(xdrs, sizep)) { 831902Swollman return (FALSE); 841902Swollman } 851902Swollman c = *sizep; 86101147Snectar if ((c > maxsize || UINT_MAX/elsize < c) && 87101045Sdarrenr (xdrs->x_op != XDR_FREE)) { 881902Swollman return (FALSE); 891902Swollman } 901902Swollman nodesize = c * elsize; 911902Swollman 921902Swollman /* 931902Swollman * if we are deserializing, we may need to allocate an array. 941902Swollman * We also save time by checking for a null array if we are freeing. 951902Swollman */ 961902Swollman if (target == NULL) 971902Swollman switch (xdrs->x_op) { 981902Swollman case XDR_DECODE: 991902Swollman if (c == 0) 1001902Swollman return (TRUE); 1011902Swollman *addrp = target = mem_alloc(nodesize); 1021902Swollman if (target == NULL) { 10374462Salfred warnx("xdr_array: out of memory"); 1041902Swollman return (FALSE); 1051902Swollman } 10621062Speter memset(target, 0, nodesize); 1071902Swollman break; 1081902Swollman 1091902Swollman case XDR_FREE: 1101902Swollman return (TRUE); 11174462Salfred 11274462Salfred case XDR_ENCODE: 11374462Salfred break; 1141902Swollman } 11574462Salfred 1161902Swollman /* 1171902Swollman * now we xdr each element of array 1181902Swollman */ 1191902Swollman for (i = 0; (i < c) && stat; i++) { 12074462Salfred stat = (*elproc)(xdrs, target); 1211902Swollman target += elsize; 1221902Swollman } 1231902Swollman 1241902Swollman /* 1251902Swollman * the array may need freeing 1261902Swollman */ 1271902Swollman if (xdrs->x_op == XDR_FREE) { 1281902Swollman mem_free(*addrp, nodesize); 1291902Swollman *addrp = NULL; 1301902Swollman } 1311902Swollman return (stat); 1321902Swollman} 1331902Swollman 1341902Swollman/* 1351902Swollman * xdr_vector(): 1361902Swollman * 1371902Swollman * XDR a fixed length array. Unlike variable-length arrays, 1381902Swollman * the storage of fixed length arrays is static and unfreeable. 1391902Swollman * > basep: base of the array 1401902Swollman * > size: size of the array 1411902Swollman * > elemsize: size of each element 1421902Swollman * > xdr_elem: routine to XDR each element 1431902Swollman */ 1441902Swollmanbool_t 1451902Swollmanxdr_vector(xdrs, basep, nelem, elemsize, xdr_elem) 14674462Salfred XDR *xdrs; 14774462Salfred char *basep; 14874462Salfred u_int nelem; 14974462Salfred u_int elemsize; 15074462Salfred xdrproc_t xdr_elem; 1511902Swollman{ 15274462Salfred u_int i; 15374462Salfred char *elptr; 1541902Swollman 1551902Swollman elptr = basep; 1561902Swollman for (i = 0; i < nelem; i++) { 157101045Sdarrenr if (!(*xdr_elem)(xdrs, elptr)) { 1581902Swollman return(FALSE); 1591902Swollman } 1601902Swollman elptr += elemsize; 1611902Swollman } 16274462Salfred return(TRUE); 1631902Swollman} 164