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