diff --git a/Makefile b/Makefile index abec33f8..a741034a 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ # ################################################################ # Version number -export VERSION := 0.4.4 +export VERSION := 0.4.5 PRGDIR = programs ZSTDDIR = lib diff --git a/NEWS b/NEWS index 079483a3..ee9a4585 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,6 @@ +v0.4.5 +new : -m/--multiple : compress/decompress multiple files + v0.4.4 Fixed : high compression modes for Windows 32 bits new : external dictionary API extended to buffered mode and accessible through command line diff --git a/lib/zstd.h b/lib/zstd.h index b0c841f3..d6eb0b51 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -62,7 +62,7 @@ extern "C" { ***************************************/ #define ZSTD_VERSION_MAJOR 0 /* for breaking interface changes */ #define ZSTD_VERSION_MINOR 4 /* for new (non-breaking) interface capabilities */ -#define ZSTD_VERSION_RELEASE 3 /* for tweaks, bug-fixes, or development */ +#define ZSTD_VERSION_RELEASE 5 /* for tweaks, bug-fixes, or development */ #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE) ZSTDLIB_API unsigned ZSTD_versionNumber (void); diff --git a/lib/zstd_compress.c b/lib/zstd_compress.c index 83f85c77..c6d81ebe 100644 --- a/lib/zstd_compress.c +++ b/lib/zstd_compress.c @@ -2117,7 +2117,7 @@ size_t ZSTD_compressBegin(ZSTD_CCtx* ctx, void* dst, size_t maxDstSize, int comp } -/** ZSTD_compressEnd +/*! ZSTD_compressEnd * Write frame epilogue * @return : nb of bytes written into dst (or an error code) */ size_t ZSTD_compressEnd(ZSTD_CCtx* ctx, void* dst, size_t maxDstSize) @@ -2139,6 +2139,7 @@ size_t ZSTD_compressEnd(ZSTD_CCtx* ctx, void* dst, size_t maxDstSize) size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize, + const void* dict,size_t dictSize, ZSTD_parameters params) { BYTE* const ostart = (BYTE*)dst; @@ -2151,9 +2152,15 @@ size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx, op += oSize; maxDstSize -= oSize; + /* dictionary */ + if (dict) + { + oSize = ZSTD_compress_insertDictionary(ctx, dict, dictSize); + if (ZSTD_isError(oSize)) return oSize; + } + /* body (compression) */ - ctx->base = (const BYTE*)src; - oSize = ZSTD_compress_generic (ctx, op, maxDstSize, src, srcSize); + oSize = ZSTD_compressContinue (ctx, op, maxDstSize, src, srcSize); if(ZSTD_isError(oSize)) return oSize; op += oSize; maxDstSize -= oSize; @@ -2166,9 +2173,14 @@ size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx, return (op - ostart); } +size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize, const void* dict, size_t dictSize, int compressionLevel) +{ + return ZSTD_compress_advanced(ctx, dst, maxDstSize, src, srcSize, dict, dictSize, ZSTD_getParams(compressionLevel, srcSize+dictSize)); +} + size_t ZSTD_compressCCtx (ZSTD_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize, int compressionLevel) { - return ZSTD_compress_advanced(ctx, dst, maxDstSize, src, srcSize, ZSTD_getParams(compressionLevel, srcSize)); + return ZSTD_compress_advanced(ctx, dst, maxDstSize, src, srcSize, NULL, 0, ZSTD_getParams(compressionLevel, srcSize)); } size_t ZSTD_compress(void* dst, size_t maxDstSize, const void* src, size_t srcSize, int compressionLevel) @@ -2180,3 +2192,4 @@ size_t ZSTD_compress(void* dst, size_t maxDstSize, const void* src, size_t srcSi free(ctxBody.workSpace); /* can't free ctxBody, since it's on stack; free heap content */ return result; } + diff --git a/lib/zstd_decompress.c b/lib/zstd_decompress.c index e1c30a8d..3431e327 100644 --- a/lib/zstd_decompress.c +++ b/lib/zstd_decompress.c @@ -676,7 +676,10 @@ static size_t ZSTD_decompressBlock( } -size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) +size_t ZSTD_decompress_usingDict(ZSTD_DCtx* ctx, + void* dst, size_t maxDstSize, + const void* src, size_t srcSize, + const void* dict, size_t dictSize) { const BYTE* ip = (const BYTE*)src; const BYTE* iend = ip + srcSize; @@ -686,9 +689,19 @@ size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx, void* dst, size_t maxDstSize, const v size_t remainingSize = srcSize; blockProperties_t blockProperties; - /* init */ - ctx->vBase = ctx->base = ctx->dictEnd = dst; + ZSTD_resetDCtx(ctx); + if (dict) + { + ZSTD_decompress_insertDictionary(ctx, dict, dictSize); + ctx->dictEnd = ctx->previousDstEnd; + ctx->vBase = (const char*)dst - ((const char*)(ctx->previousDstEnd) - (const char*)(ctx->base)); + ctx->base = dst; + } + else + { + ctx->vBase = ctx->base = ctx->dictEnd = dst; + } /* Frame Header */ { @@ -749,10 +762,16 @@ size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx, void* dst, size_t maxDstSize, const v return op-ostart; } + +size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) +{ + return ZSTD_decompress_usingDict(dctx, dst, maxDstSize, src, srcSize, NULL, 0); +} + size_t ZSTD_decompress(void* dst, size_t maxDstSize, const void* src, size_t srcSize) { - ZSTD_DCtx ctx; - return ZSTD_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize); + ZSTD_DCtx dctx; + return ZSTD_decompressDCtx(&dctx, dst, maxDstSize, src, srcSize); } diff --git a/lib/zstd_static.h b/lib/zstd_static.h index ebb8c83c..f78d464c 100644 --- a/lib/zstd_static.h +++ b/lib/zstd_static.h @@ -80,7 +80,7 @@ typedef struct /* ************************************* -* Advanced function +* Advanced functions ***************************************/ /** ZSTD_getParams * return ZSTD_parameters structure for a selected compression level and srcSize. @@ -91,16 +91,43 @@ ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, U64 srcSizeHint * correct params value to remain within authorized range */ ZSTDLIB_API void ZSTD_validateParams(ZSTD_parameters* params); +/** ZSTD_compress_usingDict +* Same as ZSTD_compressCCtx(), using a Dictionary content as prefix +* Note : dict can be NULL, in which case, it's equivalent to ZSTD_compressCCtx() */ +ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, + void* dst, size_t maxDstSize, + const void* src, size_t srcSize, + const void* dict,size_t dictSize, + int compressionLevel); + /** ZSTD_compress_advanced -* Same as ZSTD_compressCCtx(), with fine-tune control of each compression parameter */ +* Same as ZSTD_compress_usingDict(), with fine-tune control of each compression parameter */ ZSTDLIB_API size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize, + const void* dict,size_t dictSize, ZSTD_parameters params); +/** Decompression context management */ +typedef struct ZSTD_DCtx_s ZSTD_DCtx; +ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void); +ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx); + +/** ZSTD_decompressDCtx +* Same as ZSTD_decompress, with pre-allocated DCtx structure */ +size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize); + +/** ZSTD_decompress_usingDict +* Same as ZSTD_decompressDCtx, using a Dictionary content as prefix +* Note : dict can be NULL, in which case, it's equivalent to ZSTD_decompressDCtx() */ +size_t ZSTD_decompress_usingDict(ZSTD_DCtx* ctx, + void* dst, size_t maxDstSize, + const void* src, size_t srcSize, + const void* dict, size_t dictSize); + /* ************************************** -* Streaming functions (bufferless mode) +* Streaming functions (direct mode) ****************************************/ ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, void* dst, size_t maxDstSize, int compressionLevel); ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* ctx, void* dst, size_t maxDstSize, ZSTD_parameters params); @@ -110,7 +137,7 @@ ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t maxD ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t maxDstSize); /** - Streaming compression, bufferless mode + Streaming compression, direct mode (bufferless) A ZSTD_CCtx object is required to track streaming operations. Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage it. @@ -131,14 +158,11 @@ ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t maxDstSiz Finish a frame with ZSTD_compressEnd(), which will write the epilogue. Without it, the frame will be considered incomplete by decoders. - You can then re-use ZSTD_CCtx to compress new frames. + + You can then reuse ZSTD_CCtx to compress new frames. */ -typedef struct ZSTD_DCtx_s ZSTD_DCtx; -ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void); -ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx); - ZSTDLIB_API size_t ZSTD_resetDCtx(ZSTD_DCtx* dctx); ZSTDLIB_API size_t ZSTD_getFrameParams(ZSTD_parameters* params, const void* src, size_t srcSize); ZSTDLIB_API void ZSTD_decompress_insertDictionary(ZSTD_DCtx* ctx, const void* src, size_t srcSize); @@ -160,7 +184,8 @@ ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t ma >0 : means there is not enough data into src. Provides the expected size to successfully decode header. errorCode, which can be tested using ZSTD_isError() (For example, if it's not a ZSTD header) - Then, you can optionally insert a dictionary. This operation must mimic the compressor behavior, otherwise decompression will fail or be corrupted. + Then, you can optionally insert a dictionary. + This operation must mimic the compressor behavior, otherwise decompression will fail or be corrupted. Then it's possible to start decompression. Use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively. @@ -207,28 +232,28 @@ static const ZSTD_parameters ZSTD_defaultParameters[4][ZSTD_MAX_CLEVEL+1] = { { 0, 26, 27, 25, 9, 5, ZSTD_btlazy2 }, /* level 20 */ }, { /* for srcSize <= 256 KB */ - /* W, C, H, S, L, strat */ - { 0, 0, 0, 0, 0, 0, ZSTD_fast }, /* level 0 - never used */ - { 0, 18, 16, 15, 1, 7, ZSTD_fast }, /* level 1 */ - { 0, 18, 16, 16, 1, 7, ZSTD_fast }, /* level 2 */ - { 0, 18, 18, 18, 1, 7, ZSTD_fast }, /* level 3 */ - { 0, 18, 14, 15, 4, 6, ZSTD_greedy }, /* level 4 */ - { 0, 18, 16, 16, 1, 6, ZSTD_lazy }, /* level 5 */ - { 0, 18, 15, 15, 3, 6, ZSTD_lazy }, /* level 6 */ - { 0, 18, 15, 15, 4, 6, ZSTD_lazy }, /* level 7 */ - { 0, 18, 16, 18, 4, 6, ZSTD_lazy }, /* level 8 */ - { 0, 18, 18, 18, 4, 6, ZSTD_lazy }, /* level 9 */ - { 0, 18, 18, 18, 5, 6, ZSTD_lazy }, /* level 10 */ - { 0, 18, 18, 19, 6, 6, ZSTD_lazy }, /* level 11 */ - { 0, 18, 18, 19, 7, 6, ZSTD_lazy }, /* level 12 */ - { 0, 18, 19, 15, 7, 5, ZSTD_btlazy2 }, /* level 13 */ - { 0, 18, 19, 16, 8, 5, ZSTD_btlazy2 }, /* level 14 */ - { 0, 18, 19, 17, 9, 5, ZSTD_btlazy2 }, /* level 15 */ - { 0, 18, 19, 17, 10, 5, ZSTD_btlazy2 }, /* level 16 */ - { 0, 18, 19, 17, 11, 5, ZSTD_btlazy2 }, /* level 17 */ - { 0, 18, 19, 17, 12, 5, ZSTD_btlazy2 }, /* level 18 */ - { 0, 18, 19, 17, 13, 5, ZSTD_btlazy2 }, /* level 19 */ - { 0, 18, 19, 17, 14, 5, ZSTD_btlazy2 }, /* level 20 */ + /* W, C, H, S, L, strat */ + { 0, 18, 13, 14, 1, 7, ZSTD_fast }, /* level 0 - never used */ + { 0, 18, 14, 15, 1, 6, ZSTD_fast }, /* level 1 */ + { 0, 18, 14, 15, 1, 5, ZSTD_fast }, /* level 2 */ + { 0, 18, 12, 15, 3, 7, ZSTD_greedy }, /* level 3 */ + { 0, 18, 13, 15, 4, 7, ZSTD_greedy }, /* level 4 */ + { 0, 18, 14, 15, 5, 7, ZSTD_greedy }, /* level 5 */ + { 0, 18, 13, 15, 4, 7, ZSTD_lazy }, /* level 6 */ + { 0, 18, 14, 16, 5, 7, ZSTD_lazy }, /* level 7 */ + { 0, 18, 15, 16, 6, 7, ZSTD_lazy }, /* level 8 */ + { 0, 18, 15, 15, 7, 7, ZSTD_lazy }, /* level 9 */ + { 0, 18, 16, 16, 7, 7, ZSTD_lazy }, /* level 10 */ + { 0, 18, 16, 16, 8, 4, ZSTD_lazy }, /* level 11 */ + { 0, 18, 17, 16, 8, 4, ZSTD_lazy }, /* level 12 */ + { 0, 18, 17, 16, 9, 4, ZSTD_lazy }, /* level 13 */ + { 0, 18, 18, 16, 9, 4, ZSTD_lazy }, /* level 14 */ + { 0, 18, 17, 17, 9, 4, ZSTD_lazy2 }, /* level 15 */ + { 0, 18, 18, 18, 9, 4, ZSTD_lazy2 }, /* level 16 */ + { 0, 18, 18, 18, 10, 4, ZSTD_lazy2 }, /* level 17 */ + { 0, 18, 18, 18, 11, 4, ZSTD_lazy2 }, /* level 18 */ + { 0, 18, 18, 18, 12, 4, ZSTD_lazy2 }, /* level 19 */ + { 0, 18, 18, 18, 13, 4, ZSTD_lazy2 }, /* level 20 */ }, { /* for srcSize <= 128 KB */ /* W, C, H, S, L, strat */ diff --git a/programs/Makefile b/programs/Makefile index 822f2d24..57fa87c1 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -30,7 +30,7 @@ # fullbench32: Same as fullbench, but forced to compile in 32-bits mode # ########################################################################## -VERSION?= 0.4.4 +VERSION?= 0.4.5 DESTDIR?= PREFIX ?= /usr/local diff --git a/programs/bench.c b/programs/bench.c index c48112f1..b80d6990 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -27,7 +27,10 @@ * Compiler Options ****************************************/ /* Disable some Visual warning messages */ -#define _CRT_SECURE_NO_WARNINGS /* fopen */ +#ifdef _MSC_VER +# define _CRT_SECURE_NO_WARNINGS /* fopen */ +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +#endif /* Unix Large Files support (>4GB) */ #define _FILE_OFFSET_BITS 64 @@ -60,7 +63,7 @@ #endif #include "mem.h" -#include "zstd.h" +#include "zstd_static.h" #include "xxhash.h" #include "datagen.h" /* RDG_genBuffer */ @@ -72,6 +75,10 @@ # define S_ISREG(x) (((x) & S_IFMT) == S_IFREG) #endif +#ifdef _MSC_VER +#define snprintf sprintf_s +#endif + /* ************************************* * Constants @@ -90,9 +97,28 @@ static U32 g_compressibilityDefault = 50; /* ************************************* -* Macros +* console display ***************************************/ -#define DISPLAY(...) fprintf(stderr, __VA_ARGS__) +#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 */ + + +/* ************************************* +* Exceptions +***************************************/ +#ifndef DEBUG +# define DEBUG 0 +#endif +#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); \ +} /* ************************************* @@ -156,13 +182,27 @@ static int BMK_GetMilliSpan( int nTimeStart ) return nSpan; } +static U64 BMK_getFileSize(const char* infilename) +{ + int r; +#if defined(_MSC_VER) + struct _stat64 statbuf; + r = _stat64(infilename, &statbuf); +#else + struct stat statbuf; + r = stat(infilename, &statbuf); +#endif + if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */ + return (U64)statbuf.st_size; +} + /* ******************************************************** * Bench functions **********************************************************/ typedef struct { - char* srcPtr; + const char* srcPtr; size_t srcSize; char* cPtr; size_t cRoom; @@ -171,57 +211,55 @@ typedef struct size_t resSize; } blockParam_t; -typedef size_t (*compressor_t) (void* dst, size_t maxDstSize, const void* src, size_t srcSize, int compressionLevel); - #define MIN(a,b) ((a)<(b) ? (a) : (b)) -static int BMK_benchMem(void* srcBuffer, size_t srcSize, const char* fileName, int cLevel) +static int BMK_benchMem(const void* srcBuffer, size_t srcSize, + const char* displayName, int cLevel, + const size_t* fileSizes, U32 nbFiles, + const void* dictBuffer, size_t dictBufferSize) { const size_t blockSize = (g_blockSize ? g_blockSize : srcSize) + (!srcSize); /* avoid div by 0 */ - const U32 nbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize); - blockParam_t* const blockTable = (blockParam_t*) malloc(nbBlocks * sizeof(blockParam_t)); - const size_t maxCompressedSize = (size_t)nbBlocks * ZSTD_compressBound(blockSize); + const U32 maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles; + blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t)); + const size_t maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */ void* const compressedBuffer = malloc(maxCompressedSize); void* const resultBuffer = malloc(srcSize); - const compressor_t compressor = ZSTD_compress; - U64 crcOrig; + ZSTD_CCtx* ctx = ZSTD_createCCtx(); + ZSTD_DCtx* dctx = ZSTD_createDCtx(); + U64 crcOrig = XXH64(srcBuffer, srcSize, 0); + U32 nbBlocks = 0; /* init */ - if (strlen(fileName)>16) - fileName += strlen(fileName)-16; + if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */ /* Memory allocation & restrictions */ - if (!compressedBuffer || !resultBuffer || !blockTable) - { - DISPLAY("\nError: not enough memory!\n"); - free(compressedBuffer); - free(resultBuffer); - free(blockTable); - return 12; - } - - /* Calculating input Checksum */ - crcOrig = XXH64(srcBuffer, srcSize, 0); + if (!compressedBuffer || !resultBuffer || !blockTable || !ctx || !dctx) + EXM_THROW(31, "not enough memory"); /* Init blockTable data */ { - U32 i; - size_t remaining = srcSize; - char* srcPtr = (char*)srcBuffer; + U32 fileNb; + const char* srcPtr = (const char*)srcBuffer; char* cPtr = (char*)compressedBuffer; char* resPtr = (char*)resultBuffer; - for (i=0; i\r", loopNb, fileName, (U32)srcSize); + DISPLAY("%2i-%-17.17s :%10u ->\r", loopNb, displayName, (U32)srcSize); memset(compressedBuffer, 0xE5, maxCompressedSize); nbLoops = 0; @@ -254,7 +292,11 @@ static int BMK_benchMem(void* srcBuffer, size_t srcSize, const char* fileName, i while (BMK_GetMilliSpan(milliTime) < TIMELOOP) { for (blockNb=0; blockNb%10i (%5.3f),%6.1f MB/s\r", loopNb, fileName, (int)srcSize, (int)cSize, ratio, (double)srcSize / fastestC / 1000.); + DISPLAY("%2i-%-17.17s :%10i ->%10i (%5.3f),%6.1f MB/s\r", loopNb, displayName, (int)srcSize, (int)cSize, ratio, (double)srcSize / fastestC / 1000.); #if 1 /* Decompression */ @@ -277,13 +319,15 @@ static int BMK_benchMem(void* srcBuffer, size_t srcSize, const char* fileName, i for ( ; BMK_GetMilliSpan(milliTime) < TIMELOOP; nbLoops++) { for (blockNb=0; blockNb%10i (%5.3f),%6.1f MB/s ,%6.1f MB/s\r", loopNb, fileName, (int)srcSize, (int)cSize, ratio, (double)srcSize / fastestC / 1000., (double)srcSize / fastestD / 1000.); + DISPLAY("%2i-%-17.17s :%10i ->%10i (%5.3f),%6.1f MB/s ,%6.1f MB/s\r", loopNb, displayName, (int)srcSize, (int)cSize, ratio, (double)srcSize / fastestC / 1000., (double)srcSize / fastestD / 1000.); /* CRC Checking */ crcCheck = XXH64(resultBuffer, srcSize, 0); @@ -291,10 +335,10 @@ static int BMK_benchMem(void* srcBuffer, size_t srcSize, const char* fileName, i { unsigned u; unsigned eBlockSize = (unsigned)(MIN(65536*2, blockSize)); - DISPLAY("\n!!! WARNING !!! %14s : Invalid Checksum : %x != %x\n", fileName, (unsigned)crcOrig, (unsigned)crcCheck); + DISPLAY("\n!!! WARNING !!! %14s : Invalid Checksum : %x != %x\n", displayName, (unsigned)crcOrig, (unsigned)crcCheck); for (u=0; u%10i (%5.3f),%6.1f MB/s ,%6.1f MB/s \n", cLevel, fileName, (int)srcSize, (int)cSize, ratio, (double)srcSize / fastestC / 1000., (double)srcSize / fastestD / 1000.); + DISPLAY("%2i-%-17.17s :%10i ->%10i (%5.3f),%6.1f MB/s ,%6.1f MB/s \n", cLevel, displayName, (int)srcSize, (int)cSize, ratio, (double)srcSize / fastestC / 1000., (double)srcSize / fastestD / 1000.); + else + DISPLAY("X \n"); } - /* End cleaning */ + /* clean up */ free(compressedBuffer); free(resultBuffer); + ZSTD_freeCCtx(ctx); + ZSTD_freeDCtx(dctx); return 0; } -static U64 BMK_GetFileSize(const char* infilename) -{ - int r; -#if defined(_MSC_VER) - struct _stat64 statbuf; - r = _stat64(infilename, &statbuf); -#else - struct stat statbuf; - r = stat(infilename, &statbuf); -#endif - if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */ - return (U64)statbuf.st_size; -} - static size_t BMK_findMaxMem(U64 requiredMem) { size_t step = 64 MB; @@ -349,124 +383,143 @@ static size_t BMK_findMaxMem(U64 requiredMem) return (size_t)(requiredMem - step); } -static int BMK_benchOneFile(const char* inFileName, int cLevel) +static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize, + const char* displayName, int cLevel, + const size_t* fileSizes, unsigned nbFiles, + const void* dictBuffer, size_t dictBufferSize) { - FILE* inFile; - U64 inFileSize; - size_t benchedSize, readSize; - void* srcBuffer; - int result=0; - - /* Check file existence */ - inFile = fopen(inFileName, "rb"); - if (inFile == NULL) - { - DISPLAY("Pb opening %s\n", inFileName); - return 11; - } - - /* Memory allocation & restrictions */ - inFileSize = BMK_GetFileSize(inFileName); - benchedSize = BMK_findMaxMem(inFileSize * 3) / 3; - if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize; - if (benchedSize < inFileSize) - DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", inFileName, (int)(benchedSize >> 20)); - srcBuffer = malloc(benchedSize); - if (!srcBuffer) - { - DISPLAY("\nError: not enough memory!\n"); - fclose(inFile); - return 12; - } - - /* Fill input buffer */ - DISPLAY("Loading %s... \r", inFileName); - readSize = fread(srcBuffer, 1, benchedSize, inFile); - fclose(inFile); - - if (readSize != benchedSize) - { - DISPLAY("\nError: problem reading file '%s' !! \n", inFileName); - free(srcBuffer); - return 13; - } - - /* Bench */ - if (cLevel<0) + if (cLevel < 0) { int l; for (l=1; l <= -cLevel; l++) - result = BMK_benchMem(srcBuffer, benchedSize, inFileName, l); + BMK_benchMem(srcBuffer, benchedSize, + displayName, l, + fileSizes, nbFiles, + dictBuffer, dictBufferSize); + return; } - else - result = BMK_benchMem(srcBuffer, benchedSize, inFileName, cLevel); + BMK_benchMem(srcBuffer, benchedSize, + displayName, cLevel, + fileSizes, nbFiles, + dictBuffer, dictBufferSize); +} + +static U64 BMK_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles) +{ + U64 total = 0; + unsigned n; + for (n=0; n bufferSize-pos) fileSize = bufferSize-pos; + readSize = fread(buff+pos, 1, (size_t)fileSize, f); + if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]); + pos += readSize; + fileSizes[n] = (size_t)fileSize; + fclose(f); + } +} + +static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles, + const char* dictFileName, int cLevel) +{ + void* srcBuffer; + size_t benchedSize; + void* dictBuffer = NULL; + size_t dictBufferSize = 0; + size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t)); + U64 totalSizeToLoad = BMK_getTotalFileSize(fileNamesTable, nbFiles); + char mfName[20] = {0}; + const char* displayName = NULL; + + if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes"); + + /* Load dictionary */ + if (dictFileName != NULL) + { + U64 dictFileSize = BMK_getFileSize(dictFileName); + if (dictFileSize > 64 MB) EXM_THROW(10, "dictionary file %s too large", dictFileName); + dictBufferSize = (size_t)dictFileSize; + dictBuffer = malloc(dictBufferSize); + if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize); + BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1); + } + + /* Memory allocation & restrictions */ + benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3; + if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad; + if (benchedSize < totalSizeToLoad) + DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20)); + srcBuffer = malloc(benchedSize); + if (!srcBuffer) EXM_THROW(12, "not enough memory"); + + /* Load input buffer */ + BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles); + + /* Bench */ + snprintf (mfName, sizeof(mfName), " %u files", nbFiles); + if (nbFiles > 1) displayName = mfName; + else displayName = fileNamesTable[0]; + + BMK_benchCLevel(srcBuffer, benchedSize, + displayName, cLevel, + fileSizes, nbFiles, + dictBuffer, dictBufferSize); /* clean up */ free(srcBuffer); - DISPLAY("\n"); - return result; + free(dictBuffer); + free(fileSizes); } -static int BMK_syntheticTest(int cLevel, double compressibility) +static void BMK_syntheticTest(int cLevel, double compressibility) { + char name[20] = {0}; size_t benchedSize = 10000000; void* srcBuffer = malloc(benchedSize); - int result=0; - char name[20] = {0}; /* Memory allocation */ - if (!srcBuffer) - { - DISPLAY("\nError: not enough memory!\n"); - free(srcBuffer); - return 12; - } + if (!srcBuffer) EXM_THROW(21, "not enough memory"); /* Fill input buffer */ RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0); /* Bench */ -#ifdef _MSC_VER - sprintf_s(name, 20, "Synthetic %2u%%", (unsigned)(compressibility*100)); -#else - snprintf (name, 20, "Synthetic %2u%%", (unsigned)(compressibility*100)); -#endif - /* Bench */ - if (cLevel<0) - { - int l; - for (l=1; l <= -cLevel; l++) - result = BMK_benchMem(srcBuffer, benchedSize, name, l); - } - else - result = BMK_benchMem(srcBuffer, benchedSize, name, cLevel); + snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100)); + BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, &benchedSize, 1, NULL, 0); - /* End */ + /* clean up */ free(srcBuffer); - DISPLAY("\n"); - return result; } -int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, unsigned cLevel) +int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, + const char* dictFileName, int cLevel) { double compressibility = (double)g_compressibilityDefault / 100; if (nbFiles == 0) - { BMK_syntheticTest(cLevel, compressibility); - } else - { - /* Loop for each file */ - unsigned fileIdx = 0; - while (fileIdx MAX_DICT_SIZE) - { - int seekResult; - if (dictSize > 1 GB) EXM_THROW(21, "Dictionary file %s is too large", dictFileName); /* avoid extreme cases */ - DISPLAYLEVEL(2,"Dictionary %s is too large : using last %u bytes only \n", dictFileName, MAX_DICT_SIZE); - seekResult = fseek(dictHandle, (size_t)(dictSize-MAX_DICT_SIZE), SEEK_SET); /* use end of file */ - if (seekResult != 0) EXM_THROW(21, "Error seeking into dictionary file %s", dictFileName); - dictSize = MAX_DICT_SIZE; - } - dictBuff = (BYTE*)malloc((size_t)dictSize); - if (dictBuff==NULL) EXM_THROW(20, "Allocation error : not enough memory for dictBuff"); - readSize = fread(dictBuff, 1, (size_t)dictSize, dictHandle); - if (readSize!=dictSize) EXM_THROW(21, "Error reading dictionary file %s", dictFileName); - fclose(dictHandle); + DISPLAYLEVEL(4,"Using stdin for input\n"); + *fileInPtr = stdin; + SET_BINARY_MODE(stdin); + } + else + { + *fileInPtr = fopen(srcFileName, "rb"); } + if ( *fileInPtr==0 ) + { + DISPLAYLEVEL(1, "Unable to access file for processing: %s\n", srcFileName); + return 1; + } + + if (!strcmp (dstFileName, stdoutmark)) + { + DISPLAYLEVEL(4,"Using stdout for output\n"); + *fileOutPtr = stdout; + SET_BINARY_MODE(stdout); + } + else + { + /* Check if destination file already exists */ + if (!g_overwrite) + { + *fileOutPtr = fopen( dstFileName, "rb" ); + if (*fileOutPtr != 0) + { + /* prompt for overwrite authorization */ + int ch = 'N'; + fclose(*fileOutPtr); + DISPLAY("Warning : %s already exists \n", dstFileName); + if ((g_displayLevel <= 1) || (*fileInPtr == stdin)) + { + /* No interaction possible */ + DISPLAY("Operation aborted : %s already exists \n", dstFileName); + return 1; + } + DISPLAY("Overwrite ? (y/N) : "); + while((ch = getchar()) != '\n' && ch != EOF); /* flush integrated */ + if ((ch!='Y') && (ch!='y')) + { + DISPLAY("No. Operation aborted : %s already exists \n", dstFileName); + return 1; + } + } + } + *fileOutPtr = fopen( dstFileName, "wb" ); + } + + if (*fileOutPtr==0) EXM_THROW(13, "Pb opening %s", dstFileName); + + return 0; +} + +/*!FIO_loadFile +* creates a buffer, pointed by *bufferPtr, +* loads "filename" content into it +* up to MAX_DICT_SIZE bytes +*/ +static size_t FIO_loadFile(void** bufferPtr, const char* fileName) +{ + FILE* fileHandle; + size_t readSize; + U64 fileSize; + + *bufferPtr = NULL; + if (fileName == NULL) + return 0; + + DISPLAYLEVEL(4,"Loading %s as dictionary \n", fileName); + fileHandle = fopen(fileName, "rb"); + if (fileHandle==0) EXM_THROW(31, "Error opening file %s", fileName); + fileSize = FIO_getFileSize(fileName); + if (fileSize > MAX_DICT_SIZE) + { + 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"); + readSize = fread(*bufferPtr, 1, (size_t)fileSize, fileHandle); + if (readSize!=fileSize) EXM_THROW(35, "Error reading dictionary file %s", fileName); + fclose(fileHandle); + return (size_t)fileSize; +} + + +/* ********************************************************************** +* Compression +************************************************************************/ +typedef struct { + void* srcBuffer; + size_t srcBufferSize; + void* dstBuffer; + size_t dstBufferSize; + void* dictBuffer; + size_t dictBufferSize; + ZBUFF_CCtx* ctx; +} 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 */ + ress.dictBufferSize = FIO_loadFile(&(ress.dictBuffer), dictFileName); + + 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)); +} + + +/* + * FIO_compressFilename_extRess() + * result : 0 : compression completed correctly + * 1 : missing or pb opening srcFileName + */ +static int FIO_compressFilename_extRess(cRess_t ress, + const char* dstFileName, const char* srcFileName, + int cLevel) +{ + FILE* srcFile; + FILE* dstFile; + U64 filesize = 0; + U64 compressedfilesize = 0; + size_t dictSize = ress.dictBufferSize; + size_t sizeCheck, errorCode; + + /* File check */ + if (FIO_getFiles(&dstFile, &srcFile, dstFileName, srcFileName)) return 1; + /* init */ - FIO_getFileHandles(&finput, &foutput, input_filename, output_filename); - filesize = FIO_getFileSize(input_filename) + dictSize; - errorCode = ZBUFF_compressInit_advanced(ctx, ZSTD_getParams(cLevel, filesize)); - if (ZBUFF_isError(errorCode)) EXM_THROW(22, "Error initializing compression"); - errorCode = ZBUFF_compressWithDictionary(ctx, dictBuff, (size_t)dictSize); + filesize = FIO_getFileSize(srcFileName) + dictSize; + errorCode = ZBUFF_compressInit_advanced(ress.ctx, ZSTD_getParams(cLevel, filesize)); + if (ZBUFF_isError(errorCode)) EXM_THROW(21, "Error initializing compression"); + errorCode = ZBUFF_compressWithDictionary(ress.ctx, ress.dictBuffer, ress.dictBufferSize); if (ZBUFF_isError(errorCode)) EXM_THROW(22, "Error initializing dictionary"); /* Main compression loop */ @@ -296,7 +365,7 @@ unsigned long long FIO_compressFilename(const char* output_filename, const char* size_t inSize; /* Fill input Buffer */ - inSize = fread(inBuff, (size_t)1, inBuffSize, finput); + inSize = fread(ress.srcBuffer, (size_t)1, ress.srcBufferSize, srcFile); if (inSize==0) break; filesize += inSize; DISPLAYUPDATE(2, "\rRead : %u MB ", (U32)(filesize>>20)); @@ -304,8 +373,8 @@ unsigned long long FIO_compressFilename(const char* output_filename, const char* { /* Compress (buffered streaming ensures appropriate formatting) */ size_t usedInSize = inSize; - size_t cSize = outBuffSize; - size_t result = ZBUFF_compressContinue(ctx, outBuff, &cSize, inBuff, &usedInSize); + size_t cSize = ress.dstBufferSize; + size_t result = ZBUFF_compressContinue(ress.ctx, ress.dstBuffer, &cSize, ress.srcBuffer, &usedInSize); if (ZBUFF_isError(result)) EXM_THROW(23, "Compression error : %s ", ZBUFF_getErrorName(result)); if (inSize != usedInSize) @@ -313,8 +382,8 @@ unsigned long long FIO_compressFilename(const char* output_filename, const char* EXM_THROW(24, "Compression error : input block not fully consumed"); /* Write cBlock */ - sizeCheck = fwrite(outBuff, 1, cSize, foutput); - if (sizeCheck!=cSize) EXM_THROW(25, "Write error : cannot write compressed block into %s", output_filename); + sizeCheck = fwrite(ress.dstBuffer, 1, cSize, dstFile); + if (sizeCheck!=cSize) EXM_THROW(25, "Write error : cannot write compressed block into %s", dstFileName); compressedfilesize += cSize; } @@ -323,12 +392,12 @@ unsigned long long FIO_compressFilename(const char* output_filename, const char* /* End of Frame */ { - size_t cSize = outBuffSize; - size_t result = ZBUFF_compressEnd(ctx, outBuff, &cSize); + size_t cSize = ress.dstBufferSize; + size_t result = ZBUFF_compressEnd(ress.ctx, ress.dstBuffer, &cSize); if (result!=0) EXM_THROW(26, "Compression error : cannot create frame end"); - sizeCheck = fwrite(outBuff, 1, cSize, foutput); - if (sizeCheck!=cSize) EXM_THROW(27, "Write error : cannot write frame end into %s", output_filename); + sizeCheck = fwrite(ress.dstBuffer, 1, cSize, dstFile); + if (sizeCheck!=cSize) EXM_THROW(27, "Write error : cannot write frame end into %s", dstFileName); compressedfilesize += cSize; } @@ -338,52 +407,149 @@ unsigned long long FIO_compressFilename(const char* output_filename, const char* (unsigned long long) filesize, (unsigned long long) compressedfilesize, (double)compressedfilesize/filesize*100); /* clean */ - free(inBuff); - free(outBuff); - free(dictBuff); - ZBUFF_freeCCtx(ctx); - fclose(finput); - if (fclose(foutput)) EXM_THROW(28, "Write error : cannot properly close %s", output_filename); + fclose(srcFile); + if (fclose(dstFile)) EXM_THROW(28, "Write error : cannot properly close %s", dstFileName); - return compressedfilesize; + return 0; } -unsigned long long FIO_decompressFrame(FILE* foutput, FILE* finput, - BYTE* inBuff, size_t inBuffSize, size_t alreadyLoaded, - BYTE* outBuff, size_t outBuffSize, - BYTE* dictBuff, size_t dictSize, - ZBUFF_DCtx* dctx) +int FIO_compressFilename(const char* dstFileName, const char* srcFileName, + const char* dictFileName, int compressionLevel) +{ + clock_t start, end; + cRess_t ress; + int issueWithSrcFile = 0; + + /* Init */ + start = clock(); + ress = FIO_createCResources(dictFileName); + + /* Compress File */ + issueWithSrcFile += FIO_compressFilename_extRess(ress, dstFileName, srcFileName, compressionLevel); + + /* Free resources */ + FIO_freeCResources(ress); + + /* Final Status */ + end = clock(); + { + double seconds = (double)(end - start) / CLOCKS_PER_SEC; + DISPLAYLEVEL(4, "Completed in %.2f sec \n", seconds); + } + + return issueWithSrcFile; +} + + +#define FNSPACE 30 +int FIO_compressMultipleFilenames(const char** inFileNamesTable, unsigned nbFiles, + const char* suffix, + const char* dictFileName, int compressionLevel) +{ + unsigned u; + int missed_files = 0; + char* dstFileName = (char*)malloc(FNSPACE); + size_t dfnSize = FNSPACE; + const size_t suffixSize = strlen(suffix); + cRess_t ress; + + /* init */ + ress = FIO_createCResources(dictFileName); + + /* loop on each file */ + for (u=0; u>20) ); if (toRead == 0) break; - if (readSize) continue; /* still some data left within inBuff */ + if (readSize) EXM_THROW(38, "Decoding error : should consume entire input"); /* Fill input buffer */ - if (toRead > inBuffSize) EXM_THROW(34, "too large block"); - readSize = fread(inBuff, 1, toRead, finput); + if (toRead > ress.srcBufferSize) EXM_THROW(34, "too large block"); + readSize = fread(ress.srcBuffer, 1, toRead, finput); if (readSize != toRead) EXM_THROW(35, "Read error"); } @@ -391,88 +557,96 @@ unsigned long long FIO_decompressFrame(FILE* foutput, FILE* finput, } -unsigned long long FIO_decompressFilename(const char* output_filename, const char* input_filename, const char* dictFileName) +static int FIO_decompressFile_extRess(dRess_t ress, + const char* dstFileName, const char* srcFileName) { - FILE* finput, *foutput; - BYTE* inBuff=NULL; - size_t inBuffSize = ZBUFF_recommendedDInSize(); - BYTE* outBuff=NULL; - size_t outBuffSize = ZBUFF_recommendedDOutSize(); - BYTE* dictBuff=NULL; - size_t dictSize = 0; - U64 filesize = 0; - size_t toRead; - - /* dictionary */ - if (dictFileName) - { - FILE* dictHandle; - size_t readSize; - DISPLAYLEVEL(4,"Using %s as dictionary \n", dictFileName); - dictHandle = fopen(dictFileName, "rb"); - if (dictHandle==0) EXM_THROW(21, "Error opening dictionary file %s", dictFileName); - dictSize = (size_t)FIO_getFileSize(dictFileName); - if (dictSize > MAX_DICT_SIZE) - { - int seekResult; - if (dictSize > 1 GB) EXM_THROW(21, "Dictionary file %s is too large", dictFileName); /* avoid extreme cases */ - DISPLAYLEVEL(2,"Dictionary %s is too large : using last %u bytes only \n", dictFileName, MAX_DICT_SIZE); - seekResult = fseek(dictHandle, dictSize-MAX_DICT_SIZE, SEEK_SET); /* use end of file */ - if (seekResult != 0) EXM_THROW(21, "Error seeking into dictionary file %s", dictFileName); - dictSize = MAX_DICT_SIZE; - } - dictBuff = (BYTE*)malloc(dictSize); - if (dictBuff==NULL) EXM_THROW(20, "Allocation error : not enough memory for dictBuff"); - readSize = fread(dictBuff, 1, (size_t)dictSize, dictHandle); - if (readSize!=dictSize) EXM_THROW(21, "Error reading dictionary file %s", dictFileName); - fclose(dictHandle); - } + unsigned long long filesize = 0; + FILE* srcFile; + FILE* dstFile; /* Init */ - ZBUFF_DCtx* dctx = ZBUFF_createDCtx(); - FIO_getFileHandles(&finput, &foutput, input_filename, output_filename); - - /* Allocate Memory (if needed) */ - inBuff = (BYTE*)malloc(inBuffSize); - outBuff = (BYTE*)malloc(outBuffSize); - if (!inBuff || !outBuff) EXM_THROW(33, "Allocation error : not enough memory"); + if (FIO_getFiles(&dstFile, &srcFile, dstFileName, srcFileName)) return 1; /* for each frame */ for ( ; ; ) { size_t sizeCheck; /* check magic number -> version */ - toRead = 4; - sizeCheck = fread(inBuff, (size_t)1, toRead, finput); + size_t toRead = 4; + sizeCheck = fread(ress.srcBuffer, (size_t)1, toRead, srcFile); if (sizeCheck==0) break; /* no more input */ if (sizeCheck != toRead) EXM_THROW(31, "Read error : cannot read header"); #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1) - if (ZSTD_isLegacy(MEM_readLE32(inBuff))) + if (ZSTD_isLegacy(MEM_readLE32(ress.srcBuffer))) { - filesize += FIO_decompressLegacyFrame(foutput, finput, MEM_readLE32(inBuff)); + filesize += FIO_decompressLegacyFrame(dstFile, srcFile, MEM_readLE32(ress.srcBuffer)); continue; } #endif /* ZSTD_LEGACY_SUPPORT */ - filesize += FIO_decompressFrame(foutput, finput, - inBuff, inBuffSize, toRead, - outBuff, outBuffSize, - dictBuff, dictSize, - dctx); + filesize += FIO_decompressFrame(ress, dstFile, srcFile, toRead); } + /* Final Status */ DISPLAYLEVEL(2, "\r%79s\r", ""); - DISPLAYLEVEL(2, "Decoded %llu bytes \n", (long long unsigned)filesize); + DISPLAYLEVEL(2, "Successfully decoded %llu bytes \n", filesize); - /* clean */ - free(inBuff); - free(outBuff); - free(dictBuff); - ZBUFF_freeDCtx(dctx); - fclose(finput); - if (fclose(foutput)) EXM_THROW(38, "Write error : cannot properly close %s", output_filename); + /* Close */ + fclose(srcFile); + if (fclose(dstFile)) EXM_THROW(38, "Write error : cannot properly close %s", dstFileName); - return filesize; + return 0; } +int FIO_decompressFilename(const char* dstFileName, const char* srcFileName, + const char* dictFileName) +{ + int missingFiles = 0; + dRess_t ress = FIO_createDResources(dictFileName); + + missingFiles += FIO_decompressFile_extRess(ress, dstFileName, srcFileName); + + FIO_freeDResources(ress); + return missingFiles; +} + + +#define MAXSUFFIXSIZE 8 +int FIO_decompressMultipleFilenames(const char** srcNamesTable, unsigned nbFiles, + const char* suffix, + const char* dictFileName) +{ + unsigned u; + int skippedFiles = 0; + int missingFiles = 0; + char* dstFileName = (char*)malloc(FNSPACE); + size_t dfnSize = FNSPACE; + const size_t suffixSize = strlen(suffix); + dRess_t ress; + + if (dstFileName==NULL) EXM_THROW(70, "not enough memory for dstFileName"); + ress = FIO_createDResources(dictFileName); + + for (u=0; u /dev/full" echo foo | $ZSTD > /dev/full && die "write error not detected!" +echo "echo foo | $ZSTD | $ZSTD -d > /dev/full" echo foo | $ZSTD | $ZSTD -d > /dev/full && die "write error not detected!" + echo "*** dictionary tests *** " ./datagen > tmpDict @@ -49,6 +52,19 @@ echo "*** dictionary tests *** " ./datagen -g1M | $ZSTD -D tmpDict | $ZSTD -D tmpDict -dv | md5sum > tmp2 diff -q tmp1 tmp2 +echo "*** multiple files tests *** " + +./datagen -s1 > tmp1 2> /dev/null +./datagen -s2 -g100K > tmp2 2> /dev/null +./datagen -s3 -g1M > tmp3 2> /dev/null +$ZSTD -f -m tmp* +ls -ls tmp* +rm tmp1 tmp2 tmp3 +$ZSTD -df -m *.zst +ls -ls tmp* +$ZSTD -f -m tmp1 notHere tmp2 && die "missing file not detected!" +rm tmp* + echo "**** zstd round-trip tests **** " roundTripTest diff --git a/programs/zstd.1 b/programs/zstd.1 index fdc8cc46..8d69c4dd 100644 --- a/programs/zstd.1 +++ b/programs/zstd.1 @@ -31,12 +31,21 @@ is equivalent to It is based on the \fBLZ77\fR family, with FSE & huff0 entropy stage. zstd offers compression speed > 200 MB/s per core. It also features a fast decoder, with speed > 500 MB/s per core. + +\fBzstd\fR command line is generally similar to gzip, but features the following differences : + - Original files are preserved + - By default, \fBzstd file1 file2\fR means : compress file1 \fBinto\fR file2. + Use \fB-m\fR command if you want : compress file1 into file1.zstd and file2 into file2.zst + - By default, when compressing files, \fBzstd\fR displays advancement notification and result summary. + Use \fB-q\fR to turn them off + + \fBzstd\fR supports the following options : .SH OPTIONS .TP -.B \-1 - fast compression (default) +.B \-# + # compression level [1-19](default:1) .TP .B \-d decompression @@ -44,6 +53,14 @@ It also features a fast decoder, with speed > 500 MB/s per core. .B \-f overwrite output without prompting .TP +.BR \-m ", " --multiple + multiple files mode + In this mode, multiple files on the command line means compression or decompression of each named file + Notifications are also turned off by default +.TP +.B \-D + Use next file as dictionary content for compress / decompression +.TP .BR \-h/\-H ", " --help display help/long help and exit .TP @@ -53,20 +70,17 @@ It also features a fast decoder, with speed > 500 MB/s per core. .BR \-v ", " --verbose verbose mode .TP -.B \-q - suppress warnings; specify twice to suppress errors too +.BR \-q ", " --quiet + suppress warnings and notifications; specify twice to suppress errors too .TP -.B \-c +.B \-c force write to standard output, even if it is the console .TP -.B \-t - test compressed file integrity -.TP .B \-z force compression .TP -.B \-b - benchmark file(s) +.B \-b# + benchmark file(s) using compression level # .TP .B \-i# iteration loops [1-9](default : 3), benchmark mode only diff --git a/programs/zstdcli.c b/programs/zstdcli.c index e04e40fd..564686c6 100644 --- a/programs/zstdcli.c +++ b/programs/zstdcli.c @@ -43,23 +43,26 @@ #include /* fprintf, getchar */ #include /* exit, calloc, free */ #include /* strcmp, strlen */ -#include "bench.h" /* BMK_benchFiles, BMK_SetNbIterations */ #include "fileio.h" +#ifndef ZSTD_NOBENCH +# include "bench.h" /* BMK_benchFiles, BMK_SetNbIterations */ +#endif +#include "zstd.h" /* ZSTD version numbers */ /************************************** * OS-specific Includes **************************************/ #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) -# include // _O_BINARY -# include // _setmode, _isatty +# include /* _O_BINARY */ +# include /* _setmode, _isatty */ # ifdef __MINGW32__ /* int _fileno(FILE *stream); // seems no longer useful // MINGW somehow forgets to include this windows declaration into */ # endif # define SET_BINARY_MODE(file) _setmode(_fileno(file), _O_BINARY) # define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream)) #else -# include // isatty +# include /* isatty */ # define SET_BINARY_MODE(file) # define IS_CONSOLE(stdStream) isatty(fileno(stdStream)) #endif @@ -70,7 +73,9 @@ **************************************/ #define COMPRESSOR_NAME "zstd command line interface" #ifndef ZSTD_VERSION -# define ZSTD_VERSION "v0.4.4" +# define QUOTE(str) #str +# define EXPAND_AND_QUOTE(str) QUOTE(str) +# define ZSTD_VERSION "v" EXPAND_AND_QUOTE(ZSTD_VERSION_MAJOR) "." EXPAND_AND_QUOTE(ZSTD_VERSION_MINOR) "." EXPAND_AND_QUOTE(ZSTD_VERSION_RELEASE) #endif #define AUTHOR "Yann Collet" #define WELCOME_MESSAGE "*** %s %i-bits %s, by %s (%s) ***\n", COMPRESSOR_NAME, (int)(sizeof(void*)*8), ZSTD_VERSION, AUTHOR, __DATE__ @@ -135,9 +140,9 @@ static int usage_advanced(const char* programName) DISPLAY( " -V : display Version number and exit\n"); DISPLAY( " -v : verbose mode\n"); DISPLAY( " -q : suppress warnings; specify twice to suppress errors too\n"); + DISPLAY( " -m : multiple input filenames mode \n"); DISPLAY( " -c : force write to standard output, even if it is the console\n"); DISPLAY( " -D file: use file content as Dictionary \n"); - //DISPLAY( " -t : test compressed file integrity\n"); #ifndef ZSTD_NOBENCH DISPLAY( "Benchmark arguments :\n"); DISPLAY( " -b# : benchmark file(s), using # compression level (default : 1) \n"); @@ -172,22 +177,24 @@ int main(int argCount, const char** argv) decode=0, forceStdout=0, main_pause=0, - nextEntryIsDictionary=0; + nextEntryIsDictionary=0, + multiple=0, + operationResult=0; unsigned cLevel = 1; + const char** filenameTable = (const char**)malloc(argCount * sizeof(const char*)); /* argCount >= 1 */ + unsigned filenameIdx = 0; const char* programName = argv[0]; - const char* inFileName = NULL; const char* outFileName = NULL; const char* dictFileName = NULL; char* dynNameSpace = NULL; const char extension[] = ZSTD_EXTENSION; - unsigned fileNameStart = 0; - unsigned nbFiles = 0; int rangeBench = 1; /* init */ - (void)rangeBench; (void)nbFiles; (void)fileNameStart; /* not used when ZSTD_NOBENCH set */ + (void)rangeBench; /* not used when ZSTD_NOBENCH set */ + if (filenameTable==NULL) { DISPLAY("not enough memory\n"); exit(1); } displayOut = stderr; - /* Pick out basename component. Don't rely on stdlib because of conflicting behavior. */ + /* Pick out program name from path. Don't rely on stdlib because of conflicting behavior */ for (i = (int)strlen(programName); i > 0; i--) { if (programName[i] == '/') { i++; break; } } programName += i; @@ -205,7 +212,9 @@ int main(int argCount, const char** argv) /* long commands (--long-word) */ if (!strcmp(argument, "--version")) { displayOut=stdout; DISPLAY(WELCOME_MESSAGE); return 0; } if (!strcmp(argument, "--help")) { displayOut=stdout; return usage_advanced(programName); } + if (!strcmp(argument, "--multiple")) { multiple=1; continue; } if (!strcmp(argument, "--verbose")) { displayLevel=4; continue; } + if (!strcmp(argument, "--quiet")) { displayLevel--; continue; } /* Decode commands (note : aggregated commands are allowed) */ if (argument[0]=='-') @@ -213,9 +222,8 @@ int main(int argCount, const char** argv) /* '-' means stdin/stdout */ if (argument[1]==0) { - if (!inFileName) inFileName=stdinmark; - else outFileName=stdoutmark; - continue; + if (!filenameIdx) { filenameIdx=1, filenameTable[0]=stdinmark; continue; } + outFileName=stdoutmark; continue; } argument++; @@ -248,6 +256,9 @@ int main(int argCount, const char** argv) /* Decoding */ case 'd': decode=1; argument++; break; + /* Multiple input files */ + case 'm': multiple=1; argument++; break; + /* Force stdout, even if stdout==console */ case 'c': forceStdout=1; outFileName=stdoutmark; displayLevel=1; argument++; break; @@ -323,16 +334,8 @@ int main(int argCount, const char** argv) continue; } - /* first provided filename is input */ - if (!inFileName) { inFileName = argument; fileNameStart = i; nbFiles = argCount-i; continue; } - - /* second provided filename is output */ - if (!outFileName) - { - outFileName = argument; - if (!strcmp (outFileName, nullString)) outFileName = nulmark; - continue; - } + /* add filename to list */ + filenameTable[filenameIdx++] = argument; } /* Welcome message (if verbose) */ @@ -342,27 +345,28 @@ int main(int argCount, const char** argv) if (bench) { #ifndef ZSTD_NOBENCH - BMK_benchFiles(argv+fileNameStart, nbFiles, cLevel*rangeBench); + BMK_benchFiles(filenameTable, filenameIdx, dictFileName, cLevel*rangeBench); #endif goto _end; } /* No input filename ==> use stdin */ - if(!inFileName) { inFileName=stdinmark; } + if(!filenameIdx) filenameIdx=1, filenameTable[0]=stdinmark; /* Check if input defined as console; trigger an error in this case */ - if (!strcmp(inFileName, stdinmark) && IS_CONSOLE(stdin) ) return badusage(programName); + if (!strcmp(filenameTable[0], stdinmark) && IS_CONSOLE(stdin) ) return badusage(programName); /* No output filename ==> try to select one automatically (when possible) */ - while (!outFileName) + if (filenameIdx>=2) outFileName = filenameTable[1]; + while (!outFileName) /* while : just to allow break statement */ { if (!IS_CONSOLE(stdout)) { outFileName=stdoutmark; break; } /* Default to stdout whenever possible (i.e. not a console) */ if (!decode) /* compression to file */ { - size_t l = strlen(inFileName); + size_t l = strlen(filenameTable[0]); dynNameSpace = (char*)calloc(1,l+5); if (dynNameSpace==NULL) { DISPLAY("not enough memory\n"); exit(1); } - strcpy(dynNameSpace, inFileName); + strcpy(dynNameSpace, filenameTable[0]); strcpy(dynNameSpace+l, ZSTD_EXTENSION); outFileName = dynNameSpace; DISPLAYLEVEL(2, "Compressed filename will be : %s \n", outFileName); @@ -370,8 +374,8 @@ int main(int argCount, const char** argv) } /* decompression to file (automatic name will work only if input filename has correct format extension) */ { - size_t filenameSize = strlen(inFileName); - if (strcmp(inFileName + (filenameSize-4), extension)) + size_t filenameSize = strlen(filenameTable[0]); + if (strcmp(filenameTable[0] + (filenameSize-4), extension)) { DISPLAYLEVEL(1, "unknown suffix - cannot determine destination filename\n"); return badusage(programName); @@ -379,7 +383,7 @@ int main(int argCount, const char** argv) dynNameSpace = (char*)calloc(1,filenameSize+1); if (dynNameSpace==NULL) { DISPLAY("not enough memory\n"); exit(1); } outFileName = dynNameSpace; - strcpy(dynNameSpace, inFileName); + strcpy(dynNameSpace, filenameTable[0]); dynNameSpace[filenameSize-4]=0; DISPLAYLEVEL(2, "Decoding file %s \n", outFileName); } @@ -388,18 +392,36 @@ int main(int argCount, const char** argv) /* Check if output is defined as console; trigger an error in this case */ if (!strcmp(outFileName,stdoutmark) && IS_CONSOLE(stdout) && !forceStdout) return badusage(programName); - /* No warning message in pure pipe mode (stdin + stdout) */ - if (!strcmp(inFileName, stdinmark) && !strcmp(outFileName,stdoutmark) && (displayLevel==2)) displayLevel=1; + /* No warning message in pure pipe mode (stdin + stdout) or multiple mode */ + if (!strcmp(filenameTable[0], stdinmark) && !strcmp(outFileName,stdoutmark) && (displayLevel==2)) displayLevel=1; + if (multiple && (displayLevel==2)) displayLevel=1; + + if ((!multiple) && (filenameIdx>2)) + { + DISPLAY("Too many files on the command line (%u > 2). Do you mean -m ? \n", filenameIdx); + return filenameIdx; + } /* IO Stream/File */ FIO_setNotificationLevel(displayLevel); if (decode) - FIO_decompressFilename(outFileName, inFileName, dictFileName); + { + if (multiple) + operationResult = FIO_decompressMultipleFilenames(filenameTable, filenameIdx, ZSTD_EXTENSION, dictFileName); + else + operationResult = FIO_decompressFilename(outFileName, filenameTable[0], dictFileName); + } else - FIO_compressFilename(outFileName, inFileName, dictFileName, cLevel); + { + if (multiple) + operationResult = FIO_compressMultipleFilenames(filenameTable, filenameIdx, ZSTD_EXTENSION, dictFileName, cLevel); + else + operationResult = FIO_compressFilename(outFileName, filenameTable[0], dictFileName, cLevel); + } _end: if (main_pause) waitEnter(); free(dynNameSpace); - return 0; + free((void*)filenameTable); + return operationResult; }