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