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