Home | History | Annotate | Line # | Download | only in rpc
xdr_stdio.c revision 1.17.44.1
      1  1.17.44.1       riz /*	$NetBSD: xdr_stdio.c,v 1.17.44.1 2013/03/14 22:03:14 riz Exp $	*/
      2        1.3       cgd 
      3        1.1       cgd /*
      4  1.17.44.1       riz  * Copyright (c) 2010, Oracle America, Inc.
      5  1.17.44.1       riz  *
      6  1.17.44.1       riz  * Redistribution and use in source and binary forms, with or without
      7  1.17.44.1       riz  * modification, are permitted provided that the following conditions are
      8  1.17.44.1       riz  * met:
      9  1.17.44.1       riz  *
     10  1.17.44.1       riz  *     * Redistributions of source code must retain the above copyright
     11  1.17.44.1       riz  *       notice, this list of conditions and the following disclaimer.
     12  1.17.44.1       riz  *     * Redistributions in binary form must reproduce the above
     13  1.17.44.1       riz  *       copyright notice, this list of conditions and the following
     14  1.17.44.1       riz  *       disclaimer in the documentation and/or other materials
     15  1.17.44.1       riz  *       provided with the distribution.
     16  1.17.44.1       riz  *     * Neither the name of the "Oracle America, Inc." nor the names of its
     17  1.17.44.1       riz  *       contributors may be used to endorse or promote products derived
     18  1.17.44.1       riz  *       from this software without specific prior written permission.
     19  1.17.44.1       riz  *
     20  1.17.44.1       riz  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  1.17.44.1       riz  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  1.17.44.1       riz  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  1.17.44.1       riz  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     24  1.17.44.1       riz  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     25  1.17.44.1       riz  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.17.44.1       riz  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27  1.17.44.1       riz  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  1.17.44.1       riz  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  1.17.44.1       riz  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30  1.17.44.1       riz  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  1.17.44.1       riz  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32        1.1       cgd  */
     33        1.1       cgd 
     34        1.4  christos #include <sys/cdefs.h>
     35        1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     36        1.4  christos #if 0
     37        1.4  christos static char *sccsid = "@(#)xdr_stdio.c 1.16 87/08/11 Copyr 1984 Sun Micro";
     38        1.4  christos static char *sccsid = "@(#)xdr_stdio.c	2.1 88/07/29 4.0 RPCSRC";
     39        1.4  christos #else
     40  1.17.44.1       riz __RCSID("$NetBSD: xdr_stdio.c,v 1.17.44.1 2013/03/14 22:03:14 riz Exp $");
     41        1.4  christos #endif
     42        1.1       cgd #endif
     43        1.1       cgd 
     44        1.1       cgd /*
     45        1.1       cgd  * xdr_stdio.c, XDR implementation on standard i/o file.
     46        1.1       cgd  *
     47        1.1       cgd  * Copyright (C) 1984, Sun Microsystems, Inc.
     48        1.1       cgd  *
     49        1.1       cgd  * This set of routines implements a XDR on a stdio stream.
     50        1.1       cgd  * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
     51        1.1       cgd  * from the stream.
     52        1.1       cgd  */
     53        1.1       cgd 
     54        1.5       jtc #include "namespace.h"
     55        1.9     lukem 
     56        1.9     lukem #include <stdio.h>
     57        1.9     lukem 
     58        1.8     lukem #include <rpc/types.h>
     59        1.1       cgd #include <rpc/xdr.h>
     60        1.5       jtc 
     61        1.5       jtc #ifdef __weak_alias
     62       1.14   mycroft __weak_alias(xdrstdio_create,_xdrstdio_create)
     63        1.5       jtc #endif
     64        1.1       cgd 
     65        1.4  christos static void xdrstdio_destroy __P((XDR *));
     66        1.8     lukem static bool_t xdrstdio_getlong __P((XDR *, long *));
     67       1.11   mycroft static bool_t xdrstdio_putlong __P((XDR *, const long *));
     68       1.11   mycroft static bool_t xdrstdio_getbytes __P((XDR *, char *, u_int));
     69       1.11   mycroft static bool_t xdrstdio_putbytes __P((XDR *, const char *, u_int));
     70        1.8     lukem static u_int xdrstdio_getpos __P((XDR *));
     71        1.8     lukem static bool_t xdrstdio_setpos __P((XDR *, u_int));
     72        1.8     lukem static int32_t *xdrstdio_inline __P((XDR *, u_int));
     73        1.1       cgd 
     74        1.1       cgd /*
     75        1.1       cgd  * Ops vector for stdio type XDR
     76        1.1       cgd  */
     77       1.10   mycroft static const struct xdr_ops	xdrstdio_ops = {
     78        1.1       cgd 	xdrstdio_getlong,	/* deseraialize a long int */
     79        1.1       cgd 	xdrstdio_putlong,	/* seraialize a long int */
     80        1.1       cgd 	xdrstdio_getbytes,	/* deserialize counted bytes */
     81        1.1       cgd 	xdrstdio_putbytes,	/* serialize counted bytes */
     82        1.1       cgd 	xdrstdio_getpos,	/* get offset in the stream */
     83        1.1       cgd 	xdrstdio_setpos,	/* set offset in the stream */
     84        1.1       cgd 	xdrstdio_inline,	/* prime stream for inline macros */
     85       1.17  christos 	xdrstdio_destroy,	/* destroy stream */
     86       1.17  christos 	NULL,			/* xdrstdio_control */
     87        1.1       cgd };
     88        1.1       cgd 
     89        1.1       cgd /*
     90        1.1       cgd  * Initialize a stdio xdr stream.
     91        1.1       cgd  * Sets the xdr stream handle xdrs for use on the stream file.
     92        1.1       cgd  * Operation flag is set to op.
     93        1.1       cgd  */
     94        1.1       cgd void
     95        1.1       cgd xdrstdio_create(xdrs, file, op)
     96        1.9     lukem 	XDR *xdrs;
     97        1.1       cgd 	FILE *file;
     98        1.1       cgd 	enum xdr_op op;
     99        1.1       cgd {
    100        1.1       cgd 
    101        1.1       cgd 	xdrs->x_op = op;
    102        1.1       cgd 	xdrs->x_ops = &xdrstdio_ops;
    103       1.12  christos 	xdrs->x_private = file;
    104        1.1       cgd 	xdrs->x_handy = 0;
    105        1.1       cgd 	xdrs->x_base = 0;
    106        1.1       cgd }
    107        1.1       cgd 
    108        1.1       cgd /*
    109        1.1       cgd  * Destroy a stdio xdr stream.
    110        1.1       cgd  * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
    111        1.1       cgd  */
    112        1.1       cgd static void
    113        1.1       cgd xdrstdio_destroy(xdrs)
    114        1.9     lukem 	XDR *xdrs;
    115        1.1       cgd {
    116        1.1       cgd 	(void)fflush((FILE *)xdrs->x_private);
    117       1.13     lukem 		/* XXX: should we close the file ?? */
    118       1.12  christos }
    119        1.1       cgd 
    120        1.1       cgd static bool_t
    121        1.1       cgd xdrstdio_getlong(xdrs, lp)
    122        1.1       cgd 	XDR *xdrs;
    123        1.9     lukem 	long *lp;
    124        1.1       cgd {
    125       1.15    martin 	u_int32_t temp;
    126        1.1       cgd 
    127       1.15    martin 	if (fread(&temp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
    128        1.1       cgd 		return (FALSE);
    129       1.15    martin 	*lp = (long)ntohl(temp);
    130        1.1       cgd 	return (TRUE);
    131        1.1       cgd }
    132        1.1       cgd 
    133        1.1       cgd static bool_t
    134        1.1       cgd xdrstdio_putlong(xdrs, lp)
    135        1.1       cgd 	XDR *xdrs;
    136       1.11   mycroft 	const long *lp;
    137        1.1       cgd {
    138       1.16    martin 	int32_t mycopy = htonl((u_int32_t)*lp);
    139        1.1       cgd 
    140       1.12  christos 	if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
    141        1.1       cgd 		return (FALSE);
    142        1.1       cgd 	return (TRUE);
    143        1.1       cgd }
    144        1.1       cgd 
    145        1.1       cgd static bool_t
    146        1.1       cgd xdrstdio_getbytes(xdrs, addr, len)
    147        1.1       cgd 	XDR *xdrs;
    148       1.11   mycroft 	char *addr;
    149        1.8     lukem 	u_int len;
    150        1.1       cgd {
    151        1.1       cgd 
    152       1.12  christos 	if ((len != 0) && (fread(addr, (size_t)len, 1, (FILE *)xdrs->x_private) != 1))
    153        1.1       cgd 		return (FALSE);
    154        1.1       cgd 	return (TRUE);
    155        1.1       cgd }
    156        1.1       cgd 
    157        1.1       cgd static bool_t
    158        1.1       cgd xdrstdio_putbytes(xdrs, addr, len)
    159        1.1       cgd 	XDR *xdrs;
    160       1.11   mycroft 	const char *addr;
    161        1.8     lukem 	u_int len;
    162        1.1       cgd {
    163        1.1       cgd 
    164       1.12  christos 	if ((len != 0) && (fwrite(addr, (size_t)len, 1,
    165       1.12  christos 	    (FILE *)xdrs->x_private) != 1))
    166        1.1       cgd 		return (FALSE);
    167        1.1       cgd 	return (TRUE);
    168        1.1       cgd }
    169        1.1       cgd 
    170        1.8     lukem static u_int
    171        1.1       cgd xdrstdio_getpos(xdrs)
    172        1.1       cgd 	XDR *xdrs;
    173        1.1       cgd {
    174        1.1       cgd 
    175        1.8     lukem 	return ((u_int) ftell((FILE *)xdrs->x_private));
    176        1.1       cgd }
    177        1.1       cgd 
    178        1.1       cgd static bool_t
    179        1.1       cgd xdrstdio_setpos(xdrs, pos)
    180        1.1       cgd 	XDR *xdrs;
    181        1.8     lukem 	u_int pos;
    182        1.1       cgd {
    183        1.1       cgd 
    184        1.8     lukem 	return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ?
    185        1.1       cgd 		FALSE : TRUE);
    186        1.1       cgd }
    187        1.1       cgd 
    188       1.12  christos /* ARGSUSED */
    189        1.2       cgd static int32_t *
    190        1.1       cgd xdrstdio_inline(xdrs, len)
    191        1.1       cgd 	XDR *xdrs;
    192        1.8     lukem 	u_int len;
    193        1.1       cgd {
    194        1.1       cgd 
    195        1.1       cgd 	/*
    196        1.1       cgd 	 * Must do some work to implement this: must insure
    197        1.1       cgd 	 * enough data in the underlying stdio buffer,
    198        1.1       cgd 	 * that the buffer is aligned so that we can indirect through a
    199        1.8     lukem 	 * long *, and stuff this pointer in xdrs->x_buf.  Doing
    200        1.1       cgd 	 * a fread or fwrite to a scratch buffer would defeat
    201        1.1       cgd 	 * most of the gains to be had here and require storage
    202        1.1       cgd 	 * management on this buffer, so we don't do this.
    203        1.1       cgd 	 */
    204        1.1       cgd 	return (NULL);
    205        1.1       cgd }
    206