1*22ce4affSfengbojiang /*
2*22ce4affSfengbojiang * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3*22ce4affSfengbojiang * All rights reserved.
4*22ce4affSfengbojiang *
5*22ce4affSfengbojiang * This source code is licensed under both the BSD-style license (found in the
6*22ce4affSfengbojiang * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*22ce4affSfengbojiang * in the COPYING file in the root directory of this source tree).
8*22ce4affSfengbojiang * You may select, at your option, one of the above-listed licenses.
9*22ce4affSfengbojiang */
10*22ce4affSfengbojiang
11*22ce4affSfengbojiang
12*22ce4affSfengbojiang /*-************************************
13*22ce4affSfengbojiang * Tuning parameters
14*22ce4affSfengbojiang **************************************/
15*22ce4affSfengbojiang #ifndef ZSTDCLI_CLEVEL_DEFAULT
16*22ce4affSfengbojiang # define ZSTDCLI_CLEVEL_DEFAULT 3
17*22ce4affSfengbojiang #endif
18*22ce4affSfengbojiang
19*22ce4affSfengbojiang #ifndef ZSTDCLI_CLEVEL_MAX
20*22ce4affSfengbojiang # define ZSTDCLI_CLEVEL_MAX 19 /* without using --ultra */
21*22ce4affSfengbojiang #endif
22*22ce4affSfengbojiang
23*22ce4affSfengbojiang #ifndef ZSTDCLI_NBTHREADS_DEFAULT
24*22ce4affSfengbojiang # define ZSTDCLI_NBTHREADS_DEFAULT 1
25*22ce4affSfengbojiang #endif
26*22ce4affSfengbojiang
27*22ce4affSfengbojiang /*-************************************
28*22ce4affSfengbojiang * Dependencies
29*22ce4affSfengbojiang **************************************/
30*22ce4affSfengbojiang #include "platform.h" /* IS_CONSOLE, PLATFORM_POSIX_VERSION */
31*22ce4affSfengbojiang #include "util.h" /* UTIL_HAS_CREATEFILELIST, UTIL_createFileList */
32*22ce4affSfengbojiang #include <stdlib.h> /* getenv */
33*22ce4affSfengbojiang #include <string.h> /* strcmp, strlen */
34*22ce4affSfengbojiang #include <stdio.h> /* fprintf(), stdin, stdout, stderr */
35*22ce4affSfengbojiang #include <errno.h> /* errno */
36*22ce4affSfengbojiang #include <assert.h> /* assert */
37*22ce4affSfengbojiang
38*22ce4affSfengbojiang #include "fileio.h" /* stdinmark, stdoutmark, ZSTD_EXTENSION */
39*22ce4affSfengbojiang #ifndef ZSTD_NOBENCH
40*22ce4affSfengbojiang # include "benchzstd.h" /* BMK_benchFiles */
41*22ce4affSfengbojiang #endif
42*22ce4affSfengbojiang #ifndef ZSTD_NODICT
43*22ce4affSfengbojiang # include "dibio.h" /* ZDICT_cover_params_t, DiB_trainFromFiles() */
44*22ce4affSfengbojiang #endif
45*22ce4affSfengbojiang #include "../lib/zstd.h" /* ZSTD_VERSION_STRING, ZSTD_minCLevel, ZSTD_maxCLevel */
46*22ce4affSfengbojiang
47*22ce4affSfengbojiang
48*22ce4affSfengbojiang /*-************************************
49*22ce4affSfengbojiang * Constants
50*22ce4affSfengbojiang **************************************/
51*22ce4affSfengbojiang #define COMPRESSOR_NAME "zstd command line interface"
52*22ce4affSfengbojiang #ifndef ZSTD_VERSION
53*22ce4affSfengbojiang # define ZSTD_VERSION "v" ZSTD_VERSION_STRING
54*22ce4affSfengbojiang #endif
55*22ce4affSfengbojiang #define AUTHOR "Yann Collet"
56*22ce4affSfengbojiang #define WELCOME_MESSAGE "*** %s %i-bits %s, by %s ***\n", COMPRESSOR_NAME, (int)(sizeof(size_t)*8), ZSTD_VERSION, AUTHOR
57*22ce4affSfengbojiang
58*22ce4affSfengbojiang #define ZSTD_ZSTDMT "zstdmt"
59*22ce4affSfengbojiang #define ZSTD_UNZSTD "unzstd"
60*22ce4affSfengbojiang #define ZSTD_CAT "zstdcat"
61*22ce4affSfengbojiang #define ZSTD_ZCAT "zcat"
62*22ce4affSfengbojiang #define ZSTD_GZ "gzip"
63*22ce4affSfengbojiang #define ZSTD_GUNZIP "gunzip"
64*22ce4affSfengbojiang #define ZSTD_GZCAT "gzcat"
65*22ce4affSfengbojiang #define ZSTD_LZMA "lzma"
66*22ce4affSfengbojiang #define ZSTD_UNLZMA "unlzma"
67*22ce4affSfengbojiang #define ZSTD_XZ "xz"
68*22ce4affSfengbojiang #define ZSTD_UNXZ "unxz"
69*22ce4affSfengbojiang #define ZSTD_LZ4 "lz4"
70*22ce4affSfengbojiang #define ZSTD_UNLZ4 "unlz4"
71*22ce4affSfengbojiang
72*22ce4affSfengbojiang #define KB *(1 <<10)
73*22ce4affSfengbojiang #define MB *(1 <<20)
74*22ce4affSfengbojiang #define GB *(1U<<30)
75*22ce4affSfengbojiang
76*22ce4affSfengbojiang #define DISPLAY_LEVEL_DEFAULT 2
77*22ce4affSfengbojiang
78*22ce4affSfengbojiang static const char* g_defaultDictName = "dictionary";
79*22ce4affSfengbojiang static const unsigned g_defaultMaxDictSize = 110 KB;
80*22ce4affSfengbojiang static const int g_defaultDictCLevel = 3;
81*22ce4affSfengbojiang static const unsigned g_defaultSelectivityLevel = 9;
82*22ce4affSfengbojiang static const unsigned g_defaultMaxWindowLog = 27;
83*22ce4affSfengbojiang #define OVERLAP_LOG_DEFAULT 9999
84*22ce4affSfengbojiang #define LDM_PARAM_DEFAULT 9999 /* Default for parameters where 0 is valid */
85*22ce4affSfengbojiang static U32 g_overlapLog = OVERLAP_LOG_DEFAULT;
86*22ce4affSfengbojiang static U32 g_ldmHashLog = 0;
87*22ce4affSfengbojiang static U32 g_ldmMinMatch = 0;
88*22ce4affSfengbojiang static U32 g_ldmHashRateLog = LDM_PARAM_DEFAULT;
89*22ce4affSfengbojiang static U32 g_ldmBucketSizeLog = LDM_PARAM_DEFAULT;
90*22ce4affSfengbojiang
91*22ce4affSfengbojiang
92*22ce4affSfengbojiang #define DEFAULT_ACCEL 1
93*22ce4affSfengbojiang
94*22ce4affSfengbojiang typedef enum { cover, fastCover, legacy } dictType;
95*22ce4affSfengbojiang
96*22ce4affSfengbojiang /*-************************************
97*22ce4affSfengbojiang * Display Macros
98*22ce4affSfengbojiang **************************************/
99*22ce4affSfengbojiang #define DISPLAY_F(f, ...) fprintf((f), __VA_ARGS__)
100*22ce4affSfengbojiang #define DISPLAYOUT(...) DISPLAY_F(stdout, __VA_ARGS__)
101*22ce4affSfengbojiang #define DISPLAY(...) DISPLAY_F(stderr, __VA_ARGS__)
102*22ce4affSfengbojiang #define DISPLAYLEVEL(l, ...) { if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } }
103*22ce4affSfengbojiang static int g_displayLevel = DISPLAY_LEVEL_DEFAULT; /* 0 : no display, 1: errors, 2 : + result + interaction + warnings, 3 : + progression, 4 : + information */
104*22ce4affSfengbojiang
105*22ce4affSfengbojiang
106*22ce4affSfengbojiang /*-************************************
107*22ce4affSfengbojiang * Command Line
108*22ce4affSfengbojiang **************************************/
109*22ce4affSfengbojiang /* print help either in `stderr` or `stdout` depending on originating request
110*22ce4affSfengbojiang * error (badusage) => stderr
111*22ce4affSfengbojiang * help (usage_advanced) => stdout
112*22ce4affSfengbojiang */
usage(FILE * f,const char * programName)113*22ce4affSfengbojiang static void usage(FILE* f, const char* programName)
114*22ce4affSfengbojiang {
115*22ce4affSfengbojiang DISPLAY_F(f, "Usage : \n");
116*22ce4affSfengbojiang DISPLAY_F(f, " %s [args] [FILE(s)] [-o file] \n", programName);
117*22ce4affSfengbojiang DISPLAY_F(f, "\n");
118*22ce4affSfengbojiang DISPLAY_F(f, "FILE : a filename \n");
119*22ce4affSfengbojiang DISPLAY_F(f, " with no FILE, or when FILE is - , read standard input\n");
120*22ce4affSfengbojiang DISPLAY_F(f, "Arguments : \n");
121*22ce4affSfengbojiang #ifndef ZSTD_NOCOMPRESS
122*22ce4affSfengbojiang DISPLAY_F(f, " -# : # compression level (1-%d, default: %d) \n", ZSTDCLI_CLEVEL_MAX, ZSTDCLI_CLEVEL_DEFAULT);
123*22ce4affSfengbojiang #endif
124*22ce4affSfengbojiang #ifndef ZSTD_NODECOMPRESS
125*22ce4affSfengbojiang DISPLAY_F(f, " -d : decompression \n");
126*22ce4affSfengbojiang #endif
127*22ce4affSfengbojiang DISPLAY_F(f, " -D DICT: use DICT as Dictionary for compression or decompression \n");
128*22ce4affSfengbojiang DISPLAY_F(f, " -o file: result stored into `file` (only 1 output file) \n");
129*22ce4affSfengbojiang DISPLAY_F(f, " -f : overwrite output without prompting, also (de)compress links \n");
130*22ce4affSfengbojiang DISPLAY_F(f, "--rm : remove source file(s) after successful de/compression \n");
131*22ce4affSfengbojiang DISPLAY_F(f, " -k : preserve source file(s) (default) \n");
132*22ce4affSfengbojiang DISPLAY_F(f, " -h/-H : display help/long help and exit \n");
133*22ce4affSfengbojiang }
134*22ce4affSfengbojiang
usage_advanced(const char * programName)135*22ce4affSfengbojiang static void usage_advanced(const char* programName)
136*22ce4affSfengbojiang {
137*22ce4affSfengbojiang DISPLAYOUT(WELCOME_MESSAGE);
138*22ce4affSfengbojiang usage(stdout, programName);
139*22ce4affSfengbojiang DISPLAYOUT( "\n");
140*22ce4affSfengbojiang DISPLAYOUT( "Advanced arguments : \n");
141*22ce4affSfengbojiang DISPLAYOUT( " -V : display Version number and exit \n");
142*22ce4affSfengbojiang
143*22ce4affSfengbojiang DISPLAYOUT( " -c : force write to standard output, even if it is the console \n");
144*22ce4affSfengbojiang
145*22ce4affSfengbojiang DISPLAYOUT( " -v : verbose mode; specify multiple times to increase verbosity \n");
146*22ce4affSfengbojiang DISPLAYOUT( " -q : suppress warnings; specify twice to suppress errors too \n");
147*22ce4affSfengbojiang DISPLAYOUT( "--no-progress : do not display the progress counter \n");
148*22ce4affSfengbojiang
149*22ce4affSfengbojiang #ifdef UTIL_HAS_CREATEFILELIST
150*22ce4affSfengbojiang DISPLAYOUT( " -r : operate recursively on directories \n");
151*22ce4affSfengbojiang DISPLAYOUT( "--filelist FILE : read list of files to operate upon from FILE \n");
152*22ce4affSfengbojiang DISPLAYOUT( "--output-dir-flat DIR : processed files are stored into DIR \n");
153*22ce4affSfengbojiang #endif
154*22ce4affSfengbojiang
155*22ce4affSfengbojiang #ifdef UTIL_HAS_MIRRORFILELIST
156*22ce4affSfengbojiang DISPLAYOUT( "--output-dir-mirror DIR : processed files are stored into DIR respecting original directory structure \n");
157*22ce4affSfengbojiang #endif
158*22ce4affSfengbojiang
159*22ce4affSfengbojiang
160*22ce4affSfengbojiang #ifndef ZSTD_NOCOMPRESS
161*22ce4affSfengbojiang DISPLAYOUT( "--[no-]check : during compression, add XXH64 integrity checksum to frame (default: enabled)");
162*22ce4affSfengbojiang #ifndef ZSTD_NODECOMPRESS
163*22ce4affSfengbojiang DISPLAYOUT( ". If specified with -d, decompressor will ignore/validate checksums in compressed frame (default: validate).");
164*22ce4affSfengbojiang #endif
165*22ce4affSfengbojiang #else
166*22ce4affSfengbojiang #ifdef ZSTD_NOCOMPRESS
167*22ce4affSfengbojiang DISPLAYOUT( "--[no-]check : during decompression, ignore/validate checksums in compressed frame (default: validate).");
168*22ce4affSfengbojiang #endif
169*22ce4affSfengbojiang #endif /* ZSTD_NOCOMPRESS */
170*22ce4affSfengbojiang DISPLAYOUT( "\n");
171*22ce4affSfengbojiang
172*22ce4affSfengbojiang DISPLAYOUT( "-- : All arguments after \"--\" are treated as files \n");
173*22ce4affSfengbojiang
174*22ce4affSfengbojiang #ifndef ZSTD_NOCOMPRESS
175*22ce4affSfengbojiang DISPLAYOUT( "\n");
176*22ce4affSfengbojiang DISPLAYOUT( "Advanced compression arguments : \n");
177*22ce4affSfengbojiang DISPLAYOUT( "--ultra : enable levels beyond %i, up to %i (requires more memory) \n", ZSTDCLI_CLEVEL_MAX, ZSTD_maxCLevel());
178*22ce4affSfengbojiang DISPLAYOUT( "--long[=#]: enable long distance matching with given window log (default: %u) \n", g_defaultMaxWindowLog);
179*22ce4affSfengbojiang DISPLAYOUT( "--fast[=#]: switch to very fast compression levels (default: %u) \n", 1);
180*22ce4affSfengbojiang DISPLAYOUT( "--adapt : dynamically adapt compression level to I/O conditions \n");
181*22ce4affSfengbojiang # ifdef ZSTD_MULTITHREAD
182*22ce4affSfengbojiang DISPLAYOUT( " -T# : spawns # compression threads (default: 1, 0==# cores) \n");
183*22ce4affSfengbojiang DISPLAYOUT( " -B# : select size of each job (default: 0==automatic) \n");
184*22ce4affSfengbojiang DISPLAYOUT( "--single-thread : use a single thread for both I/O and compression (result slightly different than -T1) \n");
185*22ce4affSfengbojiang DISPLAYOUT( "--rsyncable : compress using a rsync-friendly method (-B sets block size) \n");
186*22ce4affSfengbojiang # endif
187*22ce4affSfengbojiang DISPLAYOUT( "--exclude-compressed: only compress files that are not already compressed \n");
188*22ce4affSfengbojiang DISPLAYOUT( "--stream-size=# : specify size of streaming input from `stdin` \n");
189*22ce4affSfengbojiang DISPLAYOUT( "--size-hint=# optimize compression parameters for streaming input of approximately this size \n");
190*22ce4affSfengbojiang DISPLAYOUT( "--target-compressed-block-size=# : generate compressed block of approximately targeted size \n");
191*22ce4affSfengbojiang DISPLAYOUT( "--no-dictID : don't write dictID into header (dictionary compression only) \n");
192*22ce4affSfengbojiang DISPLAYOUT( "--[no-]compress-literals : force (un)compressed literals \n");
193*22ce4affSfengbojiang
194*22ce4affSfengbojiang DISPLAYOUT( "--format=zstd : compress files to the .zst format (default) \n");
195*22ce4affSfengbojiang #ifdef ZSTD_GZCOMPRESS
196*22ce4affSfengbojiang DISPLAYOUT( "--format=gzip : compress files to the .gz format \n");
197*22ce4affSfengbojiang #endif
198*22ce4affSfengbojiang #ifdef ZSTD_LZMACOMPRESS
199*22ce4affSfengbojiang DISPLAYOUT( "--format=xz : compress files to the .xz format \n");
200*22ce4affSfengbojiang DISPLAYOUT( "--format=lzma : compress files to the .lzma format \n");
201*22ce4affSfengbojiang #endif
202*22ce4affSfengbojiang #ifdef ZSTD_LZ4COMPRESS
203*22ce4affSfengbojiang DISPLAYOUT( "--format=lz4 : compress files to the .lz4 format \n");
204*22ce4affSfengbojiang #endif
205*22ce4affSfengbojiang #endif /* !ZSTD_NOCOMPRESS */
206*22ce4affSfengbojiang
207*22ce4affSfengbojiang #ifndef ZSTD_NODECOMPRESS
208*22ce4affSfengbojiang DISPLAYOUT( "\n");
209*22ce4affSfengbojiang DISPLAYOUT( "Advanced decompression arguments : \n");
210*22ce4affSfengbojiang DISPLAYOUT( " -l : print information about zstd compressed files \n");
211*22ce4affSfengbojiang DISPLAYOUT( "--test : test compressed file integrity \n");
212*22ce4affSfengbojiang DISPLAYOUT( " -M# : Set a memory usage limit for decompression \n");
213*22ce4affSfengbojiang # if ZSTD_SPARSE_DEFAULT
214*22ce4affSfengbojiang DISPLAYOUT( "--[no-]sparse : sparse mode (default: enabled on file, disabled on stdout) \n");
215*22ce4affSfengbojiang # else
216*22ce4affSfengbojiang DISPLAYOUT( "--[no-]sparse : sparse mode (default: disabled) \n");
217*22ce4affSfengbojiang # endif
218*22ce4affSfengbojiang #endif /* ZSTD_NODECOMPRESS */
219*22ce4affSfengbojiang
220*22ce4affSfengbojiang #ifndef ZSTD_NODICT
221*22ce4affSfengbojiang DISPLAYOUT( "\n");
222*22ce4affSfengbojiang DISPLAYOUT( "Dictionary builder : \n");
223*22ce4affSfengbojiang DISPLAYOUT( "--train ## : create a dictionary from a training set of files \n");
224*22ce4affSfengbojiang DISPLAYOUT( "--train-cover[=k=#,d=#,steps=#,split=#,shrink[=#]] : use the cover algorithm with optional args \n");
225*22ce4affSfengbojiang DISPLAYOUT( "--train-fastcover[=k=#,d=#,f=#,steps=#,split=#,accel=#,shrink[=#]] : use the fast cover algorithm with optional args \n");
226*22ce4affSfengbojiang DISPLAYOUT( "--train-legacy[=s=#] : use the legacy algorithm with selectivity (default: %u) \n", g_defaultSelectivityLevel);
227*22ce4affSfengbojiang DISPLAYOUT( " -o DICT : DICT is dictionary name (default: %s) \n", g_defaultDictName);
228*22ce4affSfengbojiang DISPLAYOUT( "--maxdict=# : limit dictionary to specified size (default: %u) \n", g_defaultMaxDictSize);
229*22ce4affSfengbojiang DISPLAYOUT( "--dictID=# : force dictionary ID to specified value (default: random) \n");
230*22ce4affSfengbojiang #endif
231*22ce4affSfengbojiang
232*22ce4affSfengbojiang #ifndef ZSTD_NOBENCH
233*22ce4affSfengbojiang DISPLAYOUT( "\n");
234*22ce4affSfengbojiang DISPLAYOUT( "Benchmark arguments : \n");
235*22ce4affSfengbojiang DISPLAYOUT( " -b# : benchmark file(s), using # compression level (default: %d) \n", ZSTDCLI_CLEVEL_DEFAULT);
236*22ce4affSfengbojiang DISPLAYOUT( " -e# : test all compression levels successively from -b# to -e# (default: 1) \n");
237*22ce4affSfengbojiang DISPLAYOUT( " -i# : minimum evaluation time in seconds (default: 3s) \n");
238*22ce4affSfengbojiang DISPLAYOUT( " -B# : cut file into independent blocks of size # (default: no block) \n");
239*22ce4affSfengbojiang DISPLAYOUT( " -S : output one benchmark result per input file (default: consolidated result) \n");
240*22ce4affSfengbojiang DISPLAYOUT( "--priority=rt : set process priority to real-time \n");
241*22ce4affSfengbojiang #endif
242*22ce4affSfengbojiang
243*22ce4affSfengbojiang }
244*22ce4affSfengbojiang
badusage(const char * programName)245*22ce4affSfengbojiang static void badusage(const char* programName)
246*22ce4affSfengbojiang {
247*22ce4affSfengbojiang DISPLAYLEVEL(1, "Incorrect parameters \n");
248*22ce4affSfengbojiang if (g_displayLevel >= 2) usage(stderr, programName);
249*22ce4affSfengbojiang }
250*22ce4affSfengbojiang
waitEnter(void)251*22ce4affSfengbojiang static void waitEnter(void)
252*22ce4affSfengbojiang {
253*22ce4affSfengbojiang int unused;
254*22ce4affSfengbojiang DISPLAY("Press enter to continue... \n");
255*22ce4affSfengbojiang unused = getchar();
256*22ce4affSfengbojiang (void)unused;
257*22ce4affSfengbojiang }
258*22ce4affSfengbojiang
lastNameFromPath(const char * path)259*22ce4affSfengbojiang static const char* lastNameFromPath(const char* path)
260*22ce4affSfengbojiang {
261*22ce4affSfengbojiang const char* name = path;
262*22ce4affSfengbojiang if (strrchr(name, '/')) name = strrchr(name, '/') + 1;
263*22ce4affSfengbojiang if (strrchr(name, '\\')) name = strrchr(name, '\\') + 1; /* windows */
264*22ce4affSfengbojiang return name;
265*22ce4affSfengbojiang }
266*22ce4affSfengbojiang
267*22ce4affSfengbojiang /*! exeNameMatch() :
268*22ce4affSfengbojiang @return : a non-zero value if exeName matches test, excluding the extension
269*22ce4affSfengbojiang */
exeNameMatch(const char * exeName,const char * test)270*22ce4affSfengbojiang static int exeNameMatch(const char* exeName, const char* test)
271*22ce4affSfengbojiang {
272*22ce4affSfengbojiang return !strncmp(exeName, test, strlen(test)) &&
273*22ce4affSfengbojiang (exeName[strlen(test)] == '\0' || exeName[strlen(test)] == '.');
274*22ce4affSfengbojiang }
275*22ce4affSfengbojiang
errorOut(const char * msg)276*22ce4affSfengbojiang static void errorOut(const char* msg)
277*22ce4affSfengbojiang {
278*22ce4affSfengbojiang DISPLAY("%s \n", msg); exit(1);
279*22ce4affSfengbojiang }
280*22ce4affSfengbojiang
281*22ce4affSfengbojiang /*! readU32FromCharChecked() :
282*22ce4affSfengbojiang * @return 0 if success, and store the result in *value.
283*22ce4affSfengbojiang * allows and interprets K, KB, KiB, M, MB and MiB suffix.
284*22ce4affSfengbojiang * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
285*22ce4affSfengbojiang * @return 1 if an overflow error occurs */
readU32FromCharChecked(const char ** stringPtr,unsigned * value)286*22ce4affSfengbojiang static int readU32FromCharChecked(const char** stringPtr, unsigned* value)
287*22ce4affSfengbojiang {
288*22ce4affSfengbojiang unsigned result = 0;
289*22ce4affSfengbojiang while ((**stringPtr >='0') && (**stringPtr <='9')) {
290*22ce4affSfengbojiang unsigned const max = ((unsigned)(-1)) / 10;
291*22ce4affSfengbojiang unsigned last = result;
292*22ce4affSfengbojiang if (result > max) return 1; /* overflow error */
293*22ce4affSfengbojiang result *= 10;
294*22ce4affSfengbojiang result += (unsigned)(**stringPtr - '0');
295*22ce4affSfengbojiang if (result < last) return 1; /* overflow error */
296*22ce4affSfengbojiang (*stringPtr)++ ;
297*22ce4affSfengbojiang }
298*22ce4affSfengbojiang if ((**stringPtr=='K') || (**stringPtr=='M')) {
299*22ce4affSfengbojiang unsigned const maxK = ((unsigned)(-1)) >> 10;
300*22ce4affSfengbojiang if (result > maxK) return 1; /* overflow error */
301*22ce4affSfengbojiang result <<= 10;
302*22ce4affSfengbojiang if (**stringPtr=='M') {
303*22ce4affSfengbojiang if (result > maxK) return 1; /* overflow error */
304*22ce4affSfengbojiang result <<= 10;
305*22ce4affSfengbojiang }
306*22ce4affSfengbojiang (*stringPtr)++; /* skip `K` or `M` */
307*22ce4affSfengbojiang if (**stringPtr=='i') (*stringPtr)++;
308*22ce4affSfengbojiang if (**stringPtr=='B') (*stringPtr)++;
309*22ce4affSfengbojiang }
310*22ce4affSfengbojiang *value = result;
311*22ce4affSfengbojiang return 0;
312*22ce4affSfengbojiang }
313*22ce4affSfengbojiang
314*22ce4affSfengbojiang /*! readU32FromChar() :
315*22ce4affSfengbojiang * @return : unsigned integer value read from input in `char` format.
316*22ce4affSfengbojiang * allows and interprets K, KB, KiB, M, MB and MiB suffix.
317*22ce4affSfengbojiang * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
318*22ce4affSfengbojiang * Note : function will exit() program if digit sequence overflows */
readU32FromChar(const char ** stringPtr)319*22ce4affSfengbojiang static unsigned readU32FromChar(const char** stringPtr) {
320*22ce4affSfengbojiang static const char errorMsg[] = "error: numeric value overflows 32-bit unsigned int";
321*22ce4affSfengbojiang unsigned result;
322*22ce4affSfengbojiang if (readU32FromCharChecked(stringPtr, &result)) { errorOut(errorMsg); }
323*22ce4affSfengbojiang return result;
324*22ce4affSfengbojiang }
325*22ce4affSfengbojiang
326*22ce4affSfengbojiang /*! readSizeTFromCharChecked() :
327*22ce4affSfengbojiang * @return 0 if success, and store the result in *value.
328*22ce4affSfengbojiang * allows and interprets K, KB, KiB, M, MB and MiB suffix.
329*22ce4affSfengbojiang * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
330*22ce4affSfengbojiang * @return 1 if an overflow error occurs */
readSizeTFromCharChecked(const char ** stringPtr,size_t * value)331*22ce4affSfengbojiang static int readSizeTFromCharChecked(const char** stringPtr, size_t* value)
332*22ce4affSfengbojiang {
333*22ce4affSfengbojiang size_t result = 0;
334*22ce4affSfengbojiang while ((**stringPtr >='0') && (**stringPtr <='9')) {
335*22ce4affSfengbojiang size_t const max = ((size_t)(-1)) / 10;
336*22ce4affSfengbojiang size_t last = result;
337*22ce4affSfengbojiang if (result > max) return 1; /* overflow error */
338*22ce4affSfengbojiang result *= 10;
339*22ce4affSfengbojiang result += (size_t)(**stringPtr - '0');
340*22ce4affSfengbojiang if (result < last) return 1; /* overflow error */
341*22ce4affSfengbojiang (*stringPtr)++ ;
342*22ce4affSfengbojiang }
343*22ce4affSfengbojiang if ((**stringPtr=='K') || (**stringPtr=='M')) {
344*22ce4affSfengbojiang size_t const maxK = ((size_t)(-1)) >> 10;
345*22ce4affSfengbojiang if (result > maxK) return 1; /* overflow error */
346*22ce4affSfengbojiang result <<= 10;
347*22ce4affSfengbojiang if (**stringPtr=='M') {
348*22ce4affSfengbojiang if (result > maxK) return 1; /* overflow error */
349*22ce4affSfengbojiang result <<= 10;
350*22ce4affSfengbojiang }
351*22ce4affSfengbojiang (*stringPtr)++; /* skip `K` or `M` */
352*22ce4affSfengbojiang if (**stringPtr=='i') (*stringPtr)++;
353*22ce4affSfengbojiang if (**stringPtr=='B') (*stringPtr)++;
354*22ce4affSfengbojiang }
355*22ce4affSfengbojiang *value = result;
356*22ce4affSfengbojiang return 0;
357*22ce4affSfengbojiang }
358*22ce4affSfengbojiang
359*22ce4affSfengbojiang /*! readSizeTFromChar() :
360*22ce4affSfengbojiang * @return : size_t value read from input in `char` format.
361*22ce4affSfengbojiang * allows and interprets K, KB, KiB, M, MB and MiB suffix.
362*22ce4affSfengbojiang * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
363*22ce4affSfengbojiang * Note : function will exit() program if digit sequence overflows */
readSizeTFromChar(const char ** stringPtr)364*22ce4affSfengbojiang static size_t readSizeTFromChar(const char** stringPtr) {
365*22ce4affSfengbojiang static const char errorMsg[] = "error: numeric value overflows size_t";
366*22ce4affSfengbojiang size_t result;
367*22ce4affSfengbojiang if (readSizeTFromCharChecked(stringPtr, &result)) { errorOut(errorMsg); }
368*22ce4affSfengbojiang return result;
369*22ce4affSfengbojiang }
370*22ce4affSfengbojiang
371*22ce4affSfengbojiang /** longCommandWArg() :
372*22ce4affSfengbojiang * check if *stringPtr is the same as longCommand.
373*22ce4affSfengbojiang * If yes, @return 1 and advances *stringPtr to the position which immediately follows longCommand.
374*22ce4affSfengbojiang * @return 0 and doesn't modify *stringPtr otherwise.
375*22ce4affSfengbojiang */
longCommandWArg(const char ** stringPtr,const char * longCommand)376*22ce4affSfengbojiang static int longCommandWArg(const char** stringPtr, const char* longCommand)
377*22ce4affSfengbojiang {
378*22ce4affSfengbojiang size_t const comSize = strlen(longCommand);
379*22ce4affSfengbojiang int const result = !strncmp(*stringPtr, longCommand, comSize);
380*22ce4affSfengbojiang if (result) *stringPtr += comSize;
381*22ce4affSfengbojiang return result;
382*22ce4affSfengbojiang }
383*22ce4affSfengbojiang
384*22ce4affSfengbojiang
385*22ce4affSfengbojiang #ifndef ZSTD_NODICT
386*22ce4affSfengbojiang
387*22ce4affSfengbojiang static const unsigned kDefaultRegression = 1;
388*22ce4affSfengbojiang /**
389*22ce4affSfengbojiang * parseCoverParameters() :
390*22ce4affSfengbojiang * reads cover parameters from *stringPtr (e.g. "--train-cover=k=48,d=8,steps=32") into *params
391*22ce4affSfengbojiang * @return 1 means that cover parameters were correct
392*22ce4affSfengbojiang * @return 0 in case of malformed parameters
393*22ce4affSfengbojiang */
parseCoverParameters(const char * stringPtr,ZDICT_cover_params_t * params)394*22ce4affSfengbojiang static unsigned parseCoverParameters(const char* stringPtr, ZDICT_cover_params_t* params)
395*22ce4affSfengbojiang {
396*22ce4affSfengbojiang memset(params, 0, sizeof(*params));
397*22ce4affSfengbojiang for (; ;) {
398*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "k=")) { params->k = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
399*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "d=")) { params->d = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
400*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "steps=")) { params->steps = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
401*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "split=")) {
402*22ce4affSfengbojiang unsigned splitPercentage = readU32FromChar(&stringPtr);
403*22ce4affSfengbojiang params->splitPoint = (double)splitPercentage / 100.0;
404*22ce4affSfengbojiang if (stringPtr[0]==',') { stringPtr++; continue; } else break;
405*22ce4affSfengbojiang }
406*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "shrink")) {
407*22ce4affSfengbojiang params->shrinkDictMaxRegression = kDefaultRegression;
408*22ce4affSfengbojiang params->shrinkDict = 1;
409*22ce4affSfengbojiang if (stringPtr[0]=='=') {
410*22ce4affSfengbojiang stringPtr++;
411*22ce4affSfengbojiang params->shrinkDictMaxRegression = readU32FromChar(&stringPtr);
412*22ce4affSfengbojiang }
413*22ce4affSfengbojiang if (stringPtr[0]==',') {
414*22ce4affSfengbojiang stringPtr++;
415*22ce4affSfengbojiang continue;
416*22ce4affSfengbojiang }
417*22ce4affSfengbojiang else break;
418*22ce4affSfengbojiang }
419*22ce4affSfengbojiang return 0;
420*22ce4affSfengbojiang }
421*22ce4affSfengbojiang if (stringPtr[0] != 0) return 0;
422*22ce4affSfengbojiang DISPLAYLEVEL(4, "cover: k=%u\nd=%u\nsteps=%u\nsplit=%u\nshrink%u\n", params->k, params->d, params->steps, (unsigned)(params->splitPoint * 100), params->shrinkDictMaxRegression);
423*22ce4affSfengbojiang return 1;
424*22ce4affSfengbojiang }
425*22ce4affSfengbojiang
426*22ce4affSfengbojiang /**
427*22ce4affSfengbojiang * parseFastCoverParameters() :
428*22ce4affSfengbojiang * reads fastcover parameters from *stringPtr (e.g. "--train-fastcover=k=48,d=8,f=20,steps=32,accel=2") into *params
429*22ce4affSfengbojiang * @return 1 means that fastcover parameters were correct
430*22ce4affSfengbojiang * @return 0 in case of malformed parameters
431*22ce4affSfengbojiang */
parseFastCoverParameters(const char * stringPtr,ZDICT_fastCover_params_t * params)432*22ce4affSfengbojiang static unsigned parseFastCoverParameters(const char* stringPtr, ZDICT_fastCover_params_t* params)
433*22ce4affSfengbojiang {
434*22ce4affSfengbojiang memset(params, 0, sizeof(*params));
435*22ce4affSfengbojiang for (; ;) {
436*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "k=")) { params->k = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
437*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "d=")) { params->d = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
438*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "f=")) { params->f = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
439*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "steps=")) { params->steps = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
440*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "accel=")) { params->accel = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
441*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "split=")) {
442*22ce4affSfengbojiang unsigned splitPercentage = readU32FromChar(&stringPtr);
443*22ce4affSfengbojiang params->splitPoint = (double)splitPercentage / 100.0;
444*22ce4affSfengbojiang if (stringPtr[0]==',') { stringPtr++; continue; } else break;
445*22ce4affSfengbojiang }
446*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "shrink")) {
447*22ce4affSfengbojiang params->shrinkDictMaxRegression = kDefaultRegression;
448*22ce4affSfengbojiang params->shrinkDict = 1;
449*22ce4affSfengbojiang if (stringPtr[0]=='=') {
450*22ce4affSfengbojiang stringPtr++;
451*22ce4affSfengbojiang params->shrinkDictMaxRegression = readU32FromChar(&stringPtr);
452*22ce4affSfengbojiang }
453*22ce4affSfengbojiang if (stringPtr[0]==',') {
454*22ce4affSfengbojiang stringPtr++;
455*22ce4affSfengbojiang continue;
456*22ce4affSfengbojiang }
457*22ce4affSfengbojiang else break;
458*22ce4affSfengbojiang }
459*22ce4affSfengbojiang return 0;
460*22ce4affSfengbojiang }
461*22ce4affSfengbojiang if (stringPtr[0] != 0) return 0;
462*22ce4affSfengbojiang DISPLAYLEVEL(4, "cover: k=%u\nd=%u\nf=%u\nsteps=%u\nsplit=%u\naccel=%u\nshrink=%u\n", params->k, params->d, params->f, params->steps, (unsigned)(params->splitPoint * 100), params->accel, params->shrinkDictMaxRegression);
463*22ce4affSfengbojiang return 1;
464*22ce4affSfengbojiang }
465*22ce4affSfengbojiang
466*22ce4affSfengbojiang /**
467*22ce4affSfengbojiang * parseLegacyParameters() :
468*22ce4affSfengbojiang * reads legacy dictionary builder parameters from *stringPtr (e.g. "--train-legacy=selectivity=8") into *selectivity
469*22ce4affSfengbojiang * @return 1 means that legacy dictionary builder parameters were correct
470*22ce4affSfengbojiang * @return 0 in case of malformed parameters
471*22ce4affSfengbojiang */
parseLegacyParameters(const char * stringPtr,unsigned * selectivity)472*22ce4affSfengbojiang static unsigned parseLegacyParameters(const char* stringPtr, unsigned* selectivity)
473*22ce4affSfengbojiang {
474*22ce4affSfengbojiang if (!longCommandWArg(&stringPtr, "s=") && !longCommandWArg(&stringPtr, "selectivity=")) { return 0; }
475*22ce4affSfengbojiang *selectivity = readU32FromChar(&stringPtr);
476*22ce4affSfengbojiang if (stringPtr[0] != 0) return 0;
477*22ce4affSfengbojiang DISPLAYLEVEL(4, "legacy: selectivity=%u\n", *selectivity);
478*22ce4affSfengbojiang return 1;
479*22ce4affSfengbojiang }
480*22ce4affSfengbojiang
defaultCoverParams(void)481*22ce4affSfengbojiang static ZDICT_cover_params_t defaultCoverParams(void)
482*22ce4affSfengbojiang {
483*22ce4affSfengbojiang ZDICT_cover_params_t params;
484*22ce4affSfengbojiang memset(¶ms, 0, sizeof(params));
485*22ce4affSfengbojiang params.d = 8;
486*22ce4affSfengbojiang params.steps = 4;
487*22ce4affSfengbojiang params.splitPoint = 1.0;
488*22ce4affSfengbojiang params.shrinkDict = 0;
489*22ce4affSfengbojiang params.shrinkDictMaxRegression = kDefaultRegression;
490*22ce4affSfengbojiang return params;
491*22ce4affSfengbojiang }
492*22ce4affSfengbojiang
defaultFastCoverParams(void)493*22ce4affSfengbojiang static ZDICT_fastCover_params_t defaultFastCoverParams(void)
494*22ce4affSfengbojiang {
495*22ce4affSfengbojiang ZDICT_fastCover_params_t params;
496*22ce4affSfengbojiang memset(¶ms, 0, sizeof(params));
497*22ce4affSfengbojiang params.d = 8;
498*22ce4affSfengbojiang params.f = 20;
499*22ce4affSfengbojiang params.steps = 4;
500*22ce4affSfengbojiang params.splitPoint = 0.75; /* different from default splitPoint of cover */
501*22ce4affSfengbojiang params.accel = DEFAULT_ACCEL;
502*22ce4affSfengbojiang params.shrinkDict = 0;
503*22ce4affSfengbojiang params.shrinkDictMaxRegression = kDefaultRegression;
504*22ce4affSfengbojiang return params;
505*22ce4affSfengbojiang }
506*22ce4affSfengbojiang #endif
507*22ce4affSfengbojiang
508*22ce4affSfengbojiang
509*22ce4affSfengbojiang /** parseAdaptParameters() :
510*22ce4affSfengbojiang * reads adapt parameters from *stringPtr (e.g. "--zstd=min=1,max=19) and store them into adaptMinPtr and adaptMaxPtr.
511*22ce4affSfengbojiang * Both adaptMinPtr and adaptMaxPtr must be already allocated and correctly initialized.
512*22ce4affSfengbojiang * There is no guarantee that any of these values will be updated.
513*22ce4affSfengbojiang * @return 1 means that parsing was successful,
514*22ce4affSfengbojiang * @return 0 in case of malformed parameters
515*22ce4affSfengbojiang */
parseAdaptParameters(const char * stringPtr,int * adaptMinPtr,int * adaptMaxPtr)516*22ce4affSfengbojiang static unsigned parseAdaptParameters(const char* stringPtr, int* adaptMinPtr, int* adaptMaxPtr)
517*22ce4affSfengbojiang {
518*22ce4affSfengbojiang for ( ; ;) {
519*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "min=")) { *adaptMinPtr = (int)readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
520*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "max=")) { *adaptMaxPtr = (int)readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
521*22ce4affSfengbojiang DISPLAYLEVEL(4, "invalid compression parameter \n");
522*22ce4affSfengbojiang return 0;
523*22ce4affSfengbojiang }
524*22ce4affSfengbojiang if (stringPtr[0] != 0) return 0; /* check the end of string */
525*22ce4affSfengbojiang if (*adaptMinPtr > *adaptMaxPtr) {
526*22ce4affSfengbojiang DISPLAYLEVEL(4, "incoherent adaptation limits \n");
527*22ce4affSfengbojiang return 0;
528*22ce4affSfengbojiang }
529*22ce4affSfengbojiang return 1;
530*22ce4affSfengbojiang }
531*22ce4affSfengbojiang
532*22ce4affSfengbojiang
533*22ce4affSfengbojiang /** parseCompressionParameters() :
534*22ce4affSfengbojiang * reads compression parameters from *stringPtr (e.g. "--zstd=wlog=23,clog=23,hlog=22,slog=6,mml=3,tlen=48,strat=6") into *params
535*22ce4affSfengbojiang * @return 1 means that compression parameters were correct
536*22ce4affSfengbojiang * @return 0 in case of malformed parameters
537*22ce4affSfengbojiang */
parseCompressionParameters(const char * stringPtr,ZSTD_compressionParameters * params)538*22ce4affSfengbojiang static unsigned parseCompressionParameters(const char* stringPtr, ZSTD_compressionParameters* params)
539*22ce4affSfengbojiang {
540*22ce4affSfengbojiang for ( ; ;) {
541*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "windowLog=") || longCommandWArg(&stringPtr, "wlog=")) { params->windowLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
542*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "chainLog=") || longCommandWArg(&stringPtr, "clog=")) { params->chainLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
543*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "hashLog=") || longCommandWArg(&stringPtr, "hlog=")) { params->hashLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
544*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "searchLog=") || longCommandWArg(&stringPtr, "slog=")) { params->searchLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
545*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "minMatch=") || longCommandWArg(&stringPtr, "mml=")) { params->minMatch = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
546*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "targetLength=") || longCommandWArg(&stringPtr, "tlen=")) { params->targetLength = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
547*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "strategy=") || longCommandWArg(&stringPtr, "strat=")) { params->strategy = (ZSTD_strategy)(readU32FromChar(&stringPtr)); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
548*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "overlapLog=") || longCommandWArg(&stringPtr, "ovlog=")) { g_overlapLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
549*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "ldmHashLog=") || longCommandWArg(&stringPtr, "lhlog=")) { g_ldmHashLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
550*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "ldmMinMatch=") || longCommandWArg(&stringPtr, "lmml=")) { g_ldmMinMatch = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
551*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "ldmBucketSizeLog=") || longCommandWArg(&stringPtr, "lblog=")) { g_ldmBucketSizeLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
552*22ce4affSfengbojiang if (longCommandWArg(&stringPtr, "ldmHashRateLog=") || longCommandWArg(&stringPtr, "lhrlog=")) { g_ldmHashRateLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
553*22ce4affSfengbojiang DISPLAYLEVEL(4, "invalid compression parameter \n");
554*22ce4affSfengbojiang return 0;
555*22ce4affSfengbojiang }
556*22ce4affSfengbojiang
557*22ce4affSfengbojiang DISPLAYLEVEL(4, "windowLog=%d, chainLog=%d, hashLog=%d, searchLog=%d \n", params->windowLog, params->chainLog, params->hashLog, params->searchLog);
558*22ce4affSfengbojiang DISPLAYLEVEL(4, "minMatch=%d, targetLength=%d, strategy=%d \n", params->minMatch, params->targetLength, params->strategy);
559*22ce4affSfengbojiang if (stringPtr[0] != 0) return 0; /* check the end of string */
560*22ce4affSfengbojiang return 1;
561*22ce4affSfengbojiang }
562*22ce4affSfengbojiang
printVersion(void)563*22ce4affSfengbojiang static void printVersion(void)
564*22ce4affSfengbojiang {
565*22ce4affSfengbojiang if (g_displayLevel < DISPLAY_LEVEL_DEFAULT) {
566*22ce4affSfengbojiang DISPLAYOUT("%s\n", ZSTD_VERSION_STRING);
567*22ce4affSfengbojiang return;
568*22ce4affSfengbojiang }
569*22ce4affSfengbojiang
570*22ce4affSfengbojiang DISPLAYOUT(WELCOME_MESSAGE);
571*22ce4affSfengbojiang if (g_displayLevel >= 3) {
572*22ce4affSfengbojiang /* format support */
573*22ce4affSfengbojiang DISPLAYOUT("*** supports: zstd");
574*22ce4affSfengbojiang #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>0) && (ZSTD_LEGACY_SUPPORT<8)
575*22ce4affSfengbojiang DISPLAYOUT(", zstd legacy v0.%d+", ZSTD_LEGACY_SUPPORT);
576*22ce4affSfengbojiang #endif
577*22ce4affSfengbojiang #ifdef ZSTD_GZCOMPRESS
578*22ce4affSfengbojiang DISPLAYOUT(", gzip");
579*22ce4affSfengbojiang #endif
580*22ce4affSfengbojiang #ifdef ZSTD_LZ4COMPRESS
581*22ce4affSfengbojiang DISPLAYOUT(", lz4");
582*22ce4affSfengbojiang #endif
583*22ce4affSfengbojiang #ifdef ZSTD_LZMACOMPRESS
584*22ce4affSfengbojiang DISPLAYOUT(", lzma, xz ");
585*22ce4affSfengbojiang #endif
586*22ce4affSfengbojiang DISPLAYOUT("\n");
587*22ce4affSfengbojiang if (g_displayLevel >= 4) {
588*22ce4affSfengbojiang /* posix support */
589*22ce4affSfengbojiang #ifdef _POSIX_C_SOURCE
590*22ce4affSfengbojiang DISPLAYOUT("_POSIX_C_SOURCE defined: %ldL\n", (long) _POSIX_C_SOURCE);
591*22ce4affSfengbojiang #endif
592*22ce4affSfengbojiang #ifdef _POSIX_VERSION
593*22ce4affSfengbojiang DISPLAYOUT("_POSIX_VERSION defined: %ldL \n", (long) _POSIX_VERSION);
594*22ce4affSfengbojiang #endif
595*22ce4affSfengbojiang #ifdef PLATFORM_POSIX_VERSION
596*22ce4affSfengbojiang DISPLAYOUT("PLATFORM_POSIX_VERSION defined: %ldL\n", (long) PLATFORM_POSIX_VERSION);
597*22ce4affSfengbojiang #endif
598*22ce4affSfengbojiang } }
599*22ce4affSfengbojiang }
600*22ce4affSfengbojiang
601*22ce4affSfengbojiang /* Environment variables for parameter setting */
602*22ce4affSfengbojiang #define ENV_CLEVEL "ZSTD_CLEVEL"
603*22ce4affSfengbojiang #define ENV_NBTHREADS "ZSTD_NBTHREADS" /* takes lower precedence than directly specifying -T# in the CLI */
604*22ce4affSfengbojiang
605*22ce4affSfengbojiang /* pick up environment variable */
init_cLevel(void)606*22ce4affSfengbojiang static int init_cLevel(void) {
607*22ce4affSfengbojiang const char* const env = getenv(ENV_CLEVEL);
608*22ce4affSfengbojiang if (env != NULL) {
609*22ce4affSfengbojiang const char* ptr = env;
610*22ce4affSfengbojiang int sign = 1;
611*22ce4affSfengbojiang if (*ptr == '-') {
612*22ce4affSfengbojiang sign = -1;
613*22ce4affSfengbojiang ptr++;
614*22ce4affSfengbojiang } else if (*ptr == '+') {
615*22ce4affSfengbojiang ptr++;
616*22ce4affSfengbojiang }
617*22ce4affSfengbojiang
618*22ce4affSfengbojiang if ((*ptr>='0') && (*ptr<='9')) {
619*22ce4affSfengbojiang unsigned absLevel;
620*22ce4affSfengbojiang if (readU32FromCharChecked(&ptr, &absLevel)) {
621*22ce4affSfengbojiang DISPLAYLEVEL(2, "Ignore environment variable setting %s=%s: numeric value too large \n", ENV_CLEVEL, env);
622*22ce4affSfengbojiang return ZSTDCLI_CLEVEL_DEFAULT;
623*22ce4affSfengbojiang } else if (*ptr == 0) {
624*22ce4affSfengbojiang return sign * (int)absLevel;
625*22ce4affSfengbojiang } }
626*22ce4affSfengbojiang
627*22ce4affSfengbojiang DISPLAYLEVEL(2, "Ignore environment variable setting %s=%s: not a valid integer value \n", ENV_CLEVEL, env);
628*22ce4affSfengbojiang }
629*22ce4affSfengbojiang
630*22ce4affSfengbojiang return ZSTDCLI_CLEVEL_DEFAULT;
631*22ce4affSfengbojiang }
632*22ce4affSfengbojiang
633*22ce4affSfengbojiang #ifdef ZSTD_MULTITHREAD
init_nbThreads(void)634*22ce4affSfengbojiang static unsigned init_nbThreads(void) {
635*22ce4affSfengbojiang const char* const env = getenv(ENV_NBTHREADS);
636*22ce4affSfengbojiang if (env != NULL) {
637*22ce4affSfengbojiang const char* ptr = env;
638*22ce4affSfengbojiang if ((*ptr>='0') && (*ptr<='9')) {
639*22ce4affSfengbojiang unsigned nbThreads;
640*22ce4affSfengbojiang if (readU32FromCharChecked(&ptr, &nbThreads)) {
641*22ce4affSfengbojiang DISPLAYLEVEL(2, "Ignore environment variable setting %s=%s: numeric value too large \n", ENV_NBTHREADS, env);
642*22ce4affSfengbojiang return ZSTDCLI_NBTHREADS_DEFAULT;
643*22ce4affSfengbojiang } else if (*ptr == 0) {
644*22ce4affSfengbojiang return nbThreads;
645*22ce4affSfengbojiang }
646*22ce4affSfengbojiang }
647*22ce4affSfengbojiang DISPLAYLEVEL(2, "Ignore environment variable setting %s=%s: not a valid unsigned value \n", ENV_NBTHREADS, env);
648*22ce4affSfengbojiang }
649*22ce4affSfengbojiang
650*22ce4affSfengbojiang return ZSTDCLI_NBTHREADS_DEFAULT;
651*22ce4affSfengbojiang }
652*22ce4affSfengbojiang #endif
653*22ce4affSfengbojiang
654*22ce4affSfengbojiang #define NEXT_FIELD(ptr) { \
655*22ce4affSfengbojiang if (*argument == '=') { \
656*22ce4affSfengbojiang ptr = ++argument; \
657*22ce4affSfengbojiang argument += strlen(ptr); \
658*22ce4affSfengbojiang } else { \
659*22ce4affSfengbojiang argNb++; \
660*22ce4affSfengbojiang if (argNb >= argCount) { \
661*22ce4affSfengbojiang DISPLAY("error: missing command argument \n"); \
662*22ce4affSfengbojiang CLEAN_RETURN(1); \
663*22ce4affSfengbojiang } \
664*22ce4affSfengbojiang ptr = argv[argNb]; \
665*22ce4affSfengbojiang assert(ptr != NULL); \
666*22ce4affSfengbojiang if (ptr[0]=='-') { \
667*22ce4affSfengbojiang DISPLAY("error: command cannot be separated from its argument by another command \n"); \
668*22ce4affSfengbojiang CLEAN_RETURN(1); \
669*22ce4affSfengbojiang } } }
670*22ce4affSfengbojiang
671*22ce4affSfengbojiang #define NEXT_UINT32(val32) { \
672*22ce4affSfengbojiang const char* __nb; \
673*22ce4affSfengbojiang NEXT_FIELD(__nb); \
674*22ce4affSfengbojiang val32 = readU32FromChar(&__nb); \
675*22ce4affSfengbojiang }
676*22ce4affSfengbojiang
677*22ce4affSfengbojiang #define ZSTD_NB_STRATEGIES 9
678*22ce4affSfengbojiang static const char* ZSTD_strategyMap[ZSTD_NB_STRATEGIES + 1] = { "", "ZSTD_fast",
679*22ce4affSfengbojiang "ZSTD_dfast", "ZSTD_greedy", "ZSTD_lazy", "ZSTD_lazy2", "ZSTD_btlazy2",
680*22ce4affSfengbojiang "ZSTD_btopt", "ZSTD_btultra", "ZSTD_btultra2"};
681*22ce4affSfengbojiang
682*22ce4affSfengbojiang typedef enum { zom_compress, zom_decompress, zom_test, zom_bench, zom_train, zom_list } zstd_operation_mode;
683*22ce4affSfengbojiang
684*22ce4affSfengbojiang #define CLEAN_RETURN(i) { operationResult = (i); goto _end; }
685*22ce4affSfengbojiang
686*22ce4affSfengbojiang #ifdef ZSTD_NOCOMPRESS
687*22ce4affSfengbojiang /* symbols from compression library are not defined and should not be invoked */
688*22ce4affSfengbojiang # define MINCLEVEL -99
689*22ce4affSfengbojiang # define MAXCLEVEL 22
690*22ce4affSfengbojiang #else
691*22ce4affSfengbojiang # define MINCLEVEL ZSTD_minCLevel()
692*22ce4affSfengbojiang # define MAXCLEVEL ZSTD_maxCLevel()
693*22ce4affSfengbojiang #endif
694*22ce4affSfengbojiang
main(int const argCount,const char * argv[])695*22ce4affSfengbojiang int main(int const argCount, const char* argv[])
696*22ce4affSfengbojiang {
697*22ce4affSfengbojiang int argNb,
698*22ce4affSfengbojiang followLinks = 0,
699*22ce4affSfengbojiang forceStdout = 0,
700*22ce4affSfengbojiang hasStdout = 0,
701*22ce4affSfengbojiang ldmFlag = 0,
702*22ce4affSfengbojiang main_pause = 0,
703*22ce4affSfengbojiang nbWorkers = 0,
704*22ce4affSfengbojiang adapt = 0,
705*22ce4affSfengbojiang adaptMin = MINCLEVEL,
706*22ce4affSfengbojiang adaptMax = MAXCLEVEL,
707*22ce4affSfengbojiang rsyncable = 0,
708*22ce4affSfengbojiang nextArgumentsAreFiles = 0,
709*22ce4affSfengbojiang operationResult = 0,
710*22ce4affSfengbojiang separateFiles = 0,
711*22ce4affSfengbojiang setRealTimePrio = 0,
712*22ce4affSfengbojiang singleThread = 0,
713*22ce4affSfengbojiang showDefaultCParams = 0,
714*22ce4affSfengbojiang ultra=0,
715*22ce4affSfengbojiang contentSize=1;
716*22ce4affSfengbojiang double compressibility = 0.5;
717*22ce4affSfengbojiang unsigned bench_nbSeconds = 3; /* would be better if this value was synchronized from bench */
718*22ce4affSfengbojiang size_t blockSize = 0;
719*22ce4affSfengbojiang
720*22ce4affSfengbojiang FIO_prefs_t* const prefs = FIO_createPreferences();
721*22ce4affSfengbojiang FIO_ctx_t* const fCtx = FIO_createContext();
722*22ce4affSfengbojiang zstd_operation_mode operation = zom_compress;
723*22ce4affSfengbojiang ZSTD_compressionParameters compressionParams;
724*22ce4affSfengbojiang int cLevel = init_cLevel();
725*22ce4affSfengbojiang int cLevelLast = MINCLEVEL - 1; /* lower than minimum */
726*22ce4affSfengbojiang unsigned recursive = 0;
727*22ce4affSfengbojiang unsigned memLimit = 0;
728*22ce4affSfengbojiang FileNamesTable* filenames = UTIL_allocateFileNamesTable((size_t)argCount); /* argCount >= 1 */
729*22ce4affSfengbojiang FileNamesTable* file_of_names = UTIL_allocateFileNamesTable((size_t)argCount); /* argCount >= 1 */
730*22ce4affSfengbojiang const char* programName = argv[0];
731*22ce4affSfengbojiang const char* outFileName = NULL;
732*22ce4affSfengbojiang const char* outDirName = NULL;
733*22ce4affSfengbojiang const char* outMirroredDirName = NULL;
734*22ce4affSfengbojiang const char* dictFileName = NULL;
735*22ce4affSfengbojiang const char* patchFromDictFileName = NULL;
736*22ce4affSfengbojiang const char* suffix = ZSTD_EXTENSION;
737*22ce4affSfengbojiang unsigned maxDictSize = g_defaultMaxDictSize;
738*22ce4affSfengbojiang unsigned dictID = 0;
739*22ce4affSfengbojiang size_t streamSrcSize = 0;
740*22ce4affSfengbojiang size_t targetCBlockSize = 0;
741*22ce4affSfengbojiang size_t srcSizeHint = 0;
742*22ce4affSfengbojiang int dictCLevel = g_defaultDictCLevel;
743*22ce4affSfengbojiang unsigned dictSelect = g_defaultSelectivityLevel;
744*22ce4affSfengbojiang #ifndef ZSTD_NODICT
745*22ce4affSfengbojiang ZDICT_cover_params_t coverParams = defaultCoverParams();
746*22ce4affSfengbojiang ZDICT_fastCover_params_t fastCoverParams = defaultFastCoverParams();
747*22ce4affSfengbojiang dictType dict = fastCover;
748*22ce4affSfengbojiang #endif
749*22ce4affSfengbojiang #ifndef ZSTD_NOBENCH
750*22ce4affSfengbojiang BMK_advancedParams_t benchParams = BMK_initAdvancedParams();
751*22ce4affSfengbojiang #endif
752*22ce4affSfengbojiang ZSTD_literalCompressionMode_e literalCompressionMode = ZSTD_lcm_auto;
753*22ce4affSfengbojiang
754*22ce4affSfengbojiang
755*22ce4affSfengbojiang /* init */
756*22ce4affSfengbojiang (void)recursive; (void)cLevelLast; /* not used when ZSTD_NOBENCH set */
757*22ce4affSfengbojiang (void)memLimit;
758*22ce4affSfengbojiang assert(argCount >= 1);
759*22ce4affSfengbojiang if ((filenames==NULL) || (file_of_names==NULL)) { DISPLAY("zstd: allocation error \n"); exit(1); }
760*22ce4affSfengbojiang programName = lastNameFromPath(programName);
761*22ce4affSfengbojiang #ifdef ZSTD_MULTITHREAD
762*22ce4affSfengbojiang nbWorkers = init_nbThreads();
763*22ce4affSfengbojiang #endif
764*22ce4affSfengbojiang
765*22ce4affSfengbojiang /* preset behaviors */
766*22ce4affSfengbojiang if (exeNameMatch(programName, ZSTD_ZSTDMT)) nbWorkers=0, singleThread=0;
767*22ce4affSfengbojiang if (exeNameMatch(programName, ZSTD_UNZSTD)) operation=zom_decompress;
768*22ce4affSfengbojiang if (exeNameMatch(programName, ZSTD_CAT)) { operation=zom_decompress; FIO_overwriteMode(prefs); forceStdout=1; followLinks=1; outFileName=stdoutmark; g_displayLevel=1; } /* supports multiple formats */
769*22ce4affSfengbojiang if (exeNameMatch(programName, ZSTD_ZCAT)) { operation=zom_decompress; FIO_overwriteMode(prefs); forceStdout=1; followLinks=1; outFileName=stdoutmark; g_displayLevel=1; } /* behave like zcat, also supports multiple formats */
770*22ce4affSfengbojiang if (exeNameMatch(programName, ZSTD_GZ)) { suffix = GZ_EXTENSION; FIO_setCompressionType(prefs, FIO_gzipCompression); FIO_setRemoveSrcFile(prefs, 1); } /* behave like gzip */
771*22ce4affSfengbojiang if (exeNameMatch(programName, ZSTD_GUNZIP)) { operation=zom_decompress; FIO_setRemoveSrcFile(prefs, 1); } /* behave like gunzip, also supports multiple formats */
772*22ce4affSfengbojiang if (exeNameMatch(programName, ZSTD_GZCAT)) { operation=zom_decompress; FIO_overwriteMode(prefs); forceStdout=1; followLinks=1; outFileName=stdoutmark; g_displayLevel=1; } /* behave like gzcat, also supports multiple formats */
773*22ce4affSfengbojiang if (exeNameMatch(programName, ZSTD_LZMA)) { suffix = LZMA_EXTENSION; FIO_setCompressionType(prefs, FIO_lzmaCompression); FIO_setRemoveSrcFile(prefs, 1); } /* behave like lzma */
774*22ce4affSfengbojiang if (exeNameMatch(programName, ZSTD_UNLZMA)) { operation=zom_decompress; FIO_setCompressionType(prefs, FIO_lzmaCompression); FIO_setRemoveSrcFile(prefs, 1); } /* behave like unlzma, also supports multiple formats */
775*22ce4affSfengbojiang if (exeNameMatch(programName, ZSTD_XZ)) { suffix = XZ_EXTENSION; FIO_setCompressionType(prefs, FIO_xzCompression); FIO_setRemoveSrcFile(prefs, 1); } /* behave like xz */
776*22ce4affSfengbojiang if (exeNameMatch(programName, ZSTD_UNXZ)) { operation=zom_decompress; FIO_setCompressionType(prefs, FIO_xzCompression); FIO_setRemoveSrcFile(prefs, 1); } /* behave like unxz, also supports multiple formats */
777*22ce4affSfengbojiang if (exeNameMatch(programName, ZSTD_LZ4)) { suffix = LZ4_EXTENSION; FIO_setCompressionType(prefs, FIO_lz4Compression); } /* behave like lz4 */
778*22ce4affSfengbojiang if (exeNameMatch(programName, ZSTD_UNLZ4)) { operation=zom_decompress; FIO_setCompressionType(prefs, FIO_lz4Compression); } /* behave like unlz4, also supports multiple formats */
779*22ce4affSfengbojiang memset(&compressionParams, 0, sizeof(compressionParams));
780*22ce4affSfengbojiang
781*22ce4affSfengbojiang /* init crash handler */
782*22ce4affSfengbojiang FIO_addAbortHandler();
783*22ce4affSfengbojiang
784*22ce4affSfengbojiang /* command switches */
785*22ce4affSfengbojiang for (argNb=1; argNb<argCount; argNb++) {
786*22ce4affSfengbojiang const char* argument = argv[argNb];
787*22ce4affSfengbojiang if (!argument) continue; /* Protection if argument empty */
788*22ce4affSfengbojiang
789*22ce4affSfengbojiang if (nextArgumentsAreFiles) {
790*22ce4affSfengbojiang UTIL_refFilename(filenames, argument);
791*22ce4affSfengbojiang continue;
792*22ce4affSfengbojiang }
793*22ce4affSfengbojiang
794*22ce4affSfengbojiang /* "-" means stdin/stdout */
795*22ce4affSfengbojiang if (!strcmp(argument, "-")){
796*22ce4affSfengbojiang UTIL_refFilename(filenames, stdinmark);
797*22ce4affSfengbojiang continue;
798*22ce4affSfengbojiang }
799*22ce4affSfengbojiang
800*22ce4affSfengbojiang /* Decode commands (note : aggregated commands are allowed) */
801*22ce4affSfengbojiang if (argument[0]=='-') {
802*22ce4affSfengbojiang
803*22ce4affSfengbojiang if (argument[1]=='-') {
804*22ce4affSfengbojiang /* long commands (--long-word) */
805*22ce4affSfengbojiang if (!strcmp(argument, "--")) { nextArgumentsAreFiles=1; continue; } /* only file names allowed from now on */
806*22ce4affSfengbojiang if (!strcmp(argument, "--list")) { operation=zom_list; continue; }
807*22ce4affSfengbojiang if (!strcmp(argument, "--compress")) { operation=zom_compress; continue; }
808*22ce4affSfengbojiang if (!strcmp(argument, "--decompress")) { operation=zom_decompress; continue; }
809*22ce4affSfengbojiang if (!strcmp(argument, "--uncompress")) { operation=zom_decompress; continue; }
810*22ce4affSfengbojiang if (!strcmp(argument, "--force")) { FIO_overwriteMode(prefs); forceStdout=1; followLinks=1; continue; }
811*22ce4affSfengbojiang if (!strcmp(argument, "--version")) { printVersion(); CLEAN_RETURN(0); }
812*22ce4affSfengbojiang if (!strcmp(argument, "--help")) { usage_advanced(programName); CLEAN_RETURN(0); }
813*22ce4affSfengbojiang if (!strcmp(argument, "--verbose")) { g_displayLevel++; continue; }
814*22ce4affSfengbojiang if (!strcmp(argument, "--quiet")) { g_displayLevel--; continue; }
815*22ce4affSfengbojiang if (!strcmp(argument, "--stdout")) { forceStdout=1; outFileName=stdoutmark; g_displayLevel-=(g_displayLevel==2); continue; }
816*22ce4affSfengbojiang if (!strcmp(argument, "--ultra")) { ultra=1; continue; }
817*22ce4affSfengbojiang if (!strcmp(argument, "--check")) { FIO_setChecksumFlag(prefs, 2); continue; }
818*22ce4affSfengbojiang if (!strcmp(argument, "--no-check")) { FIO_setChecksumFlag(prefs, 0); continue; }
819*22ce4affSfengbojiang if (!strcmp(argument, "--sparse")) { FIO_setSparseWrite(prefs, 2); continue; }
820*22ce4affSfengbojiang if (!strcmp(argument, "--no-sparse")) { FIO_setSparseWrite(prefs, 0); continue; }
821*22ce4affSfengbojiang if (!strcmp(argument, "--test")) { operation=zom_test; continue; }
822*22ce4affSfengbojiang if (!strcmp(argument, "--train")) { operation=zom_train; if (outFileName==NULL) outFileName=g_defaultDictName; continue; }
823*22ce4affSfengbojiang if (!strcmp(argument, "--no-dictID")) { FIO_setDictIDFlag(prefs, 0); continue; }
824*22ce4affSfengbojiang if (!strcmp(argument, "--keep")) { FIO_setRemoveSrcFile(prefs, 0); continue; }
825*22ce4affSfengbojiang if (!strcmp(argument, "--rm")) { FIO_setRemoveSrcFile(prefs, 1); continue; }
826*22ce4affSfengbojiang if (!strcmp(argument, "--priority=rt")) { setRealTimePrio = 1; continue; }
827*22ce4affSfengbojiang if (!strcmp(argument, "--show-default-cparams")) { showDefaultCParams = 1; continue; }
828*22ce4affSfengbojiang if (!strcmp(argument, "--content-size")) { contentSize = 1; continue; }
829*22ce4affSfengbojiang if (!strcmp(argument, "--no-content-size")) { contentSize = 0; continue; }
830*22ce4affSfengbojiang if (!strcmp(argument, "--adapt")) { adapt = 1; continue; }
831*22ce4affSfengbojiang if (longCommandWArg(&argument, "--adapt=")) { adapt = 1; if (!parseAdaptParameters(argument, &adaptMin, &adaptMax)) { badusage(programName); CLEAN_RETURN(1); } continue; }
832*22ce4affSfengbojiang if (!strcmp(argument, "--single-thread")) { nbWorkers = 0; singleThread = 1; continue; }
833*22ce4affSfengbojiang if (!strcmp(argument, "--format=zstd")) { suffix = ZSTD_EXTENSION; FIO_setCompressionType(prefs, FIO_zstdCompression); continue; }
834*22ce4affSfengbojiang #ifdef ZSTD_GZCOMPRESS
835*22ce4affSfengbojiang if (!strcmp(argument, "--format=gzip")) { suffix = GZ_EXTENSION; FIO_setCompressionType(prefs, FIO_gzipCompression); continue; }
836*22ce4affSfengbojiang #endif
837*22ce4affSfengbojiang #ifdef ZSTD_LZMACOMPRESS
838*22ce4affSfengbojiang if (!strcmp(argument, "--format=lzma")) { suffix = LZMA_EXTENSION; FIO_setCompressionType(prefs, FIO_lzmaCompression); continue; }
839*22ce4affSfengbojiang if (!strcmp(argument, "--format=xz")) { suffix = XZ_EXTENSION; FIO_setCompressionType(prefs, FIO_xzCompression); continue; }
840*22ce4affSfengbojiang #endif
841*22ce4affSfengbojiang #ifdef ZSTD_LZ4COMPRESS
842*22ce4affSfengbojiang if (!strcmp(argument, "--format=lz4")) { suffix = LZ4_EXTENSION; FIO_setCompressionType(prefs, FIO_lz4Compression); continue; }
843*22ce4affSfengbojiang #endif
844*22ce4affSfengbojiang if (!strcmp(argument, "--rsyncable")) { rsyncable = 1; continue; }
845*22ce4affSfengbojiang if (!strcmp(argument, "--compress-literals")) { literalCompressionMode = ZSTD_lcm_huffman; continue; }
846*22ce4affSfengbojiang if (!strcmp(argument, "--no-compress-literals")) { literalCompressionMode = ZSTD_lcm_uncompressed; continue; }
847*22ce4affSfengbojiang if (!strcmp(argument, "--no-progress")) { FIO_setNoProgress(1); continue; }
848*22ce4affSfengbojiang if (!strcmp(argument, "--exclude-compressed")) { FIO_setExcludeCompressedFile(prefs, 1); continue; }
849*22ce4affSfengbojiang
850*22ce4affSfengbojiang /* long commands with arguments */
851*22ce4affSfengbojiang #ifndef ZSTD_NODICT
852*22ce4affSfengbojiang if (longCommandWArg(&argument, "--train-cover")) {
853*22ce4affSfengbojiang operation = zom_train;
854*22ce4affSfengbojiang if (outFileName == NULL)
855*22ce4affSfengbojiang outFileName = g_defaultDictName;
856*22ce4affSfengbojiang dict = cover;
857*22ce4affSfengbojiang /* Allow optional arguments following an = */
858*22ce4affSfengbojiang if (*argument == 0) { memset(&coverParams, 0, sizeof(coverParams)); }
859*22ce4affSfengbojiang else if (*argument++ != '=') { badusage(programName); CLEAN_RETURN(1); }
860*22ce4affSfengbojiang else if (!parseCoverParameters(argument, &coverParams)) { badusage(programName); CLEAN_RETURN(1); }
861*22ce4affSfengbojiang continue;
862*22ce4affSfengbojiang }
863*22ce4affSfengbojiang if (longCommandWArg(&argument, "--train-fastcover")) {
864*22ce4affSfengbojiang operation = zom_train;
865*22ce4affSfengbojiang if (outFileName == NULL)
866*22ce4affSfengbojiang outFileName = g_defaultDictName;
867*22ce4affSfengbojiang dict = fastCover;
868*22ce4affSfengbojiang /* Allow optional arguments following an = */
869*22ce4affSfengbojiang if (*argument == 0) { memset(&fastCoverParams, 0, sizeof(fastCoverParams)); }
870*22ce4affSfengbojiang else if (*argument++ != '=') { badusage(programName); CLEAN_RETURN(1); }
871*22ce4affSfengbojiang else if (!parseFastCoverParameters(argument, &fastCoverParams)) { badusage(programName); CLEAN_RETURN(1); }
872*22ce4affSfengbojiang continue;
873*22ce4affSfengbojiang }
874*22ce4affSfengbojiang if (longCommandWArg(&argument, "--train-legacy")) {
875*22ce4affSfengbojiang operation = zom_train;
876*22ce4affSfengbojiang if (outFileName == NULL)
877*22ce4affSfengbojiang outFileName = g_defaultDictName;
878*22ce4affSfengbojiang dict = legacy;
879*22ce4affSfengbojiang /* Allow optional arguments following an = */
880*22ce4affSfengbojiang if (*argument == 0) { continue; }
881*22ce4affSfengbojiang else if (*argument++ != '=') { badusage(programName); CLEAN_RETURN(1); }
882*22ce4affSfengbojiang else if (!parseLegacyParameters(argument, &dictSelect)) { badusage(programName); CLEAN_RETURN(1); }
883*22ce4affSfengbojiang continue;
884*22ce4affSfengbojiang }
885*22ce4affSfengbojiang #endif
886*22ce4affSfengbojiang if (longCommandWArg(&argument, "--threads")) { NEXT_UINT32(nbWorkers); continue; }
887*22ce4affSfengbojiang if (longCommandWArg(&argument, "--memlimit")) { NEXT_UINT32(memLimit); continue; }
888*22ce4affSfengbojiang if (longCommandWArg(&argument, "--memory")) { NEXT_UINT32(memLimit); continue; }
889*22ce4affSfengbojiang if (longCommandWArg(&argument, "--memlimit-decompress")) { NEXT_UINT32(memLimit); continue; }
890*22ce4affSfengbojiang if (longCommandWArg(&argument, "--block-size=")) { blockSize = readSizeTFromChar(&argument); continue; }
891*22ce4affSfengbojiang if (longCommandWArg(&argument, "--maxdict")) { NEXT_UINT32(maxDictSize); continue; }
892*22ce4affSfengbojiang if (longCommandWArg(&argument, "--dictID")) { NEXT_UINT32(dictID); continue; }
893*22ce4affSfengbojiang if (longCommandWArg(&argument, "--zstd=")) { if (!parseCompressionParameters(argument, &compressionParams)) { badusage(programName); CLEAN_RETURN(1); } continue; }
894*22ce4affSfengbojiang if (longCommandWArg(&argument, "--stream-size=")) { streamSrcSize = readSizeTFromChar(&argument); continue; }
895*22ce4affSfengbojiang if (longCommandWArg(&argument, "--target-compressed-block-size=")) { targetCBlockSize = readSizeTFromChar(&argument); continue; }
896*22ce4affSfengbojiang if (longCommandWArg(&argument, "--size-hint=")) { srcSizeHint = readSizeTFromChar(&argument); continue; }
897*22ce4affSfengbojiang if (longCommandWArg(&argument, "--output-dir-flat")) { NEXT_FIELD(outDirName); continue; }
898*22ce4affSfengbojiang #ifdef UTIL_HAS_MIRRORFILELIST
899*22ce4affSfengbojiang if (longCommandWArg(&argument, "--output-dir-mirror")) { NEXT_FIELD(outMirroredDirName); continue; }
900*22ce4affSfengbojiang #endif
901*22ce4affSfengbojiang if (longCommandWArg(&argument, "--patch-from")) { NEXT_FIELD(patchFromDictFileName); continue; }
902*22ce4affSfengbojiang if (longCommandWArg(&argument, "--long")) {
903*22ce4affSfengbojiang unsigned ldmWindowLog = 0;
904*22ce4affSfengbojiang ldmFlag = 1;
905*22ce4affSfengbojiang /* Parse optional window log */
906*22ce4affSfengbojiang if (*argument == '=') {
907*22ce4affSfengbojiang ++argument;
908*22ce4affSfengbojiang ldmWindowLog = readU32FromChar(&argument);
909*22ce4affSfengbojiang } else if (*argument != 0) {
910*22ce4affSfengbojiang /* Invalid character following --long */
911*22ce4affSfengbojiang badusage(programName);
912*22ce4affSfengbojiang CLEAN_RETURN(1);
913*22ce4affSfengbojiang }
914*22ce4affSfengbojiang /* Only set windowLog if not already set by --zstd */
915*22ce4affSfengbojiang if (compressionParams.windowLog == 0)
916*22ce4affSfengbojiang compressionParams.windowLog = ldmWindowLog;
917*22ce4affSfengbojiang continue;
918*22ce4affSfengbojiang }
919*22ce4affSfengbojiang #ifndef ZSTD_NOCOMPRESS /* linking ZSTD_minCLevel() requires compression support */
920*22ce4affSfengbojiang if (longCommandWArg(&argument, "--fast")) {
921*22ce4affSfengbojiang /* Parse optional acceleration factor */
922*22ce4affSfengbojiang if (*argument == '=') {
923*22ce4affSfengbojiang U32 const maxFast = (U32)-ZSTD_minCLevel();
924*22ce4affSfengbojiang U32 fastLevel;
925*22ce4affSfengbojiang ++argument;
926*22ce4affSfengbojiang fastLevel = readU32FromChar(&argument);
927*22ce4affSfengbojiang if (fastLevel > maxFast) fastLevel = maxFast;
928*22ce4affSfengbojiang if (fastLevel) {
929*22ce4affSfengbojiang dictCLevel = cLevel = -(int)fastLevel;
930*22ce4affSfengbojiang } else {
931*22ce4affSfengbojiang badusage(programName);
932*22ce4affSfengbojiang CLEAN_RETURN(1);
933*22ce4affSfengbojiang }
934*22ce4affSfengbojiang } else if (*argument != 0) {
935*22ce4affSfengbojiang /* Invalid character following --fast */
936*22ce4affSfengbojiang badusage(programName);
937*22ce4affSfengbojiang CLEAN_RETURN(1);
938*22ce4affSfengbojiang } else {
939*22ce4affSfengbojiang cLevel = -1; /* default for --fast */
940*22ce4affSfengbojiang }
941*22ce4affSfengbojiang continue;
942*22ce4affSfengbojiang }
943*22ce4affSfengbojiang #endif
944*22ce4affSfengbojiang
945*22ce4affSfengbojiang if (longCommandWArg(&argument, "--filelist")) {
946*22ce4affSfengbojiang const char* listName;
947*22ce4affSfengbojiang NEXT_FIELD(listName);
948*22ce4affSfengbojiang UTIL_refFilename(file_of_names, listName);
949*22ce4affSfengbojiang continue;
950*22ce4affSfengbojiang }
951*22ce4affSfengbojiang
952*22ce4affSfengbojiang /* fall-through, will trigger bad_usage() later on */
953*22ce4affSfengbojiang }
954*22ce4affSfengbojiang
955*22ce4affSfengbojiang argument++;
956*22ce4affSfengbojiang while (argument[0]!=0) {
957*22ce4affSfengbojiang
958*22ce4affSfengbojiang #ifndef ZSTD_NOCOMPRESS
959*22ce4affSfengbojiang /* compression Level */
960*22ce4affSfengbojiang if ((*argument>='0') && (*argument<='9')) {
961*22ce4affSfengbojiang dictCLevel = cLevel = (int)readU32FromChar(&argument);
962*22ce4affSfengbojiang continue;
963*22ce4affSfengbojiang }
964*22ce4affSfengbojiang #endif
965*22ce4affSfengbojiang
966*22ce4affSfengbojiang switch(argument[0])
967*22ce4affSfengbojiang {
968*22ce4affSfengbojiang /* Display help */
969*22ce4affSfengbojiang case 'V': printVersion(); CLEAN_RETURN(0); /* Version Only */
970*22ce4affSfengbojiang case 'H':
971*22ce4affSfengbojiang case 'h': usage_advanced(programName); CLEAN_RETURN(0);
972*22ce4affSfengbojiang
973*22ce4affSfengbojiang /* Compress */
974*22ce4affSfengbojiang case 'z': operation=zom_compress; argument++; break;
975*22ce4affSfengbojiang
976*22ce4affSfengbojiang /* Decoding */
977*22ce4affSfengbojiang case 'd':
978*22ce4affSfengbojiang #ifndef ZSTD_NOBENCH
979*22ce4affSfengbojiang benchParams.mode = BMK_decodeOnly;
980*22ce4affSfengbojiang if (operation==zom_bench) { argument++; break; } /* benchmark decode (hidden option) */
981*22ce4affSfengbojiang #endif
982*22ce4affSfengbojiang operation=zom_decompress; argument++; break;
983*22ce4affSfengbojiang
984*22ce4affSfengbojiang /* Force stdout, even if stdout==console */
985*22ce4affSfengbojiang case 'c': forceStdout=1; outFileName=stdoutmark; argument++; break;
986*22ce4affSfengbojiang
987*22ce4affSfengbojiang /* Use file content as dictionary */
988*22ce4affSfengbojiang case 'D': argument++; NEXT_FIELD(dictFileName); break;
989*22ce4affSfengbojiang
990*22ce4affSfengbojiang /* Overwrite */
991*22ce4affSfengbojiang case 'f': FIO_overwriteMode(prefs); forceStdout=1; followLinks=1; argument++; break;
992*22ce4affSfengbojiang
993*22ce4affSfengbojiang /* Verbose mode */
994*22ce4affSfengbojiang case 'v': g_displayLevel++; argument++; break;
995*22ce4affSfengbojiang
996*22ce4affSfengbojiang /* Quiet mode */
997*22ce4affSfengbojiang case 'q': g_displayLevel--; argument++; break;
998*22ce4affSfengbojiang
999*22ce4affSfengbojiang /* keep source file (default) */
1000*22ce4affSfengbojiang case 'k': FIO_setRemoveSrcFile(prefs, 0); argument++; break;
1001*22ce4affSfengbojiang
1002*22ce4affSfengbojiang /* Checksum */
1003*22ce4affSfengbojiang case 'C': FIO_setChecksumFlag(prefs, 2); argument++; break;
1004*22ce4affSfengbojiang
1005*22ce4affSfengbojiang /* test compressed file */
1006*22ce4affSfengbojiang case 't': operation=zom_test; argument++; break;
1007*22ce4affSfengbojiang
1008*22ce4affSfengbojiang /* destination file name */
1009*22ce4affSfengbojiang case 'o': argument++; NEXT_FIELD(outFileName); break;
1010*22ce4affSfengbojiang
1011*22ce4affSfengbojiang /* limit memory */
1012*22ce4affSfengbojiang case 'M':
1013*22ce4affSfengbojiang argument++;
1014*22ce4affSfengbojiang memLimit = readU32FromChar(&argument);
1015*22ce4affSfengbojiang break;
1016*22ce4affSfengbojiang case 'l': operation=zom_list; argument++; break;
1017*22ce4affSfengbojiang #ifdef UTIL_HAS_CREATEFILELIST
1018*22ce4affSfengbojiang /* recursive */
1019*22ce4affSfengbojiang case 'r': recursive=1; argument++; break;
1020*22ce4affSfengbojiang #endif
1021*22ce4affSfengbojiang
1022*22ce4affSfengbojiang #ifndef ZSTD_NOBENCH
1023*22ce4affSfengbojiang /* Benchmark */
1024*22ce4affSfengbojiang case 'b':
1025*22ce4affSfengbojiang operation=zom_bench;
1026*22ce4affSfengbojiang argument++;
1027*22ce4affSfengbojiang break;
1028*22ce4affSfengbojiang
1029*22ce4affSfengbojiang /* range bench (benchmark only) */
1030*22ce4affSfengbojiang case 'e':
1031*22ce4affSfengbojiang /* compression Level */
1032*22ce4affSfengbojiang argument++;
1033*22ce4affSfengbojiang cLevelLast = (int)readU32FromChar(&argument);
1034*22ce4affSfengbojiang break;
1035*22ce4affSfengbojiang
1036*22ce4affSfengbojiang /* Modify Nb Iterations (benchmark only) */
1037*22ce4affSfengbojiang case 'i':
1038*22ce4affSfengbojiang argument++;
1039*22ce4affSfengbojiang bench_nbSeconds = readU32FromChar(&argument);
1040*22ce4affSfengbojiang break;
1041*22ce4affSfengbojiang
1042*22ce4affSfengbojiang /* cut input into blocks (benchmark only) */
1043*22ce4affSfengbojiang case 'B':
1044*22ce4affSfengbojiang argument++;
1045*22ce4affSfengbojiang blockSize = readU32FromChar(&argument);
1046*22ce4affSfengbojiang break;
1047*22ce4affSfengbojiang
1048*22ce4affSfengbojiang /* benchmark files separately (hidden option) */
1049*22ce4affSfengbojiang case 'S':
1050*22ce4affSfengbojiang argument++;
1051*22ce4affSfengbojiang separateFiles = 1;
1052*22ce4affSfengbojiang break;
1053*22ce4affSfengbojiang
1054*22ce4affSfengbojiang #endif /* ZSTD_NOBENCH */
1055*22ce4affSfengbojiang
1056*22ce4affSfengbojiang /* nb of threads (hidden option) */
1057*22ce4affSfengbojiang case 'T':
1058*22ce4affSfengbojiang argument++;
1059*22ce4affSfengbojiang nbWorkers = (int)readU32FromChar(&argument);
1060*22ce4affSfengbojiang break;
1061*22ce4affSfengbojiang
1062*22ce4affSfengbojiang /* Dictionary Selection level */
1063*22ce4affSfengbojiang case 's':
1064*22ce4affSfengbojiang argument++;
1065*22ce4affSfengbojiang dictSelect = readU32FromChar(&argument);
1066*22ce4affSfengbojiang break;
1067*22ce4affSfengbojiang
1068*22ce4affSfengbojiang /* Pause at the end (-p) or set an additional param (-p#) (hidden option) */
1069*22ce4affSfengbojiang case 'p': argument++;
1070*22ce4affSfengbojiang #ifndef ZSTD_NOBENCH
1071*22ce4affSfengbojiang if ((*argument>='0') && (*argument<='9')) {
1072*22ce4affSfengbojiang benchParams.additionalParam = (int)readU32FromChar(&argument);
1073*22ce4affSfengbojiang } else
1074*22ce4affSfengbojiang #endif
1075*22ce4affSfengbojiang main_pause=1;
1076*22ce4affSfengbojiang break;
1077*22ce4affSfengbojiang
1078*22ce4affSfengbojiang /* Select compressibility of synthetic sample */
1079*22ce4affSfengbojiang case 'P':
1080*22ce4affSfengbojiang argument++;
1081*22ce4affSfengbojiang compressibility = (double)readU32FromChar(&argument) / 100;
1082*22ce4affSfengbojiang break;
1083*22ce4affSfengbojiang
1084*22ce4affSfengbojiang /* unknown command */
1085*22ce4affSfengbojiang default : badusage(programName); CLEAN_RETURN(1);
1086*22ce4affSfengbojiang }
1087*22ce4affSfengbojiang }
1088*22ce4affSfengbojiang continue;
1089*22ce4affSfengbojiang } /* if (argument[0]=='-') */
1090*22ce4affSfengbojiang
1091*22ce4affSfengbojiang /* none of the above : add filename to list */
1092*22ce4affSfengbojiang UTIL_refFilename(filenames, argument);
1093*22ce4affSfengbojiang }
1094*22ce4affSfengbojiang
1095*22ce4affSfengbojiang /* Welcome message (if verbose) */
1096*22ce4affSfengbojiang DISPLAYLEVEL(3, WELCOME_MESSAGE);
1097*22ce4affSfengbojiang
1098*22ce4affSfengbojiang #ifdef ZSTD_MULTITHREAD
1099*22ce4affSfengbojiang if ((nbWorkers==0) && (!singleThread)) {
1100*22ce4affSfengbojiang /* automatically set # workers based on # of reported cpus */
1101*22ce4affSfengbojiang nbWorkers = UTIL_countPhysicalCores();
1102*22ce4affSfengbojiang DISPLAYLEVEL(3, "Note: %d physical core(s) detected \n", nbWorkers);
1103*22ce4affSfengbojiang }
1104*22ce4affSfengbojiang #else
1105*22ce4affSfengbojiang (void)singleThread; (void)nbWorkers;
1106*22ce4affSfengbojiang #endif
1107*22ce4affSfengbojiang
1108*22ce4affSfengbojiang #ifdef UTIL_HAS_CREATEFILELIST
1109*22ce4affSfengbojiang g_utilDisplayLevel = g_displayLevel;
1110*22ce4affSfengbojiang if (!followLinks) {
1111*22ce4affSfengbojiang unsigned u, fileNamesNb;
1112*22ce4affSfengbojiang unsigned const nbFilenames = (unsigned)filenames->tableSize;
1113*22ce4affSfengbojiang for (u=0, fileNamesNb=0; u<nbFilenames; u++) {
1114*22ce4affSfengbojiang if ( UTIL_isLink(filenames->fileNames[u])
1115*22ce4affSfengbojiang && !UTIL_isFIFO(filenames->fileNames[u])
1116*22ce4affSfengbojiang ) {
1117*22ce4affSfengbojiang DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring \n", filenames->fileNames[u]);
1118*22ce4affSfengbojiang } else {
1119*22ce4affSfengbojiang filenames->fileNames[fileNamesNb++] = filenames->fileNames[u];
1120*22ce4affSfengbojiang } }
1121*22ce4affSfengbojiang if (fileNamesNb == 0 && nbFilenames > 0) /* all names are eliminated */
1122*22ce4affSfengbojiang CLEAN_RETURN(1);
1123*22ce4affSfengbojiang filenames->tableSize = fileNamesNb;
1124*22ce4affSfengbojiang } /* if (!followLinks) */
1125*22ce4affSfengbojiang
1126*22ce4affSfengbojiang /* read names from a file */
1127*22ce4affSfengbojiang if (file_of_names->tableSize) {
1128*22ce4affSfengbojiang size_t const nbFileLists = file_of_names->tableSize;
1129*22ce4affSfengbojiang size_t flNb;
1130*22ce4affSfengbojiang for (flNb=0; flNb < nbFileLists; flNb++) {
1131*22ce4affSfengbojiang FileNamesTable* const fnt = UTIL_createFileNamesTable_fromFileName(file_of_names->fileNames[flNb]);
1132*22ce4affSfengbojiang if (fnt==NULL) {
1133*22ce4affSfengbojiang DISPLAYLEVEL(1, "zstd: error reading %s \n", file_of_names->fileNames[flNb]);
1134*22ce4affSfengbojiang CLEAN_RETURN(1);
1135*22ce4affSfengbojiang }
1136*22ce4affSfengbojiang filenames = UTIL_mergeFileNamesTable(filenames, fnt);
1137*22ce4affSfengbojiang }
1138*22ce4affSfengbojiang }
1139*22ce4affSfengbojiang
1140*22ce4affSfengbojiang if (recursive) { /* at this stage, filenameTable is a list of paths, which can contain both files and directories */
1141*22ce4affSfengbojiang UTIL_expandFNT(&filenames, followLinks);
1142*22ce4affSfengbojiang }
1143*22ce4affSfengbojiang #else
1144*22ce4affSfengbojiang (void)followLinks;
1145*22ce4affSfengbojiang #endif
1146*22ce4affSfengbojiang
1147*22ce4affSfengbojiang if (operation == zom_list) {
1148*22ce4affSfengbojiang #ifndef ZSTD_NODECOMPRESS
1149*22ce4affSfengbojiang int const ret = FIO_listMultipleFiles((unsigned)filenames->tableSize, filenames->fileNames, g_displayLevel);
1150*22ce4affSfengbojiang CLEAN_RETURN(ret);
1151*22ce4affSfengbojiang #else
1152*22ce4affSfengbojiang DISPLAY("file information is not supported \n");
1153*22ce4affSfengbojiang CLEAN_RETURN(1);
1154*22ce4affSfengbojiang #endif
1155*22ce4affSfengbojiang }
1156*22ce4affSfengbojiang
1157*22ce4affSfengbojiang /* Check if benchmark is selected */
1158*22ce4affSfengbojiang if (operation==zom_bench) {
1159*22ce4affSfengbojiang #ifndef ZSTD_NOBENCH
1160*22ce4affSfengbojiang benchParams.blockSize = blockSize;
1161*22ce4affSfengbojiang benchParams.nbWorkers = nbWorkers;
1162*22ce4affSfengbojiang benchParams.realTime = (unsigned)setRealTimePrio;
1163*22ce4affSfengbojiang benchParams.nbSeconds = bench_nbSeconds;
1164*22ce4affSfengbojiang benchParams.ldmFlag = ldmFlag;
1165*22ce4affSfengbojiang benchParams.ldmMinMatch = (int)g_ldmMinMatch;
1166*22ce4affSfengbojiang benchParams.ldmHashLog = (int)g_ldmHashLog;
1167*22ce4affSfengbojiang if (g_ldmBucketSizeLog != LDM_PARAM_DEFAULT) {
1168*22ce4affSfengbojiang benchParams.ldmBucketSizeLog = (int)g_ldmBucketSizeLog;
1169*22ce4affSfengbojiang }
1170*22ce4affSfengbojiang if (g_ldmHashRateLog != LDM_PARAM_DEFAULT) {
1171*22ce4affSfengbojiang benchParams.ldmHashRateLog = (int)g_ldmHashRateLog;
1172*22ce4affSfengbojiang }
1173*22ce4affSfengbojiang benchParams.literalCompressionMode = literalCompressionMode;
1174*22ce4affSfengbojiang
1175*22ce4affSfengbojiang if (cLevel > ZSTD_maxCLevel()) cLevel = ZSTD_maxCLevel();
1176*22ce4affSfengbojiang if (cLevelLast > ZSTD_maxCLevel()) cLevelLast = ZSTD_maxCLevel();
1177*22ce4affSfengbojiang if (cLevelLast < cLevel) cLevelLast = cLevel;
1178*22ce4affSfengbojiang if (cLevelLast > cLevel)
1179*22ce4affSfengbojiang DISPLAYLEVEL(3, "Benchmarking levels from %d to %d\n", cLevel, cLevelLast);
1180*22ce4affSfengbojiang if (filenames->tableSize > 0) {
1181*22ce4affSfengbojiang if(separateFiles) {
1182*22ce4affSfengbojiang unsigned i;
1183*22ce4affSfengbojiang for(i = 0; i < filenames->tableSize; i++) {
1184*22ce4affSfengbojiang int c;
1185*22ce4affSfengbojiang DISPLAYLEVEL(3, "Benchmarking %s \n", filenames->fileNames[i]);
1186*22ce4affSfengbojiang for(c = cLevel; c <= cLevelLast; c++) {
1187*22ce4affSfengbojiang BMK_benchFilesAdvanced(&filenames->fileNames[i], 1, dictFileName, c, &compressionParams, g_displayLevel, &benchParams);
1188*22ce4affSfengbojiang } }
1189*22ce4affSfengbojiang } else {
1190*22ce4affSfengbojiang for(; cLevel <= cLevelLast; cLevel++) {
1191*22ce4affSfengbojiang BMK_benchFilesAdvanced(filenames->fileNames, (unsigned)filenames->tableSize, dictFileName, cLevel, &compressionParams, g_displayLevel, &benchParams);
1192*22ce4affSfengbojiang } }
1193*22ce4affSfengbojiang } else {
1194*22ce4affSfengbojiang for(; cLevel <= cLevelLast; cLevel++) {
1195*22ce4affSfengbojiang BMK_syntheticTest(cLevel, compressibility, &compressionParams, g_displayLevel, &benchParams);
1196*22ce4affSfengbojiang } }
1197*22ce4affSfengbojiang
1198*22ce4affSfengbojiang #else
1199*22ce4affSfengbojiang (void)bench_nbSeconds; (void)blockSize; (void)setRealTimePrio; (void)separateFiles; (void)compressibility;
1200*22ce4affSfengbojiang #endif
1201*22ce4affSfengbojiang goto _end;
1202*22ce4affSfengbojiang }
1203*22ce4affSfengbojiang
1204*22ce4affSfengbojiang /* Check if dictionary builder is selected */
1205*22ce4affSfengbojiang if (operation==zom_train) {
1206*22ce4affSfengbojiang #ifndef ZSTD_NODICT
1207*22ce4affSfengbojiang ZDICT_params_t zParams;
1208*22ce4affSfengbojiang zParams.compressionLevel = dictCLevel;
1209*22ce4affSfengbojiang zParams.notificationLevel = (unsigned)g_displayLevel;
1210*22ce4affSfengbojiang zParams.dictID = dictID;
1211*22ce4affSfengbojiang if (dict == cover) {
1212*22ce4affSfengbojiang int const optimize = !coverParams.k || !coverParams.d;
1213*22ce4affSfengbojiang coverParams.nbThreads = (unsigned)nbWorkers;
1214*22ce4affSfengbojiang coverParams.zParams = zParams;
1215*22ce4affSfengbojiang operationResult = DiB_trainFromFiles(outFileName, maxDictSize, filenames->fileNames, (unsigned)filenames->tableSize, blockSize, NULL, &coverParams, NULL, optimize);
1216*22ce4affSfengbojiang } else if (dict == fastCover) {
1217*22ce4affSfengbojiang int const optimize = !fastCoverParams.k || !fastCoverParams.d;
1218*22ce4affSfengbojiang fastCoverParams.nbThreads = (unsigned)nbWorkers;
1219*22ce4affSfengbojiang fastCoverParams.zParams = zParams;
1220*22ce4affSfengbojiang operationResult = DiB_trainFromFiles(outFileName, maxDictSize, filenames->fileNames, (unsigned)filenames->tableSize, blockSize, NULL, NULL, &fastCoverParams, optimize);
1221*22ce4affSfengbojiang } else {
1222*22ce4affSfengbojiang ZDICT_legacy_params_t dictParams;
1223*22ce4affSfengbojiang memset(&dictParams, 0, sizeof(dictParams));
1224*22ce4affSfengbojiang dictParams.selectivityLevel = dictSelect;
1225*22ce4affSfengbojiang dictParams.zParams = zParams;
1226*22ce4affSfengbojiang operationResult = DiB_trainFromFiles(outFileName, maxDictSize, filenames->fileNames, (unsigned)filenames->tableSize, blockSize, &dictParams, NULL, NULL, 0);
1227*22ce4affSfengbojiang }
1228*22ce4affSfengbojiang #else
1229*22ce4affSfengbojiang (void)dictCLevel; (void)dictSelect; (void)dictID; (void)maxDictSize; /* not used when ZSTD_NODICT set */
1230*22ce4affSfengbojiang DISPLAYLEVEL(1, "training mode not available \n");
1231*22ce4affSfengbojiang operationResult = 1;
1232*22ce4affSfengbojiang #endif
1233*22ce4affSfengbojiang goto _end;
1234*22ce4affSfengbojiang }
1235*22ce4affSfengbojiang
1236*22ce4affSfengbojiang #ifndef ZSTD_NODECOMPRESS
1237*22ce4affSfengbojiang if (operation==zom_test) { FIO_setTestMode(prefs, 1); outFileName=nulmark; FIO_setRemoveSrcFile(prefs, 0); } /* test mode */
1238*22ce4affSfengbojiang #endif
1239*22ce4affSfengbojiang
1240*22ce4affSfengbojiang /* No input filename ==> use stdin and stdout */
1241*22ce4affSfengbojiang if (filenames->tableSize == 0) UTIL_refFilename(filenames, stdinmark);
1242*22ce4affSfengbojiang if (!strcmp(filenames->fileNames[0], stdinmark) && !outFileName)
1243*22ce4affSfengbojiang outFileName = stdoutmark; /* when input is stdin, default output is stdout */
1244*22ce4affSfengbojiang
1245*22ce4affSfengbojiang /* Check if input/output defined as console; trigger an error in this case */
1246*22ce4affSfengbojiang if (!strcmp(filenames->fileNames[0], stdinmark) && IS_CONSOLE(stdin) ) {
1247*22ce4affSfengbojiang DISPLAYLEVEL(1, "stdin is a console, aborting\n");
1248*22ce4affSfengbojiang CLEAN_RETURN(1);
1249*22ce4affSfengbojiang }
1250*22ce4affSfengbojiang if ( outFileName && !strcmp(outFileName, stdoutmark)
1251*22ce4affSfengbojiang && IS_CONSOLE(stdout)
1252*22ce4affSfengbojiang && !strcmp(filenames->fileNames[0], stdinmark)
1253*22ce4affSfengbojiang && !forceStdout
1254*22ce4affSfengbojiang && operation!=zom_decompress ) {
1255*22ce4affSfengbojiang DISPLAYLEVEL(1, "stdout is a console, aborting\n");
1256*22ce4affSfengbojiang CLEAN_RETURN(1);
1257*22ce4affSfengbojiang }
1258*22ce4affSfengbojiang
1259*22ce4affSfengbojiang #ifndef ZSTD_NOCOMPRESS
1260*22ce4affSfengbojiang /* check compression level limits */
1261*22ce4affSfengbojiang { int const maxCLevel = ultra ? ZSTD_maxCLevel() : ZSTDCLI_CLEVEL_MAX;
1262*22ce4affSfengbojiang if (cLevel > maxCLevel) {
1263*22ce4affSfengbojiang DISPLAYLEVEL(2, "Warning : compression level higher than max, reduced to %i \n", maxCLevel);
1264*22ce4affSfengbojiang cLevel = maxCLevel;
1265*22ce4affSfengbojiang } }
1266*22ce4affSfengbojiang #endif
1267*22ce4affSfengbojiang
1268*22ce4affSfengbojiang if (showDefaultCParams) {
1269*22ce4affSfengbojiang if (operation == zom_decompress) {
1270*22ce4affSfengbojiang DISPLAY("error : can't use --show-default-cparams in decomrpession mode \n");
1271*22ce4affSfengbojiang CLEAN_RETURN(1);
1272*22ce4affSfengbojiang }
1273*22ce4affSfengbojiang }
1274*22ce4affSfengbojiang
1275*22ce4affSfengbojiang if (dictFileName != NULL && patchFromDictFileName != NULL) {
1276*22ce4affSfengbojiang DISPLAY("error : can't use -D and --patch-from=# at the same time \n");
1277*22ce4affSfengbojiang CLEAN_RETURN(1);
1278*22ce4affSfengbojiang }
1279*22ce4affSfengbojiang
1280*22ce4affSfengbojiang if (patchFromDictFileName != NULL && filenames->tableSize > 1) {
1281*22ce4affSfengbojiang DISPLAY("error : can't use --patch-from=# on multiple files \n");
1282*22ce4affSfengbojiang CLEAN_RETURN(1);
1283*22ce4affSfengbojiang }
1284*22ce4affSfengbojiang
1285*22ce4affSfengbojiang /* No status message in pipe mode (stdin - stdout) */
1286*22ce4affSfengbojiang hasStdout = outFileName && !strcmp(outFileName,stdoutmark);
1287*22ce4affSfengbojiang
1288*22ce4affSfengbojiang if (hasStdout && (g_displayLevel==2)) g_displayLevel=1;
1289*22ce4affSfengbojiang
1290*22ce4affSfengbojiang /* IO Stream/File */
1291*22ce4affSfengbojiang FIO_setHasStdoutOutput(fCtx, hasStdout);
1292*22ce4affSfengbojiang FIO_setNbFilesTotal(fCtx, (int)filenames->tableSize);
1293*22ce4affSfengbojiang FIO_determineHasStdinInput(fCtx, filenames);
1294*22ce4affSfengbojiang FIO_setNotificationLevel(g_displayLevel);
1295*22ce4affSfengbojiang FIO_setPatchFromMode(prefs, patchFromDictFileName != NULL);
1296*22ce4affSfengbojiang if (memLimit == 0) {
1297*22ce4affSfengbojiang if (compressionParams.windowLog == 0) {
1298*22ce4affSfengbojiang memLimit = (U32)1 << g_defaultMaxWindowLog;
1299*22ce4affSfengbojiang } else {
1300*22ce4affSfengbojiang memLimit = (U32)1 << (compressionParams.windowLog & 31);
1301*22ce4affSfengbojiang } }
1302*22ce4affSfengbojiang if (patchFromDictFileName != NULL)
1303*22ce4affSfengbojiang dictFileName = patchFromDictFileName;
1304*22ce4affSfengbojiang FIO_setMemLimit(prefs, memLimit);
1305*22ce4affSfengbojiang if (operation==zom_compress) {
1306*22ce4affSfengbojiang #ifndef ZSTD_NOCOMPRESS
1307*22ce4affSfengbojiang FIO_setContentSize(prefs, contentSize);
1308*22ce4affSfengbojiang FIO_setNbWorkers(prefs, nbWorkers);
1309*22ce4affSfengbojiang FIO_setBlockSize(prefs, (int)blockSize);
1310*22ce4affSfengbojiang if (g_overlapLog!=OVERLAP_LOG_DEFAULT) FIO_setOverlapLog(prefs, (int)g_overlapLog);
1311*22ce4affSfengbojiang FIO_setLdmFlag(prefs, (unsigned)ldmFlag);
1312*22ce4affSfengbojiang FIO_setLdmHashLog(prefs, (int)g_ldmHashLog);
1313*22ce4affSfengbojiang FIO_setLdmMinMatch(prefs, (int)g_ldmMinMatch);
1314*22ce4affSfengbojiang if (g_ldmBucketSizeLog != LDM_PARAM_DEFAULT) FIO_setLdmBucketSizeLog(prefs, (int)g_ldmBucketSizeLog);
1315*22ce4affSfengbojiang if (g_ldmHashRateLog != LDM_PARAM_DEFAULT) FIO_setLdmHashRateLog(prefs, (int)g_ldmHashRateLog);
1316*22ce4affSfengbojiang FIO_setAdaptiveMode(prefs, (unsigned)adapt);
1317*22ce4affSfengbojiang FIO_setAdaptMin(prefs, adaptMin);
1318*22ce4affSfengbojiang FIO_setAdaptMax(prefs, adaptMax);
1319*22ce4affSfengbojiang FIO_setRsyncable(prefs, rsyncable);
1320*22ce4affSfengbojiang FIO_setStreamSrcSize(prefs, streamSrcSize);
1321*22ce4affSfengbojiang FIO_setTargetCBlockSize(prefs, targetCBlockSize);
1322*22ce4affSfengbojiang FIO_setSrcSizeHint(prefs, srcSizeHint);
1323*22ce4affSfengbojiang FIO_setLiteralCompressionMode(prefs, literalCompressionMode);
1324*22ce4affSfengbojiang if (adaptMin > cLevel) cLevel = adaptMin;
1325*22ce4affSfengbojiang if (adaptMax < cLevel) cLevel = adaptMax;
1326*22ce4affSfengbojiang
1327*22ce4affSfengbojiang /* Compare strategies constant with the ground truth */
1328*22ce4affSfengbojiang { ZSTD_bounds strategyBounds = ZSTD_cParam_getBounds(ZSTD_c_strategy);
1329*22ce4affSfengbojiang assert(ZSTD_NB_STRATEGIES == strategyBounds.upperBound);
1330*22ce4affSfengbojiang (void)strategyBounds; }
1331*22ce4affSfengbojiang
1332*22ce4affSfengbojiang if (showDefaultCParams) {
1333*22ce4affSfengbojiang size_t fileNb;
1334*22ce4affSfengbojiang for (fileNb = 0; fileNb < (size_t)filenames->tableSize; fileNb++) {
1335*22ce4affSfengbojiang unsigned long long fileSize = UTIL_getFileSize(filenames->fileNames[fileNb]);
1336*22ce4affSfengbojiang const size_t dictSize = dictFileName != NULL ? (size_t)UTIL_getFileSize(dictFileName) : 0;
1337*22ce4affSfengbojiang const ZSTD_compressionParameters cParams = ZSTD_getCParams(cLevel, fileSize, dictSize);
1338*22ce4affSfengbojiang if (fileSize != UTIL_FILESIZE_UNKNOWN) DISPLAY("%s (%u bytes)\n", filenames->fileNames[fileNb], (unsigned)fileSize);
1339*22ce4affSfengbojiang else DISPLAY("%s (src size unknown)\n", filenames->fileNames[fileNb]);
1340*22ce4affSfengbojiang DISPLAY(" - windowLog : %u\n", cParams.windowLog);
1341*22ce4affSfengbojiang DISPLAY(" - chainLog : %u\n", cParams.chainLog);
1342*22ce4affSfengbojiang DISPLAY(" - hashLog : %u\n", cParams.hashLog);
1343*22ce4affSfengbojiang DISPLAY(" - searchLog : %u\n", cParams.searchLog);
1344*22ce4affSfengbojiang DISPLAY(" - minMatch : %u\n", cParams.minMatch);
1345*22ce4affSfengbojiang DISPLAY(" - targetLength : %u\n", cParams.targetLength);
1346*22ce4affSfengbojiang assert(cParams.strategy < ZSTD_NB_STRATEGIES + 1);
1347*22ce4affSfengbojiang DISPLAY(" - strategy : %s (%u)\n", ZSTD_strategyMap[(int)cParams.strategy], (unsigned)cParams.strategy);
1348*22ce4affSfengbojiang }
1349*22ce4affSfengbojiang }
1350*22ce4affSfengbojiang
1351*22ce4affSfengbojiang if ((filenames->tableSize==1) && outFileName)
1352*22ce4affSfengbojiang operationResult = FIO_compressFilename(fCtx, prefs, outFileName, filenames->fileNames[0], dictFileName, cLevel, compressionParams);
1353*22ce4affSfengbojiang else
1354*22ce4affSfengbojiang operationResult = FIO_compressMultipleFilenames(fCtx, prefs, filenames->fileNames, outMirroredDirName, outDirName, outFileName, suffix, dictFileName, cLevel, compressionParams);
1355*22ce4affSfengbojiang #else
1356*22ce4affSfengbojiang (void)contentSize; (void)suffix; (void)adapt; (void)rsyncable; (void)ultra; (void)cLevel; (void)ldmFlag; (void)literalCompressionMode; (void)targetCBlockSize; (void)streamSrcSize; (void)srcSizeHint; (void)ZSTD_strategyMap; /* not used when ZSTD_NOCOMPRESS set */
1357*22ce4affSfengbojiang DISPLAY("Compression not supported \n");
1358*22ce4affSfengbojiang #endif
1359*22ce4affSfengbojiang } else { /* decompression or test */
1360*22ce4affSfengbojiang #ifndef ZSTD_NODECOMPRESS
1361*22ce4affSfengbojiang if (filenames->tableSize == 1 && outFileName) {
1362*22ce4affSfengbojiang operationResult = FIO_decompressFilename(fCtx, prefs, outFileName, filenames->fileNames[0], dictFileName);
1363*22ce4affSfengbojiang } else {
1364*22ce4affSfengbojiang operationResult = FIO_decompressMultipleFilenames(fCtx, prefs, filenames->fileNames, outMirroredDirName, outDirName, outFileName, dictFileName);
1365*22ce4affSfengbojiang }
1366*22ce4affSfengbojiang #else
1367*22ce4affSfengbojiang DISPLAY("Decompression not supported \n");
1368*22ce4affSfengbojiang #endif
1369*22ce4affSfengbojiang }
1370*22ce4affSfengbojiang
1371*22ce4affSfengbojiang _end:
1372*22ce4affSfengbojiang FIO_freePreferences(prefs);
1373*22ce4affSfengbojiang FIO_freeContext(fCtx);
1374*22ce4affSfengbojiang if (main_pause) waitEnter();
1375*22ce4affSfengbojiang UTIL_freeFileNamesTable(filenames);
1376*22ce4affSfengbojiang UTIL_freeFileNamesTable(file_of_names);
1377*22ce4affSfengbojiang
1378*22ce4affSfengbojiang return operationResult;
1379*22ce4affSfengbojiang }
1380