Fix unzLocateFile 3rd parameter

Replace the previous type with one that is compatible with an int in
order to be identical to the interface used in madler's zlib.

Fixes #745.
This commit is contained in:
Tulio Magno Quites Machado Filho 2023-12-08 11:56:40 -03:00 committed by Nathan Moinvaziri
parent 2c2d6e5940
commit fbd8443122
3 changed files with 23 additions and 10 deletions

View File

@ -1099,16 +1099,29 @@ int unzGoToNextFile(unzFile file) {
return err; return err;
} }
int unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func) { #ifdef WIN32
# define UNZ_DEFAULT_IGNORE_CASE 1
#else
# define UNZ_DEFAULT_IGNORE_CASE 0
#endif
int unzLocateFile(unzFile file, const char *filename, unzFileNameCase filename_case) {
mz_compat *compat = (mz_compat *)file; mz_compat *compat = (mz_compat *)file;
mz_zip_file *file_info = NULL; mz_zip_file *file_info = NULL;
uint64_t preserve_index = 0; uint64_t preserve_index = 0;
int32_t err = MZ_OK; int32_t err = MZ_OK;
int32_t result = 0; int32_t result = 0;
uint8_t ignore_case = UNZ_DEFAULT_IGNORE_CASE;
if (!compat) if (!compat)
return UNZ_PARAMERROR; return UNZ_PARAMERROR;
if (filename_case == 1) {
ignore_case = 0;
} else if (filename_case > 1) {
ignore_case = 1;
}
preserve_index = compat->entry_index; preserve_index = compat->entry_index;
err = mz_zip_goto_first_entry(compat->handle); err = mz_zip_goto_first_entry(compat->handle);
@ -1117,12 +1130,7 @@ int unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filena
if (err != MZ_OK) if (err != MZ_OK)
break; break;
if ((intptr_t)filename_compare_func > 2) { result = mz_path_compare_wc(filename, file_info->filename, !ignore_case);
result = filename_compare_func(file, filename, file_info->filename);
} else {
int32_t case_sensitive = (int32_t)(intptr_t)filename_compare_func;
result = mz_path_compare_wc(filename, file_info->filename, !case_sensitive);
}
if (result == 0) if (result == 0)
return MZ_OK; return MZ_OK;

View File

@ -308,7 +308,12 @@ typedef struct unz_file_info_s {
/***************************************************************************/ /***************************************************************************/
typedef int (*unzFileNameComparer)(unzFile file, const char *filename1, const char *filename2); /* Possible values:
0 - Uses OS default, e.g. Windows ignores case.
1 - Is case sensitive.
>= 2 - Ignore case.
*/
typedef int unzFileNameCase;
typedef int (*unzIteratorFunction)(unzFile file); typedef int (*unzIteratorFunction)(unzFile file);
typedef int (*unzIteratorFunction2)(unzFile file, unz_file_info64 *pfile_info, char *filename, typedef int (*unzIteratorFunction2)(unzFile file, unz_file_info64 *pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment,
@ -349,7 +354,7 @@ ZEXPORT int unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile_in
ZEXPORT int unzGoToFirstFile(unzFile file); ZEXPORT int unzGoToFirstFile(unzFile file);
ZEXPORT int unzGoToNextFile(unzFile file); ZEXPORT int unzGoToNextFile(unzFile file);
ZEXPORT int unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func); ZEXPORT int unzLocateFile(unzFile file, const char *filename, unzFileNameCase filename_case);
ZEXPORT int unzGetLocalExtrafield(unzFile file, void *buf, unsigned int len); ZEXPORT int unzGetLocalExtrafield(unzFile file, void *buf, unsigned int len);

View File

@ -90,7 +90,7 @@ static void test_unzip_compat(unzFile unzip) {
EXPECT_EQ(global_info64.number_disk_with_CD, 0) EXPECT_EQ(global_info64.number_disk_with_CD, 0)
<< "invalid disk with cd 64-bit"; << "invalid disk with cd 64-bit";
EXPECT_EQ(err = unzLocateFile(unzip, "test.txt", (unzFileNameComparer)(void *)1), UNZ_OK) EXPECT_EQ(err = unzLocateFile(unzip, "test.txt", 1), UNZ_OK)
<< "cannot locate test file (err: " << err << ")"; << "cannot locate test file (err: " << err << ")";
EXPECT_EQ(err = unzGoToFirstFile(unzip), UNZ_OK); EXPECT_EQ(err = unzGoToFirstFile(unzip), UNZ_OK);