Back to index

glibc  2.9
xdr_stdio.c
Go to the documentation of this file.
00001 /*
00002  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00003  * unrestricted use provided that this legend is included on all tape
00004  * media and as a part of the software program in whole or part.  Users
00005  * may copy or modify Sun RPC without charge, but are not authorized
00006  * to license or distribute it to anyone else except as part of a product or
00007  * program developed by the user.
00008  *
00009  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00010  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00011  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00012  *
00013  * Sun RPC is provided with no support and without any obligation on the
00014  * part of Sun Microsystems, Inc. to assist in its use, correction,
00015  * modification or enhancement.
00016  *
00017  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00018  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00019  * OR ANY PART THEREOF.
00020  *
00021  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00022  * or profits or other special, indirect and consequential damages, even if
00023  * Sun has been advised of the possibility of such damages.
00024  *
00025  * Sun Microsystems, Inc.
00026  * 2550 Garcia Avenue
00027  * Mountain View, California  94043
00028  */
00029 
00030 /*
00031  * xdr_stdio.c, XDR implementation on standard i/o file.
00032  *
00033  * Copyright (C) 1984, Sun Microsystems, Inc.
00034  *
00035  * This set of routines implements a XDR on a stdio stream.
00036  * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
00037  * from the stream.
00038  */
00039 
00040 #include <rpc/types.h>
00041 #include <stdio.h>
00042 #include <rpc/xdr.h>
00043 
00044 #ifdef USE_IN_LIBIO
00045 # include <libio/iolibio.h>
00046 # define fflush(s) INTUSE(_IO_fflush) (s)
00047 # define fread(p, m, n, s) INTUSE(_IO_fread) (p, m, n, s)
00048 # define ftell(s) INTUSE(_IO_ftell) (s)
00049 # define fwrite(p, m, n, s) INTUSE(_IO_fwrite) (p, m, n, s)
00050 #endif
00051 
00052 static bool_t xdrstdio_getlong (XDR *, long *);
00053 static bool_t xdrstdio_putlong (XDR *, const long *);
00054 static bool_t xdrstdio_getbytes (XDR *, caddr_t, u_int);
00055 static bool_t xdrstdio_putbytes (XDR *, const char *, u_int);
00056 static u_int xdrstdio_getpos (const XDR *);
00057 static bool_t xdrstdio_setpos (XDR *, u_int);
00058 static int32_t *xdrstdio_inline (XDR *, u_int);
00059 static void xdrstdio_destroy (XDR *);
00060 static bool_t xdrstdio_getint32 (XDR *, int32_t *);
00061 static bool_t xdrstdio_putint32 (XDR *, const int32_t *);
00062 
00063 /*
00064  * Ops vector for stdio type XDR
00065  */
00066 static const struct xdr_ops xdrstdio_ops =
00067 {
00068   xdrstdio_getlong,         /* deserialize a long int */
00069   xdrstdio_putlong,         /* serialize a long int */
00070   xdrstdio_getbytes,        /* deserialize counted bytes */
00071   xdrstdio_putbytes,        /* serialize counted bytes */
00072   xdrstdio_getpos,          /* get offset in the stream */
00073   xdrstdio_setpos,          /* set offset in the stream */
00074   xdrstdio_inline,          /* prime stream for inline macros */
00075   xdrstdio_destroy,         /* destroy stream */
00076   xdrstdio_getint32,        /* deserialize a int */
00077   xdrstdio_putint32         /* serialize a int */
00078 };
00079 
00080 /*
00081  * Initialize a stdio xdr stream.
00082  * Sets the xdr stream handle xdrs for use on the stream file.
00083  * Operation flag is set to op.
00084  */
00085 void
00086 xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
00087 {
00088   xdrs->x_op = op;
00089   /* We have to add the const since the `struct xdr_ops' in `struct XDR'
00090      is not `const'.  */
00091   xdrs->x_ops = (struct xdr_ops *) &xdrstdio_ops;
00092   xdrs->x_private = (caddr_t) file;
00093   xdrs->x_handy = 0;
00094   xdrs->x_base = 0;
00095 }
00096 
00097 /*
00098  * Destroy a stdio xdr stream.
00099  * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
00100  */
00101 static void
00102 xdrstdio_destroy (XDR *xdrs)
00103 {
00104   (void) fflush ((FILE *) xdrs->x_private);
00105   /* xx should we close the file ?? */
00106 };
00107 
00108 static bool_t
00109 xdrstdio_getlong (XDR *xdrs, long *lp)
00110 {
00111   u_int32_t mycopy;
00112 
00113   if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
00114     return FALSE;
00115   *lp = (long) ntohl (mycopy);
00116   return TRUE;
00117 }
00118 
00119 static bool_t
00120 xdrstdio_putlong (XDR *xdrs, const long *lp)
00121 {
00122   int32_t mycopy = htonl ((u_int32_t) *lp);
00123 
00124   if (fwrite ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
00125     return FALSE;
00126   return TRUE;
00127 }
00128 
00129 static bool_t
00130 xdrstdio_getbytes (XDR *xdrs, const caddr_t addr, u_int len)
00131 {
00132   if ((len != 0) && (fread (addr, (int) len, 1,
00133                          (FILE *) xdrs->x_private) != 1))
00134     return FALSE;
00135   return TRUE;
00136 }
00137 
00138 static bool_t
00139 xdrstdio_putbytes (XDR *xdrs, const char *addr, u_int len)
00140 {
00141   if ((len != 0) && (fwrite (addr, (int) len, 1,
00142                           (FILE *) xdrs->x_private) != 1))
00143     return FALSE;
00144   return TRUE;
00145 }
00146 
00147 static u_int
00148 xdrstdio_getpos (const XDR *xdrs)
00149 {
00150   return (u_int) ftell ((FILE *) xdrs->x_private);
00151 }
00152 
00153 static bool_t
00154 xdrstdio_setpos (XDR *xdrs, u_int pos)
00155 {
00156   return fseek ((FILE *) xdrs->x_private, (long) pos, 0) < 0 ? FALSE : TRUE;
00157 }
00158 
00159 static int32_t *
00160 xdrstdio_inline (XDR *xdrs, u_int len)
00161 {
00162   /*
00163    * Must do some work to implement this: must insure
00164    * enough data in the underlying stdio buffer,
00165    * that the buffer is aligned so that we can indirect through a
00166    * long *, and stuff this pointer in xdrs->x_buf.  Doing
00167    * a fread or fwrite to a scratch buffer would defeat
00168    * most of the gains to be had here and require storage
00169    * management on this buffer, so we don't do this.
00170    */
00171   return NULL;
00172 }
00173 
00174 static bool_t
00175 xdrstdio_getint32 (XDR *xdrs, int32_t *ip)
00176 {
00177   int32_t mycopy;
00178 
00179   if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
00180     return FALSE;
00181   *ip = ntohl (mycopy);
00182   return TRUE;
00183 }
00184 
00185 static bool_t
00186 xdrstdio_putint32 (XDR *xdrs, const int32_t *ip)
00187 {
00188   int32_t mycopy = htonl (*ip);
00189 
00190   ip = &mycopy;
00191   if (fwrite ((caddr_t) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
00192     return FALSE;
00193   return TRUE;
00194 }
00195 
00196 libc_hidden_def (xdrstdio_create)