GPBCodedOutputStream.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #import <Foundation/Foundation.h>
  31. #import "GPBTypes.h"
  32. #import "GPBWireFormat.h"
  33. @class GPBBoolArray;
  34. @class GPBDoubleArray;
  35. @class GPBEnumArray;
  36. @class GPBFloatArray;
  37. @class GPBMessage;
  38. @class GPBInt32Array;
  39. @class GPBInt64Array;
  40. @class GPBUInt32Array;
  41. @class GPBUInt64Array;
  42. @class GPBUnknownFieldSet;
  43. @interface GPBCodedOutputStream : NSObject
  44. // Creates a new stream to write into data. Data must be sized to fit or it
  45. // will error when it runs out of space.
  46. + (instancetype)streamWithData:(NSMutableData *)data;
  47. + (instancetype)streamWithOutputStream:(NSOutputStream *)output;
  48. + (instancetype)streamWithOutputStream:(NSOutputStream *)output
  49. bufferSize:(size_t)bufferSize;
  50. - (instancetype)initWithOutputStream:(NSOutputStream *)output;
  51. - (instancetype)initWithData:(NSMutableData *)data;
  52. - (instancetype)initWithOutputStream:(NSOutputStream *)output
  53. bufferSize:(size_t)bufferSize;
  54. - (instancetype)initWithOutputStream:(NSOutputStream *)output
  55. data:(NSMutableData *)data;
  56. - (void)flush;
  57. - (void)writeRawByte:(uint8_t)value;
  58. - (void)writeTag:(uint32_t)fieldNumber format:(GPBWireFormat)format;
  59. - (void)writeRawLittleEndian32:(int32_t)value;
  60. - (void)writeRawLittleEndian64:(int64_t)value;
  61. - (void)writeRawVarint32:(int32_t)value;
  62. - (void)writeRawVarint64:(int64_t)value;
  63. // Note that this will truncate 64 bit values to 32.
  64. - (void)writeRawVarintSizeTAs32:(size_t)value;
  65. - (void)writeRawData:(NSData *)data;
  66. - (void)writeRawPtr:(const void *)data
  67. offset:(size_t)offset
  68. length:(size_t)length;
  69. //%PDDM-EXPAND _WRITE_DECLS()
  70. // This block of code is generated, do not edit it directly.
  71. - (void)writeDouble:(int32_t)fieldNumber value:(double)value;
  72. - (void)writeDoubles:(int32_t)fieldNumber
  73. values:(GPBDoubleArray *)values
  74. tag:(uint32_t)tag;
  75. - (void)writeDoubleNoTag:(double)value;
  76. - (void)writeFloat:(int32_t)fieldNumber value:(float)value;
  77. - (void)writeFloats:(int32_t)fieldNumber
  78. values:(GPBFloatArray *)values
  79. tag:(uint32_t)tag;
  80. - (void)writeFloatNoTag:(float)value;
  81. - (void)writeUInt64:(int32_t)fieldNumber value:(uint64_t)value;
  82. - (void)writeUInt64s:(int32_t)fieldNumber
  83. values:(GPBUInt64Array *)values
  84. tag:(uint32_t)tag;
  85. - (void)writeUInt64NoTag:(uint64_t)value;
  86. - (void)writeInt64:(int32_t)fieldNumber value:(int64_t)value;
  87. - (void)writeInt64s:(int32_t)fieldNumber
  88. values:(GPBInt64Array *)values
  89. tag:(uint32_t)tag;
  90. - (void)writeInt64NoTag:(int64_t)value;
  91. - (void)writeInt32:(int32_t)fieldNumber value:(int32_t)value;
  92. - (void)writeInt32s:(int32_t)fieldNumber
  93. values:(GPBInt32Array *)values
  94. tag:(uint32_t)tag;
  95. - (void)writeInt32NoTag:(int32_t)value;
  96. - (void)writeUInt32:(int32_t)fieldNumber value:(uint32_t)value;
  97. - (void)writeUInt32s:(int32_t)fieldNumber
  98. values:(GPBUInt32Array *)values
  99. tag:(uint32_t)tag;
  100. - (void)writeUInt32NoTag:(uint32_t)value;
  101. - (void)writeFixed64:(int32_t)fieldNumber value:(uint64_t)value;
  102. - (void)writeFixed64s:(int32_t)fieldNumber
  103. values:(GPBUInt64Array *)values
  104. tag:(uint32_t)tag;
  105. - (void)writeFixed64NoTag:(uint64_t)value;
  106. - (void)writeFixed32:(int32_t)fieldNumber value:(uint32_t)value;
  107. - (void)writeFixed32s:(int32_t)fieldNumber
  108. values:(GPBUInt32Array *)values
  109. tag:(uint32_t)tag;
  110. - (void)writeFixed32NoTag:(uint32_t)value;
  111. - (void)writeSInt32:(int32_t)fieldNumber value:(int32_t)value;
  112. - (void)writeSInt32s:(int32_t)fieldNumber
  113. values:(GPBInt32Array *)values
  114. tag:(uint32_t)tag;
  115. - (void)writeSInt32NoTag:(int32_t)value;
  116. - (void)writeSInt64:(int32_t)fieldNumber value:(int64_t)value;
  117. - (void)writeSInt64s:(int32_t)fieldNumber
  118. values:(GPBInt64Array *)values
  119. tag:(uint32_t)tag;
  120. - (void)writeSInt64NoTag:(int64_t)value;
  121. - (void)writeSFixed64:(int32_t)fieldNumber value:(int64_t)value;
  122. - (void)writeSFixed64s:(int32_t)fieldNumber
  123. values:(GPBInt64Array *)values
  124. tag:(uint32_t)tag;
  125. - (void)writeSFixed64NoTag:(int64_t)value;
  126. - (void)writeSFixed32:(int32_t)fieldNumber value:(int32_t)value;
  127. - (void)writeSFixed32s:(int32_t)fieldNumber
  128. values:(GPBInt32Array *)values
  129. tag:(uint32_t)tag;
  130. - (void)writeSFixed32NoTag:(int32_t)value;
  131. - (void)writeBool:(int32_t)fieldNumber value:(BOOL)value;
  132. - (void)writeBools:(int32_t)fieldNumber
  133. values:(GPBBoolArray *)values
  134. tag:(uint32_t)tag;
  135. - (void)writeBoolNoTag:(BOOL)value;
  136. - (void)writeEnum:(int32_t)fieldNumber value:(int32_t)value;
  137. - (void)writeEnums:(int32_t)fieldNumber
  138. values:(GPBEnumArray *)values
  139. tag:(uint32_t)tag;
  140. - (void)writeEnumNoTag:(int32_t)value;
  141. - (void)writeString:(int32_t)fieldNumber value:(NSString *)value;
  142. - (void)writeStrings:(int32_t)fieldNumber values:(NSArray *)values;
  143. - (void)writeStringNoTag:(NSString *)value;
  144. - (void)writeMessage:(int32_t)fieldNumber value:(GPBMessage *)value;
  145. - (void)writeMessages:(int32_t)fieldNumber values:(NSArray *)values;
  146. - (void)writeMessageNoTag:(GPBMessage *)value;
  147. - (void)writeData:(int32_t)fieldNumber value:(NSData *)value;
  148. - (void)writeDatas:(int32_t)fieldNumber values:(NSArray *)values;
  149. - (void)writeDataNoTag:(NSData *)value;
  150. - (void)writeGroup:(int32_t)fieldNumber
  151. value:(GPBMessage *)value;
  152. - (void)writeGroups:(int32_t)fieldNumber values:(NSArray *)values;
  153. - (void)writeGroupNoTag:(int32_t)fieldNumber
  154. value:(GPBMessage *)value;
  155. - (void)writeUnknownGroup:(int32_t)fieldNumber
  156. value:(GPBUnknownFieldSet *)value;
  157. - (void)writeUnknownGroups:(int32_t)fieldNumber values:(NSArray *)values;
  158. - (void)writeUnknownGroupNoTag:(int32_t)fieldNumber
  159. value:(GPBUnknownFieldSet *)value;
  160. //%PDDM-EXPAND-END _WRITE_DECLS()
  161. // Write a MessageSet extension field to the stream. For historical reasons,
  162. // the wire format differs from normal fields.
  163. - (void)writeMessageSetExtension:(int32_t)fieldNumber value:(GPBMessage *)value;
  164. // Write an unparsed MessageSet extension field to the stream. For
  165. // historical reasons, the wire format differs from normal fields.
  166. - (void)writeRawMessageSetExtension:(int32_t)fieldNumber value:(NSData *)value;
  167. @end
  168. CF_EXTERN_C_BEGIN
  169. size_t GPBComputeDoubleSize(int32_t fieldNumber, double value)
  170. __attribute__((const));
  171. size_t GPBComputeFloatSize(int32_t fieldNumber, float value)
  172. __attribute__((const));
  173. size_t GPBComputeUInt64Size(int32_t fieldNumber, uint64_t value)
  174. __attribute__((const));
  175. size_t GPBComputeInt64Size(int32_t fieldNumber, int64_t value)
  176. __attribute__((const));
  177. size_t GPBComputeInt32Size(int32_t fieldNumber, int32_t value)
  178. __attribute__((const));
  179. size_t GPBComputeFixed64Size(int32_t fieldNumber, uint64_t value)
  180. __attribute__((const));
  181. size_t GPBComputeFixed32Size(int32_t fieldNumber, uint32_t value)
  182. __attribute__((const));
  183. size_t GPBComputeBoolSize(int32_t fieldNumber, BOOL value)
  184. __attribute__((const));
  185. size_t GPBComputeStringSize(int32_t fieldNumber, NSString *value)
  186. __attribute__((const));
  187. size_t GPBComputeGroupSize(int32_t fieldNumber, GPBMessage *value)
  188. __attribute__((const));
  189. size_t GPBComputeUnknownGroupSize(int32_t fieldNumber,
  190. GPBUnknownFieldSet *value)
  191. __attribute__((const));
  192. size_t GPBComputeMessageSize(int32_t fieldNumber, GPBMessage *value)
  193. __attribute__((const));
  194. size_t GPBComputeDataSize(int32_t fieldNumber, NSData *value)
  195. __attribute__((const));
  196. size_t GPBComputeUInt32Size(int32_t fieldNumber, uint32_t value)
  197. __attribute__((const));
  198. size_t GPBComputeSFixed32Size(int32_t fieldNumber, int32_t value)
  199. __attribute__((const));
  200. size_t GPBComputeSFixed64Size(int32_t fieldNumber, int64_t value)
  201. __attribute__((const));
  202. size_t GPBComputeSInt32Size(int32_t fieldNumber, int32_t value)
  203. __attribute__((const));
  204. size_t GPBComputeSInt64Size(int32_t fieldNumber, int64_t value)
  205. __attribute__((const));
  206. size_t GPBComputeTagSize(int32_t fieldNumber) __attribute__((const));
  207. size_t GPBComputeWireFormatTagSize(int field_number, GPBType type)
  208. __attribute__((const));
  209. size_t GPBComputeDoubleSizeNoTag(double value) __attribute__((const));
  210. size_t GPBComputeFloatSizeNoTag(float value) __attribute__((const));
  211. size_t GPBComputeUInt64SizeNoTag(uint64_t value) __attribute__((const));
  212. size_t GPBComputeInt64SizeNoTag(int64_t value) __attribute__((const));
  213. size_t GPBComputeInt32SizeNoTag(int32_t value) __attribute__((const));
  214. size_t GPBComputeFixed64SizeNoTag(uint64_t value) __attribute__((const));
  215. size_t GPBComputeFixed32SizeNoTag(uint32_t value) __attribute__((const));
  216. size_t GPBComputeBoolSizeNoTag(BOOL value) __attribute__((const));
  217. size_t GPBComputeStringSizeNoTag(NSString *value) __attribute__((const));
  218. size_t GPBComputeGroupSizeNoTag(GPBMessage *value) __attribute__((const));
  219. size_t GPBComputeUnknownGroupSizeNoTag(GPBUnknownFieldSet *value)
  220. __attribute__((const));
  221. size_t GPBComputeMessageSizeNoTag(GPBMessage *value) __attribute__((const));
  222. size_t GPBComputeDataSizeNoTag(NSData *value) __attribute__((const));
  223. size_t GPBComputeUInt32SizeNoTag(int32_t value) __attribute__((const));
  224. size_t GPBComputeEnumSizeNoTag(int32_t value) __attribute__((const));
  225. size_t GPBComputeSFixed32SizeNoTag(int32_t value) __attribute__((const));
  226. size_t GPBComputeSFixed64SizeNoTag(int64_t value) __attribute__((const));
  227. size_t GPBComputeSInt32SizeNoTag(int32_t value) __attribute__((const));
  228. size_t GPBComputeSInt64SizeNoTag(int64_t value) __attribute__((const));
  229. // Note that this will calculate the size of 64 bit values truncated to 32.
  230. size_t GPBComputeSizeTSizeAsInt32NoTag(size_t value) __attribute__((const));
  231. size_t GPBComputeRawVarint32Size(int32_t value) __attribute__((const));
  232. size_t GPBComputeRawVarint64Size(int64_t value) __attribute__((const));
  233. // Note that this will calculate the size of 64 bit values truncated to 32.
  234. size_t GPBComputeRawVarint32SizeForInteger(NSInteger value)
  235. __attribute__((const));
  236. // Compute the number of bytes that would be needed to encode a
  237. // MessageSet extension to the stream. For historical reasons,
  238. // the wire format differs from normal fields.
  239. size_t GPBComputeMessageSetExtensionSize(int32_t fieldNumber, GPBMessage *value)
  240. __attribute__((const));
  241. // Compute the number of bytes that would be needed to encode an
  242. // unparsed MessageSet extension field to the stream. For
  243. // historical reasons, the wire format differs from normal fields.
  244. size_t GPBComputeRawMessageSetExtensionSize(int32_t fieldNumber, NSData *value)
  245. __attribute__((const));
  246. size_t GPBComputeEnumSize(int32_t fieldNumber, int32_t value)
  247. __attribute__((const));
  248. CF_EXTERN_C_END
  249. // Write methods for types that can be in packed arrays.
  250. //%PDDM-DEFINE _WRITE_PACKABLE_DECLS(NAME, ARRAY_TYPE, TYPE)
  251. //%- (void)write##NAME:(int32_t)fieldNumber value:(TYPE)value;
  252. //%- (void)write##NAME##s:(int32_t)fieldNumber
  253. //% NAME$S values:(GPB##ARRAY_TYPE##Array *)values
  254. //% NAME$S tag:(uint32_t)tag;
  255. //%- (void)write##NAME##NoTag:(TYPE)value;
  256. //%
  257. // Write methods for types that aren't in packed arrays.
  258. //%PDDM-DEFINE _WRITE_UNPACKABLE_DECLS(NAME, TYPE)
  259. //%- (void)write##NAME:(int32_t)fieldNumber value:(TYPE)value;
  260. //%- (void)write##NAME##s:(int32_t)fieldNumber values:(NSArray *)values;
  261. //%- (void)write##NAME##NoTag:(TYPE)value;
  262. //%
  263. // Special write methods for Groups.
  264. //%PDDM-DEFINE _WRITE_GROUP_DECLS(NAME, TYPE)
  265. //%- (void)write##NAME:(int32_t)fieldNumber
  266. //% NAME$S value:(TYPE)value;
  267. //%- (void)write##NAME##s:(int32_t)fieldNumber values:(NSArray *)values;
  268. //%- (void)write##NAME##NoTag:(int32_t)fieldNumber
  269. //% NAME$S value:(TYPE)value;
  270. //%
  271. // One macro to hide it all up above.
  272. //%PDDM-DEFINE _WRITE_DECLS()
  273. //%_WRITE_PACKABLE_DECLS(Double, Double, double)
  274. //%_WRITE_PACKABLE_DECLS(Float, Float, float)
  275. //%_WRITE_PACKABLE_DECLS(UInt64, UInt64, uint64_t)
  276. //%_WRITE_PACKABLE_DECLS(Int64, Int64, int64_t)
  277. //%_WRITE_PACKABLE_DECLS(Int32, Int32, int32_t)
  278. //%_WRITE_PACKABLE_DECLS(UInt32, UInt32, uint32_t)
  279. //%_WRITE_PACKABLE_DECLS(Fixed64, UInt64, uint64_t)
  280. //%_WRITE_PACKABLE_DECLS(Fixed32, UInt32, uint32_t)
  281. //%_WRITE_PACKABLE_DECLS(SInt32, Int32, int32_t)
  282. //%_WRITE_PACKABLE_DECLS(SInt64, Int64, int64_t)
  283. //%_WRITE_PACKABLE_DECLS(SFixed64, Int64, int64_t)
  284. //%_WRITE_PACKABLE_DECLS(SFixed32, Int32, int32_t)
  285. //%_WRITE_PACKABLE_DECLS(Bool, Bool, BOOL)
  286. //%_WRITE_PACKABLE_DECLS(Enum, Enum, int32_t)
  287. //%_WRITE_UNPACKABLE_DECLS(String, NSString *)
  288. //%_WRITE_UNPACKABLE_DECLS(Message, GPBMessage *)
  289. //%_WRITE_UNPACKABLE_DECLS(Data, NSData *)
  290. //%_WRITE_GROUP_DECLS(Group, GPBMessage *)
  291. //%_WRITE_GROUP_DECLS(UnknownGroup, GPBUnknownFieldSet *)