Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

xdr_arra.c

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

Generated on Sun Dec 2 15:56:54 2001 for rcslib by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001