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 328360efbdSAlfred Perlstein #include <sys/cdefs.h> 33eae561b3SGarrett Wollman #if defined(LIBC_SCCS) && !defined(lint) 34eae561b3SGarrett Wollman /*static char *sccsid = "from: @(#)xdr_stdio.c 1.16 87/08/11 Copyr 1984 Sun Micro";*/ 35eae561b3SGarrett Wollman /*static char *sccsid = "from: @(#)xdr_stdio.c 2.1 88/07/29 4.0 RPCSRC";*/ 367f3dea24SPeter Wemm static char *rcsid = "$FreeBSD$"; 37eae561b3SGarrett Wollman #endif 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 { 116eae561b3SGarrett Wollman 1178360efbdSAlfred Perlstein if (fread(lp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1) 118eae561b3SGarrett Wollman return (FALSE); 1198360efbdSAlfred Perlstein *lp = (long)ntohl((u_int32_t)*lp); 120eae561b3SGarrett Wollman return (TRUE); 121eae561b3SGarrett Wollman } 122eae561b3SGarrett Wollman 123eae561b3SGarrett Wollman static bool_t 124eae561b3SGarrett Wollman xdrstdio_putlong(xdrs, lp) 125eae561b3SGarrett Wollman XDR *xdrs; 1268360efbdSAlfred Perlstein const long *lp; 127eae561b3SGarrett Wollman { 1288360efbdSAlfred Perlstein long mycopy = (long)htonl((u_int32_t)*lp); 129eae561b3SGarrett Wollman 1308360efbdSAlfred Perlstein if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1) 131eae561b3SGarrett Wollman return (FALSE); 132eae561b3SGarrett Wollman return (TRUE); 133eae561b3SGarrett Wollman } 134eae561b3SGarrett Wollman 135eae561b3SGarrett Wollman static bool_t 136eae561b3SGarrett Wollman xdrstdio_getbytes(xdrs, addr, len) 137eae561b3SGarrett Wollman XDR *xdrs; 1388360efbdSAlfred Perlstein char *addr; 139eae561b3SGarrett Wollman u_int len; 140eae561b3SGarrett Wollman { 141eae561b3SGarrett Wollman 1428360efbdSAlfred Perlstein if ((len != 0) && (fread(addr, (size_t)len, 1, (FILE *)xdrs->x_private) != 1)) 143eae561b3SGarrett Wollman return (FALSE); 144eae561b3SGarrett Wollman return (TRUE); 145eae561b3SGarrett Wollman } 146eae561b3SGarrett Wollman 147eae561b3SGarrett Wollman static bool_t 148eae561b3SGarrett Wollman xdrstdio_putbytes(xdrs, addr, len) 149eae561b3SGarrett Wollman XDR *xdrs; 1508360efbdSAlfred Perlstein const char *addr; 151eae561b3SGarrett Wollman u_int len; 152eae561b3SGarrett Wollman { 153eae561b3SGarrett Wollman 1548360efbdSAlfred Perlstein if ((len != 0) && (fwrite(addr, (size_t)len, 1, 1558360efbdSAlfred Perlstein (FILE *)xdrs->x_private) != 1)) 156eae561b3SGarrett Wollman return (FALSE); 157eae561b3SGarrett Wollman return (TRUE); 158eae561b3SGarrett Wollman } 159eae561b3SGarrett Wollman 160eae561b3SGarrett Wollman static u_int 161eae561b3SGarrett Wollman xdrstdio_getpos(xdrs) 162eae561b3SGarrett Wollman XDR *xdrs; 163eae561b3SGarrett Wollman { 164eae561b3SGarrett Wollman 165eae561b3SGarrett Wollman return ((u_int) ftell((FILE *)xdrs->x_private)); 166eae561b3SGarrett Wollman } 167eae561b3SGarrett Wollman 168eae561b3SGarrett Wollman static bool_t 169eae561b3SGarrett Wollman xdrstdio_setpos(xdrs, pos) 170eae561b3SGarrett Wollman XDR *xdrs; 171eae561b3SGarrett Wollman u_int pos; 172eae561b3SGarrett Wollman { 173eae561b3SGarrett Wollman 174eae561b3SGarrett Wollman return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ? 175eae561b3SGarrett Wollman FALSE : TRUE); 176eae561b3SGarrett Wollman } 177eae561b3SGarrett Wollman 1788360efbdSAlfred Perlstein /* ARGSUSED */ 1791ad08a09SPeter Wemm static int32_t * 180eae561b3SGarrett Wollman xdrstdio_inline(xdrs, len) 181eae561b3SGarrett Wollman XDR *xdrs; 182eae561b3SGarrett Wollman u_int len; 183eae561b3SGarrett Wollman { 184eae561b3SGarrett Wollman 185eae561b3SGarrett Wollman /* 186eae561b3SGarrett Wollman * Must do some work to implement this: must insure 187eae561b3SGarrett Wollman * enough data in the underlying stdio buffer, 188eae561b3SGarrett Wollman * that the buffer is aligned so that we can indirect through a 189eae561b3SGarrett Wollman * long *, and stuff this pointer in xdrs->x_buf. Doing 190eae561b3SGarrett Wollman * a fread or fwrite to a scratch buffer would defeat 191eae561b3SGarrett Wollman * most of the gains to be had here and require storage 192eae561b3SGarrett Wollman * management on this buffer, so we don't do this. 193eae561b3SGarrett Wollman */ 194eae561b3SGarrett Wollman return (NULL); 195eae561b3SGarrett Wollman } 196