2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A/*
2N/A * Portions of this source code were derived from Berkeley
2N/A * 4.3 BSD under license from the Regents of the University of
2N/A * California.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * xdr_stdio.c, XDR implementation on standard i/o file.
2N/A *
2N/A * This set of routines implements a XDR on a stdio stream.
2N/A * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
2N/A * from the stream.
2N/A */
2N/A
2N/A#include "mt.h"
2N/A#include "rpc_mt.h"
2N/A#include <rpc/types.h>
2N/A#include <stdio.h>
2N/A#include <rpc/xdr.h>
2N/A#include <sys/types.h>
2N/A#include <inttypes.h>
2N/A
2N/Astatic struct xdr_ops *xdrstdio_ops(void);
2N/A
2N/A/*
2N/A * Initialize a stdio xdr stream.
2N/A * Sets the xdr stream handle xdrs for use on the stream file.
2N/A * Operation flag is set to op.
2N/A */
2N/Avoid
2N/Axdrstdio_create(XDR *xdrs, FILE *file, const enum xdr_op op)
2N/A{
2N/A xdrs->x_op = op;
2N/A xdrs->x_ops = xdrstdio_ops();
2N/A xdrs->x_private = (caddr_t)file;
2N/A xdrs->x_handy = 0;
2N/A xdrs->x_base = 0;
2N/A}
2N/A
2N/A/*
2N/A * Destroy a stdio xdr stream.
2N/A * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
2N/A */
2N/Astatic void
2N/Axdrstdio_destroy(XDR *xdrs)
2N/A{
2N/A /* LINTED pointer cast */
2N/A (void) fflush((FILE *)xdrs->x_private);
2N/A /* xx should we close the file ?? */
2N/A}
2N/A
2N/A
2N/Astatic bool_t
2N/Axdrstdio_getint32(XDR *xdrs, int32_t *lp)
2N/A{
2N/A if (fread((caddr_t)lp, sizeof (int32_t), 1,
2N/A /* LINTED pointer cast */
2N/A (FILE *)xdrs->x_private) != 1)
2N/A return (FALSE);
2N/A *lp = ntohl(*lp);
2N/A return (TRUE);
2N/A}
2N/A
2N/Astatic bool_t
2N/Axdrstdio_putint32(XDR *xdrs, int32_t *lp)
2N/A{
2N/A
2N/A int32_t mycopy = htonl(*lp);
2N/A lp = &mycopy;
2N/A
2N/A if (fwrite((caddr_t)lp, sizeof (int32_t), 1,
2N/A /* LINTED pointer cast */
2N/A (FILE *)xdrs->x_private) != 1)
2N/A return (FALSE);
2N/A return (TRUE);
2N/A}
2N/A
2N/Astatic bool_t
2N/Axdrstdio_getlong(XDR *xdrs, long *lp)
2N/A{
2N/A int32_t i;
2N/A
2N/A if (!xdrstdio_getint32(xdrs, &i))
2N/A return (FALSE);
2N/A *lp = (long)i;
2N/A return (TRUE);
2N/A}
2N/A
2N/Astatic bool_t
2N/Axdrstdio_putlong(XDR *xdrs, long *lp)
2N/A{
2N/A int32_t i;
2N/A
2N/A#if defined(_LP64)
2N/A if ((*lp > INT32_MAX) || (*lp < INT32_MIN))
2N/A return (FALSE);
2N/A#endif
2N/A i = (int32_t)*lp;
2N/A
2N/A return (xdrstdio_putint32(xdrs, &i));
2N/A}
2N/A
2N/Astatic bool_t
2N/Axdrstdio_getbytes(XDR *xdrs, caddr_t addr, int len)
2N/A{
2N/A if ((len != 0) &&
2N/A /* LINTED pointer cast */
2N/A (fread(addr, (int)len, 1, (FILE *)xdrs->x_private) != 1))
2N/A return (FALSE);
2N/A return (TRUE);
2N/A}
2N/A
2N/Astatic bool_t
2N/Axdrstdio_putbytes(XDR *xdrs, caddr_t addr, int len)
2N/A{
2N/A if ((len != 0) &&
2N/A /* LINTED pointer cast */
2N/A (fwrite(addr, (int)len, 1, (FILE *)xdrs->x_private) != 1))
2N/A return (FALSE);
2N/A return (TRUE);
2N/A}
2N/A
2N/Astatic uint_t
2N/Axdrstdio_getpos(XDR *xdrs)
2N/A{
2N/A /* LINTED pointer cast */
2N/A return ((uint_t)ftell((FILE *)xdrs->x_private));
2N/A}
2N/A
2N/Astatic bool_t
2N/Axdrstdio_setpos(XDR *xdrs, uint_t pos)
2N/A{
2N/A /* LINTED pointer cast */
2N/A return ((fseek((FILE *)xdrs->x_private,
2N/A (int)pos, 0) < 0) ? FALSE : TRUE);
2N/A}
2N/A
2N/A/* ARGSUSED */
2N/Astatic rpc_inline_t *
2N/Axdrstdio_inline(XDR *xdrs, int len)
2N/A{
2N/A /*
2N/A * Must do some work to implement this: must insure
2N/A * enough data in the underlying stdio buffer,
2N/A * that the buffer is aligned so that we can indirect through a
2N/A * long *, and stuff this pointer in xdrs->x_buf. Doing
2N/A * a fread or fwrite to a scratch buffer would defeat
2N/A * most of the gains to be had here and require storage
2N/A * management on this buffer, so we don't do this.
2N/A */
2N/A return (NULL);
2N/A}
2N/A
2N/A/* ARGSUSED */
2N/Astatic bool_t
2N/Axdrstdio_control(XDR *xdrs, int request, void *info)
2N/A{
2N/A return (FALSE);
2N/A}
2N/A
2N/Astatic struct xdr_ops *
2N/Axdrstdio_ops(void)
2N/A{
2N/A static struct xdr_ops ops;
2N/A extern mutex_t ops_lock;
2N/A
2N/A/* VARIABLES PROTECTED BY ops_lock: ops */
2N/A
2N/A (void) mutex_lock(&ops_lock);
2N/A if (ops.x_getlong == NULL) {
2N/A ops.x_getlong = xdrstdio_getlong;
2N/A ops.x_putlong = xdrstdio_putlong;
2N/A ops.x_getbytes = xdrstdio_getbytes;
2N/A ops.x_putbytes = xdrstdio_putbytes;
2N/A ops.x_getpostn = xdrstdio_getpos;
2N/A ops.x_setpostn = xdrstdio_setpos;
2N/A ops.x_inline = xdrstdio_inline;
2N/A ops.x_destroy = xdrstdio_destroy;
2N/A ops.x_control = xdrstdio_control;
2N/A#if defined(_LP64)
2N/A ops.x_getint32 = xdrstdio_getint32;
2N/A ops.x_putint32 = xdrstdio_putint32;
2N/A#endif
2N/A }
2N/A (void) mutex_unlock(&ops_lock);
2N/A return (&ops);
2N/A}