blob: 315a38c37e1632318f49a304ff01711b338aa53d [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
39#endif // ZSTD_LEGACY_SUPPORT
40
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 Colletb1f3f4b2015-10-18 22:18:32 +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 "mem.h"
Yann Collet4856a002015-01-24 01:58:16 +010067#include "fileio.h"
68#include "zstd_static.h"
Yann Collet2acb5d32015-10-29 16:49:43 +010069#include "zstdhc_static.h"
Yann Collet4856a002015-01-24 01:58:16 +010070
Yann Colletb1f3f4b2015-10-18 22:18:32 +010071#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
72# include "zstd_v01.h" /* legacy */
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 Colleteeb8ba12015-10-22 16:55:40 +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 */
82# ifdef __MINGW32__
Yann Collete1e6f7d2015-01-25 15:50:24 +010083 /* 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 +010084# endif
Yann Colletb5e06dc2015-07-04 23:20:56 -080085# define SET_BINARY_MODE(file) { int unused = _setmode(_fileno(file), _O_BINARY); (void)unused; }
Yann Collet4856a002015-01-24 01:58:16 +010086# define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
87#else
88# include <unistd.h> /* isatty */
89# define SET_BINARY_MODE(file)
90# define IS_CONSOLE(stdStream) isatty(fileno(stdStream))
91#endif
92
93
Yann Colleteeb8ba12015-10-22 16:55:40 +010094/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010095* Constants
Yann Colleteeb8ba12015-10-22 16:55:40 +010096***************************************/
Yann Collet4856a002015-01-24 01:58:16 +010097#define KB *(1U<<10)
98#define MB *(1U<<20)
99#define GB *(1U<<30)
100
101#define _1BIT 0x01
102#define _2BITS 0x03
103#define _3BITS 0x07
104#define _4BITS 0x0F
105#define _6BITS 0x3F
106#define _8BITS 0xFF
107
108#define BIT6 0x40
109#define BIT7 0x80
110
Yann Colletaacace32015-02-01 11:57:30 +0100111//static const unsigned FIO_maxBlockSizeID = 0xB; /* => 2MB block */
Yann Collet4856a002015-01-24 01:58:16 +0100112static const unsigned FIO_blockHeaderSize = 3;
113
114#define FIO_FRAMEHEADERSIZE 5 /* as a define, because needed to allocated table on stack */
115#define FSE_CHECKSUM_SEED 0
116
117#define CACHELINE 64
118
119
Yann Colleteeb8ba12015-10-22 16:55:40 +0100120/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100121* Macros
Yann Colleteeb8ba12015-10-22 16:55:40 +0100122***************************************/
Yann Collet4856a002015-01-24 01:58:16 +0100123#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
124#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
125static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
126
127#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
128 if ((FIO_GetMilliSpan(g_time) > refreshRate) || (g_displayLevel>=4)) \
129 { g_time = clock(); DISPLAY(__VA_ARGS__); \
130 if (g_displayLevel>=4) fflush(stdout); } }
131static const unsigned refreshRate = 150;
132static clock_t g_time = 0;
133
134
Yann Colleteeb8ba12015-10-22 16:55:40 +0100135/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100136* Local Parameters
Yann Colleteeb8ba12015-10-22 16:55:40 +0100137***************************************/
Yann Collet4856a002015-01-24 01:58:16 +0100138static U32 g_overwrite = 0;
139
140void FIO_overwriteMode(void) { g_overwrite=1; }
141void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; }
142
143
Yann Colleteeb8ba12015-10-22 16:55:40 +0100144/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100145* Exceptions
Yann Colleteeb8ba12015-10-22 16:55:40 +0100146***************************************/
147#ifndef DEBUG
148# define DEBUG 0
149#endif
Yann Collet4856a002015-01-24 01:58:16 +0100150#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
151#define EXM_THROW(error, ...) \
152{ \
153 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
154 DISPLAYLEVEL(1, "Error %i : ", error); \
155 DISPLAYLEVEL(1, __VA_ARGS__); \
156 DISPLAYLEVEL(1, "\n"); \
157 exit(error); \
158}
159
160
Yann Colleteeb8ba12015-10-22 16:55:40 +0100161/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100162* Functions
Yann Colleteeb8ba12015-10-22 16:55:40 +0100163***************************************/
Yann Collet4856a002015-01-24 01:58:16 +0100164static unsigned FIO_GetMilliSpan(clock_t nPrevious)
165{
166 clock_t nCurrent = clock();
167 unsigned nSpan = (unsigned)(((nCurrent - nPrevious) * 1000) / CLOCKS_PER_SEC);
168 return nSpan;
169}
170
171
172static void FIO_getFileHandles(FILE** pfinput, FILE** pfoutput, const char* input_filename, const char* output_filename)
173{
174 if (!strcmp (input_filename, stdinmark))
175 {
176 DISPLAYLEVEL(4,"Using stdin for input\n");
177 *pfinput = stdin;
178 SET_BINARY_MODE(stdin);
179 }
180 else
181 {
182 *pfinput = fopen(input_filename, "rb");
183 }
184
185 if (!strcmp (output_filename, stdoutmark))
186 {
187 DISPLAYLEVEL(4,"Using stdout for output\n");
188 *pfoutput = stdout;
189 SET_BINARY_MODE(stdout);
190 }
191 else
192 {
193 /* Check if destination file already exists */
194 *pfoutput=0;
195 if (strcmp(output_filename,nulmark)) *pfoutput = fopen( output_filename, "rb" );
196 if (*pfoutput!=0)
197 {
198 fclose(*pfoutput);
199 if (!g_overwrite)
200 {
201 char ch;
202 if (g_displayLevel <= 1) /* No interaction possible */
203 EXM_THROW(11, "Operation aborted : %s already exists", output_filename);
204 DISPLAYLEVEL(2, "Warning : %s already exists\n", output_filename);
205 DISPLAYLEVEL(2, "Overwrite ? (Y/N) : ");
206 ch = (char)getchar();
207 if ((ch!='Y') && (ch!='y')) EXM_THROW(11, "Operation aborted : %s already exists", output_filename);
208 }
209 }
210 *pfoutput = fopen( output_filename, "wb" );
211 }
212
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100213 if ( *pfinput==0 ) EXM_THROW(12, "Pb opening src : %s", input_filename);
214 if ( *pfoutput==0) EXM_THROW(13, "Pb opening dst : %s", output_filename);
Yann Collet4856a002015-01-24 01:58:16 +0100215}
216
Yann Collet2acb5d32015-10-29 16:49:43 +0100217typedef void* (*FIO_createC) (void);
218static void* local_ZSTD_createCCtx(void) { return (void*) ZSTD_createCCtx(); }
219static void* local_ZSTD_HC_createCCtx(void) { return (void*) ZSTD_HC_createCCtx(); }
Yann Collet4856a002015-01-24 01:58:16 +0100220
Yann Collet2acb5d32015-10-29 16:49:43 +0100221typedef size_t (*FIO_initC) (void* ctx, void* dst, size_t maxDstSize, int cLevel);
222static size_t local_ZSTD_compressBegin (void* ctx, void* dst, size_t maxDstSize, int cLevel)
223{
224 (void)cLevel;
225 return ZSTD_compressBegin((ZSTD_CCtx*)ctx, dst, maxDstSize);
226}
227static size_t local_ZSTD_HC_compressBegin (void* ctx, void* dst, size_t maxDstSize, int cLevel)
228{
229 return ZSTD_HC_compressBegin((ZSTD_HC_CCtx*)ctx, dst, maxDstSize, cLevel);
230}
231
232typedef size_t (*FIO_continueC) (void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);
233static size_t local_ZSTD_compressContinue (void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
234{
235 return ZSTD_compressContinue((ZSTD_CCtx*)ctx, dst, maxDstSize, src, srcSize);
236}
237static size_t local_ZSTD_HC_compressContinue (void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
238{
239 return ZSTD_HC_compressContinue((ZSTD_HC_CCtx*)ctx, dst, maxDstSize, src, srcSize);
240}
241
242typedef size_t (*FIO_endC) (void* ctx, void* dst, size_t maxDstSize);
243static size_t local_ZSTD_compressEnd (void* ctx, void* dst, size_t maxDstSize)
244{
245 return ZSTD_compressEnd((ZSTD_CCtx*)ctx, dst, maxDstSize);
246}
247static size_t local_ZSTD_HC_compressEnd (void* ctx, void* dst, size_t maxDstSize)
248{
249 return ZSTD_HC_compressEnd((ZSTD_HC_CCtx*)ctx, dst, maxDstSize);
250}
251
252typedef void (*FIO_freeC) (void* ctx);
253static void local_ZSTD_freeCCtx(void* ctx) { ZSTD_freeCCtx((ZSTD_CCtx*)ctx); }
254static void local_ZSTD_HC_freeCCtx(void* ctx) { ZSTD_HC_freeCCtx((ZSTD_HC_CCtx*)ctx); }
255
256
257unsigned long long FIO_compressFilename(const char* output_filename, const char* input_filename, int cLevel)
Yann Collet4856a002015-01-24 01:58:16 +0100258{
259 U64 filesize = 0;
260 U64 compressedfilesize = 0;
261 BYTE* inBuff;
262 BYTE* inSlot;
263 BYTE* inEnd;
264 BYTE* outBuff;
265 size_t blockSize = 128 KB;
266 size_t inBuffSize = 4 * blockSize;
267 size_t outBuffSize = ZSTD_compressBound(blockSize);
268 FILE* finput;
269 FILE* foutput;
270 size_t sizeCheck, cSize;
Yann Collet2acb5d32015-10-29 16:49:43 +0100271 void* ctx;
272 FIO_createC createC=NULL;
273 FIO_initC initC=NULL;
274 FIO_continueC continueC = NULL;
275 FIO_endC endC = NULL;
276 FIO_freeC freeC = NULL;
Yann Collet4856a002015-01-24 01:58:16 +0100277
278 /* Init */
Yann Collet2acb5d32015-10-29 16:49:43 +0100279 if (cLevel <= 1)
280 {
281 createC = local_ZSTD_createCCtx;
282 initC = local_ZSTD_compressBegin;
283 continueC = local_ZSTD_compressContinue;
284 endC = local_ZSTD_compressEnd;
285 freeC = local_ZSTD_freeCCtx;
286 }
287 else
288 {
289 createC = local_ZSTD_HC_createCCtx;
290 initC = local_ZSTD_HC_compressBegin;
291 continueC = local_ZSTD_HC_compressContinue;
292 endC = local_ZSTD_HC_compressEnd;
293 freeC = local_ZSTD_HC_freeCCtx;
294 }
Yann Collet4856a002015-01-24 01:58:16 +0100295 FIO_getFileHandles(&finput, &foutput, input_filename, output_filename);
Yann Collet4856a002015-01-24 01:58:16 +0100296
297 /* Allocate Memory */
Yann Collet2acb5d32015-10-29 16:49:43 +0100298 ctx = createC();
Yann Collet213089c2015-06-18 07:43:16 -0800299 inBuff = (BYTE*)malloc(inBuffSize);
300 outBuff = (BYTE*)malloc(outBuffSize);
Yann Collet353c5d22015-10-21 14:39:26 +0100301 if (!inBuff || !outBuff || !ctx) EXM_THROW(21, "Allocation error : not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100302 inSlot = inBuff;
303 inEnd = inBuff + inBuffSize;
304
305 /* Write Frame Header */
Yann Collet2acb5d32015-10-29 16:49:43 +0100306 cSize = initC(ctx, outBuff, outBuffSize, cLevel);
Yann Collet4856a002015-01-24 01:58:16 +0100307 if (ZSTD_isError(cSize)) EXM_THROW(22, "Compression error : cannot create frame header");
308
309 sizeCheck = fwrite(outBuff, 1, cSize, foutput);
Yann Collet61d08c52015-08-25 18:13:32 +0100310 if (sizeCheck!=cSize) EXM_THROW(23, "Write error : cannot write header into %s", output_filename);
Yann Collet4856a002015-01-24 01:58:16 +0100311 compressedfilesize += cSize;
312
313 /* Main compression loop */
314 while (1)
315 {
316 size_t inSize;
317
318 /* Fill input Buffer */
319 if (inSlot + blockSize > inEnd) inSlot = inBuff;
320 inSize = fread(inSlot, (size_t)1, blockSize, finput);
321 if (inSize==0) break;
322 filesize += inSize;
323 DISPLAYUPDATE(2, "\rRead : %u MB ", (U32)(filesize>>20));
324
325 /* Compress Block */
Yann Collet2acb5d32015-10-29 16:49:43 +0100326 cSize = continueC(ctx, outBuff, outBuffSize, inSlot, inSize);
Yann Collet4856a002015-01-24 01:58:16 +0100327 if (ZSTD_isError(cSize))
328 EXM_THROW(24, "Compression error : %s ", ZSTD_getErrorName(cSize));
329
330 /* Write cBlock */
331 sizeCheck = fwrite(outBuff, 1, cSize, foutput);
Yann Collet61d08c52015-08-25 18:13:32 +0100332 if (sizeCheck!=cSize) EXM_THROW(25, "Write error : cannot write compressed block into %s", output_filename);
Yann Collet4856a002015-01-24 01:58:16 +0100333 compressedfilesize += cSize;
334 inSlot += inSize;
335
336 DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", (U32)(filesize>>20), (double)compressedfilesize/filesize*100);
337 }
338
339 /* End of Frame */
Yann Collet2acb5d32015-10-29 16:49:43 +0100340 cSize = endC(ctx, outBuff, outBuffSize);
Yann Collet4856a002015-01-24 01:58:16 +0100341 if (ZSTD_isError(cSize)) EXM_THROW(26, "Compression error : cannot create frame end");
342
343 sizeCheck = fwrite(outBuff, 1, cSize, foutput);
Yann Collet61d08c52015-08-25 18:13:32 +0100344 if (sizeCheck!=cSize) EXM_THROW(27, "Write error : cannot write frame end into %s", output_filename);
Yann Collet4856a002015-01-24 01:58:16 +0100345 compressedfilesize += cSize;
346
347 /* Status */
348 DISPLAYLEVEL(2, "\r%79s\r", "");
349 DISPLAYLEVEL(2,"Compressed %llu bytes into %llu bytes ==> %.2f%%\n",
350 (unsigned long long) filesize, (unsigned long long) compressedfilesize, (double)compressedfilesize/filesize*100);
351
352 /* clean */
353 free(inBuff);
354 free(outBuff);
Yann Collet2acb5d32015-10-29 16:49:43 +0100355 freeC(ctx);
Yann Collet5b147602015-08-25 17:41:46 +0100356 fclose(finput);
Yann Collet61d08c52015-08-25 18:13:32 +0100357 if (fclose(foutput)) EXM_THROW(28, "Write error : cannot properly close %s", output_filename);
Yann Collet4856a002015-01-24 01:58:16 +0100358
359 return compressedfilesize;
360}
361
362
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100363#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
364
365unsigned long long FIOv01_decompressFrame(FILE* foutput, FILE* finput)
366{
367 size_t outBuffSize = 512 KB;
368 BYTE* outBuff = (BYTE*)malloc(outBuffSize);
369 size_t inBuffSize = 128 KB + 8;
370 BYTE inBuff[128 KB + 8];
371 BYTE* op = outBuff;
372 BYTE* const oend = outBuff + outBuffSize;
373 U64 filesize = 0;
374 size_t toRead;
375 size_t sizeCheck;
376 ZSTDv01_Dctx* dctx = ZSTDv01_createDCtx();
377
378
379 /* init */
380 if (outBuff==NULL) EXM_THROW(41, "Error : not enough memory to decode legacy frame");
381
382 /* restore header, already read from input */
383 MEM_writeLE32(inBuff, ZSTDv01_magicNumberLE);
384 sizeCheck = ZSTDv01_decompressContinue(dctx, NULL, 0, inBuff, sizeof(ZSTDv01_magicNumberLE)); /* Decode frame header */
385 if (ZSTDv01_isError(sizeCheck)) EXM_THROW(42, "Error decoding legacy header");
386
387 /* Main decompression Loop */
388 toRead = ZSTDv01_nextSrcSizeToDecompress(dctx);
389 while (toRead)
390 {
391 size_t readSize, decodedSize;
392
393 /* Fill input buffer */
394 if (toRead > inBuffSize)
395 EXM_THROW(43, "too large block");
396 readSize = fread(inBuff, 1, toRead, finput);
397 if (readSize != toRead)
398 EXM_THROW(44, "Read error");
399
400 /* Decode block */
401 decodedSize = ZSTDv01_decompressContinue(dctx, op, oend-op, inBuff, readSize);
402 if (ZSTDv01_isError(decodedSize)) EXM_THROW(45, "Decoding error : input corrupted");
403
404 if (decodedSize) /* not a header */
405 {
406 /* Write block */
407 sizeCheck = fwrite(op, 1, decodedSize, foutput);
408 if (sizeCheck != decodedSize) EXM_THROW(46, "Write error : unable to write data block to destination file");
409 filesize += decodedSize;
410 op += decodedSize;
411 if (op==oend) op = outBuff;
412 DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(filesize>>20) );
413 }
414
415 /* prepare for next Block */
416 toRead = ZSTDv01_nextSrcSizeToDecompress(dctx);
417 }
418
419 /* release resources */
420 free(outBuff);
421 free(dctx);
422 return filesize;
423}
424#endif /* ZSTD_LEGACY_SUPPORT */
425
426
Yann Colletbe50aaa2015-09-10 23:26:09 +0100427unsigned long long FIO_decompressFrame(FILE* foutput, FILE* finput,
428 BYTE* inBuff, size_t inBuffSize,
429 BYTE* outBuff, size_t outBuffSize,
Yann Collet353c5d22015-10-21 14:39:26 +0100430 ZSTD_DCtx* dctx)
Yann Collet4856a002015-01-24 01:58:16 +0100431{
Yann Colletbe50aaa2015-09-10 23:26:09 +0100432 BYTE* op = outBuff;
433 BYTE* const oend = outBuff + outBuffSize;
Yann Collet4856a002015-01-24 01:58:16 +0100434 U64 filesize = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100435 size_t toRead;
436 size_t sizeCheck;
437
438
Yann Collet4856a002015-01-24 01:58:16 +0100439 /* Main decompression Loop */
Yann Colletc5d46b52015-02-16 18:06:26 +0100440 toRead = ZSTD_nextSrcSizeToDecompress(dctx);
Yann Collet4856a002015-01-24 01:58:16 +0100441 while (toRead)
442 {
443 size_t readSize, decodedSize;
444
445 /* Fill input buffer */
Yann Collet18850292015-08-24 20:17:11 +0100446 if (toRead > inBuffSize)
447 EXM_THROW(34, "too large block");
Yann Collet4856a002015-01-24 01:58:16 +0100448 readSize = fread(inBuff, 1, toRead, finput);
449 if (readSize != toRead)
Yann Collet18850292015-08-24 20:17:11 +0100450 EXM_THROW(35, "Read error");
Yann Collet4856a002015-01-24 01:58:16 +0100451
452 /* Decode block */
453 decodedSize = ZSTD_decompressContinue(dctx, op, oend-op, inBuff, readSize);
Yann Collet18850292015-08-24 20:17:11 +0100454 if (ZSTD_isError(decodedSize)) EXM_THROW(36, "Decoding error : input corrupted");
Yann Collet4856a002015-01-24 01:58:16 +0100455
Yann Colletc5d46b52015-02-16 18:06:26 +0100456 if (decodedSize) /* not a header */
457 {
458 /* Write block */
459 sizeCheck = fwrite(op, 1, decodedSize, foutput);
Yann Collet18850292015-08-24 20:17:11 +0100460 if (sizeCheck != decodedSize) EXM_THROW(37, "Write error : unable to write data block to destination file");
Yann Colletc5d46b52015-02-16 18:06:26 +0100461 filesize += decodedSize;
462 op += decodedSize;
463 if (op==oend) op = outBuff;
464 DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(filesize>>20) );
465 }
Yann Collet4856a002015-01-24 01:58:16 +0100466
467 /* prepare for next Block */
Yann Colletc5d46b52015-02-16 18:06:26 +0100468 toRead = ZSTD_nextSrcSizeToDecompress(dctx);
Yann Collet4856a002015-01-24 01:58:16 +0100469 }
470
Yann Colletbe50aaa2015-09-10 23:26:09 +0100471 return filesize;
472}
473
474
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100475#define MAXHEADERSIZE (FIO_FRAMEHEADERSIZE+3)
476unsigned long long FIO_decompressFilename(const char* output_filename, const char* input_filename)
477{
478 FILE* finput, *foutput;
479 BYTE* inBuff=NULL;
480 size_t inBuffSize = 0;
481 BYTE* outBuff=NULL;
482 size_t outBuffSize = 0;
483 U32 blockSize = 128 KB;
484 U32 wNbBlocks = 4;
485 U64 filesize = 0;
486 BYTE* header[MAXHEADERSIZE];
487 size_t toRead;
488 size_t sizeCheck;
489
490
491 /* Init */
Yann Collet353c5d22015-10-21 14:39:26 +0100492 ZSTD_DCtx* dctx = ZSTD_createDCtx();
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100493 FIO_getFileHandles(&finput, &foutput, input_filename, output_filename);
494
495 /* for each frame */
496 for ( ; ; )
497 {
498 /* check magic number -> version */
499 U32 magicNumber;
500 toRead = sizeof(ZSTD_magicNumber);;
501 sizeCheck = fread(header, (size_t)1, toRead, finput);
502 if (sizeCheck==0) break; /* no more input */
503 if (sizeCheck != toRead) EXM_THROW(31, "Read error : cannot read header");
504
505 magicNumber = MEM_readLE32(header);
506 switch(magicNumber)
507 {
508#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
509 case ZSTDv01_magicNumberLE:
510 filesize += FIOv01_decompressFrame(foutput, finput);
511 continue;
512#endif /* ZSTD_LEGACY_SUPPORT */
513 case ZSTD_magicNumber:
514 break; /* normal case */
515 default :
516 EXM_THROW(32, "Error : unknown frame prefix");
517 }
518
519 /* prepare frame decompression, by completing header */
520 ZSTD_resetDCtx(dctx);
521 toRead = ZSTD_nextSrcSizeToDecompress(dctx) - sizeof(ZSTD_magicNumber);
522 if (toRead > MAXHEADERSIZE) EXM_THROW(30, "Not enough memory to read header");
523 sizeCheck = fread(header+sizeof(ZSTD_magicNumber), (size_t)1, toRead, finput);
524 if (sizeCheck != toRead) EXM_THROW(31, "Read error : cannot read header");
525 sizeCheck = ZSTD_decompressContinue(dctx, NULL, 0, header, sizeof(ZSTD_magicNumber)+toRead); // Decode frame header
526 if (ZSTD_isError(sizeCheck)) EXM_THROW(32, "Error decoding header");
527
528 /* Here later : blockSize determination */
529
530 /* Allocate Memory (if needed) */
531 {
532 size_t newInBuffSize = blockSize + FIO_blockHeaderSize;
533 size_t newOutBuffSize = wNbBlocks * blockSize;
534 if (newInBuffSize > inBuffSize)
535 {
536 free(inBuff);
537 inBuffSize = newInBuffSize;
538 inBuff = (BYTE*)malloc(inBuffSize);
539 }
540 if (newOutBuffSize > outBuffSize)
541 {
542 free(outBuff);
543 outBuffSize = newOutBuffSize;
544 outBuff = (BYTE*)malloc(outBuffSize);
545 }
546 }
547 if (!inBuff || !outBuff) EXM_THROW(33, "Allocation error : not enough memory");
548
549 filesize += FIO_decompressFrame(foutput, finput, inBuff, inBuffSize, outBuff, outBuffSize, dctx);
550 }
551
552 DISPLAYLEVEL(2, "\r%79s\r", "");
553 DISPLAYLEVEL(2, "Decoded %llu bytes \n", (long long unsigned)filesize);
554
555 /* clean */
556 free(inBuff);
557 free(outBuff);
558 ZSTD_freeDCtx(dctx);
559 fclose(finput);
560 if (fclose(foutput)) EXM_THROW(38, "Write error : cannot properly close %s", output_filename);
561
562 return filesize;
563}
564
565
566#if 0
Yann Colletbe50aaa2015-09-10 23:26:09 +0100567unsigned long long FIO_decompressFilename(const char* output_filename, const char* input_filename)
568{
569 FILE* finput, *foutput;
570 BYTE* inBuff=NULL;
571 size_t inBuffSize = 0;
572 BYTE* outBuff=NULL;
573 size_t outBuffSize = 0;
574 U32 blockSize = 128 KB;
575 U32 wNbBlocks = 4;
576 U64 filesize = 0;
577 BYTE* header[MAXHEADERSIZE];
578 ZSTD_Dctx* dctx;
579 size_t toRead;
580 size_t sizeCheck;
581
582
583 /* Init */
584 FIO_getFileHandles(&finput, &foutput, input_filename, output_filename);
585 dctx = ZSTD_createDCtx();
586
587 /* for each frame */
588 for ( ; ; )
589 {
590 /* check header */
591 ZSTD_resetDCtx(dctx);
592 toRead = ZSTD_nextSrcSizeToDecompress(dctx);
593 if (toRead > MAXHEADERSIZE) EXM_THROW(30, "Not enough memory to read header");
594 sizeCheck = fread(header, (size_t)1, toRead, finput);
595 if (sizeCheck==0) break; /* no more input */
596 if (sizeCheck != toRead) EXM_THROW(31, "Read error : cannot read header");
597 sizeCheck = ZSTD_decompressContinue(dctx, NULL, 0, header, toRead); // Decode frame header
598 if (ZSTD_isError(sizeCheck)) EXM_THROW(32, "Error decoding header");
599
600 /* Here later : blockSize determination */
601
602 /* Allocate Memory (if needed) */
603 {
604 size_t newInBuffSize = blockSize + FIO_blockHeaderSize;
605 size_t newOutBuffSize = wNbBlocks * blockSize;
606 if (newInBuffSize > inBuffSize)
607 {
608 free(inBuff);
609 inBuffSize = newInBuffSize;
610 inBuff = (BYTE*)malloc(inBuffSize);
611 }
612 if (newOutBuffSize > outBuffSize)
613 {
614 free(outBuff);
615 outBuffSize = newOutBuffSize;
616 outBuff = (BYTE*)malloc(outBuffSize);
617 }
618 }
619 if (!inBuff || !outBuff) EXM_THROW(33, "Allocation error : not enough memory");
620
621 filesize += FIO_decompressFrame(foutput, finput, inBuff, inBuffSize, outBuff, outBuffSize, dctx);
622 }
623
Yann Collet4856a002015-01-24 01:58:16 +0100624 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100625 DISPLAYLEVEL(2, "Decoded %llu bytes \n", (long long unsigned)filesize);
Yann Collet4856a002015-01-24 01:58:16 +0100626
627 /* clean */
628 free(inBuff);
629 free(outBuff);
Yann Collet4856a002015-01-24 01:58:16 +0100630 ZSTD_freeDCtx(dctx);
Yann Collet5abd8202015-08-27 03:16:04 +0100631 fclose(finput);
632 if (fclose(foutput)) EXM_THROW(38, "Write error : cannot properly close %s", output_filename);
Yann Collet4856a002015-01-24 01:58:16 +0100633
634 return filesize;
635}
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100636#endif