4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
30 * Portions of this source code were derived from Berkeley
31 * 4.3 BSD under license from the Regents of the University of
35 #pragma ident "%Z%%M% %I% %E% SMI"
38 * xdr_stdio.c, XDR implementation on standard i/o file.
40 * This set of routines implements a XDR on a stdio stream.
41 * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
47 #include <rpc/types.h>
50 #include <sys/types.h>
53 static struct xdr_ops
*xdrstdio_ops(void);
56 * Initialize a stdio xdr stream.
57 * Sets the xdr stream handle xdrs for use on the stream file.
58 * Operation flag is set to op.
61 xdrstdio_create(XDR
*xdrs
, FILE *file
, const enum xdr_op op
)
64 xdrs
->x_ops
= xdrstdio_ops();
65 xdrs
->x_private
= (caddr_t
)file
;
71 * Destroy a stdio xdr stream.
72 * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
75 xdrstdio_destroy(XDR
*xdrs
)
77 /* LINTED pointer cast */
78 (void) fflush((FILE *)xdrs
->x_private
);
79 /* xx should we close the file ?? */
84 xdrstdio_getint32(XDR
*xdrs
, int32_t *lp
)
86 if (fread((caddr_t
)lp
, sizeof (int32_t), 1,
87 /* LINTED pointer cast */
88 (FILE *)xdrs
->x_private
) != 1)
95 xdrstdio_putint32(XDR
*xdrs
, int32_t *lp
)
98 int32_t mycopy
= htonl(*lp
);
101 if (fwrite((caddr_t
)lp
, sizeof (int32_t), 1,
102 /* LINTED pointer cast */
103 (FILE *)xdrs
->x_private
) != 1)
109 xdrstdio_getlong(XDR
*xdrs
, long *lp
)
113 if (!xdrstdio_getint32(xdrs
, &i
))
120 xdrstdio_putlong(XDR
*xdrs
, long *lp
)
125 if ((*lp
> INT32_MAX
) || (*lp
< INT32_MIN
))
130 return (xdrstdio_putint32(xdrs
, &i
));
134 xdrstdio_getbytes(XDR
*xdrs
, caddr_t addr
, int len
)
137 /* LINTED pointer cast */
138 (fread(addr
, (int)len
, 1, (FILE *)xdrs
->x_private
) != 1))
144 xdrstdio_putbytes(XDR
*xdrs
, caddr_t addr
, int len
)
147 /* LINTED pointer cast */
148 (fwrite(addr
, (int)len
, 1, (FILE *)xdrs
->x_private
) != 1))
154 xdrstdio_getpos(XDR
*xdrs
)
156 /* LINTED pointer cast */
157 return ((uint_t
)ftell((FILE *)xdrs
->x_private
));
161 xdrstdio_setpos(XDR
*xdrs
, uint_t pos
)
163 /* LINTED pointer cast */
164 return ((fseek((FILE *)xdrs
->x_private
,
165 (int)pos
, 0) < 0) ? FALSE
: TRUE
);
169 static rpc_inline_t
*
170 xdrstdio_inline(XDR
*xdrs
, int len
)
173 * Must do some work to implement this: must insure
174 * enough data in the underlying stdio buffer,
175 * that the buffer is aligned so that we can indirect through a
176 * long *, and stuff this pointer in xdrs->x_buf. Doing
177 * a fread or fwrite to a scratch buffer would defeat
178 * most of the gains to be had here and require storage
179 * management on this buffer, so we don't do this.
186 xdrstdio_control(XDR
*xdrs
, int request
, void *info
)
191 static struct xdr_ops
*
194 static struct xdr_ops ops
;
195 extern mutex_t ops_lock
;
197 /* VARIABLES PROTECTED BY ops_lock: ops */
199 (void) mutex_lock(&ops_lock
);
200 if (ops
.x_getlong
== NULL
) {
201 ops
.x_getlong
= xdrstdio_getlong
;
202 ops
.x_putlong
= xdrstdio_putlong
;
203 ops
.x_getbytes
= xdrstdio_getbytes
;
204 ops
.x_putbytes
= xdrstdio_putbytes
;
205 ops
.x_getpostn
= xdrstdio_getpos
;
206 ops
.x_setpostn
= xdrstdio_setpos
;
207 ops
.x_inline
= xdrstdio_inline
;
208 ops
.x_destroy
= xdrstdio_destroy
;
209 ops
.x_control
= xdrstdio_control
;
211 ops
.x_getint32
= xdrstdio_getint32
;
212 ops
.x_putint32
= xdrstdio_putint32
;
215 (void) mutex_unlock(&ops_lock
);