Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Unified Diff: third_party/zlib/google/zip_reader.cc

Issue 2961373002: Improve Zip File Scanning on Mac (Closed)
Patch Set: avoiding multiple calls to unzReadCurrentFile Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..9a43cee90311e2c616f00120cbafbead3acac097 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,55 @@ 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]);
+ uint64_t total_num_bytes_extracted = 0;
+ bool entire_file_extracted = false;
+ bool extraction_complete = false;
+ bool entire_buffer_written_to_delegate = false;
+
while (true) {
- const int num_bytes_read = unzReadCurrentFile(zip_file_, buf.get(),
- internal::kZipBufSize);
+ const int num_bytes_read =
+ unzReadCurrentFile(zip_file_, buf.get(), internal::kZipBufSize);
+
+ // Ensures function returns true if the |num_bytes_to_extract| cap has
+ // been hit, but also the entire file has been read (i.e. the file size
+ // is the same as |num_bytes_to_extract|).
+ if (extraction_complete) {
+ if (entire_buffer_written_to_delegate && num_bytes_read == 0)
+ entire_file_extracted = true;
+ break;
+ }
+
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, 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;
- break;
+ auto unread_bytes = base::CheckedNumeric<uint64_t>(num_bytes_to_extract) -
+ total_num_bytes_extracted;
+ uint64_t num_bytes_to_write =
+ std::min<uint64_t>(unread_bytes.ValueOrDie(),
+ base::checked_cast<uint64_t>(num_bytes_read));
+ total_num_bytes_extracted += num_bytes_to_write;
+ // If error or cap on extraction has been hit, sets boolean to true to the
+ // loop breaks on the next iteration.
+ if (!delegate->WriteBytes(buf.get(), num_bytes_to_write) ||
+ num_bytes_to_extract == total_num_bytes_extracted) {
+ extraction_complete = true;
+ if (num_bytes_to_write ==
+ base::checked_cast<uint64_t>(num_bytes_read)) {
+ entire_buffer_written_to_delegate = true;
+ }
}
}
}
unzCloseCurrentFile(zip_file_);
- return success;
+ return entire_file_extracted;
}
bool ZipReader::ExtractCurrentEntryToFilePath(
@@ -336,7 +361,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 +442,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 +466,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;
}

Powered by Google App Engine
This is Rietveld 408576698