blob: 803d491110d2c1b803e2a1359a4ec97fd3841274 [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
inikep3d2c58c2016-08-10 14:28:47 +020011#include <stdio.h> /* vsprintf */
12#include <stdarg.h> /* va_list, for z_gzprintf */
inikep3eabe9b2016-05-12 17:15:41 +020013#include <zlib.h>
14#include "zstd_zlibwrapper.h"
Yann Collet16f72992016-06-04 19:52:06 +020015#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_MAGICNUMBER */
inikep3eabe9b2016-05-12 17:15:41 +020016#include "zstd.h"
inikep3d2c58c2016-08-10 14:28:47 +020017#include "zstd_internal.h" /* defaultCustomMem */
inikep3eabe9b2016-05-12 17:15:41 +020018
19
20#define Z_INFLATE_SYNC 8
inikep8fc58482016-09-16 17:14:01 +020021#define ZLIB_HEADERSIZE 4
22#define ZSTD_HEADERSIZE ZSTD_frameHeaderSize_min
inikep67a1f4d2016-09-26 20:49:18 +020023#define ZWRAP_DEFAULT_CLEVEL 3 /* Z_DEFAULT_COMPRESSION is translated to ZWRAP_DEFAULT_CLEVEL for zstd */
inikep3eabe9b2016-05-12 17:15:41 +020024
inikep68cd4762016-09-23 12:42:21 +020025#define LOG_WRAPPERC(...) /* printf(__VA_ARGS__) */
26#define LOG_WRAPPERD(...) /* printf(__VA_ARGS__) */
inikep3eabe9b2016-05-12 17:15:41 +020027
inikepcd2f6b62016-09-23 20:03:17 +020028#define FINISH_WITH_GZ_ERR(msg) { (void)msg; return Z_STREAM_ERROR; }
29#define FINISH_WITH_NULL_ERR(msg) { (void)msg; return NULL; }
inikepd7557172016-09-22 11:52:00 +020030
31
32
inikep3eabe9b2016-05-12 17:15:41 +020033#ifndef ZWRAP_USE_ZSTD
34 #define ZWRAP_USE_ZSTD 0
35#endif
36
inikep252c20d2016-09-23 09:08:40 +020037static int g_ZWRAP_useZSTDcompression = ZWRAP_USE_ZSTD; /* 0 = don't use ZSTD */
inikepd7557172016-09-22 11:52:00 +020038
inikep252c20d2016-09-23 09:08:40 +020039void ZWRAP_useZSTDcompression(int turn_on) { g_ZWRAP_useZSTDcompression = turn_on; }
inikepd7557172016-09-22 11:52:00 +020040
inikep252c20d2016-09-23 09:08:40 +020041int ZWRAP_isUsingZSTDcompression(void) { return g_ZWRAP_useZSTDcompression; }
inikep3eabe9b2016-05-12 17:15:41 +020042
43
44
inikepd7557172016-09-22 11:52:00 +020045static ZWRAP_decompress_type g_ZWRAPdecompressionType = ZWRAP_AUTO;
inikep3eabe9b2016-05-12 17:15:41 +020046
inikep252c20d2016-09-23 09:08:40 +020047void ZWRAP_setDecompressionType(ZWRAP_decompress_type type) { g_ZWRAPdecompressionType = type; };
inikep3eabe9b2016-05-12 17:15:41 +020048
inikep252c20d2016-09-23 09:08:40 +020049ZWRAP_decompress_type ZWRAP_getDecompressionType(void) { return g_ZWRAPdecompressionType; }
inikep3eabe9b2016-05-12 17:15:41 +020050
inikep3eabe9b2016-05-12 17:15:41 +020051
52
inikepcd2f6b62016-09-23 20:03:17 +020053const char * zstdVersion(void) { return ZSTD_VERSION_STRING; }
54
55ZEXTERN const char * ZEXPORT z_zlibVersion OF((void)) { return zlibVersion(); }
56
57
inikep230a61f2016-09-21 16:46:35 +020058
inikep7cab86f2016-06-02 18:24:07 +020059static void* ZWRAP_allocFunction(void* opaque, size_t size)
inikepff9114a2016-06-02 16:52:36 +020060{
61 z_streamp strm = (z_streamp) opaque;
62 void* address = strm->zalloc(strm->opaque, 1, size);
63 /* printf("ZWRAP alloc %p, %d \n", address, (int)size); */
64 return address;
65}
66
inikep7cab86f2016-06-02 18:24:07 +020067static void ZWRAP_freeFunction(void* opaque, void* address)
inikepff9114a2016-06-02 16:52:36 +020068{
69 z_streamp strm = (z_streamp) opaque;
70 strm->zfree(strm->opaque, address);
71 /* if (address) printf("ZWRAP free %p \n", address); */
72}
73
74
inikep3eabe9b2016-05-12 17:15:41 +020075
76/* *** Compression *** */
inikep706876f2016-09-27 16:56:07 +020077typedef enum { ZWRAP_useInit, ZWRAP_useReset, ZWRAP_streamEnd } ZWRAP_state_t;
inikep3eabe9b2016-05-12 17:15:41 +020078
79typedef struct {
inikepb0773452016-09-16 14:06:10 +020080 ZSTD_CStream* zbc;
inikep3eabe9b2016-05-12 17:15:41 +020081 int compressionLevel;
inikep2a746092016-06-03 14:53:51 +020082 ZSTD_customMem customMem;
inikepff9114a2016-06-02 16:52:36 +020083 z_stream allocFunc; /* copy of zalloc, zfree, opaque */
inikepb0773452016-09-16 14:06:10 +020084 ZSTD_inBuffer inBuffer;
85 ZSTD_outBuffer outBuffer;
inikep706876f2016-09-27 16:56:07 +020086 ZWRAP_state_t comprState;
inikep230a61f2016-09-21 16:46:35 +020087 unsigned long long pledgedSrcSize;
inikep3eabe9b2016-05-12 17:15:41 +020088} ZWRAP_CCtx;
89
90
inikepf040be92016-06-03 16:31:57 +020091size_t ZWRAP_freeCCtx(ZWRAP_CCtx* zwc)
92{
93 if (zwc==NULL) return 0; /* support free on NULL */
inikep61abecc2016-09-21 19:30:29 +020094 if (zwc->zbc) ZSTD_freeCStream(zwc->zbc);
inikepf040be92016-06-03 16:31:57 +020095 zwc->customMem.customFree(zwc->customMem.opaque, zwc);
96 return 0;
97}
98
99
inikepff9114a2016-06-02 16:52:36 +0200100ZWRAP_CCtx* ZWRAP_createCCtx(z_streamp strm)
inikep3eabe9b2016-05-12 17:15:41 +0200101{
inikep2a746092016-06-03 14:53:51 +0200102 ZWRAP_CCtx* zwc;
103
inikepff9114a2016-06-02 16:52:36 +0200104 if (strm->zalloc && strm->zfree) {
inikep2a746092016-06-03 14:53:51 +0200105 zwc = (ZWRAP_CCtx*)strm->zalloc(strm->opaque, 1, sizeof(ZWRAP_CCtx));
106 if (zwc==NULL) return NULL;
107 memset(zwc, 0, sizeof(ZWRAP_CCtx));
108 memcpy(&zwc->allocFunc, strm, sizeof(z_stream));
109 { ZSTD_customMem ZWRAP_customMem = { ZWRAP_allocFunction, ZWRAP_freeFunction, &zwc->allocFunc };
110 memcpy(&zwc->customMem, &ZWRAP_customMem, sizeof(ZSTD_customMem));
111 }
112 } else {
113 zwc = (ZWRAP_CCtx*)defaultCustomMem.customAlloc(defaultCustomMem.opaque, sizeof(ZWRAP_CCtx));
114 if (zwc==NULL) return NULL;
115 memset(zwc, 0, sizeof(ZWRAP_CCtx));
116 memcpy(&zwc->customMem, &defaultCustomMem, sizeof(ZSTD_customMem));
inikepff9114a2016-06-02 16:52:36 +0200117 }
inikep2a746092016-06-03 14:53:51 +0200118
inikep3eabe9b2016-05-12 17:15:41 +0200119 return zwc;
120}
121
122
inikep67a1f4d2016-09-26 20:49:18 +0200123int ZWRAP_initializeCStream(ZWRAP_CCtx* zwc, const void* dict, size_t dictSize, unsigned long long pledgedSrcSize)
inikep61abecc2016-09-21 19:30:29 +0200124{
inikep2bb83e82016-09-23 18:59:53 +0200125 LOG_WRAPPERC("- ZWRAP_initializeCStream=%p\n", zwc);
inikep67a1f4d2016-09-26 20:49:18 +0200126 if (zwc == NULL || zwc->zbc == NULL) return Z_STREAM_ERROR;
127
128 if (!pledgedSrcSize) pledgedSrcSize = zwc->pledgedSrcSize;
129 { ZSTD_parameters const params = ZSTD_getParams(zwc->compressionLevel, pledgedSrcSize, dictSize);
130 size_t errorCode;
131 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);
132 errorCode = ZSTD_initCStream_advanced(zwc->zbc, dict, dictSize, params, pledgedSrcSize);
133 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR; }
inikep8e8b0462016-09-22 14:42:32 +0200134
inikep61abecc2016-09-21 19:30:29 +0200135 return Z_OK;
136}
137
138
inikepadc4c162016-09-21 19:39:25 +0200139int ZWRAPC_finishWithError(ZWRAP_CCtx* zwc, z_streamp strm, int error)
inikepc4ab5712016-09-19 14:54:13 +0200140{
inikepadc4c162016-09-21 19:39:25 +0200141 LOG_WRAPPERC("- ZWRAPC_finishWithError=%d\n", error);
inikepc4ab5712016-09-19 14:54:13 +0200142 if (zwc) ZWRAP_freeCCtx(zwc);
143 if (strm) strm->state = NULL;
inikep18f66452016-09-20 12:50:59 +0200144 return (error) ? error : Z_STREAM_ERROR;
inikepc4ab5712016-09-19 14:54:13 +0200145}
146
147
inikepadc4c162016-09-21 19:39:25 +0200148int ZWRAPC_finishWithErrorMsg(z_streamp strm, char* message)
inikep146ef582016-09-21 14:05:01 +0200149{
150 ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
151 strm->msg = message;
152 if (zwc == NULL) return Z_STREAM_ERROR;
153
inikepadc4c162016-09-21 19:39:25 +0200154 return ZWRAPC_finishWithError(zwc, strm, 0);
inikep146ef582016-09-21 14:05:01 +0200155}
156
157
inikep252c20d2016-09-23 09:08:40 +0200158int ZWRAP_setPledgedSrcSize(z_streamp strm, unsigned long long pledgedSrcSize)
inikep230a61f2016-09-21 16:46:35 +0200159{
160 ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
161 if (zwc == NULL) return Z_STREAM_ERROR;
162
163 zwc->pledgedSrcSize = pledgedSrcSize;
inikep6072eaa2016-09-27 15:24:44 +0200164 zwc->comprState = ZWRAP_useInit;
inikep230a61f2016-09-21 16:46:35 +0200165 return Z_OK;
166}
167
168
inikep3eabe9b2016-05-12 17:15:41 +0200169ZEXTERN int ZEXPORT z_deflateInit_ OF((z_streamp strm, int level,
170 const char *version, int stream_size))
171{
172 ZWRAP_CCtx* zwc;
Yann Collet4ded9e52016-08-30 10:04:33 -0700173
inikep554b3b92016-09-20 15:18:00 +0200174 LOG_WRAPPERC("- deflateInit level=%d\n", level);
inikep252c20d2016-09-23 09:08:40 +0200175 if (!g_ZWRAP_useZSTDcompression) {
inikep3eabe9b2016-05-12 17:15:41 +0200176 return deflateInit_((strm), (level), version, stream_size);
177 }
178
inikepff9114a2016-06-02 16:52:36 +0200179 zwc = ZWRAP_createCCtx(strm);
inikep3eabe9b2016-05-12 17:15:41 +0200180 if (zwc == NULL) return Z_MEM_ERROR;
181
inikep8b452452016-06-01 10:50:17 +0200182 if (level == Z_DEFAULT_COMPRESSION)
183 level = ZWRAP_DEFAULT_CLEVEL;
184
inikep3eabe9b2016-05-12 17:15:41 +0200185 zwc->compressionLevel = level;
inikepe02bf992016-06-02 12:00:32 +0200186 strm->state = (struct internal_state*) zwc; /* use state which in not used by user */
inikep3eabe9b2016-05-12 17:15:41 +0200187 strm->total_in = 0;
188 strm->total_out = 0;
inikep68cd4762016-09-23 12:42:21 +0200189 strm->adler = 0;
inikep3eabe9b2016-05-12 17:15:41 +0200190 return Z_OK;
191}
192
193
inikep8b452452016-06-01 10:50:17 +0200194ZEXTERN int ZEXPORT z_deflateInit2_ OF((z_streamp strm, int level, int method,
inikep3eabe9b2016-05-12 17:15:41 +0200195 int windowBits, int memLevel,
196 int strategy, const char *version,
197 int stream_size))
198{
inikep252c20d2016-09-23 09:08:40 +0200199 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200200 return deflateInit2_(strm, level, method, windowBits, memLevel, strategy, version, stream_size);
201
202 return z_deflateInit_ (strm, level, version, stream_size);
203}
204
205
inikep706876f2016-09-27 16:56:07 +0200206int ZWRAP_deflateResetWithoutDict(z_streamp strm)
207{
inikep856f91e2016-09-27 17:14:04 +0200208 LOG_WRAPPERC("- ZWRAP_deflateResetWithoutDict\n");
209 if (!g_ZWRAP_useZSTDcompression)
210 return deflateReset(strm);
211
inikep706876f2016-09-27 16:56:07 +0200212 strm->total_in = 0;
213 strm->total_out = 0;
214 strm->adler = 0;
215 return Z_OK;
216}
217
218
inikep61016872016-09-19 14:27:29 +0200219ZEXTERN int ZEXPORT z_deflateReset OF((z_streamp strm))
220{
inikep554b3b92016-09-20 15:18:00 +0200221 LOG_WRAPPERC("- deflateReset\n");
inikep252c20d2016-09-23 09:08:40 +0200222 if (!g_ZWRAP_useZSTDcompression)
inikep61016872016-09-19 14:27:29 +0200223 return deflateReset(strm);
inikepc038c302016-09-20 12:54:26 +0200224
inikep706876f2016-09-27 16:56:07 +0200225 ZWRAP_deflateResetWithoutDict(strm);
226
227 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
228 if (zwc) zwc->comprState = 0;
229 }
inikepc038c302016-09-20 12:54:26 +0200230 return Z_OK;
inikep61016872016-09-19 14:27:29 +0200231}
232
233
inikep3eabe9b2016-05-12 17:15:41 +0200234ZEXTERN int ZEXPORT z_deflateSetDictionary OF((z_streamp strm,
235 const Bytef *dictionary,
236 uInt dictLength))
237{
inikep252c20d2016-09-23 09:08:40 +0200238 if (!g_ZWRAP_useZSTDcompression) {
inikep554b3b92016-09-20 15:18:00 +0200239 LOG_WRAPPERC("- deflateSetDictionary\n");
inikep3eabe9b2016-05-12 17:15:41 +0200240 return deflateSetDictionary(strm, dictionary, dictLength);
inikep554b3b92016-09-20 15:18:00 +0200241 }
inikep3eabe9b2016-05-12 17:15:41 +0200242
inikepe02bf992016-06-02 12:00:32 +0200243 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
inikep554b3b92016-09-20 15:18:00 +0200244 LOG_WRAPPERC("- deflateSetDictionary level=%d\n", (int)zwc->compressionLevel);
inikep61abecc2016-09-21 19:30:29 +0200245 if (!zwc) return Z_STREAM_ERROR;
246 if (zwc->zbc == NULL) {
inikep67a1f4d2016-09-26 20:49:18 +0200247 zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
inikepa03b7a72016-09-26 22:11:55 +0200248 if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
inikep61abecc2016-09-21 19:30:29 +0200249 }
inikepa03b7a72016-09-26 22:11:55 +0200250 { int res = ZWRAP_initializeCStream(zwc, dictionary, dictLength, 0);
251 if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res); }
inikep6072eaa2016-09-27 15:24:44 +0200252 zwc->comprState = ZWRAP_useReset;
inikep3eabe9b2016-05-12 17:15:41 +0200253 }
Yann Collet4ded9e52016-08-30 10:04:33 -0700254
inikep3eabe9b2016-05-12 17:15:41 +0200255 return Z_OK;
256}
257
inikepc4ab5712016-09-19 14:54:13 +0200258
inikep3eabe9b2016-05-12 17:15:41 +0200259ZEXTERN int ZEXPORT z_deflate OF((z_streamp strm, int flush))
260{
261 ZWRAP_CCtx* zwc;
262
inikep252c20d2016-09-23 09:08:40 +0200263 if (!g_ZWRAP_useZSTDcompression) {
inikep554b3b92016-09-20 15:18:00 +0200264 int res;
265 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);
266 res = deflate(strm, flush);
inikep3eabe9b2016-05-12 17:15:41 +0200267 return res;
268 }
269
inikepe02bf992016-06-02 12:00:32 +0200270 zwc = (ZWRAP_CCtx*) strm->state;
inikep2bb83e82016-09-23 18:59:53 +0200271 if (zwc == NULL) { LOG_WRAPPERC("zwc == NULL\n"); return Z_STREAM_ERROR; }
inikep3eabe9b2016-05-12 17:15:41 +0200272
inikep61abecc2016-09-21 19:30:29 +0200273 if (zwc->zbc == NULL) {
inikep67a1f4d2016-09-26 20:49:18 +0200274 int res;
275 zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
inikep60dddc22016-09-26 22:47:39 +0200276 if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
inikep67a1f4d2016-09-26 20:49:18 +0200277 res = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : 0);
inikepadc4c162016-09-21 19:39:25 +0200278 if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res);
inikep6072eaa2016-09-27 15:24:44 +0200279 if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
inikepcf3ec082016-09-23 10:30:26 +0200280 } else {
281 if (strm->total_in == 0) {
inikep6072eaa2016-09-27 15:24:44 +0200282 if (zwc->comprState == ZWRAP_useReset) {
inikep67a1f4d2016-09-26 20:49:18 +0200283 size_t const errorCode = ZSTD_resetCStream(zwc->zbc, (flush == Z_FINISH) ? strm->avail_in : zwc->pledgedSrcSize);
284 if (ZSTD_isError(errorCode)) { LOG_WRAPPERC("ERROR: ZSTD_resetCStream errorCode=%s\n", ZSTD_getErrorName(errorCode)); return ZWRAPC_finishWithError(zwc, strm, 0); }
285 } else {
286 int res = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : 0);
287 if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res);
inikep6072eaa2016-09-27 15:24:44 +0200288 if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
inikep67a1f4d2016-09-26 20:49:18 +0200289 }
inikepcf3ec082016-09-23 10:30:26 +0200290 }
inikep61abecc2016-09-21 19:30:29 +0200291 }
292
inikep554b3b92016-09-20 15:18:00 +0200293 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);
inikep3eabe9b2016-05-12 17:15:41 +0200294 if (strm->avail_in > 0) {
inikepb0773452016-09-16 14:06:10 +0200295 zwc->inBuffer.src = strm->next_in;
296 zwc->inBuffer.size = strm->avail_in;
297 zwc->inBuffer.pos = 0;
298 zwc->outBuffer.dst = strm->next_out;
299 zwc->outBuffer.size = strm->avail_out;
300 zwc->outBuffer.pos = 0;
301 { size_t const errorCode = ZSTD_compressStream(zwc->zbc, &zwc->outBuffer, &zwc->inBuffer);
inikep554b3b92016-09-20 15:18:00 +0200302 LOG_WRAPPERC("deflate ZSTD_compressStream srcSize=%d dstCapacity=%d\n", (int)zwc->inBuffer.size, (int)zwc->outBuffer.size);
inikepadc4c162016-09-21 19:39:25 +0200303 if (ZSTD_isError(errorCode)) return ZWRAPC_finishWithError(zwc, strm, 0);
inikepb0773452016-09-16 14:06:10 +0200304 }
305 strm->next_out += zwc->outBuffer.pos;
306 strm->total_out += zwc->outBuffer.pos;
307 strm->avail_out -= zwc->outBuffer.pos;
308 strm->total_in += zwc->inBuffer.pos;
309 strm->next_in += zwc->inBuffer.pos;
310 strm->avail_in -= zwc->inBuffer.pos;
inikep3eabe9b2016-05-12 17:15:41 +0200311 }
312
inikepadc4c162016-09-21 19:39:25 +0200313 if (flush == Z_FULL_FLUSH || flush == Z_BLOCK || flush == Z_TREES) return ZWRAPC_finishWithErrorMsg(strm, "Z_FULL_FLUSH, Z_BLOCK and Z_TREES are not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200314
Dmitry Krot2ed3ba22016-08-13 22:02:45 +0300315 if (flush == Z_FINISH) {
inikep3eabe9b2016-05-12 17:15:41 +0200316 size_t bytesLeft;
inikepb0773452016-09-16 14:06:10 +0200317 zwc->outBuffer.dst = strm->next_out;
318 zwc->outBuffer.size = strm->avail_out;
319 zwc->outBuffer.pos = 0;
inikepe46bad02016-09-19 13:24:07 +0200320 bytesLeft = ZSTD_endStream(zwc->zbc, &zwc->outBuffer);
inikep554b3b92016-09-20 15:18:00 +0200321 LOG_WRAPPERC("deflate ZSTD_endStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
inikepadc4c162016-09-21 19:39:25 +0200322 if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);
inikepb0773452016-09-16 14:06:10 +0200323 strm->next_out += zwc->outBuffer.pos;
324 strm->total_out += zwc->outBuffer.pos;
325 strm->avail_out -= zwc->outBuffer.pos;
inikep554b3b92016-09-20 15:18:00 +0200326 if (bytesLeft == 0) { 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 +0200327 }
inikepe46bad02016-09-19 13:24:07 +0200328 else
inikep61016872016-09-19 14:27:29 +0200329 if (flush == Z_SYNC_FLUSH || flush == Z_PARTIAL_FLUSH) {
Dmitry Krot2ed3ba22016-08-13 22:02:45 +0300330 size_t bytesLeft;
inikep8fc58482016-09-16 17:14:01 +0200331 zwc->outBuffer.dst = strm->next_out;
332 zwc->outBuffer.size = strm->avail_out;
333 zwc->outBuffer.pos = 0;
inikepb0773452016-09-16 14:06:10 +0200334 bytesLeft = ZSTD_flushStream(zwc->zbc, &zwc->outBuffer);
inikep554b3b92016-09-20 15:18:00 +0200335 LOG_WRAPPERC("deflate ZSTD_flushStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
inikepadc4c162016-09-21 19:39:25 +0200336 if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);
inikepb0773452016-09-16 14:06:10 +0200337 strm->next_out += zwc->outBuffer.pos;
338 strm->total_out += zwc->outBuffer.pos;
339 strm->avail_out -= zwc->outBuffer.pos;
Dmitry Krot2ed3ba22016-08-13 22:02:45 +0300340 }
inikep554b3b92016-09-20 15:18:00 +0200341 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 +0200342 return Z_OK;
343}
344
345
Yann Collet4ded9e52016-08-30 10:04:33 -0700346ZEXTERN int ZEXPORT z_deflateEnd OF((z_streamp strm))
inikep3eabe9b2016-05-12 17:15:41 +0200347{
inikep252c20d2016-09-23 09:08:40 +0200348 if (!g_ZWRAP_useZSTDcompression) {
inikep554b3b92016-09-20 15:18:00 +0200349 LOG_WRAPPERC("- deflateEnd\n");
inikep3eabe9b2016-05-12 17:15:41 +0200350 return deflateEnd(strm);
351 }
inikep554b3b92016-09-20 15:18:00 +0200352 LOG_WRAPPERC("- deflateEnd total_in=%d total_out=%d\n", (int)(strm->total_in), (int)(strm->total_out));
inikep3fa1b742016-09-21 13:51:57 +0200353 { size_t errorCode;
354 ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
355 if (zwc == NULL) return Z_OK; /* structures are already freed */
inikepc4ab5712016-09-19 14:54:13 +0200356 strm->state = NULL;
inikep3fa1b742016-09-21 13:51:57 +0200357 errorCode = ZWRAP_freeCCtx(zwc);
inikep18f66452016-09-20 12:50:59 +0200358 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +0200359 }
360 return Z_OK;
361}
362
363
364ZEXTERN uLong ZEXPORT z_deflateBound OF((z_streamp strm,
365 uLong sourceLen))
366{
inikep252c20d2016-09-23 09:08:40 +0200367 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200368 return deflateBound(strm, sourceLen);
369
370 return ZSTD_compressBound(sourceLen);
371}
372
373
374ZEXTERN int ZEXPORT z_deflateParams OF((z_streamp strm,
375 int level,
376 int strategy))
377{
inikep252c20d2016-09-23 09:08:40 +0200378 if (!g_ZWRAP_useZSTDcompression) {
inikep554b3b92016-09-20 15:18:00 +0200379 LOG_WRAPPERC("- deflateParams level=%d strategy=%d\n", level, strategy);
inikep3eabe9b2016-05-12 17:15:41 +0200380 return deflateParams(strm, level, strategy);
381 }
382
383 return Z_OK;
384}
385
386
387
388
389
390/* *** Decompression *** */
inikep706876f2016-09-27 16:56:07 +0200391typedef enum { ZWRAP_ZLIB_STREAM, ZWRAP_ZSTD_STREAM, ZWRAP_UNKNOWN_STREAM } ZWRAP_stream_type;
inikep3eabe9b2016-05-12 17:15:41 +0200392
393typedef struct {
inikepb0773452016-09-16 14:06:10 +0200394 ZSTD_DStream* zbd;
inikep8fc58482016-09-16 17:14:01 +0200395 char headerBuf[16]; /* should be equal or bigger than ZSTD_frameHeaderSize_min */
inikep3eabe9b2016-05-12 17:15:41 +0200396 int errorCount;
inikep706876f2016-09-27 16:56:07 +0200397 ZWRAP_state_t decompState;
inikep86fc8e02016-09-20 16:22:28 +0200398 ZSTD_inBuffer inBuffer;
399 ZSTD_outBuffer outBuffer;
Yann Collet4ded9e52016-08-30 10:04:33 -0700400
inikep3eabe9b2016-05-12 17:15:41 +0200401 /* zlib params */
402 int stream_size;
403 char *version;
404 int windowBits;
inikepf040be92016-06-03 16:31:57 +0200405 ZSTD_customMem customMem;
inikepff9114a2016-06-02 16:52:36 +0200406 z_stream allocFunc; /* copy of zalloc, zfree, opaque */
inikep3eabe9b2016-05-12 17:15:41 +0200407} ZWRAP_DCtx;
408
409
inikep706876f2016-09-27 16:56:07 +0200410int ZWRAP_isUsingZSTDdecompression(z_streamp strm)
411{
412 if (strm == NULL) return 0;
413 return (strm->reserved == ZWRAP_ZSTD_STREAM);
414}
415
416
inikep86fc8e02016-09-20 16:22:28 +0200417void ZWRAP_initDCtx(ZWRAP_DCtx* zwd)
418{
inikep706876f2016-09-27 16:56:07 +0200419 zwd->errorCount = 0;
inikep86fc8e02016-09-20 16:22:28 +0200420 zwd->outBuffer.pos = 0;
421 zwd->outBuffer.size = 0;
422}
423
inikepcd2f6b62016-09-23 20:03:17 +0200424
inikepff9114a2016-06-02 16:52:36 +0200425ZWRAP_DCtx* ZWRAP_createDCtx(z_streamp strm)
inikep3eabe9b2016-05-12 17:15:41 +0200426{
inikepf040be92016-06-03 16:31:57 +0200427 ZWRAP_DCtx* zwd;
428
429 if (strm->zalloc && strm->zfree) {
430 zwd = (ZWRAP_DCtx*)strm->zalloc(strm->opaque, 1, sizeof(ZWRAP_DCtx));
431 if (zwd==NULL) return NULL;
432 memset(zwd, 0, sizeof(ZWRAP_DCtx));
433 memcpy(&zwd->allocFunc, strm, sizeof(z_stream));
434 { ZSTD_customMem ZWRAP_customMem = { ZWRAP_allocFunction, ZWRAP_freeFunction, &zwd->allocFunc };
435 memcpy(&zwd->customMem, &ZWRAP_customMem, sizeof(ZSTD_customMem));
436 }
437 } else {
438 zwd = (ZWRAP_DCtx*)defaultCustomMem.customAlloc(defaultCustomMem.opaque, sizeof(ZWRAP_DCtx));
439 if (zwd==NULL) return NULL;
440 memset(zwd, 0, sizeof(ZWRAP_DCtx));
441 memcpy(&zwd->customMem, &defaultCustomMem, sizeof(ZSTD_customMem));
442 }
443
inikep67a1f4d2016-09-26 20:49:18 +0200444 MEM_STATIC_ASSERT(sizeof(zwd->headerBuf) >= ZSTD_frameHeaderSize_min); /* if compilation fails here, assertion is false */
inikep86fc8e02016-09-20 16:22:28 +0200445 ZWRAP_initDCtx(zwd);
inikep3eabe9b2016-05-12 17:15:41 +0200446 return zwd;
447}
448
449
450size_t ZWRAP_freeDCtx(ZWRAP_DCtx* zwd)
451{
452 if (zwd==NULL) return 0; /* support free on null */
inikepf7ab3ad2016-09-22 17:59:10 +0200453 if (zwd->zbd) ZSTD_freeDStream(zwd->zbd);
inikepf040be92016-06-03 16:31:57 +0200454 if (zwd->version) zwd->customMem.customFree(zwd->customMem.opaque, zwd->version);
455 zwd->customMem.customFree(zwd->customMem.opaque, zwd);
inikep3eabe9b2016-05-12 17:15:41 +0200456 return 0;
457}
458
459
inikepadc4c162016-09-21 19:39:25 +0200460int ZWRAPD_finishWithError(ZWRAP_DCtx* zwd, z_streamp strm, int error)
inikep0bb930b2016-09-19 14:31:16 +0200461{
inikepadc4c162016-09-21 19:39:25 +0200462 LOG_WRAPPERD("- ZWRAPD_finishWithError=%d\n", error);
inikep0bb930b2016-09-19 14:31:16 +0200463 if (zwd) ZWRAP_freeDCtx(zwd);
inikepc4ab5712016-09-19 14:54:13 +0200464 if (strm) strm->state = NULL;
inikep18f66452016-09-20 12:50:59 +0200465 return (error) ? error : Z_STREAM_ERROR;
inikep0bb930b2016-09-19 14:31:16 +0200466}
467
468
inikepadc4c162016-09-21 19:39:25 +0200469int ZWRAPD_finishWithErrorMsg(z_streamp strm, char* message)
inikep146ef582016-09-21 14:05:01 +0200470{
471 ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
472 strm->msg = message;
473 if (zwd == NULL) return Z_STREAM_ERROR;
474
inikepadc4c162016-09-21 19:39:25 +0200475 return ZWRAPD_finishWithError(zwd, strm, 0);
inikep146ef582016-09-21 14:05:01 +0200476}
477
478
inikep3eabe9b2016-05-12 17:15:41 +0200479ZEXTERN int ZEXPORT z_inflateInit_ OF((z_streamp strm,
480 const char *version, int stream_size))
481{
inikepd7557172016-09-22 11:52:00 +0200482 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
483 return inflateInit(strm);
484 }
485
486 {
inikepff9114a2016-06-02 16:52:36 +0200487 ZWRAP_DCtx* zwd = ZWRAP_createDCtx(strm);
inikep554b3b92016-09-20 15:18:00 +0200488 LOG_WRAPPERD("- inflateInit\n");
inikepadc4c162016-09-21 19:39:25 +0200489 if (zwd == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
inikep3eabe9b2016-05-12 17:15:41 +0200490
inikepf040be92016-06-03 16:31:57 +0200491 zwd->version = zwd->customMem.customAlloc(zwd->customMem.opaque, strlen(version) + 1);
inikepadc4c162016-09-21 19:39:25 +0200492 if (zwd->version == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
inikepf040be92016-06-03 16:31:57 +0200493 strcpy(zwd->version, version);
inikep3eabe9b2016-05-12 17:15:41 +0200494
inikepf040be92016-06-03 16:31:57 +0200495 zwd->stream_size = stream_size;
inikepe02bf992016-06-02 12:00:32 +0200496 strm->state = (struct internal_state*) zwd; /* use state which in not used by user */
inikep3eabe9b2016-05-12 17:15:41 +0200497 strm->total_in = 0;
498 strm->total_out = 0;
inikep706876f2016-09-27 16:56:07 +0200499 strm->reserved = ZWRAP_UNKNOWN_STREAM; /* mark as unknown steam */
inikep68cd4762016-09-23 12:42:21 +0200500 strm->adler = 0;
inikepd7557172016-09-22 11:52:00 +0200501 }
inikep3eabe9b2016-05-12 17:15:41 +0200502
503 return Z_OK;
504}
505
506
507ZEXTERN int ZEXPORT z_inflateInit2_ OF((z_streamp strm, int windowBits,
508 const char *version, int stream_size))
509{
inikepd7557172016-09-22 11:52:00 +0200510 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
511 return inflateInit2_(strm, windowBits, version, stream_size);
512 }
513
514 {
inikep3eabe9b2016-05-12 17:15:41 +0200515 int ret = z_inflateInit_ (strm, version, stream_size);
516 if (ret == Z_OK) {
517 ZWRAP_DCtx* zwd = (ZWRAP_DCtx*)strm->state;
inikep69413002016-09-20 16:40:50 +0200518 if (zwd == NULL) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +0200519 zwd->windowBits = windowBits;
520 }
521 return ret;
inikepd7557172016-09-22 11:52:00 +0200522 }
inikep3eabe9b2016-05-12 17:15:41 +0200523}
524
inikep706876f2016-09-27 16:56:07 +0200525int ZWRAP_inflateResetWithoutDict(z_streamp strm)
inikep18f66452016-09-20 12:50:59 +0200526{
inikep856f91e2016-09-27 17:14:04 +0200527 LOG_WRAPPERD("- ZWRAP_inflateResetWithoutDict\n");
528 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
529 return inflateReset(strm);
530
inikep554b3b92016-09-20 15:18:00 +0200531 { ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
532 if (zwd == NULL) return Z_STREAM_ERROR;
inikepf7ab3ad2016-09-22 17:59:10 +0200533 if (zwd->zbd) {
534 size_t const errorCode = ZSTD_resetDStream(zwd->zbd);
535 if (ZSTD_isError(errorCode)) return ZWRAPD_finishWithError(zwd, strm, 0);
536 }
inikep86fc8e02016-09-20 16:22:28 +0200537 ZWRAP_initDCtx(zwd);
inikep706876f2016-09-27 16:56:07 +0200538 zwd->decompState = ZWRAP_useReset;
inikep554b3b92016-09-20 15:18:00 +0200539 }
540
inikep18f66452016-09-20 12:50:59 +0200541 strm->total_in = 0;
542 strm->total_out = 0;
543 return Z_OK;
544}
545
546
inikep706876f2016-09-27 16:56:07 +0200547ZEXTERN int ZEXPORT z_inflateReset OF((z_streamp strm))
548{
549 LOG_WRAPPERD("- inflateReset\n");
550 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
551 return inflateReset(strm);
552
553 { int ret = ZWRAP_inflateResetWithoutDict(strm);
554 if (ret != Z_OK) return ret; }
555
556 { ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
557 if (zwd == NULL) return Z_STREAM_ERROR;
558 zwd->decompState = ZWRAP_useInit; }
559
560 return Z_OK;
561}
562
563
inikep69413002016-09-20 16:40:50 +0200564#if ZLIB_VERNUM >= 0x1240
565ZEXTERN int ZEXPORT z_inflateReset2 OF((z_streamp strm,
566 int windowBits))
567{
inikepd7557172016-09-22 11:52:00 +0200568 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep69413002016-09-20 16:40:50 +0200569 return inflateReset2(strm, windowBits);
570
571 { int ret = z_inflateReset (strm);
572 if (ret == Z_OK) {
573 ZWRAP_DCtx* zwd = (ZWRAP_DCtx*)strm->state;
574 if (zwd == NULL) return Z_STREAM_ERROR;
575 zwd->windowBits = windowBits;
576 }
577 return ret;
578 }
579}
580#endif
581
582
inikep3eabe9b2016-05-12 17:15:41 +0200583ZEXTERN int ZEXPORT z_inflateSetDictionary OF((z_streamp strm,
584 const Bytef *dictionary,
585 uInt dictLength))
586{
inikep554b3b92016-09-20 15:18:00 +0200587 LOG_WRAPPERD("- inflateSetDictionary\n");
inikepd7557172016-09-22 11:52:00 +0200588 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +0200589 return inflateSetDictionary(strm, dictionary, dictLength);
590
inikep1c9521f2016-06-13 12:00:46 +0200591 { size_t errorCode;
592 ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
inikepf7ab3ad2016-09-22 17:59:10 +0200593 if (zwd == NULL || zwd->zbd == NULL) return Z_STREAM_ERROR;
inikepb0773452016-09-16 14:06:10 +0200594 errorCode = ZSTD_initDStream_usingDict(zwd->zbd, dictionary, dictLength);
inikepadc4c162016-09-21 19:39:25 +0200595 if (ZSTD_isError(errorCode)) return ZWRAPD_finishWithError(zwd, strm, 0);
inikep706876f2016-09-27 16:56:07 +0200596 zwd->decompState = ZWRAP_useReset;
Yann Collet4ded9e52016-08-30 10:04:33 -0700597
inikep8fc58482016-09-16 17:14:01 +0200598 if (strm->total_in == ZSTD_HEADERSIZE) {
inikepb0773452016-09-16 14:06:10 +0200599 zwd->inBuffer.src = zwd->headerBuf;
600 zwd->inBuffer.size = strm->total_in;
601 zwd->inBuffer.pos = 0;
602 zwd->outBuffer.dst = strm->next_out;
603 zwd->outBuffer.size = 0;
604 zwd->outBuffer.pos = 0;
605 errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
inikep554b3b92016-09-20 15:18:00 +0200606 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 +0200607 if (zwd->inBuffer.pos < zwd->outBuffer.size || ZSTD_isError(errorCode)) {
inikep554b3b92016-09-20 15:18:00 +0200608 LOG_WRAPPERD("ERROR: ZSTD_decompressStream %s\n", ZSTD_getErrorName(errorCode));
inikepadc4c162016-09-21 19:39:25 +0200609 return ZWRAPD_finishWithError(zwd, strm, 0);
inikep3eabe9b2016-05-12 17:15:41 +0200610 }
611 }
612 }
613
614 return Z_OK;
615}
616
617
Yann Collet4ded9e52016-08-30 10:04:33 -0700618ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush))
inikep3eabe9b2016-05-12 17:15:41 +0200619{
inikep57b97082016-09-23 14:59:46 +0200620 ZWRAP_DCtx* zwd;
inikep554b3b92016-09-20 15:18:00 +0200621 int res;
inikepd7557172016-09-22 11:52:00 +0200622 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
inikep554b3b92016-09-20 15:18:00 +0200623 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);
624 res = inflate(strm, flush);
inikep86fc8e02016-09-20 16:22:28 +0200625 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 +0200626 return res;
627 }
inikep3eabe9b2016-05-12 17:15:41 +0200628
inikepe82c8112016-09-23 16:20:13 +0200629 if (strm->avail_in <= 0) return Z_OK;
630
inikepcd2f6b62016-09-23 20:03:17 +0200631 { size_t errorCode, srcSize;
inikep57b97082016-09-23 14:59:46 +0200632 zwd = (ZWRAP_DCtx*) strm->state;
inikep554b3b92016-09-20 15:18:00 +0200633 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 +0200634
inikep57b97082016-09-23 14:59:46 +0200635 if (zwd == NULL) return Z_STREAM_ERROR;
inikep706876f2016-09-27 16:56:07 +0200636 if (zwd->decompState == ZWRAP_streamEnd) return Z_STREAM_END;
inikep86fc8e02016-09-20 16:22:28 +0200637
inikep57b97082016-09-23 14:59:46 +0200638 if (strm->total_in < ZLIB_HEADERSIZE) {
639 if (strm->total_in == 0 && strm->avail_in >= ZLIB_HEADERSIZE) {
640 if (MEM_readLE32(strm->next_in) != ZSTD_MAGICNUMBER) {
641 if (zwd->windowBits)
642 errorCode = inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size);
643 else
644 errorCode = inflateInit_(strm, zwd->version, zwd->stream_size);
inikep3eabe9b2016-05-12 17:15:41 +0200645
inikep706876f2016-09-27 16:56:07 +0200646 strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */
inikep57b97082016-09-23 14:59:46 +0200647 errorCode = ZWRAP_freeDCtx(zwd);
648 if (ZSTD_isError(errorCode)) goto error;
inikep3eabe9b2016-05-12 17:15:41 +0200649
inikep57b97082016-09-23 14:59:46 +0200650 if (flush == Z_INFLATE_SYNC) res = inflateSync(strm);
651 else res = inflate(strm, flush);
652 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);
653 return res;
654 }
655 } else {
656 srcSize = MIN(strm->avail_in, ZLIB_HEADERSIZE - strm->total_in);
657 memcpy(zwd->headerBuf+strm->total_in, strm->next_in, srcSize);
658 strm->total_in += srcSize;
659 strm->next_in += srcSize;
660 strm->avail_in -= srcSize;
661 if (strm->total_in < ZLIB_HEADERSIZE) return Z_OK;
inikep3eabe9b2016-05-12 17:15:41 +0200662
inikep57b97082016-09-23 14:59:46 +0200663 if (MEM_readLE32(zwd->headerBuf) != ZSTD_MAGICNUMBER) {
664 z_stream strm2;
665 strm2.next_in = strm->next_in;
666 strm2.avail_in = strm->avail_in;
667 strm2.next_out = strm->next_out;
668 strm2.avail_out = strm->avail_out;
Yann Collet4ded9e52016-08-30 10:04:33 -0700669
inikep57b97082016-09-23 14:59:46 +0200670 if (zwd->windowBits)
671 errorCode = inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size);
672 else
673 errorCode = inflateInit_(strm, zwd->version, zwd->stream_size);
674 LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", (int)errorCode);
675 if (errorCode != Z_OK) return ZWRAPD_finishWithError(zwd, strm, (int)errorCode);
inikepe02bf992016-06-02 12:00:32 +0200676
inikep57b97082016-09-23 14:59:46 +0200677 /* inflate header */
678 strm->next_in = (unsigned char*)zwd->headerBuf;
679 strm->avail_in = ZLIB_HEADERSIZE;
680 strm->avail_out = 0;
681 errorCode = inflate(strm, Z_NO_FLUSH);
682 LOG_WRAPPERD("ZLIB inflate errorCode=%d strm->avail_in=%d\n", (int)errorCode, (int)strm->avail_in);
683 if (errorCode != Z_OK) return ZWRAPD_finishWithError(zwd, strm, (int)errorCode);
684 if (strm->avail_in > 0) goto error;
inikepe02bf992016-06-02 12:00:32 +0200685
inikep57b97082016-09-23 14:59:46 +0200686 strm->next_in = strm2.next_in;
687 strm->avail_in = strm2.avail_in;
688 strm->next_out = strm2.next_out;
689 strm->avail_out = strm2.avail_out;
690
inikep706876f2016-09-27 16:56:07 +0200691 strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */
inikep57b97082016-09-23 14:59:46 +0200692 errorCode = ZWRAP_freeDCtx(zwd);
693 if (ZSTD_isError(errorCode)) goto error;
694
695 if (flush == Z_INFLATE_SYNC) res = inflateSync(strm);
696 else res = inflate(strm, flush);
697 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);
698 return res;
699 }
inikep3eabe9b2016-05-12 17:15:41 +0200700 }
inikep8fc58482016-09-16 17:14:01 +0200701 }
702
inikep706876f2016-09-27 16:56:07 +0200703 strm->reserved = ZWRAP_ZSTD_STREAM; /* mark as zstd steam */
704
inikep57b97082016-09-23 14:59:46 +0200705 if (flush == Z_INFLATE_SYNC) { strm->msg = "inflateSync is not supported!"; goto error; }
706
inikepf7ab3ad2016-09-22 17:59:10 +0200707 if (!zwd->zbd) {
708 zwd->zbd = ZSTD_createDStream_advanced(zwd->customMem);
709 if (zwd->zbd == NULL) { LOG_WRAPPERD("ERROR: ZSTD_createDStream_advanced\n"); goto error; }
710 }
711
inikep8fc58482016-09-16 17:14:01 +0200712 if (strm->total_in < ZSTD_HEADERSIZE)
713 {
inikep57b97082016-09-23 14:59:46 +0200714 if (strm->total_in == 0 && strm->avail_in >= ZSTD_HEADERSIZE) {
inikep706876f2016-09-27 16:56:07 +0200715 if (zwd->decompState == ZWRAP_useInit) {
inikep57b97082016-09-23 14:59:46 +0200716 errorCode = ZSTD_initDStream(zwd->zbd);
717 if (ZSTD_isError(errorCode)) { LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n", ZSTD_getErrorName(errorCode)); goto error; }
718 }
719 } else {
720 srcSize = MIN(strm->avail_in, ZSTD_HEADERSIZE - strm->total_in);
721 memcpy(zwd->headerBuf+strm->total_in, strm->next_in, srcSize);
722 strm->total_in += srcSize;
723 strm->next_in += srcSize;
724 strm->avail_in -= srcSize;
725 if (strm->total_in < ZSTD_HEADERSIZE) return Z_OK;
inikep3eabe9b2016-05-12 17:15:41 +0200726
inikep57b97082016-09-23 14:59:46 +0200727 errorCode = ZSTD_initDStream(zwd->zbd);
728 if (ZSTD_isError(errorCode)) { LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n", ZSTD_getErrorName(errorCode)); goto error; }
inikep3eabe9b2016-05-12 17:15:41 +0200729
inikep57b97082016-09-23 14:59:46 +0200730 zwd->inBuffer.src = zwd->headerBuf;
731 zwd->inBuffer.size = ZSTD_HEADERSIZE;
732 zwd->inBuffer.pos = 0;
733 zwd->outBuffer.dst = strm->next_out;
734 zwd->outBuffer.size = 0;
735 zwd->outBuffer.pos = 0;
736 errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
737 LOG_WRAPPERD("inflate ZSTD_decompressStream1 errorCode=%d srcSize=%d dstCapacity=%d\n", (int)errorCode, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);
738 if (ZSTD_isError(errorCode)) {
739 LOG_WRAPPERD("ERROR: ZSTD_decompressStream1 %s\n", ZSTD_getErrorName(errorCode));
740 goto error;
741 }
inikepe82c8112016-09-23 16:20:13 +0200742 if (zwd->inBuffer.pos != zwd->inBuffer.size) goto error; /* not consumed */
inikep3eabe9b2016-05-12 17:15:41 +0200743 }
inikep3eabe9b2016-05-12 17:15:41 +0200744 }
745
inikepb0773452016-09-16 14:06:10 +0200746 zwd->inBuffer.src = strm->next_in;
747 zwd->inBuffer.size = strm->avail_in;
748 zwd->inBuffer.pos = 0;
749 zwd->outBuffer.dst = strm->next_out;
750 zwd->outBuffer.size = strm->avail_out;
751 zwd->outBuffer.pos = 0;
752 errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
inikep554b3b92016-09-20 15:18:00 +0200753 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 +0200754 if (ZSTD_isError(errorCode)) {
inikep3eabe9b2016-05-12 17:15:41 +0200755 zwd->errorCount++;
inikep86fc8e02016-09-20 16:22:28 +0200756 LOG_WRAPPERD("ERROR: ZSTD_decompressStream2 %s zwd->errorCount=%d\n", ZSTD_getErrorName(errorCode), zwd->errorCount);
inikep1c9521f2016-06-13 12:00:46 +0200757 if (zwd->errorCount<=1) return Z_NEED_DICT; else goto error;
inikep3eabe9b2016-05-12 17:15:41 +0200758 }
inikepf7ab3ad2016-09-22 17:59:10 +0200759 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 +0200760 strm->next_out += zwd->outBuffer.pos;
761 strm->total_out += zwd->outBuffer.pos;
762 strm->avail_out -= zwd->outBuffer.pos;
inikepf7ab3ad2016-09-22 17:59:10 +0200763 strm->total_in += zwd->inBuffer.pos;
764 strm->next_in += zwd->inBuffer.pos;
765 strm->avail_in -= zwd->inBuffer.pos;
inikep86fc8e02016-09-20 16:22:28 +0200766 if (errorCode == 0) {
767 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);
inikep706876f2016-09-27 16:56:07 +0200768 zwd->decompState = ZWRAP_streamEnd;
inikep86fc8e02016-09-20 16:22:28 +0200769 return Z_STREAM_END;
770 }
inikep3eabe9b2016-05-12 17:15:41 +0200771 }
inikep86fc8e02016-09-20 16:22:28 +0200772 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 +0200773 return Z_OK;
inikep57b97082016-09-23 14:59:46 +0200774
775error:
776 return ZWRAPD_finishWithError(zwd, strm, 0);
inikep3eabe9b2016-05-12 17:15:41 +0200777}
778
779
780ZEXTERN int ZEXPORT z_inflateEnd OF((z_streamp strm))
781{
inikepd7557172016-09-22 11:52:00 +0200782 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikepe02bf992016-06-02 12:00:32 +0200783 return inflateEnd(strm);
Yann Collet4ded9e52016-08-30 10:04:33 -0700784
inikep554b3b92016-09-20 15:18:00 +0200785 LOG_WRAPPERD("- inflateEnd total_in=%d total_out=%d\n", (int)(strm->total_in), (int)(strm->total_out));
inikep3fa1b742016-09-21 13:51:57 +0200786 { size_t errorCode;
787 ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
788 if (zwd == NULL) return Z_OK; /* structures are already freed */
inikep1c9521f2016-06-13 12:00:46 +0200789 strm->state = NULL;
inikep3fa1b742016-09-21 13:51:57 +0200790 errorCode = ZWRAP_freeDCtx(zwd);
791 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +0200792 }
inikep3fa1b742016-09-21 13:51:57 +0200793 return Z_OK;
inikep3eabe9b2016-05-12 17:15:41 +0200794}
795
796
797ZEXTERN int ZEXPORT z_inflateSync OF((z_streamp strm))
798{
inikepd7557172016-09-22 11:52:00 +0200799 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
inikep18f66452016-09-20 12:50:59 +0200800 return inflateSync(strm);
801 }
inikep61016872016-09-19 14:27:29 +0200802
inikep3eabe9b2016-05-12 17:15:41 +0200803 return z_inflate(strm, Z_INFLATE_SYNC);
804}
805
806
807
808
inikepcd2f6b62016-09-23 20:03:17 +0200809
inikep3eabe9b2016-05-12 17:15:41 +0200810/* Advanced compression functions */
811ZEXTERN int ZEXPORT z_deflateCopy OF((z_streamp dest,
812 z_streamp source))
813{
inikep252c20d2016-09-23 09:08:40 +0200814 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200815 return deflateCopy(dest, source);
inikepadc4c162016-09-21 19:39:25 +0200816 return ZWRAPC_finishWithErrorMsg(source, "deflateCopy is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200817}
818
819
inikep3eabe9b2016-05-12 17:15:41 +0200820ZEXTERN int ZEXPORT z_deflateTune OF((z_streamp strm,
821 int good_length,
822 int max_lazy,
823 int nice_length,
824 int max_chain))
825{
inikep252c20d2016-09-23 09:08:40 +0200826 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200827 return deflateTune(strm, good_length, max_lazy, nice_length, max_chain);
inikepadc4c162016-09-21 19:39:25 +0200828 return ZWRAPC_finishWithErrorMsg(strm, "deflateTune is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200829}
830
831
inikepbf25d7a2016-06-02 10:19:35 +0200832#if ZLIB_VERNUM >= 0x1260
inikep3eabe9b2016-05-12 17:15:41 +0200833ZEXTERN int ZEXPORT z_deflatePending OF((z_streamp strm,
834 unsigned *pending,
835 int *bits))
836{
inikep252c20d2016-09-23 09:08:40 +0200837 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200838 return deflatePending(strm, pending, bits);
inikepadc4c162016-09-21 19:39:25 +0200839 return ZWRAPC_finishWithErrorMsg(strm, "deflatePending is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200840}
inikepbf25d7a2016-06-02 10:19:35 +0200841#endif
inikep3eabe9b2016-05-12 17:15:41 +0200842
843
844ZEXTERN int ZEXPORT z_deflatePrime OF((z_streamp strm,
845 int bits,
846 int value))
847{
inikep252c20d2016-09-23 09:08:40 +0200848 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200849 return deflatePrime(strm, bits, value);
inikepadc4c162016-09-21 19:39:25 +0200850 return ZWRAPC_finishWithErrorMsg(strm, "deflatePrime is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200851}
852
853
854ZEXTERN int ZEXPORT z_deflateSetHeader OF((z_streamp strm,
855 gz_headerp head))
856{
inikep252c20d2016-09-23 09:08:40 +0200857 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200858 return deflateSetHeader(strm, head);
inikepadc4c162016-09-21 19:39:25 +0200859 return ZWRAPC_finishWithErrorMsg(strm, "deflateSetHeader is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200860}
861
862
863
864
inikep61016872016-09-19 14:27:29 +0200865/* Advanced decompression functions */
inikepbf25d7a2016-06-02 10:19:35 +0200866#if ZLIB_VERNUM >= 0x1280
inikep3eabe9b2016-05-12 17:15:41 +0200867ZEXTERN int ZEXPORT z_inflateGetDictionary OF((z_streamp strm,
868 Bytef *dictionary,
869 uInt *dictLength))
870{
inikepd7557172016-09-22 11:52:00 +0200871 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +0200872 return inflateGetDictionary(strm, dictionary, dictLength);
inikepadc4c162016-09-21 19:39:25 +0200873 return ZWRAPD_finishWithErrorMsg(strm, "inflateGetDictionary is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200874}
inikepbf25d7a2016-06-02 10:19:35 +0200875#endif
inikep3eabe9b2016-05-12 17:15:41 +0200876
877
878ZEXTERN int ZEXPORT z_inflateCopy OF((z_streamp dest,
879 z_streamp source))
880{
inikepd7557172016-09-22 11:52:00 +0200881 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !source->reserved)
inikep3eabe9b2016-05-12 17:15:41 +0200882 return inflateCopy(dest, source);
inikepadc4c162016-09-21 19:39:25 +0200883 return ZWRAPD_finishWithErrorMsg(source, "inflateCopy is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200884}
885
886
inikepbf25d7a2016-06-02 10:19:35 +0200887#if ZLIB_VERNUM >= 0x1240
inikepbf25d7a2016-06-02 10:19:35 +0200888ZEXTERN long ZEXPORT z_inflateMark OF((z_streamp strm))
889{
inikepd7557172016-09-22 11:52:00 +0200890 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikepbf25d7a2016-06-02 10:19:35 +0200891 return inflateMark(strm);
inikepadc4c162016-09-21 19:39:25 +0200892 return ZWRAPD_finishWithErrorMsg(strm, "inflateMark is not supported!");
inikepbf25d7a2016-06-02 10:19:35 +0200893}
894#endif
inikep3eabe9b2016-05-12 17:15:41 +0200895
896
897ZEXTERN int ZEXPORT z_inflatePrime OF((z_streamp strm,
898 int bits,
899 int value))
900{
inikepd7557172016-09-22 11:52:00 +0200901 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +0200902 return inflatePrime(strm, bits, value);
inikepadc4c162016-09-21 19:39:25 +0200903 return ZWRAPD_finishWithErrorMsg(strm, "inflatePrime is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200904}
905
906
inikep3eabe9b2016-05-12 17:15:41 +0200907ZEXTERN int ZEXPORT z_inflateGetHeader OF((z_streamp strm,
908 gz_headerp head))
909{
inikepd7557172016-09-22 11:52:00 +0200910 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +0200911 return inflateGetHeader(strm, head);
inikepadc4c162016-09-21 19:39:25 +0200912 return ZWRAPD_finishWithErrorMsg(strm, "inflateGetHeader is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200913}
914
915
916ZEXTERN int ZEXPORT z_inflateBackInit_ OF((z_streamp strm, int windowBits,
917 unsigned char FAR *window,
918 const char *version,
919 int stream_size))
920{
inikepd7557172016-09-22 11:52:00 +0200921 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +0200922 return inflateBackInit_(strm, windowBits, window, version, stream_size);
inikepadc4c162016-09-21 19:39:25 +0200923 return ZWRAPD_finishWithErrorMsg(strm, "inflateBackInit is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200924}
925
926
927ZEXTERN int ZEXPORT z_inflateBack OF((z_streamp strm,
928 in_func in, void FAR *in_desc,
929 out_func out, void FAR *out_desc))
930{
inikepd7557172016-09-22 11:52:00 +0200931 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +0200932 return inflateBack(strm, in, in_desc, out, out_desc);
inikepadc4c162016-09-21 19:39:25 +0200933 return ZWRAPD_finishWithErrorMsg(strm, "inflateBack is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200934}
935
936
937ZEXTERN int ZEXPORT z_inflateBackEnd OF((z_streamp strm))
938{
inikepd7557172016-09-22 11:52:00 +0200939 if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
inikep3eabe9b2016-05-12 17:15:41 +0200940 return inflateBackEnd(strm);
inikepadc4c162016-09-21 19:39:25 +0200941 return ZWRAPD_finishWithErrorMsg(strm, "inflateBackEnd is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200942}
943
944
945ZEXTERN uLong ZEXPORT z_zlibCompileFlags OF((void)) { return zlibCompileFlags(); };
946
947
948
949 /* utility functions */
950#ifndef Z_SOLO
951
952ZEXTERN int ZEXPORT z_compress OF((Bytef *dest, uLongf *destLen,
953 const Bytef *source, uLong sourceLen))
954{
inikep252c20d2016-09-23 09:08:40 +0200955 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200956 return compress(dest, destLen, source, sourceLen);
957
Yann Collet4ded9e52016-08-30 10:04:33 -0700958 { size_t dstCapacity = *destLen;
inikepde2c92f2016-06-03 19:44:03 +0200959 size_t const errorCode = ZSTD_compress(dest, dstCapacity, source, sourceLen, ZWRAP_DEFAULT_CLEVEL);
inikep554b3b92016-09-20 15:18:00 +0200960 LOG_WRAPPERD("z_compress sourceLen=%d dstCapacity=%d\n", (int)sourceLen, (int)dstCapacity);
inikep18f66452016-09-20 12:50:59 +0200961 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +0200962 *destLen = errorCode;
963 }
964 return Z_OK;
965}
966
967
968ZEXTERN int ZEXPORT z_compress2 OF((Bytef *dest, uLongf *destLen,
969 const Bytef *source, uLong sourceLen,
970 int level))
971{
inikep252c20d2016-09-23 09:08:40 +0200972 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200973 return compress2(dest, destLen, source, sourceLen, level);
Yann Collet4ded9e52016-08-30 10:04:33 -0700974
975 { size_t dstCapacity = *destLen;
inikepff2d1892016-06-02 22:15:09 +0200976 size_t const errorCode = ZSTD_compress(dest, dstCapacity, source, sourceLen, level);
inikep18f66452016-09-20 12:50:59 +0200977 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +0200978 *destLen = errorCode;
979 }
980 return Z_OK;
981}
982
983
984ZEXTERN uLong ZEXPORT z_compressBound OF((uLong sourceLen))
985{
inikep252c20d2016-09-23 09:08:40 +0200986 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +0200987 return compressBound(sourceLen);
988
989 return ZSTD_compressBound(sourceLen);
990}
991
992
993ZEXTERN int ZEXPORT z_uncompress OF((Bytef *dest, uLongf *destLen,
994 const Bytef *source, uLong sourceLen))
995{
996 if (sourceLen < 4 || MEM_readLE32(source) != ZSTD_MAGICNUMBER)
inikep3eabe9b2016-05-12 17:15:41 +0200997 return uncompress(dest, destLen, source, sourceLen);
998
Yann Collet4ded9e52016-08-30 10:04:33 -0700999 { size_t dstCapacity = *destLen;
inikepff2d1892016-06-02 22:15:09 +02001000 size_t const errorCode = ZSTD_decompress(dest, dstCapacity, source, sourceLen);
inikep18f66452016-09-20 12:50:59 +02001001 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +02001002 *destLen = errorCode;
1003 }
1004 return Z_OK;
1005}
1006
1007
1008
1009 /* gzip file access functions */
1010ZEXTERN gzFile ZEXPORT z_gzopen OF((const char *path, const char *mode))
1011{
inikep252c20d2016-09-23 09:08:40 +02001012 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001013 return gzopen(path, mode);
1014 FINISH_WITH_NULL_ERR("gzopen is not supported!");
1015}
1016
1017
1018ZEXTERN gzFile ZEXPORT z_gzdopen OF((int fd, const char *mode))
1019{
inikep252c20d2016-09-23 09:08:40 +02001020 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001021 return gzdopen(fd, mode);
1022 FINISH_WITH_NULL_ERR("gzdopen is not supported!");
1023}
1024
1025
inikepbf25d7a2016-06-02 10:19:35 +02001026#if ZLIB_VERNUM >= 0x1240
inikep3eabe9b2016-05-12 17:15:41 +02001027ZEXTERN int ZEXPORT z_gzbuffer OF((gzFile file, unsigned size))
1028{
inikep252c20d2016-09-23 09:08:40 +02001029 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001030 return gzbuffer(file, size);
inikep4af2c9d2016-06-03 17:39:31 +02001031 FINISH_WITH_GZ_ERR("gzbuffer is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001032}
1033
1034
inikepbf25d7a2016-06-02 10:19:35 +02001035ZEXTERN z_off_t ZEXPORT z_gzoffset OF((gzFile file))
1036{
inikep252c20d2016-09-23 09:08:40 +02001037 if (!g_ZWRAP_useZSTDcompression)
inikepbf25d7a2016-06-02 10:19:35 +02001038 return gzoffset(file);
inikep4af2c9d2016-06-03 17:39:31 +02001039 FINISH_WITH_GZ_ERR("gzoffset is not supported!");
inikepbf25d7a2016-06-02 10:19:35 +02001040}
1041
1042
1043ZEXTERN int ZEXPORT z_gzclose_r OF((gzFile file))
1044{
inikep252c20d2016-09-23 09:08:40 +02001045 if (!g_ZWRAP_useZSTDcompression)
inikepbf25d7a2016-06-02 10:19:35 +02001046 return gzclose_r(file);
inikep4af2c9d2016-06-03 17:39:31 +02001047 FINISH_WITH_GZ_ERR("gzclose_r is not supported!");
inikepbf25d7a2016-06-02 10:19:35 +02001048}
1049
1050
1051ZEXTERN int ZEXPORT z_gzclose_w OF((gzFile file))
1052{
inikep252c20d2016-09-23 09:08:40 +02001053 if (!g_ZWRAP_useZSTDcompression)
inikepbf25d7a2016-06-02 10:19:35 +02001054 return gzclose_w(file);
inikep4af2c9d2016-06-03 17:39:31 +02001055 FINISH_WITH_GZ_ERR("gzclose_w is not supported!");
inikepbf25d7a2016-06-02 10:19:35 +02001056}
1057#endif
1058
1059
inikep3eabe9b2016-05-12 17:15:41 +02001060ZEXTERN int ZEXPORT z_gzsetparams OF((gzFile file, int level, int strategy))
1061{
inikep252c20d2016-09-23 09:08:40 +02001062 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001063 return gzsetparams(file, level, strategy);
inikep4af2c9d2016-06-03 17:39:31 +02001064 FINISH_WITH_GZ_ERR("gzsetparams is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001065}
1066
1067
1068ZEXTERN int ZEXPORT z_gzread OF((gzFile file, voidp buf, unsigned len))
1069{
inikep252c20d2016-09-23 09:08:40 +02001070 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001071 return gzread(file, buf, len);
inikep4af2c9d2016-06-03 17:39:31 +02001072 FINISH_WITH_GZ_ERR("gzread is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001073}
1074
1075
1076ZEXTERN int ZEXPORT z_gzwrite OF((gzFile file,
1077 voidpc buf, unsigned len))
1078{
inikep252c20d2016-09-23 09:08:40 +02001079 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001080 return gzwrite(file, buf, len);
inikep4af2c9d2016-06-03 17:39:31 +02001081 FINISH_WITH_GZ_ERR("gzwrite is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001082}
1083
1084
inikepbf25d7a2016-06-02 10:19:35 +02001085#if ZLIB_VERNUM >= 0x1260
inikep3eabe9b2016-05-12 17:15:41 +02001086ZEXTERN int ZEXPORTVA z_gzprintf Z_ARG((gzFile file, const char *format, ...))
inikepbf25d7a2016-06-02 10:19:35 +02001087#else
1088ZEXTERN int ZEXPORTVA z_gzprintf OF((gzFile file, const char *format, ...))
1089#endif
inikep3eabe9b2016-05-12 17:15:41 +02001090{
inikep252c20d2016-09-23 09:08:40 +02001091 if (!g_ZWRAP_useZSTDcompression) {
inikep3eabe9b2016-05-12 17:15:41 +02001092 int ret;
1093 char buf[1024];
1094 va_list args;
1095 va_start (args, format);
1096 ret = vsprintf (buf, format, args);
1097 va_end (args);
1098
1099 ret = gzprintf(file, buf);
inikep3eabe9b2016-05-12 17:15:41 +02001100 return ret;
1101 }
inikep4af2c9d2016-06-03 17:39:31 +02001102 FINISH_WITH_GZ_ERR("gzprintf is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001103}
1104
1105
1106ZEXTERN int ZEXPORT z_gzputs OF((gzFile file, const char *s))
1107{
inikep252c20d2016-09-23 09:08:40 +02001108 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001109 return gzputs(file, s);
inikep4af2c9d2016-06-03 17:39:31 +02001110 FINISH_WITH_GZ_ERR("gzputs is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001111}
1112
1113
1114ZEXTERN char * ZEXPORT z_gzgets OF((gzFile file, char *buf, int len))
1115{
inikep252c20d2016-09-23 09:08:40 +02001116 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001117 return gzgets(file, buf, len);
1118 FINISH_WITH_NULL_ERR("gzgets is not supported!");
1119}
1120
1121
1122ZEXTERN int ZEXPORT z_gzputc OF((gzFile file, int c))
1123{
inikep252c20d2016-09-23 09:08:40 +02001124 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001125 return gzputc(file, c);
inikep4af2c9d2016-06-03 17:39:31 +02001126 FINISH_WITH_GZ_ERR("gzputc is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001127}
1128
1129
inikepbf25d7a2016-06-02 10:19:35 +02001130#if ZLIB_VERNUM == 0x1260
1131ZEXTERN int ZEXPORT z_gzgetc_ OF((gzFile file))
1132#else
inikep3eabe9b2016-05-12 17:15:41 +02001133ZEXTERN int ZEXPORT z_gzgetc OF((gzFile file))
inikepbf25d7a2016-06-02 10:19:35 +02001134#endif
inikep3eabe9b2016-05-12 17:15:41 +02001135{
inikep252c20d2016-09-23 09:08:40 +02001136 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001137 return gzgetc(file);
inikep4af2c9d2016-06-03 17:39:31 +02001138 FINISH_WITH_GZ_ERR("gzgetc is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001139}
1140
1141
1142ZEXTERN int ZEXPORT z_gzungetc OF((int c, gzFile file))
1143{
inikep252c20d2016-09-23 09:08:40 +02001144 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001145 return gzungetc(c, file);
inikep4af2c9d2016-06-03 17:39:31 +02001146 FINISH_WITH_GZ_ERR("gzungetc is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001147}
1148
1149
1150ZEXTERN int ZEXPORT z_gzflush OF((gzFile file, int flush))
1151{
inikep252c20d2016-09-23 09:08:40 +02001152 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001153 return gzflush(file, flush);
inikep4af2c9d2016-06-03 17:39:31 +02001154 FINISH_WITH_GZ_ERR("gzflush is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001155}
1156
1157
1158ZEXTERN z_off_t ZEXPORT z_gzseek OF((gzFile file, z_off_t offset, int whence))
1159{
inikep252c20d2016-09-23 09:08:40 +02001160 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001161 return gzseek(file, offset, whence);
inikep4af2c9d2016-06-03 17:39:31 +02001162 FINISH_WITH_GZ_ERR("gzseek is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001163}
1164
1165
1166ZEXTERN int ZEXPORT z_gzrewind OF((gzFile file))
1167{
inikep252c20d2016-09-23 09:08:40 +02001168 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001169 return gzrewind(file);
inikep4af2c9d2016-06-03 17:39:31 +02001170 FINISH_WITH_GZ_ERR("gzrewind is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001171}
1172
1173
1174ZEXTERN z_off_t ZEXPORT z_gztell OF((gzFile file))
1175{
inikep252c20d2016-09-23 09:08:40 +02001176 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001177 return gztell(file);
inikep4af2c9d2016-06-03 17:39:31 +02001178 FINISH_WITH_GZ_ERR("gztell is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001179}
1180
1181
inikep3eabe9b2016-05-12 17:15:41 +02001182ZEXTERN int ZEXPORT z_gzeof OF((gzFile file))
1183{
inikep252c20d2016-09-23 09:08:40 +02001184 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001185 return gzeof(file);
inikep4af2c9d2016-06-03 17:39:31 +02001186 FINISH_WITH_GZ_ERR("gzeof is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001187}
1188
1189
1190ZEXTERN int ZEXPORT z_gzdirect OF((gzFile file))
1191{
inikep252c20d2016-09-23 09:08:40 +02001192 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001193 return gzdirect(file);
inikep4af2c9d2016-06-03 17:39:31 +02001194 FINISH_WITH_GZ_ERR("gzdirect is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001195}
1196
1197
1198ZEXTERN int ZEXPORT z_gzclose OF((gzFile file))
1199{
inikep252c20d2016-09-23 09:08:40 +02001200 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001201 return gzclose(file);
inikep4af2c9d2016-06-03 17:39:31 +02001202 FINISH_WITH_GZ_ERR("gzclose is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001203}
1204
1205
inikep3eabe9b2016-05-12 17:15:41 +02001206ZEXTERN const char * ZEXPORT z_gzerror OF((gzFile file, int *errnum))
1207{
inikep252c20d2016-09-23 09:08:40 +02001208 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001209 return gzerror(file, errnum);
1210 FINISH_WITH_NULL_ERR("gzerror is not supported!");
1211}
1212
1213
1214ZEXTERN void ZEXPORT z_gzclearerr OF((gzFile file))
1215{
inikep252c20d2016-09-23 09:08:40 +02001216 if (!g_ZWRAP_useZSTDcompression)
inikep3eabe9b2016-05-12 17:15:41 +02001217 gzclearerr(file);
1218}
1219
1220
1221#endif /* !Z_SOLO */
1222
1223
1224 /* checksum functions */
1225
1226ZEXTERN uLong ZEXPORT z_adler32 OF((uLong adler, const Bytef *buf, uInt len))
1227{
1228 return adler32(adler, buf, len);
1229}
1230
1231ZEXTERN uLong ZEXPORT z_crc32 OF((uLong crc, const Bytef *buf, uInt len))
1232{
1233 return crc32(crc, buf, len);
1234}