blob: a078cf556a4c6b17963ef94b50a003b94001da39 [file] [log] [blame]
Yann Collet4ded9e52016-08-30 10:04:33 -07001/**
2 * Copyright (c) 2016-present, Przemyslaw Skibinski, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 */
inikep3eabe9b2016-05-12 17:15:41 +02009
inikep3eabe9b2016-05-12 17:15:41 +020010
Yann Colletae728a42017-05-30 17:11:39 -070011#include <stdlib.h>
inikep3d2c58c2016-08-10 14:28:47 +020012#include <stdio.h> /* vsprintf */
13#include <stdarg.h> /* va_list, for z_gzprintf */
Przemyslaw Skibinski0694ae22016-11-04 16:05:28 +010014#define NO_DUMMY_DECL
Przemyslaw Skibinski6cecb352016-11-04 17:49:17 +010015#define ZLIB_CONST
Przemyslaw Skibinski0694ae22016-11-04 16:05:28 +010016#include <zlib.h> /* without #define Z_PREFIX */
inikep3eabe9b2016-05-12 17:15:41 +020017#include "zstd_zlibwrapper.h"
Yann Collet16f72992016-06-04 19:52:06 +020018#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_MAGICNUMBER */
inikep3eabe9b2016-05-12 17:15:41 +020019#include "zstd.h"
inikep3d2c58c2016-08-10 14:28:47 +020020#include "zstd_internal.h" /* defaultCustomMem */
inikep3eabe9b2016-05-12 17:15:41 +020021
22
23#define Z_INFLATE_SYNC 8
inikep8fc58482016-09-16 17:14:01 +020024#define ZLIB_HEADERSIZE 4
25#define ZSTD_HEADERSIZE ZSTD_frameHeaderSize_min
inikep67a1f4d2016-09-26 20:49:18 +020026#define ZWRAP_DEFAULT_CLEVEL 3 /* Z_DEFAULT_COMPRESSION is translated to ZWRAP_DEFAULT_CLEVEL for zstd */
inikep3eabe9b2016-05-12 17:15:41 +020027
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010028#define LOG_WRAPPERC(...) /* printf(__VA_ARGS__) */
29#define LOG_WRAPPERD(...) /* printf(__VA_ARGS__) */
inikep3eabe9b2016-05-12 17:15:41 +020030
inikepcd2f6b62016-09-23 20:03:17 +020031#define FINISH_WITH_GZ_ERR(msg) { (void)msg; return Z_STREAM_ERROR; }
32#define FINISH_WITH_NULL_ERR(msg) { (void)msg; return NULL; }
inikepd7557172016-09-22 11:52:00 +020033
34
35
inikep3eabe9b2016-05-12 17:15:41 +020036#ifndef ZWRAP_USE_ZSTD
37 #define ZWRAP_USE_ZSTD 0
38#endif
39
inikep252c20d2016-09-23 09:08:40 +020040static int g_ZWRAP_useZSTDcompression = ZWRAP_USE_ZSTD; /* 0 = don't use ZSTD */
inikepd7557172016-09-22 11:52:00 +020041
inikep252c20d2016-09-23 09:08:40 +020042void ZWRAP_useZSTDcompression(int turn_on) { g_ZWRAP_useZSTDcompression = turn_on; }
inikepd7557172016-09-22 11:52:00 +020043
inikep252c20d2016-09-23 09:08:40 +020044int ZWRAP_isUsingZSTDcompression(void) { return g_ZWRAP_useZSTDcompression; }
inikep3eabe9b2016-05-12 17:15:41 +020045
46
47
inikepd7557172016-09-22 11:52:00 +020048static ZWRAP_decompress_type g_ZWRAPdecompressionType = ZWRAP_AUTO;
inikep3eabe9b2016-05-12 17:15:41 +020049
inikep252c20d2016-09-23 09:08:40 +020050void ZWRAP_setDecompressionType(ZWRAP_decompress_type type) { g_ZWRAPdecompressionType = type; };
inikep3eabe9b2016-05-12 17:15:41 +020051
inikep252c20d2016-09-23 09:08:40 +020052ZWRAP_decompress_type ZWRAP_getDecompressionType(void) { return g_ZWRAPdecompressionType; }
inikep3eabe9b2016-05-12 17:15:41 +020053
inikep3eabe9b2016-05-12 17:15:41 +020054
55
inikepcd2f6b62016-09-23 20:03:17 +020056const char * zstdVersion(void) { return ZSTD_VERSION_STRING; }
57
58ZEXTERN const char * ZEXPORT z_zlibVersion OF((void)) { return zlibVersion(); }
59
60
inikep230a61f2016-09-21 16:46:35 +020061
inikep7cab86f2016-06-02 18:24:07 +020062static void* ZWRAP_allocFunction(void* opaque, size_t size)
inikepff9114a2016-06-02 16:52:36 +020063{
64 z_streamp strm = (z_streamp) opaque;
Yann Collet9ceb49e2016-12-22 15:26:33 +010065 void* address = strm->zalloc(strm->opaque, 1, (uInt)size);
inikepff9114a2016-06-02 16:52:36 +020066 /* printf("ZWRAP alloc %p, %d \n", address, (int)size); */
67 return address;
68}
69
inikep7cab86f2016-06-02 18:24:07 +020070static void ZWRAP_freeFunction(void* opaque, void* address)
inikepff9114a2016-06-02 16:52:36 +020071{
72 z_streamp strm = (z_streamp) opaque;
73 strm->zfree(strm->opaque, address);
74 /* if (address) printf("ZWRAP free %p \n", address); */
75}
76
77
inikep3eabe9b2016-05-12 17:15:41 +020078
79/* *** Compression *** */
inikep706876f2016-09-27 16:56:07 +020080typedef enum { ZWRAP_useInit, ZWRAP_useReset, ZWRAP_streamEnd } ZWRAP_state_t;
inikep3eabe9b2016-05-12 17:15:41 +020081
82typedef struct {
inikepb0773452016-09-16 14:06:10 +020083 ZSTD_CStream* zbc;
inikep3eabe9b2016-05-12 17:15:41 +020084 int compressionLevel;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +010085 int streamEnd; /* a flag to signal the end of a stream */
86 unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */
inikep2a746092016-06-03 14:53:51 +020087 ZSTD_customMem customMem;
inikepff9114a2016-06-02 16:52:36 +020088 z_stream allocFunc; /* copy of zalloc, zfree, opaque */
inikepb0773452016-09-16 14:06:10 +020089 ZSTD_inBuffer inBuffer;
90 ZSTD_outBuffer outBuffer;
inikep706876f2016-09-27 16:56:07 +020091 ZWRAP_state_t comprState;
inikep230a61f2016-09-21 16:46:35 +020092 unsigned long long pledgedSrcSize;
Przemyslaw Skibinski0694ae22016-11-04 16:05:28 +010093} ZWRAP_CCtx;
94
95typedef ZWRAP_CCtx internal_state;
96
inikep3eabe9b2016-05-12 17:15:41 +020097
98
inikepf040be92016-06-03 16:31:57 +020099size_t ZWRAP_freeCCtx(ZWRAP_CCtx* zwc)
100{
101 if (zwc==NULL) return 0; /* support free on NULL */
inikep61abecc2016-09-21 19:30:29 +0200102 if (zwc->zbc) ZSTD_freeCStream(zwc->zbc);
inikepf040be92016-06-03 16:31:57 +0200103 zwc->customMem.customFree(zwc->customMem.opaque, zwc);
104 return 0;
105}
106
107
inikepff9114a2016-06-02 16:52:36 +0200108ZWRAP_CCtx* ZWRAP_createCCtx(z_streamp strm)
inikep3eabe9b2016-05-12 17:15:41 +0200109{
inikep2a746092016-06-03 14:53:51 +0200110 ZWRAP_CCtx* zwc;
111
inikepff9114a2016-06-02 16:52:36 +0200112 if (strm->zalloc && strm->zfree) {
inikep2a746092016-06-03 14:53:51 +0200113 zwc = (ZWRAP_CCtx*)strm->zalloc(strm->opaque, 1, sizeof(ZWRAP_CCtx));
114 if (zwc==NULL) return NULL;
115 memset(zwc, 0, sizeof(ZWRAP_CCtx));
116 memcpy(&zwc->allocFunc, strm, sizeof(z_stream));
117 { ZSTD_customMem ZWRAP_customMem = { ZWRAP_allocFunction, ZWRAP_freeFunction, &zwc->allocFunc };
118 memcpy(&zwc->customMem, &ZWRAP_customMem, sizeof(ZSTD_customMem));
119 }
120 } else {
Yann Colletae728a42017-05-30 17:11:39 -0700121 zwc = (ZWRAP_CCtx*)calloc(1, sizeof(*zwc));
inikep2a746092016-06-03 14:53:51 +0200122 if (zwc==NULL) return NULL;
inikepff9114a2016-06-02 16:52:36 +0200123 }
inikep2a746092016-06-03 14:53:51 +0200124
inikep3eabe9b2016-05-12 17:15:41 +0200125 return zwc;
126}
127
128
inikep67a1f4d2016-09-26 20:49:18 +0200129int ZWRAP_initializeCStream(ZWRAP_CCtx* zwc, const void* dict, size_t dictSize, unsigned long long pledgedSrcSize)
inikep61abecc2016-09-21 19:30:29 +0200130{
inikep2bb83e82016-09-23 18:59:53 +0200131 LOG_WRAPPERC("- ZWRAP_initializeCStream=%p\n", zwc);
inikep67a1f4d2016-09-26 20:49:18 +0200132 if (zwc == NULL || zwc->zbc == NULL) return Z_STREAM_ERROR;
Yann Colletba75e9d2016-12-21 19:57:18 +0100133
inikep67a1f4d2016-09-26 20:49:18 +0200134 if (!pledgedSrcSize) pledgedSrcSize = zwc->pledgedSrcSize;
135 { ZSTD_parameters const params = ZSTD_getParams(zwc->compressionLevel, pledgedSrcSize, dictSize);
136 size_t errorCode;
137 LOG_WRAPPERC("pledgedSrcSize=%d windowLog=%d chainLog=%d hashLog=%d searchLog=%d searchLength=%d strategy=%d\n", (int)pledgedSrcSize, params.cParams.windowLog, params.cParams.chainLog, params.cParams.hashLog, params.cParams.searchLog, params.cParams.searchLength, params.cParams.strategy);
138 errorCode = ZSTD_initCStream_advanced(zwc->zbc, dict, dictSize, params, pledgedSrcSize);
139 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR; }
inikep8e8b0462016-09-22 14:42:32 +0200140
inikep61abecc2016-09-21 19:30:29 +0200141 return Z_OK;
142}
143
144
inikepadc4c162016-09-21 19:39:25 +0200145int ZWRAPC_finishWithError(ZWRAP_CCtx* zwc, z_streamp strm, int error)
inikepc4ab5712016-09-19 14:54:13 +0200146{
inikepadc4c162016-09-21 19:39:25 +0200147 LOG_WRAPPERC("- ZWRAPC_finishWithError=%d\n", error);
inikepc4ab5712016-09-19 14:54:13 +0200148 if (zwc) ZWRAP_freeCCtx(zwc);
149 if (strm) strm->state = NULL;
inikep18f66452016-09-20 12:50:59 +0200150 return (error) ? error : Z_STREAM_ERROR;
inikepc4ab5712016-09-19 14:54:13 +0200151}
152
153
inikepadc4c162016-09-21 19:39:25 +0200154int ZWRAPC_finishWithErrorMsg(z_streamp strm, char* message)
inikep146ef582016-09-21 14:05:01 +0200155{
156 ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
157 strm->msg = message;
158 if (zwc == NULL) return Z_STREAM_ERROR;
Yann Colletba75e9d2016-12-21 19:57:18 +0100159
inikepadc4c162016-09-21 19:39:25 +0200160 return ZWRAPC_finishWithError(zwc, strm, 0);
inikep146ef582016-09-21 14:05:01 +0200161}
162
163
inikep252c20d2016-09-23 09:08:40 +0200164int ZWRAP_setPledgedSrcSize(z_streamp strm, unsigned long long pledgedSrcSize)
inikep230a61f2016-09-21 16:46:35 +0200165{
166 ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
167 if (zwc == NULL) return Z_STREAM_ERROR;
Yann Colletba75e9d2016-12-21 19:57:18 +0100168
inikep230a61f2016-09-21 16:46:35 +0200169 zwc->pledgedSrcSize = pledgedSrcSize;
inikep6072eaa2016-09-27 15:24:44 +0200170 zwc->comprState = ZWRAP_useInit;
inikep230a61f2016-09-21 16:46:35 +0200171 return Z_OK;
172}
173
174
inikep3eabe9b2016-05-12 17:15:41 +0200175ZEXTERN int ZEXPORT z_deflateInit_ OF((z_streamp strm, int level,
176 const char *version, int stream_size))
177{
178 ZWRAP_CCtx* zwc;
Yann Collet4ded9e52016-08-30 10:04:33 -0700179
inikep554b3b92016-09-20 15:18:00 +0200180 LOG_WRAPPERC("- deflateInit level=%d\n", level);
inikep252c20d2016-09-23 09:08:40 +0200181 if (!g_ZWRAP_useZSTDcompression) {
inikep3eabe9b2016-05-12 17:15:41 +0200182 return deflateInit_((strm), (level), version, stream_size);
183 }
184
inikepff9114a2016-06-02 16:52:36 +0200185 zwc = ZWRAP_createCCtx(strm);
inikep3eabe9b2016-05-12 17:15:41 +0200186 if (zwc == NULL) return Z_MEM_ERROR;
187
inikep8b452452016-06-01 10:50:17 +0200188 if (level == Z_DEFAULT_COMPRESSION)
189 level = ZWRAP_DEFAULT_CLEVEL;
190
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100191 zwc->streamEnd = 0;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100192 zwc->totalInBytes = 0;
inikep3eabe9b2016-05-12 17:15:41 +0200193 zwc->compressionLevel = level;
inikepe02bf992016-06-02 12:00:32 +0200194 strm->state = (struct internal_state*) zwc; /* use state which in not used by user */
inikep3eabe9b2016-05-12 17:15:41 +0200195 strm->total_in = 0;
196 strm->total_out = 0;
inikep68cd4762016-09-23 12:42:21 +0200197 strm->adler = 0;
inikep3eabe9b2016-05-12 17:15:41 +0200198 return Z_OK;
199}
200
201
inikep8b452452016-06-01 10:50:17 +0200202ZEXTERN int ZEXPORT z_deflateInit2_ OF((z_streamp strm, int level, int method,
inikep3eabe9b2016-05-12 17:15:41 +0200203 int windowBits, int memLevel,
204 int strategy, const char *version,
205 int stream_size))
206{
inikep252c20d2016-09-23 09:08:40 +0200207 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200208 return deflateInit2_(strm, level, method, windowBits, memLevel, strategy, version, stream_size);
209
210 return z_deflateInit_ (strm, level, version, stream_size);
211}
212
213
inikep20859af2016-09-27 17:27:43 +0200214int ZWRAP_deflateReset_keepDict(z_streamp strm)
inikep706876f2016-09-27 16:56:07 +0200215{
inikep20859af2016-09-27 17:27:43 +0200216 LOG_WRAPPERC("- ZWRAP_deflateReset_keepDict\n");
inikep856f91e2016-09-27 17:14:04 +0200217 if (!g_ZWRAP_useZSTDcompression)
218 return deflateReset(strm);
219
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100220 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
Yann Colletae728a42017-05-30 17:11:39 -0700221 if (zwc) {
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100222 zwc->streamEnd = 0;
223 zwc->totalInBytes = 0;
224 }
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100225 }
226
inikep706876f2016-09-27 16:56:07 +0200227 strm->total_in = 0;
228 strm->total_out = 0;
229 strm->adler = 0;
230 return Z_OK;
231}
232
233
inikep61016872016-09-19 14:27:29 +0200234ZEXTERN int ZEXPORT z_deflateReset OF((z_streamp strm))
235{
inikep554b3b92016-09-20 15:18:00 +0200236 LOG_WRAPPERC("- deflateReset\n");
inikep252c20d2016-09-23 09:08:40 +0200237 if (!g_ZWRAP_useZSTDcompression)
inikep61016872016-09-19 14:27:29 +0200238 return deflateReset(strm);
Yann Colletba75e9d2016-12-21 19:57:18 +0100239
inikep20859af2016-09-27 17:27:43 +0200240 ZWRAP_deflateReset_keepDict(strm);
inikep706876f2016-09-27 16:56:07 +0200241
242 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100243 if (zwc) zwc->comprState = ZWRAP_useInit;
inikep706876f2016-09-27 16:56:07 +0200244 }
inikepc038c302016-09-20 12:54:26 +0200245 return Z_OK;
inikep61016872016-09-19 14:27:29 +0200246}
247
248
inikep3eabe9b2016-05-12 17:15:41 +0200249ZEXTERN int ZEXPORT z_deflateSetDictionary OF((z_streamp strm,
250 const Bytef *dictionary,
251 uInt dictLength))
252{
inikep252c20d2016-09-23 09:08:40 +0200253 if (!g_ZWRAP_useZSTDcompression) {
inikep554b3b92016-09-20 15:18:00 +0200254 LOG_WRAPPERC("- deflateSetDictionary\n");
inikep3eabe9b2016-05-12 17:15:41 +0200255 return deflateSetDictionary(strm, dictionary, dictLength);
inikep554b3b92016-09-20 15:18:00 +0200256 }
inikep3eabe9b2016-05-12 17:15:41 +0200257
inikepe02bf992016-06-02 12:00:32 +0200258 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
inikep554b3b92016-09-20 15:18:00 +0200259 LOG_WRAPPERC("- deflateSetDictionary level=%d\n", (int)zwc->compressionLevel);
inikep61abecc2016-09-21 19:30:29 +0200260 if (!zwc) return Z_STREAM_ERROR;
261 if (zwc->zbc == NULL) {
inikep67a1f4d2016-09-26 20:49:18 +0200262 zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
inikepa03b7a72016-09-26 22:11:55 +0200263 if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
inikep61abecc2016-09-21 19:30:29 +0200264 }
inikepa03b7a72016-09-26 22:11:55 +0200265 { int res = ZWRAP_initializeCStream(zwc, dictionary, dictLength, 0);
266 if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res); }
inikep6072eaa2016-09-27 15:24:44 +0200267 zwc->comprState = ZWRAP_useReset;
inikep3eabe9b2016-05-12 17:15:41 +0200268 }
Yann Collet4ded9e52016-08-30 10:04:33 -0700269
inikep3eabe9b2016-05-12 17:15:41 +0200270 return Z_OK;
271}
272
inikepc4ab5712016-09-19 14:54:13 +0200273
inikep3eabe9b2016-05-12 17:15:41 +0200274ZEXTERN int ZEXPORT z_deflate OF((z_streamp strm, int flush))
275{
276 ZWRAP_CCtx* zwc;
277
inikep252c20d2016-09-23 09:08:40 +0200278 if (!g_ZWRAP_useZSTDcompression) {
inikep554b3b92016-09-20 15:18:00 +0200279 int res;
280 LOG_WRAPPERC("- deflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d\n", (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);
281 res = deflate(strm, flush);
inikep3eabe9b2016-05-12 17:15:41 +0200282 return res;
283 }
284
inikepe02bf992016-06-02 12:00:32 +0200285 zwc = (ZWRAP_CCtx*) strm->state;
inikep2bb83e82016-09-23 18:59:53 +0200286 if (zwc == NULL) { LOG_WRAPPERC("zwc == NULL\n"); return Z_STREAM_ERROR; }
inikep3eabe9b2016-05-12 17:15:41 +0200287
inikep61abecc2016-09-21 19:30:29 +0200288 if (zwc->zbc == NULL) {
inikep67a1f4d2016-09-26 20:49:18 +0200289 int res;
290 zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
Yann Colletba75e9d2016-12-21 19:57:18 +0100291 if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
inikep67a1f4d2016-09-26 20:49:18 +0200292 res = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : 0);
inikepadc4c162016-09-21 19:39:25 +0200293 if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res);
inikep6072eaa2016-09-27 15:24:44 +0200294 if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
inikepcf3ec082016-09-23 10:30:26 +0200295 } else {
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100296 if (zwc->totalInBytes == 0) {
inikep6072eaa2016-09-27 15:24:44 +0200297 if (zwc->comprState == ZWRAP_useReset) {
inikep67a1f4d2016-09-26 20:49:18 +0200298 size_t const errorCode = ZSTD_resetCStream(zwc->zbc, (flush == Z_FINISH) ? strm->avail_in : zwc->pledgedSrcSize);
299 if (ZSTD_isError(errorCode)) { LOG_WRAPPERC("ERROR: ZSTD_resetCStream errorCode=%s\n", ZSTD_getErrorName(errorCode)); return ZWRAPC_finishWithError(zwc, strm, 0); }
300 } else {
301 int res = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : 0);
302 if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res);
inikep6072eaa2016-09-27 15:24:44 +0200303 if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
inikep67a1f4d2016-09-26 20:49:18 +0200304 }
inikepcf3ec082016-09-23 10:30:26 +0200305 }
inikep61abecc2016-09-21 19:30:29 +0200306 }
307
Przemyslaw Skibinskieee427e2016-12-13 19:14:04 +0100308 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 +0200309 if (strm->avail_in > 0) {
inikepb0773452016-09-16 14:06:10 +0200310 zwc->inBuffer.src = strm->next_in;
311 zwc->inBuffer.size = strm->avail_in;
312 zwc->inBuffer.pos = 0;
313 zwc->outBuffer.dst = strm->next_out;
314 zwc->outBuffer.size = strm->avail_out;
315 zwc->outBuffer.pos = 0;
316 { size_t const errorCode = ZSTD_compressStream(zwc->zbc, &zwc->outBuffer, &zwc->inBuffer);
inikep554b3b92016-09-20 15:18:00 +0200317 LOG_WRAPPERC("deflate ZSTD_compressStream srcSize=%d dstCapacity=%d\n", (int)zwc->inBuffer.size, (int)zwc->outBuffer.size);
inikepadc4c162016-09-21 19:39:25 +0200318 if (ZSTD_isError(errorCode)) return ZWRAPC_finishWithError(zwc, strm, 0);
inikepb0773452016-09-16 14:06:10 +0200319 }
320 strm->next_out += zwc->outBuffer.pos;
321 strm->total_out += zwc->outBuffer.pos;
322 strm->avail_out -= zwc->outBuffer.pos;
323 strm->total_in += zwc->inBuffer.pos;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100324 zwc->totalInBytes += zwc->inBuffer.pos;
inikepb0773452016-09-16 14:06:10 +0200325 strm->next_in += zwc->inBuffer.pos;
326 strm->avail_in -= zwc->inBuffer.pos;
inikep3eabe9b2016-05-12 17:15:41 +0200327 }
328
Yann Colletba75e9d2016-12-21 19:57:18 +0100329 if (flush == Z_FULL_FLUSH
Przemyslaw Skibinskiedd3e2a2016-11-28 12:46:16 +0100330#if ZLIB_VERNUM >= 0x1240
Yann Colletba75e9d2016-12-21 19:57:18 +0100331 || flush == Z_TREES
Przemyslaw Skibinskiedd3e2a2016-11-28 12:46:16 +0100332#endif
333 || flush == Z_BLOCK)
334 return ZWRAPC_finishWithErrorMsg(strm, "Z_FULL_FLUSH, Z_BLOCK and Z_TREES are not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200335
Dmitry Krot2ed3ba22016-08-13 22:02:45 +0300336 if (flush == Z_FINISH) {
inikep3eabe9b2016-05-12 17:15:41 +0200337 size_t bytesLeft;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100338 if (zwc->streamEnd) return Z_STREAM_END;
inikepb0773452016-09-16 14:06:10 +0200339 zwc->outBuffer.dst = strm->next_out;
340 zwc->outBuffer.size = strm->avail_out;
341 zwc->outBuffer.pos = 0;
inikepe46bad02016-09-19 13:24:07 +0200342 bytesLeft = ZSTD_endStream(zwc->zbc, &zwc->outBuffer);
inikep554b3b92016-09-20 15:18:00 +0200343 LOG_WRAPPERC("deflate ZSTD_endStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
inikepadc4c162016-09-21 19:39:25 +0200344 if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);
inikepb0773452016-09-16 14:06:10 +0200345 strm->next_out += zwc->outBuffer.pos;
346 strm->total_out += zwc->outBuffer.pos;
347 strm->avail_out -= zwc->outBuffer.pos;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100348 if (bytesLeft == 0) { zwc->streamEnd = 1; LOG_WRAPPERC("Z_STREAM_END2 strm->total_in=%d strm->avail_out=%d strm->total_out=%d\n", (int)strm->total_in, (int)strm->avail_out, (int)strm->total_out); return Z_STREAM_END; }
inikep3eabe9b2016-05-12 17:15:41 +0200349 }
inikepe46bad02016-09-19 13:24:07 +0200350 else
inikep61016872016-09-19 14:27:29 +0200351 if (flush == Z_SYNC_FLUSH || flush == Z_PARTIAL_FLUSH) {
Dmitry Krot2ed3ba22016-08-13 22:02:45 +0300352 size_t bytesLeft;
inikep8fc58482016-09-16 17:14:01 +0200353 zwc->outBuffer.dst = strm->next_out;
354 zwc->outBuffer.size = strm->avail_out;
355 zwc->outBuffer.pos = 0;
inikepb0773452016-09-16 14:06:10 +0200356 bytesLeft = ZSTD_flushStream(zwc->zbc, &zwc->outBuffer);
inikep554b3b92016-09-20 15:18:00 +0200357 LOG_WRAPPERC("deflate ZSTD_flushStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
inikepadc4c162016-09-21 19:39:25 +0200358 if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);
inikepb0773452016-09-16 14:06:10 +0200359 strm->next_out += zwc->outBuffer.pos;
360 strm->total_out += zwc->outBuffer.pos;
361 strm->avail_out -= zwc->outBuffer.pos;
Dmitry Krot2ed3ba22016-08-13 22:02:45 +0300362 }
Przemyslaw Skibinskieee427e2016-12-13 19:14:04 +0100363 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 +0200364 return Z_OK;
365}
366
367
Yann Collet4ded9e52016-08-30 10:04:33 -0700368ZEXTERN int ZEXPORT z_deflateEnd OF((z_streamp strm))
inikep3eabe9b2016-05-12 17:15:41 +0200369{
inikep252c20d2016-09-23 09:08:40 +0200370 if (!g_ZWRAP_useZSTDcompression) {
inikep554b3b92016-09-20 15:18:00 +0200371 LOG_WRAPPERC("- deflateEnd\n");
inikep3eabe9b2016-05-12 17:15:41 +0200372 return deflateEnd(strm);
373 }
inikep554b3b92016-09-20 15:18:00 +0200374 LOG_WRAPPERC("- deflateEnd total_in=%d total_out=%d\n", (int)(strm->total_in), (int)(strm->total_out));
inikep3fa1b742016-09-21 13:51:57 +0200375 { size_t errorCode;
376 ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
377 if (zwc == NULL) return Z_OK; /* structures are already freed */
inikepc4ab5712016-09-19 14:54:13 +0200378 strm->state = NULL;
inikep3fa1b742016-09-21 13:51:57 +0200379 errorCode = ZWRAP_freeCCtx(zwc);
inikep18f66452016-09-20 12:50:59 +0200380 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +0200381 }
382 return Z_OK;
383}
384
385
386ZEXTERN uLong ZEXPORT z_deflateBound OF((z_streamp strm,
387 uLong sourceLen))
388{
inikep252c20d2016-09-23 09:08:40 +0200389 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200390 return deflateBound(strm, sourceLen);
391
392 return ZSTD_compressBound(sourceLen);
393}
394
395
396ZEXTERN int ZEXPORT z_deflateParams OF((z_streamp strm,
397 int level,
398 int strategy))
399{
inikep252c20d2016-09-23 09:08:40 +0200400 if (!g_ZWRAP_useZSTDcompression) {
inikep554b3b92016-09-20 15:18:00 +0200401 LOG_WRAPPERC("- deflateParams level=%d strategy=%d\n", level, strategy);
inikep3eabe9b2016-05-12 17:15:41 +0200402 return deflateParams(strm, level, strategy);
403 }
404
405 return Z_OK;
406}
407
408
409
410
411
412/* *** Decompression *** */
inikep706876f2016-09-27 16:56:07 +0200413typedef enum { ZWRAP_ZLIB_STREAM, ZWRAP_ZSTD_STREAM, ZWRAP_UNKNOWN_STREAM } ZWRAP_stream_type;
inikep3eabe9b2016-05-12 17:15:41 +0200414
415typedef struct {
inikepb0773452016-09-16 14:06:10 +0200416 ZSTD_DStream* zbd;
inikep8fc58482016-09-16 17:14:01 +0200417 char headerBuf[16]; /* should be equal or bigger than ZSTD_frameHeaderSize_min */
inikep3eabe9b2016-05-12 17:15:41 +0200418 int errorCount;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100419 unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */
inikep706876f2016-09-27 16:56:07 +0200420 ZWRAP_state_t decompState;
inikep86fc8e02016-09-20 16:22:28 +0200421 ZSTD_inBuffer inBuffer;
422 ZSTD_outBuffer outBuffer;
Yann Collet4ded9e52016-08-30 10:04:33 -0700423
inikep3eabe9b2016-05-12 17:15:41 +0200424 /* zlib params */
425 int stream_size;
426 char *version;
427 int windowBits;
inikepf040be92016-06-03 16:31:57 +0200428 ZSTD_customMem customMem;
inikepff9114a2016-06-02 16:52:36 +0200429 z_stream allocFunc; /* copy of zalloc, zfree, opaque */
Przemyslaw Skibinski0694ae22016-11-04 16:05:28 +0100430} ZWRAP_DCtx;
inikep3eabe9b2016-05-12 17:15:41 +0200431
432
Yann Colletba75e9d2016-12-21 19:57:18 +0100433int ZWRAP_isUsingZSTDdecompression(z_streamp strm)
inikep706876f2016-09-27 16:56:07 +0200434{
435 if (strm == NULL) return 0;
436 return (strm->reserved == ZWRAP_ZSTD_STREAM);
437}
438
439
inikep86fc8e02016-09-20 16:22:28 +0200440void ZWRAP_initDCtx(ZWRAP_DCtx* zwd)
441{
inikep706876f2016-09-27 16:56:07 +0200442 zwd->errorCount = 0;
inikep86fc8e02016-09-20 16:22:28 +0200443 zwd->outBuffer.pos = 0;
444 zwd->outBuffer.size = 0;
445}
446
inikepcd2f6b62016-09-23 20:03:17 +0200447
inikepff9114a2016-06-02 16:52:36 +0200448ZWRAP_DCtx* ZWRAP_createDCtx(z_streamp strm)
inikep3eabe9b2016-05-12 17:15:41 +0200449{
inikepf040be92016-06-03 16:31:57 +0200450 ZWRAP_DCtx* zwd;
451
452 if (strm->zalloc && strm->zfree) {
453 zwd = (ZWRAP_DCtx*)strm->zalloc(strm->opaque, 1, sizeof(ZWRAP_DCtx));
454 if (zwd==NULL) return NULL;
455 memset(zwd, 0, sizeof(ZWRAP_DCtx));
456 memcpy(&zwd->allocFunc, strm, sizeof(z_stream));
457 { ZSTD_customMem ZWRAP_customMem = { ZWRAP_allocFunction, ZWRAP_freeFunction, &zwd->allocFunc };
458 memcpy(&zwd->customMem, &ZWRAP_customMem, sizeof(ZSTD_customMem));
459 }
460 } else {
Yann Colletae728a42017-05-30 17:11:39 -0700461 zwd = (ZWRAP_DCtx*)calloc(1, sizeof(*zwd));
inikepf040be92016-06-03 16:31:57 +0200462 if (zwd==NULL) return NULL;
inikepf040be92016-06-03 16:31:57 +0200463 }
464
Yann Colletba75e9d2016-12-21 19:57:18 +0100465 MEM_STATIC_ASSERT(sizeof(zwd->headerBuf) >= ZSTD_FRAMEHEADERSIZE_MIN); /* if compilation fails here, assertion is false */
inikep86fc8e02016-09-20 16:22:28 +0200466 ZWRAP_initDCtx(zwd);
inikep3eabe9b2016-05-12 17:15:41 +0200467 return zwd;
468}
469
470
471size_t ZWRAP_freeDCtx(ZWRAP_DCtx* zwd)
472{
473 if (zwd==NULL) return 0; /* support free on null */
inikepf7ab3ad2016-09-22 17:59:10 +0200474 if (zwd->zbd) ZSTD_freeDStream(zwd->zbd);
inikepf040be92016-06-03 16:31:57 +0200475 if (zwd->version) zwd->customMem.customFree(zwd->customMem.opaque, zwd->version);
476 zwd->customMem.customFree(zwd->customMem.opaque, zwd);
inikep3eabe9b2016-05-12 17:15:41 +0200477 return 0;
478}
479
480
inikepadc4c162016-09-21 19:39:25 +0200481int ZWRAPD_finishWithError(ZWRAP_DCtx* zwd, z_streamp strm, int error)
inikep0bb930b2016-09-19 14:31:16 +0200482{
inikepadc4c162016-09-21 19:39:25 +0200483 LOG_WRAPPERD("- ZWRAPD_finishWithError=%d\n", error);
inikep0bb930b2016-09-19 14:31:16 +0200484 if (zwd) ZWRAP_freeDCtx(zwd);
inikepc4ab5712016-09-19 14:54:13 +0200485 if (strm) strm->state = NULL;
inikep18f66452016-09-20 12:50:59 +0200486 return (error) ? error : Z_STREAM_ERROR;
inikep0bb930b2016-09-19 14:31:16 +0200487}
488
489
inikepadc4c162016-09-21 19:39:25 +0200490int ZWRAPD_finishWithErrorMsg(z_streamp strm, char* message)
inikep146ef582016-09-21 14:05:01 +0200491{
492 ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
493 strm->msg = message;
494 if (zwd == NULL) return Z_STREAM_ERROR;
Yann Colletba75e9d2016-12-21 19:57:18 +0100495
inikepadc4c162016-09-21 19:39:25 +0200496 return ZWRAPD_finishWithError(zwd, strm, 0);
inikep146ef582016-09-21 14:05:01 +0200497}
498
499
inikep3eabe9b2016-05-12 17:15:41 +0200500ZEXTERN int ZEXPORT z_inflateInit_ OF((z_streamp strm,
501 const char *version, int stream_size))
502{
inikepd7557172016-09-22 11:52:00 +0200503 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
inikep22e27302016-09-27 18:21:17 +0200504 strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */
inikepd7557172016-09-22 11:52:00 +0200505 return inflateInit(strm);
506 }
507
508 {
inikepff9114a2016-06-02 16:52:36 +0200509 ZWRAP_DCtx* zwd = ZWRAP_createDCtx(strm);
inikep554b3b92016-09-20 15:18:00 +0200510 LOG_WRAPPERD("- inflateInit\n");
inikepadc4c162016-09-21 19:39:25 +0200511 if (zwd == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
inikep3eabe9b2016-05-12 17:15:41 +0200512
inikepf040be92016-06-03 16:31:57 +0200513 zwd->version = zwd->customMem.customAlloc(zwd->customMem.opaque, strlen(version) + 1);
inikepadc4c162016-09-21 19:39:25 +0200514 if (zwd->version == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
inikepf040be92016-06-03 16:31:57 +0200515 strcpy(zwd->version, version);
inikep3eabe9b2016-05-12 17:15:41 +0200516
inikepf040be92016-06-03 16:31:57 +0200517 zwd->stream_size = stream_size;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100518 zwd->totalInBytes = 0;
inikepe02bf992016-06-02 12:00:32 +0200519 strm->state = (struct internal_state*) zwd; /* use state which in not used by user */
inikep3eabe9b2016-05-12 17:15:41 +0200520 strm->total_in = 0;
521 strm->total_out = 0;
inikep706876f2016-09-27 16:56:07 +0200522 strm->reserved = ZWRAP_UNKNOWN_STREAM; /* mark as unknown steam */
inikep68cd4762016-09-23 12:42:21 +0200523 strm->adler = 0;
inikepd7557172016-09-22 11:52:00 +0200524 }
inikep3eabe9b2016-05-12 17:15:41 +0200525
526 return Z_OK;
527}
528
529
530ZEXTERN int ZEXPORT z_inflateInit2_ OF((z_streamp strm, int windowBits,
531 const char *version, int stream_size))
532{
inikepd7557172016-09-22 11:52:00 +0200533 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
534 return inflateInit2_(strm, windowBits, version, stream_size);
535 }
Yann Colletba75e9d2016-12-21 19:57:18 +0100536
inikepd7557172016-09-22 11:52:00 +0200537 {
inikep3eabe9b2016-05-12 17:15:41 +0200538 int ret = z_inflateInit_ (strm, version, stream_size);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100539 LOG_WRAPPERD("- inflateInit2 windowBits=%d\n", windowBits);
inikep3eabe9b2016-05-12 17:15:41 +0200540 if (ret == Z_OK) {
541 ZWRAP_DCtx* zwd = (ZWRAP_DCtx*)strm->state;
inikep69413002016-09-20 16:40:50 +0200542 if (zwd == NULL) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +0200543 zwd->windowBits = windowBits;
544 }
545 return ret;
inikepd7557172016-09-22 11:52:00 +0200546 }
inikep3eabe9b2016-05-12 17:15:41 +0200547}
548
inikep20859af2016-09-27 17:27:43 +0200549int ZWRAP_inflateReset_keepDict(z_streamp strm)
inikep18f66452016-09-20 12:50:59 +0200550{
inikep20859af2016-09-27 17:27:43 +0200551 LOG_WRAPPERD("- ZWRAP_inflateReset_keepDict\n");
inikep856f91e2016-09-27 17:14:04 +0200552 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
553 return inflateReset(strm);
554
inikep554b3b92016-09-20 15:18:00 +0200555 { ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
556 if (zwd == NULL) return Z_STREAM_ERROR;
inikep86fc8e02016-09-20 16:22:28 +0200557 ZWRAP_initDCtx(zwd);
inikep706876f2016-09-27 16:56:07 +0200558 zwd->decompState = ZWRAP_useReset;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100559 zwd->totalInBytes = 0;
inikep554b3b92016-09-20 15:18:00 +0200560 }
Yann Colletba75e9d2016-12-21 19:57:18 +0100561
inikep18f66452016-09-20 12:50:59 +0200562 strm->total_in = 0;
563 strm->total_out = 0;
564 return Z_OK;
565}
566
567
inikep706876f2016-09-27 16:56:07 +0200568ZEXTERN int ZEXPORT z_inflateReset OF((z_streamp strm))
569{
570 LOG_WRAPPERD("- inflateReset\n");
571 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
572 return inflateReset(strm);
573
inikep20859af2016-09-27 17:27:43 +0200574 { int ret = ZWRAP_inflateReset_keepDict(strm);
inikep706876f2016-09-27 16:56:07 +0200575 if (ret != Z_OK) return ret; }
576
577 { ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
Yann Colletba75e9d2016-12-21 19:57:18 +0100578 if (zwd == NULL) return Z_STREAM_ERROR;
inikep706876f2016-09-27 16:56:07 +0200579 zwd->decompState = ZWRAP_useInit; }
580
581 return Z_OK;
582}
583
584
inikep69413002016-09-20 16:40:50 +0200585#if ZLIB_VERNUM >= 0x1240
586ZEXTERN int ZEXPORT z_inflateReset2 OF((z_streamp strm,
587 int windowBits))
588{
inikepd7557172016-09-22 11:52:00 +0200589 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep69413002016-09-20 16:40:50 +0200590 return inflateReset2(strm, windowBits);
591
592 { int ret = z_inflateReset (strm);
593 if (ret == Z_OK) {
594 ZWRAP_DCtx* zwd = (ZWRAP_DCtx*)strm->state;
595 if (zwd == NULL) return Z_STREAM_ERROR;
596 zwd->windowBits = windowBits;
597 }
598 return ret;
599 }
600}
601#endif
602
603
inikep3eabe9b2016-05-12 17:15:41 +0200604ZEXTERN int ZEXPORT z_inflateSetDictionary OF((z_streamp strm,
605 const Bytef *dictionary,
606 uInt dictLength))
607{
inikep554b3b92016-09-20 15:18:00 +0200608 LOG_WRAPPERD("- inflateSetDictionary\n");
inikepd7557172016-09-22 11:52:00 +0200609 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +0200610 return inflateSetDictionary(strm, dictionary, dictLength);
611
inikep1c9521f2016-06-13 12:00:46 +0200612 { size_t errorCode;
613 ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
inikepf7ab3ad2016-09-22 17:59:10 +0200614 if (zwd == NULL || zwd->zbd == NULL) return Z_STREAM_ERROR;
inikepb0773452016-09-16 14:06:10 +0200615 errorCode = ZSTD_initDStream_usingDict(zwd->zbd, dictionary, dictLength);
inikepadc4c162016-09-21 19:39:25 +0200616 if (ZSTD_isError(errorCode)) return ZWRAPD_finishWithError(zwd, strm, 0);
Yann Colletba75e9d2016-12-21 19:57:18 +0100617 zwd->decompState = ZWRAP_useReset;
Yann Collet4ded9e52016-08-30 10:04:33 -0700618
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100619 if (zwd->totalInBytes == ZSTD_HEADERSIZE) {
inikepb0773452016-09-16 14:06:10 +0200620 zwd->inBuffer.src = zwd->headerBuf;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100621 zwd->inBuffer.size = zwd->totalInBytes;
inikepb0773452016-09-16 14:06:10 +0200622 zwd->inBuffer.pos = 0;
623 zwd->outBuffer.dst = strm->next_out;
624 zwd->outBuffer.size = 0;
625 zwd->outBuffer.pos = 0;
626 errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
inikep554b3b92016-09-20 15:18:00 +0200627 LOG_WRAPPERD("inflateSetDictionary ZSTD_decompressStream errorCode=%d srcSize=%d dstCapacity=%d\n", (int)errorCode, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);
inikep8fc58482016-09-16 17:14:01 +0200628 if (zwd->inBuffer.pos < zwd->outBuffer.size || ZSTD_isError(errorCode)) {
inikep554b3b92016-09-20 15:18:00 +0200629 LOG_WRAPPERD("ERROR: ZSTD_decompressStream %s\n", ZSTD_getErrorName(errorCode));
inikepadc4c162016-09-21 19:39:25 +0200630 return ZWRAPD_finishWithError(zwd, strm, 0);
inikep3eabe9b2016-05-12 17:15:41 +0200631 }
632 }
633 }
634
635 return Z_OK;
636}
637
638
Yann Collet4ded9e52016-08-30 10:04:33 -0700639ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush))
inikep3eabe9b2016-05-12 17:15:41 +0200640{
inikep57b97082016-09-23 14:59:46 +0200641 ZWRAP_DCtx* zwd;
inikep554b3b92016-09-20 15:18:00 +0200642 int res;
inikepd7557172016-09-22 11:52:00 +0200643 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
inikep554b3b92016-09-20 15:18:00 +0200644 LOG_WRAPPERD("- inflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d\n", (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);
645 res = inflate(strm, flush);
inikep86fc8e02016-09-20 16:22:28 +0200646 LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n", (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, res);
inikep554b3b92016-09-20 15:18:00 +0200647 return res;
648 }
inikep3eabe9b2016-05-12 17:15:41 +0200649
inikepe82c8112016-09-23 16:20:13 +0200650 if (strm->avail_in <= 0) return Z_OK;
651
inikepcd2f6b62016-09-23 20:03:17 +0200652 { size_t errorCode, srcSize;
inikep57b97082016-09-23 14:59:46 +0200653 zwd = (ZWRAP_DCtx*) strm->state;
inikep554b3b92016-09-20 15:18:00 +0200654 LOG_WRAPPERD("- inflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d\n", (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);
inikep86fc8e02016-09-20 16:22:28 +0200655
inikep57b97082016-09-23 14:59:46 +0200656 if (zwd == NULL) return Z_STREAM_ERROR;
inikep706876f2016-09-27 16:56:07 +0200657 if (zwd->decompState == ZWRAP_streamEnd) return Z_STREAM_END;
inikep86fc8e02016-09-20 16:22:28 +0200658
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100659 if (zwd->totalInBytes < ZLIB_HEADERSIZE) {
660 if (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) {
inikep57b97082016-09-23 14:59:46 +0200661 if (MEM_readLE32(strm->next_in) != ZSTD_MAGICNUMBER) {
662 if (zwd->windowBits)
663 errorCode = inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size);
664 else
665 errorCode = inflateInit_(strm, zwd->version, zwd->stream_size);
inikep3eabe9b2016-05-12 17:15:41 +0200666
inikep706876f2016-09-27 16:56:07 +0200667 strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */
inikep57b97082016-09-23 14:59:46 +0200668 errorCode = ZWRAP_freeDCtx(zwd);
669 if (ZSTD_isError(errorCode)) goto error;
inikep3eabe9b2016-05-12 17:15:41 +0200670
inikep57b97082016-09-23 14:59:46 +0200671 if (flush == Z_INFLATE_SYNC) res = inflateSync(strm);
672 else res = inflate(strm, flush);
673 LOG_WRAPPERD("- inflate3 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n", (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, res);
674 return res;
675 }
676 } else {
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100677 srcSize = MIN(strm->avail_in, ZLIB_HEADERSIZE - zwd->totalInBytes);
678 memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);
inikep57b97082016-09-23 14:59:46 +0200679 strm->total_in += srcSize;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100680 zwd->totalInBytes += srcSize;
inikep57b97082016-09-23 14:59:46 +0200681 strm->next_in += srcSize;
682 strm->avail_in -= srcSize;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100683 if (zwd->totalInBytes < ZLIB_HEADERSIZE) return Z_OK;
inikep3eabe9b2016-05-12 17:15:41 +0200684
inikep57b97082016-09-23 14:59:46 +0200685 if (MEM_readLE32(zwd->headerBuf) != ZSTD_MAGICNUMBER) {
686 z_stream strm2;
687 strm2.next_in = strm->next_in;
688 strm2.avail_in = strm->avail_in;
689 strm2.next_out = strm->next_out;
690 strm2.avail_out = strm->avail_out;
Yann Collet4ded9e52016-08-30 10:04:33 -0700691
inikep57b97082016-09-23 14:59:46 +0200692 if (zwd->windowBits)
693 errorCode = inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size);
694 else
695 errorCode = inflateInit_(strm, zwd->version, zwd->stream_size);
696 LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", (int)errorCode);
697 if (errorCode != Z_OK) return ZWRAPD_finishWithError(zwd, strm, (int)errorCode);
inikepe02bf992016-06-02 12:00:32 +0200698
inikep57b97082016-09-23 14:59:46 +0200699 /* inflate header */
700 strm->next_in = (unsigned char*)zwd->headerBuf;
701 strm->avail_in = ZLIB_HEADERSIZE;
702 strm->avail_out = 0;
703 errorCode = inflate(strm, Z_NO_FLUSH);
704 LOG_WRAPPERD("ZLIB inflate errorCode=%d strm->avail_in=%d\n", (int)errorCode, (int)strm->avail_in);
705 if (errorCode != Z_OK) return ZWRAPD_finishWithError(zwd, strm, (int)errorCode);
706 if (strm->avail_in > 0) goto error;
inikepe02bf992016-06-02 12:00:32 +0200707
inikep57b97082016-09-23 14:59:46 +0200708 strm->next_in = strm2.next_in;
709 strm->avail_in = strm2.avail_in;
710 strm->next_out = strm2.next_out;
711 strm->avail_out = strm2.avail_out;
712
inikep706876f2016-09-27 16:56:07 +0200713 strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */
inikep57b97082016-09-23 14:59:46 +0200714 errorCode = ZWRAP_freeDCtx(zwd);
715 if (ZSTD_isError(errorCode)) goto error;
716
717 if (flush == Z_INFLATE_SYNC) res = inflateSync(strm);
718 else res = inflate(strm, flush);
719 LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n", (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, res);
720 return res;
721 }
inikep3eabe9b2016-05-12 17:15:41 +0200722 }
inikep8fc58482016-09-16 17:14:01 +0200723 }
724
inikep706876f2016-09-27 16:56:07 +0200725 strm->reserved = ZWRAP_ZSTD_STREAM; /* mark as zstd steam */
726
inikep57b97082016-09-23 14:59:46 +0200727 if (flush == Z_INFLATE_SYNC) { strm->msg = "inflateSync is not supported!"; goto error; }
728
inikepf7ab3ad2016-09-22 17:59:10 +0200729 if (!zwd->zbd) {
730 zwd->zbd = ZSTD_createDStream_advanced(zwd->customMem);
731 if (zwd->zbd == NULL) { LOG_WRAPPERD("ERROR: ZSTD_createDStream_advanced\n"); goto error; }
inikep22e27302016-09-27 18:21:17 +0200732 zwd->decompState = ZWRAP_useInit;
inikepf7ab3ad2016-09-22 17:59:10 +0200733 }
734
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100735 if (zwd->totalInBytes < ZSTD_HEADERSIZE)
inikep8fc58482016-09-16 17:14:01 +0200736 {
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100737 if (zwd->totalInBytes == 0 && strm->avail_in >= ZSTD_HEADERSIZE) {
inikep706876f2016-09-27 16:56:07 +0200738 if (zwd->decompState == ZWRAP_useInit) {
inikep57b97082016-09-23 14:59:46 +0200739 errorCode = ZSTD_initDStream(zwd->zbd);
740 if (ZSTD_isError(errorCode)) { LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n", ZSTD_getErrorName(errorCode)); goto error; }
inikep22e27302016-09-27 18:21:17 +0200741 } else {
742 errorCode = ZSTD_resetDStream(zwd->zbd);
743 if (ZSTD_isError(errorCode)) goto error;
inikep57b97082016-09-23 14:59:46 +0200744 }
745 } else {
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100746 srcSize = MIN(strm->avail_in, ZSTD_HEADERSIZE - zwd->totalInBytes);
747 memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);
inikep57b97082016-09-23 14:59:46 +0200748 strm->total_in += srcSize;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100749 zwd->totalInBytes += srcSize;
inikep57b97082016-09-23 14:59:46 +0200750 strm->next_in += srcSize;
751 strm->avail_in -= srcSize;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100752 if (zwd->totalInBytes < ZSTD_HEADERSIZE) return Z_OK;
inikep3eabe9b2016-05-12 17:15:41 +0200753
inikep22e27302016-09-27 18:21:17 +0200754 if (zwd->decompState == ZWRAP_useInit) {
755 errorCode = ZSTD_initDStream(zwd->zbd);
756 if (ZSTD_isError(errorCode)) { LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n", ZSTD_getErrorName(errorCode)); goto error; }
757 } else {
758 errorCode = ZSTD_resetDStream(zwd->zbd);
759 if (ZSTD_isError(errorCode)) goto error;
760 }
inikep3eabe9b2016-05-12 17:15:41 +0200761
inikep57b97082016-09-23 14:59:46 +0200762 zwd->inBuffer.src = zwd->headerBuf;
763 zwd->inBuffer.size = ZSTD_HEADERSIZE;
764 zwd->inBuffer.pos = 0;
765 zwd->outBuffer.dst = strm->next_out;
766 zwd->outBuffer.size = 0;
767 zwd->outBuffer.pos = 0;
768 errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
769 LOG_WRAPPERD("inflate ZSTD_decompressStream1 errorCode=%d srcSize=%d dstCapacity=%d\n", (int)errorCode, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);
770 if (ZSTD_isError(errorCode)) {
771 LOG_WRAPPERD("ERROR: ZSTD_decompressStream1 %s\n", ZSTD_getErrorName(errorCode));
772 goto error;
773 }
inikepe82c8112016-09-23 16:20:13 +0200774 if (zwd->inBuffer.pos != zwd->inBuffer.size) goto error; /* not consumed */
inikep3eabe9b2016-05-12 17:15:41 +0200775 }
inikep3eabe9b2016-05-12 17:15:41 +0200776 }
777
inikepb0773452016-09-16 14:06:10 +0200778 zwd->inBuffer.src = strm->next_in;
779 zwd->inBuffer.size = strm->avail_in;
780 zwd->inBuffer.pos = 0;
781 zwd->outBuffer.dst = strm->next_out;
782 zwd->outBuffer.size = strm->avail_out;
783 zwd->outBuffer.pos = 0;
784 errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
inikep554b3b92016-09-20 15:18:00 +0200785 LOG_WRAPPERD("inflate ZSTD_decompressStream2 errorCode=%d srcSize=%d dstCapacity=%d\n", (int)errorCode, (int)strm->avail_in, (int)strm->avail_out);
inikep3eabe9b2016-05-12 17:15:41 +0200786 if (ZSTD_isError(errorCode)) {
inikep3eabe9b2016-05-12 17:15:41 +0200787 zwd->errorCount++;
inikep86fc8e02016-09-20 16:22:28 +0200788 LOG_WRAPPERD("ERROR: ZSTD_decompressStream2 %s zwd->errorCount=%d\n", ZSTD_getErrorName(errorCode), zwd->errorCount);
inikep1c9521f2016-06-13 12:00:46 +0200789 if (zwd->errorCount<=1) return Z_NEED_DICT; else goto error;
inikep3eabe9b2016-05-12 17:15:41 +0200790 }
inikepf7ab3ad2016-09-22 17:59:10 +0200791 LOG_WRAPPERD("inflate inBuffer.pos=%d inBuffer.size=%d outBuffer.pos=%d outBuffer.size=%d o\n", (int)zwd->inBuffer.pos, (int)zwd->inBuffer.size, (int)zwd->outBuffer.pos, (int)zwd->outBuffer.size);
inikepb0773452016-09-16 14:06:10 +0200792 strm->next_out += zwd->outBuffer.pos;
793 strm->total_out += zwd->outBuffer.pos;
794 strm->avail_out -= zwd->outBuffer.pos;
inikepf7ab3ad2016-09-22 17:59:10 +0200795 strm->total_in += zwd->inBuffer.pos;
Przemyslaw Skibinski502966a2017-01-19 12:10:52 +0100796 zwd->totalInBytes += zwd->inBuffer.pos;
inikepf7ab3ad2016-09-22 17:59:10 +0200797 strm->next_in += zwd->inBuffer.pos;
798 strm->avail_in -= zwd->inBuffer.pos;
Yann Colletba75e9d2016-12-21 19:57:18 +0100799 if (errorCode == 0) {
800 LOG_WRAPPERD("inflate Z_STREAM_END1 avail_in=%d avail_out=%d total_in=%d total_out=%d\n", (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);
801 zwd->decompState = ZWRAP_streamEnd;
inikep86fc8e02016-09-20 16:22:28 +0200802 return Z_STREAM_END;
803 }
inikep3eabe9b2016-05-12 17:15:41 +0200804 }
inikep86fc8e02016-09-20 16:22:28 +0200805 LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n", (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, Z_OK);
inikep3eabe9b2016-05-12 17:15:41 +0200806 return Z_OK;
inikep57b97082016-09-23 14:59:46 +0200807
808error:
809 return ZWRAPD_finishWithError(zwd, strm, 0);
inikep3eabe9b2016-05-12 17:15:41 +0200810}
811
812
813ZEXTERN int ZEXPORT z_inflateEnd OF((z_streamp strm))
814{
inikepd7557172016-09-22 11:52:00 +0200815 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikepe02bf992016-06-02 12:00:32 +0200816 return inflateEnd(strm);
Yann Collet4ded9e52016-08-30 10:04:33 -0700817
inikep554b3b92016-09-20 15:18:00 +0200818 LOG_WRAPPERD("- inflateEnd total_in=%d total_out=%d\n", (int)(strm->total_in), (int)(strm->total_out));
inikep3fa1b742016-09-21 13:51:57 +0200819 { size_t errorCode;
820 ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
821 if (zwd == NULL) return Z_OK; /* structures are already freed */
inikep1c9521f2016-06-13 12:00:46 +0200822 strm->state = NULL;
inikep3fa1b742016-09-21 13:51:57 +0200823 errorCode = ZWRAP_freeDCtx(zwd);
824 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +0200825 }
inikep3fa1b742016-09-21 13:51:57 +0200826 return Z_OK;
inikep3eabe9b2016-05-12 17:15:41 +0200827}
828
829
830ZEXTERN int ZEXPORT z_inflateSync OF((z_streamp strm))
831{
inikepd7557172016-09-22 11:52:00 +0200832 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
inikep18f66452016-09-20 12:50:59 +0200833 return inflateSync(strm);
834 }
inikep61016872016-09-19 14:27:29 +0200835
inikep3eabe9b2016-05-12 17:15:41 +0200836 return z_inflate(strm, Z_INFLATE_SYNC);
837}
838
839
840
841
inikepcd2f6b62016-09-23 20:03:17 +0200842
inikep3eabe9b2016-05-12 17:15:41 +0200843/* Advanced compression functions */
844ZEXTERN int ZEXPORT z_deflateCopy OF((z_streamp dest,
845 z_streamp source))
846{
inikep252c20d2016-09-23 09:08:40 +0200847 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200848 return deflateCopy(dest, source);
inikepadc4c162016-09-21 19:39:25 +0200849 return ZWRAPC_finishWithErrorMsg(source, "deflateCopy is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200850}
851
852
inikep3eabe9b2016-05-12 17:15:41 +0200853ZEXTERN int ZEXPORT z_deflateTune OF((z_streamp strm,
854 int good_length,
855 int max_lazy,
856 int nice_length,
857 int max_chain))
858{
inikep252c20d2016-09-23 09:08:40 +0200859 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200860 return deflateTune(strm, good_length, max_lazy, nice_length, max_chain);
inikepadc4c162016-09-21 19:39:25 +0200861 return ZWRAPC_finishWithErrorMsg(strm, "deflateTune is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200862}
863
864
inikepbf25d7a2016-06-02 10:19:35 +0200865#if ZLIB_VERNUM >= 0x1260
inikep3eabe9b2016-05-12 17:15:41 +0200866ZEXTERN int ZEXPORT z_deflatePending OF((z_streamp strm,
867 unsigned *pending,
868 int *bits))
869{
inikep252c20d2016-09-23 09:08:40 +0200870 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200871 return deflatePending(strm, pending, bits);
inikepadc4c162016-09-21 19:39:25 +0200872 return ZWRAPC_finishWithErrorMsg(strm, "deflatePending is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200873}
inikepbf25d7a2016-06-02 10:19:35 +0200874#endif
inikep3eabe9b2016-05-12 17:15:41 +0200875
876
877ZEXTERN int ZEXPORT z_deflatePrime OF((z_streamp strm,
878 int bits,
879 int value))
880{
inikep252c20d2016-09-23 09:08:40 +0200881 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200882 return deflatePrime(strm, bits, value);
inikepadc4c162016-09-21 19:39:25 +0200883 return ZWRAPC_finishWithErrorMsg(strm, "deflatePrime is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200884}
885
886
887ZEXTERN int ZEXPORT z_deflateSetHeader OF((z_streamp strm,
888 gz_headerp head))
889{
inikep252c20d2016-09-23 09:08:40 +0200890 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200891 return deflateSetHeader(strm, head);
inikepadc4c162016-09-21 19:39:25 +0200892 return ZWRAPC_finishWithErrorMsg(strm, "deflateSetHeader is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200893}
894
895
896
897
inikep61016872016-09-19 14:27:29 +0200898/* Advanced decompression functions */
inikepbf25d7a2016-06-02 10:19:35 +0200899#if ZLIB_VERNUM >= 0x1280
inikep3eabe9b2016-05-12 17:15:41 +0200900ZEXTERN int ZEXPORT z_inflateGetDictionary OF((z_streamp strm,
901 Bytef *dictionary,
902 uInt *dictLength))
903{
inikepd7557172016-09-22 11:52:00 +0200904 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +0200905 return inflateGetDictionary(strm, dictionary, dictLength);
inikepadc4c162016-09-21 19:39:25 +0200906 return ZWRAPD_finishWithErrorMsg(strm, "inflateGetDictionary is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200907}
inikepbf25d7a2016-06-02 10:19:35 +0200908#endif
inikep3eabe9b2016-05-12 17:15:41 +0200909
910
911ZEXTERN int ZEXPORT z_inflateCopy OF((z_streamp dest,
912 z_streamp source))
913{
inikepd7557172016-09-22 11:52:00 +0200914 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !source->reserved)
inikep3eabe9b2016-05-12 17:15:41 +0200915 return inflateCopy(dest, source);
inikepadc4c162016-09-21 19:39:25 +0200916 return ZWRAPD_finishWithErrorMsg(source, "inflateCopy is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200917}
918
919
inikepbf25d7a2016-06-02 10:19:35 +0200920#if ZLIB_VERNUM >= 0x1240
inikepbf25d7a2016-06-02 10:19:35 +0200921ZEXTERN long ZEXPORT z_inflateMark OF((z_streamp strm))
922{
inikepd7557172016-09-22 11:52:00 +0200923 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikepbf25d7a2016-06-02 10:19:35 +0200924 return inflateMark(strm);
inikepadc4c162016-09-21 19:39:25 +0200925 return ZWRAPD_finishWithErrorMsg(strm, "inflateMark is not supported!");
inikepbf25d7a2016-06-02 10:19:35 +0200926}
927#endif
inikep3eabe9b2016-05-12 17:15:41 +0200928
929
930ZEXTERN int ZEXPORT z_inflatePrime OF((z_streamp strm,
931 int bits,
932 int value))
933{
inikepd7557172016-09-22 11:52:00 +0200934 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +0200935 return inflatePrime(strm, bits, value);
inikepadc4c162016-09-21 19:39:25 +0200936 return ZWRAPD_finishWithErrorMsg(strm, "inflatePrime is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200937}
938
939
inikep3eabe9b2016-05-12 17:15:41 +0200940ZEXTERN int ZEXPORT z_inflateGetHeader OF((z_streamp strm,
941 gz_headerp head))
942{
inikepd7557172016-09-22 11:52:00 +0200943 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +0200944 return inflateGetHeader(strm, head);
inikepadc4c162016-09-21 19:39:25 +0200945 return ZWRAPD_finishWithErrorMsg(strm, "inflateGetHeader is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200946}
947
948
949ZEXTERN int ZEXPORT z_inflateBackInit_ OF((z_streamp strm, int windowBits,
950 unsigned char FAR *window,
951 const char *version,
952 int stream_size))
953{
inikepd7557172016-09-22 11:52:00 +0200954 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +0200955 return inflateBackInit_(strm, windowBits, window, version, stream_size);
inikepadc4c162016-09-21 19:39:25 +0200956 return ZWRAPD_finishWithErrorMsg(strm, "inflateBackInit is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200957}
958
959
960ZEXTERN int ZEXPORT z_inflateBack OF((z_streamp strm,
961 in_func in, void FAR *in_desc,
962 out_func out, void FAR *out_desc))
963{
inikepd7557172016-09-22 11:52:00 +0200964 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +0200965 return inflateBack(strm, in, in_desc, out, out_desc);
inikepadc4c162016-09-21 19:39:25 +0200966 return ZWRAPD_finishWithErrorMsg(strm, "inflateBack is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200967}
968
969
970ZEXTERN int ZEXPORT z_inflateBackEnd OF((z_streamp strm))
971{
inikepd7557172016-09-22 11:52:00 +0200972 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +0200973 return inflateBackEnd(strm);
inikepadc4c162016-09-21 19:39:25 +0200974 return ZWRAPD_finishWithErrorMsg(strm, "inflateBackEnd is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200975}
976
977
978ZEXTERN uLong ZEXPORT z_zlibCompileFlags OF((void)) { return zlibCompileFlags(); };
979
980
981
982 /* utility functions */
983#ifndef Z_SOLO
984
985ZEXTERN int ZEXPORT z_compress OF((Bytef *dest, uLongf *destLen,
986 const Bytef *source, uLong sourceLen))
987{
inikep252c20d2016-09-23 09:08:40 +0200988 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200989 return compress(dest, destLen, source, sourceLen);
990
Yann Collet4ded9e52016-08-30 10:04:33 -0700991 { size_t dstCapacity = *destLen;
inikepde2c92f2016-06-03 19:44:03 +0200992 size_t const errorCode = ZSTD_compress(dest, dstCapacity, source, sourceLen, ZWRAP_DEFAULT_CLEVEL);
inikep554b3b92016-09-20 15:18:00 +0200993 LOG_WRAPPERD("z_compress sourceLen=%d dstCapacity=%d\n", (int)sourceLen, (int)dstCapacity);
inikep18f66452016-09-20 12:50:59 +0200994 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +0200995 *destLen = errorCode;
996 }
997 return Z_OK;
998}
999
1000
1001ZEXTERN int ZEXPORT z_compress2 OF((Bytef *dest, uLongf *destLen,
1002 const Bytef *source, uLong sourceLen,
1003 int level))
1004{
inikep252c20d2016-09-23 09:08:40 +02001005 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001006 return compress2(dest, destLen, source, sourceLen, level);
Yann Collet4ded9e52016-08-30 10:04:33 -07001007
1008 { size_t dstCapacity = *destLen;
inikepff2d1892016-06-02 22:15:09 +02001009 size_t const errorCode = ZSTD_compress(dest, dstCapacity, source, sourceLen, level);
inikep18f66452016-09-20 12:50:59 +02001010 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +02001011 *destLen = errorCode;
1012 }
1013 return Z_OK;
1014}
1015
1016
1017ZEXTERN uLong ZEXPORT z_compressBound OF((uLong sourceLen))
1018{
inikep252c20d2016-09-23 09:08:40 +02001019 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001020 return compressBound(sourceLen);
1021
1022 return ZSTD_compressBound(sourceLen);
1023}
1024
1025
1026ZEXTERN int ZEXPORT z_uncompress OF((Bytef *dest, uLongf *destLen,
1027 const Bytef *source, uLong sourceLen))
1028{
1029 if (sourceLen < 4 || MEM_readLE32(source) != ZSTD_MAGICNUMBER)
inikep3eabe9b2016-05-12 17:15:41 +02001030 return uncompress(dest, destLen, source, sourceLen);
1031
Yann Collet4ded9e52016-08-30 10:04:33 -07001032 { size_t dstCapacity = *destLen;
inikepff2d1892016-06-02 22:15:09 +02001033 size_t const errorCode = ZSTD_decompress(dest, dstCapacity, source, sourceLen);
inikep18f66452016-09-20 12:50:59 +02001034 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +02001035 *destLen = errorCode;
1036 }
1037 return Z_OK;
1038}
1039
inikep3eabe9b2016-05-12 17:15:41 +02001040#endif /* !Z_SOLO */
1041
1042
1043 /* checksum functions */
1044
1045ZEXTERN uLong ZEXPORT z_adler32 OF((uLong adler, const Bytef *buf, uInt len))
1046{
1047 return adler32(adler, buf, len);
1048}
1049
1050ZEXTERN uLong ZEXPORT z_crc32 OF((uLong crc, const Bytef *buf, uInt len))
1051{
1052 return crc32(crc, buf, len);
1053}
Przemyslaw Skibinski5b114d32017-01-17 13:02:06 +01001054
Przemyslaw Skibinskic9512db2017-01-18 12:51:44 +01001055
1056#if ZLIB_VERNUM >= 0x12B0
1057ZEXTERN uLong ZEXPORT z_adler32_z OF((uLong adler, const Bytef *buf, z_size_t len))
1058{
1059 return adler32_z(adler, buf, len);
1060}
1061
1062ZEXTERN uLong ZEXPORT z_crc32_z OF((uLong crc, const Bytef *buf, z_size_t len))
1063{
1064 return crc32_z(crc, buf, len);
1065}
1066#endif
1067
1068
Przemyslaw Skibinski5edab912017-01-18 10:39:39 +01001069#if ZLIB_VERNUM >= 0x1270
Przemyslaw Skibinski5b114d32017-01-17 13:02:06 +01001070ZEXTERN const z_crc_t FAR * ZEXPORT z_get_crc_table OF((void))
1071{
1072 return get_crc_table();
1073}
Przemyslaw Skibinski5edab912017-01-18 10:39:39 +01001074#endif