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.
(cherry picked from commit d9e85b14afd5a2ee14bb4df46cf576c5f9c25f24)
Change-Id: I31d7554378f94fc8995f707471d57cb98311e2c2
diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc
index 7535263..1298caf 100644
--- a/libziparchive/zip_archive.cc
+++ b/libziparchive/zip_archive.cc
@@ -924,6 +924,7 @@
std::unique_ptr<z_stream, decltype(zstream_deleter)> zstream_guard(&zstream, zstream_deleter);
+ const bool compute_crc = (crc_out != nullptr);
uint64_t crc = 0;
uint32_t remaining_bytes = compressed_length;
do {
@@ -955,9 +956,8 @@
if (zstream.avail_out == 0 || (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) {
const size_t write_size = zstream.next_out - &write_buf[0];
if (!writer->Append(&write_buf[0], write_size)) {
- // The file might have declared a bogus length.
- return kInconsistentInformation;
- } else {
+ return kIoError;
+ } else if (compute_crc) {
crc = crc32(crc, &write_buf[0], write_size);
}
@@ -974,7 +974,9 @@
// it ourselves above because there are no additional gains to be made by
// having zlib calculate it for us, since they do it by calling crc32 in
// the same manner that we have above.
- *crc_out = crc;
+ if (compute_crc) {
+ *crc_out = crc;
+ }
if (zstream.total_out != uncompressed_length || remaining_bytes != 0) {
ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")", zstream.total_out,