2 * Copyright (c) Meta Platforms, Inc. and affiliates.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
12 /* === Tuning parameters === */
13 #ifndef ZWRAP_USE_ZSTD
14 #define ZWRAP_USE_ZSTD 0
18 /* === Dependencies === */
20 #include <stdio.h> /* vsprintf */
21 #include <stdarg.h> /* va_list, for z_gzprintf */
25 #include <zlib.h> /* without #define Z_PREFIX */
26 #include "zstd_zlibwrapper.h"
27 #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_isFrame, ZSTD_MAGICNUMBER, ZSTD_customMem */
31 /* === Constants === */
32 #define Z_INFLATE_SYNC 8
33 #define ZLIB_HEADERSIZE 4
34 #define ZSTD_HEADERSIZE ZSTD_FRAMEHEADERSIZE_MIN(ZSTD_f_zstd1)
35 #define ZWRAP_DEFAULT_CLEVEL 3 /* Z_DEFAULT_COMPRESSION is translated to ZWRAP_DEFAULT_CLEVEL for zstd */
39 #define LOG_WRAPPERC(...) /* fprintf(stderr, __VA_ARGS__) */
40 #define LOG_WRAPPERD(...) /* fprintf(stderr, __VA_ARGS__) */
42 #define FINISH_WITH_GZ_ERR(msg) { (void)msg; return Z_STREAM_ERROR; }
43 #define FINISH_WITH_NULL_ERR(msg) { (void)msg; return NULL; }
47 #define MIN(x,y) ((x) < (y) ? (x) : (y))
49 static unsigned ZWRAP_isLittleEndian(void)
51 const union { unsigned u; char c[4]; } one = { 1 }; /* don't use static : performance detrimental */
56 # define __has_builtin(x) 0
59 static unsigned ZWRAP_swap32(unsigned in)
61 #if defined(_MSC_VER) /* Visual Studio */
62 return _byteswap_ulong(in);
63 #elif (defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) \
64 || (defined(__clang__) && __has_builtin(__builtin_bswap32))
65 return __builtin_bswap32(in);
67 return ((in << 24) & 0xff000000 ) |
68 ((in << 8) & 0x00ff0000 ) |
69 ((in >> 8) & 0x0000ff00 ) |
70 ((in >> 24) & 0x000000ff );
74 static unsigned ZWRAP_readLE32(const void* ptr)
77 memcpy(&value, ptr, sizeof(value));
78 if (ZWRAP_isLittleEndian())
81 return ZWRAP_swap32(value);
86 static int g_ZWRAP_useZSTDcompression = ZWRAP_USE_ZSTD; /* 0 = don't use ZSTD */
88 void ZWRAP_useZSTDcompression(int turn_on) { g_ZWRAP_useZSTDcompression = turn_on; }
90 int ZWRAP_isUsingZSTDcompression(void) { return g_ZWRAP_useZSTDcompression; }
94 static ZWRAP_decompress_type g_ZWRAPdecompressionType = ZWRAP_AUTO;
96 void ZWRAP_setDecompressionType(ZWRAP_decompress_type type) { g_ZWRAPdecompressionType = type; }
98 ZWRAP_decompress_type ZWRAP_getDecompressionType(void) { return g_ZWRAPdecompressionType; }
102 const char * zstdVersion(void) { return ZSTD_VERSION_STRING; }
104 ZEXTERN const char * ZEXPORT z_zlibVersion _Z_OF((void)) { return zlibVersion(); }
106 static void* ZWRAP_allocFunction(void* opaque, size_t size)
108 z_streamp strm = (z_streamp) opaque;
109 void* address = strm->zalloc(strm->opaque, 1, (uInt)size);
110 /* LOG_WRAPPERC("ZWRAP alloc %p, %d \n", address, (int)size); */
114 static void ZWRAP_freeFunction(void* opaque, void* address)
116 z_streamp strm = (z_streamp) opaque;
117 strm->zfree(strm->opaque, address);
118 /* if (address) LOG_WRAPPERC("ZWRAP free %p \n", address); */
121 static void* ZWRAP_customMalloc(size_t size, ZSTD_customMem customMem)
123 if (customMem.customAlloc)
124 return customMem.customAlloc(customMem.opaque, size);
128 static void* ZWRAP_customCalloc(size_t size, ZSTD_customMem customMem)
130 if (customMem.customAlloc) {
131 /* calloc implemented as malloc+memset;
132 * not as efficient as calloc, but next best guess for custom malloc */
133 void* const ptr = customMem.customAlloc(customMem.opaque, size);
134 memset(ptr, 0, size);
137 return calloc(1, size);
140 static void ZWRAP_customFree(void* ptr, ZSTD_customMem customMem)
143 if (customMem.customFree)
144 customMem.customFree(customMem.opaque, ptr);
152 /* === Compression === */
153 typedef enum { ZWRAP_useInit, ZWRAP_useReset, ZWRAP_streamEnd } ZWRAP_state_t;
157 int compressionLevel;
158 int streamEnd; /* a flag to signal the end of a stream */
159 unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */
160 ZSTD_customMem customMem;
161 z_stream allocFunc; /* copy of zalloc, zfree, opaque */
162 ZSTD_inBuffer inBuffer;
163 ZSTD_outBuffer outBuffer;
164 ZWRAP_state_t comprState;
165 unsigned long long pledgedSrcSize;
168 /* typedef ZWRAP_CCtx internal_state; */
172 static size_t ZWRAP_freeCCtx(ZWRAP_CCtx* zwc)
174 if (zwc==NULL) return 0; /* support free on NULL */
175 ZSTD_freeCStream(zwc->zbc);
176 ZWRAP_customFree(zwc, zwc->customMem);
181 static ZWRAP_CCtx* ZWRAP_createCCtx(z_streamp strm)
184 ZSTD_customMem customMem = { NULL, NULL, NULL };
186 if (strm->zalloc && strm->zfree) {
187 customMem.customAlloc = ZWRAP_allocFunction;
188 customMem.customFree = ZWRAP_freeFunction;
190 customMem.opaque = strm;
192 zwc = (ZWRAP_CCtx*)ZWRAP_customCalloc(sizeof(ZWRAP_CCtx), customMem);
193 if (zwc == NULL) return NULL;
194 zwc->allocFunc = *strm;
195 customMem.opaque = &zwc->allocFunc;
196 zwc->customMem = customMem;
202 static int ZWRAP_initializeCStream(ZWRAP_CCtx* zwc, const void* dict, size_t dictSize, unsigned long long pledgedSrcSize)
204 LOG_WRAPPERC("- ZWRAP_initializeCStream=%p\n", zwc);
205 if (zwc == NULL || zwc->zbc == NULL) return Z_STREAM_ERROR;
207 if (!pledgedSrcSize) pledgedSrcSize = zwc->pledgedSrcSize;
208 { unsigned initErr = 0;
209 ZSTD_parameters const params = ZSTD_getParams(zwc->compressionLevel, pledgedSrcSize, dictSize);
210 ZSTD_CCtx_params* cctxParams = ZSTD_createCCtxParams();
211 if (!cctxParams) return Z_STREAM_ERROR;
212 LOG_WRAPPERC("pledgedSrcSize=%d windowLog=%d chainLog=%d hashLog=%d searchLog=%d minMatch=%d strategy=%d\n",
213 (int)pledgedSrcSize, params.cParams.windowLog, params.cParams.chainLog, params.cParams.hashLog, params.cParams.searchLog, params.cParams.minMatch, params.cParams.strategy);
215 initErr |= ZSTD_isError(ZSTD_CCtx_reset(zwc->zbc, ZSTD_reset_session_only));
216 initErr |= ZSTD_isError(ZSTD_CCtxParams_init_advanced(cctxParams, params));
217 initErr |= ZSTD_isError(ZSTD_CCtx_setParametersUsingCCtxParams(zwc->zbc, cctxParams));
218 initErr |= ZSTD_isError(ZSTD_CCtx_setPledgedSrcSize(zwc->zbc, pledgedSrcSize));
219 initErr |= ZSTD_isError(ZSTD_CCtx_loadDictionary(zwc->zbc, dict, dictSize));
221 ZSTD_freeCCtxParams(cctxParams);
222 if (initErr) return Z_STREAM_ERROR;
229 static int ZWRAPC_finishWithError(ZWRAP_CCtx* zwc, z_streamp strm, int error)
231 LOG_WRAPPERC("- ZWRAPC_finishWithError=%d\n", error);
232 if (zwc) ZWRAP_freeCCtx(zwc);
233 if (strm) strm->state = NULL;
234 return (error) ? error : Z_STREAM_ERROR;
238 static int ZWRAPC_finishWithErrorMsg(z_streamp strm, char* message)
240 ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
242 if (zwc == NULL) return Z_STREAM_ERROR;
244 return ZWRAPC_finishWithError(zwc, strm, 0);
248 int ZWRAP_setPledgedSrcSize(z_streamp strm, unsigned long long pledgedSrcSize)
250 ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
251 if (zwc == NULL) return Z_STREAM_ERROR;
253 zwc->pledgedSrcSize = pledgedSrcSize;
254 zwc->comprState = ZWRAP_useInit;
258 static struct internal_state* convert_into_sis(void* ptr)
260 return (struct internal_state*) ptr;
263 ZEXTERN int ZEXPORT z_deflateInit_ _Z_OF((z_streamp strm, int level,
264 const char *version, int stream_size))
268 LOG_WRAPPERC("- deflateInit level=%d\n", level);
269 if (!g_ZWRAP_useZSTDcompression) {
270 return deflateInit_((strm), (level), version, stream_size);
273 zwc = ZWRAP_createCCtx(strm);
274 if (zwc == NULL) return Z_MEM_ERROR;
276 if (level == Z_DEFAULT_COMPRESSION)
277 level = ZWRAP_DEFAULT_CLEVEL;
280 zwc->totalInBytes = 0;
281 zwc->compressionLevel = level;
282 strm->state = convert_into_sis(zwc); /* use state which in not used by user */
290 ZEXTERN int ZEXPORT z_deflateInit2_ _Z_OF((z_streamp strm, int level, int method,
291 int windowBits, int memLevel,
292 int strategy, const char *version,
295 if (!g_ZWRAP_useZSTDcompression)
296 return deflateInit2_(strm, level, method, windowBits, memLevel, strategy, version, stream_size);
298 return z_deflateInit_ (strm, level, version, stream_size);
302 int ZWRAP_deflateReset_keepDict(z_streamp strm)
304 LOG_WRAPPERC("- ZWRAP_deflateReset_keepDict\n");
305 if (!g_ZWRAP_useZSTDcompression)
306 return deflateReset(strm);
308 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
311 zwc->totalInBytes = 0;
322 ZEXTERN int ZEXPORT z_deflateReset _Z_OF((z_streamp strm))
324 LOG_WRAPPERC("- deflateReset\n");
325 if (!g_ZWRAP_useZSTDcompression)
326 return deflateReset(strm);
328 ZWRAP_deflateReset_keepDict(strm);
330 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
331 if (zwc) zwc->comprState = ZWRAP_useInit;
337 ZEXTERN int ZEXPORT z_deflateSetDictionary _Z_OF((z_streamp strm,
338 const Bytef *dictionary,
341 if (!g_ZWRAP_useZSTDcompression) {
342 LOG_WRAPPERC("- deflateSetDictionary\n");
343 return deflateSetDictionary(strm, dictionary, dictLength);
346 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
347 LOG_WRAPPERC("- deflateSetDictionary level=%d\n", (int)zwc->compressionLevel);
348 if (!zwc) return Z_STREAM_ERROR;
349 if (zwc->zbc == NULL) {
350 zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
351 if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
353 { int res = ZWRAP_initializeCStream(zwc, dictionary, dictLength, ZSTD_CONTENTSIZE_UNKNOWN);
354 if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res); }
355 zwc->comprState = ZWRAP_useReset;
362 ZEXTERN int ZEXPORT z_deflate _Z_OF((z_streamp strm, int flush))
366 if (!g_ZWRAP_useZSTDcompression) {
367 LOG_WRAPPERC("- deflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d\n",
368 (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);
369 return deflate(strm, flush);
372 zwc = (ZWRAP_CCtx*) strm->state;
373 if (zwc == NULL) { LOG_WRAPPERC("zwc == NULL\n"); return Z_STREAM_ERROR; }
375 if (zwc->zbc == NULL) {
376 zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
377 if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
378 { int const initErr = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : ZSTD_CONTENTSIZE_UNKNOWN);
379 if (initErr != Z_OK) return ZWRAPC_finishWithError(zwc, strm, initErr); }
380 if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
382 if (zwc->totalInBytes == 0) {
383 if (zwc->comprState == ZWRAP_useReset) {
384 size_t resetErr = ZSTD_CCtx_reset(zwc->zbc, ZSTD_reset_session_only);
385 if (ZSTD_isError(resetErr)) {
386 LOG_WRAPPERC("ERROR: ZSTD_CCtx_reset errorCode=%s\n",
387 ZSTD_getErrorName(resetErr));
388 return ZWRAPC_finishWithError(zwc, strm, 0);
390 resetErr = ZSTD_CCtx_setPledgedSrcSize(zwc->zbc, (flush == Z_FINISH) ? strm->avail_in : zwc->pledgedSrcSize);
391 if (ZSTD_isError(resetErr)) {
392 LOG_WRAPPERC("ERROR: ZSTD_CCtx_setPledgedSrcSize errorCode=%s\n",
393 ZSTD_getErrorName(resetErr));
394 return ZWRAPC_finishWithError(zwc, strm, 0);
397 int const res = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : ZSTD_CONTENTSIZE_UNKNOWN);
398 if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res);
399 if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
401 } /* (zwc->totalInBytes == 0) */
402 } /* ! (zwc->zbc == NULL) */
404 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);
405 if (strm->avail_in > 0) {
406 zwc->inBuffer.src = strm->next_in;
407 zwc->inBuffer.size = strm->avail_in;
408 zwc->inBuffer.pos = 0;
409 zwc->outBuffer.dst = strm->next_out;
410 zwc->outBuffer.size = strm->avail_out;
411 zwc->outBuffer.pos = 0;
412 { size_t const cErr = ZSTD_compressStream(zwc->zbc, &zwc->outBuffer, &zwc->inBuffer);
413 LOG_WRAPPERC("deflate ZSTD_compressStream srcSize=%d dstCapacity=%d\n", (int)zwc->inBuffer.size, (int)zwc->outBuffer.size);
414 if (ZSTD_isError(cErr)) return ZWRAPC_finishWithError(zwc, strm, 0);
416 strm->next_out += zwc->outBuffer.pos;
417 strm->total_out += zwc->outBuffer.pos;
418 strm->avail_out -= zwc->outBuffer.pos;
419 strm->total_in += zwc->inBuffer.pos;
420 zwc->totalInBytes += zwc->inBuffer.pos;
421 strm->next_in += zwc->inBuffer.pos;
422 strm->avail_in -= zwc->inBuffer.pos;
425 if (flush == Z_FULL_FLUSH
426 #if ZLIB_VERNUM >= 0x1240
430 return ZWRAPC_finishWithErrorMsg(strm, "Z_FULL_FLUSH, Z_BLOCK and Z_TREES are not supported!");
432 if (flush == Z_FINISH) {
434 if (zwc->streamEnd) return Z_STREAM_END;
435 zwc->outBuffer.dst = strm->next_out;
436 zwc->outBuffer.size = strm->avail_out;
437 zwc->outBuffer.pos = 0;
438 bytesLeft = ZSTD_endStream(zwc->zbc, &zwc->outBuffer);
439 LOG_WRAPPERC("deflate ZSTD_endStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
440 if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);
441 strm->next_out += zwc->outBuffer.pos;
442 strm->total_out += zwc->outBuffer.pos;
443 strm->avail_out -= zwc->outBuffer.pos;
444 if (bytesLeft == 0) {
446 LOG_WRAPPERC("Z_STREAM_END2 strm->total_in=%d strm->avail_out=%d strm->total_out=%d\n",
447 (int)strm->total_in, (int)strm->avail_out, (int)strm->total_out);
451 if (flush == Z_SYNC_FLUSH || flush == Z_PARTIAL_FLUSH) {
453 zwc->outBuffer.dst = strm->next_out;
454 zwc->outBuffer.size = strm->avail_out;
455 zwc->outBuffer.pos = 0;
456 bytesLeft = ZSTD_flushStream(zwc->zbc, &zwc->outBuffer);
457 LOG_WRAPPERC("deflate ZSTD_flushStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
458 if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);
459 strm->next_out += zwc->outBuffer.pos;
460 strm->total_out += zwc->outBuffer.pos;
461 strm->avail_out -= zwc->outBuffer.pos;
463 LOG_WRAPPERC("- deflate3 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);
468 ZEXTERN int ZEXPORT z_deflateEnd _Z_OF((z_streamp strm))
470 if (!g_ZWRAP_useZSTDcompression) {
471 LOG_WRAPPERC("- deflateEnd\n");
472 return deflateEnd(strm);
474 LOG_WRAPPERC("- deflateEnd total_in=%d total_out=%d\n", (int)(strm->total_in), (int)(strm->total_out));
476 ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
477 if (zwc == NULL) return Z_OK; /* structures are already freed */
479 errorCode = ZWRAP_freeCCtx(zwc);
480 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
486 ZEXTERN uLong ZEXPORT z_deflateBound _Z_OF((z_streamp strm,
489 if (!g_ZWRAP_useZSTDcompression)
490 return deflateBound(strm, sourceLen);
492 return ZSTD_compressBound(sourceLen);
496 ZEXTERN int ZEXPORT z_deflateParams _Z_OF((z_streamp strm,
500 if (!g_ZWRAP_useZSTDcompression) {
501 LOG_WRAPPERC("- deflateParams level=%d strategy=%d\n", level, strategy);
502 return deflateParams(strm, level, strategy);
512 /* === Decompression === */
514 typedef enum { ZWRAP_ZLIB_STREAM, ZWRAP_ZSTD_STREAM, ZWRAP_UNKNOWN_STREAM } ZWRAP_stream_type;
518 char headerBuf[16]; /* must be >= ZSTD_frameHeaderSize_min */
520 unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */
521 ZWRAP_state_t decompState;
522 ZSTD_inBuffer inBuffer;
523 ZSTD_outBuffer outBuffer;
529 ZSTD_customMem customMem;
530 z_stream allocFunc; /* just to copy zalloc, zfree, opaque */
534 static void ZWRAP_initDCtx(ZWRAP_DCtx* zwd)
537 zwd->outBuffer.pos = 0;
538 zwd->outBuffer.size = 0;
541 static ZWRAP_DCtx* ZWRAP_createDCtx(z_streamp strm)
544 ZSTD_customMem customMem = { NULL, NULL, NULL };
546 if (strm->zalloc && strm->zfree) {
547 customMem.customAlloc = ZWRAP_allocFunction;
548 customMem.customFree = ZWRAP_freeFunction;
550 customMem.opaque = strm;
552 zwd = (ZWRAP_DCtx*)ZWRAP_customCalloc(sizeof(ZWRAP_DCtx), customMem);
553 if (zwd == NULL) return NULL;
554 zwd->allocFunc = *strm;
555 customMem.opaque = &zwd->allocFunc;
556 zwd->customMem = customMem;
562 static size_t ZWRAP_freeDCtx(ZWRAP_DCtx* zwd)
564 if (zwd==NULL) return 0; /* support free on null */
565 ZSTD_freeDStream(zwd->zbd);
566 ZWRAP_customFree(zwd->version, zwd->customMem);
567 ZWRAP_customFree(zwd, zwd->customMem);
572 int ZWRAP_isUsingZSTDdecompression(z_streamp strm)
574 if (strm == NULL) return 0;
575 return (strm->reserved == ZWRAP_ZSTD_STREAM);
579 static int ZWRAPD_finishWithError(ZWRAP_DCtx* zwd, z_streamp strm, int error)
581 LOG_WRAPPERD("- ZWRAPD_finishWithError=%d\n", error);
584 return (error) ? error : Z_STREAM_ERROR;
587 static int ZWRAPD_finishWithErrorMsg(z_streamp strm, char* message)
589 ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
591 if (zwd == NULL) return Z_STREAM_ERROR;
593 return ZWRAPD_finishWithError(zwd, strm, 0);
597 ZEXTERN int ZEXPORT z_inflateInit_ _Z_OF((z_streamp strm,
598 const char* version, int stream_size))
600 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
601 strm->reserved = ZWRAP_ZLIB_STREAM;
602 return inflateInit(strm);
605 { ZWRAP_DCtx* const zwd = ZWRAP_createDCtx(strm);
606 LOG_WRAPPERD("- inflateInit\n");
607 if (zwd == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
609 zwd->version = (char*)ZWRAP_customMalloc(strlen(version)+1, zwd->customMem);
610 if (zwd->version == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
611 strcpy(zwd->version, version);
613 zwd->stream_size = stream_size;
614 zwd->totalInBytes = 0;
615 strm->state = convert_into_sis(zwd);
618 strm->reserved = ZWRAP_UNKNOWN_STREAM;
626 ZEXTERN int ZEXPORT z_inflateInit2_ _Z_OF((z_streamp strm, int windowBits,
627 const char *version, int stream_size))
629 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
630 return inflateInit2_(strm, windowBits, version, stream_size);
633 { int const ret = z_inflateInit_ (strm, version, stream_size);
634 LOG_WRAPPERD("- inflateInit2 windowBits=%d\n", windowBits);
636 ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*)strm->state;
637 if (zwd == NULL) return Z_STREAM_ERROR;
638 zwd->windowBits = windowBits;
644 int ZWRAP_inflateReset_keepDict(z_streamp strm)
646 LOG_WRAPPERD("- ZWRAP_inflateReset_keepDict\n");
647 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
648 return inflateReset(strm);
650 { ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
651 if (zwd == NULL) return Z_STREAM_ERROR;
653 zwd->decompState = ZWRAP_useReset;
654 zwd->totalInBytes = 0;
663 ZEXTERN int ZEXPORT z_inflateReset _Z_OF((z_streamp strm))
665 LOG_WRAPPERD("- inflateReset\n");
666 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
667 return inflateReset(strm);
669 { int const ret = ZWRAP_inflateReset_keepDict(strm);
670 if (ret != Z_OK) return ret; }
672 { ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
673 if (zwd == NULL) return Z_STREAM_ERROR;
674 zwd->decompState = ZWRAP_useInit; }
680 #if ZLIB_VERNUM >= 0x1240
681 ZEXTERN int ZEXPORT z_inflateReset2 _Z_OF((z_streamp strm,
684 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
685 return inflateReset2(strm, windowBits);
687 { int const ret = z_inflateReset (strm);
689 ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*)strm->state;
690 if (zwd == NULL) return Z_STREAM_ERROR;
691 zwd->windowBits = windowBits;
699 ZEXTERN int ZEXPORT z_inflateSetDictionary _Z_OF((z_streamp strm,
700 const Bytef *dictionary,
703 LOG_WRAPPERD("- inflateSetDictionary\n");
704 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
705 return inflateSetDictionary(strm, dictionary, dictLength);
707 { ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
708 if (zwd == NULL || zwd->zbd == NULL) return Z_STREAM_ERROR;
709 { size_t const resetErr = ZSTD_DCtx_reset(zwd->zbd, ZSTD_reset_session_only);
710 if (ZSTD_isError(resetErr)) return ZWRAPD_finishWithError(zwd, strm, 0); }
711 { size_t const loadErr = ZSTD_DCtx_loadDictionary(zwd->zbd, dictionary, dictLength);
712 if (ZSTD_isError(loadErr)) return ZWRAPD_finishWithError(zwd, strm, 0); }
713 zwd->decompState = ZWRAP_useReset;
715 if (zwd->totalInBytes == ZSTD_HEADERSIZE) {
716 zwd->inBuffer.src = zwd->headerBuf;
717 zwd->inBuffer.size = zwd->totalInBytes;
718 zwd->inBuffer.pos = 0;
719 zwd->outBuffer.dst = strm->next_out;
720 zwd->outBuffer.size = 0;
721 zwd->outBuffer.pos = 0;
722 { size_t const errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
723 LOG_WRAPPERD("inflateSetDictionary ZSTD_decompressStream errorCode=%d srcSize=%d dstCapacity=%d\n",
724 (int)errorCode, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);
725 if (zwd->inBuffer.pos < zwd->outBuffer.size || ZSTD_isError(errorCode)) {
726 LOG_WRAPPERD("ERROR: ZSTD_decompressStream %s\n",
727 ZSTD_getErrorName(errorCode));
728 return ZWRAPD_finishWithError(zwd, strm, 0);
735 ZEXTERN int ZEXPORT z_inflate _Z_OF((z_streamp strm, int flush))
739 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
740 int const result = inflate(strm, flush);
741 LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
742 (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, result);
746 if (strm->avail_in <= 0) return Z_OK;
748 zwd = (ZWRAP_DCtx*) strm->state;
749 LOG_WRAPPERD("- inflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d\n",
750 (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);
752 if (zwd == NULL) return Z_STREAM_ERROR;
753 if (zwd->decompState == ZWRAP_streamEnd) return Z_STREAM_END;
755 if (zwd->totalInBytes < ZLIB_HEADERSIZE) {
756 if (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) {
757 if (ZWRAP_readLE32(strm->next_in) != ZSTD_MAGICNUMBER) {
758 { int const initErr = (zwd->windowBits) ?
759 inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size) :
760 inflateInit_(strm, zwd->version, zwd->stream_size);
761 LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", initErr);
762 if (initErr != Z_OK) return ZWRAPD_finishWithError(zwd, strm, initErr);
765 strm->reserved = ZWRAP_ZLIB_STREAM;
766 { size_t const freeErr = ZWRAP_freeDCtx(zwd);
767 if (ZSTD_isError(freeErr)) goto error; }
769 { int const result = (flush == Z_INFLATE_SYNC) ?
771 inflate(strm, flush);
772 LOG_WRAPPERD("- inflate3 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
773 (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, res);
776 } else { /* ! (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) */
777 size_t const srcSize = MIN(strm->avail_in, ZLIB_HEADERSIZE - zwd->totalInBytes);
778 memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);
779 strm->total_in += srcSize;
780 zwd->totalInBytes += srcSize;
781 strm->next_in += srcSize;
782 strm->avail_in -= srcSize;
783 if (zwd->totalInBytes < ZLIB_HEADERSIZE) return Z_OK;
785 if (ZWRAP_readLE32(zwd->headerBuf) != ZSTD_MAGICNUMBER) {
787 strm2.next_in = strm->next_in;
788 strm2.avail_in = strm->avail_in;
789 strm2.next_out = strm->next_out;
790 strm2.avail_out = strm->avail_out;
792 { int const initErr = (zwd->windowBits) ?
793 inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size) :
794 inflateInit_(strm, zwd->version, zwd->stream_size);
795 LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", initErr);
796 if (initErr != Z_OK) return ZWRAPD_finishWithError(zwd, strm, initErr);
800 strm->next_in = (unsigned char*)zwd->headerBuf;
801 strm->avail_in = ZLIB_HEADERSIZE;
803 { int const dErr = inflate(strm, Z_NO_FLUSH);
804 LOG_WRAPPERD("ZLIB inflate errorCode=%d strm->avail_in=%d\n",
805 dErr, (int)strm->avail_in);
807 return ZWRAPD_finishWithError(zwd, strm, dErr);
809 if (strm->avail_in > 0) goto error;
811 strm->next_in = strm2.next_in;
812 strm->avail_in = strm2.avail_in;
813 strm->next_out = strm2.next_out;
814 strm->avail_out = strm2.avail_out;
816 strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */
817 { size_t const freeErr = ZWRAP_freeDCtx(zwd);
818 if (ZSTD_isError(freeErr)) goto error; }
820 { int const result = (flush == Z_INFLATE_SYNC) ?
822 inflate(strm, flush);
823 LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
824 (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, res);
826 } } } /* if ! (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) */
827 } /* (zwd->totalInBytes < ZLIB_HEADERSIZE) */
829 strm->reserved = ZWRAP_ZSTD_STREAM; /* mark as zstd steam */
831 if (flush == Z_INFLATE_SYNC) { strm->msg = "inflateSync is not supported!"; goto error; }
834 zwd->zbd = ZSTD_createDStream_advanced(zwd->customMem);
835 if (zwd->zbd == NULL) { LOG_WRAPPERD("ERROR: ZSTD_createDStream_advanced\n"); goto error; }
836 zwd->decompState = ZWRAP_useInit;
839 if (zwd->totalInBytes < ZSTD_HEADERSIZE) {
840 if (zwd->totalInBytes == 0 && strm->avail_in >= ZSTD_HEADERSIZE) {
841 if (zwd->decompState == ZWRAP_useInit) {
842 size_t const initErr = ZSTD_initDStream(zwd->zbd);
843 if (ZSTD_isError(initErr)) {
844 LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n",
845 ZSTD_getErrorName(initErr));
849 size_t const resetErr = ZSTD_DCtx_reset(zwd->zbd, ZSTD_reset_session_only);
850 if (ZSTD_isError(resetErr)) goto error;
853 size_t const srcSize = MIN(strm->avail_in, ZSTD_HEADERSIZE - zwd->totalInBytes);
854 memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);
855 strm->total_in += srcSize;
856 zwd->totalInBytes += srcSize;
857 strm->next_in += srcSize;
858 strm->avail_in -= srcSize;
859 if (zwd->totalInBytes < ZSTD_HEADERSIZE) return Z_OK;
861 if (zwd->decompState == ZWRAP_useInit) {
862 size_t const initErr = ZSTD_initDStream(zwd->zbd);
863 if (ZSTD_isError(initErr)) {
864 LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n",
865 ZSTD_getErrorName(initErr));
869 size_t const resetErr = ZSTD_DCtx_reset(zwd->zbd, ZSTD_reset_session_only);
870 if (ZSTD_isError(resetErr)) goto error;
873 zwd->inBuffer.src = zwd->headerBuf;
874 zwd->inBuffer.size = ZSTD_HEADERSIZE;
875 zwd->inBuffer.pos = 0;
876 zwd->outBuffer.dst = strm->next_out;
877 zwd->outBuffer.size = 0;
878 zwd->outBuffer.pos = 0;
879 { size_t const dErr = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
880 LOG_WRAPPERD("inflate ZSTD_decompressStream1 errorCode=%d srcSize=%d dstCapacity=%d\n",
881 (int)dErr, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);
882 if (ZSTD_isError(dErr)) {
883 LOG_WRAPPERD("ERROR: ZSTD_decompressStream1 %s\n", ZSTD_getErrorName(dErr));
886 if (zwd->inBuffer.pos != zwd->inBuffer.size) goto error; /* not consumed */
888 } /* (zwd->totalInBytes < ZSTD_HEADERSIZE) */
890 zwd->inBuffer.src = strm->next_in;
891 zwd->inBuffer.size = strm->avail_in;
892 zwd->inBuffer.pos = 0;
893 zwd->outBuffer.dst = strm->next_out;
894 zwd->outBuffer.size = strm->avail_out;
895 zwd->outBuffer.pos = 0;
896 { size_t const dErr = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
897 LOG_WRAPPERD("inflate ZSTD_decompressStream2 errorCode=%d srcSize=%d dstCapacity=%d\n",
898 (int)dErr, (int)strm->avail_in, (int)strm->avail_out);
899 if (ZSTD_isError(dErr)) {
901 LOG_WRAPPERD("ERROR: ZSTD_decompressStream2 %s zwd->errorCount=%d\n",
902 ZSTD_getErrorName(dErr), zwd->errorCount);
903 if (zwd->errorCount<=1) return Z_NEED_DICT; else goto error;
905 LOG_WRAPPERD("inflate inBuffer.pos=%d inBuffer.size=%d outBuffer.pos=%d outBuffer.size=%d o\n",
906 (int)zwd->inBuffer.pos, (int)zwd->inBuffer.size, (int)zwd->outBuffer.pos, (int)zwd->outBuffer.size);
907 strm->next_out += zwd->outBuffer.pos;
908 strm->total_out += zwd->outBuffer.pos;
909 strm->avail_out -= zwd->outBuffer.pos;
910 strm->total_in += zwd->inBuffer.pos;
911 zwd->totalInBytes += zwd->inBuffer.pos;
912 strm->next_in += zwd->inBuffer.pos;
913 strm->avail_in -= zwd->inBuffer.pos;
915 LOG_WRAPPERD("inflate Z_STREAM_END1 avail_in=%d avail_out=%d total_in=%d total_out=%d\n",
916 (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);
917 zwd->decompState = ZWRAP_streamEnd;
920 } /* dErr lifetime */
922 LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
923 (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, Z_OK);
927 return ZWRAPD_finishWithError(zwd, strm, 0);
931 ZEXTERN int ZEXPORT z_inflateEnd _Z_OF((z_streamp strm))
933 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
934 return inflateEnd(strm);
936 LOG_WRAPPERD("- inflateEnd total_in=%d total_out=%d\n",
937 (int)(strm->total_in), (int)(strm->total_out));
938 { ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
939 if (zwd == NULL) return Z_OK; /* structures are already freed */
940 { size_t const freeErr = ZWRAP_freeDCtx(zwd);
941 if (ZSTD_isError(freeErr)) return Z_STREAM_ERROR; }
948 ZEXTERN int ZEXPORT z_inflateSync _Z_OF((z_streamp strm))
950 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
951 return inflateSync(strm);
954 return z_inflate(strm, Z_INFLATE_SYNC);
959 /* Advanced compression functions */
960 ZEXTERN int ZEXPORT z_deflateCopy _Z_OF((z_streamp dest,
963 if (!g_ZWRAP_useZSTDcompression)
964 return deflateCopy(dest, source);
965 return ZWRAPC_finishWithErrorMsg(source, "deflateCopy is not supported!");
969 ZEXTERN int ZEXPORT z_deflateTune _Z_OF((z_streamp strm,
975 if (!g_ZWRAP_useZSTDcompression)
976 return deflateTune(strm, good_length, max_lazy, nice_length, max_chain);
977 return ZWRAPC_finishWithErrorMsg(strm, "deflateTune is not supported!");
981 #if ZLIB_VERNUM >= 0x1260
982 ZEXTERN int ZEXPORT z_deflatePending _Z_OF((z_streamp strm,
986 if (!g_ZWRAP_useZSTDcompression)
987 return deflatePending(strm, pending, bits);
988 return ZWRAPC_finishWithErrorMsg(strm, "deflatePending is not supported!");
993 ZEXTERN int ZEXPORT z_deflatePrime _Z_OF((z_streamp strm,
997 if (!g_ZWRAP_useZSTDcompression)
998 return deflatePrime(strm, bits, value);
999 return ZWRAPC_finishWithErrorMsg(strm, "deflatePrime is not supported!");
1003 ZEXTERN int ZEXPORT z_deflateSetHeader _Z_OF((z_streamp strm,
1006 if (!g_ZWRAP_useZSTDcompression)
1007 return deflateSetHeader(strm, head);
1008 return ZWRAPC_finishWithErrorMsg(strm, "deflateSetHeader is not supported!");
1014 /* Advanced decompression functions */
1015 #if ZLIB_VERNUM >= 0x1280
1016 ZEXTERN int ZEXPORT z_inflateGetDictionary _Z_OF((z_streamp strm,
1020 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1021 return inflateGetDictionary(strm, dictionary, dictLength);
1022 return ZWRAPD_finishWithErrorMsg(strm, "inflateGetDictionary is not supported!");
1027 ZEXTERN int ZEXPORT z_inflateCopy _Z_OF((z_streamp dest,
1030 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !source->reserved)
1031 return inflateCopy(dest, source);
1032 return ZWRAPD_finishWithErrorMsg(source, "inflateCopy is not supported!");
1036 #if ZLIB_VERNUM >= 0x1240
1037 ZEXTERN long ZEXPORT z_inflateMark _Z_OF((z_streamp strm))
1039 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1040 return inflateMark(strm);
1041 return ZWRAPD_finishWithErrorMsg(strm, "inflateMark is not supported!");
1046 ZEXTERN int ZEXPORT z_inflatePrime _Z_OF((z_streamp strm,
1050 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1051 return inflatePrime(strm, bits, value);
1052 return ZWRAPD_finishWithErrorMsg(strm, "inflatePrime is not supported!");
1056 ZEXTERN int ZEXPORT z_inflateGetHeader _Z_OF((z_streamp strm,
1059 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1060 return inflateGetHeader(strm, head);
1061 return ZWRAPD_finishWithErrorMsg(strm, "inflateGetHeader is not supported!");
1065 ZEXTERN int ZEXPORT z_inflateBackInit_ _Z_OF((z_streamp strm, int windowBits,
1066 unsigned char FAR *window,
1067 const char *version,
1070 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1071 return inflateBackInit_(strm, windowBits, window, version, stream_size);
1072 return ZWRAPD_finishWithErrorMsg(strm, "inflateBackInit is not supported!");
1076 ZEXTERN int ZEXPORT z_inflateBack _Z_OF((z_streamp strm,
1077 in_func in, void FAR *in_desc,
1078 out_func out, void FAR *out_desc))
1080 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1081 return inflateBack(strm, in, in_desc, out, out_desc);
1082 return ZWRAPD_finishWithErrorMsg(strm, "inflateBack is not supported!");
1086 ZEXTERN int ZEXPORT z_inflateBackEnd _Z_OF((z_streamp strm))
1088 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1089 return inflateBackEnd(strm);
1090 return ZWRAPD_finishWithErrorMsg(strm, "inflateBackEnd is not supported!");
1094 ZEXTERN uLong ZEXPORT z_zlibCompileFlags _Z_OF((void)) { return zlibCompileFlags(); }
1098 /* === utility functions === */
1101 ZEXTERN int ZEXPORT z_compress _Z_OF((Bytef *dest, uLongf *destLen,
1102 const Bytef *source, uLong sourceLen))
1104 if (!g_ZWRAP_useZSTDcompression)
1105 return compress(dest, destLen, source, sourceLen);
1107 { size_t dstCapacity = *destLen;
1108 size_t const cSize = ZSTD_compress(dest, dstCapacity,
1110 ZWRAP_DEFAULT_CLEVEL);
1111 LOG_WRAPPERD("z_compress sourceLen=%d dstCapacity=%d\n",
1112 (int)sourceLen, (int)dstCapacity);
1113 if (ZSTD_isError(cSize)) return Z_STREAM_ERROR;
1120 ZEXTERN int ZEXPORT z_compress2 _Z_OF((Bytef *dest, uLongf *destLen,
1121 const Bytef *source, uLong sourceLen,
1124 if (!g_ZWRAP_useZSTDcompression)
1125 return compress2(dest, destLen, source, sourceLen, level);
1127 { size_t dstCapacity = *destLen;
1128 size_t const cSize = ZSTD_compress(dest, dstCapacity, source, sourceLen, level);
1129 if (ZSTD_isError(cSize)) return Z_STREAM_ERROR;
1136 ZEXTERN uLong ZEXPORT z_compressBound _Z_OF((uLong sourceLen))
1138 if (!g_ZWRAP_useZSTDcompression)
1139 return compressBound(sourceLen);
1141 return ZSTD_compressBound(sourceLen);
1145 ZEXTERN int ZEXPORT z_uncompress _Z_OF((Bytef *dest, uLongf *destLen,
1146 const Bytef *source, uLong sourceLen))
1148 if (!ZSTD_isFrame(source, sourceLen))
1149 return uncompress(dest, destLen, source, sourceLen);
1151 { size_t dstCapacity = *destLen;
1152 size_t const dSize = ZSTD_decompress(dest, dstCapacity, source, sourceLen);
1153 if (ZSTD_isError(dSize)) return Z_STREAM_ERROR;
1159 #endif /* !Z_SOLO */
1162 /* checksum functions */
1164 ZEXTERN uLong ZEXPORT z_adler32 _Z_OF((uLong adler, const Bytef *buf, uInt len))
1166 return adler32(adler, buf, len);
1169 ZEXTERN uLong ZEXPORT z_crc32 _Z_OF((uLong crc, const Bytef *buf, uInt len))
1171 return crc32(crc, buf, len);
1175 #if ZLIB_VERNUM >= 0x12B0
1176 ZEXTERN uLong ZEXPORT z_adler32_z _Z_OF((uLong adler, const Bytef *buf, z_size_t len))
1178 return adler32_z(adler, buf, len);
1181 ZEXTERN uLong ZEXPORT z_crc32_z _Z_OF((uLong crc, const Bytef *buf, z_size_t len))
1183 return crc32_z(crc, buf, len);
1188 #if ZLIB_VERNUM >= 0x1270
1189 ZEXTERN const z_crc_t FAR * ZEXPORT z_get_crc_table _Z_OF((void))
1191 return get_crc_table();
1195 /* Error function */
1196 ZEXTERN const char * ZEXPORT z_zError _Z_OF((int err))
1198 /* Just use zlib Error function */