Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/xdr_stdio.c

raw symbols SCIP
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 /*
31 * xdr_stdio.c, XDR implementation on standard i/o file.
32 *
33 * Copyright (C) 1984, Sun Microsystems, Inc.
34 *
35 * This set of routines implements a XDR on a stdio stream.
36 * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
37 * from the stream.
38 */
39
40 #include <stdio.h>
41 #include <stdint.h>
42
43 #include <arpa/inet.h>
44 #include <rpc/types.h>
45 #include <rpc/xdr.h>
46 #include "un-namespace.h"
47
48 static void xdrstdio_destroy(XDR *);
49 static bool_t xdrstdio_getlong(XDR *, long *);
50 static bool_t xdrstdio_putlong(XDR *, const long *);
51 static bool_t xdrstdio_getbytes(XDR *, char *, u_int);
52 static bool_t xdrstdio_putbytes(XDR *, const char *, u_int);
53 static u_int xdrstdio_getpos(XDR *);
54 static bool_t xdrstdio_setpos(XDR *, u_int);
55 static int32_t *xdrstdio_inline(XDR *, u_int);
56
57 /*
58 * Ops vector for stdio type XDR
59 */
60 static const struct xdr_ops xdrstdio_ops = {
61 xdrstdio_getlong, /* deseraialize a long int */
62 xdrstdio_putlong, /* seraialize a long int */
63 xdrstdio_getbytes, /* deserialize counted bytes */
64 xdrstdio_putbytes, /* serialize counted bytes */
65 xdrstdio_getpos, /* get offset in the stream */
66 xdrstdio_setpos, /* set offset in the stream */
67 xdrstdio_inline, /* prime stream for inline macros */
68 xdrstdio_destroy /* destroy stream */
69 };
70
71 /*
72 * Initialize a stdio xdr stream.
73 * Sets the xdr stream handle xdrs for use on the stream file.
74 * Operation flag is set to op.
75 */
76 void
77 xdrstdio_create(xdrs, file, op)
78 XDR *xdrs;
79 FILE *file;
80 enum xdr_op op;
81 {
82
83 xdrs->x_op = op;
84 xdrs->x_ops = &xdrstdio_ops;
85 xdrs->x_private = file;
86 xdrs->x_handy = 0;
87 xdrs->x_base = 0;
88 }
89
90 /*
91 * Destroy a stdio xdr stream.
92 * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
93 */
94 static void
95 xdrstdio_destroy(xdrs)
96 XDR *xdrs;
97 {
98 (void)fflush((FILE *)xdrs->x_private);
99 /* XXX: should we close the file ?? */
100 }
101
102 static bool_t
103 xdrstdio_getlong(xdrs, lp)
104 XDR *xdrs;
105 long *lp;
106 {
107 int32_t mycopy;
108
109 if (fread(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
110 return (FALSE);
111
112 *lp = (long)ntohl(mycopy);
113 return (TRUE);
114 }
115
116 static bool_t
117 xdrstdio_putlong(xdrs, lp)
118 XDR *xdrs;
119 const long *lp;
120 {
121 int32_t mycopy;
122
123 #if defined(_LP64)
124 if ((*lp > UINT32_MAX) || (*lp < INT32_MIN))
125 return (FALSE);
126 #endif
127
128 mycopy = (int32_t)htonl((int32_t)*lp);
129 if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
130 return (FALSE);
131 return (TRUE);
132 }
133
134 static bool_t
135 xdrstdio_getbytes(xdrs, addr, len)
136 XDR *xdrs;
137 char *addr;
138 u_int len;
139 {
140
141 if ((len != 0) && (fread(addr, (size_t)len, 1, (FILE *)xdrs->x_private) != 1))
142 return (FALSE);
143 return (TRUE);
144 }
145
146 static bool_t
147 xdrstdio_putbytes(xdrs, addr, len)
148 XDR *xdrs;
149 const char *addr;
150 u_int len;
151 {
152
153 if ((len != 0) && (fwrite(addr, (size_t)len, 1,
154 (FILE *)xdrs->x_private) != 1))
155 return (FALSE);
156 return (TRUE);
157 }
158
159 static u_int
160 xdrstdio_getpos(xdrs)
161 XDR *xdrs;
162 {
163
164 return ((u_int) ftell((FILE *)xdrs->x_private));
165 }
166
167 static bool_t
168 xdrstdio_setpos(xdrs, pos)
169 XDR *xdrs;
170 u_int pos;
171 {
172
173 return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ?
174 FALSE : TRUE);
175 }
176
177 /* ARGSUSED */
178 static int32_t *
179 xdrstdio_inline(xdrs, len)
180 XDR *xdrs;
181 u_int len;
182 {
183
184 /*
185 * Must do some work to implement this: must insure
186 * enough data in the underlying stdio buffer,
187 * that the buffer is aligned so that we can indirect through a
188 * long *, and stuff this pointer in xdrs->x_buf. Doing
189 * a fread or fwrite to a scratch buffer would defeat
190 * most of the gains to be had here and require storage
191 * management on this buffer, so we don't do this.
192 */
193 return (NULL);
194 }