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