GetTagFieldNumber() public static method

Given a tag value, determines the field number (the upper 29 bits).
public static GetTagFieldNumber ( uint tag ) : int
tag uint
return int
Ejemplo n.º 1
0
        /// <summary>
        /// Tries to merge a field from the coded input, returning true if the field was merged.
        /// If the set is null or the field was not otherwise merged, this returns false.
        /// </summary>
        public static bool TryMergeFieldFrom <TTarget>(ref ExtensionSet <TTarget> set, CodedInputStream stream) where TTarget : IExtendableMessage <TTarget>
        {
            Extension extension;
            int       lastFieldNumber = WireFormat.GetTagFieldNumber(stream.LastTag);

            IExtensionValue extensionValue;

            if (set != null && set.ValuesByNumber.TryGetValue(lastFieldNumber, out extensionValue))
            {
                extensionValue.MergeFrom(stream);
                return(true);
            }
            else if (stream.ExtensionRegistry != null && stream.ExtensionRegistry.ContainsInputField(stream, typeof(TTarget), out extension))
            {
                IExtensionValue value = extension.CreateValue();
                value.MergeFrom(stream);
                set = (set ?? new ExtensionSet <TTarget>());
                set.ValuesByNumber.Add(extension.FieldNumber, value);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        private void SkipGroup(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...
            recursionDepth++;
            if (recursionDepth >= recursionLimit)
            {
                throw InvalidProtocolBufferException.RecursionLimitExceeded();
            }
            uint tag;

            while (true)
            {
                tag = ReadTag();
                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();
            }
            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);
            }
            recursionDepth--;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Parse a single field from <paramref name="input"/> and merge it
        /// into this set.
        /// </summary>
        /// <param name="input">The coded input stream containing the field</param>
        /// <returns>false if the tag is an "end group" tag, true otherwise</returns>
        private bool MergeFieldFrom(CodedInputStream input)
        {
            uint tag    = input.LastTag;
            int  number = WireFormat.GetTagFieldNumber(tag);

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

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

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

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

            case WireFormat.WireType.StartGroup:
            {
                uint            endTag = WireFormat.MakeTag(number, WireFormat.WireType.EndGroup);
                UnknownFieldSet set    = new UnknownFieldSet();
                while (input.ReadTag() != endTag)
                {
                    set.MergeFieldFrom(input);
                }
                GetOrAddField(number).AddGroup(set);
                return(true);
            }

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

            default:
                throw InvalidProtocolBufferException.InvalidWireType();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Reads a field tag, returning the tag of 0 for "end of stream".
        /// </summary>
        /// <remarks>
        /// If this method returns 0, it doesn't necessarily mean the end of all
        /// the data in this CodedInputStream; it may be the end of the logical stream
        /// for an embedded message, for example.
        /// </remarks>
        /// <returns>The next field tag, or 0 for end of stream. (0 is never a valid tag.)</returns>
        public uint ReadTag()
        {
            if (hasNextTag)
            {
                lastTag    = nextTag;
                hasNextTag = false;
                return(lastTag);
            }

            // Optimize for the incredibly common case of having at least two bytes left in the buffer,
            // and those two bytes being enough to get the tag. This will be true for fields up to 4095.
            if (bufferPos + 2 <= bufferSize)
            {
                int tmp = buffer[bufferPos++];
                if (tmp < 128)
                {
                    lastTag = (uint)tmp;
                }
                else
                {
                    int result = tmp & 0x7f;
                    if ((tmp = buffer[bufferPos++]) < 128)
                    {
                        result |= tmp << 7;
                        lastTag = (uint)result;
                    }
                    else
                    {
                        // Nope, rewind and go the potentially slow route.
                        bufferPos -= 2;
                        lastTag    = ReadRawVarint32();
                    }
                }
            }
            else
            {
                if (IsAtEnd)
                {
                    lastTag = 0;
                    return(0);
                }

                lastTag = ReadRawVarint32();
            }
            if (WireFormat.GetTagFieldNumber(lastTag) == 0)
            {
                // If we actually read a tag with a field of 0, that's not a valid tag.
                throw InvalidProtocolBufferException.InvalidTag();
            }
            if (ReachedLimit)
            {
                return(0);
            }
            return(lastTag);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Parses the next tag.
        /// If the end of logical stream was reached, an invalid tag of 0 is returned.
        /// </summary>
        public static uint ParseTag(ref ReadOnlySpan <byte> buffer, ref ParserInternalState state)
        {
            // The "nextTag" logic is there only as an optimization for reading non-packed repeated / map
            // fields and is strictly speaking not necessary.
            // TODO(jtattermusch): look into simplifying the ParseTag logic.
            if (state.hasNextTag)
            {
                state.lastTag    = state.nextTag;
                state.hasNextTag = false;
                return(state.lastTag);
            }

            // Optimize for the incredibly common case of having at least two bytes left in the buffer,
            // and those two bytes being enough to get the tag. This will be true for fields up to 4095.
            if (state.bufferPos + 2 <= state.bufferSize)
            {
                int tmp = buffer[state.bufferPos++];
                if (tmp < 128)
                {
                    state.lastTag = (uint)tmp;
                }
                else
                {
                    int result = tmp & 0x7f;
                    if ((tmp = buffer[state.bufferPos++]) < 128)
                    {
                        result       |= tmp << 7;
                        state.lastTag = (uint)result;
                    }
                    else
                    {
                        // Nope, rewind and go the potentially slow route.
                        state.bufferPos -= 2;
                        state.lastTag    = ParsingPrimitives.ParseRawVarint32(ref buffer, ref state);
                    }
                }
            }
            else
            {
                if (SegmentedBufferHelper.IsAtEnd(ref buffer, ref state))
                {
                    state.lastTag = 0;
                    return(0);
                }

                state.lastTag = ParsingPrimitives.ParseRawVarint32(ref buffer, ref state);
            }
            if (WireFormat.GetTagFieldNumber(state.lastTag) == 0)
            {
                // If we actually read a tag with a field of 0, that's not a valid tag.
                throw InvalidProtocolBufferException.InvalidTag();
            }
            return(state.lastTag);
        }
Ejemplo n.º 6
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();
            }
        }
        /// <summary>
        /// Parse a single field from <paramref name="input"/> and merge it
        /// into this set.
        /// </summary>
        /// <param name="input">The coded input stream containing the field</param>
        /// <returns>false if the tag is an "end group" tag, true otherwise</returns>
        private void MergeFieldFrom(CodedInputStream input)
        {
            uint tag    = input.LastTag;
            int  number = WireFormat.GetTagFieldNumber(tag);

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

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

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

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

            case WireFormat.WireType.StartGroup:
            {
                input.SkipGroup(tag);
                return;
            }

            case WireFormat.WireType.EndGroup:
            {
                throw new InvalidProtocolBufferException("Merge an unknown field of end-group tag, indicating that the corresponding start-group was missing.");
            }

            default:
                throw new InvalidOperationException("Wire Type is invalid.");
            }
        }
        public static void ReadGroup(ref ParseContext ctx, IMessage message)
        {
            if (ctx.state.recursionDepth >= ctx.state.recursionLimit)
            {
                throw InvalidProtocolBufferException.RecursionLimitExceeded();
            }
            ++ctx.state.recursionDepth;

            uint tag         = ctx.state.lastTag;
            int  fieldNumber = WireFormat.GetTagFieldNumber(tag);

            ReadRawMessage(ref ctx, message);
            CheckLastTagWas(ref ctx.state, WireFormat.MakeTag(fieldNumber, WireFormat.WireType.EndGroup));

            --ctx.state.recursionDepth;
        }
