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 14 kB

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