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