xref: /reactos/dll/3rdparty/libtirpc/src/xdr_stdio.c (revision c2c66aff)
1*c2c66affSColin Finck /*
2*c2c66affSColin Finck  * Copyright (c) 2009, Sun Microsystems, Inc.
3*c2c66affSColin Finck  * All rights reserved.
4*c2c66affSColin Finck  *
5*c2c66affSColin Finck  * Redistribution and use in source and binary forms, with or without
6*c2c66affSColin Finck  * modification, are permitted provided that the following conditions are met:
7*c2c66affSColin Finck  * - Redistributions of source code must retain the above copyright notice,
8*c2c66affSColin Finck  *   this list of conditions and the following disclaimer.
9*c2c66affSColin Finck  * - Redistributions in binary form must reproduce the above copyright notice,
10*c2c66affSColin Finck  *   this list of conditions and the following disclaimer in the documentation
11*c2c66affSColin Finck  *   and/or other materials provided with the distribution.
12*c2c66affSColin Finck  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13*c2c66affSColin Finck  *   contributors may be used to endorse or promote products derived
14*c2c66affSColin Finck  *   from this software without specific prior written permission.
15*c2c66affSColin Finck  *
16*c2c66affSColin Finck  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*c2c66affSColin Finck  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*c2c66affSColin Finck  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*c2c66affSColin Finck  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*c2c66affSColin Finck  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*c2c66affSColin Finck  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*c2c66affSColin Finck  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*c2c66affSColin Finck  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*c2c66affSColin Finck  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*c2c66affSColin Finck  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*c2c66affSColin Finck  * POSSIBILITY OF SUCH DAMAGE.
27*c2c66affSColin Finck  */
28*c2c66affSColin Finck 
29*c2c66affSColin Finck //#include <sys/cdefs.h>
30*c2c66affSColin Finck 
31*c2c66affSColin Finck /*
32*c2c66affSColin Finck  * xdr_stdio.c, XDR implementation on standard i/o file.
33*c2c66affSColin Finck  *
34*c2c66affSColin Finck  * Copyright (C) 1984, Sun Microsystems, Inc.
35*c2c66affSColin Finck  *
36*c2c66affSColin Finck  * This set of routines implements a XDR on a stdio stream.
37*c2c66affSColin Finck  * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
38*c2c66affSColin Finck  * from the stream.
39*c2c66affSColin Finck  */
40*c2c66affSColin Finck 
41*c2c66affSColin Finck #include <wintirpc.h>
42*c2c66affSColin Finck #include "namespace.h"
43*c2c66affSColin Finck #include <stdio.h>
44*c2c66affSColin Finck 
45*c2c66affSColin Finck //#include <arpa/inet.h>
46*c2c66affSColin Finck #include <rpc/types.h>
47*c2c66affSColin Finck #include <rpc/xdr.h>
48*c2c66affSColin Finck #include "un-namespace.h"
49*c2c66affSColin Finck 
50*c2c66affSColin Finck static void xdrstdio_destroy(XDR *);
51*c2c66affSColin Finck static bool_t xdrstdio_getlong(XDR *, long *);
52*c2c66affSColin Finck static bool_t xdrstdio_putlong(XDR *, const long *);
53*c2c66affSColin Finck static bool_t xdrstdio_getbytes(XDR *, char *, u_int);
54*c2c66affSColin Finck static bool_t xdrstdio_putbytes(XDR *, const char *, u_int);
55*c2c66affSColin Finck static u_int xdrstdio_getpos(XDR *);
56*c2c66affSColin Finck static bool_t xdrstdio_setpos(XDR *, u_int);
57*c2c66affSColin Finck static int32_t *xdrstdio_inline(XDR *, u_int);
58*c2c66affSColin Finck 
59*c2c66affSColin Finck /*
60*c2c66affSColin Finck  * Ops vector for stdio type XDR
61*c2c66affSColin Finck  */
62*c2c66affSColin Finck static const struct xdr_ops	xdrstdio_ops = {
63*c2c66affSColin Finck 	xdrstdio_getlong,	/* deseraialize a long int */
64*c2c66affSColin Finck 	xdrstdio_putlong,	/* seraialize a long int */
65*c2c66affSColin Finck 	xdrstdio_getbytes,	/* deserialize counted bytes */
66*c2c66affSColin Finck 	xdrstdio_putbytes,	/* serialize counted bytes */
67*c2c66affSColin Finck 	xdrstdio_getpos,	/* get offset in the stream */
68*c2c66affSColin Finck 	xdrstdio_setpos,	/* set offset in the stream */
69*c2c66affSColin Finck 	xdrstdio_inline,	/* prime stream for inline macros */
70*c2c66affSColin Finck 	xdrstdio_destroy	/* destroy stream */
71*c2c66affSColin Finck };
72*c2c66affSColin Finck 
73*c2c66affSColin Finck /*
74*c2c66affSColin Finck  * Initialize a stdio xdr stream.
75*c2c66affSColin Finck  * Sets the xdr stream handle xdrs for use on the stream file.
76*c2c66affSColin Finck  * Operation flag is set to op.
77*c2c66affSColin Finck  */
78*c2c66affSColin Finck void
xdrstdio_create(xdrs,file,op)79*c2c66affSColin Finck xdrstdio_create(xdrs, file, op)
80*c2c66affSColin Finck 	XDR *xdrs;
81*c2c66affSColin Finck 	FILE *file;
82*c2c66affSColin Finck 	enum xdr_op op;
83*c2c66affSColin Finck {
84*c2c66affSColin Finck 
85*c2c66affSColin Finck 	xdrs->x_op = op;
86*c2c66affSColin Finck 	xdrs->x_ops = &xdrstdio_ops;
87*c2c66affSColin Finck 	xdrs->x_private = file;
88*c2c66affSColin Finck 	xdrs->x_handy = 0;
89*c2c66affSColin Finck 	xdrs->x_base = 0;
90*c2c66affSColin Finck }
91*c2c66affSColin Finck 
92*c2c66affSColin Finck /*
93*c2c66affSColin Finck  * Destroy a stdio xdr stream.
94*c2c66affSColin Finck  * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
95*c2c66affSColin Finck  */
96*c2c66affSColin Finck static void
xdrstdio_destroy(xdrs)97*c2c66affSColin Finck xdrstdio_destroy(xdrs)
98*c2c66affSColin Finck 	XDR *xdrs;
99*c2c66affSColin Finck {
100*c2c66affSColin Finck 	(void)fflush((FILE *)xdrs->x_private);
101*c2c66affSColin Finck 		/* XXX: should we close the file ?? */
102*c2c66affSColin Finck }
103*c2c66affSColin Finck 
104*c2c66affSColin Finck static bool_t
xdrstdio_getlong(xdrs,lp)105*c2c66affSColin Finck xdrstdio_getlong(xdrs, lp)
106*c2c66affSColin Finck 	XDR *xdrs;
107*c2c66affSColin Finck 	long *lp;
108*c2c66affSColin Finck {
109*c2c66affSColin Finck 
110*c2c66affSColin Finck 	if (fread(lp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
111*c2c66affSColin Finck 		return (FALSE);
112*c2c66affSColin Finck 	*lp = (long)ntohl((u_int32_t)*lp);
113*c2c66affSColin Finck 	return (TRUE);
114*c2c66affSColin Finck }
115*c2c66affSColin Finck 
116*c2c66affSColin Finck static bool_t
xdrstdio_putlong(xdrs,lp)117*c2c66affSColin Finck xdrstdio_putlong(xdrs, lp)
118*c2c66affSColin Finck 	XDR *xdrs;
119*c2c66affSColin Finck 	const long *lp;
120*c2c66affSColin Finck {
121*c2c66affSColin Finck 	long mycopy = (long)htonl((u_int32_t)*lp);
122*c2c66affSColin Finck 
123*c2c66affSColin Finck 	if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
124*c2c66affSColin Finck 		return (FALSE);
125*c2c66affSColin Finck 	return (TRUE);
126*c2c66affSColin Finck }
127*c2c66affSColin Finck 
128*c2c66affSColin Finck static bool_t
xdrstdio_getbytes(xdrs,addr,len)129*c2c66affSColin Finck xdrstdio_getbytes(xdrs, addr, len)
130*c2c66affSColin Finck 	XDR *xdrs;
131*c2c66affSColin Finck 	char *addr;
132*c2c66affSColin Finck 	u_int len;
133*c2c66affSColin Finck {
134*c2c66affSColin Finck 
135*c2c66affSColin Finck 	if ((len != 0) && (fread(addr, (size_t)len, 1, (FILE *)xdrs->x_private) != 1))
136*c2c66affSColin Finck 		return (FALSE);
137*c2c66affSColin Finck 	return (TRUE);
138*c2c66affSColin Finck }
139*c2c66affSColin Finck 
140*c2c66affSColin Finck static bool_t
xdrstdio_putbytes(xdrs,addr,len)141*c2c66affSColin Finck xdrstdio_putbytes(xdrs, addr, len)
142*c2c66affSColin Finck 	XDR *xdrs;
143*c2c66affSColin Finck 	const char *addr;
144*c2c66affSColin Finck 	u_int len;
145*c2c66affSColin Finck {
146*c2c66affSColin Finck 
147*c2c66affSColin Finck 	if ((len != 0) && (fwrite(addr, (size_t)len, 1,
148*c2c66affSColin Finck 	    (FILE *)xdrs->x_private) != 1))
149*c2c66affSColin Finck 		return (FALSE);
150*c2c66affSColin Finck 	return (TRUE);
151*c2c66affSColin Finck }
152*c2c66affSColin Finck 
153*c2c66affSColin Finck static u_int
xdrstdio_getpos(xdrs)154*c2c66affSColin Finck xdrstdio_getpos(xdrs)
155*c2c66affSColin Finck 	XDR *xdrs;
156*c2c66affSColin Finck {
157*c2c66affSColin Finck 
158*c2c66affSColin Finck 	return ((u_int) ftell((FILE *)xdrs->x_private));
159*c2c66affSColin Finck }
160*c2c66affSColin Finck 
161*c2c66affSColin Finck static bool_t
xdrstdio_setpos(xdrs,pos)162*c2c66affSColin Finck xdrstdio_setpos(xdrs, pos)
163*c2c66affSColin Finck 	XDR *xdrs;
164*c2c66affSColin Finck 	u_int pos;
165*c2c66affSColin Finck {
166*c2c66affSColin Finck 
167*c2c66affSColin Finck 	return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ?
168*c2c66affSColin Finck 		FALSE : TRUE);
169*c2c66affSColin Finck }
170*c2c66affSColin Finck 
171*c2c66affSColin Finck /* ARGSUSED */
172*c2c66affSColin Finck static int32_t *
xdrstdio_inline(xdrs,len)173*c2c66affSColin Finck xdrstdio_inline(xdrs, len)
174*c2c66affSColin Finck 	XDR *xdrs;
175*c2c66affSColin Finck 	u_int len;
176*c2c66affSColin Finck {
177*c2c66affSColin Finck 
178*c2c66affSColin Finck 	/*
179*c2c66affSColin Finck 	 * Must do some work to implement this: must insure
180*c2c66affSColin Finck 	 * enough data in the underlying stdio buffer,
181*c2c66affSColin Finck 	 * that the buffer is aligned so that we can indirect through a
182*c2c66affSColin Finck 	 * long *, and stuff this pointer in xdrs->x_buf.  Doing
183*c2c66affSColin Finck 	 * a fread or fwrite to a scratch buffer would defeat
184*c2c66affSColin Finck 	 * most of the gains to be had here and require storage
185*c2c66affSColin Finck 	 * management on this buffer, so we don't do this.
186*c2c66affSColin Finck 	 */
187*c2c66affSColin Finck 	return (NULL);
188*c2c66affSColin Finck }
189