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