significant zlib wrapper code refactoring

code indentation
variable scope and names
constify

Only coding style changes.
The logic should remain the same.
This commit is contained in:
Yann Collet 2017-06-02 17:10:49 -07:00
parent 4effccbf56
commit 33a7e679e5
3 changed files with 305 additions and 270 deletions

View File

@ -1659,7 +1659,7 @@ size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const
size_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t srcSize) size_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
{ {
#if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE==1) #if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE>=1)
size_t regenSize; size_t regenSize;
ZSTD_DCtx* const dctx = ZSTD_createDCtx(); ZSTD_DCtx* const dctx = ZSTD_createDCtx();
if (dctx==NULL) return ERROR(memory_allocation); if (dctx==NULL) return ERROR(memory_allocation);

View File

@ -18,9 +18,12 @@ EXAMPLE_PATH = examples
PROGRAMS_PATH = ../programs PROGRAMS_PATH = ../programs
TEST_FILE = ../doc/zstd_compression_format.md TEST_FILE = ../doc/zstd_compression_format.md
CPPFLAGS = -DXXH_NAMESPACE=ZSTD_ -I$(ZLIB_PATH) -I$(PROGRAMS_PATH) -I$(ZSTDLIBDIR) -I$(ZSTDLIBDIR)/common -I$(ZLIBWRAPPER_PATH) CPPFLAGS = -DXXH_NAMESPACE=ZSTD_ -I$(ZLIB_PATH) -I$(PROGRAMS_PATH) \
-I$(ZSTDLIBDIR) -I$(ZSTDLIBDIR)/common -I$(ZLIBWRAPPER_PATH)
CFLAGS ?= $(MOREFLAGS) -O3 -std=gnu99 CFLAGS ?= $(MOREFLAGS) -O3 -std=gnu99
CFLAGS += -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow -Wswitch-enum -Wdeclaration-after-statement -Wstrict-prototypes -Wundef -Wstrict-aliasing=1 CFLAGS += -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow -Wswitch-enum \
-Wdeclaration-after-statement -Wstrict-prototypes -Wundef \
-Wstrict-aliasing=1
# Define *.exe as extension for Windows systems # Define *.exe as extension for Windows systems
@ -48,8 +51,8 @@ test: example fitblk example_zstd fitblk_zstd zwrapbench minigzip minigzip_zstd
#cp example$(EXT).gz example$(EXT)_gz.gz #cp example$(EXT).gz example$(EXT)_gz.gz
./minigzip_zstd -d example$(EXT).gz ./minigzip_zstd -d example$(EXT).gz
@echo ---- minigzip end ---- @echo ---- minigzip end ----
./zwrapbench -qb3B1K $(TEST_FILE) ./zwrapbench -qi1b3B1K $(TEST_FILE)
./zwrapbench -rqb1e5 ../lib ../programs ../tests ./zwrapbench -rqi1b1e5 ../lib ../programs ../tests
#valgrindTest: ZSTDLIBRARY = $(ZSTDLIBDIR)/libzstd.so #valgrindTest: ZSTDLIBRARY = $(ZSTDLIBDIR)/libzstd.so
valgrindTest: VALGRIND = LD_LIBRARY_PATH=$(ZSTDLIBDIR) valgrind --track-origins=yes --leak-check=full --error-exitcode=1 valgrindTest: VALGRIND = LD_LIBRARY_PATH=$(ZSTDLIBDIR) valgrind --track-origins=yes --leak-check=full --error-exitcode=1
@ -61,8 +64,8 @@ valgrindTest: clean example fitblk example_zstd fitblk_zstd zwrapbench
$(VALGRIND) ./fitblk 40960 <$(TEST_FILE) $(VALGRIND) ./fitblk 40960 <$(TEST_FILE)
$(VALGRIND) ./fitblk_zstd 10240 <$(TEST_FILE) $(VALGRIND) ./fitblk_zstd 10240 <$(TEST_FILE)
$(VALGRIND) ./fitblk_zstd 40960 <$(TEST_FILE) $(VALGRIND) ./fitblk_zstd 40960 <$(TEST_FILE)
$(VALGRIND) ./zwrapbench -qb3B1K $(TEST_FILE) $(VALGRIND) ./zwrapbench -qi1b3B1K $(TEST_FILE)
$(VALGRIND) ./zwrapbench -rqb1e5 ../lib ../programs ../tests $(VALGRIND) ./zwrapbench -rqi1b1e5 ../lib ../programs ../tests
#.c.o: #.c.o:
# $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $< # $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

View File

@ -8,6 +8,13 @@
*/ */
/* === Tuning parameters === */
#ifndef ZWRAP_USE_ZSTD
#define ZWRAP_USE_ZSTD 0
#endif
/* === Dependencies === */
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> /* vsprintf */ #include <stdio.h> /* vsprintf */
#include <stdarg.h> /* va_list, for z_gzprintf */ #include <stdarg.h> /* va_list, for z_gzprintf */
@ -20,24 +27,23 @@
#include "zstd_internal.h" /* ZSTD_malloc, ZSTD_free */ #include "zstd_internal.h" /* ZSTD_malloc, ZSTD_free */
/* === Constants === */
#define Z_INFLATE_SYNC 8 #define Z_INFLATE_SYNC 8
#define ZLIB_HEADERSIZE 4 #define ZLIB_HEADERSIZE 4
#define ZSTD_HEADERSIZE ZSTD_frameHeaderSize_min #define ZSTD_HEADERSIZE ZSTD_frameHeaderSize_min
#define ZWRAP_DEFAULT_CLEVEL 3 /* Z_DEFAULT_COMPRESSION is translated to ZWRAP_DEFAULT_CLEVEL for zstd */ #define ZWRAP_DEFAULT_CLEVEL 3 /* Z_DEFAULT_COMPRESSION is translated to ZWRAP_DEFAULT_CLEVEL for zstd */
#define LOG_WRAPPERC(...) /* printf(__VA_ARGS__) */
#define LOG_WRAPPERD(...) /* printf(__VA_ARGS__) */ /* === Debug === */
#define LOG_WRAPPERC(...) /* fprintf(stderr, __VA_ARGS__) */
#define LOG_WRAPPERD(...) /* fprintf(stderr, __VA_ARGS__) */
#define FINISH_WITH_GZ_ERR(msg) { (void)msg; return Z_STREAM_ERROR; } #define FINISH_WITH_GZ_ERR(msg) { (void)msg; return Z_STREAM_ERROR; }
#define FINISH_WITH_NULL_ERR(msg) { (void)msg; return NULL; } #define FINISH_WITH_NULL_ERR(msg) { (void)msg; return NULL; }
/* === Wrapper === */
#ifndef ZWRAP_USE_ZSTD static int g_ZWRAP_useZSTDcompression = ZWRAP_USE_ZSTD; /* 0 = don't use ZSTD */
#define ZWRAP_USE_ZSTD 0
#endif
static int g_ZWRAP_useZSTDcompression = ZWRAP_USE_ZSTD; /* 0 = don't use ZSTD */
void ZWRAP_useZSTDcompression(int turn_on) { g_ZWRAP_useZSTDcompression = turn_on; } void ZWRAP_useZSTDcompression(int turn_on) { g_ZWRAP_useZSTDcompression = turn_on; }
@ -63,7 +69,7 @@ static void* ZWRAP_allocFunction(void* opaque, size_t size)
{ {
z_streamp strm = (z_streamp) opaque; z_streamp strm = (z_streamp) opaque;
void* address = strm->zalloc(strm->opaque, 1, (uInt)size); void* address = strm->zalloc(strm->opaque, 1, (uInt)size);
/* printf("ZWRAP alloc %p, %d \n", address, (int)size); */ /* LOG_WRAPPERC("ZWRAP alloc %p, %d \n", address, (int)size); */
return address; return address;
} }
@ -71,12 +77,12 @@ static void ZWRAP_freeFunction(void* opaque, void* address)
{ {
z_streamp strm = (z_streamp) opaque; z_streamp strm = (z_streamp) opaque;
strm->zfree(strm->opaque, address); strm->zfree(strm->opaque, address);
/* if (address) printf("ZWRAP free %p \n", address); */ /* if (address) LOG_WRAPPERC("ZWRAP free %p \n", address); */
} }
/* *** Compression *** */ /* === Compression === */
typedef enum { ZWRAP_useInit, ZWRAP_useReset, ZWRAP_streamEnd } ZWRAP_state_t; typedef enum { ZWRAP_useInit, ZWRAP_useReset, ZWRAP_streamEnd } ZWRAP_state_t;
typedef struct { typedef struct {
@ -96,16 +102,16 @@ typedef ZWRAP_CCtx internal_state;
size_t ZWRAP_freeCCtx(ZWRAP_CCtx* zwc) static size_t ZWRAP_freeCCtx(ZWRAP_CCtx* zwc)
{ {
if (zwc==NULL) return 0; /* support free on NULL */ if (zwc==NULL) return 0; /* support free on NULL */
if (zwc->zbc) ZSTD_freeCStream(zwc->zbc); ZSTD_freeCStream(zwc->zbc);
ZSTD_free(zwc, zwc->customMem); ZSTD_free(zwc, zwc->customMem);
return 0; return 0;
} }
ZWRAP_CCtx* ZWRAP_createCCtx(z_streamp strm) static ZWRAP_CCtx* ZWRAP_createCCtx(z_streamp strm)
{ {
ZWRAP_CCtx* zwc; ZWRAP_CCtx* zwc;
@ -114,9 +120,8 @@ ZWRAP_CCtx* ZWRAP_createCCtx(z_streamp strm)
if (zwc==NULL) return NULL; if (zwc==NULL) return NULL;
memset(zwc, 0, sizeof(ZWRAP_CCtx)); memset(zwc, 0, sizeof(ZWRAP_CCtx));
memcpy(&zwc->allocFunc, strm, sizeof(z_stream)); memcpy(&zwc->allocFunc, strm, sizeof(z_stream));
{ ZSTD_customMem ZWRAP_customMem = { ZWRAP_allocFunction, ZWRAP_freeFunction, &zwc->allocFunc }; { ZSTD_customMem const ZWRAP_customMem = { ZWRAP_allocFunction, ZWRAP_freeFunction, &zwc->allocFunc };
memcpy(&zwc->customMem, &ZWRAP_customMem, sizeof(ZSTD_customMem)); zwc->customMem = ZWRAP_customMem; }
}
} else { } else {
zwc = (ZWRAP_CCtx*)calloc(1, sizeof(*zwc)); zwc = (ZWRAP_CCtx*)calloc(1, sizeof(*zwc));
if (zwc==NULL) return NULL; if (zwc==NULL) return NULL;
@ -126,23 +131,25 @@ ZWRAP_CCtx* ZWRAP_createCCtx(z_streamp strm)
} }
int ZWRAP_initializeCStream(ZWRAP_CCtx* zwc, const void* dict, size_t dictSize, unsigned long long pledgedSrcSize) static int ZWRAP_initializeCStream(ZWRAP_CCtx* zwc, const void* dict, size_t dictSize, unsigned long long pledgedSrcSize)
{ {
LOG_WRAPPERC("- ZWRAP_initializeCStream=%p\n", zwc); LOG_WRAPPERC("- ZWRAP_initializeCStream=%p\n", zwc);
if (zwc == NULL || zwc->zbc == NULL) return Z_STREAM_ERROR; if (zwc == NULL || zwc->zbc == NULL) return Z_STREAM_ERROR;
if (!pledgedSrcSize) pledgedSrcSize = zwc->pledgedSrcSize; if (!pledgedSrcSize) pledgedSrcSize = zwc->pledgedSrcSize;
{ ZSTD_parameters const params = ZSTD_getParams(zwc->compressionLevel, pledgedSrcSize, dictSize); { ZSTD_parameters const params = ZSTD_getParams(zwc->compressionLevel, pledgedSrcSize, dictSize);
size_t errorCode; size_t initErr;
LOG_WRAPPERC("pledgedSrcSize=%d windowLog=%d chainLog=%d hashLog=%d searchLog=%d searchLength=%d strategy=%d\n", (int)pledgedSrcSize, params.cParams.windowLog, params.cParams.chainLog, params.cParams.hashLog, params.cParams.searchLog, params.cParams.searchLength, params.cParams.strategy); LOG_WRAPPERC("pledgedSrcSize=%d windowLog=%d chainLog=%d hashLog=%d searchLog=%d searchLength=%d strategy=%d\n",
errorCode = ZSTD_initCStream_advanced(zwc->zbc, dict, dictSize, params, pledgedSrcSize); (int)pledgedSrcSize, params.cParams.windowLog, params.cParams.chainLog, params.cParams.hashLog, params.cParams.searchLog, params.cParams.searchLength, params.cParams.strategy);
if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR; } initErr = ZSTD_initCStream_advanced(zwc->zbc, dict, dictSize, params, pledgedSrcSize);
if (ZSTD_isError(initErr)) return Z_STREAM_ERROR;
}
return Z_OK; return Z_OK;
} }
int ZWRAPC_finishWithError(ZWRAP_CCtx* zwc, z_streamp strm, int error) static int ZWRAPC_finishWithError(ZWRAP_CCtx* zwc, z_streamp strm, int error)
{ {
LOG_WRAPPERC("- ZWRAPC_finishWithError=%d\n", error); LOG_WRAPPERC("- ZWRAPC_finishWithError=%d\n", error);
if (zwc) ZWRAP_freeCCtx(zwc); if (zwc) ZWRAP_freeCCtx(zwc);
@ -151,7 +158,7 @@ int ZWRAPC_finishWithError(ZWRAP_CCtx* zwc, z_streamp strm, int error)
} }
int ZWRAPC_finishWithErrorMsg(z_streamp strm, char* message) static int ZWRAPC_finishWithErrorMsg(z_streamp strm, char* message)
{ {
ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state; ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
strm->msg = message; strm->msg = message;
@ -276,34 +283,36 @@ ZEXTERN int ZEXPORT z_deflate OF((z_streamp strm, int flush))
ZWRAP_CCtx* zwc; ZWRAP_CCtx* zwc;
if (!g_ZWRAP_useZSTDcompression) { if (!g_ZWRAP_useZSTDcompression) {
int res; LOG_WRAPPERC("- deflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d\n",
LOG_WRAPPERC("- deflate1 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); (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);
res = deflate(strm, flush); return deflate(strm, flush);
return res;
} }
zwc = (ZWRAP_CCtx*) strm->state; zwc = (ZWRAP_CCtx*) strm->state;
if (zwc == NULL) { LOG_WRAPPERC("zwc == NULL\n"); return Z_STREAM_ERROR; } if (zwc == NULL) { LOG_WRAPPERC("zwc == NULL\n"); return Z_STREAM_ERROR; }
if (zwc->zbc == NULL) { if (zwc->zbc == NULL) {
int res;
zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem); zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0); if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
res = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : 0); { int const initErr = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : 0);
if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res); if (initErr != Z_OK) return ZWRAPC_finishWithError(zwc, strm, initErr); }
if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset; if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
} else { } else {
if (zwc->totalInBytes == 0) { if (zwc->totalInBytes == 0) {
if (zwc->comprState == ZWRAP_useReset) { if (zwc->comprState == ZWRAP_useReset) {
size_t const errorCode = ZSTD_resetCStream(zwc->zbc, (flush == Z_FINISH) ? strm->avail_in : zwc->pledgedSrcSize); size_t const resetErr = 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); } if (ZSTD_isError(resetErr)) {
LOG_WRAPPERC("ERROR: ZSTD_resetCStream errorCode=%s\n",
ZSTD_getErrorName(resetErr));
return ZWRAPC_finishWithError(zwc, strm, 0);
}
} else { } else {
int res = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : 0); int const res = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : 0);
if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res); if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res);
if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset; if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
} }
} } /* (zwc->totalInBytes == 0) */
} } /* ! (zwc->zbc == NULL) */
LOG_WRAPPERC("- deflate2 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); LOG_WRAPPERC("- deflate2 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 (strm->avail_in > 0) { if (strm->avail_in > 0) {
@ -313,9 +322,9 @@ ZEXTERN int ZEXPORT z_deflate OF((z_streamp strm, int flush))
zwc->outBuffer.dst = strm->next_out; zwc->outBuffer.dst = strm->next_out;
zwc->outBuffer.size = strm->avail_out; zwc->outBuffer.size = strm->avail_out;
zwc->outBuffer.pos = 0; zwc->outBuffer.pos = 0;
{ size_t const errorCode = ZSTD_compressStream(zwc->zbc, &zwc->outBuffer, &zwc->inBuffer); { size_t const cErr = ZSTD_compressStream(zwc->zbc, &zwc->outBuffer, &zwc->inBuffer);
LOG_WRAPPERC("deflate ZSTD_compressStream srcSize=%d dstCapacity=%d\n", (int)zwc->inBuffer.size, (int)zwc->outBuffer.size); LOG_WRAPPERC("deflate ZSTD_compressStream srcSize=%d dstCapacity=%d\n", (int)zwc->inBuffer.size, (int)zwc->outBuffer.size);
if (ZSTD_isError(errorCode)) return ZWRAPC_finishWithError(zwc, strm, 0); if (ZSTD_isError(cErr)) return ZWRAPC_finishWithError(zwc, strm, 0);
} }
strm->next_out += zwc->outBuffer.pos; strm->next_out += zwc->outBuffer.pos;
strm->total_out += zwc->outBuffer.pos; strm->total_out += zwc->outBuffer.pos;
@ -345,8 +354,12 @@ ZEXTERN int ZEXPORT z_deflate OF((z_streamp strm, int flush))
strm->next_out += zwc->outBuffer.pos; strm->next_out += zwc->outBuffer.pos;
strm->total_out += zwc->outBuffer.pos; strm->total_out += zwc->outBuffer.pos;
strm->avail_out -= zwc->outBuffer.pos; strm->avail_out -= zwc->outBuffer.pos;
if (bytesLeft == 0) { zwc->streamEnd = 1; LOG_WRAPPERC("Z_STREAM_END2 strm->total_in=%d strm->avail_out=%d strm->total_out=%d\n", (int)strm->total_in, (int)strm->avail_out, (int)strm->total_out); return Z_STREAM_END; } if (bytesLeft == 0) {
} zwc->streamEnd = 1;
LOG_WRAPPERC("Z_STREAM_END2 strm->total_in=%d strm->avail_out=%d strm->total_out=%d\n",
(int)strm->total_in, (int)strm->avail_out, (int)strm->total_out);
return Z_STREAM_END;
} }
else else
if (flush == Z_SYNC_FLUSH || flush == Z_PARTIAL_FLUSH) { if (flush == Z_SYNC_FLUSH || flush == Z_PARTIAL_FLUSH) {
size_t bytesLeft; size_t bytesLeft;
@ -409,12 +422,13 @@ ZEXTERN int ZEXPORT z_deflateParams OF((z_streamp strm,
/* *** Decompression *** */ /* === Decompression === */
typedef enum { ZWRAP_ZLIB_STREAM, ZWRAP_ZSTD_STREAM, ZWRAP_UNKNOWN_STREAM } ZWRAP_stream_type; typedef enum { ZWRAP_ZLIB_STREAM, ZWRAP_ZSTD_STREAM, ZWRAP_UNKNOWN_STREAM } ZWRAP_stream_type;
typedef struct { typedef struct {
ZSTD_DStream* zbd; ZSTD_DStream* zbd;
char headerBuf[16]; /* should be equal or bigger than ZSTD_frameHeaderSize_min */ char headerBuf[16]; /* must be >= ZSTD_frameHeaderSize_min */
int errorCount; int errorCount;
unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */ unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */
ZWRAP_state_t decompState; ZWRAP_state_t decompState;
@ -426,10 +440,48 @@ typedef struct {
char *version; char *version;
int windowBits; int windowBits;
ZSTD_customMem customMem; ZSTD_customMem customMem;
z_stream allocFunc; /* copy of zalloc, zfree, opaque */ z_stream allocFunc; /* just to copy zalloc, zfree, opaque */
} ZWRAP_DCtx; } ZWRAP_DCtx;
static void ZWRAP_initDCtx(ZWRAP_DCtx* zwd)
{
zwd->errorCount = 0;
zwd->outBuffer.pos = 0;
zwd->outBuffer.size = 0;
}
static ZWRAP_DCtx* ZWRAP_createDCtx(z_streamp strm)
{
ZWRAP_DCtx* zwd;
MEM_STATIC_ASSERT(sizeof(zwd->headerBuf) >= ZSTD_FRAMEHEADERSIZE_MIN); /* check static buffer size condition */
if (strm->zalloc && strm->zfree) {
zwd = (ZWRAP_DCtx*)strm->zalloc(strm->opaque, 1, sizeof(ZWRAP_DCtx));
if (zwd==NULL) return NULL;
memset(zwd, 0, sizeof(ZWRAP_DCtx));
zwd->allocFunc = *strm; /* just to copy zalloc, zfree & opaque */
{ ZSTD_customMem const ZWRAP_customMem = { ZWRAP_allocFunction, ZWRAP_freeFunction, &zwd->allocFunc };
zwd->customMem = ZWRAP_customMem; }
} else {
zwd = (ZWRAP_DCtx*)calloc(1, sizeof(*zwd));
if (zwd==NULL) return NULL;
}
ZWRAP_initDCtx(zwd);
return zwd;
}
static size_t ZWRAP_freeDCtx(ZWRAP_DCtx* zwd)
{
if (zwd==NULL) return 0; /* support free on null */
ZSTD_freeDStream(zwd->zbd);
ZSTD_free(zwd->version, zwd->customMem);
ZSTD_free(zwd, zwd->customMem);
return 0;
}
int ZWRAP_isUsingZSTDdecompression(z_streamp strm) int ZWRAP_isUsingZSTDdecompression(z_streamp strm)
{ {
if (strm == NULL) return 0; if (strm == NULL) return 0;
@ -437,59 +489,17 @@ int ZWRAP_isUsingZSTDdecompression(z_streamp strm)
} }
void ZWRAP_initDCtx(ZWRAP_DCtx* zwd) static int ZWRAPD_finishWithError(ZWRAP_DCtx* zwd, z_streamp strm, int error)
{
zwd->errorCount = 0;
zwd->outBuffer.pos = 0;
zwd->outBuffer.size = 0;
}
ZWRAP_DCtx* ZWRAP_createDCtx(z_streamp strm)
{
ZWRAP_DCtx* zwd;
if (strm->zalloc && strm->zfree) {
zwd = (ZWRAP_DCtx*)strm->zalloc(strm->opaque, 1, sizeof(ZWRAP_DCtx));
if (zwd==NULL) return NULL;
memset(zwd, 0, sizeof(ZWRAP_DCtx));
memcpy(&zwd->allocFunc, strm, sizeof(z_stream));
{ ZSTD_customMem ZWRAP_customMem = { ZWRAP_allocFunction, ZWRAP_freeFunction, &zwd->allocFunc };
memcpy(&zwd->customMem, &ZWRAP_customMem, sizeof(ZSTD_customMem));
}
} else {
zwd = (ZWRAP_DCtx*)calloc(1, sizeof(*zwd));
if (zwd==NULL) return NULL;
}
MEM_STATIC_ASSERT(sizeof(zwd->headerBuf) >= ZSTD_FRAMEHEADERSIZE_MIN); /* if compilation fails here, assertion is false */
ZWRAP_initDCtx(zwd);
return zwd;
}
size_t ZWRAP_freeDCtx(ZWRAP_DCtx* zwd)
{
if (zwd==NULL) return 0; /* support free on null */
if (zwd->zbd) ZSTD_freeDStream(zwd->zbd);
ZSTD_free(zwd->version, zwd->customMem);
ZSTD_free(zwd, zwd->customMem);
return 0;
}
int ZWRAPD_finishWithError(ZWRAP_DCtx* zwd, z_streamp strm, int error)
{ {
LOG_WRAPPERD("- ZWRAPD_finishWithError=%d\n", error); LOG_WRAPPERD("- ZWRAPD_finishWithError=%d\n", error);
if (zwd) ZWRAP_freeDCtx(zwd); ZWRAP_freeDCtx(zwd);
if (strm) strm->state = NULL; strm->state = NULL;
return (error) ? error : Z_STREAM_ERROR; return (error) ? error : Z_STREAM_ERROR;
} }
static int ZWRAPD_finishWithErrorMsg(z_streamp strm, char* message)
int ZWRAPD_finishWithErrorMsg(z_streamp strm, char* message)
{ {
ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state; ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
strm->msg = message; strm->msg = message;
if (zwd == NULL) return Z_STREAM_ERROR; if (zwd == NULL) return Z_STREAM_ERROR;
@ -501,11 +511,11 @@ ZEXTERN int ZEXPORT z_inflateInit_ OF((z_streamp strm,
const char *version, int stream_size)) const char *version, int stream_size))
{ {
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) { if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */ strm->reserved = ZWRAP_ZLIB_STREAM;
return inflateInit(strm); return inflateInit(strm);
} }
{ ZWRAP_DCtx* zwd = ZWRAP_createDCtx(strm); { ZWRAP_DCtx* const zwd = ZWRAP_createDCtx(strm);
LOG_WRAPPERD("- inflateInit\n"); LOG_WRAPPERD("- inflateInit\n");
if (zwd == NULL) return ZWRAPD_finishWithError(zwd, strm, 0); if (zwd == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
@ -515,10 +525,10 @@ ZEXTERN int ZEXPORT z_inflateInit_ OF((z_streamp strm,
zwd->stream_size = stream_size; zwd->stream_size = stream_size;
zwd->totalInBytes = 0; zwd->totalInBytes = 0;
strm->state = (struct internal_state*) zwd; /* use state which in not used by user */ strm->state = (struct internal_state*) zwd;
strm->total_in = 0; strm->total_in = 0;
strm->total_out = 0; strm->total_out = 0;
strm->reserved = ZWRAP_UNKNOWN_STREAM; /* mark as unknown steam */ strm->reserved = ZWRAP_UNKNOWN_STREAM;
strm->adler = 0; strm->adler = 0;
} }
@ -533,7 +543,7 @@ ZEXTERN int ZEXPORT z_inflateInit2_ OF((z_streamp strm, int windowBits,
return inflateInit2_(strm, windowBits, version, stream_size); return inflateInit2_(strm, windowBits, version, stream_size);
} }
{ int ret = z_inflateInit_ (strm, version, stream_size); { int const ret = z_inflateInit_ (strm, version, stream_size);
LOG_WRAPPERD("- inflateInit2 windowBits=%d\n", windowBits); LOG_WRAPPERD("- inflateInit2 windowBits=%d\n", windowBits);
if (ret == Z_OK) { if (ret == Z_OK) {
ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*)strm->state; ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*)strm->state;
@ -569,10 +579,10 @@ ZEXTERN int ZEXPORT z_inflateReset OF((z_streamp strm))
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
return inflateReset(strm); return inflateReset(strm);
{ int ret = ZWRAP_inflateReset_keepDict(strm); { int const ret = ZWRAP_inflateReset_keepDict(strm);
if (ret != Z_OK) return ret; } if (ret != Z_OK) return ret; }
{ ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state; { ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
if (zwd == NULL) return Z_STREAM_ERROR; if (zwd == NULL) return Z_STREAM_ERROR;
zwd->decompState = ZWRAP_useInit; } zwd->decompState = ZWRAP_useInit; }
@ -587,9 +597,9 @@ ZEXTERN int ZEXPORT z_inflateReset2 OF((z_streamp strm,
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
return inflateReset2(strm, windowBits); return inflateReset2(strm, windowBits);
{ int ret = z_inflateReset (strm); { int const ret = z_inflateReset (strm);
if (ret == Z_OK) { if (ret == Z_OK) {
ZWRAP_DCtx* zwd = (ZWRAP_DCtx*)strm->state; ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*)strm->state;
if (zwd == NULL) return Z_STREAM_ERROR; if (zwd == NULL) return Z_STREAM_ERROR;
zwd->windowBits = windowBits; zwd->windowBits = windowBits;
} }
@ -607,11 +617,10 @@ ZEXTERN int ZEXPORT z_inflateSetDictionary OF((z_streamp strm,
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
return inflateSetDictionary(strm, dictionary, dictLength); return inflateSetDictionary(strm, dictionary, dictLength);
{ size_t errorCode; { ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
if (zwd == NULL || zwd->zbd == NULL) return Z_STREAM_ERROR; if (zwd == NULL || zwd->zbd == NULL) return Z_STREAM_ERROR;
errorCode = ZSTD_initDStream_usingDict(zwd->zbd, dictionary, dictLength); { size_t const initErr = ZSTD_initDStream_usingDict(zwd->zbd, dictionary, dictLength);
if (ZSTD_isError(errorCode)) return ZWRAPD_finishWithError(zwd, strm, 0); if (ZSTD_isError(initErr)) return ZWRAPD_finishWithError(zwd, strm, 0); }
zwd->decompState = ZWRAP_useReset; zwd->decompState = ZWRAP_useReset;
if (zwd->totalInBytes == ZSTD_HEADERSIZE) { if (zwd->totalInBytes == ZSTD_HEADERSIZE) {
@ -621,14 +630,14 @@ ZEXTERN int ZEXPORT z_inflateSetDictionary OF((z_streamp strm,
zwd->outBuffer.dst = strm->next_out; zwd->outBuffer.dst = strm->next_out;
zwd->outBuffer.size = 0; zwd->outBuffer.size = 0;
zwd->outBuffer.pos = 0; zwd->outBuffer.pos = 0;
errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer); { size_t const errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
LOG_WRAPPERD("inflateSetDictionary ZSTD_decompressStream errorCode=%d srcSize=%d dstCapacity=%d\n", (int)errorCode, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size); LOG_WRAPPERD("inflateSetDictionary ZSTD_decompressStream errorCode=%d srcSize=%d dstCapacity=%d\n",
if (zwd->inBuffer.pos < zwd->outBuffer.size || ZSTD_isError(errorCode)) { (int)errorCode, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);
LOG_WRAPPERD("ERROR: ZSTD_decompressStream %s\n", ZSTD_getErrorName(errorCode)); if (zwd->inBuffer.pos < zwd->outBuffer.size || ZSTD_isError(errorCode)) {
return ZWRAPD_finishWithError(zwd, strm, 0); LOG_WRAPPERD("ERROR: ZSTD_decompressStream %s\n",
} ZSTD_getErrorName(errorCode));
} return ZWRAPD_finishWithError(zwd, strm, 0);
} } } } }
return Z_OK; return Z_OK;
} }
@ -637,156 +646,175 @@ ZEXTERN int ZEXPORT z_inflateSetDictionary OF((z_streamp strm,
ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush)) ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush))
{ {
ZWRAP_DCtx* zwd; ZWRAP_DCtx* zwd;
int res;
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) { if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
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); int const result = inflate(strm, flush);
res = inflate(strm, flush); LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n", (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, res); (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, result);
return res; return result;
} }
if (strm->avail_in <= 0) return Z_OK; if (strm->avail_in <= 0) return Z_OK;
{ size_t errorCode, srcSize; zwd = (ZWRAP_DCtx*) strm->state;
zwd = (ZWRAP_DCtx*) strm->state; LOG_WRAPPERD("- inflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d\n",
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); (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 == NULL) return Z_STREAM_ERROR;
if (zwd->decompState == ZWRAP_streamEnd) return Z_STREAM_END; if (zwd->decompState == ZWRAP_streamEnd) return Z_STREAM_END;
if (zwd->totalInBytes < ZLIB_HEADERSIZE) { if (zwd->totalInBytes < ZLIB_HEADERSIZE) {
if (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) { if (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) {
if (MEM_readLE32(strm->next_in) != ZSTD_MAGICNUMBER) { if (MEM_readLE32(strm->next_in) != ZSTD_MAGICNUMBER) {
if (zwd->windowBits) { int const initErr = (zwd->windowBits) ?
errorCode = inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size); inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size) :
else inflateInit_(strm, zwd->version, zwd->stream_size);
errorCode = inflateInit_(strm, zwd->version, zwd->stream_size); LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", initErr);
if (initErr != Z_OK) return ZWRAPD_finishWithError(zwd, strm, initErr);
strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */
errorCode = ZWRAP_freeDCtx(zwd);
if (ZSTD_isError(errorCode)) goto error;
if (flush == Z_INFLATE_SYNC) res = inflateSync(strm);
else res = inflate(strm, flush);
LOG_WRAPPERD("- inflate3 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n", (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, res);
return res;
}
} else {
srcSize = MIN(strm->avail_in, ZLIB_HEADERSIZE - zwd->totalInBytes);
memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);
strm->total_in += srcSize;
zwd->totalInBytes += srcSize;
strm->next_in += srcSize;
strm->avail_in -= srcSize;
if (zwd->totalInBytes < ZLIB_HEADERSIZE) return Z_OK;
if (MEM_readLE32(zwd->headerBuf) != ZSTD_MAGICNUMBER) {
z_stream strm2;
strm2.next_in = strm->next_in;
strm2.avail_in = strm->avail_in;
strm2.next_out = strm->next_out;
strm2.avail_out = strm->avail_out;
if (zwd->windowBits)
errorCode = inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size);
else
errorCode = inflateInit_(strm, zwd->version, zwd->stream_size);
LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", (int)errorCode);
if (errorCode != Z_OK) return ZWRAPD_finishWithError(zwd, strm, (int)errorCode);
/* inflate header */
strm->next_in = (unsigned char*)zwd->headerBuf;
strm->avail_in = ZLIB_HEADERSIZE;
strm->avail_out = 0;
errorCode = inflate(strm, Z_NO_FLUSH);
LOG_WRAPPERD("ZLIB inflate errorCode=%d strm->avail_in=%d\n", (int)errorCode, (int)strm->avail_in);
if (errorCode != Z_OK) return ZWRAPD_finishWithError(zwd, strm, (int)errorCode);
if (strm->avail_in > 0) goto error;
strm->next_in = strm2.next_in;
strm->avail_in = strm2.avail_in;
strm->next_out = strm2.next_out;
strm->avail_out = strm2.avail_out;
strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */
errorCode = ZWRAP_freeDCtx(zwd);
if (ZSTD_isError(errorCode)) goto error;
if (flush == Z_INFLATE_SYNC) res = inflateSync(strm);
else res = inflate(strm, flush);
LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n", (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, res);
return res;
}
}
}
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) {
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 (zwd->totalInBytes < ZSTD_HEADERSIZE)
{
if (zwd->totalInBytes == 0 && strm->avail_in >= ZSTD_HEADERSIZE) {
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 - zwd->totalInBytes);
memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);
strm->total_in += srcSize;
zwd->totalInBytes += srcSize;
strm->next_in += srcSize;
strm->avail_in -= srcSize;
if (zwd->totalInBytes < ZSTD_HEADERSIZE) return Z_OK;
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; strm->reserved = ZWRAP_ZLIB_STREAM;
zwd->inBuffer.size = ZSTD_HEADERSIZE; { size_t const freeErr = ZWRAP_freeDCtx(zwd);
zwd->inBuffer.pos = 0; if (ZSTD_isError(freeErr)) goto error; }
zwd->outBuffer.dst = strm->next_out;
zwd->outBuffer.size = 0; { int const result = (flush == Z_INFLATE_SYNC) ?
zwd->outBuffer.pos = 0; inflateSync(strm) :
errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer); inflate(strm, flush);
LOG_WRAPPERD("inflate ZSTD_decompressStream1 errorCode=%d srcSize=%d dstCapacity=%d\n", (int)errorCode, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size); LOG_WRAPPERD("- inflate3 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
if (ZSTD_isError(errorCode)) { (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, res);
LOG_WRAPPERD("ERROR: ZSTD_decompressStream1 %s\n", ZSTD_getErrorName(errorCode)); return result;
} }
} else { /* ! (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) */
size_t const srcSize = MIN(strm->avail_in, ZLIB_HEADERSIZE - zwd->totalInBytes);
memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);
strm->total_in += srcSize;
zwd->totalInBytes += srcSize;
strm->next_in += srcSize;
strm->avail_in -= srcSize;
if (zwd->totalInBytes < ZLIB_HEADERSIZE) return Z_OK;
if (MEM_readLE32(zwd->headerBuf) != ZSTD_MAGICNUMBER) {
z_stream strm2;
strm2.next_in = strm->next_in;
strm2.avail_in = strm->avail_in;
strm2.next_out = strm->next_out;
strm2.avail_out = strm->avail_out;
{ int const initErr = (zwd->windowBits) ?
inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size) :
inflateInit_(strm, zwd->version, zwd->stream_size);
LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", initErr);
if (initErr != Z_OK) return ZWRAPD_finishWithError(zwd, strm, initErr);
}
/* inflate header */
strm->next_in = (unsigned char*)zwd->headerBuf;
strm->avail_in = ZLIB_HEADERSIZE;
strm->avail_out = 0;
{ int const dErr = inflate(strm, Z_NO_FLUSH);
LOG_WRAPPERD("ZLIB inflate errorCode=%d strm->avail_in=%d\n",
dErr, (int)strm->avail_in);
if (dErr != Z_OK)
return ZWRAPD_finishWithError(zwd, strm, dErr);
}
if (strm->avail_in > 0) goto error;
strm->next_in = strm2.next_in;
strm->avail_in = strm2.avail_in;
strm->next_out = strm2.next_out;
strm->avail_out = strm2.avail_out;
strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */
{ size_t const freeErr = ZWRAP_freeDCtx(zwd);
if (ZSTD_isError(freeErr)) goto error; }
{ int const result = (flush == Z_INFLATE_SYNC) ?
inflateSync(strm) :
inflate(strm, flush);
LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
(int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, res);
return result;
} } } /* if ! (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) */
} /* (zwd->totalInBytes < ZLIB_HEADERSIZE) */
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) {
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 (zwd->totalInBytes < ZSTD_HEADERSIZE) {
if (zwd->totalInBytes == 0 && strm->avail_in >= ZSTD_HEADERSIZE) {
if (zwd->decompState == ZWRAP_useInit) {
size_t const initErr = ZSTD_initDStream(zwd->zbd);
if (ZSTD_isError(initErr)) {
LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n",
ZSTD_getErrorName(initErr));
goto error; goto error;
} }
if (zwd->inBuffer.pos != zwd->inBuffer.size) goto error; /* not consumed */ } else {
size_t const resetErr = ZSTD_resetDStream(zwd->zbd);
if (ZSTD_isError(resetErr)) goto error;
} }
} } else {
size_t const srcSize = MIN(strm->avail_in, ZSTD_HEADERSIZE - zwd->totalInBytes);
memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);
strm->total_in += srcSize;
zwd->totalInBytes += srcSize;
strm->next_in += srcSize;
strm->avail_in -= srcSize;
if (zwd->totalInBytes < ZSTD_HEADERSIZE) return Z_OK;
zwd->inBuffer.src = strm->next_in; if (zwd->decompState == ZWRAP_useInit) {
zwd->inBuffer.size = strm->avail_in; size_t const initErr = ZSTD_initDStream(zwd->zbd);
zwd->inBuffer.pos = 0; if (ZSTD_isError(initErr)) {
zwd->outBuffer.dst = strm->next_out; LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n",
zwd->outBuffer.size = strm->avail_out; ZSTD_getErrorName(initErr));
zwd->outBuffer.pos = 0; goto error;
errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer); }
LOG_WRAPPERD("inflate ZSTD_decompressStream2 errorCode=%d srcSize=%d dstCapacity=%d\n", (int)errorCode, (int)strm->avail_in, (int)strm->avail_out); } else {
if (ZSTD_isError(errorCode)) { size_t const resetErr = ZSTD_resetDStream(zwd->zbd);
if (ZSTD_isError(resetErr)) goto error;
}
zwd->inBuffer.src = zwd->headerBuf;
zwd->inBuffer.size = ZSTD_HEADERSIZE;
zwd->inBuffer.pos = 0;
zwd->outBuffer.dst = strm->next_out;
zwd->outBuffer.size = 0;
zwd->outBuffer.pos = 0;
{ size_t const dErr = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
LOG_WRAPPERD("inflate ZSTD_decompressStream1 errorCode=%d srcSize=%d dstCapacity=%d\n",
(int)dErr, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);
if (ZSTD_isError(dErr)) {
LOG_WRAPPERD("ERROR: ZSTD_decompressStream1 %s\n", ZSTD_getErrorName(dErr));
goto error;
} }
if (zwd->inBuffer.pos != zwd->inBuffer.size) goto error; /* not consumed */
}
} /* (zwd->totalInBytes < ZSTD_HEADERSIZE) */
zwd->inBuffer.src = strm->next_in;
zwd->inBuffer.size = strm->avail_in;
zwd->inBuffer.pos = 0;
zwd->outBuffer.dst = strm->next_out;
zwd->outBuffer.size = strm->avail_out;
zwd->outBuffer.pos = 0;
{ size_t const dErr = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
LOG_WRAPPERD("inflate ZSTD_decompressStream2 errorCode=%d srcSize=%d dstCapacity=%d\n",
(int)dErr, (int)strm->avail_in, (int)strm->avail_out);
if (ZSTD_isError(dErr)) {
zwd->errorCount++; zwd->errorCount++;
LOG_WRAPPERD("ERROR: ZSTD_decompressStream2 %s zwd->errorCount=%d\n", ZSTD_getErrorName(errorCode), zwd->errorCount); LOG_WRAPPERD("ERROR: ZSTD_decompressStream2 %s zwd->errorCount=%d\n",
ZSTD_getErrorName(dErr), zwd->errorCount);
if (zwd->errorCount<=1) return Z_NEED_DICT; else goto error; if (zwd->errorCount<=1) return Z_NEED_DICT; else goto error;
} }
LOG_WRAPPERD("inflate inBuffer.pos=%d inBuffer.size=%d outBuffer.pos=%d outBuffer.size=%d o\n", (int)zwd->inBuffer.pos, (int)zwd->inBuffer.size, (int)zwd->outBuffer.pos, (int)zwd->outBuffer.size); LOG_WRAPPERD("inflate inBuffer.pos=%d inBuffer.size=%d outBuffer.pos=%d outBuffer.size=%d o\n",
(int)zwd->inBuffer.pos, (int)zwd->inBuffer.size, (int)zwd->outBuffer.pos, (int)zwd->outBuffer.size);
strm->next_out += zwd->outBuffer.pos; strm->next_out += zwd->outBuffer.pos;
strm->total_out += zwd->outBuffer.pos; strm->total_out += zwd->outBuffer.pos;
strm->avail_out -= zwd->outBuffer.pos; strm->avail_out -= zwd->outBuffer.pos;
@ -794,13 +822,16 @@ ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush))
zwd->totalInBytes += zwd->inBuffer.pos; zwd->totalInBytes += zwd->inBuffer.pos;
strm->next_in += zwd->inBuffer.pos; strm->next_in += zwd->inBuffer.pos;
strm->avail_in -= zwd->inBuffer.pos; strm->avail_in -= zwd->inBuffer.pos;
if (errorCode == 0) { if (dErr == 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); 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 = ZWRAP_streamEnd; zwd->decompState = ZWRAP_streamEnd;
return Z_STREAM_END; return Z_STREAM_END;
} }
} } /* dErr lifetime */
LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n", (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, Z_OK);
LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
(int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, Z_OK);
return Z_OK; return Z_OK;
error: error:
@ -813,13 +844,13 @@ ZEXTERN int ZEXPORT z_inflateEnd OF((z_streamp strm))
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
return inflateEnd(strm); return inflateEnd(strm);
LOG_WRAPPERD("- inflateEnd total_in=%d total_out=%d\n", (int)(strm->total_in), (int)(strm->total_out)); LOG_WRAPPERD("- inflateEnd total_in=%d total_out=%d\n",
{ size_t errorCode; (int)(strm->total_in), (int)(strm->total_out));
ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state; { ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
if (zwd == NULL) return Z_OK; /* structures are already freed */ if (zwd == NULL) return Z_OK; /* structures are already freed */
{ size_t const freeErr = ZWRAP_freeDCtx(zwd);
if (ZSTD_isError(freeErr)) return Z_STREAM_ERROR; }
strm->state = NULL; strm->state = NULL;
errorCode = ZWRAP_freeDCtx(zwd);
if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
} }
return Z_OK; return Z_OK;
} }
@ -836,8 +867,6 @@ ZEXTERN int ZEXPORT z_inflateSync OF((z_streamp strm))
/* Advanced compression functions */ /* Advanced compression functions */
ZEXTERN int ZEXPORT z_deflateCopy OF((z_streamp dest, ZEXTERN int ZEXPORT z_deflateCopy OF((z_streamp dest,
z_streamp source)) z_streamp source))
@ -977,7 +1006,7 @@ ZEXTERN uLong ZEXPORT z_zlibCompileFlags OF((void)) { return zlibCompileFlags();
/* utility functions */ /* === utility functions === */
#ifndef Z_SOLO #ifndef Z_SOLO
ZEXTERN int ZEXPORT z_compress OF((Bytef *dest, uLongf *destLen, ZEXTERN int ZEXPORT z_compress OF((Bytef *dest, uLongf *destLen,
@ -986,11 +1015,14 @@ ZEXTERN int ZEXPORT z_compress OF((Bytef *dest, uLongf *destLen,
if (!g_ZWRAP_useZSTDcompression) if (!g_ZWRAP_useZSTDcompression)
return compress(dest, destLen, source, sourceLen); return compress(dest, destLen, source, sourceLen);
{ size_t dstCapacity = *destLen; { size_t dstCapacity = *destLen;
size_t const errorCode = ZSTD_compress(dest, dstCapacity, source, sourceLen, ZWRAP_DEFAULT_CLEVEL); size_t const cSize = ZSTD_compress(dest, dstCapacity,
LOG_WRAPPERD("z_compress sourceLen=%d dstCapacity=%d\n", (int)sourceLen, (int)dstCapacity); source, sourceLen,
if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR; ZWRAP_DEFAULT_CLEVEL);
*destLen = errorCode; LOG_WRAPPERD("z_compress sourceLen=%d dstCapacity=%d\n",
(int)sourceLen, (int)dstCapacity);
if (ZSTD_isError(cSize)) return Z_STREAM_ERROR;
*destLen = cSize;
} }
return Z_OK; return Z_OK;
} }