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..c3335edb9d1876935d4681ee10c42fca4abdba9b 100644 |
--- a/third_party/zlib/google/zip_reader.cc |
+++ b/third_party/zlib/google/zip_reader.cc |
@@ -290,6 +290,12 @@ bool ZipReader::LocateAndOpenEntry(const base::FilePath& path_in_zip) { |
} |
bool ZipReader::ExtractCurrentEntry(WriterDelegate* delegate) const { |
+ return ExtractFromBeginningOfCurrentEntry(delegate, -1); |
+} |
+ |
+bool ZipReader::ExtractFromBeginningOfCurrentEntry( |
+ WriterDelegate* delegate, |
+ int64_t num_bytes_to_extract) const { |
DCHECK(zip_file_); |
const int open_result = unzOpenCurrentFile(zip_file_); |
@@ -301,12 +307,34 @@ bool ZipReader::ExtractCurrentEntry(WriterDelegate* delegate) const { |
bool success = true; // This becomes false when something bad happens. |
std::unique_ptr<char[]> buf(new char[internal::kZipBufSize]); |
+ uint64_t total_num_bytes_read = 0; |
while (true) { |
- const int num_bytes_read = unzReadCurrentFile(zip_file_, buf.get(), |
- internal::kZipBufSize); |
+ uint64_t bytes_to_read = internal::kZipBufSize; |
+ // This should never result in a negative value, as |num_bytes_to_extract| |
+ // will always be greater than |total_num_bytes_read|. |
+ if (num_bytes_to_extract >= 0) { |
+ auto unread_bytes = |
+ base::CheckedNumeric<uint64_t>( |
+ base::checked_cast<uint64_t>(num_bytes_to_extract)) - |
+ total_num_bytes_read; |
+ if (unread_bytes.ValueOrDie() < internal::kZipBufSize) { |
+ bytes_to_read = unread_bytes.ValueOrDie(); |
+ } |
+ } |
+ |
+ const int num_bytes_read = |
+ unzReadCurrentFile(zip_file_, buf.get(), bytes_to_read); |
+ |
if (num_bytes_read == 0) { |
- // Reached the end of the file. |
- break; |
+ // Reached the end of the file. Return true if entire file was supposed to |
+ // be read, false if not. |
+ if (num_bytes_to_extract == -1) { |
+ success = true; |
+ break; |
+ } else { |
+ success = false; |
+ break; |
+ } |
} else if (num_bytes_read < 0) { |
// If num_bytes_read < 0, then it's a specific UNZ_* error code. |
success = false; |
@@ -318,6 +346,14 @@ bool ZipReader::ExtractCurrentEntry(WriterDelegate* delegate) const { |
break; |
} |
} |
+ // Previous check ensures that num_bytes_read is not negative here. |
+ total_num_bytes_read += base::checked_cast<uint64_t>(num_bytes_read); |
+ if (num_bytes_to_extract >= 0 && |
+ total_num_bytes_read == |
+ base::checked_cast<uint64_t>(num_bytes_to_extract)) { |
+ // Read all the data. |
+ break; |
+ } |
} |
unzCloseCurrentFile(zip_file_); |
@@ -421,9 +457,21 @@ bool ZipReader::ExtractCurrentEntryToFile(base::File* file) const { |
bool ZipReader::ExtractCurrentEntryToString(size_t max_read_bytes, |
std::string* output) const { |
+ return ExtractFromBeginningOfCurrentEntryToString(max_read_bytes, true, |
+ output); |
+} |
+ |
+bool ZipReader::ExtractFromBeginningOfCurrentEntryToString( |
+ size_t num_bytes, |
+ bool read_entire_file, |
+ std::string* output) const { |
DCHECK(output); |
DCHECK(zip_file_); |
- DCHECK_NE(0U, max_read_bytes); |
+ |
+ if (num_bytes == 0) { |
+ output->clear(); |
+ return true; |
+ } |
if (current_entry_info()->is_directory()) { |
output->clear(); |
@@ -435,14 +483,28 @@ bool ZipReader::ExtractCurrentEntryToString(size_t max_read_bytes, |
// correct. However, we need to assume that the uncompressed size could be |
// 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), |
- current_entry_info()->original_size()))); |
+ if (read_entire_file) { |
+ contents.reserve( |
+ static_cast<size_t>(std::min(static_cast<int64_t>(num_bytes), |
+ current_entry_info()->original_size()))); |
+ } else { |
+ contents.reserve(num_bytes); |
+ } |
- StringWriterDelegate writer(max_read_bytes, &contents); |
- if (!ExtractCurrentEntry(&writer)) |
- return false; |
- output->swap(contents); |
+ StringWriterDelegate writer(num_bytes, &contents); |
+ if (read_entire_file) { |
+ if (!ExtractCurrentEntry(&writer)) { |
+ output->clear(); |
+ return false; |
+ } |
+ output->swap(contents); |
+ } else { |
+ if (!ExtractFromBeginningOfCurrentEntry(&writer, num_bytes)) { |
+ output->clear(); |
+ return false; |
+ } |
+ output->swap(contents); |
+ } |
return true; |
} |