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