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