18360efbdSAlfred Perlstein /* $NetBSD: xdr_stdio.c,v 1.14 2000/01/22 22:19:19 mycroft Exp $ */ 28360efbdSAlfred Perlstein 3a204967aSHiroki Sato /*- 4a204967aSHiroki Sato * Copyright (c) 2010, Oracle America, Inc. 5eae561b3SGarrett Wollman * 6a204967aSHiroki Sato * Redistribution and use in source and binary forms, with or without 7a204967aSHiroki Sato * modification, are permitted provided that the following conditions are 8a204967aSHiroki Sato * met: 9eae561b3SGarrett Wollman * 10a204967aSHiroki Sato * * Redistributions of source code must retain the above copyright 11a204967aSHiroki Sato * notice, this list of conditions and the following disclaimer. 12a204967aSHiroki Sato * * Redistributions in binary form must reproduce the above 13a204967aSHiroki Sato * copyright notice, this list of conditions and the following 14a204967aSHiroki Sato * disclaimer in the documentation and/or other materials 15a204967aSHiroki Sato * provided with the distribution. 16a204967aSHiroki Sato * * Neither the name of the "Oracle America, Inc." nor the names of its 17a204967aSHiroki Sato * contributors may be used to endorse or promote products derived 18a204967aSHiroki Sato * from this software without specific prior written permission. 19eae561b3SGarrett Wollman * 20a204967aSHiroki Sato * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21a204967aSHiroki Sato * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22a204967aSHiroki Sato * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23a204967aSHiroki Sato * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24a204967aSHiroki Sato * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 25a204967aSHiroki Sato * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26a204967aSHiroki Sato * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 27a204967aSHiroki Sato * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28a204967aSHiroki Sato * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29a204967aSHiroki Sato * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30a204967aSHiroki Sato * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31a204967aSHiroki Sato * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32eae561b3SGarrett Wollman */ 33eae561b3SGarrett Wollman 34eae561b3SGarrett Wollman #if defined(LIBC_SCCS) && !defined(lint) 35a9bdcd37SDavid E. O'Brien static char *sccsid2 = "@(#)xdr_stdio.c 1.16 87/08/11 Copyr 1984 Sun Micro"; 36333fc21eSDavid E. O'Brien static char *sccsid = "@(#)xdr_stdio.c 2.1 88/07/29 4.0 RPCSRC"; 37eae561b3SGarrett Wollman #endif 38333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 39333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 40eae561b3SGarrett Wollman 41eae561b3SGarrett Wollman /* 42eae561b3SGarrett Wollman * xdr_stdio.c, XDR implementation on standard i/o file. 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 86*d660d38dSCraig Rodrigues xdrstdio_create(XDR *xdrs, FILE *file, enum xdr_op op) 87eae561b3SGarrett Wollman { 88eae561b3SGarrett Wollman 89eae561b3SGarrett Wollman xdrs->x_op = op; 90eae561b3SGarrett Wollman xdrs->x_ops = &xdrstdio_ops; 918360efbdSAlfred Perlstein xdrs->x_private = file; 92eae561b3SGarrett Wollman xdrs->x_handy = 0; 93eae561b3SGarrett Wollman xdrs->x_base = 0; 94eae561b3SGarrett Wollman } 95eae561b3SGarrett Wollman 96eae561b3SGarrett Wollman /* 97eae561b3SGarrett Wollman * Destroy a stdio xdr stream. 98eae561b3SGarrett Wollman * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create. 99eae561b3SGarrett Wollman */ 100eae561b3SGarrett Wollman static void 101*d660d38dSCraig Rodrigues xdrstdio_destroy(XDR *xdrs) 102eae561b3SGarrett Wollman { 103eae561b3SGarrett Wollman (void)fflush((FILE *)xdrs->x_private); 1048360efbdSAlfred Perlstein /* XXX: should we close the file ?? */ 1051ad08a09SPeter Wemm } 106eae561b3SGarrett Wollman 107eae561b3SGarrett Wollman static bool_t 108*d660d38dSCraig Rodrigues xdrstdio_getlong(XDR *xdrs, long *lp) 109eae561b3SGarrett Wollman { 11009c37f5dSMartin Blapp u_int32_t temp; 111eae561b3SGarrett Wollman 11209c37f5dSMartin Blapp if (fread(&temp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1) 113eae561b3SGarrett Wollman return (FALSE); 11409c37f5dSMartin Blapp *lp = (long)ntohl(temp); 115eae561b3SGarrett Wollman return (TRUE); 116eae561b3SGarrett Wollman } 117eae561b3SGarrett Wollman 118eae561b3SGarrett Wollman static bool_t 119*d660d38dSCraig Rodrigues xdrstdio_putlong(XDR *xdrs, const long *lp) 120eae561b3SGarrett Wollman { 12109c37f5dSMartin Blapp int32_t mycopy = htonl((u_int32_t)*lp); 122eae561b3SGarrett Wollman 1238360efbdSAlfred Perlstein if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1) 124eae561b3SGarrett Wollman return (FALSE); 125eae561b3SGarrett Wollman return (TRUE); 126eae561b3SGarrett Wollman } 127eae561b3SGarrett Wollman 128eae561b3SGarrett Wollman static bool_t 129*d660d38dSCraig Rodrigues xdrstdio_getbytes(XDR *xdrs, char *addr, u_int len) 130eae561b3SGarrett Wollman { 131eae561b3SGarrett Wollman 1328360efbdSAlfred Perlstein if ((len != 0) && (fread(addr, (size_t)len, 1, (FILE *)xdrs->x_private) != 1)) 133eae561b3SGarrett Wollman return (FALSE); 134eae561b3SGarrett Wollman return (TRUE); 135eae561b3SGarrett Wollman } 136eae561b3SGarrett Wollman 137eae561b3SGarrett Wollman static bool_t 138*d660d38dSCraig Rodrigues xdrstdio_putbytes(XDR *xdrs, const char *addr, u_int len) 139eae561b3SGarrett Wollman { 140eae561b3SGarrett Wollman 1418360efbdSAlfred Perlstein if ((len != 0) && (fwrite(addr, (size_t)len, 1, 1428360efbdSAlfred Perlstein (FILE *)xdrs->x_private) != 1)) 143eae561b3SGarrett Wollman return (FALSE); 144eae561b3SGarrett Wollman return (TRUE); 145eae561b3SGarrett Wollman } 146eae561b3SGarrett Wollman 147eae561b3SGarrett Wollman static u_int 148*d660d38dSCraig Rodrigues xdrstdio_getpos(XDR *xdrs) 149eae561b3SGarrett Wollman { 150eae561b3SGarrett Wollman 151eae561b3SGarrett Wollman return ((u_int) ftell((FILE *)xdrs->x_private)); 152eae561b3SGarrett Wollman } 153eae561b3SGarrett Wollman 154eae561b3SGarrett Wollman static bool_t 155*d660d38dSCraig Rodrigues xdrstdio_setpos(XDR *xdrs, u_int pos) 156eae561b3SGarrett Wollman { 157eae561b3SGarrett Wollman 158eae561b3SGarrett Wollman return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ? 159eae561b3SGarrett Wollman FALSE : TRUE); 160eae561b3SGarrett Wollman } 161eae561b3SGarrett Wollman 1628360efbdSAlfred Perlstein /* ARGSUSED */ 1631ad08a09SPeter Wemm static int32_t * 164*d660d38dSCraig Rodrigues xdrstdio_inline(XDR *xdrs, u_int len) 165eae561b3SGarrett Wollman { 166eae561b3SGarrett Wollman 167eae561b3SGarrett Wollman /* 168eae561b3SGarrett Wollman * Must do some work to implement this: must insure 169eae561b3SGarrett Wollman * enough data in the underlying stdio buffer, 170eae561b3SGarrett Wollman * that the buffer is aligned so that we can indirect through a 171eae561b3SGarrett Wollman * long *, and stuff this pointer in xdrs->x_buf. Doing 172eae561b3SGarrett Wollman * a fread or fwrite to a scratch buffer would defeat 173eae561b3SGarrett Wollman * most of the gains to be had here and require storage 174eae561b3SGarrett Wollman * management on this buffer, so we don't do this. 175eae561b3SGarrett Wollman */ 176eae561b3SGarrett Wollman return (NULL); 177eae561b3SGarrett Wollman } 178