]> git.saurik.com Git - apple/libinfo.git/blame_incremental - rpc.subproj/xdr_stdio.c
Libinfo-542.40.3.tar.gz
[apple/libinfo.git] / rpc.subproj / xdr_stdio.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 1999-2018 Apple 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)
57static char *sccsid = "@(#)xdr_stdio.c 1.16 87/08/11 Copyr 1984 Sun Micro";
58static char *sccsid = "@(#)xdr_stdio.c 2.1 88/07/29 4.0 RPCSRC";
59#endif
60#include "libinfo_common.h"
61
62#include <sys/cdefs.h>
63
64/*
65 * xdr_stdio.c, XDR implementation on standard i/o file.
66 *
67 * Copyright (C) 1984, Sun Microsystems, Inc.
68 *
69 * This set of routines implements a XDR on a stdio stream.
70 * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
71 * from the stream.
72 */
73
74#include <stdio.h>
75
76#include <arpa/inet.h>
77#include <rpc/types.h>
78#include <rpc/xdr.h>
79
80static void xdrstdio_destroy(XDR *);
81#ifdef __LP64__
82static bool_t xdrstdio_getlong(XDR *, int *);
83static bool_t xdrstdio_putlong(XDR *, const int *);
84#else
85static bool_t xdrstdio_getlong(XDR *, long *);
86static bool_t xdrstdio_putlong(XDR *, const long *);
87#endif
88static bool_t xdrstdio_getbytes(XDR *, char *, u_int);
89static bool_t xdrstdio_putbytes(XDR *, const char *, u_int);
90static u_int xdrstdio_getpos(XDR *);
91static bool_t xdrstdio_setpos(XDR *, u_int);
92static int32_t *xdrstdio_inline(XDR *, u_int);
93
94/*
95 * Ops vector for stdio type XDR
96 */
97static const struct xdr_ops xdrstdio_ops = {
98 xdrstdio_getlong, /* deseraialize a long int */
99 xdrstdio_putlong, /* seraialize a long int */
100 xdrstdio_getbytes, /* deserialize counted bytes */
101 xdrstdio_putbytes, /* serialize counted bytes */
102 xdrstdio_getpos, /* get offset in the stream */
103 xdrstdio_setpos, /* set offset in the stream */
104 xdrstdio_inline, /* prime stream for inline macros */
105 xdrstdio_destroy /* destroy stream */
106};
107
108/*
109 * Initialize a stdio xdr stream.
110 * Sets the xdr stream handle xdrs for use on the stream file.
111 * Operation flag is set to op.
112 */
113LIBINFO_EXPORT
114void
115xdrstdio_create(xdrs, file, op)
116 XDR *xdrs;
117 FILE *file;
118 enum xdr_op op;
119{
120 xdrs->x_op = op;
121 xdrs->x_ops = &xdrstdio_ops;
122 xdrs->x_private = file;
123 xdrs->x_handy = 0;
124 xdrs->x_base = 0;
125}
126
127/*
128 * Destroy a stdio xdr stream.
129 * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
130 */
131static void
132xdrstdio_destroy(xdrs)
133 XDR *xdrs;
134{
135 (void)fflush((FILE *)xdrs->x_private);
136 /* XXX: should we close the file ?? */
137}
138
139static bool_t
140xdrstdio_getlong(xdrs, lp)
141 XDR *xdrs;
142#ifdef __LP64__
143 int *lp;
144#else
145 long *lp;
146#endif
147{
148 u_int32_t temp;
149
150 if (fread(&temp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
151 return (FALSE);
152 *lp = ntohl(temp);
153 return (TRUE);
154}
155
156static bool_t
157xdrstdio_putlong(xdrs, lp)
158 XDR *xdrs;
159#ifdef __LP64__
160 const int *lp;
161#else
162 const long *lp;
163#endif
164{
165 int32_t mycopy = htonl(*lp);
166
167 if (fwrite(&mycopy, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1) return (FALSE);
168 return (TRUE);
169}
170
171static bool_t
172xdrstdio_getbytes(xdrs, addr, len)
173 XDR *xdrs;
174 char *addr;
175 u_int len;
176{
177 size_t flen = len;
178
179 if ((len != 0) && (fread(addr, flen, 1, (FILE *)xdrs->x_private) != 1)) return (FALSE);
180 return (TRUE);
181}
182
183static bool_t
184xdrstdio_putbytes(xdrs, addr, len)
185 XDR *xdrs;
186 const char *addr;
187 u_int len;
188{
189 size_t flen = len;
190
191 if ((len != 0) && (fwrite(addr, flen, 1, (FILE *)xdrs->x_private) != 1)) return (FALSE);
192 return (TRUE);
193}
194
195/* This only works if file offsets are <= UINT32_MAX */
196static u_int
197xdrstdio_getpos(xdrs)
198 XDR *xdrs;
199{
200 long offset;
201 u_int val;
202
203 offset = ftell((FILE *)xdrs->x_private);
204#ifdef __LP64__
205 if (offset > UINT32_MAX) return -1;
206#endif
207 val = offset;
208 return val;
209}
210
211/* This only works if file offsets are <= UINT32_MAX */
212static bool_t
213xdrstdio_setpos(xdrs, pos)
214 XDR *xdrs;
215 u_int pos;
216{
217 long offset;
218
219 offset = pos;
220 return ((fseek((FILE *)xdrs->x_private, offset, 0) < 0) ? FALSE : TRUE);
221}
222
223/* ARGSUSED */
224static int32_t *
225xdrstdio_inline(xdrs, len)
226 XDR *xdrs;
227 u_int len;
228{
229
230 /*
231 * Must do some work to implement this: must insure
232 * enough data in the underlying stdio buffer,
233 * that the buffer is aligned so that we can indirect through a
234 * long *, and stuff this pointer in xdrs->x_buf. Doing
235 * a fread or fwrite to a scratch buffer would defeat
236 * most of the gains to be had here and require storage
237 * management on this buffer, so we don't do this.
238 */
239 return (NULL);
240}