Utilities.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // Protocol Buffers for Objective C
  2. //
  3. // Copyright 2010 Booyah Inc.
  4. // Copyright 2008 Cyrus Najmabadi
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. #import "Message.h"
  18. #ifdef __cplusplus
  19. extern "C"{
  20. #endif
  21. SInt64 convertFloat64ToInt64(Float64 f);
  22. SInt32 convertFloat32ToInt32(Float32 f);
  23. Float64 convertInt64ToFloat64(SInt64 f);
  24. Float32 convertInt32ToFloat32(SInt32 f);
  25. UInt64 convertInt64ToUInt64(SInt64 i);
  26. SInt64 convertUInt64ToInt64(UInt64 u);
  27. UInt32 convertInt32ToUInt32(SInt32 i);
  28. SInt32 convertUInt32ToInt32(UInt32 u);
  29. SInt32 logicalRightShift32(SInt32 value, SInt32 spaces);
  30. SInt64 logicalRightShift64(SInt64 value, SInt32 spaces);
  31. /**
  32. * Decode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers
  33. * into values that can be efficiently encoded with varint. (Otherwise,
  34. * negative values must be sign-extended to 64 bits to be varint encoded,
  35. * thus always taking 10 bytes on the wire.)
  36. *
  37. * @param n An unsigned 32-bit integer, stored in a signed int.
  38. * @return A signed 32-bit integer.
  39. */
  40. SInt32 decodeZigZag32(SInt32 n);
  41. /**
  42. * Decode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers
  43. * into values that can be efficiently encoded with varint. (Otherwise,
  44. * negative values must be sign-extended to 64 bits to be varint encoded,
  45. * thus always taking 10 bytes on the wire.)
  46. *
  47. * @param n An unsigned 64-bit integer, stored in a signed int.
  48. * @return A signed 64-bit integer.
  49. */
  50. SInt64 decodeZigZag64(SInt64 n);
  51. /**
  52. * Encode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers
  53. * into values that can be efficiently encoded with varint. (Otherwise,
  54. * negative values must be sign-extended to 64 bits to be varint encoded,
  55. * thus always taking 10 bytes on the wire.)
  56. *
  57. * @param n A signed 32-bit integer.
  58. * @return An unsigned 32-bit integer, stored in a signed int.
  59. */
  60. SInt32 encodeZigZag32(SInt32 n);
  61. /**
  62. * Encode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers
  63. * into values that can be efficiently encoded with varint. (Otherwise,
  64. * negative values must be sign-extended to 64 bits to be varint encoded,
  65. * thus always taking 10 bytes on the wire.)
  66. *
  67. * @param n A signed 64-bit integer.
  68. * @return An unsigned 64-bit integer, stored in a signed int.
  69. */
  70. SInt64 encodeZigZag64(SInt64 n);
  71. /**
  72. * Compute the number of bytes that would be needed to encode a
  73. * {@code double} field, including tag.
  74. */
  75. SInt32 computeDoubleSize(SInt32 fieldNumber, Float64 value);
  76. /**
  77. * Compute the number of bytes that would be needed to encode a
  78. * {@code float} field, including tag.
  79. */
  80. SInt32 computeFloatSize(SInt32 fieldNumber, Float32 value);
  81. /**
  82. * Compute the number of bytes that would be needed to encode a
  83. * {@code uint64} field, including tag.
  84. */
  85. SInt32 computeUInt64Size(SInt32 fieldNumber, SInt64 value);
  86. /**
  87. * Compute the number of bytes that would be needed to encode an
  88. * {@code int64} field, including tag.
  89. */
  90. SInt32 computeInt64Size(SInt32 fieldNumber, SInt64 value);
  91. /**
  92. * Compute the number of bytes that would be needed to encode an
  93. * {@code int32} field, including tag.
  94. */
  95. SInt32 computeInt32Size(SInt32 fieldNumber, SInt32 value);
  96. /**
  97. * Compute the number of bytes that would be needed to encode a
  98. * {@code fixed64} field, including tag.
  99. */
  100. SInt32 computeFixed64Size(SInt32 fieldNumber, SInt64 value);
  101. /**
  102. * Compute the number of bytes that would be needed to encode a
  103. * {@code fixed32} field, including tag.
  104. */
  105. SInt32 computeFixed32Size(SInt32 fieldNumber, SInt32 value);
  106. /**
  107. * Compute the number of bytes that would be needed to encode a
  108. * {@code bool} field, including tag.
  109. */
  110. SInt32 computeBoolSize(SInt32 fieldNumber, BOOL value);
  111. /**
  112. * Compute the number of bytes that would be needed to encode a
  113. * {@code string} field, including tag.
  114. */
  115. SInt32 computeStringSize(SInt32 fieldNumber, const NSString* value);
  116. /**
  117. * Compute the number of bytes that would be needed to encode a
  118. * {@code group} field, including tag.
  119. */
  120. SInt32 computeGroupSize(SInt32 fieldNumber, const id<PBMessage> value);
  121. /**
  122. * Compute the number of bytes that would be needed to encode a
  123. * {@code group} field represented by an {@code PBUnknownFieldSet}, including
  124. * tag.
  125. */
  126. SInt32 computeUnknownGroupSize(SInt32 fieldNumber, const PBUnknownFieldSet* value);
  127. /**
  128. * Compute the number of bytes that would be needed to encode an
  129. * embedded message field, including tag.
  130. */
  131. SInt32 computeMessageSize(SInt32 fieldNumber, const id<PBMessage> value);
  132. /**
  133. * Compute the number of bytes that would be needed to encode a
  134. * {@code bytes} field, including tag.
  135. */
  136. SInt32 computeDataSize(SInt32 fieldNumber, const NSData* value);
  137. /**
  138. * Compute the number of bytes that would be needed to encode a
  139. * {@code uint32} field, including tag.
  140. */
  141. SInt32 computeUInt32Size(SInt32 fieldNumber, SInt32 value);
  142. /**
  143. * Compute the number of bytes that would be needed to encode an
  144. * {@code sfixed32} field, including tag.
  145. */
  146. SInt32 computeSFixed32Size(SInt32 fieldNumber, SInt32 value);
  147. /**
  148. * Compute the number of bytes that would be needed to encode an
  149. * {@code sfixed64} field, including tag.
  150. */
  151. SInt32 computeSFixed64Size(SInt32 fieldNumber, SInt64 value);
  152. /**
  153. * Compute the number of bytes that would be needed to encode an
  154. * {@code sint32} field, including tag.
  155. */
  156. SInt32 computeSInt32Size(SInt32 fieldNumber, SInt32 value);
  157. /**
  158. * Compute the number of bytes that would be needed to encode an
  159. * {@code sint64} field, including tag.
  160. */
  161. SInt32 computeSInt64Size(SInt32 fieldNumber, SInt64 value);
  162. /** Compute the number of bytes that would be needed to encode a tag. */
  163. SInt32 computeTagSize(SInt32 fieldNumber);
  164. /**
  165. * Compute the number of bytes that would be needed to encode a
  166. * {@code double} field, including tag.
  167. */
  168. SInt32 computeDoubleSizeNoTag(Float64 value);
  169. /**
  170. * Compute the number of bytes that would be needed to encode a
  171. * {@code float} field, including tag.
  172. */
  173. SInt32 computeFloatSizeNoTag(Float32 value);
  174. /**
  175. * Compute the number of bytes that would be needed to encode a
  176. * {@code uint64} field, including tag.
  177. */
  178. SInt32 computeUInt64SizeNoTag(SInt64 value);
  179. /**
  180. * Compute the number of bytes that would be needed to encode an
  181. * {@code int64} field, including tag.
  182. */
  183. SInt32 computeInt64SizeNoTag(SInt64 value);
  184. /**
  185. * Compute the number of bytes that would be needed to encode an
  186. * {@code int32} field, including tag.
  187. */
  188. SInt32 computeInt32SizeNoTag(SInt32 value);
  189. /**
  190. * Compute the number of bytes that would be needed to encode a
  191. * {@code fixed64} field, including tag.
  192. */
  193. SInt32 computeFixed64SizeNoTag(SInt64 value);
  194. /**
  195. * Compute the number of bytes that would be needed to encode a
  196. * {@code fixed32} field, including tag.
  197. */
  198. SInt32 computeFixed32SizeNoTag(SInt32 value);
  199. /**
  200. * Compute the number of bytes that would be needed to encode a
  201. * {@code bool} field, including tag.
  202. */
  203. SInt32 computeBoolSizeNoTag(BOOL value);
  204. /**
  205. * Compute the number of bytes that would be needed to encode a
  206. * {@code string} field, including tag.
  207. */
  208. SInt32 computeStringSizeNoTag(const NSString* value);
  209. /**
  210. * Compute the number of bytes that would be needed to encode a
  211. * {@code group} field, including tag.
  212. */
  213. SInt32 computeGroupSizeNoTag(const id<PBMessage> value);
  214. /**
  215. * Compute the number of bytes that would be needed to encode a
  216. * {@code group} field represented by an {@code PBUnknownFieldSet}, including
  217. * tag.
  218. */
  219. SInt32 computeUnknownGroupSizeNoTag(const PBUnknownFieldSet* value);
  220. /**
  221. * Compute the number of bytes that would be needed to encode an
  222. * embedded message field, including tag.
  223. */
  224. SInt32 computeMessageSizeNoTag(const id<PBMessage> value);
  225. /**
  226. * Compute the number of bytes that would be needed to encode a
  227. * {@code bytes} field, including tag.
  228. */
  229. SInt32 computeDataSizeNoTag(const NSData* value);
  230. /**
  231. * Compute the number of bytes that would be needed to encode a
  232. * {@code uint32} field, including tag.
  233. */
  234. SInt32 computeUInt32SizeNoTag(SInt32 value);
  235. /**
  236. * Compute the number of bytes that would be needed to encode an
  237. * enum field, including tag. Caller is responsible for converting the
  238. * enum value to its numeric value.
  239. */
  240. SInt32 computeEnumSizeNoTag(SInt32 value);
  241. /**
  242. * Compute the number of bytes that would be needed to encode an
  243. * {@code sfixed32} field, including tag.
  244. */
  245. SInt32 computeSFixed32SizeNoTag(SInt32 value);
  246. /**
  247. * Compute the number of bytes that would be needed to encode an
  248. * {@code sfixed64} field, including tag.
  249. */
  250. SInt32 computeSFixed64SizeNoTag(SInt64 value);
  251. /**
  252. * Compute the number of bytes that would be needed to encode an
  253. * {@code sint32} field, including tag.
  254. */
  255. SInt32 computeSInt32SizeNoTag(SInt32 value);
  256. /**
  257. * Compute the number of bytes that would be needed to encode an
  258. * {@code sint64} field, including tag.
  259. */
  260. SInt32 computeSInt64SizeNoTag(SInt64 value);
  261. /**
  262. * Compute the number of bytes that would be needed to encode a varint.
  263. * {@code value} is treated as unsigned, so it won't be sign-extended if
  264. * negative.
  265. */
  266. SInt32 computeRawVarint32Size(SInt32 value);
  267. /** Compute the number of bytes that would be needed to encode a varint. */
  268. SInt32 computeRawVarint64Size(SInt64 value);
  269. /**
  270. * Compute the number of bytes that would be needed to encode a
  271. * MessageSet extension to the stream. For historical reasons,
  272. * the wire format differs from normal fields.
  273. */
  274. SInt32 computeMessageSetExtensionSize(SInt32 fieldNumber, const id<PBMessage> value);
  275. /**
  276. * Compute the number of bytes that would be needed to encode an
  277. * unparsed MessageSet extension field to the stream. For
  278. * historical reasons, the wire format differs from normal fields.
  279. */
  280. SInt32 computeRawMessageSetExtensionSize(SInt32 fieldNumber, const NSData* value);
  281. /**
  282. * Compute the number of bytes that would be needed to encode an
  283. * enum field, including tag. Caller is responsible for converting the
  284. * enum value to its numeric value.
  285. */
  286. SInt32 computeEnumSize(SInt32 fieldNumber, SInt32 value);
  287. #ifdef __cplusplus
  288. }
  289. #endif