1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
39 #include <google/protobuf/wire_format.h>
41 #include <google/protobuf/stubs/logging.h>
42 #include <google/protobuf/stubs/common.h>
43 #include <google/protobuf/stubs/stringprintf.h>
44 #include <google/protobuf/descriptor.pb.h>
45 #include <google/protobuf/io/coded_stream.h>
46 #include <google/protobuf/io/zero_copy_stream.h>
47 #include <google/protobuf/io/zero_copy_stream_impl.h>
48 #include <google/protobuf/descriptor.h>
49 #include <google/protobuf/dynamic_message.h>
50 #include <google/protobuf/map_field.h>
51 #include <google/protobuf/map_field_inl.h>
52 #include <google/protobuf/unknown_field_set.h>
55 #include <google/protobuf/port_def.inc>
57 const size_t kMapEntryTagByteSize = 2;
63 // Forward declare static functions
64 static size_t MapKeyDataOnlyByteSize(const FieldDescriptor* field,
66 static size_t MapValueRefDataOnlyByteSize(const FieldDescriptor* field,
67 const MapValueRef& value);
69 // ===================================================================
71 bool UnknownFieldSetFieldSkipper::SkipField(io::CodedInputStream* input,
73 return WireFormat::SkipField(input, tag, unknown_fields_);
76 bool UnknownFieldSetFieldSkipper::SkipMessage(io::CodedInputStream* input) {
77 return WireFormat::SkipMessage(input, unknown_fields_);
80 void UnknownFieldSetFieldSkipper::SkipUnknownEnum(int field_number, int value) {
81 unknown_fields_->AddVarint(field_number, value);
84 bool WireFormat::SkipField(io::CodedInputStream* input, uint32 tag,
85 UnknownFieldSet* unknown_fields) {
86 int number = WireFormatLite::GetTagFieldNumber(tag);
87 // Field number 0 is illegal.
88 if (number == 0) return false;
90 switch (WireFormatLite::GetTagWireType(tag)) {
91 case WireFormatLite::WIRETYPE_VARINT: {
93 if (!input->ReadVarint64(&value)) return false;
94 if (unknown_fields != NULL) unknown_fields->AddVarint(number, value);
97 case WireFormatLite::WIRETYPE_FIXED64: {
99 if (!input->ReadLittleEndian64(&value)) return false;
100 if (unknown_fields != NULL) unknown_fields->AddFixed64(number, value);
103 case WireFormatLite::WIRETYPE_LENGTH_DELIMITED: {
105 if (!input->ReadVarint32(&length)) return false;
106 if (unknown_fields == NULL) {
107 if (!input->Skip(length)) return false;
109 if (!input->ReadString(unknown_fields->AddLengthDelimited(number),
116 case WireFormatLite::WIRETYPE_START_GROUP: {
117 if (!input->IncrementRecursionDepth()) return false;
118 if (!SkipMessage(input, (unknown_fields == NULL)
120 : unknown_fields->AddGroup(number))) {
123 input->DecrementRecursionDepth();
124 // Check that the ending tag matched the starting tag.
125 if (!input->LastTagWas(
126 WireFormatLite::MakeTag(WireFormatLite::GetTagFieldNumber(tag),
127 WireFormatLite::WIRETYPE_END_GROUP))) {
132 case WireFormatLite::WIRETYPE_END_GROUP: {
135 case WireFormatLite::WIRETYPE_FIXED32: {
137 if (!input->ReadLittleEndian32(&value)) return false;
138 if (unknown_fields != NULL) unknown_fields->AddFixed32(number, value);
147 bool WireFormat::SkipMessage(io::CodedInputStream* input,
148 UnknownFieldSet* unknown_fields) {
150 uint32 tag = input->ReadTag();
152 // End of input. This is a valid place to end, so return true.
156 WireFormatLite::WireType wire_type = WireFormatLite::GetTagWireType(tag);
158 if (wire_type == WireFormatLite::WIRETYPE_END_GROUP) {
159 // Must be the end of the message.
163 if (!SkipField(input, tag, unknown_fields)) return false;
167 bool WireFormat::ReadPackedEnumPreserveUnknowns(io::CodedInputStream* input,
169 bool (*is_valid)(int),
170 UnknownFieldSet* unknown_fields,
171 RepeatedField<int>* values) {
173 if (!input->ReadVarint32(&length)) return false;
174 io::CodedInputStream::Limit limit = input->PushLimit(length);
175 while (input->BytesUntilLimit() > 0) {
177 if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
181 if (is_valid == NULL || is_valid(value)) {
184 unknown_fields->AddVarint(field_number, value);
187 input->PopLimit(limit);
191 void WireFormat::SerializeUnknownFields(const UnknownFieldSet& unknown_fields,
192 io::CodedOutputStream* output) {
193 for (int i = 0; i < unknown_fields.field_count(); i++) {
194 const UnknownField& field = unknown_fields.field(i);
195 switch (field.type()) {
196 case UnknownField::TYPE_VARINT:
197 output->WriteVarint32(WireFormatLite::MakeTag(
198 field.number(), WireFormatLite::WIRETYPE_VARINT));
199 output->WriteVarint64(field.varint());
201 case UnknownField::TYPE_FIXED32:
202 output->WriteVarint32(WireFormatLite::MakeTag(
203 field.number(), WireFormatLite::WIRETYPE_FIXED32));
204 output->WriteLittleEndian32(field.fixed32());
206 case UnknownField::TYPE_FIXED64:
207 output->WriteVarint32(WireFormatLite::MakeTag(
208 field.number(), WireFormatLite::WIRETYPE_FIXED64));
209 output->WriteLittleEndian64(field.fixed64());
211 case UnknownField::TYPE_LENGTH_DELIMITED:
212 output->WriteVarint32(WireFormatLite::MakeTag(
213 field.number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED));
214 output->WriteVarint32(field.length_delimited().size());
215 output->WriteRawMaybeAliased(field.length_delimited().data(),
216 field.length_delimited().size());
218 case UnknownField::TYPE_GROUP:
219 output->WriteVarint32(WireFormatLite::MakeTag(
220 field.number(), WireFormatLite::WIRETYPE_START_GROUP));
221 SerializeUnknownFields(field.group(), output);
222 output->WriteVarint32(WireFormatLite::MakeTag(
223 field.number(), WireFormatLite::WIRETYPE_END_GROUP));
229 uint8* WireFormat::SerializeUnknownFieldsToArray(
230 const UnknownFieldSet& unknown_fields, uint8* target) {
231 for (int i = 0; i < unknown_fields.field_count(); i++) {
232 const UnknownField& field = unknown_fields.field(i);
234 switch (field.type()) {
235 case UnknownField::TYPE_VARINT:
236 target = WireFormatLite::WriteInt64ToArray(field.number(),
237 field.varint(), target);
239 case UnknownField::TYPE_FIXED32:
240 target = WireFormatLite::WriteFixed32ToArray(field.number(),
241 field.fixed32(), target);
243 case UnknownField::TYPE_FIXED64:
244 target = WireFormatLite::WriteFixed64ToArray(field.number(),
245 field.fixed64(), target);
247 case UnknownField::TYPE_LENGTH_DELIMITED:
248 target = WireFormatLite::WriteBytesToArray(
249 field.number(), field.length_delimited(), target);
251 case UnknownField::TYPE_GROUP:
252 target = WireFormatLite::WriteTagToArray(
253 field.number(), WireFormatLite::WIRETYPE_START_GROUP, target);
254 target = SerializeUnknownFieldsToArray(field.group(), target);
255 target = WireFormatLite::WriteTagToArray(
256 field.number(), WireFormatLite::WIRETYPE_END_GROUP, target);
263 void WireFormat::SerializeUnknownMessageSetItems(
264 const UnknownFieldSet& unknown_fields, io::CodedOutputStream* output) {
265 for (int i = 0; i < unknown_fields.field_count(); i++) {
266 const UnknownField& field = unknown_fields.field(i);
267 // The only unknown fields that are allowed to exist in a MessageSet are
268 // messages, which are length-delimited.
269 if (field.type() == UnknownField::TYPE_LENGTH_DELIMITED) {
271 output->WriteVarint32(WireFormatLite::kMessageSetItemStartTag);
274 output->WriteVarint32(WireFormatLite::kMessageSetTypeIdTag);
275 output->WriteVarint32(field.number());
278 output->WriteVarint32(WireFormatLite::kMessageSetMessageTag);
279 field.SerializeLengthDelimitedNoTag(output);
282 output->WriteVarint32(WireFormatLite::kMessageSetItemEndTag);
287 uint8* WireFormat::SerializeUnknownMessageSetItemsToArray(
288 const UnknownFieldSet& unknown_fields, uint8* target) {
289 for (int i = 0; i < unknown_fields.field_count(); i++) {
290 const UnknownField& field = unknown_fields.field(i);
292 // The only unknown fields that are allowed to exist in a MessageSet are
293 // messages, which are length-delimited.
294 if (field.type() == UnknownField::TYPE_LENGTH_DELIMITED) {
296 target = io::CodedOutputStream::WriteTagToArray(
297 WireFormatLite::kMessageSetItemStartTag, target);
300 target = io::CodedOutputStream::WriteTagToArray(
301 WireFormatLite::kMessageSetTypeIdTag, target);
303 io::CodedOutputStream::WriteVarint32ToArray(field.number(), target);
306 target = io::CodedOutputStream::WriteTagToArray(
307 WireFormatLite::kMessageSetMessageTag, target);
308 target = field.SerializeLengthDelimitedNoTagToArray(target);
311 target = io::CodedOutputStream::WriteTagToArray(
312 WireFormatLite::kMessageSetItemEndTag, target);
319 size_t WireFormat::ComputeUnknownFieldsSize(
320 const UnknownFieldSet& unknown_fields) {
322 for (int i = 0; i < unknown_fields.field_count(); i++) {
323 const UnknownField& field = unknown_fields.field(i);
325 switch (field.type()) {
326 case UnknownField::TYPE_VARINT:
327 size += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag(
328 field.number(), WireFormatLite::WIRETYPE_VARINT));
329 size += io::CodedOutputStream::VarintSize64(field.varint());
331 case UnknownField::TYPE_FIXED32:
332 size += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag(
333 field.number(), WireFormatLite::WIRETYPE_FIXED32));
334 size += sizeof(int32);
336 case UnknownField::TYPE_FIXED64:
337 size += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag(
338 field.number(), WireFormatLite::WIRETYPE_FIXED64));
339 size += sizeof(int64);
341 case UnknownField::TYPE_LENGTH_DELIMITED:
342 size += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag(
343 field.number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED));
344 size += io::CodedOutputStream::VarintSize32(
345 field.length_delimited().size());
346 size += field.length_delimited().size();
348 case UnknownField::TYPE_GROUP:
349 size += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag(
350 field.number(), WireFormatLite::WIRETYPE_START_GROUP));
351 size += ComputeUnknownFieldsSize(field.group());
352 size += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag(
353 field.number(), WireFormatLite::WIRETYPE_END_GROUP));
361 size_t WireFormat::ComputeUnknownMessageSetItemsSize(
362 const UnknownFieldSet& unknown_fields) {
364 for (int i = 0; i < unknown_fields.field_count(); i++) {
365 const UnknownField& field = unknown_fields.field(i);
367 // The only unknown fields that are allowed to exist in a MessageSet are
368 // messages, which are length-delimited.
369 if (field.type() == UnknownField::TYPE_LENGTH_DELIMITED) {
370 size += WireFormatLite::kMessageSetItemTagsSize;
371 size += io::CodedOutputStream::VarintSize32(field.number());
373 int field_size = field.GetLengthDelimitedSize();
374 size += io::CodedOutputStream::VarintSize32(field_size);
382 // ===================================================================
384 bool WireFormat::ParseAndMergePartial(io::CodedInputStream* input,
386 const Descriptor* descriptor = message->GetDescriptor();
387 const Reflection* message_reflection = message->GetReflection();
390 uint32 tag = input->ReadTag();
392 // End of input. This is a valid place to end, so return true.
396 if (WireFormatLite::GetTagWireType(tag) ==
397 WireFormatLite::WIRETYPE_END_GROUP) {
398 // Must be the end of the message.
402 const FieldDescriptor* field = NULL;
404 if (descriptor != NULL) {
405 int field_number = WireFormatLite::GetTagFieldNumber(tag);
406 field = descriptor->FindFieldByNumber(field_number);
408 // If that failed, check if the field is an extension.
409 if (field == NULL && descriptor->IsExtensionNumber(field_number)) {
410 if (input->GetExtensionPool() == NULL) {
411 field = message_reflection->FindKnownExtensionByNumber(field_number);
413 field = input->GetExtensionPool()->FindExtensionByNumber(
414 descriptor, field_number);
418 // If that failed, but we're a MessageSet, and this is the tag for a
419 // MessageSet item, then parse that.
420 if (field == NULL && descriptor->options().message_set_wire_format() &&
421 tag == WireFormatLite::kMessageSetItemStartTag) {
422 if (!ParseAndMergeMessageSetItem(input, message)) {
425 continue; // Skip ParseAndMergeField(); already taken care of.
429 if (!ParseAndMergeField(tag, field, message, input)) {
435 bool WireFormat::SkipMessageSetField(io::CodedInputStream* input,
437 UnknownFieldSet* unknown_fields) {
439 if (!input->ReadVarint32(&length)) return false;
440 return input->ReadString(unknown_fields->AddLengthDelimited(field_number),
444 bool WireFormat::ParseAndMergeMessageSetField(uint32 field_number,
445 const FieldDescriptor* field,
447 io::CodedInputStream* input) {
448 const Reflection* message_reflection = message->GetReflection();
450 // We store unknown MessageSet extensions as groups.
451 return SkipMessageSetField(
452 input, field_number, message_reflection->MutableUnknownFields(message));
453 } else if (field->is_repeated() ||
454 field->type() != FieldDescriptor::TYPE_MESSAGE) {
455 // This shouldn't happen as we only allow optional message extensions to
457 GOOGLE_LOG(ERROR) << "Extensions of MessageSets must be optional messages.";
460 Message* sub_message = message_reflection->MutableMessage(
461 message, field, input->GetExtensionFactory());
462 return WireFormatLite::ReadMessage(input, sub_message);
466 static bool StrictUtf8Check(const FieldDescriptor* field) {
467 return field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3;
470 bool WireFormat::ParseAndMergeField(
472 const FieldDescriptor* field, // May be NULL for unknown
473 Message* message, io::CodedInputStream* input) {
474 const Reflection* message_reflection = message->GetReflection();
476 enum { UNKNOWN, NORMAL_FORMAT, PACKED_FORMAT } value_format;
479 value_format = UNKNOWN;
480 } else if (WireFormatLite::GetTagWireType(tag) ==
481 WireTypeForFieldType(field->type())) {
482 value_format = NORMAL_FORMAT;
483 } else if (field->is_packable() &&
484 WireFormatLite::GetTagWireType(tag) ==
485 WireFormatLite::WIRETYPE_LENGTH_DELIMITED) {
486 value_format = PACKED_FORMAT;
488 // We don't recognize this field. Either the field number is unknown
489 // or the wire type doesn't match. Put it in our unknown field set.
490 value_format = UNKNOWN;
493 if (value_format == UNKNOWN) {
494 return SkipField(input, tag,
495 message_reflection->MutableUnknownFields(message));
496 } else if (value_format == PACKED_FORMAT) {
498 if (!input->ReadVarint32(&length)) return false;
499 io::CodedInputStream::Limit limit = input->PushLimit(length);
501 switch (field->type()) {
502 #define HANDLE_PACKED_TYPE(TYPE, CPPTYPE, CPPTYPE_METHOD) \
503 case FieldDescriptor::TYPE_##TYPE: { \
504 while (input->BytesUntilLimit() > 0) { \
506 if (!WireFormatLite::ReadPrimitive<CPPTYPE, \
507 WireFormatLite::TYPE_##TYPE>(input, \
510 message_reflection->Add##CPPTYPE_METHOD(message, field, value); \
515 HANDLE_PACKED_TYPE(INT32, int32, Int32)
516 HANDLE_PACKED_TYPE(INT64, int64, Int64)
517 HANDLE_PACKED_TYPE(SINT32, int32, Int32)
518 HANDLE_PACKED_TYPE(SINT64, int64, Int64)
519 HANDLE_PACKED_TYPE(UINT32, uint32, UInt32)
520 HANDLE_PACKED_TYPE(UINT64, uint64, UInt64)
522 HANDLE_PACKED_TYPE(FIXED32, uint32, UInt32)
523 HANDLE_PACKED_TYPE(FIXED64, uint64, UInt64)
524 HANDLE_PACKED_TYPE(SFIXED32, int32, Int32)
525 HANDLE_PACKED_TYPE(SFIXED64, int64, Int64)
527 HANDLE_PACKED_TYPE(FLOAT, float, Float)
528 HANDLE_PACKED_TYPE(DOUBLE, double, Double)
530 HANDLE_PACKED_TYPE(BOOL, bool, Bool)
531 #undef HANDLE_PACKED_TYPE
533 case FieldDescriptor::TYPE_ENUM: {
534 while (input->BytesUntilLimit() > 0) {
536 if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
539 if (message->GetDescriptor()->file()->syntax() ==
540 FileDescriptor::SYNTAX_PROTO3) {
541 message_reflection->AddEnumValue(message, field, value);
543 const EnumValueDescriptor* enum_value =
544 field->enum_type()->FindValueByNumber(value);
545 if (enum_value != NULL) {
546 message_reflection->AddEnum(message, field, enum_value);
548 // The enum value is not one of the known values. Add it to the
550 int64 sign_extended_value = static_cast<int64>(value);
551 message_reflection->MutableUnknownFields(message)->AddVarint(
552 WireFormatLite::GetTagFieldNumber(tag), sign_extended_value);
560 case FieldDescriptor::TYPE_STRING:
561 case FieldDescriptor::TYPE_GROUP:
562 case FieldDescriptor::TYPE_MESSAGE:
563 case FieldDescriptor::TYPE_BYTES:
564 // Can't have packed fields of these types: these should be caught by
565 // the protocol compiler.
570 input->PopLimit(limit);
572 // Non-packed value (value_format == NORMAL_FORMAT)
573 switch (field->type()) {
574 #define HANDLE_TYPE(TYPE, CPPTYPE, CPPTYPE_METHOD) \
575 case FieldDescriptor::TYPE_##TYPE: { \
577 if (!WireFormatLite::ReadPrimitive<CPPTYPE, WireFormatLite::TYPE_##TYPE>( \
580 if (field->is_repeated()) { \
581 message_reflection->Add##CPPTYPE_METHOD(message, field, value); \
583 message_reflection->Set##CPPTYPE_METHOD(message, field, value); \
588 HANDLE_TYPE(INT32, int32, Int32)
589 HANDLE_TYPE(INT64, int64, Int64)
590 HANDLE_TYPE(SINT32, int32, Int32)
591 HANDLE_TYPE(SINT64, int64, Int64)
592 HANDLE_TYPE(UINT32, uint32, UInt32)
593 HANDLE_TYPE(UINT64, uint64, UInt64)
595 HANDLE_TYPE(FIXED32, uint32, UInt32)
596 HANDLE_TYPE(FIXED64, uint64, UInt64)
597 HANDLE_TYPE(SFIXED32, int32, Int32)
598 HANDLE_TYPE(SFIXED64, int64, Int64)
600 HANDLE_TYPE(FLOAT, float, Float)
601 HANDLE_TYPE(DOUBLE, double, Double)
603 HANDLE_TYPE(BOOL, bool, Bool)
606 case FieldDescriptor::TYPE_ENUM: {
608 if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
611 if (field->is_repeated()) {
612 message_reflection->AddEnumValue(message, field, value);
614 message_reflection->SetEnumValue(message, field, value);
619 // Handle strings separately so that we can optimize the ctype=CORD case.
620 case FieldDescriptor::TYPE_STRING: {
621 bool strict_utf8_check = StrictUtf8Check(field);
623 if (!WireFormatLite::ReadString(input, &value)) return false;
624 if (strict_utf8_check) {
625 if (!WireFormatLite::VerifyUtf8String(value.data(), value.length(),
626 WireFormatLite::PARSE,
627 field->full_name().c_str())) {
631 VerifyUTF8StringNamedField(value.data(), value.length(), PARSE,
632 field->full_name().c_str());
634 if (field->is_repeated()) {
635 message_reflection->AddString(message, field, value);
637 message_reflection->SetString(message, field, value);
642 case FieldDescriptor::TYPE_BYTES: {
644 if (!WireFormatLite::ReadBytes(input, &value)) return false;
645 if (field->is_repeated()) {
646 message_reflection->AddString(message, field, value);
648 message_reflection->SetString(message, field, value);
653 case FieldDescriptor::TYPE_GROUP: {
654 Message* sub_message;
655 if (field->is_repeated()) {
656 sub_message = message_reflection->AddMessage(
657 message, field, input->GetExtensionFactory());
659 sub_message = message_reflection->MutableMessage(
660 message, field, input->GetExtensionFactory());
663 if (!WireFormatLite::ReadGroup(WireFormatLite::GetTagFieldNumber(tag),
669 case FieldDescriptor::TYPE_MESSAGE: {
670 Message* sub_message;
671 if (field->is_repeated()) {
672 sub_message = message_reflection->AddMessage(
673 message, field, input->GetExtensionFactory());
675 sub_message = message_reflection->MutableMessage(
676 message, field, input->GetExtensionFactory());
679 if (!WireFormatLite::ReadMessage(input, sub_message)) return false;
688 bool WireFormat::ParseAndMergeMessageSetItem(io::CodedInputStream* input,
690 struct MSReflective {
691 bool ParseField(int type_id, io::CodedInputStream* input) {
692 const FieldDescriptor* field =
693 message_reflection->FindKnownExtensionByNumber(type_id);
694 return ParseAndMergeMessageSetField(type_id, field, message, input);
697 bool SkipField(uint32 tag, io::CodedInputStream* input) {
698 return WireFormat::SkipField(input, tag, NULL);
701 const Reflection* message_reflection;
705 return ParseMessageSetItemImpl(
706 input, MSReflective{message->GetReflection(), message});
709 // ===================================================================
711 void WireFormat::SerializeWithCachedSizes(const Message& message, int size,
712 io::CodedOutputStream* output) {
713 const Descriptor* descriptor = message.GetDescriptor();
714 const Reflection* message_reflection = message.GetReflection();
715 int expected_endpoint = output->ByteCount() + size;
717 std::vector<const FieldDescriptor*> fields;
719 // Fields of map entry should always be serialized.
720 if (descriptor->options().map_entry()) {
721 for (int i = 0; i < descriptor->field_count(); i++) {
722 fields.push_back(descriptor->field(i));
725 message_reflection->ListFields(message, &fields);
728 for (int i = 0; i < fields.size(); i++) {
729 SerializeFieldWithCachedSizes(fields[i], message, output);
732 if (descriptor->options().message_set_wire_format()) {
733 SerializeUnknownMessageSetItems(
734 message_reflection->GetUnknownFields(message), output);
736 SerializeUnknownFields(message_reflection->GetUnknownFields(message),
740 GOOGLE_CHECK_EQ(output->ByteCount(), expected_endpoint)
741 << ": Protocol message serialized to a size different from what was "
742 "originally expected. Perhaps it was modified by another thread "
743 "during serialization?";
746 static void SerializeMapKeyWithCachedSizes(const FieldDescriptor* field,
748 io::CodedOutputStream* output) {
749 switch (field->type()) {
750 case FieldDescriptor::TYPE_DOUBLE:
751 case FieldDescriptor::TYPE_FLOAT:
752 case FieldDescriptor::TYPE_GROUP:
753 case FieldDescriptor::TYPE_MESSAGE:
754 case FieldDescriptor::TYPE_BYTES:
755 case FieldDescriptor::TYPE_ENUM:
756 GOOGLE_LOG(FATAL) << "Unsupported";
758 #define CASE_TYPE(FieldType, CamelFieldType, CamelCppType) \
759 case FieldDescriptor::TYPE_##FieldType: \
760 WireFormatLite::Write##CamelFieldType(1, value.Get##CamelCppType##Value(), \
763 CASE_TYPE(INT64, Int64, Int64)
764 CASE_TYPE(UINT64, UInt64, UInt64)
765 CASE_TYPE(INT32, Int32, Int32)
766 CASE_TYPE(FIXED64, Fixed64, UInt64)
767 CASE_TYPE(FIXED32, Fixed32, UInt32)
768 CASE_TYPE(BOOL, Bool, Bool)
769 CASE_TYPE(UINT32, UInt32, UInt32)
770 CASE_TYPE(SFIXED32, SFixed32, Int32)
771 CASE_TYPE(SFIXED64, SFixed64, Int64)
772 CASE_TYPE(SINT32, SInt32, Int32)
773 CASE_TYPE(SINT64, SInt64, Int64)
774 CASE_TYPE(STRING, String, String)
779 static void SerializeMapValueRefWithCachedSizes(const FieldDescriptor* field,
780 const MapValueRef& value,
781 io::CodedOutputStream* output) {
782 switch (field->type()) {
783 #define CASE_TYPE(FieldType, CamelFieldType, CamelCppType) \
784 case FieldDescriptor::TYPE_##FieldType: \
785 WireFormatLite::Write##CamelFieldType(2, value.Get##CamelCppType##Value(), \
788 CASE_TYPE(INT64, Int64, Int64)
789 CASE_TYPE(UINT64, UInt64, UInt64)
790 CASE_TYPE(INT32, Int32, Int32)
791 CASE_TYPE(FIXED64, Fixed64, UInt64)
792 CASE_TYPE(FIXED32, Fixed32, UInt32)
793 CASE_TYPE(BOOL, Bool, Bool)
794 CASE_TYPE(UINT32, UInt32, UInt32)
795 CASE_TYPE(SFIXED32, SFixed32, Int32)
796 CASE_TYPE(SFIXED64, SFixed64, Int64)
797 CASE_TYPE(SINT32, SInt32, Int32)
798 CASE_TYPE(SINT64, SInt64, Int64)
799 CASE_TYPE(ENUM, Enum, Enum)
800 CASE_TYPE(DOUBLE, Double, Double)
801 CASE_TYPE(FLOAT, Float, Float)
802 CASE_TYPE(STRING, String, String)
803 CASE_TYPE(BYTES, Bytes, String)
804 CASE_TYPE(MESSAGE, Message, Message)
805 CASE_TYPE(GROUP, Group, Message)
812 static std::vector<MapKey> SortKey(const Message& message,
813 const Reflection* reflection,
814 const FieldDescriptor* field) {
815 std::vector<MapKey> sorted_key_list;
816 for (MapIterator it =
817 reflection->MapBegin(const_cast<Message*>(&message), field);
818 it != reflection->MapEnd(const_cast<Message*>(&message), field);
820 sorted_key_list.push_back(it.GetKey());
822 MapKeyComparator comparator;
823 std::sort(sorted_key_list.begin(), sorted_key_list.end(), comparator);
824 return sorted_key_list;
828 class MapKeyComparator {
830 bool operator()(const MapKey& a, const MapKey& b) const {
831 GOOGLE_DCHECK(a.type() == b.type());
833 #define CASE_TYPE(CppType, CamelCppType) \
834 case FieldDescriptor::CPPTYPE_##CppType: { \
835 return a.Get##CamelCppType##Value() < b.Get##CamelCppType##Value(); \
837 CASE_TYPE(STRING, String)
838 CASE_TYPE(INT64, Int64)
839 CASE_TYPE(INT32, Int32)
840 CASE_TYPE(UINT64, UInt64)
841 CASE_TYPE(UINT32, UInt32)
842 CASE_TYPE(BOOL, Bool)
846 GOOGLE_LOG(DFATAL) << "Invalid key for map field.";
853 static void SerializeMapEntry(const FieldDescriptor* field, const MapKey& key,
854 const MapValueRef& value,
855 io::CodedOutputStream* output) {
856 const FieldDescriptor* key_field = field->message_type()->field(0);
857 const FieldDescriptor* value_field = field->message_type()->field(1);
859 WireFormatLite::WriteTag(field->number(),
860 WireFormatLite::WIRETYPE_LENGTH_DELIMITED, output);
861 size_t size = kMapEntryTagByteSize;
862 size += MapKeyDataOnlyByteSize(key_field, key);
863 size += MapValueRefDataOnlyByteSize(value_field, value);
864 output->WriteVarint32(size);
865 SerializeMapKeyWithCachedSizes(key_field, key, output);
866 SerializeMapValueRefWithCachedSizes(value_field, value, output);
869 void WireFormat::SerializeFieldWithCachedSizes(const FieldDescriptor* field,
870 const Message& message,
871 io::CodedOutputStream* output) {
872 const Reflection* message_reflection = message.GetReflection();
874 if (field->is_extension() &&
875 field->containing_type()->options().message_set_wire_format() &&
876 field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
877 !field->is_repeated()) {
878 SerializeMessageSetItemWithCachedSizes(field, message, output);
882 // For map fields, we can use either repeated field reflection or map
883 // reflection. Our choice has some subtle effects. If we use repeated field
884 // reflection here, then the repeated field representation becomes
885 // authoritative for this field: any existing references that came from map
886 // reflection remain valid for reading, but mutations to them are lost and
887 // will be overwritten next time we call map reflection!
889 // So far this mainly affects Python, which keeps long-term references to map
890 // values around, and always uses map reflection. See: b/35918691
892 // Here we choose to use map reflection API as long as the internal
893 // map is valid. In this way, the serialization doesn't change map field's
894 // internal state and existing references that came from map reflection remain
895 // valid for both reading and writing.
896 if (field->is_map()) {
897 const MapFieldBase* map_field =
898 message_reflection->GetMapData(message, field);
899 if (map_field->IsMapValid()) {
900 if (output->IsSerializationDeterministic()) {
901 std::vector<MapKey> sorted_key_list =
902 MapKeySorter::SortKey(message, message_reflection, field);
903 for (std::vector<MapKey>::iterator it = sorted_key_list.begin();
904 it != sorted_key_list.end(); ++it) {
905 MapValueRef map_value;
906 message_reflection->InsertOrLookupMapValue(
907 const_cast<Message*>(&message), field, *it, &map_value);
908 SerializeMapEntry(field, *it, map_value, output);
911 for (MapIterator it = message_reflection->MapBegin(
912 const_cast<Message*>(&message), field);
914 message_reflection->MapEnd(const_cast<Message*>(&message), field);
916 SerializeMapEntry(field, it.GetKey(), it.GetValueRef(), output);
926 if (field->is_repeated()) {
927 count = message_reflection->FieldSize(message, field);
928 } else if (field->containing_type()->options().map_entry()) {
929 // Map entry fields always need to be serialized.
931 } else if (message_reflection->HasField(message, field)) {
935 // map_entries is for maps that'll be deterministically serialized.
936 std::vector<const Message*> map_entries;
937 if (count > 1 && field->is_map() && output->IsSerializationDeterministic()) {
939 DynamicMapSorter::Sort(message, count, message_reflection, field);
942 const bool is_packed = field->is_packed();
943 if (is_packed && count > 0) {
944 WireFormatLite::WriteTag(field->number(),
945 WireFormatLite::WIRETYPE_LENGTH_DELIMITED, output);
946 const size_t data_size = FieldDataOnlyByteSize(field, message);
947 output->WriteVarint32(data_size);
950 for (int j = 0; j < count; j++) {
951 switch (field->type()) {
952 #define HANDLE_PRIMITIVE_TYPE(TYPE, CPPTYPE, TYPE_METHOD, CPPTYPE_METHOD) \
953 case FieldDescriptor::TYPE_##TYPE: { \
954 const CPPTYPE value = \
955 field->is_repeated() \
956 ? message_reflection->GetRepeated##CPPTYPE_METHOD(message, field, \
958 : message_reflection->Get##CPPTYPE_METHOD(message, field); \
960 WireFormatLite::Write##TYPE_METHOD##NoTag(value, output); \
962 WireFormatLite::Write##TYPE_METHOD(field->number(), value, output); \
967 HANDLE_PRIMITIVE_TYPE(INT32, int32, Int32, Int32)
968 HANDLE_PRIMITIVE_TYPE(INT64, int64, Int64, Int64)
969 HANDLE_PRIMITIVE_TYPE(SINT32, int32, SInt32, Int32)
970 HANDLE_PRIMITIVE_TYPE(SINT64, int64, SInt64, Int64)
971 HANDLE_PRIMITIVE_TYPE(UINT32, uint32, UInt32, UInt32)
972 HANDLE_PRIMITIVE_TYPE(UINT64, uint64, UInt64, UInt64)
974 HANDLE_PRIMITIVE_TYPE(FIXED32, uint32, Fixed32, UInt32)
975 HANDLE_PRIMITIVE_TYPE(FIXED64, uint64, Fixed64, UInt64)
976 HANDLE_PRIMITIVE_TYPE(SFIXED32, int32, SFixed32, Int32)
977 HANDLE_PRIMITIVE_TYPE(SFIXED64, int64, SFixed64, Int64)
979 HANDLE_PRIMITIVE_TYPE(FLOAT, float, Float, Float)
980 HANDLE_PRIMITIVE_TYPE(DOUBLE, double, Double, Double)
982 HANDLE_PRIMITIVE_TYPE(BOOL, bool, Bool, Bool)
983 #undef HANDLE_PRIMITIVE_TYPE
985 #define HANDLE_TYPE(TYPE, TYPE_METHOD, CPPTYPE_METHOD) \
986 case FieldDescriptor::TYPE_##TYPE: \
987 WireFormatLite::Write##TYPE_METHOD( \
989 field->is_repeated() \
990 ? (map_entries.empty() \
991 ? message_reflection->GetRepeated##CPPTYPE_METHOD(message, \
994 : message_reflection->Get##CPPTYPE_METHOD(message, field), \
998 HANDLE_TYPE(GROUP, Group, Message)
999 HANDLE_TYPE(MESSAGE, Message, Message)
1002 case FieldDescriptor::TYPE_ENUM: {
1003 const EnumValueDescriptor* value =
1004 field->is_repeated()
1005 ? message_reflection->GetRepeatedEnum(message, field, j)
1006 : message_reflection->GetEnum(message, field);
1008 WireFormatLite::WriteEnumNoTag(value->number(), output);
1010 WireFormatLite::WriteEnum(field->number(), value->number(), output);
1015 // Handle strings separately so that we can get string references
1016 // instead of copying.
1017 case FieldDescriptor::TYPE_STRING: {
1018 bool strict_utf8_check = StrictUtf8Check(field);
1019 std::string scratch;
1020 const std::string& value =
1021 field->is_repeated()
1022 ? message_reflection->GetRepeatedStringReference(message, field,
1024 : message_reflection->GetStringReference(message, field,
1026 if (strict_utf8_check) {
1027 WireFormatLite::VerifyUtf8String(value.data(), value.length(),
1028 WireFormatLite::SERIALIZE,
1029 field->full_name().c_str());
1031 VerifyUTF8StringNamedField(value.data(), value.length(), SERIALIZE,
1032 field->full_name().c_str());
1034 WireFormatLite::WriteString(field->number(), value, output);
1038 case FieldDescriptor::TYPE_BYTES: {
1039 std::string scratch;
1040 const std::string& value =
1041 field->is_repeated()
1042 ? message_reflection->GetRepeatedStringReference(message, field,
1044 : message_reflection->GetStringReference(message, field,
1046 WireFormatLite::WriteBytes(field->number(), value, output);
1053 void WireFormat::SerializeMessageSetItemWithCachedSizes(
1054 const FieldDescriptor* field, const Message& message,
1055 io::CodedOutputStream* output) {
1056 const Reflection* message_reflection = message.GetReflection();
1059 output->WriteVarint32(WireFormatLite::kMessageSetItemStartTag);
1062 output->WriteVarint32(WireFormatLite::kMessageSetTypeIdTag);
1063 output->WriteVarint32(field->number());
1066 output->WriteVarint32(WireFormatLite::kMessageSetMessageTag);
1068 const Message& sub_message = message_reflection->GetMessage(message, field);
1069 output->WriteVarint32(sub_message.GetCachedSize());
1070 sub_message.SerializeWithCachedSizes(output);
1073 output->WriteVarint32(WireFormatLite::kMessageSetItemEndTag);
1076 // ===================================================================
1078 size_t WireFormat::ByteSize(const Message& message) {
1079 const Descriptor* descriptor = message.GetDescriptor();
1080 const Reflection* message_reflection = message.GetReflection();
1082 size_t our_size = 0;
1084 std::vector<const FieldDescriptor*> fields;
1086 // Fields of map entry should always be serialized.
1087 if (descriptor->options().map_entry()) {
1088 for (int i = 0; i < descriptor->field_count(); i++) {
1089 fields.push_back(descriptor->field(i));
1092 message_reflection->ListFields(message, &fields);
1095 for (int i = 0; i < fields.size(); i++) {
1096 our_size += FieldByteSize(fields[i], message);
1099 if (descriptor->options().message_set_wire_format()) {
1100 our_size += ComputeUnknownMessageSetItemsSize(
1101 message_reflection->GetUnknownFields(message));
1104 ComputeUnknownFieldsSize(message_reflection->GetUnknownFields(message));
1110 size_t WireFormat::FieldByteSize(const FieldDescriptor* field,
1111 const Message& message) {
1112 const Reflection* message_reflection = message.GetReflection();
1114 if (field->is_extension() &&
1115 field->containing_type()->options().message_set_wire_format() &&
1116 field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
1117 !field->is_repeated()) {
1118 return MessageSetItemByteSize(field, message);
1122 if (field->is_repeated()) {
1123 count = FromIntSize(message_reflection->FieldSize(message, field));
1124 } else if (field->containing_type()->options().map_entry()) {
1125 // Map entry fields always need to be serialized.
1127 } else if (message_reflection->HasField(message, field)) {
1131 const size_t data_size = FieldDataOnlyByteSize(field, message);
1132 size_t our_size = data_size;
1133 if (field->is_packed()) {
1134 if (data_size > 0) {
1135 // Packed fields get serialized like a string, not their native type.
1136 // Technically this doesn't really matter; the size only changes if it's
1138 our_size += TagSize(field->number(), FieldDescriptor::TYPE_STRING);
1139 our_size += io::CodedOutputStream::VarintSize32(data_size);
1142 our_size += count * TagSize(field->number(), field->type());
1147 static size_t MapKeyDataOnlyByteSize(const FieldDescriptor* field,
1148 const MapKey& value) {
1149 GOOGLE_DCHECK_EQ(FieldDescriptor::TypeToCppType(field->type()), value.type());
1150 switch (field->type()) {
1151 case FieldDescriptor::TYPE_DOUBLE:
1152 case FieldDescriptor::TYPE_FLOAT:
1153 case FieldDescriptor::TYPE_GROUP:
1154 case FieldDescriptor::TYPE_MESSAGE:
1155 case FieldDescriptor::TYPE_BYTES:
1156 case FieldDescriptor::TYPE_ENUM:
1157 GOOGLE_LOG(FATAL) << "Unsupported";
1159 #define CASE_TYPE(FieldType, CamelFieldType, CamelCppType) \
1160 case FieldDescriptor::TYPE_##FieldType: \
1161 return WireFormatLite::CamelFieldType##Size( \
1162 value.Get##CamelCppType##Value());
1164 #define FIXED_CASE_TYPE(FieldType, CamelFieldType) \
1165 case FieldDescriptor::TYPE_##FieldType: \
1166 return WireFormatLite::k##CamelFieldType##Size;
1168 CASE_TYPE(INT32, Int32, Int32);
1169 CASE_TYPE(INT64, Int64, Int64);
1170 CASE_TYPE(UINT32, UInt32, UInt32);
1171 CASE_TYPE(UINT64, UInt64, UInt64);
1172 CASE_TYPE(SINT32, SInt32, Int32);
1173 CASE_TYPE(SINT64, SInt64, Int64);
1174 CASE_TYPE(STRING, String, String);
1175 FIXED_CASE_TYPE(FIXED32, Fixed32);
1176 FIXED_CASE_TYPE(FIXED64, Fixed64);
1177 FIXED_CASE_TYPE(SFIXED32, SFixed32);
1178 FIXED_CASE_TYPE(SFIXED64, SFixed64);
1179 FIXED_CASE_TYPE(BOOL, Bool);
1182 #undef FIXED_CASE_TYPE
1184 GOOGLE_LOG(FATAL) << "Cannot get here";
1188 static size_t MapValueRefDataOnlyByteSize(const FieldDescriptor* field,
1189 const MapValueRef& value) {
1190 switch (field->type()) {
1191 case FieldDescriptor::TYPE_GROUP:
1192 GOOGLE_LOG(FATAL) << "Unsupported";
1194 #define CASE_TYPE(FieldType, CamelFieldType, CamelCppType) \
1195 case FieldDescriptor::TYPE_##FieldType: \
1196 return WireFormatLite::CamelFieldType##Size( \
1197 value.Get##CamelCppType##Value());
1199 #define FIXED_CASE_TYPE(FieldType, CamelFieldType) \
1200 case FieldDescriptor::TYPE_##FieldType: \
1201 return WireFormatLite::k##CamelFieldType##Size;
1203 CASE_TYPE(INT32, Int32, Int32);
1204 CASE_TYPE(INT64, Int64, Int64);
1205 CASE_TYPE(UINT32, UInt32, UInt32);
1206 CASE_TYPE(UINT64, UInt64, UInt64);
1207 CASE_TYPE(SINT32, SInt32, Int32);
1208 CASE_TYPE(SINT64, SInt64, Int64);
1209 CASE_TYPE(STRING, String, String);
1210 CASE_TYPE(BYTES, Bytes, String);
1211 CASE_TYPE(ENUM, Enum, Enum);
1212 CASE_TYPE(MESSAGE, Message, Message);
1213 FIXED_CASE_TYPE(FIXED32, Fixed32);
1214 FIXED_CASE_TYPE(FIXED64, Fixed64);
1215 FIXED_CASE_TYPE(SFIXED32, SFixed32);
1216 FIXED_CASE_TYPE(SFIXED64, SFixed64);
1217 FIXED_CASE_TYPE(DOUBLE, Double);
1218 FIXED_CASE_TYPE(FLOAT, Float);
1219 FIXED_CASE_TYPE(BOOL, Bool);
1222 #undef FIXED_CASE_TYPE
1224 GOOGLE_LOG(FATAL) << "Cannot get here";
1228 size_t WireFormat::FieldDataOnlyByteSize(const FieldDescriptor* field,
1229 const Message& message) {
1230 const Reflection* message_reflection = message.GetReflection();
1232 size_t data_size = 0;
1234 if (field->is_map()) {
1235 const MapFieldBase* map_field =
1236 message_reflection->GetMapData(message, field);
1237 if (map_field->IsMapValid()) {
1238 MapIterator iter(const_cast<Message*>(&message), field);
1239 MapIterator end(const_cast<Message*>(&message), field);
1240 const FieldDescriptor* key_field = field->message_type()->field(0);
1241 const FieldDescriptor* value_field = field->message_type()->field(1);
1242 for (map_field->MapBegin(&iter), map_field->MapEnd(&end); iter != end;
1244 size_t size = kMapEntryTagByteSize;
1245 size += MapKeyDataOnlyByteSize(key_field, iter.GetKey());
1246 size += MapValueRefDataOnlyByteSize(value_field, iter.GetValueRef());
1247 data_size += WireFormatLite::LengthDelimitedSize(size);
1254 if (field->is_repeated()) {
1256 internal::FromIntSize(message_reflection->FieldSize(message, field));
1257 } else if (field->containing_type()->options().map_entry()) {
1258 // Map entry fields always need to be serialized.
1260 } else if (message_reflection->HasField(message, field)) {
1264 switch (field->type()) {
1265 #define HANDLE_TYPE(TYPE, TYPE_METHOD, CPPTYPE_METHOD) \
1266 case FieldDescriptor::TYPE_##TYPE: \
1267 if (field->is_repeated()) { \
1268 for (int j = 0; j < count; j++) { \
1269 data_size += WireFormatLite::TYPE_METHOD##Size( \
1270 message_reflection->GetRepeated##CPPTYPE_METHOD(message, field, \
1274 data_size += WireFormatLite::TYPE_METHOD##Size( \
1275 message_reflection->Get##CPPTYPE_METHOD(message, field)); \
1279 #define HANDLE_FIXED_TYPE(TYPE, TYPE_METHOD) \
1280 case FieldDescriptor::TYPE_##TYPE: \
1281 data_size += count * WireFormatLite::k##TYPE_METHOD##Size; \
1284 HANDLE_TYPE(INT32, Int32, Int32)
1285 HANDLE_TYPE(INT64, Int64, Int64)
1286 HANDLE_TYPE(SINT32, SInt32, Int32)
1287 HANDLE_TYPE(SINT64, SInt64, Int64)
1288 HANDLE_TYPE(UINT32, UInt32, UInt32)
1289 HANDLE_TYPE(UINT64, UInt64, UInt64)
1291 HANDLE_FIXED_TYPE(FIXED32, Fixed32)
1292 HANDLE_FIXED_TYPE(FIXED64, Fixed64)
1293 HANDLE_FIXED_TYPE(SFIXED32, SFixed32)
1294 HANDLE_FIXED_TYPE(SFIXED64, SFixed64)
1296 HANDLE_FIXED_TYPE(FLOAT, Float)
1297 HANDLE_FIXED_TYPE(DOUBLE, Double)
1299 HANDLE_FIXED_TYPE(BOOL, Bool)
1301 HANDLE_TYPE(GROUP, Group, Message)
1302 HANDLE_TYPE(MESSAGE, Message, Message)
1304 #undef HANDLE_FIXED_TYPE
1306 case FieldDescriptor::TYPE_ENUM: {
1307 if (field->is_repeated()) {
1308 for (int j = 0; j < count; j++) {
1309 data_size += WireFormatLite::EnumSize(
1310 message_reflection->GetRepeatedEnum(message, field, j)->number());
1313 data_size += WireFormatLite::EnumSize(
1314 message_reflection->GetEnum(message, field)->number());
1319 // Handle strings separately so that we can get string references
1320 // instead of copying.
1321 case FieldDescriptor::TYPE_STRING:
1322 case FieldDescriptor::TYPE_BYTES: {
1323 for (int j = 0; j < count; j++) {
1324 std::string scratch;
1325 const std::string& value =
1326 field->is_repeated()
1327 ? message_reflection->GetRepeatedStringReference(message, field,
1329 : message_reflection->GetStringReference(message, field,
1331 data_size += WireFormatLite::StringSize(value);
1339 size_t WireFormat::MessageSetItemByteSize(const FieldDescriptor* field,
1340 const Message& message) {
1341 const Reflection* message_reflection = message.GetReflection();
1343 size_t our_size = WireFormatLite::kMessageSetItemTagsSize;
1346 our_size += io::CodedOutputStream::VarintSize32(field->number());
1349 const Message& sub_message = message_reflection->GetMessage(message, field);
1350 size_t message_size = sub_message.ByteSizeLong();
1352 our_size += io::CodedOutputStream::VarintSize32(message_size);
1353 our_size += message_size;
1358 } // namespace internal
1359 } // namespace protobuf
1360 } // namespace google