1*84d9c625SLionel Sambuc /* $NetBSD: xdr_stdio.c,v 1.19 2013/03/11 20:19:30 tron Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
4*84d9c625SLionel Sambuc * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras *
6*84d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
7*84d9c625SLionel Sambuc * modification, are permitted provided that the following conditions are
8*84d9c625SLionel Sambuc * met:
92fe8fb19SBen Gras *
10*84d9c625SLionel Sambuc * * Redistributions of source code must retain the above copyright
11*84d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*84d9c625SLionel Sambuc * * Redistributions in binary form must reproduce the above
13*84d9c625SLionel Sambuc * copyright notice, this list of conditions and the following
14*84d9c625SLionel Sambuc * disclaimer in the documentation and/or other materials
15*84d9c625SLionel Sambuc * provided with the distribution.
16*84d9c625SLionel Sambuc * * Neither the name of the "Oracle America, Inc." nor the names of its
17*84d9c625SLionel Sambuc * contributors may be used to endorse or promote products derived
18*84d9c625SLionel Sambuc * from this software without specific prior written permission.
192fe8fb19SBen Gras *
20*84d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*84d9c625SLionel Sambuc * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*84d9c625SLionel Sambuc * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*84d9c625SLionel Sambuc * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*84d9c625SLionel Sambuc * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*84d9c625SLionel Sambuc * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*84d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*84d9c625SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*84d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*84d9c625SLionel Sambuc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*84d9c625SLionel Sambuc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*84d9c625SLionel Sambuc * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras */
332fe8fb19SBen Gras
342fe8fb19SBen Gras #include <sys/cdefs.h>
352fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
362fe8fb19SBen Gras #if 0
372fe8fb19SBen Gras static char *sccsid = "@(#)xdr_stdio.c 1.16 87/08/11 Copyr 1984 Sun Micro";
382fe8fb19SBen Gras static char *sccsid = "@(#)xdr_stdio.c 2.1 88/07/29 4.0 RPCSRC";
392fe8fb19SBen Gras #else
40*84d9c625SLionel Sambuc __RCSID("$NetBSD: xdr_stdio.c,v 1.19 2013/03/11 20:19:30 tron Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras
442fe8fb19SBen Gras /*
452fe8fb19SBen Gras * xdr_stdio.c, XDR implementation on standard i/o file.
462fe8fb19SBen Gras *
472fe8fb19SBen Gras * Copyright (C) 1984, Sun Microsystems, Inc.
482fe8fb19SBen Gras *
492fe8fb19SBen Gras * This set of routines implements a XDR on a stdio stream.
502fe8fb19SBen Gras * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
512fe8fb19SBen Gras * from the stream.
522fe8fb19SBen Gras */
532fe8fb19SBen Gras
542fe8fb19SBen Gras #include "namespace.h"
552fe8fb19SBen Gras
562fe8fb19SBen Gras #include <stdio.h>
572fe8fb19SBen Gras
582fe8fb19SBen Gras #include <rpc/types.h>
592fe8fb19SBen Gras #include <rpc/xdr.h>
602fe8fb19SBen Gras
612fe8fb19SBen Gras #ifdef __weak_alias
622fe8fb19SBen Gras __weak_alias(xdrstdio_create,_xdrstdio_create)
632fe8fb19SBen Gras #endif
642fe8fb19SBen Gras
65f14fb602SLionel Sambuc static void xdrstdio_destroy(XDR *);
66f14fb602SLionel Sambuc static bool_t xdrstdio_getlong(XDR *, long *);
67f14fb602SLionel Sambuc static bool_t xdrstdio_putlong(XDR *, const long *);
68f14fb602SLionel Sambuc static bool_t xdrstdio_getbytes(XDR *, char *, u_int);
69f14fb602SLionel Sambuc static bool_t xdrstdio_putbytes(XDR *, const char *, u_int);
70f14fb602SLionel Sambuc static u_int xdrstdio_getpos(XDR *);
71f14fb602SLionel Sambuc static bool_t xdrstdio_setpos(XDR *, u_int);
72f14fb602SLionel Sambuc static int32_t *xdrstdio_inline(XDR *, u_int);
732fe8fb19SBen Gras
742fe8fb19SBen Gras /*
752fe8fb19SBen Gras * Ops vector for stdio type XDR
762fe8fb19SBen Gras */
772fe8fb19SBen Gras static const struct xdr_ops xdrstdio_ops = {
782fe8fb19SBen Gras xdrstdio_getlong, /* deseraialize a long int */
792fe8fb19SBen Gras xdrstdio_putlong, /* seraialize a long int */
802fe8fb19SBen Gras xdrstdio_getbytes, /* deserialize counted bytes */
812fe8fb19SBen Gras xdrstdio_putbytes, /* serialize counted bytes */
822fe8fb19SBen Gras xdrstdio_getpos, /* get offset in the stream */
832fe8fb19SBen Gras xdrstdio_setpos, /* set offset in the stream */
842fe8fb19SBen Gras xdrstdio_inline, /* prime stream for inline macros */
852fe8fb19SBen Gras xdrstdio_destroy, /* destroy stream */
862fe8fb19SBen Gras NULL, /* xdrstdio_control */
872fe8fb19SBen Gras };
882fe8fb19SBen Gras
892fe8fb19SBen Gras /*
902fe8fb19SBen Gras * Initialize a stdio xdr stream.
912fe8fb19SBen Gras * Sets the xdr stream handle xdrs for use on the stream file.
922fe8fb19SBen Gras * Operation flag is set to op.
932fe8fb19SBen Gras */
942fe8fb19SBen Gras void
xdrstdio_create(XDR * xdrs,FILE * file,enum xdr_op op)95f14fb602SLionel Sambuc xdrstdio_create(XDR *xdrs, FILE *file, enum xdr_op op)
962fe8fb19SBen Gras {
972fe8fb19SBen Gras
982fe8fb19SBen Gras xdrs->x_op = op;
992fe8fb19SBen Gras xdrs->x_ops = &xdrstdio_ops;
1002fe8fb19SBen Gras xdrs->x_private = file;
1012fe8fb19SBen Gras xdrs->x_handy = 0;
1022fe8fb19SBen Gras xdrs->x_base = 0;
1032fe8fb19SBen Gras }
1042fe8fb19SBen Gras
1052fe8fb19SBen Gras /*
1062fe8fb19SBen Gras * Destroy a stdio xdr stream.
1072fe8fb19SBen Gras * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
1082fe8fb19SBen Gras */
1092fe8fb19SBen Gras static void
xdrstdio_destroy(XDR * xdrs)110f14fb602SLionel Sambuc xdrstdio_destroy(XDR *xdrs)
1112fe8fb19SBen Gras {
1122fe8fb19SBen Gras (void)fflush((FILE *)xdrs->x_private);
1132fe8fb19SBen Gras /* XXX: should we close the file ?? */
1142fe8fb19SBen Gras }
1152fe8fb19SBen Gras
1162fe8fb19SBen Gras static bool_t
xdrstdio_getlong(XDR * xdrs,long * lp)117f14fb602SLionel Sambuc xdrstdio_getlong(XDR *xdrs, long *lp)
1182fe8fb19SBen Gras {
1192fe8fb19SBen Gras u_int32_t temp;
1202fe8fb19SBen Gras
1212fe8fb19SBen Gras if (fread(&temp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
1222fe8fb19SBen Gras return (FALSE);
1232fe8fb19SBen Gras *lp = (long)ntohl(temp);
1242fe8fb19SBen Gras return (TRUE);
1252fe8fb19SBen Gras }
1262fe8fb19SBen Gras
1272fe8fb19SBen Gras static bool_t
xdrstdio_putlong(XDR * xdrs,const long * lp)128f14fb602SLionel Sambuc xdrstdio_putlong(XDR *xdrs, const long *lp)
1292fe8fb19SBen Gras {
1302fe8fb19SBen Gras int32_t mycopy = htonl((u_int32_t)*lp);
1312fe8fb19SBen Gras
1322fe8fb19SBen Gras if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
1332fe8fb19SBen Gras return (FALSE);
1342fe8fb19SBen Gras return (TRUE);
1352fe8fb19SBen Gras }
1362fe8fb19SBen Gras
1372fe8fb19SBen Gras static bool_t
xdrstdio_getbytes(XDR * xdrs,char * addr,u_int len)138f14fb602SLionel Sambuc xdrstdio_getbytes(XDR *xdrs, char *addr, u_int len)
1392fe8fb19SBen Gras {
1402fe8fb19SBen Gras
1412fe8fb19SBen Gras if ((len != 0) && (fread(addr, (size_t)len, 1, (FILE *)xdrs->x_private) != 1))
1422fe8fb19SBen Gras return (FALSE);
1432fe8fb19SBen Gras return (TRUE);
1442fe8fb19SBen Gras }
1452fe8fb19SBen Gras
1462fe8fb19SBen Gras static bool_t
xdrstdio_putbytes(XDR * xdrs,const char * addr,u_int len)147f14fb602SLionel Sambuc xdrstdio_putbytes(XDR *xdrs, const char *addr, u_int len)
1482fe8fb19SBen Gras {
1492fe8fb19SBen Gras
1502fe8fb19SBen Gras if ((len != 0) && (fwrite(addr, (size_t)len, 1,
1512fe8fb19SBen Gras (FILE *)xdrs->x_private) != 1))
1522fe8fb19SBen Gras return (FALSE);
1532fe8fb19SBen Gras return (TRUE);
1542fe8fb19SBen Gras }
1552fe8fb19SBen Gras
1562fe8fb19SBen Gras static u_int
xdrstdio_getpos(XDR * xdrs)157f14fb602SLionel Sambuc xdrstdio_getpos(XDR *xdrs)
1582fe8fb19SBen Gras {
1592fe8fb19SBen Gras
1602fe8fb19SBen Gras return ((u_int) ftell((FILE *)xdrs->x_private));
1612fe8fb19SBen Gras }
1622fe8fb19SBen Gras
1632fe8fb19SBen Gras static bool_t
xdrstdio_setpos(XDR * xdrs,u_int pos)164f14fb602SLionel Sambuc xdrstdio_setpos(XDR *xdrs, u_int pos)
1652fe8fb19SBen Gras {
1662fe8fb19SBen Gras
1672fe8fb19SBen Gras return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ?
1682fe8fb19SBen Gras FALSE : TRUE);
1692fe8fb19SBen Gras }
1702fe8fb19SBen Gras
1712fe8fb19SBen Gras /* ARGSUSED */
1722fe8fb19SBen Gras static int32_t *
xdrstdio_inline(XDR * xdrs,u_int len)173f14fb602SLionel Sambuc xdrstdio_inline(XDR *xdrs, u_int len)
1742fe8fb19SBen Gras {
1752fe8fb19SBen Gras
1762fe8fb19SBen Gras /*
1772fe8fb19SBen Gras * Must do some work to implement this: must insure
1782fe8fb19SBen Gras * enough data in the underlying stdio buffer,
1792fe8fb19SBen Gras * that the buffer is aligned so that we can indirect through a
1802fe8fb19SBen Gras * long *, and stuff this pointer in xdrs->x_buf. Doing
1812fe8fb19SBen Gras * a fread or fwrite to a scratch buffer would defeat
1822fe8fb19SBen Gras * most of the gains to be had here and require storage
1832fe8fb19SBen Gras * management on this buffer, so we don't do this.
1842fe8fb19SBen Gras */
1852fe8fb19SBen Gras return (NULL);
1862fe8fb19SBen Gras }
187