Ejemplo n.º 9
0
        public void MaximumFieldNumber()
        {
            MemoryStream      ms     = new MemoryStream();
            CodedOutputStream output = new CodedOutputStream(ms);

            int  fieldNumber = 0x1FFFFFFF;
            uint tag         = WireFormat.MakeTag(fieldNumber, WireFormat.WireType.LengthDelimited);

            output.WriteRawVarint32(tag);
            output.WriteString("field 1");
            output.Flush();
            ms.Position = 0;

            CodedInputStream input = new CodedInputStream(ms);

            Assert.AreEqual(tag, input.ReadTag());
            Assert.AreEqual(fieldNumber, WireFormat.GetTagFieldNumber(tag));
        }
        public void TestSlowPathAvoidance()
        {
            using (var ms = new MemoryStream())
            {
                CodedOutputStream output = new CodedOutputStream(ms);
                output.WriteTag(1, WireFormat.WireType.LengthDelimited);
                output.WriteBytes(ByteString.CopyFrom(new byte[100]));
                output.WriteTag(2, WireFormat.WireType.LengthDelimited);
                output.WriteBytes(ByteString.CopyFrom(new byte[100]));
                output.Flush();

                ms.Position = 0;
                CodedInputStream input = new CodedInputStream(ms, new byte[ms.Length / 2], 0, 0, false);

                uint tag = input.ReadTag();
                Assert.AreEqual(1, WireFormat.GetTagFieldNumber(tag));
                Assert.AreEqual(100, input.ReadBytes().Length);

                tag = input.ReadTag();
                Assert.AreEqual(2, WireFormat.GetTagFieldNumber(tag));
                Assert.AreEqual(100, input.ReadBytes().Length);
            }
        }
        /// <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--;
        }
Ejemplo n.º 12
0
 internal bool ContainsInputField(CodedInputStream stream, Type target, out Extension extension)
 {
     return(extensions.TryGetValue(new ObjectIntPair <Type>(target, WireFormat.GetTagFieldNumber(stream.LastTag)), out extension));
 }