Remove a bunch of global variables that tracked settings for the IO module, and move them in to a struct

This commit is contained in:
Tim Zakian 2019-01-10 15:27:47 -08:00
parent d4a40c6e39
commit 5822e667cc
3 changed files with 209 additions and 166 deletions

View File

@ -92,7 +92,7 @@ static unsigned displayLevel = 2; /* 0 : no display ; 1: errors only ; 2 : dow
***************************************/
#define DEFAULT_COMPRESSOR LZ4IO_compressFilename
#define DEFAULT_DECOMPRESSOR LZ4IO_decompressFilename
int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output_filename, int compressionlevel); /* hidden function */
int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* prefs, const char* input_filename, const char* output_filename, int compressionlevel); /* hidden function */
/*-***************************
@ -318,9 +318,10 @@ int main(int argc, const char** argv)
char* dynNameSpace = NULL;
const char** inFileNames = (const char**) calloc(argc, sizeof(char*));
unsigned ifnIdx=0;
LZ4IO_prefs_t* prefs = LZ4IO_defaultPreferences();
const char nullOutput[] = NULL_OUTPUT;
const char extension[] = LZ4_EXTENSION;
size_t blockSize = LZ4IO_setBlockSizeID(LZ4_BLOCKSIZEID_DEFAULT);
size_t blockSize = LZ4IO_setBlockSizeID(prefs, LZ4_BLOCKSIZEID_DEFAULT);
const char* const exeName = lastNameFromPath(argv[0]);
#ifdef UTIL_HAS_CREATEFILELIST
const char** extendedFileList = NULL;
@ -334,14 +335,14 @@ int main(int argc, const char** argv)
return 1;
}
inFileNames[0] = stdinmark;
LZ4IO_setOverwrite(0);
LZ4IO_setOverwrite(prefs, 0);
/* predefined behaviors, based on binary/link name */
if (exeNameMatch(exeName, LZ4CAT)) {
mode = om_decompress;
LZ4IO_setOverwrite(1);
LZ4IO_setPassThrough(1);
LZ4IO_setRemoveSrcFile(0);
LZ4IO_setOverwrite(prefs, 1);
LZ4IO_setPassThrough(prefs, 1);
LZ4IO_setRemoveSrcFile(prefs, 0);
forceStdout=1;
output_filename=stdoutmark;
displayLevel=1;
@ -373,23 +374,23 @@ int main(int argc, const char** argv)
|| (!strcmp(argument, "--uncompress"))) { mode = om_decompress; continue; }
if (!strcmp(argument, "--multiple")) { multiple_inputs = 1; continue; }
if (!strcmp(argument, "--test")) { mode = om_test; continue; }
if (!strcmp(argument, "--force")) { LZ4IO_setOverwrite(1); continue; }
if (!strcmp(argument, "--no-force")) { LZ4IO_setOverwrite(0); continue; }
if (!strcmp(argument, "--force")) { LZ4IO_setOverwrite(prefs, 1); continue; }
if (!strcmp(argument, "--no-force")) { LZ4IO_setOverwrite(prefs, 0); continue; }
if ((!strcmp(argument, "--stdout"))
|| (!strcmp(argument, "--to-stdout"))) { forceStdout=1; output_filename=stdoutmark; continue; }
if (!strcmp(argument, "--frame-crc")) { LZ4IO_setStreamChecksumMode(1); continue; }
if (!strcmp(argument, "--no-frame-crc")) { LZ4IO_setStreamChecksumMode(0); continue; }
if (!strcmp(argument, "--content-size")) { LZ4IO_setContentSize(1); continue; }
if (!strcmp(argument, "--no-content-size")) { LZ4IO_setContentSize(0); continue; }
if (!strcmp(argument, "--sparse")) { LZ4IO_setSparseFile(2); continue; }
if (!strcmp(argument, "--no-sparse")) { LZ4IO_setSparseFile(0); continue; }
if (!strcmp(argument, "--favor-decSpeed")) { LZ4IO_favorDecSpeed(1); continue; }
if (!strcmp(argument, "--frame-crc")) { LZ4IO_setStreamChecksumMode(prefs, 1); continue; }
if (!strcmp(argument, "--no-frame-crc")) { LZ4IO_setStreamChecksumMode(prefs, 0); continue; }
if (!strcmp(argument, "--content-size")) { LZ4IO_setContentSize(prefs, 1); continue; }
if (!strcmp(argument, "--no-content-size")) { LZ4IO_setContentSize(prefs, 0); continue; }
if (!strcmp(argument, "--sparse")) { LZ4IO_setSparseFile(prefs, 2); continue; }
if (!strcmp(argument, "--no-sparse")) { LZ4IO_setSparseFile(prefs, 0); continue; }
if (!strcmp(argument, "--favor-decSpeed")) { LZ4IO_favorDecSpeed(prefs, 1); continue; }
if (!strcmp(argument, "--verbose")) { displayLevel++; continue; }
if (!strcmp(argument, "--quiet")) { if (displayLevel) displayLevel--; continue; }
if (!strcmp(argument, "--version")) { DISPLAY(WELCOME_MESSAGE); return 0; }
if (!strcmp(argument, "--help")) { usage_advanced(exeName); goto _cleanup; }
if (!strcmp(argument, "--keep")) { LZ4IO_setRemoveSrcFile(0); continue; } /* keep source file (default) */
if (!strcmp(argument, "--rm")) { LZ4IO_setRemoveSrcFile(1); continue; }
if (!strcmp(argument, "--keep")) { LZ4IO_setRemoveSrcFile(prefs, 0); continue; } /* keep source file (default) */
if (!strcmp(argument, "--rm")) { LZ4IO_setRemoveSrcFile(prefs, 1); continue; }
if (longCommandWArg(&argument, "--fast")) {
/* Parse optional acceleration factor */
if (*argument == '=') {
@ -420,7 +421,7 @@ int main(int argc, const char** argv)
if (!strcmp(argument, "c1")) { cLevel=9; argument++; continue; } /* -c1 (high compression) */
if (!strcmp(argument, "c2")) { cLevel=12; argument++; continue; } /* -c2 (very high compression) */
if (!strcmp(argument, "hc")) { cLevel=12; argument++; continue; } /* -hc (very high compression) */
if (!strcmp(argument, "y")) { LZ4IO_setOverwrite(1); continue; } /* -y (answer 'yes' to overwrite permission) */
if (!strcmp(argument, "y")) { LZ4IO_setOverwrite(prefs, 1); continue; } /* -y (answer 'yes' to overwrite permission) */
}
if ((*argument>='0') && (*argument<='9')) {
@ -472,14 +473,14 @@ int main(int argc, const char** argv)
case 'c':
forceStdout=1;
output_filename=stdoutmark;
LZ4IO_setPassThrough(1);
LZ4IO_setPassThrough(prefs, 1);
break;
/* Test integrity */
case 't': mode = om_test; break;
/* Overwrite */
case 'f': LZ4IO_setOverwrite(1); break;
case 'f': LZ4IO_setOverwrite(prefs, 1); break;
/* Verbose mode */
case 'v': displayLevel++; break;
@ -488,7 +489,7 @@ int main(int argc, const char** argv)
case 'q': if (displayLevel) displayLevel--; break;
/* keep source file (default anyway, so useless) (for xz/lzma compatibility) */
case 'k': LZ4IO_setRemoveSrcFile(0); break;
case 'k': LZ4IO_setRemoveSrcFile(prefs, 0); break;
/* Modify Block Properties */
case 'B':
@ -496,8 +497,8 @@ int main(int argc, const char** argv)
int exitBlockProperties=0;
switch(argument[1])
{
case 'D': LZ4IO_setBlockMode(LZ4IO_blockLinked); argument++; break;
case 'X': LZ4IO_setBlockChecksumMode(1); argument ++; break; /* disabled by default */
case 'D': LZ4IO_setBlockMode(prefs, LZ4IO_blockLinked); argument++; break;
case 'X': LZ4IO_setBlockChecksumMode(prefs, 1); argument ++; break; /* disabled by default */
default :
if (argument[1] < '0' || argument[1] > '9') {
exitBlockProperties=1;
@ -509,12 +510,12 @@ int main(int argc, const char** argv)
argument--;
if (B < 4) badusage(exeName);
if (B <= 7) {
blockSize = LZ4IO_setBlockSizeID(B);
blockSize = LZ4IO_setBlockSizeID(prefs, B);
BMK_setBlockSize(blockSize);
DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10));
} else {
if (B < 32) badusage(exeName);
blockSize = LZ4IO_setBlockSize(B);
blockSize = LZ4IO_setBlockSize(prefs, B);
BMK_setBlockSize(blockSize);
if (blockSize >= 1024) {
DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10));
@ -623,7 +624,7 @@ int main(int argc, const char** argv)
}
if (mode == om_test) {
LZ4IO_setTestMode(1);
LZ4IO_setTestMode(prefs, 1);
output_filename = nulmark;
mode = om_decompress; /* defer to decompress */
}
@ -633,7 +634,7 @@ int main(int argc, const char** argv)
DISPLAYLEVEL(1, "refusing to read from a console\n");
exit(1);
}
LZ4IO_setDictionaryFilename(dictionary_filename);
LZ4IO_setDictionaryFilename(prefs, dictionary_filename);
}
/* compress or decompress */
@ -699,18 +700,18 @@ int main(int argc, const char** argv)
if (ifnIdx == 0) multiple_inputs = 0;
if (mode == om_decompress) {
if (multiple_inputs)
operationResult = LZ4IO_decompressMultipleFilenames(inFileNames, ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION);
operationResult = LZ4IO_decompressMultipleFilenames(prefs, inFileNames, ifnIdx, !strcmp(output_filename,stdoutmark) ? stdoutmark : LZ4_EXTENSION);
else
operationResult = DEFAULT_DECOMPRESSOR(input_filename, output_filename);
operationResult = DEFAULT_DECOMPRESSOR(prefs, input_filename, output_filename);
} else { /* compression is default action */
if (legacy_format) {
DISPLAYLEVEL(3, "! Generating LZ4 Legacy format (deprecated) ! \n");
LZ4IO_compressFilename_Legacy(input_filename, output_filename, cLevel);
LZ4IO_compressFilename_Legacy(prefs, input_filename, output_filename, cLevel);
} else {
if (multiple_inputs)
operationResult = LZ4IO_compressMultipleFilenames(inFileNames, ifnIdx, LZ4_EXTENSION, cLevel);
operationResult = LZ4IO_compressMultipleFilenames(prefs, inFileNames, ifnIdx, LZ4_EXTENSION, cLevel);
else
operationResult = DEFAULT_COMPRESSOR(input_filename, output_filename, cLevel);
operationResult = DEFAULT_COMPRESSOR(prefs, input_filename, output_filename, cLevel);
}
}
@ -723,6 +724,7 @@ _cleanup:
inFileNames = NULL;
}
#endif
LZ4IO_freePreferences(prefs);
free((void*)inFileNames);
return operationResult;
}

