blob: e6671308c3e8ce0267586b107678a2bf20747f2e [file] [log] [blame]
Yann Collet4ded9e52016-08-30 10:04:33 -07001/**
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 Collet4856a002015-01-24 01:58:16 +01009
Yann Collet8dafb1a2017-01-25 17:01:13 -080010
Yann Colleteeb8ba12015-10-22 16:55:40 +010011/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010012* Compiler Options
Yann Colleteeb8ba12015-10-22 16:55:40 +010013***************************************/
Yann Collet94ca85d2016-08-14 01:19:12 +020014#ifdef _MSC_VER /* Visual */
Przemyslaw Skibinskie6797412016-12-21 13:47:11 +010015# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
Yann Collet94ca85d2016-08-14 01:19:12 +020016# pragma warning(disable : 4204) /* non-constant aggregate initializer */
17#endif
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010018#if defined(__MINGW32__) && !defined(_POSIX_SOURCE)
19# define _POSIX_SOURCE 1 /* disable %llu warnings with MinGW on Windows */
20#endif
Yann Collet4856a002015-01-24 01:58:16 +010021
Yann Collet179b1972016-11-02 17:30:49 -070022
Yann Collet6f3acba2016-02-12 20:19:48 +010023/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010024* Includes
Yann Colleteeb8ba12015-10-22 16:55:40 +010025***************************************/
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +010026#include "platform.h" /* Large Files support, SET_BINARY_MODE */
27#include "util.h" /* UTIL_getFileSize */
Yann Collet9f432922015-11-09 17:42:17 +010028#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 */
inikepd5ff2c32016-04-28 14:40:45 +020033
Sean Purcell279be202017-04-06 12:56:40 -070034#if defined (_MSC_VER)
35# include <sys/stat.h>
36# include <io.h>
37#endif
38
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010039#include "mem.h"
Yann Collet4856a002015-01-24 01:58:16 +010040#include "fileio.h"
Yann Colletd3b7f8d2016-06-04 19:47:02 +020041#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_magicNumber, ZSTD_frameHeaderSize_max */
42#include "zstd.h"
Yann Collet500014a2017-01-19 16:59:56 -080043#ifdef ZSTD_MULTITHREAD
44# include "zstdmt_compress.h"
Przemyslaw Skibinski6b508b12016-12-05 18:02:40 +010045#endif
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +010046#if defined(ZSTD_GZCOMPRESS) || defined(ZSTD_GZDECOMPRESS)
Nick Terrell2cb8ee82017-02-06 11:32:13 -080047# include <zlib.h>
Yann Collet500014a2017-01-19 16:59:56 -080048# if !defined(z_const)
49# define z_const
50# endif
Przemyslaw Skibinskiabfb51f2016-11-30 15:05:54 +010051#endif
Nick Terrellaa8bcf32017-03-13 18:11:07 -070052#if defined(ZSTD_LZMACOMPRESS) || defined(ZSTD_LZMADECOMPRESS)
53# include <lzma.h>
54#endif
Yann Collet4856a002015-01-24 01:58:16 +010055
Sean Purcell4de86322017-04-24 16:48:25 -070056#define LZ4_MAGICNUMBER 0x184D2204
57#if defined(ZSTD_LZ4COMPRESS) || defined(ZSTD_LZ4DECOMPRESS)
58# include <lz4frame.h>
Sean Purcell2c4b6fe2017-04-25 11:00:54 -070059# include <lz4.h>
Sean Purcell4de86322017-04-24 16:48:25 -070060#endif
61
Yann Collet4856a002015-01-24 01:58:16 +010062
Yann Collet6f3acba2016-02-12 20:19:48 +010063/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010064* Constants
Yann Colleteeb8ba12015-10-22 16:55:40 +010065***************************************/
Yann Collet92d75662016-07-03 01:10:53 +020066#define KB *(1<<10)
67#define MB *(1<<20)
68#define GB *(1U<<30)
69
Yann Collet4856a002015-01-24 01:58:16 +010070#define _1BIT 0x01
71#define _2BITS 0x03
72#define _3BITS 0x07
73#define _4BITS 0x0F
74#define _6BITS 0x3F
75#define _8BITS 0xFF
76
Yann Collet88fcd292015-11-25 14:42:45 +010077#define BLOCKSIZE (128 KB)
78#define ROLLBUFFERSIZE (BLOCKSIZE*8*64)
Yann Collet4856a002015-01-24 01:58:16 +010079
Yann Collet6f3acba2016-02-12 20:19:48 +010080#define FIO_FRAMEHEADERSIZE 5 /* as a define, because needed to allocated table on stack */
81#define FSE_CHECKSUM_SEED 0
Yann Collet4856a002015-01-24 01:58:16 +010082
83#define CACHELINE 64
84
Yann Collet0e300592017-04-11 14:41:02 -070085#define DICTSIZE_MAX (32 MB) /* protection against large input (attack scenario) */
Yann Collet6f3acba2016-02-12 20:19:48 +010086
inikep3c7c3522016-04-22 13:59:05 +020087#define FNSPACE 30
88
Yann Collet4856a002015-01-24 01:58:16 +010089
Yann Collet459a6b72016-02-15 20:37:23 +010090/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010091* Macros
Yann Colleteeb8ba12015-10-22 16:55:40 +010092***************************************/
Yann Collet4856a002015-01-24 01:58:16 +010093#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
Paul Cruze2089922017-06-15 12:27:32 -070094#define DISPLAYOUT(...) fprintf(stdout, __VA_ARGS__)
Yann Collet5d9b8942017-01-27 13:30:18 -080095#define DISPLAYLEVEL(l, ...) { if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } }
Sean Purcell042ba122017-03-23 11:13:52 -070096static int g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
Yann Collet8a1d1a62016-03-10 21:02:25 +010097void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; }
Yann Collet4856a002015-01-24 01:58:16 +010098
Yann Collet5d9b8942017-01-27 13:30:18 -080099#define DISPLAYUPDATE(l, ...) { if (g_displayLevel>=l) { \
Yann Colletb71adf42016-07-02 01:05:31 +0200100 if ((clock() - g_time > refreshRate) || (g_displayLevel>=4)) \
Yann Collet4856a002015-01-24 01:58:16 +0100101 { g_time = clock(); DISPLAY(__VA_ARGS__); \
Sean Purcell042ba122017-03-23 11:13:52 -0700102 if (g_displayLevel>=4) fflush(stderr); } } }
Yann Colletb71adf42016-07-02 01:05:31 +0200103static const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100;
Yann Collet4856a002015-01-24 01:58:16 +0100104static clock_t g_time = 0;
105
Sean Purcell42bac7f2017-04-13 15:35:05 -0700106#undef MIN
107#define MIN(a,b) ((a) < (b) ? (a) : (b))
Yann Collet92d75662016-07-03 01:10:53 +0200108
Yann Collet31533ba2017-04-27 00:29:04 -0700109
Przemyslaw Skibinskibf336572017-02-14 09:45:33 +0100110/* ************************************************************
111* Avoid fseek()'s 2GiB barrier with MSVC, MacOS, *BSD, MinGW
112***************************************************************/
ds77168d9b82017-02-12 10:27:18 +0100113#if defined(_MSC_VER) && _MSC_VER >= 1400
114# define LONG_SEEK _fseeki64
Przemyslaw Skibinskibf336572017-02-14 09:45:33 +0100115#elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
Przemyslaw Skibinskice13d082017-02-14 09:52:52 +0100116# define LONG_SEEK fseeko
ds77168d9b82017-02-12 10:27:18 +0100117#elif defined(__MINGW32__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS) && defined(__MSVCRT__)
118# define LONG_SEEK fseeko64
119#elif defined(_WIN32) && !defined(__DJGPP__)
120# include <windows.h>
121 static int LONG_SEEK(FILE* file, __int64 offset, int origin) {
122 LARGE_INTEGER off;
123 DWORD method;
124 off.QuadPart = offset;
125 if (origin == SEEK_END)
126 method = FILE_END;
127 else if (origin == SEEK_CUR)
128 method = FILE_CURRENT;
129 else
130 method = FILE_BEGIN;
131
132 if (SetFilePointerEx((HANDLE) _get_osfhandle(_fileno(file)), off, NULL, method))
ds776220bfc2017-02-13 12:00:59 +0100133 return 0;
ds77168d9b82017-02-12 10:27:18 +0100134 else
ds776220bfc2017-02-13 12:00:59 +0100135 return -1;
ds77168d9b82017-02-12 10:27:18 +0100136 }
137#else
138# define LONG_SEEK fseek
139#endif
140
Yann Collet459a6b72016-02-15 20:37:23 +0100141
142/*-*************************************
Yann Colletb71adf42016-07-02 01:05:31 +0200143* Local Parameters - Not thread safe
Yann Colleteeb8ba12015-10-22 16:55:40 +0100144***************************************/
Przemyslaw Skibinski98509a72017-02-14 09:23:32 +0100145static FIO_compressionType_t g_compressionType = FIO_zstdCompression;
146void FIO_setCompressionType(FIO_compressionType_t compressionType) { g_compressionType = compressionType; }
Yann Collet4856a002015-01-24 01:58:16 +0100147static U32 g_overwrite = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100148void FIO_overwriteMode(void) { g_overwrite=1; }
Yann Collet75424d12016-05-23 16:56:56 +0200149static U32 g_sparseFileSupport = 1; /* 0 : no sparse allowed; 1: auto (file yes, stdout no); 2: force sparse */
150void FIO_setSparseWrite(unsigned sparse) { g_sparseFileSupport=sparse; }
Yann Collet6381e992016-05-31 02:29:45 +0200151static U32 g_dictIDFlag = 1;
152void FIO_setDictIDFlag(unsigned dictIDFlag) { g_dictIDFlag = dictIDFlag; }
Yann Colletc98f8e72016-06-20 16:31:24 +0200153static U32 g_checksumFlag = 1;
Yann Collet87cfbe32016-06-01 19:22:15 +0200154void FIO_setChecksumFlag(unsigned checksumFlag) { g_checksumFlag = checksumFlag; }
Yann Colletb09b12c2016-06-09 22:59:51 +0200155static U32 g_removeSrcFile = 0;
156void FIO_setRemoveSrcFile(unsigned flag) { g_removeSrcFile = (flag>0); }
Yann Colletd4cda272016-10-14 13:13:13 -0700157static U32 g_memLimit = 0;
158void FIO_setMemLimit(unsigned memLimit) { g_memLimit = memLimit; }
Yann Collet500014a2017-01-19 16:59:56 -0800159static U32 g_nbThreads = 1;
160void FIO_setNbThreads(unsigned nbThreads) {
161#ifndef ZSTD_MULTITHREAD
162 if (nbThreads > 1) DISPLAYLEVEL(2, "Note : multi-threading is disabled \n");
163#endif
164 g_nbThreads = nbThreads;
165}
Yann Collet512cbe82017-01-24 17:02:26 -0800166static U32 g_blockSize = 0;
167void FIO_setBlockSize(unsigned blockSize) {
168 if (blockSize && g_nbThreads==1)
169 DISPLAYLEVEL(2, "Setting block size is useless in single-thread mode \n");
170#ifdef ZSTD_MULTITHREAD
171 if (blockSize-1 < ZSTDMT_SECTION_SIZE_MIN-1) /* intentional underflow */
172 DISPLAYLEVEL(2, "Note : minimum block size is %u KB \n", (ZSTDMT_SECTION_SIZE_MIN>>10));
173#endif
174 g_blockSize = blockSize;
175}
Yann Collet8d8513f2017-01-30 14:37:08 -0800176#define FIO_OVERLAP_LOG_NOTSET 9999
177static U32 g_overlapLog = FIO_OVERLAP_LOG_NOTSET;
Yann Collet6be23372017-01-30 11:17:26 -0800178void FIO_setOverlapLog(unsigned overlapLog){
179 if (overlapLog && g_nbThreads==1)
180 DISPLAYLEVEL(2, "Setting overlapLog is useless in single-thread mode \n");
181 g_overlapLog = overlapLog;
182}
Yann Collet4856a002015-01-24 01:58:16 +0100183
184
Yann Collet459a6b72016-02-15 20:37:23 +0100185/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +0100186* Exceptions
Yann Colleteeb8ba12015-10-22 16:55:40 +0100187***************************************/
188#ifndef DEBUG
189# define DEBUG 0
190#endif
Yann Collet4856a002015-01-24 01:58:16 +0100191#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
192#define EXM_THROW(error, ...) \
193{ \
194 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
195 DISPLAYLEVEL(1, "Error %i : ", error); \
196 DISPLAYLEVEL(1, __VA_ARGS__); \
Yann Colletcdff19c2016-11-11 17:26:54 -0800197 DISPLAYLEVEL(1, " \n"); \
Yann Collet4856a002015-01-24 01:58:16 +0100198 exit(error); \
199}
200
201
Yann Collet459a6b72016-02-15 20:37:23 +0100202/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +0100203* Functions
Yann Colleteeb8ba12015-10-22 16:55:40 +0100204***************************************/
Sean Purcell279be202017-04-06 12:56:40 -0700205/** FIO_remove() :
206 * @result : Unlink `fileName`, even if it's read-only */
207static int FIO_remove(const char* path)
208{
209#if defined(_WIN32) || defined(WIN32)
Yann Collet01a1abf2017-05-05 19:15:24 -0700210 /* windows doesn't allow remove read-only files,
211 * so try to make it writable first */
Sean Purcell279be202017-04-06 12:56:40 -0700212 chmod(path, _S_IWRITE);
213#endif
214 return remove(path);
215}
216
Yann Colletcdff19c2016-11-11 17:26:54 -0800217/** FIO_openSrcFile() :
218 * condition : `dstFileName` must be non-NULL.
219 * @result : FILE* to `dstFileName`, or NULL if it fails */
Yann Colletf0624362016-02-12 15:56:46 +0100220static FILE* FIO_openSrcFile(const char* srcFileName)
221{
222 FILE* f;
223
224 if (!strcmp (srcFileName, stdinmark)) {
225 DISPLAYLEVEL(4,"Using stdin for input\n");
226 f = stdin;
227 SET_BINARY_MODE(stdin);
228 } else {
Przemyslaw Skibinski442c75f2017-02-14 09:38:51 +0100229 if (!UTIL_isRegFile(srcFileName)) {
Przemyslaw Skibinski5022a182017-01-25 13:11:26 +0100230 DISPLAYLEVEL(1, "zstd: %s is not a regular file -- ignored \n", srcFileName);
231 return NULL;
232 }
Yann Colletf0624362016-02-12 15:56:46 +0100233 f = fopen(srcFileName, "rb");
Yann Colletfb5e3852016-08-13 20:49:47 +0200234 if ( f==NULL ) DISPLAYLEVEL(1, "zstd: %s: %s \n", srcFileName, strerror(errno));
Yann Colletf0624362016-02-12 15:56:46 +0100235 }
236
Yann Colletf0624362016-02-12 15:56:46 +0100237 return f;
238}
239
Yann Collet43eeea42016-09-15 15:38:44 +0200240/** FIO_openDstFile() :
241 * condition : `dstFileName` must be non-NULL.
242 * @result : FILE* to `dstFileName`, or NULL if it fails */
Yann Colletf0624362016-02-12 15:56:46 +0100243static FILE* FIO_openDstFile(const char* dstFileName)
244{
245 FILE* f;
246
247 if (!strcmp (dstFileName, stdoutmark)) {
248 DISPLAYLEVEL(4,"Using stdout for output\n");
249 f = stdout;
250 SET_BINARY_MODE(stdout);
Yann Collet75424d12016-05-23 16:56:56 +0200251 if (g_sparseFileSupport==1) {
252 g_sparseFileSupport = 0;
253 DISPLAYLEVEL(4, "Sparse File Support is automatically disabled on stdout ; try --sparse \n");
254 }
Yann Colletf0624362016-02-12 15:56:46 +0100255 } else {
Nick Terrelleb386172017-03-31 15:20:50 -0700256 if (g_sparseFileSupport == 1) {
257 g_sparseFileSupport = ZSTD_SPARSE_DEFAULT;
Nick Terrell96fe5452017-03-31 15:16:43 -0700258 }
Sean Purcell279be202017-04-06 12:56:40 -0700259 if (strcmp (dstFileName, nulmark)) { /* Check if destination file already exists */
Yann Colletf0624362016-02-12 15:56:46 +0100260 f = fopen( dstFileName, "rb" );
261 if (f != 0) { /* dest file exists, prompt for overwrite authorization */
262 fclose(f);
Sean Purcell279be202017-04-06 12:56:40 -0700263 if (!g_overwrite) {
264 if (g_displayLevel <= 1) {
265 /* No interaction possible */
266 DISPLAY("zstd: %s already exists; not overwritten \n", dstFileName);
Yann Colletb71adf42016-07-02 01:05:31 +0200267 return NULL;
Yann Colletf0624362016-02-12 15:56:46 +0100268 }
Sean Purcell279be202017-04-06 12:56:40 -0700269 DISPLAY("zstd: %s already exists; do you wish to overwrite (y/N) ? ", dstFileName);
270 { int ch = getchar();
271 if ((ch!='Y') && (ch!='y')) {
272 DISPLAY(" not overwritten \n");
273 return NULL;
274 }
275 while ((ch!=EOF) && (ch!='\n')) ch = getchar(); /* flush rest of input line */
276 }
277 }
278
279 /* need to unlink */
280 FIO_remove(dstFileName);
281 } }
Yann Colletf0624362016-02-12 15:56:46 +0100282 f = fopen( dstFileName, "wb" );
Yann Colletfb5e3852016-08-13 20:49:47 +0200283 if (f==NULL) DISPLAYLEVEL(1, "zstd: %s: %s\n", dstFileName, strerror(errno));
Yann Colletf0624362016-02-12 15:56:46 +0100284 }
Yann Colletb71adf42016-07-02 01:05:31 +0200285
Yann Colletf0624362016-02-12 15:56:46 +0100286 return f;
287}
288
289
Yann Collet0e300592017-04-11 14:41:02 -0700290/*! FIO_createDictBuffer() :
291 * creates a buffer, pointed by `*bufferPtr`,
292 * loads `filename` content into it, up to DICTSIZE_MAX bytes.
293 * @return : loaded size
294 * if fileName==NULL, returns 0 and a NULL pointer
295 */
296static size_t FIO_createDictBuffer(void** bufferPtr, const char* fileName)
Yann Colletdeb078b2015-12-17 20:30:14 +0100297{
298 FILE* fileHandle;
Yann Colletdeb078b2015-12-17 20:30:14 +0100299 U64 fileSize;
300
301 *bufferPtr = NULL;
Yann Collet2ce49232016-02-02 14:36:49 +0100302 if (fileName == NULL) return 0;
Yann Colletdeb078b2015-12-17 20:30:14 +0100303
304 DISPLAYLEVEL(4,"Loading %s as dictionary \n", fileName);
305 fileHandle = fopen(fileName, "rb");
Yann Colletb71adf42016-07-02 01:05:31 +0200306 if (fileHandle==0) EXM_THROW(31, "zstd: %s: %s", fileName, strerror(errno));
inikep69fcd7c2016-04-28 12:23:33 +0200307 fileSize = UTIL_getFileSize(fileName);
Yann Collet0e300592017-04-11 14:41:02 -0700308 if (fileSize > DICTSIZE_MAX) EXM_THROW(32, "Dictionary file %s is too large (> %u MB)", fileName, DICTSIZE_MAX >> 20); /* avoid extreme cases */
Yann Colletb71adf42016-07-02 01:05:31 +0200309 *bufferPtr = malloc((size_t)fileSize);
Yann Colleted7fb842016-07-02 11:14:30 +0200310 if (*bufferPtr==NULL) EXM_THROW(34, "zstd: %s", strerror(errno));
Yann Collet6fca9e72016-05-31 02:40:42 +0200311 { size_t const readSize = fread(*bufferPtr, 1, (size_t)fileSize, fileHandle);
312 if (readSize!=fileSize) EXM_THROW(35, "Error reading dictionary file %s", fileName); }
Yann Colletdeb078b2015-12-17 20:30:14 +0100313 fclose(fileHandle);
314 return (size_t)fileSize;
315}
316
inikep3c7c3522016-04-22 13:59:05 +0200317#ifndef ZSTD_NOCOMPRESS
Yann Collet4f137032015-12-17 02:23:58 +0100318
Yann Collet8a1d1a62016-03-10 21:02:25 +0100319/*-**********************************************************************
Yann Collet4f137032015-12-17 02:23:58 +0100320* Compression
321************************************************************************/
322typedef struct {
Yann Collet500014a2017-01-19 16:59:56 -0800323 FILE* srcFile;
324 FILE* dstFile;
Yann Collet4f137032015-12-17 02:23:58 +0100325 void* srcBuffer;
326 size_t srcBufferSize;
327 void* dstBuffer;
328 size_t dstBufferSize;
Yann Collet500014a2017-01-19 16:59:56 -0800329#ifdef ZSTD_MULTITHREAD
330 ZSTDMT_CCtx* cctx;
331#else
Yann Collet6263ba52016-08-13 23:45:45 +0200332 ZSTD_CStream* cctx;
Yann Collet500014a2017-01-19 16:59:56 -0800333#endif
Yann Collet4f137032015-12-17 02:23:58 +0100334} cRess_t;
335
Yann Collet500014a2017-01-19 16:59:56 -0800336static cRess_t FIO_createCResources(const char* dictFileName, int cLevel,
Sean Purcell0f5c95a2017-02-07 16:33:48 -0800337 U64 srcSize, int srcRegFile,
338 ZSTD_compressionParameters* comprParams) {
Yann Collet4f137032015-12-17 02:23:58 +0100339 cRess_t ress;
Yann Collet5e80dd32016-07-13 17:38:39 +0200340 memset(&ress, 0, sizeof(ress));
Yann Collet4f137032015-12-17 02:23:58 +0100341
Yann Collet500014a2017-01-19 16:59:56 -0800342#ifdef ZSTD_MULTITHREAD
343 ress.cctx = ZSTDMT_createCCtx(g_nbThreads);
Yann Collete12ae022017-05-16 17:32:33 -0700344 if (ress.cctx == NULL)
345 EXM_THROW(30, "zstd: allocation error : can't create ZSTD_CStream");
Yann Collet8d8513f2017-01-30 14:37:08 -0800346 if ((cLevel==ZSTD_maxCLevel()) && (g_overlapLog==FIO_OVERLAP_LOG_NOTSET))
Yann Collete12ae022017-05-16 17:32:33 -0700347 /* use complete window for overlap */
348 ZSTDMT_setMTCtxParameter(ress.cctx, ZSTDMT_p_overlapSectionLog, 9);
Yann Collet8d8513f2017-01-30 14:37:08 -0800349 if (g_overlapLog != FIO_OVERLAP_LOG_NOTSET)
Yann Collet6be23372017-01-30 11:17:26 -0800350 ZSTDMT_setMTCtxParameter(ress.cctx, ZSTDMT_p_overlapSectionLog, g_overlapLog);
Yann Collet500014a2017-01-19 16:59:56 -0800351#else
Yann Collet6263ba52016-08-13 23:45:45 +0200352 ress.cctx = ZSTD_createCStream();
Yann Collete12ae022017-05-16 17:32:33 -0700353 if (ress.cctx == NULL)
354 EXM_THROW(30, "zstd: allocation error : can't create ZSTD_CStream");
Yann Collet8dafb1a2017-01-25 17:01:13 -0800355#endif
Yann Collet6263ba52016-08-13 23:45:45 +0200356 ress.srcBufferSize = ZSTD_CStreamInSize();
Yann Collet4f137032015-12-17 02:23:58 +0100357 ress.srcBuffer = malloc(ress.srcBufferSize);
Yann Collet6263ba52016-08-13 23:45:45 +0200358 ress.dstBufferSize = ZSTD_CStreamOutSize();
Yann Collet4f137032015-12-17 02:23:58 +0100359 ress.dstBuffer = malloc(ress.dstBufferSize);
Yann Collete12ae022017-05-16 17:32:33 -0700360 if (!ress.srcBuffer || !ress.dstBuffer)
361 EXM_THROW(31, "zstd: allocation error : not enough memory");
Yann Collet4f137032015-12-17 02:23:58 +0100362
363 /* dictionary */
Yann Collet43eeea42016-09-15 15:38:44 +0200364 { void* dictBuffer;
Yann Collet5c42d0e2017-04-11 16:57:32 -0700365 size_t const dictBuffSize = FIO_createDictBuffer(&dictBuffer, dictFileName); /* works with dictFileName==NULL */
Yann Collete12ae022017-05-16 17:32:33 -0700366 if (dictFileName && (dictBuffer==NULL))
367 EXM_THROW(32, "zstd: allocation error : can't create dictBuffer");
Yann Collet993060e2016-09-21 16:46:08 +0200368 { ZSTD_parameters params = ZSTD_getParams(cLevel, srcSize, dictBuffSize);
Sean Purcell0f5c95a2017-02-07 16:33:48 -0800369 params.fParams.contentSizeFlag = srcRegFile;
Yann Collet43eeea42016-09-15 15:38:44 +0200370 params.fParams.checksumFlag = g_checksumFlag;
371 params.fParams.noDictIDFlag = !g_dictIDFlag;
Przemyslaw Skibinski8349d672016-12-13 13:24:59 +0100372 if (comprParams->windowLog) params.cParams.windowLog = comprParams->windowLog;
373 if (comprParams->chainLog) params.cParams.chainLog = comprParams->chainLog;
374 if (comprParams->hashLog) params.cParams.hashLog = comprParams->hashLog;
375 if (comprParams->searchLog) params.cParams.searchLog = comprParams->searchLog;
376 if (comprParams->searchLength) params.cParams.searchLength = comprParams->searchLength;
377 if (comprParams->targetLength) params.cParams.targetLength = comprParams->targetLength;
Yann Collete12ae022017-05-16 17:32:33 -0700378 if (comprParams->strategy) params.cParams.strategy = (ZSTD_strategy) comprParams->strategy;
Yann Collet500014a2017-01-19 16:59:56 -0800379#ifdef ZSTD_MULTITHREAD
380 { size_t const errorCode = ZSTDMT_initCStream_advanced(ress.cctx, dictBuffer, dictBuffSize, params, srcSize);
Yann Collet512cbe82017-01-24 17:02:26 -0800381 if (ZSTD_isError(errorCode)) EXM_THROW(33, "Error initializing CStream : %s", ZSTD_getErrorName(errorCode));
382 ZSTDMT_setMTCtxParameter(ress.cctx, ZSTDMT_p_sectionSize, g_blockSize);
Yann Collet500014a2017-01-19 16:59:56 -0800383#else
Yann Collet993060e2016-09-21 16:46:08 +0200384 { size_t const errorCode = ZSTD_initCStream_advanced(ress.cctx, dictBuffer, dictBuffSize, params, srcSize);
Yann Collet43eeea42016-09-15 15:38:44 +0200385 if (ZSTD_isError(errorCode)) EXM_THROW(33, "Error initializing CStream : %s", ZSTD_getErrorName(errorCode));
Yann Collet512cbe82017-01-24 17:02:26 -0800386#endif
Yann Collet43eeea42016-09-15 15:38:44 +0200387 } }
388 free(dictBuffer);
389 }
Yann Collet4f137032015-12-17 02:23:58 +0100390
391 return ress;
392}
393
394static void FIO_freeCResources(cRess_t ress)
395{
Yann Collet4f137032015-12-17 02:23:58 +0100396 free(ress.srcBuffer);
397 free(ress.dstBuffer);
Yann Collet500014a2017-01-19 16:59:56 -0800398#ifdef ZSTD_MULTITHREAD
399 ZSTDMT_freeCCtx(ress.cctx);
400#else
Yann Collet43eeea42016-09-15 15:38:44 +0200401 ZSTD_freeCStream(ress.cctx); /* never fails */
Yann Collet500014a2017-01-19 16:59:56 -0800402#endif
Yann Collet4f137032015-12-17 02:23:58 +0100403}
404
405
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100406#ifdef ZSTD_GZCOMPRESS
Yann Collete12ae022017-05-16 17:32:33 -0700407static unsigned long long FIO_compressGzFrame(cRess_t* ress,
408 const char* srcFileName, U64 const srcFileSize,
409 int compressionLevel, U64* readsize)
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100410{
411 unsigned long long inFileSize = 0, outFileSize = 0;
412 z_stream strm;
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100413 int ret;
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100414
Yann Collete12ae022017-05-16 17:32:33 -0700415 if (compressionLevel > Z_BEST_COMPRESSION)
416 compressionLevel = Z_BEST_COMPRESSION;
Przemyslaw Skibinski64f72212017-02-13 21:00:41 +0100417
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100418 strm.zalloc = Z_NULL;
419 strm.zfree = Z_NULL;
420 strm.opaque = Z_NULL;
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100421
Yann Collete12ae022017-05-16 17:32:33 -0700422 ret = deflateInit2(&strm, compressionLevel, Z_DEFLATED,
423 15 /* maxWindowLogSize */ + 16 /* gzip only */,
424 8, Z_DEFAULT_STRATEGY); /* see http://www.zlib.net/manual.html */
425 if (ret != Z_OK)
426 EXM_THROW(71, "zstd: %s: deflateInit2 error %d \n", srcFileName, ret);
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100427
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100428 strm.next_in = 0;
Przemyslaw Skibinski862698f2017-02-27 13:21:05 +0100429 strm.avail_in = 0;
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100430 strm.next_out = (Bytef*)ress->dstBuffer;
431 strm.avail_out = (uInt)ress->dstBufferSize;
432
433 while (1) {
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100434 if (strm.avail_in == 0) {
435 size_t const inSize = fread(ress->srcBuffer, 1, ress->srcBufferSize, ress->srcFile);
436 if (inSize == 0) break;
437 inFileSize += inSize;
438 strm.next_in = (z_const unsigned char*)ress->srcBuffer;
439 strm.avail_in = (uInt)inSize;
440 }
441 ret = deflate(&strm, Z_NO_FLUSH);
Yann Collete12ae022017-05-16 17:32:33 -0700442 if (ret != Z_OK)
443 EXM_THROW(72, "zstd: %s: deflate error %d \n", srcFileName, ret);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100444 { size_t const decompBytes = ress->dstBufferSize - strm.avail_out;
445 if (decompBytes) {
Yann Collete12ae022017-05-16 17:32:33 -0700446 if (fwrite(ress->dstBuffer, 1, decompBytes, ress->dstFile) != decompBytes)
447 EXM_THROW(73, "Write error : cannot write to output file");
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100448 outFileSize += decompBytes;
449 strm.next_out = (Bytef*)ress->dstBuffer;
450 strm.avail_out = (uInt)ress->dstBufferSize;
451 }
452 }
Yann Collete12ae022017-05-16 17:32:33 -0700453 if (!srcFileSize)
454 DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%%",
455 (U32)(inFileSize>>20),
456 (double)outFileSize/inFileSize*100)
457 else
458 DISPLAYUPDATE(2, "\rRead : %u / %u MB ==> %.2f%%",
459 (U32)(inFileSize>>20), (U32)(srcFileSize>>20),
460 (double)outFileSize/inFileSize*100);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100461 }
462
463 while (1) {
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100464 ret = deflate(&strm, Z_FINISH);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100465 { size_t const decompBytes = ress->dstBufferSize - strm.avail_out;
466 if (decompBytes) {
Yann Collete12ae022017-05-16 17:32:33 -0700467 if (fwrite(ress->dstBuffer, 1, decompBytes, ress->dstFile) != decompBytes)
468 EXM_THROW(75, "Write error : cannot write to output file");
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100469 outFileSize += decompBytes;
470 strm.next_out = (Bytef*)ress->dstBuffer;
471 strm.avail_out = (uInt)ress->dstBufferSize;
Yann Collete12ae022017-05-16 17:32:33 -0700472 } }
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100473 if (ret == Z_STREAM_END) break;
Yann Collete12ae022017-05-16 17:32:33 -0700474 if (ret != Z_BUF_ERROR)
475 EXM_THROW(77, "zstd: %s: deflate error %d \n", srcFileName, ret);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100476 }
477
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100478 ret = deflateEnd(&strm);
Yann Collete12ae022017-05-16 17:32:33 -0700479 if (ret != Z_OK)
480 EXM_THROW(79, "zstd: %s: deflateEnd error %d \n", srcFileName, ret);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100481 *readsize = inFileSize;
482
483 return outFileSize;
484}
485#endif
486
487
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700488#ifdef ZSTD_LZMACOMPRESS
Yann Collete12ae022017-05-16 17:32:33 -0700489static unsigned long long FIO_compressLzmaFrame(cRess_t* ress,
490 const char* srcFileName, U64 const srcFileSize,
491 int compressionLevel, U64* readsize, int plain_lzma)
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700492{
493 unsigned long long inFileSize = 0, outFileSize = 0;
494 lzma_stream strm = LZMA_STREAM_INIT;
495 lzma_action action = LZMA_RUN;
496 lzma_ret ret;
497
498 if (compressionLevel < 0) compressionLevel = 0;
499 if (compressionLevel > 9) compressionLevel = 9;
500
501 if (plain_lzma) {
502 lzma_options_lzma opt_lzma;
Yann Collete12ae022017-05-16 17:32:33 -0700503 if (lzma_lzma_preset(&opt_lzma, compressionLevel))
504 EXM_THROW(71, "zstd: %s: lzma_lzma_preset error", srcFileName);
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700505 ret = lzma_alone_encoder(&strm, &opt_lzma); /* LZMA */
Yann Collete12ae022017-05-16 17:32:33 -0700506 if (ret != LZMA_OK)
507 EXM_THROW(71, "zstd: %s: lzma_alone_encoder error %d", srcFileName, ret);
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700508 } else {
509 ret = lzma_easy_encoder(&strm, compressionLevel, LZMA_CHECK_CRC64); /* XZ */
Yann Collete12ae022017-05-16 17:32:33 -0700510 if (ret != LZMA_OK)
511 EXM_THROW(71, "zstd: %s: lzma_easy_encoder error %d", srcFileName, ret);
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700512 }
513
514 strm.next_in = 0;
515 strm.avail_in = 0;
516 strm.next_out = ress->dstBuffer;
517 strm.avail_out = ress->dstBufferSize;
518
519 while (1) {
520 if (strm.avail_in == 0) {
521 size_t const inSize = fread(ress->srcBuffer, 1, ress->srcBufferSize, ress->srcFile);
522 if (inSize == 0) action = LZMA_FINISH;
523 inFileSize += inSize;
524 strm.next_in = ress->srcBuffer;
525 strm.avail_in = inSize;
526 }
527
528 ret = lzma_code(&strm, action);
529
Yann Collete12ae022017-05-16 17:32:33 -0700530 if (ret != LZMA_OK && ret != LZMA_STREAM_END)
531 EXM_THROW(72, "zstd: %s: lzma_code encoding error %d", srcFileName, ret);
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700532 { size_t const compBytes = ress->dstBufferSize - strm.avail_out;
533 if (compBytes) {
Yann Collete12ae022017-05-16 17:32:33 -0700534 if (fwrite(ress->dstBuffer, 1, compBytes, ress->dstFile) != compBytes)
535 EXM_THROW(73, "Write error : cannot write to output file");
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700536 outFileSize += compBytes;
537 strm.next_out = ress->dstBuffer;
538 strm.avail_out = ress->dstBufferSize;
Yann Collete12ae022017-05-16 17:32:33 -0700539 } }
540 if (!srcFileSize)
541 DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%%",
542 (U32)(inFileSize>>20),
543 (double)outFileSize/inFileSize*100)
544 else
545 DISPLAYUPDATE(2, "\rRead : %u / %u MB ==> %.2f%%",
546 (U32)(inFileSize>>20), (U32)(srcFileSize>>20),
547 (double)outFileSize/inFileSize*100);
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700548 if (ret == LZMA_STREAM_END) break;
549 }
550
551 lzma_end(&strm);
552 *readsize = inFileSize;
553
554 return outFileSize;
555}
556#endif
557
Sean Purcell4de86322017-04-24 16:48:25 -0700558#ifdef ZSTD_LZ4COMPRESS
559static int FIO_LZ4_GetBlockSize_FromBlockId (int id) { return (1 << (8 + (2 * id))); }
Yann Collete12ae022017-05-16 17:32:33 -0700560static unsigned long long FIO_compressLz4Frame(cRess_t* ress,
561 const char* srcFileName, U64 const srcFileSize,
562 int compressionLevel, U64* readsize)
Sean Purcell4de86322017-04-24 16:48:25 -0700563{
564 unsigned long long inFileSize = 0, outFileSize = 0;
565
566 LZ4F_preferences_t prefs;
Sean Purcell2c4b6fe2017-04-25 11:00:54 -0700567 LZ4F_compressionContext_t ctx;
Sean Purcell4de86322017-04-24 16:48:25 -0700568
569 LZ4F_errorCode_t const errorCode = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION);
Yann Collete12ae022017-05-16 17:32:33 -0700570 if (LZ4F_isError(errorCode))
571 EXM_THROW(31, "zstd: failed to create lz4 compression context");
Sean Purcell4de86322017-04-24 16:48:25 -0700572
573 memset(&prefs, 0, sizeof(prefs));
574
Sean Purcelleab41c12017-04-26 10:17:38 -0700575#if LZ4_VERSION_NUMBER <= 10600
576#define LZ4F_blockIndependent blockIndependent
577#define LZ4F_max4MB max4MB
578#endif
579
Sean Purcell4de86322017-04-24 16:48:25 -0700580 prefs.autoFlush = 1;
581 prefs.compressionLevel = compressionLevel;
Sean Purcelleab41c12017-04-26 10:17:38 -0700582 prefs.frameInfo.blockMode = LZ4F_blockIndependent; /* stick to defaults for lz4 cli */
583 prefs.frameInfo.blockSizeID = LZ4F_max4MB;
Sean Purcell2c4b6fe2017-04-25 11:00:54 -0700584 prefs.frameInfo.contentChecksumFlag = (contentChecksum_t)g_checksumFlag;
585#if LZ4_VERSION_NUMBER >= 10600
Sean Purcell4de86322017-04-24 16:48:25 -0700586 prefs.frameInfo.contentSize = srcFileSize;
Sean Purcell2c4b6fe2017-04-25 11:00:54 -0700587#endif
Sean Purcell4de86322017-04-24 16:48:25 -0700588
589 {
Sean Purcelleab41c12017-04-26 10:17:38 -0700590 size_t blockSize = FIO_LZ4_GetBlockSize_FromBlockId(LZ4F_max4MB);
Sean Purcell4de86322017-04-24 16:48:25 -0700591 size_t readSize;
592 size_t headerSize = LZ4F_compressBegin(ctx, ress->dstBuffer, ress->dstBufferSize, &prefs);
Yann Collete12ae022017-05-16 17:32:33 -0700593 if (LZ4F_isError(headerSize))
594 EXM_THROW(33, "File header generation failed : %s",
595 LZ4F_getErrorName(headerSize));
Sean Purcell4de86322017-04-24 16:48:25 -0700596 { size_t const sizeCheck = fwrite(ress->dstBuffer, 1, headerSize, ress->dstFile);
597 if (sizeCheck!=headerSize) EXM_THROW(34, "Write error : cannot write header"); }
598 outFileSize += headerSize;
599
600 /* Read first block */
601 readSize = fread(ress->srcBuffer, (size_t)1, (size_t)blockSize, ress->srcFile);
602 inFileSize += readSize;
603
604 /* Main Loop */
605 while (readSize>0) {
606 size_t outSize;
607
608 /* Compress Block */
609 outSize = LZ4F_compressUpdate(ctx, ress->dstBuffer, ress->dstBufferSize, ress->srcBuffer, readSize, NULL);
Yann Collete12ae022017-05-16 17:32:33 -0700610 if (LZ4F_isError(outSize))
611 EXM_THROW(35, "zstd: %s: lz4 compression failed : %s",
612 srcFileName, LZ4F_getErrorName(outSize));
Sean Purcell4de86322017-04-24 16:48:25 -0700613 outFileSize += outSize;
Yann Collete12ae022017-05-16 17:32:33 -0700614 if (!srcFileSize)
615 DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%%",
616 (U32)(inFileSize>>20),
617 (double)outFileSize/inFileSize*100)
618 else
619 DISPLAYUPDATE(2, "\rRead : %u / %u MB ==> %.2f%%",
620 (U32)(inFileSize>>20), (U32)(srcFileSize>>20),
621 (double)outFileSize/inFileSize*100);
Sean Purcell4de86322017-04-24 16:48:25 -0700622
623 /* Write Block */
624 { size_t const sizeCheck = fwrite(ress->dstBuffer, 1, outSize, ress->dstFile);
625 if (sizeCheck!=outSize) EXM_THROW(36, "Write error : cannot write compressed block"); }
626
627 /* Read next block */
628 readSize = fread(ress->srcBuffer, (size_t)1, (size_t)blockSize, ress->srcFile);
629 inFileSize += readSize;
630 }
631 if (ferror(ress->srcFile)) EXM_THROW(37, "Error reading %s ", srcFileName);
632
633 /* End of Stream mark */
634 headerSize = LZ4F_compressEnd(ctx, ress->dstBuffer, ress->dstBufferSize, NULL);
Yann Collete12ae022017-05-16 17:32:33 -0700635 if (LZ4F_isError(headerSize))
636 EXM_THROW(38, "zstd: %s: lz4 end of file generation failed : %s",
637 srcFileName, LZ4F_getErrorName(headerSize));
Sean Purcell4de86322017-04-24 16:48:25 -0700638
639 { size_t const sizeCheck = fwrite(ress->dstBuffer, 1, headerSize, ress->dstFile);
640 if (sizeCheck!=headerSize) EXM_THROW(39, "Write error : cannot write end of stream"); }
641 outFileSize += headerSize;
642 }
643
644 *readsize = inFileSize;
645 LZ4F_freeCompressionContext(ctx);
646
647 return outFileSize;
648}
649#endif
650
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700651
Yann Colletf0624362016-02-12 15:56:46 +0100652/*! FIO_compressFilename_internal() :
Yann Collet8a1d1a62016-03-10 21:02:25 +0100653 * same as FIO_compressFilename_extRess(), with `ress.desFile` already opened.
Yann Colletf0624362016-02-12 15:56:46 +0100654 * @return : 0 : compression completed correctly,
655 * 1 : missing or pb opening srcFileName
Yann Collet4f137032015-12-17 02:23:58 +0100656 */
Yann Colletf0624362016-02-12 15:56:46 +0100657static int FIO_compressFilename_internal(cRess_t ress,
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100658 const char* dstFileName, const char* srcFileName, int compressionLevel)
Yann Collet4f137032015-12-17 02:23:58 +0100659{
Yann Colletf8494622016-05-07 22:43:40 +0200660 FILE* const srcFile = ress.srcFile;
661 FILE* const dstFile = ress.dstFile;
Yann Colletb44be742016-03-26 20:52:14 +0100662 U64 readsize = 0;
Yann Collet4f137032015-12-17 02:23:58 +0100663 U64 compressedfilesize = 0;
inikep69fcd7c2016-04-28 12:23:33 +0200664 U64 const fileSize = UTIL_getFileSize(srcFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100665
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700666 switch (g_compressionType) {
667 case FIO_zstdCompression:
668 break;
Yann Collet2e63a872017-05-02 15:40:42 -0700669
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700670 case FIO_gzipCompression:
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100671#ifdef ZSTD_GZCOMPRESS
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700672 compressedfilesize = FIO_compressGzFrame(&ress, srcFileName, fileSize, compressionLevel, &readsize);
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100673#else
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700674 (void)compressionLevel;
Yann Collete12ae022017-05-16 17:32:33 -0700675 EXM_THROW(20, "zstd: %s: file cannot be compressed as gzip (zstd compiled without ZSTD_GZCOMPRESS) -- ignored \n",
676 srcFileName);
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100677#endif
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700678 goto finish;
Yann Collet2e63a872017-05-02 15:40:42 -0700679
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700680 case FIO_xzCompression:
681 case FIO_lzmaCompression:
682#ifdef ZSTD_LZMACOMPRESS
683 compressedfilesize = FIO_compressLzmaFrame(&ress, srcFileName, fileSize, compressionLevel, &readsize, g_compressionType==FIO_lzmaCompression);
684#else
685 (void)compressionLevel;
Yann Collete12ae022017-05-16 17:32:33 -0700686 EXM_THROW(20, "zstd: %s: file cannot be compressed as xz/lzma (zstd compiled without ZSTD_LZMACOMPRESS) -- ignored \n",
687 srcFileName);
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700688#endif
Yann Collet2e63a872017-05-02 15:40:42 -0700689 goto finish;
690
Sean Purcell4de86322017-04-24 16:48:25 -0700691 case FIO_lz4Compression:
692#ifdef ZSTD_LZ4COMPRESS
693 compressedfilesize = FIO_compressLz4Frame(&ress, srcFileName, fileSize, compressionLevel, &readsize);
694#else
695 (void)compressionLevel;
Yann Collete12ae022017-05-16 17:32:33 -0700696 EXM_THROW(20, "zstd: %s: file cannot be compressed as lz4 (zstd compiled without ZSTD_LZ4COMPRESS) -- ignored \n",
697 srcFileName);
Sean Purcell4de86322017-04-24 16:48:25 -0700698#endif
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700699 goto finish;
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100700 }
701
Yann Collet4f137032015-12-17 02:23:58 +0100702 /* init */
Yann Collet500014a2017-01-19 16:59:56 -0800703#ifdef ZSTD_MULTITHREAD
704 { size_t const resetError = ZSTDMT_resetCStream(ress.cctx, fileSize);
705#else
Yann Collet43eeea42016-09-15 15:38:44 +0200706 { size_t const resetError = ZSTD_resetCStream(ress.cctx, fileSize);
Yann Collet500014a2017-01-19 16:59:56 -0800707#endif
Yann Collete12ae022017-05-16 17:32:33 -0700708 if (ZSTD_isError(resetError))
709 EXM_THROW(21, "Error initializing compression : %s",
710 ZSTD_getErrorName(resetError));
Yann Collet43eeea42016-09-15 15:38:44 +0200711 }
Yann Collet4f137032015-12-17 02:23:58 +0100712
713 /* Main compression loop */
Yann Collet1c8e1942016-01-26 16:31:22 +0100714 while (1) {
Yann Collet4f137032015-12-17 02:23:58 +0100715 /* Fill input Buffer */
Yann Colletb44be742016-03-26 20:52:14 +0100716 size_t const inSize = fread(ress.srcBuffer, (size_t)1, ress.srcBufferSize, srcFile);
Yann Collet4f137032015-12-17 02:23:58 +0100717 if (inSize==0) break;
Yann Colletb44be742016-03-26 20:52:14 +0100718 readsize += inSize;
Yann Collet4f137032015-12-17 02:23:58 +0100719
Yann Collet53e17fb2016-08-17 01:39:22 +0200720 { ZSTD_inBuffer inBuff = { ress.srcBuffer, inSize, 0 };
Yann Collet5c42d0e2017-04-11 16:57:32 -0700721 while (inBuff.pos != inBuff.size) {
722 ZSTD_outBuffer outBuff = { ress.dstBuffer, ress.dstBufferSize, 0 };
Yann Collet500014a2017-01-19 16:59:56 -0800723#ifdef ZSTD_MULTITHREAD
724 size_t const result = ZSTDMT_compressStream(ress.cctx, &outBuff, &inBuff);
725#else
726 size_t const result = ZSTD_compressStream(ress.cctx, &outBuff, &inBuff);
727#endif
Yann Collete12ae022017-05-16 17:32:33 -0700728 if (ZSTD_isError(result))
729 EXM_THROW(23, "Compression error : %s ", ZSTD_getErrorName(result));
Yann Collet4f137032015-12-17 02:23:58 +0100730
Yann Collet943cff92017-01-25 12:31:07 -0800731 /* Write compressed stream */
732 if (outBuff.pos) {
733 size_t const sizeCheck = fwrite(ress.dstBuffer, 1, outBuff.pos, dstFile);
Yann Collete12ae022017-05-16 17:32:33 -0700734 if (sizeCheck!=outBuff.pos)
735 EXM_THROW(25, "Write error : cannot write compressed block into %s", dstFileName);
Yann Collet943cff92017-01-25 12:31:07 -0800736 compressedfilesize += outBuff.pos;
737 } } }
Yann Collet5c42d0e2017-04-11 16:57:32 -0700738 if (g_nbThreads > 1) {
Yann Collete12ae022017-05-16 17:32:33 -0700739 if (!fileSize)
740 DISPLAYUPDATE(2, "\rRead : %u MB", (U32)(readsize>>20))
741 else
742 DISPLAYUPDATE(2, "\rRead : %u / %u MB",
743 (U32)(readsize>>20), (U32)(fileSize>>20));
Yann Collet5c42d0e2017-04-11 16:57:32 -0700744 } else {
Yann Collete12ae022017-05-16 17:32:33 -0700745 if (!fileSize)
746 DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%%",
747 (U32)(readsize>>20),
748 (double)compressedfilesize/readsize*100)
749 else
750 DISPLAYUPDATE(2, "\rRead : %u / %u MB ==> %.2f%%",
751 (U32)(readsize>>20), (U32)(fileSize>>20),
752 (double)compressedfilesize/readsize*100);
Yann Collet5c42d0e2017-04-11 16:57:32 -0700753 }
Yann Collet4f137032015-12-17 02:23:58 +0100754 }
755
756 /* End of Frame */
Yann Collet500014a2017-01-19 16:59:56 -0800757 { size_t result = 1;
758 while (result!=0) { /* note : is there any possibility of endless loop ? */
759 ZSTD_outBuffer outBuff = { ress.dstBuffer, ress.dstBufferSize, 0 };
760#ifdef ZSTD_MULTITHREAD
761 result = ZSTDMT_endStream(ress.cctx, &outBuff);
762#else
763 result = ZSTD_endStream(ress.cctx, &outBuff);
764#endif
765 if (ZSTD_isError(result)) EXM_THROW(26, "Compression error during frame end : %s", ZSTD_getErrorName(result));
766 { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, outBuff.pos, dstFile);
767 if (sizeCheck!=outBuff.pos) EXM_THROW(27, "Write error : cannot write frame end into %s", dstFileName); }
768 compressedfilesize += outBuff.pos;
769 }
Yann Collet4f137032015-12-17 02:23:58 +0100770 }
771
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100772finish:
Yann Collet4f137032015-12-17 02:23:58 +0100773 /* Status */
774 DISPLAYLEVEL(2, "\r%79s\r", "");
David Lam0f270452016-08-13 11:26:21 -0700775 DISPLAYLEVEL(2,"%-20s :%6.2f%% (%6llu => %6llu bytes, %s) \n", srcFileName,
Yann Collet44f684d2016-07-13 19:30:40 +0200776 (double)compressedfilesize/(readsize+(!readsize) /* avoid div by zero */ )*100,
777 (unsigned long long)readsize, (unsigned long long) compressedfilesize,
778 dstFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100779
Yann Collet4f137032015-12-17 02:23:58 +0100780 return 0;
781}
782
783
Yann Colletb71adf42016-07-02 01:05:31 +0200784/*! FIO_compressFilename_srcFile() :
785 * note : ress.destFile already opened
Yann Collet459a6b72016-02-15 20:37:23 +0100786 * @return : 0 : compression completed correctly,
787 * 1 : missing or pb opening srcFileName
788 */
789static int FIO_compressFilename_srcFile(cRess_t ress,
Yann Collete12ae022017-05-16 17:32:33 -0700790 const char* dstFileName, const char* srcFileName,
791 int compressionLevel)
Yann Collet459a6b72016-02-15 20:37:23 +0100792{
793 int result;
794
795 /* File check */
Yann Colletb09b12c2016-06-09 22:59:51 +0200796 if (UTIL_isDirectory(srcFileName)) {
797 DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
798 return 1;
799 }
Przemyslaw Skibinski64fa2db2017-01-25 13:02:33 +0100800
Yann Collet459a6b72016-02-15 20:37:23 +0100801 ress.srcFile = FIO_openSrcFile(srcFileName);
802 if (!ress.srcFile) return 1; /* srcFile could not be opened */
803
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100804 result = FIO_compressFilename_internal(ress, dstFileName, srcFileName, compressionLevel);
Yann Collet459a6b72016-02-15 20:37:23 +0100805
Yann Collet459a6b72016-02-15 20:37:23 +0100806 fclose(ress.srcFile);
Yann Colletdccd6b62017-02-27 15:57:50 -0800807 if (g_removeSrcFile /* --rm */ && !result && strcmp(srcFileName, stdinmark)) {
808 if (remove(srcFileName))
809 EXM_THROW(1, "zstd: %s: %s", srcFileName, strerror(errno));
810 }
Yann Collet459a6b72016-02-15 20:37:23 +0100811 return result;
812}
813
814
Yann Colletb09b12c2016-06-09 22:59:51 +0200815/*! FIO_compressFilename_dstFile() :
Yann Colletf0624362016-02-12 15:56:46 +0100816 * @return : 0 : compression completed correctly,
Yann Colletb09b12c2016-06-09 22:59:51 +0200817 * 1 : pb
Yann Colletf0624362016-02-12 15:56:46 +0100818 */
Yann Colletb09b12c2016-06-09 22:59:51 +0200819static int FIO_compressFilename_dstFile(cRess_t ress,
Yann Collete12ae022017-05-16 17:32:33 -0700820 const char* dstFileName,
821 const char* srcFileName,
822 int compressionLevel)
Yann Colletf0624362016-02-12 15:56:46 +0100823{
824 int result;
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100825 stat_t statbuf;
826 int stat_result = 0;
Yann Colletf0624362016-02-12 15:56:46 +0100827
828 ress.dstFile = FIO_openDstFile(dstFileName);
Yann Collet43eeea42016-09-15 15:38:44 +0200829 if (ress.dstFile==NULL) return 1; /* could not open dstFileName */
Yann Colletf0624362016-02-12 15:56:46 +0100830
Yann Collete12ae022017-05-16 17:32:33 -0700831 if (strcmp (srcFileName, stdinmark) && UTIL_getFileStat(srcFileName, &statbuf))
832 stat_result = 1;
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100833 result = FIO_compressFilename_srcFile(ress, dstFileName, srcFileName, compressionLevel);
Chip Turner6de382c2016-03-13 22:24:46 -0700834
Yann Collete12ae022017-05-16 17:32:33 -0700835 if (fclose(ress.dstFile)) { /* error closing dstFile */
836 DISPLAYLEVEL(1, "zstd: %s: %s \n", dstFileName, strerror(errno));
837 result=1;
838 }
839 if (result!=0) { /* remove operation artefact */
840 if (remove(dstFileName))
841 EXM_THROW(1, "zstd: %s: %s", dstFileName, strerror(errno));
842 }
843 else if (strcmp (dstFileName, stdoutmark) && stat_result)
844 UTIL_setFileStat(dstFileName, &statbuf);
Yann Colletf0624362016-02-12 15:56:46 +0100845 return result;
846}
847
848
Yann Collet9d909222015-12-17 14:09:55 +0100849int FIO_compressFilename(const char* dstFileName, const char* srcFileName,
Przemyslaw Skibinski8349d672016-12-13 13:24:59 +0100850 const char* dictFileName, int compressionLevel, ZSTD_compressionParameters* comprParams)
Yann Collet4856a002015-01-24 01:58:16 +0100851{
Yann Collet6381e992016-05-31 02:29:45 +0200852 clock_t const start = clock();
Yann Collet993060e2016-09-21 16:46:08 +0200853 U64 const srcSize = UTIL_getFileSize(srcFileName);
Sean Purcell0f5c95a2017-02-07 16:33:48 -0800854 int const regFile = UTIL_isRegFile(srcFileName);
Yann Colletb09b12c2016-06-09 22:59:51 +0200855
Sean Purcell0f5c95a2017-02-07 16:33:48 -0800856 cRess_t const ress = FIO_createCResources(dictFileName, compressionLevel, srcSize, regFile, comprParams);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100857 int const result = FIO_compressFilename_dstFile(ress, dstFileName, srcFileName, compressionLevel);
Yann Collet9d909222015-12-17 14:09:55 +0100858
Yann Colletb71adf42016-07-02 01:05:31 +0200859 double const seconds = (double)(clock() - start) / CLOCKS_PER_SEC;
860 DISPLAYLEVEL(4, "Completed in %.2f sec \n", seconds);
861
862 FIO_freeCResources(ress);
863 return result;
Yann Collet4856a002015-01-24 01:58:16 +0100864}
865
Paul Cruz786b7ca2017-06-12 13:46:39 -0700866typedef struct {
867 int numActualFrames;
868 int numSkippableFrames;
Paul Cruzfc428ab2017-06-14 13:26:19 -0700869 unsigned long long decompressedSize;
Paul Cruz6e020442017-06-12 15:51:59 -0700870 int canComputeDecompSize;
Paul Cruzfc428ab2017-06-14 13:26:19 -0700871 unsigned long long compressedSize;
Paul Cruz786b7ca2017-06-12 13:46:39 -0700872 int usesCheck;
873} fileInfo_t;
874
875/*
876 * Reads information from file, stores in *info
877 * if successful, returns 0, otherwise returns 1
878 */
Paul Cruze2089922017-06-15 12:27:32 -0700879static int getFileInfo(fileInfo_t* info, const char* inFileName){
880 FILE* const srcFile = FIO_openSrcFile(inFileName);
881 if (srcFile == NULL) {
882 DISPLAY("Error: could not open source file %s\n", inFileName);
Paul Cruz786b7ca2017-06-12 13:46:39 -0700883 return 1;
884 }
Paul Cruzfc428ab2017-06-14 13:26:19 -0700885 info->compressedSize = (unsigned long long)UTIL_getFileSize(inFileName);
Paul Cruz786b7ca2017-06-12 13:46:39 -0700886 info->decompressedSize = 0;
887 info->numActualFrames = 0;
Paul Cruze2089922017-06-15 12:27:32 -0700888 info->numSkippableFrames = 0;
Paul Cruz6e020442017-06-12 15:51:59 -0700889 info->canComputeDecompSize = 1;
Paul Cruz786b7ca2017-06-12 13:46:39 -0700890 /* begin analyzing frame */
Paul Cruze2089922017-06-15 12:27:32 -0700891 for( ; ; ){
Paul Cruza9b77c82017-06-15 14:13:28 -0700892 BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];
893 size_t const numBytesRead = fread(headerBuffer, 1, sizeof(headerBuffer), srcFile);
894 if (numBytesRead < ZSTD_frameHeaderSize_min) {
895 if(feof(srcFile)){
896 DISPLAY("ran out of files\n");
897 break;
898 }
899 else{
900 DISPLAY("Error: did not reach end of file but ran out of frames\n");
Paul Cruze2089922017-06-15 12:27:32 -0700901 fclose(srcFile);
Paul Cruze2089922017-06-15 12:27:32 -0700902 return 1;
Paul Cruz618a7b62017-06-12 16:53:50 -0700903 }
Paul Cruza9b77c82017-06-15 14:13:28 -0700904 }
905 U32 const magicNumber = MEM_readLE32(headerBuffer);
906 if (magicNumber == ZSTD_MAGICNUMBER) {
907 U64 const frameContentSize = ZSTD_getFrameContentSize(headerBuffer, numBytesRead);
908 if (frameContentSize == ZSTD_CONTENTSIZE_ERROR || frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN) {
909 info->canComputeDecompSize = 0;
910 DISPLAY("could not compute decompressed size\n");
911 }
912 else {
913 info->decompressedSize += frameContentSize;
914 }
915 size_t const headerSize = ZSTD_frameHeaderSize(headerBuffer, numBytesRead);
916 if (ZSTD_isError(headerSize)) {
Paul Cruza9b77c82017-06-15 14:13:28 -0700917 DISPLAY("Error: could not determine frame header size\n");
Paul Cruz4495e9a2017-06-15 15:02:54 -0700918 fclose(srcFile);
Paul Cruza9b77c82017-06-15 14:13:28 -0700919 return 1;
920 }
Paul Cruz9cb602e2017-06-12 15:22:48 -0700921
Paul Cruze2089922017-06-15 12:27:32 -0700922 {
Paul Cruza9b77c82017-06-15 14:13:28 -0700923 /* go back to the beginning of the frame */
924 int const ret = fseek(srcFile, -numBytesRead, SEEK_CUR);
925 if (ret != 0) {
926 DISPLAY("Error: could not rewind to beginning of frame\n");
Paul Cruze2089922017-06-15 12:27:32 -0700927 fclose(srcFile);
928 return 1;
929 }
930 }
Paul Cruz9cb602e2017-06-12 15:22:48 -0700931
Paul Cruz81fa33b2017-06-13 10:06:49 -0700932 {
Paul Cruza9b77c82017-06-15 14:13:28 -0700933 /* skip frame header */
934 int const ret = fseek(srcFile, headerSize, SEEK_CUR);
935 if (ret != 0) {
936 DISPLAY("Error: could not skip header\n");
937 fclose(srcFile);
938 return 1;
Paul Cruz81fa33b2017-06-13 10:06:49 -0700939 }
Paul Cruz9cb602e2017-06-12 15:22:48 -0700940 }
941
942 /* skip the rest of the blocks in the frame */
Paul Cruza9b77c82017-06-15 14:13:28 -0700943 {
944 int lastBlock = 0;
945 int readBytes = 0;
946 do{
947 BYTE blockHeaderBuffer[3];
948 U32 blockHeader;
949 int blockSize;
950 readBytes = fread(blockHeaderBuffer, 1, 3, srcFile);
951 if (readBytes != 3) {
952 DISPLAY("There was a problem reading the block header\n");
953 fclose(srcFile);
954 return 1;
955 }
956 blockHeader = MEM_readLE24(blockHeaderBuffer);
957 lastBlock = blockHeader & 1;
958 blockSize = (blockHeader - (blockHeader & 7)) >> 3;
959 fseek(srcFile, blockSize, SEEK_CUR);
960 }while (lastBlock != 1);
961 }
Paul Cruz81fa33b2017-06-13 10:06:49 -0700962 {
963 /* check if checksum is used */
Paul Cruza9b77c82017-06-15 14:13:28 -0700964 BYTE frameHeaderDescriptor = headerBuffer[4];
Paul Cruz81fa33b2017-06-13 10:06:49 -0700965 int contentChecksumFlag = (frameHeaderDescriptor & (1 << 2)) >> 2;
Paul Cruze2089922017-06-15 12:27:32 -0700966 if (contentChecksumFlag) {
Paul Cruz81fa33b2017-06-13 10:06:49 -0700967 info->usesCheck = 1;
968 }
Paul Cruze2089922017-06-15 12:27:32 -0700969 if (contentChecksumFlag) {
Paul Cruz81fa33b2017-06-13 10:06:49 -0700970 fseek(srcFile, 4, SEEK_CUR);
971 }
Paul Cruz9cb602e2017-06-12 15:22:48 -0700972 }
Paul Cruza9b77c82017-06-15 14:13:28 -0700973 info->numActualFrames++;
Paul Cruz9cb602e2017-06-12 15:22:48 -0700974 }
Paul Cruze2089922017-06-15 12:27:32 -0700975 else if (magicNumber == ZSTD_MAGIC_SKIPPABLE_START) {
Paul Cruz9cb602e2017-06-12 15:22:48 -0700976 BYTE frameSizeBuffer[4];
Paul Cruz28dbf8e2017-06-12 17:09:08 -0700977 long frameSize;
Paul Cruz618a7b62017-06-12 16:53:50 -0700978 size_t readBytes = fread(frameSizeBuffer, 1, 4, srcFile);
Paul Cruzc828b522017-06-12 17:19:53 -0700979 info->numSkippableFrames++;
Paul Cruze2089922017-06-15 12:27:32 -0700980 if (readBytes != 4) {
Paul Cruz618a7b62017-06-12 16:53:50 -0700981 DISPLAY("There was an error reading skippable frame size");
982 exit(1);
983 }
Paul Cruz28dbf8e2017-06-12 17:09:08 -0700984 frameSize = MEM_readLE32(frameSizeBuffer);
Paul Cruza9b77c82017-06-15 14:13:28 -0700985 {
986 int const ret = fseek(srcFile, frameSize, SEEK_CUR);
987 if (ret != 0) {
988 DISPLAY("Error: could not find end of skippable frame\n");
989 fclose(srcFile);
990 return 1;
991 }
992 }
Paul Cruz9cb602e2017-06-12 15:22:48 -0700993 fseek(srcFile, frameSize, SEEK_CUR);
Paul Cruza9b77c82017-06-15 14:13:28 -0700994 info->numSkippableFrames++;
Paul Cruz9cb602e2017-06-12 15:22:48 -0700995 }
Paul Cruz786b7ca2017-06-12 13:46:39 -0700996 }
Paul Cruze2089922017-06-15 12:27:32 -0700997 fclose(srcFile);
Paul Cruz786b7ca2017-06-12 13:46:39 -0700998 return 0;
999}
Paul Cruz81fa33b2017-06-13 10:06:49 -07001000void displayInfo(const char* inFileName, fileInfo_t* info, int displayLevel){
Paul Cruze2089922017-06-15 12:27:32 -07001001 double const compressedSizeMB = (double)info->compressedSize/(1 MB);
1002 double const decompressedSizeMB = (double)info->decompressedSize/(1 MB);
Paul Cruz81fa33b2017-06-13 10:06:49 -07001003
1004 if(displayLevel<=2){
1005 if(info->usesCheck && info->canComputeDecompSize){
Paul Cruza9b77c82017-06-15 14:13:28 -07001006 DISPLAYOUT("Skippable Non-Skippable Compressed Uncompressed Ratio Check Filename\n");
1007 DISPLAYOUT("%9d %13d %7.2f MB %7.2f MB %5.3f XXH64 %s\n",
Paul Cruz81fa33b2017-06-13 10:06:49 -07001008 info->numSkippableFrames, info->numActualFrames, compressedSizeMB, decompressedSizeMB,
1009 compressedSizeMB/decompressedSizeMB, inFileName);
1010 }
1011 else if(!info->usesCheck){
Paul Cruza9b77c82017-06-15 14:13:28 -07001012 DISPLAYOUT("Skippable Non-Skippable Compressed Uncompressed Ratio Check Filename\n");
1013 DISPLAYOUT("%9d %13d %7.2f MB %7.2f MB %5.3f %s\n",
Paul Cruz81fa33b2017-06-13 10:06:49 -07001014 info->numSkippableFrames, info->numActualFrames, compressedSizeMB, decompressedSizeMB,
1015 compressedSizeMB/decompressedSizeMB, inFileName);
1016 }
1017 else if(!info->canComputeDecompSize){
Paul Cruza9b77c82017-06-15 14:13:28 -07001018 DISPLAYOUT("Skippable Non-Skippable Compressed Uncompressed Ratio Check Filename\n");
1019 DISPLAYOUT("%9d %13d %7.2f MB XXH64 %s\n",
Paul Cruz81fa33b2017-06-13 10:06:49 -07001020 info->numSkippableFrames, info->numActualFrames, compressedSizeMB, inFileName);
1021 }
1022 else{
Paul Cruza9b77c82017-06-15 14:13:28 -07001023 DISPLAYOUT("Skippable Non-Skippable Filename\n");
1024 DISPLAYOUT("%9d %13d %7.2f MB %s\n",
Paul Cruz81fa33b2017-06-13 10:06:49 -07001025 info->numSkippableFrames, info->numActualFrames, compressedSizeMB, inFileName);
1026 }
1027 }
1028 else{
Paul Cruza9b77c82017-06-15 14:13:28 -07001029 DISPLAYOUT("# Zstandard Frames: %d\n", info->numActualFrames);
1030 DISPLAYOUT("# Skippable Frames: %d\n", info->numSkippableFrames);
1031 DISPLAYOUT("Compressed Size: %.2f MB (%llu B)\n", compressedSizeMB, info->compressedSize);
1032 if (info->canComputeDecompSize) {
1033 DISPLAYOUT("Decompressed Size: %.2f MB (%llu B)\n", decompressedSizeMB, info->decompressedSize);
1034 DISPLAYOUT("Ratio: %.4f\n", compressedSizeMB/decompressedSizeMB);
Paul Cruz81fa33b2017-06-13 10:06:49 -07001035 }
Paul Cruza9b77c82017-06-15 14:13:28 -07001036 if (info->usesCheck) {
1037 DISPLAYOUT("Check: XXH64\n");
Paul Cruz81fa33b2017-06-13 10:06:49 -07001038 }
1039 }
Paul Cruz4495e9a2017-06-15 15:02:54 -07001040
1041 DISPLAYOUT("\n");
Paul Cruz81fa33b2017-06-13 10:06:49 -07001042}
Paul Cruz786b7ca2017-06-12 13:46:39 -07001043
1044int FIO_listFile(const char* inFileName, int displayLevel){
Paul Cruz81fa33b2017-06-13 10:06:49 -07001045 DISPLAY("File: %s\n", inFileName);
Paul Cruz4495e9a2017-06-15 15:02:54 -07001046 fileInfo_t* info = (fileInfo_t*)malloc(sizeof(fileInfo_t));
1047 int error = getFileInfo(info, inFileName);
1048 if (error == 1) {
1049 DISPLAY("An error occurred with getting file info\n");
Paul Cruza3d54cf2017-06-12 10:58:34 -07001050 return 1;
Paul Cruz4128f672017-06-05 15:00:06 -07001051 }
Paul Cruz4495e9a2017-06-15 15:02:54 -07001052 displayInfo(inFileName, info, displayLevel);
1053 free(info);
Paul Cruz901435e2017-06-05 14:45:31 -07001054 return 0;
1055}
Yann Collet4856a002015-01-24 01:58:16 +01001056
Yann Collet9d909222015-12-17 14:09:55 +01001057int FIO_compressMultipleFilenames(const char** inFileNamesTable, unsigned nbFiles,
Yann Collet4f137032015-12-17 02:23:58 +01001058 const char* suffix,
Przemyslaw Skibinski8349d672016-12-13 13:24:59 +01001059 const char* dictFileName, int compressionLevel,
1060 ZSTD_compressionParameters* comprParams)
Yann Collet4f137032015-12-17 02:23:58 +01001061{
Yann Collet4f137032015-12-17 02:23:58 +01001062 int missed_files = 0;
Yann Collet4f137032015-12-17 02:23:58 +01001063 size_t dfnSize = FNSPACE;
Yann Collet44f684d2016-07-13 19:30:40 +02001064 char* dstFileName = (char*)malloc(FNSPACE);
Yann Colletf8494622016-05-07 22:43:40 +02001065 size_t const suffixSize = suffix ? strlen(suffix) : 0;
Yann Collet993060e2016-09-21 16:46:08 +02001066 U64 const srcSize = (nbFiles != 1) ? 0 : UTIL_getFileSize(inFileNamesTable[0]) ;
Sean Purcell0f5c95a2017-02-07 16:33:48 -08001067 int const regFile = (nbFiles != 1) ? 0 : UTIL_isRegFile(inFileNamesTable[0]);
1068 cRess_t ress = FIO_createCResources(dictFileName, compressionLevel, srcSize, regFile, comprParams);
Yann Collet4f137032015-12-17 02:23:58 +01001069
1070 /* init */
Yann Collete12ae022017-05-16 17:32:33 -07001071 if (dstFileName==NULL)
1072 EXM_THROW(27, "FIO_compressMultipleFilenames : allocation error for dstFileName");
1073 if (suffix == NULL)
1074 EXM_THROW(28, "FIO_compressMultipleFilenames : dst unknown"); /* should never happen */
Yann Collet4f137032015-12-17 02:23:58 +01001075
1076 /* loop on each file */
Yann Collet459a6b72016-02-15 20:37:23 +01001077 if (!strcmp(suffix, stdoutmark)) {
Yann Colletf8494622016-05-07 22:43:40 +02001078 unsigned u;
Yann Collet459a6b72016-02-15 20:37:23 +01001079 ress.dstFile = stdout;
inikep60af95d2016-05-19 10:29:49 +02001080 SET_BINARY_MODE(stdout);
Yann Collet459a6b72016-02-15 20:37:23 +01001081 for (u=0; u<nbFiles; u++)
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +01001082 missed_files += FIO_compressFilename_srcFile(ress, stdoutmark, inFileNamesTable[u], compressionLevel);
Yann Collete12ae022017-05-16 17:32:33 -07001083 if (fclose(ress.dstFile))
1084 EXM_THROW(29, "Write error : cannot properly close stdout");
Yann Collet459a6b72016-02-15 20:37:23 +01001085 } else {
Yann Colletf8494622016-05-07 22:43:40 +02001086 unsigned u;
Yann Colletf0624362016-02-12 15:56:46 +01001087 for (u=0; u<nbFiles; u++) {
Yann Collete12ae022017-05-16 17:32:33 -07001088 size_t const ifnSize = strlen(inFileNamesTable[u]);
1089 if (dfnSize <= ifnSize+suffixSize+1) { /* resize name buffer */
1090 free(dstFileName);
1091 dfnSize = ifnSize + 20;
1092 dstFileName = (char*)malloc(dfnSize);
1093 if (!dstFileName)
1094 EXM_THROW(30, "zstd: %s", strerror(errno));
1095 }
Yann Colletf0624362016-02-12 15:56:46 +01001096 strcpy(dstFileName, inFileNamesTable[u]);
1097 strcat(dstFileName, suffix);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +01001098 missed_files += FIO_compressFilename_dstFile(ress, dstFileName, inFileNamesTable[u], compressionLevel);
Yann Collet459a6b72016-02-15 20:37:23 +01001099 } }
Yann Collet4f137032015-12-17 02:23:58 +01001100
1101 /* Close & Free */
1102 FIO_freeCResources(ress);
1103 free(dstFileName);
1104
1105 return missed_files;
1106}
1107
Yann Colletf8494622016-05-07 22:43:40 +02001108#endif /* #ifndef ZSTD_NOCOMPRESS */
inikep3c7c3522016-04-22 13:59:05 +02001109
Yann Collet4f137032015-12-17 02:23:58 +01001110
inikepdb396432016-04-22 18:22:30 +02001111
1112#ifndef ZSTD_NODECOMPRESS
1113
Yann Collet4f137032015-12-17 02:23:58 +01001114/* **************************************************************************
1115* Decompression
1116****************************************************************************/
Yann Colletdeb078b2015-12-17 20:30:14 +01001117typedef struct {
1118 void* srcBuffer;
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001119 size_t srcBufferLoaded;
Yann Colletdeb078b2015-12-17 20:30:14 +01001120 size_t srcBufferSize;
1121 void* dstBuffer;
1122 size_t dstBufferSize;
Yann Collet6263ba52016-08-13 23:45:45 +02001123 ZSTD_DStream* dctx;
Yann Collet1f1f2392016-02-12 18:33:26 +01001124 FILE* dstFile;
Yann Colletdeb078b2015-12-17 20:30:14 +01001125} dRess_t;
1126
1127static dRess_t FIO_createDResources(const char* dictFileName)
1128{
1129 dRess_t ress;
Yann Collet5e80dd32016-07-13 17:38:39 +02001130 memset(&ress, 0, sizeof(ress));
Yann Colletdeb078b2015-12-17 20:30:14 +01001131
Yann Collet5e80dd32016-07-13 17:38:39 +02001132 /* Allocation */
Yann Collet6263ba52016-08-13 23:45:45 +02001133 ress.dctx = ZSTD_createDStream();
1134 if (ress.dctx==NULL) EXM_THROW(60, "Can't create ZSTD_DStream");
Yann Colletbb002742017-01-25 16:25:38 -08001135 ZSTD_setDStreamParameter(ress.dctx, DStream_p_maxWindowSize, g_memLimit);
Yann Collet6263ba52016-08-13 23:45:45 +02001136 ress.srcBufferSize = ZSTD_DStreamInSize();
Yann Colletdeb078b2015-12-17 20:30:14 +01001137 ress.srcBuffer = malloc(ress.srcBufferSize);
Yann Collet6263ba52016-08-13 23:45:45 +02001138 ress.dstBufferSize = ZSTD_DStreamOutSize();
Yann Colletdeb078b2015-12-17 20:30:14 +01001139 ress.dstBuffer = malloc(ress.dstBufferSize);
Yann Collete12ae022017-05-16 17:32:33 -07001140 if (!ress.srcBuffer || !ress.dstBuffer)
1141 EXM_THROW(61, "Allocation error : not enough memory");
Yann Colletdeb078b2015-12-17 20:30:14 +01001142
1143 /* dictionary */
Yann Collet3ecbe6a2016-09-14 17:26:59 +02001144 { void* dictBuffer;
Yann Collet0e300592017-04-11 14:41:02 -07001145 size_t const dictBufferSize = FIO_createDictBuffer(&dictBuffer, dictFileName);
Yann Collet3ecbe6a2016-09-14 17:26:59 +02001146 size_t const initError = ZSTD_initDStream_usingDict(ress.dctx, dictBuffer, dictBufferSize);
Yann Collete12ae022017-05-16 17:32:33 -07001147 if (ZSTD_isError(initError))
1148 EXM_THROW(61, "ZSTD_initDStream_usingDict error : %s", ZSTD_getErrorName(initError));
Yann Collet3ecbe6a2016-09-14 17:26:59 +02001149 free(dictBuffer);
1150 }
Yann Colletdeb078b2015-12-17 20:30:14 +01001151
1152 return ress;
1153}
1154
1155static void FIO_freeDResources(dRess_t ress)
1156{
Yann Collet6263ba52016-08-13 23:45:45 +02001157 size_t const errorCode = ZSTD_freeDStream(ress.dctx);
Yann Collete12ae022017-05-16 17:32:33 -07001158 if (ZSTD_isError(errorCode))
1159 EXM_THROW(69, "Error : can't free ZSTD_DStream context resource : %s",
1160 ZSTD_getErrorName(errorCode));
Yann Colletdeb078b2015-12-17 20:30:14 +01001161 free(ress.srcBuffer);
1162 free(ress.dstBuffer);
Yann Colletdeb078b2015-12-17 20:30:14 +01001163}
Yann Collet4f137032015-12-17 02:23:58 +01001164
1165
Yann Collet75424d12016-05-23 16:56:56 +02001166/** FIO_fwriteSparse() :
1167* @return : storedSkips, to be provided to next call to FIO_fwriteSparse() of LZ4IO_fwriteSparseEnd() */
1168static unsigned FIO_fwriteSparse(FILE* file, const void* buffer, size_t bufferSize, unsigned storedSkips)
1169{
1170 const size_t* const bufferT = (const size_t*)buffer; /* Buffer is supposed malloc'ed, hence aligned on size_t */
1171 size_t bufferSizeT = bufferSize / sizeof(size_t);
1172 const size_t* const bufferTEnd = bufferT + bufferSizeT;
1173 const size_t* ptrT = bufferT;
1174 static const size_t segmentSizeT = (32 KB) / sizeof(size_t); /* 0-test re-attempted every 32 KB */
1175
1176 if (!g_sparseFileSupport) { /* normal write */
1177 size_t const sizeCheck = fwrite(buffer, 1, bufferSize, file);
1178 if (sizeCheck != bufferSize) EXM_THROW(70, "Write error : cannot write decoded block");
1179 return 0;
1180 }
1181
1182 /* avoid int overflow */
1183 if (storedSkips > 1 GB) {
ds77168d9b82017-02-12 10:27:18 +01001184 int const seekResult = LONG_SEEK(file, 1 GB, SEEK_CUR);
Yann Collet75424d12016-05-23 16:56:56 +02001185 if (seekResult != 0) EXM_THROW(71, "1 GB skip error (sparse file support)");
1186 storedSkips -= 1 GB;
1187 }
1188
1189 while (ptrT < bufferTEnd) {
1190 size_t seg0SizeT = segmentSizeT;
1191 size_t nb0T;
1192
1193 /* count leading zeros */
1194 if (seg0SizeT > bufferSizeT) seg0SizeT = bufferSizeT;
1195 bufferSizeT -= seg0SizeT;
1196 for (nb0T=0; (nb0T < seg0SizeT) && (ptrT[nb0T] == 0); nb0T++) ;
1197 storedSkips += (unsigned)(nb0T * sizeof(size_t));
1198
1199 if (nb0T != seg0SizeT) { /* not all 0s */
ds77168d9b82017-02-12 10:27:18 +01001200 int const seekResult = LONG_SEEK(file, storedSkips, SEEK_CUR);
Yann Collet75424d12016-05-23 16:56:56 +02001201 if (seekResult) EXM_THROW(72, "Sparse skip error ; try --no-sparse");
1202 storedSkips = 0;
1203 seg0SizeT -= nb0T;
1204 ptrT += nb0T;
1205 { size_t const sizeCheck = fwrite(ptrT, sizeof(size_t), seg0SizeT, file);
Yann Collete12ae022017-05-16 17:32:33 -07001206 if (sizeCheck != seg0SizeT)
1207 EXM_THROW(73, "Write error : cannot write decoded block");
Yann Collet75424d12016-05-23 16:56:56 +02001208 } }
1209 ptrT += seg0SizeT;
1210 }
1211
1212 { static size_t const maskT = sizeof(size_t)-1;
Yann Collete12ae022017-05-16 17:32:33 -07001213 if (bufferSize & maskT) {
1214 /* size not multiple of sizeof(size_t) : implies end of block */
Yann Collet75424d12016-05-23 16:56:56 +02001215 const char* const restStart = (const char*)bufferTEnd;
1216 const char* restPtr = restStart;
1217 size_t restSize = bufferSize & maskT;
1218 const char* const restEnd = restStart + restSize;
1219 for ( ; (restPtr < restEnd) && (*restPtr == 0); restPtr++) ;
1220 storedSkips += (unsigned) (restPtr - restStart);
1221 if (restPtr != restEnd) {
ds77168d9b82017-02-12 10:27:18 +01001222 int seekResult = LONG_SEEK(file, storedSkips, SEEK_CUR);
Yann Collete12ae022017-05-16 17:32:33 -07001223 if (seekResult)
1224 EXM_THROW(74, "Sparse skip error ; try --no-sparse");
Yann Collet75424d12016-05-23 16:56:56 +02001225 storedSkips = 0;
1226 { size_t const sizeCheck = fwrite(restPtr, 1, restEnd - restPtr, file);
Yann Collete12ae022017-05-16 17:32:33 -07001227 if (sizeCheck != (size_t)(restEnd - restPtr))
1228 EXM_THROW(75, "Write error : cannot write decoded end of block");
Yann Collet75424d12016-05-23 16:56:56 +02001229 } } } }
1230
1231 return storedSkips;
1232}
1233
1234static void FIO_fwriteSparseEnd(FILE* file, unsigned storedSkips)
1235{
1236 if (storedSkips-->0) { /* implies g_sparseFileSupport>0 */
ds77168d9b82017-02-12 10:27:18 +01001237 int const seekResult = LONG_SEEK(file, storedSkips, SEEK_CUR);
Nick Terrellaa8bcf32017-03-13 18:11:07 -07001238 if (seekResult != 0) EXM_THROW(69, "Final skip error (sparse file)");
Yann Collet75424d12016-05-23 16:56:56 +02001239 { const char lastZeroByte[1] = { 0 };
1240 size_t const sizeCheck = fwrite(lastZeroByte, 1, 1, file);
Yann Collete12ae022017-05-16 17:32:33 -07001241 if (sizeCheck != 1)
1242 EXM_THROW(69, "Write error : cannot write last zero");
Yann Collet75424d12016-05-23 16:56:56 +02001243 } }
1244}
1245
Yann Colletbe50aaa2015-09-10 23:26:09 +01001246
Yann Colletde95f962016-05-23 19:46:47 +02001247/** FIO_passThrough() : just copy input into output, for compatibility with gzip -df mode
1248 @return : 0 (no error) */
Przemyslaw Skibinski7c6bbc32016-12-05 18:31:14 +01001249static unsigned FIO_passThrough(FILE* foutput, FILE* finput, void* buffer, size_t bufferSize, size_t alreadyLoaded)
Yann Colletde95f962016-05-23 19:46:47 +02001250{
Yann Collet179b1972016-11-02 17:30:49 -07001251 size_t const blockSize = MIN(64 KB, bufferSize);
Yann Colletddbb8e22016-05-24 00:52:14 +02001252 size_t readFromInput = 1;
Yann Colletde95f962016-05-23 19:46:47 +02001253 unsigned storedSkips = 0;
1254
Przemyslaw Skibinski7c6bbc32016-12-05 18:31:14 +01001255 /* assumption : ress->srcBufferLoaded bytes already loaded and stored within buffer */
1256 { size_t const sizeCheck = fwrite(buffer, 1, alreadyLoaded, foutput);
1257 if (sizeCheck != alreadyLoaded) EXM_THROW(50, "Pass-through write error"); }
Yann Colletde95f962016-05-23 19:46:47 +02001258
Yann Colletddbb8e22016-05-24 00:52:14 +02001259 while (readFromInput) {
1260 readFromInput = fread(buffer, 1, blockSize, finput);
1261 storedSkips = FIO_fwriteSparse(foutput, buffer, readFromInput, storedSkips);
Yann Colletde95f962016-05-23 19:46:47 +02001262 }
1263
1264 FIO_fwriteSparseEnd(foutput, storedSkips);
1265 return 0;
1266}
1267
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001268
1269/** FIO_decompressFrame() :
Yann Collet01a1abf2017-05-05 19:15:24 -07001270 @return : size of decoded frame, or an error code
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001271*/
Yann Collet01a1abf2017-05-05 19:15:24 -07001272#define FIO_ERROR_ZSTD_DECODING ((unsigned long long)(-2))
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001273unsigned long long FIO_decompressFrame(dRess_t* ress,
1274 FILE* finput,
Yann Collet01a1abf2017-05-05 19:15:24 -07001275 const char* srcFileName,
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001276 U64 alreadyDecoded)
1277{
1278 U64 frameSize = 0;
1279 U32 storedSkips = 0;
1280
1281 ZSTD_resetDStream(ress->dctx);
Yann Collet01a1abf2017-05-05 19:15:24 -07001282 if (strlen(srcFileName)>20) srcFileName += strlen(srcFileName)-20; /* display last 20 characters */
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001283
1284 /* Header loading (optional, saves one loop) */
1285 { size_t const toRead = 9;
1286 if (ress->srcBufferLoaded < toRead)
1287 ress->srcBufferLoaded += fread(((char*)ress->srcBuffer) + ress->srcBufferLoaded, 1, toRead - ress->srcBufferLoaded, finput);
1288 }
1289
1290 /* Main decompression Loop */
1291 while (1) {
1292 ZSTD_inBuffer inBuff = { ress->srcBuffer, ress->srcBufferLoaded, 0 };
1293 ZSTD_outBuffer outBuff= { ress->dstBuffer, ress->dstBufferSize, 0 };
1294 size_t const readSizeHint = ZSTD_decompressStream(ress->dctx, &outBuff, &inBuff);
Yann Collet01a1abf2017-05-05 19:15:24 -07001295 if (ZSTD_isError(readSizeHint)) {
1296 DISPLAYLEVEL(1, "%s : Decoding error (36) : %s \n",
1297 srcFileName, ZSTD_getErrorName(readSizeHint));
1298 return FIO_ERROR_ZSTD_DECODING;
1299 }
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001300
1301 /* Write block */
Yann Collet500014a2017-01-19 16:59:56 -08001302 storedSkips = FIO_fwriteSparse(ress->dstFile, ress->dstBuffer, outBuff.pos, storedSkips);
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001303 frameSize += outBuff.pos;
Yann Collet01a1abf2017-05-05 19:15:24 -07001304 DISPLAYUPDATE(2, "\r%-20.20s : %u MB... ",
1305 srcFileName, (U32)((alreadyDecoded+frameSize)>>20) );
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001306
1307 if (inBuff.pos > 0) {
1308 memmove(ress->srcBuffer, (char*)ress->srcBuffer + inBuff.pos, inBuff.size - inBuff.pos);
1309 ress->srcBufferLoaded -= inBuff.pos;
1310 }
1311
1312 if (readSizeHint == 0) break; /* end of frame */
Yann Collet01a1abf2017-05-05 19:15:24 -07001313 if (inBuff.size != inBuff.pos) {
1314 DISPLAYLEVEL(1, "%s : Decoding error (37) : should consume entire input \n",
1315 srcFileName);
1316 return FIO_ERROR_ZSTD_DECODING;
1317 }
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001318
1319 /* Fill input buffer */
1320 { size_t const toRead = MIN(readSizeHint, ress->srcBufferSize); /* support large skippable frames */
1321 if (ress->srcBufferLoaded < toRead)
Yann Collet01a1abf2017-05-05 19:15:24 -07001322 ress->srcBufferLoaded += fread((char*)ress->srcBuffer + ress->srcBufferLoaded,
1323 1, toRead - ress->srcBufferLoaded, finput);
1324 if (ress->srcBufferLoaded < toRead) {
1325 DISPLAYLEVEL(1, "%s : Read error (39) : premature end \n",
1326 srcFileName);
1327 return FIO_ERROR_ZSTD_DECODING;
1328 } } }
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001329
1330 FIO_fwriteSparseEnd(ress->dstFile, storedSkips);
1331
1332 return frameSize;
1333}
1334
1335
Przemyslaw Skibinski19aad422016-12-01 11:56:31 +01001336#ifdef ZSTD_GZDECOMPRESS
Yann Collete12ae022017-05-16 17:32:33 -07001337static unsigned long long FIO_decompressGzFrame(dRess_t* ress,
1338 FILE* srcFile, const char* srcFileName)
Przemyslaw Skibinskib0f2ef22016-12-02 13:50:29 +01001339{
Yann Collet5bd42372016-12-02 12:40:57 -08001340 unsigned long long outFileSize = 0;
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +01001341 z_stream strm;
Nick Terrellaa8bcf32017-03-13 18:11:07 -07001342 int flush = Z_NO_FLUSH;
Przemyslaw Skibinski862698f2017-02-27 13:21:05 +01001343 int ret;
Yann Collet5bd42372016-12-02 12:40:57 -08001344
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +01001345 strm.zalloc = Z_NULL;
1346 strm.zfree = Z_NULL;
1347 strm.opaque = Z_NULL;
1348 strm.next_in = 0;
Przemyslaw Skibinski862698f2017-02-27 13:21:05 +01001349 strm.avail_in = 0;
Yann Collete12ae022017-05-16 17:32:33 -07001350 /* see http://www.zlib.net/manual.html */
1351 if (inflateInit2(&strm, 15 /* maxWindowLogSize */ + 16 /* gzip only */) != Z_OK)
1352 return 0;
Yann Collet5bd42372016-12-02 12:40:57 -08001353
Yann Colletb02ac8d2017-02-03 08:43:06 -08001354 strm.next_out = (Bytef*)ress->dstBuffer;
Yann Colletc3cba9d2017-02-02 17:12:50 -08001355 strm.avail_out = (uInt)ress->dstBufferSize;
1356 strm.avail_in = (uInt)ress->srcBufferLoaded;
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001357 strm.next_in = (z_const unsigned char*)ress->srcBuffer;
Przemyslaw Skibinski4b504f12016-12-02 13:11:39 +01001358
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +01001359 for ( ; ; ) {
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001360 if (strm.avail_in == 0) {
1361 ress->srcBufferLoaded = fread(ress->srcBuffer, 1, ress->srcBufferSize, srcFile);
Nick Terrellaa8bcf32017-03-13 18:11:07 -07001362 if (ress->srcBufferLoaded == 0) flush = Z_FINISH;
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001363 strm.next_in = (z_const unsigned char*)ress->srcBuffer;
Yann Colletc3cba9d2017-02-02 17:12:50 -08001364 strm.avail_in = (uInt)ress->srcBufferLoaded;
Przemyslaw Skibinski4b504f12016-12-02 13:11:39 +01001365 }
Nick Terrellaa8bcf32017-03-13 18:11:07 -07001366 ret = inflate(&strm, flush);
1367 if (ret == Z_BUF_ERROR) EXM_THROW(39, "zstd: %s: premature end", srcFileName);
Yann Collete12ae022017-05-16 17:32:33 -07001368 if (ret != Z_OK && ret != Z_STREAM_END) {
1369 DISPLAY("zstd: %s: inflate error %d \n", srcFileName, ret);
1370 return 0;
1371 }
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001372 { size_t const decompBytes = ress->dstBufferSize - strm.avail_out;
Yann Collet5bd42372016-12-02 12:40:57 -08001373 if (decompBytes) {
Yann Collete12ae022017-05-16 17:32:33 -07001374 if (fwrite(ress->dstBuffer, 1, decompBytes, ress->dstFile) != decompBytes)
1375 EXM_THROW(31, "Write error : cannot write to output file");
Yann Collet5bd42372016-12-02 12:40:57 -08001376 outFileSize += decompBytes;
Yann Colletb02ac8d2017-02-03 08:43:06 -08001377 strm.next_out = (Bytef*)ress->dstBuffer;
Yann Colletc3cba9d2017-02-02 17:12:50 -08001378 strm.avail_out = (uInt)ress->dstBufferSize;
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001379 }
1380 }
1381 if (ret == Z_STREAM_END) break;
1382 }
Przemyslaw Skibinski19aad422016-12-01 11:56:31 +01001383
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001384 if (strm.avail_in > 0) memmove(ress->srcBuffer, strm.next_in, strm.avail_in);
1385 ress->srcBufferLoaded = strm.avail_in;
Przemyslaw Skibinski862698f2017-02-27 13:21:05 +01001386 ret = inflateEnd(&strm);
Nick Terrellaa8bcf32017-03-13 18:11:07 -07001387 if (ret != Z_OK) EXM_THROW(32, "zstd: %s: inflateEnd error %d", srcFileName, ret);
1388 return outFileSize;
1389}
1390#endif
1391
1392
1393#ifdef ZSTD_LZMADECOMPRESS
1394static unsigned long long FIO_decompressLzmaFrame(dRess_t* ress, FILE* srcFile, const char* srcFileName, int plain_lzma)
1395{
1396 unsigned long long outFileSize = 0;
1397 lzma_stream strm = LZMA_STREAM_INIT;
1398 lzma_action action = LZMA_RUN;
1399 lzma_ret ret;
1400
1401 strm.next_in = 0;
1402 strm.avail_in = 0;
1403 if (plain_lzma) {
1404 ret = lzma_alone_decoder(&strm, UINT64_MAX); /* LZMA */
1405 } else {
1406 ret = lzma_stream_decoder(&strm, UINT64_MAX, 0); /* XZ */
1407 }
1408
Yann Collete12ae022017-05-16 17:32:33 -07001409 if (ret != LZMA_OK)
1410 EXM_THROW(71, "zstd: %s: lzma_alone_decoder/lzma_stream_decoder error %d",
1411 srcFileName, ret);
Nick Terrellaa8bcf32017-03-13 18:11:07 -07001412
1413 strm.next_out = ress->dstBuffer;
1414 strm.avail_out = ress->dstBufferSize;
1415 strm.avail_in = ress->srcBufferLoaded;
1416 strm.next_in = ress->srcBuffer;
1417
1418 for ( ; ; ) {
1419 if (strm.avail_in == 0) {
1420 ress->srcBufferLoaded = fread(ress->srcBuffer, 1, ress->srcBufferSize, srcFile);
1421 if (ress->srcBufferLoaded == 0) action = LZMA_FINISH;
1422 strm.next_in = ress->srcBuffer;
1423 strm.avail_in = ress->srcBufferLoaded;
1424 }
1425 ret = lzma_code(&strm, action);
1426
Yann Collete12ae022017-05-16 17:32:33 -07001427 if (ret == LZMA_BUF_ERROR)
1428 EXM_THROW(39, "zstd: %s: premature end", srcFileName);
1429 if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
1430 DISPLAY("zstd: %s: lzma_code decoding error %d \n", srcFileName, ret);
1431 return 0;
1432 }
Nick Terrellaa8bcf32017-03-13 18:11:07 -07001433 { size_t const decompBytes = ress->dstBufferSize - strm.avail_out;
1434 if (decompBytes) {
Yann Collete12ae022017-05-16 17:32:33 -07001435 if (fwrite(ress->dstBuffer, 1, decompBytes, ress->dstFile) != decompBytes)
1436 EXM_THROW(31, "Write error : cannot write to output file");
Nick Terrellaa8bcf32017-03-13 18:11:07 -07001437 outFileSize += decompBytes;
1438 strm.next_out = ress->dstBuffer;
1439 strm.avail_out = ress->dstBufferSize;
Yann Collete12ae022017-05-16 17:32:33 -07001440 } }
Nick Terrellaa8bcf32017-03-13 18:11:07 -07001441 if (ret == LZMA_STREAM_END) break;
1442 }
1443
1444 if (strm.avail_in > 0) memmove(ress->srcBuffer, strm.next_in, strm.avail_in);
1445 ress->srcBufferLoaded = strm.avail_in;
1446 lzma_end(&strm);
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +01001447 return outFileSize;
Przemyslaw Skibinski19aad422016-12-01 11:56:31 +01001448}
1449#endif
1450
Sean Purcell4de86322017-04-24 16:48:25 -07001451#ifdef ZSTD_LZ4DECOMPRESS
Yann Collete12ae022017-05-16 17:32:33 -07001452static unsigned long long FIO_decompressLz4Frame(dRess_t* ress,
1453 FILE* srcFile, const char* srcFileName)
Sean Purcell4de86322017-04-24 16:48:25 -07001454{
1455 unsigned long long filesize = 0;
1456 LZ4F_errorCode_t nextToLoad;
Sean Purcell2c4b6fe2017-04-25 11:00:54 -07001457 LZ4F_decompressionContext_t dCtx;
Sean Purcell4de86322017-04-24 16:48:25 -07001458 LZ4F_errorCode_t const errorCode = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION);
1459
Yann Collete12ae022017-05-16 17:32:33 -07001460 if (LZ4F_isError(errorCode))
1461 EXM_THROW(61, "zstd: failed to create lz4 decompression context");
Sean Purcell4de86322017-04-24 16:48:25 -07001462
1463 /* Init feed with magic number (already consumed from FILE* sFile) */
1464 { size_t inSize = 4;
1465 size_t outSize= 0;
1466 MEM_writeLE32(ress->srcBuffer, LZ4_MAGICNUMBER);
1467 nextToLoad = LZ4F_decompress(dCtx, ress->dstBuffer, &outSize, ress->srcBuffer, &inSize, NULL);
Yann Collete12ae022017-05-16 17:32:33 -07001468 if (LZ4F_isError(nextToLoad))
1469 EXM_THROW(62, "zstd: %s: lz4 header error : %s",
1470 srcFileName, LZ4F_getErrorName(nextToLoad));
Sean Purcell4de86322017-04-24 16:48:25 -07001471 }
1472
1473 /* Main Loop */
1474 for (;nextToLoad;) {
1475 size_t readSize;
1476 size_t pos = 0;
1477 size_t decodedBytes = ress->dstBufferSize;
1478
1479 /* Read input */
1480 if (nextToLoad > ress->srcBufferSize) nextToLoad = ress->srcBufferSize;
1481 readSize = fread(ress->srcBuffer, 1, nextToLoad, srcFile);
1482 if (!readSize) break; /* reached end of file or stream */
1483
1484 while ((pos < readSize) || (decodedBytes == ress->dstBufferSize)) { /* still to read, or still to flush */
1485 /* Decode Input (at least partially) */
1486 size_t remaining = readSize - pos;
1487 decodedBytes = ress->dstBufferSize;
1488 nextToLoad = LZ4F_decompress(dCtx, ress->dstBuffer, &decodedBytes, (char*)(ress->srcBuffer)+pos, &remaining, NULL);
Yann Collete12ae022017-05-16 17:32:33 -07001489 if (LZ4F_isError(nextToLoad))
1490 EXM_THROW(66, "zstd: %s: decompression error : %s",
1491 srcFileName, LZ4F_getErrorName(nextToLoad));
Sean Purcell4de86322017-04-24 16:48:25 -07001492 pos += remaining;
1493
1494 /* Write Block */
1495 if (decodedBytes) {
Yann Collete12ae022017-05-16 17:32:33 -07001496 if (fwrite(ress->dstBuffer, 1, decodedBytes, ress->dstFile) != decodedBytes)
1497 EXM_THROW(63, "Write error : cannot write to output file");
Sean Purcell4de86322017-04-24 16:48:25 -07001498 filesize += decodedBytes;
1499 DISPLAYUPDATE(2, "\rDecompressed : %u MB ", (unsigned)(filesize>>20));
1500 }
1501
1502 if (!nextToLoad) break;
1503 }
1504 }
1505 /* can be out because readSize == 0, which could be an fread() error */
1506 if (ferror(srcFile)) EXM_THROW(67, "zstd: %s: read error", srcFileName);
1507
1508 if (nextToLoad!=0) EXM_THROW(68, "zstd: %s: unfinished stream", srcFileName);
1509
1510 LZ4F_freeDecompressionContext(dCtx);
1511 ress->srcBufferLoaded = 0; /* LZ4F will go to the frame boundary */
1512
1513 return filesize;
1514}
1515#endif
1516
1517
Przemyslaw Skibinski19aad422016-12-01 11:56:31 +01001518
Yann Collet1f1f2392016-02-12 18:33:26 +01001519/** FIO_decompressSrcFile() :
1520 Decompression `srcFileName` into `ress.dstFile`
1521 @return : 0 : OK
1522 1 : operation not started
1523*/
Yann Collet743b33f2016-12-02 15:18:57 -08001524static int FIO_decompressSrcFile(dRess_t ress, const char* dstFileName, const char* srcFileName)
Yann Colletb1f3f4b2015-10-18 22:18:32 +01001525{
Przemyslaw Skibinskib0f2ef22016-12-02 13:50:29 +01001526 FILE* srcFile;
Yann Colleta1dd6b92016-07-26 16:44:09 +02001527 unsigned readSomething = 0;
Yann Collet5bd42372016-12-02 12:40:57 -08001528 unsigned long long filesize = 0;
Przemyslaw Skibinski0e146752016-11-30 13:34:21 +01001529
Yann Colletb09b12c2016-06-09 22:59:51 +02001530 if (UTIL_isDirectory(srcFileName)) {
1531 DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
1532 return 1;
1533 }
Przemyslaw Skibinski0e146752016-11-30 13:34:21 +01001534
Przemyslaw Skibinskib0f2ef22016-12-02 13:50:29 +01001535 srcFile = FIO_openSrcFile(srcFileName);
Yann Collet0b9b8942017-02-27 00:27:30 -08001536 if (srcFile==NULL) return 1;
Yann Collet88fcd292015-11-25 14:42:45 +01001537
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +01001538 /* for each frame */
1539 for ( ; ; ) {
1540 /* check magic number -> version */
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +01001541 size_t const toRead = 4;
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +01001542 const BYTE* buf = (const BYTE*)ress.srcBuffer;
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001543 if (ress.srcBufferLoaded < toRead)
Yann Collet01a1abf2017-05-05 19:15:24 -07001544 ress.srcBufferLoaded += fread((char*)ress.srcBuffer + ress.srcBufferLoaded,
1545 (size_t)1, toRead - ress.srcBufferLoaded, srcFile);
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001546 if (ress.srcBufferLoaded==0) {
Yann Collet01a1abf2017-05-05 19:15:24 -07001547 if (readSomething==0) {
1548 DISPLAY("zstd: %s: unexpected end of file \n", srcFileName);
1549 fclose(srcFile);
1550 return 1;
1551 } /* srcFileName is empty */
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +01001552 break; /* no more input */
1553 }
1554 readSomething = 1; /* there is at least >= 4 bytes in srcFile */
Yann Collete12ae022017-05-16 17:32:33 -07001555 if (ress.srcBufferLoaded < toRead) {
1556 DISPLAY("zstd: %s: unknown header \n", srcFileName);
1557 fclose(srcFile);
1558 return 1;
1559 }
Nick Terrellaa8bcf32017-03-13 18:11:07 -07001560 if (buf[0] == 31 && buf[1] == 139) { /* gz magic number */
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +01001561#ifdef ZSTD_GZDECOMPRESS
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +01001562 unsigned long long const result = FIO_decompressGzFrame(&ress, srcFile, srcFileName);
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +01001563 if (result == 0) return 1;
1564 filesize += result;
Przemyslaw Skibinskiabfb51f2016-11-30 15:05:54 +01001565#else
Yann Collet5bd42372016-12-02 12:40:57 -08001566 DISPLAYLEVEL(1, "zstd: %s: gzip file cannot be uncompressed (zstd compiled without ZSTD_GZDECOMPRESS) -- ignored \n", srcFileName);
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +01001567 return 1;
Przemyslaw Skibinskiabfb51f2016-11-30 15:05:54 +01001568#endif
Nick Terrellaa8bcf32017-03-13 18:11:07 -07001569 } else if ((buf[0] == 0xFD && buf[1] == 0x37) /* xz magic number */
1570 || (buf[0] == 0x5D && buf[1] == 0x00)) { /* lzma header (no magic number) */
1571#ifdef ZSTD_LZMADECOMPRESS
1572 unsigned long long const result = FIO_decompressLzmaFrame(&ress, srcFile, srcFileName, buf[0] != 0xFD);
1573 if (result == 0) return 1;
1574 filesize += result;
1575#else
1576 DISPLAYLEVEL(1, "zstd: %s: xz/lzma file cannot be uncompressed (zstd compiled without ZSTD_LZMADECOMPRESS) -- ignored \n", srcFileName);
1577 return 1;
1578#endif
Sean Purcell4de86322017-04-24 16:48:25 -07001579 } else if (MEM_readLE32(buf) == LZ4_MAGICNUMBER) {
1580#ifdef ZSTD_LZ4DECOMPRESS
1581 unsigned long long const result = FIO_decompressLz4Frame(&ress, srcFile, srcFileName);
1582 if (result == 0) return 1;
1583 filesize += result;
1584#else
1585 DISPLAYLEVEL(1, "zstd: %s: lz4 file cannot be uncompressed (zstd compiled without ZSTD_LZ4DECOMPRESS) -- ignored \n", srcFileName);
1586 return 1;
1587#endif
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +01001588 } else {
1589 if (!ZSTD_isFrame(ress.srcBuffer, toRead)) {
Yann Collet743b33f2016-12-02 15:18:57 -08001590 if ((g_overwrite) && !strcmp (dstFileName, stdoutmark)) { /* pass-through mode */
Yann Collet01a1abf2017-05-05 19:15:24 -07001591 unsigned const result = FIO_passThrough(ress.dstFile, srcFile,
1592 ress.srcBuffer, ress.srcBufferSize, ress.srcBufferLoaded);
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +01001593 if (fclose(srcFile)) EXM_THROW(32, "zstd: %s close error", srcFileName); /* error should never happen */
1594 return result;
1595 } else {
1596 DISPLAYLEVEL(1, "zstd: %s: not in zstd format \n", srcFileName);
1597 fclose(srcFile);
1598 return 1;
1599 } }
Yann Collet01a1abf2017-05-05 19:15:24 -07001600 { unsigned long long const frameSize = FIO_decompressFrame(&ress, srcFile, srcFileName, filesize);
1601 if (frameSize == FIO_ERROR_ZSTD_DECODING) {
1602 fclose(srcFile);
1603 return 1;
1604 }
1605 filesize += frameSize;
Yann Collete12ae022017-05-16 17:32:33 -07001606 } }
Yann Collet01a1abf2017-05-05 19:15:24 -07001607 } /* for each frame */
Yann Colletb1f3f4b2015-10-18 22:18:32 +01001608
Yann Colletdeb078b2015-12-17 20:30:14 +01001609 /* Final Status */
Yann Colletb1f3f4b2015-10-18 22:18:32 +01001610 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Collet1c69baa2016-08-28 12:47:17 -07001611 DISPLAYLEVEL(2, "%-20s: %llu bytes \n", srcFileName, filesize);
Yann Colletb1f3f4b2015-10-18 22:18:32 +01001612
Przemyslaw Skibinskib0f2ef22016-12-02 13:50:29 +01001613 /* Close file */
1614 if (fclose(srcFile)) EXM_THROW(33, "zstd: %s close error", srcFileName); /* error should never happen */
Yann Collete12ae022017-05-16 17:32:33 -07001615 if (g_removeSrcFile /* --rm */ && strcmp(srcFileName, stdinmark)) {
1616 if (remove(srcFileName))
1617 EXM_THROW(34, "zstd: %s: %s", srcFileName, strerror(errno));
1618 }
Yann Collet1f1f2392016-02-12 18:33:26 +01001619 return 0;
1620}
Yann Colletb1f3f4b2015-10-18 22:18:32 +01001621
Yann Collet1f1f2392016-02-12 18:33:26 +01001622
1623/** FIO_decompressFile_extRess() :
1624 decompress `srcFileName` into `dstFileName`
1625 @return : 0 : OK
1626 1 : operation aborted (src not available, dst already taken, etc.)
1627*/
Yann Colletb09b12c2016-06-09 22:59:51 +02001628static int FIO_decompressDstFile(dRess_t ress,
Yann Collet5bd42372016-12-02 12:40:57 -08001629 const char* dstFileName, const char* srcFileName)
Yann Collet1f1f2392016-02-12 18:33:26 +01001630{
Chip Turner6de382c2016-03-13 22:24:46 -07001631 int result;
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +01001632 stat_t statbuf;
1633 int stat_result = 0;
Przemyslaw Skibinskia42794d2016-11-02 13:08:39 +01001634
Yann Collet1f1f2392016-02-12 18:33:26 +01001635 ress.dstFile = FIO_openDstFile(dstFileName);
1636 if (ress.dstFile==0) return 1;
1637
Yann Collete12ae022017-05-16 17:32:33 -07001638 if (strcmp (srcFileName, stdinmark) && UTIL_getFileStat(srcFileName, &statbuf))
1639 stat_result = 1;
Yann Collet743b33f2016-12-02 15:18:57 -08001640 result = FIO_decompressSrcFile(ress, dstFileName, srcFileName);
Yann Collet1f1f2392016-02-12 18:33:26 +01001641
Yann Collete12ae022017-05-16 17:32:33 -07001642 if (fclose(ress.dstFile))
1643 EXM_THROW(38, "Write error : cannot properly close %s", dstFileName);
Przemyslaw Skibinskia42794d2016-11-02 13:08:39 +01001644
Yann Colletaad9fe52016-09-07 07:00:08 +02001645 if ( (result != 0)
1646 && strcmp(dstFileName, nulmark) /* special case : don't remove() /dev/null (#316) */
1647 && remove(dstFileName) )
Yann Collet03d3f232016-09-07 07:01:33 +02001648 result=1; /* don't do anything special if remove() fails */
Yann Collete12ae022017-05-16 17:32:33 -07001649 else if (strcmp (dstFileName, stdoutmark) && stat_result)
1650 UTIL_setFileStat(dstFileName, &statbuf);
Chip Turner6de382c2016-03-13 22:24:46 -07001651 return result;
Yann Colletb1f3f4b2015-10-18 22:18:32 +01001652}
1653
1654
Yann Colletdeb078b2015-12-17 20:30:14 +01001655int FIO_decompressFilename(const char* dstFileName, const char* srcFileName,
1656 const char* dictFileName)
1657{
1658 int missingFiles = 0;
1659 dRess_t ress = FIO_createDResources(dictFileName);
1660
Yann Colletb09b12c2016-06-09 22:59:51 +02001661 missingFiles += FIO_decompressDstFile(ress, dstFileName, srcFileName);
Yann Colletdeb078b2015-12-17 20:30:14 +01001662
1663 FIO_freeDResources(ress);
1664 return missingFiles;
1665}
1666
1667
1668#define MAXSUFFIXSIZE 8
1669int FIO_decompressMultipleFilenames(const char** srcNamesTable, unsigned nbFiles,
1670 const char* suffix,
1671 const char* dictFileName)
1672{
Yann Colletdeb078b2015-12-17 20:30:14 +01001673 int skippedFiles = 0;
1674 int missingFiles = 0;
Yann Collet8b23eea2016-05-10 05:37:43 +02001675 dRess_t ress = FIO_createDResources(dictFileName);
Yann Colletdeb078b2015-12-17 20:30:14 +01001676
Yann Collete12ae022017-05-16 17:32:33 -07001677 if (suffix==NULL)
1678 EXM_THROW(70, "zstd: decompression: unknown dst"); /* should never happen */
Yann Collet44f684d2016-07-13 19:30:40 +02001679
Yann Collet743b33f2016-12-02 15:18:57 -08001680 if (!strcmp(suffix, stdoutmark) || !strcmp(suffix, nulmark)) { /* special cases : -c or -t */
Yann Colletf8494622016-05-07 22:43:40 +02001681 unsigned u;
Yann Colletaccfd802016-02-15 19:33:16 +01001682 ress.dstFile = FIO_openDstFile(suffix);
1683 if (ress.dstFile == 0) EXM_THROW(71, "cannot open %s", suffix);
1684 for (u=0; u<nbFiles; u++)
Yann Collet743b33f2016-12-02 15:18:57 -08001685 missingFiles += FIO_decompressSrcFile(ress, suffix, srcNamesTable[u]);
Yann Collete12ae022017-05-16 17:32:33 -07001686 if (fclose(ress.dstFile))
1687 EXM_THROW(72, "Write error : cannot properly close stdout");
Yann Colletaccfd802016-02-15 19:33:16 +01001688 } else {
Nick Terrellaa8bcf32017-03-13 18:11:07 -07001689 size_t suffixSize;
Yann Collet8b23eea2016-05-10 05:37:43 +02001690 size_t dfnSize = FNSPACE;
Yann Colletf8494622016-05-07 22:43:40 +02001691 unsigned u;
Yann Collet8b23eea2016-05-10 05:37:43 +02001692 char* dstFileName = (char*)malloc(FNSPACE);
Yann Collete12ae022017-05-16 17:32:33 -07001693 if (dstFileName==NULL)
1694 EXM_THROW(73, "not enough memory for dstFileName");
Yann Collet1f1f2392016-02-12 18:33:26 +01001695 for (u=0; u<nbFiles; u++) { /* create dstFileName */
Yann Collet8b23eea2016-05-10 05:37:43 +02001696 const char* const srcFileName = srcNamesTable[u];
Nick Terrellaa8bcf32017-03-13 18:11:07 -07001697 const char* const suffixPtr = strrchr(srcFileName, '.');
Yann Collet8b23eea2016-05-10 05:37:43 +02001698 size_t const sfnSize = strlen(srcFileName);
Nick Terrellaa8bcf32017-03-13 18:11:07 -07001699 if (!suffixPtr) {
Yann Collete12ae022017-05-16 17:32:33 -07001700 DISPLAYLEVEL(1, "zstd: %s: unknown suffix -- ignored \n",
1701 srcFileName);
Nick Terrellaa8bcf32017-03-13 18:11:07 -07001702 skippedFiles++;
1703 continue;
1704 }
1705 suffixSize = strlen(suffixPtr);
Yann Colletaccfd802016-02-15 19:33:16 +01001706 if (dfnSize+suffixSize <= sfnSize+1) {
1707 free(dstFileName);
1708 dfnSize = sfnSize + 20;
1709 dstFileName = (char*)malloc(dfnSize);
Yann Collete12ae022017-05-16 17:32:33 -07001710 if (dstFileName==NULL)
1711 EXM_THROW(74, "not enough memory for dstFileName");
Yann Colletaccfd802016-02-15 19:33:16 +01001712 }
Yann Collet01a1abf2017-05-05 19:15:24 -07001713 if (sfnSize <= suffixSize
1714 || (strcmp(suffixPtr, GZ_EXTENSION)
1715 && strcmp(suffixPtr, XZ_EXTENSION)
1716 && strcmp(suffixPtr, ZSTD_EXTENSION)
1717 && strcmp(suffixPtr, LZMA_EXTENSION)
1718 && strcmp(suffixPtr, LZ4_EXTENSION)) ) {
1719 DISPLAYLEVEL(1, "zstd: %s: unknown suffix (%s/%s/%s/%s/%s expected) -- ignored \n",
1720 srcFileName, GZ_EXTENSION, XZ_EXTENSION, ZSTD_EXTENSION, LZMA_EXTENSION, LZ4_EXTENSION);
Nick Terrellaa8bcf32017-03-13 18:11:07 -07001721 skippedFiles++;
1722 continue;
Przemyslaw Skibinski0e146752016-11-30 13:34:21 +01001723 } else {
1724 memcpy(dstFileName, srcFileName, sfnSize - suffixSize);
1725 dstFileName[sfnSize-suffixSize] = '\0';
Yann Collet1f1f2392016-02-12 18:33:26 +01001726 }
Yann Colletb09b12c2016-06-09 22:59:51 +02001727 missingFiles += FIO_decompressDstFile(ress, dstFileName, srcFileName);
Yann Collet8b23eea2016-05-10 05:37:43 +02001728 }
1729 free(dstFileName);
1730 }
Yann Colletdeb078b2015-12-17 20:30:14 +01001731
1732 FIO_freeDResources(ress);
Yann Colletdeb078b2015-12-17 20:30:14 +01001733 return missingFiles + skippedFiles;
1734}
Yann Colletaccfd802016-02-15 19:33:16 +01001735
Yann Collet8b23eea2016-05-10 05:37:43 +02001736#endif /* #ifndef ZSTD_NODECOMPRESS */