Beispiel #1
0
        public static void SkipLastField(ref ReadOnlySpan <byte> buffer, ref ParserInternalState state)
        {
            if (state.lastTag == 0)
            {
                throw new InvalidOperationException("SkipLastField cannot be called at the end of a stream");
            }
            switch (WireFormat.GetTagWireType(state.lastTag))
            {
            case WireFormat.WireType.StartGroup:
                SkipGroup(ref buffer, ref state, state.lastTag);
                break;

            case WireFormat.WireType.EndGroup:
                throw new InvalidProtocolBufferException(
                          "SkipLastField called on an end-group tag, indicating that the corresponding start-group was missing");

            case WireFormat.WireType.Fixed32:
                ParsingPrimitives.ParseRawLittleEndian32(ref buffer, ref state);
                break;

            case WireFormat.WireType.Fixed64:
                ParsingPrimitives.ParseRawLittleEndian64(ref buffer, ref state);
                break;

            case WireFormat.WireType.LengthDelimited:
                var length = ParsingPrimitives.ParseLength(ref buffer, ref state);
                ParsingPrimitives.SkipRawBytes(ref buffer, ref state, length);
                break;

            case WireFormat.WireType.Varint:
                ParsingPrimitives.ParseRawVarint32(ref buffer, ref state);
                break;
            }
        }
        internal static float?ReadFloatWrapperSlow(ref ReadOnlySpan <byte> buffer, ref ParserInternalState state)
        {
            int length = ParsingPrimitives.ParseLength(ref buffer, ref state);

            if (length == 0)
            {
                return(0F);
            }
            int   finalBufferPos = state.totalBytesRetired + state.bufferPos + length;
            float result         = 0F;

            do
            {
                // field=1, type=32-bit = tag of 13
                if (ParsingPrimitives.ParseTag(ref buffer, ref state) == 13)
                {
                    result = ParsingPrimitives.ParseFloat(ref buffer, ref state);
                }
                else
                {
                    ParsingPrimitivesMessages.SkipLastField(ref buffer, ref state);
                }
            }while (state.totalBytesRetired + state.bufferPos < finalBufferPos);
            return(result);
        }
        internal static ulong?ReadUInt64WrapperSlow(ref ReadOnlySpan <byte> buffer, ref ParserInternalState state)
        {
            // field=1, type=varint = tag of 8
            const int expectedTag = 8;
            int       length      = ParsingPrimitives.ParseLength(ref buffer, ref state);

            if (length == 0)
            {
                return(0L);
            }
            int   finalBufferPos = state.totalBytesRetired + state.bufferPos + length;
            ulong result         = 0L;

            do
            {
                if (ParsingPrimitives.ParseTag(ref buffer, ref state) == expectedTag)
                {
                    result = ParsingPrimitives.ParseRawVarint64(ref buffer, ref state);
                }
                else
                {
                    ParsingPrimitivesMessages.SkipLastField(ref buffer, ref state);
                }
            }while (state.totalBytesRetired + state.bufferPos < finalBufferPos);
            return(result);
        }
        internal static uint?ReadUInt32WrapperSlow(ref ReadOnlySpan <byte> buffer, ref ParserInternalState state)
        {
            int length = ParsingPrimitives.ParseLength(ref buffer, ref state);

            if (length == 0)
            {
                return(0);
            }
            int  finalBufferPos = state.totalBytesRetired + state.bufferPos + length;
            uint result         = 0;

            do
            {
                // field=1, type=varint = tag of 8
                if (ParsingPrimitives.ParseTag(ref buffer, ref state) == 8)
                {
                    result = ParsingPrimitives.ParseRawVarint32(ref buffer, ref state);
                }
                else
                {
                    ParsingPrimitivesMessages.SkipLastField(ref buffer, ref state);
                }
            }while (state.totalBytesRetired + state.bufferPos < finalBufferPos);
            return(result);
        }
        internal static double?ReadDoubleWrapperSlow(ref ReadOnlySpan <byte> buffer, ref ParserInternalState state)
        {
            int length = ParsingPrimitives.ParseLength(ref buffer, ref state);

            if (length == 0)
            {
                return(0D);
            }
            int    finalBufferPos = state.totalBytesRetired + state.bufferPos + length;
            double result         = 0D;

            do
            {
                // field=1, type=64-bit = tag of 9
                if (ParsingPrimitives.ParseTag(ref buffer, ref state) == 9)
                {
                    result = ParsingPrimitives.ParseDouble(ref buffer, ref state);
                }
                else
                {
                    ParsingPrimitivesMessages.SkipLastField(ref buffer, ref state);
                }
            }while (state.totalBytesRetired + state.bufferPos < finalBufferPos);
            return(result);
        }
Beispiel #6
0
        public static void ReadMessage(ref ParseContext ctx, IMessage message)
        {
            int length = ParsingPrimitives.ParseLength(ref ctx.buffer, ref ctx.state);

            if (ctx.state.recursionDepth >= ctx.state.recursionLimit)
            {
                throw InvalidProtocolBufferException.RecursionLimitExceeded();
            }
            int oldLimit = SegmentedBufferHelper.PushLimit(ref ctx.state, length);

            ++ctx.state.recursionDepth;

            ReadRawMessage(ref ctx, message);

            CheckReadEndOfStreamTag(ref ctx.state);
            // Check that we've read exactly as much data as expected.
            if (!SegmentedBufferHelper.IsReachedLimit(ref ctx.state))
            {
                throw InvalidProtocolBufferException.TruncatedMessage();
            }
            --ctx.state.recursionDepth;
            SegmentedBufferHelper.PopLimit(ref ctx.state, oldLimit);
        }
        public static ByteString ReadBytes(ref ReadOnlySpan <byte> buffer, ref ParserInternalState state)
        {
            int length = ParsingPrimitives.ParseLength(ref buffer, ref state);

            return(ByteString.AttachBytes(ParsingPrimitives.ReadRawBytes(ref buffer, ref state, length)));
        }
        public static string ReadString(ref ReadOnlySpan <byte> buffer, ref ParserInternalState state)
        {
            int length = ParsingPrimitives.ParseLength(ref buffer, ref state);

            return(ParsingPrimitives.ReadRawString(ref buffer, ref state, length));
        }