View File

@ -107,20 +107,23 @@ static clock_t g_time = 0;
/**************************************
* Local Parameters
**************************************/
static int g_passThrough = 0;
static int g_overwrite = 1;
static int g_testMode = 0;
static int g_blockSizeId = LZ4IO_BLOCKSIZEID_DEFAULT;
static size_t g_blockSize = 0;
static int g_blockChecksum = 0;
static int g_streamChecksum = 1;
static int g_blockIndependence = 1;
static int g_sparseFileSupport = 1;
static int g_contentSizeFlag = 0;
static int g_useDictionary = 0;
static unsigned g_favorDecSpeed = 0;
static const char* g_dictionaryFilename = NULL;
struct LZ4IO_prefs_s {
int passThrough;
int overwrite;
int testMode;
int blockSizeId;
size_t blockSize;
int blockChecksum;
int streamChecksum;
int blockIndependence;
int sparseFileSupport;
int contentSizeFlag;
int useDictionary;
unsigned favorDecSpeed;
char const* dictionaryFilename;
U32 removeSrcFile;
};
/**************************************
* Exceptions
@ -152,81 +155,108 @@ static const char* g_dictionaryFilename = NULL;
/* ****************** Parameters ******************** */
/* ************************************************** */
int LZ4IO_setDictionaryFilename(const char* dictionaryFilename) {
g_dictionaryFilename = dictionaryFilename;
g_useDictionary = dictionaryFilename != NULL;
return g_useDictionary;
LZ4IO_prefs_t* LZ4IO_defaultPreferences(void)
{
LZ4IO_prefs_t* ret = malloc(sizeof(LZ4IO_prefs_t));
ret->passThrough = 0;
ret->overwrite = 1;
ret->testMode = 0;
ret->blockSizeId = LZ4IO_BLOCKSIZEID_DEFAULT;
ret->blockSize = 0;
ret->blockChecksum = 0;
ret->streamChecksum = 1;
ret->blockIndependence = 1;
ret->sparseFileSupport = 1;
ret->contentSizeFlag = 0;
ret->useDictionary = 0;
ret->favorDecSpeed = 0;
ret->dictionaryFilename = NULL;
ret->removeSrcFile = 0;
return ret;
}
void LZ4IO_freePreferences(LZ4IO_prefs_t* prefs)
{
free(prefs);
}
int LZ4IO_setDictionaryFilename(LZ4IO_prefs_t* prefs, const char* dictionaryFilename)
{
prefs->dictionaryFilename = dictionaryFilename;
prefs->useDictionary = dictionaryFilename != NULL;
return prefs->useDictionary;
}
/* Default setting : passThrough = 0; return : passThrough mode (0/1) */
int LZ4IO_setPassThrough(int yes)
int LZ4IO_setPassThrough(LZ4IO_prefs_t* prefs, int yes)
{
g_passThrough = (yes!=0);
return g_passThrough;
prefs->passThrough = (yes!=0);
return prefs->passThrough;
}
/* Default setting : overwrite = 1; return : overwrite mode (0/1) */
int LZ4IO_setOverwrite(int yes)
int LZ4IO_setOverwrite(LZ4IO_prefs_t* prefs, int yes)
{
g_overwrite = (yes!=0);
return g_overwrite;
prefs->overwrite = (yes!=0);
return prefs->overwrite;
}
/* Default setting : testMode = 0; return : testMode (0/1) */
int LZ4IO_setTestMode(int yes)
int LZ4IO_setTestMode(LZ4IO_prefs_t* prefs, int yes)
{
g_testMode = (yes!=0);
return g_testMode;
prefs->testMode = (yes!=0);
return prefs->testMode;
}
/* blockSizeID : valid values : 4-5-6-7 */
size_t LZ4IO_setBlockSizeID(unsigned bsid)
size_t LZ4IO_setBlockSizeID(LZ4IO_prefs_t* prefs, unsigned bsid)
{
static const size_t blockSizeTable[] = { 64 KB, 256 KB, 1 MB, 4 MB };
static const unsigned minBlockSizeID = 4;
static const unsigned maxBlockSizeID = 7;
if ((bsid < minBlockSizeID) || (bsid > maxBlockSizeID)) return 0;
g_blockSizeId = bsid;
g_blockSize = blockSizeTable[g_blockSizeId-minBlockSizeID];
return g_blockSize;
prefs->blockSizeId = bsid;
prefs->blockSize = blockSizeTable[prefs->blockSizeId-minBlockSizeID];
return prefs->blockSize;
}
size_t LZ4IO_setBlockSize(size_t blockSize)
size_t LZ4IO_setBlockSize(LZ4IO_prefs_t* prefs, size_t blockSize)
{
static const size_t minBlockSize = 32;
static const size_t maxBlockSize = 4 MB;
unsigned bsid = 0;
if (blockSize < minBlockSize) blockSize = minBlockSize;
if (blockSize > maxBlockSize) blockSize = maxBlockSize;
g_blockSize = blockSize;
prefs->blockSize = blockSize;
blockSize--;
/* find which of { 64k, 256k, 1MB, 4MB } is closest to blockSize */
while (blockSize >>= 2)
bsid++;
if (bsid < 7) bsid = 7;
g_blockSizeId = bsid-3;
return g_blockSize;
prefs->blockSizeId = bsid-3;
return prefs->blockSize;
}
int LZ4IO_setBlockMode(LZ4IO_blockMode_t blockMode)
int LZ4IO_setBlockMode(LZ4IO_prefs_t* prefs, LZ4IO_blockMode_t blockMode)
{
g_blockIndependence = (blockMode == LZ4IO_blockIndependent);
return g_blockIndependence;
prefs->blockIndependence = (blockMode == LZ4IO_blockIndependent);
return prefs->blockIndependence;
}
/* Default setting : no block checksum */
int LZ4IO_setBlockChecksumMode(int enable)
int LZ4IO_setBlockChecksumMode(LZ4IO_prefs_t* prefs, int enable)
{
g_blockChecksum = (enable != 0);
return g_blockChecksum;
prefs->blockChecksum = (enable != 0);
return prefs->blockChecksum;
}
/* Default setting : checksum enabled */
int LZ4IO_setStreamChecksumMode(int enable)
int LZ4IO_setStreamChecksumMode(LZ4IO_prefs_t* prefs, int enable)
{
g_streamChecksum = (enable != 0);
return g_streamChecksum;
prefs->streamChecksum = (enable != 0);
return prefs->streamChecksum;
}
/* Default setting : 0 (no notification) */
@ -237,27 +267,29 @@ int LZ4IO_setNotificationLevel(int level)
}
/* Default setting : 0 (disabled) */
int LZ4IO_setSparseFile(int enable)
int LZ4IO_setSparseFile(LZ4IO_prefs_t* prefs, int enable)
{
g_sparseFileSupport = (enable!=0);
return g_sparseFileSupport;
prefs->sparseFileSupport = (enable!=0);
return prefs->sparseFileSupport;
}
/* Default setting : 0 (disabled) */
int LZ4IO_setContentSize(int enable)
int LZ4IO_setContentSize(LZ4IO_prefs_t* prefs, int enable)
{
g_contentSizeFlag = (enable!=0);
return g_contentSizeFlag;
prefs->contentSizeFlag = (enable!=0);
return prefs->contentSizeFlag;
}
/* Default setting : 0 (disabled) */
void LZ4IO_favorDecSpeed(int favor)
void LZ4IO_favorDecSpeed(LZ4IO_prefs_t* prefs, int favor)
{
g_favorDecSpeed = (favor!=0);
prefs->favorDecSpeed = (favor!=0);
}
static U32 g_removeSrcFile = 0;
void LZ4IO_setRemoveSrcFile(unsigned flag) { g_removeSrcFile = (flag>0); }
void LZ4IO_setRemoveSrcFile(LZ4IO_prefs_t* prefs, unsigned flag)
{
prefs->removeSrcFile = (flag>0);
}
@ -292,7 +324,7 @@ static FILE* LZ4IO_openSrcFile(const char* srcFileName)
/** FIO_openDstFile() :
* condition : `dstFileName` must be non-NULL.
* @result : FILE* to `dstFileName`, or NULL if it fails */
static FILE* LZ4IO_openDstFile(const char* dstFileName)
static FILE* LZ4IO_openDstFile(LZ4IO_prefs_t* prefs, const char* dstFileName)
{
FILE* f;
@ -300,12 +332,12 @@ static FILE* LZ4IO_openDstFile(const char* dstFileName)
DISPLAYLEVEL(4,"Using stdout for output\n");
f = stdout;
SET_BINARY_MODE(stdout);
if (g_sparseFileSupport==1) {
g_sparseFileSupport = 0;
if (prefs->sparseFileSupport==1) {
prefs->sparseFileSupport = 0;
DISPLAYLEVEL(4, "Sparse File Support is automatically disabled on stdout ; try --sparse \n");
}
} else {
if (!g_overwrite && strcmp (dstFileName, nulmark)) { /* Check if destination file already exists */
if (!prefs->overwrite && strcmp (dstFileName, nulmark)) { /* Check if destination file already exists */
f = fopen( dstFileName, "rb" );
if (f != NULL) { /* dest exists, prompt for overwrite authorization */
fclose(f);
@ -326,7 +358,7 @@ static FILE* LZ4IO_openDstFile(const char* dstFileName)
}
/* sparse file */
if (f && g_sparseFileSupport) { SET_SPARSE_FILE_MODE(f); }
if (f && prefs->sparseFileSupport) { SET_SPARSE_FILE_MODE(f); }
return f;
}
@ -356,7 +388,7 @@ static int LZ4IO_LZ4_compress(const char* src, char* dst, int srcSize, int dstSi
/* LZ4IO_compressFilename_Legacy :
* This function is intentionally "hidden" (not published in .h)
* It generates compressed streams using the old 'legacy' format */
int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output_filename, int compressionlevel)
int LZ4IO_compressFilename_Legacy(LZ4IO_prefs_t* prefs, const char* input_filename, const char* output_filename, int compressionlevel)
{
typedef int (*compress_f)(const char* src, char* dst, int srcSize, int dstSize, int cLevel);
compress_f const compressionFunction = (compressionlevel < 3) ? LZ4IO_LZ4_compress : LZ4_compress_HC;
@ -374,7 +406,7 @@ int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output
if (finput == NULL)
EXM_THROW(20, "%s : open file error ", input_filename);
foutput = LZ4IO_openDstFile(output_filename);
foutput = LZ4IO_openDstFile(prefs, output_filename);
if (foutput == NULL) {
fclose(finput);
EXM_THROW(20, "%s : open file error ", input_filename);
@ -453,7 +485,7 @@ typedef struct {
LZ4F_CDict* cdict;
} cRess_t;
static void* LZ4IO_createDict(const char* dictFilename, size_t *dictSize) {
static void* LZ4IO_createDict(LZ4IO_prefs_t* prefs, size_t *dictSize) {
size_t readSize;
size_t dictEnd = 0;
size_t dictLen = 0;
@ -461,6 +493,7 @@ static void* LZ4IO_createDict(const char* dictFilename, size_t *dictSize) {
size_t circularBufSize = LZ4_MAX_DICT_SIZE;
char* circularBuf;
char* dictBuf;
const char* dictFilename = prefs->dictionaryFilename;
FILE* dictFile;
if (!dictFilename) EXM_THROW(25, "Dictionary error : no filename provided");
@ -510,23 +543,23 @@ static void* LZ4IO_createDict(const char* dictFilename, size_t *dictSize) {
return dictBuf;
}
static LZ4F_CDict* LZ4IO_createCDict(void) {
static LZ4F_CDict* LZ4IO_createCDict(LZ4IO_prefs_t* prefs) {
size_t dictionarySize;
void* dictionaryBuffer;
LZ4F_CDict* cdict;
if (!g_useDictionary) {
if (!prefs->useDictionary) {
return NULL;
}
dictionaryBuffer = LZ4IO_createDict(g_dictionaryFilename, &dictionarySize);
dictionaryBuffer = LZ4IO_createDict(prefs, &dictionarySize);
if (!dictionaryBuffer) EXM_THROW(25, "Dictionary error : could not create dictionary");
cdict = LZ4F_createCDict(dictionaryBuffer, dictionarySize);
free(dictionaryBuffer);
return cdict;
}
static cRess_t LZ4IO_createCResources(void)
static cRess_t LZ4IO_createCResources(LZ4IO_prefs_t* prefs)
{
const size_t blockSize = g_blockSize;
const size_t blockSize = prefs->blockSize;
cRess_t ress;
LZ4F_errorCode_t const errorCode = LZ4F_createCompressionContext(&(ress.ctx), LZ4F_VERSION);
@ -539,7 +572,7 @@ static cRess_t LZ4IO_createCResources(void)
ress.dstBuffer = malloc(ress.dstBufferSize);
if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(31, "Allocation error : not enough memory");
ress.cdict = LZ4IO_createCDict();
ress.cdict = LZ4IO_createCDict(prefs);
return ress;
}
@ -561,7 +594,7 @@ static void LZ4IO_freeCResources(cRess_t ress)
* result : 0 : compression completed correctly
* 1 : missing or pb opening srcFileName
*/
static int LZ4IO_compressFilename_extRess(cRess_t ress, const char* srcFileName, const char* dstFileName, int compressionLevel)
static int LZ4IO_compressFilename_extRess(LZ4IO_prefs_t* io_prefs, cRess_t ress, const char* srcFileName, const char* dstFileName, int compressionLevel)
{
unsigned long long filesize = 0;
unsigned long long compressedfilesize = 0;
@ -570,7 +603,7 @@ static int LZ4IO_compressFilename_extRess(cRess_t ress, const char* srcFileName,
void* const srcBuffer = ress.srcBuffer;
void* const dstBuffer = ress.dstBuffer;
const size_t dstBufferSize = ress.dstBufferSize;
const size_t blockSize = g_blockSize;
const size_t blockSize = io_prefs->blockSize;
size_t readSize;
LZ4F_compressionContext_t ctx = ress.ctx; /* just a pointer */
LZ4F_preferences_t prefs;
@ -578,7 +611,7 @@ static int LZ4IO_compressFilename_extRess(cRess_t ress, const char* srcFileName,
/* Init */
srcFile = LZ4IO_openSrcFile(srcFileName);
if (srcFile == NULL) return 1;
dstFile = LZ4IO_openDstFile(dstFileName);
dstFile = LZ4IO_openDstFile(io_prefs, dstFileName);
if (dstFile == NULL) { fclose(srcFile); return 1; }
memset(&prefs, 0, sizeof(prefs));
@ -586,12 +619,12 @@ static int LZ4IO_compressFilename_extRess(cRess_t ress, const char* srcFileName,
/* Set compression parameters */
prefs.autoFlush = 1;
prefs.compressionLevel = compressionLevel;
prefs.frameInfo.blockMode = (LZ4F_blockMode_t)g_blockIndependence;
prefs.frameInfo.blockSizeID = (LZ4F_blockSizeID_t)g_blockSizeId;
prefs.frameInfo.blockChecksumFlag = (LZ4F_blockChecksum_t)g_blockChecksum;
prefs.frameInfo.contentChecksumFlag = (LZ4F_contentChecksum_t)g_streamChecksum;
prefs.favorDecSpeed = g_favorDecSpeed;
if (g_contentSizeFlag) {
prefs.frameInfo.blockMode = (LZ4F_blockMode_t)io_prefs->blockIndependence;
prefs.frameInfo.blockSizeID = (LZ4F_blockSizeID_t)io_prefs->blockSizeId;
prefs.frameInfo.blockChecksumFlag = (LZ4F_blockChecksum_t)io_prefs->blockChecksum;
prefs.frameInfo.contentChecksumFlag = (LZ4F_contentChecksum_t)io_prefs->streamChecksum;
prefs.favorDecSpeed = io_prefs->favorDecSpeed;
if (io_prefs->contentSizeFlag) {
U64 const fileSize = UTIL_getFileSize(srcFileName);
prefs.frameInfo.contentSize = fileSize; /* == 0 if input == stdin */
if (fileSize==0)
@ -670,7 +703,7 @@ static int LZ4IO_compressFilename_extRess(cRess_t ress, const char* srcFileName,
UTIL_setFileStat(dstFileName, &statbuf);
} }
if (g_removeSrcFile) { /* remove source file : --rm */
if (io_prefs->removeSrcFile) { /* remove source file : --rm */
if (remove(srcFileName))
EXM_THROW(40, "Remove error : %s: %s", srcFileName, strerror(errno));
}
@ -685,13 +718,13 @@ static int LZ4IO_compressFilename_extRess(cRess_t ress, const char* srcFileName,
}
int LZ4IO_compressFilename(const char* srcFileName, const char* dstFileName, int compressionLevel)
int LZ4IO_compressFilename(LZ4IO_prefs_t* prefs, const char* srcFileName, const char* dstFileName, int compressionLevel)
{
UTIL_time_t const timeStart = UTIL_getTime();
clock_t const cpuStart = clock();
cRess_t const ress = LZ4IO_createCResources();
cRess_t const ress = LZ4IO_createCResources(prefs);
int const result = LZ4IO_compressFilename_extRess(ress, srcFileName, dstFileName, compressionLevel);
int const result = LZ4IO_compressFilename_extRess(prefs, ress, srcFileName, dstFileName, compressionLevel);
/* Free resources */
LZ4IO_freeCResources(ress);
@ -710,7 +743,7 @@ int LZ4IO_compressFilename(const char* srcFileName, const char* dstFileName, int
#define FNSPACE 30
int LZ4IO_compressMultipleFilenames(const char** inFileNamesTable, int ifntSize, const char* suffix, int compressionLevel)
int LZ4IO_compressMultipleFilenames(LZ4IO_prefs_t* prefs, const char** inFileNamesTable, int ifntSize, const char* suffix, int compressionLevel)
{
int i;
int missed_files = 0;
@ -720,7 +753,7 @@ int LZ4IO_compressMultipleFilenames(const char** inFileNamesTable, int ifntSize,
cRess_t ress;
if (dstFileName == NULL) return ifntSize; /* not enough memory */
ress = LZ4IO_createCResources();
ress = LZ4IO_createCResources(prefs);
/* loop on each file */
for (i=0; i<ifntSize; i++) {
@ -729,7 +762,7 @@ int LZ4IO_compressMultipleFilenames(const char** inFileNamesTable, int ifntSize,
strcpy(dstFileName, inFileNamesTable[i]);
strcat(dstFileName, suffix);
missed_files += LZ4IO_compressFilename_extRess(ress, inFileNamesTable[i], dstFileName, compressionLevel);
missed_files += LZ4IO_compressFilename_extRess(prefs, ress, inFileNamesTable[i], dstFileName, compressionLevel);
}
/* Close & Free */
@ -755,7 +788,7 @@ static unsigned LZ4IO_readLE32 (const void* s)
}
static unsigned LZ4IO_fwriteSparse(FILE* file, const void* buffer, size_t bufferSize, unsigned storedSkips)
static unsigned LZ4IO_fwriteSparse(LZ4IO_prefs_t* prefs, FILE* file, const void* buffer, size_t bufferSize, unsigned storedSkips)
{
const size_t sizeT = sizeof(size_t);
const size_t maskT = sizeT -1 ;
@ -765,7 +798,7 @@ static unsigned LZ4IO_fwriteSparse(FILE* file, const void* buffer, size_t buffer
const size_t* const bufferTEnd = bufferT + bufferSizeT;
const size_t segmentSizeT = (32 KB) / sizeT;
if (!g_sparseFileSupport) { /* normal write */
if (!prefs->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;
@ -834,7 +867,7 @@ static void LZ4IO_fwriteSparseEnd(FILE* file, unsigned storedSkips)
static unsigned g_magicRead = 0; /* out-parameter of LZ4IO_decodeLegacyStream() */
static unsigned long long LZ4IO_decodeLegacyStream(FILE* finput, FILE* foutput)
static unsigned long long LZ4IO_decodeLegacyStream(LZ4IO_prefs_t* prefs, FILE* finput, FILE* foutput)
{
unsigned long long streamSize = 0;
unsigned storedSkips = 0;
@ -868,7 +901,7 @@ static unsigned long long LZ4IO_decodeLegacyStream(FILE* finput, FILE* foutput)
if (decodeSize < 0) EXM_THROW(53, "Decoding Failed ! Corrupted input detected !");
streamSize += decodeSize;
/* Write Block */
storedSkips = LZ4IO_fwriteSparse(foutput, out_buff, decodeSize, storedSkips); /* success or die */
storedSkips = LZ4IO_fwriteSparse(prefs, foutput, out_buff, decodeSize, storedSkips); /* success or die */
} }
if (ferror(finput)) EXM_THROW(54, "Read error : ferror");
@ -894,19 +927,19 @@ typedef struct {
size_t dictBufferSize;
} dRess_t;
static void LZ4IO_loadDDict(dRess_t* ress) {
if (!g_useDictionary) {
static void LZ4IO_loadDDict(LZ4IO_prefs_t* prefs, dRess_t* ress) {
if (!prefs->useDictionary) {
ress->dictBuffer = NULL;
ress->dictBufferSize = 0;
return;
}
ress->dictBuffer = LZ4IO_createDict(g_dictionaryFilename, &ress->dictBufferSize);
ress->dictBuffer = LZ4IO_createDict(prefs, &ress->dictBufferSize);
if (!ress->dictBuffer) EXM_THROW(25, "Dictionary error : could not create dictionary");
}
static const size_t LZ4IO_dBufferSize = 64 KB;
static dRess_t LZ4IO_createDResources(void)
static dRess_t LZ4IO_createDResources(LZ4IO_prefs_t* prefs)
{
dRess_t ress;
@ -921,7 +954,7 @@ static dRess_t LZ4IO_createDResources(void)
ress.dstBuffer = malloc(ress.dstBufferSize);
if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(61, "Allocation error : not enough memory");
LZ4IO_loadDDict(&ress);
LZ4IO_loadDDict(prefs, &ress);
ress.dstFile = NULL;
return ress;
@ -937,7 +970,7 @@ static void LZ4IO_freeDResources(dRess_t ress)
}
static unsigned long long LZ4IO_decompressLZ4F(dRess_t ress, FILE* srcFile, FILE* dstFile)
static unsigned long long LZ4IO_decompressLZ4F(LZ4IO_prefs_t* prefs, dRess_t ress, FILE* srcFile, FILE* dstFile)
{
unsigned long long filesize = 0;
LZ4F_errorCode_t nextToLoad;
@ -972,8 +1005,8 @@ static unsigned long long LZ4IO_decompressLZ4F(dRess_t ress, FILE* srcFile, FILE
/* Write Block */
if (decodedBytes) {
if (!g_testMode)
storedSkips = LZ4IO_fwriteSparse(dstFile, ress.dstBuffer, decodedBytes, storedSkips);
if (!prefs->testMode)
storedSkips = LZ4IO_fwriteSparse(prefs, dstFile, ress.dstBuffer, decodedBytes, storedSkips);
filesize += decodedBytes;
DISPLAYUPDATE(2, "\rDecompressed : %u MB ", (unsigned)(filesize>>20));
}
@ -984,7 +1017,7 @@ static unsigned long long LZ4IO_decompressLZ4F(dRess_t ress, FILE* srcFile, FILE
/* can be out because readSize == 0, which could be an fread() error */
if (ferror(srcFile)) EXM_THROW(67, "Read error");
if (!g_testMode) LZ4IO_fwriteSparseEnd(dstFile, storedSkips);
if (!prefs->testMode) LZ4IO_fwriteSparseEnd(dstFile, storedSkips);
if (nextToLoad!=0) EXM_THROW(68, "Unfinished stream");
return filesize;
@ -993,7 +1026,7 @@ static unsigned long long LZ4IO_decompressLZ4F(dRess_t ress, FILE* srcFile, FILE
#define PTSIZE (64 KB)
#define PTSIZET (PTSIZE / sizeof(size_t))
static unsigned long long LZ4IO_passThrough(FILE* finput, FILE* foutput, unsigned char MNstore[MAGICNUMBER_SIZE])
static unsigned long long LZ4IO_passThrough(LZ4IO_prefs_t* prefs, FILE* finput, FILE* foutput, unsigned char MNstore[MAGICNUMBER_SIZE])
{
size_t buffer[PTSIZET];
size_t readBytes = 1;
@ -1006,7 +1039,7 @@ static unsigned long long LZ4IO_passThrough(FILE* finput, FILE* foutput, unsigne
while (readBytes) {
readBytes = fread(buffer, 1, PTSIZE, finput);
total += readBytes;
storedSkips = LZ4IO_fwriteSparse(foutput, buffer, readBytes, storedSkips);
storedSkips = LZ4IO_fwriteSparse(prefs, foutput, buffer, readBytes, storedSkips);
}
if (ferror(finput)) EXM_THROW(51, "Read Error");
@ -1033,7 +1066,7 @@ static int fseek_u32(FILE *fp, unsigned offset, int where)
}
#define ENDOFSTREAM ((unsigned long long)-1)
static unsigned long long selectDecoder(dRess_t ress, FILE* finput, FILE* foutput)
static unsigned long long selectDecoder(LZ4IO_prefs_t* prefs, dRess_t ress, FILE* finput, FILE* foutput)
{
unsigned char MNstore[MAGICNUMBER_SIZE];
unsigned magicNumber;
@ -1059,10 +1092,10 @@ static unsigned long long selectDecoder(dRess_t ress, FILE* finput, FILE* foutpu
switch(magicNumber)
{
case LZ4IO_MAGICNUMBER:
return LZ4IO_decompressLZ4F(ress, finput, foutput);
return LZ4IO_decompressLZ4F(prefs, ress, finput, foutput);
case LEGACY_MAGICNUMBER:
DISPLAYLEVEL(4, "Detected : Legacy format \n");
return LZ4IO_decodeLegacyStream(finput, foutput);
return LZ4IO_decodeLegacyStream(prefs, finput, foutput);
case LZ4IO_SKIPPABLE0:
DISPLAYLEVEL(4, "Skipping detected skippable area \n");
{ size_t const nbReadBytes = fread(MNstore, 1, 4, finput);
@ -1079,9 +1112,9 @@ static unsigned long long selectDecoder(dRess_t ress, FILE* finput, FILE* foutpu
default:
if (nbFrames == 1) { /* just started */
/* Wrong magic number at the beginning of 1st stream */
if (!g_testMode && g_overwrite && g_passThrough) {
if (!prefs->testMode && prefs->overwrite && prefs->passThrough) {
nbFrames = 0;
return LZ4IO_passThrough(finput, foutput, MNstore);
return LZ4IO_passThrough(prefs, finput, foutput, MNstore);
}
EXM_THROW(44,"Unrecognized header : file cannot be decoded");
}
@ -1096,7 +1129,7 @@ static unsigned long long selectDecoder(dRess_t ress, FILE* finput, FILE* foutpu
}
static int LZ4IO_decompressSrcFile(dRess_t ress, const char* input_filename, const char* output_filename)
static int LZ4IO_decompressSrcFile(LZ4IO_prefs_t* prefs, dRess_t ress, const char* input_filename, const char* output_filename)
{
FILE* const foutput = ress.dstFile;
unsigned long long filesize = 0;
@ -1108,14 +1141,14 @@ static int LZ4IO_decompressSrcFile(dRess_t ress, const char* input_filename, con
/* Loop over multiple streams */
for ( ; ; ) { /* endless loop, see break condition */
unsigned long long const decodedSize =
selectDecoder(ress, finput, foutput);
selectDecoder(prefs, ress, finput, foutput);
if (decodedSize == ENDOFSTREAM) break;
filesize += decodedSize;
}
/* Close input */
fclose(finput);
if (g_removeSrcFile) { /* --rm */
if (prefs->removeSrcFile) { /* --rm */
if (remove(input_filename))
EXM_THROW(45, "Remove error : %s: %s", input_filename, strerror(errno));
}
@ -1129,11 +1162,11 @@ static int LZ4IO_decompressSrcFile(dRess_t ress, const char* input_filename, con
}
static int LZ4IO_decompressDstFile(dRess_t ress, const char* input_filename, const char* output_filename)
static int LZ4IO_decompressDstFile(LZ4IO_prefs_t* prefs, dRess_t ress, const char* input_filename, const char* output_filename)
{
stat_t statbuf;
int stat_result = 0;
FILE* const foutput = LZ4IO_openDstFile(output_filename);
FILE* const foutput = LZ4IO_openDstFile(prefs, output_filename);
if (foutput==NULL) return 1; /* failure */
if ( strcmp(input_filename, stdinmark)
@ -1141,7 +1174,7 @@ static int LZ4IO_decompressDstFile(dRess_t ress, const char* input_filename, con
stat_result = 1;
ress.dstFile = foutput;
LZ4IO_decompressSrcFile(ress, input_filename, output_filename);
LZ4IO_decompressSrcFile(prefs, ress, input_filename, output_filename);
fclose(foutput);
@ -1157,12 +1190,12 @@ static int LZ4IO_decompressDstFile(dRess_t ress, const char* input_filename, con
}
int LZ4IO_decompressFilename(const char* input_filename, const char* output_filename)
int LZ4IO_decompressFilename(LZ4IO_prefs_t* prefs, const char* input_filename, const char* output_filename)
{
dRess_t const ress = LZ4IO_createDResources();
dRess_t const ress = LZ4IO_createDResources(prefs);
clock_t const start = clock();
int const missingFiles = LZ4IO_decompressDstFile(ress, input_filename, output_filename);
int const missingFiles = LZ4IO_decompressDstFile(prefs, ress, input_filename, output_filename);
clock_t const end = clock();
double const seconds = (double)(end - start) / CLOCKS_PER_SEC;
@ -1173,7 +1206,7 @@ int LZ4IO_decompressFilename(const char* input_filename, const char* output_file
}
int LZ4IO_decompressMultipleFilenames(const char** inFileNamesTable, int ifntSize, const char* suffix)
int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* prefs, const char** inFileNamesTable, int ifntSize, const char* suffix)
{
int i;
int skippedFiles = 0;
@ -1181,16 +1214,16 @@ int LZ4IO_decompressMultipleFilenames(const char** inFileNamesTable, int ifntSiz
char* outFileName = (char*)malloc(FNSPACE);
size_t ofnSize = FNSPACE;
size_t const suffixSize = strlen(suffix);
dRess_t ress = LZ4IO_createDResources();
dRess_t ress = LZ4IO_createDResources(prefs);
if (outFileName==NULL) return ifntSize; /* not enough memory */
ress.dstFile = LZ4IO_openDstFile(stdoutmark);
ress.dstFile = LZ4IO_openDstFile(prefs, stdoutmark);
for (i=0; i<ifntSize; i++) {
size_t const ifnSize = strlen(inFileNamesTable[i]);
const char* const suffixPtr = inFileNamesTable[i] + ifnSize - suffixSize;
if (!strcmp(suffix, stdoutmark)) {
missingFiles += LZ4IO_decompressSrcFile(ress, inFileNamesTable[i], stdoutmark);
missingFiles += LZ4IO_decompressSrcFile(prefs, ress, inFileNamesTable[i], stdoutmark);
continue;
}
if (ofnSize <= ifnSize-suffixSize+1) { free(outFileName); ofnSize = ifnSize + 20; outFileName = (char*)malloc(ofnSize); if (outFileName==NULL) return ifntSize; }
@ -1201,7 +1234,7 @@ int LZ4IO_decompressMultipleFilenames(const char** inFileNamesTable, int ifntSiz
}
memcpy(outFileName, inFileNamesTable[i], ifnSize - suffixSize);
outFileName[ifnSize-suffixSize] = '\0';
missingFiles += LZ4IO_decompressDstFile(ress, inFileNamesTable[i], outFileName);
missingFiles += LZ4IO_decompressDstFile(prefs, ress, inFileNamesTable[i], outFileName);
}
LZ4IO_freeDResources(ress);

View File

@ -48,69 +48,77 @@ static const char nulmark[] = "nul";
static const char nulmark[] = "/dev/null";
#endif
/* ************************************************** */
/* ****************** Type Definitions ************** */
/* ************************************************** */
typedef struct LZ4IO_prefs_s LZ4IO_prefs_t;
LZ4IO_prefs_t* LZ4IO_defaultPreferences(void);
void LZ4IO_freePreferences(LZ4IO_prefs_t* prefs);
/* ************************************************** */
/* ****************** Functions ********************* */
/* ************************************************** */
int LZ4IO_compressFilename (const char* input_filename, const char* output_filename, int compressionlevel);
int LZ4IO_decompressFilename(const char* input_filename, const char* output_filename);
int LZ4IO_compressFilename (LZ4IO_prefs_t* prefs, const char* input_filename, const char* output_filename, int compressionlevel);
int LZ4IO_decompressFilename(LZ4IO_prefs_t* prefs, const char* input_filename, const char* output_filename);
int LZ4IO_compressMultipleFilenames(const char** inFileNamesTable, int ifntSize, const char* suffix, int compressionlevel);
int LZ4IO_decompressMultipleFilenames(const char** inFileNamesTable, int ifntSize, const char* suffix);
int LZ4IO_compressMultipleFilenames(LZ4IO_prefs_t* prefs, const char** inFileNamesTable, int ifntSize, const char* suffix, int compressionlevel);
int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* prefs, const char** inFileNamesTable, int ifntSize, const char* suffix);
/* ************************************************** */
/* ****************** Parameters ******************** */
/* ************************************************** */
int LZ4IO_setDictionaryFilename(const char* dictionaryFilename);
int LZ4IO_setDictionaryFilename(LZ4IO_prefs_t* prefs, const char* dictionaryFilename);
/* Default setting : passThrough = 0;
return : passThrough mode (0/1) */
int LZ4IO_setPassThrough(int yes);
int LZ4IO_setPassThrough(LZ4IO_prefs_t* prefs, int yes);
/* Default setting : overwrite = 1;
return : overwrite mode (0/1) */
int LZ4IO_setOverwrite(int yes);
int LZ4IO_setOverwrite(LZ4IO_prefs_t* prefs, int yes);
/* Default setting : testMode = 0;
return : testMode (0/1) */
int LZ4IO_setTestMode(int yes);
int LZ4IO_setTestMode(LZ4IO_prefs_t* prefs, int yes);
/* blockSizeID : valid values : 4-5-6-7
return : 0 if error, blockSize if OK */
size_t LZ4IO_setBlockSizeID(unsigned blockSizeID);
size_t LZ4IO_setBlockSizeID(LZ4IO_prefs_t* prefs, unsigned blockSizeID);
/* blockSize : valid values : 32 -> 4MB
return : 0 if error, actual blocksize if OK */
size_t LZ4IO_setBlockSize(size_t blockSize);
size_t LZ4IO_setBlockSize(LZ4IO_prefs_t* prefs, size_t blockSize);
/* Default setting : independent blocks */
typedef enum { LZ4IO_blockLinked=0, LZ4IO_blockIndependent} LZ4IO_blockMode_t;
int LZ4IO_setBlockMode(LZ4IO_blockMode_t blockMode);
int LZ4IO_setBlockMode(LZ4IO_prefs_t* prefs, LZ4IO_blockMode_t blockMode);
/* Default setting : no block checksum */
int LZ4IO_setBlockChecksumMode(int xxhash);
int LZ4IO_setBlockChecksumMode(LZ4IO_prefs_t* prefs, int xxhash);
/* Default setting : stream checksum enabled */
int LZ4IO_setStreamChecksumMode(int xxhash);
int LZ4IO_setStreamChecksumMode(LZ4IO_prefs_t* prefs, int xxhash);
/* Default setting : 0 (no notification) */
int LZ4IO_setNotificationLevel(int level);
/* Default setting : 0 (disabled) */
int LZ4IO_setSparseFile(int enable);
int LZ4IO_setSparseFile(LZ4IO_prefs_t* prefs, int enable);
/* Default setting : 0 == no content size present in frame header */
int LZ4IO_setContentSize(int enable);
int LZ4IO_setContentSize(LZ4IO_prefs_t* prefs, int enable);
/* Default setting : 0 == src file preserved */
void LZ4IO_setRemoveSrcFile(unsigned flag);
void LZ4IO_setRemoveSrcFile(LZ4IO_prefs_t* prefs, unsigned flag);
/* Default setting : 0 == favor compression ratio
* Note : 1 only works for high compression levels (10+) */
void LZ4IO_favorDecSpeed(int favor);
void LZ4IO_favorDecSpeed(LZ4IO_prefs_t* prefs, int favor);
#endif /* LZ4IO_H_237902873 */