2 * xdr_stdio.c, XDR implementation on standard i/o file.
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
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.
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.
33 * This set of routines implements a XDR on a stdio stream.
34 * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
38 #include <rpc/types.h>
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)
48 static bool_t
xdrstdio_getlong (XDR
*, long *);
49 static bool_t
xdrstdio_putlong (XDR
*, const long *);
50 static bool_t
xdrstdio_getbytes (XDR
*, caddr_t
, u_int
);
51 static bool_t
xdrstdio_putbytes (XDR
*, const char *, u_int
);
52 static u_int
xdrstdio_getpos (const XDR
*);
53 static bool_t
xdrstdio_setpos (XDR
*, u_int
);
54 static int32_t *xdrstdio_inline (XDR
*, u_int
);
55 static void xdrstdio_destroy (XDR
*);
56 static bool_t
xdrstdio_getint32 (XDR
*, int32_t *);
57 static bool_t
xdrstdio_putint32 (XDR
*, const int32_t *);
60 * Ops vector for stdio type XDR
62 static const struct xdr_ops xdrstdio_ops
=
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 */
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.
82 xdrstdio_create (XDR
*xdrs
, FILE *file
, enum xdr_op op
)
85 /* We have to add the const since the `struct xdr_ops' in `struct XDR'
87 xdrs
->x_ops
= (struct xdr_ops
*) &xdrstdio_ops
;
88 xdrs
->x_private
= (caddr_t
) file
;
94 * Destroy a stdio xdr stream.
95 * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
98 xdrstdio_destroy (XDR
*xdrs
)
100 (void) fflush ((FILE *) xdrs
->x_private
);
101 /* xx should we close the file ?? */
105 xdrstdio_getlong (XDR
*xdrs
, long *lp
)
109 if (fread ((caddr_t
) &mycopy
, 4, 1, (FILE *) xdrs
->x_private
) != 1)
111 *lp
= (long) ntohl (mycopy
);
116 xdrstdio_putlong (XDR
*xdrs
, const long *lp
)
118 int32_t mycopy
= htonl ((u_int32_t
) *lp
);
120 if (fwrite ((caddr_t
) &mycopy
, 4, 1, (FILE *) xdrs
->x_private
) != 1)
126 xdrstdio_getbytes (XDR
*xdrs
, const caddr_t addr
, u_int len
)
128 if ((len
!= 0) && (fread (addr
, (int) len
, 1,
129 (FILE *) xdrs
->x_private
) != 1))
135 xdrstdio_putbytes (XDR
*xdrs
, const char *addr
, u_int len
)
137 if ((len
!= 0) && (fwrite (addr
, (int) len
, 1,
138 (FILE *) xdrs
->x_private
) != 1))
144 xdrstdio_getpos (const XDR
*xdrs
)
146 return (u_int
) ftell ((FILE *) xdrs
->x_private
);
150 xdrstdio_setpos (XDR
*xdrs
, u_int pos
)
152 return fseek ((FILE *) xdrs
->x_private
, (long) pos
, 0) < 0 ? FALSE
: TRUE
;
156 xdrstdio_inline (XDR
*xdrs
, u_int len
)
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.
171 xdrstdio_getint32 (XDR
*xdrs
, int32_t *ip
)
175 if (fread ((caddr_t
) &mycopy
, 4, 1, (FILE *) xdrs
->x_private
) != 1)
177 *ip
= ntohl (mycopy
);
182 xdrstdio_putint32 (XDR
*xdrs
, const int32_t *ip
)
184 int32_t mycopy
= htonl (*ip
);
187 if (fwrite ((caddr_t
) ip
, 4, 1, (FILE *) xdrs
->x_private
) != 1)
191 #ifdef EXPORT_RPC_SYMBOLS
192 libc_hidden_def (xdrstdio_create
)
194 libc_hidden_nolink_sunrpc (xdrstdio_create
, GLIBC_2_0
)