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

source code of include/google/protobuf/unknown_field_set.h