2015-01-24 00:58:16 +00:00
|
|
|
/*
|
2016-02-11 03:17:50 +00:00
|
|
|
fileio.c - File i/o handler for zstd
|
|
|
|
Copyright (C) Yann Collet 2013-2016
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
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 :
|
2016-02-11 03:17:50 +00:00
|
|
|
- zstd homepage : http://www.zstd.net
|
2015-01-24 00:58:16 +00:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
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 15:55:40 +00:00
|
|
|
/* *************************************
|
2015-10-18 21:18:32 +00:00
|
|
|
* Tuning options
|
2015-10-22 15:55:40 +00:00
|
|
|
***************************************/
|
2015-10-18 21:18:32 +00:00
|
|
|
#ifndef ZSTD_LEGACY_SUPPORT
|
2016-02-11 03:17:50 +00:00
|
|
|
/* LEGACY_SUPPORT :
|
2015-10-18 21:18:32 +00:00
|
|
|
* decompressor can decode older formats (starting from Zstd 0.1+) */
|
|
|
|
# define ZSTD_LEGACY_SUPPORT 1
|
2015-11-09 16:42:17 +00:00
|
|
|
#endif
|
2015-10-18 21:18:32 +00:00
|
|
|
|
|
|
|
|
2015-10-22 15:55:40 +00:00
|
|
|
/* *************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Compiler Options
|
2015-10-22 15:55:40 +00:00
|
|
|
***************************************/
|
2016-05-05 11:10:57 +00:00
|
|
|
#define _POSIX_SOURCE 1 /* enable %llu on Windows */
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
|
2016-02-12 19:19:48 +00:00
|
|
|
/*-*************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Includes
|
2015-10-22 15:55:40 +00:00
|
|
|
***************************************/
|
2016-05-05 11:58:56 +00:00
|
|
|
#include "util.h" /* Compiler options, UTIL_GetFileSize */
|
2015-11-09 16:42:17 +00: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 */
|
2016-04-28 12:40:45 +00:00
|
|
|
|
|
|
|
#include "mem.h"
|
2015-01-24 00:58:16 +00:00
|
|
|
#include "fileio.h"
|
2016-06-04 17:47:02 +00:00
|
|
|
#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_magicNumber, ZSTD_frameHeaderSize_max */
|
|
|
|
#include "zstd.h"
|
2016-05-25 09:31:16 +00:00
|
|
|
#include "zstd_internal.h" /* MIN, KB, MB */
|
2016-06-04 17:12:48 +00:00
|
|
|
#define ZBUFF_STATIC_LINKING_ONLY
|
|
|
|
#include "zbuff.h"
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2015-10-18 21:18:32 +00:00
|
|
|
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
|
2016-02-12 19:19:48 +00:00
|
|
|
# include "zstd_legacy.h" /* ZSTD_isLegacy */
|
|
|
|
# include "fileio_legacy.h" /* FIO_decompressLegacyFrame */
|
2015-10-22 15:55:40 +00:00
|
|
|
#endif
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2016-02-12 19:19:48 +00:00
|
|
|
/*-*************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* OS-specific Includes
|
2015-10-22 15:55:40 +00:00
|
|
|
***************************************/
|
2015-01-24 00:58:16 +00:00
|
|
|
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
# include <fcntl.h> /* _O_BINARY */
|
|
|
|
# include <io.h> /* _setmode, _isatty */
|
2016-05-19 08:29:49 +00:00
|
|
|
# define SET_BINARY_MODE(file) { if (_setmode(_fileno(file), _O_BINARY) == -1) perror("Cannot set _O_BINARY"); }
|
2015-01-24 00:58:16 +00:00
|
|
|
#else
|
|
|
|
# include <unistd.h> /* isatty */
|
|
|
|
# define SET_BINARY_MODE(file)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2016-02-12 19:19:48 +00:00
|
|
|
/*-*************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Constants
|
2015-10-22 15:55:40 +00:00
|
|
|
***************************************/
|
2015-01-24 00:58:16 +00:00
|
|
|
#define _1BIT 0x01
|
|
|
|
#define _2BITS 0x03
|
|
|
|
#define _3BITS 0x07
|
|
|
|
#define _4BITS 0x0F
|
|
|
|
#define _6BITS 0x3F
|
|
|
|
#define _8BITS 0xFF
|
|
|
|
|
2015-11-25 13:42:45 +00:00
|
|
|
#define BLOCKSIZE (128 KB)
|
|
|
|
#define ROLLBUFFERSIZE (BLOCKSIZE*8*64)
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2016-02-12 19:19:48 +00:00
|
|
|
#define FIO_FRAMEHEADERSIZE 5 /* as a define, because needed to allocated table on stack */
|
|
|
|
#define FSE_CHECKSUM_SEED 0
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
#define CACHELINE 64
|
|
|
|
|
2016-05-31 00:40:42 +00:00
|
|
|
#define MAX_DICT_SIZE (8 MB) /* protection against large input (attack scenario) */
|
2016-02-12 19:19:48 +00:00
|
|
|
|
2016-04-22 11:59:05 +00:00
|
|
|
#define FNSPACE 30
|
|
|
|
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2016-02-15 19:37:23 +00:00
|
|
|
/*-*************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Macros
|
2015-10-22 15:55:40 +00:00
|
|
|
***************************************/
|
2015-01-24 00:58:16 +00: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 */
|
2016-03-10 20:02:25 +00:00
|
|
|
void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; }
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
#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;
|
|
|
|
|
2016-05-09 02:44:45 +00:00
|
|
|
static unsigned FIO_GetMilliSpan(clock_t nPrevious)
|
|
|
|
{
|
|
|
|
clock_t const nCurrent = clock();
|
|
|
|
return (unsigned)(((nCurrent - nPrevious) * 1000) / CLOCKS_PER_SEC);
|
|
|
|
}
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2016-02-15 19:37:23 +00:00
|
|
|
|
|
|
|
/*-*************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Local Parameters
|
2015-10-22 15:55:40 +00:00
|
|
|
***************************************/
|
2015-01-24 00:58:16 +00:00
|
|
|
static U32 g_overwrite = 0;
|
|
|
|
void FIO_overwriteMode(void) { g_overwrite=1; }
|
2016-03-10 20:02:25 +00:00
|
|
|
static U32 g_maxWLog = 23;
|
|
|
|
void FIO_setMaxWLog(unsigned maxWLog) { g_maxWLog = maxWLog; }
|
2016-05-23 14:56:56 +00:00
|
|
|
static U32 g_sparseFileSupport = 1; /* 0 : no sparse allowed; 1: auto (file yes, stdout no); 2: force sparse */
|
|
|
|
void FIO_setSparseWrite(unsigned sparse) { g_sparseFileSupport=sparse; }
|
2016-05-31 00:29:45 +00:00
|
|
|
static U32 g_dictIDFlag = 1;
|
|
|
|
void FIO_setDictIDFlag(unsigned dictIDFlag) { g_dictIDFlag = dictIDFlag; }
|
2016-06-01 17:22:15 +00:00
|
|
|
static U32 g_checksumFlag = 0;
|
|
|
|
void FIO_setChecksumFlag(unsigned checksumFlag) { g_checksumFlag = checksumFlag; }
|
2016-06-09 20:59:51 +00:00
|
|
|
static U32 g_removeSrcFile = 0;
|
|
|
|
void FIO_setRemoveSrcFile(unsigned flag) { g_removeSrcFile = (flag>0); }
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
|
2016-02-15 19:37:23 +00:00
|
|
|
/*-*************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Exceptions
|
2015-10-22 15:55:40 +00:00
|
|
|
***************************************/
|
|
|
|
#ifndef DEBUG
|
|
|
|
# define DEBUG 0
|
|
|
|
#endif
|
2015-01-24 00:58:16 +00: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); \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-15 19:37:23 +00:00
|
|
|
/*-*************************************
|
2015-01-24 00:58:16 +00:00
|
|
|
* Functions
|
2015-10-22 15:55:40 +00:00
|
|
|
***************************************/
|
2016-02-12 14:56:46 +00:00
|
|
|
static FILE* FIO_openSrcFile(const char* srcFileName)
|
2015-12-17 01:23:58 +00:00
|
|
|
{
|
2016-02-12 14:56:46 +00:00
|
|
|
FILE* f;
|
|
|
|
|
2016-02-02 13:36:49 +00:00
|
|
|
if (!strcmp (srcFileName, stdinmark)) {
|
2015-12-17 01:23:58 +00:00
|
|
|
DISPLAYLEVEL(4,"Using stdin for input\n");
|
2016-02-12 14:56:46 +00:00
|
|
|
f = stdin;
|
2015-12-17 01:23:58 +00:00
|
|
|
SET_BINARY_MODE(stdin);
|
2016-02-02 13:36:49 +00:00
|
|
|
} else {
|
2016-02-12 14:56:46 +00:00
|
|
|
f = fopen(srcFileName, "rb");
|
2015-12-17 01:23:58 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 14:56:46 +00:00
|
|
|
if ( f==NULL ) DISPLAYLEVEL(1, "zstd: %s: No such file\n", srcFileName);
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static FILE* FIO_openDstFile(const char* dstFileName)
|
|
|
|
{
|
|
|
|
FILE* f;
|
2015-12-17 01:23:58 +00:00
|
|
|
|
2016-02-02 13:36:49 +00:00
|
|
|
if (!strcmp (dstFileName, stdoutmark)) {
|
2015-12-17 01:23:58 +00:00
|
|
|
DISPLAYLEVEL(4,"Using stdout for output\n");
|
2016-02-12 14:56:46 +00:00
|
|
|
f = stdout;
|
2015-12-17 01:23:58 +00:00
|
|
|
SET_BINARY_MODE(stdout);
|
2016-05-23 14:56:56 +00:00
|
|
|
if (g_sparseFileSupport==1) {
|
|
|
|
g_sparseFileSupport = 0;
|
|
|
|
DISPLAYLEVEL(4, "Sparse File Support is automatically disabled on stdout ; try --sparse \n");
|
|
|
|
}
|
2016-02-02 13:36:49 +00:00
|
|
|
} else {
|
2016-05-31 00:29:45 +00:00
|
|
|
if (!g_overwrite && strcmp (dstFileName, nulmark)) { /* Check if destination file already exists */
|
2016-02-12 14:56:46 +00:00
|
|
|
f = fopen( dstFileName, "rb" );
|
|
|
|
if (f != 0) { /* dest file exists, prompt for overwrite authorization */
|
|
|
|
fclose(f);
|
|
|
|
if (g_displayLevel <= 1) {
|
2015-12-17 19:30:14 +00:00
|
|
|
/* No interaction possible */
|
2016-02-12 14:56:46 +00:00
|
|
|
DISPLAY("zstd: %s already exists; not overwritten \n", dstFileName);
|
|
|
|
return 0;
|
2015-12-17 19:30:14 +00:00
|
|
|
}
|
2016-02-12 14:56:46 +00:00
|
|
|
DISPLAY("zstd: %s already exists; do you wish to overwrite (y/N) ? ", dstFileName);
|
2016-05-23 14:56:56 +00:00
|
|
|
{ int ch = getchar();
|
2016-02-02 13:36:49 +00:00
|
|
|
if ((ch!='Y') && (ch!='y')) {
|
2016-02-12 14:56:46 +00:00
|
|
|
DISPLAY(" not overwritten \n");
|
|
|
|
return 0;
|
2015-12-29 10:57:15 +00:00
|
|
|
}
|
|
|
|
while ((ch!=EOF) && (ch!='\n')) ch = getchar(); /* flush rest of input line */
|
2016-02-02 13:36:49 +00:00
|
|
|
} } }
|
2016-02-12 14:56:46 +00:00
|
|
|
f = fopen( dstFileName, "wb" );
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-10 20:02:25 +00:00
|
|
|
/*! FIO_loadFile() :
|
2016-03-15 11:56:03 +00:00
|
|
|
* creates a buffer, pointed by `*bufferPtr`,
|
2016-03-10 20:02:25 +00:00
|
|
|
* loads `filename` content into it,
|
2016-05-31 00:40:42 +00:00
|
|
|
* up to MAX_DICT_SIZE bytes.
|
|
|
|
* @return : loaded size
|
2015-12-17 19:30:14 +00:00
|
|
|
*/
|
|
|
|
static size_t FIO_loadFile(void** bufferPtr, const char* fileName)
|
|
|
|
{
|
|
|
|
FILE* fileHandle;
|
|
|
|
U64 fileSize;
|
|
|
|
|
|
|
|
*bufferPtr = NULL;
|
2016-02-02 13:36:49 +00:00
|
|
|
if (fileName == NULL) return 0;
|
2015-12-17 19:30:14 +00:00
|
|
|
|
|
|
|
DISPLAYLEVEL(4,"Loading %s as dictionary \n", fileName);
|
|
|
|
fileHandle = fopen(fileName, "rb");
|
|
|
|
if (fileHandle==0) EXM_THROW(31, "Error opening file %s", fileName);
|
2016-04-28 10:23:33 +00:00
|
|
|
fileSize = UTIL_getFileSize(fileName);
|
2016-02-02 13:36:49 +00:00
|
|
|
if (fileSize > MAX_DICT_SIZE) {
|
2015-12-17 19:30:14 +00:00
|
|
|
int seekResult;
|
|
|
|
if (fileSize > 1 GB) EXM_THROW(32, "Dictionary file %s is too large", fileName); /* avoid extreme cases */
|
|
|
|
DISPLAYLEVEL(2,"Dictionary %s is too large : using last %u bytes only \n", fileName, MAX_DICT_SIZE);
|
|
|
|
seekResult = fseek(fileHandle, (long int)(fileSize-MAX_DICT_SIZE), SEEK_SET); /* use end of file */
|
|
|
|
if (seekResult != 0) EXM_THROW(33, "Error seeking into file %s", fileName);
|
|
|
|
fileSize = MAX_DICT_SIZE;
|
|
|
|
}
|
|
|
|
*bufferPtr = (BYTE*)malloc((size_t)fileSize);
|
|
|
|
if (*bufferPtr==NULL) EXM_THROW(34, "Allocation error : not enough memory for dictBuffer");
|
2016-05-31 00:40:42 +00:00
|
|
|
{ size_t const readSize = fread(*bufferPtr, 1, (size_t)fileSize, fileHandle);
|
|
|
|
if (readSize!=fileSize) EXM_THROW(35, "Error reading dictionary file %s", fileName); }
|
2015-12-17 19:30:14 +00:00
|
|
|
fclose(fileHandle);
|
|
|
|
return (size_t)fileSize;
|
|
|
|
}
|
|
|
|
|
2016-04-22 11:59:05 +00:00
|
|
|
#ifndef ZSTD_NOCOMPRESS
|
2015-12-17 01:23:58 +00:00
|
|
|
|
2016-03-10 20:02:25 +00:00
|
|
|
/*-**********************************************************************
|
2015-12-17 01:23:58 +00:00
|
|
|
* Compression
|
|
|
|
************************************************************************/
|
|
|
|
typedef struct {
|
|
|
|
void* srcBuffer;
|
|
|
|
size_t srcBufferSize;
|
|
|
|
void* dstBuffer;
|
|
|
|
size_t dstBufferSize;
|
|
|
|
void* dictBuffer;
|
|
|
|
size_t dictBufferSize;
|
|
|
|
ZBUFF_CCtx* ctx;
|
2016-02-12 14:56:46 +00:00
|
|
|
FILE* dstFile;
|
2016-02-15 19:37:23 +00:00
|
|
|
FILE* srcFile;
|
2015-12-17 01:23:58 +00:00
|
|
|
} cRess_t;
|
|
|
|
|
|
|
|
static cRess_t FIO_createCResources(const char* dictFileName)
|
|
|
|
{
|
|
|
|
cRess_t ress;
|
|
|
|
|
|
|
|
ress.ctx = ZBUFF_createCCtx();
|
|
|
|
if (ress.ctx == NULL) EXM_THROW(30, "Allocation error : can't create ZBUFF context");
|
|
|
|
|
|
|
|
/* Allocate Memory */
|
|
|
|
ress.srcBufferSize = ZBUFF_recommendedCInSize();
|
|
|
|
ress.srcBuffer = malloc(ress.srcBufferSize);
|
|
|
|
ress.dstBufferSize = ZBUFF_recommendedCOutSize();
|
|
|
|
ress.dstBuffer = malloc(ress.dstBufferSize);
|
|
|
|
if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(31, "Allocation error : not enough memory");
|
|
|
|
|
|
|
|
/* dictionary */
|
2015-12-17 19:30:14 +00:00
|
|
|
ress.dictBufferSize = FIO_loadFile(&(ress.dictBuffer), dictFileName);
|
2015-12-17 01:23:58 +00:00
|
|
|
|
|
|
|
return ress;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FIO_freeCResources(cRess_t ress)
|
|
|
|
{
|
|
|
|
size_t errorCode;
|
|
|
|
free(ress.srcBuffer);
|
|
|
|
free(ress.dstBuffer);
|
|
|
|
free(ress.dictBuffer);
|
|
|
|
errorCode = ZBUFF_freeCCtx(ress.ctx);
|
|
|
|
if (ZBUFF_isError(errorCode)) EXM_THROW(38, "Error : can't release ZBUFF context resource : %s", ZBUFF_getErrorName(errorCode));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-12 14:56:46 +00:00
|
|
|
/*! FIO_compressFilename_internal() :
|
2016-03-10 20:02:25 +00:00
|
|
|
* same as FIO_compressFilename_extRess(), with `ress.desFile` already opened.
|
2016-02-12 14:56:46 +00:00
|
|
|
* @return : 0 : compression completed correctly,
|
|
|
|
* 1 : missing or pb opening srcFileName
|
2015-12-17 01:23:58 +00:00
|
|
|
*/
|
2016-02-12 14:56:46 +00:00
|
|
|
static int FIO_compressFilename_internal(cRess_t ress,
|
|
|
|
const char* dstFileName, const char* srcFileName,
|
|
|
|
int cLevel)
|
2015-12-17 01:23:58 +00:00
|
|
|
{
|
2016-05-07 20:43:40 +00:00
|
|
|
FILE* const srcFile = ress.srcFile;
|
|
|
|
FILE* const dstFile = ress.dstFile;
|
2016-03-26 19:52:14 +00:00
|
|
|
U64 readsize = 0;
|
2015-12-17 01:23:58 +00:00
|
|
|
U64 compressedfilesize = 0;
|
2016-04-28 10:23:33 +00:00
|
|
|
U64 const fileSize = UTIL_getFileSize(srcFileName);
|
2015-12-17 01:23:58 +00:00
|
|
|
|
|
|
|
/* init */
|
2016-05-07 20:43:40 +00:00
|
|
|
{ ZSTD_parameters params;
|
2016-05-29 03:01:04 +00:00
|
|
|
memset(¶ms, 0, sizeof(params));
|
2016-05-07 20:43:40 +00:00
|
|
|
params.cParams = ZSTD_getCParams(cLevel, fileSize, ress.dictBufferSize);
|
|
|
|
params.fParams.contentSizeFlag = 1;
|
2016-06-01 17:22:15 +00:00
|
|
|
params.fParams.checksumFlag = g_checksumFlag;
|
2016-05-31 00:29:45 +00:00
|
|
|
params.fParams.noDictIDFlag = !g_dictIDFlag;
|
2016-06-03 22:09:02 +00:00
|
|
|
if ((g_maxWLog) && (params.cParams.windowLog > g_maxWLog)) {
|
|
|
|
params.cParams.windowLog = g_maxWLog;
|
|
|
|
params.cParams = ZSTD_adjustCParams(params.cParams, fileSize, ress.dictBufferSize);
|
|
|
|
}
|
2016-05-31 00:40:42 +00:00
|
|
|
{ size_t const errorCode = ZBUFF_compressInit_advanced(ress.ctx, ress.dictBuffer, ress.dictBufferSize, params, fileSize);
|
|
|
|
if (ZBUFF_isError(errorCode)) EXM_THROW(21, "Error initializing compression : %s", ZBUFF_getErrorName(errorCode));
|
|
|
|
} }
|
2015-12-17 01:23:58 +00:00
|
|
|
|
|
|
|
/* Main compression loop */
|
2016-03-26 19:52:14 +00:00
|
|
|
readsize = 0;
|
2016-01-26 15:31:22 +00:00
|
|
|
while (1) {
|
2015-12-17 01:23:58 +00:00
|
|
|
/* Fill input Buffer */
|
2016-03-26 19:52:14 +00:00
|
|
|
size_t const inSize = fread(ress.srcBuffer, (size_t)1, ress.srcBufferSize, srcFile);
|
2015-12-17 01:23:58 +00:00
|
|
|
if (inSize==0) break;
|
2016-03-26 19:52:14 +00:00
|
|
|
readsize += inSize;
|
|
|
|
DISPLAYUPDATE(2, "\rRead : %u MB ", (U32)(readsize>>20));
|
2015-12-17 01:23:58 +00:00
|
|
|
|
2016-02-12 14:56:46 +00:00
|
|
|
{ /* Compress using buffered streaming */
|
2015-12-17 01:23:58 +00:00
|
|
|
size_t usedInSize = inSize;
|
|
|
|
size_t cSize = ress.dstBufferSize;
|
2016-05-07 20:43:40 +00:00
|
|
|
{ size_t const result = ZBUFF_compressContinue(ress.ctx, ress.dstBuffer, &cSize, ress.srcBuffer, &usedInSize);
|
|
|
|
if (ZBUFF_isError(result)) EXM_THROW(23, "Compression error : %s ", ZBUFF_getErrorName(result)); }
|
2015-12-17 01:23:58 +00:00
|
|
|
if (inSize != usedInSize)
|
|
|
|
/* inBuff should be entirely consumed since buffer sizes are recommended ones */
|
|
|
|
EXM_THROW(24, "Compression error : input block not fully consumed");
|
|
|
|
|
|
|
|
/* Write cBlock */
|
2016-05-07 20:43:40 +00:00
|
|
|
{ size_t const sizeCheck = fwrite(ress.dstBuffer, 1, cSize, dstFile);
|
|
|
|
if (sizeCheck!=cSize) EXM_THROW(25, "Write error : cannot write compressed block into %s", dstFileName); }
|
2015-12-17 01:23:58 +00:00
|
|
|
compressedfilesize += cSize;
|
|
|
|
}
|
2016-03-26 19:52:14 +00:00
|
|
|
DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", (U32)(readsize>>20), (double)compressedfilesize/readsize*100);
|
2015-12-17 01:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* End of Frame */
|
2016-03-26 19:52:14 +00:00
|
|
|
{ size_t cSize = ress.dstBufferSize;
|
|
|
|
size_t const result = ZBUFF_compressEnd(ress.ctx, ress.dstBuffer, &cSize);
|
2015-12-17 01:23:58 +00:00
|
|
|
if (result!=0) EXM_THROW(26, "Compression error : cannot create frame end");
|
|
|
|
|
2016-05-07 20:43:40 +00:00
|
|
|
{ size_t const sizeCheck = fwrite(ress.dstBuffer, 1, cSize, dstFile);
|
|
|
|
if (sizeCheck!=cSize) EXM_THROW(27, "Write error : cannot write frame end into %s", dstFileName); }
|
2015-12-17 01:23:58 +00:00
|
|
|
compressedfilesize += cSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Status */
|
|
|
|
DISPLAYLEVEL(2, "\r%79s\r", "");
|
2016-06-09 20:59:51 +00:00
|
|
|
DISPLAYLEVEL(2,"%-20.20s :%6.2f%% (%6llu =>%6llu bytes, %s) \n", srcFileName,
|
|
|
|
(double)compressedfilesize/readsize*100, (unsigned long long)readsize, (unsigned long long) compressedfilesize,
|
|
|
|
dstFileName);
|
2015-12-17 01:23:58 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-15 19:37:23 +00:00
|
|
|
/*! FIO_compressFilename_internal() :
|
2016-03-15 11:56:03 +00:00
|
|
|
* same as FIO_compressFilename_extRess(), with ress.destFile already opened (typically stdout)
|
2016-02-15 19:37:23 +00:00
|
|
|
* @return : 0 : compression completed correctly,
|
|
|
|
* 1 : missing or pb opening srcFileName
|
|
|
|
*/
|
|
|
|
static int FIO_compressFilename_srcFile(cRess_t ress,
|
|
|
|
const char* dstFileName, const char* srcFileName,
|
|
|
|
int cLevel)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
/* File check */
|
2016-06-09 20:59:51 +00:00
|
|
|
if (UTIL_isDirectory(srcFileName)) {
|
|
|
|
DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-02-15 19:37:23 +00:00
|
|
|
ress.srcFile = FIO_openSrcFile(srcFileName);
|
|
|
|
if (!ress.srcFile) return 1; /* srcFile could not be opened */
|
|
|
|
|
|
|
|
result = FIO_compressFilename_internal(ress, dstFileName, srcFileName, cLevel);
|
|
|
|
|
|
|
|
fclose(ress.srcFile);
|
2016-06-09 20:59:51 +00:00
|
|
|
if ((g_removeSrcFile) && (!result)) remove(srcFileName);
|
2016-02-15 19:37:23 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-09 20:59:51 +00:00
|
|
|
/*! FIO_compressFilename_dstFile() :
|
2016-02-12 14:56:46 +00:00
|
|
|
* @return : 0 : compression completed correctly,
|
2016-06-09 20:59:51 +00:00
|
|
|
* 1 : pb
|
2016-02-12 14:56:46 +00:00
|
|
|
*/
|
2016-06-09 20:59:51 +00:00
|
|
|
static int FIO_compressFilename_dstFile(cRess_t ress,
|
2016-02-12 14:56:46 +00:00
|
|
|
const char* dstFileName, const char* srcFileName,
|
|
|
|
int cLevel)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
ress.dstFile = FIO_openDstFile(dstFileName);
|
2016-02-15 19:37:23 +00:00
|
|
|
if (ress.dstFile==0) { fclose(ress.srcFile); return 1; }
|
2016-02-12 14:56:46 +00:00
|
|
|
|
2016-06-09 20:59:51 +00:00
|
|
|
result = FIO_compressFilename_srcFile(ress, dstFileName, srcFileName, cLevel);
|
2016-03-14 05:24:46 +00:00
|
|
|
|
2016-02-12 14:56:46 +00:00
|
|
|
if (fclose(ress.dstFile)) EXM_THROW(28, "Write error : cannot properly close %s", dstFileName);
|
2016-06-09 20:59:51 +00:00
|
|
|
if (result!=0) remove(dstFileName); /* remove operation artefact */
|
2016-02-12 14:56:46 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-17 13:09:55 +00:00
|
|
|
int FIO_compressFilename(const char* dstFileName, const char* srcFileName,
|
|
|
|
const char* dictFileName, int compressionLevel)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2016-05-31 00:29:45 +00:00
|
|
|
clock_t const start = clock();
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2016-06-09 20:59:51 +00:00
|
|
|
cRess_t const ress = FIO_createCResources(dictFileName);
|
|
|
|
int const issueWithSrcFile = FIO_compressFilename_dstFile(ress, dstFileName, srcFileName, compressionLevel);
|
2015-12-17 13:09:55 +00:00
|
|
|
FIO_freeCResources(ress);
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2016-06-09 20:59:51 +00:00
|
|
|
{ double const seconds = (double)(clock() - start) / CLOCKS_PER_SEC;
|
2015-12-17 13:09:55 +00:00
|
|
|
DISPLAYLEVEL(4, "Completed in %.2f sec \n", seconds);
|
2015-11-25 13:42:45 +00:00
|
|
|
}
|
2015-12-17 13:09:55 +00:00
|
|
|
return issueWithSrcFile;
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-17 13:09:55 +00:00
|
|
|
int FIO_compressMultipleFilenames(const char** inFileNamesTable, unsigned nbFiles,
|
2015-12-17 01:23:58 +00:00
|
|
|
const char* suffix,
|
|
|
|
const char* dictFileName, int compressionLevel)
|
|
|
|
{
|
|
|
|
int missed_files = 0;
|
2016-05-07 20:43:40 +00:00
|
|
|
char* dstFileName = (char*)malloc(FNSPACE);
|
2015-12-17 01:23:58 +00:00
|
|
|
size_t dfnSize = FNSPACE;
|
2016-05-07 20:43:40 +00:00
|
|
|
size_t const suffixSize = suffix ? strlen(suffix) : 0;
|
2015-12-17 01:23:58 +00:00
|
|
|
cRess_t ress;
|
|
|
|
|
|
|
|
/* init */
|
|
|
|
ress = FIO_createCResources(dictFileName);
|
|
|
|
|
|
|
|
/* loop on each file */
|
2016-02-15 19:37:23 +00:00
|
|
|
if (!strcmp(suffix, stdoutmark)) {
|
2016-05-07 20:43:40 +00:00
|
|
|
unsigned u;
|
2016-02-15 19:37:23 +00:00
|
|
|
ress.dstFile = stdout;
|
2016-05-19 08:29:49 +00:00
|
|
|
SET_BINARY_MODE(stdout);
|
2016-02-15 19:37:23 +00:00
|
|
|
for (u=0; u<nbFiles; u++)
|
|
|
|
missed_files += FIO_compressFilename_srcFile(ress, stdoutmark,
|
2016-03-10 20:02:25 +00:00
|
|
|
inFileNamesTable[u], compressionLevel);
|
2016-02-15 19:37:23 +00:00
|
|
|
if (fclose(ress.dstFile)) EXM_THROW(29, "Write error : cannot properly close %s", stdoutmark);
|
|
|
|
} else {
|
2016-05-07 20:43:40 +00:00
|
|
|
unsigned u;
|
2016-02-12 14:56:46 +00:00
|
|
|
for (u=0; u<nbFiles; u++) {
|
|
|
|
size_t ifnSize = strlen(inFileNamesTable[u]);
|
|
|
|
if (dfnSize <= ifnSize+suffixSize+1) { free(dstFileName); dfnSize = ifnSize + 20; dstFileName = (char*)malloc(dfnSize); }
|
|
|
|
strcpy(dstFileName, inFileNamesTable[u]);
|
|
|
|
strcat(dstFileName, suffix);
|
2016-06-09 20:59:51 +00:00
|
|
|
missed_files += FIO_compressFilename_dstFile(ress, dstFileName,
|
2016-02-12 14:56:46 +00:00
|
|
|
inFileNamesTable[u], compressionLevel);
|
2016-02-15 19:37:23 +00:00
|
|
|
} }
|
2015-12-17 01:23:58 +00:00
|
|
|
|
|
|
|
/* Close & Free */
|
|
|
|
FIO_freeCResources(ress);
|
|
|
|
free(dstFileName);
|
|
|
|
|
|
|
|
return missed_files;
|
|
|
|
}
|
|
|
|
|
2016-05-07 20:43:40 +00:00
|
|
|
#endif /* #ifndef ZSTD_NOCOMPRESS */
|
2016-04-22 11:59:05 +00:00
|
|
|
|
2015-12-17 01:23:58 +00:00
|
|
|
|
2016-04-22 16:22:30 +00:00
|
|
|
|
|
|
|
#ifndef ZSTD_NODECOMPRESS
|
|
|
|
|
2015-12-17 01:23:58 +00:00
|
|
|
/* **************************************************************************
|
|
|
|
* Decompression
|
|
|
|
****************************************************************************/
|
2015-12-17 19:30:14 +00:00
|
|
|
typedef struct {
|
|
|
|
void* srcBuffer;
|
|
|
|
size_t srcBufferSize;
|
|
|
|
void* dstBuffer;
|
|
|
|
size_t dstBufferSize;
|
|
|
|
void* dictBuffer;
|
|
|
|
size_t dictBufferSize;
|
|
|
|
ZBUFF_DCtx* dctx;
|
2016-02-12 17:33:26 +00:00
|
|
|
FILE* dstFile;
|
2015-12-17 19:30:14 +00:00
|
|
|
} dRess_t;
|
|
|
|
|
|
|
|
static dRess_t FIO_createDResources(const char* dictFileName)
|
|
|
|
{
|
|
|
|
dRess_t ress;
|
|
|
|
|
|
|
|
/* init */
|
|
|
|
ress.dctx = ZBUFF_createDCtx();
|
|
|
|
if (ress.dctx==NULL) EXM_THROW(60, "Can't create ZBUFF decompression context");
|
2015-12-17 01:23:58 +00:00
|
|
|
|
2015-12-17 19:30:14 +00:00
|
|
|
/* Allocate Memory */
|
|
|
|
ress.srcBufferSize = ZBUFF_recommendedDInSize();
|
|
|
|
ress.srcBuffer = malloc(ress.srcBufferSize);
|
|
|
|
ress.dstBufferSize = ZBUFF_recommendedDOutSize();
|
|
|
|
ress.dstBuffer = malloc(ress.dstBufferSize);
|
|
|
|
if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(61, "Allocation error : not enough memory");
|
2015-12-17 01:23:58 +00:00
|
|
|
|
2015-12-17 19:30:14 +00:00
|
|
|
/* dictionary */
|
|
|
|
ress.dictBufferSize = FIO_loadFile(&(ress.dictBuffer), dictFileName);
|
|
|
|
|
|
|
|
return ress;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FIO_freeDResources(dRess_t ress)
|
|
|
|
{
|
2016-05-07 20:43:40 +00:00
|
|
|
size_t const errorCode = ZBUFF_freeDCtx(ress.dctx);
|
2015-12-17 19:30:14 +00:00
|
|
|
if (ZBUFF_isError(errorCode)) EXM_THROW(69, "Error : can't free ZBUFF context resource : %s", ZBUFF_getErrorName(errorCode));
|
|
|
|
free(ress.srcBuffer);
|
|
|
|
free(ress.dstBuffer);
|
|
|
|
free(ress.dictBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-23 14:56:56 +00:00
|
|
|
/** FIO_fwriteSparse() :
|
|
|
|
* @return : storedSkips, to be provided to next call to FIO_fwriteSparse() of LZ4IO_fwriteSparseEnd() */
|
|
|
|
static unsigned FIO_fwriteSparse(FILE* file, const void* buffer, size_t bufferSize, unsigned storedSkips)
|
|
|
|
{
|
|
|
|
const size_t* const bufferT = (const size_t*)buffer; /* Buffer is supposed malloc'ed, hence aligned on size_t */
|
|
|
|
size_t bufferSizeT = bufferSize / sizeof(size_t);
|
|
|
|
const size_t* const bufferTEnd = bufferT + bufferSizeT;
|
|
|
|
const size_t* ptrT = bufferT;
|
|
|
|
static const size_t segmentSizeT = (32 KB) / sizeof(size_t); /* 0-test re-attempted every 32 KB */
|
|
|
|
|
|
|
|
if (!g_sparseFileSupport) { /* normal write */
|
|
|
|
size_t const sizeCheck = fwrite(buffer, 1, bufferSize, file);
|
|
|
|
if (sizeCheck != bufferSize) EXM_THROW(70, "Write error : cannot write decoded block");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* avoid int overflow */
|
|
|
|
if (storedSkips > 1 GB) {
|
|
|
|
int const seekResult = fseek(file, 1 GB, SEEK_CUR);
|
|
|
|
if (seekResult != 0) EXM_THROW(71, "1 GB skip error (sparse file support)");
|
|
|
|
storedSkips -= 1 GB;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (ptrT < bufferTEnd) {
|
|
|
|
size_t seg0SizeT = segmentSizeT;
|
|
|
|
size_t nb0T;
|
|
|
|
|
|
|
|
/* count leading zeros */
|
|
|
|
if (seg0SizeT > bufferSizeT) seg0SizeT = bufferSizeT;
|
|
|
|
bufferSizeT -= seg0SizeT;
|
|
|
|
for (nb0T=0; (nb0T < seg0SizeT) && (ptrT[nb0T] == 0); nb0T++) ;
|
|
|
|
storedSkips += (unsigned)(nb0T * sizeof(size_t));
|
|
|
|
|
|
|
|
if (nb0T != seg0SizeT) { /* not all 0s */
|
|
|
|
int const seekResult = fseek(file, storedSkips, SEEK_CUR);
|
|
|
|
if (seekResult) EXM_THROW(72, "Sparse skip error ; try --no-sparse");
|
|
|
|
storedSkips = 0;
|
|
|
|
seg0SizeT -= nb0T;
|
|
|
|
ptrT += nb0T;
|
|
|
|
{ size_t const sizeCheck = fwrite(ptrT, sizeof(size_t), seg0SizeT, file);
|
|
|
|
if (sizeCheck != seg0SizeT) EXM_THROW(73, "Write error : cannot write decoded block");
|
|
|
|
} }
|
|
|
|
ptrT += seg0SizeT;
|
|
|
|
}
|
|
|
|
|
|
|
|
{ static size_t const maskT = sizeof(size_t)-1;
|
|
|
|
if (bufferSize & maskT) { /* size not multiple of sizeof(size_t) : implies end of block */
|
|
|
|
const char* const restStart = (const char*)bufferTEnd;
|
|
|
|
const char* restPtr = restStart;
|
|
|
|
size_t restSize = bufferSize & maskT;
|
|
|
|
const char* const restEnd = restStart + restSize;
|
|
|
|
for ( ; (restPtr < restEnd) && (*restPtr == 0); restPtr++) ;
|
|
|
|
storedSkips += (unsigned) (restPtr - restStart);
|
|
|
|
if (restPtr != restEnd) {
|
|
|
|
int seekResult = fseek(file, storedSkips, SEEK_CUR);
|
|
|
|
if (seekResult) EXM_THROW(74, "Sparse skip error ; try --no-sparse");
|
|
|
|
storedSkips = 0;
|
|
|
|
{ size_t const sizeCheck = fwrite(restPtr, 1, restEnd - restPtr, file);
|
|
|
|
if (sizeCheck != (size_t)(restEnd - restPtr)) EXM_THROW(75, "Write error : cannot write decoded end of block");
|
|
|
|
} } } }
|
|
|
|
|
|
|
|
return storedSkips;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FIO_fwriteSparseEnd(FILE* file, unsigned storedSkips)
|
|
|
|
{
|
|
|
|
if (storedSkips-->0) { /* implies g_sparseFileSupport>0 */
|
|
|
|
int const seekResult = fseek(file, storedSkips, SEEK_CUR);
|
|
|
|
if (seekResult != 0) EXM_THROW(69, "Final skip error (sparse file)\n");
|
|
|
|
{ const char lastZeroByte[1] = { 0 };
|
|
|
|
size_t const sizeCheck = fwrite(lastZeroByte, 1, 1, file);
|
|
|
|
if (sizeCheck != 1) EXM_THROW(69, "Write error : cannot write last zero\n");
|
|
|
|
} }
|
|
|
|
}
|
|
|
|
|
2016-03-15 11:56:03 +00:00
|
|
|
/** FIO_decompressFrame() :
|
|
|
|
@return : size of decoded frame
|
|
|
|
*/
|
2015-12-17 19:30:14 +00:00
|
|
|
unsigned long long FIO_decompressFrame(dRess_t ress,
|
|
|
|
FILE* foutput, FILE* finput, size_t alreadyLoaded)
|
2015-01-24 00:58:16 +00:00
|
|
|
{
|
2015-11-25 13:42:45 +00:00
|
|
|
U64 frameSize = 0;
|
2016-03-15 11:56:03 +00:00
|
|
|
size_t readSize;
|
2016-05-23 14:56:56 +00:00
|
|
|
U32 storedSkips = 0;
|
2015-01-24 00:58:16 +00:00
|
|
|
|
2016-01-26 14:58:49 +00:00
|
|
|
ZBUFF_decompressInitDictionary(ress.dctx, ress.dictBuffer, ress.dictBufferSize);
|
2016-03-15 11:56:03 +00:00
|
|
|
|
2016-05-10 12:14:19 +00:00
|
|
|
/* Header loading (optional, saves one loop) */
|
2016-06-05 22:26:38 +00:00
|
|
|
{ size_t const toLoad = 9 - alreadyLoaded; /* assumption : 9 >= alreadyLoaded */
|
2016-05-10 12:14:19 +00:00
|
|
|
size_t const loadedSize = fread(((char*)ress.srcBuffer) + alreadyLoaded, 1, toLoad, finput);
|
2016-05-10 03:56:09 +00:00
|
|
|
readSize = alreadyLoaded + loadedSize;
|
2016-03-15 11:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Main decompression Loop */
|
2016-02-02 13:36:49 +00:00
|
|
|
while (1) {
|
2015-12-17 19:30:14 +00:00
|
|
|
size_t inSize=readSize, decodedSize=ress.dstBufferSize;
|
2016-05-10 03:37:43 +00:00
|
|
|
size_t const toRead = ZBUFF_decompressContinue(ress.dctx, ress.dstBuffer, &decodedSize, ress.srcBuffer, &inSize);
|
2015-11-25 13:42:45 +00:00
|
|
|
if (ZBUFF_isError(toRead)) EXM_THROW(36, "Decoding error : %s", ZBUFF_getErrorName(toRead));
|
|
|
|
readSize -= inSize;
|
|
|
|
|
|
|
|
/* Write block */
|
2016-05-23 14:56:56 +00:00
|
|
|
storedSkips = FIO_fwriteSparse(foutput, ress.dstBuffer, decodedSize, storedSkips);
|
2015-11-25 13:42:45 +00:00
|
|
|
frameSize += decodedSize;
|
|
|
|
DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(frameSize>>20) );
|
|
|
|
|
2016-03-15 11:56:03 +00:00
|
|
|
if (toRead == 0) break; /* end of frame */
|
2015-12-17 19:30:14 +00:00
|
|
|
if (readSize) EXM_THROW(38, "Decoding error : should consume entire input");
|
2015-01-24 00:58:16 +00:00
|
|
|
|
|
|
|
/* Fill input buffer */
|
2015-12-17 19:30:14 +00:00
|
|
|
if (toRead > ress.srcBufferSize) EXM_THROW(34, "too large block");
|
|
|
|
readSize = fread(ress.srcBuffer, 1, toRead, finput);
|
2016-03-15 11:56:03 +00:00
|
|
|
if (readSize != toRead)
|
|
|
|
EXM_THROW(35, "Read error");
|
2015-01-24 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 14:56:56 +00:00
|
|
|
FIO_fwriteSparseEnd(foutput, storedSkips);
|
|
|
|
|
2015-11-25 13:42:45 +00:00
|
|
|
return frameSize;
|
2015-09-10 22:26:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-23 17:46:47 +00:00
|
|
|
/** FIO_passThrough() : just copy input into output, for compatibility with gzip -df mode
|
|
|
|
@return : 0 (no error) */
|
2016-05-23 22:52:14 +00:00
|
|
|
static unsigned FIO_passThrough(FILE* foutput, FILE* finput, void* buffer, size_t bufferSize)
|
2016-05-23 17:46:47 +00:00
|
|
|
{
|
|
|
|
size_t const blockSize = MIN (64 KB, bufferSize);
|
2016-05-23 22:52:14 +00:00
|
|
|
size_t readFromInput = 1;
|
2016-05-23 17:46:47 +00:00
|
|
|
unsigned storedSkips = 0;
|
|
|
|
|
|
|
|
/* assumption : first 4 bytes already loaded (magic number detection), and stored within buffer */
|
|
|
|
{ size_t const sizeCheck = fwrite(buffer, 1, 4, foutput);
|
|
|
|
if (sizeCheck != 4) EXM_THROW(50, "Pass-through write error"); }
|
|
|
|
|
2016-05-23 22:52:14 +00:00
|
|
|
while (readFromInput) {
|
|
|
|
readFromInput = fread(buffer, 1, blockSize, finput);
|
|
|
|
storedSkips = FIO_fwriteSparse(foutput, buffer, readFromInput, storedSkips);
|
2016-05-23 17:46:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FIO_fwriteSparseEnd(foutput, storedSkips);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-12 17:33:26 +00:00
|
|
|
/** FIO_decompressSrcFile() :
|
|
|
|
Decompression `srcFileName` into `ress.dstFile`
|
|
|
|
@return : 0 : OK
|
|
|
|
1 : operation not started
|
|
|
|
*/
|
|
|
|
static int FIO_decompressSrcFile(dRess_t ress, const char* srcFileName)
|
2015-10-18 21:18:32 +00:00
|
|
|
{
|
2015-12-17 19:30:14 +00:00
|
|
|
unsigned long long filesize = 0;
|
2016-05-07 20:43:40 +00:00
|
|
|
FILE* const dstFile = ress.dstFile;
|
2016-06-09 20:59:51 +00:00
|
|
|
FILE* srcFile;
|
|
|
|
|
|
|
|
if (UTIL_isDirectory(srcFileName)) {
|
|
|
|
DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
srcFile = FIO_openSrcFile(srcFileName);
|
2016-02-12 17:33:26 +00:00
|
|
|
if (srcFile==0) return 1;
|
2015-11-25 13:42:45 +00:00
|
|
|
|
2015-10-18 21:18:32 +00:00
|
|
|
/* for each frame */
|
2016-02-02 13:36:49 +00:00
|
|
|
for ( ; ; ) {
|
2015-11-30 10:53:11 +00:00
|
|
|
/* check magic number -> version */
|
2016-04-07 17:35:23 +00:00
|
|
|
size_t const toRead = 4;
|
|
|
|
size_t const sizeCheck = fread(ress.srcBuffer, (size_t)1, toRead, srcFile);
|
2015-11-30 10:53:11 +00:00
|
|
|
if (sizeCheck==0) break; /* no more input */
|
2016-02-12 17:33:26 +00:00
|
|
|
if (sizeCheck != toRead) EXM_THROW(31, "zstd: %s read error : cannot read header", srcFileName);
|
2016-05-07 20:43:40 +00:00
|
|
|
{ U32 const magic = MEM_readLE32(ress.srcBuffer);
|
|
|
|
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
|
|
|
|
if (ZSTD_isLegacy(magic)) {
|
2016-05-08 06:23:51 +00:00
|
|
|
filesize += FIO_decompressLegacyFrame(dstFile, srcFile, ress.dictBuffer, ress.dictBufferSize, magic);
|
2016-05-07 20:43:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-05-10 03:37:43 +00:00
|
|
|
#endif
|
2016-05-31 10:43:46 +00:00
|
|
|
if (((magic & 0xFFFFFFF0U) != ZSTD_MAGIC_SKIPPABLE_START) && (magic != ZSTD_MAGICNUMBER)) {
|
2016-05-23 17:46:47 +00:00
|
|
|
if (g_overwrite) /* -df : pass-through mode */
|
|
|
|
return FIO_passThrough(dstFile, srcFile, ress.srcBuffer, ress.srcBufferSize);
|
|
|
|
else {
|
|
|
|
DISPLAYLEVEL(1, "zstd: %s: not in zstd format \n", srcFileName);
|
|
|
|
return 1;
|
|
|
|
} } }
|
2015-12-17 19:30:14 +00:00
|
|
|
filesize += FIO_decompressFrame(ress, dstFile, srcFile, toRead);
|
2015-10-18 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
2015-12-17 19:30:14 +00:00
|
|
|
/* Final Status */
|
2015-10-18 21:18:32 +00:00
|
|
|
DISPLAYLEVEL(2, "\r%79s\r", "");
|
2016-05-31 00:29:45 +00:00
|
|
|
DISPLAYLEVEL(2, "%-20.20s: %llu bytes \n", srcFileName, filesize);
|
2015-10-18 21:18:32 +00:00
|
|
|
|
2015-12-17 19:30:14 +00:00
|
|
|
/* Close */
|
|
|
|
fclose(srcFile);
|
2016-06-09 20:59:51 +00:00
|
|
|
if (g_removeSrcFile) remove(srcFileName);
|
2016-02-12 17:33:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** FIO_decompressFile_extRess() :
|
|
|
|
decompress `srcFileName` into `dstFileName`
|
|
|
|
@return : 0 : OK
|
|
|
|
1 : operation aborted (src not available, dst already taken, etc.)
|
|
|
|
*/
|
2016-06-09 20:59:51 +00:00
|
|
|
static int FIO_decompressDstFile(dRess_t ress,
|
2016-02-12 17:33:26 +00:00
|
|
|
const char* dstFileName, const char* srcFileName)
|
|
|
|
{
|
2016-03-14 05:24:46 +00:00
|
|
|
int result;
|
2016-02-12 17:33:26 +00:00
|
|
|
ress.dstFile = FIO_openDstFile(dstFileName);
|
|
|
|
if (ress.dstFile==0) return 1;
|
2015-12-17 19:30:14 +00:00
|
|
|
|
2016-03-14 05:24:46 +00:00
|
|
|
result = FIO_decompressSrcFile(ress, srcFileName);
|
2016-02-12 17:33:26 +00:00
|
|
|
|
|
|
|
if (fclose(ress.dstFile)) EXM_THROW(38, "Write error : cannot properly close %s", dstFileName);
|
2016-06-09 20:59:51 +00:00
|
|
|
if (result != 0) remove(dstFileName);
|
2016-03-14 05:24:46 +00:00
|
|
|
return result;
|
2015-10-18 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-17 19:30:14 +00:00
|
|
|
int FIO_decompressFilename(const char* dstFileName, const char* srcFileName,
|
|
|
|
const char* dictFileName)
|
|
|
|
{
|
|
|
|
int missingFiles = 0;
|
|
|
|
dRess_t ress = FIO_createDResources(dictFileName);
|
|
|
|
|
2016-06-09 20:59:51 +00:00
|
|
|
missingFiles += FIO_decompressDstFile(ress, dstFileName, srcFileName);
|
2015-12-17 19:30:14 +00:00
|
|
|
|
|
|
|
FIO_freeDResources(ress);
|
|
|
|
return missingFiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define MAXSUFFIXSIZE 8
|
|
|
|
int FIO_decompressMultipleFilenames(const char** srcNamesTable, unsigned nbFiles,
|
|
|
|
const char* suffix,
|
|
|
|
const char* dictFileName)
|
|
|
|
{
|
|
|
|
int skippedFiles = 0;
|
|
|
|
int missingFiles = 0;
|
2016-05-10 03:37:43 +00:00
|
|
|
dRess_t ress = FIO_createDResources(dictFileName);
|
2015-12-17 19:30:14 +00:00
|
|
|
|
2016-02-15 18:33:16 +00:00
|
|
|
if (!strcmp(suffix, stdoutmark) || !strcmp(suffix, nulmark)) {
|
2016-05-07 20:43:40 +00:00
|
|
|
unsigned u;
|
2016-02-15 18:33:16 +00:00
|
|
|
ress.dstFile = FIO_openDstFile(suffix);
|
|
|
|
if (ress.dstFile == 0) EXM_THROW(71, "cannot open %s", suffix);
|
|
|
|
for (u=0; u<nbFiles; u++)
|
|
|
|
missingFiles += FIO_decompressSrcFile(ress, srcNamesTable[u]);
|
|
|
|
if (fclose(ress.dstFile)) EXM_THROW(39, "Write error : cannot properly close %s", stdoutmark);
|
|
|
|
} else {
|
2016-05-10 03:37:43 +00:00
|
|
|
size_t const suffixSize = suffix ? strlen(suffix) : 0;
|
|
|
|
size_t dfnSize = FNSPACE;
|
2016-05-07 20:43:40 +00:00
|
|
|
unsigned u;
|
2016-05-10 03:37:43 +00:00
|
|
|
char* dstFileName = (char*)malloc(FNSPACE);
|
|
|
|
if (dstFileName==NULL) EXM_THROW(70, "not enough memory for dstFileName");
|
2016-02-12 17:33:26 +00:00
|
|
|
for (u=0; u<nbFiles; u++) { /* create dstFileName */
|
2016-05-10 03:37:43 +00:00
|
|
|
const char* const srcFileName = srcNamesTable[u];
|
|
|
|
size_t const sfnSize = strlen(srcFileName);
|
|
|
|
const char* const suffixPtr = srcFileName + sfnSize - suffixSize;
|
2016-02-15 18:33:16 +00:00
|
|
|
if (dfnSize+suffixSize <= sfnSize+1) {
|
|
|
|
free(dstFileName);
|
|
|
|
dfnSize = sfnSize + 20;
|
|
|
|
dstFileName = (char*)malloc(dfnSize);
|
|
|
|
if (dstFileName==NULL) EXM_THROW(71, "not enough memory for dstFileName");
|
|
|
|
}
|
|
|
|
if (sfnSize <= suffixSize || strcmp(suffixPtr, suffix) != 0) {
|
2016-02-12 17:33:26 +00:00
|
|
|
DISPLAYLEVEL(1, "zstd: %s: unknown suffix (%4s expected) -- ignored \n", srcFileName, suffix);
|
|
|
|
skippedFiles++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
memcpy(dstFileName, srcFileName, sfnSize - suffixSize);
|
|
|
|
dstFileName[sfnSize-suffixSize] = '\0';
|
|
|
|
|
2016-06-09 20:59:51 +00:00
|
|
|
missingFiles += FIO_decompressDstFile(ress, dstFileName, srcFileName);
|
2016-05-10 03:37:43 +00:00
|
|
|
}
|
|
|
|
free(dstFileName);
|
|
|
|
}
|
2015-12-17 19:30:14 +00:00
|
|
|
|
|
|
|
FIO_freeDResources(ress);
|
|
|
|
return missingFiles + skippedFiles;
|
|
|
|
}
|
2016-02-15 18:33:16 +00:00
|
|
|
|
2016-05-10 03:37:43 +00:00
|
|
|
#endif /* #ifndef ZSTD_NODECOMPRESS */
|