MovePrevious() public abstract method

public abstract MovePrevious ( ) : bool
return bool
Ejemplo n.º 1
0
        internal unsafe void MovePrevious(bool bThrow)
        {
            if (fallbackBufferHelper.bFallingBack)
            {
                fallbackBuffer.MovePrevious();                      // don't use last fallback
            }
            else
            {
                Debug.Assert(_chars > _charStart || (bThrow && (_bytes == _byteStart)),
                             "[EncodingByteBuffer.MovePrevious]expected previous data or throw");
                if (_chars > _charStart)
                {
                    _chars--;                                        // don't use last char
                }
            }

            if (bThrow)
            {
                _enc.ThrowBytesOverflow(_encoder, _bytes == _byteStart);    // Throw? (and reset fallback if not converting)
            }
        }
Ejemplo n.º 2
0
        internal override unsafe int GetBytes(char *chars, int charCount,
                                              byte *bytes, int byteCount, EncoderNLS encoder)
        {
            // Just need to ASSERT, this is called by something else internal that checked parameters already
            Debug.Assert(bytes != null, "[ASCIIEncoding.GetBytes]bytes is null");
            Debug.Assert(byteCount >= 0, "[ASCIIEncoding.GetBytes]byteCount is negative");
            Debug.Assert(chars != null, "[ASCIIEncoding.GetBytes]chars is null");
            Debug.Assert(charCount >= 0, "[ASCIIEncoding.GetBytes]charCount is negative");

            // Assert because we shouldn't be able to have a null encoder.
            Debug.Assert(encoderFallback != null, "[ASCIIEncoding.GetBytes]Attempting to use null encoder fallback");

            // Get any left over characters
            char charLeftOver = (char)0;
            EncoderReplacementFallback fallback = null;

            // For fallback we may need a fallback buffer, we know we aren't default fallback.
            EncoderFallbackBuffer fallbackBuffer = null;
            char *charsForFallback;

            // prepare our end
            char *charEnd   = chars + charCount;
            byte *byteStart = bytes;
            char *charStart = chars;

            if (encoder != null)
            {
                charLeftOver = encoder.charLeftOver;
                fallback     = encoder.Fallback as EncoderReplacementFallback;

                // We mustn't have left over fallback data when counting
                if (encoder.InternalHasFallbackBuffer)
                {
                    // We always need the fallback buffer in get bytes so we can flush any remaining ones if necessary
                    fallbackBuffer = encoder.FallbackBuffer;
                    if (fallbackBuffer.Remaining > 0 && encoder.m_throwOnOverflow)
                    {
                        throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback.GetType()));
                    }

                    // Set our internal fallback interesting things.
                    fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, true);
                }

                Debug.Assert(charLeftOver == 0 || Char.IsHighSurrogate(charLeftOver),
                             "[ASCIIEncoding.GetBytes]leftover character should be high surrogate");

                // Verify that we have no fallbackbuffer, for ASCII its always empty, so just assert
                Debug.Assert(!encoder.m_throwOnOverflow || !encoder.InternalHasFallbackBuffer ||
                             encoder.FallbackBuffer.Remaining == 0,
                             "[ASCIICodePageEncoding.GetBytes]Expected empty fallback buffer");
            }
            else
            {
                fallback = this.EncoderFallback as EncoderReplacementFallback;
            }


            // See if we do the fast default or slightly slower fallback
            if (fallback != null && fallback.MaxCharCount == 1)
            {
                // Fast version
                char cReplacement = fallback.DefaultString[0];

                // Check for replacements in range, otherwise fall back to slow version.
                if (cReplacement <= (char)0x7f)
                {
                    // We should have exactly as many output bytes as input bytes, unless there's a left
                    // over character, in which case we may need one more.
                    // If we had a left over character will have to add a ?  (This happens if they had a funky
                    // fallback last time, but not this time.) (We can't spit any out though
                    // because with fallback encoder each surrogate is treated as a seperate code point)
                    if (charLeftOver > 0)
                    {
                        // Have to have room
                        // Throw even if doing no throw version because this is just 1 char,
                        // so buffer will never be big enough
                        if (byteCount == 0)
                        {
                            ThrowBytesOverflow(encoder, true);
                        }

                        // This'll make sure we still have more room and also make sure our return value is correct.
                        *(bytes++) = (byte)cReplacement;
                        byteCount--;                // We used one of the ones we were counting.
                    }

                    // This keeps us from overrunning our output buffer
                    if (byteCount < charCount)
                    {
                        // Throw or make buffer smaller?
                        ThrowBytesOverflow(encoder, byteCount < 1);

                        // Just use what we can
                        charEnd = chars + byteCount;
                    }

                    // We just do a quick copy
                    while (chars < charEnd)
                    {
                        char ch2 = *(chars++);
                        if (ch2 >= 0x0080)
                        {
                            *(bytes++) = (byte)cReplacement;
                        }
                        else
                        {
                            *(bytes++) = unchecked ((byte)(ch2));
                        }
                    }

                    // Clear encoder
                    if (encoder != null)
                    {
                        encoder.charLeftOver = (char)0;
                        encoder.m_charsUsed  = (int)(chars - charStart);
                    }

                    return((int)(bytes - byteStart));
                }
            }

            // Slower version, have to do real fallback.

            // prepare our end
            byte *byteEnd = bytes + byteCount;

            // We may have a left over character from last time, try and process it.
            if (charLeftOver > 0)
            {
                // Initialize the buffer
                Debug.Assert(encoder != null,
                             "[ASCIIEncoding.GetBytes]Expected non null encoder if we have surrogate left over");
                fallbackBuffer = encoder.FallbackBuffer;
                fallbackBuffer.InternalInitialize(chars, charEnd, encoder, true);

                // Since left over char was a surrogate, it'll have to be fallen back.
                // Get Fallback
                // This will fallback a pair if *chars is a low surrogate
                charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered
                fallbackBuffer.InternalFallback(charLeftOver, ref charsForFallback);
                chars = charsForFallback;
            }

            // Now we may have fallback char[] already from the encoder

            // Go ahead and do it, including the fallback.
            char ch;

            while ((ch = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != 0 ||
                   chars < charEnd)
            {
                // First unwind any fallback
                if (ch == 0)
                {
                    // No fallback, just get next char
                    ch = *chars;
                    chars++;
                }

                // Check for fallback, this'll catch surrogate pairs too.
                // All characters >= 0x80 must fall back.
                if (ch > 0x7f)
                {
                    // Initialize the buffer
                    if (fallbackBuffer == null)
                    {
                        if (encoder == null)
                        {
                            fallbackBuffer = this.encoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            fallbackBuffer = encoder.FallbackBuffer;
                        }
                        fallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, true);
                    }

                    // Get Fallback
                    charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered
                    fallbackBuffer.InternalFallback(ch, ref charsForFallback);
                    chars = charsForFallback;

                    // Go ahead & continue (& do the fallback)
                    continue;
                }

                // We'll use this one
                // Bounds check
                if (bytes >= byteEnd)
                {
                    // didn't use this char, we'll throw or use buffer
                    if (fallbackBuffer == null || fallbackBuffer.bFallingBack == false)
                    {
                        Debug.Assert(chars > charStart || bytes == byteStart,
                                     "[ASCIIEncoding.GetBytes]Expected chars to have advanced already.");
                        chars--;                                        // don't use last char
                    }
                    else
                    {
                        fallbackBuffer.MovePrevious();
                    }

                    // Are we throwing or using buffer?
                    ThrowBytesOverflow(encoder, bytes == byteStart);    // throw?
                    break;                                              // don't throw, stop
                }

                // Go ahead and add it
                *bytes = unchecked ((byte)ch);
                bytes++;
            }

            // Need to do encoder stuff
            if (encoder != null)
            {
                // Fallback stuck it in encoder if necessary, but we have to clear MustFlush cases
                if (fallbackBuffer != null && !fallbackBuffer.bUsedEncoder)
                {
                    // Clear it in case of MustFlush
                    encoder.charLeftOver = (char)0;
                }

                // Set our chars used count
                encoder.m_charsUsed = (int)(chars - charStart);
            }

            Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0 ||
                         (encoder != null && !encoder.m_throwOnOverflow),
                         "[ASCIIEncoding.GetBytes]Expected Empty fallback buffer at end");

            return((int)(bytes - byteStart));
        }
