示例#1
0
        public override int GetCharCount(byte[] bytes, int index, int count)
        {
            int byteEnd     = index + count;
            int outputChars = 0;

            while (index < byteEnd)
            {
                byte b = bytes[index];
#if FEATURE_ENCODING
                if (b > 0x7f)
                {
                    DecoderFallbackBuffer dfb = DecoderFallback.CreateFallbackBuffer();
                    try {
                        if (dfb.Fallback(new[] { b }, 0))
                        {
                            outputChars += dfb.Remaining;
                        }
                    } catch (DecoderFallbackException ex) {
                        var dfe = new DecoderFallbackException("ordinal out of range(128)", ex.BytesUnknown, ex.Index);
                        dfe.Data.Add("encoding", EncodingName);
                        throw dfe;
                    }
                }
                else
                {
                    outputChars++;
                }
#else
                outputChars++;
#endif
                index++;
            }
            return(outputChars);
        }
    private int GetCharCount(byte[] bytes, int index, int count, DecoderFallbackBuffer dfb)
    {
        int byteEnd     = index + count;
        int outputChars = 0;

        while (index < byteEnd)
        {
            byte b = bytes[index];
            if (b > 0x7f)
            {
                if (dfb == null)
                {
                    dfb = DecoderFallback.CreateFallbackBuffer();
                }
                try {
                    if (dfb.Fallback(new[] { b }, index))
                    {
                        outputChars += dfb.Remaining;
                        while (dfb.GetNextChar() != char.MinValue) /* empty */ } {
                }
            } catch (DecoderFallbackException ex) {
                var dfe = new DecoderFallbackException("ordinal not in range(128)", ex.BytesUnknown, ex.Index);
                dfe.Data.Add("encoding", EncodingName);
                throw dfe;
            }
        }
        else
        {
            outputChars++;
        }
        index++;
    }
    return(outputChars);
}
示例#3
0
        private void DecodeIfNeeded()
        {
            if (this._decoded != null)
            {
                return;
            }

            if (this._encoded == null)
            {
                return;
            }

            if (this._type != BinaryType.Unknwon)
            {
                return;
            }

            try
            {
                this._decoded = MessagePackConvert.DecodeStringStrict(this._encoded);
                this._type    = BinaryType.String;
            }
            catch (DecoderFallbackException ex)
            {
                this._decodingError = ex;
                this._type          = BinaryType.Blob;
            }
        }
示例#4
0
 // Copy constructor for debugger proxy
 private MessagePackString(MessagePackString other)
 {
     this._encoded       = other._encoded;
     this._decoded       = other._decoded;
     this._decodingError = other._decodingError;
     this._type          = other._type;
 }
示例#5
0
 public MessagePackString(byte[] encoded, bool isBinary)
 {
     this._encoded = encoded;
     this._type    = isBinary ? BinaryType.Blob : BinaryType.Unknwon;
     if (isBinary)
     {
         this._decodingError = IsBinary;
     }
 }
