• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016-2020, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under both the BSD-style license (found in the
6  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7  * in the COPYING file in the root directory of this source tree).
8  * You may select, at your option, one of the above-listed licenses.
9  */
10 
11 
12 /* ===   Tuning parameters   === */
13 #ifndef ZWRAP_USE_ZSTD
14     #define ZWRAP_USE_ZSTD 0
15 #endif
16 
17 
18 /* ===   Dependencies   === */
19 #include <stdlib.h>
20 #include <stdio.h>                 /* vsprintf */
21 #include <stdarg.h>                /* va_list, for z_gzprintf */
22 #include <string.h>
23 #define NO_DUMMY_DECL
24 #define ZLIB_CONST
25 #include <zlib.h>                  /* without #define Z_PREFIX */
26 #include "zstd_zlibwrapper.h"
27 #define ZSTD_STATIC_LINKING_ONLY   /* ZSTD_isFrame, ZSTD_MAGICNUMBER, ZSTD_customMem */
28 #include "zstd.h"
29 
30 
31 /* ===   Constants   === */
32 #define Z_INFLATE_SYNC              8
33 #define ZLIB_HEADERSIZE             4
34 #define ZSTD_HEADERSIZE             ZSTD_FRAMEHEADERSIZE_MIN(ZSTD_f_zstd1)
35 #define ZWRAP_DEFAULT_CLEVEL        3   /* Z_DEFAULT_COMPRESSION is translated to ZWRAP_DEFAULT_CLEVEL for zstd */
36 
37 
38 /* ===   Debug   === */
39 #define LOG_WRAPPERC(...)  /* fprintf(stderr, __VA_ARGS__) */
40 #define LOG_WRAPPERD(...)  /* fprintf(stderr, __VA_ARGS__) */
41 
42 #define FINISH_WITH_GZ_ERR(msg) { (void)msg; return Z_STREAM_ERROR; }
43 #define FINISH_WITH_NULL_ERR(msg) { (void)msg; return NULL; }
44 
45 /* ===   Utility   === */
46 
47 #define MIN(x,y) ((x) < (y) ? (x) : (y))
48 
ZWRAP_isLittleEndian(void)49 static unsigned ZWRAP_isLittleEndian(void)
50 {
51     const union { unsigned u; char c[4]; } one = { 1 };   /* don't use static : performance detrimental  */
52     return one.c[0];
53 }
54 
55 #ifndef __has_builtin
56 # define __has_builtin(x) 0
57 #endif
58 
ZWRAP_swap32(unsigned in)59 static unsigned ZWRAP_swap32(unsigned in)
60 {
61 #if defined(_MSC_VER)     /* Visual Studio */
62     return _byteswap_ulong(in);
63 #elif (defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) \
64   || (defined(__clang__) && __has_builtin(__builtin_bswap32))
65     return __builtin_bswap32(in);
66 #else
67     return  ((in << 24) & 0xff000000 ) |
68             ((in <<  8) & 0x00ff0000 ) |
69             ((in >>  8) & 0x0000ff00 ) |
70             ((in >> 24) & 0x000000ff );
71 #endif
72 }
73 
ZWRAP_readLE32(const void * ptr)74 static unsigned ZWRAP_readLE32(const void* ptr)
75 {
76     unsigned value;
77     memcpy(&value, ptr, sizeof(value));
78     if (ZWRAP_isLittleEndian())
79         return value;
80     else
81         return ZWRAP_swap32(value);
82 }
83 
84 
85 /* ===   Wrapper   === */
86 static int g_ZWRAP_useZSTDcompression = ZWRAP_USE_ZSTD; /* 0 = don't use ZSTD */
87 
ZWRAP_useZSTDcompression(int turn_on)88 void ZWRAP_useZSTDcompression(int turn_on) { g_ZWRAP_useZSTDcompression = turn_on; }
89 
ZWRAP_isUsingZSTDcompression(void)90 int ZWRAP_isUsingZSTDcompression(void) { return g_ZWRAP_useZSTDcompression; }
91 
92 
93 
94 static ZWRAP_decompress_type g_ZWRAPdecompressionType = ZWRAP_AUTO;
95 
ZWRAP_setDecompressionType(ZWRAP_decompress_type type)96 void ZWRAP_setDecompressionType(ZWRAP_decompress_type type) { g_ZWRAPdecompressionType = type; }
97 
ZWRAP_getDecompressionType(void)98 ZWRAP_decompress_type ZWRAP_getDecompressionType(void) { return g_ZWRAPdecompressionType; }
99 
100 
101 
zstdVersion(void)102 const char * zstdVersion(void) { return ZSTD_VERSION_STRING; }
103 
z_zlibVersion(void)104 ZEXTERN const char * ZEXPORT z_zlibVersion OF((void)) { return zlibVersion();  }
105 
ZWRAP_allocFunction(void * opaque,size_t size)106 static void* ZWRAP_allocFunction(void* opaque, size_t size)
107 {
108     z_streamp strm = (z_streamp) opaque;
109     void* address = strm->zalloc(strm->opaque, 1, (uInt)size);
110     /* LOG_WRAPPERC("ZWRAP alloc %p, %d \n", address, (int)size); */
111     return address;
112 }
113 
ZWRAP_freeFunction(void * opaque,void * address)114 static void ZWRAP_freeFunction(void* opaque, void* address)
115 {
116     z_streamp strm = (z_streamp) opaque;
117     strm->zfree(strm->opaque, address);
118    /* if (address) LOG_WRAPPERC("ZWRAP free %p \n", address); */
119 }
120 
ZWRAP_customMalloc(size_t size,ZSTD_customMem customMem)121 static void* ZWRAP_customMalloc(size_t size, ZSTD_customMem customMem)
122 {
123     if (customMem.customAlloc)
124         return customMem.customAlloc(customMem.opaque, size);
125     return malloc(size);
126 }
127 
ZWRAP_customCalloc(size_t size,ZSTD_customMem customMem)128 static void* ZWRAP_customCalloc(size_t size, ZSTD_customMem customMem)
129 {
130     if (customMem.customAlloc) {
131         /* calloc implemented as malloc+memset;
132          * not as efficient as calloc, but next best guess for custom malloc */
133         void* const ptr = customMem.customAlloc(customMem.opaque, size);
134         memset(ptr, 0, size);
135         return ptr;
136     }
137     return calloc(1, size);
138 }
139 
ZWRAP_customFree(void * ptr,ZSTD_customMem customMem)140 static void ZWRAP_customFree(void* ptr, ZSTD_customMem customMem)
141 {
142     if (ptr!=NULL) {
143         if (customMem.customFree)
144             customMem.customFree(customMem.opaque, ptr);
145         else
146             free(ptr);
147     }
148 }
149 
150 
151 
152 /* ===   Compression   === */
153 typedef enum { ZWRAP_useInit, ZWRAP_useReset, ZWRAP_streamEnd } ZWRAP_state_t;
154 
155 typedef struct {
156     ZSTD_CStream* zbc;
157     int compressionLevel;
158     int streamEnd; /* a flag to signal the end of a stream */
159     unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */
160     ZSTD_customMem customMem;
161     z_stream allocFunc; /* copy of zalloc, zfree, opaque */
162     ZSTD_inBuffer inBuffer;
163     ZSTD_outBuffer outBuffer;
164     ZWRAP_state_t comprState;
165     unsigned long long pledgedSrcSize;
166 } ZWRAP_CCtx;
167 
168 /* typedef ZWRAP_CCtx internal_state; */
169 
170 
171 
ZWRAP_freeCCtx(ZWRAP_CCtx * zwc)172 static size_t ZWRAP_freeCCtx(ZWRAP_CCtx* zwc)
173 {
174     if (zwc==NULL) return 0;   /* support free on NULL */
175     ZSTD_freeCStream(zwc->zbc);
176     ZWRAP_customFree(zwc, zwc->customMem);
177     return 0;
178 }
179 
180 
ZWRAP_createCCtx(z_streamp strm)181 static ZWRAP_CCtx* ZWRAP_createCCtx(z_streamp strm)
182 {
183     ZWRAP_CCtx* zwc;
184     ZSTD_customMem customMem = { NULL, NULL, NULL };
185 
186     if (strm->zalloc && strm->zfree) {
187         customMem.customAlloc = ZWRAP_allocFunction;
188         customMem.customFree = ZWRAP_freeFunction;
189     }
190     customMem.opaque = strm;
191 
192     zwc = (ZWRAP_CCtx*)ZWRAP_customCalloc(sizeof(ZWRAP_CCtx), customMem);
193     if (zwc == NULL) return NULL;
194     zwc->allocFunc = *strm;
195     customMem.opaque = &zwc->allocFunc;
196     zwc->customMem = customMem;
197 
198     return zwc;
199 }
200 
201 
ZWRAP_initializeCStream(ZWRAP_CCtx * zwc,const void * dict,size_t dictSize,unsigned long long pledgedSrcSize)202 static int ZWRAP_initializeCStream(ZWRAP_CCtx* zwc, const void* dict, size_t dictSize, unsigned long long pledgedSrcSize)
203 {
204     LOG_WRAPPERC("- ZWRAP_initializeCStream=%p\n", zwc);
205     if (zwc == NULL || zwc->zbc == NULL) return Z_STREAM_ERROR;
206 
207     if (!pledgedSrcSize) pledgedSrcSize = zwc->pledgedSrcSize;
208     {   ZSTD_parameters const params = ZSTD_getParams(zwc->compressionLevel, pledgedSrcSize, dictSize);
209         size_t initErr;
210         LOG_WRAPPERC("pledgedSrcSize=%d windowLog=%d chainLog=%d hashLog=%d searchLog=%d minMatch=%d strategy=%d\n",
211                     (int)pledgedSrcSize, params.cParams.windowLog, params.cParams.chainLog, params.cParams.hashLog, params.cParams.searchLog, params.cParams.minMatch, params.cParams.strategy);
212         initErr = ZSTD_initCStream_advanced(zwc->zbc, dict, dictSize, params, pledgedSrcSize);
213         if (ZSTD_isError(initErr)) return Z_STREAM_ERROR;
214     }
215 
216     return Z_OK;
217 }
218 
219 
ZWRAPC_finishWithError(ZWRAP_CCtx * zwc,z_streamp strm,int error)220 static int ZWRAPC_finishWithError(ZWRAP_CCtx* zwc, z_streamp strm, int error)
221 {
222     LOG_WRAPPERC("- ZWRAPC_finishWithError=%d\n", error);
223     if (zwc) ZWRAP_freeCCtx(zwc);
224     if (strm) strm->state = NULL;
225     return (error) ? error : Z_STREAM_ERROR;
226 }
227 
228 
ZWRAPC_finishWithErrorMsg(z_streamp strm,char * message)229 static int ZWRAPC_finishWithErrorMsg(z_streamp strm, char* message)
230 {
231     ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
232     strm->msg = message;
233     if (zwc == NULL) return Z_STREAM_ERROR;
234 
235     return ZWRAPC_finishWithError(zwc, strm, 0);
236 }
237 
238 
ZWRAP_setPledgedSrcSize(z_streamp strm,unsigned long long pledgedSrcSize)239 int ZWRAP_setPledgedSrcSize(z_streamp strm, unsigned long long pledgedSrcSize)
240 {
241     ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
242     if (zwc == NULL) return Z_STREAM_ERROR;
243 
244     zwc->pledgedSrcSize = pledgedSrcSize;
245     zwc->comprState = ZWRAP_useInit;
246     return Z_OK;
247 }
248 
convert_into_sis(void * ptr)249 static struct internal_state* convert_into_sis(void* ptr)
250 {
251     return (struct internal_state*) ptr;
252 }
253 
z_deflateInit_(z_streamp strm,int level,const char * version,int stream_size)254 ZEXTERN int ZEXPORT z_deflateInit_ OF((z_streamp strm, int level,
255                                      const char *version, int stream_size))
256 {
257     ZWRAP_CCtx* zwc;
258 
259     LOG_WRAPPERC("- deflateInit level=%d\n", level);
260     if (!g_ZWRAP_useZSTDcompression) {
261         return deflateInit_((strm), (level), version, stream_size);
262     }
263 
264     zwc = ZWRAP_createCCtx(strm);
265     if (zwc == NULL) return Z_MEM_ERROR;
266 
267     if (level == Z_DEFAULT_COMPRESSION)
268         level = ZWRAP_DEFAULT_CLEVEL;
269 
270     zwc->streamEnd = 0;
271     zwc->totalInBytes = 0;
272     zwc->compressionLevel = level;
273     strm->state = convert_into_sis(zwc); /* use state which in not used by user */
274     strm->total_in = 0;
275     strm->total_out = 0;
276     strm->adler = 0;
277     return Z_OK;
278 }
279 
280 
z_deflateInit2_(z_streamp strm,int level,int method,int windowBits,int memLevel,int strategy,const char * version,int stream_size)281 ZEXTERN int ZEXPORT z_deflateInit2_ OF((z_streamp strm, int level, int method,
282                                       int windowBits, int memLevel,
283                                       int strategy, const char *version,
284                                       int stream_size))
285 {
286     if (!g_ZWRAP_useZSTDcompression)
287         return deflateInit2_(strm, level, method, windowBits, memLevel, strategy, version, stream_size);
288 
289     return z_deflateInit_ (strm, level, version, stream_size);
290 }
291 
292 
ZWRAP_deflateReset_keepDict(z_streamp strm)293 int ZWRAP_deflateReset_keepDict(z_streamp strm)
294 {
295     LOG_WRAPPERC("- ZWRAP_deflateReset_keepDict\n");
296     if (!g_ZWRAP_useZSTDcompression)
297         return deflateReset(strm);
298 
299     { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
300       if (zwc) {
301           zwc->streamEnd = 0;
302           zwc->totalInBytes = 0;
303       }
304     }
305 
306     strm->total_in = 0;
307     strm->total_out = 0;
308     strm->adler = 0;
309     return Z_OK;
310 }
311 
312 
z_deflateReset(z_streamp strm)313 ZEXTERN int ZEXPORT z_deflateReset OF((z_streamp strm))
314 {
315     LOG_WRAPPERC("- deflateReset\n");
316     if (!g_ZWRAP_useZSTDcompression)
317         return deflateReset(strm);
318 
319     ZWRAP_deflateReset_keepDict(strm);
320 
321     { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
322       if (zwc) zwc->comprState = ZWRAP_useInit;
323     }
324     return Z_OK;
325 }
326 
327 
z_deflateSetDictionary(z_streamp strm,const Bytef * dictionary,uInt dictLength)328 ZEXTERN int ZEXPORT z_deflateSetDictionary OF((z_streamp strm,
329                                              const Bytef *dictionary,
330                                              uInt  dictLength))
331 {
332     if (!g_ZWRAP_useZSTDcompression) {
333         LOG_WRAPPERC("- deflateSetDictionary\n");
334         return deflateSetDictionary(strm, dictionary, dictLength);
335     }
336 
337     {   ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
338         LOG_WRAPPERC("- deflateSetDictionary level=%d\n", (int)zwc->compressionLevel);
339         if (!zwc) return Z_STREAM_ERROR;
340         if (zwc->zbc == NULL) {
341             zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
342             if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
343         }
344         { int res = ZWRAP_initializeCStream(zwc, dictionary, dictLength, ZSTD_CONTENTSIZE_UNKNOWN);
345           if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res); }
346         zwc->comprState = ZWRAP_useReset;
347     }
348 
349     return Z_OK;
350 }
351 
352 
z_deflate(z_streamp strm,int flush)353 ZEXTERN int ZEXPORT z_deflate OF((z_streamp strm, int flush))
354 {
355     ZWRAP_CCtx* zwc;
356 
357     if (!g_ZWRAP_useZSTDcompression) {
358         LOG_WRAPPERC("- deflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d\n",
359                     (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);
360         return deflate(strm, flush);
361     }
362 
363     zwc = (ZWRAP_CCtx*) strm->state;
364     if (zwc == NULL) { LOG_WRAPPERC("zwc == NULL\n"); return Z_STREAM_ERROR; }
365 
366     if (zwc->zbc == NULL) {
367         zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
368         if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
369         { int const initErr = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : ZSTD_CONTENTSIZE_UNKNOWN);
370           if (initErr != Z_OK) return ZWRAPC_finishWithError(zwc, strm, initErr); }
371         if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
372     } else {
373         if (zwc->totalInBytes == 0) {
374             if (zwc->comprState == ZWRAP_useReset) {
375                 size_t const resetErr = ZSTD_resetCStream(zwc->zbc, (flush == Z_FINISH) ? strm->avail_in : zwc->pledgedSrcSize);
376                 if (ZSTD_isError(resetErr)) {
377                     LOG_WRAPPERC("ERROR: ZSTD_resetCStream errorCode=%s\n",
378                                 ZSTD_getErrorName(resetErr));
379                     return ZWRAPC_finishWithError(zwc, strm, 0);
380                 }
381             } else {
382                 int const res = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : ZSTD_CONTENTSIZE_UNKNOWN);
383                 if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res);
384                 if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
385             }
386         }  /* (zwc->totalInBytes == 0) */
387     }  /* ! (zwc->zbc == NULL) */
388 
389     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);
390     if (strm->avail_in > 0) {
391         zwc->inBuffer.src = strm->next_in;
392         zwc->inBuffer.size = strm->avail_in;
393         zwc->inBuffer.pos = 0;
394         zwc->outBuffer.dst = strm->next_out;
395         zwc->outBuffer.size = strm->avail_out;
396         zwc->outBuffer.pos = 0;
397         { size_t const cErr = ZSTD_compressStream(zwc->zbc, &zwc->outBuffer, &zwc->inBuffer);
398           LOG_WRAPPERC("deflate ZSTD_compressStream srcSize=%d dstCapacity=%d\n", (int)zwc->inBuffer.size, (int)zwc->outBuffer.size);
399           if (ZSTD_isError(cErr)) return ZWRAPC_finishWithError(zwc, strm, 0);
400         }
401         strm->next_out += zwc->outBuffer.pos;
402         strm->total_out += zwc->outBuffer.pos;
403         strm->avail_out -= zwc->outBuffer.pos;
404         strm->total_in += zwc->inBuffer.pos;
405         zwc->totalInBytes += zwc->inBuffer.pos;
406         strm->next_in += zwc->inBuffer.pos;
407         strm->avail_in -= zwc->inBuffer.pos;
408     }
409 
410     if (flush == Z_FULL_FLUSH
411 #if ZLIB_VERNUM >= 0x1240
412         || flush == Z_TREES
413 #endif
414         || flush == Z_BLOCK)
415         return ZWRAPC_finishWithErrorMsg(strm, "Z_FULL_FLUSH, Z_BLOCK and Z_TREES are not supported!");
416 
417     if (flush == Z_FINISH) {
418         size_t bytesLeft;
419         if (zwc->streamEnd) return Z_STREAM_END;
420         zwc->outBuffer.dst = strm->next_out;
421         zwc->outBuffer.size = strm->avail_out;
422         zwc->outBuffer.pos = 0;
423         bytesLeft = ZSTD_endStream(zwc->zbc, &zwc->outBuffer);
424         LOG_WRAPPERC("deflate ZSTD_endStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
425         if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);
426         strm->next_out += zwc->outBuffer.pos;
427         strm->total_out += zwc->outBuffer.pos;
428         strm->avail_out -= zwc->outBuffer.pos;
429         if (bytesLeft == 0) {
430             zwc->streamEnd = 1;
431             LOG_WRAPPERC("Z_STREAM_END2 strm->total_in=%d strm->avail_out=%d strm->total_out=%d\n",
432                         (int)strm->total_in, (int)strm->avail_out, (int)strm->total_out);
433             return Z_STREAM_END;
434     }   }
435     else
436     if (flush == Z_SYNC_FLUSH || flush == Z_PARTIAL_FLUSH) {
437         size_t bytesLeft;
438         zwc->outBuffer.dst = strm->next_out;
439         zwc->outBuffer.size = strm->avail_out;
440         zwc->outBuffer.pos = 0;
441         bytesLeft = ZSTD_flushStream(zwc->zbc, &zwc->outBuffer);
442         LOG_WRAPPERC("deflate ZSTD_flushStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
443         if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);
444         strm->next_out += zwc->outBuffer.pos;
445         strm->total_out += zwc->outBuffer.pos;
446         strm->avail_out -= zwc->outBuffer.pos;
447     }
448     LOG_WRAPPERC("- deflate3 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d\n", (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);
449     return Z_OK;
450 }
451 
452 
z_deflateEnd(z_streamp strm)453 ZEXTERN int ZEXPORT z_deflateEnd OF((z_streamp strm))
454 {
455     if (!g_ZWRAP_useZSTDcompression) {
456         LOG_WRAPPERC("- deflateEnd\n");
457         return deflateEnd(strm);
458     }
459     LOG_WRAPPERC("- deflateEnd total_in=%d total_out=%d\n", (int)(strm->total_in), (int)(strm->total_out));
460     {   size_t errorCode;
461         ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
462         if (zwc == NULL) return Z_OK;  /* structures are already freed */
463         strm->state = NULL;
464         errorCode = ZWRAP_freeCCtx(zwc);
465         if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
466     }
467     return Z_OK;
468 }
469 
470 
z_deflateBound(z_streamp strm,uLong sourceLen)471 ZEXTERN uLong ZEXPORT z_deflateBound OF((z_streamp strm,
472                                        uLong sourceLen))
473 {
474     if (!g_ZWRAP_useZSTDcompression)
475         return deflateBound(strm, sourceLen);
476 
477     return ZSTD_compressBound(sourceLen);
478 }
479 
480 
z_deflateParams(z_streamp strm,int level,int strategy)481 ZEXTERN int ZEXPORT z_deflateParams OF((z_streamp strm,
482                                       int level,
483                                       int strategy))
484 {
485     if (!g_ZWRAP_useZSTDcompression) {
486         LOG_WRAPPERC("- deflateParams level=%d strategy=%d\n", level, strategy);
487         return deflateParams(strm, level, strategy);
488     }
489 
490     return Z_OK;
491 }
492 
493 
494 
495 
496 
497 /* ===   Decompression   === */
498 
499 typedef enum { ZWRAP_ZLIB_STREAM, ZWRAP_ZSTD_STREAM, ZWRAP_UNKNOWN_STREAM } ZWRAP_stream_type;
500 
501 typedef struct {
502     ZSTD_DStream* zbd;
503     char headerBuf[16];   /* must be >= ZSTD_frameHeaderSize_min */
504     int errorCount;
505     unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */
506     ZWRAP_state_t decompState;
507     ZSTD_inBuffer inBuffer;
508     ZSTD_outBuffer outBuffer;
509 
510     /* zlib params */
511     int stream_size;
512     char *version;
513     int windowBits;
514     ZSTD_customMem customMem;
515     z_stream allocFunc; /* just to copy zalloc, zfree, opaque */
516 } ZWRAP_DCtx;
517 
518 
ZWRAP_initDCtx(ZWRAP_DCtx * zwd)519 static void ZWRAP_initDCtx(ZWRAP_DCtx* zwd)
520 {
521     zwd->errorCount = 0;
522     zwd->outBuffer.pos = 0;
523     zwd->outBuffer.size = 0;
524 }
525 
ZWRAP_createDCtx(z_streamp strm)526 static ZWRAP_DCtx* ZWRAP_createDCtx(z_streamp strm)
527 {
528     ZWRAP_DCtx* zwd;
529     ZSTD_customMem customMem = { NULL, NULL, NULL };
530 
531     if (strm->zalloc && strm->zfree) {
532         customMem.customAlloc = ZWRAP_allocFunction;
533         customMem.customFree = ZWRAP_freeFunction;
534     }
535     customMem.opaque = strm;
536 
537     zwd = (ZWRAP_DCtx*)ZWRAP_customCalloc(sizeof(ZWRAP_DCtx), customMem);
538     if (zwd == NULL) return NULL;
539     zwd->allocFunc = *strm;
540     customMem.opaque = &zwd->allocFunc;
541     zwd->customMem = customMem;
542 
543     ZWRAP_initDCtx(zwd);
544     return zwd;
545 }
546 
ZWRAP_freeDCtx(ZWRAP_DCtx * zwd)547 static size_t ZWRAP_freeDCtx(ZWRAP_DCtx* zwd)
548 {
549     if (zwd==NULL) return 0;   /* support free on null */
550     ZSTD_freeDStream(zwd->zbd);
551     ZWRAP_customFree(zwd->version, zwd->customMem);
552     ZWRAP_customFree(zwd, zwd->customMem);
553     return 0;
554 }
555 
556 
ZWRAP_isUsingZSTDdecompression(z_streamp strm)557 int ZWRAP_isUsingZSTDdecompression(z_streamp strm)
558 {
559     if (strm == NULL) return 0;
560     return (strm->reserved == ZWRAP_ZSTD_STREAM);
561 }
562 
563 
ZWRAPD_finishWithError(ZWRAP_DCtx * zwd,z_streamp strm,int error)564 static int ZWRAPD_finishWithError(ZWRAP_DCtx* zwd, z_streamp strm, int error)
565 {
566     LOG_WRAPPERD("- ZWRAPD_finishWithError=%d\n", error);
567     ZWRAP_freeDCtx(zwd);
568     strm->state = NULL;
569     return (error) ? error : Z_STREAM_ERROR;
570 }
571 
ZWRAPD_finishWithErrorMsg(z_streamp strm,char * message)572 static int ZWRAPD_finishWithErrorMsg(z_streamp strm, char* message)
573 {
574     ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
575     strm->msg = message;
576     if (zwd == NULL) return Z_STREAM_ERROR;
577 
578     return ZWRAPD_finishWithError(zwd, strm, 0);
579 }
580 
581 
z_inflateInit_(z_streamp strm,const char * version,int stream_size)582 ZEXTERN int ZEXPORT z_inflateInit_ OF((z_streamp strm,
583                                      const char* version, int stream_size))
584 {
585     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
586         strm->reserved = ZWRAP_ZLIB_STREAM;
587         return inflateInit(strm);
588     }
589 
590     {   ZWRAP_DCtx* const zwd = ZWRAP_createDCtx(strm);
591         LOG_WRAPPERD("- inflateInit\n");
592         if (zwd == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
593 
594         zwd->version = (char*)ZWRAP_customMalloc(strlen(version)+1, zwd->customMem);
595         if (zwd->version == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
596         strcpy(zwd->version, version);
597 
598         zwd->stream_size = stream_size;
599         zwd->totalInBytes = 0;
600         strm->state = convert_into_sis(zwd);
601         strm->total_in = 0;
602         strm->total_out = 0;
603         strm->reserved = ZWRAP_UNKNOWN_STREAM;
604         strm->adler = 0;
605     }
606 
607     return Z_OK;
608 }
609 
610 
z_inflateInit2_(z_streamp strm,int windowBits,const char * version,int stream_size)611 ZEXTERN int ZEXPORT z_inflateInit2_ OF((z_streamp strm, int  windowBits,
612                                       const char *version, int stream_size))
613 {
614     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
615         return inflateInit2_(strm, windowBits, version, stream_size);
616     }
617 
618     {   int const ret = z_inflateInit_ (strm, version, stream_size);
619         LOG_WRAPPERD("- inflateInit2 windowBits=%d\n", windowBits);
620         if (ret == Z_OK) {
621             ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*)strm->state;
622             if (zwd == NULL) return Z_STREAM_ERROR;
623             zwd->windowBits = windowBits;
624         }
625         return ret;
626     }
627 }
628 
ZWRAP_inflateReset_keepDict(z_streamp strm)629 int ZWRAP_inflateReset_keepDict(z_streamp strm)
630 {
631     LOG_WRAPPERD("- ZWRAP_inflateReset_keepDict\n");
632     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
633         return inflateReset(strm);
634 
635     {   ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
636         if (zwd == NULL) return Z_STREAM_ERROR;
637         ZWRAP_initDCtx(zwd);
638         zwd->decompState = ZWRAP_useReset;
639         zwd->totalInBytes = 0;
640     }
641 
642     strm->total_in = 0;
643     strm->total_out = 0;
644     return Z_OK;
645 }
646 
647 
z_inflateReset(z_streamp strm)648 ZEXTERN int ZEXPORT z_inflateReset OF((z_streamp strm))
649 {
650     LOG_WRAPPERD("- inflateReset\n");
651     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
652         return inflateReset(strm);
653 
654     { int const ret = ZWRAP_inflateReset_keepDict(strm);
655       if (ret != Z_OK) return ret; }
656 
657     { ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
658       if (zwd == NULL) return Z_STREAM_ERROR;
659       zwd->decompState = ZWRAP_useInit; }
660 
661     return Z_OK;
662 }
663 
664 
665 #if ZLIB_VERNUM >= 0x1240
z_inflateReset2(z_streamp strm,int windowBits)666 ZEXTERN int ZEXPORT z_inflateReset2 OF((z_streamp strm,
667                                       int windowBits))
668 {
669     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
670         return inflateReset2(strm, windowBits);
671 
672     {   int const ret = z_inflateReset (strm);
673         if (ret == Z_OK) {
674             ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*)strm->state;
675             if (zwd == NULL) return Z_STREAM_ERROR;
676             zwd->windowBits = windowBits;
677         }
678         return ret;
679     }
680 }
681 #endif
682 
683 
z_inflateSetDictionary(z_streamp strm,const Bytef * dictionary,uInt dictLength)684 ZEXTERN int ZEXPORT z_inflateSetDictionary OF((z_streamp strm,
685                                              const Bytef *dictionary,
686                                              uInt  dictLength))
687 {
688     LOG_WRAPPERD("- inflateSetDictionary\n");
689     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
690         return inflateSetDictionary(strm, dictionary, dictLength);
691 
692     {   ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
693         if (zwd == NULL || zwd->zbd == NULL) return Z_STREAM_ERROR;
694         { size_t const initErr = ZSTD_initDStream_usingDict(zwd->zbd, dictionary, dictLength);
695           if (ZSTD_isError(initErr)) return ZWRAPD_finishWithError(zwd, strm, 0); }
696         zwd->decompState = ZWRAP_useReset;
697 
698         if (zwd->totalInBytes == ZSTD_HEADERSIZE) {
699             zwd->inBuffer.src = zwd->headerBuf;
700             zwd->inBuffer.size = zwd->totalInBytes;
701             zwd->inBuffer.pos = 0;
702             zwd->outBuffer.dst = strm->next_out;
703             zwd->outBuffer.size = 0;
704             zwd->outBuffer.pos = 0;
705             {   size_t const errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
706                 LOG_WRAPPERD("inflateSetDictionary ZSTD_decompressStream errorCode=%d srcSize=%d dstCapacity=%d\n",
707                              (int)errorCode, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);
708                 if (zwd->inBuffer.pos < zwd->outBuffer.size || ZSTD_isError(errorCode)) {
709                     LOG_WRAPPERD("ERROR: ZSTD_decompressStream %s\n",
710                                  ZSTD_getErrorName(errorCode));
711                     return ZWRAPD_finishWithError(zwd, strm, 0);
712     }   }   }   }
713 
714     return Z_OK;
715 }
716 
717 
z_inflate(z_streamp strm,int flush)718 ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush))
719 {
720     ZWRAP_DCtx* zwd;
721 
722     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
723         int const result = inflate(strm, flush);
724         LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
725                      (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, result);
726         return result;
727     }
728 
729     if (strm->avail_in <= 0) return Z_OK;
730 
731     zwd = (ZWRAP_DCtx*) strm->state;
732     LOG_WRAPPERD("- inflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d\n",
733                  (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);
734 
735     if (zwd == NULL) return Z_STREAM_ERROR;
736     if (zwd->decompState == ZWRAP_streamEnd) return Z_STREAM_END;
737 
738     if (zwd->totalInBytes < ZLIB_HEADERSIZE) {
739         if (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) {
740             if (ZWRAP_readLE32(strm->next_in) != ZSTD_MAGICNUMBER) {
741                 {   int const initErr = (zwd->windowBits) ?
742                                 inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size) :
743                                 inflateInit_(strm, zwd->version, zwd->stream_size);
744                     LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", initErr);
745                     if (initErr != Z_OK) return ZWRAPD_finishWithError(zwd, strm, initErr);
746                 }
747 
748                 strm->reserved = ZWRAP_ZLIB_STREAM;
749                 { size_t const freeErr = ZWRAP_freeDCtx(zwd);
750                   if (ZSTD_isError(freeErr)) goto error; }
751 
752                 {   int const result = (flush == Z_INFLATE_SYNC) ?
753                                         inflateSync(strm) :
754                                         inflate(strm, flush);
755                     LOG_WRAPPERD("- inflate3 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
756                                  (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, res);
757                     return result;
758             }   }
759         } else {  /* ! (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) */
760             size_t const srcSize = MIN(strm->avail_in, ZLIB_HEADERSIZE - zwd->totalInBytes);
761             memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);
762             strm->total_in += srcSize;
763             zwd->totalInBytes += srcSize;
764             strm->next_in += srcSize;
765             strm->avail_in -= srcSize;
766             if (zwd->totalInBytes < ZLIB_HEADERSIZE) return Z_OK;
767 
768             if (ZWRAP_readLE32(zwd->headerBuf) != ZSTD_MAGICNUMBER) {
769                 z_stream strm2;
770                 strm2.next_in = strm->next_in;
771                 strm2.avail_in = strm->avail_in;
772                 strm2.next_out = strm->next_out;
773                 strm2.avail_out = strm->avail_out;
774 
775                 {   int const initErr = (zwd->windowBits) ?
776                                 inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size) :
777                                 inflateInit_(strm, zwd->version, zwd->stream_size);
778                     LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", initErr);
779                     if (initErr != Z_OK) return ZWRAPD_finishWithError(zwd, strm, initErr);
780                 }
781 
782                 /* inflate header */
783                 strm->next_in = (unsigned char*)zwd->headerBuf;
784                 strm->avail_in = ZLIB_HEADERSIZE;
785                 strm->avail_out = 0;
786                 {   int const dErr = inflate(strm, Z_NO_FLUSH);
787                     LOG_WRAPPERD("ZLIB inflate errorCode=%d strm->avail_in=%d\n",
788                                   dErr, (int)strm->avail_in);
789                     if (dErr != Z_OK)
790                         return ZWRAPD_finishWithError(zwd, strm, dErr);
791                 }
792                 if (strm->avail_in > 0) goto error;
793 
794                 strm->next_in = strm2.next_in;
795                 strm->avail_in = strm2.avail_in;
796                 strm->next_out = strm2.next_out;
797                 strm->avail_out = strm2.avail_out;
798 
799                 strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */
800                 { size_t const freeErr = ZWRAP_freeDCtx(zwd);
801                   if (ZSTD_isError(freeErr)) goto error; }
802 
803                 {   int const result = (flush == Z_INFLATE_SYNC) ?
804                                        inflateSync(strm) :
805                                        inflate(strm, flush);
806                     LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
807                                  (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, res);
808                     return result;
809         }   }   }  /* if ! (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) */
810     }  /* (zwd->totalInBytes < ZLIB_HEADERSIZE) */
811 
812     strm->reserved = ZWRAP_ZSTD_STREAM; /* mark as zstd steam */
813 
814     if (flush == Z_INFLATE_SYNC) { strm->msg = "inflateSync is not supported!"; goto error; }
815 
816     if (!zwd->zbd) {
817         zwd->zbd = ZSTD_createDStream_advanced(zwd->customMem);
818         if (zwd->zbd == NULL) { LOG_WRAPPERD("ERROR: ZSTD_createDStream_advanced\n"); goto error; }
819         zwd->decompState = ZWRAP_useInit;
820     }
821 
822     if (zwd->totalInBytes < ZSTD_HEADERSIZE) {
823         if (zwd->totalInBytes == 0 && strm->avail_in >= ZSTD_HEADERSIZE) {
824             if (zwd->decompState == ZWRAP_useInit) {
825                 size_t const initErr = ZSTD_initDStream(zwd->zbd);
826                 if (ZSTD_isError(initErr)) {
827                     LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n",
828                                  ZSTD_getErrorName(initErr));
829                     goto error;
830                 }
831             } else {
832                 size_t const resetErr = ZSTD_resetDStream(zwd->zbd);
833                 if (ZSTD_isError(resetErr)) goto error;
834             }
835         } else {
836             size_t const srcSize = MIN(strm->avail_in, ZSTD_HEADERSIZE - zwd->totalInBytes);
837             memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);
838             strm->total_in += srcSize;
839             zwd->totalInBytes += srcSize;
840             strm->next_in += srcSize;
841             strm->avail_in -= srcSize;
842             if (zwd->totalInBytes < ZSTD_HEADERSIZE) return Z_OK;
843 
844             if (zwd->decompState == ZWRAP_useInit) {
845                 size_t const initErr = ZSTD_initDStream(zwd->zbd);
846                 if (ZSTD_isError(initErr)) {
847                     LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n",
848                                 ZSTD_getErrorName(initErr));
849                     goto error;
850                 }
851             } else {
852                 size_t const resetErr = ZSTD_resetDStream(zwd->zbd);
853                 if (ZSTD_isError(resetErr)) goto error;
854             }
855 
856             zwd->inBuffer.src = zwd->headerBuf;
857             zwd->inBuffer.size = ZSTD_HEADERSIZE;
858             zwd->inBuffer.pos = 0;
859             zwd->outBuffer.dst = strm->next_out;
860             zwd->outBuffer.size = 0;
861             zwd->outBuffer.pos = 0;
862             {   size_t const dErr = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
863                 LOG_WRAPPERD("inflate ZSTD_decompressStream1 errorCode=%d srcSize=%d dstCapacity=%d\n",
864                             (int)dErr, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);
865                 if (ZSTD_isError(dErr)) {
866                     LOG_WRAPPERD("ERROR: ZSTD_decompressStream1 %s\n", ZSTD_getErrorName(dErr));
867                     goto error;
868             }   }
869             if (zwd->inBuffer.pos != zwd->inBuffer.size) goto error; /* not consumed */
870         }
871     }   /* (zwd->totalInBytes < ZSTD_HEADERSIZE) */
872 
873     zwd->inBuffer.src = strm->next_in;
874     zwd->inBuffer.size = strm->avail_in;
875     zwd->inBuffer.pos = 0;
876     zwd->outBuffer.dst = strm->next_out;
877     zwd->outBuffer.size = strm->avail_out;
878     zwd->outBuffer.pos = 0;
879     {   size_t const dErr = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
880         LOG_WRAPPERD("inflate ZSTD_decompressStream2 errorCode=%d srcSize=%d dstCapacity=%d\n",
881                     (int)dErr, (int)strm->avail_in, (int)strm->avail_out);
882         if (ZSTD_isError(dErr)) {
883             zwd->errorCount++;
884             LOG_WRAPPERD("ERROR: ZSTD_decompressStream2 %s zwd->errorCount=%d\n",
885                         ZSTD_getErrorName(dErr), zwd->errorCount);
886             if (zwd->errorCount<=1) return Z_NEED_DICT; else goto error;
887         }
888         LOG_WRAPPERD("inflate inBuffer.pos=%d inBuffer.size=%d outBuffer.pos=%d outBuffer.size=%d o\n",
889                     (int)zwd->inBuffer.pos, (int)zwd->inBuffer.size, (int)zwd->outBuffer.pos, (int)zwd->outBuffer.size);
890         strm->next_out += zwd->outBuffer.pos;
891         strm->total_out += zwd->outBuffer.pos;
892         strm->avail_out -= zwd->outBuffer.pos;
893         strm->total_in += zwd->inBuffer.pos;
894         zwd->totalInBytes += zwd->inBuffer.pos;
895         strm->next_in += zwd->inBuffer.pos;
896         strm->avail_in -= zwd->inBuffer.pos;
897         if (dErr == 0) {
898             LOG_WRAPPERD("inflate Z_STREAM_END1 avail_in=%d avail_out=%d total_in=%d total_out=%d\n",
899                         (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);
900             zwd->decompState = ZWRAP_streamEnd;
901             return Z_STREAM_END;
902         }
903     }  /* dErr lifetime */
904 
905     LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
906                 (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, Z_OK);
907     return Z_OK;
908 
909 error:
910     return ZWRAPD_finishWithError(zwd, strm, 0);
911 }
912 
913 
z_inflateEnd(z_streamp strm)914 ZEXTERN int ZEXPORT z_inflateEnd OF((z_streamp strm))
915 {
916     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
917         return inflateEnd(strm);
918 
919     LOG_WRAPPERD("- inflateEnd total_in=%d total_out=%d\n",
920                 (int)(strm->total_in), (int)(strm->total_out));
921     {   ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
922         if (zwd == NULL) return Z_OK;  /* structures are already freed */
923         { size_t const freeErr = ZWRAP_freeDCtx(zwd);
924           if (ZSTD_isError(freeErr)) return Z_STREAM_ERROR; }
925         strm->state = NULL;
926     }
927     return Z_OK;
928 }
929 
930 
z_inflateSync(z_streamp strm)931 ZEXTERN int ZEXPORT z_inflateSync OF((z_streamp strm))
932 {
933     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
934         return inflateSync(strm);
935     }
936 
937     return z_inflate(strm, Z_INFLATE_SYNC);
938 }
939 
940 
941 
942 /* Advanced compression functions */
z_deflateCopy(z_streamp dest,z_streamp source)943 ZEXTERN int ZEXPORT z_deflateCopy OF((z_streamp dest,
944                                     z_streamp source))
945 {
946     if (!g_ZWRAP_useZSTDcompression)
947         return deflateCopy(dest, source);
948     return ZWRAPC_finishWithErrorMsg(source, "deflateCopy is not supported!");
949 }
950 
951 
z_deflateTune(z_streamp strm,int good_length,int max_lazy,int nice_length,int max_chain)952 ZEXTERN int ZEXPORT z_deflateTune OF((z_streamp strm,
953                                     int good_length,
954                                     int max_lazy,
955                                     int nice_length,
956                                     int max_chain))
957 {
958     if (!g_ZWRAP_useZSTDcompression)
959         return deflateTune(strm, good_length, max_lazy, nice_length, max_chain);
960     return ZWRAPC_finishWithErrorMsg(strm, "deflateTune is not supported!");
961 }
962 
963 
964 #if ZLIB_VERNUM >= 0x1260
z_deflatePending(z_streamp strm,unsigned * pending,int * bits)965 ZEXTERN int ZEXPORT z_deflatePending OF((z_streamp strm,
966                                        unsigned *pending,
967                                        int *bits))
968 {
969     if (!g_ZWRAP_useZSTDcompression)
970         return deflatePending(strm, pending, bits);
971     return ZWRAPC_finishWithErrorMsg(strm, "deflatePending is not supported!");
972 }
973 #endif
974 
975 
z_deflatePrime(z_streamp strm,int bits,int value)976 ZEXTERN int ZEXPORT z_deflatePrime OF((z_streamp strm,
977                                      int bits,
978                                      int value))
979 {
980     if (!g_ZWRAP_useZSTDcompression)
981         return deflatePrime(strm, bits, value);
982     return ZWRAPC_finishWithErrorMsg(strm, "deflatePrime is not supported!");
983 }
984 
985 
z_deflateSetHeader(z_streamp strm,gz_headerp head)986 ZEXTERN int ZEXPORT z_deflateSetHeader OF((z_streamp strm,
987                                          gz_headerp head))
988 {
989     if (!g_ZWRAP_useZSTDcompression)
990         return deflateSetHeader(strm, head);
991     return ZWRAPC_finishWithErrorMsg(strm, "deflateSetHeader is not supported!");
992 }
993 
994 
995 
996 
997 /* Advanced decompression functions */
998 #if ZLIB_VERNUM >= 0x1280
z_inflateGetDictionary(z_streamp strm,Bytef * dictionary,uInt * dictLength)999 ZEXTERN int ZEXPORT z_inflateGetDictionary OF((z_streamp strm,
1000                                              Bytef *dictionary,
1001                                              uInt  *dictLength))
1002 {
1003     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1004         return inflateGetDictionary(strm, dictionary, dictLength);
1005     return ZWRAPD_finishWithErrorMsg(strm, "inflateGetDictionary is not supported!");
1006 }
1007 #endif
1008 
1009 
z_inflateCopy(z_streamp dest,z_streamp source)1010 ZEXTERN int ZEXPORT z_inflateCopy OF((z_streamp dest,
1011                                     z_streamp source))
1012 {
1013     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !source->reserved)
1014         return inflateCopy(dest, source);
1015     return ZWRAPD_finishWithErrorMsg(source, "inflateCopy is not supported!");
1016 }
1017 
1018 
1019 #if ZLIB_VERNUM >= 0x1240
z_inflateMark(z_streamp strm)1020 ZEXTERN long ZEXPORT z_inflateMark OF((z_streamp strm))
1021 {
1022     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1023         return inflateMark(strm);
1024     return ZWRAPD_finishWithErrorMsg(strm, "inflateMark is not supported!");
1025 }
1026 #endif
1027 
1028 
z_inflatePrime(z_streamp strm,int bits,int value)1029 ZEXTERN int ZEXPORT z_inflatePrime OF((z_streamp strm,
1030                                      int bits,
1031                                      int value))
1032 {
1033     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1034         return inflatePrime(strm, bits, value);
1035     return ZWRAPD_finishWithErrorMsg(strm, "inflatePrime is not supported!");
1036 }
1037 
1038 
z_inflateGetHeader(z_streamp strm,gz_headerp head)1039 ZEXTERN int ZEXPORT z_inflateGetHeader OF((z_streamp strm,
1040                                          gz_headerp head))
1041 {
1042     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1043         return inflateGetHeader(strm, head);
1044     return ZWRAPD_finishWithErrorMsg(strm, "inflateGetHeader is not supported!");
1045 }
1046 
1047 
z_inflateBackInit_(z_streamp strm,int windowBits,unsigned char FAR * window,const char * version,int stream_size)1048 ZEXTERN int ZEXPORT z_inflateBackInit_ OF((z_streamp strm, int windowBits,
1049                                          unsigned char FAR *window,
1050                                          const char *version,
1051                                          int stream_size))
1052 {
1053     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1054         return inflateBackInit_(strm, windowBits, window, version, stream_size);
1055     return ZWRAPD_finishWithErrorMsg(strm, "inflateBackInit is not supported!");
1056 }
1057 
1058 
z_inflateBack(z_streamp strm,in_func in,void FAR * in_desc,out_func out,void FAR * out_desc)1059 ZEXTERN int ZEXPORT z_inflateBack OF((z_streamp strm,
1060                                     in_func in, void FAR *in_desc,
1061                                     out_func out, void FAR *out_desc))
1062 {
1063     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1064         return inflateBack(strm, in, in_desc, out, out_desc);
1065     return ZWRAPD_finishWithErrorMsg(strm, "inflateBack is not supported!");
1066 }
1067 
1068 
z_inflateBackEnd(z_streamp strm)1069 ZEXTERN int ZEXPORT z_inflateBackEnd OF((z_streamp strm))
1070 {
1071     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1072         return inflateBackEnd(strm);
1073     return ZWRAPD_finishWithErrorMsg(strm, "inflateBackEnd is not supported!");
1074 }
1075 
1076 
z_zlibCompileFlags(void)1077 ZEXTERN uLong ZEXPORT z_zlibCompileFlags OF((void)) { return zlibCompileFlags(); }
1078 
1079 
1080 
1081                     /* ===   utility functions  === */
1082 #ifndef Z_SOLO
1083 
z_compress(Bytef * dest,uLongf * destLen,const Bytef * source,uLong sourceLen)1084 ZEXTERN int ZEXPORT z_compress OF((Bytef *dest,   uLongf *destLen,
1085                                  const Bytef *source, uLong sourceLen))
1086 {
1087     if (!g_ZWRAP_useZSTDcompression)
1088         return compress(dest, destLen, source, sourceLen);
1089 
1090     {   size_t dstCapacity = *destLen;
1091         size_t const cSize = ZSTD_compress(dest, dstCapacity,
1092                                            source, sourceLen,
1093                                            ZWRAP_DEFAULT_CLEVEL);
1094         LOG_WRAPPERD("z_compress sourceLen=%d dstCapacity=%d\n",
1095                     (int)sourceLen, (int)dstCapacity);
1096         if (ZSTD_isError(cSize)) return Z_STREAM_ERROR;
1097         *destLen = cSize;
1098     }
1099     return Z_OK;
1100 }
1101 
1102 
z_compress2(Bytef * dest,uLongf * destLen,const Bytef * source,uLong sourceLen,int level)1103 ZEXTERN int ZEXPORT z_compress2 OF((Bytef *dest,   uLongf *destLen,
1104                                   const Bytef *source, uLong sourceLen,
1105                                   int level))
1106 {
1107     if (!g_ZWRAP_useZSTDcompression)
1108         return compress2(dest, destLen, source, sourceLen, level);
1109 
1110     { size_t dstCapacity = *destLen;
1111       size_t const cSize = ZSTD_compress(dest, dstCapacity, source, sourceLen, level);
1112       if (ZSTD_isError(cSize)) return Z_STREAM_ERROR;
1113       *destLen = cSize;
1114     }
1115     return Z_OK;
1116 }
1117 
1118 
z_compressBound(uLong sourceLen)1119 ZEXTERN uLong ZEXPORT z_compressBound OF((uLong sourceLen))
1120 {
1121     if (!g_ZWRAP_useZSTDcompression)
1122         return compressBound(sourceLen);
1123 
1124     return ZSTD_compressBound(sourceLen);
1125 }
1126 
1127 
z_uncompress(Bytef * dest,uLongf * destLen,const Bytef * source,uLong sourceLen)1128 ZEXTERN int ZEXPORT z_uncompress OF((Bytef *dest,   uLongf *destLen,
1129                                    const Bytef *source, uLong sourceLen))
1130 {
1131     if (!ZSTD_isFrame(source, sourceLen))
1132         return uncompress(dest, destLen, source, sourceLen);
1133 
1134     { size_t dstCapacity = *destLen;
1135       size_t const dSize = ZSTD_decompress(dest, dstCapacity, source, sourceLen);
1136       if (ZSTD_isError(dSize)) return Z_STREAM_ERROR;
1137       *destLen = dSize;
1138      }
1139     return Z_OK;
1140 }
1141 
1142 #endif /* !Z_SOLO */
1143 
1144 
1145                         /* checksum functions */
1146 
z_adler32(uLong adler,const Bytef * buf,uInt len)1147 ZEXTERN uLong ZEXPORT z_adler32 OF((uLong adler, const Bytef *buf, uInt len))
1148 {
1149     return adler32(adler, buf, len);
1150 }
1151 
z_crc32(uLong crc,const Bytef * buf,uInt len)1152 ZEXTERN uLong ZEXPORT z_crc32   OF((uLong crc, const Bytef *buf, uInt len))
1153 {
1154     return crc32(crc, buf, len);
1155 }
1156 
1157 
1158 #if ZLIB_VERNUM >= 0x12B0
z_adler32_z(uLong adler,const Bytef * buf,z_size_t len)1159 ZEXTERN uLong ZEXPORT z_adler32_z OF((uLong adler, const Bytef *buf, z_size_t len))
1160 {
1161     return adler32_z(adler, buf, len);
1162 }
1163 
z_crc32_z(uLong crc,const Bytef * buf,z_size_t len)1164 ZEXTERN uLong ZEXPORT z_crc32_z OF((uLong crc, const Bytef *buf, z_size_t len))
1165 {
1166     return crc32_z(crc, buf, len);
1167 }
1168 #endif
1169 
1170 
1171 #if ZLIB_VERNUM >= 0x1270
z_get_crc_table(void)1172 ZEXTERN const z_crc_t FAR * ZEXPORT z_get_crc_table    OF((void))
1173 {
1174     return get_crc_table();
1175 }
1176 #endif
1177