xdr_stdio.c revision 1902
11573Srgrimes/* 21573Srgrimes * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 31573Srgrimes * unrestricted use provided that this legend is included on all tape 41573Srgrimes * media and as a part of the software program in whole or part. Users 51573Srgrimes * may copy or modify Sun RPC without charge, but are not authorized 61573Srgrimes * to license or distribute it to anyone else except as part of a product or 71573Srgrimes * program developed by the user. 81573Srgrimes * 91573Srgrimes * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 101573Srgrimes * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 111573Srgrimes * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 121573Srgrimes * 131573Srgrimes * Sun RPC is provided with no support and without any obligation on the 141573Srgrimes * part of Sun Microsystems, Inc. to assist in its use, correction, 151573Srgrimes * modification or enhancement. 161573Srgrimes * 171573Srgrimes * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 181573Srgrimes * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 191573Srgrimes * OR ANY PART THEREOF. 201573Srgrimes * 211573Srgrimes * In no event will Sun Microsystems, Inc. be liable for any lost revenue 221573Srgrimes * or profits or other special, indirect and consequential damages, even if 231573Srgrimes * Sun has been advised of the possibility of such damages. 241573Srgrimes * 251573Srgrimes * Sun Microsystems, Inc. 261573Srgrimes * 2550 Garcia Avenue 271573Srgrimes * Mountain View, California 94043 281573Srgrimes */ 291573Srgrimes 301573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 311573Srgrimes/*static char *sccsid = "from: @(#)xdr_stdio.c 1.16 87/08/11 Copyr 1984 Sun Micro";*/ 3254770Sgreen/*static char *sccsid = "from: @(#)xdr_stdio.c 2.1 88/07/29 4.0 RPCSRC";*/ 3354770Sgreenstatic char *rcsid = "$Id: xdr_stdio.c,v 1.1 1993/10/27 05:41:14 paul Exp $"; 341573Srgrimes#endif 351573Srgrimes 361573Srgrimes/* 3723668Speter * xdr_stdio.c, XDR implementation on standard i/o file. 381573Srgrimes * 3990045Sobrien * Copyright (C) 1984, Sun Microsystems, Inc. 4090045Sobrien * 411573Srgrimes * This set of routines implements a XDR on a stdio stream. 4271579Sdeischen * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes 4377497Skris * from the stream. 441573Srgrimes */ 451573Srgrimes 461573Srgrimes#include <rpc/types.h> 471573Srgrimes#include <stdio.h> 481573Srgrimes#include <rpc/xdr.h> 491573Srgrimes 501573Srgrimesstatic bool_t xdrstdio_getlong(); 511573Srgrimesstatic bool_t xdrstdio_putlong(); 521573Srgrimesstatic bool_t xdrstdio_getbytes(); 531573Srgrimesstatic bool_t xdrstdio_putbytes(); 5471579Sdeischenstatic u_int xdrstdio_getpos(); 551573Srgrimesstatic bool_t xdrstdio_setpos(); 5690045Sobrienstatic long * xdrstdio_inline(); 5790045Sobrienstatic void xdrstdio_destroy(); 5890045Sobrien 5990045Sobrien/* 6090045Sobrien * Ops vector for stdio type XDR 6190045Sobrien */ 6290045Sobrienstatic struct xdr_ops xdrstdio_ops = { 6390045Sobrien xdrstdio_getlong, /* deseraialize a long int */ 6490045Sobrien xdrstdio_putlong, /* seraialize a long int */ 6590045Sobrien xdrstdio_getbytes, /* deserialize counted bytes */ 661573Srgrimes xdrstdio_putbytes, /* serialize counted bytes */ 6728913Simp xdrstdio_getpos, /* get offset in the stream */ 681573Srgrimes xdrstdio_setpos, /* set offset in the stream */ 6928913Simp xdrstdio_inline, /* prime stream for inline macros */ 7028913Simp xdrstdio_destroy /* destroy stream */ 7128913Simp}; 721573Srgrimes 731573Srgrimes/* 741573Srgrimes * Initialize a stdio xdr stream. 751573Srgrimes * Sets the xdr stream handle xdrs for use on the stream file. 761573Srgrimes * Operation flag is set to op. 771573Srgrimes */ 781573Srgrimesvoid 791573Srgrimesxdrstdio_create(xdrs, file, op) 801573Srgrimes register XDR *xdrs; 811573Srgrimes FILE *file; 821573Srgrimes enum xdr_op op; 8390045Sobrien{ 8490045Sobrien 851573Srgrimes xdrs->x_op = op; 8690045Sobrien xdrs->x_ops = &xdrstdio_ops; 8790045Sobrien xdrs->x_private = (caddr_t)file; 8890045Sobrien xdrs->x_handy = 0; 891573Srgrimes xdrs->x_base = 0; 901573Srgrimes} 911573Srgrimes 921573Srgrimes/* 931573Srgrimes * Destroy a stdio xdr stream. 941573Srgrimes * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create. 951573Srgrimes */ 961573Srgrimesstatic void 971573Srgrimesxdrstdio_destroy(xdrs) 981573Srgrimes register XDR *xdrs; 991573Srgrimes{ 1001573Srgrimes (void)fflush((FILE *)xdrs->x_private); 1011573Srgrimes /* xx should we close the file ?? */ 1021573Srgrimes}; 1031573Srgrimes 1041573Srgrimesstatic bool_t 10554770Sgreenxdrstdio_getlong(xdrs, lp) 10654770Sgreen XDR *xdrs; 10754770Sgreen register long *lp; 1081573Srgrimes{ 1091573Srgrimes 1101573Srgrimes if (fread((caddr_t)lp, sizeof(long), 1, (FILE *)xdrs->x_private) != 1) 1111573Srgrimes return (FALSE); 1121573Srgrimes#ifndef mc68000 1131573Srgrimes *lp = ntohl(*lp); 1141573Srgrimes#endif 1151573Srgrimes return (TRUE); 1161573Srgrimes} 1171573Srgrimes 1181573Srgrimesstatic bool_t 1191573Srgrimesxdrstdio_putlong(xdrs, lp) 1201573Srgrimes XDR *xdrs; 1211573Srgrimes long *lp; 1221573Srgrimes{ 1231573Srgrimes 1241573Srgrimes#ifndef mc68000 12564740Sgreen long mycopy = htonl(*lp); 1261573Srgrimes lp = &mycopy; 1271573Srgrimes#endif 1281573Srgrimes if (fwrite((caddr_t)lp, sizeof(long), 1, (FILE *)xdrs->x_private) != 1) 1291573Srgrimes return (FALSE); 1301573Srgrimes return (TRUE); 1311573Srgrimes} 1321573Srgrimes 1331573Srgrimesstatic bool_t 1341573Srgrimesxdrstdio_getbytes(xdrs, addr, len) 1351573Srgrimes XDR *xdrs; 1361573Srgrimes caddr_t addr; 1371573Srgrimes u_int len; 1381573Srgrimes{ 1391573Srgrimes 1401573Srgrimes if ((len != 0) && (fread(addr, (int)len, 1, (FILE *)xdrs->x_private) != 1)) 1411573Srgrimes return (FALSE); 1421573Srgrimes return (TRUE); 1431573Srgrimes} 1441573Srgrimes 1451573Srgrimesstatic bool_t 1461573Srgrimesxdrstdio_putbytes(xdrs, addr, len) 1471573Srgrimes XDR *xdrs; 1481573Srgrimes caddr_t addr; 1491573Srgrimes u_int len; 1501573Srgrimes{ 1511573Srgrimes 1521573Srgrimes if ((len != 0) && (fwrite(addr, (int)len, 1, (FILE *)xdrs->x_private) != 1)) 1531573Srgrimes return (FALSE); 1541573Srgrimes return (TRUE); 1551573Srgrimes} 1561573Srgrimes 1571573Srgrimesstatic u_int 1581573Srgrimesxdrstdio_getpos(xdrs) 1591573Srgrimes XDR *xdrs; 1601573Srgrimes{ 1611573Srgrimes 1621573Srgrimes return ((u_int) ftell((FILE *)xdrs->x_private)); 1631573Srgrimes} 1641573Srgrimes 1651573Srgrimesstatic bool_t 1661573Srgrimesxdrstdio_setpos(xdrs, pos) 1671573Srgrimes XDR *xdrs; 1681573Srgrimes u_int pos; 1691573Srgrimes{ 1701573Srgrimes 1711573Srgrimes return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ? 1721573Srgrimes FALSE : TRUE); 17354770Sgreen} 1741573Srgrimes 1751573Srgrimesstatic long * 1761573Srgrimesxdrstdio_inline(xdrs, len) 1771573Srgrimes XDR *xdrs; 1781573Srgrimes u_int len; 17956698Sjasone{ 1801573Srgrimes 1811573Srgrimes /* 1821573Srgrimes * Must do some work to implement this: must insure 1831573Srgrimes * enough data in the underlying stdio buffer, 1841573Srgrimes * that the buffer is aligned so that we can indirect through a 1851573Srgrimes * long *, and stuff this pointer in xdrs->x_buf. Doing 1861573Srgrimes * a fread or fwrite to a scratch buffer would defeat 1871573Srgrimes * most of the gains to be had here and require storage 1881573Srgrimes * management on this buffer, so we don't do this. 1891573Srgrimes */ 1901573Srgrimes return (NULL); 1911573Srgrimes} 1921573Srgrimes