]>
Commit | Line | Data |
---|---|---|
03fb6eb0 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
ad21edcc A |
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. | |
03fb6eb0 A |
13 | * |
14 | * The Original Code and all software distributed under the License are | |
ad21edcc | 15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
03fb6eb0 A |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
ad21edcc A |
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. | |
03fb6eb0 A |
21 | * |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
c29f2fcc A |
24 | |
25 | /* $NetBSD: xdr_stdio.c,v 1.14 2000/01/22 22:19:19 mycroft Exp $ */ | |
26 | ||
03fb6eb0 A |
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. | |
c29f2fcc | 34 | * |
03fb6eb0 A |
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. | |
c29f2fcc | 38 | * |
03fb6eb0 A |
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. | |
c29f2fcc | 42 | * |
03fb6eb0 A |
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. | |
c29f2fcc | 46 | * |
03fb6eb0 A |
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. | |
c29f2fcc | 50 | * |
03fb6eb0 A |
51 | * Sun Microsystems, Inc. |
52 | * 2550 Garcia Avenue | |
53 | * Mountain View, California 94043 | |
54 | */ | |
55 | ||
56 | #if defined(LIBC_SCCS) && !defined(lint) | |
c29f2fcc A |
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"; | |
03fb6eb0 | 59 | #endif |
c29f2fcc | 60 | #include <sys/cdefs.h> |
03fb6eb0 A |
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 | ||
03fb6eb0 | 72 | #include <stdio.h> |
c29f2fcc A |
73 | |
74 | #include <arpa/inet.h> | |
75 | #include <rpc/types.h> | |
03fb6eb0 A |
76 | #include <rpc/xdr.h> |
77 | ||
c29f2fcc | 78 | static void xdrstdio_destroy(XDR *); |
b3dd680f A |
79 | #ifdef __LP64__ |
80 | static bool_t xdrstdio_getlong(XDR *, int *); | |
81 | static bool_t xdrstdio_putlong(XDR *, const int *); | |
82 | #else | |
c29f2fcc A |
83 | static bool_t xdrstdio_getlong(XDR *, long *); |
84 | static bool_t xdrstdio_putlong(XDR *, const long *); | |
b3dd680f | 85 | #endif |
c29f2fcc A |
86 | static bool_t xdrstdio_getbytes(XDR *, char *, u_int); |
87 | static bool_t xdrstdio_putbytes(XDR *, const char *, u_int); | |
88 | static u_int xdrstdio_getpos(XDR *); | |
89 | static bool_t xdrstdio_setpos(XDR *, u_int); | |
90 | static int32_t *xdrstdio_inline(XDR *, u_int); | |
03fb6eb0 A |
91 | |
92 | /* | |
93 | * Ops vector for stdio type XDR | |
94 | */ | |
c29f2fcc | 95 | static const struct xdr_ops xdrstdio_ops = { |
03fb6eb0 A |
96 | xdrstdio_getlong, /* deseraialize a long int */ |
97 | xdrstdio_putlong, /* seraialize a long int */ | |
98 | xdrstdio_getbytes, /* deserialize counted bytes */ | |
99 | xdrstdio_putbytes, /* serialize counted bytes */ | |
100 | xdrstdio_getpos, /* get offset in the stream */ | |
101 | xdrstdio_setpos, /* set offset in the stream */ | |
102 | xdrstdio_inline, /* prime stream for inline macros */ | |
103 | xdrstdio_destroy /* destroy stream */ | |
104 | }; | |
105 | ||
106 | /* | |
107 | * Initialize a stdio xdr stream. | |
108 | * Sets the xdr stream handle xdrs for use on the stream file. | |
109 | * Operation flag is set to op. | |
110 | */ | |
111 | void | |
112 | xdrstdio_create(xdrs, file, op) | |
c29f2fcc | 113 | XDR *xdrs; |
03fb6eb0 A |
114 | FILE *file; |
115 | enum xdr_op op; | |
116 | { | |
03fb6eb0 A |
117 | xdrs->x_op = op; |
118 | xdrs->x_ops = &xdrstdio_ops; | |
c29f2fcc | 119 | xdrs->x_private = file; |
03fb6eb0 A |
120 | xdrs->x_handy = 0; |
121 | xdrs->x_base = 0; | |
122 | } | |
123 | ||
124 | /* | |
125 | * Destroy a stdio xdr stream. | |
126 | * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create. | |
127 | */ | |
128 | static void | |
129 | xdrstdio_destroy(xdrs) | |
c29f2fcc | 130 | XDR *xdrs; |
03fb6eb0 A |
131 | { |
132 | (void)fflush((FILE *)xdrs->x_private); | |
b3dd680f | 133 | /* XXX: should we close the file ?? */ |
c29f2fcc | 134 | } |
03fb6eb0 A |
135 | |
136 | static bool_t | |
137 | xdrstdio_getlong(xdrs, lp) | |
138 | XDR *xdrs; | |
b3dd680f A |
139 | #ifdef __LP64__ |
140 | int *lp; | |
141 | #else | |
c29f2fcc | 142 | long *lp; |
b3dd680f | 143 | #endif |
03fb6eb0 | 144 | { |
c29f2fcc | 145 | u_int32_t temp; |
03fb6eb0 | 146 | |
c29f2fcc | 147 | if (fread(&temp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1) |
03fb6eb0 | 148 | return (FALSE); |
b3dd680f | 149 | *lp = ntohl(temp); |
03fb6eb0 A |
150 | return (TRUE); |
151 | } | |
152 | ||
153 | static bool_t | |
154 | xdrstdio_putlong(xdrs, lp) | |
155 | XDR *xdrs; | |
b3dd680f A |
156 | #ifdef __LP64__ |
157 | const int *lp; | |
158 | #else | |
c29f2fcc | 159 | const long *lp; |
b3dd680f | 160 | #endif |
03fb6eb0 | 161 | { |
b3dd680f | 162 | int32_t mycopy = htonl(*lp); |
03fb6eb0 | 163 | |
b3dd680f | 164 | if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1) return (FALSE); |
03fb6eb0 A |
165 | return (TRUE); |
166 | } | |
167 | ||
168 | static bool_t | |
169 | xdrstdio_getbytes(xdrs, addr, len) | |
170 | XDR *xdrs; | |
c29f2fcc | 171 | char *addr; |
03fb6eb0 A |
172 | u_int len; |
173 | { | |
b3dd680f | 174 | size_t flen = len; |
03fb6eb0 | 175 | |
b3dd680f | 176 | if ((len != 0) && (fread(addr, flen, 1, (FILE *)xdrs->x_private) != 1)) return (FALSE); |
03fb6eb0 A |
177 | return (TRUE); |
178 | } | |
179 | ||
180 | static bool_t | |
181 | xdrstdio_putbytes(xdrs, addr, len) | |
182 | XDR *xdrs; | |
c29f2fcc | 183 | const char *addr; |
03fb6eb0 A |
184 | u_int len; |
185 | { | |
b3dd680f | 186 | size_t flen = len; |
03fb6eb0 | 187 | |
b3dd680f | 188 | if ((len != 0) && (fwrite(addr, flen, 1, (FILE *)xdrs->x_private) != 1)) return (FALSE); |
03fb6eb0 A |
189 | return (TRUE); |
190 | } | |
191 | ||
b3dd680f | 192 | /* This only works if file offsets are <= UINT32_MAX */ |
03fb6eb0 A |
193 | static u_int |
194 | xdrstdio_getpos(xdrs) | |
195 | XDR *xdrs; | |
196 | { | |
b3dd680f A |
197 | long offset; |
198 | u_int val; | |
03fb6eb0 | 199 | |
b3dd680f A |
200 | offset = ftell((FILE *)xdrs->x_private); |
201 | #ifdef __LP64__ | |
202 | if (offset > UINT32_MAX) return -1; | |
203 | #endif | |
204 | val = offset; | |
205 | return val; | |
03fb6eb0 A |
206 | } |
207 | ||
b3dd680f | 208 | /* This only works if file offsets are <= UINT32_MAX */ |
03fb6eb0 A |
209 | static bool_t |
210 | xdrstdio_setpos(xdrs, pos) | |
211 | XDR *xdrs; | |
212 | u_int pos; | |
213 | { | |
b3dd680f | 214 | long offset; |
03fb6eb0 | 215 | |
b3dd680f A |
216 | offset = pos; |
217 | return ((fseek((FILE *)xdrs->x_private, offset, 0) < 0) ? FALSE : TRUE); | |
03fb6eb0 A |
218 | } |
219 | ||
c29f2fcc A |
220 | /* ARGSUSED */ |
221 | static int32_t * | |
03fb6eb0 A |
222 | xdrstdio_inline(xdrs, len) |
223 | XDR *xdrs; | |
224 | u_int len; | |
225 | { | |
226 | ||
227 | /* | |
228 | * Must do some work to implement this: must insure | |
229 | * enough data in the underlying stdio buffer, | |
230 | * that the buffer is aligned so that we can indirect through a | |
231 | * long *, and stuff this pointer in xdrs->x_buf. Doing | |
232 | * a fread or fwrite to a scratch buffer would defeat | |
233 | * most of the gains to be had here and require storage | |
234 | * management on this buffer, so we don't do this. | |
235 | */ | |
236 | return (NULL); | |
237 | } |