Exemple #1
0
        internal override unsafe int GetBytes(char *chars, int charCount,
                                              byte *bytes, int byteCount, EncoderNLS baseEncoder)
        {
            Debug.Assert(byteCount >= 0, "[UTF7Encoding.GetBytes]byteCount >=0");
            Debug.Assert(chars != null, "[UTF7Encoding.GetBytes]chars!=null");
            Debug.Assert(charCount >= 0, "[UTF7Encoding.GetBytes]charCount >=0");

            // Get encoder info
            UTF7Encoding.Encoder encoder = (UTF7Encoding.Encoder)baseEncoder;

            // Default bits & count
            int bits     = 0;
            int bitCount = -1;

            // prepare our helpers
            Encoding.EncodingByteBuffer buffer = new Encoding.EncodingByteBuffer(
                this, encoder, bytes, byteCount, chars, charCount);

            if (encoder != null)
            {
                bits     = encoder.bits;
                bitCount = encoder.bitCount;

                // May have had too many left over
                while (bitCount >= 6)
                {
                    bitCount -= 6;
                    // If we fail we'll never really have enough room
                    if (!buffer.AddByte(_base64Bytes[(bits >> bitCount) & 0x3F]))
                    {
                        ThrowBytesOverflow(encoder, buffer.Count == 0);
                    }
                }
            }

            while (buffer.MoreData)
            {
                char currentChar = buffer.GetNextChar();

                if (currentChar < 0x80 && _directEncode[currentChar])
                {
                    if (bitCount >= 0)
                    {
                        if (bitCount > 0)
                        {
                            // Try to add the next byte
                            if (!buffer.AddByte(_base64Bytes[bits << 6 - bitCount & 0x3F]))
                            {
                                break;                                          // Stop here, didn't throw
                            }
                            bitCount = 0;
                        }

                        // Need to get emit '-' and our char, 2 bytes total
                        if (!buffer.AddByte((byte)'-'))
                        {
                            break;                                          // Stop here, didn't throw
                        }
                        bitCount = -1;
                    }

                    // Need to emit our char
                    if (!buffer.AddByte((byte)currentChar))
                    {
                        break;                                          // Stop here, didn't throw
                    }
                }
                else if (bitCount < 0 && currentChar == '+')
                {
                    if (!buffer.AddByte((byte)'+', (byte)'-'))
                    {
                        break;                                          // Stop here, didn't throw
                    }
                }
                else
                {
                    if (bitCount < 0)
                    {
                        // Need to emit a + and 12 bits (3 bytes)
                        // Only 12 of the 16 bits will be emitted this time, the other 4 wait 'til next time
                        if (!buffer.AddByte((byte)'+'))
                        {
                            break;                                          // Stop here, didn't throw
                        }
                        // We're now in bit mode, but haven't stored data yet
                        bitCount = 0;
                    }

                    // Add our bits
                    bits      = bits << 16 | currentChar;
                    bitCount += 16;

                    while (bitCount >= 6)
                    {
                        bitCount -= 6;
                        if (!buffer.AddByte(_base64Bytes[(bits >> bitCount) & 0x3F]))
                        {
                            bitCount   += 6;                            // We didn't use these bits
                            currentChar = buffer.GetNextChar();         // We're processing this char still, but AddByte
                                                                        // --'d it when we ran out of space
                            break;                                      // Stop here, not enough room for bytes
                        }
                    }

                    if (bitCount >= 6)
                    {
                        break;                  // Didn't have room to encode enough bits
                    }
                }
            }

            // Now if we have bits left over we have to encode them.
            // MustFlush may have been cleared by encoding.ThrowBytesOverflow earlier if converting
            if (bitCount >= 0 && (encoder == null || encoder.MustFlush))
            {
                // Do we have bits we have to stick in?
                if (bitCount > 0)
                {
                    if (buffer.AddByte(_base64Bytes[(bits << (6 - bitCount)) & 0x3F]))
                    {
                        // Emitted spare bits, 0 bits left
                        bitCount = 0;
                    }
                }

                // If converting and failed bitCount above, then we'll fail this too
                if (buffer.AddByte((byte)'-'))
                {
                    // turned off bit mode';
                    bits     = 0;
                    bitCount = -1;
                }
                else
                {
                    // If not successful, convert will maintain state for next time, also
                    // AddByte will have decremented our char count, however we need it to remain the same
                    buffer.GetNextChar();
                }
            }

            // Do we have an encoder we're allowed to use?
            // bytes == null if counting, so don't use encoder then
            if (bytes != null && encoder != null)
            {
                // We already cleared bits & bitcount for mustflush case
                encoder.bits       = bits;
                encoder.bitCount   = bitCount;
                encoder._charsUsed = buffer.CharsUsed;
            }

            return(buffer.Count);
        }
