blob: c599c476a26817d5d0e81a484ddab57db02a3aec [file] [log] [blame]
Yann Collet4856a002015-01-24 01:58:16 +01001/*
2 fileio.c - File i/o handler
3 Copyright (C) Yann Collet 2013-2015
4
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 :
22 - zstd source repository : https://github.com/Cyan4973/zstd
23 - Public forum : https://groups.google.com/forum/#!forum/lz4c
24*/
25/*
26 Note : this is stand-alone program.
27 It is not part of ZSTD compression library, it is a user program of ZSTD library.
28 The license of ZSTD library is BSD.
29 The license of this file is GPLv2.
30*/
31
Yann Colleteeb8ba12015-10-22 16:55:40 +010032/* *************************************
Yann Colletb1f3f4b2015-10-18 22:18:32 +010033* Tuning options
Yann Colleteeb8ba12015-10-22 16:55:40 +010034***************************************/
Yann Colletb1f3f4b2015-10-18 22:18:32 +010035#ifndef ZSTD_LEGACY_SUPPORT
36/**LEGACY_SUPPORT :
37* decompressor can decode older formats (starting from Zstd 0.1+) */
38# define ZSTD_LEGACY_SUPPORT 1
Yann Collet9f432922015-11-09 17:42:17 +010039#endif
Yann Colletb1f3f4b2015-10-18 22:18:32 +010040
41
Yann Colleteeb8ba12015-10-22 16:55:40 +010042/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010043* Compiler Options
Yann Colleteeb8ba12015-10-22 16:55:40 +010044***************************************/
Yann Collet4856a002015-01-24 01:58:16 +010045/* Disable some Visual warning messages */
46#ifdef _MSC_VER
47# define _CRT_SECURE_NO_WARNINGS
48# define _CRT_SECURE_NO_DEPRECATE /* VS2005 */
49# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
50#endif
51
52#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
53
54#define _FILE_OFFSET_BITS 64 /* Large file support on 32-bits unix */
55#define _POSIX_SOURCE 1 /* enable fileno() within <stdio.h> on unix */
56
57
Yann Colleteeb8ba12015-10-22 16:55:40 +010058/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010059* Includes
Yann Colleteeb8ba12015-10-22 16:55:40 +010060***************************************/
Yann Collet9f432922015-11-09 17:42:17 +010061#include <stdio.h> /* fprintf, fopen, fread, _fileno, stdin, stdout */
62#include <stdlib.h> /* malloc, free */
63#include <string.h> /* strcmp, strlen */
64#include <time.h> /* clock */
65#include <errno.h> /* errno */
66#include <sys/types.h> /* stat64 */
67#include <sys/stat.h> /* stat64 */
Yann Colletb1f3f4b2015-10-18 22:18:32 +010068#include "mem.h"
Yann Collet4856a002015-01-24 01:58:16 +010069#include "fileio.h"
70#include "zstd_static.h"
71
Yann Colletb1f3f4b2015-10-18 22:18:32 +010072#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
Yann Colletaa074052015-10-30 11:21:50 +010073# include "zstd_legacy.h" /* legacy */
74# include "fileio_legacy.h" /* legacy */
Yann Colleteeb8ba12015-10-22 16:55:40 +010075#endif
Yann Colletb1f3f4b2015-10-18 22:18:32 +010076
Yann Collet4856a002015-01-24 01:58:16 +010077
Yann Colleteeb8ba12015-10-22 16:55:40 +010078/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010079* OS-specific Includes
Yann Colleteeb8ba12015-10-22 16:55:40 +010080***************************************/
Yann Collet4856a002015-01-24 01:58:16 +010081#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
82# include <fcntl.h> /* _O_BINARY */
83# include <io.h> /* _setmode, _isatty */
84# ifdef __MINGW32__
Yann Collete1e6f7d2015-01-25 15:50:24 +010085 /* int _fileno(FILE *stream); // seems no longer useful // MINGW somehow forgets to include this windows declaration into <stdio.h> */
Yann Collet4856a002015-01-24 01:58:16 +010086# endif
Yann Colletb5e06dc2015-07-04 23:20:56 -080087# define SET_BINARY_MODE(file) { int unused = _setmode(_fileno(file), _O_BINARY); (void)unused; }
Yann Collet4856a002015-01-24 01:58:16 +010088# define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
89#else
90# include <unistd.h> /* isatty */
91# define SET_BINARY_MODE(file)
92# define IS_CONSOLE(stdStream) isatty(fileno(stdStream))
93#endif
94
Yann Colletb7d6e8f2015-11-09 18:20:39 +010095#if !defined(S_ISREG)
96# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
97#endif
98
Yann Collet4856a002015-01-24 01:58:16 +010099
Yann Colleteeb8ba12015-10-22 16:55:40 +0100100/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100101* Constants
Yann Colleteeb8ba12015-10-22 16:55:40 +0100102***************************************/
Yann Collet4856a002015-01-24 01:58:16 +0100103#define KB *(1U<<10)
104#define MB *(1U<<20)
105#define GB *(1U<<30)
106
107#define _1BIT 0x01
108#define _2BITS 0x03
109#define _3BITS 0x07
110#define _4BITS 0x0F
111#define _6BITS 0x3F
112#define _8BITS 0xFF
113
114#define BIT6 0x40
115#define BIT7 0x80
116
Yann Colletaacace32015-02-01 11:57:30 +0100117//static const unsigned FIO_maxBlockSizeID = 0xB; /* => 2MB block */
Yann Collet4856a002015-01-24 01:58:16 +0100118static const unsigned FIO_blockHeaderSize = 3;
119
120#define FIO_FRAMEHEADERSIZE 5 /* as a define, because needed to allocated table on stack */
121#define FSE_CHECKSUM_SEED 0
122
123#define CACHELINE 64
124
125
Yann Colleteeb8ba12015-10-22 16:55:40 +0100126/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100127* Macros
Yann Colleteeb8ba12015-10-22 16:55:40 +0100128***************************************/
Yann Collet4856a002015-01-24 01:58:16 +0100129#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
130#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
131static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
132
133#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
134 if ((FIO_GetMilliSpan(g_time) > refreshRate) || (g_displayLevel>=4)) \
135 { g_time = clock(); DISPLAY(__VA_ARGS__); \
136 if (g_displayLevel>=4) fflush(stdout); } }
137static const unsigned refreshRate = 150;
138static clock_t g_time = 0;
139
140
Yann Colleteeb8ba12015-10-22 16:55:40 +0100141/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100142* Local Parameters
Yann Colleteeb8ba12015-10-22 16:55:40 +0100143***************************************/
Yann Collet4856a002015-01-24 01:58:16 +0100144static U32 g_overwrite = 0;
145
146void FIO_overwriteMode(void) { g_overwrite=1; }
147void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; }
148
149
Yann Colleteeb8ba12015-10-22 16:55:40 +0100150/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100151* Exceptions
Yann Colleteeb8ba12015-10-22 16:55:40 +0100152***************************************/
153#ifndef DEBUG
154# define DEBUG 0
155#endif
Yann Collet4856a002015-01-24 01:58:16 +0100156#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
157#define EXM_THROW(error, ...) \
158{ \
159 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
160 DISPLAYLEVEL(1, "Error %i : ", error); \
161 DISPLAYLEVEL(1, __VA_ARGS__); \
162 DISPLAYLEVEL(1, "\n"); \
163 exit(error); \
164}
165
166
Yann Colleteeb8ba12015-10-22 16:55:40 +0100167/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100168* Functions
Yann Colleteeb8ba12015-10-22 16:55:40 +0100169***************************************/
Yann Collet4856a002015-01-24 01:58:16 +0100170static unsigned FIO_GetMilliSpan(clock_t nPrevious)
171{
172 clock_t nCurrent = clock();
173 unsigned nSpan = (unsigned)(((nCurrent - nPrevious) * 1000) / CLOCKS_PER_SEC);
174 return nSpan;
175}
176
177
178static void FIO_getFileHandles(FILE** pfinput, FILE** pfoutput, const char* input_filename, const char* output_filename)
179{
180 if (!strcmp (input_filename, stdinmark))
181 {
182 DISPLAYLEVEL(4,"Using stdin for input\n");
183 *pfinput = stdin;
184 SET_BINARY_MODE(stdin);
185 }
186 else
187 {
188 *pfinput = fopen(input_filename, "rb");
189 }
190
191 if (!strcmp (output_filename, stdoutmark))
192 {
193 DISPLAYLEVEL(4,"Using stdout for output\n");
194 *pfoutput = stdout;
195 SET_BINARY_MODE(stdout);
196 }
197 else
198 {
199 /* Check if destination file already exists */
200 *pfoutput=0;
201 if (strcmp(output_filename,nulmark)) *pfoutput = fopen( output_filename, "rb" );
202 if (*pfoutput!=0)
203 {
204 fclose(*pfoutput);
205 if (!g_overwrite)
206 {
207 char ch;
208 if (g_displayLevel <= 1) /* No interaction possible */
209 EXM_THROW(11, "Operation aborted : %s already exists", output_filename);
210 DISPLAYLEVEL(2, "Warning : %s already exists\n", output_filename);
211 DISPLAYLEVEL(2, "Overwrite ? (Y/N) : ");
212 ch = (char)getchar();
213 if ((ch!='Y') && (ch!='y')) EXM_THROW(11, "Operation aborted : %s already exists", output_filename);
214 }
215 }
216 *pfoutput = fopen( output_filename, "wb" );
217 }
218
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100219 if ( *pfinput==0 ) EXM_THROW(12, "Pb opening src : %s", input_filename);
220 if ( *pfoutput==0) EXM_THROW(13, "Pb opening dst : %s", output_filename);
Yann Collet4856a002015-01-24 01:58:16 +0100221}
222
Yann Collet9f432922015-11-09 17:42:17 +0100223
224static U64 FIO_getFileSize(const char* infilename)
225{
226 int r;
227#if defined(_MSC_VER)
228 struct _stat64 statbuf;
229 r = _stat64(infilename, &statbuf);
230#else
231 struct stat statbuf;
232 r = stat(infilename, &statbuf);
233#endif
234 if (r || !S_ISREG(statbuf.st_mode)) return 0;
235 return (U64)statbuf.st_size;
236}
237
238
Yann Collet2acb5d32015-10-29 16:49:43 +0100239unsigned long long FIO_compressFilename(const char* output_filename, const char* input_filename, int cLevel)
Yann Collet4856a002015-01-24 01:58:16 +0100240{
241 U64 filesize = 0;
242 U64 compressedfilesize = 0;
243 BYTE* inBuff;
244 BYTE* inSlot;
245 BYTE* inEnd;
246 BYTE* outBuff;
247 size_t blockSize = 128 KB;
248 size_t inBuffSize = 4 * blockSize;
249 size_t outBuffSize = ZSTD_compressBound(blockSize);
250 FILE* finput;
251 FILE* foutput;
252 size_t sizeCheck, cSize;
Yann Collet5be2dd22015-11-11 13:43:58 +0100253 ZSTD_CCtx* ctx;
Yann Collet4856a002015-01-24 01:58:16 +0100254
Yann Collet5be2dd22015-11-11 13:43:58 +0100255 /* init */
Yann Collet4856a002015-01-24 01:58:16 +0100256 FIO_getFileHandles(&finput, &foutput, input_filename, output_filename);
Yann Collet9f432922015-11-09 17:42:17 +0100257 filesize = FIO_getFileSize(input_filename);
Yann Collet4856a002015-01-24 01:58:16 +0100258
259 /* Allocate Memory */
Yann Collet5be2dd22015-11-11 13:43:58 +0100260 ctx = ZSTD_createCCtx();
Yann Collet213089c2015-06-18 07:43:16 -0800261 inBuff = (BYTE*)malloc(inBuffSize);
262 outBuff = (BYTE*)malloc(outBuffSize);
Yann Collet353c5d22015-10-21 14:39:26 +0100263 if (!inBuff || !outBuff || !ctx) EXM_THROW(21, "Allocation error : not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100264 inSlot = inBuff;
265 inEnd = inBuff + inBuffSize;
266
267 /* Write Frame Header */
Yann Collet5be2dd22015-11-11 13:43:58 +0100268 cSize = ZSTD_compressBegin(ctx, outBuff, outBuffSize, cLevel, filesize);
Yann Collet4856a002015-01-24 01:58:16 +0100269 if (ZSTD_isError(cSize)) EXM_THROW(22, "Compression error : cannot create frame header");
270
271 sizeCheck = fwrite(outBuff, 1, cSize, foutput);
Yann Collet61d08c52015-08-25 18:13:32 +0100272 if (sizeCheck!=cSize) EXM_THROW(23, "Write error : cannot write header into %s", output_filename);
Yann Collet4856a002015-01-24 01:58:16 +0100273 compressedfilesize += cSize;
Yann Collet9f432922015-11-09 17:42:17 +0100274 filesize = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100275
276 /* Main compression loop */
277 while (1)
278 {
279 size_t inSize;
280
281 /* Fill input Buffer */
282 if (inSlot + blockSize > inEnd) inSlot = inBuff;
283 inSize = fread(inSlot, (size_t)1, blockSize, finput);
284 if (inSize==0) break;
285 filesize += inSize;
Yann Colletd7233d62015-11-22 14:40:51 +0100286 DISPLAYUPDATE(2, "\rRead : %u MB ", (U32)(filesize>>20));
Yann Collet4856a002015-01-24 01:58:16 +0100287
288 /* Compress Block */
Yann Collet5be2dd22015-11-11 13:43:58 +0100289 cSize = ZSTD_compressContinue(ctx, outBuff, outBuffSize, inSlot, inSize);
Yann Collet4856a002015-01-24 01:58:16 +0100290 if (ZSTD_isError(cSize))
291 EXM_THROW(24, "Compression error : %s ", ZSTD_getErrorName(cSize));
292
293 /* Write cBlock */
294 sizeCheck = fwrite(outBuff, 1, cSize, foutput);
Yann Collet61d08c52015-08-25 18:13:32 +0100295 if (sizeCheck!=cSize) EXM_THROW(25, "Write error : cannot write compressed block into %s", output_filename);
Yann Collet4856a002015-01-24 01:58:16 +0100296 compressedfilesize += cSize;
297 inSlot += inSize;
298
299 DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", (U32)(filesize>>20), (double)compressedfilesize/filesize*100);
300 }
301
302 /* End of Frame */
Yann Collet5be2dd22015-11-11 13:43:58 +0100303 cSize = ZSTD_compressEnd(ctx, outBuff, outBuffSize);
Yann Collet4856a002015-01-24 01:58:16 +0100304 if (ZSTD_isError(cSize)) EXM_THROW(26, "Compression error : cannot create frame end");
305
306 sizeCheck = fwrite(outBuff, 1, cSize, foutput);
Yann Collet61d08c52015-08-25 18:13:32 +0100307 if (sizeCheck!=cSize) EXM_THROW(27, "Write error : cannot write frame end into %s", output_filename);
Yann Collet4856a002015-01-24 01:58:16 +0100308 compressedfilesize += cSize;
309
310 /* Status */
311 DISPLAYLEVEL(2, "\r%79s\r", "");
312 DISPLAYLEVEL(2,"Compressed %llu bytes into %llu bytes ==> %.2f%%\n",
313 (unsigned long long) filesize, (unsigned long long) compressedfilesize, (double)compressedfilesize/filesize*100);
314
315 /* clean */
316 free(inBuff);
317 free(outBuff);
Yann Collet5be2dd22015-11-11 13:43:58 +0100318 ZSTD_freeCCtx(ctx);
Yann Collet5b147602015-08-25 17:41:46 +0100319 fclose(finput);
Yann Collet61d08c52015-08-25 18:13:32 +0100320 if (fclose(foutput)) EXM_THROW(28, "Write error : cannot properly close %s", output_filename);
Yann Collet4856a002015-01-24 01:58:16 +0100321
322 return compressedfilesize;
323}
324
325
Yann Colletbe50aaa2015-09-10 23:26:09 +0100326unsigned long long FIO_decompressFrame(FILE* foutput, FILE* finput,
327 BYTE* inBuff, size_t inBuffSize,
328 BYTE* outBuff, size_t outBuffSize,
Yann Collet353c5d22015-10-21 14:39:26 +0100329 ZSTD_DCtx* dctx)
Yann Collet4856a002015-01-24 01:58:16 +0100330{
Yann Colletbe50aaa2015-09-10 23:26:09 +0100331 BYTE* op = outBuff;
332 BYTE* const oend = outBuff + outBuffSize;
Yann Collet4856a002015-01-24 01:58:16 +0100333 U64 filesize = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100334 size_t toRead;
335 size_t sizeCheck;
336
337
Yann Collet4856a002015-01-24 01:58:16 +0100338 /* Main decompression Loop */
Yann Colletc5d46b52015-02-16 18:06:26 +0100339 toRead = ZSTD_nextSrcSizeToDecompress(dctx);
Yann Collet4856a002015-01-24 01:58:16 +0100340 while (toRead)
341 {
342 size_t readSize, decodedSize;
343
344 /* Fill input buffer */
Yann Collet18850292015-08-24 20:17:11 +0100345 if (toRead > inBuffSize)
346 EXM_THROW(34, "too large block");
Yann Collet4856a002015-01-24 01:58:16 +0100347 readSize = fread(inBuff, 1, toRead, finput);
348 if (readSize != toRead)
Yann Collet18850292015-08-24 20:17:11 +0100349 EXM_THROW(35, "Read error");
Yann Collet4856a002015-01-24 01:58:16 +0100350
351 /* Decode block */
352 decodedSize = ZSTD_decompressContinue(dctx, op, oend-op, inBuff, readSize);
Yann Collet18850292015-08-24 20:17:11 +0100353 if (ZSTD_isError(decodedSize)) EXM_THROW(36, "Decoding error : input corrupted");
Yann Collet4856a002015-01-24 01:58:16 +0100354
Yann Colletc5d46b52015-02-16 18:06:26 +0100355 if (decodedSize) /* not a header */
356 {
357 /* Write block */
358 sizeCheck = fwrite(op, 1, decodedSize, foutput);
Yann Collet18850292015-08-24 20:17:11 +0100359 if (sizeCheck != decodedSize) EXM_THROW(37, "Write error : unable to write data block to destination file");
Yann Colletc5d46b52015-02-16 18:06:26 +0100360 filesize += decodedSize;
361 op += decodedSize;
362 if (op==oend) op = outBuff;
363 DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(filesize>>20) );
364 }
Yann Collet4856a002015-01-24 01:58:16 +0100365
366 /* prepare for next Block */
Yann Colletc5d46b52015-02-16 18:06:26 +0100367 toRead = ZSTD_nextSrcSizeToDecompress(dctx);
Yann Collet4856a002015-01-24 01:58:16 +0100368 }
369
Yann Colletbe50aaa2015-09-10 23:26:09 +0100370 return filesize;
371}
372
373
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100374#define MAXHEADERSIZE (FIO_FRAMEHEADERSIZE+3)
375unsigned long long FIO_decompressFilename(const char* output_filename, const char* input_filename)
376{
377 FILE* finput, *foutput;
378 BYTE* inBuff=NULL;
379 size_t inBuffSize = 0;
380 BYTE* outBuff=NULL;
381 size_t outBuffSize = 0;
382 U32 blockSize = 128 KB;
383 U32 wNbBlocks = 4;
384 U64 filesize = 0;
385 BYTE* header[MAXHEADERSIZE];
386 size_t toRead;
387 size_t sizeCheck;
388
389
390 /* Init */
Yann Collet353c5d22015-10-21 14:39:26 +0100391 ZSTD_DCtx* dctx = ZSTD_createDCtx();
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100392 FIO_getFileHandles(&finput, &foutput, input_filename, output_filename);
393
394 /* for each frame */
395 for ( ; ; )
396 {
397 /* check magic number -> version */
398 U32 magicNumber;
399 toRead = sizeof(ZSTD_magicNumber);;
400 sizeCheck = fread(header, (size_t)1, toRead, finput);
401 if (sizeCheck==0) break; /* no more input */
402 if (sizeCheck != toRead) EXM_THROW(31, "Read error : cannot read header");
403
404 magicNumber = MEM_readLE32(header);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100405#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
Yann Colletaa074052015-10-30 11:21:50 +0100406 if (ZSTD_isLegacy(magicNumber))
407 {
408 filesize += FIO_decompressLegacyFrame(foutput, finput, magicNumber);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100409 continue;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100410 }
Yann Colletaa074052015-10-30 11:21:50 +0100411#endif /* ZSTD_LEGACY_SUPPORT */
412 if (magicNumber != ZSTD_magicNumber) EXM_THROW(32, "Error : unknown frame prefix");
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100413
414 /* prepare frame decompression, by completing header */
415 ZSTD_resetDCtx(dctx);
416 toRead = ZSTD_nextSrcSizeToDecompress(dctx) - sizeof(ZSTD_magicNumber);
417 if (toRead > MAXHEADERSIZE) EXM_THROW(30, "Not enough memory to read header");
Yann Collet4114f952015-10-30 06:40:22 +0100418 sizeCheck = fread(&header[sizeof(ZSTD_magicNumber)], 1, toRead, finput);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100419 if (sizeCheck != toRead) EXM_THROW(31, "Read error : cannot read header");
420 sizeCheck = ZSTD_decompressContinue(dctx, NULL, 0, header, sizeof(ZSTD_magicNumber)+toRead); // Decode frame header
421 if (ZSTD_isError(sizeCheck)) EXM_THROW(32, "Error decoding header");
422
423 /* Here later : blockSize determination */
424
425 /* Allocate Memory (if needed) */
426 {
427 size_t newInBuffSize = blockSize + FIO_blockHeaderSize;
428 size_t newOutBuffSize = wNbBlocks * blockSize;
429 if (newInBuffSize > inBuffSize)
430 {
431 free(inBuff);
432 inBuffSize = newInBuffSize;
433 inBuff = (BYTE*)malloc(inBuffSize);
434 }
435 if (newOutBuffSize > outBuffSize)
436 {
437 free(outBuff);
438 outBuffSize = newOutBuffSize;
439 outBuff = (BYTE*)malloc(outBuffSize);
440 }
441 }
442 if (!inBuff || !outBuff) EXM_THROW(33, "Allocation error : not enough memory");
443
444 filesize += FIO_decompressFrame(foutput, finput, inBuff, inBuffSize, outBuff, outBuffSize, dctx);
445 }
446
447 DISPLAYLEVEL(2, "\r%79s\r", "");
448 DISPLAYLEVEL(2, "Decoded %llu bytes \n", (long long unsigned)filesize);
449
450 /* clean */
451 free(inBuff);
452 free(outBuff);
453 ZSTD_freeDCtx(dctx);
454 fclose(finput);
455 if (fclose(foutput)) EXM_THROW(38, "Write error : cannot properly close %s", output_filename);
456
457 return filesize;
458}
459
460
461#if 0
Yann Colletbe50aaa2015-09-10 23:26:09 +0100462unsigned long long FIO_decompressFilename(const char* output_filename, const char* input_filename)
463{
464 FILE* finput, *foutput;
465 BYTE* inBuff=NULL;
466 size_t inBuffSize = 0;
467 BYTE* outBuff=NULL;
468 size_t outBuffSize = 0;
469 U32 blockSize = 128 KB;
470 U32 wNbBlocks = 4;
471 U64 filesize = 0;
472 BYTE* header[MAXHEADERSIZE];
473 ZSTD_Dctx* dctx;
474 size_t toRead;
475 size_t sizeCheck;
476
477
478 /* Init */
479 FIO_getFileHandles(&finput, &foutput, input_filename, output_filename);
480 dctx = ZSTD_createDCtx();
481
482 /* for each frame */
483 for ( ; ; )
484 {
485 /* check header */
486 ZSTD_resetDCtx(dctx);
487 toRead = ZSTD_nextSrcSizeToDecompress(dctx);
488 if (toRead > MAXHEADERSIZE) EXM_THROW(30, "Not enough memory to read header");
489 sizeCheck = fread(header, (size_t)1, toRead, finput);
490 if (sizeCheck==0) break; /* no more input */
491 if (sizeCheck != toRead) EXM_THROW(31, "Read error : cannot read header");
492 sizeCheck = ZSTD_decompressContinue(dctx, NULL, 0, header, toRead); // Decode frame header
493 if (ZSTD_isError(sizeCheck)) EXM_THROW(32, "Error decoding header");
494
495 /* Here later : blockSize determination */
496
497 /* Allocate Memory (if needed) */
498 {
499 size_t newInBuffSize = blockSize + FIO_blockHeaderSize;
500 size_t newOutBuffSize = wNbBlocks * blockSize;
501 if (newInBuffSize > inBuffSize)
502 {
503 free(inBuff);
504 inBuffSize = newInBuffSize;
505 inBuff = (BYTE*)malloc(inBuffSize);
506 }
507 if (newOutBuffSize > outBuffSize)
508 {
509 free(outBuff);
510 outBuffSize = newOutBuffSize;
511 outBuff = (BYTE*)malloc(outBuffSize);
512 }
513 }
514 if (!inBuff || !outBuff) EXM_THROW(33, "Allocation error : not enough memory");
515
516 filesize += FIO_decompressFrame(foutput, finput, inBuff, inBuffSize, outBuff, outBuffSize, dctx);
517 }
518
Yann Collet4856a002015-01-24 01:58:16 +0100519 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100520 DISPLAYLEVEL(2, "Decoded %llu bytes \n", (long long unsigned)filesize);
Yann Collet4856a002015-01-24 01:58:16 +0100521
522 /* clean */
523 free(inBuff);
524 free(outBuff);
Yann Collet4856a002015-01-24 01:58:16 +0100525 ZSTD_freeDCtx(dctx);
Yann Collet5abd8202015-08-27 03:16:04 +0100526 fclose(finput);
527 if (fclose(foutput)) EXM_THROW(38, "Write error : cannot properly close %s", output_filename);
Yann Collet4856a002015-01-24 01:58:16 +0100528
529 return filesize;
530}
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100531#endif