wire_format_lite.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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. // Author: kenton@google.com (Kenton Varda)
  31. // atenasio@google.com (Chris Atenasio) (ZigZag transform)
  32. // wink@google.com (Wink Saville) (refactored from wire_format.h)
  33. // Based on original Protocol Buffers design by
  34. // Sanjay Ghemawat, Jeff Dean, and others.
  35. //
  36. // This header is logically internal, but is made public because it is used
  37. // from protocol-compiler-generated code, which may reside in other components.
  38. #ifndef GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__
  39. #define GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__
  40. #include <string>
  41. #include <google/protobuf/stubs/common.h>
  42. #include <google/protobuf/io/coded_stream.h>
  43. #include <google/protobuf/message_lite.h>
  44. #include <google/protobuf/stubs/port.h>
  45. #include <google/protobuf/repeated_field.h>
  46. // Do UTF-8 validation on string type in Debug build only
  47. #ifndef NDEBUG
  48. #define GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  49. #endif
  50. // Avoid conflict with iOS where <ConditionalMacros.h> #defines TYPE_BOOL.
  51. //
  52. // If some one needs the macro TYPE_BOOL in a file that includes this header, it's
  53. // possible to bring it back using push/pop_macro as follows.
  54. //
  55. // #pragma push_macro("TYPE_BOOL")
  56. // #include this header and/or all headers that need the macro to be undefined.
  57. // #pragma pop_macro("TYPE_BOOL")
  58. #undef TYPE_BOOL
  59. namespace google {
  60. namespace protobuf {
  61. template <typename T> class RepeatedField; // repeated_field.h
  62. }
  63. namespace protobuf {
  64. namespace internal {
  65. class StringPieceField;
  66. // This class is for internal use by the protocol buffer library and by
  67. // protocol-complier-generated message classes. It must not be called
  68. // directly by clients.
  69. //
  70. // This class contains helpers for implementing the binary protocol buffer
  71. // wire format without the need for reflection. Use WireFormat when using
  72. // reflection.
  73. //
  74. // This class is really a namespace that contains only static methods.
  75. class LIBPROTOBUF_EXPORT WireFormatLite {
  76. public:
  77. // -----------------------------------------------------------------
  78. // Helper constants and functions related to the format. These are
  79. // mostly meant for internal and generated code to use.
  80. // The wire format is composed of a sequence of tag/value pairs, each
  81. // of which contains the value of one field (or one element of a repeated
  82. // field). Each tag is encoded as a varint. The lower bits of the tag
  83. // identify its wire type, which specifies the format of the data to follow.
  84. // The rest of the bits contain the field number. Each type of field (as
  85. // declared by FieldDescriptor::Type, in descriptor.h) maps to one of
  86. // these wire types. Immediately following each tag is the field's value,
  87. // encoded in the format specified by the wire type. Because the tag
  88. // identifies the encoding of this data, it is possible to skip
  89. // unrecognized fields for forwards compatibility.
  90. enum WireType {
  91. WIRETYPE_VARINT = 0,
  92. WIRETYPE_FIXED64 = 1,
  93. WIRETYPE_LENGTH_DELIMITED = 2,
  94. WIRETYPE_START_GROUP = 3,
  95. WIRETYPE_END_GROUP = 4,
  96. WIRETYPE_FIXED32 = 5,
  97. };
  98. // Lite alternative to FieldDescriptor::Type. Must be kept in sync.
  99. enum FieldType {
  100. TYPE_DOUBLE = 1,
  101. TYPE_FLOAT = 2,
  102. TYPE_INT64 = 3,
  103. TYPE_UINT64 = 4,
  104. TYPE_INT32 = 5,
  105. TYPE_FIXED64 = 6,
  106. TYPE_FIXED32 = 7,
  107. TYPE_BOOL = 8,
  108. TYPE_STRING = 9,
  109. TYPE_GROUP = 10,
  110. TYPE_MESSAGE = 11,
  111. TYPE_BYTES = 12,
  112. TYPE_UINT32 = 13,
  113. TYPE_ENUM = 14,
  114. TYPE_SFIXED32 = 15,
  115. TYPE_SFIXED64 = 16,
  116. TYPE_SINT32 = 17,
  117. TYPE_SINT64 = 18,
  118. MAX_FIELD_TYPE = 18,
  119. };
  120. // Lite alternative to FieldDescriptor::CppType. Must be kept in sync.
  121. enum CppType {
  122. CPPTYPE_INT32 = 1,
  123. CPPTYPE_INT64 = 2,
  124. CPPTYPE_UINT32 = 3,
  125. CPPTYPE_UINT64 = 4,
  126. CPPTYPE_DOUBLE = 5,
  127. CPPTYPE_FLOAT = 6,
  128. CPPTYPE_BOOL = 7,
  129. CPPTYPE_ENUM = 8,
  130. CPPTYPE_STRING = 9,
  131. CPPTYPE_MESSAGE = 10,
  132. MAX_CPPTYPE = 10,
  133. };
  134. // Helper method to get the CppType for a particular Type.
  135. static CppType FieldTypeToCppType(FieldType type);
  136. // Given a FieldDescriptor::Type return its WireType
  137. static inline WireFormatLite::WireType WireTypeForFieldType(
  138. WireFormatLite::FieldType type) {
  139. return kWireTypeForFieldType[type];
  140. }
  141. // Number of bits in a tag which identify the wire type.
  142. static const int kTagTypeBits = 3;
  143. // Mask for those bits.
  144. static const uint32 kTagTypeMask = (1 << kTagTypeBits) - 1;
  145. // Helper functions for encoding and decoding tags. (Inlined below and in
  146. // _inl.h)
  147. //
  148. // This is different from MakeTag(field->number(), field->type()) in the case
  149. // of packed repeated fields.
  150. static uint32 MakeTag(int field_number, WireType type);
  151. static WireType GetTagWireType(uint32 tag);
  152. static int GetTagFieldNumber(uint32 tag);
  153. // Compute the byte size of a tag. For groups, this includes both the start
  154. // and end tags.
  155. static inline size_t TagSize(int field_number,
  156. WireFormatLite::FieldType type);
  157. // Skips a field value with the given tag. The input should start
  158. // positioned immediately after the tag. Skipped values are simply discarded,
  159. // not recorded anywhere. See WireFormat::SkipField() for a version that
  160. // records to an UnknownFieldSet.
  161. static bool SkipField(io::CodedInputStream* input, uint32 tag);
  162. // Skips a field value with the given tag. The input should start
  163. // positioned immediately after the tag. Skipped values are recorded to a
  164. // CodedOutputStream.
  165. static bool SkipField(io::CodedInputStream* input, uint32 tag,
  166. io::CodedOutputStream* output);
  167. // Reads and ignores a message from the input. Skipped values are simply
  168. // discarded, not recorded anywhere. See WireFormat::SkipMessage() for a
  169. // version that records to an UnknownFieldSet.
  170. static bool SkipMessage(io::CodedInputStream* input);
  171. // Reads and ignores a message from the input. Skipped values are recorded
  172. // to a CodedOutputStream.
  173. static bool SkipMessage(io::CodedInputStream* input,
  174. io::CodedOutputStream* output);
  175. // This macro does the same thing as WireFormatLite::MakeTag(), but the
  176. // result is usable as a compile-time constant, which makes it usable
  177. // as a switch case or a template input. WireFormatLite::MakeTag() is more
  178. // type-safe, though, so prefer it if possible.
  179. #define GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(FIELD_NUMBER, TYPE) \
  180. static_cast<uint32>( \
  181. (static_cast<uint32>(FIELD_NUMBER) << ::google::protobuf::internal::WireFormatLite::kTagTypeBits) \
  182. | (TYPE))
  183. // These are the tags for the old MessageSet format, which was defined as:
  184. // message MessageSet {
  185. // repeated group Item = 1 {
  186. // required int32 type_id = 2;
  187. // required string message = 3;
  188. // }
  189. // }
  190. static const int kMessageSetItemNumber = 1;
  191. static const int kMessageSetTypeIdNumber = 2;
  192. static const int kMessageSetMessageNumber = 3;
  193. static const int kMessageSetItemStartTag =
  194. GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kMessageSetItemNumber,
  195. WireFormatLite::WIRETYPE_START_GROUP);
  196. static const int kMessageSetItemEndTag =
  197. GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kMessageSetItemNumber,
  198. WireFormatLite::WIRETYPE_END_GROUP);
  199. static const int kMessageSetTypeIdTag =
  200. GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kMessageSetTypeIdNumber,
  201. WireFormatLite::WIRETYPE_VARINT);
  202. static const int kMessageSetMessageTag =
  203. GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kMessageSetMessageNumber,
  204. WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
  205. // Byte size of all tags of a MessageSet::Item combined.
  206. static const size_t kMessageSetItemTagsSize;
  207. // Helper functions for converting between floats/doubles and IEEE-754
  208. // uint32s/uint64s so that they can be written. (Assumes your platform
  209. // uses IEEE-754 floats.)
  210. static uint32 EncodeFloat(float value);
  211. static float DecodeFloat(uint32 value);
  212. static uint64 EncodeDouble(double value);
  213. static double DecodeDouble(uint64 value);
  214. // Helper functions for mapping signed integers to unsigned integers in
  215. // such a way that numbers with small magnitudes will encode to smaller
  216. // varints. If you simply static_cast a negative number to an unsigned
  217. // number and varint-encode it, it will always take 10 bytes, defeating
  218. // the purpose of varint. So, for the "sint32" and "sint64" field types,
  219. // we ZigZag-encode the values.
  220. static uint32 ZigZagEncode32(int32 n);
  221. static int32 ZigZagDecode32(uint32 n);
  222. static uint64 ZigZagEncode64(int64 n);
  223. static int64 ZigZagDecode64(uint64 n);
  224. // =================================================================
  225. // Methods for reading/writing individual field. The implementations
  226. // of these methods are defined in wire_format_lite_inl.h; you must #include
  227. // that file to use these.
  228. #ifdef NDEBUG
  229. #define INL GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE
  230. #else
  231. // Avoid excessive inlining in non-optimized builds. Without other optimizations
  232. // the inlining is not going to provide benefits anyway and the huge resulting
  233. // functions, especially in the proto-generated serialization functions, produce
  234. // stack frames so large that many tests run into stack overflows (b/32192897).
  235. #define INL
  236. #endif
  237. // Read fields, not including tags. The assumption is that you already
  238. // read the tag to determine what field to read.
  239. // For primitive fields, we just use a templatized routine parameterized by
  240. // the represented type and the FieldType. These are specialized with the
  241. // appropriate definition for each declared type.
  242. template <typename CType, enum FieldType DeclaredType>
  243. INL static bool ReadPrimitive(io::CodedInputStream* input, CType* value);
  244. // Reads repeated primitive values, with optimizations for repeats.
  245. // tag_size and tag should both be compile-time constants provided by the
  246. // protocol compiler.
  247. template <typename CType, enum FieldType DeclaredType>
  248. INL static bool ReadRepeatedPrimitive(int tag_size, uint32 tag,
  249. io::CodedInputStream* input,
  250. RepeatedField<CType>* value);
  251. // Identical to ReadRepeatedPrimitive, except will not inline the
  252. // implementation.
  253. template <typename CType, enum FieldType DeclaredType>
  254. static bool ReadRepeatedPrimitiveNoInline(int tag_size, uint32 tag,
  255. io::CodedInputStream* input,
  256. RepeatedField<CType>* value);
  257. // Reads a primitive value directly from the provided buffer. It returns a
  258. // pointer past the segment of data that was read.
  259. //
  260. // This is only implemented for the types with fixed wire size, e.g.
  261. // float, double, and the (s)fixed* types.
  262. template <typename CType, enum FieldType DeclaredType> INL
  263. static const uint8* ReadPrimitiveFromArray(const uint8* buffer, CType* value);
  264. // Reads a primitive packed field.
  265. //
  266. // This is only implemented for packable types.
  267. template <typename CType, enum FieldType DeclaredType>
  268. INL static bool ReadPackedPrimitive(io::CodedInputStream* input,
  269. RepeatedField<CType>* value);
  270. // Identical to ReadPackedPrimitive, except will not inline the
  271. // implementation.
  272. template <typename CType, enum FieldType DeclaredType>
  273. static bool ReadPackedPrimitiveNoInline(io::CodedInputStream* input,
  274. RepeatedField<CType>* value);
  275. // Read a packed enum field. If the is_valid function is not NULL, values for
  276. // which is_valid(value) returns false are silently dropped.
  277. static bool ReadPackedEnumNoInline(io::CodedInputStream* input,
  278. bool (*is_valid)(int),
  279. RepeatedField<int>* values);
  280. // Read a packed enum field. If the is_valid function is not NULL, values for
  281. // which is_valid(value) returns false are appended to unknown_fields_stream.
  282. static bool ReadPackedEnumPreserveUnknowns(
  283. io::CodedInputStream* input, int field_number, bool (*is_valid)(int),
  284. io::CodedOutputStream* unknown_fields_stream, RepeatedField<int>* values);
  285. // Read a string. ReadString(..., string* value) requires an existing string.
  286. static inline bool ReadString(io::CodedInputStream* input, string* value);
  287. // ReadString(..., string** p) is internal-only, and should only be called
  288. // from generated code. It starts by setting *p to "new string"
  289. // if *p == &GetEmptyStringAlreadyInited(). It then invokes
  290. // ReadString(io::CodedInputStream* input, *p). This is useful for reducing
  291. // code size.
  292. static inline bool ReadString(io::CodedInputStream* input, string** p);
  293. // Analogous to ReadString().
  294. static bool ReadBytes(io::CodedInputStream* input, string* value);
  295. static bool ReadBytes(io::CodedInputStream* input, string** p);
  296. enum Operation {
  297. PARSE = 0,
  298. SERIALIZE = 1,
  299. };
  300. // Returns true if the data is valid UTF-8.
  301. static bool VerifyUtf8String(const char* data, int size,
  302. Operation op,
  303. const char* field_name);
  304. template <typename MessageType>
  305. static inline bool ReadGroup(int field_number, io::CodedInputStream* input,
  306. MessageType* value);
  307. template <typename MessageType>
  308. static inline bool ReadMessage(io::CodedInputStream* input,
  309. MessageType* value);
  310. // Do not use.
  311. template <typename MessageType>
  312. static inline bool ReadGroupNoVirtual(int field_number,
  313. io::CodedInputStream* input,
  314. MessageType* value) {
  315. return ReadGroup(field_number, input, value);
  316. }
  317. template<typename MessageType>
  318. static inline bool ReadMessageNoVirtual(io::CodedInputStream* input,
  319. MessageType* value) {
  320. return ReadMessage(input, value);
  321. }
  322. // Write a tag. The Write*() functions typically include the tag, so
  323. // normally there's no need to call this unless using the Write*NoTag()
  324. // variants.
  325. INL static void WriteTag(int field_number, WireType type,
  326. io::CodedOutputStream* output);
  327. // Write fields, without tags.
  328. INL static void WriteInt32NoTag(int32 value, io::CodedOutputStream* output);
  329. INL static void WriteInt64NoTag(int64 value, io::CodedOutputStream* output);
  330. INL static void WriteUInt32NoTag(uint32 value, io::CodedOutputStream* output);
  331. INL static void WriteUInt64NoTag(uint64 value, io::CodedOutputStream* output);
  332. INL static void WriteSInt32NoTag(int32 value, io::CodedOutputStream* output);
  333. INL static void WriteSInt64NoTag(int64 value, io::CodedOutputStream* output);
  334. INL static void WriteFixed32NoTag(uint32 value,
  335. io::CodedOutputStream* output);
  336. INL static void WriteFixed64NoTag(uint64 value,
  337. io::CodedOutputStream* output);
  338. INL static void WriteSFixed32NoTag(int32 value,
  339. io::CodedOutputStream* output);
  340. INL static void WriteSFixed64NoTag(int64 value,
  341. io::CodedOutputStream* output);
  342. INL static void WriteFloatNoTag(float value, io::CodedOutputStream* output);
  343. INL static void WriteDoubleNoTag(double value, io::CodedOutputStream* output);
  344. INL static void WriteBoolNoTag(bool value, io::CodedOutputStream* output);
  345. INL static void WriteEnumNoTag(int value, io::CodedOutputStream* output);
  346. // Write array of primitive fields, without tags
  347. static void WriteFloatArray(const float* a, int n,
  348. io::CodedOutputStream* output);
  349. static void WriteDoubleArray(const double* a, int n,
  350. io::CodedOutputStream* output);
  351. static void WriteFixed32Array(const uint32* a, int n,
  352. io::CodedOutputStream* output);
  353. static void WriteFixed64Array(const uint64* a, int n,
  354. io::CodedOutputStream* output);
  355. static void WriteSFixed32Array(const int32* a, int n,
  356. io::CodedOutputStream* output);
  357. static void WriteSFixed64Array(const int64* a, int n,
  358. io::CodedOutputStream* output);
  359. static void WriteBoolArray(const bool* a, int n,
  360. io::CodedOutputStream* output);
  361. // Write fields, including tags.
  362. static void WriteInt32(int field_number, int32 value,
  363. io::CodedOutputStream* output);
  364. static void WriteInt64(int field_number, int64 value,
  365. io::CodedOutputStream* output);
  366. static void WriteUInt32(int field_number, uint32 value,
  367. io::CodedOutputStream* output);
  368. static void WriteUInt64(int field_number, uint64 value,
  369. io::CodedOutputStream* output);
  370. static void WriteSInt32(int field_number, int32 value,
  371. io::CodedOutputStream* output);
  372. static void WriteSInt64(int field_number, int64 value,
  373. io::CodedOutputStream* output);
  374. static void WriteFixed32(int field_number, uint32 value,
  375. io::CodedOutputStream* output);
  376. static void WriteFixed64(int field_number, uint64 value,
  377. io::CodedOutputStream* output);
  378. static void WriteSFixed32(int field_number, int32 value,
  379. io::CodedOutputStream* output);
  380. static void WriteSFixed64(int field_number, int64 value,
  381. io::CodedOutputStream* output);
  382. static void WriteFloat(int field_number, float value,
  383. io::CodedOutputStream* output);
  384. static void WriteDouble(int field_number, double value,
  385. io::CodedOutputStream* output);
  386. static void WriteBool(int field_number, bool value,
  387. io::CodedOutputStream* output);
  388. static void WriteEnum(int field_number, int value,
  389. io::CodedOutputStream* output);
  390. static void WriteString(int field_number, const string& value,
  391. io::CodedOutputStream* output);
  392. static void WriteBytes(int field_number, const string& value,
  393. io::CodedOutputStream* output);
  394. static void WriteStringMaybeAliased(int field_number, const string& value,
  395. io::CodedOutputStream* output);
  396. static void WriteBytesMaybeAliased(int field_number, const string& value,
  397. io::CodedOutputStream* output);
  398. static void WriteGroup(int field_number, const MessageLite& value,
  399. io::CodedOutputStream* output);
  400. static void WriteMessage(int field_number, const MessageLite& value,
  401. io::CodedOutputStream* output);
  402. // Like above, but these will check if the output stream has enough
  403. // space to write directly to a flat array.
  404. static void WriteGroupMaybeToArray(int field_number, const MessageLite& value,
  405. io::CodedOutputStream* output);
  406. static void WriteMessageMaybeToArray(int field_number,
  407. const MessageLite& value,
  408. io::CodedOutputStream* output);
  409. // Like above, but de-virtualize the call to SerializeWithCachedSizes(). The
  410. // pointer must point at an instance of MessageType, *not* a subclass (or
  411. // the subclass must not override SerializeWithCachedSizes()).
  412. template <typename MessageType>
  413. static inline void WriteGroupNoVirtual(int field_number,
  414. const MessageType& value,
  415. io::CodedOutputStream* output);
  416. template <typename MessageType>
  417. static inline void WriteMessageNoVirtual(int field_number,
  418. const MessageType& value,
  419. io::CodedOutputStream* output);
  420. // Like above, but use only *ToArray methods of CodedOutputStream.
  421. INL static uint8* WriteTagToArray(int field_number, WireType type,
  422. uint8* target);
  423. // Write fields, without tags.
  424. INL static uint8* WriteInt32NoTagToArray(int32 value, uint8* target);
  425. INL static uint8* WriteInt64NoTagToArray(int64 value, uint8* target);
  426. INL static uint8* WriteUInt32NoTagToArray(uint32 value, uint8* target);
  427. INL static uint8* WriteUInt64NoTagToArray(uint64 value, uint8* target);
  428. INL static uint8* WriteSInt32NoTagToArray(int32 value, uint8* target);
  429. INL static uint8* WriteSInt64NoTagToArray(int64 value, uint8* target);
  430. INL static uint8* WriteFixed32NoTagToArray(uint32 value, uint8* target);
  431. INL static uint8* WriteFixed64NoTagToArray(uint64 value, uint8* target);
  432. INL static uint8* WriteSFixed32NoTagToArray(int32 value, uint8* target);
  433. INL static uint8* WriteSFixed64NoTagToArray(int64 value, uint8* target);
  434. INL static uint8* WriteFloatNoTagToArray(float value, uint8* target);
  435. INL static uint8* WriteDoubleNoTagToArray(double value, uint8* target);
  436. INL static uint8* WriteBoolNoTagToArray(bool value, uint8* target);
  437. INL static uint8* WriteEnumNoTagToArray(int value, uint8* target);
  438. // Write fields, without tags. These require that value.size() > 0.
  439. template<typename T>
  440. INL static uint8* WritePrimitiveNoTagToArray(
  441. const RepeatedField<T>& value,
  442. uint8* (*Writer)(T, uint8*), uint8* target);
  443. template<typename T>
  444. INL static uint8* WriteFixedNoTagToArray(
  445. const RepeatedField<T>& value,
  446. uint8* (*Writer)(T, uint8*), uint8* target);
  447. INL static uint8* WriteInt32NoTagToArray(
  448. const RepeatedField< int32>& value, uint8* output);
  449. INL static uint8* WriteInt64NoTagToArray(
  450. const RepeatedField< int64>& value, uint8* output);
  451. INL static uint8* WriteUInt32NoTagToArray(
  452. const RepeatedField<uint32>& value, uint8* output);
  453. INL static uint8* WriteUInt64NoTagToArray(
  454. const RepeatedField<uint64>& value, uint8* output);
  455. INL static uint8* WriteSInt32NoTagToArray(
  456. const RepeatedField< int32>& value, uint8* output);
  457. INL static uint8* WriteSInt64NoTagToArray(
  458. const RepeatedField< int64>& value, uint8* output);
  459. INL static uint8* WriteFixed32NoTagToArray(
  460. const RepeatedField<uint32>& value, uint8* output);
  461. INL static uint8* WriteFixed64NoTagToArray(
  462. const RepeatedField<uint64>& value, uint8* output);
  463. INL static uint8* WriteSFixed32NoTagToArray(
  464. const RepeatedField< int32>& value, uint8* output);
  465. INL static uint8* WriteSFixed64NoTagToArray(
  466. const RepeatedField< int64>& value, uint8* output);
  467. INL static uint8* WriteFloatNoTagToArray(
  468. const RepeatedField< float>& value, uint8* output);
  469. INL static uint8* WriteDoubleNoTagToArray(
  470. const RepeatedField<double>& value, uint8* output);
  471. INL static uint8* WriteBoolNoTagToArray(
  472. const RepeatedField< bool>& value, uint8* output);
  473. INL static uint8* WriteEnumNoTagToArray(
  474. const RepeatedField< int>& value, uint8* output);
  475. // Write fields, including tags.
  476. INL static uint8* WriteInt32ToArray(int field_number, int32 value,
  477. uint8* target);
  478. INL static uint8* WriteInt64ToArray(int field_number, int64 value,
  479. uint8* target);
  480. INL static uint8* WriteUInt32ToArray(int field_number, uint32 value,
  481. uint8* target);
  482. INL static uint8* WriteUInt64ToArray(int field_number, uint64 value,
  483. uint8* target);
  484. INL static uint8* WriteSInt32ToArray(int field_number, int32 value,
  485. uint8* target);
  486. INL static uint8* WriteSInt64ToArray(int field_number, int64 value,
  487. uint8* target);
  488. INL static uint8* WriteFixed32ToArray(int field_number, uint32 value,
  489. uint8* target);
  490. INL static uint8* WriteFixed64ToArray(int field_number, uint64 value,
  491. uint8* target);
  492. INL static uint8* WriteSFixed32ToArray(int field_number, int32 value,
  493. uint8* target);
  494. INL static uint8* WriteSFixed64ToArray(int field_number, int64 value,
  495. uint8* target);
  496. INL static uint8* WriteFloatToArray(int field_number, float value,
  497. uint8* target);
  498. INL static uint8* WriteDoubleToArray(int field_number, double value,
  499. uint8* target);
  500. INL static uint8* WriteBoolToArray(int field_number, bool value,
  501. uint8* target);
  502. INL static uint8* WriteEnumToArray(int field_number, int value,
  503. uint8* target);
  504. template<typename T>
  505. INL static uint8* WritePrimitiveToArray(
  506. int field_number,
  507. const RepeatedField<T>& value,
  508. uint8* (*Writer)(int, T, uint8*), uint8* target);
  509. INL static uint8* WriteInt32ToArray(
  510. int field_number, const RepeatedField< int32>& value, uint8* output);
  511. INL static uint8* WriteInt64ToArray(
  512. int field_number, const RepeatedField< int64>& value, uint8* output);
  513. INL static uint8* WriteUInt32ToArray(
  514. int field_number, const RepeatedField<uint32>& value, uint8* output);
  515. INL static uint8* WriteUInt64ToArray(
  516. int field_number, const RepeatedField<uint64>& value, uint8* output);
  517. INL static uint8* WriteSInt32ToArray(
  518. int field_number, const RepeatedField< int32>& value, uint8* output);
  519. INL static uint8* WriteSInt64ToArray(
  520. int field_number, const RepeatedField< int64>& value, uint8* output);
  521. INL static uint8* WriteFixed32ToArray(
  522. int field_number, const RepeatedField<uint32>& value, uint8* output);
  523. INL static uint8* WriteFixed64ToArray(
  524. int field_number, const RepeatedField<uint64>& value, uint8* output);
  525. INL static uint8* WriteSFixed32ToArray(
  526. int field_number, const RepeatedField< int32>& value, uint8* output);
  527. INL static uint8* WriteSFixed64ToArray(
  528. int field_number, const RepeatedField< int64>& value, uint8* output);
  529. INL static uint8* WriteFloatToArray(
  530. int field_number, const RepeatedField< float>& value, uint8* output);
  531. INL static uint8* WriteDoubleToArray(
  532. int field_number, const RepeatedField<double>& value, uint8* output);
  533. INL static uint8* WriteBoolToArray(
  534. int field_number, const RepeatedField< bool>& value, uint8* output);
  535. INL static uint8* WriteEnumToArray(
  536. int field_number, const RepeatedField< int>& value, uint8* output);
  537. INL static uint8* WriteStringToArray(int field_number, const string& value,
  538. uint8* target);
  539. INL static uint8* WriteBytesToArray(int field_number, const string& value,
  540. uint8* target);
  541. // Whether to serialize deterministically (e.g., map keys are
  542. // sorted) is a property of a CodedOutputStream, and in the process
  543. // of serialization, the "ToArray" variants may be invoked. But they don't
  544. // have a CodedOutputStream available, so they get an additional parameter
  545. // telling them whether to serialize deterministically.
  546. template<typename MessageType>
  547. INL static uint8* InternalWriteGroupToArray(int field_number,
  548. const MessageType& value,
  549. bool deterministic,
  550. uint8* target);
  551. template<typename MessageType>
  552. INL static uint8* InternalWriteMessageToArray(int field_number,
  553. const MessageType& value,
  554. bool deterministic,
  555. uint8* target);
  556. // Like above, but de-virtualize the call to SerializeWithCachedSizes(). The
  557. // pointer must point at an instance of MessageType, *not* a subclass (or
  558. // the subclass must not override SerializeWithCachedSizes()).
  559. template <typename MessageType>
  560. INL static uint8* InternalWriteGroupNoVirtualToArray(int field_number,
  561. const MessageType& value,
  562. bool deterministic,
  563. uint8* target);
  564. template <typename MessageType>
  565. INL static uint8* InternalWriteMessageNoVirtualToArray(
  566. int field_number, const MessageType& value, bool deterministic,
  567. uint8* target);
  568. // For backward-compatibility, the last four methods also have versions
  569. // that are non-deterministic always.
  570. INL static uint8* WriteGroupToArray(int field_number,
  571. const MessageLite& value, uint8* target) {
  572. return InternalWriteGroupToArray(field_number, value, false, target);
  573. }
  574. INL static uint8* WriteMessageToArray(int field_number,
  575. const MessageLite& value,
  576. uint8* target) {
  577. return InternalWriteMessageToArray(field_number, value, false, target);
  578. }
  579. template <typename MessageType>
  580. INL static uint8* WriteGroupNoVirtualToArray(int field_number,
  581. const MessageType& value,
  582. uint8* target) {
  583. return InternalWriteGroupNoVirtualToArray(field_number, value, false,
  584. target);
  585. }
  586. template <typename MessageType>
  587. INL static uint8* WriteMessageNoVirtualToArray(int field_number,
  588. const MessageType& value,
  589. uint8* target) {
  590. return InternalWriteMessageNoVirtualToArray(field_number, value, false,
  591. target);
  592. }
  593. #undef INL
  594. // Compute the byte size of a field. The XxSize() functions do NOT include
  595. // the tag, so you must also call TagSize(). (This is because, for repeated
  596. // fields, you should only call TagSize() once and multiply it by the element
  597. // count, but you may have to call XxSize() for each individual element.)
  598. static inline size_t Int32Size ( int32 value);
  599. static inline size_t Int64Size ( int64 value);
  600. static inline size_t UInt32Size (uint32 value);
  601. static inline size_t UInt64Size (uint64 value);
  602. static inline size_t SInt32Size ( int32 value);
  603. static inline size_t SInt64Size ( int64 value);
  604. static inline size_t EnumSize ( int value);
  605. static size_t Int32Size (const RepeatedField< int32>& value);
  606. static size_t Int64Size (const RepeatedField< int64>& value);
  607. static size_t UInt32Size(const RepeatedField<uint32>& value);
  608. static size_t UInt64Size(const RepeatedField<uint64>& value);
  609. static size_t SInt32Size(const RepeatedField< int32>& value);
  610. static size_t SInt64Size(const RepeatedField< int64>& value);
  611. static size_t EnumSize (const RepeatedField< int>& value);
  612. // These types always have the same size.
  613. static const size_t kFixed32Size = 4;
  614. static const size_t kFixed64Size = 8;
  615. static const size_t kSFixed32Size = 4;
  616. static const size_t kSFixed64Size = 8;
  617. static const size_t kFloatSize = 4;
  618. static const size_t kDoubleSize = 8;
  619. static const size_t kBoolSize = 1;
  620. static inline size_t StringSize(const string& value);
  621. static inline size_t BytesSize (const string& value);
  622. template<typename MessageType>
  623. static inline size_t GroupSize (const MessageType& value);
  624. template<typename MessageType>
  625. static inline size_t MessageSize(const MessageType& value);
  626. // Like above, but de-virtualize the call to ByteSize(). The
  627. // pointer must point at an instance of MessageType, *not* a subclass (or
  628. // the subclass must not override ByteSize()).
  629. template<typename MessageType>
  630. static inline size_t GroupSizeNoVirtual (const MessageType& value);
  631. template<typename MessageType>
  632. static inline size_t MessageSizeNoVirtual(const MessageType& value);
  633. // Given the length of data, calculate the byte size of the data on the
  634. // wire if we encode the data as a length delimited field.
  635. static inline size_t LengthDelimitedSize(size_t length);
  636. private:
  637. // A helper method for the repeated primitive reader. This method has
  638. // optimizations for primitive types that have fixed size on the wire, and
  639. // can be read using potentially faster paths.
  640. template <typename CType, enum FieldType DeclaredType>
  641. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE
  642. static bool ReadRepeatedFixedSizePrimitive(
  643. int tag_size,
  644. uint32 tag,
  645. google::protobuf::io::CodedInputStream* input,
  646. RepeatedField<CType>* value);
  647. // Like ReadRepeatedFixedSizePrimitive but for packed primitive fields.
  648. template <typename CType, enum FieldType DeclaredType>
  649. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE
  650. static bool ReadPackedFixedSizePrimitive(
  651. google::protobuf::io::CodedInputStream* input, RepeatedField<CType>* value);
  652. static const CppType kFieldTypeToCppTypeMap[];
  653. static const WireFormatLite::WireType kWireTypeForFieldType[];
  654. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(WireFormatLite);
  655. };
  656. // A class which deals with unknown values. The default implementation just
  657. // discards them. WireFormat defines a subclass which writes to an
  658. // UnknownFieldSet. This class is used by ExtensionSet::ParseField(), since
  659. // ExtensionSet is part of the lite library but UnknownFieldSet is not.
  660. class LIBPROTOBUF_EXPORT FieldSkipper {
  661. public:
  662. FieldSkipper() {}
  663. virtual ~FieldSkipper() {}
  664. // Skip a field whose tag has already been consumed.
  665. virtual bool SkipField(io::CodedInputStream* input, uint32 tag);
  666. // Skip an entire message or group, up to an end-group tag (which is consumed)
  667. // or end-of-stream.
  668. virtual bool SkipMessage(io::CodedInputStream* input);
  669. // Deal with an already-parsed unrecognized enum value. The default
  670. // implementation does nothing, but the UnknownFieldSet-based implementation
  671. // saves it as an unknown varint.
  672. virtual void SkipUnknownEnum(int field_number, int value);
  673. };
  674. // Subclass of FieldSkipper which saves skipped fields to a CodedOutputStream.
  675. class LIBPROTOBUF_EXPORT CodedOutputStreamFieldSkipper : public FieldSkipper {
  676. public:
  677. explicit CodedOutputStreamFieldSkipper(io::CodedOutputStream* unknown_fields)
  678. : unknown_fields_(unknown_fields) {}
  679. virtual ~CodedOutputStreamFieldSkipper() {}
  680. // implements FieldSkipper -----------------------------------------
  681. virtual bool SkipField(io::CodedInputStream* input, uint32 tag);
  682. virtual bool SkipMessage(io::CodedInputStream* input);
  683. virtual void SkipUnknownEnum(int field_number, int value);
  684. protected:
  685. io::CodedOutputStream* unknown_fields_;
  686. };
  687. // inline methods ====================================================
  688. inline WireFormatLite::CppType
  689. WireFormatLite::FieldTypeToCppType(FieldType type) {
  690. return kFieldTypeToCppTypeMap[type];
  691. }
  692. inline uint32 WireFormatLite::MakeTag(int field_number, WireType type) {
  693. return GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(field_number, type);
  694. }
  695. inline WireFormatLite::WireType WireFormatLite::GetTagWireType(uint32 tag) {
  696. return static_cast<WireType>(tag & kTagTypeMask);
  697. }
  698. inline int WireFormatLite::GetTagFieldNumber(uint32 tag) {
  699. return static_cast<int>(tag >> kTagTypeBits);
  700. }
  701. inline size_t WireFormatLite::TagSize(int field_number,
  702. WireFormatLite::FieldType type) {
  703. size_t result = io::CodedOutputStream::VarintSize32(
  704. static_cast<uint32>(field_number << kTagTypeBits));
  705. if (type == TYPE_GROUP) {
  706. // Groups have both a start and an end tag.
  707. return result * 2;
  708. } else {
  709. return result;
  710. }
  711. }
  712. inline uint32 WireFormatLite::EncodeFloat(float value) {
  713. union {float f; uint32 i;};
  714. f = value;
  715. return i;
  716. }
  717. inline float WireFormatLite::DecodeFloat(uint32 value) {
  718. union {float f; uint32 i;};
  719. i = value;
  720. return f;
  721. }
  722. inline uint64 WireFormatLite::EncodeDouble(double value) {
  723. union {double f; uint64 i;};
  724. f = value;
  725. return i;
  726. }
  727. inline double WireFormatLite::DecodeDouble(uint64 value) {
  728. union {double f; uint64 i;};
  729. i = value;
  730. return f;
  731. }
  732. // ZigZag Transform: Encodes signed integers so that they can be
  733. // effectively used with varint encoding.
  734. //
  735. // varint operates on unsigned integers, encoding smaller numbers into
  736. // fewer bytes. If you try to use it on a signed integer, it will treat
  737. // this number as a very large unsigned integer, which means that even
  738. // small signed numbers like -1 will take the maximum number of bytes
  739. // (10) to encode. ZigZagEncode() maps signed integers to unsigned
  740. // in such a way that those with a small absolute value will have smaller
  741. // encoded values, making them appropriate for encoding using varint.
  742. //
  743. // int32 -> uint32
  744. // -------------------------
  745. // 0 -> 0
  746. // -1 -> 1
  747. // 1 -> 2
  748. // -2 -> 3
  749. // ... -> ...
  750. // 2147483647 -> 4294967294
  751. // -2147483648 -> 4294967295
  752. //
  753. // >> encode >>
  754. // << decode <<
  755. inline uint32 WireFormatLite::ZigZagEncode32(int32 n) {
  756. // Note: the right-shift must be arithmetic
  757. // Note: left shift must be unsigned because of overflow
  758. return (static_cast<uint32>(n) << 1) ^ static_cast<uint32>(n >> 31);
  759. }
  760. inline int32 WireFormatLite::ZigZagDecode32(uint32 n) {
  761. // Note: Using unsigned types prevent undefined behavior
  762. return static_cast<int32>((n >> 1) ^ (~(n & 1) + 1));
  763. }
  764. inline uint64 WireFormatLite::ZigZagEncode64(int64 n) {
  765. // Note: the right-shift must be arithmetic
  766. // Note: left shift must be unsigned because of overflow
  767. return (static_cast<uint64>(n) << 1) ^ static_cast<uint64>(n >> 63);
  768. }
  769. inline int64 WireFormatLite::ZigZagDecode64(uint64 n) {
  770. // Note: Using unsigned types prevent undefined behavior
  771. return static_cast<int64>((n >> 1) ^ (~(n & 1) + 1));
  772. }
  773. // String is for UTF-8 text only, but, even so, ReadString() can simply
  774. // call ReadBytes().
  775. inline bool WireFormatLite::ReadString(io::CodedInputStream* input,
  776. string* value) {
  777. return ReadBytes(input, value);
  778. }
  779. inline bool WireFormatLite::ReadString(io::CodedInputStream* input,
  780. string** p) {
  781. return ReadBytes(input, p);
  782. }
  783. } // namespace internal
  784. } // namespace protobuf
  785. } // namespace google
  786. #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__