1*47c0e0c3Stron /* $NetBSD: xdr_stdio.c,v 1.19 2013/03/11 20:19:30 tron Exp $ */
29e15c989Scgd
363d7b677Scgd /*
4*47c0e0c3Stron * Copyright (c) 2010, Oracle America, Inc.
563d7b677Scgd *
6*47c0e0c3Stron * Redistribution and use in source and binary forms, with or without
7*47c0e0c3Stron * modification, are permitted provided that the following conditions are
8*47c0e0c3Stron * met:
963d7b677Scgd *
10*47c0e0c3Stron * * Redistributions of source code must retain the above copyright
11*47c0e0c3Stron * notice, this list of conditions and the following disclaimer.
12*47c0e0c3Stron * * Redistributions in binary form must reproduce the above
13*47c0e0c3Stron * copyright notice, this list of conditions and the following
14*47c0e0c3Stron * disclaimer in the documentation and/or other materials
15*47c0e0c3Stron * provided with the distribution.
16*47c0e0c3Stron * * Neither the name of the "Oracle America, Inc." nor the names of its
17*47c0e0c3Stron * contributors may be used to endorse or promote products derived
18*47c0e0c3Stron * from this software without specific prior written permission.
1963d7b677Scgd *
20*47c0e0c3Stron * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*47c0e0c3Stron * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*47c0e0c3Stron * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*47c0e0c3Stron * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*47c0e0c3Stron * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*47c0e0c3Stron * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*47c0e0c3Stron * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*47c0e0c3Stron * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*47c0e0c3Stron * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*47c0e0c3Stron * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*47c0e0c3Stron * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*47c0e0c3Stron * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3263d7b677Scgd */
3363d7b677Scgd
34c63c52b2Schristos #include <sys/cdefs.h>
3563d7b677Scgd #if defined(LIBC_SCCS) && !defined(lint)
36c63c52b2Schristos #if 0
37c63c52b2Schristos static char *sccsid = "@(#)xdr_stdio.c 1.16 87/08/11 Copyr 1984 Sun Micro";
38c63c52b2Schristos static char *sccsid = "@(#)xdr_stdio.c 2.1 88/07/29 4.0 RPCSRC";
39c63c52b2Schristos #else
40*47c0e0c3Stron __RCSID("$NetBSD: xdr_stdio.c,v 1.19 2013/03/11 20:19:30 tron Exp $");
41c63c52b2Schristos #endif
4263d7b677Scgd #endif
4363d7b677Scgd
4463d7b677Scgd /*
4563d7b677Scgd * xdr_stdio.c, XDR implementation on standard i/o file.
4663d7b677Scgd *
4763d7b677Scgd * Copyright (C) 1984, Sun Microsystems, Inc.
4863d7b677Scgd *
4963d7b677Scgd * This set of routines implements a XDR on a stdio stream.
5063d7b677Scgd * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
5163d7b677Scgd * from the stream.
5263d7b677Scgd */
5363d7b677Scgd
5443fa6fe3Sjtc #include "namespace.h"
5546e6c5e8Slukem
56ce147c1cSlukem #include <stdio.h>
5746e6c5e8Slukem
5846e6c5e8Slukem #include <rpc/types.h>
5963d7b677Scgd #include <rpc/xdr.h>
6063d7b677Scgd
6143fa6fe3Sjtc #ifdef __weak_alias
6260549036Smycroft __weak_alias(xdrstdio_create,_xdrstdio_create)
6343fa6fe3Sjtc #endif
6443fa6fe3Sjtc
65adb74221Smatt static void xdrstdio_destroy(XDR *);
66adb74221Smatt static bool_t xdrstdio_getlong(XDR *, long *);
67adb74221Smatt static bool_t xdrstdio_putlong(XDR *, const long *);
68adb74221Smatt static bool_t xdrstdio_getbytes(XDR *, char *, u_int);
69adb74221Smatt static bool_t xdrstdio_putbytes(XDR *, const char *, u_int);
70adb74221Smatt static u_int xdrstdio_getpos(XDR *);
71adb74221Smatt static bool_t xdrstdio_setpos(XDR *, u_int);
72adb74221Smatt static int32_t *xdrstdio_inline(XDR *, u_int);
7363d7b677Scgd
7463d7b677Scgd /*
7563d7b677Scgd * Ops vector for stdio type XDR
7663d7b677Scgd */
77423c2aadSmycroft static const struct xdr_ops xdrstdio_ops = {
7863d7b677Scgd xdrstdio_getlong, /* deseraialize a long int */
7963d7b677Scgd xdrstdio_putlong, /* seraialize a long int */
8063d7b677Scgd xdrstdio_getbytes, /* deserialize counted bytes */
8163d7b677Scgd xdrstdio_putbytes, /* serialize counted bytes */
8263d7b677Scgd xdrstdio_getpos, /* get offset in the stream */
8363d7b677Scgd xdrstdio_setpos, /* set offset in the stream */
8463d7b677Scgd xdrstdio_inline, /* prime stream for inline macros */
85ce2c90c7Schristos xdrstdio_destroy, /* destroy stream */
86ce2c90c7Schristos NULL, /* xdrstdio_control */
8763d7b677Scgd };
8863d7b677Scgd
8963d7b677Scgd /*
9063d7b677Scgd * Initialize a stdio xdr stream.
9163d7b677Scgd * Sets the xdr stream handle xdrs for use on the stream file.
9263d7b677Scgd * Operation flag is set to op.
9363d7b677Scgd */
9463d7b677Scgd void
xdrstdio_create(XDR * xdrs,FILE * file,enum xdr_op op)95adb74221Smatt xdrstdio_create(XDR *xdrs, FILE *file, enum xdr_op op)
9663d7b677Scgd {
9763d7b677Scgd
9863d7b677Scgd xdrs->x_op = op;
9963d7b677Scgd xdrs->x_ops = &xdrstdio_ops;
1001325a26dSchristos xdrs->x_private = file;
10163d7b677Scgd xdrs->x_handy = 0;
10263d7b677Scgd xdrs->x_base = 0;
10363d7b677Scgd }
10463d7b677Scgd
10563d7b677Scgd /*
10663d7b677Scgd * Destroy a stdio xdr stream.
10763d7b677Scgd * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
10863d7b677Scgd */
10963d7b677Scgd static void
xdrstdio_destroy(XDR * xdrs)110adb74221Smatt xdrstdio_destroy(XDR *xdrs)
11163d7b677Scgd {
11263d7b677Scgd (void)fflush((FILE *)xdrs->x_private);
11315896e79Slukem /* XXX: should we close the file ?? */
1141325a26dSchristos }
11563d7b677Scgd
11663d7b677Scgd static bool_t
xdrstdio_getlong(XDR * xdrs,long * lp)117adb74221Smatt xdrstdio_getlong(XDR *xdrs, long *lp)
11863d7b677Scgd {
119a2c6783dSmartin u_int32_t temp;
12063d7b677Scgd
121a2c6783dSmartin if (fread(&temp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
12263d7b677Scgd return (FALSE);
123a2c6783dSmartin *lp = (long)ntohl(temp);
12463d7b677Scgd return (TRUE);
12563d7b677Scgd }
12663d7b677Scgd
12763d7b677Scgd static bool_t
xdrstdio_putlong(XDR * xdrs,const long * lp)128adb74221Smatt xdrstdio_putlong(XDR *xdrs, const long *lp)
12963d7b677Scgd {
130f6ee845eSmartin int32_t mycopy = htonl((u_int32_t)*lp);
13163d7b677Scgd
1321325a26dSchristos if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
13363d7b677Scgd return (FALSE);
13463d7b677Scgd return (TRUE);
13563d7b677Scgd }
13663d7b677Scgd
13763d7b677Scgd static bool_t
xdrstdio_getbytes(XDR * xdrs,char * addr,u_int len)138adb74221Smatt xdrstdio_getbytes(XDR *xdrs, char *addr, u_int len)
13963d7b677Scgd {
14063d7b677Scgd
1411325a26dSchristos if ((len != 0) && (fread(addr, (size_t)len, 1, (FILE *)xdrs->x_private) != 1))
14263d7b677Scgd return (FALSE);
14363d7b677Scgd return (TRUE);
14463d7b677Scgd }
14563d7b677Scgd
14663d7b677Scgd static bool_t
xdrstdio_putbytes(XDR * xdrs,const char * addr,u_int len)147adb74221Smatt xdrstdio_putbytes(XDR *xdrs, const char *addr, u_int len)
14863d7b677Scgd {
14963d7b677Scgd
1501325a26dSchristos if ((len != 0) && (fwrite(addr, (size_t)len, 1,
1511325a26dSchristos (FILE *)xdrs->x_private) != 1))
15263d7b677Scgd return (FALSE);
15363d7b677Scgd return (TRUE);
15463d7b677Scgd }
15563d7b677Scgd
156ce147c1cSlukem static u_int
xdrstdio_getpos(XDR * xdrs)157adb74221Smatt xdrstdio_getpos(XDR *xdrs)
15863d7b677Scgd {
15963d7b677Scgd
160ce147c1cSlukem return ((u_int) ftell((FILE *)xdrs->x_private));
16163d7b677Scgd }
16263d7b677Scgd
16363d7b677Scgd static bool_t
xdrstdio_setpos(XDR * xdrs,u_int pos)164adb74221Smatt xdrstdio_setpos(XDR *xdrs, u_int pos)
16563d7b677Scgd {
16663d7b677Scgd
167ce147c1cSlukem return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ?
16863d7b677Scgd FALSE : TRUE);
16963d7b677Scgd }
17063d7b677Scgd
1711325a26dSchristos /* ARGSUSED */
1722e2a3a25Scgd static int32_t *
xdrstdio_inline(XDR * xdrs,u_int len)173adb74221Smatt xdrstdio_inline(XDR *xdrs, u_int len)
17463d7b677Scgd {
17563d7b677Scgd
17663d7b677Scgd /*
17763d7b677Scgd * Must do some work to implement this: must insure
17863d7b677Scgd * enough data in the underlying stdio buffer,
17963d7b677Scgd * that the buffer is aligned so that we can indirect through a
180ce147c1cSlukem * long *, and stuff this pointer in xdrs->x_buf. Doing
18163d7b677Scgd * a fread or fwrite to a scratch buffer would defeat
18263d7b677Scgd * most of the gains to be had here and require storage
18363d7b677Scgd * management on this buffer, so we don't do this.
18463d7b677Scgd */
18563d7b677Scgd return (NULL);
18663d7b677Scgd }
187