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