Skip to content

Commit

Permalink
Fix corrupt archive if uncompressed file smaller than 4 byte and file…
Browse files Browse the repository at this point in the history
… is added by mz_zip_writer_add_mem*
  • Loading branch information
uroni committed Jul 10, 2018
1 parent 3616bf8 commit 46cdde2
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions miniz_zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -3186,6 +3186,17 @@ mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive *pZip, const char *pArchive_n
}
#endif /* #ifndef MINIZ_NO_TIME */

if (!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA))
{
uncomp_crc32 = (mz_uint32)mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, buf_size);
uncomp_size = buf_size;
if (uncomp_size <= 3)
{
level = 0;
store_data_uncompressed = MZ_TRUE;
}
}

archive_name_size = strlen(pArchive_name);
if (archive_name_size > MZ_UINT16_MAX)
return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);
Expand Down Expand Up @@ -3301,24 +3312,13 @@ mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive *pZip, const char *pArchive_n
cur_archive_file_ofs += archive_name_size;
}

if (user_extra_data_len > 0)
{
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, user_extra_data, user_extra_data_len) != user_extra_data_len)
return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);

cur_archive_file_ofs += user_extra_data_len;
}
if (user_extra_data_len > 0)
{
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, user_extra_data, user_extra_data_len) != user_extra_data_len)
return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);

if (!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA))
{
uncomp_crc32 = (mz_uint32)mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, buf_size);
uncomp_size = buf_size;
if (uncomp_size <= 3)
{
level = 0;
store_data_uncompressed = MZ_TRUE;
}
}
cur_archive_file_ofs += user_extra_data_len;
}

if (store_data_uncompressed)
{
Expand Down

0 comments on commit 46cdde2

Please sign in to comment.