Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: third_party/protobuf/src/google/protobuf/wire_format.cc

Issue 1842653006: Update //third_party/protobuf to version 3. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update sync unittest and README.chromium Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above 11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer 12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the 13 // in the documentation and/or other materials provided with the
(...skipping 17 matching lines...) Expand all
31 // Author: kenton@google.com (Kenton Varda) 31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by 32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others. 33 // Sanjay Ghemawat, Jeff Dean, and others.
34 34
35 #include <stack> 35 #include <stack>
36 #include <string> 36 #include <string>
37 #include <vector> 37 #include <vector>
38 38
39 #include <google/protobuf/wire_format.h> 39 #include <google/protobuf/wire_format.h>
40 40
41 #include <google/protobuf/stubs/logging.h>
41 #include <google/protobuf/stubs/common.h> 42 #include <google/protobuf/stubs/common.h>
43 #include <google/protobuf/stubs/stringprintf.h>
42 #include <google/protobuf/descriptor.h> 44 #include <google/protobuf/descriptor.h>
43 #include <google/protobuf/wire_format_lite_inl.h> 45 #include <google/protobuf/wire_format_lite_inl.h>
44 #include <google/protobuf/descriptor.pb.h> 46 #include <google/protobuf/descriptor.pb.h>
45 #include <google/protobuf/io/coded_stream.h> 47 #include <google/protobuf/io/coded_stream.h>
46 #include <google/protobuf/io/zero_copy_stream.h> 48 #include <google/protobuf/io/zero_copy_stream.h>
47 #include <google/protobuf/io/zero_copy_stream_impl.h> 49 #include <google/protobuf/io/zero_copy_stream_impl.h>
48 #include <google/protobuf/unknown_field_set.h> 50 #include <google/protobuf/unknown_field_set.h>
49 51
50 52
51 53
52 namespace google { 54 namespace google {
53 namespace protobuf { 55 namespace protobuf {
54 namespace internal { 56 namespace internal {
55 57
58 // ===================================================================
59
60 bool UnknownFieldSetFieldSkipper::SkipField(
61 io::CodedInputStream* input, uint32 tag) {
62 return WireFormat::SkipField(input, tag, unknown_fields_);
63 }
64
65 bool UnknownFieldSetFieldSkipper::SkipMessage(io::CodedInputStream* input) {
66 return WireFormat::SkipMessage(input, unknown_fields_);
67 }
68
69 void UnknownFieldSetFieldSkipper::SkipUnknownEnum(
70 int field_number, int value) {
71 unknown_fields_->AddVarint(field_number, value);
72 }
73
74 bool WireFormat::SkipField(io::CodedInputStream* input, uint32 tag,
75 UnknownFieldSet* unknown_fields) {
76 int number = WireFormatLite::GetTagFieldNumber(tag);
77
78 switch (WireFormatLite::GetTagWireType(tag)) {
79 case WireFormatLite::WIRETYPE_VARINT: {
80 uint64 value;
81 if (!input->ReadVarint64(&value)) return false;
82 if (unknown_fields != NULL) unknown_fields->AddVarint(number, value);
83 return true;
84 }
85 case WireFormatLite::WIRETYPE_FIXED64: {
86 uint64 value;
87 if (!input->ReadLittleEndian64(&value)) return false;
88 if (unknown_fields != NULL) unknown_fields->AddFixed64(number, value);
89 return true;
90 }
91 case WireFormatLite::WIRETYPE_LENGTH_DELIMITED: {
92 uint32 length;
93 if (!input->ReadVarint32(&length)) return false;
94 if (unknown_fields == NULL) {
95 if (!input->Skip(length)) return false;
96 } else {
97 if (!input->ReadString(unknown_fields->AddLengthDelimited(number),
98 length)) {
99 return false;
100 }
101 }
102 return true;
103 }
104 case WireFormatLite::WIRETYPE_START_GROUP: {
105 if (!input->IncrementRecursionDepth()) return false;
106 if (!SkipMessage(input, (unknown_fields == NULL) ?
107 NULL : unknown_fields->AddGroup(number))) {
108 return false;
109 }
110 input->DecrementRecursionDepth();
111 // Check that the ending tag matched the starting tag.
112 if (!input->LastTagWas(WireFormatLite::MakeTag(
113 WireFormatLite::GetTagFieldNumber(tag),
114 WireFormatLite::WIRETYPE_END_GROUP))) {
115 return false;
116 }
117 return true;
118 }
119 case WireFormatLite::WIRETYPE_END_GROUP: {
120 return false;
121 }
122 case WireFormatLite::WIRETYPE_FIXED32: {
123 uint32 value;
124 if (!input->ReadLittleEndian32(&value)) return false;
125 if (unknown_fields != NULL) unknown_fields->AddFixed32(number, value);
126 return true;
127 }
128 default: {
129 return false;
130 }
131 }
132 }
133
134 bool WireFormat::SkipMessage(io::CodedInputStream* input,
135 UnknownFieldSet* unknown_fields) {
136 while (true) {
137 uint32 tag = input->ReadTag();
138 if (tag == 0) {
139 // End of input. This is a valid place to end, so return true.
140 return true;
141 }
142
143 WireFormatLite::WireType wire_type = WireFormatLite::GetTagWireType(tag);
144
145 if (wire_type == WireFormatLite::WIRETYPE_END_GROUP) {
146 // Must be the end of the message.
147 return true;
148 }
149
150 if (!SkipField(input, tag, unknown_fields)) return false;
151 }
152 }
153
154 bool WireFormat::ReadPackedEnumPreserveUnknowns(io::CodedInputStream* input,
155 uint32 field_number,
156 bool (*is_valid)(int),
157 UnknownFieldSet* unknown_fields,
158 RepeatedField<int>* values) {
159 uint32 length;
160 if (!input->ReadVarint32(&length)) return false;
161 io::CodedInputStream::Limit limit = input->PushLimit(length);
162 while (input->BytesUntilLimit() > 0) {
163 int value;
164 if (!google::protobuf::internal::WireFormatLite::ReadPrimitive<
165 int, WireFormatLite::TYPE_ENUM>(input, &value)) {
166 return false;
167 }
168 if (is_valid == NULL || is_valid(value)) {
169 values->Add(value);
170 } else {
171 unknown_fields->AddVarint(field_number, value);
172 }
173 }
174 input->PopLimit(limit);
175 return true;
176 }
177
178
179 void WireFormat::SerializeUnknownFields(const UnknownFieldSet& unknown_fields,
180 io::CodedOutputStream* output) {
181 for (int i = 0; i < unknown_fields.field_count(); i++) {
182 const UnknownField& field = unknown_fields.field(i);
183 switch (field.type()) {
184 case UnknownField::TYPE_VARINT:
185 output->WriteVarint32(WireFormatLite::MakeTag(field.number(),
186 WireFormatLite::WIRETYPE_VARINT));
187 output->WriteVarint64(field.varint());
188 break;
189 case UnknownField::TYPE_FIXED32:
190 output->WriteVarint32(WireFormatLite::MakeTag(field.number(),
191 WireFormatLite::WIRETYPE_FIXED32));
192 output->WriteLittleEndian32(field.fixed32());
193 break;
194 case UnknownField::TYPE_FIXED64:
195 output->WriteVarint32(WireFormatLite::MakeTag(field.number(),
196 WireFormatLite::WIRETYPE_FIXED64));
197 output->WriteLittleEndian64(field.fixed64());
198 break;
199 case UnknownField::TYPE_LENGTH_DELIMITED:
200 output->WriteVarint32(WireFormatLite::MakeTag(field.number(),
201 WireFormatLite::WIRETYPE_LENGTH_DELIMITED));
202 output->WriteVarint32(field.length_delimited().size());
203 output->WriteRawMaybeAliased(field.length_delimited().data(),
204 field.length_delimited().size());
205 break;
206 case UnknownField::TYPE_GROUP:
207 output->WriteVarint32(WireFormatLite::MakeTag(field.number(),
208 WireFormatLite::WIRETYPE_START_GROUP));
209 SerializeUnknownFields(field.group(), output);
210 output->WriteVarint32(WireFormatLite::MakeTag(field.number(),
211 WireFormatLite::WIRETYPE_END_GROUP));
212 break;
213 }
214 }
215 }
216
217 uint8* WireFormat::SerializeUnknownFieldsToArray(
218 const UnknownFieldSet& unknown_fields,
219 uint8* target) {
220 for (int i = 0; i < unknown_fields.field_count(); i++) {
221 const UnknownField& field = unknown_fields.field(i);
222
223 switch (field.type()) {
224 case UnknownField::TYPE_VARINT:
225 target = WireFormatLite::WriteInt64ToArray(
226 field.number(), field.varint(), target);
227 break;
228 case UnknownField::TYPE_FIXED32:
229 target = WireFormatLite::WriteFixed32ToArray(
230 field.number(), field.fixed32(), target);
231 break;
232 case UnknownField::TYPE_FIXED64:
233 target = WireFormatLite::WriteFixed64ToArray(
234 field.number(), field.fixed64(), target);
235 break;
236 case UnknownField::TYPE_LENGTH_DELIMITED:
237 target = WireFormatLite::WriteBytesToArray(
238 field.number(), field.length_delimited(), target);
239 break;
240 case UnknownField::TYPE_GROUP:
241 target = WireFormatLite::WriteTagToArray(
242 field.number(), WireFormatLite::WIRETYPE_START_GROUP, target);
243 target = SerializeUnknownFieldsToArray(field.group(), target);
244 target = WireFormatLite::WriteTagToArray(
245 field.number(), WireFormatLite::WIRETYPE_END_GROUP, target);
246 break;
247 }
248 }
249 return target;
250 }
251
252 void WireFormat::SerializeUnknownMessageSetItems(
253 const UnknownFieldSet& unknown_fields,
254 io::CodedOutputStream* output) {
255 for (int i = 0; i < unknown_fields.field_count(); i++) {
256 const UnknownField& field = unknown_fields.field(i);
257 // The only unknown fields that are allowed to exist in a MessageSet are
258 // messages, which are length-delimited.
259 if (field.type() == UnknownField::TYPE_LENGTH_DELIMITED) {
260 // Start group.
261 output->WriteVarint32(WireFormatLite::kMessageSetItemStartTag);
262
263 // Write type ID.
264 output->WriteVarint32(WireFormatLite::kMessageSetTypeIdTag);
265 output->WriteVarint32(field.number());
266
267 // Write message.
268 output->WriteVarint32(WireFormatLite::kMessageSetMessageTag);
269 field.SerializeLengthDelimitedNoTag(output);
270
271 // End group.
272 output->WriteVarint32(WireFormatLite::kMessageSetItemEndTag);
273 }
274 }
275 }
276
277 uint8* WireFormat::SerializeUnknownMessageSetItemsToArray(
278 const UnknownFieldSet& unknown_fields,
279 uint8* target) {
280 for (int i = 0; i < unknown_fields.field_count(); i++) {
281 const UnknownField& field = unknown_fields.field(i);
282
283 // The only unknown fields that are allowed to exist in a MessageSet are
284 // messages, which are length-delimited.
285 if (field.type() == UnknownField::TYPE_LENGTH_DELIMITED) {
286 // Start group.
287 target = io::CodedOutputStream::WriteTagToArray(
288 WireFormatLite::kMessageSetItemStartTag, target);
289
290 // Write type ID.
291 target = io::CodedOutputStream::WriteTagToArray(
292 WireFormatLite::kMessageSetTypeIdTag, target);
293 target = io::CodedOutputStream::WriteVarint32ToArray(
294 field.number(), target);
295
296 // Write message.
297 target = io::CodedOutputStream::WriteTagToArray(
298 WireFormatLite::kMessageSetMessageTag, target);
299 target = field.SerializeLengthDelimitedNoTagToArray(target);
300
301 // End group.
302 target = io::CodedOutputStream::WriteTagToArray(
303 WireFormatLite::kMessageSetItemEndTag, target);
304 }
305 }
306
307 return target;
308 }
309
310 int WireFormat::ComputeUnknownFieldsSize(
311 const UnknownFieldSet& unknown_fields) {
312 int size = 0;
313 for (int i = 0; i < unknown_fields.field_count(); i++) {
314 const UnknownField& field = unknown_fields.field(i);
315
316 switch (field.type()) {
317 case UnknownField::TYPE_VARINT:
318 size += io::CodedOutputStream::VarintSize32(
319 WireFormatLite::MakeTag(field.number(),
320 WireFormatLite::WIRETYPE_VARINT));
321 size += io::CodedOutputStream::VarintSize64(field.varint());
322 break;
323 case UnknownField::TYPE_FIXED32:
324 size += io::CodedOutputStream::VarintSize32(
325 WireFormatLite::MakeTag(field.number(),
326 WireFormatLite::WIRETYPE_FIXED32));
327 size += sizeof(int32);
328 break;
329 case UnknownField::TYPE_FIXED64:
330 size += io::CodedOutputStream::VarintSize32(
331 WireFormatLite::MakeTag(field.number(),
332 WireFormatLite::WIRETYPE_FIXED64));
333 size += sizeof(int64);
334 break;
335 case UnknownField::TYPE_LENGTH_DELIMITED:
336 size += io::CodedOutputStream::VarintSize32(
337 WireFormatLite::MakeTag(field.number(),
338 WireFormatLite::WIRETYPE_LENGTH_DELIMITED));
339 size += io::CodedOutputStream::VarintSize32(
340 field.length_delimited().size());
341 size += field.length_delimited().size();
342 break;
343 case UnknownField::TYPE_GROUP:
344 size += io::CodedOutputStream::VarintSize32(
345 WireFormatLite::MakeTag(field.number(),
346 WireFormatLite::WIRETYPE_START_GROUP));
347 size += ComputeUnknownFieldsSize(field.group());
348 size += io::CodedOutputStream::VarintSize32(
349 WireFormatLite::MakeTag(field.number(),
350 WireFormatLite::WIRETYPE_END_GROUP));
351 break;
352 }
353 }
354
355 return size;
356 }
357
358 int WireFormat::ComputeUnknownMessageSetItemsSize(
359 const UnknownFieldSet& unknown_fields) {
360 int size = 0;
361 for (int i = 0; i < unknown_fields.field_count(); i++) {
362 const UnknownField& field = unknown_fields.field(i);
363
364 // The only unknown fields that are allowed to exist in a MessageSet are
365 // messages, which are length-delimited.
366 if (field.type() == UnknownField::TYPE_LENGTH_DELIMITED) {
367 size += WireFormatLite::kMessageSetItemTagsSize;
368 size += io::CodedOutputStream::VarintSize32(field.number());
369
370 int field_size = field.GetLengthDelimitedSize();
371 size += io::CodedOutputStream::VarintSize32(field_size);
372 size += field_size;
373 }
374 }
375
376 return size;
377 }
378
379 // ===================================================================
380
56 bool WireFormat::ParseAndMergePartial(io::CodedInputStream* input, 381 bool WireFormat::ParseAndMergePartial(io::CodedInputStream* input,
57 Message* message) { 382 Message* message) {
58 const Descriptor* descriptor = message->GetDescriptor(); 383 const Descriptor* descriptor = message->GetDescriptor();
59 const Reflection* message_reflection = message->GetReflection(); 384 const Reflection* message_reflection = message->GetReflection();
60 385
61 while(true) { 386 while(true) {
62 uint32 tag = input->ReadTag(); 387 uint32 tag = input->ReadTag();
63 if (tag == 0) { 388 if (tag == 0) {
64 // End of input. This is a valid place to end, so return true. 389 // End of input. This is a valid place to end, so return true.
65 return true; 390 return true;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 continue; // Skip ParseAndMergeField(); already taken care of. 423 continue; // Skip ParseAndMergeField(); already taken care of.
99 } 424 }
100 } 425 }
101 426
102 if (!ParseAndMergeField(tag, field, message, input)) { 427 if (!ParseAndMergeField(tag, field, message, input)) {
103 return false; 428 return false;
104 } 429 }
105 } 430 }
106 } 431 }
107 432
433 bool WireFormat::SkipMessageSetField(io::CodedInputStream* input,
434 uint32 field_number,
435 UnknownFieldSet* unknown_fields) {
436 uint32 length;
437 if (!input->ReadVarint32(&length)) return false;
438 return input->ReadString(
439 unknown_fields->AddLengthDelimited(field_number), length);
440 }
441
442 bool WireFormat::ParseAndMergeMessageSetField(uint32 field_number,
443 const FieldDescriptor* field,
444 Message* message,
445 io::CodedInputStream* input) {
446 const Reflection* message_reflection = message->GetReflection();
447 if (field == NULL) {
448 // We store unknown MessageSet extensions as groups.
449 return SkipMessageSetField(
450 input, field_number, message_reflection->MutableUnknownFields(message));
451 } else if (field->is_repeated() ||
452 field->type() != FieldDescriptor::TYPE_MESSAGE) {
453 // This shouldn't happen as we only allow optional message extensions to
454 // MessageSet.
455 GOOGLE_LOG(ERROR) << "Extensions of MessageSets must be optional messages.";
456 return false;
457 } else {
458 Message* sub_message = message_reflection->MutableMessage(
459 message, field, input->GetExtensionFactory());
460 return WireFormatLite::ReadMessage(input, sub_message);
461 }
462 }
463
464 static bool StrictUtf8Check(const FieldDescriptor* field) {
465 return field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3;
466 }
467
108 bool WireFormat::ParseAndMergeField( 468 bool WireFormat::ParseAndMergeField(
109 uint32 tag, 469 uint32 tag,
110 const FieldDescriptor* field, // May be NULL for unknown 470 const FieldDescriptor* field, // May be NULL for unknown
111 Message* message, 471 Message* message,
112 io::CodedInputStream* input) { 472 io::CodedInputStream* input) {
113 const Reflection* message_reflection = message->GetReflection(); 473 const Reflection* message_reflection = message->GetReflection();
114 474
115 enum { UNKNOWN, NORMAL_FORMAT, PACKED_FORMAT } value_format; 475 enum { UNKNOWN, NORMAL_FORMAT, PACKED_FORMAT } value_format;
116 476
117 if (field == NULL) { 477 if (field == NULL) {
118 value_format = UNKNOWN; 478 value_format = UNKNOWN;
119 } else if (WireFormatLite::GetTagWireType(tag) == 479 } else if (WireFormatLite::GetTagWireType(tag) ==
120 WireTypeForFieldType(field->type())) { 480 WireTypeForFieldType(field->type())) {
121 value_format = NORMAL_FORMAT; 481 value_format = NORMAL_FORMAT;
122 } else if (field->is_packable() && 482 } else if (field->is_packable() &&
123 WireFormatLite::GetTagWireType(tag) == 483 WireFormatLite::GetTagWireType(tag) ==
124 WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { 484 WireFormatLite::WIRETYPE_LENGTH_DELIMITED) {
125 value_format = PACKED_FORMAT; 485 value_format = PACKED_FORMAT;
126 } else { 486 } else {
127 // We don't recognize this field. Either the field number is unknown 487 // We don't recognize this field. Either the field number is unknown
128 // or the wire type doesn't match. Put it in our unknown field set. 488 // or the wire type doesn't match. Put it in our unknown field set.
129 value_format = UNKNOWN; 489 value_format = UNKNOWN;
130 } 490 }
131 491
132 if (value_format == UNKNOWN) { 492 if (value_format == UNKNOWN) {
133 return WireFormatLite::SkipField( 493 return SkipField(input, tag,
134 input, tag, message_reflection->MutableUnknownFields(message)); 494 message_reflection->MutableUnknownFields(message));
135 } else if (value_format == PACKED_FORMAT) { 495 } else if (value_format == PACKED_FORMAT) {
136 uint32 length; 496 uint32 length;
137 if (!input->ReadVarint32(&length)) return false; 497 if (!input->ReadVarint32(&length)) return false;
138 io::CodedInputStream::Limit limit = input->PushLimit(length); 498 io::CodedInputStream::Limit limit = input->PushLimit(length);
139 499
140 switch (field->type()) { 500 switch (field->type()) {
141 #define HANDLE_PACKED_TYPE(TYPE, CPPTYPE, CPPTYPE_METHOD) \ 501 #define HANDLE_PACKED_TYPE(TYPE, CPPTYPE, CPPTYPE_METHOD) \
142 case FieldDescriptor::TYPE_##TYPE: { \ 502 case FieldDescriptor::TYPE_##TYPE: { \
143 while (input->BytesUntilLimit() > 0) { \ 503 while (input->BytesUntilLimit() > 0) { \
144 CPPTYPE value; \ 504 CPPTYPE value; \
(...skipping 21 matching lines...) Expand all
166 HANDLE_PACKED_TYPE(DOUBLE, double, Double) 526 HANDLE_PACKED_TYPE(DOUBLE, double, Double)
167 527
168 HANDLE_PACKED_TYPE(BOOL, bool, Bool) 528 HANDLE_PACKED_TYPE(BOOL, bool, Bool)
169 #undef HANDLE_PACKED_TYPE 529 #undef HANDLE_PACKED_TYPE
170 530
171 case FieldDescriptor::TYPE_ENUM: { 531 case FieldDescriptor::TYPE_ENUM: {
172 while (input->BytesUntilLimit() > 0) { 532 while (input->BytesUntilLimit() > 0) {
173 int value; 533 int value;
174 if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>( 534 if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
175 input, &value)) return false; 535 input, &value)) return false;
176 const EnumValueDescriptor* enum_value = 536 if (message->GetDescriptor()->file()->syntax() ==
177 field->enum_type()->FindValueByNumber(value); 537 FileDescriptor::SYNTAX_PROTO3) {
178 if (enum_value != NULL) { 538 message_reflection->AddEnumValue(message, field, value);
179 message_reflection->AddEnum(message, field, enum_value); 539 } else {
540 const EnumValueDescriptor* enum_value =
541 field->enum_type()->FindValueByNumber(value);
542 if (enum_value != NULL) {
543 message_reflection->AddEnum(message, field, enum_value);
544 } else {
545 // The enum value is not one of the known values. Add it to the
546 // UnknownFieldSet.
547 int64 sign_extended_value = static_cast<int64>(value);
548 message_reflection->MutableUnknownFields(message)
549 ->AddVarint(
550 WireFormatLite::GetTagFieldNumber(tag),
551 sign_extended_value);
552 }
180 } 553 }
181 } 554 }
182 555
183 break; 556 break;
184 } 557 }
185 558
186 case FieldDescriptor::TYPE_STRING: 559 case FieldDescriptor::TYPE_STRING:
187 case FieldDescriptor::TYPE_GROUP: 560 case FieldDescriptor::TYPE_GROUP:
188 case FieldDescriptor::TYPE_MESSAGE: 561 case FieldDescriptor::TYPE_MESSAGE:
189 case FieldDescriptor::TYPE_BYTES: 562 case FieldDescriptor::TYPE_BYTES:
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 HANDLE_TYPE(FLOAT , float , Float ) 599 HANDLE_TYPE(FLOAT , float , Float )
227 HANDLE_TYPE(DOUBLE, double, Double) 600 HANDLE_TYPE(DOUBLE, double, Double)
228 601
229 HANDLE_TYPE(BOOL, bool, Bool) 602 HANDLE_TYPE(BOOL, bool, Bool)
230 #undef HANDLE_TYPE 603 #undef HANDLE_TYPE
231 604
232 case FieldDescriptor::TYPE_ENUM: { 605 case FieldDescriptor::TYPE_ENUM: {
233 int value; 606 int value;
234 if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>( 607 if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
235 input, &value)) return false; 608 input, &value)) return false;
236 const EnumValueDescriptor* enum_value = 609 if (message->GetDescriptor()->file()->syntax() ==
237 field->enum_type()->FindValueByNumber(value); 610 FileDescriptor::SYNTAX_PROTO3) {
238 if (enum_value != NULL) {
239 if (field->is_repeated()) { 611 if (field->is_repeated()) {
240 message_reflection->AddEnum(message, field, enum_value); 612 message_reflection->AddEnumValue(message, field, value);
241 } else { 613 } else {
242 message_reflection->SetEnum(message, field, enum_value); 614 message_reflection->SetEnumValue(message, field, value);
243 } 615 }
244 } else { 616 } else {
245 // The enum value is not one of the known values. Add it to the 617 const EnumValueDescriptor* enum_value =
246 // UnknownFieldSet. 618 field->enum_type()->FindValueByNumber(value);
247 int64 sign_extended_value = static_cast<int64>(value); 619 if (enum_value != NULL) {
248 message_reflection->MutableUnknownFields(message) 620 if (field->is_repeated()) {
249 ->AddVarint(WireFormatLite::GetTagFieldNumber(tag), 621 message_reflection->AddEnum(message, field, enum_value);
250 sign_extended_value); 622 } else {
623 message_reflection->SetEnum(message, field, enum_value);
624 }
625 } else {
626 // The enum value is not one of the known values. Add it to the
627 // UnknownFieldSet.
628 int64 sign_extended_value = static_cast<int64>(value);
629 message_reflection->MutableUnknownFields(message)
630 ->AddVarint(
631 WireFormatLite::GetTagFieldNumber(tag),
632 sign_extended_value);
633 }
251 } 634 }
252 break; 635 break;
253 } 636 }
254 637
255 // Handle strings separately so that we can optimize the ctype=CORD case. 638 // Handle strings separately so that we can optimize the ctype=CORD case.
256 case FieldDescriptor::TYPE_STRING: { 639 case FieldDescriptor::TYPE_STRING: {
640 bool strict_utf8_check = StrictUtf8Check(field);
257 string value; 641 string value;
258 if (!WireFormatLite::ReadString(input, &value)) return false; 642 if (!WireFormatLite::ReadString(input, &value)) return false;
259 VerifyUTF8String(value.data(), value.length(), PARSE); 643 if (strict_utf8_check) {
644 if (!WireFormatLite::VerifyUtf8String(
645 value.data(), value.length(), WireFormatLite::PARSE,
646 field->full_name().c_str())) {
647 return false;
648 }
649 } else {
650 VerifyUTF8StringNamedField(value.data(), value.length(), PARSE,
651 field->full_name().c_str());
652 }
260 if (field->is_repeated()) { 653 if (field->is_repeated()) {
261 message_reflection->AddString(message, field, value); 654 message_reflection->AddString(message, field, value);
262 } else { 655 } else {
263 message_reflection->SetString(message, field, value); 656 message_reflection->SetString(message, field, value);
264 } 657 }
265 break; 658 break;
266 } 659 }
267 660
268 case FieldDescriptor::TYPE_BYTES: { 661 case FieldDescriptor::TYPE_BYTES: {
269 string value; 662 string value;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 706
314 bool WireFormat::ParseAndMergeMessageSetItem( 707 bool WireFormat::ParseAndMergeMessageSetItem(
315 io::CodedInputStream* input, 708 io::CodedInputStream* input,
316 Message* message) { 709 Message* message) {
317 const Reflection* message_reflection = message->GetReflection(); 710 const Reflection* message_reflection = message->GetReflection();
318 711
319 // This method parses a group which should contain two fields: 712 // This method parses a group which should contain two fields:
320 // required int32 type_id = 2; 713 // required int32 type_id = 2;
321 // required data message = 3; 714 // required data message = 3;
322 715
323 // Once we see a type_id, we'll construct a fake tag for this extension 716 uint32 last_type_id = 0;
324 // which is the tag it would have had under the proto2 extensions wire
325 // format.
326 uint32 fake_tag = 0;
327 717
328 // Once we see a type_id, we'll look up the FieldDescriptor for the 718 // Once we see a type_id, we'll look up the FieldDescriptor for the
329 // extension. 719 // extension.
330 const FieldDescriptor* field = NULL; 720 const FieldDescriptor* field = NULL;
331 721
332 // If we see message data before the type_id, we'll append it to this so 722 // If we see message data before the type_id, we'll append it to this so
333 // we can parse it later. 723 // we can parse it later.
334 string message_data; 724 string message_data;
335 725
336 while (true) { 726 while (true) {
337 uint32 tag = input->ReadTag(); 727 uint32 tag = input->ReadTag();
338 if (tag == 0) return false; 728 if (tag == 0) return false;
339 729
340 switch (tag) { 730 switch (tag) {
341 case WireFormatLite::kMessageSetTypeIdTag: { 731 case WireFormatLite::kMessageSetTypeIdTag: {
342 uint32 type_id; 732 uint32 type_id;
343 if (!input->ReadVarint32(&type_id)) return false; 733 if (!input->ReadVarint32(&type_id)) return false;
344 fake_tag = WireFormatLite::MakeTag( 734 last_type_id = type_id;
345 type_id, WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
346 field = message_reflection->FindKnownExtensionByNumber(type_id); 735 field = message_reflection->FindKnownExtensionByNumber(type_id);
347 736
348 if (!message_data.empty()) { 737 if (!message_data.empty()) {
349 // We saw some message data before the type_id. Have to parse it 738 // We saw some message data before the type_id. Have to parse it
350 // now. 739 // now.
351 io::ArrayInputStream raw_input(message_data.data(), 740 io::ArrayInputStream raw_input(message_data.data(),
352 message_data.size()); 741 message_data.size());
353 io::CodedInputStream sub_input(&raw_input); 742 io::CodedInputStream sub_input(&raw_input);
354 if (!ParseAndMergeField(fake_tag, field, message, 743 if (!ParseAndMergeMessageSetField(last_type_id, field, message,
355 &sub_input)) { 744 &sub_input)) {
356 return false; 745 return false;
357 } 746 }
358 message_data.clear(); 747 message_data.clear();
359 } 748 }
360 749
361 break; 750 break;
362 } 751 }
363 752
364 case WireFormatLite::kMessageSetMessageTag: { 753 case WireFormatLite::kMessageSetMessageTag: {
365 if (fake_tag == 0) { 754 if (last_type_id == 0) {
366 // We haven't seen a type_id yet. Append this data to message_data. 755 // We haven't seen a type_id yet. Append this data to message_data.
367 string temp; 756 string temp;
368 uint32 length; 757 uint32 length;
369 if (!input->ReadVarint32(&length)) return false; 758 if (!input->ReadVarint32(&length)) return false;
370 if (!input->ReadString(&temp, length)) return false; 759 if (!input->ReadString(&temp, length)) return false;
371 io::StringOutputStream output_stream(&message_data); 760 io::StringOutputStream output_stream(&message_data);
372 io::CodedOutputStream coded_output(&output_stream); 761 io::CodedOutputStream coded_output(&output_stream);
373 coded_output.WriteVarint32(length); 762 coded_output.WriteVarint32(length);
374 coded_output.WriteString(temp); 763 coded_output.WriteString(temp);
375 } else { 764 } else {
376 // Already saw type_id, so we can parse this directly. 765 // Already saw type_id, so we can parse this directly.
377 if (!ParseAndMergeField(fake_tag, field, message, input)) { 766 if (!ParseAndMergeMessageSetField(last_type_id, field, message,
767 input)) {
378 return false; 768 return false;
379 } 769 }
380 } 770 }
381 771
382 break; 772 break;
383 } 773 }
384 774
385 case WireFormatLite::kMessageSetItemEndTag: { 775 case WireFormatLite::kMessageSetItemEndTag: {
386 return true; 776 return true;
387 } 777 }
388 778
389 default: { 779 default: {
390 if (!WireFormatLite::SkipField(input, tag, NULL)) return false; 780 if (!SkipField(input, tag, NULL)) return false;
391 } 781 }
392 } 782 }
393 } 783 }
394 } 784 }
395 785
396 // =================================================================== 786 // ===================================================================
397 787
398 void WireFormat::SerializeWithCachedSizes( 788 void WireFormat::SerializeWithCachedSizes(
399 const Message& message, 789 const Message& message,
400 int size, io::CodedOutputStream* output) { 790 int size, io::CodedOutputStream* output) {
401 const Descriptor* descriptor = message.GetDescriptor(); 791 const Descriptor* descriptor = message.GetDescriptor();
402 const Reflection* message_reflection = message.GetReflection(); 792 const Reflection* message_reflection = message.GetReflection();
403 int expected_endpoint = output->ByteCount() + size; 793 int expected_endpoint = output->ByteCount() + size;
404 794
405 vector<const FieldDescriptor*> fields; 795 vector<const FieldDescriptor*> fields;
406 message_reflection->ListFields(message, &fields); 796 message_reflection->ListFields(message, &fields);
407 for (int i = 0; i < fields.size(); i++) { 797 for (int i = 0; i < fields.size(); i++) {
408 SerializeFieldWithCachedSizes(fields[i], message, output); 798 SerializeFieldWithCachedSizes(fields[i], message, output);
409 } 799 }
410 800
411 if (descriptor->options().message_set_wire_format()) { 801 if (descriptor->options().message_set_wire_format()) {
412 WireFormatLite::SerializeUnknownMessageSetItems( 802 SerializeUnknownMessageSetItems(
413 message_reflection->GetUnknownFields(message), output); 803 message_reflection->GetUnknownFields(message), output);
414 } else { 804 } else {
415 WireFormatLite::SerializeUnknownFields( 805 SerializeUnknownFields(
416 message_reflection->GetUnknownFields(message), output); 806 message_reflection->GetUnknownFields(message), output);
417 } 807 }
418 808
419 GOOGLE_CHECK_EQ(output->ByteCount(), expected_endpoint) 809 GOOGLE_CHECK_EQ(output->ByteCount(), expected_endpoint)
420 << ": Protocol message serialized to a size different from what was " 810 << ": Protocol message serialized to a size different from what was "
421 "originally expected. Perhaps it was modified by another thread " 811 "originally expected. Perhaps it was modified by another thread "
422 "during serialization?"; 812 "during serialization?";
423 } 813 }
424 814
425 void WireFormat::SerializeFieldWithCachedSizes( 815 void WireFormat::SerializeFieldWithCachedSizes(
(...skipping 11 matching lines...) Expand all
437 } 827 }
438 828
439 int count = 0; 829 int count = 0;
440 830
441 if (field->is_repeated()) { 831 if (field->is_repeated()) {
442 count = message_reflection->FieldSize(message, field); 832 count = message_reflection->FieldSize(message, field);
443 } else if (message_reflection->HasField(message, field)) { 833 } else if (message_reflection->HasField(message, field)) {
444 count = 1; 834 count = 1;
445 } 835 }
446 836
447 const bool is_packed = field->options().packed(); 837 const bool is_packed = field->is_packed();
448 if (is_packed && count > 0) { 838 if (is_packed && count > 0) {
449 WireFormatLite::WriteTag(field->number(), 839 WireFormatLite::WriteTag(field->number(),
450 WireFormatLite::WIRETYPE_LENGTH_DELIMITED, output); 840 WireFormatLite::WIRETYPE_LENGTH_DELIMITED, output);
451 const int data_size = FieldDataOnlyByteSize(field, message); 841 const int data_size = FieldDataOnlyByteSize(field, message);
452 output->WriteVarint32(data_size); 842 output->WriteVarint32(data_size);
453 } 843 }
454 844
455 for (int j = 0; j < count; j++) { 845 for (int j = 0; j < count; j++) {
456 switch (field->type()) { 846 switch (field->type()) {
457 #define HANDLE_PRIMITIVE_TYPE(TYPE, CPPTYPE, TYPE_METHOD, CPPTYPE_METHOD) \ 847 #define HANDLE_PRIMITIVE_TYPE(TYPE, CPPTYPE, TYPE_METHOD, CPPTYPE_METHOD) \
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 WireFormatLite::WriteEnumNoTag(value->number(), output); 900 WireFormatLite::WriteEnumNoTag(value->number(), output);
511 } else { 901 } else {
512 WireFormatLite::WriteEnum(field->number(), value->number(), output); 902 WireFormatLite::WriteEnum(field->number(), value->number(), output);
513 } 903 }
514 break; 904 break;
515 } 905 }
516 906
517 // Handle strings separately so that we can get string references 907 // Handle strings separately so that we can get string references
518 // instead of copying. 908 // instead of copying.
519 case FieldDescriptor::TYPE_STRING: { 909 case FieldDescriptor::TYPE_STRING: {
910 bool strict_utf8_check = StrictUtf8Check(field);
520 string scratch; 911 string scratch;
521 const string& value = field->is_repeated() ? 912 const string& value = field->is_repeated() ?
522 message_reflection->GetRepeatedStringReference( 913 message_reflection->GetRepeatedStringReference(
523 message, field, j, &scratch) : 914 message, field, j, &scratch) :
524 message_reflection->GetStringReference(message, field, &scratch); 915 message_reflection->GetStringReference(message, field, &scratch);
525 VerifyUTF8String(value.data(), value.length(), SERIALIZE); 916 if (strict_utf8_check) {
917 WireFormatLite::VerifyUtf8String(value.data(), value.length(),
918 WireFormatLite::SERIALIZE,
919 field->full_name().c_str());
920 } else {
921 VerifyUTF8StringNamedField(value.data(), value.length(), SERIALIZE,
922 field->full_name().c_str());
923 }
526 WireFormatLite::WriteString(field->number(), value, output); 924 WireFormatLite::WriteString(field->number(), value, output);
527 break; 925 break;
528 } 926 }
529 927
530 case FieldDescriptor::TYPE_BYTES: { 928 case FieldDescriptor::TYPE_BYTES: {
531 string scratch; 929 string scratch;
532 const string& value = field->is_repeated() ? 930 const string& value = field->is_repeated() ?
533 message_reflection->GetRepeatedStringReference( 931 message_reflection->GetRepeatedStringReference(
534 message, field, j, &scratch) : 932 message, field, j, &scratch) :
535 message_reflection->GetStringReference(message, field, &scratch); 933 message_reflection->GetStringReference(message, field, &scratch);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 970
573 int our_size = 0; 971 int our_size = 0;
574 972
575 vector<const FieldDescriptor*> fields; 973 vector<const FieldDescriptor*> fields;
576 message_reflection->ListFields(message, &fields); 974 message_reflection->ListFields(message, &fields);
577 for (int i = 0; i < fields.size(); i++) { 975 for (int i = 0; i < fields.size(); i++) {
578 our_size += FieldByteSize(fields[i], message); 976 our_size += FieldByteSize(fields[i], message);
579 } 977 }
580 978
581 if (descriptor->options().message_set_wire_format()) { 979 if (descriptor->options().message_set_wire_format()) {
582 our_size += WireFormatLite::ComputeUnknownMessageSetItemsSize( 980 our_size += ComputeUnknownMessageSetItemsSize(
583 message_reflection->GetUnknownFields(message)); 981 message_reflection->GetUnknownFields(message));
584 } else { 982 } else {
585 our_size += WireFormatLite::ComputeUnknownFieldsSize( 983 our_size += ComputeUnknownFieldsSize(
586 message_reflection->GetUnknownFields(message)); 984 message_reflection->GetUnknownFields(message));
587 } 985 }
588 986
589 return our_size; 987 return our_size;
590 } 988 }
591 989
592 int WireFormat::FieldByteSize( 990 int WireFormat::FieldByteSize(
593 const FieldDescriptor* field, 991 const FieldDescriptor* field,
594 const Message& message) { 992 const Message& message) {
595 const Reflection* message_reflection = message.GetReflection(); 993 const Reflection* message_reflection = message.GetReflection();
596 994
597 if (field->is_extension() && 995 if (field->is_extension() &&
598 field->containing_type()->options().message_set_wire_format() && 996 field->containing_type()->options().message_set_wire_format() &&
599 field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE && 997 field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
600 !field->is_repeated()) { 998 !field->is_repeated()) {
601 return MessageSetItemByteSize(field, message); 999 return MessageSetItemByteSize(field, message);
602 } 1000 }
603 1001
604 int count = 0; 1002 int count = 0;
605 if (field->is_repeated()) { 1003 if (field->is_repeated()) {
606 count = message_reflection->FieldSize(message, field); 1004 count = message_reflection->FieldSize(message, field);
607 } else if (message_reflection->HasField(message, field)) { 1005 } else if (message_reflection->HasField(message, field)) {
608 count = 1; 1006 count = 1;
609 } 1007 }
610 1008
611 const int data_size = FieldDataOnlyByteSize(field, message); 1009 const int data_size = FieldDataOnlyByteSize(field, message);
612 int our_size = data_size; 1010 int our_size = data_size;
613 if (field->options().packed()) { 1011 if (field->is_packed()) {
614 if (data_size > 0) { 1012 if (data_size > 0) {
615 // Packed fields get serialized like a string, not their native type. 1013 // Packed fields get serialized like a string, not their native type.
616 // Technically this doesn't really matter; the size only changes if it's 1014 // Technically this doesn't really matter; the size only changes if it's
617 // a GROUP 1015 // a GROUP
618 our_size += TagSize(field->number(), FieldDescriptor::TYPE_STRING); 1016 our_size += TagSize(field->number(), FieldDescriptor::TYPE_STRING);
619 our_size += io::CodedOutputStream::VarintSize32(data_size); 1017 our_size += io::CodedOutputStream::VarintSize32(data_size);
620 } 1018 }
621 } else { 1019 } else {
622 our_size += count * TagSize(field->number(), field->type()); 1020 our_size += count * TagSize(field->number(), field->type());
623 } 1021 }
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
723 // message 1121 // message
724 const Message& sub_message = message_reflection->GetMessage(message, field); 1122 const Message& sub_message = message_reflection->GetMessage(message, field);
725 int message_size = sub_message.ByteSize(); 1123 int message_size = sub_message.ByteSize();
726 1124
727 our_size += io::CodedOutputStream::VarintSize32(message_size); 1125 our_size += io::CodedOutputStream::VarintSize32(message_size);
728 our_size += message_size; 1126 our_size += message_size;
729 1127
730 return our_size; 1128 return our_size;
731 } 1129 }
732 1130
733 void WireFormat::VerifyUTF8StringFallback(const char* data,
734 int size,
735 Operation op) {
736 if (!IsStructurallyValidUTF8(data, size)) {
737 const char* operation_str = NULL;
738 switch (op) {
739 case PARSE:
740 operation_str = "parsing";
741 break;
742 case SERIALIZE:
743 operation_str = "serializing";
744 break;
745 // no default case: have the compiler warn if a case is not covered.
746 }
747 GOOGLE_LOG(ERROR) << "String field contains invalid UTF-8 data when "
748 << operation_str
749 << " a protocol buffer. Use the 'bytes' type if you intend to "
750 "send raw bytes.";
751 }
752 }
753
754
755 } // namespace internal 1131 } // namespace internal
756 } // namespace protobuf 1132 } // namespace protobuf
757 } // namespace google 1133 } // namespace google
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698