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

Workaround to access zip files with more than 65535 files. #77

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

amalgamation/
94 changes: 94 additions & 0 deletions miniz_zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,100 @@ static mz_bool mz_zip_reader_read_central_dir(mz_zip_archive *pZip, mz_uint flag
cdir_size = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_SIZE_OFS);
cdir_ofs = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_OFS_OFS);

/*
ZIP files may contain more files, than "Total number of central directory records" in the EOCD can hold.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When it may happen? What software generates such archives?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure but I think it is possible for python to do it.

Thus iterate through central directory for the truthful number of files. Sanity checks will be done later nevertheless.
*/
if( !pZip->m_pState->m_zip64 )
{
mz_uint8 *buf_ptr = pBuf;
mz_uint32 buf_remain = 0;
mz_uint32 total_files = 0;
mz_uint32 ofs = 0;
mz_bool done = MZ_FALSE;

MZ_ASSERT( sizeof(buf_u32) > MZ_ZIP_CENTRAL_DIR_HEADER_SIZE );

while( buf_ptr && ofs < cdir_size && done != MZ_TRUE )
{
mz_uint32 cdh_signature = 0;
mz_uint32 cdh_size = 0;
mz_bool need_data = MZ_FALSE;

/* Try to get the current header size */
if( buf_remain > sizeof(mz_uint32) )
{
cdh_signature = MZ_READ_LE32(buf_ptr);

switch( cdh_signature )
{
case MZ_ZIP_CENTRAL_DIR_HEADER_SIG:
if( buf_remain >= MZ_ZIP_CENTRAL_DIR_HEADER_SIZE )
{
if( MZ_READ_LE16(buf_ptr + MZ_ZIP_CDH_DISK_START_OFS) == 0 )
{
cdh_size = MZ_ZIP_CENTRAL_DIR_HEADER_SIZE +
MZ_READ_LE16(buf_ptr + MZ_ZIP_CDH_FILENAME_LEN_OFS) +
MZ_READ_LE16(buf_ptr + MZ_ZIP_CDH_EXTRA_LEN_OFS) +
MZ_READ_LE16(buf_ptr + MZ_ZIP_CDH_COMMENT_LEN_OFS);
total_files++;
}
else buf_ptr = NULL; /* fail */
}
break;

case MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG:
if( cdir_size - ofs >= MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE )
{
done = MZ_TRUE;
}
else buf_ptr = NULL; /* fail */
break;

default:
buf_ptr = NULL; /* fail */
break;
}
}

if( buf_ptr && done != MZ_TRUE )
{
if( !cdh_size )
need_data = MZ_TRUE;
else
{
ofs += cdh_size;
if( cdh_size < buf_remain )
{
buf_ptr += cdh_size;
buf_remain -= cdh_size;
}
else need_data = MZ_TRUE;
}

if( need_data )
{
mz_uint32 bytes_to_read = MZ_MIN( sizeof(buf_u32), cdir_size - ofs );

if( bytes_to_read )
{
if (pZip->m_pRead(pZip->m_pIO_opaque, cdir_ofs + ofs, pBuf, bytes_to_read ) != bytes_to_read )
return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);

buf_ptr = pBuf;
buf_remain = bytes_to_read;
}
else done = (total_files > 0) ? MZ_TRUE : MZ_FALSE;
}
}
}

if( done )
{
pZip->m_total_files = cdir_entries_on_this_disk = total_files;
}
}

if (pZip->m_pState->m_zip64)
{
mz_uint32 zip64_total_num_of_disks = MZ_READ_LE32(pZip64_locator + MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS);
Expand Down