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

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

Issue 2961373002: Improve Zip File Scanning on Mac (Closed)
Patch Set: minor Created 3 years, 4 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
« no previous file with comments | « third_party/zlib/google/zip_reader.h ('k') | third_party/zlib/google/zip_reader_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..08c10b6c036b9cb5e254395952a94e8c7ee5f186 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,39 @@ 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, 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 +345,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 +426,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 +450,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;
}
« no previous file with comments | « third_party/zlib/google/zip_reader.h ('k') | third_party/zlib/google/zip_reader_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698