InternalInitialize() приватный Метод

private InternalInitialize ( byte byteStart, char charEnd ) : void
byteStart byte
charEnd char
Результат void
Пример #1
0
        internal override unsafe int GetCharCount(byte *bytes, int count, DecoderNLS decoder)
        {
            DecoderReplacementFallback replacementFallback = decoder != null ? decoder.Fallback as DecoderReplacementFallback : this.DecoderFallback as DecoderReplacementFallback;

            if (replacementFallback != null && replacementFallback.MaxCharCount == 1)
            {
                return(count);
            }
            DecoderFallbackBuffer decoderFallbackBuffer = (DecoderFallbackBuffer)null;
            int num1 = count;

            byte[] bytes1 = new byte[1];
            byte * numPtr = bytes + count;

            while (bytes < numPtr)
            {
                byte num2 = *bytes;
                ++bytes;
                if ((int)num2 >= 128)
                {
                    if (decoderFallbackBuffer == null)
                    {
                        decoderFallbackBuffer = decoder != null ? decoder.FallbackBuffer : this.DecoderFallback.CreateFallbackBuffer();
                        decoderFallbackBuffer.InternalInitialize(numPtr - count, (char *)null);
                    }
                    bytes1[0] = num2;
                    num1      = num1 - 1 + decoderFallbackBuffer.InternalFallback(bytes1, bytes);
                }
            }
            return(num1);
        }
Пример #2
0
        internal override unsafe int GetCharCount(byte *bytes, int count, DecoderNLS decoder)
        {
            base.CheckMemorySection();
            bool isMicrosoftBestFitFallback            = false;
            DecoderReplacementFallback decoderFallback = null;

            if (decoder == null)
            {
                decoderFallback            = base.DecoderFallback as DecoderReplacementFallback;
                isMicrosoftBestFitFallback = base.DecoderFallback.IsMicrosoftBestFitFallback;
            }
            else
            {
                decoderFallback            = decoder.Fallback as DecoderReplacementFallback;
                isMicrosoftBestFitFallback = decoder.Fallback.IsMicrosoftBestFitFallback;
            }
            if (isMicrosoftBestFitFallback || ((decoderFallback != null) && (decoderFallback.MaxCharCount == 1)))
            {
                return(count);
            }
            DecoderFallbackBuffer fallbackBuffer = null;
            int num = count;

            byte[] buffer2 = new byte[1];
            byte * numPtr  = bytes + count;

            while (bytes < numPtr)
            {
                char ch = this.mapBytesToUnicode[bytes[0]];
                bytes++;
                if (ch == 0xfffd)
                {
                    if (fallbackBuffer == null)
                    {
                        if (decoder == null)
                        {
                            fallbackBuffer = base.DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            fallbackBuffer = decoder.FallbackBuffer;
                        }
                        fallbackBuffer.InternalInitialize(numPtr - count, null);
                    }
                    buffer2[0] = *(bytes - 1);
                    num--;
                    num += fallbackBuffer.InternalFallback(buffer2, bytes);
                }
            }
            return(num);
        }
        internal unsafe override int GetCharCount(byte *bytes, int count, DecoderNLS decoder)
        {
            base.CheckMemorySection();
            DecoderReplacementFallback decoderReplacementFallback;
            bool isMicrosoftBestFitFallback;

            if (decoder == null)
            {
                decoderReplacementFallback = (base.DecoderFallback as DecoderReplacementFallback);
                isMicrosoftBestFitFallback = base.DecoderFallback.IsMicrosoftBestFitFallback;
            }
            else
            {
                decoderReplacementFallback = (decoder.Fallback as DecoderReplacementFallback);
                isMicrosoftBestFitFallback = decoder.Fallback.IsMicrosoftBestFitFallback;
            }
            if (isMicrosoftBestFitFallback || (decoderReplacementFallback != null && decoderReplacementFallback.MaxCharCount == 1))
            {
                return(count);
            }
            DecoderFallbackBuffer decoderFallbackBuffer = null;
            int num = count;

            byte[] array = new byte[1];
            byte * ptr   = bytes + count;

            while (bytes < ptr)
            {
                char c = this.mapBytesToUnicode[*bytes];
                bytes++;
                if (c == '�')
                {
                    if (decoderFallbackBuffer == null)
                    {
                        if (decoder == null)
                        {
                            decoderFallbackBuffer = base.DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            decoderFallbackBuffer = decoder.FallbackBuffer;
                        }
                        decoderFallbackBuffer.InternalInitialize(ptr - count, null);
                    }
                    array[0] = *(bytes - 1);
                    num--;
                    num += decoderFallbackBuffer.InternalFallback(array, bytes);
                }
            }
            return(num);
        }
        internal override unsafe int GetCharCount(byte *bytes, int count, DecoderNLS decoder)
        {
            DecoderReplacementFallback decoderFallback = null;

            if (decoder == null)
            {
                decoderFallback = base.DecoderFallback as DecoderReplacementFallback;
            }
            else
            {
                decoderFallback = decoder.Fallback as DecoderReplacementFallback;
            }
            if ((decoderFallback != null) && (decoderFallback.MaxCharCount == 1))
            {
                return(count);
            }
            DecoderFallbackBuffer fallbackBuffer = null;
            int num = count;

            byte[] buffer2 = new byte[1];
            byte * numPtr  = bytes + count;

            while (bytes < numPtr)
            {
                byte num2 = bytes[0];
                bytes++;
                if (num2 >= 0x80)
                {
                    if (fallbackBuffer == null)
                    {
                        if (decoder == null)
                        {
                            fallbackBuffer = base.DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            fallbackBuffer = decoder.FallbackBuffer;
                        }
                        fallbackBuffer.InternalInitialize(numPtr - count, null);
                    }
                    buffer2[0] = num2;
                    num--;
                    num += fallbackBuffer.InternalFallback(buffer2, bytes);
                }
            }
            return(num);
        }
Пример #5
0
        internal unsafe override int GetCharCount(byte *bytes, int count, DecoderNLS decoder)
        {
            DecoderReplacementFallback decoderReplacementFallback;

            if (decoder == null)
            {
                decoderReplacementFallback = (base.DecoderFallback as DecoderReplacementFallback);
            }
            else
            {
                decoderReplacementFallback = (decoder.Fallback as DecoderReplacementFallback);
            }
            if (decoderReplacementFallback != null && decoderReplacementFallback.MaxCharCount == 1)
            {
                return(count);
            }
            DecoderFallbackBuffer decoderFallbackBuffer = null;
            int num = count;

            byte[] array = new byte[1];
            byte * ptr   = bytes + count;

            while (bytes < ptr)
            {
                byte b = *bytes;
                bytes++;
                if (b >= 128)
                {
                    if (decoderFallbackBuffer == null)
                    {
                        if (decoder == null)
                        {
                            decoderFallbackBuffer = base.DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            decoderFallbackBuffer = decoder.FallbackBuffer;
                        }
                        decoderFallbackBuffer.InternalInitialize(ptr - count, null);
                    }
                    array[0] = b;
                    num--;
                    num += decoderFallbackBuffer.InternalFallback(array, bytes);
                }
            }
            return(num);
        }
Пример #6
0
        internal override unsafe int GetCharCount(byte *bytes, int count, DecoderNLS decoder)
        {
            this.CheckMemorySection();
            DecoderReplacementFallback replacementFallback;
            bool microsoftBestFitFallback;

            if (decoder == null)
            {
                replacementFallback      = this.DecoderFallback as DecoderReplacementFallback;
                microsoftBestFitFallback = this.DecoderFallback.IsMicrosoftBestFitFallback;
            }
            else
            {
                replacementFallback      = decoder.Fallback as DecoderReplacementFallback;
                microsoftBestFitFallback = decoder.Fallback.IsMicrosoftBestFitFallback;
            }
            if (microsoftBestFitFallback || replacementFallback != null && replacementFallback.MaxCharCount == 1)
            {
                return(count);
            }
            DecoderFallbackBuffer decoderFallbackBuffer = (DecoderFallbackBuffer)null;
            int num1 = count;

            byte[] bytes1 = new byte[1];
            byte * numPtr = bytes + count;

            while (bytes < numPtr)
            {
                int num2 = (int)this.mapBytesToUnicode[*bytes];
                ++bytes;
                int num3 = 65533;
                if (num2 == num3)
                {
                    if (decoderFallbackBuffer == null)
                    {
                        decoderFallbackBuffer = decoder != null ? decoder.FallbackBuffer : this.DecoderFallback.CreateFallbackBuffer();
                        decoderFallbackBuffer.InternalInitialize(numPtr - count, (char *)null);
                    }
                    bytes1[0] = *(bytes - 1);
                    num1      = num1 - 1 + decoderFallbackBuffer.InternalFallback(bytes1, bytes);
                }
            }
            return(num1);
        }
Пример #7
0
        internal override unsafe int GetChars(byte *bytes, int byteCount, char *chars, int charCount, DecoderNLS baseDecoder)
        {
            UTF32Decoder          decoder        = (UTF32Decoder)baseDecoder;
            char *                chPtr          = chars;
            char *                chPtr2         = chars + charCount;
            byte *                numPtr         = bytes;
            byte *                numPtr2        = bytes + byteCount;
            int                   readByteCount  = 0;
            uint                  iChar          = 0;
            DecoderFallbackBuffer fallbackBuffer = null;

            if (decoder != null)
            {
                readByteCount  = decoder.readByteCount;
                iChar          = (uint)decoder.iChar;
                fallbackBuffer = baseDecoder.FallbackBuffer;
            }
            else
            {
                fallbackBuffer = base.decoderFallback.CreateFallbackBuffer();
            }
            fallbackBuffer.InternalInitialize(bytes, chars + charCount);
            while (bytes < numPtr2)
            {
                if (this.bigEndian)
                {
                    iChar = iChar << 8;
                    bytes++;
                    iChar += bytes[0];
                }
                else
                {
                    iChar = iChar >> 8;
                    bytes++;
                    iChar += (uint)(bytes[0] << 0x18);
                }
                readByteCount++;
                if (readByteCount >= 4)
                {
                    readByteCount = 0;
                    if ((iChar > 0x10ffff) || ((iChar >= 0xd800) && (iChar <= 0xdfff)))
                    {
                        byte[] buffer2;
                        if (this.bigEndian)
                        {
                            buffer2 = new byte[] { (byte)(iChar >> 0x18), (byte)(iChar >> 0x10), (byte)(iChar >> 8), (byte)iChar };
                        }
                        else
                        {
                            buffer2 = new byte[] { (byte)iChar, (byte)(iChar >> 8), (byte)(iChar >> 0x10), (byte)(iChar >> 0x18) };
                        }
                        if (!fallbackBuffer.InternalFallback(buffer2, bytes, ref chars))
                        {
                            bytes -= 4;
                            iChar  = 0;
                            fallbackBuffer.InternalReset();
                            base.ThrowCharsOverflow(decoder, chars == chPtr);
                            break;
                        }
                        iChar = 0;
                    }
                    else
                    {
                        if (iChar >= 0x10000)
                        {
                            if (chars >= (chPtr2 - 1))
                            {
                                bytes -= 4;
                                iChar  = 0;
                                base.ThrowCharsOverflow(decoder, chars == chPtr);
                                break;
                            }
                            chars++;
                            chars[0] = this.GetHighSurrogate(iChar);
                            iChar    = this.GetLowSurrogate(iChar);
                        }
                        else if (chars >= chPtr2)
                        {
                            bytes -= 4;
                            iChar  = 0;
                            base.ThrowCharsOverflow(decoder, chars == chPtr);
                            break;
                        }
                        chars++;
                        chars[0] = (char)iChar;
                        iChar    = 0;
                    }
                }
            }
            if ((readByteCount > 0) && ((decoder == null) || decoder.MustFlush))
            {
                byte[] buffer3 = new byte[readByteCount];
                int    num3    = readByteCount;
                if (!this.bigEndian)
                {
                    while (num3 > 0)
                    {
                        buffer3[--num3] = (byte)(iChar >> 0x18);
                        iChar           = iChar << 8;
                    }
                }
                else
                {
                    while (num3 > 0)
                    {
                        buffer3[--num3] = (byte)iChar;
                        iChar           = iChar >> 8;
                    }
                }
                if (!fallbackBuffer.InternalFallback(buffer3, bytes, ref chars))
                {
                    fallbackBuffer.InternalReset();
                    base.ThrowCharsOverflow(decoder, chars == chPtr);
                }
                else
                {
                    readByteCount = 0;
                    iChar         = 0;
                }
            }
            if (decoder != null)
            {
                decoder.iChar         = (int)iChar;
                decoder.readByteCount = readByteCount;
                decoder.m_bytesUsed   = (int)((long)((bytes - numPtr) / 1));
            }
            return((int)((long)((chars - chPtr) / 2)));
        }