Ejemplo n.º 3
0
        internal override unsafe int GetBytes(char *chars, int charCount,
                                              byte *bytes, int byteCount, EncoderNLS encoder)
        {
            Debug.Assert(chars != null, "[UTF32Encoding.GetBytes]chars!=null");
            Debug.Assert(bytes != null, "[UTF32Encoding.GetBytes]bytes!=null");
            Debug.Assert(byteCount >= 0, "[UTF32Encoding.GetBytes]byteCount >=0");
            Debug.Assert(charCount >= 0, "[UTF32Encoding.GetBytes]charCount >=0");

            char *charStart = chars;
            char *charEnd   = chars + charCount;
            byte *byteStart = bytes;
            byte *byteEnd   = bytes + byteCount;

            char highSurrogate = '\0';

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

            if (encoder != null)
            {
                highSurrogate  = encoder.charLeftOver;
                fallbackBuffer = encoder.FallbackBuffer;

                // We mustn't have left over fallback data when not converting
                if (encoder.m_throwOnOverflow && fallbackBuffer.Remaining > 0)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty",
                                                                              this.EncodingName, encoder.Fallback.GetType()));
                }
            }
            else
            {
                fallbackBuffer = this.encoderFallback.CreateFallbackBuffer();
            }

            // Set our internal fallback interesting things.
            fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, true);

            char ch;

