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