blob: a5e9f08c443b53abd1808528001a1cdb9012815c [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 Collet4856a002015-01-24 01:58:16 +010044/* Disable some Visual warning messages */
45#ifdef _MSC_VER
46# define _CRT_SECURE_NO_WARNINGS
47# define _CRT_SECURE_NO_DEPRECATE /* VS2005 */
48# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
49#endif
50
Yann Collet4856a002015-01-24 01:58:16 +010051#define _FILE_OFFSET_BITS 64 /* Large file support on 32-bits unix */
52#define _POSIX_SOURCE 1 /* enable fileno() within <stdio.h> on unix */
53
54
Yann Collet6f3acba2016-02-12 20:19:48 +010055/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010056* Includes
Yann Colleteeb8ba12015-10-22 16:55:40 +010057***************************************/
inikepbab43172016-04-29 15:19:40 +020058#include "util.h" /* UTIL_GetFileSize */
Yann Collet9f432922015-11-09 17:42:17 +010059#include <stdio.h> /* fprintf, fopen, fread, _fileno, stdin, stdout */
60#include <stdlib.h> /* malloc, free */
61#include <string.h> /* strcmp, strlen */
62#include <time.h> /* clock */
63#include <errno.h> /* errno */
inikepd5ff2c32016-04-28 14:40:45 +020064
inikepd5ff2c32016-04-28 14:40:45 +020065#include "mem.h"
Yann Collet4856a002015-01-24 01:58:16 +010066#include "fileio.h"
Yann Collet09b21ee2016-03-15 12:56:03 +010067#include "zstd_static.h" /* ZSTD_magicNumber, ZSTD_frameHeaderSize_max */
Yann Collet62ae5fb2016-02-12 18:59:11 +010068#include "zbuff_static.h"
Yann Collet4856a002015-01-24 01:58:16 +010069
Yann Colletb1f3f4b2015-10-18 22:18:32 +010070#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
Yann Collet6f3acba2016-02-12 20:19:48 +010071# include "zstd_legacy.h" /* ZSTD_isLegacy */
72# include "fileio_legacy.h" /* FIO_decompressLegacyFrame */
Yann Colleteeb8ba12015-10-22 16:55:40 +010073#endif
Yann Colletb1f3f4b2015-10-18 22:18:32 +010074
Yann Collet4856a002015-01-24 01:58:16 +010075
Yann Collet6f3acba2016-02-12 20:19:48 +010076/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010077* OS-specific Includes
Yann Colleteeb8ba12015-10-22 16:55:40 +010078***************************************/
Yann Collet4856a002015-01-24 01:58:16 +010079#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
80# include <fcntl.h> /* _O_BINARY */
81# include <io.h> /* _setmode, _isatty */
Yann Colletb5e06dc2015-07-04 23:20:56 -080082# define SET_BINARY_MODE(file) { int unused = _setmode(_fileno(file), _O_BINARY); (void)unused; }
Yann Collet4856a002015-01-24 01:58:16 +010083# define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
84#else
85# include <unistd.h> /* isatty */
86# define SET_BINARY_MODE(file)
87# define IS_CONSOLE(stdStream) isatty(fileno(stdStream))
88#endif
89
90
Yann Collet6f3acba2016-02-12 20:19:48 +010091/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010092* Constants
Yann Colleteeb8ba12015-10-22 16:55:40 +010093***************************************/
Yann Collet4856a002015-01-24 01:58:16 +010094#define _1BIT 0x01
95#define _2BITS 0x03
96#define _3BITS 0x07
97#define _4BITS 0x0F
98#define _6BITS 0x3F
99#define _8BITS 0xFF
100
Yann Collet88fcd292015-11-25 14:42:45 +0100101#define BLOCKSIZE (128 KB)
102#define ROLLBUFFERSIZE (BLOCKSIZE*8*64)
Yann Collet4856a002015-01-24 01:58:16 +0100103
Yann Collet6f3acba2016-02-12 20:19:48 +0100104#define FIO_FRAMEHEADERSIZE 5 /* as a define, because needed to allocated table on stack */
105#define FSE_CHECKSUM_SEED 0
Yann Collet4856a002015-01-24 01:58:16 +0100106
107#define CACHELINE 64
108
Yann Collet6f3acba2016-02-12 20:19:48 +0100109#define MAX_DICT_SIZE (1 MB) /* protection against large input (attack scenario) ; can be changed */
110
inikep3c7c3522016-04-22 13:59:05 +0200111#define FNSPACE 30
112
Yann Collet4856a002015-01-24 01:58:16 +0100113
Yann Collet459a6b72016-02-15 20:37:23 +0100114/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +0100115* Macros
Yann Colleteeb8ba12015-10-22 16:55:40 +0100116***************************************/
Yann Collet4856a002015-01-24 01:58:16 +0100117#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
118#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
119static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
Yann Collet8a1d1a62016-03-10 21:02:25 +0100120void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; }
Yann Collet4856a002015-01-24 01:58:16 +0100121
122#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
123 if ((FIO_GetMilliSpan(g_time) > refreshRate) || (g_displayLevel>=4)) \
124 { g_time = clock(); DISPLAY(__VA_ARGS__); \
125 if (g_displayLevel>=4) fflush(stdout); } }
126static const unsigned refreshRate = 150;
127static clock_t g_time = 0;
128
129
Yann Collet459a6b72016-02-15 20:37:23 +0100130
131/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +0100132* Local Parameters
Yann Colleteeb8ba12015-10-22 16:55:40 +0100133***************************************/
Yann Collet4856a002015-01-24 01:58:16 +0100134static U32 g_overwrite = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100135void FIO_overwriteMode(void) { g_overwrite=1; }
Yann Collet8a1d1a62016-03-10 21:02:25 +0100136static U32 g_maxWLog = 23;
137void FIO_setMaxWLog(unsigned maxWLog) { g_maxWLog = maxWLog; }
Yann Collet4856a002015-01-24 01:58:16 +0100138
139
Yann Collet459a6b72016-02-15 20:37:23 +0100140/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +0100141* Exceptions
Yann Colleteeb8ba12015-10-22 16:55:40 +0100142***************************************/
143#ifndef DEBUG
144# define DEBUG 0
145#endif
Yann Collet4856a002015-01-24 01:58:16 +0100146#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
147#define EXM_THROW(error, ...) \
148{ \
149 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
150 DISPLAYLEVEL(1, "Error %i : ", error); \
151 DISPLAYLEVEL(1, __VA_ARGS__); \
152 DISPLAYLEVEL(1, "\n"); \
153 exit(error); \
154}
155
156
Yann Collet459a6b72016-02-15 20:37:23 +0100157/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +0100158* Functions
Yann Colleteeb8ba12015-10-22 16:55:40 +0100159***************************************/
Yann Collet4856a002015-01-24 01:58:16 +0100160static unsigned FIO_GetMilliSpan(clock_t nPrevious)
161{
162 clock_t nCurrent = clock();
163 unsigned nSpan = (unsigned)(((nCurrent - nPrevious) * 1000) / CLOCKS_PER_SEC);
164 return nSpan;
165}
166
167
Yann Colletf0624362016-02-12 15:56:46 +0100168static FILE* FIO_openSrcFile(const char* srcFileName)
169{
170 FILE* f;
171
172 if (!strcmp (srcFileName, stdinmark)) {
173 DISPLAYLEVEL(4,"Using stdin for input\n");
174 f = stdin;
175 SET_BINARY_MODE(stdin);
176 } else {
177 f = fopen(srcFileName, "rb");
178 }
179
180 if ( f==NULL ) DISPLAYLEVEL(1, "zstd: %s: No such file\n", srcFileName);
181
182 return f;
183}
184
185
186static FILE* FIO_openDstFile(const char* dstFileName)
187{
188 FILE* f;
189
190 if (!strcmp (dstFileName, stdoutmark)) {
191 DISPLAYLEVEL(4,"Using stdout for output\n");
192 f = stdout;
193 SET_BINARY_MODE(stdout);
194 } else {
195 if (!g_overwrite) { /* Check if destination file already exists */
196 f = fopen( dstFileName, "rb" );
197 if (f != 0) { /* dest file exists, prompt for overwrite authorization */
198 fclose(f);
199 if (g_displayLevel <= 1) {
200 /* No interaction possible */
201 DISPLAY("zstd: %s already exists; not overwritten \n", dstFileName);
202 return 0;
203 }
204 DISPLAY("zstd: %s already exists; do you wish to overwrite (y/N) ? ", dstFileName);
205 {
206 int ch = getchar();
207 if ((ch!='Y') && (ch!='y')) {
208 DISPLAY(" not overwritten \n");
209 return 0;
210 }
211 while ((ch!=EOF) && (ch!='\n')) ch = getchar(); /* flush rest of input line */
212 } } }
213 f = fopen( dstFileName, "wb" );
214 }
215 return f;
216}
217
218
Yann Collet8a1d1a62016-03-10 21:02:25 +0100219/*! FIO_loadFile() :
Yann Collet09b21ee2016-03-15 12:56:03 +0100220* creates a buffer, pointed by `*bufferPtr`,
Yann Collet8a1d1a62016-03-10 21:02:25 +0100221* loads `filename` content into it,
222* up to MAX_DICT_SIZE bytes
Yann Colletdeb078b2015-12-17 20:30:14 +0100223*/
224static size_t FIO_loadFile(void** bufferPtr, const char* fileName)
225{
226 FILE* fileHandle;
227 size_t readSize;
228 U64 fileSize;
229
230 *bufferPtr = NULL;
Yann Collet2ce49232016-02-02 14:36:49 +0100231 if (fileName == NULL) return 0;
Yann Colletdeb078b2015-12-17 20:30:14 +0100232
233 DISPLAYLEVEL(4,"Loading %s as dictionary \n", fileName);
234 fileHandle = fopen(fileName, "rb");
235 if (fileHandle==0) EXM_THROW(31, "Error opening file %s", fileName);
inikep69fcd7c2016-04-28 12:23:33 +0200236 fileSize = UTIL_getFileSize(fileName);
Yann Collet2ce49232016-02-02 14:36:49 +0100237 if (fileSize > MAX_DICT_SIZE) {
Yann Colletdeb078b2015-12-17 20:30:14 +0100238 int seekResult;
239 if (fileSize > 1 GB) EXM_THROW(32, "Dictionary file %s is too large", fileName); /* avoid extreme cases */
240 DISPLAYLEVEL(2,"Dictionary %s is too large : using last %u bytes only \n", fileName, MAX_DICT_SIZE);
241 seekResult = fseek(fileHandle, (long int)(fileSize-MAX_DICT_SIZE), SEEK_SET); /* use end of file */
242 if (seekResult != 0) EXM_THROW(33, "Error seeking into file %s", fileName);
243 fileSize = MAX_DICT_SIZE;
244 }
245 *bufferPtr = (BYTE*)malloc((size_t)fileSize);
246 if (*bufferPtr==NULL) EXM_THROW(34, "Allocation error : not enough memory for dictBuffer");
247 readSize = fread(*bufferPtr, 1, (size_t)fileSize, fileHandle);
248 if (readSize!=fileSize) EXM_THROW(35, "Error reading dictionary file %s", fileName);
249 fclose(fileHandle);
250 return (size_t)fileSize;
251}
252
inikep3c7c3522016-04-22 13:59:05 +0200253#ifndef ZSTD_NOCOMPRESS
Yann Collet4f137032015-12-17 02:23:58 +0100254
Yann Collet8a1d1a62016-03-10 21:02:25 +0100255/*-**********************************************************************
Yann Collet4f137032015-12-17 02:23:58 +0100256* Compression
257************************************************************************/
258typedef struct {
259 void* srcBuffer;
260 size_t srcBufferSize;
261 void* dstBuffer;
262 size_t dstBufferSize;
263 void* dictBuffer;
264 size_t dictBufferSize;
265 ZBUFF_CCtx* ctx;
Yann Colletf0624362016-02-12 15:56:46 +0100266 FILE* dstFile;
Yann Collet459a6b72016-02-15 20:37:23 +0100267 FILE* srcFile;
Yann Collet4f137032015-12-17 02:23:58 +0100268} cRess_t;
269
270static cRess_t FIO_createCResources(const char* dictFileName)
271{
272 cRess_t ress;
273
274 ress.ctx = ZBUFF_createCCtx();
275 if (ress.ctx == NULL) EXM_THROW(30, "Allocation error : can't create ZBUFF context");
276
277 /* Allocate Memory */
278 ress.srcBufferSize = ZBUFF_recommendedCInSize();
279 ress.srcBuffer = malloc(ress.srcBufferSize);
280 ress.dstBufferSize = ZBUFF_recommendedCOutSize();
281 ress.dstBuffer = malloc(ress.dstBufferSize);
282 if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(31, "Allocation error : not enough memory");
283
284 /* dictionary */
Yann Colletdeb078b2015-12-17 20:30:14 +0100285 ress.dictBufferSize = FIO_loadFile(&(ress.dictBuffer), dictFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100286
287 return ress;
288}
289
290static void FIO_freeCResources(cRess_t ress)
291{
292 size_t errorCode;
293 free(ress.srcBuffer);
294 free(ress.dstBuffer);
295 free(ress.dictBuffer);
296 errorCode = ZBUFF_freeCCtx(ress.ctx);
297 if (ZBUFF_isError(errorCode)) EXM_THROW(38, "Error : can't release ZBUFF context resource : %s", ZBUFF_getErrorName(errorCode));
298}
299
300
Yann Colletf0624362016-02-12 15:56:46 +0100301/*! FIO_compressFilename_internal() :
Yann Collet8a1d1a62016-03-10 21:02:25 +0100302 * same as FIO_compressFilename_extRess(), with `ress.desFile` already opened.
Yann Colletf0624362016-02-12 15:56:46 +0100303 * @return : 0 : compression completed correctly,
304 * 1 : missing or pb opening srcFileName
Yann Collet4f137032015-12-17 02:23:58 +0100305 */
Yann Colletf0624362016-02-12 15:56:46 +0100306static int FIO_compressFilename_internal(cRess_t ress,
307 const char* dstFileName, const char* srcFileName,
308 int cLevel)
Yann Collet4f137032015-12-17 02:23:58 +0100309{
Yann Colletf8494622016-05-07 22:43:40 +0200310 FILE* const srcFile = ress.srcFile;
311 FILE* const dstFile = ress.dstFile;
Yann Colletb44be742016-03-26 20:52:14 +0100312 U64 readsize = 0;
Yann Collet4f137032015-12-17 02:23:58 +0100313 U64 compressedfilesize = 0;
inikep69fcd7c2016-04-28 12:23:33 +0200314 U64 const fileSize = UTIL_getFileSize(srcFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100315
Yann Collet4f137032015-12-17 02:23:58 +0100316 /* init */
Yann Colletf8494622016-05-07 22:43:40 +0200317 { ZSTD_parameters params;
318 params.cParams = ZSTD_getCParams(cLevel, fileSize, ress.dictBufferSize);
319 params.fParams.contentSizeFlag = 1;
320 if (g_maxWLog) if (params.cParams.windowLog > g_maxWLog) params.cParams.windowLog = g_maxWLog;
321 { size_t const errorCode = ZBUFF_compressInit_advanced(ress.ctx, ress.dictBuffer, ress.dictBufferSize, params, fileSize);
322 if (ZBUFF_isError(errorCode)) EXM_THROW(21, "Error initializing compression : %s", ZBUFF_getErrorName(errorCode)); }
323 }
Yann Collet4f137032015-12-17 02:23:58 +0100324
325 /* Main compression loop */
Yann Colletb44be742016-03-26 20:52:14 +0100326 readsize = 0;
Yann Collet1c8e1942016-01-26 16:31:22 +0100327 while (1) {
Yann Collet4f137032015-12-17 02:23:58 +0100328 /* Fill input Buffer */
Yann Colletb44be742016-03-26 20:52:14 +0100329 size_t const inSize = fread(ress.srcBuffer, (size_t)1, ress.srcBufferSize, srcFile);
Yann Collet4f137032015-12-17 02:23:58 +0100330 if (inSize==0) break;
Yann Colletb44be742016-03-26 20:52:14 +0100331 readsize += inSize;
332 DISPLAYUPDATE(2, "\rRead : %u MB ", (U32)(readsize>>20));
Yann Collet4f137032015-12-17 02:23:58 +0100333
Yann Colletf0624362016-02-12 15:56:46 +0100334 { /* Compress using buffered streaming */
Yann Collet4f137032015-12-17 02:23:58 +0100335 size_t usedInSize = inSize;
336 size_t cSize = ress.dstBufferSize;
Yann Colletf8494622016-05-07 22:43:40 +0200337 { size_t const result = ZBUFF_compressContinue(ress.ctx, ress.dstBuffer, &cSize, ress.srcBuffer, &usedInSize);
338 if (ZBUFF_isError(result)) EXM_THROW(23, "Compression error : %s ", ZBUFF_getErrorName(result)); }
Yann Collet4f137032015-12-17 02:23:58 +0100339 if (inSize != usedInSize)
340 /* inBuff should be entirely consumed since buffer sizes are recommended ones */
341 EXM_THROW(24, "Compression error : input block not fully consumed");
342
343 /* Write cBlock */
Yann Colletf8494622016-05-07 22:43:40 +0200344 { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, cSize, dstFile);
345 if (sizeCheck!=cSize) EXM_THROW(25, "Write error : cannot write compressed block into %s", dstFileName); }
Yann Collet4f137032015-12-17 02:23:58 +0100346 compressedfilesize += cSize;
347 }
Yann Colletb44be742016-03-26 20:52:14 +0100348 DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", (U32)(readsize>>20), (double)compressedfilesize/readsize*100);
Yann Collet4f137032015-12-17 02:23:58 +0100349 }
350
351 /* End of Frame */
Yann Colletb44be742016-03-26 20:52:14 +0100352 { size_t cSize = ress.dstBufferSize;
353 size_t const result = ZBUFF_compressEnd(ress.ctx, ress.dstBuffer, &cSize);
Yann Collet4f137032015-12-17 02:23:58 +0100354 if (result!=0) EXM_THROW(26, "Compression error : cannot create frame end");
355
Yann Colletf8494622016-05-07 22:43:40 +0200356 { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, cSize, dstFile);
357 if (sizeCheck!=cSize) EXM_THROW(27, "Write error : cannot write frame end into %s", dstFileName); }
Yann Collet4f137032015-12-17 02:23:58 +0100358 compressedfilesize += cSize;
359 }
360
361 /* Status */
362 DISPLAYLEVEL(2, "\r%79s\r", "");
363 DISPLAYLEVEL(2,"Compressed %llu bytes into %llu bytes ==> %.2f%%\n",
Yann Colletb44be742016-03-26 20:52:14 +0100364 (unsigned long long)readsize, (unsigned long long) compressedfilesize, (double)compressedfilesize/readsize*100);
Yann Collet4f137032015-12-17 02:23:58 +0100365
Yann Collet4f137032015-12-17 02:23:58 +0100366 return 0;
367}
368
369
Yann Collet459a6b72016-02-15 20:37:23 +0100370/*! FIO_compressFilename_internal() :
Yann Collet09b21ee2016-03-15 12:56:03 +0100371 * same as FIO_compressFilename_extRess(), with ress.destFile already opened (typically stdout)
Yann Collet459a6b72016-02-15 20:37:23 +0100372 * @return : 0 : compression completed correctly,
373 * 1 : missing or pb opening srcFileName
374 */
375static int FIO_compressFilename_srcFile(cRess_t ress,
376 const char* dstFileName, const char* srcFileName,
377 int cLevel)
378{
379 int result;
380
381 /* File check */
382 ress.srcFile = FIO_openSrcFile(srcFileName);
383 if (!ress.srcFile) return 1; /* srcFile could not be opened */
384
385 result = FIO_compressFilename_internal(ress, dstFileName, srcFileName, cLevel);
386
Yann Collet459a6b72016-02-15 20:37:23 +0100387 fclose(ress.srcFile);
388 return result;
389}
390
391
Yann Colletf0624362016-02-12 15:56:46 +0100392/*! FIO_compressFilename_extRess() :
393 * @return : 0 : compression completed correctly,
394 * 1 : missing or pb opening srcFileName
395 */
396static int FIO_compressFilename_extRess(cRess_t ress,
397 const char* dstFileName, const char* srcFileName,
398 int cLevel)
399{
400 int result;
401
Yann Collet459a6b72016-02-15 20:37:23 +0100402 ress.srcFile = FIO_openSrcFile(srcFileName);
403 if (ress.srcFile==0) return 1;
Yann Colletf0624362016-02-12 15:56:46 +0100404 ress.dstFile = FIO_openDstFile(dstFileName);
Yann Collet459a6b72016-02-15 20:37:23 +0100405 if (ress.dstFile==0) { fclose(ress.srcFile); return 1; }
Yann Colletf0624362016-02-12 15:56:46 +0100406
407 result = FIO_compressFilename_internal(ress, dstFileName, srcFileName, cLevel);
Yann Collet09b21ee2016-03-15 12:56:03 +0100408 if (result!=0) remove(dstFileName); /* remove operation artefact */
Chip Turner6de382c2016-03-13 22:24:46 -0700409
Yann Collet459a6b72016-02-15 20:37:23 +0100410 fclose(ress.srcFile); /* no pb to expect : only reading */
Yann Colletf0624362016-02-12 15:56:46 +0100411 if (fclose(ress.dstFile)) EXM_THROW(28, "Write error : cannot properly close %s", dstFileName);
412 return result;
413}
414
415
Yann Collet9d909222015-12-17 14:09:55 +0100416int FIO_compressFilename(const char* dstFileName, const char* srcFileName,
417 const char* dictFileName, int compressionLevel)
Yann Collet4856a002015-01-24 01:58:16 +0100418{
Yann Collet9d909222015-12-17 14:09:55 +0100419 clock_t start, end;
420 cRess_t ress;
421 int issueWithSrcFile = 0;
Yann Collet88fcd292015-11-25 14:42:45 +0100422
Yann Collet9d909222015-12-17 14:09:55 +0100423 /* Init */
424 start = clock();
425 ress = FIO_createCResources(dictFileName);
Yann Colletf6f3d752015-12-13 13:35:21 +0100426
Yann Collet9d909222015-12-17 14:09:55 +0100427 /* Compress File */
428 issueWithSrcFile += FIO_compressFilename_extRess(ress, dstFileName, srcFileName, compressionLevel);
429
430 /* Free resources */
431 FIO_freeCResources(ress);
432
433 /* Final Status */
434 end = clock();
Yann Colletf8494622016-05-07 22:43:40 +0200435 { double seconds = (double)(end - start) / CLOCKS_PER_SEC;
Yann Collet9d909222015-12-17 14:09:55 +0100436 DISPLAYLEVEL(4, "Completed in %.2f sec \n", seconds);
Yann Colletf6f3d752015-12-13 13:35:21 +0100437 }
Yann Collet4856a002015-01-24 01:58:16 +0100438
Yann Collet9d909222015-12-17 14:09:55 +0100439 return issueWithSrcFile;
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;
460 for (u=0; u<nbFiles; u++)
461 missed_files += FIO_compressFilename_srcFile(ress, stdoutmark,
Yann Collet8a1d1a62016-03-10 21:02:25 +0100462 inFileNamesTable[u], compressionLevel);
Yann Collet459a6b72016-02-15 20:37:23 +0100463 if (fclose(ress.dstFile)) EXM_THROW(29, "Write error : cannot properly close %s", stdoutmark);
464 } else {
Yann Colletf8494622016-05-07 22:43:40 +0200465 unsigned u;
Yann Colletf0624362016-02-12 15:56:46 +0100466 for (u=0; u<nbFiles; u++) {
467 size_t ifnSize = strlen(inFileNamesTable[u]);
468 if (dfnSize <= ifnSize+suffixSize+1) { free(dstFileName); dfnSize = ifnSize + 20; dstFileName = (char*)malloc(dfnSize); }
469 strcpy(dstFileName, inFileNamesTable[u]);
470 strcat(dstFileName, suffix);
471 missed_files += FIO_compressFilename_extRess(ress, dstFileName,
472 inFileNamesTable[u], compressionLevel);
Yann Collet459a6b72016-02-15 20:37:23 +0100473 } }
Yann Collet4f137032015-12-17 02:23:58 +0100474
475 /* Close & Free */
476 FIO_freeCResources(ress);
477 free(dstFileName);
478
479 return missed_files;
480}
481
Yann Colletf8494622016-05-07 22:43:40 +0200482#endif /* #ifndef ZSTD_NOCOMPRESS */
inikep3c7c3522016-04-22 13:59:05 +0200483
Yann Collet4f137032015-12-17 02:23:58 +0100484
inikepdb396432016-04-22 18:22:30 +0200485
486#ifndef ZSTD_NODECOMPRESS
487
Yann Collet4f137032015-12-17 02:23:58 +0100488/* **************************************************************************
489* Decompression
490****************************************************************************/
Yann Colletdeb078b2015-12-17 20:30:14 +0100491typedef struct {
492 void* srcBuffer;
493 size_t srcBufferSize;
494 void* dstBuffer;
495 size_t dstBufferSize;
496 void* dictBuffer;
497 size_t dictBufferSize;
498 ZBUFF_DCtx* dctx;
Yann Collet1f1f2392016-02-12 18:33:26 +0100499 FILE* dstFile;
Yann Colletdeb078b2015-12-17 20:30:14 +0100500} dRess_t;
501
502static dRess_t FIO_createDResources(const char* dictFileName)
503{
504 dRess_t ress;
505
506 /* init */
507 ress.dctx = ZBUFF_createDCtx();
508 if (ress.dctx==NULL) EXM_THROW(60, "Can't create ZBUFF decompression context");
509
510 /* Allocate Memory */
511 ress.srcBufferSize = ZBUFF_recommendedDInSize();
512 ress.srcBuffer = malloc(ress.srcBufferSize);
513 ress.dstBufferSize = ZBUFF_recommendedDOutSize();
514 ress.dstBuffer = malloc(ress.dstBufferSize);
515 if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(61, "Allocation error : not enough memory");
516
517 /* dictionary */
518 ress.dictBufferSize = FIO_loadFile(&(ress.dictBuffer), dictFileName);
519
520 return ress;
521}
522
523static void FIO_freeDResources(dRess_t ress)
524{
Yann Colletf8494622016-05-07 22:43:40 +0200525 size_t const errorCode = ZBUFF_freeDCtx(ress.dctx);
Yann Colletdeb078b2015-12-17 20:30:14 +0100526 if (ZBUFF_isError(errorCode)) EXM_THROW(69, "Error : can't free ZBUFF context resource : %s", ZBUFF_getErrorName(errorCode));
527 free(ress.srcBuffer);
528 free(ress.dstBuffer);
529 free(ress.dictBuffer);
530}
Yann Collet4f137032015-12-17 02:23:58 +0100531
532
Yann Collet09b21ee2016-03-15 12:56:03 +0100533/** FIO_decompressFrame() :
534 @return : size of decoded frame
535*/
Yann Colletdeb078b2015-12-17 20:30:14 +0100536unsigned long long FIO_decompressFrame(dRess_t ress,
537 FILE* foutput, FILE* finput, size_t alreadyLoaded)
Yann Collet4856a002015-01-24 01:58:16 +0100538{
Yann Collet88fcd292015-11-25 14:42:45 +0100539 U64 frameSize = 0;
Yann Collet09b21ee2016-03-15 12:56:03 +0100540 size_t readSize;
541
542 ZBUFF_decompressInitDictionary(ress.dctx, ress.dictBuffer, ress.dictBufferSize);
543
544 /* Complete Header loading */
545 { size_t const toLoad = ZSTD_frameHeaderSize_max - alreadyLoaded; /* assumption : alreadyLoaded <= ZSTD_frameHeaderSize_max */
546 size_t const checkSize = fread(((char*)ress.srcBuffer) + alreadyLoaded, 1, toLoad, finput);
Yann Collet9e8b09a2016-04-07 19:35:23 +0200547 if (checkSize != toLoad) EXM_THROW(32, "Read error"); /* assumption : srcSize >= ZSTD_frameHeaderSize_max */
Yann Collet09b21ee2016-03-15 12:56:03 +0100548 }
549 readSize = ZSTD_frameHeaderSize_max;
Yann Collet4856a002015-01-24 01:58:16 +0100550
Yann Collet4856a002015-01-24 01:58:16 +0100551 /* Main decompression Loop */
Yann Collet2ce49232016-02-02 14:36:49 +0100552 while (1) {
Yann Collet88fcd292015-11-25 14:42:45 +0100553 /* Decode */
Yann Colletdeb078b2015-12-17 20:30:14 +0100554 size_t inSize=readSize, decodedSize=ress.dstBufferSize;
555 size_t toRead = ZBUFF_decompressContinue(ress.dctx, ress.dstBuffer, &decodedSize, ress.srcBuffer, &inSize);
Yann Collet88fcd292015-11-25 14:42:45 +0100556 if (ZBUFF_isError(toRead)) EXM_THROW(36, "Decoding error : %s", ZBUFF_getErrorName(toRead));
Yann Collet88fcd292015-11-25 14:42:45 +0100557 readSize -= inSize;
Yann Collet88fcd292015-11-25 14:42:45 +0100558
559 /* Write block */
Yann Collet09b21ee2016-03-15 12:56:03 +0100560 { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, decodedSize, foutput);
Yann Collet9e8b09a2016-04-07 19:35:23 +0200561 if (sizeCheck != decodedSize) EXM_THROW(37, "Write error : unable to write data block into destination"); }
Yann Collet88fcd292015-11-25 14:42:45 +0100562 frameSize += decodedSize;
563 DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(frameSize>>20) );
564
Yann Collet09b21ee2016-03-15 12:56:03 +0100565 if (toRead == 0) break; /* end of frame */
Yann Colletdeb078b2015-12-17 20:30:14 +0100566 if (readSize) EXM_THROW(38, "Decoding error : should consume entire input");
Yann Collet4856a002015-01-24 01:58:16 +0100567
568 /* Fill input buffer */
Yann Colletdeb078b2015-12-17 20:30:14 +0100569 if (toRead > ress.srcBufferSize) EXM_THROW(34, "too large block");
570 readSize = fread(ress.srcBuffer, 1, toRead, finput);
Yann Collet09b21ee2016-03-15 12:56:03 +0100571 if (readSize != toRead)
572 EXM_THROW(35, "Read error");
Yann Collet4856a002015-01-24 01:58:16 +0100573 }
574
Yann Collet88fcd292015-11-25 14:42:45 +0100575 return frameSize;
Yann Colletbe50aaa2015-09-10 23:26:09 +0100576}
577
578
Yann Collet1f1f2392016-02-12 18:33:26 +0100579/** FIO_decompressSrcFile() :
580 Decompression `srcFileName` into `ress.dstFile`
581 @return : 0 : OK
582 1 : operation not started
583*/
584static int FIO_decompressSrcFile(dRess_t ress, const char* srcFileName)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100585{
Yann Colletdeb078b2015-12-17 20:30:14 +0100586 unsigned long long filesize = 0;
Yann Colletf8494622016-05-07 22:43:40 +0200587 FILE* const dstFile = ress.dstFile;
588 FILE* const srcFile = FIO_openSrcFile(srcFileName);
Yann Collet1f1f2392016-02-12 18:33:26 +0100589 if (srcFile==0) return 1;
Yann Collet88fcd292015-11-25 14:42:45 +0100590
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100591 /* for each frame */
Yann Collet2ce49232016-02-02 14:36:49 +0100592 for ( ; ; ) {
Yann Colleta85a8dd2015-11-30 11:53:11 +0100593 /* check magic number -> version */
Yann Collet9e8b09a2016-04-07 19:35:23 +0200594 size_t const toRead = 4;
595 size_t const sizeCheck = fread(ress.srcBuffer, (size_t)1, toRead, srcFile);
Yann Colleta85a8dd2015-11-30 11:53:11 +0100596 if (sizeCheck==0) break; /* no more input */
Yann Collet1f1f2392016-02-12 18:33:26 +0100597 if (sizeCheck != toRead) EXM_THROW(31, "zstd: %s read error : cannot read header", srcFileName);
Yann Colletf8494622016-05-07 22:43:40 +0200598 { U32 const magic = MEM_readLE32(ress.srcBuffer);
599#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
600 if (ZSTD_isLegacy(magic)) {
601 filesize += FIO_decompressLegacyFrame(dstFile, srcFile, magic);
602 continue;
603 }
Yann Colletaa074052015-10-30 11:21:50 +0100604#endif /* ZSTD_LEGACY_SUPPORT */
Yann Colletf8494622016-05-07 22:43:40 +0200605 if (magic != ZSTD_MAGICNUMBER) {
606 DISPLAYLEVEL(1, "zstd: %s: not in zstd format \n", srcFileName);
607 return 1;
608 } }
Yann Colletdeb078b2015-12-17 20:30:14 +0100609 filesize += FIO_decompressFrame(ress, dstFile, srcFile, toRead);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100610 }
611
Yann Colletdeb078b2015-12-17 20:30:14 +0100612 /* Final Status */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100613 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Colletdeb078b2015-12-17 20:30:14 +0100614 DISPLAYLEVEL(2, "Successfully decoded %llu bytes \n", filesize);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100615
Yann Colletdeb078b2015-12-17 20:30:14 +0100616 /* Close */
617 fclose(srcFile);
Yann Collet1f1f2392016-02-12 18:33:26 +0100618 return 0;
619}
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100620
Yann Collet1f1f2392016-02-12 18:33:26 +0100621
622/** FIO_decompressFile_extRess() :
623 decompress `srcFileName` into `dstFileName`
624 @return : 0 : OK
625 1 : operation aborted (src not available, dst already taken, etc.)
626*/
627static int FIO_decompressFile_extRess(dRess_t ress,
628 const char* dstFileName, const char* srcFileName)
629{
Chip Turner6de382c2016-03-13 22:24:46 -0700630 int result;
Yann Collet1f1f2392016-02-12 18:33:26 +0100631 ress.dstFile = FIO_openDstFile(dstFileName);
632 if (ress.dstFile==0) return 1;
633
Chip Turner6de382c2016-03-13 22:24:46 -0700634 result = FIO_decompressSrcFile(ress, srcFileName);
635 if (result != 0) {
Chip Turner9da7f862016-03-14 07:44:59 -0700636 remove(dstFileName);
Chip Turner6de382c2016-03-13 22:24:46 -0700637 }
Yann Collet1f1f2392016-02-12 18:33:26 +0100638
639 if (fclose(ress.dstFile)) EXM_THROW(38, "Write error : cannot properly close %s", dstFileName);
Chip Turner6de382c2016-03-13 22:24:46 -0700640 return result;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100641}
642
643
Yann Colletdeb078b2015-12-17 20:30:14 +0100644int FIO_decompressFilename(const char* dstFileName, const char* srcFileName,
645 const char* dictFileName)
646{
647 int missingFiles = 0;
648 dRess_t ress = FIO_createDResources(dictFileName);
649
650 missingFiles += FIO_decompressFile_extRess(ress, dstFileName, srcFileName);
651
652 FIO_freeDResources(ress);
653 return missingFiles;
654}
655
656
657#define MAXSUFFIXSIZE 8
658int FIO_decompressMultipleFilenames(const char** srcNamesTable, unsigned nbFiles,
659 const char* suffix,
660 const char* dictFileName)
661{
Yann Colletdeb078b2015-12-17 20:30:14 +0100662 int skippedFiles = 0;
663 int missingFiles = 0;
Yann Colletf8494622016-05-07 22:43:40 +0200664 char* dstFileName = (char*)malloc(FNSPACE);
Yann Colletdeb078b2015-12-17 20:30:14 +0100665 size_t dfnSize = FNSPACE;
Yann Colletf8494622016-05-07 22:43:40 +0200666 size_t const suffixSize = suffix ? strlen(suffix) : 0;
Yann Colletdeb078b2015-12-17 20:30:14 +0100667 dRess_t ress;
668
669 if (dstFileName==NULL) EXM_THROW(70, "not enough memory for dstFileName");
670 ress = FIO_createDResources(dictFileName);
671
Yann Colletaccfd802016-02-15 19:33:16 +0100672 if (!strcmp(suffix, stdoutmark) || !strcmp(suffix, nulmark)) {
Yann Colletf8494622016-05-07 22:43:40 +0200673 unsigned u;
Yann Colletaccfd802016-02-15 19:33:16 +0100674 ress.dstFile = FIO_openDstFile(suffix);
675 if (ress.dstFile == 0) EXM_THROW(71, "cannot open %s", suffix);
676 for (u=0; u<nbFiles; u++)
677 missingFiles += FIO_decompressSrcFile(ress, srcNamesTable[u]);
678 if (fclose(ress.dstFile)) EXM_THROW(39, "Write error : cannot properly close %s", stdoutmark);
679 } else {
Yann Colletf8494622016-05-07 22:43:40 +0200680 unsigned u;
Yann Collet1f1f2392016-02-12 18:33:26 +0100681 for (u=0; u<nbFiles; u++) { /* create dstFileName */
682 const char* srcFileName = srcNamesTable[u];
683 size_t sfnSize = strlen(srcFileName);
684 const char* suffixPtr = srcFileName + sfnSize - suffixSize;
Yann Colletaccfd802016-02-15 19:33:16 +0100685 if (dfnSize+suffixSize <= sfnSize+1) {
686 free(dstFileName);
687 dfnSize = sfnSize + 20;
688 dstFileName = (char*)malloc(dfnSize);
689 if (dstFileName==NULL) EXM_THROW(71, "not enough memory for dstFileName");
690 }
691 if (sfnSize <= suffixSize || strcmp(suffixPtr, suffix) != 0) {
Yann Collet1f1f2392016-02-12 18:33:26 +0100692 DISPLAYLEVEL(1, "zstd: %s: unknown suffix (%4s expected) -- ignored \n", srcFileName, suffix);
693 skippedFiles++;
694 continue;
695 }
696 memcpy(dstFileName, srcFileName, sfnSize - suffixSize);
697 dstFileName[sfnSize-suffixSize] = '\0';
Yann Colletdeb078b2015-12-17 20:30:14 +0100698
Yann Collet1f1f2392016-02-12 18:33:26 +0100699 missingFiles += FIO_decompressFile_extRess(ress, dstFileName, srcFileName);
Yann Colletaccfd802016-02-15 19:33:16 +0100700 } }
Yann Colletdeb078b2015-12-17 20:30:14 +0100701
702 FIO_freeDResources(ress);
703 free(dstFileName);
704 return missingFiles + skippedFiles;
705}
Yann Colletaccfd802016-02-15 19:33:16 +0100706
inikepdb396432016-04-22 18:22:30 +0200707#endif // #ifndef ZSTD_NODECOMPRESS