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