public void Defaults ()
		{
			DecoderReplacementFallback f =
				new DecoderReplacementFallback ();
			Assert.AreEqual ("?", f.DefaultString, "#1");
			Assert.AreEqual (1, f.MaxCharCount, "#2");

			f = new DecoderReplacementFallback (String.Empty);
			Assert.AreEqual (String.Empty, f.DefaultString, "#3");
			Assert.AreEqual (0, f.MaxCharCount, "#4");

			f = Encoding.UTF8.DecoderFallback as DecoderReplacementFallback;
			Assert.IsNotNull (f, "#5");
			// This behavior was introduced as
			// http://support.microsoft.com/kb/940521/
			Assert.AreEqual ("\uFFFD", f.DefaultString, "#6");
			Assert.AreEqual (1, f.MaxCharCount, "#7");

			// after beta2 this test became invalid.
			//f = new MyEncoding ().DecoderFallback as DecoderReplacementFallback;
			//Assert.IsNotNull (f, "#8");
			//Assert.AreEqual (String.Empty, f.DefaultString, "#9");
			//Assert.AreEqual (0, f.MaxCharCount, "#10");

			f = DecoderFallback.ReplacementFallback as DecoderReplacementFallback;
			Assert.AreEqual ("?", f.DefaultString, "#11");
			Assert.AreEqual (1, f.MaxCharCount, "#12");
		}
Ejemplo n.º 2
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);
        }
        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)));
        }
 // Construction
 public DecoderReplacementFallbackBuffer(DecoderReplacementFallback fallback)
 {
     this.strDefault = fallback.DefaultString;
 }
Ejemplo n.º 5
0
			public override DecoderFallbackBuffer CreateFallbackBuffer ()
			{
				fallback = new DecoderReplacementFallback ();
				return new DecoderTestFallbackBuffer (fallback, fallbackAction);
			}
Ejemplo n.º 6
0
        [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, "[ASCIIEncoding.GetCharCount]bytes is null");
            Contract.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;
                Contract.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
            Contract.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0,
                            "[ASCIIEncoding.GetCharCount]Expected Empty fallback buffer");

            // Converted sequence is same length as input
            return(charCount);
        }
Ejemplo n.º 7
0
        /// <summary>Indicates whether the value of a specified object is equal to the <see cref="T:System.Text.DecoderReplacementFallback" /> object.</summary>
        /// <returns>true if <paramref name="value" /> is a <see cref="T:System.Text.DecoderReplacementFallback" /> object having a <see cref="P:System.Text.DecoderReplacementFallback.DefaultString" /> property that is equal to the <see cref="P:System.Text.DecoderReplacementFallback.DefaultString" /> property of the current <see cref="T:System.Text.DecoderReplacementFallback" /> object; otherwise, false. </returns>
        /// <param name="value">A <see cref="T:System.Text.DecoderReplacementFallback" /> object.</param>
        /// <filterpriority>2</filterpriority>
        public override bool Equals(object value)
        {
            DecoderReplacementFallback decoderReplacementFallback = value as DecoderReplacementFallback;

            return(decoderReplacementFallback != null && this.replacement == decoderReplacementFallback.replacement);
        }
 // Construction
 public DecoderReplacementFallbackBuffer(DecoderReplacementFallback fallback)
 {
     // TODO-NULLABLE: NullReferenceException (fallback)
     _strDefault = fallback.DefaultString;
 }
Ejemplo n.º 9
0
        public override bool Equals(object value)
        {
            DecoderReplacementFallback f = value as DecoderReplacementFallback;

            return(f != null && replacement == f.replacement);
        }
Ejemplo n.º 10
0
        public static Encoding GetEncoding(string charset, string fallback)
        {
            int codepage;

            if (charset == null)
                throw new ArgumentNullException ("charset");

            if (fallback == null)
                throw new ArgumentNullException ("fallback");

            if ((codepage = GetCodePage (charset)) == -1)
                throw new NotSupportedException ();

            var encoderFallback = new EncoderReplacementFallback (fallback);
            var decoderFallback = new DecoderReplacementFallback (fallback);

            return Encoding.GetEncoding (codepage, encoderFallback, decoderFallback);
        }
Ejemplo n.º 11
0
        public static Encoding GetEncoding(int codepage, string fallback)
        {
            if (fallback == null)
                throw new ArgumentNullException ("fallback");

            var encoderFallback = new EncoderReplacementFallback (fallback);
            var decoderFallback = new DecoderReplacementFallback (fallback);

            return Encoding.GetEncoding (codepage, encoderFallback, decoderFallback);
        }
Ejemplo n.º 12
0
 public DecoderReplacementFallbackBuffer(
     DecoderReplacementFallback fallback)
 {
     if (fallback == null)
         throw new ArgumentNullException("fallback");
     replacement = fallback.DefaultString;
     current = 0;
 }
