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