/*
         * //ENCODINGFALLBACK
         * internal unsafe int CallUnicodeToBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, int byteCount, out bool usedDefaultChar) {
         *  //
         *  // For MLang, there is no way to detect if default characters are used during the conversion.
         *  // So always set used default char to true.
         *  //
         *  usedDefaultChar = true;
         *  return (nativeUnicodeToBytes(m_pIMLangConvertCharsetFromUnicode, chars, charIndex, charCount, bytes, byteIndex, byteCount));
         * }
         *
         * internal unsafe int CallBytesToUnicode(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, int charCount) {
         *  return (nativeBytesToUnicode(m_pIMLangConvertCharsetToUnicode, bytes, byteIndex, out byteCount, chars, charIndex, charCount));
         * }
         */

        public override int GetCharCount(byte[] bytes, int index, int count)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes",
                                                Environment.GetResourceString("ArgumentNull_Array"));
            }
            if (index < 0 || count < 0)
            {
                throw new ArgumentOutOfRangeException((index < 0 ? "index" : "count"),
                                                      Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (bytes.Length - index < count)
            {
                throw new ArgumentOutOfRangeException("bytes",
                                                      Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
            }

            int charCount = 0;
            int result;
            int dwMode = 0;

            if (count < 3 && IsISCIICodePage(CodePage))
            {
                // Because of the way that MLang handles DLL-based code page, we will always fail (and get AgrumentException as a result)
                // when the byteCount is less than 3.
                // Therfore, let's call Win32 to convert these bytes directly.
                result = CodePageEncoding.BytesToUnicodeNative(CodePage, bytes, index, count, null, 0, 0);
            }
            else
            {
                result = nativeBytesToUnicode(CodePage, bytes, index, out count, null, 0, charCount, ref dwMode);
            }

            return(result);
        }
        public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
        {
            if (bytes == null || chars == null)
            {
                throw new ArgumentNullException((bytes == null ? "bytes" : "chars"),
                                                Environment.GetResourceString("ArgumentNull_Array"));
            }
            if (byteIndex < 0 || byteCount < 0)
            {
                throw new ArgumentOutOfRangeException((byteIndex < 0 ? "byteIndex" : "byteCount"),
                                                      Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (bytes.Length - byteIndex < byteCount)
            {
                throw new ArgumentOutOfRangeException("bytes",
                                                      Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
            }
            if (charIndex < 0 || charIndex > chars.Length)
            {
                throw new ArgumentOutOfRangeException("charIndex",
                                                      Environment.GetResourceString("ArgumentOutOfRange_Index"));
            }
            if (byteCount == 0)
            {
                return(0);
            }
            // There are cases that a call to nativeBytesToUnicode() may generate an empty string.  For example, if
            // bytes only contain a lead byte.
            // Therefore, we should allow charIndex to be equal to chars.Length.

            int charCount = chars.Length - charIndex;

            int result;
            int dwMode = 0;

            if (byteCount < 3 && IsISCIICodePage(CodePage))
            {
                if (charIndex == chars.Length)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_ConversionOverflow"));
                }
                // Because of the way that MLang handles DLL-based code page, we will always fail (and get AgrumentException as a result)
                // when the byteCount is less than 3.
                // Therfore, let's call Win32 to convert these bytes directly.
                result = CodePageEncoding.BytesToUnicodeNative(CodePage, bytes, byteIndex, byteCount,
                                                               chars, charIndex, chars.Length - charIndex);
                if (result == 0)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_ConversionOverflow"));
                }
            }
            else
            {
                result = nativeBytesToUnicode(
                    CodePage,
                    bytes, byteIndex, out byteCount,
                    chars, charIndex, charCount, ref dwMode);
            }

            return(result);
        }