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