Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1 | /* |
Yann Collet | 4488661 | 2016-02-11 04:17:50 +0100 | [diff] [blame] | 2 | fileio.c - File i/o handler for zstd |
| 3 | Copyright (C) Yann Collet 2013-2016 |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 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 : |
Yann Collet | 4488661 | 2016-02-11 04:17:50 +0100 | [diff] [blame] | 22 | - zstd homepage : http://www.zstd.net |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 23 | */ |
| 24 | /* |
| 25 | Note : this is stand-alone program. |
| 26 | It is not part of ZSTD compression library, it is a user program of ZSTD library. |
| 27 | The license of ZSTD library is BSD. |
| 28 | The license of this file is GPLv2. |
| 29 | */ |
| 30 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 31 | /* ************************************* |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 32 | * Tuning options |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 33 | ***************************************/ |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 34 | #ifndef ZSTD_LEGACY_SUPPORT |
Yann Collet | 4488661 | 2016-02-11 04:17:50 +0100 | [diff] [blame] | 35 | /* LEGACY_SUPPORT : |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 36 | * decompressor can decode older formats (starting from Zstd 0.1+) */ |
| 37 | # define ZSTD_LEGACY_SUPPORT 1 |
Yann Collet | 9f43292 | 2015-11-09 17:42:17 +0100 | [diff] [blame] | 38 | #endif |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 39 | |
| 40 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 41 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 42 | * Compiler Options |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 43 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 44 | /* Disable some Visual warning messages */ |
| 45 | #ifdef _MSC_VER |
| 46 | # define _CRT_SECURE_NO_WARNINGS |
| 47 | # define _CRT_SECURE_NO_DEPRECATE /* VS2005 */ |
| 48 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 49 | #endif |
| 50 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 51 | #define _FILE_OFFSET_BITS 64 /* Large file support on 32-bits unix */ |
| 52 | #define _POSIX_SOURCE 1 /* enable fileno() within <stdio.h> on unix */ |
| 53 | |
| 54 | |
Yann Collet | 6f3acba | 2016-02-12 20:19:48 +0100 | [diff] [blame] | 55 | /*-************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 56 | * Includes |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 57 | ***************************************/ |
inikep | bab4317 | 2016-04-29 15:19:40 +0200 | [diff] [blame] | 58 | #include "util.h" /* UTIL_GetFileSize */ |
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 */ |
inikep | d5ff2c3 | 2016-04-28 14:40:45 +0200 | [diff] [blame] | 64 | |
inikep | d5ff2c3 | 2016-04-28 14:40:45 +0200 | [diff] [blame] | 65 | #include "mem.h" |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 66 | #include "fileio.h" |
Yann Collet | 09b21ee | 2016-03-15 12:56:03 +0100 | [diff] [blame] | 67 | #include "zstd_static.h" /* ZSTD_magicNumber, ZSTD_frameHeaderSize_max */ |
Yann Collet | 62ae5fb | 2016-02-12 18:59:11 +0100 | [diff] [blame] | 68 | #include "zbuff_static.h" |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 69 | |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 70 | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1) |
Yann Collet | 6f3acba | 2016-02-12 20:19:48 +0100 | [diff] [blame] | 71 | # include "zstd_legacy.h" /* ZSTD_isLegacy */ |
| 72 | # include "fileio_legacy.h" /* FIO_decompressLegacyFrame */ |
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 | 6f3acba | 2016-02-12 20:19:48 +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 */ |
Yann Collet | b5e06dc | 2015-07-04 23:20:56 -0800 | [diff] [blame] | 82 | # 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] | 83 | # define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream)) |
| 84 | #else |
| 85 | # include <unistd.h> /* isatty */ |
| 86 | # define SET_BINARY_MODE(file) |
| 87 | # define IS_CONSOLE(stdStream) isatty(fileno(stdStream)) |
| 88 | #endif |
| 89 | |
| 90 | |
Yann Collet | 6f3acba | 2016-02-12 20:19:48 +0100 | [diff] [blame] | 91 | /*-************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 92 | * Constants |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 93 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 94 | #define _1BIT 0x01 |
| 95 | #define _2BITS 0x03 |
| 96 | #define _3BITS 0x07 |
| 97 | #define _4BITS 0x0F |
| 98 | #define _6BITS 0x3F |
| 99 | #define _8BITS 0xFF |
| 100 | |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 101 | #define BLOCKSIZE (128 KB) |
| 102 | #define ROLLBUFFERSIZE (BLOCKSIZE*8*64) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 103 | |
Yann Collet | 6f3acba | 2016-02-12 20:19:48 +0100 | [diff] [blame] | 104 | #define FIO_FRAMEHEADERSIZE 5 /* as a define, because needed to allocated table on stack */ |
| 105 | #define FSE_CHECKSUM_SEED 0 |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 106 | |
| 107 | #define CACHELINE 64 |
| 108 | |
Yann Collet | 6f3acba | 2016-02-12 20:19:48 +0100 | [diff] [blame] | 109 | #define MAX_DICT_SIZE (1 MB) /* protection against large input (attack scenario) ; can be changed */ |
| 110 | |
inikep | 3c7c352 | 2016-04-22 13:59:05 +0200 | [diff] [blame] | 111 | #define FNSPACE 30 |
| 112 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 113 | |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 114 | /*-************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 115 | * Macros |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 116 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 117 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 118 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 119 | static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */ |
Yann Collet | 8a1d1a6 | 2016-03-10 21:02:25 +0100 | [diff] [blame] | 120 | void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 121 | |
| 122 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
| 123 | if ((FIO_GetMilliSpan(g_time) > refreshRate) || (g_displayLevel>=4)) \ |
| 124 | { g_time = clock(); DISPLAY(__VA_ARGS__); \ |
| 125 | if (g_displayLevel>=4) fflush(stdout); } } |
| 126 | static const unsigned refreshRate = 150; |
| 127 | static clock_t g_time = 0; |
| 128 | |
| 129 | |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 130 | |
| 131 | /*-************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 132 | * Local Parameters |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 133 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 134 | static U32 g_overwrite = 0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 135 | void FIO_overwriteMode(void) { g_overwrite=1; } |
Yann Collet | 8a1d1a6 | 2016-03-10 21:02:25 +0100 | [diff] [blame] | 136 | static U32 g_maxWLog = 23; |
| 137 | void FIO_setMaxWLog(unsigned maxWLog) { g_maxWLog = maxWLog; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 138 | |
| 139 | |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 140 | /*-************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 141 | * Exceptions |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 142 | ***************************************/ |
| 143 | #ifndef DEBUG |
| 144 | # define DEBUG 0 |
| 145 | #endif |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 146 | #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); |
| 147 | #define EXM_THROW(error, ...) \ |
| 148 | { \ |
| 149 | DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ |
| 150 | DISPLAYLEVEL(1, "Error %i : ", error); \ |
| 151 | DISPLAYLEVEL(1, __VA_ARGS__); \ |
| 152 | DISPLAYLEVEL(1, "\n"); \ |
| 153 | exit(error); \ |
| 154 | } |
| 155 | |
| 156 | |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 157 | /*-************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 158 | * Functions |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 159 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 160 | static unsigned FIO_GetMilliSpan(clock_t nPrevious) |
| 161 | { |
| 162 | clock_t nCurrent = clock(); |
| 163 | unsigned nSpan = (unsigned)(((nCurrent - nPrevious) * 1000) / CLOCKS_PER_SEC); |
| 164 | return nSpan; |
| 165 | } |
| 166 | |
| 167 | |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 168 | static FILE* FIO_openSrcFile(const char* srcFileName) |
| 169 | { |
| 170 | FILE* f; |
| 171 | |
| 172 | if (!strcmp (srcFileName, stdinmark)) { |
| 173 | DISPLAYLEVEL(4,"Using stdin for input\n"); |
| 174 | f = stdin; |
| 175 | SET_BINARY_MODE(stdin); |
| 176 | } else { |
| 177 | f = fopen(srcFileName, "rb"); |
| 178 | } |
| 179 | |
| 180 | if ( f==NULL ) DISPLAYLEVEL(1, "zstd: %s: No such file\n", srcFileName); |
| 181 | |
| 182 | return f; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | static FILE* FIO_openDstFile(const char* dstFileName) |
| 187 | { |
| 188 | FILE* f; |
| 189 | |
| 190 | if (!strcmp (dstFileName, stdoutmark)) { |
| 191 | DISPLAYLEVEL(4,"Using stdout for output\n"); |
| 192 | f = stdout; |
| 193 | SET_BINARY_MODE(stdout); |
| 194 | } else { |
| 195 | if (!g_overwrite) { /* Check if destination file already exists */ |
| 196 | f = fopen( dstFileName, "rb" ); |
| 197 | if (f != 0) { /* dest file exists, prompt for overwrite authorization */ |
| 198 | fclose(f); |
| 199 | if (g_displayLevel <= 1) { |
| 200 | /* No interaction possible */ |
| 201 | DISPLAY("zstd: %s already exists; not overwritten \n", dstFileName); |
| 202 | return 0; |
| 203 | } |
| 204 | DISPLAY("zstd: %s already exists; do you wish to overwrite (y/N) ? ", dstFileName); |
| 205 | { |
| 206 | int ch = getchar(); |
| 207 | if ((ch!='Y') && (ch!='y')) { |
| 208 | DISPLAY(" not overwritten \n"); |
| 209 | return 0; |
| 210 | } |
| 211 | while ((ch!=EOF) && (ch!='\n')) ch = getchar(); /* flush rest of input line */ |
| 212 | } } } |
| 213 | f = fopen( dstFileName, "wb" ); |
| 214 | } |
| 215 | return f; |
| 216 | } |
| 217 | |
| 218 | |
Yann Collet | 8a1d1a6 | 2016-03-10 21:02:25 +0100 | [diff] [blame] | 219 | /*! FIO_loadFile() : |
Yann Collet | 09b21ee | 2016-03-15 12:56:03 +0100 | [diff] [blame] | 220 | * creates a buffer, pointed by `*bufferPtr`, |
Yann Collet | 8a1d1a6 | 2016-03-10 21:02:25 +0100 | [diff] [blame] | 221 | * loads `filename` content into it, |
| 222 | * up to MAX_DICT_SIZE bytes |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 223 | */ |
| 224 | static size_t FIO_loadFile(void** bufferPtr, const char* fileName) |
| 225 | { |
| 226 | FILE* fileHandle; |
| 227 | size_t readSize; |
| 228 | U64 fileSize; |
| 229 | |
| 230 | *bufferPtr = NULL; |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 231 | if (fileName == NULL) return 0; |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 232 | |
| 233 | DISPLAYLEVEL(4,"Loading %s as dictionary \n", fileName); |
| 234 | fileHandle = fopen(fileName, "rb"); |
| 235 | if (fileHandle==0) EXM_THROW(31, "Error opening file %s", fileName); |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 236 | fileSize = UTIL_getFileSize(fileName); |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 237 | if (fileSize > MAX_DICT_SIZE) { |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 238 | int seekResult; |
| 239 | if (fileSize > 1 GB) EXM_THROW(32, "Dictionary file %s is too large", fileName); /* avoid extreme cases */ |
| 240 | DISPLAYLEVEL(2,"Dictionary %s is too large : using last %u bytes only \n", fileName, MAX_DICT_SIZE); |
| 241 | seekResult = fseek(fileHandle, (long int)(fileSize-MAX_DICT_SIZE), SEEK_SET); /* use end of file */ |
| 242 | if (seekResult != 0) EXM_THROW(33, "Error seeking into file %s", fileName); |
| 243 | fileSize = MAX_DICT_SIZE; |
| 244 | } |
| 245 | *bufferPtr = (BYTE*)malloc((size_t)fileSize); |
| 246 | if (*bufferPtr==NULL) EXM_THROW(34, "Allocation error : not enough memory for dictBuffer"); |
| 247 | readSize = fread(*bufferPtr, 1, (size_t)fileSize, fileHandle); |
| 248 | if (readSize!=fileSize) EXM_THROW(35, "Error reading dictionary file %s", fileName); |
| 249 | fclose(fileHandle); |
| 250 | return (size_t)fileSize; |
| 251 | } |
| 252 | |
inikep | 3c7c352 | 2016-04-22 13:59:05 +0200 | [diff] [blame] | 253 | #ifndef ZSTD_NOCOMPRESS |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 254 | |
Yann Collet | 8a1d1a6 | 2016-03-10 21:02:25 +0100 | [diff] [blame] | 255 | /*-********************************************************************** |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 256 | * Compression |
| 257 | ************************************************************************/ |
| 258 | typedef struct { |
| 259 | void* srcBuffer; |
| 260 | size_t srcBufferSize; |
| 261 | void* dstBuffer; |
| 262 | size_t dstBufferSize; |
| 263 | void* dictBuffer; |
| 264 | size_t dictBufferSize; |
| 265 | ZBUFF_CCtx* ctx; |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 266 | FILE* dstFile; |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 267 | FILE* srcFile; |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 268 | } cRess_t; |
| 269 | |
| 270 | static cRess_t FIO_createCResources(const char* dictFileName) |
| 271 | { |
| 272 | cRess_t ress; |
| 273 | |
| 274 | ress.ctx = ZBUFF_createCCtx(); |
| 275 | if (ress.ctx == NULL) EXM_THROW(30, "Allocation error : can't create ZBUFF context"); |
| 276 | |
| 277 | /* Allocate Memory */ |
| 278 | ress.srcBufferSize = ZBUFF_recommendedCInSize(); |
| 279 | ress.srcBuffer = malloc(ress.srcBufferSize); |
| 280 | ress.dstBufferSize = ZBUFF_recommendedCOutSize(); |
| 281 | ress.dstBuffer = malloc(ress.dstBufferSize); |
| 282 | if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(31, "Allocation error : not enough memory"); |
| 283 | |
| 284 | /* dictionary */ |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 285 | ress.dictBufferSize = FIO_loadFile(&(ress.dictBuffer), dictFileName); |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 286 | |
| 287 | return ress; |
| 288 | } |
| 289 | |
| 290 | static void FIO_freeCResources(cRess_t ress) |
| 291 | { |
| 292 | size_t errorCode; |
| 293 | free(ress.srcBuffer); |
| 294 | free(ress.dstBuffer); |
| 295 | free(ress.dictBuffer); |
| 296 | errorCode = ZBUFF_freeCCtx(ress.ctx); |
| 297 | if (ZBUFF_isError(errorCode)) EXM_THROW(38, "Error : can't release ZBUFF context resource : %s", ZBUFF_getErrorName(errorCode)); |
| 298 | } |
| 299 | |
| 300 | |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 301 | /*! FIO_compressFilename_internal() : |
Yann Collet | 8a1d1a6 | 2016-03-10 21:02:25 +0100 | [diff] [blame] | 302 | * same as FIO_compressFilename_extRess(), with `ress.desFile` already opened. |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 303 | * @return : 0 : compression completed correctly, |
| 304 | * 1 : missing or pb opening srcFileName |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 305 | */ |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 306 | static int FIO_compressFilename_internal(cRess_t ress, |
| 307 | const char* dstFileName, const char* srcFileName, |
| 308 | int cLevel) |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 309 | { |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 310 | FILE* const srcFile = ress.srcFile; |
| 311 | FILE* const dstFile = ress.dstFile; |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 312 | U64 readsize = 0; |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 313 | U64 compressedfilesize = 0; |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 314 | U64 const fileSize = UTIL_getFileSize(srcFileName); |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 315 | |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 316 | /* init */ |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 317 | { ZSTD_parameters params; |
| 318 | params.cParams = ZSTD_getCParams(cLevel, fileSize, ress.dictBufferSize); |
| 319 | params.fParams.contentSizeFlag = 1; |
| 320 | if (g_maxWLog) if (params.cParams.windowLog > g_maxWLog) params.cParams.windowLog = g_maxWLog; |
| 321 | { size_t const errorCode = ZBUFF_compressInit_advanced(ress.ctx, ress.dictBuffer, ress.dictBufferSize, params, fileSize); |
| 322 | if (ZBUFF_isError(errorCode)) EXM_THROW(21, "Error initializing compression : %s", ZBUFF_getErrorName(errorCode)); } |
| 323 | } |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 324 | |
| 325 | /* Main compression loop */ |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 326 | readsize = 0; |
Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 327 | while (1) { |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 328 | /* Fill input Buffer */ |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 329 | size_t const inSize = fread(ress.srcBuffer, (size_t)1, ress.srcBufferSize, srcFile); |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 330 | if (inSize==0) break; |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 331 | readsize += inSize; |
| 332 | DISPLAYUPDATE(2, "\rRead : %u MB ", (U32)(readsize>>20)); |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 333 | |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 334 | { /* Compress using buffered streaming */ |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 335 | size_t usedInSize = inSize; |
| 336 | size_t cSize = ress.dstBufferSize; |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 337 | { size_t const result = ZBUFF_compressContinue(ress.ctx, ress.dstBuffer, &cSize, ress.srcBuffer, &usedInSize); |
| 338 | if (ZBUFF_isError(result)) EXM_THROW(23, "Compression error : %s ", ZBUFF_getErrorName(result)); } |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 339 | if (inSize != usedInSize) |
| 340 | /* inBuff should be entirely consumed since buffer sizes are recommended ones */ |
| 341 | EXM_THROW(24, "Compression error : input block not fully consumed"); |
| 342 | |
| 343 | /* Write cBlock */ |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 344 | { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, cSize, dstFile); |
| 345 | if (sizeCheck!=cSize) EXM_THROW(25, "Write error : cannot write compressed block into %s", dstFileName); } |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 346 | compressedfilesize += cSize; |
| 347 | } |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 348 | DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", (U32)(readsize>>20), (double)compressedfilesize/readsize*100); |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /* End of Frame */ |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 352 | { size_t cSize = ress.dstBufferSize; |
| 353 | size_t const result = ZBUFF_compressEnd(ress.ctx, ress.dstBuffer, &cSize); |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 354 | if (result!=0) EXM_THROW(26, "Compression error : cannot create frame end"); |
| 355 | |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 356 | { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, cSize, dstFile); |
| 357 | if (sizeCheck!=cSize) EXM_THROW(27, "Write error : cannot write frame end into %s", dstFileName); } |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 358 | compressedfilesize += cSize; |
| 359 | } |
| 360 | |
| 361 | /* Status */ |
| 362 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
| 363 | DISPLAYLEVEL(2,"Compressed %llu bytes into %llu bytes ==> %.2f%%\n", |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 364 | (unsigned long long)readsize, (unsigned long long) compressedfilesize, (double)compressedfilesize/readsize*100); |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 365 | |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 370 | /*! FIO_compressFilename_internal() : |
Yann Collet | 09b21ee | 2016-03-15 12:56:03 +0100 | [diff] [blame] | 371 | * same as FIO_compressFilename_extRess(), with ress.destFile already opened (typically stdout) |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 372 | * @return : 0 : compression completed correctly, |
| 373 | * 1 : missing or pb opening srcFileName |
| 374 | */ |
| 375 | static int FIO_compressFilename_srcFile(cRess_t ress, |
| 376 | const char* dstFileName, const char* srcFileName, |
| 377 | int cLevel) |
| 378 | { |
| 379 | int result; |
| 380 | |
| 381 | /* File check */ |
| 382 | ress.srcFile = FIO_openSrcFile(srcFileName); |
| 383 | if (!ress.srcFile) return 1; /* srcFile could not be opened */ |
| 384 | |
| 385 | result = FIO_compressFilename_internal(ress, dstFileName, srcFileName, cLevel); |
| 386 | |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 387 | fclose(ress.srcFile); |
| 388 | return result; |
| 389 | } |
| 390 | |
| 391 | |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 392 | /*! FIO_compressFilename_extRess() : |
| 393 | * @return : 0 : compression completed correctly, |
| 394 | * 1 : missing or pb opening srcFileName |
| 395 | */ |
| 396 | static int FIO_compressFilename_extRess(cRess_t ress, |
| 397 | const char* dstFileName, const char* srcFileName, |
| 398 | int cLevel) |
| 399 | { |
| 400 | int result; |
| 401 | |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 402 | ress.srcFile = FIO_openSrcFile(srcFileName); |
| 403 | if (ress.srcFile==0) return 1; |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 404 | ress.dstFile = FIO_openDstFile(dstFileName); |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 405 | if (ress.dstFile==0) { fclose(ress.srcFile); return 1; } |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 406 | |
| 407 | result = FIO_compressFilename_internal(ress, dstFileName, srcFileName, cLevel); |
Yann Collet | 09b21ee | 2016-03-15 12:56:03 +0100 | [diff] [blame] | 408 | if (result!=0) remove(dstFileName); /* remove operation artefact */ |
Chip Turner | 6de382c | 2016-03-13 22:24:46 -0700 | [diff] [blame] | 409 | |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 410 | fclose(ress.srcFile); /* no pb to expect : only reading */ |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 411 | if (fclose(ress.dstFile)) EXM_THROW(28, "Write error : cannot properly close %s", dstFileName); |
| 412 | return result; |
| 413 | } |
| 414 | |
| 415 | |
Yann Collet | 9d90922 | 2015-12-17 14:09:55 +0100 | [diff] [blame] | 416 | int FIO_compressFilename(const char* dstFileName, const char* srcFileName, |
| 417 | const char* dictFileName, int compressionLevel) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 418 | { |
Yann Collet | 9d90922 | 2015-12-17 14:09:55 +0100 | [diff] [blame] | 419 | clock_t start, end; |
| 420 | cRess_t ress; |
| 421 | int issueWithSrcFile = 0; |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 422 | |
Yann Collet | 9d90922 | 2015-12-17 14:09:55 +0100 | [diff] [blame] | 423 | /* Init */ |
| 424 | start = clock(); |
| 425 | ress = FIO_createCResources(dictFileName); |
Yann Collet | f6f3d75 | 2015-12-13 13:35:21 +0100 | [diff] [blame] | 426 | |
Yann Collet | 9d90922 | 2015-12-17 14:09:55 +0100 | [diff] [blame] | 427 | /* Compress File */ |
| 428 | issueWithSrcFile += FIO_compressFilename_extRess(ress, dstFileName, srcFileName, compressionLevel); |
| 429 | |
| 430 | /* Free resources */ |
| 431 | FIO_freeCResources(ress); |
| 432 | |
| 433 | /* Final Status */ |
| 434 | end = clock(); |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 435 | { double seconds = (double)(end - start) / CLOCKS_PER_SEC; |
Yann Collet | 9d90922 | 2015-12-17 14:09:55 +0100 | [diff] [blame] | 436 | DISPLAYLEVEL(4, "Completed in %.2f sec \n", seconds); |
Yann Collet | f6f3d75 | 2015-12-13 13:35:21 +0100 | [diff] [blame] | 437 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 438 | |
Yann Collet | 9d90922 | 2015-12-17 14:09:55 +0100 | [diff] [blame] | 439 | return issueWithSrcFile; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | |
Yann Collet | 9d90922 | 2015-12-17 14:09:55 +0100 | [diff] [blame] | 443 | int FIO_compressMultipleFilenames(const char** inFileNamesTable, unsigned nbFiles, |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 444 | const char* suffix, |
| 445 | const char* dictFileName, int compressionLevel) |
| 446 | { |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 447 | int missed_files = 0; |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 448 | char* dstFileName = (char*)malloc(FNSPACE); |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 449 | size_t dfnSize = FNSPACE; |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 450 | size_t const suffixSize = suffix ? strlen(suffix) : 0; |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 451 | cRess_t ress; |
| 452 | |
| 453 | /* init */ |
| 454 | ress = FIO_createCResources(dictFileName); |
| 455 | |
| 456 | /* loop on each file */ |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 457 | if (!strcmp(suffix, stdoutmark)) { |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 458 | unsigned u; |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 459 | ress.dstFile = stdout; |
| 460 | for (u=0; u<nbFiles; u++) |
| 461 | missed_files += FIO_compressFilename_srcFile(ress, stdoutmark, |
Yann Collet | 8a1d1a6 | 2016-03-10 21:02:25 +0100 | [diff] [blame] | 462 | inFileNamesTable[u], compressionLevel); |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 463 | if (fclose(ress.dstFile)) EXM_THROW(29, "Write error : cannot properly close %s", stdoutmark); |
| 464 | } else { |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 465 | unsigned u; |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 466 | for (u=0; u<nbFiles; u++) { |
| 467 | size_t ifnSize = strlen(inFileNamesTable[u]); |
| 468 | if (dfnSize <= ifnSize+suffixSize+1) { free(dstFileName); dfnSize = ifnSize + 20; dstFileName = (char*)malloc(dfnSize); } |
| 469 | strcpy(dstFileName, inFileNamesTable[u]); |
| 470 | strcat(dstFileName, suffix); |
| 471 | missed_files += FIO_compressFilename_extRess(ress, dstFileName, |
| 472 | inFileNamesTable[u], compressionLevel); |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 473 | } } |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 474 | |
| 475 | /* Close & Free */ |
| 476 | FIO_freeCResources(ress); |
| 477 | free(dstFileName); |
| 478 | |
| 479 | return missed_files; |
| 480 | } |
| 481 | |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 482 | #endif /* #ifndef ZSTD_NOCOMPRESS */ |
inikep | 3c7c352 | 2016-04-22 13:59:05 +0200 | [diff] [blame] | 483 | |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 484 | |
inikep | db39643 | 2016-04-22 18:22:30 +0200 | [diff] [blame] | 485 | |
| 486 | #ifndef ZSTD_NODECOMPRESS |
| 487 | |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 488 | /* ************************************************************************** |
| 489 | * Decompression |
| 490 | ****************************************************************************/ |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 491 | typedef struct { |
| 492 | void* srcBuffer; |
| 493 | size_t srcBufferSize; |
| 494 | void* dstBuffer; |
| 495 | size_t dstBufferSize; |
| 496 | void* dictBuffer; |
| 497 | size_t dictBufferSize; |
| 498 | ZBUFF_DCtx* dctx; |
Yann Collet | 1f1f239 | 2016-02-12 18:33:26 +0100 | [diff] [blame] | 499 | FILE* dstFile; |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 500 | } dRess_t; |
| 501 | |
| 502 | static dRess_t FIO_createDResources(const char* dictFileName) |
| 503 | { |
| 504 | dRess_t ress; |
| 505 | |
| 506 | /* init */ |
| 507 | ress.dctx = ZBUFF_createDCtx(); |
| 508 | if (ress.dctx==NULL) EXM_THROW(60, "Can't create ZBUFF decompression context"); |
| 509 | |
| 510 | /* Allocate Memory */ |
| 511 | ress.srcBufferSize = ZBUFF_recommendedDInSize(); |
| 512 | ress.srcBuffer = malloc(ress.srcBufferSize); |
| 513 | ress.dstBufferSize = ZBUFF_recommendedDOutSize(); |
| 514 | ress.dstBuffer = malloc(ress.dstBufferSize); |
| 515 | if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(61, "Allocation error : not enough memory"); |
| 516 | |
| 517 | /* dictionary */ |
| 518 | ress.dictBufferSize = FIO_loadFile(&(ress.dictBuffer), dictFileName); |
| 519 | |
| 520 | return ress; |
| 521 | } |
| 522 | |
| 523 | static void FIO_freeDResources(dRess_t ress) |
| 524 | { |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 525 | size_t const errorCode = ZBUFF_freeDCtx(ress.dctx); |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 526 | if (ZBUFF_isError(errorCode)) EXM_THROW(69, "Error : can't free ZBUFF context resource : %s", ZBUFF_getErrorName(errorCode)); |
| 527 | free(ress.srcBuffer); |
| 528 | free(ress.dstBuffer); |
| 529 | free(ress.dictBuffer); |
| 530 | } |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 531 | |
| 532 | |
Yann Collet | 09b21ee | 2016-03-15 12:56:03 +0100 | [diff] [blame] | 533 | /** FIO_decompressFrame() : |
| 534 | @return : size of decoded frame |
| 535 | */ |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 536 | unsigned long long FIO_decompressFrame(dRess_t ress, |
| 537 | FILE* foutput, FILE* finput, size_t alreadyLoaded) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 538 | { |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 539 | U64 frameSize = 0; |
Yann Collet | 09b21ee | 2016-03-15 12:56:03 +0100 | [diff] [blame] | 540 | size_t readSize; |
| 541 | |
| 542 | ZBUFF_decompressInitDictionary(ress.dctx, ress.dictBuffer, ress.dictBufferSize); |
| 543 | |
| 544 | /* Complete Header loading */ |
| 545 | { size_t const toLoad = ZSTD_frameHeaderSize_max - alreadyLoaded; /* assumption : alreadyLoaded <= ZSTD_frameHeaderSize_max */ |
| 546 | size_t const checkSize = fread(((char*)ress.srcBuffer) + alreadyLoaded, 1, toLoad, finput); |
Yann Collet | 9e8b09a | 2016-04-07 19:35:23 +0200 | [diff] [blame] | 547 | if (checkSize != toLoad) EXM_THROW(32, "Read error"); /* assumption : srcSize >= ZSTD_frameHeaderSize_max */ |
Yann Collet | 09b21ee | 2016-03-15 12:56:03 +0100 | [diff] [blame] | 548 | } |
| 549 | readSize = ZSTD_frameHeaderSize_max; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 550 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 551 | /* Main decompression Loop */ |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 552 | while (1) { |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 553 | /* Decode */ |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 554 | size_t inSize=readSize, decodedSize=ress.dstBufferSize; |
| 555 | size_t toRead = ZBUFF_decompressContinue(ress.dctx, ress.dstBuffer, &decodedSize, ress.srcBuffer, &inSize); |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 556 | 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] | 557 | readSize -= inSize; |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 558 | |
| 559 | /* Write block */ |
Yann Collet | 09b21ee | 2016-03-15 12:56:03 +0100 | [diff] [blame] | 560 | { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, decodedSize, foutput); |
Yann Collet | 9e8b09a | 2016-04-07 19:35:23 +0200 | [diff] [blame] | 561 | if (sizeCheck != decodedSize) EXM_THROW(37, "Write error : unable to write data block into destination"); } |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 562 | frameSize += decodedSize; |
| 563 | DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(frameSize>>20) ); |
| 564 | |
Yann Collet | 09b21ee | 2016-03-15 12:56:03 +0100 | [diff] [blame] | 565 | if (toRead == 0) break; /* end of frame */ |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 566 | if (readSize) EXM_THROW(38, "Decoding error : should consume entire input"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 567 | |
| 568 | /* Fill input buffer */ |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 569 | if (toRead > ress.srcBufferSize) EXM_THROW(34, "too large block"); |
| 570 | readSize = fread(ress.srcBuffer, 1, toRead, finput); |
Yann Collet | 09b21ee | 2016-03-15 12:56:03 +0100 | [diff] [blame] | 571 | if (readSize != toRead) |
| 572 | EXM_THROW(35, "Read error"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 573 | } |
| 574 | |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 575 | return frameSize; |
Yann Collet | be50aaa | 2015-09-10 23:26:09 +0100 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | |
Yann Collet | 1f1f239 | 2016-02-12 18:33:26 +0100 | [diff] [blame] | 579 | /** FIO_decompressSrcFile() : |
| 580 | Decompression `srcFileName` into `ress.dstFile` |
| 581 | @return : 0 : OK |
| 582 | 1 : operation not started |
| 583 | */ |
| 584 | static int FIO_decompressSrcFile(dRess_t ress, const char* srcFileName) |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 585 | { |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 586 | unsigned long long filesize = 0; |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 587 | FILE* const dstFile = ress.dstFile; |
| 588 | FILE* const srcFile = FIO_openSrcFile(srcFileName); |
Yann Collet | 1f1f239 | 2016-02-12 18:33:26 +0100 | [diff] [blame] | 589 | if (srcFile==0) return 1; |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 590 | |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 591 | /* for each frame */ |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 592 | for ( ; ; ) { |
Yann Collet | a85a8dd | 2015-11-30 11:53:11 +0100 | [diff] [blame] | 593 | /* check magic number -> version */ |
Yann Collet | 9e8b09a | 2016-04-07 19:35:23 +0200 | [diff] [blame] | 594 | size_t const toRead = 4; |
| 595 | size_t const sizeCheck = fread(ress.srcBuffer, (size_t)1, toRead, srcFile); |
Yann Collet | a85a8dd | 2015-11-30 11:53:11 +0100 | [diff] [blame] | 596 | if (sizeCheck==0) break; /* no more input */ |
Yann Collet | 1f1f239 | 2016-02-12 18:33:26 +0100 | [diff] [blame] | 597 | if (sizeCheck != toRead) EXM_THROW(31, "zstd: %s read error : cannot read header", srcFileName); |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 598 | { U32 const magic = MEM_readLE32(ress.srcBuffer); |
| 599 | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) |
| 600 | if (ZSTD_isLegacy(magic)) { |
| 601 | filesize += FIO_decompressLegacyFrame(dstFile, srcFile, magic); |
| 602 | continue; |
| 603 | } |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 604 | #endif /* ZSTD_LEGACY_SUPPORT */ |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 605 | if (magic != ZSTD_MAGICNUMBER) { |
| 606 | DISPLAYLEVEL(1, "zstd: %s: not in zstd format \n", srcFileName); |
| 607 | return 1; |
| 608 | } } |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 609 | filesize += FIO_decompressFrame(ress, dstFile, srcFile, toRead); |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 610 | } |
| 611 | |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 612 | /* Final Status */ |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 613 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 614 | DISPLAYLEVEL(2, "Successfully decoded %llu bytes \n", filesize); |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 615 | |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 616 | /* Close */ |
| 617 | fclose(srcFile); |
Yann Collet | 1f1f239 | 2016-02-12 18:33:26 +0100 | [diff] [blame] | 618 | return 0; |
| 619 | } |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 620 | |
Yann Collet | 1f1f239 | 2016-02-12 18:33:26 +0100 | [diff] [blame] | 621 | |
| 622 | /** FIO_decompressFile_extRess() : |
| 623 | decompress `srcFileName` into `dstFileName` |
| 624 | @return : 0 : OK |
| 625 | 1 : operation aborted (src not available, dst already taken, etc.) |
| 626 | */ |
| 627 | static int FIO_decompressFile_extRess(dRess_t ress, |
| 628 | const char* dstFileName, const char* srcFileName) |
| 629 | { |
Chip Turner | 6de382c | 2016-03-13 22:24:46 -0700 | [diff] [blame] | 630 | int result; |
Yann Collet | 1f1f239 | 2016-02-12 18:33:26 +0100 | [diff] [blame] | 631 | ress.dstFile = FIO_openDstFile(dstFileName); |
| 632 | if (ress.dstFile==0) return 1; |
| 633 | |
Chip Turner | 6de382c | 2016-03-13 22:24:46 -0700 | [diff] [blame] | 634 | result = FIO_decompressSrcFile(ress, srcFileName); |
| 635 | if (result != 0) { |
Chip Turner | 9da7f86 | 2016-03-14 07:44:59 -0700 | [diff] [blame] | 636 | remove(dstFileName); |
Chip Turner | 6de382c | 2016-03-13 22:24:46 -0700 | [diff] [blame] | 637 | } |
Yann Collet | 1f1f239 | 2016-02-12 18:33:26 +0100 | [diff] [blame] | 638 | |
| 639 | if (fclose(ress.dstFile)) EXM_THROW(38, "Write error : cannot properly close %s", dstFileName); |
Chip Turner | 6de382c | 2016-03-13 22:24:46 -0700 | [diff] [blame] | 640 | return result; |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 644 | int FIO_decompressFilename(const char* dstFileName, const char* srcFileName, |
| 645 | const char* dictFileName) |
| 646 | { |
| 647 | int missingFiles = 0; |
| 648 | dRess_t ress = FIO_createDResources(dictFileName); |
| 649 | |
| 650 | missingFiles += FIO_decompressFile_extRess(ress, dstFileName, srcFileName); |
| 651 | |
| 652 | FIO_freeDResources(ress); |
| 653 | return missingFiles; |
| 654 | } |
| 655 | |
| 656 | |
| 657 | #define MAXSUFFIXSIZE 8 |
| 658 | int FIO_decompressMultipleFilenames(const char** srcNamesTable, unsigned nbFiles, |
| 659 | const char* suffix, |
| 660 | const char* dictFileName) |
| 661 | { |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 662 | int skippedFiles = 0; |
| 663 | int missingFiles = 0; |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 664 | char* dstFileName = (char*)malloc(FNSPACE); |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 665 | size_t dfnSize = FNSPACE; |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 666 | size_t const suffixSize = suffix ? strlen(suffix) : 0; |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 667 | dRess_t ress; |
| 668 | |
| 669 | if (dstFileName==NULL) EXM_THROW(70, "not enough memory for dstFileName"); |
| 670 | ress = FIO_createDResources(dictFileName); |
| 671 | |
Yann Collet | accfd80 | 2016-02-15 19:33:16 +0100 | [diff] [blame] | 672 | if (!strcmp(suffix, stdoutmark) || !strcmp(suffix, nulmark)) { |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 673 | unsigned u; |
Yann Collet | accfd80 | 2016-02-15 19:33:16 +0100 | [diff] [blame] | 674 | ress.dstFile = FIO_openDstFile(suffix); |
| 675 | if (ress.dstFile == 0) EXM_THROW(71, "cannot open %s", suffix); |
| 676 | for (u=0; u<nbFiles; u++) |
| 677 | missingFiles += FIO_decompressSrcFile(ress, srcNamesTable[u]); |
| 678 | if (fclose(ress.dstFile)) EXM_THROW(39, "Write error : cannot properly close %s", stdoutmark); |
| 679 | } else { |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame^] | 680 | unsigned u; |
Yann Collet | 1f1f239 | 2016-02-12 18:33:26 +0100 | [diff] [blame] | 681 | for (u=0; u<nbFiles; u++) { /* create dstFileName */ |
| 682 | const char* srcFileName = srcNamesTable[u]; |
| 683 | size_t sfnSize = strlen(srcFileName); |
| 684 | const char* suffixPtr = srcFileName + sfnSize - suffixSize; |
Yann Collet | accfd80 | 2016-02-15 19:33:16 +0100 | [diff] [blame] | 685 | if (dfnSize+suffixSize <= sfnSize+1) { |
| 686 | free(dstFileName); |
| 687 | dfnSize = sfnSize + 20; |
| 688 | dstFileName = (char*)malloc(dfnSize); |
| 689 | if (dstFileName==NULL) EXM_THROW(71, "not enough memory for dstFileName"); |
| 690 | } |
| 691 | if (sfnSize <= suffixSize || strcmp(suffixPtr, suffix) != 0) { |
Yann Collet | 1f1f239 | 2016-02-12 18:33:26 +0100 | [diff] [blame] | 692 | DISPLAYLEVEL(1, "zstd: %s: unknown suffix (%4s expected) -- ignored \n", srcFileName, suffix); |
| 693 | skippedFiles++; |
| 694 | continue; |
| 695 | } |
| 696 | memcpy(dstFileName, srcFileName, sfnSize - suffixSize); |
| 697 | dstFileName[sfnSize-suffixSize] = '\0'; |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 698 | |
Yann Collet | 1f1f239 | 2016-02-12 18:33:26 +0100 | [diff] [blame] | 699 | missingFiles += FIO_decompressFile_extRess(ress, dstFileName, srcFileName); |
Yann Collet | accfd80 | 2016-02-15 19:33:16 +0100 | [diff] [blame] | 700 | } } |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 701 | |
| 702 | FIO_freeDResources(ress); |
| 703 | free(dstFileName); |
| 704 | return missingFiles + skippedFiles; |
| 705 | } |
Yann Collet | accfd80 | 2016-02-15 19:33:16 +0100 | [diff] [blame] | 706 | |
inikep | db39643 | 2016-04-22 18:22:30 +0200 | [diff] [blame] | 707 | #endif // #ifndef ZSTD_NODECOMPRESS |