Line data Source code
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 <stack>
36 : #include <string>
37 : #include <vector>
38 :
39 : #include <google/protobuf/wire_format.h>
40 :
41 : #include <google/protobuf/stubs/common.h>
42 : #include <google/protobuf/stubs/stringprintf.h>
43 : #include <google/protobuf/descriptor.h>
44 : #include <google/protobuf/wire_format_lite_inl.h>
45 : #include <google/protobuf/descriptor.pb.h>
46 : #include <google/protobuf/io/coded_stream.h>
47 : #include <google/protobuf/io/zero_copy_stream.h>
48 : #include <google/protobuf/io/zero_copy_stream_impl.h>
49 : #include <google/protobuf/unknown_field_set.h>
50 :
51 :
52 :
53 : namespace google {
54 : namespace protobuf {
55 : namespace internal {
56 :
57 : namespace {
58 :
59 : // This function turns out to be convenient when using some macros later.
60 : inline int GetEnumNumber(const EnumValueDescriptor* descriptor) {
61 : return descriptor->number();
62 : }
63 :
64 : } // anonymous namespace
65 :
66 : // ===================================================================
67 :
68 0 : bool UnknownFieldSetFieldSkipper::SkipField(
69 : io::CodedInputStream* input, uint32 tag) {
70 0 : return WireFormat::SkipField(input, tag, unknown_fields_);
71 : }
72 :
73 0 : bool UnknownFieldSetFieldSkipper::SkipMessage(io::CodedInputStream* input) {
74 0 : return WireFormat::SkipMessage(input, unknown_fields_);
75 : }
76 :
77 0 : void UnknownFieldSetFieldSkipper::SkipUnknownEnum(
78 : int field_number, int value) {
79 0 : unknown_fields_->AddVarint(field_number, value);
80 0 : }
81 :
82 0 : bool WireFormat::SkipField(io::CodedInputStream* input, uint32 tag,
83 : UnknownFieldSet* unknown_fields) {
84 0 : int number = WireFormatLite::GetTagFieldNumber(tag);
85 :
86 0 : switch (WireFormatLite::GetTagWireType(tag)) {
87 : case WireFormatLite::WIRETYPE_VARINT: {
88 : uint64 value;
89 0 : if (!input->ReadVarint64(&value)) return false;
90 0 : if (unknown_fields != NULL) unknown_fields->AddVarint(number, value);
91 0 : return true;
92 : }
93 : case WireFormatLite::WIRETYPE_FIXED64: {
94 : uint64 value;
95 0 : if (!input->ReadLittleEndian64(&value)) return false;
96 0 : if (unknown_fields != NULL) unknown_fields->AddFixed64(number, value);
97 0 : return true;
98 : }
99 : case WireFormatLite::WIRETYPE_LENGTH_DELIMITED: {
100 : uint32 length;
101 0 : if (!input->ReadVarint32(&length)) return false;
102 0 : if (unknown_fields == NULL) {
103 0 : if (!input->Skip(length)) return false;
104 : } else {
105 0 : if (!input->ReadString(unknown_fields->AddLengthDelimited(number),
106 : length)) {
107 0 : return false;
108 : }
109 : }
110 0 : return true;
111 : }
112 : case WireFormatLite::WIRETYPE_START_GROUP: {
113 0 : if (!input->IncrementRecursionDepth()) return false;
114 0 : if (!SkipMessage(input, (unknown_fields == NULL) ?
115 : NULL : unknown_fields->AddGroup(number))) {
116 0 : return false;
117 : }
118 0 : input->DecrementRecursionDepth();
119 : // Check that the ending tag matched the starting tag.
120 0 : if (!input->LastTagWas(WireFormatLite::MakeTag(
121 : WireFormatLite::GetTagFieldNumber(tag),
122 : WireFormatLite::WIRETYPE_END_GROUP))) {
123 0 : return false;
124 : }
125 0 : return true;
126 : }
127 : case WireFormatLite::WIRETYPE_END_GROUP: {
128 0 : return false;
129 : }
130 : case WireFormatLite::WIRETYPE_FIXED32: {
131 : uint32 value;
132 0 : if (!input->ReadLittleEndian32(&value)) return false;
133 0 : if (unknown_fields != NULL) unknown_fields->AddFixed32(number, value);
134 0 : return true;
135 : }
136 : default: {
137 0 : return false;
138 : }
139 : }
140 : }
141 :
142 0 : bool WireFormat::SkipMessage(io::CodedInputStream* input,
143 : UnknownFieldSet* unknown_fields) {
144 : while(true) {
145 0 : uint32 tag = input->ReadTag();
146 0 : if (tag == 0) {
147 : // End of input. This is a valid place to end, so return true.
148 0 : return true;
149 : }
150 :
151 0 : WireFormatLite::WireType wire_type = WireFormatLite::GetTagWireType(tag);
152 :
153 0 : if (wire_type == WireFormatLite::WIRETYPE_END_GROUP) {
154 : // Must be the end of the message.
155 0 : return true;
156 : }
157 :
158 0 : if (!SkipField(input, tag, unknown_fields)) return false;
159 0 : }
160 : }
161 :
162 0 : void WireFormat::SerializeUnknownFields(const UnknownFieldSet& unknown_fields,
163 : io::CodedOutputStream* output) {
164 0 : for (int i = 0; i < unknown_fields.field_count(); i++) {
165 0 : const UnknownField& field = unknown_fields.field(i);
166 0 : switch (field.type()) {
167 : case UnknownField::TYPE_VARINT:
168 0 : output->WriteVarint32(WireFormatLite::MakeTag(field.number(),
169 0 : WireFormatLite::WIRETYPE_VARINT));
170 0 : output->WriteVarint64(field.varint());
171 0 : break;
172 : case UnknownField::TYPE_FIXED32:
173 0 : output->WriteVarint32(WireFormatLite::MakeTag(field.number(),
174 0 : WireFormatLite::WIRETYPE_FIXED32));
175 0 : output->WriteLittleEndian32(field.fixed32());
176 0 : break;
177 : case UnknownField::TYPE_FIXED64:
178 0 : output->WriteVarint32(WireFormatLite::MakeTag(field.number(),
179 0 : WireFormatLite::WIRETYPE_FIXED64));
180 0 : output->WriteLittleEndian64(field.fixed64());
181 0 : break;
182 : case UnknownField::TYPE_LENGTH_DELIMITED:
183 0 : output->WriteVarint32(WireFormatLite::MakeTag(field.number(),
184 0 : WireFormatLite::WIRETYPE_LENGTH_DELIMITED));
185 0 : output->WriteVarint32(field.length_delimited().size());
186 0 : output->WriteRawMaybeAliased(field.length_delimited().data(),
187 0 : field.length_delimited().size());
188 0 : break;
189 : case UnknownField::TYPE_GROUP:
190 0 : output->WriteVarint32(WireFormatLite::MakeTag(field.number(),
191 0 : WireFormatLite::WIRETYPE_START_GROUP));
192 0 : SerializeUnknownFields(field.group(), output);
193 0 : output->WriteVarint32(WireFormatLite::MakeTag(field.number(),
194 0 : WireFormatLite::WIRETYPE_END_GROUP));
195 0 : break;
196 : }
197 : }
198 0 : }
199 :
200 0 : uint8* WireFormat::SerializeUnknownFieldsToArray(
201 : const UnknownFieldSet& unknown_fields,
202 : uint8* target) {
203 0 : for (int i = 0; i < unknown_fields.field_count(); i++) {
204 0 : const UnknownField& field = unknown_fields.field(i);
205 :
206 0 : switch (field.type()) {
207 : case UnknownField::TYPE_VARINT:
208 0 : target = WireFormatLite::WriteInt64ToArray(
209 0 : field.number(), field.varint(), target);
210 0 : break;
211 : case UnknownField::TYPE_FIXED32:
212 0 : target = WireFormatLite::WriteFixed32ToArray(
213 0 : field.number(), field.fixed32(), target);
214 0 : break;
215 : case UnknownField::TYPE_FIXED64:
216 0 : target = WireFormatLite::WriteFixed64ToArray(
217 0 : field.number(), field.fixed64(), target);
218 0 : break;
219 : case UnknownField::TYPE_LENGTH_DELIMITED:
220 0 : target = WireFormatLite::WriteBytesToArray(
221 0 : field.number(), field.length_delimited(), target);
222 0 : break;
223 : case UnknownField::TYPE_GROUP:
224 0 : target = WireFormatLite::WriteTagToArray(
225 0 : field.number(), WireFormatLite::WIRETYPE_START_GROUP, target);
226 0 : target = SerializeUnknownFieldsToArray(field.group(), target);
227 0 : target = WireFormatLite::WriteTagToArray(
228 0 : field.number(), WireFormatLite::WIRETYPE_END_GROUP, target);
229 0 : break;
230 : }
231 : }
232 0 : return target;
233 : }
234 :
235 0 : void WireFormat::SerializeUnknownMessageSetItems(
236 : const UnknownFieldSet& unknown_fields,
237 : io::CodedOutputStream* output) {
238 0 : for (int i = 0; i < unknown_fields.field_count(); i++) {
239 0 : const UnknownField& field = unknown_fields.field(i);
240 : // The only unknown fields that are allowed to exist in a MessageSet are
241 : // messages, which are length-delimited.
242 0 : if (field.type() == UnknownField::TYPE_LENGTH_DELIMITED) {
243 : // Start group.
244 0 : output->WriteVarint32(WireFormatLite::kMessageSetItemStartTag);
245 :
246 : // Write type ID.
247 0 : output->WriteVarint32(WireFormatLite::kMessageSetTypeIdTag);
248 0 : output->WriteVarint32(field.number());
249 :
250 : // Write message.
251 0 : output->WriteVarint32(WireFormatLite::kMessageSetMessageTag);
252 0 : field.SerializeLengthDelimitedNoTag(output);
253 :
254 : // End group.
255 0 : output->WriteVarint32(WireFormatLite::kMessageSetItemEndTag);
256 : }
257 : }
258 0 : }
259 :
260 0 : uint8* WireFormat::SerializeUnknownMessageSetItemsToArray(
261 : const UnknownFieldSet& unknown_fields,
262 : uint8* target) {
263 0 : for (int i = 0; i < unknown_fields.field_count(); i++) {
264 0 : const UnknownField& field = unknown_fields.field(i);
265 :
266 : // The only unknown fields that are allowed to exist in a MessageSet are
267 : // messages, which are length-delimited.
268 0 : if (field.type() == UnknownField::TYPE_LENGTH_DELIMITED) {
269 : // Start group.
270 : target = io::CodedOutputStream::WriteTagToArray(
271 0 : WireFormatLite::kMessageSetItemStartTag, target);
272 :
273 : // Write type ID.
274 : target = io::CodedOutputStream::WriteTagToArray(
275 0 : WireFormatLite::kMessageSetTypeIdTag, target);
276 0 : target = io::CodedOutputStream::WriteVarint32ToArray(
277 0 : field.number(), target);
278 :
279 : // Write message.
280 : target = io::CodedOutputStream::WriteTagToArray(
281 0 : WireFormatLite::kMessageSetMessageTag, target);
282 0 : target = field.SerializeLengthDelimitedNoTagToArray(target);
283 :
284 : // End group.
285 : target = io::CodedOutputStream::WriteTagToArray(
286 0 : WireFormatLite::kMessageSetItemEndTag, target);
287 : }
288 : }
289 :
290 0 : return target;
291 : }
292 :
293 0 : int WireFormat::ComputeUnknownFieldsSize(
294 : const UnknownFieldSet& unknown_fields) {
295 0 : int size = 0;
296 0 : for (int i = 0; i < unknown_fields.field_count(); i++) {
297 0 : const UnknownField& field = unknown_fields.field(i);
298 :
299 0 : switch (field.type()) {
300 : case UnknownField::TYPE_VARINT:
301 0 : size += io::CodedOutputStream::VarintSize32(
302 : WireFormatLite::MakeTag(field.number(),
303 : WireFormatLite::WIRETYPE_VARINT));
304 0 : size += io::CodedOutputStream::VarintSize64(field.varint());
305 0 : break;
306 : case UnknownField::TYPE_FIXED32:
307 0 : size += io::CodedOutputStream::VarintSize32(
308 : WireFormatLite::MakeTag(field.number(),
309 : WireFormatLite::WIRETYPE_FIXED32));
310 0 : size += sizeof(int32);
311 0 : break;
312 : case UnknownField::TYPE_FIXED64:
313 0 : size += io::CodedOutputStream::VarintSize32(
314 : WireFormatLite::MakeTag(field.number(),
315 : WireFormatLite::WIRETYPE_FIXED64));
316 0 : size += sizeof(int64);
317 0 : break;
318 : case UnknownField::TYPE_LENGTH_DELIMITED:
319 0 : size += io::CodedOutputStream::VarintSize32(
320 : WireFormatLite::MakeTag(field.number(),
321 : WireFormatLite::WIRETYPE_LENGTH_DELIMITED));
322 0 : size += io::CodedOutputStream::VarintSize32(
323 0 : field.length_delimited().size());
324 0 : size += field.length_delimited().size();
325 0 : break;
326 : case UnknownField::TYPE_GROUP:
327 0 : size += io::CodedOutputStream::VarintSize32(
328 : WireFormatLite::MakeTag(field.number(),
329 : WireFormatLite::WIRETYPE_START_GROUP));
330 0 : size += ComputeUnknownFieldsSize(field.group());
331 0 : size += io::CodedOutputStream::VarintSize32(
332 : WireFormatLite::MakeTag(field.number(),
333 : WireFormatLite::WIRETYPE_END_GROUP));
334 0 : break;
335 : }
336 : }
337 :
338 0 : return size;
339 : }
340 :
341 0 : int WireFormat::ComputeUnknownMessageSetItemsSize(
342 : const UnknownFieldSet& unknown_fields) {
343 0 : int size = 0;
344 0 : for (int i = 0; i < unknown_fields.field_count(); i++) {
345 0 : const UnknownField& field = unknown_fields.field(i);
346 :
347 : // The only unknown fields that are allowed to exist in a MessageSet are
348 : // messages, which are length-delimited.
349 0 : if (field.type() == UnknownField::TYPE_LENGTH_DELIMITED) {
350 0 : size += WireFormatLite::kMessageSetItemTagsSize;
351 0 : size += io::CodedOutputStream::VarintSize32(field.number());
352 :
353 0 : int field_size = field.GetLengthDelimitedSize();
354 0 : size += io::CodedOutputStream::VarintSize32(field_size);
355 0 : size += field_size;
356 : }
357 : }
358 :
359 0 : return size;
360 : }
361 :
362 : // ===================================================================
363 :
364 0 : bool WireFormat::ParseAndMergePartial(io::CodedInputStream* input,
365 : Message* message) {
366 0 : const Descriptor* descriptor = message->GetDescriptor();
367 0 : const Reflection* message_reflection = message->GetReflection();
368 :
369 : while(true) {
370 0 : uint32 tag = input->ReadTag();
371 0 : if (tag == 0) {
372 : // End of input. This is a valid place to end, so return true.
373 0 : return true;
374 : }
375 :
376 0 : if (WireFormatLite::GetTagWireType(tag) ==
377 : WireFormatLite::WIRETYPE_END_GROUP) {
378 : // Must be the end of the message.
379 0 : return true;
380 : }
381 :
382 0 : const FieldDescriptor* field = NULL;
383 :
384 0 : if (descriptor != NULL) {
385 0 : int field_number = WireFormatLite::GetTagFieldNumber(tag);
386 0 : field = descriptor->FindFieldByNumber(field_number);
387 :
388 : // If that failed, check if the field is an extension.
389 0 : if (field == NULL && descriptor->IsExtensionNumber(field_number)) {
390 0 : if (input->GetExtensionPool() == NULL) {
391 0 : field = message_reflection->FindKnownExtensionByNumber(field_number);
392 : } else {
393 : field = input->GetExtensionPool()
394 0 : ->FindExtensionByNumber(descriptor, field_number);
395 : }
396 : }
397 :
398 : // If that failed, but we're a MessageSet, and this is the tag for a
399 : // MessageSet item, then parse that.
400 0 : if (field == NULL &&
401 0 : descriptor->options().message_set_wire_format() &&
402 : tag == WireFormatLite::kMessageSetItemStartTag) {
403 0 : if (!ParseAndMergeMessageSetItem(input, message)) {
404 0 : return false;
405 : }
406 0 : continue; // Skip ParseAndMergeField(); already taken care of.
407 : }
408 : }
409 :
410 0 : if (!ParseAndMergeField(tag, field, message, input)) {
411 0 : return false;
412 : }
413 0 : }
414 : }
415 :
416 0 : bool WireFormat::SkipMessageSetField(io::CodedInputStream* input,
417 : uint32 field_number,
418 : UnknownFieldSet* unknown_fields) {
419 : uint32 length;
420 0 : if (!input->ReadVarint32(&length)) return false;
421 0 : return input->ReadString(
422 0 : unknown_fields->AddLengthDelimited(field_number), length);
423 : }
424 :
425 0 : bool WireFormat::ParseAndMergeMessageSetField(uint32 field_number,
426 : const FieldDescriptor* field,
427 : Message* message,
428 : io::CodedInputStream* input) {
429 0 : const Reflection* message_reflection = message->GetReflection();
430 0 : if (field == NULL) {
431 : // We store unknown MessageSet extensions as groups.
432 0 : return SkipMessageSetField(
433 0 : input, field_number, message_reflection->MutableUnknownFields(message));
434 0 : } else if (field->is_repeated() ||
435 0 : field->type() != FieldDescriptor::TYPE_MESSAGE) {
436 : // This shouldn't happen as we only allow optional message extensions to
437 : // MessageSet.
438 0 : GOOGLE_LOG(ERROR) << "Extensions of MessageSets must be optional messages.";
439 0 : return false;
440 : } else {
441 0 : Message* sub_message = message_reflection->MutableMessage(
442 0 : message, field, input->GetExtensionFactory());
443 0 : return WireFormatLite::ReadMessage(input, sub_message);
444 : }
445 : }
446 :
447 0 : bool WireFormat::ParseAndMergeField(
448 : uint32 tag,
449 : const FieldDescriptor* field, // May be NULL for unknown
450 : Message* message,
451 : io::CodedInputStream* input) {
452 0 : const Reflection* message_reflection = message->GetReflection();
453 :
454 : enum { UNKNOWN, NORMAL_FORMAT, PACKED_FORMAT } value_format;
455 :
456 0 : if (field == NULL) {
457 0 : value_format = UNKNOWN;
458 0 : } else if (WireFormatLite::GetTagWireType(tag) ==
459 0 : WireTypeForFieldType(field->type())) {
460 0 : value_format = NORMAL_FORMAT;
461 0 : } else if (field->is_packable() &&
462 0 : WireFormatLite::GetTagWireType(tag) ==
463 : WireFormatLite::WIRETYPE_LENGTH_DELIMITED) {
464 0 : value_format = PACKED_FORMAT;
465 : } else {
466 : // We don't recognize this field. Either the field number is unknown
467 : // or the wire type doesn't match. Put it in our unknown field set.
468 0 : value_format = UNKNOWN;
469 : }
470 :
471 0 : if (value_format == UNKNOWN) {
472 0 : return SkipField(input, tag,
473 0 : message_reflection->MutableUnknownFields(message));
474 0 : } else if (value_format == PACKED_FORMAT) {
475 : uint32 length;
476 0 : if (!input->ReadVarint32(&length)) return false;
477 0 : io::CodedInputStream::Limit limit = input->PushLimit(length);
478 :
479 0 : switch (field->type()) {
480 : #define HANDLE_PACKED_TYPE(TYPE, CPPTYPE, CPPTYPE_METHOD) \
481 : case FieldDescriptor::TYPE_##TYPE: { \
482 : while (input->BytesUntilLimit() > 0) { \
483 : CPPTYPE value; \
484 : if (!WireFormatLite::ReadPrimitive< \
485 : CPPTYPE, WireFormatLite::TYPE_##TYPE>(input, &value)) \
486 : return false; \
487 : message_reflection->Add##CPPTYPE_METHOD(message, field, value); \
488 : } \
489 : break; \
490 : }
491 :
492 0 : HANDLE_PACKED_TYPE( INT32, int32, Int32)
493 0 : HANDLE_PACKED_TYPE( INT64, int64, Int64)
494 0 : HANDLE_PACKED_TYPE(SINT32, int32, Int32)
495 0 : HANDLE_PACKED_TYPE(SINT64, int64, Int64)
496 0 : HANDLE_PACKED_TYPE(UINT32, uint32, UInt32)
497 0 : HANDLE_PACKED_TYPE(UINT64, uint64, UInt64)
498 :
499 0 : HANDLE_PACKED_TYPE( FIXED32, uint32, UInt32)
500 0 : HANDLE_PACKED_TYPE( FIXED64, uint64, UInt64)
501 0 : HANDLE_PACKED_TYPE(SFIXED32, int32, Int32)
502 0 : HANDLE_PACKED_TYPE(SFIXED64, int64, Int64)
503 :
504 0 : HANDLE_PACKED_TYPE(FLOAT , float , Float )
505 0 : HANDLE_PACKED_TYPE(DOUBLE, double, Double)
506 :
507 0 : HANDLE_PACKED_TYPE(BOOL, bool, Bool)
508 : #undef HANDLE_PACKED_TYPE
509 :
510 : case FieldDescriptor::TYPE_ENUM: {
511 0 : while (input->BytesUntilLimit() > 0) {
512 : int value;
513 0 : if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
514 0 : input, &value)) return false;
515 : const EnumValueDescriptor* enum_value =
516 0 : field->enum_type()->FindValueByNumber(value);
517 0 : if (enum_value != NULL) {
518 0 : message_reflection->AddEnum(message, field, enum_value);
519 : }
520 : }
521 :
522 0 : break;
523 : }
524 :
525 : case FieldDescriptor::TYPE_STRING:
526 : case FieldDescriptor::TYPE_GROUP:
527 : case FieldDescriptor::TYPE_MESSAGE:
528 : case FieldDescriptor::TYPE_BYTES:
529 : // Can't have packed fields of these types: these should be caught by
530 : // the protocol compiler.
531 0 : return false;
532 : break;
533 : }
534 :
535 0 : input->PopLimit(limit);
536 : } else {
537 : // Non-packed value (value_format == NORMAL_FORMAT)
538 0 : switch (field->type()) {
539 : #define HANDLE_TYPE(TYPE, CPPTYPE, CPPTYPE_METHOD) \
540 : case FieldDescriptor::TYPE_##TYPE: { \
541 : CPPTYPE value; \
542 : if (!WireFormatLite::ReadPrimitive< \
543 : CPPTYPE, WireFormatLite::TYPE_##TYPE>(input, &value)) \
544 : return false; \
545 : if (field->is_repeated()) { \
546 : message_reflection->Add##CPPTYPE_METHOD(message, field, value); \
547 : } else { \
548 : message_reflection->Set##CPPTYPE_METHOD(message, field, value); \
549 : } \
550 : break; \
551 : }
552 :
553 0 : HANDLE_TYPE( INT32, int32, Int32)
554 0 : HANDLE_TYPE( INT64, int64, Int64)
555 0 : HANDLE_TYPE(SINT32, int32, Int32)
556 0 : HANDLE_TYPE(SINT64, int64, Int64)
557 0 : HANDLE_TYPE(UINT32, uint32, UInt32)
558 0 : HANDLE_TYPE(UINT64, uint64, UInt64)
559 :
560 0 : HANDLE_TYPE( FIXED32, uint32, UInt32)
561 0 : HANDLE_TYPE( FIXED64, uint64, UInt64)
562 0 : HANDLE_TYPE(SFIXED32, int32, Int32)
563 0 : HANDLE_TYPE(SFIXED64, int64, Int64)
564 :
565 0 : HANDLE_TYPE(FLOAT , float , Float )
566 0 : HANDLE_TYPE(DOUBLE, double, Double)
567 :
568 0 : HANDLE_TYPE(BOOL, bool, Bool)
569 : #undef HANDLE_TYPE
570 :
571 : case FieldDescriptor::TYPE_ENUM: {
572 : int value;
573 0 : if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
574 0 : input, &value)) return false;
575 : const EnumValueDescriptor* enum_value =
576 0 : field->enum_type()->FindValueByNumber(value);
577 0 : if (enum_value != NULL) {
578 0 : if (field->is_repeated()) {
579 0 : message_reflection->AddEnum(message, field, enum_value);
580 : } else {
581 0 : message_reflection->SetEnum(message, field, enum_value);
582 : }
583 : } else {
584 : // The enum value is not one of the known values. Add it to the
585 : // UnknownFieldSet.
586 0 : int64 sign_extended_value = static_cast<int64>(value);
587 0 : message_reflection->MutableUnknownFields(message)
588 0 : ->AddVarint(WireFormatLite::GetTagFieldNumber(tag),
589 0 : sign_extended_value);
590 : }
591 0 : break;
592 : }
593 :
594 : // Handle strings separately so that we can optimize the ctype=CORD case.
595 : case FieldDescriptor::TYPE_STRING: {
596 0 : string value;
597 0 : if (!WireFormatLite::ReadString(input, &value)) return false;
598 0 : VerifyUTF8StringNamedField(value.data(), value.length(), PARSE,
599 0 : field->name().c_str());
600 0 : if (field->is_repeated()) {
601 0 : message_reflection->AddString(message, field, value);
602 : } else {
603 0 : message_reflection->SetString(message, field, value);
604 : }
605 0 : break;
606 : }
607 :
608 : case FieldDescriptor::TYPE_BYTES: {
609 0 : string value;
610 0 : if (!WireFormatLite::ReadBytes(input, &value)) return false;
611 0 : if (field->is_repeated()) {
612 0 : message_reflection->AddString(message, field, value);
613 : } else {
614 0 : message_reflection->SetString(message, field, value);
615 : }
616 0 : break;
617 : }
618 :
619 : case FieldDescriptor::TYPE_GROUP: {
620 : Message* sub_message;
621 0 : if (field->is_repeated()) {
622 0 : sub_message = message_reflection->AddMessage(
623 0 : message, field, input->GetExtensionFactory());
624 : } else {
625 0 : sub_message = message_reflection->MutableMessage(
626 0 : message, field, input->GetExtensionFactory());
627 : }
628 :
629 0 : if (!WireFormatLite::ReadGroup(WireFormatLite::GetTagFieldNumber(tag),
630 : input, sub_message))
631 0 : return false;
632 0 : break;
633 : }
634 :
635 : case FieldDescriptor::TYPE_MESSAGE: {
636 : Message* sub_message;
637 0 : if (field->is_repeated()) {
638 0 : sub_message = message_reflection->AddMessage(
639 0 : message, field, input->GetExtensionFactory());
640 : } else {
641 0 : sub_message = message_reflection->MutableMessage(
642 0 : message, field, input->GetExtensionFactory());
643 : }
644 :
645 0 : if (!WireFormatLite::ReadMessage(input, sub_message)) return false;
646 0 : break;
647 : }
648 : }
649 : }
650 :
651 0 : return true;
652 : }
653 :
654 0 : bool WireFormat::ParseAndMergeMessageSetItem(
655 : io::CodedInputStream* input,
656 : Message* message) {
657 0 : const Reflection* message_reflection = message->GetReflection();
658 :
659 : // This method parses a group which should contain two fields:
660 : // required int32 type_id = 2;
661 : // required data message = 3;
662 :
663 0 : uint32 last_type_id = 0;
664 :
665 : // Once we see a type_id, we'll look up the FieldDescriptor for the
666 : // extension.
667 0 : const FieldDescriptor* field = NULL;
668 :
669 : // If we see message data before the type_id, we'll append it to this so
670 : // we can parse it later.
671 0 : string message_data;
672 :
673 : while (true) {
674 0 : uint32 tag = input->ReadTag();
675 0 : if (tag == 0) return false;
676 :
677 0 : switch (tag) {
678 : case WireFormatLite::kMessageSetTypeIdTag: {
679 : uint32 type_id;
680 0 : if (!input->ReadVarint32(&type_id)) return false;
681 0 : last_type_id = type_id;
682 0 : field = message_reflection->FindKnownExtensionByNumber(type_id);
683 :
684 0 : if (!message_data.empty()) {
685 : // We saw some message data before the type_id. Have to parse it
686 : // now.
687 0 : io::ArrayInputStream raw_input(message_data.data(),
688 0 : message_data.size());
689 0 : io::CodedInputStream sub_input(&raw_input);
690 0 : if (!ParseAndMergeMessageSetField(last_type_id, field, message,
691 : &sub_input)) {
692 0 : return false;
693 : }
694 0 : message_data.clear();
695 : }
696 :
697 0 : break;
698 : }
699 :
700 : case WireFormatLite::kMessageSetMessageTag: {
701 0 : if (last_type_id == 0) {
702 : // We haven't seen a type_id yet. Append this data to message_data.
703 0 : string temp;
704 : uint32 length;
705 0 : if (!input->ReadVarint32(&length)) return false;
706 0 : if (!input->ReadString(&temp, length)) return false;
707 0 : io::StringOutputStream output_stream(&message_data);
708 0 : io::CodedOutputStream coded_output(&output_stream);
709 0 : coded_output.WriteVarint32(length);
710 0 : coded_output.WriteString(temp);
711 : } else {
712 : // Already saw type_id, so we can parse this directly.
713 0 : if (!ParseAndMergeMessageSetField(last_type_id, field, message,
714 : input)) {
715 0 : return false;
716 : }
717 : }
718 :
719 0 : break;
720 : }
721 :
722 : case WireFormatLite::kMessageSetItemEndTag: {
723 0 : return true;
724 : }
725 :
726 : default: {
727 0 : if (!SkipField(input, tag, NULL)) return false;
728 : }
729 : }
730 0 : }
731 : }
732 :
733 : // ===================================================================
734 :
735 0 : void WireFormat::SerializeWithCachedSizes(
736 : const Message& message,
737 : int size, io::CodedOutputStream* output) {
738 0 : const Descriptor* descriptor = message.GetDescriptor();
739 0 : const Reflection* message_reflection = message.GetReflection();
740 0 : int expected_endpoint = output->ByteCount() + size;
741 :
742 0 : vector<const FieldDescriptor*> fields;
743 0 : message_reflection->ListFields(message, &fields);
744 0 : for (int i = 0; i < fields.size(); i++) {
745 0 : SerializeFieldWithCachedSizes(fields[i], message, output);
746 : }
747 :
748 0 : if (descriptor->options().message_set_wire_format()) {
749 0 : SerializeUnknownMessageSetItems(
750 0 : message_reflection->GetUnknownFields(message), output);
751 : } else {
752 0 : SerializeUnknownFields(
753 0 : message_reflection->GetUnknownFields(message), output);
754 : }
755 :
756 0 : GOOGLE_CHECK_EQ(output->ByteCount(), expected_endpoint)
757 : << ": Protocol message serialized to a size different from what was "
758 : "originally expected. Perhaps it was modified by another thread "
759 0 : "during serialization?";
760 0 : }
761 :
762 0 : void WireFormat::SerializeFieldWithCachedSizes(
763 : const FieldDescriptor* field,
764 : const Message& message,
765 : io::CodedOutputStream* output) {
766 0 : const Reflection* message_reflection = message.GetReflection();
767 :
768 0 : if (field->is_extension() &&
769 0 : field->containing_type()->options().message_set_wire_format() &&
770 0 : field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
771 0 : !field->is_repeated()) {
772 0 : SerializeMessageSetItemWithCachedSizes(field, message, output);
773 0 : return;
774 : }
775 :
776 0 : int count = 0;
777 :
778 0 : if (field->is_repeated()) {
779 0 : count = message_reflection->FieldSize(message, field);
780 0 : } else if (message_reflection->HasField(message, field)) {
781 0 : count = 1;
782 : }
783 :
784 0 : const bool is_packed = field->options().packed();
785 0 : if (is_packed && count > 0) {
786 0 : WireFormatLite::WriteTag(field->number(),
787 : WireFormatLite::WIRETYPE_LENGTH_DELIMITED, output);
788 0 : const int data_size = FieldDataOnlyByteSize(field, message);
789 0 : output->WriteVarint32(data_size);
790 : }
791 :
792 0 : for (int j = 0; j < count; j++) {
793 0 : switch (field->type()) {
794 : #define HANDLE_PRIMITIVE_TYPE(TYPE, CPPTYPE, TYPE_METHOD, CPPTYPE_METHOD) \
795 : case FieldDescriptor::TYPE_##TYPE: { \
796 : const CPPTYPE value = field->is_repeated() ? \
797 : message_reflection->GetRepeated##CPPTYPE_METHOD( \
798 : message, field, j) : \
799 : message_reflection->Get##CPPTYPE_METHOD( \
800 : message, field); \
801 : if (is_packed) { \
802 : WireFormatLite::Write##TYPE_METHOD##NoTag(value, output); \
803 : } else { \
804 : WireFormatLite::Write##TYPE_METHOD(field->number(), value, output); \
805 : } \
806 : break; \
807 : }
808 :
809 0 : HANDLE_PRIMITIVE_TYPE( INT32, int32, Int32, Int32)
810 0 : HANDLE_PRIMITIVE_TYPE( INT64, int64, Int64, Int64)
811 0 : HANDLE_PRIMITIVE_TYPE(SINT32, int32, SInt32, Int32)
812 0 : HANDLE_PRIMITIVE_TYPE(SINT64, int64, SInt64, Int64)
813 0 : HANDLE_PRIMITIVE_TYPE(UINT32, uint32, UInt32, UInt32)
814 0 : HANDLE_PRIMITIVE_TYPE(UINT64, uint64, UInt64, UInt64)
815 :
816 0 : HANDLE_PRIMITIVE_TYPE( FIXED32, uint32, Fixed32, UInt32)
817 0 : HANDLE_PRIMITIVE_TYPE( FIXED64, uint64, Fixed64, UInt64)
818 0 : HANDLE_PRIMITIVE_TYPE(SFIXED32, int32, SFixed32, Int32)
819 0 : HANDLE_PRIMITIVE_TYPE(SFIXED64, int64, SFixed64, Int64)
820 :
821 0 : HANDLE_PRIMITIVE_TYPE(FLOAT , float , Float , Float )
822 0 : HANDLE_PRIMITIVE_TYPE(DOUBLE, double, Double, Double)
823 :
824 0 : HANDLE_PRIMITIVE_TYPE(BOOL, bool, Bool, Bool)
825 : #undef HANDLE_PRIMITIVE_TYPE
826 :
827 : case FieldDescriptor::TYPE_GROUP:
828 0 : WireFormatLite::WriteGroup(
829 : field->number(),
830 0 : field->is_repeated() ?
831 : message_reflection->GetRepeatedMessage(
832 0 : message, field, j) :
833 0 : message_reflection->GetMessage(message, field),
834 0 : output);
835 0 : break;
836 :
837 : case FieldDescriptor::TYPE_MESSAGE:
838 0 : WireFormatLite::WriteMessage(
839 : field->number(),
840 0 : field->is_repeated() ?
841 : message_reflection->GetRepeatedMessage(
842 0 : message, field, j) :
843 0 : message_reflection->GetMessage(message, field),
844 0 : output);
845 0 : break;
846 :
847 : case FieldDescriptor::TYPE_ENUM: {
848 0 : const EnumValueDescriptor* value = field->is_repeated() ?
849 0 : message_reflection->GetRepeatedEnum(message, field, j) :
850 0 : message_reflection->GetEnum(message, field);
851 0 : if (is_packed) {
852 0 : WireFormatLite::WriteEnumNoTag(value->number(), output);
853 : } else {
854 0 : WireFormatLite::WriteEnum(field->number(), value->number(), output);
855 : }
856 0 : break;
857 : }
858 :
859 : // Handle strings separately so that we can get string references
860 : // instead of copying.
861 : case FieldDescriptor::TYPE_STRING: {
862 0 : string scratch;
863 0 : const string& value = field->is_repeated() ?
864 : message_reflection->GetRepeatedStringReference(
865 0 : message, field, j, &scratch) :
866 0 : message_reflection->GetStringReference(message, field, &scratch);
867 0 : VerifyUTF8StringNamedField(value.data(), value.length(), SERIALIZE,
868 0 : field->name().c_str());
869 0 : WireFormatLite::WriteString(field->number(), value, output);
870 0 : break;
871 : }
872 :
873 : case FieldDescriptor::TYPE_BYTES: {
874 0 : string scratch;
875 0 : const string& value = field->is_repeated() ?
876 : message_reflection->GetRepeatedStringReference(
877 0 : message, field, j, &scratch) :
878 0 : message_reflection->GetStringReference(message, field, &scratch);
879 0 : WireFormatLite::WriteBytes(field->number(), value, output);
880 0 : break;
881 : }
882 : }
883 : }
884 : }
885 :
886 0 : void WireFormat::SerializeMessageSetItemWithCachedSizes(
887 : const FieldDescriptor* field,
888 : const Message& message,
889 : io::CodedOutputStream* output) {
890 0 : const Reflection* message_reflection = message.GetReflection();
891 :
892 : // Start group.
893 0 : output->WriteVarint32(WireFormatLite::kMessageSetItemStartTag);
894 :
895 : // Write type ID.
896 0 : output->WriteVarint32(WireFormatLite::kMessageSetTypeIdTag);
897 0 : output->WriteVarint32(field->number());
898 :
899 : // Write message.
900 0 : output->WriteVarint32(WireFormatLite::kMessageSetMessageTag);
901 :
902 0 : const Message& sub_message = message_reflection->GetMessage(message, field);
903 0 : output->WriteVarint32(sub_message.GetCachedSize());
904 0 : sub_message.SerializeWithCachedSizes(output);
905 :
906 : // End group.
907 0 : output->WriteVarint32(WireFormatLite::kMessageSetItemEndTag);
908 0 : }
909 :
910 : // ===================================================================
911 :
912 0 : int WireFormat::ByteSize(const Message& message) {
913 0 : const Descriptor* descriptor = message.GetDescriptor();
914 0 : const Reflection* message_reflection = message.GetReflection();
915 :
916 0 : int our_size = 0;
917 :
918 0 : vector<const FieldDescriptor*> fields;
919 0 : message_reflection->ListFields(message, &fields);
920 0 : for (int i = 0; i < fields.size(); i++) {
921 0 : our_size += FieldByteSize(fields[i], message);
922 : }
923 :
924 0 : if (descriptor->options().message_set_wire_format()) {
925 0 : our_size += ComputeUnknownMessageSetItemsSize(
926 0 : message_reflection->GetUnknownFields(message));
927 : } else {
928 0 : our_size += ComputeUnknownFieldsSize(
929 0 : message_reflection->GetUnknownFields(message));
930 : }
931 :
932 0 : return our_size;
933 : }
934 :
935 0 : int WireFormat::FieldByteSize(
936 : const FieldDescriptor* field,
937 : const Message& message) {
938 0 : const Reflection* message_reflection = message.GetReflection();
939 :
940 0 : if (field->is_extension() &&
941 0 : field->containing_type()->options().message_set_wire_format() &&
942 0 : field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
943 0 : !field->is_repeated()) {
944 0 : return MessageSetItemByteSize(field, message);
945 : }
946 :
947 0 : int count = 0;
948 0 : if (field->is_repeated()) {
949 0 : count = message_reflection->FieldSize(message, field);
950 0 : } else if (message_reflection->HasField(message, field)) {
951 0 : count = 1;
952 : }
953 :
954 0 : const int data_size = FieldDataOnlyByteSize(field, message);
955 0 : int our_size = data_size;
956 0 : if (field->options().packed()) {
957 0 : if (data_size > 0) {
958 : // Packed fields get serialized like a string, not their native type.
959 : // Technically this doesn't really matter; the size only changes if it's
960 : // a GROUP
961 0 : our_size += TagSize(field->number(), FieldDescriptor::TYPE_STRING);
962 0 : our_size += io::CodedOutputStream::VarintSize32(data_size);
963 : }
964 : } else {
965 0 : our_size += count * TagSize(field->number(), field->type());
966 : }
967 0 : return our_size;
968 : }
969 :
970 0 : int WireFormat::FieldDataOnlyByteSize(
971 : const FieldDescriptor* field,
972 : const Message& message) {
973 0 : const Reflection* message_reflection = message.GetReflection();
974 :
975 0 : int count = 0;
976 0 : if (field->is_repeated()) {
977 0 : count = message_reflection->FieldSize(message, field);
978 0 : } else if (message_reflection->HasField(message, field)) {
979 0 : count = 1;
980 : }
981 :
982 0 : int data_size = 0;
983 0 : switch (field->type()) {
984 : #define HANDLE_TYPE(TYPE, TYPE_METHOD, CPPTYPE_METHOD) \
985 : case FieldDescriptor::TYPE_##TYPE: \
986 : if (field->is_repeated()) { \
987 : for (int j = 0; j < count; j++) { \
988 : data_size += WireFormatLite::TYPE_METHOD##Size( \
989 : message_reflection->GetRepeated##CPPTYPE_METHOD( \
990 : message, field, j)); \
991 : } \
992 : } else { \
993 : data_size += WireFormatLite::TYPE_METHOD##Size( \
994 : message_reflection->Get##CPPTYPE_METHOD(message, field)); \
995 : } \
996 : break;
997 :
998 : #define HANDLE_FIXED_TYPE(TYPE, TYPE_METHOD) \
999 : case FieldDescriptor::TYPE_##TYPE: \
1000 : data_size += count * WireFormatLite::k##TYPE_METHOD##Size; \
1001 : break;
1002 :
1003 0 : HANDLE_TYPE( INT32, Int32, Int32)
1004 0 : HANDLE_TYPE( INT64, Int64, Int64)
1005 0 : HANDLE_TYPE(SINT32, SInt32, Int32)
1006 0 : HANDLE_TYPE(SINT64, SInt64, Int64)
1007 0 : HANDLE_TYPE(UINT32, UInt32, UInt32)
1008 0 : HANDLE_TYPE(UINT64, UInt64, UInt64)
1009 :
1010 0 : HANDLE_FIXED_TYPE( FIXED32, Fixed32)
1011 0 : HANDLE_FIXED_TYPE( FIXED64, Fixed64)
1012 0 : HANDLE_FIXED_TYPE(SFIXED32, SFixed32)
1013 0 : HANDLE_FIXED_TYPE(SFIXED64, SFixed64)
1014 :
1015 0 : HANDLE_FIXED_TYPE(FLOAT , Float )
1016 0 : HANDLE_FIXED_TYPE(DOUBLE, Double)
1017 :
1018 0 : HANDLE_FIXED_TYPE(BOOL, Bool)
1019 :
1020 0 : HANDLE_TYPE(GROUP , Group , Message)
1021 0 : HANDLE_TYPE(MESSAGE, Message, Message)
1022 : #undef HANDLE_TYPE
1023 : #undef HANDLE_FIXED_TYPE
1024 :
1025 : case FieldDescriptor::TYPE_ENUM: {
1026 0 : if (field->is_repeated()) {
1027 0 : for (int j = 0; j < count; j++) {
1028 0 : data_size += WireFormatLite::EnumSize(
1029 0 : message_reflection->GetRepeatedEnum(message, field, j)->number());
1030 : }
1031 : } else {
1032 0 : data_size += WireFormatLite::EnumSize(
1033 0 : message_reflection->GetEnum(message, field)->number());
1034 : }
1035 0 : break;
1036 : }
1037 :
1038 : // Handle strings separately so that we can get string references
1039 : // instead of copying.
1040 : case FieldDescriptor::TYPE_STRING:
1041 : case FieldDescriptor::TYPE_BYTES: {
1042 0 : for (int j = 0; j < count; j++) {
1043 0 : string scratch;
1044 0 : const string& value = field->is_repeated() ?
1045 : message_reflection->GetRepeatedStringReference(
1046 0 : message, field, j, &scratch) :
1047 0 : message_reflection->GetStringReference(message, field, &scratch);
1048 0 : data_size += WireFormatLite::StringSize(value);
1049 : }
1050 0 : break;
1051 : }
1052 : }
1053 0 : return data_size;
1054 : }
1055 :
1056 0 : int WireFormat::MessageSetItemByteSize(
1057 : const FieldDescriptor* field,
1058 : const Message& message) {
1059 0 : const Reflection* message_reflection = message.GetReflection();
1060 :
1061 0 : int our_size = WireFormatLite::kMessageSetItemTagsSize;
1062 :
1063 : // type_id
1064 0 : our_size += io::CodedOutputStream::VarintSize32(field->number());
1065 :
1066 : // message
1067 0 : const Message& sub_message = message_reflection->GetMessage(message, field);
1068 0 : int message_size = sub_message.ByteSize();
1069 :
1070 0 : our_size += io::CodedOutputStream::VarintSize32(message_size);
1071 0 : our_size += message_size;
1072 :
1073 0 : return our_size;
1074 : }
1075 :
1076 738 : void WireFormat::VerifyUTF8StringFallback(const char* data,
1077 : int size,
1078 : Operation op,
1079 : const char* field_name) {
1080 738 : if (!IsStructurallyValidUTF8(data, size)) {
1081 0 : const char* operation_str = NULL;
1082 0 : switch (op) {
1083 : case PARSE:
1084 0 : operation_str = "parsing";
1085 0 : break;
1086 : case SERIALIZE:
1087 0 : operation_str = "serializing";
1088 0 : break;
1089 : // no default case: have the compiler warn if a case is not covered.
1090 : }
1091 0 : string quoted_field_name = "";
1092 0 : if (field_name != NULL) {
1093 0 : quoted_field_name = StringPrintf(" '%s'", field_name);
1094 : }
1095 : // no space below to avoid double space when the field name is missing.
1096 0 : GOOGLE_LOG(ERROR) << "String field" << quoted_field_name << " contains invalid "
1097 0 : << "UTF-8 data when " << operation_str << " a protocol "
1098 0 : << "buffer. Use the 'bytes' type if you intend to send raw "
1099 0 : << "bytes. ";
1100 : }
1101 738 : }
1102 :
1103 :
1104 : } // namespace internal
1105 : } // namespace protobuf
1106 : } // namespace google
|