blob: 430127771ea4cfc009b02f34c236bfaf35e4b8cc [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
347 strm.zalloc = Z_NULL;
348 strm.zfree = Z_NULL;
349 strm.opaque = Z_NULL;
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100350
351 if (deflateInit2(&strm, compressionLevel, Z_DEFLATED, 15 /* maxWindowLogSize */ + 16 /* gzip only */, 8, Z_DEFAULT_STRATEGY) != Z_OK)
352 EXM_THROW(71, "zstd: %s: deflateInit2 error %d \n", srcFileName, ret); /* see http://www.zlib.net/manual.html */
353
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100354 strm.next_in = 0;
355 strm.avail_in = Z_NULL;
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100356 strm.next_out = (Bytef*)ress->dstBuffer;
357 strm.avail_out = (uInt)ress->dstBufferSize;
358
359 while (1) {
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100360 if (strm.avail_in == 0) {
361 size_t const inSize = fread(ress->srcBuffer, 1, ress->srcBufferSize, ress->srcFile);
362 if (inSize == 0) break;
363 inFileSize += inSize;
364 strm.next_in = (z_const unsigned char*)ress->srcBuffer;
365 strm.avail_in = (uInt)inSize;
366 }
367 ret = deflate(&strm, Z_NO_FLUSH);
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100368 if (ret != Z_OK) EXM_THROW(72, "zstd: %s: deflate error %d \n", srcFileName, ret);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100369 { size_t const decompBytes = ress->dstBufferSize - strm.avail_out;
370 if (decompBytes) {
371 if (fwrite(ress->dstBuffer, 1, decompBytes, ress->dstFile) != decompBytes) EXM_THROW(73, "Write error : cannot write to output file");
372 outFileSize += decompBytes;
373 strm.next_out = (Bytef*)ress->dstBuffer;
374 strm.avail_out = (uInt)ress->dstBufferSize;
375 }
376 }
377 if (!srcFileSize) DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%%", (U32)(inFileSize>>20), (double)outFileSize/inFileSize*100)
378 else DISPLAYUPDATE(2, "\rRead : %u / %u MB ==> %.2f%%", (U32)(inFileSize>>20), (U32)(srcFileSize>>20), (double)outFileSize/inFileSize*100);
379 }
380
381 while (1) {
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100382 ret = deflate(&strm, Z_FINISH);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100383 { size_t const decompBytes = ress->dstBufferSize - strm.avail_out;
384 if (decompBytes) {
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100385 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 +0100386 outFileSize += decompBytes;
387 strm.next_out = (Bytef*)ress->dstBuffer;
388 strm.avail_out = (uInt)ress->dstBufferSize;
389 }
390 }
391 if (ret == Z_STREAM_END) break;
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100392 if (ret != Z_BUF_ERROR) EXM_THROW(77, "zstd: %s: deflate error %d \n", srcFileName, ret);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100393 }
394
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100395 ret = deflateEnd(&strm);
396 if (ret != Z_OK) EXM_THROW(79, "zstd: %s: deflateEnd error %d \n", srcFileName, ret);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100397 *readsize = inFileSize;
398
399 return outFileSize;
400}
401#endif
402
403
Yann Colletf0624362016-02-12 15:56:46 +0100404/*! FIO_compressFilename_internal() :
Yann Collet8a1d1a62016-03-10 21:02:25 +0100405 * same as FIO_compressFilename_extRess(), with `ress.desFile` already opened.
Yann Colletf0624362016-02-12 15:56:46 +0100406 * @return : 0 : compression completed correctly,
407 * 1 : missing or pb opening srcFileName
Yann Collet4f137032015-12-17 02:23:58 +0100408 */
Yann Colletf0624362016-02-12 15:56:46 +0100409static int FIO_compressFilename_internal(cRess_t ress,
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100410 const char* dstFileName, const char* srcFileName, int compressionLevel)
Yann Collet4f137032015-12-17 02:23:58 +0100411{
Yann Colletf8494622016-05-07 22:43:40 +0200412 FILE* const srcFile = ress.srcFile;
413 FILE* const dstFile = ress.dstFile;
Yann Colletb44be742016-03-26 20:52:14 +0100414 U64 readsize = 0;
Yann Collet4f137032015-12-17 02:23:58 +0100415 U64 compressedfilesize = 0;
inikep69fcd7c2016-04-28 12:23:33 +0200416 U64 const fileSize = UTIL_getFileSize(srcFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100417
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100418 if (g_compresionType) {
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100419#ifdef ZSTD_GZCOMPRESS
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100420 compressedfilesize = FIO_compressGzFrame(&ress, srcFileName, fileSize, compressionLevel, &readsize);
421 // printf("g_compresionType=%d compressionLevel=%d compressedfilesize=%d\n", g_compresionType, compressionLevel, (int)compressedfilesize);
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100422#else
Przemyslaw Skibinski4f9eaa72017-02-08 18:08:09 +0100423 (void)compressionLevel;
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100424 EXM_THROW(20, "zstd: %s: file cannot be compressed as gzip (zstd compiled without ZSTD_GZCOMPRESS) -- ignored \n", srcFileName);
425#endif
Przemyslaw Skibinski4f9eaa72017-02-08 18:08:09 +0100426 goto finish;
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100427 }
428
Yann Collet4f137032015-12-17 02:23:58 +0100429 /* init */
Yann Collet500014a2017-01-19 16:59:56 -0800430#ifdef ZSTD_MULTITHREAD
431 { size_t const resetError = ZSTDMT_resetCStream(ress.cctx, fileSize);
432#else
Yann Collet43eeea42016-09-15 15:38:44 +0200433 { size_t const resetError = ZSTD_resetCStream(ress.cctx, fileSize);
Yann Collet500014a2017-01-19 16:59:56 -0800434#endif
Yann Collet43eeea42016-09-15 15:38:44 +0200435 if (ZSTD_isError(resetError)) EXM_THROW(21, "Error initializing compression : %s", ZSTD_getErrorName(resetError));
436 }
Yann Collet4f137032015-12-17 02:23:58 +0100437
438 /* Main compression loop */
Yann Collet1c8e1942016-01-26 16:31:22 +0100439 while (1) {
Yann Collet4f137032015-12-17 02:23:58 +0100440 /* Fill input Buffer */
Yann Colletb44be742016-03-26 20:52:14 +0100441 size_t const inSize = fread(ress.srcBuffer, (size_t)1, ress.srcBufferSize, srcFile);
Yann Collet4f137032015-12-17 02:23:58 +0100442 if (inSize==0) break;
Yann Colletb44be742016-03-26 20:52:14 +0100443 readsize += inSize;
Yann Collet4f137032015-12-17 02:23:58 +0100444
Yann Collet53e17fb2016-08-17 01:39:22 +0200445 { ZSTD_inBuffer inBuff = { ress.srcBuffer, inSize, 0 };
Yann Collet500014a2017-01-19 16:59:56 -0800446 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 -0800447 ZSTD_outBuffer outBuff= { ress.dstBuffer, ress.dstBufferSize, 0 };
Yann Collet500014a2017-01-19 16:59:56 -0800448#ifdef ZSTD_MULTITHREAD
449 size_t const result = ZSTDMT_compressStream(ress.cctx, &outBuff, &inBuff);
450#else
451 size_t const result = ZSTD_compressStream(ress.cctx, &outBuff, &inBuff);
452#endif
453 if (ZSTD_isError(result)) EXM_THROW(23, "Compression error : %s ", ZSTD_getErrorName(result));
Yann Collet4f137032015-12-17 02:23:58 +0100454
Yann Collet943cff92017-01-25 12:31:07 -0800455 /* Write compressed stream */
456 if (outBuff.pos) {
457 size_t const sizeCheck = fwrite(ress.dstBuffer, 1, outBuff.pos, dstFile);
458 if (sizeCheck!=outBuff.pos) EXM_THROW(25, "Write error : cannot write compressed block into %s", dstFileName);
459 compressedfilesize += outBuff.pos;
460 } } }
Yann Collet5d9b8942017-01-27 13:30:18 -0800461#ifdef ZSTD_MULTITHREAD
462 if (!fileSize) DISPLAYUPDATE(2, "\rRead : %u MB", (U32)(readsize>>20))
463 else DISPLAYUPDATE(2, "\rRead : %u / %u MB", (U32)(readsize>>20), (U32)(fileSize>>20));
464#else
465 if (!fileSize) DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%%", (U32)(readsize>>20), (double)compressedfilesize/readsize*100)
466 else DISPLAYUPDATE(2, "\rRead : %u / %u MB ==> %.2f%%", (U32)(readsize>>20), (U32)(fileSize>>20), (double)compressedfilesize/readsize*100);
467#endif
Yann Collet4f137032015-12-17 02:23:58 +0100468 }
469
470 /* End of Frame */
Yann Collet500014a2017-01-19 16:59:56 -0800471 { size_t result = 1;
472 while (result!=0) { /* note : is there any possibility of endless loop ? */
473 ZSTD_outBuffer outBuff = { ress.dstBuffer, ress.dstBufferSize, 0 };
474#ifdef ZSTD_MULTITHREAD
475 result = ZSTDMT_endStream(ress.cctx, &outBuff);
476#else
477 result = ZSTD_endStream(ress.cctx, &outBuff);
478#endif
479 if (ZSTD_isError(result)) EXM_THROW(26, "Compression error during frame end : %s", ZSTD_getErrorName(result));
480 { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, outBuff.pos, dstFile);
481 if (sizeCheck!=outBuff.pos) EXM_THROW(27, "Write error : cannot write frame end into %s", dstFileName); }
482 compressedfilesize += outBuff.pos;
483 }
Yann Collet4f137032015-12-17 02:23:58 +0100484 }
485
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100486finish:
Yann Collet4f137032015-12-17 02:23:58 +0100487 /* Status */
488 DISPLAYLEVEL(2, "\r%79s\r", "");
David Lam0f270452016-08-13 11:26:21 -0700489 DISPLAYLEVEL(2,"%-20s :%6.2f%% (%6llu => %6llu bytes, %s) \n", srcFileName,
Yann Collet44f684d2016-07-13 19:30:40 +0200490 (double)compressedfilesize/(readsize+(!readsize) /* avoid div by zero */ )*100,
491 (unsigned long long)readsize, (unsigned long long) compressedfilesize,
492 dstFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100493
Yann Collet4f137032015-12-17 02:23:58 +0100494 return 0;
495}
496
497
Yann Colletb71adf42016-07-02 01:05:31 +0200498/*! FIO_compressFilename_srcFile() :
499 * note : ress.destFile already opened
Yann Collet459a6b72016-02-15 20:37:23 +0100500 * @return : 0 : compression completed correctly,
501 * 1 : missing or pb opening srcFileName
502 */
503static int FIO_compressFilename_srcFile(cRess_t ress,
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100504 const char* dstFileName, const char* srcFileName, int compressionLevel)
Yann Collet459a6b72016-02-15 20:37:23 +0100505{
506 int result;
507
508 /* File check */
Yann Colletb09b12c2016-06-09 22:59:51 +0200509 if (UTIL_isDirectory(srcFileName)) {
510 DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
511 return 1;
512 }
Przemyslaw Skibinski64fa2db2017-01-25 13:02:33 +0100513
Yann Collet459a6b72016-02-15 20:37:23 +0100514 ress.srcFile = FIO_openSrcFile(srcFileName);
515 if (!ress.srcFile) return 1; /* srcFile could not be opened */
516
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100517 result = FIO_compressFilename_internal(ress, dstFileName, srcFileName, compressionLevel);
Yann Collet459a6b72016-02-15 20:37:23 +0100518
Yann Collet459a6b72016-02-15 20:37:23 +0100519 fclose(ress.srcFile);
Yann Collet43eeea42016-09-15 15:38:44 +0200520 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 +0100521 return result;
522}
523
524
Yann Colletb09b12c2016-06-09 22:59:51 +0200525/*! FIO_compressFilename_dstFile() :
Yann Colletf0624362016-02-12 15:56:46 +0100526 * @return : 0 : compression completed correctly,
Yann Colletb09b12c2016-06-09 22:59:51 +0200527 * 1 : pb
Yann Colletf0624362016-02-12 15:56:46 +0100528 */
Yann Colletb09b12c2016-06-09 22:59:51 +0200529static int FIO_compressFilename_dstFile(cRess_t ress,
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100530 const char* dstFileName, const char* srcFileName, int compressionLevel)
Yann Colletf0624362016-02-12 15:56:46 +0100531{
532 int result;
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100533 stat_t statbuf;
534 int stat_result = 0;
Yann Colletf0624362016-02-12 15:56:46 +0100535
536 ress.dstFile = FIO_openDstFile(dstFileName);
Yann Collet43eeea42016-09-15 15:38:44 +0200537 if (ress.dstFile==NULL) return 1; /* could not open dstFileName */
Yann Colletf0624362016-02-12 15:56:46 +0100538
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100539 if (strcmp (srcFileName, stdinmark) && UTIL_getFileStat(srcFileName, &statbuf)) stat_result = 1;
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100540 result = FIO_compressFilename_srcFile(ress, dstFileName, srcFileName, compressionLevel);
Chip Turner6de382c2016-03-13 22:24:46 -0700541
Yann Collet43eeea42016-09-15 15:38:44 +0200542 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 +0200543 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 +0100544 else if (strcmp (dstFileName, stdoutmark) && stat_result) UTIL_setFileStat(dstFileName, &statbuf);
Yann Colletf0624362016-02-12 15:56:46 +0100545 return result;
546}
547
548
Yann Collet9d909222015-12-17 14:09:55 +0100549int FIO_compressFilename(const char* dstFileName, const char* srcFileName,
Przemyslaw Skibinski8349d672016-12-13 13:24:59 +0100550 const char* dictFileName, int compressionLevel, ZSTD_compressionParameters* comprParams)
Yann Collet4856a002015-01-24 01:58:16 +0100551{
Yann Collet6381e992016-05-31 02:29:45 +0200552 clock_t const start = clock();
Yann Collet993060e2016-09-21 16:46:08 +0200553 U64 const srcSize = UTIL_getFileSize(srcFileName);
Yann Colletb09b12c2016-06-09 22:59:51 +0200554
Przemyslaw Skibinski8349d672016-12-13 13:24:59 +0100555 cRess_t const ress = FIO_createCResources(dictFileName, compressionLevel, srcSize, comprParams);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100556 int const result = FIO_compressFilename_dstFile(ress, dstFileName, srcFileName, compressionLevel);
Yann Collet9d909222015-12-17 14:09:55 +0100557
Yann Colletb71adf42016-07-02 01:05:31 +0200558 double const seconds = (double)(clock() - start) / CLOCKS_PER_SEC;
559 DISPLAYLEVEL(4, "Completed in %.2f sec \n", seconds);
560
561 FIO_freeCResources(ress);
562 return result;
Yann Collet4856a002015-01-24 01:58:16 +0100563}
564
565
Yann Collet9d909222015-12-17 14:09:55 +0100566int FIO_compressMultipleFilenames(const char** inFileNamesTable, unsigned nbFiles,
Yann Collet4f137032015-12-17 02:23:58 +0100567 const char* suffix,
Przemyslaw Skibinski8349d672016-12-13 13:24:59 +0100568 const char* dictFileName, int compressionLevel,
569 ZSTD_compressionParameters* comprParams)
Yann Collet4f137032015-12-17 02:23:58 +0100570{
Yann Collet4f137032015-12-17 02:23:58 +0100571 int missed_files = 0;
Yann Collet4f137032015-12-17 02:23:58 +0100572 size_t dfnSize = FNSPACE;
Yann Collet44f684d2016-07-13 19:30:40 +0200573 char* dstFileName = (char*)malloc(FNSPACE);
Yann Colletf8494622016-05-07 22:43:40 +0200574 size_t const suffixSize = suffix ? strlen(suffix) : 0;
Yann Collet993060e2016-09-21 16:46:08 +0200575 U64 const srcSize = (nbFiles != 1) ? 0 : UTIL_getFileSize(inFileNamesTable[0]) ;
Przemyslaw Skibinski8349d672016-12-13 13:24:59 +0100576 cRess_t ress = FIO_createCResources(dictFileName, compressionLevel, srcSize, comprParams);
Yann Collet4f137032015-12-17 02:23:58 +0100577
578 /* init */
Yann Collet44f684d2016-07-13 19:30:40 +0200579 if (dstFileName==NULL) EXM_THROW(27, "FIO_compressMultipleFilenames : allocation error for dstFileName");
580 if (suffix == NULL) EXM_THROW(28, "FIO_compressMultipleFilenames : dst unknown"); /* should never happen */
Yann Collet4f137032015-12-17 02:23:58 +0100581
582 /* loop on each file */
Yann Collet459a6b72016-02-15 20:37:23 +0100583 if (!strcmp(suffix, stdoutmark)) {
Yann Colletf8494622016-05-07 22:43:40 +0200584 unsigned u;
Yann Collet459a6b72016-02-15 20:37:23 +0100585 ress.dstFile = stdout;
inikep60af95d2016-05-19 10:29:49 +0200586 SET_BINARY_MODE(stdout);
Yann Collet459a6b72016-02-15 20:37:23 +0100587 for (u=0; u<nbFiles; u++)
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100588 missed_files += FIO_compressFilename_srcFile(ress, stdoutmark, inFileNamesTable[u], compressionLevel);
Przemyslaw Skibinski26306fc2016-11-03 11:38:01 +0100589 if (fclose(ress.dstFile)) EXM_THROW(29, "Write error : cannot properly close stdout");
Yann Collet459a6b72016-02-15 20:37:23 +0100590 } else {
Yann Colletf8494622016-05-07 22:43:40 +0200591 unsigned u;
Yann Colletf0624362016-02-12 15:56:46 +0100592 for (u=0; u<nbFiles; u++) {
593 size_t ifnSize = strlen(inFileNamesTable[u]);
594 if (dfnSize <= ifnSize+suffixSize+1) { free(dstFileName); dfnSize = ifnSize + 20; dstFileName = (char*)malloc(dfnSize); }
595 strcpy(dstFileName, inFileNamesTable[u]);
596 strcat(dstFileName, suffix);
Przemyslaw Skibinski02018c82017-02-08 16:54:23 +0100597 missed_files += FIO_compressFilename_dstFile(ress, dstFileName, inFileNamesTable[u], compressionLevel);
Yann Collet459a6b72016-02-15 20:37:23 +0100598 } }
Yann Collet4f137032015-12-17 02:23:58 +0100599
600 /* Close & Free */
601 FIO_freeCResources(ress);
602 free(dstFileName);
603
604 return missed_files;
605}
606
Yann Colletf8494622016-05-07 22:43:40 +0200607#endif /* #ifndef ZSTD_NOCOMPRESS */
inikep3c7c3522016-04-22 13:59:05 +0200608
Yann Collet4f137032015-12-17 02:23:58 +0100609
inikepdb396432016-04-22 18:22:30 +0200610
611#ifndef ZSTD_NODECOMPRESS
612
Yann Collet4f137032015-12-17 02:23:58 +0100613/* **************************************************************************
614* Decompression
615****************************************************************************/
Yann Colletdeb078b2015-12-17 20:30:14 +0100616typedef struct {
617 void* srcBuffer;
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100618 size_t srcBufferLoaded;
Yann Colletdeb078b2015-12-17 20:30:14 +0100619 size_t srcBufferSize;
620 void* dstBuffer;
621 size_t dstBufferSize;
Yann Collet6263ba52016-08-13 23:45:45 +0200622 ZSTD_DStream* dctx;
Yann Collet1f1f2392016-02-12 18:33:26 +0100623 FILE* dstFile;
Yann Colletdeb078b2015-12-17 20:30:14 +0100624} dRess_t;
625
626static dRess_t FIO_createDResources(const char* dictFileName)
627{
628 dRess_t ress;
Yann Collet5e80dd32016-07-13 17:38:39 +0200629 memset(&ress, 0, sizeof(ress));
Yann Colletdeb078b2015-12-17 20:30:14 +0100630
Yann Collet5e80dd32016-07-13 17:38:39 +0200631 /* Allocation */
Yann Collet6263ba52016-08-13 23:45:45 +0200632 ress.dctx = ZSTD_createDStream();
633 if (ress.dctx==NULL) EXM_THROW(60, "Can't create ZSTD_DStream");
Yann Colletbb002742017-01-25 16:25:38 -0800634 ZSTD_setDStreamParameter(ress.dctx, DStream_p_maxWindowSize, g_memLimit);
Yann Collet6263ba52016-08-13 23:45:45 +0200635 ress.srcBufferSize = ZSTD_DStreamInSize();
Yann Colletdeb078b2015-12-17 20:30:14 +0100636 ress.srcBuffer = malloc(ress.srcBufferSize);
Yann Collet6263ba52016-08-13 23:45:45 +0200637 ress.dstBufferSize = ZSTD_DStreamOutSize();
Yann Colletdeb078b2015-12-17 20:30:14 +0100638 ress.dstBuffer = malloc(ress.dstBufferSize);
639 if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(61, "Allocation error : not enough memory");
640
641 /* dictionary */
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200642 { void* dictBuffer;
643 size_t const dictBufferSize = FIO_loadFile(&dictBuffer, dictFileName);
644 size_t const initError = ZSTD_initDStream_usingDict(ress.dctx, dictBuffer, dictBufferSize);
645 if (ZSTD_isError(initError)) EXM_THROW(61, "ZSTD_initDStream_usingDict error : %s", ZSTD_getErrorName(initError));
646 free(dictBuffer);
647 }
Yann Colletdeb078b2015-12-17 20:30:14 +0100648
649 return ress;
650}
651
652static void FIO_freeDResources(dRess_t ress)
653{
Yann Collet6263ba52016-08-13 23:45:45 +0200654 size_t const errorCode = ZSTD_freeDStream(ress.dctx);
655 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 +0100656 free(ress.srcBuffer);
657 free(ress.dstBuffer);
Yann Colletdeb078b2015-12-17 20:30:14 +0100658}
Yann Collet4f137032015-12-17 02:23:58 +0100659
660
Yann Collet75424d12016-05-23 16:56:56 +0200661/** FIO_fwriteSparse() :
662* @return : storedSkips, to be provided to next call to FIO_fwriteSparse() of LZ4IO_fwriteSparseEnd() */
663static unsigned FIO_fwriteSparse(FILE* file, const void* buffer, size_t bufferSize, unsigned storedSkips)
664{
665 const size_t* const bufferT = (const size_t*)buffer; /* Buffer is supposed malloc'ed, hence aligned on size_t */
666 size_t bufferSizeT = bufferSize / sizeof(size_t);
667 const size_t* const bufferTEnd = bufferT + bufferSizeT;
668 const size_t* ptrT = bufferT;
669 static const size_t segmentSizeT = (32 KB) / sizeof(size_t); /* 0-test re-attempted every 32 KB */
670
671 if (!g_sparseFileSupport) { /* normal write */
672 size_t const sizeCheck = fwrite(buffer, 1, bufferSize, file);
673 if (sizeCheck != bufferSize) EXM_THROW(70, "Write error : cannot write decoded block");
674 return 0;
675 }
676
677 /* avoid int overflow */
678 if (storedSkips > 1 GB) {
679 int const seekResult = fseek(file, 1 GB, SEEK_CUR);
680 if (seekResult != 0) EXM_THROW(71, "1 GB skip error (sparse file support)");
681 storedSkips -= 1 GB;
682 }
683
684 while (ptrT < bufferTEnd) {
685 size_t seg0SizeT = segmentSizeT;
686 size_t nb0T;
687
688 /* count leading zeros */
689 if (seg0SizeT > bufferSizeT) seg0SizeT = bufferSizeT;
690 bufferSizeT -= seg0SizeT;
691 for (nb0T=0; (nb0T < seg0SizeT) && (ptrT[nb0T] == 0); nb0T++) ;
692 storedSkips += (unsigned)(nb0T * sizeof(size_t));
693
694 if (nb0T != seg0SizeT) { /* not all 0s */
695 int const seekResult = fseek(file, storedSkips, SEEK_CUR);
696 if (seekResult) EXM_THROW(72, "Sparse skip error ; try --no-sparse");
697 storedSkips = 0;
698 seg0SizeT -= nb0T;
699 ptrT += nb0T;
700 { size_t const sizeCheck = fwrite(ptrT, sizeof(size_t), seg0SizeT, file);
701 if (sizeCheck != seg0SizeT) EXM_THROW(73, "Write error : cannot write decoded block");
702 } }
703 ptrT += seg0SizeT;
704 }
705
706 { static size_t const maskT = sizeof(size_t)-1;
707 if (bufferSize & maskT) { /* size not multiple of sizeof(size_t) : implies end of block */
708 const char* const restStart = (const char*)bufferTEnd;
709 const char* restPtr = restStart;
710 size_t restSize = bufferSize & maskT;
711 const char* const restEnd = restStart + restSize;
712 for ( ; (restPtr < restEnd) && (*restPtr == 0); restPtr++) ;
713 storedSkips += (unsigned) (restPtr - restStart);
714 if (restPtr != restEnd) {
715 int seekResult = fseek(file, storedSkips, SEEK_CUR);
716 if (seekResult) EXM_THROW(74, "Sparse skip error ; try --no-sparse");
717 storedSkips = 0;
718 { size_t const sizeCheck = fwrite(restPtr, 1, restEnd - restPtr, file);
719 if (sizeCheck != (size_t)(restEnd - restPtr)) EXM_THROW(75, "Write error : cannot write decoded end of block");
720 } } } }
721
722 return storedSkips;
723}
724
725static void FIO_fwriteSparseEnd(FILE* file, unsigned storedSkips)
726{
727 if (storedSkips-->0) { /* implies g_sparseFileSupport>0 */
728 int const seekResult = fseek(file, storedSkips, SEEK_CUR);
729 if (seekResult != 0) EXM_THROW(69, "Final skip error (sparse file)\n");
730 { const char lastZeroByte[1] = { 0 };
731 size_t const sizeCheck = fwrite(lastZeroByte, 1, 1, file);
732 if (sizeCheck != 1) EXM_THROW(69, "Write error : cannot write last zero\n");
733 } }
734}
735
Yann Colletbe50aaa2015-09-10 23:26:09 +0100736
Yann Colletde95f962016-05-23 19:46:47 +0200737/** FIO_passThrough() : just copy input into output, for compatibility with gzip -df mode
738 @return : 0 (no error) */
Przemyslaw Skibinski7c6bbc32016-12-05 18:31:14 +0100739static unsigned FIO_passThrough(FILE* foutput, FILE* finput, void* buffer, size_t bufferSize, size_t alreadyLoaded)
Yann Colletde95f962016-05-23 19:46:47 +0200740{
Yann Collet179b1972016-11-02 17:30:49 -0700741 size_t const blockSize = MIN(64 KB, bufferSize);
Yann Colletddbb8e22016-05-24 00:52:14 +0200742 size_t readFromInput = 1;
Yann Colletde95f962016-05-23 19:46:47 +0200743 unsigned storedSkips = 0;
744
Przemyslaw Skibinski7c6bbc32016-12-05 18:31:14 +0100745 /* assumption : ress->srcBufferLoaded bytes already loaded and stored within buffer */
746 { size_t const sizeCheck = fwrite(buffer, 1, alreadyLoaded, foutput);
747 if (sizeCheck != alreadyLoaded) EXM_THROW(50, "Pass-through write error"); }
Yann Colletde95f962016-05-23 19:46:47 +0200748
Yann Colletddbb8e22016-05-24 00:52:14 +0200749 while (readFromInput) {
750 readFromInput = fread(buffer, 1, blockSize, finput);
751 storedSkips = FIO_fwriteSparse(foutput, buffer, readFromInput, storedSkips);
Yann Colletde95f962016-05-23 19:46:47 +0200752 }
753
754 FIO_fwriteSparseEnd(foutput, storedSkips);
755 return 0;
756}
757
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100758
759/** FIO_decompressFrame() :
760 @return : size of decoded frame
761*/
762unsigned long long FIO_decompressFrame(dRess_t* ress,
763 FILE* finput,
764 U64 alreadyDecoded)
765{
766 U64 frameSize = 0;
767 U32 storedSkips = 0;
768
769 ZSTD_resetDStream(ress->dctx);
770
771 /* Header loading (optional, saves one loop) */
772 { size_t const toRead = 9;
773 if (ress->srcBufferLoaded < toRead)
774 ress->srcBufferLoaded += fread(((char*)ress->srcBuffer) + ress->srcBufferLoaded, 1, toRead - ress->srcBufferLoaded, finput);
775 }
776
777 /* Main decompression Loop */
778 while (1) {
779 ZSTD_inBuffer inBuff = { ress->srcBuffer, ress->srcBufferLoaded, 0 };
780 ZSTD_outBuffer outBuff= { ress->dstBuffer, ress->dstBufferSize, 0 };
781 size_t const readSizeHint = ZSTD_decompressStream(ress->dctx, &outBuff, &inBuff);
782 if (ZSTD_isError(readSizeHint)) EXM_THROW(36, "Decoding error : %s", ZSTD_getErrorName(readSizeHint));
783
784 /* Write block */
Yann Collet500014a2017-01-19 16:59:56 -0800785 storedSkips = FIO_fwriteSparse(ress->dstFile, ress->dstBuffer, outBuff.pos, storedSkips);
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100786 frameSize += outBuff.pos;
787 DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)((alreadyDecoded+frameSize)>>20) );
788
789 if (inBuff.pos > 0) {
790 memmove(ress->srcBuffer, (char*)ress->srcBuffer + inBuff.pos, inBuff.size - inBuff.pos);
791 ress->srcBufferLoaded -= inBuff.pos;
792 }
793
794 if (readSizeHint == 0) break; /* end of frame */
795 if (inBuff.size != inBuff.pos) EXM_THROW(37, "Decoding error : should consume entire input");
796
797 /* Fill input buffer */
798 { size_t const toRead = MIN(readSizeHint, ress->srcBufferSize); /* support large skippable frames */
799 if (ress->srcBufferLoaded < toRead)
800 ress->srcBufferLoaded += fread(((char*)ress->srcBuffer) + ress->srcBufferLoaded, 1, toRead - ress->srcBufferLoaded, finput);
801 if (ress->srcBufferLoaded < toRead) EXM_THROW(39, "Read error : premature end");
802 } }
803
804 FIO_fwriteSparseEnd(ress->dstFile, storedSkips);
805
806 return frameSize;
807}
808
809
Przemyslaw Skibinski19aad422016-12-01 11:56:31 +0100810#ifdef ZSTD_GZDECOMPRESS
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100811static unsigned long long FIO_decompressGzFrame(dRess_t* ress, FILE* srcFile, const char* srcFileName)
Przemyslaw Skibinskib0f2ef22016-12-02 13:50:29 +0100812{
Yann Collet5bd42372016-12-02 12:40:57 -0800813 unsigned long long outFileSize = 0;
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +0100814 z_stream strm;
Yann Collet5bd42372016-12-02 12:40:57 -0800815
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +0100816 strm.zalloc = Z_NULL;
817 strm.zfree = Z_NULL;
818 strm.opaque = Z_NULL;
819 strm.next_in = 0;
820 strm.avail_in = Z_NULL;
Yann Collet5bd42372016-12-02 12:40:57 -0800821 if (inflateInit2(&strm, 15 /* maxWindowLogSize */ + 16 /* gzip only */) != Z_OK) return 0; /* see http://www.zlib.net/manual.html */
822
Yann Colletb02ac8d2017-02-03 08:43:06 -0800823 strm.next_out = (Bytef*)ress->dstBuffer;
Yann Colletc3cba9d2017-02-02 17:12:50 -0800824 strm.avail_out = (uInt)ress->dstBufferSize;
825 strm.avail_in = (uInt)ress->srcBufferLoaded;
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100826 strm.next_in = (z_const unsigned char*)ress->srcBuffer;
Przemyslaw Skibinski4b504f12016-12-02 13:11:39 +0100827
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +0100828 for ( ; ; ) {
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100829 int ret;
830 if (strm.avail_in == 0) {
831 ress->srcBufferLoaded = fread(ress->srcBuffer, 1, ress->srcBufferSize, srcFile);
832 if (ress->srcBufferLoaded == 0) break;
833 strm.next_in = (z_const unsigned char*)ress->srcBuffer;
Yann Colletc3cba9d2017-02-02 17:12:50 -0800834 strm.avail_in = (uInt)ress->srcBufferLoaded;
Przemyslaw Skibinski4b504f12016-12-02 13:11:39 +0100835 }
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100836 ret = inflate(&strm, Z_NO_FLUSH);
837 if (ret != Z_OK && ret != Z_STREAM_END) { DISPLAY("zstd: %s: inflate error %d \n", srcFileName, ret); return 0; }
838 { size_t const decompBytes = ress->dstBufferSize - strm.avail_out;
Yann Collet5bd42372016-12-02 12:40:57 -0800839 if (decompBytes) {
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100840 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 -0800841 outFileSize += decompBytes;
Yann Colletb02ac8d2017-02-03 08:43:06 -0800842 strm.next_out = (Bytef*)ress->dstBuffer;
Yann Colletc3cba9d2017-02-02 17:12:50 -0800843 strm.avail_out = (uInt)ress->dstBufferSize;
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100844 }
845 }
846 if (ret == Z_STREAM_END) break;
847 }
Przemyslaw Skibinski19aad422016-12-01 11:56:31 +0100848
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100849 if (strm.avail_in > 0) memmove(ress->srcBuffer, strm.next_in, strm.avail_in);
850 ress->srcBufferLoaded = strm.avail_in;
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +0100851 inflateEnd(&strm);
852 return outFileSize;
Przemyslaw Skibinski19aad422016-12-01 11:56:31 +0100853}
854#endif
855
856
Yann Collet1f1f2392016-02-12 18:33:26 +0100857/** FIO_decompressSrcFile() :
858 Decompression `srcFileName` into `ress.dstFile`
859 @return : 0 : OK
860 1 : operation not started
861*/
Yann Collet743b33f2016-12-02 15:18:57 -0800862static int FIO_decompressSrcFile(dRess_t ress, const char* dstFileName, const char* srcFileName)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100863{
Przemyslaw Skibinskib0f2ef22016-12-02 13:50:29 +0100864 FILE* srcFile;
Yann Colleta1dd6b92016-07-26 16:44:09 +0200865 unsigned readSomething = 0;
Yann Collet5bd42372016-12-02 12:40:57 -0800866 unsigned long long filesize = 0;
Przemyslaw Skibinski0e146752016-11-30 13:34:21 +0100867
Yann Colletb09b12c2016-06-09 22:59:51 +0200868 if (UTIL_isDirectory(srcFileName)) {
869 DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
870 return 1;
871 }
Przemyslaw Skibinski0e146752016-11-30 13:34:21 +0100872
Przemyslaw Skibinskib0f2ef22016-12-02 13:50:29 +0100873 srcFile = FIO_openSrcFile(srcFileName);
874 if (srcFile==0) return 1;
Yann Collet88fcd292015-11-25 14:42:45 +0100875
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +0100876 /* for each frame */
877 for ( ; ; ) {
878 /* check magic number -> version */
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +0100879 size_t const toRead = 4;
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +0100880 const BYTE* buf = (const BYTE*)ress.srcBuffer;
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100881 if (ress.srcBufferLoaded < toRead)
882 ress.srcBufferLoaded += fread((char*)ress.srcBuffer + ress.srcBufferLoaded, (size_t)1, toRead - ress.srcBufferLoaded, srcFile);
883 if (ress.srcBufferLoaded==0) {
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +0100884 if (readSomething==0) { DISPLAY("zstd: %s: unexpected end of file \n", srcFileName); fclose(srcFile); return 1; } /* srcFileName is empty */
885 break; /* no more input */
886 }
887 readSomething = 1; /* there is at least >= 4 bytes in srcFile */
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100888 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 +0100889 if (buf[0] == 31 && buf[1] == 139) { /* gz header */
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +0100890#ifdef ZSTD_GZDECOMPRESS
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100891 unsigned long long const result = FIO_decompressGzFrame(&ress, srcFile, srcFileName);
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +0100892 if (result == 0) return 1;
893 filesize += result;
Przemyslaw Skibinskiabfb51f2016-11-30 15:05:54 +0100894#else
Yann Collet5bd42372016-12-02 12:40:57 -0800895 DISPLAYLEVEL(1, "zstd: %s: gzip file cannot be uncompressed (zstd compiled without ZSTD_GZDECOMPRESS) -- ignored \n", srcFileName);
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +0100896 return 1;
Przemyslaw Skibinskiabfb51f2016-11-30 15:05:54 +0100897#endif
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +0100898 } else {
899 if (!ZSTD_isFrame(ress.srcBuffer, toRead)) {
Yann Collet743b33f2016-12-02 15:18:57 -0800900 if ((g_overwrite) && !strcmp (dstFileName, stdoutmark)) { /* pass-through mode */
Przemyslaw Skibinski7c6bbc32016-12-05 18:31:14 +0100901 unsigned const result = FIO_passThrough(ress.dstFile, srcFile, ress.srcBuffer, ress.srcBufferSize, ress.srcBufferLoaded);
Przemyslaw Skibinski690753e2016-12-02 16:20:16 +0100902 if (fclose(srcFile)) EXM_THROW(32, "zstd: %s close error", srcFileName); /* error should never happen */
903 return result;
904 } else {
905 DISPLAYLEVEL(1, "zstd: %s: not in zstd format \n", srcFileName);
906 fclose(srcFile);
907 return 1;
908 } }
Przemyslaw Skibinskib493e3b2016-12-05 17:39:38 +0100909 filesize += FIO_decompressFrame(&ress, srcFile, filesize);
Przemyslaw Skibinskic5eebca2016-12-02 15:01:31 +0100910 }
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100911 }
912
Yann Colletdeb078b2015-12-17 20:30:14 +0100913 /* Final Status */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100914 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Collet1c69baa2016-08-28 12:47:17 -0700915 DISPLAYLEVEL(2, "%-20s: %llu bytes \n", srcFileName, filesize);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100916
Przemyslaw Skibinskib0f2ef22016-12-02 13:50:29 +0100917 /* Close file */
918 if (fclose(srcFile)) EXM_THROW(33, "zstd: %s close error", srcFileName); /* error should never happen */
919 if (g_removeSrcFile) { if (remove(srcFileName)) EXM_THROW(34, "zstd: %s: %s", srcFileName, strerror(errno)); };
Yann Collet1f1f2392016-02-12 18:33:26 +0100920 return 0;
921}
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100922
Yann Collet1f1f2392016-02-12 18:33:26 +0100923
924/** FIO_decompressFile_extRess() :
925 decompress `srcFileName` into `dstFileName`
926 @return : 0 : OK
927 1 : operation aborted (src not available, dst already taken, etc.)
928*/
Yann Colletb09b12c2016-06-09 22:59:51 +0200929static int FIO_decompressDstFile(dRess_t ress,
Yann Collet5bd42372016-12-02 12:40:57 -0800930 const char* dstFileName, const char* srcFileName)
Yann Collet1f1f2392016-02-12 18:33:26 +0100931{
Chip Turner6de382c2016-03-13 22:24:46 -0700932 int result;
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100933 stat_t statbuf;
934 int stat_result = 0;
Przemyslaw Skibinskia42794d2016-11-02 13:08:39 +0100935
Yann Collet1f1f2392016-02-12 18:33:26 +0100936 ress.dstFile = FIO_openDstFile(dstFileName);
937 if (ress.dstFile==0) return 1;
938
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100939 if (strcmp (srcFileName, stdinmark) && UTIL_getFileStat(srcFileName, &statbuf)) stat_result = 1;
Yann Collet743b33f2016-12-02 15:18:57 -0800940 result = FIO_decompressSrcFile(ress, dstFileName, srcFileName);
Yann Collet1f1f2392016-02-12 18:33:26 +0100941
Yann Collet0018ca22016-11-07 14:41:21 -0800942 if (fclose(ress.dstFile)) EXM_THROW(38, "Write error : cannot properly close %s", dstFileName);
Przemyslaw Skibinskia42794d2016-11-02 13:08:39 +0100943
Yann Colletaad9fe52016-09-07 07:00:08 +0200944 if ( (result != 0)
945 && strcmp(dstFileName, nulmark) /* special case : don't remove() /dev/null (#316) */
946 && remove(dstFileName) )
Yann Collet03d3f232016-09-07 07:01:33 +0200947 result=1; /* don't do anything special if remove() fails */
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100948 else if (strcmp (dstFileName, stdoutmark) && stat_result) UTIL_setFileStat(dstFileName, &statbuf);
Chip Turner6de382c2016-03-13 22:24:46 -0700949 return result;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100950}
951
952
Yann Colletdeb078b2015-12-17 20:30:14 +0100953int FIO_decompressFilename(const char* dstFileName, const char* srcFileName,
954 const char* dictFileName)
955{
956 int missingFiles = 0;
957 dRess_t ress = FIO_createDResources(dictFileName);
958
Yann Colletb09b12c2016-06-09 22:59:51 +0200959 missingFiles += FIO_decompressDstFile(ress, dstFileName, srcFileName);
Yann Colletdeb078b2015-12-17 20:30:14 +0100960
961 FIO_freeDResources(ress);
962 return missingFiles;
963}
964
965
966#define MAXSUFFIXSIZE 8
967int FIO_decompressMultipleFilenames(const char** srcNamesTable, unsigned nbFiles,
968 const char* suffix,
969 const char* dictFileName)
970{
Yann Colletdeb078b2015-12-17 20:30:14 +0100971 int skippedFiles = 0;
972 int missingFiles = 0;
Yann Collet8b23eea2016-05-10 05:37:43 +0200973 dRess_t ress = FIO_createDResources(dictFileName);
Yann Colletdeb078b2015-12-17 20:30:14 +0100974
Yann Collet44f684d2016-07-13 19:30:40 +0200975 if (suffix==NULL) EXM_THROW(70, "zstd: decompression: unknown dst"); /* should never happen */
976
Yann Collet743b33f2016-12-02 15:18:57 -0800977 if (!strcmp(suffix, stdoutmark) || !strcmp(suffix, nulmark)) { /* special cases : -c or -t */
Yann Colletf8494622016-05-07 22:43:40 +0200978 unsigned u;
Yann Colletaccfd802016-02-15 19:33:16 +0100979 ress.dstFile = FIO_openDstFile(suffix);
980 if (ress.dstFile == 0) EXM_THROW(71, "cannot open %s", suffix);
981 for (u=0; u<nbFiles; u++)
Yann Collet743b33f2016-12-02 15:18:57 -0800982 missingFiles += FIO_decompressSrcFile(ress, suffix, srcNamesTable[u]);
Przemyslaw Skibinski26306fc2016-11-03 11:38:01 +0100983 if (fclose(ress.dstFile)) EXM_THROW(72, "Write error : cannot properly close stdout");
Yann Colletaccfd802016-02-15 19:33:16 +0100984 } else {
Yann Collet44f684d2016-07-13 19:30:40 +0200985 size_t const suffixSize = strlen(suffix);
Przemyslaw Skibinski4b504f12016-12-02 13:11:39 +0100986 size_t const gzipSuffixSize = strlen(GZ_EXTENSION);
Yann Collet8b23eea2016-05-10 05:37:43 +0200987 size_t dfnSize = FNSPACE;
Yann Colletf8494622016-05-07 22:43:40 +0200988 unsigned u;
Yann Collet8b23eea2016-05-10 05:37:43 +0200989 char* dstFileName = (char*)malloc(FNSPACE);
Yann Collet44f684d2016-07-13 19:30:40 +0200990 if (dstFileName==NULL) EXM_THROW(73, "not enough memory for dstFileName");
Yann Collet1f1f2392016-02-12 18:33:26 +0100991 for (u=0; u<nbFiles; u++) { /* create dstFileName */
Yann Collet8b23eea2016-05-10 05:37:43 +0200992 const char* const srcFileName = srcNamesTable[u];
993 size_t const sfnSize = strlen(srcFileName);
994 const char* const suffixPtr = srcFileName + sfnSize - suffixSize;
Przemyslaw Skibinski4b504f12016-12-02 13:11:39 +0100995 const char* const gzipSuffixPtr = srcFileName + sfnSize - gzipSuffixSize;
Yann Colletaccfd802016-02-15 19:33:16 +0100996 if (dfnSize+suffixSize <= sfnSize+1) {
997 free(dstFileName);
998 dfnSize = sfnSize + 20;
999 dstFileName = (char*)malloc(dfnSize);
Yann Collet44f684d2016-07-13 19:30:40 +02001000 if (dstFileName==NULL) EXM_THROW(74, "not enough memory for dstFileName");
Yann Colletaccfd802016-02-15 19:33:16 +01001001 }
1002 if (sfnSize <= suffixSize || strcmp(suffixPtr, suffix) != 0) {
Przemyslaw Skibinski4b504f12016-12-02 13:11:39 +01001003 if (sfnSize <= gzipSuffixSize || strcmp(gzipSuffixPtr, GZ_EXTENSION) != 0) {
Przemyslaw Skibinski8489f182016-12-05 13:47:00 +01001004 DISPLAYLEVEL(1, "zstd: %s: unknown suffix (%s/%s expected) -- ignored \n", srcFileName, suffix, GZ_EXTENSION);
Przemyslaw Skibinski0e146752016-11-30 13:34:21 +01001005 skippedFiles++;
1006 continue;
1007 } else {
Przemyslaw Skibinski4b504f12016-12-02 13:11:39 +01001008 memcpy(dstFileName, srcFileName, sfnSize - gzipSuffixSize);
1009 dstFileName[sfnSize-gzipSuffixSize] = '\0';
Przemyslaw Skibinski0e146752016-11-30 13:34:21 +01001010 }
1011 } else {
1012 memcpy(dstFileName, srcFileName, sfnSize - suffixSize);
1013 dstFileName[sfnSize-suffixSize] = '\0';
Yann Collet1f1f2392016-02-12 18:33:26 +01001014 }
Yann Colletdeb078b2015-12-17 20:30:14 +01001015
Yann Colletb09b12c2016-06-09 22:59:51 +02001016 missingFiles += FIO_decompressDstFile(ress, dstFileName, srcFileName);
Yann Collet8b23eea2016-05-10 05:37:43 +02001017 }
1018 free(dstFileName);
1019 }
Yann Colletdeb078b2015-12-17 20:30:14 +01001020
1021 FIO_freeDResources(ress);
Yann Colletdeb078b2015-12-17 20:30:14 +01001022 return missingFiles + skippedFiles;
1023}
Yann Colletaccfd802016-02-15 19:33:16 +01001024
Yann Collet8b23eea2016-05-10 05:37:43 +02001025#endif /* #ifndef ZSTD_NODECOMPRESS */