/****************************************************************************/
/*	Copyright (c) 2009, Bendor Research Pty. Ltd. All rights reserved.		*/
/*	This software is released under the GNU General Public License, version	*/
/*	3, with the exemptions that 1) the header files installed by this 	 	*/
/*	package can be included in closed-source code and 2) the libraries		*/
/*	built by this package can be statically linked into a closed-source 	*/
/*	binary. See the file LICENSE for the details.							*/
/*	This software is provided "AS IS", without any warranty or guarantee.	*/
/*	By using this software you acknowledge that neither Bendor Research nor	*/
/*	any of its personnel or affiliates can be held liable for any damages,	*/
/*	including, but not limited to, loss of business, loss of money, loss of */
/*	life, loss or reputation, arising out of this software being faulty or	*/
/*	not performing as advertised, even if Bendor Research was aware of such	*/
/*	faults in the software. You use this software at your own risk, any and */
/*	all liabilities that arise out of your using this software are strictly	*/
/*	yours.																	*/
/****************************************************************************/

#include <nanu-nanu.h>

/** @addtogroup nanu */
/** @{ */

/**
*	Pushes a new item onto the stack
*
*	Creates a new number on the top of the stack. The value of the
*	new number will be that of the number represented by the series
*	of bytes at the location identified by the \p data argument.
*	The number of bytes that represent the number is given in the
*	\p size argument. If \p size is a positive number, then the bytes
*	will be interpreted in a big-endian order, if it is negative,
*	then the length of the number is taken as the absolute value of
*	\p size and the bytes will be processes in little-endian order.<br>
*	The size of the number can not be 0.
*
*	@param	nanu			The natural number machine state descriptor
*	@param	buff			Pointer to the byte stream of the data
*	@param	size			The number of bytes in the data
*
*	@retval	BC_NANU_NANU	The operation succeeded
*	@retval	BC_NANU_SOVR	The stack was full
*	@retval	BC_NANU_SIZE	The size parameter is invalid
*	@retval	BC_NANU_OMEM	There's not enough memory in the pool to store
*							the number.
*
*	@par Stack image
*		...  ==> new_number ...
*/

int		BC_NanuPush( BC_NANU nanu, const void *buff, int size )
{
unsigned int	word, *wptr;
unsigned char	*dptr, *data;
int				vref;

	data = (unsigned char *) buff;

	if ( ! size )

		return( _bc_NanuError( nanu, BC_NANU_SIZE ) );

	if ( size < 0 ) {

		// Little-endian byte order

		size = -size;
		for ( dptr = data + size - 1 ; size > 1 && ! *dptr-- ; size-- );
		word = ( size + 3 ) / 4;

		if ( ( vref = _bc_NanuVarNew( nanu, word ) ) < 0 )

			return( _bc_NanuError( nanu, vref ) );

		wptr = _bc_NanuVarPtr( nanu, vref );

		while ( size >= 4 ) {

			word  = *data++;
			word |= *data++ <<  8;
			word |= *data++ << 16;
			word |= *data++ << 24;
			*wptr++ = word;
			size -= 4;
		}

		if ( size > 0 ) *wptr  = *data++;
		if ( size > 1 ) *wptr |= *data++ << 8;
		if ( size > 2 ) *wptr |= *data++ << 16;
	}
	else {

		while ( size > 1 && ! *data ) {

			data++;
			size--;
		}

		word = ( size + 3 ) / 4;

		if ( ( vref = _bc_NanuVarNew( nanu, word ) ) < 0 )

			return( _bc_NanuError( nanu, vref ) );

		data += size;
		wptr  = _bc_NanuVarPtr( nanu, vref );

		while ( size >= 4 ) {

			word  = *--data;
			word |= *--data <<  8;
			word |= *--data << 16;
			word |= *--data << 24;
			*wptr++ = word;
			size -= 4;
		}

		if ( size > 0 ) *wptr  = *--data;
		if ( size > 1 ) *wptr |= *--data <<  8;
		if ( size > 2 ) *wptr |= *--data << 16;
	}

	return( _bc_NanuError( nanu, _bc_NanuStkPut( nanu, vref ) ) );
}

/** @} */

