You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

wire_format.h 21 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. // Based on original Protocol Buffers design by
  33. // Sanjay Ghemawat, Jeff Dean, and others.
  34. //
  35. // This header is logically internal, but is made public because it is used
  36. // from protocol-compiler-generated code, which may reside in other components.
  37. #ifndef GOOGLE_PROTOBUF_WIRE_FORMAT_H__
  38. #define GOOGLE_PROTOBUF_WIRE_FORMAT_H__
  39. #include <google/protobuf/stubs/common.h>
  40. #include <google/protobuf/io/coded_stream.h>
  41. #include <google/protobuf/stubs/casts.h>
  42. #include <google/protobuf/descriptor.h>
  43. #include <google/protobuf/generated_message_util.h>
  44. #include <google/protobuf/message.h>
  45. #include <google/protobuf/metadata_lite.h>
  46. #include <google/protobuf/parse_context.h>
  47. #include <google/protobuf/wire_format_lite.h>
  48. #ifdef SWIG
  49. #error "You cannot SWIG proto headers"
  50. #endif
  51. // Must be included last.
  52. #include <google/protobuf/port_def.inc>
  53. namespace google
  54. {
  55. namespace protobuf
  56. {
  57. class MapKey; // map_field.h
  58. class UnknownFieldSet; // unknown_field_set.h
  59. } // namespace protobuf
  60. } // namespace google
  61. namespace google
  62. {
  63. namespace protobuf
  64. {
  65. namespace internal
  66. {
  67. // This class is for internal use by the protocol buffer library and by
  68. // protocol-compiler-generated message classes. It must not be called
  69. // directly by clients.
  70. //
  71. // This class contains code for implementing the binary protocol buffer
  72. // wire format via reflection. The WireFormatLite class implements the
  73. // non-reflection based routines.
  74. //
  75. // This class is really a namespace that contains only static methods
  76. class PROTOBUF_EXPORT WireFormat
  77. {
  78. public:
  79. // Given a field return its WireType
  80. static inline WireFormatLite::WireType WireTypeForField(
  81. const FieldDescriptor* field
  82. );
  83. // Given a FieldDescriptor::Type return its WireType
  84. static inline WireFormatLite::WireType WireTypeForFieldType(
  85. FieldDescriptor::Type type
  86. );
  87. // Compute the byte size of a tag. For groups, this includes both the start
  88. // and end tags.
  89. static inline size_t TagSize(int field_number, FieldDescriptor::Type type);
  90. // These procedures can be used to implement the methods of Message which
  91. // handle parsing and serialization of the protocol buffer wire format
  92. // using only the Reflection interface. When you ask the protocol
  93. // compiler to optimize for code size rather than speed, it will implement
  94. // those methods in terms of these procedures. Of course, these are much
  95. // slower than the specialized implementations which the protocol compiler
  96. // generates when told to optimize for speed.
  97. // Read a message in protocol buffer wire format.
  98. //
  99. // This procedure reads either to the end of the input stream or through
  100. // a WIRETYPE_END_GROUP tag ending the message, whichever comes first.
  101. // It returns false if the input is invalid.
  102. //
  103. // Required fields are NOT checked by this method. You must call
  104. // IsInitialized() on the resulting message yourself.
  105. static bool ParseAndMergePartial(io::CodedInputStream* input, Message* message);
  106. // This is meant for internal protobuf use (WireFormat is an internal class).
  107. // This is the reflective implementation of the _InternalParse functionality.
  108. static const char* _InternalParse(Message* msg, const char* ptr, internal::ParseContext* ctx);
  109. // Serialize a message in protocol buffer wire format.
  110. //
  111. // Any embedded messages within the message must have their correct sizes
  112. // cached. However, the top-level message need not; its size is passed as
  113. // a parameter to this procedure.
  114. //
  115. // These return false iff the underlying stream returns a write error.
  116. static void SerializeWithCachedSizes(const Message& message, int size, io::CodedOutputStream* output)
  117. {
  118. int expected_endpoint = output->ByteCount() + size;
  119. output->SetCur(
  120. _InternalSerialize(message, output->Cur(), output->EpsCopy())
  121. );
  122. GOOGLE_CHECK_EQ(output->ByteCount(), expected_endpoint)
  123. << ": Protocol message serialized to a size different from what was "
  124. "originally expected. Perhaps it was modified by another thread "
  125. "during serialization?";
  126. }
  127. static uint8_t* _InternalSerialize(const Message& message, uint8_t* target, io::EpsCopyOutputStream* stream);
  128. // Implements Message::ByteSize() via reflection. WARNING: The result
  129. // of this method is *not* cached anywhere. However, all embedded messages
  130. // will have their ByteSize() methods called, so their sizes will be cached.
  131. // Therefore, calling this method is sufficient to allow you to call
  132. // WireFormat::SerializeWithCachedSizes() on the same object.
  133. static size_t ByteSize(const Message& message);
  134. // -----------------------------------------------------------------
  135. // Helpers for dealing with unknown fields
  136. // Skips a field value of the given WireType. The input should start
  137. // positioned immediately after the tag. If unknown_fields is non-nullptr,
  138. // the contents of the field will be added to it.
  139. static bool SkipField(io::CodedInputStream* input, uint32_t tag, UnknownFieldSet* unknown_fields);
  140. // Reads and ignores a message from the input. If unknown_fields is
  141. // non-nullptr, the contents will be added to it.
  142. static bool SkipMessage(io::CodedInputStream* input, UnknownFieldSet* unknown_fields);
  143. // Read a packed enum field. If the is_valid function is not nullptr, values
  144. // for which is_valid(value) returns false are appended to
  145. // unknown_fields_stream.
  146. static bool ReadPackedEnumPreserveUnknowns(io::CodedInputStream* input, uint32_t field_number, bool (*is_valid)(int), UnknownFieldSet* unknown_fields, RepeatedField<int>* values);
  147. // Write the contents of an UnknownFieldSet to the output.
  148. static void SerializeUnknownFields(const UnknownFieldSet& unknown_fields, io::CodedOutputStream* output)
  149. {
  150. output->SetCur(InternalSerializeUnknownFieldsToArray(
  151. unknown_fields, output->Cur(), output->EpsCopy()
  152. ));
  153. }
  154. // Same as above, except writing directly to the provided buffer.
  155. // Requires that the buffer have sufficient capacity for
  156. // ComputeUnknownFieldsSize(unknown_fields).
  157. //
  158. // Returns a pointer past the last written byte.
  159. static uint8_t* SerializeUnknownFieldsToArray(
  160. const UnknownFieldSet& unknown_fields, uint8_t* target
  161. )
  162. {
  163. io::EpsCopyOutputStream stream(
  164. target, static_cast<int>(ComputeUnknownFieldsSize(unknown_fields)), io::CodedOutputStream::IsDefaultSerializationDeterministic()
  165. );
  166. return InternalSerializeUnknownFieldsToArray(unknown_fields, target, &stream);
  167. }
  168. static uint8_t* InternalSerializeUnknownFieldsToArray(
  169. const UnknownFieldSet& unknown_fields, uint8_t* target, io::EpsCopyOutputStream* stream
  170. );
  171. // Same thing except for messages that have the message_set_wire_format
  172. // option.
  173. static void SerializeUnknownMessageSetItems(
  174. const UnknownFieldSet& unknown_fields, io::CodedOutputStream* output
  175. )
  176. {
  177. output->SetCur(InternalSerializeUnknownMessageSetItemsToArray(
  178. unknown_fields, output->Cur(), output->EpsCopy()
  179. ));
  180. }
  181. // Same as above, except writing directly to the provided buffer.
  182. // Requires that the buffer have sufficient capacity for
  183. // ComputeUnknownMessageSetItemsSize(unknown_fields).
  184. //
  185. // Returns a pointer past the last written byte.
  186. static uint8_t* SerializeUnknownMessageSetItemsToArray(
  187. const UnknownFieldSet& unknown_fields, uint8_t* target
  188. );
  189. static uint8_t* InternalSerializeUnknownMessageSetItemsToArray(
  190. const UnknownFieldSet& unknown_fields, uint8_t* target, io::EpsCopyOutputStream* stream
  191. );
  192. // Compute the size of the UnknownFieldSet on the wire.
  193. static size_t ComputeUnknownFieldsSize(const UnknownFieldSet& unknown_fields);
  194. // Same thing except for messages that have the message_set_wire_format
  195. // option.
  196. static size_t ComputeUnknownMessageSetItemsSize(
  197. const UnknownFieldSet& unknown_fields
  198. );
  199. // Helper functions for encoding and decoding tags. (Inlined below and in
  200. // _inl.h)
  201. //
  202. // This is different from MakeTag(field->number(), field->type()) in the
  203. // case of packed repeated fields.
  204. static uint32_t MakeTag(const FieldDescriptor* field);
  205. // Parse a single field. The input should start out positioned immediately
  206. // after the tag.
  207. static bool ParseAndMergeField(
  208. uint32_t tag,
  209. const FieldDescriptor* field, // May be nullptr for unknown
  210. Message* message,
  211. io::CodedInputStream* input
  212. );
  213. // Serialize a single field.
  214. static void SerializeFieldWithCachedSizes(
  215. const FieldDescriptor* field, // Cannot be nullptr
  216. const Message& message,
  217. io::CodedOutputStream* output
  218. )
  219. {
  220. output->SetCur(InternalSerializeField(field, message, output->Cur(), output->EpsCopy()));
  221. }
  222. static uint8_t* InternalSerializeField(
  223. const FieldDescriptor* field, // Cannot be nullptr
  224. const Message& message,
  225. uint8_t* target,
  226. io::EpsCopyOutputStream* stream
  227. );
  228. // Compute size of a single field. If the field is a message type, this
  229. // will call ByteSize() for the embedded message, insuring that it caches
  230. // its size.
  231. static size_t FieldByteSize(const FieldDescriptor* field, // Can't be nullptr
  232. const Message& message);
  233. // Parse/serialize a MessageSet::Item group. Used with messages that use
  234. // option message_set_wire_format = true.
  235. static bool ParseAndMergeMessageSetItem(io::CodedInputStream* input, Message* message);
  236. static void SerializeMessageSetItemWithCachedSizes(
  237. const FieldDescriptor* field, const Message& message, io::CodedOutputStream* output
  238. )
  239. {
  240. output->SetCur(InternalSerializeMessageSetItem(
  241. field, message, output->Cur(), output->EpsCopy()
  242. ));
  243. }
  244. static uint8_t* InternalSerializeMessageSetItem(
  245. const FieldDescriptor* field, const Message& message, uint8_t* target, io::EpsCopyOutputStream* stream
  246. );
  247. static size_t MessageSetItemByteSize(const FieldDescriptor* field, const Message& message);
  248. // Computes the byte size of a field, excluding tags. For packed fields, it
  249. // only includes the size of the raw data, and not the size of the total
  250. // length, but for other length-delimited types, the size of the length is
  251. // included.
  252. static size_t FieldDataOnlyByteSize(
  253. const FieldDescriptor* field, // Cannot be nullptr
  254. const Message& message
  255. );
  256. enum Operation
  257. {
  258. PARSE = 0,
  259. SERIALIZE = 1,
  260. };
  261. // Verifies that a string field is valid UTF8, logging an error if not.
  262. // This function will not be called by newly generated protobuf code
  263. // but remains present to support existing code.
  264. static void VerifyUTF8String(const char* data, int size, Operation op);
  265. // The NamedField variant takes a field name in order to produce an
  266. // informative error message if verification fails.
  267. static void VerifyUTF8StringNamedField(const char* data, int size, Operation op, const char* field_name);
  268. private:
  269. struct MessageSetParser;
  270. // Skip a MessageSet field.
  271. static bool SkipMessageSetField(io::CodedInputStream* input, uint32_t field_number, UnknownFieldSet* unknown_fields);
  272. // Parse a MessageSet field.
  273. static bool ParseAndMergeMessageSetField(uint32_t field_number, const FieldDescriptor* field, Message* message, io::CodedInputStream* input);
  274. // Parses the value from the wire that belongs to tag.
  275. static const char* _InternalParseAndMergeField(Message* msg, const char* ptr, internal::ParseContext* ctx, uint64_t tag, const Reflection* reflection, const FieldDescriptor* field);
  276. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(WireFormat);
  277. };
  278. // Subclass of FieldSkipper which saves skipped fields to an UnknownFieldSet.
  279. class PROTOBUF_EXPORT UnknownFieldSetFieldSkipper : public FieldSkipper
  280. {
  281. public:
  282. UnknownFieldSetFieldSkipper(UnknownFieldSet* unknown_fields) :
  283. unknown_fields_(unknown_fields)
  284. {
  285. }
  286. ~UnknownFieldSetFieldSkipper() override
  287. {
  288. }
  289. // implements FieldSkipper -----------------------------------------
  290. bool SkipField(io::CodedInputStream* input, uint32_t tag) override;
  291. bool SkipMessage(io::CodedInputStream* input) override;
  292. void SkipUnknownEnum(int field_number, int value) override;
  293. protected:
  294. UnknownFieldSet* unknown_fields_;
  295. };
  296. // inline methods ====================================================
  297. inline WireFormatLite::WireType WireFormat::WireTypeForField(
  298. const FieldDescriptor* field
  299. )
  300. {
  301. if (field->is_packed())
  302. {
  303. return WireFormatLite::WIRETYPE_LENGTH_DELIMITED;
  304. }
  305. else
  306. {
  307. return WireTypeForFieldType(field->type());
  308. }
  309. }
  310. inline WireFormatLite::WireType WireFormat::WireTypeForFieldType(
  311. FieldDescriptor::Type type
  312. )
  313. {
  314. // Some compilers don't like enum -> enum casts, so we implicit_cast to
  315. // int first.
  316. return WireFormatLite::WireTypeForFieldType(
  317. static_cast<WireFormatLite::FieldType>(implicit_cast<int>(type))
  318. );
  319. }
  320. inline uint32_t WireFormat::MakeTag(const FieldDescriptor* field)
  321. {
  322. return WireFormatLite::MakeTag(field->number(), WireTypeForField(field));
  323. }
  324. inline size_t WireFormat::TagSize(int field_number, FieldDescriptor::Type type)
  325. {
  326. // Some compilers don't like enum -> enum casts, so we implicit_cast to
  327. // int first.
  328. return WireFormatLite::TagSize(
  329. field_number,
  330. static_cast<WireFormatLite::FieldType>(implicit_cast<int>(type))
  331. );
  332. }
  333. inline void WireFormat::VerifyUTF8String(const char* data, int size, WireFormat::Operation op)
  334. {
  335. #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  336. WireFormatLite::VerifyUtf8String(
  337. data, size, static_cast<WireFormatLite::Operation>(op), nullptr
  338. );
  339. #else
  340. // Avoid the compiler warning about unused variables.
  341. (void)data;
  342. (void)size;
  343. (void)op;
  344. #endif
  345. }
  346. inline void WireFormat::VerifyUTF8StringNamedField(const char* data, int size, WireFormat::Operation op, const char* field_name)
  347. {
  348. #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  349. WireFormatLite::VerifyUtf8String(
  350. data, size, static_cast<WireFormatLite::Operation>(op), field_name
  351. );
  352. #else
  353. // Avoid the compiler warning about unused variables.
  354. (void)data;
  355. (void)size;
  356. (void)op;
  357. (void)field_name;
  358. #endif
  359. }
  360. inline uint8_t* InternalSerializeUnknownMessageSetItemsToArray(
  361. const UnknownFieldSet& unknown_fields, uint8_t* target, io::EpsCopyOutputStream* stream
  362. )
  363. {
  364. return WireFormat::InternalSerializeUnknownMessageSetItemsToArray(
  365. unknown_fields, target, stream
  366. );
  367. }
  368. inline size_t ComputeUnknownMessageSetItemsSize(
  369. const UnknownFieldSet& unknown_fields
  370. )
  371. {
  372. return WireFormat::ComputeUnknownMessageSetItemsSize(unknown_fields);
  373. }
  374. // Compute the size of the UnknownFieldSet on the wire.
  375. PROTOBUF_EXPORT
  376. size_t ComputeUnknownFieldsSize(const InternalMetadata& metadata, size_t size, CachedSize* cached_size);
  377. size_t MapKeyDataOnlyByteSize(const FieldDescriptor* field, const MapKey& value);
  378. uint8_t* SerializeMapKeyWithCachedSizes(const FieldDescriptor* field, const MapKey& value, uint8_t* target, io::EpsCopyOutputStream* stream);
  379. } // namespace internal
  380. } // namespace protobuf
  381. } // namespace google
  382. #include <google/protobuf/port_undef.inc>
  383. #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_H__