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