Пример #8
0
        internal override unsafe int GetCharCount(byte *bytes, int count, DecoderNLS baseDecoder)
        {
            UTF32Decoder          decoder        = (UTF32Decoder)baseDecoder;
            int                   num            = 0;
            byte *                numPtr         = bytes + count;
            byte *                byteStart      = bytes;
            int                   readByteCount  = 0;
            uint                  iChar          = 0;
            DecoderFallbackBuffer fallbackBuffer = null;

            if (decoder != null)
            {
                readByteCount  = decoder.readByteCount;
                iChar          = (uint)decoder.iChar;
                fallbackBuffer = decoder.FallbackBuffer;
            }
            else
            {
                fallbackBuffer = base.decoderFallback.CreateFallbackBuffer();
            }
            fallbackBuffer.InternalInitialize(byteStart, null);
            while ((bytes < numPtr) && (num >= 0))
            {
                if (this.bigEndian)
                {
                    iChar = iChar << 8;
                    bytes++;
                    iChar += bytes[0];
                }
                else
                {
                    iChar = iChar >> 8;
                    bytes++;
                    iChar += (uint)(bytes[0] << 0x18);
                }
                readByteCount++;
                if (readByteCount >= 4)
                {
                    readByteCount = 0;
                    if ((iChar > 0x10ffff) || ((iChar >= 0xd800) && (iChar <= 0xdfff)))
                    {
                        byte[] buffer2;
                        if (this.bigEndian)
                        {
                            buffer2 = new byte[] { (byte)(iChar >> 0x18), (byte)(iChar >> 0x10), (byte)(iChar >> 8), (byte)iChar };
                        }
                        else
                        {
                            buffer2 = new byte[] { (byte)iChar, (byte)(iChar >> 8), (byte)(iChar >> 0x10), (byte)(iChar >> 0x18) };
                        }
                        num  += fallbackBuffer.InternalFallback(buffer2, bytes);
                        iChar = 0;
                    }
                    else
                    {
                        if (iChar >= 0x10000)
                        {
                            num++;
                        }
                        num++;
                        iChar = 0;
                    }
                }
            }
            if ((readByteCount > 0) && ((decoder == null) || decoder.MustFlush))
            {
                byte[] buffer3 = new byte[readByteCount];
                if (!this.bigEndian)
                {
                    while (readByteCount > 0)
                    {
                        buffer3[--readByteCount] = (byte)(iChar >> 0x18);
                        iChar = iChar << 8;
                    }
                }
                else
                {
                    while (readByteCount > 0)
                    {
                        buffer3[--readByteCount] = (byte)iChar;
                        iChar = iChar >> 8;
                    }
                }
                num += fallbackBuffer.InternalFallback(buffer3, bytes);
            }
            if (num < 0)
            {
                throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_GetByteCountOverflow"));
            }
            return(num);
        }
Пример #9
0
        internal override unsafe int GetChars(byte *bytes, int byteCount, char *chars, int charCount, DecoderNLS baseDecoder)
        {
            base.CheckMemorySection();
            DBCSDecoder           decoder        = (DBCSDecoder)baseDecoder;
            byte *                numPtr         = bytes;
            byte *                numPtr2        = bytes + byteCount;
            char *                chPtr          = chars;
            char *                charEnd        = chars + charCount;
            bool                  flag           = false;
            DecoderFallbackBuffer fallbackBuffer = null;

            if ((decoder != null) && (decoder.bLeftOver > 0))
            {
                if (byteCount == 0)
                {
                    if (!decoder.MustFlush)
                    {
                        return(0);
                    }
                    fallbackBuffer = decoder.FallbackBuffer;
                    fallbackBuffer.InternalInitialize(bytes, charEnd);
                    byte[] buffer2 = new byte[] { decoder.bLeftOver };
                    if (!fallbackBuffer.InternalFallback(buffer2, bytes, ref chars))
                    {
                        base.ThrowCharsOverflow(decoder, true);
                    }
                    decoder.bLeftOver = 0;
                    return((int)((long)((chars - chPtr) / 2)));
                }
                int index = decoder.bLeftOver << 8;
                index |= bytes[0];
                bytes++;
                char ch = this.mapBytesToUnicode[index];
                if ((ch == '\0') && (index != 0))
                {
                    fallbackBuffer = decoder.FallbackBuffer;
                    fallbackBuffer.InternalInitialize(numPtr2 - byteCount, charEnd);
                    byte[] buffer3 = new byte[] { (byte)(index >> 8), (byte)index };
                    if (!fallbackBuffer.InternalFallback(buffer3, bytes, ref chars))
                    {
                        base.ThrowCharsOverflow(decoder, true);
                    }
                }
                else
                {
                    if (chars >= charEnd)
                    {
                        base.ThrowCharsOverflow(decoder, true);
                    }
                    chars++;
                    chars[0] = ch;
                }
            }
            while (bytes < numPtr2)
            {
                int num2 = bytes[0];
                bytes++;
                char ch2 = this.mapBytesToUnicode[num2];
                if (ch2 == 0xfffe)
                {
                    if (bytes < numPtr2)
                    {
                        num2  = num2 << 8;
                        num2 |= bytes[0];
                        bytes++;
                        ch2 = this.mapBytesToUnicode[num2];
                    }
                    else if ((decoder == null) || decoder.MustFlush)
                    {
                        ch2 = '\0';
                    }
                    else
                    {
                        flag = true;
                        decoder.bLeftOver = (byte)num2;
                        break;
                    }
                }
                if ((ch2 == '\0') && (num2 != 0))
                {
                    if (fallbackBuffer == null)
                    {
                        if (decoder == null)
                        {
                            fallbackBuffer = base.DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            fallbackBuffer = decoder.FallbackBuffer;
                        }
                        fallbackBuffer.InternalInitialize(numPtr2 - byteCount, charEnd);
                    }
                    byte[] buffer4 = null;
                    if (num2 < 0x100)
                    {
                        buffer4 = new byte[] { (byte)num2 };
                    }
                    else
                    {
                        buffer4 = new byte[] { (byte)(num2 >> 8), (byte)num2 };
                    }
                    if (fallbackBuffer.InternalFallback(buffer4, bytes, ref chars))
                    {
                        continue;
                    }
                    bytes -= buffer4.Length;
                    fallbackBuffer.InternalReset();
                    base.ThrowCharsOverflow(decoder, bytes == numPtr);
                    break;
                }
                if (chars >= charEnd)
                {
                    bytes--;
                    if (num2 >= 0x100)
                    {
                        bytes--;
                    }
                    base.ThrowCharsOverflow(decoder, bytes == numPtr);
                    break;
                }
                chars++;
                chars[0] = ch2;
            }
            if (decoder != null)
            {
                if (!flag)
                {
                    decoder.bLeftOver = 0;
                }
                decoder.m_bytesUsed = (int)((long)((bytes - numPtr) / 1));
            }
            return((int)((long)((chars - chPtr) / 2)));
        }
Пример #10
0
        internal override unsafe int GetChars(byte *bytes, int byteCount, char *chars, int charCount, DecoderNLS decoder)
        {
            this.CheckMemorySection();
            byte *numPtr1 = bytes + byteCount;
            byte *numPtr2 = bytes;
            char *chPtr   = chars;
            DecoderReplacementFallback replacementFallback;
            bool microsoftBestFitFallback;

            if (decoder == null)
            {
                replacementFallback      = this.DecoderFallback as DecoderReplacementFallback;
                microsoftBestFitFallback = this.DecoderFallback.IsMicrosoftBestFitFallback;
            }
            else
            {
                replacementFallback      = decoder.Fallback as DecoderReplacementFallback;
                microsoftBestFitFallback = decoder.Fallback.IsMicrosoftBestFitFallback;
            }
            if (microsoftBestFitFallback || replacementFallback != null && replacementFallback.MaxCharCount == 1)
            {
                char ch1 = replacementFallback != null ? replacementFallback.DefaultString[0] : '?';
                if (charCount < byteCount)
                {
                    this.ThrowCharsOverflow(decoder, charCount < 1);
                    numPtr1 = bytes + charCount;
                }
                while (bytes < numPtr1)
                {
                    char ch2;
                    if (microsoftBestFitFallback)
                    {
                        if (this.arrayBytesBestFit == null)
                        {
                            this.ReadBestFitTable();
                        }
                        ch2 = this.arrayBytesBestFit[(int)*bytes];
                    }
                    else
                    {
                        ch2 = this.mapBytesToUnicode[*bytes];
                    }
                    ++bytes;
                    *chars = (int)ch2 != 65533 ? ch2 : ch1;
                    chars += 2;
                }
                if (decoder != null)
                {
                    decoder.m_bytesUsed = (int)(bytes - numPtr2);
                }
                return((int)(chars - chPtr));
            }
            DecoderFallbackBuffer decoderFallbackBuffer = (DecoderFallbackBuffer)null;

            byte[] bytes1  = new byte[1];
            char * charEnd = chars + charCount;

            while (bytes < numPtr1)
            {
                char ch = this.mapBytesToUnicode[*bytes];
                ++bytes;
                if ((int)ch == 65533)
                {
                    if (decoderFallbackBuffer == null)
                    {
                        decoderFallbackBuffer = decoder != null ? decoder.FallbackBuffer : this.DecoderFallback.CreateFallbackBuffer();
                        decoderFallbackBuffer.InternalInitialize(numPtr1 - byteCount, charEnd);
                    }
                    bytes1[0] = *(bytes - 1);
                    if (!decoderFallbackBuffer.InternalFallback(bytes1, bytes, ref chars))
                    {
                        --bytes;
                        decoderFallbackBuffer.InternalReset();
                        this.ThrowCharsOverflow(decoder, bytes == numPtr2);
                        break;
                    }
                }
                else
                {
                    if (chars >= charEnd)
                    {
                        --bytes;
                        this.ThrowCharsOverflow(decoder, bytes == numPtr2);
                        break;
                    }
                    *chars = ch;
                    chars += 2;
                }
            }
            if (decoder != null)
            {
                decoder.m_bytesUsed = (int)(bytes - numPtr2);
            }
            return((int)(chars - chPtr));
        }
