2015-01-23 16:58:16 -08:00
|
|
|
/*
|
|
|
|
fileio.c - File i/o handler
|
|
|
|
Copyright (C) Yann Collet 2013-2015
|
|
|
|
|
|
|
|
GPL v2 License
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
You can contact the author at :
|
|
|
|
- zstd source repository : https://github.com/Cyan4973/zstd
|
|
|
|
- Public forum : https://groups.google.com/forum/#!forum/lz4c
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
Note : this is stand-alone program.
|
|
|
|
It is not part of ZSTD compression library, it is a user program of ZSTD library.
|
|
|
|
The license of ZSTD library is BSD.
|
|
|
|
The license of this file is GPLv2.
|
|
|
|
*/
|
|
|
|
|
2015-10-22 08:55:40 -07:00
|
|
|
/* *************************************
|
2015-10-18 14:18:32 -07:00
|
|
|
* Tuning options
|
2015-10-22 08:55:40 -07:00
|
|
|
***************************************/
|
2015-10-18 14:18:32 -07:00
|
|
|
#ifndef ZSTD_LEGACY_SUPPORT
|
|
|
|
/**LEGACY_SUPPORT :
|
|
|
|
* decompressor can decode older formats (starting from Zstd 0.1+) */
|
|
|
|
# define ZSTD_LEGACY_SUPPORT 1
|
|
|
|
#endif // ZSTD_LEGACY_SUPPORT
|
|
|
|
|
|
|
|
|
2015-10-22 08:55:40 -07:00
|
|
|
/* *************************************
|
2015-01-23 16:58:16 -08:00
|
|
|
* Compiler Options
|
2015-10-22 08:55:40 -07:00
|
|
|
***************************************/
|
2015-01-23 16:58:16 -08:00
|
|
|
/* Disable some Visual warning messages */
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
# define _CRT_SECURE_NO_WARNINGS
|
|
|
|
# define _CRT_SECURE_NO_DEPRECATE /* VS2005 */
|
|
|
|
# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
|
|
|
|
|
|
|
|
#define _FILE_OFFSET_BITS 64 /* Large file support on 32-bits unix */
|
|
|
|
#define _POSIX_SOURCE 1 /* enable fileno() within <stdio.h> on unix */
|
|
|
|
|
|
|
|
|
2015-10-22 08:55:40 -07:00
|
|
|
/* *************************************
|
2015-01-23 16:58:16 -08:00
|
|
|
* Includes
|
2015-10-22 08:55:40 -07:00
|
|
|
***************************************/
|
2015-10-18 14:18:32 -07:00
|
|
|
#include <stdio.h> /* fprintf, fopen, fread, _fileno, stdin, stdout */
|
|
|
|
#include <stdlib.h> /* malloc, free */
|
|
|
|
#include <string.h> /* strcmp, strlen */
|
|
|
|
#include <time.h> /* clock */
|
|
|
|
#include <errno.h> /* errno */
|
|
|
|
#include "mem.h"
|
2015-01-23 16:58:16 -08:00
|
|
|
#include "fileio.h"
|
|
|
|
#include "zstd_static.h"
|
2015-10-29 08:49:43 -07:00
|
|
|
#include "zstdhc_static.h"
|
2015-01-23 16:58:16 -08:00
|
|
|
|
2015-10-18 14:18:32 -07:00
|
|
|
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
|
2015-10-30 03:21:50 -07:00
|
|
|
# include "zstd_legacy.h" /* legacy */
|
|
|
|
# include "fileio_legacy.h" /* legacy */
|
2015-10-22 08:55:40 -07:00
|
|
|
#endif
|
2015-10-18 14:18:32 -07:00
|
|
|
|
2015-01-23 16:58:16 -08:00
|
|
|
|
2015-10-22 08:55:40 -07:00
|
|
|
/* *************************************
|
2015-01-23 16:58:16 -08:00
|
|
|
* OS-specific Includes
|
2015-10-22 08:55:40 -07:00
|
|
|
***************************************/
|
2015-01-23 16:58:16 -08:00
|
|
|
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
# include <fcntl.h> /* _O_BINARY */
|
|
|
|
# include <io.h> /* _setmode, _isatty */
|
|
|
|
# ifdef __MINGW32__
|
2015-01-25 06:50:24 -08:00
|
|
|
/* int _fileno(FILE *stream); // seems no longer useful // MINGW somehow forgets to include this windows declaration into <stdio.h> */
|
2015-01-23 16:58:16 -08:00
|
|
|
# endif
|
2015-07-05 00:20:56 -07:00
|
|
|
# define SET_BINARY_MODE(file) { int unused = _setmode(_fileno(file), _O_BINARY); (void)unused; }
|
2015-01-23 16:58:16 -08:00
|
|
|
# define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
|
|
|
|
#else
|
|
|
|
# include <unistd.h> /* isatty */
|
|
|
|
# define SET_BINARY_MODE(file)
|
|
|
|
# define IS_CONSOLE(stdStream) isatty(fileno(stdStream))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2015-10-22 08:55:40 -07:00
|
|
|
/* *************************************
|
2015-01-23 16:58:16 -08:00
|
|
|
* Constants
|
2015-10-22 08:55:40 -07:00
|
|
|
***************************************/
|
2015-01-23 16:58:16 -08:00
|
|
|
#define KB *(1U<<10)
|
|
|
|
#define MB *(1U<<20)
|
|
|
|
#define GB *(1U<<30)
|
|
|
|
|
|
|
|
#define _1BIT 0x01
|
|
|
|
#define _2BITS 0x03
|
|
|
|
#define _3BITS 0x07
|
|
|
|
#define _4BITS 0x0F
|
|
|
|
#define _6BITS 0x3F
|
|
|
|
#define _8BITS 0xFF
|
|
|
|
|
|
|
|
#define BIT6 0x40
|
|
|
|
#define BIT7 0x80
|
|
|
|
|
2015-02-01 02:57:30 -08:00
|
|
|
//static const unsigned FIO_maxBlockSizeID = 0xB; /* => 2MB block */
|
2015-01-23 16:58:16 -08:00
|
|
|
static const unsigned FIO_blockHeaderSize = 3;
|
|
|
|
|
|
|
|
#define FIO_FRAMEHEADERSIZE 5 /* as a define, because needed to allocated table on stack */
|
|
|
|
#define FSE_CHECKSUM_SEED 0
|
|
|
|
|
|
|
|
#define CACHELINE 64
|
|
|
|
|
|
|
|
|
2015-10-22 08:55:40 -07:00
|
|
|
/* *************************************
|
2015-01-23 16:58:16 -08:00
|
|
|
* Macros
|
2015-10-22 08:55:40 -07:00
|
|
|
***************************************/
|
2015-01-23 16:58:16 -08:00
|
|
|
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
|
|
|
|
#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
|
|
|
|
static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
|
|
|
|
|
|
|
|
#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
|
|
|
|
if ((FIO_GetMilliSpan(g_time) > refreshRate) || (g_displayLevel>=4)) \
|
|
|
|
{ g_time = clock(); DISPLAY(__VA_ARGS__); \
|
|
|
|
if (g_displayLevel>=4) fflush(stdout); } }
|
|
|
|
static const unsigned refreshRate = 150;
|
|
|
|
static clock_t g_time = 0;
|
|
|
|
|
|
|
|
|
2015-10-22 08:55:40 -07:00
|
|
|
/* *************************************
|
2015-01-23 16:58:16 -08:00
|
|
|
* Local Parameters
|
2015-10-22 08:55:40 -07:00
|
|
|
***************************************/
|
2015-01-23 16:58:16 -08:00
|
|
|
static U32 g_overwrite = 0;
|
|
|
|
|
|
|
|
void FIO_overwriteMode(void) { g_overwrite=1; }
|
|
|
|
void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; }
|
|
|
|
|
|
|
|
|
2015-10-22 08:55:40 -07:00
|
|
|
/* *************************************
|
2015-01-23 16:58:16 -08:00
|
|
|
* Exceptions
|
2015-10-22 08:55:40 -07:00
|
|
|
***************************************/
|
|
|
|
#ifndef DEBUG
|
|
|
|
# define DEBUG 0
|
|
|
|
#endif
|
2015-01-23 16:58:16 -08:00
|
|
|
#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
|
|
|
|
#define EXM_THROW(error, ...) \
|
|
|
|
{ \
|
|
|
|
DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
|
|
|
|
DISPLAYLEVEL(1, "Error %i : ", error); \
|
|
|
|
DISPLAYLEVEL(1, __VA_ARGS__); \
|
|
|
|
DISPLAYLEVEL(1, "\n"); \
|
|
|
|
exit(error); \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-22 08:55:40 -07:00
|
|
|
/* *************************************
|
2015-01-23 16:58:16 -08:00
|
|
|
* Functions
|
2015-10-22 08:55:40 -07:00
|
|
|
***************************************/
|
2015-01-23 16:58:16 -08:00
|
|
|
static unsigned FIO_GetMilliSpan(clock_t nPrevious)
|
|
|
|
{
|
|
|
|
clock_t nCurrent = clock();
|
|
|
|
unsigned nSpan = (unsigned)(((nCurrent - nPrevious) * 1000) / CLOCKS_PER_SEC);
|
|
|
|
return nSpan;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void FIO_getFileHandles(FILE** pfinput, FILE** pfoutput, const char* input_filename, const char* output_filename)
|
|
|
|
{
|
|
|
|
if (!strcmp (input_filename, stdinmark))
|
|
|
|
{
|
|
|
|
DISPLAYLEVEL(4,"Using stdin for input\n");
|
|
|
|
*pfinput = stdin;
|
|
|
|
SET_BINARY_MODE(stdin);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*pfinput = fopen(input_filename, "rb");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp (output_filename, stdoutmark))
|
|
|
|
{
|
|
|
|
DISPLAYLEVEL(4,"Using stdout for output\n");
|
|
|
|
*pfoutput = stdout;
|
|
|
|
SET_BINARY_MODE(stdout);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Check if destination file already exists */
|
|
|
|
*pfoutput=0;
|
|
|
|
if (strcmp(output_filename,nulmark)) *pfoutput = fopen( output_filename, "rb" );
|
|
|
|
if (*pfoutput!=0)
|
|
|
|
{
|
|
|
|
fclose(*pfoutput);
|
|
|
|
if (!g_overwrite)
|
|
|
|
{
|
|
|
|
char ch;
|
|
|
|
if (g_displayLevel <= 1) /* No interaction possible */
|
|
|
|
EXM_THROW(11, "Operation aborted : %s already exists", output_filename);
|
|
|
|
DISPLAYLEVEL(2, "Warning : %s already exists\n", output_filename);
|
|
|
|
DISPLAYLEVEL(2, "Overwrite ? (Y/N) : ");
|
|
|
|
ch = (char)getchar();
|
|
|
|
if ((ch!='Y') && (ch!='y')) EXM_THROW(11, "Operation aborted : %s already exists", output_filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*pfoutput = fopen( output_filename, "wb" );
|
|
|
|
}
|
|
|
|
|
2015-10-18 14:18:32 -07:00
|
|
|
if ( *pfinput==0 ) EXM_THROW(12, "Pb opening src : %s", input_filename);
|
|
|
|
if ( *pfoutput==0) EXM_THROW(13, "Pb opening dst : %s", output_filename);
|
2015-01-23 16:58:16 -08:00
|
|
|
}
|
|
|
|
|
2015-10-29 08:49:43 -07:00
|
|
|
typedef void* (*FIO_createC) (void);
|
|
|
|
static void* local_ZSTD_createCCtx(void) { return (void*) ZSTD_createCCtx(); }
|
|
|
|
static void* local_ZSTD_HC_createCCtx(void) { return (void*) ZSTD_HC_createCCtx(); }
|
2015-01-23 16:58:16 -08:00
|
|
|
|
2015-10-29 08:49:43 -07:00
|
|
|
typedef size_t (*FIO_initC) (void* ctx, void* dst, size_t maxDstSize, int cLevel);
|
|
|
|
static size_t local_ZSTD_compressBegin (void* ctx, void* dst, size_t maxDstSize, int cLevel)
|
|
|
|
{
|
|
|
|
(void)cLevel;
|
|
|
|
return ZSTD_compressBegin((ZSTD_CCtx*)ctx, dst, maxDstSize);
|
|
|
|
}
|
|
|
|
static size_t local_ZSTD_HC_compressBegin (void* ctx, void* dst, size_t maxDstSize, int cLevel)
|
|
|
|
{
|
|
|
|
return ZSTD_HC_compressBegin((ZSTD_HC_CCtx*)ctx, dst, maxDstSize, cLevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef size_t (*FIO_continueC) (void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);
|
|
|
|
static size_t local_ZSTD_compressContinue (void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
|
|
|
|
{
|
|
|
|
return ZSTD_compressContinue((ZSTD_CCtx*)ctx, dst, maxDstSize, src, srcSize);
|
|
|
|
}
|
|
|
|
static size_t local_ZSTD_HC_compressContinue (void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
|
|
|
|
{
|
|
|
|
return ZSTD_HC_compressContinue((ZSTD_HC_CCtx*)ctx, dst, maxDstSize, src, srcSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef size_t (*FIO_endC) (void* ctx, void* dst, size_t maxDstSize);
|
|
|
|
static size_t local_ZSTD_compressEnd (void* ctx, void* dst, size_t maxDstSize)
|
|
|
|
{
|
|
|
|
return ZSTD_compressEnd((ZSTD_CCtx*)ctx, dst, maxDstSize);
|
|
|
|
}
|
|
|
|
static size_t local_ZSTD_HC_compressEnd (void* ctx, void* dst, size_t maxDstSize)
|
|
|
|
{
|
|
|
|
return ZSTD_HC_compressEnd((ZSTD_HC_CCtx*)ctx, dst, maxDstSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void (*FIO_freeC) (void* ctx);
|
|
|
|
static void local_ZSTD_freeCCtx(void* ctx) { ZSTD_freeCCtx((ZSTD_CCtx*)ctx); }
|
|
|
|
static void local_ZSTD_HC_freeCCtx(void* ctx) { ZSTD_HC_freeCCtx((ZSTD_HC_CCtx*)ctx); }
|
|
|
|
|
|
|
|
|
|
|
|
unsigned long long FIO_compressFilename(const char* output_filename, const char* input_filename, int cLevel)
|
2015-01-23 16:58:16 -08:00
|
|
|
{
|
|
|
|
U64 filesize = 0;
|
|
|
|
U64 compressedfilesize = 0;
|
|
|
|
BYTE* inBuff;
|
|
|
|
BYTE* inSlot;
|
|
|
|
BYTE* inEnd;
|
|
|
|
BYTE* outBuff;
|
|
|
|
size_t blockSize = 128 KB;
|
|
|
|
size_t inBuffSize = 4 * blockSize;
|
|
|
|
size_t outBuffSize = ZSTD_compressBound(blockSize);
|
|
|
|
FILE* finput;
|
|
|
|
FILE* foutput;
|
|
|
|
size_t sizeCheck, cSize;
|
2015-10-29 08:49:43 -07:00
|
|
|
void* ctx;
|
|
|
|
FIO_createC createC=NULL;
|
|
|
|
FIO_initC initC=NULL;
|
|
|
|
FIO_continueC continueC = NULL;
|
|
|
|
FIO_endC endC = NULL;
|
|
|
|
FIO_freeC freeC = NULL;
|
2015-01-23 16:58:16 -08:00
|
|
|
|
|
|
|
/* Init */
|
2015-10-29 08:49:43 -07:00
|
|
|
if (cLevel <= 1)
|
|
|
|
{
|
|
|
|
createC = local_ZSTD_createCCtx;
|
|
|
|
initC = local_ZSTD_compressBegin;
|
|
|
|
continueC = local_ZSTD_compressContinue;
|
|
|
|
endC = local_ZSTD_compressEnd;
|
|
|
|
freeC = local_ZSTD_freeCCtx;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
createC = local_ZSTD_HC_createCCtx;
|
|
|
|
initC = local_ZSTD_HC_compressBegin;
|
|
|
|
continueC = local_ZSTD_HC_compressContinue;
|
|
|
|
endC = local_ZSTD_HC_compressEnd;
|
|
|
|
freeC = local_ZSTD_HC_freeCCtx;
|
|
|
|
}
|
2015-01-23 16:58:16 -08:00
|
|
|
FIO_getFileHandles(&finput, &foutput, input_filename, output_filename);
|
|
|
|
|
|
|
|
/* Allocate Memory */
|
2015-10-29 08:49:43 -07:00
|
|
|
ctx = createC();
|
2015-06-18 08:43:16 -07:00
|
|
|
inBuff = (BYTE*)malloc(inBuffSize);
|
|
|
|
outBuff = (BYTE*)malloc(outBuffSize);
|
2015-10-21 06:39:26 -07:00
|
|
|
if (!inBuff || !outBuff || !ctx) EXM_THROW(21, "Allocation error : not enough memory");
|
2015-01-23 16:58:16 -08:00
|
|
|
inSlot = inBuff;
|
|
|
|
inEnd = inBuff + inBuffSize;
|
|
|
|
|
|
|
|
/* Write Frame Header */
|
2015-10-29 08:49:43 -07:00
|
|
|
cSize = initC(ctx, outBuff, outBuffSize, cLevel);
|
2015-01-23 16:58:16 -08:00
|
|
|
if (ZSTD_isError(cSize)) EXM_THROW(22, "Compression error : cannot create frame header");
|
|
|
|
|
|
|
|
sizeCheck = fwrite(outBuff, 1, cSize, foutput);
|
2015-08-25 10:13:32 -07:00
|
|
|
if (sizeCheck!=cSize) EXM_THROW(23, "Write error : cannot write header into %s", output_filename);
|
2015-01-23 16:58:16 -08:00
|
|
|
compressedfilesize += cSize;
|
|
|
|
|
|
|
|
/* Main compression loop */
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
size_t inSize;
|
|
|
|
|
|
|
|
/* Fill input Buffer */
|
|
|
|
if (inSlot + blockSize > inEnd) inSlot = inBuff;
|
|
|
|
inSize = fread(inSlot, (size_t)1, blockSize, finput);
|
|
|
|
if (inSize==0) break;
|
|
|
|
filesize += inSize;
|
|
|
|
DISPLAYUPDATE(2, "\rRead : %u MB ", (U32)(filesize>>20));
|
|
|
|
|
|
|
|
/* Compress Block */
|
2015-10-29 08:49:43 -07:00
|
|
|
cSize = continueC(ctx, outBuff, outBuffSize, inSlot, inSize);
|
2015-01-23 16:58:16 -08:00
|
|
|
if (ZSTD_isError(cSize))
|
|
|
|
EXM_THROW(24, "Compression error : %s ", ZSTD_getErrorName(cSize));
|
|
|
|
|
|
|
|
/* Write cBlock */
|
|
|
|
sizeCheck = fwrite(outBuff, 1, cSize, foutput);
|
2015-08-25 10:13:32 -07:00
|
|
|
if (sizeCheck!=cSize) EXM_THROW(25, "Write error : cannot write compressed block into %s", output_filename);
|
2015-01-23 16:58:16 -08:00
|
|
|
compressedfilesize += cSize;
|
|
|
|
inSlot += inSize;
|
|
|
|
|
|
|
|
DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", (U32)(filesize>>20), (double)compressedfilesize/filesize*100);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End of Frame */
|
2015-10-29 08:49:43 -07:00
|
|
|
cSize = endC(ctx, outBuff, outBuffSize);
|
2015-01-23 16:58:16 -08:00
|
|
|
if (ZSTD_isError(cSize)) EXM_THROW(26, "Compression error : cannot create frame end");
|
|
|
|
|
|
|
|
sizeCheck = fwrite(outBuff, 1, cSize, foutput);
|
2015-08-25 10:13:32 -07:00
|
|
|
if (sizeCheck!=cSize) EXM_THROW(27, "Write error : cannot write frame end into %s", output_filename);
|
2015-01-23 16:58:16 -08:00
|
|
|
compressedfilesize += cSize;
|
|
|
|
|
|
|
|
/* Status */
|
|
|
|
DISPLAYLEVEL(2, "\r%79s\r", "");
|
|
|
|
DISPLAYLEVEL(2,"Compressed %llu bytes into %llu bytes ==> %.2f%%\n",
|
|
|
|
(unsigned long long) filesize, (unsigned long long) compressedfilesize, (double)compressedfilesize/filesize*100);
|
|
|
|
|
|
|
|
/* clean */
|
|
|
|
free(inBuff);
|
|
|
|
free(outBuff);
|
2015-10-29 08:49:43 -07:00
|
|
|
freeC(ctx);
|
2015-08-25 09:41:46 -07:00
|
|
|
fclose(finput);
|
2015-08-25 10:13:32 -07:00
|
|
|
if (fclose(foutput)) EXM_THROW(28, "Write error : cannot properly close %s", output_filename);
|
2015-01-23 16:58:16 -08:00
|
|
|
|
|
|
|
return compressedfilesize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-10 15:26:09 -07:00
|
|
|
unsigned long long FIO_decompressFrame(FILE* foutput, FILE* finput,
|
|
|
|
BYTE* inBuff, size_t inBuffSize,
|
|
|
|
BYTE* outBuff, size_t outBuffSize,
|
2015-10-21 06:39:26 -07:00
|
|
|
ZSTD_DCtx* dctx)
|
2015-01-23 16:58:16 -08:00
|
|
|
{
|
2015-09-10 15:26:09 -07:00
|
|
|
BYTE* op = outBuff;
|
|
|
|
BYTE* const oend = outBuff + outBuffSize;
|
2015-01-23 16:58:16 -08:00
|
|
|
U64 filesize = 0;
|
|
|
|
size_t toRead;
|
|
|
|
size_t sizeCheck;
|
|
|
|
|
|
|
|
|
|
|
|
/* Main decompression Loop */
|
2015-02-16 09:06:26 -08:00
|
|
|
toRead = ZSTD_nextSrcSizeToDecompress(dctx);
|
2015-01-23 16:58:16 -08:00
|
|
|
while (toRead)
|
|
|
|
{
|
|
|
|
size_t readSize, decodedSize;
|
|
|
|
|
|
|
|
/* Fill input buffer */
|
2015-08-24 12:17:11 -07:00
|
|
|
if (toRead > inBuffSize)
|
|
|
|
EXM_THROW(34, "too large block");
|
2015-01-23 16:58:16 -08:00
|
|
|
readSize = fread(inBuff, 1, toRead, finput);
|
|
|
|
if (readSize != toRead)
|
2015-08-24 12:17:11 -07:00
|
|
|
EXM_THROW(35, "Read error");
|
2015-01-23 16:58:16 -08:00
|
|
|
|
|
|
|
/* Decode block */
|
|
|
|
decodedSize = ZSTD_decompressContinue(dctx, op, oend-op, inBuff, readSize);
|
2015-08-24 12:17:11 -07:00
|
|
|
if (ZSTD_isError(decodedSize)) EXM_THROW(36, "Decoding error : input corrupted");
|
2015-01-23 16:58:16 -08:00
|
|
|
|
2015-02-16 09:06:26 -08:00
|
|
|
if (decodedSize) /* not a header */
|
|
|
|
{
|
|
|
|
/* Write block */
|
|
|
|
sizeCheck = fwrite(op, 1, decodedSize, foutput);
|
2015-08-24 12:17:11 -07:00
|
|
|
if (sizeCheck != decodedSize) EXM_THROW(37, "Write error : unable to write data block to destination file");
|
2015-02-16 09:06:26 -08:00
|
|
|
filesize += decodedSize;
|
|
|
|
op += decodedSize;
|
|
|
|
if (op==oend) op = outBuff;
|
|
|
|
DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(filesize>>20) );
|
|
|
|
}
|
2015-01-23 16:58:16 -08:00
|
|
|
|
|
|
|
/* prepare for next Block */
|
2015-02-16 09:06:26 -08:00
|
|
|
toRead = ZSTD_nextSrcSizeToDecompress(dctx);
|
2015-01-23 16:58:16 -08:00
|
|
|
}
|
|
|
|
|
2015-09-10 15:26:09 -07:00
|
|
|
return filesize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-18 14:18:32 -07:00
|
|
|
#define MAXHEADERSIZE (FIO_FRAMEHEADERSIZE+3)
|
|
|
|
unsigned long long FIO_decompressFilename(const char* output_filename, const char* input_filename)
|
|
|
|
{
|
|
|
|
FILE* finput, *foutput;
|
|
|
|
BYTE* inBuff=NULL;
|
|
|
|
size_t inBuffSize = 0;
|
|
|
|
BYTE* outBuff=NULL;
|
|
|
|
size_t outBuffSize = 0;
|
|
|
|
U32 blockSize = 128 KB;
|
|
|
|
U32 wNbBlocks = 4;
|
|
|
|
U64 filesize = 0;
|
|
|
|
BYTE* header[MAXHEADERSIZE];
|
|
|
|
size_t toRead;
|
|
|
|
size_t sizeCheck;
|
|
|
|
|
|
|
|
|
|
|
|
/* Init */
|
2015-10-21 06:39:26 -07:00
|
|
|
ZSTD_DCtx* dctx = ZSTD_createDCtx();
|
2015-10-18 14:18:32 -07:00
|
|
|
FIO_getFileHandles(&finput, &foutput, input_filename, output_filename);
|
|
|
|
|
|
|
|
/* for each frame */
|
|
|
|
for ( ; ; )
|
|
|
|
{
|
|
|
|
/* check magic number -> version */
|
|
|
|
U32 magicNumber;
|
|
|
|
toRead = sizeof(ZSTD_magicNumber);;
|
|
|
|
sizeCheck = fread(header, (size_t)1, toRead, finput);
|
|
|
|
if (sizeCheck==0) break; /* no more input */
|
|
|
|
if (sizeCheck != toRead) EXM_THROW(31, "Read error : cannot read header");
|
|
|
|
|
|
|
|
magicNumber = MEM_readLE32(header);
|
|
|
|
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
|
2015-10-30 03:21:50 -07:00
|
|
|
if (ZSTD_isLegacy(magicNumber))
|
|
|
|
{
|
|
|
|
filesize += FIO_decompressLegacyFrame(foutput, finput, magicNumber);
|
2015-10-18 14:18:32 -07:00
|
|
|
continue;
|
|
|
|
}
|
2015-10-30 03:21:50 -07:00
|
|
|
#endif /* ZSTD_LEGACY_SUPPORT */
|
|
|
|
if (magicNumber != ZSTD_magicNumber) EXM_THROW(32, "Error : unknown frame prefix");
|
2015-10-18 14:18:32 -07:00
|
|
|
|
|
|
|
/* prepare frame decompression, by completing header */
|
|
|
|
ZSTD_resetDCtx(dctx);
|
|
|
|
toRead = ZSTD_nextSrcSizeToDecompress(dctx) - sizeof(ZSTD_magicNumber);
|
|
|
|
if (toRead > MAXHEADERSIZE) EXM_THROW(30, "Not enough memory to read header");
|
2015-10-29 22:40:22 -07:00
|
|
|
sizeCheck = fread(&header[sizeof(ZSTD_magicNumber)], 1, toRead, finput);
|
2015-10-18 14:18:32 -07:00
|
|
|
if (sizeCheck != toRead) EXM_THROW(31, "Read error : cannot read header");
|
|
|
|
sizeCheck = ZSTD_decompressContinue(dctx, NULL, 0, header, sizeof(ZSTD_magicNumber)+toRead); // Decode frame header
|
|
|
|
if (ZSTD_isError(sizeCheck)) EXM_THROW(32, "Error decoding header");
|
|
|
|
|
|
|
|
/* Here later : blockSize determination */
|
|
|
|
|
|
|
|
/* Allocate Memory (if needed) */
|
|
|
|
{
|
|
|
|
size_t newInBuffSize = blockSize + FIO_blockHeaderSize;
|
|
|
|
size_t newOutBuffSize = wNbBlocks * blockSize;
|
|
|
|
if (newInBuffSize > inBuffSize)
|
|
|
|
{
|
|
|
|
free(inBuff);
|
|
|
|
inBuffSize = newInBuffSize;
|
|
|
|
inBuff = (BYTE*)malloc(inBuffSize);
|
|
|
|
}
|
|
|
|
if (newOutBuffSize > outBuffSize)
|
|
|
|
{
|
|
|
|
free(outBuff);
|
|
|
|
outBuffSize = newOutBuffSize;
|
|
|
|
outBuff = (BYTE*)malloc(outBuffSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!inBuff || !outBuff) EXM_THROW(33, "Allocation error : not enough memory");
|
|
|
|
|
|
|
|
filesize += FIO_decompressFrame(foutput, finput, inBuff, inBuffSize, outBuff, outBuffSize, dctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
DISPLAYLEVEL(2, "\r%79s\r", "");
|
|
|
|
DISPLAYLEVEL(2, "Decoded %llu bytes \n", (long long unsigned)filesize);
|
|
|
|
|
|
|
|
/* clean */
|
|
|
|
free(inBuff);
|
|
|
|
free(outBuff);
|
|
|
|
ZSTD_freeDCtx(dctx);
|
|
|
|
fclose(finput);
|
|
|
|
if (fclose(foutput)) EXM_THROW(38, "Write error : cannot properly close %s", output_filename);
|
|
|
|
|
|
|
|
return filesize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
2015-09-10 15:26:09 -07:00
|
|
|
unsigned long long FIO_decompressFilename(const char* output_filename, const char* input_filename)
|
|
|
|
{
|
|
|
|
FILE* finput, *foutput;
|
|
|
|
BYTE* inBuff=NULL;
|
|
|
|
size_t inBuffSize = 0;
|
|
|
|
BYTE* outBuff=NULL;
|
|
|
|
size_t outBuffSize = 0;
|
|
|
|
U32 blockSize = 128 KB;
|
|
|
|
U32 wNbBlocks = 4;
|
|
|
|
U64 filesize = 0;
|
|
|
|
BYTE* header[MAXHEADERSIZE];
|
|
|
|
ZSTD_Dctx* dctx;
|
|
|
|
size_t toRead;
|
|
|
|
size_t sizeCheck;
|
|
|
|
|
|
|
|
|
|
|
|
/* Init */
|
|
|
|
FIO_getFileHandles(&finput, &foutput, input_filename, output_filename);
|
|
|
|
dctx = ZSTD_createDCtx();
|
|
|
|
|
|
|
|
/* for each frame */
|
|
|
|
for ( ; ; )
|
|
|
|
{
|
|
|
|
/* check header */
|
|
|
|
ZSTD_resetDCtx(dctx);
|
|
|
|
toRead = ZSTD_nextSrcSizeToDecompress(dctx);
|
|
|
|
if (toRead > MAXHEADERSIZE) EXM_THROW(30, "Not enough memory to read header");
|
|
|
|
sizeCheck = fread(header, (size_t)1, toRead, finput);
|
|
|
|
if (sizeCheck==0) break; /* no more input */
|
|
|
|
if (sizeCheck != toRead) EXM_THROW(31, "Read error : cannot read header");
|
|
|
|
sizeCheck = ZSTD_decompressContinue(dctx, NULL, 0, header, toRead); // Decode frame header
|
|
|
|
if (ZSTD_isError(sizeCheck)) EXM_THROW(32, "Error decoding header");
|
|
|
|
|
|
|
|
/* Here later : blockSize determination */
|
|
|
|
|
|
|
|
/* Allocate Memory (if needed) */
|
|
|
|
{
|
|
|
|
size_t newInBuffSize = blockSize + FIO_blockHeaderSize;
|
|
|
|
size_t newOutBuffSize = wNbBlocks * blockSize;
|
|
|
|
if (newInBuffSize > inBuffSize)
|
|
|
|
{
|
|
|
|
free(inBuff);
|
|
|
|
inBuffSize = newInBuffSize;
|
|
|
|
inBuff = (BYTE*)malloc(inBuffSize);
|
|
|
|
}
|
|
|
|
if (newOutBuffSize > outBuffSize)
|
|
|
|
{
|
|
|
|
free(outBuff);
|
|
|
|
outBuffSize = newOutBuffSize;
|
|
|
|
outBuff = (BYTE*)malloc(outBuffSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!inBuff || !outBuff) EXM_THROW(33, "Allocation error : not enough memory");
|
|
|
|
|
|
|
|
filesize += FIO_decompressFrame(foutput, finput, inBuff, inBuffSize, outBuff, outBuffSize, dctx);
|
|
|
|
}
|
|
|
|
|
2015-01-23 16:58:16 -08:00
|
|
|
DISPLAYLEVEL(2, "\r%79s\r", "");
|
2015-10-18 14:18:32 -07:00
|
|
|
DISPLAYLEVEL(2, "Decoded %llu bytes \n", (long long unsigned)filesize);
|
2015-01-23 16:58:16 -08:00
|
|
|
|
|
|
|
/* clean */
|
|
|
|
free(inBuff);
|
|
|
|
free(outBuff);
|
|
|
|
ZSTD_freeDCtx(dctx);
|
2015-08-26 19:16:04 -07:00
|
|
|
fclose(finput);
|
|
|
|
if (fclose(foutput)) EXM_THROW(38, "Write error : cannot properly close %s", output_filename);
|
2015-01-23 16:58:16 -08:00
|
|
|
|
|
|
|
return filesize;
|
|
|
|
}
|
2015-10-18 14:18:32 -07:00
|
|
|
#endif
|