// codecvt facet for multibyte code as serialized wide-character code
 #pragma once
#ifndef _CVT_ONE_ONE_
#define _CVT_ONE_ONE_
#ifndef RC_INVOKED
#include <stdexcept>
#include <locale>
#include <cwchar>

 #pragma pack(push,_CRT_PACKING)
 #pragma warning(push,3)

 #pragma warning(disable: 4127 6294 6326)

namespace stdext {
	namespace cvt {


 #ifndef _LITTLE_FIRST
  #define _LITTLE_FIRST		1
  #define _BIG_FIRST		2
  #define _BYTES_PER_WORD	4

enum codecvt_mode {
	consume_header = 4,
	generate_header = 2,
	little_endian = 1};
 #endif /* _LITTLE_FIRST */

typedef _CSTD mbstate_t _Statype;

		// TEMPLATE CLASS codecvt_one_one
template<class _Elem,
	unsigned long _Maxcode = 0xffffffff,
	codecvt_mode _Mode = (codecvt_mode)0,
	size_t _Bytes_per_word = sizeof (_Elem)>
	class codecvt_one_one
	: public _STD codecvt<_Elem, char, _Statype>
	{	// facet for converting between _Elem and serialized byte sequences
public:
 	typedef _STD codecvt<_Elem, char, _Statype> _Mybase;
	typedef typename _Mybase::result result;
	typedef char _Byte;
	typedef _Elem intern_type;
	typedef _Byte extern_type;
	typedef _Statype state_type;

	explicit codecvt_one_one(size_t _Refs = 0)
		: _Mybase(_Refs)
		{	// construct with ref count
		if (_Bytes_per_word < 1 || 4 < _Bytes_per_word)
			_THROW(_STD range_error, "bad byte count parameter");
		}

	virtual ~codecvt_one_one()
		{	// destroy the object
		}

protected:
	virtual result do_in(_Statype& _State,
		const _Byte *_First1, const _Byte *_Last1, const _Byte *& _Mid1,
		_Elem *_First2, _Elem *_Last2, _Elem *& _Mid2) const
		{	// convert bytes [_First1, _Last1) to [_First2, _Last)
		char *_Pstate = (char *)&_State;
		size_t _Count;
		_Mid1 = _First1;
		_Mid2 = _First2;

		for (; _Bytes_per_word <= _Last1 - _Mid1 && _Mid2 != _Last2; )
			{	// convert a multibyte sequence
			unsigned char *_Ptr = (unsigned char *)_Mid1;
			unsigned long _Ch = 0;

			if (*_Pstate == _LITTLE_FIRST)
				for (_Count = _Bytes_per_word; 0 < _Count; )
					_Ch = _Ch << 8 | _Ptr[--_Count];
			else if (*_Pstate == _BIG_FIRST)
				for (_Count = 0; _Count < _Bytes_per_word; ++_Count)
					_Ch = _Ch << 8 | _Ptr[_Count];
			else
				{	// no header seen yet, try preferred mode
				unsigned char _Default_endian = (_Mode & little_endian) != 0
					? _LITTLE_FIRST : _BIG_FIRST;

				 if ((_Mode & little_endian) != 0)
					for (_Count = _Bytes_per_word; 0 < _Count; )
						_Ch = _Ch << 8 | _Ptr[--_Count];
				else
					for (_Count = 0; _Count < _Bytes_per_word; ++_Count)
						_Ch = _Ch << 8 | _Ptr[_Count];
				if ((_Mode & consume_header) == 0
					|| _Ch != 0xfeff
						&& _Ch != (0xfffe0000 >> 8 * (4 - _Bytes_per_word)))
					*_Pstate = _Default_endian;
				else
					{	// consume header, fixate on endianness, and retry
					_Mid1 += _Bytes_per_word;
					*_Pstate = (char)(_Ch = 0xfeff
						? _Default_endian
						: (unsigned char)(3 - _Default_endian));
					result _Ans = do_in(_State, _Mid1, _Last1, _Mid1,
						_First2, _Last2, _Mid2);

					if (_Ans == _Mybase::partial)
						{	// not enough bytes, roll back header
						*_Pstate = 0;
						_Mid1 = _First1;
						}
					return (_Ans);
					}
				}

			_Mid1 += _Bytes_per_word;
			if (_Maxcode < _Ch)
				return (_Mybase::error);	// code too large
			*_Mid2++ = (_Elem)_Ch;
			}

		return (_First1 == _Mid1 ? _Mybase::partial : _Mybase::ok);
		}

	virtual result do_out(_Statype& _State,
		const _Elem *_First1, const _Elem *_Last1, const _Elem *& _Mid1,
		_Byte *_First2, _Byte *_Last2, _Byte *& _Mid2) const
		{	// convert [_First1, _Last1) to bytes [_First2, _Last)
		char *_Pstate = (char *)&_State;
		size_t _Count;
		_Mid1 = _First1;
		_Mid2 = _First2;

		if (*_Pstate == 0)
			{	// determine endianness once, maybe generate header
			unsigned long _Header = 0xfeff;

			*_Pstate = (_Mode & little_endian) != 0
				? _LITTLE_FIRST : _BIG_FIRST;
			if ((_Mode & generate_header) == 0)
				;
			else if (_Last2 - _Mid2 < 2 * _Bytes_per_word)
				return (_Mybase::partial);	// not enough room for both
			else if (*_Pstate == _LITTLE_FIRST)
				for (_Count = 0; _Count < _Bytes_per_word; ++_Count)
					{	// put LS byte first
					*_Mid2++ = (unsigned char)_Header;
					_Header >>= 8;
					}
			else
				for (_Header <<= 8 * (4 - _Bytes_per_word), _Count = 0;
					_Count < _Bytes_per_word; ++_Count)
					{	// put MS byte first
					*_Mid2++ = (unsigned char)(_Header >> 24);
					_Header <<= 8;
					}
			}

		for (; _Mid1 != _Last1 && _Bytes_per_word <= _Last2 - _Mid2; )
			{	// convert and put a wide char
			unsigned long _Ch = (unsigned long)*_Mid1++;

			if (_Maxcode < _Ch)
				return (_Mybase::error);

			if (*_Pstate == _LITTLE_FIRST)
				for (_Count = 0; _Count < _Bytes_per_word; ++_Count)
					{	// put LS byte first
					*_Mid2++ = (unsigned char)_Ch;
					_Ch >>= 8;
					}
			else
				for (_Ch <<= 8 * (4 - _Bytes_per_word), _Count = 0;
					_Count < _Bytes_per_word; ++_Count)
					{	// put MS byte first
					*_Mid2++ = (unsigned char)(_Ch >> 24);
					_Ch <<= 8;
					}
			}

		return (_First1 == _Mid1 ? _Mybase::partial : _Mybase::ok);
		}

	virtual result do_unshift(_Statype&,
		_Byte *_First2, _Byte *, _Byte *& _Mid2) const
		{	// generate bytes to return to default shift state
		_Mid2 = _First2;
		return (_Mybase::ok);
		}

	virtual int do_length(const _Statype& _State, const _Byte *_First1,
		const _Byte *_Last1, size_t _Count) const _THROW0()
		{	// return min(_Count, converted length of bytes [_First1, _Last1))
		size_t _Wchars = 0;
		_Statype _Mystate = _State;

		for (; _Wchars < _Count && _First1 != _Last1; )
			{	// convert another wide character
			const _Byte *_Mid1;
			_Elem *_Mid2;
			_Elem _Ch;

			switch (do_in(_Mystate, _First1, _Last1, _Mid1,
				&_Ch, &_Ch + 1, _Mid2))
				{	// test result of single wide-char conversion
			case _Mybase::noconv:
				return ((int)(_Wchars + (_Last1 - _First1)));

			case  _Mybase::ok:
				if (_Mid2 == &_Ch + 1)
					++_Wchars;	// replacement do_in might not convert one
				_First1 = _Mid1;
				break;

			default:
				return ((int)_Wchars);	// error or partial
				}
			}

		return ((int)_Wchars);
		}

	virtual bool do_always_noconv() const _THROW0()
		{	// return true if conversions never change input
		return (false);
		}

	virtual int do_max_length() const _THROW0()
		{	// return maximum length required for a conversion
		return ((_Mode & (consume_header | generate_header)) != 0
			? 2 * _Bytes_per_word : _Bytes_per_word);
		}

	virtual int do_encoding() const _THROW0()
		{	// return length of code sequence (from codecvt)
		return ((_Mode & (consume_header | generate_header)) != 0
			? -1 : (int)_Bytes_per_word);	// -1 => state dependent
		}
	};
		}	// namespace cvt
	}	// namespace stdext

 #ifdef _TEST_IT
  #define NCHARS	0x10000
  #define MYWC_MAX	0xffff
  #define MYFILE	"one_one"
  #define MYNAME	stdext::cvt::codecvt_one_one<Mywchar>

  #include <cvt/xtest>
 #endif /* _TEST_IT */
 #pragma warning(pop)
 #pragma pack(pop)

#endif /* RC_INVOKED */
#endif /* _CVT_ONE_ONE_ */

/*
 * Copyright (c) 1992-2009 by P.J. Plauger.  ALL RIGHTS RESERVED. 
 * Consult your license regarding permissions and restrictions.
V5.20:0009 */
