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