mac: Fix an error in a unit_test from linking against the 10.10 SDK.
[chromium-blink-merge.git] / third_party / zlib / google / zip_reader.cc
blob59d96da14263cbfb88d2230e8250396152e8b85c
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"
7 #include "base/bind.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>
17 #else
18 #include "third_party/zlib/contrib/minizip/unzip.h"
19 #if defined(OS_WIN)
20 #include "third_party/zlib/contrib/minizip/iowin32.h"
21 #endif // defined(OS_WIN)
22 #endif // defined(USE_SYSTEM_MINIZIP)
24 namespace zip {
26 namespace {
28 // FilePathWriterDelegate ------------------------------------------------------
30 // A writer delegate that writes a file at a given path.
31 class FilePathWriterDelegate : public WriterDelegate {
32 public:
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;
45 private:
46 base::FilePath output_file_path_;
47 base::File file_;
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()))
64 return false;
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
79 // std::string.
80 class StringWriterDelegate : public WriterDelegate {
81 public:
82 StringWriterDelegate(size_t max_read_bytes, std::string* output);
83 ~StringWriterDelegate() override;
85 // WriterDelegate methods:
87 // Returns true.
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;
94 private:
95 size_t max_read_bytes_;
96 std::string* output_;
98 DISALLOW_COPY_AND_ASSIGN(StringWriterDelegate);
101 StringWriterDelegate::StringWriterDelegate(size_t max_read_bytes,
102 std::string* output)
103 : max_read_bytes_(max_read_bytes),
104 output_(output) {
107 StringWriterDelegate::~StringWriterDelegate() {
110 bool StringWriterDelegate::PrepareOutput() {
111 return true;
114 bool StringWriterDelegate::WriteBytes(const char* data, int num_bytes) {
115 if (output_->size() + num_bytes > max_read_bytes_)
116 return false;
117 output_->append(data, num_bytes);
118 return true;
121 } // namespace
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(),
141 &file_name_utf16)) {
142 is_unsafe_ = true;
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))
148 is_unsafe_ = true;
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);
163 } else {
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) {
171 Reset();
174 ZipReader::~ZipReader() {
175 Close();
178 bool ZipReader::Open(const base::FilePath& zip_file_path) {
179 DCHECK(!zip_file_);
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());
184 if (!zip_file_) {
185 return false;
188 return OpenInternal();
191 bool ZipReader::OpenFromPlatformFile(base::PlatformFile zip_fd) {
192 DCHECK(!zip_file_);
194 #if defined(OS_POSIX)
195 zip_file_ = internal::OpenFdForUnzipping(zip_fd);
196 #elif defined(OS_WIN)
197 zip_file_ = internal::OpenHandleForUnzipping(zip_fd);
198 #endif
199 if (!zip_file_) {
200 return false;
203 return OpenInternal();
206 bool ZipReader::OpenFromString(const std::string& data) {
207 zip_file_ = internal::PrepareMemoryForUnzipping(data);
208 if (!zip_file_)
209 return false;
210 return OpenInternal();
213 void ZipReader::Close() {
214 if (zip_file_) {
215 unzClose(zip_file_);
217 Reset();
220 bool ZipReader::HasMore() {
221 return !reached_end_;
224 bool ZipReader::AdvanceToNextEntry() {
225 DCHECK(zip_file_);
227 // Should not go further if we already reached the end.
228 if (reached_end_)
229 return false;
231 unz_file_pos position = {};
232 if (unzGetFilePos(zip_file_, &position) != UNZ_OK)
233 return false;
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_) {
238 reached_end_ = true;
239 } else {
240 DCHECK_LT(current_entry_index + 1, num_entries_);
241 if (unzGoToNextFile(zip_file_) != UNZ_OK) {
242 return false;
245 current_entry_info_.reset();
246 return true;
249 bool ZipReader::OpenCurrentEntryInZip() {
250 DCHECK(zip_file_);
252 unz_file_info raw_file_info = {};
253 char raw_file_name_in_zip[internal::kZipMaxPath] = {};
254 const int result = unzGetCurrentFileInfo(zip_file_,
255 &raw_file_info,
256 raw_file_name_in_zip,
257 sizeof(raw_file_name_in_zip) - 1,
258 NULL, // extraField.
259 0, // extraFieldBufferSize.
260 NULL, // szComment.
261 0); // commentBufferSize.
262 if (result != UNZ_OK)
263 return false;
264 if (raw_file_name_in_zip[0] == '\0')
265 return false;
266 current_entry_info_.reset(
267 new EntryInfo(raw_file_name_in_zip, raw_file_info));
268 return true;
271 bool ZipReader::LocateAndOpenEntry(const base::FilePath& path_in_zip) {
272 DCHECK(zip_file_);
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)
281 return false;
283 // Then Open the entry.
284 return OpenCurrentEntryInZip();
287 bool ZipReader::ExtractCurrentEntry(WriterDelegate* delegate) const {
288 DCHECK(zip_file_);
290 const int open_result = unzOpenCurrentFile(zip_file_);
291 if (open_result != UNZ_OK)
292 return false;
294 if (!delegate->PrepareOutput())
295 return false;
297 bool success = true; // This becomes false when something bad happens.
298 scoped_ptr<char[]> buf(new char[internal::kZipBufSize]);
299 while (true) {
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.
304 break;
305 } else if (num_bytes_read < 0) {
306 // If num_bytes_read < 0, then it's a specific UNZ_* error code.
307 success = false;
308 break;
309 } else if (num_bytes_read > 0) {
310 // Some data is read.
311 if (!delegate->WriteBytes(buf.get(), num_bytes_read)) {
312 success = false;
313 break;
318 unzCloseCurrentFile(zip_file_);
320 return success;
323 bool ZipReader::ExtractCurrentEntryToFilePath(
324 const base::FilePath& output_file_path) const {
325 DCHECK(zip_file_);
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);
337 if (success &&
338 current_entry_info()->last_modified() != base::Time::UnixEpoch()) {
339 base::TouchFile(output_file_path,
340 base::Time::Now(),
341 current_entry_info()->last_modified());
344 return success;
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) {
352 DCHECK(zip_file_);
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);
359 } else {
360 DVLOG(1) << "Unzip failed: unable to create directory.";
361 base::MessageLoopProxy::current()->PostTask(FROM_HERE, failure_callback);
363 return;
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);
369 return;
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);
376 return;
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);
386 return;
389 base::MessageLoop::current()->PostTask(
390 FROM_HERE,
391 base::Bind(&ZipReader::ExtractChunk,
392 weak_ptr_factory_.GetWeakPtr(),
393 Passed(output_file.Pass()),
394 success_callback,
395 failure_callback,
396 progress_callback,
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 {
410 DCHECK(zip_file_);
412 // If this is a directory, there's nothing to extract to the file, so return
413 // false.
414 if (current_entry_info()->is_directory())
415 return false;
417 FileWriterDelegate writer(file);
418 return ExtractCurrentEntry(&writer);
421 bool ZipReader::ExtractCurrentEntryToString(size_t max_read_bytes,
422 std::string* output) const {
423 DCHECK(output);
424 DCHECK(zip_file_);
425 DCHECK_NE(0U, max_read_bytes);
427 if (current_entry_info()->is_directory()) {
428 output->clear();
429 return true;
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))
443 return false;
444 output->swap(contents);
445 return true;
448 bool ZipReader::OpenInternal() {
449 DCHECK(zip_file_);
451 unz_global_info zip_info = {}; // Zero-clear.
452 if (unzGetGlobalInfo(zip_file_, &zip_info) != UNZ_OK) {
453 return false;
455 num_entries_ = zip_info.number_entry;
456 if (num_entries_ < 0)
457 return false;
459 // We are already at the end if the zip file is empty.
460 reached_end_ = (num_entries_ == 0);
461 return true;
464 void ZipReader::Reset() {
465 zip_file_ = NULL;
466 num_entries_ = 0;
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_,
479 buffer,
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();
489 } else {
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();
493 return;
496 int64 current_progress = offset + num_bytes_read;
498 progress_callback.Run(current_progress);
500 base::MessageLoop::current()->PostTask(
501 FROM_HERE,
502 base::Bind(&ZipReader::ExtractChunk,
503 weak_ptr_factory_.GetWeakPtr(),
504 Passed(output_file.Pass()),
505 success_callback,
506 failure_callback,
507 progress_callback,
508 current_progress));
513 // FileWriterDelegate ----------------------------------------------------------
515 FileWriterDelegate::FileWriterDelegate(base::File* file)
516 : file_(file),
517 file_length_(0) {
520 FileWriterDelegate::~FileWriterDelegate() {
521 #if !defined(NDEBUG)
522 const bool success =
523 #endif
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;
539 } // namespace zip