diff options
author | RedworkDE <10944644+RedworkDE@users.noreply.github.com> | 2023-02-15 12:25:33 +0100 |
---|---|---|
committer | Mark Adler <git@madler.net> | 2023-07-29 23:13:45 -0700 |
commit | f209ca7be7981dc8fca79428706057e4ebc929ee (patch) | |
tree | 82fa4b7d52755fe6685ddf4d591bbb331a3d3f4a | |
parent | a566e156b3fa07b566ddbf6801b517a9dba04fa3 (diff) | |
download | zlib-f209ca7be7981dc8fca79428706057e4ebc929ee.tar.gz zlib-f209ca7be7981dc8fca79428706057e4ebc929ee.tar.bz2 zlib-f209ca7be7981dc8fca79428706057e4ebc929ee.zip |
minizip: Fix being unable to open empty zip file
-rw-r--r-- | contrib/minizip/unzip.c | 48 |
1 files changed, 26 insertions, 22 deletions
diff --git a/contrib/minizip/unzip.c b/contrib/minizip/unzip.c index ad2eb3b..3adc692 100644 --- a/contrib/minizip/unzip.c +++ b/contrib/minizip/unzip.c | |||
@@ -379,6 +379,10 @@ extern int ZEXPORT unzStringFileNameCompare (const char* fileName1, | |||
379 | #define BUFREADCOMMENT (0x400) | 379 | #define BUFREADCOMMENT (0x400) |
380 | #endif | 380 | #endif |
381 | 381 | ||
382 | #ifndef CENTRALDIRINVALID | ||
383 | #define CENTRALDIRINVALID ((ZPOS64_T)(-1)) | ||
384 | #endif | ||
385 | |||
382 | /* | 386 | /* |
383 | Locate the Central directory of a zipfile (at the end, just before | 387 | Locate the Central directory of a zipfile (at the end, just before |
384 | the global comment) | 388 | the global comment) |
@@ -388,10 +392,10 @@ local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_f | |||
388 | ZPOS64_T uSizeFile; | 392 | ZPOS64_T uSizeFile; |
389 | ZPOS64_T uBackRead; | 393 | ZPOS64_T uBackRead; |
390 | ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */ | 394 | ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */ |
391 | ZPOS64_T uPosFound=0; | 395 | ZPOS64_T uPosFound=CENTRALDIRINVALID; |
392 | 396 | ||
393 | if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0) | 397 | if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0) |
394 | return 0; | 398 | return CENTRALDIRINVALID; |
395 | 399 | ||
396 | 400 | ||
397 | uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream); | 401 | uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream); |
@@ -401,7 +405,7 @@ local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_f | |||
401 | 405 | ||
402 | buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); | 406 | buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); |
403 | if (buf==NULL) | 407 | if (buf==NULL) |
404 | return 0; | 408 | return CENTRALDIRINVALID; |
405 | 409 | ||
406 | uBackRead = 4; | 410 | uBackRead = 4; |
407 | while (uBackRead<uMaxBack) | 411 | while (uBackRead<uMaxBack) |
@@ -431,7 +435,7 @@ local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_f | |||
431 | break; | 435 | break; |
432 | } | 436 | } |
433 | 437 | ||
434 | if (uPosFound!=0) | 438 | if (uPosFound!=CENTRALDIRINVALID) |
435 | break; | 439 | break; |
436 | } | 440 | } |
437 | TRYFREE(buf); | 441 | TRYFREE(buf); |
@@ -449,12 +453,12 @@ local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib | |||
449 | ZPOS64_T uSizeFile; | 453 | ZPOS64_T uSizeFile; |
450 | ZPOS64_T uBackRead; | 454 | ZPOS64_T uBackRead; |
451 | ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */ | 455 | ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */ |
452 | ZPOS64_T uPosFound=0; | 456 | ZPOS64_T uPosFound=CENTRALDIRINVALID; |
453 | uLong uL; | 457 | uLong uL; |
454 | ZPOS64_T relativeOffset; | 458 | ZPOS64_T relativeOffset; |
455 | 459 | ||
456 | if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0) | 460 | if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0) |
457 | return 0; | 461 | return CENTRALDIRINVALID; |
458 | 462 | ||
459 | 463 | ||
460 | uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream); | 464 | uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream); |
@@ -464,7 +468,7 @@ local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib | |||
464 | 468 | ||
465 | buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); | 469 | buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); |
466 | if (buf==NULL) | 470 | if (buf==NULL) |
467 | return 0; | 471 | return CENTRALDIRINVALID; |
468 | 472 | ||
469 | uBackRead = 4; | 473 | uBackRead = 4; |
470 | while (uBackRead<uMaxBack) | 474 | while (uBackRead<uMaxBack) |
@@ -494,47 +498,47 @@ local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib | |||
494 | break; | 498 | break; |
495 | } | 499 | } |
496 | 500 | ||
497 | if (uPosFound!=0) | 501 | if (uPosFound!=CENTRALDIRINVALID) |
498 | break; | 502 | break; |
499 | } | 503 | } |
500 | TRYFREE(buf); | 504 | TRYFREE(buf); |
501 | if (uPosFound == 0) | 505 | if (uPosFound == CENTRALDIRINVALID) |
502 | return 0; | 506 | return CENTRALDIRINVALID; |
503 | 507 | ||
504 | /* Zip64 end of central directory locator */ | 508 | /* Zip64 end of central directory locator */ |
505 | if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0) | 509 | if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0) |
506 | return 0; | 510 | return CENTRALDIRINVALID; |
507 | 511 | ||
508 | /* the signature, already checked */ | 512 | /* the signature, already checked */ |
509 | if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK) | 513 | if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK) |
510 | return 0; | 514 | return CENTRALDIRINVALID; |
511 | 515 | ||
512 | /* number of the disk with the start of the zip64 end of central directory */ | 516 | /* number of the disk with the start of the zip64 end of central directory */ |
513 | if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK) | 517 | if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK) |
514 | return 0; | 518 | return CENTRALDIRINVALID; |
515 | if (uL != 0) | 519 | if (uL != 0) |
516 | return 0; | 520 | return CENTRALDIRINVALID; |
517 | 521 | ||
518 | /* relative offset of the zip64 end of central directory record */ | 522 | /* relative offset of the zip64 end of central directory record */ |
519 | if (unz64local_getLong64(pzlib_filefunc_def,filestream,&relativeOffset)!=UNZ_OK) | 523 | if (unz64local_getLong64(pzlib_filefunc_def,filestream,&relativeOffset)!=UNZ_OK) |
520 | return 0; | 524 | return CENTRALDIRINVALID; |
521 | 525 | ||
522 | /* total number of disks */ | 526 | /* total number of disks */ |
523 | if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK) | 527 | if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK) |
524 | return 0; | 528 | return CENTRALDIRINVALID; |
525 | if (uL != 1) | 529 | if (uL != 1) |
526 | return 0; | 530 | return CENTRALDIRINVALID; |
527 | 531 | ||
528 | /* Goto end of central directory record */ | 532 | /* Goto end of central directory record */ |
529 | if (ZSEEK64(*pzlib_filefunc_def,filestream, relativeOffset,ZLIB_FILEFUNC_SEEK_SET)!=0) | 533 | if (ZSEEK64(*pzlib_filefunc_def,filestream, relativeOffset,ZLIB_FILEFUNC_SEEK_SET)!=0) |
530 | return 0; | 534 | return CENTRALDIRINVALID; |
531 | 535 | ||
532 | /* the signature */ | 536 | /* the signature */ |
533 | if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK) | 537 | if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK) |
534 | return 0; | 538 | return CENTRALDIRINVALID; |
535 | 539 | ||
536 | if (uL != 0x06064b50) | 540 | if (uL != 0x06064b50) |
537 | return 0; | 541 | return CENTRALDIRINVALID; |
538 | 542 | ||
539 | return relativeOffset; | 543 | return relativeOffset; |
540 | } | 544 | } |
@@ -587,7 +591,7 @@ local unzFile unzOpenInternal(const void *path, | |||
587 | return NULL; | 591 | return NULL; |
588 | 592 | ||
589 | central_pos = unz64local_SearchCentralDir64(&us.z_filefunc,us.filestream); | 593 | central_pos = unz64local_SearchCentralDir64(&us.z_filefunc,us.filestream); |
590 | if (central_pos) | 594 | if (central_pos!=CENTRALDIRINVALID) |
591 | { | 595 | { |
592 | uLong uS; | 596 | uLong uS; |
593 | ZPOS64_T uL64; | 597 | ZPOS64_T uL64; |
@@ -649,7 +653,7 @@ local unzFile unzOpenInternal(const void *path, | |||
649 | else | 653 | else |
650 | { | 654 | { |
651 | central_pos = unz64local_SearchCentralDir(&us.z_filefunc,us.filestream); | 655 | central_pos = unz64local_SearchCentralDir(&us.z_filefunc,us.filestream); |
652 | if (central_pos==0) | 656 | if (central_pos==CENTRALDIRINVALID) |
653 | err=UNZ_ERRNO; | 657 | err=UNZ_ERRNO; |
654 | 658 | ||
655 | us.isZip64 = 0; | 659 | us.isZip64 = 0; |