blob: 0ae5012a184cd714e9139e54dd70f561aac77606 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Nick Terrellac58c8d2020-03-26 15:19:05 -07002 * Copyright (c) 2016-2020, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
Yann Collet4ded9e52016-08-30 10:04:33 -07003 * All rights reserved.
4 *
Yann Collet32fb4072017-08-18 16:52:05 -07005 * 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).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Yann Collet4ded9e52016-08-30 10:04:33 -07009 */
inikep3eabe9b2016-05-12 17:15:41 +020010
inikep3eabe9b2016-05-12 17:15:41 +020011
Yann Collet33a7e672017-06-02 17:10:49 -070012/* === Tuning parameters === */
13#ifndef ZWRAP_USE_ZSTD
14 #define ZWRAP_USE_ZSTD 0
15#endif
16
17
18/* === Dependencies === */
Yann Colletae728a42017-05-30 17:11:39 -070019#include <stdlib.h>
inikep3d2c58c2016-08-10 14:28:47 +020020#include <stdio.h> /* vsprintf */
21#include <stdarg.h> /* va_list, for z_gzprintf */
Nick Terrellb2ca26b2020-12-02 15:05:11 -080022#include <string.h>
Przemyslaw Skibinski0694ae22016-11-04 16:05:28 +010023#define NO_DUMMY_DECL
Przemyslaw Skibinski6cecb352016-11-04 17:49:17 +010024#define ZLIB_CONST
Przemyslaw Skibinski0694ae22016-11-04 16:05:28 +010025#include <zlib.h> /* without #define Z_PREFIX */
inikep3eabe9b2016-05-12 17:15:41 +020026#include "zstd_zlibwrapper.h"
Nick Terrellb2ca26b2020-12-02 15:05:11 -080027#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_isFrame, ZSTD_MAGICNUMBER, ZSTD_customMem */
inikep3eabe9b2016-05-12 17:15:41 +020028#include "zstd.h"
inikep3eabe9b2016-05-12 17:15:41 +020029
30
Yann Collet33a7e672017-06-02 17:10:49 -070031/* === Constants === */
inikep3eabe9b2016-05-12 17:15:41 +020032#define Z_INFLATE_SYNC 8
inikep8fc58482016-09-16 17:14:01 +020033#define ZLIB_HEADERSIZE 4
Nick Terrellb1ec94e2019-10-21 19:42:14 -070034#define ZSTD_HEADERSIZE ZSTD_FRAMEHEADERSIZE_MIN(ZSTD_f_zstd1)
inikep67a1f4d2016-09-26 20:49:18 +020035#define ZWRAP_DEFAULT_CLEVEL 3 /* Z_DEFAULT_COMPRESSION is translated to ZWRAP_DEFAULT_CLEVEL for zstd */
inikep3eabe9b2016-05-12 17:15:41 +020036
Yann Collet33a7e672017-06-02 17:10:49 -070037
38/* === Debug === */
39#define LOG_WRAPPERC(...) /* fprintf(stderr, __VA_ARGS__) */
40#define LOG_WRAPPERD(...) /* fprintf(stderr, __VA_ARGS__) */
inikep3eabe9b2016-05-12 17:15:41 +020041
inikepcd2f6b62016-09-23 20:03:17 +020042#define FINISH_WITH_GZ_ERR(msg) { (void)msg; return Z_STREAM_ERROR; }
43#define FINISH_WITH_NULL_ERR(msg) { (void)msg; return NULL; }
inikepd7557172016-09-22 11:52:00 +020044
Nick Terrellb2ca26b2020-12-02 15:05:11 -080045/* === Utility === */
46
47#define MIN(x,y) ((x) < (y) ? (x) : (y))
48
49static unsigned ZWRAP_isLittleEndian(void)
50{
51 const union { unsigned u; char c[4]; } one = { 1 }; /* don't use static : performance detrimental */
52 return one.c[0];
53}
54
55#ifndef __has_builtin
56# define __has_builtin(x) 0
57#endif
58
59static unsigned ZWRAP_swap32(unsigned in)
60{
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);
66#else
67 return ((in << 24) & 0xff000000 ) |
68 ((in << 8) & 0x00ff0000 ) |
69 ((in >> 8) & 0x0000ff00 ) |
70 ((in >> 24) & 0x000000ff );
71#endif
72}
73
74static unsigned ZWRAP_readLE32(const void* ptr)
75{
76 unsigned value;
77 memcpy(&value, ptr, sizeof(value));
78 if (ZWRAP_isLittleEndian())
79 return value;
80 else
81 return ZWRAP_swap32(value);
82}
83
inikepd7557172016-09-22 11:52:00 +020084
Yann Collet33a7e672017-06-02 17:10:49 -070085/* === Wrapper === */
86static int g_ZWRAP_useZSTDcompression = ZWRAP_USE_ZSTD; /* 0 = don't use ZSTD */
inikepd7557172016-09-22 11:52:00 +020087
inikep252c20d2016-09-23 09:08:40 +020088void ZWRAP_useZSTDcompression(int turn_on) { g_ZWRAP_useZSTDcompression = turn_on; }
inikepd7557172016-09-22 11:52:00 +020089
inikep252c20d2016-09-23 09:08:40 +020090int ZWRAP_isUsingZSTDcompression(void) { return g_ZWRAP_useZSTDcompression; }
inikep3eabe9b2016-05-12 17:15:41 +020091
92
93
inikepd7557172016-09-22 11:52:00 +020094static ZWRAP_decompress_type g_ZWRAPdecompressionType = ZWRAP_AUTO;
inikep3eabe9b2016-05-12 17:15:41 +020095
Yann Colletcb18fff2019-09-24 17:50:58 -070096void ZWRAP_setDecompressionType(ZWRAP_decompress_type type) { g_ZWRAPdecompressionType = type; }
inikep3eabe9b2016-05-12 17:15:41 +020097
inikep252c20d2016-09-23 09:08:40 +020098ZWRAP_decompress_type ZWRAP_getDecompressionType(void) { return g_ZWRAPdecompressionType; }
inikep3eabe9b2016-05-12 17:15:41 +020099
inikep3eabe9b2016-05-12 17:15:41 +0200100
101
inikepcd2f6b62016-09-23 20:03:17 +0200102const char * zstdVersion(void) { return ZSTD_VERSION_STRING; }
103
104ZEXTERN const char * ZEXPORT z_zlibVersion OF((void)) { return zlibVersion(); }
105
inikep7cab86f2016-06-02 18:24:07 +0200106static void* ZWRAP_allocFunction(void* opaque, size_t size)
inikepff9114a2016-06-02 16:52:36 +0200107{
108 z_streamp strm = (z_streamp) opaque;
Yann Collet9ceb49e2016-12-22 15:26:33 +0100109 void* address = strm->zalloc(strm->opaque, 1, (uInt)size);
Yann Collet33a7e672017-06-02 17:10:49 -0700110 /* LOG_WRAPPERC("ZWRAP alloc %p, %d \n", address, (int)size); */
inikepff9114a2016-06-02 16:52:36 +0200111 return address;
112}
113
inikep7cab86f2016-06-02 18:24:07 +0200114static void ZWRAP_freeFunction(void* opaque, void* address)
inikepff9114a2016-06-02 16:52:36 +0200115{
116 z_streamp strm = (z_streamp) opaque;
117 strm->zfree(strm->opaque, address);
Yann Collet33a7e672017-06-02 17:10:49 -0700118 /* if (address) LOG_WRAPPERC("ZWRAP free %p \n", address); */
inikepff9114a2016-06-02 16:52:36 +0200119}
120
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800121static void* ZWRAP_customMalloc(size_t size, ZSTD_customMem customMem)
122{
123 if (customMem.customAlloc)
124 return customMem.customAlloc(customMem.opaque, size);
125 return malloc(size);
126}
127
128static void* ZWRAP_customCalloc(size_t size, ZSTD_customMem customMem)
129{
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);
135 return ptr;
136 }
137 return calloc(1, size);
138}
139
140static void ZWRAP_customFree(void* ptr, ZSTD_customMem customMem)
141{
142 if (ptr!=NULL) {
143 if (customMem.customFree)
144 customMem.customFree(customMem.opaque, ptr);
145 else
146 free(ptr);
147 }
148}
149
inikepff9114a2016-06-02 16:52:36 +0200150
inikep3eabe9b2016-05-12 17:15:41 +0200151
Yann Collet33a7e672017-06-02 17:10:49 -0700152/* === Compression === */
inikep706876f2016-09-27 16:56:07 +0200153typedef enum { ZWRAP_useInit, ZWRAP_useReset, ZWRAP_streamEnd } ZWRAP_state_t;
inikep3eabe9b2016-05-12 17:15:41 +0200154
155typedef struct {
inikepb0773452016-09-16 14:06:10 +0200156 ZSTD_CStream* zbc;
inikep3eabe9b2016-05-12 17:15:41 +0200157 int compressionLevel;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100158 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 */
inikep2a746092016-06-03 14:53:51 +0200160 ZSTD_customMem customMem;
inikepff9114a2016-06-02 16:52:36 +0200161 z_stream allocFunc; /* copy of zalloc, zfree, opaque */
inikepb0773452016-09-16 14:06:10 +0200162 ZSTD_inBuffer inBuffer;
163 ZSTD_outBuffer outBuffer;
inikep706876f2016-09-27 16:56:07 +0200164 ZWRAP_state_t comprState;
inikep230a61f2016-09-21 16:46:35 +0200165 unsigned long long pledgedSrcSize;
Przemyslaw Skibinski0694ae22016-11-04 16:05:28 +0100166} ZWRAP_CCtx;
167
Yann Collet69c94012019-09-26 15:01:29 -0700168/* typedef ZWRAP_CCtx internal_state; */
Przemyslaw Skibinski0694ae22016-11-04 16:05:28 +0100169
inikep3eabe9b2016-05-12 17:15:41 +0200170
171
Yann Collet33a7e672017-06-02 17:10:49 -0700172static size_t ZWRAP_freeCCtx(ZWRAP_CCtx* zwc)
inikepf040be92016-06-03 16:31:57 +0200173{
174 if (zwc==NULL) return 0; /* support free on NULL */
Yann Collet33a7e672017-06-02 17:10:49 -0700175 ZSTD_freeCStream(zwc->zbc);
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800176 ZWRAP_customFree(zwc, zwc->customMem);
inikepf040be92016-06-03 16:31:57 +0200177 return 0;
178}
179
180
Yann Collet33a7e672017-06-02 17:10:49 -0700181static ZWRAP_CCtx* ZWRAP_createCCtx(z_streamp strm)
inikep3eabe9b2016-05-12 17:15:41 +0200182{
inikep2a746092016-06-03 14:53:51 +0200183 ZWRAP_CCtx* zwc;
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800184 ZSTD_customMem customMem = { NULL, NULL, NULL };
Yann Collet24779682020-12-04 20:25:01 -0800185
inikepff9114a2016-06-02 16:52:36 +0200186 if (strm->zalloc && strm->zfree) {
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800187 customMem.customAlloc = ZWRAP_allocFunction;
188 customMem.customFree = ZWRAP_freeFunction;
inikepff9114a2016-06-02 16:52:36 +0200189 }
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800190 customMem.opaque = strm;
191
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;
inikep2a746092016-06-03 14:53:51 +0200197
inikep3eabe9b2016-05-12 17:15:41 +0200198 return zwc;
199}
200
201
Yann Collet33a7e672017-06-02 17:10:49 -0700202static int ZWRAP_initializeCStream(ZWRAP_CCtx* zwc, const void* dict, size_t dictSize, unsigned long long pledgedSrcSize)
inikep61abecc2016-09-21 19:30:29 +0200203{
inikep2bb83e82016-09-23 18:59:53 +0200204 LOG_WRAPPERC("- ZWRAP_initializeCStream=%p\n", zwc);
inikep67a1f4d2016-09-26 20:49:18 +0200205 if (zwc == NULL || zwc->zbc == NULL) return Z_STREAM_ERROR;
Yann Colletba75e9d2016-12-21 19:57:18 +0100206
inikep67a1f4d2016-09-26 20:49:18 +0200207 if (!pledgedSrcSize) pledgedSrcSize = zwc->pledgedSrcSize;
Yann Collet33a7e672017-06-02 17:10:49 -0700208 { ZSTD_parameters const params = ZSTD_getParams(zwc->compressionLevel, pledgedSrcSize, dictSize);
209 size_t initErr;
Yann Collet2e7fd6a2018-11-20 15:13:27 -0800210 LOG_WRAPPERC("pledgedSrcSize=%d windowLog=%d chainLog=%d hashLog=%d searchLog=%d minMatch=%d strategy=%d\n",
211 (int)pledgedSrcSize, params.cParams.windowLog, params.cParams.chainLog, params.cParams.hashLog, params.cParams.searchLog, params.cParams.minMatch, params.cParams.strategy);
Yann Collet33a7e672017-06-02 17:10:49 -0700212 initErr = ZSTD_initCStream_advanced(zwc->zbc, dict, dictSize, params, pledgedSrcSize);
213 if (ZSTD_isError(initErr)) return Z_STREAM_ERROR;
214 }
inikep8e8b0462016-09-22 14:42:32 +0200215
inikep61abecc2016-09-21 19:30:29 +0200216 return Z_OK;
217}
218
219
Yann Collet33a7e672017-06-02 17:10:49 -0700220static int ZWRAPC_finishWithError(ZWRAP_CCtx* zwc, z_streamp strm, int error)
inikepc4ab5712016-09-19 14:54:13 +0200221{
inikepadc4c162016-09-21 19:39:25 +0200222 LOG_WRAPPERC("- ZWRAPC_finishWithError=%d\n", error);
inikepc4ab5712016-09-19 14:54:13 +0200223 if (zwc) ZWRAP_freeCCtx(zwc);
224 if (strm) strm->state = NULL;
inikep18f66452016-09-20 12:50:59 +0200225 return (error) ? error : Z_STREAM_ERROR;
inikepc4ab5712016-09-19 14:54:13 +0200226}
227
228
Yann Collet33a7e672017-06-02 17:10:49 -0700229static int ZWRAPC_finishWithErrorMsg(z_streamp strm, char* message)
inikep146ef582016-09-21 14:05:01 +0200230{
231 ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
232 strm->msg = message;
233 if (zwc == NULL) return Z_STREAM_ERROR;
Yann Colletba75e9d2016-12-21 19:57:18 +0100234
inikepadc4c162016-09-21 19:39:25 +0200235 return ZWRAPC_finishWithError(zwc, strm, 0);
inikep146ef582016-09-21 14:05:01 +0200236}
237
238
inikep252c20d2016-09-23 09:08:40 +0200239int ZWRAP_setPledgedSrcSize(z_streamp strm, unsigned long long pledgedSrcSize)
inikep230a61f2016-09-21 16:46:35 +0200240{
241 ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
242 if (zwc == NULL) return Z_STREAM_ERROR;
Yann Colletba75e9d2016-12-21 19:57:18 +0100243
inikep230a61f2016-09-21 16:46:35 +0200244 zwc->pledgedSrcSize = pledgedSrcSize;
inikep6072eaa2016-09-27 15:24:44 +0200245 zwc->comprState = ZWRAP_useInit;
inikep230a61f2016-09-21 16:46:35 +0200246 return Z_OK;
247}
248
Yann Collet24779682020-12-04 20:25:01 -0800249static struct internal_state* convert_into_sis(void* ptr)
250{
251 return (struct internal_state*) ptr;
252}
inikep230a61f2016-09-21 16:46:35 +0200253
inikep3eabe9b2016-05-12 17:15:41 +0200254ZEXTERN int ZEXPORT z_deflateInit_ OF((z_streamp strm, int level,
255 const char *version, int stream_size))
256{
257 ZWRAP_CCtx* zwc;
Yann Collet4ded9e52016-08-30 10:04:33 -0700258
inikep554b3b92016-09-20 15:18:00 +0200259 LOG_WRAPPERC("- deflateInit level=%d\n", level);
inikep252c20d2016-09-23 09:08:40 +0200260 if (!g_ZWRAP_useZSTDcompression) {
inikep3eabe9b2016-05-12 17:15:41 +0200261 return deflateInit_((strm), (level), version, stream_size);
262 }
263
inikepff9114a2016-06-02 16:52:36 +0200264 zwc = ZWRAP_createCCtx(strm);
inikep3eabe9b2016-05-12 17:15:41 +0200265 if (zwc == NULL) return Z_MEM_ERROR;
266
inikep8b452452016-06-01 10:50:17 +0200267 if (level == Z_DEFAULT_COMPRESSION)
268 level = ZWRAP_DEFAULT_CLEVEL;
269
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100270 zwc->streamEnd = 0;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100271 zwc->totalInBytes = 0;
inikep3eabe9b2016-05-12 17:15:41 +0200272 zwc->compressionLevel = level;
Yann Collet24779682020-12-04 20:25:01 -0800273 strm->state = convert_into_sis(zwc); /* use state which in not used by user */
inikep3eabe9b2016-05-12 17:15:41 +0200274 strm->total_in = 0;
275 strm->total_out = 0;
inikep68cd4762016-09-23 12:42:21 +0200276 strm->adler = 0;
inikep3eabe9b2016-05-12 17:15:41 +0200277 return Z_OK;
278}
279
280
inikep8b452452016-06-01 10:50:17 +0200281ZEXTERN int ZEXPORT z_deflateInit2_ OF((z_streamp strm, int level, int method,
inikep3eabe9b2016-05-12 17:15:41 +0200282 int windowBits, int memLevel,
283 int strategy, const char *version,
284 int stream_size))
285{
inikep252c20d2016-09-23 09:08:40 +0200286 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200287 return deflateInit2_(strm, level, method, windowBits, memLevel, strategy, version, stream_size);
288
289 return z_deflateInit_ (strm, level, version, stream_size);
290}
291
292
inikep20859af2016-09-27 17:27:43 +0200293int ZWRAP_deflateReset_keepDict(z_streamp strm)
inikep706876f2016-09-27 16:56:07 +0200294{
inikep20859af2016-09-27 17:27:43 +0200295 LOG_WRAPPERC("- ZWRAP_deflateReset_keepDict\n");
inikep856f91e2016-09-27 17:14:04 +0200296 if (!g_ZWRAP_useZSTDcompression)
297 return deflateReset(strm);
298
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100299 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
Yann Colletae728a42017-05-30 17:11:39 -0700300 if (zwc) {
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100301 zwc->streamEnd = 0;
302 zwc->totalInBytes = 0;
303 }
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100304 }
305
inikep706876f2016-09-27 16:56:07 +0200306 strm->total_in = 0;
307 strm->total_out = 0;
308 strm->adler = 0;
309 return Z_OK;
310}
311
312
inikep61016872016-09-19 14:27:29 +0200313ZEXTERN int ZEXPORT z_deflateReset OF((z_streamp strm))
314{
inikep554b3b92016-09-20 15:18:00 +0200315 LOG_WRAPPERC("- deflateReset\n");
inikep252c20d2016-09-23 09:08:40 +0200316 if (!g_ZWRAP_useZSTDcompression)
inikep61016872016-09-19 14:27:29 +0200317 return deflateReset(strm);
Yann Colletba75e9d2016-12-21 19:57:18 +0100318
inikep20859af2016-09-27 17:27:43 +0200319 ZWRAP_deflateReset_keepDict(strm);
inikep706876f2016-09-27 16:56:07 +0200320
321 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100322 if (zwc) zwc->comprState = ZWRAP_useInit;
inikep706876f2016-09-27 16:56:07 +0200323 }
inikepc038c302016-09-20 12:54:26 +0200324 return Z_OK;
inikep61016872016-09-19 14:27:29 +0200325}
326
327
inikep3eabe9b2016-05-12 17:15:41 +0200328ZEXTERN int ZEXPORT z_deflateSetDictionary OF((z_streamp strm,
329 const Bytef *dictionary,
330 uInt dictLength))
331{
inikep252c20d2016-09-23 09:08:40 +0200332 if (!g_ZWRAP_useZSTDcompression) {
inikep554b3b92016-09-20 15:18:00 +0200333 LOG_WRAPPERC("- deflateSetDictionary\n");
inikep3eabe9b2016-05-12 17:15:41 +0200334 return deflateSetDictionary(strm, dictionary, dictLength);
inikep554b3b92016-09-20 15:18:00 +0200335 }
inikep3eabe9b2016-05-12 17:15:41 +0200336
inikepe02bf992016-06-02 12:00:32 +0200337 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
inikep554b3b92016-09-20 15:18:00 +0200338 LOG_WRAPPERC("- deflateSetDictionary level=%d\n", (int)zwc->compressionLevel);
inikep61abecc2016-09-21 19:30:29 +0200339 if (!zwc) return Z_STREAM_ERROR;
340 if (zwc->zbc == NULL) {
inikep67a1f4d2016-09-26 20:49:18 +0200341 zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
inikepa03b7a72016-09-26 22:11:55 +0200342 if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
inikep61abecc2016-09-21 19:30:29 +0200343 }
Yann Colletca1a9eb2017-10-18 11:18:27 -0700344 { int res = ZWRAP_initializeCStream(zwc, dictionary, dictLength, ZSTD_CONTENTSIZE_UNKNOWN);
inikepa03b7a72016-09-26 22:11:55 +0200345 if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res); }
inikep6072eaa2016-09-27 15:24:44 +0200346 zwc->comprState = ZWRAP_useReset;
inikep3eabe9b2016-05-12 17:15:41 +0200347 }
Yann Collet4ded9e52016-08-30 10:04:33 -0700348
inikep3eabe9b2016-05-12 17:15:41 +0200349 return Z_OK;
350}
351
inikepc4ab5712016-09-19 14:54:13 +0200352
inikep3eabe9b2016-05-12 17:15:41 +0200353ZEXTERN int ZEXPORT z_deflate OF((z_streamp strm, int flush))
354{
355 ZWRAP_CCtx* zwc;
356
inikep252c20d2016-09-23 09:08:40 +0200357 if (!g_ZWRAP_useZSTDcompression) {
Yann Collet33a7e672017-06-02 17:10:49 -0700358 LOG_WRAPPERC("- deflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d\n",
359 (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);
360 return deflate(strm, flush);
inikep3eabe9b2016-05-12 17:15:41 +0200361 }
362
inikepe02bf992016-06-02 12:00:32 +0200363 zwc = (ZWRAP_CCtx*) strm->state;
inikep2bb83e82016-09-23 18:59:53 +0200364 if (zwc == NULL) { LOG_WRAPPERC("zwc == NULL\n"); return Z_STREAM_ERROR; }
inikep3eabe9b2016-05-12 17:15:41 +0200365
inikep61abecc2016-09-21 19:30:29 +0200366 if (zwc->zbc == NULL) {
inikep67a1f4d2016-09-26 20:49:18 +0200367 zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
Yann Colletba75e9d2016-12-21 19:57:18 +0100368 if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
Yann Colletca1a9eb2017-10-18 11:18:27 -0700369 { int const initErr = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : ZSTD_CONTENTSIZE_UNKNOWN);
Yann Collet33a7e672017-06-02 17:10:49 -0700370 if (initErr != Z_OK) return ZWRAPC_finishWithError(zwc, strm, initErr); }
inikep6072eaa2016-09-27 15:24:44 +0200371 if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
inikepcf3ec082016-09-23 10:30:26 +0200372 } else {
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100373 if (zwc->totalInBytes == 0) {
inikep6072eaa2016-09-27 15:24:44 +0200374 if (zwc->comprState == ZWRAP_useReset) {
Yann Collet33a7e672017-06-02 17:10:49 -0700375 size_t const resetErr = ZSTD_resetCStream(zwc->zbc, (flush == Z_FINISH) ? strm->avail_in : zwc->pledgedSrcSize);
376 if (ZSTD_isError(resetErr)) {
377 LOG_WRAPPERC("ERROR: ZSTD_resetCStream errorCode=%s\n",
378 ZSTD_getErrorName(resetErr));
379 return ZWRAPC_finishWithError(zwc, strm, 0);
380 }
inikep67a1f4d2016-09-26 20:49:18 +0200381 } else {
Yann Colletca1a9eb2017-10-18 11:18:27 -0700382 int const res = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : ZSTD_CONTENTSIZE_UNKNOWN);
inikep67a1f4d2016-09-26 20:49:18 +0200383 if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res);
inikep6072eaa2016-09-27 15:24:44 +0200384 if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
inikep67a1f4d2016-09-26 20:49:18 +0200385 }
Yann Collet33a7e672017-06-02 17:10:49 -0700386 } /* (zwc->totalInBytes == 0) */
387 } /* ! (zwc->zbc == NULL) */
inikep61abecc2016-09-21 19:30:29 +0200388
Przemyslaw Skibinskieee427e2016-12-13 19:14:04 +0100389 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);
inikep3eabe9b2016-05-12 17:15:41 +0200390 if (strm->avail_in > 0) {
inikepb0773452016-09-16 14:06:10 +0200391 zwc->inBuffer.src = strm->next_in;
392 zwc->inBuffer.size = strm->avail_in;
393 zwc->inBuffer.pos = 0;
394 zwc->outBuffer.dst = strm->next_out;
395 zwc->outBuffer.size = strm->avail_out;
396 zwc->outBuffer.pos = 0;
Yann Collet33a7e672017-06-02 17:10:49 -0700397 { size_t const cErr = ZSTD_compressStream(zwc->zbc, &zwc->outBuffer, &zwc->inBuffer);
inikep554b3b92016-09-20 15:18:00 +0200398 LOG_WRAPPERC("deflate ZSTD_compressStream srcSize=%d dstCapacity=%d\n", (int)zwc->inBuffer.size, (int)zwc->outBuffer.size);
Yann Collet33a7e672017-06-02 17:10:49 -0700399 if (ZSTD_isError(cErr)) return ZWRAPC_finishWithError(zwc, strm, 0);
inikepb0773452016-09-16 14:06:10 +0200400 }
401 strm->next_out += zwc->outBuffer.pos;
402 strm->total_out += zwc->outBuffer.pos;
403 strm->avail_out -= zwc->outBuffer.pos;
404 strm->total_in += zwc->inBuffer.pos;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100405 zwc->totalInBytes += zwc->inBuffer.pos;
inikepb0773452016-09-16 14:06:10 +0200406 strm->next_in += zwc->inBuffer.pos;
407 strm->avail_in -= zwc->inBuffer.pos;
inikep3eabe9b2016-05-12 17:15:41 +0200408 }
409
Yann Colletba75e9d2016-12-21 19:57:18 +0100410 if (flush == Z_FULL_FLUSH
Przemyslaw Skibinskiedd3e2a2016-11-28 12:46:16 +0100411#if ZLIB_VERNUM >= 0x1240
Yann Colletba75e9d2016-12-21 19:57:18 +0100412 || flush == Z_TREES
Przemyslaw Skibinskiedd3e2a2016-11-28 12:46:16 +0100413#endif
414 || flush == Z_BLOCK)
415 return ZWRAPC_finishWithErrorMsg(strm, "Z_FULL_FLUSH, Z_BLOCK and Z_TREES are not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200416
Dmitry Krot2ed3ba22016-08-13 22:02:45 +0300417 if (flush == Z_FINISH) {
inikep3eabe9b2016-05-12 17:15:41 +0200418 size_t bytesLeft;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100419 if (zwc->streamEnd) return Z_STREAM_END;
inikepb0773452016-09-16 14:06:10 +0200420 zwc->outBuffer.dst = strm->next_out;
421 zwc->outBuffer.size = strm->avail_out;
422 zwc->outBuffer.pos = 0;
inikepe46bad02016-09-19 13:24:07 +0200423 bytesLeft = ZSTD_endStream(zwc->zbc, &zwc->outBuffer);
inikep554b3b92016-09-20 15:18:00 +0200424 LOG_WRAPPERC("deflate ZSTD_endStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
inikepadc4c162016-09-21 19:39:25 +0200425 if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);
inikepb0773452016-09-16 14:06:10 +0200426 strm->next_out += zwc->outBuffer.pos;
427 strm->total_out += zwc->outBuffer.pos;
428 strm->avail_out -= zwc->outBuffer.pos;
Yann Collet33a7e672017-06-02 17:10:49 -0700429 if (bytesLeft == 0) {
430 zwc->streamEnd = 1;
431 LOG_WRAPPERC("Z_STREAM_END2 strm->total_in=%d strm->avail_out=%d strm->total_out=%d\n",
432 (int)strm->total_in, (int)strm->avail_out, (int)strm->total_out);
433 return Z_STREAM_END;
434 } }
inikepe46bad02016-09-19 13:24:07 +0200435 else
inikep61016872016-09-19 14:27:29 +0200436 if (flush == Z_SYNC_FLUSH || flush == Z_PARTIAL_FLUSH) {
Dmitry Krot2ed3ba22016-08-13 22:02:45 +0300437 size_t bytesLeft;
inikep8fc58482016-09-16 17:14:01 +0200438 zwc->outBuffer.dst = strm->next_out;
439 zwc->outBuffer.size = strm->avail_out;
440 zwc->outBuffer.pos = 0;
inikepb0773452016-09-16 14:06:10 +0200441 bytesLeft = ZSTD_flushStream(zwc->zbc, &zwc->outBuffer);
inikep554b3b92016-09-20 15:18:00 +0200442 LOG_WRAPPERC("deflate ZSTD_flushStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
inikepadc4c162016-09-21 19:39:25 +0200443 if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);
inikepb0773452016-09-16 14:06:10 +0200444 strm->next_out += zwc->outBuffer.pos;
445 strm->total_out += zwc->outBuffer.pos;
446 strm->avail_out -= zwc->outBuffer.pos;
Dmitry Krot2ed3ba22016-08-13 22:02:45 +0300447 }
Przemyslaw Skibinskieee427e2016-12-13 19:14:04 +0100448 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);
inikep3eabe9b2016-05-12 17:15:41 +0200449 return Z_OK;
450}
451
452
Yann Collet4ded9e52016-08-30 10:04:33 -0700453ZEXTERN int ZEXPORT z_deflateEnd OF((z_streamp strm))
inikep3eabe9b2016-05-12 17:15:41 +0200454{
inikep252c20d2016-09-23 09:08:40 +0200455 if (!g_ZWRAP_useZSTDcompression) {
inikep554b3b92016-09-20 15:18:00 +0200456 LOG_WRAPPERC("- deflateEnd\n");
inikep3eabe9b2016-05-12 17:15:41 +0200457 return deflateEnd(strm);
458 }
inikep554b3b92016-09-20 15:18:00 +0200459 LOG_WRAPPERC("- deflateEnd total_in=%d total_out=%d\n", (int)(strm->total_in), (int)(strm->total_out));
inikep3fa1b742016-09-21 13:51:57 +0200460 { size_t errorCode;
461 ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
462 if (zwc == NULL) return Z_OK; /* structures are already freed */
inikepc4ab5712016-09-19 14:54:13 +0200463 strm->state = NULL;
inikep3fa1b742016-09-21 13:51:57 +0200464 errorCode = ZWRAP_freeCCtx(zwc);
inikep18f66452016-09-20 12:50:59 +0200465 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +0200466 }
467 return Z_OK;
468}
469
470
471ZEXTERN uLong ZEXPORT z_deflateBound OF((z_streamp strm,
472 uLong sourceLen))
473{
inikep252c20d2016-09-23 09:08:40 +0200474 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200475 return deflateBound(strm, sourceLen);
476
477 return ZSTD_compressBound(sourceLen);
478}
479
480
481ZEXTERN int ZEXPORT z_deflateParams OF((z_streamp strm,
482 int level,
483 int strategy))
484{
inikep252c20d2016-09-23 09:08:40 +0200485 if (!g_ZWRAP_useZSTDcompression) {
inikep554b3b92016-09-20 15:18:00 +0200486 LOG_WRAPPERC("- deflateParams level=%d strategy=%d\n", level, strategy);
inikep3eabe9b2016-05-12 17:15:41 +0200487 return deflateParams(strm, level, strategy);
488 }
489
490 return Z_OK;
491}
492
493
494
495
496
Yann Collet33a7e672017-06-02 17:10:49 -0700497/* === Decompression === */
498
inikep706876f2016-09-27 16:56:07 +0200499typedef enum { ZWRAP_ZLIB_STREAM, ZWRAP_ZSTD_STREAM, ZWRAP_UNKNOWN_STREAM } ZWRAP_stream_type;
inikep3eabe9b2016-05-12 17:15:41 +0200500
501typedef struct {
inikepb0773452016-09-16 14:06:10 +0200502 ZSTD_DStream* zbd;
Yann Collet33a7e672017-06-02 17:10:49 -0700503 char headerBuf[16]; /* must be >= ZSTD_frameHeaderSize_min */
inikep3eabe9b2016-05-12 17:15:41 +0200504 int errorCount;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100505 unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */
inikep706876f2016-09-27 16:56:07 +0200506 ZWRAP_state_t decompState;
inikep86fc8e02016-09-20 16:22:28 +0200507 ZSTD_inBuffer inBuffer;
508 ZSTD_outBuffer outBuffer;
Yann Collet4ded9e52016-08-30 10:04:33 -0700509
inikep3eabe9b2016-05-12 17:15:41 +0200510 /* zlib params */
511 int stream_size;
512 char *version;
513 int windowBits;
inikepf040be92016-06-03 16:31:57 +0200514 ZSTD_customMem customMem;
Yann Collet33a7e672017-06-02 17:10:49 -0700515 z_stream allocFunc; /* just to copy zalloc, zfree, opaque */
Przemyslaw Skibinski0694ae22016-11-04 16:05:28 +0100516} ZWRAP_DCtx;
inikep3eabe9b2016-05-12 17:15:41 +0200517
518
Yann Collet33a7e672017-06-02 17:10:49 -0700519static void ZWRAP_initDCtx(ZWRAP_DCtx* zwd)
520{
521 zwd->errorCount = 0;
522 zwd->outBuffer.pos = 0;
523 zwd->outBuffer.size = 0;
524}
525
526static ZWRAP_DCtx* ZWRAP_createDCtx(z_streamp strm)
527{
528 ZWRAP_DCtx* zwd;
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800529 ZSTD_customMem customMem = { NULL, NULL, NULL };
Yann Collet33a7e672017-06-02 17:10:49 -0700530
531 if (strm->zalloc && strm->zfree) {
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800532 customMem.customAlloc = ZWRAP_allocFunction;
533 customMem.customFree = ZWRAP_freeFunction;
Yann Collet33a7e672017-06-02 17:10:49 -0700534 }
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800535 customMem.opaque = strm;
536
537 zwd = (ZWRAP_DCtx*)ZWRAP_customCalloc(sizeof(ZWRAP_DCtx), customMem);
538 if (zwd == NULL) return NULL;
539 zwd->allocFunc = *strm;
540 customMem.opaque = &zwd->allocFunc;
541 zwd->customMem = customMem;
Yann Collet33a7e672017-06-02 17:10:49 -0700542
543 ZWRAP_initDCtx(zwd);
544 return zwd;
545}
546
547static size_t ZWRAP_freeDCtx(ZWRAP_DCtx* zwd)
548{
549 if (zwd==NULL) return 0; /* support free on null */
550 ZSTD_freeDStream(zwd->zbd);
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800551 ZWRAP_customFree(zwd->version, zwd->customMem);
552 ZWRAP_customFree(zwd, zwd->customMem);
Yann Collet33a7e672017-06-02 17:10:49 -0700553 return 0;
554}
555
556
Yann Colletba75e9d2016-12-21 19:57:18 +0100557int ZWRAP_isUsingZSTDdecompression(z_streamp strm)
inikep706876f2016-09-27 16:56:07 +0200558{
559 if (strm == NULL) return 0;
560 return (strm->reserved == ZWRAP_ZSTD_STREAM);
561}
562
563
Yann Collet33a7e672017-06-02 17:10:49 -0700564static int ZWRAPD_finishWithError(ZWRAP_DCtx* zwd, z_streamp strm, int error)
inikep0bb930b2016-09-19 14:31:16 +0200565{
inikepadc4c162016-09-21 19:39:25 +0200566 LOG_WRAPPERD("- ZWRAPD_finishWithError=%d\n", error);
Yann Collet33a7e672017-06-02 17:10:49 -0700567 ZWRAP_freeDCtx(zwd);
568 strm->state = NULL;
inikep18f66452016-09-20 12:50:59 +0200569 return (error) ? error : Z_STREAM_ERROR;
inikep0bb930b2016-09-19 14:31:16 +0200570}
571
Yann Collet33a7e672017-06-02 17:10:49 -0700572static int ZWRAPD_finishWithErrorMsg(z_streamp strm, char* message)
inikep146ef582016-09-21 14:05:01 +0200573{
Yann Collet33a7e672017-06-02 17:10:49 -0700574 ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
inikep146ef582016-09-21 14:05:01 +0200575 strm->msg = message;
576 if (zwd == NULL) return Z_STREAM_ERROR;
Yann Colletba75e9d2016-12-21 19:57:18 +0100577
inikepadc4c162016-09-21 19:39:25 +0200578 return ZWRAPD_finishWithError(zwd, strm, 0);
inikep146ef582016-09-21 14:05:01 +0200579}
580
581
inikep3eabe9b2016-05-12 17:15:41 +0200582ZEXTERN int ZEXPORT z_inflateInit_ OF((z_streamp strm,
Yann Collet69c94012019-09-26 15:01:29 -0700583 const char* version, int stream_size))
inikep3eabe9b2016-05-12 17:15:41 +0200584{
inikepd7557172016-09-22 11:52:00 +0200585 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
Yann Collet33a7e672017-06-02 17:10:49 -0700586 strm->reserved = ZWRAP_ZLIB_STREAM;
inikepd7557172016-09-22 11:52:00 +0200587 return inflateInit(strm);
588 }
589
Yann Collet33a7e672017-06-02 17:10:49 -0700590 { ZWRAP_DCtx* const zwd = ZWRAP_createDCtx(strm);
Yann Colletdcb75352017-06-02 14:01:21 -0700591 LOG_WRAPPERD("- inflateInit\n");
592 if (zwd == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
inikep3eabe9b2016-05-12 17:15:41 +0200593
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800594 zwd->version = (char*)ZWRAP_customMalloc(strlen(version)+1, zwd->customMem);
Yann Colletdcb75352017-06-02 14:01:21 -0700595 if (zwd->version == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
596 strcpy(zwd->version, version);
inikep3eabe9b2016-05-12 17:15:41 +0200597
Yann Colletdcb75352017-06-02 14:01:21 -0700598 zwd->stream_size = stream_size;
599 zwd->totalInBytes = 0;
Yann Collet24779682020-12-04 20:25:01 -0800600 strm->state = convert_into_sis(zwd);
Yann Colletdcb75352017-06-02 14:01:21 -0700601 strm->total_in = 0;
602 strm->total_out = 0;
Yann Collet33a7e672017-06-02 17:10:49 -0700603 strm->reserved = ZWRAP_UNKNOWN_STREAM;
Yann Colletdcb75352017-06-02 14:01:21 -0700604 strm->adler = 0;
inikepd7557172016-09-22 11:52:00 +0200605 }
inikep3eabe9b2016-05-12 17:15:41 +0200606
607 return Z_OK;
608}
609
610
611ZEXTERN int ZEXPORT z_inflateInit2_ OF((z_streamp strm, int windowBits,
612 const char *version, int stream_size))
613{
inikepd7557172016-09-22 11:52:00 +0200614 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
615 return inflateInit2_(strm, windowBits, version, stream_size);
616 }
Yann Colletba75e9d2016-12-21 19:57:18 +0100617
Yann Collet33a7e672017-06-02 17:10:49 -0700618 { int const ret = z_inflateInit_ (strm, version, stream_size);
Yann Colletdcb75352017-06-02 14:01:21 -0700619 LOG_WRAPPERD("- inflateInit2 windowBits=%d\n", windowBits);
620 if (ret == Z_OK) {
621 ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*)strm->state;
622 if (zwd == NULL) return Z_STREAM_ERROR;
623 zwd->windowBits = windowBits;
624 }
625 return ret;
inikepd7557172016-09-22 11:52:00 +0200626 }
inikep3eabe9b2016-05-12 17:15:41 +0200627}
628
inikep20859af2016-09-27 17:27:43 +0200629int ZWRAP_inflateReset_keepDict(z_streamp strm)
inikep18f66452016-09-20 12:50:59 +0200630{
inikep20859af2016-09-27 17:27:43 +0200631 LOG_WRAPPERD("- ZWRAP_inflateReset_keepDict\n");
inikep856f91e2016-09-27 17:14:04 +0200632 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
633 return inflateReset(strm);
634
Yann Colletdcb75352017-06-02 14:01:21 -0700635 { ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
inikep554b3b92016-09-20 15:18:00 +0200636 if (zwd == NULL) return Z_STREAM_ERROR;
inikep86fc8e02016-09-20 16:22:28 +0200637 ZWRAP_initDCtx(zwd);
inikep706876f2016-09-27 16:56:07 +0200638 zwd->decompState = ZWRAP_useReset;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100639 zwd->totalInBytes = 0;
inikep554b3b92016-09-20 15:18:00 +0200640 }
Yann Colletba75e9d2016-12-21 19:57:18 +0100641
inikep18f66452016-09-20 12:50:59 +0200642 strm->total_in = 0;
643 strm->total_out = 0;
644 return Z_OK;
645}
646
647
inikep706876f2016-09-27 16:56:07 +0200648ZEXTERN int ZEXPORT z_inflateReset OF((z_streamp strm))
649{
650 LOG_WRAPPERD("- inflateReset\n");
651 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
652 return inflateReset(strm);
653
Yann Collet33a7e672017-06-02 17:10:49 -0700654 { int const ret = ZWRAP_inflateReset_keepDict(strm);
inikep706876f2016-09-27 16:56:07 +0200655 if (ret != Z_OK) return ret; }
656
Yann Collet33a7e672017-06-02 17:10:49 -0700657 { ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
Yann Colletba75e9d2016-12-21 19:57:18 +0100658 if (zwd == NULL) return Z_STREAM_ERROR;
inikep706876f2016-09-27 16:56:07 +0200659 zwd->decompState = ZWRAP_useInit; }
660
661 return Z_OK;
662}
663
664
inikep69413002016-09-20 16:40:50 +0200665#if ZLIB_VERNUM >= 0x1240
666ZEXTERN int ZEXPORT z_inflateReset2 OF((z_streamp strm,
667 int windowBits))
668{
inikepd7557172016-09-22 11:52:00 +0200669 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep69413002016-09-20 16:40:50 +0200670 return inflateReset2(strm, windowBits);
671
Yann Collet33a7e672017-06-02 17:10:49 -0700672 { int const ret = z_inflateReset (strm);
inikep69413002016-09-20 16:40:50 +0200673 if (ret == Z_OK) {
Yann Collet33a7e672017-06-02 17:10:49 -0700674 ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*)strm->state;
inikep69413002016-09-20 16:40:50 +0200675 if (zwd == NULL) return Z_STREAM_ERROR;
676 zwd->windowBits = windowBits;
677 }
678 return ret;
679 }
680}
681#endif
682
683
inikep3eabe9b2016-05-12 17:15:41 +0200684ZEXTERN int ZEXPORT z_inflateSetDictionary OF((z_streamp strm,
685 const Bytef *dictionary,
686 uInt dictLength))
687{
inikep554b3b92016-09-20 15:18:00 +0200688 LOG_WRAPPERD("- inflateSetDictionary\n");
inikepd7557172016-09-22 11:52:00 +0200689 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +0200690 return inflateSetDictionary(strm, dictionary, dictLength);
691
Yann Collet33a7e672017-06-02 17:10:49 -0700692 { ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
inikepf7ab3ad2016-09-22 17:59:10 +0200693 if (zwd == NULL || zwd->zbd == NULL) return Z_STREAM_ERROR;
Yann Collet33a7e672017-06-02 17:10:49 -0700694 { size_t const initErr = ZSTD_initDStream_usingDict(zwd->zbd, dictionary, dictLength);
695 if (ZSTD_isError(initErr)) return ZWRAPD_finishWithError(zwd, strm, 0); }
Yann Colletba75e9d2016-12-21 19:57:18 +0100696 zwd->decompState = ZWRAP_useReset;
Yann Collet4ded9e52016-08-30 10:04:33 -0700697
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100698 if (zwd->totalInBytes == ZSTD_HEADERSIZE) {
inikepb0773452016-09-16 14:06:10 +0200699 zwd->inBuffer.src = zwd->headerBuf;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100700 zwd->inBuffer.size = zwd->totalInBytes;
inikepb0773452016-09-16 14:06:10 +0200701 zwd->inBuffer.pos = 0;
702 zwd->outBuffer.dst = strm->next_out;
703 zwd->outBuffer.size = 0;
704 zwd->outBuffer.pos = 0;
Yann Collet33a7e672017-06-02 17:10:49 -0700705 { size_t const errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
706 LOG_WRAPPERD("inflateSetDictionary ZSTD_decompressStream errorCode=%d srcSize=%d dstCapacity=%d\n",
707 (int)errorCode, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);
708 if (zwd->inBuffer.pos < zwd->outBuffer.size || ZSTD_isError(errorCode)) {
709 LOG_WRAPPERD("ERROR: ZSTD_decompressStream %s\n",
710 ZSTD_getErrorName(errorCode));
711 return ZWRAPD_finishWithError(zwd, strm, 0);
712 } } } }
inikep3eabe9b2016-05-12 17:15:41 +0200713
714 return Z_OK;
715}
716
717
Yann Collet4ded9e52016-08-30 10:04:33 -0700718ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush))
inikep3eabe9b2016-05-12 17:15:41 +0200719{
inikep57b97082016-09-23 14:59:46 +0200720 ZWRAP_DCtx* zwd;
Yann Collet33a7e672017-06-02 17:10:49 -0700721
inikepd7557172016-09-22 11:52:00 +0200722 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
Yann Collet33a7e672017-06-02 17:10:49 -0700723 int const result = inflate(strm, flush);
724 LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
725 (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, result);
726 return result;
inikep554b3b92016-09-20 15:18:00 +0200727 }
inikep3eabe9b2016-05-12 17:15:41 +0200728
inikepe82c8112016-09-23 16:20:13 +0200729 if (strm->avail_in <= 0) return Z_OK;
730
Yann Collet33a7e672017-06-02 17:10:49 -0700731 zwd = (ZWRAP_DCtx*) strm->state;
732 LOG_WRAPPERD("- inflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d\n",
733 (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);
inikep86fc8e02016-09-20 16:22:28 +0200734
Yann Collet33a7e672017-06-02 17:10:49 -0700735 if (zwd == NULL) return Z_STREAM_ERROR;
736 if (zwd->decompState == ZWRAP_streamEnd) return Z_STREAM_END;
inikep86fc8e02016-09-20 16:22:28 +0200737
Yann Collet33a7e672017-06-02 17:10:49 -0700738 if (zwd->totalInBytes < ZLIB_HEADERSIZE) {
739 if (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) {
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800740 if (ZWRAP_readLE32(strm->next_in) != ZSTD_MAGICNUMBER) {
Yann Collet33a7e672017-06-02 17:10:49 -0700741 { int const initErr = (zwd->windowBits) ?
742 inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size) :
743 inflateInit_(strm, zwd->version, zwd->stream_size);
744 LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", initErr);
745 if (initErr != Z_OK) return ZWRAPD_finishWithError(zwd, strm, initErr);
inikep22e27302016-09-27 18:21:17 +0200746 }
inikep3eabe9b2016-05-12 17:15:41 +0200747
Yann Collet33a7e672017-06-02 17:10:49 -0700748 strm->reserved = ZWRAP_ZLIB_STREAM;
749 { size_t const freeErr = ZWRAP_freeDCtx(zwd);
750 if (ZSTD_isError(freeErr)) goto error; }
751
752 { int const result = (flush == Z_INFLATE_SYNC) ?
753 inflateSync(strm) :
754 inflate(strm, flush);
755 LOG_WRAPPERD("- inflate3 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
756 (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, res);
757 return result;
758 } }
759 } else { /* ! (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) */
760 size_t const srcSize = MIN(strm->avail_in, ZLIB_HEADERSIZE - zwd->totalInBytes);
761 memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);
762 strm->total_in += srcSize;
763 zwd->totalInBytes += srcSize;
764 strm->next_in += srcSize;
765 strm->avail_in -= srcSize;
766 if (zwd->totalInBytes < ZLIB_HEADERSIZE) return Z_OK;
767
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800768 if (ZWRAP_readLE32(zwd->headerBuf) != ZSTD_MAGICNUMBER) {
Yann Collet33a7e672017-06-02 17:10:49 -0700769 z_stream strm2;
770 strm2.next_in = strm->next_in;
771 strm2.avail_in = strm->avail_in;
772 strm2.next_out = strm->next_out;
773 strm2.avail_out = strm->avail_out;
774
775 { int const initErr = (zwd->windowBits) ?
776 inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size) :
777 inflateInit_(strm, zwd->version, zwd->stream_size);
778 LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", initErr);
779 if (initErr != Z_OK) return ZWRAPD_finishWithError(zwd, strm, initErr);
780 }
781
782 /* inflate header */
783 strm->next_in = (unsigned char*)zwd->headerBuf;
784 strm->avail_in = ZLIB_HEADERSIZE;
785 strm->avail_out = 0;
786 { int const dErr = inflate(strm, Z_NO_FLUSH);
787 LOG_WRAPPERD("ZLIB inflate errorCode=%d strm->avail_in=%d\n",
788 dErr, (int)strm->avail_in);
789 if (dErr != Z_OK)
790 return ZWRAPD_finishWithError(zwd, strm, dErr);
791 }
792 if (strm->avail_in > 0) goto error;
793
794 strm->next_in = strm2.next_in;
795 strm->avail_in = strm2.avail_in;
796 strm->next_out = strm2.next_out;
797 strm->avail_out = strm2.avail_out;
798
799 strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */
800 { size_t const freeErr = ZWRAP_freeDCtx(zwd);
801 if (ZSTD_isError(freeErr)) goto error; }
802
803 { int const result = (flush == Z_INFLATE_SYNC) ?
804 inflateSync(strm) :
805 inflate(strm, flush);
806 LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
807 (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, res);
808 return result;
809 } } } /* if ! (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) */
810 } /* (zwd->totalInBytes < ZLIB_HEADERSIZE) */
811
812 strm->reserved = ZWRAP_ZSTD_STREAM; /* mark as zstd steam */
813
814 if (flush == Z_INFLATE_SYNC) { strm->msg = "inflateSync is not supported!"; goto error; }
815
816 if (!zwd->zbd) {
817 zwd->zbd = ZSTD_createDStream_advanced(zwd->customMem);
818 if (zwd->zbd == NULL) { LOG_WRAPPERD("ERROR: ZSTD_createDStream_advanced\n"); goto error; }
819 zwd->decompState = ZWRAP_useInit;
820 }
821
822 if (zwd->totalInBytes < ZSTD_HEADERSIZE) {
823 if (zwd->totalInBytes == 0 && strm->avail_in >= ZSTD_HEADERSIZE) {
824 if (zwd->decompState == ZWRAP_useInit) {
825 size_t const initErr = ZSTD_initDStream(zwd->zbd);
826 if (ZSTD_isError(initErr)) {
827 LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n",
828 ZSTD_getErrorName(initErr));
inikep57b97082016-09-23 14:59:46 +0200829 goto error;
830 }
Yann Collet33a7e672017-06-02 17:10:49 -0700831 } else {
832 size_t const resetErr = ZSTD_resetDStream(zwd->zbd);
833 if (ZSTD_isError(resetErr)) goto error;
inikep3eabe9b2016-05-12 17:15:41 +0200834 }
Yann Collet33a7e672017-06-02 17:10:49 -0700835 } else {
836 size_t const srcSize = MIN(strm->avail_in, ZSTD_HEADERSIZE - zwd->totalInBytes);
837 memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);
838 strm->total_in += srcSize;
839 zwd->totalInBytes += srcSize;
840 strm->next_in += srcSize;
841 strm->avail_in -= srcSize;
842 if (zwd->totalInBytes < ZSTD_HEADERSIZE) return Z_OK;
inikep3eabe9b2016-05-12 17:15:41 +0200843
Yann Collet33a7e672017-06-02 17:10:49 -0700844 if (zwd->decompState == ZWRAP_useInit) {
845 size_t const initErr = ZSTD_initDStream(zwd->zbd);
846 if (ZSTD_isError(initErr)) {
847 LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n",
848 ZSTD_getErrorName(initErr));
849 goto error;
850 }
851 } else {
852 size_t const resetErr = ZSTD_resetDStream(zwd->zbd);
853 if (ZSTD_isError(resetErr)) goto error;
854 }
855
856 zwd->inBuffer.src = zwd->headerBuf;
857 zwd->inBuffer.size = ZSTD_HEADERSIZE;
858 zwd->inBuffer.pos = 0;
859 zwd->outBuffer.dst = strm->next_out;
860 zwd->outBuffer.size = 0;
861 zwd->outBuffer.pos = 0;
862 { size_t const dErr = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
863 LOG_WRAPPERD("inflate ZSTD_decompressStream1 errorCode=%d srcSize=%d dstCapacity=%d\n",
864 (int)dErr, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);
865 if (ZSTD_isError(dErr)) {
866 LOG_WRAPPERD("ERROR: ZSTD_decompressStream1 %s\n", ZSTD_getErrorName(dErr));
867 goto error;
868 } }
869 if (zwd->inBuffer.pos != zwd->inBuffer.size) goto error; /* not consumed */
870 }
871 } /* (zwd->totalInBytes < ZSTD_HEADERSIZE) */
872
873 zwd->inBuffer.src = strm->next_in;
874 zwd->inBuffer.size = strm->avail_in;
875 zwd->inBuffer.pos = 0;
876 zwd->outBuffer.dst = strm->next_out;
877 zwd->outBuffer.size = strm->avail_out;
878 zwd->outBuffer.pos = 0;
879 { size_t const dErr = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
880 LOG_WRAPPERD("inflate ZSTD_decompressStream2 errorCode=%d srcSize=%d dstCapacity=%d\n",
881 (int)dErr, (int)strm->avail_in, (int)strm->avail_out);
882 if (ZSTD_isError(dErr)) {
inikep3eabe9b2016-05-12 17:15:41 +0200883 zwd->errorCount++;
Yann Collet33a7e672017-06-02 17:10:49 -0700884 LOG_WRAPPERD("ERROR: ZSTD_decompressStream2 %s zwd->errorCount=%d\n",
885 ZSTD_getErrorName(dErr), zwd->errorCount);
inikep1c9521f2016-06-13 12:00:46 +0200886 if (zwd->errorCount<=1) return Z_NEED_DICT; else goto error;
inikep3eabe9b2016-05-12 17:15:41 +0200887 }
Yann Collet33a7e672017-06-02 17:10:49 -0700888 LOG_WRAPPERD("inflate inBuffer.pos=%d inBuffer.size=%d outBuffer.pos=%d outBuffer.size=%d o\n",
889 (int)zwd->inBuffer.pos, (int)zwd->inBuffer.size, (int)zwd->outBuffer.pos, (int)zwd->outBuffer.size);
inikepb0773452016-09-16 14:06:10 +0200890 strm->next_out += zwd->outBuffer.pos;
891 strm->total_out += zwd->outBuffer.pos;
892 strm->avail_out -= zwd->outBuffer.pos;
inikepf7ab3ad2016-09-22 17:59:10 +0200893 strm->total_in += zwd->inBuffer.pos;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100894 zwd->totalInBytes += zwd->inBuffer.pos;
inikepf7ab3ad2016-09-22 17:59:10 +0200895 strm->next_in += zwd->inBuffer.pos;
896 strm->avail_in -= zwd->inBuffer.pos;
Yann Collet33a7e672017-06-02 17:10:49 -0700897 if (dErr == 0) {
898 LOG_WRAPPERD("inflate Z_STREAM_END1 avail_in=%d avail_out=%d total_in=%d total_out=%d\n",
899 (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);
Yann Colletba75e9d2016-12-21 19:57:18 +0100900 zwd->decompState = ZWRAP_streamEnd;
inikep86fc8e02016-09-20 16:22:28 +0200901 return Z_STREAM_END;
902 }
Yann Collet33a7e672017-06-02 17:10:49 -0700903 } /* dErr lifetime */
904
905 LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
906 (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, Z_OK);
inikep3eabe9b2016-05-12 17:15:41 +0200907 return Z_OK;
inikep57b97082016-09-23 14:59:46 +0200908
909error:
910 return ZWRAPD_finishWithError(zwd, strm, 0);
inikep3eabe9b2016-05-12 17:15:41 +0200911}
912
913
914ZEXTERN int ZEXPORT z_inflateEnd OF((z_streamp strm))
915{
inikepd7557172016-09-22 11:52:00 +0200916 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikepe02bf992016-06-02 12:00:32 +0200917 return inflateEnd(strm);
Yann Collet4ded9e52016-08-30 10:04:33 -0700918
Yann Collet33a7e672017-06-02 17:10:49 -0700919 LOG_WRAPPERD("- inflateEnd total_in=%d total_out=%d\n",
920 (int)(strm->total_in), (int)(strm->total_out));
921 { ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
inikep3fa1b742016-09-21 13:51:57 +0200922 if (zwd == NULL) return Z_OK; /* structures are already freed */
Yann Collet33a7e672017-06-02 17:10:49 -0700923 { size_t const freeErr = ZWRAP_freeDCtx(zwd);
924 if (ZSTD_isError(freeErr)) return Z_STREAM_ERROR; }
inikep1c9521f2016-06-13 12:00:46 +0200925 strm->state = NULL;
inikep3eabe9b2016-05-12 17:15:41 +0200926 }
inikep3fa1b742016-09-21 13:51:57 +0200927 return Z_OK;
inikep3eabe9b2016-05-12 17:15:41 +0200928}
929
930
931ZEXTERN int ZEXPORT z_inflateSync OF((z_streamp strm))
932{
inikepd7557172016-09-22 11:52:00 +0200933 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
inikep18f66452016-09-20 12:50:59 +0200934 return inflateSync(strm);
935 }
inikep61016872016-09-19 14:27:29 +0200936
inikep3eabe9b2016-05-12 17:15:41 +0200937 return z_inflate(strm, Z_INFLATE_SYNC);
938}
939
940
941
inikep3eabe9b2016-05-12 17:15:41 +0200942/* Advanced compression functions */
943ZEXTERN int ZEXPORT z_deflateCopy OF((z_streamp dest,
944 z_streamp source))
945{
inikep252c20d2016-09-23 09:08:40 +0200946 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200947 return deflateCopy(dest, source);
inikepadc4c162016-09-21 19:39:25 +0200948 return ZWRAPC_finishWithErrorMsg(source, "deflateCopy is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200949}
950
951
inikep3eabe9b2016-05-12 17:15:41 +0200952ZEXTERN int ZEXPORT z_deflateTune OF((z_streamp strm,
953 int good_length,
954 int max_lazy,
955 int nice_length,
956 int max_chain))
957{
inikep252c20d2016-09-23 09:08:40 +0200958 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200959 return deflateTune(strm, good_length, max_lazy, nice_length, max_chain);
inikepadc4c162016-09-21 19:39:25 +0200960 return ZWRAPC_finishWithErrorMsg(strm, "deflateTune is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200961}
962
963
inikepbf25d7a2016-06-02 10:19:35 +0200964#if ZLIB_VERNUM >= 0x1260
inikep3eabe9b2016-05-12 17:15:41 +0200965ZEXTERN int ZEXPORT z_deflatePending OF((z_streamp strm,
966 unsigned *pending,
967 int *bits))
968{
inikep252c20d2016-09-23 09:08:40 +0200969 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200970 return deflatePending(strm, pending, bits);
inikepadc4c162016-09-21 19:39:25 +0200971 return ZWRAPC_finishWithErrorMsg(strm, "deflatePending is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200972}
inikepbf25d7a2016-06-02 10:19:35 +0200973#endif
inikep3eabe9b2016-05-12 17:15:41 +0200974
975
976ZEXTERN int ZEXPORT z_deflatePrime OF((z_streamp strm,
977 int bits,
978 int value))
979{
inikep252c20d2016-09-23 09:08:40 +0200980 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200981 return deflatePrime(strm, bits, value);
inikepadc4c162016-09-21 19:39:25 +0200982 return ZWRAPC_finishWithErrorMsg(strm, "deflatePrime is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200983}
984
985
986ZEXTERN int ZEXPORT z_deflateSetHeader OF((z_streamp strm,
987 gz_headerp head))
988{
inikep252c20d2016-09-23 09:08:40 +0200989 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200990 return deflateSetHeader(strm, head);
inikepadc4c162016-09-21 19:39:25 +0200991 return ZWRAPC_finishWithErrorMsg(strm, "deflateSetHeader is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200992}
993
994
995
996
inikep61016872016-09-19 14:27:29 +0200997/* Advanced decompression functions */
inikepbf25d7a2016-06-02 10:19:35 +0200998#if ZLIB_VERNUM >= 0x1280
inikep3eabe9b2016-05-12 17:15:41 +0200999ZEXTERN int ZEXPORT z_inflateGetDictionary OF((z_streamp strm,
1000 Bytef *dictionary,
1001 uInt *dictLength))
1002{
inikepd7557172016-09-22 11:52:00 +02001003 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +02001004 return inflateGetDictionary(strm, dictionary, dictLength);
inikepadc4c162016-09-21 19:39:25 +02001005 return ZWRAPD_finishWithErrorMsg(strm, "inflateGetDictionary is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001006}
inikepbf25d7a2016-06-02 10:19:35 +02001007#endif
inikep3eabe9b2016-05-12 17:15:41 +02001008
1009
1010ZEXTERN int ZEXPORT z_inflateCopy OF((z_streamp dest,
1011 z_streamp source))
1012{
inikepd7557172016-09-22 11:52:00 +02001013 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !source->reserved)
inikep3eabe9b2016-05-12 17:15:41 +02001014 return inflateCopy(dest, source);
inikepadc4c162016-09-21 19:39:25 +02001015 return ZWRAPD_finishWithErrorMsg(source, "inflateCopy is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001016}
1017
1018
inikepbf25d7a2016-06-02 10:19:35 +02001019#if ZLIB_VERNUM >= 0x1240
inikepbf25d7a2016-06-02 10:19:35 +02001020ZEXTERN long ZEXPORT z_inflateMark OF((z_streamp strm))
1021{
inikepd7557172016-09-22 11:52:00 +02001022 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikepbf25d7a2016-06-02 10:19:35 +02001023 return inflateMark(strm);
inikepadc4c162016-09-21 19:39:25 +02001024 return ZWRAPD_finishWithErrorMsg(strm, "inflateMark is not supported!");
inikepbf25d7a2016-06-02 10:19:35 +02001025}
1026#endif
inikep3eabe9b2016-05-12 17:15:41 +02001027
1028
1029ZEXTERN int ZEXPORT z_inflatePrime OF((z_streamp strm,
1030 int bits,
1031 int value))
1032{
inikepd7557172016-09-22 11:52:00 +02001033 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +02001034 return inflatePrime(strm, bits, value);
inikepadc4c162016-09-21 19:39:25 +02001035 return ZWRAPD_finishWithErrorMsg(strm, "inflatePrime is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001036}
1037
1038
inikep3eabe9b2016-05-12 17:15:41 +02001039ZEXTERN int ZEXPORT z_inflateGetHeader OF((z_streamp strm,
1040 gz_headerp head))
1041{
inikepd7557172016-09-22 11:52:00 +02001042 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +02001043 return inflateGetHeader(strm, head);
inikepadc4c162016-09-21 19:39:25 +02001044 return ZWRAPD_finishWithErrorMsg(strm, "inflateGetHeader is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001045}
1046
1047
1048ZEXTERN int ZEXPORT z_inflateBackInit_ OF((z_streamp strm, int windowBits,
1049 unsigned char FAR *window,
1050 const char *version,
1051 int stream_size))
1052{
inikepd7557172016-09-22 11:52:00 +02001053 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +02001054 return inflateBackInit_(strm, windowBits, window, version, stream_size);
inikepadc4c162016-09-21 19:39:25 +02001055 return ZWRAPD_finishWithErrorMsg(strm, "inflateBackInit is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001056}
1057
1058
1059ZEXTERN int ZEXPORT z_inflateBack OF((z_streamp strm,
1060 in_func in, void FAR *in_desc,
1061 out_func out, void FAR *out_desc))
1062{
inikepd7557172016-09-22 11:52:00 +02001063 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +02001064 return inflateBack(strm, in, in_desc, out, out_desc);
inikepadc4c162016-09-21 19:39:25 +02001065 return ZWRAPD_finishWithErrorMsg(strm, "inflateBack is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001066}
1067
1068
1069ZEXTERN int ZEXPORT z_inflateBackEnd OF((z_streamp strm))
1070{
inikepd7557172016-09-22 11:52:00 +02001071 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +02001072 return inflateBackEnd(strm);
inikepadc4c162016-09-21 19:39:25 +02001073 return ZWRAPD_finishWithErrorMsg(strm, "inflateBackEnd is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001074}
1075
1076
Yann Colletcb18fff2019-09-24 17:50:58 -07001077ZEXTERN uLong ZEXPORT z_zlibCompileFlags OF((void)) { return zlibCompileFlags(); }
inikep3eabe9b2016-05-12 17:15:41 +02001078
1079
1080
Yann Collet33a7e672017-06-02 17:10:49 -07001081 /* === utility functions === */
inikep3eabe9b2016-05-12 17:15:41 +02001082#ifndef Z_SOLO
1083
1084ZEXTERN int ZEXPORT z_compress OF((Bytef *dest, uLongf *destLen,
1085 const Bytef *source, uLong sourceLen))
1086{
inikep252c20d2016-09-23 09:08:40 +02001087 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001088 return compress(dest, destLen, source, sourceLen);
1089
Yann Collet33a7e672017-06-02 17:10:49 -07001090 { size_t dstCapacity = *destLen;
1091 size_t const cSize = ZSTD_compress(dest, dstCapacity,
1092 source, sourceLen,
1093 ZWRAP_DEFAULT_CLEVEL);
1094 LOG_WRAPPERD("z_compress sourceLen=%d dstCapacity=%d\n",
1095 (int)sourceLen, (int)dstCapacity);
1096 if (ZSTD_isError(cSize)) return Z_STREAM_ERROR;
1097 *destLen = cSize;
inikep3eabe9b2016-05-12 17:15:41 +02001098 }
1099 return Z_OK;
1100}
1101
1102
1103ZEXTERN int ZEXPORT z_compress2 OF((Bytef *dest, uLongf *destLen,
1104 const Bytef *source, uLong sourceLen,
1105 int level))
1106{
inikep252c20d2016-09-23 09:08:40 +02001107 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001108 return compress2(dest, destLen, source, sourceLen, level);
Yann Collet4ded9e52016-08-30 10:04:33 -07001109
1110 { size_t dstCapacity = *destLen;
Yann Collet4effccb2017-06-02 14:24:58 -07001111 size_t const cSize = ZSTD_compress(dest, dstCapacity, source, sourceLen, level);
1112 if (ZSTD_isError(cSize)) return Z_STREAM_ERROR;
1113 *destLen = cSize;
inikep3eabe9b2016-05-12 17:15:41 +02001114 }
1115 return Z_OK;
1116}
1117
1118
1119ZEXTERN uLong ZEXPORT z_compressBound OF((uLong sourceLen))
1120{
inikep252c20d2016-09-23 09:08:40 +02001121 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001122 return compressBound(sourceLen);
1123
1124 return ZSTD_compressBound(sourceLen);
1125}
1126
1127
1128ZEXTERN int ZEXPORT z_uncompress OF((Bytef *dest, uLongf *destLen,
1129 const Bytef *source, uLong sourceLen))
1130{
Yann Collet4effccb2017-06-02 14:24:58 -07001131 if (!ZSTD_isFrame(source, sourceLen))
inikep3eabe9b2016-05-12 17:15:41 +02001132 return uncompress(dest, destLen, source, sourceLen);
1133
Yann Collet4ded9e52016-08-30 10:04:33 -07001134 { size_t dstCapacity = *destLen;
Yann Collet4effccb2017-06-02 14:24:58 -07001135 size_t const dSize = ZSTD_decompress(dest, dstCapacity, source, sourceLen);
1136 if (ZSTD_isError(dSize)) return Z_STREAM_ERROR;
1137 *destLen = dSize;
inikep3eabe9b2016-05-12 17:15:41 +02001138 }
1139 return Z_OK;
1140}
1141
inikep3eabe9b2016-05-12 17:15:41 +02001142#endif /* !Z_SOLO */
1143
1144
1145 /* checksum functions */
1146
1147ZEXTERN uLong ZEXPORT z_adler32 OF((uLong adler, const Bytef *buf, uInt len))
1148{
1149 return adler32(adler, buf, len);
1150}
1151
1152ZEXTERN uLong ZEXPORT z_crc32 OF((uLong crc, const Bytef *buf, uInt len))
1153{
1154 return crc32(crc, buf, len);
1155}
Przemyslaw Skibinski5b114d32017-01-17 13:02:06 +01001156
Przemyslaw Skibinskic9512db2017-01-18 12:51:44 +01001157
1158#if ZLIB_VERNUM >= 0x12B0
1159ZEXTERN uLong ZEXPORT z_adler32_z OF((uLong adler, const Bytef *buf, z_size_t len))
1160{
1161 return adler32_z(adler, buf, len);
1162}
1163
1164ZEXTERN uLong ZEXPORT z_crc32_z OF((uLong crc, const Bytef *buf, z_size_t len))
1165{
1166 return crc32_z(crc, buf, len);
1167}
1168#endif
1169
1170
Przemyslaw Skibinski5edab912017-01-18 10:39:39 +01001171#if ZLIB_VERNUM >= 0x1270
Przemyslaw Skibinski5b114d32017-01-17 13:02:06 +01001172ZEXTERN const z_crc_t FAR * ZEXPORT z_get_crc_table OF((void))
1173{
1174 return get_crc_table();
1175}
Przemyslaw Skibinski5edab912017-01-18 10:39:39 +01001176#endif