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