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