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