Example #1
0
        private Span Read(
            int tag)
        {
            Span span = Unsafe.Add(ref _stack0, _depth);
            ReadOnlySpan <byte> bytes = span.ApplyTo(_buffer);

            if (_failed || bytes.Length < 2 || bytes[0] != tag)
            {
                goto failed;
            }

            int start  = 2;
            int length = 0;

            if ((bytes[1] & ~0x7F) == 0)
            {
                length = bytes[1];
            }
            else
            {
                int count = bytes[1] & 0x7F;
                if (count < 1 || count > sizeof(int) || count > bytes.Length - 2 || bytes[2] == 0)
                {
                    goto failed;
                }
                while (count-- > 0)
                {
                    length = (length << 8) | bytes[start++];
                }
                if (length < 0x80)
                {
                    goto failed;
                }
            }

            if (length > bytes.Length - start)
            {
                goto failed;
            }

            Unsafe.Add(ref _stack0, _depth) = span.Slice(start + length);
            return(span.Slice(start, length));

failed:
            Fail();
            return(default);