Blame sunrpc/xdr_ref.c

Packit 6c4009
/*
Packit 6c4009
 * xdr_reference.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 "pointers".  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 <wchar.h>
Packit 6c4009
#include <libio/iolibio.h>
Packit 6c4009
#include <shlib-compat.h>
Packit 6c4009
Packit 6c4009
#define LASTUNSIGNED	((u_int)0-1)
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * XDR an indirect pointer
Packit 6c4009
 * xdr_reference is for recursively translating a structure that is
Packit 6c4009
 * referenced by a pointer inside the structure that is currently being
Packit 6c4009
 * translated.  pp references a pointer to storage. If *pp is null
Packit 6c4009
 * the  necessary storage is allocated.
Packit 6c4009
 * size is the size of the referneced structure.
Packit 6c4009
 * proc is the routine to handle the referenced structure.
Packit 6c4009
 */
Packit 6c4009
bool_t
Packit 6c4009
xdr_reference (XDR *xdrs,
Packit 6c4009
	       /* the pointer to work on */
Packit 6c4009
	       caddr_t *pp,
Packit 6c4009
	       /* size of the object pointed to */
Packit 6c4009
	       u_int size,
Packit 6c4009
	       /* xdr routine to handle the object */
Packit 6c4009
	       xdrproc_t proc)
Packit 6c4009
{
Packit 6c4009
  caddr_t loc = *pp;
Packit 6c4009
  bool_t stat;
Packit 6c4009
Packit 6c4009
  if (loc == NULL)
Packit 6c4009
    switch (xdrs->x_op)
Packit 6c4009
      {
Packit 6c4009
      case XDR_FREE:
Packit 6c4009
	return TRUE;
Packit 6c4009
Packit 6c4009
      case XDR_DECODE:
Packit 6c4009
	*pp = loc = (caddr_t) calloc (1, size);
Packit 6c4009
	if (loc == 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
      default:
Packit 6c4009
	break;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  stat = (*proc) (xdrs, loc, LASTUNSIGNED);
Packit 6c4009
Packit 6c4009
  if (xdrs->x_op == XDR_FREE)
Packit 6c4009
    {
Packit 6c4009
      mem_free (loc, size);
Packit 6c4009
      *pp = NULL;
Packit 6c4009
    }
Packit 6c4009
  return stat;
Packit 6c4009
}
Packit 6c4009
libc_hidden_nolink_sunrpc (xdr_reference, GLIBC_2_0)
Packit 6c4009
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * xdr_pointer():
Packit 6c4009
 *
Packit 6c4009
 * XDR a pointer to a possibly recursive data structure. This
Packit 6c4009
 * differs with xdr_reference in that it can serialize/deserialize
Packit 6c4009
 * trees correctly.
Packit 6c4009
 *
Packit 6c4009
 *  What's sent is actually a union:
Packit 6c4009
 *
Packit 6c4009
 *  union object_pointer switch (boolean b) {
Packit 6c4009
 *  case TRUE: object_data data;
Packit 6c4009
 *  case FALSE: void nothing;
Packit 6c4009
 *  }
Packit 6c4009
 *
Packit 6c4009
 * > objpp: Pointer to the pointer to the object.
Packit 6c4009
 * > obj_size: size of the object.
Packit 6c4009
 * > xdr_obj: routine to XDR an object.
Packit 6c4009
 *
Packit 6c4009
 */
Packit 6c4009
bool_t
Packit 6c4009
xdr_pointer (XDR *xdrs, char **objpp, u_int obj_size, xdrproc_t xdr_obj)
Packit 6c4009
{
Packit 6c4009
Packit 6c4009
  bool_t more_data;
Packit 6c4009
Packit 6c4009
  more_data = (*objpp != NULL);
Packit 6c4009
  if (!xdr_bool (xdrs, &more_data))
Packit 6c4009
    {
Packit 6c4009
      return FALSE;
Packit 6c4009
    }
Packit 6c4009
  if (!more_data)
Packit 6c4009
    {
Packit 6c4009
      *objpp = NULL;
Packit 6c4009
      return TRUE;
Packit 6c4009
    }
Packit 6c4009
  return xdr_reference (xdrs, objpp, obj_size, xdr_obj);
Packit 6c4009
}
Packit 6c4009
#ifdef EXPORT_RPC_SYMBOLS
Packit 6c4009
libc_hidden_def (xdr_pointer)
Packit 6c4009
#else
Packit 6c4009
libc_hidden_nolink_sunrpc (xdr_pointer, GLIBC_2_0)
Packit 6c4009
#endif