Blame sunrpc/xdr_mem.c

Packit 6c4009
/*
Packit 6c4009
 * xdr_mem.c, XDR implementation using memory buffers.
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
 * If you have some data to be interpreted as external data representation
Packit 6c4009
 * or to be converted to external data representation in a memory buffer,
Packit 6c4009
 * then this is the package for you.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <rpc/rpc.h>
Packit 6c4009
#include <shlib-compat.h>
Packit 6c4009
Packit 6c4009
static bool_t xdrmem_getlong (XDR *, long *);
Packit 6c4009
static bool_t xdrmem_putlong (XDR *, const long *);
Packit 6c4009
static bool_t xdrmem_getbytes (XDR *, caddr_t, u_int);
Packit 6c4009
static bool_t xdrmem_putbytes (XDR *, const char *, u_int);
Packit 6c4009
static u_int xdrmem_getpos (const XDR *);
Packit 6c4009
static bool_t xdrmem_setpos (XDR *, u_int);
Packit 6c4009
static int32_t *xdrmem_inline (XDR *, u_int);
Packit 6c4009
static void xdrmem_destroy (XDR *);
Packit 6c4009
static bool_t xdrmem_getint32 (XDR *, int32_t *);
Packit 6c4009
static bool_t xdrmem_putint32 (XDR *, const int32_t *);
Packit 6c4009
Packit 6c4009
static const struct xdr_ops xdrmem_ops =
Packit 6c4009
{
Packit 6c4009
  xdrmem_getlong,
Packit 6c4009
  xdrmem_putlong,
Packit 6c4009
  xdrmem_getbytes,
Packit 6c4009
  xdrmem_putbytes,
Packit 6c4009
  xdrmem_getpos,
Packit 6c4009
  xdrmem_setpos,
Packit 6c4009
  xdrmem_inline,
Packit 6c4009
  xdrmem_destroy,
Packit 6c4009
  xdrmem_getint32,
Packit 6c4009
  xdrmem_putint32
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * The procedure xdrmem_create initializes a stream descriptor for a
Packit 6c4009
 * memory buffer.
Packit 6c4009
 */
Packit 6c4009
void
Packit 6c4009
xdrmem_create (XDR *xdrs, const caddr_t addr, u_int size, enum xdr_op op)
Packit 6c4009
{
Packit 6c4009
  xdrs->x_op = op;
Packit 6c4009
  /* We have to add the const since the `struct xdr_ops' in `struct XDR'
Packit 6c4009
     is not `const'.  */
Packit 6c4009
  xdrs->x_ops = (struct xdr_ops *) &xdrmem_ops;
Packit 6c4009
  xdrs->x_private = xdrs->x_base = addr;
Packit 6c4009
  xdrs->x_handy = size;
Packit 6c4009
}
Packit 6c4009
#ifdef EXPORT_RPC_SYMBOLS
Packit 6c4009
libc_hidden_def (xdrmem_create)
Packit 6c4009
#else
Packit 6c4009
libc_hidden_nolink_sunrpc (xdrmem_create, GLIBC_2_0)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Nothing needs to be done for the memory case.  The argument is clearly
Packit 6c4009
 * const.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