示例#6
0
        public static void Decode(byte[] metadata, NameValueCollection container)
        {
            if (metadata == null)
            {
                throw new ArgumentNullException("metadata");
            }
            container.Clear();
            string str = null;

            try
            {
                str = MetadataEncoding.CreateAsciiEncoder().GetString(metadata);
            }
            catch (DecoderFallbackException decoderFallbackException1)
            {
                DecoderFallbackException decoderFallbackException = decoderFallbackException1;
                string      str1             = string.Join(" ", Array.ConvertAll <byte, string>(decoderFallbackException.BytesUnknown, (byte b) => string.Format(CultureInfo.InvariantCulture, "{0:X2}", new object[] { b })));
                CultureInfo invariantCulture = CultureInfo.InvariantCulture;
                object[]    index            = new object[] { str1, decoderFallbackException.Index };
                throw new MetadataFormatException(string.Format(invariantCulture, "Failed to decode metadata because bytes at index {1} are not valid ASCII. Unknown bytes: '{0}'", index), decoderFallbackException);
            }
            using (StringReader stringReader = new StringReader(str))
            {
                string str2 = null;
                while (true)
                {
                    string str3 = stringReader.ReadLine();
                    str2 = str3;
                    if (str3 == null)
                    {
                        break;
                    }
                    str2 = str2.Trim();
                    if (str2.Length != 0)
                    {
                        char[]   chrArray  = new char[] { ':' };
                        string[] strArrays = str2.Split(chrArray, 2);
                        if ((int)strArrays.Length != 2)
                        {
                            CultureInfo cultureInfo = CultureInfo.InvariantCulture;
                            object[]    objArray    = new object[] { str2 };
                            throw new MetadataFormatException(string.Format(cultureInfo, "Metadata is incorrectly formatted: '{0}'", objArray));
                        }
                        string str4 = strArrays[0];
                        string str5 = strArrays[1];
                        if (str4.Length == 0)
                        {
                            CultureInfo invariantCulture1 = CultureInfo.InvariantCulture;
                            object[]    objArray1         = new object[] { str2 };
                            throw new MetadataFormatException(string.Format(invariantCulture1, "Metadata key is empty: '{0}'", objArray1));
                        }
                        container.Set(str4, str5);
                    }
                }
            }
        }
 public void Ctor_String_Exception(string message, Exception innerException)
 {
     DecoderFallbackException ex = new DecoderFallbackException(message, innerException);
     Assert.Null(ex.BytesUnknown);
     Assert.Equal(0, ex.Index);
     Assert.Null(ex.StackTrace);
     Assert.Equal(0, ex.Data.Count);
     Assert.Same(innerException, ex.InnerException);
     Assert.Equal(message, ex.Message);
 }
 public void Ctor_String(string message)
 {
     DecoderFallbackException decoderFallbackException = new DecoderFallbackException(message);
     Assert.Null(decoderFallbackException.BytesUnknown);
     Assert.Equal(0, decoderFallbackException.Index);
     Assert.Null(decoderFallbackException.StackTrace);
     Assert.Null(decoderFallbackException.InnerException);
     Assert.Equal(0, decoderFallbackException.Data.Count);
     Assert.Equal(message, decoderFallbackException.Message);
 }
        public void Ctor_Empty()
        {
            DecoderFallbackException decoderFallbackException = new DecoderFallbackException();
            Assert.Null(decoderFallbackException.BytesUnknown);
            Assert.Equal(0, decoderFallbackException.Index);
            Assert.Null(decoderFallbackException.StackTrace);
            Assert.Null(decoderFallbackException.InnerException);
            Assert.Equal(0, decoderFallbackException.Data.Count);

            Assert.Equal(new ArgumentException().Message, decoderFallbackException.Message);
        }
        public void Ctor_String_Exception(string message, Exception innerException)
        {
            DecoderFallbackException ex = new DecoderFallbackException(message, innerException);

            Assert.Null(ex.BytesUnknown);
            Assert.Equal(0, ex.Index);
            Assert.Null(ex.StackTrace);
            Assert.Equal(0, ex.Data.Count);
            Assert.Same(innerException, ex.InnerException);
            Assert.Equal(message, ex.Message);
        }
示例#11
0
        public MessagePackString(byte[] encoded, bool isBinary)
        {
            Contract.Assert(encoded != null, "encoded != null");

            this._encoded = encoded;
            this._type    = isBinary ? BinaryType.Blob : BinaryType.Unknwon;
            if (isBinary)
            {
                this._decodingError = IsBinary;
            }
        }
 public static Exception /*!*/ CreateTranscodingError(DecoderFallbackException /*!*/ e, RubyEncoding /*!*/ fromEncoding, RubyEncoding /*!*/ toEncoding)
 {
     throw new UndefinedConversionError(
               FormatMessage(
                   "\"{0}\" to {2} in conversion from {1} to UTF-8 to {2}",
                   BitConverter.ToString(e.BytesUnknown),
                   fromEncoding,
                   toEncoding
                   )
               );
 }
        public void Ctor_String(string message)
        {
            DecoderFallbackException decoderFallbackException = new DecoderFallbackException(message);

            Assert.Null(decoderFallbackException.BytesUnknown);
            Assert.Equal(0, decoderFallbackException.Index);
            Assert.Null(decoderFallbackException.StackTrace);
            Assert.Null(decoderFallbackException.InnerException);
            Assert.Equal(0, decoderFallbackException.Data.Count);
            Assert.Equal(message, decoderFallbackException.Message);
        }
        public void Ctor_Empty()
        {
            DecoderFallbackException decoderFallbackException = new DecoderFallbackException();

            Assert.Null(decoderFallbackException.BytesUnknown);
            Assert.Equal(0, decoderFallbackException.Index);
            Assert.Null(decoderFallbackException.StackTrace);
            Assert.Null(decoderFallbackException.InnerException);
            Assert.Equal(0, decoderFallbackException.Data.Count);

            Assert.Equal(new ArgumentException().Message, decoderFallbackException.Message);
        }
