Blame sunrpc/xdr_array.c

Packit 6c4009
/*
Packit 6c4009
 * xdr_array.c, Generic XDR routines implementation.
Packit 6c4009
 *
Packit 6c4009
 * Copyright (c) 2010, Oracle America, Inc.
Packit 6c4009
 *
Packit 6c4009
 * Redistribution and use in source and binary forms, with or without
Packit 6c4009
 * modification, are permitted provided that the following conditions are
Packit 6c4009
 * met:
Packit 6c4009
 *
Packit 6c4009
 *     * Redistributions of source code must retain the above copyright
Packit 6c4009
 *       notice, this list of conditions and the following disclaimer.
Packit 6c4009
 *     * Redistributions in binary form must reproduce the above
Packit 6c4009
 *       copyright notice, this list of conditions and the following
Packit 6c4009
 *       disclaimer in the documentation and/or other materials
Packit 6c4009
 *       provided with the distribution.
Packit 6c4009
 *     * Neither the name of the "Oracle America, Inc." nor the names of its
Packit 6c4009
 *       contributors may be used to endorse or promote products derived
Packit 6c4009
 *       from this software without specific prior written permission.
Packit 6c4009
 *
Packit 6c4009
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 6c4009
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 6c4009
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit 6c4009
 *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit 6c4009
 *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
Packit 6c4009
 *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 6c4009
 *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
Packit 6c4009
 *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit 6c4009
 *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit 6c4009
 *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit 6c4009
 *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 6c4009
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 6c4009
 *
Packit 6c4009
 * These are the "non-trivial" xdr primitives used to serialize and
Packit 6c4009
 * de-serialize arrays.  See xdr.h for more info on the interface to xdr.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <rpc/types.h>
Packit 6c4009
#include <rpc/xdr.h>
Packit 6c4009
#include <libintl.h>
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <wchar.h>
Packit 6c4009
#include <shlib-compat.h>
Packit 6c4009
Packit 6c4009
#define LASTUNSIGNED	((u_int)0-1)
Packit 6c4009
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * XDR an array of arbitrary elements
Packit 6c4009
 * *addrp is a pointer to the array, *sizep is the number of elements.
Packit 6c4009
 * If addrp is NULL (*sizep * elsize) bytes are allocated.
Packit 6c4009
 * elsize is the size (in bytes) of each element, and elproc is the
Packit 6c4009
 * xdr procedure to call to handle each element of the array.
Packit 6c4009
 */
Packit 6c4009
bool_t
Packit 6c4009
xdr_array (XDR *xdrs,
Packit 6c4009
	   /* array pointer */
Packit 6c4009
	   caddr_t *addrp,
Packit 6c4009
	   /* number of elements */
Packit 6c4009
	   u_int *sizep,
Packit 6c4009
	   /* max numberof elements */
Packit 6c4009
	   u_int maxsize,
Packit 6c4009
	   /* size in bytes of each element */
Packit 6c4009
	   u_int elsize,
Packit 6c4009
	   /* xdr routine to handle each element */
Packit 6c4009
	   xdrproc_t elproc)
Packit 6c4009
{
Packit 6c4009
  u_int i;
Packit 6c4009
  caddr_t target = *addrp;
Packit 6c4009
  u_int c;		/* the actual element count */
Packit 6c4009
  bool_t stat = TRUE;
Packit 6c4009
Packit 6c4009
  /* like strings, arrays are really counted arrays */
Packit 6c4009
  if (!xdr_u_int (xdrs, sizep))
Packit 6c4009
    {
Packit 6c4009
      return FALSE;
Packit 6c4009
    }
Packit 6c4009
  c = *sizep;
Packit 6c4009
  /*
Packit 6c4009
   * XXX: Let the overflow possibly happen with XDR_FREE because mem_free()
Packit 6c4009
   * doesn't actually use its second argument anyway.
Packit 6c4009
   */
Packit 6c4009
  if ((c > maxsize || c > UINT_MAX / elsize) && (xdrs->x_op != XDR_FREE))
Packit 6c4009
    {
Packit 6c4009
      return FALSE;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /*
Packit 6c4009
   * if we are deserializing, we may need to allocate an array.
Packit 6c4009
   * We also save time by checking for a null array if we are freeing.
Packit 6c4009
   */
Packit 6c4009
  if (target == NULL)
Packit 6c4009
    switch (xdrs->x_op)
Packit 6c4009
      {
Packit 6c4009
      case XDR_DECODE:
Packit 6c4009
	if (c == 0)
Packit 6c4009
	  return TRUE;
Packit 6c4009
	*addrp = target = calloc (c, elsize);
Packit 6c4009
	if (target == NULL)
Packit 6c4009
	  {
Packit 6c4009
	    (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
Packit 6c4009
	    return FALSE;
Packit 6c4009
	  }
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      case XDR_FREE:
Packit 6c4009
	return TRUE;
Packit 6c4009
      default:
Packit 6c4009
	break;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  /*
Packit 6c4009
   * now we xdr each element of array
Packit 6c4009
   */
Packit 6c4009
  for (i = 0; (i < c) && stat; i++)
Packit 6c4009
    {
Packit 6c4009
      stat = (*elproc) (xdrs, target, LASTUNSIGNED);
Packit 6c4009
      target += elsize;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /*
Packit 6c4009
   * the array may need freeing
Packit 6c4009
   */
Packit 6c4009
  if (xdrs->x_op == XDR_FREE)
Packit 6c4009
    {
Packit 6c4009
      mem_free (*addrp, c * elsize);
Packit 6c4009
      *addrp = NULL;
Packit 6c4009
    }
Packit 6c4009
  return stat;
Packit 6c4009
}
Packit 6c4009
#ifdef EXPORT_RPC_SYMBOLS
Packit 6c4009
libc_hidden_def (xdr_array)
Packit 6c4009
#else
Packit 6c4009
libc_hidden_nolink_sunrpc (xdr_array, GLIBC_2_0)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * xdr_vector():
Packit 6c4009
 *
Packit 6c4009
 * XDR a fixed length array. Unlike variable-length arrays,
Packit 6c4009
 * the storage of fixed length arrays is static and unfreeable.
Packit 6c4009
 * > basep: base of the array
Packit 6c4009
 * > size: size of the array
Packit 6c4009
 * > elemsize: size of each element
Packit 6c4009
 * > xdr_elem: routine to XDR each element
Packit 6c4009
 */
Packit 6c4009
bool_t
Packit 6c4009
xdr_vector (XDR *xdrs, char *basep, u_int nelem, u_int elemsize,
Packit 6c4009
	    xdrproc_t xdr_elem)
Packit 6c4009
{
Packit 6c4009
  u_int i;
Packit 6c4009
  char *elptr;
Packit 6c4009
Packit 6c4009
  elptr = basep;
Packit 6c4009
  for (i = 0; i < nelem; i++)
Packit 6c4009
    {
Packit 6c4009
      if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
Packit 6c4009
	{
Packit 6c4009
	  return FALSE;
Packit 6c4009
	}
Packit 6c4009
      elptr += elemsize;
Packit 6c4009
    }
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
libc_hidden_nolink_sunrpc (xdr_vector, GLIBC_2_0)