xref: /freebsd/sys/contrib/zstd/zlibWrapper/zstd_zlibwrapper.c (revision 5ff13fbc199bdf5f0572845351c68ee5ca828e71)
1 /*
2  * Copyright (c) 2016-2021, 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     {   unsigned initErr = 0;
209         ZSTD_parameters const params = ZSTD_getParams(zwc->compressionLevel, pledgedSrcSize, dictSize);
210         ZSTD_CCtx_params* cctxParams = ZSTD_createCCtxParams();
211         if (!cctxParams) return Z_STREAM_ERROR;
212         LOG_WRAPPERC("pledgedSrcSize=%d windowLog=%d chainLog=%d hashLog=%d searchLog=%d minMatch=%d strategy=%d\n",
213                     (int)pledgedSrcSize, params.cParams.windowLog, params.cParams.chainLog, params.cParams.hashLog, params.cParams.searchLog, params.cParams.minMatch, params.cParams.strategy);
214 
215         initErr |= ZSTD_isError(ZSTD_CCtx_reset(zwc->zbc, ZSTD_reset_session_only));
216         initErr |= ZSTD_isError(ZSTD_CCtxParams_init_advanced(cctxParams, params));
217         initErr |= ZSTD_isError(ZSTD_CCtx_setParametersUsingCCtxParams(zwc->zbc, cctxParams));
218         initErr |= ZSTD_isError(ZSTD_CCtx_setPledgedSrcSize(zwc->zbc, pledgedSrcSize));
219         initErr |= ZSTD_isError(ZSTD_CCtx_loadDictionary(zwc->zbc, dict, dictSize));
220 
221         ZSTD_freeCCtxParams(cctxParams);
222         if (initErr) return Z_STREAM_ERROR;
223     }
224 
225     return Z_OK;
226 }
227 
228 
ZWRAPC_finishWithError(ZWRAP_CCtx * zwc,z_streamp strm,int error)229 static int ZWRAPC_finishWithError(ZWRAP_CCtx* zwc, z_streamp strm, int error)
230 {
231     LOG_WRAPPERC("- ZWRAPC_finishWithError=%d\n", error);
232     if (zwc) ZWRAP_freeCCtx(zwc);
233     if (strm) strm->state = NULL;
234     return (error) ? error : Z_STREAM_ERROR;
235 }
236 
237 
ZWRAPC_finishWithErrorMsg(z_streamp strm,char * message)238 static int ZWRAPC_finishWithErrorMsg(z_streamp strm, char* message)
239 {
240     ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
241     strm->msg = message;
242     if (zwc == NULL) return Z_STREAM_ERROR;
243 
244     return ZWRAPC_finishWithError(zwc, strm, 0);
245 }
246 
247 
ZWRAP_setPledgedSrcSize(z_streamp strm,unsigned long long pledgedSrcSize)248 int ZWRAP_setPledgedSrcSize(z_streamp strm, unsigned long long pledgedSrcSize)
249 {
250     ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
251     if (zwc == NULL) return Z_STREAM_ERROR;
252 
253     zwc->pledgedSrcSize = pledgedSrcSize;
254     zwc->comprState = ZWRAP_useInit;
255     return Z_OK;
256 }
257 
convert_into_sis(void * ptr)258 static struct internal_state* convert_into_sis(void* ptr)
259 {
260     return (struct internal_state*) ptr;
261 }
262 
z_deflateInit_(z_streamp strm,int level,const char * version,int stream_size)263 ZEXTERN int ZEXPORT z_deflateInit_ OF((z_streamp strm, int level,
264                                      const char *version, int stream_size))
265 {
266     ZWRAP_CCtx* zwc;
267 
268     LOG_WRAPPERC("- deflateInit level=%d\n", level);
269     if (!g_ZWRAP_useZSTDcompression) {
270         return deflateInit_((strm), (level), version, stream_size);
271     }
272 
273     zwc = ZWRAP_createCCtx(strm);
274     if (zwc == NULL) return Z_MEM_ERROR;
275 
276     if (level == Z_DEFAULT_COMPRESSION)
277         level = ZWRAP_DEFAULT_CLEVEL;
278 
279     zwc->streamEnd = 0;
280     zwc->totalInBytes = 0;
281     zwc->compressionLevel = level;
282     strm->state = convert_into_sis(zwc); /* use state which in not used by user */
283     strm->total_in = 0;
284     strm->total_out = 0;
285     strm->adler = 0;
286     return Z_OK;
287 }
288 
289 
z_deflateInit2_(z_streamp strm,int level,int method,int windowBits,int memLevel,int strategy,const char * version,int stream_size)290 ZEXTERN int ZEXPORT z_deflateInit2_ OF((z_streamp strm, int level, int method,
291                                       int windowBits, int memLevel,
292                                       int strategy, const char *version,
293                                       int stream_size))
294 {
295     if (!g_ZWRAP_useZSTDcompression)
296         return deflateInit2_(strm, level, method, windowBits, memLevel, strategy, version, stream_size);
297 
298     return z_deflateInit_ (strm, level, version, stream_size);
299 }
300 
301 
ZWRAP_deflateReset_keepDict(z_streamp strm)302 int ZWRAP_deflateReset_keepDict(z_streamp strm)
303 {
304     LOG_WRAPPERC("- ZWRAP_deflateReset_keepDict\n");
305     if (!g_ZWRAP_useZSTDcompression)
306         return deflateReset(strm);
307 
308     { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
309       if (zwc) {
310           zwc->streamEnd = 0;
311           zwc->totalInBytes = 0;
312       }
313     }
314 
315     strm->total_in = 0;
316     strm->total_out = 0;
317     strm->adler = 0;
318     return Z_OK;
319 }
320 
321 
z_deflateReset(z_streamp strm)322 ZEXTERN int ZEXPORT z_deflateReset OF((z_streamp strm))
323 {
324     LOG_WRAPPERC("- deflateReset\n");
325     if (!g_ZWRAP_useZSTDcompression)
326         return deflateReset(strm);
327 
328     ZWRAP_deflateReset_keepDict(strm);
329 
330     { ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
331       if (zwc) zwc->comprState = ZWRAP_useInit;
332     }
333     return Z_OK;
334 }
335 
336 
z_deflateSetDictionary(z_streamp strm,const Bytef * dictionary,uInt dictLength)337 ZEXTERN int ZEXPORT z_deflateSetDictionary OF((z_streamp strm,
338                                              const Bytef *dictionary,
339                                              uInt  dictLength))
340 {
341     if (!g_ZWRAP_useZSTDcompression) {
342         LOG_WRAPPERC("- deflateSetDictionary\n");
343         return deflateSetDictionary(strm, dictionary, dictLength);
344     }
345 
346     {   ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
347         LOG_WRAPPERC("- deflateSetDictionary level=%d\n", (int)zwc->compressionLevel);
348         if (!zwc) return Z_STREAM_ERROR;
349         if (zwc->zbc == NULL) {
350             zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
351             if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
352         }
353         { int res = ZWRAP_initializeCStream(zwc, dictionary, dictLength, ZSTD_CONTENTSIZE_UNKNOWN);
354           if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res); }
355         zwc->comprState = ZWRAP_useReset;
356     }
357 
358     return Z_OK;
359 }
360 
361 
z_deflate(z_streamp strm,int flush)362 ZEXTERN int ZEXPORT z_deflate OF((z_streamp strm, int flush))
363 {
364     ZWRAP_CCtx* zwc;
365 
366     if (!g_ZWRAP_useZSTDcompression) {
367         LOG_WRAPPERC("- deflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d\n",
368                     (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);
369         return deflate(strm, flush);
370     }
371 
372     zwc = (ZWRAP_CCtx*) strm->state;
373     if (zwc == NULL) { LOG_WRAPPERC("zwc == NULL\n"); return Z_STREAM_ERROR; }
374 
375     if (zwc->zbc == NULL) {
376         zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
377         if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
378         { int const initErr = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : ZSTD_CONTENTSIZE_UNKNOWN);
379           if (initErr != Z_OK) return ZWRAPC_finishWithError(zwc, strm, initErr); }
380         if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
381     } else {
382         if (zwc->totalInBytes == 0) {
383             if (zwc->comprState == ZWRAP_useReset) {
384                 size_t resetErr = ZSTD_CCtx_reset(zwc->zbc, ZSTD_reset_session_only);
385                 if (ZSTD_isError(resetErr)) {
386                     LOG_WRAPPERC("ERROR: ZSTD_CCtx_reset errorCode=%s\n",
387                                 ZSTD_getErrorName(resetErr));
388                     return ZWRAPC_finishWithError(zwc, strm, 0);
389                 }
390                 resetErr = ZSTD_CCtx_setPledgedSrcSize(zwc->zbc, (flush == Z_FINISH) ? strm->avail_in : zwc->pledgedSrcSize);
391                 if (ZSTD_isError(resetErr)) {
392                     LOG_WRAPPERC("ERROR: ZSTD_CCtx_setPledgedSrcSize errorCode=%s\n",
393                                 ZSTD_getErrorName(resetErr));
394                     return ZWRAPC_finishWithError(zwc, strm, 0);
395                 }
396             } else {
397                 int const res = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : ZSTD_CONTENTSIZE_UNKNOWN);
398                 if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res);
399                 if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
400             }
401         }  /* (zwc->totalInBytes == 0) */
402     }  /* ! (zwc->zbc == NULL) */
403 
404     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);
405     if (strm->avail_in > 0) {
406         zwc->inBuffer.src = strm->next_in;
407         zwc->inBuffer.size = strm->avail_in;
408         zwc->inBuffer.pos = 0;
409         zwc->outBuffer.dst = strm->next_out;
410         zwc->outBuffer.size = strm->avail_out;
411         zwc->outBuffer.pos = 0;
412         { size_t const cErr = ZSTD_compressStream(zwc->zbc, &zwc->outBuffer, &zwc->inBuffer);
413           LOG_WRAPPERC("deflate ZSTD_compressStream srcSize=%d dstCapacity=%d\n", (int)zwc->inBuffer.size, (int)zwc->outBuffer.size);
414           if (ZSTD_isError(cErr)) return ZWRAPC_finishWithError(zwc, strm, 0);
415         }
416         strm->next_out += zwc->outBuffer.pos;
417         strm->total_out += zwc->outBuffer.pos;
418         strm->avail_out -= zwc->outBuffer.pos;
419         strm->total_in += zwc->inBuffer.pos;
420         zwc->totalInBytes += zwc->inBuffer.pos;
421         strm->next_in += zwc->inBuffer.pos;
422         strm->avail_in -= zwc->inBuffer.pos;
423     }
424 
425     if (flush == Z_FULL_FLUSH
426 #if ZLIB_VERNUM >= 0x1240
427         || flush == Z_TREES
428 #endif
429         || flush == Z_BLOCK)
430         return ZWRAPC_finishWithErrorMsg(strm, "Z_FULL_FLUSH, Z_BLOCK and Z_TREES are not supported!");
431 
432     if (flush == Z_FINISH) {
433         size_t bytesLeft;
434         if (zwc->streamEnd) return Z_STREAM_END;
435         zwc->outBuffer.dst = strm->next_out;
436         zwc->outBuffer.size = strm->avail_out;
437         zwc->outBuffer.pos = 0;
438         bytesLeft = ZSTD_endStream(zwc->zbc, &zwc->outBuffer);
439         LOG_WRAPPERC("deflate ZSTD_endStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
440         if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);
441         strm->next_out += zwc->outBuffer.pos;
442         strm->total_out += zwc->outBuffer.pos;
443         strm->avail_out -= zwc->outBuffer.pos;
444         if (bytesLeft == 0) {
445             zwc->streamEnd = 1;
446             LOG_WRAPPERC("Z_STREAM_END2 strm->total_in=%d strm->avail_out=%d strm->total_out=%d\n",
447                         (int)strm->total_in, (int)strm->avail_out, (int)strm->total_out);
448             return Z_STREAM_END;
449     }   }
450     else
451     if (flush == Z_SYNC_FLUSH || flush == Z_PARTIAL_FLUSH) {
452         size_t bytesLeft;
453         zwc->outBuffer.dst = strm->next_out;
454         zwc->outBuffer.size = strm->avail_out;
455         zwc->outBuffer.pos = 0;
456         bytesLeft = ZSTD_flushStream(zwc->zbc, &zwc->outBuffer);
457         LOG_WRAPPERC("deflate ZSTD_flushStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
458         if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);
459         strm->next_out += zwc->outBuffer.pos;
460         strm->total_out += zwc->outBuffer.pos;
461         strm->avail_out -= zwc->outBuffer.pos;
462     }
463     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);
464     return Z_OK;
465 }
466 
467 
z_deflateEnd(z_streamp strm)468 ZEXTERN int ZEXPORT z_deflateEnd OF((z_streamp strm))
469 {
470     if (!g_ZWRAP_useZSTDcompression) {
471         LOG_WRAPPERC("- deflateEnd\n");
472         return deflateEnd(strm);
473     }
474     LOG_WRAPPERC("- deflateEnd total_in=%d total_out=%d\n", (int)(strm->total_in), (int)(strm->total_out));
475     {   size_t errorCode;
476         ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
477         if (zwc == NULL) return Z_OK;  /* structures are already freed */
478         strm->state = NULL;
479         errorCode = ZWRAP_freeCCtx(zwc);
480         if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
481     }
482     return Z_OK;
483 }
484 
485 
z_deflateBound(z_streamp strm,uLong sourceLen)486 ZEXTERN uLong ZEXPORT z_deflateBound OF((z_streamp strm,
487                                        uLong sourceLen))
488 {
489     if (!g_ZWRAP_useZSTDcompression)
490         return deflateBound(strm, sourceLen);
491 
492     return ZSTD_compressBound(sourceLen);
493 }
494 
495 
z_deflateParams(z_streamp strm,int level,int strategy)496 ZEXTERN int ZEXPORT z_deflateParams OF((z_streamp strm,
497                                       int level,
498                                       int strategy))
499 {
500     if (!g_ZWRAP_useZSTDcompression) {
501         LOG_WRAPPERC("- deflateParams level=%d strategy=%d\n", level, strategy);
502         return deflateParams(strm, level, strategy);
503     }
504 
505     return Z_OK;
506 }
507 
508 
509 
510 
511 
512 /* ===   Decompression   === */
513 
514 typedef enum { ZWRAP_ZLIB_STREAM, ZWRAP_ZSTD_STREAM, ZWRAP_UNKNOWN_STREAM } ZWRAP_stream_type;
515 
516 typedef struct {
517     ZSTD_DStream* zbd;
518     char headerBuf[16];   /* must be >= ZSTD_frameHeaderSize_min */
519     int errorCount;
520     unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */
521     ZWRAP_state_t decompState;
522     ZSTD_inBuffer inBuffer;
523     ZSTD_outBuffer outBuffer;
524 
525     /* zlib params */
526     int stream_size;
527     char *version;
528     int windowBits;
529     ZSTD_customMem customMem;
530     z_stream allocFunc; /* just to copy zalloc, zfree, opaque */
531 } ZWRAP_DCtx;
532 
533 
ZWRAP_initDCtx(ZWRAP_DCtx * zwd)534 static void ZWRAP_initDCtx(ZWRAP_DCtx* zwd)
535 {
536     zwd->errorCount = 0;
537     zwd->outBuffer.pos = 0;
538     zwd->outBuffer.size = 0;
539 }
540 
ZWRAP_createDCtx(z_streamp strm)541 static ZWRAP_DCtx* ZWRAP_createDCtx(z_streamp strm)
542 {
543     ZWRAP_DCtx* zwd;
544     ZSTD_customMem customMem = { NULL, NULL, NULL };
545 
546     if (strm->zalloc && strm->zfree) {
547         customMem.customAlloc = ZWRAP_allocFunction;
548         customMem.customFree = ZWRAP_freeFunction;
549     }
550     customMem.opaque = strm;
551 
552     zwd = (ZWRAP_DCtx*)ZWRAP_customCalloc(sizeof(ZWRAP_DCtx), customMem);
553     if (zwd == NULL) return NULL;
554     zwd->allocFunc = *strm;
555     customMem.opaque = &zwd->allocFunc;
556     zwd->customMem = customMem;
557 
558     ZWRAP_initDCtx(zwd);
559     return zwd;
560 }
561 
ZWRAP_freeDCtx(ZWRAP_DCtx * zwd)562 static size_t ZWRAP_freeDCtx(ZWRAP_DCtx* zwd)
563 {
564     if (zwd==NULL) return 0;   /* support free on null */
565     ZSTD_freeDStream(zwd->zbd);
566     ZWRAP_customFree(zwd->version, zwd->customMem);
567     ZWRAP_customFree(zwd, zwd->customMem);
568     return 0;
569 }
570 
571 
ZWRAP_isUsingZSTDdecompression(z_streamp strm)572 int ZWRAP_isUsingZSTDdecompression(z_streamp strm)
573 {
574     if (strm == NULL) return 0;
575     return (strm->reserved == ZWRAP_ZSTD_STREAM);
576 }
577 
578 
ZWRAPD_finishWithError(ZWRAP_DCtx * zwd,z_streamp strm,int error)579 static int ZWRAPD_finishWithError(ZWRAP_DCtx* zwd, z_streamp strm, int error)
580 {
581     LOG_WRAPPERD("- ZWRAPD_finishWithError=%d\n", error);
582     ZWRAP_freeDCtx(zwd);
583     strm->state = NULL;
584     return (error) ? error : Z_STREAM_ERROR;
585 }
586 
ZWRAPD_finishWithErrorMsg(z_streamp strm,char * message)587 static int ZWRAPD_finishWithErrorMsg(z_streamp strm, char* message)
588 {
589     ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
590     strm->msg = message;
591     if (zwd == NULL) return Z_STREAM_ERROR;
592 
593     return ZWRAPD_finishWithError(zwd, strm, 0);
594 }
595 
596 
z_inflateInit_(z_streamp strm,const char * version,int stream_size)597 ZEXTERN int ZEXPORT z_inflateInit_ OF((z_streamp strm,
598                                      const char* version, int stream_size))
599 {
600     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
601         strm->reserved = ZWRAP_ZLIB_STREAM;
602         return inflateInit(strm);
603     }
604 
605     {   ZWRAP_DCtx* const zwd = ZWRAP_createDCtx(strm);
606         LOG_WRAPPERD("- inflateInit\n");
607         if (zwd == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
608 
609         zwd->version = (char*)ZWRAP_customMalloc(strlen(version)+1, zwd->customMem);
610         if (zwd->version == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
611         strcpy(zwd->version, version);
612 
613         zwd->stream_size = stream_size;
614         zwd->totalInBytes = 0;
615         strm->state = convert_into_sis(zwd);
616         strm->total_in = 0;
617         strm->total_out = 0;
618         strm->reserved = ZWRAP_UNKNOWN_STREAM;
619         strm->adler = 0;
620     }
621 
622     return Z_OK;
623 }
624 
625 
z_inflateInit2_(z_streamp strm,int windowBits,const char * version,int stream_size)626 ZEXTERN int ZEXPORT z_inflateInit2_ OF((z_streamp strm, int  windowBits,
627                                       const char *version, int stream_size))
628 {
629     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
630         return inflateInit2_(strm, windowBits, version, stream_size);
631     }
632 
633     {   int const ret = z_inflateInit_ (strm, version, stream_size);
634         LOG_WRAPPERD("- inflateInit2 windowBits=%d\n", windowBits);
635         if (ret == Z_OK) {
636             ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*)strm->state;
637             if (zwd == NULL) return Z_STREAM_ERROR;
638             zwd->windowBits = windowBits;
639         }
640         return ret;
641     }
642 }
643 
ZWRAP_inflateReset_keepDict(z_streamp strm)644 int ZWRAP_inflateReset_keepDict(z_streamp strm)
645 {
646     LOG_WRAPPERD("- ZWRAP_inflateReset_keepDict\n");
647     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
648         return inflateReset(strm);
649 
650     {   ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
651         if (zwd == NULL) return Z_STREAM_ERROR;
652         ZWRAP_initDCtx(zwd);
653         zwd->decompState = ZWRAP_useReset;
654         zwd->totalInBytes = 0;
655     }
656 
657     strm->total_in = 0;
658     strm->total_out = 0;
659     return Z_OK;
660 }
661 
662 
z_inflateReset(z_streamp strm)663 ZEXTERN int ZEXPORT z_inflateReset OF((z_streamp strm))
664 {
665     LOG_WRAPPERD("- inflateReset\n");
666     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
667         return inflateReset(strm);
668 
669     { int const ret = ZWRAP_inflateReset_keepDict(strm);
670       if (ret != Z_OK) return ret; }
671 
672     { ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
673       if (zwd == NULL) return Z_STREAM_ERROR;
674       zwd->decompState = ZWRAP_useInit; }
675 
676     return Z_OK;
677 }
678 
679 
680 #if ZLIB_VERNUM >= 0x1240
z_inflateReset2(z_streamp strm,int windowBits)681 ZEXTERN int ZEXPORT z_inflateReset2 OF((z_streamp strm,
682                                       int windowBits))
683 {
684     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
685         return inflateReset2(strm, windowBits);
686 
687     {   int const ret = z_inflateReset (strm);
688         if (ret == Z_OK) {
689             ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*)strm->state;
690             if (zwd == NULL) return Z_STREAM_ERROR;
691             zwd->windowBits = windowBits;
692         }
693         return ret;
694     }
695 }
696 #endif
697 
698 
z_inflateSetDictionary(z_streamp strm,const Bytef * dictionary,uInt dictLength)699 ZEXTERN int ZEXPORT z_inflateSetDictionary OF((z_streamp strm,
700                                              const Bytef *dictionary,
701                                              uInt  dictLength))
702 {
703     LOG_WRAPPERD("- inflateSetDictionary\n");
704     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
705         return inflateSetDictionary(strm, dictionary, dictLength);
706 
707     {   ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
708         if (zwd == NULL || zwd->zbd == NULL) return Z_STREAM_ERROR;
709         { size_t const initErr = ZSTD_initDStream_usingDict(zwd->zbd, dictionary, dictLength);
710           if (ZSTD_isError(initErr)) return ZWRAPD_finishWithError(zwd, strm, 0); }
711         zwd->decompState = ZWRAP_useReset;
712 
713         if (zwd->totalInBytes == ZSTD_HEADERSIZE) {
714             zwd->inBuffer.src = zwd->headerBuf;
715             zwd->inBuffer.size = zwd->totalInBytes;
716             zwd->inBuffer.pos = 0;
717             zwd->outBuffer.dst = strm->next_out;
718             zwd->outBuffer.size = 0;
719             zwd->outBuffer.pos = 0;
720             {   size_t const errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
721                 LOG_WRAPPERD("inflateSetDictionary ZSTD_decompressStream errorCode=%d srcSize=%d dstCapacity=%d\n",
722                              (int)errorCode, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);
723                 if (zwd->inBuffer.pos < zwd->outBuffer.size || ZSTD_isError(errorCode)) {
724                     LOG_WRAPPERD("ERROR: ZSTD_decompressStream %s\n",
725                                  ZSTD_getErrorName(errorCode));
726                     return ZWRAPD_finishWithError(zwd, strm, 0);
727     }   }   }   }
728 
729     return Z_OK;
730 }
731 
732 
z_inflate(z_streamp strm,int flush)733 ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush))
734 {
735     ZWRAP_DCtx* zwd;
736 
737     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
738         int const result = inflate(strm, flush);
739         LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
740                      (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, result);
741         return result;
742     }
743 
744     if (strm->avail_in <= 0) return Z_OK;
745 
746     zwd = (ZWRAP_DCtx*) strm->state;
747     LOG_WRAPPERD("- inflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d\n",
748                  (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);
749 
750     if (zwd == NULL) return Z_STREAM_ERROR;
751     if (zwd->decompState == ZWRAP_streamEnd) return Z_STREAM_END;
752 
753     if (zwd->totalInBytes < ZLIB_HEADERSIZE) {
754         if (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) {
755             if (ZWRAP_readLE32(strm->next_in) != ZSTD_MAGICNUMBER) {
756                 {   int const initErr = (zwd->windowBits) ?
757                                 inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size) :
758                                 inflateInit_(strm, zwd->version, zwd->stream_size);
759                     LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", initErr);
760                     if (initErr != Z_OK) return ZWRAPD_finishWithError(zwd, strm, initErr);
761                 }
762 
763                 strm->reserved = ZWRAP_ZLIB_STREAM;
764                 { size_t const freeErr = ZWRAP_freeDCtx(zwd);
765                   if (ZSTD_isError(freeErr)) goto error; }
766 
767                 {   int const result = (flush == Z_INFLATE_SYNC) ?
768                                         inflateSync(strm) :
769                                         inflate(strm, flush);
770                     LOG_WRAPPERD("- inflate3 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
771                                  (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, res);
772                     return result;
773             }   }
774         } else {  /* ! (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) */
775             size_t const srcSize = MIN(strm->avail_in, ZLIB_HEADERSIZE - zwd->totalInBytes);
776             memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);
777             strm->total_in += srcSize;
778             zwd->totalInBytes += srcSize;
779             strm->next_in += srcSize;
780             strm->avail_in -= srcSize;
781             if (zwd->totalInBytes < ZLIB_HEADERSIZE) return Z_OK;
782 
783             if (ZWRAP_readLE32(zwd->headerBuf) != ZSTD_MAGICNUMBER) {
784                 z_stream strm2;
785                 strm2.next_in = strm->next_in;
786                 strm2.avail_in = strm->avail_in;
787                 strm2.next_out = strm->next_out;
788                 strm2.avail_out = strm->avail_out;
789 
790                 {   int const initErr = (zwd->windowBits) ?
791                                 inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size) :
792                                 inflateInit_(strm, zwd->version, zwd->stream_size);
793                     LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", initErr);
794                     if (initErr != Z_OK) return ZWRAPD_finishWithError(zwd, strm, initErr);
795                 }
796 
797                 /* inflate header */
798                 strm->next_in = (unsigned char*)zwd->headerBuf;
799                 strm->avail_in = ZLIB_HEADERSIZE;
800                 strm->avail_out = 0;
801                 {   int const dErr = inflate(strm, Z_NO_FLUSH);
802                     LOG_WRAPPERD("ZLIB inflate errorCode=%d strm->avail_in=%d\n",
803                                   dErr, (int)strm->avail_in);
804                     if (dErr != Z_OK)
805                         return ZWRAPD_finishWithError(zwd, strm, dErr);
806                 }
807                 if (strm->avail_in > 0) goto error;
808 
809                 strm->next_in = strm2.next_in;
810                 strm->avail_in = strm2.avail_in;
811                 strm->next_out = strm2.next_out;
812                 strm->avail_out = strm2.avail_out;
813 
814                 strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */
815                 { size_t const freeErr = ZWRAP_freeDCtx(zwd);
816                   if (ZSTD_isError(freeErr)) goto error; }
817 
818                 {   int const result = (flush == Z_INFLATE_SYNC) ?
819                                        inflateSync(strm) :
820                                        inflate(strm, flush);
821                     LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
822                                  (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, res);
823                     return result;
824         }   }   }  /* if ! (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) */
825     }  /* (zwd->totalInBytes < ZLIB_HEADERSIZE) */
826 
827     strm->reserved = ZWRAP_ZSTD_STREAM; /* mark as zstd steam */
828 
829     if (flush == Z_INFLATE_SYNC) { strm->msg = "inflateSync is not supported!"; goto error; }
830 
831     if (!zwd->zbd) {
832         zwd->zbd = ZSTD_createDStream_advanced(zwd->customMem);
833         if (zwd->zbd == NULL) { LOG_WRAPPERD("ERROR: ZSTD_createDStream_advanced\n"); goto error; }
834         zwd->decompState = ZWRAP_useInit;
835     }
836 
837     if (zwd->totalInBytes < ZSTD_HEADERSIZE) {
838         if (zwd->totalInBytes == 0 && strm->avail_in >= ZSTD_HEADERSIZE) {
839             if (zwd->decompState == ZWRAP_useInit) {
840                 size_t const initErr = ZSTD_initDStream(zwd->zbd);
841                 if (ZSTD_isError(initErr)) {
842                     LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n",
843                                  ZSTD_getErrorName(initErr));
844                     goto error;
845                 }
846             } else {
847                 size_t const resetErr = ZSTD_DCtx_reset(zwd->zbd, ZSTD_reset_session_only);
848                 if (ZSTD_isError(resetErr)) goto error;
849             }
850         } else {
851             size_t const srcSize = MIN(strm->avail_in, ZSTD_HEADERSIZE - zwd->totalInBytes);
852             memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);
853             strm->total_in += srcSize;
854             zwd->totalInBytes += srcSize;
855             strm->next_in += srcSize;
856             strm->avail_in -= srcSize;
857             if (zwd->totalInBytes < ZSTD_HEADERSIZE) return Z_OK;
858 
859             if (zwd->decompState == ZWRAP_useInit) {
860                 size_t const initErr = ZSTD_initDStream(zwd->zbd);
861                 if (ZSTD_isError(initErr)) {
862                     LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n",
863                                 ZSTD_getErrorName(initErr));
864                     goto error;
865                 }
866             } else {
867                 size_t const resetErr = ZSTD_DCtx_reset(zwd->zbd, ZSTD_reset_session_only);
868                 if (ZSTD_isError(resetErr)) goto error;
869             }
870 
871             zwd->inBuffer.src = zwd->headerBuf;
872             zwd->inBuffer.size = ZSTD_HEADERSIZE;
873             zwd->inBuffer.pos = 0;
874             zwd->outBuffer.dst = strm->next_out;
875             zwd->outBuffer.size = 0;
876             zwd->outBuffer.pos = 0;
877             {   size_t const dErr = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
878                 LOG_WRAPPERD("inflate ZSTD_decompressStream1 errorCode=%d srcSize=%d dstCapacity=%d\n",
879                             (int)dErr, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);
880                 if (ZSTD_isError(dErr)) {
881                     LOG_WRAPPERD("ERROR: ZSTD_decompressStream1 %s\n", ZSTD_getErrorName(dErr));
882                     goto error;
883             }   }
884             if (zwd->inBuffer.pos != zwd->inBuffer.size) goto error; /* not consumed */
885         }
886     }   /* (zwd->totalInBytes < ZSTD_HEADERSIZE) */
887 
888     zwd->inBuffer.src = strm->next_in;
889     zwd->inBuffer.size = strm->avail_in;
890     zwd->inBuffer.pos = 0;
891     zwd->outBuffer.dst = strm->next_out;
892     zwd->outBuffer.size = strm->avail_out;
893     zwd->outBuffer.pos = 0;
894     {   size_t const dErr = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
895         LOG_WRAPPERD("inflate ZSTD_decompressStream2 errorCode=%d srcSize=%d dstCapacity=%d\n",
896                     (int)dErr, (int)strm->avail_in, (int)strm->avail_out);
897         if (ZSTD_isError(dErr)) {
898             zwd->errorCount++;
899             LOG_WRAPPERD("ERROR: ZSTD_decompressStream2 %s zwd->errorCount=%d\n",
900                         ZSTD_getErrorName(dErr), zwd->errorCount);
901             if (zwd->errorCount<=1) return Z_NEED_DICT; else goto error;
902         }
903         LOG_WRAPPERD("inflate inBuffer.pos=%d inBuffer.size=%d outBuffer.pos=%d outBuffer.size=%d o\n",
904                     (int)zwd->inBuffer.pos, (int)zwd->inBuffer.size, (int)zwd->outBuffer.pos, (int)zwd->outBuffer.size);
905         strm->next_out += zwd->outBuffer.pos;
906         strm->total_out += zwd->outBuffer.pos;
907         strm->avail_out -= zwd->outBuffer.pos;
908         strm->total_in += zwd->inBuffer.pos;
909         zwd->totalInBytes += zwd->inBuffer.pos;
910         strm->next_in += zwd->inBuffer.pos;
911         strm->avail_in -= zwd->inBuffer.pos;
912         if (dErr == 0) {
913             LOG_WRAPPERD("inflate Z_STREAM_END1 avail_in=%d avail_out=%d total_in=%d total_out=%d\n",
914                         (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);
915             zwd->decompState = ZWRAP_streamEnd;
916             return Z_STREAM_END;
917         }
918     }  /* dErr lifetime */
919 
920     LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",
921                 (int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, Z_OK);
922     return Z_OK;
923 
924 error:
925     return ZWRAPD_finishWithError(zwd, strm, 0);
926 }
927 
928 
z_inflateEnd(z_streamp strm)929 ZEXTERN int ZEXPORT z_inflateEnd OF((z_streamp strm))
930 {
931     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
932         return inflateEnd(strm);
933 
934     LOG_WRAPPERD("- inflateEnd total_in=%d total_out=%d\n",
935                 (int)(strm->total_in), (int)(strm->total_out));
936     {   ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
937         if (zwd == NULL) return Z_OK;  /* structures are already freed */
938         { size_t const freeErr = ZWRAP_freeDCtx(zwd);
939           if (ZSTD_isError(freeErr)) return Z_STREAM_ERROR; }
940         strm->state = NULL;
941     }
942     return Z_OK;
943 }
944 
945 
z_inflateSync(z_streamp strm)946 ZEXTERN int ZEXPORT z_inflateSync OF((z_streamp strm))
947 {
948     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
949         return inflateSync(strm);
950     }
951 
952     return z_inflate(strm, Z_INFLATE_SYNC);
953 }
954 
955 
956 
957 /* Advanced compression functions */
z_deflateCopy(z_streamp dest,z_streamp source)958 ZEXTERN int ZEXPORT z_deflateCopy OF((z_streamp dest,
959                                     z_streamp source))
960 {
961     if (!g_ZWRAP_useZSTDcompression)
962         return deflateCopy(dest, source);
963     return ZWRAPC_finishWithErrorMsg(source, "deflateCopy is not supported!");
964 }
965 
966 
z_deflateTune(z_streamp strm,int good_length,int max_lazy,int nice_length,int max_chain)967 ZEXTERN int ZEXPORT z_deflateTune OF((z_streamp strm,
968                                     int good_length,
969                                     int max_lazy,
970                                     int nice_length,
971                                     int max_chain))
972 {
973     if (!g_ZWRAP_useZSTDcompression)
974         return deflateTune(strm, good_length, max_lazy, nice_length, max_chain);
975     return ZWRAPC_finishWithErrorMsg(strm, "deflateTune is not supported!");
976 }
977 
978 
979 #if ZLIB_VERNUM >= 0x1260
z_deflatePending(z_streamp strm,unsigned * pending,int * bits)980 ZEXTERN int ZEXPORT z_deflatePending OF((z_streamp strm,
981                                        unsigned *pending,
982                                        int *bits))
983 {
984     if (!g_ZWRAP_useZSTDcompression)
985         return deflatePending(strm, pending, bits);
986     return ZWRAPC_finishWithErrorMsg(strm, "deflatePending is not supported!");
987 }
988 #endif
989 
990 
z_deflatePrime(z_streamp strm,int bits,int value)991 ZEXTERN int ZEXPORT z_deflatePrime OF((z_streamp strm,
992                                      int bits,
993                                      int value))
994 {
995     if (!g_ZWRAP_useZSTDcompression)
996         return deflatePrime(strm, bits, value);
997     return ZWRAPC_finishWithErrorMsg(strm, "deflatePrime is not supported!");
998 }
999 
1000 
z_deflateSetHeader(z_streamp strm,gz_headerp head)1001 ZEXTERN int ZEXPORT z_deflateSetHeader OF((z_streamp strm,
1002                                          gz_headerp head))
1003 {
1004     if (!g_ZWRAP_useZSTDcompression)
1005         return deflateSetHeader(strm, head);
1006     return ZWRAPC_finishWithErrorMsg(strm, "deflateSetHeader is not supported!");
1007 }
1008 
1009 
1010 
1011 
1012 /* Advanced decompression functions */
1013 #if ZLIB_VERNUM >= 0x1280
z_inflateGetDictionary(z_streamp strm,Bytef * dictionary,uInt * dictLength)1014 ZEXTERN int ZEXPORT z_inflateGetDictionary OF((z_streamp strm,
1015                                              Bytef *dictionary,
1016                                              uInt  *dictLength))
1017 {
1018     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1019         return inflateGetDictionary(strm, dictionary, dictLength);
1020     return ZWRAPD_finishWithErrorMsg(strm, "inflateGetDictionary is not supported!");
1021 }
1022 #endif
1023 
1024 
z_inflateCopy(z_streamp dest,z_streamp source)1025 ZEXTERN int ZEXPORT z_inflateCopy OF((z_streamp dest,
1026                                     z_streamp source))
1027 {
1028     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !source->reserved)
1029         return inflateCopy(dest, source);
1030     return ZWRAPD_finishWithErrorMsg(source, "inflateCopy is not supported!");
1031 }
1032 
1033 
1034 #if ZLIB_VERNUM >= 0x1240
z_inflateMark(z_streamp strm)1035 ZEXTERN long ZEXPORT z_inflateMark OF((z_streamp strm))
1036 {
1037     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1038         return inflateMark(strm);
1039     return ZWRAPD_finishWithErrorMsg(strm, "inflateMark is not supported!");
1040 }
1041 #endif
1042 
1043 
z_inflatePrime(z_streamp strm,int bits,int value)1044 ZEXTERN int ZEXPORT z_inflatePrime OF((z_streamp strm,
1045                                      int bits,
1046                                      int value))
1047 {
1048     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1049         return inflatePrime(strm, bits, value);
1050     return ZWRAPD_finishWithErrorMsg(strm, "inflatePrime is not supported!");
1051 }
1052 
1053 
z_inflateGetHeader(z_streamp strm,gz_headerp head)1054 ZEXTERN int ZEXPORT z_inflateGetHeader OF((z_streamp strm,
1055                                          gz_headerp head))
1056 {
1057     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1058         return inflateGetHeader(strm, head);
1059     return ZWRAPD_finishWithErrorMsg(strm, "inflateGetHeader is not supported!");
1060 }
1061 
1062 
z_inflateBackInit_(z_streamp strm,int windowBits,unsigned char FAR * window,const char * version,int stream_size)1063 ZEXTERN int ZEXPORT z_inflateBackInit_ OF((z_streamp strm, int windowBits,
1064                                          unsigned char FAR *window,
1065                                          const char *version,
1066                                          int stream_size))
1067 {
1068     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1069         return inflateBackInit_(strm, windowBits, window, version, stream_size);
1070     return ZWRAPD_finishWithErrorMsg(strm, "inflateBackInit is not supported!");
1071 }
1072 
1073 
z_inflateBack(z_streamp strm,in_func in,void FAR * in_desc,out_func out,void FAR * out_desc)1074 ZEXTERN int ZEXPORT z_inflateBack OF((z_streamp strm,
1075                                     in_func in, void FAR *in_desc,
1076                                     out_func out, void FAR *out_desc))
1077 {
1078     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1079         return inflateBack(strm, in, in_desc, out, out_desc);
1080     return ZWRAPD_finishWithErrorMsg(strm, "inflateBack is not supported!");
1081 }
1082 
1083 
z_inflateBackEnd(z_streamp strm)1084 ZEXTERN int ZEXPORT z_inflateBackEnd OF((z_streamp strm))
1085 {
1086     if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
1087         return inflateBackEnd(strm);
1088     return ZWRAPD_finishWithErrorMsg(strm, "inflateBackEnd is not supported!");
1089 }
1090 
1091 
z_zlibCompileFlags(void)1092 ZEXTERN uLong ZEXPORT z_zlibCompileFlags OF((void)) { return zlibCompileFlags(); }
1093 
1094 
1095 
1096                     /* ===   utility functions  === */
1097 #ifndef Z_SOLO
1098 
z_compress(Bytef * dest,uLongf * destLen,const Bytef * source,uLong sourceLen)1099 ZEXTERN int ZEXPORT z_compress OF((Bytef *dest,   uLongf *destLen,
1100                                  const Bytef *source, uLong sourceLen))
1101 {
1102     if (!g_ZWRAP_useZSTDcompression)
1103         return compress(dest, destLen, source, sourceLen);
1104 
1105     {   size_t dstCapacity = *destLen;
1106         size_t const cSize = ZSTD_compress(dest, dstCapacity,
1107                                            source, sourceLen,
1108                                            ZWRAP_DEFAULT_CLEVEL);
1109         LOG_WRAPPERD("z_compress sourceLen=%d dstCapacity=%d\n",
1110                     (int)sourceLen, (int)dstCapacity);
1111         if (ZSTD_isError(cSize)) return Z_STREAM_ERROR;
1112         *destLen = cSize;
1113     }
1114     return Z_OK;
1115 }
1116 
1117 
z_compress2(Bytef * dest,uLongf * destLen,const Bytef * source,uLong sourceLen,int level)1118 ZEXTERN int ZEXPORT z_compress2 OF((Bytef *dest,   uLongf *destLen,
1119                                   const Bytef *source, uLong sourceLen,
1120                                   int level))
1121 {
1122     if (!g_ZWRAP_useZSTDcompression)
1123         return compress2(dest, destLen, source, sourceLen, level);
1124 
1125     { size_t dstCapacity = *destLen;
1126       size_t const cSize = ZSTD_compress(dest, dstCapacity, source, sourceLen, level);
1127       if (ZSTD_isError(cSize)) return Z_STREAM_ERROR;
1128       *destLen = cSize;
1129     }
1130     return Z_OK;
1131 }
1132 
1133 
z_compressBound(uLong sourceLen)1134 ZEXTERN uLong ZEXPORT z_compressBound OF((uLong sourceLen))
1135 {
1136     if (!g_ZWRAP_useZSTDcompression)
1137         return compressBound(sourceLen);
1138 
1139     return ZSTD_compressBound(sourceLen);
1140 }
1141 
1142 
z_uncompress(Bytef * dest,uLongf * destLen,const Bytef * source,uLong sourceLen)1143 ZEXTERN int ZEXPORT z_uncompress OF((Bytef *dest,   uLongf *destLen,
1144                                    const Bytef *source, uLong sourceLen))
1145 {
1146     if (!ZSTD_isFrame(source, sourceLen))
1147         return uncompress(dest, destLen, source, sourceLen);
1148 
1149     { size_t dstCapacity = *destLen;
1150       size_t const dSize = ZSTD_decompress(dest, dstCapacity, source, sourceLen);
1151       if (ZSTD_isError(dSize)) return Z_STREAM_ERROR;
1152       *destLen = dSize;
1153      }
1154     return Z_OK;
1155 }
1156 
1157 #endif /* !Z_SOLO */
1158 
1159 
1160                         /* checksum functions */
1161 
z_adler32(uLong adler,const Bytef * buf,uInt len)1162 ZEXTERN uLong ZEXPORT z_adler32 OF((uLong adler, const Bytef *buf, uInt len))
1163 {
1164     return adler32(adler, buf, len);
1165 }
1166 
z_crc32(uLong crc,const Bytef * buf,uInt len)1167 ZEXTERN uLong ZEXPORT z_crc32   OF((uLong crc, const Bytef *buf, uInt len))
1168 {
1169     return crc32(crc, buf, len);
1170 }
1171 
1172 
1173 #if ZLIB_VERNUM >= 0x12B0
z_adler32_z(uLong adler,const Bytef * buf,z_size_t len)1174 ZEXTERN uLong ZEXPORT z_adler32_z OF((uLong adler, const Bytef *buf, z_size_t len))
1175 {
1176     return adler32_z(adler, buf, len);
1177 }
1178 
z_crc32_z(uLong crc,const Bytef * buf,z_size_t len)1179 ZEXTERN uLong ZEXPORT z_crc32_z OF((uLong crc, const Bytef *buf, z_size_t len))
1180 {
1181     return crc32_z(crc, buf, len);
1182 }
1183 #endif
1184 
1185 
1186 #if ZLIB_VERNUM >= 0x1270
z_get_crc_table(void)1187 ZEXTERN const z_crc_t FAR * ZEXPORT z_get_crc_table    OF((void))
1188 {
1189     return get_crc_table();
1190 }
1191 #endif
1192 
1193                         /* Error function */
z_zError(int err)1194 ZEXTERN const char * ZEXPORT z_zError OF((int err))
1195 {
1196     /* Just use zlib Error function */
1197     return zError(err);
1198 }
1199