示例#15
0
        public MessagePackString(byte[] encoded, bool isBinary)
        {
#if !UNITY
            Contract.Assert(encoded != null);
#endif // !UNITY
            this._encoded = encoded;
            this._type    = isBinary ? BinaryType.Blob : BinaryType.Unknwon;
            if (isBinary)
            {
                this._decodingError = IsBinary;
            }
        }
        public void Ctor()
        {
            DecoderFallbackException ex = new DecoderFallbackException();
            Assert.Null(ex.BytesUnknown);
            Assert.Equal(default(int), ex.Index);
            Assert.Null(ex.StackTrace);
            Assert.Null(ex.InnerException);
            Assert.Equal(0, ex.Data.Count);
            ArgumentException arg = new ArgumentException();

            Assert.Equal(ex.Message, arg.Message);
        }
        public void Ctor()
        {
            DecoderFallbackException ex = new DecoderFallbackException();

            Assert.Null(ex.BytesUnknown);
            Assert.Equal(default(int), ex.Index);
            Assert.Null(ex.StackTrace);
            Assert.Null(ex.InnerException);
            Assert.Equal(0, ex.Data.Count);
            ArgumentException arg = new ArgumentException();

            Assert.Equal(ex.Message, arg.Message);
        }
        public void Ctor2()
        {
            string message = "Test message.";
            DecoderFallbackException ex = new DecoderFallbackException(message);
            Assert.Null(ex.BytesUnknown);
            Assert.Equal(default(int), ex.Index);
            Assert.Null(ex.StackTrace);
            Assert.Null(ex.InnerException);
            Assert.Equal(0, ex.Data.Count);
            Assert.Equal(ex.Message, message);

            message = "";
            ex = new DecoderFallbackException(message);
            Assert.Null(ex.BytesUnknown);
            Assert.Equal(default(int), ex.Index);
            Assert.Equal(message, ex.Message);
        }
        public void Ctor2()
        {
            string message = "Test message.";
            DecoderFallbackException ex = new DecoderFallbackException(message);

            Assert.Null(ex.BytesUnknown);
            Assert.Equal(default(int), ex.Index);
            Assert.Null(ex.StackTrace);
            Assert.Null(ex.InnerException);
            Assert.Equal(0, ex.Data.Count);
            Assert.Equal(ex.Message, message);

            message = "";
            ex      = new DecoderFallbackException(message);
            Assert.Null(ex.BytesUnknown);
            Assert.Equal(default(int), ex.Index);
            Assert.Equal(message, ex.Message);
        }
