1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "third_party/zlib/google/zip_reader.h"
8 #include "base/files/file.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "third_party/zlib/google/zip_internal.h"
15 #if defined(USE_SYSTEM_MINIZIP)
16 #include <minizip/unzip.h>
18 #include "third_party/zlib/contrib/minizip/unzip.h"
20 #include "third_party/zlib/contrib/minizip/iowin32.h"
21 #endif // defined(OS_WIN)
22 #endif // defined(USE_SYSTEM_MINIZIP)
28 // FilePathWriterDelegate ------------------------------------------------------
30 // A writer delegate that writes a file at a given path.
31 class FilePathWriterDelegate
: public WriterDelegate
{
33 explicit FilePathWriterDelegate(const base::FilePath
& output_file_path
);
34 ~FilePathWriterDelegate() override
;
36 // WriterDelegate methods:
38 // Creates the output file and any necessary intermediate directories.
39 bool PrepareOutput() override
;
41 // Writes |num_bytes| bytes of |data| to the file, returning false if not all
42 // bytes could be written.
43 bool WriteBytes(const char* data
, int num_bytes
) override
;
46 base::FilePath output_file_path_
;
49 DISALLOW_COPY_AND_ASSIGN(FilePathWriterDelegate
);
52 FilePathWriterDelegate::FilePathWriterDelegate(
53 const base::FilePath
& output_file_path
)
54 : output_file_path_(output_file_path
) {
57 FilePathWriterDelegate::~FilePathWriterDelegate() {
60 bool FilePathWriterDelegate::PrepareOutput() {
61 // We can't rely on parent directory entries being specified in the
62 // zip, so we make sure they are created.
63 if (!base::CreateDirectory(output_file_path_
.DirName()))
66 file_
.Initialize(output_file_path_
,
67 base::File::FLAG_CREATE_ALWAYS
| base::File::FLAG_WRITE
);
68 return file_
.IsValid();
71 bool FilePathWriterDelegate::WriteBytes(const char* data
, int num_bytes
) {
72 return num_bytes
== file_
.WriteAtCurrentPos(data
, num_bytes
);
76 // StringWriterDelegate --------------------------------------------------------
78 // A writer delegate that writes no more than |max_read_bytes| to a given
80 class StringWriterDelegate
: public WriterDelegate
{
82 StringWriterDelegate(size_t max_read_bytes
, std::string
* output
);
83 ~StringWriterDelegate() override
;
85 // WriterDelegate methods:
88 bool PrepareOutput() override
;
90 // Appends |num_bytes| bytes from |data| to the output string. Returns false
91 // if |num_bytes| will cause the string to exceed |max_read_bytes|.
92 bool WriteBytes(const char* data
, int num_bytes
) override
;
95 size_t max_read_bytes_
;
98 DISALLOW_COPY_AND_ASSIGN(StringWriterDelegate
);
101 StringWriterDelegate::StringWriterDelegate(size_t max_read_bytes
,
103 : max_read_bytes_(max_read_bytes
),
107 StringWriterDelegate::~StringWriterDelegate() {
110 bool StringWriterDelegate::PrepareOutput() {
114 bool StringWriterDelegate::WriteBytes(const char* data
, int num_bytes
) {
115 if (output_
->size() + num_bytes
> max_read_bytes_
)
117 output_
->append(data
, num_bytes
);
123 // TODO(satorux): The implementation assumes that file names in zip files
124 // are encoded in UTF-8. This is true for zip files created by Zip()
125 // function in zip.h, but not true for user-supplied random zip files.
126 ZipReader::EntryInfo::EntryInfo(const std::string
& file_name_in_zip
,
127 const unz_file_info
& raw_file_info
)
128 : file_path_(base::FilePath::FromUTF8Unsafe(file_name_in_zip
)),
129 is_directory_(false) {
130 original_size_
= raw_file_info
.uncompressed_size
;
132 // Directory entries in zip files end with "/".
133 is_directory_
= EndsWith(file_name_in_zip
, "/", false);
135 // Check the file name here for directory traversal issues.
136 is_unsafe_
= file_path_
.ReferencesParent();
138 // We also consider that the file name is unsafe, if it's invalid UTF-8.
139 base::string16 file_name_utf16
;
140 if (!base::UTF8ToUTF16(file_name_in_zip
.data(), file_name_in_zip
.size(),
145 // We also consider that the file name is unsafe, if it's absolute.
146 // On Windows, IsAbsolute() returns false for paths starting with "/".
147 if (file_path_
.IsAbsolute() || StartsWithASCII(file_name_in_zip
, "/", false))
150 // Construct the last modified time. The timezone info is not present in
151 // zip files, so we construct the time as local time.
152 base::Time::Exploded exploded_time
= {}; // Zero-clear.
153 exploded_time
.year
= raw_file_info
.tmu_date
.tm_year
;
154 // The month in zip file is 0-based, whereas ours is 1-based.
155 exploded_time
.month
= raw_file_info
.tmu_date
.tm_mon
+ 1;
156 exploded_time
.day_of_month
= raw_file_info
.tmu_date
.tm_mday
;
157 exploded_time
.hour
= raw_file_info
.tmu_date
.tm_hour
;
158 exploded_time
.minute
= raw_file_info
.tmu_date
.tm_min
;
159 exploded_time
.second
= raw_file_info
.tmu_date
.tm_sec
;
160 exploded_time
.millisecond
= 0;
161 if (exploded_time
.HasValidValues()) {
162 last_modified_
= base::Time::FromLocalExploded(exploded_time
);
164 // Use Unix time epoch if the time stamp data is invalid.
165 last_modified_
= base::Time::UnixEpoch();
169 ZipReader::ZipReader()
170 : weak_ptr_factory_(this) {
174 ZipReader::~ZipReader() {
178 bool ZipReader::Open(const base::FilePath
& zip_file_path
) {
181 // Use of "Unsafe" function does not look good, but there is no way to do
182 // this safely on Linux. See file_util.h for details.
183 zip_file_
= internal::OpenForUnzipping(zip_file_path
.AsUTF8Unsafe());
188 return OpenInternal();
191 bool ZipReader::OpenFromPlatformFile(base::PlatformFile zip_fd
) {
194 #if defined(OS_POSIX)
195 zip_file_
= internal::OpenFdForUnzipping(zip_fd
);
196 #elif defined(OS_WIN)
197 zip_file_
= internal::OpenHandleForUnzipping(zip_fd
);
203 return OpenInternal();
206 bool ZipReader::OpenFromString(const std::string
& data
) {
207 zip_file_
= internal::PrepareMemoryForUnzipping(data
);
210 return OpenInternal();
213 void ZipReader::Close() {
220 bool ZipReader::HasMore() {
221 return !reached_end_
;
224 bool ZipReader::AdvanceToNextEntry() {
227 // Should not go further if we already reached the end.
231 unz_file_pos position
= {};
232 if (unzGetFilePos(zip_file_
, &position
) != UNZ_OK
)
234 const int current_entry_index
= position
.num_of_file
;
235 // If we are currently at the last entry, then the next position is the
236 // end of the zip file, so mark that we reached the end.
237 if (current_entry_index
+ 1 == num_entries_
) {
240 DCHECK_LT(current_entry_index
+ 1, num_entries_
);
241 if (unzGoToNextFile(zip_file_
) != UNZ_OK
) {
245 current_entry_info_
.reset();
249 bool ZipReader::OpenCurrentEntryInZip() {
252 unz_file_info raw_file_info
= {};
253 char raw_file_name_in_zip
[internal::kZipMaxPath
] = {};
254 const int result
= unzGetCurrentFileInfo(zip_file_
,
256 raw_file_name_in_zip
,
257 sizeof(raw_file_name_in_zip
) - 1,
259 0, // extraFieldBufferSize.
261 0); // commentBufferSize.
262 if (result
!= UNZ_OK
)
264 if (raw_file_name_in_zip
[0] == '\0')
266 current_entry_info_
.reset(
267 new EntryInfo(raw_file_name_in_zip
, raw_file_info
));
271 bool ZipReader::LocateAndOpenEntry(const base::FilePath
& path_in_zip
) {
274 current_entry_info_
.reset();
275 reached_end_
= false;
276 const int kDefaultCaseSensivityOfOS
= 0;
277 const int result
= unzLocateFile(zip_file_
,
278 path_in_zip
.AsUTF8Unsafe().c_str(),
279 kDefaultCaseSensivityOfOS
);
280 if (result
!= UNZ_OK
)
283 // Then Open the entry.
284 return OpenCurrentEntryInZip();
287 bool ZipReader::ExtractCurrentEntry(WriterDelegate
* delegate
) const {
290 const int open_result
= unzOpenCurrentFile(zip_file_
);
291 if (open_result
!= UNZ_OK
)
294 if (!delegate
->PrepareOutput())
297 bool success
= true; // This becomes false when something bad happens.
298 scoped_ptr
<char[]> buf(new char[internal::kZipBufSize
]);
300 const int num_bytes_read
= unzReadCurrentFile(zip_file_
, buf
.get(),
301 internal::kZipBufSize
);
302 if (num_bytes_read
== 0) {
303 // Reached the end of the file.
305 } else if (num_bytes_read
< 0) {
306 // If num_bytes_read < 0, then it's a specific UNZ_* error code.
309 } else if (num_bytes_read
> 0) {
310 // Some data is read.
311 if (!delegate
->WriteBytes(buf
.get(), num_bytes_read
)) {
318 unzCloseCurrentFile(zip_file_
);
323 bool ZipReader::ExtractCurrentEntryToFilePath(
324 const base::FilePath
& output_file_path
) const {
327 // If this is a directory, just create it and return.
328 if (current_entry_info()->is_directory())
329 return base::CreateDirectory(output_file_path
);
331 bool success
= false;
333 FilePathWriterDelegate
writer(output_file_path
);
334 success
= ExtractCurrentEntry(&writer
);
338 current_entry_info()->last_modified() != base::Time::UnixEpoch()) {
339 base::TouchFile(output_file_path
,
341 current_entry_info()->last_modified());
347 void ZipReader::ExtractCurrentEntryToFilePathAsync(
348 const base::FilePath
& output_file_path
,
349 const SuccessCallback
& success_callback
,
350 const FailureCallback
& failure_callback
,
351 const ProgressCallback
& progress_callback
) {
353 DCHECK(current_entry_info_
.get());
355 // If this is a directory, just create it and return.
356 if (current_entry_info()->is_directory()) {
357 if (base::CreateDirectory(output_file_path
)) {
358 base::MessageLoopProxy::current()->PostTask(FROM_HERE
, success_callback
);
360 DVLOG(1) << "Unzip failed: unable to create directory.";
361 base::MessageLoopProxy::current()->PostTask(FROM_HERE
, failure_callback
);
366 if (unzOpenCurrentFile(zip_file_
) != UNZ_OK
) {
367 DVLOG(1) << "Unzip failed: unable to open current zip entry.";
368 base::MessageLoopProxy::current()->PostTask(FROM_HERE
, failure_callback
);
372 base::FilePath output_dir_path
= output_file_path
.DirName();
373 if (!base::CreateDirectory(output_dir_path
)) {
374 DVLOG(1) << "Unzip failed: unable to create containing directory.";
375 base::MessageLoopProxy::current()->PostTask(FROM_HERE
, failure_callback
);
379 const int flags
= base::File::FLAG_CREATE_ALWAYS
| base::File::FLAG_WRITE
;
380 base::File
output_file(output_file_path
, flags
);
382 if (!output_file
.IsValid()) {
383 DVLOG(1) << "Unzip failed: unable to create platform file at "
384 << output_file_path
.value();
385 base::MessageLoopProxy::current()->PostTask(FROM_HERE
, failure_callback
);
389 base::MessageLoop::current()->PostTask(
391 base::Bind(&ZipReader::ExtractChunk
,
392 weak_ptr_factory_
.GetWeakPtr(),
393 Passed(output_file
.Pass()),
397 0 /* initial offset */));
400 bool ZipReader::ExtractCurrentEntryIntoDirectory(
401 const base::FilePath
& output_directory_path
) const {
402 DCHECK(current_entry_info_
.get());
404 base::FilePath output_file_path
= output_directory_path
.Append(
405 current_entry_info()->file_path());
406 return ExtractCurrentEntryToFilePath(output_file_path
);
409 bool ZipReader::ExtractCurrentEntryToFile(base::File
* file
) const {
412 // If this is a directory, there's nothing to extract to the file, so return
414 if (current_entry_info()->is_directory())
417 FileWriterDelegate
writer(file
);
418 return ExtractCurrentEntry(&writer
);
421 bool ZipReader::ExtractCurrentEntryToString(size_t max_read_bytes
,
422 std::string
* output
) const {
425 DCHECK_NE(0U, max_read_bytes
);
427 if (current_entry_info()->is_directory()) {
432 // The original_size() is the best hint for the real size, so it saves
433 // doing reallocations for the common case when the uncompressed size is
434 // correct. However, we need to assume that the uncompressed size could be
435 // incorrect therefore this function needs to read as much data as possible.
436 std::string contents
;
437 contents
.reserve(static_cast<size_t>(std::min(
438 static_cast<int64
>(max_read_bytes
),
439 current_entry_info()->original_size())));
441 StringWriterDelegate
writer(max_read_bytes
, &contents
);
442 if (!ExtractCurrentEntry(&writer
))
444 output
->swap(contents
);
448 bool ZipReader::OpenInternal() {
451 unz_global_info zip_info
= {}; // Zero-clear.
452 if (unzGetGlobalInfo(zip_file_
, &zip_info
) != UNZ_OK
) {
455 num_entries_
= zip_info
.number_entry
;
456 if (num_entries_
< 0)
459 // We are already at the end if the zip file is empty.
460 reached_end_
= (num_entries_
== 0);
464 void ZipReader::Reset() {
467 reached_end_
= false;
468 current_entry_info_
.reset();
471 void ZipReader::ExtractChunk(base::File output_file
,
472 const SuccessCallback
& success_callback
,
473 const FailureCallback
& failure_callback
,
474 const ProgressCallback
& progress_callback
,
475 const int64 offset
) {
476 char buffer
[internal::kZipBufSize
];
478 const int num_bytes_read
= unzReadCurrentFile(zip_file_
,
480 internal::kZipBufSize
);
482 if (num_bytes_read
== 0) {
483 unzCloseCurrentFile(zip_file_
);
484 success_callback
.Run();
485 } else if (num_bytes_read
< 0) {
486 DVLOG(1) << "Unzip failed: error while reading zipfile "
487 << "(" << num_bytes_read
<< ")";
488 failure_callback
.Run();
490 if (num_bytes_read
!= output_file
.Write(offset
, buffer
, num_bytes_read
)) {
491 DVLOG(1) << "Unzip failed: unable to write all bytes to target.";
492 failure_callback
.Run();
496 int64 current_progress
= offset
+ num_bytes_read
;
498 progress_callback
.Run(current_progress
);
500 base::MessageLoop::current()->PostTask(
502 base::Bind(&ZipReader::ExtractChunk
,
503 weak_ptr_factory_
.GetWeakPtr(),
504 Passed(output_file
.Pass()),
513 // FileWriterDelegate ----------------------------------------------------------
515 FileWriterDelegate::FileWriterDelegate(base::File
* file
)
520 FileWriterDelegate::~FileWriterDelegate() {
524 file_
->SetLength(file_length_
);
525 DPLOG_IF(ERROR
, !success
) << "Failed updating length of written file";
528 bool FileWriterDelegate::PrepareOutput() {
529 return file_
->Seek(base::File::FROM_BEGIN
, 0) >= 0;
532 bool FileWriterDelegate::WriteBytes(const char* data
, int num_bytes
) {
533 int bytes_written
= file_
->WriteAtCurrentPos(data
, num_bytes
);
534 if (bytes_written
> 0)
535 file_length_
+= bytes_written
;
536 return bytes_written
== num_bytes
;