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