/****************************************************************************/
/*	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 */
/** @{ */

/**
*	Stores the top of the stack at a given location
*
*	If the \p size parameter is negative, then the buffer size is taken
*	as the absolute value of the parameter and the result will be stored
*	in little-endian order. Otherwise, the result is stored in big-endian
*	order. The stored item is removed from the stack. If the buffer given
*	is larger than the required size, then it is zero-padded so that the
*	entire buffer, interpreted in the given endianness, represents the
*	actual result. The function returns the number of significant bytes
*	of the result, that is, the padding zeroes are not counted.
*	Note, that if the number to store is 0 it counts as one significant
*	byte, thus the function never returns 0.
*	<br><br>
*	For example, if the number on top of the stack is 0x1234 then:
*	@code
*	BC_NANU nanu;
*	char buff[ 5 ];
*
*	Call								Buffer
*	BC_NanuPull( nanu, buff, +5 )		00 00 00 12 34
*	BC_NanuPull( nanu, buff, -5 )		34 12 00 00 00
*	@endcode
*
*	@param	nanu			The natural number machine state descriptor
*	@param	buff			Pointer to the buffer
*	@param	size			The size of the buffer
*
*	@retval	>0				The number of significant bytes in the result.
*	@retval	BC_NANU_SUND	The stack was empty
*	@retval	BC_NANU_SIZE	The size was less than needed to store the number
*
*	@par Stack image
*		a ...  ==> ...
*/

int		BC_NanuPull( BC_NANU nanu, void *buff, int size )
{
int				vref, step, retv;
unsigned		blen, temp, *wptr;
unsigned char	*data;

	data = (unsigned char *) buff;

	if ( ( vref = _bc_NanuStkGet( nanu ) ) < 0 )

		return( _bc_NanuError( nanu, vref ) );

	if ( ! size )

		return( _bc_NanuError( nanu, BC_NANU_SIZE ) );

	temp = _bc_NanuVarLen( nanu, vref );
	wptr = _bc_NanuVarPtr( nanu, vref );
	blen = temp * sizeof( unsigned );
	temp = temp - 1;

	if ( ! ( wptr[ temp ] & 0xff000000u ) ) blen--;
	if ( ! ( wptr[ temp ] & 0xffff0000u ) ) blen--;
	if ( ! ( wptr[ temp ] & 0xffffff00u ) ) blen--;

	if ( size < 0 ) {

		size = -size;
		step = 1;
	}
	else {

		data += size - 1;
		step = -1;
	}

	if ( size < (int) blen ) return( BC_NANU_SIZE );

	for ( retv = blen ; blen >= 4 ; blen -= 4, size -= 4 ) {

		temp  = *wptr++;
		*data = temp;			data += step;
		*data = temp >> 8;		data += step;
		*data = temp >> 16;		data += step;
		*data = temp >> 24;		data += step;
	}

	temp = blen ? *wptr : 0;

	while ( size-- ) {

		*data = temp;
		data += step;
		temp >>= 8;
	}

	_bc_NanuVarDec( nanu, vref );
	return( retv );
}

/** @} */

