• 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 #include <google/protobuf/unknown_field_set.h>
36 
37 #include <google/protobuf/stubs/logging.h>
38 #include <google/protobuf/stubs/common.h>
39 #include <google/protobuf/parse_context.h>
40 #include <google/protobuf/io/coded_stream.h>
41 #include <google/protobuf/io/zero_copy_stream.h>
42 #include <google/protobuf/io/zero_copy_stream_impl.h>
43 #include <google/protobuf/metadata.h>
44 #include <google/protobuf/wire_format.h>
45 #include <google/protobuf/stubs/stl_util.h>
46 
47 #include <google/protobuf/port_def.inc>
48 
49 namespace google {
50 namespace protobuf {
51 
default_instance()52 const UnknownFieldSet* UnknownFieldSet::default_instance() {
53   static auto instance = internal::OnShutdownDelete(new UnknownFieldSet());
54   return instance;
55 }
56 
ClearFallback()57 void UnknownFieldSet::ClearFallback() {
58   GOOGLE_DCHECK(!fields_.empty());
59   int n = fields_.size();
60   do {
61     (fields_)[--n].Delete();
62   } while (n > 0);
63   fields_.clear();
64 }
65 
InternalMergeFrom(const UnknownFieldSet & other)66 void UnknownFieldSet::InternalMergeFrom(const UnknownFieldSet& other) {
67   int other_field_count = other.field_count();
68   if (other_field_count > 0) {
69     fields_.reserve(fields_.size() + other_field_count);
70     for (int i = 0; i < other_field_count; i++) {
71       fields_.push_back((other.fields_)[i]);
72       fields_.back().DeepCopy((other.fields_)[i]);
73     }
74   }
75 }
76 
MergeFrom(const UnknownFieldSet & other)77 void UnknownFieldSet::MergeFrom(const UnknownFieldSet& other) {
78   int other_field_count = other.field_count();
79   if (other_field_count > 0) {
80     fields_.reserve(fields_.size() + other_field_count);
81     for (int i = 0; i < other_field_count; i++) {
82       fields_.push_back((other.fields_)[i]);
83       fields_.back().DeepCopy((other.fields_)[i]);
84     }
85   }
86 }
87 
88 // A specialized MergeFrom for performance when we are merging from an UFS that
89 // is temporary and can be destroyed in the process.
MergeFromAndDestroy(UnknownFieldSet * other)90 void UnknownFieldSet::MergeFromAndDestroy(UnknownFieldSet* other) {
91   if (fields_.empty()) {
92     fields_ = std::move(other->fields_);
93   } else {
94     fields_.insert(fields_.end(),
95                    std::make_move_iterator(other->fields_.begin()),
96                    std::make_move_iterator(other->fields_.end()));
97   }
98   other->fields_.clear();
99 }
100 
MergeToInternalMetdata(const UnknownFieldSet & other,internal::InternalMetadataWithArena * metadata)101 void UnknownFieldSet::MergeToInternalMetdata(
102     const UnknownFieldSet& other,
103     internal::InternalMetadataWithArena* metadata) {
104   metadata->mutable_unknown_fields()->MergeFrom(other);
105 }
106 
SpaceUsedExcludingSelfLong() const107 size_t UnknownFieldSet::SpaceUsedExcludingSelfLong() const {
108   if (fields_.empty()) return 0;
109 
110   size_t total_size = sizeof(fields_) + sizeof(UnknownField) * fields_.size();
111 
112   for (int i = 0; i < fields_.size(); i++) {
113     const UnknownField& field = (fields_)[i];
114     switch (field.type()) {
115       case UnknownField::TYPE_LENGTH_DELIMITED:
116         total_size += sizeof(*field.data_.length_delimited_.string_value) +
117                       internal::StringSpaceUsedExcludingSelfLong(
118                           *field.data_.length_delimited_.string_value);
119         break;
120       case UnknownField::TYPE_GROUP:
121         total_size += field.data_.group_->SpaceUsedLong();
122         break;
123       default:
124         break;
125     }
126   }
127   return total_size;
128 }
129 
SpaceUsedLong() const130 size_t UnknownFieldSet::SpaceUsedLong() const {
131   return sizeof(*this) + SpaceUsedExcludingSelf();
132 }
133 
AddVarint(int number,uint64 value)134 void UnknownFieldSet::AddVarint(int number, uint64 value) {
135   UnknownField field;
136   field.number_ = number;
137   field.SetType(UnknownField::TYPE_VARINT);
138   field.data_.varint_ = value;
139   fields_.push_back(field);
140 }
141 
AddFixed32(int number,uint32 value)142 void UnknownFieldSet::AddFixed32(int number, uint32 value) {
143   UnknownField field;
144   field.number_ = number;
145   field.SetType(UnknownField::TYPE_FIXED32);
146   field.data_.fixed32_ = value;
147   fields_.push_back(field);
148 }
149 
AddFixed64(int number,uint64 value)150 void UnknownFieldSet::AddFixed64(int number, uint64 value) {
151   UnknownField field;
152   field.number_ = number;
153   field.SetType(UnknownField::TYPE_FIXED64);
154   field.data_.fixed64_ = value;
155   fields_.push_back(field);
156 }
157 
AddLengthDelimited(int number)158 std::string* UnknownFieldSet::AddLengthDelimited(int number) {
159   UnknownField field;
160   field.number_ = number;
161   field.SetType(UnknownField::TYPE_LENGTH_DELIMITED);
162   field.data_.length_delimited_.string_value = new std::string;
163   fields_.push_back(field);
164   return field.data_.length_delimited_.string_value;
165 }
166 
167 
AddGroup(int number)168 UnknownFieldSet* UnknownFieldSet::AddGroup(int number) {
169   UnknownField field;
170   field.number_ = number;
171   field.SetType(UnknownField::TYPE_GROUP);
172   field.data_.group_ = new UnknownFieldSet;
173   fields_.push_back(field);
174   return field.data_.group_;
175 }
176 
AddField(const UnknownField & field)177 void UnknownFieldSet::AddField(const UnknownField& field) {
178   fields_.push_back(field);
179   fields_.back().DeepCopy(field);
180 }
181 
DeleteSubrange(int start,int num)182 void UnknownFieldSet::DeleteSubrange(int start, int num) {
183   // Delete the specified fields.
184   for (int i = 0; i < num; ++i) {
185     (fields_)[i + start].Delete();
186   }
187   // Slide down the remaining fields.
188   for (int i = start + num; i < fields_.size(); ++i) {
189     (fields_)[i - num] = (fields_)[i];
190   }
191   // Pop off the # of deleted fields.
192   for (int i = 0; i < num; ++i) {
193     fields_.pop_back();
194   }
195 }
196 
DeleteByNumber(int number)197 void UnknownFieldSet::DeleteByNumber(int number) {
198   int left = 0;  // The number of fields left after deletion.
199   for (int i = 0; i < fields_.size(); ++i) {
200     UnknownField* field = &(fields_)[i];
201     if (field->number() == number) {
202       field->Delete();
203     } else {
204       if (i != left) {
205         (fields_)[left] = (fields_)[i];
206       }
207       ++left;
208     }
209   }
210   fields_.resize(left);
211 }
212 
MergeFromCodedStream(io::CodedInputStream * input)213 bool UnknownFieldSet::MergeFromCodedStream(io::CodedInputStream* input) {
214   UnknownFieldSet other;
215   if (internal::WireFormat::SkipMessage(input, &other) &&
216       input->ConsumedEntireMessage()) {
217     MergeFromAndDestroy(&other);
218     return true;
219   } else {
220     return false;
221   }
222 }
223 
ParseFromCodedStream(io::CodedInputStream * input)224 bool UnknownFieldSet::ParseFromCodedStream(io::CodedInputStream* input) {
225   Clear();
226   return MergeFromCodedStream(input);
227 }
228 
ParseFromZeroCopyStream(io::ZeroCopyInputStream * input)229 bool UnknownFieldSet::ParseFromZeroCopyStream(io::ZeroCopyInputStream* input) {
230   io::CodedInputStream coded_input(input);
231   return (ParseFromCodedStream(&coded_input) &&
232           coded_input.ConsumedEntireMessage());
233 }
234 
ParseFromArray(const void * data,int size)235 bool UnknownFieldSet::ParseFromArray(const void* data, int size) {
236   io::ArrayInputStream input(data, size);
237   return ParseFromZeroCopyStream(&input);
238 }
239 
Delete()240 void UnknownField::Delete() {
241   switch (type()) {
242     case UnknownField::TYPE_LENGTH_DELIMITED:
243       delete data_.length_delimited_.string_value;
244       break;
245     case UnknownField::TYPE_GROUP:
246       delete data_.group_;
247       break;
248     default:
249       break;
250   }
251 }
252 
DeepCopy(const UnknownField & other)253 void UnknownField::DeepCopy(const UnknownField& other) {
254   switch (type()) {
255     case UnknownField::TYPE_LENGTH_DELIMITED:
256       data_.length_delimited_.string_value =
257           new std::string(*data_.length_delimited_.string_value);
258       break;
259     case UnknownField::TYPE_GROUP: {
260       UnknownFieldSet* group = new UnknownFieldSet();
261       group->InternalMergeFrom(*data_.group_);
262       data_.group_ = group;
263       break;
264     }
265     default:
266       break;
267   }
268 }
269 
270 
SerializeLengthDelimitedNoTag(io::CodedOutputStream * output) const271 void UnknownField::SerializeLengthDelimitedNoTag(
272     io::CodedOutputStream* output) const {
273   GOOGLE_DCHECK_EQ(TYPE_LENGTH_DELIMITED, type());
274   const std::string& data = *data_.length_delimited_.string_value;
275   output->WriteVarint32(data.size());
276   output->WriteRawMaybeAliased(data.data(), data.size());
277 }
278 
SerializeLengthDelimitedNoTagToArray(uint8 * target) const279 uint8* UnknownField::SerializeLengthDelimitedNoTagToArray(uint8* target) const {
280   GOOGLE_DCHECK_EQ(TYPE_LENGTH_DELIMITED, type());
281   const std::string& data = *data_.length_delimited_.string_value;
282   target = io::CodedOutputStream::WriteVarint32ToArray(data.size(), target);
283   target = io::CodedOutputStream::WriteStringToArray(data, target);
284   return target;
285 }
286 
287 #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
288 namespace internal {
PackedEnumParser(void * object,const char * ptr,ParseContext * ctx,bool (* is_valid)(int),InternalMetadataWithArena * metadata,int field_num)289 const char* PackedEnumParser(void* object, const char* ptr, ParseContext* ctx,
290                              bool (*is_valid)(int),
291                              InternalMetadataWithArena* metadata,
292                              int field_num) {
293   return ctx->ReadPackedVarint(
294       ptr, [object, is_valid, metadata, field_num](uint64 val) {
295         if (is_valid(val)) {
296           static_cast<RepeatedField<int>*>(object)->Add(val);
297         } else {
298           WriteVarint(field_num, val, metadata->mutable_unknown_fields());
299         }
300       });
301 }
PackedEnumParserArg(void * object,const char * ptr,ParseContext * ctx,bool (* is_valid)(const void *,int),const void * data,InternalMetadataWithArena * metadata,int field_num)302 const char* PackedEnumParserArg(void* object, const char* ptr,
303                                 ParseContext* ctx,
304                                 bool (*is_valid)(const void*, int),
305                                 const void* data,
306                                 InternalMetadataWithArena* metadata,
307                                 int field_num) {
308   return ctx->ReadPackedVarint(
309       ptr, [object, is_valid, data, metadata, field_num](uint64 val) {
310         if (is_valid(data, val)) {
311           static_cast<RepeatedField<int>*>(object)->Add(val);
312         } else {
313           WriteVarint(field_num, val, metadata->mutable_unknown_fields());
314         }
315       });
316 }
317 
318 class UnknownFieldParserHelper {
319  public:
UnknownFieldParserHelper(UnknownFieldSet * unknown)320   explicit UnknownFieldParserHelper(UnknownFieldSet* unknown)
321       : unknown_(unknown) {}
322 
AddVarint(uint32 num,uint64 value)323   void AddVarint(uint32 num, uint64 value) { unknown_->AddVarint(num, value); }
AddFixed64(uint32 num,uint64 value)324   void AddFixed64(uint32 num, uint64 value) {
325     unknown_->AddFixed64(num, value);
326   }
ParseLengthDelimited(uint32 num,const char * ptr,ParseContext * ctx)327   const char* ParseLengthDelimited(uint32 num, const char* ptr,
328                                    ParseContext* ctx) {
329     std::string* s = unknown_->AddLengthDelimited(num);
330     int size = ReadSize(&ptr);
331     GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
332     return ctx->ReadString(ptr, size, s);
333   }
ParseGroup(uint32 num,const char * ptr,ParseContext * ctx)334   const char* ParseGroup(uint32 num, const char* ptr, ParseContext* ctx) {
335     UnknownFieldParserHelper child(unknown_->AddGroup(num));
336     return ctx->ParseGroup(&child, ptr, num * 8 + 3);
337   }
AddFixed32(uint32 num,uint32 value)338   void AddFixed32(uint32 num, uint32 value) {
339     unknown_->AddFixed32(num, value);
340   }
341 
_InternalParse(const char * ptr,ParseContext * ctx)342   const char* _InternalParse(const char* ptr, ParseContext* ctx) {
343     return WireFormatParser(*this, ptr, ctx);
344   }
345 
346  private:
347   UnknownFieldSet* unknown_;
348 };
349 
UnknownGroupParse(UnknownFieldSet * unknown,const char * ptr,ParseContext * ctx)350 const char* UnknownGroupParse(UnknownFieldSet* unknown, const char* ptr,
351                               ParseContext* ctx) {
352   UnknownFieldParserHelper field_parser(unknown);
353   return WireFormatParser(field_parser, ptr, ctx);
354 }
355 
UnknownFieldParse(uint64 tag,UnknownFieldSet * unknown,const char * ptr,ParseContext * ctx)356 const char* UnknownFieldParse(uint64 tag, UnknownFieldSet* unknown,
357                               const char* ptr, ParseContext* ctx) {
358   UnknownFieldParserHelper field_parser(unknown);
359   return FieldParser(tag, field_parser, ptr, ctx);
360 }
361 
UnknownFieldParse(uint32 tag,InternalMetadataWithArena * metadata,const char * ptr,ParseContext * ctx)362 const char* UnknownFieldParse(uint32 tag, InternalMetadataWithArena* metadata,
363                               const char* ptr, ParseContext* ctx) {
364   return UnknownFieldParse(tag, metadata->mutable_unknown_fields(), ptr, ctx);
365 }
366 
367 }  // namespace internal
368 #endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
369 
370 }  // namespace protobuf
371 }  // namespace google
372