1 /* 2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 3 * unrestricted use provided that this legend is included on all tape 4 * media and as a part of the software program in whole or part. Users 5 * may copy or modify Sun RPC without charge, but are not authorized 6 * to license or distribute it to anyone else except as part of a product or 7 * program developed by the user. 8 * 9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 10 * WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 12 * 13 * Sun RPC is provided with no support and without any obligation on the 14 * part of Sun Microsystems, Inc. to assist in its use, correction, 15 * modification or enhancement. 16 * 17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 19 * OR ANY PART THEREOF. 20 * 21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 22 * or profits or other special, indirect and consequential damages, even if 23 * Sun has been advised of the possibility of such damages. 24 * 25 * Sun Microsystems, Inc. 26 * 2550 Garcia Avenue 27 * Mountain View, California 94043 28 * 29 * @(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro 30 * @(#)xdr_array.c 2.1 88/07/29 4.0 RPCSRC 31 * $NetBSD: xdr_array.c,v 1.12 2000/01/22 22:19:18 mycroft Exp $ 32 * $FreeBSD: src/lib/libc/xdr/xdr_array.c,v 1.15 2004/10/16 06:32:43 obrien Exp $ 33 */ 34 35 /* 36 * xdr_array.c, Generic XDR routines impelmentation. 37 * 38 * Copyright (C) 1984, Sun Microsystems, Inc. 39 * 40 * These are the "non-trivial" xdr primitives used to serialize and de-serialize 41 * arrays. See xdr.h for more info on the interface to xdr. 42 */ 43 44 #include "namespace.h" 45 #include <err.h> 46 #include <limits.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 51 #include <rpc/types.h> 52 #include <rpc/xdr.h> 53 #include "un-namespace.h" 54 55 /* 56 * XDR an array of arbitrary elements 57 * *addrp is a pointer to the array, *sizep is the number of elements. 58 * If addrp is NULL (*sizep * elsize) bytes are allocated. 59 * elsize is the size (in bytes) of each element, and elproc is the 60 * xdr procedure to call to handle each element of the array. 61 * 62 * Parameters: 63 * addrp: array pointer 64 * sizep: number of elements 65 * maxsize: max number of elements 66 * elsize: size in bytes of each element 67 * elproc: xdr routine to handle each element 68 */ 69 bool_t 70 xdr_array(XDR *xdrs, caddr_t *addrp, u_int *sizep, u_int maxsize, u_int elsize, 71 xdrproc_t elproc) 72 { 73 u_int i; 74 caddr_t target = *addrp; 75 u_int c; /* the actual element count */ 76 bool_t stat = TRUE; 77 u_int nodesize; 78 79 /* like strings, arrays are really counted arrays */ 80 if (!xdr_u_int(xdrs, sizep)) { 81 return (FALSE); 82 } 83 c = *sizep; 84 if ((c > maxsize || UINT_MAX/elsize < c) && 85 (xdrs->x_op != XDR_FREE)) { 86 return (FALSE); 87 } 88 nodesize = c * elsize; 89 90 /* 91 * if we are deserializing, we may need to allocate an array. 92 * We also save time by checking for a null array if we are freeing. 93 */ 94 if (target == NULL) 95 switch (xdrs->x_op) { 96 case XDR_DECODE: 97 if (c == 0) 98 return (TRUE); 99 *addrp = target = mem_alloc(nodesize); 100 if (target == NULL) { 101 warnx("xdr_array: out of memory"); 102 return (FALSE); 103 } 104 memset(target, 0, nodesize); 105 break; 106 107 case XDR_FREE: 108 return (TRUE); 109 110 case XDR_ENCODE: 111 break; 112 } 113 114 /* 115 * now we xdr each element of array 116 */ 117 for (i = 0; (i < c) && stat; i++) { 118 stat = (*elproc)(xdrs, target); 119 target += elsize; 120 } 121 122 /* 123 * the array may need freeing 124 */ 125 if (xdrs->x_op == XDR_FREE) { 126 mem_free(*addrp, nodesize); 127 *addrp = NULL; 128 } 129 return (stat); 130 } 131 132 /* 133 * xdr_vector(): 134 * 135 * XDR a fixed length array. Unlike variable-length arrays, 136 * the storage of fixed length arrays is static and unfreeable. 137 * > basep: base of the array 138 * > size: size of the array 139 * > elemsize: size of each element 140 * > xdr_elem: routine to XDR each element 141 */ 142 bool_t 143 xdr_vector(XDR *xdrs, char *basep, u_int nelem, u_int elemsize, 144 xdrproc_t xdr_elem) 145 { 146 u_int i; 147 char *elptr; 148 149 elptr = basep; 150 for (i = 0; i < nelem; i++) { 151 if (!(*xdr_elem)(xdrs, elptr)) { 152 return(FALSE); 153 } 154 elptr += elemsize; 155 } 156 return(TRUE); 157 } 158