blob: f805545b5ab050cb528f3040bd92ac3ca6691f4e [file] [log] [blame]
Yann Collet4856a002015-01-24 01:58:16 +01001/*
Yann Collet44886612016-02-11 04:17:50 +01002 fileio.c - File i/o handler for zstd
3 Copyright (C) Yann Collet 2013-2016
Yann Collet4856a002015-01-24 01:58:16 +01004
5 GPL v2 License
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 You can contact the author at :
Yann Collet44886612016-02-11 04:17:50 +010022 - zstd homepage : http://www.zstd.net
Yann Collet4856a002015-01-24 01:58:16 +010023*/
24/*
25 Note : this is stand-alone program.
26 It is not part of ZSTD compression library, it is a user program of ZSTD library.
27 The license of ZSTD library is BSD.
28 The license of this file is GPLv2.
29*/
30
Yann Colleteeb8ba12015-10-22 16:55:40 +010031/* *************************************
Yann Colletb1f3f4b2015-10-18 22:18:32 +010032* Tuning options
Yann Colleteeb8ba12015-10-22 16:55:40 +010033***************************************/
Yann Colletb1f3f4b2015-10-18 22:18:32 +010034#ifndef ZSTD_LEGACY_SUPPORT
Yann Collet44886612016-02-11 04:17:50 +010035/* LEGACY_SUPPORT :
Yann Colletb1f3f4b2015-10-18 22:18:32 +010036* decompressor can decode older formats (starting from Zstd 0.1+) */
37# define ZSTD_LEGACY_SUPPORT 1
Yann Collet9f432922015-11-09 17:42:17 +010038#endif
Yann Colletb1f3f4b2015-10-18 22:18:32 +010039
40
Yann Colleteeb8ba12015-10-22 16:55:40 +010041/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010042* Compiler Options
Yann Colleteeb8ba12015-10-22 16:55:40 +010043***************************************/
Yann Colletb2b53092016-07-03 00:17:39 +020044#define _POSIX_SOURCE 1 /* enable %llu on Windows */
45#define _CRT_SECURE_NO_WARNINGS /* removes Visual warning on strerror() */
Yann Collet4856a002015-01-24 01:58:16 +010046
47
Yann Collet6f3acba2016-02-12 20:19:48 +010048/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010049* Includes
Yann Colleteeb8ba12015-10-22 16:55:40 +010050***************************************/
Yann Colletc62cda92016-07-03 01:36:57 +020051#include "util.h" /* Compiler options, UTIL_GetFileSize, _LARGEFILE64_SOURCE */
Yann Collet9f432922015-11-09 17:42:17 +010052#include <stdio.h> /* fprintf, fopen, fread, _fileno, stdin, stdout */
53#include <stdlib.h> /* malloc, free */
54#include <string.h> /* strcmp, strlen */
55#include <time.h> /* clock */
56#include <errno.h> /* errno */
inikepd5ff2c32016-04-28 14:40:45 +020057
inikepd5ff2c32016-04-28 14:40:45 +020058#include "mem.h"
Yann Collet4856a002015-01-24 01:58:16 +010059#include "fileio.h"
Yann Colletd3b7f8d2016-06-04 19:47:02 +020060#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_magicNumber, ZSTD_frameHeaderSize_max */
61#include "zstd.h"
Yann Collet5347aee2016-06-04 19:12:48 +020062#define ZBUFF_STATIC_LINKING_ONLY
63#include "zbuff.h"
Yann Collet4856a002015-01-24 01:58:16 +010064
Yann Colletb1f3f4b2015-10-18 22:18:32 +010065#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
Yann Collet6f3acba2016-02-12 20:19:48 +010066# include "zstd_legacy.h" /* ZSTD_isLegacy */
67# include "fileio_legacy.h" /* FIO_decompressLegacyFrame */
Yann Colleteeb8ba12015-10-22 16:55:40 +010068#endif
Yann Colletb1f3f4b2015-10-18 22:18:32 +010069
Yann Collet4856a002015-01-24 01:58:16 +010070
Yann Collet6f3acba2016-02-12 20:19:48 +010071/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010072* OS-specific Includes
Yann Colleteeb8ba12015-10-22 16:55:40 +010073***************************************/
Yann Collet4856a002015-01-24 01:58:16 +010074#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
75# include <fcntl.h> /* _O_BINARY */
76# include <io.h> /* _setmode, _isatty */
inikep60af95d2016-05-19 10:29:49 +020077# define SET_BINARY_MODE(file) { if (_setmode(_fileno(file), _O_BINARY) == -1) perror("Cannot set _O_BINARY"); }
Yann Collet4856a002015-01-24 01:58:16 +010078#else
79# include <unistd.h> /* isatty */
80# define SET_BINARY_MODE(file)
Yann Collet4856a002015-01-24 01:58:16 +010081#endif
82
83
Yann Collet6f3acba2016-02-12 20:19:48 +010084/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010085* Constants
Yann Colleteeb8ba12015-10-22 16:55:40 +010086***************************************/
Yann Collet92d75662016-07-03 01:10:53 +020087#define KB *(1<<10)
88#define MB *(1<<20)
89#define GB *(1U<<30)
90
Yann Collet4856a002015-01-24 01:58:16 +010091#define _1BIT 0x01
92#define _2BITS 0x03
93#define _3BITS 0x07
94#define _4BITS 0x0F
95#define _6BITS 0x3F
96#define _8BITS 0xFF
97
Yann Collet88fcd292015-11-25 14:42:45 +010098#define BLOCKSIZE (128 KB)
99#define ROLLBUFFERSIZE (BLOCKSIZE*8*64)
Yann Collet4856a002015-01-24 01:58:16 +0100100
Yann Collet6f3acba2016-02-12 20:19:48 +0100101#define FIO_FRAMEHEADERSIZE 5 /* as a define, because needed to allocated table on stack */
102#define FSE_CHECKSUM_SEED 0
Yann Collet4856a002015-01-24 01:58:16 +0100103
104#define CACHELINE 64
105
Yann Collet6fca9e72016-05-31 02:40:42 +0200106#define MAX_DICT_SIZE (8 MB) /* protection against large input (attack scenario) */
Yann Collet6f3acba2016-02-12 20:19:48 +0100107
inikep3c7c3522016-04-22 13:59:05 +0200108#define FNSPACE 30
109
Yann Collet4856a002015-01-24 01:58:16 +0100110
Yann Collet459a6b72016-02-15 20:37:23 +0100111/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +0100112* Macros
Yann Colleteeb8ba12015-10-22 16:55:40 +0100113***************************************/
Yann Collet4856a002015-01-24 01:58:16 +0100114#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
115#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
116static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
Yann Collet8a1d1a62016-03-10 21:02:25 +0100117void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; }
Yann Collet4856a002015-01-24 01:58:16 +0100118
119#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
Yann Colletb71adf42016-07-02 01:05:31 +0200120 if ((clock() - g_time > refreshRate) || (g_displayLevel>=4)) \
Yann Collet4856a002015-01-24 01:58:16 +0100121 { g_time = clock(); DISPLAY(__VA_ARGS__); \
122 if (g_displayLevel>=4) fflush(stdout); } }
Yann Colletb71adf42016-07-02 01:05:31 +0200123static const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100;
Yann Collet4856a002015-01-24 01:58:16 +0100124static clock_t g_time = 0;
125
Yann Collet92d75662016-07-03 01:10:53 +0200126#define MIN(a,b) ((a) < (b) ? (a) : (b))
127
Yann Collet459a6b72016-02-15 20:37:23 +0100128
129/*-*************************************
Yann Colletb71adf42016-07-02 01:05:31 +0200130* Local Parameters - Not thread safe
Yann Colleteeb8ba12015-10-22 16:55:40 +0100131***************************************/
Yann Collet4856a002015-01-24 01:58:16 +0100132static U32 g_overwrite = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100133void FIO_overwriteMode(void) { g_overwrite=1; }
Yann Collet8a1d1a62016-03-10 21:02:25 +0100134static U32 g_maxWLog = 23;
135void FIO_setMaxWLog(unsigned maxWLog) { g_maxWLog = maxWLog; }
Yann Collet75424d12016-05-23 16:56:56 +0200136static U32 g_sparseFileSupport = 1; /* 0 : no sparse allowed; 1: auto (file yes, stdout no); 2: force sparse */
137void FIO_setSparseWrite(unsigned sparse) { g_sparseFileSupport=sparse; }
Yann Collet6381e992016-05-31 02:29:45 +0200138static U32 g_dictIDFlag = 1;
139void FIO_setDictIDFlag(unsigned dictIDFlag) { g_dictIDFlag = dictIDFlag; }
Yann Colletc98f8e72016-06-20 16:31:24 +0200140static U32 g_checksumFlag = 1;
Yann Collet87cfbe32016-06-01 19:22:15 +0200141void FIO_setChecksumFlag(unsigned checksumFlag) { g_checksumFlag = checksumFlag; }
Yann Colletb09b12c2016-06-09 22:59:51 +0200142static U32 g_removeSrcFile = 0;
143void FIO_setRemoveSrcFile(unsigned flag) { g_removeSrcFile = (flag>0); }
Yann Collet4856a002015-01-24 01:58:16 +0100144
145
Yann Collet459a6b72016-02-15 20:37:23 +0100146/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +0100147* Exceptions
Yann Colleteeb8ba12015-10-22 16:55:40 +0100148***************************************/
149#ifndef DEBUG
150# define DEBUG 0
151#endif
Yann Collet4856a002015-01-24 01:58:16 +0100152#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
153#define EXM_THROW(error, ...) \
154{ \
155 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
156 DISPLAYLEVEL(1, "Error %i : ", error); \
157 DISPLAYLEVEL(1, __VA_ARGS__); \
158 DISPLAYLEVEL(1, "\n"); \
159 exit(error); \
160}
161
162
Yann Collet459a6b72016-02-15 20:37:23 +0100163/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +0100164* Functions
Yann Colleteeb8ba12015-10-22 16:55:40 +0100165***************************************/
Yann Colletf0624362016-02-12 15:56:46 +0100166static FILE* FIO_openSrcFile(const char* srcFileName)
167{
168 FILE* f;
169
170 if (!strcmp (srcFileName, stdinmark)) {
171 DISPLAYLEVEL(4,"Using stdin for input\n");
172 f = stdin;
173 SET_BINARY_MODE(stdin);
174 } else {
175 f = fopen(srcFileName, "rb");
176 }
177
Yann Colletb71adf42016-07-02 01:05:31 +0200178 if ( f==NULL ) DISPLAYLEVEL(1, "zstd: %s: %s \n", srcFileName, strerror(errno));
Yann Colletf0624362016-02-12 15:56:46 +0100179
180 return f;
181}
182
183
184static 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" );
215 }
Yann Colletb71adf42016-07-02 01:05:31 +0200216
217 if (f==NULL) DISPLAYLEVEL(1, "zstd: %s: %s\n", dstFileName, strerror(errno));
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 */
243 DISPLAYLEVEL(2,"Dictionary %s is too large : using last %u bytes only \n", fileName, MAX_DICT_SIZE);
244 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 {
262 void* srcBuffer;
263 size_t srcBufferSize;
264 void* dstBuffer;
265 size_t dstBufferSize;
266 void* dictBuffer;
267 size_t dictBufferSize;
268 ZBUFF_CCtx* ctx;
Yann Colletf0624362016-02-12 15:56:46 +0100269 FILE* dstFile;
Yann Collet459a6b72016-02-15 20:37:23 +0100270 FILE* srcFile;
Yann Collet4f137032015-12-17 02:23:58 +0100271} cRess_t;
272
273static cRess_t FIO_createCResources(const char* dictFileName)
274{
275 cRess_t ress;
276
277 ress.ctx = ZBUFF_createCCtx();
Yann Colleted7fb842016-07-02 11:14:30 +0200278 if (ress.ctx == NULL) EXM_THROW(30, "zstd: allocation error : can't create ZBUFF context");
Yann Collet4f137032015-12-17 02:23:58 +0100279
280 /* Allocate Memory */
281 ress.srcBufferSize = ZBUFF_recommendedCInSize();
282 ress.srcBuffer = malloc(ress.srcBufferSize);
283 ress.dstBufferSize = ZBUFF_recommendedCOutSize();
284 ress.dstBuffer = malloc(ress.dstBufferSize);
Yann Colleted7fb842016-07-02 11:14:30 +0200285 if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(31, "zstd: allocation error : not enough memory");
Yann Collet4f137032015-12-17 02:23:58 +0100286
287 /* dictionary */
Yann Colletdeb078b2015-12-17 20:30:14 +0100288 ress.dictBufferSize = FIO_loadFile(&(ress.dictBuffer), dictFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100289
290 return ress;
291}
292
293static void FIO_freeCResources(cRess_t ress)
294{
295 size_t errorCode;
296 free(ress.srcBuffer);
297 free(ress.dstBuffer);
298 free(ress.dictBuffer);
299 errorCode = ZBUFF_freeCCtx(ress.ctx);
Yann Colleted7fb842016-07-02 11:14:30 +0200300 if (ZBUFF_isError(errorCode)) EXM_THROW(38, "zstd: error : can't release ZBUFF context resource : %s", ZBUFF_getErrorName(errorCode));
Yann Collet4f137032015-12-17 02:23:58 +0100301}
302
303
Yann Colletf0624362016-02-12 15:56:46 +0100304/*! FIO_compressFilename_internal() :
Yann Collet8a1d1a62016-03-10 21:02:25 +0100305 * same as FIO_compressFilename_extRess(), with `ress.desFile` already opened.
Yann Colletf0624362016-02-12 15:56:46 +0100306 * @return : 0 : compression completed correctly,
307 * 1 : missing or pb opening srcFileName
Yann Collet4f137032015-12-17 02:23:58 +0100308 */
Yann Colletf0624362016-02-12 15:56:46 +0100309static int FIO_compressFilename_internal(cRess_t ress,
310 const char* dstFileName, const char* srcFileName,
311 int cLevel)
Yann Collet4f137032015-12-17 02:23:58 +0100312{
Yann Colletf8494622016-05-07 22:43:40 +0200313 FILE* const srcFile = ress.srcFile;
314 FILE* const dstFile = ress.dstFile;
Yann Colletb44be742016-03-26 20:52:14 +0100315 U64 readsize = 0;
Yann Collet4f137032015-12-17 02:23:58 +0100316 U64 compressedfilesize = 0;
inikep69fcd7c2016-04-28 12:23:33 +0200317 U64 const fileSize = UTIL_getFileSize(srcFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100318
Yann Collet4f137032015-12-17 02:23:58 +0100319 /* init */
Yann Collet6c6e1752016-06-27 15:28:45 +0200320 { ZSTD_parameters params = ZSTD_getParams(cLevel, fileSize, ress.dictBufferSize);
Yann Colletf8494622016-05-07 22:43:40 +0200321 params.fParams.contentSizeFlag = 1;
Yann Collet87cfbe32016-06-01 19:22:15 +0200322 params.fParams.checksumFlag = g_checksumFlag;
Yann Collet6381e992016-05-31 02:29:45 +0200323 params.fParams.noDictIDFlag = !g_dictIDFlag;
Yann Collet0d311602016-06-04 00:09:02 +0200324 if ((g_maxWLog) && (params.cParams.windowLog > g_maxWLog)) {
325 params.cParams.windowLog = g_maxWLog;
326 params.cParams = ZSTD_adjustCParams(params.cParams, fileSize, ress.dictBufferSize);
327 }
Yann Collet6fca9e72016-05-31 02:40:42 +0200328 { size_t const errorCode = ZBUFF_compressInit_advanced(ress.ctx, ress.dictBuffer, ress.dictBufferSize, params, fileSize);
329 if (ZBUFF_isError(errorCode)) EXM_THROW(21, "Error initializing compression : %s", ZBUFF_getErrorName(errorCode));
330 } }
Yann Collet4f137032015-12-17 02:23:58 +0100331
332 /* Main compression loop */
Yann Collet1c8e1942016-01-26 16:31:22 +0100333 while (1) {
Yann Collet4f137032015-12-17 02:23:58 +0100334 /* Fill input Buffer */
Yann Colletb44be742016-03-26 20:52:14 +0100335 size_t const inSize = fread(ress.srcBuffer, (size_t)1, ress.srcBufferSize, srcFile);
Yann Collet4f137032015-12-17 02:23:58 +0100336 if (inSize==0) break;
Yann Colletb44be742016-03-26 20:52:14 +0100337 readsize += inSize;
338 DISPLAYUPDATE(2, "\rRead : %u MB ", (U32)(readsize>>20));
Yann Collet4f137032015-12-17 02:23:58 +0100339
Yann Collet2cac5b32016-07-13 14:15:08 +0200340 /* Compress using buffered streaming */
341 { size_t usedInSize = inSize;
Yann Collet4f137032015-12-17 02:23:58 +0100342 size_t cSize = ress.dstBufferSize;
Yann Colletf8494622016-05-07 22:43:40 +0200343 { size_t const result = ZBUFF_compressContinue(ress.ctx, ress.dstBuffer, &cSize, ress.srcBuffer, &usedInSize);
344 if (ZBUFF_isError(result)) EXM_THROW(23, "Compression error : %s ", ZBUFF_getErrorName(result)); }
Yann Collet4f137032015-12-17 02:23:58 +0100345 if (inSize != usedInSize)
346 /* inBuff should be entirely consumed since buffer sizes are recommended ones */
347 EXM_THROW(24, "Compression error : input block not fully consumed");
348
349 /* Write cBlock */
Yann Colletf8494622016-05-07 22:43:40 +0200350 { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, cSize, dstFile);
351 if (sizeCheck!=cSize) EXM_THROW(25, "Write error : cannot write compressed block into %s", dstFileName); }
Yann Collet4f137032015-12-17 02:23:58 +0100352 compressedfilesize += cSize;
353 }
Yann Colletb44be742016-03-26 20:52:14 +0100354 DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", (U32)(readsize>>20), (double)compressedfilesize/readsize*100);
Yann Collet4f137032015-12-17 02:23:58 +0100355 }
356
357 /* End of Frame */
Yann Colletb44be742016-03-26 20:52:14 +0100358 { size_t cSize = ress.dstBufferSize;
359 size_t const result = ZBUFF_compressEnd(ress.ctx, ress.dstBuffer, &cSize);
Yann Collet4f137032015-12-17 02:23:58 +0100360 if (result!=0) EXM_THROW(26, "Compression error : cannot create frame end");
361
Yann Colletf8494622016-05-07 22:43:40 +0200362 { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, cSize, dstFile);
363 if (sizeCheck!=cSize) EXM_THROW(27, "Write error : cannot write frame end into %s", dstFileName); }
Yann Collet4f137032015-12-17 02:23:58 +0100364 compressedfilesize += cSize;
365 }
366
367 /* Status */
Yann Collet2cac5b32016-07-13 14:15:08 +0200368 { size_t const len = strlen(srcFileName);
369 if (len > 20) srcFileName += len-20; }
Yann Collet4f137032015-12-17 02:23:58 +0100370 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Collet2cac5b32016-07-13 14:15:08 +0200371 DISPLAYLEVEL(2,"%-20.20s :%6.2f%% (%6llu => %6llu bytes, %s) \n", srcFileName,
Yann Colletb09b12c2016-06-09 22:59:51 +0200372 (double)compressedfilesize/readsize*100, (unsigned long long)readsize, (unsigned long long) compressedfilesize,
373 dstFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100374
Yann Collet4f137032015-12-17 02:23:58 +0100375 return 0;
376}
377
378
Yann Colletb71adf42016-07-02 01:05:31 +0200379/*! FIO_compressFilename_srcFile() :
380 * note : ress.destFile already opened
Yann Collet459a6b72016-02-15 20:37:23 +0100381 * @return : 0 : compression completed correctly,
382 * 1 : missing or pb opening srcFileName
383 */
384static int FIO_compressFilename_srcFile(cRess_t ress,
385 const char* dstFileName, const char* srcFileName,
386 int cLevel)
387{
388 int result;
389
390 /* File check */
Yann Colletb09b12c2016-06-09 22:59:51 +0200391 if (UTIL_isDirectory(srcFileName)) {
392 DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
393 return 1;
394 }
Yann Collet459a6b72016-02-15 20:37:23 +0100395 ress.srcFile = FIO_openSrcFile(srcFileName);
396 if (!ress.srcFile) return 1; /* srcFile could not be opened */
397
398 result = FIO_compressFilename_internal(ress, dstFileName, srcFileName, cLevel);
399
Yann Collet459a6b72016-02-15 20:37:23 +0100400 fclose(ress.srcFile);
Yann Colletb09b12c2016-06-09 22:59:51 +0200401 if ((g_removeSrcFile) && (!result)) remove(srcFileName);
Yann Collet459a6b72016-02-15 20:37:23 +0100402 return result;
403}
404
405
Yann Colletb09b12c2016-06-09 22:59:51 +0200406/*! FIO_compressFilename_dstFile() :
Yann Colletf0624362016-02-12 15:56:46 +0100407 * @return : 0 : compression completed correctly,
Yann Colletb09b12c2016-06-09 22:59:51 +0200408 * 1 : pb
Yann Colletf0624362016-02-12 15:56:46 +0100409 */
Yann Colletb09b12c2016-06-09 22:59:51 +0200410static int FIO_compressFilename_dstFile(cRess_t ress,
Yann Colletf0624362016-02-12 15:56:46 +0100411 const char* dstFileName, const char* srcFileName,
412 int cLevel)
413{
414 int result;
415
416 ress.dstFile = FIO_openDstFile(dstFileName);
Yann Colletb71adf42016-07-02 01:05:31 +0200417 if (ress.dstFile==0) return 1;
Yann Colletf0624362016-02-12 15:56:46 +0100418
Yann Colletb09b12c2016-06-09 22:59:51 +0200419 result = FIO_compressFilename_srcFile(ress, dstFileName, srcFileName, cLevel);
Chip Turner6de382c2016-03-13 22:24:46 -0700420
Yann Colletb71adf42016-07-02 01:05:31 +0200421 if (fclose(ress.dstFile)) { DISPLAYLEVEL(1, "zstd: %s: %s \n", dstFileName, strerror(errno)); result=1; }
Yann Colletb09b12c2016-06-09 22:59:51 +0200422 if (result!=0) remove(dstFileName); /* remove operation artefact */
Yann Colletf0624362016-02-12 15:56:46 +0100423 return result;
424}
425
426
Yann Collet9d909222015-12-17 14:09:55 +0100427int FIO_compressFilename(const char* dstFileName, const char* srcFileName,
428 const char* dictFileName, int compressionLevel)
Yann Collet4856a002015-01-24 01:58:16 +0100429{
Yann Collet6381e992016-05-31 02:29:45 +0200430 clock_t const start = clock();
Yann Colletb09b12c2016-06-09 22:59:51 +0200431
Yann Collet6381e992016-05-31 02:29:45 +0200432 cRess_t const ress = FIO_createCResources(dictFileName);
Yann Colletb71adf42016-07-02 01:05:31 +0200433 int const result = FIO_compressFilename_dstFile(ress, dstFileName, srcFileName, compressionLevel);
Yann Collet9d909222015-12-17 14:09:55 +0100434
Yann Colletb71adf42016-07-02 01:05:31 +0200435 double const seconds = (double)(clock() - start) / CLOCKS_PER_SEC;
436 DISPLAYLEVEL(4, "Completed in %.2f sec \n", seconds);
437
438 FIO_freeCResources(ress);
439 return result;
Yann Collet4856a002015-01-24 01:58:16 +0100440}
441
442
Yann Collet9d909222015-12-17 14:09:55 +0100443int FIO_compressMultipleFilenames(const char** inFileNamesTable, unsigned nbFiles,
Yann Collet4f137032015-12-17 02:23:58 +0100444 const char* suffix,
445 const char* dictFileName, int compressionLevel)
446{
Yann Collet4f137032015-12-17 02:23:58 +0100447 int missed_files = 0;
Yann Colletf8494622016-05-07 22:43:40 +0200448 char* dstFileName = (char*)malloc(FNSPACE);
Yann Collet4f137032015-12-17 02:23:58 +0100449 size_t dfnSize = FNSPACE;
Yann Colletf8494622016-05-07 22:43:40 +0200450 size_t const suffixSize = suffix ? strlen(suffix) : 0;
Yann Collet4f137032015-12-17 02:23:58 +0100451 cRess_t ress;
452
453 /* init */
454 ress = FIO_createCResources(dictFileName);
455
456 /* loop on each file */
Yann Collet459a6b72016-02-15 20:37:23 +0100457 if (!strcmp(suffix, stdoutmark)) {
Yann Colletf8494622016-05-07 22:43:40 +0200458 unsigned u;
Yann Collet459a6b72016-02-15 20:37:23 +0100459 ress.dstFile = stdout;
inikep60af95d2016-05-19 10:29:49 +0200460 SET_BINARY_MODE(stdout);
Yann Collet459a6b72016-02-15 20:37:23 +0100461 for (u=0; u<nbFiles; u++)
462 missed_files += FIO_compressFilename_srcFile(ress, stdoutmark,
Yann Collet8a1d1a62016-03-10 21:02:25 +0100463 inFileNamesTable[u], compressionLevel);
Yann Collet459a6b72016-02-15 20:37:23 +0100464 if (fclose(ress.dstFile)) EXM_THROW(29, "Write error : cannot properly close %s", stdoutmark);
465 } else {
Yann Colletf8494622016-05-07 22:43:40 +0200466 unsigned u;
Yann Colletf0624362016-02-12 15:56:46 +0100467 for (u=0; u<nbFiles; u++) {
468 size_t ifnSize = strlen(inFileNamesTable[u]);
469 if (dfnSize <= ifnSize+suffixSize+1) { free(dstFileName); dfnSize = ifnSize + 20; dstFileName = (char*)malloc(dfnSize); }
470 strcpy(dstFileName, inFileNamesTable[u]);
471 strcat(dstFileName, suffix);
Yann Colletb09b12c2016-06-09 22:59:51 +0200472 missed_files += FIO_compressFilename_dstFile(ress, dstFileName,
Yann Colletf0624362016-02-12 15:56:46 +0100473 inFileNamesTable[u], compressionLevel);
Yann Collet459a6b72016-02-15 20:37:23 +0100474 } }
Yann Collet4f137032015-12-17 02:23:58 +0100475
476 /* Close & Free */
477 FIO_freeCResources(ress);
478 free(dstFileName);
479
480 return missed_files;
481}
482
Yann Colletf8494622016-05-07 22:43:40 +0200483#endif /* #ifndef ZSTD_NOCOMPRESS */
inikep3c7c3522016-04-22 13:59:05 +0200484
Yann Collet4f137032015-12-17 02:23:58 +0100485
inikepdb396432016-04-22 18:22:30 +0200486
487#ifndef ZSTD_NODECOMPRESS
488
Yann Collet4f137032015-12-17 02:23:58 +0100489/* **************************************************************************
490* Decompression
491****************************************************************************/
Yann Colletdeb078b2015-12-17 20:30:14 +0100492typedef struct {
493 void* srcBuffer;
494 size_t srcBufferSize;
495 void* dstBuffer;
496 size_t dstBufferSize;
497 void* dictBuffer;
498 size_t dictBufferSize;
499 ZBUFF_DCtx* dctx;
Yann Collet1f1f2392016-02-12 18:33:26 +0100500 FILE* dstFile;
Yann Colletdeb078b2015-12-17 20:30:14 +0100501} dRess_t;
502
503static dRess_t FIO_createDResources(const char* dictFileName)
504{
505 dRess_t ress;
506
507 /* init */
508 ress.dctx = ZBUFF_createDCtx();
509 if (ress.dctx==NULL) EXM_THROW(60, "Can't create ZBUFF decompression context");
510
511 /* Allocate Memory */
512 ress.srcBufferSize = ZBUFF_recommendedDInSize();
513 ress.srcBuffer = malloc(ress.srcBufferSize);
514 ress.dstBufferSize = ZBUFF_recommendedDOutSize();
515 ress.dstBuffer = malloc(ress.dstBufferSize);
516 if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(61, "Allocation error : not enough memory");
517
518 /* dictionary */
519 ress.dictBufferSize = FIO_loadFile(&(ress.dictBuffer), dictFileName);
520
521 return ress;
522}
523
524static void FIO_freeDResources(dRess_t ress)
525{
Yann Colletf8494622016-05-07 22:43:40 +0200526 size_t const errorCode = ZBUFF_freeDCtx(ress.dctx);
Yann Colletdeb078b2015-12-17 20:30:14 +0100527 if (ZBUFF_isError(errorCode)) EXM_THROW(69, "Error : can't free ZBUFF context resource : %s", ZBUFF_getErrorName(errorCode));
528 free(ress.srcBuffer);
529 free(ress.dstBuffer);
530 free(ress.dictBuffer);
531}
Yann Collet4f137032015-12-17 02:23:58 +0100532
533
Yann Collet75424d12016-05-23 16:56:56 +0200534/** FIO_fwriteSparse() :
535* @return : storedSkips, to be provided to next call to FIO_fwriteSparse() of LZ4IO_fwriteSparseEnd() */
536static unsigned FIO_fwriteSparse(FILE* file, const void* buffer, size_t bufferSize, unsigned storedSkips)
537{
538 const size_t* const bufferT = (const size_t*)buffer; /* Buffer is supposed malloc'ed, hence aligned on size_t */
539 size_t bufferSizeT = bufferSize / sizeof(size_t);
540 const size_t* const bufferTEnd = bufferT + bufferSizeT;
541 const size_t* ptrT = bufferT;
542 static const size_t segmentSizeT = (32 KB) / sizeof(size_t); /* 0-test re-attempted every 32 KB */
543
544 if (!g_sparseFileSupport) { /* normal write */
545 size_t const sizeCheck = fwrite(buffer, 1, bufferSize, file);
546 if (sizeCheck != bufferSize) EXM_THROW(70, "Write error : cannot write decoded block");
547 return 0;
548 }
549
550 /* avoid int overflow */
551 if (storedSkips > 1 GB) {
552 int const seekResult = fseek(file, 1 GB, SEEK_CUR);
553 if (seekResult != 0) EXM_THROW(71, "1 GB skip error (sparse file support)");
554 storedSkips -= 1 GB;
555 }
556
557 while (ptrT < bufferTEnd) {
558 size_t seg0SizeT = segmentSizeT;
559 size_t nb0T;
560
561 /* count leading zeros */
562 if (seg0SizeT > bufferSizeT) seg0SizeT = bufferSizeT;
563 bufferSizeT -= seg0SizeT;
564 for (nb0T=0; (nb0T < seg0SizeT) && (ptrT[nb0T] == 0); nb0T++) ;
565 storedSkips += (unsigned)(nb0T * sizeof(size_t));
566
567 if (nb0T != seg0SizeT) { /* not all 0s */
568 int const seekResult = fseek(file, storedSkips, SEEK_CUR);
569 if (seekResult) EXM_THROW(72, "Sparse skip error ; try --no-sparse");
570 storedSkips = 0;
571 seg0SizeT -= nb0T;
572 ptrT += nb0T;
573 { size_t const sizeCheck = fwrite(ptrT, sizeof(size_t), seg0SizeT, file);
574 if (sizeCheck != seg0SizeT) EXM_THROW(73, "Write error : cannot write decoded block");
575 } }
576 ptrT += seg0SizeT;
577 }
578
579 { static size_t const maskT = sizeof(size_t)-1;
580 if (bufferSize & maskT) { /* size not multiple of sizeof(size_t) : implies end of block */
581 const char* const restStart = (const char*)bufferTEnd;
582 const char* restPtr = restStart;
583 size_t restSize = bufferSize & maskT;
584 const char* const restEnd = restStart + restSize;
585 for ( ; (restPtr < restEnd) && (*restPtr == 0); restPtr++) ;
586 storedSkips += (unsigned) (restPtr - restStart);
587 if (restPtr != restEnd) {
588 int seekResult = fseek(file, storedSkips, SEEK_CUR);
589 if (seekResult) EXM_THROW(74, "Sparse skip error ; try --no-sparse");
590 storedSkips = 0;
591 { size_t const sizeCheck = fwrite(restPtr, 1, restEnd - restPtr, file);
592 if (sizeCheck != (size_t)(restEnd - restPtr)) EXM_THROW(75, "Write error : cannot write decoded end of block");
593 } } } }
594
595 return storedSkips;
596}
597
598static void FIO_fwriteSparseEnd(FILE* file, unsigned storedSkips)
599{
600 if (storedSkips-->0) { /* implies g_sparseFileSupport>0 */
601 int const seekResult = fseek(file, storedSkips, SEEK_CUR);
602 if (seekResult != 0) EXM_THROW(69, "Final skip error (sparse file)\n");
603 { const char lastZeroByte[1] = { 0 };
604 size_t const sizeCheck = fwrite(lastZeroByte, 1, 1, file);
605 if (sizeCheck != 1) EXM_THROW(69, "Write error : cannot write last zero\n");
606 } }
607}
608
Yann Collet09b21ee2016-03-15 12:56:03 +0100609/** FIO_decompressFrame() :
610 @return : size of decoded frame
611*/
Yann Colletdeb078b2015-12-17 20:30:14 +0100612unsigned long long FIO_decompressFrame(dRess_t ress,
613 FILE* foutput, FILE* finput, size_t alreadyLoaded)
Yann Collet4856a002015-01-24 01:58:16 +0100614{
Yann Collet88fcd292015-11-25 14:42:45 +0100615 U64 frameSize = 0;
Yann Collet09b21ee2016-03-15 12:56:03 +0100616 size_t readSize;
Yann Collet75424d12016-05-23 16:56:56 +0200617 U32 storedSkips = 0;
Yann Collet09b21ee2016-03-15 12:56:03 +0100618
619 ZBUFF_decompressInitDictionary(ress.dctx, ress.dictBuffer, ress.dictBufferSize);
620
Yann Colletbd39d542016-05-10 14:14:19 +0200621 /* Header loading (optional, saves one loop) */
Yann Collet673f0d72016-06-06 00:26:38 +0200622 { size_t const toLoad = 9 - alreadyLoaded; /* assumption : 9 >= alreadyLoaded */
Yann Colletbd39d542016-05-10 14:14:19 +0200623 size_t const loadedSize = fread(((char*)ress.srcBuffer) + alreadyLoaded, 1, toLoad, finput);
Yann Colletd6931172016-05-10 05:56:09 +0200624 readSize = alreadyLoaded + loadedSize;
Yann Collet09b21ee2016-03-15 12:56:03 +0100625 }
Yann Collet4856a002015-01-24 01:58:16 +0100626
Yann Collet4856a002015-01-24 01:58:16 +0100627 /* Main decompression Loop */
Yann Collet2ce49232016-02-02 14:36:49 +0100628 while (1) {
Yann Colletdeb078b2015-12-17 20:30:14 +0100629 size_t inSize=readSize, decodedSize=ress.dstBufferSize;
Yann Collet8b23eea2016-05-10 05:37:43 +0200630 size_t const toRead = ZBUFF_decompressContinue(ress.dctx, ress.dstBuffer, &decodedSize, ress.srcBuffer, &inSize);
Yann Collet88fcd292015-11-25 14:42:45 +0100631 if (ZBUFF_isError(toRead)) EXM_THROW(36, "Decoding error : %s", ZBUFF_getErrorName(toRead));
Yann Collet88fcd292015-11-25 14:42:45 +0100632 readSize -= inSize;
Yann Collet88fcd292015-11-25 14:42:45 +0100633
634 /* Write block */
Yann Collet75424d12016-05-23 16:56:56 +0200635 storedSkips = FIO_fwriteSparse(foutput, ress.dstBuffer, decodedSize, storedSkips);
Yann Collet88fcd292015-11-25 14:42:45 +0100636 frameSize += decodedSize;
637 DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(frameSize>>20) );
638
Yann Collet09b21ee2016-03-15 12:56:03 +0100639 if (toRead == 0) break; /* end of frame */
Yann Colletdeb078b2015-12-17 20:30:14 +0100640 if (readSize) EXM_THROW(38, "Decoding error : should consume entire input");
Yann Collet4856a002015-01-24 01:58:16 +0100641
642 /* Fill input buffer */
Yann Colletdeb078b2015-12-17 20:30:14 +0100643 if (toRead > ress.srcBufferSize) EXM_THROW(34, "too large block");
644 readSize = fread(ress.srcBuffer, 1, toRead, finput);
Yann Collet09b21ee2016-03-15 12:56:03 +0100645 if (readSize != toRead)
646 EXM_THROW(35, "Read error");
Yann Collet4856a002015-01-24 01:58:16 +0100647 }
648
Yann Collet75424d12016-05-23 16:56:56 +0200649 FIO_fwriteSparseEnd(foutput, storedSkips);
650
Yann Collet88fcd292015-11-25 14:42:45 +0100651 return frameSize;
Yann Colletbe50aaa2015-09-10 23:26:09 +0100652}
653
654
Yann Colletde95f962016-05-23 19:46:47 +0200655/** FIO_passThrough() : just copy input into output, for compatibility with gzip -df mode
656 @return : 0 (no error) */
Yann Colletddbb8e22016-05-24 00:52:14 +0200657static unsigned FIO_passThrough(FILE* foutput, FILE* finput, void* buffer, size_t bufferSize)
Yann Colletde95f962016-05-23 19:46:47 +0200658{
659 size_t const blockSize = MIN (64 KB, bufferSize);
Yann Colletddbb8e22016-05-24 00:52:14 +0200660 size_t readFromInput = 1;
Yann Colletde95f962016-05-23 19:46:47 +0200661 unsigned storedSkips = 0;
662
663 /* assumption : first 4 bytes already loaded (magic number detection), and stored within buffer */
664 { size_t const sizeCheck = fwrite(buffer, 1, 4, foutput);
665 if (sizeCheck != 4) EXM_THROW(50, "Pass-through write error"); }
666
Yann Colletddbb8e22016-05-24 00:52:14 +0200667 while (readFromInput) {
668 readFromInput = fread(buffer, 1, blockSize, finput);
669 storedSkips = FIO_fwriteSparse(foutput, buffer, readFromInput, storedSkips);
Yann Colletde95f962016-05-23 19:46:47 +0200670 }
671
672 FIO_fwriteSparseEnd(foutput, storedSkips);
673 return 0;
674}
675
676
Yann Collet1f1f2392016-02-12 18:33:26 +0100677/** FIO_decompressSrcFile() :
678 Decompression `srcFileName` into `ress.dstFile`
679 @return : 0 : OK
680 1 : operation not started
681*/
682static int FIO_decompressSrcFile(dRess_t ress, const char* srcFileName)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100683{
Yann Colletdeb078b2015-12-17 20:30:14 +0100684 unsigned long long filesize = 0;
Yann Colletf8494622016-05-07 22:43:40 +0200685 FILE* const dstFile = ress.dstFile;
Yann Colletb09b12c2016-06-09 22:59:51 +0200686 FILE* srcFile;
687
688 if (UTIL_isDirectory(srcFileName)) {
689 DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
690 return 1;
691 }
692 srcFile = FIO_openSrcFile(srcFileName);
Yann Collet1f1f2392016-02-12 18:33:26 +0100693 if (srcFile==0) return 1;
Yann Collet88fcd292015-11-25 14:42:45 +0100694
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100695 /* for each frame */
Yann Collet2ce49232016-02-02 14:36:49 +0100696 for ( ; ; ) {
Yann Colleta85a8dd2015-11-30 11:53:11 +0100697 /* check magic number -> version */
Yann Collet9e8b09a2016-04-07 19:35:23 +0200698 size_t const toRead = 4;
699 size_t const sizeCheck = fread(ress.srcBuffer, (size_t)1, toRead, srcFile);
Yann Colleta85a8dd2015-11-30 11:53:11 +0100700 if (sizeCheck==0) break; /* no more input */
Yann Collet1f1f2392016-02-12 18:33:26 +0100701 if (sizeCheck != toRead) EXM_THROW(31, "zstd: %s read error : cannot read header", srcFileName);
Yann Colletf8494622016-05-07 22:43:40 +0200702 { U32 const magic = MEM_readLE32(ress.srcBuffer);
703#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
Yann Collet19c27d22016-07-07 14:40:13 +0200704 if (ZSTD_isLegacy(ress.srcBuffer, 4)) {
Yann Collet95af06f2016-05-08 08:23:51 +0200705 filesize += FIO_decompressLegacyFrame(dstFile, srcFile, ress.dictBuffer, ress.dictBufferSize, magic);
Yann Colletf8494622016-05-07 22:43:40 +0200706 continue;
707 }
Yann Collet8b23eea2016-05-10 05:37:43 +0200708#endif
inikepf772bf52016-05-31 12:43:46 +0200709 if (((magic & 0xFFFFFFF0U) != ZSTD_MAGIC_SKIPPABLE_START) && (magic != ZSTD_MAGICNUMBER)) {
Yann Colletde95f962016-05-23 19:46:47 +0200710 if (g_overwrite) /* -df : pass-through mode */
711 return FIO_passThrough(dstFile, srcFile, ress.srcBuffer, ress.srcBufferSize);
712 else {
713 DISPLAYLEVEL(1, "zstd: %s: not in zstd format \n", srcFileName);
714 return 1;
715 } } }
Yann Colletdeb078b2015-12-17 20:30:14 +0100716 filesize += FIO_decompressFrame(ress, dstFile, srcFile, toRead);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100717 }
718
Yann Colletdeb078b2015-12-17 20:30:14 +0100719 /* Final Status */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100720 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Collet6381e992016-05-31 02:29:45 +0200721 DISPLAYLEVEL(2, "%-20.20s: %llu bytes \n", srcFileName, filesize);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100722
Yann Colletdeb078b2015-12-17 20:30:14 +0100723 /* Close */
724 fclose(srcFile);
Yann Colletb09b12c2016-06-09 22:59:51 +0200725 if (g_removeSrcFile) remove(srcFileName);
Yann Collet1f1f2392016-02-12 18:33:26 +0100726 return 0;
727}
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100728
Yann Collet1f1f2392016-02-12 18:33:26 +0100729
730/** FIO_decompressFile_extRess() :
731 decompress `srcFileName` into `dstFileName`
732 @return : 0 : OK
733 1 : operation aborted (src not available, dst already taken, etc.)
734*/
Yann Colletb09b12c2016-06-09 22:59:51 +0200735static int FIO_decompressDstFile(dRess_t ress,
Yann Collet1f1f2392016-02-12 18:33:26 +0100736 const char* dstFileName, const char* srcFileName)
737{
Chip Turner6de382c2016-03-13 22:24:46 -0700738 int result;
Yann Collet1f1f2392016-02-12 18:33:26 +0100739 ress.dstFile = FIO_openDstFile(dstFileName);
740 if (ress.dstFile==0) return 1;
741
Chip Turner6de382c2016-03-13 22:24:46 -0700742 result = FIO_decompressSrcFile(ress, srcFileName);
Yann Collet1f1f2392016-02-12 18:33:26 +0100743
744 if (fclose(ress.dstFile)) EXM_THROW(38, "Write error : cannot properly close %s", dstFileName);
Yann Colletb09b12c2016-06-09 22:59:51 +0200745 if (result != 0) remove(dstFileName);
Chip Turner6de382c2016-03-13 22:24:46 -0700746 return result;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100747}
748
749
Yann Colletdeb078b2015-12-17 20:30:14 +0100750int FIO_decompressFilename(const char* dstFileName, const char* srcFileName,
751 const char* dictFileName)
752{
753 int missingFiles = 0;
754 dRess_t ress = FIO_createDResources(dictFileName);
755
Yann Colletb09b12c2016-06-09 22:59:51 +0200756 missingFiles += FIO_decompressDstFile(ress, dstFileName, srcFileName);
Yann Colletdeb078b2015-12-17 20:30:14 +0100757
758 FIO_freeDResources(ress);
759 return missingFiles;
760}
761
762
763#define MAXSUFFIXSIZE 8
764int FIO_decompressMultipleFilenames(const char** srcNamesTable, unsigned nbFiles,
765 const char* suffix,
766 const char* dictFileName)
767{
Yann Colletdeb078b2015-12-17 20:30:14 +0100768 int skippedFiles = 0;
769 int missingFiles = 0;
Yann Collet8b23eea2016-05-10 05:37:43 +0200770 dRess_t ress = FIO_createDResources(dictFileName);
Yann Colletdeb078b2015-12-17 20:30:14 +0100771
Yann Colletaccfd802016-02-15 19:33:16 +0100772 if (!strcmp(suffix, stdoutmark) || !strcmp(suffix, nulmark)) {
Yann Colletf8494622016-05-07 22:43:40 +0200773 unsigned u;
Yann Colletaccfd802016-02-15 19:33:16 +0100774 ress.dstFile = FIO_openDstFile(suffix);
775 if (ress.dstFile == 0) EXM_THROW(71, "cannot open %s", suffix);
776 for (u=0; u<nbFiles; u++)
777 missingFiles += FIO_decompressSrcFile(ress, srcNamesTable[u]);
778 if (fclose(ress.dstFile)) EXM_THROW(39, "Write error : cannot properly close %s", stdoutmark);
779 } else {
Yann Collet8b23eea2016-05-10 05:37:43 +0200780 size_t const suffixSize = suffix ? strlen(suffix) : 0;
781 size_t dfnSize = FNSPACE;
Yann Colletf8494622016-05-07 22:43:40 +0200782 unsigned u;
Yann Collet8b23eea2016-05-10 05:37:43 +0200783 char* dstFileName = (char*)malloc(FNSPACE);
784 if (dstFileName==NULL) EXM_THROW(70, "not enough memory for dstFileName");
Yann Collet1f1f2392016-02-12 18:33:26 +0100785 for (u=0; u<nbFiles; u++) { /* create dstFileName */
Yann Collet8b23eea2016-05-10 05:37:43 +0200786 const char* const srcFileName = srcNamesTable[u];
787 size_t const sfnSize = strlen(srcFileName);
788 const char* const suffixPtr = srcFileName + sfnSize - suffixSize;
Yann Colletaccfd802016-02-15 19:33:16 +0100789 if (dfnSize+suffixSize <= sfnSize+1) {
790 free(dstFileName);
791 dfnSize = sfnSize + 20;
792 dstFileName = (char*)malloc(dfnSize);
793 if (dstFileName==NULL) EXM_THROW(71, "not enough memory for dstFileName");
794 }
795 if (sfnSize <= suffixSize || strcmp(suffixPtr, suffix) != 0) {
Yann Collet1f1f2392016-02-12 18:33:26 +0100796 DISPLAYLEVEL(1, "zstd: %s: unknown suffix (%4s expected) -- ignored \n", srcFileName, suffix);
797 skippedFiles++;
798 continue;
799 }
800 memcpy(dstFileName, srcFileName, sfnSize - suffixSize);
801 dstFileName[sfnSize-suffixSize] = '\0';
Yann Colletdeb078b2015-12-17 20:30:14 +0100802
Yann Colletb09b12c2016-06-09 22:59:51 +0200803 missingFiles += FIO_decompressDstFile(ress, dstFileName, srcFileName);
Yann Collet8b23eea2016-05-10 05:37:43 +0200804 }
805 free(dstFileName);
806 }
Yann Colletdeb078b2015-12-17 20:30:14 +0100807
808 FIO_freeDResources(ress);
Yann Colletdeb078b2015-12-17 20:30:14 +0100809 return missingFiles + skippedFiles;
810}
Yann Colletaccfd802016-02-15 19:33:16 +0100811
Yann Collet8b23eea2016-05-10 05:37:43 +0200812#endif /* #ifndef ZSTD_NODECOMPRESS */