TryAgain:

            while (((ch = fallbackBuffer.InternalGetNextChar()) != 0) || chars < charEnd)
            {
                // First unwind any fallback
                if (ch == 0)
                {
                    // No fallback, just get next char
                    ch = *chars;
                    chars++;
                }

                // Do we need a low surrogate?
                if (highSurrogate != '\0')
                {
                    //
                    // In previous char, we encountered a high surrogate, so we are expecting a low surrogate here.
                    //
                    if (Char.IsLowSurrogate(ch))
                    {
                        // Is it a legal one?
                        uint iTemp = GetSurrogate(highSurrogate, ch);
                        highSurrogate = '\0';

                        //
                        // One surrogate pair will be translated into 4 bytes UTF32.
                        //
                        if (bytes + 3 >= byteEnd)
                        {
                            // Don't have 4 bytes
                            if (fallbackBuffer.bFallingBack)
                            {
                                fallbackBuffer.MovePrevious();                  // Aren't using these 2 fallback chars
                                fallbackBuffer.MovePrevious();
                            }
                            else
                            {
                                // If we don't have enough room, then either we should've advanced a while
                                // or we should have bytes==byteStart and throw below
                                Debug.Assert(chars > charStart + 1 || bytes == byteStart,
                                             "[UnicodeEncoding.GetBytes]Expected chars to have when no room to add surrogate pair");
                                chars -= 2;                                     // Aren't using those 2 chars
                            }
                            ThrowBytesOverflow(encoder, bytes == byteStart);    // Throw maybe (if no bytes written)
                            highSurrogate = (char)0;                            // Nothing left over (we backed up to start of pair if supplimentary)
                            break;
                        }

                        if (bigEndian)
                        {
                            *(bytes++) = (byte)(0x00);
                            *(bytes++) = (byte)(iTemp >> 16);       // Implies & 0xFF, which isn't needed cause high are all 0
                            *(bytes++) = (byte)(iTemp >> 8);        // Implies & 0xFF
                            *(bytes++) = (byte)(iTemp);             // Implies & 0xFF
                        }
                        else
                        {
                            *(bytes++) = (byte)(iTemp);             // Implies & 0xFF
                            *(bytes++) = (byte)(iTemp >> 8);        // Implies & 0xFF
                            *(bytes++) = (byte)(iTemp >> 16);       // Implies & 0xFF, which isn't needed cause high are all 0
                            *(bytes++) = (byte)(0x00);
                        }
                        continue;
                    }

                    // We are missing our low surrogate, decrement chars and fallback the high surrogate
                    // The high surrogate may have come from the encoder, but nothing else did.
                    Debug.Assert(chars > charStart,
                                 "[UTF32Encoding.GetBytes]Expected chars to have advanced if no low surrogate");
                    chars--;

                    // Do the fallback
                    charsForFallback = chars;
                    fallbackBuffer.InternalFallback(highSurrogate, ref charsForFallback);
                    chars = charsForFallback;

                    // We're going to fallback the old high surrogate.
                    highSurrogate = '\0';
                    continue;
                }

                // Do we have another high surrogate?, if so remember it
                if (Char.IsHighSurrogate(ch))
                {
                    //
                    // We'll have a high surrogate to check next time.
                    //
                    highSurrogate = ch;
                    continue;
                }

                // Check for illegal characters (low surrogate)
                if (Char.IsLowSurrogate(ch))
                {
                    // We have a leading low surrogate, do the fallback
                    charsForFallback = chars;
                    fallbackBuffer.InternalFallback(ch, ref charsForFallback);
                    chars = charsForFallback;

                    // Try again with fallback buffer
                    continue;
                }

                // We get to add the character, yippee.
                if (bytes + 3 >= byteEnd)
                {
                    // Don't have 4 bytes
                    if (fallbackBuffer.bFallingBack)
                    {
                        fallbackBuffer.MovePrevious();                  // Aren't using this fallback char
                    }
                    else
                    {
                        // Must've advanced already
                        Debug.Assert(chars > charStart,
                                     "[UTF32Encoding.GetBytes]Expected chars to have advanced if normal character");
                        chars--;                                        // Aren't using this char
                    }
                    ThrowBytesOverflow(encoder, bytes == byteStart);    // Throw maybe (if no bytes written)
                    break;                                              // Didn't throw, stop
                }

                if (bigEndian)
                {
                    *(bytes++) = (byte)(0x00);
                    *(bytes++) = (byte)(0x00);
                    *(bytes++) = (byte)((uint)ch >> 8); // Implies & 0xFF
                    *(bytes++) = (byte)(ch);            // Implies & 0xFF
                }
                else
                {
                    *(bytes++) = (byte)(ch);            // Implies & 0xFF
                    *(bytes++) = (byte)((uint)ch >> 8); // Implies & 0xFF
                    *(bytes++) = (byte)(0x00);
                    *(bytes++) = (byte)(0x00);
                }
            }

            // May have to do our last surrogate
            if ((encoder == null || encoder.MustFlush) && highSurrogate > 0)
            {
                // We have to do the fallback for the lonely high surrogate
                charsForFallback = chars;
                fallbackBuffer.InternalFallback(highSurrogate, ref charsForFallback);
                chars = charsForFallback;

                highSurrogate = (char)0;
                goto TryAgain;
            }

            // Fix our encoder if we have one
            Debug.Assert(highSurrogate == 0 || (encoder != null && !encoder.MustFlush),
                         "[UTF32Encoding.GetBytes]Expected encoder to be flushed.");

            if (encoder != null)
            {
                // Remember our left over surrogate (or 0 if flushing)
                encoder.charLeftOver = highSurrogate;

                // Need # chars used
                encoder.m_charsUsed = (int)(chars - charStart);
            }

            // return the new length
            return((int)(bytes - byteStart));
        }
