Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't open zip with 65535 entries #596

Closed
Nekto89 opened this issue Oct 20, 2021 · 3 comments
Closed

Can't open zip with 65535 entries #596

Nekto89 opened this issue Oct 20, 2021 · 3 comments

Comments

@Nekto89
Copy link

Nekto89 commented Oct 20, 2021

fast "dirty" code for reproducing the issue. I'm not sure that I've used API properly. I'm creating zip files with 65534, 65536 and 65535 empty files inside. 65535 doesn't work for some reason. 7-zip can open result zip without any errors

#include <mz.h>
#include <mz_os.h>
#include <mz_strm.h>
#include <mz_strm_os.h>
#include <mz_zip.h>
#include <mz_zip_rw.h>

#include <cstdio>
#include <ctime>
#include <string>

int write_archive(const char* archive_filename, const std::size_t number_of_files) {
  void* zip_writer = nullptr;
  const char* path = archive_filename;
  mz_zip_writer_create(&zip_writer);
  if (mz_zip_writer_open_file(zip_writer, path, 0, 0) != MZ_OK) {
    mz_zip_writer_delete(&zip_writer);
    std::printf("Couldn't open zip for writing");
    return -1;
  }

  for (std::size_t i = 0; i < number_of_files; ++i) {
    const auto filename = std::to_string(i);

    mz_zip_file file_info = { 0 };
    file_info.filename = filename.c_str();
    file_info.modified_date = std::time(nullptr);
    file_info.version_madeby = MZ_VERSION_MADEBY;
    file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
    file_info.flag = MZ_ZIP_FLAG_UTF8;

    if (mz_zip_writer_entry_open(zip_writer, &file_info) != MZ_OK) {
      std::printf("Couldn't open entry");
      return -2;
    }
    if (mz_zip_writer_entry_close(zip_writer) != MZ_OK) {
      std::printf("Couldn't close entry");
      return -3;
    }
  }

  if (mz_zip_writer_close(zip_writer) != MZ_OK) {
    std::printf("Couldn't close writer");
    return -4;
  }
  mz_zip_writer_delete(&zip_writer);
  return 0;
}

bool can_archive_be_opened(const char* path) {
  void* zip_reader = nullptr;
  void* file_stream = nullptr;

  mz_zip_reader_create(&zip_reader);
  mz_stream_os_create(&file_stream);

  auto err = mz_stream_os_open(file_stream, path, MZ_OPEN_MODE_READ);
  if (err == MZ_OK) {
    err = mz_zip_reader_open(zip_reader, file_stream);
    if (err == MZ_OK) {
      mz_zip_reader_close(zip_reader);
    }
  }
  mz_stream_os_delete(&file_stream);
  mz_zip_reader_delete(&zip_reader);
  return err == MZ_OK;
}

int main() {
  for (std::size_t i : {65534, 65536, 65535})
  {
    const auto filename = "test" + std::to_string(i) + ".zip";
    const auto write_result = write_archive(filename.c_str(), i);
    if (write_result != 0)
      return write_result;
    const auto read_result = can_archive_be_opened(filename.c_str());
    if (!read_result) {
      std::printf("Can't open %s", filename.c_str());
      return -5;
    }
  }
}
@nmoinvaz
Copy link
Member

nmoinvaz commented Nov 13, 2021

if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT16_MAX) {

Should probably be:

if (zip->cd_offset >= UINT32_MAX || zip->number_entry >= UINT16_MAX) {

@Nekto89
Copy link
Author

Nekto89 commented Nov 14, 2021

Isn't 65535 still valid number of files for ZIP? I thought that the problem is in reading.

@nmoinvaz
Copy link
Member

If the value is 64k then it will read the value in the zip64 header instead.

ponapalt added a commit to ponapalt/minizip-ng that referenced this issue Dec 21, 2021
ponapalt added a commit to ponapalt/minizip-ng that referenced this issue Jun 24, 2022
ponapalt added a commit to ponapalt/minizip-ng that referenced this issue Jun 24, 2022
ponapalt added a commit to ponapalt/minizip-ng that referenced this issue Jun 24, 2022
ponapalt added a commit to ponapalt/minizip-ng that referenced this issue Oct 15, 2022
ponapalt added a commit to ponapalt/minizip-ng that referenced this issue Oct 16, 2022
ponapalt added a commit to ponapalt/minizip-ng that referenced this issue Oct 16, 2022
ponapalt added a commit to ponapalt/minizip-ng that referenced this issue Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants