internal void MergeFrom(ICodedInputStream input, ExtensionRegistry extensionRegistry, IBuilder builder) { uint tag; string name; while (input.ReadTag(out tag, out name)) { if (tag == 0 && name != null) { FieldDescriptor fieldByName = builder.DescriptorForType.FindFieldByName(name); if (fieldByName != null) { tag = WireFormat.MakeTag(fieldByName); } else { ExtensionInfo extension = extensionRegistry.FindByName(builder.DescriptorForType, name); if (extension != null) { tag = WireFormat.MakeTag(extension.Descriptor); } } } if (tag == 0) { if (input.SkipField()) { continue; //can't merge unknown without field tag } break; } if (!MergeFieldFrom(input, extensionRegistry, builder, tag, name)) { // end group tag break; } } }
/// <summary> /// Parse an entire message from <paramref name="input"/> and merge /// its fields into this set. /// </summary> public Builder MergeFrom(ICodedInputStream input) { uint tag; string name; while (input.ReadTag(out tag, out name)) { if (tag == 0) { if (input.SkipField()) { continue; //can't merge unknown without field tag } break; } if (!MergeFieldFrom(tag, input)) { break; } } return(this); }
/// <summary> /// Called by MergeFieldFrom to parse a MessageSet extension. /// </summary> private void MergeMessageSetExtensionFromCodedStream(ICodedInputStream input, ExtensionRegistry extensionRegistry, IBuilder builder) { MessageDescriptor type = builder.DescriptorForType; // The wire format for MessageSet is: // message MessageSet { // repeated group Item = 1 { // required int32 typeId = 2; // required bytes message = 3; // } // } // "typeId" is the extension's field number. The extension can only be // a message type, where "message" contains the encoded bytes of that // message. // // In practice, we will probably never see a MessageSet item in which // the message appears before the type ID, or where either field does not // appear exactly once. However, in theory such cases are valid, so we // should be prepared to accept them. int typeId = 0; ByteString rawBytes = null; // If we encounter "message" before "typeId" IBuilderLite subBuilder = null; FieldDescriptor field = null; uint lastTag = WireFormat.MessageSetTag.ItemStart; uint tag; string name; while (input.ReadTag(out tag, out name)) { if (tag == 0 && name != null) { if (name == "type_id") { tag = WireFormat.MessageSetTag.TypeID; } else if (name == "message") { tag = WireFormat.MessageSetTag.Message; } } if (tag == 0) { if (input.SkipField()) { continue; //can't merge unknown without field tag } break; } lastTag = tag; if (tag == WireFormat.MessageSetTag.TypeID) { typeId = 0; // Zero is not a valid type ID. if (input.ReadInt32(ref typeId) && typeId != 0) { ExtensionInfo extension = extensionRegistry[type, typeId]; if (extension != null) { field = extension.Descriptor; subBuilder = extension.DefaultInstance.WeakCreateBuilderForType(); IMessageLite originalMessage = (IMessageLite)builder[field]; if (originalMessage != null) { subBuilder.WeakMergeFrom(originalMessage); } if (rawBytes != null) { // We already encountered the message. Parse it now. // TODO(jonskeet): Check this is okay. It's subtly different from the Java, as it doesn't create an input stream from rawBytes. // In fact, why don't we just call MergeFrom(rawBytes)? And what about the extension registry? subBuilder.WeakMergeFrom(rawBytes.CreateCodedInput()); rawBytes = null; } } else { // Unknown extension number. If we already saw data, put it // in rawBytes. if (rawBytes != null) { MergeField(typeId, UnknownField.CreateBuilder().AddLengthDelimited(rawBytes).Build()); rawBytes = null; } } } } else if (tag == WireFormat.MessageSetTag.Message) { if (subBuilder != null) { // We already know the type, so we can parse directly from the input // with no copying. Hooray! input.ReadMessage(subBuilder, extensionRegistry); } else if (input.ReadBytes(ref rawBytes)) { if (typeId != 0) { // We don't know how to parse this. Ignore it. MergeField(typeId, UnknownField.CreateBuilder().AddLengthDelimited(rawBytes).Build()); } } } else { // Unknown tag. Skip it. if (!input.SkipField()) { break; // end of group } } } if (lastTag != WireFormat.MessageSetTag.ItemEnd) { throw InvalidProtocolBufferException.InvalidEndTag(); } if (subBuilder != null) { builder[field] = subBuilder.WeakBuild(); } }
/// <summary> /// Parse a single field from <paramref name="input"/> and merge it /// into this set. /// </summary> /// <param name="tag">The field's tag number, which was already parsed.</param> /// <param name="input">The coded input stream containing the field</param> /// <returns>false if the tag is an "end group" tag, true otherwise</returns> public bool MergeFieldFrom(uint tag, ICodedInputStream input) { if (tag == 0) { input.SkipField(); return(true); } int number = WireFormat.GetTagFieldNumber(tag); switch (WireFormat.GetTagWireType(tag)) { case WireFormat.WireType.Varint: { ulong uint64 = 0; if (input.ReadUInt64(ref uint64)) { GetFieldBuilder(number).AddVarint(uint64); } return(true); } case WireFormat.WireType.Fixed32: { uint uint32 = 0; if (input.ReadFixed32(ref uint32)) { GetFieldBuilder(number).AddFixed32(uint32); } return(true); } case WireFormat.WireType.Fixed64: { ulong uint64 = 0; if (input.ReadFixed64(ref uint64)) { GetFieldBuilder(number).AddFixed64(uint64); } return(true); } case WireFormat.WireType.LengthDelimited: { ByteString bytes = null; if (input.ReadBytes(ref bytes)) { GetFieldBuilder(number).AddLengthDelimited(bytes); } return(true); } case WireFormat.WireType.StartGroup: { Builder subBuilder = CreateBuilder(); #pragma warning disable 0612 input.ReadUnknownGroup(number, subBuilder); #pragma warning restore 0612 GetFieldBuilder(number).AddGroup(subBuilder.Build()); return(true); } case WireFormat.WireType.EndGroup: return(false); default: throw InvalidProtocolBufferException.InvalidWireType(); } }
protected virtual bool ParseUnknownField(ICodedInputStream input, ExtensionRegistry extensionRegistry, uint tag, string fieldName) { return(input.SkipField()); }
protected override bool ParseUnknownField(ICodedInputStream input, ExtensionRegistry extensionRegistry, uint tag, string fieldName) { FieldSet extensions = MessageBeingBuilt.Extensions; WireFormat.WireType wireType = WireFormat.GetTagWireType(tag); int fieldNumber = WireFormat.GetTagFieldNumber(tag); IGeneratedExtensionLite extension = extensionRegistry[DefaultInstanceForType, fieldNumber]; if (extension == null) //unknown field { return(input.SkipField()); } IFieldDescriptorLite field = extension.Descriptor; // Unknown field or wrong wire type. Skip. if (field == null) { return(input.SkipField()); } WireFormat.WireType expectedType = field.IsPacked ? WireFormat.WireType.LengthDelimited : WireFormat.GetWireType(field.FieldType); if (wireType != expectedType) { expectedType = WireFormat.GetWireType(field.FieldType); if (wireType == expectedType) { //Allowed as of 2.3, this is unpacked data for a packed array } else if (field.IsRepeated && wireType == WireFormat.WireType.LengthDelimited && (expectedType == WireFormat.WireType.Varint || expectedType == WireFormat.WireType.Fixed32 || expectedType == WireFormat.WireType.Fixed64)) { //Allowed as of 2.3, this is packed data for an unpacked array } else { return(input.SkipField()); } } if (!field.IsRepeated && wireType != WireFormat.GetWireType(field.FieldType)) //invalid wire type { return(input.SkipField()); } switch (field.FieldType) { case FieldType.Group: case FieldType.Message: { if (!field.IsRepeated) { IMessageLite message = extensions[extension.Descriptor] as IMessageLite; IBuilderLite subBuilder = (message ?? extension.MessageDefaultInstance).WeakToBuilder(); if (field.FieldType == FieldType.Group) { input.ReadGroup(field.FieldNumber, subBuilder, extensionRegistry); } else { input.ReadMessage(subBuilder, extensionRegistry); } extensions[field] = subBuilder.WeakBuild(); } else { List <IMessageLite> list = new List <IMessageLite>(); if (field.FieldType == FieldType.Group) { input.ReadGroupArray(tag, fieldName, list, extension.MessageDefaultInstance, extensionRegistry); } else { input.ReadMessageArray(tag, fieldName, list, extension.MessageDefaultInstance, extensionRegistry); } foreach (IMessageLite m in list) { extensions.AddRepeatedField(field, m); } return(true); } break; } case FieldType.Enum: { if (!field.IsRepeated) { object unknown; IEnumLite value = null; if (input.ReadEnum(ref value, out unknown, field.EnumType)) { extensions[field] = value; } } else { ICollection <object> unknown; List <IEnumLite> list = new List <IEnumLite>(); input.ReadEnumArray(tag, fieldName, list, out unknown, field.EnumType); foreach (IEnumLite en in list) { extensions.AddRepeatedField(field, en); } } break; } default: { if (!field.IsRepeated) { object value = null; if (input.ReadPrimitiveField(field.FieldType, ref value)) { extensions[field] = value; } } else { List <object> list = new List <object>(); input.ReadPrimitiveArray(field.FieldType, tag, fieldName, list); foreach (object oval in list) { extensions.AddRepeatedField(field, oval); } } break; } } return(true); }
protected override bool ParseUnknownField(ICodedInputStream input, ExtensionRegistry extensionRegistry, uint tag, string fieldName) { TMessage messageBeingBuilt = this.MessageBeingBuilt; FieldSet extensions = messageBeingBuilt.Extensions; WireFormat.WireType tagWireType = WireFormat.GetTagWireType(tag); int tagFieldNumber = WireFormat.GetTagFieldNumber(tag); IGeneratedExtensionLite generatedExtensionLite = extensionRegistry[this.DefaultInstanceForType, tagFieldNumber]; if (generatedExtensionLite == null) { return(input.SkipField()); } IFieldDescriptorLite descriptor = generatedExtensionLite.Descriptor; if (descriptor == null) { return(input.SkipField()); } WireFormat.WireType wireType = descriptor.IsPacked ? WireFormat.WireType.LengthDelimited : WireFormat.GetWireType(descriptor.FieldType); if (tagWireType != wireType) { wireType = WireFormat.GetWireType(descriptor.FieldType); if (tagWireType != wireType && (!descriptor.IsRepeated || tagWireType != WireFormat.WireType.LengthDelimited || (wireType != WireFormat.WireType.Varint && wireType != WireFormat.WireType.Fixed32 && wireType != WireFormat.WireType.Fixed64))) { return(input.SkipField()); } } if (!descriptor.IsRepeated && tagWireType != WireFormat.GetWireType(descriptor.FieldType)) { return(input.SkipField()); } FieldType fieldType = descriptor.FieldType; switch (fieldType) { case FieldType.Group: case FieldType.Message: { if (descriptor.IsRepeated) { List <IMessageLite> list = new List <IMessageLite>(); if (descriptor.FieldType == FieldType.Group) { input.ReadGroupArray <IMessageLite>(tag, fieldName, list, generatedExtensionLite.MessageDefaultInstance, extensionRegistry); } else { input.ReadMessageArray <IMessageLite>(tag, fieldName, list, generatedExtensionLite.MessageDefaultInstance, extensionRegistry); } using (List <IMessageLite> .Enumerator enumerator = list.GetEnumerator()) { while (enumerator.MoveNext()) { IMessageLite current = enumerator.Current; extensions.AddRepeatedField(descriptor, current); } } return(true); } IMessageLite messageLite = extensions[generatedExtensionLite.Descriptor] as IMessageLite; IBuilderLite builderLite = (messageLite ?? generatedExtensionLite.MessageDefaultInstance).WeakToBuilder(); if (descriptor.FieldType == FieldType.Group) { input.ReadGroup(descriptor.FieldNumber, builderLite, extensionRegistry); } else { input.ReadMessage(builderLite, extensionRegistry); } extensions[descriptor] = builderLite.WeakBuild(); break; } default: if (fieldType == FieldType.Enum) { if (!descriptor.IsRepeated) { IEnumLite value = null; object obj; if (input.ReadEnum(ref value, out obj, descriptor.EnumType)) { extensions[descriptor] = value; break; } break; } else { List <IEnumLite> list2 = new List <IEnumLite>(); ICollection <object> collection; input.ReadEnumArray(tag, fieldName, list2, out collection, descriptor.EnumType); using (List <IEnumLite> .Enumerator enumerator2 = list2.GetEnumerator()) { while (enumerator2.MoveNext()) { IEnumLite current2 = enumerator2.Current; extensions.AddRepeatedField(descriptor, current2); } break; } } } if (!descriptor.IsRepeated) { object value2 = null; if (input.ReadPrimitiveField(descriptor.FieldType, ref value2)) { extensions[descriptor] = value2; } } else { List <object> list3 = new List <object>(); input.ReadPrimitiveArray(descriptor.FieldType, tag, fieldName, list3); using (List <object> .Enumerator enumerator3 = list3.GetEnumerator()) { while (enumerator3.MoveNext()) { object current3 = enumerator3.Current; extensions.AddRepeatedField(descriptor, current3); } } } break; } return(true); }
/// <summary> /// Called by MergeFieldFrom to parse a MessageSet extension. /// </summary> private void MergeMessageSetExtensionFromCodedStream(ICodedInputStream input, ExtensionRegistry extensionRegistry, IBuilder builder) { MessageDescriptor type = builder.DescriptorForType; // The wire format for MessageSet is: // message MessageSet { // repeated group Item = 1 { // required int32 typeId = 2; // required bytes message = 3; // } // } // "typeId" is the extension's field number. The extension can only be // a message type, where "message" contains the encoded bytes of that // message. // // In practice, we will probably never see a MessageSet item in which // the message appears before the type ID, or where either field does not // appear exactly once. However, in theory such cases are valid, so we // should be prepared to accept them. int typeId = 0; ByteString rawBytes = null; // If we encounter "message" before "typeId" IBuilderLite subBuilder = null; FieldDescriptor field = null; uint lastTag = WireFormat.MessageSetTag.ItemStart; uint tag; string name; while (input.ReadTag(out tag, out name)) { if (tag == 0 && name != null) { if (name == "type_id") { tag = WireFormat.MessageSetTag.TypeID; } else if (name == "message") { tag = WireFormat.MessageSetTag.Message; } } if (tag == 0) { if (input.SkipField()) { continue; //can't merge unknown without field tag } break; } lastTag = tag; if (tag == WireFormat.MessageSetTag.TypeID) { typeId = 0; // Zero is not a valid type ID. if (input.ReadInt32(ref typeId) && typeId != 0) { ExtensionInfo extension = extensionRegistry[type, typeId]; if (extension != null) { field = extension.Descriptor; subBuilder = extension.DefaultInstance.WeakCreateBuilderForType(); IMessageLite originalMessage = (IMessageLite) builder[field]; if (originalMessage != null) { subBuilder.WeakMergeFrom(originalMessage); } if (rawBytes != null) { // We already encountered the message. Parse it now. // TODO(jonskeet): Check this is okay. It's subtly different from the Java, as it doesn't create an input stream from rawBytes. // In fact, why don't we just call MergeFrom(rawBytes)? And what about the extension registry? subBuilder.WeakMergeFrom(rawBytes.CreateCodedInput()); rawBytes = null; } } else { // Unknown extension number. If we already saw data, put it // in rawBytes. if (rawBytes != null) { MergeField(typeId, UnknownField.CreateBuilder().AddLengthDelimited(rawBytes).Build()); rawBytes = null; } } } } else if (tag == WireFormat.MessageSetTag.Message) { if (subBuilder != null) { // We already know the type, so we can parse directly from the input // with no copying. Hooray! input.ReadMessage(subBuilder, extensionRegistry); } else if (input.ReadBytes(ref rawBytes)) { if (typeId != 0) { // We don't know how to parse this. Ignore it. MergeField(typeId, UnknownField.CreateBuilder().AddLengthDelimited(rawBytes).Build()); } } } else { // Unknown tag. Skip it. if (!input.SkipField()) { break; // end of group } } } if (lastTag != WireFormat.MessageSetTag.ItemEnd) { throw InvalidProtocolBufferException.InvalidEndTag(); } if (subBuilder != null) { builder[field] = subBuilder.WeakBuild(); } }
internal void MergeFrom(ICodedInputStream input, ExtensionRegistry extensionRegistry, IBuilder builder) { uint tag; string name; while (input.ReadTag(out tag, out name)) { if (tag == 0 && name != null) { FieldDescriptor fieldByName = builder.DescriptorForType.FindFieldByName(name); if (fieldByName != null) { tag = WireFormat.MakeTag(fieldByName); } else { ExtensionInfo extension = extensionRegistry.FindByName(builder.DescriptorForType, name); if (extension != null) { tag = WireFormat.MakeTag(extension.Descriptor); } } } if (tag == 0) { if (input.SkipField()) { continue; //can't merge unknown without field tag } break; } if (!MergeFieldFrom(input, extensionRegistry, builder, tag, name)) { // end group tag break; } } }
/// <summary> /// Parse a single field from <paramref name="input"/> and merge it /// into this set. /// </summary> /// <param name="tag">The field's tag number, which was already parsed.</param> /// <param name="input">The coded input stream containing the field</param> /// <returns>false if the tag is an "end group" tag, true otherwise</returns> public bool MergeFieldFrom(uint tag, ICodedInputStream input) { if (tag == 0) { input.SkipField(); return true; } int number = WireFormat.GetTagFieldNumber(tag); switch (WireFormat.GetTagWireType(tag)) { case WireFormat.WireType.Varint: { ulong uint64 = 0; if (input.ReadUInt64(ref uint64)) { GetFieldBuilder(number).AddVarint(uint64); } return true; } case WireFormat.WireType.Fixed32: { uint uint32 = 0; if (input.ReadFixed32(ref uint32)) { GetFieldBuilder(number).AddFixed32(uint32); } return true; } case WireFormat.WireType.Fixed64: { ulong uint64 = 0; if (input.ReadFixed64(ref uint64)) { GetFieldBuilder(number).AddFixed64(uint64); } return true; } case WireFormat.WireType.LengthDelimited: { ByteString bytes = null; if (input.ReadBytes(ref bytes)) { GetFieldBuilder(number).AddLengthDelimited(bytes); } return true; } case WireFormat.WireType.StartGroup: { Builder subBuilder = CreateBuilder(); #pragma warning disable 0612 input.ReadUnknownGroup(number, subBuilder); #pragma warning restore 0612 GetFieldBuilder(number).AddGroup(subBuilder.Build()); return true; } case WireFormat.WireType.EndGroup: return false; default: throw InvalidProtocolBufferException.InvalidWireType(); } }
/// <summary> /// Parse an entire message from <paramref name="input"/> and merge /// its fields into this set. /// </summary> public Builder MergeFrom(ICodedInputStream input) { uint tag; string name; while (input.ReadTag(out tag, out name)) { if (tag == 0) { if (input.SkipField()) { continue; //can't merge unknown without field tag } break; } if (!MergeFieldFrom(tag, input)) { break; } } return this; }