xref: /freebsd/sys/contrib/zstd/programs/fileio.c (revision 206b73d0)
1 /*
2  * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
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.
9  */
10 
11 
12 /* *************************************
13 *  Compiler Options
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 */
18 #endif
19 #if defined(__MINGW32__) && !defined(_POSIX_SOURCE)
20 #  define _POSIX_SOURCE 1          /* disable %llu warnings with MinGW on Windows */
21 #endif
22 
23 /*-*************************************
24 *  Includes
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 */
31 #include <assert.h>
32 #include <errno.h>      /* errno */
33 #include <signal.h>
34 #include "timefn.h"     /* UTIL_getTime, UTIL_clockSpanMicro */
35 
36 #if defined (_MSC_VER)
37 #  include <sys/stat.h>
38 #  include <io.h>
39 #endif
40 
41 #include "mem.h"       /* U32, U64 */
42 #include "fileio.h"
43 
44 #define ZSTD_STATIC_LINKING_ONLY   /* ZSTD_magicNumber, ZSTD_frameHeaderSize_max */
45 #include "zstd.h"
46 #include "zstd_errors.h"           /* ZSTD_error_frameParameter_windowTooLarge */
47 
48 #if defined(ZSTD_GZCOMPRESS) || defined(ZSTD_GZDECOMPRESS)
49 #  include <zlib.h>
50 #  if !defined(z_const)
51 #    define z_const
52 #  endif
53 #endif
54 
55 #if defined(ZSTD_LZMACOMPRESS) || defined(ZSTD_LZMADECOMPRESS)
56 #  include <lzma.h>
57 #endif
58 
59 #define LZ4_MAGICNUMBER 0x184D2204
60 #if defined(ZSTD_LZ4COMPRESS) || defined(ZSTD_LZ4DECOMPRESS)
61 #  define LZ4F_ENABLE_OBSOLETE_ENUMS
62 #  include <lz4frame.h>
63 #  include <lz4.h>
64 #endif
65 
66 
67 /*-*************************************
68 *  Constants
69 ***************************************/
70 #define KB *(1<<10)
71 #define MB *(1<<20)
72 #define GB *(1U<<30)
73 
74 #define ADAPT_WINDOWLOG_DEFAULT 23   /* 8 MB */
75 #define DICTSIZE_MAX (32 MB)   /* protection against large input (attack scenario) */
76 
77 #define FNSPACE 30
78 
79 
80 /*-*************************************
81 *  Macros
82 ***************************************/
83 
84 struct FIO_display_prefs_s {
85     int displayLevel;   /* 0 : no display;  1: errors;  2: + result + interaction + warnings;  3: + progression;  4: + information */
86     U32 noProgress;
87 };
88 
89 static FIO_display_prefs_t g_display_prefs = {2, 0};
90 
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__); } }
94 
95 static const U64 g_refreshRate = SEC_TO_MICRO / 6;
96 static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
97 
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);       \
106     }   }   }
107 
108 #undef MIN  /* in case it would be already defined */
109 #define MIN(a,b)    ((a) < (b) ? (a) : (b))
110 
111 
112 #define EXM_THROW(error, ...)                                             \
113 {                                                                         \
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");                                               \
119     exit(error);                                                          \
120 }
121 
122 #define CHECK_V(v, f)                                \
123     v = f;                                           \
124     if (ZSTD_isError(v)) {                           \
125         DISPLAYLEVEL(5, "%s \n", #f);                \
126         EXM_THROW(11, "%s", ZSTD_getErrorName(v));   \
127     }
128 #define CHECK(f) { size_t err; CHECK_V(err, f); }
129 
130 
131 /*-************************************
132 *  Signal (Ctrl-C trapping)
133 **************************************/
134 static const char* g_artefact = NULL;
135 static void INThandler(int sig)
136 {
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 */
140 #endif
141     if (g_artefact) {
142         assert(UTIL_isRegularFile(g_artefact));
143         remove(g_artefact);
144     }
145     DISPLAY("\n");
146     exit(2);
147 }
148 static void addHandler(char const* dstFileName)
149 {
150     if (UTIL_isRegularFile(dstFileName)) {
151         g_artefact = dstFileName;
152         signal(SIGINT, INThandler);
153     } else {
154         g_artefact = NULL;
155     }
156 }
157 /* Idempotent */
158 static void clearHandler(void)
159 {
160     if (g_artefact) signal(SIGINT, SIG_DFL);
161     g_artefact = NULL;
162 }
163 
164 
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
174 #endif
175 
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
181 #  else
182 #    define BACKTRACE_ENABLE 0
183 #  endif
184 #endif
185 
186 /* note : after this point, BACKTRACE_ENABLE is necessarily defined */
187 
188 
189 #if BACKTRACE_ENABLE
190 
191 #include <execinfo.h>   /* backtrace, backtrace_symbols */
192 
193 #define MAX_STACK_FRAMES    50
194 
195 static void ABRThandler(int sig) {
196     const char* name;
197     void* addrlist[MAX_STACK_FRAMES];
198     char** symbollist;
199     int addrlen, i;
200 
201     switch (sig) {
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";
208     }
209 
210     DISPLAY("Caught %s signal, printing stack:\n", name);
211     /* Retrieve current stack addresses. */
212     addrlen = backtrace(addrlist, MAX_STACK_FRAMES);
213     if (addrlen == 0) {
214         DISPLAY("\n");
215         return;
216     }
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]);
222     }
223     free(symbollist);
224     /* Reset and raise the signal so default handler runs. */
225     signal(sig, SIG_DFL);
226     raise(sig);
227 }
228 #endif
229 
230 void FIO_addAbortHandler()
231 {
232 #if BACKTRACE_ENABLE
233     signal(SIGABRT, ABRThandler);
234     signal(SIGFPE, ABRThandler);
235     signal(SIGILL, ABRThandler);
236     signal(SIGSEGV, ABRThandler);
237     signal(SIGBUS, ABRThandler);
238 #endif
239 }
240 
241 
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) {
257         LARGE_INTEGER off;
258         DWORD method;
259         off.QuadPart = offset;
260         if (origin == SEEK_END)
261             method = FILE_END;
262         else if (origin == SEEK_CUR)
263             method = FILE_CURRENT;
264         else
265             method = FILE_BEGIN;
266 
267         if (SetFilePointerEx((HANDLE) _get_osfhandle(_fileno(file)), off, NULL, method))
268             return 0;
269         else
270             return -1;
271     }
272     static __int64 LONG_TELL(FILE* file) {
273         LARGE_INTEGER off, newOff;
274         off.QuadPart = 0;
275         newOff.QuadPart = 0;
276         SetFilePointerEx((HANDLE) _get_osfhandle(_fileno(file)), off, &newOff, FILE_CURRENT);
277         return newOff.QuadPart;
278     }
279 #else
280 #   define LONG_SEEK fseek
281 #   define LONG_TELL ftell
282 #endif
283 
284 
285 /*-*************************************
286 *  Parameters: Typedefs
287 ***************************************/
288 
289 struct FIO_prefs_s {
290 
291     /* Algorithm preferences */
292     FIO_compressionType_t compressionType;
293     U32 sparseFileSupport;   /* 0: no sparse allowed; 1: auto (file yes, stdout no); 2: force sparse */
294     int dictIDFlag;
295     int checksumFlag;
296     int blockSize;
297     int overlapLog;
298     U32 adaptiveMode;
299     int rsyncable;
300     int minAdaptLevel;
301     int maxAdaptLevel;
302     int ldmFlag;
303     int ldmHashLog;
304     int ldmMinMatch;
305     int ldmBucketSizeLog;
306     int ldmHashRateLog;
307     size_t targetCBlockSize;
308     ZSTD_literalCompressionMode_e literalCompressionMode;
309 
310     /* IO preferences */
311     U32 removeSrcFile;
312     U32 overwrite;
313 
314     /* Computation resources preferences */
315     unsigned memLimit;
316     int nbWorkers;
317 };
318 
319 
320 /*-*************************************
321 *  Parameters: Initialization
322 ***************************************/
323 
324 #define FIO_OVERLAP_LOG_NOTSET 9999
325 #define FIO_LDM_PARAM_NOTSET 9999
326 
327 
328 FIO_prefs_t* FIO_createPreferences(void)
329 {
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");
332 
333     ret->compressionType = FIO_zstdCompression;
334     ret->overwrite = 0;
335     ret->sparseFileSupport = ZSTD_SPARSE_DEFAULT;
336     ret->dictIDFlag = 1;
337     ret->checksumFlag = 1;
338     ret->removeSrcFile = 0;
339     ret->memLimit = 0;
340     ret->nbWorkers = 1;
341     ret->blockSize = 0;
342     ret->overlapLog = FIO_OVERLAP_LOG_NOTSET;
343     ret->adaptiveMode = 0;
344     ret->rsyncable = 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 */
347     ret->ldmFlag = 0;
348     ret->ldmHashLog = 0;
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;
354     return ret;
355 }
356 
357 void FIO_freePreferences(FIO_prefs_t* const prefs)
358 {
359     free(prefs);
360 }
361 
362 
363 /*-*************************************
364 *  Parameters: Display Options
365 ***************************************/
366 
367 void FIO_setNotificationLevel(int level) { g_display_prefs.displayLevel=level; }
368 
369 void FIO_setNoProgress(unsigned noProgress) { g_display_prefs.noProgress = noProgress; }
370 
371 
372 /*-*************************************
373 *  Parameters: Setters
374 ***************************************/
375 
376 void FIO_setCompressionType(FIO_prefs_t* const prefs, FIO_compressionType_t compressionType) { prefs->compressionType = compressionType; }
377 
378 void FIO_overwriteMode(FIO_prefs_t* const prefs) { prefs->overwrite = 1; }
379 
380 void FIO_setSparseWrite(FIO_prefs_t* const prefs, unsigned sparse) { prefs->sparseFileSupport = sparse; }
381 
382 void FIO_setDictIDFlag(FIO_prefs_t* const prefs, int dictIDFlag) { prefs->dictIDFlag = dictIDFlag; }
383 
384 void FIO_setChecksumFlag(FIO_prefs_t* const prefs, int checksumFlag) { prefs->checksumFlag = checksumFlag; }
385 
386 void FIO_setRemoveSrcFile(FIO_prefs_t* const prefs, unsigned flag) { prefs->removeSrcFile = (flag>0); }
387 
388 void FIO_setMemLimit(FIO_prefs_t* const prefs, unsigned memLimit) { prefs->memLimit = memLimit; }
389 
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");
393 #endif
394     prefs->nbWorkers = nbWorkers;
395 }
396 
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;
401 }
402 
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;
407 }
408 
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;
413 }
414 
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;
419 }
420 
421 void FIO_setTargetCBlockSize(FIO_prefs_t* const prefs, size_t targetCBlockSize) {
422     prefs->targetCBlockSize = targetCBlockSize;
423 }
424 
425 void FIO_setLiteralCompressionMode(
426         FIO_prefs_t* const prefs,
427         ZSTD_literalCompressionMode_e mode) {
428     prefs->literalCompressionMode = mode;
429 }
430 
431 void FIO_setAdaptMin(FIO_prefs_t* const prefs, int minCLevel)
432 {
433 #ifndef ZSTD_NOCOMPRESS
434     assert(minCLevel >= ZSTD_minCLevel());
435 #endif
436     prefs->minAdaptLevel = minCLevel;
437 }
438 
439 void FIO_setAdaptMax(FIO_prefs_t* const prefs, int maxCLevel)
440 {
441     prefs->maxAdaptLevel = maxCLevel;
442 }
443 
444 void FIO_setLdmFlag(FIO_prefs_t* const prefs, unsigned ldmFlag) {
445     prefs->ldmFlag = (ldmFlag>0);
446 }
447 
448 void FIO_setLdmHashLog(FIO_prefs_t* const prefs, int ldmHashLog) {
449     prefs->ldmHashLog = ldmHashLog;
450 }
451 
452 void FIO_setLdmMinMatch(FIO_prefs_t* const prefs, int ldmMinMatch) {
453     prefs->ldmMinMatch = ldmMinMatch;
454 }
455 
456 void FIO_setLdmBucketSizeLog(FIO_prefs_t* const prefs, int ldmBucketSizeLog) {
457     prefs->ldmBucketSizeLog = ldmBucketSizeLog;
458 }
459 
460 
461 void FIO_setLdmHashRateLog(FIO_prefs_t* const prefs, int ldmHashRateLog) {
462     prefs->ldmHashRateLog = ldmHashRateLog;
463 }
464 
465 
466 /*-*************************************
467 *  Functions
468 ***************************************/
469 /** FIO_remove() :
470  * @result : Unlink `fileName`, even if it's read-only */
471 static int FIO_remove(const char* path)
472 {
473     if (!UTIL_isRegularFile(path)) {
474         DISPLAYLEVEL(2, "zstd: Refusing to remove non-regular file %s \n", path);
475         return 0;
476     }
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);
481 #endif
482     return remove(path);
483 }
484 
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)
489 {
490     assert(srcFileName != NULL);
491     if (!strcmp (srcFileName, stdinmark)) {
492         DISPLAYLEVEL(4,"Using stdin for input \n");
493         SET_BINARY_MODE(stdin);
494         return stdin;
495     }
496 
497     if (!UTIL_fileExist(srcFileName)) {
498         DISPLAYLEVEL(1, "zstd: can't stat %s : %s -- ignored \n",
499                         srcFileName, strerror(errno));
500         return NULL;
501     }
502 
503     if (!UTIL_isRegularFile(srcFileName)) {
504         DISPLAYLEVEL(1, "zstd: %s is not a regular file -- ignored \n",
505                         srcFileName);
506         return NULL;
507     }
508 
509     {   FILE* const f = fopen(srcFileName, "rb");
510         if (f == NULL)
511             DISPLAYLEVEL(1, "zstd: %s: %s \n", srcFileName, strerror(errno));
512         return f;
513     }
514 }
515 
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)
520 {
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");
528         }
529         return stdout;
530     }
531 
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");
535         return NULL;
536     }
537 
538     if (prefs->sparseFileSupport == 1) {
539         prefs->sparseFileSupport = ZSTD_SPARSE_DEFAULT;
540     }
541 
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",
547                         dstFileName);
548         }
549         if (fCheck != NULL) {  /* dst file exists, authorization prompt */
550             fclose(fCheck);
551             if (!prefs->overwrite) {
552                 if (g_display_prefs.displayLevel <= 1) {
553                     /* No interaction possible */
554                     DISPLAY("zstd: %s already exists; not overwritten  \n",
555                             dstFileName);
556                     return NULL;
557                 }
558                 DISPLAY("zstd: %s already exists; overwrite (y/N) ? ",
559                         dstFileName);
560                 {   int ch = getchar();
561                     if ((ch!='Y') && (ch!='y')) {
562                         DISPLAY("    not overwritten  \n");
563                         return NULL;
564                     }
565                     /* flush rest of input line */
566                     while ((ch!=EOF) && (ch!='\n')) ch = getchar();
567             }   }
568             /* need to unlink */
569             FIO_remove(dstFileName);
570     }   }
571 
572     {   FILE* const f = fopen( dstFileName, "wb" );
573         if (f == NULL) {
574             DISPLAYLEVEL(1, "zstd: %s: %s\n", dstFileName, strerror(errno));
575         } else {
576             chmod(dstFileName, 00600);
577         }
578         return f;
579     }
580 }
581 
582 
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
588  */
589 static size_t FIO_createDictBuffer(void** bufferPtr, const char* fileName)
590 {
591     FILE* fileHandle;
592     U64 fileSize;
593 
594     assert(bufferPtr != NULL);
595     *bufferPtr = NULL;
596     if (fileName == NULL) return 0;
597 
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));
601 
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 */
606     }
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));
613     }
614     fclose(fileHandle);
615     return (size_t)fileSize;
616 }
617 
618 #ifndef ZSTD_NOCOMPRESS
619 
620 /* **********************************************************************
621  *  Compression
622  ************************************************************************/
623 typedef struct {
624     FILE* srcFile;
625     FILE* dstFile;
626     void*  srcBuffer;
627     size_t srcBufferSize;
628     void*  dstBuffer;
629     size_t dstBufferSize;
630     const char* dictFileName;
631     ZSTD_CStream* cctx;
632 } cRess_t;
633 
634 static cRess_t FIO_createCResources(FIO_prefs_t* const prefs,
635                                     const char* dictFileName, int cLevel,
636                                     U64 srcSize,
637                                     ZSTD_compressionParameters comprParams) {
638     cRess_t ress;
639     memset(&ress, 0, sizeof(ress));
640 
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",
645                     strerror(errno));
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");
652 
653     /* Advanced parameters, including dictionary */
654     {   void* dictBuffer;
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;
659 
660         if (prefs->adaptiveMode && !prefs->ldmFlag && !comprParams.windowLog)
661             comprParams.windowLog = ADAPT_WINDOWLOG_DEFAULT;
662 
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) );
676         }
677         if (prefs->ldmHashRateLog != FIO_LDM_PARAM_NOTSET) {
678             CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_ldmHashRateLog, prefs->ldmHashRateLog) );
679         }
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) );
697         }
698         CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_rsyncable, prefs->rsyncable) );
699 #endif
700         /* dictionary */
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 */
704 
705         free(dictBuffer);
706     }
707 
708     return ress;
709 }
710 
711 static void FIO_freeCResources(cRess_t ress)
712 {
713     free(ress.srcBuffer);
714     free(ress.dstBuffer);
715     ZSTD_freeCStream(ress.cctx);   /* never fails */
716 }
717 
718 
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)
724 {
725     unsigned long long inFileSize = 0, outFileSize = 0;
726     z_stream strm;
727     int ret;
728 
729     if (compressionLevel > Z_BEST_COMPRESSION)
730         compressionLevel = Z_BEST_COMPRESSION;
731 
732     strm.zalloc = Z_NULL;
733     strm.zfree = Z_NULL;
734     strm.opaque = Z_NULL;
735 
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 */
739     if (ret != Z_OK)
740         EXM_THROW(71, "zstd: %s: deflateInit2 error %d \n", srcFileName, ret);
741 
742     strm.next_in = 0;
743     strm.avail_in = 0;
744     strm.next_out = (Bytef*)ress->dstBuffer;
745     strm.avail_out = (uInt)ress->dstBufferSize;
746 
747     while (1) {
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;
754         }
755         ret = deflate(&strm, Z_NO_FLUSH);
756         if (ret != Z_OK)
757             EXM_THROW(72, "zstd: %s: deflate error %d \n", srcFileName, ret);
758         {   size_t const decompBytes = ress->dstBufferSize - strm.avail_out;
759             if (decompBytes) {
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;
765             }
766         }
767         if (srcFileSize == UTIL_FILESIZE_UNKNOWN)
768             DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%%",
769                             (unsigned)(inFileSize>>20),
770                             (double)outFileSize/inFileSize*100)
771         else
772             DISPLAYUPDATE(2, "\rRead : %u / %u MB ==> %.2f%%",
773                             (unsigned)(inFileSize>>20), (unsigned)(srcFileSize>>20),
774                             (double)outFileSize/inFileSize*100);
775     }
776 
777     while (1) {
778         ret = deflate(&strm, Z_FINISH);
779         {   size_t const decompBytes = ress->dstBufferSize - strm.avail_out;
780             if (decompBytes) {
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;
786         }   }
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);
790     }
791 
792     ret = deflateEnd(&strm);
793     if (ret != Z_OK)
794         EXM_THROW(79, "zstd: %s: deflateEnd error %d \n", srcFileName, ret);
795     *readsize = inFileSize;
796 
797     return outFileSize;
798 }
799 #endif
800 
801 
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)
807 {
808     unsigned long long inFileSize = 0, outFileSize = 0;
809     lzma_stream strm = LZMA_STREAM_INIT;
810     lzma_action action = LZMA_RUN;
811     lzma_ret ret;
812 
813     if (compressionLevel < 0) compressionLevel = 0;
814     if (compressionLevel > 9) compressionLevel = 9;
815 
816     if (plain_lzma) {
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 */
821         if (ret != LZMA_OK)
822             EXM_THROW(71, "zstd: %s: lzma_alone_encoder error %d", srcFileName, ret);
823     } else {
824         ret = lzma_easy_encoder(&strm, compressionLevel, LZMA_CHECK_CRC64); /* XZ */
825         if (ret != LZMA_OK)
826             EXM_THROW(71, "zstd: %s: lzma_easy_encoder error %d", srcFileName, ret);
827     }
828 
829     strm.next_in = 0;
830     strm.avail_in = 0;
831     strm.next_out = (BYTE*)ress->dstBuffer;
832     strm.avail_out = ress->dstBufferSize;
833 
834     while (1) {
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;
841         }
842 
843         ret = lzma_code(&strm, action);
844 
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;
848             if (compBytes) {
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;
854         }   }
855         if (srcFileSize == UTIL_FILESIZE_UNKNOWN)
856             DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%%",
857                             (unsigned)(inFileSize>>20),
858                             (double)outFileSize/inFileSize*100)
859         else
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;
864     }
865 
866     lzma_end(&strm);
867     *readsize = inFileSize;
868 
869     return outFileSize;
870 }
871 #endif
872 
873 #ifdef ZSTD_LZ4COMPRESS
874 
875 #if LZ4_VERSION_NUMBER <= 10600
876 #define LZ4F_blockLinked blockLinked
877 #define LZ4F_max64KB max64KB
878 #endif
879 
880 static int FIO_LZ4_GetBlockSize_FromBlockId (int id) { return (1 << (8 + (2 * id))); }
881 
882 static unsigned long long
883 FIO_compressLz4Frame(cRess_t* ress,
884                      const char* srcFileName, U64 const srcFileSize,
885                      int compressionLevel, int checksumFlag,
886                      U64* readsize)
887 {
888     const size_t blockSize = FIO_LZ4_GetBlockSize_FromBlockId(LZ4F_max64KB);
889     unsigned long long inFileSize = 0, outFileSize = 0;
890 
891     LZ4F_preferences_t prefs;
892     LZ4F_compressionContext_t ctx;
893 
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");
897 
898     memset(&prefs, 0, sizeof(prefs));
899 
900     assert(blockSize <= ress->srcBufferSize);
901 
902     prefs.autoFlush = 1;
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;
909 #endif
910     assert(LZ4F_compressBound(blockSize, &prefs) <= ress->dstBufferSize);
911 
912     {
913         size_t readSize;
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;
921 
922         /* Read first block */
923         readSize  = fread(ress->srcBuffer, (size_t)1, (size_t)blockSize, ress->srcFile);
924         inFileSize += readSize;
925 
926         /* Main Loop */
927         while (readSize>0) {
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)
939             } else {
940                 DISPLAYUPDATE(2, "\rRead : %u / %u MB ==> %.2f%%",
941                                 (unsigned)(inFileSize>>20), (unsigned)(srcFileSize>>20),
942                                 (double)outFileSize/inFileSize*100);
943             }
944 
945             /* Write Block */
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));
949             }
950 
951             /* Read next block */
952             readSize  = fread(ress->srcBuffer, (size_t)1, (size_t)blockSize, ress->srcFile);
953             inFileSize += readSize;
954         }
955         if (ferror(ress->srcFile)) EXM_THROW(37, "Error reading %s ", srcFileName);
956 
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));
962 
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)",
966                             strerror(errno));
967         }
968         outFileSize += headerSize;
969     }
970 
971     *readsize = inFileSize;
972     LZ4F_freeCompressionContext(ctx);
973 
974     return outFileSize;
975 }
976 #endif
977 
978 
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)
984 {
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;
990 
991     /* stats */
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;
1000 
1001     DISPLAYLEVEL(6, "compression using zstd format \n");
1002 
1003     /* init */
1004     if (fileSize != UTIL_FILESIZE_UNKNOWN) {
1005         CHECK(ZSTD_CCtx_setPledgedSrcSize(ress.cctx, fileSize));
1006     }
1007     (void)srcFileName;
1008 
1009     /* Main compression loop */
1010     do {
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;
1017 
1018         if ((inSize == 0) || (*readsize == fileSize))
1019             directive = ZSTD_e_end;
1020 
1021         stillToFlush = 1;
1022         while ((inBuff.pos != inBuff.size)   /* input buffer must be entirely ingested */
1023             || (directive == ZSTD_e_end && stillToFlush != 0) ) {
1024 
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));
1029 
1030             /* count stats */
1031             inputPresented++;
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;
1034 
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);
1038             if (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)",
1042                                     strerror(errno));
1043                 compressedfilesize += outBuff.pos;
1044             }
1045 
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;
1050 
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%% ",
1054                                 compressionLevel,
1055                                 (unsigned)((zfp.ingested - zfp.consumed) >> 20),
1056                                 (unsigned)(zfp.consumed >> 20),
1057                                 (unsigned)(zfp.produced >> 20),
1058                                 cShare );
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();
1065                 }
1066 
1067                 /* adaptive mode : statistics measurement and speed correction */
1068                 if (prefs->adaptiveMode) {
1069 
1070                     /* check output speed */
1071                     if (zfp.currentJobID > 1) {  /* only possible if nbWorkers >= 1 */
1072 
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);
1077 
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 */
1084                           ) {
1085                             DISPLAYLEVEL(6, "all buffers full : compression stopped => slow down \n")
1086                             speedChange = slower;
1087                         }
1088 
1089                         previous_zfp_update = zfp;
1090 
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 */
1093                           ) {
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;
1096                         }
1097                         flushWaiting = 0;
1098                     }
1099 
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")
1103 
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 */
1123                                 ) {
1124                                     DISPLAYLEVEL(6, "recommend faster as in(%llu) >= (%llu)comp(%llu) <= out(%llu) \n",
1125                                                     newlyIngested, newlyConsumed, newlyProduced, newlyFlushed);
1126                                     speedChange = faster;
1127                                 }
1128                             }
1129                             inputBlocked = 0;
1130                             inputPresented = 0;
1131                         }
1132 
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);
1140                         }
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);
1147                         }
1148                         speedChange = noChange;
1149 
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);
1156 
1157     if (ferror(srcFile)) {
1158         EXM_THROW(26, "Read error : I/O error");
1159     }
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);
1163     }
1164 
1165     return compressedfilesize;
1166 }
1167 
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
1172  */
1173 static int
1174 FIO_compressFilename_internal(FIO_prefs_t* const prefs,
1175                               cRess_t ress,
1176                               const char* dstFileName, const char* srcFileName,
1177                               int compressionLevel)
1178 {
1179     UTIL_time_t const timeStart = UTIL_getTime();
1180     clock_t const cpuStart = clock();
1181     U64 readsize = 0;
1182     U64 compressedfilesize = 0;
1183     U64 const fileSize = UTIL_getFileSize(srcFileName);
1184     DISPLAYLEVEL(5, "%s: %u bytes \n", srcFileName, (unsigned)fileSize);
1185 
1186     /* compression format selection */
1187     switch (prefs->compressionType) {
1188         default:
1189         case FIO_zstdCompression:
1190             compressedfilesize = FIO_compressZstdFrame(prefs, &ress, srcFileName, fileSize, compressionLevel, &readsize);
1191             break;
1192 
1193         case FIO_gzipCompression:
1194 #ifdef ZSTD_GZCOMPRESS
1195             compressedfilesize = FIO_compressGzFrame(&ress, srcFileName, fileSize, compressionLevel, &readsize);
1196 #else
1197             (void)compressionLevel;
1198             EXM_THROW(20, "zstd: %s: file cannot be compressed as gzip (zstd compiled without ZSTD_GZCOMPRESS) -- ignored \n",
1199                             srcFileName);
1200 #endif
1201             break;
1202 
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);
1207 #else
1208             (void)compressionLevel;
1209             EXM_THROW(20, "zstd: %s: file cannot be compressed as xz/lzma (zstd compiled without ZSTD_LZMACOMPRESS) -- ignored \n",
1210                             srcFileName);
1211 #endif
1212             break;
1213 
1214         case FIO_lz4Compression:
1215 #ifdef ZSTD_LZ4COMPRESS
1216             compressedfilesize = FIO_compressLz4Frame(&ress, srcFileName, fileSize, compressionLevel, prefs->checksumFlag, &readsize);
1217 #else
1218             (void)compressionLevel;
1219             EXM_THROW(20, "zstd: %s: file cannot be compressed as lz4 (zstd compiled without ZSTD_LZ4COMPRESS) -- ignored \n",
1220                             srcFileName);
1221 #endif
1222             break;
1223     }
1224 
1225     /* Status */
1226     DISPLAYLEVEL(2, "\r%79s\r", "");
1227     DISPLAYLEVEL(2,"%-20s :%6.2f%%   (%6llu => %6llu bytes, %s) \n",
1228         srcFileName,
1229         (double)compressedfilesize / (readsize+(!readsize)/*avoid div by zero*/) * 100,
1230         (unsigned long long)readsize, (unsigned long long) compressedfilesize,
1231          dstFileName);
1232 
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);
1241     }
1242     return 0;
1243 }
1244 
1245 
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,
1253  *            1 : pb
1254  */
1255 static int FIO_compressFilename_dstFile(FIO_prefs_t* const prefs,
1256                                         cRess_t ress,
1257                                         const char* dstFileName,
1258                                         const char* srcFileName,
1259                                         int compressionLevel)
1260 {
1261     int closeDstFile = 0;
1262     int result;
1263     stat_t statbuf;
1264     int transfer_permissions = 0;
1265 
1266     assert(ress.srcFile != NULL);
1267 
1268     if (ress.dstFile == NULL) {
1269         closeDstFile = 1;
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.
1276          */
1277         addHandler(dstFileName);
1278 
1279         if ( strcmp (srcFileName, stdinmark)
1280           && UTIL_getFileStat(srcFileName, &statbuf))
1281             transfer_permissions = 1;
1282     }
1283 
1284     result = FIO_compressFilename_internal(prefs, ress, dstFileName, srcFileName, compressionLevel);
1285 
1286     if (closeDstFile) {
1287         FILE* const dstFile = ress.dstFile;
1288         ress.dstFile = NULL;
1289 
1290         clearHandler();
1291 
1292         if (fclose(dstFile)) { /* error closing dstFile */
1293             DISPLAYLEVEL(1, "zstd: %s: %s \n", dstFileName, strerror(errno));
1294             result=1;
1295         }
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 */
1299           ) {
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);
1305         }
1306     }
1307 
1308     return result;
1309 }
1310 
1311 
1312 /*! FIO_compressFilename_srcFile() :
1313  *  @return : 0 : compression completed correctly,
1314  *            1 : missing or pb opening srcFileName
1315  */
1316 static int
1317 FIO_compressFilename_srcFile(FIO_prefs_t* const prefs,
1318                              cRess_t ress,
1319                              const char* dstFileName,
1320                              const char* srcFileName,
1321                              int compressionLevel)
1322 {
1323     int result;
1324 
1325     /* ensure src is not a directory */
1326     if (UTIL_isDirectory(srcFileName)) {
1327         DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
1328         return 1;
1329     }
1330 
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);
1334         return 1;
1335     }
1336 
1337     ress.srcFile = FIO_openSrcFile(srcFileName);
1338     if (ress.srcFile == NULL) return 1;   /* srcFile could not be opened */
1339 
1340     result = FIO_compressFilename_dstFile(prefs, ress, dstFileName, srcFileName, compressionLevel);
1341 
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 */
1347       ) {
1348         /* We must clear the handler, since after this point calling it would
1349          * delete both the source and destination files.
1350          */
1351         clearHandler();
1352         if (FIO_remove(srcFileName))
1353             EXM_THROW(1, "zstd: %s: %s", srcFileName, strerror(errno));
1354     }
1355     return result;
1356 }
1357 
1358 
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)
1363 {
1364     U64 const fileSize = UTIL_getFileSize(srcFileName);
1365     U64 const srcSize = (fileSize == UTIL_FILESIZE_UNKNOWN) ? ZSTD_CONTENTSIZE_UNKNOWN : fileSize;
1366 
1367     cRess_t const ress = FIO_createCResources(prefs, dictFileName, compressionLevel, srcSize, comprParams);
1368     int const result = FIO_compressFilename_srcFile(prefs, ress, dstFileName, srcFileName, compressionLevel);
1369 
1370 
1371     FIO_freeCResources(ress);
1372     return result;
1373 }
1374 
1375 
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)
1380  */
1381 static const char*
1382 FIO_determineCompressedName(const char* srcFileName, const char* suffix)
1383 {
1384     static size_t dfnbCapacity = 0;
1385     static char* dstFileNameBuffer = NULL;   /* using static allocation : this function cannot be multi-threaded */
1386 
1387     size_t const sfnSize = strlen(srcFileName);
1388     size_t const suffixSize = strlen(suffix);
1389 
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));
1397     }   }
1398     assert(dstFileNameBuffer != NULL);
1399     memcpy(dstFileNameBuffer, srcFileName, sfnSize);
1400     memcpy(dstFileNameBuffer+sfnSize, suffix, suffixSize+1 /* Include terminating null */);
1401 
1402     return dstFileNameBuffer;
1403 }
1404 
1405 
1406 /* FIO_compressMultipleFilenames() :
1407  * compress nbFiles files
1408  * into one destination (outFileName)
1409  * or into one file each (outFileName == NULL, but suffix != NULL).
1410  */
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)
1416 {
1417     int error = 0;
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);
1422 
1423     /* init */
1424     assert(outFileName != NULL || suffix != NULL);
1425 
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 */
1429             error = 1;
1430         } else {
1431             unsigned u;
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;
1438         }
1439     } else {
1440         unsigned u;
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);
1445     }   }
1446 
1447     FIO_freeCResources(ress);
1448     return error;
1449 }
1450 
1451 #endif /* #ifndef ZSTD_NOCOMPRESS */
1452 
1453 
1454 
1455 #ifndef ZSTD_NODECOMPRESS
1456 
1457 /* **************************************************************************
1458  *  Decompression
1459  ***************************************************************************/
1460 typedef struct {
1461     void*  srcBuffer;
1462     size_t srcBufferSize;
1463     size_t srcBufferLoaded;
1464     void*  dstBuffer;
1465     size_t dstBufferSize;
1466     ZSTD_DStream* dctx;
1467     FILE*  dstFile;
1468 } dRess_t;
1469 
1470 static dRess_t FIO_createDResources(FIO_prefs_t* const prefs, const char* dictFileName)
1471 {
1472     dRess_t ress;
1473     memset(&ress, 0, sizeof(ress));
1474 
1475     /* Allocation */
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");
1486 
1487     /* dictionary */
1488     {   void* dictBuffer;
1489         size_t const dictBufferSize = FIO_createDictBuffer(&dictBuffer, dictFileName);
1490         CHECK( ZSTD_initDStream_usingDict(ress.dctx, dictBuffer, dictBufferSize) );
1491         free(dictBuffer);
1492     }
1493 
1494     return ress;
1495 }
1496 
1497 static void FIO_freeDResources(dRess_t ress)
1498 {
1499     CHECK( ZSTD_freeDStream(ress.dctx) );
1500     free(ress.srcBuffer);
1501     free(ress.dstBuffer);
1502 }
1503 
1504 
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)
1508 {
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 */
1514 
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)",
1519                             strerror(errno));
1520         return 0;
1521     }
1522 
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;
1529     }
1530 
1531     while (ptrT < bufferTEnd) {
1532         size_t seg0SizeT = segmentSizeT;
1533         size_t nb0T;
1534 
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));
1540 
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");
1544             storedSkips = 0;
1545             seg0SizeT -= nb0T;
1546             ptrT += nb0T;
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");
1550         }   }
1551         ptrT += seg0SizeT;
1552     }
1553 
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);
1565                 if (seekResult)
1566                     EXM_THROW(74, "Sparse skip error ; try --no-sparse");
1567                 storedSkips = 0;
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");
1571     }   }   }   }
1572 
1573     return storedSkips;
1574 }
1575 
1576 static void
1577 FIO_fwriteSparseEnd(FIO_prefs_t* const prefs, FILE* file, unsigned storedSkips)
1578 {
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");
1589     }   }
1590 }
1591 
1592 
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)
1599 {
1600     size_t const blockSize = MIN(64 KB, bufferSize);
1601     size_t readFromInput = 1;
1602     unsigned storedSkips = 0;
1603 
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");
1608             return 1;
1609     }   }
1610 
1611     while (readFromInput) {
1612         readFromInput = fread(buffer, 1, blockSize, finput);
1613         storedSkips = FIO_fwriteSparse(prefs, foutput, buffer, readFromInput, storedSkips);
1614     }
1615 
1616     FIO_fwriteSparseEnd(prefs, foutput, storedSkips);
1617     return 0;
1618 }
1619 
1620 /* FIO_highbit64() :
1621  * gives position of highest bit.
1622  * note : only works for v > 0 !
1623  */
1624 static unsigned FIO_highbit64(unsigned long long v)
1625 {
1626     unsigned count = 0;
1627     assert(v != 0);
1628     v >>= 1;
1629     while (v) { v >>= 1; count++; }
1630     return count;
1631 }
1632 
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)
1636 {
1637     ZSTD_frameHeader header;
1638 
1639     /* Help message only for one specific error */
1640     if (ZSTD_getErrorCode(err) != ZSTD_error_frameParameter_windowTooLarge)
1641         return;
1642 
1643     /* Try to decode the frame header */
1644     err = ZSTD_getFrameHeader(&header, ress->srcBuffer, ress->srcBufferLoaded);
1645     if (err == 0) {
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);
1656             return;
1657         }
1658     }
1659     DISPLAYLEVEL(1, "%s : Window log larger than ZSTD_WINDOWLOG_MAX=%u; not supported\n",
1660                     srcFileName, ZSTD_WINDOWLOG_MAX);
1661 }
1662 
1663 /** FIO_decompressFrame() :
1664  *  @return : size of decoded zstd frame, or an error code
1665 */
1666 #define FIO_ERROR_FRAME_DECODING   ((unsigned long long)(-2))
1667 static unsigned long long FIO_decompressZstdFrame(
1668                                        FIO_prefs_t* const prefs,
1669                                        dRess_t* ress,
1670                                        FILE* finput,
1671                                        const char* srcFileName,
1672                                        U64 alreadyDecoded)
1673 {
1674     U64 frameSize = 0;
1675     U32 storedSkips = 0;
1676 
1677     size_t const srcFileLength = strlen(srcFileName);
1678     if (srcFileLength>20) srcFileName += srcFileLength-20;  /* display last 20 characters only */
1679 
1680     ZSTD_resetDStream(ress->dctx);
1681 
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);
1688     }   }
1689 
1690     /* Main decompression Loop */
1691     while (1) {
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;
1700         }
1701 
1702         /* Write block */
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) );
1707 
1708         if (inBuff.pos > 0) {
1709             memmove(ress->srcBuffer, (char*)ress->srcBuffer + inBuff.pos, inBuff.size - inBuff.pos);
1710             ress->srcBufferLoaded -= inBuff.pos;
1711         }
1712 
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",
1716                             srcFileName);
1717             return FIO_ERROR_FRAME_DECODING;
1718         }
1719 
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);
1726                 if (readSize==0) {
1727                     DISPLAYLEVEL(1, "%s : Read error (39) : premature end \n",
1728                                     srcFileName);
1729                     return FIO_ERROR_FRAME_DECODING;
1730                 }
1731                 ress->srcBufferLoaded += readSize;
1732     }   }   }
1733 
1734     FIO_fwriteSparseEnd(prefs, ress->dstFile, storedSkips);
1735 
1736     return frameSize;
1737 }
1738 
1739 
1740 #ifdef ZSTD_GZDECOMPRESS
1741 static unsigned long long FIO_decompressGzFrame(dRess_t* ress,
1742                                     FILE* srcFile, const char* srcFileName)
1743 {
1744     unsigned long long outFileSize = 0;
1745     z_stream strm;
1746     int flush = Z_NO_FLUSH;
1747     int decodingError = 0;
1748 
1749     strm.zalloc = Z_NULL;
1750     strm.zfree = Z_NULL;
1751     strm.opaque = Z_NULL;
1752     strm.next_in = 0;
1753     strm.avail_in = 0;
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;
1757 
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;
1762 
1763     for ( ; ; ) {
1764         int ret;
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;
1770         }
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;
1775         }
1776         if (ret != Z_OK && ret != Z_STREAM_END) {
1777             DISPLAYLEVEL(1, "zstd: %s: inflate error %d \n", srcFileName, ret);
1778             decodingError = 1; break;
1779         }
1780         {   size_t const decompBytes = ress->dstBufferSize - strm.avail_out;
1781             if (decompBytes) {
1782                 if (fwrite(ress->dstBuffer, 1, decompBytes, ress->dstFile) != decompBytes) {
1783                     DISPLAYLEVEL(1, "zstd: %s \n", strerror(errno));
1784                     decodingError = 1; break;
1785                 }
1786                 outFileSize += decompBytes;
1787                 strm.next_out = (Bytef*)ress->dstBuffer;
1788                 strm.avail_out = (uInt)ress->dstBufferSize;
1789             }
1790         }
1791         if (ret == Z_STREAM_END) break;
1792     }
1793 
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);
1800         decodingError = 1;
1801     }
1802     return decodingError ? FIO_ERROR_FRAME_DECODING : outFileSize;
1803 }
1804 #endif
1805 
1806 
1807 #ifdef ZSTD_LZMADECOMPRESS
1808 static unsigned long long FIO_decompressLzmaFrame(dRess_t* ress, FILE* srcFile, const char* srcFileName, int plain_lzma)
1809 {
1810     unsigned long long outFileSize = 0;
1811     lzma_stream strm = LZMA_STREAM_INIT;
1812     lzma_action action = LZMA_RUN;
1813     lzma_ret initRet;
1814     int decodingError = 0;
1815 
1816     strm.next_in = 0;
1817     strm.avail_in = 0;
1818     if (plain_lzma) {
1819         initRet = lzma_alone_decoder(&strm, UINT64_MAX); /* LZMA */
1820     } else {
1821         initRet = lzma_stream_decoder(&strm, UINT64_MAX, 0); /* XZ */
1822     }
1823 
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;
1829     }
1830 
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;
1835 
1836     for ( ; ; ) {
1837         lzma_ret ret;
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;
1843         }
1844         ret = lzma_code(&strm, action);
1845 
1846         if (ret == LZMA_BUF_ERROR) {
1847             DISPLAYLEVEL(1, "zstd: %s: premature lzma end \n", srcFileName);
1848             decodingError = 1; break;
1849         }
1850         if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
1851             DISPLAYLEVEL(1, "zstd: %s: lzma_code decoding error %d \n",
1852                             srcFileName, ret);
1853             decodingError = 1; break;
1854         }
1855         {   size_t const decompBytes = ress->dstBufferSize - strm.avail_out;
1856             if (decompBytes) {
1857                 if (fwrite(ress->dstBuffer, 1, decompBytes, ress->dstFile) != decompBytes) {
1858                     DISPLAYLEVEL(1, "zstd: %s \n", strerror(errno));
1859                     decodingError = 1; break;
1860                 }
1861                 outFileSize += decompBytes;
1862                 strm.next_out = (BYTE*)ress->dstBuffer;
1863                 strm.avail_out = ress->dstBufferSize;
1864         }   }
1865         if (ret == LZMA_STREAM_END) break;
1866     }
1867 
1868     if (strm.avail_in > 0)
1869         memmove(ress->srcBuffer, strm.next_in, strm.avail_in);
1870     ress->srcBufferLoaded = strm.avail_in;
1871     lzma_end(&strm);
1872     return decodingError ? FIO_ERROR_FRAME_DECODING : outFileSize;
1873 }
1874 #endif
1875 
1876 #ifdef ZSTD_LZ4DECOMPRESS
1877 static unsigned long long FIO_decompressLz4Frame(dRess_t* ress,
1878                                     FILE* srcFile, const char* srcFileName)
1879 {
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;
1885 
1886     if (LZ4F_isError(errorCode)) {
1887         DISPLAYLEVEL(1, "zstd: failed to create lz4 decompression context \n");
1888         return FIO_ERROR_FRAME_DECODING;
1889     }
1890 
1891     /* Init feed with magic number (already consumed from FILE* sFile) */
1892     {   size_t inSize = 4;
1893         size_t outSize= 0;
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;
1901     }   }
1902 
1903     /* Main Loop */
1904     for (;nextToLoad;) {
1905         size_t readSize;
1906         size_t pos = 0;
1907         size_t decodedBytes = ress->dstBufferSize;
1908 
1909         /* Read input */
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 */
1913 
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;
1923             }
1924             pos += remaining;
1925 
1926             /* Write Block */
1927             if (decodedBytes) {
1928                 if (fwrite(ress->dstBuffer, 1, decodedBytes, ress->dstFile) != decodedBytes) {
1929                     DISPLAYLEVEL(1, "zstd: %s \n", strerror(errno));
1930                     decodingError = 1; nextToLoad = 0; break;
1931                 }
1932                 filesize += decodedBytes;
1933                 DISPLAYUPDATE(2, "\rDecompressed : %u MB  ", (unsigned)(filesize>>20));
1934             }
1935 
1936             if (!nextToLoad) break;
1937         }
1938     }
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);
1942         decodingError=1;
1943     }
1944 
1945     if (nextToLoad!=0) {
1946         DISPLAYLEVEL(1, "zstd: %s: unfinished lz4 stream \n", srcFileName);
1947         decodingError=1;
1948     }
1949 
1950     LZ4F_freeDecompressionContext(dCtx);
1951     ress->srcBufferLoaded = 0; /* LZ4F will reach exact frame boundary */
1952 
1953     return decodingError ? FIO_ERROR_FRAME_DECODING : filesize;
1954 }
1955 #endif
1956 
1957 
1958 
1959 /** FIO_decompressFrames() :
1960  *  Find and decode frames inside srcFile
1961  *  srcFile presumed opened and valid
1962  * @return : 0 : OK
1963  *           1 : error
1964  */
1965 static int FIO_decompressFrames(FIO_prefs_t* const prefs, dRess_t ress, FILE* srcFile,
1966                         const char* dstFileName, const char* srcFileName)
1967 {
1968     unsigned readSomething = 0;
1969     unsigned long long filesize = 0;
1970     assert(srcFile != NULL);
1971 
1972     /* for each frame */
1973     for ( ; ; ) {
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);
1983                 return 1;
1984             }  /* else, just reached frame boundary */
1985             break;   /* no more input */
1986         }
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);
1990             return 1;
1991         }
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;
2001 #else
2002             DISPLAYLEVEL(1, "zstd: %s: gzip file cannot be uncompressed (zstd compiled without HAVE_ZLIB) -- ignored \n", srcFileName);
2003             return 1;
2004 #endif
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;
2011 #else
2012             DISPLAYLEVEL(1, "zstd: %s: xz/lzma file cannot be uncompressed (zstd compiled without HAVE_LZMA) -- ignored \n", srcFileName);
2013             return 1;
2014 #endif
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;
2020 #else
2021             DISPLAYLEVEL(1, "zstd: %s: lz4 file cannot be uncompressed (zstd compiled without HAVE_LZ4) -- ignored \n", srcFileName);
2022             return 1;
2023 #endif
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);
2029         } else {
2030             DISPLAYLEVEL(1, "zstd: %s: unsupported format \n", srcFileName);
2031             return 1;
2032     }   }  /* for each frame */
2033 
2034     /* Final Status */
2035     DISPLAYLEVEL(2, "\r%79s\r", "");
2036     DISPLAYLEVEL(2, "%-20s: %llu bytes \n", srcFileName, filesize);
2037 
2038     return 0;
2039 }
2040 
2041 /** FIO_decompressDstFile() :
2042     open `dstFileName`,
2043     or path-through if ress.dstFile is already != 0,
2044     then start decompression process (FIO_decompressFrames()).
2045     @return : 0 : OK
2046               1 : operation aborted
2047 */
2048 static int FIO_decompressDstFile(FIO_prefs_t* const prefs,
2049                                  dRess_t ress, FILE* srcFile,
2050                                  const char* dstFileName, const char* srcFileName)
2051 {
2052     int result;
2053     stat_t statbuf;
2054     int transfer_permissions = 0;
2055     int releaseDstFile = 0;
2056 
2057     if (ress.dstFile == NULL) {
2058         releaseDstFile = 1;
2059 
2060         ress.dstFile = FIO_openDstFile(prefs, srcFileName, dstFileName);
2061         if (ress.dstFile==0) return 1;
2062 
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.
2066          */
2067         addHandler(dstFileName);
2068 
2069         if ( strcmp(srcFileName, stdinmark)   /* special case : don't transfer permissions from stdin */
2070           && UTIL_getFileStat(srcFileName, &statbuf) )
2071             transfer_permissions = 1;
2072     }
2073 
2074 
2075     result = FIO_decompressFrames(prefs, ress, srcFile, dstFileName, srcFileName);
2076 
2077     if (releaseDstFile) {
2078         FILE* const dstFile = ress.dstFile;
2079         clearHandler();
2080         ress.dstFile = NULL;
2081         if (fclose(dstFile)) {
2082             DISPLAYLEVEL(1, "zstd: %s: %s \n", dstFileName, strerror(errno));
2083             result = 1;
2084         }
2085 
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 */
2089           ) {
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 */
2096         }
2097     }
2098 
2099     return result;
2100 }
2101 
2102 
2103 /** FIO_decompressSrcFile() :
2104     Open `srcFileName`, transfer control to decompressDstFile()
2105     @return : 0 : OK
2106               1 : error
2107 */
2108 static int FIO_decompressSrcFile(FIO_prefs_t* const prefs, dRess_t ress, const char* dstFileName, const char* srcFileName)
2109 {
2110     FILE* srcFile;
2111     int result;
2112 
2113     if (UTIL_isDirectory(srcFileName)) {
2114         DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
2115         return 1;
2116     }
2117 
2118     srcFile = FIO_openSrcFile(srcFileName);
2119     if (srcFile==NULL) return 1;
2120     ress.srcBufferLoaded = 0;
2121 
2122     result = FIO_decompressDstFile(prefs, ress, srcFile, dstFileName, srcFileName);
2123 
2124     /* Close file */
2125     if (fclose(srcFile)) {
2126         DISPLAYLEVEL(1, "zstd: %s: %s \n", srcFileName, strerror(errno));  /* error should not happen */
2127         return 1;
2128     }
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.
2134          */
2135         clearHandler();
2136         if (FIO_remove(srcFileName)) {
2137             /* failed to remove src file */
2138             DISPLAYLEVEL(1, "zstd: %s: %s \n", srcFileName, strerror(errno));
2139             return 1;
2140     }   }
2141     return result;
2142 }
2143 
2144 
2145 
2146 int FIO_decompressFilename(FIO_prefs_t* const prefs,
2147                            const char* dstFileName, const char* srcFileName,
2148                            const char* dictFileName)
2149 {
2150     dRess_t const ress = FIO_createDResources(prefs, dictFileName);
2151 
2152     int const decodingError = FIO_decompressSrcFile(prefs, ress, dstFileName, srcFileName);
2153 
2154     FIO_freeDResources(ress);
2155     return decodingError;
2156 }
2157 
2158 
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 */
2163 static const char*
2164 FIO_determineDstName(const char* srcFileName)
2165 {
2166     static size_t dfnbCapacity = 0;
2167     static char* dstFileNameBuffer = NULL;   /* using static allocation : this function cannot be multi-threaded */
2168 
2169     size_t const sfnSize = strlen(srcFileName);
2170     size_t suffixSize;
2171     const char* const suffixPtr = strrchr(srcFileName, '.');
2172     if (suffixPtr == NULL) {
2173         DISPLAYLEVEL(1, "zstd: %s: unknown suffix -- ignored \n",
2174                         srcFileName);
2175         return NULL;
2176     }
2177     suffixSize = strlen(suffixPtr);
2178 
2179     /* check suffix is authorized */
2180     if (sfnSize <= suffixSize
2181         || (   strcmp(suffixPtr, ZSTD_EXTENSION)
2182         #ifdef ZSTD_GZDECOMPRESS
2183             && strcmp(suffixPtr, GZ_EXTENSION)
2184         #endif
2185         #ifdef ZSTD_LZMADECOMPRESS
2186             && strcmp(suffixPtr, XZ_EXTENSION)
2187             && strcmp(suffixPtr, LZMA_EXTENSION)
2188         #endif
2189         #ifdef ZSTD_LZ4DECOMPRESS
2190             && strcmp(suffixPtr, LZ4_EXTENSION)
2191         #endif
2192             ) ) {
2193         const char* suffixlist = ZSTD_EXTENSION
2194         #ifdef ZSTD_GZDECOMPRESS
2195             "/" GZ_EXTENSION
2196         #endif
2197         #ifdef ZSTD_LZMADECOMPRESS
2198             "/" XZ_EXTENSION "/" LZMA_EXTENSION
2199         #endif
2200         #ifdef ZSTD_LZ4DECOMPRESS
2201             "/" LZ4_EXTENSION
2202         #endif
2203         ;
2204         DISPLAYLEVEL(1, "zstd: %s: unknown suffix (%s expected) -- ignored \n",
2205                      srcFileName, suffixlist);
2206         return NULL;
2207     }
2208 
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));
2216     }
2217 
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;
2223 
2224     /* note : dstFileNameBuffer memory is not going to be free */
2225 }
2226 
2227 
2228 int
2229 FIO_decompressMultipleFilenames(FIO_prefs_t* const prefs,
2230                                 const char* srcNamesTable[], unsigned nbFiles,
2231                                 const char* outFileName,
2232                                 const char* dictFileName)
2233 {
2234     int error = 0;
2235     dRess_t ress = FIO_createDResources(prefs, dictFileName);
2236 
2237     if (outFileName) {
2238         unsigned u;
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",
2245                         strerror(errno));
2246     } else {
2247         unsigned u;
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; }
2252 
2253             error |= FIO_decompressSrcFile(prefs, ress, dstFileName, srcFileName);
2254         }
2255     }
2256 
2257     FIO_freeDResources(ress);
2258     return error;
2259 }
2260 
2261 
2262 
2263 /* **************************************************************************
2264  *  .zst file info (--list command)
2265  ***************************************************************************/
2266 
2267 typedef struct {
2268     U64 decompressedSize;
2269     U64 compressedSize;
2270     U64 windowSize;
2271     int numActualFrames;
2272     int numSkippableFrames;
2273     int decompUnavailable;
2274     int usesCheck;
2275     U32 nbFiles;
2276 } fileInfo_t;
2277 
2278 typedef enum {
2279   info_success=0,
2280   info_frame_error=1,
2281   info_not_zstd=2,
2282   info_file_error=3,
2283   info_truncated_input=4,
2284 } InfoError;
2285 
2286 #define ERROR_IF(c,n,...) {             \
2287     if (c) {                           \
2288         DISPLAYLEVEL(1, __VA_ARGS__);  \
2289         DISPLAYLEVEL(1, " \n");        \
2290         return n;                      \
2291     }                                  \
2292 }
2293 
2294 static InfoError
2295 FIO_analyzeFrames(fileInfo_t* info, FILE* const srcFile)
2296 {
2297     /* begin analyzing frame */
2298     for ( ; ; ) {
2299         BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];
2300         size_t const numBytesRead = fread(headerBuffer, 1, sizeof(headerBuffer), srcFile);
2301         if (numBytesRead < ZSTD_FRAMEHEADERSIZE_MIN) {
2302             if ( feof(srcFile)
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",
2310                   file_position,
2311                   file_size);
2312                 break;  /* correct end of file => success */
2313             }
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");
2316         }
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;
2325                 } else {
2326                     info->decompressedSize += frameContentSize;
2327                 }
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");
2336                 }
2337 
2338                 /* skip all blocks in the frame */
2339                 {   int lastBlock = 0;
2340                     do {
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");
2353                         }
2354                     } while (lastBlock != 1);
2355                 }
2356 
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");
2364                 }   }
2365                 info->numActualFrames++;
2366             }
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++;
2374             }
2375             /* unknown content */
2376             else {
2377                 return info_not_zstd;
2378             }
2379         }  /* magic number analysis */
2380     }  /* end analyzing frames */
2381     return info_success;
2382 }
2383 
2384 
2385 static InfoError
2386 getFileInfo_fileConfirmed(fileInfo_t* info, const char* inFileName)
2387 {
2388     InfoError status;
2389     FILE* const srcFile = FIO_openSrcFile(inFileName);
2390     ERROR_IF(srcFile == NULL, info_file_error, "Error: could not open source file %s", inFileName);
2391 
2392     info->compressedSize = UTIL_getFileSize(inFileName);
2393     status = FIO_analyzeFrames(info, srcFile);
2394 
2395     fclose(srcFile);
2396     info->nbFiles = 1;
2397     return status;
2398 }
2399 
2400 
2401 /** getFileInfo() :
2402  *  Reads information from file, stores in *info
2403  * @return : InfoError status
2404  */
2405 static InfoError
2406 getFileInfo(fileInfo_t* info, const char* srcFileName)
2407 {
2408     ERROR_IF(!UTIL_isRegularFile(srcFileName),
2409             info_file_error, "Error : %s is not a file", srcFileName);
2410     return getFileInfo_fileConfirmed(info, srcFileName);
2411 }
2412 
2413 
2414 static void
2415 displayInfo(const char* inFileName, const fileInfo_t* info, int displayLevel)
2416 {
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);
2431         } else {
2432             DISPLAYOUT("%6d  %5d  %7.2f %2s                       %5s  %s\n",
2433                     info->numSkippableFrames + info->numActualFrames,
2434                     info->numSkippableFrames,
2435                     compressedSizeUnit, unitStr,
2436                     checkString, inFileName);
2437         }
2438     } else {
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);
2454         }
2455         DISPLAYOUT("Check: %s\n", checkString);
2456         DISPLAYOUT("\n");
2457     }
2458 }
2459 
2460 static fileInfo_t FIO_addFInfo(fileInfo_t fi1, fileInfo_t fi2)
2461 {
2462     fileInfo_t total;
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;
2471     return total;
2472 }
2473 
2474 static int
2475 FIO_listFile(fileInfo_t* total, const char* inFileName, int displayLevel)
2476 {
2477     fileInfo_t info;
2478     memset(&info, 0, sizeof(info));
2479     {   InfoError const error = getFileInfo(&info, inFileName);
2480         switch (error) {
2481             case info_frame_error:
2482                 /* display error, but provide output */
2483                 DISPLAYLEVEL(1, "Error while parsing \"%s\" \n", inFileName);
2484                 break;
2485             case info_not_zstd:
2486                 DISPLAYOUT("File \"%s\" not compressed by zstd \n", inFileName);
2487                 if (displayLevel > 2) DISPLAYOUT("\n");
2488                 return 1;
2489             case info_file_error:
2490                 /* error occurred while opening the file */
2491                 if (displayLevel > 2) DISPLAYOUT("\n");
2492                 return 1;
2493             case info_truncated_input:
2494                 DISPLAYOUT("File \"%s\" is truncated \n", inFileName);
2495                 if (displayLevel > 2) DISPLAYOUT("\n");
2496                 return 1;
2497             case info_success:
2498             default:
2499                 break;
2500         }
2501 
2502         displayInfo(inFileName, &info, displayLevel);
2503         *total = FIO_addFInfo(*total, info);
2504         assert(error == info_success || error == info_frame_error);
2505         return error;
2506     }
2507 }
2508 
2509 int FIO_listMultipleFiles(unsigned numFiles, const char** filenameTable, int displayLevel)
2510 {
2511     /* ensure no specified input is stdin (needs fseek() capability) */
2512     {   unsigned u;
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");
2516     }   }
2517 
2518     if (numFiles == 0) {
2519         if (!IS_CONSOLE(stdin)) {
2520             DISPLAYLEVEL(1, "zstd: --list does not support reading from standard input \n");
2521         }
2522         DISPLAYLEVEL(1, "No files given \n");
2523         return 1;
2524     }
2525 
2526     if (displayLevel <= 2) {
2527         DISPLAYOUT("Frames  Skips  Compressed  Uncompressed  Ratio  Check  Filename\n");
2528     }
2529     {   int error = 0;
2530         fileInfo_t total;
2531         memset(&total, 0, sizeof(total));
2532         total.usesCheck = 1;
2533         /* --list each file, and check for any error */
2534         {   unsigned u;
2535             for (u=0; u<numFiles;u++) {
2536                 error |= FIO_listFile(&total, filenameTable[u], displayLevel);
2537         }   }
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);
2552             } else {
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);
2558         }   }
2559         return error;
2560     }
2561 }
2562 
2563 
2564 #endif /* #ifndef ZSTD_NODECOMPRESS */
2565