示例#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;
            }
        }
示例#2
0
        /// <summary>
        /// Parse a single field from <paramref name="ctx"/> and merge it
        /// into this set.
        /// </summary>
        /// <param name="ctx">The parse context from which to read the field</param>
        /// <returns>false if the tag is an "end group" tag, true otherwise</returns>
        private bool MergeFieldFrom(ref ParseContext ctx)
        {
            uint tag    = ctx.LastTag;
            int  number = WireFormat.GetTagFieldNumber(tag);

            switch (WireFormat.GetTagWireType(tag))
            {
            case WireFormat.WireType.Varint:
            {
                ulong uint64 = ctx.ReadUInt64();
                GetOrAddField(number).AddVarint(uint64);
                return(true);
            }

            case WireFormat.WireType.Fixed32:
            {
                uint uint32 = ctx.ReadFixed32();
                GetOrAddField(number).AddFixed32(uint32);
                return(true);
            }

            case WireFormat.WireType.Fixed64:
            {
                ulong uint64 = ctx.ReadFixed64();
                GetOrAddField(number).AddFixed64(uint64);
                return(true);
            }

            case WireFormat.WireType.LengthDelimited:
            {
                ByteString bytes = ctx.ReadBytes();
                GetOrAddField(number).AddLengthDelimited(bytes);
                return(true);
            }

            case WireFormat.WireType.StartGroup:
            {
                UnknownFieldSet set = new UnknownFieldSet();
                ParsingPrimitivesMessages.ReadGroup(ref ctx, number, set);
                GetOrAddField(number).AddGroup(set);
                return(true);
            }

            case WireFormat.WireType.EndGroup:
            {
                return(false);
            }

            default:
                throw InvalidProtocolBufferException.InvalidWireType();
            }
        }
示例#3
0
        /// <summary>
        /// Skip a group.
        /// </summary>
        public static void SkipGroup(ref ReadOnlySpan <byte> buffer, ref ParserInternalState state, uint startGroupTag)
        {
            // Note: Currently we expect this to be the way that groups are read. We could put the recursion
            // depth changes into the ReadTag method instead, potentially...
            state.recursionDepth++;
            if (state.recursionDepth >= state.recursionLimit)
            {
                throw InvalidProtocolBufferException.RecursionLimitExceeded();
            }
            uint tag;

            while (true)
            {
                tag = ParsingPrimitives.ParseTag(ref buffer, ref state);
                if (tag == 0)
                {
                    throw InvalidProtocolBufferException.TruncatedMessage();
                }
                // Can't call SkipLastField for this case- that would throw.
                if (WireFormat.GetTagWireType(tag) == WireFormat.WireType.EndGroup)
                {
                    break;
                }
                // This recursion will allow us to handle nested groups.
                SkipLastField(ref buffer, ref state);
            }
            int startField = WireFormat.GetTagFieldNumber(startGroupTag);
            int endField   = WireFormat.GetTagFieldNumber(tag);

            if (startField != endField)
            {
                throw new InvalidProtocolBufferException(
                          $"Mismatched end-group tag. Started with field {startField}; ended with field {endField}");
            }
            state.recursionDepth--;
        }