/****************************************************************************/
/*	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 <wchar.h>
#include <blc_wchar.h>

/*
*	Convert a multibyte sequence into a 16-bit wchar, with RFC conformance check
*	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*	For the description see the mb2wc docs in the wchar.h header.
*/

int		blc_mbtowc_rfc_16( void *wc, const char *mb, size_t ml )
{
unsigned int	st;
unsigned int	tr;
unsigned int	ct;

	if ( ! mb ) return( 0 );
	if ( ! ml ) return( -1 );
	if ( ! *mb ) return( 0 );

	for ( ct = 1 ; ( st = _blc_mbtowc_rfc( &tr, *mb++, 0 ) ) ; ct++ ) {

		if ( ct == ml ) return( -1 );
		if ( tr > 0x3ff ) return( -1 );
	}

	if ( ( tr & 0xf800 ) == 0xd800 ) return( -1 );
	if ( wc ) *((short *) wc) = tr;
	return( ct );
}


