blob: b333ded2465a9be4f40c3202b0aec766985f1c40 [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***************************************/
inikep0bd0fae2016-05-05 13:10:57 +020044#define _POSIX_SOURCE 1 /* enable %llu on Windows */
Yann Collet4856a002015-01-24 01:58:16 +010045
46
Yann Collet6f3acba2016-02-12 20:19:48 +010047/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010048* Includes
Yann Colleteeb8ba12015-10-22 16:55:40 +010049***************************************/
inikep13c84242016-05-05 13:58:56 +020050#include "util.h" /* Compiler options, UTIL_GetFileSize */
Yann Collet9f432922015-11-09 17:42:17 +010051#include <stdio.h> /* fprintf, fopen, fread, _fileno, stdin, stdout */
52#include <stdlib.h> /* malloc, free */
53#include <string.h> /* strcmp, strlen */
54#include <time.h> /* clock */
55#include <errno.h> /* errno */
inikepd5ff2c32016-04-28 14:40:45 +020056
inikepd5ff2c32016-04-28 14:40:45 +020057#include "mem.h"
Yann Collet4856a002015-01-24 01:58:16 +010058#include "fileio.h"
Yann Collet09b21ee2016-03-15 12:56:03 +010059#include "zstd_static.h" /* ZSTD_magicNumber, ZSTD_frameHeaderSize_max */
inikepe75909e2016-05-25 11:31:16 +020060#include "zstd_internal.h" /* MIN, KB, MB */
Yann Collet62ae5fb2016-02-12 18:59:11 +010061#include "zbuff_static.h"
Yann Collet4856a002015-01-24 01:58:16 +010062
Yann Colletb1f3f4b2015-10-18 22:18:32 +010063#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
Yann Collet6f3acba2016-02-12 20:19:48 +010064# include "zstd_legacy.h" /* ZSTD_isLegacy */
65# include "fileio_legacy.h" /* FIO_decompressLegacyFrame */
Yann Colleteeb8ba12015-10-22 16:55:40 +010066#endif
Yann Colletb1f3f4b2015-10-18 22:18:32 +010067
Yann Collet4856a002015-01-24 01:58:16 +010068
Yann Collet6f3acba2016-02-12 20:19:48 +010069/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010070* OS-specific Includes
Yann Colleteeb8ba12015-10-22 16:55:40 +010071***************************************/
Yann Collet4856a002015-01-24 01:58:16 +010072#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
73# include <fcntl.h> /* _O_BINARY */
74# include <io.h> /* _setmode, _isatty */
inikep60af95d2016-05-19 10:29:49 +020075# define SET_BINARY_MODE(file) { if (_setmode(_fileno(file), _O_BINARY) == -1) perror("Cannot set _O_BINARY"); }
Yann Collet4856a002015-01-24 01:58:16 +010076#else
77# include <unistd.h> /* isatty */
78# define SET_BINARY_MODE(file)
Yann Collet4856a002015-01-24 01:58:16 +010079#endif
80
81
Yann Collet6f3acba2016-02-12 20:19:48 +010082/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010083* Constants
Yann Colleteeb8ba12015-10-22 16:55:40 +010084***************************************/
Yann Collet4856a002015-01-24 01:58:16 +010085#define _1BIT 0x01
86#define _2BITS 0x03
87#define _3BITS 0x07
88#define _4BITS 0x0F
89#define _6BITS 0x3F
90#define _8BITS 0xFF
91
Yann Collet88fcd292015-11-25 14:42:45 +010092#define BLOCKSIZE (128 KB)
93#define ROLLBUFFERSIZE (BLOCKSIZE*8*64)
Yann Collet4856a002015-01-24 01:58:16 +010094
Yann Collet6f3acba2016-02-12 20:19:48 +010095#define FIO_FRAMEHEADERSIZE 5 /* as a define, because needed to allocated table on stack */
96#define FSE_CHECKSUM_SEED 0
Yann Collet4856a002015-01-24 01:58:16 +010097
98#define CACHELINE 64
99
Yann Collet6f3acba2016-02-12 20:19:48 +0100100#define MAX_DICT_SIZE (1 MB) /* protection against large input (attack scenario) ; can be changed */
101
inikep3c7c3522016-04-22 13:59:05 +0200102#define FNSPACE 30
103
Yann Collet4856a002015-01-24 01:58:16 +0100104
Yann Collet459a6b72016-02-15 20:37:23 +0100105/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +0100106* Macros
Yann Colleteeb8ba12015-10-22 16:55:40 +0100107***************************************/
Yann Collet4856a002015-01-24 01:58:16 +0100108#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
109#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
110static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
Yann Collet8a1d1a62016-03-10 21:02:25 +0100111void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; }
Yann Collet4856a002015-01-24 01:58:16 +0100112
113#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
114 if ((FIO_GetMilliSpan(g_time) > refreshRate) || (g_displayLevel>=4)) \
115 { g_time = clock(); DISPLAY(__VA_ARGS__); \
116 if (g_displayLevel>=4) fflush(stdout); } }
117static const unsigned refreshRate = 150;
118static clock_t g_time = 0;
119
Yann Colletf6ca09b2016-05-09 04:44:45 +0200120static unsigned FIO_GetMilliSpan(clock_t nPrevious)
121{
122 clock_t const nCurrent = clock();
123 return (unsigned)(((nCurrent - nPrevious) * 1000) / CLOCKS_PER_SEC);
124}
Yann Collet4856a002015-01-24 01:58:16 +0100125
Yann Collet459a6b72016-02-15 20:37:23 +0100126
127/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +0100128* Local Parameters
Yann Colleteeb8ba12015-10-22 16:55:40 +0100129***************************************/
Yann Collet4856a002015-01-24 01:58:16 +0100130static U32 g_overwrite = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100131void FIO_overwriteMode(void) { g_overwrite=1; }
Yann Collet8a1d1a62016-03-10 21:02:25 +0100132static U32 g_maxWLog = 23;
133void FIO_setMaxWLog(unsigned maxWLog) { g_maxWLog = maxWLog; }
Yann Collet75424d12016-05-23 16:56:56 +0200134static U32 g_sparseFileSupport = 1; /* 0 : no sparse allowed; 1: auto (file yes, stdout no); 2: force sparse */
135void FIO_setSparseWrite(unsigned sparse) { g_sparseFileSupport=sparse; }
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__); \
150 DISPLAYLEVEL(1, "\n"); \
151 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 Colletf0624362016-02-12 15:56:46 +0100158static FILE* FIO_openSrcFile(const char* srcFileName)
159{
160 FILE* f;
161
162 if (!strcmp (srcFileName, stdinmark)) {
163 DISPLAYLEVEL(4,"Using stdin for input\n");
164 f = stdin;
165 SET_BINARY_MODE(stdin);
166 } else {
167 f = fopen(srcFileName, "rb");
168 }
169
170 if ( f==NULL ) DISPLAYLEVEL(1, "zstd: %s: No such file\n", srcFileName);
171
172 return f;
173}
174
175
176static FILE* FIO_openDstFile(const char* dstFileName)
177{
178 FILE* f;
179
180 if (!strcmp (dstFileName, stdoutmark)) {
181 DISPLAYLEVEL(4,"Using stdout for output\n");
182 f = stdout;
183 SET_BINARY_MODE(stdout);
Yann Collet75424d12016-05-23 16:56:56 +0200184 if (g_sparseFileSupport==1) {
185 g_sparseFileSupport = 0;
186 DISPLAYLEVEL(4, "Sparse File Support is automatically disabled on stdout ; try --sparse \n");
187 }
Yann Colletf0624362016-02-12 15:56:46 +0100188 } else {
189 if (!g_overwrite) { /* Check if destination file already exists */
190 f = fopen( dstFileName, "rb" );
191 if (f != 0) { /* dest file exists, prompt for overwrite authorization */
192 fclose(f);
193 if (g_displayLevel <= 1) {
194 /* No interaction possible */
195 DISPLAY("zstd: %s already exists; not overwritten \n", dstFileName);
196 return 0;
197 }
198 DISPLAY("zstd: %s already exists; do you wish to overwrite (y/N) ? ", dstFileName);
Yann Collet75424d12016-05-23 16:56:56 +0200199 { int ch = getchar();
Yann Colletf0624362016-02-12 15:56:46 +0100200 if ((ch!='Y') && (ch!='y')) {
201 DISPLAY(" not overwritten \n");
202 return 0;
203 }
204 while ((ch!=EOF) && (ch!='\n')) ch = getchar(); /* flush rest of input line */
205 } } }
206 f = fopen( dstFileName, "wb" );
207 }
208 return f;
209}
210
211
Yann Collet8a1d1a62016-03-10 21:02:25 +0100212/*! FIO_loadFile() :
Yann Collet09b21ee2016-03-15 12:56:03 +0100213* creates a buffer, pointed by `*bufferPtr`,
Yann Collet8a1d1a62016-03-10 21:02:25 +0100214* loads `filename` content into it,
215* up to MAX_DICT_SIZE bytes
Yann Colletdeb078b2015-12-17 20:30:14 +0100216*/
217static size_t FIO_loadFile(void** bufferPtr, const char* fileName)
218{
219 FILE* fileHandle;
220 size_t readSize;
221 U64 fileSize;
222
223 *bufferPtr = NULL;
Yann Collet2ce49232016-02-02 14:36:49 +0100224 if (fileName == NULL) return 0;
Yann Colletdeb078b2015-12-17 20:30:14 +0100225
226 DISPLAYLEVEL(4,"Loading %s as dictionary \n", fileName);
227 fileHandle = fopen(fileName, "rb");
228 if (fileHandle==0) EXM_THROW(31, "Error opening file %s", fileName);
inikep69fcd7c2016-04-28 12:23:33 +0200229 fileSize = UTIL_getFileSize(fileName);
Yann Collet2ce49232016-02-02 14:36:49 +0100230 if (fileSize > MAX_DICT_SIZE) {
Yann Colletdeb078b2015-12-17 20:30:14 +0100231 int seekResult;
232 if (fileSize > 1 GB) EXM_THROW(32, "Dictionary file %s is too large", fileName); /* avoid extreme cases */
233 DISPLAYLEVEL(2,"Dictionary %s is too large : using last %u bytes only \n", fileName, MAX_DICT_SIZE);
234 seekResult = fseek(fileHandle, (long int)(fileSize-MAX_DICT_SIZE), SEEK_SET); /* use end of file */
235 if (seekResult != 0) EXM_THROW(33, "Error seeking into file %s", fileName);
236 fileSize = MAX_DICT_SIZE;
237 }
238 *bufferPtr = (BYTE*)malloc((size_t)fileSize);
239 if (*bufferPtr==NULL) EXM_THROW(34, "Allocation error : not enough memory for dictBuffer");
240 readSize = fread(*bufferPtr, 1, (size_t)fileSize, fileHandle);
241 if (readSize!=fileSize) EXM_THROW(35, "Error reading dictionary file %s", fileName);
242 fclose(fileHandle);
243 return (size_t)fileSize;
244}
245
inikep3c7c3522016-04-22 13:59:05 +0200246#ifndef ZSTD_NOCOMPRESS
Yann Collet4f137032015-12-17 02:23:58 +0100247
Yann Collet8a1d1a62016-03-10 21:02:25 +0100248/*-**********************************************************************
Yann Collet4f137032015-12-17 02:23:58 +0100249* Compression
250************************************************************************/
251typedef struct {
252 void* srcBuffer;
253 size_t srcBufferSize;
254 void* dstBuffer;
255 size_t dstBufferSize;
256 void* dictBuffer;
257 size_t dictBufferSize;
258 ZBUFF_CCtx* ctx;
Yann Colletf0624362016-02-12 15:56:46 +0100259 FILE* dstFile;
Yann Collet459a6b72016-02-15 20:37:23 +0100260 FILE* srcFile;
Yann Collet4f137032015-12-17 02:23:58 +0100261} cRess_t;
262
263static cRess_t FIO_createCResources(const char* dictFileName)
264{
265 cRess_t ress;
266
267 ress.ctx = ZBUFF_createCCtx();
268 if (ress.ctx == NULL) EXM_THROW(30, "Allocation error : can't create ZBUFF context");
269
270 /* Allocate Memory */
271 ress.srcBufferSize = ZBUFF_recommendedCInSize();
272 ress.srcBuffer = malloc(ress.srcBufferSize);
273 ress.dstBufferSize = ZBUFF_recommendedCOutSize();
274 ress.dstBuffer = malloc(ress.dstBufferSize);
275 if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(31, "Allocation error : not enough memory");
276
277 /* dictionary */
Yann Colletdeb078b2015-12-17 20:30:14 +0100278 ress.dictBufferSize = FIO_loadFile(&(ress.dictBuffer), dictFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100279
280 return ress;
281}
282
283static void FIO_freeCResources(cRess_t ress)
284{
285 size_t errorCode;
286 free(ress.srcBuffer);
287 free(ress.dstBuffer);
288 free(ress.dictBuffer);
289 errorCode = ZBUFF_freeCCtx(ress.ctx);
290 if (ZBUFF_isError(errorCode)) EXM_THROW(38, "Error : can't release ZBUFF context resource : %s", ZBUFF_getErrorName(errorCode));
291}
292
293
Yann Colletf0624362016-02-12 15:56:46 +0100294/*! FIO_compressFilename_internal() :
Yann Collet8a1d1a62016-03-10 21:02:25 +0100295 * same as FIO_compressFilename_extRess(), with `ress.desFile` already opened.
Yann Colletf0624362016-02-12 15:56:46 +0100296 * @return : 0 : compression completed correctly,
297 * 1 : missing or pb opening srcFileName
Yann Collet4f137032015-12-17 02:23:58 +0100298 */
Yann Colletf0624362016-02-12 15:56:46 +0100299static int FIO_compressFilename_internal(cRess_t ress,
300 const char* dstFileName, const char* srcFileName,
301 int cLevel)
Yann Collet4f137032015-12-17 02:23:58 +0100302{
Yann Colletf8494622016-05-07 22:43:40 +0200303 FILE* const srcFile = ress.srcFile;
304 FILE* const dstFile = ress.dstFile;
Yann Colletb44be742016-03-26 20:52:14 +0100305 U64 readsize = 0;
Yann Collet4f137032015-12-17 02:23:58 +0100306 U64 compressedfilesize = 0;
inikep69fcd7c2016-04-28 12:23:33 +0200307 U64 const fileSize = UTIL_getFileSize(srcFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100308
Yann Collet4f137032015-12-17 02:23:58 +0100309 /* init */
Yann Colletf8494622016-05-07 22:43:40 +0200310 { ZSTD_parameters params;
311 params.cParams = ZSTD_getCParams(cLevel, fileSize, ress.dictBufferSize);
312 params.fParams.contentSizeFlag = 1;
313 if (g_maxWLog) if (params.cParams.windowLog > g_maxWLog) params.cParams.windowLog = g_maxWLog;
314 { size_t const errorCode = ZBUFF_compressInit_advanced(ress.ctx, ress.dictBuffer, ress.dictBufferSize, params, fileSize);
315 if (ZBUFF_isError(errorCode)) EXM_THROW(21, "Error initializing compression : %s", ZBUFF_getErrorName(errorCode)); }
316 }
Yann Collet4f137032015-12-17 02:23:58 +0100317
318 /* Main compression loop */
Yann Colletb44be742016-03-26 20:52:14 +0100319 readsize = 0;
Yann Collet1c8e1942016-01-26 16:31:22 +0100320 while (1) {
Yann Collet4f137032015-12-17 02:23:58 +0100321 /* Fill input Buffer */
Yann Colletb44be742016-03-26 20:52:14 +0100322 size_t const inSize = fread(ress.srcBuffer, (size_t)1, ress.srcBufferSize, srcFile);
Yann Collet4f137032015-12-17 02:23:58 +0100323 if (inSize==0) break;
Yann Colletb44be742016-03-26 20:52:14 +0100324 readsize += inSize;
325 DISPLAYUPDATE(2, "\rRead : %u MB ", (U32)(readsize>>20));
Yann Collet4f137032015-12-17 02:23:58 +0100326
Yann Colletf0624362016-02-12 15:56:46 +0100327 { /* Compress using buffered streaming */
Yann Collet4f137032015-12-17 02:23:58 +0100328 size_t usedInSize = inSize;
329 size_t cSize = ress.dstBufferSize;
Yann Colletf8494622016-05-07 22:43:40 +0200330 { size_t const result = ZBUFF_compressContinue(ress.ctx, ress.dstBuffer, &cSize, ress.srcBuffer, &usedInSize);
331 if (ZBUFF_isError(result)) EXM_THROW(23, "Compression error : %s ", ZBUFF_getErrorName(result)); }
Yann Collet4f137032015-12-17 02:23:58 +0100332 if (inSize != usedInSize)
333 /* inBuff should be entirely consumed since buffer sizes are recommended ones */
334 EXM_THROW(24, "Compression error : input block not fully consumed");
335
336 /* Write cBlock */
Yann Colletf8494622016-05-07 22:43:40 +0200337 { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, cSize, dstFile);
338 if (sizeCheck!=cSize) EXM_THROW(25, "Write error : cannot write compressed block into %s", dstFileName); }
Yann Collet4f137032015-12-17 02:23:58 +0100339 compressedfilesize += cSize;
340 }
Yann Colletb44be742016-03-26 20:52:14 +0100341 DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", (U32)(readsize>>20), (double)compressedfilesize/readsize*100);
Yann Collet4f137032015-12-17 02:23:58 +0100342 }
343
344 /* End of Frame */
Yann Colletb44be742016-03-26 20:52:14 +0100345 { size_t cSize = ress.dstBufferSize;
346 size_t const result = ZBUFF_compressEnd(ress.ctx, ress.dstBuffer, &cSize);
Yann Collet4f137032015-12-17 02:23:58 +0100347 if (result!=0) EXM_THROW(26, "Compression error : cannot create frame end");
348
Yann Colletf8494622016-05-07 22:43:40 +0200349 { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, cSize, dstFile);
350 if (sizeCheck!=cSize) EXM_THROW(27, "Write error : cannot write frame end into %s", dstFileName); }
Yann Collet4f137032015-12-17 02:23:58 +0100351 compressedfilesize += cSize;
352 }
353
354 /* Status */
355 DISPLAYLEVEL(2, "\r%79s\r", "");
356 DISPLAYLEVEL(2,"Compressed %llu bytes into %llu bytes ==> %.2f%%\n",
Yann Colletb44be742016-03-26 20:52:14 +0100357 (unsigned long long)readsize, (unsigned long long) compressedfilesize, (double)compressedfilesize/readsize*100);
Yann Collet4f137032015-12-17 02:23:58 +0100358
Yann Collet4f137032015-12-17 02:23:58 +0100359 return 0;
360}
361
362
Yann Collet459a6b72016-02-15 20:37:23 +0100363/*! FIO_compressFilename_internal() :
Yann Collet09b21ee2016-03-15 12:56:03 +0100364 * same as FIO_compressFilename_extRess(), with ress.destFile already opened (typically stdout)
Yann Collet459a6b72016-02-15 20:37:23 +0100365 * @return : 0 : compression completed correctly,
366 * 1 : missing or pb opening srcFileName
367 */
368static int FIO_compressFilename_srcFile(cRess_t ress,
369 const char* dstFileName, const char* srcFileName,
370 int cLevel)
371{
372 int result;
373
374 /* File check */
375 ress.srcFile = FIO_openSrcFile(srcFileName);
376 if (!ress.srcFile) return 1; /* srcFile could not be opened */
377
378 result = FIO_compressFilename_internal(ress, dstFileName, srcFileName, cLevel);
379
Yann Collet459a6b72016-02-15 20:37:23 +0100380 fclose(ress.srcFile);
381 return result;
382}
383
384
Yann Colletf0624362016-02-12 15:56:46 +0100385/*! FIO_compressFilename_extRess() :
386 * @return : 0 : compression completed correctly,
387 * 1 : missing or pb opening srcFileName
388 */
389static int FIO_compressFilename_extRess(cRess_t ress,
390 const char* dstFileName, const char* srcFileName,
391 int cLevel)
392{
393 int result;
394
Yann Collet459a6b72016-02-15 20:37:23 +0100395 ress.srcFile = FIO_openSrcFile(srcFileName);
396 if (ress.srcFile==0) return 1;
Yann Colletf0624362016-02-12 15:56:46 +0100397 ress.dstFile = FIO_openDstFile(dstFileName);
Yann Collet459a6b72016-02-15 20:37:23 +0100398 if (ress.dstFile==0) { fclose(ress.srcFile); return 1; }
Yann Colletf0624362016-02-12 15:56:46 +0100399
400 result = FIO_compressFilename_internal(ress, dstFileName, srcFileName, cLevel);
Yann Collet09b21ee2016-03-15 12:56:03 +0100401 if (result!=0) remove(dstFileName); /* remove operation artefact */
Chip Turner6de382c2016-03-13 22:24:46 -0700402
Yann Collet459a6b72016-02-15 20:37:23 +0100403 fclose(ress.srcFile); /* no pb to expect : only reading */
Yann Colletf0624362016-02-12 15:56:46 +0100404 if (fclose(ress.dstFile)) EXM_THROW(28, "Write error : cannot properly close %s", dstFileName);
405 return result;
406}
407
408
Yann Collet9d909222015-12-17 14:09:55 +0100409int FIO_compressFilename(const char* dstFileName, const char* srcFileName,
410 const char* dictFileName, int compressionLevel)
Yann Collet4856a002015-01-24 01:58:16 +0100411{
Yann Collet8b23eea2016-05-10 05:37:43 +0200412 clock_t start;
Yann Collet9d909222015-12-17 14:09:55 +0100413 cRess_t ress;
414 int issueWithSrcFile = 0;
Yann Collet88fcd292015-11-25 14:42:45 +0100415
Yann Collet9d909222015-12-17 14:09:55 +0100416 /* Init */
417 start = clock();
418 ress = FIO_createCResources(dictFileName);
Yann Colletf6f3d752015-12-13 13:35:21 +0100419
Yann Collet9d909222015-12-17 14:09:55 +0100420 issueWithSrcFile += FIO_compressFilename_extRess(ress, dstFileName, srcFileName, compressionLevel);
421
Yann Collet9d909222015-12-17 14:09:55 +0100422 FIO_freeCResources(ress);
423
Yann Collet8b23eea2016-05-10 05:37:43 +0200424 { double seconds = (double)(clock() - start) / CLOCKS_PER_SEC;
Yann Collet9d909222015-12-17 14:09:55 +0100425 DISPLAYLEVEL(4, "Completed in %.2f sec \n", seconds);
Yann Colletf6f3d752015-12-13 13:35:21 +0100426 }
Yann Collet9d909222015-12-17 14:09:55 +0100427 return issueWithSrcFile;
Yann Collet4856a002015-01-24 01:58:16 +0100428}
429
430
Yann Collet9d909222015-12-17 14:09:55 +0100431int FIO_compressMultipleFilenames(const char** inFileNamesTable, unsigned nbFiles,
Yann Collet4f137032015-12-17 02:23:58 +0100432 const char* suffix,
433 const char* dictFileName, int compressionLevel)
434{
Yann Collet4f137032015-12-17 02:23:58 +0100435 int missed_files = 0;
Yann Colletf8494622016-05-07 22:43:40 +0200436 char* dstFileName = (char*)malloc(FNSPACE);
Yann Collet4f137032015-12-17 02:23:58 +0100437 size_t dfnSize = FNSPACE;
Yann Colletf8494622016-05-07 22:43:40 +0200438 size_t const suffixSize = suffix ? strlen(suffix) : 0;
Yann Collet4f137032015-12-17 02:23:58 +0100439 cRess_t ress;
440
441 /* init */
442 ress = FIO_createCResources(dictFileName);
443
444 /* loop on each file */
Yann Collet459a6b72016-02-15 20:37:23 +0100445 if (!strcmp(suffix, stdoutmark)) {
Yann Colletf8494622016-05-07 22:43:40 +0200446 unsigned u;
Yann Collet459a6b72016-02-15 20:37:23 +0100447 ress.dstFile = stdout;
inikep60af95d2016-05-19 10:29:49 +0200448 SET_BINARY_MODE(stdout);
Yann Collet459a6b72016-02-15 20:37:23 +0100449 for (u=0; u<nbFiles; u++)
450 missed_files += FIO_compressFilename_srcFile(ress, stdoutmark,
Yann Collet8a1d1a62016-03-10 21:02:25 +0100451 inFileNamesTable[u], compressionLevel);
Yann Collet459a6b72016-02-15 20:37:23 +0100452 if (fclose(ress.dstFile)) EXM_THROW(29, "Write error : cannot properly close %s", stdoutmark);
453 } else {
Yann Colletf8494622016-05-07 22:43:40 +0200454 unsigned u;
Yann Colletf0624362016-02-12 15:56:46 +0100455 for (u=0; u<nbFiles; u++) {
456 size_t ifnSize = strlen(inFileNamesTable[u]);
457 if (dfnSize <= ifnSize+suffixSize+1) { free(dstFileName); dfnSize = ifnSize + 20; dstFileName = (char*)malloc(dfnSize); }
458 strcpy(dstFileName, inFileNamesTable[u]);
459 strcat(dstFileName, suffix);
460 missed_files += FIO_compressFilename_extRess(ress, dstFileName,
461 inFileNamesTable[u], compressionLevel);
Yann Collet459a6b72016-02-15 20:37:23 +0100462 } }
Yann Collet4f137032015-12-17 02:23:58 +0100463
464 /* Close & Free */
465 FIO_freeCResources(ress);
466 free(dstFileName);
467
468 return missed_files;
469}
470
Yann Colletf8494622016-05-07 22:43:40 +0200471#endif /* #ifndef ZSTD_NOCOMPRESS */
inikep3c7c3522016-04-22 13:59:05 +0200472
Yann Collet4f137032015-12-17 02:23:58 +0100473
inikepdb396432016-04-22 18:22:30 +0200474
475#ifndef ZSTD_NODECOMPRESS
476
Yann Collet4f137032015-12-17 02:23:58 +0100477/* **************************************************************************
478* Decompression
479****************************************************************************/
Yann Colletdeb078b2015-12-17 20:30:14 +0100480typedef struct {
481 void* srcBuffer;
482 size_t srcBufferSize;
483 void* dstBuffer;
484 size_t dstBufferSize;
485 void* dictBuffer;
486 size_t dictBufferSize;
487 ZBUFF_DCtx* dctx;
Yann Collet1f1f2392016-02-12 18:33:26 +0100488 FILE* dstFile;
Yann Colletdeb078b2015-12-17 20:30:14 +0100489} dRess_t;
490
491static dRess_t FIO_createDResources(const char* dictFileName)
492{
493 dRess_t ress;
494
495 /* init */
496 ress.dctx = ZBUFF_createDCtx();
497 if (ress.dctx==NULL) EXM_THROW(60, "Can't create ZBUFF decompression context");
498
499 /* Allocate Memory */
500 ress.srcBufferSize = ZBUFF_recommendedDInSize();
501 ress.srcBuffer = malloc(ress.srcBufferSize);
502 ress.dstBufferSize = ZBUFF_recommendedDOutSize();
503 ress.dstBuffer = malloc(ress.dstBufferSize);
504 if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(61, "Allocation error : not enough memory");
505
506 /* dictionary */
507 ress.dictBufferSize = FIO_loadFile(&(ress.dictBuffer), dictFileName);
508
509 return ress;
510}
511
512static void FIO_freeDResources(dRess_t ress)
513{
Yann Colletf8494622016-05-07 22:43:40 +0200514 size_t const errorCode = ZBUFF_freeDCtx(ress.dctx);
Yann Colletdeb078b2015-12-17 20:30:14 +0100515 if (ZBUFF_isError(errorCode)) EXM_THROW(69, "Error : can't free ZBUFF context resource : %s", ZBUFF_getErrorName(errorCode));
516 free(ress.srcBuffer);
517 free(ress.dstBuffer);
518 free(ress.dictBuffer);
519}
Yann Collet4f137032015-12-17 02:23:58 +0100520
521
Yann Collet75424d12016-05-23 16:56:56 +0200522/** FIO_fwriteSparse() :
523* @return : storedSkips, to be provided to next call to FIO_fwriteSparse() of LZ4IO_fwriteSparseEnd() */
524static unsigned FIO_fwriteSparse(FILE* file, const void* buffer, size_t bufferSize, unsigned storedSkips)
525{
526 const size_t* const bufferT = (const size_t*)buffer; /* Buffer is supposed malloc'ed, hence aligned on size_t */
527 size_t bufferSizeT = bufferSize / sizeof(size_t);
528 const size_t* const bufferTEnd = bufferT + bufferSizeT;
529 const size_t* ptrT = bufferT;
530 static const size_t segmentSizeT = (32 KB) / sizeof(size_t); /* 0-test re-attempted every 32 KB */
531
532 if (!g_sparseFileSupport) { /* normal write */
533 size_t const sizeCheck = fwrite(buffer, 1, bufferSize, file);
534 if (sizeCheck != bufferSize) EXM_THROW(70, "Write error : cannot write decoded block");
535 return 0;
536 }
537
538 /* avoid int overflow */
539 if (storedSkips > 1 GB) {
540 int const seekResult = fseek(file, 1 GB, SEEK_CUR);
541 if (seekResult != 0) EXM_THROW(71, "1 GB skip error (sparse file support)");
542 storedSkips -= 1 GB;
543 }
544
545 while (ptrT < bufferTEnd) {
546 size_t seg0SizeT = segmentSizeT;
547 size_t nb0T;
548
549 /* count leading zeros */
550 if (seg0SizeT > bufferSizeT) seg0SizeT = bufferSizeT;
551 bufferSizeT -= seg0SizeT;
552 for (nb0T=0; (nb0T < seg0SizeT) && (ptrT[nb0T] == 0); nb0T++) ;
553 storedSkips += (unsigned)(nb0T * sizeof(size_t));
554
555 if (nb0T != seg0SizeT) { /* not all 0s */
556 int const seekResult = fseek(file, storedSkips, SEEK_CUR);
557 if (seekResult) EXM_THROW(72, "Sparse skip error ; try --no-sparse");
558 storedSkips = 0;
559 seg0SizeT -= nb0T;
560 ptrT += nb0T;
561 { size_t const sizeCheck = fwrite(ptrT, sizeof(size_t), seg0SizeT, file);
562 if (sizeCheck != seg0SizeT) EXM_THROW(73, "Write error : cannot write decoded block");
563 } }
564 ptrT += seg0SizeT;
565 }
566
567 { static size_t const maskT = sizeof(size_t)-1;
568 if (bufferSize & maskT) { /* size not multiple of sizeof(size_t) : implies end of block */
569 const char* const restStart = (const char*)bufferTEnd;
570 const char* restPtr = restStart;
571 size_t restSize = bufferSize & maskT;
572 const char* const restEnd = restStart + restSize;
573 for ( ; (restPtr < restEnd) && (*restPtr == 0); restPtr++) ;
574 storedSkips += (unsigned) (restPtr - restStart);
575 if (restPtr != restEnd) {
576 int seekResult = fseek(file, storedSkips, SEEK_CUR);
577 if (seekResult) EXM_THROW(74, "Sparse skip error ; try --no-sparse");
578 storedSkips = 0;
579 { size_t const sizeCheck = fwrite(restPtr, 1, restEnd - restPtr, file);
580 if (sizeCheck != (size_t)(restEnd - restPtr)) EXM_THROW(75, "Write error : cannot write decoded end of block");
581 } } } }
582
583 return storedSkips;
584}
585
586static void FIO_fwriteSparseEnd(FILE* file, unsigned storedSkips)
587{
588 if (storedSkips-->0) { /* implies g_sparseFileSupport>0 */
589 int const seekResult = fseek(file, storedSkips, SEEK_CUR);
590 if (seekResult != 0) EXM_THROW(69, "Final skip error (sparse file)\n");
591 { const char lastZeroByte[1] = { 0 };
592 size_t const sizeCheck = fwrite(lastZeroByte, 1, 1, file);
593 if (sizeCheck != 1) EXM_THROW(69, "Write error : cannot write last zero\n");
594 } }
595}
596
Yann Collet09b21ee2016-03-15 12:56:03 +0100597/** FIO_decompressFrame() :
598 @return : size of decoded frame
599*/
Yann Colletdeb078b2015-12-17 20:30:14 +0100600unsigned long long FIO_decompressFrame(dRess_t ress,
601 FILE* foutput, FILE* finput, size_t alreadyLoaded)
Yann Collet4856a002015-01-24 01:58:16 +0100602{
Yann Collet88fcd292015-11-25 14:42:45 +0100603 U64 frameSize = 0;
Yann Collet09b21ee2016-03-15 12:56:03 +0100604 size_t readSize;
Yann Collet75424d12016-05-23 16:56:56 +0200605 U32 storedSkips = 0;
Yann Collet09b21ee2016-03-15 12:56:03 +0100606
607 ZBUFF_decompressInitDictionary(ress.dctx, ress.dictBuffer, ress.dictBufferSize);
608
Yann Colletbd39d542016-05-10 14:14:19 +0200609 /* Header loading (optional, saves one loop) */
610 { size_t const toLoad = ZSTD_frameHeaderSize_min - alreadyLoaded; /* assumption : ZSTD_frameHeaderSize_min >= alreadyLoaded */
611 size_t const loadedSize = fread(((char*)ress.srcBuffer) + alreadyLoaded, 1, toLoad, finput);
Yann Colletd6931172016-05-10 05:56:09 +0200612 readSize = alreadyLoaded + loadedSize;
Yann Collet09b21ee2016-03-15 12:56:03 +0100613 }
Yann Collet4856a002015-01-24 01:58:16 +0100614
Yann Collet4856a002015-01-24 01:58:16 +0100615 /* Main decompression Loop */
Yann Collet2ce49232016-02-02 14:36:49 +0100616 while (1) {
Yann Collet88fcd292015-11-25 14:42:45 +0100617 /* Decode */
Yann Colletdeb078b2015-12-17 20:30:14 +0100618 size_t inSize=readSize, decodedSize=ress.dstBufferSize;
Yann Collet8b23eea2016-05-10 05:37:43 +0200619 size_t const toRead = ZBUFF_decompressContinue(ress.dctx, ress.dstBuffer, &decodedSize, ress.srcBuffer, &inSize);
Yann Collet88fcd292015-11-25 14:42:45 +0100620 if (ZBUFF_isError(toRead)) EXM_THROW(36, "Decoding error : %s", ZBUFF_getErrorName(toRead));
Yann Collet88fcd292015-11-25 14:42:45 +0100621 readSize -= inSize;
Yann Collet88fcd292015-11-25 14:42:45 +0100622
623 /* Write block */
Yann Collet75424d12016-05-23 16:56:56 +0200624 storedSkips = FIO_fwriteSparse(foutput, ress.dstBuffer, decodedSize, storedSkips);
Yann Collet88fcd292015-11-25 14:42:45 +0100625 frameSize += decodedSize;
626 DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(frameSize>>20) );
627
Yann Collet09b21ee2016-03-15 12:56:03 +0100628 if (toRead == 0) break; /* end of frame */
Yann Colletdeb078b2015-12-17 20:30:14 +0100629 if (readSize) EXM_THROW(38, "Decoding error : should consume entire input");
Yann Collet4856a002015-01-24 01:58:16 +0100630
631 /* Fill input buffer */
Yann Colletdeb078b2015-12-17 20:30:14 +0100632 if (toRead > ress.srcBufferSize) EXM_THROW(34, "too large block");
633 readSize = fread(ress.srcBuffer, 1, toRead, finput);
Yann Collet09b21ee2016-03-15 12:56:03 +0100634 if (readSize != toRead)
635 EXM_THROW(35, "Read error");
Yann Collet4856a002015-01-24 01:58:16 +0100636 }
637
Yann Collet75424d12016-05-23 16:56:56 +0200638 FIO_fwriteSparseEnd(foutput, storedSkips);
639
Yann Collet88fcd292015-11-25 14:42:45 +0100640 return frameSize;
Yann Colletbe50aaa2015-09-10 23:26:09 +0100641}
642
643
Yann Colletde95f962016-05-23 19:46:47 +0200644/** FIO_passThrough() : just copy input into output, for compatibility with gzip -df mode
645 @return : 0 (no error) */
Yann Colletddbb8e22016-05-24 00:52:14 +0200646static unsigned FIO_passThrough(FILE* foutput, FILE* finput, void* buffer, size_t bufferSize)
Yann Colletde95f962016-05-23 19:46:47 +0200647{
648 size_t const blockSize = MIN (64 KB, bufferSize);
Yann Colletddbb8e22016-05-24 00:52:14 +0200649 size_t readFromInput = 1;
Yann Colletde95f962016-05-23 19:46:47 +0200650 unsigned storedSkips = 0;
651
652 /* assumption : first 4 bytes already loaded (magic number detection), and stored within buffer */
653 { size_t const sizeCheck = fwrite(buffer, 1, 4, foutput);
654 if (sizeCheck != 4) EXM_THROW(50, "Pass-through write error"); }
655
Yann Colletddbb8e22016-05-24 00:52:14 +0200656 while (readFromInput) {
657 readFromInput = fread(buffer, 1, blockSize, finput);
658 storedSkips = FIO_fwriteSparse(foutput, buffer, readFromInput, storedSkips);
Yann Colletde95f962016-05-23 19:46:47 +0200659 }
660
661 FIO_fwriteSparseEnd(foutput, storedSkips);
662 return 0;
663}
664
665
Yann Collet1f1f2392016-02-12 18:33:26 +0100666/** FIO_decompressSrcFile() :
667 Decompression `srcFileName` into `ress.dstFile`
668 @return : 0 : OK
669 1 : operation not started
670*/
671static int FIO_decompressSrcFile(dRess_t ress, const char* srcFileName)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100672{
Yann Colletdeb078b2015-12-17 20:30:14 +0100673 unsigned long long filesize = 0;
Yann Colletf8494622016-05-07 22:43:40 +0200674 FILE* const dstFile = ress.dstFile;
675 FILE* const srcFile = FIO_openSrcFile(srcFileName);
Yann Collet1f1f2392016-02-12 18:33:26 +0100676 if (srcFile==0) return 1;
Yann Collet88fcd292015-11-25 14:42:45 +0100677
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100678 /* for each frame */
Yann Collet2ce49232016-02-02 14:36:49 +0100679 for ( ; ; ) {
Yann Colleta85a8dd2015-11-30 11:53:11 +0100680 /* check magic number -> version */
Yann Collet9e8b09a2016-04-07 19:35:23 +0200681 size_t const toRead = 4;
682 size_t const sizeCheck = fread(ress.srcBuffer, (size_t)1, toRead, srcFile);
Yann Colleta85a8dd2015-11-30 11:53:11 +0100683 if (sizeCheck==0) break; /* no more input */
Yann Collet1f1f2392016-02-12 18:33:26 +0100684 if (sizeCheck != toRead) EXM_THROW(31, "zstd: %s read error : cannot read header", srcFileName);
Yann Colletf8494622016-05-07 22:43:40 +0200685 { U32 const magic = MEM_readLE32(ress.srcBuffer);
686#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
687 if (ZSTD_isLegacy(magic)) {
Yann Collet95af06f2016-05-08 08:23:51 +0200688 filesize += FIO_decompressLegacyFrame(dstFile, srcFile, ress.dictBuffer, ress.dictBufferSize, magic);
Yann Colletf8494622016-05-07 22:43:40 +0200689 continue;
690 }
Yann Collet8b23eea2016-05-10 05:37:43 +0200691#endif
inikepf772bf52016-05-31 12:43:46 +0200692 if (((magic & 0xFFFFFFF0U) != ZSTD_MAGIC_SKIPPABLE_START) && (magic != ZSTD_MAGICNUMBER)) {
Yann Colletde95f962016-05-23 19:46:47 +0200693 if (g_overwrite) /* -df : pass-through mode */
694 return FIO_passThrough(dstFile, srcFile, ress.srcBuffer, ress.srcBufferSize);
695 else {
696 DISPLAYLEVEL(1, "zstd: %s: not in zstd format \n", srcFileName);
697 return 1;
698 } } }
Yann Colletdeb078b2015-12-17 20:30:14 +0100699 filesize += FIO_decompressFrame(ress, dstFile, srcFile, toRead);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100700 }
701
Yann Colletdeb078b2015-12-17 20:30:14 +0100702 /* Final Status */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100703 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Colletdeb078b2015-12-17 20:30:14 +0100704 DISPLAYLEVEL(2, "Successfully decoded %llu bytes \n", filesize);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100705
Yann Colletdeb078b2015-12-17 20:30:14 +0100706 /* Close */
707 fclose(srcFile);
Yann Collet1f1f2392016-02-12 18:33:26 +0100708 return 0;
709}
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100710
Yann Collet1f1f2392016-02-12 18:33:26 +0100711
712/** FIO_decompressFile_extRess() :
713 decompress `srcFileName` into `dstFileName`
714 @return : 0 : OK
715 1 : operation aborted (src not available, dst already taken, etc.)
716*/
717static int FIO_decompressFile_extRess(dRess_t ress,
718 const char* dstFileName, const char* srcFileName)
719{
Chip Turner6de382c2016-03-13 22:24:46 -0700720 int result;
Yann Collet1f1f2392016-02-12 18:33:26 +0100721 ress.dstFile = FIO_openDstFile(dstFileName);
722 if (ress.dstFile==0) return 1;
723
Chip Turner6de382c2016-03-13 22:24:46 -0700724 result = FIO_decompressSrcFile(ress, srcFileName);
Yann Collet8b23eea2016-05-10 05:37:43 +0200725 if (result != 0) remove(dstFileName);
Yann Collet1f1f2392016-02-12 18:33:26 +0100726
727 if (fclose(ress.dstFile)) EXM_THROW(38, "Write error : cannot properly close %s", dstFileName);
Chip Turner6de382c2016-03-13 22:24:46 -0700728 return result;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100729}
730
731
Yann Colletdeb078b2015-12-17 20:30:14 +0100732int FIO_decompressFilename(const char* dstFileName, const char* srcFileName,
733 const char* dictFileName)
734{
735 int missingFiles = 0;
736 dRess_t ress = FIO_createDResources(dictFileName);
737
738 missingFiles += FIO_decompressFile_extRess(ress, dstFileName, srcFileName);
739
740 FIO_freeDResources(ress);
741 return missingFiles;
742}
743
744
745#define MAXSUFFIXSIZE 8
746int FIO_decompressMultipleFilenames(const char** srcNamesTable, unsigned nbFiles,
747 const char* suffix,
748 const char* dictFileName)
749{
Yann Colletdeb078b2015-12-17 20:30:14 +0100750 int skippedFiles = 0;
751 int missingFiles = 0;
Yann Collet8b23eea2016-05-10 05:37:43 +0200752 dRess_t ress = FIO_createDResources(dictFileName);
Yann Colletdeb078b2015-12-17 20:30:14 +0100753
Yann Colletaccfd802016-02-15 19:33:16 +0100754 if (!strcmp(suffix, stdoutmark) || !strcmp(suffix, nulmark)) {
Yann Colletf8494622016-05-07 22:43:40 +0200755 unsigned u;
Yann Colletaccfd802016-02-15 19:33:16 +0100756 ress.dstFile = FIO_openDstFile(suffix);
757 if (ress.dstFile == 0) EXM_THROW(71, "cannot open %s", suffix);
758 for (u=0; u<nbFiles; u++)
759 missingFiles += FIO_decompressSrcFile(ress, srcNamesTable[u]);
760 if (fclose(ress.dstFile)) EXM_THROW(39, "Write error : cannot properly close %s", stdoutmark);
761 } else {
Yann Collet8b23eea2016-05-10 05:37:43 +0200762 size_t const suffixSize = suffix ? strlen(suffix) : 0;
763 size_t dfnSize = FNSPACE;
Yann Colletf8494622016-05-07 22:43:40 +0200764 unsigned u;
Yann Collet8b23eea2016-05-10 05:37:43 +0200765 char* dstFileName = (char*)malloc(FNSPACE);
766 if (dstFileName==NULL) EXM_THROW(70, "not enough memory for dstFileName");
Yann Collet1f1f2392016-02-12 18:33:26 +0100767 for (u=0; u<nbFiles; u++) { /* create dstFileName */
Yann Collet8b23eea2016-05-10 05:37:43 +0200768 const char* const srcFileName = srcNamesTable[u];
769 size_t const sfnSize = strlen(srcFileName);
770 const char* const suffixPtr = srcFileName + sfnSize - suffixSize;
Yann Colletaccfd802016-02-15 19:33:16 +0100771 if (dfnSize+suffixSize <= sfnSize+1) {
772 free(dstFileName);
773 dfnSize = sfnSize + 20;
774 dstFileName = (char*)malloc(dfnSize);
775 if (dstFileName==NULL) EXM_THROW(71, "not enough memory for dstFileName");
776 }
777 if (sfnSize <= suffixSize || strcmp(suffixPtr, suffix) != 0) {
Yann Collet1f1f2392016-02-12 18:33:26 +0100778 DISPLAYLEVEL(1, "zstd: %s: unknown suffix (%4s expected) -- ignored \n", srcFileName, suffix);
779 skippedFiles++;
780 continue;
781 }
782 memcpy(dstFileName, srcFileName, sfnSize - suffixSize);
783 dstFileName[sfnSize-suffixSize] = '\0';
Yann Colletdeb078b2015-12-17 20:30:14 +0100784
Yann Collet1f1f2392016-02-12 18:33:26 +0100785 missingFiles += FIO_decompressFile_extRess(ress, dstFileName, srcFileName);
Yann Collet8b23eea2016-05-10 05:37:43 +0200786 }
787 free(dstFileName);
788 }
Yann Colletdeb078b2015-12-17 20:30:14 +0100789
790 FIO_freeDResources(ress);
Yann Colletdeb078b2015-12-17 20:30:14 +0100791 return missingFiles + skippedFiles;
792}
Yann Colletaccfd802016-02-15 19:33:16 +0100793
Yann Collet8b23eea2016-05-10 05:37:43 +0200794#endif /* #ifndef ZSTD_NODECOMPRESS */