00001 /* @(#)xdr_array.c 2.1 88/07/29 4.0 RPCSRC */ 00002 /* 00003 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 00004 * unrestricted use provided that this legend is included on all tape 00005 * media and as a part of the software program in whole or part. Users 00006 * may copy or modify Sun RPC without charge, but are not authorized 00007 * to license or distribute it to anyone else except as part of a product or 00008 * program developed by the user. 00009 * 00010 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 00011 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 00012 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 00013 * 00014 * Sun RPC is provided with no support and without any obligation on the 00015 * part of Sun Microsystems, Inc. to assist in its use, correction, 00016 * modification or enhancement. 00017 * 00018 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 00019 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 00020 * OR ANY PART THEREOF. 00021 * 00022 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 00023 * or profits or other special, indirect and consequential damages, even if 00024 * Sun has been advised of the possibility of such damages. 00025 * 00026 * Sun Microsystems, Inc. 00027 * 2550 Garcia Avenue 00028 * Mountain View, California 94043 00029 */ 00030 #if !defined(lint) && defined(SCCSIDS) 00031 static char sccsid[] = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro"; 00032 #endif 00033 00038 /* 00039 * xdr_array.c, Generic XDR routines impelmentation. 00040 * 00041 * Copyright (C) 1984, Sun Microsystems, Inc. 00042 * 00043 * These are the "non-trivial" xdr primitives used to serialize and de-serialize 00044 * arrays. See xdr.h for more info on the interface to xdr. 00045 */ 00046 00047 #include <stdio.h> 00048 00049 #include <rpc/types.h> 00050 #include <rpc/xdr.h> 00051 #include <string.h> /* CUBEOS: for bzero */ 00052 00053 00054 #define LASTUNSIGNED ((u_int)0-1) 00055 00056 00057 /* 00058 * XDR an array of arbitrary elements 00059 * *addrp is a pointer to the array, *sizep is the number of elements. 00060 * If addrp is NULL (*sizep * elsize) bytes are allocated. 00061 * elsize is the size (in bytes) of each element, and elproc is the 00062 * xdr procedure to call to handle each element of the array. 00063 */ 00064 bool_t 00065 xdr_array (xdrs, addrp, sizep, maxsize, elsize, elproc) 00066 register XDR *xdrs; 00067 caddr_t *addrp; /* array pointer */ 00068 u_int *sizep; /* number of elements */ 00069 u_int maxsize; /* max numberof elements */ 00070 u_int elsize; /* size in bytes of each element */ 00071 xdrproc_t elproc; /* xdr routine to handle each element */ 00072 { 00073 register u_int i; 00074 register caddr_t target = *addrp; 00075 register u_int c; /* the actual element count */ 00076 register bool_t stat = TRUE; 00077 register u_int nodesize; 00078 00079 /* like strings, arrays are really counted arrays */ 00080 if (!xdr_u_int (xdrs, sizep)) { 00081 return (FALSE); 00082 } 00083 c = *sizep; 00084 if ((c > maxsize) && (xdrs->x_op != XDR_FREE)) { 00085 return (FALSE); 00086 } 00087 nodesize = c * elsize; 00088 00089 /* 00090 * if we are deserializing, we may need to allocate an array. 00091 * We also save time by checking for a null array if we are freeing. 00092 */ 00093 if (target == NULL) 00094 switch (xdrs->x_op) { 00095 case XDR_DECODE: 00096 if (c == 0) 00097 return (TRUE); 00098 *addrp = target = mem_alloc (nodesize); 00099 if (target == NULL) { 00100 (void) fprintf (stderr, 00101 "xdr_array: out of memory\n"); 00102 return (FALSE); 00103 } 00104 bzero (target, nodesize); 00105 break; 00106 00107 case XDR_FREE: 00108 return (TRUE); 00109 } 00110 /* 00111 * now we xdr each element of array 00112 */ 00113 for (i = 0; (i < c) && stat; i++) { 00114 stat = (*elproc) (xdrs, target, LASTUNSIGNED); 00115 target += elsize; 00116 } 00117 00118 /* 00119 * the array may need freeing 00120 */ 00121 if (xdrs->x_op == XDR_FREE) { 00122 mem_free (*addrp, nodesize); 00123 *addrp = NULL; 00124 } 00125 return (stat); 00126 } 00127 00128 /* 00129 * xdr_vector(): 00130 * 00131 * XDR a fixed length array. Unlike variable-length arrays, 00132 * the storage of fixed length arrays is static and unfreeable. 00133 * > basep: base of the array 00134 * > size: size of the array 00135 * > elemsize: size of each element 00136 * > xdr_elem: routine to XDR each element 00137 */ 00138 bool_t 00139 xdr_vector (xdrs, basep, nelem, elemsize, xdr_elem) 00140 register XDR *xdrs; 00141 register char *basep; 00142 register u_int nelem; 00143 register u_int elemsize; 00144 register xdrproc_t xdr_elem; 00145 { 00146 register u_int i; 00147 register char *elptr; 00148 00149 elptr = basep; 00150 for (i = 0; i < nelem; i++) { 00151 if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED)) { 00152 return (FALSE); 00153 } 00154 elptr += elemsize; 00155 } 00156 return (TRUE); 00157 }