1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* $NetBSD: xdr_stdio.c,v 1.14 2000/01/22 22:19:19 mycroft Exp $ */ |
---|
4 | |
---|
5 | /* |
---|
6 | * Sun RPC is a product of Sun Microsystems, Inc. and is provided for |
---|
7 | * unrestricted use provided that this legend is included on all tape |
---|
8 | * media and as a part of the software program in whole or part. Users |
---|
9 | * may copy or modify Sun RPC without charge, but are not authorized |
---|
10 | * to license or distribute it to anyone else except as part of a product or |
---|
11 | * program developed by the user. |
---|
12 | * |
---|
13 | * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE |
---|
14 | * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR |
---|
15 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. |
---|
16 | * |
---|
17 | * Sun RPC is provided with no support and without any obligation on the |
---|
18 | * part of Sun Microsystems, Inc. to assist in its use, correction, |
---|
19 | * modification or enhancement. |
---|
20 | * |
---|
21 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE |
---|
22 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC |
---|
23 | * OR ANY PART THEREOF. |
---|
24 | * |
---|
25 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue |
---|
26 | * or profits or other special, indirect and consequential damages, even if |
---|
27 | * Sun has been advised of the possibility of such damages. |
---|
28 | * |
---|
29 | * Sun Microsystems, Inc. |
---|
30 | * 2550 Garcia Avenue |
---|
31 | * Mountain View, California 94043 |
---|
32 | */ |
---|
33 | |
---|
34 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
35 | static char *sccsid2 = "@(#)xdr_stdio.c 1.16 87/08/11 Copyr 1984 Sun Micro"; |
---|
36 | static char *sccsid = "@(#)xdr_stdio.c 2.1 88/07/29 4.0 RPCSRC"; |
---|
37 | #endif |
---|
38 | #include <sys/cdefs.h> |
---|
39 | __FBSDID("$FreeBSD$"); |
---|
40 | |
---|
41 | /* |
---|
42 | * xdr_stdio.c, XDR implementation on standard i/o file. |
---|
43 | * |
---|
44 | * Copyright (C) 1984, Sun Microsystems, Inc. |
---|
45 | * |
---|
46 | * This set of routines implements a XDR on a stdio stream. |
---|
47 | * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes |
---|
48 | * from the stream. |
---|
49 | */ |
---|
50 | |
---|
51 | #include "namespace.h" |
---|
52 | #include <stdio.h> |
---|
53 | |
---|
54 | #include <arpa/inet.h> |
---|
55 | #include <rpc/types.h> |
---|
56 | #include <rpc/xdr.h> |
---|
57 | #include "un-namespace.h" |
---|
58 | |
---|
59 | static void xdrstdio_destroy(XDR *); |
---|
60 | static bool_t xdrstdio_getlong(XDR *, long *); |
---|
61 | static bool_t xdrstdio_putlong(XDR *, const long *); |
---|
62 | static bool_t xdrstdio_getbytes(XDR *, char *, u_int); |
---|
63 | static bool_t xdrstdio_putbytes(XDR *, const char *, u_int); |
---|
64 | static u_int xdrstdio_getpos(XDR *); |
---|
65 | static bool_t xdrstdio_setpos(XDR *, u_int); |
---|
66 | static int32_t *xdrstdio_inline(XDR *, u_int); |
---|
67 | |
---|
68 | /* |
---|
69 | * Ops vector for stdio type XDR |
---|
70 | */ |
---|
71 | static const struct xdr_ops xdrstdio_ops = { |
---|
72 | xdrstdio_getlong, /* deseraialize a long int */ |
---|
73 | xdrstdio_putlong, /* seraialize a long int */ |
---|
74 | xdrstdio_getbytes, /* deserialize counted bytes */ |
---|
75 | xdrstdio_putbytes, /* serialize counted bytes */ |
---|
76 | xdrstdio_getpos, /* get offset in the stream */ |
---|
77 | xdrstdio_setpos, /* set offset in the stream */ |
---|
78 | xdrstdio_inline, /* prime stream for inline macros */ |
---|
79 | xdrstdio_destroy /* destroy stream */ |
---|
80 | }; |
---|
81 | |
---|
82 | /* |
---|
83 | * Initialize a stdio xdr stream. |
---|
84 | * Sets the xdr stream handle xdrs for use on the stream file. |
---|
85 | * Operation flag is set to op. |
---|
86 | */ |
---|
87 | void |
---|
88 | xdrstdio_create(xdrs, file, op) |
---|
89 | XDR *xdrs; |
---|
90 | FILE *file; |
---|
91 | enum xdr_op op; |
---|
92 | { |
---|
93 | |
---|
94 | xdrs->x_op = op; |
---|
95 | xdrs->x_ops = &xdrstdio_ops; |
---|
96 | xdrs->x_private = file; |
---|
97 | xdrs->x_handy = 0; |
---|
98 | xdrs->x_base = 0; |
---|
99 | } |
---|
100 | |
---|
101 | /* |
---|
102 | * Destroy a stdio xdr stream. |
---|
103 | * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create. |
---|
104 | */ |
---|
105 | static void |
---|
106 | xdrstdio_destroy(xdrs) |
---|
107 | XDR *xdrs; |
---|
108 | { |
---|
109 | (void)fflush((FILE *)xdrs->x_private); |
---|
110 | /* XXX: should we close the file ?? */ |
---|
111 | } |
---|
112 | |
---|
113 | static bool_t |
---|
114 | xdrstdio_getlong(xdrs, lp) |
---|
115 | XDR *xdrs; |
---|
116 | long *lp; |
---|
117 | { |
---|
118 | u_int32_t temp; |
---|
119 | |
---|
120 | if (fread(&temp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1) |
---|
121 | return (FALSE); |
---|
122 | *lp = (long)ntohl(temp); |
---|
123 | return (TRUE); |
---|
124 | } |
---|
125 | |
---|
126 | static bool_t |
---|
127 | xdrstdio_putlong(xdrs, lp) |
---|
128 | XDR *xdrs; |
---|
129 | const long *lp; |
---|
130 | { |
---|
131 | int32_t mycopy = htonl((u_int32_t)*lp); |
---|
132 | |
---|
133 | if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1) |
---|
134 | return (FALSE); |
---|
135 | return (TRUE); |
---|
136 | } |
---|
137 | |
---|
138 | static bool_t |
---|
139 | xdrstdio_getbytes(xdrs, addr, len) |
---|
140 | XDR *xdrs; |
---|
141 | char *addr; |
---|
142 | u_int len; |
---|
143 | { |
---|
144 | |
---|
145 | if ((len != 0) && (fread(addr, (size_t)len, 1, (FILE *)xdrs->x_private) != 1)) |
---|
146 | return (FALSE); |
---|
147 | return (TRUE); |
---|
148 | } |
---|
149 | |
---|
150 | static bool_t |
---|
151 | xdrstdio_putbytes(xdrs, addr, len) |
---|
152 | XDR *xdrs; |
---|
153 | const char *addr; |
---|
154 | u_int len; |
---|
155 | { |
---|
156 | |
---|
157 | if ((len != 0) && (fwrite(addr, (size_t)len, 1, |
---|
158 | (FILE *)xdrs->x_private) != 1)) |
---|
159 | return (FALSE); |
---|
160 | return (TRUE); |
---|
161 | } |
---|
162 | |
---|
163 | static u_int |
---|
164 | xdrstdio_getpos(xdrs) |
---|
165 | XDR *xdrs; |
---|
166 | { |
---|
167 | |
---|
168 | return ((u_int) ftell((FILE *)xdrs->x_private)); |
---|
169 | } |
---|
170 | |
---|
171 | static bool_t |
---|
172 | xdrstdio_setpos(xdrs, pos) |
---|
173 | XDR *xdrs; |
---|
174 | u_int pos; |
---|
175 | { |
---|
176 | |
---|
177 | return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ? |
---|
178 | FALSE : TRUE); |
---|
179 | } |
---|
180 | |
---|
181 | /* ARGSUSED */ |
---|
182 | static int32_t * |
---|
183 | xdrstdio_inline(xdrs, len) |
---|
184 | XDR *xdrs; |
---|
185 | u_int len; |
---|
186 | { |
---|
187 | |
---|
188 | /* |
---|
189 | * Must do some work to implement this: must insure |
---|
190 | * enough data in the underlying stdio buffer, |
---|
191 | * that the buffer is aligned so that we can indirect through a |
---|
192 | * long *, and stuff this pointer in xdrs->x_buf. Doing |
---|
193 | * a fread or fwrite to a scratch buffer would defeat |
---|
194 | * most of the gains to be had here and require storage |
---|
195 | * management on this buffer, so we don't do this. |
---|
196 | */ |
---|
197 | return (NULL); |
---|
198 | } |
---|