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