Blame sunrpc/xdr_stdio.c

Packit Service 82fcde
/*
Packit Service 82fcde
 * xdr_stdio.c, XDR implementation on standard i/o file.
Packit Service 82fcde
 *
Packit Service 82fcde
 * Copyright (c) 2010, Oracle America, Inc.
Packit Service 82fcde
 *
Packit Service 82fcde
 * Redistribution and use in source and binary forms, with or without
Packit Service 82fcde
 * modification, are permitted provided that the following conditions are
Packit Service 82fcde
 * met:
Packit Service 82fcde
 *
Packit Service 82fcde
 *     * Redistributions of source code must retain the above copyright
Packit Service 82fcde
 *       notice, this list of conditions and the following disclaimer.
Packit Service 82fcde
 *     * Redistributions in binary form must reproduce the above
Packit Service 82fcde
 *       copyright notice, this list of conditions and the following
Packit Service 82fcde
 *       disclaimer in the documentation and/or other materials
Packit Service 82fcde
 *       provided with the distribution.
Packit Service 82fcde
 *     * Neither the name of the "Oracle America, Inc." nor the names of its
Packit Service 82fcde
 *       contributors may be used to endorse or promote products derived
Packit Service 82fcde
 *       from this software without specific prior written permission.
Packit Service 82fcde
 *
Packit Service 82fcde
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit Service 82fcde
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit Service 82fcde
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit Service 82fcde
 *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit Service 82fcde
 *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
Packit Service 82fcde
 *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit Service 82fcde
 *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
Packit Service 82fcde
 *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit Service 82fcde
 *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit Service 82fcde
 *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit Service 82fcde
 *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit Service 82fcde
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 82fcde
 *
Packit Service 82fcde
 * This set of routines implements a XDR on a stdio stream.
Packit Service 82fcde
 * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
Packit Service 82fcde
 * from the stream.
Packit Service 82fcde
 */
Packit Service 82fcde
Packit Service 82fcde
#include <rpc/types.h>
Packit Service 82fcde
#include <stdio.h>
Packit Service 82fcde
#include <rpc/xdr.h>
Packit Service 82fcde
Packit Service 82fcde
#include <libio/iolibio.h>
Packit Service 82fcde
#include <shlib-compat.h>
Packit Service 82fcde
Packit Service 82fcde
#define fflush(s) _IO_fflush (s)
Packit Service 82fcde
#define fread(p, m, n, s) _IO_fread (p, m, n, s)
Packit Service 82fcde
#define ftell(s) _IO_ftell (s)
Packit Service 82fcde
#define fwrite(p, m, n, s) _IO_fwrite (p, m, n, s)
Packit Service 82fcde
Packit Service 82fcde
static bool_t xdrstdio_getlong (XDR *, long *);
Packit Service 82fcde
static bool_t xdrstdio_putlong (XDR *, const long *);
Packit Service 82fcde
static bool_t xdrstdio_getbytes (XDR *, caddr_t, u_int);
Packit Service 82fcde
static bool_t xdrstdio_putbytes (XDR *, const char *, u_int);
Packit Service 82fcde
static u_int xdrstdio_getpos (const XDR *);
Packit Service 82fcde
static bool_t xdrstdio_setpos (XDR *, u_int);
Packit Service 82fcde
static int32_t *xdrstdio_inline (XDR *, u_int);
Packit Service 82fcde
static void xdrstdio_destroy (XDR *);
Packit Service 82fcde
static bool_t xdrstdio_getint32 (XDR *, int32_t *);
Packit Service 82fcde
static bool_t xdrstdio_putint32 (XDR *, const int32_t *);
Packit Service 82fcde
Packit Service 82fcde
/*
Packit Service 82fcde
 * Ops vector for stdio type XDR
Packit Service 82fcde
 */
Packit Service 82fcde
static const struct xdr_ops xdrstdio_ops =
Packit Service 82fcde
{
Packit Service 82fcde
  xdrstdio_getlong,		/* deserialize a long int */
Packit Service 82fcde
  xdrstdio_putlong,		/* serialize a long int */
Packit Service 82fcde
  xdrstdio_getbytes,		/* deserialize counted bytes */
Packit Service 82fcde
  xdrstdio_putbytes,		/* serialize counted bytes */
Packit Service 82fcde
  xdrstdio_getpos,		/* get offset in the stream */
Packit Service 82fcde
  xdrstdio_setpos,		/* set offset in the stream */
Packit Service 82fcde
  xdrstdio_inline,		/* prime stream for inline macros */
Packit Service 82fcde
  xdrstdio_destroy,		/* destroy stream */
Packit Service 82fcde
  xdrstdio_getint32,		/* deserialize a int */
Packit Service 82fcde
  xdrstdio_putint32		/* serialize a int */
Packit Service 82fcde
};
Packit Service 82fcde
Packit Service 82fcde
/*
Packit Service 82fcde
 * Initialize a stdio xdr stream.
Packit Service 82fcde
 * Sets the xdr stream handle xdrs for use on the stream file.
Packit Service 82fcde
 * Operation flag is set to op.
Packit Service 82fcde
 */
