/*= -*- c-file-style: "linux"; indent-tabs-mode: t; -*-
 *
 * n4: A fast userspace NFSv4 server and client library.
 * $Id: inbuf.c,v 1.8 2001/07/20 07:40:52 mbp Exp $
 * 
 * Copyright (C) 2001 by Martin Pool <mbp@samba.org>
 * http://n4.samba.org/
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#include <includes.h>


/**
 * \file inbuf.c
 *
 * Input buffers, like output buffers, are normally denominated in
 * units of words, since they're always packed and aligned on word
 * boundaries.  Functions which deal with other units, like
 * UNPACK_BYTES, measure in those units instead.
 **/

/**
 * Assert there is enough input for us to continue decoding.
 *
 * \param words_reqd Number of 32-bit words required to proceed.
 * 
 * \return N4OK if there are at least \p words_reqd integers remaining
 * in the input buffer; otherwise N4ERR_RPC_SHORT.
 **/
n4err n4inbuf_need_impl(char const *domain, 
                                char const *file, int line,
                                n4inbuf *b, int words_reqd)
{
    size_t needed_bytes = (sizeof *b->p) * (b->p - b->start + words_reqd);
    
    if (needed_bytes > b->packet_bytes) {
        n4log0(N4LOG_CRIT|N4LOG_NOFN, domain, NULL, 
               "%s(%d): packet in inbuf is too short; "
               "require %ld, have %ld bytes",
               file, line, 
               (long) needed_bytes, (long) b->packet_bytes);
        return N4ERR_RPC_SHORT;
    }

    return N4OK;
}



n4err n4buf_check_inbuf(n4inbuf const *inb)
{
    size_t len = n4inbuf_tell_bytes(inb);
    if (len > inb->packet_bytes) {
        n4fatal("overrun on inbuf @%p; actual length %ld, read up to %ld",
                 inb, (long) inb->packet_bytes, (long) len);
    }

    return N4OK;
}



/**
 * Check that an input buffer is empty.  This can be used when we
 * think we've consumed a complete packet.  If it finds the packet is
 * not empty, then either we parsed it incorrectly, or there was
 * trailing garbage.
 **/
n4err n4inbuf_finished_p(const n4inbuf *inb)
{
    size_t used_bytes = n4inbuf_tell_bytes(inb);
    if (used_bytes == inb->packet_bytes) {
        n4trace("finished dealing with input");
        return N4OK;
    } else {
        n4log_error("trailing garbage in RPC input?  "
                    "consumed %ld of %ld bytes",
                    (long) used_bytes,
                    (long) inb->packet_bytes);
        return N4ERR_RPC_GARBAGE;
    }
}


void n4inbuf_show(const struct n4inbuf *inb,
                  const char *where)
{
    n4trace("inbuf cursor @%#x %s", n4inbuf_tell_bytes(inb), where);
}


int n4inbuf_tell_bytes(const struct n4inbuf *inb)
{
    return (sizeof *inb->p) * (inb->p - inb->start);
}


void n4inbuf_skip(struct n4inbuf *inb, int n_words)
{
    n4_assert(n_words > 0);
    inb->p += n_words;
}


void n4unpack_nocheck (struct n4inbuf *inb,
		       uint32_t *d)
{
	*d = *(inb->p)++;
}


uint32_t UNPACK (struct n4inbuf *inb)
{
	return *(inb->p)++;
}


guint64 UNPACK_HYPER(n4inbuf *b)
{
    return *((guint64 *) b->p)++;
}


/**
 * Read a single word from the input buffer, and convert to host byte
 * order.  Checks that sufficient data remains in the input buffer.
 **/
n4err n4unpack_checked(struct n4inbuf *inb, uint32_t *out)
{
	CHECK_ERR(n4inbuf_need(inb, 1));
	*out = FROM_NET(UNPACK(inb));
	return N4OK;
}


/**
 * Retrieve a boolean value from the network and store it in \p bool.
 *
 * XDR requires that bools be packed as (network-byte-order) 0 or 1,
 * so if it's anything else there's an error and we probably have a
 * broken peer or have got desynchronized.
 **/
n4err n4unpack_bool_checked (struct n4inbuf *inb,
                             bool_t *bool)
{
	CHECK_ERR (n4inbuf_need (inb, 1));
	return n4unpack_bool_nocheck (inb, bool);
}


n4err n4unpack_bool_nocheck (struct n4inbuf *inb,
			     bool_t *bool)
{
	switch ((*bool = FROM_NET (UNPACK (inb)))) {
	case 0:
	case 1:
		return N4OK;
	default:
		return N4ERR_RPC_GARBAGE;
	}
}
	

	
n4err n4unpack64_checked(n4inbuf *inb, uint64_t *out)
{
	CHECK_ERR(n4inbuf_need(inb, 2));
	*out = FROM_NET_HYPER(UNPACK_HYPER(inb));
	return N4OK;
}


void n4unpack_signed64_nocheck (struct n4inbuf *inb,
				int64_t *d)
{
	*d = GINT64_FROM_BE (UNPACK_HYPER (inb));
}


/**
 * Read a 64-bit unsigned integer from the network and convert to host
 * byte order.
 *
 * \note This does not check that there's is enough data remaining in
 * the input buffer, so you should only call this after calling
 * n4inbuf_need().
 **/
n4err n4unpack64_nocheck (struct n4inbuf *inb,
                          uint64_t *out)
{
	*out = FROM_NET_HYPER (UNPACK_HYPER (inb));
	return N4OK;
}


const void * UNPACK_BYTES(n4inbuf *b, size_t n_bytes)
{
    gconstpointer p;
    
    p = (gconstpointer) b->p;
    b->p += PADDED(n_bytes);
    
    return p;
}

