typedef struct {
ZSTD_CStream* zbc;
int compressionLevel;
- int streamEnd;
+ int streamEnd; /* a flag to signal the end of a stream */
+ unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */
ZSTD_customMem customMem;
z_stream allocFunc; /* copy of zalloc, zfree, opaque */
ZSTD_inBuffer inBuffer;
level = ZWRAP_DEFAULT_CLEVEL;
zwc->streamEnd = 0;
+ zwc->totalInBytes = 0;
zwc->compressionLevel = level;
strm->state = (struct internal_state*) zwc; /* use state which in not used by user */
strm->total_in = 0;
return deflateReset(strm);
{ ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
- if (zwc) zwc->streamEnd = 0;
+ if (zwc) {
+ zwc->streamEnd = 0;
+ zwc->totalInBytes = 0;
+ }
}
strm->total_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->totalInBytes == 0) {
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); }
strm->total_out += zwc->outBuffer.pos;
strm->avail_out -= zwc->outBuffer.pos;
strm->total_in += zwc->inBuffer.pos;
+ zwc->totalInBytes += zwc->inBuffer.pos;
strm->next_in += zwc->inBuffer.pos;
strm->avail_in -= zwc->inBuffer.pos;
}
ZSTD_DStream* zbd;
char headerBuf[16]; /* should be equal or bigger than ZSTD_frameHeaderSize_min */
int errorCount;
+ unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */
ZWRAP_state_t decompState;
ZSTD_inBuffer inBuffer;
ZSTD_outBuffer outBuffer;
strcpy(zwd->version, version);
zwd->stream_size = stream_size;
+ zwd->totalInBytes = 0;
strm->state = (struct internal_state*) zwd; /* use state which in not used by user */
strm->total_in = 0;
strm->total_out = 0;
if (zwd == NULL) return Z_STREAM_ERROR;
ZWRAP_initDCtx(zwd);
zwd->decompState = ZWRAP_useReset;
+ zwd->totalInBytes = 0;
}
strm->total_in = 0;
if (ZSTD_isError(errorCode)) return ZWRAPD_finishWithError(zwd, strm, 0);
zwd->decompState = ZWRAP_useReset;
- if (strm->total_in == ZSTD_HEADERSIZE) {
+ if (zwd->totalInBytes == ZSTD_HEADERSIZE) {
zwd->inBuffer.src = zwd->headerBuf;
- zwd->inBuffer.size = strm->total_in;
+ zwd->inBuffer.size = zwd->totalInBytes;
zwd->inBuffer.pos = 0;
zwd->outBuffer.dst = strm->next_out;
zwd->outBuffer.size = 0;
if (zwd == NULL) return Z_STREAM_ERROR;
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) {
+ if (zwd->totalInBytes < ZLIB_HEADERSIZE) {
+ if (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) {
if (MEM_readLE32(strm->next_in) != ZSTD_MAGICNUMBER) {
if (zwd->windowBits)
errorCode = inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size);
return res;
}
} else {
- srcSize = MIN(strm->avail_in, ZLIB_HEADERSIZE - strm->total_in);
- memcpy(zwd->headerBuf+strm->total_in, strm->next_in, srcSize);
+ 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 (strm->total_in < ZLIB_HEADERSIZE) return Z_OK;
+ if (zwd->totalInBytes < ZLIB_HEADERSIZE) return Z_OK;
if (MEM_readLE32(zwd->headerBuf) != ZSTD_MAGICNUMBER) {
z_stream strm2;
zwd->decompState = ZWRAP_useInit;
}
- if (strm->total_in < ZSTD_HEADERSIZE)
+ if (zwd->totalInBytes < ZSTD_HEADERSIZE)
{
- if (strm->total_in == 0 && strm->avail_in >= 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; }
if (ZSTD_isError(errorCode)) goto error;
}
} else {
- srcSize = MIN(strm->avail_in, ZSTD_HEADERSIZE - strm->total_in);
- memcpy(zwd->headerBuf+strm->total_in, strm->next_in, srcSize);
+ 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 (strm->total_in < ZSTD_HEADERSIZE) return Z_OK;
+ if (zwd->totalInBytes < ZSTD_HEADERSIZE) return Z_OK;
if (zwd->decompState == ZWRAP_useInit) {
errorCode = ZSTD_initDStream(zwd->zbd);
strm->total_out += zwd->outBuffer.pos;
strm->avail_out -= zwd->outBuffer.pos;
strm->total_in += zwd->inBuffer.pos;
+ zwd->totalInBytes += zwd->inBuffer.pos;
strm->next_in += zwd->inBuffer.pos;
strm->avail_in -= zwd->inBuffer.pos;
if (errorCode == 0) {