mirror of
				https://github.com/eledio-devices/thirdparty-miniz.git
				synced 2025-10-31 00:32:38 +01:00 
			
		
		
		
	miniz_zip: fix mz_zip_reader_extract_to_heap to read correct sizes
Fixes: https://github.com/richgel999/miniz/issues/218
This commit is contained in:
		
							
								
								
									
										33
									
								
								miniz_zip.c
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								miniz_zip.c
									
									
									
									
									
								
							| @@ -1456,7 +1456,8 @@ mz_bool mz_zip_reader_locate_file_v2(mz_zip_archive *pZip, const char *pName, co | ||||
|     return mz_zip_set_error(pZip, MZ_ZIP_FILE_NOT_FOUND); | ||||
| } | ||||
|  | ||||
| mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size) | ||||
| static | ||||
| mz_bool mz_zip_reader_extract_to_mem_no_alloc1(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size, const mz_zip_archive_file_stat *st) | ||||
| { | ||||
|     int status = TINFL_STATUS_DONE; | ||||
|     mz_uint64 needed_size, cur_file_ofs, comp_remaining, out_buf_ofs = 0, read_buf_size, read_buf_ofs = 0, read_buf_avail; | ||||
| @@ -1469,6 +1470,9 @@ mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, mz_uint file | ||||
|     if ((!pZip) || (!pZip->m_pState) || ((buf_size) && (!pBuf)) || ((user_read_buf_size) && (!pUser_read_buf)) || (!pZip->m_pRead)) | ||||
|         return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); | ||||
|  | ||||
|     if (st) { | ||||
|         file_stat = *st; | ||||
|     } else | ||||
|     if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) | ||||
|         return MZ_FALSE; | ||||
|  | ||||
| @@ -1599,17 +1603,22 @@ mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, mz_uint file | ||||
|     return status == TINFL_STATUS_DONE; | ||||
| } | ||||
|  | ||||
| mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size) | ||||
| { | ||||
|     return mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, buf_size, flags, pUser_read_buf, user_read_buf_size, NULL); | ||||
| } | ||||
|  | ||||
| mz_bool mz_zip_reader_extract_file_to_mem_no_alloc(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size) | ||||
| { | ||||
|     mz_uint32 file_index; | ||||
|     if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index)) | ||||
|         return MZ_FALSE; | ||||
|     return mz_zip_reader_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size, flags, pUser_read_buf, user_read_buf_size); | ||||
|     return mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, buf_size, flags, pUser_read_buf, user_read_buf_size, NULL); | ||||
| } | ||||
|  | ||||
| mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags) | ||||
| { | ||||
|     return mz_zip_reader_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size, flags, NULL, 0); | ||||
|     return mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, buf_size, flags, NULL, 0, NULL); | ||||
| } | ||||
|  | ||||
| mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags) | ||||
| @@ -1619,23 +1628,17 @@ mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive *pZip, const char *pFil | ||||
|  | ||||
| void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, size_t *pSize, mz_uint flags) | ||||
| { | ||||
|     mz_uint64 comp_size, uncomp_size, alloc_size; | ||||
|     const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index); | ||||
|     mz_zip_archive_file_stat file_stat; | ||||
|     mz_uint64 alloc_size; | ||||
|     void *pBuf; | ||||
|  | ||||
|     if (pSize) | ||||
|         *pSize = 0; | ||||
|  | ||||
|     if (!p) | ||||
|     { | ||||
|         mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); | ||||
|         return NULL; | ||||
|     } | ||||
|     if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) | ||||
|         return MZ_FALSE; | ||||
|  | ||||
|     comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS); | ||||
|     uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS); | ||||
|  | ||||
|     alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? comp_size : uncomp_size; | ||||
|     alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size : file_stat.m_uncomp_size; | ||||
|     if (((sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF)) | ||||
|     { | ||||
|         mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); | ||||
| @@ -1648,7 +1651,7 @@ void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, si | ||||
|         return NULL; | ||||
|     } | ||||
|  | ||||
|     if (!mz_zip_reader_extract_to_mem(pZip, file_index, pBuf, (size_t)alloc_size, flags)) | ||||
|     if (!mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, (size_t)alloc_size, flags, NULL, 0, &file_stat)) | ||||
|     { | ||||
|         pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); | ||||
|         return NULL; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user