wire_format.h 18 KB

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