2
0

zstd_zlibwrapper.c 42 KB

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