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 | |
| 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 | 9f43292 | 2015-11-09 17:42:17 +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 <sys/types.h> /* stat64 */ |
| 67 | #include <sys/stat.h> /* stat64 */ |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 68 | #include "mem.h" |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 69 | #include "fileio.h" |
| 70 | #include "zstd_static.h" |
| 71 | |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 72 | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1) |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 73 | # include "zstd_legacy.h" /* legacy */ |
| 74 | # include "fileio_legacy.h" /* legacy */ |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 75 | #endif |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 76 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 77 | |
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 | * OS-specific Includes |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 80 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 81 | #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) |
| 82 | # include <fcntl.h> /* _O_BINARY */ |
| 83 | # include <io.h> /* _setmode, _isatty */ |
| 84 | # ifdef __MINGW32__ |
Yann Collet | e1e6f7d | 2015-01-25 15:50:24 +0100 | [diff] [blame] | 85 | /* 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] | 86 | # endif |
Yann Collet | b5e06dc | 2015-07-04 23:20:56 -0800 | [diff] [blame] | 87 | # 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] | 88 | # define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream)) |
| 89 | #else |
| 90 | # include <unistd.h> /* isatty */ |
| 91 | # define SET_BINARY_MODE(file) |
| 92 | # define IS_CONSOLE(stdStream) isatty(fileno(stdStream)) |
| 93 | #endif |
| 94 | |
Yann Collet | b7d6e8f | 2015-11-09 18:20:39 +0100 | [diff] [blame] | 95 | #if !defined(S_ISREG) |
| 96 | # define S_ISREG(x) (((x) & S_IFMT) == S_IFREG) |
| 97 | #endif |
| 98 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 99 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 100 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 101 | * Constants |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 102 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 103 | #define KB *(1U<<10) |
| 104 | #define MB *(1U<<20) |
| 105 | #define GB *(1U<<30) |
| 106 | |
| 107 | #define _1BIT 0x01 |
| 108 | #define _2BITS 0x03 |
| 109 | #define _3BITS 0x07 |
| 110 | #define _4BITS 0x0F |
| 111 | #define _6BITS 0x3F |
| 112 | #define _8BITS 0xFF |
| 113 | |
| 114 | #define BIT6 0x40 |
| 115 | #define BIT7 0x80 |
| 116 | |
Yann Collet | aacace3 | 2015-02-01 11:57:30 +0100 | [diff] [blame] | 117 | //static const unsigned FIO_maxBlockSizeID = 0xB; /* => 2MB block */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 118 | static const unsigned FIO_blockHeaderSize = 3; |
| 119 | |
| 120 | #define FIO_FRAMEHEADERSIZE 5 /* as a define, because needed to allocated table on stack */ |
| 121 | #define FSE_CHECKSUM_SEED 0 |
| 122 | |
| 123 | #define CACHELINE 64 |
| 124 | |
| 125 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 126 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 127 | * Macros |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 128 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 129 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 130 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 131 | static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */ |
| 132 | |
| 133 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
| 134 | if ((FIO_GetMilliSpan(g_time) > refreshRate) || (g_displayLevel>=4)) \ |
| 135 | { g_time = clock(); DISPLAY(__VA_ARGS__); \ |
| 136 | if (g_displayLevel>=4) fflush(stdout); } } |
| 137 | static const unsigned refreshRate = 150; |
| 138 | static clock_t g_time = 0; |
| 139 | |
| 140 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 141 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 142 | * Local Parameters |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 143 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 144 | static U32 g_overwrite = 0; |
| 145 | |
| 146 | void FIO_overwriteMode(void) { g_overwrite=1; } |
| 147 | void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; } |
| 148 | |
| 149 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 150 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 151 | * Exceptions |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 152 | ***************************************/ |
| 153 | #ifndef DEBUG |
| 154 | # define DEBUG 0 |
| 155 | #endif |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 156 | #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); |
| 157 | #define EXM_THROW(error, ...) \ |
| 158 | { \ |
| 159 | DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ |
| 160 | DISPLAYLEVEL(1, "Error %i : ", error); \ |
| 161 | DISPLAYLEVEL(1, __VA_ARGS__); \ |
| 162 | DISPLAYLEVEL(1, "\n"); \ |
| 163 | exit(error); \ |
| 164 | } |
| 165 | |
| 166 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 167 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 168 | * Functions |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 169 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 170 | static unsigned FIO_GetMilliSpan(clock_t nPrevious) |
| 171 | { |
| 172 | clock_t nCurrent = clock(); |
| 173 | unsigned nSpan = (unsigned)(((nCurrent - nPrevious) * 1000) / CLOCKS_PER_SEC); |
| 174 | return nSpan; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | static void FIO_getFileHandles(FILE** pfinput, FILE** pfoutput, const char* input_filename, const char* output_filename) |
| 179 | { |
| 180 | if (!strcmp (input_filename, stdinmark)) |
| 181 | { |
| 182 | DISPLAYLEVEL(4,"Using stdin for input\n"); |
| 183 | *pfinput = stdin; |
| 184 | SET_BINARY_MODE(stdin); |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | *pfinput = fopen(input_filename, "rb"); |
| 189 | } |
| 190 | |
| 191 | if (!strcmp (output_filename, stdoutmark)) |
| 192 | { |
| 193 | DISPLAYLEVEL(4,"Using stdout for output\n"); |
| 194 | *pfoutput = stdout; |
| 195 | SET_BINARY_MODE(stdout); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | /* Check if destination file already exists */ |
| 200 | *pfoutput=0; |
| 201 | if (strcmp(output_filename,nulmark)) *pfoutput = fopen( output_filename, "rb" ); |
| 202 | if (*pfoutput!=0) |
| 203 | { |
| 204 | fclose(*pfoutput); |
| 205 | if (!g_overwrite) |
| 206 | { |
| 207 | char ch; |
| 208 | if (g_displayLevel <= 1) /* No interaction possible */ |
| 209 | EXM_THROW(11, "Operation aborted : %s already exists", output_filename); |
| 210 | DISPLAYLEVEL(2, "Warning : %s already exists\n", output_filename); |
| 211 | DISPLAYLEVEL(2, "Overwrite ? (Y/N) : "); |
| 212 | ch = (char)getchar(); |
| 213 | if ((ch!='Y') && (ch!='y')) EXM_THROW(11, "Operation aborted : %s already exists", output_filename); |
| 214 | } |
| 215 | } |
| 216 | *pfoutput = fopen( output_filename, "wb" ); |
| 217 | } |
| 218 | |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 219 | if ( *pfinput==0 ) EXM_THROW(12, "Pb opening src : %s", input_filename); |
| 220 | if ( *pfoutput==0) EXM_THROW(13, "Pb opening dst : %s", output_filename); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Yann Collet | 9f43292 | 2015-11-09 17:42:17 +0100 | [diff] [blame] | 223 | |
| 224 | static U64 FIO_getFileSize(const char* infilename) |
| 225 | { |
| 226 | int r; |
| 227 | #if defined(_MSC_VER) |
| 228 | struct _stat64 statbuf; |
| 229 | r = _stat64(infilename, &statbuf); |
| 230 | #else |
| 231 | struct stat statbuf; |
| 232 | r = stat(infilename, &statbuf); |
| 233 | #endif |
| 234 | if (r || !S_ISREG(statbuf.st_mode)) return 0; |
| 235 | return (U64)statbuf.st_size; |
| 236 | } |
| 237 | |
| 238 | |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 239 | 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] | 240 | { |
| 241 | U64 filesize = 0; |
| 242 | U64 compressedfilesize = 0; |
| 243 | BYTE* inBuff; |
| 244 | BYTE* inSlot; |
| 245 | BYTE* inEnd; |
| 246 | BYTE* outBuff; |
| 247 | size_t blockSize = 128 KB; |
| 248 | size_t inBuffSize = 4 * blockSize; |
| 249 | size_t outBuffSize = ZSTD_compressBound(blockSize); |
| 250 | FILE* finput; |
| 251 | FILE* foutput; |
| 252 | size_t sizeCheck, cSize; |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 253 | ZSTD_CCtx* ctx; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 254 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 255 | /* init */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 256 | FIO_getFileHandles(&finput, &foutput, input_filename, output_filename); |
Yann Collet | 9f43292 | 2015-11-09 17:42:17 +0100 | [diff] [blame] | 257 | filesize = FIO_getFileSize(input_filename); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 258 | |
| 259 | /* Allocate Memory */ |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 260 | ctx = ZSTD_createCCtx(); |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 261 | inBuff = (BYTE*)malloc(inBuffSize); |
| 262 | outBuff = (BYTE*)malloc(outBuffSize); |
Yann Collet | 353c5d2 | 2015-10-21 14:39:26 +0100 | [diff] [blame] | 263 | if (!inBuff || !outBuff || !ctx) EXM_THROW(21, "Allocation error : not enough memory"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 264 | inSlot = inBuff; |
| 265 | inEnd = inBuff + inBuffSize; |
| 266 | |
| 267 | /* Write Frame Header */ |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 268 | cSize = ZSTD_compressBegin(ctx, outBuff, outBuffSize, cLevel, filesize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 269 | if (ZSTD_isError(cSize)) EXM_THROW(22, "Compression error : cannot create frame header"); |
| 270 | |
| 271 | sizeCheck = fwrite(outBuff, 1, cSize, foutput); |
Yann Collet | 61d08c5 | 2015-08-25 18:13:32 +0100 | [diff] [blame] | 272 | 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] | 273 | compressedfilesize += cSize; |
Yann Collet | 9f43292 | 2015-11-09 17:42:17 +0100 | [diff] [blame] | 274 | filesize = 0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 275 | |
| 276 | /* Main compression loop */ |
| 277 | while (1) |
| 278 | { |
| 279 | size_t inSize; |
| 280 | |
| 281 | /* Fill input Buffer */ |
| 282 | if (inSlot + blockSize > inEnd) inSlot = inBuff; |
| 283 | inSize = fread(inSlot, (size_t)1, blockSize, finput); |
| 284 | if (inSize==0) break; |
| 285 | filesize += inSize; |
Yann Collet | d7233d6 | 2015-11-22 14:40:51 +0100 | [diff] [blame^] | 286 | DISPLAYUPDATE(2, "\rRead : %u MB ", (U32)(filesize>>20)); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 287 | |
| 288 | /* Compress Block */ |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 289 | cSize = ZSTD_compressContinue(ctx, outBuff, outBuffSize, inSlot, inSize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 290 | if (ZSTD_isError(cSize)) |
| 291 | EXM_THROW(24, "Compression error : %s ", ZSTD_getErrorName(cSize)); |
| 292 | |
| 293 | /* Write cBlock */ |
| 294 | sizeCheck = fwrite(outBuff, 1, cSize, foutput); |
Yann Collet | 61d08c5 | 2015-08-25 18:13:32 +0100 | [diff] [blame] | 295 | 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] | 296 | compressedfilesize += cSize; |
| 297 | inSlot += inSize; |
| 298 | |
| 299 | DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", (U32)(filesize>>20), (double)compressedfilesize/filesize*100); |
| 300 | } |
| 301 | |
| 302 | /* End of Frame */ |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 303 | cSize = ZSTD_compressEnd(ctx, outBuff, outBuffSize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 304 | if (ZSTD_isError(cSize)) EXM_THROW(26, "Compression error : cannot create frame end"); |
| 305 | |
| 306 | sizeCheck = fwrite(outBuff, 1, cSize, foutput); |
Yann Collet | 61d08c5 | 2015-08-25 18:13:32 +0100 | [diff] [blame] | 307 | 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] | 308 | compressedfilesize += cSize; |
| 309 | |
| 310 | /* Status */ |
| 311 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
| 312 | DISPLAYLEVEL(2,"Compressed %llu bytes into %llu bytes ==> %.2f%%\n", |
| 313 | (unsigned long long) filesize, (unsigned long long) compressedfilesize, (double)compressedfilesize/filesize*100); |
| 314 | |
| 315 | /* clean */ |
| 316 | free(inBuff); |
| 317 | free(outBuff); |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 318 | ZSTD_freeCCtx(ctx); |
Yann Collet | 5b14760 | 2015-08-25 17:41:46 +0100 | [diff] [blame] | 319 | fclose(finput); |
Yann Collet | 61d08c5 | 2015-08-25 18:13:32 +0100 | [diff] [blame] | 320 | 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] | 321 | |
| 322 | return compressedfilesize; |
| 323 | } |
| 324 | |
| 325 | |
Yann Collet | be50aaa | 2015-09-10 23:26:09 +0100 | [diff] [blame] | 326 | unsigned long long FIO_decompressFrame(FILE* foutput, FILE* finput, |
| 327 | BYTE* inBuff, size_t inBuffSize, |
| 328 | BYTE* outBuff, size_t outBuffSize, |
Yann Collet | 353c5d2 | 2015-10-21 14:39:26 +0100 | [diff] [blame] | 329 | ZSTD_DCtx* dctx) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 330 | { |
Yann Collet | be50aaa | 2015-09-10 23:26:09 +0100 | [diff] [blame] | 331 | BYTE* op = outBuff; |
| 332 | BYTE* const oend = outBuff + outBuffSize; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 333 | U64 filesize = 0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 334 | size_t toRead; |
| 335 | size_t sizeCheck; |
| 336 | |
| 337 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 338 | /* Main decompression Loop */ |
Yann Collet | c5d46b5 | 2015-02-16 18:06:26 +0100 | [diff] [blame] | 339 | toRead = ZSTD_nextSrcSizeToDecompress(dctx); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 340 | while (toRead) |
| 341 | { |
| 342 | size_t readSize, decodedSize; |
| 343 | |
| 344 | /* Fill input buffer */ |
Yann Collet | 1885029 | 2015-08-24 20:17:11 +0100 | [diff] [blame] | 345 | if (toRead > inBuffSize) |
| 346 | EXM_THROW(34, "too large block"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 347 | readSize = fread(inBuff, 1, toRead, finput); |
| 348 | if (readSize != toRead) |
Yann Collet | 1885029 | 2015-08-24 20:17:11 +0100 | [diff] [blame] | 349 | EXM_THROW(35, "Read error"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 350 | |
| 351 | /* Decode block */ |
| 352 | decodedSize = ZSTD_decompressContinue(dctx, op, oend-op, inBuff, readSize); |
Yann Collet | 1885029 | 2015-08-24 20:17:11 +0100 | [diff] [blame] | 353 | if (ZSTD_isError(decodedSize)) EXM_THROW(36, "Decoding error : input corrupted"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 354 | |
Yann Collet | c5d46b5 | 2015-02-16 18:06:26 +0100 | [diff] [blame] | 355 | if (decodedSize) /* not a header */ |
| 356 | { |
| 357 | /* Write block */ |
| 358 | sizeCheck = fwrite(op, 1, decodedSize, foutput); |
Yann Collet | 1885029 | 2015-08-24 20:17:11 +0100 | [diff] [blame] | 359 | 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] | 360 | filesize += decodedSize; |
| 361 | op += decodedSize; |
| 362 | if (op==oend) op = outBuff; |
| 363 | DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(filesize>>20) ); |
| 364 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 365 | |
| 366 | /* prepare for next Block */ |
Yann Collet | c5d46b5 | 2015-02-16 18:06:26 +0100 | [diff] [blame] | 367 | toRead = ZSTD_nextSrcSizeToDecompress(dctx); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 368 | } |
| 369 | |
Yann Collet | be50aaa | 2015-09-10 23:26:09 +0100 | [diff] [blame] | 370 | return filesize; |
| 371 | } |
| 372 | |
| 373 | |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 374 | #define MAXHEADERSIZE (FIO_FRAMEHEADERSIZE+3) |
| 375 | unsigned long long FIO_decompressFilename(const char* output_filename, const char* input_filename) |
| 376 | { |
| 377 | FILE* finput, *foutput; |
| 378 | BYTE* inBuff=NULL; |
| 379 | size_t inBuffSize = 0; |
| 380 | BYTE* outBuff=NULL; |
| 381 | size_t outBuffSize = 0; |
| 382 | U32 blockSize = 128 KB; |
| 383 | U32 wNbBlocks = 4; |
| 384 | U64 filesize = 0; |
| 385 | BYTE* header[MAXHEADERSIZE]; |
| 386 | size_t toRead; |
| 387 | size_t sizeCheck; |
| 388 | |
| 389 | |
| 390 | /* Init */ |
Yann Collet | 353c5d2 | 2015-10-21 14:39:26 +0100 | [diff] [blame] | 391 | ZSTD_DCtx* dctx = ZSTD_createDCtx(); |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 392 | FIO_getFileHandles(&finput, &foutput, input_filename, output_filename); |
| 393 | |
| 394 | /* for each frame */ |
| 395 | for ( ; ; ) |
| 396 | { |
| 397 | /* check magic number -> version */ |
| 398 | U32 magicNumber; |
| 399 | toRead = sizeof(ZSTD_magicNumber);; |
| 400 | sizeCheck = fread(header, (size_t)1, toRead, finput); |
| 401 | if (sizeCheck==0) break; /* no more input */ |
| 402 | if (sizeCheck != toRead) EXM_THROW(31, "Read error : cannot read header"); |
| 403 | |
| 404 | magicNumber = MEM_readLE32(header); |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 405 | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1) |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 406 | if (ZSTD_isLegacy(magicNumber)) |
| 407 | { |
| 408 | filesize += FIO_decompressLegacyFrame(foutput, finput, magicNumber); |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 409 | continue; |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 410 | } |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 411 | #endif /* ZSTD_LEGACY_SUPPORT */ |
| 412 | if (magicNumber != ZSTD_magicNumber) EXM_THROW(32, "Error : unknown frame prefix"); |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 413 | |
| 414 | /* prepare frame decompression, by completing header */ |
| 415 | ZSTD_resetDCtx(dctx); |
| 416 | toRead = ZSTD_nextSrcSizeToDecompress(dctx) - sizeof(ZSTD_magicNumber); |
| 417 | if (toRead > MAXHEADERSIZE) EXM_THROW(30, "Not enough memory to read header"); |
Yann Collet | 4114f95 | 2015-10-30 06:40:22 +0100 | [diff] [blame] | 418 | sizeCheck = fread(&header[sizeof(ZSTD_magicNumber)], 1, toRead, finput); |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 419 | if (sizeCheck != toRead) EXM_THROW(31, "Read error : cannot read header"); |
| 420 | sizeCheck = ZSTD_decompressContinue(dctx, NULL, 0, header, sizeof(ZSTD_magicNumber)+toRead); // Decode frame header |
| 421 | if (ZSTD_isError(sizeCheck)) EXM_THROW(32, "Error decoding header"); |
| 422 | |
| 423 | /* Here later : blockSize determination */ |
| 424 | |
| 425 | /* Allocate Memory (if needed) */ |
| 426 | { |
| 427 | size_t newInBuffSize = blockSize + FIO_blockHeaderSize; |
| 428 | size_t newOutBuffSize = wNbBlocks * blockSize; |
| 429 | if (newInBuffSize > inBuffSize) |
| 430 | { |
| 431 | free(inBuff); |
| 432 | inBuffSize = newInBuffSize; |
| 433 | inBuff = (BYTE*)malloc(inBuffSize); |
| 434 | } |
| 435 | if (newOutBuffSize > outBuffSize) |
| 436 | { |
| 437 | free(outBuff); |
| 438 | outBuffSize = newOutBuffSize; |
| 439 | outBuff = (BYTE*)malloc(outBuffSize); |
| 440 | } |
| 441 | } |
| 442 | if (!inBuff || !outBuff) EXM_THROW(33, "Allocation error : not enough memory"); |
| 443 | |
| 444 | filesize += FIO_decompressFrame(foutput, finput, inBuff, inBuffSize, outBuff, outBuffSize, dctx); |
| 445 | } |
| 446 | |
| 447 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
| 448 | DISPLAYLEVEL(2, "Decoded %llu bytes \n", (long long unsigned)filesize); |
| 449 | |
| 450 | /* clean */ |
| 451 | free(inBuff); |
| 452 | free(outBuff); |
| 453 | ZSTD_freeDCtx(dctx); |
| 454 | fclose(finput); |
| 455 | if (fclose(foutput)) EXM_THROW(38, "Write error : cannot properly close %s", output_filename); |
| 456 | |
| 457 | return filesize; |
| 458 | } |
| 459 | |
| 460 | |
| 461 | #if 0 |
Yann Collet | be50aaa | 2015-09-10 23:26:09 +0100 | [diff] [blame] | 462 | unsigned long long FIO_decompressFilename(const char* output_filename, const char* input_filename) |
| 463 | { |
| 464 | FILE* finput, *foutput; |
| 465 | BYTE* inBuff=NULL; |
| 466 | size_t inBuffSize = 0; |
| 467 | BYTE* outBuff=NULL; |
| 468 | size_t outBuffSize = 0; |
| 469 | U32 blockSize = 128 KB; |
| 470 | U32 wNbBlocks = 4; |
| 471 | U64 filesize = 0; |
| 472 | BYTE* header[MAXHEADERSIZE]; |
| 473 | ZSTD_Dctx* dctx; |
| 474 | size_t toRead; |
| 475 | size_t sizeCheck; |
| 476 | |
| 477 | |
| 478 | /* Init */ |
| 479 | FIO_getFileHandles(&finput, &foutput, input_filename, output_filename); |
| 480 | dctx = ZSTD_createDCtx(); |
| 481 | |
| 482 | /* for each frame */ |
| 483 | for ( ; ; ) |
| 484 | { |
| 485 | /* check header */ |
| 486 | ZSTD_resetDCtx(dctx); |
| 487 | toRead = ZSTD_nextSrcSizeToDecompress(dctx); |
| 488 | if (toRead > MAXHEADERSIZE) EXM_THROW(30, "Not enough memory to read header"); |
| 489 | sizeCheck = fread(header, (size_t)1, toRead, finput); |
| 490 | if (sizeCheck==0) break; /* no more input */ |
| 491 | if (sizeCheck != toRead) EXM_THROW(31, "Read error : cannot read header"); |
| 492 | sizeCheck = ZSTD_decompressContinue(dctx, NULL, 0, header, toRead); // Decode frame header |
| 493 | if (ZSTD_isError(sizeCheck)) EXM_THROW(32, "Error decoding header"); |
| 494 | |
| 495 | /* Here later : blockSize determination */ |
| 496 | |
| 497 | /* Allocate Memory (if needed) */ |
| 498 | { |
| 499 | size_t newInBuffSize = blockSize + FIO_blockHeaderSize; |
| 500 | size_t newOutBuffSize = wNbBlocks * blockSize; |
| 501 | if (newInBuffSize > inBuffSize) |
| 502 | { |
| 503 | free(inBuff); |
| 504 | inBuffSize = newInBuffSize; |
| 505 | inBuff = (BYTE*)malloc(inBuffSize); |
| 506 | } |
| 507 | if (newOutBuffSize > outBuffSize) |
| 508 | { |
| 509 | free(outBuff); |
| 510 | outBuffSize = newOutBuffSize; |
| 511 | outBuff = (BYTE*)malloc(outBuffSize); |
| 512 | } |
| 513 | } |
| 514 | if (!inBuff || !outBuff) EXM_THROW(33, "Allocation error : not enough memory"); |
| 515 | |
| 516 | filesize += FIO_decompressFrame(foutput, finput, inBuff, inBuffSize, outBuff, outBuffSize, dctx); |
| 517 | } |
| 518 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 519 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 520 | DISPLAYLEVEL(2, "Decoded %llu bytes \n", (long long unsigned)filesize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 521 | |
| 522 | /* clean */ |
| 523 | free(inBuff); |
| 524 | free(outBuff); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 525 | ZSTD_freeDCtx(dctx); |
Yann Collet | 5abd820 | 2015-08-27 03:16:04 +0100 | [diff] [blame] | 526 | fclose(finput); |
| 527 | 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] | 528 | |
| 529 | return filesize; |
| 530 | } |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 531 | #endif |