Home | History | Annotate | Line # | Download | only in rpc
xdr_stdio.c revision 1.17.42.1
      1  1.17.42.1      yamt /*	$NetBSD: xdr_stdio.c,v 1.17.42.1 2012/04/17 00:05:23 yamt Exp $	*/
      2        1.3       cgd 
      3        1.1       cgd /*
      4        1.1       cgd  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5        1.1       cgd  * unrestricted use provided that this legend is included on all tape
      6        1.1       cgd  * media and as a part of the software program in whole or part.  Users
      7        1.1       cgd  * may copy or modify Sun RPC without charge, but are not authorized
      8        1.1       cgd  * to license or distribute it to anyone else except as part of a product or
      9        1.1       cgd  * program developed by the user.
     10        1.1       cgd  *
     11        1.1       cgd  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12        1.1       cgd  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13        1.1       cgd  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14        1.1       cgd  *
     15        1.1       cgd  * Sun RPC is provided with no support and without any obligation on the
     16        1.1       cgd  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17        1.1       cgd  * modification or enhancement.
     18        1.1       cgd  *
     19        1.1       cgd  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20        1.1       cgd  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21        1.1       cgd  * OR ANY PART THEREOF.
     22        1.1       cgd  *
     23        1.1       cgd  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24        1.1       cgd  * or profits or other special, indirect and consequential damages, even if
     25        1.1       cgd  * Sun has been advised of the possibility of such damages.
     26        1.1       cgd  *
     27        1.1       cgd  * Sun Microsystems, Inc.
     28        1.1       cgd  * 2550 Garcia Avenue
     29        1.1       cgd  * Mountain View, California  94043
     30        1.1       cgd  */
     31        1.1       cgd 
     32        1.4  christos #include <sys/cdefs.h>
     33        1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     34        1.4  christos #if 0
     35        1.4  christos static char *sccsid = "@(#)xdr_stdio.c 1.16 87/08/11 Copyr 1984 Sun Micro";
     36        1.4  christos static char *sccsid = "@(#)xdr_stdio.c	2.1 88/07/29 4.0 RPCSRC";
     37        1.4  christos #else
     38  1.17.42.1      yamt __RCSID("$NetBSD: xdr_stdio.c,v 1.17.42.1 2012/04/17 00:05:23 yamt Exp $");
     39        1.4  christos #endif
     40        1.1       cgd #endif
     41        1.1       cgd 
     42        1.1       cgd /*
     43        1.1       cgd  * xdr_stdio.c, XDR implementation on standard i/o file.
     44        1.1       cgd  *
     45        1.1       cgd  * Copyright (C) 1984, Sun Microsystems, Inc.
     46        1.1       cgd  *
     47        1.1       cgd  * This set of routines implements a XDR on a stdio stream.
     48        1.1       cgd  * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
     49        1.1       cgd  * from the stream.
     50        1.1       cgd  */
     51        1.1       cgd 
     52        1.5       jtc #include "namespace.h"
     53        1.9     lukem 
     54        1.9     lukem #include <stdio.h>
     55        1.9     lukem 
     56        1.8     lukem #include <rpc/types.h>
     57        1.1       cgd #include <rpc/xdr.h>
     58        1.5       jtc 
     59        1.5       jtc #ifdef __weak_alias
     60       1.14   mycroft __weak_alias(xdrstdio_create,_xdrstdio_create)
     61        1.5       jtc #endif
     62        1.1       cgd 
     63  1.17.42.1      yamt static void xdrstdio_destroy(XDR *);
     64  1.17.42.1      yamt static bool_t xdrstdio_getlong(XDR *, long *);
     65  1.17.42.1      yamt static bool_t xdrstdio_putlong(XDR *, const long *);
     66  1.17.42.1      yamt static bool_t xdrstdio_getbytes(XDR *, char *, u_int);
     67  1.17.42.1      yamt static bool_t xdrstdio_putbytes(XDR *, const char *, u_int);
     68  1.17.42.1      yamt static u_int xdrstdio_getpos(XDR *);
     69  1.17.42.1      yamt static bool_t xdrstdio_setpos(XDR *, u_int);
     70  1.17.42.1      yamt static int32_t *xdrstdio_inline(XDR *, u_int);
     71        1.1       cgd 
     72        1.1       cgd /*
     73        1.1       cgd  * Ops vector for stdio type XDR
     74        1.1       cgd  */
     75       1.10   mycroft static const struct xdr_ops	xdrstdio_ops = {
     76        1.1       cgd 	xdrstdio_getlong,	/* deseraialize a long int */
     77        1.1       cgd 	xdrstdio_putlong,	/* seraialize a long int */
     78        1.1       cgd 	xdrstdio_getbytes,	/* deserialize counted bytes */
     79        1.1       cgd 	xdrstdio_putbytes,	/* serialize counted bytes */
     80        1.1       cgd 	xdrstdio_getpos,	/* get offset in the stream */
     81        1.1       cgd 	xdrstdio_setpos,	/* set offset in the stream */
     82        1.1       cgd 	xdrstdio_inline,	/* prime stream for inline macros */
     83       1.17  christos 	xdrstdio_destroy,	/* destroy stream */
     84       1.17  christos 	NULL,			/* xdrstdio_control */
     85        1.1       cgd };
     86        1.1       cgd 
     87        1.1       cgd /*
     88        1.1       cgd  * Initialize a stdio xdr stream.
     89        1.1       cgd  * Sets the xdr stream handle xdrs for use on the stream file.
     90        1.1       cgd  * Operation flag is set to op.
     91        1.1       cgd  */
     92        1.1       cgd void
     93  1.17.42.1      yamt xdrstdio_create(XDR *xdrs, FILE *file, enum xdr_op op)
     94        1.1       cgd {
     95        1.1       cgd 
     96        1.1       cgd 	xdrs->x_op = op;
     97        1.1       cgd 	xdrs->x_ops = &xdrstdio_ops;
     98       1.12  christos 	xdrs->x_private = file;
     99        1.1       cgd 	xdrs->x_handy = 0;
    100        1.1       cgd 	xdrs->x_base = 0;
    101        1.1       cgd }
    102        1.1       cgd 
    103        1.1       cgd /*
    104        1.1       cgd  * Destroy a stdio xdr stream.
    105        1.1       cgd  * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
    106        1.1       cgd  */
    107        1.1       cgd static void
    108  1.17.42.1      yamt xdrstdio_destroy(XDR *xdrs)
    109        1.1       cgd {
    110        1.1       cgd 	(void)fflush((FILE *)xdrs->x_private);
    111       1.13     lukem 		/* XXX: should we close the file ?? */
    112       1.12  christos }
    113        1.1       cgd 
    114        1.1       cgd static bool_t
    115  1.17.42.1      yamt xdrstdio_getlong(XDR *xdrs, long *lp)
    116        1.1       cgd {
    117       1.15    martin 	u_int32_t temp;
    118        1.1       cgd 
    119       1.15    martin 	if (fread(&temp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
    120        1.1       cgd 		return (FALSE);
    121       1.15    martin 	*lp = (long)ntohl(temp);
    122        1.1       cgd 	return (TRUE);
    123        1.1       cgd }
    124        1.1       cgd 
    125        1.1       cgd static bool_t
    126  1.17.42.1      yamt xdrstdio_putlong(XDR *xdrs, const long *lp)
    127        1.1       cgd {
    128       1.16    martin 	int32_t mycopy = htonl((u_int32_t)*lp);
    129        1.1       cgd 
    130       1.12  christos 	if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
    131        1.1       cgd 		return (FALSE);
    132        1.1       cgd 	return (TRUE);
    133        1.1       cgd }
    134        1.1       cgd 
    135        1.1       cgd static bool_t
    136  1.17.42.1      yamt xdrstdio_getbytes(XDR *xdrs, char *addr, u_int len)
    137        1.1       cgd {
    138        1.1       cgd 
    139       1.12  christos 	if ((len != 0) && (fread(addr, (size_t)len, 1, (FILE *)xdrs->x_private) != 1))
    140        1.1       cgd 		return (FALSE);
    141        1.1       cgd 	return (TRUE);
    142        1.1       cgd }
    143        1.1       cgd 
    144        1.1       cgd static bool_t
    145  1.17.42.1      yamt xdrstdio_putbytes(XDR *xdrs, const char *addr, u_int len)
    146        1.1       cgd {
    147        1.1       cgd 
    148       1.12  christos 	if ((len != 0) && (fwrite(addr, (size_t)len, 1,
    149       1.12  christos 	    (FILE *)xdrs->x_private) != 1))
    150        1.1       cgd 		return (FALSE);
    151        1.1       cgd 	return (TRUE);
    152        1.1       cgd }
    153        1.1       cgd 
    154        1.8     lukem static u_int
    155  1.17.42.1      yamt xdrstdio_getpos(XDR *xdrs)
    156        1.1       cgd {
    157        1.1       cgd 
    158        1.8     lukem 	return ((u_int) ftell((FILE *)xdrs->x_private));
    159        1.1       cgd }
    160        1.1       cgd 
    161        1.1       cgd static bool_t
    162  1.17.42.1      yamt xdrstdio_setpos(XDR *xdrs, u_int pos)
    163        1.1       cgd {
    164        1.1       cgd 
    165        1.8     lukem 	return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ?
    166        1.1       cgd 		FALSE : TRUE);
    167        1.1       cgd }
    168        1.1       cgd 
    169       1.12  christos /* ARGSUSED */
    170        1.2       cgd static int32_t *
    171  1.17.42.1      yamt xdrstdio_inline(XDR *xdrs, u_int len)
    172        1.1       cgd {
    173        1.1       cgd 
    174        1.1       cgd 	/*
    175        1.1       cgd 	 * Must do some work to implement this: must insure
    176        1.1       cgd 	 * enough data in the underlying stdio buffer,
    177        1.1       cgd 	 * that the buffer is aligned so that we can indirect through a
    178        1.8     lukem 	 * long *, and stuff this pointer in xdrs->x_buf.  Doing
    179        1.1       cgd 	 * a fread or fwrite to a scratch buffer would defeat
    180        1.1       cgd 	 * most of the gains to be had here and require storage
    181        1.1       cgd 	 * management on this buffer, so we don't do this.
    182        1.1       cgd 	 */
    183        1.1       cgd 	return (NULL);
    184        1.1       cgd }
    185