Blame src/xdr_stdio.c

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