10c16b537SWarner Losh /*
20c16b537SWarner Losh * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
30c16b537SWarner Losh * All rights reserved.
40c16b537SWarner Losh *
50c16b537SWarner Losh * This source code is licensed under both the BSD-style license (found in the
60c16b537SWarner Losh * LICENSE file in the root directory of this source tree) and the GPLv2 (found
70c16b537SWarner Losh * in the COPYING file in the root directory of this source tree).
80c16b537SWarner Losh * You may select, at your option, one of the above-listed licenses.
90c16b537SWarner Losh */
100c16b537SWarner Losh
110c16b537SWarner Losh
120c16b537SWarner Losh /*-************************************
130c16b537SWarner Losh * Tuning parameters
140c16b537SWarner Losh **************************************/
150c16b537SWarner Losh #ifndef ZSTDCLI_CLEVEL_DEFAULT
160c16b537SWarner Losh # define ZSTDCLI_CLEVEL_DEFAULT 3
170c16b537SWarner Losh #endif
180c16b537SWarner Losh
190c16b537SWarner Losh #ifndef ZSTDCLI_CLEVEL_MAX
200c16b537SWarner Losh # define ZSTDCLI_CLEVEL_MAX 19 /* without using --ultra */
210c16b537SWarner Losh #endif
220c16b537SWarner Losh
230c16b537SWarner Losh
240c16b537SWarner Losh
250c16b537SWarner Losh /*-************************************
260c16b537SWarner Losh * Dependencies
270c16b537SWarner Losh **************************************/
280c16b537SWarner Losh #include "platform.h" /* IS_CONSOLE, PLATFORM_POSIX_VERSION */
290c16b537SWarner Losh #include "util.h" /* UTIL_HAS_CREATEFILELIST, UTIL_createFileList */
300c16b537SWarner Losh #include <stdio.h> /* fprintf(), stdin, stdout, stderr */
310c16b537SWarner Losh #include <string.h> /* strcmp, strlen */
320c16b537SWarner Losh #include <errno.h> /* errno */
330c16b537SWarner Losh #include "fileio.h" /* stdinmark, stdoutmark, ZSTD_EXTENSION */
340c16b537SWarner Losh #ifndef ZSTD_NOBENCH
350c16b537SWarner Losh # include "bench.h" /* BMK_benchFiles, BMK_SetNbSeconds */
360c16b537SWarner Losh #endif
370c16b537SWarner Losh #ifndef ZSTD_NODICT
380c16b537SWarner Losh # include "dibio.h" /* ZDICT_cover_params_t, DiB_trainFromFiles() */
390c16b537SWarner Losh #endif
400c16b537SWarner Losh #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel */
410c16b537SWarner Losh #include "zstd.h" /* ZSTD_VERSION_STRING */
420c16b537SWarner Losh
430c16b537SWarner Losh
440c16b537SWarner Losh /*-************************************
450c16b537SWarner Losh * Constants
460c16b537SWarner Losh **************************************/
470c16b537SWarner Losh #define COMPRESSOR_NAME "zstd command line interface"
480c16b537SWarner Losh #ifndef ZSTD_VERSION
490c16b537SWarner Losh # define ZSTD_VERSION "v" ZSTD_VERSION_STRING
500c16b537SWarner Losh #endif
510c16b537SWarner Losh #define AUTHOR "Yann Collet"
520c16b537SWarner Losh #define WELCOME_MESSAGE "*** %s %i-bits %s, by %s ***\n", COMPRESSOR_NAME, (int)(sizeof(size_t)*8), ZSTD_VERSION, AUTHOR
530c16b537SWarner Losh
540c16b537SWarner Losh #define ZSTD_ZSTDMT "zstdmt"
550c16b537SWarner Losh #define ZSTD_UNZSTD "unzstd"
560c16b537SWarner Losh #define ZSTD_CAT "zstdcat"
57*19fcbaf1SConrad Meyer #define ZSTD_ZCAT "zcat"
580c16b537SWarner Losh #define ZSTD_GZ "gzip"
590c16b537SWarner Losh #define ZSTD_GUNZIP "gunzip"
600c16b537SWarner Losh #define ZSTD_GZCAT "gzcat"
610c16b537SWarner Losh #define ZSTD_LZMA "lzma"
620c16b537SWarner Losh #define ZSTD_UNLZMA "unlzma"
630c16b537SWarner Losh #define ZSTD_XZ "xz"
640c16b537SWarner Losh #define ZSTD_UNXZ "unxz"
650c16b537SWarner Losh #define ZSTD_LZ4 "lz4"
660c16b537SWarner Losh #define ZSTD_UNLZ4 "unlz4"
670c16b537SWarner Losh
680c16b537SWarner Losh #define KB *(1 <<10)
690c16b537SWarner Losh #define MB *(1 <<20)
700c16b537SWarner Losh #define GB *(1U<<30)
710c16b537SWarner Losh
721ad8ac7aSBaptiste Daroussin #define DISPLAY_LEVEL_DEFAULT 2
730c16b537SWarner Losh
740c16b537SWarner Losh static const char* g_defaultDictName = "dictionary";
750c16b537SWarner Losh static const unsigned g_defaultMaxDictSize = 110 KB;
760c16b537SWarner Losh static const int g_defaultDictCLevel = 3;
770c16b537SWarner Losh static const unsigned g_defaultSelectivityLevel = 9;
780c16b537SWarner Losh static const unsigned g_defaultMaxWindowLog = 27;
790c16b537SWarner Losh #define OVERLAP_LOG_DEFAULT 9999
800c16b537SWarner Losh #define LDM_PARAM_DEFAULT 9999 /* Default for parameters where 0 is valid */
810c16b537SWarner Losh static U32 g_overlapLog = OVERLAP_LOG_DEFAULT;
820c16b537SWarner Losh static U32 g_ldmHashLog = 0;
830c16b537SWarner Losh static U32 g_ldmMinMatch = 0;
840c16b537SWarner Losh static U32 g_ldmHashEveryLog = LDM_PARAM_DEFAULT;
850c16b537SWarner Losh static U32 g_ldmBucketSizeLog = LDM_PARAM_DEFAULT;
860c16b537SWarner Losh
870c16b537SWarner Losh
880c16b537SWarner Losh /*-************************************
890c16b537SWarner Losh * Display Macros
900c16b537SWarner Losh **************************************/
910c16b537SWarner Losh #define DISPLAY(...) fprintf(g_displayOut, __VA_ARGS__)
920c16b537SWarner Losh #define DISPLAYLEVEL(l, ...) { if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } }
930c16b537SWarner Losh static int g_displayLevel = DISPLAY_LEVEL_DEFAULT; /* 0 : no display, 1: errors, 2 : + result + interaction + warnings, 3 : + progression, 4 : + information */
940c16b537SWarner Losh static FILE* g_displayOut;
950c16b537SWarner Losh
960c16b537SWarner Losh
970c16b537SWarner Losh /*-************************************
980c16b537SWarner Losh * Command Line
990c16b537SWarner Losh **************************************/
usage(const char * programName)1000c16b537SWarner Losh static int usage(const char* programName)
1010c16b537SWarner Losh {
1020c16b537SWarner Losh DISPLAY( "Usage : \n");
1030c16b537SWarner Losh DISPLAY( " %s [args] [FILE(s)] [-o file] \n", programName);
1040c16b537SWarner Losh DISPLAY( "\n");
1050c16b537SWarner Losh DISPLAY( "FILE : a filename \n");
1060c16b537SWarner Losh DISPLAY( " with no FILE, or when FILE is - , read standard input\n");
1070c16b537SWarner Losh DISPLAY( "Arguments : \n");
1080c16b537SWarner Losh #ifndef ZSTD_NOCOMPRESS
1090c16b537SWarner Losh DISPLAY( " -# : # compression level (1-%d, default: %d) \n", ZSTDCLI_CLEVEL_MAX, ZSTDCLI_CLEVEL_DEFAULT);
1100c16b537SWarner Losh #endif
1110c16b537SWarner Losh #ifndef ZSTD_NODECOMPRESS
1120c16b537SWarner Losh DISPLAY( " -d : decompression \n");
1130c16b537SWarner Losh #endif
1140c16b537SWarner Losh DISPLAY( " -D file: use `file` as Dictionary \n");
1150c16b537SWarner Losh DISPLAY( " -o file: result stored into `file` (only if 1 input file) \n");
1160c16b537SWarner Losh DISPLAY( " -f : overwrite output without prompting and (de)compress links \n");
1170c16b537SWarner Losh DISPLAY( "--rm : remove source file(s) after successful de/compression \n");
1180c16b537SWarner Losh DISPLAY( " -k : preserve source file(s) (default) \n");
1190c16b537SWarner Losh DISPLAY( " -h/-H : display help/long help and exit \n");
1200c16b537SWarner Losh return 0;
1210c16b537SWarner Losh }
1220c16b537SWarner Losh
usage_advanced(const char * programName)1230c16b537SWarner Losh static int usage_advanced(const char* programName)
1240c16b537SWarner Losh {
1250c16b537SWarner Losh DISPLAY(WELCOME_MESSAGE);
1260c16b537SWarner Losh usage(programName);
1270c16b537SWarner Losh DISPLAY( "\n");
1280c16b537SWarner Losh DISPLAY( "Advanced arguments : \n");
1290c16b537SWarner Losh DISPLAY( " -V : display Version number and exit \n");
1300c16b537SWarner Losh DISPLAY( " -v : verbose mode; specify multiple times to increase verbosity\n");
1310c16b537SWarner Losh DISPLAY( " -q : suppress warnings; specify twice to suppress errors too\n");
1320c16b537SWarner Losh DISPLAY( " -c : force write to standard output, even if it is the console\n");
1330c16b537SWarner Losh DISPLAY( " -l : print information about zstd compressed files \n");
1340c16b537SWarner Losh #ifndef ZSTD_NOCOMPRESS
1350c16b537SWarner Losh DISPLAY( "--ultra : enable levels beyond %i, up to %i (requires more memory)\n", ZSTDCLI_CLEVEL_MAX, ZSTD_maxCLevel());
1360c16b537SWarner Losh DISPLAY( "--long[=#]: enable long distance matching with given window log (default: %u)\n", g_defaultMaxWindowLog);
137*19fcbaf1SConrad Meyer DISPLAY( "--fast[=#]: switch to ultra fast compression level (default: %u)\n", 1);
1380c16b537SWarner Losh #ifdef ZSTD_MULTITHREAD
139*19fcbaf1SConrad Meyer DISPLAY( " -T# : spawns # compression threads (default: 1, 0==# cores) \n");
1400c16b537SWarner Losh DISPLAY( " -B# : select size of each job (default: 0==automatic) \n");
1410c16b537SWarner Losh #endif
1420c16b537SWarner Losh DISPLAY( "--no-dictID : don't write dictID into header (dictionary compression)\n");
1430c16b537SWarner Losh DISPLAY( "--[no-]check : integrity check (default: enabled) \n");
1440c16b537SWarner Losh #endif
1450c16b537SWarner Losh #ifdef UTIL_HAS_CREATEFILELIST
1460c16b537SWarner Losh DISPLAY( " -r : operate recursively on directories \n");
1470c16b537SWarner Losh #endif
1480c16b537SWarner Losh #ifdef ZSTD_GZCOMPRESS
1490c16b537SWarner Losh DISPLAY( "--format=gzip : compress files to the .gz format \n");
1500c16b537SWarner Losh #endif
1510c16b537SWarner Losh #ifdef ZSTD_LZMACOMPRESS
1520c16b537SWarner Losh DISPLAY( "--format=xz : compress files to the .xz format \n");
1530c16b537SWarner Losh DISPLAY( "--format=lzma : compress files to the .lzma format \n");
1540c16b537SWarner Losh #endif
1550c16b537SWarner Losh #ifdef ZSTD_LZ4COMPRESS
1560c16b537SWarner Losh DISPLAY( "--format=lz4 : compress files to the .lz4 format \n");
1570c16b537SWarner Losh #endif
1580c16b537SWarner Losh #ifndef ZSTD_NODECOMPRESS
1590c16b537SWarner Losh DISPLAY( "--test : test compressed file integrity \n");
1600c16b537SWarner Losh #if ZSTD_SPARSE_DEFAULT
1610c16b537SWarner Losh DISPLAY( "--[no-]sparse : sparse mode (default: enabled on file, disabled on stdout)\n");
1620c16b537SWarner Losh #else
1630c16b537SWarner Losh DISPLAY( "--[no-]sparse : sparse mode (default: disabled)\n");
1640c16b537SWarner Losh #endif
1650c16b537SWarner Losh #endif
1660c16b537SWarner Losh DISPLAY( " -M# : Set a memory usage limit for decompression \n");
1670c16b537SWarner Losh DISPLAY( "-- : All arguments after \"--\" are treated as files \n");
1680c16b537SWarner Losh #ifndef ZSTD_NODICT
1690c16b537SWarner Losh DISPLAY( "\n");
1700c16b537SWarner Losh DISPLAY( "Dictionary builder : \n");
1710c16b537SWarner Losh DISPLAY( "--train ## : create a dictionary from a training set of files \n");
1720c16b537SWarner Losh DISPLAY( "--train-cover[=k=#,d=#,steps=#] : use the cover algorithm with optional args\n");
1730c16b537SWarner Losh DISPLAY( "--train-legacy[=s=#] : use the legacy algorithm with selectivity (default: %u)\n", g_defaultSelectivityLevel);
1740c16b537SWarner Losh DISPLAY( " -o file : `file` is dictionary name (default: %s) \n", g_defaultDictName);
1750c16b537SWarner Losh DISPLAY( "--maxdict=# : limit dictionary to specified size (default: %u) \n", g_defaultMaxDictSize);
1760c16b537SWarner Losh DISPLAY( "--dictID=# : force dictionary ID to specified value (default: random)\n");
1770c16b537SWarner Losh #endif
1780c16b537SWarner Losh #ifndef ZSTD_NOBENCH
1790c16b537SWarner Losh DISPLAY( "\n");
1800c16b537SWarner Losh DISPLAY( "Benchmark arguments : \n");
181*19fcbaf1SConrad Meyer DISPLAY( " -b# : benchmark file(s), using # compression level (default: %d) \n", ZSTDCLI_CLEVEL_DEFAULT);
1820c16b537SWarner Losh DISPLAY( " -e# : test all compression levels from -bX to # (default: 1)\n");
1830c16b537SWarner Losh DISPLAY( " -i# : minimum evaluation time in seconds (default: 3s) \n");
1840c16b537SWarner Losh DISPLAY( " -B# : cut file into independent blocks of size # (default: no block)\n");
1850c16b537SWarner Losh DISPLAY( "--priority=rt : set process priority to real-time \n");
1860c16b537SWarner Losh #endif
1870c16b537SWarner Losh return 0;
1880c16b537SWarner Losh }
1890c16b537SWarner Losh
badusage(const char * programName)1900c16b537SWarner Losh static int badusage(const char* programName)
1910c16b537SWarner Losh {
1920c16b537SWarner Losh DISPLAYLEVEL(1, "Incorrect parameters\n");
1930c16b537SWarner Losh if (g_displayLevel >= 2) usage(programName);
1940c16b537SWarner Losh return 1;
1950c16b537SWarner Losh }
1960c16b537SWarner Losh
waitEnter(void)1970c16b537SWarner Losh static void waitEnter(void)
1980c16b537SWarner Losh {
1990c16b537SWarner Losh int unused;
2000c16b537SWarner Losh DISPLAY("Press enter to continue...\n");
2010c16b537SWarner Losh unused = getchar();
2020c16b537SWarner Losh (void)unused;
2030c16b537SWarner Losh }
2040c16b537SWarner Losh
lastNameFromPath(const char * path)2050c16b537SWarner Losh static const char* lastNameFromPath(const char* path)
2060c16b537SWarner Losh {
2070c16b537SWarner Losh const char* name = path;
2080c16b537SWarner Losh if (strrchr(name, '/')) name = strrchr(name, '/') + 1;
2090c16b537SWarner Losh if (strrchr(name, '\\')) name = strrchr(name, '\\') + 1; /* windows */
2100c16b537SWarner Losh return name;
2110c16b537SWarner Losh }
2120c16b537SWarner Losh
2130c16b537SWarner Losh /*! exeNameMatch() :
2140c16b537SWarner Losh @return : a non-zero value if exeName matches test, excluding the extension
2150c16b537SWarner Losh */
exeNameMatch(const char * exeName,const char * test)2160c16b537SWarner Losh static int exeNameMatch(const char* exeName, const char* test)
2170c16b537SWarner Losh {
2180c16b537SWarner Losh return !strncmp(exeName, test, strlen(test)) &&
2190c16b537SWarner Losh (exeName[strlen(test)] == '\0' || exeName[strlen(test)] == '.');
2200c16b537SWarner Losh }
2210c16b537SWarner Losh
2220c16b537SWarner Losh /*! readU32FromChar() :
223*19fcbaf1SConrad Meyer * @return : unsigned integer value read from input in `char` format.
224*19fcbaf1SConrad Meyer * allows and interprets K, KB, KiB, M, MB and MiB suffix.
225*19fcbaf1SConrad Meyer * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
226*19fcbaf1SConrad Meyer * Note : function result can overflow if digit string > MAX_UINT */
readU32FromChar(const char ** stringPtr)2270c16b537SWarner Losh static unsigned readU32FromChar(const char** stringPtr)
2280c16b537SWarner Losh {
2290c16b537SWarner Losh unsigned result = 0;
2300c16b537SWarner Losh while ((**stringPtr >='0') && (**stringPtr <='9'))
2310c16b537SWarner Losh result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
2320c16b537SWarner Losh if ((**stringPtr=='K') || (**stringPtr=='M')) {
2330c16b537SWarner Losh result <<= 10;
2340c16b537SWarner Losh if (**stringPtr=='M') result <<= 10;
2350c16b537SWarner Losh (*stringPtr)++ ;
2360c16b537SWarner Losh if (**stringPtr=='i') (*stringPtr)++;
2370c16b537SWarner Losh if (**stringPtr=='B') (*stringPtr)++;
2380c16b537SWarner Losh }
2390c16b537SWarner Losh return result;
2400c16b537SWarner Losh }
2410c16b537SWarner Losh
2420c16b537SWarner Losh /** longCommandWArg() :
2430c16b537SWarner Losh * check if *stringPtr is the same as longCommand.
2440c16b537SWarner Losh * If yes, @return 1 and advances *stringPtr to the position which immediately follows longCommand.
2450c16b537SWarner Losh * @return 0 and doesn't modify *stringPtr otherwise.
2460c16b537SWarner Losh */
longCommandWArg(const char ** stringPtr,const char * longCommand)2470c16b537SWarner Losh static unsigned longCommandWArg(const char** stringPtr, const char* longCommand)
2480c16b537SWarner Losh {
2490c16b537SWarner Losh size_t const comSize = strlen(longCommand);
2500c16b537SWarner Losh int const result = !strncmp(*stringPtr, longCommand, comSize);
2510c16b537SWarner Losh if (result) *stringPtr += comSize;
2520c16b537SWarner Losh return result;
2530c16b537SWarner Losh }
2540c16b537SWarner Losh
2550c16b537SWarner Losh
2560c16b537SWarner Losh #ifndef ZSTD_NODICT
2570c16b537SWarner Losh /**
2580c16b537SWarner Losh * parseCoverParameters() :
2590c16b537SWarner Losh * reads cover parameters from *stringPtr (e.g. "--train-cover=k=48,d=8,steps=32") into *params
2600c16b537SWarner Losh * @return 1 means that cover parameters were correct
2610c16b537SWarner Losh * @return 0 in case of malformed parameters
2620c16b537SWarner Losh */
parseCoverParameters(const char * stringPtr,ZDICT_cover_params_t * params)2630c16b537SWarner Losh static unsigned parseCoverParameters(const char* stringPtr, ZDICT_cover_params_t* params)
2640c16b537SWarner Losh {
2650c16b537SWarner Losh memset(params, 0, sizeof(*params));
2660c16b537SWarner Losh for (; ;) {
2670c16b537SWarner Losh if (longCommandWArg(&stringPtr, "k=")) { params->k = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
2680c16b537SWarner Losh if (longCommandWArg(&stringPtr, "d=")) { params->d = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
2690c16b537SWarner Losh if (longCommandWArg(&stringPtr, "steps=")) { params->steps = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
2700c16b537SWarner Losh return 0;
2710c16b537SWarner Losh }
2720c16b537SWarner Losh if (stringPtr[0] != 0) return 0;
2730c16b537SWarner Losh DISPLAYLEVEL(4, "cover: k=%u\nd=%u\nsteps=%u\n", params->k, params->d, params->steps);
2740c16b537SWarner Losh return 1;
2750c16b537SWarner Losh }
2760c16b537SWarner Losh
2770c16b537SWarner Losh /**
2780c16b537SWarner Losh * parseLegacyParameters() :
2790c16b537SWarner Losh * reads legacy dictioanry builter parameters from *stringPtr (e.g. "--train-legacy=selectivity=8") into *selectivity
2800c16b537SWarner Losh * @return 1 means that legacy dictionary builder parameters were correct
2810c16b537SWarner Losh * @return 0 in case of malformed parameters
2820c16b537SWarner Losh */
parseLegacyParameters(const char * stringPtr,unsigned * selectivity)2830c16b537SWarner Losh static unsigned parseLegacyParameters(const char* stringPtr, unsigned* selectivity)
2840c16b537SWarner Losh {
2850c16b537SWarner Losh if (!longCommandWArg(&stringPtr, "s=") && !longCommandWArg(&stringPtr, "selectivity=")) { return 0; }
2860c16b537SWarner Losh *selectivity = readU32FromChar(&stringPtr);
2870c16b537SWarner Losh if (stringPtr[0] != 0) return 0;
2880c16b537SWarner Losh DISPLAYLEVEL(4, "legacy: selectivity=%u\n", *selectivity);
2890c16b537SWarner Losh return 1;
2900c16b537SWarner Losh }
2910c16b537SWarner Losh
defaultCoverParams(void)2920c16b537SWarner Losh static ZDICT_cover_params_t defaultCoverParams(void)
2930c16b537SWarner Losh {
2940c16b537SWarner Losh ZDICT_cover_params_t params;
2950c16b537SWarner Losh memset(¶ms, 0, sizeof(params));
2960c16b537SWarner Losh params.d = 8;
2970c16b537SWarner Losh params.steps = 4;
2980c16b537SWarner Losh return params;
2990c16b537SWarner Losh }
3000c16b537SWarner Losh #endif
3010c16b537SWarner Losh
3020c16b537SWarner Losh
3030c16b537SWarner Losh /** parseCompressionParameters() :
3040c16b537SWarner Losh * reads compression parameters from *stringPtr (e.g. "--zstd=wlog=23,clog=23,hlog=22,slog=6,slen=3,tlen=48,strat=6") into *params
3050c16b537SWarner Losh * @return 1 means that compression parameters were correct
3060c16b537SWarner Losh * @return 0 in case of malformed parameters
3070c16b537SWarner Losh */
parseCompressionParameters(const char * stringPtr,ZSTD_compressionParameters * params)3080c16b537SWarner Losh static unsigned parseCompressionParameters(const char* stringPtr, ZSTD_compressionParameters* params)
3090c16b537SWarner Losh {
3100c16b537SWarner Losh for ( ; ;) {
3110c16b537SWarner Losh if (longCommandWArg(&stringPtr, "windowLog=") || longCommandWArg(&stringPtr, "wlog=")) { params->windowLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
3120c16b537SWarner Losh if (longCommandWArg(&stringPtr, "chainLog=") || longCommandWArg(&stringPtr, "clog=")) { params->chainLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
3130c16b537SWarner Losh if (longCommandWArg(&stringPtr, "hashLog=") || longCommandWArg(&stringPtr, "hlog=")) { params->hashLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
3140c16b537SWarner Losh if (longCommandWArg(&stringPtr, "searchLog=") || longCommandWArg(&stringPtr, "slog=")) { params->searchLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
3150c16b537SWarner Losh if (longCommandWArg(&stringPtr, "searchLength=") || longCommandWArg(&stringPtr, "slen=")) { params->searchLength = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
3160c16b537SWarner Losh if (longCommandWArg(&stringPtr, "targetLength=") || longCommandWArg(&stringPtr, "tlen=")) { params->targetLength = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
3170c16b537SWarner Losh if (longCommandWArg(&stringPtr, "strategy=") || longCommandWArg(&stringPtr, "strat=")) { params->strategy = (ZSTD_strategy)(readU32FromChar(&stringPtr)); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
3180c16b537SWarner Losh if (longCommandWArg(&stringPtr, "overlapLog=") || longCommandWArg(&stringPtr, "ovlog=")) { g_overlapLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
3190c16b537SWarner Losh if (longCommandWArg(&stringPtr, "ldmHashLog=") || longCommandWArg(&stringPtr, "ldmhlog=")) { g_ldmHashLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
3200c16b537SWarner Losh if (longCommandWArg(&stringPtr, "ldmSearchLength=") || longCommandWArg(&stringPtr, "ldmslen=")) { g_ldmMinMatch = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
3210c16b537SWarner Losh if (longCommandWArg(&stringPtr, "ldmBucketSizeLog=") || longCommandWArg(&stringPtr, "ldmblog=")) { g_ldmBucketSizeLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
3220c16b537SWarner Losh if (longCommandWArg(&stringPtr, "ldmHashEveryLog=") || longCommandWArg(&stringPtr, "ldmhevery=")) { g_ldmHashEveryLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
323*19fcbaf1SConrad Meyer DISPLAYLEVEL(4, "invalid compression parameter \n");
3240c16b537SWarner Losh return 0;
3250c16b537SWarner Losh }
3260c16b537SWarner Losh
327*19fcbaf1SConrad Meyer DISPLAYLEVEL(4, "windowLog=%d, chainLog=%d, hashLog=%d, searchLog=%d \n", params->windowLog, params->chainLog, params->hashLog, params->searchLog);
328*19fcbaf1SConrad Meyer DISPLAYLEVEL(4, "searchLength=%d, targetLength=%d, strategy=%d \n", params->searchLength, params->targetLength, params->strategy);
3290c16b537SWarner Losh if (stringPtr[0] != 0) return 0; /* check the end of string */
3300c16b537SWarner Losh return 1;
3310c16b537SWarner Losh }
3320c16b537SWarner Losh
printVersion(void)3330c16b537SWarner Losh static void printVersion(void)
3340c16b537SWarner Losh {
3350c16b537SWarner Losh DISPLAY(WELCOME_MESSAGE);
3360c16b537SWarner Losh /* format support */
3370c16b537SWarner Losh DISPLAYLEVEL(3, "*** supports: zstd");
3380c16b537SWarner Losh #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>0) && (ZSTD_LEGACY_SUPPORT<8)
3390c16b537SWarner Losh DISPLAYLEVEL(3, ", zstd legacy v0.%d+", ZSTD_LEGACY_SUPPORT);
3400c16b537SWarner Losh #endif
3410c16b537SWarner Losh #ifdef ZSTD_GZCOMPRESS
3420c16b537SWarner Losh DISPLAYLEVEL(3, ", gzip");
3430c16b537SWarner Losh #endif
3440c16b537SWarner Losh #ifdef ZSTD_LZ4COMPRESS
3450c16b537SWarner Losh DISPLAYLEVEL(3, ", lz4");
3460c16b537SWarner Losh #endif
3470c16b537SWarner Losh #ifdef ZSTD_LZMACOMPRESS
3480c16b537SWarner Losh DISPLAYLEVEL(3, ", lzma, xz ");
3490c16b537SWarner Losh #endif
3500c16b537SWarner Losh DISPLAYLEVEL(3, "\n");
3510c16b537SWarner Losh /* posix support */
3520c16b537SWarner Losh #ifdef _POSIX_C_SOURCE
3530c16b537SWarner Losh DISPLAYLEVEL(4, "_POSIX_C_SOURCE defined: %ldL\n", (long) _POSIX_C_SOURCE);
3540c16b537SWarner Losh #endif
3550c16b537SWarner Losh #ifdef _POSIX_VERSION
3560c16b537SWarner Losh DISPLAYLEVEL(4, "_POSIX_VERSION defined: %ldL \n", (long) _POSIX_VERSION);
3570c16b537SWarner Losh #endif
3580c16b537SWarner Losh #ifdef PLATFORM_POSIX_VERSION
3590c16b537SWarner Losh DISPLAYLEVEL(4, "PLATFORM_POSIX_VERSION defined: %ldL\n", (long) PLATFORM_POSIX_VERSION);
3600c16b537SWarner Losh #endif
3610c16b537SWarner Losh }
3620c16b537SWarner Losh
3630c16b537SWarner Losh typedef enum { zom_compress, zom_decompress, zom_test, zom_bench, zom_train, zom_list } zstd_operation_mode;
3640c16b537SWarner Losh
3650c16b537SWarner Losh #define CLEAN_RETURN(i) { operationResult = (i); goto _end; }
3660c16b537SWarner Losh
main(int argCount,const char * argv[])3670c16b537SWarner Losh int main(int argCount, const char* argv[])
3680c16b537SWarner Losh {
3690c16b537SWarner Losh int argNb,
3700c16b537SWarner Losh followLinks = 0,
371*19fcbaf1SConrad Meyer forceStdout = 0,
372*19fcbaf1SConrad Meyer lastCommand = 0,
373*19fcbaf1SConrad Meyer ldmFlag = 0,
3740c16b537SWarner Losh main_pause = 0,
375*19fcbaf1SConrad Meyer nbWorkers = 0,
3760c16b537SWarner Losh nextArgumentIsOutFileName = 0,
3770c16b537SWarner Losh nextArgumentIsMaxDict = 0,
3780c16b537SWarner Losh nextArgumentIsDictID = 0,
3790c16b537SWarner Losh nextArgumentsAreFiles = 0,
380*19fcbaf1SConrad Meyer nextEntryIsDictionary = 0,
381*19fcbaf1SConrad Meyer operationResult = 0,
382052d3c12SConrad Meyer separateFiles = 0,
383*19fcbaf1SConrad Meyer setRealTimePrio = 0,
384*19fcbaf1SConrad Meyer singleThread = 0,
385*19fcbaf1SConrad Meyer ultra=0;
3860c16b537SWarner Losh unsigned bench_nbSeconds = 3; /* would be better if this value was synchronized from bench */
3870c16b537SWarner Losh size_t blockSize = 0;
3880c16b537SWarner Losh zstd_operation_mode operation = zom_compress;
3890c16b537SWarner Losh ZSTD_compressionParameters compressionParams;
3900c16b537SWarner Losh int cLevel = ZSTDCLI_CLEVEL_DEFAULT;
391*19fcbaf1SConrad Meyer int cLevelLast = -1000000000;
3920c16b537SWarner Losh unsigned recursive = 0;
3930c16b537SWarner Losh unsigned memLimit = 0;
3940c16b537SWarner Losh const char** filenameTable = (const char**)malloc(argCount * sizeof(const char*)); /* argCount >= 1 */
3950c16b537SWarner Losh unsigned filenameIdx = 0;
3960c16b537SWarner Losh const char* programName = argv[0];
3970c16b537SWarner Losh const char* outFileName = NULL;
3980c16b537SWarner Losh const char* dictFileName = NULL;
3990c16b537SWarner Losh const char* suffix = ZSTD_EXTENSION;
4000c16b537SWarner Losh unsigned maxDictSize = g_defaultMaxDictSize;
4010c16b537SWarner Losh unsigned dictID = 0;
4020c16b537SWarner Losh int dictCLevel = g_defaultDictCLevel;
4030c16b537SWarner Losh unsigned dictSelect = g_defaultSelectivityLevel;
4040c16b537SWarner Losh #ifdef UTIL_HAS_CREATEFILELIST
4050c16b537SWarner Losh const char** extendedFileList = NULL;
4060c16b537SWarner Losh char* fileNamesBuf = NULL;
4070c16b537SWarner Losh unsigned fileNamesNb;
4080c16b537SWarner Losh #endif
4090c16b537SWarner Losh #ifndef ZSTD_NODICT
4100c16b537SWarner Losh ZDICT_cover_params_t coverParams = defaultCoverParams();
4110c16b537SWarner Losh int cover = 1;
4120c16b537SWarner Losh #endif
4130c16b537SWarner Losh
4140c16b537SWarner Losh
4150c16b537SWarner Losh /* init */
4160c16b537SWarner Losh (void)recursive; (void)cLevelLast; /* not used when ZSTD_NOBENCH set */
4170c16b537SWarner Losh (void)dictCLevel; (void)dictSelect; (void)dictID; (void)maxDictSize; /* not used when ZSTD_NODICT set */
4180c16b537SWarner Losh (void)ultra; (void)cLevel; (void)ldmFlag; /* not used when ZSTD_NOCOMPRESS set */
4190c16b537SWarner Losh (void)memLimit; /* not used when ZSTD_NODECOMPRESS set */
4200c16b537SWarner Losh if (filenameTable==NULL) { DISPLAY("zstd: %s \n", strerror(errno)); exit(1); }
4210c16b537SWarner Losh filenameTable[0] = stdinmark;
4220c16b537SWarner Losh g_displayOut = stderr;
4230c16b537SWarner Losh programName = lastNameFromPath(programName);
424*19fcbaf1SConrad Meyer #ifdef ZSTD_MULTITHREAD
425*19fcbaf1SConrad Meyer nbWorkers = 1;
426*19fcbaf1SConrad Meyer #endif
4270c16b537SWarner Losh
4280c16b537SWarner Losh /* preset behaviors */
429*19fcbaf1SConrad Meyer if (exeNameMatch(programName, ZSTD_ZSTDMT)) nbWorkers=0;
4300c16b537SWarner Losh if (exeNameMatch(programName, ZSTD_UNZSTD)) operation=zom_decompress;
431*19fcbaf1SConrad Meyer if (exeNameMatch(programName, ZSTD_CAT)) { operation=zom_decompress; forceStdout=1; FIO_overwriteMode(); outFileName=stdoutmark; g_displayLevel=1; } /* supports multiple formats */
432*19fcbaf1SConrad Meyer if (exeNameMatch(programName, ZSTD_ZCAT)) { operation=zom_decompress; forceStdout=1; FIO_overwriteMode(); outFileName=stdoutmark; g_displayLevel=1; } /* behave like zcat, also supports multiple formats */
4330c16b537SWarner Losh if (exeNameMatch(programName, ZSTD_GZ)) { suffix = GZ_EXTENSION; FIO_setCompressionType(FIO_gzipCompression); FIO_setRemoveSrcFile(1); } /* behave like gzip */
434*19fcbaf1SConrad Meyer if (exeNameMatch(programName, ZSTD_GUNZIP)) { operation=zom_decompress; FIO_setRemoveSrcFile(1); } /* behave like gunzip, also supports multiple formats */
435*19fcbaf1SConrad Meyer if (exeNameMatch(programName, ZSTD_GZCAT)) { operation=zom_decompress; forceStdout=1; FIO_overwriteMode(); outFileName=stdoutmark; g_displayLevel=1; } /* behave like gzcat, also supports multiple formats */
4360c16b537SWarner Losh if (exeNameMatch(programName, ZSTD_LZMA)) { suffix = LZMA_EXTENSION; FIO_setCompressionType(FIO_lzmaCompression); FIO_setRemoveSrcFile(1); } /* behave like lzma */
437*19fcbaf1SConrad Meyer if (exeNameMatch(programName, ZSTD_UNLZMA)) { operation=zom_decompress; FIO_setCompressionType(FIO_lzmaCompression); FIO_setRemoveSrcFile(1); } /* behave like unlzma, also supports multiple formats */
4380c16b537SWarner Losh if (exeNameMatch(programName, ZSTD_XZ)) { suffix = XZ_EXTENSION; FIO_setCompressionType(FIO_xzCompression); FIO_setRemoveSrcFile(1); } /* behave like xz */
439*19fcbaf1SConrad Meyer if (exeNameMatch(programName, ZSTD_UNXZ)) { operation=zom_decompress; FIO_setCompressionType(FIO_xzCompression); FIO_setRemoveSrcFile(1); } /* behave like unxz, also supports multiple formats */
440*19fcbaf1SConrad Meyer if (exeNameMatch(programName, ZSTD_LZ4)) { suffix = LZ4_EXTENSION; FIO_setCompressionType(FIO_lz4Compression); } /* behave like lz4 */
441*19fcbaf1SConrad Meyer if (exeNameMatch(programName, ZSTD_UNLZ4)) { operation=zom_decompress; FIO_setCompressionType(FIO_lz4Compression); } /* behave like unlz4, also supports multiple formats */
4420c16b537SWarner Losh memset(&compressionParams, 0, sizeof(compressionParams));
4430c16b537SWarner Losh
4440c16b537SWarner Losh /* command switches */
4450c16b537SWarner Losh for (argNb=1; argNb<argCount; argNb++) {
4460c16b537SWarner Losh const char* argument = argv[argNb];
4470c16b537SWarner Losh if(!argument) continue; /* Protection if argument empty */
4480c16b537SWarner Losh
4490c16b537SWarner Losh if (nextArgumentsAreFiles==0) {
4500c16b537SWarner Losh /* "-" means stdin/stdout */
4510c16b537SWarner Losh if (!strcmp(argument, "-")){
4520c16b537SWarner Losh if (!filenameIdx) {
4530c16b537SWarner Losh filenameIdx=1, filenameTable[0]=stdinmark;
4540c16b537SWarner Losh outFileName=stdoutmark;
4550c16b537SWarner Losh g_displayLevel-=(g_displayLevel==2);
4560c16b537SWarner Losh continue;
4570c16b537SWarner Losh } }
4580c16b537SWarner Losh
4590c16b537SWarner Losh /* Decode commands (note : aggregated commands are allowed) */
4600c16b537SWarner Losh if (argument[0]=='-') {
4610c16b537SWarner Losh
4620c16b537SWarner Losh if (argument[1]=='-') {
4630c16b537SWarner Losh /* long commands (--long-word) */
4640c16b537SWarner Losh if (!strcmp(argument, "--")) { nextArgumentsAreFiles=1; continue; } /* only file names allowed from now on */
4650c16b537SWarner Losh if (!strcmp(argument, "--list")) { operation=zom_list; continue; }
4660c16b537SWarner Losh if (!strcmp(argument, "--compress")) { operation=zom_compress; continue; }
4670c16b537SWarner Losh if (!strcmp(argument, "--decompress")) { operation=zom_decompress; continue; }
4680c16b537SWarner Losh if (!strcmp(argument, "--uncompress")) { operation=zom_decompress; continue; }
4690c16b537SWarner Losh if (!strcmp(argument, "--force")) { FIO_overwriteMode(); forceStdout=1; followLinks=1; continue; }
4700c16b537SWarner Losh if (!strcmp(argument, "--version")) { g_displayOut=stdout; DISPLAY(WELCOME_MESSAGE); CLEAN_RETURN(0); }
4710c16b537SWarner Losh if (!strcmp(argument, "--help")) { g_displayOut=stdout; CLEAN_RETURN(usage_advanced(programName)); }
4720c16b537SWarner Losh if (!strcmp(argument, "--verbose")) { g_displayLevel++; continue; }
4730c16b537SWarner Losh if (!strcmp(argument, "--quiet")) { g_displayLevel--; continue; }
4740c16b537SWarner Losh if (!strcmp(argument, "--stdout")) { forceStdout=1; outFileName=stdoutmark; g_displayLevel-=(g_displayLevel==2); continue; }
4750c16b537SWarner Losh if (!strcmp(argument, "--ultra")) { ultra=1; continue; }
4760c16b537SWarner Losh if (!strcmp(argument, "--check")) { FIO_setChecksumFlag(2); continue; }
4770c16b537SWarner Losh if (!strcmp(argument, "--no-check")) { FIO_setChecksumFlag(0); continue; }
4780c16b537SWarner Losh if (!strcmp(argument, "--sparse")) { FIO_setSparseWrite(2); continue; }
4790c16b537SWarner Losh if (!strcmp(argument, "--no-sparse")) { FIO_setSparseWrite(0); continue; }
4800c16b537SWarner Losh if (!strcmp(argument, "--test")) { operation=zom_test; continue; }
4810c16b537SWarner Losh if (!strcmp(argument, "--train")) { operation=zom_train; outFileName=g_defaultDictName; continue; }
4820c16b537SWarner Losh if (!strcmp(argument, "--maxdict")) { nextArgumentIsMaxDict=1; lastCommand=1; continue; } /* kept available for compatibility with old syntax ; will be removed one day */
4830c16b537SWarner Losh if (!strcmp(argument, "--dictID")) { nextArgumentIsDictID=1; lastCommand=1; continue; } /* kept available for compatibility with old syntax ; will be removed one day */
4840c16b537SWarner Losh if (!strcmp(argument, "--no-dictID")) { FIO_setDictIDFlag(0); continue; }
4850c16b537SWarner Losh if (!strcmp(argument, "--keep")) { FIO_setRemoveSrcFile(0); continue; }
4860c16b537SWarner Losh if (!strcmp(argument, "--rm")) { FIO_setRemoveSrcFile(1); continue; }
4870c16b537SWarner Losh if (!strcmp(argument, "--priority=rt")) { setRealTimePrio = 1; continue; }
488*19fcbaf1SConrad Meyer if (!strcmp(argument, "--single-thread")) { nbWorkers = 0; singleThread = 1; continue; }
4890c16b537SWarner Losh #ifdef ZSTD_GZCOMPRESS
4900c16b537SWarner Losh if (!strcmp(argument, "--format=gzip")) { suffix = GZ_EXTENSION; FIO_setCompressionType(FIO_gzipCompression); continue; }
4910c16b537SWarner Losh #endif
4920c16b537SWarner Losh #ifdef ZSTD_LZMACOMPRESS
4930c16b537SWarner Losh if (!strcmp(argument, "--format=lzma")) { suffix = LZMA_EXTENSION; FIO_setCompressionType(FIO_lzmaCompression); continue; }
4940c16b537SWarner Losh if (!strcmp(argument, "--format=xz")) { suffix = XZ_EXTENSION; FIO_setCompressionType(FIO_xzCompression); continue; }
4950c16b537SWarner Losh #endif
4960c16b537SWarner Losh #ifdef ZSTD_LZ4COMPRESS
4970c16b537SWarner Losh if (!strcmp(argument, "--format=lz4")) { suffix = LZ4_EXTENSION; FIO_setCompressionType(FIO_lz4Compression); continue; }
4980c16b537SWarner Losh #endif
4990c16b537SWarner Losh
5000c16b537SWarner Losh /* long commands with arguments */
5010c16b537SWarner Losh #ifndef ZSTD_NODICT
5020c16b537SWarner Losh if (longCommandWArg(&argument, "--train-cover")) {
5030c16b537SWarner Losh operation = zom_train;
5040c16b537SWarner Losh outFileName = g_defaultDictName;
5050c16b537SWarner Losh cover = 1;
5060c16b537SWarner Losh /* Allow optional arguments following an = */
5070c16b537SWarner Losh if (*argument == 0) { memset(&coverParams, 0, sizeof(coverParams)); }
5080c16b537SWarner Losh else if (*argument++ != '=') { CLEAN_RETURN(badusage(programName)); }
5090c16b537SWarner Losh else if (!parseCoverParameters(argument, &coverParams)) { CLEAN_RETURN(badusage(programName)); }
5100c16b537SWarner Losh continue;
5110c16b537SWarner Losh }
5120c16b537SWarner Losh if (longCommandWArg(&argument, "--train-legacy")) {
5130c16b537SWarner Losh operation = zom_train;
5140c16b537SWarner Losh outFileName = g_defaultDictName;
5150c16b537SWarner Losh cover = 0;
5160c16b537SWarner Losh /* Allow optional arguments following an = */
5170c16b537SWarner Losh if (*argument == 0) { continue; }
5180c16b537SWarner Losh else if (*argument++ != '=') { CLEAN_RETURN(badusage(programName)); }
5190c16b537SWarner Losh else if (!parseLegacyParameters(argument, &dictSelect)) { CLEAN_RETURN(badusage(programName)); }
5200c16b537SWarner Losh continue;
5210c16b537SWarner Losh }
5220c16b537SWarner Losh #endif
523*19fcbaf1SConrad Meyer if (longCommandWArg(&argument, "--threads=")) { nbWorkers = readU32FromChar(&argument); continue; }
5240c16b537SWarner Losh if (longCommandWArg(&argument, "--memlimit=")) { memLimit = readU32FromChar(&argument); continue; }
5250c16b537SWarner Losh if (longCommandWArg(&argument, "--memory=")) { memLimit = readU32FromChar(&argument); continue; }
5260c16b537SWarner Losh if (longCommandWArg(&argument, "--memlimit-decompress=")) { memLimit = readU32FromChar(&argument); continue; }
5270c16b537SWarner Losh if (longCommandWArg(&argument, "--block-size=")) { blockSize = readU32FromChar(&argument); continue; }
5280c16b537SWarner Losh if (longCommandWArg(&argument, "--maxdict=")) { maxDictSize = readU32FromChar(&argument); continue; }
5290c16b537SWarner Losh if (longCommandWArg(&argument, "--dictID=")) { dictID = readU32FromChar(&argument); continue; }
5300c16b537SWarner Losh if (longCommandWArg(&argument, "--zstd=")) { if (!parseCompressionParameters(argument, &compressionParams)) CLEAN_RETURN(badusage(programName)); continue; }
5310c16b537SWarner Losh if (longCommandWArg(&argument, "--long")) {
5320c16b537SWarner Losh unsigned ldmWindowLog = 0;
5330c16b537SWarner Losh ldmFlag = 1;
5340c16b537SWarner Losh /* Parse optional window log */
5350c16b537SWarner Losh if (*argument == '=') {
5360c16b537SWarner Losh ++argument;
5370c16b537SWarner Losh ldmWindowLog = readU32FromChar(&argument);
5380c16b537SWarner Losh } else if (*argument != 0) {
5390c16b537SWarner Losh /* Invalid character following --long */
5400c16b537SWarner Losh CLEAN_RETURN(badusage(programName));
5410c16b537SWarner Losh }
5420c16b537SWarner Losh /* Only set windowLog if not already set by --zstd */
5430c16b537SWarner Losh if (compressionParams.windowLog == 0)
5440c16b537SWarner Losh compressionParams.windowLog = ldmWindowLog;
5450c16b537SWarner Losh continue;
5460c16b537SWarner Losh }
547*19fcbaf1SConrad Meyer if (longCommandWArg(&argument, "--fast")) {
548*19fcbaf1SConrad Meyer /* Parse optional window log */
549*19fcbaf1SConrad Meyer if (*argument == '=') {
550*19fcbaf1SConrad Meyer U32 fastLevel;
551*19fcbaf1SConrad Meyer ++argument;
552*19fcbaf1SConrad Meyer fastLevel = readU32FromChar(&argument);
553*19fcbaf1SConrad Meyer if (fastLevel) cLevel = - (int)fastLevel;
554*19fcbaf1SConrad Meyer } else if (*argument != 0) {
555*19fcbaf1SConrad Meyer /* Invalid character following --fast */
556*19fcbaf1SConrad Meyer CLEAN_RETURN(badusage(programName));
557*19fcbaf1SConrad Meyer } else {
558*19fcbaf1SConrad Meyer cLevel = -1; /* default for --fast */
559*19fcbaf1SConrad Meyer }
560*19fcbaf1SConrad Meyer continue;
561*19fcbaf1SConrad Meyer }
5620c16b537SWarner Losh /* fall-through, will trigger bad_usage() later on */
5630c16b537SWarner Losh }
5640c16b537SWarner Losh
5650c16b537SWarner Losh argument++;
5660c16b537SWarner Losh while (argument[0]!=0) {
5670c16b537SWarner Losh if (lastCommand) {
5680c16b537SWarner Losh DISPLAY("error : command must be followed by argument \n");
5690c16b537SWarner Losh CLEAN_RETURN(1);
5700c16b537SWarner Losh }
5710c16b537SWarner Losh #ifndef ZSTD_NOCOMPRESS
5720c16b537SWarner Losh /* compression Level */
5730c16b537SWarner Losh if ((*argument>='0') && (*argument<='9')) {
5740c16b537SWarner Losh dictCLevel = cLevel = readU32FromChar(&argument);
5750c16b537SWarner Losh continue;
5760c16b537SWarner Losh }
5770c16b537SWarner Losh #endif
5780c16b537SWarner Losh
5790c16b537SWarner Losh switch(argument[0])
5800c16b537SWarner Losh {
5810c16b537SWarner Losh /* Display help */
5820c16b537SWarner Losh case 'V': g_displayOut=stdout; printVersion(); CLEAN_RETURN(0); /* Version Only */
5830c16b537SWarner Losh case 'H':
5840c16b537SWarner Losh case 'h': g_displayOut=stdout; CLEAN_RETURN(usage_advanced(programName));
5850c16b537SWarner Losh
5860c16b537SWarner Losh /* Compress */
5870c16b537SWarner Losh case 'z': operation=zom_compress; argument++; break;
5880c16b537SWarner Losh
5890c16b537SWarner Losh /* Decoding */
5900c16b537SWarner Losh case 'd':
5910c16b537SWarner Losh #ifndef ZSTD_NOBENCH
592*19fcbaf1SConrad Meyer BMK_setDecodeOnlyMode(1);
593*19fcbaf1SConrad Meyer if (operation==zom_bench) { argument++; break; } /* benchmark decode (hidden option) */
5940c16b537SWarner Losh #endif
5950c16b537SWarner Losh operation=zom_decompress; argument++; break;
5960c16b537SWarner Losh
5970c16b537SWarner Losh /* Force stdout, even if stdout==console */
5980c16b537SWarner Losh case 'c': forceStdout=1; outFileName=stdoutmark; argument++; break;
5990c16b537SWarner Losh
6000c16b537SWarner Losh /* Use file content as dictionary */
6010c16b537SWarner Losh case 'D': nextEntryIsDictionary = 1; lastCommand = 1; argument++; break;
6020c16b537SWarner Losh
6030c16b537SWarner Losh /* Overwrite */
6040c16b537SWarner Losh case 'f': FIO_overwriteMode(); forceStdout=1; followLinks=1; argument++; break;
6050c16b537SWarner Losh
6060c16b537SWarner Losh /* Verbose mode */
6070c16b537SWarner Losh case 'v': g_displayLevel++; argument++; break;
6080c16b537SWarner Losh
6090c16b537SWarner Losh /* Quiet mode */
6100c16b537SWarner Losh case 'q': g_displayLevel--; argument++; break;
6110c16b537SWarner Losh
6120c16b537SWarner Losh /* keep source file (default) */
6130c16b537SWarner Losh case 'k': FIO_setRemoveSrcFile(0); argument++; break;
6140c16b537SWarner Losh
6150c16b537SWarner Losh /* Checksum */
6160c16b537SWarner Losh case 'C': FIO_setChecksumFlag(2); argument++; break;
6170c16b537SWarner Losh
6180c16b537SWarner Losh /* test compressed file */
6190c16b537SWarner Losh case 't': operation=zom_test; argument++; break;
6200c16b537SWarner Losh
6210c16b537SWarner Losh /* destination file name */
6220c16b537SWarner Losh case 'o': nextArgumentIsOutFileName=1; lastCommand=1; argument++; break;
6230c16b537SWarner Losh
6240c16b537SWarner Losh /* limit decompression memory */
6250c16b537SWarner Losh case 'M':
6260c16b537SWarner Losh argument++;
6270c16b537SWarner Losh memLimit = readU32FromChar(&argument);
6280c16b537SWarner Losh break;
6290c16b537SWarner Losh case 'l': operation=zom_list; argument++; break;
6300c16b537SWarner Losh #ifdef UTIL_HAS_CREATEFILELIST
6310c16b537SWarner Losh /* recursive */
6320c16b537SWarner Losh case 'r': recursive=1; argument++; break;
6330c16b537SWarner Losh #endif
6340c16b537SWarner Losh
6350c16b537SWarner Losh #ifndef ZSTD_NOBENCH
6360c16b537SWarner Losh /* Benchmark */
6370c16b537SWarner Losh case 'b':
6380c16b537SWarner Losh operation=zom_bench;
6390c16b537SWarner Losh argument++;
6400c16b537SWarner Losh break;
6410c16b537SWarner Losh
6420c16b537SWarner Losh /* range bench (benchmark only) */
6430c16b537SWarner Losh case 'e':
6440c16b537SWarner Losh /* compression Level */
6450c16b537SWarner Losh argument++;
6460c16b537SWarner Losh cLevelLast = readU32FromChar(&argument);
6470c16b537SWarner Losh break;
6480c16b537SWarner Losh
6490c16b537SWarner Losh /* Modify Nb Iterations (benchmark only) */
6500c16b537SWarner Losh case 'i':
6510c16b537SWarner Losh argument++;
6520c16b537SWarner Losh bench_nbSeconds = readU32FromChar(&argument);
6530c16b537SWarner Losh break;
6540c16b537SWarner Losh
6550c16b537SWarner Losh /* cut input into blocks (benchmark only) */
6560c16b537SWarner Losh case 'B':
6570c16b537SWarner Losh argument++;
6580c16b537SWarner Losh blockSize = readU32FromChar(&argument);
6590c16b537SWarner Losh break;
6600c16b537SWarner Losh
661052d3c12SConrad Meyer /* benchmark files separately (hidden option) */
662052d3c12SConrad Meyer case 'S':
663052d3c12SConrad Meyer argument++;
664052d3c12SConrad Meyer separateFiles = 1;
665052d3c12SConrad Meyer break;
666052d3c12SConrad Meyer
6670c16b537SWarner Losh #endif /* ZSTD_NOBENCH */
6680c16b537SWarner Losh
6690c16b537SWarner Losh /* nb of threads (hidden option) */
6700c16b537SWarner Losh case 'T':
6710c16b537SWarner Losh argument++;
672*19fcbaf1SConrad Meyer nbWorkers = readU32FromChar(&argument);
6730c16b537SWarner Losh break;
6740c16b537SWarner Losh
6750c16b537SWarner Losh /* Dictionary Selection level */
6760c16b537SWarner Losh case 's':
6770c16b537SWarner Losh argument++;
6780c16b537SWarner Losh dictSelect = readU32FromChar(&argument);
6790c16b537SWarner Losh break;
6800c16b537SWarner Losh
6810c16b537SWarner Losh /* Pause at the end (-p) or set an additional param (-p#) (hidden option) */
6820c16b537SWarner Losh case 'p': argument++;
6830c16b537SWarner Losh #ifndef ZSTD_NOBENCH
6840c16b537SWarner Losh if ((*argument>='0') && (*argument<='9')) {
6850c16b537SWarner Losh BMK_setAdditionalParam(readU32FromChar(&argument));
6860c16b537SWarner Losh } else
6870c16b537SWarner Losh #endif
6880c16b537SWarner Losh main_pause=1;
6890c16b537SWarner Losh break;
6900c16b537SWarner Losh /* unknown command */
6910c16b537SWarner Losh default : CLEAN_RETURN(badusage(programName));
6920c16b537SWarner Losh }
6930c16b537SWarner Losh }
6940c16b537SWarner Losh continue;
6950c16b537SWarner Losh } /* if (argument[0]=='-') */
6960c16b537SWarner Losh
6970c16b537SWarner Losh if (nextArgumentIsMaxDict) { /* kept available for compatibility with old syntax ; will be removed one day */
6980c16b537SWarner Losh nextArgumentIsMaxDict = 0;
6990c16b537SWarner Losh lastCommand = 0;
7000c16b537SWarner Losh maxDictSize = readU32FromChar(&argument);
7010c16b537SWarner Losh continue;
7020c16b537SWarner Losh }
7030c16b537SWarner Losh
7040c16b537SWarner Losh if (nextArgumentIsDictID) { /* kept available for compatibility with old syntax ; will be removed one day */
7050c16b537SWarner Losh nextArgumentIsDictID = 0;
7060c16b537SWarner Losh lastCommand = 0;
7070c16b537SWarner Losh dictID = readU32FromChar(&argument);
7080c16b537SWarner Losh continue;
7090c16b537SWarner Losh }
7100c16b537SWarner Losh
7110c16b537SWarner Losh } /* if (nextArgumentIsAFile==0) */
7120c16b537SWarner Losh
7130c16b537SWarner Losh if (nextEntryIsDictionary) {
7140c16b537SWarner Losh nextEntryIsDictionary = 0;
7150c16b537SWarner Losh lastCommand = 0;
7160c16b537SWarner Losh dictFileName = argument;
7170c16b537SWarner Losh continue;
7180c16b537SWarner Losh }
7190c16b537SWarner Losh
7200c16b537SWarner Losh if (nextArgumentIsOutFileName) {
7210c16b537SWarner Losh nextArgumentIsOutFileName = 0;
7220c16b537SWarner Losh lastCommand = 0;
7230c16b537SWarner Losh outFileName = argument;
7240c16b537SWarner Losh if (!strcmp(outFileName, "-")) outFileName = stdoutmark;
7250c16b537SWarner Losh continue;
7260c16b537SWarner Losh }
7270c16b537SWarner Losh
7280c16b537SWarner Losh /* add filename to list */
7290c16b537SWarner Losh filenameTable[filenameIdx++] = argument;
7300c16b537SWarner Losh }
7310c16b537SWarner Losh
7320c16b537SWarner Losh if (lastCommand) { /* forgotten argument */
7330c16b537SWarner Losh DISPLAY("error : command must be followed by argument \n");
7340c16b537SWarner Losh CLEAN_RETURN(1);
7350c16b537SWarner Losh }
7360c16b537SWarner Losh
7370c16b537SWarner Losh /* Welcome message (if verbose) */
7380c16b537SWarner Losh DISPLAYLEVEL(3, WELCOME_MESSAGE);
7390c16b537SWarner Losh
740*19fcbaf1SConrad Meyer #ifdef ZSTD_MULTITHREAD
741*19fcbaf1SConrad Meyer if ((nbWorkers==0) && (!singleThread)) {
742*19fcbaf1SConrad Meyer /* automatically set # workers based on # of reported cpus */
743*19fcbaf1SConrad Meyer nbWorkers = UTIL_countPhysicalCores();
744*19fcbaf1SConrad Meyer DISPLAYLEVEL(3, "Note: %d physical core(s) detected \n", nbWorkers);
7450c16b537SWarner Losh }
746*19fcbaf1SConrad Meyer #endif
7470c16b537SWarner Losh
7480c16b537SWarner Losh g_utilDisplayLevel = g_displayLevel;
7490c16b537SWarner Losh if (!followLinks) {
7500c16b537SWarner Losh unsigned u;
7510c16b537SWarner Losh for (u=0, fileNamesNb=0; u<filenameIdx; u++) {
7520c16b537SWarner Losh if (UTIL_isLink(filenameTable[u])) {
7530c16b537SWarner Losh DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", filenameTable[u]);
7540c16b537SWarner Losh } else {
7550c16b537SWarner Losh filenameTable[fileNamesNb++] = filenameTable[u];
7560c16b537SWarner Losh }
7570c16b537SWarner Losh }
7580c16b537SWarner Losh filenameIdx = fileNamesNb;
7590c16b537SWarner Losh }
7600c16b537SWarner Losh #ifdef UTIL_HAS_CREATEFILELIST
7610c16b537SWarner Losh if (recursive) { /* at this stage, filenameTable is a list of paths, which can contain both files and directories */
7620c16b537SWarner Losh extendedFileList = UTIL_createFileList(filenameTable, filenameIdx, &fileNamesBuf, &fileNamesNb, followLinks);
7630c16b537SWarner Losh if (extendedFileList) {
7640c16b537SWarner Losh unsigned u;
7650c16b537SWarner Losh for (u=0; u<fileNamesNb; u++) DISPLAYLEVEL(4, "%u %s\n", u, extendedFileList[u]);
7660c16b537SWarner Losh free((void*)filenameTable);
7670c16b537SWarner Losh filenameTable = extendedFileList;
7680c16b537SWarner Losh filenameIdx = fileNamesNb;
7690c16b537SWarner Losh }
7700c16b537SWarner Losh }
7710c16b537SWarner Losh #endif
7720c16b537SWarner Losh
7730c16b537SWarner Losh if (operation == zom_list) {
7740c16b537SWarner Losh #ifndef ZSTD_NODECOMPRESS
7750c16b537SWarner Losh int const ret = FIO_listMultipleFiles(filenameIdx, filenameTable, g_displayLevel);
7760c16b537SWarner Losh CLEAN_RETURN(ret);
7770c16b537SWarner Losh #else
7780c16b537SWarner Losh DISPLAY("file information is not supported \n");
7790c16b537SWarner Losh CLEAN_RETURN(1);
7800c16b537SWarner Losh #endif
7810c16b537SWarner Losh }
7820c16b537SWarner Losh
7830c16b537SWarner Losh /* Check if benchmark is selected */
7840c16b537SWarner Losh if (operation==zom_bench) {
7850c16b537SWarner Losh #ifndef ZSTD_NOBENCH
7860c16b537SWarner Losh BMK_setNotificationLevel(g_displayLevel);
787052d3c12SConrad Meyer BMK_setSeparateFiles(separateFiles);
7880c16b537SWarner Losh BMK_setBlockSize(blockSize);
789*19fcbaf1SConrad Meyer BMK_setNbWorkers(nbWorkers);
790052d3c12SConrad Meyer BMK_setRealTime(setRealTimePrio);
7910c16b537SWarner Losh BMK_setNbSeconds(bench_nbSeconds);
7920c16b537SWarner Losh BMK_setLdmFlag(ldmFlag);
7930c16b537SWarner Losh BMK_setLdmMinMatch(g_ldmMinMatch);
7940c16b537SWarner Losh BMK_setLdmHashLog(g_ldmHashLog);
7950c16b537SWarner Losh if (g_ldmBucketSizeLog != LDM_PARAM_DEFAULT) {
7960c16b537SWarner Losh BMK_setLdmBucketSizeLog(g_ldmBucketSizeLog);
7970c16b537SWarner Losh }
7980c16b537SWarner Losh if (g_ldmHashEveryLog != LDM_PARAM_DEFAULT) {
7990c16b537SWarner Losh BMK_setLdmHashEveryLog(g_ldmHashEveryLog);
8000c16b537SWarner Losh }
801052d3c12SConrad Meyer BMK_benchFiles(filenameTable, filenameIdx, dictFileName, cLevel, cLevelLast, &compressionParams);
802052d3c12SConrad Meyer #else
803052d3c12SConrad Meyer (void)bench_nbSeconds; (void)blockSize; (void)setRealTimePrio; (void)separateFiles;
8040c16b537SWarner Losh #endif
8050c16b537SWarner Losh goto _end;
8060c16b537SWarner Losh }
8070c16b537SWarner Losh
8080c16b537SWarner Losh /* Check if dictionary builder is selected */
8090c16b537SWarner Losh if (operation==zom_train) {
8100c16b537SWarner Losh #ifndef ZSTD_NODICT
8110c16b537SWarner Losh ZDICT_params_t zParams;
8120c16b537SWarner Losh zParams.compressionLevel = dictCLevel;
8130c16b537SWarner Losh zParams.notificationLevel = g_displayLevel;
8140c16b537SWarner Losh zParams.dictID = dictID;
8150c16b537SWarner Losh if (cover) {
8160c16b537SWarner Losh int const optimize = !coverParams.k || !coverParams.d;
817*19fcbaf1SConrad Meyer coverParams.nbThreads = nbWorkers;
8180c16b537SWarner Losh coverParams.zParams = zParams;
8190c16b537SWarner Losh operationResult = DiB_trainFromFiles(outFileName, maxDictSize, filenameTable, filenameIdx, blockSize, NULL, &coverParams, optimize);
8200c16b537SWarner Losh } else {
8210c16b537SWarner Losh ZDICT_legacy_params_t dictParams;
8220c16b537SWarner Losh memset(&dictParams, 0, sizeof(dictParams));
8230c16b537SWarner Losh dictParams.selectivityLevel = dictSelect;
8240c16b537SWarner Losh dictParams.zParams = zParams;
8250c16b537SWarner Losh operationResult = DiB_trainFromFiles(outFileName, maxDictSize, filenameTable, filenameIdx, blockSize, &dictParams, NULL, 0);
8260c16b537SWarner Losh }
8270c16b537SWarner Losh #endif
8280c16b537SWarner Losh goto _end;
8290c16b537SWarner Losh }
8300c16b537SWarner Losh
8310c16b537SWarner Losh #ifndef ZSTD_NODECOMPRESS
8320c16b537SWarner Losh if (operation==zom_test) { outFileName=nulmark; FIO_setRemoveSrcFile(0); } /* test mode */
8330c16b537SWarner Losh #endif
8340c16b537SWarner Losh
8350c16b537SWarner Losh /* No input filename ==> use stdin and stdout */
8360c16b537SWarner Losh filenameIdx += !filenameIdx; /* filenameTable[0] is stdin by default */
837*19fcbaf1SConrad Meyer if (!strcmp(filenameTable[0], stdinmark) && !outFileName)
838*19fcbaf1SConrad Meyer outFileName = stdoutmark; /* when input is stdin, default output is stdout */
8390c16b537SWarner Losh
8400c16b537SWarner Losh /* Check if input/output defined as console; trigger an error in this case */
841*19fcbaf1SConrad Meyer if (!strcmp(filenameTable[0], stdinmark) && IS_CONSOLE(stdin) )
842*19fcbaf1SConrad Meyer CLEAN_RETURN(badusage(programName));
843*19fcbaf1SConrad Meyer if ( outFileName && !strcmp(outFileName, stdoutmark)
844*19fcbaf1SConrad Meyer && IS_CONSOLE(stdout)
845*19fcbaf1SConrad Meyer && !strcmp(filenameTable[0], stdinmark)
846*19fcbaf1SConrad Meyer && !forceStdout
847*19fcbaf1SConrad Meyer && operation!=zom_decompress )
8480c16b537SWarner Losh CLEAN_RETURN(badusage(programName));
8490c16b537SWarner Losh
8500c16b537SWarner Losh #ifndef ZSTD_NOCOMPRESS
8510c16b537SWarner Losh /* check compression level limits */
8520c16b537SWarner Losh { int const maxCLevel = ultra ? ZSTD_maxCLevel() : ZSTDCLI_CLEVEL_MAX;
8530c16b537SWarner Losh if (cLevel > maxCLevel) {
8540c16b537SWarner Losh DISPLAYLEVEL(2, "Warning : compression level higher than max, reduced to %i \n", maxCLevel);
8550c16b537SWarner Losh cLevel = maxCLevel;
8560c16b537SWarner Losh } }
8570c16b537SWarner Losh #endif
8580c16b537SWarner Losh
8590c16b537SWarner Losh /* No status message in pipe mode (stdin - stdout) or multi-files mode */
8600c16b537SWarner Losh if (!strcmp(filenameTable[0], stdinmark) && outFileName && !strcmp(outFileName,stdoutmark) && (g_displayLevel==2)) g_displayLevel=1;
8610c16b537SWarner Losh if ((filenameIdx>1) & (g_displayLevel==2)) g_displayLevel=1;
8620c16b537SWarner Losh
8630c16b537SWarner Losh /* IO Stream/File */
8640c16b537SWarner Losh FIO_setNotificationLevel(g_displayLevel);
8650c16b537SWarner Losh if (operation==zom_compress) {
8660c16b537SWarner Losh #ifndef ZSTD_NOCOMPRESS
867*19fcbaf1SConrad Meyer FIO_setNbWorkers(nbWorkers);
8680c16b537SWarner Losh FIO_setBlockSize((U32)blockSize);
8690c16b537SWarner Losh FIO_setLdmFlag(ldmFlag);
8700c16b537SWarner Losh FIO_setLdmHashLog(g_ldmHashLog);
8710c16b537SWarner Losh FIO_setLdmMinMatch(g_ldmMinMatch);
8720c16b537SWarner Losh if (g_ldmBucketSizeLog != LDM_PARAM_DEFAULT) {
8730c16b537SWarner Losh FIO_setLdmBucketSizeLog(g_ldmBucketSizeLog);
8740c16b537SWarner Losh }
8750c16b537SWarner Losh if (g_ldmHashEveryLog != LDM_PARAM_DEFAULT) {
8760c16b537SWarner Losh FIO_setLdmHashEveryLog(g_ldmHashEveryLog);
8770c16b537SWarner Losh }
8780c16b537SWarner Losh
8790c16b537SWarner Losh if (g_overlapLog!=OVERLAP_LOG_DEFAULT) FIO_setOverlapLog(g_overlapLog);
8800c16b537SWarner Losh if ((filenameIdx==1) && outFileName)
8810c16b537SWarner Losh operationResult = FIO_compressFilename(outFileName, filenameTable[0], dictFileName, cLevel, &compressionParams);
8820c16b537SWarner Losh else
883052d3c12SConrad Meyer operationResult = FIO_compressMultipleFilenames(filenameTable, filenameIdx, outFileName, suffix, dictFileName, cLevel, &compressionParams);
8840c16b537SWarner Losh #else
8850c16b537SWarner Losh (void)suffix;
8860c16b537SWarner Losh DISPLAY("Compression not supported\n");
8870c16b537SWarner Losh #endif
8880c16b537SWarner Losh } else { /* decompression or test */
8890c16b537SWarner Losh #ifndef ZSTD_NODECOMPRESS
8900c16b537SWarner Losh if (memLimit == 0) {
8910c16b537SWarner Losh if (compressionParams.windowLog == 0)
8920c16b537SWarner Losh memLimit = (U32)1 << g_defaultMaxWindowLog;
8930c16b537SWarner Losh else {
8940c16b537SWarner Losh memLimit = (U32)1 << (compressionParams.windowLog & 31);
8950c16b537SWarner Losh }
8960c16b537SWarner Losh }
8970c16b537SWarner Losh FIO_setMemLimit(memLimit);
8980c16b537SWarner Losh if (filenameIdx==1 && outFileName)
8990c16b537SWarner Losh operationResult = FIO_decompressFilename(outFileName, filenameTable[0], dictFileName);
9000c16b537SWarner Losh else
901052d3c12SConrad Meyer operationResult = FIO_decompressMultipleFilenames(filenameTable, filenameIdx, outFileName, dictFileName);
9020c16b537SWarner Losh #else
9030c16b537SWarner Losh DISPLAY("Decompression not supported\n");
9040c16b537SWarner Losh #endif
9050c16b537SWarner Losh }
9060c16b537SWarner Losh
9070c16b537SWarner Losh _end:
9080c16b537SWarner Losh if (main_pause) waitEnter();
9090c16b537SWarner Losh #ifdef UTIL_HAS_CREATEFILELIST
9100c16b537SWarner Losh if (extendedFileList)
9110c16b537SWarner Losh UTIL_freeFileList(extendedFileList, fileNamesBuf);
9120c16b537SWarner Losh else
9130c16b537SWarner Losh #endif
9140c16b537SWarner Losh free((void*)filenameTable);
9150c16b537SWarner Losh return operationResult;
9160c16b537SWarner Losh }
917