示例#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 (!_map.Mapping.TryGetValue(b, out _))
                {
                    DecoderFallbackBuffer dfb = DecoderFallback.CreateFallbackBuffer();
                    if (dfb.Fallback(new[] { b }, 0))
                    {
                        outputChars += dfb.Remaining;
                    }
                }
                else
                {
                    outputChars++;
                }
                index++;
            }
            return(outputChars);
        }
示例#2
0
        public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
        {
            int byteEnd     = byteIndex + byteCount;
            int outputChars = 0;

            while (byteIndex < byteEnd)
            {
                byte b = bytes[byteIndex];
                char val;

                if (!_map.Mapping.TryGetValue(b, out val))
                {
                    DecoderFallbackBuffer dfb = DecoderFallback.CreateFallbackBuffer();
                    if (dfb.Fallback(new[] { b }, 0))
                    {
                        while (dfb.Remaining != 0)
                        {
                            chars[charIndex++] = (char)((int)_map.Mapping[(int)dfb.GetNextChar()]);
                            outputChars++;
                        }
                    }
                }
                else
                {
                    chars[charIndex++] = val;
                    outputChars++;
                }
                byteIndex++;
            }
            return(outputChars);
        }
示例#3
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);
        }
示例#4
0
        public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
        {
            int byteEnd     = byteIndex + byteCount;
            int outputChars = 0;

            while (byteIndex < byteEnd)
            {
                byte b = bytes[byteIndex];
                if (b > 0x7f)
                {
                    DecoderFallbackBuffer dfb = DecoderFallback.CreateFallbackBuffer();
                    if (dfb.Fallback(new byte[] { b }, byteIndex))
                    {
                        while (dfb.Remaining != 0)
                        {
                            chars[charIndex++] = dfb.GetNextChar();
                            outputChars++;
                        }
                    }
                }
                else
                {
                    chars[charIndex++] = (char)b;
                    outputChars++;
                }
                byteIndex++;
            }
            return(outputChars);
        }
示例#5
0
        protected override int GetChars(ReadOnlySpan <byte> bytes, Span <char> chars, bool write)
        {
            if (bytes.IsEmpty)
            {
                return(0);
            }

            for (int i = 0; i < bytes.Length; i++)
            {
                byte b = bytes[i];

                if ((uint)b >= (uint)_isAllowed.Length || !_isAllowed[b])
                {
                    DecoderFallback.CreateFallbackBuffer().Fallback(
                        new[] { b },
                        i);

                    Debug.Fail("Fallback should have thrown");
                    throw new CryptographicException();
                }

                if (write)
                {
                    chars[i] = (char)b;
                }
            }

            return(bytes.Length);
        }
示例#6
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();
                    if (dfb.Fallback(new[] { b }, 0))
                    {
                        outputChars += dfb.Remaining;
                    }
                }
                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);
}
示例#8
0
        public override DecoderFallbackBuffer CreateFallbackBuffer()
        {
            // called by the decoder when it encounters
            // the first byte that it is unable to successfully decode

            WasTriggered = true;

            return(decoder_.CreateFallbackBuffer());
        }
示例#9
0
        void DecodeInvalidBytes(Stream stream, Action <Stream, string> decodedText, params byte[] data)
        {
            DecoderFallbackBuffer fallback = DecoderFallback.CreateFallbackBuffer();
            bool result = fallback.Fallback(data, 0);

            while (result && fallback.Remaining > 0)
            {
                decodedText(stream, $"{fallback.GetNextChar()}");
            }
        }
示例#10
0
        private int GetChars(byte[] bytes, int byteIndex, int byteCount,
                             char[] chars, int charIndex,
                             ref DecoderFallbackBuffer buffer)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes");
            }
            if (chars == null)
            {
                throw new ArgumentNullException("chars");
            }
            if (byteIndex < 0 || byteIndex > bytes.Length)
            {
                throw new ArgumentOutOfRangeException("byteIndex", "ArgRange_Array");
            }
            if (byteCount < 0 || byteCount > (bytes.Length - byteIndex))
            {
                throw new ArgumentOutOfRangeException("byteCount", "ArgRange_Array");
            }
            if (charIndex < 0 || charIndex > chars.Length)
            {
                throw new ArgumentOutOfRangeException("charIndex", "ArgRange_Array");
            }

            if ((chars.Length - charIndex) < byteCount)
            {
                throw new ArgumentException("Arg_InsufficientSpace");
            }

            int count = byteCount;

            while (count-- > 0)
            {
                char c = (char)bytes[byteIndex++];
                if (c < '\x80')
                {
                    chars[charIndex++] = c;
                }
                else
                {
                    if (buffer == null)
                    {
                        buffer = DecoderFallback.CreateFallbackBuffer();
                    }
                    buffer.Fallback(bytes, byteIndex);
                    while (buffer.Remaining > 0)
                    {
                        chars[charIndex++] = buffer.GetNextChar();
                    }
                }
            }
            return(byteCount);
        }
