UnknownFieldSet.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2015 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using System;
  33. using System.Collections.Generic;
  34. using System.IO;
  35. using System.Security;
  36. using Google.Protobuf.Reflection;
  37. namespace Google.Protobuf
  38. {
  39. /// <summary>
  40. /// Used to keep track of fields which were seen when parsing a protocol message
  41. /// but whose field numbers or types are unrecognized. This most frequently
  42. /// occurs when new fields are added to a message type and then messages containing
  43. /// those fields are read by old software that was built before the new types were
  44. /// added.
  45. ///
  46. /// Most users will never need to use this class directly.
  47. /// </summary>
  48. public sealed partial class UnknownFieldSet
  49. {
  50. private readonly IDictionary<int, UnknownField> fields;
  51. /// <summary>
  52. /// Creates a new UnknownFieldSet.
  53. /// </summary>
  54. internal UnknownFieldSet()
  55. {
  56. this.fields = new Dictionary<int, UnknownField>();
  57. }
  58. /// <summary>
  59. /// Checks whether or not the given field number is present in the set.
  60. /// </summary>
  61. internal bool HasField(int field)
  62. {
  63. return fields.ContainsKey(field);
  64. }
  65. /// <summary>
  66. /// Serializes the set and writes it to <paramref name="output"/>.
  67. /// </summary>
  68. public void WriteTo(CodedOutputStream output)
  69. {
  70. foreach (KeyValuePair<int, UnknownField> entry in fields)
  71. {
  72. entry.Value.WriteTo(entry.Key, output);
  73. }
  74. }
  75. /// <summary>
  76. /// Gets the number of bytes required to encode this set.
  77. /// </summary>
  78. public int CalculateSize()
  79. {
  80. int result = 0;
  81. foreach (KeyValuePair<int, UnknownField> entry in fields)
  82. {
  83. result += entry.Value.GetSerializedSize(entry.Key);
  84. }
  85. return result;
  86. }
  87. /// <summary>
  88. /// Checks if two unknown field sets are equal.
  89. /// </summary>
  90. public override bool Equals(object other)
  91. {
  92. if (ReferenceEquals(this, other))
  93. {
  94. return true;
  95. }
  96. UnknownFieldSet otherSet = other as UnknownFieldSet;
  97. IDictionary<int, UnknownField> otherFields = otherSet.fields;
  98. if (fields.Count != otherFields.Count)
  99. {
  100. return false;
  101. }
  102. foreach (KeyValuePair<int, UnknownField> leftEntry in fields)
  103. {
  104. UnknownField rightValue;
  105. if (!otherFields.TryGetValue(leftEntry.Key, out rightValue))
  106. {
  107. return false;
  108. }
  109. if (!leftEntry.Value.Equals(rightValue))
  110. {
  111. return false;
  112. }
  113. }
  114. return true;
  115. }
  116. /// <summary>
  117. /// Gets the unknown field set's hash code.
  118. /// </summary>
  119. public override int GetHashCode()
  120. {
  121. int ret = 1;
  122. foreach (KeyValuePair<int, UnknownField> field in fields)
  123. {
  124. // Use ^ here to make the field order irrelevant.
  125. int hash = field.Key.GetHashCode() ^ field.Value.GetHashCode();
  126. ret ^= hash;
  127. }
  128. return ret;
  129. }
  130. // Optimization: We keep around the last field that was
  131. // modified so that we can efficiently add to it multiple times in a
  132. // row (important when parsing an unknown repeated field).
  133. private int lastFieldNumber;
  134. private UnknownField lastField;
  135. private UnknownField GetOrAddField(int number)
  136. {
  137. if (lastField != null && number == lastFieldNumber)
  138. {
  139. return lastField;
  140. }
  141. if (number == 0)
  142. {
  143. return null;
  144. }
  145. UnknownField existing;
  146. if (fields.TryGetValue(number, out existing))
  147. {
  148. return existing;
  149. }
  150. lastField = new UnknownField();
  151. AddOrReplaceField(number, lastField);
  152. lastFieldNumber = number;
  153. return lastField;
  154. }
  155. /// <summary>
  156. /// Adds a field to the set. If a field with the same number already exists, it
  157. /// is replaced.
  158. /// </summary>
  159. internal UnknownFieldSet AddOrReplaceField(int number, UnknownField field)
  160. {
  161. if (number == 0)
  162. {
  163. throw new ArgumentOutOfRangeException("number", "Zero is not a valid field number.");
  164. }
  165. fields[number] = field;
  166. return this;
  167. }
  168. /// <summary>
  169. /// Parse a single field from <paramref name="ctx"/> and merge it
  170. /// into this set.
  171. /// </summary>
  172. /// <param name="ctx">The parse context from which to read the field</param>
  173. /// <returns>false if the tag is an "end group" tag, true otherwise</returns>
  174. private bool MergeFieldFrom(ref ParseContext ctx)
  175. {
  176. uint tag = ctx.LastTag;
  177. int number = WireFormat.GetTagFieldNumber(tag);
  178. switch (WireFormat.GetTagWireType(tag))
  179. {
  180. case WireFormat.WireType.Varint:
  181. {
  182. ulong uint64 = ctx.ReadUInt64();
  183. GetOrAddField(number).AddVarint(uint64);
  184. return true;
  185. }
  186. case WireFormat.WireType.Fixed32:
  187. {
  188. uint uint32 = ctx.ReadFixed32();
  189. GetOrAddField(number).AddFixed32(uint32);
  190. return true;
  191. }
  192. case WireFormat.WireType.Fixed64:
  193. {
  194. ulong uint64 = ctx.ReadFixed64();
  195. GetOrAddField(number).AddFixed64(uint64);
  196. return true;
  197. }
  198. case WireFormat.WireType.LengthDelimited:
  199. {
  200. ByteString bytes = ctx.ReadBytes();
  201. GetOrAddField(number).AddLengthDelimited(bytes);
  202. return true;
  203. }
  204. case WireFormat.WireType.StartGroup:
  205. {
  206. UnknownFieldSet set = new UnknownFieldSet();
  207. ParsingPrimitivesMessages.ReadGroup(ref ctx, number, set);
  208. GetOrAddField(number).AddGroup(set);
  209. return true;
  210. }
  211. case WireFormat.WireType.EndGroup:
  212. {
  213. return false;
  214. }
  215. default:
  216. throw InvalidProtocolBufferException.InvalidWireType();
  217. }
  218. }
  219. internal void MergeGroupFrom(ref ParseContext ctx)
  220. {
  221. while (true)
  222. {
  223. uint tag = ctx.ReadTag();
  224. if (tag == 0)
  225. {
  226. break;
  227. }
  228. if (!MergeFieldFrom(ref ctx))
  229. {
  230. break;
  231. }
  232. }
  233. }
  234. /// <summary>
  235. /// Create a new UnknownFieldSet if unknownFields is null.
  236. /// Parse a single field from <paramref name="input"/> and merge it
  237. /// into unknownFields. If <paramref name="input"/> is configured to discard unknown fields,
  238. /// <paramref name="unknownFields"/> will be returned as-is and the field will be skipped.
  239. /// </summary>
  240. /// <param name="unknownFields">The UnknownFieldSet which need to be merged</param>
  241. /// <param name="input">The coded input stream containing the field</param>
  242. /// <returns>The merged UnknownFieldSet</returns>
  243. public static UnknownFieldSet MergeFieldFrom(UnknownFieldSet unknownFields,
  244. CodedInputStream input)
  245. {
  246. ParseContext.Initialize(input, out ParseContext ctx);
  247. try
  248. {
  249. return MergeFieldFrom(unknownFields, ref ctx);
  250. }
  251. finally
  252. {
  253. ctx.CopyStateTo(input);
  254. }
  255. }
  256. /// <summary>
  257. /// Create a new UnknownFieldSet if unknownFields is null.
  258. /// Parse a single field from <paramref name="ctx"/> and merge it
  259. /// into unknownFields. If <paramref name="ctx"/> is configured to discard unknown fields,
  260. /// <paramref name="unknownFields"/> will be returned as-is and the field will be skipped.
  261. /// </summary>
  262. /// <param name="unknownFields">The UnknownFieldSet which need to be merged</param>
  263. /// <param name="ctx">The parse context from which to read the field</param>
  264. /// <returns>The merged UnknownFieldSet</returns>
  265. [SecuritySafeCritical]
  266. public static UnknownFieldSet MergeFieldFrom(UnknownFieldSet unknownFields,
  267. ref ParseContext ctx)
  268. {
  269. if (ctx.DiscardUnknownFields)
  270. {
  271. ParsingPrimitivesMessages.SkipLastField(ref ctx.buffer, ref ctx.state);
  272. return unknownFields;
  273. }
  274. if (unknownFields == null)
  275. {
  276. unknownFields = new UnknownFieldSet();
  277. }
  278. if (!unknownFields.MergeFieldFrom(ref ctx))
  279. {
  280. throw new InvalidProtocolBufferException("Merge an unknown field of end-group tag, indicating that the corresponding start-group was missing."); // match the old code-gen
  281. }
  282. return unknownFields;
  283. }
  284. /// <summary>
  285. /// Merges the fields from <paramref name="other"/> into this set.
  286. /// If a field number exists in both sets, the values in <paramref name="other"/>
  287. /// will be appended to the values in this set.
  288. /// </summary>
  289. private UnknownFieldSet MergeFrom(UnknownFieldSet other)
  290. {
  291. if (other != null)
  292. {
  293. foreach (KeyValuePair<int, UnknownField> entry in other.fields)
  294. {
  295. MergeField(entry.Key, entry.Value);
  296. }
  297. }
  298. return this;
  299. }
  300. /// <summary>
  301. /// Created a new UnknownFieldSet to <paramref name="unknownFields"/> if
  302. /// needed and merges the fields from <paramref name="other"/> into the first set.
  303. /// If a field number exists in both sets, the values in <paramref name="other"/>
  304. /// will be appended to the values in this set.
  305. /// </summary>
  306. public static UnknownFieldSet MergeFrom(UnknownFieldSet unknownFields,
  307. UnknownFieldSet other)
  308. {
  309. if (other == null)
  310. {
  311. return unknownFields;
  312. }
  313. if (unknownFields == null)
  314. {
  315. unknownFields = new UnknownFieldSet();
  316. }
  317. unknownFields.MergeFrom(other);
  318. return unknownFields;
  319. }
  320. /// <summary>
  321. /// Adds a field to the unknown field set. If a field with the same
  322. /// number already exists, the two are merged.
  323. /// </summary>
  324. private UnknownFieldSet MergeField(int number, UnknownField field)
  325. {
  326. if (number == 0)
  327. {
  328. throw new ArgumentOutOfRangeException("number", "Zero is not a valid field number.");
  329. }
  330. if (HasField(number))
  331. {
  332. GetOrAddField(number).MergeFrom(field);
  333. }
  334. else
  335. {
  336. AddOrReplaceField(number, field);
  337. }
  338. return this;
  339. }
  340. /// <summary>
  341. /// Clone an unknown field set from <paramref name="other"/>.
  342. /// </summary>
  343. public static UnknownFieldSet Clone(UnknownFieldSet other)
  344. {
  345. if (other == null)
  346. {
  347. return null;
  348. }
  349. UnknownFieldSet unknownFields = new UnknownFieldSet();
  350. unknownFields.MergeFrom(other);
  351. return unknownFields;
  352. }
  353. }
  354. }