@@ -2716,7 +2716,7 @@ tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_nex
27162716 }
27172717
27182718 dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start ;
2719- if ((dist > dist_from_out_buf_start || dist_from_out_buf_start == 0 ) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF ))
2719+ if ((dist == 0 || dist > dist_from_out_buf_start || dist_from_out_buf_start == 0 ) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF ))
27202720 {
27212721 TINFL_CR_RETURN_FOREVER (37 , TINFL_STATUS_FAILED );
27222722 }
@@ -3235,7 +3235,7 @@ struct mz_zip_internal_state_tag
32353235
32363236#define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE (array_ptr , element_size ) (array_ptr)->m_element_size = element_size
32373237
3238- #if defined(DEBUG ) || defined(_DEBUG ) || defined( NDEBUG )
3238+ #if defined(DEBUG ) || defined(_DEBUG )
32393239static MZ_FORCEINLINE mz_uint mz_zip_array_range_check (const mz_zip_array * pArray , mz_uint index )
32403240{
32413241 MZ_ASSERT (index < pArray -> m_size );
@@ -6392,13 +6392,13 @@ mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive *pZip, const char *pArchive_n
63926392 return MZ_TRUE ;
63936393}
63946394
6395- mz_bool mz_zip_writer_add_read_buf_callback (mz_zip_archive * pZip , const char * pArchive_name , mz_file_read_func read_callback , void * callback_opaque , mz_uint64 size_to_add , const MZ_TIME_T * pFile_time , const void * pComment , mz_uint16 comment_size , mz_uint level_and_flags ,
6395+ mz_bool mz_zip_writer_add_read_buf_callback (mz_zip_archive * pZip , const char * pArchive_name , mz_file_read_func read_callback , void * callback_opaque , mz_uint64 max_size , const MZ_TIME_T * pFile_time , const void * pComment , mz_uint16 comment_size , mz_uint level_and_flags ,
63966396 const char * user_extra_data , mz_uint user_extra_data_len , const char * user_extra_data_central , mz_uint user_extra_data_central_len )
63976397{
63986398 mz_uint16 gen_flags = MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR ;
63996399 mz_uint uncomp_crc32 = MZ_CRC32_INIT , level , num_alignment_padding_bytes ;
64006400 mz_uint16 method = 0 , dos_time = 0 , dos_date = 0 , ext_attributes = 0 ;
6401- mz_uint64 local_dir_header_ofs , cur_archive_file_ofs = pZip -> m_archive_size , uncomp_size = size_to_add , comp_size = 0 ;
6401+ mz_uint64 local_dir_header_ofs , cur_archive_file_ofs = pZip -> m_archive_size , uncomp_size = 0 , comp_size = 0 ;
64026402 size_t archive_name_size ;
64036403 mz_uint8 local_dir_header [MZ_ZIP_LOCAL_DIR_HEADER_SIZE ];
64046404 mz_uint8 * pExtra_data = NULL ;
@@ -6420,7 +6420,7 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA
64206420
64216421 pState = pZip -> m_pState ;
64226422
6423- if ((!pState -> m_zip64 ) && (uncomp_size > MZ_UINT32_MAX ))
6423+ if ((!pState -> m_zip64 ) && (max_size > MZ_UINT32_MAX ))
64246424 {
64256425 /* Source file is too large for non-zip64 */
64266426 /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */
@@ -6477,7 +6477,7 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA
64776477 }
64786478#endif
64796479
6480- if (uncomp_size <= 3 )
6480+ if (max_size <= 3 )
64816481 level = 0 ;
64826482
64836483 if (!mz_zip_writer_write_zeros (pZip , cur_archive_file_ofs , num_alignment_padding_bytes ))
@@ -6493,19 +6493,19 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA
64936493 MZ_ASSERT ((cur_archive_file_ofs & (pZip -> m_file_offset_alignment - 1 )) == 0 );
64946494 }
64956495
6496- if (uncomp_size && level )
6496+ if (max_size && level )
64976497 {
64986498 method = MZ_DEFLATED ;
64996499 }
65006500
65016501 MZ_CLEAR_OBJ (local_dir_header );
65026502 if (pState -> m_zip64 )
65036503 {
6504- if (uncomp_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX )
6504+ if (max_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX )
65056505 {
65066506 pExtra_data = extra_data ;
6507- extra_size = mz_zip_writer_create_zip64_extra_data (extra_data , (uncomp_size >= MZ_UINT32_MAX ) ? & uncomp_size : NULL ,
6508- (uncomp_size >= MZ_UINT32_MAX ) ? & comp_size : NULL , (local_dir_header_ofs >= MZ_UINT32_MAX ) ? & local_dir_header_ofs : NULL );
6507+ extra_size = mz_zip_writer_create_zip64_extra_data (extra_data , (max_size >= MZ_UINT32_MAX ) ? & uncomp_size : NULL ,
6508+ (max_size >= MZ_UINT32_MAX ) ? & comp_size : NULL , (local_dir_header_ofs >= MZ_UINT32_MAX ) ? & local_dir_header_ofs : NULL );
65096509 }
65106510
65116511 if (!mz_zip_writer_create_local_dir_header (pZip , local_dir_header , (mz_uint16 )archive_name_size , (mz_uint16 )(extra_size + user_extra_data_len ), 0 , 0 , 0 , method , gen_flags , dos_time , dos_date ))
@@ -6556,9 +6556,8 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA
65566556 cur_archive_file_ofs += user_extra_data_len ;
65576557 }
65586558
6559- if (uncomp_size )
6559+ if (max_size )
65606560 {
6561- mz_uint64 uncomp_remaining = uncomp_size ;
65626561 void * pRead_buf = pZip -> m_pAlloc (pZip -> m_pAlloc_opaque , 1 , MZ_ZIP_MAX_IO_BUF_SIZE );
65636562 if (!pRead_buf )
65646563 {
@@ -6567,19 +6566,27 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA
65676566
65686567 if (!level )
65696568 {
6570- while (uncomp_remaining )
6569+ while (1 )
65716570 {
6572- mz_uint n = (mz_uint )MZ_MIN ((mz_uint64 )MZ_ZIP_MAX_IO_BUF_SIZE , uncomp_remaining );
6573- if ((read_callback (callback_opaque , file_ofs , pRead_buf , n ) != n ) || (pZip -> m_pWrite (pZip -> m_pIO_opaque , cur_archive_file_ofs , pRead_buf , n ) != n ))
6571+ size_t n = read_callback (callback_opaque , file_ofs , pRead_buf , MZ_ZIP_MAX_IO_BUF_SIZE );
6572+ if (n == 0 )
6573+ break ;
6574+
6575+ if ((n > MZ_ZIP_MAX_IO_BUF_SIZE ) || (file_ofs + n > max_size ))
65746576 {
65756577 pZip -> m_pFree (pZip -> m_pAlloc_opaque , pRead_buf );
65766578 return mz_zip_set_error (pZip , MZ_ZIP_FILE_READ_FAILED );
65776579 }
6578- file_ofs += n ;
6580+ if (pZip -> m_pWrite (pZip -> m_pIO_opaque , cur_archive_file_ofs , pRead_buf , n ) != n )
6581+ {
6582+ pZip -> m_pFree (pZip -> m_pAlloc_opaque , pRead_buf );
6583+ return mz_zip_set_error (pZip , MZ_ZIP_FILE_WRITE_FAILED );
6584+ }
6585+ file_ofs += n ;
65796586 uncomp_crc32 = (mz_uint32 )mz_crc32 (uncomp_crc32 , (const mz_uint8 * )pRead_buf , n );
6580- uncomp_remaining -= n ;
65816587 cur_archive_file_ofs += n ;
65826588 }
6589+ uncomp_size = file_ofs ;
65836590 comp_size = uncomp_size ;
65846591 }
65856592 else
@@ -6606,24 +6613,26 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA
66066613
66076614 for (;;)
66086615 {
6609- size_t in_buf_size = (mz_uint32 )MZ_MIN (uncomp_remaining , (mz_uint64 )MZ_ZIP_MAX_IO_BUF_SIZE );
66106616 tdefl_status status ;
66116617 tdefl_flush flush = TDEFL_NO_FLUSH ;
66126618
6613- if (read_callback (callback_opaque , file_ofs , pRead_buf , in_buf_size )!= in_buf_size )
6619+ size_t n = read_callback (callback_opaque , file_ofs , pRead_buf , MZ_ZIP_MAX_IO_BUF_SIZE );
6620+ if ((n > MZ_ZIP_MAX_IO_BUF_SIZE ) || (file_ofs + n > max_size ))
66146621 {
66156622 mz_zip_set_error (pZip , MZ_ZIP_FILE_READ_FAILED );
66166623 break ;
66176624 }
66186625
6619- file_ofs += in_buf_size ;
6620- uncomp_crc32 = (mz_uint32 )mz_crc32 (uncomp_crc32 , (const mz_uint8 * )pRead_buf , in_buf_size );
6621- uncomp_remaining -= in_buf_size ;
6626+ file_ofs += n ;
6627+ uncomp_crc32 = (mz_uint32 )mz_crc32 (uncomp_crc32 , (const mz_uint8 * )pRead_buf , n );
66226628
66236629 if (pZip -> m_pNeeds_keepalive != NULL && pZip -> m_pNeeds_keepalive (pZip -> m_pIO_opaque ))
66246630 flush = TDEFL_FULL_FLUSH ;
66256631
6626- status = tdefl_compress_buffer (pComp , pRead_buf , in_buf_size , uncomp_remaining ? flush : TDEFL_FINISH );
6632+ if (n == 0 )
6633+ flush = TDEFL_FINISH ;
6634+
6635+ status = tdefl_compress_buffer (pComp , pRead_buf , n , flush );
66276636 if (status == TDEFL_STATUS_DONE )
66286637 {
66296638 result = MZ_TRUE ;
@@ -6644,6 +6653,7 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA
66446653 return MZ_FALSE ;
66456654 }
66466655
6656+ uncomp_size = file_ofs ;
66476657 comp_size = state .m_comp_size ;
66486658 cur_archive_file_ofs = state .m_cur_archive_file_ofs ;
66496659 }
@@ -6708,10 +6718,10 @@ static size_t mz_file_read_func_stdio(void *pOpaque, mz_uint64 file_ofs, void *p
67086718 return MZ_FREAD (pBuf , 1 , n , pSrc_file );
67096719}
67106720
6711- mz_bool mz_zip_writer_add_cfile (mz_zip_archive * pZip , const char * pArchive_name , MZ_FILE * pSrc_file , mz_uint64 size_to_add , const MZ_TIME_T * pFile_time , const void * pComment , mz_uint16 comment_size , mz_uint level_and_flags ,
6721+ mz_bool mz_zip_writer_add_cfile (mz_zip_archive * pZip , const char * pArchive_name , MZ_FILE * pSrc_file , mz_uint64 max_size , const MZ_TIME_T * pFile_time , const void * pComment , mz_uint16 comment_size , mz_uint level_and_flags ,
67126722 const char * user_extra_data , mz_uint user_extra_data_len , const char * user_extra_data_central , mz_uint user_extra_data_central_len )
67136723{
6714- return mz_zip_writer_add_read_buf_callback (pZip , pArchive_name , mz_file_read_func_stdio , pSrc_file , size_to_add , pFile_time , pComment , comment_size , level_and_flags ,
6724+ return mz_zip_writer_add_read_buf_callback (pZip , pArchive_name , mz_file_read_func_stdio , pSrc_file , max_size , pFile_time , pComment , comment_size , level_and_flags ,
67156725 user_extra_data , user_extra_data_len , user_extra_data_central , user_extra_data_central_len );
67166726}
67176727
0 commit comments