Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | fileio.c - File i/o handler |
| 3 | Copyright (C) Yann Collet 2013-2015 |
| 4 | |
| 5 | GPL v2 License |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License along |
| 18 | with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | |
| 21 | You can contact the author at : |
| 22 | - zstd source repository : https://github.com/Cyan4973/zstd |
| 23 | - Public forum : https://groups.google.com/forum/#!forum/lz4c |
| 24 | */ |
| 25 | /* |
| 26 | Note : this is stand-alone program. |
| 27 | It is not part of ZSTD compression library, it is a user program of ZSTD library. |
| 28 | The license of ZSTD library is BSD. |
| 29 | The license of this file is GPLv2. |
| 30 | */ |
| 31 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 32 | /* ************************************* |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 33 | * Tuning options |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 34 | ***************************************/ |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 35 | #ifndef ZSTD_LEGACY_SUPPORT |
| 36 | /**LEGACY_SUPPORT : |
| 37 | * decompressor can decode older formats (starting from Zstd 0.1+) */ |
| 38 | # define ZSTD_LEGACY_SUPPORT 1 |
| 39 | #endif // ZSTD_LEGACY_SUPPORT |
| 40 | |
| 41 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 42 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 43 | * Compiler Options |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 44 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 45 | /* Disable some Visual warning messages */ |
| 46 | #ifdef _MSC_VER |
| 47 | # define _CRT_SECURE_NO_WARNINGS |
| 48 | # define _CRT_SECURE_NO_DEPRECATE /* VS2005 */ |
| 49 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 50 | #endif |
| 51 | |
| 52 | #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) |
| 53 | |
| 54 | #define _FILE_OFFSET_BITS 64 /* Large file support on 32-bits unix */ |
| 55 | #define _POSIX_SOURCE 1 /* enable fileno() within <stdio.h> on unix */ |
| 56 | |
| 57 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 58 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 59 | * Includes |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 60 | ***************************************/ |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 61 | #include <stdio.h> /* fprintf, fopen, fread, _fileno, stdin, stdout */ |
| 62 | #include <stdlib.h> /* malloc, free */ |
| 63 | #include <string.h> /* strcmp, strlen */ |
| 64 | #include <time.h> /* clock */ |
| 65 | #include <errno.h> /* errno */ |
| 66 | #include "mem.h" |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 67 | #include "fileio.h" |
| 68 | #include "zstd_static.h" |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame^] | 69 | #include "zstdhc_static.h" |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 70 | |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 71 | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1) |
| 72 | # include "zstd_v01.h" /* legacy */ |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 73 | #endif |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 74 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 75 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 76 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 77 | * OS-specific Includes |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 78 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 79 | #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) |
| 80 | # include <fcntl.h> /* _O_BINARY */ |
| 81 | # include <io.h> /* _setmode, _isatty */ |
| 82 | # ifdef __MINGW32__ |
Yann Collet | e1e6f7d | 2015-01-25 15:50:24 +0100 | [diff] [blame] | 83 | /* int _fileno(FILE *stream); // seems no longer useful // MINGW somehow forgets to include this windows declaration into <stdio.h> */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 84 | # endif |
Yann Collet | b5e06dc | 2015-07-04 23:20:56 -0800 | [diff] [blame] | 85 | # define SET_BINARY_MODE(file) { int unused = _setmode(_fileno(file), _O_BINARY); (void)unused; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 86 | # define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream)) |
| 87 | #else |
| 88 | # include <unistd.h> /* isatty */ |
| 89 | # define SET_BINARY_MODE(file) |
| 90 | # define IS_CONSOLE(stdStream) isatty(fileno(stdStream)) |
| 91 | #endif |
| 92 | |
| 93 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 94 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 95 | * Constants |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 96 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 97 | #define KB *(1U<<10) |
| 98 | #define MB *(1U<<20) |
| 99 | #define GB *(1U<<30) |
| 100 | |
| 101 | #define _1BIT 0x01 |
| 102 | #define _2BITS 0x03 |
| 103 | #define _3BITS 0x07 |
| 104 | #define _4BITS 0x0F |
| 105 | #define _6BITS 0x3F |
| 106 | #define _8BITS 0xFF |
| 107 | |
| 108 | #define BIT6 0x40 |
| 109 | #define BIT7 0x80 |
| 110 | |
Yann Collet | aacace3 | 2015-02-01 11:57:30 +0100 | [diff] [blame] | 111 | //static const unsigned FIO_maxBlockSizeID = 0xB; /* => 2MB block */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 112 | static const unsigned FIO_blockHeaderSize = 3; |
| 113 | |
| 114 | #define FIO_FRAMEHEADERSIZE 5 /* as a define, because needed to allocated table on stack */ |
| 115 | #define FSE_CHECKSUM_SEED 0 |
| 116 | |
| 117 | #define CACHELINE 64 |
| 118 | |
| 119 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 120 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 121 | * Macros |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 122 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 123 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 124 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 125 | static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */ |
| 126 | |
| 127 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
| 128 | if ((FIO_GetMilliSpan(g_time) > refreshRate) || (g_displayLevel>=4)) \ |
| 129 | { g_time = clock(); DISPLAY(__VA_ARGS__); \ |
| 130 | if (g_displayLevel>=4) fflush(stdout); } } |
| 131 | static const unsigned refreshRate = 150; |
| 132 | static clock_t g_time = 0; |
| 133 | |
| 134 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 135 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 136 | * Local Parameters |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 137 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 138 | static U32 g_overwrite = 0; |
| 139 | |
| 140 | void FIO_overwriteMode(void) { g_overwrite=1; } |
| 141 | void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; } |
| 142 | |
| 143 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 144 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 145 | * Exceptions |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 146 | ***************************************/ |
| 147 | #ifndef DEBUG |
| 148 | # define DEBUG 0 |
| 149 | #endif |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 150 | #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); |
| 151 | #define EXM_THROW(error, ...) \ |
| 152 | { \ |
| 153 | DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ |
| 154 | DISPLAYLEVEL(1, "Error %i : ", error); \ |
| 155 | DISPLAYLEVEL(1, __VA_ARGS__); \ |
| 156 | DISPLAYLEVEL(1, "\n"); \ |
| 157 | exit(error); \ |
| 158 | } |
| 159 | |
| 160 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 161 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 162 | * Functions |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 163 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 164 | static unsigned FIO_GetMilliSpan(clock_t nPrevious) |
| 165 | { |
| 166 | clock_t nCurrent = clock(); |
| 167 | unsigned nSpan = (unsigned)(((nCurrent - nPrevious) * 1000) / CLOCKS_PER_SEC); |
| 168 | return nSpan; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | static void FIO_getFileHandles(FILE** pfinput, FILE** pfoutput, const char* input_filename, const char* output_filename) |
| 173 | { |
| 174 | if (!strcmp (input_filename, stdinmark)) |
| 175 | { |
| 176 | DISPLAYLEVEL(4,"Using stdin for input\n"); |
| 177 | *pfinput = stdin; |
| 178 | SET_BINARY_MODE(stdin); |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | *pfinput = fopen(input_filename, "rb"); |
| 183 | } |
| 184 | |
| 185 | if (!strcmp (output_filename, stdoutmark)) |
| 186 | { |
| 187 | DISPLAYLEVEL(4,"Using stdout for output\n"); |
| 188 | *pfoutput = stdout; |
| 189 | SET_BINARY_MODE(stdout); |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | /* Check if destination file already exists */ |
| 194 | *pfoutput=0; |
| 195 | if (strcmp(output_filename,nulmark)) *pfoutput = fopen( output_filename, "rb" ); |
| 196 | if (*pfoutput!=0) |
| 197 | { |
| 198 | fclose(*pfoutput); |
| 199 | if (!g_overwrite) |
| 200 | { |
| 201 | char ch; |
| 202 | if (g_displayLevel <= 1) /* No interaction possible */ |
| 203 | EXM_THROW(11, "Operation aborted : %s already exists", output_filename); |
| 204 | DISPLAYLEVEL(2, "Warning : %s already exists\n", output_filename); |
| 205 | DISPLAYLEVEL(2, "Overwrite ? (Y/N) : "); |
| 206 | ch = (char)getchar(); |
| 207 | if ((ch!='Y') && (ch!='y')) EXM_THROW(11, "Operation aborted : %s already exists", output_filename); |
| 208 | } |
| 209 | } |
| 210 | *pfoutput = fopen( output_filename, "wb" ); |
| 211 | } |
| 212 | |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 213 | if ( *pfinput==0 ) EXM_THROW(12, "Pb opening src : %s", input_filename); |
| 214 | if ( *pfoutput==0) EXM_THROW(13, "Pb opening dst : %s", output_filename); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 215 | } |
| 216 | |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame^] | 217 | typedef void* (*FIO_createC) (void); |
| 218 | static void* local_ZSTD_createCCtx(void) { return (void*) ZSTD_createCCtx(); } |
| 219 | static void* local_ZSTD_HC_createCCtx(void) { return (void*) ZSTD_HC_createCCtx(); } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 220 | |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame^] | 221 | typedef size_t (*FIO_initC) (void* ctx, void* dst, size_t maxDstSize, int cLevel); |
| 222 | static size_t local_ZSTD_compressBegin (void* ctx, void* dst, size_t maxDstSize, int cLevel) |
| 223 | { |
| 224 | (void)cLevel; |
| 225 | return ZSTD_compressBegin((ZSTD_CCtx*)ctx, dst, maxDstSize); |
| 226 | } |
| 227 | static size_t local_ZSTD_HC_compressBegin (void* ctx, void* dst, size_t maxDstSize, int cLevel) |
| 228 | { |
| 229 | return ZSTD_HC_compressBegin((ZSTD_HC_CCtx*)ctx, dst, maxDstSize, cLevel); |
| 230 | } |
| 231 | |
| 232 | typedef size_t (*FIO_continueC) (void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize); |
| 233 | static size_t local_ZSTD_compressContinue (void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
| 234 | { |
| 235 | return ZSTD_compressContinue((ZSTD_CCtx*)ctx, dst, maxDstSize, src, srcSize); |
| 236 | } |
| 237 | static size_t local_ZSTD_HC_compressContinue (void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
| 238 | { |
| 239 | return ZSTD_HC_compressContinue((ZSTD_HC_CCtx*)ctx, dst, maxDstSize, src, srcSize); |
| 240 | } |
| 241 | |
| 242 | typedef size_t (*FIO_endC) (void* ctx, void* dst, size_t maxDstSize); |
| 243 | static size_t local_ZSTD_compressEnd (void* ctx, void* dst, size_t maxDstSize) |
| 244 | { |
| 245 | return ZSTD_compressEnd((ZSTD_CCtx*)ctx, dst, maxDstSize); |
| 246 | } |
| 247 | static size_t local_ZSTD_HC_compressEnd (void* ctx, void* dst, size_t maxDstSize) |
| 248 | { |
| 249 | return ZSTD_HC_compressEnd((ZSTD_HC_CCtx*)ctx, dst, maxDstSize); |
| 250 | } |
| 251 | |
| 252 | typedef void (*FIO_freeC) (void* ctx); |
| 253 | static void local_ZSTD_freeCCtx(void* ctx) { ZSTD_freeCCtx((ZSTD_CCtx*)ctx); } |
| 254 | static void local_ZSTD_HC_freeCCtx(void* ctx) { ZSTD_HC_freeCCtx((ZSTD_HC_CCtx*)ctx); } |
| 255 | |
| 256 | |
| 257 | unsigned long long FIO_compressFilename(const char* output_filename, const char* input_filename, int cLevel) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 258 | { |
| 259 | U64 filesize = 0; |
| 260 | U64 compressedfilesize = 0; |
| 261 | BYTE* inBuff; |
| 262 | BYTE* inSlot; |
| 263 | BYTE* inEnd; |
| 264 | BYTE* outBuff; |
| 265 | size_t blockSize = 128 KB; |
| 266 | size_t inBuffSize = 4 * blockSize; |
| 267 | size_t outBuffSize = ZSTD_compressBound(blockSize); |
| 268 | FILE* finput; |
| 269 | FILE* foutput; |
| 270 | size_t sizeCheck, cSize; |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame^] | 271 | void* ctx; |
| 272 | FIO_createC createC=NULL; |
| 273 | FIO_initC initC=NULL; |
| 274 | FIO_continueC continueC = NULL; |
| 275 | FIO_endC endC = NULL; |
| 276 | FIO_freeC freeC = NULL; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 277 | |
| 278 | /* Init */ |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame^] | 279 | if (cLevel <= 1) |
| 280 | { |
| 281 | createC = local_ZSTD_createCCtx; |
| 282 | initC = local_ZSTD_compressBegin; |
| 283 | continueC = local_ZSTD_compressContinue; |
| 284 | endC = local_ZSTD_compressEnd; |
| 285 | freeC = local_ZSTD_freeCCtx; |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | createC = local_ZSTD_HC_createCCtx; |
| 290 | initC = local_ZSTD_HC_compressBegin; |
| 291 | continueC = local_ZSTD_HC_compressContinue; |
| 292 | endC = local_ZSTD_HC_compressEnd; |
| 293 | freeC = local_ZSTD_HC_freeCCtx; |
| 294 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 295 | FIO_getFileHandles(&finput, &foutput, input_filename, output_filename); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 296 | |
| 297 | /* Allocate Memory */ |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame^] | 298 | ctx = createC(); |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 299 | inBuff = (BYTE*)malloc(inBuffSize); |
| 300 | outBuff = (BYTE*)malloc(outBuffSize); |
Yann Collet | 353c5d2 | 2015-10-21 14:39:26 +0100 | [diff] [blame] | 301 | if (!inBuff || !outBuff || !ctx) EXM_THROW(21, "Allocation error : not enough memory"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 302 | inSlot = inBuff; |
| 303 | inEnd = inBuff + inBuffSize; |
| 304 | |
| 305 | /* Write Frame Header */ |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame^] | 306 | cSize = initC(ctx, outBuff, outBuffSize, cLevel); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 307 | if (ZSTD_isError(cSize)) EXM_THROW(22, "Compression error : cannot create frame header"); |
| 308 | |
| 309 | sizeCheck = fwrite(outBuff, 1, cSize, foutput); |
Yann Collet | 61d08c5 | 2015-08-25 18:13:32 +0100 | [diff] [blame] | 310 | if (sizeCheck!=cSize) EXM_THROW(23, "Write error : cannot write header into %s", output_filename); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 311 | compressedfilesize += cSize; |
| 312 | |
| 313 | /* Main compression loop */ |
| 314 | while (1) |
| 315 | { |
| 316 | size_t inSize; |
| 317 | |
| 318 | /* Fill input Buffer */ |
| 319 | if (inSlot + blockSize > inEnd) inSlot = inBuff; |
| 320 | inSize = fread(inSlot, (size_t)1, blockSize, finput); |
| 321 | if (inSize==0) break; |
| 322 | filesize += inSize; |
| 323 | DISPLAYUPDATE(2, "\rRead : %u MB ", (U32)(filesize>>20)); |
| 324 | |
| 325 | /* Compress Block */ |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame^] | 326 | cSize = continueC(ctx, outBuff, outBuffSize, inSlot, inSize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 327 | if (ZSTD_isError(cSize)) |
| 328 | EXM_THROW(24, "Compression error : %s ", ZSTD_getErrorName(cSize)); |
| 329 | |
| 330 | /* Write cBlock */ |
| 331 | sizeCheck = fwrite(outBuff, 1, cSize, foutput); |
Yann Collet | 61d08c5 | 2015-08-25 18:13:32 +0100 | [diff] [blame] | 332 | if (sizeCheck!=cSize) EXM_THROW(25, "Write error : cannot write compressed block into %s", output_filename); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 333 | compressedfilesize += cSize; |
| 334 | inSlot += inSize; |
| 335 | |
| 336 | DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", (U32)(filesize>>20), (double)compressedfilesize/filesize*100); |
| 337 | } |
| 338 | |
| 339 | /* End of Frame */ |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame^] | 340 | cSize = endC(ctx, outBuff, outBuffSize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 341 | if (ZSTD_isError(cSize)) EXM_THROW(26, "Compression error : cannot create frame end"); |
| 342 | |
| 343 | sizeCheck = fwrite(outBuff, 1, cSize, foutput); |
Yann Collet | 61d08c5 | 2015-08-25 18:13:32 +0100 | [diff] [blame] | 344 | if (sizeCheck!=cSize) EXM_THROW(27, "Write error : cannot write frame end into %s", output_filename); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 345 | compressedfilesize += cSize; |
| 346 | |
| 347 | /* Status */ |
| 348 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
| 349 | DISPLAYLEVEL(2,"Compressed %llu bytes into %llu bytes ==> %.2f%%\n", |
| 350 | (unsigned long long) filesize, (unsigned long long) compressedfilesize, (double)compressedfilesize/filesize*100); |
| 351 | |
| 352 | /* clean */ |
| 353 | free(inBuff); |
| 354 | free(outBuff); |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame^] | 355 | freeC(ctx); |
Yann Collet | 5b14760 | 2015-08-25 17:41:46 +0100 | [diff] [blame] | 356 | fclose(finput); |
Yann Collet | 61d08c5 | 2015-08-25 18:13:32 +0100 | [diff] [blame] | 357 | if (fclose(foutput)) EXM_THROW(28, "Write error : cannot properly close %s", output_filename); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 358 | |
| 359 | return compressedfilesize; |
| 360 | } |
| 361 | |
| 362 | |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 363 | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1) |
| 364 | |
| 365 | unsigned long long FIOv01_decompressFrame(FILE* foutput, FILE* finput) |
| 366 | { |
| 367 | size_t outBuffSize = 512 KB; |
| 368 | BYTE* outBuff = (BYTE*)malloc(outBuffSize); |
| 369 | size_t inBuffSize = 128 KB + 8; |
| 370 | BYTE inBuff[128 KB + 8]; |
| 371 | BYTE* op = outBuff; |
| 372 | BYTE* const oend = outBuff + outBuffSize; |
| 373 | U64 filesize = 0; |
| 374 | size_t toRead; |
| 375 | size_t sizeCheck; |
| 376 | ZSTDv01_Dctx* dctx = ZSTDv01_createDCtx(); |
| 377 | |
| 378 | |
| 379 | /* init */ |
| 380 | if (outBuff==NULL) EXM_THROW(41, "Error : not enough memory to decode legacy frame"); |
| 381 | |
| 382 | /* restore header, already read from input */ |
| 383 | MEM_writeLE32(inBuff, ZSTDv01_magicNumberLE); |
| 384 | sizeCheck = ZSTDv01_decompressContinue(dctx, NULL, 0, inBuff, sizeof(ZSTDv01_magicNumberLE)); /* Decode frame header */ |
| 385 | if (ZSTDv01_isError(sizeCheck)) EXM_THROW(42, "Error decoding legacy header"); |
| 386 | |
| 387 | /* Main decompression Loop */ |
| 388 | toRead = ZSTDv01_nextSrcSizeToDecompress(dctx); |
| 389 | while (toRead) |
| 390 | { |
| 391 | size_t readSize, decodedSize; |
| 392 | |
| 393 | /* Fill input buffer */ |
| 394 | if (toRead > inBuffSize) |
| 395 | EXM_THROW(43, "too large block"); |
| 396 | readSize = fread(inBuff, 1, toRead, finput); |
| 397 | if (readSize != toRead) |
| 398 | EXM_THROW(44, "Read error"); |
| 399 | |
| 400 | /* Decode block */ |
| 401 | decodedSize = ZSTDv01_decompressContinue(dctx, op, oend-op, inBuff, readSize); |
| 402 | if (ZSTDv01_isError(decodedSize)) EXM_THROW(45, "Decoding error : input corrupted"); |
| 403 | |
| 404 | if (decodedSize) /* not a header */ |
| 405 | { |
| 406 | /* Write block */ |
| 407 | sizeCheck = fwrite(op, 1, decodedSize, foutput); |
| 408 | if (sizeCheck != decodedSize) EXM_THROW(46, "Write error : unable to write data block to destination file"); |
| 409 | filesize += decodedSize; |
| 410 | op += decodedSize; |
| 411 | if (op==oend) op = outBuff; |
| 412 | DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(filesize>>20) ); |
| 413 | } |
| 414 | |
| 415 | /* prepare for next Block */ |
| 416 | toRead = ZSTDv01_nextSrcSizeToDecompress(dctx); |
| 417 | } |
| 418 | |
| 419 | /* release resources */ |
| 420 | free(outBuff); |
| 421 | free(dctx); |
| 422 | return filesize; |
| 423 | } |
| 424 | #endif /* ZSTD_LEGACY_SUPPORT */ |
| 425 | |
| 426 | |
Yann Collet | be50aaa | 2015-09-10 23:26:09 +0100 | [diff] [blame] | 427 | unsigned long long FIO_decompressFrame(FILE* foutput, FILE* finput, |
| 428 | BYTE* inBuff, size_t inBuffSize, |
| 429 | BYTE* outBuff, size_t outBuffSize, |
Yann Collet | 353c5d2 | 2015-10-21 14:39:26 +0100 | [diff] [blame] | 430 | ZSTD_DCtx* dctx) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 431 | { |
Yann Collet | be50aaa | 2015-09-10 23:26:09 +0100 | [diff] [blame] | 432 | BYTE* op = outBuff; |
| 433 | BYTE* const oend = outBuff + outBuffSize; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 434 | U64 filesize = 0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 435 | size_t toRead; |
| 436 | size_t sizeCheck; |
| 437 | |
| 438 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 439 | /* Main decompression Loop */ |
Yann Collet | c5d46b5 | 2015-02-16 18:06:26 +0100 | [diff] [blame] | 440 | toRead = ZSTD_nextSrcSizeToDecompress(dctx); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 441 | while (toRead) |
| 442 | { |
| 443 | size_t readSize, decodedSize; |
| 444 | |
| 445 | /* Fill input buffer */ |
Yann Collet | 1885029 | 2015-08-24 20:17:11 +0100 | [diff] [blame] | 446 | if (toRead > inBuffSize) |
| 447 | EXM_THROW(34, "too large block"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 448 | readSize = fread(inBuff, 1, toRead, finput); |
| 449 | if (readSize != toRead) |
Yann Collet | 1885029 | 2015-08-24 20:17:11 +0100 | [diff] [blame] | 450 | EXM_THROW(35, "Read error"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 451 | |
| 452 | /* Decode block */ |
| 453 | decodedSize = ZSTD_decompressContinue(dctx, op, oend-op, inBuff, readSize); |
Yann Collet | 1885029 | 2015-08-24 20:17:11 +0100 | [diff] [blame] | 454 | if (ZSTD_isError(decodedSize)) EXM_THROW(36, "Decoding error : input corrupted"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 455 | |
Yann Collet | c5d46b5 | 2015-02-16 18:06:26 +0100 | [diff] [blame] | 456 | if (decodedSize) /* not a header */ |
| 457 | { |
| 458 | /* Write block */ |
| 459 | sizeCheck = fwrite(op, 1, decodedSize, foutput); |
Yann Collet | 1885029 | 2015-08-24 20:17:11 +0100 | [diff] [blame] | 460 | if (sizeCheck != decodedSize) EXM_THROW(37, "Write error : unable to write data block to destination file"); |
Yann Collet | c5d46b5 | 2015-02-16 18:06:26 +0100 | [diff] [blame] | 461 | filesize += decodedSize; |
| 462 | op += decodedSize; |
| 463 | if (op==oend) op = outBuff; |
| 464 | DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(filesize>>20) ); |
| 465 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 466 | |
| 467 | /* prepare for next Block */ |
Yann Collet | c5d46b5 | 2015-02-16 18:06:26 +0100 | [diff] [blame] | 468 | toRead = ZSTD_nextSrcSizeToDecompress(dctx); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 469 | } |
| 470 | |
Yann Collet | be50aaa | 2015-09-10 23:26:09 +0100 | [diff] [blame] | 471 | return filesize; |
| 472 | } |
| 473 | |
| 474 | |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 475 | #define MAXHEADERSIZE (FIO_FRAMEHEADERSIZE+3) |
| 476 | unsigned long long FIO_decompressFilename(const char* output_filename, const char* input_filename) |
| 477 | { |
| 478 | FILE* finput, *foutput; |
| 479 | BYTE* inBuff=NULL; |
| 480 | size_t inBuffSize = 0; |
| 481 | BYTE* outBuff=NULL; |
| 482 | size_t outBuffSize = 0; |
| 483 | U32 blockSize = 128 KB; |
| 484 | U32 wNbBlocks = 4; |
| 485 | U64 filesize = 0; |
| 486 | BYTE* header[MAXHEADERSIZE]; |
| 487 | size_t toRead; |
| 488 | size_t sizeCheck; |
| 489 | |
| 490 | |
| 491 | /* Init */ |
Yann Collet | 353c5d2 | 2015-10-21 14:39:26 +0100 | [diff] [blame] | 492 | ZSTD_DCtx* dctx = ZSTD_createDCtx(); |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 493 | FIO_getFileHandles(&finput, &foutput, input_filename, output_filename); |
| 494 | |
| 495 | /* for each frame */ |
| 496 | for ( ; ; ) |
| 497 | { |
| 498 | /* check magic number -> version */ |
| 499 | U32 magicNumber; |
| 500 | toRead = sizeof(ZSTD_magicNumber);; |
| 501 | sizeCheck = fread(header, (size_t)1, toRead, finput); |
| 502 | if (sizeCheck==0) break; /* no more input */ |
| 503 | if (sizeCheck != toRead) EXM_THROW(31, "Read error : cannot read header"); |
| 504 | |
| 505 | magicNumber = MEM_readLE32(header); |
| 506 | switch(magicNumber) |
| 507 | { |
| 508 | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1) |
| 509 | case ZSTDv01_magicNumberLE: |
| 510 | filesize += FIOv01_decompressFrame(foutput, finput); |
| 511 | continue; |
| 512 | #endif /* ZSTD_LEGACY_SUPPORT */ |
| 513 | case ZSTD_magicNumber: |
| 514 | break; /* normal case */ |
| 515 | default : |
| 516 | EXM_THROW(32, "Error : unknown frame prefix"); |
| 517 | } |
| 518 | |
| 519 | /* prepare frame decompression, by completing header */ |
| 520 | ZSTD_resetDCtx(dctx); |
| 521 | toRead = ZSTD_nextSrcSizeToDecompress(dctx) - sizeof(ZSTD_magicNumber); |
| 522 | if (toRead > MAXHEADERSIZE) EXM_THROW(30, "Not enough memory to read header"); |
| 523 | sizeCheck = fread(header+sizeof(ZSTD_magicNumber), (size_t)1, toRead, finput); |
| 524 | if (sizeCheck != toRead) EXM_THROW(31, "Read error : cannot read header"); |
| 525 | sizeCheck = ZSTD_decompressContinue(dctx, NULL, 0, header, sizeof(ZSTD_magicNumber)+toRead); // Decode frame header |
| 526 | if (ZSTD_isError(sizeCheck)) EXM_THROW(32, "Error decoding header"); |
| 527 | |
| 528 | /* Here later : blockSize determination */ |
| 529 | |
| 530 | /* Allocate Memory (if needed) */ |
| 531 | { |
| 532 | size_t newInBuffSize = blockSize + FIO_blockHeaderSize; |
| 533 | size_t newOutBuffSize = wNbBlocks * blockSize; |
| 534 | if (newInBuffSize > inBuffSize) |
| 535 | { |
| 536 | free(inBuff); |
| 537 | inBuffSize = newInBuffSize; |
| 538 | inBuff = (BYTE*)malloc(inBuffSize); |
| 539 | } |
| 540 | if (newOutBuffSize > outBuffSize) |
| 541 | { |
| 542 | free(outBuff); |
| 543 | outBuffSize = newOutBuffSize; |
| 544 | outBuff = (BYTE*)malloc(outBuffSize); |
| 545 | } |
| 546 | } |
| 547 | if (!inBuff || !outBuff) EXM_THROW(33, "Allocation error : not enough memory"); |
| 548 | |
| 549 | filesize += FIO_decompressFrame(foutput, finput, inBuff, inBuffSize, outBuff, outBuffSize, dctx); |
| 550 | } |
| 551 | |
| 552 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
| 553 | DISPLAYLEVEL(2, "Decoded %llu bytes \n", (long long unsigned)filesize); |
| 554 | |
| 555 | /* clean */ |
| 556 | free(inBuff); |
| 557 | free(outBuff); |
| 558 | ZSTD_freeDCtx(dctx); |
| 559 | fclose(finput); |
| 560 | if (fclose(foutput)) EXM_THROW(38, "Write error : cannot properly close %s", output_filename); |
| 561 | |
| 562 | return filesize; |
| 563 | } |
| 564 | |
| 565 | |
| 566 | #if 0 |
Yann Collet | be50aaa | 2015-09-10 23:26:09 +0100 | [diff] [blame] | 567 | unsigned long long FIO_decompressFilename(const char* output_filename, const char* input_filename) |
| 568 | { |
| 569 | FILE* finput, *foutput; |
| 570 | BYTE* inBuff=NULL; |
| 571 | size_t inBuffSize = 0; |
| 572 | BYTE* outBuff=NULL; |
| 573 | size_t outBuffSize = 0; |
| 574 | U32 blockSize = 128 KB; |
| 575 | U32 wNbBlocks = 4; |
| 576 | U64 filesize = 0; |
| 577 | BYTE* header[MAXHEADERSIZE]; |
| 578 | ZSTD_Dctx* dctx; |
| 579 | size_t toRead; |
| 580 | size_t sizeCheck; |
| 581 | |
| 582 | |
| 583 | /* Init */ |
| 584 | FIO_getFileHandles(&finput, &foutput, input_filename, output_filename); |
| 585 | dctx = ZSTD_createDCtx(); |
| 586 | |
| 587 | /* for each frame */ |
| 588 | for ( ; ; ) |
| 589 | { |
| 590 | /* check header */ |
| 591 | ZSTD_resetDCtx(dctx); |
| 592 | toRead = ZSTD_nextSrcSizeToDecompress(dctx); |
| 593 | if (toRead > MAXHEADERSIZE) EXM_THROW(30, "Not enough memory to read header"); |
| 594 | sizeCheck = fread(header, (size_t)1, toRead, finput); |
| 595 | if (sizeCheck==0) break; /* no more input */ |
| 596 | if (sizeCheck != toRead) EXM_THROW(31, "Read error : cannot read header"); |
| 597 | sizeCheck = ZSTD_decompressContinue(dctx, NULL, 0, header, toRead); // Decode frame header |
| 598 | if (ZSTD_isError(sizeCheck)) EXM_THROW(32, "Error decoding header"); |
| 599 | |
| 600 | /* Here later : blockSize determination */ |
| 601 | |
| 602 | /* Allocate Memory (if needed) */ |
| 603 | { |
| 604 | size_t newInBuffSize = blockSize + FIO_blockHeaderSize; |
| 605 | size_t newOutBuffSize = wNbBlocks * blockSize; |
| 606 | if (newInBuffSize > inBuffSize) |
| 607 | { |
| 608 | free(inBuff); |
| 609 | inBuffSize = newInBuffSize; |
| 610 | inBuff = (BYTE*)malloc(inBuffSize); |
| 611 | } |
| 612 | if (newOutBuffSize > outBuffSize) |
| 613 | { |
| 614 | free(outBuff); |
| 615 | outBuffSize = newOutBuffSize; |
| 616 | outBuff = (BYTE*)malloc(outBuffSize); |
| 617 | } |
| 618 | } |
| 619 | if (!inBuff || !outBuff) EXM_THROW(33, "Allocation error : not enough memory"); |
| 620 | |
| 621 | filesize += FIO_decompressFrame(foutput, finput, inBuff, inBuffSize, outBuff, outBuffSize, dctx); |
| 622 | } |
| 623 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 624 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 625 | DISPLAYLEVEL(2, "Decoded %llu bytes \n", (long long unsigned)filesize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 626 | |
| 627 | /* clean */ |
| 628 | free(inBuff); |
| 629 | free(outBuff); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 630 | ZSTD_freeDCtx(dctx); |
Yann Collet | 5abd820 | 2015-08-27 03:16:04 +0100 | [diff] [blame] | 631 | fclose(finput); |
| 632 | if (fclose(foutput)) EXM_THROW(38, "Write error : cannot properly close %s", output_filename); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 633 | |
| 634 | return filesize; |
| 635 | } |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 636 | #endif |