Пример #11
0
        // This is internal and called by something else,
        internal override unsafe int GetCharCount(byte *bytes, int count, DecoderNLS decoder)
        {
            // Just assert, we're called internally so these should be safe, checked already
            Debug.Assert(bytes != null, "[ASCIIEncoding.GetCharCount]bytes is null");
            Debug.Assert(count >= 0, "[ASCIIEncoding.GetCharCount]byteCount is negative");

            // ASCII doesn't do best fit, so don't have to check for it, find out which decoder fallback we're using
            DecoderReplacementFallback fallback = null;

            if (decoder == null)
            {
                fallback = this.DecoderFallback as DecoderReplacementFallback;
            }
            else
            {
                fallback = decoder.Fallback as DecoderReplacementFallback;
                Debug.Assert(!decoder.m_throwOnOverflow || !decoder.InternalHasFallbackBuffer ||
                             decoder.FallbackBuffer.Remaining == 0,
                             "[ASCIICodePageEncoding.GetCharCount]Expected empty fallback buffer");
            }

            if (fallback != null && fallback.MaxCharCount == 1)
            {
                // Just return length, SBCS stay the same length because they don't map to surrogate
                // pairs and we don't have a decoder fallback.

                return(count);
            }

            // Only need decoder fallback buffer if not using default replacement fallback, no best fit for ASCII
            DecoderFallbackBuffer fallbackBuffer = null;

            // Have to do it the hard way.
            // Assume charCount will be == count
            int charCount = count;

            byte[] byteBuffer = new byte[1];

            // Do it our fast way
            byte *byteEnd = bytes + count;

            // Quick loop
            while (bytes < byteEnd)
            {
                // Faster if don't use *bytes++;
                byte b = *bytes;
                bytes++;

                // If unknown we have to do fallback count
                if (b >= 0x80)
                {
                    if (fallbackBuffer == null)
                    {
                        if (decoder == null)
                        {
                            fallbackBuffer = this.DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            fallbackBuffer = decoder.FallbackBuffer;
                        }
                        fallbackBuffer.InternalInitialize(byteEnd - count, null);
                    }

                    // Use fallback buffer
                    byteBuffer[0] = b;
                    charCount--;            // Have to unreserve the one we already allocated for b
                    charCount += fallbackBuffer.InternalFallback(byteBuffer, bytes);
                }
            }

            // Fallback buffer must be empty
            Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0,
                         "[ASCIIEncoding.GetCharCount]Expected Empty fallback buffer");

            // Converted sequence is same length as input
            return(charCount);
        }
        [System.Security.SecurityCritical]  // auto-generated
        internal override unsafe int GetCharCount(byte *bytes, int count, DecoderNLS decoder)
        {
            // Just assert, we're called internally so these should be safe, checked already
            Contract.Assert(bytes != null, "[SBCSCodePageEncoding.GetCharCount]bytes is null");
            Contract.Assert(count >= 0, "[SBCSCodePageEncoding.GetCharCount]byteCount is negative");

            CheckMemorySection();

            // See if we have best fit
            bool bUseBestFit = false;

            // Only need decoder fallback buffer if not using default replacement fallback or best fit fallback.
            DecoderReplacementFallback fallback = null;

            if (decoder == null)
            {
                fallback    = this.DecoderFallback as DecoderReplacementFallback;
                bUseBestFit = this.DecoderFallback.IsMicrosoftBestFitFallback;
            }
            else
            {
                fallback    = decoder.Fallback as DecoderReplacementFallback;
                bUseBestFit = decoder.Fallback.IsMicrosoftBestFitFallback;
                Contract.Assert(!decoder.m_throwOnOverflow || !decoder.InternalHasFallbackBuffer ||
                                decoder.FallbackBuffer.Remaining == 0,
                                "[SBCSCodePageEncoding.GetChars]Expected empty fallback buffer at start");
            }

            if (bUseBestFit || (fallback != null && fallback.MaxCharCount == 1))
            {
                // Just return length, SBCS stay the same length because they don't map to surrogate
                // pairs and we don't have a decoder fallback.
                return(count);
            }

            // Might need one of these later
            DecoderFallbackBuffer fallbackBuffer = null;

            // Have to do it the hard way.
            // Assume charCount will be == count
            int charCount = count;

            byte[] byteBuffer = new byte[1];

            // Do it our fast way
            byte *byteEnd = bytes + count;

            // Quick loop
            while (bytes < byteEnd)
            {
                // Faster if don't use *bytes++;
                char c;
                c = mapBytesToUnicode[*bytes];
                bytes++;

                // If unknown we have to do fallback count
                if (c == UNKNOWN_CHAR)
                {
                    // Must have a fallback buffer
                    if (fallbackBuffer == null)
                    {
                        // Need to adjust count so we get real start
                        if (decoder == null)
                        {
                            fallbackBuffer = this.DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            fallbackBuffer = decoder.FallbackBuffer;
                        }
                        fallbackBuffer.InternalInitialize(byteEnd - count, null);
                    }

                    // Use fallback buffer
                    byteBuffer[0] = *(bytes - 1);
                    charCount--;                            // We'd already reserved one for *(bytes-1)
                    charCount += fallbackBuffer.InternalFallback(byteBuffer, bytes);
                }
            }

            // Fallback buffer must be empty
            Contract.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0,
                            "[SBCSEncoding.GetCharCount]Expected Empty fallback buffer at end");

            // Converted sequence is same length as input
            return(charCount);
        }
Пример #13
0
        internal override unsafe int GetCharCount(byte *bytes, int count, DecoderNLS baseDecoder)
        {
            Debug.Assert(bytes != null, "[UTF32Encoding.GetCharCount]bytes!=null");
            Debug.Assert(count >= 0, "[UTF32Encoding.GetCharCount]count >=0");

            UTF32Decoder decoder = (UTF32Decoder)baseDecoder;

            // None so far!
            int   charCount = 0;
            byte *end       = bytes + count;
            byte *byteStart = bytes;

            // Set up decoder
            int  readCount = 0;
            uint iChar     = 0;

            // For fallback we may need a fallback buffer
            DecoderFallbackBuffer fallbackBuffer = null;

            // See if there's anything in our decoder
            if (decoder != null)
            {
                readCount      = decoder.readByteCount;
                iChar          = (uint)decoder.iChar;
                fallbackBuffer = decoder.FallbackBuffer;

                // Shouldn't have anything in fallback buffer for GetCharCount
                // (don't have to check m_throwOnOverflow for chars or count)
                Debug.Assert(fallbackBuffer.Remaining == 0,
                             "[UTF32Encoding.GetCharCount]Expected empty fallback buffer at start");
            }
            else
            {
                fallbackBuffer = this.decoderFallback.CreateFallbackBuffer();
            }

            // Set our internal fallback interesting things.
            fallbackBuffer.InternalInitialize(byteStart, null);

            // Loop through our input, 4 characters at a time!
            while (bytes < end && charCount >= 0)
            {
                // Get our next character
                if (bigEndian)
                {
                    // Scoot left and add it to the bottom
                    iChar <<= 8;
                    iChar  += *(bytes++);
                }
                else
                {
                    // Scoot right and add it to the top
                    iChar >>= 8;
                    iChar  += (uint)(*(bytes++)) << 24;
                }

                readCount++;

                // See if we have all the bytes yet
                if (readCount < 4)
                {
                    continue;
                }

                // Have the bytes
                readCount = 0;

                // See if its valid to encode
                if (iChar > 0x10FFFF || (iChar >= 0xD800 && iChar <= 0xDFFF))
                {
                    // Need to fall back these 4 bytes
                    byte[] fallbackBytes;
                    if (bigEndian)
                    {
                        fallbackBytes = new byte[] {
                            unchecked ((byte)(iChar >> 24)), unchecked ((byte)(iChar >> 16)),
                            unchecked ((byte)(iChar >> 8)), unchecked ((byte)(iChar))
                        };
                    }
                    else
                    {
                        fallbackBytes = new byte[] {
                            unchecked ((byte)(iChar)), unchecked ((byte)(iChar >> 8)),
                            unchecked ((byte)(iChar >> 16)), unchecked ((byte)(iChar >> 24))
                        };
                    }

                    charCount += fallbackBuffer.InternalFallback(fallbackBytes, bytes);

                    // Ignore the illegal character
                    iChar = 0;
                    continue;
                }

                // Ok, we have something we can add to our output
                if (iChar >= 0x10000)
                {
                    // Surrogates take 2
                    charCount++;
                }

                // Add the rest of the surrogate or our normal character
                charCount++;

                // iChar is back to 0
                iChar = 0;
            }

            // See if we have something left over that has to be decoded
            if (readCount > 0 && (decoder == null || decoder.MustFlush))
            {
                // Oops, there's something left over with no place to go.
                byte[] fallbackBytes = new byte[readCount];
                if (bigEndian)
                {
                    while (readCount > 0)
                    {
                        fallbackBytes[--readCount] = unchecked ((byte)iChar);
                        iChar >>= 8;
                    }
                }
                else
                {
                    while (readCount > 0)
                    {
                        fallbackBytes[--readCount] = unchecked ((byte)(iChar >> 24));
                        iChar <<= 8;
                    }
                }

                charCount += fallbackBuffer.InternalFallback(fallbackBytes, bytes);
            }

            // Check for overflows.
            if (charCount < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count), Environment.GetResourceString("ArgumentOutOfRange_GetByteCountOverflow"));
            }

            // Shouldn't have anything in fallback buffer for GetCharCount
            // (don't have to check m_throwOnOverflow for chars or count)
            Debug.Assert(fallbackBuffer.Remaining == 0,
                         "[UTF32Encoding.GetCharCount]Expected empty fallback buffer at end");

            // Return our count
            return(charCount);
        }
