From 6072eaaa2154930f4d483568622487fa0082b8aa Mon Sep 17 00:00:00 2001 From: inikep Date: Tue, 27 Sep 2016 15:24:44 +0200 Subject: [PATCH 1/6] improved speed of deflate without Z_FINISH --- zlibWrapper/zstd_zlibwrapper.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/zlibWrapper/zstd_zlibwrapper.c b/zlibWrapper/zstd_zlibwrapper.c index 3464c600d..bbb6ff16d 100644 --- a/zlibWrapper/zstd_zlibwrapper.c +++ b/zlibWrapper/zstd_zlibwrapper.c @@ -74,6 +74,7 @@ static void ZWRAP_freeFunction(void* opaque, void* address) /* *** Compression *** */ +typedef enum { ZWRAP_useInit, ZWRAP_useReset } comprState_t; typedef struct { ZSTD_CStream* zbc; @@ -82,7 +83,7 @@ typedef struct { z_stream allocFunc; /* copy of zalloc, zfree, opaque */ ZSTD_inBuffer inBuffer; ZSTD_outBuffer outBuffer; - int comprState; + comprState_t comprState; unsigned long long pledgedSrcSize; } ZWRAP_CCtx; @@ -160,6 +161,7 @@ int ZWRAP_setPledgedSrcSize(z_streamp strm, unsigned long long pledgedSrcSize) if (zwc == NULL) return Z_STREAM_ERROR; zwc->pledgedSrcSize = pledgedSrcSize; + zwc->comprState = ZWRAP_useInit; return Z_OK; } @@ -210,10 +212,6 @@ ZEXTERN int ZEXPORT z_deflateReset OF((z_streamp strm)) strm->total_in = 0; strm->total_out = 0; strm->adler = 0; - - { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state; - if (zwc) zwc->comprState = 0; - } return Z_OK; } @@ -236,7 +234,7 @@ ZEXTERN int ZEXPORT z_deflateSetDictionary OF((z_streamp strm, } { int res = ZWRAP_initializeCStream(zwc, dictionary, dictLength, 0); if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res); } - zwc->comprState = Z_NEED_DICT; + zwc->comprState = ZWRAP_useReset; } return Z_OK; @@ -263,14 +261,16 @@ ZEXTERN int ZEXPORT z_deflate OF((z_streamp strm, int flush)) if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0); res = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : 0); if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res); + if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset; } else { if (strm->total_in == 0) { - if (zwc->comprState == Z_NEED_DICT) { + if (zwc->comprState == ZWRAP_useReset) { size_t const errorCode = ZSTD_resetCStream(zwc->zbc, (flush == Z_FINISH) ? strm->avail_in : zwc->pledgedSrcSize); if (ZSTD_isError(errorCode)) { LOG_WRAPPERC("ERROR: ZSTD_resetCStream errorCode=%s\n", ZSTD_getErrorName(errorCode)); return ZWRAPC_finishWithError(zwc, strm, 0); } } else { int res = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : 0); if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res); + if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset; } } } From 572d428b5964bcae17d303f2c941cced1167c4c6 Mon Sep 17 00:00:00 2001 From: inikep Date: Tue, 27 Sep 2016 15:25:20 +0200 Subject: [PATCH 2/6] updated description of ZWRAP_setPledgedSrcSize --- zlibWrapper/README.md | 2 +- zlibWrapper/zstd_zlibwrapper.h | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/zlibWrapper/README.md b/zlibWrapper/README.md index 163009a8b..427cdbe09 100644 --- a/zlibWrapper/README.md +++ b/zlibWrapper/README.md @@ -85,7 +85,7 @@ During streaming compression the compressor never knows how big is data to compr Zstandard compression can be improved by providing size of source data to the compressor. By default streaming compressor assumes that data is bigger than 256 KB but it can hurt compression speed on smaller data. The zstd wrapper provides the `ZWRAP_setPledgedSrcSize()` function that allows to change a pledged source size for a given compression stream. The function will change zstd compression parameters what may improve compression speed and/or ratio. -It should be called just after `deflateInit()`. The function is only helpful when data is compressed in blocks. There will be no change in case of `deflateInit()` immediately followed by `deflate(strm, Z_FINISH)` +It should be called just after `deflateInit()`or `deflateReset()` and before `deflate()` or `deflateSetDictionary()`. The function is only helpful when data is compressed in blocks. There will be no change in case of `deflateInit()` or `deflateReset()` immediately followed by `deflate(strm, Z_FINISH)` as this case is automatically detected. diff --git a/zlibWrapper/zstd_zlibwrapper.h b/zlibWrapper/zstd_zlibwrapper.h index 5447b3a91..6a9cddc22 100644 --- a/zlibWrapper/zstd_zlibwrapper.h +++ b/zlibWrapper/zstd_zlibwrapper.h @@ -39,8 +39,9 @@ int ZWRAP_isUsingZSTDcompression(void); /* Changes a pledged source size for a given compression stream. It will change ZSTD compression parameters what may improve compression speed and/or ratio. - The function should be called just after deflateInit(). It's only helpful when data is compressed in blocks. - There will be no change in case of deflateInit() immediately followed by deflate(strm, Z_FINISH) + The function should be called just after deflateInit() or deflateReset() and before deflate() or deflateSetDictionary(). + It's only helpful when data is compressed in blocks. + There will be no change in case of deflateInit() or deflateReset() immediately followed by deflate(strm, Z_FINISH) as this case is automatically detected. */ int ZWRAP_setPledgedSrcSize(z_streamp strm, unsigned long long pledgedSrcSize); From 706876f09a0a1ca03997687f45e307081c1c2f7e Mon Sep 17 00:00:00 2001 From: inikep Date: Tue, 27 Sep 2016 16:56:07 +0200 Subject: [PATCH 3/6] added ZWRAP_deflateResetWithoutDict and ZWRAP_inflateResetWithoutDict --- zlibWrapper/examples/zwrapbench.c | 14 ++++-- zlibWrapper/zstd_zlibwrapper.c | 74 ++++++++++++++++++++++--------- zlibWrapper/zstd_zlibwrapper.h | 17 ++++--- 3 files changed, 77 insertions(+), 28 deletions(-) diff --git a/zlibWrapper/examples/zwrapbench.c b/zlibWrapper/examples/zwrapbench.c index 99f91739b..02e6b77d3 100644 --- a/zlibWrapper/examples/zwrapbench.c +++ b/zlibWrapper/examples/zwrapbench.c @@ -282,6 +282,7 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, } else if (compressor == BMK_ZWRAP_ZLIB_REUSE || compressor == BMK_ZWRAP_ZSTD_REUSE || compressor == BMK_ZLIB_REUSE) { z_stream def; int ret; + int useSetDict = (dictBuffer != NULL); if (compressor == BMK_ZLIB_REUSE || compressor == BMK_ZWRAP_ZLIB_REUSE) ZWRAP_useZSTDcompression(0); else ZWRAP_useZSTDcompression(1); def.zalloc = Z_NULL; @@ -296,11 +297,15 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, do { U32 blockNb; for (blockNb=0; blockNbtotal_in = 0; + strm->total_out = 0; + strm->adler = 0; + return Z_OK; +} + + ZEXTERN int ZEXPORT z_deflateReset OF((z_streamp strm)) { LOG_WRAPPERC("- deflateReset\n"); if (!g_ZWRAP_useZSTDcompression) return deflateReset(strm); - strm->total_in = 0; - strm->total_out = 0; - strm->adler = 0; + ZWRAP_deflateResetWithoutDict(strm); + + { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state; + if (zwc) zwc->comprState = 0; + } return Z_OK; } @@ -373,12 +384,13 @@ ZEXTERN int ZEXPORT z_deflateParams OF((z_streamp strm, /* *** Decompression *** */ +typedef enum { ZWRAP_ZLIB_STREAM, ZWRAP_ZSTD_STREAM, ZWRAP_UNKNOWN_STREAM } ZWRAP_stream_type; typedef struct { ZSTD_DStream* zbd; char headerBuf[16]; /* should be equal or bigger than ZSTD_frameHeaderSize_min */ int errorCount; - int decompState; + ZWRAP_state_t decompState; ZSTD_inBuffer inBuffer; ZSTD_outBuffer outBuffer; @@ -391,9 +403,16 @@ typedef struct { } ZWRAP_DCtx; +int ZWRAP_isUsingZSTDdecompression(z_streamp strm) +{ + if (strm == NULL) return 0; + return (strm->reserved == ZWRAP_ZSTD_STREAM); +} + + void ZWRAP_initDCtx(ZWRAP_DCtx* zwd) { - zwd->errorCount = zwd->decompState = 0; + zwd->errorCount = 0; zwd->outBuffer.pos = 0; zwd->outBuffer.size = 0; } @@ -473,7 +492,7 @@ ZEXTERN int ZEXPORT z_inflateInit_ OF((z_streamp strm, strm->state = (struct internal_state*) zwd; /* use state which in not used by user */ strm->total_in = 0; strm->total_out = 0; - strm->reserved = 1; /* mark as unknown steam */ + strm->reserved = ZWRAP_UNKNOWN_STREAM; /* mark as unknown steam */ strm->adler = 0; } @@ -499,13 +518,8 @@ ZEXTERN int ZEXPORT z_inflateInit2_ OF((z_streamp strm, int windowBits, } } - -ZEXTERN int ZEXPORT z_inflateReset OF((z_streamp strm)) +int ZWRAP_inflateResetWithoutDict(z_streamp strm) { - LOG_WRAPPERD("- inflateReset\n"); - if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) - return inflateReset(strm); - { ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state; if (zwd == NULL) return Z_STREAM_ERROR; if (zwd->zbd) { @@ -513,6 +527,7 @@ ZEXTERN int ZEXPORT z_inflateReset OF((z_streamp strm)) if (ZSTD_isError(errorCode)) return ZWRAPD_finishWithError(zwd, strm, 0); } ZWRAP_initDCtx(zwd); + zwd->decompState = ZWRAP_useReset; } strm->total_in = 0; @@ -521,6 +536,23 @@ ZEXTERN int ZEXPORT z_inflateReset OF((z_streamp strm)) } +ZEXTERN int ZEXPORT z_inflateReset OF((z_streamp strm)) +{ + LOG_WRAPPERD("- inflateReset\n"); + if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) + return inflateReset(strm); + + { int ret = ZWRAP_inflateResetWithoutDict(strm); + if (ret != Z_OK) return ret; } + + { ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state; + if (zwd == NULL) return Z_STREAM_ERROR; + zwd->decompState = ZWRAP_useInit; } + + return Z_OK; +} + + #if ZLIB_VERNUM >= 0x1240 ZEXTERN int ZEXPORT z_inflateReset2 OF((z_streamp strm, int windowBits)) @@ -553,7 +585,7 @@ ZEXTERN int ZEXPORT z_inflateSetDictionary OF((z_streamp strm, if (zwd == NULL || zwd->zbd == NULL) return Z_STREAM_ERROR; errorCode = ZSTD_initDStream_usingDict(zwd->zbd, dictionary, dictLength); if (ZSTD_isError(errorCode)) return ZWRAPD_finishWithError(zwd, strm, 0); - zwd->decompState = Z_NEED_DICT; + zwd->decompState = ZWRAP_useReset; if (strm->total_in == ZSTD_HEADERSIZE) { zwd->inBuffer.src = zwd->headerBuf; @@ -593,7 +625,7 @@ ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush)) LOG_WRAPPERD("- inflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d\n", (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out); if (zwd == NULL) return Z_STREAM_ERROR; - if (zwd->decompState == Z_STREAM_END) return Z_STREAM_END; + if (zwd->decompState == ZWRAP_streamEnd) return Z_STREAM_END; if (strm->total_in < ZLIB_HEADERSIZE) { if (strm->total_in == 0 && strm->avail_in >= ZLIB_HEADERSIZE) { @@ -603,7 +635,7 @@ ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush)) else errorCode = inflateInit_(strm, zwd->version, zwd->stream_size); - strm->reserved = 0; /* mark as zlib stream */ + strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */ errorCode = ZWRAP_freeDCtx(zwd); if (ZSTD_isError(errorCode)) goto error; @@ -648,7 +680,7 @@ ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush)) strm->next_out = strm2.next_out; strm->avail_out = strm2.avail_out; - strm->reserved = 0; /* mark as zlib stream */ + strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */ errorCode = ZWRAP_freeDCtx(zwd); if (ZSTD_isError(errorCode)) goto error; @@ -660,6 +692,8 @@ ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush)) } } + strm->reserved = ZWRAP_ZSTD_STREAM; /* mark as zstd steam */ + if (flush == Z_INFLATE_SYNC) { strm->msg = "inflateSync is not supported!"; goto error; } if (!zwd->zbd) { @@ -670,7 +704,7 @@ ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush)) if (strm->total_in < ZSTD_HEADERSIZE) { if (strm->total_in == 0 && strm->avail_in >= ZSTD_HEADERSIZE) { - if (zwd->decompState != Z_NEED_DICT) { + if (zwd->decompState == ZWRAP_useInit) { errorCode = ZSTD_initDStream(zwd->zbd); if (ZSTD_isError(errorCode)) { LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n", ZSTD_getErrorName(errorCode)); goto error; } } @@ -723,7 +757,7 @@ ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush)) strm->avail_in -= zwd->inBuffer.pos; if (errorCode == 0) { LOG_WRAPPERD("inflate Z_STREAM_END1 avail_in=%d avail_out=%d total_in=%d total_out=%d\n", (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out); - zwd->decompState = Z_STREAM_END; + zwd->decompState = ZWRAP_streamEnd; return Z_STREAM_END; } } diff --git a/zlibWrapper/zstd_zlibwrapper.h b/zlibWrapper/zstd_zlibwrapper.h index 6a9cddc22..9df055814 100644 --- a/zlibWrapper/zstd_zlibwrapper.h +++ b/zlibWrapper/zstd_zlibwrapper.h @@ -30,11 +30,11 @@ extern "C" { const char * zstdVersion(void); -/* COMPRESSION */ +/*** COMPRESSION ***/ /* enables/disables zstd compression during runtime */ void ZWRAP_useZSTDcompression(int turn_on); -/* check if zstd compression is turned on */ +/* checks if zstd compression is turned on */ int ZWRAP_isUsingZSTDcompression(void); /* Changes a pledged source size for a given compression stream. @@ -45,18 +45,25 @@ int ZWRAP_isUsingZSTDcompression(void); as this case is automatically detected. */ int ZWRAP_setPledgedSrcSize(z_streamp strm, unsigned long long pledgedSrcSize); +/* similar to deflateReset but preserves dictionary set using deflateSetDictionary */ +int ZWRAP_deflateResetWithoutDict(z_streamp strm); -/* DECOMPRESSION */ + +/*** DECOMPRESSION ***/ typedef enum { ZWRAP_FORCE_ZLIB, ZWRAP_AUTO } ZWRAP_decompress_type; /* enables/disables automatic recognition of zstd/zlib compressed data during runtime */ void ZWRAP_setDecompressionType(ZWRAP_decompress_type type); -/* check zstd decompression type */ +/* checks zstd decompression type */ ZWRAP_decompress_type ZWRAP_getDecompressionType(void); +/* checks if zstd decompression is used for a given stream */ +int ZWRAP_isUsingZSTDdecompression(z_streamp strm); - +/* Similar to inflateReset but preserves dictionary set using inflateSetDictionary. + inflate() will return Z_NEED_DICT only for the first time. */ +int ZWRAP_inflateResetWithoutDict(z_streamp strm); #if defined (__cplusplus) From 856f91ebef775f5d092b9212da5b4fb4440afc53 Mon Sep 17 00:00:00 2001 From: inikep Date: Tue, 27 Sep 2016 17:14:04 +0200 Subject: [PATCH 4/6] redirection to deflateReset and inflateReset --- zlibWrapper/zstd_zlibwrapper.c | 8 ++++++++ zlibWrapper/zstd_zlibwrapper.h | 8 ++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/zlibWrapper/zstd_zlibwrapper.c b/zlibWrapper/zstd_zlibwrapper.c index 0a7995b0b..803d49111 100644 --- a/zlibWrapper/zstd_zlibwrapper.c +++ b/zlibWrapper/zstd_zlibwrapper.c @@ -205,6 +205,10 @@ ZEXTERN int ZEXPORT z_deflateInit2_ OF((z_streamp strm, int level, int method, int ZWRAP_deflateResetWithoutDict(z_streamp strm) { + LOG_WRAPPERC("- ZWRAP_deflateResetWithoutDict\n"); + if (!g_ZWRAP_useZSTDcompression) + return deflateReset(strm); + strm->total_in = 0; strm->total_out = 0; strm->adler = 0; @@ -520,6 +524,10 @@ ZEXTERN int ZEXPORT z_inflateInit2_ OF((z_streamp strm, int windowBits, int ZWRAP_inflateResetWithoutDict(z_streamp strm) { + LOG_WRAPPERD("- ZWRAP_inflateResetWithoutDict\n"); + if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) + return inflateReset(strm); + { ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state; if (zwd == NULL) return Z_STREAM_ERROR; if (zwd->zbd) { diff --git a/zlibWrapper/zstd_zlibwrapper.h b/zlibWrapper/zstd_zlibwrapper.h index 9df055814..f2e4ce265 100644 --- a/zlibWrapper/zstd_zlibwrapper.h +++ b/zlibWrapper/zstd_zlibwrapper.h @@ -45,10 +45,13 @@ int ZWRAP_isUsingZSTDcompression(void); as this case is automatically detected. */ int ZWRAP_setPledgedSrcSize(z_streamp strm, unsigned long long pledgedSrcSize); -/* similar to deflateReset but preserves dictionary set using deflateSetDictionary */ +/* Similar to deflateReset but preserves dictionary set using deflateSetDictionary. + It should improve compression speed because there will be less calls to deflateSetDictionary + When using zlib compression this method redirects to deflateReset. */ int ZWRAP_deflateResetWithoutDict(z_streamp strm); + /*** DECOMPRESSION ***/ typedef enum { ZWRAP_FORCE_ZLIB, ZWRAP_AUTO } ZWRAP_decompress_type; @@ -62,7 +65,8 @@ ZWRAP_decompress_type ZWRAP_getDecompressionType(void); int ZWRAP_isUsingZSTDdecompression(z_streamp strm); /* Similar to inflateReset but preserves dictionary set using inflateSetDictionary. - inflate() will return Z_NEED_DICT only for the first time. */ + inflate() will return Z_NEED_DICT only for the first time what will improve decompression speed. + For zlib streams this method redirects to inflateReset. */ int ZWRAP_inflateResetWithoutDict(z_streamp strm); From 20859afb4c9e2c97fbc843b60e929226b009b1fc Mon Sep 17 00:00:00 2001 From: inikep Date: Tue, 27 Sep 2016 17:27:43 +0200 Subject: [PATCH 5/6] renamed to ZWRAP_deflateReset_keepDict --- zlibWrapper/examples/zwrapbench.c | 6 +++--- zlibWrapper/zstd_zlibwrapper.c | 12 ++++++------ zlibWrapper/zstd_zlibwrapper.h | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/zlibWrapper/examples/zwrapbench.c b/zlibWrapper/examples/zwrapbench.c index 02e6b77d3..d16fcfdd5 100644 --- a/zlibWrapper/examples/zwrapbench.c +++ b/zlibWrapper/examples/zwrapbench.c @@ -298,14 +298,14 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, U32 blockNb; for (blockNb=0; blockNbstate; if (zwc) zwc->comprState = 0; @@ -522,9 +522,9 @@ ZEXTERN int ZEXPORT z_inflateInit2_ OF((z_streamp strm, int windowBits, } } -int ZWRAP_inflateResetWithoutDict(z_streamp strm) +int ZWRAP_inflateReset_keepDict(z_streamp strm) { - LOG_WRAPPERD("- ZWRAP_inflateResetWithoutDict\n"); + LOG_WRAPPERD("- ZWRAP_inflateReset_keepDict\n"); if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) return inflateReset(strm); @@ -550,7 +550,7 @@ ZEXTERN int ZEXPORT z_inflateReset OF((z_streamp strm)) if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) return inflateReset(strm); - { int ret = ZWRAP_inflateResetWithoutDict(strm); + { int ret = ZWRAP_inflateReset_keepDict(strm); if (ret != Z_OK) return ret; } { ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state; diff --git a/zlibWrapper/zstd_zlibwrapper.h b/zlibWrapper/zstd_zlibwrapper.h index f2e4ce265..9abbb7aa2 100644 --- a/zlibWrapper/zstd_zlibwrapper.h +++ b/zlibWrapper/zstd_zlibwrapper.h @@ -48,7 +48,7 @@ int ZWRAP_setPledgedSrcSize(z_streamp strm, unsigned long long pledgedSrcSize); /* Similar to deflateReset but preserves dictionary set using deflateSetDictionary. It should improve compression speed because there will be less calls to deflateSetDictionary When using zlib compression this method redirects to deflateReset. */ -int ZWRAP_deflateResetWithoutDict(z_streamp strm); +int ZWRAP_deflateReset_keepDict(z_streamp strm); @@ -67,7 +67,7 @@ int ZWRAP_isUsingZSTDdecompression(z_streamp strm); /* Similar to inflateReset but preserves dictionary set using inflateSetDictionary. inflate() will return Z_NEED_DICT only for the first time what will improve decompression speed. For zlib streams this method redirects to inflateReset. */ -int ZWRAP_inflateResetWithoutDict(z_streamp strm); +int ZWRAP_inflateReset_keepDict(z_streamp strm); #if defined (__cplusplus) From 22e27300817b672f580f98da875fefdc965af1bb Mon Sep 17 00:00:00 2001 From: inikep Date: Tue, 27 Sep 2016 18:21:17 +0200 Subject: [PATCH 6/6] ZSTD_resetDStream moved to inflate() --- zlibWrapper/zstd_zlibwrapper.c | 18 ++++++++++++------ zlibWrapper/zstd_zlibwrapper.h | 3 ++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/zlibWrapper/zstd_zlibwrapper.c b/zlibWrapper/zstd_zlibwrapper.c index 97f9269a4..31e784a80 100644 --- a/zlibWrapper/zstd_zlibwrapper.c +++ b/zlibWrapper/zstd_zlibwrapper.c @@ -480,6 +480,7 @@ ZEXTERN int ZEXPORT z_inflateInit_ OF((z_streamp strm, const char *version, int stream_size)) { if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) { + strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */ return inflateInit(strm); } @@ -530,10 +531,6 @@ int ZWRAP_inflateReset_keepDict(z_streamp strm) { ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state; if (zwd == NULL) return Z_STREAM_ERROR; - if (zwd->zbd) { - size_t const errorCode = ZSTD_resetDStream(zwd->zbd); - if (ZSTD_isError(errorCode)) return ZWRAPD_finishWithError(zwd, strm, 0); - } ZWRAP_initDCtx(zwd); zwd->decompState = ZWRAP_useReset; } @@ -707,6 +704,7 @@ ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush)) if (!zwd->zbd) { zwd->zbd = ZSTD_createDStream_advanced(zwd->customMem); if (zwd->zbd == NULL) { LOG_WRAPPERD("ERROR: ZSTD_createDStream_advanced\n"); goto error; } + zwd->decompState = ZWRAP_useInit; } if (strm->total_in < ZSTD_HEADERSIZE) @@ -715,6 +713,9 @@ ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush)) if (zwd->decompState == ZWRAP_useInit) { errorCode = ZSTD_initDStream(zwd->zbd); if (ZSTD_isError(errorCode)) { LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n", ZSTD_getErrorName(errorCode)); goto error; } + } else { + errorCode = ZSTD_resetDStream(zwd->zbd); + if (ZSTD_isError(errorCode)) goto error; } } else { srcSize = MIN(strm->avail_in, ZSTD_HEADERSIZE - strm->total_in); @@ -724,8 +725,13 @@ ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush)) strm->avail_in -= srcSize; if (strm->total_in < ZSTD_HEADERSIZE) return Z_OK; - errorCode = ZSTD_initDStream(zwd->zbd); - if (ZSTD_isError(errorCode)) { LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n", ZSTD_getErrorName(errorCode)); goto error; } + if (zwd->decompState == ZWRAP_useInit) { + errorCode = ZSTD_initDStream(zwd->zbd); + if (ZSTD_isError(errorCode)) { LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n", ZSTD_getErrorName(errorCode)); goto error; } + } else { + errorCode = ZSTD_resetDStream(zwd->zbd); + if (ZSTD_isError(errorCode)) goto error; + } zwd->inBuffer.src = zwd->headerBuf; zwd->inBuffer.size = ZSTD_HEADERSIZE; diff --git a/zlibWrapper/zstd_zlibwrapper.h b/zlibWrapper/zstd_zlibwrapper.h index 9abbb7aa2..873413907 100644 --- a/zlibWrapper/zstd_zlibwrapper.h +++ b/zlibWrapper/zstd_zlibwrapper.h @@ -61,7 +61,8 @@ void ZWRAP_setDecompressionType(ZWRAP_decompress_type type); /* checks zstd decompression type */ ZWRAP_decompress_type ZWRAP_getDecompressionType(void); -/* checks if zstd decompression is used for a given stream */ +/* Checks if zstd decompression is used for a given stream. + If will return 1 only when inflate() was called and zstd header was detected. */ int ZWRAP_isUsingZSTDdecompression(z_streamp strm); /* Similar to inflateReset but preserves dictionary set using inflateSetDictionary.