18360efbdSAlfred Perlstein /* $NetBSD: xdr_stdio.c,v 1.14 2000/01/22 22:19:19 mycroft Exp $ */ 28360efbdSAlfred Perlstein 3eae561b3SGarrett Wollman /* 4eae561b3SGarrett Wollman * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 5eae561b3SGarrett Wollman * unrestricted use provided that this legend is included on all tape 6eae561b3SGarrett Wollman * media and as a part of the software program in whole or part. Users 7eae561b3SGarrett Wollman * may copy or modify Sun RPC without charge, but are not authorized 8eae561b3SGarrett Wollman * to license or distribute it to anyone else except as part of a product or 9eae561b3SGarrett Wollman * program developed by the user. 10eae561b3SGarrett Wollman * 11eae561b3SGarrett Wollman * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 12eae561b3SGarrett Wollman * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 13eae561b3SGarrett Wollman * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 14eae561b3SGarrett Wollman * 15eae561b3SGarrett Wollman * Sun RPC is provided with no support and without any obligation on the 16eae561b3SGarrett Wollman * part of Sun Microsystems, Inc. to assist in its use, correction, 17eae561b3SGarrett Wollman * modification or enhancement. 18eae561b3SGarrett Wollman * 19eae561b3SGarrett Wollman * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 20eae561b3SGarrett Wollman * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 21eae561b3SGarrett Wollman * OR ANY PART THEREOF. 22eae561b3SGarrett Wollman * 23eae561b3SGarrett Wollman * In no event will Sun Microsystems, Inc. be liable for any lost revenue 24eae561b3SGarrett Wollman * or profits or other special, indirect and consequential damages, even if 25eae561b3SGarrett Wollman * Sun has been advised of the possibility of such damages. 26eae561b3SGarrett Wollman * 27eae561b3SGarrett Wollman * Sun Microsystems, Inc. 28eae561b3SGarrett Wollman * 2550 Garcia Avenue 29eae561b3SGarrett Wollman * Mountain View, California 94043 30eae561b3SGarrett Wollman */ 31eae561b3SGarrett Wollman 32eae561b3SGarrett Wollman #if defined(LIBC_SCCS) && !defined(lint) 33a9bdcd37SDavid E. O'Brien static char *sccsid2 = "@(#)xdr_stdio.c 1.16 87/08/11 Copyr 1984 Sun Micro"; 34333fc21eSDavid E. O'Brien static char *sccsid = "@(#)xdr_stdio.c 2.1 88/07/29 4.0 RPCSRC"; 35eae561b3SGarrett Wollman #endif 36333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 37333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 38eae561b3SGarrett Wollman 39eae561b3SGarrett Wollman /* 40eae561b3SGarrett Wollman * xdr_stdio.c, XDR implementation on standard i/o file. 41eae561b3SGarrett Wollman * 42eae561b3SGarrett Wollman * Copyright (C) 1984, Sun Microsystems, Inc. 43eae561b3SGarrett Wollman * 44eae561b3SGarrett Wollman * This set of routines implements a XDR on a stdio stream. 45eae561b3SGarrett Wollman * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes 46eae561b3SGarrett Wollman * from the stream. 47eae561b3SGarrett Wollman */ 48eae561b3SGarrett Wollman 498360efbdSAlfred Perlstein #include "namespace.h" 50eae561b3SGarrett Wollman #include <stdio.h> 51eae561b3SGarrett Wollman 52fd8e4ebcSMike Barcroft #include <arpa/inet.h> 538360efbdSAlfred Perlstein #include <rpc/types.h> 548360efbdSAlfred Perlstein #include <rpc/xdr.h> 558360efbdSAlfred Perlstein #include "un-namespace.h" 568360efbdSAlfred Perlstein 57c05ac53bSDavid E. O'Brien static void xdrstdio_destroy(XDR *); 58c05ac53bSDavid E. O'Brien static bool_t xdrstdio_getlong(XDR *, long *); 59c05ac53bSDavid E. O'Brien static bool_t xdrstdio_putlong(XDR *, const long *); 60c05ac53bSDavid E. O'Brien static bool_t xdrstdio_getbytes(XDR *, char *, u_int); 61c05ac53bSDavid E. O'Brien static bool_t xdrstdio_putbytes(XDR *, const char *, u_int); 62c05ac53bSDavid E. O'Brien static u_int xdrstdio_getpos(XDR *); 63c05ac53bSDavid E. O'Brien static bool_t xdrstdio_setpos(XDR *, u_int); 64c05ac53bSDavid E. O'Brien static int32_t *xdrstdio_inline(XDR *, u_int); 65eae561b3SGarrett Wollman 66eae561b3SGarrett Wollman /* 67eae561b3SGarrett Wollman * Ops vector for stdio type XDR 68eae561b3SGarrett Wollman */ 698360efbdSAlfred Perlstein static const struct xdr_ops xdrstdio_ops = { 70eae561b3SGarrett Wollman xdrstdio_getlong, /* deseraialize a long int */ 71eae561b3SGarrett Wollman xdrstdio_putlong, /* seraialize a long int */ 72eae561b3SGarrett Wollman xdrstdio_getbytes, /* deserialize counted bytes */ 73eae561b3SGarrett Wollman xdrstdio_putbytes, /* serialize counted bytes */ 74eae561b3SGarrett Wollman xdrstdio_getpos, /* get offset in the stream */ 75eae561b3SGarrett Wollman xdrstdio_setpos, /* set offset in the stream */ 76eae561b3SGarrett Wollman xdrstdio_inline, /* prime stream for inline macros */ 77eae561b3SGarrett Wollman xdrstdio_destroy /* destroy stream */ 78eae561b3SGarrett Wollman }; 79eae561b3SGarrett Wollman 80eae561b3SGarrett Wollman /* 81eae561b3SGarrett Wollman * Initialize a stdio xdr stream. 82eae561b3SGarrett Wollman * Sets the xdr stream handle xdrs for use on the stream file. 83eae561b3SGarrett Wollman * Operation flag is set to op. 84eae561b3SGarrett Wollman */ 85eae561b3SGarrett Wollman void 86eae561b3SGarrett Wollman xdrstdio_create(xdrs, file, op) 878360efbdSAlfred Perlstein XDR *xdrs; 88eae561b3SGarrett Wollman FILE *file; 89eae561b3SGarrett Wollman enum xdr_op op; 90eae561b3SGarrett Wollman { 91eae561b3SGarrett Wollman 92eae561b3SGarrett Wollman xdrs->x_op = op; 93eae561b3SGarrett Wollman xdrs->x_ops = &xdrstdio_ops; 948360efbdSAlfred Perlstein xdrs->x_private = file; 95eae561b3SGarrett Wollman xdrs->x_handy = 0; 96eae561b3SGarrett Wollman xdrs->x_base = 0; 97eae561b3SGarrett Wollman } 98eae561b3SGarrett Wollman 99eae561b3SGarrett Wollman /* 100eae561b3SGarrett Wollman * Destroy a stdio xdr stream. 101eae561b3SGarrett Wollman * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create. 102eae561b3SGarrett Wollman */ 103eae561b3SGarrett Wollman static void 104eae561b3SGarrett Wollman xdrstdio_destroy(xdrs) 1058360efbdSAlfred Perlstein XDR *xdrs; 106eae561b3SGarrett Wollman { 107eae561b3SGarrett Wollman (void)fflush((FILE *)xdrs->x_private); 1088360efbdSAlfred Perlstein /* XXX: should we close the file ?? */ 1091ad08a09SPeter Wemm } 110eae561b3SGarrett Wollman 111eae561b3SGarrett Wollman static bool_t 112eae561b3SGarrett Wollman xdrstdio_getlong(xdrs, lp) 113eae561b3SGarrett Wollman XDR *xdrs; 1148360efbdSAlfred Perlstein long *lp; 115eae561b3SGarrett Wollman { 11609c37f5dSMartin Blapp u_int32_t temp; 117eae561b3SGarrett Wollman 11809c37f5dSMartin Blapp if (fread(&temp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1) 119eae561b3SGarrett Wollman return (FALSE); 12009c37f5dSMartin Blapp *lp = (long)ntohl(temp); 121eae561b3SGarrett Wollman return (TRUE); 122eae561b3SGarrett Wollman } 123eae561b3SGarrett Wollman 124eae561b3SGarrett Wollman static bool_t 125eae561b3SGarrett Wollman xdrstdio_putlong(xdrs, lp) 126eae561b3SGarrett Wollman XDR *xdrs; 1278360efbdSAlfred Perlstein const long *lp; 128eae561b3SGarrett Wollman { 12909c37f5dSMartin Blapp int32_t mycopy = htonl((u_int32_t)*lp); 130eae561b3SGarrett Wollman 1318360efbdSAlfred Perlstein if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1) 132eae561b3SGarrett Wollman return (FALSE); 133eae561b3SGarrett Wollman return (TRUE); 134eae561b3SGarrett Wollman } 135eae561b3SGarrett Wollman 136eae561b3SGarrett Wollman static bool_t 137eae561b3SGarrett Wollman xdrstdio_getbytes(xdrs, addr, len) 138eae561b3SGarrett Wollman XDR *xdrs; 1398360efbdSAlfred Perlstein char *addr; 140eae561b3SGarrett Wollman u_int len; 141eae561b3SGarrett Wollman { 142eae561b3SGarrett Wollman 1438360efbdSAlfred Perlstein if ((len != 0) && (fread(addr, (size_t)len, 1, (FILE *)xdrs->x_private) != 1)) 144eae561b3SGarrett Wollman return (FALSE); 145eae561b3SGarrett Wollman return (TRUE); 146eae561b3SGarrett Wollman } 147eae561b3SGarrett Wollman 148eae561b3SGarrett Wollman static bool_t 149eae561b3SGarrett Wollman xdrstdio_putbytes(xdrs, addr, len) 150eae561b3SGarrett Wollman XDR *xdrs; 1518360efbdSAlfred Perlstein const char *addr; 152eae561b3SGarrett Wollman u_int len; 153eae561b3SGarrett Wollman { 154eae561b3SGarrett Wollman 1558360efbdSAlfred Perlstein if ((len != 0) && (fwrite(addr, (size_t)len, 1, 1568360efbdSAlfred Perlstein (FILE *)xdrs->x_private) != 1)) 157eae561b3SGarrett Wollman return (FALSE); 158eae561b3SGarrett Wollman return (TRUE); 159eae561b3SGarrett Wollman } 160eae561b3SGarrett Wollman 161eae561b3SGarrett Wollman static u_int 162eae561b3SGarrett Wollman xdrstdio_getpos(xdrs) 163eae561b3SGarrett Wollman XDR *xdrs; 164eae561b3SGarrett Wollman { 165eae561b3SGarrett Wollman 166eae561b3SGarrett Wollman return ((u_int) ftell((FILE *)xdrs->x_private)); 167eae561b3SGarrett Wollman } 168eae561b3SGarrett Wollman 169eae561b3SGarrett Wollman static bool_t 170eae561b3SGarrett Wollman xdrstdio_setpos(xdrs, pos) 171eae561b3SGarrett Wollman XDR *xdrs; 172eae561b3SGarrett Wollman u_int pos; 173eae561b3SGarrett Wollman { 174eae561b3SGarrett Wollman 175eae561b3SGarrett Wollman return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ? 176eae561b3SGarrett Wollman FALSE : TRUE); 177eae561b3SGarrett Wollman } 178eae561b3SGarrett Wollman 1798360efbdSAlfred Perlstein /* ARGSUSED */ 1801ad08a09SPeter Wemm static int32_t * 181eae561b3SGarrett Wollman xdrstdio_inline(xdrs, len) 182eae561b3SGarrett Wollman XDR *xdrs; 183eae561b3SGarrett Wollman u_int len; 184eae561b3SGarrett Wollman { 185eae561b3SGarrett Wollman 186eae561b3SGarrett Wollman /* 187eae561b3SGarrett Wollman * Must do some work to implement this: must insure 188eae561b3SGarrett Wollman * enough data in the underlying stdio buffer, 189eae561b3SGarrett Wollman * that the buffer is aligned so that we can indirect through a 190eae561b3SGarrett Wollman * long *, and stuff this pointer in xdrs->x_buf. Doing 191eae561b3SGarrett Wollman * a fread or fwrite to a scratch buffer would defeat 192eae561b3SGarrett Wollman * most of the gains to be had here and require storage 193eae561b3SGarrett Wollman * management on this buffer, so we don't do this. 194eae561b3SGarrett Wollman */ 195eae561b3SGarrett Wollman return (NULL); 196eae561b3SGarrett Wollman } 197