Пример #14
0
        internal override unsafe int GetCharCount(byte *bytes, int count, DecoderNLS baseDecoder)
        {
            this.CheckMemorySection();
            DBCSCodePageEncoding.DBCSDecoder dbcsDecoder           = (DBCSCodePageEncoding.DBCSDecoder)baseDecoder;
            DecoderFallbackBuffer            decoderFallbackBuffer = (DecoderFallbackBuffer)null;
            byte *numPtr = bytes + count;
            int   num1   = count;

            if (dbcsDecoder != null && (int)dbcsDecoder.bLeftOver > 0)
            {
                if (count == 0)
                {
                    if (!dbcsDecoder.MustFlush)
                    {
                        return(0);
                    }
                    DecoderFallbackBuffer fallbackBuffer = dbcsDecoder.FallbackBuffer;
                    fallbackBuffer.InternalInitialize(bytes, (char *)null);
                    byte[] bytes1 = new byte[1] {
                        dbcsDecoder.bLeftOver
                    };
                    return(fallbackBuffer.InternalFallback(bytes1, bytes));
                }
                int index = (int)dbcsDecoder.bLeftOver << 8 | (int)*bytes;
                ++bytes;
                if ((int)this.mapBytesToUnicode[index] == 0 && index != 0)
                {
                    int num2 = num1 - 1;
                    decoderFallbackBuffer = dbcsDecoder.FallbackBuffer;
                    decoderFallbackBuffer.InternalInitialize(numPtr - count, (char *)null);
                    byte[] bytes1 = new byte[2] {
                        (byte)(index >> 8), (byte)index
                    };
                    num1 = num2 + decoderFallbackBuffer.InternalFallback(bytes1, bytes);
                }
            }
            while (bytes < numPtr)
            {
                int index = (int)*bytes;
                ++bytes;
                char ch = this.mapBytesToUnicode[index];
                if ((int)ch == 65534)
                {
                    --num1;
                    if (bytes < numPtr)
                    {
                        index = index << 8 | (int)*bytes;
                        ++bytes;
                        ch = this.mapBytesToUnicode[index];
                    }
                    else if (dbcsDecoder == null || dbcsDecoder.MustFlush)
                    {
                        ++num1;
                        ch = char.MinValue;
                    }
                    else
                    {
                        break;
                    }
                }
                if ((int)ch == 0 && index != 0)
                {
                    if (decoderFallbackBuffer == null)
                    {
                        decoderFallbackBuffer = dbcsDecoder != null ? dbcsDecoder.FallbackBuffer : this.DecoderFallback.CreateFallbackBuffer();
                        decoderFallbackBuffer.InternalInitialize(numPtr - count, (char *)null);
                    }
                    int    num2 = num1 - 1;
                    byte[] bytes1;
                    if (index < 256)
                    {
                        bytes1 = new byte[1] {
                            (byte)index
                        }
                    }
                    ;
                    else
                    {
                        bytes1 = new byte[2]
                        {
                            (byte)(index >> 8),
                            (byte)index
                        }
                    };
                    num1 = num2 + decoderFallbackBuffer.InternalFallback(bytes1, bytes);
                }
            }
            return(num1);
        }
        internal unsafe override int GetChars(byte *bytes, int byteCount, char *chars, int charCount, DecoderNLS decoder)
        {
            base.CheckMemorySection();
            byte *ptr  = bytes + byteCount;
            byte *ptr2 = bytes;
            char *ptr3 = chars;
            DecoderReplacementFallback decoderReplacementFallback;
            bool isMicrosoftBestFitFallback;

            if (decoder == null)
            {
                decoderReplacementFallback = (base.DecoderFallback as DecoderReplacementFallback);
                isMicrosoftBestFitFallback = base.DecoderFallback.IsMicrosoftBestFitFallback;
            }
            else
            {
                decoderReplacementFallback = (decoder.Fallback as DecoderReplacementFallback);
                isMicrosoftBestFitFallback = decoder.Fallback.IsMicrosoftBestFitFallback;
            }
            if (isMicrosoftBestFitFallback || (decoderReplacementFallback != null && decoderReplacementFallback.MaxCharCount == 1))
            {
                char c;
                if (decoderReplacementFallback == null)
                {
                    c = '?';
                }
                else
                {
                    c = decoderReplacementFallback.DefaultString[0];
                }
                if (charCount < byteCount)
                {
                    base.ThrowCharsOverflow(decoder, charCount < 1);
                    ptr = bytes + charCount;
                }
                while (bytes < ptr)
                {
                    char c2;
                    if (isMicrosoftBestFitFallback)
                    {
                        if (this.arrayBytesBestFit == null)
                        {
                            this.ReadBestFitTable();
                        }
                        c2 = this.arrayBytesBestFit[(int)(*bytes)];
                    }
                    else
                    {
                        c2 = this.mapBytesToUnicode[*bytes];
                    }
                    bytes++;
                    if (c2 == '�')
                    {
                        *chars = c;
                    }
                    else
                    {
                        *chars = c2;
                    }
                    chars++;
                }
                if (decoder != null)
                {
                    decoder.m_bytesUsed = (int)((long)(bytes - ptr2));
                }
                return((int)((long)(chars - ptr3)));
            }
            DecoderFallbackBuffer decoderFallbackBuffer = null;

            byte[] array = new byte[1];
            char * ptr4  = chars + charCount;

            while (bytes < ptr)
            {
                char c3 = this.mapBytesToUnicode[*bytes];
                bytes++;
                if (c3 == '�')
                {
                    if (decoderFallbackBuffer == null)
                    {
                        if (decoder == null)
                        {
                            decoderFallbackBuffer = base.DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            decoderFallbackBuffer = decoder.FallbackBuffer;
                        }
                        decoderFallbackBuffer.InternalInitialize(ptr - byteCount, ptr4);
                    }
                    array[0] = *(bytes - 1);
                    if (!decoderFallbackBuffer.InternalFallback(array, bytes, ref chars))
                    {
                        bytes--;
                        decoderFallbackBuffer.InternalReset();
                        base.ThrowCharsOverflow(decoder, bytes == ptr2);
                        break;
                    }
                }
                else
                {
                    if (chars >= ptr4)
                    {
                        bytes--;
                        base.ThrowCharsOverflow(decoder, bytes == ptr2);
                        break;
                    }
                    *chars = c3;
                    chars++;
                }
            }
            if (decoder != null)
            {
                decoder.m_bytesUsed = (int)((long)(bytes - ptr2));
            }
            return((int)((long)(chars - ptr3)));
        }
Пример #16
0
        internal override unsafe int GetChars(byte *bytes, int byteCount, char *chars, int charCount, DecoderNLS baseDecoder)
        {
            this.CheckMemorySection();
            DBCSCodePageEncoding.DBCSDecoder dbcsDecoder = (DBCSCodePageEncoding.DBCSDecoder)baseDecoder;
            byte *numPtr1 = bytes;
            byte *numPtr2 = bytes + byteCount;
            char *chPtr   = chars;
            char *charEnd = chars + charCount;
            bool  flag    = false;
            DecoderFallbackBuffer decoderFallbackBuffer = (DecoderFallbackBuffer)null;

            if (dbcsDecoder != null && (int)dbcsDecoder.bLeftOver > 0)
            {
                if (byteCount == 0)
                {
                    if (!dbcsDecoder.MustFlush)
                    {
                        return(0);
                    }
                    DecoderFallbackBuffer fallbackBuffer = dbcsDecoder.FallbackBuffer;
                    fallbackBuffer.InternalInitialize(bytes, charEnd);
                    byte[] bytes1 = new byte[1] {
                        dbcsDecoder.bLeftOver
                    };
                    if (!fallbackBuffer.InternalFallback(bytes1, bytes, ref chars))
                    {
                        this.ThrowCharsOverflow((DecoderNLS)dbcsDecoder, true);
                    }
                    dbcsDecoder.bLeftOver = (byte)0;
                    return((int)(chars - chPtr));
                }
                int index = (int)dbcsDecoder.bLeftOver << 8 | (int)*bytes;
                ++bytes;
                char ch = this.mapBytesToUnicode[index];
                if ((int)ch == 0 && index != 0)
                {
                    decoderFallbackBuffer = dbcsDecoder.FallbackBuffer;
                    decoderFallbackBuffer.InternalInitialize(numPtr2 - byteCount, charEnd);
                    byte[] bytes1 = new byte[2] {
                        (byte)(index >> 8), (byte)index
                    };
                    if (!decoderFallbackBuffer.InternalFallback(bytes1, bytes, ref chars))
                    {
                        this.ThrowCharsOverflow((DecoderNLS)dbcsDecoder, true);
                    }
                }
                else
                {
                    if (chars >= charEnd)
                    {
                        this.ThrowCharsOverflow((DecoderNLS)dbcsDecoder, true);
                    }
                    *chars++ = ch;
                }
            }
            while (bytes < numPtr2)
            {
                int index = (int)*bytes;
                ++bytes;
                char ch = this.mapBytesToUnicode[index];
                if ((int)ch == 65534)
                {
                    if (bytes < numPtr2)
                    {
                        index = index << 8 | (int)*bytes;
                        ++bytes;
                        ch = this.mapBytesToUnicode[index];
                    }
                    else if (dbcsDecoder == null || dbcsDecoder.MustFlush)
                    {
                        ch = char.MinValue;
                    }
                    else
                    {
                        flag = true;
                        dbcsDecoder.bLeftOver = (byte)index;
                        break;
                    }
                }
                if ((int)ch == 0 && index != 0)
                {
                    if (decoderFallbackBuffer == null)
                    {
                        decoderFallbackBuffer = dbcsDecoder != null ? dbcsDecoder.FallbackBuffer : this.DecoderFallback.CreateFallbackBuffer();
                        decoderFallbackBuffer.InternalInitialize(numPtr2 - byteCount, charEnd);
                    }
                    byte[] bytes1;
                    if (index < 256)
                    {
                        bytes1 = new byte[1] {
                            (byte)index
                        }
                    }
                    ;
                    else
                    {
                        bytes1 = new byte[2]
                        {
                            (byte)(index >> 8),
                            (byte)index
                        }
                    };
                    if (!decoderFallbackBuffer.InternalFallback(bytes1, bytes, ref chars))
                    {
                        bytes -= bytes1.Length;
                        decoderFallbackBuffer.InternalReset();
                        this.ThrowCharsOverflow((DecoderNLS)dbcsDecoder, bytes == numPtr1);
                        break;
                    }
                }
                else
                {
                    if (chars >= charEnd)
                    {
                        --bytes;
                        if (index >= 256)
                        {
                            --bytes;
                        }
                        this.ThrowCharsOverflow((DecoderNLS)dbcsDecoder, bytes == numPtr1);
                        break;
                    }
                    *chars++ = ch;
                }
            }
            if (dbcsDecoder != null)
            {
                if (!flag)
                {
                    dbcsDecoder.bLeftOver = (byte)0;
                }
                dbcsDecoder.m_bytesUsed = (int)(bytes - numPtr1);
            }
            return((int)(chars - chPtr));
        }
