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

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

Issue 179963002: New Zip::ZipFromMemory API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixup! New Zip::ZipFromMemory API. Created 6 years, 10 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 7b7870ee981daf4287255dc50475890f094901e7..bfafb5d6bac128875e6252b3424d19a9c4dce41e 100644
--- a/third_party/zlib/google/zip_reader.cc
+++ b/third_party/zlib/google/zip_reader.cc
@@ -6,6 +6,7 @@
#include "base/file_util.h"
#include "base/logging.h"
+#include "base/memory/ref_counted_memory.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
@@ -351,6 +352,84 @@ bool ZipReader::ExtractCurrentEntryToFd(const int fd) {
}
#endif // defined(OS_POSIX)
+bool ZipReader::ExtractCurrentEntryToRefCountedMemory(
+ int max_read_bytes,
+ base::RefCountedMemory** output) const {
+ DCHECK(output);
+ DCHECK(zip_file_);
+ DCHECK(max_read_bytes > 0);
+
+ if (current_entry_info()->is_directory()) {
+ *output = NULL;
+ return true;
+ }
+
+ const int open_result = unzOpenCurrentFile(zip_file_);
+ if (open_result != UNZ_OK)
+ return false;
+
+ // The original_size() is the best hint for the real size, so it saves
+ // doing reallocations for the common case when the uncompressed size is
+ // 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::vector<unsigned char> contents(std::min<int64>(
+ max_read_bytes, current_entry_info()->original_size()) + 1, 0);
+
+ int buffer_index = 0;
+ bool success = true;
+ while (true) {
+ const int bytes_to_read = contents.size() - buffer_index;
+ const int bytes_read = unzReadCurrentFile(
+ zip_file_,
+ &(contents[buffer_index]),
+ bytes_to_read);
+ DCHECK(bytes_read <= bytes_to_read);
+
+ if (bytes_read == 0) {
+ contents.resize(buffer_index, 0);
+ // Reached the end of the file.
+ break;
+ } else if (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)
+ buffer_index += bytes_read;
+
+ if (buffer_index > max_read_bytes) {
+ success = false;
+ break;
+ }
+
+ if (bytes_read == bytes_to_read) {
+ // Filled up the buffer, no more space left, need to resize.
+ contents.resize(contents.size() + internal::kZipBufSize, 0);
+ } else {
+ contents.resize(buffer_index, 0);
+ // Read less bytes than asked. That means it reached end of file.
+ break;
+ }
+ }
+ }
+
+ unzCloseCurrentFile(zip_file_);
+ if (success)
+ *output = base::RefCountedBytes::TakeVector(&contents);
+
+ return success;
+}
+
+bool ZipReader::ExtractCurrentEntryToRefCountedMemory(
+ int max_read_bytes,
+ scoped_refptr<base::RefCountedMemory>* output) const {
+ DCHECK(output);
+ base::RefCountedMemory* ptr = NULL;
+ bool result = ExtractCurrentEntryToRefCountedMemory(max_read_bytes, &ptr);
+ if (result && ptr)
+ *output = make_scoped_refptr(ptr);
+ return result;
+}
+
bool ZipReader::OpenInternal() {
DCHECK(zip_file_);

Powered by Google App Engine
This is Rietveld 408576698