blob: 479ddd4e0060a28d7069279fa3d165524a1f2204 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Elliott Hughes44aba642023-09-12 20:18:59 +00002 * Copyright (c) Meta Platforms, Inc. and affiliates.
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
Elliott Hughes44aba642023-09-12 20:18:59 +0000104ZEXTERN const char * ZEXPORT z_zlibVersion _Z_OF((void)) { return zlibVersion(); }
inikepcd2f6b62016-09-23 20:03:17 +0200105
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;
sen698f2612021-05-06 17:59:32 -0400208 { 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;
Yann Collet2e7fd6a2018-11-20 15:13:27 -0800212 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);
sen698f2612021-05-06 17:59:32 -0400214
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));
220
221 ZSTD_freeCCtxParams(cctxParams);
222 if (initErr) return Z_STREAM_ERROR;
Yann Collet33a7e672017-06-02 17:10:49 -0700223 }
inikep8e8b0462016-09-22 14:42:32 +0200224
inikep61abecc2016-09-21 19:30:29 +0200225 return Z_OK;
226}
227
228
Yann Collet33a7e672017-06-02 17:10:49 -0700229static int ZWRAPC_finishWithError(ZWRAP_CCtx* zwc, z_streamp strm, int error)
inikepc4ab5712016-09-19 14:54:13 +0200230{
inikepadc4c162016-09-21 19:39:25 +0200231 LOG_WRAPPERC("- ZWRAPC_finishWithError=%d\n", error);
inikepc4ab5712016-09-19 14:54:13 +0200232 if (zwc) ZWRAP_freeCCtx(zwc);
233 if (strm) strm->state = NULL;
inikep18f66452016-09-20 12:50:59 +0200234 return (error) ? error : Z_STREAM_ERROR;
inikepc4ab5712016-09-19 14:54:13 +0200235}
236
237
Yann Collet33a7e672017-06-02 17:10:49 -0700238static int ZWRAPC_finishWithErrorMsg(z_streamp strm, char* message)
inikep146ef582016-09-21 14:05:01 +0200239{
240 ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
241 strm->msg = message;
242 if (zwc == NULL) return Z_STREAM_ERROR;
Yann Colletba75e9d2016-12-21 19:57:18 +0100243
inikepadc4c162016-09-21 19:39:25 +0200244 return ZWRAPC_finishWithError(zwc, strm, 0);
inikep146ef582016-09-21 14:05:01 +0200245}
246
247
inikep252c20d2016-09-23 09:08:40 +0200248int ZWRAP_setPledgedSrcSize(z_streamp strm, unsigned long long pledgedSrcSize)
inikep230a61f2016-09-21 16:46:35 +0200249{
250 ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
251 if (zwc == NULL) return Z_STREAM_ERROR;
Yann Colletba75e9d2016-12-21 19:57:18 +0100252
inikep230a61f2016-09-21 16:46:35 +0200253 zwc->pledgedSrcSize = pledgedSrcSize;
inikep6072eaa2016-09-27 15:24:44 +0200254 zwc->comprState = ZWRAP_useInit;
inikep230a61f2016-09-21 16:46:35 +0200255 return Z_OK;
256}
257
Yann Collet24779682020-12-04 20:25:01 -0800258static struct internal_state* convert_into_sis(void* ptr)
259{
260 return (struct internal_state*) ptr;
261}
inikep230a61f2016-09-21 16:46:35 +0200262
Elliott Hughes44aba642023-09-12 20:18:59 +0000263ZEXTERN int ZEXPORT z_deflateInit_ _Z_OF((z_streamp strm, int level,
inikep3eabe9b2016-05-12 17:15:41 +0200264 const char *version, int stream_size))
265{
266 ZWRAP_CCtx* zwc;
Yann Collet4ded9e52016-08-30 10:04:33 -0700267
inikep554b3b92016-09-20 15:18:00 +0200268 LOG_WRAPPERC("- deflateInit level=%d\n", level);
inikep252c20d2016-09-23 09:08:40 +0200269 if (!g_ZWRAP_useZSTDcompression) {
inikep3eabe9b2016-05-12 17:15:41 +0200270 return deflateInit_((strm), (level), version, stream_size);
271 }
272
inikepff9114a2016-06-02 16:52:36 +0200273 zwc = ZWRAP_createCCtx(strm);
inikep3eabe9b2016-05-12 17:15:41 +0200274 if (zwc == NULL) return Z_MEM_ERROR;
275
inikep8b452452016-06-01 10:50:17 +0200276 if (level == Z_DEFAULT_COMPRESSION)
277 level = ZWRAP_DEFAULT_CLEVEL;
278
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100279 zwc->streamEnd = 0;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100280 zwc->totalInBytes = 0;
inikep3eabe9b2016-05-12 17:15:41 +0200281 zwc->compressionLevel = level;
Yann Collet24779682020-12-04 20:25:01 -0800282 strm->state = convert_into_sis(zwc); /* use state which in not used by user */
inikep3eabe9b2016-05-12 17:15:41 +0200283 strm->total_in = 0;
284 strm->total_out = 0;
inikep68cd4762016-09-23 12:42:21 +0200285 strm->adler = 0;
inikep3eabe9b2016-05-12 17:15:41 +0200286 return Z_OK;
287}
288
289
Elliott Hughes44aba642023-09-12 20:18:59 +0000290ZEXTERN int ZEXPORT z_deflateInit2_ _Z_OF((z_streamp strm, int level, int method,
inikep3eabe9b2016-05-12 17:15:41 +0200291 int windowBits, int memLevel,
292 int strategy, const char *version,
293 int stream_size))
294{
inikep252c20d2016-09-23 09:08:40 +0200295 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200296 return deflateInit2_(strm, level, method, windowBits, memLevel, strategy, version, stream_size);
297
298 return z_deflateInit_ (strm, level, version, stream_size);
299}
300
301
inikep20859af2016-09-27 17:27:43 +0200302int ZWRAP_deflateReset_keepDict(z_streamp strm)
inikep706876f2016-09-27 16:56:07 +0200303{
inikep20859af2016-09-27 17:27:43 +0200304 LOG_WRAPPERC("- ZWRAP_deflateReset_keepDict\n");
inikep856f91e2016-09-27 17:14:04 +0200305 if (!g_ZWRAP_useZSTDcompression)
306 return deflateReset(strm);
307
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100308 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
Yann Colletae728a42017-05-30 17:11:39 -0700309 if (zwc) {
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100310 zwc->streamEnd = 0;
311 zwc->totalInBytes = 0;
312 }
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100313 }
314
inikep706876f2016-09-27 16:56:07 +0200315 strm->total_in = 0;
316 strm->total_out = 0;
317 strm->adler = 0;
318 return Z_OK;
319}
320
321
Elliott Hughes44aba642023-09-12 20:18:59 +0000322ZEXTERN int ZEXPORT z_deflateReset _Z_OF((z_streamp strm))
inikep61016872016-09-19 14:27:29 +0200323{
inikep554b3b92016-09-20 15:18:00 +0200324 LOG_WRAPPERC("- deflateReset\n");
inikep252c20d2016-09-23 09:08:40 +0200325 if (!g_ZWRAP_useZSTDcompression)
inikep61016872016-09-19 14:27:29 +0200326 return deflateReset(strm);
Yann Colletba75e9d2016-12-21 19:57:18 +0100327
inikep20859af2016-09-27 17:27:43 +0200328 ZWRAP_deflateReset_keepDict(strm);
inikep706876f2016-09-27 16:56:07 +0200329
330 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100331 if (zwc) zwc->comprState = ZWRAP_useInit;
inikep706876f2016-09-27 16:56:07 +0200332 }
inikepc038c302016-09-20 12:54:26 +0200333 return Z_OK;
inikep61016872016-09-19 14:27:29 +0200334}
335
336
Elliott Hughes44aba642023-09-12 20:18:59 +0000337ZEXTERN int ZEXPORT z_deflateSetDictionary _Z_OF((z_streamp strm,
inikep3eabe9b2016-05-12 17:15:41 +0200338 const Bytef *dictionary,
339 uInt dictLength))
340{
inikep252c20d2016-09-23 09:08:40 +0200341 if (!g_ZWRAP_useZSTDcompression) {
inikep554b3b92016-09-20 15:18:00 +0200342 LOG_WRAPPERC("- deflateSetDictionary\n");
inikep3eabe9b2016-05-12 17:15:41 +0200343 return deflateSetDictionary(strm, dictionary, dictLength);
inikep554b3b92016-09-20 15:18:00 +0200344 }
inikep3eabe9b2016-05-12 17:15:41 +0200345
inikepe02bf992016-06-02 12:00:32 +0200346 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
inikep554b3b92016-09-20 15:18:00 +0200347 LOG_WRAPPERC("- deflateSetDictionary level=%d\n", (int)zwc->compressionLevel);
inikep61abecc2016-09-21 19:30:29 +0200348 if (!zwc) return Z_STREAM_ERROR;
349 if (zwc->zbc == NULL) {
inikep67a1f4d2016-09-26 20:49:18 +0200350 zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
inikepa03b7a72016-09-26 22:11:55 +0200351 if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
inikep61abecc2016-09-21 19:30:29 +0200352 }
Yann Colletca1a9eb2017-10-18 11:18:27 -0700353 { int res = ZWRAP_initializeCStream(zwc, dictionary, dictLength, ZSTD_CONTENTSIZE_UNKNOWN);
inikepa03b7a72016-09-26 22:11:55 +0200354 if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res); }
inikep6072eaa2016-09-27 15:24:44 +0200355 zwc->comprState = ZWRAP_useReset;
inikep3eabe9b2016-05-12 17:15:41 +0200356 }
Yann Collet4ded9e52016-08-30 10:04:33 -0700357
inikep3eabe9b2016-05-12 17:15:41 +0200358 return Z_OK;
359}
360
inikepc4ab5712016-09-19 14:54:13 +0200361
Elliott Hughes44aba642023-09-12 20:18:59 +0000362ZEXTERN int ZEXPORT z_deflate _Z_OF((z_streamp strm, int flush))
inikep3eabe9b2016-05-12 17:15:41 +0200363{
364 ZWRAP_CCtx* zwc;
365
inikep252c20d2016-09-23 09:08:40 +0200366 if (!g_ZWRAP_useZSTDcompression) {
Yann Collet33a7e672017-06-02 17:10:49 -0700367 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);
inikep3eabe9b2016-05-12 17:15:41 +0200370 }
371
inikepe02bf992016-06-02 12:00:32 +0200372 zwc = (ZWRAP_CCtx*) strm->state;
inikep2bb83e82016-09-23 18:59:53 +0200373 if (zwc == NULL) { LOG_WRAPPERC("zwc == NULL\n"); return Z_STREAM_ERROR; }
inikep3eabe9b2016-05-12 17:15:41 +0200374
inikep61abecc2016-09-21 19:30:29 +0200375 if (zwc->zbc == NULL) {
inikep67a1f4d2016-09-26 20:49:18 +0200376 zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
Yann Colletba75e9d2016-12-21 19:57:18 +0100377 if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
Yann Colletca1a9eb2017-10-18 11:18:27 -0700378 { int const initErr = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : ZSTD_CONTENTSIZE_UNKNOWN);
Yann Collet33a7e672017-06-02 17:10:49 -0700379 if (initErr != Z_OK) return ZWRAPC_finishWithError(zwc, strm, initErr); }
inikep6072eaa2016-09-27 15:24:44 +0200380 if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
inikepcf3ec082016-09-23 10:30:26 +0200381 } else {
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100382 if (zwc->totalInBytes == 0) {
inikep6072eaa2016-09-27 15:24:44 +0200383 if (zwc->comprState == ZWRAP_useReset) {
Stephen Kittadb54292021-02-20 17:28:19 +0100384 size_t resetErr = ZSTD_CCtx_reset(zwc->zbc, ZSTD_reset_session_only);
Yann Collet33a7e672017-06-02 17:10:49 -0700385 if (ZSTD_isError(resetErr)) {
Stephen Kittadb54292021-02-20 17:28:19 +0100386 LOG_WRAPPERC("ERROR: ZSTD_CCtx_reset errorCode=%s\n",
387 ZSTD_getErrorName(resetErr));
388 return ZWRAPC_finishWithError(zwc, strm, 0);
389 }
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",
Yann Collet33a7e672017-06-02 17:10:49 -0700393 ZSTD_getErrorName(resetErr));
394 return ZWRAPC_finishWithError(zwc, strm, 0);
395 }
inikep67a1f4d2016-09-26 20:49:18 +0200396 } else {
Yann Colletca1a9eb2017-10-18 11:18:27 -0700397 int const res = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : ZSTD_CONTENTSIZE_UNKNOWN);
inikep67a1f4d2016-09-26 20:49:18 +0200398 if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res);
inikep6072eaa2016-09-27 15:24:44 +0200399 if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
inikep67a1f4d2016-09-26 20:49:18 +0200400 }
Yann Collet33a7e672017-06-02 17:10:49 -0700401 } /* (zwc->totalInBytes == 0) */
402 } /* ! (zwc->zbc == NULL) */
inikep61abecc2016-09-21 19:30:29 +0200403
Przemyslaw Skibinskieee427e2016-12-13 19:14:04 +0100404 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 +0200405 if (strm->avail_in > 0) {
inikepb0773452016-09-16 14:06:10 +0200406 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;
Yann Collet33a7e672017-06-02 17:10:49 -0700412 { size_t const cErr = ZSTD_compressStream(zwc->zbc, &zwc->outBuffer, &zwc->inBuffer);
inikep554b3b92016-09-20 15:18:00 +0200413 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 -0700414 if (ZSTD_isError(cErr)) return ZWRAPC_finishWithError(zwc, strm, 0);
inikepb0773452016-09-16 14:06:10 +0200415 }
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;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100420 zwc->totalInBytes += zwc->inBuffer.pos;
inikepb0773452016-09-16 14:06:10 +0200421 strm->next_in += zwc->inBuffer.pos;
422 strm->avail_in -= zwc->inBuffer.pos;
inikep3eabe9b2016-05-12 17:15:41 +0200423 }
424
Yann Colletba75e9d2016-12-21 19:57:18 +0100425 if (flush == Z_FULL_FLUSH
Przemyslaw Skibinskiedd3e2a2016-11-28 12:46:16 +0100426#if ZLIB_VERNUM >= 0x1240
Yann Colletba75e9d2016-12-21 19:57:18 +0100427 || flush == Z_TREES
Przemyslaw Skibinskiedd3e2a2016-11-28 12:46:16 +0100428#endif
429 || flush == Z_BLOCK)
430 return ZWRAPC_finishWithErrorMsg(strm, "Z_FULL_FLUSH, Z_BLOCK and Z_TREES are not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200431
Dmitry Krot2ed3ba22016-08-13 22:02:45 +0300432 if (flush == Z_FINISH) {
inikep3eabe9b2016-05-12 17:15:41 +0200433 size_t bytesLeft;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100434 if (zwc->streamEnd) return Z_STREAM_END;
inikepb0773452016-09-16 14:06:10 +0200435 zwc->outBuffer.dst = strm->next_out;
436 zwc->outBuffer.size = strm->avail_out;
437 zwc->outBuffer.pos = 0;
inikepe46bad02016-09-19 13:24:07 +0200438 bytesLeft = ZSTD_endStream(zwc->zbc, &zwc->outBuffer);
inikep554b3b92016-09-20 15:18:00 +0200439 LOG_WRAPPERC("deflate ZSTD_endStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
inikepadc4c162016-09-21 19:39:25 +0200440 if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);
inikepb0773452016-09-16 14:06:10 +0200441 strm->next_out += zwc->outBuffer.pos;
442 strm->total_out += zwc->outBuffer.pos;
443 strm->avail_out -= zwc->outBuffer.pos;
Yann Collet33a7e672017-06-02 17:10:49 -0700444 if (bytesLeft == 0) {
445 zwc->streamEnd = 1;
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);
448 return Z_STREAM_END;
449 } }
inikepe46bad02016-09-19 13:24:07 +0200450 else
inikep61016872016-09-19 14:27:29 +0200451 if (flush == Z_SYNC_FLUSH || flush == Z_PARTIAL_FLUSH) {
Dmitry Krot2ed3ba22016-08-13 22:02:45 +0300452 size_t bytesLeft;
inikep8fc58482016-09-16 17:14:01 +0200453 zwc->outBuffer.dst = strm->next_out;
454 zwc->outBuffer.size = strm->avail_out;
455 zwc->outBuffer.pos = 0;
inikepb0773452016-09-16 14:06:10 +0200456 bytesLeft = ZSTD_flushStream(zwc->zbc, &zwc->outBuffer);
inikep554b3b92016-09-20 15:18:00 +0200457 LOG_WRAPPERC("deflate ZSTD_flushStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
inikepadc4c162016-09-21 19:39:25 +0200458 if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);
inikepb0773452016-09-16 14:06:10 +0200459 strm->next_out += zwc->outBuffer.pos;
460 strm->total_out += zwc->outBuffer.pos;
461 strm->avail_out -= zwc->outBuffer.pos;
Dmitry Krot2ed3ba22016-08-13 22:02:45 +0300462 }
Przemyslaw Skibinskieee427e2016-12-13 19:14:04 +0100463 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 +0200464 return Z_OK;
465}
466
467
Elliott Hughes44aba642023-09-12 20:18:59 +0000468ZEXTERN int ZEXPORT z_deflateEnd _Z_OF((z_streamp strm))
inikep3eabe9b2016-05-12 17:15:41 +0200469{
inikep252c20d2016-09-23 09:08:40 +0200470 if (!g_ZWRAP_useZSTDcompression) {
inikep554b3b92016-09-20 15:18:00 +0200471 LOG_WRAPPERC("- deflateEnd\n");
inikep3eabe9b2016-05-12 17:15:41 +0200472 return deflateEnd(strm);
473 }
inikep554b3b92016-09-20 15:18:00 +0200474 LOG_WRAPPERC("- deflateEnd total_in=%d total_out=%d\n", (int)(strm->total_in), (int)(strm->total_out));
inikep3fa1b742016-09-21 13:51:57 +0200475 { size_t errorCode;
476 ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
477 if (zwc == NULL) return Z_OK; /* structures are already freed */
inikepc4ab5712016-09-19 14:54:13 +0200478 strm->state = NULL;
inikep3fa1b742016-09-21 13:51:57 +0200479 errorCode = ZWRAP_freeCCtx(zwc);
inikep18f66452016-09-20 12:50:59 +0200480 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +0200481 }
482 return Z_OK;
483}
484
485
Elliott Hughes44aba642023-09-12 20:18:59 +0000486ZEXTERN uLong ZEXPORT z_deflateBound _Z_OF((z_streamp strm,
inikep3eabe9b2016-05-12 17:15:41 +0200487 uLong sourceLen))
488{
inikep252c20d2016-09-23 09:08:40 +0200489 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200490 return deflateBound(strm, sourceLen);
491
492 return ZSTD_compressBound(sourceLen);
493}
494
495
Elliott Hughes44aba642023-09-12 20:18:59 +0000496ZEXTERN int ZEXPORT z_deflateParams _Z_OF((z_streamp strm,
inikep3eabe9b2016-05-12 17:15:41 +0200497 int level,
498 int strategy))
499{
inikep252c20d2016-09-23 09:08:40 +0200500 if (!g_ZWRAP_useZSTDcompression) {
inikep554b3b92016-09-20 15:18:00 +0200501 LOG_WRAPPERC("- deflateParams level=%d strategy=%d\n", level, strategy);
inikep3eabe9b2016-05-12 17:15:41 +0200502 return deflateParams(strm, level, strategy);
503 }
504
505 return Z_OK;
506}
507
508
509
510
511
Yann Collet33a7e672017-06-02 17:10:49 -0700512/* === Decompression === */
513
inikep706876f2016-09-27 16:56:07 +0200514typedef enum { ZWRAP_ZLIB_STREAM, ZWRAP_ZSTD_STREAM, ZWRAP_UNKNOWN_STREAM } ZWRAP_stream_type;
inikep3eabe9b2016-05-12 17:15:41 +0200515
516typedef struct {
inikepb0773452016-09-16 14:06:10 +0200517 ZSTD_DStream* zbd;
Yann Collet33a7e672017-06-02 17:10:49 -0700518 char headerBuf[16]; /* must be >= ZSTD_frameHeaderSize_min */
inikep3eabe9b2016-05-12 17:15:41 +0200519 int errorCount;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100520 unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */
inikep706876f2016-09-27 16:56:07 +0200521 ZWRAP_state_t decompState;
inikep86fc8e02016-09-20 16:22:28 +0200522 ZSTD_inBuffer inBuffer;
523 ZSTD_outBuffer outBuffer;
Yann Collet4ded9e52016-08-30 10:04:33 -0700524
inikep3eabe9b2016-05-12 17:15:41 +0200525 /* zlib params */
526 int stream_size;
527 char *version;
528 int windowBits;
inikepf040be92016-06-03 16:31:57 +0200529 ZSTD_customMem customMem;
Yann Collet33a7e672017-06-02 17:10:49 -0700530 z_stream allocFunc; /* just to copy zalloc, zfree, opaque */
Przemyslaw Skibinski0694ae22016-11-04 16:05:28 +0100531} ZWRAP_DCtx;
inikep3eabe9b2016-05-12 17:15:41 +0200532
533
Yann Collet33a7e672017-06-02 17:10:49 -0700534static void ZWRAP_initDCtx(ZWRAP_DCtx* zwd)
535{
536 zwd->errorCount = 0;
537 zwd->outBuffer.pos = 0;
538 zwd->outBuffer.size = 0;
539}
540
541static ZWRAP_DCtx* ZWRAP_createDCtx(z_streamp strm)
542{
543 ZWRAP_DCtx* zwd;
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800544 ZSTD_customMem customMem = { NULL, NULL, NULL };
Yann Collet33a7e672017-06-02 17:10:49 -0700545
546 if (strm->zalloc && strm->zfree) {
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800547 customMem.customAlloc = ZWRAP_allocFunction;
548 customMem.customFree = ZWRAP_freeFunction;
Yann Collet33a7e672017-06-02 17:10:49 -0700549 }
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800550 customMem.opaque = strm;
551
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;
Yann Collet33a7e672017-06-02 17:10:49 -0700557
558 ZWRAP_initDCtx(zwd);
559 return zwd;
560}
561
562static size_t ZWRAP_freeDCtx(ZWRAP_DCtx* zwd)
563{
564 if (zwd==NULL) return 0; /* support free on null */
565 ZSTD_freeDStream(zwd->zbd);
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800566 ZWRAP_customFree(zwd->version, zwd->customMem);
567 ZWRAP_customFree(zwd, zwd->customMem);
Yann Collet33a7e672017-06-02 17:10:49 -0700568 return 0;
569}
570
571
Yann Colletba75e9d2016-12-21 19:57:18 +0100572int ZWRAP_isUsingZSTDdecompression(z_streamp strm)
inikep706876f2016-09-27 16:56:07 +0200573{
574 if (strm == NULL) return 0;
575 return (strm->reserved == ZWRAP_ZSTD_STREAM);
576}
577
578
Yann Collet33a7e672017-06-02 17:10:49 -0700579static int ZWRAPD_finishWithError(ZWRAP_DCtx* zwd, z_streamp strm, int error)
inikep0bb930b2016-09-19 14:31:16 +0200580{
inikepadc4c162016-09-21 19:39:25 +0200581 LOG_WRAPPERD("- ZWRAPD_finishWithError=%d\n", error);
Yann Collet33a7e672017-06-02 17:10:49 -0700582 ZWRAP_freeDCtx(zwd);
583 strm->state = NULL;
inikep18f66452016-09-20 12:50:59 +0200584 return (error) ? error : Z_STREAM_ERROR;
inikep0bb930b2016-09-19 14:31:16 +0200585}
586
Yann Collet33a7e672017-06-02 17:10:49 -0700587static int ZWRAPD_finishWithErrorMsg(z_streamp strm, char* message)
inikep146ef582016-09-21 14:05:01 +0200588{
Yann Collet33a7e672017-06-02 17:10:49 -0700589 ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
inikep146ef582016-09-21 14:05:01 +0200590 strm->msg = message;
591 if (zwd == NULL) return Z_STREAM_ERROR;
Yann Colletba75e9d2016-12-21 19:57:18 +0100592
inikepadc4c162016-09-21 19:39:25 +0200593 return ZWRAPD_finishWithError(zwd, strm, 0);
inikep146ef582016-09-21 14:05:01 +0200594}
595
596
Elliott Hughes44aba642023-09-12 20:18:59 +0000597ZEXTERN int ZEXPORT z_inflateInit_ _Z_OF((z_streamp strm,
Yann Collet69c94012019-09-26 15:01:29 -0700598 const char* version, int stream_size))
inikep3eabe9b2016-05-12 17:15:41 +0200599{
inikepd7557172016-09-22 11:52:00 +0200600 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
Yann Collet33a7e672017-06-02 17:10:49 -0700601 strm->reserved = ZWRAP_ZLIB_STREAM;
inikepd7557172016-09-22 11:52:00 +0200602 return inflateInit(strm);
603 }
604
Yann Collet33a7e672017-06-02 17:10:49 -0700605 { ZWRAP_DCtx* const zwd = ZWRAP_createDCtx(strm);
Yann Colletdcb75352017-06-02 14:01:21 -0700606 LOG_WRAPPERD("- inflateInit\n");
607 if (zwd == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
inikep3eabe9b2016-05-12 17:15:41 +0200608
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800609 zwd->version = (char*)ZWRAP_customMalloc(strlen(version)+1, zwd->customMem);
Yann Colletdcb75352017-06-02 14:01:21 -0700610 if (zwd->version == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
611 strcpy(zwd->version, version);
inikep3eabe9b2016-05-12 17:15:41 +0200612
Yann Colletdcb75352017-06-02 14:01:21 -0700613 zwd->stream_size = stream_size;
614 zwd->totalInBytes = 0;
Yann Collet24779682020-12-04 20:25:01 -0800615 strm->state = convert_into_sis(zwd);
Yann Colletdcb75352017-06-02 14:01:21 -0700616 strm->total_in = 0;
617 strm->total_out = 0;
Yann Collet33a7e672017-06-02 17:10:49 -0700618 strm->reserved = ZWRAP_UNKNOWN_STREAM;
Yann Colletdcb75352017-06-02 14:01:21 -0700619 strm->adler = 0;
inikepd7557172016-09-22 11:52:00 +0200620 }
inikep3eabe9b2016-05-12 17:15:41 +0200621
622 return Z_OK;
623}
624
625
Elliott Hughes44aba642023-09-12 20:18:59 +0000626ZEXTERN int ZEXPORT z_inflateInit2_ _Z_OF((z_streamp strm, int windowBits,
inikep3eabe9b2016-05-12 17:15:41 +0200627 const char *version, int stream_size))
628{
inikepd7557172016-09-22 11:52:00 +0200629 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
630 return inflateInit2_(strm, windowBits, version, stream_size);
631 }
Yann Colletba75e9d2016-12-21 19:57:18 +0100632
Yann Collet33a7e672017-06-02 17:10:49 -0700633 { int const ret = z_inflateInit_ (strm, version, stream_size);
Yann Colletdcb75352017-06-02 14:01:21 -0700634 LOG_WRAPPERD("- inflateInit2 windowBits=%d\n", windowBits);
635 if (ret == Z_OK) {
636 ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*)strm->state;
637 if (zwd == NULL) return Z_STREAM_ERROR;
638 zwd->windowBits = windowBits;
639 }
640 return ret;
inikepd7557172016-09-22 11:52:00 +0200641 }
inikep3eabe9b2016-05-12 17:15:41 +0200642}
643
inikep20859af2016-09-27 17:27:43 +0200644int ZWRAP_inflateReset_keepDict(z_streamp strm)
inikep18f66452016-09-20 12:50:59 +0200645{
inikep20859af2016-09-27 17:27:43 +0200646 LOG_WRAPPERD("- ZWRAP_inflateReset_keepDict\n");
inikep856f91e2016-09-27 17:14:04 +0200647 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
648 return inflateReset(strm);
649
Yann Colletdcb75352017-06-02 14:01:21 -0700650 { ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
inikep554b3b92016-09-20 15:18:00 +0200651 if (zwd == NULL) return Z_STREAM_ERROR;
inikep86fc8e02016-09-20 16:22:28 +0200652 ZWRAP_initDCtx(zwd);
inikep706876f2016-09-27 16:56:07 +0200653 zwd->decompState = ZWRAP_useReset;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100654 zwd->totalInBytes = 0;
inikep554b3b92016-09-20 15:18:00 +0200655 }
Yann Colletba75e9d2016-12-21 19:57:18 +0100656
inikep18f66452016-09-20 12:50:59 +0200657 strm->total_in = 0;
658 strm->total_out = 0;
659 return Z_OK;
660}
661
662
Elliott Hughes44aba642023-09-12 20:18:59 +0000663ZEXTERN int ZEXPORT z_inflateReset _Z_OF((z_streamp strm))
inikep706876f2016-09-27 16:56:07 +0200664{
665 LOG_WRAPPERD("- inflateReset\n");
666 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
667 return inflateReset(strm);
668
Yann Collet33a7e672017-06-02 17:10:49 -0700669 { int const ret = ZWRAP_inflateReset_keepDict(strm);
inikep706876f2016-09-27 16:56:07 +0200670 if (ret != Z_OK) return ret; }
671
Yann Collet33a7e672017-06-02 17:10:49 -0700672 { ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
Yann Colletba75e9d2016-12-21 19:57:18 +0100673 if (zwd == NULL) return Z_STREAM_ERROR;
inikep706876f2016-09-27 16:56:07 +0200674 zwd->decompState = ZWRAP_useInit; }
675
676 return Z_OK;
677}
678
679
inikep69413002016-09-20 16:40:50 +0200680#if ZLIB_VERNUM >= 0x1240
Elliott Hughes44aba642023-09-12 20:18:59 +0000681ZEXTERN int ZEXPORT z_inflateReset2 _Z_OF((z_streamp strm,
inikep69413002016-09-20 16:40:50 +0200682 int windowBits))
683{
inikepd7557172016-09-22 11:52:00 +0200684 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep69413002016-09-20 16:40:50 +0200685 return inflateReset2(strm, windowBits);
686
Yann Collet33a7e672017-06-02 17:10:49 -0700687 { int const ret = z_inflateReset (strm);
inikep69413002016-09-20 16:40:50 +0200688 if (ret == Z_OK) {
Yann Collet33a7e672017-06-02 17:10:49 -0700689 ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*)strm->state;
inikep69413002016-09-20 16:40:50 +0200690 if (zwd == NULL) return Z_STREAM_ERROR;
691 zwd->windowBits = windowBits;
692 }
693 return ret;
694 }
695}
696#endif
697
698
Elliott Hughes44aba642023-09-12 20:18:59 +0000699ZEXTERN int ZEXPORT z_inflateSetDictionary _Z_OF((z_streamp strm,
inikep3eabe9b2016-05-12 17:15:41 +0200700 const Bytef *dictionary,
701 uInt dictLength))
702{
inikep554b3b92016-09-20 15:18:00 +0200703 LOG_WRAPPERD("- inflateSetDictionary\n");
inikepd7557172016-09-22 11:52:00 +0200704 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +0200705 return inflateSetDictionary(strm, dictionary, dictLength);
706
Yann Collet33a7e672017-06-02 17:10:49 -0700707 { ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
inikepf7ab3ad2016-09-22 17:59:10 +0200708 if (zwd == NULL || zwd->zbd == NULL) return Z_STREAM_ERROR;
Elliott Hughes44aba642023-09-12 20:18:59 +0000709 { 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); }
Yann Colletba75e9d2016-12-21 19:57:18 +0100713 zwd->decompState = ZWRAP_useReset;
Yann Collet4ded9e52016-08-30 10:04:33 -0700714
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100715 if (zwd->totalInBytes == ZSTD_HEADERSIZE) {
inikepb0773452016-09-16 14:06:10 +0200716 zwd->inBuffer.src = zwd->headerBuf;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100717 zwd->inBuffer.size = zwd->totalInBytes;
inikepb0773452016-09-16 14:06:10 +0200718 zwd->inBuffer.pos = 0;
719 zwd->outBuffer.dst = strm->next_out;
720 zwd->outBuffer.size = 0;
721 zwd->outBuffer.pos = 0;
Yann Collet33a7e672017-06-02 17:10:49 -0700722 { 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);
729 } } } }
inikep3eabe9b2016-05-12 17:15:41 +0200730
731 return Z_OK;
732}
733
734
Elliott Hughes44aba642023-09-12 20:18:59 +0000735ZEXTERN int ZEXPORT z_inflate _Z_OF((z_streamp strm, int flush))
inikep3eabe9b2016-05-12 17:15:41 +0200736{
inikep57b97082016-09-23 14:59:46 +0200737 ZWRAP_DCtx* zwd;
Yann Collet33a7e672017-06-02 17:10:49 -0700738
inikepd7557172016-09-22 11:52:00 +0200739 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
Yann Collet33a7e672017-06-02 17:10:49 -0700740 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);
743 return result;
inikep554b3b92016-09-20 15:18:00 +0200744 }
inikep3eabe9b2016-05-12 17:15:41 +0200745
inikepe82c8112016-09-23 16:20:13 +0200746 if (strm->avail_in <= 0) return Z_OK;
747
Yann Collet33a7e672017-06-02 17:10:49 -0700748 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);
inikep86fc8e02016-09-20 16:22:28 +0200751
Yann Collet33a7e672017-06-02 17:10:49 -0700752 if (zwd == NULL) return Z_STREAM_ERROR;
753 if (zwd->decompState == ZWRAP_streamEnd) return Z_STREAM_END;
inikep86fc8e02016-09-20 16:22:28 +0200754
Yann Collet33a7e672017-06-02 17:10:49 -0700755 if (zwd->totalInBytes < ZLIB_HEADERSIZE) {
756 if (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) {
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800757 if (ZWRAP_readLE32(strm->next_in) != ZSTD_MAGICNUMBER) {
Yann Collet33a7e672017-06-02 17:10:49 -0700758 { 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);
inikep22e27302016-09-27 18:21:17 +0200763 }
inikep3eabe9b2016-05-12 17:15:41 +0200764
Yann Collet33a7e672017-06-02 17:10:49 -0700765 strm->reserved = ZWRAP_ZLIB_STREAM;
766 { size_t const freeErr = ZWRAP_freeDCtx(zwd);
767 if (ZSTD_isError(freeErr)) goto error; }
768
769 { int const result = (flush == Z_INFLATE_SYNC) ?
770 inflateSync(strm) :
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);
774 return result;
775 } }
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;
784
Nick Terrellb2ca26b2020-12-02 15:05:11 -0800785 if (ZWRAP_readLE32(zwd->headerBuf) != ZSTD_MAGICNUMBER) {
Yann Collet33a7e672017-06-02 17:10:49 -0700786 z_stream strm2;
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;
791
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);
797 }
798
799 /* inflate header */
800 strm->next_in = (unsigned char*)zwd->headerBuf;
801 strm->avail_in = ZLIB_HEADERSIZE;
802 strm->avail_out = 0;
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);
806 if (dErr != Z_OK)
807 return ZWRAPD_finishWithError(zwd, strm, dErr);
808 }
809 if (strm->avail_in > 0) goto error;
810
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;
815
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; }
819
820 { int const result = (flush == Z_INFLATE_SYNC) ?
821 inflateSync(strm) :
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);
825 return result;
826 } } } /* if ! (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) */
827 } /* (zwd->totalInBytes < ZLIB_HEADERSIZE) */
828
829 strm->reserved = ZWRAP_ZSTD_STREAM; /* mark as zstd steam */
830
831 if (flush == Z_INFLATE_SYNC) { strm->msg = "inflateSync is not supported!"; goto error; }
832
833 if (!zwd->zbd) {
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;
837 }
838
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));
inikep57b97082016-09-23 14:59:46 +0200846 goto error;
847 }
Yann Collet33a7e672017-06-02 17:10:49 -0700848 } else {
Stephen Kittadb54292021-02-20 17:28:19 +0100849 size_t const resetErr = ZSTD_DCtx_reset(zwd->zbd, ZSTD_reset_session_only);
Yann Collet33a7e672017-06-02 17:10:49 -0700850 if (ZSTD_isError(resetErr)) goto error;
inikep3eabe9b2016-05-12 17:15:41 +0200851 }
Yann Collet33a7e672017-06-02 17:10:49 -0700852 } else {
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;
inikep3eabe9b2016-05-12 17:15:41 +0200860
Yann Collet33a7e672017-06-02 17:10:49 -0700861 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));
866 goto error;
867 }
868 } else {
Stephen Kittadb54292021-02-20 17:28:19 +0100869 size_t const resetErr = ZSTD_DCtx_reset(zwd->zbd, ZSTD_reset_session_only);
Yann Collet33a7e672017-06-02 17:10:49 -0700870 if (ZSTD_isError(resetErr)) goto error;
871 }
872
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));
884 goto error;
885 } }
886 if (zwd->inBuffer.pos != zwd->inBuffer.size) goto error; /* not consumed */
887 }
888 } /* (zwd->totalInBytes < ZSTD_HEADERSIZE) */
889
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)) {
inikep3eabe9b2016-05-12 17:15:41 +0200900 zwd->errorCount++;
Yann Collet33a7e672017-06-02 17:10:49 -0700901 LOG_WRAPPERD("ERROR: ZSTD_decompressStream2 %s zwd->errorCount=%d\n",
902 ZSTD_getErrorName(dErr), zwd->errorCount);
inikep1c9521f2016-06-13 12:00:46 +0200903 if (zwd->errorCount<=1) return Z_NEED_DICT; else goto error;
inikep3eabe9b2016-05-12 17:15:41 +0200904 }
Yann Collet33a7e672017-06-02 17:10:49 -0700905 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);
inikepb0773452016-09-16 14:06:10 +0200907 strm->next_out += zwd->outBuffer.pos;
908 strm->total_out += zwd->outBuffer.pos;
909 strm->avail_out -= zwd->outBuffer.pos;
inikepf7ab3ad2016-09-22 17:59:10 +0200910 strm->total_in += zwd->inBuffer.pos;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100911 zwd->totalInBytes += zwd->inBuffer.pos;
inikepf7ab3ad2016-09-22 17:59:10 +0200912 strm->next_in += zwd->inBuffer.pos;
913 strm->avail_in -= zwd->inBuffer.pos;
Yann Collet33a7e672017-06-02 17:10:49 -0700914 if (dErr == 0) {
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);
Yann Colletba75e9d2016-12-21 19:57:18 +0100917 zwd->decompState = ZWRAP_streamEnd;
inikep86fc8e02016-09-20 16:22:28 +0200918 return Z_STREAM_END;
919 }
Yann Collet33a7e672017-06-02 17:10:49 -0700920 } /* dErr lifetime */
921
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);
inikep3eabe9b2016-05-12 17:15:41 +0200924 return Z_OK;
inikep57b97082016-09-23 14:59:46 +0200925
926error:
927 return ZWRAPD_finishWithError(zwd, strm, 0);
inikep3eabe9b2016-05-12 17:15:41 +0200928}
929
930
Elliott Hughes44aba642023-09-12 20:18:59 +0000931ZEXTERN int ZEXPORT z_inflateEnd _Z_OF((z_streamp strm))
inikep3eabe9b2016-05-12 17:15:41 +0200932{
inikepd7557172016-09-22 11:52:00 +0200933 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikepe02bf992016-06-02 12:00:32 +0200934 return inflateEnd(strm);
Yann Collet4ded9e52016-08-30 10:04:33 -0700935
Yann Collet33a7e672017-06-02 17:10:49 -0700936 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;
inikep3fa1b742016-09-21 13:51:57 +0200939 if (zwd == NULL) return Z_OK; /* structures are already freed */
Yann Collet33a7e672017-06-02 17:10:49 -0700940 { size_t const freeErr = ZWRAP_freeDCtx(zwd);
941 if (ZSTD_isError(freeErr)) return Z_STREAM_ERROR; }
inikep1c9521f2016-06-13 12:00:46 +0200942 strm->state = NULL;
inikep3eabe9b2016-05-12 17:15:41 +0200943 }
inikep3fa1b742016-09-21 13:51:57 +0200944 return Z_OK;
inikep3eabe9b2016-05-12 17:15:41 +0200945}
946
947
Elliott Hughes44aba642023-09-12 20:18:59 +0000948ZEXTERN int ZEXPORT z_inflateSync _Z_OF((z_streamp strm))
inikep3eabe9b2016-05-12 17:15:41 +0200949{
inikepd7557172016-09-22 11:52:00 +0200950 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
inikep18f66452016-09-20 12:50:59 +0200951 return inflateSync(strm);
952 }
inikep61016872016-09-19 14:27:29 +0200953
inikep3eabe9b2016-05-12 17:15:41 +0200954 return z_inflate(strm, Z_INFLATE_SYNC);
955}
956
957
958
inikep3eabe9b2016-05-12 17:15:41 +0200959/* Advanced compression functions */
Elliott Hughes44aba642023-09-12 20:18:59 +0000960ZEXTERN int ZEXPORT z_deflateCopy _Z_OF((z_streamp dest,
inikep3eabe9b2016-05-12 17:15:41 +0200961 z_streamp source))
962{
inikep252c20d2016-09-23 09:08:40 +0200963 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200964 return deflateCopy(dest, source);
inikepadc4c162016-09-21 19:39:25 +0200965 return ZWRAPC_finishWithErrorMsg(source, "deflateCopy is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200966}
967
968
Elliott Hughes44aba642023-09-12 20:18:59 +0000969ZEXTERN int ZEXPORT z_deflateTune _Z_OF((z_streamp strm,
inikep3eabe9b2016-05-12 17:15:41 +0200970 int good_length,
971 int max_lazy,
972 int nice_length,
973 int max_chain))
974{
inikep252c20d2016-09-23 09:08:40 +0200975 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200976 return deflateTune(strm, good_length, max_lazy, nice_length, max_chain);
inikepadc4c162016-09-21 19:39:25 +0200977 return ZWRAPC_finishWithErrorMsg(strm, "deflateTune is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200978}
979
980
inikepbf25d7a2016-06-02 10:19:35 +0200981#if ZLIB_VERNUM >= 0x1260
Elliott Hughes44aba642023-09-12 20:18:59 +0000982ZEXTERN int ZEXPORT z_deflatePending _Z_OF((z_streamp strm,
inikep3eabe9b2016-05-12 17:15:41 +0200983 unsigned *pending,
984 int *bits))
985{
inikep252c20d2016-09-23 09:08:40 +0200986 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200987 return deflatePending(strm, pending, bits);
inikepadc4c162016-09-21 19:39:25 +0200988 return ZWRAPC_finishWithErrorMsg(strm, "deflatePending is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200989}
inikepbf25d7a2016-06-02 10:19:35 +0200990#endif
inikep3eabe9b2016-05-12 17:15:41 +0200991
992
Elliott Hughes44aba642023-09-12 20:18:59 +0000993ZEXTERN int ZEXPORT z_deflatePrime _Z_OF((z_streamp strm,
inikep3eabe9b2016-05-12 17:15:41 +0200994 int bits,
995 int value))
996{
inikep252c20d2016-09-23 09:08:40 +0200997 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200998 return deflatePrime(strm, bits, value);
inikepadc4c162016-09-21 19:39:25 +0200999 return ZWRAPC_finishWithErrorMsg(strm, "deflatePrime is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001000}
1001
1002
Elliott Hughes44aba642023-09-12 20:18:59 +00001003ZEXTERN int ZEXPORT z_deflateSetHeader _Z_OF((z_streamp strm,
inikep3eabe9b2016-05-12 17:15:41 +02001004 gz_headerp head))
1005{
inikep252c20d2016-09-23 09:08:40 +02001006 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001007 return deflateSetHeader(strm, head);
inikepadc4c162016-09-21 19:39:25 +02001008 return ZWRAPC_finishWithErrorMsg(strm, "deflateSetHeader is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001009}
1010
1011
1012
1013
inikep61016872016-09-19 14:27:29 +02001014/* Advanced decompression functions */
inikepbf25d7a2016-06-02 10:19:35 +02001015#if ZLIB_VERNUM >= 0x1280
Elliott Hughes44aba642023-09-12 20:18:59 +00001016ZEXTERN int ZEXPORT z_inflateGetDictionary _Z_OF((z_streamp strm,
inikep3eabe9b2016-05-12 17:15:41 +02001017 Bytef *dictionary,
1018 uInt *dictLength))
1019{
inikepd7557172016-09-22 11:52:00 +02001020 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +02001021 return inflateGetDictionary(strm, dictionary, dictLength);
inikepadc4c162016-09-21 19:39:25 +02001022 return ZWRAPD_finishWithErrorMsg(strm, "inflateGetDictionary is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001023}
inikepbf25d7a2016-06-02 10:19:35 +02001024#endif
inikep3eabe9b2016-05-12 17:15:41 +02001025
1026
Elliott Hughes44aba642023-09-12 20:18:59 +00001027ZEXTERN int ZEXPORT z_inflateCopy _Z_OF((z_streamp dest,
inikep3eabe9b2016-05-12 17:15:41 +02001028 z_streamp source))
1029{
inikepd7557172016-09-22 11:52:00 +02001030 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !source->reserved)
inikep3eabe9b2016-05-12 17:15:41 +02001031 return inflateCopy(dest, source);
inikepadc4c162016-09-21 19:39:25 +02001032 return ZWRAPD_finishWithErrorMsg(source, "inflateCopy is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001033}
1034
1035
inikepbf25d7a2016-06-02 10:19:35 +02001036#if ZLIB_VERNUM >= 0x1240
Elliott Hughes44aba642023-09-12 20:18:59 +00001037ZEXTERN long ZEXPORT z_inflateMark _Z_OF((z_streamp strm))
inikepbf25d7a2016-06-02 10:19:35 +02001038{
inikepd7557172016-09-22 11:52:00 +02001039 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikepbf25d7a2016-06-02 10:19:35 +02001040 return inflateMark(strm);
inikepadc4c162016-09-21 19:39:25 +02001041 return ZWRAPD_finishWithErrorMsg(strm, "inflateMark is not supported!");
inikepbf25d7a2016-06-02 10:19:35 +02001042}
1043#endif
inikep3eabe9b2016-05-12 17:15:41 +02001044
1045
Elliott Hughes44aba642023-09-12 20:18:59 +00001046ZEXTERN int ZEXPORT z_inflatePrime _Z_OF((z_streamp strm,
inikep3eabe9b2016-05-12 17:15:41 +02001047 int bits,
1048 int value))
1049{
inikepd7557172016-09-22 11:52:00 +02001050 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +02001051 return inflatePrime(strm, bits, value);
inikepadc4c162016-09-21 19:39:25 +02001052 return ZWRAPD_finishWithErrorMsg(strm, "inflatePrime is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001053}
1054
1055
Elliott Hughes44aba642023-09-12 20:18:59 +00001056ZEXTERN int ZEXPORT z_inflateGetHeader _Z_OF((z_streamp strm,
inikep3eabe9b2016-05-12 17:15:41 +02001057 gz_headerp head))
1058{
inikepd7557172016-09-22 11:52:00 +02001059 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +02001060 return inflateGetHeader(strm, head);
inikepadc4c162016-09-21 19:39:25 +02001061 return ZWRAPD_finishWithErrorMsg(strm, "inflateGetHeader is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001062}
1063
1064
Elliott Hughes44aba642023-09-12 20:18:59 +00001065ZEXTERN int ZEXPORT z_inflateBackInit_ _Z_OF((z_streamp strm, int windowBits,
inikep3eabe9b2016-05-12 17:15:41 +02001066 unsigned char FAR *window,
1067 const char *version,
1068 int stream_size))
1069{
inikepd7557172016-09-22 11:52:00 +02001070 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +02001071 return inflateBackInit_(strm, windowBits, window, version, stream_size);
inikepadc4c162016-09-21 19:39:25 +02001072 return ZWRAPD_finishWithErrorMsg(strm, "inflateBackInit is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001073}
1074
1075
Elliott Hughes44aba642023-09-12 20:18:59 +00001076ZEXTERN int ZEXPORT z_inflateBack _Z_OF((z_streamp strm,
inikep3eabe9b2016-05-12 17:15:41 +02001077 in_func in, void FAR *in_desc,
1078 out_func out, void FAR *out_desc))
1079{
inikepd7557172016-09-22 11:52:00 +02001080 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +02001081 return inflateBack(strm, in, in_desc, out, out_desc);
inikepadc4c162016-09-21 19:39:25 +02001082 return ZWRAPD_finishWithErrorMsg(strm, "inflateBack is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001083}
1084
1085
Elliott Hughes44aba642023-09-12 20:18:59 +00001086ZEXTERN int ZEXPORT z_inflateBackEnd _Z_OF((z_streamp strm))
inikep3eabe9b2016-05-12 17:15:41 +02001087{
inikepd7557172016-09-22 11:52:00 +02001088 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +02001089 return inflateBackEnd(strm);
inikepadc4c162016-09-21 19:39:25 +02001090 return ZWRAPD_finishWithErrorMsg(strm, "inflateBackEnd is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001091}
1092
1093
Elliott Hughes44aba642023-09-12 20:18:59 +00001094ZEXTERN uLong ZEXPORT z_zlibCompileFlags _Z_OF((void)) { return zlibCompileFlags(); }
inikep3eabe9b2016-05-12 17:15:41 +02001095
1096
1097
Yann Collet33a7e672017-06-02 17:10:49 -07001098 /* === utility functions === */
inikep3eabe9b2016-05-12 17:15:41 +02001099#ifndef Z_SOLO
1100
Elliott Hughes44aba642023-09-12 20:18:59 +00001101ZEXTERN int ZEXPORT z_compress _Z_OF((Bytef *dest, uLongf *destLen,
inikep3eabe9b2016-05-12 17:15:41 +02001102 const Bytef *source, uLong sourceLen))
1103{
inikep252c20d2016-09-23 09:08:40 +02001104 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001105 return compress(dest, destLen, source, sourceLen);
1106
Yann Collet33a7e672017-06-02 17:10:49 -07001107 { size_t dstCapacity = *destLen;
1108 size_t const cSize = ZSTD_compress(dest, dstCapacity,
1109 source, sourceLen,
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;
1114 *destLen = cSize;
inikep3eabe9b2016-05-12 17:15:41 +02001115 }
1116 return Z_OK;
1117}
1118
1119
Elliott Hughes44aba642023-09-12 20:18:59 +00001120ZEXTERN int ZEXPORT z_compress2 _Z_OF((Bytef *dest, uLongf *destLen,
inikep3eabe9b2016-05-12 17:15:41 +02001121 const Bytef *source, uLong sourceLen,
1122 int level))
1123{
inikep252c20d2016-09-23 09:08:40 +02001124 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001125 return compress2(dest, destLen, source, sourceLen, level);
Yann Collet4ded9e52016-08-30 10:04:33 -07001126
1127 { size_t dstCapacity = *destLen;
Yann Collet4effccb2017-06-02 14:24:58 -07001128 size_t const cSize = ZSTD_compress(dest, dstCapacity, source, sourceLen, level);
1129 if (ZSTD_isError(cSize)) return Z_STREAM_ERROR;
1130 *destLen = cSize;
inikep3eabe9b2016-05-12 17:15:41 +02001131 }
1132 return Z_OK;
1133}
1134
1135
Elliott Hughes44aba642023-09-12 20:18:59 +00001136ZEXTERN uLong ZEXPORT z_compressBound _Z_OF((uLong sourceLen))
inikep3eabe9b2016-05-12 17:15:41 +02001137{
inikep252c20d2016-09-23 09:08:40 +02001138 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001139 return compressBound(sourceLen);
1140
1141 return ZSTD_compressBound(sourceLen);
1142}
1143
1144
Elliott Hughes44aba642023-09-12 20:18:59 +00001145ZEXTERN int ZEXPORT z_uncompress _Z_OF((Bytef *dest, uLongf *destLen,
inikep3eabe9b2016-05-12 17:15:41 +02001146 const Bytef *source, uLong sourceLen))
1147{
Yann Collet4effccb2017-06-02 14:24:58 -07001148 if (!ZSTD_isFrame(source, sourceLen))
inikep3eabe9b2016-05-12 17:15:41 +02001149 return uncompress(dest, destLen, source, sourceLen);
1150
Yann Collet4ded9e52016-08-30 10:04:33 -07001151 { size_t dstCapacity = *destLen;
Yann Collet4effccb2017-06-02 14:24:58 -07001152 size_t const dSize = ZSTD_decompress(dest, dstCapacity, source, sourceLen);
1153 if (ZSTD_isError(dSize)) return Z_STREAM_ERROR;
1154 *destLen = dSize;
inikep3eabe9b2016-05-12 17:15:41 +02001155 }
1156 return Z_OK;
1157}
1158
inikep3eabe9b2016-05-12 17:15:41 +02001159#endif /* !Z_SOLO */
1160
1161
1162 /* checksum functions */
1163
Elliott Hughes44aba642023-09-12 20:18:59 +00001164ZEXTERN uLong ZEXPORT z_adler32 _Z_OF((uLong adler, const Bytef *buf, uInt len))
inikep3eabe9b2016-05-12 17:15:41 +02001165{
1166 return adler32(adler, buf, len);
1167}
1168
Elliott Hughes44aba642023-09-12 20:18:59 +00001169ZEXTERN uLong ZEXPORT z_crc32 _Z_OF((uLong crc, const Bytef *buf, uInt len))
inikep3eabe9b2016-05-12 17:15:41 +02001170{
1171 return crc32(crc, buf, len);
1172}
Przemyslaw Skibinski5b114d32017-01-17 13:02:06 +01001173
Przemyslaw Skibinskic9512db2017-01-18 12:51:44 +01001174
1175#if ZLIB_VERNUM >= 0x12B0
Elliott Hughes44aba642023-09-12 20:18:59 +00001176ZEXTERN uLong ZEXPORT z_adler32_z _Z_OF((uLong adler, const Bytef *buf, z_size_t len))
Przemyslaw Skibinskic9512db2017-01-18 12:51:44 +01001177{
1178 return adler32_z(adler, buf, len);
1179}
1180
Elliott Hughes44aba642023-09-12 20:18:59 +00001181ZEXTERN uLong ZEXPORT z_crc32_z _Z_OF((uLong crc, const Bytef *buf, z_size_t len))
Przemyslaw Skibinskic9512db2017-01-18 12:51:44 +01001182{
1183 return crc32_z(crc, buf, len);
1184}
1185#endif
1186
1187
Przemyslaw Skibinski5edab912017-01-18 10:39:39 +01001188#if ZLIB_VERNUM >= 0x1270
Elliott Hughes44aba642023-09-12 20:18:59 +00001189ZEXTERN const z_crc_t FAR * ZEXPORT z_get_crc_table _Z_OF((void))
Przemyslaw Skibinski5b114d32017-01-17 13:02:06 +01001190{
1191 return get_crc_table();
1192}
Przemyslaw Skibinski5edab912017-01-18 10:39:39 +01001193#endif
koalae00412f2021-06-11 19:29:27 +08001194
1195 /* Error function */
Elliott Hughes44aba642023-09-12 20:18:59 +00001196ZEXTERN const char * ZEXPORT z_zError _Z_OF((int err))
koalae00412f2021-06-11 19:29:27 +08001197{
1198 /* Just use zlib Error function */
1199 return zError(err);
1200}