Пример #17
0
        internal unsafe override int GetChars(byte *bytes, int byteCount, char *chars, int charCount, DecoderNLS decoder)
        {
            byte *ptr  = bytes + byteCount;
            byte *ptr2 = bytes;
            char *ptr3 = chars;
            DecoderReplacementFallback decoderReplacementFallback;

            if (decoder == null)
            {
                decoderReplacementFallback = (base.DecoderFallback as DecoderReplacementFallback);
            }
            else
            {
                decoderReplacementFallback = (decoder.Fallback as DecoderReplacementFallback);
            }
            if (decoderReplacementFallback != null && decoderReplacementFallback.MaxCharCount == 1)
            {
                char c = decoderReplacementFallback.DefaultString[0];
                if (charCount < byteCount)
                {
                    base.ThrowCharsOverflow(decoder, charCount < 1);
                    ptr = bytes + charCount;
                }
                while (bytes < ptr)
                {
                    byte b = *(bytes++);
                    if (b >= 128)
                    {
                        *(chars++) = c;
                    }
                    else
                    {
                        *(chars++) = (char)b;
                    }
                }
                if (decoder != null)
                {
                    decoder.m_bytesUsed = (int)((long)(bytes - ptr2));
                }
                return((int)((long)(chars - ptr3)));
            }
            DecoderFallbackBuffer decoderFallbackBuffer = null;

            byte[] array = new byte[1];
            char * ptr4  = chars + charCount;

            while (bytes < ptr)
            {
                byte b2 = *bytes;
                bytes++;
                if (b2 >= 128)
                {
                    if (decoderFallbackBuffer == null)
                    {
                        if (decoder == null)
                        {
                            decoderFallbackBuffer = base.DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            decoderFallbackBuffer = decoder.FallbackBuffer;
                        }
                        decoderFallbackBuffer.InternalInitialize(ptr - byteCount, ptr4);
                    }
                    array[0] = b2;
                    if (!decoderFallbackBuffer.InternalFallback(array, bytes, ref chars))
                    {
                        bytes--;
                        decoderFallbackBuffer.InternalReset();
                        base.ThrowCharsOverflow(decoder, chars == ptr3);
                        break;
                    }
                }
                else
                {
                    if (chars >= ptr4)
                    {
                        bytes--;
                        base.ThrowCharsOverflow(decoder, chars == ptr3);
                        break;
                    }
                    *chars = (char)b2;
                    chars++;
                }
            }
            if (decoder != null)
            {
                decoder.m_bytesUsed = (int)((long)(bytes - ptr2));
            }
            return((int)((long)(chars - ptr3)));
        }
        internal unsafe override int GetChars(byte *bytes, int byteCount, char *chars, int charCount, DecoderNLS baseDecoder)
        {
            base.CheckMemorySection();
            DBCSCodePageEncoding.DBCSDecoder dbcsdecoder = (DBCSCodePageEncoding.DBCSDecoder)baseDecoder;
            byte *ptr  = bytes;
            byte *ptr2 = bytes + byteCount;
            char *ptr3 = chars;
            char *ptr4 = chars + charCount;
            bool  flag = false;
            DecoderFallbackBuffer decoderFallbackBuffer = null;

            if (dbcsdecoder != null && dbcsdecoder.bLeftOver > 0)
            {
                if (byteCount == 0)
                {
                    if (!dbcsdecoder.MustFlush)
                    {
                        return(0);
                    }
                    decoderFallbackBuffer = dbcsdecoder.FallbackBuffer;
                    decoderFallbackBuffer.InternalInitialize(bytes, ptr4);
                    byte[] bytes2 = new byte[]
                    {
                        dbcsdecoder.bLeftOver
                    };
                    if (!decoderFallbackBuffer.InternalFallback(bytes2, bytes, ref chars))
                    {
                        base.ThrowCharsOverflow(dbcsdecoder, true);
                    }
                    dbcsdecoder.bLeftOver = 0;
                    return((int)((long)(chars - ptr3)));
                }
                else
                {
                    int num = (int)dbcsdecoder.bLeftOver << 8;
                    num |= (int)(*bytes);
                    bytes++;
                    char c = this.mapBytesToUnicode[num];
                    if (c == '\0' && num != 0)
                    {
                        decoderFallbackBuffer = dbcsdecoder.FallbackBuffer;
                        decoderFallbackBuffer.InternalInitialize(ptr2 - byteCount, ptr4);
                        byte[] bytes3 = new byte[]
                        {
                            (byte)(num >> 8),
                            (byte)num
                        };
                        if (!decoderFallbackBuffer.InternalFallback(bytes3, bytes, ref chars))
                        {
                            base.ThrowCharsOverflow(dbcsdecoder, true);
                        }
                    }
                    else
                    {
                        if (chars >= ptr4)
                        {
                            base.ThrowCharsOverflow(dbcsdecoder, true);
                        }
                        *(chars++) = c;
                    }
                }
            }
            while (bytes < ptr2)
            {
                int num2 = (int)(*bytes);
                bytes++;
                char c2 = this.mapBytesToUnicode[num2];
                if (c2 == '￾')
                {
                    if (bytes < ptr2)
                    {
                        num2 <<= 8;
                        num2  |= (int)(*bytes);
                        bytes++;
                        c2 = this.mapBytesToUnicode[num2];
                    }
                    else
                    {
                        if (dbcsdecoder != null && !dbcsdecoder.MustFlush)
                        {
                            flag = true;
                            dbcsdecoder.bLeftOver = (byte)num2;
                            break;
                        }
                        c2 = '\0';
                    }
                }
                if (c2 == '\0' && num2 != 0)
                {
                    if (decoderFallbackBuffer == null)
                    {
                        if (dbcsdecoder == null)
                        {
                            decoderFallbackBuffer = base.DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            decoderFallbackBuffer = dbcsdecoder.FallbackBuffer;
                        }
                        decoderFallbackBuffer.InternalInitialize(ptr2 - byteCount, ptr4);
                    }
                    byte[] array;
                    if (num2 < 256)
                    {
                        array = new byte[]
                        {
                            (byte)num2
                        };
                    }
                    else
                    {
                        array = new byte[]
                        {
                            (byte)(num2 >> 8),
                            (byte)num2
                        };
                    }
                    if (!decoderFallbackBuffer.InternalFallback(array, bytes, ref chars))
                    {
                        bytes -= array.Length;
                        decoderFallbackBuffer.InternalReset();
                        base.ThrowCharsOverflow(dbcsdecoder, bytes == ptr);
                        break;
                    }
                }
                else
                {
                    if (chars >= ptr4)
                    {
                        bytes--;
                        if (num2 >= 256)
                        {
                            bytes--;
                        }
                        base.ThrowCharsOverflow(dbcsdecoder, bytes == ptr);
                        break;
                    }
                    *(chars++) = c2;
                }
            }
            if (dbcsdecoder != null)
            {
                if (!flag)
                {
                    dbcsdecoder.bLeftOver = 0;
                }
                dbcsdecoder.m_bytesUsed = (int)((long)(bytes - ptr));
            }
            return((int)((long)(chars - ptr3)));
        }
        internal unsafe override int GetCharCount(byte *bytes, int count, DecoderNLS baseDecoder)
        {
            base.CheckMemorySection();
            DBCSCodePageEncoding.DBCSDecoder dbcsdecoder           = (DBCSCodePageEncoding.DBCSDecoder)baseDecoder;
            DecoderFallbackBuffer            decoderFallbackBuffer = null;
            byte *ptr = bytes + count;
            int   num = count;

            if (dbcsdecoder != null && dbcsdecoder.bLeftOver > 0)
            {
                if (count == 0)
                {
                    if (!dbcsdecoder.MustFlush)
                    {
                        return(0);
                    }
                    decoderFallbackBuffer = dbcsdecoder.FallbackBuffer;
                    decoderFallbackBuffer.InternalInitialize(bytes, null);
                    byte[] bytes2 = new byte[]
                    {
                        dbcsdecoder.bLeftOver
                    };
                    return(decoderFallbackBuffer.InternalFallback(bytes2, bytes));
                }
                else
                {
                    int num2 = (int)dbcsdecoder.bLeftOver << 8;
                    num2 |= (int)(*bytes);
                    bytes++;
                    if (this.mapBytesToUnicode[num2] == '\0' && num2 != 0)
                    {
                        num--;
                        decoderFallbackBuffer = dbcsdecoder.FallbackBuffer;
                        decoderFallbackBuffer.InternalInitialize(ptr - count, null);
                        byte[] bytes3 = new byte[]
                        {
                            (byte)(num2 >> 8),
                            (byte)num2
                        };
                        num += decoderFallbackBuffer.InternalFallback(bytes3, bytes);
                    }
                }
            }
            while (bytes < ptr)
            {
                int num3 = (int)(*bytes);
                bytes++;
                char c = this.mapBytesToUnicode[num3];
                if (c == '￾')
                {
                    num--;
                    if (bytes < ptr)
                    {
                        num3 <<= 8;
                        num3  |= (int)(*bytes);
                        bytes++;
                        c = this.mapBytesToUnicode[num3];
                    }
                    else
                    {
                        if (dbcsdecoder != null && !dbcsdecoder.MustFlush)
                        {
                            break;
                        }
                        num++;
                        c = '\0';
                    }
                }
                if (c == '\0' && num3 != 0)
                {
                    if (decoderFallbackBuffer == null)
                    {
                        if (dbcsdecoder == null)
                        {
                            decoderFallbackBuffer = base.DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            decoderFallbackBuffer = dbcsdecoder.FallbackBuffer;
                        }
                        decoderFallbackBuffer.InternalInitialize(ptr - count, null);
                    }
                    num--;
                    byte[] bytes4;
                    if (num3 < 256)
                    {
                        bytes4 = new byte[]
                        {
                            (byte)num3
                        };
                    }
                    else
                    {
                        bytes4 = new byte[]
                        {
                            (byte)(num3 >> 8),
                            (byte)num3
                        };
                    }
                    num += decoderFallbackBuffer.InternalFallback(bytes4, bytes);
                }
            }
            return(num);
        }
Пример #20
0
        // This is internal and called by something else,
        internal override unsafe int GetCharCount(byte *bytes, int count, DecoderNLS decoder)
        {
            // ASCII doesn't do best fit, so don't have to check for it, find out which decoder fallback we're using
            DecoderReplacementFallback fallback = null;

            if (decoder == null)
            {
                fallback = this.DecoderFallback as DecoderReplacementFallback;
            }
            else
            {
                fallback = decoder.Fallback as DecoderReplacementFallback;
            }

            if (fallback != null && fallback.MaxCharCount == 1)
            {
                // Just return length, SBCS stay the same length because they don't map to surrogate
                // pairs and we don't have a decoder fallback.

                return(count);
            }

            // Only need decoder fallback buffer if not using default replacement fallback, no best fit for ASCII
            DecoderFallbackBuffer fallbackBuffer = null;

            // Have to do it the hard way.
            // Assume charCount will be == count
            int charCount = count;

            byte[] byteBuffer = new byte[1];

            // Do it our fast way
            byte *byteEnd = bytes + count;

            // Quick loop
            while (bytes < byteEnd)
            {
                // Faster if don't use *bytes++;
                byte b = *bytes;
                bytes++;

                // If unknown we have to do fallback count
                if (b >= 0x80)
                {
                    if (fallbackBuffer == null)
                    {
                        if (decoder == null)
                        {
                            fallbackBuffer = this.DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            fallbackBuffer = decoder.FallbackBuffer;
                        }
                        fallbackBuffer.InternalInitialize(byteEnd - count, null);
                    }

                    // Use fallback buffer
                    byteBuffer[0] = b;
                    charCount--;            // Have to unreserve the one we already allocated for b
                    charCount += fallbackBuffer.InternalFallback(byteBuffer, bytes);
                }
            }

            // Converted sequence is same length as input
            return(charCount);
        }
