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 (the "License").
2N/A * You may not use this file except in compliance 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 (c) 1986, 2012, Oracle and/or its affiliates. All rights reserved.
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/*
2N/A * xdr_mem.h, XDR implementation using memory buffers.
2N/A *
2N/A * If you have some data to be interpreted as external data representation
2N/A * or to be converted to external data representation in a memory buffer,
2N/A * then this is the package for you.
2N/A */
2N/A
2N/A#include "mt.h"
2N/A#include "rpc_mt.h"
2N/A#include <sys/types.h>
2N/A#include <rpc/types.h>
2N/A#include <rpc/xdr.h>
2N/A#include <memory.h>
2N/A#include <inttypes.h>
2N/A
2N/Astatic struct xdr_ops *xdrmem_ops(void);
2N/A
2N/A/*
2N/A * Meaning of the private areas of the xdr struct for xdr_mem
2N/A * x_base : Base from where the xdr stream starts
2N/A * x_private : The current position of the stream.
2N/A * x_handy : The size of the stream buffer.
2N/A */
2N/A
2N/A/*
2N/A * The procedure xdrmem_create initializes a stream descriptor for a
2N/A * memory buffer.
2N/A */
2N/Avoid
2N/Axdrmem_create(XDR *xdrs, const caddr_t addr, const uint_t size,
2N/A const enum xdr_op op)
2N/A{
2N/A caddr_t eaddr = addr;
2N/A
2N/A xdrs->x_op = op;
2N/A xdrs->x_ops = xdrmem_ops();
2N/A xdrs->x_private = xdrs->x_base = 0;
2N/A /*
2N/A * We check here that the size is with in the range of the
2N/A * address space. If not we set x_handy to zero. This will cause
2N/A * all xdrmem entry points to fail.
2N/A */
2N/A eaddr = addr + size;
2N/A
2N/A if (eaddr < addr)
2N/A xdrs->x_handy = 0;
2N/A else {
2N/A xdrs->x_handy = size;
2N/A xdrs->x_private = xdrs->x_base = addr;
2N/A }
2N/A}
2N/A
2N/A/* ARGSUSED */
2N/Astatic void
2N/Axdrmem_destroy(XDR *xdrs)
2N/A{
2N/A}
2N/A
2N/Astatic bool_t
2N/Axdrmem_getlong(XDR *xdrs, long *lp)
2N/A{
2N/A if (sizeof (int32_t) > (uint32_t)xdrs->x_handy) {
2N/A xdrs->x_private += (uint_t)xdrs->x_handy;
2N/A xdrs->x_handy = 0;
2N/A return (FALSE);
2N/A }
2N/A xdrs->x_handy -= sizeof (int32_t);
2N/A /* LINTED pointer cast */
2N/A *lp = (int32_t)ntohl((uint32_t)(*((int32_t *)(xdrs->x_private))));
2N/A xdrs->x_private += sizeof (int32_t);
2N/A return (TRUE);
2N/A}
2N/A
2N/Astatic bool_t
2N/Axdrmem_putlong(XDR *xdrs, long *lp)
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
2N/A if ((sizeof (int32_t) > (uint32_t)xdrs->x_handy)) {
2N/A xdrs->x_private += (uint_t)xdrs->x_handy;
2N/A xdrs->x_handy = 0;
2N/A return (FALSE);
2N/A }
2N/A xdrs->x_handy -= sizeof (int32_t);
2N/A /* LINTED pointer cast */
2N/A *(int32_t *)xdrs->x_private = (int32_t)htonl((uint32_t)(*lp));
2N/A xdrs->x_private += sizeof (int32_t);
2N/A return (TRUE);
2N/A}
2N/A
2N/A#if defined(_LP64)
2N/Astatic bool_t
2N/Axdrmem_getint32(XDR *xdrs, int32_t *ip)
2N/A{
2N/A if (sizeof (int32_t) > (uint_t)xdrs->x_handy) {
2N/A xdrs->x_private += (uint_t)xdrs->x_handy;
2N/A xdrs->x_handy = 0;
2N/A return (FALSE);
2N/A }
2N/A xdrs->x_handy -= sizeof (int32_t);
2N/A /* LINTED pointer cast */
2N/A *ip = (int32_t)ntohl((uint32_t)(*((int32_t *)(xdrs->x_private))));
2N/A xdrs->x_private += sizeof (int32_t);
2N/A return (TRUE);
2N/A}
2N/A
2N/Astatic bool_t
2N/Axdrmem_putint32(XDR *xdrs, int32_t *ip)
2N/A{
2N/A if (sizeof (int32_t) > (uint32_t)xdrs->x_handy) {
2N/A xdrs->x_private += (uint_t)xdrs->x_handy;
2N/A xdrs->x_handy = 0;
2N/A return (FALSE);
2N/A }
2N/A xdrs->x_handy -= sizeof (int32_t);
2N/A /* LINTED pointer cast */
2N/A *(int32_t *)xdrs->x_private = (int32_t)htonl((uint32_t)(*ip));
2N/A xdrs->x_private += sizeof (int32_t);
2N/A return (TRUE);
2N/A}
2N/A#endif /* _LP64 */
2N/A
2N/Astatic bool_t
2N/Axdrmem_getbytes(XDR *xdrs, caddr_t addr, int len)
2N/A{
2N/A if ((uint32_t)len > (uint32_t)xdrs->x_handy) {
2N/A xdrs->x_private += (uint_t)xdrs->x_handy;
2N/A xdrs->x_handy = 0;
2N/A return (FALSE);
2N/A }
2N/A xdrs->x_handy -= len;
2N/A (void) memcpy(addr, xdrs->x_private, (uint_t)len);
2N/A xdrs->x_private += (uint_t)len;
2N/A return (TRUE);
2N/A}
2N/A
2N/Astatic bool_t
2N/Axdrmem_putbytes(XDR *xdrs, caddr_t addr, int len)
2N/A{
2N/A if ((uint32_t)len > (uint32_t)xdrs->x_handy) {
2N/A xdrs->x_private += (uint_t)xdrs->x_handy;
2N/A xdrs->x_handy = 0;
2N/A return (FALSE);
2N/A }
2N/A xdrs->x_handy -= len;
2N/A (void) memcpy(xdrs->x_private, addr, (uint_t)len);
2N/A xdrs->x_private += (uint_t)len;
2N/A return (TRUE);
2N/A}
2N/A
2N/Astatic uint_t
2N/Axdrmem_getpos(XDR *xdrs)
2N/A{
2N/A return (uint_t)((uintptr_t)xdrs->x_private - (uintptr_t)xdrs->x_base);
2N/A}
2N/A
2N/Astatic bool_t
2N/Axdrmem_setpos(XDR *xdrs, uint_t pos)
2N/A{
2N/A caddr_t newaddr = xdrs->x_base + pos;
2N/A caddr_t lastaddr = xdrs->x_private + (uint_t)xdrs->x_handy;
2N/A
2N/A if (newaddr > lastaddr)
2N/A return (FALSE);
2N/A xdrs->x_private = newaddr;
2N/A xdrs->x_handy = (int)((uintptr_t)lastaddr - (uintptr_t)newaddr);
2N/A return (TRUE);
2N/A}
2N/A
2N/Astatic rpc_inline_t *
2N/Axdrmem_inline(XDR *xdrs, int len)
2N/A{
2N/A rpc_inline_t *buf = 0;
2N/A
2N/A if ((uint32_t)xdrs->x_handy >= (uint32_t)len) {
2N/A xdrs->x_handy -= len;
2N/A /* LINTED pointer cast */
2N/A buf = (rpc_inline_t *)xdrs->x_private;
2N/A xdrs->x_private += (uint_t)len;
2N/A }
2N/A return (buf);
2N/A}
2N/A
2N/Astatic bool_t
2N/Axdrmem_control(XDR *xdrs, int request, void *info)
2N/A{
2N/A xdr_bytesrec *xptr;
2N/A
2N/A switch (request) {
2N/A case XDR_GET_BYTES_AVAIL:
2N/A xptr = (xdr_bytesrec *) info;
2N/A xptr->xc_is_last_record = TRUE;
2N/A xptr->xc_num_avail = xdrs->x_handy;
2N/A return (TRUE);
2N/A default:
2N/A return (FALSE);
2N/A
2N/A }
2N/A
2N/A}
2N/A
2N/Astatic struct xdr_ops *
2N/Axdrmem_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 (void) mutex_lock(&ops_lock);
2N/A if (ops.x_getlong == NULL) {
2N/A ops.x_getlong = xdrmem_getlong;
2N/A ops.x_putlong = xdrmem_putlong;
2N/A ops.x_getbytes = xdrmem_getbytes;
2N/A ops.x_putbytes = xdrmem_putbytes;
2N/A ops.x_getpostn = xdrmem_getpos;
2N/A ops.x_setpostn = xdrmem_setpos;
2N/A ops.x_inline = xdrmem_inline;
2N/A ops.x_destroy = xdrmem_destroy;
2N/A ops.x_control = xdrmem_control;
2N/A#if defined(_LP64)
2N/A ops.x_getint32 = xdrmem_getint32;
2N/A ops.x_putint32 = xdrmem_putint32;
2N/A#endif
2N/A }
2N/A (void) mutex_unlock(&ops_lock);
2N/A return (&ops);
2N/A}