xdr_array.c revision 74462
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 3274462Salfred#include <sys/cdefs.h> 331902Swollman#if defined(LIBC_SCCS) && !defined(lint) 341902Swollman/*static char *sccsid = "from: @(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";*/ 351902Swollman/*static char *sccsid = "from: @(#)xdr_array.c 2.1 88/07/29 4.0 RPCSRC";*/ 3650476Speterstatic char *rcsid = "$FreeBSD: head/lib/libc/xdr/xdr_array.c 74462 2001-03-19 12:50:13Z alfred $"; 371902Swollman#endif 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> 501902Swollman#include <stdio.h> 511902Swollman#include <stdlib.h> 5211669Sphk#include <string.h> 5374462Salfred 541902Swollman#include <rpc/types.h> 551902Swollman#include <rpc/xdr.h> 5674462Salfred#include "un-namespace.h" 571902Swollman 581902Swollman/* 591902Swollman * XDR an array of arbitrary elements 601902Swollman * *addrp is a pointer to the array, *sizep is the number of elements. 611902Swollman * If addrp is NULL (*sizep * elsize) bytes are allocated. 621902Swollman * elsize is the size (in bytes) of each element, and elproc is the 631902Swollman * xdr procedure to call to handle each element of the array. 641902Swollman */ 651902Swollmanbool_t 661902Swollmanxdr_array(xdrs, addrp, sizep, maxsize, elsize, elproc) 6774462Salfred XDR *xdrs; 681902Swollman caddr_t *addrp; /* array pointer */ 691902Swollman u_int *sizep; /* number of elements */ 701902Swollman u_int maxsize; /* max numberof elements */ 711902Swollman u_int elsize; /* size in bytes of each element */ 721902Swollman xdrproc_t elproc; /* xdr routine to handle each element */ 731902Swollman{ 7474462Salfred u_int i; 7574462Salfred caddr_t target = *addrp; 7674462Salfred u_int c; /* the actual element count */ 7774462Salfred bool_t stat = TRUE; 7874462Salfred u_int nodesize; 791902Swollman 801902Swollman /* like strings, arrays are really counted arrays */ 811902Swollman if (! xdr_u_int(xdrs, sizep)) { 821902Swollman return (FALSE); 831902Swollman } 841902Swollman c = *sizep; 851902Swollman if ((c > maxsize) && (xdrs->x_op != XDR_FREE)) { 861902Swollman return (FALSE); 871902Swollman } 881902Swollman nodesize = c * elsize; 891902Swollman 901902Swollman /* 911902Swollman * if we are deserializing, we may need to allocate an array. 921902Swollman * We also save time by checking for a null array if we are freeing. 931902Swollman */ 941902Swollman if (target == NULL) 951902Swollman switch (xdrs->x_op) { 961902Swollman case XDR_DECODE: 971902Swollman if (c == 0) 981902Swollman return (TRUE); 991902Swollman *addrp = target = mem_alloc(nodesize); 1001902Swollman if (target == NULL) { 10174462Salfred warnx("xdr_array: out of memory"); 1021902Swollman return (FALSE); 1031902Swollman } 10421062Speter memset(target, 0, nodesize); 1051902Swollman break; 1061902Swollman 1071902Swollman case XDR_FREE: 1081902Swollman return (TRUE); 10974462Salfred 11074462Salfred case XDR_ENCODE: 11174462Salfred break; 1121902Swollman } 11374462Salfred 1141902Swollman /* 1151902Swollman * now we xdr each element of array 1161902Swollman */ 1171902Swollman for (i = 0; (i < c) && stat; i++) { 11874462Salfred stat = (*elproc)(xdrs, target); 1191902Swollman target += elsize; 1201902Swollman } 1211902Swollman 1221902Swollman /* 1231902Swollman * the array may need freeing 1241902Swollman */ 1251902Swollman if (xdrs->x_op == XDR_FREE) { 1261902Swollman mem_free(*addrp, nodesize); 1271902Swollman *addrp = NULL; 1281902Swollman } 1291902Swollman return (stat); 1301902Swollman} 1311902Swollman 1321902Swollman/* 1331902Swollman * xdr_vector(): 1341902Swollman * 1351902Swollman * XDR a fixed length array. Unlike variable-length arrays, 1361902Swollman * the storage of fixed length arrays is static and unfreeable. 1371902Swollman * > basep: base of the array 1381902Swollman * > size: size of the array 1391902Swollman * > elemsize: size of each element 1401902Swollman * > xdr_elem: routine to XDR each element 1411902Swollman */ 1421902Swollmanbool_t 1431902Swollmanxdr_vector(xdrs, basep, nelem, elemsize, xdr_elem) 14474462Salfred XDR *xdrs; 14574462Salfred char *basep; 14674462Salfred u_int nelem; 14774462Salfred u_int elemsize; 14874462Salfred xdrproc_t xdr_elem; 1491902Swollman{ 15074462Salfred u_int i; 15174462Salfred char *elptr; 1521902Swollman 1531902Swollman elptr = basep; 1541902Swollman for (i = 0; i < nelem; i++) { 15574462Salfred if (! (*xdr_elem)(xdrs, elptr)) { 1561902Swollman return(FALSE); 1571902Swollman } 1581902Swollman elptr += elemsize; 1591902Swollman } 16074462Salfred return(TRUE); 1611902Swollman} 162