diff --git a/include/ziparchive/zip_archive.h b/include/ziparchive/zip_archive.h index ece86931a..73ae68d3f 100644 --- a/include/ziparchive/zip_archive.h +++ b/include/ziparchive/zip_archive.h @@ -28,8 +28,8 @@ /* Zip compression methods we support */ enum { - kCompressStored = 0, // no compression - kCompressDeflated = 8, // standard deflate + kCompressStored = 0, // no compression + kCompressDeflated = 8, // standard deflate }; struct ZipString { @@ -44,19 +44,17 @@ struct ZipString { explicit ZipString(const char* entry_name); bool operator==(const ZipString& rhs) const { - return name && (name_length == rhs.name_length) && - (memcmp(name, rhs.name, name_length) == 0); + return name && (name_length == rhs.name_length) && (memcmp(name, rhs.name, name_length) == 0); } bool StartsWith(const ZipString& prefix) const { return name && (name_length >= prefix.name_length) && - (memcmp(name, prefix.name, prefix.name_length) == 0); + (memcmp(name, prefix.name, prefix.name_length) == 0); } bool EndsWith(const ZipString& suffix) const { return name && (name_length >= suffix.name_length) && - (memcmp(name + name_length - suffix.name_length, suffix.name, - suffix.name_length) == 0); + (memcmp(name + name_length - suffix.name_length, suffix.name, suffix.name_length) == 0); } }; @@ -134,11 +132,11 @@ int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle); * * Returns 0 on success, and negative values on failure. */ -int32_t OpenArchiveFd(const int fd, const char* debugFileName, - ZipArchiveHandle *handle, bool assume_ownership = true); +int32_t OpenArchiveFd(const int fd, const char* debugFileName, ZipArchiveHandle* handle, + bool assume_ownership = true); int32_t OpenArchiveFromMemory(void* address, size_t length, const char* debugFileName, - ZipArchiveHandle *handle); + ZipArchiveHandle* handle); /* * Close archive, releasing resources associated with it. This will * unmap the central directory of the zipfile and free all internal @@ -164,8 +162,7 @@ void CloseArchive(ZipArchiveHandle handle); * On non-Windows platforms this method does not modify internal state and * can be called concurrently. */ -int32_t FindEntry(const ZipArchiveHandle handle, const ZipString& entryName, - ZipEntry* data); +int32_t FindEntry(const ZipArchiveHandle handle, const ZipString& entryName, ZipEntry* data); /* * Start iterating over all entries of a zip file. The order of iteration @@ -180,8 +177,7 @@ int32_t FindEntry(const ZipArchiveHandle handle, const ZipString& entryName, * * Returns 0 on success and negative values on failure. */ -int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr, - const ZipString* optional_prefix, +int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr, const ZipString* optional_prefix, const ZipString* optional_suffix); /* @@ -217,8 +213,7 @@ int32_t ExtractEntryToFile(ZipArchiveHandle handle, ZipEntry* entry, int fd); * * Returns 0 on success and negative values on failure. */ -int32_t ExtractToMemory(ZipArchiveHandle handle, ZipEntry* entry, - uint8_t* begin, uint32_t size); +int32_t ExtractToMemory(ZipArchiveHandle handle, ZipEntry* entry, uint8_t* begin, uint32_t size); int GetFileDescriptor(const ZipArchiveHandle handle); @@ -230,9 +225,9 @@ typedef bool (*ProcessZipEntryFunction)(const uint8_t* buf, size_t buf_size, voi /* * Stream the uncompressed data through the supplied function, * passing cookie to it each time it gets called. -*/ + */ int32_t ProcessZipEntryContents(ZipArchiveHandle handle, ZipEntry* entry, - ProcessZipEntryFunction func, void* cookie); + ProcessZipEntryFunction func, void* cookie); #endif #endif // LIBZIPARCHIVE_ZIPARCHIVE_H_ diff --git a/include/ziparchive/zip_writer.h b/include/ziparchive/zip_writer.h index 08ead4845..94595143d 100644 --- a/include/ziparchive/zip_writer.h +++ b/include/ziparchive/zip_writer.h @@ -17,9 +17,9 @@ #ifndef LIBZIPARCHIVE_ZIPWRITER_H_ #define LIBZIPARCHIVE_ZIPWRITER_H_ +#include #include #include -#include #include #include @@ -50,7 +50,7 @@ * fclose(file); */ class ZipWriter { -public: + public: enum { /** * Flag to compress the zip entry using deflate. @@ -120,8 +120,7 @@ public: /** * Same as StartAlignedEntry(const char*, size_t), but sets a last modified time for the entry. */ - int32_t StartAlignedEntryWithTime(const char* path, size_t flags, time_t time, - uint32_t alignment); + int32_t StartAlignedEntryWithTime(const char* path, size_t flags, time_t time, uint32_t alignment); /** * Writes bytes to the zip file for the previously started zip entry. @@ -156,7 +155,7 @@ public: */ int32_t Finish(); -private: + private: DISALLOW_COPY_AND_ASSIGN(ZipWriter); int32_t HandleError(int32_t error_code); @@ -179,7 +178,7 @@ private: std::vector files_; FileEntry current_file_entry_; - std::unique_ptr z_stream_; + std::unique_ptr z_stream_; std::vector buffer_; }; diff --git a/libziparchive/entry_name_utils-inl.h b/libziparchive/entry_name_utils-inl.h index ddbc286bb..5fc2fb458 100644 --- a/libziparchive/entry_name_utils-inl.h +++ b/libziparchive/entry_name_utils-inl.h @@ -55,5 +55,4 @@ inline bool IsValidEntryName(const uint8_t* entry_name, const size_t length) { return true; } - #endif // LIBZIPARCHIVE_ENTRY_NAME_UTILS_INL_H_ diff --git a/libziparchive/entry_name_utils_test.cc b/libziparchive/entry_name_utils_test.cc index 20715bbb6..d83d8540c 100644 --- a/libziparchive/entry_name_utils_test.cc +++ b/libziparchive/entry_name_utils_test.cc @@ -20,44 +20,43 @@ TEST(entry_name_utils, NullChars) { // 'A', 'R', '\0', 'S', 'E' - const uint8_t zeroes[] = { 0x41, 0x52, 0x00, 0x53, 0x45 }; + const uint8_t zeroes[] = {0x41, 0x52, 0x00, 0x53, 0x45}; ASSERT_FALSE(IsValidEntryName(zeroes, sizeof(zeroes))); - const uint8_t zeroes_continuation_chars[] = { 0xc2, 0xa1, 0xc2, 0x00 }; - ASSERT_FALSE(IsValidEntryName(zeroes_continuation_chars, - sizeof(zeroes_continuation_chars))); + const uint8_t zeroes_continuation_chars[] = {0xc2, 0xa1, 0xc2, 0x00}; + ASSERT_FALSE(IsValidEntryName(zeroes_continuation_chars, sizeof(zeroes_continuation_chars))); } TEST(entry_name_utils, InvalidSequence) { // 0xfe is an invalid start byte - const uint8_t invalid[] = { 0x41, 0xfe }; + const uint8_t invalid[] = {0x41, 0xfe}; ASSERT_FALSE(IsValidEntryName(invalid, sizeof(invalid))); // 0x91 is an invalid start byte (it's a valid continuation byte). - const uint8_t invalid2[] = { 0x41, 0x91 }; + const uint8_t invalid2[] = {0x41, 0x91}; ASSERT_FALSE(IsValidEntryName(invalid2, sizeof(invalid2))); } TEST(entry_name_utils, TruncatedContinuation) { // Malayalam script with truncated bytes. There should be 2 bytes // after 0xe0 - const uint8_t truncated[] = { 0xe0, 0xb4, 0x85, 0xe0, 0xb4 }; + const uint8_t truncated[] = {0xe0, 0xb4, 0x85, 0xe0, 0xb4}; ASSERT_FALSE(IsValidEntryName(truncated, sizeof(truncated))); // 0xc2 is the start of a 2 byte sequence that we've subsequently // dropped. - const uint8_t truncated2[] = { 0xc2, 0xc2, 0xa1 }; + const uint8_t truncated2[] = {0xc2, 0xc2, 0xa1}; ASSERT_FALSE(IsValidEntryName(truncated2, sizeof(truncated2))); } TEST(entry_name_utils, BadContinuation) { // 0x41 is an invalid continuation char, since it's MSBs // aren't "10..." (are 01). - const uint8_t bad[] = { 0xc2, 0xa1, 0xc2, 0x41 }; + const uint8_t bad[] = {0xc2, 0xa1, 0xc2, 0x41}; ASSERT_FALSE(IsValidEntryName(bad, sizeof(bad))); // 0x41 is an invalid continuation char, since it's MSBs // aren't "10..." (are 11). - const uint8_t bad2[] = { 0xc2, 0xa1, 0xc2, 0xfe }; + const uint8_t bad2[] = {0xc2, 0xa1, 0xc2, 0xfe}; ASSERT_FALSE(IsValidEntryName(bad2, sizeof(bad2))); } diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc index a310a0777..17c268bc2 100644 --- a/libziparchive/zip_archive.cc +++ b/libziparchive/zip_archive.cc @@ -115,8 +115,7 @@ static uint32_t ComputeHash(const ZipString& name) { * Convert a ZipEntry to a hash table index, verifying that it's in a * valid range. */ -static int64_t EntryToIndex(const ZipString* hash_table, - const uint32_t hash_table_size, +static int64_t EntryToIndex(const ZipString* hash_table, const uint32_t hash_table_size, const ZipString& name) { const uint32_t hash = ComputeHash(name); @@ -137,7 +136,7 @@ static int64_t EntryToIndex(const ZipString* hash_table, /* * Add a new entry to the hash table. */ -static int32_t AddToHash(ZipString *hash_table, const uint64_t hash_table_size, +static int32_t AddToHash(ZipString* hash_table, const uint64_t hash_table_size, const ZipString& name) { const uint64_t hash = ComputeHash(name); uint32_t ent = hash & (hash_table_size - 1); @@ -161,13 +160,12 @@ static int32_t AddToHash(ZipString *hash_table, const uint64_t hash_table_size, } static int32_t MapCentralDirectory0(const char* debug_file_name, ZipArchive* archive, - off64_t file_length, off64_t read_amount, - uint8_t* scan_buffer) { + off64_t file_length, off64_t read_amount, uint8_t* scan_buffer) { const off64_t search_start = file_length - read_amount; - if(!archive->mapped_zip.ReadAtOffset(scan_buffer, read_amount, search_start)) { - ALOGE("Zip: read %" PRId64 " from offset %" PRId64 " failed", - static_cast(read_amount), static_cast(search_start)); + if (!archive->mapped_zip.ReadAtOffset(scan_buffer, read_amount, search_start)) { + ALOGE("Zip: read %" PRId64 " from offset %" PRId64 " failed", static_cast(read_amount), + static_cast(search_start)); return kIoError; } @@ -198,8 +196,7 @@ static int32_t MapCentralDirectory0(const char* debug_file_name, ZipArchive* arc * Verify that there's no trailing space at the end of the central directory * and its comment. */ - const off64_t calculated_length = eocd_offset + sizeof(EocdRecord) - + eocd->comment_length; + const off64_t calculated_length = eocd_offset + sizeof(EocdRecord) + eocd->comment_length; if (calculated_length != file_length) { ALOGW("Zip: %" PRId64 " extraneous bytes at the end of the central directory", static_cast(file_length - calculated_length)); @@ -212,7 +209,7 @@ static int32_t MapCentralDirectory0(const char* debug_file_name, ZipArchive* arc */ if (static_cast(eocd->cd_start_offset) + eocd->cd_size > eocd_offset) { ALOGW("Zip: bad offsets (dir %" PRIu32 ", size %" PRIu32 ", eocd %" PRId64 ")", - eocd->cd_start_offset, eocd->cd_size, static_cast(eocd_offset)); + eocd->cd_start_offset, eocd->cd_size, static_cast(eocd_offset)); #if defined(__ANDROID__) if (eocd->cd_start_offset + eocd->cd_size <= eocd_offset) { android_errorWriteLog(0x534e4554, "31251826"); @@ -225,8 +222,8 @@ static int32_t MapCentralDirectory0(const char* debug_file_name, ZipArchive* arc return kEmptyArchive; } - ALOGV("+++ num_entries=%" PRIu32 " dir_size=%" PRIu32 " dir_offset=%" PRIu32, - eocd->num_records, eocd->cd_size, eocd->cd_start_offset); + ALOGV("+++ num_entries=%" PRIu32 " dir_size=%" PRIu32 " dir_offset=%" PRIu32, eocd->num_records, + eocd->cd_size, eocd->cd_start_offset); /* * It all looks good. Create a mapping for the CD, and set the fields @@ -255,7 +252,6 @@ static int32_t MapCentralDirectory0(const char* debug_file_name, ZipArchive* arc * num_entries */ static int32_t MapCentralDirectory(const char* debug_file_name, ZipArchive* archive) { - // Test file length. We use lseek64 to make sure the file // is small enough to be a zip file (Its size must be less than // 0xffffffff bytes). @@ -292,8 +288,8 @@ static int32_t MapCentralDirectory(const char* debug_file_name, ZipArchive* arch } std::vector scan_buffer(read_amount); - int32_t result = MapCentralDirectory0(debug_file_name, archive, file_length, read_amount, - scan_buffer.data()); + int32_t result = + MapCentralDirectory0(debug_file_name, archive, file_length, read_amount, scan_buffer.data()); return result; } @@ -314,8 +310,8 @@ static int32_t ParseZipArchive(ZipArchive* archive) { * least one unused entry to avoid an infinite loop during creation. */ archive->hash_table_size = RoundUpPower2(1 + (num_entries * 4) / 3); - archive->hash_table = reinterpret_cast(calloc(archive->hash_table_size, - sizeof(ZipString))); + archive->hash_table = + reinterpret_cast(calloc(archive->hash_table_size, sizeof(ZipString))); if (archive->hash_table == nullptr) { ALOGW("Zip: unable to allocate the %u-entry hash_table, entry size: %zu", archive->hash_table_size, sizeof(ZipString)); @@ -337,8 +333,7 @@ static int32_t ParseZipArchive(ZipArchive* archive) { return -1; } - const CentralDirectoryRecord* cdr = - reinterpret_cast(ptr); + const CentralDirectoryRecord* cdr = reinterpret_cast(ptr); if (cdr->record_signature != CentralDirectoryRecord::kSignature) { ALOGW("Zip: missed a central dir sig (at %" PRIu16 ")", i); return -1; @@ -347,7 +342,7 @@ static int32_t ParseZipArchive(ZipArchive* archive) { const off64_t local_header_offset = cdr->local_file_header_offset; if (local_header_offset >= archive->directory_offset) { ALOGW("Zip: bad LFH offset %" PRId64 " at entry %" PRIu16, - static_cast(local_header_offset), i); + static_cast(local_header_offset), i); return -1; } @@ -357,8 +352,10 @@ static int32_t ParseZipArchive(ZipArchive* archive) { const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord); if (file_name + file_name_length > cd_end) { - ALOGW("Zip: file name boundary exceeds the central directory range, file_name_length: " - "%" PRIx16 ", cd_length: %zu", file_name_length, cd_length); + ALOGW( + "Zip: file name boundary exceeds the central directory range, file_name_length: " + "%" PRIx16 ", cd_length: %zu", + file_name_length, cd_length); return -1; } /* check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters */ @@ -370,8 +367,7 @@ static int32_t ParseZipArchive(ZipArchive* archive) { ZipString entry_name; entry_name.name = file_name; entry_name.name_length = file_name_length; - const int add_result = AddToHash(archive->hash_table, - archive->hash_table_size, entry_name); + const int add_result = AddToHash(archive->hash_table, archive->hash_table_size, entry_name); if (add_result != 0) { ALOGW("Zip: Error adding entry to hash table %d", add_result); return add_result; @@ -379,8 +375,7 @@ static int32_t ParseZipArchive(ZipArchive* archive) { ptr += sizeof(CentralDirectoryRecord) + file_name_length + extra_length + comment_length; if ((ptr - cd_ptr) > static_cast(cd_length)) { - ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu16, - ptr - cd_ptr, cd_length, i); + ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu16, ptr - cd_ptr, cd_length, i); return -1; } } @@ -389,8 +384,7 @@ static int32_t ParseZipArchive(ZipArchive* archive) { return 0; } -static int32_t OpenArchiveInternal(ZipArchive* archive, - const char* debug_file_name) { +static int32_t OpenArchiveInternal(ZipArchive* archive, const char* debug_file_name) { int32_t result = -1; if ((result = MapCentralDirectory(debug_file_name, archive)) != 0) { return result; @@ -403,8 +397,8 @@ static int32_t OpenArchiveInternal(ZipArchive* archive, return 0; } -int32_t OpenArchiveFd(int fd, const char* debug_file_name, - ZipArchiveHandle* handle, bool assume_ownership) { +int32_t OpenArchiveFd(int fd, const char* debug_file_name, ZipArchiveHandle* handle, + bool assume_ownership) { ZipArchive* archive = new ZipArchive(fd, assume_ownership); *handle = archive; return OpenArchiveInternal(archive, debug_file_name); @@ -424,7 +418,7 @@ int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) { } int32_t OpenArchiveFromMemory(void* address, size_t length, const char* debug_file_name, - ZipArchiveHandle *handle) { + ZipArchiveHandle* handle) { ZipArchive* archive = new ZipArchive(address, length); *handle = archive; return OpenArchiveInternal(archive, debug_file_name); @@ -464,8 +458,7 @@ static int32_t ValidateDataDescriptor(MappedZipFile& mapped_zip, ZipEntry* entry return 0; } -static int32_t FindEntry(const ZipArchive* archive, const int ent, - ZipEntry* data) { +static int32_t FindEntry(const ZipArchive* archive, const int ent, ZipEntry* data) { const uint16_t nameLen = archive->hash_table[ent].name_length; // Recover the start of the central directory entry from the filename @@ -483,8 +476,7 @@ static int32_t FindEntry(const ZipArchive* archive, const int ent, return kInvalidOffset; } - const CentralDirectoryRecord *cdr = - reinterpret_cast(ptr); + const CentralDirectoryRecord* cdr = reinterpret_cast(ptr); // The offset of the start of the central directory in the zipfile. // We keep this lying around so that we can sanity check all our lengths @@ -512,15 +504,15 @@ static int32_t FindEntry(const ZipArchive* archive, const int ent, uint8_t lfh_buf[sizeof(LocalFileHeader)]; if (!archive->mapped_zip.ReadAtOffset(lfh_buf, sizeof(lfh_buf), local_header_offset)) { ALOGW("Zip: failed reading lfh name from offset %" PRId64, - static_cast(local_header_offset)); + static_cast(local_header_offset)); return kIoError; } - const LocalFileHeader *lfh = reinterpret_cast(lfh_buf); + const LocalFileHeader* lfh = reinterpret_cast(lfh_buf); if (lfh->lfh_signature != LocalFileHeader::kSignature) { ALOGW("Zip: didn't find signature at start of lfh, offset=%" PRId64, - static_cast(local_header_offset)); + static_cast(local_header_offset)); return kInvalidOffset; } @@ -549,13 +541,12 @@ static int32_t FindEntry(const ZipArchive* archive, const int ent, // header agree on the crc, compressed, and uncompressed sizes of the entry. if ((lfh->gpb_flags & kGPBDDFlagMask) == 0) { data->has_data_descriptor = 0; - if (data->compressed_length != lfh->compressed_size - || data->uncompressed_length != lfh->uncompressed_size - || data->crc32 != lfh->crc32) { - ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32 - ", %" PRIx32 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}", - data->compressed_length, data->uncompressed_length, data->crc32, - lfh->compressed_size, lfh->uncompressed_size, lfh->crc32); + if (data->compressed_length != lfh->compressed_size || + data->uncompressed_length != lfh->uncompressed_size || data->crc32 != lfh->crc32) { + ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32 ", %" PRIx32 + "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}", + data->compressed_length, data->uncompressed_length, data->crc32, lfh->compressed_size, + lfh->uncompressed_size, lfh->crc32); return kInconsistentInformation; } } else { @@ -593,8 +584,8 @@ static int32_t FindEntry(const ZipArchive* archive, const int ent, return kInconsistentInformation; } - const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader) - + lfh->file_name_length + lfh->extra_field_length; + const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader) + + lfh->file_name_length + lfh->extra_field_length; if (data_offset > cd_offset) { ALOGW("Zip: bad data offset %" PRId64 " in zip", static_cast(data_offset)); return kInvalidOffset; @@ -602,16 +593,17 @@ static int32_t FindEntry(const ZipArchive* archive, const int ent, if (static_cast(data_offset + data->compressed_length) > cd_offset) { ALOGW("Zip: bad compressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")", - static_cast(data_offset), data->compressed_length, static_cast(cd_offset)); + static_cast(data_offset), data->compressed_length, + static_cast(cd_offset)); return kInvalidOffset; } if (data->method == kCompressStored && - static_cast(data_offset + data->uncompressed_length) > cd_offset) { - ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")", - static_cast(data_offset), data->uncompressed_length, - static_cast(cd_offset)); - return kInvalidOffset; + static_cast(data_offset + data->uncompressed_length) > cd_offset) { + ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")", + static_cast(data_offset), data->uncompressed_length, + static_cast(cd_offset)); + return kInvalidOffset; } data->offset = data_offset; @@ -626,8 +618,7 @@ struct IterationHandle { ZipString suffix; ZipArchive* archive; - IterationHandle(const ZipString* in_prefix, - const ZipString* in_suffix) { + IterationHandle(const ZipString* in_prefix, const ZipString* in_suffix) { if (in_prefix) { uint8_t* name_copy = new uint8_t[in_prefix->name_length]; memcpy(name_copy, in_prefix->name, in_prefix->name_length); @@ -654,8 +645,7 @@ struct IterationHandle { } }; -int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr, - const ZipString* optional_prefix, +int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr, const ZipString* optional_prefix, const ZipString* optional_suffix) { ZipArchive* archive = reinterpret_cast(handle); @@ -668,7 +658,7 @@ int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr, cookie->position = 0; cookie->archive = archive; - *cookie_ptr = cookie ; + *cookie_ptr = cookie; return 0; } @@ -676,16 +666,14 @@ void EndIteration(void* cookie) { delete reinterpret_cast(cookie); } -int32_t FindEntry(const ZipArchiveHandle handle, const ZipString& entryName, - ZipEntry* data) { +int32_t FindEntry(const ZipArchiveHandle handle, const ZipString& entryName, ZipEntry* data) { const ZipArchive* archive = reinterpret_cast(handle); if (entryName.name_length == 0) { ALOGW("Zip: Invalid filename %.*s", entryName.name_length, entryName.name); return kInvalidEntryName; } - const int64_t ent = EntryToIndex(archive->hash_table, - archive->hash_table_size, entryName); + const int64_t ent = EntryToIndex(archive->hash_table, archive->hash_table_size, entryName); if (ent < 0) { ALOGV("Zip: Could not find entry %.*s", entryName.name_length, entryName.name); @@ -713,10 +701,8 @@ int32_t Next(void* cookie, ZipEntry* data, ZipString* name) { for (uint32_t i = currentOffset; i < hash_table_length; ++i) { if (hash_table[i].name != NULL && - (handle->prefix.name_length == 0 || - hash_table[i].StartsWith(handle->prefix)) && - (handle->suffix.name_length == 0 || - hash_table[i].EndsWith(handle->suffix))) { + (handle->prefix.name_length == 0 || hash_table[i].StartsWith(handle->prefix)) && + (handle->suffix.name_length == 0 || hash_table[i].EndsWith(handle->suffix))) { handle->position = (i + 1); const int error = FindEntry(archive, i, data); if (!error) { @@ -736,8 +722,10 @@ class Writer { public: virtual bool Append(uint8_t* buf, size_t buf_size) = 0; virtual ~Writer() {} + protected: Writer() = default; + private: DISALLOW_COPY_AND_ASSIGN(Writer); }; @@ -747,14 +735,12 @@ class Writer { // the data appended to it. class MemoryWriter : public Writer { public: - MemoryWriter(uint8_t* buf, size_t size) : Writer(), - buf_(buf), size_(size), bytes_written_(0) { - } + MemoryWriter(uint8_t* buf, size_t size) : Writer(), buf_(buf), size_(size), bytes_written_(0) {} virtual bool Append(uint8_t* buf, size_t buf_size) override { if (bytes_written_ + buf_size > size_) { - ALOGW("Zip: Unexpected size " ZD " (declared) vs " ZD " (actual)", - size_, bytes_written_ + buf_size); + ALOGW("Zip: Unexpected size " ZD " (declared) vs " ZD " (actual)", size_, + bytes_written_ + buf_size); return false; } @@ -773,7 +759,6 @@ class MemoryWriter : public Writer { // The file will be truncated to the end of the written data. class FileWriter : public Writer { public: - // Creates a FileWriter for |fd| and prepare to write |entry| to it, // guaranteeing that the file descriptor is valid and that there's enough // space on the volume to write out the entry completely and that the file @@ -832,8 +817,8 @@ class FileWriter : public Writer { virtual bool Append(uint8_t* buf, size_t buf_size) override { if (total_bytes_written_ + buf_size > declared_length_) { - ALOGW("Zip: Unexpected size " ZD " (declared) vs " ZD " (actual)", - declared_length_, total_bytes_written_ + buf_size); + ALOGW("Zip: Unexpected size " ZD " (declared) vs " ZD " (actual)", declared_length_, + total_bytes_written_ + buf_size); return false; } @@ -846,13 +831,10 @@ class FileWriter : public Writer { return result; } + private: - FileWriter(const int fd, const size_t declared_length) : - Writer(), - fd_(fd), - declared_length_(declared_length), - total_bytes_written_(0) { - } + FileWriter(const int fd, const size_t declared_length) + : Writer(), fd_(fd), declared_length_(declared_length), total_bytes_written_(0) {} const int fd_; const size_t declared_length_; @@ -895,8 +877,7 @@ static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* e zerr = zlib_inflateInit2(&zstream, -MAX_WBITS); if (zerr != Z_OK) { if (zerr == Z_VERSION_ERROR) { - ALOGE("Installed zlib is not compatible with linked version (%s)", - ZLIB_VERSION); + ALOGE("Installed zlib is not compatible with linked version (%s)", ZLIB_VERSION); } else { ALOGW("Call to inflateInit2 failed (zerr=%d)", zerr); } @@ -905,7 +886,7 @@ static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* e } auto zstream_deleter = [](z_stream* stream) { - inflateEnd(stream); /* free up any allocated structures */ + inflateEnd(stream); /* free up any allocated structures */ }; std::unique_ptr zstream_guard(&zstream, zstream_deleter); @@ -932,15 +913,13 @@ static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* e /* uncompress the data */ zerr = inflate(&zstream, Z_NO_FLUSH); if (zerr != Z_OK && zerr != Z_STREAM_END) { - ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)", - zerr, zstream.next_in, zstream.avail_in, - zstream.next_out, zstream.avail_out); + ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)", zerr, zstream.next_in, + zstream.avail_in, zstream.next_out, zstream.avail_out); return kZlibError; } /* write when we're full or when we're done */ - if (zstream.avail_out == 0 || - (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) { + 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. @@ -954,7 +933,7 @@ static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* e } } while (zerr == Z_OK); - assert(zerr == Z_STREAM_END); /* other errors should've been caught */ + assert(zerr == Z_STREAM_END); /* other errors should've been caught */ // NOTE: zstream.adler is always set to 0, because we're using the -MAX_WBITS // "feature" of zlib to tell it there won't be a zlib file header. zlib @@ -965,8 +944,8 @@ static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* e *crc_out = crc; if (zstream.total_out != uncompressed_length || compressed_length != 0) { - ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")", - zstream.total_out, uncompressed_length); + ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")", zstream.total_out, + uncompressed_length); return kInconsistentInformation; } @@ -974,7 +953,7 @@ static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* e } static int32_t CopyEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry, Writer* writer, - uint64_t *crc_out) { + uint64_t* crc_out) { static const uint32_t kBufSize = 32768; std::vector buf(kBufSize); @@ -1004,8 +983,7 @@ static int32_t CopyEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entr return 0; } -int32_t ExtractToWriter(ZipArchiveHandle handle, - ZipEntry* entry, Writer* writer) { +int32_t ExtractToWriter(ZipArchiveHandle handle, ZipEntry* entry, Writer* writer) { ZipArchive* archive = reinterpret_cast(handle); const uint16_t method = entry->method; off64_t data_offset = entry->offset; @@ -1040,14 +1018,12 @@ int32_t ExtractToWriter(ZipArchiveHandle handle, return return_value; } -int32_t ExtractToMemory(ZipArchiveHandle handle, ZipEntry* entry, - uint8_t* begin, uint32_t size) { +int32_t ExtractToMemory(ZipArchiveHandle handle, ZipEntry* entry, uint8_t* begin, uint32_t size) { std::unique_ptr writer(new MemoryWriter(begin, size)); return ExtractToWriter(handle, entry, writer.get()); } -int32_t ExtractEntryToFile(ZipArchiveHandle handle, - ZipEntry* entry, int fd) { +int32_t ExtractEntryToFile(ZipArchiveHandle handle, ZipEntry* entry, int fd) { std::unique_ptr writer(FileWriter::Create(fd, entry)); if (writer.get() == nullptr) { return kIoError; @@ -1074,8 +1050,7 @@ int GetFileDescriptor(const ZipArchiveHandle handle) { return reinterpret_cast(handle)->mapped_zip.GetFileDescriptor(); } -ZipString::ZipString(const char* entry_name) - : name(reinterpret_cast(entry_name)) { +ZipString::ZipString(const char* entry_name) : name(reinterpret_cast(entry_name)) { size_t len = strlen(entry_name); CHECK_LE(len, static_cast(UINT16_MAX)); name_length = static_cast(len); @@ -1084,10 +1059,8 @@ ZipString::ZipString(const char* entry_name) #if !defined(_WIN32) class ProcessWriter : public Writer { public: - ProcessWriter(ProcessZipEntryFunction func, void* cookie) : Writer(), - proc_function_(func), - cookie_(cookie) { - } + ProcessWriter(ProcessZipEntryFunction func, void* cookie) + : Writer(), proc_function_(func), cookie_(cookie) {} virtual bool Append(uint8_t* buf, size_t buf_size) override { return proc_function_(buf, buf_size, cookie_); @@ -1104,7 +1077,7 @@ int32_t ProcessZipEntryContents(ZipArchiveHandle handle, ZipEntry* entry, return ExtractToWriter(handle, entry, &writer); } -#endif //!defined(_WIN32) +#endif //! defined(_WIN32) int MappedZipFile::GetFileDescriptor() const { if (!has_fd_) { @@ -1147,8 +1120,7 @@ bool MappedZipFile::SeekToOffset(off64_t offset) { return true; } else { if (offset < 0 || offset > static_cast(data_length_)) { - ALOGE("Zip: invalid offset: %" PRId64 ", data length: %" PRId64 "\n" , offset, - data_length_); + ALOGE("Zip: invalid offset: %" PRId64 ", data length: %" PRId64 "\n", offset, data_length_); return false; } @@ -1159,7 +1131,7 @@ bool MappedZipFile::SeekToOffset(off64_t offset) { bool MappedZipFile::ReadData(uint8_t* buffer, size_t read_amount) { if (has_fd_) { - if(!android::base::ReadFully(fd_, buffer, read_amount)) { + if (!android::base::ReadFully(fd_, buffer, read_amount)) { ALOGE("Zip: read from %d failed\n", fd_); return false; } @@ -1185,7 +1157,6 @@ bool MappedZipFile::ReadAtOffset(uint8_t* buf, size_t len, off64_t off) { return false; } return ReadData(buf, len); - } void CentralDirectory::Initialize(void* map_base_ptr, off64_t cd_start_offset, size_t cd_size) { @@ -1196,13 +1167,13 @@ void CentralDirectory::Initialize(void* map_base_ptr, off64_t cd_start_offset, s bool ZipArchive::InitializeCentralDirectory(const char* debug_file_name, off64_t cd_start_offset, size_t cd_size) { if (mapped_zip.HasFd()) { - if (!directory_map->create(debug_file_name, mapped_zip.GetFileDescriptor(), - cd_start_offset, cd_size, true /* read only */)) { + if (!directory_map->create(debug_file_name, mapped_zip.GetFileDescriptor(), cd_start_offset, + cd_size, true /* read only */)) { return false; } CHECK_EQ(directory_map->getDataLength(), cd_size); - central_directory.Initialize(directory_map->getDataPtr(), 0/*offset*/, cd_size); + central_directory.Initialize(directory_map->getDataPtr(), 0 /*offset*/, cd_size); } else { if (mapped_zip.GetBasePtr() == nullptr) { ALOGE("Zip: Failed to map central directory, bad mapped_zip base pointer\n"); @@ -1210,9 +1181,10 @@ bool ZipArchive::InitializeCentralDirectory(const char* debug_file_name, off64_t } if (static_cast(cd_start_offset) + static_cast(cd_size) > mapped_zip.GetFileLength()) { - ALOGE("Zip: Failed to map central directory, offset exceeds mapped memory region (" - "start_offset %" PRId64 ", cd_size %zu, mapped_region_size %" PRId64 ")", - static_cast(cd_start_offset), cd_size, mapped_zip.GetFileLength()); + ALOGE( + "Zip: Failed to map central directory, offset exceeds mapped memory region (" + "start_offset %" PRId64 ", cd_size %zu, mapped_region_size %" PRId64 ")", + static_cast(cd_start_offset), cd_size, mapped_zip.GetFileLength()); return false; } diff --git a/libziparchive/zip_archive_common.h b/libziparchive/zip_archive_common.h index bc1ebb429..8b99bde91 100644 --- a/libziparchive/zip_archive_common.h +++ b/libziparchive/zip_archive_common.h @@ -57,6 +57,7 @@ struct EocdRecord { uint32_t cd_start_offset; // Length of the central directory comment. uint16_t comment_length; + private: EocdRecord() = default; DISALLOW_COPY_AND_ASSIGN(EocdRecord); @@ -111,6 +112,7 @@ struct CentralDirectoryRecord { // The offset to the local file header for this entry, from the // beginning of this archive. uint32_t local_file_header_offset; + private: CentralDirectoryRecord() = default; DISALLOW_COPY_AND_ASSIGN(CentralDirectoryRecord); @@ -149,6 +151,7 @@ struct LocalFileHeader { // The length of the extra field info (in bytes). This data // will appear immediately after the entry file name. uint16_t extra_field_length; + private: LocalFileHeader() = default; DISALLOW_COPY_AND_ASSIGN(LocalFileHeader); @@ -164,6 +167,7 @@ struct DataDescriptor { uint32_t compressed_size; // Uncompressed size of the entry. uint32_t uncompressed_size; + private: DataDescriptor() = default; DISALLOW_COPY_AND_ASSIGN(DataDescriptor); diff --git a/libziparchive/zip_archive_private.h b/libziparchive/zip_archive_private.h index de93ecdd1..840f1afec 100644 --- a/libziparchive/zip_archive_private.h +++ b/libziparchive/zip_archive_private.h @@ -92,21 +92,17 @@ enum ErrorCodes : int32_t { class MappedZipFile { public: - explicit MappedZipFile(const int fd) : - has_fd_(true), - fd_(fd), - base_ptr_(nullptr), - data_length_(0), - read_pos_(0) {} + explicit MappedZipFile(const int fd) + : has_fd_(true), fd_(fd), base_ptr_(nullptr), data_length_(0), read_pos_(0) {} - explicit MappedZipFile(void* address, size_t length) : - has_fd_(false), - fd_(-1), - base_ptr_(address), - data_length_(static_cast(length)), - read_pos_(0) {} + explicit MappedZipFile(void* address, size_t length) + : has_fd_(false), + fd_(-1), + base_ptr_(address), + data_length_(static_cast(length)), + read_pos_(0) {} - bool HasFd() const {return has_fd_;} + bool HasFd() const { return has_fd_; } int GetFileDescriptor() const; @@ -137,13 +133,11 @@ class MappedZipFile { class CentralDirectory { public: - CentralDirectory(void) : - base_ptr_(nullptr), - length_(0) {} + CentralDirectory(void) : base_ptr_(nullptr), length_(0) {} - const uint8_t* GetBasePtr() const {return base_ptr_;} + const uint8_t* GetBasePtr() const { return base_ptr_; } - size_t GetMapLength() const {return length_;} + size_t GetMapLength() const { return length_; } void Initialize(void* map_base_ptr, off64_t cd_start_offset, size_t cd_size); @@ -172,25 +166,25 @@ struct ZipArchive { uint32_t hash_table_size; ZipString* hash_table; - ZipArchive(const int fd, bool assume_ownership) : - mapped_zip(fd), - close_file(assume_ownership), - directory_offset(0), - central_directory(), - directory_map(new android::FileMap()), - num_entries(0), - hash_table_size(0), - hash_table(nullptr) {} + ZipArchive(const int fd, bool assume_ownership) + : mapped_zip(fd), + close_file(assume_ownership), + directory_offset(0), + central_directory(), + directory_map(new android::FileMap()), + num_entries(0), + hash_table_size(0), + hash_table(nullptr) {} - ZipArchive(void* address, size_t length) : - mapped_zip(address, length), - close_file(false), - directory_offset(0), - central_directory(), - directory_map(new android::FileMap()), - num_entries(0), - hash_table_size(0), - hash_table(nullptr) {} + ZipArchive(void* address, size_t length) + : mapped_zip(address, length), + close_file(false), + directory_offset(0), + central_directory(), + directory_map(new android::FileMap()), + num_entries(0), + hash_table_size(0), + hash_table(nullptr) {} ~ZipArchive() { if (close_file && mapped_zip.GetFileDescriptor() >= 0) { @@ -202,7 +196,6 @@ struct ZipArchive { bool InitializeCentralDirectory(const char* debug_file_name, off64_t cd_start_offset, size_t cd_size); - }; #endif // LIBZIPARCHIVE_ZIPARCHIVE_PRIVATE_H_ diff --git a/libziparchive/zip_archive_stream_entry.cc b/libziparchive/zip_archive_stream_entry.cc index 3f336a6f2..50352effb 100644 --- a/libziparchive/zip_archive_stream_entry.cc +++ b/libziparchive/zip_archive_stream_entry.cc @@ -162,8 +162,7 @@ bool ZipArchiveStreamEntryCompressed::Init(const ZipEntry& entry) { int zerr = zlib_inflateInit2(&z_stream_, -MAX_WBITS); if (zerr != Z_OK) { if (zerr == Z_VERSION_ERROR) { - ALOGE("Installed zlib is not compatible with linked version (%s)", - ZLIB_VERSION); + ALOGE("Installed zlib is not compatible with linked version (%s)", ZLIB_VERSION); } else { ALOGE("Call to inflateInit2 failed (zerr=%d)", zerr); } @@ -193,13 +192,14 @@ ZipArchiveStreamEntryCompressed::~ZipArchiveStreamEntryCompressed() { bool ZipArchiveStreamEntryCompressed::Verify() { return z_stream_init_ && uncompressed_length_ == 0 && compressed_length_ == 0 && - crc32_ == computed_crc32_; + crc32_ == computed_crc32_; } const std::vector* ZipArchiveStreamEntryCompressed::Read() { if (z_stream_.avail_out == 0) { z_stream_.next_out = out_.data(); - z_stream_.avail_out = out_.size();; + z_stream_.avail_out = out_.size(); + ; } while (true) { @@ -226,9 +226,8 @@ const std::vector* ZipArchiveStreamEntryCompressed::Read() { int zerr = inflate(&z_stream_, Z_NO_FLUSH); if (zerr != Z_OK && zerr != Z_STREAM_END) { - ALOGE("inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)", - zerr, z_stream_.next_in, z_stream_.avail_in, - z_stream_.next_out, z_stream_.avail_out); + ALOGE("inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)", zerr, z_stream_.next_in, + z_stream_.avail_in, z_stream_.next_out, z_stream_.avail_out); return nullptr; } @@ -276,8 +275,8 @@ bool ZipArchiveStreamEntryRawCompressed::Verify() { return length_ == 0; } -ZipArchiveStreamEntry* ZipArchiveStreamEntry::Create( - ZipArchiveHandle handle, const ZipEntry& entry) { +ZipArchiveStreamEntry* ZipArchiveStreamEntry::Create(ZipArchiveHandle handle, + const ZipEntry& entry) { ZipArchiveStreamEntry* stream = nullptr; if (entry.method != kCompressStored) { stream = new ZipArchiveStreamEntryCompressed(handle); @@ -292,8 +291,8 @@ ZipArchiveStreamEntry* ZipArchiveStreamEntry::Create( return stream; } -ZipArchiveStreamEntry* ZipArchiveStreamEntry::CreateRaw( - ZipArchiveHandle handle, const ZipEntry& entry) { +ZipArchiveStreamEntry* ZipArchiveStreamEntry::CreateRaw(ZipArchiveHandle handle, + const ZipEntry& entry) { ZipArchiveStreamEntry* stream = nullptr; if (entry.method == kCompressStored) { // Not compressed, don't need to do anything special. diff --git a/libziparchive/zip_archive_test.cc b/libziparchive/zip_archive_test.cc index 47f4bbd57..dbc14f02c 100644 --- a/libziparchive/zip_archive_test.cc +++ b/libziparchive/zip_archive_test.cc @@ -44,21 +44,13 @@ static const std::string kCrashApk = "crash.apk"; static const std::string kBadFilenameZip = "bad_filename.zip"; static const std::string kUpdateZip = "dummy-update.zip"; -static const std::vector kATxtContents { - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', - '\n' -}; +static const std::vector kATxtContents{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'a', + 'b', 'c', 'd', 'e', 'f', 'g', 'h', '\n'}; -static const std::vector kATxtContentsCompressed { - 'K', 'L', 'J', 'N', 'I', 'M', 'K', 207, 'H', - 132, 210, '\\', '\0' -}; +static const std::vector kATxtContentsCompressed{'K', 'L', 'J', 'N', 'I', 'M', 'K', + 207, 'H', 132, 210, '\\', '\0'}; -static const std::vector kBTxtContents { - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', - '\n' -}; +static const std::vector kBTxtContents{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '\n'}; static const std::string kATxtName("a.txt"); static const std::string kBTxtName("b.txt"); @@ -67,14 +59,12 @@ static const std::string kEmptyTxtName("empty.txt"); static const std::string kLargeCompressTxtName("compress.txt"); static const std::string kLargeUncompressTxtName("uncompress.txt"); -static int32_t OpenArchiveWrapper(const std::string& name, - ZipArchiveHandle* handle) { +static int32_t OpenArchiveWrapper(const std::string& name, ZipArchiveHandle* handle) { const std::string abs_path = test_data_dir + "/" + name; return OpenArchive(abs_path.c_str(), handle); } -static void AssertNameEquals(const std::string& name_str, - const ZipString& name) { +static void AssertNameEquals(const std::string& name_str, const ZipString& name) { ASSERT_EQ(name_str.size(), name.name_length); ASSERT_EQ(0, memcmp(name_str.c_str(), name.name, name.name_length)); } @@ -345,47 +335,36 @@ TEST(ziparchive, ExtractToMemory) { } static const uint32_t kEmptyEntriesZip[] = { - 0x04034b50, 0x0000000a, 0x63600000, 0x00004438, 0x00000000, 0x00000000, - 0x00090000, 0x6d65001c, 0x2e797470, 0x55747874, 0x03000954, 0x52e25c13, - 0x52e25c24, 0x000b7875, 0x42890401, 0x88040000, 0x50000013, 0x1e02014b, - 0x00000a03, 0x60000000, 0x00443863, 0x00000000, 0x00000000, 0x09000000, - 0x00001800, 0x00000000, 0xa0000000, 0x00000081, 0x706d6500, 0x742e7974, - 0x54557478, 0x13030005, 0x7552e25c, 0x01000b78, 0x00428904, 0x13880400, - 0x4b500000, 0x00000605, 0x00010000, 0x004f0001, 0x00430000, 0x00000000 }; + 0x04034b50, 0x0000000a, 0x63600000, 0x00004438, 0x00000000, 0x00000000, 0x00090000, + 0x6d65001c, 0x2e797470, 0x55747874, 0x03000954, 0x52e25c13, 0x52e25c24, 0x000b7875, + 0x42890401, 0x88040000, 0x50000013, 0x1e02014b, 0x00000a03, 0x60000000, 0x00443863, + 0x00000000, 0x00000000, 0x09000000, 0x00001800, 0x00000000, 0xa0000000, 0x00000081, + 0x706d6500, 0x742e7974, 0x54557478, 0x13030005, 0x7552e25c, 0x01000b78, 0x00428904, + 0x13880400, 0x4b500000, 0x00000605, 0x00010000, 0x004f0001, 0x00430000, 0x00000000}; // This is a zip file containing a single entry (ab.txt) that contains // 90072 repetitions of the string "ab\n" and has an uncompressed length // of 270216 bytes. static const uint16_t kAbZip[] = { - 0x4b50, 0x0403, 0x0014, 0x0000, 0x0008, 0x51d2, 0x4698, 0xc4b0, - 0x2cda, 0x011b, 0x0000, 0x1f88, 0x0004, 0x0006, 0x001c, 0x6261, - 0x742e, 0x7478, 0x5455, 0x0009, 0x7c03, 0x3a09, 0x7c55, 0x3a09, - 0x7555, 0x0b78, 0x0100, 0x8904, 0x0042, 0x0400, 0x1388, 0x0000, - 0xc2ed, 0x0d31, 0x0000, 0x030c, 0x7fa0, 0x3b2e, 0x22ff, 0xa2aa, - 0x841f, 0x45fc, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, - 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, - 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, - 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, - 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, - 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, - 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, - 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, - 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, - 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, - 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, - 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, - 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, - 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, - 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, - 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, - 0x5555, 0x5555, 0x5555, 0x5555, 0xdd55, 0x502c, 0x014b, 0x1e02, - 0x1403, 0x0000, 0x0800, 0xd200, 0x9851, 0xb046, 0xdac4, 0x1b2c, - 0x0001, 0x8800, 0x041f, 0x0600, 0x1800, 0x0000, 0x0000, 0x0100, - 0x0000, 0xa000, 0x0081, 0x0000, 0x6100, 0x2e62, 0x7874, 0x5574, - 0x0554, 0x0300, 0x097c, 0x553a, 0x7875, 0x000b, 0x0401, 0x4289, - 0x0000, 0x8804, 0x0013, 0x5000, 0x054b, 0x0006, 0x0000, 0x0100, - 0x0100, 0x4c00, 0x0000, 0x5b00, 0x0001, 0x0000, 0x0000 -}; + 0x4b50, 0x0403, 0x0014, 0x0000, 0x0008, 0x51d2, 0x4698, 0xc4b0, 0x2cda, 0x011b, 0x0000, 0x1f88, + 0x0004, 0x0006, 0x001c, 0x6261, 0x742e, 0x7478, 0x5455, 0x0009, 0x7c03, 0x3a09, 0x7c55, 0x3a09, + 0x7555, 0x0b78, 0x0100, 0x8904, 0x0042, 0x0400, 0x1388, 0x0000, 0xc2ed, 0x0d31, 0x0000, 0x030c, + 0x7fa0, 0x3b2e, 0x22ff, 0xa2aa, 0x841f, 0x45fc, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, + 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, + 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, + 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, + 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, + 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, + 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, + 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, + 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, + 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, + 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, 0x5555, + 0x5555, 0x5555, 0x5555, 0x5555, 0xdd55, 0x502c, 0x014b, 0x1e02, 0x1403, 0x0000, 0x0800, 0xd200, + 0x9851, 0xb046, 0xdac4, 0x1b2c, 0x0001, 0x8800, 0x041f, 0x0600, 0x1800, 0x0000, 0x0000, 0x0100, + 0x0000, 0xa000, 0x0081, 0x0000, 0x6100, 0x2e62, 0x7874, 0x5574, 0x0554, 0x0300, 0x097c, 0x553a, + 0x7875, 0x000b, 0x0401, 0x4289, 0x0000, 0x8804, 0x0013, 0x5000, 0x054b, 0x0006, 0x0000, 0x0100, + 0x0100, 0x4c00, 0x0000, 0x5b00, 0x0001, 0x0000, 0x0000}; static const std::string kAbTxtName("ab.txt"); static const size_t kAbUncompressedSize = 270216; @@ -406,7 +385,6 @@ TEST(ziparchive, EmptyEntries) { uint8_t buffer[1]; ASSERT_EQ(0, ExtractToMemory(handle, &entry, buffer, 1)); - TemporaryFile tmp_output_file; ASSERT_NE(-1, tmp_output_file.fd); ASSERT_EQ(0, ExtractEntryToFile(handle, &entry, tmp_output_file.fd)); @@ -420,7 +398,7 @@ TEST(ziparchive, EntryLargerThan32K) { TemporaryFile tmp_file; ASSERT_NE(-1, tmp_file.fd); ASSERT_TRUE(android::base::WriteFully(tmp_file.fd, reinterpret_cast(kAbZip), - sizeof(kAbZip) - 1)); + sizeof(kAbZip) - 1)); ZipArchiveHandle handle; ASSERT_EQ(0, OpenArchiveFd(tmp_file.fd, "EntryLargerThan32KTest", &handle)); @@ -448,8 +426,7 @@ TEST(ziparchive, EntryLargerThan32K) { // the same as the memory buffer we extracted directly to. std::vector file_contents(kAbUncompressedSize); ASSERT_EQ(0, lseek64(tmp_output_file.fd, 0, SEEK_SET)); - ASSERT_TRUE(android::base::ReadFully(tmp_output_file.fd, &file_contents[0], - file_contents.size())); + ASSERT_TRUE(android::base::ReadFully(tmp_output_file.fd, &file_contents[0], file_contents.size())); ASSERT_EQ(file_contents, buffer); for (int i = 0; i < 90072; ++i) { @@ -465,7 +442,7 @@ TEST(ziparchive, TrailerAfterEOCD) { ASSERT_NE(-1, tmp_file.fd); // Create a file with 8 bytes of random garbage. - static const uint8_t trailer[] = { 'A' ,'n', 'd', 'r', 'o', 'i', 'd', 'z' }; + static const uint8_t trailer[] = {'A', 'n', 'd', 'r', 'o', 'i', 'd', 'z'}; ASSERT_TRUE(android::base::WriteFully(tmp_file.fd, kEmptyEntriesZip, sizeof(kEmptyEntriesZip))); ASSERT_TRUE(android::base::WriteFully(tmp_file.fd, trailer, sizeof(trailer))); @@ -476,7 +453,7 @@ TEST(ziparchive, TrailerAfterEOCD) { TEST(ziparchive, ExtractToFile) { TemporaryFile tmp_file; ASSERT_NE(-1, tmp_file.fd); - const uint8_t data[8] = { '1', '2', '3', '4', '5', '6', '7', '8' }; + const uint8_t data[8] = {'1', '2', '3', '4', '5', '6', '7', '8'}; const size_t data_size = sizeof(data); ASSERT_TRUE(android::base::WriteFully(tmp_file.fd, data, data_size)); @@ -490,7 +467,6 @@ TEST(ziparchive, ExtractToFile) { ASSERT_EQ(0, FindEntry(handle, name, &entry)); ASSERT_EQ(0, ExtractEntryToFile(handle, &entry, tmp_file.fd)); - // Assert that the first 8 bytes of the file haven't been clobbered. uint8_t read_buffer[data_size]; ASSERT_EQ(0, lseek64(tmp_file.fd, 0, SEEK_SET)); @@ -499,10 +475,9 @@ TEST(ziparchive, ExtractToFile) { // Assert that the remainder of the file contains the incompressed data. std::vector uncompressed_data(entry.uncompressed_length); - ASSERT_TRUE(android::base::ReadFully(tmp_file.fd, uncompressed_data.data(), - entry.uncompressed_length)); - ASSERT_EQ(0, memcmp(&uncompressed_data[0], kATxtContents.data(), - kATxtContents.size())); + ASSERT_TRUE( + android::base::ReadFully(tmp_file.fd, uncompressed_data.data(), entry.uncompressed_length)); + ASSERT_EQ(0, memcmp(&uncompressed_data[0], kATxtContents.data(), kATxtContents.size())); // Assert that the total length of the file is sane ASSERT_EQ(static_cast(data_size + kATxtContents.size()), @@ -519,7 +494,7 @@ TEST(ziparchive, OpenFromMemory) { // Memory map the file first and open the archive from the memory region. android::FileMap file_map; - file_map.create(zip_path.c_str(), fd, 0/*offset*/, sb.st_size, true); + file_map.create(zip_path.c_str(), fd, 0 /*offset*/, sb.st_size, true); ZipArchiveHandle handle; ASSERT_EQ(0, OpenArchiveFromMemory(file_map.getDataPtr(), file_map.getDataLength(), zip_path.c_str(), &handle)); @@ -535,9 +510,8 @@ TEST(ziparchive, OpenFromMemory) { } #endif -static void ZipArchiveStreamTest( - ZipArchiveHandle& handle, const std::string& entry_name, bool raw, - bool verified, ZipEntry* entry, std::vector* read_data) { +static void ZipArchiveStreamTest(ZipArchiveHandle& handle, const std::string& entry_name, bool raw, + bool verified, ZipEntry* entry, std::vector* read_data) { ZipString name; SetZipString(&name, entry_name); ASSERT_EQ(0, FindEntry(handle, name, entry)); @@ -566,9 +540,9 @@ static void ZipArchiveStreamTest( ASSERT_EQ(total_size, read_data->size()); } -static void ZipArchiveStreamTestUsingContents( - const std::string& zip_file, const std::string& entry_name, - const std::vector& contents, bool raw) { +static void ZipArchiveStreamTestUsingContents(const std::string& zip_file, + const std::string& entry_name, + const std::vector& contents, bool raw) { ZipArchiveHandle handle; ASSERT_EQ(0, OpenArchiveWrapper(zip_file, &handle)); @@ -582,7 +556,8 @@ static void ZipArchiveStreamTestUsingContents( CloseArchive(handle); } -static void ZipArchiveStreamTestUsingMemory(const std::string& zip_file, const std::string& entry_name) { +static void ZipArchiveStreamTestUsingMemory(const std::string& zip_file, + const std::string& entry_name) { ZipArchiveHandle handle; ASSERT_EQ(0, OpenArchiveWrapper(zip_file, &handle)); @@ -745,10 +720,8 @@ TEST(ziparchive, ErrorCodeString) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); - static struct option options[] = { - { "test_data_dir", required_argument, nullptr, 't' }, - { nullptr, 0, nullptr, 0 } - }; + static struct option options[] = {{"test_data_dir", required_argument, nullptr, 't'}, + {nullptr, 0, nullptr, 0}}; while (true) { int option_index; diff --git a/libziparchive/zip_writer.cc b/libziparchive/zip_writer.cc index 6d28bdb5e..6ad3366e4 100644 --- a/libziparchive/zip_writer.cc +++ b/libziparchive/zip_writer.cc @@ -16,11 +16,11 @@ #include "ziparchive/zip_writer.h" -#include #include #include #include -#define DEF_MEM_LEVEL 8 // normally in zutil.h? +#include +#define DEF_MEM_LEVEL 8 // normally in zutil.h? #include #include @@ -33,13 +33,13 @@ #include "zip_archive_common.h" #if !defined(powerof2) -#define powerof2(x) ((((x)-1)&(x))==0) +#define powerof2(x) ((((x)-1) & (x)) == 0) #endif /* Zip compression methods we support */ enum { - kCompressStored = 0, // no compression - kCompressDeflated = 8, // standard deflate + kCompressStored = 0, // no compression + kCompressDeflated = 8, // standard deflate }; // Size of the output buffer used for compression. @@ -67,10 +67,7 @@ static const int32_t kInvalidAlign32Flag = -5; static const int32_t kInvalidAlignment = -6; static const char* sErrorCodes[] = { - "Invalid state", - "IO error", - "Invalid entry name", - "Zlib error", + "Invalid state", "IO error", "Invalid entry name", "Zlib error", }; const char* ZipWriter::ErrorCodeString(int32_t error_code) { @@ -85,9 +82,13 @@ static void DeleteZStream(z_stream* stream) { delete stream; } -ZipWriter::ZipWriter(FILE* f) : file_(f), seekable_(false), current_offset_(0), - state_(State::kWritingZip), z_stream_(nullptr, DeleteZStream), - buffer_(kBufSize) { +ZipWriter::ZipWriter(FILE* f) + : file_(f), + seekable_(false), + current_offset_(0), + state_(State::kWritingZip), + z_stream_(nullptr, DeleteZStream), + buffer_(kBufSize) { // Check if the file is seekable (regular file). If fstat fails, that's fine, subsequent calls // will fail as well. struct stat file_stats; @@ -96,13 +97,14 @@ ZipWriter::ZipWriter(FILE* f) : file_(f), seekable_(false), current_offset_(0), } } -ZipWriter::ZipWriter(ZipWriter&& writer) : file_(writer.file_), - seekable_(writer.seekable_), - current_offset_(writer.current_offset_), - state_(writer.state_), - files_(std::move(writer.files_)), - z_stream_(std::move(writer.z_stream_)), - buffer_(std::move(writer.buffer_)){ +ZipWriter::ZipWriter(ZipWriter&& writer) + : file_(writer.file_), + seekable_(writer.seekable_), + current_offset_(writer.current_offset_), + state_(writer.state_), + files_(std::move(writer.files_)), + z_stream_(std::move(writer.z_stream_)), + buffer_(std::move(writer.buffer_)) { writer.file_ = nullptr; writer.state_ = State::kError; } @@ -154,10 +156,10 @@ static void ExtractTimeAndDate(time_t when, uint16_t* out_time, uint16_t* out_da struct tm* ptm; #if !defined(_WIN32) - struct tm tm_result; - ptm = localtime_r(&when, &tm_result); + struct tm tm_result; + ptm = localtime_r(&when, &tm_result); #else - ptm = localtime(&when); + ptm = localtime(&when); #endif int year = ptm->tm_year; @@ -193,8 +195,8 @@ static void CopyFromFileEntry(const ZipWriter::FileEntry& src, bool use_data_des dst->extra_field_length = src.padding_length; } -int32_t ZipWriter::StartAlignedEntryWithTime(const char* path, size_t flags, - time_t time, uint32_t alignment) { +int32_t ZipWriter::StartAlignedEntryWithTime(const char* path, size_t flags, time_t time, + uint32_t alignment) { if (state_ != State::kWritingZip) { return kInvalidState; } @@ -252,9 +254,8 @@ int32_t ZipWriter::StartAlignedEntryWithTime(const char* path, size_t flags, return HandleError(kIoError); } - if (file_entry.padding_length != 0 && - fwrite(zero_padding.data(), 1, file_entry.padding_length, file_) - != file_entry.padding_length) { + if (file_entry.padding_length != 0 && fwrite(zero_padding.data(), 1, file_entry.padding_length, + file_) != file_entry.padding_length) { return HandleError(kIoError); } @@ -292,7 +293,7 @@ int32_t ZipWriter::PrepareDeflate() { CHECK(state_ == State::kWritingZip); // Initialize the z_stream for compression. - z_stream_ = std::unique_ptr(new z_stream(), DeleteZStream); + z_stream_ = std::unique_ptr(new z_stream(), DeleteZStream); #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wold-style-cast" @@ -331,8 +332,8 @@ int32_t ZipWriter::WriteBytes(const void* data, size_t len) { return result; } - current_file_entry_.crc32 = crc32(current_file_entry_.crc32, - reinterpret_cast(data), len); + current_file_entry_.crc32 = + crc32(current_file_entry_.crc32, reinterpret_cast(data), len); current_file_entry_.uncompressed_size += len; return kNoError; } diff --git a/libziparchive/zip_writer_test.cc b/libziparchive/zip_writer_test.cc index 9ad025255..c2842734e 100644 --- a/libziparchive/zip_writer_test.cc +++ b/libziparchive/zip_writer_test.cc @@ -14,8 +14,8 @@ * limitations under the License. */ -#include "ziparchive/zip_archive.h" #include "ziparchive/zip_writer.h" +#include "ziparchive/zip_archive.h" #include #include @@ -361,7 +361,7 @@ TEST_F(zipwriter, TruncateFileAfterBackup) { ASSERT_EQ(0, writer.StartEntry("large.txt", 0)); std::vector data; - data.resize(1024*1024, 0xef); + data.resize(1024 * 1024, 0xef); ASSERT_EQ(0, writer.WriteBytes(data.data(), data.size())); ASSERT_EQ(0, writer.FinishEntry()); @@ -382,8 +382,9 @@ static ::testing::AssertionResult AssertFileEntryContentsEq(const std::string& e ZipArchiveHandle handle, ZipEntry* zip_entry) { if (expected.size() != zip_entry->uncompressed_length) { - return ::testing::AssertionFailure() << "uncompressed entry size " - << zip_entry->uncompressed_length << " does not match expected size " << expected.size(); + return ::testing::AssertionFailure() + << "uncompressed entry size " << zip_entry->uncompressed_length + << " does not match expected size " << expected.size(); } std::string actual; @@ -396,7 +397,7 @@ static ::testing::AssertionResult AssertFileEntryContentsEq(const std::string& e if (expected != actual) { return ::testing::AssertionFailure() << "actual zip_entry data '" << actual - << "' does not match expected '" << expected << "'"; + << "' does not match expected '" << expected << "'"; } return ::testing::AssertionSuccess(); }