| 1 | /* zip.c -- Backwards compatible unzip interface |
| 2 | part of the minizip-ng project |
| 3 | |
| 4 | Copyright (C) Nathan Moinvaziri |
| 5 | https://github.com/zlib-ng/minizip-ng |
| 6 | Copyright (C) 1998-2010 Gilles Vollant |
| 7 | https://www.winimage.com/zLibDll/minizip.html |
| 8 | |
| 9 | This program is distributed under the terms of the same license as zlib. |
| 10 | See the accompanying LICENSE file for the full text of the license. |
| 11 | */ |
| 12 | |
| 13 | #include "mz.h" |
| 14 | #include "mz_os.h" |
| 15 | #include "mz_strm.h" |
| 16 | #include "mz_strm_os.h" |
| 17 | #include "mz_zip.h" |
| 18 | |
| 19 | #include <stdio.h> /* SEEK */ |
| 20 | |
| 21 | #include "unzip.h" |
| 22 | |
| 23 | /***************************************************************************/ |
| 24 | |
| 25 | typedef struct mz_unzip_compat_s { |
| 26 | void *stream; |
| 27 | void *handle; |
| 28 | uint64_t entry_index; |
| 29 | int64_t entry_pos; |
| 30 | int64_t total_out; |
| 31 | } mz_unzip_compat; |
| 32 | |
| 33 | /***************************************************************************/ |
| 34 | |
| 35 | unzFile unzOpen(const char *path) { |
| 36 | return unzOpen64(path); |
| 37 | } |
| 38 | |
| 39 | unzFile unzOpen64(const void *path) { |
| 40 | return unzOpen2(path, NULL); |
| 41 | } |
| 42 | |
| 43 | unzFile unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc_def) { |
| 44 | unzFile unz = NULL; |
| 45 | void *stream = NULL; |
| 46 | |
| 47 | if (pzlib_filefunc_def) { |
| 48 | if (pzlib_filefunc_def->zopen_file) { |
| 49 | stream = mz_stream_ioapi_create(); |
| 50 | if (!stream) |
| 51 | return NULL; |
| 52 | mz_stream_ioapi_set_filefunc(stream, pzlib_filefunc_def); |
| 53 | } else if (pzlib_filefunc_def->opaque) { |
| 54 | stream = mz_stream_create((mz_stream_vtbl *)pzlib_filefunc_def->opaque); |
| 55 | if (!stream) |
| 56 | return NULL; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if (!stream) { |
| 61 | stream = mz_stream_os_create(); |
| 62 | if (!stream) |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | if (mz_stream_open(stream, path, MZ_OPEN_MODE_READ) != MZ_OK) { |
| 67 | mz_stream_delete(&stream); |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | unz = unzOpen_MZ(stream); |
| 72 | if (!unz) { |
| 73 | mz_stream_close(stream); |
| 74 | mz_stream_delete(&stream); |
| 75 | return NULL; |
| 76 | } |
| 77 | return unz; |
| 78 | } |
| 79 | |
| 80 | unzFile unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def) { |
| 81 | unzFile unz = NULL; |
| 82 | void *stream = NULL; |
| 83 | |
| 84 | if (pzlib_filefunc_def) { |
| 85 | if (pzlib_filefunc_def->zopen64_file) { |
| 86 | stream = mz_stream_ioapi_create(); |
| 87 | if (!stream) |
| 88 | return NULL; |
| 89 | mz_stream_ioapi_set_filefunc64(stream, pzlib_filefunc_def); |
| 90 | } else if (pzlib_filefunc_def->opaque) { |
| 91 | stream = mz_stream_create((mz_stream_vtbl *)pzlib_filefunc_def->opaque); |
| 92 | if (!stream) |
| 93 | return NULL; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if (!stream) { |
| 98 | stream = mz_stream_os_create(); |
| 99 | if (!stream) |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | if (mz_stream_open(stream, path, MZ_OPEN_MODE_READ) != MZ_OK) { |
| 104 | mz_stream_delete(&stream); |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | unz = unzOpen_MZ(stream); |
| 109 | if (!unz) { |
| 110 | mz_stream_close(stream); |
| 111 | mz_stream_delete(&stream); |
| 112 | return NULL; |
| 113 | } |
| 114 | return unz; |
| 115 | } |
| 116 | |
| 117 | void *unzGetHandle_MZ(unzFile file) { |
| 118 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 119 | if (!compat) |
| 120 | return NULL; |
| 121 | return compat->handle; |
| 122 | } |
| 123 | |
| 124 | void *unzGetStream_MZ(unzFile file) { |
| 125 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 126 | if (!compat) |
| 127 | return NULL; |
| 128 | return compat->stream; |
| 129 | } |
| 130 | |
| 131 | unzFile unzOpen_MZ(void *stream) { |
| 132 | mz_unzip_compat *compat = NULL; |
| 133 | int32_t err = MZ_OK; |
| 134 | void *handle = NULL; |
| 135 | |
| 136 | handle = mz_zip_create(); |
| 137 | if (!handle) |
| 138 | return NULL; |
| 139 | |
| 140 | mz_zip_set_recover(handle, 1); |
| 141 | |
| 142 | err = mz_zip_open(handle, stream, MZ_OPEN_MODE_READ); |
| 143 | if (err != MZ_OK) { |
| 144 | mz_zip_delete(&handle); |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | compat = (mz_unzip_compat *)calloc(1, sizeof(mz_unzip_compat)); |
| 149 | if (compat) { |
| 150 | compat->handle = handle; |
| 151 | compat->stream = stream; |
| 152 | |
| 153 | mz_zip_goto_first_entry(compat->handle); |
| 154 | } else { |
| 155 | mz_zip_delete(&handle); |
| 156 | } |
| 157 | |
| 158 | return (unzFile)compat; |
| 159 | } |
| 160 | |
| 161 | int unzClose(unzFile file) { |
| 162 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 163 | int32_t err = MZ_OK; |
| 164 | |
| 165 | if (!compat) |
| 166 | return UNZ_PARAMERROR; |
| 167 | |
| 168 | if (compat->handle) |
| 169 | err = unzClose_MZ(file); |
| 170 | |
| 171 | if (compat->stream) { |
| 172 | mz_stream_close(compat->stream); |
| 173 | mz_stream_delete(&compat->stream); |
| 174 | } |
| 175 | |
| 176 | free(compat); |
| 177 | |
| 178 | return err; |
| 179 | } |
| 180 | |
| 181 | /* Only closes the zip handle, does not close the stream */ |
| 182 | int unzClose_MZ(unzFile file) { |
| 183 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 184 | int32_t err = MZ_OK; |
| 185 | |
| 186 | if (!compat) |
| 187 | return UNZ_PARAMERROR; |
| 188 | |
| 189 | err = mz_zip_close(compat->handle); |
| 190 | mz_zip_delete(&compat->handle); |
| 191 | |
| 192 | return err; |
| 193 | } |
| 194 | |
| 195 | int unzGetGlobalInfo(unzFile file, unz_global_info *pglobal_info32) { |
| 196 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 197 | unz_global_info64 global_info64; |
| 198 | int32_t err = MZ_OK; |
| 199 | |
| 200 | memset(pglobal_info32, 0, sizeof(unz_global_info)); |
| 201 | if (!compat) |
| 202 | return UNZ_PARAMERROR; |
| 203 | |
| 204 | err = unzGetGlobalInfo64(file, &global_info64); |
| 205 | if (err == MZ_OK) { |
| 206 | pglobal_info32->number_entry = (uint32_t)global_info64.number_entry; |
| 207 | pglobal_info32->size_comment = global_info64.size_comment; |
| 208 | pglobal_info32->number_disk_with_CD = global_info64.number_disk_with_CD; |
| 209 | } |
| 210 | return err; |
| 211 | } |
| 212 | |
| 213 | int unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info) { |
| 214 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 215 | const char *comment_ptr = NULL; |
| 216 | int32_t err = MZ_OK; |
| 217 | |
| 218 | memset(pglobal_info, 0, sizeof(unz_global_info64)); |
| 219 | if (!compat) |
| 220 | return UNZ_PARAMERROR; |
| 221 | err = mz_zip_get_comment(compat->handle, &comment_ptr); |
| 222 | if (err == MZ_OK) |
| 223 | pglobal_info->size_comment = (uint16_t)strlen(comment_ptr); |
| 224 | if ((err == MZ_OK) || (err == MZ_EXIST_ERROR)) |
| 225 | err = mz_zip_get_number_entry(compat->handle, &pglobal_info->number_entry); |
| 226 | if (err == MZ_OK) |
| 227 | err = mz_zip_get_disk_number_with_cd(compat->handle, &pglobal_info->number_disk_with_CD); |
| 228 | return err; |
| 229 | } |
| 230 | |
| 231 | int unzGetGlobalComment(unzFile file, char *comment, unsigned long comment_size) { |
| 232 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 233 | const char *comment_ptr = NULL; |
| 234 | int32_t err = MZ_OK; |
| 235 | |
| 236 | if (!comment || !comment_size) |
| 237 | return UNZ_PARAMERROR; |
| 238 | err = mz_zip_get_comment(compat->handle, &comment_ptr); |
| 239 | if (err == MZ_OK) { |
| 240 | strncpy(comment, comment_ptr, comment_size - 1); |
| 241 | comment[comment_size - 1] = 0; |
| 242 | } |
| 243 | return err; |
| 244 | } |
| 245 | |
| 246 | int unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password) { |
| 247 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 248 | mz_zip_file *file_info = NULL; |
| 249 | int32_t err = MZ_OK; |
| 250 | void *stream = NULL; |
| 251 | |
| 252 | if (!compat) |
| 253 | return UNZ_PARAMERROR; |
| 254 | if (method) |
| 255 | *method = 0; |
| 256 | if (level) |
| 257 | *level = 0; |
| 258 | |
| 259 | if (mz_zip_entry_is_open(compat->handle) == MZ_OK) { |
| 260 | /* zlib minizip does not error out here if close returns errors */ |
| 261 | unzCloseCurrentFile(file); |
| 262 | } |
| 263 | |
| 264 | compat->total_out = 0; |
| 265 | err = mz_zip_entry_read_open(compat->handle, (uint8_t)raw, password); |
| 266 | if (err == MZ_OK) |
| 267 | err = mz_zip_entry_get_info(compat->handle, &file_info); |
| 268 | if (err == MZ_OK) { |
| 269 | if (method) { |
| 270 | *method = file_info->compression_method; |
| 271 | } |
| 272 | |
| 273 | if (level) { |
| 274 | *level = 6; |
| 275 | switch (file_info->flag & 0x06) { |
| 276 | case MZ_ZIP_FLAG_DEFLATE_SUPER_FAST: |
| 277 | *level = 1; |
| 278 | break; |
| 279 | case MZ_ZIP_FLAG_DEFLATE_FAST: |
| 280 | *level = 2; |
| 281 | break; |
| 282 | case MZ_ZIP_FLAG_DEFLATE_MAX: |
| 283 | *level = 9; |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | if (err == MZ_OK) |
| 289 | err = mz_zip_get_stream(compat->handle, &stream); |
| 290 | if (err == MZ_OK) |
| 291 | compat->entry_pos = mz_stream_tell(stream); |
| 292 | return err; |
| 293 | } |
| 294 | |
| 295 | int unzOpenCurrentFile(unzFile file) { |
| 296 | return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL); |
| 297 | } |
| 298 | |
| 299 | int unzOpenCurrentFilePassword(unzFile file, const char *password) { |
| 300 | return unzOpenCurrentFile3(file, NULL, NULL, 0, password); |
| 301 | } |
| 302 | |
| 303 | int unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw) { |
| 304 | return unzOpenCurrentFile3(file, method, level, raw, NULL); |
| 305 | } |
| 306 | |
| 307 | int unzReadCurrentFile(unzFile file, void *buf, uint32_t len) { |
| 308 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 309 | int32_t err = MZ_OK; |
| 310 | if (!compat || len >= INT32_MAX) |
| 311 | return UNZ_PARAMERROR; |
| 312 | err = mz_zip_entry_read(compat->handle, buf, (int32_t)len); |
| 313 | if (err > 0) |
| 314 | compat->total_out += (uint32_t)err; |
| 315 | return err; |
| 316 | } |
| 317 | |
| 318 | int unzCloseCurrentFile(unzFile file) { |
| 319 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 320 | int32_t err = MZ_OK; |
| 321 | if (!compat) |
| 322 | return UNZ_PARAMERROR; |
| 323 | err = mz_zip_entry_close(compat->handle); |
| 324 | return err; |
| 325 | } |
| 326 | |
| 327 | static void unzConvertTimeToUnzTime(time_t time, tm_unz *tmu_date) { |
| 328 | struct tm tm_date; |
| 329 | memset(&tm_date, 0, sizeof(struct tm)); |
| 330 | mz_zip_time_t_to_tm(time, &tm_date); |
| 331 | memcpy(tmu_date, &tm_date, sizeof(tm_unz)); |
| 332 | tmu_date->tm_year += 1900; |
| 333 | } |
| 334 | |
| 335 | int unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename, unsigned long filename_size, |
| 336 | void *extrafield, unsigned long extrafield_size, char *comment, unsigned long comment_size) { |
| 337 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 338 | mz_zip_file *file_info = NULL; |
| 339 | uint16_t bytes_to_copy = 0; |
| 340 | int32_t err = MZ_OK; |
| 341 | |
| 342 | if (!compat) |
| 343 | return UNZ_PARAMERROR; |
| 344 | |
| 345 | err = mz_zip_entry_get_info(compat->handle, &file_info); |
| 346 | if (err != MZ_OK) |
| 347 | return err; |
| 348 | |
| 349 | if (pfile_info) { |
| 350 | pfile_info->version = file_info->version_madeby; |
| 351 | pfile_info->version_needed = file_info->version_needed; |
| 352 | pfile_info->flag = file_info->flag; |
| 353 | pfile_info->compression_method = file_info->compression_method; |
| 354 | pfile_info->mz_dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date); |
| 355 | unzConvertTimeToUnzTime(file_info->modified_date, &pfile_info->tmu_date); |
| 356 | pfile_info->crc = file_info->crc; |
| 357 | |
| 358 | pfile_info->size_filename = file_info->filename_size; |
| 359 | pfile_info->size_file_extra = file_info->extrafield_size; |
| 360 | pfile_info->size_file_comment = file_info->comment_size; |
| 361 | |
| 362 | pfile_info->disk_num_start = (uint16_t)file_info->disk_number; |
| 363 | pfile_info->internal_fa = file_info->internal_fa; |
| 364 | pfile_info->external_fa = file_info->external_fa; |
| 365 | |
| 366 | pfile_info->compressed_size = (uint32_t)file_info->compressed_size; |
| 367 | pfile_info->uncompressed_size = (uint32_t)file_info->uncompressed_size; |
| 368 | } |
| 369 | if (filename_size > 0 && filename && file_info->filename) { |
| 370 | bytes_to_copy = (uint16_t)filename_size; |
| 371 | if (bytes_to_copy > file_info->filename_size) |
| 372 | bytes_to_copy = file_info->filename_size; |
| 373 | memcpy(filename, file_info->filename, bytes_to_copy); |
| 374 | if (bytes_to_copy < filename_size) |
| 375 | filename[bytes_to_copy] = 0; |
| 376 | } |
| 377 | if (extrafield_size > 0 && extrafield) { |
| 378 | bytes_to_copy = (uint16_t)extrafield_size; |
| 379 | if (bytes_to_copy > file_info->extrafield_size) |
| 380 | bytes_to_copy = file_info->extrafield_size; |
| 381 | memcpy(extrafield, file_info->extrafield, bytes_to_copy); |
| 382 | } |
| 383 | if (comment_size > 0 && comment && file_info->comment) { |
| 384 | bytes_to_copy = (uint16_t)comment_size; |
| 385 | if (bytes_to_copy > file_info->comment_size) |
| 386 | bytes_to_copy = file_info->comment_size; |
| 387 | memcpy(comment, file_info->comment, bytes_to_copy); |
| 388 | if (bytes_to_copy < comment_size) |
| 389 | comment[bytes_to_copy] = 0; |
| 390 | } |
| 391 | return err; |
| 392 | } |
| 393 | |
| 394 | int unzGetCurrentFileInfo64(unzFile file, unz_file_info64 *pfile_info, char *filename, unsigned long filename_size, |
| 395 | void *extrafield, unsigned long extrafield_size, char *comment, |
| 396 | unsigned long comment_size) { |
| 397 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 398 | mz_zip_file *file_info = NULL; |
| 399 | uint16_t bytes_to_copy = 0; |
| 400 | int32_t err = MZ_OK; |
| 401 | |
| 402 | if (!compat) |
| 403 | return UNZ_PARAMERROR; |
| 404 | |
| 405 | err = mz_zip_entry_get_info(compat->handle, &file_info); |
| 406 | if (err != MZ_OK) |
| 407 | return err; |
| 408 | |
| 409 | if (pfile_info) { |
| 410 | pfile_info->version = file_info->version_madeby; |
| 411 | pfile_info->version_needed = file_info->version_needed; |
| 412 | pfile_info->flag = file_info->flag; |
| 413 | pfile_info->compression_method = file_info->compression_method; |
| 414 | pfile_info->mz_dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date); |
| 415 | unzConvertTimeToUnzTime(file_info->modified_date, &pfile_info->tmu_date); |
| 416 | pfile_info->crc = file_info->crc; |
| 417 | |
| 418 | pfile_info->size_filename = file_info->filename_size; |
| 419 | pfile_info->size_file_extra = file_info->extrafield_size; |
| 420 | pfile_info->size_file_comment = file_info->comment_size; |
| 421 | |
| 422 | pfile_info->disk_num_start = file_info->disk_number; |
| 423 | pfile_info->internal_fa = file_info->internal_fa; |
| 424 | pfile_info->external_fa = file_info->external_fa; |
| 425 | |
| 426 | pfile_info->compressed_size = (uint64_t)file_info->compressed_size; |
| 427 | pfile_info->uncompressed_size = (uint64_t)file_info->uncompressed_size; |
| 428 | } |
| 429 | if (filename_size > 0 && filename && file_info->filename) { |
| 430 | bytes_to_copy = (uint16_t)filename_size; |
| 431 | if (bytes_to_copy > file_info->filename_size) |
| 432 | bytes_to_copy = file_info->filename_size; |
| 433 | memcpy(filename, file_info->filename, bytes_to_copy); |
| 434 | if (bytes_to_copy < filename_size) |
| 435 | filename[bytes_to_copy] = 0; |
| 436 | } |
| 437 | if (extrafield_size > 0 && extrafield) { |
| 438 | bytes_to_copy = (uint16_t)extrafield_size; |
| 439 | if (bytes_to_copy > file_info->extrafield_size) |
| 440 | bytes_to_copy = file_info->extrafield_size; |
| 441 | memcpy(extrafield, file_info->extrafield, bytes_to_copy); |
| 442 | } |
| 443 | if (comment_size > 0 && comment && file_info->comment) { |
| 444 | bytes_to_copy = (uint16_t)comment_size; |
| 445 | if (bytes_to_copy > file_info->comment_size) |
| 446 | bytes_to_copy = file_info->comment_size; |
| 447 | memcpy(comment, file_info->comment, bytes_to_copy); |
| 448 | if (bytes_to_copy < comment_size) |
| 449 | comment[bytes_to_copy] = 0; |
| 450 | } |
| 451 | return err; |
| 452 | } |
| 453 | |
| 454 | int unzGoToFirstFile(unzFile file) { |
| 455 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 456 | if (!compat) |
| 457 | return UNZ_PARAMERROR; |
| 458 | compat->entry_index = 0; |
| 459 | return mz_zip_goto_first_entry(compat->handle); |
| 460 | } |
| 461 | |
| 462 | int unzGoToNextFile(unzFile file) { |
| 463 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 464 | int32_t err = MZ_OK; |
| 465 | if (!compat) |
| 466 | return UNZ_PARAMERROR; |
| 467 | err = mz_zip_goto_next_entry(compat->handle); |
| 468 | if (err != MZ_END_OF_LIST) |
| 469 | compat->entry_index += 1; |
| 470 | return err; |
| 471 | } |
| 472 | |
| 473 | #if !defined(MZ_COMPAT_VERSION) || MZ_COMPAT_VERSION < 110 |
| 474 | # ifdef WIN32 |
| 475 | # define UNZ_DEFAULT_IGNORE_CASE 1 |
| 476 | # else |
| 477 | # define UNZ_DEFAULT_IGNORE_CASE 0 |
| 478 | # endif |
| 479 | |
| 480 | int unzLocateFile(unzFile file, const char *filename, unzFileNameCase filename_case) { |
| 481 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 482 | mz_zip_file *file_info = NULL; |
| 483 | uint64_t preserve_index = 0; |
| 484 | int32_t err = MZ_OK; |
| 485 | int32_t result = 0; |
| 486 | uint8_t ignore_case = UNZ_DEFAULT_IGNORE_CASE; |
| 487 | |
| 488 | if (!compat) |
| 489 | return UNZ_PARAMERROR; |
| 490 | |
| 491 | if (filename_case == 1) { |
| 492 | ignore_case = 0; |
| 493 | } else if (filename_case > 1) { |
| 494 | ignore_case = 1; |
| 495 | } |
| 496 | |
| 497 | preserve_index = compat->entry_index; |
| 498 | |
| 499 | err = mz_zip_goto_first_entry(compat->handle); |
| 500 | while (err == MZ_OK) { |
| 501 | err = mz_zip_entry_get_info(compat->handle, &file_info); |
| 502 | if (err != MZ_OK) |
| 503 | break; |
| 504 | |
| 505 | result = mz_path_compare_wc(filename, file_info->filename, !ignore_case); |
| 506 | |
| 507 | if (result == 0) |
| 508 | return MZ_OK; |
| 509 | |
| 510 | err = mz_zip_goto_next_entry(compat->handle); |
| 511 | } |
| 512 | |
| 513 | compat->entry_index = preserve_index; |
| 514 | return err; |
| 515 | } |
| 516 | #else |
| 517 | int unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func) { |
| 518 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 519 | mz_zip_file *file_info = NULL; |
| 520 | uint64_t preserve_index = 0; |
| 521 | int32_t err = MZ_OK; |
| 522 | int32_t result = 0; |
| 523 | |
| 524 | if (!compat) |
| 525 | return UNZ_PARAMERROR; |
| 526 | |
| 527 | preserve_index = compat->entry_index; |
| 528 | |
| 529 | err = mz_zip_goto_first_entry(compat->handle); |
| 530 | while (err == MZ_OK) { |
| 531 | err = mz_zip_entry_get_info(compat->handle, &file_info); |
| 532 | if (err != MZ_OK) |
| 533 | break; |
| 534 | |
| 535 | if ((intptr_t)filename_compare_func > 2) { |
| 536 | result = filename_compare_func(file, filename, file_info->filename); |
| 537 | } else { |
| 538 | int32_t case_sensitive = (int32_t)(intptr_t)filename_compare_func; |
| 539 | result = mz_path_compare_wc(filename, file_info->filename, !case_sensitive); |
| 540 | } |
| 541 | |
| 542 | if (result == 0) |
| 543 | return MZ_OK; |
| 544 | |
| 545 | err = mz_zip_goto_next_entry(compat->handle); |
| 546 | } |
| 547 | |
| 548 | compat->entry_index = preserve_index; |
| 549 | return err; |
| 550 | } |
| 551 | #endif |
| 552 | |
| 553 | /***************************************************************************/ |
| 554 | |
| 555 | int unzGetFilePos(unzFile file, unz_file_pos *file_pos) { |
| 556 | unz64_file_pos file_pos64; |
| 557 | int32_t err = 0; |
| 558 | |
| 559 | err = unzGetFilePos64(file, &file_pos64); |
| 560 | if (err < 0) |
| 561 | return err; |
| 562 | |
| 563 | file_pos->pos_in_zip_directory = (uint32_t)file_pos64.pos_in_zip_directory; |
| 564 | file_pos->num_of_file = (uint32_t)file_pos64.num_of_file; |
| 565 | return err; |
| 566 | } |
| 567 | |
| 568 | int unzGoToFilePos(unzFile file, unz_file_pos *file_pos) { |
| 569 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 570 | unz64_file_pos file_pos64; |
| 571 | |
| 572 | if (!compat || !file_pos) |
| 573 | return UNZ_PARAMERROR; |
| 574 | |
| 575 | file_pos64.pos_in_zip_directory = file_pos->pos_in_zip_directory; |
| 576 | file_pos64.num_of_file = file_pos->num_of_file; |
| 577 | |
| 578 | return unzGoToFilePos64(file, &file_pos64); |
| 579 | } |
| 580 | |
| 581 | int unzGetFilePos64(unzFile file, unz64_file_pos *file_pos) { |
| 582 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 583 | int64_t offset = 0; |
| 584 | |
| 585 | if (!compat || !file_pos) |
| 586 | return UNZ_PARAMERROR; |
| 587 | |
| 588 | offset = unzGetOffset64(file); |
| 589 | if (offset < 0) |
| 590 | return (int)offset; |
| 591 | |
| 592 | file_pos->pos_in_zip_directory = offset; |
| 593 | file_pos->num_of_file = compat->entry_index; |
| 594 | return UNZ_OK; |
| 595 | } |
| 596 | |
| 597 | int unzGoToFilePos64(unzFile file, const unz64_file_pos *file_pos) { |
| 598 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 599 | int32_t err = MZ_OK; |
| 600 | |
| 601 | if (!compat || !file_pos) |
| 602 | return UNZ_PARAMERROR; |
| 603 | |
| 604 | err = mz_zip_goto_entry(compat->handle, file_pos->pos_in_zip_directory); |
| 605 | if (err == MZ_OK) |
| 606 | compat->entry_index = file_pos->num_of_file; |
| 607 | return err; |
| 608 | } |
| 609 | |
| 610 | unsigned long unzGetOffset(unzFile file) { |
| 611 | return (uint32_t)unzGetOffset64(file); |
| 612 | } |
| 613 | |
| 614 | int64_t unzGetOffset64(unzFile file) { |
| 615 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 616 | if (!compat) |
| 617 | return UNZ_PARAMERROR; |
| 618 | return mz_zip_get_entry(compat->handle); |
| 619 | } |
| 620 | |
| 621 | int unzSetOffset(unzFile file, unsigned long pos) { |
| 622 | return unzSetOffset64(file, pos); |
| 623 | } |
| 624 | |
| 625 | int unzSetOffset64(unzFile file, int64_t pos) { |
| 626 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 627 | if (!compat) |
| 628 | return UNZ_PARAMERROR; |
| 629 | return (int)mz_zip_goto_entry(compat->handle, pos); |
| 630 | } |
| 631 | |
| 632 | int unzGetLocalExtrafield(unzFile file, void *buf, unsigned int len) { |
| 633 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 634 | mz_zip_file *file_info = NULL; |
| 635 | int32_t err = MZ_OK; |
| 636 | int32_t bytes_to_copy = 0; |
| 637 | |
| 638 | if (!compat || !buf || len >= INT32_MAX) |
| 639 | return UNZ_PARAMERROR; |
| 640 | |
| 641 | err = mz_zip_entry_get_local_info(compat->handle, &file_info); |
| 642 | if (err != MZ_OK) |
| 643 | return err; |
| 644 | |
| 645 | bytes_to_copy = (int32_t)len; |
| 646 | if (bytes_to_copy > file_info->extrafield_size) |
| 647 | bytes_to_copy = file_info->extrafield_size; |
| 648 | |
| 649 | memcpy(buf, file_info->extrafield, bytes_to_copy); |
| 650 | return MZ_OK; |
| 651 | } |
| 652 | |
| 653 | int32_t unzTell(unzFile file) { |
| 654 | return unztell(file); |
| 655 | } |
| 656 | |
| 657 | int32_t unztell(unzFile file) { |
| 658 | return (int32_t)unztell64(file); |
| 659 | } |
| 660 | |
| 661 | uint64_t unzTell64(unzFile file) { |
| 662 | return unztell64(file); |
| 663 | } |
| 664 | |
| 665 | uint64_t unztell64(unzFile file) { |
| 666 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 667 | if (!compat) |
| 668 | return UNZ_PARAMERROR; |
| 669 | return compat->total_out; |
| 670 | } |
| 671 | |
| 672 | int unzSeek(unzFile file, int32_t offset, int origin) { |
| 673 | return unzSeek64(file, offset, origin); |
| 674 | } |
| 675 | |
| 676 | int unzSeek64(unzFile file, int64_t offset, int origin) { |
| 677 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 678 | mz_zip_file *file_info = NULL; |
| 679 | int64_t position = 0; |
| 680 | int32_t err = MZ_OK; |
| 681 | void *stream = NULL; |
| 682 | |
| 683 | if (!compat) |
| 684 | return UNZ_PARAMERROR; |
| 685 | err = mz_zip_entry_get_info(compat->handle, &file_info); |
| 686 | if (err != MZ_OK) |
| 687 | return err; |
| 688 | if (file_info->compression_method != MZ_COMPRESS_METHOD_STORE) |
| 689 | return UNZ_ERRNO; |
| 690 | |
| 691 | if (origin == SEEK_SET) |
| 692 | position = offset; |
| 693 | else if (origin == SEEK_CUR) |
| 694 | position = compat->total_out + offset; |
| 695 | else if (origin == SEEK_END) |
| 696 | position = (int64_t)file_info->compressed_size + offset; |
| 697 | else |
| 698 | return UNZ_PARAMERROR; |
| 699 | |
| 700 | if (position > (int64_t)file_info->compressed_size) |
| 701 | return UNZ_PARAMERROR; |
| 702 | |
| 703 | err = mz_zip_get_stream(compat->handle, &stream); |
| 704 | if (err == MZ_OK) |
| 705 | err = mz_stream_seek(stream, compat->entry_pos + position, MZ_SEEK_SET); |
| 706 | if (err == MZ_OK) |
| 707 | compat->total_out = position; |
| 708 | return err; |
| 709 | } |
| 710 | |
| 711 | int unzEndOfFile(unzFile file) { |
| 712 | return unzeof(file); |
| 713 | } |
| 714 | |
| 715 | int unzeof(unzFile file) { |
| 716 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 717 | mz_zip_file *file_info = NULL; |
| 718 | int32_t err = MZ_OK; |
| 719 | |
| 720 | if (!compat) |
| 721 | return UNZ_PARAMERROR; |
| 722 | err = mz_zip_entry_get_info(compat->handle, &file_info); |
| 723 | if (err != MZ_OK) |
| 724 | return err; |
| 725 | if (compat->total_out == (int64_t)file_info->uncompressed_size) |
| 726 | return 1; |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | void *unzGetStream(unzFile file) { |
| 731 | mz_unzip_compat *compat = (mz_unzip_compat *)file; |
| 732 | if (!compat) |
| 733 | return NULL; |
| 734 | return (void *)compat->stream; |
| 735 | } |
| 736 | |
| 737 | /***************************************************************************/ |