blob: f8cbce4c0bbcd2b432d7bac68ebbe1904c4c787f [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
Jon Skeet43da7ae2009-05-28 21:45:43 +0100197 public override bool Equals(object other) {
198 if (ReferenceEquals(this, other)) {
199 return true;
200 }
201 UnknownFieldSet otherSet = other as UnknownFieldSet;
202 return otherSet != null && Dictionaries.Equals(fields, otherSet.fields);
203 }
204
205 public override int GetHashCode() {
206 return Dictionaries.GetHashCode(fields);
207 }
208
Jon Skeet68036862008-10-22 13:30:34 +0100209 /// <summary>
210 /// Parses an UnknownFieldSet from the given input.
211 /// </summary>
212 public static UnknownFieldSet ParseFrom(CodedInputStream input) {
213 return CreateBuilder().MergeFrom(input).Build();
214 }
215
216 /// <summary>
217 /// Parses an UnknownFieldSet from the given data.
218 /// </summary>
219 public static UnknownFieldSet ParseFrom(ByteString data) {
220 return CreateBuilder().MergeFrom(data).Build();
221 }
222
223 /// <summary>
224 /// Parses an UnknownFieldSet from the given data.
225 /// </summary>
226 public static UnknownFieldSet ParseFrom(byte[] data) {
227 return CreateBuilder().MergeFrom(data).Build();
228 }
229
230 /// <summary>
231 /// Parses an UnknownFieldSet from the given input.
232 /// </summary>
233 public static UnknownFieldSet ParseFrom(Stream input) {
234 return CreateBuilder().MergeFrom(input).Build();
235 }
236
237 /// <summary>
238 /// Builder for UnknownFieldSets.
239 /// </summary>
240 public sealed class Builder
241 {
242 /// <summary>
243 /// Mapping from number to field. Note that by using a SortedList we ensure
244 /// that the fields will be serialized in ascending order.
245 /// </summary>
246 private IDictionary<int, UnknownField> fields = new SortedList<int, UnknownField>();
247
248 // Optimization: We keep around a builder for the last field that was
249 // modified so that we can efficiently add to it multiple times in a
250 // row (important when parsing an unknown repeated field).
Jon Skeet43da7ae2009-05-28 21:45:43 +0100251 private int lastFieldNumber;
252 private UnknownField.Builder lastField;
Jon Skeet68036862008-10-22 13:30:34 +0100253
254 internal Builder() {
255 }
256
257 /// <summary>
258 /// Returns a field builder for the specified field number, including any values
259 /// which already exist.
260 /// </summary>
261 private UnknownField.Builder GetFieldBuilder(int number) {
262 if (lastField != null) {
263 if (number == lastFieldNumber) {
264 return lastField;
265 }
266 // Note: AddField() will reset lastField and lastFieldNumber.
267 AddField(lastFieldNumber, lastField.Build());
268 }
269 if (number == 0) {
270 return null;
271 }
272
273 lastField = UnknownField.CreateBuilder();
274 UnknownField existing;
275 if (fields.TryGetValue(number, out existing)) {
276 lastField.MergeFrom(existing);
277 }
278 lastFieldNumber = number;
279 return lastField;
280 }
281
282 /// <summary>
283 /// Build the UnknownFieldSet and return it. Once this method has been called,
284 /// this instance will no longer be usable. Calling any method after this
285 /// will throw a NullReferenceException.
286 /// </summary>
287 public UnknownFieldSet Build() {
288 GetFieldBuilder(0); // Force lastField to be built.
289 UnknownFieldSet result = fields.Count == 0 ? DefaultInstance : new UnknownFieldSet(fields);
290 fields = null;
291 return result;
292 }
293
294 /// <summary>
295 /// Adds a field to the set. If a field with the same number already exists, it
296 /// is replaced.
297 /// </summary>
298 public Builder AddField(int number, UnknownField field) {
299 if (number == 0) {
300 throw new ArgumentOutOfRangeException("number", "Zero is not a valid field number.");
301 }
302 if (lastField != null && lastFieldNumber == number) {
303 // Discard this.
304 lastField = null;
305 lastFieldNumber = 0;
306 }
307 fields[number] = field;
308 return this;
309 }
310
311 /// <summary>
312 /// Resets the builder to an empty set.
313 /// </summary>
314 public Builder Clear() {
315 fields.Clear();
316 lastFieldNumber = 0;
317 lastField = null;
318 return this;
319 }
320
321 /// <summary>
322 /// Parse an entire message from <paramref name="input"/> and merge
323 /// its fields into this set.
324 /// </summary>
325 public Builder MergeFrom(CodedInputStream input) {
326 while (true) {
327 uint tag = input.ReadTag();
328 if (tag == 0 || !MergeFieldFrom(tag, input)) {
329 break;
330 }
331 }
332 return this;
333 }
334
335 /// <summary>
336 /// Parse a single field from <paramref name="input"/> and merge it
337 /// into this set.
338 /// </summary>
339 /// <param name="tag">The field's tag number, which was already parsed.</param>
340 /// <param name="input">The coded input stream containing the field</param>
341 /// <returns>false if the tag is an "end group" tag, true otherwise</returns>
342 public bool MergeFieldFrom(uint tag, CodedInputStream input) {
343 int number = WireFormat.GetTagFieldNumber(tag);
344 switch (WireFormat.GetTagWireType(tag)) {
345 case WireFormat.WireType.Varint:
346 GetFieldBuilder(number).AddVarint(input.ReadUInt64());
347 return true;
348 case WireFormat.WireType.Fixed64:
349 GetFieldBuilder(number).AddFixed64(input.ReadFixed64());
350 return true;
351 case WireFormat.WireType.LengthDelimited:
352 GetFieldBuilder(number).AddLengthDelimited(input.ReadBytes());
353 return true;
354 case WireFormat.WireType.StartGroup: {
355 Builder subBuilder = CreateBuilder();
356 input.ReadUnknownGroup(number, subBuilder);
357 GetFieldBuilder(number).AddGroup(subBuilder.Build());
358 return true;
359 }
360 case WireFormat.WireType.EndGroup:
361 return false;
362 case WireFormat.WireType.Fixed32:
363 GetFieldBuilder(number).AddFixed32(input.ReadFixed32());
364 return true;
365 default:
366 throw InvalidProtocolBufferException.InvalidWireType();
367 }
368 }
369
370 /// <summary>
371 /// Parses <paramref name="input"/> 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(Stream input) {
376 CodedInputStream codedInput = CodedInputStream.CreateInstance(input);
377 MergeFrom(codedInput);
378 codedInput.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(ByteString data) {
388 CodedInputStream input = data.CreateCodedInput();
389 MergeFrom(input);
390 input.CheckLastTagWas(0);
391 return this;
392 }
393
394 /// <summary>
395 /// Parses <paramref name="data"/> as an UnknownFieldSet and merge it
396 /// with the set being built. This is just a small wrapper around
397 /// MergeFrom(CodedInputStream).
398 /// </summary>
399 public Builder MergeFrom(byte[] data) {
400 CodedInputStream input = CodedInputStream.CreateInstance(data);
401 MergeFrom(input);
402 input.CheckLastTagWas(0);
403 return this;
404 }
405
406 /// <summary>
407 /// Convenience method for merging a new field containing a single varint
408 /// value. This is used in particular when an unknown enum value is
409 /// encountered.
410 /// </summary>
411 public Builder MergeVarintField(int number, ulong value) {
412 if (number == 0) {
413 throw new ArgumentOutOfRangeException("number", "Zero is not a valid field number.");
414 }
415 GetFieldBuilder(number).AddVarint(value);
416 return this;
417 }
418
419 /// <summary>
420 /// Merges the fields from <paramref name="other"/> into this set.
421 /// If a field number exists in both sets, the values in <paramref name="other"/>
422 /// will be appended to the values in this set.
423 /// </summary>
424 public Builder MergeFrom(UnknownFieldSet other) {
425 if (other != DefaultInstance) {
426 foreach(KeyValuePair<int, UnknownField> entry in other.fields) {
427 MergeField(entry.Key, entry.Value);
428 }
429 }
430 return this;
431 }
432
433 /// <summary>
434 /// Checks if the given field number is present in the set.
435 /// </summary>
436 public bool HasField(int number) {
437 if (number == 0) {
438 throw new ArgumentOutOfRangeException("number", "Zero is not a valid field number.");
439 }
440 return number == lastFieldNumber || fields.ContainsKey(number);
441 }
442
443 /// <summary>
444 /// Adds a field to the unknown field set. If a field with the same
445 /// number already exists, the two are merged.
446 /// </summary>
447 public Builder MergeField(int number, UnknownField field) {
448 if (number == 0) {
449 throw new ArgumentOutOfRangeException("number", "Zero is not a valid field number.");
450 }
451 if (HasField(number)) {
452 GetFieldBuilder(number).MergeFrom(field);
453 } else {
454 // Optimization: We could call getFieldBuilder(number).mergeFrom(field)
455 // in this case, but that would create a copy of the Field object.
456 // We'd rather reuse the one passed to us, so call AddField() instead.
457 AddField(number, field);
458 }
459 return this;
460 }
461
462 internal void MergeFrom(CodedInputStream input, ExtensionRegistry extensionRegistry, IBuilder builder) {
463 while (true) {
464 uint tag = input.ReadTag();
465 if (tag == 0) {
466 break;
467 }
468 if (!MergeFieldFrom(input, extensionRegistry, builder, tag)) {
469 // end group tag
470 break;
471 }
472 }
473 }
474
475 /// <summary>
476 /// Like <see cref="MergeFrom(CodedInputStream, ExtensionRegistry, IBuilder)" />
477 /// but parses a single field.
478 /// </summary>
479 /// <param name="input">The input to read the field from</param>
480 /// <param name="extensionRegistry">Registry to use when an extension field is encountered</param>
481 /// <param name="builder">Builder to merge field into, if it's a known field</param>
482 /// <param name="tag">The tag, which should already have been read from the input</param>
483 /// <returns>true unless the tag is an end-group tag</returns>
484 internal bool MergeFieldFrom(CodedInputStream input,
485 ExtensionRegistry extensionRegistry, IBuilder builder, uint tag) {
486
Jon Skeet68036862008-10-22 13:30:34 +0100487 MessageDescriptor type = builder.DescriptorForType;
488 if (type.Options.MessageSetWireFormat && tag == WireFormat.MessageSetTag.ItemStart) {
489 MergeMessageSetExtensionFromCodedStream(input, extensionRegistry, builder);
490 return true;
491 }
492
493 WireFormat.WireType wireType = WireFormat.GetTagWireType(tag);
494 int fieldNumber = WireFormat.GetTagFieldNumber(tag);
495
496 FieldDescriptor field;
497 IMessage defaultFieldInstance = null;
498
499 if (type.IsExtensionNumber(fieldNumber)) {
500 ExtensionInfo extension = extensionRegistry[type, fieldNumber];
501 if (extension == null) {
502 field = null;
503 } else {
504 field = extension.Descriptor;
505 defaultFieldInstance = extension.DefaultInstance;
506 }
507 } else {
508 field = type.FindFieldByNumber(fieldNumber);
509 }
510
511 // Unknown field or wrong wire type. Skip.
Jon Skeet25a28582009-02-18 16:06:22 +0000512 if (field == null || wireType != WireFormat.GetWireType(field)) {
Jon Skeet68036862008-10-22 13:30:34 +0100513 return MergeFieldFrom(tag, input);
514 }
515
Jon Skeet25a28582009-02-18 16:06:22 +0000516 if (field.IsPacked) {
517 int length = (int)input.ReadRawVarint32();
518 int limit = input.PushLimit(length);
519 if (field.FieldType == FieldType.Enum) {
520 while (!input.ReachedLimit) {
Jon Skeet68036862008-10-22 13:30:34 +0100521 int rawValue = input.ReadEnum();
Jon Skeet25a28582009-02-18 16:06:22 +0000522 object value = field.EnumType.FindValueByNumber(rawValue);
Jon Skeet68036862008-10-22 13:30:34 +0100523 if (value == null) {
Jon Skeet25a28582009-02-18 16:06:22 +0000524 // If the number isn't recognized as a valid value for this
525 // enum, drop it (don't even add it to unknownFields).
Jon Skeet68036862008-10-22 13:30:34 +0100526 return true;
527 }
Jon Skeet25a28582009-02-18 16:06:22 +0000528 builder.WeakAddRepeatedField(field, value);
Jon Skeet68036862008-10-22 13:30:34 +0100529 }
Jon Skeet25a28582009-02-18 16:06:22 +0000530 } else {
531 while (!input.ReachedLimit) {
532 Object value = input.ReadPrimitiveField(field.FieldType);
533 builder.WeakAddRepeatedField(field, value);
534 }
535 }
536 input.PopLimit(limit);
Jon Skeet68036862008-10-22 13:30:34 +0100537 } else {
Jon Skeet25a28582009-02-18 16:06:22 +0000538 object value;
539 switch (field.FieldType) {
540 case FieldType.Group:
541 case FieldType.Message: {
542 IBuilder subBuilder;
543 if (defaultFieldInstance != null) {
544 subBuilder = defaultFieldInstance.WeakCreateBuilderForType();
545 } else {
546 subBuilder = builder.CreateBuilderForField(field);
547 }
548 if (!field.IsRepeated) {
549 subBuilder.WeakMergeFrom((IMessage)builder[field]);
550 }
551 if (field.FieldType == FieldType.Group) {
552 input.ReadGroup(field.FieldNumber, subBuilder, extensionRegistry);
553 } else {
554 input.ReadMessage(subBuilder, extensionRegistry);
555 }
556 value = subBuilder.WeakBuild();
557 break;
558 }
559 case FieldType.Enum: {
560 int rawValue = input.ReadEnum();
561 value = field.EnumType.FindValueByNumber(rawValue);
562 // If the number isn't recognized as a valid value for this enum,
563 // drop it.
564 if (value == null) {
565 MergeVarintField(fieldNumber, (ulong)rawValue);
566 return true;
567 }
568 break;
569 }
570 default:
571 value = input.ReadPrimitiveField(field.FieldType);
572 break;
573 }
574 if (field.IsRepeated) {
575 builder.WeakAddRepeatedField(field, value);
576 } else {
577 builder[field] = value;
578 }
Jon Skeet68036862008-10-22 13:30:34 +0100579 }
580 return true;
581 }
582
583 /// <summary>
584 /// Called by MergeFieldFrom to parse a MessageSet extension.
585 /// </summary>
586 private void MergeMessageSetExtensionFromCodedStream(CodedInputStream input,
587 ExtensionRegistry extensionRegistry, IBuilder builder) {
588 MessageDescriptor type = builder.DescriptorForType;
589
590 // The wire format for MessageSet is:
591 // message MessageSet {
592 // repeated group Item = 1 {
593 // required int32 typeId = 2;
594 // required bytes message = 3;
595 // }
596 // }
597 // "typeId" is the extension's field number. The extension can only be
598 // a message type, where "message" contains the encoded bytes of that
599 // message.
600 //
601 // In practice, we will probably never see a MessageSet item in which
602 // the message appears before the type ID, or where either field does not
603 // appear exactly once. However, in theory such cases are valid, so we
604 // should be prepared to accept them.
605
606 int typeId = 0;
607 ByteString rawBytes = null; // If we encounter "message" before "typeId"
608 IBuilder subBuilder = null;
609 FieldDescriptor field = null;
610
611 while (true) {
612 uint tag = input.ReadTag();
613 if (tag == 0) {
614 break;
615 }
616
617 if (tag == WireFormat.MessageSetTag.TypeID) {
618 typeId = input.ReadInt32();
619 // Zero is not a valid type ID.
620 if (typeId != 0) {
621 ExtensionInfo extension = extensionRegistry[type, typeId];
622 if (extension != null) {
623 field = extension.Descriptor;
624 subBuilder = extension.DefaultInstance.WeakCreateBuilderForType();
625 IMessage originalMessage = (IMessage)builder[field];
626 if (originalMessage != null) {
627 subBuilder.WeakMergeFrom(originalMessage);
628 }
629 if (rawBytes != null) {
630 // We already encountered the message. Parse it now.
631 // TODO(jonskeet): Check this is okay. It's subtly different from the Java, as it doesn't create an input stream from rawBytes.
632 // In fact, why don't we just call MergeFrom(rawBytes)? And what about the extension registry?
633 subBuilder.WeakMergeFrom(rawBytes.CreateCodedInput());
634 rawBytes = null;
635 }
636 } else {
637 // Unknown extension number. If we already saw data, put it
638 // in rawBytes.
639 if (rawBytes != null) {
640 MergeField(typeId, UnknownField.CreateBuilder().AddLengthDelimited(rawBytes).Build());
641 rawBytes = null;
642 }
643 }
644 }
645 } else if (tag == WireFormat.MessageSetTag.Message) {
646 if (typeId == 0) {
647 // We haven't seen a type ID yet, so we have to store the raw bytes for now.
648 rawBytes = input.ReadBytes();
649 } else if (subBuilder == null) {
650 // We don't know how to parse this. Ignore it.
651 MergeField(typeId, UnknownField.CreateBuilder().AddLengthDelimited(input.ReadBytes()).Build());
652 } else {
653 // We already know the type, so we can parse directly from the input
654 // with no copying. Hooray!
655 input.ReadMessage(subBuilder, extensionRegistry);
656 }
657 } else {
658 // Unknown tag. Skip it.
659 if (!input.SkipField(tag)) {
660 break; // end of group
661 }
662 }
663 }
664
665 input.CheckLastTagWas(WireFormat.MessageSetTag.ItemEnd);
666
667 if (subBuilder != null) {
668 builder[field] = subBuilder.WeakBuild();
669 }
670 }
671 }
672 }
673}