Bring in a transport-independent RPC (TI-RPC).
[dragonfly.git] / lib / libc / xdr / xdr_stdio.c
blobcf0a27f70bcf5dcb48dd57d8ca8cc1fafe2f8afc
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.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
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.
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.
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.
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
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 * $DragonFly: src/lib/libc/xdr/xdr_stdio.c,v 1.4 2005/12/05 00:47:57 swildner Exp $
37 * xdr_stdio.c, XDR implementation on standard i/o file.
39 * Copyright (C) 1984, Sun Microsystems, Inc.
41 * This set of routines implements a XDR on a stdio stream.
42 * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
43 * from the stream.
46 #include "namespace.h"
47 #include <stdio.h>
49 #include <arpa/inet.h>
50 #include <rpc/types.h>
51 #include <rpc/xdr.h>
52 #include "un-namespace.h"
54 static void xdrstdio_destroy(XDR *);
55 static bool_t xdrstdio_getlong(XDR *, long *);
56 static bool_t xdrstdio_putlong(XDR *, const long *);
57 static bool_t xdrstdio_getbytes(XDR *, char *, u_int);
58 static bool_t xdrstdio_putbytes(XDR *, const char *, u_int);
59 static u_int xdrstdio_getpos(XDR *);
60 static bool_t xdrstdio_setpos(XDR *, u_int);
61 static int32_t *xdrstdio_inline(XDR *, u_int);
64 * Ops vector for stdio type XDR
66 static const struct xdr_ops xdrstdio_ops = {
67 xdrstdio_getlong, /* deseraialize a long int */
68 xdrstdio_putlong, /* seraialize a long int */
69 xdrstdio_getbytes, /* deserialize counted bytes */
70 xdrstdio_putbytes, /* serialize counted bytes */
71 xdrstdio_getpos, /* get offset in the stream */
72 xdrstdio_setpos, /* set offset in the stream */
73 xdrstdio_inline, /* prime stream for inline macros */
74 xdrstdio_destroy /* destroy stream */
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.
82 void
83 xdrstdio_create(XDR *xdrs, FILE *file, enum xdr_op op)
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;
94 * Destroy a stdio xdr stream.
95 * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
97 static void
98 xdrstdio_destroy(XDR *xdrs)
100 fflush((FILE *)xdrs->x_private);
101 /* XXX: should we close the file ?? */
104 static bool_t
105 xdrstdio_getlong(XDR *xdrs, long *lp)
107 u_int32_t temp;
109 if (fread(&temp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
110 return (FALSE);
111 *lp = (long)ntohl(temp);
112 return (TRUE);
115 static bool_t
116 xdrstdio_putlong(XDR *xdrs, const long *lp)
118 int32_t mycopy = htonl((u_int32_t)*lp);
120 if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
121 return (FALSE);
122 return (TRUE);
125 static bool_t
126 xdrstdio_getbytes(XDR *xdrs, char *addr, u_int len)
129 if ((len != 0) && (fread(addr, (size_t)len, 1, (FILE *)xdrs->x_private) != 1))
130 return (FALSE);
131 return (TRUE);
134 static bool_t
135 xdrstdio_putbytes(XDR *xdrs, const char *addr, u_int len)
138 if ((len != 0) && (fwrite(addr, (size_t)len, 1,
139 (FILE *)xdrs->x_private) != 1))
140 return (FALSE);
141 return (TRUE);
144 static u_int
145 xdrstdio_getpos(XDR *xdrs)
148 return ((u_int) ftell((FILE *)xdrs->x_private));
151 static bool_t
152 xdrstdio_setpos(XDR *xdrs, u_int pos)
155 return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ?
156 FALSE : TRUE);
159 /* ARGSUSED */
160 static int32_t *
161 xdrstdio_inline(XDR *xdrs __unused, u_int len __unused)
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.
173 return (NULL);