xdrmem_destroy (XDR *xdrs)
Packit 6c4009
{
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Gets the next word from the memory referenced by xdrs and places it
Packit 6c4009
 * in the long pointed to by lp.  It then increments the private word to
Packit 6c4009
 * point at the next element.  Neither object pointed to is const
Packit 6c4009
 */
Packit 6c4009
static bool_t
Packit 6c4009
xdrmem_getlong (XDR *xdrs, long *lp)
Packit 6c4009
{
Packit 6c4009
  if (xdrs->x_handy < 4)
Packit 6c4009
    return FALSE;
Packit 6c4009
  xdrs->x_handy -= 4;
Packit 6c4009
  *lp = (int32_t) ntohl ((*((int32_t *) (xdrs->x_private))));
Packit 6c4009
  xdrs->x_private += 4;
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Puts the long pointed to by lp in the memory referenced by xdrs.  It
Packit 6c4009
 * then increments the private word to point at the next element.  The
Packit 6c4009
 * long pointed at is const
Packit 6c4009
 */
Packit 6c4009
static bool_t
Packit 6c4009
xdrmem_putlong (XDR *xdrs, const long *lp)
Packit 6c4009
{
Packit 6c4009
  if (xdrs->x_handy < 4)
Packit 6c4009
    return FALSE;
Packit 6c4009
  xdrs->x_handy -= 4;
Packit 6c4009
  *(int32_t *) xdrs->x_private = htonl (*lp);
Packit 6c4009
  xdrs->x_private += 4;
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Gets an unaligned number of bytes from the xdrs structure and writes them
Packit 6c4009
 * to the address passed in addr.  Be very careful when calling this routine
Packit 6c4009
 * as it could leave the xdrs pointing to an unaligned structure which is not
Packit 6c4009
 * a good idea.  None of the things pointed to are const.
Packit 6c4009
 */
Packit 6c4009
static bool_t
Packit 6c4009
xdrmem_getbytes (XDR *xdrs, caddr_t addr, u_int len)
Packit 6c4009
{
Packit 6c4009
  if (xdrs->x_handy < len)
Packit 6c4009
    return FALSE;
Packit 6c4009
  xdrs->x_handy -= len;
Packit 6c4009
  memcpy (addr, xdrs->x_private, len);
Packit 6c4009
  xdrs->x_private += len;
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * The complementary function to the above.  The same warnings apply about
Packit 6c4009
 * unaligned data.  The source address is const.
Packit 6c4009
 */
Packit 6c4009
static bool_t
Packit 6c4009
xdrmem_putbytes (XDR *xdrs, const char *addr, u_int len)
Packit 6c4009
{
Packit 6c4009
  if (xdrs->x_handy < len)
Packit 6c4009
    return FALSE;
Packit 6c4009
  xdrs->x_handy -= len;
Packit 6c4009
  memcpy (xdrs->x_private, addr, len);
Packit 6c4009
  xdrs->x_private += len;
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Not sure what this one does.  But it clearly doesn't modify the contents
Packit 6c4009
 * of xdrs.  **FIXME** does this not assume u_int == u_long?
Packit 6c4009
 */
Packit 6c4009
static u_int
Packit 6c4009
xdrmem_getpos (const XDR *xdrs)
Packit 6c4009
{
Packit 6c4009
  return (u_long) xdrs->x_private - (u_long) xdrs->x_base;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * xdrs modified
Packit 6c4009
 */
Packit 6c4009
static bool_t
Packit 6c4009
xdrmem_setpos (XDR *xdrs, u_int pos)
Packit 6c4009
{
Packit 6c4009
  caddr_t newaddr = xdrs->x_base + pos;
Packit 6c4009
  caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
Packit 6c4009
  size_t handy = lastaddr - newaddr;
Packit 6c4009
Packit 6c4009
  if (newaddr > lastaddr
Packit 6c4009
      || newaddr < xdrs->x_base
Packit 6c4009
      || handy != (u_int) handy)
Packit 6c4009
    return FALSE;
Packit 6c4009
Packit 6c4009
  xdrs->x_private = newaddr;
Packit 6c4009
  xdrs->x_handy = (u_int) handy;
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * xdrs modified
Packit 6c4009
 */
Packit 6c4009
static int32_t *
Packit 6c4009
xdrmem_inline (XDR *xdrs, u_int len)
Packit 6c4009
{
Packit 6c4009
  int32_t *buf = 0;
Packit 6c4009
Packit 6c4009
  if (xdrs->x_handy >= len)
Packit 6c4009
    {
Packit 6c4009
      xdrs->x_handy -= len;
Packit 6c4009
      buf = (int32_t *) xdrs->x_private;
Packit 6c4009
      xdrs->x_private += len;
Packit 6c4009
    }
Packit 6c4009
  return buf;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Gets the next word from the memory referenced by xdrs and places it
Packit 6c4009
 * in the int pointed to by ip.  It then increments the private word to
Packit 6c4009
 * point at the next element.  Neither object pointed to is const
Packit 6c4009
 */
Packit 6c4009
static bool_t
Packit 6c4009
xdrmem_getint32 (XDR *xdrs, int32_t *ip)
Packit 6c4009
{
Packit 6c4009
  if (xdrs->x_handy < 4)
Packit 6c4009
    return FALSE;
Packit 6c4009
  xdrs->x_handy -= 4;
Packit 6c4009
  *ip = ntohl ((*((int32_t *) (xdrs->x_private))));
Packit 6c4009
  xdrs->x_private += 4;
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Puts the long pointed to by lp in the memory referenced by xdrs.  It
Packit 6c4009
 * then increments the private word to point at the next element.  The
Packit 6c4009
 * long pointed at is const
Packit 6c4009
 */
Packit 6c4009
static bool_t
Packit 6c4009
xdrmem_putint32 (XDR *xdrs, const int32_t *ip)
Packit 6c4009
{
Packit 6c4009
  if (xdrs->x_handy < 4)
Packit 6c4009
    return FALSE;
Packit 6c4009
  xdrs->x_handy -= 4;
Packit 6c4009
  *(int32_t *) xdrs->x_private = htonl (*ip);
Packit 6c4009
  xdrs->x_private += 4;
Packit 6c4009
  return TRUE;
Packit 6c4009
}