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 #pragma ident "%Z%%M% %I% %E% SMI" 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate /* 387c478bd9Sstevel@tonic-gate * xdr_stdio.c, XDR implementation on standard i/o file. 397c478bd9Sstevel@tonic-gate * 407c478bd9Sstevel@tonic-gate * This set of routines implements a XDR on a stdio stream. 417c478bd9Sstevel@tonic-gate * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes 427c478bd9Sstevel@tonic-gate * from the stream. 437c478bd9Sstevel@tonic-gate */ 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate #include "mt.h" 467c478bd9Sstevel@tonic-gate #include "rpc_mt.h" 477c478bd9Sstevel@tonic-gate #include <rpc/types.h> 487c478bd9Sstevel@tonic-gate #include <stdio.h> 497c478bd9Sstevel@tonic-gate #include <rpc/xdr.h> 507c478bd9Sstevel@tonic-gate #include <sys/types.h> 517c478bd9Sstevel@tonic-gate #include <inttypes.h> 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate static struct xdr_ops *xdrstdio_ops(void); 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate /* 567c478bd9Sstevel@tonic-gate * Initialize a stdio xdr stream. 577c478bd9Sstevel@tonic-gate * Sets the xdr stream handle xdrs for use on the stream file. 587c478bd9Sstevel@tonic-gate * Operation flag is set to op. 597c478bd9Sstevel@tonic-gate */ 607c478bd9Sstevel@tonic-gate void 61*61961e0fSrobinson xdrstdio_create(XDR *xdrs, FILE *file, const enum xdr_op op) 627c478bd9Sstevel@tonic-gate { 637c478bd9Sstevel@tonic-gate xdrs->x_op = op; 647c478bd9Sstevel@tonic-gate xdrs->x_ops = xdrstdio_ops(); 657c478bd9Sstevel@tonic-gate xdrs->x_private = (caddr_t)file; 667c478bd9Sstevel@tonic-gate xdrs->x_handy = 0; 677c478bd9Sstevel@tonic-gate xdrs->x_base = 0; 687c478bd9Sstevel@tonic-gate } 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate /* 717c478bd9Sstevel@tonic-gate * Destroy a stdio xdr stream. 727c478bd9Sstevel@tonic-gate * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create. 737c478bd9Sstevel@tonic-gate */ 747c478bd9Sstevel@tonic-gate static void 757c478bd9Sstevel@tonic-gate xdrstdio_destroy(XDR *xdrs) 767c478bd9Sstevel@tonic-gate { 77*61961e0fSrobinson /* LINTED pointer cast */ 787c478bd9Sstevel@tonic-gate (void) fflush((FILE *)xdrs->x_private); 797c478bd9Sstevel@tonic-gate /* xx should we close the file ?? */ 807c478bd9Sstevel@tonic-gate } 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate static bool_t 847c478bd9Sstevel@tonic-gate xdrstdio_getint32(XDR *xdrs, int32_t *lp) 857c478bd9Sstevel@tonic-gate { 867c478bd9Sstevel@tonic-gate if (fread((caddr_t)lp, sizeof (int32_t), 1, 87*61961e0fSrobinson /* LINTED pointer cast */ 88*61961e0fSrobinson (FILE *)xdrs->x_private) != 1) 897c478bd9Sstevel@tonic-gate return (FALSE); 907c478bd9Sstevel@tonic-gate *lp = ntohl(*lp); 917c478bd9Sstevel@tonic-gate return (TRUE); 927c478bd9Sstevel@tonic-gate } 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate static bool_t 957c478bd9Sstevel@tonic-gate xdrstdio_putint32(XDR *xdrs, int32_t *lp) 967c478bd9Sstevel@tonic-gate { 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate int32_t mycopy = htonl(*lp); 997c478bd9Sstevel@tonic-gate lp = &mycopy; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate if (fwrite((caddr_t)lp, sizeof (int32_t), 1, 102*61961e0fSrobinson /* LINTED pointer cast */ 103*61961e0fSrobinson (FILE *)xdrs->x_private) != 1) 1047c478bd9Sstevel@tonic-gate return (FALSE); 1057c478bd9Sstevel@tonic-gate return (TRUE); 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate static bool_t 109*61961e0fSrobinson xdrstdio_getlong(XDR *xdrs, long *lp) 1107c478bd9Sstevel@tonic-gate { 1117c478bd9Sstevel@tonic-gate int32_t i; 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate if (!xdrstdio_getint32(xdrs, &i)) 1147c478bd9Sstevel@tonic-gate return (FALSE); 1157c478bd9Sstevel@tonic-gate *lp = (long)i; 1167c478bd9Sstevel@tonic-gate return (TRUE); 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate static bool_t 120*61961e0fSrobinson xdrstdio_putlong(XDR *xdrs, long *lp) 1217c478bd9Sstevel@tonic-gate { 1227c478bd9Sstevel@tonic-gate int32_t i; 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate #if defined(_LP64) 125*61961e0fSrobinson if ((*lp > INT32_MAX) || (*lp < INT32_MIN)) 1267c478bd9Sstevel@tonic-gate return (FALSE); 1277c478bd9Sstevel@tonic-gate #endif 1287c478bd9Sstevel@tonic-gate i = (int32_t)*lp; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate return (xdrstdio_putint32(xdrs, &i)); 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate static bool_t 1347c478bd9Sstevel@tonic-gate xdrstdio_getbytes(XDR *xdrs, caddr_t addr, int len) 1357c478bd9Sstevel@tonic-gate { 1367c478bd9Sstevel@tonic-gate if ((len != 0) && 137*61961e0fSrobinson /* LINTED pointer cast */ 138*61961e0fSrobinson (fread(addr, (int)len, 1, (FILE *)xdrs->x_private) != 1)) 1397c478bd9Sstevel@tonic-gate return (FALSE); 1407c478bd9Sstevel@tonic-gate return (TRUE); 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate static bool_t 1447c478bd9Sstevel@tonic-gate xdrstdio_putbytes(XDR *xdrs, caddr_t addr, int len) 1457c478bd9Sstevel@tonic-gate { 1467c478bd9Sstevel@tonic-gate if ((len != 0) && 147*61961e0fSrobinson /* LINTED pointer cast */ 148*61961e0fSrobinson (fwrite(addr, (int)len, 1, (FILE *)xdrs->x_private) != 1)) 1497c478bd9Sstevel@tonic-gate return (FALSE); 1507c478bd9Sstevel@tonic-gate return (TRUE); 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate static uint_t 1547c478bd9Sstevel@tonic-gate xdrstdio_getpos(XDR *xdrs) 1557c478bd9Sstevel@tonic-gate { 156*61961e0fSrobinson /* LINTED pointer cast */ 157*61961e0fSrobinson return ((uint_t)ftell((FILE *)xdrs->x_private)); 1587c478bd9Sstevel@tonic-gate } 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate static bool_t 1617c478bd9Sstevel@tonic-gate xdrstdio_setpos(XDR *xdrs, uint_t pos) 1627c478bd9Sstevel@tonic-gate { 163*61961e0fSrobinson /* LINTED pointer cast */ 164*61961e0fSrobinson return ((fseek((FILE *)xdrs->x_private, 165*61961e0fSrobinson (int)pos, 0) < 0) ? FALSE : TRUE); 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate 168*61961e0fSrobinson /* ARGSUSED */ 1697c478bd9Sstevel@tonic-gate static rpc_inline_t * 1707c478bd9Sstevel@tonic-gate xdrstdio_inline(XDR *xdrs, int len) 1717c478bd9Sstevel@tonic-gate { 1727c478bd9Sstevel@tonic-gate /* 1737c478bd9Sstevel@tonic-gate * Must do some work to implement this: must insure 1747c478bd9Sstevel@tonic-gate * enough data in the underlying stdio buffer, 1757c478bd9Sstevel@tonic-gate * that the buffer is aligned so that we can indirect through a 1767c478bd9Sstevel@tonic-gate * long *, and stuff this pointer in xdrs->x_buf. Doing 1777c478bd9Sstevel@tonic-gate * a fread or fwrite to a scratch buffer would defeat 1787c478bd9Sstevel@tonic-gate * most of the gains to be had here and require storage 1797c478bd9Sstevel@tonic-gate * management on this buffer, so we don't do this. 1807c478bd9Sstevel@tonic-gate */ 1817c478bd9Sstevel@tonic-gate return (NULL); 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate 184*61961e0fSrobinson /* ARGSUSED */ 1857c478bd9Sstevel@tonic-gate static bool_t 1867c478bd9Sstevel@tonic-gate xdrstdio_control(XDR *xdrs, int request, void *info) 1877c478bd9Sstevel@tonic-gate { 1887c478bd9Sstevel@tonic-gate return (FALSE); 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate static struct xdr_ops * 192*61961e0fSrobinson xdrstdio_ops(void) 1937c478bd9Sstevel@tonic-gate { 1947c478bd9Sstevel@tonic-gate static struct xdr_ops ops; 1957c478bd9Sstevel@tonic-gate extern mutex_t ops_lock; 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY ops_lock: ops */ 1987c478bd9Sstevel@tonic-gate 199*61961e0fSrobinson (void) mutex_lock(&ops_lock); 2007c478bd9Sstevel@tonic-gate if (ops.x_getlong == NULL) { 2017c478bd9Sstevel@tonic-gate ops.x_getlong = xdrstdio_getlong; 2027c478bd9Sstevel@tonic-gate ops.x_putlong = xdrstdio_putlong; 2037c478bd9Sstevel@tonic-gate ops.x_getbytes = xdrstdio_getbytes; 2047c478bd9Sstevel@tonic-gate ops.x_putbytes = xdrstdio_putbytes; 2057c478bd9Sstevel@tonic-gate ops.x_getpostn = xdrstdio_getpos; 2067c478bd9Sstevel@tonic-gate ops.x_setpostn = xdrstdio_setpos; 2077c478bd9Sstevel@tonic-gate ops.x_inline = xdrstdio_inline; 2087c478bd9Sstevel@tonic-gate ops.x_destroy = xdrstdio_destroy; 2097c478bd9Sstevel@tonic-gate ops.x_control = xdrstdio_control; 2107c478bd9Sstevel@tonic-gate #if defined(_LP64) 2117c478bd9Sstevel@tonic-gate ops.x_getint32 = xdrstdio_getint32; 2127c478bd9Sstevel@tonic-gate ops.x_putint32 = xdrstdio_putint32; 2137c478bd9Sstevel@tonic-gate #endif 2147c478bd9Sstevel@tonic-gate } 215*61961e0fSrobinson (void) mutex_unlock(&ops_lock); 2167c478bd9Sstevel@tonic-gate return (&ops); 2177c478bd9Sstevel@tonic-gate } 218