CodedOutputStream.ComputeSize.cs 10 KB

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