2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
12 /* *************************************
14 ***************************************/
15 #ifdef _MSC_VER /* Visual */
16 # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
17 # pragma warning(disable : 4204) /* non-constant aggregate initializer */
19 #if defined(__MINGW32__) && !defined(_POSIX_SOURCE)
20 # define _POSIX_SOURCE 1 /* disable %llu warnings with MinGW on Windows */
23 /*-*************************************
25 ***************************************/
26 #include "platform.h" /* Large Files support, SET_BINARY_MODE */
27 #include "util.h" /* UTIL_getFileSize, UTIL_isRegularFile, UTIL_isSameFile */
28 #include <stdio.h> /* fprintf, fopen, fread, _fileno, stdin, stdout */
29 #include <stdlib.h> /* malloc, free */
30 #include <string.h> /* strcmp, strlen */
32 #include <errno.h> /* errno */
34 #include "timefn.h" /* UTIL_getTime, UTIL_clockSpanMicro */
36 #if defined (_MSC_VER)
37 # include <sys/stat.h>
41 #include "mem.h" /* U32, U64 */
44 #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_magicNumber, ZSTD_frameHeaderSize_max */
46 #include "zstd_errors.h" /* ZSTD_error_frameParameter_windowTooLarge */
48 #if defined(ZSTD_GZCOMPRESS) || defined(ZSTD_GZDECOMPRESS)
50 # if !defined(z_const)
55 #if defined(ZSTD_LZMACOMPRESS) || defined(ZSTD_LZMADECOMPRESS)
59 #define LZ4_MAGICNUMBER 0x184D2204
60 #if defined(ZSTD_LZ4COMPRESS) || defined(ZSTD_LZ4DECOMPRESS)
61 # define LZ4F_ENABLE_OBSOLETE_ENUMS
62 # include <lz4frame.h>
67 /*-*************************************
69 ***************************************/
74 #define ADAPT_WINDOWLOG_DEFAULT 23 /* 8 MB */
75 #define DICTSIZE_MAX (32 MB) /* protection against large input (attack scenario) */
80 /*-*************************************
82 ***************************************/
84 struct FIO_display_prefs_s {
85 int displayLevel; /* 0 : no display; 1: errors; 2: + result + interaction + warnings; 3: + progression; 4: + information */
89 static FIO_display_prefs_t g_display_prefs = {2, 0};
91 #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
92 #define DISPLAYOUT(...) fprintf(stdout, __VA_ARGS__)
93 #define DISPLAYLEVEL(l, ...) { if (g_display_prefs.displayLevel>=l) { DISPLAY(__VA_ARGS__); } }
95 static const U64 g_refreshRate = SEC_TO_MICRO / 6;
96 static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
98 #define READY_FOR_UPDATE() (!g_display_prefs.noProgress && UTIL_clockSpanMicro(g_displayClock) > g_refreshRate)
99 #define DELAY_NEXT_UPDATE() { g_displayClock = UTIL_getTime(); }
100 #define DISPLAYUPDATE(l, ...) { \
101 if (g_display_prefs.displayLevel>=l && !g_display_prefs.noProgress) { \
102 if (READY_FOR_UPDATE() || (g_display_prefs.displayLevel>=4)) { \
103 DELAY_NEXT_UPDATE(); \
104 DISPLAY(__VA_ARGS__); \
105 if (g_display_prefs.displayLevel>=4) fflush(stderr); \
108 #undef MIN /* in case it would be already defined */
109 #define MIN(a,b) ((a) < (b) ? (a) : (b))
112 #define EXM_THROW(error, ...) \
114 DISPLAYLEVEL(1, "zstd: "); \
115 DISPLAYLEVEL(5, "Error defined at %s, line %i : \n", __FILE__, __LINE__); \
116 DISPLAYLEVEL(1, "error %i : ", error); \
117 DISPLAYLEVEL(1, __VA_ARGS__); \
118 DISPLAYLEVEL(1, " \n"); \
122 #define CHECK_V(v, f) \
124 if (ZSTD_isError(v)) { \
125 DISPLAYLEVEL(5, "%s \n", #f); \
126 EXM_THROW(11, "%s", ZSTD_getErrorName(v)); \
128 #define CHECK(f) { size_t err; CHECK_V(err, f); }
131 /*-************************************
132 * Signal (Ctrl-C trapping)
133 **************************************/
134 static const char* g_artefact = NULL;
135 static void INThandler(int sig)
137 assert(sig==SIGINT); (void)sig;
138 #if !defined(_MSC_VER)
139 signal(sig, SIG_IGN); /* this invocation generates a buggy warning in Visual Studio */
142 assert(UTIL_isRegularFile(g_artefact));
148 static void addHandler(char const* dstFileName)
150 if (UTIL_isRegularFile(dstFileName)) {
151 g_artefact = dstFileName;
152 signal(SIGINT, INThandler);
158 static void clearHandler(void)
160 if (g_artefact) signal(SIGINT, SIG_DFL);
165 /*-*********************************************************
166 * Termination signal trapping (Print debug stack trace)
167 ***********************************************************/
168 #if defined(__has_feature) && !defined(BACKTRACE_ENABLE) /* Clang compiler */
169 # if (__has_feature(address_sanitizer))
170 # define BACKTRACE_ENABLE 0
171 # endif /* __has_feature(address_sanitizer) */
172 #elif defined(__SANITIZE_ADDRESS__) && !defined(BACKTRACE_ENABLE) /* GCC compiler */
173 # define BACKTRACE_ENABLE 0
176 #if !defined(BACKTRACE_ENABLE)
177 /* automatic detector : backtrace enabled by default on linux+glibc and osx */
178 # if (defined(__linux__) && (defined(__GLIBC__) && !defined(__UCLIBC__))) \
179 || (defined(__APPLE__) && defined(__MACH__))
180 # define BACKTRACE_ENABLE 1
182 # define BACKTRACE_ENABLE 0
186 /* note : after this point, BACKTRACE_ENABLE is necessarily defined */
191 #include <execinfo.h> /* backtrace, backtrace_symbols */
193 #define MAX_STACK_FRAMES 50
195 static void ABRThandler(int sig) {
197 void* addrlist[MAX_STACK_FRAMES];
202 case SIGABRT: name = "SIGABRT"; break;
203 case SIGFPE: name = "SIGFPE"; break;
204 case SIGILL: name = "SIGILL"; break;
205 case SIGINT: name = "SIGINT"; break;
206 case SIGSEGV: name = "SIGSEGV"; break;
207 default: name = "UNKNOWN";
210 DISPLAY("Caught %s signal, printing stack:\n", name);
211 /* Retrieve current stack addresses. */
212 addrlen = backtrace(addrlist, MAX_STACK_FRAMES);
217 /* Create readable strings to each frame. */
218 symbollist = backtrace_symbols(addrlist, addrlen);
219 /* Print the stack trace, excluding calls handling the signal. */
220 for (i = ZSTD_START_SYMBOLLIST_FRAME; i < addrlen; i++) {
221 DISPLAY("%s\n", symbollist[i]);
224 /* Reset and raise the signal so default handler runs. */
225 signal(sig, SIG_DFL);
230 void FIO_addAbortHandler()
233 signal(SIGABRT, ABRThandler);
234 signal(SIGFPE, ABRThandler);
235 signal(SIGILL, ABRThandler);
236 signal(SIGSEGV, ABRThandler);
237 signal(SIGBUS, ABRThandler);
242 /*-************************************************************
243 * Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW
244 ***************************************************************/
245 #if defined(_MSC_VER) && _MSC_VER >= 1400
246 # define LONG_SEEK _fseeki64
247 # define LONG_TELL _ftelli64
248 #elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
249 # define LONG_SEEK fseeko
250 # define LONG_TELL ftello
251 #elif defined(__MINGW32__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS) && defined(__MSVCRT__)
252 # define LONG_SEEK fseeko64
253 # define LONG_TELL ftello64
254 #elif defined(_WIN32) && !defined(__DJGPP__)
255 # include <windows.h>
256 static int LONG_SEEK(FILE* file, __int64 offset, int origin) {
259 off.QuadPart = offset;
260 if (origin == SEEK_END)
262 else if (origin == SEEK_CUR)
263 method = FILE_CURRENT;
267 if (SetFilePointerEx((HANDLE) _get_osfhandle(_fileno(file)), off, NULL, method))
272 static __int64 LONG_TELL(FILE* file) {
273 LARGE_INTEGER off, newOff;
276 SetFilePointerEx((HANDLE) _get_osfhandle(_fileno(file)), off, &newOff, FILE_CURRENT);
277 return newOff.QuadPart;
280 # define LONG_SEEK fseek
281 # define LONG_TELL ftell
285 /*-*************************************
286 * Parameters: Typedefs
287 ***************************************/
291 /* Algorithm preferences */
292 FIO_compressionType_t compressionType;
293 U32 sparseFileSupport; /* 0: no sparse allowed; 1: auto (file yes, stdout no); 2: force sparse */
305 int ldmBucketSizeLog;
307 size_t targetCBlockSize;
308 ZSTD_literalCompressionMode_e literalCompressionMode;
314 /* Computation resources preferences */
320 /*-*************************************
321 * Parameters: Initialization
322 ***************************************/
324 #define FIO_OVERLAP_LOG_NOTSET 9999
325 #define FIO_LDM_PARAM_NOTSET 9999
328 FIO_prefs_t* FIO_createPreferences(void)
330 FIO_prefs_t* const ret = (FIO_prefs_t*)malloc(sizeof(FIO_prefs_t));
331 if (!ret) EXM_THROW(21, "Allocation error : not enough memory");
333 ret->compressionType = FIO_zstdCompression;
335 ret->sparseFileSupport = ZSTD_SPARSE_DEFAULT;
337 ret->checksumFlag = 1;
338 ret->removeSrcFile = 0;
342 ret->overlapLog = FIO_OVERLAP_LOG_NOTSET;
343 ret->adaptiveMode = 0;
345 ret->minAdaptLevel = -50; /* initializing this value requires a constant, so ZSTD_minCLevel() doesn't work */
346 ret->maxAdaptLevel = 22; /* initializing this value requires a constant, so ZSTD_maxCLevel() doesn't work */
349 ret->ldmMinMatch = 0;
350 ret->ldmBucketSizeLog = FIO_LDM_PARAM_NOTSET;
351 ret->ldmHashRateLog = FIO_LDM_PARAM_NOTSET;
352 ret->targetCBlockSize = 0;
353 ret->literalCompressionMode = ZSTD_lcm_auto;
357 void FIO_freePreferences(FIO_prefs_t* const prefs)
363 /*-*************************************
364 * Parameters: Display Options
365 ***************************************/
367 void FIO_setNotificationLevel(int level) { g_display_prefs.displayLevel=level; }
369 void FIO_setNoProgress(unsigned noProgress) { g_display_prefs.noProgress = noProgress; }
372 /*-*************************************
373 * Parameters: Setters
374 ***************************************/
376 void FIO_setCompressionType(FIO_prefs_t* const prefs, FIO_compressionType_t compressionType) { prefs->compressionType = compressionType; }
378 void FIO_overwriteMode(FIO_prefs_t* const prefs) { prefs->overwrite = 1; }
380 void FIO_setSparseWrite(FIO_prefs_t* const prefs, unsigned sparse) { prefs->sparseFileSupport = sparse; }
382 void FIO_setDictIDFlag(FIO_prefs_t* const prefs, int dictIDFlag) { prefs->dictIDFlag = dictIDFlag; }
384 void FIO_setChecksumFlag(FIO_prefs_t* const prefs, int checksumFlag) { prefs->checksumFlag = checksumFlag; }
386 void FIO_setRemoveSrcFile(FIO_prefs_t* const prefs, unsigned flag) { prefs->removeSrcFile = (flag>0); }
388 void FIO_setMemLimit(FIO_prefs_t* const prefs, unsigned memLimit) { prefs->memLimit = memLimit; }
390 void FIO_setNbWorkers(FIO_prefs_t* const prefs, int nbWorkers) {
391 #ifndef ZSTD_MULTITHREAD
392 if (nbWorkers > 0) DISPLAYLEVEL(2, "Note : multi-threading is disabled \n");
394 prefs->nbWorkers = nbWorkers;
397 void FIO_setBlockSize(FIO_prefs_t* const prefs, int blockSize) {
398 if (blockSize && prefs->nbWorkers==0)
399 DISPLAYLEVEL(2, "Setting block size is useless in single-thread mode \n");
400 prefs->blockSize = blockSize;
403 void FIO_setOverlapLog(FIO_prefs_t* const prefs, int overlapLog){
404 if (overlapLog && prefs->nbWorkers==0)
405 DISPLAYLEVEL(2, "Setting overlapLog is useless in single-thread mode \n");
406 prefs->overlapLog = overlapLog;
409 void FIO_setAdaptiveMode(FIO_prefs_t* const prefs, unsigned adapt) {
410 if ((adapt>0) && (prefs->nbWorkers==0))
411 EXM_THROW(1, "Adaptive mode is not compatible with single thread mode \n");
412 prefs->adaptiveMode = adapt;
415 void FIO_setRsyncable(FIO_prefs_t* const prefs, int rsyncable) {
416 if ((rsyncable>0) && (prefs->nbWorkers==0))
417 EXM_THROW(1, "Rsyncable mode is not compatible with single thread mode \n");
418 prefs->rsyncable = rsyncable;
421 void FIO_setTargetCBlockSize(FIO_prefs_t* const prefs, size_t targetCBlockSize) {
422 prefs->targetCBlockSize = targetCBlockSize;
425 void FIO_setLiteralCompressionMode(
426 FIO_prefs_t* const prefs,
427 ZSTD_literalCompressionMode_e mode) {
428 prefs->literalCompressionMode = mode;
431 void FIO_setAdaptMin(FIO_prefs_t* const prefs, int minCLevel)
433 #ifndef ZSTD_NOCOMPRESS
434 assert(minCLevel >= ZSTD_minCLevel());
436 prefs->minAdaptLevel = minCLevel;
439 void FIO_setAdaptMax(FIO_prefs_t* const prefs, int maxCLevel)
441 prefs->maxAdaptLevel = maxCLevel;
444 void FIO_setLdmFlag(FIO_prefs_t* const prefs, unsigned ldmFlag) {
445 prefs->ldmFlag = (ldmFlag>0);
448 void FIO_setLdmHashLog(FIO_prefs_t* const prefs, int ldmHashLog) {
449 prefs->ldmHashLog = ldmHashLog;
452 void FIO_setLdmMinMatch(FIO_prefs_t* const prefs, int ldmMinMatch) {
453 prefs->ldmMinMatch = ldmMinMatch;
456 void FIO_setLdmBucketSizeLog(FIO_prefs_t* const prefs, int ldmBucketSizeLog) {
457 prefs->ldmBucketSizeLog = ldmBucketSizeLog;
461 void FIO_setLdmHashRateLog(FIO_prefs_t* const prefs, int ldmHashRateLog) {
462 prefs->ldmHashRateLog = ldmHashRateLog;
466 /*-*************************************
468 ***************************************/
470 * @result : Unlink `fileName`, even if it's read-only */
471 static int FIO_remove(const char* path)
473 if (!UTIL_isRegularFile(path)) {
474 DISPLAYLEVEL(2, "zstd: Refusing to remove non-regular file %s \n", path);
477 #if defined(_WIN32) || defined(WIN32)
478 /* windows doesn't allow remove read-only files,
479 * so try to make it writable first */
480 chmod(path, _S_IWRITE);
485 /** FIO_openSrcFile() :
486 * condition : `srcFileName` must be non-NULL.
487 * @result : FILE* to `srcFileName`, or NULL if it fails */
488 static FILE* FIO_openSrcFile(const char* srcFileName)
490 assert(srcFileName != NULL);
491 if (!strcmp (srcFileName, stdinmark)) {
492 DISPLAYLEVEL(4,"Using stdin for input \n");
493 SET_BINARY_MODE(stdin);
497 if (!UTIL_fileExist(srcFileName)) {
498 DISPLAYLEVEL(1, "zstd: can't stat %s : %s -- ignored \n",
499 srcFileName, strerror(errno));
503 if (!UTIL_isRegularFile(srcFileName)) {
504 DISPLAYLEVEL(1, "zstd: %s is not a regular file -- ignored \n",
509 { FILE* const f = fopen(srcFileName, "rb");
511 DISPLAYLEVEL(1, "zstd: %s: %s \n", srcFileName, strerror(errno));
516 /** FIO_openDstFile() :
517 * condition : `dstFileName` must be non-NULL.
518 * @result : FILE* to `dstFileName`, or NULL if it fails */
519 static FILE* FIO_openDstFile(FIO_prefs_t* const prefs, const char* srcFileName, const char* dstFileName)
521 assert(dstFileName != NULL);
522 if (!strcmp (dstFileName, stdoutmark)) {
523 DISPLAYLEVEL(4,"Using stdout for output \n");
524 SET_BINARY_MODE(stdout);
525 if (prefs->sparseFileSupport == 1) {
526 prefs->sparseFileSupport = 0;
527 DISPLAYLEVEL(4, "Sparse File Support is automatically disabled on stdout ; try --sparse \n");
532 /* ensure dst is not the same as src */
533 if (srcFileName != NULL && UTIL_isSameFile(srcFileName, dstFileName)) {
534 DISPLAYLEVEL(1, "zstd: Refusing to open an output file which will overwrite the input file \n");
538 if (prefs->sparseFileSupport == 1) {
539 prefs->sparseFileSupport = ZSTD_SPARSE_DEFAULT;
542 if (UTIL_isRegularFile(dstFileName)) {
543 /* Check if destination file already exists */
544 FILE* const fCheck = fopen( dstFileName, "rb" );
545 if (!strcmp(dstFileName, nulmark)) {
546 EXM_THROW(40, "%s is unexpectedly categorized as a regular file",
549 if (fCheck != NULL) { /* dst file exists, authorization prompt */
551 if (!prefs->overwrite) {
552 if (g_display_prefs.displayLevel <= 1) {
553 /* No interaction possible */
554 DISPLAY("zstd: %s already exists; not overwritten \n",
558 DISPLAY("zstd: %s already exists; overwrite (y/N) ? ",
560 { int ch = getchar();
561 if ((ch!='Y') && (ch!='y')) {
562 DISPLAY(" not overwritten \n");
565 /* flush rest of input line */
566 while ((ch!=EOF) && (ch!='\n')) ch = getchar();
569 FIO_remove(dstFileName);
572 { FILE* const f = fopen( dstFileName, "wb" );
574 DISPLAYLEVEL(1, "zstd: %s: %s\n", dstFileName, strerror(errno));
576 chmod(dstFileName, 00600);
583 /*! FIO_createDictBuffer() :
584 * creates a buffer, pointed by `*bufferPtr`,
585 * loads `filename` content into it, up to DICTSIZE_MAX bytes.
586 * @return : loaded size
587 * if fileName==NULL, returns 0 and a NULL pointer
589 static size_t FIO_createDictBuffer(void** bufferPtr, const char* fileName)
594 assert(bufferPtr != NULL);
596 if (fileName == NULL) return 0;
598 DISPLAYLEVEL(4,"Loading %s as dictionary \n", fileName);
599 fileHandle = fopen(fileName, "rb");
600 if (fileHandle==NULL) EXM_THROW(31, "%s: %s", fileName, strerror(errno));
602 fileSize = UTIL_getFileSize(fileName);
603 if (fileSize > DICTSIZE_MAX) {
604 EXM_THROW(32, "Dictionary file %s is too large (> %u MB)",
605 fileName, DICTSIZE_MAX >> 20); /* avoid extreme cases */
607 *bufferPtr = malloc((size_t)fileSize);
608 if (*bufferPtr==NULL) EXM_THROW(34, "%s", strerror(errno));
609 { size_t const readSize = fread(*bufferPtr, 1, (size_t)fileSize, fileHandle);
610 if (readSize != fileSize)
611 EXM_THROW(35, "Error reading dictionary file %s : %s",
612 fileName, strerror(errno));
615 return (size_t)fileSize;
618 #ifndef ZSTD_NOCOMPRESS
620 /* **********************************************************************
622 ************************************************************************/
627 size_t srcBufferSize;
629 size_t dstBufferSize;
630 const char* dictFileName;
634 static cRess_t FIO_createCResources(FIO_prefs_t* const prefs,
635 const char* dictFileName, int cLevel,
637 ZSTD_compressionParameters comprParams) {
639 memset(&ress, 0, sizeof(ress));
641 DISPLAYLEVEL(6, "FIO_createCResources \n");
642 ress.cctx = ZSTD_createCCtx();
643 if (ress.cctx == NULL)
644 EXM_THROW(30, "allocation error (%s): can't create ZSTD_CCtx",
646 ress.srcBufferSize = ZSTD_CStreamInSize();
647 ress.srcBuffer = malloc(ress.srcBufferSize);
648 ress.dstBufferSize = ZSTD_CStreamOutSize();
649 ress.dstBuffer = malloc(ress.dstBufferSize);
650 if (!ress.srcBuffer || !ress.dstBuffer)
651 EXM_THROW(31, "allocation error : not enough memory");
653 /* Advanced parameters, including dictionary */
655 size_t const dictBuffSize = FIO_createDictBuffer(&dictBuffer, dictFileName); /* works with dictFileName==NULL */
656 if (dictFileName && (dictBuffer==NULL))
657 EXM_THROW(32, "allocation error : can't create dictBuffer");
658 ress.dictFileName = dictFileName;
660 if (prefs->adaptiveMode && !prefs->ldmFlag && !comprParams.windowLog)
661 comprParams.windowLog = ADAPT_WINDOWLOG_DEFAULT;
663 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_contentSizeFlag, 1) ); /* always enable content size when available (note: supposed to be default) */
664 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_dictIDFlag, prefs->dictIDFlag) );
665 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_checksumFlag, prefs->checksumFlag) );
666 /* compression level */
667 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_compressionLevel, cLevel) );
668 /* max compressed block size */
669 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_targetCBlockSize, (int)prefs->targetCBlockSize) );
670 /* long distance matching */
671 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_enableLongDistanceMatching, prefs->ldmFlag) );
672 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_ldmHashLog, prefs->ldmHashLog) );
673 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_ldmMinMatch, prefs->ldmMinMatch) );
674 if (prefs->ldmBucketSizeLog != FIO_LDM_PARAM_NOTSET) {
675 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_ldmBucketSizeLog, prefs->ldmBucketSizeLog) );
677 if (prefs->ldmHashRateLog != FIO_LDM_PARAM_NOTSET) {
678 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_ldmHashRateLog, prefs->ldmHashRateLog) );
680 /* compression parameters */
681 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_windowLog, (int)comprParams.windowLog) );
682 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_chainLog, (int)comprParams.chainLog) );
683 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_hashLog, (int)comprParams.hashLog) );
684 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_searchLog, (int)comprParams.searchLog) );
685 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_minMatch, (int)comprParams.minMatch) );
686 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_targetLength, (int)comprParams.targetLength) );
687 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_strategy, comprParams.strategy) );
688 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_literalCompressionMode, (int)prefs->literalCompressionMode) );
689 /* multi-threading */
690 #ifdef ZSTD_MULTITHREAD
691 DISPLAYLEVEL(5,"set nb workers = %u \n", prefs->nbWorkers);
692 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_nbWorkers, prefs->nbWorkers) );
693 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_jobSize, prefs->blockSize) );
694 if (prefs->overlapLog != FIO_OVERLAP_LOG_NOTSET) {
695 DISPLAYLEVEL(3,"set overlapLog = %u \n", prefs->overlapLog);
696 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_overlapLog, prefs->overlapLog) );
698 CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_rsyncable, prefs->rsyncable) );
701 CHECK( ZSTD_CCtx_setPledgedSrcSize(ress.cctx, srcSize) ); /* set the value temporarily for dictionary loading, to adapt compression parameters */
702 CHECK( ZSTD_CCtx_loadDictionary(ress.cctx, dictBuffer, dictBuffSize) );
703 CHECK( ZSTD_CCtx_setPledgedSrcSize(ress.cctx, ZSTD_CONTENTSIZE_UNKNOWN) ); /* reset */
711 static void FIO_freeCResources(cRess_t ress)
713 free(ress.srcBuffer);
714 free(ress.dstBuffer);
715 ZSTD_freeCStream(ress.cctx); /* never fails */
719 #ifdef ZSTD_GZCOMPRESS
720 static unsigned long long
721 FIO_compressGzFrame(cRess_t* ress,
722 const char* srcFileName, U64 const srcFileSize,
723 int compressionLevel, U64* readsize)
725 unsigned long long inFileSize = 0, outFileSize = 0;
729 if (compressionLevel > Z_BEST_COMPRESSION)
730 compressionLevel = Z_BEST_COMPRESSION;
732 strm.zalloc = Z_NULL;
734 strm.opaque = Z_NULL;
736 ret = deflateInit2(&strm, compressionLevel, Z_DEFLATED,
737 15 /* maxWindowLogSize */ + 16 /* gzip only */,
738 8, Z_DEFAULT_STRATEGY); /* see http://www.zlib.net/manual.html */
740 EXM_THROW(71, "zstd: %s: deflateInit2 error %d \n", srcFileName, ret);
744 strm.next_out = (Bytef*)ress->dstBuffer;
745 strm.avail_out = (uInt)ress->dstBufferSize;
748 if (strm.avail_in == 0) {
749 size_t const inSize = fread(ress->srcBuffer, 1, ress->srcBufferSize, ress->srcFile);
750 if (inSize == 0) break;
751 inFileSize += inSize;
752 strm.next_in = (z_const unsigned char*)ress->srcBuffer;
753 strm.avail_in = (uInt)inSize;
755 ret = deflate(&strm, Z_NO_FLUSH);
757 EXM_THROW(72, "zstd: %s: deflate error %d \n", srcFileName, ret);
758 { size_t const decompBytes = ress->dstBufferSize - strm.avail_out;
760 if (fwrite(ress->dstBuffer, 1, decompBytes, ress->dstFile) != decompBytes)
761 EXM_THROW(73, "Write error : cannot write to output file");
762 outFileSize += decompBytes;
763 strm.next_out = (Bytef*)ress->dstBuffer;
764 strm.avail_out = (uInt)ress->dstBufferSize;
767 if (srcFileSize == UTIL_FILESIZE_UNKNOWN)
768 DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%%",
769 (unsigned)(inFileSize>>20),
770 (double)outFileSize/inFileSize*100)
772 DISPLAYUPDATE(2, "\rRead : %u / %u MB ==> %.2f%%",
773 (unsigned)(inFileSize>>20), (unsigned)(srcFileSize>>20),
774 (double)outFileSize/inFileSize*100);
778 ret = deflate(&strm, Z_FINISH);
779 { size_t const decompBytes = ress->dstBufferSize - strm.avail_out;
781 if (fwrite(ress->dstBuffer, 1, decompBytes, ress->dstFile) != decompBytes)
782 EXM_THROW(75, "Write error : %s", strerror(errno));
783 outFileSize += decompBytes;
784 strm.next_out = (Bytef*)ress->dstBuffer;
785 strm.avail_out = (uInt)ress->dstBufferSize;
787 if (ret == Z_STREAM_END) break;
788 if (ret != Z_BUF_ERROR)
789 EXM_THROW(77, "zstd: %s: deflate error %d \n", srcFileName, ret);
792 ret = deflateEnd(&strm);
794 EXM_THROW(79, "zstd: %s: deflateEnd error %d \n", srcFileName, ret);
795 *readsize = inFileSize;
802 #ifdef ZSTD_LZMACOMPRESS
803 static unsigned long long
804 FIO_compressLzmaFrame(cRess_t* ress,
805 const char* srcFileName, U64 const srcFileSize,
806 int compressionLevel, U64* readsize, int plain_lzma)
808 unsigned long long inFileSize = 0, outFileSize = 0;
809 lzma_stream strm = LZMA_STREAM_INIT;
810 lzma_action action = LZMA_RUN;
813 if (compressionLevel < 0) compressionLevel = 0;
814 if (compressionLevel > 9) compressionLevel = 9;
817 lzma_options_lzma opt_lzma;
818 if (lzma_lzma_preset(&opt_lzma, compressionLevel))
819 EXM_THROW(71, "zstd: %s: lzma_lzma_preset error", srcFileName);
820 ret = lzma_alone_encoder(&strm, &opt_lzma); /* LZMA */
822 EXM_THROW(71, "zstd: %s: lzma_alone_encoder error %d", srcFileName, ret);
824 ret = lzma_easy_encoder(&strm, compressionLevel, LZMA_CHECK_CRC64); /* XZ */
826 EXM_THROW(71, "zstd: %s: lzma_easy_encoder error %d", srcFileName, ret);
831 strm.next_out = (BYTE*)ress->dstBuffer;
832 strm.avail_out = ress->dstBufferSize;
835 if (strm.avail_in == 0) {
836 size_t const inSize = fread(ress->srcBuffer, 1, ress->srcBufferSize, ress->srcFile);
837 if (inSize == 0) action = LZMA_FINISH;
838 inFileSize += inSize;
839 strm.next_in = (BYTE const*)ress->srcBuffer;
840 strm.avail_in = inSize;
843 ret = lzma_code(&strm, action);
845 if (ret != LZMA_OK && ret != LZMA_STREAM_END)
846 EXM_THROW(72, "zstd: %s: lzma_code encoding error %d", srcFileName, ret);
847 { size_t const compBytes = ress->dstBufferSize - strm.avail_out;
849 if (fwrite(ress->dstBuffer, 1, compBytes, ress->dstFile) != compBytes)
850 EXM_THROW(73, "Write error : %s", strerror(errno));
851 outFileSize += compBytes;
852 strm.next_out = (BYTE*)ress->dstBuffer;
853 strm.avail_out = ress->dstBufferSize;
855 if (srcFileSize == UTIL_FILESIZE_UNKNOWN)
856 DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%%",
857 (unsigned)(inFileSize>>20),
858 (double)outFileSize/inFileSize*100)
860 DISPLAYUPDATE(2, "\rRead : %u / %u MB ==> %.2f%%",
861 (unsigned)(inFileSize>>20), (unsigned)(srcFileSize>>20),
862 (double)outFileSize/inFileSize*100);
863 if (ret == LZMA_STREAM_END) break;
867 *readsize = inFileSize;
873 #ifdef ZSTD_LZ4COMPRESS
875 #if LZ4_VERSION_NUMBER <= 10600
876 #define LZ4F_blockLinked blockLinked
877 #define LZ4F_max64KB max64KB
880 static int FIO_LZ4_GetBlockSize_FromBlockId (int id) { return (1 << (8 + (2 * id))); }
882 static unsigned long long
883 FIO_compressLz4Frame(cRess_t* ress,
884 const char* srcFileName, U64 const srcFileSize,
885 int compressionLevel, int checksumFlag,
888 const size_t blockSize = FIO_LZ4_GetBlockSize_FromBlockId(LZ4F_max64KB);
889 unsigned long long inFileSize = 0, outFileSize = 0;
891 LZ4F_preferences_t prefs;
892 LZ4F_compressionContext_t ctx;
894 LZ4F_errorCode_t const errorCode = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION);
895 if (LZ4F_isError(errorCode))
896 EXM_THROW(31, "zstd: failed to create lz4 compression context");
898 memset(&prefs, 0, sizeof(prefs));
900 assert(blockSize <= ress->srcBufferSize);
903 prefs.compressionLevel = compressionLevel;
904 prefs.frameInfo.blockMode = LZ4F_blockLinked;
905 prefs.frameInfo.blockSizeID = LZ4F_max64KB;
906 prefs.frameInfo.contentChecksumFlag = (contentChecksum_t)checksumFlag;
907 #if LZ4_VERSION_NUMBER >= 10600
908 prefs.frameInfo.contentSize = (srcFileSize==UTIL_FILESIZE_UNKNOWN) ? 0 : srcFileSize;
910 assert(LZ4F_compressBound(blockSize, &prefs) <= ress->dstBufferSize);
914 size_t headerSize = LZ4F_compressBegin(ctx, ress->dstBuffer, ress->dstBufferSize, &prefs);
915 if (LZ4F_isError(headerSize))
916 EXM_THROW(33, "File header generation failed : %s",
917 LZ4F_getErrorName(headerSize));
918 if (fwrite(ress->dstBuffer, 1, headerSize, ress->dstFile) != headerSize)
919 EXM_THROW(34, "Write error : %s (cannot write header)", strerror(errno));
920 outFileSize += headerSize;
922 /* Read first block */
923 readSize = fread(ress->srcBuffer, (size_t)1, (size_t)blockSize, ress->srcFile);
924 inFileSize += readSize;
928 size_t const outSize = LZ4F_compressUpdate(ctx,
929 ress->dstBuffer, ress->dstBufferSize,
930 ress->srcBuffer, readSize, NULL);
931 if (LZ4F_isError(outSize))
932 EXM_THROW(35, "zstd: %s: lz4 compression failed : %s",
933 srcFileName, LZ4F_getErrorName(outSize));
934 outFileSize += outSize;
935 if (srcFileSize == UTIL_FILESIZE_UNKNOWN) {
936 DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%%",
937 (unsigned)(inFileSize>>20),
938 (double)outFileSize/inFileSize*100)
940 DISPLAYUPDATE(2, "\rRead : %u / %u MB ==> %.2f%%",
941 (unsigned)(inFileSize>>20), (unsigned)(srcFileSize>>20),
942 (double)outFileSize/inFileSize*100);
946 { size_t const sizeCheck = fwrite(ress->dstBuffer, 1, outSize, ress->dstFile);
947 if (sizeCheck != outSize)
948 EXM_THROW(36, "Write error : %s", strerror(errno));
951 /* Read next block */
952 readSize = fread(ress->srcBuffer, (size_t)1, (size_t)blockSize, ress->srcFile);
953 inFileSize += readSize;
955 if (ferror(ress->srcFile)) EXM_THROW(37, "Error reading %s ", srcFileName);
957 /* End of Stream mark */
958 headerSize = LZ4F_compressEnd(ctx, ress->dstBuffer, ress->dstBufferSize, NULL);
959 if (LZ4F_isError(headerSize))
960 EXM_THROW(38, "zstd: %s: lz4 end of file generation failed : %s",
961 srcFileName, LZ4F_getErrorName(headerSize));
963 { size_t const sizeCheck = fwrite(ress->dstBuffer, 1, headerSize, ress->dstFile);
964 if (sizeCheck != headerSize)
965 EXM_THROW(39, "Write error : %s (cannot write end of stream)",
968 outFileSize += headerSize;
971 *readsize = inFileSize;
972 LZ4F_freeCompressionContext(ctx);
979 static unsigned long long
980 FIO_compressZstdFrame(FIO_prefs_t* const prefs,
981 const cRess_t* ressPtr,
982 const char* srcFileName, U64 fileSize,
983 int compressionLevel, U64* readsize)
985 cRess_t const ress = *ressPtr;
986 FILE* const srcFile = ress.srcFile;
987 FILE* const dstFile = ress.dstFile;
988 U64 compressedfilesize = 0;
989 ZSTD_EndDirective directive = ZSTD_e_continue;
992 ZSTD_frameProgression previous_zfp_update = { 0, 0, 0, 0, 0, 0 };
993 ZSTD_frameProgression previous_zfp_correction = { 0, 0, 0, 0, 0, 0 };
994 typedef enum { noChange, slower, faster } speedChange_e;
995 speedChange_e speedChange = noChange;
996 unsigned flushWaiting = 0;
997 unsigned inputPresented = 0;
998 unsigned inputBlocked = 0;
999 unsigned lastJobID = 0;
1001 DISPLAYLEVEL(6, "compression using zstd format \n");
1004 if (fileSize != UTIL_FILESIZE_UNKNOWN) {
1005 CHECK(ZSTD_CCtx_setPledgedSrcSize(ress.cctx, fileSize));
1009 /* Main compression loop */
1011 size_t stillToFlush;
1012 /* Fill input Buffer */
1013 size_t const inSize = fread(ress.srcBuffer, (size_t)1, ress.srcBufferSize, srcFile);
1014 ZSTD_inBuffer inBuff = { ress.srcBuffer, inSize, 0 };
1015 DISPLAYLEVEL(6, "fread %u bytes from source \n", (unsigned)inSize);
1016 *readsize += inSize;
1018 if ((inSize == 0) || (*readsize == fileSize))
1019 directive = ZSTD_e_end;
1022 while ((inBuff.pos != inBuff.size) /* input buffer must be entirely ingested */
1023 || (directive == ZSTD_e_end && stillToFlush != 0) ) {
1025 size_t const oldIPos = inBuff.pos;
1026 ZSTD_outBuffer outBuff = { ress.dstBuffer, ress.dstBufferSize, 0 };
1027 size_t const toFlushNow = ZSTD_toFlushNow(ress.cctx);
1028 CHECK_V(stillToFlush, ZSTD_compressStream2(ress.cctx, &outBuff, &inBuff, directive));
1032 if (oldIPos == inBuff.pos) inputBlocked++; /* input buffer is full and can't take any more : input speed is faster than consumption rate */
1033 if (!toFlushNow) flushWaiting = 1;
1035 /* Write compressed stream */
1036 DISPLAYLEVEL(6, "ZSTD_compress_generic(end:%u) => input pos(%u)<=(%u)size ; output generated %u bytes \n",
1037 (unsigned)directive, (unsigned)inBuff.pos, (unsigned)inBuff.size, (unsigned)outBuff.pos);
1039 size_t const sizeCheck = fwrite(ress.dstBuffer, 1, outBuff.pos, dstFile);
1040 if (sizeCheck != outBuff.pos)
1041 EXM_THROW(25, "Write error : %s (cannot write compressed block)",
1043 compressedfilesize += outBuff.pos;
1046 /* display notification; and adapt compression level */
1047 if (READY_FOR_UPDATE()) {
1048 ZSTD_frameProgression const zfp = ZSTD_getFrameProgression(ress.cctx);
1049 double const cShare = (double)zfp.produced / (zfp.consumed + !zfp.consumed/*avoid div0*/) * 100;
1051 /* display progress notifications */
1052 if (g_display_prefs.displayLevel >= 3) {
1053 DISPLAYUPDATE(3, "\r(L%i) Buffered :%4u MB - Consumed :%4u MB - Compressed :%4u MB => %.2f%% ",
1055 (unsigned)((zfp.ingested - zfp.consumed) >> 20),
1056 (unsigned)(zfp.consumed >> 20),
1057 (unsigned)(zfp.produced >> 20),
1059 } else { /* summarized notifications if == 2; */
1060 DISPLAYLEVEL(2, "\rRead : %u ", (unsigned)(zfp.consumed >> 20));
1061 if (fileSize != UTIL_FILESIZE_UNKNOWN)
1062 DISPLAYLEVEL(2, "/ %u ", (unsigned)(fileSize >> 20));
1063 DISPLAYLEVEL(2, "MB ==> %2.f%% ", cShare);
1064 DELAY_NEXT_UPDATE();
1067 /* adaptive mode : statistics measurement and speed correction */
1068 if (prefs->adaptiveMode) {
1070 /* check output speed */
1071 if (zfp.currentJobID > 1) { /* only possible if nbWorkers >= 1 */
1073 unsigned long long newlyProduced = zfp.produced - previous_zfp_update.produced;
1074 unsigned long long newlyFlushed = zfp.flushed - previous_zfp_update.flushed;
1075 assert(zfp.produced >= previous_zfp_update.produced);
1076 assert(prefs->nbWorkers >= 1);
1078 /* test if compression is blocked
1079 * either because output is slow and all buffers are full
1080 * or because input is slow and no job can start while waiting for at least one buffer to be filled.
1081 * note : exclude starting part, since currentJobID > 1 */
1082 if ( (zfp.consumed == previous_zfp_update.consumed) /* no data compressed : no data available, or no more buffer to compress to, OR compression is really slow (compression of a single block is slower than update rate)*/
1083 && (zfp.nbActiveWorkers == 0) /* confirmed : no compression ongoing */
1085 DISPLAYLEVEL(6, "all buffers full : compression stopped => slow down \n")
1086 speedChange = slower;
1089 previous_zfp_update = zfp;
1091 if ( (newlyProduced > (newlyFlushed * 9 / 8)) /* compression produces more data than output can flush (though production can be spiky, due to work unit : (N==4)*block sizes) */
1092 && (flushWaiting == 0) /* flush speed was never slowed by lack of production, so it's operating at max capacity */
1094 DISPLAYLEVEL(6, "compression faster than flush (%llu > %llu), and flushed was never slowed down by lack of production => slow down \n", newlyProduced, newlyFlushed);
1095 speedChange = slower;
1100 /* course correct only if there is at least one new job completed */
1101 if (zfp.currentJobID > lastJobID) {
1102 DISPLAYLEVEL(6, "compression level adaptation check \n")
1104 /* check input speed */
1105 if (zfp.currentJobID > (unsigned)(prefs->nbWorkers+1)) { /* warm up period, to fill all workers */
1106 if (inputBlocked <= 0) {
1107 DISPLAYLEVEL(6, "input is never blocked => input is slower than ingestion \n");
1108 speedChange = slower;
1109 } else if (speedChange == noChange) {
1110 unsigned long long newlyIngested = zfp.ingested - previous_zfp_correction.ingested;
1111 unsigned long long newlyConsumed = zfp.consumed - previous_zfp_correction.consumed;
1112 unsigned long long newlyProduced = zfp.produced - previous_zfp_correction.produced;
1113 unsigned long long newlyFlushed = zfp.flushed - previous_zfp_correction.flushed;
1114 previous_zfp_correction = zfp;
1115 assert(inputPresented > 0);
1116 DISPLAYLEVEL(6, "input blocked %u/%u(%.2f) - ingested:%u vs %u:consumed - flushed:%u vs %u:produced \n",
1117 inputBlocked, inputPresented, (double)inputBlocked/inputPresented*100,
1118 (unsigned)newlyIngested, (unsigned)newlyConsumed,
1119 (unsigned)newlyFlushed, (unsigned)newlyProduced);
1120 if ( (inputBlocked > inputPresented / 8) /* input is waiting often, because input buffers is full : compression or output too slow */
1121 && (newlyFlushed * 33 / 32 > newlyProduced) /* flush everything that is produced */
1122 && (newlyIngested * 33 / 32 > newlyConsumed) /* input speed as fast or faster than compression speed */
1124 DISPLAYLEVEL(6, "recommend faster as in(%llu) >= (%llu)comp(%llu) <= out(%llu) \n",
1125 newlyIngested, newlyConsumed, newlyProduced, newlyFlushed);
1126 speedChange = faster;
1133 if (speedChange == slower) {
1134 DISPLAYLEVEL(6, "slower speed , higher compression \n")
1135 compressionLevel ++;
1136 if (compressionLevel > ZSTD_maxCLevel()) compressionLevel = ZSTD_maxCLevel();
1137 if (compressionLevel > prefs->maxAdaptLevel) compressionLevel = prefs->maxAdaptLevel;
1138 compressionLevel += (compressionLevel == 0); /* skip 0 */
1139 ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_compressionLevel, compressionLevel);
1141 if (speedChange == faster) {
1142 DISPLAYLEVEL(6, "faster speed , lighter compression \n")
1143 compressionLevel --;
1144 if (compressionLevel < prefs->minAdaptLevel) compressionLevel = prefs->minAdaptLevel;
1145 compressionLevel -= (compressionLevel == 0); /* skip 0 */
1146 ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_compressionLevel, compressionLevel);
1148 speedChange = noChange;
1150 lastJobID = zfp.currentJobID;
1151 } /* if (zfp.currentJobID > lastJobID) */
1152 } /* if (g_adaptiveMode) */
1153 } /* if (READY_FOR_UPDATE()) */
1154 } /* while ((inBuff.pos != inBuff.size) */
1155 } while (directive != ZSTD_e_end);
1157 if (ferror(srcFile)) {
1158 EXM_THROW(26, "Read error : I/O error");
1160 if (fileSize != UTIL_FILESIZE_UNKNOWN && *readsize != fileSize) {
1161 EXM_THROW(27, "Read error : Incomplete read : %llu / %llu B",
1162 (unsigned long long)*readsize, (unsigned long long)fileSize);
1165 return compressedfilesize;
1168 /*! FIO_compressFilename_internal() :
1169 * same as FIO_compressFilename_extRess(), with `ress.desFile` already opened.
1170 * @return : 0 : compression completed correctly,
1171 * 1 : missing or pb opening srcFileName
1174 FIO_compressFilename_internal(FIO_prefs_t* const prefs,
1176 const char* dstFileName, const char* srcFileName,
1177 int compressionLevel)
1179 UTIL_time_t const timeStart = UTIL_getTime();
1180 clock_t const cpuStart = clock();
1182 U64 compressedfilesize = 0;
1183 U64 const fileSize = UTIL_getFileSize(srcFileName);
1184 DISPLAYLEVEL(5, "%s: %u bytes \n", srcFileName, (unsigned)fileSize);
1186 /* compression format selection */
1187 switch (prefs->compressionType) {
1189 case FIO_zstdCompression:
1190 compressedfilesize = FIO_compressZstdFrame(prefs, &ress, srcFileName, fileSize, compressionLevel, &readsize);
1193 case FIO_gzipCompression:
1194 #ifdef ZSTD_GZCOMPRESS
1195 compressedfilesize = FIO_compressGzFrame(&ress, srcFileName, fileSize, compressionLevel, &readsize);
1197 (void)compressionLevel;
1198 EXM_THROW(20, "zstd: %s: file cannot be compressed as gzip (zstd compiled without ZSTD_GZCOMPRESS) -- ignored \n",
1203 case FIO_xzCompression:
1204 case FIO_lzmaCompression:
1205 #ifdef ZSTD_LZMACOMPRESS
1206 compressedfilesize = FIO_compressLzmaFrame(&ress, srcFileName, fileSize, compressionLevel, &readsize, prefs->compressionType==FIO_lzmaCompression);
1208 (void)compressionLevel;
1209 EXM_THROW(20, "zstd: %s: file cannot be compressed as xz/lzma (zstd compiled without ZSTD_LZMACOMPRESS) -- ignored \n",
1214 case FIO_lz4Compression:
1215 #ifdef ZSTD_LZ4COMPRESS
1216 compressedfilesize = FIO_compressLz4Frame(&ress, srcFileName, fileSize, compressionLevel, prefs->checksumFlag, &readsize);
1218 (void)compressionLevel;
1219 EXM_THROW(20, "zstd: %s: file cannot be compressed as lz4 (zstd compiled without ZSTD_LZ4COMPRESS) -- ignored \n",
1226 DISPLAYLEVEL(2, "\r%79s\r", "");
1227 DISPLAYLEVEL(2,"%-20s :%6.2f%% (%6llu => %6llu bytes, %s) \n",
1229 (double)compressedfilesize / (readsize+(!readsize)/*avoid div by zero*/) * 100,
1230 (unsigned long long)readsize, (unsigned long long) compressedfilesize,
1233 /* Elapsed Time and CPU Load */
1234 { clock_t const cpuEnd = clock();
1235 double const cpuLoad_s = (double)(cpuEnd - cpuStart) / CLOCKS_PER_SEC;
1236 U64 const timeLength_ns = UTIL_clockSpanNano(timeStart);
1237 double const timeLength_s = (double)timeLength_ns / 1000000000;
1238 double const cpuLoad_pct = (cpuLoad_s / timeLength_s) * 100;
1239 DISPLAYLEVEL(4, "%-20s : Completed in %.2f sec (cpu load : %.0f%%)\n",
1240 srcFileName, timeLength_s, cpuLoad_pct);
1246 /*! FIO_compressFilename_dstFile() :
1247 * open dstFileName, or pass-through if ress.dstFile != NULL,
1248 * then start compression with FIO_compressFilename_internal().
1249 * Manages source removal (--rm) and file permissions transfer.
1250 * note : ress.srcFile must be != NULL,
1251 * so reach this function through FIO_compressFilename_srcFile().
1252 * @return : 0 : compression completed correctly,
1255 static int FIO_compressFilename_dstFile(FIO_prefs_t* const prefs,
1257 const char* dstFileName,
1258 const char* srcFileName,
1259 int compressionLevel)
1261 int closeDstFile = 0;
1264 int transfer_permissions = 0;
1266 assert(ress.srcFile != NULL);
1268 if (ress.dstFile == NULL) {
1270 DISPLAYLEVEL(6, "FIO_compressFilename_dstFile: opening dst: %s", dstFileName);
1271 ress.dstFile = FIO_openDstFile(prefs, srcFileName, dstFileName);
1272 if (ress.dstFile==NULL) return 1; /* could not open dstFileName */
1273 /* Must only be added after FIO_openDstFile() succeeds.
1274 * Otherwise we may delete the destination file if it already exists,
1275 * and the user presses Ctrl-C when asked if they wish to overwrite.
1277 addHandler(dstFileName);
1279 if ( strcmp (srcFileName, stdinmark)
1280 && UTIL_getFileStat(srcFileName, &statbuf))
1281 transfer_permissions = 1;
1284 result = FIO_compressFilename_internal(prefs, ress, dstFileName, srcFileName, compressionLevel);
1287 FILE* const dstFile = ress.dstFile;
1288 ress.dstFile = NULL;
1292 if (fclose(dstFile)) { /* error closing dstFile */
1293 DISPLAYLEVEL(1, "zstd: %s: %s \n", dstFileName, strerror(errno));
1296 if ( (result != 0) /* operation failure */
1297 && strcmp(dstFileName, nulmark) /* special case : don't remove() /dev/null */
1298 && strcmp(dstFileName, stdoutmark) /* special case : don't remove() stdout */
1300 FIO_remove(dstFileName); /* remove compression artefact; note don't do anything special if remove() fails */
1301 } else if ( strcmp(dstFileName, stdoutmark)
1302 && strcmp(dstFileName, nulmark)
1303 && transfer_permissions) {
1304 UTIL_setFileStat(dstFileName, &statbuf);
1312 /*! FIO_compressFilename_srcFile() :
1313 * @return : 0 : compression completed correctly,
1314 * 1 : missing or pb opening srcFileName
1317 FIO_compressFilename_srcFile(FIO_prefs_t* const prefs,
1319 const char* dstFileName,
1320 const char* srcFileName,
1321 int compressionLevel)
1325 /* ensure src is not a directory */
1326 if (UTIL_isDirectory(srcFileName)) {
1327 DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
1331 /* ensure src is not the same as dict (if present) */
1332 if (ress.dictFileName != NULL && UTIL_isSameFile(srcFileName, ress.dictFileName)) {
1333 DISPLAYLEVEL(1, "zstd: cannot use %s as an input file and dictionary \n", srcFileName);
1337 ress.srcFile = FIO_openSrcFile(srcFileName);
1338 if (ress.srcFile == NULL) return 1; /* srcFile could not be opened */
1340 result = FIO_compressFilename_dstFile(prefs, ress, dstFileName, srcFileName, compressionLevel);
1342 fclose(ress.srcFile);
1343 ress.srcFile = NULL;
1344 if ( prefs->removeSrcFile /* --rm */
1345 && result == 0 /* success */
1346 && strcmp(srcFileName, stdinmark) /* exception : don't erase stdin */
1348 /* We must clear the handler, since after this point calling it would
1349 * delete both the source and destination files.
1352 if (FIO_remove(srcFileName))
1353 EXM_THROW(1, "zstd: %s: %s", srcFileName, strerror(errno));
1359 int FIO_compressFilename(FIO_prefs_t* const prefs,
1360 const char* dstFileName, const char* srcFileName,
1361 const char* dictFileName, int compressionLevel,
1362 ZSTD_compressionParameters comprParams)
1364 U64 const fileSize = UTIL_getFileSize(srcFileName);
1365 U64 const srcSize = (fileSize == UTIL_FILESIZE_UNKNOWN) ? ZSTD_CONTENTSIZE_UNKNOWN : fileSize;
1367 cRess_t const ress = FIO_createCResources(prefs, dictFileName, compressionLevel, srcSize, comprParams);
1368 int const result = FIO_compressFilename_srcFile(prefs, ress, dstFileName, srcFileName, compressionLevel);
1371 FIO_freeCResources(ress);
1376 /* FIO_determineCompressedName() :
1377 * create a destination filename for compressed srcFileName.
1378 * @return a pointer to it.
1379 * This function never returns an error (it may abort() in case of pb)
1382 FIO_determineCompressedName(const char* srcFileName, const char* suffix)
1384 static size_t dfnbCapacity = 0;
1385 static char* dstFileNameBuffer = NULL; /* using static allocation : this function cannot be multi-threaded */
1387 size_t const sfnSize = strlen(srcFileName);
1388 size_t const suffixSize = strlen(suffix);
1390 if (dfnbCapacity <= sfnSize+suffixSize+1) {
1391 /* resize buffer for dstName */
1392 free(dstFileNameBuffer);
1393 dfnbCapacity = sfnSize + suffixSize + 30;
1394 dstFileNameBuffer = (char*)malloc(dfnbCapacity);
1395 if (!dstFileNameBuffer) {
1396 EXM_THROW(30, "zstd: %s", strerror(errno));
1398 assert(dstFileNameBuffer != NULL);
1399 memcpy(dstFileNameBuffer, srcFileName, sfnSize);
1400 memcpy(dstFileNameBuffer+sfnSize, suffix, suffixSize+1 /* Include terminating null */);
1402 return dstFileNameBuffer;
1406 /* FIO_compressMultipleFilenames() :
1407 * compress nbFiles files
1408 * into one destination (outFileName)
1409 * or into one file each (outFileName == NULL, but suffix != NULL).
1411 int FIO_compressMultipleFilenames(FIO_prefs_t* const prefs,
1412 const char** inFileNamesTable, unsigned nbFiles,
1413 const char* outFileName, const char* suffix,
1414 const char* dictFileName, int compressionLevel,
1415 ZSTD_compressionParameters comprParams)
1418 U64 const firstFileSize = UTIL_getFileSize(inFileNamesTable[0]);
1419 U64 const firstSrcSize = (firstFileSize == UTIL_FILESIZE_UNKNOWN) ? ZSTD_CONTENTSIZE_UNKNOWN : firstFileSize;
1420 U64 const srcSize = (nbFiles != 1) ? ZSTD_CONTENTSIZE_UNKNOWN : firstSrcSize ;
1421 cRess_t ress = FIO_createCResources(prefs, dictFileName, compressionLevel, srcSize, comprParams);
1424 assert(outFileName != NULL || suffix != NULL);
1426 if (outFileName != NULL) { /* output into a single destination (stdout typically) */
1427 ress.dstFile = FIO_openDstFile(prefs, NULL, outFileName);
1428 if (ress.dstFile == NULL) { /* could not open outFileName */
1432 for (u=0; u<nbFiles; u++)
1433 error |= FIO_compressFilename_srcFile(prefs, ress, outFileName, inFileNamesTable[u], compressionLevel);
1434 if (fclose(ress.dstFile))
1435 EXM_THROW(29, "Write error (%s) : cannot properly close %s",
1436 strerror(errno), outFileName);
1437 ress.dstFile = NULL;
1441 for (u=0; u<nbFiles; u++) {
1442 const char* const srcFileName = inFileNamesTable[u];
1443 const char* const dstFileName = FIO_determineCompressedName(srcFileName, suffix); /* cannot fail */
1444 error |= FIO_compressFilename_srcFile(prefs, ress, dstFileName, srcFileName, compressionLevel);
1447 FIO_freeCResources(ress);
1451 #endif /* #ifndef ZSTD_NOCOMPRESS */
1455 #ifndef ZSTD_NODECOMPRESS
1457 /* **************************************************************************
1459 ***************************************************************************/
1462 size_t srcBufferSize;
1463 size_t srcBufferLoaded;
1465 size_t dstBufferSize;
1470 static dRess_t FIO_createDResources(FIO_prefs_t* const prefs, const char* dictFileName)
1473 memset(&ress, 0, sizeof(ress));
1476 ress.dctx = ZSTD_createDStream();
1477 if (ress.dctx==NULL)
1478 EXM_THROW(60, "Error: %s : can't create ZSTD_DStream", strerror(errno));
1479 CHECK( ZSTD_DCtx_setMaxWindowSize(ress.dctx, prefs->memLimit) );
1480 ress.srcBufferSize = ZSTD_DStreamInSize();
1481 ress.srcBuffer = malloc(ress.srcBufferSize);
1482 ress.dstBufferSize = ZSTD_DStreamOutSize();
1483 ress.dstBuffer = malloc(ress.dstBufferSize);
1484 if (!ress.srcBuffer || !ress.dstBuffer)
1485 EXM_THROW(61, "Allocation error : not enough memory");
1489 size_t const dictBufferSize = FIO_createDictBuffer(&dictBuffer, dictFileName);
1490 CHECK( ZSTD_initDStream_usingDict(ress.dctx, dictBuffer, dictBufferSize) );
1497 static void FIO_freeDResources(dRess_t ress)
1499 CHECK( ZSTD_freeDStream(ress.dctx) );
1500 free(ress.srcBuffer);
1501 free(ress.dstBuffer);
1505 /** FIO_fwriteSparse() :
1506 * @return : storedSkips, to be provided to next call to FIO_fwriteSparse() of LZ4IO_fwriteSparseEnd() */
1507 static unsigned FIO_fwriteSparse(FIO_prefs_t* const prefs, FILE* file, const void* buffer, size_t bufferSize, unsigned storedSkips)
1509 const size_t* const bufferT = (const size_t*)buffer; /* Buffer is supposed malloc'ed, hence aligned on size_t */
1510 size_t bufferSizeT = bufferSize / sizeof(size_t);
1511 const size_t* const bufferTEnd = bufferT + bufferSizeT;
1512 const size_t* ptrT = bufferT;
1513 static const size_t segmentSizeT = (32 KB) / sizeof(size_t); /* 0-test re-attempted every 32 KB */
1515 if (!prefs->sparseFileSupport) { /* normal write */
1516 size_t const sizeCheck = fwrite(buffer, 1, bufferSize, file);
1517 if (sizeCheck != bufferSize)
1518 EXM_THROW(70, "Write error : %s (cannot write decoded block)",
1523 /* avoid int overflow */
1524 if (storedSkips > 1 GB) {
1525 int const seekResult = LONG_SEEK(file, 1 GB, SEEK_CUR);
1526 if (seekResult != 0)
1527 EXM_THROW(71, "1 GB skip error (sparse file support)");
1528 storedSkips -= 1 GB;
1531 while (ptrT < bufferTEnd) {
1532 size_t seg0SizeT = segmentSizeT;
1535 /* count leading zeros */
1536 if (seg0SizeT > bufferSizeT) seg0SizeT = bufferSizeT;
1537 bufferSizeT -= seg0SizeT;
1538 for (nb0T=0; (nb0T < seg0SizeT) && (ptrT[nb0T] == 0); nb0T++) ;
1539 storedSkips += (unsigned)(nb0T * sizeof(size_t));
1541 if (nb0T != seg0SizeT) { /* not all 0s */
1542 int const seekResult = LONG_SEEK(file, storedSkips, SEEK_CUR);
1543 if (seekResult) EXM_THROW(72, "Sparse skip error ; try --no-sparse");
1547 { size_t const sizeCheck = fwrite(ptrT, sizeof(size_t), seg0SizeT, file);
1548 if (sizeCheck != seg0SizeT)
1549 EXM_THROW(73, "Write error : cannot write decoded block");
1554 { static size_t const maskT = sizeof(size_t)-1;
1555 if (bufferSize & maskT) {
1556 /* size not multiple of sizeof(size_t) : implies end of block */
1557 const char* const restStart = (const char*)bufferTEnd;
1558 const char* restPtr = restStart;
1559 size_t restSize = bufferSize & maskT;
1560 const char* const restEnd = restStart + restSize;
1561 for ( ; (restPtr < restEnd) && (*restPtr == 0); restPtr++) ;
1562 storedSkips += (unsigned) (restPtr - restStart);
1563 if (restPtr != restEnd) {
1564 int seekResult = LONG_SEEK(file, storedSkips, SEEK_CUR);
1566 EXM_THROW(74, "Sparse skip error ; try --no-sparse");
1568 { size_t const sizeCheck = fwrite(restPtr, 1, (size_t)(restEnd - restPtr), file);
1569 if (sizeCheck != (size_t)(restEnd - restPtr))
1570 EXM_THROW(75, "Write error : cannot write decoded end of block");
1577 FIO_fwriteSparseEnd(FIO_prefs_t* const prefs, FILE* file, unsigned storedSkips)
1579 if (storedSkips>0) {
1580 assert(prefs->sparseFileSupport > 0); /* storedSkips>0 implies sparse support is enabled */
1581 (void)prefs; /* assert can be disabled, in which case prefs becomes unused */
1582 if (LONG_SEEK(file, storedSkips-1, SEEK_CUR) != 0)
1583 EXM_THROW(69, "Final skip error (sparse file support)");
1584 /* last zero must be explicitly written,
1585 * so that skipped ones get implicitly translated as zero by FS */
1586 { const char lastZeroByte[1] = { 0 };
1587 if (fwrite(lastZeroByte, 1, 1, file) != 1)
1588 EXM_THROW(69, "Write error : cannot write last zero");
1593 /** FIO_passThrough() : just copy input into output, for compatibility with gzip -df mode
1594 @return : 0 (no error) */
1595 static int FIO_passThrough(FIO_prefs_t* const prefs,
1596 FILE* foutput, FILE* finput,
1597 void* buffer, size_t bufferSize,
1598 size_t alreadyLoaded)
1600 size_t const blockSize = MIN(64 KB, bufferSize);
1601 size_t readFromInput = 1;
1602 unsigned storedSkips = 0;
1604 /* assumption : ress->srcBufferLoaded bytes already loaded and stored within buffer */
1605 { size_t const sizeCheck = fwrite(buffer, 1, alreadyLoaded, foutput);
1606 if (sizeCheck != alreadyLoaded) {
1607 DISPLAYLEVEL(1, "Pass-through write error \n");
1611 while (readFromInput) {
1612 readFromInput = fread(buffer, 1, blockSize, finput);
1613 storedSkips = FIO_fwriteSparse(prefs, foutput, buffer, readFromInput, storedSkips);
1616 FIO_fwriteSparseEnd(prefs, foutput, storedSkips);
1620 /* FIO_highbit64() :
1621 * gives position of highest bit.
1622 * note : only works for v > 0 !
1624 static unsigned FIO_highbit64(unsigned long long v)
1629 while (v) { v >>= 1; count++; }
1633 /* FIO_zstdErrorHelp() :
1634 * detailed error message when requested window size is too large */
1635 static void FIO_zstdErrorHelp(FIO_prefs_t* const prefs, dRess_t* ress, size_t err, char const* srcFileName)
1637 ZSTD_frameHeader header;
1639 /* Help message only for one specific error */
1640 if (ZSTD_getErrorCode(err) != ZSTD_error_frameParameter_windowTooLarge)
1643 /* Try to decode the frame header */
1644 err = ZSTD_getFrameHeader(&header, ress->srcBuffer, ress->srcBufferLoaded);
1646 unsigned long long const windowSize = header.windowSize;
1647 unsigned const windowLog = FIO_highbit64(windowSize) + ((windowSize & (windowSize - 1)) != 0);
1648 assert(prefs->memLimit > 0);
1649 DISPLAYLEVEL(1, "%s : Window size larger than maximum : %llu > %u\n",
1650 srcFileName, windowSize, prefs->memLimit);
1651 if (windowLog <= ZSTD_WINDOWLOG_MAX) {
1652 unsigned const windowMB = (unsigned)((windowSize >> 20) + ((windowSize & ((1 MB) - 1)) != 0));
1653 assert(windowSize < (U64)(1ULL << 52)); /* ensure now overflow for windowMB */
1654 DISPLAYLEVEL(1, "%s : Use --long=%u or --memory=%uMB\n",
1655 srcFileName, windowLog, windowMB);
1659 DISPLAYLEVEL(1, "%s : Window log larger than ZSTD_WINDOWLOG_MAX=%u; not supported\n",
1660 srcFileName, ZSTD_WINDOWLOG_MAX);
1663 /** FIO_decompressFrame() :
1664 * @return : size of decoded zstd frame, or an error code
1666 #define FIO_ERROR_FRAME_DECODING ((unsigned long long)(-2))
1667 static unsigned long long FIO_decompressZstdFrame(
1668 FIO_prefs_t* const prefs,
1671 const char* srcFileName,
1675 U32 storedSkips = 0;
1677 size_t const srcFileLength = strlen(srcFileName);
1678 if (srcFileLength>20) srcFileName += srcFileLength-20; /* display last 20 characters only */
1680 ZSTD_resetDStream(ress->dctx);
1682 /* Header loading : ensures ZSTD_getFrameHeader() will succeed */
1683 { size_t const toDecode = ZSTD_FRAMEHEADERSIZE_MAX;
1684 if (ress->srcBufferLoaded < toDecode) {
1685 size_t const toRead = toDecode - ress->srcBufferLoaded;
1686 void* const startPosition = (char*)ress->srcBuffer + ress->srcBufferLoaded;
1687 ress->srcBufferLoaded += fread(startPosition, 1, toRead, finput);
1690 /* Main decompression Loop */
1692 ZSTD_inBuffer inBuff = { ress->srcBuffer, ress->srcBufferLoaded, 0 };
1693 ZSTD_outBuffer outBuff= { ress->dstBuffer, ress->dstBufferSize, 0 };
1694 size_t const readSizeHint = ZSTD_decompressStream(ress->dctx, &outBuff, &inBuff);
1695 if (ZSTD_isError(readSizeHint)) {
1696 DISPLAYLEVEL(1, "%s : Decoding error (36) : %s \n",
1697 srcFileName, ZSTD_getErrorName(readSizeHint));
1698 FIO_zstdErrorHelp(prefs, ress, readSizeHint, srcFileName);
1699 return FIO_ERROR_FRAME_DECODING;
1703 storedSkips = FIO_fwriteSparse(prefs, ress->dstFile, ress->dstBuffer, outBuff.pos, storedSkips);
1704 frameSize += outBuff.pos;
1705 DISPLAYUPDATE(2, "\r%-20.20s : %u MB... ",
1706 srcFileName, (unsigned)((alreadyDecoded+frameSize)>>20) );
1708 if (inBuff.pos > 0) {
1709 memmove(ress->srcBuffer, (char*)ress->srcBuffer + inBuff.pos, inBuff.size - inBuff.pos);
1710 ress->srcBufferLoaded -= inBuff.pos;
1713 if (readSizeHint == 0) break; /* end of frame */
1714 if (inBuff.size != inBuff.pos) {
1715 DISPLAYLEVEL(1, "%s : Decoding error (37) : should consume entire input \n",
1717 return FIO_ERROR_FRAME_DECODING;
1720 /* Fill input buffer */
1721 { size_t const toDecode = MIN(readSizeHint, ress->srcBufferSize); /* support large skippable frames */
1722 if (ress->srcBufferLoaded < toDecode) {
1723 size_t const toRead = toDecode - ress->srcBufferLoaded; /* > 0 */
1724 void* const startPosition = (char*)ress->srcBuffer + ress->srcBufferLoaded;
1725 size_t const readSize = fread(startPosition, 1, toRead, finput);
1727 DISPLAYLEVEL(1, "%s : Read error (39) : premature end \n",
1729 return FIO_ERROR_FRAME_DECODING;
1731 ress->srcBufferLoaded += readSize;
1734 FIO_fwriteSparseEnd(prefs, ress->dstFile, storedSkips);
1740 #ifdef ZSTD_GZDECOMPRESS
1741 static unsigned long long FIO_decompressGzFrame(dRess_t* ress,
1742 FILE* srcFile, const char* srcFileName)
1744 unsigned long long outFileSize = 0;
1746 int flush = Z_NO_FLUSH;
1747 int decodingError = 0;
1749 strm.zalloc = Z_NULL;
1750 strm.zfree = Z_NULL;
1751 strm.opaque = Z_NULL;
1754 /* see http://www.zlib.net/manual.html */
1755 if (inflateInit2(&strm, 15 /* maxWindowLogSize */ + 16 /* gzip only */) != Z_OK)
1756 return FIO_ERROR_FRAME_DECODING;
1758 strm.next_out = (Bytef*)ress->dstBuffer;
1759 strm.avail_out = (uInt)ress->dstBufferSize;
1760 strm.avail_in = (uInt)ress->srcBufferLoaded;
1761 strm.next_in = (z_const unsigned char*)ress->srcBuffer;
1765 if (strm.avail_in == 0) {
1766 ress->srcBufferLoaded = fread(ress->srcBuffer, 1, ress->srcBufferSize, srcFile);
1767 if (ress->srcBufferLoaded == 0) flush = Z_FINISH;
1768 strm.next_in = (z_const unsigned char*)ress->srcBuffer;
1769 strm.avail_in = (uInt)ress->srcBufferLoaded;
1771 ret = inflate(&strm, flush);
1772 if (ret == Z_BUF_ERROR) {
1773 DISPLAYLEVEL(1, "zstd: %s: premature gz end \n", srcFileName);
1774 decodingError = 1; break;
1776 if (ret != Z_OK && ret != Z_STREAM_END) {
1777 DISPLAYLEVEL(1, "zstd: %s: inflate error %d \n", srcFileName, ret);
1778 decodingError = 1; break;
1780 { size_t const decompBytes = ress->dstBufferSize - strm.avail_out;
1782 if (fwrite(ress->dstBuffer, 1, decompBytes, ress->dstFile) != decompBytes) {
1783 DISPLAYLEVEL(1, "zstd: %s \n", strerror(errno));
1784 decodingError = 1; break;
1786 outFileSize += decompBytes;
1787 strm.next_out = (Bytef*)ress->dstBuffer;
1788 strm.avail_out = (uInt)ress->dstBufferSize;
1791 if (ret == Z_STREAM_END) break;
1794 if (strm.avail_in > 0)
1795 memmove(ress->srcBuffer, strm.next_in, strm.avail_in);
1796 ress->srcBufferLoaded = strm.avail_in;
1797 if ( (inflateEnd(&strm) != Z_OK) /* release resources ; error detected */
1798 && (decodingError==0) ) {
1799 DISPLAYLEVEL(1, "zstd: %s: inflateEnd error \n", srcFileName);
1802 return decodingError ? FIO_ERROR_FRAME_DECODING : outFileSize;
1807 #ifdef ZSTD_LZMADECOMPRESS
1808 static unsigned long long FIO_decompressLzmaFrame(dRess_t* ress, FILE* srcFile, const char* srcFileName, int plain_lzma)
1810 unsigned long long outFileSize = 0;
1811 lzma_stream strm = LZMA_STREAM_INIT;
1812 lzma_action action = LZMA_RUN;
1814 int decodingError = 0;
1819 initRet = lzma_alone_decoder(&strm, UINT64_MAX); /* LZMA */
1821 initRet = lzma_stream_decoder(&strm, UINT64_MAX, 0); /* XZ */
1824 if (initRet != LZMA_OK) {
1825 DISPLAYLEVEL(1, "zstd: %s: %s error %d \n",
1826 plain_lzma ? "lzma_alone_decoder" : "lzma_stream_decoder",
1827 srcFileName, initRet);
1828 return FIO_ERROR_FRAME_DECODING;
1831 strm.next_out = (BYTE*)ress->dstBuffer;
1832 strm.avail_out = ress->dstBufferSize;
1833 strm.next_in = (BYTE const*)ress->srcBuffer;
1834 strm.avail_in = ress->srcBufferLoaded;
1838 if (strm.avail_in == 0) {
1839 ress->srcBufferLoaded = fread(ress->srcBuffer, 1, ress->srcBufferSize, srcFile);
1840 if (ress->srcBufferLoaded == 0) action = LZMA_FINISH;
1841 strm.next_in = (BYTE const*)ress->srcBuffer;
1842 strm.avail_in = ress->srcBufferLoaded;
1844 ret = lzma_code(&strm, action);
1846 if (ret == LZMA_BUF_ERROR) {
1847 DISPLAYLEVEL(1, "zstd: %s: premature lzma end \n", srcFileName);
1848 decodingError = 1; break;
1850 if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
1851 DISPLAYLEVEL(1, "zstd: %s: lzma_code decoding error %d \n",
1853 decodingError = 1; break;
1855 { size_t const decompBytes = ress->dstBufferSize - strm.avail_out;
1857 if (fwrite(ress->dstBuffer, 1, decompBytes, ress->dstFile) != decompBytes) {
1858 DISPLAYLEVEL(1, "zstd: %s \n", strerror(errno));
1859 decodingError = 1; break;
1861 outFileSize += decompBytes;
1862 strm.next_out = (BYTE*)ress->dstBuffer;
1863 strm.avail_out = ress->dstBufferSize;
1865 if (ret == LZMA_STREAM_END) break;
1868 if (strm.avail_in > 0)
1869 memmove(ress->srcBuffer, strm.next_in, strm.avail_in);
1870 ress->srcBufferLoaded = strm.avail_in;
1872 return decodingError ? FIO_ERROR_FRAME_DECODING : outFileSize;
1876 #ifdef ZSTD_LZ4DECOMPRESS
1877 static unsigned long long FIO_decompressLz4Frame(dRess_t* ress,
1878 FILE* srcFile, const char* srcFileName)
1880 unsigned long long filesize = 0;
1881 LZ4F_errorCode_t nextToLoad;
1882 LZ4F_decompressionContext_t dCtx;
1883 LZ4F_errorCode_t const errorCode = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION);
1884 int decodingError = 0;
1886 if (LZ4F_isError(errorCode)) {
1887 DISPLAYLEVEL(1, "zstd: failed to create lz4 decompression context \n");
1888 return FIO_ERROR_FRAME_DECODING;
1891 /* Init feed with magic number (already consumed from FILE* sFile) */
1892 { size_t inSize = 4;
1894 MEM_writeLE32(ress->srcBuffer, LZ4_MAGICNUMBER);
1895 nextToLoad = LZ4F_decompress(dCtx, ress->dstBuffer, &outSize, ress->srcBuffer, &inSize, NULL);
1896 if (LZ4F_isError(nextToLoad)) {
1897 DISPLAYLEVEL(1, "zstd: %s: lz4 header error : %s \n",
1898 srcFileName, LZ4F_getErrorName(nextToLoad));
1899 LZ4F_freeDecompressionContext(dCtx);
1900 return FIO_ERROR_FRAME_DECODING;
1904 for (;nextToLoad;) {
1907 size_t decodedBytes = ress->dstBufferSize;
1910 if (nextToLoad > ress->srcBufferSize) nextToLoad = ress->srcBufferSize;
1911 readSize = fread(ress->srcBuffer, 1, nextToLoad, srcFile);
1912 if (!readSize) break; /* reached end of file or stream */
1914 while ((pos < readSize) || (decodedBytes == ress->dstBufferSize)) { /* still to read, or still to flush */
1915 /* Decode Input (at least partially) */
1916 size_t remaining = readSize - pos;
1917 decodedBytes = ress->dstBufferSize;
1918 nextToLoad = LZ4F_decompress(dCtx, ress->dstBuffer, &decodedBytes, (char*)(ress->srcBuffer)+pos, &remaining, NULL);
1919 if (LZ4F_isError(nextToLoad)) {
1920 DISPLAYLEVEL(1, "zstd: %s: lz4 decompression error : %s \n",
1921 srcFileName, LZ4F_getErrorName(nextToLoad));
1922 decodingError = 1; nextToLoad = 0; break;
1928 if (fwrite(ress->dstBuffer, 1, decodedBytes, ress->dstFile) != decodedBytes) {
1929 DISPLAYLEVEL(1, "zstd: %s \n", strerror(errno));
1930 decodingError = 1; nextToLoad = 0; break;
1932 filesize += decodedBytes;
1933 DISPLAYUPDATE(2, "\rDecompressed : %u MB ", (unsigned)(filesize>>20));
1936 if (!nextToLoad) break;
1939 /* can be out because readSize == 0, which could be an fread() error */
1940 if (ferror(srcFile)) {
1941 DISPLAYLEVEL(1, "zstd: %s: read error \n", srcFileName);
1945 if (nextToLoad!=0) {
1946 DISPLAYLEVEL(1, "zstd: %s: unfinished lz4 stream \n", srcFileName);
1950 LZ4F_freeDecompressionContext(dCtx);
1951 ress->srcBufferLoaded = 0; /* LZ4F will reach exact frame boundary */
1953 return decodingError ? FIO_ERROR_FRAME_DECODING : filesize;
1959 /** FIO_decompressFrames() :
1960 * Find and decode frames inside srcFile
1961 * srcFile presumed opened and valid
1965 static int FIO_decompressFrames(FIO_prefs_t* const prefs, dRess_t ress, FILE* srcFile,
1966 const char* dstFileName, const char* srcFileName)
1968 unsigned readSomething = 0;
1969 unsigned long long filesize = 0;
1970 assert(srcFile != NULL);
1972 /* for each frame */
1974 /* check magic number -> version */
1975 size_t const toRead = 4;
1976 const BYTE* const buf = (const BYTE*)ress.srcBuffer;
1977 if (ress.srcBufferLoaded < toRead) /* load up to 4 bytes for header */
1978 ress.srcBufferLoaded += fread((char*)ress.srcBuffer + ress.srcBufferLoaded,
1979 (size_t)1, toRead - ress.srcBufferLoaded, srcFile);
1980 if (ress.srcBufferLoaded==0) {
1981 if (readSomething==0) { /* srcFile is empty (which is invalid) */
1982 DISPLAYLEVEL(1, "zstd: %s: unexpected end of file \n", srcFileName);
1984 } /* else, just reached frame boundary */
1985 break; /* no more input */
1987 readSomething = 1; /* there is at least 1 byte in srcFile */
1988 if (ress.srcBufferLoaded < toRead) {
1989 DISPLAYLEVEL(1, "zstd: %s: unknown header \n", srcFileName);
1992 if (ZSTD_isFrame(buf, ress.srcBufferLoaded)) {
1993 unsigned long long const frameSize = FIO_decompressZstdFrame(prefs, &ress, srcFile, srcFileName, filesize);
1994 if (frameSize == FIO_ERROR_FRAME_DECODING) return 1;
1995 filesize += frameSize;
1996 } else if (buf[0] == 31 && buf[1] == 139) { /* gz magic number */
1997 #ifdef ZSTD_GZDECOMPRESS
1998 unsigned long long const frameSize = FIO_decompressGzFrame(&ress, srcFile, srcFileName);
1999 if (frameSize == FIO_ERROR_FRAME_DECODING) return 1;
2000 filesize += frameSize;
2002 DISPLAYLEVEL(1, "zstd: %s: gzip file cannot be uncompressed (zstd compiled without HAVE_ZLIB) -- ignored \n", srcFileName);
2005 } else if ((buf[0] == 0xFD && buf[1] == 0x37) /* xz magic number */
2006 || (buf[0] == 0x5D && buf[1] == 0x00)) { /* lzma header (no magic number) */
2007 #ifdef ZSTD_LZMADECOMPRESS
2008 unsigned long long const frameSize = FIO_decompressLzmaFrame(&ress, srcFile, srcFileName, buf[0] != 0xFD);
2009 if (frameSize == FIO_ERROR_FRAME_DECODING) return 1;
2010 filesize += frameSize;
2012 DISPLAYLEVEL(1, "zstd: %s: xz/lzma file cannot be uncompressed (zstd compiled without HAVE_LZMA) -- ignored \n", srcFileName);
2015 } else if (MEM_readLE32(buf) == LZ4_MAGICNUMBER) {
2016 #ifdef ZSTD_LZ4DECOMPRESS
2017 unsigned long long const frameSize = FIO_decompressLz4Frame(&ress, srcFile, srcFileName);
2018 if (frameSize == FIO_ERROR_FRAME_DECODING) return 1;
2019 filesize += frameSize;
2021 DISPLAYLEVEL(1, "zstd: %s: lz4 file cannot be uncompressed (zstd compiled without HAVE_LZ4) -- ignored \n", srcFileName);
2024 } else if ((prefs->overwrite) && !strcmp (dstFileName, stdoutmark)) { /* pass-through mode */
2025 return FIO_passThrough(prefs,
2026 ress.dstFile, srcFile,
2027 ress.srcBuffer, ress.srcBufferSize,
2028 ress.srcBufferLoaded);
2030 DISPLAYLEVEL(1, "zstd: %s: unsupported format \n", srcFileName);
2032 } } /* for each frame */
2035 DISPLAYLEVEL(2, "\r%79s\r", "");
2036 DISPLAYLEVEL(2, "%-20s: %llu bytes \n", srcFileName, filesize);
2041 /** FIO_decompressDstFile() :
2043 or path-through if ress.dstFile is already != 0,
2044 then start decompression process (FIO_decompressFrames()).
2046 1 : operation aborted
2048 static int FIO_decompressDstFile(FIO_prefs_t* const prefs,
2049 dRess_t ress, FILE* srcFile,
2050 const char* dstFileName, const char* srcFileName)
2054 int transfer_permissions = 0;
2055 int releaseDstFile = 0;
2057 if (ress.dstFile == NULL) {
2060 ress.dstFile = FIO_openDstFile(prefs, srcFileName, dstFileName);
2061 if (ress.dstFile==0) return 1;
2063 /* Must only be added after FIO_openDstFile() succeeds.
2064 * Otherwise we may delete the destination file if it already exists,
2065 * and the user presses Ctrl-C when asked if they wish to overwrite.
2067 addHandler(dstFileName);
2069 if ( strcmp(srcFileName, stdinmark) /* special case : don't transfer permissions from stdin */
2070 && UTIL_getFileStat(srcFileName, &statbuf) )
2071 transfer_permissions = 1;
2075 result = FIO_decompressFrames(prefs, ress, srcFile, dstFileName, srcFileName);
2077 if (releaseDstFile) {
2078 FILE* const dstFile = ress.dstFile;
2080 ress.dstFile = NULL;
2081 if (fclose(dstFile)) {
2082 DISPLAYLEVEL(1, "zstd: %s: %s \n", dstFileName, strerror(errno));
2086 if ( (result != 0) /* operation failure */
2087 && strcmp(dstFileName, nulmark) /* special case : don't remove() /dev/null (#316) */
2088 && strcmp(dstFileName, stdoutmark) /* special case : don't remove() stdout */
2090 FIO_remove(dstFileName); /* remove decompression artefact; note: don't do anything special if remove() fails */
2091 } else { /* operation success */
2092 if ( strcmp(dstFileName, stdoutmark) /* special case : don't chmod stdout */
2093 && strcmp(dstFileName, nulmark) /* special case : don't chmod /dev/null */
2094 && transfer_permissions ) /* file permissions correctly extracted from src */
2095 UTIL_setFileStat(dstFileName, &statbuf); /* transfer file permissions from src into dst */
2103 /** FIO_decompressSrcFile() :
2104 Open `srcFileName`, transfer control to decompressDstFile()
2108 static int FIO_decompressSrcFile(FIO_prefs_t* const prefs, dRess_t ress, const char* dstFileName, const char* srcFileName)
2113 if (UTIL_isDirectory(srcFileName)) {
2114 DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
2118 srcFile = FIO_openSrcFile(srcFileName);
2119 if (srcFile==NULL) return 1;
2120 ress.srcBufferLoaded = 0;
2122 result = FIO_decompressDstFile(prefs, ress, srcFile, dstFileName, srcFileName);
2125 if (fclose(srcFile)) {
2126 DISPLAYLEVEL(1, "zstd: %s: %s \n", srcFileName, strerror(errno)); /* error should not happen */
2129 if ( prefs->removeSrcFile /* --rm */
2130 && (result==0) /* decompression successful */
2131 && strcmp(srcFileName, stdinmark) ) /* not stdin */ {
2132 /* We must clear the handler, since after this point calling it would
2133 * delete both the source and destination files.
2136 if (FIO_remove(srcFileName)) {
2137 /* failed to remove src file */
2138 DISPLAYLEVEL(1, "zstd: %s: %s \n", srcFileName, strerror(errno));
2146 int FIO_decompressFilename(FIO_prefs_t* const prefs,
2147 const char* dstFileName, const char* srcFileName,
2148 const char* dictFileName)
2150 dRess_t const ress = FIO_createDResources(prefs, dictFileName);
2152 int const decodingError = FIO_decompressSrcFile(prefs, ress, dstFileName, srcFileName);
2154 FIO_freeDResources(ress);
2155 return decodingError;
2159 /* FIO_determineDstName() :
2160 * create a destination filename from a srcFileName.
2161 * @return a pointer to it.
2162 * @return == NULL if there is an error */
2164 FIO_determineDstName(const char* srcFileName)
2166 static size_t dfnbCapacity = 0;
2167 static char* dstFileNameBuffer = NULL; /* using static allocation : this function cannot be multi-threaded */
2169 size_t const sfnSize = strlen(srcFileName);
2171 const char* const suffixPtr = strrchr(srcFileName, '.');
2172 if (suffixPtr == NULL) {
2173 DISPLAYLEVEL(1, "zstd: %s: unknown suffix -- ignored \n",
2177 suffixSize = strlen(suffixPtr);
2179 /* check suffix is authorized */
2180 if (sfnSize <= suffixSize
2181 || ( strcmp(suffixPtr, ZSTD_EXTENSION)
2182 #ifdef ZSTD_GZDECOMPRESS
2183 && strcmp(suffixPtr, GZ_EXTENSION)
2185 #ifdef ZSTD_LZMADECOMPRESS
2186 && strcmp(suffixPtr, XZ_EXTENSION)
2187 && strcmp(suffixPtr, LZMA_EXTENSION)
2189 #ifdef ZSTD_LZ4DECOMPRESS
2190 && strcmp(suffixPtr, LZ4_EXTENSION)
2193 const char* suffixlist = ZSTD_EXTENSION
2194 #ifdef ZSTD_GZDECOMPRESS
2197 #ifdef ZSTD_LZMADECOMPRESS
2198 "/" XZ_EXTENSION "/" LZMA_EXTENSION
2200 #ifdef ZSTD_LZ4DECOMPRESS
2204 DISPLAYLEVEL(1, "zstd: %s: unknown suffix (%s expected) -- ignored \n",
2205 srcFileName, suffixlist);
2209 /* allocate enough space to write dstFilename into it */
2210 if (dfnbCapacity+suffixSize <= sfnSize+1) {
2211 free(dstFileNameBuffer);
2212 dfnbCapacity = sfnSize + 20;
2213 dstFileNameBuffer = (char*)malloc(dfnbCapacity);
2214 if (dstFileNameBuffer==NULL)
2215 EXM_THROW(74, "%s : not enough memory for dstFileName", strerror(errno));
2218 /* return dst name == src name truncated from suffix */
2219 assert(dstFileNameBuffer != NULL);
2220 memcpy(dstFileNameBuffer, srcFileName, sfnSize - suffixSize);
2221 dstFileNameBuffer[sfnSize-suffixSize] = '\0';
2222 return dstFileNameBuffer;
2224 /* note : dstFileNameBuffer memory is not going to be free */
2229 FIO_decompressMultipleFilenames(FIO_prefs_t* const prefs,
2230 const char* srcNamesTable[], unsigned nbFiles,
2231 const char* outFileName,
2232 const char* dictFileName)
2235 dRess_t ress = FIO_createDResources(prefs, dictFileName);
2239 ress.dstFile = FIO_openDstFile(prefs, NULL, outFileName);
2240 if (ress.dstFile == 0) EXM_THROW(71, "cannot open %s", outFileName);
2241 for (u=0; u<nbFiles; u++)
2242 error |= FIO_decompressSrcFile(prefs, ress, outFileName, srcNamesTable[u]);
2243 if (fclose(ress.dstFile))
2244 EXM_THROW(72, "Write error : %s : cannot properly close output file",
2248 for (u=0; u<nbFiles; u++) { /* create dstFileName */
2249 const char* const srcFileName = srcNamesTable[u];
2250 const char* const dstFileName = FIO_determineDstName(srcFileName);
2251 if (dstFileName == NULL) { error=1; continue; }
2253 error |= FIO_decompressSrcFile(prefs, ress, dstFileName, srcFileName);
2257 FIO_freeDResources(ress);
2263 /* **************************************************************************
2264 * .zst file info (--list command)
2265 ***************************************************************************/
2268 U64 decompressedSize;
2271 int numActualFrames;
2272 int numSkippableFrames;
2273 int decompUnavailable;
2283 info_truncated_input=4,
2286 #define ERROR_IF(c,n,...) { \
2288 DISPLAYLEVEL(1, __VA_ARGS__); \
2289 DISPLAYLEVEL(1, " \n"); \
2295 FIO_analyzeFrames(fileInfo_t* info, FILE* const srcFile)
2297 /* begin analyzing frame */
2299 BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];
2300 size_t const numBytesRead = fread(headerBuffer, 1, sizeof(headerBuffer), srcFile);
2301 if (numBytesRead < ZSTD_FRAMEHEADERSIZE_MIN) {
2303 && (numBytesRead == 0)
2304 && (info->compressedSize > 0)
2305 && (info->compressedSize != UTIL_FILESIZE_UNKNOWN) ) {
2306 unsigned long long file_position = (unsigned long long) LONG_TELL(srcFile);
2307 unsigned long long file_size = (unsigned long long) info->compressedSize;
2308 ERROR_IF(file_position != file_size, info_truncated_input,
2309 "Error: seeked to position %llu, which is beyond file size of %llu\n",
2312 break; /* correct end of file => success */
2314 ERROR_IF(feof(srcFile), info_not_zstd, "Error: reached end of file with incomplete frame");
2315 ERROR_IF(1, info_frame_error, "Error: did not reach end of file but ran out of frames");
2317 { U32 const magicNumber = MEM_readLE32(headerBuffer);
2318 /* Zstandard frame */
2319 if (magicNumber == ZSTD_MAGICNUMBER) {
2320 ZSTD_frameHeader header;
2321 U64 const frameContentSize = ZSTD_getFrameContentSize(headerBuffer, numBytesRead);
2322 if ( frameContentSize == ZSTD_CONTENTSIZE_ERROR
2323 || frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN ) {
2324 info->decompUnavailable = 1;
2326 info->decompressedSize += frameContentSize;
2328 ERROR_IF(ZSTD_getFrameHeader(&header, headerBuffer, numBytesRead) != 0,
2329 info_frame_error, "Error: could not decode frame header");
2330 info->windowSize = header.windowSize;
2331 /* move to the end of the frame header */
2332 { size_t const headerSize = ZSTD_frameHeaderSize(headerBuffer, numBytesRead);
2333 ERROR_IF(ZSTD_isError(headerSize), info_frame_error, "Error: could not determine frame header size");
2334 ERROR_IF(fseek(srcFile, ((long)headerSize)-((long)numBytesRead), SEEK_CUR) != 0,
2335 info_frame_error, "Error: could not move to end of frame header");
2338 /* skip all blocks in the frame */
2339 { int lastBlock = 0;
2341 BYTE blockHeaderBuffer[3];
2342 ERROR_IF(fread(blockHeaderBuffer, 1, 3, srcFile) != 3,
2343 info_frame_error, "Error while reading block header");
2344 { U32 const blockHeader = MEM_readLE24(blockHeaderBuffer);
2345 U32 const blockTypeID = (blockHeader >> 1) & 3;
2346 U32 const isRLE = (blockTypeID == 1);
2347 U32 const isWrongBlock = (blockTypeID == 3);
2348 long const blockSize = isRLE ? 1 : (long)(blockHeader >> 3);
2349 ERROR_IF(isWrongBlock, info_frame_error, "Error: unsupported block type");
2350 lastBlock = blockHeader & 1;
2351 ERROR_IF(fseek(srcFile, blockSize, SEEK_CUR) != 0,
2352 info_frame_error, "Error: could not skip to end of block");
2354 } while (lastBlock != 1);
2357 /* check if checksum is used */
2358 { BYTE const frameHeaderDescriptor = headerBuffer[4];
2359 int const contentChecksumFlag = (frameHeaderDescriptor & (1 << 2)) >> 2;
2360 if (contentChecksumFlag) {
2361 info->usesCheck = 1;
2362 ERROR_IF(fseek(srcFile, 4, SEEK_CUR) != 0,
2363 info_frame_error, "Error: could not skip past checksum");
2365 info->numActualFrames++;
2367 /* Skippable frame */
2368 else if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
2369 U32 const frameSize = MEM_readLE32(headerBuffer + 4);
2370 long const seek = (long)(8 + frameSize - numBytesRead);
2371 ERROR_IF(LONG_SEEK(srcFile, seek, SEEK_CUR) != 0,
2372 info_frame_error, "Error: could not find end of skippable frame");
2373 info->numSkippableFrames++;
2375 /* unknown content */
2377 return info_not_zstd;
2379 } /* magic number analysis */
2380 } /* end analyzing frames */
2381 return info_success;
2386 getFileInfo_fileConfirmed(fileInfo_t* info, const char* inFileName)
2389 FILE* const srcFile = FIO_openSrcFile(inFileName);
2390 ERROR_IF(srcFile == NULL, info_file_error, "Error: could not open source file %s", inFileName);
2392 info->compressedSize = UTIL_getFileSize(inFileName);
2393 status = FIO_analyzeFrames(info, srcFile);
2402 * Reads information from file, stores in *info
2403 * @return : InfoError status
2406 getFileInfo(fileInfo_t* info, const char* srcFileName)
2408 ERROR_IF(!UTIL_isRegularFile(srcFileName),
2409 info_file_error, "Error : %s is not a file", srcFileName);
2410 return getFileInfo_fileConfirmed(info, srcFileName);
2415 displayInfo(const char* inFileName, const fileInfo_t* info, int displayLevel)
2417 unsigned const unit = info->compressedSize < (1 MB) ? (1 KB) : (1 MB);
2418 const char* const unitStr = info->compressedSize < (1 MB) ? "KB" : "MB";
2419 double const windowSizeUnit = (double)info->windowSize / unit;
2420 double const compressedSizeUnit = (double)info->compressedSize / unit;
2421 double const decompressedSizeUnit = (double)info->decompressedSize / unit;
2422 double const ratio = (info->compressedSize == 0) ? 0 : ((double)info->decompressedSize)/info->compressedSize;
2423 const char* const checkString = (info->usesCheck ? "XXH64" : "None");
2424 if (displayLevel <= 2) {
2425 if (!info->decompUnavailable) {
2426 DISPLAYOUT("%6d %5d %7.2f %2s %9.2f %2s %5.3f %5s %s\n",
2427 info->numSkippableFrames + info->numActualFrames,
2428 info->numSkippableFrames,
2429 compressedSizeUnit, unitStr, decompressedSizeUnit, unitStr,
2430 ratio, checkString, inFileName);
2432 DISPLAYOUT("%6d %5d %7.2f %2s %5s %s\n",
2433 info->numSkippableFrames + info->numActualFrames,
2434 info->numSkippableFrames,
2435 compressedSizeUnit, unitStr,
2436 checkString, inFileName);
2439 DISPLAYOUT("%s \n", inFileName);
2440 DISPLAYOUT("# Zstandard Frames: %d\n", info->numActualFrames);
2441 if (info->numSkippableFrames)
2442 DISPLAYOUT("# Skippable Frames: %d\n", info->numSkippableFrames);
2443 DISPLAYOUT("Window Size: %.2f %2s (%llu B)\n",
2444 windowSizeUnit, unitStr,
2445 (unsigned long long)info->windowSize);
2446 DISPLAYOUT("Compressed Size: %.2f %2s (%llu B)\n",
2447 compressedSizeUnit, unitStr,
2448 (unsigned long long)info->compressedSize);
2449 if (!info->decompUnavailable) {
2450 DISPLAYOUT("Decompressed Size: %.2f %2s (%llu B)\n",
2451 decompressedSizeUnit, unitStr,
2452 (unsigned long long)info->decompressedSize);
2453 DISPLAYOUT("Ratio: %.4f\n", ratio);
2455 DISPLAYOUT("Check: %s\n", checkString);
2460 static fileInfo_t FIO_addFInfo(fileInfo_t fi1, fileInfo_t fi2)
2463 memset(&total, 0, sizeof(total));
2464 total.numActualFrames = fi1.numActualFrames + fi2.numActualFrames;
2465 total.numSkippableFrames = fi1.numSkippableFrames + fi2.numSkippableFrames;
2466 total.compressedSize = fi1.compressedSize + fi2.compressedSize;
2467 total.decompressedSize = fi1.decompressedSize + fi2.decompressedSize;
2468 total.decompUnavailable = fi1.decompUnavailable | fi2.decompUnavailable;
2469 total.usesCheck = fi1.usesCheck & fi2.usesCheck;
2470 total.nbFiles = fi1.nbFiles + fi2.nbFiles;
2475 FIO_listFile(fileInfo_t* total, const char* inFileName, int displayLevel)
2478 memset(&info, 0, sizeof(info));
2479 { InfoError const error = getFileInfo(&info, inFileName);
2481 case info_frame_error:
2482 /* display error, but provide output */
2483 DISPLAYLEVEL(1, "Error while parsing \"%s\" \n", inFileName);
2486 DISPLAYOUT("File \"%s\" not compressed by zstd \n", inFileName);
2487 if (displayLevel > 2) DISPLAYOUT("\n");
2489 case info_file_error:
2490 /* error occurred while opening the file */
2491 if (displayLevel > 2) DISPLAYOUT("\n");
2493 case info_truncated_input:
2494 DISPLAYOUT("File \"%s\" is truncated \n", inFileName);
2495 if (displayLevel > 2) DISPLAYOUT("\n");
2502 displayInfo(inFileName, &info, displayLevel);
2503 *total = FIO_addFInfo(*total, info);
2504 assert(error == info_success || error == info_frame_error);
2509 int FIO_listMultipleFiles(unsigned numFiles, const char** filenameTable, int displayLevel)
2511 /* ensure no specified input is stdin (needs fseek() capability) */
2513 for (u=0; u<numFiles;u++) {
2514 ERROR_IF(!strcmp (filenameTable[u], stdinmark),
2515 1, "zstd: --list does not support reading from standard input");
2518 if (numFiles == 0) {
2519 if (!IS_CONSOLE(stdin)) {
2520 DISPLAYLEVEL(1, "zstd: --list does not support reading from standard input \n");
2522 DISPLAYLEVEL(1, "No files given \n");
2526 if (displayLevel <= 2) {
2527 DISPLAYOUT("Frames Skips Compressed Uncompressed Ratio Check Filename\n");
2531 memset(&total, 0, sizeof(total));
2532 total.usesCheck = 1;
2533 /* --list each file, and check for any error */
2535 for (u=0; u<numFiles;u++) {
2536 error |= FIO_listFile(&total, filenameTable[u], displayLevel);
2538 if (numFiles > 1 && displayLevel <= 2) { /* display total */
2539 unsigned const unit = total.compressedSize < (1 MB) ? (1 KB) : (1 MB);
2540 const char* const unitStr = total.compressedSize < (1 MB) ? "KB" : "MB";
2541 double const compressedSizeUnit = (double)total.compressedSize / unit;
2542 double const decompressedSizeUnit = (double)total.decompressedSize / unit;
2543 double const ratio = (total.compressedSize == 0) ? 0 : ((double)total.decompressedSize)/total.compressedSize;
2544 const char* const checkString = (total.usesCheck ? "XXH64" : "");
2545 DISPLAYOUT("----------------------------------------------------------------- \n");
2546 if (total.decompUnavailable) {
2547 DISPLAYOUT("%6d %5d %7.2f %2s %5s %u files\n",
2548 total.numSkippableFrames + total.numActualFrames,
2549 total.numSkippableFrames,
2550 compressedSizeUnit, unitStr,
2551 checkString, (unsigned)total.nbFiles);
2553 DISPLAYOUT("%6d %5d %7.2f %2s %9.2f %2s %5.3f %5s %u files\n",
2554 total.numSkippableFrames + total.numActualFrames,
2555 total.numSkippableFrames,
2556 compressedSizeUnit, unitStr, decompressedSizeUnit, unitStr,
2557 ratio, checkString, (unsigned)total.nbFiles);
2564 #endif /* #ifndef ZSTD_NODECOMPRESS */