internal override unsafe int GetChars(byte *bytes, int byteCount, char *chars, int charCount, DecoderNLS baseDecoder)
        {
            ISO2022Decoder decoder = (ISO2022Decoder)baseDecoder;
            int            num     = 0;

            switch (this.CodePage)
            {
            case 0xc42c:
            case 0xc42d:
            case 0xc42e:
                return(this.GetCharsCP5022xJP(bytes, byteCount, chars, charCount, decoder));

            case 0xc42f:
            case 0xc430:
                return(num);

            case 0xc431:
                return(this.GetCharsCP50225KR(bytes, byteCount, chars, charCount, decoder));

            case 0xcec8:
                return(this.GetCharsCP52936(bytes, byteCount, chars, charCount, decoder));
            }
            return(num);
        }
Ejemplo n.º 2
0
        [System.Security.SecurityCritical]  // auto-generated
        private unsafe int GetCharsCP5022xJP(byte* bytes, int byteCount,
                                                  char* chars, int charCount, ISO2022Decoder decoder)
        {
            // Get our info.
            Encoding.EncodingCharBuffer buffer = new Encoding.EncodingCharBuffer(
                this, decoder, chars, charCount, bytes, byteCount);

            // No mode information yet
            ISO2022Modes currentMode = ISO2022Modes.ModeASCII;      // Our current Mode
            ISO2022Modes shiftInMode = ISO2022Modes.ModeASCII;      // Mode that we'll shift in to
            byte[] escapeBytes = new byte[4];
            int escapeCount = 0;

            if (decoder != null)
            {
                currentMode = decoder.currentMode;
                shiftInMode = decoder.shiftInOutMode;

                // See if we have leftover decoder buffer to use
                // Load our bytesLeftOver
                escapeCount = decoder.bytesLeftOverCount;

                // Don't want to mess up decoder if we're counting or throw an exception
                for (int i = 0; i < escapeCount; i++)
                    escapeBytes[i] = decoder.bytesLeftOver[i];
            }

            // Do this until the end
            while (buffer.MoreData || escapeCount > 0)
            {
                byte ch;

                if (escapeCount > 0)
                {
                    // Get more escape sequences if necessary
                    if (escapeBytes[0] == ESCAPE)
                    {
                        // Stop if no more input
                        if (!buffer.MoreData)
                        {
                            if (decoder != null && !decoder.MustFlush)
                                break;
                        }
                        else
                        {
                            // Add it to the sequence we can check
                            escapeBytes[escapeCount++] = buffer.GetNextByte();

                            // We have an escape sequence
                            ISO2022Modes modeReturn =
                                CheckEscapeSequenceJP(escapeBytes, escapeCount);

                            if (modeReturn != ISO2022Modes.ModeInvalidEscape)
                            {
                                if (modeReturn != ISO2022Modes.ModeIncompleteEscape)
                                {
                                    // Processed escape correctly
                                    escapeCount = 0;

                                    // We're now this mode
                                    currentMode = shiftInMode = modeReturn;
                                }

                                // Either way, continue to get next escape or real byte
                                continue;
                            }
                        }

                        // If ModeInvalidEscape, or no input & must flush, then fall through to add escape.
                    }

                    // Read next escape byte and move them down one.
                    ch = DecrementEscapeBytes(ref escapeBytes, ref escapeCount);
                }
                else
                {
                    // Get our next byte
                    ch = buffer.GetNextByte();

                    if (ch == ESCAPE)
                    {
                        // We'll have an escape sequence, use it if we don't have one buffered already
                        if (escapeCount == 0)
                        {
                            // Start this new escape sequence
                            escapeBytes[0] = ch;
                            escapeCount = 1;
                            continue;
                        }

                        // Flush the previous escape sequence, then reuse this escape byte
                        buffer.AdjustBytes(-1);
                    }
                }

                if (ch == SHIFT_OUT)
                {
                   shiftInMode = currentMode;
                   currentMode = ISO2022Modes.ModeHalfwidthKatakana;
                   continue;
                }
                else if (ch == SHIFT_IN)
                {
                   currentMode = shiftInMode;
                   continue;
                }

                // Get our full character
                ushort iBytes = ch;
                bool b2Bytes = false;

                if (currentMode == ISO2022Modes.ModeJIS0208)
                {
                    //
                    //  To handle errors, we need to check:
                    //    1. if trailbyte is there
                    //    2. if code is valid
                    //
                    if (escapeCount > 0)
                    {
                        // Let another escape fall through
                        if (escapeBytes[0] != ESCAPE)
                        {
                            // Move them down one & get the next data
                            iBytes <<= 8;
                            iBytes |= DecrementEscapeBytes(ref escapeBytes, ref escapeCount);
                            b2Bytes = true;
                        }
                    }
                    else if (buffer.MoreData)
                    {
                        iBytes <<= 8;
                        iBytes |= buffer.GetNextByte();
                        b2Bytes = true;
                    }
                    else
                    {
                        // Not enough input, use decoder if possible
                        if (decoder == null || decoder.MustFlush)
                        {
                            // No decoder, do fallback for this byte
                            buffer.Fallback(ch);
                            break;
                        }

                        // Stick it in the decoder if we're not counting
                        if (chars != null)
                        {
                            escapeBytes[0] = ch;
                            escapeCount = 1;
                        }
                        break;
                    }

                    // MLang treated JIS 0208 '*' lead byte like a single halfwidth katakana
                    // escape, so use 0x8e00 as katakana lead byte and keep same trail byte.
                    // 0x2a lead byte range is normally unused in JIS 0208, so shouldn't have
                    // any wierd compatibility issues.
                    if ((b2Bytes == true) && ((iBytes & 0xff00) == 0x2a00))
                    {
                        iBytes = (ushort)(iBytes & 0xff);
                        iBytes |= (LEADBYTE_HALFWIDTH << 8);   // Put us in the halfwidth katakana range
                    }
                }
                else if (iBytes >= 0xA1 && iBytes <= 0xDF)
                {
                    // Everett accidentally mapped Katakana like shift-jis (932),
                    // even though this is a 7 bit code page.  We keep that mapping
                    iBytes |= (LEADBYTE_HALFWIDTH << 8);    // Map to halfwidth katakana range
                    iBytes &= 0xff7f;                       // remove extra 0x80
                }
                else if (currentMode == ISO2022Modes.ModeHalfwidthKatakana )
                {
                    // Add 0x10 lead byte that our encoding expects for Katakana:
                    iBytes |= (LEADBYTE_HALFWIDTH << 8);
                }

                // We have an iBytes to try to convert.
                char c = mapBytesToUnicode[iBytes];

                // See if it was unknown
                if (c == UNKNOWN_CHAR_FLAG && iBytes != 0)
                {
                    // Have to do fallback
                    if (b2Bytes)
                    {
                        if (!buffer.Fallback((byte)(iBytes >> 8), (byte)iBytes))
                            break;
                    }
                    else
                    {
                        if (!buffer.Fallback(ch))
                            break;
                    }
                }
                else
                {
                    // If we were JIS 0208, then we consumed an extra byte
                    if (!buffer.AddChar(c, b2Bytes ? 2:1))
                        break;
                }
            }

            // Make sure our decoder state matches our mode, if not counting
            if (chars != null && decoder != null)
            {
                // Remember it if we don't flush
                if (!decoder.MustFlush || escapeCount != 0)
                {
                    // Either not flushing or had state (from convert)
                    Contract.Assert(!decoder.MustFlush || !decoder.m_throwOnOverflow,
                        "[ISO2022Encoding.GetCharsCP5022xJP]Expected no state or not converting or not flushing");
                                        
                    decoder.currentMode = currentMode;
                    decoder.shiftInOutMode = shiftInMode;

                    // Remember escape buffer
                    decoder.bytesLeftOverCount = escapeCount;
                    decoder.bytesLeftOver = escapeBytes;
                }
                else
                {
                    // We flush, clear buffer
                    decoder.currentMode = ISO2022Modes.ModeASCII;
                    decoder.shiftInOutMode = ISO2022Modes.ModeASCII;
                    decoder.bytesLeftOverCount = 0;
                    // Slightly different if counting/not counting
                }

                decoder.m_bytesUsed = buffer.BytesUsed;
            }

            // Return # of characters we found
            return buffer.Count;
        }
