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.

unknown_field_set.h 17 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // Contains classes used to keep track of unrecognized fields seen while
  35. // parsing a protocol message.
  36. #ifndef GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__
  37. #define GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__
  38. #include <assert.h>
  39. #include <string>
  40. #include <vector>
  41. #include <google/protobuf/stubs/common.h>
  42. #include <google/protobuf/stubs/logging.h>
  43. #include <google/protobuf/io/coded_stream.h>
  44. #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
  45. #include <google/protobuf/port.h>
  46. #include <google/protobuf/message_lite.h>
  47. #include <google/protobuf/parse_context.h>
  48. // Must be included last.
  49. #include <google/protobuf/port_def.inc>
  50. #ifdef SWIG
  51. #error "You cannot SWIG proto headers"
  52. #endif
  53. namespace google
  54. {
  55. namespace protobuf
  56. {
  57. namespace internal
  58. {
  59. class InternalMetadata; // metadata_lite.h
  60. class WireFormat; // wire_format.h
  61. class MessageSetFieldSkipperUsingCord;
  62. // extension_set_heavy.cc
  63. } // namespace internal
  64. class Message; // message.h
  65. class UnknownField; // below
  66. // An UnknownFieldSet contains fields that were encountered while parsing a
  67. // message but were not defined by its type. Keeping track of these can be
  68. // useful, especially in that they may be written if the message is serialized
  69. // again without being cleared in between. This means that software which
  70. // simply receives messages and forwards them to other servers does not need
  71. // to be updated every time a new field is added to the message definition.
  72. //
  73. // To get the UnknownFieldSet attached to any message, call
  74. // Reflection::GetUnknownFields().
  75. //
  76. // This class is necessarily tied to the protocol buffer wire format, unlike
  77. // the Reflection interface which is independent of any serialization scheme.
  78. class PROTOBUF_EXPORT UnknownFieldSet
  79. {
  80. public:
  81. UnknownFieldSet();
  82. ~UnknownFieldSet();
  83. // Remove all fields.
  84. inline void Clear();
  85. // Remove all fields and deallocate internal data objects
  86. void ClearAndFreeMemory();
  87. // Is this set empty?
  88. inline bool empty() const;
  89. // Merge the contents of some other UnknownFieldSet with this one.
  90. void MergeFrom(const UnknownFieldSet& other);
  91. // Similar to above, but this function will destroy the contents of other.
  92. void MergeFromAndDestroy(UnknownFieldSet* other);
  93. // Merge the contents an UnknownFieldSet with the UnknownFieldSet in
  94. // *metadata, if there is one. If *metadata doesn't have an UnknownFieldSet
  95. // then add one to it and make it be a copy of the first arg.
  96. static void MergeToInternalMetadata(const UnknownFieldSet& other, internal::InternalMetadata* metadata);
  97. // Swaps the contents of some other UnknownFieldSet with this one.
  98. inline void Swap(UnknownFieldSet* x);
  99. // Computes (an estimate of) the total number of bytes currently used for
  100. // storing the unknown fields in memory. Does NOT include
  101. // sizeof(*this) in the calculation.
  102. size_t SpaceUsedExcludingSelfLong() const;
  103. int SpaceUsedExcludingSelf() const
  104. {
  105. return internal::ToIntSize(SpaceUsedExcludingSelfLong());
  106. }
  107. // Version of SpaceUsed() including sizeof(*this).
  108. size_t SpaceUsedLong() const;
  109. int SpaceUsed() const
  110. {
  111. return internal::ToIntSize(SpaceUsedLong());
  112. }
  113. // Returns the number of fields present in the UnknownFieldSet.
  114. inline int field_count() const;
  115. // Get a field in the set, where 0 <= index < field_count(). The fields
  116. // appear in the order in which they were added.
  117. inline const UnknownField& field(int index) const;
  118. // Get a mutable pointer to a field in the set, where
  119. // 0 <= index < field_count(). The fields appear in the order in which
  120. // they were added.
  121. inline UnknownField* mutable_field(int index);
  122. // Adding fields ---------------------------------------------------
  123. void AddVarint(int number, uint64_t value);
  124. void AddFixed32(int number, uint32_t value);
  125. void AddFixed64(int number, uint64_t value);
  126. void AddLengthDelimited(int number, const std::string& value);
  127. std::string* AddLengthDelimited(int number);
  128. UnknownFieldSet* AddGroup(int number);
  129. // Adds an unknown field from another set.
  130. void AddField(const UnknownField& field);
  131. // Delete fields with indices in the range [start .. start+num-1].
  132. // Caution: implementation moves all fields with indices [start+num .. ].
  133. void DeleteSubrange(int start, int num);
  134. // Delete all fields with a specific field number. The order of left fields
  135. // is preserved.
  136. // Caution: implementation moves all fields after the first deleted field.
  137. void DeleteByNumber(int number);
  138. // Parsing helpers -------------------------------------------------
  139. // These work exactly like the similarly-named methods of Message.
  140. bool MergeFromCodedStream(io::CodedInputStream* input);
  141. bool ParseFromCodedStream(io::CodedInputStream* input);
  142. bool ParseFromZeroCopyStream(io::ZeroCopyInputStream* input);
  143. bool ParseFromArray(const void* data, int size);
  144. inline bool ParseFromString(const std::string& data)
  145. {
  146. return ParseFromArray(data.data(), static_cast<int>(data.size()));
  147. }
  148. // Merges this message's unknown field data (if any). This works whether
  149. // the message is a lite or full proto (for legacy reasons, lite and full
  150. // return different types for MessageType::unknown_fields()).
  151. template<typename MessageType>
  152. bool MergeFromMessage(const MessageType& message);
  153. // Serialization.
  154. bool SerializeToString(std::string* output) const;
  155. bool SerializeToCodedStream(io::CodedOutputStream* output) const;
  156. static const UnknownFieldSet& default_instance();
  157. private:
  158. // For InternalMergeFrom
  159. friend class UnknownField;
  160. // Merges from other UnknownFieldSet. This method assumes, that this object
  161. // is newly created and has no fields.
  162. void InternalMergeFrom(const UnknownFieldSet& other);
  163. void ClearFallback();
  164. template<typename MessageType, typename std::enable_if<std::is_base_of<Message, MessageType>::value, int>::type = 0>
  165. bool InternalMergeFromMessage(const MessageType& message)
  166. {
  167. MergeFrom(message.GetReflection()->GetUnknownFields(message));
  168. return true;
  169. }
  170. template<typename MessageType, typename std::enable_if<std::is_base_of<MessageLite, MessageType>::value && !std::is_base_of<Message, MessageType>::value, int>::type = 0>
  171. bool InternalMergeFromMessage(const MessageType& message)
  172. {
  173. const auto& unknown_fields = message.unknown_fields();
  174. io::ArrayInputStream array_stream(unknown_fields.data(), unknown_fields.size());
  175. io::CodedInputStream coded_stream(&array_stream);
  176. return MergeFromCodedStream(&coded_stream);
  177. }
  178. std::vector<UnknownField> fields_;
  179. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UnknownFieldSet);
  180. };
  181. namespace internal
  182. {
  183. inline void WriteVarint(uint32_t num, uint64_t val, UnknownFieldSet* unknown)
  184. {
  185. unknown->AddVarint(num, val);
  186. }
  187. inline void WriteLengthDelimited(uint32_t num, StringPiece val, UnknownFieldSet* unknown)
  188. {
  189. unknown->AddLengthDelimited(num)->assign(val.data(), val.size());
  190. }
  191. PROTOBUF_EXPORT
  192. const char* UnknownGroupParse(UnknownFieldSet* unknown, const char* ptr, ParseContext* ctx);
  193. PROTOBUF_EXPORT
  194. const char* UnknownFieldParse(uint64_t tag, UnknownFieldSet* unknown, const char* ptr, ParseContext* ctx);
  195. } // namespace internal
  196. // Represents one field in an UnknownFieldSet.
  197. class PROTOBUF_EXPORT UnknownField
  198. {
  199. public:
  200. enum Type
  201. {
  202. TYPE_VARINT,
  203. TYPE_FIXED32,
  204. TYPE_FIXED64,
  205. TYPE_LENGTH_DELIMITED,
  206. TYPE_GROUP
  207. };
  208. // The field's field number, as seen on the wire.
  209. inline int number() const;
  210. // The field type.
  211. inline Type type() const;
  212. // Accessors -------------------------------------------------------
  213. // Each method works only for UnknownFields of the corresponding type.
  214. inline uint64_t varint() const;
  215. inline uint32_t fixed32() const;
  216. inline uint64_t fixed64() const;
  217. inline const std::string& length_delimited() const;
  218. inline const UnknownFieldSet& group() const;
  219. inline void set_varint(uint64_t value);
  220. inline void set_fixed32(uint32_t value);
  221. inline void set_fixed64(uint64_t value);
  222. inline void set_length_delimited(const std::string& value);
  223. inline std::string* mutable_length_delimited();
  224. inline UnknownFieldSet* mutable_group();
  225. inline size_t GetLengthDelimitedSize() const;
  226. uint8_t* InternalSerializeLengthDelimitedNoTag(
  227. uint8_t* target, io::EpsCopyOutputStream* stream
  228. ) const;
  229. // If this UnknownField contains a pointer, delete it.
  230. void Delete();
  231. // Make a deep copy of any pointers in this UnknownField.
  232. void DeepCopy(const UnknownField& other);
  233. // Set the wire type of this UnknownField. Should only be used when this
  234. // UnknownField is being created.
  235. inline void SetType(Type type);
  236. union LengthDelimited
  237. {
  238. std::string* string_value;
  239. };
  240. uint32_t number_;
  241. uint32_t type_;
  242. union
  243. {
  244. uint64_t varint_;
  245. uint32_t fixed32_;
  246. uint64_t fixed64_;
  247. mutable union LengthDelimited length_delimited_;
  248. UnknownFieldSet* group_;
  249. } data_;
  250. };
  251. // ===================================================================
  252. // inline implementations
  253. inline UnknownFieldSet::UnknownFieldSet()
  254. {
  255. }
  256. inline UnknownFieldSet::~UnknownFieldSet()
  257. {
  258. Clear();
  259. }
  260. inline void UnknownFieldSet::ClearAndFreeMemory()
  261. {
  262. Clear();
  263. }
  264. inline void UnknownFieldSet::Clear()
  265. {
  266. if (!fields_.empty())
  267. {
  268. ClearFallback();
  269. }
  270. }
  271. inline bool UnknownFieldSet::empty() const
  272. {
  273. return fields_.empty();
  274. }
  275. inline void UnknownFieldSet::Swap(UnknownFieldSet* x)
  276. {
  277. fields_.swap(x->fields_);
  278. }
  279. inline int UnknownFieldSet::field_count() const
  280. {
  281. return static_cast<int>(fields_.size());
  282. }
  283. inline const UnknownField& UnknownFieldSet::field(int index) const
  284. {
  285. return (fields_)[static_cast<size_t>(index)];
  286. }
  287. inline UnknownField* UnknownFieldSet::mutable_field(int index)
  288. {
  289. return &(fields_)[static_cast<size_t>(index)];
  290. }
  291. inline void UnknownFieldSet::AddLengthDelimited(int number, const std::string& value)
  292. {
  293. AddLengthDelimited(number)->assign(value);
  294. }
  295. inline int UnknownField::number() const
  296. {
  297. return static_cast<int>(number_);
  298. }
  299. inline UnknownField::Type UnknownField::type() const
  300. {
  301. return static_cast<Type>(type_);
  302. }
  303. inline uint64_t UnknownField::varint() const
  304. {
  305. assert(type() == TYPE_VARINT);
  306. return data_.varint_;
  307. }
  308. inline uint32_t UnknownField::fixed32() const
  309. {
  310. assert(type() == TYPE_FIXED32);
  311. return data_.fixed32_;
  312. }
  313. inline uint64_t UnknownField::fixed64() const
  314. {
  315. assert(type() == TYPE_FIXED64);
  316. return data_.fixed64_;
  317. }
  318. inline const std::string& UnknownField::length_delimited() const
  319. {
  320. assert(type() == TYPE_LENGTH_DELIMITED);
  321. return *data_.length_delimited_.string_value;
  322. }
  323. inline const UnknownFieldSet& UnknownField::group() const
  324. {
  325. assert(type() == TYPE_GROUP);
  326. return *data_.group_;
  327. }
  328. inline void UnknownField::set_varint(uint64_t value)
  329. {
  330. assert(type() == TYPE_VARINT);
  331. data_.varint_ = value;
  332. }
  333. inline void UnknownField::set_fixed32(uint32_t value)
  334. {
  335. assert(type() == TYPE_FIXED32);
  336. data_.fixed32_ = value;
  337. }
  338. inline void UnknownField::set_fixed64(uint64_t value)
  339. {
  340. assert(type() == TYPE_FIXED64);
  341. data_.fixed64_ = value;
  342. }
  343. inline void UnknownField::set_length_delimited(const std::string& value)
  344. {
  345. assert(type() == TYPE_LENGTH_DELIMITED);
  346. data_.length_delimited_.string_value->assign(value);
  347. }
  348. inline std::string* UnknownField::mutable_length_delimited()
  349. {
  350. assert(type() == TYPE_LENGTH_DELIMITED);
  351. return data_.length_delimited_.string_value;
  352. }
  353. inline UnknownFieldSet* UnknownField::mutable_group()
  354. {
  355. assert(type() == TYPE_GROUP);
  356. return data_.group_;
  357. }
  358. template<typename MessageType>
  359. bool UnknownFieldSet::MergeFromMessage(const MessageType& message)
  360. {
  361. // SFINAE will route to the right version.
  362. return InternalMergeFromMessage(message);
  363. }
  364. inline size_t UnknownField::GetLengthDelimitedSize() const
  365. {
  366. GOOGLE_DCHECK_EQ(TYPE_LENGTH_DELIMITED, type());
  367. return data_.length_delimited_.string_value->size();
  368. }
  369. inline void UnknownField::SetType(Type type)
  370. {
  371. type_ = type;
  372. }
  373. } // namespace protobuf
  374. } // namespace google
  375. #include <google/protobuf/port_undef.inc>
  376. #endif // GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__