Пример #21
0
        internal override unsafe int GetChars(byte *bytes, int byteCount,
                                              char *chars, int charCount, DecoderNLS decoder)
        {
            // Do it fast way if using ? replacement fallback
            byte *byteEnd   = bytes + byteCount;
            byte *byteStart = bytes;
            char *charStart = chars;

            // Note: ASCII doesn't do best fit, but we have to fallback if they use something > 0x7f
            // Only need decoder fallback buffer if not using ? fallback.
            // ASCII doesn't do best fit, so don't have to check for it, find out which decoder fallback we're using
            DecoderReplacementFallback fallback = null;

            if (decoder == null)
            {
                fallback = this.DecoderFallback as DecoderReplacementFallback;
            }
            else
            {
                fallback = decoder.Fallback as DecoderReplacementFallback;
            }

            if (fallback != null && fallback.MaxCharCount == 1)
            {
                // Try it the fast way
                char replacementChar = fallback.DefaultString[0];

                // Need byteCount chars, otherwise too small buffer
                if (charCount < byteCount)
                {
                    // Need at least 1 output byte, throw if must throw
                    ThrowCharsOverflow(decoder, charCount < 1);

                    // Not throwing, use what we can
                    byteEnd = bytes + charCount;
                }

                // Quick loop, just do '?' replacement because we don't have fallbacks for decodings.
                while (bytes < byteEnd)
                {
                    byte b = *(bytes++);
                    if (b >= 0x80)
                    {
                        // This is an invalid byte in the ASCII encoding.
                        *(chars++) = replacementChar;
                    }
                    else
                    {
                        *(chars++) = unchecked ((char)b);
                    }
                }

                // bytes & chars used are the same
                if (decoder != null)
                {
                    decoder.m_bytesUsed = (int)(bytes - byteStart);
                }
                return((int)(chars - charStart));
            }

            // Slower way's going to need a fallback buffer
            DecoderFallbackBuffer fallbackBuffer = null;

            byte[] byteBuffer = new byte[1];
            char * charEnd    = chars + charCount;

            // Not quite so fast loop
            while (bytes < byteEnd)
            {
                // Faster if don't use *bytes++;
                byte b = *(bytes);
                bytes++;

                if (b >= 0x80)
                {
                    // This is an invalid byte in the ASCII encoding.
                    if (fallbackBuffer == null)
                    {
                        if (decoder == null)
                        {
                            fallbackBuffer = this.DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            fallbackBuffer = decoder.FallbackBuffer;
                        }
                        fallbackBuffer.InternalInitialize(byteEnd - byteCount, charEnd);
                    }

                    // Use fallback buffer
                    byteBuffer[0] = b;

                    // Note that chars won't get updated unless this succeeds
                    if (!fallbackBuffer.InternalFallback(byteBuffer, bytes, ref chars))
                    {
                        bytes--;                                            // unused byte
                        fallbackBuffer.InternalReset();                     // Didn't fall this back
                        ThrowCharsOverflow(decoder, chars == charStart);    // throw?
                        break;                                              // don't throw, but stop loop
                    }
                }
                else
                {
                    // Make sure we have buffer space
                    if (chars >= charEnd)
                    {
                        bytes--;                                            // unused byte
                        ThrowCharsOverflow(decoder, chars == charStart);    // throw?
                        break;                                              // don't throw, but stop loop
                    }

                    *(chars) = unchecked ((char)b);
                    chars++;
                }
            }

            // Might have had decoder fallback stuff.
            if (decoder != null)
                decoder.m_bytesUsed = (int)(bytes - byteStart); }
Пример #22
0
        internal override unsafe int GetChars(byte *bytes, int byteCount, char *chars, int charCount, DecoderNLS decoder)
        {
            base.CheckMemorySection();
            bool  isMicrosoftBestFitFallback = false;
            byte *numPtr  = bytes + byteCount;
            byte *numPtr2 = bytes;
            char *chPtr   = chars;
            DecoderReplacementFallback decoderFallback = null;

            if (decoder == null)
            {
                decoderFallback            = base.DecoderFallback as DecoderReplacementFallback;
                isMicrosoftBestFitFallback = base.DecoderFallback.IsMicrosoftBestFitFallback;
            }
            else
            {
                decoderFallback            = decoder.Fallback as DecoderReplacementFallback;
                isMicrosoftBestFitFallback = decoder.Fallback.IsMicrosoftBestFitFallback;
            }
            if (isMicrosoftBestFitFallback || ((decoderFallback != null) && (decoderFallback.MaxCharCount == 1)))
            {
                char ch;
                if (decoderFallback == null)
                {
                    ch = '?';
                }
                else
                {
                    ch = decoderFallback.DefaultString[0];
                }
                if (charCount < byteCount)
                {
                    base.ThrowCharsOverflow(decoder, charCount < 1);
                    numPtr = bytes + charCount;
                }
                while (bytes < numPtr)
                {
                    char ch2;
                    if (isMicrosoftBestFitFallback)
                    {
                        if (base.arrayBytesBestFit == null)
                        {
                            this.ReadBestFitTable();
                        }
                        ch2 = base.arrayBytesBestFit[bytes[0]];
                    }
                    else
                    {
                        ch2 = this.mapBytesToUnicode[bytes[0]];
                    }
                    bytes++;
                    if (ch2 == 0xfffd)
                    {
                        chars[0] = ch;
                    }
                    else
                    {
                        chars[0] = ch2;
                    }
                    chars++;
                }
                if (decoder != null)
                {
                    decoder.m_bytesUsed = (int)((long)((bytes - numPtr2) / 1));
                }
                return((int)((long)((chars - chPtr) / 2)));
            }
            DecoderFallbackBuffer fallbackBuffer = null;

            byte[] buffer2 = new byte[1];
            char * charEnd = chars + charCount;

            while (bytes < numPtr)
            {
                char ch3 = this.mapBytesToUnicode[bytes[0]];
                bytes++;
                if (ch3 == 0xfffd)
                {
                    if (fallbackBuffer == null)
                    {
                        if (decoder == null)
                        {
                            fallbackBuffer = base.DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            fallbackBuffer = decoder.FallbackBuffer;
                        }
                        fallbackBuffer.InternalInitialize(numPtr - byteCount, charEnd);
                    }
                    buffer2[0] = *(bytes - 1);
                    if (fallbackBuffer.InternalFallback(buffer2, bytes, ref chars))
                    {
                        continue;
                    }
                    bytes--;
                    fallbackBuffer.InternalReset();
                    base.ThrowCharsOverflow(decoder, bytes == numPtr2);
                    break;
                }
                if (chars >= charEnd)
                {
                    bytes--;
                    base.ThrowCharsOverflow(decoder, bytes == numPtr2);
                    break;
                }
                chars[0] = ch3;
                chars++;
            }
            if (decoder != null)
            {
                decoder.m_bytesUsed = (int)((long)((bytes - numPtr2) / 1));
            }
            return((int)((long)((chars - chPtr) / 2)));
        }