示例#20
0
private int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, DecoderFallbackBuffer dfb)
{
    int byteEnd     = byteIndex + byteCount;
    int outputChars = 0;

    while (byteIndex < byteEnd)
    {
        byte b = bytes[byteIndex];
#if FEATURE_ENCODING
        if (b > 0x7f)
        {
            if (dfb == null)
            {
                dfb = DecoderFallback.CreateFallbackBuffer();
            }
            try {
                if (dfb.Fallback(new[] { b }, byteIndex))
                {
                    while (dfb.Remaining != 0)
                    {
                        chars[charIndex++] = dfb.GetNextChar();
                        outputChars++;
                    }
                }
            } catch (DecoderFallbackException ex) {
                var dfe = new DecoderFallbackException("ordinal not in range(128)", ex.BytesUnknown, ex.Index);
                dfe.Data.Add("encoding", EncodingName);
                throw dfe;
            }
        }
        else
        {
            chars[charIndex++] = (char)b;
            outputChars++;
        }
#else
        chars[charIndex++] = (char)b;
        outputChars++;
#endif
        byteIndex++;
    }
    return(outputChars);
}
        public void Ctor3()
        {
            string message = "Test message.";
            string innerMsg = "Invalid Op Message.";
            Exception innerException = new InvalidOperationException(innerMsg);
            DecoderFallbackException ex = new DecoderFallbackException(message, innerException);
            Assert.Null(ex.BytesUnknown);
            Assert.Equal(default(int), ex.Index);
            Assert.Null(ex.StackTrace);
            Assert.Equal(0, ex.Data.Count);
            Assert.Equal(innerException, ex.InnerException);
            Assert.Equal(innerMsg, ex.InnerException.Message);
            Assert.Equal(message, ex.Message);

            message = "";
            ex = new DecoderFallbackException(message, null);
            Assert.Null(ex.BytesUnknown);
            Assert.Equal(default(int), ex.Index);
            Assert.Equal(message, ex.Message);
            Assert.Null(ex.InnerException);
        }
        public void Ctor3()
        {
            string    message           = "Test message.";
            string    innerMsg          = "Invalid Op Message.";
            Exception innerException    = new InvalidOperationException(innerMsg);
            DecoderFallbackException ex = new DecoderFallbackException(message, innerException);

            Assert.Null(ex.BytesUnknown);
            Assert.Equal(default(int), ex.Index);
            Assert.Null(ex.StackTrace);
            Assert.Equal(0, ex.Data.Count);
            Assert.Equal(innerException, ex.InnerException);
            Assert.Equal(innerMsg, ex.InnerException.Message);
            Assert.Equal(message, ex.Message);

            message = "";
            ex      = new DecoderFallbackException(message, null);
            Assert.Null(ex.BytesUnknown);
            Assert.Equal(default(int), ex.Index);
            Assert.Equal(message, ex.Message);
            Assert.Null(ex.InnerException);
        }
示例#23
0
        public static void DecoderFallbackExceptionIndexTests(byte[] firstPayload, byte[] secondPayload, int expectedIndex)
        {
            UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);

            // First test GetChars / GetChars

            Decoder decoder = encoding.GetDecoder();

            Assert.Equal(0, decoder.GetChars(firstPayload, 0, firstPayload.Length, new char[0], 0, flush: false));

            DecoderFallbackException ex = Assert.Throws <DecoderFallbackException>(
                () => decoder.GetChars(secondPayload, 0, secondPayload.Length, new char[8], 0, flush: true));

            Assert.Equal(expectedIndex, ex.Index);

            // Then test GetChars / GetCharCount

            decoder = encoding.GetDecoder();
            Assert.Equal(0, decoder.GetChars(firstPayload, 0, firstPayload.Length, new char[0], 0, flush: false));

            ex = Assert.Throws <DecoderFallbackException>(
                () => decoder.GetCharCount(secondPayload, 0, secondPayload.Length, flush: true));
            Assert.Equal(expectedIndex, ex.Index);
        }
示例#24
0
 private static IActionResult OnDecoderException(DecoderFallbackException ex)
 {
     return(ErrorResult(400, new ErrorDto {
         Message = ex.Message
     }));
 }
示例#25
0
 public static InvalidOperationException GetInvalidOperationException_ReadInvalidUTF8(DecoderFallbackException innerException)
 {
     return(GetInvalidOperationException(SR.CannotTranscodeInvalidUtf8, innerException));
 }
示例#26
0
 private static ErrorDto OnDecoderException(DecoderFallbackException ex)
 {
     return(new ErrorDto {
         StatusCode = 400, Message = ex.Message
     });
 }
 internal CursivelyHeadersAreNotUTF8Exception(DecoderFallbackException innerException)
     : base(innerException.Message, innerException)
 {
 }
示例#28
0
 public static Exception /*!*/ CreateArgumentError(DecoderFallbackException /*!*/ e, RubyEncoding /*!*/ encoding)
 {
     return(RubyExceptions.CreateArgumentError(String.Format("invalid byte sequence {0} in {1}",
                                                             BitConverter.ToString(e.BytesUnknown), encoding)));
 }
 public static Exception /*!*/ CreateInvalidByteSequenceError(DecoderFallbackException /*!*/ e, RubyEncoding /*!*/ encoding)
 {
     return(new InvalidByteSequenceError(
                FormatMessage("invalid byte sequence {0} on {1}", BitConverter.ToString(e.BytesUnknown), encoding)
                ));
 }