]>
Commit | Line | Data |
---|---|---|
28f540f4 | 1 | /* |
a7ab6ec8 | 2 | * xdr_stdio.c, XDR implementation on standard i/o file. |
6d52618b | 3 | * |
a7ab6ec8 | 4 | * Copyright (c) 2010, Oracle America, Inc. |
6d52618b | 5 | * |
a7ab6ec8 UD |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions are | |
8 | * met: | |
6d52618b | 9 | * |
a7ab6ec8 UD |
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. | |
cb636bb2 | 19 | * |
a7ab6ec8 UD |
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. | |
28f540f4 RM |
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 | ||
3ce1f295 UD |
42 | #include <libio/iolibio.h> |
43 | #define fflush(s) INTUSE(_IO_fflush) (s) | |
44 | #define fread(p, m, n, s) INTUSE(_IO_fread) (p, m, n, s) | |
45 | #define ftell(s) INTUSE(_IO_ftell) (s) | |
46 | #define fwrite(p, m, n, s) INTUSE(_IO_fwrite) (p, m, n, s) | |
50304ef0 | 47 | |
e7fd8a39 | 48 | static bool_t xdrstdio_getlong (XDR *, long *); |
1f205a47 | 49 | static bool_t xdrstdio_putlong (XDR *, const long *); |
e7fd8a39 | 50 | static bool_t xdrstdio_getbytes (XDR *, caddr_t, u_int); |
bfbc5754 UD |
51 | static bool_t xdrstdio_putbytes (XDR *, const char *, u_int); |
52 | static u_int xdrstdio_getpos (const XDR *); | |
e7fd8a39 | 53 | static bool_t xdrstdio_setpos (XDR *, u_int); |
55187f62 | 54 | static int32_t *xdrstdio_inline (XDR *, u_int); |
92f1da4d | 55 | static void xdrstdio_destroy (XDR *); |
7d1de115 UD |
56 | static bool_t xdrstdio_getint32 (XDR *, int32_t *); |
57 | static bool_t xdrstdio_putint32 (XDR *, const int32_t *); | |
28f540f4 RM |
58 | |
59 | /* | |
60 | * Ops vector for stdio type XDR | |
61 | */ | |
e7fd8a39 UD |
62 | static 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 */ | |
7d1de115 UD |
71 | xdrstdio_destroy, /* destroy stream */ |
72 | xdrstdio_getint32, /* deserialize a int */ | |
73 | xdrstdio_putint32 /* serialize a int */ | |
28f540f4 RM |
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 | */ | |
81 | void | |
f8afba91 | 82 | xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op) |
28f540f4 | 83 | { |
e7fd8a39 | 84 | xdrs->x_op = op; |
737547be UD |
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; | |
e7fd8a39 UD |
88 | xdrs->x_private = (caddr_t) file; |
89 | xdrs->x_handy = 0; | |
90 | xdrs->x_base = 0; | |
28f540f4 RM |
91 | } |
92 | ||
93 | /* | |
94 | * Destroy a stdio xdr stream. | |
95 | * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create. | |
96 | */ | |
97 | static void | |
f8afba91 | 98 | xdrstdio_destroy (XDR *xdrs) |
28f540f4 | 99 | { |
e7fd8a39 UD |
100 | (void) fflush ((FILE *) xdrs->x_private); |
101 | /* xx should we close the file ?? */ | |
28f540f4 RM |
102 | }; |
103 | ||
104 | static bool_t | |
f8afba91 | 105 | xdrstdio_getlong (XDR *xdrs, long *lp) |
28f540f4 | 106 | { |
12c879f8 | 107 | u_int32_t mycopy; |
28f540f4 | 108 | |
12c879f8 | 109 | if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1) |
e7fd8a39 | 110 | return FALSE; |
12c879f8 | 111 | *lp = (long) ntohl (mycopy); |
e7fd8a39 | 112 | return TRUE; |
28f540f4 RM |
113 | } |
114 | ||
115 | static bool_t | |
1f205a47 | 116 | xdrstdio_putlong (XDR *xdrs, const long *lp) |
28f540f4 | 117 | { |
12c879f8 UD |
118 | int32_t mycopy = htonl ((u_int32_t) *lp); |
119 | ||
120 | if (fwrite ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1) | |
e7fd8a39 UD |
121 | return FALSE; |
122 | return TRUE; | |
28f540f4 RM |
123 | } |
124 | ||
125 | static bool_t | |
f8afba91 | 126 | xdrstdio_getbytes (XDR *xdrs, const caddr_t addr, u_int len) |
28f540f4 | 127 | { |
f8afba91 UD |
128 | if ((len != 0) && (fread (addr, (int) len, 1, |
129 | (FILE *) xdrs->x_private) != 1)) | |
e7fd8a39 UD |
130 | return FALSE; |
131 | return TRUE; | |
28f540f4 RM |
132 | } |
133 | ||
134 | static bool_t | |
bfbc5754 | 135 | xdrstdio_putbytes (XDR *xdrs, const char *addr, u_int len) |
28f540f4 | 136 | { |
f8afba91 UD |
137 | if ((len != 0) && (fwrite (addr, (int) len, 1, |
138 | (FILE *) xdrs->x_private) != 1)) | |
e7fd8a39 UD |
139 | return FALSE; |
140 | return TRUE; | |
28f540f4 RM |
141 | } |
142 | ||
143 | static u_int | |
bfbc5754 | 144 | xdrstdio_getpos (const XDR *xdrs) |
28f540f4 | 145 | { |
e7fd8a39 | 146 | return (u_int) ftell ((FILE *) xdrs->x_private); |
28f540f4 RM |
147 | } |
148 | ||
149 | static bool_t | |
e7fd8a39 | 150 | xdrstdio_setpos (XDR *xdrs, u_int pos) |
6d52618b | 151 | { |
e7fd8a39 | 152 | return fseek ((FILE *) xdrs->x_private, (long) pos, 0) < 0 ? FALSE : TRUE; |
28f540f4 RM |
153 | } |
154 | ||
f8afba91 | 155 | static int32_t * |
55187f62 | 156 | xdrstdio_inline (XDR *xdrs, u_int len) |
28f540f4 | 157 | { |
e7fd8a39 UD |
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; | |
28f540f4 | 168 | } |
7d1de115 UD |
169 | |
170 | static bool_t | |
171 | xdrstdio_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 | ||
181 | static bool_t | |
182 | xdrstdio_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 | } | |
7b57bfe5 | 191 | #ifdef EXPORT_RPC_SYMBOLS |
e436294b | 192 | libc_hidden_def (xdrstdio_create) |
7b57bfe5 | 193 | #else |
021db4be | 194 | libc_hidden_nolink_sunrpc (xdrstdio_create, GLIBC_2_0) |
7b57bfe5 | 195 | #endif |