Ejemplo n.º 4
0
        internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder)
        {
            char ch2;

            base.CheckMemorySection();
            EncoderFallbackBuffer fallbackBuffer = null;
            char *charEnd      = chars + charCount;
            char *chPtr2       = chars;
            byte *numPtr       = bytes;
            byte *numPtr2      = bytes + byteCount;
            char  charLeftOver = '\0';

            if (encoder != null)
            {
                charLeftOver   = encoder.charLeftOver;
                fallbackBuffer = encoder.FallbackBuffer;
                fallbackBuffer.InternalInitialize(chars, charEnd, encoder, true);
                if (encoder.m_throwOnOverflow && (fallbackBuffer.Remaining > 0))
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[] { this.EncodingName, encoder.Fallback.GetType() }));
                }
                if (charLeftOver > '\0')
                {
                    fallbackBuffer.InternalFallback(charLeftOver, ref chars);
                }
            }
            while (((ch2 = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != '\0') || (chars < charEnd))
            {
                if (ch2 == '\0')
                {
                    ch2 = chars[0];
                    chars++;
                }
                ushort num = this.mapUnicodeToBytes[ch2];
                if ((num == 0) && (ch2 != '\0'))
                {
                    if (fallbackBuffer == null)
                    {
                        fallbackBuffer = base.encoderFallback.CreateFallbackBuffer();
                        fallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, true);
                    }
                    fallbackBuffer.InternalFallback(ch2, ref chars);
                }
                else
                {
                    if (num >= 0x100)
                    {
                        if ((bytes + 1) >= numPtr2)
                        {
                            if ((fallbackBuffer == null) || !fallbackBuffer.bFallingBack)
                            {
                                chars--;
                            }
                            else
                            {
                                fallbackBuffer.MovePrevious();
                            }
                            base.ThrowBytesOverflow(encoder, chars == chPtr2);
                            break;
                        }
                        bytes[0] = (byte)(num >> 8);
                        bytes++;
                    }
                    else if (bytes >= numPtr2)
                    {
                        if ((fallbackBuffer == null) || !fallbackBuffer.bFallingBack)
                        {
                            chars--;
                        }
                        else
                        {
                            fallbackBuffer.MovePrevious();
                        }
                        base.ThrowBytesOverflow(encoder, chars == chPtr2);
                        break;
                    }
                    bytes[0] = (byte)(num & 0xff);
                    bytes++;
                }
            }
            if (encoder != null)
            {
                if ((fallbackBuffer != null) && !fallbackBuffer.bUsedEncoder)
                {
                    encoder.charLeftOver = '\0';
                }
                encoder.m_charsUsed = (int)((long)((chars - chPtr2) / 2));
            }
            return((int)((long)((bytes - numPtr) / 1)));
        }
Ejemplo n.º 5
0
        internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder)
        {
            char  ch2;
            char *charStart = chars;
            char *charEnd   = chars + charCount;
            byte *numPtr    = bytes;
            byte *numPtr2   = bytes + byteCount;
            char  cHigh     = '\0';
            EncoderFallbackBuffer fallbackBuffer = null;

            if (encoder != null)
            {
                cHigh          = encoder.charLeftOver;
                fallbackBuffer = encoder.FallbackBuffer;
                if (encoder.m_throwOnOverflow && (fallbackBuffer.Remaining > 0))
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[] { this.EncodingName, encoder.Fallback.GetType() }));
                }
            }
            else
            {
                fallbackBuffer = base.encoderFallback.CreateFallbackBuffer();
            }
            fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, true);