Пример #23
0
        internal override unsafe int GetChars(byte *bytes, int byteCount,
                                              char *chars, int charCount, DecoderNLS baseDecoder)
        {
            Debug.Assert(chars != null, "[UTF32Encoding.GetChars]chars!=null");
            Debug.Assert(bytes != null, "[UTF32Encoding.GetChars]bytes!=null");
            Debug.Assert(byteCount >= 0, "[UTF32Encoding.GetChars]byteCount >=0");
            Debug.Assert(charCount >= 0, "[UTF32Encoding.GetChars]charCount >=0");

            UTF32Decoder decoder = (UTF32Decoder)baseDecoder;

            // None so far!
            char *charStart = chars;
            char *charEnd   = chars + charCount;

            byte *byteStart = bytes;
            byte *byteEnd   = bytes + byteCount;

            // See if there's anything in our decoder (but don't clear it yet)
            int  readCount = 0;
            uint iChar     = 0;

            // For fallback we may need a fallback buffer
            DecoderFallbackBuffer fallbackBuffer = null;
            char *charsForFallback;

            // See if there's anything in our decoder
            if (decoder != null)
            {
                readCount      = decoder.readByteCount;
                iChar          = (uint)decoder.iChar;
                fallbackBuffer = baseDecoder.FallbackBuffer;

                // Shouldn't have anything in fallback buffer for GetChars
                // (don't have to check m_throwOnOverflow for chars)
                Debug.Assert(fallbackBuffer.Remaining == 0,
                             "[UTF32Encoding.GetChars]Expected empty fallback buffer at start");
            }
            else
            {
                fallbackBuffer = this.decoderFallback.CreateFallbackBuffer();
            }

            // Set our internal fallback interesting things.
            fallbackBuffer.InternalInitialize(bytes, chars + charCount);

            // Loop through our input, 4 characters at a time!
            while (bytes < byteEnd)
            {
                // Get our next character
                if (bigEndian)
                {
                    // Scoot left and add it to the bottom
                    iChar <<= 8;
                    iChar  += *(bytes++);
                }
                else
                {
                    // Scoot right and add it to the top
                    iChar >>= 8;
                    iChar  += (uint)(*(bytes++)) << 24;
                }

                readCount++;

                // See if we have all the bytes yet
                if (readCount < 4)
                {
                    continue;
                }

                // Have the bytes
                readCount = 0;

                // See if its valid to encode
                if (iChar > 0x10FFFF || (iChar >= 0xD800 && iChar <= 0xDFFF))
                {
                    // Need to fall back these 4 bytes
                    byte[] fallbackBytes;
                    if (bigEndian)
                    {
                        fallbackBytes = new byte[] {
                            unchecked ((byte)(iChar >> 24)), unchecked ((byte)(iChar >> 16)),
                            unchecked ((byte)(iChar >> 8)), unchecked ((byte)(iChar))
                        };
                    }
                    else
                    {
                        fallbackBytes = new byte[] {
                            unchecked ((byte)(iChar)), unchecked ((byte)(iChar >> 8)),
                            unchecked ((byte)(iChar >> 16)), unchecked ((byte)(iChar >> 24))
                        };
                    }

                    // Chars won't be updated unless this works.
                    charsForFallback = chars;
                    bool fallbackResult = fallbackBuffer.InternalFallback(fallbackBytes, bytes, ref charsForFallback);
                    chars = charsForFallback;

                    if (!fallbackResult)
                    {
                        // Couldn't fallback, throw or wait til next time
                        // We either read enough bytes for bytes-=4 to work, or we're
                        // going to throw in ThrowCharsOverflow because chars == charStart
                        Debug.Assert(bytes >= byteStart + 4 || chars == charStart,
                                     "[UTF32Encoding.GetChars]Expected to have consumed bytes or throw (bad surrogate)");
                        bytes -= 4;                                      // get back to where we were
                        iChar  = 0;                                      // Remembering nothing
                        fallbackBuffer.InternalReset();
                        ThrowCharsOverflow(decoder, chars == charStart); // Might throw, if no chars output
                        break;                                           // Stop here, didn't throw
                    }

                    // Ignore the illegal character
                    iChar = 0;
                    continue;
                }


                // Ok, we have something we can add to our output
                if (iChar >= 0x10000)
                {
                    // Surrogates take 2
                    if (chars >= charEnd - 1)
                    {
                        // Throwing or stopping
                        // We either read enough bytes for bytes-=4 to work, or we're
                        // going to throw in ThrowCharsOverflow because chars == charStart
                        Debug.Assert(bytes >= byteStart + 4 || chars == charStart,
                                     "[UTF32Encoding.GetChars]Expected to have consumed bytes or throw (surrogate)");
                        bytes -= 4;                                      // get back to where we were
                        iChar  = 0;                                      // Remembering nothing
                        ThrowCharsOverflow(decoder, chars == charStart); // Might throw, if no chars output
                        break;                                           // Stop here, didn't throw
                    }

                    *(chars++) = GetHighSurrogate(iChar);
                    iChar      = GetLowSurrogate(iChar);
                }
                // Bounds check for normal character
                else if (chars >= charEnd)
                {
                    // Throwing or stopping
                    // We either read enough bytes for bytes-=4 to work, or we're
                    // going to throw in ThrowCharsOverflow because chars == charStart
                    Debug.Assert(bytes >= byteStart + 4 || chars == charStart,
                                 "[UTF32Encoding.GetChars]Expected to have consumed bytes or throw (normal char)");
                    bytes -= 4;                                      // get back to where we were
                    iChar  = 0;                                      // Remembering nothing
                    ThrowCharsOverflow(decoder, chars == charStart); // Might throw, if no chars output
                    break;                                           // Stop here, didn't throw
                }

                // Add the rest of the surrogate or our normal character
                *(chars++) = (char)iChar;

                // iChar is back to 0
                iChar = 0;
            }

            // See if we have something left over that has to be decoded
            if (readCount > 0 && (decoder == null || decoder.MustFlush))
            {
                // Oops, there's something left over with no place to go.
                byte[] fallbackBytes = new byte[readCount];
                int    tempCount     = readCount;
                if (bigEndian)
                {
                    while (tempCount > 0)
                    {
                        fallbackBytes[--tempCount] = unchecked ((byte)iChar);
                        iChar >>= 8;
                    }
                }
                else
                {
                    while (tempCount > 0)
                    {
                        fallbackBytes[--tempCount] = unchecked ((byte)(iChar >> 24));
                        iChar <<= 8;
                    }
                }

                charsForFallback = chars;
                bool fallbackResult = fallbackBuffer.InternalFallback(fallbackBytes, bytes, ref charsForFallback);
                chars = charsForFallback;

                if (!fallbackResult)
                {
                    // Couldn't fallback.
                    fallbackBuffer.InternalReset();
                    ThrowCharsOverflow(decoder, chars == charStart);// Might throw, if no chars output
                    // Stop here, didn't throw, backed up, so still nothing in buffer
                }
                else
                {
                    // Don't clear our decoder unless we could fall it back.
                    // If we caught the if above, then we're a convert() and will catch this next time.
                    readCount = 0;
                    iChar     = 0;
                }
            }

            // Remember any left over stuff, clearing buffer as well for MustFlush
            if (decoder != null)
            {
                decoder.iChar         = (int)iChar;
                decoder.readByteCount = readCount;
                decoder.m_bytesUsed   = (int)(bytes - byteStart);
            }

            // Shouldn't have anything in fallback buffer for GetChars
            // (don't have to check m_throwOnOverflow for chars)
            Debug.Assert(fallbackBuffer.Remaining == 0,
                         "[UTF32Encoding.GetChars]Expected empty fallback buffer at end");

            // Return our count
            return((int)(chars - charStart));
        }
Пример #24
0
        internal override unsafe int GetChars(byte *bytes, int byteCount, char *chars, int charCount, DecoderNLS decoder)
        {
            byte *numPtr1 = bytes + byteCount;
            byte *numPtr2 = bytes;
            char *chPtr   = chars;
            DecoderReplacementFallback replacementFallback = decoder != null ? decoder.Fallback as DecoderReplacementFallback : this.DecoderFallback as DecoderReplacementFallback;

            if (replacementFallback != null && replacementFallback.MaxCharCount == 1)
            {
                char ch = replacementFallback.DefaultString[0];
                if (charCount < byteCount)
                {
                    this.ThrowCharsOverflow(decoder, charCount < 1);
                    numPtr1 = bytes + charCount;
                }
                while (bytes < numPtr1)
                {
                    byte num     = *bytes++;
                    *    chars++ = (int)num < 128 ? (char)num : ch;
                }
                if (decoder != null)
                {
                    decoder.m_bytesUsed = (int)(bytes - numPtr2);
                }
                return((int)(chars - chPtr));
            }
            DecoderFallbackBuffer decoderFallbackBuffer = (DecoderFallbackBuffer)null;

            byte[] bytes1  = new byte[1];
            char * charEnd = chars + charCount;

            while (bytes < numPtr1)
            {
                byte num = *bytes;
                ++bytes;
                if ((int)num >= 128)
                {
                    if (decoderFallbackBuffer == null)
                    {
                        decoderFallbackBuffer = decoder != null ? decoder.FallbackBuffer : this.DecoderFallback.CreateFallbackBuffer();
                        decoderFallbackBuffer.InternalInitialize(numPtr1 - byteCount, charEnd);
                    }
                    bytes1[0] = num;
                    if (!decoderFallbackBuffer.InternalFallback(bytes1, bytes, ref chars))
                    {
                        --bytes;
                        decoderFallbackBuffer.InternalReset();
                        this.ThrowCharsOverflow(decoder, chars == chPtr);
                        break;
                    }
                }
                else
                {
                    if (chars >= charEnd)
                    {
                        --bytes;
                        this.ThrowCharsOverflow(decoder, chars == chPtr);
                        break;
                    }
                    *chars = (char)num;
                    chars += 2;
                }
            }
            if (decoder != null)
            {
                decoder.m_bytesUsed = (int)(bytes - numPtr2);
            }
            return((int)(chars - chPtr));
        }
        [System.Security.SecurityCritical]  // auto-generated
        internal override unsafe int GetChars(byte *bytes, int byteCount,
                                              char *chars, int charCount, DecoderNLS decoder)
        {
            // Just need to ASSERT, this is called by something else internal that checked parameters already
            Contract.Assert(bytes != null, "[SBCSCodePageEncoding.GetChars]bytes is null");
            Contract.Assert(byteCount >= 0, "[SBCSCodePageEncoding.GetChars]byteCount is negative");
            Contract.Assert(chars != null, "[SBCSCodePageEncoding.GetChars]chars is null");
            Contract.Assert(charCount >= 0, "[SBCSCodePageEncoding.GetChars]charCount is negative");

            CheckMemorySection();

            // See if we have best fit
            bool bUseBestFit = false;

            // Do it fast way if using ? replacement or best fit fallbacks
            byte *byteEnd   = bytes + byteCount;
            byte *byteStart = bytes;
            char *charStart = chars;

            // Only need decoder fallback buffer if not using default replacement fallback or best fit fallback.
            DecoderReplacementFallback fallback = null;

            if (decoder == null)
            {
                fallback    = this.DecoderFallback as DecoderReplacementFallback;
                bUseBestFit = this.DecoderFallback.IsMicrosoftBestFitFallback;
            }
            else
            {
                fallback    = decoder.Fallback as DecoderReplacementFallback;
                bUseBestFit = decoder.Fallback.IsMicrosoftBestFitFallback;
                Contract.Assert(!decoder.m_throwOnOverflow || !decoder.InternalHasFallbackBuffer ||
                                decoder.FallbackBuffer.Remaining == 0,
                                "[SBCSCodePageEncoding.GetChars]Expected empty fallback buffer at start");
            }

            if (bUseBestFit || (fallback != null && fallback.MaxCharCount == 1))
            {
                // Try it the fast way
                char replacementChar;
                if (fallback == null)
                {
                    replacementChar = '?';  // Best fit alwasy has ? for fallback for SBCS
                }
                else
                {
                    replacementChar = fallback.DefaultString[0];
                }

                // Need byteCount chars, otherwise too small buffer
                if (charCount < byteCount)
                {
                    // Need at least 1 output byte, throw if must throw
                    ThrowCharsOverflow(decoder, charCount < 1);

                    // Not throwing, use what we can
                    byteEnd = bytes + charCount;
                }

                // Quick loop, just do '?' replacement because we don't have fallbacks for decodings.
                while (bytes < byteEnd)
                {
                    char c;
                    if (bUseBestFit)
                    {
                        if (arrayBytesBestFit == null)
                        {
                            ReadBestFitTable();
                        }
                        c = arrayBytesBestFit[*bytes];
                    }
                    else
                    {
                        c = mapBytesToUnicode[*bytes];
                    }
                    bytes++;

                    if (c == UNKNOWN_CHAR)
                    {
                        // This is an invalid byte in the ASCII encoding.
                        *chars = replacementChar;
                    }
                    else
                    {
                        *chars = c;
                    }
                    chars++;
                }

                // bytes & chars used are the same
                if (decoder != null)
                {
                    decoder.m_bytesUsed = (int)(bytes - byteStart);
                }
                return((int)(chars - charStart));
            }

            // Slower way's going to need a fallback buffer
            DecoderFallbackBuffer fallbackBuffer = null;

            byte[] byteBuffer = new byte[1];
            char * charEnd    = chars + charCount;

            // Not quite so fast loop
            while (bytes < byteEnd)
            {
                // Faster if don't use *bytes++;
                char c = mapBytesToUnicode[*bytes];
                bytes++;

                // See if it was unknown
                if (c == UNKNOWN_CHAR)
                {
                    // Make sure we have a fallback buffer
                    if (fallbackBuffer == null)
                    {
                        if (decoder == null)
                        {
                            fallbackBuffer = this.DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            fallbackBuffer = decoder.FallbackBuffer;
                        }
                        fallbackBuffer.InternalInitialize(byteEnd - byteCount, charEnd);
                    }

                    // Use fallback buffer
                    Contract.Assert(bytes > byteStart,
                                    "[SBCSCodePageEncoding.GetChars]Expected bytes to have advanced already (unknown byte)");
                    byteBuffer[0] = *(bytes - 1);
                    // Fallback adds fallback to chars, but doesn't increment chars unless the whole thing fits.
                    if (!fallbackBuffer.InternalFallback(byteBuffer, bytes, ref chars))
                    {
                        // May or may not throw, but we didn't get this byte
                        bytes--;                                            // unused byte
                        fallbackBuffer.InternalReset();                     // Didn't fall this back
                        ThrowCharsOverflow(decoder, bytes == byteStart);    // throw?
                        break;                                              // don't throw, but stop loop
                    }
                }
                else
                {
                    // Make sure we have buffer space
                    if (chars >= charEnd)
                    {
                        Contract.Assert(bytes > byteStart,
                                        "[SBCSCodePageEncoding.GetChars]Expected bytes to have advanced already (known byte)");
                        bytes--;                                            // unused byte
                        ThrowCharsOverflow(decoder, bytes == byteStart);    // throw?
                        break;                                              // don't throw, but stop loop
                    }

                    *(chars) = c;
                    chars++;
                }
            }

            // Might have had decoder fallback stuff.
            if (decoder != null)
            {
                decoder.m_bytesUsed = (int)(bytes - byteStart);
            }

            // Expect Empty fallback buffer for GetChars
            Contract.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0,
                            "[SBCSEncoding.GetChars]Expected Empty fallback buffer at end");

            return((int)(chars - charStart));
        }
        internal override unsafe int GetChars(byte *bytes, int byteCount, char *chars, int charCount, DecoderNLS decoder)
        {
            byte *numPtr  = bytes + byteCount;
            byte *numPtr2 = bytes;
            char *chPtr   = chars;
            DecoderReplacementFallback decoderFallback = null;

            if (decoder == null)
            {
                decoderFallback = base.DecoderFallback as DecoderReplacementFallback;
            }
            else
            {
                decoderFallback = decoder.Fallback as DecoderReplacementFallback;
            }
            if ((decoderFallback != null) && (decoderFallback.MaxCharCount == 1))
            {
                char ch = decoderFallback.DefaultString[0];
                if (charCount < byteCount)
                {
                    base.ThrowCharsOverflow(decoder, charCount < 1);
                    numPtr = bytes + charCount;
                }
                while (bytes < numPtr)
                {
                    bytes++;
                    byte num = bytes[0];
                    if (num >= 0x80)
                    {
                        chars++;
                        chars[0] = ch;
                    }
                    else
                    {
                        chars++;
                        chars[0] = (char)num;
                    }
                }
                if (decoder != null)
                {
                    decoder.m_bytesUsed = (int)((long)((bytes - numPtr2) / 1));
                }
                return((int)((long)((chars - chPtr) / 2)));
            }
            DecoderFallbackBuffer fallbackBuffer = null;

            byte[] buffer2 = new byte[1];
            char * charEnd = chars + charCount;

            while (bytes < numPtr)
            {
                byte num2 = bytes[0];
                bytes++;
                if (num2 >= 0x80)
                {
                    if (fallbackBuffer == null)
                    {
                        if (decoder == null)
                        {
                            fallbackBuffer = base.DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            fallbackBuffer = decoder.FallbackBuffer;
                        }
                        fallbackBuffer.InternalInitialize(numPtr - byteCount, charEnd);
                    }
                    buffer2[0] = num2;
                    if (fallbackBuffer.InternalFallback(buffer2, bytes, ref chars))
                    {
                        continue;
                    }
                    bytes--;
                    fallbackBuffer.InternalReset();
                    base.ThrowCharsOverflow(decoder, chars == chPtr);
                    break;
                }
                if (chars >= charEnd)
                {
                    bytes--;
                    base.ThrowCharsOverflow(decoder, chars == chPtr);
                    break;
                }
                chars[0] = (char)num2;
                chars++;
            }
            if (decoder != null)
            {
                decoder.m_bytesUsed = (int)((long)((bytes - numPtr2) / 1));
            }
            return((int)((long)((chars - chPtr) / 2)));
        }
