xref: /onnv-gate/usr/src/lib/libnsl/rpc/xdr_stdio.c (revision 132:e3f7eaf7dde4)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
21*132Srobinson  */
22*132Srobinson 
23*132Srobinson /*
24*132Srobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
310Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
320Sstevel@tonic-gate  * California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * xdr_stdio.c, XDR implementation on standard i/o file.
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  * This set of routines implements a XDR on a stdio stream.
410Sstevel@tonic-gate  * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
420Sstevel@tonic-gate  * from the stream.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include "mt.h"
460Sstevel@tonic-gate #include "rpc_mt.h"
470Sstevel@tonic-gate #include <rpc/types.h>
480Sstevel@tonic-gate #include <stdio.h>
490Sstevel@tonic-gate #include <rpc/xdr.h>
500Sstevel@tonic-gate #include <sys/types.h>
510Sstevel@tonic-gate #include <inttypes.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static struct xdr_ops *xdrstdio_ops(void);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * Initialize a stdio xdr stream.
570Sstevel@tonic-gate  * Sets the xdr stream handle xdrs for use on the stream file.
580Sstevel@tonic-gate  * Operation flag is set to op.
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate void
xdrstdio_create(XDR * xdrs,FILE * file,const enum xdr_op op)61*132Srobinson xdrstdio_create(XDR *xdrs, FILE *file, const enum xdr_op op)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	xdrs->x_op = op;
640Sstevel@tonic-gate 	xdrs->x_ops = xdrstdio_ops();
650Sstevel@tonic-gate 	xdrs->x_private = (caddr_t)file;
660Sstevel@tonic-gate 	xdrs->x_handy = 0;
670Sstevel@tonic-gate 	xdrs->x_base = 0;
680Sstevel@tonic-gate }
690Sstevel@tonic-gate 
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate  * Destroy a stdio xdr stream.
720Sstevel@tonic-gate  * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate static void
xdrstdio_destroy(XDR * xdrs)750Sstevel@tonic-gate xdrstdio_destroy(XDR *xdrs)
760Sstevel@tonic-gate {
77*132Srobinson 	/* LINTED pointer cast */
780Sstevel@tonic-gate 	(void) fflush((FILE *)xdrs->x_private);
790Sstevel@tonic-gate 	/* xx should we close the file ?? */
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 
830Sstevel@tonic-gate static bool_t
xdrstdio_getint32(XDR * xdrs,int32_t * lp)840Sstevel@tonic-gate xdrstdio_getint32(XDR *xdrs, int32_t *lp)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate 	if (fread((caddr_t)lp, sizeof (int32_t), 1,
87*132Srobinson 			/* LINTED pointer cast */
88*132Srobinson 			(FILE *)xdrs->x_private) != 1)
890Sstevel@tonic-gate 		return (FALSE);
900Sstevel@tonic-gate 	*lp = ntohl(*lp);
910Sstevel@tonic-gate 	return (TRUE);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate static bool_t
xdrstdio_putint32(XDR * xdrs,int32_t * lp)950Sstevel@tonic-gate xdrstdio_putint32(XDR *xdrs, int32_t *lp)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	int32_t mycopy = htonl(*lp);
990Sstevel@tonic-gate 	lp = &mycopy;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if (fwrite((caddr_t)lp, sizeof (int32_t), 1,
102*132Srobinson 			/* LINTED pointer cast */
103*132Srobinson 			(FILE *)xdrs->x_private) != 1)
1040Sstevel@tonic-gate 		return (FALSE);
1050Sstevel@tonic-gate 	return (TRUE);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static bool_t
xdrstdio_getlong(XDR * xdrs,long * lp)109*132Srobinson xdrstdio_getlong(XDR *xdrs, long *lp)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	int32_t i;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	if (!xdrstdio_getint32(xdrs, &i))
1140Sstevel@tonic-gate 		return (FALSE);
1150Sstevel@tonic-gate 	*lp = (long)i;
1160Sstevel@tonic-gate 	return (TRUE);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate static bool_t
xdrstdio_putlong(XDR * xdrs,long * lp)120*132Srobinson xdrstdio_putlong(XDR *xdrs, long *lp)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	int32_t i;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate #if defined(_LP64)
125*132Srobinson 	if ((*lp > INT32_MAX) || (*lp < INT32_MIN))
1260Sstevel@tonic-gate 		return (FALSE);
1270Sstevel@tonic-gate #endif
1280Sstevel@tonic-gate 	i = (int32_t)*lp;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	return (xdrstdio_putint32(xdrs, &i));
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate static bool_t
xdrstdio_getbytes(XDR * xdrs,caddr_t addr,int len)1340Sstevel@tonic-gate xdrstdio_getbytes(XDR *xdrs, caddr_t addr, int len)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate 	if ((len != 0) &&
137*132Srobinson 		/* LINTED pointer cast */
138*132Srobinson 		(fread(addr, (int)len, 1, (FILE *)xdrs->x_private) != 1))
1390Sstevel@tonic-gate 		return (FALSE);
1400Sstevel@tonic-gate 	return (TRUE);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate static bool_t
xdrstdio_putbytes(XDR * xdrs,caddr_t addr,int len)1440Sstevel@tonic-gate xdrstdio_putbytes(XDR *xdrs, caddr_t addr, int len)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	if ((len != 0) &&
147*132Srobinson 		/* LINTED pointer cast */
148*132Srobinson 		(fwrite(addr, (int)len, 1, (FILE *)xdrs->x_private) != 1))
1490Sstevel@tonic-gate 		return (FALSE);
1500Sstevel@tonic-gate 	return (TRUE);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate static uint_t
xdrstdio_getpos(XDR * xdrs)1540Sstevel@tonic-gate xdrstdio_getpos(XDR *xdrs)
1550Sstevel@tonic-gate {
156*132Srobinson 	/* LINTED pointer cast */
157*132Srobinson 	return ((uint_t)ftell((FILE *)xdrs->x_private));
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate static bool_t
xdrstdio_setpos(XDR * xdrs,uint_t pos)1610Sstevel@tonic-gate xdrstdio_setpos(XDR *xdrs, uint_t pos)
1620Sstevel@tonic-gate {
163*132Srobinson 	/* LINTED pointer cast */
164*132Srobinson 	return ((fseek((FILE *)xdrs->x_private,
165*132Srobinson 			(int)pos, 0) < 0) ? FALSE : TRUE);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
168*132Srobinson /* ARGSUSED */
1690Sstevel@tonic-gate static rpc_inline_t *
xdrstdio_inline(XDR * xdrs,int len)1700Sstevel@tonic-gate xdrstdio_inline(XDR *xdrs, int len)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate 	/*
1730Sstevel@tonic-gate 	 * Must do some work to implement this: must insure
1740Sstevel@tonic-gate 	 * enough data in the underlying stdio buffer,
1750Sstevel@tonic-gate 	 * that the buffer is aligned so that we can indirect through a
1760Sstevel@tonic-gate 	 * long *, and stuff this pointer in xdrs->x_buf.  Doing
1770Sstevel@tonic-gate 	 * a fread or fwrite to a scratch buffer would defeat
1780Sstevel@tonic-gate 	 * most of the gains to be had here and require storage
1790Sstevel@tonic-gate 	 * management on this buffer, so we don't do this.
1800Sstevel@tonic-gate 	 */
1810Sstevel@tonic-gate 	return (NULL);
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
184*132Srobinson /* ARGSUSED */
1850Sstevel@tonic-gate static bool_t
xdrstdio_control(XDR * xdrs,int request,void * info)1860Sstevel@tonic-gate xdrstdio_control(XDR *xdrs, int request, void *info)
1870Sstevel@tonic-gate {
188*132Srobinson 	return (FALSE);
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate static struct xdr_ops *
xdrstdio_ops(void)192*132Srobinson xdrstdio_ops(void)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate 	static struct xdr_ops ops;
1950Sstevel@tonic-gate 	extern mutex_t	ops_lock;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate /* VARIABLES PROTECTED BY ops_lock: ops */
1980Sstevel@tonic-gate 
199*132Srobinson 	(void) mutex_lock(&ops_lock);
2000Sstevel@tonic-gate 	if (ops.x_getlong == NULL) {
2010Sstevel@tonic-gate 		ops.x_getlong = xdrstdio_getlong;
2020Sstevel@tonic-gate 		ops.x_putlong = xdrstdio_putlong;
2030Sstevel@tonic-gate 		ops.x_getbytes = xdrstdio_getbytes;
2040Sstevel@tonic-gate 		ops.x_putbytes = xdrstdio_putbytes;
2050Sstevel@tonic-gate 		ops.x_getpostn = xdrstdio_getpos;
2060Sstevel@tonic-gate 		ops.x_setpostn = xdrstdio_setpos;
2070Sstevel@tonic-gate 		ops.x_inline = xdrstdio_inline;
2080Sstevel@tonic-gate 		ops.x_destroy = xdrstdio_destroy;
2090Sstevel@tonic-gate 		ops.x_control = xdrstdio_control;
2100Sstevel@tonic-gate #if defined(_LP64)
2110Sstevel@tonic-gate 		ops.x_getint32 = xdrstdio_getint32;
2120Sstevel@tonic-gate 		ops.x_putint32 = xdrstdio_putint32;
2130Sstevel@tonic-gate #endif
2140Sstevel@tonic-gate 	}
215*132Srobinson 	(void) mutex_unlock(&ops_lock);
2160Sstevel@tonic-gate 	return (&ops);
2170Sstevel@tonic-gate }
218