Пример #1
0
        internal override unsafe int GetChars(byte *bytes, int byteCount, char *chars, int charCount, DecoderNLS baseDecoder)
        {
            UTF32Encoding.UTF32Decoder utF32Decoder = (UTF32Encoding.UTF32Decoder)baseDecoder;
            char *chPtr1  = chars;
            char *chPtr2  = chars + charCount;
            byte *numPtr1 = bytes;
            byte *numPtr2 = bytes + byteCount;
            int   length  = 0;
            uint  iChar   = 0;
            DecoderFallbackBuffer fallbackBuffer;

            if (utF32Decoder != null)
            {
                length         = utF32Decoder.readByteCount;
                iChar          = (uint)utF32Decoder.iChar;
                fallbackBuffer = baseDecoder.FallbackBuffer;
            }
            else
            {
                fallbackBuffer = this.decoderFallback.CreateFallbackBuffer();
            }
            fallbackBuffer.InternalInitialize(bytes, chars + charCount);
            while (bytes < numPtr2)
            {
                iChar = !this.bigEndian ? (iChar >> 8) + ((uint)*bytes++ << 24) : (iChar << 8) + (uint)*bytes++;
                ++length;
                if (length >= 4)
                {
                    length = 0;
                    if (iChar > 1114111U || iChar >= 55296U && iChar <= 57343U)
                    {
                        byte[] bytes1;
                        if (this.bigEndian)
                        {
                            bytes1 = new byte[4]
                            {
                                (byte)(iChar >> 24),
                                (byte)(iChar >> 16),
                                (byte)(iChar >> 8),
                                (byte)iChar
                            }
                        }
                        ;
                        else
                        {
                            bytes1 = new byte[4]
                            {
                                (byte)iChar,
                                (byte)(iChar >> 8),
                                (byte)(iChar >> 16),
                                (byte)(iChar >> 24)
                            }
                        };
                        if (!fallbackBuffer.InternalFallback(bytes1, bytes, ref chars))
                        {
                            bytes -= 4;
                            iChar  = 0U;
                            fallbackBuffer.InternalReset();
                            this.ThrowCharsOverflow((DecoderNLS)utF32Decoder, chars == chPtr1);
                            break;
                        }
                        iChar = 0U;
                    }
                    else
                    {
                        if (iChar >= 65536U)
                        {
                            if ((UIntPtr)chars >= (UIntPtr)chPtr2 - new UIntPtr(2))
                            {
                                bytes -= 4;
                                iChar  = 0U;
                                this.ThrowCharsOverflow((DecoderNLS)utF32Decoder, chars == chPtr1);
                                break;
                            }
                            *chars++ = this.GetHighSurrogate(iChar);
                            iChar = (uint)this.GetLowSurrogate(iChar);
                        }
                        else if (chars >= chPtr2)
                        {
                            bytes -= 4;
                            iChar  = 0U;
                            this.ThrowCharsOverflow((DecoderNLS)utF32Decoder, chars == chPtr1);
                            break;
                        }
                        *chars++ = (char)iChar;
                        iChar = 0U;
                    }
                }
            }
            if (length > 0 && (utF32Decoder == null || utF32Decoder.MustFlush))
            {
                byte[] bytes1 = new byte[length];
                int    num    = length;
                if (this.bigEndian)
                {
                    while (num > 0)
                    {
                        bytes1[--num] = (byte)iChar;
                        iChar       >>= 8;
                    }
                }
                else
                {
                    while (num > 0)
                    {
                        bytes1[--num] = (byte)(iChar >> 24);
                        iChar       <<= 8;
                    }
                }
                if (!fallbackBuffer.InternalFallback(bytes1, bytes, ref chars))
                {
                    fallbackBuffer.InternalReset();
                    this.ThrowCharsOverflow((DecoderNLS)utF32Decoder, chars == chPtr1);
                }
                else
                {
                    length = 0;
                    iChar  = 0U;
                }
            }
            if (utF32Decoder != null)
            {
                utF32Decoder.iChar         = (int)iChar;
                utF32Decoder.readByteCount = length;
                utF32Decoder.m_bytesUsed   = (int)(bytes - numPtr1);
            }
            return((int)(chars - chPtr1));
        }
Пример #2
0
        internal override unsafe int GetCharCount(byte *bytes, int count, DecoderNLS baseDecoder)
        {
            UTF32Encoding.UTF32Decoder utF32Decoder = (UTF32Encoding.UTF32Decoder)baseDecoder;
            int   num1      = 0;
            byte *numPtr    = bytes + count;
            byte *byteStart = bytes;
            int   length    = 0;
            uint  num2      = 0;
            DecoderFallbackBuffer fallbackBuffer;

            if (utF32Decoder != null)
            {
                length         = utF32Decoder.readByteCount;
                num2           = (uint)utF32Decoder.iChar;
                fallbackBuffer = utF32Decoder.FallbackBuffer;
            }
            else
            {
                fallbackBuffer = this.decoderFallback.CreateFallbackBuffer();
            }
            fallbackBuffer.InternalInitialize(byteStart, (char *)null);
            while (bytes < numPtr && num1 >= 0)
            {
                num2 = !this.bigEndian ? (num2 >> 8) + ((uint)*bytes++ << 24) : (num2 << 8) + (uint)*bytes++;
                ++length;
                if (length >= 4)
                {
                    length = 0;
                    if (num2 > 1114111U || num2 >= 55296U && num2 <= 57343U)
                    {
                        byte[] bytes1;
                        if (this.bigEndian)
                        {
                            bytes1 = new byte[4]
                            {
                                (byte)(num2 >> 24),
                                (byte)(num2 >> 16),
                                (byte)(num2 >> 8),
                                (byte)num2
                            }
                        }
                        ;
                        else
                        {
                            bytes1 = new byte[4]
                            {
                                (byte)num2,
                                (byte)(num2 >> 8),
                                (byte)(num2 >> 16),
                                (byte)(num2 >> 24)
                            }
                        };
                        num1 += fallbackBuffer.InternalFallback(bytes1, bytes);
                        num2  = 0U;
                    }
                    else
                    {
                        if (num2 >= 65536U)
                        {
                            ++num1;
                        }
                        ++num1;
                        num2 = 0U;
                    }
                }
            }
            if (length > 0 && (utF32Decoder == null || utF32Decoder.MustFlush))
            {
                byte[] bytes1 = new byte[length];
                if (this.bigEndian)
                {
                    while (length > 0)
                    {
                        bytes1[--length] = (byte)num2;
                        num2           >>= 8;
                    }
                }
                else
                {
                    while (length > 0)
                    {
                        bytes1[--length] = (byte)(num2 >> 24);
                        num2           <<= 8;
                    }
                }
                num1 += fallbackBuffer.InternalFallback(bytes1, bytes);
            }
            if (num1 < 0)
            {
                throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_GetByteCountOverflow"));
            }
            return(num1);
        }