blob: 9f1560b4334ba9053e393693c952334024df7d01 [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
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010034#include "mem.h"
Yann Collet4856a002015-01-24 01:58:16 +010035#include "fileio.h"
Yann Colletd3b7f8d2016-06-04 19:47:02 +020036#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_magicNumber, ZSTD_frameHeaderSize_max */
37#include "zstd.h"
Yann Collet500014a2017-01-19 16:59:56 -080038#ifdef ZSTD_MULTITHREAD
39# include "zstdmt_compress.h"
Przemyslaw Skibinski6b508b12016-12-05 18:02:40 +010040#endif
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +010041#if defined(ZSTD_GZCOMPRESS) || defined(ZSTD_GZDECOMPRESS)
Nick Terrell2cb8ee82017-02-06 11:32:13 -080042# include <zlib.h>
Yann Collet500014a2017-01-19 16:59:56 -080043# if !defined(z_const)
44# define z_const
45# endif
Przemyslaw Skibinskiabfb51f2016-11-30 15:05:54 +010046#endif
Yann Collet4856a002015-01-24 01:58:16 +010047
48
Yann Collet6f3acba2016-02-12 20:19:48 +010049/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010050* Constants
Yann Colleteeb8ba12015-10-22 16:55:40 +010051***************************************/
Yann Collet92d75662016-07-03 01:10:53 +020052#define KB *(1<<10)
53#define MB *(1<<20)
54#define GB *(1U<<30)
55
Yann Collet4856a002015-01-24 01:58:16 +010056#define _1BIT 0x01
57#define _2BITS 0x03
58#define _3BITS 0x07
59#define _4BITS 0x0F
60#define _6BITS 0x3F
61#define _8BITS 0xFF
62
Yann Collet88fcd292015-11-25 14:42:45 +010063#define BLOCKSIZE (128 KB)
64#define ROLLBUFFERSIZE (BLOCKSIZE*8*64)
Yann Collet4856a002015-01-24 01:58:16 +010065
Yann Collet6f3acba2016-02-12 20:19:48 +010066#define FIO_FRAMEHEADERSIZE 5 /* as a define, because needed to allocated table on stack */
67#define FSE_CHECKSUM_SEED 0
Yann Collet4856a002015-01-24 01:58:16 +010068
69#define CACHELINE 64
70
Yann Collet6fca9e72016-05-31 02:40:42 +020071#define MAX_DICT_SIZE (8 MB) /* protection against large input (attack scenario) */
Yann Collet6f3acba2016-02-12 20:19:48 +010072
inikep3c7c3522016-04-22 13:59:05 +020073#define FNSPACE 30
Przemyslaw Skibinski0e146752016-11-30 13:34:21 +010074#define GZ_EXTENSION ".gz"
inikep3c7c3522016-04-22 13:59:05 +020075
Yann Collet4856a002015-01-24 01:58:16 +010076
Yann Collet459a6b72016-02-15 20:37:23 +010077/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010078* Macros
Yann Colleteeb8ba12015-10-22 16:55:40 +010079***************************************/
Yann Collet4856a002015-01-24 01:58:16 +010080#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
Yann Collet5d9b8942017-01-27 13:30:18 -080081#define DISPLAYLEVEL(l, ...) { if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } }
Yann Collet4856a002015-01-24 01:58:16 +010082static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
Yann Collet8a1d1a62016-03-10 21:02:25 +010083void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; }
Yann Collet4856a002015-01-24 01:58:16 +010084
Yann Collet5d9b8942017-01-27 13:30:18 -080085#define DISPLAYUPDATE(l, ...) { if (g_displayLevel>=l) { \
Yann Colletb71adf42016-07-02 01:05:31 +020086 if ((clock() - g_time > refreshRate) || (g_displayLevel>=4)) \
Yann Collet4856a002015-01-24 01:58:16 +010087 { g_time = clock(); DISPLAY(__VA_ARGS__); \
Yann Collet5d9b8942017-01-27 13:30:18 -080088 if (g_displayLevel>=4) fflush(stdout); } } }
Yann Colletb71adf42016-07-02 01:05:31 +020089static const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100;
Yann Collet4856a002015-01-24 01:58:16 +010090static clock_t g_time = 0;
91
Yann Collet92d75662016-07-03 01:10:53 +020092#define MIN(a,b) ((a) < (b) ? (a) : (b))
93
Yann Collet459a6b72016-02-15 20:37:23 +010094
95/*-*************************************
Yann Colletb71adf42016-07-02 01:05:31 +020096* Local Parameters - Not thread safe
Yann Colleteeb8ba12015-10-22 16:55:40 +010097***************************************/
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +010098static FIO_compresionType_t g_compresionType = FIO_zstdCompression;
99void FIO_setCompresionType(FIO_compresionType_t compresionType) { g_compresionType = compresionType; }
Yann Collet4856a002015-01-24 01:58:16 +0100100static U32 g_overwrite = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100101void FIO_overwriteMode(void) { g_overwrite=1; }
Yann Collet75424d12016-05-23 16:56:56 +0200102static U32 g_sparseFileSupport = 1; /* 0 : no sparse allowed; 1: auto (file yes, stdout no); 2: force sparse */
103void FIO_setSparseWrite(unsigned sparse) { g_sparseFileSupport=sparse; }
Yann Collet6381e992016-05-31 02:29:45 +0200104static U32 g_dictIDFlag = 1;
105void FIO_setDictIDFlag(unsigned dictIDFlag) { g_dictIDFlag = dictIDFlag; }
Yann Colletc98f8e72016-06-20 16:31:24 +0200106static U32 g_checksumFlag = 1;
Yann Collet87cfbe32016-06-01 19:22:15 +0200107void FIO_setChecksumFlag(unsigned checksumFlag) { g_checksumFlag = checksumFlag; }
Yann Colletb09b12c2016-06-09 22:59:51 +0200108static U32 g_removeSrcFile = 0;
109void FIO_setRemoveSrcFile(unsigned flag) { g_removeSrcFile = (flag>0); }
Yann Colletd4cda272016-10-14 13:13:13 -0700110static U32 g_memLimit = 0;
111void FIO_setMemLimit(unsigned memLimit) { g_memLimit = memLimit; }
Yann Collet500014a2017-01-19 16:59:56 -0800112static U32 g_nbThreads = 1;
113void FIO_setNbThreads(unsigned nbThreads) {
114#ifndef ZSTD_MULTITHREAD
115 if (nbThreads > 1) DISPLAYLEVEL(2, "Note : multi-threading is disabled \n");
116#endif
117 g_nbThreads = nbThreads;
118}
Yann Collet512cbe82017-01-24 17:02:26 -0800119static U32 g_blockSize = 0;
120void FIO_setBlockSize(unsigned blockSize) {
121 if (blockSize && g_nbThreads==1)
122 DISPLAYLEVEL(2, "Setting block size is useless in single-thread mode \n");
123#ifdef ZSTD_MULTITHREAD
124 if (blockSize-1 < ZSTDMT_SECTION_SIZE_MIN-1) /* intentional underflow */
125 DISPLAYLEVEL(2, "Note : minimum block size is %u KB \n", (ZSTDMT_SECTION_SIZE_MIN>>10));
126#endif
127 g_blockSize = blockSize;
128}
Yann Collet8d8513f2017-01-30 14:37:08 -0800129#define FIO_OVERLAP_LOG_NOTSET 9999
130static U32 g_overlapLog = FIO_OVERLAP_LOG_NOTSET;
Yann Collet6be23372017-01-30 11:17:26 -0800131void FIO_setOverlapLog(unsigned overlapLog){
132 if (overlapLog && g_nbThreads==1)
133 DISPLAYLEVEL(2, "Setting overlapLog is useless in single-thread mode \n");
134 g_overlapLog = overlapLog;
135}
Yann Collet4856a002015-01-24 01:58:16 +0100136
137
Yann Collet459a6b72016-02-15 20:37:23 +0100138/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +0100139* Exceptions
Yann Colleteeb8ba12015-10-22 16:55:40 +0100140***************************************/
141#ifndef DEBUG
142# define DEBUG 0
143#endif
Yann Collet4856a002015-01-24 01:58:16 +0100144#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
145#define EXM_THROW(error, ...) \
146{ \
147 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
148 DISPLAYLEVEL(1, "Error %i : ", error); \
149 DISPLAYLEVEL(1, __VA_ARGS__); \
Yann Colletcdff19c2016-11-11 17:26:54 -0800150 DISPLAYLEVEL(1, " \n"); \
Yann Collet4856a002015-01-24 01:58:16 +0100151 exit(error); \
152}
153
154
Yann Collet459a6b72016-02-15 20:37:23 +0100155/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +0100156* Functions
Yann Colleteeb8ba12015-10-22 16:55:40 +0100157***************************************/
Yann Colletcdff19c2016-11-11 17:26:54 -0800158/** FIO_openSrcFile() :
159 * condition : `dstFileName` must be non-NULL.
160 * @result : FILE* to `dstFileName`, or NULL if it fails */
Yann Colletf0624362016-02-12 15:56:46 +0100161static FILE* FIO_openSrcFile(const char* srcFileName)
162{
163 FILE* f;
164
165 if (!strcmp (srcFileName, stdinmark)) {
166 DISPLAYLEVEL(4,"Using stdin for input\n");
167 f = stdin;
168 SET_BINARY_MODE(stdin);
169 } else {
Przemyslaw Skibinski5022a182017-01-25 13:11:26 +0100170 if (!UTIL_doesFileExists(srcFileName)) {
171 DISPLAYLEVEL(1, "zstd: %s is not a regular file -- ignored \n", srcFileName);
172 return NULL;
173 }
Yann Colletf0624362016-02-12 15:56:46 +0100174 f = fopen(srcFileName, "rb");
Yann Colletfb5e3852016-08-13 20:49:47 +0200175 if ( f==NULL ) DISPLAYLEVEL(1, "zstd: %s: %s \n", srcFileName, strerror(errno));
Yann Colletf0624362016-02-12 15:56:46 +0100176 }
177
Yann Colletf0624362016-02-12 15:56:46 +0100178 return f;
179}
180
Yann Collet43eeea42016-09-15 15:38:44 +0200181/** FIO_openDstFile() :
182 * condition : `dstFileName` must be non-NULL.
183 * @result : FILE* to `dstFileName`, or NULL if it fails */
Yann Colletf0624362016-02-12 15:56:46 +0100184static FILE* FIO_openDstFile(const char* dstFileName)
185{
186 FILE* f;
187
188 if (!strcmp (dstFileName, stdoutmark)) {
189 DISPLAYLEVEL(4,"Using stdout for output\n");
190 f = stdout;
191 SET_BINARY_MODE(stdout);
Yann Collet75424d12016-05-23 16:56:56 +0200192 if (g_sparseFileSupport==1) {
193 g_sparseFileSupport = 0;
194 DISPLAYLEVEL(4, "Sparse File Support is automatically disabled on stdout ; try --sparse \n");
195 }
Yann Colletf0624362016-02-12 15:56:46 +0100196 } else {
Yann Collet6381e992016-05-31 02:29:45 +0200197 if (!g_overwrite && strcmp (dstFileName, nulmark)) { /* Check if destination file already exists */
Yann Colletf0624362016-02-12 15:56:46 +0100198 f = fopen( dstFileName, "rb" );
199 if (f != 0) { /* dest file exists, prompt for overwrite authorization */
200 fclose(f);
201 if (g_displayLevel <= 1) {
202 /* No interaction possible */
203 DISPLAY("zstd: %s already exists; not overwritten \n", dstFileName);
Yann Colletb71adf42016-07-02 01:05:31 +0200204 return NULL;
Yann Colletf0624362016-02-12 15:56:46 +0100205 }
206 DISPLAY("zstd: %s already exists; do you wish to overwrite (y/N) ? ", dstFileName);
Yann Collet75424d12016-05-23 16:56:56 +0200207 { int ch = getchar();
Yann Colletf0624362016-02-12 15:56:46 +0100208 if ((ch!='Y') && (ch!='y')) {
209 DISPLAY(" not overwritten \n");
Yann Colletb71adf42016-07-02 01:05:31 +0200210 return NULL;
Yann Colletf0624362016-02-12 15:56:46 +0100211 }
212 while ((ch!=EOF) && (ch!='\n')) ch = getchar(); /* flush rest of input line */
213 } } }
214 f = fopen( dstFileName, "wb" );
Yann Colletfb5e3852016-08-13 20:49:47 +0200215 if (f==NULL) DISPLAYLEVEL(1, "zstd: %s: %s\n", dstFileName, strerror(errno));
Yann Colletf0624362016-02-12 15:56:46 +0100216 }
Yann Colletb71adf42016-07-02 01:05:31 +0200217
Yann Colletf0624362016-02-12 15:56:46 +0100218 return f;
219}
220
221
Yann Collet8a1d1a62016-03-10 21:02:25 +0100222/*! FIO_loadFile() :
Yann Collet09b21ee2016-03-15 12:56:03 +0100223* creates a buffer, pointed by `*bufferPtr`,
Yann Collet8a1d1a62016-03-10 21:02:25 +0100224* loads `filename` content into it,
Yann Collet6fca9e72016-05-31 02:40:42 +0200225* up to MAX_DICT_SIZE bytes.
226* @return : loaded size
Yann Colletdeb078b2015-12-17 20:30:14 +0100227*/
228static size_t FIO_loadFile(void** bufferPtr, const char* fileName)
229{
230 FILE* fileHandle;
Yann Colletdeb078b2015-12-17 20:30:14 +0100231 U64 fileSize;
232
233 *bufferPtr = NULL;
Yann Collet2ce49232016-02-02 14:36:49 +0100234 if (fileName == NULL) return 0;
Yann Colletdeb078b2015-12-17 20:30:14 +0100235
236 DISPLAYLEVEL(4,"Loading %s as dictionary \n", fileName);
237 fileHandle = fopen(fileName, "rb");
Yann Colletb71adf42016-07-02 01:05:31 +0200238 if (fileHandle==0) EXM_THROW(31, "zstd: %s: %s", fileName, strerror(errno));
inikep69fcd7c2016-04-28 12:23:33 +0200239 fileSize = UTIL_getFileSize(fileName);
Yann Collet2ce49232016-02-02 14:36:49 +0100240 if (fileSize > MAX_DICT_SIZE) {
Yann Colletdeb078b2015-12-17 20:30:14 +0100241 int seekResult;
242 if (fileSize > 1 GB) EXM_THROW(32, "Dictionary file %s is too large", fileName); /* avoid extreme cases */
inikep5a548702016-08-18 09:00:25 +0200243 DISPLAYLEVEL(2,"Dictionary %s is too large : using last %u bytes only \n", fileName, (U32)MAX_DICT_SIZE);
Yann Colletdeb078b2015-12-17 20:30:14 +0100244 seekResult = fseek(fileHandle, (long int)(fileSize-MAX_DICT_SIZE), SEEK_SET); /* use end of file */
Yann Colletb71adf42016-07-02 01:05:31 +0200245 if (seekResult != 0) EXM_THROW(33, "zstd: %s: %s", fileName, strerror(errno));
Yann Colletdeb078b2015-12-17 20:30:14 +0100246 fileSize = MAX_DICT_SIZE;
247 }
Yann Colletb71adf42016-07-02 01:05:31 +0200248 *bufferPtr = malloc((size_t)fileSize);
Yann Colleted7fb842016-07-02 11:14:30 +0200249 if (*bufferPtr==NULL) EXM_THROW(34, "zstd: %s", strerror(errno));
Yann Collet6fca9e72016-05-31 02:40:42 +0200250 { size_t const readSize = fread(*bufferPtr, 1, (size_t)fileSize, fileHandle);
251 if (readSize!=fileSize) EXM_THROW(35, "Error reading dictionary file %s", fileName); }
Yann Colletdeb078b2015-12-17 20:30:14 +0100252 fclose(fileHandle);
253 return (size_t)fileSize;
254}
255
inikep3c7c3522016-04-22 13:59:05 +0200256#ifndef ZSTD_NOCOMPRESS
Yann Collet4f137032015-12-17 02:23:58 +0100257
Yann Collet8a1d1a62016-03-10 21:02:25 +0100258/*-**********************************************************************
Yann Collet4f137032015-12-17 02:23:58 +0100259* Compression
260************************************************************************/
261typedef struct {
Yann Collet500014a2017-01-19 16:59:56 -0800262 FILE* srcFile;
263 FILE* dstFile;
Yann Collet4f137032015-12-17 02:23:58 +0100264 void* srcBuffer;
265 size_t srcBufferSize;
266 void* dstBuffer;
267 size_t dstBufferSize;
Yann Collet500014a2017-01-19 16:59:56 -0800268#ifdef ZSTD_MULTITHREAD
269 ZSTDMT_CCtx* cctx;
270#else
Yann Collet6263ba52016-08-13 23:45:45 +0200271 ZSTD_CStream* cctx;
Yann Collet500014a2017-01-19 16:59:56 -0800272#endif
Yann Collet4f137032015-12-17 02:23:58 +0100273} cRess_t;
274
Yann Collet500014a2017-01-19 16:59:56 -0800275static cRess_t FIO_createCResources(const char* dictFileName, int cLevel,
Przemyslaw Skibinski8349d672016-12-13 13:24:59 +0100276 U64 srcSize, ZSTD_compressionParameters* comprParams)
Yann Collet4f137032015-12-17 02:23:58 +0100277{
278 cRess_t ress;
Yann Collet5e80dd32016-07-13 17:38:39 +0200279 memset(&ress, 0, sizeof(ress));
Yann Collet4f137032015-12-17 02:23:58 +0100280
Yann Collet500014a2017-01-19 16:59:56 -0800281#ifdef ZSTD_MULTITHREAD
282 ress.cctx = ZSTDMT_createCCtx(g_nbThreads);
Yann Collet8dafb1a2017-01-25 17:01:13 -0800283 if (ress.cctx == NULL) EXM_THROW(30, "zstd: allocation error : can't create ZSTD_CStream");
Yann Collet8d8513f2017-01-30 14:37:08 -0800284 if ((cLevel==ZSTD_maxCLevel()) && (g_overlapLog==FIO_OVERLAP_LOG_NOTSET))
Yann Collet3672d062017-01-30 13:35:45 -0800285 ZSTDMT_setMTCtxParameter(ress.cctx, ZSTDMT_p_overlapSectionLog, 9); /* use complete window for overlap */
Yann Collet8d8513f2017-01-30 14:37:08 -0800286 if (g_overlapLog != FIO_OVERLAP_LOG_NOTSET)
Yann Collet6be23372017-01-30 11:17:26 -0800287 ZSTDMT_setMTCtxParameter(ress.cctx, ZSTDMT_p_overlapSectionLog, g_overlapLog);
Yann Collet500014a2017-01-19 16:59:56 -0800288#else
Yann Collet6263ba52016-08-13 23:45:45 +0200289 ress.cctx = ZSTD_createCStream();
290 if (ress.cctx == NULL) EXM_THROW(30, "zstd: allocation error : can't create ZSTD_CStream");
Yann Collet8dafb1a2017-01-25 17:01:13 -0800291#endif
Yann Collet6263ba52016-08-13 23:45:45 +0200292 ress.srcBufferSize = ZSTD_CStreamInSize();
Yann Collet4f137032015-12-17 02:23:58 +0100293 ress.srcBuffer = malloc(ress.srcBufferSize);
Yann Collet6263ba52016-08-13 23:45:45 +0200294 ress.dstBufferSize = ZSTD_CStreamOutSize();
Yann Collet4f137032015-12-17 02:23:58 +0100295 ress.dstBuffer = malloc(ress.dstBufferSize);
Yann Colleted7fb842016-07-02 11:14:30 +0200296 if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(31, "zstd: allocation error : not enough memory");
Yann Collet4f137032015-12-17 02:23:58 +0100297
298 /* dictionary */
Yann Collet43eeea42016-09-15 15:38:44 +0200299 { void* dictBuffer;
300 size_t const dictBuffSize = FIO_loadFile(&dictBuffer, dictFileName);
301 if (dictFileName && (dictBuffer==NULL)) EXM_THROW(32, "zstd: allocation error : can't create dictBuffer");
Yann Collet993060e2016-09-21 16:46:08 +0200302 { ZSTD_parameters params = ZSTD_getParams(cLevel, srcSize, dictBuffSize);
Yann Collet43eeea42016-09-15 15:38:44 +0200303 params.fParams.contentSizeFlag = 1;
304 params.fParams.checksumFlag = g_checksumFlag;
305 params.fParams.noDictIDFlag = !g_dictIDFlag;
Przemyslaw Skibinski8349d672016-12-13 13:24:59 +0100306 if (comprParams->windowLog) params.cParams.windowLog = comprParams->windowLog;
307 if (comprParams->chainLog) params.cParams.chainLog = comprParams->chainLog;
308 if (comprParams->hashLog) params.cParams.hashLog = comprParams->hashLog;
309 if (comprParams->searchLog) params.cParams.searchLog = comprParams->searchLog;
310 if (comprParams->searchLength) params.cParams.searchLength = comprParams->searchLength;
311 if (comprParams->targetLength) params.cParams.targetLength = comprParams->targetLength;
Przemyslaw Skibinskic71e5522016-12-13 20:04:32 +0100312 if (comprParams->strategy) params.cParams.strategy = (ZSTD_strategy)(comprParams->strategy - 1);
Yann Collet500014a2017-01-19 16:59:56 -0800313#ifdef ZSTD_MULTITHREAD
314 { size_t const errorCode = ZSTDMT_initCStream_advanced(ress.cctx, dictBuffer, dictBuffSize, params, srcSize);
Yann Collet512cbe82017-01-24 17:02:26 -0800315 if (ZSTD_isError(errorCode)) EXM_THROW(33, "Error initializing CStream : %s", ZSTD_getErrorName(errorCode));
316 ZSTDMT_setMTCtxParameter(ress.cctx, ZSTDMT_p_sectionSize, g_blockSize);
Yann Collet500014a2017-01-19 16:59:56 -0800317#else
Yann Collet993060e2016-09-21 16:46:08 +0200318 { size_t const errorCode = ZSTD_initCStream_advanced(ress.cctx, dictBuffer, dictBuffSize, params, srcSize);
Yann Collet43eeea42016-09-15 15:38:44 +0200319 if (ZSTD_isError(errorCode)) EXM_THROW(33, "Error initializing CStream : %s", ZSTD_getErrorName(errorCode));
Yann Collet512cbe82017-01-24 17:02:26 -0800320#endif
Yann Collet43eeea42016-09-15 15:38:44 +0200321 } }
322 free(dictBuffer);
323 }
Yann Collet4f137032015-12-17 02:23:58 +0100324
325 return ress;
326}
327
328static void FIO_freeCResources(cRess_t ress)
329{
Yann Collet4f137032015-12-17 02:23:58 +0100330 free(ress.srcBuffer);
331 free(ress.dstBuffer);
Yann Collet500014a2017-01-19 16:59:56 -0800332#ifdef ZSTD_MULTITHREAD
333 ZSTDMT_freeCCtx(ress.cctx);
334#else
Yann Collet43eeea42016-09-15 15:38:44 +0200335 ZSTD_freeCStream(ress.cctx); /* never fails */
Yann Collet500014a2017-01-19 16:59:56 -0800336#endif
Yann Collet4f137032015-12-17 02:23:58 +0100337}
338
339
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100340#ifdef ZSTD_GZCOMPRESS
341static unsigned long long FIO_compressGzFrame(cRess_t* ress, const char* srcFileName, U64 const srcFileSize, int compressionLevel, U64* readsize)
342{
343 unsigned long long inFileSize = 0, outFileSize = 0;
344 z_stream strm;
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100345 int ret;
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100346
Przemyslaw Skibinski64f72212017-02-13 21:00:41 +0100347 if (compressionLevel > Z_BEST_COMPRESSION) compressionLevel = Z_BEST_COMPRESSION;
348
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100349 strm.zalloc = Z_NULL;
350 strm.zfree = Z_NULL;
351 strm.opaque = Z_NULL;
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100352
353 if (deflateInit2(&strm, compressionLevel, Z_DEFLATED, 15 /* maxWindowLogSize */ + 16 /* gzip only */, 8, Z_DEFAULT_STRATEGY) != Z_OK)
354 EXM_THROW(71, "zstd: %s: deflateInit2 error %d \n", srcFileName, ret); /* see http://www.zlib.net/manual.html */
355
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100356 strm.next_in = 0;
357 strm.avail_in = Z_NULL;
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100358 strm.next_out = (Bytef*)ress->dstBuffer;
359 strm.avail_out = (uInt)ress->dstBufferSize;
360
361 while (1) {
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100362 if (strm.avail_in == 0) {
363 size_t const inSize = fread(ress->srcBuffer, 1, ress->srcBufferSize, ress->srcFile);
364 if (inSize == 0) break;
365 inFileSize += inSize;
366 strm.next_in = (z_const unsigned char*)ress->srcBuffer;
367 strm.avail_in = (uInt)inSize;
368 }
369 ret = deflate(&strm, Z_NO_FLUSH);
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100370 if (ret != Z_OK) EXM_THROW(72, "zstd: %s: deflate error %d \n", srcFileName, ret);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100371 { size_t const decompBytes = ress->dstBufferSize - strm.avail_out;
372 if (decompBytes) {
373 if (fwrite(ress->dstBuffer, 1, decompBytes, ress->dstFile) != decompBytes) EXM_THROW(73, "Write error : cannot write to output file");
374 outFileSize += decompBytes;
375 strm.next_out = (Bytef*)ress->dstBuffer;
376 strm.avail_out = (uInt)ress->dstBufferSize;
377 }
378 }
379 if (!srcFileSize) DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%%", (U32)(inFileSize>>20), (double)outFileSize/inFileSize*100)
380 else DISPLAYUPDATE(2, "\rRead : %u / %u MB ==> %.2f%%", (U32)(inFileSize>>20), (U32)(srcFileSize>>20), (double)outFileSize/inFileSize*100);
381 }
382
383 while (1) {
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100384 ret = deflate(&strm, Z_FINISH);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100385 { size_t const decompBytes = ress->dstBufferSize - strm.avail_out;
386 if (decompBytes) {
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100387 if (fwrite(ress->dstBuffer, 1, decompBytes, ress->dstFile) != decompBytes) EXM_THROW(75, "Write error : cannot write to output file");
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100388 outFileSize += decompBytes;
389 strm.next_out = (Bytef*)ress->dstBuffer;
390 strm.avail_out = (uInt)ress->dstBufferSize;
391 }
392 }
393 if (ret == Z_STREAM_END) break;
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100394 if (ret != Z_BUF_ERROR) EXM_THROW(77, "zstd: %s: deflate error %d \n", srcFileName, ret);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100395 }
396
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100397 ret = deflateEnd(&strm);
398 if (ret != Z_OK) EXM_THROW(79, "zstd: %s: deflateEnd error %d \n", srcFileName, ret);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100399 *readsize = inFileSize;
400
401 return outFileSize;
402}
403#endif
404
405
Yann Colletf0624362016-02-12 15:56:46 +0100406/*! FIO_compressFilename_internal() :
Yann Collet8a1d1a62016-03-10 21:02:25 +0100407 * same as FIO_compressFilename_extRess(), with `ress.desFile` already opened.
Yann Colletf0624362016-02-12 15:56:46 +0100408 * @return : 0 : compression completed correctly,
409 * 1 : missing or pb opening srcFileName
Yann Collet4f137032015-12-17 02:23:58 +0100410 */
Yann Colletf0624362016-02-12 15:56:46 +0100411static int FIO_compressFilename_internal(cRess_t ress,
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100412 const char* dstFileName, const char* srcFileName, int compressionLevel)
Yann Collet4f137032015-12-17 02:23:58 +0100413{
Yann Colletf8494622016-05-07 22:43:40 +0200414 FILE* const srcFile = ress.srcFile;
415 FILE* const dstFile = ress.dstFile;
Yann Colletb44be742016-03-26 20:52:14 +0100416 U64 readsize = 0;
Yann Collet4f137032015-12-17 02:23:58 +0100417 U64 compressedfilesize = 0;
inikep69fcd7c2016-04-28 12:23:33 +0200418 U64 const fileSize = UTIL_getFileSize(srcFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100419
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100420 if (g_compresionType) {
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100421#ifdef ZSTD_GZCOMPRESS
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100422 compressedfilesize = FIO_compressGzFrame(&ress, srcFileName, fileSize, compressionLevel, &readsize);
423 // printf("g_compresionType=%d compressionLevel=%d compressedfilesize=%d\n", g_compresionType, compressionLevel, (int)compressedfilesize);
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100424#else
Przemyslaw Skibinski4f9eaa72017-02-08 18:08:09 +0100425 (void)compressionLevel;
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100426 EXM_THROW(20, "zstd: %s: file cannot be compressed as gzip (zstd compiled without ZSTD_GZCOMPRESS) -- ignored \n", srcFileName);
427#endif
Przemyslaw Skibinski4f9eaa72017-02-08 18:08:09 +0100428 goto finish;
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100429 }
430
Yann Collet4f137032015-12-17 02:23:58 +0100431 /* init */
Yann Collet500014a2017-01-19 16:59:56 -0800432#ifdef ZSTD_MULTITHREAD
433 { size_t const resetError = ZSTDMT_resetCStream(ress.cctx, fileSize);
434#else
Yann Collet43eeea42016-09-15 15:38:44 +0200435 { size_t const resetError = ZSTD_resetCStream(ress.cctx, fileSize);
Yann Collet500014a2017-01-19 16:59:56 -0800436#endif
Yann Collet43eeea42016-09-15 15:38:44 +0200437 if (ZSTD_isError(resetError)) EXM_THROW(21, "Error initializing compression : %s", ZSTD_getErrorName(resetError));
438 }
Yann Collet4f137032015-12-17 02:23:58 +0100439
440 /* Main compression loop */
Yann Collet1c8e1942016-01-26 16:31:22 +0100441 while (1) {
Yann Collet4f137032015-12-17 02:23:58 +0100442 /* Fill input Buffer */
Yann Colletb44be742016-03-26 20:52:14 +0100443 size_t const inSize = fread(ress.srcBuffer, (size_t)1, ress.srcBufferSize, srcFile);
Yann Collet4f137032015-12-17 02:23:58 +0100444 if (inSize==0) break;
Yann Colletb44be742016-03-26 20:52:14 +0100445 readsize += inSize;
Yann Collet4f137032015-12-17 02:23:58 +0100446
Yann Collet53e17fb2016-08-17 01:39:22 +0200447 { ZSTD_inBuffer inBuff = { ress.srcBuffer, inSize, 0 };
Yann Collet500014a2017-01-19 16:59:56 -0800448 while (inBuff.pos != inBuff.size) { /* note : is there any possibility of endless loop ? for example, if outBuff is not large enough ? */
Yann Collet943cff92017-01-25 12:31:07 -0800449 ZSTD_outBuffer outBuff= { ress.dstBuffer, ress.dstBufferSize, 0 };
Yann Collet500014a2017-01-19 16:59:56 -0800450#ifdef ZSTD_MULTITHREAD
451 size_t const result = ZSTDMT_compressStream(ress.cctx, &outBuff, &inBuff);
452#else
453 size_t const result = ZSTD_compressStream(ress.cctx, &outBuff, &inBuff);
454#endif
455 if (ZSTD_isError(result)) EXM_THROW(23, "Compression error : %s ", ZSTD_getErrorName(result));
Yann Collet4f137032015-12-17 02:23:58 +0100456
Yann Collet943cff92017-01-25 12:31:07 -0800457 /* Write compressed stream */
458 if (outBuff.pos) {
459 size_t const sizeCheck = fwrite(ress.dstBuffer, 1, outBuff.pos, dstFile);
460 if (sizeCheck!=outBuff.pos) EXM_THROW(25, "Write error : cannot write compressed block into %s", dstFileName);
461 compressedfilesize += outBuff.pos;
462 } } }
Yann Collet5d9b8942017-01-27 13:30:18 -0800463#ifdef ZSTD_MULTITHREAD
464 if (!fileSize) DISPLAYUPDATE(2, "\rRead : %u MB", (U32)(readsize>>20))
465 else DISPLAYUPDATE(2, "\rRead : %u / %u MB", (U32)(readsize>>20), (U32)(fileSize>>20));
466#else
467 if (!fileSize) DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%%", (U32)(readsize>>20), (double)compressedfilesize/readsize*100)
468 else DISPLAYUPDATE(2, "\rRead : %u / %u MB ==> %.2f%%", (U32)(readsize>>20), (U32)(fileSize>>20), (double)compressedfilesize/readsize*100);
469#endif
Yann Collet4f137032015-12-17 02:23:58 +0100470 }
471
472 /* End of Frame */
Yann Collet500014a2017-01-19 16:59:56 -0800473 { size_t result = 1;
474 while (result!=0) { /* note : is there any possibility of endless loop ? */
475 ZSTD_outBuffer outBuff = { ress.dstBuffer, ress.dstBufferSize, 0 };
476#ifdef ZSTD_MULTITHREAD
477 result = ZSTDMT_endStream(ress.cctx, &outBuff);
478#else
479 result = ZSTD_endStream(ress.cctx, &outBuff);
480#endif
481 if (ZSTD_isError(result)) EXM_THROW(26, "Compression error during frame end : %s", ZSTD_getErrorName(result));
482 { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, outBuff.pos, dstFile);
483 if (sizeCheck!=outBuff.pos) EXM_THROW(27, "Write error : cannot write frame end into %s", dstFileName); }
484 compressedfilesize += outBuff.pos;
485 }
Yann Collet4f137032015-12-17 02:23:58 +0100486 }
487
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100488finish:
Yann Collet4f137032015-12-17 02:23:58 +0100489 /* Status */
490 DISPLAYLEVEL(2, "\r%79s\r", "");
David Lam0f270452016-08-13 11:26:21 -0700491 DISPLAYLEVEL(2,"%-20s :%6.2f%% (%6llu => %6llu bytes, %s) \n", srcFileName,
Yann Collet44f684d2016-07-13 19:30:40 +0200492 (double)compressedfilesize/(readsize+(!readsize) /* avoid div by zero */ )*100,
493 (unsigned long long)readsize, (unsigned long long) compressedfilesize,
494 dstFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100495
Yann Collet4f137032015-12-17 02:23:58 +0100496 return 0;
497}
498
499
Yann Colletb71adf42016-07-02 01:05:31 +0200500/*! FIO_compressFilename_srcFile() :
501 * note : ress.destFile already opened
Yann Collet459a6b72016-02-15 20:37:23 +0100502 * @return : 0 : compression completed correctly,
503 * 1 : missing or pb opening srcFileName
504 */
505static int FIO_compressFilename_srcFile(cRess_t ress,
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100506 const char* dstFileName, const char* srcFileName, int compressionLevel)
Yann Collet459a6b72016-02-15 20:37:23 +0100507{
508 int result;
509
510 /* File check */
Yann Colletb09b12c2016-06-09 22:59:51 +0200511 if (UTIL_isDirectory(srcFileName)) {
512 DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
513 return 1;
514 }
Przemyslaw Skibinski64fa2db2017-01-25 13:02:33 +0100515
Yann Collet459a6b72016-02-15 20:37:23 +0100516 ress.srcFile = FIO_openSrcFile(srcFileName);
517 if (!ress.srcFile) return 1; /* srcFile could not be opened */
518
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100519 result = FIO_compressFilename_internal(ress, dstFileName, srcFileName, compressionLevel);
Yann Collet459a6b72016-02-15 20:37:23 +0100520
Yann Collet459a6b72016-02-15 20:37:23 +0100521 fclose(ress.srcFile);
Yann Collet43eeea42016-09-15 15:38:44 +0200522 if (g_removeSrcFile && !result) { if (remove(srcFileName)) EXM_THROW(1, "zstd: %s: %s", srcFileName, strerror(errno)); } /* remove source file : --rm */
Yann Collet459a6b72016-02-15 20:37:23 +0100523 return result;
524}
525
526
Yann Colletb09b12c2016-06-09 22:59:51 +0200527/*! FIO_compressFilename_dstFile() :
Yann Colletf0624362016-02-12 15:56:46 +0100528 * @return : 0 : compression completed correctly,
Yann Colletb09b12c2016-06-09 22:59:51 +0200529 * 1 : pb
Yann Colletf0624362016-02-12 15:56:46 +0100530 */
Yann Colletb09b12c2016-06-09 22:59:51 +0200531static int FIO_compressFilename_dstFile(cRess_t ress,
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100532 const char* dstFileName, const char* srcFileName, int compressionLevel)
Yann Colletf0624362016-02-12 15:56:46 +0100533{
534 int result;
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100535 stat_t statbuf;
536 int stat_result = 0;
Yann Colletf0624362016-02-12 15:56:46 +0100537
538 ress.dstFile = FIO_openDstFile(dstFileName);
Yann Collet43eeea42016-09-15 15:38:44 +0200539 if (ress.dstFile==NULL) return 1; /* could not open dstFileName */
Yann Colletf0624362016-02-12 15:56:46 +0100540
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100541 if (strcmp (srcFileName, stdinmark) && UTIL_getFileStat(srcFileName, &statbuf)) stat_result = 1;
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100542 result = FIO_compressFilename_srcFile(ress, dstFileName, srcFileName, compressionLevel);
Chip Turner6de382c2016-03-13 22:24:46 -0700543
Yann Collet43eeea42016-09-15 15:38:44 +0200544 if (fclose(ress.dstFile)) { DISPLAYLEVEL(1, "zstd: %s: %s \n", dstFileName, strerror(errno)); result=1; } /* error closing dstFile */
Yann Collet44f684d2016-07-13 19:30:40 +0200545 if (result!=0) { if (remove(dstFileName)) EXM_THROW(1, "zstd: %s: %s", dstFileName, strerror(errno)); } /* remove operation artefact */
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100546 else if (strcmp (dstFileName, stdoutmark) && stat_result) UTIL_setFileStat(dstFileName, &statbuf);
Yann Colletf0624362016-02-12 15:56:46 +0100547 return result;
548}
549
550
Yann Collet9d909222015-12-17 14:09:55 +0100551int FIO_compressFilename(const char* dstFileName, const char* srcFileName,
Przemyslaw Skibinski8349d672016-12-13 13:24:59 +0100552 const char* dictFileName, int compressionLevel, ZSTD_compressionParameters* comprParams)
Yann Collet4856a002015-01-24 01:58:16 +0100553{
Yann Collet6381e992016-05-31 02:29:45 +0200554 clock_t const start = clock();
Yann Collet993060e2016-09-21 16:46:08 +0200555 U64 const srcSize = UTIL_getFileSize(srcFileName);
Yann Colletb09b12c2016-06-09 22:59:51 +0200556
Przemyslaw Skibinski8349d672016-12-13 13:24:59 +0100557 cRess_t const ress = FIO_createCResources(dictFileName, compressionLevel, srcSize, comprParams);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100558 int const result = FIO_compressFilename_dstFile(ress, dstFileName, srcFileName, compressionLevel);
Yann Collet9d909222015-12-17 14:09:55 +0100559
Yann Colletb71adf42016-07-02 01:05:31 +0200560 double const seconds = (double)(clock() - start) / CLOCKS_PER_SEC;
561 DISPLAYLEVEL(4, "Completed in %.2f sec \n", seconds);
562
563 FIO_freeCResources(ress);
564 return result;
Yann Collet4856a002015-01-24 01:58:16 +0100565}
566
567
Yann Collet9d909222015-12-17 14:09:55 +0100568int FIO_compressMultipleFilenames(const char** inFileNamesTable, unsigned nbFiles,
Yann Collet4f137032015-12-17 02:23:58 +0100569 const char* suffix,
Przemyslaw Skibinski8349d672016-12-13 13:24:59 +0100570 const char* dictFileName, int compressionLevel,
571 ZSTD_compressionParameters* comprParams)
Yann Collet4f137032015-12-17 02:23:58 +0100572{
Yann Collet4f137032015-12-17 02:23:58 +0100573 int missed_files = 0;
Yann Collet4f137032015-12-17 02:23:58 +0100574 size_t dfnSize = FNSPACE;
Yann Collet44f684d2016-07-13 19:30:40 +0200575 char* dstFileName = (char*)malloc(FNSPACE);
Yann Colletf8494622016-05-07 22:43:40 +0200576 size_t const suffixSize = suffix ? strlen(suffix) : 0;
Yann Collet993060e2016-09-21 16:46:08 +0200577 U64 const srcSize = (nbFiles != 1) ? 0 : UTIL_getFileSize(inFileNamesTable[0]) ;
Przemyslaw Skibinski8349d672016-12-13 13:24:59 +0100578 cRess_t ress = FIO_createCResources(dictFileName, compressionLevel, srcSize, comprParams);
Yann Collet4f137032015-12-17 02:23:58 +0100579
580 /* init */
Yann Collet44f684d2016-07-13 19:30:40 +0200581 if (dstFileName==NULL) EXM_THROW(27, "FIO_compressMultipleFilenames : allocation error for dstFileName");
582 if (suffix == NULL) EXM_THROW(28, "FIO_compressMultipleFilenames : dst unknown"); /* should never happen */
Yann Collet4f137032015-12-17 02:23:58 +0100583
584 /* loop on each file */
Yann Collet459a6b72016-02-15 20:37:23 +0100585 if (!strcmp(suffix, stdoutmark)) {
Yann Colletf8494622016-05-07 22:43:40 +0200586 unsigned u;
Yann Collet459a6b72016-02-15 20:37:23 +0100587 ress.dstFile = stdout;
inikep60af95d2016-05-19 10:29:49 +0200588 SET_BINARY_MODE(stdout);
Yann Collet459a6b72016-02-15 20:37:23 +0100589 for (u=0; u<nbFiles; u++)
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100590 missed_files += FIO_compressFilename_srcFile(ress, stdoutmark, inFileNamesTable[u], compressionLevel);
Przemyslaw Skibinski26306fc2016-11-03 11:38:01 +0100591 if (fclose(ress.dstFile)) EXM_THROW(29, "Write error : cannot properly close stdout");
Yann Collet459a6b72016-02-15 20:37:23 +0100592 } else {
Yann Colletf8494622016-05-07 22:43:40 +0200593 unsigned u;
Yann Colletf0624362016-02-12 15:56:46 +0100594 for (u=0; u<nbFiles; u++) {
595 size_t ifnSize = strlen(inFileNamesTable[u]);
596 if (dfnSize <= ifnSize+suffixSize+1) { free(dstFileName); dfnSize = ifnSize + 20; dstFileName = (char*)malloc(dfnSize); }
597 strcpy(dstFileName, inFileNamesTable[u]);
598 strcat(dstFileName, suffix);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100599 missed_files += FIO_compressFilename_dstFile(ress, dstFileName, inFileNamesTable[u], compressionLevel);
Yann Collet459a6b72016-02-15 20:37:23 +0100600 } }
Yann Collet4f137032015-12-17 02:23:58 +0100601
602 /* Close & Free */
603 FIO_freeCResources(ress);
604 free(dstFileName);
605
606 return missed_files;
607}
608
Yann Colletf8494622016-05-07 22:43:40 +0200609#endif /* #ifndef ZSTD_NOCOMPRESS */
inikep3c7c3522016-04-22 13:59:05 +0200610
Yann Collet4f137032015-12-17 02:23:58 +0100611
inikepdb396432016-04-22 18:22:30 +0200612
613#ifndef ZSTD_NODECOMPRESS
614
Yann Collet4f137032015-12-17 02:23:58 +0100615/* **************************************************************************
616* Decompression
617****************************************************************************/
Yann Colletdeb078b2015-12-17 20:30:14 +0100618typedef struct {
619 void* srcBuffer;
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100620 size_t srcBufferLoaded;
Yann Colletdeb078b2015-12-17 20:30:14 +0100621 size_t srcBufferSize;
622 void* dstBuffer;
623 size_t dstBufferSize;
Yann Collet6263ba52016-08-13 23:45:45 +0200624 ZSTD_DStream* dctx;
Yann Collet1f1f2392016-02-12 18:33:26 +0100625 FILE* dstFile;
Yann Colletdeb078b2015-12-17 20:30:14 +0100626} dRess_t;
627
628static dRess_t FIO_createDResources(const char* dictFileName)
629{
630 dRess_t ress;
Yann Collet5e80dd32016-07-13 17:38:39 +0200631 memset(&ress, 0, sizeof(ress));
Yann Colletdeb078b2015-12-17 20:30:14 +0100632
Yann Collet5e80dd32016-07-13 17:38:39 +0200633 /* Allocation */
Yann Collet6263ba52016-08-13 23:45:45 +0200634 ress.dctx = ZSTD_createDStream();
635 if (ress.dctx==NULL) EXM_THROW(60, "Can't create ZSTD_DStream");
Yann Colletbb002742017-01-25 16:25:38 -0800636 ZSTD_setDStreamParameter(ress.dctx, DStream_p_maxWindowSize, g_memLimit);
Yann Collet6263ba52016-08-13 23:45:45 +0200637 ress.srcBufferSize = ZSTD_DStreamInSize();
Yann Colletdeb078b2015-12-17 20:30:14 +0100638 ress.srcBuffer = malloc(ress.srcBufferSize);
Yann Collet6263ba52016-08-13 23:45:45 +0200639 ress.dstBufferSize = ZSTD_DStreamOutSize();
Yann Colletdeb078b2015-12-17 20:30:14 +0100640 ress.dstBuffer = malloc(ress.dstBufferSize);
641 if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(61, "Allocation error : not enough memory");
642
643 /* dictionary */
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200644 { void* dictBuffer;
645 size_t const dictBufferSize = FIO_loadFile(&dictBuffer, dictFileName);
646 size_t const initError = ZSTD_initDStream_usingDict(ress.dctx, dictBuffer, dictBufferSize);
647 if (ZSTD_isError(initError)) EXM_THROW(61, "ZSTD_initDStream_usingDict error : %s", ZSTD_getErrorName(initError));
648 free(dictBuffer);
649 }
Yann Colletdeb078b2015-12-17 20:30:14 +0100650
651 return ress;
652}
653
654static void FIO_freeDResources(dRess_t ress)
655{
Yann Collet6263ba52016-08-13 23:45:45 +0200656 size_t const errorCode = ZSTD_freeDStream(ress.dctx);
657 if (ZSTD_isError(errorCode)) EXM_THROW(69, "Error : can't free ZSTD_DStream context resource : %s", ZSTD_getErrorName(errorCode));
Yann Colletdeb078b2015-12-17 20:30:14 +0100658 free(ress.srcBuffer);
659 free(ress.dstBuffer);
Yann Colletdeb078b2015-12-17 20:30:14 +0100660}
Yann Collet4f137032015-12-17 02:23:58 +0100661
662
Yann Collet75424d12016-05-23 16:56:56 +0200663/** FIO_fwriteSparse() :
664* @return : storedSkips, to be provided to next call to FIO_fwriteSparse() of LZ4IO_fwriteSparseEnd() */
665static unsigned FIO_fwriteSparse(FILE* file, const void* buffer, size_t bufferSize, unsigned storedSkips)
666{
667 const size_t* const bufferT = (const size_t*)buffer; /* Buffer is supposed malloc'ed, hence aligned on size_t */
668 size_t bufferSizeT = bufferSize / sizeof(size_t);
669 const size_t* const bufferTEnd = bufferT + bufferSizeT;
670 const size_t* ptrT = bufferT;
671 static const size_t segmentSizeT = (32 KB) / sizeof(size_t); /* 0-test re-attempted every 32 KB */
672
673 if (!g_sparseFileSupport) { /* normal write */
674 size_t const sizeCheck = fwrite(buffer, 1, bufferSize, file);
675 if (sizeCheck != bufferSize) EXM_THROW(70, "Write error : cannot write decoded block");
676 return 0;
677 }
678
679 /* avoid int overflow */
680 if (storedSkips > 1 GB) {
681 int const seekResult = fseek(file, 1 GB, SEEK_CUR);
682 if (seekResult != 0) EXM_THROW(71, "1 GB skip error (sparse file support)");
683 storedSkips -= 1 GB;
684 }
685
686 while (ptrT < bufferTEnd) {
687 size_t seg0SizeT = segmentSizeT;
688 size_t nb0T;
689
690 /* count leading zeros */
691 if (seg0SizeT > bufferSizeT) seg0SizeT = bufferSizeT;
692 bufferSizeT -= seg0SizeT;
693 for (nb0T=0; (nb0T < seg0SizeT) && (ptrT[nb0T] == 0); nb0T++) ;
694 storedSkips += (unsigned)(nb0T * sizeof(size_t));
695
696 if (nb0T != seg0SizeT) { /* not all 0s */
697 int const seekResult = fseek(file, storedSkips, SEEK_CUR);
698 if (seekResult) EXM_THROW(72, "Sparse skip error ; try --no-sparse");
699 storedSkips = 0;
700 seg0SizeT -= nb0T;
701 ptrT += nb0T;
702 { size_t const sizeCheck = fwrite(ptrT, sizeof(size_t), seg0SizeT, file);
703 if (sizeCheck != seg0SizeT) EXM_THROW(73, "Write error : cannot write decoded block");
704 } }
705 ptrT += seg0SizeT;
706 }
707
708 { static size_t const maskT = sizeof(size_t)-1;
709 if (bufferSize & maskT) { /* size not multiple of sizeof(size_t) : implies end of block */
710 const char* const restStart = (const char*)bufferTEnd;
711 const char* restPtr = restStart;
712 size_t restSize = bufferSize & maskT;
713 const char* const restEnd = restStart + restSize;
714 for ( ; (restPtr < restEnd) && (*restPtr == 0); restPtr++) ;
715 storedSkips += (unsigned) (restPtr - restStart);
716 if (restPtr != restEnd) {
717 int seekResult = fseek(file, storedSkips, SEEK_CUR);
718 if (seekResult) EXM_THROW(74, "Sparse skip error ; try --no-sparse");
719 storedSkips = 0;
720 { size_t const sizeCheck = fwrite(restPtr, 1, restEnd - restPtr, file);
721 if (sizeCheck != (size_t)(restEnd - restPtr)) EXM_THROW(75, "Write error : cannot write decoded end of block");
722 } } } }
723
724 return storedSkips;
725}
726
727static void FIO_fwriteSparseEnd(FILE* file, unsigned storedSkips)
728{
729 if (storedSkips-->0) { /* implies g_sparseFileSupport>0 */
730 int const seekResult = fseek(file, storedSkips, SEEK_CUR);
731 if (seekResult != 0) EXM_THROW(69, "Final skip error (sparse file)\n");
732 { const char lastZeroByte[1] = { 0 };
733 size_t const sizeCheck = fwrite(lastZeroByte, 1, 1, file);
734 if (sizeCheck != 1) EXM_THROW(69, "Write error : cannot write last zero\n");
735 } }
736}
737
Yann Colletbe50aaa2015-09-10 23:26:09 +0100738
Yann Colletde95f962016-05-23 19:46:47 +0200739/** FIO_passThrough() : just copy input into output, for compatibility with gzip -df mode
740 @return : 0 (no error) */
Przemyslaw Skibinski7c6bbc32016-12-05 18:31:14 +0100741static unsigned FIO_passThrough(FILE* foutput, FILE* finput, void* buffer, size_t bufferSize, size_t alreadyLoaded)
Yann Colletde95f962016-05-23 19:46:47 +0200742{
Yann Collet179b1972016-11-02 17:30:49 -0700743 size_t const blockSize = MIN(64 KB, bufferSize);
Yann Colletddbb8e22016-05-24 00:52:14 +0200744 size_t readFromInput = 1;
Yann Colletde95f962016-05-23 19:46:47 +0200745 unsigned storedSkips = 0;
746
Przemyslaw Skibinski7c6bbc32016-12-05 18:31:14 +0100747 /* assumption : ress->srcBufferLoaded bytes already loaded and stored within buffer */
748 { size_t const sizeCheck = fwrite(buffer, 1, alreadyLoaded, foutput);
749 if (sizeCheck != alreadyLoaded) EXM_THROW(50, "Pass-through write error"); }
Yann Colletde95f962016-05-23 19:46:47 +0200750
Yann Colletddbb8e22016-05-24 00:52:14 +0200751 while (readFromInput) {
752 readFromInput = fread(buffer, 1, blockSize, finput);
753 storedSkips = FIO_fwriteSparse(foutput, buffer, readFromInput, storedSkips);
Yann Colletde95f962016-05-23 19:46:47 +0200754 }
755
756 FIO_fwriteSparseEnd(foutput, storedSkips);
757 return 0;
758}
759
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100760
761/** FIO_decompressFrame() :
762 @return : size of decoded frame
763*/
764unsigned long long FIO_decompressFrame(dRess_t* ress,
765 FILE* finput,
766 U64 alreadyDecoded)
767{
768 U64 frameSize = 0;
769 U32 storedSkips = 0;
770
771 ZSTD_resetDStream(ress->dctx);
772
773 /* Header loading (optional, saves one loop) */
774 { size_t const toRead = 9;
775 if (ress->srcBufferLoaded < toRead)
776 ress->srcBufferLoaded += fread(((char*)ress->srcBuffer) + ress->srcBufferLoaded, 1, toRead - ress->srcBufferLoaded, finput);
777 }
778
779 /* Main decompression Loop */
780 while (1) {
781 ZSTD_inBuffer inBuff = { ress->srcBuffer, ress->srcBufferLoaded, 0 };
782 ZSTD_outBuffer outBuff= { ress->dstBuffer, ress->dstBufferSize, 0 };
783 size_t const readSizeHint = ZSTD_decompressStream(ress->dctx, &outBuff, &inBuff);
784 if (ZSTD_isError(readSizeHint)) EXM_THROW(36, "Decoding error : %s", ZSTD_getErrorName(readSizeHint));
785
786 /* Write block */
Yann Collet500014a2017-01-19 16:59:56 -0800787 storedSkips = FIO_fwriteSparse(ress->dstFile, ress->dstBuffer, outBuff.pos, storedSkips);
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100788 frameSize += outBuff.pos;
789 DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)((alreadyDecoded+frameSize)>>20) );
790
791 if (inBuff.pos > 0) {
792 memmove(ress->srcBuffer, (char*)ress->srcBuffer + inBuff.pos, inBuff.size - inBuff.pos);
793 ress->srcBufferLoaded -= inBuff.pos;
794 }
795
796 if (readSizeHint == 0) break; /* end of frame */
797 if (inBuff.size != inBuff.pos) EXM_THROW(37, "Decoding error : should consume entire input");
798
799 /* Fill input buffer */
800 { size_t const toRead = MIN(readSizeHint, ress->srcBufferSize); /* support large skippable frames */
801 if (ress->srcBufferLoaded < toRead)
802 ress->srcBufferLoaded += fread(((char*)ress->srcBuffer) + ress->srcBufferLoaded, 1, toRead - ress->srcBufferLoaded, finput);
803 if (ress->srcBufferLoaded < toRead) EXM_THROW(39, "Read error : premature end");
804 } }
805
806 FIO_fwriteSparseEnd(ress->dstFile, storedSkips);
807
808 return frameSize;
809}
810
811
Przemyslaw Skibinski19aad422016-12-01 11:56:31 +0100812#ifdef ZSTD_GZDECOMPRESS
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100813static unsigned long long FIO_decompressGzFrame(dRess_t* ress, FILE* srcFile, const char* srcFileName)
Przemyslaw Skibinskib0f2ef22016-12-02 13:50:29 +0100814{
Yann Collet5bd42372016-12-02 12:40:57 -0800815 unsigned long long outFileSize = 0;
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +0100816 z_stream strm;
Yann Collet5bd42372016-12-02 12:40:57 -0800817
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +0100818 strm.zalloc = Z_NULL;
819 strm.zfree = Z_NULL;
820 strm.opaque = Z_NULL;
821 strm.next_in = 0;
822 strm.avail_in = Z_NULL;
Yann Collet5bd42372016-12-02 12:40:57 -0800823 if (inflateInit2(&strm, 15 /* maxWindowLogSize */ + 16 /* gzip only */) != Z_OK) return 0; /* see http://www.zlib.net/manual.html */
824
Yann Colletb02ac8d2017-02-03 08:43:06 -0800825 strm.next_out = (Bytef*)ress->dstBuffer;
Yann Colletc3cba9d2017-02-02 17:12:50 -0800826 strm.avail_out = (uInt)ress->dstBufferSize;
827 strm.avail_in = (uInt)ress->srcBufferLoaded;
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100828 strm.next_in = (z_const unsigned char*)ress->srcBuffer;
Przemyslaw Skibinski4b504f12016-12-02 13:11:39 +0100829
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +0100830 for ( ; ; ) {
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100831 int ret;
832 if (strm.avail_in == 0) {
833 ress->srcBufferLoaded = fread(ress->srcBuffer, 1, ress->srcBufferSize, srcFile);
834 if (ress->srcBufferLoaded == 0) break;
835 strm.next_in = (z_const unsigned char*)ress->srcBuffer;
Yann Colletc3cba9d2017-02-02 17:12:50 -0800836 strm.avail_in = (uInt)ress->srcBufferLoaded;
Przemyslaw Skibinski4b504f12016-12-02 13:11:39 +0100837 }
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100838 ret = inflate(&strm, Z_NO_FLUSH);
839 if (ret != Z_OK && ret != Z_STREAM_END) { DISPLAY("zstd: %s: inflate error %d \n", srcFileName, ret); return 0; }
840 { size_t const decompBytes = ress->dstBufferSize - strm.avail_out;
Yann Collet5bd42372016-12-02 12:40:57 -0800841 if (decompBytes) {
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100842 if (fwrite(ress->dstBuffer, 1, decompBytes, ress->dstFile) != decompBytes) EXM_THROW(31, "Write error : cannot write to output file");
Yann Collet5bd42372016-12-02 12:40:57 -0800843 outFileSize += decompBytes;
Yann Colletb02ac8d2017-02-03 08:43:06 -0800844 strm.next_out = (Bytef*)ress->dstBuffer;
Yann Colletc3cba9d2017-02-02 17:12:50 -0800845 strm.avail_out = (uInt)ress->dstBufferSize;
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100846 }
847 }
848 if (ret == Z_STREAM_END) break;
849 }
Przemyslaw Skibinski19aad422016-12-01 11:56:31 +0100850
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100851 if (strm.avail_in > 0) memmove(ress->srcBuffer, strm.next_in, strm.avail_in);
852 ress->srcBufferLoaded = strm.avail_in;
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +0100853 inflateEnd(&strm);
854 return outFileSize;
Przemyslaw Skibinski19aad422016-12-01 11:56:31 +0100855}
856#endif
857
858
Yann Collet1f1f2392016-02-12 18:33:26 +0100859/** FIO_decompressSrcFile() :
860 Decompression `srcFileName` into `ress.dstFile`
861 @return : 0 : OK
862 1 : operation not started
863*/
Yann Collet743b33f2016-12-02 15:18:57 -0800864static int FIO_decompressSrcFile(dRess_t ress, const char* dstFileName, const char* srcFileName)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100865{
Przemyslaw Skibinskib0f2ef22016-12-02 13:50:29 +0100866 FILE* srcFile;
Yann Colleta1dd6b92016-07-26 16:44:09 +0200867 unsigned readSomething = 0;
Yann Collet5bd42372016-12-02 12:40:57 -0800868 unsigned long long filesize = 0;
Przemyslaw Skibinski0e146752016-11-30 13:34:21 +0100869
Yann Colletb09b12c2016-06-09 22:59:51 +0200870 if (UTIL_isDirectory(srcFileName)) {
871 DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
872 return 1;
873 }
Przemyslaw Skibinski0e146752016-11-30 13:34:21 +0100874
Przemyslaw Skibinskib0f2ef22016-12-02 13:50:29 +0100875 srcFile = FIO_openSrcFile(srcFileName);
876 if (srcFile==0) return 1;
Yann Collet88fcd292015-11-25 14:42:45 +0100877
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +0100878 /* for each frame */
879 for ( ; ; ) {
880 /* check magic number -> version */
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +0100881 size_t const toRead = 4;
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +0100882 const BYTE* buf = (const BYTE*)ress.srcBuffer;
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100883 if (ress.srcBufferLoaded < toRead)
884 ress.srcBufferLoaded += fread((char*)ress.srcBuffer + ress.srcBufferLoaded, (size_t)1, toRead - ress.srcBufferLoaded, srcFile);
885 if (ress.srcBufferLoaded==0) {
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +0100886 if (readSomething==0) { DISPLAY("zstd: %s: unexpected end of file \n", srcFileName); fclose(srcFile); return 1; } /* srcFileName is empty */
887 break; /* no more input */
888 }
889 readSomething = 1; /* there is at least >= 4 bytes in srcFile */
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100890 if (ress.srcBufferLoaded < toRead) { DISPLAY("zstd: %s: unknown header \n", srcFileName); fclose(srcFile); return 1; } /* srcFileName is empty */
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +0100891 if (buf[0] == 31 && buf[1] == 139) { /* gz header */
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +0100892#ifdef ZSTD_GZDECOMPRESS
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100893 unsigned long long const result = FIO_decompressGzFrame(&ress, srcFile, srcFileName);
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +0100894 if (result == 0) return 1;
895 filesize += result;
Przemyslaw Skibinskiabfb51f2016-11-30 15:05:54 +0100896#else
Yann Collet5bd42372016-12-02 12:40:57 -0800897 DISPLAYLEVEL(1, "zstd: %s: gzip file cannot be uncompressed (zstd compiled without ZSTD_GZDECOMPRESS) -- ignored \n", srcFileName);
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +0100898 return 1;
Przemyslaw Skibinskiabfb51f2016-11-30 15:05:54 +0100899#endif
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +0100900 } else {
901 if (!ZSTD_isFrame(ress.srcBuffer, toRead)) {
Yann Collet743b33f2016-12-02 15:18:57 -0800902 if ((g_overwrite) && !strcmp (dstFileName, stdoutmark)) { /* pass-through mode */
Przemyslaw Skibinski7c6bbc32016-12-05 18:31:14 +0100903 unsigned const result = FIO_passThrough(ress.dstFile, srcFile, ress.srcBuffer, ress.srcBufferSize, ress.srcBufferLoaded);
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +0100904 if (fclose(srcFile)) EXM_THROW(32, "zstd: %s close error", srcFileName); /* error should never happen */
905 return result;
906 } else {
907 DISPLAYLEVEL(1, "zstd: %s: not in zstd format \n", srcFileName);
908 fclose(srcFile);
909 return 1;
910 } }
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100911 filesize += FIO_decompressFrame(&ress, srcFile, filesize);
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +0100912 }
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100913 }
914
Yann Colletdeb078b2015-12-17 20:30:14 +0100915 /* Final Status */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100916 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Collet1c69baa2016-08-28 12:47:17 -0700917 DISPLAYLEVEL(2, "%-20s: %llu bytes \n", srcFileName, filesize);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100918
Przemyslaw Skibinskib0f2ef22016-12-02 13:50:29 +0100919 /* Close file */
920 if (fclose(srcFile)) EXM_THROW(33, "zstd: %s close error", srcFileName); /* error should never happen */
921 if (g_removeSrcFile) { if (remove(srcFileName)) EXM_THROW(34, "zstd: %s: %s", srcFileName, strerror(errno)); };
Yann Collet1f1f2392016-02-12 18:33:26 +0100922 return 0;
923}
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100924
Yann Collet1f1f2392016-02-12 18:33:26 +0100925
926/** FIO_decompressFile_extRess() :
927 decompress `srcFileName` into `dstFileName`
928 @return : 0 : OK
929 1 : operation aborted (src not available, dst already taken, etc.)
930*/
Yann Colletb09b12c2016-06-09 22:59:51 +0200931static int FIO_decompressDstFile(dRess_t ress,
Yann Collet5bd42372016-12-02 12:40:57 -0800932 const char* dstFileName, const char* srcFileName)
Yann Collet1f1f2392016-02-12 18:33:26 +0100933{
Chip Turner6de382c2016-03-13 22:24:46 -0700934 int result;
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100935 stat_t statbuf;
936 int stat_result = 0;
Przemyslaw Skibinskia42794d2016-11-02 13:08:39 +0100937
Yann Collet1f1f2392016-02-12 18:33:26 +0100938 ress.dstFile = FIO_openDstFile(dstFileName);
939 if (ress.dstFile==0) return 1;
940
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100941 if (strcmp (srcFileName, stdinmark) && UTIL_getFileStat(srcFileName, &statbuf)) stat_result = 1;
Yann Collet743b33f2016-12-02 15:18:57 -0800942 result = FIO_decompressSrcFile(ress, dstFileName, srcFileName);
Yann Collet1f1f2392016-02-12 18:33:26 +0100943
Yann Collet0018ca22016-11-07 14:41:21 -0800944 if (fclose(ress.dstFile)) EXM_THROW(38, "Write error : cannot properly close %s", dstFileName);
Przemyslaw Skibinskia42794d2016-11-02 13:08:39 +0100945
Yann Colletaad9fe52016-09-07 07:00:08 +0200946 if ( (result != 0)
947 && strcmp(dstFileName, nulmark) /* special case : don't remove() /dev/null (#316) */
948 && remove(dstFileName) )
Yann Collet03d3f232016-09-07 07:01:33 +0200949 result=1; /* don't do anything special if remove() fails */
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100950 else if (strcmp (dstFileName, stdoutmark) && stat_result) UTIL_setFileStat(dstFileName, &statbuf);
Chip Turner6de382c2016-03-13 22:24:46 -0700951 return result;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100952}
953
954
Yann Colletdeb078b2015-12-17 20:30:14 +0100955int FIO_decompressFilename(const char* dstFileName, const char* srcFileName,
956 const char* dictFileName)
957{
958 int missingFiles = 0;
959 dRess_t ress = FIO_createDResources(dictFileName);
960
Yann Colletb09b12c2016-06-09 22:59:51 +0200961 missingFiles += FIO_decompressDstFile(ress, dstFileName, srcFileName);
Yann Colletdeb078b2015-12-17 20:30:14 +0100962
963 FIO_freeDResources(ress);
964 return missingFiles;
965}
966
967
968#define MAXSUFFIXSIZE 8
969int FIO_decompressMultipleFilenames(const char** srcNamesTable, unsigned nbFiles,
970 const char* suffix,
971 const char* dictFileName)
972{
Yann Colletdeb078b2015-12-17 20:30:14 +0100973 int skippedFiles = 0;
974 int missingFiles = 0;
Yann Collet8b23eea2016-05-10 05:37:43 +0200975 dRess_t ress = FIO_createDResources(dictFileName);
Yann Colletdeb078b2015-12-17 20:30:14 +0100976
Yann Collet44f684d2016-07-13 19:30:40 +0200977 if (suffix==NULL) EXM_THROW(70, "zstd: decompression: unknown dst"); /* should never happen */
978
Yann Collet743b33f2016-12-02 15:18:57 -0800979 if (!strcmp(suffix, stdoutmark) || !strcmp(suffix, nulmark)) { /* special cases : -c or -t */
Yann Colletf8494622016-05-07 22:43:40 +0200980 unsigned u;
Yann Colletaccfd802016-02-15 19:33:16 +0100981 ress.dstFile = FIO_openDstFile(suffix);
982 if (ress.dstFile == 0) EXM_THROW(71, "cannot open %s", suffix);
983 for (u=0; u<nbFiles; u++)
Yann Collet743b33f2016-12-02 15:18:57 -0800984 missingFiles += FIO_decompressSrcFile(ress, suffix, srcNamesTable[u]);
Przemyslaw Skibinski26306fc2016-11-03 11:38:01 +0100985 if (fclose(ress.dstFile)) EXM_THROW(72, "Write error : cannot properly close stdout");
Yann Colletaccfd802016-02-15 19:33:16 +0100986 } else {
Yann Collet44f684d2016-07-13 19:30:40 +0200987 size_t const suffixSize = strlen(suffix);
Przemyslaw Skibinski4b504f12016-12-02 13:11:39 +0100988 size_t const gzipSuffixSize = strlen(GZ_EXTENSION);
Yann Collet8b23eea2016-05-10 05:37:43 +0200989 size_t dfnSize = FNSPACE;
Yann Colletf8494622016-05-07 22:43:40 +0200990 unsigned u;
Yann Collet8b23eea2016-05-10 05:37:43 +0200991 char* dstFileName = (char*)malloc(FNSPACE);
Yann Collet44f684d2016-07-13 19:30:40 +0200992 if (dstFileName==NULL) EXM_THROW(73, "not enough memory for dstFileName");
Yann Collet1f1f2392016-02-12 18:33:26 +0100993 for (u=0; u<nbFiles; u++) { /* create dstFileName */
Yann Collet8b23eea2016-05-10 05:37:43 +0200994 const char* const srcFileName = srcNamesTable[u];
995 size_t const sfnSize = strlen(srcFileName);
996 const char* const suffixPtr = srcFileName + sfnSize - suffixSize;
Przemyslaw Skibinski4b504f12016-12-02 13:11:39 +0100997 const char* const gzipSuffixPtr = srcFileName + sfnSize - gzipSuffixSize;
Yann Colletaccfd802016-02-15 19:33:16 +0100998 if (dfnSize+suffixSize <= sfnSize+1) {
999 free(dstFileName);
1000 dfnSize = sfnSize + 20;
1001 dstFileName = (char*)malloc(dfnSize);
Yann Collet44f684d2016-07-13 19:30:40 +02001002 if (dstFileName==NULL) EXM_THROW(74, "not enough memory for dstFileName");
Yann Colletaccfd802016-02-15 19:33:16 +01001003 }
1004 if (sfnSize <= suffixSize || strcmp(suffixPtr, suffix) != 0) {
Przemyslaw Skibinski4b504f12016-12-02 13:11:39 +01001005 if (sfnSize <= gzipSuffixSize || strcmp(gzipSuffixPtr, GZ_EXTENSION) != 0) {
Przemyslaw Skibinski8489f182016-12-05 13:47:00 +01001006 DISPLAYLEVEL(1, "zstd: %s: unknown suffix (%s/%s expected) -- ignored \n", srcFileName, suffix, GZ_EXTENSION);
Przemyslaw Skibinski0e146752016-11-30 13:34:21 +01001007 skippedFiles++;
1008 continue;
1009 } else {
Przemyslaw Skibinski4b504f12016-12-02 13:11:39 +01001010 memcpy(dstFileName, srcFileName, sfnSize - gzipSuffixSize);
1011 dstFileName[sfnSize-gzipSuffixSize] = '\0';
Przemyslaw Skibinski0e146752016-11-30 13:34:21 +01001012 }
1013 } else {
1014 memcpy(dstFileName, srcFileName, sfnSize - suffixSize);
1015 dstFileName[sfnSize-suffixSize] = '\0';
Yann Collet1f1f2392016-02-12 18:33:26 +01001016 }
Yann Colletdeb078b2015-12-17 20:30:14 +01001017
Yann Colletb09b12c2016-06-09 22:59:51 +02001018 missingFiles += FIO_decompressDstFile(ress, dstFileName, srcFileName);
Yann Collet8b23eea2016-05-10 05:37:43 +02001019 }
1020 free(dstFileName);
1021 }
Yann Colletdeb078b2015-12-17 20:30:14 +01001022
1023 FIO_freeDResources(ress);
Yann Colletdeb078b2015-12-17 20:30:14 +01001024 return missingFiles + skippedFiles;
1025}
Yann Colletaccfd802016-02-15 19:33:16 +01001026
Yann Collet8b23eea2016-05-10 05:37:43 +02001027#endif /* #ifndef ZSTD_NODECOMPRESS */