summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNarayan Kamath2017-10-30 06:17:28 -0500
committerNarayan Kamath2017-10-30 13:26:41 -0500
commit2d1e23f5d6cd6424a261dae5b261197f3611290e (patch)
tree5e4c47a07c4b6359cfcc20c9a73aef50e8add691 /libziparchive/zip_archive.cc
parent51ec0a1387affefdc640536490bbceaf3127d7d7 (diff)
downloadplatform-system-core-2d1e23f5d6cd6424a261dae5b261197f3611290e.tar.gz
platform-system-core-2d1e23f5d6cd6424a261dae5b261197f3611290e.tar.xz
platform-system-core-2d1e23f5d6cd6424a261dae5b261197f3611290e.zip
zip_archive: Allow crc_out to be nullptr in Inflate.
Only compute the crc32 if required. In addition : - Add unit tests for Inflate that cover this addition. - Fix an inconsistency in return codes that was revealed by this new test. Bug: 35246701 Test: zip_archive_tests Test: make; zipalign. Merged-In: I31d7554378f94fc8995f707471d57cb98311e2c2 Change-Id: I05111bfa665c610f93d1c1dee987a509bf87aa65
Diffstat (limited to 'libziparchive/zip_archive.cc')
-rw-r--r--libziparchive/zip_archive.cc10
1 files changed, 6 insertions, 4 deletions
diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc
index a4b5dc551..35d0f0b6a 100644
--- a/libziparchive/zip_archive.cc
+++ b/libziparchive/zip_archive.cc
@@ -908,6 +908,7 @@ int32_t Inflate(const Reader& reader, const uint32_t compressed_length,
908 908
909 std::unique_ptr<z_stream, decltype(zstream_deleter)> zstream_guard(&zstream, zstream_deleter); 909 std::unique_ptr<z_stream, decltype(zstream_deleter)> zstream_guard(&zstream, zstream_deleter);
910 910
911 const bool compute_crc = (crc_out != nullptr);
911 uint64_t crc = 0; 912 uint64_t crc = 0;
912 uint32_t remaining_bytes = compressed_length; 913 uint32_t remaining_bytes = compressed_length;
913 do { 914 do {
@@ -939,9 +940,8 @@ int32_t Inflate(const Reader& reader, const uint32_t compressed_length,
939 if (zstream.avail_out == 0 || (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) { 940 if (zstream.avail_out == 0 || (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) {
940 const size_t write_size = zstream.next_out - &write_buf[0]; 941 const size_t write_size = zstream.next_out - &write_buf[0];
941 if (!writer->Append(&write_buf[0], write_size)) { 942 if (!writer->Append(&write_buf[0], write_size)) {
942 // The file might have declared a bogus length. 943 return kIoError;
943 return kInconsistentInformation; 944 } else if (compute_crc) {
944 } else {
945 crc = crc32(crc, &write_buf[0], write_size); 945 crc = crc32(crc, &write_buf[0], write_size);
946 } 946 }
947 947
@@ -958,7 +958,9 @@ int32_t Inflate(const Reader& reader, const uint32_t compressed_length,
958 // it ourselves above because there are no additional gains to be made by 958 // it ourselves above because there are no additional gains to be made by
959 // having zlib calculate it for us, since they do it by calling crc32 in 959 // having zlib calculate it for us, since they do it by calling crc32 in
960 // the same manner that we have above. 960 // the same manner that we have above.
961 *crc_out = crc; 961 if (compute_crc) {
962 *crc_out = crc;
963 }
962 964
963 if (zstream.total_out != uncompressed_length || remaining_bytes != 0) { 965 if (zstream.total_out != uncompressed_length || remaining_bytes != 0) {
964 ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")", zstream.total_out, 966 ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")", zstream.total_out,