From 4de8f55113007fdc8e34107950e605fc0209d465 Mon Sep 17 00:00:00 2001 From: "jieluo@google.com" Date: Fri, 18 Jul 2014 00:47:59 +0000 Subject: down integrate to svn --- src/google/protobuf/wire_format_lite_inl.h | 101 +++++++++++++++++++++++++---- 1 file changed, 90 insertions(+), 11 deletions(-) (limited to 'src/google/protobuf/wire_format_lite_inl.h') diff --git a/src/google/protobuf/wire_format_lite_inl.h b/src/google/protobuf/wire_format_lite_inl.h index 641cc92f..ba9f9ce3 100644 --- a/src/google/protobuf/wire_format_lite_inl.h +++ b/src/google/protobuf/wire_format_lite_inl.h @@ -150,8 +150,8 @@ template <> inline bool WireFormatLite::ReadPrimitive( io::CodedInputStream* input, bool* value) { - uint32 temp; - if (!input->ReadVarint32(&temp)) return false; + uint64 temp; + if (!input->ReadVarint64(&temp)) return false; *value = temp != 0; return true; } @@ -221,10 +221,11 @@ inline const uint8* WireFormatLite::ReadPrimitiveFromArray< } template -inline bool WireFormatLite::ReadRepeatedPrimitive(int, // tag_size, unused. - uint32 tag, - io::CodedInputStream* input, - RepeatedField* values) { +inline bool WireFormatLite::ReadRepeatedPrimitive( + int, // tag_size, unused. + uint32 tag, + io::CodedInputStream* input, + RepeatedField* values) { CType value; if (!ReadPrimitive(input, &value)) return false; values->Add(value); @@ -284,7 +285,7 @@ inline bool WireFormatLite::ReadRepeatedFixedSizePrimitive( return true; } -// Specializations of ReadRepeatedPrimitive for the fixed size types, which use +// Specializations of ReadRepeatedPrimitive for the fixed size types, which use // the optimized code path. #define READ_REPEATED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE) \ template <> \ @@ -333,6 +334,86 @@ inline bool WireFormatLite::ReadPackedPrimitive(io::CodedInputStream* input, return true; } +template +inline bool WireFormatLite::ReadPackedFixedSizePrimitive( + io::CodedInputStream* input, RepeatedField* values) { + uint32 length; + if (!input->ReadVarint32(&length)) return false; + const uint32 old_entries = values->size(); + const uint32 new_entries = length / sizeof(CType); + const uint32 new_bytes = new_entries * sizeof(CType); + if (new_bytes != length) return false; + // We would *like* to pre-allocate the buffer to write into (for + // speed), but *must* avoid performing a very large allocation due + // to a malicious user-supplied "length" above. So we have a fast + // path that pre-allocates when the "length" is less than a bound. + // We determine the bound by calling BytesUntilTotalBytesLimit() and + // BytesUntilLimit(). These return -1 to mean "no limit set". + // There are four cases: + // TotalBytesLimit Limit + // -1 -1 Use slow path. + // -1 >= 0 Use fast path if length <= Limit. + // >= 0 -1 Use slow path. + // >= 0 >= 0 Use fast path if length <= min(both limits). + int64 bytes_limit = input->BytesUntilTotalBytesLimit(); + if (bytes_limit == -1) { + bytes_limit = input->BytesUntilLimit(); + } else { + bytes_limit = + min(bytes_limit, static_cast(input->BytesUntilLimit())); + } + if (bytes_limit >= new_bytes) { + // Fast-path that pre-allocates *values to the final size. +#if defined(PROTOBUF_LITTLE_ENDIAN) + values->Resize(old_entries + new_entries, 0); + // values->mutable_data() may change after Resize(), so do this after: + void* dest = reinterpret_cast(values->mutable_data() + old_entries); + if (!input->ReadRaw(dest, new_bytes)) { + values->Truncate(old_entries); + return false; + } +#else + values->Reserve(old_entries + new_entries); + CType value; + for (int i = 0; i < new_entries; ++i) { + if (!ReadPrimitive(input, &value)) return false; + values->AddAlreadyReserved(value); + } +#endif + } else { + // This is the slow-path case where "length" may be too large to + // safely allocate. We read as much as we can into *values + // without pre-allocating "length" bytes. + CType value; + for (int i = 0; i < new_entries; ++i) { + if (!ReadPrimitive(input, &value)) return false; + values->Add(value); + } + } + return true; +} + +// Specializations of ReadPackedPrimitive for the fixed size types, which use +// an optimized code path. +#define READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE) \ +template <> \ +inline bool WireFormatLite::ReadPackedPrimitive< \ + CPPTYPE, WireFormatLite::DECLARED_TYPE>( \ + io::CodedInputStream* input, \ + RepeatedField* values) { \ + return ReadPackedFixedSizePrimitive< \ + CPPTYPE, WireFormatLite::DECLARED_TYPE>(input, values); \ +} + +READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(uint32, TYPE_FIXED32); +READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(uint64, TYPE_FIXED64); +READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(int32, TYPE_SFIXED32); +READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(int64, TYPE_SFIXED64); +READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(float, TYPE_FLOAT); +READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(double, TYPE_DOUBLE); + +#undef READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE + template bool WireFormatLite::ReadPackedPrimitiveNoInline(io::CodedInputStream* input, RepeatedField* values) { @@ -660,15 +741,13 @@ inline uint8* WireFormatLite::WriteStringToArray(int field_number, // WriteString() to avoid code duplication. If the implementations become // different, you will need to update that usage. target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target); - target = io::CodedOutputStream::WriteVarint32ToArray(value.size(), target); - return io::CodedOutputStream::WriteStringToArray(value, target); + return io::CodedOutputStream::WriteStringWithSizeToArray(value, target); } inline uint8* WireFormatLite::WriteBytesToArray(int field_number, const string& value, uint8* target) { target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target); - target = io::CodedOutputStream::WriteVarint32ToArray(value.size(), target); - return io::CodedOutputStream::WriteStringToArray(value, target); + return io::CodedOutputStream::WriteStringWithSizeToArray(value, target); } -- cgit v1.2.3