unknown_field_set.h 14 KB

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