Chromium Code Reviews| Index: third_party/zlib/google/zip_reader.cc |
| diff --git a/third_party/zlib/google/zip_reader.cc b/third_party/zlib/google/zip_reader.cc |
| index ff5a39514af113d28f5266d2c119d51a4d386328..f15383d74db4147d3e5a786735a04949341c8cc5 100644 |
| --- a/third_party/zlib/google/zip_reader.cc |
| +++ b/third_party/zlib/google/zip_reader.cc |
| @@ -289,7 +289,8 @@ bool ZipReader::LocateAndOpenEntry(const base::FilePath& path_in_zip) { |
| return OpenCurrentEntryInZip(); |
| } |
| -bool ZipReader::ExtractCurrentEntry(WriterDelegate* delegate) const { |
| +bool ZipReader::ExtractCurrentEntry(WriterDelegate* delegate, |
| + uint64_t num_bytes_to_extract) const { |
| DCHECK(zip_file_); |
| const int open_result = unzOpenCurrentFile(zip_file_); |
| @@ -298,31 +299,40 @@ bool ZipReader::ExtractCurrentEntry(WriterDelegate* delegate) const { |
| if (!delegate->PrepareOutput()) |
| return false; |
| - |
| - bool success = true; // This becomes false when something bad happens. |
| std::unique_ptr<char[]> buf(new char[internal::kZipBufSize]); |
| - while (true) { |
| - const int num_bytes_read = unzReadCurrentFile(zip_file_, buf.get(), |
| - internal::kZipBufSize); |
| + |
| + uint64_t remaining_capacity = num_bytes_to_extract; |
| + bool entire_file_extracted = false; |
| + |
| + while (remaining_capacity > 0) { |
| + const int num_bytes_read = |
| + unzReadCurrentFile(zip_file_, buf.get(), internal::kZipBufSize); |
| + |
| if (num_bytes_read == 0) { |
| - // Reached the end of the file. |
| + entire_file_extracted = true; |
| break; |
| - } else if (num_bytes_read < 0) { |
| + } |
| + if (num_bytes_read < 0) { |
|
satorux1
2017/08/03 08:36:48
nit: else if
(sorry, my code sample was bad)
mortonm
2017/08/03 15:01:23
Done.
|
| // If num_bytes_read < 0, then it's a specific UNZ_* error code. |
| - success = false; |
| break; |
| } else if (num_bytes_read > 0) { |
| - // Some data is read. |
| - if (!delegate->WriteBytes(buf.get(), num_bytes_read)) { |
| - success = false; |
| + uint64_t num_bytes_to_write = std::min<uint64_t>( |
| + remaining_capacity, base::checked_cast<uint64_t>(num_bytes_read)); |
| + if (!delegate->WriteBytes(buf.get(), num_bytes_to_write)) |
| break; |
| + if (remaining_capacity == base::checked_cast<uint64_t>(num_bytes_read)) { |
| + // Ensures function returns true if the entire file has been read. |
| + entire_file_extracted = |
| + (unzReadCurrentFile(zip_file_, buf.get(), 1) == 0); |
| } |
| + CHECK_GE(remaining_capacity, num_bytes_to_write); |
| + remaining_capacity -= num_bytes_to_write; |
| } |
| } |
| unzCloseCurrentFile(zip_file_); |
| - return success; |
| + return entire_file_extracted; |
| } |
| bool ZipReader::ExtractCurrentEntryToFilePath( |
| @@ -336,7 +346,8 @@ bool ZipReader::ExtractCurrentEntryToFilePath( |
| bool success = false; |
| { |
| FilePathWriterDelegate writer(output_file_path); |
| - success = ExtractCurrentEntry(&writer); |
| + success = |
| + ExtractCurrentEntry(&writer, std::numeric_limits<uint64_t>::max()); |
| } |
| if (success && |
| @@ -416,14 +427,18 @@ bool ZipReader::ExtractCurrentEntryToFile(base::File* file) const { |
| return false; |
| FileWriterDelegate writer(file); |
| - return ExtractCurrentEntry(&writer); |
| + return ExtractCurrentEntry(&writer, std::numeric_limits<uint64_t>::max()); |
| } |
| -bool ZipReader::ExtractCurrentEntryToString(size_t max_read_bytes, |
| +bool ZipReader::ExtractCurrentEntryToString(uint64_t max_read_bytes, |
| std::string* output) const { |
| DCHECK(output); |
| DCHECK(zip_file_); |
| - DCHECK_NE(0U, max_read_bytes); |
| + |
| + if (max_read_bytes == 0) { |
| + output->clear(); |
| + return true; |
| + } |
| if (current_entry_info()->is_directory()) { |
| output->clear(); |
| @@ -436,12 +451,23 @@ bool ZipReader::ExtractCurrentEntryToString(size_t max_read_bytes, |
| // incorrect therefore this function needs to read as much data as possible. |
| std::string contents; |
| contents.reserve( |
| - static_cast<size_t>(std::min(static_cast<int64_t>(max_read_bytes), |
| + static_cast<size_t>(std::min(base::checked_cast<int64_t>(max_read_bytes), |
| current_entry_info()->original_size()))); |
| StringWriterDelegate writer(max_read_bytes, &contents); |
| - if (!ExtractCurrentEntry(&writer)) |
| + if (!ExtractCurrentEntry(&writer, max_read_bytes)) { |
| + if (contents.length() < max_read_bytes) { |
| + // There was an error in extracting entry. If ExtractCurrentEntry() |
| + // returns false, the entire file was not read - in which case |
| + // contents.length() should equal |max_read_bytes| unless an error |
| + // occurred which caused extraction to be aborted. |
| + output->clear(); |
| + } else { |
| + // |num_bytes| is less than the length of current entry. |
| + output->swap(contents); |
| + } |
| return false; |
| + } |
| output->swap(contents); |
| return true; |
| } |