xref: /freebsd/lib/libc/xdr/xdr_stdio.c (revision a204967a654a3221876c8348b8e216c82510d1b9)
18360efbdSAlfred Perlstein /*	$NetBSD: xdr_stdio.c,v 1.14 2000/01/22 22:19:19 mycroft Exp $	*/
28360efbdSAlfred Perlstein 
3*a204967aSHiroki Sato /*-
4*a204967aSHiroki Sato  * Copyright (c) 2010, Oracle America, Inc.
5eae561b3SGarrett Wollman  *
6*a204967aSHiroki Sato  * Redistribution and use in source and binary forms, with or without
7*a204967aSHiroki Sato  * modification, are permitted provided that the following conditions are
8*a204967aSHiroki Sato  * met:
9eae561b3SGarrett Wollman  *
10*a204967aSHiroki Sato  *     * Redistributions of source code must retain the above copyright
11*a204967aSHiroki Sato  *       notice, this list of conditions and the following disclaimer.
12*a204967aSHiroki Sato  *     * Redistributions in binary form must reproduce the above
13*a204967aSHiroki Sato  *       copyright notice, this list of conditions and the following
14*a204967aSHiroki Sato  *       disclaimer in the documentation and/or other materials
15*a204967aSHiroki Sato  *       provided with the distribution.
16*a204967aSHiroki Sato  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17*a204967aSHiroki Sato  *       contributors may be used to endorse or promote products derived
18*a204967aSHiroki Sato  *       from this software without specific prior written permission.
19eae561b3SGarrett Wollman  *
20*a204967aSHiroki Sato  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*a204967aSHiroki Sato  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*a204967aSHiroki Sato  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*a204967aSHiroki Sato  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*a204967aSHiroki Sato  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*a204967aSHiroki Sato  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*a204967aSHiroki Sato  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*a204967aSHiroki Sato  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*a204967aSHiroki Sato  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*a204967aSHiroki Sato  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*a204967aSHiroki Sato  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*a204967aSHiroki 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
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