诸暨麻将添加redis
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

393 řádky
17 KiB

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