Label_023F:
            while (((ch2 = fallbackBuffer.InternalGetNextChar()) != '\0') || (chars < charEnd))
            {
                if (ch2 == '\0')
                {
                    ch2 = chars[0];
                    chars++;
                }
                if (cHigh != '\0')
                {
                    if (char.IsLowSurrogate(ch2))
                    {
                        uint surrogate = this.GetSurrogate(cHigh, ch2);
                        cHigh = '\0';
                        if ((bytes + 3) >= numPtr2)
                        {
                            if (fallbackBuffer.bFallingBack)
                            {
                                fallbackBuffer.MovePrevious();
                                fallbackBuffer.MovePrevious();
                            }
                            else
                            {
                                chars -= 2;
                            }
                            base.ThrowBytesOverflow(encoder, bytes == numPtr);
                            cHigh = '\0';
                            break;
                        }
                        if (this.bigEndian)
                        {
                            bytes++;
                            bytes[0] = 0;
                            bytes++;
                            bytes[0] = (byte)(surrogate >> 0x10);
                            bytes++;
                            bytes[0] = (byte)(surrogate >> 8);
                            bytes++;
                            bytes[0] = (byte)surrogate;
                        }
                        else
                        {
                            bytes++;
                            bytes[0] = (byte)surrogate;
                            bytes++;
                            bytes[0] = (byte)(surrogate >> 8);
                            bytes++;
                            bytes[0] = (byte)(surrogate >> 0x10);
                            bytes++;
                            bytes[0] = 0;
                        }
                    }
                    else
                    {
                        chars--;
                        fallbackBuffer.InternalFallback(cHigh, ref chars);
                        cHigh = '\0';
                    }
                }
                else if (char.IsHighSurrogate(ch2))
                {
                    cHigh = ch2;
                }
                else
                {
                    if (char.IsLowSurrogate(ch2))
                    {
                        fallbackBuffer.InternalFallback(ch2, ref chars);
                        continue;
                    }
                    if ((bytes + 3) >= numPtr2)
                    {
                        if (fallbackBuffer.bFallingBack)
                        {
                            fallbackBuffer.MovePrevious();
                        }
                        else
                        {
                            chars--;
                        }
                        base.ThrowBytesOverflow(encoder, bytes == numPtr);
                        break;
                    }
                    if (this.bigEndian)
                    {
                        bytes++;
                        bytes[0] = 0;
                        bytes++;
                        bytes[0] = 0;
                        bytes++;
                        bytes[0] = (byte)(ch2 >> 8);
                        bytes++;
                        bytes[0] = (byte)ch2;
                    }
                    else
                    {
                        bytes++;
                        bytes[0] = (byte)ch2;
                        bytes++;
                        bytes[0] = (byte)(ch2 >> 8);
                        bytes++;
                        bytes[0] = 0;
                        bytes++;
                        bytes[0] = 0;
                    }
                }
            }
            if (((encoder == null) || encoder.MustFlush) && (cHigh > '\0'))
            {
                fallbackBuffer.InternalFallback(cHigh, ref chars);
                cHigh = '\0';
                goto Label_023F;
            }
            if (encoder != null)
            {
                encoder.charLeftOver = cHigh;
                encoder.m_charsUsed  = (int)((long)((chars - charStart) / 2));
            }
            return((int)((long)((bytes - numPtr) / 1)));
        }
        internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder)
        {
            char ch4;
            char charLeftOver = '\0';
            EncoderReplacementFallback encoderFallback = null;
            EncoderFallbackBuffer      fallbackBuffer  = null;
            char *charEnd   = chars + charCount;
            byte *numPtr    = bytes;
            char *charStart = chars;

            if (encoder != null)
            {
                charLeftOver    = encoder.charLeftOver;
                encoderFallback = encoder.Fallback as EncoderReplacementFallback;
                if (encoder.InternalHasFallbackBuffer)
                {
                    fallbackBuffer = encoder.FallbackBuffer;
                    if ((fallbackBuffer.Remaining > 0) && encoder.m_throwOnOverflow)
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[] { this.EncodingName, encoder.Fallback.GetType() }));
                    }
                    fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, true);
                }
            }
            else
            {
                encoderFallback = base.EncoderFallback as EncoderReplacementFallback;
            }
            if ((encoderFallback != null) && (encoderFallback.MaxCharCount == 1))
            {
                char ch2 = encoderFallback.DefaultString[0];
                if (ch2 <= '\x007f')
                {
                    if (charLeftOver > '\0')
                    {
                        if (byteCount == 0)
                        {
                            base.ThrowBytesOverflow(encoder, true);
                        }
                        bytes++;
                        bytes[0] = (byte)ch2;
                        byteCount--;
                    }
                    if (byteCount < charCount)
                    {
                        base.ThrowBytesOverflow(encoder, byteCount < 1);
                        charEnd = chars + byteCount;
                    }
                    while (chars < charEnd)
                    {
                        chars++;
                        char ch3 = chars[0];
                        if (ch3 >= '\x0080')
                        {
                            bytes++;
                            bytes[0] = (byte)ch2;
                        }
                        else
                        {
                            bytes++;
                            bytes[0] = (byte)ch3;
                        }
                    }
                    if (encoder != null)
                    {
                        encoder.charLeftOver = '\0';
                        encoder.m_charsUsed  = (int)((long)((chars - charStart) / 2));
                    }
                    return((int)((long)((bytes - numPtr) / 1)));
                }
            }
            byte *numPtr2 = bytes + byteCount;

            if (charLeftOver > '\0')
            {
                fallbackBuffer = encoder.FallbackBuffer;
                fallbackBuffer.InternalInitialize(chars, charEnd, encoder, true);
                fallbackBuffer.InternalFallback(charLeftOver, ref chars);
            }
            while (((ch4 = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != '\0') || (chars < charEnd))
            {
                if (ch4 == '\0')
                {
                    ch4 = chars[0];
                    chars++;
                }
                if (ch4 > '\x007f')
                {
                    if (fallbackBuffer == null)
                    {
                        if (encoder == null)
                        {
                            fallbackBuffer = base.encoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            fallbackBuffer = encoder.FallbackBuffer;
                        }
                        fallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, true);
                    }
                    fallbackBuffer.InternalFallback(ch4, ref chars);
                }
                else
                {
                    if (bytes >= numPtr2)
                    {
                        if ((fallbackBuffer == null) || !fallbackBuffer.bFallingBack)
                        {
                            chars--;
                        }
                        else
                        {
                            fallbackBuffer.MovePrevious();
                        }
                        base.ThrowBytesOverflow(encoder, bytes == numPtr);
                        break;
                    }
                    bytes[0] = (byte)ch4;
                    bytes++;
                }
            }
            if (encoder != null)
            {
                if ((fallbackBuffer != null) && !fallbackBuffer.bUsedEncoder)
                {
                    encoder.charLeftOver = '\0';
                }
                encoder.m_charsUsed = (int)((long)((chars - charStart) / 2));
            }
            return((int)((long)((bytes - numPtr) / 1)));
        }
