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