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/wire_format_lite.h>
41 #include <google/protobuf/io/coded_stream.h>
42 #include <google/protobuf/io/zero_copy_stream.h>
43 #include <google/protobuf/io/zero_copy_stream_impl.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
MergeToInternalMetadata(const UnknownFieldSet & other,internal::InternalMetadata * metadata)101 void UnknownFieldSet::MergeToInternalMetadata(
102 const UnknownFieldSet& other, internal::InternalMetadata* metadata) {
103 metadata->mutable_unknown_fields<UnknownFieldSet>()->MergeFrom(other);
104 }
105
SpaceUsedExcludingSelfLong() const106 size_t UnknownFieldSet::SpaceUsedExcludingSelfLong() const {
107 if (fields_.empty()) return 0;
108
109 size_t total_size = sizeof(fields_) + sizeof(UnknownField) * fields_.size();
110
111 for (const UnknownField& field : fields_) {
112 switch (field.type()) {
113 case UnknownField::TYPE_LENGTH_DELIMITED:
114 total_size += sizeof(*field.data_.length_delimited_.string_value) +
115 internal::StringSpaceUsedExcludingSelfLong(
116 *field.data_.length_delimited_.string_value);
117 break;
118 case UnknownField::TYPE_GROUP:
119 total_size += field.data_.group_->SpaceUsedLong();
120 break;
121 default:
122 break;
123 }
124 }
125 return total_size;
126 }
127
SpaceUsedLong() const128 size_t UnknownFieldSet::SpaceUsedLong() const {
129 return sizeof(*this) + SpaceUsedExcludingSelf();
130 }
131
AddVarint(int number,uint64 value)132 void UnknownFieldSet::AddVarint(int number, uint64 value) {
133 UnknownField field;
134 field.number_ = number;
135 field.SetType(UnknownField::TYPE_VARINT);
136 field.data_.varint_ = value;
137 fields_.push_back(field);
138 }
139
AddFixed32(int number,uint32 value)140 void UnknownFieldSet::AddFixed32(int number, uint32 value) {
141 UnknownField field;
142 field.number_ = number;
143 field.SetType(UnknownField::TYPE_FIXED32);
144 field.data_.fixed32_ = value;
145 fields_.push_back(field);
146 }
147
AddFixed64(int number,uint64 value)148 void UnknownFieldSet::AddFixed64(int number, uint64 value) {
149 UnknownField field;
150 field.number_ = number;
151 field.SetType(UnknownField::TYPE_FIXED64);
152 field.data_.fixed64_ = value;
153 fields_.push_back(field);
154 }
155
AddLengthDelimited(int number)156 std::string* UnknownFieldSet::AddLengthDelimited(int number) {
157 UnknownField field;
158 field.number_ = number;
159 field.SetType(UnknownField::TYPE_LENGTH_DELIMITED);
160 field.data_.length_delimited_.string_value = new std::string;
161 fields_.push_back(field);
162 return field.data_.length_delimited_.string_value;
163 }
164
165
AddGroup(int number)166 UnknownFieldSet* UnknownFieldSet::AddGroup(int number) {
167 UnknownField field;
168 field.number_ = number;
169 field.SetType(UnknownField::TYPE_GROUP);
170 field.data_.group_ = new UnknownFieldSet;
171 fields_.push_back(field);
172 return field.data_.group_;
173 }
174
AddField(const UnknownField & field)175 void UnknownFieldSet::AddField(const UnknownField& field) {
176 fields_.push_back(field);
177 fields_.back().DeepCopy(field);
178 }
179
DeleteSubrange(int start,int num)180 void UnknownFieldSet::DeleteSubrange(int start, int num) {
181 // Delete the specified fields.
182 for (int i = 0; i < num; ++i) {
183 (fields_)[i + start].Delete();
184 }
185 // Slide down the remaining fields.
186 for (int i = start + num; i < fields_.size(); ++i) {
187 (fields_)[i - num] = (fields_)[i];
188 }
189 // Pop off the # of deleted fields.
190 for (int i = 0; i < num; ++i) {
191 fields_.pop_back();
192 }
193 }
194
DeleteByNumber(int number)195 void UnknownFieldSet::DeleteByNumber(int number) {
196 int left = 0; // The number of fields left after deletion.
197 for (int i = 0; i < fields_.size(); ++i) {
198 UnknownField* field = &(fields_)[i];
199 if (field->number() == number) {
200 field->Delete();
201 } else {
202 if (i != left) {
203 (fields_)[left] = (fields_)[i];
204 }
205 ++left;
206 }
207 }
208 fields_.resize(left);
209 }
210
MergeFromCodedStream(io::CodedInputStream * input)211 bool UnknownFieldSet::MergeFromCodedStream(io::CodedInputStream* input) {
212 UnknownFieldSet other;
213 if (internal::WireFormat::SkipMessage(input, &other) &&
214 input->ConsumedEntireMessage()) {
215 MergeFromAndDestroy(&other);
216 return true;
217 } else {
218 return false;
219 }
220 }
221
ParseFromCodedStream(io::CodedInputStream * input)222 bool UnknownFieldSet::ParseFromCodedStream(io::CodedInputStream* input) {
223 Clear();
224 return MergeFromCodedStream(input);
225 }
226
ParseFromZeroCopyStream(io::ZeroCopyInputStream * input)227 bool UnknownFieldSet::ParseFromZeroCopyStream(io::ZeroCopyInputStream* input) {
228 io::CodedInputStream coded_input(input);
229 return (ParseFromCodedStream(&coded_input) &&
230 coded_input.ConsumedEntireMessage());
231 }
232
ParseFromArray(const void * data,int size)233 bool UnknownFieldSet::ParseFromArray(const void* data, int size) {
234 io::ArrayInputStream input(data, size);
235 return ParseFromZeroCopyStream(&input);
236 }
237
Delete()238 void UnknownField::Delete() {
239 switch (type()) {
240 case UnknownField::TYPE_LENGTH_DELIMITED:
241 delete data_.length_delimited_.string_value;
242 break;
243 case UnknownField::TYPE_GROUP:
244 delete data_.group_;
245 break;
246 default:
247 break;
248 }
249 }
250
DeepCopy(const UnknownField & other)251 void UnknownField::DeepCopy(const UnknownField& other) {
252 switch (type()) {
253 case UnknownField::TYPE_LENGTH_DELIMITED:
254 data_.length_delimited_.string_value =
255 new std::string(*data_.length_delimited_.string_value);
256 break;
257 case UnknownField::TYPE_GROUP: {
258 UnknownFieldSet* group = new UnknownFieldSet();
259 group->InternalMergeFrom(*data_.group_);
260 data_.group_ = group;
261 break;
262 }
263 default:
264 break;
265 }
266 }
267
268
InternalSerializeLengthDelimitedNoTag(uint8 * target,io::EpsCopyOutputStream * stream) const269 uint8* UnknownField::InternalSerializeLengthDelimitedNoTag(
270 uint8* target, io::EpsCopyOutputStream* stream) const {
271 GOOGLE_DCHECK_EQ(TYPE_LENGTH_DELIMITED, type());
272 const std::string& data = *data_.length_delimited_.string_value;
273 target = io::CodedOutputStream::WriteVarint32ToArray(data.size(), target);
274 target = stream->WriteRaw(data.data(), data.size(), target);
275 return target;
276 }
277
278 namespace internal {
279
280 class UnknownFieldParserHelper {
281 public:
UnknownFieldParserHelper(UnknownFieldSet * unknown)282 explicit UnknownFieldParserHelper(UnknownFieldSet* unknown)
283 : unknown_(unknown) {}
284
AddVarint(uint32 num,uint64 value)285 void AddVarint(uint32 num, uint64 value) { unknown_->AddVarint(num, value); }
AddFixed64(uint32 num,uint64 value)286 void AddFixed64(uint32 num, uint64 value) {
287 unknown_->AddFixed64(num, value);
288 }
ParseLengthDelimited(uint32 num,const char * ptr,ParseContext * ctx)289 const char* ParseLengthDelimited(uint32 num, const char* ptr,
290 ParseContext* ctx) {
291 std::string* s = unknown_->AddLengthDelimited(num);
292 int size = ReadSize(&ptr);
293 GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
294 return ctx->ReadString(ptr, size, s);
295 }
ParseGroup(uint32 num,const char * ptr,ParseContext * ctx)296 const char* ParseGroup(uint32 num, const char* ptr, ParseContext* ctx) {
297 UnknownFieldParserHelper child(unknown_->AddGroup(num));
298 return ctx->ParseGroup(&child, ptr, num * 8 + 3);
299 }
AddFixed32(uint32 num,uint32 value)300 void AddFixed32(uint32 num, uint32 value) {
301 unknown_->AddFixed32(num, value);
302 }
303
_InternalParse(const char * ptr,ParseContext * ctx)304 const char* _InternalParse(const char* ptr, ParseContext* ctx) {
305 return WireFormatParser(*this, ptr, ctx);
306 }
307
308 private:
309 UnknownFieldSet* unknown_;
310 };
311
UnknownGroupParse(UnknownFieldSet * unknown,const char * ptr,ParseContext * ctx)312 const char* UnknownGroupParse(UnknownFieldSet* unknown, const char* ptr,
313 ParseContext* ctx) {
314 UnknownFieldParserHelper field_parser(unknown);
315 return WireFormatParser(field_parser, ptr, ctx);
316 }
317
UnknownFieldParse(uint64 tag,UnknownFieldSet * unknown,const char * ptr,ParseContext * ctx)318 const char* UnknownFieldParse(uint64 tag, UnknownFieldSet* unknown,
319 const char* ptr, ParseContext* ctx) {
320 UnknownFieldParserHelper field_parser(unknown);
321 return FieldParser(tag, field_parser, ptr, ctx);
322 }
323
324 } // namespace internal
325 } // namespace protobuf
326 } // namespace google
327