示例#11
0
        public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
        {
            int byteEnd     = byteIndex + byteCount;
            int outputChars = 0;

            while (byteIndex < byteEnd)
            {
                byte   b = bytes[byteIndex];
                object val;
                object obj = ScriptingRuntimeHelpers.Int32ToObject((int)b);

                if (!_map.TryGetValue(obj, out val) || val == null)
                {
                    DecoderFallbackBuffer dfb = DecoderFallback.CreateFallbackBuffer();
                    if (dfb.Fallback(new[] { b }, 0))
                    {
                        while (dfb.Remaining != 0)
                        {
                            chars[charIndex++] = dfb.GetNextChar();
                            outputChars++;
                        }
                    }
                }
                else if (val is string)
                {
                    string v = val as string;
                    for (int i = 0; i < v.Length; i++)
                    {
                        chars[charIndex++] = v[i];
                        outputChars++;
                    }
                }
                else if (val is int)
                {
                    chars[charIndex++] = (char)(int)val;
                    outputChars++;
                }
                else
                {
                    throw PythonOps.TypeError("charmap must be an int, str, or None");
                }
                byteIndex++;
            }
            return(outputChars);
        }
示例#12
0
        protected override int GetChars(ReadOnlySpan <byte> bytes, Span <char> chars, bool write)
        {
            if (bytes.IsEmpty)
            {
                return(0);
            }

            if (bytes.Length % 2 != 0)
            {
                DecoderFallback.CreateFallbackBuffer().Fallback(
                    bytes.Slice(bytes.Length - 1).ToArray(),
                    bytes.Length - 1);

                Debug.Fail("Fallback should have thrown");
                throw new CryptographicException();
            }

            int writeIdx = 0;

            for (int i = 0; i < bytes.Length; i += 2)
            {
                int  val = bytes[i] << 8 | bytes[i + 1];
                char c   = (char)val;

                if (char.IsSurrogate(c))
                {
                    DecoderFallback.CreateFallbackBuffer().Fallback(
                        bytes.Slice(i, 2).ToArray(),
                        i);

                    Debug.Fail("Fallback should have thrown");
                    throw new CryptographicException();
                }

                if (write)
                {
                    chars[writeIdx] = c;
                }

                writeIdx++;
            }

            return(writeIdx);
        }
示例#13
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);
}
        protected override int GetChars(ReadOnlySpan <byte> bytes, Span <char> chars, bool write)
        {
            if (bytes.IsEmpty)
            {
                return(0);
            }

            if (bytes.Length % 2 != 0)
            {
                DecoderFallback.CreateFallbackBuffer().Fallback(
                    bytes.Slice(bytes.Length - 1).ToArray(),
                    bytes.Length - 1);

                Debug.Fail("Fallback should have thrown");
                throw new InvalidOperationException();
            }

            int writeIdx = 0;

            for (int i = 0; i < bytes.Length; i += 2)
            {
                char c = (char)BinaryPrimitives.ReadInt16BigEndian(bytes.Slice(i));

                if (char.IsSurrogate(c))
                {
                    DecoderFallback.CreateFallbackBuffer().Fallback(
                        bytes.Slice(i, 2).ToArray(),
                        i);

                    Debug.Fail("Fallback should have thrown");
                    throw new InvalidOperationException();
                }

                if (write)
                {
                    chars[writeIdx] = c;
                }

                writeIdx++;
            }

            return(writeIdx);
        }
示例#15
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];

                object val;
                object byteObj = ScriptingRuntimeHelpers.Int32ToObject((int)b);

                if (!_map.TryGetValue(byteObj, out val) || val == null)
                {
                    DecoderFallbackBuffer dfb = DecoderFallback.CreateFallbackBuffer();
                    if (dfb.Fallback(new[] { b }, 0))
                    {
                        outputChars += dfb.Remaining;
                    }
                }
                else if (val is string)
                {
                    outputChars += ((string)val).Length;
                }
                else if (val is int)
                {
                    outputChars++;
                }
                else
                {
                    throw PythonOps.TypeError("charmap must be an int, str, or None");
                }
                index++;
            }
            return(outputChars);
        }