/****************************************************************************/
/*	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 of 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 <blc_wchar.h>

/*
*	Processing state for mbtowc(): no RFC conformance
*	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*	'resp' is a pointer where the resulting wide char will be stored.
*	'next' is the next character from the input
*	'stat' is the state variable.
*
*	First you call the function with the first character in the multibyte
*	sequence and the stat parameter being 0. You do not have to initialise
*	the location pointed by resp. From then on the you do the following:
*
*	-	if the return value is negative, then the multibyte sequence
*		contains an illegal character. The conversion is aborted.
*
*	-	if the return value is zero, then the conversion finished and
*		the wide character is stored in *resp. Note that it is a 32-bit
*		value, if you use 16-bit wchar_t, you must check for overflow.
*
*	-	If the return value is positive, then the conversion is still
*		ongoing. You have to fetch the next character in the multibyte
*		sequence and call the function again, passing it the same resp,
*		the new character and the return value as its 'stat' parameter.
*		Note that *resp is updated in each step. If you want to abort
*		early for 16-bit wchar_t overflow, then check if *resp > 0x3ff.
*		If it is, then the next call would cause the wide char to grow
*		over the 16-bit range, so you can flag error.
*/

int		_blc_mbtowc_any( unsigned *resp, unsigned char next, int stat )
{
unsigned mask;

	if ( stat ) {

		if ( ! ( next & 0x80 ) ) {

			*resp = next;
			return( 0 );
		}
		else {

			for ( mask = 0xfe, stat = 5 ; stat ; stat--, mask &= 2 * mask ) {

				if ( ( next & mask ) == ( mask & 2 * mask ) ) {

					*resp = next & ~mask;
					return( 6 - stat );
				}
			}

			return( -1 );
		}
	}
	else {

		if ( ( next & 0xc0 ) != 0x80 ) return( -1 );
		*resp = ( *resp << 6 ) | ( next & 0x3f );
		return( stat - 1 );
	}
}

