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