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