2 * Copyright (c) 2016-present, Przemyslaw Skibinski, Facebook, Inc.
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
11 #include <stdio.h> /* vsprintf */
12 #include <stdarg.h> /* va_list, for z_gzprintf */
15 #include <zlib.h> /* without #define Z_PREFIX */
16 #include "zstd_zlibwrapper.h"
17 #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_MAGICNUMBER */
19 #include "zstd_internal.h" /* defaultCustomMem */
22 #define Z_INFLATE_SYNC 8
23 #define ZLIB_HEADERSIZE 4
24 #define ZSTD_HEADERSIZE ZSTD_frameHeaderSize_min
25 #define ZWRAP_DEFAULT_CLEVEL 3 /* Z_DEFAULT_COMPRESSION is translated to ZWRAP_DEFAULT_CLEVEL for zstd */
27 #define LOG_WRAPPERC(...) /* printf(__VA_ARGS__) */
28 #define LOG_WRAPPERD(...) /* printf(__VA_ARGS__) */
30 #define FINISH_WITH_GZ_ERR(msg) { (void)msg; return Z_STREAM_ERROR; }
31 #define FINISH_WITH_NULL_ERR(msg) { (void)msg; return NULL; }
35 #ifndef ZWRAP_USE_ZSTD
36 #define ZWRAP_USE_ZSTD 0
39 static int g_ZWRAP_useZSTDcompression = ZWRAP_USE_ZSTD; /* 0 = don't use ZSTD */
41 void ZWRAP_useZSTDcompression(int turn_on) { g_ZWRAP_useZSTDcompression = turn_on; }
43 int ZWRAP_isUsingZSTDcompression(void) { return g_ZWRAP_useZSTDcompression; }
47 static ZWRAP_decompress_type g_ZWRAPdecompressionType = ZWRAP_AUTO;
49 void ZWRAP_setDecompressionType(ZWRAP_decompress_type type) { g_ZWRAPdecompressionType = type; };
51 ZWRAP_decompress_type ZWRAP_getDecompressionType(void) { return g_ZWRAPdecompressionType; }
55 const char * zstdVersion(void) { return ZSTD_VERSION_STRING; }
57 ZEXTERN const char * ZEXPORT z_zlibVersion OF((void)) { return zlibVersion(); }
61 static void* ZWRAP_allocFunction(void* opaque, size_t size)
63 z_streamp strm = (z_streamp) opaque;
64 void* address = strm->zalloc(strm->opaque, 1, (uInt)size);
65 /* printf("ZWRAP alloc %p, %d \n", address, (int)size); */
69 static void ZWRAP_freeFunction(void* opaque, void* address)
71 z_streamp strm = (z_streamp) opaque;
72 strm->zfree(strm->opaque, address);
73 /* if (address) printf("ZWRAP free %p \n", address); */
78 /* *** Compression *** */
79 typedef enum { ZWRAP_useInit, ZWRAP_useReset, ZWRAP_streamEnd } ZWRAP_state_t;
84 int streamEnd; /* a flag to signal the end of a stream */
85 unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */
86 ZSTD_customMem customMem;
87 z_stream allocFunc; /* copy of zalloc, zfree, opaque */
88 ZSTD_inBuffer inBuffer;
89 ZSTD_outBuffer outBuffer;
90 ZWRAP_state_t comprState;
91 unsigned long long pledgedSrcSize;
94 typedef ZWRAP_CCtx internal_state;
98 size_t ZWRAP_freeCCtx(ZWRAP_CCtx* zwc)
100 if (zwc==NULL) return 0; /* support free on NULL */
101 if (zwc->zbc) ZSTD_freeCStream(zwc->zbc);
102 zwc->customMem.customFree(zwc->customMem.opaque, zwc);
107 ZWRAP_CCtx* ZWRAP_createCCtx(z_streamp strm)
111 if (strm->zalloc && strm->zfree) {
112 zwc = (ZWRAP_CCtx*)strm->zalloc(strm->opaque, 1, sizeof(ZWRAP_CCtx));
113 if (zwc==NULL) return NULL;
114 memset(zwc, 0, sizeof(ZWRAP_CCtx));
115 memcpy(&zwc->allocFunc, strm, sizeof(z_stream));
116 { ZSTD_customMem ZWRAP_customMem = { ZWRAP_allocFunction, ZWRAP_freeFunction, &zwc->allocFunc };
117 memcpy(&zwc->customMem, &ZWRAP_customMem, sizeof(ZSTD_customMem));
120 zwc = (ZWRAP_CCtx*)defaultCustomMem.customAlloc(defaultCustomMem.opaque, sizeof(ZWRAP_CCtx));
121 if (zwc==NULL) return NULL;
122 memset(zwc, 0, sizeof(ZWRAP_CCtx));
123 memcpy(&zwc->customMem, &defaultCustomMem, sizeof(ZSTD_customMem));
130 int ZWRAP_initializeCStream(ZWRAP_CCtx* zwc, const void* dict, size_t dictSize, unsigned long long pledgedSrcSize)
132 LOG_WRAPPERC("- ZWRAP_initializeCStream=%p\n", zwc);
133 if (zwc == NULL || zwc->zbc == NULL) return Z_STREAM_ERROR;
135 if (!pledgedSrcSize) pledgedSrcSize = zwc->pledgedSrcSize;
136 { ZSTD_parameters const params = ZSTD_getParams(zwc->compressionLevel, pledgedSrcSize, dictSize);
138 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);
139 errorCode = ZSTD_initCStream_advanced(zwc->zbc, dict, dictSize, params, pledgedSrcSize);
140 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR; }
146 int ZWRAPC_finishWithError(ZWRAP_CCtx* zwc, z_streamp strm, int error)
148 LOG_WRAPPERC("- ZWRAPC_finishWithError=%d\n", error);
149 if (zwc) ZWRAP_freeCCtx(zwc);
150 if (strm) strm->state = NULL;
151 return (error) ? error : Z_STREAM_ERROR;
155 int ZWRAPC_finishWithErrorMsg(z_streamp strm, char* message)
157 ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
159 if (zwc == NULL) return Z_STREAM_ERROR;
161 return ZWRAPC_finishWithError(zwc, strm, 0);
165 int ZWRAP_setPledgedSrcSize(z_streamp strm, unsigned long long pledgedSrcSize)
167 ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
168 if (zwc == NULL) return Z_STREAM_ERROR;
170 zwc->pledgedSrcSize = pledgedSrcSize;
171 zwc->comprState = ZWRAP_useInit;
176 ZEXTERN int ZEXPORT z_deflateInit_ OF((z_streamp strm, int level,
177 const char *version, int stream_size))
181 LOG_WRAPPERC("- deflateInit level=%d\n", level);
182 if (!g_ZWRAP_useZSTDcompression) {
183 return deflateInit_((strm), (level), version, stream_size);
186 zwc = ZWRAP_createCCtx(strm);
187 if (zwc == NULL) return Z_MEM_ERROR;
189 if (level == Z_DEFAULT_COMPRESSION)
190 level = ZWRAP_DEFAULT_CLEVEL;
193 zwc->totalInBytes = 0;
194 zwc->compressionLevel = level;
195 strm->state = (struct internal_state*) zwc; /* use state which in not used by user */
203 ZEXTERN int ZEXPORT z_deflateInit2_ OF((z_streamp strm, int level, int method,
204 int windowBits, int memLevel,
205 int strategy, const char *version,
208 if (!g_ZWRAP_useZSTDcompression)
209 return deflateInit2_(strm, level, method, windowBits, memLevel, strategy, version, stream_size);
211 return z_deflateInit_ (strm, level, version, stream_size);
215 int ZWRAP_deflateReset_keepDict(z_streamp strm)
217 LOG_WRAPPERC("- ZWRAP_deflateReset_keepDict\n");
218 if (!g_ZWRAP_useZSTDcompression)
219 return deflateReset(strm);
221 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
224 zwc->totalInBytes = 0;
235 ZEXTERN int ZEXPORT z_deflateReset OF((z_streamp strm))
237 LOG_WRAPPERC("- deflateReset\n");
238 if (!g_ZWRAP_useZSTDcompression)
239 return deflateReset(strm);
241 ZWRAP_deflateReset_keepDict(strm);
243 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
244 if (zwc) zwc->comprState = ZWRAP_useInit;
250 ZEXTERN int ZEXPORT z_deflateSetDictionary OF((z_streamp strm,
251 const Bytef *dictionary,
254 if (!g_ZWRAP_useZSTDcompression) {
255 LOG_WRAPPERC("- deflateSetDictionary\n");
256 return deflateSetDictionary(strm, dictionary, dictLength);
259 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
260 LOG_WRAPPERC("- deflateSetDictionary level=%d\n", (int)zwc->compressionLevel);
261 if (!zwc) return Z_STREAM_ERROR;
262 if (zwc->zbc == NULL) {
263 zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
264 if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
266 { int res = ZWRAP_initializeCStream(zwc, dictionary, dictLength, 0);
267 if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res); }
268 zwc->comprState = ZWRAP_useReset;
275 ZEXTERN int ZEXPORT z_deflate OF((z_streamp strm, int flush))
279 if (!g_ZWRAP_useZSTDcompression) {
281 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);
282 res = deflate(strm, flush);
286 zwc = (ZWRAP_CCtx*) strm->state;
287 if (zwc == NULL) { LOG_WRAPPERC("zwc == NULL\n"); return Z_STREAM_ERROR; }
289 if (zwc->zbc == NULL) {
291 zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
292 if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
293 res = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : 0);
294 if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res);
295 if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
297 if (zwc->totalInBytes == 0) {
298 if (zwc->comprState == ZWRAP_useReset) {
299 size_t const errorCode = ZSTD_resetCStream(zwc->zbc, (flush == Z_FINISH) ? strm->avail_in : zwc->pledgedSrcSize);
300 if (ZSTD_isError(errorCode)) { LOG_WRAPPERC("ERROR: ZSTD_resetCStream errorCode=%s\n", ZSTD_getErrorName(errorCode)); return ZWRAPC_finishWithError(zwc, strm, 0); }
302 int res = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : 0);
303 if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res);
304 if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
309 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);
310 if (strm->avail_in > 0) {
311 zwc->inBuffer.src = strm->next_in;
312 zwc->inBuffer.size = strm->avail_in;
313 zwc->inBuffer.pos = 0;
314 zwc->outBuffer.dst = strm->next_out;
315 zwc->outBuffer.size = strm->avail_out;
316 zwc->outBuffer.pos = 0;
317 { size_t const errorCode = ZSTD_compressStream(zwc->zbc, &zwc->outBuffer, &zwc->inBuffer);
318 LOG_WRAPPERC("deflate ZSTD_compressStream srcSize=%d dstCapacity=%d\n", (int)zwc->inBuffer.size, (int)zwc->outBuffer.size);
319 if (ZSTD_isError(errorCode)) return ZWRAPC_finishWithError(zwc, strm, 0);
321 strm->next_out += zwc->outBuffer.pos;
322 strm->total_out += zwc->outBuffer.pos;
323 strm->avail_out -= zwc->outBuffer.pos;
324 strm->total_in += zwc->inBuffer.pos;
325 zwc->totalInBytes += zwc->inBuffer.pos;
326 strm->next_in += zwc->inBuffer.pos;
327 strm->avail_in -= zwc->inBuffer.pos;
330 if (flush == Z_FULL_FLUSH
331 #if ZLIB_VERNUM >= 0x1240
335 return ZWRAPC_finishWithErrorMsg(strm, "Z_FULL_FLUSH, Z_BLOCK and Z_TREES are not supported!");
337 if (flush == Z_FINISH) {
339 if (zwc->streamEnd) return Z_STREAM_END;
340 zwc->outBuffer.dst = strm->next_out;
341 zwc->outBuffer.size = strm->avail_out;
342 zwc->outBuffer.pos = 0;
343 bytesLeft = ZSTD_endStream(zwc->zbc, &zwc->outBuffer);
344 LOG_WRAPPERC("deflate ZSTD_endStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
345 if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);
346 strm->next_out += zwc->outBuffer.pos;
347 strm->total_out += zwc->outBuffer.pos;
348 strm->avail_out -= zwc->outBuffer.pos;
349 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; }
352 if (flush == Z_SYNC_FLUSH || flush == Z_PARTIAL_FLUSH) {
354 zwc->outBuffer.dst = strm->next_out;
355 zwc->outBuffer.size = strm->avail_out;
356 zwc->outBuffer.pos = 0;
357 bytesLeft = ZSTD_flushStream(zwc->zbc, &zwc->outBuffer);
358 LOG_WRAPPERC("deflate ZSTD_flushStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
359 if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);
360 strm->next_out += zwc->outBuffer.pos;
361 strm->total_out += zwc->outBuffer.pos;
362 strm->avail_out -= zwc->outBuffer.pos;
364 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);
369 ZEXTERN int ZEXPORT z_deflateEnd OF((z_streamp strm))
371 if (!g_ZWRAP_useZSTDcompression) {
372 LOG_WRAPPERC("- deflateEnd\n");
373 return deflateEnd(strm);
375 LOG_WRAPPERC("- deflateEnd total_in=%d total_out=%d\n", (int)(strm->total_in), (int)(strm->total_out));
377 ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
378 if (zwc == NULL) return Z_OK; /* structures are already freed */
380 errorCode = ZWRAP_freeCCtx(zwc);
381 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
387 ZEXTERN uLong ZEXPORT z_deflateBound OF((z_streamp strm,
390 if (!g_ZWRAP_useZSTDcompression)
391 return deflateBound(strm, sourceLen);
393 return ZSTD_compressBound(sourceLen);
397 ZEXTERN int ZEXPORT z_deflateParams OF((z_streamp strm,
401 if (!g_ZWRAP_useZSTDcompression) {
402 LOG_WRAPPERC("- deflateParams level=%d strategy=%d\n", level, strategy);
403 return deflateParams(strm, level, strategy);
413 /* *** Decompression *** */
414 typedef enum { ZWRAP_ZLIB_STREAM, ZWRAP_ZSTD_STREAM, ZWRAP_UNKNOWN_STREAM } ZWRAP_stream_type;
418 char headerBuf[16]; /* should be equal or bigger than ZSTD_frameHeaderSize_min */
420 unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */
421 ZWRAP_state_t decompState;
422 ZSTD_inBuffer inBuffer;
423 ZSTD_outBuffer outBuffer;
429 ZSTD_customMem customMem;
430 z_stream allocFunc; /* copy of zalloc, zfree, opaque */
434 int ZWRAP_isUsingZSTDdecompression(z_streamp strm)
436 if (strm == NULL) return 0;
437 return (strm->reserved == ZWRAP_ZSTD_STREAM);
441 void ZWRAP_initDCtx(ZWRAP_DCtx* zwd)
444 zwd->outBuffer.pos = 0;
445 zwd->outBuffer.size = 0;
449 ZWRAP_DCtx* ZWRAP_createDCtx(z_streamp strm)
453 if (strm->zalloc && strm->zfree) {
454 zwd = (ZWRAP_DCtx*)strm->zalloc(strm->opaque, 1, sizeof(ZWRAP_DCtx));
455 if (zwd==NULL) return NULL;
456 memset(zwd, 0, sizeof(ZWRAP_DCtx));
457 memcpy(&zwd->allocFunc, strm, sizeof(z_stream));
458 { ZSTD_customMem ZWRAP_customMem = { ZWRAP_allocFunction, ZWRAP_freeFunction, &zwd->allocFunc };
459 memcpy(&zwd->customMem, &ZWRAP_customMem, sizeof(ZSTD_customMem));
462 zwd = (ZWRAP_DCtx*)defaultCustomMem.customAlloc(defaultCustomMem.opaque, sizeof(ZWRAP_DCtx));
463 if (zwd==NULL) return NULL;
464 memset(zwd, 0, sizeof(ZWRAP_DCtx));
465 memcpy(&zwd->customMem, &defaultCustomMem, sizeof(ZSTD_customMem));
468 MEM_STATIC_ASSERT(sizeof(zwd->headerBuf) >= ZSTD_FRAMEHEADERSIZE_MIN); /* if compilation fails here, assertion is false */
474 size_t ZWRAP_freeDCtx(ZWRAP_DCtx* zwd)
476 if (zwd==NULL) return 0; /* support free on null */
477 if (zwd->zbd) ZSTD_freeDStream(zwd->zbd);
478 if (zwd->version) zwd->customMem.customFree(zwd->customMem.opaque, zwd->version);
479 zwd->customMem.customFree(zwd->customMem.opaque, zwd);
484 int ZWRAPD_finishWithError(ZWRAP_DCtx* zwd, z_streamp strm, int error)
486 LOG_WRAPPERD("- ZWRAPD_finishWithError=%d\n", error);
487 if (zwd) ZWRAP_freeDCtx(zwd);
488 if (strm) strm->state = NULL;
489 return (error) ? error : Z_STREAM_ERROR;
493 int ZWRAPD_finishWithErrorMsg(z_streamp strm, char* message)
495 ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
497 if (zwd == NULL) return Z_STREAM_ERROR;
499 return ZWRAPD_finishWithError(zwd, strm, 0);
503 ZEXTERN int ZEXPORT z_inflateInit_ OF((z_streamp strm,
504 const char *version, int stream_size))
506 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
507 strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */
508 return inflateInit(strm);
512 ZWRAP_DCtx* zwd = ZWRAP_createDCtx(strm);
513 LOG_WRAPPERD("- inflateInit\n");
514 if (zwd == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
516 zwd->version = zwd->customMem.customAlloc(zwd->customMem.opaque, strlen(version) + 1);
517 if (zwd->version == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
518 strcpy(zwd->version, version);
520 zwd->stream_size = stream_size;
521 zwd->totalInBytes = 0;
522 strm->state = (struct internal_state*) zwd; /* use state which in not used by user */
525 strm->reserved = ZWRAP_UNKNOWN_STREAM; /* mark as unknown steam */
533 ZEXTERN int ZEXPORT z_inflateInit2_ OF((z_streamp strm, int windowBits,
534 const char *version, int stream_size))
536 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
537 return inflateInit2_(strm, windowBits, version, stream_size);
541 int ret = z_inflateInit_ (strm, version, stream_size);
542 LOG_WRAPPERD("- inflateInit2 windowBits=%d\n", windowBits);
544 ZWRAP_DCtx* zwd = (ZWRAP_DCtx*)strm->state;
545 if (zwd == NULL) return Z_STREAM_ERROR;
546 zwd->windowBits = windowBits;
552 int ZWRAP_inflateReset_keepDict(z_streamp strm)
554 LOG_WRAPPERD("- ZWRAP_inflateReset_keepDict\n");
555 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
556 return inflateReset(strm);
558 { ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
559 if (zwd == NULL) return Z_STREAM_ERROR;
561 zwd->decompState = ZWRAP_useReset;
562 zwd->totalInBytes = 0;
571 ZEXTERN int ZEXPORT z_inflateReset OF((z_streamp strm))
573 LOG_WRAPPERD("- inflateReset\n");
574 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
575 return inflateReset(strm);
577 { int ret = ZWRAP_inflateReset_keepDict(strm);
578 if (ret != Z_OK) return ret; }
580 { ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
581 if (zwd == NULL) return Z_STREAM_ERROR;
582 zwd->decompState = ZWRAP_useInit; }
588 #if ZLIB_VERNUM >= 0x1240
589 ZEXTERN int ZEXPORT z_inflateReset2 OF((z_streamp strm,
592 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
593 return inflateReset2(strm, windowBits);
595 { int ret = z_inflateReset (strm);
597 ZWRAP_DCtx* zwd = (ZWRAP_DCtx*)strm->state;
598 if (zwd == NULL) return Z_STREAM_ERROR;
599 zwd->windowBits = windowBits;
607 ZEXTERN int ZEXPORT z_inflateSetDictionary OF((z_streamp strm,
608 const Bytef *dictionary,
611 LOG_WRAPPERD("- inflateSetDictionary\n");
612 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
613 return inflateSetDictionary(strm, dictionary, dictLength);
616 ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
617 if (zwd == NULL || zwd->zbd == NULL) return Z_STREAM_ERROR;
618 errorCode = ZSTD_initDStream_usingDict(zwd->zbd, dictionary, dictLength);
619 if (ZSTD_isError(errorCode)) return ZWRAPD_finishWithError(zwd, strm, 0);
620 zwd->decompState = ZWRAP_useReset;
622 if (zwd->totalInBytes == ZSTD_HEADERSIZE) {
623 zwd->inBuffer.src = zwd->headerBuf;
624 zwd->inBuffer.size = zwd->totalInBytes;
625 zwd->inBuffer.pos = 0;
626 zwd->outBuffer.dst = strm->next_out;
627 zwd->outBuffer.size = 0;
628 zwd->outBuffer.pos = 0;
629 errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
630 LOG_WRAPPERD("inflateSetDictionary ZSTD_decompressStream errorCode=%d srcSize=%d dstCapacity=%d\n", (int)errorCode, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);
631 if (zwd->inBuffer.pos < zwd->outBuffer.size || ZSTD_isError(errorCode)) {
632 LOG_WRAPPERD("ERROR: ZSTD_decompressStream %s\n", ZSTD_getErrorName(errorCode));
633 return ZWRAPD_finishWithError(zwd, strm, 0);
642 ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush))
646 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
647 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);
648 res = inflate(strm, flush);
649 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);
653 if (strm->avail_in <= 0) return Z_OK;
655 { size_t errorCode, srcSize;
656 zwd = (ZWRAP_DCtx*) strm->state;
657 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);
659 if (zwd == NULL) return Z_STREAM_ERROR;
660 if (zwd->decompState == ZWRAP_streamEnd) return Z_STREAM_END;
662 if (zwd->totalInBytes < ZLIB_HEADERSIZE) {
663 if (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) {
664 if (MEM_readLE32(strm->next_in) != ZSTD_MAGICNUMBER) {
666 errorCode = inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size);
668 errorCode = inflateInit_(strm, zwd->version, zwd->stream_size);
670 strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */
671 errorCode = ZWRAP_freeDCtx(zwd);
672 if (ZSTD_isError(errorCode)) goto error;
674 if (flush == Z_INFLATE_SYNC) res = inflateSync(strm);
675 else res = inflate(strm, flush);
676 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);
680 srcSize = MIN(strm->avail_in, ZLIB_HEADERSIZE - zwd->totalInBytes);
681 memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);
682 strm->total_in += srcSize;
683 zwd->totalInBytes += srcSize;
684 strm->next_in += srcSize;
685 strm->avail_in -= srcSize;
686 if (zwd->totalInBytes < ZLIB_HEADERSIZE) return Z_OK;
688 if (MEM_readLE32(zwd->headerBuf) != ZSTD_MAGICNUMBER) {
690 strm2.next_in = strm->next_in;
691 strm2.avail_in = strm->avail_in;
692 strm2.next_out = strm->next_out;
693 strm2.avail_out = strm->avail_out;
696 errorCode = inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size);
698 errorCode = inflateInit_(strm, zwd->version, zwd->stream_size);
699 LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", (int)errorCode);
700 if (errorCode != Z_OK) return ZWRAPD_finishWithError(zwd, strm, (int)errorCode);
703 strm->next_in = (unsigned char*)zwd->headerBuf;
704 strm->avail_in = ZLIB_HEADERSIZE;
706 errorCode = inflate(strm, Z_NO_FLUSH);
707 LOG_WRAPPERD("ZLIB inflate errorCode=%d strm->avail_in=%d\n", (int)errorCode, (int)strm->avail_in);
708 if (errorCode != Z_OK) return ZWRAPD_finishWithError(zwd, strm, (int)errorCode);
709 if (strm->avail_in > 0) goto error;
711 strm->next_in = strm2.next_in;
712 strm->avail_in = strm2.avail_in;
713 strm->next_out = strm2.next_out;
714 strm->avail_out = strm2.avail_out;
716 strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */
717 errorCode = ZWRAP_freeDCtx(zwd);
718 if (ZSTD_isError(errorCode)) goto error;
720 if (flush == Z_INFLATE_SYNC) res = inflateSync(strm);
721 else res = inflate(strm, flush);
722 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);
728 strm->reserved = ZWRAP_ZSTD_STREAM; /* mark as zstd steam */
730 if (flush == Z_INFLATE_SYNC) { strm->msg = "inflateSync is not supported!"; goto error; }
733 zwd->zbd = ZSTD_createDStream_advanced(zwd->customMem);
734 if (zwd->zbd == NULL) { LOG_WRAPPERD("ERROR: ZSTD_createDStream_advanced\n"); goto error; }
735 zwd->decompState = ZWRAP_useInit;
738 if (zwd->totalInBytes < ZSTD_HEADERSIZE)
740 if (zwd->totalInBytes == 0 && strm->avail_in >= ZSTD_HEADERSIZE) {
741 if (zwd->decompState == ZWRAP_useInit) {
742 errorCode = ZSTD_initDStream(zwd->zbd);
743 if (ZSTD_isError(errorCode)) { LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n", ZSTD_getErrorName(errorCode)); goto error; }
745 errorCode = ZSTD_resetDStream(zwd->zbd);
746 if (ZSTD_isError(errorCode)) goto error;
749 srcSize = MIN(strm->avail_in, ZSTD_HEADERSIZE - zwd->totalInBytes);
750 memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);
751 strm->total_in += srcSize;
752 zwd->totalInBytes += srcSize;
753 strm->next_in += srcSize;
754 strm->avail_in -= srcSize;
755 if (zwd->totalInBytes < ZSTD_HEADERSIZE) return Z_OK;
757 if (zwd->decompState == ZWRAP_useInit) {
758 errorCode = ZSTD_initDStream(zwd->zbd);
759 if (ZSTD_isError(errorCode)) { LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n", ZSTD_getErrorName(errorCode)); goto error; }
761 errorCode = ZSTD_resetDStream(zwd->zbd);
762 if (ZSTD_isError(errorCode)) goto error;
765 zwd->inBuffer.src = zwd->headerBuf;
766 zwd->inBuffer.size = ZSTD_HEADERSIZE;
767 zwd->inBuffer.pos = 0;
768 zwd->outBuffer.dst = strm->next_out;
769 zwd->outBuffer.size = 0;
770 zwd->outBuffer.pos = 0;
771 errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
772 LOG_WRAPPERD("inflate ZSTD_decompressStream1 errorCode=%d srcSize=%d dstCapacity=%d\n", (int)errorCode, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);
773 if (ZSTD_isError(errorCode)) {
774 LOG_WRAPPERD("ERROR: ZSTD_decompressStream1 %s\n", ZSTD_getErrorName(errorCode));
777 if (zwd->inBuffer.pos != zwd->inBuffer.size) goto error; /* not consumed */
781 zwd->inBuffer.src = strm->next_in;
782 zwd->inBuffer.size = strm->avail_in;
783 zwd->inBuffer.pos = 0;
784 zwd->outBuffer.dst = strm->next_out;
785 zwd->outBuffer.size = strm->avail_out;
786 zwd->outBuffer.pos = 0;
787 errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
788 LOG_WRAPPERD("inflate ZSTD_decompressStream2 errorCode=%d srcSize=%d dstCapacity=%d\n", (int)errorCode, (int)strm->avail_in, (int)strm->avail_out);
789 if (ZSTD_isError(errorCode)) {
791 LOG_WRAPPERD("ERROR: ZSTD_decompressStream2 %s zwd->errorCount=%d\n", ZSTD_getErrorName(errorCode), zwd->errorCount);
792 if (zwd->errorCount<=1) return Z_NEED_DICT; else goto error;
794 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);
795 strm->next_out += zwd->outBuffer.pos;
796 strm->total_out += zwd->outBuffer.pos;
797 strm->avail_out -= zwd->outBuffer.pos;
798 strm->total_in += zwd->inBuffer.pos;
799 zwd->totalInBytes += zwd->inBuffer.pos;
800 strm->next_in += zwd->inBuffer.pos;
801 strm->avail_in -= zwd->inBuffer.pos;
802 if (errorCode == 0) {
803 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);
804 zwd->decompState = ZWRAP_streamEnd;
808 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);
812 return ZWRAPD_finishWithError(zwd, strm, 0);
816 ZEXTERN int ZEXPORT z_inflateEnd OF((z_streamp strm))
818 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
819 return inflateEnd(strm);
821 LOG_WRAPPERD("- inflateEnd total_in=%d total_out=%d\n", (int)(strm->total_in), (int)(strm->total_out));
823 ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
824 if (zwd == NULL) return Z_OK; /* structures are already freed */
826 errorCode = ZWRAP_freeDCtx(zwd);
827 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
833 ZEXTERN int ZEXPORT z_inflateSync OF((z_streamp strm))
835 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
836 return inflateSync(strm);
839 return z_inflate(strm, Z_INFLATE_SYNC);
846 /* Advanced compression functions */
847 ZEXTERN int ZEXPORT z_deflateCopy OF((z_streamp dest,
850 if (!g_ZWRAP_useZSTDcompression)
851 return deflateCopy(dest, source);
852 return ZWRAPC_finishWithErrorMsg(source, "deflateCopy is not supported!");
856 ZEXTERN int ZEXPORT z_deflateTune OF((z_streamp strm,
862 if (!g_ZWRAP_useZSTDcompression)
863 return deflateTune(strm, good_length, max_lazy, nice_length, max_chain);
864 return ZWRAPC_finishWithErrorMsg(strm, "deflateTune is not supported!");
868 #if ZLIB_VERNUM >= 0x1260
869 ZEXTERN int ZEXPORT z_deflatePending OF((z_streamp strm,
873 if (!g_ZWRAP_useZSTDcompression)
874 return deflatePending(strm, pending, bits);
875 return ZWRAPC_finishWithErrorMsg(strm, "deflatePending is not supported!");
880 ZEXTERN int ZEXPORT z_deflatePrime OF((z_streamp strm,
884 if (!g_ZWRAP_useZSTDcompression)
885 return deflatePrime(strm, bits, value);
886 return ZWRAPC_finishWithErrorMsg(strm, "deflatePrime is not supported!");
890 ZEXTERN int ZEXPORT z_deflateSetHeader OF((z_streamp strm,
893 if (!g_ZWRAP_useZSTDcompression)
894 return deflateSetHeader(strm, head);
895 return ZWRAPC_finishWithErrorMsg(strm, "deflateSetHeader is not supported!");
901 /* Advanced decompression functions */
902 #if ZLIB_VERNUM >= 0x1280
903 ZEXTERN int ZEXPORT z_inflateGetDictionary OF((z_streamp strm,
907 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
908 return inflateGetDictionary(strm, dictionary, dictLength);
909 return ZWRAPD_finishWithErrorMsg(strm, "inflateGetDictionary is not supported!");
914 ZEXTERN int ZEXPORT z_inflateCopy OF((z_streamp dest,
917 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !source->reserved)
918 return inflateCopy(dest, source);
919 return ZWRAPD_finishWithErrorMsg(source, "inflateCopy is not supported!");
923 #if ZLIB_VERNUM >= 0x1240
924 ZEXTERN long ZEXPORT z_inflateMark OF((z_streamp strm))
926 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
927 return inflateMark(strm);
928 return ZWRAPD_finishWithErrorMsg(strm, "inflateMark is not supported!");
933 ZEXTERN int ZEXPORT z_inflatePrime OF((z_streamp strm,
937 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
938 return inflatePrime(strm, bits, value);
939 return ZWRAPD_finishWithErrorMsg(strm, "inflatePrime is not supported!");
943 ZEXTERN int ZEXPORT z_inflateGetHeader OF((z_streamp strm,
946 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
947 return inflateGetHeader(strm, head);
948 return ZWRAPD_finishWithErrorMsg(strm, "inflateGetHeader is not supported!");
952 ZEXTERN int ZEXPORT z_inflateBackInit_ OF((z_streamp strm, int windowBits,
953 unsigned char FAR *window,
957 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
958 return inflateBackInit_(strm, windowBits, window, version, stream_size);
959 return ZWRAPD_finishWithErrorMsg(strm, "inflateBackInit is not supported!");
963 ZEXTERN int ZEXPORT z_inflateBack OF((z_streamp strm,
964 in_func in, void FAR *in_desc,
965 out_func out, void FAR *out_desc))
967 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
968 return inflateBack(strm, in, in_desc, out, out_desc);
969 return ZWRAPD_finishWithErrorMsg(strm, "inflateBack is not supported!");
973 ZEXTERN int ZEXPORT z_inflateBackEnd OF((z_streamp strm))
975 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
976 return inflateBackEnd(strm);
977 return ZWRAPD_finishWithErrorMsg(strm, "inflateBackEnd is not supported!");
981 ZEXTERN uLong ZEXPORT z_zlibCompileFlags OF((void)) { return zlibCompileFlags(); };
985 /* utility functions */
988 ZEXTERN int ZEXPORT z_compress OF((Bytef *dest, uLongf *destLen,
989 const Bytef *source, uLong sourceLen))
991 if (!g_ZWRAP_useZSTDcompression)
992 return compress(dest, destLen, source, sourceLen);
994 { size_t dstCapacity = *destLen;
995 size_t const errorCode = ZSTD_compress(dest, dstCapacity, source, sourceLen, ZWRAP_DEFAULT_CLEVEL);
996 LOG_WRAPPERD("z_compress sourceLen=%d dstCapacity=%d\n", (int)sourceLen, (int)dstCapacity);
997 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
998 *destLen = errorCode;
1004 ZEXTERN int ZEXPORT z_compress2 OF((Bytef *dest, uLongf *destLen,
1005 const Bytef *source, uLong sourceLen,
1008 if (!g_ZWRAP_useZSTDcompression)
1009 return compress2(dest, destLen, source, sourceLen, level);
1011 { size_t dstCapacity = *destLen;
1012 size_t const errorCode = ZSTD_compress(dest, dstCapacity, source, sourceLen, level);
1013 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
1014 *destLen = errorCode;
1020 ZEXTERN uLong ZEXPORT z_compressBound OF((uLong sourceLen))
1022 if (!g_ZWRAP_useZSTDcompression)
1023 return compressBound(sourceLen);
1025 return ZSTD_compressBound(sourceLen);
1029 ZEXTERN int ZEXPORT z_uncompress OF((Bytef *dest, uLongf *destLen,
1030 const Bytef *source, uLong sourceLen))
1032 if (sourceLen < 4 || MEM_readLE32(source) != ZSTD_MAGICNUMBER)
1033 return uncompress(dest, destLen, source, sourceLen);
1035 { size_t dstCapacity = *destLen;
1036 size_t const errorCode = ZSTD_decompress(dest, dstCapacity, source, sourceLen);
1037 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
1038 *destLen = errorCode;
1043 #endif /* !Z_SOLO */
1046 /* checksum functions */
1048 ZEXTERN uLong ZEXPORT z_adler32 OF((uLong adler, const Bytef *buf, uInt len))
1050 return adler32(adler, buf, len);
1053 ZEXTERN uLong ZEXPORT z_crc32 OF((uLong crc, const Bytef *buf, uInt len))
1055 return crc32(crc, buf, len);
1059 #if ZLIB_VERNUM >= 0x12B0
1060 ZEXTERN uLong ZEXPORT z_adler32_z OF((uLong adler, const Bytef *buf, z_size_t len))
1062 return adler32_z(adler, buf, len);
1065 ZEXTERN uLong ZEXPORT z_crc32_z OF((uLong crc, const Bytef *buf, z_size_t len))
1067 return crc32_z(crc, buf, len);
1072 #if ZLIB_VERNUM >= 0x1270
1073 ZEXTERN const z_crc_t FAR * ZEXPORT z_get_crc_table OF((void))
1075 return get_crc_table();