Back to index

glibc  2.9
xdr_array.c
Go to the documentation of this file.
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 
00034 /*
00035  * xdr_array.c, Generic XDR routines implementation.
00036  *
00037  * Copyright (C) 1984, Sun Microsystems, Inc.
00038  *
00039  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
00040  * arrays.  See xdr.h for more info on the interface to xdr.
00041  */
00042 
00043 #include <stdio.h>
00044 #include <string.h>
00045 #include <rpc/types.h>
00046 #include <rpc/xdr.h>
00047 #include <libintl.h>
00048 #include <limits.h>
00049 
00050 #ifdef USE_IN_LIBIO
00051 # include <wchar.h>
00052 #endif
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      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   u_int i;
00074   caddr_t target = *addrp;
00075   u_int c;           /* the actual element count */
00076   bool_t stat = TRUE;
00077 
00078   /* like strings, arrays are really counted arrays */
00079   if (!INTUSE(xdr_u_int) (xdrs, sizep))
00080     {
00081       return FALSE;
00082     }
00083   c = *sizep;
00084   /*
00085    * XXX: Let the overflow possibly happen with XDR_FREE because mem_free()
00086    * doesn't actually use its second argument anyway.
00087    */
00088   if ((c > maxsize || c > UINT_MAX / elsize) && (xdrs->x_op != XDR_FREE))
00089     {
00090       return FALSE;
00091     }
00092 
00093   /*
00094    * if we are deserializing, we may need to allocate an array.
00095    * We also save time by checking for a null array if we are freeing.
00096    */
00097   if (target == NULL)
00098     switch (xdrs->x_op)
00099       {
00100       case XDR_DECODE:
00101        if (c == 0)
00102          return TRUE;
00103        *addrp = target = calloc (c, elsize);
00104        if (target == NULL)
00105          {
00106            (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
00107            return FALSE;
00108          }
00109        break;
00110 
00111       case XDR_FREE:
00112        return TRUE;
00113       default:
00114        break;
00115       }
00116 
00117   /*
00118    * now we xdr each element of array
00119    */
00120   for (i = 0; (i < c) && stat; i++)
00121     {
00122       stat = (*elproc) (xdrs, target, LASTUNSIGNED);
00123       target += elsize;
00124     }
00125 
00126   /*
00127    * the array may need freeing
00128    */
00129   if (xdrs->x_op == XDR_FREE)
00130     {
00131       mem_free (*addrp, c * elsize);
00132       *addrp = NULL;
00133     }
00134   return stat;
00135 }
00136 INTDEF(xdr_array)
00137 
00138 /*
00139  * xdr_vector():
00140  *
00141  * XDR a fixed length array. Unlike variable-length arrays,
00142  * the storage of fixed length arrays is static and unfreeable.
00143  * > basep: base of the array
00144  * > size: size of the array
00145  * > elemsize: size of each element
00146  * > xdr_elem: routine to XDR each element
00147  */
00148 bool_t
00149 xdr_vector (xdrs, basep, nelem, elemsize, xdr_elem)
00150      XDR *xdrs;
00151      char *basep;
00152      u_int nelem;
00153      u_int elemsize;
00154      xdrproc_t xdr_elem;
00155 {
00156   u_int i;
00157   char *elptr;
00158 
00159   elptr = basep;
00160   for (i = 0; i < nelem; i++)
00161     {
00162       if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
00163        {
00164          return FALSE;
00165        }
00166       elptr += elemsize;
00167     }
00168   return TRUE;
00169 }