blob: ecae53cec2db6d307617a0910d85fa72a4f61c85 [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.
500 if (field == null || wireType != WireFormat.GetWireType(field.FieldType)) {
501 return MergeFieldFrom(tag, input);
502 }
503
504 object value;
505 switch (field.FieldType) {
506 case FieldType.Group:
507 case FieldType.Message: {
508 IBuilder subBuilder;
509 if (defaultFieldInstance != null) {
510 subBuilder = defaultFieldInstance.WeakCreateBuilderForType();
511 } else {
512 subBuilder = builder.CreateBuilderForField(field);
513 }
514 if (!field.IsRepeated) {
515 subBuilder.WeakMergeFrom((IMessage)builder[field]);
516 }
517 if (field.FieldType == FieldType.Group) {
518 input.ReadGroup(field.FieldNumber, subBuilder, extensionRegistry);
519 } else {
520 input.ReadMessage(subBuilder, extensionRegistry);
521 }
522 value = subBuilder.WeakBuild();
523 break;
524 }
525 case FieldType.Enum: {
526 int rawValue = input.ReadEnum();
527 value = field.EnumType.FindValueByNumber(rawValue);
528 // If the number isn't recognized as a valid value for this enum,
529 // drop it.
530 if (value == null) {
531 MergeVarintField(fieldNumber, (ulong)rawValue);
532 return true;
533 }
534 break;
535 }
536 default:
537 value = input.ReadPrimitiveField(field.FieldType);
538 break;
539 }
540 if (field.IsRepeated) {
541 builder.WeakAddRepeatedField(field, value);
542 } else {
543 builder[field] = value;
544 }
545 return true;
546 }
547
548 /// <summary>
549 /// Called by MergeFieldFrom to parse a MessageSet extension.
550 /// </summary>
551 private void MergeMessageSetExtensionFromCodedStream(CodedInputStream input,
552 ExtensionRegistry extensionRegistry, IBuilder builder) {
553 MessageDescriptor type = builder.DescriptorForType;
554
555 // The wire format for MessageSet is:
556 // message MessageSet {
557 // repeated group Item = 1 {
558 // required int32 typeId = 2;
559 // required bytes message = 3;
560 // }
561 // }
562 // "typeId" is the extension's field number. The extension can only be
563 // a message type, where "message" contains the encoded bytes of that
564 // message.
565 //
566 // In practice, we will probably never see a MessageSet item in which
567 // the message appears before the type ID, or where either field does not
568 // appear exactly once. However, in theory such cases are valid, so we
569 // should be prepared to accept them.
570
571 int typeId = 0;
572 ByteString rawBytes = null; // If we encounter "message" before "typeId"
573 IBuilder subBuilder = null;
574 FieldDescriptor field = null;
575
576 while (true) {
577 uint tag = input.ReadTag();
578 if (tag == 0) {
579 break;
580 }
581
582 if (tag == WireFormat.MessageSetTag.TypeID) {
583 typeId = input.ReadInt32();
584 // Zero is not a valid type ID.
585 if (typeId != 0) {
586 ExtensionInfo extension = extensionRegistry[type, typeId];
587 if (extension != null) {
588 field = extension.Descriptor;
589 subBuilder = extension.DefaultInstance.WeakCreateBuilderForType();
590 IMessage originalMessage = (IMessage)builder[field];
591 if (originalMessage != null) {
592 subBuilder.WeakMergeFrom(originalMessage);
593 }
594 if (rawBytes != null) {
595 // We already encountered the message. Parse it now.
596 // TODO(jonskeet): Check this is okay. It's subtly different from the Java, as it doesn't create an input stream from rawBytes.
597 // In fact, why don't we just call MergeFrom(rawBytes)? And what about the extension registry?
598 subBuilder.WeakMergeFrom(rawBytes.CreateCodedInput());
599 rawBytes = null;
600 }
601 } else {
602 // Unknown extension number. If we already saw data, put it
603 // in rawBytes.
604 if (rawBytes != null) {
605 MergeField(typeId, UnknownField.CreateBuilder().AddLengthDelimited(rawBytes).Build());
606 rawBytes = null;
607 }
608 }
609 }
610 } else if (tag == WireFormat.MessageSetTag.Message) {
611 if (typeId == 0) {
612 // We haven't seen a type ID yet, so we have to store the raw bytes for now.
613 rawBytes = input.ReadBytes();
614 } else if (subBuilder == null) {
615 // We don't know how to parse this. Ignore it.
616 MergeField(typeId, UnknownField.CreateBuilder().AddLengthDelimited(input.ReadBytes()).Build());
617 } else {
618 // We already know the type, so we can parse directly from the input
619 // with no copying. Hooray!
620 input.ReadMessage(subBuilder, extensionRegistry);
621 }
622 } else {
623 // Unknown tag. Skip it.
624 if (!input.SkipField(tag)) {
625 break; // end of group
626 }
627 }
628 }
629
630 input.CheckLastTagWas(WireFormat.MessageSetTag.ItemEnd);
631
632 if (subBuilder != null) {
633 builder[field] = subBuilder.WeakBuild();
634 }
635 }
636 }
637 }
638}