xref: /illumos-gate/usr/src/lib/libnsl/rpc/xdr_stdio.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*61961e0fSrobinson  */
22*61961e0fSrobinson 
23*61961e0fSrobinson /*
24*61961e0fSrobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
317c478bd9Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
327c478bd9Sstevel@tonic-gate  * California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * xdr_stdio.c, XDR implementation on standard i/o file.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * This set of routines implements a XDR on a stdio stream.
397c478bd9Sstevel@tonic-gate  * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
407c478bd9Sstevel@tonic-gate  * from the stream.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include "mt.h"
447c478bd9Sstevel@tonic-gate #include "rpc_mt.h"
457c478bd9Sstevel@tonic-gate #include <rpc/types.h>
467c478bd9Sstevel@tonic-gate #include <stdio.h>
477c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
487c478bd9Sstevel@tonic-gate #include <sys/types.h>
497c478bd9Sstevel@tonic-gate #include <inttypes.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static struct xdr_ops *xdrstdio_ops(void);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * Initialize a stdio xdr stream.
557c478bd9Sstevel@tonic-gate  * Sets the xdr stream handle xdrs for use on the stream file.
567c478bd9Sstevel@tonic-gate  * Operation flag is set to op.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate void
xdrstdio_create(XDR * xdrs,FILE * file,const enum xdr_op op)59*61961e0fSrobinson xdrstdio_create(XDR *xdrs, FILE *file, const enum xdr_op op)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	xdrs->x_op = op;
627c478bd9Sstevel@tonic-gate 	xdrs->x_ops = xdrstdio_ops();
637c478bd9Sstevel@tonic-gate 	xdrs->x_private = (caddr_t)file;
647c478bd9Sstevel@tonic-gate 	xdrs->x_handy = 0;
657c478bd9Sstevel@tonic-gate 	xdrs->x_base = 0;
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * Destroy a stdio xdr stream.
707c478bd9Sstevel@tonic-gate  * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate static void
xdrstdio_destroy(XDR * xdrs)737c478bd9Sstevel@tonic-gate xdrstdio_destroy(XDR *xdrs)
747c478bd9Sstevel@tonic-gate {
75*61961e0fSrobinson 	/* LINTED pointer cast */
767c478bd9Sstevel@tonic-gate 	(void) fflush((FILE *)xdrs->x_private);
777c478bd9Sstevel@tonic-gate 	/* xx should we close the file ?? */
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static bool_t
xdrstdio_getint32(XDR * xdrs,int32_t * lp)827c478bd9Sstevel@tonic-gate xdrstdio_getint32(XDR *xdrs, int32_t *lp)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	if (fread((caddr_t)lp, sizeof (int32_t), 1,
85*61961e0fSrobinson 			/* LINTED pointer cast */
86*61961e0fSrobinson 			(FILE *)xdrs->x_private) != 1)
877c478bd9Sstevel@tonic-gate 		return (FALSE);
887c478bd9Sstevel@tonic-gate 	*lp = ntohl(*lp);
897c478bd9Sstevel@tonic-gate 	return (TRUE);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static bool_t
xdrstdio_putint32(XDR * xdrs,int32_t * lp)937c478bd9Sstevel@tonic-gate xdrstdio_putint32(XDR *xdrs, int32_t *lp)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	int32_t mycopy = htonl(*lp);
977c478bd9Sstevel@tonic-gate 	lp = &mycopy;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if (fwrite((caddr_t)lp, sizeof (int32_t), 1,
100*61961e0fSrobinson 			/* LINTED pointer cast */
101*61961e0fSrobinson 			(FILE *)xdrs->x_private) != 1)
1027c478bd9Sstevel@tonic-gate 		return (FALSE);
1037c478bd9Sstevel@tonic-gate 	return (TRUE);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static bool_t
xdrstdio_getlong(XDR * xdrs,long * lp)107*61961e0fSrobinson xdrstdio_getlong(XDR *xdrs, long *lp)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	int32_t i;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (!xdrstdio_getint32(xdrs, &i))
1127c478bd9Sstevel@tonic-gate 		return (FALSE);
1137c478bd9Sstevel@tonic-gate 	*lp = (long)i;
1147c478bd9Sstevel@tonic-gate 	return (TRUE);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static bool_t
xdrstdio_putlong(XDR * xdrs,long * lp)118*61961e0fSrobinson xdrstdio_putlong(XDR *xdrs, long *lp)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	int32_t i;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate #if defined(_LP64)
123*61961e0fSrobinson 	if ((*lp > INT32_MAX) || (*lp < INT32_MIN))
1247c478bd9Sstevel@tonic-gate 		return (FALSE);
1257c478bd9Sstevel@tonic-gate #endif
1267c478bd9Sstevel@tonic-gate 	i = (int32_t)*lp;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	return (xdrstdio_putint32(xdrs, &i));
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static bool_t
xdrstdio_getbytes(XDR * xdrs,caddr_t addr,int len)1327c478bd9Sstevel@tonic-gate xdrstdio_getbytes(XDR *xdrs, caddr_t addr, int len)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	if ((len != 0) &&
135*61961e0fSrobinson 		/* LINTED pointer cast */
136*61961e0fSrobinson 		(fread(addr, (int)len, 1, (FILE *)xdrs->x_private) != 1))
1377c478bd9Sstevel@tonic-gate 		return (FALSE);
1387c478bd9Sstevel@tonic-gate 	return (TRUE);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate static bool_t
xdrstdio_putbytes(XDR * xdrs,caddr_t addr,int len)1427c478bd9Sstevel@tonic-gate xdrstdio_putbytes(XDR *xdrs, caddr_t addr, int len)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	if ((len != 0) &&
145*61961e0fSrobinson 		/* LINTED pointer cast */
146*61961e0fSrobinson 		(fwrite(addr, (int)len, 1, (FILE *)xdrs->x_private) != 1))
1477c478bd9Sstevel@tonic-gate 		return (FALSE);
1487c478bd9Sstevel@tonic-gate 	return (TRUE);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static uint_t
xdrstdio_getpos(XDR * xdrs)1527c478bd9Sstevel@tonic-gate xdrstdio_getpos(XDR *xdrs)
1537c478bd9Sstevel@tonic-gate {
154*61961e0fSrobinson 	/* LINTED pointer cast */
155*61961e0fSrobinson 	return ((uint_t)ftell((FILE *)xdrs->x_private));
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate static bool_t
xdrstdio_setpos(XDR * xdrs,uint_t pos)1597c478bd9Sstevel@tonic-gate xdrstdio_setpos(XDR *xdrs, uint_t pos)
1607c478bd9Sstevel@tonic-gate {
161*61961e0fSrobinson 	/* LINTED pointer cast */
162*61961e0fSrobinson 	return ((fseek((FILE *)xdrs->x_private,
163*61961e0fSrobinson 			(int)pos, 0) < 0) ? FALSE : TRUE);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
166*61961e0fSrobinson /* ARGSUSED */
1677c478bd9Sstevel@tonic-gate static rpc_inline_t *
xdrstdio_inline(XDR * xdrs,int len)1687c478bd9Sstevel@tonic-gate xdrstdio_inline(XDR *xdrs, int len)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	/*
1717c478bd9Sstevel@tonic-gate 	 * Must do some work to implement this: must insure
1727c478bd9Sstevel@tonic-gate 	 * enough data in the underlying stdio buffer,
1737c478bd9Sstevel@tonic-gate 	 * that the buffer is aligned so that we can indirect through a
1747c478bd9Sstevel@tonic-gate 	 * long *, and stuff this pointer in xdrs->x_buf.  Doing
1757c478bd9Sstevel@tonic-gate 	 * a fread or fwrite to a scratch buffer would defeat
1767c478bd9Sstevel@tonic-gate 	 * most of the gains to be had here and require storage
1777c478bd9Sstevel@tonic-gate 	 * management on this buffer, so we don't do this.
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 	return (NULL);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
182*61961e0fSrobinson /* ARGSUSED */
1837c478bd9Sstevel@tonic-gate static bool_t
xdrstdio_control(XDR * xdrs,int request,void * info)1847c478bd9Sstevel@tonic-gate xdrstdio_control(XDR *xdrs, int request, void *info)
1857c478bd9Sstevel@tonic-gate {
186*61961e0fSrobinson 	return (FALSE);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate static struct xdr_ops *
xdrstdio_ops(void)190*61961e0fSrobinson xdrstdio_ops(void)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	static struct xdr_ops ops;
1937c478bd9Sstevel@tonic-gate 	extern mutex_t	ops_lock;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY ops_lock: ops */
1967c478bd9Sstevel@tonic-gate 
197*61961e0fSrobinson 	(void) mutex_lock(&ops_lock);
1987c478bd9Sstevel@tonic-gate 	if (ops.x_getlong == NULL) {
1997c478bd9Sstevel@tonic-gate 		ops.x_getlong = xdrstdio_getlong;
2007c478bd9Sstevel@tonic-gate 		ops.x_putlong = xdrstdio_putlong;
2017c478bd9Sstevel@tonic-gate 		ops.x_getbytes = xdrstdio_getbytes;
2027c478bd9Sstevel@tonic-gate 		ops.x_putbytes = xdrstdio_putbytes;
2037c478bd9Sstevel@tonic-gate 		ops.x_getpostn = xdrstdio_getpos;
2047c478bd9Sstevel@tonic-gate 		ops.x_setpostn = xdrstdio_setpos;
2057c478bd9Sstevel@tonic-gate 		ops.x_inline = xdrstdio_inline;
2067c478bd9Sstevel@tonic-gate 		ops.x_destroy = xdrstdio_destroy;
2077c478bd9Sstevel@tonic-gate 		ops.x_control = xdrstdio_control;
2087c478bd9Sstevel@tonic-gate #if defined(_LP64)
2097c478bd9Sstevel@tonic-gate 		ops.x_getint32 = xdrstdio_getint32;
2107c478bd9Sstevel@tonic-gate 		ops.x_putint32 = xdrstdio_putint32;
2117c478bd9Sstevel@tonic-gate #endif
2127c478bd9Sstevel@tonic-gate 	}
213*61961e0fSrobinson 	(void) mutex_unlock(&ops_lock);
2147c478bd9Sstevel@tonic-gate 	return (&ops);
2157c478bd9Sstevel@tonic-gate }
216