blob: 28f42526efb688f6612f54d0f9ded3ef95c14949 [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
inikep8b452452016-06-01 10:50:17 +020023#define ZWRAP_DEFAULT_CLEVEL 5 /* Z_DEFAULT_COMPRESSION is translated to ZWRAP_DEFAULT_CLEVEL for zstd */
inikep3eabe9b2016-05-12 17:15:41 +020024
inikep554b3b92016-09-20 15:18:00 +020025#define LOG_WRAPPERC(...) /*printf(__VA_ARGS__)*/
26#define LOG_WRAPPERD(...) /*printf(__VA_ARGS__)*/
inikep3eabe9b2016-05-12 17:15:41 +020027
28
inikep4af2c9d2016-06-03 17:39:31 +020029#define FINISH_WITH_GZ_ERR(msg) { \
30 (void)msg; \
inikep18f66452016-09-20 12:50:59 +020031 return Z_STREAM_ERROR; \
inikep4af2c9d2016-06-03 17:39:31 +020032}
33
34#define FINISH_WITH_ERR(strm, message) { \
35 strm->msg = message; \
inikep18f66452016-09-20 12:50:59 +020036 return Z_STREAM_ERROR; \
inikep3eabe9b2016-05-12 17:15:41 +020037}
38
39#define FINISH_WITH_NULL_ERR(msg) { \
inikep4af2c9d2016-06-03 17:39:31 +020040 (void)msg; \
inikep3eabe9b2016-05-12 17:15:41 +020041 return NULL; \
42}
43
44#ifndef ZWRAP_USE_ZSTD
45 #define ZWRAP_USE_ZSTD 0
46#endif
47
48static int g_useZSTD = ZWRAP_USE_ZSTD; /* 0 = don't use ZSTD */
49
50
51
52void useZSTD(int turn_on) { g_useZSTD = turn_on; }
53
inikep614fdde2016-06-02 18:40:41 +020054int isUsingZSTD(void) { return g_useZSTD; }
inikep3eabe9b2016-05-12 17:15:41 +020055
inikep614fdde2016-06-02 18:40:41 +020056const char * zstdVersion(void) { return ZSTD_VERSION_STRING; }
inikep3eabe9b2016-05-12 17:15:41 +020057
58ZEXTERN const char * ZEXPORT z_zlibVersion OF((void)) { return zlibVersion(); }
59
60
inikep7cab86f2016-06-02 18:24:07 +020061static void* ZWRAP_allocFunction(void* opaque, size_t size)
inikepff9114a2016-06-02 16:52:36 +020062{
63 z_streamp strm = (z_streamp) opaque;
64 void* address = strm->zalloc(strm->opaque, 1, size);
65 /* printf("ZWRAP alloc %p, %d \n", address, (int)size); */
66 return address;
67}
68
inikep7cab86f2016-06-02 18:24:07 +020069static void ZWRAP_freeFunction(void* opaque, void* address)
inikepff9114a2016-06-02 16:52:36 +020070{
71 z_streamp strm = (z_streamp) opaque;
72 strm->zfree(strm->opaque, address);
73 /* if (address) printf("ZWRAP free %p \n", address); */
74}
75
76
inikep3eabe9b2016-05-12 17:15:41 +020077
78/* *** Compression *** */
79
80typedef struct {
inikepb0773452016-09-16 14:06:10 +020081 ZSTD_CStream* zbc;
inikep3eabe9b2016-05-12 17:15:41 +020082 int compressionLevel;
inikep2a746092016-06-03 14:53:51 +020083 ZSTD_customMem customMem;
inikepff9114a2016-06-02 16:52:36 +020084 z_stream allocFunc; /* copy of zalloc, zfree, opaque */
inikepb0773452016-09-16 14:06:10 +020085 ZSTD_inBuffer inBuffer;
86 ZSTD_outBuffer outBuffer;
inikep3eabe9b2016-05-12 17:15:41 +020087} ZWRAP_CCtx;
88
89
inikepf040be92016-06-03 16:31:57 +020090size_t ZWRAP_freeCCtx(ZWRAP_CCtx* zwc)
91{
92 if (zwc==NULL) return 0; /* support free on NULL */
inikepb0773452016-09-16 14:06:10 +020093 ZSTD_freeCStream(zwc->zbc);
inikepf040be92016-06-03 16:31:57 +020094 zwc->customMem.customFree(zwc->customMem.opaque, zwc);
95 return 0;
96}
97
98
inikepff9114a2016-06-02 16:52:36 +020099ZWRAP_CCtx* ZWRAP_createCCtx(z_streamp strm)
inikep3eabe9b2016-05-12 17:15:41 +0200100{
inikep2a746092016-06-03 14:53:51 +0200101 ZWRAP_CCtx* zwc;
102
inikepff9114a2016-06-02 16:52:36 +0200103 if (strm->zalloc && strm->zfree) {
inikep2a746092016-06-03 14:53:51 +0200104 zwc = (ZWRAP_CCtx*)strm->zalloc(strm->opaque, 1, sizeof(ZWRAP_CCtx));
105 if (zwc==NULL) return NULL;
106 memset(zwc, 0, sizeof(ZWRAP_CCtx));
107 memcpy(&zwc->allocFunc, strm, sizeof(z_stream));
108 { ZSTD_customMem ZWRAP_customMem = { ZWRAP_allocFunction, ZWRAP_freeFunction, &zwc->allocFunc };
109 memcpy(&zwc->customMem, &ZWRAP_customMem, sizeof(ZSTD_customMem));
110 }
111 } else {
112 zwc = (ZWRAP_CCtx*)defaultCustomMem.customAlloc(defaultCustomMem.opaque, sizeof(ZWRAP_CCtx));
113 if (zwc==NULL) return NULL;
114 memset(zwc, 0, sizeof(ZWRAP_CCtx));
115 memcpy(&zwc->customMem, &defaultCustomMem, sizeof(ZSTD_customMem));
inikepff9114a2016-06-02 16:52:36 +0200116 }
inikep2a746092016-06-03 14:53:51 +0200117
inikepb0773452016-09-16 14:06:10 +0200118 zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
inikepf040be92016-06-03 16:31:57 +0200119 if (zwc->zbc == NULL) { ZWRAP_freeCCtx(zwc); return NULL; }
inikep3eabe9b2016-05-12 17:15:41 +0200120 return zwc;
121}
122
123
inikepc4ab5712016-09-19 14:54:13 +0200124int ZWRAPC_finish_with_error(ZWRAP_CCtx* zwc, z_streamp strm, int error)
125{
inikep554b3b92016-09-20 15:18:00 +0200126 LOG_WRAPPERC("- ZWRAPC_finish_with_error=%d\n", error);
inikepc4ab5712016-09-19 14:54:13 +0200127 if (zwc) ZWRAP_freeCCtx(zwc);
128 if (strm) strm->state = NULL;
inikep18f66452016-09-20 12:50:59 +0200129 return (error) ? error : Z_STREAM_ERROR;
inikepc4ab5712016-09-19 14:54:13 +0200130}
131
132
inikep3eabe9b2016-05-12 17:15:41 +0200133ZEXTERN int ZEXPORT z_deflateInit_ OF((z_streamp strm, int level,
134 const char *version, int stream_size))
135{
136 ZWRAP_CCtx* zwc;
Yann Collet4ded9e52016-08-30 10:04:33 -0700137
inikep554b3b92016-09-20 15:18:00 +0200138 LOG_WRAPPERC("- deflateInit level=%d\n", level);
inikep3eabe9b2016-05-12 17:15:41 +0200139 if (!g_useZSTD) {
inikep3eabe9b2016-05-12 17:15:41 +0200140 return deflateInit_((strm), (level), version, stream_size);
141 }
142
inikepff9114a2016-06-02 16:52:36 +0200143 zwc = ZWRAP_createCCtx(strm);
inikep3eabe9b2016-05-12 17:15:41 +0200144 if (zwc == NULL) return Z_MEM_ERROR;
145
inikep8b452452016-06-01 10:50:17 +0200146 if (level == Z_DEFAULT_COMPRESSION)
147 level = ZWRAP_DEFAULT_CLEVEL;
148
inikepb0773452016-09-16 14:06:10 +0200149 { size_t const errorCode = ZSTD_initCStream(zwc->zbc, level);
inikepc4ab5712016-09-19 14:54:13 +0200150 if (ZSTD_isError(errorCode)) return ZWRAPC_finish_with_error(zwc, strm, 0); }
inikep3eabe9b2016-05-12 17:15:41 +0200151
152 zwc->compressionLevel = level;
inikepe02bf992016-06-02 12:00:32 +0200153 strm->state = (struct internal_state*) zwc; /* use state which in not used by user */
inikep3eabe9b2016-05-12 17:15:41 +0200154 strm->total_in = 0;
155 strm->total_out = 0;
156 return Z_OK;
157}
158
159
inikep8b452452016-06-01 10:50:17 +0200160ZEXTERN int ZEXPORT z_deflateInit2_ OF((z_streamp strm, int level, int method,
inikep3eabe9b2016-05-12 17:15:41 +0200161 int windowBits, int memLevel,
162 int strategy, const char *version,
163 int stream_size))
164{
165 if (!g_useZSTD)
166 return deflateInit2_(strm, level, method, windowBits, memLevel, strategy, version, stream_size);
167
168 return z_deflateInit_ (strm, level, version, stream_size);
169}
170
171
inikep61016872016-09-19 14:27:29 +0200172ZEXTERN int ZEXPORT z_deflateReset OF((z_streamp strm))
173{
inikep554b3b92016-09-20 15:18:00 +0200174 LOG_WRAPPERC("- deflateReset\n");
inikep61016872016-09-19 14:27:29 +0200175 if (!g_useZSTD)
176 return deflateReset(strm);
inikep18f66452016-09-20 12:50:59 +0200177
inikepc038c302016-09-20 12:54:26 +0200178 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
inikepc038c302016-09-20 12:54:26 +0200179 if (zwc == NULL) return Z_STREAM_ERROR;
180 { size_t const errorCode = ZSTD_resetCStream(zwc->zbc, 0);
181 if (ZSTD_isError(errorCode)) return ZWRAPC_finish_with_error(zwc, strm, 0); }
182 }
183
184 strm->total_in = 0;
185 strm->total_out = 0;
186 return Z_OK;
inikep61016872016-09-19 14:27:29 +0200187}
188
189
inikep3eabe9b2016-05-12 17:15:41 +0200190ZEXTERN int ZEXPORT z_deflateSetDictionary OF((z_streamp strm,
191 const Bytef *dictionary,
192 uInt dictLength))
193{
inikep554b3b92016-09-20 15:18:00 +0200194 if (!g_useZSTD) {
195 LOG_WRAPPERC("- deflateSetDictionary\n");
inikep3eabe9b2016-05-12 17:15:41 +0200196 return deflateSetDictionary(strm, dictionary, dictLength);
inikep554b3b92016-09-20 15:18:00 +0200197 }
inikep3eabe9b2016-05-12 17:15:41 +0200198
inikepe02bf992016-06-02 12:00:32 +0200199 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
inikep554b3b92016-09-20 15:18:00 +0200200 LOG_WRAPPERC("- deflateSetDictionary level=%d\n", (int)zwc->compressionLevel);
inikep18f66452016-09-20 12:50:59 +0200201 if (zwc == NULL) return Z_STREAM_ERROR;
inikepb0773452016-09-16 14:06:10 +0200202 { size_t const errorCode = ZSTD_initCStream_usingDict(zwc->zbc, dictionary, dictLength, zwc->compressionLevel);
inikepc4ab5712016-09-19 14:54:13 +0200203 if (ZSTD_isError(errorCode)) return ZWRAPC_finish_with_error(zwc, strm, 0); }
inikep3eabe9b2016-05-12 17:15:41 +0200204 }
Yann Collet4ded9e52016-08-30 10:04:33 -0700205
inikep3eabe9b2016-05-12 17:15:41 +0200206 return Z_OK;
207}
208
inikepc4ab5712016-09-19 14:54:13 +0200209
inikep3eabe9b2016-05-12 17:15:41 +0200210ZEXTERN int ZEXPORT z_deflate OF((z_streamp strm, int flush))
211{
212 ZWRAP_CCtx* zwc;
213
214 if (!g_useZSTD) {
inikep554b3b92016-09-20 15:18:00 +0200215 int res;
216 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);
217 res = deflate(strm, flush);
218 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 +0200219 return res;
220 }
221
inikepe02bf992016-06-02 12:00:32 +0200222 zwc = (ZWRAP_CCtx*) strm->state;
inikep18f66452016-09-20 12:50:59 +0200223 if (zwc == NULL) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +0200224
inikep554b3b92016-09-20 15:18:00 +0200225 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 +0200226 if (strm->avail_in > 0) {
inikepb0773452016-09-16 14:06:10 +0200227 zwc->inBuffer.src = strm->next_in;
228 zwc->inBuffer.size = strm->avail_in;
229 zwc->inBuffer.pos = 0;
230 zwc->outBuffer.dst = strm->next_out;
231 zwc->outBuffer.size = strm->avail_out;
232 zwc->outBuffer.pos = 0;
233 { size_t const errorCode = ZSTD_compressStream(zwc->zbc, &zwc->outBuffer, &zwc->inBuffer);
inikep554b3b92016-09-20 15:18:00 +0200234 LOG_WRAPPERC("deflate ZSTD_compressStream srcSize=%d dstCapacity=%d\n", (int)zwc->inBuffer.size, (int)zwc->outBuffer.size);
inikepc4ab5712016-09-19 14:54:13 +0200235 if (ZSTD_isError(errorCode)) return ZWRAPC_finish_with_error(zwc, strm, 0);
inikepb0773452016-09-16 14:06:10 +0200236 }
237 strm->next_out += zwc->outBuffer.pos;
238 strm->total_out += zwc->outBuffer.pos;
239 strm->avail_out -= zwc->outBuffer.pos;
240 strm->total_in += zwc->inBuffer.pos;
241 strm->next_in += zwc->inBuffer.pos;
242 strm->avail_in -= zwc->inBuffer.pos;
inikep3eabe9b2016-05-12 17:15:41 +0200243 }
244
inikep61016872016-09-19 14:27:29 +0200245 if (flush == Z_FULL_FLUSH || flush == Z_BLOCK || flush == Z_TREES) FINISH_WITH_ERR(strm, "Z_FULL_FLUSH, Z_BLOCK and Z_TREES are not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200246
Dmitry Krot2ed3ba22016-08-13 22:02:45 +0300247 if (flush == Z_FINISH) {
inikep3eabe9b2016-05-12 17:15:41 +0200248 size_t bytesLeft;
inikepb0773452016-09-16 14:06:10 +0200249 zwc->outBuffer.dst = strm->next_out;
250 zwc->outBuffer.size = strm->avail_out;
251 zwc->outBuffer.pos = 0;
inikepe46bad02016-09-19 13:24:07 +0200252 bytesLeft = ZSTD_endStream(zwc->zbc, &zwc->outBuffer);
inikep554b3b92016-09-20 15:18:00 +0200253 LOG_WRAPPERC("deflate ZSTD_endStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
inikepc4ab5712016-09-19 14:54:13 +0200254 if (ZSTD_isError(bytesLeft)) return ZWRAPC_finish_with_error(zwc, strm, 0);
inikepb0773452016-09-16 14:06:10 +0200255 strm->next_out += zwc->outBuffer.pos;
256 strm->total_out += zwc->outBuffer.pos;
257 strm->avail_out -= zwc->outBuffer.pos;
inikep554b3b92016-09-20 15:18:00 +0200258 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 +0200259 }
inikepe46bad02016-09-19 13:24:07 +0200260 else
inikep61016872016-09-19 14:27:29 +0200261 if (flush == Z_SYNC_FLUSH || flush == Z_PARTIAL_FLUSH) {
Dmitry Krot2ed3ba22016-08-13 22:02:45 +0300262 size_t bytesLeft;
inikep8fc58482016-09-16 17:14:01 +0200263 zwc->outBuffer.dst = strm->next_out;
264 zwc->outBuffer.size = strm->avail_out;
265 zwc->outBuffer.pos = 0;
inikepb0773452016-09-16 14:06:10 +0200266 bytesLeft = ZSTD_flushStream(zwc->zbc, &zwc->outBuffer);
inikep554b3b92016-09-20 15:18:00 +0200267 LOG_WRAPPERC("deflate ZSTD_flushStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
inikepc4ab5712016-09-19 14:54:13 +0200268 if (ZSTD_isError(bytesLeft)) return ZWRAPC_finish_with_error(zwc, strm, 0);
inikepb0773452016-09-16 14:06:10 +0200269 strm->next_out += zwc->outBuffer.pos;
270 strm->total_out += zwc->outBuffer.pos;
271 strm->avail_out -= zwc->outBuffer.pos;
Dmitry Krot2ed3ba22016-08-13 22:02:45 +0300272 }
inikep554b3b92016-09-20 15:18:00 +0200273 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 +0200274 return Z_OK;
275}
276
277
Yann Collet4ded9e52016-08-30 10:04:33 -0700278ZEXTERN int ZEXPORT z_deflateEnd OF((z_streamp strm))
inikep3eabe9b2016-05-12 17:15:41 +0200279{
280 if (!g_useZSTD) {
inikep554b3b92016-09-20 15:18:00 +0200281 LOG_WRAPPERC("- deflateEnd\n");
inikep3eabe9b2016-05-12 17:15:41 +0200282 return deflateEnd(strm);
283 }
inikep554b3b92016-09-20 15:18:00 +0200284 LOG_WRAPPERC("- deflateEnd total_in=%d total_out=%d\n", (int)(strm->total_in), (int)(strm->total_out));
inikepe02bf992016-06-02 12:00:32 +0200285 { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
inikep3eabe9b2016-05-12 17:15:41 +0200286 size_t const errorCode = ZWRAP_freeCCtx(zwc);
inikepc4ab5712016-09-19 14:54:13 +0200287 strm->state = NULL;
inikep18f66452016-09-20 12:50:59 +0200288 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +0200289 }
290 return Z_OK;
291}
292
293
294ZEXTERN uLong ZEXPORT z_deflateBound OF((z_streamp strm,
295 uLong sourceLen))
296{
297 if (!g_useZSTD)
298 return deflateBound(strm, sourceLen);
299
300 return ZSTD_compressBound(sourceLen);
301}
302
303
304ZEXTERN int ZEXPORT z_deflateParams OF((z_streamp strm,
305 int level,
306 int strategy))
307{
308 if (!g_useZSTD) {
inikep554b3b92016-09-20 15:18:00 +0200309 LOG_WRAPPERC("- deflateParams level=%d strategy=%d\n", level, strategy);
inikep3eabe9b2016-05-12 17:15:41 +0200310 return deflateParams(strm, level, strategy);
311 }
312
313 return Z_OK;
314}
315
316
317
318
319
320/* *** Decompression *** */
321
322typedef struct {
inikepb0773452016-09-16 14:06:10 +0200323 ZSTD_DStream* zbd;
inikep8fc58482016-09-16 17:14:01 +0200324 char headerBuf[16]; /* should be equal or bigger than ZSTD_frameHeaderSize_min */
inikep3eabe9b2016-05-12 17:15:41 +0200325 int errorCount;
Yann Collet4ded9e52016-08-30 10:04:33 -0700326
inikep3eabe9b2016-05-12 17:15:41 +0200327 /* zlib params */
328 int stream_size;
329 char *version;
330 int windowBits;
inikepf040be92016-06-03 16:31:57 +0200331 ZSTD_customMem customMem;
inikepff9114a2016-06-02 16:52:36 +0200332 z_stream allocFunc; /* copy of zalloc, zfree, opaque */
inikepb0773452016-09-16 14:06:10 +0200333 ZSTD_inBuffer inBuffer;
334 ZSTD_outBuffer outBuffer;
inikep3eabe9b2016-05-12 17:15:41 +0200335} ZWRAP_DCtx;
336
337
inikepff9114a2016-06-02 16:52:36 +0200338ZWRAP_DCtx* ZWRAP_createDCtx(z_streamp strm)
inikep3eabe9b2016-05-12 17:15:41 +0200339{
inikepf040be92016-06-03 16:31:57 +0200340 ZWRAP_DCtx* zwd;
341
342 if (strm->zalloc && strm->zfree) {
343 zwd = (ZWRAP_DCtx*)strm->zalloc(strm->opaque, 1, sizeof(ZWRAP_DCtx));
344 if (zwd==NULL) return NULL;
345 memset(zwd, 0, sizeof(ZWRAP_DCtx));
346 memcpy(&zwd->allocFunc, strm, sizeof(z_stream));
347 { ZSTD_customMem ZWRAP_customMem = { ZWRAP_allocFunction, ZWRAP_freeFunction, &zwd->allocFunc };
348 memcpy(&zwd->customMem, &ZWRAP_customMem, sizeof(ZSTD_customMem));
349 }
350 } else {
351 zwd = (ZWRAP_DCtx*)defaultCustomMem.customAlloc(defaultCustomMem.opaque, sizeof(ZWRAP_DCtx));
352 if (zwd==NULL) return NULL;
353 memset(zwd, 0, sizeof(ZWRAP_DCtx));
354 memcpy(&zwd->customMem, &defaultCustomMem, sizeof(ZSTD_customMem));
355 }
inikep8fc58482016-09-16 17:14:01 +0200356 zwd->outBuffer.pos = 0;
357 zwd->outBuffer.size = 0;
inikepf040be92016-06-03 16:31:57 +0200358
inikep3eabe9b2016-05-12 17:15:41 +0200359 return zwd;
360}
361
362
363size_t ZWRAP_freeDCtx(ZWRAP_DCtx* zwd)
364{
365 if (zwd==NULL) return 0; /* support free on null */
inikepb0773452016-09-16 14:06:10 +0200366 ZSTD_freeDStream(zwd->zbd);
inikepf040be92016-06-03 16:31:57 +0200367 if (zwd->version) zwd->customMem.customFree(zwd->customMem.opaque, zwd->version);
368 zwd->customMem.customFree(zwd->customMem.opaque, zwd);
inikep3eabe9b2016-05-12 17:15:41 +0200369 return 0;
370}
371
372
inikepc4ab5712016-09-19 14:54:13 +0200373int ZWRAPD_finish_with_error(ZWRAP_DCtx* zwd, z_streamp strm, int error)
inikep0bb930b2016-09-19 14:31:16 +0200374{
inikep554b3b92016-09-20 15:18:00 +0200375 LOG_WRAPPERD("- ZWRAPD_finish_with_error=%d\n", error);
inikep0bb930b2016-09-19 14:31:16 +0200376 if (zwd) ZWRAP_freeDCtx(zwd);
inikepc4ab5712016-09-19 14:54:13 +0200377 if (strm) strm->state = NULL;
inikep18f66452016-09-20 12:50:59 +0200378 return (error) ? error : Z_STREAM_ERROR;
inikep0bb930b2016-09-19 14:31:16 +0200379}
380
381
inikep3eabe9b2016-05-12 17:15:41 +0200382ZEXTERN int ZEXPORT z_inflateInit_ OF((z_streamp strm,
383 const char *version, int stream_size))
384{
inikepff9114a2016-06-02 16:52:36 +0200385 ZWRAP_DCtx* zwd = ZWRAP_createDCtx(strm);
inikep554b3b92016-09-20 15:18:00 +0200386 LOG_WRAPPERD("- inflateInit\n");
inikepc4ab5712016-09-19 14:54:13 +0200387 if (zwd == NULL) return ZWRAPD_finish_with_error(zwd, strm, 0);
inikep3eabe9b2016-05-12 17:15:41 +0200388
inikepf040be92016-06-03 16:31:57 +0200389 zwd->version = zwd->customMem.customAlloc(zwd->customMem.opaque, strlen(version) + 1);
inikepc4ab5712016-09-19 14:54:13 +0200390 if (zwd->version == NULL) return ZWRAPD_finish_with_error(zwd, strm, 0);
inikepf040be92016-06-03 16:31:57 +0200391 strcpy(zwd->version, version);
inikep3eabe9b2016-05-12 17:15:41 +0200392
inikepf040be92016-06-03 16:31:57 +0200393 zwd->stream_size = stream_size;
inikepe02bf992016-06-02 12:00:32 +0200394 strm->state = (struct internal_state*) zwd; /* use state which in not used by user */
inikep3eabe9b2016-05-12 17:15:41 +0200395 strm->total_in = 0;
396 strm->total_out = 0;
397 strm->reserved = 1; /* mark as unknown steam */
398
399 return Z_OK;
400}
401
402
403ZEXTERN int ZEXPORT z_inflateInit2_ OF((z_streamp strm, int windowBits,
404 const char *version, int stream_size))
405{
406 int ret = z_inflateInit_ (strm, version, stream_size);
407 if (ret == Z_OK) {
408 ZWRAP_DCtx* zwd = (ZWRAP_DCtx*)strm->state;
409 zwd->windowBits = windowBits;
410 }
411 return ret;
412}
413
414
inikep18f66452016-09-20 12:50:59 +0200415ZEXTERN int ZEXPORT z_inflateReset OF((z_streamp strm))
416{
inikep554b3b92016-09-20 15:18:00 +0200417 LOG_WRAPPERD("- inflateReset\n");
inikep18f66452016-09-20 12:50:59 +0200418 if (!strm->reserved)
419 return inflateReset(strm);
420
inikep554b3b92016-09-20 15:18:00 +0200421 { ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
422 if (zwd == NULL) return Z_STREAM_ERROR;
423 { size_t const errorCode = ZSTD_resetDStream(zwd->zbd);
424 if (ZSTD_isError(errorCode)) return ZWRAPD_finish_with_error(zwd, strm, 0); }
425 }
426
inikep18f66452016-09-20 12:50:59 +0200427 strm->total_in = 0;
428 strm->total_out = 0;
429 return Z_OK;
430}
431
432
inikep3eabe9b2016-05-12 17:15:41 +0200433ZEXTERN int ZEXPORT z_inflateSetDictionary OF((z_streamp strm,
434 const Bytef *dictionary,
435 uInt dictLength))
436{
inikep554b3b92016-09-20 15:18:00 +0200437 LOG_WRAPPERD("- inflateSetDictionary\n");
inikep3eabe9b2016-05-12 17:15:41 +0200438 if (!strm->reserved)
439 return inflateSetDictionary(strm, dictionary, dictLength);
440
inikep1c9521f2016-06-13 12:00:46 +0200441 { size_t errorCode;
442 ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
inikep18f66452016-09-20 12:50:59 +0200443 if (strm->state == NULL) return Z_STREAM_ERROR;
inikepb0773452016-09-16 14:06:10 +0200444 errorCode = ZSTD_initDStream_usingDict(zwd->zbd, dictionary, dictLength);
inikepc4ab5712016-09-19 14:54:13 +0200445 if (ZSTD_isError(errorCode)) return ZWRAPD_finish_with_error(zwd, strm, 0);
Yann Collet4ded9e52016-08-30 10:04:33 -0700446
inikep8fc58482016-09-16 17:14:01 +0200447 if (strm->total_in == ZSTD_HEADERSIZE) {
inikepb0773452016-09-16 14:06:10 +0200448 zwd->inBuffer.src = zwd->headerBuf;
449 zwd->inBuffer.size = strm->total_in;
450 zwd->inBuffer.pos = 0;
451 zwd->outBuffer.dst = strm->next_out;
452 zwd->outBuffer.size = 0;
453 zwd->outBuffer.pos = 0;
454 errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
inikep554b3b92016-09-20 15:18:00 +0200455 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 +0200456 if (zwd->inBuffer.pos < zwd->outBuffer.size || ZSTD_isError(errorCode)) {
inikep554b3b92016-09-20 15:18:00 +0200457 LOG_WRAPPERD("ERROR: ZSTD_decompressStream %s\n", ZSTD_getErrorName(errorCode));
inikepc4ab5712016-09-19 14:54:13 +0200458 return ZWRAPD_finish_with_error(zwd, strm, 0);
inikep3eabe9b2016-05-12 17:15:41 +0200459 }
460 }
461 }
462
463 return Z_OK;
464}
465
466
Yann Collet4ded9e52016-08-30 10:04:33 -0700467ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush))
inikep3eabe9b2016-05-12 17:15:41 +0200468{
inikep554b3b92016-09-20 15:18:00 +0200469 int res;
470 if (!strm->reserved) {
471 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);
472 res = inflate(strm, flush);
473 LOG_WRAPPERD("- inflate2 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);
474 return res;
475 }
inikep3eabe9b2016-05-12 17:15:41 +0200476
477 if (strm->avail_in > 0) {
inikep8fc58482016-09-16 17:14:01 +0200478 size_t errorCode, srcSize, inPos;
inikep3eabe9b2016-05-12 17:15:41 +0200479 ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
inikep18f66452016-09-20 12:50:59 +0200480 if (strm->state == NULL) return Z_STREAM_ERROR;
inikep554b3b92016-09-20 15:18:00 +0200481 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);
inikep8fc58482016-09-16 17:14:01 +0200482 // if (((strm->avail_in < ZSTD_HEADERSIZE) || (strm->total_in > 0)) && (strm->total_in < ZLIB_HEADERSIZE))
483 if (strm->total_in < ZLIB_HEADERSIZE)
inikep3eabe9b2016-05-12 17:15:41 +0200484 {
inikep8fc58482016-09-16 17:14:01 +0200485 // printf(".");
486 srcSize = MIN(strm->avail_in, ZLIB_HEADERSIZE - strm->total_in);
inikep3eabe9b2016-05-12 17:15:41 +0200487 memcpy(zwd->headerBuf+strm->total_in, strm->next_in, srcSize);
488 strm->total_in += srcSize;
489 strm->next_in += srcSize;
490 strm->avail_in -= srcSize;
inikep8fc58482016-09-16 17:14:01 +0200491 if (strm->total_in < ZLIB_HEADERSIZE) return Z_OK;
inikep3eabe9b2016-05-12 17:15:41 +0200492
493 if (MEM_readLE32(zwd->headerBuf) != ZSTD_MAGICNUMBER) {
494 z_stream strm2;
495 strm2.next_in = strm->next_in;
496 strm2.avail_in = strm->avail_in;
497 strm2.next_out = strm->next_out;
498 strm2.avail_out = strm->avail_out;
499
500 if (zwd->windowBits)
501 errorCode = inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size);
502 else
503 errorCode = inflateInit_(strm, zwd->version, zwd->stream_size);
inikep554b3b92016-09-20 15:18:00 +0200504 LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", (int)errorCode);
inikepc4ab5712016-09-19 14:54:13 +0200505 if (errorCode != Z_OK) return ZWRAPD_finish_with_error(zwd, strm, (int)errorCode);
inikep3eabe9b2016-05-12 17:15:41 +0200506
507 /* inflate header */
508 strm->next_in = (unsigned char*)zwd->headerBuf;
inikep8fc58482016-09-16 17:14:01 +0200509 strm->avail_in = ZLIB_HEADERSIZE;
inikep3eabe9b2016-05-12 17:15:41 +0200510 strm->avail_out = 0;
511 errorCode = inflate(strm, Z_NO_FLUSH);
inikep554b3b92016-09-20 15:18:00 +0200512 LOG_WRAPPERD("ZLIB inflate errorCode=%d strm->avail_in=%d\n", (int)errorCode, (int)strm->avail_in);
inikepc4ab5712016-09-19 14:54:13 +0200513 if (errorCode != Z_OK) return ZWRAPD_finish_with_error(zwd, strm, (int)errorCode);
inikep1c9521f2016-06-13 12:00:46 +0200514 if (strm->avail_in > 0) goto error;
Yann Collet4ded9e52016-08-30 10:04:33 -0700515
inikep3eabe9b2016-05-12 17:15:41 +0200516 strm->next_in = strm2.next_in;
517 strm->avail_in = strm2.avail_in;
518 strm->next_out = strm2.next_out;
519 strm->avail_out = strm2.avail_out;
inikepe02bf992016-06-02 12:00:32 +0200520
inikep3eabe9b2016-05-12 17:15:41 +0200521 strm->reserved = 0; /* mark as zlib stream */
inikep614fdde2016-06-02 18:40:41 +0200522 errorCode = ZWRAP_freeDCtx(zwd);
inikep1c9521f2016-06-13 12:00:46 +0200523 if (ZSTD_isError(errorCode)) goto error;
inikepe02bf992016-06-02 12:00:32 +0200524
inikep554b3b92016-09-20 15:18:00 +0200525 if (flush == Z_INFLATE_SYNC) res = inflateSync(strm);
526 else res = inflate(strm, flush);
527 LOG_WRAPPERD("- inflate2 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);
528 return res;
inikep3eabe9b2016-05-12 17:15:41 +0200529 }
inikep8fc58482016-09-16 17:14:01 +0200530 }
531
532 // if (((strm->avail_in < ZSTD_HEADERSIZE) || (strm->total_in > 0)) && (strm->total_in < ZSTD_HEADERSIZE))
533 if (strm->total_in < ZSTD_HEADERSIZE)
534 {
535 // printf("+");
536 srcSize = MIN(strm->avail_in, ZSTD_HEADERSIZE - strm->total_in);
537 memcpy(zwd->headerBuf+strm->total_in, strm->next_in, srcSize);
538 strm->total_in += srcSize;
539 strm->next_in += srcSize;
540 strm->avail_in -= srcSize;
541 if (strm->total_in < ZSTD_HEADERSIZE) return Z_OK;
inikep3eabe9b2016-05-12 17:15:41 +0200542
inikepb0773452016-09-16 14:06:10 +0200543 zwd->zbd = ZSTD_createDStream_advanced(zwd->customMem);
inikep1c9521f2016-06-13 12:00:46 +0200544 if (zwd->zbd == NULL) goto error;
inikepff9114a2016-06-02 16:52:36 +0200545
inikepb0773452016-09-16 14:06:10 +0200546 errorCode = ZSTD_initDStream(zwd->zbd);
inikep1c9521f2016-06-13 12:00:46 +0200547 if (ZSTD_isError(errorCode)) goto error;
inikep3eabe9b2016-05-12 17:15:41 +0200548
inikep61016872016-09-19 14:27:29 +0200549 if (flush == Z_INFLATE_SYNC) { strm->msg = "inflateSync is not supported!"; goto error; }
550
inikep8fc58482016-09-16 17:14:01 +0200551 inPos = zwd->inBuffer.pos;
inikepb0773452016-09-16 14:06:10 +0200552 zwd->inBuffer.src = zwd->headerBuf;
inikep8fc58482016-09-16 17:14:01 +0200553 zwd->inBuffer.size = ZSTD_HEADERSIZE;
inikepb0773452016-09-16 14:06:10 +0200554 zwd->inBuffer.pos = 0;
555 zwd->outBuffer.dst = strm->next_out;
556 zwd->outBuffer.size = 0;
557 zwd->outBuffer.pos = 0;
558 errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
inikep554b3b92016-09-20 15:18:00 +0200559 LOG_WRAPPERD("inflate ZSTD_decompressStream1 errorCode=%d srcSize=%d dstCapacity=%d\n", (int)errorCode, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);
inikep3eabe9b2016-05-12 17:15:41 +0200560 if (ZSTD_isError(errorCode)) {
inikep554b3b92016-09-20 15:18:00 +0200561 LOG_WRAPPERD("ERROR: ZSTD_decompressStream %s\n", ZSTD_getErrorName(errorCode));
inikep1c9521f2016-06-13 12:00:46 +0200562 goto error;
inikep3eabe9b2016-05-12 17:15:41 +0200563 }
inikep554b3b92016-09-20 15:18:00 +0200564 // LOG_WRAPPERD("1srcSize=%d inpos=%d inBuffer.pos=%d inBuffer.size=%d outBuffer.pos=%d\n", (int)srcSize, (int)inPos, (int)zwd->inBuffer.pos, (int)zwd->inBuffer.size, (int)zwd->outBuffer.pos);
565 if (zwd->inBuffer.pos != zwd->inBuffer.size) return ZWRAPD_finish_with_error(zwd, strm, 0); /* not consumed */
inikep3eabe9b2016-05-12 17:15:41 +0200566 }
567
inikep8fc58482016-09-16 17:14:01 +0200568 inPos = 0;//zwd->inBuffer.pos;
inikepb0773452016-09-16 14:06:10 +0200569 zwd->inBuffer.src = strm->next_in;
570 zwd->inBuffer.size = strm->avail_in;
571 zwd->inBuffer.pos = 0;
572 zwd->outBuffer.dst = strm->next_out;
573 zwd->outBuffer.size = strm->avail_out;
574 zwd->outBuffer.pos = 0;
575 errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
inikep554b3b92016-09-20 15:18:00 +0200576 // LOG_WRAPPERD("2 inpos=%d inBuffer.pos=%d inBuffer.size=%d outBuffer.pos=%d\n", (int)inPos, (int)zwd->inBuffer.pos, (int)zwd->inBuffer.size, (int)zwd->outBuffer.pos);
577 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 +0200578 if (ZSTD_isError(errorCode)) {
inikep554b3b92016-09-20 15:18:00 +0200579 LOG_WRAPPERD("ERROR: ZSTD_decompressStream %s\n", ZSTD_getErrorName(errorCode));
inikep3eabe9b2016-05-12 17:15:41 +0200580 zwd->errorCount++;
inikep1c9521f2016-06-13 12:00:46 +0200581 if (zwd->errorCount<=1) return Z_NEED_DICT; else goto error;
inikep3eabe9b2016-05-12 17:15:41 +0200582 }
inikepb0773452016-09-16 14:06:10 +0200583 strm->next_out += zwd->outBuffer.pos;
584 strm->total_out += zwd->outBuffer.pos;
585 strm->avail_out -= zwd->outBuffer.pos;
inikep8fc58482016-09-16 17:14:01 +0200586 strm->total_in += zwd->inBuffer.pos - inPos;
587 strm->next_in += zwd->inBuffer.pos - inPos;
588 strm->avail_in -= zwd->inBuffer.pos - inPos;
inikep554b3b92016-09-20 15:18:00 +0200589 if (errorCode == 0) { LOG_WRAPPERD("inflate Z_STREAM_END1 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; }
590 goto finish;
inikep1c9521f2016-06-13 12:00:46 +0200591error:
inikepc4ab5712016-09-19 14:54:13 +0200592 return ZWRAPD_finish_with_error(zwd, strm, 0);
inikep3eabe9b2016-05-12 17:15:41 +0200593 }
inikep554b3b92016-09-20 15:18:00 +0200594finish:
595 LOG_WRAPPERD("- inflate2 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 +0200596 return Z_OK;
597}
598
599
600ZEXTERN int ZEXPORT z_inflateEnd OF((z_streamp strm))
601{
602 int ret = Z_OK;
603 if (!strm->reserved)
inikepe02bf992016-06-02 12:00:32 +0200604 return inflateEnd(strm);
Yann Collet4ded9e52016-08-30 10:04:33 -0700605
inikep554b3b92016-09-20 15:18:00 +0200606 LOG_WRAPPERD("- inflateEnd total_in=%d total_out=%d\n", (int)(strm->total_in), (int)(strm->total_out));
inikep3eabe9b2016-05-12 17:15:41 +0200607 { ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state;
608 size_t const errorCode = ZWRAP_freeDCtx(zwd);
inikep1c9521f2016-06-13 12:00:46 +0200609 strm->state = NULL;
inikep3eabe9b2016-05-12 17:15:41 +0200610 if (ZSTD_isError(errorCode)) return Z_MEM_ERROR;
611 }
612 return ret;
613}
614
615
616ZEXTERN int ZEXPORT z_inflateSync OF((z_streamp strm))
617{
inikep18f66452016-09-20 12:50:59 +0200618 if (!strm->reserved) {
619 return inflateSync(strm);
620 }
inikep61016872016-09-19 14:27:29 +0200621
inikep3eabe9b2016-05-12 17:15:41 +0200622 return z_inflate(strm, Z_INFLATE_SYNC);
623}
624
625
626
627
628/* Advanced compression functions */
629ZEXTERN int ZEXPORT z_deflateCopy OF((z_streamp dest,
630 z_streamp source))
631{
632 if (!g_useZSTD)
633 return deflateCopy(dest, source);
inikep4af2c9d2016-06-03 17:39:31 +0200634 FINISH_WITH_ERR(source, "deflateCopy is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200635}
636
637
inikep3eabe9b2016-05-12 17:15:41 +0200638ZEXTERN int ZEXPORT z_deflateTune OF((z_streamp strm,
639 int good_length,
640 int max_lazy,
641 int nice_length,
642 int max_chain))
643{
644 if (!g_useZSTD)
645 return deflateTune(strm, good_length, max_lazy, nice_length, max_chain);
inikep4af2c9d2016-06-03 17:39:31 +0200646 FINISH_WITH_ERR(strm, "deflateTune is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200647}
648
649
inikepbf25d7a2016-06-02 10:19:35 +0200650#if ZLIB_VERNUM >= 0x1260
inikep3eabe9b2016-05-12 17:15:41 +0200651ZEXTERN int ZEXPORT z_deflatePending OF((z_streamp strm,
652 unsigned *pending,
653 int *bits))
654{
655 if (!g_useZSTD)
656 return deflatePending(strm, pending, bits);
inikep4af2c9d2016-06-03 17:39:31 +0200657 FINISH_WITH_ERR(strm, "deflatePending is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200658}
inikepbf25d7a2016-06-02 10:19:35 +0200659#endif
inikep3eabe9b2016-05-12 17:15:41 +0200660
661
662ZEXTERN int ZEXPORT z_deflatePrime OF((z_streamp strm,
663 int bits,
664 int value))
665{
666 if (!g_useZSTD)
667 return deflatePrime(strm, bits, value);
inikep4af2c9d2016-06-03 17:39:31 +0200668 FINISH_WITH_ERR(strm, "deflatePrime is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200669}
670
671
672ZEXTERN int ZEXPORT z_deflateSetHeader OF((z_streamp strm,
673 gz_headerp head))
674{
675 if (!g_useZSTD)
676 return deflateSetHeader(strm, head);
inikep4af2c9d2016-06-03 17:39:31 +0200677 FINISH_WITH_ERR(strm, "deflateSetHeader is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200678}
679
680
681
682
inikep61016872016-09-19 14:27:29 +0200683/* Advanced decompression functions */
inikepbf25d7a2016-06-02 10:19:35 +0200684#if ZLIB_VERNUM >= 0x1280
inikep3eabe9b2016-05-12 17:15:41 +0200685ZEXTERN int ZEXPORT z_inflateGetDictionary OF((z_streamp strm,
686 Bytef *dictionary,
687 uInt *dictLength))
688{
689 if (!strm->reserved)
690 return inflateGetDictionary(strm, dictionary, dictLength);
inikep4af2c9d2016-06-03 17:39:31 +0200691 FINISH_WITH_ERR(strm, "inflateGetDictionary is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200692}
inikepbf25d7a2016-06-02 10:19:35 +0200693#endif
inikep3eabe9b2016-05-12 17:15:41 +0200694
695
696ZEXTERN int ZEXPORT z_inflateCopy OF((z_streamp dest,
697 z_streamp source))
698{
699 if (!g_useZSTD)
700 return inflateCopy(dest, source);
inikep4af2c9d2016-06-03 17:39:31 +0200701 FINISH_WITH_ERR(source, "inflateCopy is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200702}
703
704
inikepbf25d7a2016-06-02 10:19:35 +0200705#if ZLIB_VERNUM >= 0x1240
inikep3eabe9b2016-05-12 17:15:41 +0200706ZEXTERN int ZEXPORT z_inflateReset2 OF((z_streamp strm,
707 int windowBits))
708{
709 if (!strm->reserved)
710 return inflateReset2(strm, windowBits);
inikep4af2c9d2016-06-03 17:39:31 +0200711 FINISH_WITH_ERR(strm, "inflateReset2 is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200712}
inikepbf25d7a2016-06-02 10:19:35 +0200713#endif
714
715
716#if ZLIB_VERNUM >= 0x1240
717ZEXTERN long ZEXPORT z_inflateMark OF((z_streamp strm))
718{
719 if (!strm->reserved)
720 return inflateMark(strm);
inikep4af2c9d2016-06-03 17:39:31 +0200721 FINISH_WITH_ERR(strm, "inflateMark is not supported!");
inikepbf25d7a2016-06-02 10:19:35 +0200722}
723#endif
inikep3eabe9b2016-05-12 17:15:41 +0200724
725
726ZEXTERN int ZEXPORT z_inflatePrime OF((z_streamp strm,
727 int bits,
728 int value))
729{
730 if (!strm->reserved)
731 return inflatePrime(strm, bits, value);
inikep4af2c9d2016-06-03 17:39:31 +0200732 FINISH_WITH_ERR(strm, "inflatePrime is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200733}
734
735
inikep3eabe9b2016-05-12 17:15:41 +0200736ZEXTERN int ZEXPORT z_inflateGetHeader OF((z_streamp strm,
737 gz_headerp head))
738{
739 if (!strm->reserved)
740 return inflateGetHeader(strm, head);
inikep4af2c9d2016-06-03 17:39:31 +0200741 FINISH_WITH_ERR(strm, "inflateGetHeader is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200742}
743
744
745ZEXTERN int ZEXPORT z_inflateBackInit_ OF((z_streamp strm, int windowBits,
746 unsigned char FAR *window,
747 const char *version,
748 int stream_size))
749{
750 if (!strm->reserved)
751 return inflateBackInit_(strm, windowBits, window, version, stream_size);
inikep4af2c9d2016-06-03 17:39:31 +0200752 FINISH_WITH_ERR(strm, "inflateBackInit is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200753}
754
755
756ZEXTERN int ZEXPORT z_inflateBack OF((z_streamp strm,
757 in_func in, void FAR *in_desc,
758 out_func out, void FAR *out_desc))
759{
760 if (!strm->reserved)
761 return inflateBack(strm, in, in_desc, out, out_desc);
inikep4af2c9d2016-06-03 17:39:31 +0200762 FINISH_WITH_ERR(strm, "inflateBack is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200763}
764
765
766ZEXTERN int ZEXPORT z_inflateBackEnd OF((z_streamp strm))
767{
768 if (!strm->reserved)
769 return inflateBackEnd(strm);
inikep4af2c9d2016-06-03 17:39:31 +0200770 FINISH_WITH_ERR(strm, "inflateBackEnd is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200771}
772
773
774ZEXTERN uLong ZEXPORT z_zlibCompileFlags OF((void)) { return zlibCompileFlags(); };
775
776
777
778 /* utility functions */
779#ifndef Z_SOLO
780
781ZEXTERN int ZEXPORT z_compress OF((Bytef *dest, uLongf *destLen,
782 const Bytef *source, uLong sourceLen))
783{
784 if (!g_useZSTD)
785 return compress(dest, destLen, source, sourceLen);
786
Yann Collet4ded9e52016-08-30 10:04:33 -0700787 { size_t dstCapacity = *destLen;
inikepde2c92f2016-06-03 19:44:03 +0200788 size_t const errorCode = ZSTD_compress(dest, dstCapacity, source, sourceLen, ZWRAP_DEFAULT_CLEVEL);
inikep554b3b92016-09-20 15:18:00 +0200789 LOG_WRAPPERD("z_compress sourceLen=%d dstCapacity=%d\n", (int)sourceLen, (int)dstCapacity);
inikep18f66452016-09-20 12:50:59 +0200790 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +0200791 *destLen = errorCode;
792 }
793 return Z_OK;
794}
795
796
797ZEXTERN int ZEXPORT z_compress2 OF((Bytef *dest, uLongf *destLen,
798 const Bytef *source, uLong sourceLen,
799 int level))
800{
801 if (!g_useZSTD)
802 return compress2(dest, destLen, source, sourceLen, level);
Yann Collet4ded9e52016-08-30 10:04:33 -0700803
804 { size_t dstCapacity = *destLen;
inikepff2d1892016-06-02 22:15:09 +0200805 size_t const errorCode = ZSTD_compress(dest, dstCapacity, source, sourceLen, level);
inikep18f66452016-09-20 12:50:59 +0200806 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +0200807 *destLen = errorCode;
808 }
809 return Z_OK;
810}
811
812
813ZEXTERN uLong ZEXPORT z_compressBound OF((uLong sourceLen))
814{
815 if (!g_useZSTD)
816 return compressBound(sourceLen);
817
818 return ZSTD_compressBound(sourceLen);
819}
820
821
822ZEXTERN int ZEXPORT z_uncompress OF((Bytef *dest, uLongf *destLen,
823 const Bytef *source, uLong sourceLen))
824{
825 if (sourceLen < 4 || MEM_readLE32(source) != ZSTD_MAGICNUMBER)
inikep3eabe9b2016-05-12 17:15:41 +0200826 return uncompress(dest, destLen, source, sourceLen);
827
Yann Collet4ded9e52016-08-30 10:04:33 -0700828 { size_t dstCapacity = *destLen;
inikepff2d1892016-06-02 22:15:09 +0200829 size_t const errorCode = ZSTD_decompress(dest, dstCapacity, source, sourceLen);
inikep18f66452016-09-20 12:50:59 +0200830 if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
inikep3eabe9b2016-05-12 17:15:41 +0200831 *destLen = errorCode;
832 }
833 return Z_OK;
834}
835
836
837
838 /* gzip file access functions */
839ZEXTERN gzFile ZEXPORT z_gzopen OF((const char *path, const char *mode))
840{
841 if (!g_useZSTD)
842 return gzopen(path, mode);
843 FINISH_WITH_NULL_ERR("gzopen is not supported!");
844}
845
846
847ZEXTERN gzFile ZEXPORT z_gzdopen OF((int fd, const char *mode))
848{
849 if (!g_useZSTD)
850 return gzdopen(fd, mode);
851 FINISH_WITH_NULL_ERR("gzdopen is not supported!");
852}
853
854
inikepbf25d7a2016-06-02 10:19:35 +0200855#if ZLIB_VERNUM >= 0x1240
inikep3eabe9b2016-05-12 17:15:41 +0200856ZEXTERN int ZEXPORT z_gzbuffer OF((gzFile file, unsigned size))
857{
858 if (!g_useZSTD)
859 return gzbuffer(file, size);
inikep4af2c9d2016-06-03 17:39:31 +0200860 FINISH_WITH_GZ_ERR("gzbuffer is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200861}
862
863
inikepbf25d7a2016-06-02 10:19:35 +0200864ZEXTERN z_off_t ZEXPORT z_gzoffset OF((gzFile file))
865{
866 if (!g_useZSTD)
867 return gzoffset(file);
inikep4af2c9d2016-06-03 17:39:31 +0200868 FINISH_WITH_GZ_ERR("gzoffset is not supported!");
inikepbf25d7a2016-06-02 10:19:35 +0200869}
870
871
872ZEXTERN int ZEXPORT z_gzclose_r OF((gzFile file))
873{
874 if (!g_useZSTD)
875 return gzclose_r(file);
inikep4af2c9d2016-06-03 17:39:31 +0200876 FINISH_WITH_GZ_ERR("gzclose_r is not supported!");
inikepbf25d7a2016-06-02 10:19:35 +0200877}
878
879
880ZEXTERN int ZEXPORT z_gzclose_w OF((gzFile file))
881{
882 if (!g_useZSTD)
883 return gzclose_w(file);
inikep4af2c9d2016-06-03 17:39:31 +0200884 FINISH_WITH_GZ_ERR("gzclose_w is not supported!");
inikepbf25d7a2016-06-02 10:19:35 +0200885}
886#endif
887
888
inikep3eabe9b2016-05-12 17:15:41 +0200889ZEXTERN int ZEXPORT z_gzsetparams OF((gzFile file, int level, int strategy))
890{
891 if (!g_useZSTD)
892 return gzsetparams(file, level, strategy);
inikep4af2c9d2016-06-03 17:39:31 +0200893 FINISH_WITH_GZ_ERR("gzsetparams is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200894}
895
896
897ZEXTERN int ZEXPORT z_gzread OF((gzFile file, voidp buf, unsigned len))
898{
899 if (!g_useZSTD)
900 return gzread(file, buf, len);
inikep4af2c9d2016-06-03 17:39:31 +0200901 FINISH_WITH_GZ_ERR("gzread is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200902}
903
904
905ZEXTERN int ZEXPORT z_gzwrite OF((gzFile file,
906 voidpc buf, unsigned len))
907{
908 if (!g_useZSTD)
909 return gzwrite(file, buf, len);
inikep4af2c9d2016-06-03 17:39:31 +0200910 FINISH_WITH_GZ_ERR("gzwrite is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200911}
912
913
inikepbf25d7a2016-06-02 10:19:35 +0200914#if ZLIB_VERNUM >= 0x1260
inikep3eabe9b2016-05-12 17:15:41 +0200915ZEXTERN int ZEXPORTVA z_gzprintf Z_ARG((gzFile file, const char *format, ...))
inikepbf25d7a2016-06-02 10:19:35 +0200916#else
917ZEXTERN int ZEXPORTVA z_gzprintf OF((gzFile file, const char *format, ...))
918#endif
inikep3eabe9b2016-05-12 17:15:41 +0200919{
920 if (!g_useZSTD) {
921 int ret;
922 char buf[1024];
923 va_list args;
924 va_start (args, format);
925 ret = vsprintf (buf, format, args);
926 va_end (args);
927
928 ret = gzprintf(file, buf);
inikep3eabe9b2016-05-12 17:15:41 +0200929 return ret;
930 }
inikep4af2c9d2016-06-03 17:39:31 +0200931 FINISH_WITH_GZ_ERR("gzprintf is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200932}
933
934
935ZEXTERN int ZEXPORT z_gzputs OF((gzFile file, const char *s))
936{
937 if (!g_useZSTD)
938 return gzputs(file, s);
inikep4af2c9d2016-06-03 17:39:31 +0200939 FINISH_WITH_GZ_ERR("gzputs is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200940}
941
942
943ZEXTERN char * ZEXPORT z_gzgets OF((gzFile file, char *buf, int len))
944{
945 if (!g_useZSTD)
946 return gzgets(file, buf, len);
947 FINISH_WITH_NULL_ERR("gzgets is not supported!");
948}
949
950
951ZEXTERN int ZEXPORT z_gzputc OF((gzFile file, int c))
952{
953 if (!g_useZSTD)
954 return gzputc(file, c);
inikep4af2c9d2016-06-03 17:39:31 +0200955 FINISH_WITH_GZ_ERR("gzputc is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200956}
957
958
inikepbf25d7a2016-06-02 10:19:35 +0200959#if ZLIB_VERNUM == 0x1260
960ZEXTERN int ZEXPORT z_gzgetc_ OF((gzFile file))
961#else
inikep3eabe9b2016-05-12 17:15:41 +0200962ZEXTERN int ZEXPORT z_gzgetc OF((gzFile file))
inikepbf25d7a2016-06-02 10:19:35 +0200963#endif
inikep3eabe9b2016-05-12 17:15:41 +0200964{
965 if (!g_useZSTD)
966 return gzgetc(file);
inikep4af2c9d2016-06-03 17:39:31 +0200967 FINISH_WITH_GZ_ERR("gzgetc is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200968}
969
970
971ZEXTERN int ZEXPORT z_gzungetc OF((int c, gzFile file))
972{
973 if (!g_useZSTD)
974 return gzungetc(c, file);
inikep4af2c9d2016-06-03 17:39:31 +0200975 FINISH_WITH_GZ_ERR("gzungetc is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200976}
977
978
979ZEXTERN int ZEXPORT z_gzflush OF((gzFile file, int flush))
980{
981 if (!g_useZSTD)
982 return gzflush(file, flush);
inikep4af2c9d2016-06-03 17:39:31 +0200983 FINISH_WITH_GZ_ERR("gzflush is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200984}
985
986
987ZEXTERN z_off_t ZEXPORT z_gzseek OF((gzFile file, z_off_t offset, int whence))
988{
989 if (!g_useZSTD)
990 return gzseek(file, offset, whence);
inikep4af2c9d2016-06-03 17:39:31 +0200991 FINISH_WITH_GZ_ERR("gzseek is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +0200992}
993
994
995ZEXTERN int ZEXPORT z_gzrewind OF((gzFile file))
996{
997 if (!g_useZSTD)
998 return gzrewind(file);
inikep4af2c9d2016-06-03 17:39:31 +0200999 FINISH_WITH_GZ_ERR("gzrewind is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001000}
1001
1002
1003ZEXTERN z_off_t ZEXPORT z_gztell OF((gzFile file))
1004{
1005 if (!g_useZSTD)
1006 return gztell(file);
inikep4af2c9d2016-06-03 17:39:31 +02001007 FINISH_WITH_GZ_ERR("gztell is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001008}
1009
1010
inikep3eabe9b2016-05-12 17:15:41 +02001011ZEXTERN int ZEXPORT z_gzeof OF((gzFile file))
1012{
1013 if (!g_useZSTD)
1014 return gzeof(file);
inikep4af2c9d2016-06-03 17:39:31 +02001015 FINISH_WITH_GZ_ERR("gzeof is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001016}
1017
1018
1019ZEXTERN int ZEXPORT z_gzdirect OF((gzFile file))
1020{
1021 if (!g_useZSTD)
1022 return gzdirect(file);
inikep4af2c9d2016-06-03 17:39:31 +02001023 FINISH_WITH_GZ_ERR("gzdirect is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001024}
1025
1026
1027ZEXTERN int ZEXPORT z_gzclose OF((gzFile file))
1028{
1029 if (!g_useZSTD)
1030 return gzclose(file);
inikep4af2c9d2016-06-03 17:39:31 +02001031 FINISH_WITH_GZ_ERR("gzclose is not supported!");
inikep3eabe9b2016-05-12 17:15:41 +02001032}
1033
1034
inikep3eabe9b2016-05-12 17:15:41 +02001035ZEXTERN const char * ZEXPORT z_gzerror OF((gzFile file, int *errnum))
1036{
1037 if (!g_useZSTD)
1038 return gzerror(file, errnum);
1039 FINISH_WITH_NULL_ERR("gzerror is not supported!");
1040}
1041
1042
1043ZEXTERN void ZEXPORT z_gzclearerr OF((gzFile file))
1044{
1045 if (!g_useZSTD)
1046 gzclearerr(file);
1047}
1048
1049
1050#endif /* !Z_SOLO */
1051
1052
1053 /* checksum functions */
1054
1055ZEXTERN uLong ZEXPORT z_adler32 OF((uLong adler, const Bytef *buf, uInt len))
1056{
1057 return adler32(adler, buf, len);
1058}
1059
1060ZEXTERN uLong ZEXPORT z_crc32 OF((uLong crc, const Bytef *buf, uInt len))
1061{
1062 return crc32(crc, buf, len);
1063}