Exemple #2
0
        internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS baseEncoder)
        {
            ISCIIEncoding.ISCIIEncoder  isciiEncoder       = (ISCIIEncoding.ISCIIEncoder)baseEncoder;
            Encoding.EncodingByteBuffer encodingByteBuffer = new Encoding.EncodingByteBuffer((Encoding)this, (EncoderNLS)isciiEncoder, bytes, byteCount, chars, charCount);
            int  num1 = this.defaultCodePage;
            bool flag = false;

            if (isciiEncoder != null)
            {
                num1 = isciiEncoder.currentCodePage;
                flag = isciiEncoder.bLastVirama;
                if ((int)isciiEncoder.charLeftOver > 0)
                {
                    encodingByteBuffer.Fallback(isciiEncoder.charLeftOver);
                    flag = false;
                }
            }
            while (encodingByteBuffer.MoreData)
            {
                char nextChar = encodingByteBuffer.GetNextChar();
                if ((int)nextChar < 160)
                {
                    if (encodingByteBuffer.AddByte((byte)nextChar))
                    {
                        flag = false;
                    }
                    else
                    {
                        break;
                    }
                }
                else if ((int)nextChar < 2305 || (int)nextChar > 3439)
                {
                    if (flag && ((int)nextChar == 8204 || (int)nextChar == 8205))
                    {
                        if ((int)nextChar == 8204)
                        {
                            if (!encodingByteBuffer.AddByte((byte)232))
                            {
                                break;
                            }
                        }
                        else if (!encodingByteBuffer.AddByte((byte)233))
                        {
                            break;
                        }
                        flag = false;
                    }
                    else
                    {
                        encodingByteBuffer.Fallback(nextChar);
                        flag = false;
                    }
                }
                else
                {
                    int  num2 = ISCIIEncoding.UnicodeToIndicChar[(int)nextChar - 2305];
                    byte b    = (byte)num2;
                    int  num3 = 15 & num2 >> 8;
                    int  num4 = 61440 & num2;
                    if (num2 == 0)
                    {
                        encodingByteBuffer.Fallback(nextChar);
                        flag = false;
                    }
                    else
                    {
                        if (num3 != num1)
                        {
                            if (encodingByteBuffer.AddByte((byte)239, (byte)(num3 | 64)))
                            {
                                num1 = num3;
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (encodingByteBuffer.AddByte(b, num4 != 0 ? 1 : 0))
                        {
                            flag = (int)b == 232;
                            if (num4 != 0 && !encodingByteBuffer.AddByte(ISCIIEncoding.SecondIndicByte[num4 >> 12]))
                            {
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            if (num1 != this.defaultCodePage && (isciiEncoder == null || isciiEncoder.MustFlush))
            {
                if (encodingByteBuffer.AddByte((byte)239, (byte)(this.defaultCodePage | 64)))
                {
                    num1 = this.defaultCodePage;
                }
                else
                {
                    int num2 = (int)encodingByteBuffer.GetNextChar();
                }
                flag = false;
            }
            if (isciiEncoder != null && (IntPtr)bytes != IntPtr.Zero)
            {
                if (!encodingByteBuffer.fallbackBuffer.bUsedEncoder)
                {
                    isciiEncoder.charLeftOver = char.MinValue;
                }
                isciiEncoder.currentCodePage = num1;
                isciiEncoder.bLastVirama     = flag;
                isciiEncoder.m_charsUsed     = encodingByteBuffer.CharsUsed;
            }
            return(encodingByteBuffer.Count);
        }
        internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder)
        {
            char charFallback = char.MinValue;

            if (encoder != null)
            {
                charFallback = encoder.charLeftOver;
            }
            Encoding.EncodingByteBuffer encodingByteBuffer = new Encoding.EncodingByteBuffer((Encoding)this, encoder, bytes, byteCount, chars, charCount);
            while (true)
            {
                while (encodingByteBuffer.MoreData)
                {
                    char nextChar = encodingByteBuffer.GetNextChar();
                    if ((int)charFallback != 0)
                    {
                        if (!char.IsLowSurrogate(nextChar))
                        {
                            encodingByteBuffer.MovePrevious(false);
                            if (!encodingByteBuffer.Fallback(charFallback))
                            {
                                charFallback = char.MinValue;
                                break;
                            }
                            charFallback = char.MinValue;
                        }
                        else
                        {
                            int  num1 = ((int)charFallback - 55296 << 10) + ((int)nextChar - 56320);
                            byte b4   = (byte)(num1 % 10 + 48);
                            int  num2 = num1 / 10;
                            byte b3   = (byte)(num2 % 126 + 129);
                            int  num3 = num2 / 126;
                            byte b2   = (byte)(num3 % 10 + 48);
                            int  num4 = num3 / 10;
                            charFallback = char.MinValue;
                            if (!encodingByteBuffer.AddByte((byte)(num4 + 144), b2, b3, b4))
                            {
                                encodingByteBuffer.MovePrevious(false);
                                break;
                            }
                            charFallback = char.MinValue;
                        }
                    }
                    else if ((int)nextChar <= (int)sbyte.MaxValue)
                    {
                        if (!encodingByteBuffer.AddByte((byte)nextChar))
                        {
                            break;
                        }
                    }
                    else if (char.IsHighSurrogate(nextChar))
                    {
                        charFallback = nextChar;
                    }
                    else if (char.IsLowSurrogate(nextChar))
                    {
                        if (!encodingByteBuffer.Fallback(nextChar))
                        {
                            break;
                        }
                    }
                    else
                    {
                        ushort num1 = this.mapUnicodeToBytes[nextChar];
                        if (this.Is4Byte(nextChar))
                        {
                            byte   b4   = (byte)((int)num1 % 10 + 48);
                            ushort num2 = (ushort)((uint)num1 / 10U);
                            byte   b3   = (byte)((int)num2 % 126 + 129);
                            ushort num3 = (ushort)((uint)num2 / 126U);
                            byte   b2   = (byte)((int)num3 % 10 + 48);
                            ushort num4 = (ushort)((uint)num3 / 10U);
                            if (!encodingByteBuffer.AddByte((byte)((uint)num4 + 129U), b2, b3, b4))
                            {
                                break;
                            }
                        }
                        else if (!encodingByteBuffer.AddByte((byte)((uint)num1 >> 8), (byte)((uint)num1 & (uint)byte.MaxValue)))
                        {
                            break;
                        }
                    }
                }
                if ((encoder == null || encoder.MustFlush) && (int)charFallback > 0)
                {
                    encodingByteBuffer.Fallback(charFallback);
                    charFallback = char.MinValue;
                }
                else
                {
                    break;
                }
            }
            if (encoder != null)
            {
                if ((IntPtr)bytes != IntPtr.Zero)
                {
                    encoder.charLeftOver = charFallback;
                }
                encoder.m_charsUsed = encodingByteBuffer.CharsUsed;
            }
            return(encodingByteBuffer.Count);
        }
        internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS baseEncoder)
        {
            ISCIIEncoder inEncoder = (ISCIIEncoder)baseEncoder;

            Encoding.EncodingByteBuffer buffer = new Encoding.EncodingByteBuffer(this, inEncoder, bytes, byteCount, chars, charCount);
            int  defaultCodePage = this.defaultCodePage;
            bool bLastVirama     = false;

            if (inEncoder != null)
            {
                defaultCodePage = inEncoder.currentCodePage;
                bLastVirama     = inEncoder.bLastVirama;
                if (inEncoder.charLeftOver > '\0')
                {
                    buffer.Fallback(inEncoder.charLeftOver);
                    bLastVirama = false;
                }
            }
            while (buffer.MoreData)
            {
                char nextChar = buffer.GetNextChar();
                if (nextChar < '\x00a0')
                {
                    if (!buffer.AddByte((byte)nextChar))
                    {
                        break;
                    }
                    bLastVirama = false;
                    continue;
                }
                if ((nextChar < 'ँ') || (nextChar > '൯'))
                {
                    if (bLastVirama && ((nextChar == '‌') || (nextChar == '‍')))
                    {
                        if (nextChar == '‌')
                        {
                            if (!buffer.AddByte(0xe8))
                            {
                                break;
                            }
                        }
                        else if (!buffer.AddByte(0xe9))
                        {
                            break;
                        }
                        bLastVirama = false;
                        continue;
                    }
                    buffer.Fallback(nextChar);
                    bLastVirama = false;
                    continue;
                }
                int  num2 = UnicodeToIndicChar[nextChar - 'ँ'];
                byte b    = (byte)num2;
                int  num4 = 15 & (num2 >> 8);
                int  num5 = 0xf000 & num2;
                if (num2 == 0)
                {
                    buffer.Fallback(nextChar);
                    bLastVirama = false;
                }
                else
                {
                    if (num4 != defaultCodePage)
                    {
                        if (!buffer.AddByte(0xef, (byte)(num4 | 0x40)))
                        {
                            break;
                        }
                        defaultCodePage = num4;
                    }
                    if (!buffer.AddByte(b, (num5 != 0) ? 1 : 0))
                    {
                        break;
                    }
                    bLastVirama = b == 0xe8;
                    if ((num5 != 0) && !buffer.AddByte(SecondIndicByte[num5 >> 12]))
                    {
                        break;
                    }
                }
            }
            if ((defaultCodePage != this.defaultCodePage) && ((inEncoder == null) || inEncoder.MustFlush))
            {
                if (buffer.AddByte(0xef, (byte)(this.defaultCodePage | 0x40)))
                {
                    defaultCodePage = this.defaultCodePage;
                }
                else
                {
                    buffer.GetNextChar();
                }
                bLastVirama = false;
            }
            if ((inEncoder != null) && (bytes != null))
            {
                if (!buffer.fallbackBuffer.bUsedEncoder)
                {
                    inEncoder.charLeftOver = '\0';
                }
                inEncoder.currentCodePage = defaultCodePage;
                inEncoder.bLastVirama     = bLastVirama;
                inEncoder.m_charsUsed     = buffer.CharsUsed;
            }
            return(buffer.Count);
        }
Exemple #5
0
        internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS baseEncoder)
        {
            Encoder inEncoder = (Encoder)baseEncoder;
            int     bits      = 0;
            int     bitCount  = -1;

            Encoding.EncodingByteBuffer buffer = new Encoding.EncodingByteBuffer(this, inEncoder, bytes, byteCount, chars, charCount);
            if (inEncoder != null)
            {
                bits     = inEncoder.bits;
                bitCount = inEncoder.bitCount;
                while (bitCount >= 6)
                {
                    bitCount -= 6;
                    if (!buffer.AddByte(this.base64Bytes[(bits >> bitCount) & 0x3f]))
                    {
                        base.ThrowBytesOverflow(inEncoder, buffer.Count == 0);
                    }
                }
            }
            while (buffer.MoreData)
            {
                char nextChar = buffer.GetNextChar();
                if ((nextChar < '\x0080') && this.directEncode[nextChar])
                {
                    if (bitCount >= 0)
                    {
                        if (bitCount > 0)
                        {
                            if (!buffer.AddByte(this.base64Bytes[(bits << (6 - bitCount)) & 0x3f]))
                            {
                                break;
                            }
                            bitCount = 0;
                        }
                        if (!buffer.AddByte(0x2d))
                        {
                            break;
                        }
                        bitCount = -1;
                    }
                    if (buffer.AddByte((byte)nextChar))
                    {
                        continue;
                    }
                    break;
                }
                if ((bitCount < 0) && (nextChar == '+'))
                {
                    if (buffer.AddByte(0x2b, (byte)0x2d))
                    {
                        continue;
                    }
                    break;
                }
                if (bitCount < 0)
                {
                    if (!buffer.AddByte(0x2b))
                    {
                        break;
                    }
                    bitCount = 0;
                }
                bits      = (bits << 0x10) | nextChar;
                bitCount += 0x10;
                while (bitCount >= 6)
                {
                    bitCount -= 6;
                    if (!buffer.AddByte(this.base64Bytes[(bits >> bitCount) & 0x3f]))
                    {
                        bitCount += 6;
                        nextChar  = buffer.GetNextChar();
                        break;
                    }
                }
                if (bitCount >= 6)
                {
                    break;
                }
            }
            if ((bitCount >= 0) && ((inEncoder == null) || inEncoder.MustFlush))
            {
                if ((bitCount > 0) && buffer.AddByte(this.base64Bytes[(bits << (6 - bitCount)) & 0x3f]))
                {
                    bitCount = 0;
                }
                if (buffer.AddByte(0x2d))
                {
                    bits     = 0;
                    bitCount = -1;
                }
                else
                {
                    buffer.GetNextChar();
                }
            }
            if ((bytes != null) && (inEncoder != null))
            {
                inEncoder.bits        = bits;
                inEncoder.bitCount    = bitCount;
                inEncoder.m_charsUsed = buffer.CharsUsed;
            }
            return(buffer.Count);
        }
        private unsafe int GetBytesCP52936(char *chars, int charCount, byte *bytes, int byteCount, ISO2022Encoder encoder)
        {
            Encoding.EncodingByteBuffer buffer = new Encoding.EncodingByteBuffer(this, encoder, bytes, byteCount, chars, charCount);
            ISO2022Modes modeASCII             = ISO2022Modes.ModeASCII;

            if (encoder != null)
            {
                char charLeftOver = encoder.charLeftOver;
                modeASCII = encoder.currentMode;
                if (charLeftOver > '\0')
                {
                    buffer.Fallback(charLeftOver);
                }
            }
            while (buffer.MoreData)
            {
                char   nextChar = buffer.GetNextChar();
                ushort num      = base.mapUnicodeToBytes[nextChar];
                if ((num == 0) && (nextChar != '\0'))
                {
                    buffer.Fallback(nextChar);
                }
                else
                {
                    byte num2 = (byte)(num >> 8);
                    byte num3 = (byte)(num & 0xff);
                    if (((num2 != 0) && (((num2 < 0xa1) || (num2 > 0xf7)) || ((num3 < 0xa1) || (num3 > 0xfe)))) || (((num2 == 0) && (num3 > 0x80)) && (num3 != 0xff)))
                    {
                        buffer.Fallback(nextChar);
                        continue;
                    }
                    if (num2 != 0)
                    {
                        if (modeASCII != ISO2022Modes.ModeHZ)
                        {
                            if (!buffer.AddByte(0x7e, 0x7b, 2))
                            {
                                break;
                            }
                            modeASCII = ISO2022Modes.ModeHZ;
                        }
                        if (buffer.AddByte((byte)(num2 & 0x7f), (byte)(num3 & 0x7f)))
                        {
                            continue;
                        }
                        break;
                    }
                    if (modeASCII != ISO2022Modes.ModeASCII)
                    {
                        if (!buffer.AddByte(0x7e, 0x7d, (num3 == 0x7e) ? 2 : 1))
                        {
                            break;
                        }
                        modeASCII = ISO2022Modes.ModeASCII;
                    }
                    if (((num3 == 0x7e) && !buffer.AddByte(0x7e, 1)) || !buffer.AddByte(num3))
                    {
                        break;
                    }
                }
            }
            if ((modeASCII != ISO2022Modes.ModeASCII) && ((encoder == null) || encoder.MustFlush))
            {
                if (buffer.AddByte(0x7e, (byte)0x7d))
                {
                    modeASCII = ISO2022Modes.ModeASCII;
                }
                else
                {
                    buffer.GetNextChar();
                }
            }
            if ((encoder != null) && (bytes != null))
            {
                encoder.currentMode = modeASCII;
                if (!buffer.fallbackBuffer.bUsedEncoder)
                {
                    encoder.charLeftOver = '\0';
                }
                encoder.m_charsUsed = buffer.CharsUsed;
            }
            return(buffer.Count);
        }
        private unsafe int GetBytesCP5022xJP(char *chars, int charCount, byte *bytes, int byteCount, ISO2022Encoder encoder)
        {
            Encoding.EncodingByteBuffer buffer = new Encoding.EncodingByteBuffer(this, encoder, bytes, byteCount, chars, charCount);
            ISO2022Modes modeASCII             = ISO2022Modes.ModeASCII;
            ISO2022Modes shiftInOutMode        = ISO2022Modes.ModeASCII;

            if (encoder != null)
            {
                char charLeftOver = encoder.charLeftOver;
                modeASCII      = encoder.currentMode;
                shiftInOutMode = encoder.shiftInOutMode;
                if (charLeftOver > '\0')
                {
                    buffer.Fallback(charLeftOver);
                }
            }
            while (buffer.MoreData)
            {
                byte   num2;
                byte   num3;
                char   nextChar = buffer.GetNextChar();
                ushort num      = base.mapUnicodeToBytes[nextChar];
                while (true)
                {
                    num2 = (byte)(num >> 8);
                    num3 = (byte)(num & 0xff);
                    if (num2 != 0x10)
                    {
                        goto Label_010A;
                    }
                    if (this.CodePage != 0xc42c)
                    {
                        break;
                    }
                    if ((num3 < 0x21) || (num3 >= (0x21 + HalfToFullWidthKanaTable.Length)))
                    {
                        buffer.Fallback(nextChar);
                        continue;
                    }
                    num = (ushort)(HalfToFullWidthKanaTable[num3 - 0x21] & 0x7f7f);
                }
                if (modeASCII != ISO2022Modes.ModeHalfwidthKatakana)
                {
                    if (this.CodePage == 0xc42e)
                    {
                        if (!buffer.AddByte(14))
                        {
                            break;
                        }
                        shiftInOutMode = modeASCII;
                        modeASCII      = ISO2022Modes.ModeHalfwidthKatakana;
                    }
                    else
                    {
                        if (!buffer.AddByte(0x1b, 40, (byte)0x49))
                        {
                            break;
                        }
                        modeASCII = ISO2022Modes.ModeHalfwidthKatakana;
                    }
                }
                if (buffer.AddByte((byte)(num3 & 0x7f)))
                {
                    continue;
                }
                break;
Label_010A:
                if (num2 != 0)
                {
                    if ((this.CodePage == 0xc42e) && (modeASCII == ISO2022Modes.ModeHalfwidthKatakana))
                    {
                        if (!buffer.AddByte(15))
                        {
                            break;
                        }
                        modeASCII = shiftInOutMode;
                    }
                    if (modeASCII != ISO2022Modes.ModeJIS0208)
                    {
                        if (!buffer.AddByte(0x1b, 0x24, (byte)0x42))
                        {
                            break;
                        }
                        modeASCII = ISO2022Modes.ModeJIS0208;
                    }
                    if (buffer.AddByte(num2, num3))
                    {
                        continue;
                    }
                    break;
                }
                if ((num != 0) || (nextChar == '\0'))
                {
                    if ((this.CodePage == 0xc42e) && (modeASCII == ISO2022Modes.ModeHalfwidthKatakana))
                    {
                        if (!buffer.AddByte(15))
                        {
                            break;
                        }
                        modeASCII = shiftInOutMode;
                    }
                    if (modeASCII != ISO2022Modes.ModeASCII)
                    {
                        if (!buffer.AddByte(0x1b, 40, (byte)0x42))
                        {
                            break;
                        }
                        modeASCII = ISO2022Modes.ModeASCII;
                    }
                    if (buffer.AddByte(num3))
                    {
                        continue;
                    }
                    break;
                }
                buffer.Fallback(nextChar);
            }
            if ((modeASCII != ISO2022Modes.ModeASCII) && ((encoder == null) || encoder.MustFlush))
            {
                if ((this.CodePage == 0xc42e) && (modeASCII == ISO2022Modes.ModeHalfwidthKatakana))
                {
                    if (buffer.AddByte(15))
                    {
                        modeASCII = shiftInOutMode;
                    }
                    else
                    {
                        buffer.GetNextChar();
                    }
                }
                if ((modeASCII != ISO2022Modes.ModeASCII) && ((this.CodePage != 0xc42e) || (modeASCII != ISO2022Modes.ModeHalfwidthKatakana)))
                {
                    if (buffer.AddByte(0x1b, 40, (byte)0x42))
                    {
                        modeASCII = ISO2022Modes.ModeASCII;
                    }
                    else
                    {
                        buffer.GetNextChar();
                    }
                }
            }
            if ((bytes != null) && (encoder != null))
            {
                encoder.currentMode    = modeASCII;
                encoder.shiftInOutMode = shiftInOutMode;
                if (!buffer.fallbackBuffer.bUsedEncoder)
                {
                    encoder.charLeftOver = '\0';
                }
                encoder.m_charsUsed = buffer.CharsUsed;
            }
            return(buffer.Count);
        }
        private unsafe int GetBytesCP50225KR(char *chars, int charCount, byte *bytes, int byteCount, ISO2022Encoder encoder)
        {
            Encoding.EncodingByteBuffer buffer = new Encoding.EncodingByteBuffer(this, encoder, bytes, byteCount, chars, charCount);
            ISO2022Modes modeASCII             = ISO2022Modes.ModeASCII;
            ISO2022Modes shiftInOutMode        = ISO2022Modes.ModeASCII;

            if (encoder != null)
            {
                char charLeftOver = encoder.charLeftOver;
                modeASCII      = encoder.currentMode;
                shiftInOutMode = encoder.shiftInOutMode;
                if (charLeftOver > '\0')
                {
                    buffer.Fallback(charLeftOver);
                }
            }
            while (buffer.MoreData)
            {
                char   nextChar = buffer.GetNextChar();
                ushort num      = base.mapUnicodeToBytes[nextChar];
                byte   num2     = (byte)(num >> 8);
                byte   num3     = (byte)(num & 0xff);
                if (num2 != 0)
                {
                    if (shiftInOutMode != ISO2022Modes.ModeKR)
                    {
                        if (!buffer.AddByte(0x1b, 0x24, 0x29, (byte)0x43))
                        {
                            break;
                        }
                        shiftInOutMode = ISO2022Modes.ModeKR;
                    }
                    if (modeASCII != ISO2022Modes.ModeKR)
                    {
                        if (!buffer.AddByte(14))
                        {
                            break;
                        }
                        modeASCII = ISO2022Modes.ModeKR;
                    }
                    if (buffer.AddByte(num2, num3))
                    {
                        continue;
                    }
                    break;
                }
                if ((num != 0) || (nextChar == '\0'))
                {
                    if (modeASCII != ISO2022Modes.ModeASCII)
                    {
                        if (!buffer.AddByte(15))
                        {
                            break;
                        }
                        modeASCII = ISO2022Modes.ModeASCII;
                    }
                    if (buffer.AddByte(num3))
                    {
                        continue;
                    }
                    break;
                }
                buffer.Fallback(nextChar);
            }
            if ((modeASCII != ISO2022Modes.ModeASCII) && ((encoder == null) || encoder.MustFlush))
            {
                if (buffer.AddByte(15))
                {
                    modeASCII = ISO2022Modes.ModeASCII;
                }
                else
                {
                    buffer.GetNextChar();
                }
            }
            if ((bytes != null) && (encoder != null))
            {
                if (!buffer.fallbackBuffer.bUsedEncoder)
                {
                    encoder.charLeftOver = '\0';
                }
                encoder.currentMode = modeASCII;
                if (!encoder.MustFlush || (encoder.charLeftOver != '\0'))
                {
                    encoder.shiftInOutMode = shiftInOutMode;
                }
                else
                {
                    encoder.shiftInOutMode = ISO2022Modes.ModeASCII;
                }
                encoder.m_charsUsed = buffer.CharsUsed;
            }
            return(buffer.Count);
        }
        internal unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder)
        {
            char c = '\0';

            if (encoder != null)
            {
                c = encoder.charLeftOver;
            }
            Encoding.EncodingByteBuffer encodingByteBuffer = new Encoding.EncodingByteBuffer(this, encoder, bytes, byteCount, chars, charCount);
            for (;;)
            {
                if (encodingByteBuffer.MoreData)
                {
                    char nextChar = encodingByteBuffer.GetNextChar();
                    if (c != '\0')
                    {
                        if (!char.IsLowSurrogate(nextChar))
                        {
                            encodingByteBuffer.MovePrevious(false);
                            if (encodingByteBuffer.Fallback(c))
                            {
                                c = '\0';
                                continue;
                            }
                            c = '\0';
                        }
                        else
                        {
                            int  num = (int)(((int)(c - '\ud800') << 10) + (nextChar - '\udc00'));
                            byte b   = (byte)(num % 10 + 48);
                            num /= 10;
                            byte b2 = (byte)(num % 126 + 129);
                            num /= 126;
                            byte b3 = (byte)(num % 10 + 48);
                            num /= 10;
                            c    = '\0';
                            if (encodingByteBuffer.AddByte((byte)(num + 144), b3, b2, b))
                            {
                                c = '\0';
                                continue;
                            }
                            encodingByteBuffer.MovePrevious(false);
                        }
                    }
                    else if (nextChar <= '\u007f')
                    {
                        if (encodingByteBuffer.AddByte((byte)nextChar))
                        {
                            continue;
                        }
                    }
                    else
                    {
                        if (char.IsHighSurrogate(nextChar))
                        {
                            c = nextChar;
                            continue;
                        }
                        if (char.IsLowSurrogate(nextChar))
                        {
                            if (encodingByteBuffer.Fallback(nextChar))
                            {
                                continue;
                            }
                        }
                        else
                        {
                            ushort num2 = this.mapUnicodeToBytes[(IntPtr)nextChar];
                            if (this.Is4Byte(nextChar))
                            {
                                byte b4 = (byte)(num2 % 10 + 48);
                                num2 /= 10;
                                byte b5 = (byte)(num2 % 126 + 129);
                                num2 /= 126;
                                byte b6 = (byte)(num2 % 10 + 48);
                                num2 /= 10;
                                if (encodingByteBuffer.AddByte((byte)(num2 + 129), b6, b5, b4))
                                {
                                    continue;
                                }
                            }
                            else if (encodingByteBuffer.AddByte((byte)(num2 >> 8), (byte)(num2 & 255)))
                            {
                                continue;
                            }
                        }
                    }
                }
                if ((encoder != null && !encoder.MustFlush) || c <= '\0')
                {
                    break;
                }
                encodingByteBuffer.Fallback(c);
                c = '\0';
            }
            if (encoder != null)
            {
                if (bytes != null)
                {
                    encoder.charLeftOver = c;
                }
                encoder.m_charsUsed = encodingByteBuffer.CharsUsed;
            }
            return(encodingByteBuffer.Count);
        }
Exemple #10
0
        internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder)
        {
            char charFallback = '\0';

            if (encoder != null)
            {
                charFallback = encoder.charLeftOver;
            }
            Encoding.EncodingByteBuffer buffer = new Encoding.EncodingByteBuffer(this, encoder, bytes, byteCount, chars, charCount);
Label_0183:
            while (buffer.MoreData)
            {
                char nextChar = buffer.GetNextChar();
                if (charFallback != '\0')
                {
                    if (!char.IsLowSurrogate(nextChar))
                    {
                        buffer.MovePrevious(false);
                        if (!buffer.Fallback(charFallback))
                        {
                            charFallback = '\0';
                            break;
                        }
                        charFallback = '\0';
                    }
                    else
                    {
                        int  num  = ((charFallback - 0xd800) << 10) + (nextChar - 0xdc00);
                        byte num2 = (byte)((num % 10) + 0x30);
                        num /= 10;
                        byte num3 = (byte)((num % 0x7e) + 0x81);
                        num /= 0x7e;
                        byte num4 = (byte)((num % 10) + 0x30);
                        num         /= 10;
                        charFallback = '\0';
                        if (!buffer.AddByte((byte)(num + 0x90), num4, num3, num2))
                        {
                            buffer.MovePrevious(false);
                            break;
                        }
                        charFallback = '\0';
                    }
                }
                else
                {
                    if (nextChar <= '\x007f')
                    {
                        if (buffer.AddByte((byte)nextChar))
                        {
                            continue;
                        }
                        break;
                    }
                    if (char.IsHighSurrogate(nextChar))
                    {
                        charFallback = nextChar;
                    }
                    else
                    {
                        if (char.IsLowSurrogate(nextChar))
                        {
                            if (buffer.Fallback(nextChar))
                            {
                                continue;
                            }
                            break;
                        }
                        ushort num5 = base.mapUnicodeToBytes[nextChar];
                        if (this.Is4Byte(nextChar))
                        {
                            byte num6 = (byte)((num5 % 10) + 0x30);
                            num5 = (ushort)(num5 / 10);
                            byte num7 = (byte)((num5 % 0x7e) + 0x81);
                            num5 = (ushort)(num5 / 0x7e);
                            byte num8 = (byte)((num5 % 10) + 0x30);
                            num5 = (ushort)(num5 / 10);
                            if (buffer.AddByte((byte)(num5 + 0x81), num8, num7, num6))
                            {
                                continue;
                            }
                            break;
                        }
                        if (!buffer.AddByte((byte)(num5 >> 8), (byte)(num5 & 0xff)))
                        {
                            break;
                        }
                    }
                }
            }
            if (((encoder == null) || encoder.MustFlush) && (charFallback > '\0'))
            {
                buffer.Fallback(charFallback);
                charFallback = '\0';
                goto Label_0183;
            }
            if (encoder != null)
            {
                if (bytes != null)
                {
                    encoder.charLeftOver = charFallback;
                }
                encoder.m_charsUsed = buffer.CharsUsed;
            }
            return(buffer.Count);
        }