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

Side by Side Diff: third_party/zlib/google/zip_reader.cc

Issue 2961373002: Improve Zip File Scanning on Mac (Closed)
Patch Set: addressing comments 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 unified diff | Download patch
OLDNEW
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
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 }
315 if (num_bytes_read < 0) {
satorux1 2017/08/03 08:36:48 nit: else if (sorry, my code sample was bad)
mortonm 2017/08/03 15:01:23 Done.
311 // If num_bytes_read < 0, then it's a specific UNZ_* error code. 316 // If num_bytes_read < 0, then it's a specific UNZ_* error code.
312 success = false;
313 break; 317 break;
314 } else if (num_bytes_read > 0) { 318 } else if (num_bytes_read > 0) {
315 // Some data is read. 319 uint64_t num_bytes_to_write = std::min<uint64_t>(
316 if (!delegate->WriteBytes(buf.get(), num_bytes_read)) { 320 remaining_capacity, base::checked_cast<uint64_t>(num_bytes_read));
317 success = false; 321 if (!delegate->WriteBytes(buf.get(), num_bytes_to_write))
318 break; 322 break;
323 if (remaining_capacity == base::checked_cast<uint64_t>(num_bytes_read)) {
324 // Ensures function returns true if the entire file has been read.
325 entire_file_extracted =
326 (unzReadCurrentFile(zip_file_, buf.get(), 1) == 0);
319 } 327 }
328 CHECK_GE(remaining_capacity, num_bytes_to_write);
329 remaining_capacity -= num_bytes_to_write;
320 } 330 }
321 } 331 }
322 332
323 unzCloseCurrentFile(zip_file_); 333 unzCloseCurrentFile(zip_file_);
324 334
325 return success; 335 return entire_file_extracted;
326 } 336 }
327 337
328 bool ZipReader::ExtractCurrentEntryToFilePath( 338 bool ZipReader::ExtractCurrentEntryToFilePath(
329 const base::FilePath& output_file_path) const { 339 const base::FilePath& output_file_path) const {
330 DCHECK(zip_file_); 340 DCHECK(zip_file_);
331 341
332 // If this is a directory, just create it and return. 342 // If this is a directory, just create it and return.
333 if (current_entry_info()->is_directory()) 343 if (current_entry_info()->is_directory())
334 return base::CreateDirectory(output_file_path); 344 return base::CreateDirectory(output_file_path);
335 345
336 bool success = false; 346 bool success = false;
337 { 347 {
338 FilePathWriterDelegate writer(output_file_path); 348 FilePathWriterDelegate writer(output_file_path);
339 success = ExtractCurrentEntry(&writer); 349 success =
350 ExtractCurrentEntry(&writer, std::numeric_limits<uint64_t>::max());
340 } 351 }
341 352
342 if (success && 353 if (success &&
343 current_entry_info()->last_modified() != base::Time::UnixEpoch()) { 354 current_entry_info()->last_modified() != base::Time::UnixEpoch()) {
344 base::TouchFile(output_file_path, 355 base::TouchFile(output_file_path,
345 base::Time::Now(), 356 base::Time::Now(),
346 current_entry_info()->last_modified()); 357 current_entry_info()->last_modified());
347 } 358 }
348 359
349 return success; 360 return success;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 420
410 bool ZipReader::ExtractCurrentEntryToFile(base::File* file) const { 421 bool ZipReader::ExtractCurrentEntryToFile(base::File* file) const {
411 DCHECK(zip_file_); 422 DCHECK(zip_file_);
412 423
413 // If this is a directory, there's nothing to extract to the file, so return 424 // If this is a directory, there's nothing to extract to the file, so return
414 // false. 425 // false.
415 if (current_entry_info()->is_directory()) 426 if (current_entry_info()->is_directory())
416 return false; 427 return false;
417 428
418 FileWriterDelegate writer(file); 429 FileWriterDelegate writer(file);
419 return ExtractCurrentEntry(&writer); 430 return ExtractCurrentEntry(&writer, std::numeric_limits<uint64_t>::max());
420 } 431 }
421 432
422 bool ZipReader::ExtractCurrentEntryToString(size_t max_read_bytes, 433 bool ZipReader::ExtractCurrentEntryToString(uint64_t max_read_bytes,
423 std::string* output) const { 434 std::string* output) const {
424 DCHECK(output); 435 DCHECK(output);
425 DCHECK(zip_file_); 436 DCHECK(zip_file_);
426 DCHECK_NE(0U, max_read_bytes); 437
438 if (max_read_bytes == 0) {
439 output->clear();
440 return true;
441 }
427 442
428 if (current_entry_info()->is_directory()) { 443 if (current_entry_info()->is_directory()) {
429 output->clear(); 444 output->clear();
430 return true; 445 return true;
431 } 446 }
432 447
433 // The original_size() is the best hint for the real size, so it saves 448 // 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 449 // doing reallocations for the common case when the uncompressed size is
435 // correct. However, we need to assume that the uncompressed size could be 450 // 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. 451 // incorrect therefore this function needs to read as much data as possible.
437 std::string contents; 452 std::string contents;
438 contents.reserve( 453 contents.reserve(
439 static_cast<size_t>(std::min(static_cast<int64_t>(max_read_bytes), 454 static_cast<size_t>(std::min(base::checked_cast<int64_t>(max_read_bytes),
440 current_entry_info()->original_size()))); 455 current_entry_info()->original_size())));
441 456
442 StringWriterDelegate writer(max_read_bytes, &contents); 457 StringWriterDelegate writer(max_read_bytes, &contents);
443 if (!ExtractCurrentEntry(&writer)) 458 if (!ExtractCurrentEntry(&writer, max_read_bytes)) {
459 if (contents.length() < max_read_bytes) {
460 // There was an error in extracting entry. If ExtractCurrentEntry()
461 // returns false, the entire file was not read - in which case
462 // contents.length() should equal |max_read_bytes| unless an error
463 // occurred which caused extraction to be aborted.
464 output->clear();
465 } else {
466 // |num_bytes| is less than the length of current entry.
467 output->swap(contents);
468 }
444 return false; 469 return false;
470 }
445 output->swap(contents); 471 output->swap(contents);
446 return true; 472 return true;
447 } 473 }
448 474
449 bool ZipReader::OpenInternal() { 475 bool ZipReader::OpenInternal() {
450 DCHECK(zip_file_); 476 DCHECK(zip_file_);
451 477
452 unz_global_info zip_info = {}; // Zero-clear. 478 unz_global_info zip_info = {}; // Zero-clear.
453 if (unzGetGlobalInfo(zip_file_, &zip_info) != UNZ_OK) { 479 if (unzGetGlobalInfo(zip_file_, &zip_info) != UNZ_OK) {
454 return false; 480 return false;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 } 550 }
525 551
526 bool FileWriterDelegate::WriteBytes(const char* data, int num_bytes) { 552 bool FileWriterDelegate::WriteBytes(const char* data, int num_bytes) {
527 int bytes_written = file_->WriteAtCurrentPos(data, num_bytes); 553 int bytes_written = file_->WriteAtCurrentPos(data, num_bytes);
528 if (bytes_written > 0) 554 if (bytes_written > 0)
529 file_length_ += bytes_written; 555 file_length_ += bytes_written;
530 return bytes_written == num_bytes; 556 return bytes_written == num_bytes;
531 } 557 }
532 558
533 } // namespace zip 559 } // namespace zip
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698