CodedOutputStream.ComputeSize.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 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. namespace Google.Protobuf
  34. {
  35. // This part of CodedOutputStream provides all the static entry points that are used
  36. // by generated code and internally to compute the size of messages prior to being
  37. // written to an instance of CodedOutputStream.
  38. public sealed partial class CodedOutputStream
  39. {
  40. private const int LittleEndian64Size = 8;
  41. private const int LittleEndian32Size = 4;
  42. internal const int DoubleSize = LittleEndian64Size;
  43. internal const int FloatSize = LittleEndian32Size;
  44. internal const int BoolSize = 1;
  45. /// <summary>
  46. /// Computes the number of bytes that would be needed to encode a
  47. /// double field, including the tag.
  48. /// </summary>
  49. public static int ComputeDoubleSize(double value)
  50. {
  51. return DoubleSize;
  52. }
  53. /// <summary>
  54. /// Computes the number of bytes that would be needed to encode a
  55. /// float field, including the tag.
  56. /// </summary>
  57. public static int ComputeFloatSize(float value)
  58. {
  59. return FloatSize;
  60. }
  61. /// <summary>
  62. /// Computes the number of bytes that would be needed to encode a
  63. /// uint64 field, including the tag.
  64. /// </summary>
  65. public static int ComputeUInt64Size(ulong value)
  66. {
  67. return ComputeRawVarint64Size(value);
  68. }
  69. /// <summary>
  70. /// Computes the number of bytes that would be needed to encode an
  71. /// int64 field, including the tag.
  72. /// </summary>
  73. public static int ComputeInt64Size(long value)
  74. {
  75. return ComputeRawVarint64Size((ulong) value);
  76. }
  77. /// <summary>
  78. /// Computes the number of bytes that would be needed to encode an
  79. /// int32 field, including the tag.
  80. /// </summary>
  81. public static int ComputeInt32Size(int value)
  82. {
  83. if (value >= 0)
  84. {
  85. return ComputeRawVarint32Size((uint) value);
  86. }
  87. else
  88. {
  89. // Must sign-extend.
  90. return 10;
  91. }
  92. }
  93. /// <summary>
  94. /// Computes the number of bytes that would be needed to encode a
  95. /// fixed64 field, including the tag.
  96. /// </summary>
  97. public static int ComputeFixed64Size(ulong value)
  98. {
  99. return LittleEndian64Size;
  100. }
  101. /// <summary>
  102. /// Computes the number of bytes that would be needed to encode a
  103. /// fixed32 field, including the tag.
  104. /// </summary>
  105. public static int ComputeFixed32Size(uint value)
  106. {
  107. return LittleEndian32Size;
  108. }
  109. /// <summary>
  110. /// Computes the number of bytes that would be needed to encode a
  111. /// bool field, including the tag.
  112. /// </summary>
  113. public static int ComputeBoolSize(bool value)
  114. {
  115. return BoolSize;
  116. }
  117. /// <summary>
  118. /// Computes the number of bytes that would be needed to encode a
  119. /// string field, including the tag.
  120. /// </summary>
  121. public static int ComputeStringSize(String value)
  122. {
  123. int byteArraySize = WritingPrimitives.Utf8Encoding.GetByteCount(value);
  124. return ComputeLengthSize(byteArraySize) + byteArraySize;
  125. }
  126. /// <summary>
  127. /// Computes the number of bytes that would be needed to encode a
  128. /// group field, including the tag.
  129. /// </summary>
  130. public static int ComputeGroupSize(IMessage value)
  131. {
  132. return value.CalculateSize();
  133. }
  134. /// <summary>
  135. /// Computes the number of bytes that would be needed to encode an
  136. /// embedded message field, including the tag.
  137. /// </summary>
  138. public static int ComputeMessageSize(IMessage value)
  139. {
  140. int size = value.CalculateSize();
  141. return ComputeLengthSize(size) + size;
  142. }
  143. /// <summary>
  144. /// Computes the number of bytes that would be needed to encode a
  145. /// bytes field, including the tag.
  146. /// </summary>
  147. public static int ComputeBytesSize(ByteString value)
  148. {
  149. return ComputeLengthSize(value.Length) + value.Length;
  150. }
  151. /// <summary>
  152. /// Computes the number of bytes that would be needed to encode a
  153. /// uint32 field, including the tag.
  154. /// </summary>
  155. public static int ComputeUInt32Size(uint value)
  156. {
  157. return ComputeRawVarint32Size(value);
  158. }
  159. /// <summary>
  160. /// Computes the number of bytes that would be needed to encode a
  161. /// enum field, including the tag. The caller is responsible for
  162. /// converting the enum value to its numeric value.
  163. /// </summary>
  164. public static int ComputeEnumSize(int value)
  165. {
  166. // Currently just a pass-through, but it's nice to separate it logically.
  167. return ComputeInt32Size(value);
  168. }
  169. /// <summary>
  170. /// Computes the number of bytes that would be needed to encode an
  171. /// sfixed32 field, including the tag.
  172. /// </summary>
  173. public static int ComputeSFixed32Size(int value)
  174. {
  175. return LittleEndian32Size;
  176. }
  177. /// <summary>
  178. /// Computes the number of bytes that would be needed to encode an
  179. /// sfixed64 field, including the tag.
  180. /// </summary>
  181. public static int ComputeSFixed64Size(long value)
  182. {
  183. return LittleEndian64Size;
  184. }
  185. /// <summary>
  186. /// Computes the number of bytes that would be needed to encode an
  187. /// sint32 field, including the tag.
  188. /// </summary>
  189. public static int ComputeSInt32Size(int value)
  190. {
  191. return ComputeRawVarint32Size(WritingPrimitives.EncodeZigZag32(value));
  192. }
  193. /// <summary>
  194. /// Computes the number of bytes that would be needed to encode an
  195. /// sint64 field, including the tag.
  196. /// </summary>
  197. public static int ComputeSInt64Size(long value)
  198. {
  199. return ComputeRawVarint64Size(WritingPrimitives.EncodeZigZag64(value));
  200. }
  201. /// <summary>
  202. /// Computes the number of bytes that would be needed to encode a length,
  203. /// as written by <see cref="WriteLength"/>.
  204. /// </summary>
  205. public static int ComputeLengthSize(int length)
  206. {
  207. return ComputeRawVarint32Size((uint) length);
  208. }
  209. /// <summary>
  210. /// Computes the number of bytes that would be needed to encode a varint.
  211. /// </summary>
  212. public static int ComputeRawVarint32Size(uint value)
  213. {
  214. if ((value & (0xffffffff << 7)) == 0)
  215. {
  216. return 1;
  217. }
  218. if ((value & (0xffffffff << 14)) == 0)
  219. {
  220. return 2;
  221. }
  222. if ((value & (0xffffffff << 21)) == 0)
  223. {
  224. return 3;
  225. }
  226. if ((value & (0xffffffff << 28)) == 0)
  227. {
  228. return 4;
  229. }
  230. return 5;
  231. }
  232. /// <summary>
  233. /// Computes the number of bytes that would be needed to encode a varint.
  234. /// </summary>
  235. public static int ComputeRawVarint64Size(ulong value)
  236. {
  237. if ((value & (0xffffffffffffffffL << 7)) == 0)
  238. {
  239. return 1;
  240. }
  241. if ((value & (0xffffffffffffffffL << 14)) == 0)
  242. {
  243. return 2;
  244. }
  245. if ((value & (0xffffffffffffffffL << 21)) == 0)
  246. {
  247. return 3;
  248. }
  249. if ((value & (0xffffffffffffffffL << 28)) == 0)
  250. {
  251. return 4;
  252. }
  253. if ((value & (0xffffffffffffffffL << 35)) == 0)
  254. {
  255. return 5;
  256. }
  257. if ((value & (0xffffffffffffffffL << 42)) == 0)
  258. {
  259. return 6;
  260. }
  261. if ((value & (0xffffffffffffffffL << 49)) == 0)
  262. {
  263. return 7;
  264. }
  265. if ((value & (0xffffffffffffffffL << 56)) == 0)
  266. {
  267. return 8;
  268. }
  269. if ((value & (0xffffffffffffffffL << 63)) == 0)
  270. {
  271. return 9;
  272. }
  273. return 10;
  274. }
  275. /// <summary>
  276. /// Computes the number of bytes that would be needed to encode a tag.
  277. /// </summary>
  278. public static int ComputeTagSize(int fieldNumber)
  279. {
  280. return ComputeRawVarint32Size(WireFormat.MakeTag(fieldNumber, 0));
  281. }
  282. }
  283. }