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 |
Yann Collet | 9f43292 | 2015-11-09 17:42:17 +0100 | [diff] [blame] | 39 | #endif |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 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 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 52 | #define _FILE_OFFSET_BITS 64 /* Large file support on 32-bits unix */ |
| 53 | #define _POSIX_SOURCE 1 /* enable fileno() within <stdio.h> on unix */ |
| 54 | |
| 55 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 56 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 57 | * Includes |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 58 | ***************************************/ |
Yann Collet | 9f43292 | 2015-11-09 17:42:17 +0100 | [diff] [blame] | 59 | #include <stdio.h> /* fprintf, fopen, fread, _fileno, stdin, stdout */ |
| 60 | #include <stdlib.h> /* malloc, free */ |
| 61 | #include <string.h> /* strcmp, strlen */ |
| 62 | #include <time.h> /* clock */ |
| 63 | #include <errno.h> /* errno */ |
| 64 | #include <sys/types.h> /* stat64 */ |
| 65 | #include <sys/stat.h> /* stat64 */ |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 66 | #include "mem.h" |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 67 | #include "fileio.h" |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 68 | #include "zstd_static.h" /* ZSTD_magicNumber */ |
| 69 | #include "zstd_buffered_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) |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 72 | # include "zstd_legacy.h" /* legacy */ |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 73 | # include "fileio_legacy.h" /* legacy */ |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 74 | #endif |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 75 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 76 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 77 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 78 | * OS-specific Includes |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 79 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 80 | #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) |
| 81 | # include <fcntl.h> /* _O_BINARY */ |
| 82 | # include <io.h> /* _setmode, _isatty */ |
| 83 | # ifdef __MINGW32__ |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 84 | // 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] | 85 | # endif |
Yann Collet | b5e06dc | 2015-07-04 23:20:56 -0800 | [diff] [blame] | 86 | # 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] | 87 | # define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream)) |
| 88 | #else |
| 89 | # include <unistd.h> /* isatty */ |
| 90 | # define SET_BINARY_MODE(file) |
| 91 | # define IS_CONSOLE(stdStream) isatty(fileno(stdStream)) |
| 92 | #endif |
| 93 | |
Yann Collet | b7d6e8f | 2015-11-09 18:20:39 +0100 | [diff] [blame] | 94 | #if !defined(S_ISREG) |
| 95 | # define S_ISREG(x) (((x) & S_IFMT) == S_IFREG) |
| 96 | #endif |
| 97 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 98 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 99 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 100 | * Constants |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 101 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 102 | #define KB *(1U<<10) |
| 103 | #define MB *(1U<<20) |
| 104 | #define GB *(1U<<30) |
| 105 | |
| 106 | #define _1BIT 0x01 |
| 107 | #define _2BITS 0x03 |
| 108 | #define _3BITS 0x07 |
| 109 | #define _4BITS 0x0F |
| 110 | #define _6BITS 0x3F |
| 111 | #define _8BITS 0xFF |
| 112 | |
| 113 | #define BIT6 0x40 |
| 114 | #define BIT7 0x80 |
| 115 | |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 116 | #define BLOCKSIZE (128 KB) |
| 117 | #define ROLLBUFFERSIZE (BLOCKSIZE*8*64) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 118 | |
| 119 | #define FIO_FRAMEHEADERSIZE 5 /* as a define, because needed to allocated table on stack */ |
| 120 | #define FSE_CHECKSUM_SEED 0 |
| 121 | |
| 122 | #define CACHELINE 64 |
| 123 | |
| 124 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 125 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 126 | * Macros |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 127 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 128 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 129 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 130 | static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */ |
| 131 | |
| 132 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
| 133 | if ((FIO_GetMilliSpan(g_time) > refreshRate) || (g_displayLevel>=4)) \ |
| 134 | { g_time = clock(); DISPLAY(__VA_ARGS__); \ |
| 135 | if (g_displayLevel>=4) fflush(stdout); } } |
| 136 | static const unsigned refreshRate = 150; |
| 137 | static clock_t g_time = 0; |
| 138 | |
| 139 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 140 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 141 | * Local Parameters |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 142 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 143 | static U32 g_overwrite = 0; |
| 144 | |
| 145 | void FIO_overwriteMode(void) { g_overwrite=1; } |
| 146 | void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; } |
| 147 | |
| 148 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 149 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 150 | * Exceptions |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 151 | ***************************************/ |
| 152 | #ifndef DEBUG |
| 153 | # define DEBUG 0 |
| 154 | #endif |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 155 | #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); |
| 156 | #define EXM_THROW(error, ...) \ |
| 157 | { \ |
| 158 | DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ |
| 159 | DISPLAYLEVEL(1, "Error %i : ", error); \ |
| 160 | DISPLAYLEVEL(1, __VA_ARGS__); \ |
| 161 | DISPLAYLEVEL(1, "\n"); \ |
| 162 | exit(error); \ |
| 163 | } |
| 164 | |
| 165 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 166 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 167 | * Functions |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 168 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 169 | static unsigned FIO_GetMilliSpan(clock_t nPrevious) |
| 170 | { |
| 171 | clock_t nCurrent = clock(); |
| 172 | unsigned nSpan = (unsigned)(((nCurrent - nPrevious) * 1000) / CLOCKS_PER_SEC); |
| 173 | return nSpan; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | static void FIO_getFileHandles(FILE** pfinput, FILE** pfoutput, const char* input_filename, const char* output_filename) |
| 178 | { |
| 179 | if (!strcmp (input_filename, stdinmark)) |
| 180 | { |
| 181 | DISPLAYLEVEL(4,"Using stdin for input\n"); |
| 182 | *pfinput = stdin; |
| 183 | SET_BINARY_MODE(stdin); |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | *pfinput = fopen(input_filename, "rb"); |
| 188 | } |
| 189 | |
| 190 | if (!strcmp (output_filename, stdoutmark)) |
| 191 | { |
| 192 | DISPLAYLEVEL(4,"Using stdout for output\n"); |
| 193 | *pfoutput = stdout; |
| 194 | SET_BINARY_MODE(stdout); |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | /* Check if destination file already exists */ |
| 199 | *pfoutput=0; |
| 200 | if (strcmp(output_filename,nulmark)) *pfoutput = fopen( output_filename, "rb" ); |
| 201 | if (*pfoutput!=0) |
| 202 | { |
| 203 | fclose(*pfoutput); |
| 204 | if (!g_overwrite) |
| 205 | { |
| 206 | char ch; |
| 207 | if (g_displayLevel <= 1) /* No interaction possible */ |
| 208 | EXM_THROW(11, "Operation aborted : %s already exists", output_filename); |
| 209 | DISPLAYLEVEL(2, "Warning : %s already exists\n", output_filename); |
| 210 | DISPLAYLEVEL(2, "Overwrite ? (Y/N) : "); |
| 211 | ch = (char)getchar(); |
| 212 | if ((ch!='Y') && (ch!='y')) EXM_THROW(11, "Operation aborted : %s already exists", output_filename); |
| 213 | } |
| 214 | } |
| 215 | *pfoutput = fopen( output_filename, "wb" ); |
| 216 | } |
| 217 | |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 218 | if ( *pfinput==0 ) EXM_THROW(12, "Pb opening src : %s", input_filename); |
| 219 | if ( *pfoutput==0) EXM_THROW(13, "Pb opening dst : %s", output_filename); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 220 | } |
| 221 | |
Yann Collet | 9f43292 | 2015-11-09 17:42:17 +0100 | [diff] [blame] | 222 | |
| 223 | static U64 FIO_getFileSize(const char* infilename) |
| 224 | { |
| 225 | int r; |
| 226 | #if defined(_MSC_VER) |
| 227 | struct _stat64 statbuf; |
| 228 | r = _stat64(infilename, &statbuf); |
| 229 | #else |
| 230 | struct stat statbuf; |
| 231 | r = stat(infilename, &statbuf); |
| 232 | #endif |
| 233 | if (r || !S_ISREG(statbuf.st_mode)) return 0; |
| 234 | return (U64)statbuf.st_size; |
| 235 | } |
| 236 | |
| 237 | |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 238 | 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] | 239 | { |
| 240 | U64 filesize = 0; |
| 241 | U64 compressedfilesize = 0; |
| 242 | BYTE* inBuff; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 243 | BYTE* outBuff; |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 244 | size_t inBuffSize = ZBUFF_recommendedCInSize(); |
| 245 | size_t outBuffSize = ZBUFF_recommendedCOutSize(); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 246 | FILE* finput; |
| 247 | FILE* foutput; |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 248 | size_t sizeCheck, errorCode; |
| 249 | ZBUFF_CCtx* ctx; |
| 250 | |
| 251 | /* Allocate Memory */ |
| 252 | ctx = ZBUFF_createCCtx(); |
| 253 | inBuff = (BYTE*)malloc(inBuffSize); |
| 254 | outBuff = (BYTE*)malloc(outBuffSize); |
| 255 | if (!inBuff || !outBuff || !ctx) EXM_THROW(21, "Allocation error : not enough memory"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 256 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 257 | /* init */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 258 | FIO_getFileHandles(&finput, &foutput, input_filename, output_filename); |
Yann Collet | 9f43292 | 2015-11-09 17:42:17 +0100 | [diff] [blame] | 259 | filesize = FIO_getFileSize(input_filename); |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 260 | errorCode = ZBUFF_compressInit_advanced(ctx, ZSTD_getParams(cLevel, filesize)); |
| 261 | if (ZBUFF_isError(errorCode)) EXM_THROW(22, "Error initializing compression"); |
Yann Collet | 9f43292 | 2015-11-09 17:42:17 +0100 | [diff] [blame] | 262 | filesize = 0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 263 | |
| 264 | /* Main compression loop */ |
| 265 | while (1) |
| 266 | { |
| 267 | size_t inSize; |
| 268 | |
| 269 | /* Fill input Buffer */ |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 270 | inSize = fread(inBuff, (size_t)1, inBuffSize, finput); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 271 | if (inSize==0) break; |
| 272 | filesize += inSize; |
Yann Collet | d7233d6 | 2015-11-22 14:40:51 +0100 | [diff] [blame] | 273 | DISPLAYUPDATE(2, "\rRead : %u MB ", (U32)(filesize>>20)); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 274 | |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 275 | { |
| 276 | /* Compress (buffered streaming ensures appropriate formatting) */ |
| 277 | size_t usedInSize = inSize; |
| 278 | size_t cSize = outBuffSize; |
| 279 | size_t result = ZBUFF_compressContinue(ctx, outBuff, &cSize, inBuff, &usedInSize); |
| 280 | if (ZBUFF_isError(result)) |
| 281 | EXM_THROW(23, "Compression error : %s ", ZBUFF_getErrorName(result)); |
| 282 | if (inSize != usedInSize) |
| 283 | /* inBuff should be entirely consumed since buffer sizes are recommended ones */ |
| 284 | EXM_THROW(24, "Compression error : input block not fully consumed"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 285 | |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 286 | /* Write cBlock */ |
| 287 | sizeCheck = fwrite(outBuff, 1, cSize, foutput); |
| 288 | if (sizeCheck!=cSize) EXM_THROW(25, "Write error : cannot write compressed block into %s", output_filename); |
| 289 | compressedfilesize += cSize; |
| 290 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 291 | |
| 292 | DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", (U32)(filesize>>20), (double)compressedfilesize/filesize*100); |
| 293 | } |
| 294 | |
| 295 | /* End of Frame */ |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 296 | { |
| 297 | size_t cSize = outBuffSize; |
| 298 | size_t result = ZBUFF_compressEnd(ctx, outBuff, &cSize); |
| 299 | if (result!=0) EXM_THROW(26, "Compression error : cannot create frame end"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 300 | |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 301 | sizeCheck = fwrite(outBuff, 1, cSize, foutput); |
| 302 | if (sizeCheck!=cSize) EXM_THROW(27, "Write error : cannot write frame end into %s", output_filename); |
| 303 | compressedfilesize += cSize; |
| 304 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 305 | |
| 306 | /* Status */ |
| 307 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
| 308 | DISPLAYLEVEL(2,"Compressed %llu bytes into %llu bytes ==> %.2f%%\n", |
| 309 | (unsigned long long) filesize, (unsigned long long) compressedfilesize, (double)compressedfilesize/filesize*100); |
| 310 | |
| 311 | /* clean */ |
| 312 | free(inBuff); |
| 313 | free(outBuff); |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 314 | ZBUFF_freeCCtx(ctx); |
Yann Collet | 5b14760 | 2015-08-25 17:41:46 +0100 | [diff] [blame] | 315 | fclose(finput); |
Yann Collet | 61d08c5 | 2015-08-25 18:13:32 +0100 | [diff] [blame] | 316 | 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] | 317 | |
| 318 | return compressedfilesize; |
| 319 | } |
| 320 | |
| 321 | |
Yann Collet | be50aaa | 2015-09-10 23:26:09 +0100 | [diff] [blame] | 322 | unsigned long long FIO_decompressFrame(FILE* foutput, FILE* finput, |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 323 | BYTE* inBuff, size_t inBuffSize, size_t alreadyLoaded, |
Yann Collet | be50aaa | 2015-09-10 23:26:09 +0100 | [diff] [blame] | 324 | BYTE* outBuff, size_t outBuffSize, |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 325 | ZBUFF_DCtx* dctx) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 326 | { |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 327 | U64 frameSize = 0; |
| 328 | size_t readSize=alreadyLoaded; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 329 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 330 | /* Main decompression Loop */ |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 331 | ZBUFF_decompressInit(dctx); |
| 332 | while (1) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 333 | { |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 334 | /* Decode */ |
| 335 | size_t sizeCheck; |
| 336 | size_t inSize=readSize, decodedSize=outBuffSize; |
| 337 | size_t inStart=0; |
| 338 | size_t toRead = ZBUFF_decompressContinue(dctx, outBuff, &decodedSize, inBuff+inStart, &inSize); |
| 339 | if (ZBUFF_isError(toRead)) EXM_THROW(36, "Decoding error : %s", ZBUFF_getErrorName(toRead)); |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 340 | readSize -= inSize; |
| 341 | inStart += inSize; |
| 342 | |
| 343 | /* Write block */ |
| 344 | sizeCheck = fwrite(outBuff, 1, decodedSize, foutput); |
| 345 | if (sizeCheck != decodedSize) EXM_THROW(37, "Write error : unable to write data block to destination file"); |
| 346 | frameSize += decodedSize; |
| 347 | DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(frameSize>>20) ); |
| 348 | |
Yann Collet | 31d1806 | 2015-11-27 14:07:36 +0100 | [diff] [blame^] | 349 | if (toRead == 0) break; |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 350 | if (readSize) continue; /* still some data left within inBuff */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 351 | |
| 352 | /* Fill input buffer */ |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 353 | if (toRead > inBuffSize) EXM_THROW(34, "too large block"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 354 | readSize = fread(inBuff, 1, toRead, finput); |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 355 | if (readSize != toRead) EXM_THROW(35, "Read error"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 356 | } |
| 357 | |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 358 | return frameSize; |
Yann Collet | be50aaa | 2015-09-10 23:26:09 +0100 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 362 | unsigned long long FIO_decompressFilename(const char* output_filename, const char* input_filename) |
| 363 | { |
| 364 | FILE* finput, *foutput; |
| 365 | BYTE* inBuff=NULL; |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 366 | size_t inBuffSize = ZBUFF_recommendedDInSize(); |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 367 | BYTE* outBuff=NULL; |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 368 | size_t outBuffSize = ZBUFF_recommendedDOutSize(); |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 369 | U64 filesize = 0; |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 370 | size_t toRead; |
| 371 | size_t sizeCheck; |
| 372 | |
| 373 | |
| 374 | /* Init */ |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 375 | ZBUFF_DCtx* dctx = ZBUFF_createDCtx(); |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 376 | FIO_getFileHandles(&finput, &foutput, input_filename, output_filename); |
| 377 | |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 378 | /* Allocate Memory (if needed) */ |
| 379 | inBuff = (BYTE*)malloc(inBuffSize); |
| 380 | outBuff = (BYTE*)malloc(outBuffSize); |
| 381 | if (!inBuff || !outBuff) EXM_THROW(33, "Allocation error : not enough memory"); |
| 382 | |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 383 | /* for each frame */ |
| 384 | for ( ; ; ) |
| 385 | { |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 386 | U32 magicNumber; |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 387 | toRead = 0; |
| 388 | |
| 389 | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1) |
| 390 | /* check magic number -> version */ |
| 391 | toRead = 4; |
| 392 | sizeCheck = fread(inBuff, (size_t)1, toRead, finput); |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 393 | if (sizeCheck==0) break; /* no more input */ |
| 394 | if (sizeCheck != toRead) EXM_THROW(31, "Read error : cannot read header"); |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 395 | magicNumber = MEM_readLE32(inBuff); |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 396 | if (ZSTD_isLegacy(magicNumber)) |
| 397 | { |
| 398 | filesize += FIO_decompressLegacyFrame(foutput, finput, magicNumber); |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 399 | continue; |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 400 | } |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 401 | #endif /* ZSTD_LEGACY_SUPPORT */ |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 402 | |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 403 | filesize += FIO_decompressFrame(foutput, finput, inBuff, inBuffSize, toRead, outBuff, outBuffSize, dctx); |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
| 407 | DISPLAYLEVEL(2, "Decoded %llu bytes \n", (long long unsigned)filesize); |
| 408 | |
| 409 | /* clean */ |
| 410 | free(inBuff); |
| 411 | free(outBuff); |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 412 | ZBUFF_freeDCtx(dctx); |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 413 | fclose(finput); |
| 414 | if (fclose(foutput)) EXM_THROW(38, "Write error : cannot properly close %s", output_filename); |
| 415 | |
| 416 | return filesize; |
| 417 | } |
| 418 | |
| 419 | |