Пример #27
0
        internal override unsafe int GetChars(byte *bytes, int byteCount,
                                              char *chars, int charCount, DecoderNLS decoder)
        {
            // Just need to ASSERT, this is called by something else internal that checked parameters already
            Debug.Assert(bytes != null, "[ASCIIEncoding.GetChars]bytes is null");
            Debug.Assert(byteCount >= 0, "[ASCIIEncoding.GetChars]byteCount is negative");
            Debug.Assert(chars != null, "[ASCIIEncoding.GetChars]chars is null");
            Debug.Assert(charCount >= 0, "[ASCIIEncoding.GetChars]charCount is negative");

            // Do it fast way if using ? replacement fallback
            byte *byteEnd   = bytes + byteCount;
            byte *byteStart = bytes;
            char *charStart = chars;

            // Note: ASCII doesn't do best fit, but we have to fallback if they use something > 0x7f
            // Only need decoder fallback buffer if not using ? fallback.
            // ASCII doesn't do best fit, so don't have to check for it, find out which decoder fallback we're using
            DecoderReplacementFallback fallback = null;
            char *charsForFallback;

            if (decoder == null)
            {
                fallback = this.DecoderFallback as DecoderReplacementFallback;
            }
            else
            {
                fallback = decoder.Fallback as DecoderReplacementFallback;
                Debug.Assert(!decoder.m_throwOnOverflow || !decoder.InternalHasFallbackBuffer ||
                             decoder.FallbackBuffer.Remaining == 0,
                             "[ASCIICodePageEncoding.GetChars]Expected empty fallback buffer");
            }

            if (fallback != null && fallback.MaxCharCount == 1)
            {
                // Try it the fast way
                char replacementChar = fallback.DefaultString[0];

                // Need byteCount chars, otherwise too small buffer
                if (charCount < byteCount)
                {
                    // Need at least 1 output byte, throw if must throw
                    ThrowCharsOverflow(decoder, charCount < 1);

                    // Not throwing, use what we can
                    byteEnd = bytes + charCount;
                }

                // Quick loop, just do '?' replacement because we don't have fallbacks for decodings.
                while (bytes < byteEnd)
                {
                    byte b = *(bytes++);
                    if (b >= 0x80)
                    {
                        // This is an invalid byte in the ASCII encoding.
                        *(chars++) = replacementChar;
                    }
                    else
                    {
                        *(chars++) = unchecked ((char)b);
                    }
                }

                // bytes & chars used are the same
                if (decoder != null)
                {
                    decoder.m_bytesUsed = (int)(bytes - byteStart);
                }
                return((int)(chars - charStart));
            }

            // Slower way's going to need a fallback buffer
            DecoderFallbackBuffer fallbackBuffer = null;

            byte[] byteBuffer = new byte[1];
            char * charEnd    = chars + charCount;

            // Not quite so fast loop
            while (bytes < byteEnd)
            {
                // Faster if don't use *bytes++;
                byte b = *(bytes);
                bytes++;

                if (b >= 0x80)
                {
                    // This is an invalid byte in the ASCII encoding.
                    if (fallbackBuffer == null)
                    {
                        if (decoder == null)
                        {
                            fallbackBuffer = this.DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            fallbackBuffer = decoder.FallbackBuffer;
                        }
                        fallbackBuffer.InternalInitialize(byteEnd - byteCount, charEnd);
                    }

                    // Use fallback buffer
                    byteBuffer[0] = b;

                    // Note that chars won't get updated unless this succeeds
                    charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered
                    bool fallbackResult = fallbackBuffer.InternalFallback(byteBuffer, bytes, ref charsForFallback);
                    chars = charsForFallback;

                    if (!fallbackResult)
                    {
                        // May or may not throw, but we didn't get this byte
                        Debug.Assert(bytes > byteStart || chars == charStart,
                                     "[ASCIIEncoding.GetChars]Expected bytes to have advanced already (fallback case)");
                        bytes--;                                            // unused byte
                        fallbackBuffer.InternalReset();                     // Didn't fall this back
                        ThrowCharsOverflow(decoder, chars == charStart);    // throw?
                        break;                                              // don't throw, but stop loop
                    }
                }
                else
                {
                    // Make sure we have buffer space
                    if (chars >= charEnd)
                    {
                        Debug.Assert(bytes > byteStart || chars == charStart,
                                     "[ASCIIEncoding.GetChars]Expected bytes to have advanced already (normal case)");
                        bytes--;                                            // unused byte
                        ThrowCharsOverflow(decoder, chars == charStart);    // throw?
                        break;                                              // don't throw, but stop loop
                    }

                    *(chars) = unchecked ((char)b);
                    chars++;
                }
            }

            // Might have had decoder fallback stuff.
            if (decoder != null)
            {
                decoder.m_bytesUsed = (int)(bytes - byteStart);
            }

            // Expect Empty fallback buffer for GetChars
            Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0,
                         "[ASCIIEncoding.GetChars]Expected Empty fallback buffer");

            return((int)(chars - charStart));
        }
Пример #28
0
        internal override unsafe int GetCharCount(byte *bytes, int count, DecoderNLS baseDecoder)
        {
            base.CheckMemorySection();
            DBCSDecoder           decoder        = (DBCSDecoder)baseDecoder;
            DecoderFallbackBuffer fallbackBuffer = null;
            byte *numPtr = bytes + count;
            int   num    = count;

            if ((decoder != null) && (decoder.bLeftOver > 0))
            {
                if (count == 0)
                {
                    if (!decoder.MustFlush)
                    {
                        return(0);
                    }
                    fallbackBuffer = decoder.FallbackBuffer;
                    fallbackBuffer.InternalInitialize(bytes, null);
                    byte[] buffer2 = new byte[] { decoder.bLeftOver };
                    return(fallbackBuffer.InternalFallback(buffer2, bytes));
                }
                int index = decoder.bLeftOver << 8;
                index |= bytes[0];
                bytes++;
                if ((this.mapBytesToUnicode[index] == '\0') && (index != 0))
                {
                    num--;
                    fallbackBuffer = decoder.FallbackBuffer;
                    fallbackBuffer.InternalInitialize(numPtr - count, null);
                    byte[] buffer3 = new byte[] { (byte)(index >> 8), (byte)index };
                    num += fallbackBuffer.InternalFallback(buffer3, bytes);
                }
            }
            while (bytes < numPtr)
            {
                int num3 = bytes[0];
                bytes++;
                char ch2 = this.mapBytesToUnicode[num3];
                if (ch2 == 0xfffe)
                {
                    num--;
                    if (bytes < numPtr)
                    {
                        num3  = num3 << 8;
                        num3 |= bytes[0];
                        bytes++;
                        ch2 = this.mapBytesToUnicode[num3];
                    }
                    else
                    {
                        if ((decoder != null) && !decoder.MustFlush)
                        {
                            return(num);
                        }
                        num++;
                        ch2 = '\0';
                    }
                }
                if ((ch2 == '\0') && (num3 != 0))
                {
                    if (fallbackBuffer == null)
                    {
                        if (decoder == null)
                        {
                            fallbackBuffer = base.DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            fallbackBuffer = decoder.FallbackBuffer;
                        }
                        fallbackBuffer.InternalInitialize(numPtr - count, null);
                    }
                    num--;
                    byte[] buffer4 = null;
                    if (num3 < 0x100)
                    {
                        buffer4 = new byte[] { (byte)num3 };
                    }
                    else
                    {
                        buffer4 = new byte[] { (byte)(num3 >> 8), (byte)num3 };
                    }
                    num += fallbackBuffer.InternalFallback(buffer4, bytes);
                }
            }
            return(num);
        }