Ejemplo n.º 13
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));
        }
 public DecoderReplacementFallbackBuffer(DecoderReplacementFallback fallback)
 {
   Contract.Requires(fallback != null);
 }
 // Construction
 public DecoderReplacementFallbackBuffer(DecoderReplacementFallback fallback)
 {
     _strDefault = fallback.DefaultString;
 }
Ejemplo n.º 16
0
 /// <summary>Initializes a new instance of the <see cref="T:System.Text.DecoderReplacementFallbackBuffer" /> class using the value of a <see cref="T:System.Text.DecoderReplacementFallback" /> object.</summary><param name="fallback">A <see cref="T:System.Text.DecoderReplacementFallback" /> object that contains a replacement string. </param>
 public DecoderReplacementFallbackBuffer(DecoderReplacementFallback fallback)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 17
0
        [System.Security.SecurityCritical]  // auto-generated
        public 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, "[SBCSCodePageEncoding.GetChars]bytes is null");
            Debug.Assert(byteCount >= 0, "[SBCSCodePageEncoding.GetChars]byteCount is negative");
            Debug.Assert(chars != null, "[SBCSCodePageEncoding.GetChars]chars is null");
            Debug.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    = DecoderFallback as DecoderReplacementFallback;
                bUseBestFit = DecoderFallback is InternalDecoderBestFitFallback;
            }
            else
            {
                fallback    = decoder.Fallback as DecoderReplacementFallback;
                bUseBestFit = decoder.Fallback is InternalDecoderBestFitFallback;
                Debug.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 always 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;

            DecoderFallbackBufferHelper fallbackHelper = new DecoderFallbackBufferHelper(
                decoder != null ? decoder.FallbackBuffer : DecoderFallback.CreateFallbackBuffer());

            // 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 = DecoderFallback.CreateFallbackBuffer();
                        }
                        else
                        {
                            fallbackBuffer = decoder.FallbackBuffer;
                        }

                        fallbackHelper = new DecoderFallbackBufferHelper(fallbackBuffer);

                        fallbackHelper.InternalInitialize(byteEnd - byteCount, charEnd);
                    }

                    // Use fallback buffer
                    Debug.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 (!fallbackHelper.InternalFallback(byteBuffer, bytes, ref chars))
                    {
                        // May or may not throw, but we didn't get this byte
                        bytes--;                                            // unused byte
                        fallbackHelper.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)
                    {
                        Debug.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
            Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0,
                         "[SBCSEncoding.GetChars]Expected Empty fallback buffer at end");

            return((int)(chars - charStart));
        }
Ejemplo n.º 18
0
        public override bool Equals(object value)
        {
            DecoderReplacementFallback fallback = value as DecoderReplacementFallback;

            return((fallback != null) && (this.strDefault == fallback.strDefault));
        }
Ejemplo n.º 19
0
        [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, "[ASCIIEncoding.GetChars]bytes is null");
            Contract.Assert(byteCount >= 0, "[ASCIIEncoding.GetChars]byteCount is negative");
            Contract.Assert(chars != null, "[ASCIIEncoding.GetChars]chars is null");
            Contract.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;

            if (decoder == null)
            {
                fallback = this.DecoderFallback as DecoderReplacementFallback;
            }
            else
            {
                fallback = decoder.Fallback as DecoderReplacementFallback;
                Contract.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
                    if (!fallbackBuffer.InternalFallback(byteBuffer, bytes, ref chars))
                    {
                        // May or may not throw, but we didn't get this byte
                        Contract.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)
                    {
                        Contract.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
            Contract.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0,
                            "[ASCIIEncoding.GetChars]Expected Empty fallback buffer");

            return((int)(chars - charStart));
        }
Ejemplo n.º 20
0
 public DecoderReplacementFallbackBuffer(DecoderReplacementFallback fallback)
 {
     Contract.Requires(fallback != null);
 }
        [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);
        }
Ejemplo n.º 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)));
        }
		public void Reset ()
		{
			DecoderReplacementFallback f = new DecoderReplacementFallback ("X");
			DecoderReplacementFallbackBuffer b = new DecoderReplacementFallbackBuffer (f);
			b.Fallback (new byte [0], 0);
			Assert.AreEqual (1, b.Remaining, "#1");
			b.Reset ();
			Assert.AreEqual (0, b.Remaining, "#2");
			b.Fallback (new byte [0], 0); // do not raise an error
			b.Reset ();
			Assert.AreEqual (0, (int) b.GetNextChar (), "#3");
		}
Ejemplo n.º 24
0
			public DecoderTestFallbackBuffer (DecoderReplacementFallback fallback, FallbackDelegate fallbackAction)
			{
				this.fallbackAction = fallbackAction;
				buffer = new DecoderReplacementFallbackBuffer (fallback);
			}