mirror of
				https://github.com/eledio-devices/thirdparty-miniz.git
				synced 2025-10-31 00:32:38 +01:00 
			
		
		
		
	Merge pull request #220 from sezero/z64
miniz_zip: fix mz_zip_reader_extract_to_heap to read correct sizes
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); |     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; |     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; |     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)) |     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); |         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)) |     if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) | ||||||
|         return MZ_FALSE; |         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; |     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_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; |     mz_uint32 file_index; | ||||||
|     if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index)) |     if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index)) | ||||||
|         return MZ_FALSE; |         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) | 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) | 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) | 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; |     mz_zip_archive_file_stat file_stat; | ||||||
|     const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index); |     mz_uint64 alloc_size; | ||||||
|     void *pBuf; |     void *pBuf; | ||||||
|  |  | ||||||
|     if (pSize) |     if (pSize) | ||||||
|         *pSize = 0; |         *pSize = 0; | ||||||
|  |  | ||||||
|     if (!p) |     if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) | ||||||
|     { |         return MZ_FALSE; | ||||||
|         mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); |  | ||||||
|         return NULL; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS); |     alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size : file_stat.m_uncomp_size; | ||||||
|     uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS); |  | ||||||
|  |  | ||||||
|     alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? comp_size : uncomp_size; |  | ||||||
|     if (((sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF)) |     if (((sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF)) | ||||||
|     { |     { | ||||||
|         mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); |         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; |         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); |         pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); | ||||||
|         return NULL; |         return NULL; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user