Blame sunrpc/xdr_stdio.c

Packit 6c4009
/*
Packit 6c4009
 * xdr_stdio.c, XDR implementation on standard i/o file.
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
 * This set of routines implements a XDR on a stdio stream.
Packit 6c4009
 * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
Packit 6c4009
 * from the stream.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <rpc/types.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <rpc/xdr.h>
Packit 6c4009
Packit 6c4009
#include <libio/iolibio.h>
Packit 6c4009
#include <shlib-compat.h>
Packit 6c4009
Packit 6c4009
#define fflush(s) _IO_fflush (s)
Packit 6c4009
#define fread(p, m, n, s) _IO_fread (p, m, n, s)
Packit 6c4009
#define ftell(s) _IO_ftell (s)
Packit 6c4009
#define fwrite(p, m, n, s) _IO_fwrite (p, m, n, s)
Packit 6c4009
Packit 6c4009
static bool_t xdrstdio_getlong (XDR *, long *);
Packit 6c4009
static bool_t xdrstdio_putlong (XDR *, const long *);
Packit 6c4009
static bool_t xdrstdio_getbytes (XDR *, caddr_t, u_int);
Packit 6c4009
static bool_t xdrstdio_putbytes (XDR *, const char *, u_int);
Packit 6c4009
static u_int xdrstdio_getpos (const XDR *);
Packit 6c4009
static bool_t xdrstdio_setpos (XDR *, u_int);
Packit 6c4009
static int32_t *xdrstdio_inline (XDR *, u_int);
Packit 6c4009
static void xdrstdio_destroy (XDR *);
Packit 6c4009
static bool_t xdrstdio_getint32 (XDR *, int32_t *);
Packit 6c4009
static bool_t xdrstdio_putint32 (XDR *, const int32_t *);
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Ops vector for stdio type XDR
Packit 6c4009
 */
Packit 6c4009
static const struct xdr_ops xdrstdio_ops =
Packit 6c4009
{
Packit 6c4009
  xdrstdio_getlong,		/* deserialize a long int */
Packit 6c4009
  xdrstdio_putlong,		/* serialize a long int */
Packit 6c4009
  xdrstdio_getbytes,		/* deserialize counted bytes */
Packit 6c4009
  xdrstdio_putbytes,		/* serialize counted bytes */
Packit 6c4009
  xdrstdio_getpos,		/* get offset in the stream */
Packit 6c4009
  xdrstdio_setpos,		/* set offset in the stream */
Packit 6c4009
  xdrstdio_inline,		/* prime stream for inline macros */
Packit 6c4009
  xdrstdio_destroy,		/* destroy stream */
Packit 6c4009
  xdrstdio_getint32,		/* deserialize a int */
Packit 6c4009
  xdrstdio_putint32		/* serialize a int */
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Initialize a stdio xdr stream.
Packit 6c4009
 * Sets the xdr stream handle xdrs for use on the stream file.
Packit 6c4009
 * Operation flag is set to op.
Packit 6c4009
 */
Packit 6c4009
void
Packit 6c4009
xdrstdio_create (XDR *xdrs, FILE *file, 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 *) &xdrstdio_ops;
Packit 6c4009
  xdrs->x_private = (caddr_t) file;
Packit 6c4009
  xdrs->x_handy = 0;
Packit 6c4009
  xdrs->x_base = 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Destroy a stdio xdr stream.
Packit 6c4009
 * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
Packit 6c4009
 */
Packit 6c4009
static void
Packit 6c4009
xdrstdio_destroy (XDR *xdrs)
Packit 6c4009
{
Packit 6c4009
  (void) fflush ((FILE *) xdrs->x_private);
Packit 6c4009
  /* xx should we close the file ?? */
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static bool_t
Packit 6c4009
xdrstdio_getlong (XDR *xdrs, long *lp)
Packit 6c4009
{
Packit 6c4009
  uint32_t mycopy;
Packit 6c4009
Packit 6c4009
  if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
Packit 6c4009
    return FALSE;
Packit 6c4009
  *lp = (long) ntohl (mycopy);
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static bool_t
Packit 6c4009
xdrstdio_putlong (XDR *xdrs, const long *lp)
Packit 6c4009
{
Packit 6c4009
  int32_t mycopy = htonl ((uint32_t) *lp);
Packit 6c4009
Packit 6c4009
  if (fwrite ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
Packit 6c4009
    return FALSE;
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static bool_t
Packit 6c4009
xdrstdio_getbytes (XDR *xdrs, const caddr_t addr, u_int len)
Packit 6c4009
{
Packit 6c4009
  if ((len != 0) && (fread (addr, (int) len, 1,
Packit 6c4009
			    (FILE *) xdrs->x_private) != 1))
Packit 6c4009
    return FALSE;
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static bool_t
Packit 6c4009
xdrstdio_putbytes (XDR *xdrs, const char *addr, u_int len)
Packit 6c4009
{
Packit 6c4009
  if ((len != 0) && (fwrite (addr, (int) len, 1,
Packit 6c4009
			     (FILE *) xdrs->x_private) != 1))
Packit 6c4009
    return FALSE;
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static u_int
Packit 6c4009
xdrstdio_getpos (const XDR *xdrs)
Packit 6c4009
{
Packit 6c4009
  return (u_int) ftell ((FILE *) xdrs->x_private);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static bool_t
Packit 6c4009
xdrstdio_setpos (XDR *xdrs, u_int pos)
Packit 6c4009
{
Packit 6c4009
  return fseek ((FILE *) xdrs->x_private, (long) pos, 0) < 0 ? FALSE : TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int32_t *
Packit 6c4009
xdrstdio_inline (XDR *xdrs, u_int len)
Packit 6c4009
{
Packit 6c4009
  /*
Packit 6c4009
   * Must do some work to implement this: must insure
Packit 6c4009
   * enough data in the underlying stdio buffer,
Packit 6c4009
   * that the buffer is aligned so that we can indirect through a
Packit 6c4009
   * long *, and stuff this pointer in xdrs->x_buf.  Doing
Packit 6c4009
   * a fread or fwrite to a scratch buffer would defeat
Packit 6c4009
   * most of the gains to be had here and require storage
Packit 6c4009
   * management on this buffer, so we don't do this.
Packit 6c4009
   */
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static bool_t
Packit 6c4009
xdrstdio_getint32 (XDR *xdrs, int32_t *ip)
Packit 6c4009
{
Packit 6c4009
  int32_t mycopy;
Packit 6c4009
Packit 6c4009
  if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
Packit 6c4009
    return FALSE;
Packit 6c4009
  *ip = ntohl (mycopy);
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static bool_t
Packit 6c4009
xdrstdio_putint32 (XDR *xdrs, const int32_t *ip)
Packit 6c4009
{
Packit 6c4009
  int32_t mycopy = htonl (*ip);
Packit 6c4009
Packit 6c4009
  ip = &mycopy;
Packit 6c4009
  if (fwrite ((caddr_t) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
Packit 6c4009
    return FALSE;
Packit 6c4009
  return TRUE;
Packit 6c4009
}
Packit 6c4009
#ifdef EXPORT_RPC_SYMBOLS
Packit 6c4009
libc_hidden_def (xdrstdio_create)
Packit 6c4009
#else
Packit 6c4009
libc_hidden_nolink_sunrpc (xdrstdio_create, GLIBC_2_0)
Packit 6c4009
#endif