OLD | NEW |
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 // https://developers.google.com/protocol-buffers/ | 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. |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 } | 244 } |
245 return true; | 245 return true; |
246 } | 246 } |
247 | 247 |
248 template <typename CType, enum WireFormatLite::FieldType DeclaredType> | 248 template <typename CType, enum WireFormatLite::FieldType DeclaredType> |
249 inline bool WireFormatLite::ReadRepeatedFixedSizePrimitive( | 249 inline bool WireFormatLite::ReadRepeatedFixedSizePrimitive( |
250 int tag_size, | 250 int tag_size, |
251 uint32 tag, | 251 uint32 tag, |
252 io::CodedInputStream* input, | 252 io::CodedInputStream* input, |
253 RepeatedField<CType>* values) { | 253 RepeatedField<CType>* values) { |
254 GOOGLE_DCHECK_EQ(UInt32Size(tag), static_cast<size_t>(tag_size)); | 254 GOOGLE_DCHECK_EQ(UInt32Size(tag), tag_size); |
255 CType value; | 255 CType value; |
256 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) | 256 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) |
257 return false; | 257 return false; |
258 values->Add(value); | 258 values->Add(value); |
259 | 259 |
260 // For fixed size values, repeated values can be read more quickly by | 260 // For fixed size values, repeated values can be read more quickly by |
261 // reading directly from a raw array. | 261 // reading directly from a raw array. |
262 // | 262 // |
263 // We can get a tight loop by only reading as many elements as can be | 263 // We can get a tight loop by only reading as many elements as can be |
264 // added to the RepeatedField without having to do any resizing. Additionally, | 264 // added to the RepeatedField without having to do any resizing. Additionally, |
265 // we only try to read as many elements as are available from the current | 265 // we only try to read as many elements as are available from the current |
266 // buffer space. Doing so avoids having to perform boundary checks when | 266 // buffer space. Doing so avoids having to perform boundary checks when |
267 // reading the value: the maximum number of elements that can be read is | 267 // reading the value: the maximum number of elements that can be read is |
268 // known outside of the loop. | 268 // known outside of the loop. |
269 const void* void_pointer; | 269 const void* void_pointer; |
270 int size; | 270 int size; |
271 input->GetDirectBufferPointerInline(&void_pointer, &size); | 271 input->GetDirectBufferPointerInline(&void_pointer, &size); |
272 if (size > 0) { | 272 if (size > 0) { |
273 const uint8* buffer = reinterpret_cast<const uint8*>(void_pointer); | 273 const uint8* buffer = reinterpret_cast<const uint8*>(void_pointer); |
274 // The number of bytes each type occupies on the wire. | 274 // The number of bytes each type occupies on the wire. |
275 const int per_value_size = tag_size + sizeof(value); | 275 const int per_value_size = tag_size + sizeof(value); |
276 | 276 |
277 // parentheses around (std::min) prevents macro expansion of min(...) | |
278 int elements_available = | 277 int elements_available = |
279 (std::min)(values->Capacity() - values->size(), size / per_value_size); | 278 std::min(values->Capacity() - values->size(), size / per_value_size); |
280 int num_read = 0; | 279 int num_read = 0; |
281 while (num_read < elements_available && | 280 while (num_read < elements_available && |
282 (buffer = io::CodedInputStream::ExpectTagFromArray( | 281 (buffer = io::CodedInputStream::ExpectTagFromArray( |
283 buffer, tag)) != NULL) { | 282 buffer, tag)) != NULL) { |
284 buffer = ReadPrimitiveFromArray<CType, DeclaredType>(buffer, &value); | 283 buffer = ReadPrimitiveFromArray<CType, DeclaredType>(buffer, &value); |
285 values->AddAlreadyReserved(value); | 284 values->AddAlreadyReserved(value); |
286 ++num_read; | 285 ++num_read; |
287 } | 286 } |
288 const int read_bytes = num_read * per_value_size; | 287 const int read_bytes = num_read * per_value_size; |
289 if (read_bytes > 0) { | 288 if (read_bytes > 0) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 uint32 tag, | 322 uint32 tag, |
324 io::CodedInputStream* input, | 323 io::CodedInputStream* input, |
325 RepeatedField<CType>* value) { | 324 RepeatedField<CType>* value) { |
326 return ReadRepeatedPrimitive<CType, DeclaredType>( | 325 return ReadRepeatedPrimitive<CType, DeclaredType>( |
327 tag_size, tag, input, value); | 326 tag_size, tag, input, value); |
328 } | 327 } |
329 | 328 |
330 template <typename CType, enum WireFormatLite::FieldType DeclaredType> | 329 template <typename CType, enum WireFormatLite::FieldType DeclaredType> |
331 inline bool WireFormatLite::ReadPackedPrimitive(io::CodedInputStream* input, | 330 inline bool WireFormatLite::ReadPackedPrimitive(io::CodedInputStream* input, |
332 RepeatedField<CType>* values) { | 331 RepeatedField<CType>* values) { |
333 int length; | 332 uint32 length; |
334 if (!input->ReadVarintSizeAsInt(&length)) return false; | 333 if (!input->ReadVarint32(&length)) return false; |
335 io::CodedInputStream::Limit limit = input->PushLimit(length); | 334 io::CodedInputStream::Limit limit = input->PushLimit(length); |
336 while (input->BytesUntilLimit() > 0) { | 335 while (input->BytesUntilLimit() > 0) { |
337 CType value; | 336 CType value; |
338 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false; | 337 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false; |
339 values->Add(value); | 338 values->Add(value); |
340 } | 339 } |
341 input->PopLimit(limit); | 340 input->PopLimit(limit); |
342 return true; | 341 return true; |
343 } | 342 } |
344 | 343 |
345 template <typename CType, enum WireFormatLite::FieldType DeclaredType> | 344 template <typename CType, enum WireFormatLite::FieldType DeclaredType> |
346 inline bool WireFormatLite::ReadPackedFixedSizePrimitive( | 345 inline bool WireFormatLite::ReadPackedFixedSizePrimitive( |
347 io::CodedInputStream* input, RepeatedField<CType>* values) { | 346 io::CodedInputStream* input, RepeatedField<CType>* values) { |
348 int length; | 347 uint32 length; |
349 if (!input->ReadVarintSizeAsInt(&length)) return false; | 348 if (!input->ReadVarint32(&length)) return false; |
350 const int old_entries = values->size(); | 349 const uint32 old_entries = values->size(); |
351 const int new_entries = length / sizeof(CType); | 350 const uint32 new_entries = length / sizeof(CType); |
352 const int new_bytes = new_entries * sizeof(CType); | 351 const uint32 new_bytes = new_entries * sizeof(CType); |
353 if (new_bytes != length) return false; | 352 if (new_bytes != length) return false; |
354 // We would *like* to pre-allocate the buffer to write into (for | 353 // We would *like* to pre-allocate the buffer to write into (for |
355 // speed), but *must* avoid performing a very large allocation due | 354 // speed), but *must* avoid performing a very large allocation due |
356 // to a malicious user-supplied "length" above. So we have a fast | 355 // to a malicious user-supplied "length" above. So we have a fast |
357 // path that pre-allocates when the "length" is less than a bound. | 356 // path that pre-allocates when the "length" is less than a bound. |
358 // We determine the bound by calling BytesUntilTotalBytesLimit() and | 357 // We determine the bound by calling BytesUntilTotalBytesLimit() and |
359 // BytesUntilLimit(). These return -1 to mean "no limit set". | 358 // BytesUntilLimit(). These return -1 to mean "no limit set". |
360 // There are four cases: | 359 // There are four cases: |
361 // TotalBytesLimit Limit | 360 // TotalBytesLimit Limit |
362 // -1 -1 Use slow path. | 361 // -1 -1 Use slow path. |
363 // -1 >= 0 Use fast path if length <= Limit. | 362 // -1 >= 0 Use fast path if length <= Limit. |
364 // >= 0 -1 Use slow path. | 363 // >= 0 -1 Use slow path. |
365 // >= 0 >= 0 Use fast path if length <= min(both limits). | 364 // >= 0 >= 0 Use fast path if length <= min(both limits). |
366 int64 bytes_limit = input->BytesUntilTotalBytesLimit(); | 365 int64 bytes_limit = input->BytesUntilTotalBytesLimit(); |
367 if (bytes_limit == -1) { | 366 if (bytes_limit == -1) { |
368 bytes_limit = input->BytesUntilLimit(); | 367 bytes_limit = input->BytesUntilLimit(); |
369 } else { | 368 } else { |
370 // parentheses around (std::min) prevents macro expansion of min(...) | |
371 bytes_limit = | 369 bytes_limit = |
372 (std::min)(bytes_limit, static_cast<int64>(input->BytesUntilLimit())); | 370 std::min(bytes_limit, static_cast<int64>(input->BytesUntilLimit())); |
373 } | 371 } |
374 if (bytes_limit >= new_bytes) { | 372 if (bytes_limit >= new_bytes) { |
375 // Fast-path that pre-allocates *values to the final size. | 373 // Fast-path that pre-allocates *values to the final size. |
376 #if defined(PROTOBUF_LITTLE_ENDIAN) | 374 #if defined(PROTOBUF_LITTLE_ENDIAN) |
377 values->Resize(old_entries + new_entries, 0); | 375 values->Resize(old_entries + new_entries, 0); |
378 // values->mutable_data() may change after Resize(), so do this after: | 376 // values->mutable_data() may change after Resize(), so do this after: |
379 void* dest = reinterpret_cast<void*>(values->mutable_data() + old_entries); | 377 void* dest = reinterpret_cast<void*>(values->mutable_data() + old_entries); |
380 if (!input->ReadRaw(dest, new_bytes)) { | 378 if (!input->ReadRaw(dest, new_bytes)) { |
381 values->Truncate(old_entries); | 379 values->Truncate(old_entries); |
382 return false; | 380 return false; |
383 } | 381 } |
384 #else | 382 #else |
385 values->Reserve(old_entries + new_entries); | 383 values->Reserve(old_entries + new_entries); |
386 CType value; | 384 CType value; |
387 for (int i = 0; i < new_entries; ++i) { | 385 for (uint32 i = 0; i < new_entries; ++i) { |
388 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false; | 386 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false; |
389 values->AddAlreadyReserved(value); | 387 values->AddAlreadyReserved(value); |
390 } | 388 } |
391 #endif | 389 #endif |
392 } else { | 390 } else { |
393 // This is the slow-path case where "length" may be too large to | 391 // This is the slow-path case where "length" may be too large to |
394 // safely allocate. We read as much as we can into *values | 392 // safely allocate. We read as much as we can into *values |
395 // without pre-allocating "length" bytes. | 393 // without pre-allocating "length" bytes. |
396 CType value; | 394 CType value; |
397 for (int i = 0; i < new_entries; ++i) { | 395 for (uint32 i = 0; i < new_entries; ++i) { |
398 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false; | 396 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false; |
399 values->Add(value); | 397 values->Add(value); |
400 } | 398 } |
401 } | 399 } |
402 return true; | 400 return true; |
403 } | 401 } |
404 | 402 |
405 // Specializations of ReadPackedPrimitive for the fixed size types, which use | 403 // Specializations of ReadPackedPrimitive for the fixed size types, which use |
406 // an optimized code path. | 404 // an optimized code path. |
407 #define READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE) \ | 405 #define READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE) \ |
(...skipping 30 matching lines...) Expand all Loading... |
438 if (!value->MergePartialFromCodedStream(input)) return false; | 436 if (!value->MergePartialFromCodedStream(input)) return false; |
439 input->DecrementRecursionDepth(); | 437 input->DecrementRecursionDepth(); |
440 // Make sure the last thing read was an end tag for this group. | 438 // Make sure the last thing read was an end tag for this group. |
441 if (!input->LastTagWas(MakeTag(field_number, WIRETYPE_END_GROUP))) { | 439 if (!input->LastTagWas(MakeTag(field_number, WIRETYPE_END_GROUP))) { |
442 return false; | 440 return false; |
443 } | 441 } |
444 return true; | 442 return true; |
445 } | 443 } |
446 inline bool WireFormatLite::ReadMessage(io::CodedInputStream* input, | 444 inline bool WireFormatLite::ReadMessage(io::CodedInputStream* input, |
447 MessageLite* value) { | 445 MessageLite* value) { |
448 int length; | 446 uint32 length; |
449 if (!input->ReadVarintSizeAsInt(&length)) return false; | 447 if (!input->ReadVarint32(&length)) return false; |
450 std::pair<io::CodedInputStream::Limit, int> p = | 448 std::pair<io::CodedInputStream::Limit, int> p = |
451 input->IncrementRecursionDepthAndPushLimit(length); | 449 input->IncrementRecursionDepthAndPushLimit(length); |
452 if (p.second < 0 || !value->MergePartialFromCodedStream(input)) return false; | 450 if (p.second < 0 || !value->MergePartialFromCodedStream(input)) return false; |
453 // Make sure that parsing stopped when the limit was hit, not at an endgroup | 451 // Make sure that parsing stopped when the limit was hit, not at an endgroup |
454 // tag. | 452 // tag. |
455 return input->DecrementRecursionDepthAndPopLimit(p.first); | 453 return input->DecrementRecursionDepthAndPopLimit(p.first); |
456 } | 454 } |
457 | 455 |
458 // We name the template parameter something long and extremely unlikely to occur | 456 // We name the template parameter something long and extremely unlikely to occur |
459 // elsewhere because a *qualified* member access expression designed to avoid | 457 // elsewhere because a *qualified* member access expression designed to avoid |
(...skipping 24 matching lines...) Expand all Loading... |
484 inline bool WireFormatLite::ReadGroupNoVirtualNoRecursionDepth( | 482 inline bool WireFormatLite::ReadGroupNoVirtualNoRecursionDepth( |
485 int field_number, io::CodedInputStream* input, | 483 int field_number, io::CodedInputStream* input, |
486 MessageType_WorkAroundCppLookupDefect* value) { | 484 MessageType_WorkAroundCppLookupDefect* value) { |
487 return value->MessageType_WorkAroundCppLookupDefect:: | 485 return value->MessageType_WorkAroundCppLookupDefect:: |
488 MergePartialFromCodedStream(input) && | 486 MergePartialFromCodedStream(input) && |
489 input->LastTagWas(MakeTag(field_number, WIRETYPE_END_GROUP)); | 487 input->LastTagWas(MakeTag(field_number, WIRETYPE_END_GROUP)); |
490 } | 488 } |
491 template<typename MessageType_WorkAroundCppLookupDefect> | 489 template<typename MessageType_WorkAroundCppLookupDefect> |
492 inline bool WireFormatLite::ReadMessageNoVirtual( | 490 inline bool WireFormatLite::ReadMessageNoVirtual( |
493 io::CodedInputStream* input, MessageType_WorkAroundCppLookupDefect* value) { | 491 io::CodedInputStream* input, MessageType_WorkAroundCppLookupDefect* value) { |
494 int length; | 492 uint32 length; |
495 if (!input->ReadVarintSizeAsInt(&length)) return false; | 493 if (!input->ReadVarint32(&length)) return false; |
496 std::pair<io::CodedInputStream::Limit, int> p = | 494 std::pair<io::CodedInputStream::Limit, int> p = |
497 input->IncrementRecursionDepthAndPushLimit(length); | 495 input->IncrementRecursionDepthAndPushLimit(length); |
498 if (p.second < 0 || !value-> | 496 if (p.second < 0 || !value-> |
499 MessageType_WorkAroundCppLookupDefect::MergePartialFromCodedStream(input)) | 497 MessageType_WorkAroundCppLookupDefect::MergePartialFromCodedStream(input)) |
500 return false; | 498 return false; |
501 // Make sure that parsing stopped when the limit was hit, not at an endgroup | 499 // Make sure that parsing stopped when the limit was hit, not at an endgroup |
502 // tag. | 500 // tag. |
503 return input->DecrementRecursionDepthAndPopLimit(p.first); | 501 return input->DecrementRecursionDepthAndPopLimit(p.first); |
504 } | 502 } |
505 template<typename MessageType_WorkAroundCppLookupDefect> | 503 template<typename MessageType_WorkAroundCppLookupDefect> |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
767 return io::CodedOutputStream::WriteStringWithSizeToArray(value, target); | 765 return io::CodedOutputStream::WriteStringWithSizeToArray(value, target); |
768 } | 766 } |
769 inline uint8* WireFormatLite::WriteBytesToArray(int field_number, | 767 inline uint8* WireFormatLite::WriteBytesToArray(int field_number, |
770 const string& value, | 768 const string& value, |
771 uint8* target) { | 769 uint8* target) { |
772 target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target); | 770 target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target); |
773 return io::CodedOutputStream::WriteStringWithSizeToArray(value, target); | 771 return io::CodedOutputStream::WriteStringWithSizeToArray(value, target); |
774 } | 772 } |
775 | 773 |
776 | 774 |
777 inline uint8* WireFormatLite::InternalWriteGroupToArray( | 775 inline uint8* WireFormatLite::WriteGroupToArray(int field_number, |
778 int field_number, const MessageLite& value, bool deterministic, | 776 const MessageLite& value, |
779 uint8* target) { | 777 uint8* target) { |
780 target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target); | 778 target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target); |
781 target = value.InternalSerializeWithCachedSizesToArray(deterministic, target); | 779 target = value.SerializeWithCachedSizesToArray(target); |
782 return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target); | 780 return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target); |
783 } | 781 } |
784 inline uint8* WireFormatLite::InternalWriteMessageToArray( | 782 inline uint8* WireFormatLite::WriteMessageToArray(int field_number, |
785 int field_number, const MessageLite& value, bool deterministic, | 783 const MessageLite& value, |
786 uint8* target) { | 784 uint8* target) { |
787 target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target); | 785 target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target); |
788 target = io::CodedOutputStream::WriteVarint32ToArray( | 786 target = io::CodedOutputStream::WriteVarint32ToArray( |
789 value.GetCachedSize(), target); | 787 value.GetCachedSize(), target); |
790 return value.InternalSerializeWithCachedSizesToArray(deterministic, target); | 788 return value.SerializeWithCachedSizesToArray(target); |
791 } | 789 } |
792 | 790 |
793 // See comment on ReadGroupNoVirtual to understand the need for this template | 791 // See comment on ReadGroupNoVirtual to understand the need for this template |
794 // parameter name. | 792 // parameter name. |
795 template<typename MessageType_WorkAroundCppLookupDefect> | 793 template<typename MessageType_WorkAroundCppLookupDefect> |
796 inline uint8* WireFormatLite::InternalWriteGroupNoVirtualToArray( | 794 inline uint8* WireFormatLite::WriteGroupNoVirtualToArray( |
797 int field_number, const MessageType_WorkAroundCppLookupDefect& value, | 795 int field_number, const MessageType_WorkAroundCppLookupDefect& value, |
798 bool deterministic, uint8* target) { | 796 uint8* target) { |
799 target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target); | 797 target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target); |
800 target = value.InternalSerializeWithCachedSizesToArray(deterministic, target); | 798 target = value.MessageType_WorkAroundCppLookupDefect |
| 799 ::SerializeWithCachedSizesToArray(target); |
801 return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target); | 800 return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target); |
802 } | 801 } |
803 template<typename MessageType_WorkAroundCppLookupDefect> | 802 template<typename MessageType_WorkAroundCppLookupDefect> |
804 inline uint8* WireFormatLite::InternalWriteMessageNoVirtualToArray( | 803 inline uint8* WireFormatLite::WriteMessageNoVirtualToArray( |
805 int field_number, const MessageType_WorkAroundCppLookupDefect& value, | 804 int field_number, const MessageType_WorkAroundCppLookupDefect& value, |
806 bool deterministic, uint8* target) { | 805 uint8* target) { |
807 target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target); | 806 target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target); |
808 target = io::CodedOutputStream::WriteVarint32ToArray( | 807 target = io::CodedOutputStream::WriteVarint32ToArray( |
809 value.MessageType_WorkAroundCppLookupDefect::GetCachedSize(), target); | 808 value.MessageType_WorkAroundCppLookupDefect::GetCachedSize(), target); |
810 return value.InternalSerializeWithCachedSizesToArray(deterministic, target); | 809 return value.MessageType_WorkAroundCppLookupDefect |
| 810 ::SerializeWithCachedSizesToArray(target); |
811 } | 811 } |
812 | 812 |
813 // =================================================================== | 813 // =================================================================== |
814 | 814 |
815 inline size_t WireFormatLite::Int32Size(int32 value) { | 815 inline int WireFormatLite::Int32Size(int32 value) { |
816 return io::CodedOutputStream::VarintSize32SignExtended(value); | 816 return io::CodedOutputStream::VarintSize32SignExtended(value); |
817 } | 817 } |
818 inline size_t WireFormatLite::Int64Size(int64 value) { | 818 inline int WireFormatLite::Int64Size(int64 value) { |
819 return io::CodedOutputStream::VarintSize64(static_cast<uint64>(value)); | 819 return io::CodedOutputStream::VarintSize64(static_cast<uint64>(value)); |
820 } | 820 } |
821 inline size_t WireFormatLite::UInt32Size(uint32 value) { | 821 inline int WireFormatLite::UInt32Size(uint32 value) { |
822 return io::CodedOutputStream::VarintSize32(value); | 822 return io::CodedOutputStream::VarintSize32(value); |
823 } | 823 } |
824 inline size_t WireFormatLite::UInt64Size(uint64 value) { | 824 inline int WireFormatLite::UInt64Size(uint64 value) { |
825 return io::CodedOutputStream::VarintSize64(value); | 825 return io::CodedOutputStream::VarintSize64(value); |
826 } | 826 } |
827 inline size_t WireFormatLite::SInt32Size(int32 value) { | 827 inline int WireFormatLite::SInt32Size(int32 value) { |
828 return io::CodedOutputStream::VarintSize32(ZigZagEncode32(value)); | 828 return io::CodedOutputStream::VarintSize32(ZigZagEncode32(value)); |
829 } | 829 } |
830 inline size_t WireFormatLite::SInt64Size(int64 value) { | 830 inline int WireFormatLite::SInt64Size(int64 value) { |
831 return io::CodedOutputStream::VarintSize64(ZigZagEncode64(value)); | 831 return io::CodedOutputStream::VarintSize64(ZigZagEncode64(value)); |
832 } | 832 } |
833 inline size_t WireFormatLite::EnumSize(int value) { | 833 inline int WireFormatLite::EnumSize(int value) { |
834 return io::CodedOutputStream::VarintSize32SignExtended(value); | 834 return io::CodedOutputStream::VarintSize32SignExtended(value); |
835 } | 835 } |
836 | 836 |
837 inline size_t WireFormatLite::StringSize(const string& value) { | 837 inline int WireFormatLite::StringSize(const string& value) { |
838 return LengthDelimitedSize(value.size()); | 838 return static_cast<int>( |
| 839 io::CodedOutputStream::VarintSize32(static_cast<uint32>(value.size())) + |
| 840 value.size()); |
839 } | 841 } |
840 inline size_t WireFormatLite::BytesSize(const string& value) { | 842 inline int WireFormatLite::BytesSize(const string& value) { |
841 return LengthDelimitedSize(value.size()); | 843 return static_cast<int>( |
| 844 io::CodedOutputStream::VarintSize32(static_cast<uint32>(value.size())) + |
| 845 value.size()); |
842 } | 846 } |
843 | 847 |
844 | 848 |
845 inline size_t WireFormatLite::GroupSize(const MessageLite& value) { | 849 inline int WireFormatLite::GroupSize(const MessageLite& value) { |
846 return value.ByteSizeLong(); | 850 return value.ByteSize(); |
847 } | 851 } |
848 inline size_t WireFormatLite::MessageSize(const MessageLite& value) { | 852 inline int WireFormatLite::MessageSize(const MessageLite& value) { |
849 return LengthDelimitedSize(value.ByteSizeLong()); | 853 return LengthDelimitedSize(value.ByteSize()); |
850 } | 854 } |
851 | 855 |
852 // See comment on ReadGroupNoVirtual to understand the need for this template | 856 // See comment on ReadGroupNoVirtual to understand the need for this template |
853 // parameter name. | 857 // parameter name. |
854 template<typename MessageType_WorkAroundCppLookupDefect> | 858 template<typename MessageType_WorkAroundCppLookupDefect> |
855 inline size_t WireFormatLite::GroupSizeNoVirtual( | 859 inline int WireFormatLite::GroupSizeNoVirtual( |
856 const MessageType_WorkAroundCppLookupDefect& value) { | 860 const MessageType_WorkAroundCppLookupDefect& value) { |
857 return value.MessageType_WorkAroundCppLookupDefect::ByteSizeLong(); | 861 return value.MessageType_WorkAroundCppLookupDefect::ByteSize(); |
858 } | 862 } |
859 template<typename MessageType_WorkAroundCppLookupDefect> | 863 template<typename MessageType_WorkAroundCppLookupDefect> |
860 inline size_t WireFormatLite::MessageSizeNoVirtual( | 864 inline int WireFormatLite::MessageSizeNoVirtual( |
861 const MessageType_WorkAroundCppLookupDefect& value) { | 865 const MessageType_WorkAroundCppLookupDefect& value) { |
862 return LengthDelimitedSize( | 866 return LengthDelimitedSize( |
863 value.MessageType_WorkAroundCppLookupDefect::ByteSizeLong()); | 867 value.MessageType_WorkAroundCppLookupDefect::ByteSize()); |
864 } | 868 } |
865 | 869 |
866 inline size_t WireFormatLite::LengthDelimitedSize(size_t length) { | 870 inline int WireFormatLite::LengthDelimitedSize(int length) { |
867 // The static_cast here prevents an error in certain compiler configurations | 871 return io::CodedOutputStream::VarintSize32(length) + length; |
868 // but is not technically correct--if length is too large to fit in a uint32 | |
869 // then it will be silently truncated. We will need to fix this if we ever | |
870 // decide to start supporting serialized messages greater than 2 GiB in size. | |
871 return length + io::CodedOutputStream::VarintSize32( | |
872 static_cast<uint32>(length)); | |
873 } | 872 } |
874 | 873 |
875 } // namespace internal | 874 } // namespace internal |
876 } // namespace protobuf | 875 } // namespace protobuf |
877 | 876 |
878 } // namespace google | 877 } // namespace google |
879 #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_INL_H__ | 878 #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_INL_H__ |
OLD | NEW |