CodedOutputStream.ComputeSize.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // http://github.com/jskeet/dotnet-protobufs/
  5. // Original C++/Java/Python code:
  6. // http://code.google.com/p/protobuf/
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. // * Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following disclaimer
  16. // in the documentation and/or other materials provided with the
  17. // distribution.
  18. // * Neither the name of Google Inc. nor the names of its
  19. // contributors may be used to endorse or promote products derived from
  20. // this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #endregion
  34. using System;
  35. using System.Globalization;
  36. using System.Text;
  37. using Google.Protobuf.Descriptors;
  38. namespace Google.Protobuf
  39. {
  40. // This part of CodedOutputStream provides all the static entry points that are used
  41. // by generated code and internally to compute the size of messages prior to being
  42. // written to an instance of CodedOutputStream.
  43. public sealed partial class CodedOutputStream
  44. {
  45. private const int LittleEndian64Size = 8;
  46. private const int LittleEndian32Size = 4;
  47. /// <summary>
  48. /// Compute the number of bytes that would be needed to encode a
  49. /// double field, including the tag.
  50. /// </summary>
  51. public static int ComputeDoubleSize(int fieldNumber, double value)
  52. {
  53. return ComputeTagSize(fieldNumber) + LittleEndian64Size;
  54. }
  55. /// <summary>
  56. /// Compute the number of bytes that would be needed to encode a
  57. /// float field, including the tag.
  58. /// </summary>
  59. public static int ComputeFloatSize(int fieldNumber, float value)
  60. {
  61. return ComputeTagSize(fieldNumber) + LittleEndian32Size;
  62. }
  63. /// <summary>
  64. /// Compute the number of bytes that would be needed to encode a
  65. /// uint64 field, including the tag.
  66. /// </summary>
  67. public static int ComputeUInt64Size(int fieldNumber, ulong value)
  68. {
  69. return ComputeTagSize(fieldNumber) + ComputeRawVarint64Size(value);
  70. }
  71. /// <summary>
  72. /// Compute the number of bytes that would be needed to encode an
  73. /// int64 field, including the tag.
  74. /// </summary>
  75. public static int ComputeInt64Size(int fieldNumber, long value)
  76. {
  77. return ComputeTagSize(fieldNumber) + ComputeRawVarint64Size((ulong) value);
  78. }
  79. /// <summary>
  80. /// Compute the number of bytes that would be needed to encode an
  81. /// int32 field, including the tag.
  82. /// </summary>
  83. public static int ComputeInt32Size(int fieldNumber, int value)
  84. {
  85. if (value >= 0)
  86. {
  87. return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size((uint) value);
  88. }
  89. else
  90. {
  91. // Must sign-extend.
  92. return ComputeTagSize(fieldNumber) + 10;
  93. }
  94. }
  95. /// <summary>
  96. /// Compute the number of bytes that would be needed to encode a
  97. /// fixed64 field, including the tag.
  98. /// </summary>
  99. public static int ComputeFixed64Size(int fieldNumber, ulong value)
  100. {
  101. return ComputeTagSize(fieldNumber) + LittleEndian64Size;
  102. }
  103. /// <summary>
  104. /// Compute the number of bytes that would be needed to encode a
  105. /// fixed32 field, including the tag.
  106. /// </summary>
  107. public static int ComputeFixed32Size(int fieldNumber, uint value)
  108. {
  109. return ComputeTagSize(fieldNumber) + LittleEndian32Size;
  110. }
  111. /// <summary>
  112. /// Compute the number of bytes that would be needed to encode a
  113. /// bool field, including the tag.
  114. /// </summary>
  115. public static int ComputeBoolSize(int fieldNumber, bool value)
  116. {
  117. return ComputeTagSize(fieldNumber) + 1;
  118. }
  119. /// <summary>
  120. /// Compute the number of bytes that would be needed to encode a
  121. /// string field, including the tag.
  122. /// </summary>
  123. public static int ComputeStringSize(int fieldNumber, String value)
  124. {
  125. int byteArraySize = UTF8.GetByteCount(value);
  126. return ComputeTagSize(fieldNumber) +
  127. ComputeRawVarint32Size((uint) byteArraySize) +
  128. byteArraySize;
  129. }
  130. /// <summary>
  131. /// Compute the number of bytes that would be needed to encode a
  132. /// group field, including the tag.
  133. /// </summary>
  134. public static int ComputeGroupSize(int fieldNumber, IMessage value)
  135. {
  136. return ComputeTagSize(fieldNumber)*2 + value.CalculateSize();
  137. }
  138. /// <summary>
  139. /// Compute the number of bytes that would be needed to encode a
  140. /// group field represented by an UnknownFieldSet, including the tag.
  141. /// </summary>
  142. [Obsolete]
  143. public static int ComputeUnknownGroupSize(int fieldNumber,
  144. IMessage value)
  145. {
  146. return ComputeTagSize(fieldNumber)*2 + value.CalculateSize();
  147. }
  148. /// <summary>
  149. /// Compute the number of bytes that would be needed to encode an
  150. /// embedded message field, including the tag.
  151. /// </summary>
  152. public static int ComputeMessageSize(int fieldNumber, IMessage value)
  153. {
  154. int size = value.CalculateSize();
  155. return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size((uint) size) + size;
  156. }
  157. /// <summary>
  158. /// Compute the number of bytes that would be needed to encode a
  159. /// bytes field, including the tag.
  160. /// </summary>
  161. public static int ComputeBytesSize(int fieldNumber, ByteString value)
  162. {
  163. return ComputeTagSize(fieldNumber) +
  164. ComputeRawVarint32Size((uint) value.Length) +
  165. value.Length;
  166. }
  167. /// <summary>
  168. /// Compute the number of bytes that would be needed to encode a
  169. /// uint32 field, including the tag.
  170. /// </summary>
  171. public static int ComputeUInt32Size(int fieldNumber, uint value)
  172. {
  173. return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size(value);
  174. }
  175. /// <summary>
  176. /// Compute the number of bytes that would be needed to encode a
  177. /// enum field, including the tag. The caller is responsible for
  178. /// converting the enum value to its numeric value.
  179. /// </summary>
  180. public static int ComputeEnumSize(int fieldNumber, int value)
  181. {
  182. return ComputeTagSize(fieldNumber) + ComputeEnumSizeNoTag(value);
  183. }
  184. /// <summary>
  185. /// Compute the number of bytes that would be needed to encode an
  186. /// sfixed32 field, including the tag.
  187. /// </summary>
  188. public static int ComputeSFixed32Size(int fieldNumber, int value)
  189. {
  190. return ComputeTagSize(fieldNumber) + LittleEndian32Size;
  191. }
  192. /// <summary>
  193. /// Compute the number of bytes that would be needed to encode an
  194. /// sfixed64 field, including the tag.
  195. /// </summary>
  196. public static int ComputeSFixed64Size(int fieldNumber, long value)
  197. {
  198. return ComputeTagSize(fieldNumber) + LittleEndian64Size;
  199. }
  200. /// <summary>
  201. /// Compute the number of bytes that would be needed to encode an
  202. /// sint32 field, including the tag.
  203. /// </summary>
  204. public static int ComputeSInt32Size(int fieldNumber, int value)
  205. {
  206. return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size(EncodeZigZag32(value));
  207. }
  208. /// <summary>
  209. /// Compute the number of bytes that would be needed to encode an
  210. /// sint64 field, including the tag.
  211. /// </summary>
  212. public static int ComputeSInt64Size(int fieldNumber, long value)
  213. {
  214. return ComputeTagSize(fieldNumber) + ComputeRawVarint64Size(EncodeZigZag64(value));
  215. }
  216. /// <summary>
  217. /// Compute the number of bytes that would be needed to encode a
  218. /// double field, including the tag.
  219. /// </summary>
  220. public static int ComputeDoubleSizeNoTag(double value)
  221. {
  222. return LittleEndian64Size;
  223. }
  224. /// <summary>
  225. /// Compute the number of bytes that would be needed to encode a
  226. /// float field, including the tag.
  227. /// </summary>
  228. public static int ComputeFloatSizeNoTag(float value)
  229. {
  230. return LittleEndian32Size;
  231. }
  232. /// <summary>
  233. /// Compute the number of bytes that would be needed to encode a
  234. /// uint64 field, including the tag.
  235. /// </summary>
  236. public static int ComputeUInt64SizeNoTag(ulong value)
  237. {
  238. return ComputeRawVarint64Size(value);
  239. }
  240. /// <summary>
  241. /// Compute the number of bytes that would be needed to encode an
  242. /// int64 field, including the tag.
  243. /// </summary>
  244. public static int ComputeInt64SizeNoTag(long value)
  245. {
  246. return ComputeRawVarint64Size((ulong) value);
  247. }
  248. /// <summary>
  249. /// Compute the number of bytes that would be needed to encode an
  250. /// int32 field, including the tag.
  251. /// </summary>
  252. public static int ComputeInt32SizeNoTag(int value)
  253. {
  254. if (value >= 0)
  255. {
  256. return ComputeRawVarint32Size((uint) value);
  257. }
  258. else
  259. {
  260. // Must sign-extend.
  261. return 10;
  262. }
  263. }
  264. /// <summary>
  265. /// Compute the number of bytes that would be needed to encode a
  266. /// fixed64 field, including the tag.
  267. /// </summary>
  268. public static int ComputeFixed64SizeNoTag(ulong value)
  269. {
  270. return LittleEndian64Size;
  271. }
  272. /// <summary>
  273. /// Compute the number of bytes that would be needed to encode a
  274. /// fixed32 field, including the tag.
  275. /// </summary>
  276. public static int ComputeFixed32SizeNoTag(uint value)
  277. {
  278. return LittleEndian32Size;
  279. }
  280. /// <summary>
  281. /// Compute the number of bytes that would be needed to encode a
  282. /// bool field, including the tag.
  283. /// </summary>
  284. public static int ComputeBoolSizeNoTag(bool value)
  285. {
  286. return 1;
  287. }
  288. /// <summary>
  289. /// Compute the number of bytes that would be needed to encode a
  290. /// string field, including the tag.
  291. /// </summary>
  292. public static int ComputeStringSizeNoTag(String value)
  293. {
  294. int byteArraySize = UTF8.GetByteCount(value);
  295. return ComputeRawVarint32Size((uint) byteArraySize) +
  296. byteArraySize;
  297. }
  298. /// <summary>
  299. /// Compute the number of bytes that would be needed to encode a
  300. /// group field, including the tag.
  301. /// </summary>
  302. public static int ComputeGroupSizeNoTag(IMessage value)
  303. {
  304. return value.CalculateSize();
  305. }
  306. /// <summary>
  307. /// Compute the number of bytes that would be needed to encode an
  308. /// embedded message field, including the tag.
  309. /// </summary>
  310. public static int ComputeMessageSizeNoTag(IMessage value)
  311. {
  312. int size = value.CalculateSize();
  313. return ComputeRawVarint32Size((uint) size) + size;
  314. }
  315. /// <summary>
  316. /// Compute the number of bytes that would be needed to encode a
  317. /// bytes field, including the tag.
  318. /// </summary>
  319. public static int ComputeBytesSizeNoTag(ByteString value)
  320. {
  321. return ComputeRawVarint32Size((uint) value.Length) +
  322. value.Length;
  323. }
  324. /// <summary>
  325. /// Compute the number of bytes that would be needed to encode a
  326. /// uint32 field, including the tag.
  327. /// </summary>
  328. public static int ComputeUInt32SizeNoTag(uint value)
  329. {
  330. return ComputeRawVarint32Size(value);
  331. }
  332. /// <summary>
  333. /// Compute the number of bytes that would be needed to encode a
  334. /// enum field, including the tag. The caller is responsible for
  335. /// converting the enum value to its numeric value.
  336. /// </summary>
  337. public static int ComputeEnumSizeNoTag(int value)
  338. {
  339. // Currently just a pass-through, but it's nice to separate it logically.
  340. return ComputeInt32SizeNoTag(value);
  341. }
  342. /// <summary>
  343. /// Compute the number of bytes that would be needed to encode an
  344. /// sfixed32 field, including the tag.
  345. /// </summary>
  346. public static int ComputeSFixed32SizeNoTag(int value)
  347. {
  348. return LittleEndian32Size;
  349. }
  350. /// <summary>
  351. /// Compute the number of bytes that would be needed to encode an
  352. /// sfixed64 field, including the tag.
  353. /// </summary>
  354. public static int ComputeSFixed64SizeNoTag(long value)
  355. {
  356. return LittleEndian64Size;
  357. }
  358. /// <summary>
  359. /// Compute the number of bytes that would be needed to encode an
  360. /// sint32 field, including the tag.
  361. /// </summary>
  362. public static int ComputeSInt32SizeNoTag(int value)
  363. {
  364. return ComputeRawVarint32Size(EncodeZigZag32(value));
  365. }
  366. /// <summary>
  367. /// Compute the number of bytes that would be needed to encode an
  368. /// sint64 field, including the tag.
  369. /// </summary>
  370. public static int ComputeSInt64SizeNoTag(long value)
  371. {
  372. return ComputeRawVarint64Size(EncodeZigZag64(value));
  373. }
  374. /// <summary>
  375. /// Compute the number of bytes that would be needed to encode a varint.
  376. /// </summary>
  377. public static int ComputeRawVarint32Size(uint value)
  378. {
  379. // TODO(jonskeet): Look at optimizing this to just hard-coded comparisons.
  380. if ((value & (0xffffffff << 7)) == 0)
  381. {
  382. return 1;
  383. }
  384. if ((value & (0xffffffff << 14)) == 0)
  385. {
  386. return 2;
  387. }
  388. if ((value & (0xffffffff << 21)) == 0)
  389. {
  390. return 3;
  391. }
  392. if ((value & (0xffffffff << 28)) == 0)
  393. {
  394. return 4;
  395. }
  396. return 5;
  397. }
  398. /// <summary>
  399. /// Compute the number of bytes that would be needed to encode a varint.
  400. /// </summary>
  401. public static int ComputeRawVarint64Size(ulong value)
  402. {
  403. // TODO(jonskeet): Look at optimizing this to just hard-coded comparisons.
  404. if ((value & (0xffffffffffffffffL << 7)) == 0)
  405. {
  406. return 1;
  407. }
  408. if ((value & (0xffffffffffffffffL << 14)) == 0)
  409. {
  410. return 2;
  411. }
  412. if ((value & (0xffffffffffffffffL << 21)) == 0)
  413. {
  414. return 3;
  415. }
  416. if ((value & (0xffffffffffffffffL << 28)) == 0)
  417. {
  418. return 4;
  419. }
  420. if ((value & (0xffffffffffffffffL << 35)) == 0)
  421. {
  422. return 5;
  423. }
  424. if ((value & (0xffffffffffffffffL << 42)) == 0)
  425. {
  426. return 6;
  427. }
  428. if ((value & (0xffffffffffffffffL << 49)) == 0)
  429. {
  430. return 7;
  431. }
  432. if ((value & (0xffffffffffffffffL << 56)) == 0)
  433. {
  434. return 8;
  435. }
  436. if ((value & (0xffffffffffffffffL << 63)) == 0)
  437. {
  438. return 9;
  439. }
  440. return 10;
  441. }
  442. /// <summary>
  443. /// Compute the number of bytes that would be needed to encode a tag.
  444. /// </summary>
  445. public static int ComputeTagSize(int fieldNumber)
  446. {
  447. return ComputeRawVarint32Size(WireFormat.MakeTag(fieldNumber, 0));
  448. }
  449. }
  450. }