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