private bool ParsePreface(ReadOnlySequence <byte> readableBuffer, out SequencePosition consumed, out SequencePosition examined)
        {
            consumed = readableBuffer.Start;
            examined = readableBuffer.End;

            if (readableBuffer.Length < ClientPreface.Length)
            {
                return(false);
            }

            var span = readableBuffer.IsSingleSegment
                ? readableBuffer.First.Span
                : readableBuffer.ToSpan();

            for (var i = 0; i < ClientPreface.Length; i++)
            {
                if (ClientPreface[i] != span[i])
                {
                    throw new Http2ConnectionErrorException(CoreStrings.Http2ErrorInvalidPreface, Http2ErrorCode.PROTOCOL_ERROR);
                }
            }

            consumed = examined = readableBuffer.GetPosition(ClientPreface.Length);
            return(true);
        }
        private bool ParsePreface(ReadOnlySequence <byte> readableBuffer, out SequencePosition consumed, out SequencePosition examined)
        {
            consumed = readableBuffer.Start;
            examined = readableBuffer.End;

            if (readableBuffer.Length < ClientPreface.Length)
            {
                return(false);
            }

            var span = readableBuffer.IsSingleSegment
                ? readableBuffer.First.Span
                : readableBuffer.ToSpan();

            for (var i = 0; i < ClientPreface.Length; i++)
            {
                if (ClientPreface[i] != span[i])
                {
                    throw new Exception("Invalid HTTP/2 connection preface.");
                }
            }

            consumed = examined = readableBuffer.GetPosition(ClientPreface.Length);
            return(true);
        }
Exemple #3
0
        /// <summary>
        /// Check whether a signature is normalized (lower-S).
        /// </summary>
        /// <param name="vchSig"></param>
        /// <returns></returns>
        public static bool CheckLowS(ReadOnlySequence<byte> vchSig)
        {
            var sig = new byte[64];
            var input = vchSig.ToSpan();

            using (var secp256k1 = new Secp256k1()) {

                if (!secp256k1.SignatureParseDerLax(sig, input)) return false;
                return !secp256k1.SignatureNormalize(Span<byte>.Empty, input);
            }
        }
        public static IList <Proto2PeerSetting> ReadSettings(ReadOnlySequence <byte> payload)
        {
            var data = payload.ToSpan();

            Debug.Assert(data.Length % SettingSize == 0, "Invalid settings payload length");
            var settingsCount = data.Length / SettingSize;

            var settings = new Proto2PeerSetting[settingsCount];

            for (int i = 0; i < settings.Length; i++)
            {
                settings[i] = ReadSetting(data);
                data        = data.Slice(SettingSize);
            }
            return(settings);
        }
 public static string AsString(this ReadOnlySequence <byte> line, Encoding encoding)
 {
     return(encoding.GetString(line.ToSpan()));
 }
        public static bool TryParseUInt64(this ReadOnlySequence <byte> bufferSequence, out ulong value, out int consumed)
        {
            value    = default;
            consumed = default;
            SequencePosition position = default;

            // Fetch the first segment
            if (!bufferSequence.TryGet(ref position, out ReadOnlyMemory <byte> first))
            {
                return(false);
            }

            // Attempt to parse the first segment. If it works (and it should in most cases), then return success.
            bool parsed = Utf8Parser.TryParse(first.Span, out value, out consumed);

            if (parsed && consumed < first.Length)
            {
                return(true);
            }

            // Apparently the we need data from the second segment to succesfully parse, and so fetch the second segment.
            if (!bufferSequence.TryGet(ref position, out ReadOnlyMemory <byte> second))
            {
                // if there is no second segment and the first parsed succesfully, return the result of the parsing.
                if (parsed)
                {
                    return(true);
                }
                return(false);
            }

            // Combine the first, the second, and potentially more segments into a stack allocated buffer
            if (first.Length < StackBufferSize)
            {
                Span <byte> destination = stackalloc byte[StackBufferSize];

                first.Span.CopyTo(destination);
                var free = destination.Slice(first.Length);

                if (second.Length > free.Length)
                {
                    second = second.Slice(0, free.Length);
                }
                second.Span.CopyTo(free);
                free = free.Slice(second.Length);

                while (free.Length > 0)
                {
                    if (bufferSequence.TryGet(ref position, out ReadOnlyMemory <byte> next))
                    {
                        if (next.Length > free.Length)
                        {
                            next = next.Slice(0, free.Length);
                        }
                        next.Span.CopyTo(free);
                        free = free.Slice(next.Length);
                    }
                    else
                    {
                        break;
                    }
                }

                var combinedStackSpan = destination.Slice(0, StackBufferSize - free.Length);

                // if the stack allocated buffer parsed succesfully (and for uint it should always do), then return success.
                if (Utf8Parser.TryParse(combinedStackSpan, out value, out consumed))
                {
                    if (consumed < combinedStackSpan.Length || combinedStackSpan.Length < StackBufferSize)
                    {
                        return(true);
                    }
                }
            }

            // for invariant culture, we should never reach this point, as invariant uint text is never longer than 127 bytes.
            // I left this code here, as we will need it for custom cultures and possibly when we shrink the stack allocated buffer.
            var combinedSpan = bufferSequence.ToSpan();

            if (!Utf8Parser.TryParse(combinedSpan, out value, out consumed))
            {
                return(false);
            }
            return(true);
        }
Exemple #7
0
        private static ReadOnlySpan <byte> TrimSDPLineCR(ReadOnlySequence <byte> seq)
        {
            var line = seq.ToSpan();

            return(TrimSDPLineCR(line));
        }
Exemple #8
0
 public virtual string Encode(ReadOnlySequence <byte> bytes) => Encode(bytes.ToSpan());
Exemple #9
0
 public void NewToSpan()
 {
     var Result = Sequence.ToSpan();
 }