wire_format.h 14 KB

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