UnknownField.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2017 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.Collections.ObjectModel;
  35. using Google.Protobuf.Collections;
  36. namespace Google.Protobuf
  37. {
  38. /// <summary>
  39. /// Represents a single field in an UnknownFieldSet.
  40. ///
  41. /// An UnknownField consists of four lists of values. The lists correspond
  42. /// to the four "wire types" used in the protocol buffer binary format.
  43. /// Normally, only one of the four lists will contain any values, since it
  44. /// is impossible to define a valid message type that declares two different
  45. /// types for the same field number. However, the code is designed to allow
  46. /// for the case where the same unknown field number is encountered using
  47. /// multiple different wire types.
  48. ///
  49. /// </summary>
  50. internal sealed class UnknownField
  51. {
  52. private List<ulong> varintList;
  53. private List<uint> fixed32List;
  54. private List<ulong> fixed64List;
  55. private List<ByteString> lengthDelimitedList;
  56. /// <summary>
  57. /// Creates a new UnknownField.
  58. /// </summary>
  59. public UnknownField()
  60. {
  61. }
  62. /// <summary>
  63. /// Checks if two unknown field are equal.
  64. /// </summary>
  65. public override bool Equals(object other)
  66. {
  67. if (ReferenceEquals(this, other))
  68. {
  69. return true;
  70. }
  71. UnknownField otherField = other as UnknownField;
  72. return otherField != null
  73. && Lists.Equals(varintList, otherField.varintList)
  74. && Lists.Equals(fixed32List, otherField.fixed32List)
  75. && Lists.Equals(fixed64List, otherField.fixed64List)
  76. && Lists.Equals(lengthDelimitedList, otherField.lengthDelimitedList);
  77. }
  78. /// <summary>
  79. /// Get the hash code of the unknown field.
  80. /// </summary>
  81. public override int GetHashCode()
  82. {
  83. int hash = 43;
  84. hash = hash * 47 + Lists.GetHashCode(varintList);
  85. hash = hash * 47 + Lists.GetHashCode(fixed32List);
  86. hash = hash * 47 + Lists.GetHashCode(fixed64List);
  87. hash = hash * 47 + Lists.GetHashCode(lengthDelimitedList);
  88. return hash;
  89. }
  90. /// <summary>
  91. /// Serializes the field, including the field number, and writes it to
  92. /// <paramref name="output"/>
  93. /// </summary>
  94. /// <param name="fieldNumber">The unknown field number.</param>
  95. /// <param name="output">The CodedOutputStream to write to.</param>
  96. internal void WriteTo(int fieldNumber, CodedOutputStream output)
  97. {
  98. if (varintList != null)
  99. {
  100. foreach (ulong value in varintList)
  101. {
  102. output.WriteTag(fieldNumber, WireFormat.WireType.Varint);
  103. output.WriteUInt64(value);
  104. }
  105. }
  106. if (fixed32List != null)
  107. {
  108. foreach (uint value in fixed32List)
  109. {
  110. output.WriteTag(fieldNumber, WireFormat.WireType.Fixed32);
  111. output.WriteFixed32(value);
  112. }
  113. }
  114. if (fixed64List != null)
  115. {
  116. foreach (ulong value in fixed64List)
  117. {
  118. output.WriteTag(fieldNumber, WireFormat.WireType.Fixed64);
  119. output.WriteFixed64(value);
  120. }
  121. }
  122. if (lengthDelimitedList != null)
  123. {
  124. foreach (ByteString value in lengthDelimitedList)
  125. {
  126. output.WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
  127. output.WriteBytes(value);
  128. }
  129. }
  130. }
  131. /// <summary>
  132. /// Computes the number of bytes required to encode this field, including field
  133. /// number.
  134. /// </summary>
  135. internal int GetSerializedSize(int fieldNumber)
  136. {
  137. int result = 0;
  138. if (varintList != null)
  139. {
  140. result += CodedOutputStream.ComputeTagSize(fieldNumber) * varintList.Count;
  141. foreach (ulong value in varintList)
  142. {
  143. result += CodedOutputStream.ComputeUInt64Size(value);
  144. }
  145. }
  146. if (fixed32List != null)
  147. {
  148. result += CodedOutputStream.ComputeTagSize(fieldNumber) * fixed32List.Count;
  149. result += CodedOutputStream.ComputeFixed32Size(1) * fixed32List.Count;
  150. }
  151. if (fixed64List != null)
  152. {
  153. result += CodedOutputStream.ComputeTagSize(fieldNumber) * fixed64List.Count;
  154. result += CodedOutputStream.ComputeFixed64Size(1) * fixed64List.Count;
  155. }
  156. if (lengthDelimitedList != null)
  157. {
  158. result += CodedOutputStream.ComputeTagSize(fieldNumber) * lengthDelimitedList.Count;
  159. foreach (ByteString value in lengthDelimitedList)
  160. {
  161. result += CodedOutputStream.ComputeBytesSize(value);
  162. }
  163. }
  164. return result;
  165. }
  166. /// <summary>
  167. /// Merge the values in <paramref name="other" /> into this field. For each list
  168. /// of values, <paramref name="other"/>'s values are append to the ones in this
  169. /// field.
  170. /// </summary>
  171. internal UnknownField MergeFrom(UnknownField other)
  172. {
  173. varintList = AddAll(varintList, other.varintList);
  174. fixed32List = AddAll(fixed32List, other.fixed32List);
  175. fixed64List = AddAll(fixed64List, other.fixed64List);
  176. lengthDelimitedList = AddAll(lengthDelimitedList, other.lengthDelimitedList);
  177. return this;
  178. }
  179. /// <summary>
  180. /// Returns a new list containing all of the given specified values from
  181. /// both the <paramref name="current"/> and <paramref name="extras"/> lists.
  182. /// If <paramref name="current" /> is null and <paramref name="extras"/> is empty,
  183. /// null is returned. Otherwise, either a new list is created (if <paramref name="current" />
  184. /// is null) or the elements of <paramref name="extras"/> are added to <paramref name="current" />.
  185. /// </summary>
  186. private static List<T> AddAll<T>(List<T> current, IList<T> extras)
  187. {
  188. if (extras.Count == 0)
  189. {
  190. return current;
  191. }
  192. if (current == null)
  193. {
  194. current = new List<T>(extras);
  195. }
  196. else
  197. {
  198. current.AddRange(extras);
  199. }
  200. return current;
  201. }
  202. /// <summary>
  203. /// Adds a varint value.
  204. /// </summary>
  205. internal UnknownField AddVarint(ulong value)
  206. {
  207. varintList = Add(varintList, value);
  208. return this;
  209. }
  210. /// <summary>
  211. /// Adds a fixed32 value.
  212. /// </summary>
  213. internal UnknownField AddFixed32(uint value)
  214. {
  215. fixed32List = Add(fixed32List, value);
  216. return this;
  217. }
  218. /// <summary>
  219. /// Adds a fixed64 value.
  220. /// </summary>
  221. internal UnknownField AddFixed64(ulong value)
  222. {
  223. fixed64List = Add(fixed64List, value);
  224. return this;
  225. }
  226. /// <summary>
  227. /// Adds a length-delimited value.
  228. /// </summary>
  229. internal UnknownField AddLengthDelimited(ByteString value)
  230. {
  231. lengthDelimitedList = Add(lengthDelimitedList, value);
  232. return this;
  233. }
  234. /// <summary>
  235. /// Adds <paramref name="value"/> to the <paramref name="list"/>, creating
  236. /// a new list if <paramref name="list"/> is null. The list is returned - either
  237. /// the original reference or the new list.
  238. /// </summary>
  239. private static List<T> Add<T>(List<T> list, T value)
  240. {
  241. if (list == null)
  242. {
  243. list = new List<T>();
  244. }
  245. list.Add(value);
  246. return list;
  247. }
  248. }
  249. }