mirror of
https://github.com/eledio-devices/thirdparty-miniz.git
synced 2025-11-01 00:38:28 +01:00
Compare commits
40 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
df65d5a1de | ||
|
|
46cdde2ac5 | ||
|
|
3616bf804b | ||
|
|
76237898c0 | ||
|
|
3af5c82fd3 | ||
|
|
55f4dfe1d2 | ||
|
|
b344eaf775 | ||
|
|
36b6c99f62 | ||
|
|
04f2169b8a | ||
|
|
14a5a1397b | ||
|
|
fef12d34f1 | ||
|
|
b01930542e | ||
|
|
9c88e826a2 | ||
|
|
22e4ef1b1d | ||
|
|
7a7d0b423b | ||
|
|
c17cc20c80 | ||
|
|
0f6b199e5b | ||
|
|
0b7d3070b9 | ||
|
|
aadc405b1c | ||
|
|
02c51d3662 | ||
|
|
9393a95f26 | ||
|
|
82e5b184e4 | ||
|
|
81df2df0f6 | ||
|
|
64d92e19ad | ||
|
|
404c574eba | ||
|
|
c1f791f81b | ||
|
|
c93e4d804c | ||
|
|
84e8458de0 | ||
|
|
78cdcb0fce | ||
|
|
0d13890f4b | ||
|
|
7ecd1b3677 | ||
|
|
a0786b126d | ||
|
|
9b1ddf6861 | ||
|
|
149b34c254 | ||
|
|
6e915ef7fc | ||
|
|
11f795a19c | ||
|
|
ec028ffe66 | ||
|
|
a6a989cc18 | ||
|
|
3d51050db5 | ||
|
|
3c7177c0cb |
@@ -1,7 +1,12 @@
|
||||
PROJECT(miniz)
|
||||
PROJECT(miniz C)
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
set(CMAKE_BUILD_TYPE Release)
|
||||
set(CMAKE_CONFIGURATION_TYPES Release)
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "")
|
||||
# CMake defaults to leaving CMAKE_BUILD_TYPE empty. This screws up
|
||||
# differentiation between debug and release builds.
|
||||
set(CMAKE_BUILD_TYPE "Release" CACHE STRING
|
||||
"Choose the type of build, options are: None (CMAKE_CXX_FLAGS or \
|
||||
CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel." FORCE)
|
||||
endif ()
|
||||
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
|
||||
|
||||
@@ -38,3 +43,10 @@ endif()
|
||||
|
||||
# add_executable(miniz_tester ${MINIZ_TESTER_SRC_LIST})
|
||||
# target_link_libraries(miniz_tester miniz)
|
||||
|
||||
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}Targets
|
||||
ARCHIVE DESTINATION lib
|
||||
LIBRARY DESTINATION lib
|
||||
)
|
||||
file(GLOB INSTALL_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
|
||||
install(FILES ${INSTALL_HEADERS} DESTINATION include/${PROJECT_NAME})
|
||||
30
ChangeLog.md
30
ChangeLog.md
@@ -1,5 +1,35 @@
|
||||
## Changelog
|
||||
|
||||
### 2.0.8
|
||||
|
||||
- Remove unimplemented functions (mz_zip_locate_file and mz_zip_locate_file_v2)
|
||||
- Add license, changelog, readme and example files to release zip
|
||||
- Fix heap overflow to user buffer in tinfl_status tinfl_decompress
|
||||
- Fix corrupt archive if uncompressed file smaller than 4 byte and file is added by mz_zip_writer_add_mem*
|
||||
|
||||
### 2.0.7
|
||||
|
||||
- Removed need in C++ compiler in cmake build
|
||||
- Fixed loads of uninitilized value errors found with Valgrind by memsetting m_dict to 0 in tdefl_init.
|
||||
- Fix resource leak in mz_zip_reader_init_file_v2
|
||||
- Fix assert with mz_zip_writer_add_mem* w/MZ_DEFAULT_COMPRESSION
|
||||
- cmake build: install library and headers
|
||||
- Remove _LARGEFILE64_SOURCE requirement from apple defines for large files
|
||||
|
||||
### 2.0.6
|
||||
|
||||
- Improve MZ_ZIP_FLAG_WRITE_ZIP64 documentation
|
||||
- Remove check for cur_archive_file_ofs > UINT_MAX, because cur_archive_file_ofs is not used after this point
|
||||
- Add cmake debug configuration
|
||||
- Fix PNG height when creating png files
|
||||
- Add "iterative" file extraction method based on mz_zip_reader_extract_to_callback.
|
||||
- Option to use memcpy for unaligned data access
|
||||
- Define processor/arch macros as zero if not set to one
|
||||
|
||||
### 2.0.4/2.0.5
|
||||
|
||||
- Fix compilation with the various omission compile definitions
|
||||
|
||||
### 2.0.3
|
||||
|
||||
- Fix GCC/clang compile warnings
|
||||
|
||||
20
amalgamate.sh
Normal file → Executable file
20
amalgamate.sh
Normal file → Executable file
@@ -41,9 +41,27 @@ done
|
||||
rm test.out
|
||||
rm main.c
|
||||
|
||||
cp ChangeLog.md amalgamation/
|
||||
cp LICENSE amalgamation/
|
||||
cp readme.md amalgamation/
|
||||
mkdir -p amalgamation/examples
|
||||
cp examples/* amalgamation/examples/
|
||||
|
||||
cd amalgamation
|
||||
! test -e miniz.zip || rm miniz.zip
|
||||
echo -e "miniz.c\nminiz.h" | zip -@ miniz
|
||||
cat << EOF | zip -@ miniz
|
||||
miniz.c
|
||||
miniz.h
|
||||
ChangeLog.md
|
||||
LICENSE
|
||||
readme.md
|
||||
examples/example1.c
|
||||
examples/example2.c
|
||||
examples/example3.c
|
||||
examples/example4.c
|
||||
examples/example5.c
|
||||
examples/example6.c
|
||||
EOF
|
||||
cd ..
|
||||
|
||||
echo "Amalgamation created."
|
||||
|
||||
3
miniz.c
3
miniz.c
@@ -559,8 +559,7 @@ const char *mz_error(int err)
|
||||
const char *m_pDesc;
|
||||
} s_error_descs[] =
|
||||
{
|
||||
{ MZ_OK, "" }, { MZ_STREAM_END, "stream end" }, { MZ_NEED_DICT, "need dictionary" }, { MZ_ERRNO, "file error" }, { MZ_STREAM_ERROR, "stream error" },
|
||||
{ MZ_DATA_ERROR, "data error" }, { MZ_MEM_ERROR, "out of memory" }, { MZ_BUF_ERROR, "buf error" }, { MZ_VERSION_ERROR, "version error" }, { MZ_PARAM_ERROR, "parameter error" }
|
||||
{ MZ_OK, "" }, { MZ_STREAM_END, "stream end" }, { MZ_NEED_DICT, "need dictionary" }, { MZ_ERRNO, "file error" }, { MZ_STREAM_ERROR, "stream error" }, { MZ_DATA_ERROR, "data error" }, { MZ_MEM_ERROR, "out of memory" }, { MZ_BUF_ERROR, "buf error" }, { MZ_VERSION_ERROR, "version error" }, { MZ_PARAM_ERROR, "parameter error" }
|
||||
};
|
||||
mz_uint i;
|
||||
for (i = 0; i < sizeof(s_error_descs) / sizeof(s_error_descs[0]); ++i)
|
||||
|
||||
16
miniz.h
16
miniz.h
@@ -1,4 +1,4 @@
|
||||
/* miniz.c v1.16 beta r1 - public domain deflate/inflate, zlib-subset, ZIP reading/writing/appending, PNG writing
|
||||
/* miniz.c 2.0.8 - public domain deflate/inflate, zlib-subset, ZIP reading/writing/appending, PNG writing
|
||||
See "unlicense" statement at the end of this file.
|
||||
Rich Geldreich <richgel99@gmail.com>, last updated Oct. 13, 2013
|
||||
Implements RFC 1950: http://www.ietf.org/rfc/rfc1950.txt and RFC 1951: http://www.ietf.org/rfc/rfc1951.txt
|
||||
@@ -159,21 +159,29 @@
|
||||
#if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__i386) || defined(__i486__) || defined(__i486) || defined(i386) || defined(__ia64__) || defined(__x86_64__)
|
||||
/* MINIZ_X86_OR_X64_CPU is only used to help set the below macros. */
|
||||
#define MINIZ_X86_OR_X64_CPU 1
|
||||
#else
|
||||
#define MINIZ_X86_OR_X64_CPU 0
|
||||
#endif
|
||||
|
||||
#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU
|
||||
/* Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian. */
|
||||
#define MINIZ_LITTLE_ENDIAN 1
|
||||
#else
|
||||
#define MINIZ_LITTLE_ENDIAN 0
|
||||
#endif
|
||||
|
||||
#if MINIZ_X86_OR_X64_CPU
|
||||
/* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient integer loads and stores from unaligned addresses. */
|
||||
#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1
|
||||
#else
|
||||
#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0
|
||||
#endif
|
||||
|
||||
#if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) || defined(__ia64__) || defined(__x86_64__)
|
||||
/* Set MINIZ_HAS_64BIT_REGISTERS to 1 if operations on 64-bit integers are reasonably fast (and don't involve compiler generated calls to helper functions). */
|
||||
#define MINIZ_HAS_64BIT_REGISTERS 1
|
||||
#else
|
||||
#define MINIZ_HAS_64BIT_REGISTERS 0
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -226,11 +234,11 @@ enum
|
||||
MZ_DEFAULT_COMPRESSION = -1
|
||||
};
|
||||
|
||||
#define MZ_VERSION "10.0.0"
|
||||
#define MZ_VERNUM 0xA000
|
||||
#define MZ_VERSION "10.0.3"
|
||||
#define MZ_VERNUM 0xA030
|
||||
#define MZ_VER_MAJOR 10
|
||||
#define MZ_VER_MINOR 0
|
||||
#define MZ_VER_REVISION 0
|
||||
#define MZ_VER_REVISION 3
|
||||
#define MZ_VER_SUBREVISION 0
|
||||
|
||||
#ifndef MINIZ_NO_ZLIB_APIS
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#pragma once
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/* ------------------- Types and macros */
|
||||
typedef unsigned char mz_uint8;
|
||||
|
||||
32
miniz_tdef.c
32
miniz_tdef.c
@@ -737,13 +737,29 @@ static int tdefl_flush_block(tdefl_compressor *d, int flush)
|
||||
}
|
||||
|
||||
#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
|
||||
#ifdef MINIZ_UNALIGNED_USE_MEMCPY
|
||||
static inline mz_uint16 TDEFL_READ_UNALIGNED_WORD(const mz_uint8* p)
|
||||
{
|
||||
mz_uint16 ret;
|
||||
memcpy(&ret, p, sizeof(mz_uint16));
|
||||
return ret;
|
||||
}
|
||||
static inline mz_uint16 TDEFL_READ_UNALIGNED_WORD2(const mz_uint16* p)
|
||||
{
|
||||
mz_uint16 ret;
|
||||
memcpy(&ret, p, sizeof(mz_uint16));
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
#define TDEFL_READ_UNALIGNED_WORD(p) *(const mz_uint16 *)(p)
|
||||
#define TDEFL_READ_UNALIGNED_WORD2(p) *(const mz_uint16 *)(p)
|
||||
#endif
|
||||
static MZ_FORCEINLINE void tdefl_find_match(tdefl_compressor *d, mz_uint lookahead_pos, mz_uint max_dist, mz_uint max_match_len, mz_uint *pMatch_dist, mz_uint *pMatch_len)
|
||||
{
|
||||
mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len;
|
||||
mz_uint num_probes_left = d->m_max_probes[match_len >= 32];
|
||||
const mz_uint16 *s = (const mz_uint16 *)(d->m_dict + pos), *p, *q;
|
||||
mz_uint16 c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]), s01 = TDEFL_READ_UNALIGNED_WORD(s);
|
||||
mz_uint16 c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]), s01 = TDEFL_READ_UNALIGNED_WORD2(s);
|
||||
MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN);
|
||||
if (max_match_len <= match_len)
|
||||
return;
|
||||
@@ -767,14 +783,14 @@ static MZ_FORCEINLINE void tdefl_find_match(tdefl_compressor *d, mz_uint lookahe
|
||||
if (!dist)
|
||||
break;
|
||||
q = (const mz_uint16 *)(d->m_dict + probe_pos);
|
||||
if (TDEFL_READ_UNALIGNED_WORD(q) != s01)
|
||||
if (TDEFL_READ_UNALIGNED_WORD2(q) != s01)
|
||||
continue;
|
||||
p = s;
|
||||
probe_len = 32;
|
||||
do
|
||||
{
|
||||
} while ((TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) &&
|
||||
(TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (--probe_len > 0));
|
||||
} while ((TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) &&
|
||||
(TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (--probe_len > 0));
|
||||
if (!probe_len)
|
||||
{
|
||||
*pMatch_dist = dist;
|
||||
@@ -883,8 +899,8 @@ static mz_bool tdefl_compress_fast(tdefl_compressor *d)
|
||||
mz_uint32 probe_len = 32;
|
||||
do
|
||||
{
|
||||
} while ((TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) &&
|
||||
(TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (--probe_len > 0));
|
||||
} while ((TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) &&
|
||||
(TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (--probe_len > 0));
|
||||
cur_match_len = ((mz_uint)(p - (const mz_uint16 *)pCur_dict) * 2) + (mz_uint)(*(const mz_uint8 *)p == *(const mz_uint8 *)q);
|
||||
if (!probe_len)
|
||||
cur_match_len = cur_match_dist ? TDEFL_MAX_MATCH_LEN : 0;
|
||||
@@ -1318,6 +1334,8 @@ tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_fun
|
||||
d->m_pSrc = NULL;
|
||||
d->m_src_buf_left = 0;
|
||||
d->m_out_buf_ofs = 0;
|
||||
if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG))
|
||||
MZ_CLEAR_OBJ(d->m_dict);
|
||||
memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0);
|
||||
memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1);
|
||||
return TDEFL_STATUS_OKAY;
|
||||
@@ -1490,7 +1508,7 @@ void *tdefl_write_image_to_png_file_in_memory_ex(const void *pImage, int w, int
|
||||
pnghdr[18] = (mz_uint8)(w >> 8);
|
||||
pnghdr[19] = (mz_uint8)w;
|
||||
pnghdr[22] = (mz_uint8)(h >> 8);
|
||||
pnghdr[22] = (mz_uint8)h;
|
||||
pnghdr[23] = (mz_uint8)h;
|
||||
pnghdr[25] = chans[num_chans];
|
||||
pnghdr[33] = (mz_uint8)(*pLen_out >> 24);
|
||||
pnghdr[34] = (mz_uint8)(*pLen_out >> 16);
|
||||
|
||||
@@ -111,8 +111,7 @@ enum
|
||||
#endif
|
||||
|
||||
/* The low-level tdefl functions below may be used directly if the above helper functions aren't flexible enough. The low-level functions don't make any heap allocations, unlike the above helper functions. */
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
TDEFL_STATUS_BAD_PARAM = -2,
|
||||
TDEFL_STATUS_PUT_BUF_FAILED = -1,
|
||||
TDEFL_STATUS_OKAY = 0,
|
||||
@@ -120,8 +119,7 @@ typedef enum
|
||||
} tdefl_status;
|
||||
|
||||
/* Must map to MZ_NO_FLUSH, MZ_SYNC_FLUSH, etc. enums */
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
TDEFL_NO_FLUSH = 0,
|
||||
TDEFL_SYNC_FLUSH = 2,
|
||||
TDEFL_FULL_FLUSH = 3,
|
||||
|
||||
@@ -45,8 +45,7 @@ extern "C" {
|
||||
status = result; \
|
||||
r->m_state = state_index; \
|
||||
goto common_exit; \
|
||||
case state_index: \
|
||||
; \
|
||||
case state_index:; \
|
||||
} \
|
||||
MZ_MACRO_END
|
||||
#define TINFL_CR_RETURN_FOREVER(state_index, result) \
|
||||
@@ -541,18 +540,19 @@ tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_nex
|
||||
}
|
||||
}
|
||||
#endif
|
||||
do
|
||||
while(counter>2)
|
||||
{
|
||||
pOut_buf_cur[0] = pSrc[0];
|
||||
pOut_buf_cur[1] = pSrc[1];
|
||||
pOut_buf_cur[2] = pSrc[2];
|
||||
pOut_buf_cur += 3;
|
||||
pSrc += 3;
|
||||
} while ((int)(counter -= 3) > 2);
|
||||
if ((int)counter > 0)
|
||||
counter -= 3;
|
||||
}
|
||||
if (counter > 0)
|
||||
{
|
||||
pOut_buf_cur[0] = pSrc[0];
|
||||
if ((int)counter > 1)
|
||||
if (counter > 1)
|
||||
pOut_buf_cur[1] = pSrc[1];
|
||||
pOut_buf_cur += counter;
|
||||
}
|
||||
|
||||
@@ -52,8 +52,7 @@ void tinfl_decompressor_free(tinfl_decompressor *pDecomp);
|
||||
#define TINFL_LZ_DICT_SIZE 32768
|
||||
|
||||
/* Return status. */
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
/* This flags indicates the inflator needs 1 or more input bytes to make forward progress, but the caller is indicating that no more are available. The compressed data */
|
||||
/* is probably corrupted. If you call the inflator again with more bytes it'll try to continue processing the input but this is a BAD sign (either the data is corrupted or you called it incorrectly). */
|
||||
/* If you call it again with no input you'll just get TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS again. */
|
||||
@@ -118,6 +117,8 @@ typedef struct
|
||||
|
||||
#if MINIZ_HAS_64BIT_REGISTERS
|
||||
#define TINFL_USE_64BIT_BITBUF 1
|
||||
#else
|
||||
#define TINFL_USE_64BIT_BITBUF 0
|
||||
#endif
|
||||
|
||||
#if TINFL_USE_64BIT_BITBUF
|
||||
|
||||
342
miniz_zip.c
342
miniz_zip.c
@@ -112,7 +112,7 @@ static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream)
|
||||
#define MZ_FFLUSH fflush
|
||||
#define MZ_FREOPEN(p, m, s) freopen64(p, m, s)
|
||||
#define MZ_DELETE_FILE remove
|
||||
#elif defined(__APPLE__) && _LARGEFILE64_SOURCE
|
||||
#elif defined(__APPLE__)
|
||||
#ifndef MINIZ_NO_TIME
|
||||
#include <utime.h>
|
||||
#endif
|
||||
@@ -978,7 +978,10 @@ mz_bool mz_zip_reader_init_file_v2(mz_zip_archive *pZip, const char *pFilename,
|
||||
/* TODO: Better sanity check archive_size and the # of actual remaining bytes */
|
||||
|
||||
if (file_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
|
||||
{
|
||||
MZ_FCLOSE(pFile);
|
||||
return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
|
||||
}
|
||||
|
||||
if (!mz_zip_reader_init_internal(pZip, flags))
|
||||
{
|
||||
@@ -1817,6 +1820,304 @@ mz_bool mz_zip_reader_extract_file_to_callback(mz_zip_archive *pZip, const char
|
||||
return mz_zip_reader_extract_to_callback(pZip, file_index, pCallback, pOpaque, flags);
|
||||
}
|
||||
|
||||
mz_zip_reader_extract_iter_state* mz_zip_reader_extract_iter_new(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags)
|
||||
{
|
||||
mz_zip_reader_extract_iter_state *pState;
|
||||
mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
|
||||
mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;
|
||||
|
||||
/* Argument sanity check */
|
||||
if ((!pZip) || (!pZip->m_pState))
|
||||
return NULL;
|
||||
|
||||
/* Allocate an iterator status structure */
|
||||
pState = (mz_zip_reader_extract_iter_state*)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_reader_extract_iter_state));
|
||||
if (!pState)
|
||||
{
|
||||
mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Fetch file details */
|
||||
if (!mz_zip_reader_file_stat(pZip, file_index, &pState->file_stat))
|
||||
{
|
||||
pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Encryption and patch files are not supported. */
|
||||
if (pState->file_stat.m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG))
|
||||
{
|
||||
mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
|
||||
pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* This function only supports decompressing stored and deflate. */
|
||||
if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (pState->file_stat.m_method != 0) && (pState->file_stat.m_method != MZ_DEFLATED))
|
||||
{
|
||||
mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
|
||||
pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Init state - save args */
|
||||
pState->pZip = pZip;
|
||||
pState->flags = flags;
|
||||
|
||||
/* Init state - reset variables to defaults */
|
||||
pState->status = TINFL_STATUS_DONE;
|
||||
#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
|
||||
pState->file_crc32 = MZ_CRC32_INIT;
|
||||
#endif
|
||||
pState->read_buf_ofs = 0;
|
||||
pState->out_buf_ofs = 0;
|
||||
pState->pRead_buf = NULL;
|
||||
pState->pWrite_buf = NULL;
|
||||
pState->out_blk_remain = 0;
|
||||
|
||||
/* Read and parse the local directory entry. */
|
||||
pState->cur_file_ofs = pState->file_stat.m_local_header_ofs;
|
||||
if (pZip->m_pRead(pZip->m_pIO_opaque, pState->cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE)
|
||||
{
|
||||
mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
|
||||
pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)
|
||||
{
|
||||
mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
|
||||
pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pState->cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);
|
||||
if ((pState->cur_file_ofs + pState->file_stat.m_comp_size) > pZip->m_archive_size)
|
||||
{
|
||||
mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
|
||||
pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Decompress the file either directly from memory or from a file input buffer. */
|
||||
if (pZip->m_pState->m_pMem)
|
||||
{
|
||||
pState->pRead_buf = (mz_uint8 *)pZip->m_pState->m_pMem + pState->cur_file_ofs;
|
||||
pState->read_buf_size = pState->read_buf_avail = pState->file_stat.m_comp_size;
|
||||
pState->comp_remaining = pState->file_stat.m_comp_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method)))
|
||||
{
|
||||
/* Decompression required, therefore intermediate read buffer required */
|
||||
pState->read_buf_size = MZ_MIN(pState->file_stat.m_comp_size, MZ_ZIP_MAX_IO_BUF_SIZE);
|
||||
if (NULL == (pState->pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)pState->read_buf_size)))
|
||||
{
|
||||
mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
|
||||
pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Decompression not required - we will be reading directly into user buffer, no temp buf required */
|
||||
pState->read_buf_size = 0;
|
||||
}
|
||||
pState->read_buf_avail = 0;
|
||||
pState->comp_remaining = pState->file_stat.m_comp_size;
|
||||
}
|
||||
|
||||
if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method)))
|
||||
{
|
||||
/* Decompression required, init decompressor */
|
||||
tinfl_init( &pState->inflator );
|
||||
|
||||
/* Allocate write buffer */
|
||||
if (NULL == (pState->pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, TINFL_LZ_DICT_SIZE)))
|
||||
{
|
||||
mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
|
||||
if (pState->pRead_buf)
|
||||
pZip->m_pFree(pZip->m_pAlloc_opaque, pState->pRead_buf);
|
||||
pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return pState;
|
||||
}
|
||||
|
||||
mz_zip_reader_extract_iter_state* mz_zip_reader_extract_file_iter_new(mz_zip_archive *pZip, const char *pFilename, mz_uint flags)
|
||||
{
|
||||
mz_uint32 file_index;
|
||||
|
||||
/* Locate file index by name */
|
||||
if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index))
|
||||
return NULL;
|
||||
|
||||
/* Construct iterator */
|
||||
return mz_zip_reader_extract_iter_new(pZip, file_index, flags);
|
||||
}
|
||||
|
||||
size_t mz_zip_reader_extract_iter_read(mz_zip_reader_extract_iter_state* pState, void* pvBuf, size_t buf_size)
|
||||
{
|
||||
size_t copied_to_caller = 0;
|
||||
|
||||
/* Argument sanity check */
|
||||
if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState) || (!pvBuf))
|
||||
return 0;
|
||||
|
||||
if ((pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method))
|
||||
{
|
||||
/* The file is stored or the caller has requested the compressed data, calc amount to return. */
|
||||
copied_to_caller = MZ_MIN( buf_size, pState->comp_remaining );
|
||||
|
||||
/* Zip is in memory....or requires reading from a file? */
|
||||
if (pState->pZip->m_pState->m_pMem)
|
||||
{
|
||||
/* Copy data to caller's buffer */
|
||||
memcpy( pvBuf, pState->pRead_buf, copied_to_caller );
|
||||
pState->pRead_buf = ((mz_uint8*)pState->pRead_buf) + copied_to_caller;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Read directly into caller's buffer */
|
||||
if (pState->pZip->m_pRead(pState->pZip->m_pIO_opaque, pState->cur_file_ofs, pvBuf, copied_to_caller) != copied_to_caller)
|
||||
{
|
||||
/* Failed to read all that was asked for, flag failure and alert user */
|
||||
mz_zip_set_error(pState->pZip, MZ_ZIP_FILE_READ_FAILED);
|
||||
pState->status = TINFL_STATUS_FAILED;
|
||||
copied_to_caller = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
|
||||
/* Compute CRC if not returning compressed data only */
|
||||
if (!(pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA))
|
||||
pState->file_crc32 = (mz_uint32)mz_crc32(pState->file_crc32, (const mz_uint8 *)pvBuf, copied_to_caller);
|
||||
#endif
|
||||
|
||||
/* Advance offsets, dec counters */
|
||||
pState->cur_file_ofs += copied_to_caller;
|
||||
pState->out_buf_ofs += copied_to_caller;
|
||||
pState->comp_remaining -= copied_to_caller;
|
||||
}
|
||||
else
|
||||
{
|
||||
do
|
||||
{
|
||||
/* Calc ptr to write buffer - given current output pos and block size */
|
||||
mz_uint8 *pWrite_buf_cur = (mz_uint8 *)pState->pWrite_buf + (pState->out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));
|
||||
|
||||
/* Calc max output size - given current output pos and block size */
|
||||
size_t in_buf_size, out_buf_size = TINFL_LZ_DICT_SIZE - (pState->out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));
|
||||
|
||||
if (!pState->out_blk_remain)
|
||||
{
|
||||
/* Read more data from file if none available (and reading from file) */
|
||||
if ((!pState->read_buf_avail) && (!pState->pZip->m_pState->m_pMem))
|
||||
{
|
||||
/* Calc read size */
|
||||
pState->read_buf_avail = MZ_MIN(pState->read_buf_size, pState->comp_remaining);
|
||||
if (pState->pZip->m_pRead(pState->pZip->m_pIO_opaque, pState->cur_file_ofs, pState->pRead_buf, (size_t)pState->read_buf_avail) != pState->read_buf_avail)
|
||||
{
|
||||
mz_zip_set_error(pState->pZip, MZ_ZIP_FILE_READ_FAILED);
|
||||
pState->status = TINFL_STATUS_FAILED;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Advance offsets, dec counters */
|
||||
pState->cur_file_ofs += pState->read_buf_avail;
|
||||
pState->comp_remaining -= pState->read_buf_avail;
|
||||
pState->read_buf_ofs = 0;
|
||||
}
|
||||
|
||||
/* Perform decompression */
|
||||
in_buf_size = (size_t)pState->read_buf_avail;
|
||||
pState->status = tinfl_decompress(&pState->inflator, (const mz_uint8 *)pState->pRead_buf + pState->read_buf_ofs, &in_buf_size, (mz_uint8 *)pState->pWrite_buf, pWrite_buf_cur, &out_buf_size, pState->comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0);
|
||||
pState->read_buf_avail -= in_buf_size;
|
||||
pState->read_buf_ofs += in_buf_size;
|
||||
|
||||
/* Update current output block size remaining */
|
||||
pState->out_blk_remain = out_buf_size;
|
||||
}
|
||||
|
||||
if (pState->out_blk_remain)
|
||||
{
|
||||
/* Calc amount to return. */
|
||||
size_t to_copy = MZ_MIN( (buf_size - copied_to_caller), pState->out_blk_remain );
|
||||
|
||||
/* Copy data to caller's buffer */
|
||||
memcpy( (uint8_t*)pvBuf + copied_to_caller, pWrite_buf_cur, to_copy );
|
||||
|
||||
#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
|
||||
/* Perform CRC */
|
||||
pState->file_crc32 = (mz_uint32)mz_crc32(pState->file_crc32, pWrite_buf_cur, to_copy);
|
||||
#endif
|
||||
|
||||
/* Decrement data consumed from block */
|
||||
pState->out_blk_remain -= to_copy;
|
||||
|
||||
/* Inc output offset, while performing sanity check */
|
||||
if ((pState->out_buf_ofs += to_copy) > pState->file_stat.m_uncomp_size)
|
||||
{
|
||||
mz_zip_set_error(pState->pZip, MZ_ZIP_DECOMPRESSION_FAILED);
|
||||
pState->status = TINFL_STATUS_FAILED;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Increment counter of data copied to caller */
|
||||
copied_to_caller += to_copy;
|
||||
}
|
||||
} while ( (copied_to_caller < buf_size) && ((pState->status == TINFL_STATUS_NEEDS_MORE_INPUT) || (pState->status == TINFL_STATUS_HAS_MORE_OUTPUT)) );
|
||||
}
|
||||
|
||||
/* Return how many bytes were copied into user buffer */
|
||||
return copied_to_caller;
|
||||
}
|
||||
|
||||
mz_bool mz_zip_reader_extract_iter_free(mz_zip_reader_extract_iter_state* pState)
|
||||
{
|
||||
int status;
|
||||
|
||||
/* Argument sanity check */
|
||||
if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState))
|
||||
return MZ_FALSE;
|
||||
|
||||
/* Was decompression completed and requested? */
|
||||
if ((pState->status == TINFL_STATUS_DONE) && (!(pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA)))
|
||||
{
|
||||
/* Make sure the entire file was decompressed, and check its CRC. */
|
||||
if (pState->out_buf_ofs != pState->file_stat.m_uncomp_size)
|
||||
{
|
||||
mz_zip_set_error(pState->pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE);
|
||||
pState->status = TINFL_STATUS_FAILED;
|
||||
}
|
||||
#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
|
||||
else if (pState->file_crc32 != pState->file_stat.m_crc32)
|
||||
{
|
||||
mz_zip_set_error(pState->pZip, MZ_ZIP_DECOMPRESSION_FAILED);
|
||||
pState->status = TINFL_STATUS_FAILED;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Free buffers */
|
||||
if (!pState->pZip->m_pState->m_pMem)
|
||||
pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState->pRead_buf);
|
||||
if (pState->pWrite_buf)
|
||||
pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState->pWrite_buf);
|
||||
|
||||
/* Save status */
|
||||
status = pState->status;
|
||||
|
||||
/* Free context */
|
||||
pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState);
|
||||
|
||||
return status == TINFL_STATUS_DONE;
|
||||
}
|
||||
|
||||
#ifndef MINIZ_NO_STDIO
|
||||
static size_t mz_zip_file_write_callback(void *pOpaque, mz_uint64 ofs, const void *pBuf, size_t n)
|
||||
{
|
||||
@@ -2830,14 +3131,15 @@ mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive *pZip, const char *pArchive_n
|
||||
mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE];
|
||||
mz_uint16 bit_flags = 0;
|
||||
|
||||
if ((int)level_and_flags < 0)
|
||||
level_and_flags = MZ_DEFAULT_LEVEL;
|
||||
|
||||
if (uncomp_size || (buf_size && !(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)))
|
||||
bit_flags |= MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR;
|
||||
|
||||
if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME))
|
||||
bit_flags |= MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8;
|
||||
|
||||
if ((int)level_and_flags < 0)
|
||||
level_and_flags = MZ_DEFAULT_LEVEL;
|
||||
level = level_and_flags & 0xF;
|
||||
store_data_uncompressed = ((!level) || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA));
|
||||
|
||||
@@ -2884,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);
|
||||
@@ -2897,7 +3210,10 @@ mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive *pZip, const char *pArchive_n
|
||||
if (!pState->m_zip64)
|
||||
{
|
||||
/* Bail early if the archive would obviously become too large */
|
||||
if ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) > 0xFFFFFFFF)
|
||||
if ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size
|
||||
+ MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + user_extra_data_len +
|
||||
pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + user_extra_data_central_len
|
||||
+ MZ_ZIP_DATA_DESCRIPTER_SIZE32) > 0xFFFFFFFF)
|
||||
{
|
||||
pState->m_zip64 = MZ_TRUE;
|
||||
/*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */
|
||||
@@ -3004,17 +3320,6 @@ mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive *pZip, const char *pArchive_n
|
||||
cur_archive_file_ofs += user_extra_data_len;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
if (store_data_uncompressed)
|
||||
{
|
||||
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pBuf, buf_size) != buf_size)
|
||||
@@ -3059,7 +3364,7 @@ mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive *pZip, const char *pArchive_n
|
||||
MZ_WRITE_LE32(local_dir_footer + 4, uncomp_crc32);
|
||||
if (pExtra_data == NULL)
|
||||
{
|
||||
if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX))
|
||||
if (comp_size > MZ_UINT32_MAX)
|
||||
return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
|
||||
|
||||
MZ_WRITE_LE32(local_dir_footer + 8, comp_size);
|
||||
@@ -3164,7 +3469,9 @@ mz_bool mz_zip_writer_add_cfile(mz_zip_archive *pZip, const char *pArchive_name,
|
||||
if (!pState->m_zip64)
|
||||
{
|
||||
/* Bail early if the archive would obviously become too large */
|
||||
if ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + 1024) > 0xFFFFFFFF)
|
||||
if ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE
|
||||
+ archive_name_size + comment_size + user_extra_data_len + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + 1024
|
||||
+ MZ_ZIP_DATA_DESCRIPTER_SIZE32 + user_extra_data_central_len) > 0xFFFFFFFF)
|
||||
{
|
||||
pState->m_zip64 = MZ_TRUE;
|
||||
/*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */
|
||||
@@ -4356,4 +4663,3 @@ mz_bool mz_zip_end(mz_zip_archive *pZip)
|
||||
#endif
|
||||
|
||||
#endif /*#ifndef MINIZ_NO_ARCHIVE_APIS*/
|
||||
|
||||
|
||||
50
miniz_zip.h
50
miniz_zip.h
@@ -81,29 +81,26 @@ typedef mz_bool (*mz_file_needs_keepalive)(void *pOpaque);
|
||||
struct mz_zip_internal_state_tag;
|
||||
typedef struct mz_zip_internal_state_tag mz_zip_internal_state;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
MZ_ZIP_MODE_INVALID = 0,
|
||||
MZ_ZIP_MODE_READING = 1,
|
||||
MZ_ZIP_MODE_WRITING = 2,
|
||||
MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED = 3
|
||||
} mz_zip_mode;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
MZ_ZIP_FLAG_CASE_SENSITIVE = 0x0100,
|
||||
MZ_ZIP_FLAG_IGNORE_PATH = 0x0200,
|
||||
MZ_ZIP_FLAG_COMPRESSED_DATA = 0x0400,
|
||||
MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x0800,
|
||||
MZ_ZIP_FLAG_VALIDATE_LOCATE_FILE_FLAG = 0x1000, /* if enabled, mz_zip_reader_locate_file() will be called on each file as its validated to ensure the func finds the file in the central dir (intended for testing) */
|
||||
MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY = 0x2000, /* validate the local headers, but don't decompress the entire file and check the crc32 */
|
||||
MZ_ZIP_FLAG_WRITE_ZIP64 = 0x4000, /* use the zip64 file format, instead of the original zip file format */
|
||||
MZ_ZIP_FLAG_WRITE_ZIP64 = 0x4000, /* always use the zip64 file format, instead of the original zip file format with automatic switch to zip64. Use as flags parameter with mz_zip_writer_init*_v2 */
|
||||
MZ_ZIP_FLAG_WRITE_ALLOW_READING = 0x8000,
|
||||
MZ_ZIP_FLAG_ASCII_FILENAME = 0x10000
|
||||
} mz_zip_flags;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
MZ_ZIP_TYPE_INVALID = 0,
|
||||
MZ_ZIP_TYPE_USER,
|
||||
MZ_ZIP_TYPE_MEMORY,
|
||||
@@ -114,8 +111,7 @@ typedef enum
|
||||
} mz_zip_type;
|
||||
|
||||
/* miniz error codes. Be sure to update mz_zip_get_error_string() if you add or modify this enum. */
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
MZ_ZIP_NO_ERROR = 0,
|
||||
MZ_ZIP_UNDEFINED_ERROR,
|
||||
MZ_ZIP_TOO_MANY_FILES,
|
||||
@@ -178,6 +174,26 @@ typedef struct
|
||||
|
||||
} mz_zip_archive;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
mz_zip_archive *pZip;
|
||||
mz_uint flags;
|
||||
|
||||
int status;
|
||||
#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
|
||||
mz_uint file_crc32;
|
||||
#endif
|
||||
mz_uint64 read_buf_size, read_buf_ofs, read_buf_avail, comp_remaining, out_buf_ofs, cur_file_ofs;
|
||||
mz_zip_archive_file_stat file_stat;
|
||||
void *pRead_buf;
|
||||
void *pWrite_buf;
|
||||
|
||||
size_t out_blk_remain;
|
||||
|
||||
tinfl_decompressor inflator;
|
||||
|
||||
} mz_zip_reader_extract_iter_state;
|
||||
|
||||
/* -------- ZIP reading */
|
||||
|
||||
/* Inits a ZIP archive reader. */
|
||||
@@ -221,13 +237,6 @@ MZ_FILE *mz_zip_get_cfile(mz_zip_archive *pZip);
|
||||
/* Reads n bytes of raw archive data, starting at file offset file_ofs, to pBuf. */
|
||||
size_t mz_zip_read_archive_data(mz_zip_archive *pZip, mz_uint64 file_ofs, void *pBuf, size_t n);
|
||||
|
||||
/* Attempts to locates a file in the archive's central directory. */
|
||||
/* Valid flags: MZ_ZIP_FLAG_CASE_SENSITIVE, MZ_ZIP_FLAG_IGNORE_PATH */
|
||||
/* Returns -1 if the file cannot be found. */
|
||||
int mz_zip_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags);
|
||||
/* Returns MZ_FALSE if the file cannot be found. */
|
||||
mz_bool mz_zip_locate_file_v2(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags, mz_uint32 *pIndex);
|
||||
|
||||
/* All mz_zip funcs set the m_last_error field in the mz_zip_archive struct. These functions retrieve/manipulate this field. */
|
||||
/* Note that the m_last_error functionality is not thread safe. */
|
||||
mz_zip_error mz_zip_set_last_error(mz_zip_archive *pZip, mz_zip_error err_num);
|
||||
@@ -285,6 +294,12 @@ void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, const char *pFile
|
||||
mz_bool mz_zip_reader_extract_to_callback(mz_zip_archive *pZip, mz_uint file_index, mz_file_write_func pCallback, void *pOpaque, mz_uint flags);
|
||||
mz_bool mz_zip_reader_extract_file_to_callback(mz_zip_archive *pZip, const char *pFilename, mz_file_write_func pCallback, void *pOpaque, mz_uint flags);
|
||||
|
||||
/* Extract a file iteratively */
|
||||
mz_zip_reader_extract_iter_state* mz_zip_reader_extract_iter_new(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags);
|
||||
mz_zip_reader_extract_iter_state* mz_zip_reader_extract_file_iter_new(mz_zip_archive *pZip, const char *pFilename, mz_uint flags);
|
||||
size_t mz_zip_reader_extract_iter_read(mz_zip_reader_extract_iter_state* pState, void* pvBuf, size_t buf_size);
|
||||
mz_bool mz_zip_reader_extract_iter_free(mz_zip_reader_extract_iter_state* pState);
|
||||
|
||||
#ifndef MINIZ_NO_STDIO
|
||||
/* Extracts a archive file to a disk file and sets its last accessed and modified times. */
|
||||
/* This function only extracts files, not archive directory records. */
|
||||
@@ -326,8 +341,11 @@ mz_bool mz_zip_end(mz_zip_archive *pZip);
|
||||
#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
|
||||
|
||||
/* Inits a ZIP archive writer. */
|
||||
/*Set pZip->m_pWrite (and pZip->m_pIO_opaque) before calling mz_zip_writer_init or mz_zip_writer_init_v2*/
|
||||
/*The output is streamable, i.e. file_ofs in mz_file_write_func always increases only by n*/
|
||||
mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size);
|
||||
mz_bool mz_zip_writer_init_v2(mz_zip_archive *pZip, mz_uint64 existing_size, mz_uint flags);
|
||||
|
||||
mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size);
|
||||
mz_bool mz_zip_writer_init_heap_v2(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size, mz_uint flags);
|
||||
|
||||
|
||||
@@ -14,13 +14,13 @@ Please use the files from the [releases page](https://github.com/richgel999/mini
|
||||
* A drop-in replacement for zlib's most used API's (tested in several open source projects that use zlib, such as libpng and libzip).
|
||||
* Fills a single threaded performance vs. compression ratio gap between several popular real-time compressors and zlib. For example, at level 1, miniz.c compresses around 5-9% better than minilzo, but is approx. 35% slower. At levels 2-9, miniz.c is designed to compare favorably against zlib's ratio and speed. See the miniz performance comparison page for example timings.
|
||||
* Not a block based compressor: miniz.c fully supports stream based processing using a coroutine-style implementation. The zlib-style API functions can be called a single byte at a time if that's all you've got.
|
||||
* Easy to use. The low-level compressor (tinfl) and decompressor (tdefl) have simple state structs which can be saved/restored as needed with simple memcpy's. The low-level codec API's don't use the heap in any way.
|
||||
* Easy to use. The low-level compressor (tdefl) and decompressor (tinfl) have simple state structs which can be saved/restored as needed with simple memcpy's. The low-level codec API's don't use the heap in any way.
|
||||
* Entire inflater (including optional zlib header parsing and Adler-32 checking) is implemented in a single function as a coroutine, which is separately available in a small (~550 line) source file: miniz_tinfl.c
|
||||
* A fairly complete (but totally optional) set of .ZIP archive manipulation and extraction API's. The archive functionality is intended to solve common problems encountered in embedded, mobile, or game development situations. (The archive API's are purposely just powerful enough to write an entire archiver given a bit of additional higher-level logic.)
|
||||
|
||||
## Known Problems
|
||||
|
||||
* No support for encrypted archives. Not sure how useful this stuff is in practice. Zip encryption is very weak.
|
||||
* No support for encrypted archives. Not sure how useful this stuff is in practice.
|
||||
* Minimal documentation. The assumption is that the user is already familiar with the basic zlib API. I need to write an API wiki - for now I've tried to place key comments before each enum/API, and I've included 6 examples that demonstrate how to use the module's major features.
|
||||
|
||||
## Special Thanks
|
||||
|
||||
@@ -993,6 +993,71 @@ static bool zip_extract(const char *pZip_filename, const char *pDst_filename)
|
||||
}
|
||||
free(p);
|
||||
}
|
||||
|
||||
if (stat.m_uncomp_size<100*1024*1024)
|
||||
{
|
||||
/* Use iterative reader to read onto heap in one big chunk */
|
||||
mz_zip_reader_extract_iter_state *pIter = mz_zip_reader_extract_iter_new(&zip, i, 0);
|
||||
void *p = malloc(stat.m_uncomp_size);
|
||||
if ((!pIter) && (0 != stat.m_uncomp_size))
|
||||
{
|
||||
print_error("Failed testing archive -4 \"%s\" err: %s!\n", pZip_filename, mz_zip_get_error_string(mz_zip_get_last_error(&zip)));
|
||||
free(p);
|
||||
mz_zip_reader_end(&zip);
|
||||
return false;
|
||||
}
|
||||
if (pIter)
|
||||
{
|
||||
if (stat.m_uncomp_size != mz_zip_reader_extract_iter_read(pIter, p, stat.m_uncomp_size) )
|
||||
{
|
||||
print_error("Failed testing archive -5 \"%s\" err: %s!\n", pZip_filename, mz_zip_get_error_string(mz_zip_get_last_error(&zip)));
|
||||
free(p);
|
||||
mz_zip_reader_extract_iter_free(pIter);
|
||||
mz_zip_reader_end(&zip);
|
||||
return false;
|
||||
}
|
||||
if (MZ_TRUE != mz_zip_reader_extract_iter_free(pIter))
|
||||
{
|
||||
print_error("Failed testing archive -6 \"%s\" err: %s!\n", pZip_filename, mz_zip_get_error_string(mz_zip_get_last_error(&zip)));
|
||||
free(p);
|
||||
mz_zip_reader_end(&zip);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
free(p);
|
||||
}
|
||||
|
||||
if (stat.m_uncomp_size<100*1024)
|
||||
{
|
||||
/* Use iterative reader to read file one byte at a time */
|
||||
mz_zip_reader_extract_iter_state *pIter = mz_zip_reader_extract_iter_new(&zip, i, 0);
|
||||
uint8_t byBuffer;
|
||||
if ((!pIter) && (0 != stat.m_uncomp_size))
|
||||
{
|
||||
print_error("Failed testing archive -7 \"%s\" err: %s!\n", pZip_filename, mz_zip_get_error_string(mz_zip_get_last_error(&zip)));
|
||||
mz_zip_reader_end(&zip);
|
||||
return false;
|
||||
}
|
||||
if (pIter)
|
||||
{
|
||||
for ( uint64_t uiIndex = 0; uiIndex < stat.m_uncomp_size; uiIndex++ )
|
||||
{
|
||||
if (sizeof(byBuffer) != mz_zip_reader_extract_iter_read(pIter, &byBuffer, sizeof(byBuffer)))
|
||||
{
|
||||
print_error("Failed testing archive -8 \"%s\" err: %s!\n", pZip_filename, mz_zip_get_error_string(mz_zip_get_last_error(&zip)));
|
||||
mz_zip_reader_extract_iter_free(pIter);
|
||||
mz_zip_reader_end(&zip);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (MZ_TRUE != mz_zip_reader_extract_iter_free(pIter))
|
||||
{
|
||||
print_error("Failed testing archive -9 \"%s\" err: %s!\n", pZip_filename, mz_zip_get_error_string(mz_zip_get_last_error(&zip)));
|
||||
mz_zip_reader_end(&zip);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("Verified %u files\n", mz_zip_reader_get_num_files(&zip));
|
||||
|
||||
@@ -1446,6 +1511,15 @@ static bool test_archives(const char *pPath, comp_options options)
|
||||
if (mz_crc32(MZ_CRC32_INIT, (const uint8*)p, extracted_size) != extracted_crc32)
|
||||
status = false;
|
||||
|
||||
mz_zip_reader_extract_iter_state *pIter = mz_zip_reader_extract_file_iter_new(&src_archive, name, 0);
|
||||
void *q = malloc(extracted_size);
|
||||
mz_zip_reader_extract_iter_read(pIter, q, extracted_size);
|
||||
mz_zip_reader_extract_iter_free(pIter);
|
||||
|
||||
if (mz_crc32(MZ_CRC32_INIT, (const uint8*)q, extracted_size) != extracted_crc32)
|
||||
status = false;
|
||||
|
||||
free(q);
|
||||
free(p);
|
||||
|
||||
if (options.m_write_archives)
|
||||
|
||||
Reference in New Issue
Block a user