Ejemplo n.º 3
0
        [System.Security.SecurityCritical]  // auto-generated
        private unsafe int GetCharsCP52936(byte* bytes, int byteCount,
                                                char* chars, int charCount, ISO2022Decoder decoder)
        {
            Contract.Assert(byteCount >=0, "[ISO2022Encoding.GetCharsCP52936]count >=0");
            Contract.Assert(bytes!=null, "[ISO2022Encoding.GetCharsCP52936]bytes!=null");

            // Get our info.
            Encoding.EncodingCharBuffer buffer = new Encoding.EncodingCharBuffer(
                this, decoder, chars, charCount, bytes, byteCount);

            // No mode information yet
            ISO2022Modes currentMode = ISO2022Modes.ModeASCII;
            int byteLeftOver = -1;
            bool bUsedDecoder = false;

            if (decoder != null)
            {
                currentMode = decoder.currentMode;
                // See if we have leftover decoder buffer to use
                // Don't want to mess up decoder if we're counting or throw an exception
                if (decoder.bytesLeftOverCount != 0 )
                {
                    // Load our bytesLeftOver
                    byteLeftOver = decoder.bytesLeftOver[0];
                }
            }

            // Do this until the end, just do '?' replacement because we don't have fallbacks for decodings.
            while (buffer.MoreData || byteLeftOver >= 0)
            {
                byte ch;

                // May have a left over byte
                if (byteLeftOver >= 0)
                {
                    ch = (byte)byteLeftOver;
                    byteLeftOver = -1;
                }
                else
                {
                    ch = buffer.GetNextByte();
                }

                // We're in escape mode
                if (ch == '~')
                {
                    // Next char is type of switch
                    if (!buffer.MoreData)
                    {
                        // We don't have anything left, it'll be in decoder or a ?
                        // don't fail if we are allowing overflows
                        if (decoder == null || decoder.MustFlush)
                        {
                            // We'll be a '?'
                            buffer.Fallback(ch);
                            // break if we fail & break if we don't (because !MoreData)
                            // Add succeeded, continue
                            break;
                        }

                        // Stick it in decoder
                        if (decoder != null)
                            decoder.ClearMustFlush();

                        if (chars != null)
                        {
                            decoder.bytesLeftOverCount = 1;
                            decoder.bytesLeftOver[0] = (byte)'~';
                            bUsedDecoder = true;
                        }
                        break;
                    }

                    // What type is it?, get 2nd byte
                    ch = buffer.GetNextByte();

                    if (ch == '~' && currentMode == ISO2022Modes.ModeASCII)
                    {
                        // Its just a ~~ replacement for ~, add it
                        if (!buffer.AddChar((char)ch, 2))
                            // Add failed, break for converting
                            break;

                        // Add succeeded, continue
                        continue;
                    }
                    else if (ch == '{')
                    {
                        // Switching to Double Byte mode
                        currentMode = ISO2022Modes.ModeHZ;
                        continue;
                    }
                    else if (ch == '}')
                    {
                        // Switching to ASCII mode
                        currentMode = ISO2022Modes.ModeASCII;
                        continue;
                    }
                    else if (ch == '\n')
                    {
                        // Ignore ~\n sequence
                        continue;
                    }
                    else
                    {
                        // Unknown escape, back up and try the '~' as a "normal" byte or lead byte
                        buffer.AdjustBytes(-1);
                        ch = (byte)'~';
                    }
                }

                // go ahead and add our data
                if (currentMode != ISO2022Modes.ModeASCII)
                {
                    // Should be ModeHZ
                    Contract.Assert(currentMode == ISO2022Modes.ModeHZ, "[ISO2022Encoding.GetCharsCP52936]Expected ModeHZ");
                    char cm;

                    // Everett allowed characters < 0x20 to be passed as if they were ASCII
                    if (ch < 0x20)
                    {
                        // Emit it as ASCII
                        goto STOREASCII;
                    }

                    // Its multibyte, should have another byte
                    if (!buffer.MoreData)
                    {
                        // No bytes left
                        // don't fail if we are allowing overflows
                        if (decoder == null || decoder.MustFlush)
                        {
                            // Not enough bytes, fallback lead byte
                            buffer.Fallback(ch);

                            // Break if we fail & break because !MoreData
                            break;
                        }

                        if (decoder != null)
                            decoder.ClearMustFlush();

                        // Stick it in decoder
                        if (chars != null)
                        {
                            decoder.bytesLeftOverCount = 1;
                            decoder.bytesLeftOver[0] = ch;
                            bUsedDecoder = true;
                        }
                        break;
                    }

                    // Everett uses space as an escape character for single SBCS bytes
                    byte ch2 = buffer.GetNextByte();
                    ushort iBytes = (ushort)(ch << 8 | ch2);

                    if (ch == ' ' && ch2 != 0)
                    {
                        // Get next char and treat it like ASCII (Everett treated space like an escape
                        // allowing the next char to be just ascii)
                        cm = (char)ch2;
                        goto STOREMULTIBYTE;
                    }

                    // Bytes should be in range: lead byte 0x21-0x77, trail byte: 0x21 - 0x7e
                    if ((ch < 0x21 || ch > 0x77 || ch2 < 0x21 || ch2 > 0x7e) &&
                    // Everett allowed high bit mappings for same characters (but only if both bits set)
                        (ch < 0xa1 || ch > 0xf7 || ch2 < 0xa1 || ch2 > 0xfe))
                    {
                        // For some reason Everett allowed XX20 to become unicode 3000... (ideo sp)
                        if (ch2 == 0x20 && 0x21 <= ch && ch <= 0x7d)
                        {
                            iBytes = 0x2121;
                            goto MULTIBYTE;
                        }

                        // Illegal char, use fallback.  If lead byte is 0 have to do it special and do it first
                        if (!buffer.Fallback((byte)(iBytes>>8), (byte)(iBytes)))
                            break;
                        continue;
                    }

                    MULTIBYTE:
                    iBytes |= 0x8080;
                    // Look up the multibyte char to stick it in our data

                    // We have a iBytes to try to convert.
                    cm = mapBytesToUnicode[iBytes];

                    STOREMULTIBYTE:

                    // See if it was unknown
                    if (cm == UNKNOWN_CHAR_FLAG && iBytes != 0)
                    {
                        // Fall back the unknown stuff
                        if (!buffer.Fallback((byte)(iBytes>>8), (byte)(iBytes)))
                            break;
                        continue;
                    }

                    if (!buffer.AddChar(cm, 2))
                        break;              // convert ran out of buffer, stop
                    continue;
                }

                // Just ASCII
                // We allow some chars > 7f because everett did, so we have to look them up.
                STOREASCII:
                char c = mapBytesToUnicode[ch];

                // Check if it was unknown
                if ((c == UNKNOWN_CHAR_FLAG || c == 0) && (ch != 0))
                {
                    // fallback the unkown bytes
                    if (!buffer.Fallback((byte)ch))
                        break;
                    continue;
                }

                // Go ahead and add our ASCII character
                if (!buffer.AddChar(c))
                    break;                  // convert ran out of buffer, stop
            }

            // Need to remember our state, IF we're not counting
            if (chars != null && decoder != null)
            {
                if (!bUsedDecoder)
                {
                    // If we didn't use it, clear the byte left over
                    decoder.bytesLeftOverCount = 0;
                }

                if (decoder.MustFlush && decoder.bytesLeftOverCount == 0)
                {
                    decoder.currentMode = ISO2022Modes.ModeASCII;
                }
                else
                {
                    // Either not flushing or had state (from convert)
                    Contract.Assert(!decoder.MustFlush || !decoder.m_throwOnOverflow,
                        "[ISO2022Encoding.GetCharsCP52936]Expected no state or not converting or not flushing");
                                        
                    decoder.currentMode = currentMode;
                }
                decoder.m_bytesUsed = buffer.BytesUsed;
            }

            // Return # of characters we found
            return buffer.Count;
        }