Ejemplo n.º 7
0
        internal unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder)
        {
            char c = '\0';
            EncoderFallbackBuffer encoderFallbackBuffer = null;
            char *ptr  = chars + charCount;
            byte *ptr2 = bytes;
            char *ptr3 = chars;
            EncoderReplacementFallback encoderReplacementFallback;

            if (encoder != null)
            {
                c = encoder.charLeftOver;
                encoderReplacementFallback = (encoder.Fallback as EncoderReplacementFallback);
                if (encoder.InternalHasFallbackBuffer)
                {
                    encoderFallbackBuffer = encoder.FallbackBuffer;
                    if (encoderFallbackBuffer.Remaining > 0 && encoder.m_throwOnOverflow)
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[]
                        {
                            this.EncodingName,
                            encoder.Fallback.GetType()
                        }));
                    }
                    encoderFallbackBuffer.InternalInitialize(ptr3, ptr, encoder, true);
                }
            }
            else
            {
                encoderReplacementFallback = (base.EncoderFallback as EncoderReplacementFallback);
            }
            if (encoderReplacementFallback != null && encoderReplacementFallback.MaxCharCount == 1)
            {
                char c2 = encoderReplacementFallback.DefaultString[0];
                if (c2 <= '\u007f')
                {
                    if (c > '\0')
                    {
                        if (byteCount == 0)
                        {
                            base.ThrowBytesOverflow(encoder, true);
                        }
                        *(bytes++) = (byte)c2;
                        byteCount--;
                    }
                    if (byteCount < charCount)
                    {
                        base.ThrowBytesOverflow(encoder, byteCount < 1);
                        ptr = chars + byteCount;
                    }
                    while (chars < ptr)
                    {
                        char c3 = *(chars++);
                        if (c3 >= '\u0080')
                        {
                            *(bytes++) = (byte)c2;
                        }
                        else
                        {
                            *(bytes++) = (byte)c3;
                        }
                    }
                    if (encoder != null)
                    {
                        encoder.charLeftOver = '\0';
                        encoder.m_charsUsed  = (int)((long)(chars - ptr3));
                    }
                    return((int)((long)(bytes - ptr2)));
                }
            }
            byte *ptr4 = bytes + byteCount;

            if (c > '\0')
            {
                encoderFallbackBuffer = encoder.FallbackBuffer;
                encoderFallbackBuffer.InternalInitialize(chars, ptr, encoder, true);
                encoderFallbackBuffer.InternalFallback(c, ref chars);
            }
            char c4;

            while ((c4 = ((encoderFallbackBuffer == null) ? '\0' : encoderFallbackBuffer.InternalGetNextChar())) != '\0' || chars < ptr)
            {
                if (c4 == '\0')
                {
                    c4 = *chars;
                    chars++;
                }
                if (c4 > '\u007f')
                {
                    if (encoderFallbackBuffer == null)
                    {
                        if (encoder == null)
                        {
                            encoderFallbackBuffer = this.encoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            encoderFallbackBuffer = encoder.FallbackBuffer;
                        }
                        encoderFallbackBuffer.InternalInitialize(ptr - charCount, ptr, encoder, true);
                    }
                    encoderFallbackBuffer.InternalFallback(c4, ref chars);
                }
                else
                {
                    if (bytes >= ptr4)
                    {
                        if (encoderFallbackBuffer == null || !encoderFallbackBuffer.bFallingBack)
                        {
                            chars--;
                        }
                        else
                        {
                            encoderFallbackBuffer.MovePrevious();
                        }
                        base.ThrowBytesOverflow(encoder, bytes == ptr2);
                        break;
                    }
                    *bytes = (byte)c4;
                    bytes++;
                }
            }
            if (encoder != null)
            {
                if (encoderFallbackBuffer != null && !encoderFallbackBuffer.bUsedEncoder)
                {
                    encoder.charLeftOver = '\0';
                }
                encoder.m_charsUsed = (int)((long)(chars - ptr3));
            }
            return((int)((long)(bytes - ptr2)));
        }
        internal unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder)
        {
            base.CheckMemorySection();
            EncoderFallbackBuffer encoderFallbackBuffer = null;
            char *ptr  = chars + charCount;
            char *ptr2 = chars;
            byte *ptr3 = bytes;
            byte *ptr4 = bytes + byteCount;

            if (encoder != null)
            {
                char charLeftOver = encoder.charLeftOver;
                encoderFallbackBuffer = encoder.FallbackBuffer;
                encoderFallbackBuffer.InternalInitialize(chars, ptr, encoder, true);
                if (encoder.m_throwOnOverflow && encoderFallbackBuffer.Remaining > 0)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[]
                    {
                        this.EncodingName,
                        encoder.Fallback.GetType()
                    }));
                }
                if (charLeftOver > '\0')
                {
                    encoderFallbackBuffer.InternalFallback(charLeftOver, ref chars);
                }
            }
            char c;

            while ((c = ((encoderFallbackBuffer == null) ? '\0' : encoderFallbackBuffer.InternalGetNextChar())) != '\0' || chars < ptr)
            {
                if (c == '\0')
                {
                    c = *chars;
                    chars++;
                }
                ushort num = this.mapUnicodeToBytes[(IntPtr)c];
                if (num == 0 && c != '\0')
                {
                    if (encoderFallbackBuffer == null)
                    {
                        encoderFallbackBuffer = this.encoderFallback.CreateFallbackBuffer();
                        encoderFallbackBuffer.InternalInitialize(ptr - charCount, ptr, encoder, true);
                    }
                    encoderFallbackBuffer.InternalFallback(c, ref chars);
                }
                else
                {
                    if (num >= 256)
                    {
                        if (bytes + 1 >= ptr4)
                        {
                            if (encoderFallbackBuffer == null || !encoderFallbackBuffer.bFallingBack)
                            {
                                chars--;
                            }
                            else
                            {
                                encoderFallbackBuffer.MovePrevious();
                            }
                            base.ThrowBytesOverflow(encoder, chars == ptr2);
                            break;
                        }
                        *bytes = (byte)(num >> 8);
                        bytes++;
                    }
                    else if (bytes >= ptr4)
                    {
                        if (encoderFallbackBuffer == null || !encoderFallbackBuffer.bFallingBack)
                        {
                            chars--;
                        }
                        else
                        {
                            encoderFallbackBuffer.MovePrevious();
                        }
                        base.ThrowBytesOverflow(encoder, chars == ptr2);
                        break;
                    }
                    *bytes = (byte)(num & 255);
                    bytes++;
                }
            }
            if (encoder != null)
            {
                if (encoderFallbackBuffer != null && !encoderFallbackBuffer.bUsedEncoder)
                {
                    encoder.charLeftOver = '\0';
                }
                encoder.m_charsUsed = (int)((long)(chars - ptr2));
            }
            return((int)((long)(bytes - ptr3)));
        }
