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..860a6eb20863a60c47157330ea84eac701b7cdc2 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, |
+ int64_t num_bytes_to_extract) const { |
DCHECK(zip_file_); |
const int open_result = unzOpenCurrentFile(zip_file_); |
@@ -298,26 +299,61 @@ bool ZipReader::ExtractCurrentEntry(WriterDelegate* delegate) const { |
if (!delegate->PrepareOutput()) |
return false; |
- |
+ LOG(ERROR) << "done with init stuff"; |
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); |
+ LOG(ERROR) << "total_num_bytes_read: " << total_num_bytes_read; |
+ 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; |
+ LOG(ERROR) << "tried to read past end of file."; |
+ break; |
+ } |
} else if (num_bytes_read < 0) { |
// If num_bytes_read < 0, then it's a specific UNZ_* error code. |
+ LOG(ERROR) << "read failed"; |
success = false; |
break; |
} else if (num_bytes_read > 0) { |
// Some data is read. |
if (!delegate->WriteBytes(buf.get(), num_bytes_read)) { |
+ LOG(ERROR) << "write failed"; |
success = false; |
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; |
+ } |
+ LOG(ERROR) << "total_num_bytes_read: " << total_num_bytes_read; |
} |
unzCloseCurrentFile(zip_file_); |
@@ -336,7 +372,7 @@ bool ZipReader::ExtractCurrentEntryToFilePath( |
bool success = false; |
{ |
FilePathWriterDelegate writer(output_file_path); |
- success = ExtractCurrentEntry(&writer); |
+ success = ExtractCurrentEntry(&writer, -1); |
} |
if (success && |
@@ -416,15 +452,20 @@ bool ZipReader::ExtractCurrentEntryToFile(base::File* file) const { |
return false; |
FileWriterDelegate writer(file); |
- return ExtractCurrentEntry(&writer); |
+ return ExtractCurrentEntry(&writer, -1); |
} |
-bool ZipReader::ExtractCurrentEntryToString(size_t max_read_bytes, |
+bool ZipReader::ExtractCurrentEntryToString(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; |
+ } |
+ LOG(ERROR) << "here1"; |
if (current_entry_info()->is_directory()) { |
output->clear(); |
return true; |
@@ -435,13 +476,23 @@ 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)) |
+ StringWriterDelegate writer(num_bytes, &contents); |
+ int64_t size_arg = read_entire_file ? ((int64_t)-1) : num_bytes; |
+ LOG(ERROR) << "size_arg: " << size_arg; |
+ LOG(ERROR) << "num_bytes: " << num_bytes; |
+ if (!ExtractCurrentEntry(&writer, size_arg)) { |
+ output->clear(); |
+ LOG(ERROR) << "ExtractCurrentEntry failed"; |
return false; |
+ } |
output->swap(contents); |
return true; |
} |