blob: aa371fad866d4a86eb90f21eb3242cceb683e315 [file] [log] [blame]
Jon Skeet60c059b2008-10-23 21:17:56 +01001// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
3// http://github.com/jskeet/dotnet-protobufs/
4// Original C++/Java/Python code:
Jon Skeet68036862008-10-22 13:30:34 +01005// http://code.google.com/p/protobuf/
6//
Jon Skeet60c059b2008-10-23 21:17:56 +01007// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions are
9// met:
Jon Skeet68036862008-10-22 13:30:34 +010010//
Jon Skeet60c059b2008-10-23 21:17:56 +010011// * Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// * Redistributions in binary form must reproduce the above
14// copyright notice, this list of conditions and the following disclaimer
15// in the documentation and/or other materials provided with the
16// distribution.
17// * Neither the name of Google Inc. nor the names of its
18// contributors may be used to endorse or promote products derived from
19// this software without specific prior written permission.
Jon Skeet68036862008-10-22 13:30:34 +010020//
Jon Skeet60c059b2008-10-23 21:17:56 +010021// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Jon Skeet68036862008-10-22 13:30:34 +010032using System;
33using System.Collections.Generic;
34using System.IO;
35using Google.ProtocolBuffers.Collections;
36using Google.ProtocolBuffers.Descriptors;
37using Google.ProtocolBuffers.DescriptorProtos;
38
39namespace Google.ProtocolBuffers {
40 /// <summary>
41 /// Used to keep track of fields which were seen when parsing a protocol message
42 /// but whose field numbers or types are unrecognized. This most frequently
43 /// occurs when new fields are added to a message type and then messages containing
44 /// those fields are read by old software that was built before the new types were
45 /// added.
46 ///
47 /// Every message contains an UnknownFieldSet.
48 ///
49 /// Most users will never need to use this class directly.
50 /// </summary>
51 public sealed class UnknownFieldSet {
52
53 private static readonly UnknownFieldSet defaultInstance = new UnknownFieldSet(new Dictionary<int, UnknownField>());
54
55 private readonly IDictionary<int, UnknownField> fields;
56
57 private UnknownFieldSet(IDictionary<int, UnknownField> fields) {
58 this.fields = fields;
59 }
60
61 /// <summary>
62 /// Creates a new unknown field set builder.
63 /// </summary>
64 public static Builder CreateBuilder() {
65 return new Builder();
66 }
67
68 /// <summary>
69 /// Creates a new unknown field set builder
70 /// and initialize it from <paramref name="original"/>.
71 /// </summary>
72 public static Builder CreateBuilder(UnknownFieldSet original) {
73 return new Builder().MergeFrom(original);
74 }
75
76 public static UnknownFieldSet DefaultInstance {
77 get { return defaultInstance; }
78 }
79
80 /// <summary>
81 /// Returns a read-only view of the mapping from field numbers to values.
82 /// </summary>
83 public IDictionary<int, UnknownField> FieldDictionary {
84 get { return Dictionaries.AsReadOnly(fields); }
85 }
86
87 /// <summary>
88 /// Checks whether or not the given field number is present in the set.
89 /// </summary>
90 public bool HasField(int field) {
91 return fields.ContainsKey(field);
92 }
93
94 /// <summary>
95 /// Fetches a field by number, returning an empty field if not present.
96 /// Never returns null.
97 /// </summary>
98 public UnknownField this[int number] {
99 get {
100 UnknownField ret;
101 if (!fields.TryGetValue(number, out ret)) {
102 ret = UnknownField.DefaultInstance;
103 }
104 return ret;
105 }
106 }
107
108 /// <summary>
109 /// Serializes the set and writes it to <paramref name="output"/>.
110 /// </summary>
111 public void WriteTo(CodedOutputStream output) {
112 foreach (KeyValuePair<int, UnknownField> entry in fields) {
113 entry.Value.WriteTo(entry.Key, output);
114 }
115 }
116
117 /// <summary>
118 /// Gets the number of bytes required to encode this set.
119 /// </summary>
120 public int SerializedSize {
121 get {
122 int result = 0;
123 foreach (KeyValuePair<int, UnknownField> entry in fields) {
124 result += entry.Value.GetSerializedSize(entry.Key);
125 }
126 return result;
127 }
128 }
129
130 /// <summary>
131 /// Converts the set to a string in protocol buffer text format. This
132 /// is just a trivial wrapper around TextFormat.PrintToString.
133 /// </summary>
134 public override String ToString() {
135 return TextFormat.PrintToString(this);
136 }
137
138 /// <summary>
139 /// Serializes the message to a ByteString and returns it. This is
140 /// just a trivial wrapper around WriteTo(CodedOutputStream).
141 /// </summary>
142 /// <returns></returns>
143 public ByteString ToByteString() {
144 ByteString.CodedBuilder codedBuilder = new ByteString.CodedBuilder(SerializedSize);
145 WriteTo(codedBuilder.CodedOutput);
146 return codedBuilder.Build();
147 }
148
149 /// <summary>
150 /// Serializes the message to a byte array and returns it. This is
151 /// just a trivial wrapper around WriteTo(CodedOutputStream).
152 /// </summary>
153 /// <returns></returns>
154 public byte[] ToByteArray() {
155 byte[] data = new byte[SerializedSize];
156 CodedOutputStream output = CodedOutputStream.CreateInstance(data);
157 WriteTo(output);
158 output.CheckNoSpaceLeft();
159 return data;
160 }
161
162 /// <summary>
163 /// Serializes the message and writes it to <paramref name="output"/>. This is
164 /// just a trivial wrapper around WriteTo(CodedOutputStream).
165 /// </summary>
166 /// <param name="output"></param>
167 public void WriteTo(Stream output) {
168 CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);
169 WriteTo(codedOutput);
170 codedOutput.Flush();
171 }
172
173 /// <summary>
174 /// Serializes the set and writes it to <paramref name="output"/> using
175 /// the MessageSet wire format.
176 /// </summary>
177 public void WriteAsMessageSetTo(CodedOutputStream output) {
178 foreach (KeyValuePair<int, UnknownField> entry in fields) {
179 entry.Value.WriteAsMessageSetExtensionTo(entry.Key, output);
180 }
181 }
182
183 /// <summary>
184 /// Gets the number of bytes required to encode this set using the MessageSet
185 /// wire format.
186 /// </summary>
187 public int SerializedSizeAsMessageSet {
188 get {
189 int result = 0;
190 foreach (KeyValuePair<int, UnknownField> entry in fields) {
191 result += entry.Value.GetSerializedSizeAsMessageSetExtension(entry.Key);
192 }
193 return result;
194 }
195 }
196
197 /// <summary>
198 /// Parses an UnknownFieldSet from the given input.
199 /// </summary>
200 public static UnknownFieldSet ParseFrom(CodedInputStream input) {
201 return CreateBuilder().MergeFrom(input).Build();
202 }
203
204 /// <summary>
205 /// Parses an UnknownFieldSet from the given data.
206 /// </summary>
207 public static UnknownFieldSet ParseFrom(ByteString data) {
208 return CreateBuilder().MergeFrom(data).Build();
209 }
210
211 /// <summary>
212 /// Parses an UnknownFieldSet from the given data.
213 /// </summary>
214 public static UnknownFieldSet ParseFrom(byte[] data) {
215 return CreateBuilder().MergeFrom(data).Build();
216 }
217
218 /// <summary>
219 /// Parses an UnknownFieldSet from the given input.
220 /// </summary>
221 public static UnknownFieldSet ParseFrom(Stream input) {
222 return CreateBuilder().MergeFrom(input).Build();
223 }
224
225 /// <summary>
226 /// Builder for UnknownFieldSets.
227 /// </summary>
228 public sealed class Builder
229 {
230 /// <summary>
231 /// Mapping from number to field. Note that by using a SortedList we ensure
232 /// that the fields will be serialized in ascending order.
233 /// </summary>
234 private IDictionary<int, UnknownField> fields = new SortedList<int, UnknownField>();
235
236 // Optimization: We keep around a builder for the last field that was
237 // modified so that we can efficiently add to it multiple times in a
238 // row (important when parsing an unknown repeated field).
239 int lastFieldNumber;
240 UnknownField.Builder lastField;
241
242 internal Builder() {
243 }
244
245 /// <summary>
246 /// Returns a field builder for the specified field number, including any values
247 /// which already exist.
248 /// </summary>
249 private UnknownField.Builder GetFieldBuilder(int number) {
250 if (lastField != null) {
251 if (number == lastFieldNumber) {
252 return lastField;
253 }
254 // Note: AddField() will reset lastField and lastFieldNumber.
255 AddField(lastFieldNumber, lastField.Build());
256 }
257 if (number == 0) {
258 return null;
259 }
260
261 lastField = UnknownField.CreateBuilder();
262 UnknownField existing;
263 if (fields.TryGetValue(number, out existing)) {
264 lastField.MergeFrom(existing);
265 }
266 lastFieldNumber = number;
267 return lastField;
268 }
269
270 /// <summary>
271 /// Build the UnknownFieldSet and return it. Once this method has been called,
272 /// this instance will no longer be usable. Calling any method after this
273 /// will throw a NullReferenceException.
274 /// </summary>
275 public UnknownFieldSet Build() {
276 GetFieldBuilder(0); // Force lastField to be built.
277 UnknownFieldSet result = fields.Count == 0 ? DefaultInstance : new UnknownFieldSet(fields);
278 fields = null;
279 return result;
280 }
281
282 /// <summary>
283 /// Adds a field to the set. If a field with the same number already exists, it
284 /// is replaced.
285 /// </summary>
286 public Builder AddField(int number, UnknownField field) {
287 if (number == 0) {
288 throw new ArgumentOutOfRangeException("number", "Zero is not a valid field number.");
289 }
290 if (lastField != null && lastFieldNumber == number) {
291 // Discard this.
292 lastField = null;
293 lastFieldNumber = 0;
294 }
295 fields[number] = field;
296 return this;
297 }
298
299 /// <summary>
300 /// Resets the builder to an empty set.
301 /// </summary>
302 public Builder Clear() {
303 fields.Clear();
304 lastFieldNumber = 0;
305 lastField = null;
306 return this;
307 }
308
309 /// <summary>
310 /// Parse an entire message from <paramref name="input"/> and merge
311 /// its fields into this set.
312 /// </summary>
313 public Builder MergeFrom(CodedInputStream input) {
314 while (true) {
315 uint tag = input.ReadTag();
316 if (tag == 0 || !MergeFieldFrom(tag, input)) {
317 break;
318 }
319 }
320 return this;
321 }
322
323 /// <summary>
324 /// Parse a single field from <paramref name="input"/> and merge it
325 /// into this set.
326 /// </summary>
327 /// <param name="tag">The field's tag number, which was already parsed.</param>
328 /// <param name="input">The coded input stream containing the field</param>
329 /// <returns>false if the tag is an "end group" tag, true otherwise</returns>
330 public bool MergeFieldFrom(uint tag, CodedInputStream input) {
331 int number = WireFormat.GetTagFieldNumber(tag);
332 switch (WireFormat.GetTagWireType(tag)) {
333 case WireFormat.WireType.Varint:
334 GetFieldBuilder(number).AddVarint(input.ReadUInt64());
335 return true;
336 case WireFormat.WireType.Fixed64:
337 GetFieldBuilder(number).AddFixed64(input.ReadFixed64());
338 return true;
339 case WireFormat.WireType.LengthDelimited:
340 GetFieldBuilder(number).AddLengthDelimited(input.ReadBytes());
341 return true;
342 case WireFormat.WireType.StartGroup: {
343 Builder subBuilder = CreateBuilder();
344 input.ReadUnknownGroup(number, subBuilder);
345 GetFieldBuilder(number).AddGroup(subBuilder.Build());
346 return true;
347 }
348 case WireFormat.WireType.EndGroup:
349 return false;
350 case WireFormat.WireType.Fixed32:
351 GetFieldBuilder(number).AddFixed32(input.ReadFixed32());
352 return true;
353 default:
354 throw InvalidProtocolBufferException.InvalidWireType();
355 }
356 }
357
358 /// <summary>
359 /// Parses <paramref name="input"/> as an UnknownFieldSet and merge it
360 /// with the set being built. This is just a small wrapper around
361 /// MergeFrom(CodedInputStream).
362 /// </summary>
363 public Builder MergeFrom(Stream input) {
364 CodedInputStream codedInput = CodedInputStream.CreateInstance(input);
365 MergeFrom(codedInput);
366 codedInput.CheckLastTagWas(0);
367 return this;
368 }
369
370 /// <summary>
371 /// Parses <paramref name="data"/> as an UnknownFieldSet and merge it
372 /// with the set being built. This is just a small wrapper around
373 /// MergeFrom(CodedInputStream).
374 /// </summary>
375 public Builder MergeFrom(ByteString data) {
376 CodedInputStream input = data.CreateCodedInput();
377 MergeFrom(input);
378 input.CheckLastTagWas(0);
379 return this;
380 }
381
382 /// <summary>
383 /// Parses <paramref name="data"/> as an UnknownFieldSet and merge it
384 /// with the set being built. This is just a small wrapper around
385 /// MergeFrom(CodedInputStream).
386 /// </summary>
387 public Builder MergeFrom(byte[] data) {
388 CodedInputStream input = CodedInputStream.CreateInstance(data);
389 MergeFrom(input);
390 input.CheckLastTagWas(0);
391 return this;
392 }
393
394 /// <summary>
395 /// Convenience method for merging a new field containing a single varint
396 /// value. This is used in particular when an unknown enum value is
397 /// encountered.
398 /// </summary>
399 public Builder MergeVarintField(int number, ulong value) {
400 if (number == 0) {
401 throw new ArgumentOutOfRangeException("number", "Zero is not a valid field number.");
402 }
403 GetFieldBuilder(number).AddVarint(value);
404 return this;
405 }
406
407 /// <summary>
408 /// Merges the fields from <paramref name="other"/> into this set.
409 /// If a field number exists in both sets, the values in <paramref name="other"/>
410 /// will be appended to the values in this set.
411 /// </summary>
412 public Builder MergeFrom(UnknownFieldSet other) {
413 if (other != DefaultInstance) {
414 foreach(KeyValuePair<int, UnknownField> entry in other.fields) {
415 MergeField(entry.Key, entry.Value);
416 }
417 }
418 return this;
419 }
420
421 /// <summary>
422 /// Checks if the given field number is present in the set.
423 /// </summary>
424 public bool HasField(int number) {
425 if (number == 0) {
426 throw new ArgumentOutOfRangeException("number", "Zero is not a valid field number.");
427 }
428 return number == lastFieldNumber || fields.ContainsKey(number);
429 }
430
431 /// <summary>
432 /// Adds a field to the unknown field set. If a field with the same
433 /// number already exists, the two are merged.
434 /// </summary>
435 public Builder MergeField(int number, UnknownField field) {
436 if (number == 0) {
437 throw new ArgumentOutOfRangeException("number", "Zero is not a valid field number.");
438 }
439 if (HasField(number)) {
440 GetFieldBuilder(number).MergeFrom(field);
441 } else {
442 // Optimization: We could call getFieldBuilder(number).mergeFrom(field)
443 // in this case, but that would create a copy of the Field object.
444 // We'd rather reuse the one passed to us, so call AddField() instead.
445 AddField(number, field);
446 }
447 return this;
448 }
449
450 internal void MergeFrom(CodedInputStream input, ExtensionRegistry extensionRegistry, IBuilder builder) {
451 while (true) {
452 uint tag = input.ReadTag();
453 if (tag == 0) {
454 break;
455 }
456 if (!MergeFieldFrom(input, extensionRegistry, builder, tag)) {
457 // end group tag
458 break;
459 }
460 }
461 }
462
463 /// <summary>
464 /// Like <see cref="MergeFrom(CodedInputStream, ExtensionRegistry, IBuilder)" />
465 /// but parses a single field.
466 /// </summary>
467 /// <param name="input">The input to read the field from</param>
468 /// <param name="extensionRegistry">Registry to use when an extension field is encountered</param>
469 /// <param name="builder">Builder to merge field into, if it's a known field</param>
470 /// <param name="tag">The tag, which should already have been read from the input</param>
471 /// <returns>true unless the tag is an end-group tag</returns>
472 internal bool MergeFieldFrom(CodedInputStream input,
473 ExtensionRegistry extensionRegistry, IBuilder builder, uint tag) {
474
Jon Skeet68036862008-10-22 13:30:34 +0100475 MessageDescriptor type = builder.DescriptorForType;
476 if (type.Options.MessageSetWireFormat && tag == WireFormat.MessageSetTag.ItemStart) {
477 MergeMessageSetExtensionFromCodedStream(input, extensionRegistry, builder);
478 return true;
479 }
480
481 WireFormat.WireType wireType = WireFormat.GetTagWireType(tag);
482 int fieldNumber = WireFormat.GetTagFieldNumber(tag);
483
484 FieldDescriptor field;
485 IMessage defaultFieldInstance = null;
486
487 if (type.IsExtensionNumber(fieldNumber)) {
488 ExtensionInfo extension = extensionRegistry[type, fieldNumber];
489 if (extension == null) {
490 field = null;
491 } else {
492 field = extension.Descriptor;
493 defaultFieldInstance = extension.DefaultInstance;
494 }
495 } else {
496 field = type.FindFieldByNumber(fieldNumber);
497 }
498
499 // Unknown field or wrong wire type. Skip.
Jon Skeet25a28582009-02-18 16:06:22 +0000500 if (field == null || wireType != WireFormat.GetWireType(field)) {
Jon Skeet68036862008-10-22 13:30:34 +0100501 return MergeFieldFrom(tag, input);
502 }
503
Jon Skeet25a28582009-02-18 16:06:22 +0000504 if (field.IsPacked) {
505 int length = (int)input.ReadRawVarint32();
506 int limit = input.PushLimit(length);
507 if (field.FieldType == FieldType.Enum) {
508 while (!input.ReachedLimit) {
Jon Skeet68036862008-10-22 13:30:34 +0100509 int rawValue = input.ReadEnum();
Jon Skeet25a28582009-02-18 16:06:22 +0000510 object value = field.EnumType.FindValueByNumber(rawValue);
Jon Skeet68036862008-10-22 13:30:34 +0100511 if (value == null) {
Jon Skeet25a28582009-02-18 16:06:22 +0000512 // If the number isn't recognized as a valid value for this
513 // enum, drop it (don't even add it to unknownFields).
Jon Skeet68036862008-10-22 13:30:34 +0100514 return true;
515 }
Jon Skeet25a28582009-02-18 16:06:22 +0000516 builder.WeakAddRepeatedField(field, value);
Jon Skeet68036862008-10-22 13:30:34 +0100517 }
Jon Skeet25a28582009-02-18 16:06:22 +0000518 } else {
519 while (!input.ReachedLimit) {
520 Object value = input.ReadPrimitiveField(field.FieldType);
521 builder.WeakAddRepeatedField(field, value);
522 }
523 }
524 input.PopLimit(limit);
Jon Skeet68036862008-10-22 13:30:34 +0100525 } else {
Jon Skeet25a28582009-02-18 16:06:22 +0000526 object value;
527 switch (field.FieldType) {
528 case FieldType.Group:
529 case FieldType.Message: {
530 IBuilder subBuilder;
531 if (defaultFieldInstance != null) {
532 subBuilder = defaultFieldInstance.WeakCreateBuilderForType();
533 } else {
534 subBuilder = builder.CreateBuilderForField(field);
535 }
536 if (!field.IsRepeated) {
537 subBuilder.WeakMergeFrom((IMessage)builder[field]);
538 }
539 if (field.FieldType == FieldType.Group) {
540 input.ReadGroup(field.FieldNumber, subBuilder, extensionRegistry);
541 } else {
542 input.ReadMessage(subBuilder, extensionRegistry);
543 }
544 value = subBuilder.WeakBuild();
545 break;
546 }
547 case FieldType.Enum: {
548 int rawValue = input.ReadEnum();
549 value = field.EnumType.FindValueByNumber(rawValue);
550 // If the number isn't recognized as a valid value for this enum,
551 // drop it.
552 if (value == null) {
553 MergeVarintField(fieldNumber, (ulong)rawValue);
554 return true;
555 }
556 break;
557 }
558 default:
559 value = input.ReadPrimitiveField(field.FieldType);
560 break;
561 }
562 if (field.IsRepeated) {
563 builder.WeakAddRepeatedField(field, value);
564 } else {
565 builder[field] = value;
566 }
Jon Skeet68036862008-10-22 13:30:34 +0100567 }
568 return true;
569 }
570
571 /// <summary>
572 /// Called by MergeFieldFrom to parse a MessageSet extension.
573 /// </summary>
574 private void MergeMessageSetExtensionFromCodedStream(CodedInputStream input,
575 ExtensionRegistry extensionRegistry, IBuilder builder) {
576 MessageDescriptor type = builder.DescriptorForType;
577
578 // The wire format for MessageSet is:
579 // message MessageSet {
580 // repeated group Item = 1 {
581 // required int32 typeId = 2;
582 // required bytes message = 3;
583 // }
584 // }
585 // "typeId" is the extension's field number. The extension can only be
586 // a message type, where "message" contains the encoded bytes of that
587 // message.
588 //
589 // In practice, we will probably never see a MessageSet item in which
590 // the message appears before the type ID, or where either field does not
591 // appear exactly once. However, in theory such cases are valid, so we
592 // should be prepared to accept them.
593
594 int typeId = 0;
595 ByteString rawBytes = null; // If we encounter "message" before "typeId"
596 IBuilder subBuilder = null;
597 FieldDescriptor field = null;
598
599 while (true) {
600 uint tag = input.ReadTag();
601 if (tag == 0) {
602 break;
603 }
604
605 if (tag == WireFormat.MessageSetTag.TypeID) {
606 typeId = input.ReadInt32();
607 // Zero is not a valid type ID.
608 if (typeId != 0) {
609 ExtensionInfo extension = extensionRegistry[type, typeId];
610 if (extension != null) {
611 field = extension.Descriptor;
612 subBuilder = extension.DefaultInstance.WeakCreateBuilderForType();
613 IMessage originalMessage = (IMessage)builder[field];
614 if (originalMessage != null) {
615 subBuilder.WeakMergeFrom(originalMessage);
616 }
617 if (rawBytes != null) {
618 // We already encountered the message. Parse it now.
619 // TODO(jonskeet): Check this is okay. It's subtly different from the Java, as it doesn't create an input stream from rawBytes.
620 // In fact, why don't we just call MergeFrom(rawBytes)? And what about the extension registry?
621 subBuilder.WeakMergeFrom(rawBytes.CreateCodedInput());
622 rawBytes = null;
623 }
624 } else {
625 // Unknown extension number. If we already saw data, put it
626 // in rawBytes.
627 if (rawBytes != null) {
628 MergeField(typeId, UnknownField.CreateBuilder().AddLengthDelimited(rawBytes).Build());
629 rawBytes = null;
630 }
631 }
632 }
633 } else if (tag == WireFormat.MessageSetTag.Message) {
634 if (typeId == 0) {
635 // We haven't seen a type ID yet, so we have to store the raw bytes for now.
636 rawBytes = input.ReadBytes();
637 } else if (subBuilder == null) {
638 // We don't know how to parse this. Ignore it.
639 MergeField(typeId, UnknownField.CreateBuilder().AddLengthDelimited(input.ReadBytes()).Build());
640 } else {
641 // We already know the type, so we can parse directly from the input
642 // with no copying. Hooray!
643 input.ReadMessage(subBuilder, extensionRegistry);
644 }
645 } else {
646 // Unknown tag. Skip it.
647 if (!input.SkipField(tag)) {
648 break; // end of group
649 }
650 }
651 }
652
653 input.CheckLastTagWas(WireFormat.MessageSetTag.ItemEnd);
654
655 if (subBuilder != null) {
656 builder[field] = subBuilder.WeakBuild();
657 }
658 }
659 }
660 }
661}