Blame sunrpc/xdr_sizeof.c

Packit 6c4009
/*
Packit 6c4009
 * xdr_sizeof.c
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
 * General purpose routine to see how much space something will use
Packit 6c4009
 * when serialized using XDR.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <rpc/types.h>
Packit 6c4009
#include <rpc/xdr.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <shlib-compat.h>
Packit 6c4009
Packit 6c4009
/* ARGSUSED */
Packit 6c4009
static bool_t
Packit 6c4009
x_putlong (XDR *xdrs, const long *longp)
Packit 6c4009
{
Packit 6c4009
  xdrs->x_handy += BYTES_PER_XDR_UNIT;
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* ARGSUSED */
Packit 6c4009
static bool_t
Packit 6c4009
x_putbytes (XDR *xdrs, const char *bp, u_int len)
Packit 6c4009
{
Packit 6c4009
  xdrs->x_handy += len;
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static u_int
Packit 6c4009
x_getpostn (const XDR *xdrs)
Packit 6c4009
{
Packit 6c4009
  return xdrs->x_handy;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* ARGSUSED */
Packit 6c4009
static bool_t
Packit 6c4009
x_setpostn (XDR *xdrs, u_int len)
Packit 6c4009
{
Packit 6c4009
  /* This is not allowed */
Packit 6c4009
  return FALSE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int32_t *
Packit 6c4009
x_inline (XDR *xdrs, u_int len)
Packit 6c4009
{
Packit 6c4009
  if (len == 0)
Packit 6c4009
    return NULL;
Packit 6c4009
  if (xdrs->x_op != XDR_ENCODE)
Packit 6c4009
    return NULL;
Packit 6c4009
  if (len < (u_int) (long int) xdrs->x_base)
Packit 6c4009
    {
Packit 6c4009
      /* x_private was already allocated */
Packit 6c4009
      xdrs->x_handy += len;
Packit 6c4009
      return (int32_t *) xdrs->x_private;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* Free the earlier space and allocate new area */
Packit 6c4009
      free (xdrs->x_private);
Packit 6c4009
      if ((xdrs->x_private = (caddr_t) malloc (len)) == NULL)
Packit 6c4009
	{
Packit 6c4009
	  xdrs->x_base = 0;
Packit 6c4009
	  return NULL;
Packit 6c4009
	}
Packit 6c4009
      xdrs->x_base = (void *) (long) len;
Packit 6c4009
      xdrs->x_handy += len;
Packit 6c4009
      return (int32_t *) xdrs->x_private;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
harmless (void)
Packit 6c4009
{
Packit 6c4009
  /* Always return FALSE/NULL, as the case may be */
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
x_destroy (XDR *xdrs)
Packit 6c4009
{
Packit 6c4009
  xdrs->x_handy = 0;
Packit 6c4009
  xdrs->x_base = 0;
Packit 6c4009
  if (xdrs->x_private)
Packit 6c4009
    {
Packit 6c4009
      free (xdrs->x_private);
Packit 6c4009
      xdrs->x_private = NULL;
Packit 6c4009
    }
Packit 6c4009
  return;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static bool_t
Packit 6c4009
x_putint32 (XDR *xdrs, const int32_t *int32p)
Packit 6c4009
{
Packit 6c4009
  xdrs->x_handy += BYTES_PER_XDR_UNIT;
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
unsigned long
Packit 6c4009
xdr_sizeof (xdrproc_t func, void *data)
Packit 6c4009
{
Packit 6c4009
  XDR x;
Packit 6c4009
  struct xdr_ops ops;
Packit 6c4009
  bool_t stat;
Packit 6c4009
  /* to stop ANSI-C compiler from complaining */
Packit 6c4009
  typedef bool_t (*dummyfunc1) (XDR *, long *);
Packit 6c4009
  typedef bool_t (*dummyfunc2) (XDR *, caddr_t, u_int);
Packit 6c4009
  typedef bool_t (*dummyfunc3) (XDR *, int32_t *);
Packit 6c4009
Packit 6c4009
  ops.x_putlong = x_putlong;
Packit 6c4009
  ops.x_putbytes = x_putbytes;
Packit 6c4009
  ops.x_inline = x_inline;
Packit 6c4009
  ops.x_getpostn = x_getpostn;
Packit 6c4009
  ops.x_setpostn = x_setpostn;
Packit 6c4009
  ops.x_destroy = x_destroy;
Packit 6c4009
  ops.x_putint32 = x_putint32;
Packit 6c4009
Packit 6c4009
  /* the other harmless ones */
Packit 6c4009
  ops.x_getlong = (dummyfunc1) harmless;
Packit 6c4009
  ops.x_getbytes = (dummyfunc2) harmless;
Packit 6c4009
  ops.x_getint32 = (dummyfunc3) harmless;
Packit 6c4009
Packit 6c4009
  x.x_op = XDR_ENCODE;
Packit 6c4009
  x.x_ops = &ops;
Packit 6c4009
  x.x_handy = 0;
Packit 6c4009
  x.x_private = (caddr_t) NULL;
Packit 6c4009
  x.x_base = (caddr_t) 0;
Packit 6c4009
Packit 6c4009
  stat = func (&x, data);
Packit 6c4009
  free (x.x_private);
Packit 6c4009
  return stat == TRUE ? x.x_handy : 0;
Packit 6c4009
}
Packit 6c4009
#ifdef EXPORT_RPC_SYMBOLS
Packit 6c4009
libc_hidden_def (xdr_sizeof)
Packit 6c4009
#else
Packit 6c4009
libc_hidden_nolink_sunrpc (xdr_sizeof, GLIBC_2_1)
Packit 6c4009
#endif