From 7789dcbfd5fd00c418f27cdbc67873592608560c Mon Sep 17 00:00:00 2001 From: Pavel Rojtberg Date: Sat, 12 Mar 2022 00:33:30 +0100 Subject: [PATCH] forward changes we use in ogre (#242) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * allow access to the file compressed size * option for case-sensitive operation * refactor naming Co-authored-by: Kuba Podgórski --- src/zip.c | 24 +++++++++++++++++++++--- src/zip.h | 36 +++++++++++++++++++++++++++++++++++- test/test_entry.c | 17 +++++++++++++++++ 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/src/zip.c b/src/zip.c index 2073455..170126d 100644 --- a/src/zip.c +++ b/src/zip.c @@ -885,7 +885,8 @@ int zip_is64(struct zip_t *zip) { return (int)zip->archive.m_pState->m_zip64; } -int zip_entry_open(struct zip_t *zip, const char *entryname) { +static int _zip_entry_open(struct zip_t *zip, const char *entryname, + int case_sensitive) { size_t entrylen = 0; mz_zip_archive *pzip = NULL; mz_uint num_alignment_padding_bytes, level; @@ -938,8 +939,9 @@ int zip_entry_open(struct zip_t *zip, const char *entryname) { pzip = &(zip->archive); if (pzip->m_zip_mode == MZ_ZIP_MODE_READING) { - zip->entry.index = - mz_zip_reader_locate_file(pzip, zip->entry.name, NULL, 0); + zip->entry.index = mz_zip_reader_locate_file( + pzip, zip->entry.name, NULL, + case_sensitive ? MZ_ZIP_FLAG_CASE_SENSITIVE : 0); if (zip->entry.index < 0) { err = ZIP_ENOENT; goto cleanup; @@ -1083,6 +1085,14 @@ cleanup: return err; } +int zip_entry_open(struct zip_t *zip, const char *entryname) { + return _zip_entry_open(zip, entryname, 0); +} + +int zip_entry_opencasesensitive(struct zip_t *zip, const char *entryname) { + return _zip_entry_open(zip, entryname, 1); +} + int zip_entry_openbyindex(struct zip_t *zip, int index) { mz_zip_archive *pZip = NULL; mz_zip_archive_file_stat stats; @@ -1290,9 +1300,17 @@ int zip_entry_isdir(struct zip_t *zip) { } unsigned long long zip_entry_size(struct zip_t *zip) { + return zip_entry_uncomp_size(zip); +} + +unsigned long long zip_entry_uncomp_size(struct zip_t *zip) { return zip ? zip->entry.uncomp_size : 0; } +unsigned long long zip_entry_comp_size(struct zip_t *zip) { + return zip ? zip->entry.comp_size : 0; +} + unsigned int zip_entry_crc32(struct zip_t *zip) { return zip ? zip->entry.uncomp_crc32 : 0; } diff --git a/src/zip.h b/src/zip.h index 0329615..d40de74 100644 --- a/src/zip.h +++ b/src/zip.h @@ -159,6 +159,21 @@ extern ZIP_EXPORT int zip_is64(struct zip_t *zip); */ extern ZIP_EXPORT int zip_entry_open(struct zip_t *zip, const char *entryname); +/** + * Opens an entry by name in the zip archive. + * + * For zip archive opened in 'w' or 'a' mode the function will append + * a new entry. In readonly mode the function tries to locate the entry + * in global dictionary (case sensitive). + * + * @param zip zip archive handler. + * @param entryname an entry name in local dictionary (case sensitive). + * + * @return the return code - 0 on success, negative number (< 0) on error. + */ +extern ZIP_EXPORT int zip_entry_opencasesensitive(struct zip_t *zip, + const char *entryname); + /** * Opens a new entry by index in the zip archive. * @@ -216,7 +231,8 @@ extern ZIP_EXPORT int zip_entry_index(struct zip_t *zip); extern ZIP_EXPORT int zip_entry_isdir(struct zip_t *zip); /** - * Returns an uncompressed size of the current zip entry. + * Returns the uncompressed size of the current zip entry. + * Alias for zip_entry_uncomp_size (for backward compatibility). * * @param zip zip archive handler. * @@ -224,6 +240,24 @@ extern ZIP_EXPORT int zip_entry_isdir(struct zip_t *zip); */ extern ZIP_EXPORT unsigned long long zip_entry_size(struct zip_t *zip); +/** + * Returns the uncompressed size of the current zip entry. + * + * @param zip zip archive handler. + * + * @return the uncompressed size in bytes. + */ +extern ZIP_EXPORT unsigned long long zip_entry_uncomp_size(struct zip_t *zip); + +/** + * Returns the compressed size of the current zip entry. + * + * @param zip zip archive handler. + * + * @return the compressed size in bytes. + */ +extern ZIP_EXPORT unsigned long long zip_entry_comp_size(struct zip_t *zip); + /** * Returns CRC-32 checksum of the current zip entry. * diff --git a/test/test_entry.c b/test/test_entry.c index eb1425d..e829630 100644 --- a/test/test_entry.c +++ b/test/test_entry.c @@ -109,6 +109,22 @@ MU_TEST(test_entry_name) { zip_close(zip); } +MU_TEST(test_entry_opencasesensitive) { + struct zip_t *zip = zip_open(ZIPNAME, 0, 'r'); + mu_check(zip != NULL); + + mu_check(zip_entry_name(zip) == NULL); + + mu_assert_int_eq(0, zip_entry_open(zip, "test/TEST-1.TXT")); + mu_check(NULL != zip_entry_name(zip)); + mu_assert_int_eq(0, zip_entry_close(zip)); + + mu_assert_int_eq(ZIP_ENOENT, + zip_entry_opencasesensitive(zip, "test/TEST-1.TXT")); + + zip_close(zip); +} + MU_TEST(test_entry_index) { struct zip_t *zip = zip_open(ZIPNAME, 0, 'r'); mu_check(zip != NULL); @@ -261,6 +277,7 @@ MU_TEST_SUITE(test_entry_suite) { MU_SUITE_CONFIGURE(&test_setup, &test_teardown); MU_RUN_TEST(test_entry_name); + MU_RUN_TEST(test_entry_opencasesensitive); MU_RUN_TEST(test_entry_index); MU_RUN_TEST(test_entry_openbyindex); MU_RUN_TEST(test_entry_read);