174462Salfred/* $NetBSD: xdr_stdio.c,v 1.14 2000/01/22 22:19:19 mycroft Exp $ */ 274462Salfred 31902Swollman/* 41902Swollman * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 51902Swollman * unrestricted use provided that this legend is included on all tape 61902Swollman * media and as a part of the software program in whole or part. Users 71902Swollman * may copy or modify Sun RPC without charge, but are not authorized 81902Swollman * to license or distribute it to anyone else except as part of a product or 91902Swollman * program developed by the user. 108870Srgrimes * 111902Swollman * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 121902Swollman * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 131902Swollman * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 148870Srgrimes * 151902Swollman * Sun RPC is provided with no support and without any obligation on the 161902Swollman * part of Sun Microsystems, Inc. to assist in its use, correction, 171902Swollman * modification or enhancement. 188870Srgrimes * 191902Swollman * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 201902Swollman * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 211902Swollman * OR ANY PART THEREOF. 228870Srgrimes * 231902Swollman * In no event will Sun Microsystems, Inc. be liable for any lost revenue 241902Swollman * or profits or other special, indirect and consequential damages, even if 251902Swollman * Sun has been advised of the possibility of such damages. 268870Srgrimes * 271902Swollman * Sun Microsystems, Inc. 281902Swollman * 2550 Garcia Avenue 291902Swollman * Mountain View, California 94043 301902Swollman */ 311902Swollman 321902Swollman#if defined(LIBC_SCCS) && !defined(lint) 33136582Sobrienstatic char *sccsid2 = "@(#)xdr_stdio.c 1.16 87/08/11 Copyr 1984 Sun Micro"; 3492986Sobrienstatic char *sccsid = "@(#)xdr_stdio.c 2.1 88/07/29 4.0 RPCSRC"; 351902Swollman#endif 3692986Sobrien#include <sys/cdefs.h> 3792986Sobrien__FBSDID("$FreeBSD$"); 381902Swollman 391902Swollman/* 401902Swollman * xdr_stdio.c, XDR implementation on standard i/o file. 411902Swollman * 421902Swollman * Copyright (C) 1984, Sun Microsystems, Inc. 431902Swollman * 441902Swollman * This set of routines implements a XDR on a stdio stream. 451902Swollman * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes 461902Swollman * from the stream. 471902Swollman */ 481902Swollman 4974462Salfred#include "namespace.h" 5074462Salfred#include <stdio.h> 5174462Salfred 5290868Smike#include <arpa/inet.h> 531902Swollman#include <rpc/types.h> 541902Swollman#include <rpc/xdr.h> 5574462Salfred#include "un-namespace.h" 561902Swollman 5792905Sobrienstatic void xdrstdio_destroy(XDR *); 5892905Sobrienstatic bool_t xdrstdio_getlong(XDR *, long *); 5992905Sobrienstatic bool_t xdrstdio_putlong(XDR *, const long *); 6092905Sobrienstatic bool_t xdrstdio_getbytes(XDR *, char *, u_int); 6192905Sobrienstatic bool_t xdrstdio_putbytes(XDR *, const char *, u_int); 6292905Sobrienstatic u_int xdrstdio_getpos(XDR *); 6392905Sobrienstatic bool_t xdrstdio_setpos(XDR *, u_int); 6492905Sobrienstatic int32_t *xdrstdio_inline(XDR *, u_int); 651902Swollman 661902Swollman/* 671902Swollman * Ops vector for stdio type XDR 681902Swollman */ 6974462Salfredstatic const struct xdr_ops xdrstdio_ops = { 701902Swollman xdrstdio_getlong, /* deseraialize a long int */ 711902Swollman xdrstdio_putlong, /* seraialize a long int */ 721902Swollman xdrstdio_getbytes, /* deserialize counted bytes */ 731902Swollman xdrstdio_putbytes, /* serialize counted bytes */ 741902Swollman xdrstdio_getpos, /* get offset in the stream */ 751902Swollman xdrstdio_setpos, /* set offset in the stream */ 761902Swollman xdrstdio_inline, /* prime stream for inline macros */ 771902Swollman xdrstdio_destroy /* destroy stream */ 781902Swollman}; 791902Swollman 801902Swollman/* 811902Swollman * Initialize a stdio xdr stream. 821902Swollman * Sets the xdr stream handle xdrs for use on the stream file. 831902Swollman * Operation flag is set to op. 841902Swollman */ 851902Swollmanvoid 861902Swollmanxdrstdio_create(xdrs, file, op) 8774462Salfred XDR *xdrs; 881902Swollman FILE *file; 891902Swollman enum xdr_op op; 901902Swollman{ 911902Swollman 921902Swollman xdrs->x_op = op; 931902Swollman xdrs->x_ops = &xdrstdio_ops; 9474462Salfred xdrs->x_private = file; 951902Swollman xdrs->x_handy = 0; 961902Swollman xdrs->x_base = 0; 971902Swollman} 981902Swollman 991902Swollman/* 1001902Swollman * Destroy a stdio xdr stream. 1011902Swollman * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create. 1021902Swollman */ 1031902Swollmanstatic void 1041902Swollmanxdrstdio_destroy(xdrs) 10574462Salfred XDR *xdrs; 1061902Swollman{ 1071902Swollman (void)fflush((FILE *)xdrs->x_private); 10874462Salfred /* XXX: should we close the file ?? */ 10921062Speter} 1101902Swollman 1111902Swollmanstatic bool_t 1121902Swollmanxdrstdio_getlong(xdrs, lp) 1131902Swollman XDR *xdrs; 11474462Salfred long *lp; 1151902Swollman{ 116124897Smbr u_int32_t temp; 1171902Swollman 118124897Smbr if (fread(&temp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1) 1191902Swollman return (FALSE); 120124897Smbr *lp = (long)ntohl(temp); 1211902Swollman return (TRUE); 1221902Swollman} 1231902Swollman 1241902Swollmanstatic bool_t 1251902Swollmanxdrstdio_putlong(xdrs, lp) 1261902Swollman XDR *xdrs; 12774462Salfred const long *lp; 1281902Swollman{ 129124897Smbr int32_t mycopy = htonl((u_int32_t)*lp); 1301902Swollman 13174462Salfred if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1) 1321902Swollman return (FALSE); 1331902Swollman return (TRUE); 1341902Swollman} 1351902Swollman 1361902Swollmanstatic bool_t 1371902Swollmanxdrstdio_getbytes(xdrs, addr, len) 1381902Swollman XDR *xdrs; 13974462Salfred char *addr; 1401902Swollman u_int len; 1411902Swollman{ 1421902Swollman 14374462Salfred if ((len != 0) && (fread(addr, (size_t)len, 1, (FILE *)xdrs->x_private) != 1)) 1441902Swollman return (FALSE); 1451902Swollman return (TRUE); 1461902Swollman} 1471902Swollman 1481902Swollmanstatic bool_t 1491902Swollmanxdrstdio_putbytes(xdrs, addr, len) 1501902Swollman XDR *xdrs; 15174462Salfred const char *addr; 1521902Swollman u_int len; 1531902Swollman{ 1541902Swollman 15574462Salfred if ((len != 0) && (fwrite(addr, (size_t)len, 1, 15674462Salfred (FILE *)xdrs->x_private) != 1)) 1571902Swollman return (FALSE); 1581902Swollman return (TRUE); 1591902Swollman} 1601902Swollman 1611902Swollmanstatic u_int 1621902Swollmanxdrstdio_getpos(xdrs) 1631902Swollman XDR *xdrs; 1641902Swollman{ 1651902Swollman 1661902Swollman return ((u_int) ftell((FILE *)xdrs->x_private)); 1671902Swollman} 1681902Swollman 1691902Swollmanstatic bool_t 17074462Salfredxdrstdio_setpos(xdrs, pos) 1711902Swollman XDR *xdrs; 1721902Swollman u_int pos; 17374462Salfred{ 1741902Swollman 1751902Swollman return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ? 1761902Swollman FALSE : TRUE); 1771902Swollman} 1781902Swollman 17974462Salfred/* ARGSUSED */ 18021062Speterstatic int32_t * 1811902Swollmanxdrstdio_inline(xdrs, len) 1821902Swollman XDR *xdrs; 1831902Swollman u_int len; 1841902Swollman{ 1851902Swollman 1861902Swollman /* 1871902Swollman * Must do some work to implement this: must insure 1881902Swollman * enough data in the underlying stdio buffer, 1891902Swollman * that the buffer is aligned so that we can indirect through a 1901902Swollman * long *, and stuff this pointer in xdrs->x_buf. Doing 1911902Swollman * a fread or fwrite to a scratch buffer would defeat 1921902Swollman * most of the gains to be had here and require storage 1931902Swollman * management on this buffer, so we don't do this. 1941902Swollman */ 1951902Swollman return (NULL); 1961902Swollman} 197