Ejemplo n.º 4
0
        [System.Security.SecurityCritical]  // auto-generated
        private unsafe int GetCharsCP50225KR(byte* bytes, int byteCount,
                                                   char* chars, int charCount, ISO2022Decoder decoder)
        {
            // Get our info.
            Encoding.EncodingCharBuffer buffer = new Encoding.EncodingCharBuffer(
                this, decoder, chars, charCount, bytes, byteCount);

            // No mode information yet
            ISO2022Modes currentMode = ISO2022Modes.ModeASCII;      // Our current Mode

            byte[] escapeBytes = new byte[4];
            int escapeCount = 0;

            if (decoder != null)
            {
                currentMode = decoder.currentMode;

                // See if we have leftover decoder buffer to use
                // Load our bytesLeftOver
                escapeCount = decoder.bytesLeftOverCount;

                // Don't want to mess up decoder if we're counting or throw an exception
                for (int i = 0; i < escapeCount; i++)
                    escapeBytes[i] = decoder.bytesLeftOver[i];
            }

            // Do this until the end, just do '?' replacement because we don't have fallbacks for decodings.
            while (buffer.MoreData || escapeCount > 0)
            {
                byte ch;

                if (escapeCount > 0)
                {
                    // Get more escape sequences if necessary
                    if (escapeBytes[0] == ESCAPE)
                    {
                        // Stop if no more input
                        if (!buffer.MoreData)
                        {
                            if (decoder != null && !decoder.MustFlush)
                                break;
                        }
                        else
                        {
                            // Add it to the sequence we can check
                            escapeBytes[escapeCount++] = buffer.GetNextByte();

                            // We have an escape sequence
                            ISO2022Modes modeReturn =
                                CheckEscapeSequenceKR(escapeBytes, escapeCount);

                            if (modeReturn != ISO2022Modes.ModeInvalidEscape)
                            {
                                if (modeReturn != ISO2022Modes.ModeIncompleteEscape)
                                {
                                    // Processed escape correctly, no effect (we know about KR mode)
                                    escapeCount = 0;
                                }

                                // Either way, continue to get next escape or real byte
                                continue;
                            }
                        }

                        // If ModeInvalidEscape, or no input & must flush, then fall through to add escape.
                    }

                    // Still have something left over in escape buffer
                    // Get it and move them down one
                    ch = DecrementEscapeBytes(ref escapeBytes, ref escapeCount);
                }
                else
                {
                    // Get our next byte
                    ch = buffer.GetNextByte();

                    if (ch == ESCAPE)
                    {
                        // We'll have an escape sequence, use it if we don't have one buffered already
                        if (escapeCount == 0)
                        {
                            // Start this new escape sequence
                            escapeBytes[0] = ch;
                            escapeCount = 1;
                            continue;
                        }

                        // Flush previous escape sequence, then reuse this escape byte
                        buffer.AdjustBytes(-1);
                    }
                }

                if (ch == SHIFT_OUT)
                {
                   currentMode = ISO2022Modes.ModeKR;
                   continue;
                }
                else if (ch == SHIFT_IN)
                {
                   currentMode = ISO2022Modes.ModeASCII;
                   continue;
                }

                // Get our full character
                ushort iBytes = ch;
                bool b2Bytes = false;

                // MLANG was passing through ' ', '\t' and '\n', so we do so as well, but I don't see that in the RFC.
                if (currentMode == ISO2022Modes.ModeKR && ch != ' ' && ch != '\t' && ch != '\n')
                {
                    //
                    //  To handle errors, we need to check:
                    //    1. if trailbyte is there
                    //    2. if code is valid
                    //
                    if (escapeCount > 0)
                    {
                        // Let another escape fall through
                        if (escapeBytes[0] != ESCAPE)
                        {
                            // Move them down one & get the next data
                            iBytes <<= 8;
                            iBytes |= DecrementEscapeBytes(ref escapeBytes, ref escapeCount);
                            b2Bytes = true;
                        }
                    }
                    else if (buffer.MoreData)
                    {
                        iBytes <<= 8;
                        iBytes |= buffer.GetNextByte();
                        b2Bytes = true;
                    }
                    else
                    {
                        // Not enough input, use decoder if possible
                        if (decoder == null || decoder.MustFlush)
                        {
                            // No decoder, do fallback for lonely 1st byte
                            buffer.Fallback(ch);
                            break;
                        }

                        // Stick it in the decoder if we're not counting
                        if (chars != null)
                        {
                            escapeBytes[0] = ch;
                            escapeCount = 1;
                        }
                        break;
                    }
                }

                // We have a iBytes to try to convert.
                char c = mapBytesToUnicode[iBytes];

                // See if it was unknown
                if (c == UNKNOWN_CHAR_FLAG && iBytes != 0)
                {
                    // Have to do fallback
                    if (b2Bytes)
                    {
                        if (!buffer.Fallback((byte)(iBytes >> 8), (byte)iBytes))
                            break;
                    }
                    else
                    {
                        if (!buffer.Fallback(ch))
                            break;
                    }
                }
                else
                {
                    if (!buffer.AddChar(c, b2Bytes ? 2:1))
                        break;
                }
            }

            // Make sure our decoder state matches our mode, if not counting
            if (chars != null && decoder != null)
            {
                // Remember it if we don't flush
                if (!decoder.MustFlush || escapeCount != 0)
                {
                    // Either not flushing or had state (from convert)
                    Contract.Assert(!decoder.MustFlush || !decoder.m_throwOnOverflow,
                        "[ISO2022Encoding.GetCharsCP50225KR]Expected no state or not converting or not flushing");
                    
                    decoder.currentMode = currentMode;

                    // Remember escape buffer
                    decoder.bytesLeftOverCount = escapeCount;
                    decoder.bytesLeftOver = escapeBytes;
                }
                else
                {
                    // We flush, clear buffer
                    decoder.currentMode = ISO2022Modes.ModeASCII;
                    decoder.shiftInOutMode = ISO2022Modes.ModeASCII;
                    decoder.bytesLeftOverCount = 0;
                }

                decoder.m_bytesUsed = buffer.BytesUsed;
            }

            // Return # of characters we found
            return buffer.Count;
        }
        private unsafe int GetCharsCP52936(byte *bytes, int byteCount, char *chars, int charCount, ISO2022Decoder decoder)
        {
            Encoding.EncodingCharBuffer buffer = new Encoding.EncodingCharBuffer(this, decoder, chars, charCount, bytes, byteCount);
            ISO2022Modes modeASCII             = ISO2022Modes.ModeASCII;
            int          num  = -1;
            bool         flag = false;

            if (decoder != null)
            {
                modeASCII = decoder.currentMode;
                if (decoder.bytesLeftOverCount != 0)
                {
                    num = decoder.bytesLeftOver[0];
                }
            }
            while (buffer.MoreData || (num >= 0))
            {
                byte nextByte;
                if (num >= 0)
                {
                    nextByte = (byte)num;
                    num      = -1;
                }
                else
                {
                    nextByte = buffer.GetNextByte();
                }
                if (nextByte == 0x7e)
                {
                    if (!buffer.MoreData)
                    {
                        if ((decoder == null) || decoder.MustFlush)
                        {
                            buffer.Fallback(nextByte);
                        }
                        else
                        {
                            if (decoder != null)
                            {
                                decoder.ClearMustFlush();
                            }
                            if (chars != null)
                            {
                                decoder.bytesLeftOverCount = 1;
                                decoder.bytesLeftOver[0]   = 0x7e;
                                flag = true;
                            }
                        }
                        break;
                    }
                    nextByte = buffer.GetNextByte();
                    if ((nextByte == 0x7e) && (modeASCII == ISO2022Modes.ModeASCII))
                    {
                        if (buffer.AddChar((char)nextByte, 2))
                        {
                            continue;
                        }
                        break;
                    }
                    if (nextByte == 0x7b)
                    {
                        modeASCII = ISO2022Modes.ModeHZ;
                        continue;
                    }
                    if (nextByte == 0x7d)
                    {
                        modeASCII = ISO2022Modes.ModeASCII;
                        continue;
                    }
                    if (nextByte == 10)
                    {
                        continue;
                    }
                    buffer.AdjustBytes(-1);
                    nextByte = 0x7e;
                }
                if ((modeASCII != ISO2022Modes.ModeASCII) && (nextByte >= 0x20))
                {
                    if (!buffer.MoreData)
                    {
                        if ((decoder == null) || decoder.MustFlush)
                        {
                            buffer.Fallback(nextByte);
                        }
                        else
                        {
                            if (decoder != null)
                            {
                                decoder.ClearMustFlush();
                            }
                            if (chars != null)
                            {
                                decoder.bytesLeftOverCount = 1;
                                decoder.bytesLeftOver[0]   = nextByte;
                                flag = true;
                            }
                        }
                    }
                    else
                    {
                        char   ch;
                        byte   num3  = buffer.GetNextByte();
                        ushort index = (ushort)((nextByte << 8) | num3);
                        if ((nextByte == 0x20) && (num3 != 0))
                        {
                            ch = (char)num3;
                        }
                        else
                        {
                            if ((((nextByte < 0x21) || (nextByte > 0x77)) || ((num3 < 0x21) || (num3 > 0x7e))) && (((nextByte < 0xa1) || (nextByte > 0xf7)) || ((num3 < 0xa1) || (num3 > 0xfe))))
                            {
                                if (((num3 == 0x20) && (0x21 <= nextByte)) && (nextByte <= 0x7d))
                                {
                                    index = 0x2121;
                                }
                                else
                                {
                                    if (buffer.Fallback((byte)(index >> 8), (byte)index))
                                    {
                                        continue;
                                    }
                                    break;
                                }
                            }
                            index = (ushort)(index | 0x8080);
                            ch    = base.mapBytesToUnicode[index];
                        }
                        if ((ch == '\0') && (index != 0))
                        {
                            if (buffer.Fallback((byte)(index >> 8), (byte)index))
                            {
                                continue;
                            }
                        }
                        else if (buffer.AddChar(ch, 2))
                        {
                            continue;
                        }
                    }
                    break;
                }
                char ch2 = base.mapBytesToUnicode[nextByte];
                if (((ch2 == '\0') || (ch2 == '\0')) && (nextByte != 0))
                {
                    if (buffer.Fallback(nextByte))
                    {
                        continue;
                    }
                    break;
                }
                if (!buffer.AddChar(ch2))
                {
                    break;
                }
            }
            if ((chars != null) && (decoder != null))
            {
                if (!flag)
                {
                    decoder.bytesLeftOverCount = 0;
                }
                if (decoder.MustFlush && (decoder.bytesLeftOverCount == 0))
                {
                    decoder.currentMode = ISO2022Modes.ModeASCII;
                }
                else
                {
                    decoder.currentMode = modeASCII;
                }
                decoder.m_bytesUsed = buffer.BytesUsed;
            }
            return(buffer.Count);
        }
        private unsafe int GetCharsCP5022xJP(byte *bytes, int byteCount, char *chars, int charCount, ISO2022Decoder decoder)
        {
            Encoding.EncodingCharBuffer buffer = new Encoding.EncodingCharBuffer(this, decoder, chars, charCount, bytes, byteCount);
            ISO2022Modes modeASCII             = ISO2022Modes.ModeASCII;
            ISO2022Modes shiftInOutMode        = ISO2022Modes.ModeASCII;

            byte[] buffer2     = new byte[4];
            int    escapeCount = 0;

            if (decoder != null)
            {
                modeASCII      = decoder.currentMode;
                shiftInOutMode = decoder.shiftInOutMode;
                escapeCount    = decoder.bytesLeftOverCount;
                for (int i = 0; i < escapeCount; i++)
                {
                    buffer2[i] = decoder.bytesLeftOver[i];
                }
            }
            while (buffer.MoreData || (escapeCount > 0))
            {
                byte nextByte;
                if (escapeCount > 0)
                {
                    if (buffer2[0] == 0x1b)
                    {
                        if (!buffer.MoreData)
                        {
                            if ((decoder != null) && !decoder.MustFlush)
                            {
                                break;
                            }
                        }
                        else
                        {
                            buffer2[escapeCount++] = buffer.GetNextByte();
                            ISO2022Modes modes3 = this.CheckEscapeSequenceJP(buffer2, escapeCount);
                            if (modes3 != ISO2022Modes.ModeInvalidEscape)
                            {
                                if (modes3 != ISO2022Modes.ModeIncompleteEscape)
                                {
                                    escapeCount = 0;
                                    modeASCII   = shiftInOutMode = modes3;
                                }
                                continue;
                            }
                        }
                    }
                    nextByte = this.DecrementEscapeBytes(ref buffer2, ref escapeCount);
                }
                else
                {
                    nextByte = buffer.GetNextByte();
                    if (nextByte == 0x1b)
                    {
                        if (escapeCount == 0)
                        {
                            buffer2[0]  = nextByte;
                            escapeCount = 1;
                            continue;
                        }
                        buffer.AdjustBytes(-1);
                    }
                }
                if (nextByte == 14)
                {
                    shiftInOutMode = modeASCII;
                    modeASCII      = ISO2022Modes.ModeHalfwidthKatakana;
                }
                else
                {
                    if (nextByte == 15)
                    {
                        modeASCII = shiftInOutMode;
                        continue;
                    }
                    ushort index = nextByte;
                    bool   flag  = false;
                    if (modeASCII == ISO2022Modes.ModeJIS0208)
                    {
                        if (escapeCount > 0)
                        {
                            if (buffer2[0] != 0x1b)
                            {
                                index = (ushort)(index << 8);
                                index = (ushort)(index | this.DecrementEscapeBytes(ref buffer2, ref escapeCount));
                                flag  = true;
                            }
                        }
                        else if (buffer.MoreData)
                        {
                            index = (ushort)(index << 8);
                            index = (ushort)(index | buffer.GetNextByte());
                            flag  = true;
                        }
                        else
                        {
                            if ((decoder == null) || decoder.MustFlush)
                            {
                                buffer.Fallback(nextByte);
                            }
                            else if (chars != null)
                            {
                                buffer2[0]  = nextByte;
                                escapeCount = 1;
                            }
                            break;
                        }
                        if (flag && ((index & 0xff00) == 0x2a00))
                        {
                            index = (ushort)(index & 0xff);
                            index = (ushort)(index | 0x1000);
                        }
                    }
                    else if ((index >= 0xa1) && (index <= 0xdf))
                    {
                        index = (ushort)(index | 0x1000);
                        index = (ushort)(index & 0xff7f);
                    }
                    else if (modeASCII == ISO2022Modes.ModeHalfwidthKatakana)
                    {
                        index = (ushort)(index | 0x1000);
                    }
                    char ch = base.mapBytesToUnicode[index];
                    if ((ch == '\0') && (index != 0))
                    {
                        if (flag)
                        {
                            if (buffer.Fallback((byte)(index >> 8), (byte)index))
                            {
                                continue;
                            }
                        }
                        else if (buffer.Fallback(nextByte))
                        {
                            continue;
                        }
                        break;
                    }
                    if (!buffer.AddChar(ch, flag ? 2 : 1))
                    {
                        break;
                    }
                }
            }
            if ((chars != null) && (decoder != null))
            {
                if (!decoder.MustFlush || (escapeCount != 0))
                {
                    decoder.currentMode        = modeASCII;
                    decoder.shiftInOutMode     = shiftInOutMode;
                    decoder.bytesLeftOverCount = escapeCount;
                    decoder.bytesLeftOver      = buffer2;
                }
                else
                {
                    decoder.currentMode        = ISO2022Modes.ModeASCII;
                    decoder.shiftInOutMode     = ISO2022Modes.ModeASCII;
                    decoder.bytesLeftOverCount = 0;
                }
                decoder.m_bytesUsed = buffer.BytesUsed;
            }
            return(buffer.Count);
        }
 private unsafe int GetCharsCP52936(byte* bytes, int byteCount, char* chars, int charCount, ISO2022Decoder decoder)
 {
     Encoding.EncodingCharBuffer buffer = new Encoding.EncodingCharBuffer(this, decoder, chars, charCount, bytes, byteCount);
     ISO2022Modes modeASCII = ISO2022Modes.ModeASCII;
     int num = -1;
     bool flag = false;
     if (decoder != null)
     {
         modeASCII = decoder.currentMode;
         if (decoder.bytesLeftOverCount != 0)
         {
             num = decoder.bytesLeftOver[0];
         }
     }
     while (buffer.MoreData || (num >= 0))
     {
         byte nextByte;
         if (num >= 0)
         {
             nextByte = (byte) num;
             num = -1;
         }
         else
         {
             nextByte = buffer.GetNextByte();
         }
         if (nextByte == 0x7e)
         {
             if (!buffer.MoreData)
             {
                 if ((decoder == null) || decoder.MustFlush)
                 {
                     buffer.Fallback(nextByte);
                 }
                 else
                 {
                     if (decoder != null)
                     {
                         decoder.ClearMustFlush();
                     }
                     if (chars != null)
                     {
                         decoder.bytesLeftOverCount = 1;
                         decoder.bytesLeftOver[0] = 0x7e;
                         flag = true;
                     }
                 }
                 break;
             }
             nextByte = buffer.GetNextByte();
             if ((nextByte == 0x7e) && (modeASCII == ISO2022Modes.ModeASCII))
             {
                 if (buffer.AddChar((char) nextByte, 2))
                 {
                     continue;
                 }
                 break;
             }
             if (nextByte == 0x7b)
             {
                 modeASCII = ISO2022Modes.ModeHZ;
                 continue;
             }
             if (nextByte == 0x7d)
             {
                 modeASCII = ISO2022Modes.ModeASCII;
                 continue;
             }
             if (nextByte == 10)
             {
                 continue;
             }
             buffer.AdjustBytes(-1);
             nextByte = 0x7e;
         }
         if ((modeASCII != ISO2022Modes.ModeASCII) && (nextByte >= 0x20))
         {
             if (!buffer.MoreData)
             {
                 if ((decoder == null) || decoder.MustFlush)
                 {
                     buffer.Fallback(nextByte);
                 }
                 else
                 {
                     if (decoder != null)
                     {
                         decoder.ClearMustFlush();
                     }
                     if (chars != null)
                     {
                         decoder.bytesLeftOverCount = 1;
                         decoder.bytesLeftOver[0] = nextByte;
                         flag = true;
                     }
                 }
             }
             else
             {
                 char ch;
                 byte num3 = buffer.GetNextByte();
                 ushort index = (ushort) ((nextByte << 8) | num3);
                 if ((nextByte == 0x20) && (num3 != 0))
                 {
                     ch = (char) num3;
                 }
                 else
                 {
                     if ((((nextByte < 0x21) || (nextByte > 0x77)) || ((num3 < 0x21) || (num3 > 0x7e))) && (((nextByte < 0xa1) || (nextByte > 0xf7)) || ((num3 < 0xa1) || (num3 > 0xfe))))
                     {
                         if (((num3 == 0x20) && (0x21 <= nextByte)) && (nextByte <= 0x7d))
                         {
                             index = 0x2121;
                         }
                         else
                         {
                             if (buffer.Fallback((byte) (index >> 8), (byte) index))
                             {
                                 continue;
                             }
                             break;
                         }
                     }
                     index = (ushort) (index | 0x8080);
                     ch = base.mapBytesToUnicode[index];
                 }
                 if ((ch == '\0') && (index != 0))
                 {
                     if (buffer.Fallback((byte) (index >> 8), (byte) index))
                     {
                         continue;
                     }
                 }
                 else if (buffer.AddChar(ch, 2))
                 {
                     continue;
                 }
             }
             break;
         }
         char ch2 = base.mapBytesToUnicode[nextByte];
         if (((ch2 == '\0') || (ch2 == '\0')) && (nextByte != 0))
         {
             if (buffer.Fallback(nextByte))
             {
                 continue;
             }
             break;
         }
         if (!buffer.AddChar(ch2))
         {
             break;
         }
     }
     if ((chars != null) && (decoder != null))
     {
         if (!flag)
         {
             decoder.bytesLeftOverCount = 0;
         }
         if (decoder.MustFlush && (decoder.bytesLeftOverCount == 0))
         {
             decoder.currentMode = ISO2022Modes.ModeASCII;
         }
         else
         {
             decoder.currentMode = modeASCII;
         }
         decoder.m_bytesUsed = buffer.BytesUsed;
     }
     return buffer.Count;
 }
 private unsafe int GetCharsCP5022xJP(byte* bytes, int byteCount, char* chars, int charCount, ISO2022Decoder decoder)
 {
     Encoding.EncodingCharBuffer buffer = new Encoding.EncodingCharBuffer(this, decoder, chars, charCount, bytes, byteCount);
     ISO2022Modes modeASCII = ISO2022Modes.ModeASCII;
     ISO2022Modes shiftInOutMode = ISO2022Modes.ModeASCII;
     byte[] buffer2 = new byte[4];
     int escapeCount = 0;
     if (decoder != null)
     {
         modeASCII = decoder.currentMode;
         shiftInOutMode = decoder.shiftInOutMode;
         escapeCount = decoder.bytesLeftOverCount;
         for (int i = 0; i < escapeCount; i++)
         {
             buffer2[i] = decoder.bytesLeftOver[i];
         }
     }
     while (buffer.MoreData || (escapeCount > 0))
     {
         byte nextByte;
         if (escapeCount > 0)
         {
             if (buffer2[0] == 0x1b)
             {
                 if (!buffer.MoreData)
                 {
                     if ((decoder != null) && !decoder.MustFlush)
                     {
                         break;
                     }
                 }
                 else
                 {
                     buffer2[escapeCount++] = buffer.GetNextByte();
                     ISO2022Modes modes3 = this.CheckEscapeSequenceJP(buffer2, escapeCount);
                     if (modes3 != ISO2022Modes.ModeInvalidEscape)
                     {
                         if (modes3 != ISO2022Modes.ModeIncompleteEscape)
                         {
                             escapeCount = 0;
                             modeASCII = shiftInOutMode = modes3;
                         }
                         continue;
                     }
                 }
             }
             nextByte = this.DecrementEscapeBytes(ref buffer2, ref escapeCount);
         }
         else
         {
             nextByte = buffer.GetNextByte();
             if (nextByte == 0x1b)
             {
                 if (escapeCount == 0)
                 {
                     buffer2[0] = nextByte;
                     escapeCount = 1;
                     continue;
                 }
                 buffer.AdjustBytes(-1);
             }
         }
         if (nextByte == 14)
         {
             shiftInOutMode = modeASCII;
             modeASCII = ISO2022Modes.ModeHalfwidthKatakana;
         }
         else
         {
             if (nextByte == 15)
             {
                 modeASCII = shiftInOutMode;
                 continue;
             }
             ushort index = nextByte;
             bool flag = false;
             if (modeASCII == ISO2022Modes.ModeJIS0208)
             {
                 if (escapeCount > 0)
                 {
                     if (buffer2[0] != 0x1b)
                     {
                         index = (ushort) (index << 8);
                         index = (ushort) (index | this.DecrementEscapeBytes(ref buffer2, ref escapeCount));
                         flag = true;
                     }
                 }
                 else if (buffer.MoreData)
                 {
                     index = (ushort) (index << 8);
                     index = (ushort) (index | buffer.GetNextByte());
                     flag = true;
                 }
                 else
                 {
                     if ((decoder == null) || decoder.MustFlush)
                     {
                         buffer.Fallback(nextByte);
                     }
                     else if (chars != null)
                     {
                         buffer2[0] = nextByte;
                         escapeCount = 1;
                     }
                     break;
                 }
                 if (flag && ((index & 0xff00) == 0x2a00))
                 {
                     index = (ushort) (index & 0xff);
                     index = (ushort) (index | 0x1000);
                 }
             }
             else if ((index >= 0xa1) && (index <= 0xdf))
             {
                 index = (ushort) (index | 0x1000);
                 index = (ushort) (index & 0xff7f);
             }
             else if (modeASCII == ISO2022Modes.ModeHalfwidthKatakana)
             {
                 index = (ushort) (index | 0x1000);
             }
             char ch = base.mapBytesToUnicode[index];
             if ((ch == '\0') && (index != 0))
             {
                 if (flag)
                 {
                     if (buffer.Fallback((byte) (index >> 8), (byte) index))
                     {
                         continue;
                     }
                 }
                 else if (buffer.Fallback(nextByte))
                 {
                     continue;
                 }
                 break;
             }
             if (!buffer.AddChar(ch, flag ? 2 : 1))
             {
                 break;
             }
         }
     }
     if ((chars != null) && (decoder != null))
     {
         if (!decoder.MustFlush || (escapeCount != 0))
         {
             decoder.currentMode = modeASCII;
             decoder.shiftInOutMode = shiftInOutMode;
             decoder.bytesLeftOverCount = escapeCount;
             decoder.bytesLeftOver = buffer2;
         }
         else
         {
             decoder.currentMode = ISO2022Modes.ModeASCII;
             decoder.shiftInOutMode = ISO2022Modes.ModeASCII;
             decoder.bytesLeftOverCount = 0;
         }
         decoder.m_bytesUsed = buffer.BytesUsed;
     }
     return buffer.Count;
 }