Ejemplo n.º 9
0
        internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder)
        {
            this.CheckMemorySection();
            EncoderFallbackBuffer encoderFallbackBuffer = (EncoderFallbackBuffer)null;
            char *charEnd = chars + charCount;
            char *chPtr   = chars;
            byte *numPtr1 = bytes;
            byte *numPtr2 = bytes + byteCount;

            if (encoder != null)
            {
                char ch = encoder.charLeftOver;
                encoderFallbackBuffer = encoder.FallbackBuffer;
                encoderFallbackBuffer.InternalInitialize(chars, charEnd, encoder, true);
                if (encoder.m_throwOnOverflow && encoderFallbackBuffer.Remaining > 0)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", (object)this.EncodingName, (object)encoder.Fallback.GetType()));
                }
                if ((int)ch > 0)
                {
                    encoderFallbackBuffer.InternalFallback(ch, ref chars);
                }
            }
            char ch1;

            while ((int)(ch1 = encoderFallbackBuffer == null ? char.MinValue : encoderFallbackBuffer.InternalGetNextChar()) != 0 || chars < charEnd)
            {
                if ((int)ch1 == 0)
                {
                    ch1    = *chars;
                    chars += 2;
                }
                ushort num = this.mapUnicodeToBytes[ch1];
                if ((int)num == 0 && (int)ch1 != 0)
                {
                    if (encoderFallbackBuffer == null)
                    {
                        encoderFallbackBuffer = this.encoderFallback.CreateFallbackBuffer();
                        encoderFallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, true);
                    }
                    encoderFallbackBuffer.InternalFallback(ch1, ref chars);
                }
                else
                {
                    if ((int)num >= 256)
                    {
                        if (bytes + 1 >= numPtr2)
                        {
                            if (encoderFallbackBuffer == null || !encoderFallbackBuffer.bFallingBack)
                            {
                                chars -= 2;
                            }
                            else
                            {
                                encoderFallbackBuffer.MovePrevious();
                            }
                            this.ThrowBytesOverflow(encoder, chars == chPtr);
                            break;
                        }
                        *bytes = (byte)((uint)num >> 8);
                        ++bytes;
                    }
                    else if (bytes >= numPtr2)
                    {
                        if (encoderFallbackBuffer == null || !encoderFallbackBuffer.bFallingBack)
                        {
                            chars -= 2;
                        }
                        else
                        {
                            encoderFallbackBuffer.MovePrevious();
                        }
                        this.ThrowBytesOverflow(encoder, chars == chPtr);
                        break;
                    }
                    *bytes = (byte)((uint)num & (uint)byte.MaxValue);
                    ++bytes;
                }
            }
            if (encoder != null)
            {
                if (encoderFallbackBuffer != null && !encoderFallbackBuffer.bUsedEncoder)
                {
                    encoder.charLeftOver = char.MinValue;
                }
                encoder.m_charsUsed = (int)(chars - chPtr);
            }
            return((int)(bytes - numPtr1));
        }
