/****************************************************************************/
/*	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(): Check for 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 or the sequence violates the
*		restrictions of RFC-3629. 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_rfc( unsigned *resp, unsigned char next, int stat )
{
	if ( stat ) {

		if ( ! ( next & 0x80 ) ) {

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

			// RFC-3629 says that the largest number can't be
			// over U+10FFFF, which can be represented by a 4
			// byte long sequence.
			// The RFC also says that the sequence must be minimum
			// length. Therefore:
			//				   wwwwwww:	-
			//			  www wwxxxxxx:	mb[0] > 1
			//		 wwwwxxxx xxyyyyyy:	mb[0] > 0 || mb[1] >= 0x20
			// wwwxx xxxxyyyy yyzzzzzz:	mb[0] > 0 || mb[1] >= 0x10

			if ( ( next & 0xe0 ) == 0xc0 ) {

				next &= 0x1f;
				*resp = next;

				if ( next )

					return( 1 );
				else
					return( -1 );
			}

			if ( ( next & 0xf0 ) == 0xe0 ) {

				next &= 0x0f;
				*resp = next;

				if ( next )

					return( 2 );
				else
					return( 0x82 );
			}

			if ( ( next & 0xf8 ) == 0xf0 ) {

				next &= 0x07;
				*resp = next;

				if ( next )

					return( 3 );
				else
					return( 0x43 );
			}

			return( -1 );
		}
	}
	else {

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

