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