Ejemplo n.º 10
0
        internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder)
        {
            char ch1 = char.MinValue;
            EncoderFallbackBuffer encoderFallbackBuffer = (EncoderFallbackBuffer)null;
            char *charEnd   = chars + charCount;
            byte *numPtr1   = bytes;
            char *charStart = chars;
            EncoderReplacementFallback replacementFallback;

            if (encoder != null)
            {
                ch1 = encoder.charLeftOver;
                replacementFallback = encoder.Fallback as EncoderReplacementFallback;
                if (encoder.InternalHasFallbackBuffer)
                {
                    encoderFallbackBuffer = encoder.FallbackBuffer;
                    if (encoderFallbackBuffer.Remaining > 0 && encoder.m_throwOnOverflow)
                    {
                        throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", (object)this.EncodingName, (object)encoder.Fallback.GetType()));
                    }
                    encoderFallbackBuffer.InternalInitialize(charStart, charEnd, encoder, true);
                }
            }
            else
            {
                replacementFallback = this.EncoderFallback as EncoderReplacementFallback;
            }
            if (replacementFallback != null && replacementFallback.MaxCharCount == 1)
            {
                char ch2 = replacementFallback.DefaultString[0];
                if ((int)ch2 <= (int)sbyte.MaxValue)
                {
                    if ((int)ch1 > 0)
                    {
                        if (byteCount == 0)
                        {
                            this.ThrowBytesOverflow(encoder, true);
                        }
                        *bytes++ = (byte)ch2;
                        --byteCount;
                    }
                    if (byteCount < charCount)
                    {
                        this.ThrowBytesOverflow(encoder, byteCount < 1);
                        charEnd = chars + byteCount;
                    }
                    while (chars < charEnd)
                    {
                        char ch3     = *chars++;
                        *    bytes++ = (int)ch3 < 128 ? (byte)ch3 : (byte)ch2;
                    }
                    if (encoder != null)
                    {
                        encoder.charLeftOver = char.MinValue;
                        encoder.m_charsUsed  = (int)(chars - charStart);
                    }
                    return((int)(bytes - numPtr1));
                }
            }
            byte *numPtr2 = bytes + byteCount;

            if ((int)ch1 > 0)
            {
                encoderFallbackBuffer = encoder.FallbackBuffer;
                encoderFallbackBuffer.InternalInitialize(chars, charEnd, encoder, true);
                encoderFallbackBuffer.InternalFallback(ch1, ref chars);
            }
            char ch4;

            while ((int)(ch4 = encoderFallbackBuffer == null ? char.MinValue : encoderFallbackBuffer.InternalGetNextChar()) != 0 || chars < charEnd)
            {
                if ((int)ch4 == 0)
                {
                    ch4    = *chars;
                    chars += 2;
                }
                if ((int)ch4 > (int)sbyte.MaxValue)
                {
                    if (encoderFallbackBuffer == null)
                    {
                        encoderFallbackBuffer = encoder != null ? encoder.FallbackBuffer : this.encoderFallback.CreateFallbackBuffer();
                        encoderFallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, true);
                    }
                    encoderFallbackBuffer.InternalFallback(ch4, ref chars);
                }
                else
                {
                    if (bytes >= numPtr2)
                    {
                        if (encoderFallbackBuffer == null || !encoderFallbackBuffer.bFallingBack)
                        {
                            chars -= 2;
                        }
                        else
                        {
                            encoderFallbackBuffer.MovePrevious();
                        }
                        this.ThrowBytesOverflow(encoder, bytes == numPtr1);
                        break;
                    }
                    *bytes = (byte)ch4;
                    ++bytes;
                }
            }
            if (encoder != null)
            {
                if (encoderFallbackBuffer != null && !encoderFallbackBuffer.bUsedEncoder)
                {
                    encoder.charLeftOver = char.MinValue;
                }
                encoder.m_charsUsed = (int)(chars - charStart);
            }
            return((int)(bytes - numPtr1));
        }