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