Packit Service 82fcde
void
Packit Service 82fcde
xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
Packit Service 82fcde
{
Packit Service 82fcde
  xdrs->x_op = op;
Packit Service 82fcde
  /* We have to add the const since the `struct xdr_ops' in `struct XDR'
Packit Service 82fcde
     is not `const'.  */
Packit Service 82fcde
  xdrs->x_ops = (struct xdr_ops *) &xdrstdio_ops;
Packit Service 82fcde
  xdrs->x_private = (caddr_t) file;
Packit Service 82fcde
  xdrs->x_handy = 0;
Packit Service 82fcde
  xdrs->x_base = 0;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/*
Packit Service 82fcde
 * Destroy a stdio xdr stream.
Packit Service 82fcde
 * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
Packit Service 82fcde
 */
Packit Service 82fcde
static void
Packit Service 82fcde
xdrstdio_destroy (XDR *xdrs)
Packit Service 82fcde
{
Packit Service 82fcde
  (void) fflush ((FILE *) xdrs->x_private);
Packit Service 82fcde
  /* xx should we close the file ?? */
Packit Service 82fcde
};
Packit Service 82fcde
Packit Service 82fcde
static bool_t
Packit Service 82fcde
xdrstdio_getlong (XDR *xdrs, long *lp)
Packit Service 82fcde
{
Packit Service 82fcde
  uint32_t mycopy;
Packit Service 82fcde
Packit Service 82fcde
  if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
Packit Service 82fcde
    return FALSE;
Packit Service 82fcde
  *lp = (long) ntohl (mycopy);
Packit Service 82fcde
  return TRUE;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static bool_t
Packit Service 82fcde
xdrstdio_putlong (XDR *xdrs, const long *lp)
Packit Service 82fcde
{
Packit Service 82fcde
  int32_t mycopy = htonl ((uint32_t) *lp);
Packit Service 82fcde
Packit Service 82fcde
  if (fwrite ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
Packit Service 82fcde
    return FALSE;
Packit Service 82fcde
  return TRUE;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static bool_t
Packit Service 82fcde
xdrstdio_getbytes (XDR *xdrs, const caddr_t addr, u_int len)
Packit Service 82fcde
{
Packit Service 82fcde
  if ((len != 0) && (fread (addr, (int) len, 1,
Packit Service 82fcde
			    (FILE *) xdrs->x_private) != 1))
Packit Service 82fcde
    return FALSE;
Packit Service 82fcde
  return TRUE;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static bool_t
Packit Service 82fcde
xdrstdio_putbytes (XDR *xdrs, const char *addr, u_int len)
Packit Service 82fcde
{
Packit Service 82fcde
  if ((len != 0) && (fwrite (addr, (int) len, 1,
Packit Service 82fcde
			     (FILE *) xdrs->x_private) != 1))
Packit Service 82fcde
    return FALSE;
Packit Service 82fcde
  return TRUE;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static u_int
Packit Service 82fcde
xdrstdio_getpos (const XDR *xdrs)
Packit Service 82fcde
{
Packit Service 82fcde
  return (u_int) ftell ((FILE *) xdrs->x_private);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static bool_t
Packit Service 82fcde
xdrstdio_setpos (XDR *xdrs, u_int pos)
Packit Service 82fcde
{
Packit Service 82fcde
  return fseek ((FILE *) xdrs->x_private, (long) pos, 0) < 0 ? FALSE : TRUE;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static int32_t *
Packit Service 82fcde
xdrstdio_inline (XDR *xdrs, u_int len)
Packit Service 82fcde
{
Packit Service 82fcde
  /*
Packit Service 82fcde
   * Must do some work to implement this: must insure
Packit Service 82fcde
   * enough data in the underlying stdio buffer,
Packit Service 82fcde
   * that the buffer is aligned so that we can indirect through a
Packit Service 82fcde
   * long *, and stuff this pointer in xdrs->x_buf.  Doing
Packit Service 82fcde
   * a fread or fwrite to a scratch buffer would defeat
Packit Service 82fcde
   * most of the gains to be had here and require storage
Packit Service 82fcde
   * management on this buffer, so we don't do this.
Packit Service 82fcde
   */
Packit Service 82fcde
  return NULL;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static bool_t
Packit Service 82fcde
xdrstdio_getint32 (XDR *xdrs, int32_t *ip)
Packit Service 82fcde
{
Packit Service 82fcde
  int32_t mycopy;
Packit Service 82fcde
Packit Service 82fcde
  if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
Packit Service 82fcde
    return FALSE;
Packit Service 82fcde
  *ip = ntohl (mycopy);
Packit Service 82fcde
  return TRUE;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static bool_t
Packit Service 82fcde
xdrstdio_putint32 (XDR *xdrs, const int32_t *ip)
Packit Service 82fcde
{
Packit Service 82fcde
  int32_t mycopy = htonl (*ip);
Packit Service 82fcde
Packit Service 82fcde
  ip = &mycopy;
Packit Service 82fcde
  if (fwrite ((caddr_t) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
Packit Service 82fcde
    return FALSE;
Packit Service 82fcde
  return TRUE;
Packit Service 82fcde
}
Packit Service 82fcde
#ifdef EXPORT_RPC_SYMBOLS
Packit Service 82fcde
libc_hidden_def (xdrstdio_create)
Packit Service 82fcde
#else
Packit Service 82fcde
libc_hidden_nolink_sunrpc (xdrstdio_create, GLIBC_2_0)
Packit Service 82fcde
#endif