| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "third_party/zlib/google/zip_reader.h" | 5 #include "third_party/zlib/google/zip_reader.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 const int result = unzLocateFile(zip_file_, | 282 const int result = unzLocateFile(zip_file_, |
| 283 path_in_zip.AsUTF8Unsafe().c_str(), | 283 path_in_zip.AsUTF8Unsafe().c_str(), |
| 284 kDefaultCaseSensivityOfOS); | 284 kDefaultCaseSensivityOfOS); |
| 285 if (result != UNZ_OK) | 285 if (result != UNZ_OK) |
| 286 return false; | 286 return false; |
| 287 | 287 |
| 288 // Then Open the entry. | 288 // Then Open the entry. |
| 289 return OpenCurrentEntryInZip(); | 289 return OpenCurrentEntryInZip(); |
| 290 } | 290 } |
| 291 | 291 |
| 292 bool ZipReader::ExtractCurrentEntry(WriterDelegate* delegate) const { | 292 bool ZipReader::ExtractCurrentEntry(WriterDelegate* delegate, |
| 293 uint64_t num_bytes_to_extract) const { |
| 293 DCHECK(zip_file_); | 294 DCHECK(zip_file_); |
| 294 | 295 |
| 295 const int open_result = unzOpenCurrentFile(zip_file_); | 296 const int open_result = unzOpenCurrentFile(zip_file_); |
| 296 if (open_result != UNZ_OK) | 297 if (open_result != UNZ_OK) |
| 297 return false; | 298 return false; |
| 298 | 299 |
| 299 if (!delegate->PrepareOutput()) | 300 if (!delegate->PrepareOutput()) |
| 300 return false; | 301 return false; |
| 302 std::unique_ptr<char[]> buf(new char[internal::kZipBufSize]); |
| 301 | 303 |
| 302 bool success = true; // This becomes false when something bad happens. | 304 uint64_t remaining_capacity = num_bytes_to_extract; |
| 303 std::unique_ptr<char[]> buf(new char[internal::kZipBufSize]); | 305 bool entire_file_extracted = false; |
| 304 while (true) { | 306 |
| 305 const int num_bytes_read = unzReadCurrentFile(zip_file_, buf.get(), | 307 while (remaining_capacity > 0) { |
| 306 internal::kZipBufSize); | 308 const int num_bytes_read = |
| 309 unzReadCurrentFile(zip_file_, buf.get(), internal::kZipBufSize); |
| 310 |
| 307 if (num_bytes_read == 0) { | 311 if (num_bytes_read == 0) { |
| 308 // Reached the end of the file. | 312 entire_file_extracted = true; |
| 309 break; | 313 break; |
| 310 } else if (num_bytes_read < 0) { | 314 } else if (num_bytes_read < 0) { |
| 311 // If num_bytes_read < 0, then it's a specific UNZ_* error code. | 315 // If num_bytes_read < 0, then it's a specific UNZ_* error code. |
| 312 success = false; | |
| 313 break; | 316 break; |
| 314 } else if (num_bytes_read > 0) { | 317 } else if (num_bytes_read > 0) { |
| 315 // Some data is read. | 318 uint64_t num_bytes_to_write = std::min<uint64_t>( |
| 316 if (!delegate->WriteBytes(buf.get(), num_bytes_read)) { | 319 remaining_capacity, base::checked_cast<uint64_t>(num_bytes_read)); |
| 317 success = false; | 320 if (!delegate->WriteBytes(buf.get(), num_bytes_to_write)) |
| 318 break; | 321 break; |
| 322 if (remaining_capacity == base::checked_cast<uint64_t>(num_bytes_read)) { |
| 323 // Ensures function returns true if the entire file has been read. |
| 324 entire_file_extracted = |
| 325 (unzReadCurrentFile(zip_file_, buf.get(), 1) == 0); |
| 319 } | 326 } |
| 327 CHECK_GE(remaining_capacity, num_bytes_to_write); |
| 328 remaining_capacity -= num_bytes_to_write; |
| 320 } | 329 } |
| 321 } | 330 } |
| 322 | 331 |
| 323 unzCloseCurrentFile(zip_file_); | 332 unzCloseCurrentFile(zip_file_); |
| 324 | 333 |
| 325 return success; | 334 return entire_file_extracted; |
| 326 } | 335 } |
| 327 | 336 |
| 328 bool ZipReader::ExtractCurrentEntryToFilePath( | 337 bool ZipReader::ExtractCurrentEntryToFilePath( |
| 329 const base::FilePath& output_file_path) const { | 338 const base::FilePath& output_file_path) const { |
| 330 DCHECK(zip_file_); | 339 DCHECK(zip_file_); |
| 331 | 340 |
| 332 // If this is a directory, just create it and return. | 341 // If this is a directory, just create it and return. |
| 333 if (current_entry_info()->is_directory()) | 342 if (current_entry_info()->is_directory()) |
| 334 return base::CreateDirectory(output_file_path); | 343 return base::CreateDirectory(output_file_path); |
| 335 | 344 |
| 336 bool success = false; | 345 bool success = false; |
| 337 { | 346 { |
| 338 FilePathWriterDelegate writer(output_file_path); | 347 FilePathWriterDelegate writer(output_file_path); |
| 339 success = ExtractCurrentEntry(&writer); | 348 success = |
| 349 ExtractCurrentEntry(&writer, std::numeric_limits<uint64_t>::max()); |
| 340 } | 350 } |
| 341 | 351 |
| 342 if (success && | 352 if (success && |
| 343 current_entry_info()->last_modified() != base::Time::UnixEpoch()) { | 353 current_entry_info()->last_modified() != base::Time::UnixEpoch()) { |
| 344 base::TouchFile(output_file_path, | 354 base::TouchFile(output_file_path, |
| 345 base::Time::Now(), | 355 base::Time::Now(), |
| 346 current_entry_info()->last_modified()); | 356 current_entry_info()->last_modified()); |
| 347 } | 357 } |
| 348 | 358 |
| 349 return success; | 359 return success; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 | 419 |
| 410 bool ZipReader::ExtractCurrentEntryToFile(base::File* file) const { | 420 bool ZipReader::ExtractCurrentEntryToFile(base::File* file) const { |
| 411 DCHECK(zip_file_); | 421 DCHECK(zip_file_); |
| 412 | 422 |
| 413 // If this is a directory, there's nothing to extract to the file, so return | 423 // If this is a directory, there's nothing to extract to the file, so return |
| 414 // false. | 424 // false. |
| 415 if (current_entry_info()->is_directory()) | 425 if (current_entry_info()->is_directory()) |
| 416 return false; | 426 return false; |
| 417 | 427 |
| 418 FileWriterDelegate writer(file); | 428 FileWriterDelegate writer(file); |
| 419 return ExtractCurrentEntry(&writer); | 429 return ExtractCurrentEntry(&writer, std::numeric_limits<uint64_t>::max()); |
| 420 } | 430 } |
| 421 | 431 |
| 422 bool ZipReader::ExtractCurrentEntryToString(size_t max_read_bytes, | 432 bool ZipReader::ExtractCurrentEntryToString(uint64_t max_read_bytes, |
| 423 std::string* output) const { | 433 std::string* output) const { |
| 424 DCHECK(output); | 434 DCHECK(output); |
| 425 DCHECK(zip_file_); | 435 DCHECK(zip_file_); |
| 426 DCHECK_NE(0U, max_read_bytes); | 436 |
| 437 if (max_read_bytes == 0) { |
| 438 output->clear(); |
| 439 return true; |
| 440 } |
| 427 | 441 |
| 428 if (current_entry_info()->is_directory()) { | 442 if (current_entry_info()->is_directory()) { |
| 429 output->clear(); | 443 output->clear(); |
| 430 return true; | 444 return true; |
| 431 } | 445 } |
| 432 | 446 |
| 433 // The original_size() is the best hint for the real size, so it saves | 447 // The original_size() is the best hint for the real size, so it saves |
| 434 // doing reallocations for the common case when the uncompressed size is | 448 // doing reallocations for the common case when the uncompressed size is |
| 435 // correct. However, we need to assume that the uncompressed size could be | 449 // correct. However, we need to assume that the uncompressed size could be |
| 436 // incorrect therefore this function needs to read as much data as possible. | 450 // incorrect therefore this function needs to read as much data as possible. |
| 437 std::string contents; | 451 std::string contents; |
| 438 contents.reserve( | 452 contents.reserve( |
| 439 static_cast<size_t>(std::min(static_cast<int64_t>(max_read_bytes), | 453 static_cast<size_t>(std::min(base::checked_cast<int64_t>(max_read_bytes), |
| 440 current_entry_info()->original_size()))); | 454 current_entry_info()->original_size()))); |
| 441 | 455 |
| 442 StringWriterDelegate writer(max_read_bytes, &contents); | 456 StringWriterDelegate writer(max_read_bytes, &contents); |
| 443 if (!ExtractCurrentEntry(&writer)) | 457 if (!ExtractCurrentEntry(&writer, max_read_bytes)) { |
| 458 if (contents.length() < max_read_bytes) { |
| 459 // There was an error in extracting entry. If ExtractCurrentEntry() |
| 460 // returns false, the entire file was not read - in which case |
| 461 // contents.length() should equal |max_read_bytes| unless an error |
| 462 // occurred which caused extraction to be aborted. |
| 463 output->clear(); |
| 464 } else { |
| 465 // |num_bytes| is less than the length of current entry. |
| 466 output->swap(contents); |
| 467 } |
| 444 return false; | 468 return false; |
| 469 } |
| 445 output->swap(contents); | 470 output->swap(contents); |
| 446 return true; | 471 return true; |
| 447 } | 472 } |
| 448 | 473 |
| 449 bool ZipReader::OpenInternal() { | 474 bool ZipReader::OpenInternal() { |
| 450 DCHECK(zip_file_); | 475 DCHECK(zip_file_); |
| 451 | 476 |
| 452 unz_global_info zip_info = {}; // Zero-clear. | 477 unz_global_info zip_info = {}; // Zero-clear. |
| 453 if (unzGetGlobalInfo(zip_file_, &zip_info) != UNZ_OK) { | 478 if (unzGetGlobalInfo(zip_file_, &zip_info) != UNZ_OK) { |
| 454 return false; | 479 return false; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 } | 549 } |
| 525 | 550 |
| 526 bool FileWriterDelegate::WriteBytes(const char* data, int num_bytes) { | 551 bool FileWriterDelegate::WriteBytes(const char* data, int num_bytes) { |
| 527 int bytes_written = file_->WriteAtCurrentPos(data, num_bytes); | 552 int bytes_written = file_->WriteAtCurrentPos(data, num_bytes); |
| 528 if (bytes_written > 0) | 553 if (bytes_written > 0) |
| 529 file_length_ += bytes_written; | 554 file_length_ += bytes_written; |
| 530 return bytes_written == num_bytes; | 555 return bytes_written == num_bytes; |
| 531 } | 556 } |
| 532 | 557 |
| 533 } // namespace zip | 558 } // namespace zip |
| OLD | NEW |