blob: d2465456b565355d1d1819061f50996c7ca531e1 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Elliott Hughes44aba642023-09-12 20:18:59 +00002 * Copyright (c) Meta Platforms, Inc. and affiliates.
Yann Collet4ded9e52016-08-30 10:04:33 -07003 * All rights reserved.
4 *
Yann Collet32fb4072017-08-18 16:52:05 -07005 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Yann Collet4ded9e52016-08-30 10:04:33 -07009 */
Yann Collet5f53b032016-08-28 10:00:49 -070010
Yann Colletfcf20872016-05-29 05:16:05 +020011
Yann Colletfcf20872016-05-29 05:16:05 +020012/*-************************************
Yann Collet2cac5b32016-07-13 14:15:08 +020013* Tuning parameters
14**************************************/
Yann Collet2b1a3632016-07-13 15:16:00 +020015#ifndef ZSTDCLI_CLEVEL_DEFAULT
16# define ZSTDCLI_CLEVEL_DEFAULT 3
Yann Collet2cac5b32016-07-13 14:15:08 +020017#endif
18
Yann Colletfe0590f2016-08-12 18:04:15 +020019#ifndef ZSTDCLI_CLEVEL_MAX
Yann Collet1c108c82017-08-19 13:33:50 -070020# define ZSTDCLI_CLEVEL_MAX 19 /* without using --ultra */
Yann Colletfe0590f2016-08-12 18:04:15 +020021#endif
22
senhuang42a71963c2020-09-09 12:35:40 -040023#ifndef ZSTDCLI_NBTHREADS_DEFAULT
24# define ZSTDCLI_NBTHREADS_DEFAULT 1
senhuang42888c3852020-09-07 18:11:42 -040025#endif
Yann Collet8dafb1a2017-01-25 17:01:13 -080026
Yann Collet2cac5b32016-07-13 14:15:08 +020027/*-************************************
Yann Collet52afb392016-11-16 08:50:54 -080028* Dependencies
Yann Colletfcf20872016-05-29 05:16:05 +020029**************************************/
Elliott Hughes44aba642023-09-12 20:18:59 +000030#include "platform.h" /* PLATFORM_POSIX_VERSION */
31#include "util.h" /* UTIL_HAS_CREATEFILELIST, UTIL_createFileList, UTIL_isConsole */
Yi Jin5324b1e2018-12-19 13:26:27 -080032#include <stdlib.h> /* getenv */
Yann Colletfcf20872016-05-29 05:16:05 +020033#include <string.h> /* strcmp, strlen */
Yann Colleta49417b2019-12-02 14:28:18 -080034#include <stdio.h> /* fprintf(), stdin, stdout, stderr */
Yann Colletf9cac7a2016-07-04 18:16:16 +020035#include <errno.h> /* errno */
Yann Colleta49417b2019-12-02 14:28:18 -080036#include <assert.h> /* assert */
37
Yann Collet1c108c82017-08-19 13:33:50 -070038#include "fileio.h" /* stdinmark, stdoutmark, ZSTD_EXTENSION */
Yann Colletfcf20872016-05-29 05:16:05 +020039#ifndef ZSTD_NOBENCH
Elliott Hughes44aba642023-09-12 20:18:59 +000040# include "benchzstd.h" /* BMK_benchFilesAdvanced */
Yann Colletfcf20872016-05-29 05:16:05 +020041#endif
Yann Colletfcf20872016-05-29 05:16:05 +020042#ifndef ZSTD_NODICT
Yann Collet1c108c82017-08-19 13:33:50 -070043# include "dibio.h" /* ZDICT_cover_params_t, DiB_trainFromFiles() */
Yann Colletfcf20872016-05-29 05:16:05 +020044#endif
Nick Terrell54a49982021-02-03 19:53:00 -080045#ifndef ZSTD_NOTRACE
46# include "zstdcli_trace.h"
47#endif
W. Felix Handte7dcca6b2020-05-01 16:20:40 -040048#include "../lib/zstd.h" /* ZSTD_VERSION_STRING, ZSTD_minCLevel, ZSTD_maxCLevel */
Elliott Hughes44aba642023-09-12 20:18:59 +000049#include "fileio_asyncio.h"
Yann Colletfcf20872016-05-29 05:16:05 +020050
51
Yann Colletfcf20872016-05-29 05:16:05 +020052/*-************************************
Yann Colletfcf20872016-05-29 05:16:05 +020053* Constants
54**************************************/
Elliott Hughes44aba642023-09-12 20:18:59 +000055#define COMPRESSOR_NAME "Zstandard CLI"
Yann Colletfcf20872016-05-29 05:16:05 +020056#ifndef ZSTD_VERSION
57# define ZSTD_VERSION "v" ZSTD_VERSION_STRING
58#endif
59#define AUTHOR "Yann Collet"
Elliott Hughes44aba642023-09-12 20:18:59 +000060#define WELCOME_MESSAGE "*** %s (%i-bit) %s, by %s ***\n", COMPRESSOR_NAME, (int)(sizeof(size_t)*8), ZSTD_VERSION, AUTHOR
Yann Colletfcf20872016-05-29 05:16:05 +020061
Sean Purcell5a61f362017-04-14 11:33:04 -070062#define ZSTD_ZSTDMT "zstdmt"
Yann Colletfcf20872016-05-29 05:16:05 +020063#define ZSTD_UNZSTD "unzstd"
Yann Colletc09d16b2017-02-14 10:45:19 -080064#define ZSTD_CAT "zstdcat"
Yann Colletcb5eba82018-01-19 11:26:35 -080065#define ZSTD_ZCAT "zcat"
Yann Colletc09d16b2017-02-14 10:45:19 -080066#define ZSTD_GZ "gzip"
67#define ZSTD_GUNZIP "gunzip"
68#define ZSTD_GZCAT "gzcat"
Nick Terrellaa8bcf32017-03-13 18:11:07 -070069#define ZSTD_LZMA "lzma"
Nick Terrell6aeb50e2017-06-26 11:24:36 -070070#define ZSTD_UNLZMA "unlzma"
Nick Terrellaa8bcf32017-03-13 18:11:07 -070071#define ZSTD_XZ "xz"
Nick Terrell6aeb50e2017-06-26 11:24:36 -070072#define ZSTD_UNXZ "unxz"
W. Felix Handted0519d42017-09-28 19:18:15 -040073#define ZSTD_LZ4 "lz4"
74#define ZSTD_UNLZ4 "unlz4"
Yann Colletfcf20872016-05-29 05:16:05 +020075
76#define KB *(1 <<10)
77#define MB *(1 <<20)
78#define GB *(1U<<30)
79
Yann Collet1c108c82017-08-19 13:33:50 -070080#define DISPLAY_LEVEL_DEFAULT 2
David Lamac43e952016-08-12 14:49:05 -070081
Yann Colletfcf20872016-05-29 05:16:05 +020082static const char* g_defaultDictName = "dictionary";
83static const unsigned g_defaultMaxDictSize = 110 KB;
Yann Colletc8431422016-09-01 15:05:57 -070084static const int g_defaultDictCLevel = 3;
Yann Colletfcf20872016-05-29 05:16:05 +020085static const unsigned g_defaultSelectivityLevel = 9;
Nick Terrellc233bdb2017-09-22 14:04:39 -070086static const unsigned g_defaultMaxWindowLog = 27;
Yann Collet8d8513f2017-01-30 14:37:08 -080087#define OVERLAP_LOG_DEFAULT 9999
Stella Lau67d4a612017-09-02 21:10:36 -070088#define LDM_PARAM_DEFAULT 9999 /* Default for parameters where 0 is valid */
Yann Collet8d8513f2017-01-30 14:37:08 -080089static U32 g_overlapLog = OVERLAP_LOG_DEFAULT;
Stella Laua1f04d52017-09-01 14:52:51 -070090static U32 g_ldmHashLog = 0;
91static U32 g_ldmMinMatch = 0;
Yann Collet41c7d0b2018-11-21 14:36:57 -080092static U32 g_ldmHashRateLog = LDM_PARAM_DEFAULT;
Stella Lau67d4a612017-09-02 21:10:36 -070093static U32 g_ldmBucketSizeLog = LDM_PARAM_DEFAULT;
Yann Colletfcf20872016-05-29 05:16:05 +020094
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -070095
96#define DEFAULT_ACCEL 1
97
98typedef enum { cover, fastCover, legacy } dictType;
Yann Colletfcf20872016-05-29 05:16:05 +020099
100/*-************************************
101* Display Macros
102**************************************/
Yann Collet4b4de742019-12-02 16:08:08 -0800103#define DISPLAY_F(f, ...) fprintf((f), __VA_ARGS__)
104#define DISPLAYOUT(...) DISPLAY_F(stdout, __VA_ARGS__)
105#define DISPLAY(...) DISPLAY_F(stderr, __VA_ARGS__)
Sean Purcell042ba122017-03-23 11:13:52 -0700106#define DISPLAYLEVEL(l, ...) { if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } }
Yann Collet1c108c82017-08-19 13:33:50 -0700107static int g_displayLevel = DISPLAY_LEVEL_DEFAULT; /* 0 : no display, 1: errors, 2 : + result + interaction + warnings, 3 : + progression, 4 : + information */
Yann Colletfcf20872016-05-29 05:16:05 +0200108
109
110/*-************************************
Yann Collet3324e872021-01-07 10:37:27 -0800111* Check Version (when CLI linked to dynamic library)
112**************************************/
113
114/* Due to usage of experimental symbols and capabilities by the CLI,
115 * the CLI must be linked against a dynamic library of same version */
116static void checkLibVersion(void)
117{
118 if (strcmp(ZSTD_VERSION_STRING, ZSTD_versionString())) {
119 DISPLAYLEVEL(1, "Error : incorrect library version (expecting : %s ; actual : %s ) \n",
120 ZSTD_VERSION_STRING, ZSTD_versionString());
121 DISPLAYLEVEL(1, "Please update library to version %s, or use stand-alone zstd binary \n",
122 ZSTD_VERSION_STRING);
123 exit(1);
124 }
125}
126
127
Elliott Hughes44aba642023-09-12 20:18:59 +0000128/*! exeNameMatch() :
129 @return : a non-zero value if exeName matches test, excluding the extension
130 */
131static int exeNameMatch(const char* exeName, const char* test)
132{
133 return !strncmp(exeName, test, strlen(test)) &&
134 (exeName[strlen(test)] == '\0' || exeName[strlen(test)] == '.');
135}
136
Yann Collet3324e872021-01-07 10:37:27 -0800137/*-************************************
Yann Colletfcf20872016-05-29 05:16:05 +0200138* Command Line
139**************************************/
Yann Collet4b4de742019-12-02 16:08:08 -0800140/* print help either in `stderr` or `stdout` depending on originating request
141 * error (badusage) => stderr
142 * help (usage_advanced) => stdout
143 */
144static void usage(FILE* f, const char* programName)
Yann Colletfcf20872016-05-29 05:16:05 +0200145{
Elliott Hughes44aba642023-09-12 20:18:59 +0000146 DISPLAY_F(f, "Compress or decompress the INPUT file(s); reads from STDIN if INPUT is `-` or not provided.\n\n");
147 DISPLAY_F(f, "Usage: %s [OPTIONS...] [INPUT... | -] [-o OUTPUT]\n\n", programName);
148 DISPLAY_F(f, "Options:\n");
149 DISPLAY_F(f, " -o OUTPUT Write output to a single file, OUTPUT.\n");
150 DISPLAY_F(f, " -k, --keep Preserve INPUT file(s). [Default] \n");
151 DISPLAY_F(f, " --rm Remove INPUT file(s) after successful (de)compression.\n");
152#ifdef ZSTD_GZCOMPRESS
153 if (exeNameMatch(programName, ZSTD_GZ)) { /* behave like gzip */
154 DISPLAY_F(f, " -n, --no-name Do not store original filename when compressing.\n\n");
155 }
156#endif
Jon Palmisciano678335c2022-12-20 15:34:02 -0500157 DISPLAY_F(f, "\n");
158#ifndef ZSTD_NOCOMPRESS
Elliott Hughes44aba642023-09-12 20:18:59 +0000159 DISPLAY_F(f, " -# Desired compression level, where `#` is a number between 1 and %d;\n", ZSTDCLI_CLEVEL_MAX);
160 DISPLAY_F(f, " lower numbers provide faster compression, higher numbers yield\n");
161 DISPLAY_F(f, " better compression ratios. [Default: %d]\n\n", ZSTDCLI_CLEVEL_DEFAULT);
Jon Palmisciano678335c2022-12-20 15:34:02 -0500162#endif
163#ifndef ZSTD_NODECOMPRESS
Elliott Hughes44aba642023-09-12 20:18:59 +0000164 DISPLAY_F(f, " -d, --decompress Perform decompression.\n");
Jon Palmisciano678335c2022-12-20 15:34:02 -0500165#endif
Elliott Hughes44aba642023-09-12 20:18:59 +0000166 DISPLAY_F(f, " -D DICT Use DICT as the dictionary for compression or decompression.\n\n");
167 DISPLAY_F(f, " -f, --force Disable input and output checks. Allows overwriting existing files,\n");
168 DISPLAY_F(f, " receiving input from the console, printing output to STDOUT, and\n");
169 DISPLAY_F(f, " operating on links, block devices, etc. Unrecognized formats will be\n");
170 DISPLAY_F(f, " passed-through through as-is.\n\n");
171
172 DISPLAY_F(f, " -h Display short usage and exit.\n");
173 DISPLAY_F(f, " -H, --help Display full help and exit.\n");
174 DISPLAY_F(f, " -V, --version Display the program version and exit.\n");
175 DISPLAY_F(f, "\n");
Yann Colletfcf20872016-05-29 05:16:05 +0200176}
177
Yann Collet4b4de742019-12-02 16:08:08 -0800178static void usage_advanced(const char* programName)
Yann Colletfcf20872016-05-29 05:16:05 +0200179{
Yann Collet4b4de742019-12-02 16:08:08 -0800180 DISPLAYOUT(WELCOME_MESSAGE);
Elliott Hughes44aba642023-09-12 20:18:59 +0000181 DISPLAYOUT("\n");
Yann Collet4b4de742019-12-02 16:08:08 -0800182 usage(stdout, programName);
Elliott Hughes44aba642023-09-12 20:18:59 +0000183 DISPLAYOUT("Advanced options:\n");
184 DISPLAYOUT(" -c, --stdout Write to STDOUT (even if it is a console) and keep the INPUT file(s).\n\n");
Yann Collete5366be2020-05-08 14:14:46 -0700185
Elliott Hughes44aba642023-09-12 20:18:59 +0000186 DISPLAYOUT(" -v, --verbose Enable verbose output; pass multiple times to increase verbosity.\n");
187 DISPLAYOUT(" -q, --quiet Suppress warnings; pass twice to suppress errors.\n");
188#ifndef ZSTD_NOTRACE
189 DISPLAYOUT(" --trace LOG Log tracing information to LOG.\n");
190#endif
191 DISPLAYOUT("\n");
192 DISPLAYOUT(" --[no-]progress Forcibly show/hide the progress counter. NOTE: Any (de)compressed\n");
193 DISPLAYOUT(" output to terminal will mix with progress counter text.\n\n");
Yann Collete5366be2020-05-08 14:14:46 -0700194
195#ifdef UTIL_HAS_CREATEFILELIST
Elliott Hughes44aba642023-09-12 20:18:59 +0000196 DISPLAYOUT(" -r Operate recursively on directories.\n");
197 DISPLAYOUT(" --filelist LIST Read a list of files to operate on from LIST.\n");
198 DISPLAYOUT(" --output-dir-flat DIR Store processed files in DIR.\n");
Yann Collete5366be2020-05-08 14:14:46 -0700199#endif
200
Xin Xie9a8ccd42020-06-19 19:35:51 -0700201#ifdef UTIL_HAS_MIRRORFILELIST
Elliott Hughes44aba642023-09-12 20:18:59 +0000202 DISPLAYOUT(" --output-dir-mirror DIR Store processed files in DIR, respecting original directory structure.\n");
Xin Xie9a8ccd42020-06-19 19:35:51 -0700203#endif
Elliott Hughes44aba642023-09-12 20:18:59 +0000204 if (AIO_supported())
205 DISPLAYOUT(" --[no-]asyncio Use asynchronous IO. [Default: Enabled]\n");
senhuang4244c54a32020-08-24 16:14:19 -0400206
Elliott Hughes44aba642023-09-12 20:18:59 +0000207 DISPLAYOUT("\n");
senhuang4244c54a32020-08-24 16:14:19 -0400208#ifndef ZSTD_NOCOMPRESS
Elliott Hughes44aba642023-09-12 20:18:59 +0000209 DISPLAYOUT(" --[no-]check Add XXH64 integrity checksums during compression. [Default: Add, Validate]\n");
senhuang4244c54a32020-08-24 16:14:19 -0400210#ifndef ZSTD_NODECOMPRESS
Elliott Hughes44aba642023-09-12 20:18:59 +0000211 DISPLAYOUT(" If `-d` is present, ignore/validate checksums during decompression.\n");
senhuang4244c54a32020-08-24 16:14:19 -0400212#endif
senhuang42e1e41ad2020-08-24 17:36:36 -0400213#else
senhuang4244c54a32020-08-24 16:14:19 -0400214#ifdef ZSTD_NOCOMPRESS
Elliott Hughes44aba642023-09-12 20:18:59 +0000215 DISPLAYOUT(" --[no-]check Ignore/validate checksums during decompression. [Default: Validate]");
senhuang4244c54a32020-08-24 16:14:19 -0400216#endif
senhuang42e1e41ad2020-08-24 17:36:36 -0400217#endif /* ZSTD_NOCOMPRESS */
Nick Terrell54a49982021-02-03 19:53:00 -0800218
Elliott Hughes44aba642023-09-12 20:18:59 +0000219 DISPLAYOUT("\n");
220 DISPLAYOUT(" -- Treat remaining arguments after `--` as files.\n");
Yann Collete5366be2020-05-08 14:14:46 -0700221
Yann Colletfcf20872016-05-29 05:16:05 +0200222#ifndef ZSTD_NOCOMPRESS
Elliott Hughes44aba642023-09-12 20:18:59 +0000223 DISPLAYOUT("\n");
224 DISPLAYOUT("Advanced compression options:\n");
225 DISPLAYOUT(" --ultra Enable levels beyond %i, up to %i; requires more memory.\n", ZSTDCLI_CLEVEL_MAX, ZSTD_maxCLevel());
226 DISPLAYOUT(" --fast[=#] Use to very fast compression levels. [Default: %u]\n", 1);
Przemyslaw Skibinskicfd4dc22017-02-08 15:17:55 +0100227#ifdef ZSTD_GZCOMPRESS
Elliott Hughes44aba642023-09-12 20:18:59 +0000228 if (exeNameMatch(programName, ZSTD_GZ)) { /* behave like gzip */
229 DISPLAYOUT(" --best Compatibility alias for `-9`.\n");
230 }
231#endif
232 DISPLAYOUT(" --adapt Dynamically adapt compression level to I/O conditions.\n");
233 DISPLAYOUT(" --long[=#] Enable long distance matching with window log #. [Default: %u]\n", g_defaultMaxWindowLog);
234 DISPLAYOUT(" --patch-from=REF Use REF as the reference point for Zstandard's diff engine. \n\n");
235# ifdef ZSTD_MULTITHREAD
236 DISPLAYOUT(" -T# Spawn # compression threads. [Default: 1; pass 0 for core count.]\n");
237 DISPLAYOUT(" --single-thread Share a single thread for I/O and compression (slightly different than `-T1`).\n");
238 DISPLAYOUT(" --auto-threads={physical|logical}\n");
239 DISPLAYOUT(" Use physical/logical cores when using `-T0`. [Default: Physical]\n\n");
240 DISPLAYOUT(" -B# Set job size to #. [Default: 0 (automatic)]\n");
241 DISPLAYOUT(" --rsyncable Compress using a rsync-friendly method (`-B` sets block size). \n");
242 DISPLAYOUT("\n");
243# endif
244 DISPLAYOUT(" --exclude-compressed Only compress files that are not already compressed.\n\n");
245
246 DISPLAYOUT(" --stream-size=# Specify size of streaming input from STDIN.\n");
247 DISPLAYOUT(" --size-hint=# Optimize compression parameters for streaming input of approximately size #.\n");
248 DISPLAYOUT(" --target-compressed-block-size=#\n");
249 DISPLAYOUT(" Generate compressed blocks of approximately # size.\n\n");
250 DISPLAYOUT(" --no-dictID Don't write `dictID` into the header (dictionary compression only).\n");
251 DISPLAYOUT(" --[no-]compress-literals Force (un)compressed literals.\n");
252 DISPLAYOUT(" --[no-]row-match-finder Explicitly enable/disable the fast, row-based matchfinder for\n");
253 DISPLAYOUT(" the 'greedy', 'lazy', and 'lazy2' strategies.\n");
254
255 DISPLAYOUT("\n");
256 DISPLAYOUT(" --format=zstd Compress files to the `.zst` format. [Default]\n");
257 DISPLAYOUT(" --mmap-dict Memory-map dictionary file rather than mallocing and loading all at once");
258#ifdef ZSTD_GZCOMPRESS
259 DISPLAYOUT(" --format=gzip Compress files to the `.gz` format.\n");
Przemyslaw Skibinskicfd4dc22017-02-08 15:17:55 +0100260#endif
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700261#ifdef ZSTD_LZMACOMPRESS
Elliott Hughes44aba642023-09-12 20:18:59 +0000262 DISPLAYOUT(" --format=xz Compress files to the `.xz` format.\n");
263 DISPLAYOUT(" --format=lzma Compress files to the `.lzma` format.\n");
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700264#endif
Sean Purcell4de86322017-04-24 16:48:25 -0700265#ifdef ZSTD_LZ4COMPRESS
Elliott Hughes44aba642023-09-12 20:18:59 +0000266 DISPLAYOUT( " --format=lz4 Compress files to the `.lz4` format.\n");
Sean Purcell4de86322017-04-24 16:48:25 -0700267#endif
Yann Collete5366be2020-05-08 14:14:46 -0700268#endif /* !ZSTD_NOCOMPRESS */
269
Yann Collet6381e992016-05-31 02:29:45 +0200270#ifndef ZSTD_NODECOMPRESS
Elliott Hughes44aba642023-09-12 20:18:59 +0000271 DISPLAYOUT("\n");
272 DISPLAYOUT("Advanced decompression options:\n");
273 DISPLAYOUT(" -l Print information about Zstandard-compressed files.\n");
274 DISPLAYOUT(" --test Test compressed file integrity.\n");
275 DISPLAYOUT(" -M# Set the memory usage limit to # megabytes.\n");
Yann Collete5366be2020-05-08 14:14:46 -0700276# if ZSTD_SPARSE_DEFAULT
Elliott Hughes44aba642023-09-12 20:18:59 +0000277 DISPLAYOUT(" --[no-]sparse Enable sparse mode. [Default: Enabled for files, disabled for STDOUT.]\n");
Yann Collete5366be2020-05-08 14:14:46 -0700278# else
Elliott Hughes44aba642023-09-12 20:18:59 +0000279 DISPLAYOUT(" --[no-]sparse Enable sparse mode. [Default: Disabled]\n");
Yann Collete5366be2020-05-08 14:14:46 -0700280# endif
Elliott Hughes44aba642023-09-12 20:18:59 +0000281 {
282 char const* passThroughDefault = "Disabled";
283 if (exeNameMatch(programName, ZSTD_CAT) ||
284 exeNameMatch(programName, ZSTD_ZCAT) ||
285 exeNameMatch(programName, ZSTD_GZCAT)) {
286 passThroughDefault = "Enabled";
287 }
288 DISPLAYOUT(" --[no-]pass-through Pass through uncompressed files as-is. [Default: %s]\n", passThroughDefault);
289 }
Yann Collete5366be2020-05-08 14:14:46 -0700290#endif /* ZSTD_NODECOMPRESS */
291
Yann Colletfcf20872016-05-29 05:16:05 +0200292#ifndef ZSTD_NODICT
Elliott Hughes44aba642023-09-12 20:18:59 +0000293 DISPLAYOUT("\n");
294 DISPLAYOUT("Dictionary builder:\n");
295 DISPLAYOUT(" --train Create a dictionary from a training set of files.\n\n");
296 DISPLAYOUT(" --train-cover[=k=#,d=#,steps=#,split=#,shrink[=#]]\n");
297 DISPLAYOUT(" Use the cover algorithm (with optional arguments).\n");
298 DISPLAYOUT(" --train-fastcover[=k=#,d=#,f=#,steps=#,split=#,accel=#,shrink[=#]]\n");
299 DISPLAYOUT(" Use the fast cover algorithm (with optional arguments).\n\n");
300 DISPLAYOUT(" --train-legacy[=s=#] Use the legacy algorithm with selectivity #. [Default: %u]\n", g_defaultSelectivityLevel);
301 DISPLAYOUT(" -o NAME Use NAME as dictionary name. [Default: %s]\n", g_defaultDictName);
302 DISPLAYOUT(" --maxdict=# Limit dictionary to specified size #. [Default: %u]\n", g_defaultMaxDictSize);
303 DISPLAYOUT(" --dictID=# Force dictionary ID to #. [Default: Random]\n");
Yann Colletfcf20872016-05-29 05:16:05 +0200304#endif
Yann Collete5366be2020-05-08 14:14:46 -0700305
Yann Colletfcf20872016-05-29 05:16:05 +0200306#ifndef ZSTD_NOBENCH
Elliott Hughes44aba642023-09-12 20:18:59 +0000307 DISPLAYOUT("\n");
308 DISPLAYOUT("Benchmark options:\n");
309 DISPLAYOUT(" -b# Perform benchmarking with compression level #. [Default: %d]\n", ZSTDCLI_CLEVEL_DEFAULT);
310 DISPLAYOUT(" -e# Test all compression levels up to #; starting level is `-b#`. [Default: 1]\n");
311 DISPLAYOUT(" -i# Set the minimum evaluation to time # seconds. [Default: 3]\n");
312 DISPLAYOUT(" -B# Cut file into independent chunks of size #. [Default: No chunking]\n");
313 DISPLAYOUT(" -S Output one benchmark result per input file. [Default: Consolidated result]\n");
314 DISPLAYOUT(" --priority=rt Set process priority to real-time.\n");
Yann Colletfcf20872016-05-29 05:16:05 +0200315#endif
Yann Collete5366be2020-05-08 14:14:46 -0700316
Yann Colletfcf20872016-05-29 05:16:05 +0200317}
318
Yann Collet4b4de742019-12-02 16:08:08 -0800319static void badusage(const char* programName)
Yann Colletfcf20872016-05-29 05:16:05 +0200320{
Yann Collet4b4de742019-12-02 16:08:08 -0800321 DISPLAYLEVEL(1, "Incorrect parameters \n");
322 if (g_displayLevel >= 2) usage(stderr, programName);
Yann Colletfcf20872016-05-29 05:16:05 +0200323}
324
Yann Colletfcf20872016-05-29 05:16:05 +0200325static void waitEnter(void)
326{
327 int unused;
Yann Collet4b4de742019-12-02 16:08:08 -0800328 DISPLAY("Press enter to continue... \n");
Yann Colletfcf20872016-05-29 05:16:05 +0200329 unused = getchar();
330 (void)unused;
331}
332
Sean Purcellef30af82017-03-24 17:06:09 -0700333static const char* lastNameFromPath(const char* path)
334{
335 const char* name = path;
336 if (strrchr(name, '/')) name = strrchr(name, '/') + 1;
337 if (strrchr(name, '\\')) name = strrchr(name, '\\') + 1; /* windows */
338 return name;
339}
340
Yann Collet9cd5c632018-05-12 14:29:33 -0700341static void errorOut(const char* msg)
342{
Elliott Hughes44aba642023-09-12 20:18:59 +0000343 DISPLAYLEVEL(1, "%s \n", msg); exit(1);
Yann Collet9cd5c632018-05-12 14:29:33 -0700344}
345
Yi Jin9b2d7082018-12-19 23:41:18 -0800346/*! readU32FromCharChecked() :
Yi Jin26a9ae32018-12-19 16:45:42 -0800347 * @return 0 if success, and store the result in *value.
Yann Collet6a9b41b2018-03-11 19:56:48 -0700348 * allows and interprets K, KB, KiB, M, MB and MiB suffix.
349 * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
Yi Jin26a9ae32018-12-19 16:45:42 -0800350 * @return 1 if an overflow error occurs */
Yi Jin9b2d7082018-12-19 23:41:18 -0800351static int readU32FromCharChecked(const char** stringPtr, unsigned* value)
Yann Collet1dd79612016-06-03 15:14:09 +0200352{
353 unsigned result = 0;
Yann Collet9cd5c632018-05-12 14:29:33 -0700354 while ((**stringPtr >='0') && (**stringPtr <='9')) {
Philip Jones4e728e22020-02-18 15:30:59 -0800355 unsigned const max = ((unsigned)(-1)) / 10;
356 unsigned last = result;
Yann Collet90c04622019-04-10 10:03:06 -0700357 if (result > max) return 1; /* overflow error */
358 result *= 10;
359 result += (unsigned)(**stringPtr - '0');
Philip Jones4e728e22020-02-18 15:30:59 -0800360 if (result < last) return 1; /* overflow error */
Yann Collet90c04622019-04-10 10:03:06 -0700361 (*stringPtr)++ ;
Yann Collet9cd5c632018-05-12 14:29:33 -0700362 }
Yann Collet33fdd092016-10-17 17:48:48 -0700363 if ((**stringPtr=='K') || (**stringPtr=='M')) {
Yann Collet9cd5c632018-05-12 14:29:33 -0700364 unsigned const maxK = ((unsigned)(-1)) >> 10;
Yann Collet90c04622019-04-10 10:03:06 -0700365 if (result > maxK) return 1; /* overflow error */
Yann Collet33fdd092016-10-17 17:48:48 -0700366 result <<= 10;
Yann Collet9cd5c632018-05-12 14:29:33 -0700367 if (**stringPtr=='M') {
Yann Collet90c04622019-04-10 10:03:06 -0700368 if (result > maxK) return 1; /* overflow error */
Yann Collet9cd5c632018-05-12 14:29:33 -0700369 result <<= 10;
370 }
371 (*stringPtr)++; /* skip `K` or `M` */
Yann Collet33fdd092016-10-17 17:48:48 -0700372 if (**stringPtr=='i') (*stringPtr)++;
373 if (**stringPtr=='B') (*stringPtr)++;
374 }
Yi Jin26a9ae32018-12-19 16:45:42 -0800375 *value = result;
376 return 0;
377}
378
379/*! readU32FromChar() :
380 * @return : unsigned integer value read from input in `char` format.
381 * allows and interprets K, KB, KiB, M, MB and MiB suffix.
382 * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
383 * Note : function will exit() program if digit sequence overflows */
384static unsigned readU32FromChar(const char** stringPtr) {
Lehman Garrison834d06b2020-02-20 14:29:58 -0500385 static const char errorMsg[] = "error: numeric value overflows 32-bit unsigned int";
Yi Jin26a9ae32018-12-19 16:45:42 -0800386 unsigned result;
Yi Jin9b2d7082018-12-19 23:41:18 -0800387 if (readU32FromCharChecked(stringPtr, &result)) { errorOut(errorMsg); }
Yann Collet1dd79612016-06-03 15:14:09 +0200388 return result;
389}
390
Binh Vo6fad35c2021-06-09 16:49:36 -0400391/*! readIntFromChar() :
392 * @return : signed integer value read from input in `char` format.
393 * allows and interprets K, KB, KiB, M, MB and MiB suffix.
394 * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
395 * Note : function will exit() program if digit sequence overflows */
396static int readIntFromChar(const char** stringPtr) {
397 static const char errorMsg[] = "error: numeric value overflows 32-bit int";
398 int sign = 1;
399 unsigned result;
400 if (**stringPtr=='-') {
401 (*stringPtr)++;
402 sign = -1;
403 }
404 if (readU32FromCharChecked(stringPtr, &result)) { errorOut(errorMsg); }
405 return (int) result * sign;
406}
407
Lehman Garrison834d06b2020-02-20 14:29:58 -0500408/*! readSizeTFromCharChecked() :
409 * @return 0 if success, and store the result in *value.
410 * allows and interprets K, KB, KiB, M, MB and MiB suffix.
411 * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
412 * @return 1 if an overflow error occurs */
413static int readSizeTFromCharChecked(const char** stringPtr, size_t* value)
414{
415 size_t result = 0;
416 while ((**stringPtr >='0') && (**stringPtr <='9')) {
417 size_t const max = ((size_t)(-1)) / 10;
418 size_t last = result;
419 if (result > max) return 1; /* overflow error */
420 result *= 10;
421 result += (size_t)(**stringPtr - '0');
422 if (result < last) return 1; /* overflow error */
423 (*stringPtr)++ ;
424 }
425 if ((**stringPtr=='K') || (**stringPtr=='M')) {
426 size_t const maxK = ((size_t)(-1)) >> 10;
427 if (result > maxK) return 1; /* overflow error */
428 result <<= 10;
429 if (**stringPtr=='M') {
430 if (result > maxK) return 1; /* overflow error */
431 result <<= 10;
432 }
433 (*stringPtr)++; /* skip `K` or `M` */
434 if (**stringPtr=='i') (*stringPtr)++;
435 if (**stringPtr=='B') (*stringPtr)++;
436 }
437 *value = result;
438 return 0;
439}
440
441/*! readSizeTFromChar() :
442 * @return : size_t value read from input in `char` format.
443 * allows and interprets K, KB, KiB, M, MB and MiB suffix.
444 * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
445 * Note : function will exit() program if digit sequence overflows */
446static size_t readSizeTFromChar(const char** stringPtr) {
447 static const char errorMsg[] = "error: numeric value overflows size_t";
448 size_t result;
449 if (readSizeTFromCharChecked(stringPtr, &result)) { errorOut(errorMsg); }
450 return result;
451}
452
Yann Collet743b33f2016-12-02 15:18:57 -0800453/** longCommandWArg() :
Yann Collet6ccd37c2017-01-30 13:07:24 -0800454 * check if *stringPtr is the same as longCommand.
Yann Collet743b33f2016-12-02 15:18:57 -0800455 * If yes, @return 1 and advances *stringPtr to the position which immediately follows longCommand.
Yann Collet6a9b41b2018-03-11 19:56:48 -0700456 * @return 0 and doesn't modify *stringPtr otherwise.
Yann Collet743b33f2016-12-02 15:18:57 -0800457 */
Yann Collet7f86ae22019-10-17 15:27:25 -0700458static int longCommandWArg(const char** stringPtr, const char* longCommand)
Yann Collet70077bc2016-10-14 14:41:17 -0700459{
460 size_t const comSize = strlen(longCommand);
Yann Collet743b33f2016-12-02 15:18:57 -0800461 int const result = !strncmp(*stringPtr, longCommand, comSize);
Yann Collet70077bc2016-10-14 14:41:17 -0700462 if (result) *stringPtr += comSize;
463 return result;
464}
465
Przemyslaw Skibinski671f28d2016-12-13 12:18:07 +0100466
Nick Terrelldf8415c2016-12-31 21:08:24 -0800467#ifndef ZSTD_NODICT
Qin Li04a9d6b2019-07-18 11:44:59 -0700468
469static const unsigned kDefaultRegression = 1;
Nick Terrelldf8415c2016-12-31 21:08:24 -0800470/**
471 * parseCoverParameters() :
Nick Terrellf376d472017-05-01 23:40:20 -0700472 * reads cover parameters from *stringPtr (e.g. "--train-cover=k=48,d=8,steps=32") into *params
Nick Terrelldf8415c2016-12-31 21:08:24 -0800473 * @return 1 means that cover parameters were correct
474 * @return 0 in case of malformed parameters
475 */
Nick Terrell5b7fd7c2017-06-26 21:07:14 -0700476static unsigned parseCoverParameters(const char* stringPtr, ZDICT_cover_params_t* params)
Nick Terrelldf8415c2016-12-31 21:08:24 -0800477{
478 memset(params, 0, sizeof(*params));
479 for (; ;) {
Nick Terrell3a1fefc2017-01-02 12:40:43 -0800480 if (longCommandWArg(&stringPtr, "k=")) { params->k = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
Nick Terrelldf8415c2016-12-31 21:08:24 -0800481 if (longCommandWArg(&stringPtr, "d=")) { params->d = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
Nick Terrell3a1fefc2017-01-02 12:40:43 -0800482 if (longCommandWArg(&stringPtr, "steps=")) { params->steps = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
Jennifer Liu348e5f72018-06-29 17:54:41 -0700483 if (longCommandWArg(&stringPtr, "split=")) {
Jennifer Liu08811842018-07-03 17:53:27 -0700484 unsigned splitPercentage = readU32FromChar(&stringPtr);
Jennifer Liu348e5f72018-06-29 17:54:41 -0700485 params->splitPoint = (double)splitPercentage / 100.0;
486 if (stringPtr[0]==',') { stringPtr++; continue; } else break;
487 }
Tyler-Tranc55d2e72019-06-27 16:26:57 -0700488 if (longCommandWArg(&stringPtr, "shrink")) {
489 params->shrinkDictMaxRegression = kDefaultRegression;
490 params->shrinkDict = 1;
491 if (stringPtr[0]=='=') {
492 stringPtr++;
493 params->shrinkDictMaxRegression = readU32FromChar(&stringPtr);
494 }
495 if (stringPtr[0]==',') {
496 stringPtr++;
497 continue;
498 }
499 else break;
500 }
Nick Terrelldf8415c2016-12-31 21:08:24 -0800501 return 0;
502 }
503 if (stringPtr[0] != 0) return 0;
Tyler-Tranc55d2e72019-06-27 16:26:57 -0700504 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);
Nick Terrelldf8415c2016-12-31 21:08:24 -0800505 return 1;
506}
Nick Terrellf376d472017-05-01 23:40:20 -0700507
508/**
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700509 * parseFastCoverParameters() :
510 * reads fastcover parameters from *stringPtr (e.g. "--train-fastcover=k=48,d=8,f=20,steps=32,accel=2") into *params
511 * @return 1 means that fastcover parameters were correct
512 * @return 0 in case of malformed parameters
513 */
514static unsigned parseFastCoverParameters(const char* stringPtr, ZDICT_fastCover_params_t* params)
515{
516 memset(params, 0, sizeof(*params));
517 for (; ;) {
518 if (longCommandWArg(&stringPtr, "k=")) { params->k = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
519 if (longCommandWArg(&stringPtr, "d=")) { params->d = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
520 if (longCommandWArg(&stringPtr, "f=")) { params->f = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
521 if (longCommandWArg(&stringPtr, "steps=")) { params->steps = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
522 if (longCommandWArg(&stringPtr, "accel=")) { params->accel = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
523 if (longCommandWArg(&stringPtr, "split=")) {
524 unsigned splitPercentage = readU32FromChar(&stringPtr);
525 params->splitPoint = (double)splitPercentage / 100.0;
526 if (stringPtr[0]==',') { stringPtr++; continue; } else break;
527 }
Tyler-Tranc55d2e72019-06-27 16:26:57 -0700528 if (longCommandWArg(&stringPtr, "shrink")) {
529 params->shrinkDictMaxRegression = kDefaultRegression;
530 params->shrinkDict = 1;
531 if (stringPtr[0]=='=') {
532 stringPtr++;
533 params->shrinkDictMaxRegression = readU32FromChar(&stringPtr);
534 }
535 if (stringPtr[0]==',') {
536 stringPtr++;
537 continue;
538 }
539 else break;
540 }
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700541 return 0;
542 }
543 if (stringPtr[0] != 0) return 0;
Tyler-Tranc55d2e72019-06-27 16:26:57 -0700544 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);
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700545 return 1;
546}
547
548/**
Nick Terrellf376d472017-05-01 23:40:20 -0700549 * parseLegacyParameters() :
Josh Sorefa880ca22019-04-12 14:18:11 -0400550 * reads legacy dictionary builder parameters from *stringPtr (e.g. "--train-legacy=selectivity=8") into *selectivity
Nick Terrellf376d472017-05-01 23:40:20 -0700551 * @return 1 means that legacy dictionary builder parameters were correct
552 * @return 0 in case of malformed parameters
553 */
554static unsigned parseLegacyParameters(const char* stringPtr, unsigned* selectivity)
555{
556 if (!longCommandWArg(&stringPtr, "s=") && !longCommandWArg(&stringPtr, "selectivity=")) { return 0; }
557 *selectivity = readU32FromChar(&stringPtr);
558 if (stringPtr[0] != 0) return 0;
559 DISPLAYLEVEL(4, "legacy: selectivity=%u\n", *selectivity);
560 return 1;
561}
562
Nick Terrell5b7fd7c2017-06-26 21:07:14 -0700563static ZDICT_cover_params_t defaultCoverParams(void)
Nick Terrellf376d472017-05-01 23:40:20 -0700564{
Nick Terrell5b7fd7c2017-06-26 21:07:14 -0700565 ZDICT_cover_params_t params;
Nick Terrellf376d472017-05-01 23:40:20 -0700566 memset(&params, 0, sizeof(params));
567 params.d = 8;
568 params.steps = 4;
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700569 params.splitPoint = 1.0;
Tyler-Tranc55d2e72019-06-27 16:26:57 -0700570 params.shrinkDict = 0;
571 params.shrinkDictMaxRegression = kDefaultRegression;
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700572 return params;
573}
574
575static ZDICT_fastCover_params_t defaultFastCoverParams(void)
576{
577 ZDICT_fastCover_params_t params;
578 memset(&params, 0, sizeof(params));
579 params.d = 8;
Jennifer Liu21721b72018-09-04 17:12:35 -0700580 params.f = 20;
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700581 params.steps = 4;
582 params.splitPoint = 0.75; /* different from default splitPoint of cover */
583 params.accel = DEFAULT_ACCEL;
Tyler-Tranc55d2e72019-06-27 16:26:57 -0700584 params.shrinkDict = 0;
585 params.shrinkDictMaxRegression = kDefaultRegression;
Nick Terrellf376d472017-05-01 23:40:20 -0700586 return params;
587}
Nick Terrelldf8415c2016-12-31 21:08:24 -0800588#endif
Yann Collet6ccd37c2017-01-30 13:07:24 -0800589
590
Yann Collet6c51bf42018-09-24 18:16:08 -0700591/** parseAdaptParameters() :
592 * reads adapt parameters from *stringPtr (e.g. "--zstd=min=1,max=19) and store them into adaptMinPtr and adaptMaxPtr.
593 * Both adaptMinPtr and adaptMaxPtr must be already allocated and correctly initialized.
594 * There is no guarantee that any of these values will be updated.
595 * @return 1 means that parsing was successful,
596 * @return 0 in case of malformed parameters
597 */
598static unsigned parseAdaptParameters(const char* stringPtr, int* adaptMinPtr, int* adaptMaxPtr)
599{
600 for ( ; ;) {
Binh Vo6fad35c2021-06-09 16:49:36 -0400601 if (longCommandWArg(&stringPtr, "min=")) { *adaptMinPtr = readIntFromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
602 if (longCommandWArg(&stringPtr, "max=")) { *adaptMaxPtr = readIntFromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
Yann Collet6c51bf42018-09-24 18:16:08 -0700603 DISPLAYLEVEL(4, "invalid compression parameter \n");
604 return 0;
605 }
606 if (stringPtr[0] != 0) return 0; /* check the end of string */
607 if (*adaptMinPtr > *adaptMaxPtr) {
608 DISPLAYLEVEL(4, "incoherent adaptation limits \n");
609 return 0;
610 }
611 return 1;
612}
613
614
Przemyslaw Skibinski671f28d2016-12-13 12:18:07 +0100615/** parseCompressionParameters() :
Yann Collete874dac2018-11-20 14:56:07 -0800616 * reads compression parameters from *stringPtr (e.g. "--zstd=wlog=23,clog=23,hlog=22,slog=6,mml=3,tlen=48,strat=6") into *params
Przemyslaw Skibinski671f28d2016-12-13 12:18:07 +0100617 * @return 1 means that compression parameters were correct
618 * @return 0 in case of malformed parameters
619 */
620static unsigned parseCompressionParameters(const char* stringPtr, ZSTD_compressionParameters* params)
621{
622 for ( ; ;) {
Przemyslaw Skibinski671f28d2016-12-13 12:18:07 +0100623 if (longCommandWArg(&stringPtr, "windowLog=") || longCommandWArg(&stringPtr, "wlog=")) { params->windowLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
624 if (longCommandWArg(&stringPtr, "chainLog=") || longCommandWArg(&stringPtr, "clog=")) { params->chainLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
625 if (longCommandWArg(&stringPtr, "hashLog=") || longCommandWArg(&stringPtr, "hlog=")) { params->hashLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
626 if (longCommandWArg(&stringPtr, "searchLog=") || longCommandWArg(&stringPtr, "slog=")) { params->searchLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
Yann Collete874dac2018-11-20 14:56:07 -0800627 if (longCommandWArg(&stringPtr, "minMatch=") || longCommandWArg(&stringPtr, "mml=")) { params->minMatch = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
Przemyslaw Skibinski671f28d2016-12-13 12:18:07 +0100628 if (longCommandWArg(&stringPtr, "targetLength=") || longCommandWArg(&stringPtr, "tlen=")) { params->targetLength = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
Yann Colletb44ab822017-06-20 14:11:49 -0700629 if (longCommandWArg(&stringPtr, "strategy=") || longCommandWArg(&stringPtr, "strat=")) { params->strategy = (ZSTD_strategy)(readU32FromChar(&stringPtr)); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
Yann Collet6ccd37c2017-01-30 13:07:24 -0800630 if (longCommandWArg(&stringPtr, "overlapLog=") || longCommandWArg(&stringPtr, "ovlog=")) { g_overlapLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
Yann Collete874dac2018-11-20 14:56:07 -0800631 if (longCommandWArg(&stringPtr, "ldmHashLog=") || longCommandWArg(&stringPtr, "lhlog=")) { g_ldmHashLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
632 if (longCommandWArg(&stringPtr, "ldmMinMatch=") || longCommandWArg(&stringPtr, "lmml=")) { g_ldmMinMatch = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
633 if (longCommandWArg(&stringPtr, "ldmBucketSizeLog=") || longCommandWArg(&stringPtr, "lblog=")) { g_ldmBucketSizeLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
Yann Collet41c7d0b2018-11-21 14:36:57 -0800634 if (longCommandWArg(&stringPtr, "ldmHashRateLog=") || longCommandWArg(&stringPtr, "lhrlog=")) { g_ldmHashRateLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; }
Yann Colletc707c6e2017-12-27 13:32:05 +0100635 DISPLAYLEVEL(4, "invalid compression parameter \n");
Przemyslaw Skibinski671f28d2016-12-13 12:18:07 +0100636 return 0;
637 }
638
Yann Colletc707c6e2017-12-27 13:32:05 +0100639 DISPLAYLEVEL(4, "windowLog=%d, chainLog=%d, hashLog=%d, searchLog=%d \n", params->windowLog, params->chainLog, params->hashLog, params->searchLog);
Yann Collete874dac2018-11-20 14:56:07 -0800640 DISPLAYLEVEL(4, "minMatch=%d, targetLength=%d, strategy=%d \n", params->minMatch, params->targetLength, params->strategy);
Przemyslaw Skibinski671f28d2016-12-13 12:18:07 +0100641 if (stringPtr[0] != 0) return 0; /* check the end of string */
Przemyslaw Skibinski671f28d2016-12-13 12:18:07 +0100642 return 1;
643}
644
Yann Collet1c108c82017-08-19 13:33:50 -0700645static void printVersion(void)
646{
Tobias Hietab1cce9a2020-06-08 14:06:37 +0200647 if (g_displayLevel < DISPLAY_LEVEL_DEFAULT) {
648 DISPLAYOUT("%s\n", ZSTD_VERSION_STRING);
649 return;
650 }
651
Yann Collet4b4de742019-12-02 16:08:08 -0800652 DISPLAYOUT(WELCOME_MESSAGE);
653 if (g_displayLevel >= 3) {
Yann Collet1c108c82017-08-19 13:33:50 -0700654 /* format support */
Yann Collet4b4de742019-12-02 16:08:08 -0800655 DISPLAYOUT("*** supports: zstd");
656 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>0) && (ZSTD_LEGACY_SUPPORT<8)
657 DISPLAYOUT(", zstd legacy v0.%d+", ZSTD_LEGACY_SUPPORT);
658 #endif
659 #ifdef ZSTD_GZCOMPRESS
660 DISPLAYOUT(", gzip");
661 #endif
662 #ifdef ZSTD_LZ4COMPRESS
663 DISPLAYOUT(", lz4");
664 #endif
665 #ifdef ZSTD_LZMACOMPRESS
666 DISPLAYOUT(", lzma, xz ");
667 #endif
668 DISPLAYOUT("\n");
669 if (g_displayLevel >= 4) {
Elliott Hughes44aba642023-09-12 20:18:59 +0000670 /* library versions */
671 DISPLAYOUT("zlib version %s\n", FIO_zlibVersion());
672 DISPLAYOUT("lz4 version %s\n", FIO_lz4Version());
673 DISPLAYOUT("lzma version %s\n", FIO_lzmaVersion());
674
Yann Collet4b4de742019-12-02 16:08:08 -0800675 /* posix support */
676 #ifdef _POSIX_C_SOURCE
677 DISPLAYOUT("_POSIX_C_SOURCE defined: %ldL\n", (long) _POSIX_C_SOURCE);
678 #endif
679 #ifdef _POSIX_VERSION
680 DISPLAYOUT("_POSIX_VERSION defined: %ldL \n", (long) _POSIX_VERSION);
681 #endif
682 #ifdef PLATFORM_POSIX_VERSION
683 DISPLAYOUT("PLATFORM_POSIX_VERSION defined: %ldL\n", (long) PLATFORM_POSIX_VERSION);
684 #endif
685 } }
Yann Collet1c108c82017-08-19 13:33:50 -0700686}
Przemyslaw Skibinski671f28d2016-12-13 12:18:07 +0100687
Kevin Svetlitski63fe6192021-11-05 12:48:13 -0700688#define ZSTD_NB_STRATEGIES 9
689static const char* ZSTD_strategyMap[ZSTD_NB_STRATEGIES + 1] = { "", "ZSTD_fast",
690 "ZSTD_dfast", "ZSTD_greedy", "ZSTD_lazy", "ZSTD_lazy2", "ZSTD_btlazy2",
691 "ZSTD_btopt", "ZSTD_btultra", "ZSTD_btultra2"};
692
Kevin Svetlitski365c9112021-11-11 12:14:56 -0800693#ifndef ZSTD_NOCOMPRESS
694
Kevin Svetlitski63fe6192021-11-05 12:48:13 -0700695static void printDefaultCParams(const char* filename, const char* dictFileName, int cLevel) {
696 unsigned long long fileSize = UTIL_getFileSize(filename);
697 const size_t dictSize = dictFileName != NULL ? (size_t)UTIL_getFileSize(dictFileName) : 0;
698 const ZSTD_compressionParameters cParams = ZSTD_getCParams(cLevel, fileSize, dictSize);
699 if (fileSize != UTIL_FILESIZE_UNKNOWN) DISPLAY("%s (%u bytes)\n", filename, (unsigned)fileSize);
700 else DISPLAY("%s (src size unknown)\n", filename);
701 DISPLAY(" - windowLog : %u\n", cParams.windowLog);
702 DISPLAY(" - chainLog : %u\n", cParams.chainLog);
703 DISPLAY(" - hashLog : %u\n", cParams.hashLog);
704 DISPLAY(" - searchLog : %u\n", cParams.searchLog);
705 DISPLAY(" - minMatch : %u\n", cParams.minMatch);
706 DISPLAY(" - targetLength : %u\n", cParams.targetLength);
707 assert(cParams.strategy < ZSTD_NB_STRATEGIES + 1);
708 DISPLAY(" - strategy : %s (%u)\n", ZSTD_strategyMap[(int)cParams.strategy], (unsigned)cParams.strategy);
709}
710
711static void printActualCParams(const char* filename, const char* dictFileName, int cLevel, const ZSTD_compressionParameters* cParams) {
712 unsigned long long fileSize = UTIL_getFileSize(filename);
713 const size_t dictSize = dictFileName != NULL ? (size_t)UTIL_getFileSize(dictFileName) : 0;
714 ZSTD_compressionParameters actualCParams = ZSTD_getCParams(cLevel, fileSize, dictSize);
715 assert(g_displayLevel >= 4);
716 actualCParams.windowLog = cParams->windowLog == 0 ? actualCParams.windowLog : cParams->windowLog;
717 actualCParams.chainLog = cParams->chainLog == 0 ? actualCParams.chainLog : cParams->chainLog;
718 actualCParams.hashLog = cParams->hashLog == 0 ? actualCParams.hashLog : cParams->hashLog;
719 actualCParams.searchLog = cParams->searchLog == 0 ? actualCParams.searchLog : cParams->searchLog;
720 actualCParams.minMatch = cParams->minMatch == 0 ? actualCParams.minMatch : cParams->minMatch;
721 actualCParams.targetLength = cParams->targetLength == 0 ? actualCParams.targetLength : cParams->targetLength;
722 actualCParams.strategy = cParams->strategy == 0 ? actualCParams.strategy : cParams->strategy;
723 DISPLAY("--zstd=wlog=%d,clog=%d,hlog=%d,slog=%d,mml=%d,tlen=%d,strat=%d\n",
724 actualCParams.windowLog, actualCParams.chainLog, actualCParams.hashLog, actualCParams.searchLog,
725 actualCParams.minMatch, actualCParams.targetLength, actualCParams.strategy);
726}
727
Kevin Svetlitski365c9112021-11-11 12:14:56 -0800728#endif
729
Yi Jin5324b1e2018-12-19 13:26:27 -0800730/* Environment variables for parameter setting */
731#define ENV_CLEVEL "ZSTD_CLEVEL"
senhuang42a71963c2020-09-09 12:35:40 -0400732#define ENV_NBTHREADS "ZSTD_NBTHREADS" /* takes lower precedence than directly specifying -T# in the CLI */
Yi Jin5324b1e2018-12-19 13:26:27 -0800733
Yann Collet4b4de742019-12-02 16:08:08 -0800734/* pick up environment variable */
Yi Jinf7b18412018-12-19 17:56:45 -0800735static int init_cLevel(void) {
Yi Jin26a9ae32018-12-19 16:45:42 -0800736 const char* const env = getenv(ENV_CLEVEL);
Yann Colleta49417b2019-12-02 14:28:18 -0800737 if (env != NULL) {
738 const char* ptr = env;
Yi Jin5324b1e2018-12-19 13:26:27 -0800739 int sign = 1;
Yi Jin26a9ae32018-12-19 16:45:42 -0800740 if (*ptr == '-') {
Yi Jin5324b1e2018-12-19 13:26:27 -0800741 sign = -1;
Yi Jin26a9ae32018-12-19 16:45:42 -0800742 ptr++;
743 } else if (*ptr == '+') {
744 ptr++;
Yi Jin5324b1e2018-12-19 13:26:27 -0800745 }
746
Yi Jin26a9ae32018-12-19 16:45:42 -0800747 if ((*ptr>='0') && (*ptr<='9')) {
748 unsigned absLevel;
Yann Collet90c04622019-04-10 10:03:06 -0700749 if (readU32FromCharChecked(&ptr, &absLevel)) {
Yann Colleta49417b2019-12-02 14:28:18 -0800750 DISPLAYLEVEL(2, "Ignore environment variable setting %s=%s: numeric value too large \n", ENV_CLEVEL, env);
Yi Jin26a9ae32018-12-19 16:45:42 -0800751 return ZSTDCLI_CLEVEL_DEFAULT;
752 } else if (*ptr == 0) {
Yann Collet7f86ae22019-10-17 15:27:25 -0700753 return sign * (int)absLevel;
Yann Colleta49417b2019-12-02 14:28:18 -0800754 } }
Yi Jin26a9ae32018-12-19 16:45:42 -0800755
Yann Colleta49417b2019-12-02 14:28:18 -0800756 DISPLAYLEVEL(2, "Ignore environment variable setting %s=%s: not a valid integer value \n", ENV_CLEVEL, env);
Yi Jin5324b1e2018-12-19 13:26:27 -0800757 }
758
759 return ZSTDCLI_CLEVEL_DEFAULT;
760}
761
senhuang42243c8df2020-09-08 09:26:16 -0400762#ifdef ZSTD_MULTITHREAD
senhuang42a71963c2020-09-09 12:35:40 -0400763static unsigned init_nbThreads(void) {
764 const char* const env = getenv(ENV_NBTHREADS);
senhuang4270fa7b32020-08-25 18:49:52 -0400765 if (env != NULL) {
766 const char* ptr = env;
767 if ((*ptr>='0') && (*ptr<='9')) {
senhuang42a71963c2020-09-09 12:35:40 -0400768 unsigned nbThreads;
769 if (readU32FromCharChecked(&ptr, &nbThreads)) {
770 DISPLAYLEVEL(2, "Ignore environment variable setting %s=%s: numeric value too large \n", ENV_NBTHREADS, env);
771 return ZSTDCLI_NBTHREADS_DEFAULT;
senhuang4270fa7b32020-08-25 18:49:52 -0400772 } else if (*ptr == 0) {
senhuang42a71963c2020-09-09 12:35:40 -0400773 return nbThreads;
senhuang4270fa7b32020-08-25 18:49:52 -0400774 }
775 }
senhuang42a71963c2020-09-09 12:35:40 -0400776 DISPLAYLEVEL(2, "Ignore environment variable setting %s=%s: not a valid unsigned value \n", ENV_NBTHREADS, env);
senhuang4270fa7b32020-08-25 18:49:52 -0400777 }
778
senhuang42a71963c2020-09-09 12:35:40 -0400779 return ZSTDCLI_NBTHREADS_DEFAULT;
senhuang4270fa7b32020-08-25 18:49:52 -0400780}
senhuang42243c8df2020-09-08 09:26:16 -0400781#endif
senhuang4270fa7b32020-08-25 18:49:52 -0400782
Yann Collet610c41f2020-07-20 17:41:32 -0700783#define NEXT_FIELD(ptr) { \
784 if (*argument == '=') { \
785 ptr = ++argument; \
786 argument += strlen(ptr); \
787 } else { \
788 argNb++; \
789 if (argNb >= argCount) { \
Elliott Hughes44aba642023-09-12 20:18:59 +0000790 DISPLAYLEVEL(1, "error: missing command argument \n"); \
Yann Collet610c41f2020-07-20 17:41:32 -0700791 CLEAN_RETURN(1); \
792 } \
793 ptr = argv[argNb]; \
794 assert(ptr != NULL); \
795 if (ptr[0]=='-') { \
Elliott Hughes44aba642023-09-12 20:18:59 +0000796 DISPLAYLEVEL(1, "error: command cannot be separated from its argument by another command \n"); \
Yann Collet610c41f2020-07-20 17:41:32 -0700797 CLEAN_RETURN(1); \
798} } }
799
800#define NEXT_UINT32(val32) { \
801 const char* __nb; \
802 NEXT_FIELD(__nb); \
803 val32 = readU32FromChar(&__nb); \
Elliott Hughes44aba642023-09-12 20:18:59 +0000804 if(*__nb != 0) { \
805 errorOut("error: only numeric values with optional suffixes K, KB, KiB, M, MB, MiB are allowed"); \
806 } \
807}
808
809#define NEXT_TSIZE(valTsize) { \
810 const char* __nb; \
811 NEXT_FIELD(__nb); \
812 valTsize = readSizeTFromChar(&__nb); \
813 if(*__nb != 0) { \
814 errorOut("error: only numeric values with optional suffixes K, KB, KiB, M, MB, MiB are allowed"); \
815 } \
Yann Colleta7e56522020-07-17 12:46:36 -0700816}
Bimba Shrestha38d010e2020-01-31 10:47:17 -0800817
Paul Cruz901435e2017-06-05 14:45:31 -0700818typedef enum { zom_compress, zom_decompress, zom_test, zom_bench, zom_train, zom_list } zstd_operation_mode;
Yann Colletfcf20872016-05-29 05:16:05 +0200819
820#define CLEAN_RETURN(i) { operationResult = (i); goto _end; }
821
Yann Collet6c51bf42018-09-24 18:16:08 -0700822#ifdef ZSTD_NOCOMPRESS
823/* symbols from compression library are not defined and should not be invoked */
Yann Colleta49417b2019-12-02 14:28:18 -0800824# define MINCLEVEL -99
Yann Collet6c51bf42018-09-24 18:16:08 -0700825# define MAXCLEVEL 22
826#else
827# define MINCLEVEL ZSTD_minCLevel()
828# define MAXCLEVEL ZSTD_maxCLevel()
829#endif
830
makise-homuraa5f518a2021-07-14 19:55:47 +0300831int main(int argCount, const char* argv[])
Yann Colletfcf20872016-05-29 05:16:05 +0200832{
833 int argNb,
Yann Collet5cb11442018-02-13 14:56:35 -0800834 followLinks = 0,
W. Felix Handte33f3e292021-05-04 16:24:46 -0400835 allowBlockDevices = 0,
W. Felix Handte8b6a4b52021-01-11 17:53:20 -0500836 forceStdin = 0,
Yann Collet5cb11442018-02-13 14:56:35 -0800837 forceStdout = 0,
senhuang427259b252020-10-07 13:47:38 -0400838 hasStdout = 0,
Yann Collet75689832018-02-09 15:53:27 -0800839 ldmFlag = 0,
Yann Collet5cb11442018-02-13 14:56:35 -0800840 main_pause = 0,
Yann Collete7a49c62018-08-11 20:48:06 -0700841 adapt = 0,
Yann Collet6c51bf42018-09-24 18:16:08 -0700842 adaptMin = MINCLEVEL,
843 adaptMax = MAXCLEVEL,
Nick Terrellf9a671a2018-11-12 19:59:42 -0800844 rsyncable = 0,
Yann Collet5cb11442018-02-13 14:56:35 -0800845 nextArgumentsAreFiles = 0,
Yann Collet5cb11442018-02-13 14:56:35 -0800846 operationResult = 0,
Yann Colletd898fb72017-11-17 00:22:55 -0800847 separateFiles = 0,
Yann Collet75689832018-02-09 15:53:27 -0800848 setRealTimePrio = 0,
Yann Collet5cb11442018-02-13 14:56:35 -0800849 singleThread = 0,
Binh Vo6a46e382021-06-16 09:38:43 -0400850 defaultLogicalCores = 0,
Bimba Shrestha2f100192020-01-30 14:12:03 -0800851 showDefaultCParams = 0,
Bimba Shrestha167244a2020-03-09 13:07:29 -0500852 ultra=0,
Elliott Hughes44aba642023-09-12 20:18:59 +0000853 contentSize=1,
854 removeSrcFile=0;
855 ZSTD_paramSwitch_e mmapDict=ZSTD_ps_auto;
856 ZSTD_paramSwitch_e useRowMatchFinder = ZSTD_ps_auto;
857 FIO_compressionType_t cType = FIO_zstdCompression;
858 unsigned nbWorkers = 0;
George Lua8eea992018-06-19 10:58:22 -0700859 double compressibility = 0.5;
Yann Collet500014a2017-01-19 16:59:56 -0800860 unsigned bench_nbSeconds = 3; /* would be better if this value was synchronized from bench */
861 size_t blockSize = 0;
Karl Ostmo5e220bf2019-01-22 17:31:13 -0800862
863 FIO_prefs_t* const prefs = FIO_createPreferences();
senhuang42d54566f2020-08-28 11:01:04 -0400864 FIO_ctx_t* const fCtx = FIO_createContext();
Elliott Hughes44aba642023-09-12 20:18:59 +0000865 FIO_progressSetting_e progress = FIO_ps_auto;
Yann Collet22de81e2016-10-28 13:58:31 -0700866 zstd_operation_mode operation = zom_compress;
Przemyslaw Skibinski671f28d2016-12-13 12:18:07 +0100867 ZSTD_compressionParameters compressionParams;
Yann Collet4b4de742019-12-02 16:08:08 -0800868 int cLevel = init_cLevel();
Yann Colleta49417b2019-12-02 14:28:18 -0800869 int cLevelLast = MINCLEVEL - 1; /* lower than minimum */
Yann Colletfcf20872016-05-29 05:16:05 +0200870 unsigned recursive = 0;
Yann Colletd4cda272016-10-14 13:13:13 -0700871 unsigned memLimit = 0;
Yann Colletb09f5932019-11-05 17:02:43 -0800872 FileNamesTable* filenames = UTIL_allocateFileNamesTable((size_t)argCount); /* argCount >= 1 */
873 FileNamesTable* file_of_names = UTIL_allocateFileNamesTable((size_t)argCount); /* argCount >= 1 */
Yann Colletfcf20872016-05-29 05:16:05 +0200874 const char* programName = argv[0];
875 const char* outFileName = NULL;
Sen Huang7f98b462019-09-05 16:03:35 -0700876 const char* outDirName = NULL;
Xin Xie9a8ccd42020-06-19 19:35:51 -0700877 const char* outMirroredDirName = NULL;
Yann Colletfcf20872016-05-29 05:16:05 +0200878 const char* dictFileName = NULL;
Bimba Shresthaf25a6e92020-01-10 14:25:24 -0800879 const char* patchFromDictFileName = NULL;
Przemyslaw Skibinskicb563062017-02-08 17:37:14 +0100880 const char* suffix = ZSTD_EXTENSION;
Yann Colletfcf20872016-05-29 05:16:05 +0200881 unsigned maxDictSize = g_defaultMaxDictSize;
Yann Collet290aaa72016-05-30 21:18:52 +0200882 unsigned dictID = 0;
Nick Magerkoaf0c9502019-08-15 23:57:55 -0700883 size_t streamSrcSize = 0;
Ephraim Park90077012019-06-24 13:40:52 -0700884 size_t targetCBlockSize = 0;
Nick Magerkodffbac52019-08-19 08:52:08 -0700885 size_t srcSizeHint = 0;
Elliott Hughes44aba642023-09-12 20:18:59 +0000886 size_t nbInputFileNames = 0;
Yann Collet41105342016-07-27 15:09:11 +0200887 int dictCLevel = g_defaultDictCLevel;
Yann Colletfcf20872016-05-29 05:16:05 +0200888 unsigned dictSelect = g_defaultSelectivityLevel;
Nick Terrelldf8415c2016-12-31 21:08:24 -0800889#ifndef ZSTD_NODICT
Nick Terrell5b7fd7c2017-06-26 21:07:14 -0700890 ZDICT_cover_params_t coverParams = defaultCoverParams();
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -0700891 ZDICT_fastCover_params_t fastCoverParams = defaultFastCoverParams();
892 dictType dict = fastCover;
Nick Terrelldf8415c2016-12-31 21:08:24 -0800893#endif
Yann Collet5203f012018-08-03 07:54:29 -0700894#ifndef ZSTD_NOBENCH
895 BMK_advancedParams_t benchParams = BMK_initAdvancedParams();
896#endif
senhuang42b5c35d72021-09-20 09:04:07 -0400897 ZSTD_paramSwitch_e literalCompressionMode = ZSTD_ps_auto;
Yann Colletfcf20872016-05-29 05:16:05 +0200898
Yann Collet00fc1ba2017-10-01 12:10:26 -0700899
Yann Colletfcf20872016-05-29 05:16:05 +0200900 /* init */
Yann Collet3324e872021-01-07 10:37:27 -0800901 checkLibVersion();
Yann Colletf3a1a842016-06-05 01:05:01 +0200902 (void)recursive; (void)cLevelLast; /* not used when ZSTD_NOBENCH set */
Bimba Shresthaf25a6e92020-01-10 14:25:24 -0800903 (void)memLimit;
Yann Colleta49417b2019-12-02 14:28:18 -0800904 assert(argCount >= 1);
Elliott Hughes44aba642023-09-12 20:18:59 +0000905 if ((filenames==NULL) || (file_of_names==NULL)) { DISPLAYLEVEL(1, "zstd: allocation error \n"); exit(1); }
Sean Purcellef30af82017-03-24 17:06:09 -0700906 programName = lastNameFromPath(programName);
Yann Collet5cb11442018-02-13 14:56:35 -0800907#ifdef ZSTD_MULTITHREAD
senhuang42a71963c2020-09-09 12:35:40 -0400908 nbWorkers = init_nbThreads();
Yann Collet5cb11442018-02-13 14:56:35 -0800909#endif
Yann Colletfcf20872016-05-29 05:16:05 +0200910
911 /* preset behaviors */
Yann Colletb5207aa2018-06-29 17:10:56 -0700912 if (exeNameMatch(programName, ZSTD_ZSTDMT)) nbWorkers=0, singleThread=0;
Sean Purcellef30af82017-03-24 17:06:09 -0700913 if (exeNameMatch(programName, ZSTD_UNZSTD)) operation=zom_decompress;
Elliott Hughes44aba642023-09-12 20:18:59 +0000914 if (exeNameMatch(programName, ZSTD_CAT)) { operation=zom_decompress; FIO_overwriteMode(prefs); forceStdout=1; followLinks=1; FIO_setPassThroughFlag(prefs, 1); outFileName=stdoutmark; g_displayLevel=1; } /* supports multiple formats */
915 if (exeNameMatch(programName, ZSTD_ZCAT)) { operation=zom_decompress; FIO_overwriteMode(prefs); forceStdout=1; followLinks=1; FIO_setPassThroughFlag(prefs, 1); outFileName=stdoutmark; g_displayLevel=1; } /* behave like zcat, also supports multiple formats */
916 if (exeNameMatch(programName, ZSTD_GZ)) { /* behave like gzip */
917 suffix = GZ_EXTENSION; cType = FIO_gzipCompression; removeSrcFile=1;
918 dictCLevel = cLevel = 6; /* gzip default is -6 */
919 }
920 if (exeNameMatch(programName, ZSTD_GUNZIP)) { operation=zom_decompress; removeSrcFile=1; } /* behave like gunzip, also supports multiple formats */
921 if (exeNameMatch(programName, ZSTD_GZCAT)) { operation=zom_decompress; FIO_overwriteMode(prefs); forceStdout=1; followLinks=1; FIO_setPassThroughFlag(prefs, 1); outFileName=stdoutmark; g_displayLevel=1; } /* behave like gzcat, also supports multiple formats */
922 if (exeNameMatch(programName, ZSTD_LZMA)) { suffix = LZMA_EXTENSION; cType = FIO_lzmaCompression; removeSrcFile=1; } /* behave like lzma */
923 if (exeNameMatch(programName, ZSTD_UNLZMA)) { operation=zom_decompress; cType = FIO_lzmaCompression; removeSrcFile=1; } /* behave like unlzma, also supports multiple formats */
924 if (exeNameMatch(programName, ZSTD_XZ)) { suffix = XZ_EXTENSION; cType = FIO_xzCompression; removeSrcFile=1; } /* behave like xz */
925 if (exeNameMatch(programName, ZSTD_UNXZ)) { operation=zom_decompress; cType = FIO_xzCompression; removeSrcFile=1; } /* behave like unxz, also supports multiple formats */
926 if (exeNameMatch(programName, ZSTD_LZ4)) { suffix = LZ4_EXTENSION; cType = FIO_lz4Compression; } /* behave like lz4 */
927 if (exeNameMatch(programName, ZSTD_UNLZ4)) { operation=zom_decompress; cType = FIO_lz4Compression; } /* behave like unlz4, also supports multiple formats */
Przemyslaw Skibinski671f28d2016-12-13 12:18:07 +0100928 memset(&compressionParams, 0, sizeof(compressionParams));
Yann Colletfcf20872016-05-29 05:16:05 +0200929
Casey McGintya06574f2018-09-06 18:46:52 -0700930 /* init crash handler */
Casey McGinty2224ecd2018-09-11 11:56:50 -0700931 FIO_addAbortHandler();
Casey McGintya06574f2018-09-06 18:46:52 -0700932
Yann Colletfcf20872016-05-29 05:16:05 +0200933 /* command switches */
Yann Collet11223492016-10-14 14:07:11 -0700934 for (argNb=1; argNb<argCount; argNb++) {
Yann Colletfcf20872016-05-29 05:16:05 +0200935 const char* argument = argv[argNb];
Yann Colletb09f5932019-11-05 17:02:43 -0800936 if (!argument) continue; /* Protection if argument empty */
Yann Colletfcf20872016-05-29 05:16:05 +0200937
Yann Colletb09f5932019-11-05 17:02:43 -0800938 if (nextArgumentsAreFiles) {
939 UTIL_refFilename(filenames, argument);
940 continue;
941 }
Yann Colletfcf20872016-05-29 05:16:05 +0200942
Yann Colletb09f5932019-11-05 17:02:43 -0800943 /* "-" means stdin/stdout */
944 if (!strcmp(argument, "-")){
945 UTIL_refFilename(filenames, stdinmark);
946 continue;
947 }
Yann Colletfcf20872016-05-29 05:16:05 +0200948
Yann Colletb09f5932019-11-05 17:02:43 -0800949 /* Decode commands (note : aggregated commands are allowed) */
950 if (argument[0]=='-') {
951
952 if (argument[1]=='-') {
953 /* long commands (--long-word) */
954 if (!strcmp(argument, "--")) { nextArgumentsAreFiles=1; continue; } /* only file names allowed from now on */
955 if (!strcmp(argument, "--list")) { operation=zom_list; continue; }
956 if (!strcmp(argument, "--compress")) { operation=zom_compress; continue; }
957 if (!strcmp(argument, "--decompress")) { operation=zom_decompress; continue; }
958 if (!strcmp(argument, "--uncompress")) { operation=zom_decompress; continue; }
W. Felix Handte33f3e292021-05-04 16:24:46 -0400959 if (!strcmp(argument, "--force")) { FIO_overwriteMode(prefs); forceStdin=1; forceStdout=1; followLinks=1; allowBlockDevices=1; continue; }
Yann Collet4b4de742019-12-02 16:08:08 -0800960 if (!strcmp(argument, "--version")) { printVersion(); CLEAN_RETURN(0); }
961 if (!strcmp(argument, "--help")) { usage_advanced(programName); CLEAN_RETURN(0); }
Yann Colletb09f5932019-11-05 17:02:43 -0800962 if (!strcmp(argument, "--verbose")) { g_displayLevel++; continue; }
963 if (!strcmp(argument, "--quiet")) { g_displayLevel--; continue; }
Elliott Hughes44aba642023-09-12 20:18:59 +0000964 if (!strcmp(argument, "--stdout")) { forceStdout=1; outFileName=stdoutmark; removeSrcFile=0; continue; }
Yann Colletb09f5932019-11-05 17:02:43 -0800965 if (!strcmp(argument, "--ultra")) { ultra=1; continue; }
966 if (!strcmp(argument, "--check")) { FIO_setChecksumFlag(prefs, 2); continue; }
967 if (!strcmp(argument, "--no-check")) { FIO_setChecksumFlag(prefs, 0); continue; }
968 if (!strcmp(argument, "--sparse")) { FIO_setSparseWrite(prefs, 2); continue; }
969 if (!strcmp(argument, "--no-sparse")) { FIO_setSparseWrite(prefs, 0); continue; }
Elliott Hughes44aba642023-09-12 20:18:59 +0000970 if (!strcmp(argument, "--pass-through")) { FIO_setPassThroughFlag(prefs, 1); continue; }
971 if (!strcmp(argument, "--no-pass-through")) { FIO_setPassThroughFlag(prefs, 0); continue; }
Yann Colletb09f5932019-11-05 17:02:43 -0800972 if (!strcmp(argument, "--test")) { operation=zom_test; continue; }
Elliott Hughes44aba642023-09-12 20:18:59 +0000973 if (!strcmp(argument, "--asyncio")) { FIO_setAsyncIOFlag(prefs, 1); continue;}
974 if (!strcmp(argument, "--no-asyncio")) { FIO_setAsyncIOFlag(prefs, 0); continue;}
Yann Colletb09f5932019-11-05 17:02:43 -0800975 if (!strcmp(argument, "--train")) { operation=zom_train; if (outFileName==NULL) outFileName=g_defaultDictName; continue; }
Yann Colletb09f5932019-11-05 17:02:43 -0800976 if (!strcmp(argument, "--no-dictID")) { FIO_setDictIDFlag(prefs, 0); continue; }
Elliott Hughes44aba642023-09-12 20:18:59 +0000977 if (!strcmp(argument, "--keep")) { removeSrcFile=0; continue; }
978 if (!strcmp(argument, "--rm")) { removeSrcFile=1; continue; }
Yann Colletb09f5932019-11-05 17:02:43 -0800979 if (!strcmp(argument, "--priority=rt")) { setRealTimePrio = 1; continue; }
Bimba Shrestha2f100192020-01-30 14:12:03 -0800980 if (!strcmp(argument, "--show-default-cparams")) { showDefaultCParams = 1; continue; }
Bimba Shrestha43874732020-03-09 14:19:05 -0500981 if (!strcmp(argument, "--content-size")) { contentSize = 1; continue; }
Bimba Shrestha6d8e7612020-03-09 14:12:52 -0500982 if (!strcmp(argument, "--no-content-size")) { contentSize = 0; continue; }
Yann Colletb09f5932019-11-05 17:02:43 -0800983 if (!strcmp(argument, "--adapt")) { adapt = 1; continue; }
Elliott Hughes44aba642023-09-12 20:18:59 +0000984 if (!strcmp(argument, "--no-row-match-finder")) { useRowMatchFinder = ZSTD_ps_disable; continue; }
985 if (!strcmp(argument, "--row-match-finder")) { useRowMatchFinder = ZSTD_ps_enable; continue; }
Yann Collet4b4de742019-12-02 16:08:08 -0800986 if (longCommandWArg(&argument, "--adapt=")) { adapt = 1; if (!parseAdaptParameters(argument, &adaptMin, &adaptMax)) { badusage(programName); CLEAN_RETURN(1); } continue; }
Yann Colletb09f5932019-11-05 17:02:43 -0800987 if (!strcmp(argument, "--single-thread")) { nbWorkers = 0; singleThread = 1; continue; }
Elliott Hughes44aba642023-09-12 20:18:59 +0000988 if (!strcmp(argument, "--format=zstd")) { suffix = ZSTD_EXTENSION; cType = FIO_zstdCompression; continue; }
989 if (!strcmp(argument, "--mmap-dict")) { mmapDict = ZSTD_ps_enable; continue; }
990 if (!strcmp(argument, "--no-mmap-dict")) { mmapDict = ZSTD_ps_disable; continue; }
Yann Colletc1c040e2017-03-01 16:49:20 -0800991#ifdef ZSTD_GZCOMPRESS
Elliott Hughes44aba642023-09-12 20:18:59 +0000992 if (!strcmp(argument, "--format=gzip")) { suffix = GZ_EXTENSION; cType = FIO_gzipCompression; continue; }
993 if (exeNameMatch(programName, ZSTD_GZ)) { /* behave like gzip */
994 if (!strcmp(argument, "--best")) { dictCLevel = cLevel = 9; continue; }
995 if (!strcmp(argument, "--no-name")) { /* ignore for now */; continue; }
996 }
Yann Colletc1c040e2017-03-01 16:49:20 -0800997#endif
Nick Terrellaa8bcf32017-03-13 18:11:07 -0700998#ifdef ZSTD_LZMACOMPRESS
Elliott Hughes44aba642023-09-12 20:18:59 +0000999 if (!strcmp(argument, "--format=lzma")) { suffix = LZMA_EXTENSION; cType = FIO_lzmaCompression; continue; }
1000 if (!strcmp(argument, "--format=xz")) { suffix = XZ_EXTENSION; cType = FIO_xzCompression; continue; }
Nick Terrellaa8bcf32017-03-13 18:11:07 -07001001#endif
Sean Purcell4de86322017-04-24 16:48:25 -07001002#ifdef ZSTD_LZ4COMPRESS
Elliott Hughes44aba642023-09-12 20:18:59 +00001003 if (!strcmp(argument, "--format=lz4")) { suffix = LZ4_EXTENSION; cType = FIO_lz4Compression; continue; }
Sean Purcell4de86322017-04-24 16:48:25 -07001004#endif
Yann Colletb09f5932019-11-05 17:02:43 -08001005 if (!strcmp(argument, "--rsyncable")) { rsyncable = 1; continue; }
senhuang42b5c35d72021-09-20 09:04:07 -04001006 if (!strcmp(argument, "--compress-literals")) { literalCompressionMode = ZSTD_ps_enable; continue; }
1007 if (!strcmp(argument, "--no-compress-literals")) { literalCompressionMode = ZSTD_ps_disable; continue; }
Elliott Hughes44aba642023-09-12 20:18:59 +00001008 if (!strcmp(argument, "--no-progress")) { progress = FIO_ps_never; continue; }
1009 if (!strcmp(argument, "--progress")) { progress = FIO_ps_always; continue; }
Yann Colletb09f5932019-11-05 17:02:43 -08001010 if (!strcmp(argument, "--exclude-compressed")) { FIO_setExcludeCompressedFile(prefs, 1); continue; }
Elliott Hughes44aba642023-09-12 20:18:59 +00001011 if (!strcmp(argument, "--fake-stdin-is-console")) { UTIL_fakeStdinIsConsole(); continue; }
1012 if (!strcmp(argument, "--fake-stdout-is-console")) { UTIL_fakeStdoutIsConsole(); continue; }
1013 if (!strcmp(argument, "--fake-stderr-is-console")) { UTIL_fakeStderrIsConsole(); continue; }
1014 if (!strcmp(argument, "--trace-file-stat")) { UTIL_traceFileStat(); continue; }
Yann Collet610c41f2020-07-20 17:41:32 -07001015
Yann Colletb09f5932019-11-05 17:02:43 -08001016 /* long commands with arguments */
Yann Colletc1c040e2017-03-01 16:49:20 -08001017#ifndef ZSTD_NODICT
Yann Colletb09f5932019-11-05 17:02:43 -08001018 if (longCommandWArg(&argument, "--train-cover")) {
1019 operation = zom_train;
1020 if (outFileName == NULL)
1021 outFileName = g_defaultDictName;
1022 dict = cover;
1023 /* Allow optional arguments following an = */
1024 if (*argument == 0) { memset(&coverParams, 0, sizeof(coverParams)); }
Yann Collet4b4de742019-12-02 16:08:08 -08001025 else if (*argument++ != '=') { badusage(programName); CLEAN_RETURN(1); }
1026 else if (!parseCoverParameters(argument, &coverParams)) { badusage(programName); CLEAN_RETURN(1); }
Yann Colletb09f5932019-11-05 17:02:43 -08001027 continue;
1028 }
1029 if (longCommandWArg(&argument, "--train-fastcover")) {
1030 operation = zom_train;
1031 if (outFileName == NULL)
1032 outFileName = g_defaultDictName;
1033 dict = fastCover;
1034 /* Allow optional arguments following an = */
1035 if (*argument == 0) { memset(&fastCoverParams, 0, sizeof(fastCoverParams)); }
Yann Collet4b4de742019-12-02 16:08:08 -08001036 else if (*argument++ != '=') { badusage(programName); CLEAN_RETURN(1); }
1037 else if (!parseFastCoverParameters(argument, &fastCoverParams)) { badusage(programName); CLEAN_RETURN(1); }
Yann Colletb09f5932019-11-05 17:02:43 -08001038 continue;
1039 }
1040 if (longCommandWArg(&argument, "--train-legacy")) {
1041 operation = zom_train;
1042 if (outFileName == NULL)
1043 outFileName = g_defaultDictName;
1044 dict = legacy;
1045 /* Allow optional arguments following an = */
1046 if (*argument == 0) { continue; }
Yann Collet4b4de742019-12-02 16:08:08 -08001047 else if (*argument++ != '=') { badusage(programName); CLEAN_RETURN(1); }
1048 else if (!parseLegacyParameters(argument, &dictSelect)) { badusage(programName); CLEAN_RETURN(1); }
Yann Colletb09f5932019-11-05 17:02:43 -08001049 continue;
1050 }
Nick Terrelldf8415c2016-12-31 21:08:24 -08001051#endif
Yann Collet610c41f2020-07-20 17:41:32 -07001052 if (longCommandWArg(&argument, "--threads")) { NEXT_UINT32(nbWorkers); continue; }
1053 if (longCommandWArg(&argument, "--memlimit")) { NEXT_UINT32(memLimit); continue; }
1054 if (longCommandWArg(&argument, "--memory")) { NEXT_UINT32(memLimit); continue; }
1055 if (longCommandWArg(&argument, "--memlimit-decompress")) { NEXT_UINT32(memLimit); continue; }
Elliott Hughes44aba642023-09-12 20:18:59 +00001056 if (longCommandWArg(&argument, "--block-size")) { NEXT_TSIZE(blockSize); continue; }
Yann Collet610c41f2020-07-20 17:41:32 -07001057 if (longCommandWArg(&argument, "--maxdict")) { NEXT_UINT32(maxDictSize); continue; }
1058 if (longCommandWArg(&argument, "--dictID")) { NEXT_UINT32(dictID); continue; }
Elliott Hughes44aba642023-09-12 20:18:59 +00001059 if (longCommandWArg(&argument, "--zstd=")) { if (!parseCompressionParameters(argument, &compressionParams)) { badusage(programName); CLEAN_RETURN(1); } ; cType = FIO_zstdCompression; continue; }
1060 if (longCommandWArg(&argument, "--stream-size")) { NEXT_TSIZE(streamSrcSize); continue; }
1061 if (longCommandWArg(&argument, "--target-compressed-block-size")) { NEXT_TSIZE(targetCBlockSize); continue; }
1062 if (longCommandWArg(&argument, "--size-hint")) { NEXT_TSIZE(srcSizeHint); continue; }
1063 if (longCommandWArg(&argument, "--output-dir-flat")) {
1064 NEXT_FIELD(outDirName);
1065 if (strlen(outDirName) == 0) {
1066 DISPLAYLEVEL(1, "error: output dir cannot be empty string (did you mean to pass '.' instead?)\n");
1067 CLEAN_RETURN(1);
1068 }
1069 continue;
1070 }
Binh Vo6a46e382021-06-16 09:38:43 -04001071 if (longCommandWArg(&argument, "--auto-threads")) {
1072 const char* threadDefault = NULL;
1073 NEXT_FIELD(threadDefault);
1074 if (strcmp(threadDefault, "logical") == 0)
1075 defaultLogicalCores = 1;
1076 continue;
1077 }
Yann Collet610c41f2020-07-20 17:41:32 -07001078#ifdef UTIL_HAS_MIRRORFILELIST
Elliott Hughes44aba642023-09-12 20:18:59 +00001079 if (longCommandWArg(&argument, "--output-dir-mirror")) {
1080 NEXT_FIELD(outMirroredDirName);
1081 if (strlen(outMirroredDirName) == 0) {
1082 DISPLAYLEVEL(1, "error: output dir cannot be empty string (did you mean to pass '.' instead?)\n");
1083 CLEAN_RETURN(1);
1084 }
1085 continue;
1086 }
Yann Collet610c41f2020-07-20 17:41:32 -07001087#endif
Nick Terrell54a49982021-02-03 19:53:00 -08001088#ifndef ZSTD_NOTRACE
1089 if (longCommandWArg(&argument, "--trace")) { char const* traceFile; NEXT_FIELD(traceFile); TRACE_enable(traceFile); continue; }
1090#endif
Yann Collet610c41f2020-07-20 17:41:32 -07001091 if (longCommandWArg(&argument, "--patch-from")) { NEXT_FIELD(patchFromDictFileName); continue; }
binhdvo325952f2021-06-09 15:35:43 -04001092 if (longCommandWArg(&argument, "--long")) {
1093 unsigned ldmWindowLog = 0;
1094 ldmFlag = 1;
1095 /* Parse optional window log */
1096 if (*argument == '=') {
1097 ++argument;
1098 ldmWindowLog = readU32FromChar(&argument);
1099 } else if (*argument != 0) {
1100 /* Invalid character following --long */
1101 badusage(programName);
1102 CLEAN_RETURN(1);
Elliott Hughes44aba642023-09-12 20:18:59 +00001103 } else {
1104 ldmWindowLog = g_defaultMaxWindowLog;
binhdvo325952f2021-06-09 15:35:43 -04001105 }
1106 /* Only set windowLog if not already set by --zstd */
1107 if (compressionParams.windowLog == 0)
1108 compressionParams.windowLog = ldmWindowLog;
1109 continue;
1110 }
Yann Collet0fc07eb2018-09-22 17:21:39 -07001111#ifndef ZSTD_NOCOMPRESS /* linking ZSTD_minCLevel() requires compression support */
Yann Colletb09f5932019-11-05 17:02:43 -08001112 if (longCommandWArg(&argument, "--fast")) {
1113 /* Parse optional acceleration factor */
1114 if (*argument == '=') {
1115 U32 const maxFast = (U32)-ZSTD_minCLevel();
1116 U32 fastLevel;
1117 ++argument;
1118 fastLevel = readU32FromChar(&argument);
1119 if (fastLevel > maxFast) fastLevel = maxFast;
1120 if (fastLevel) {
Yann Collet4b4de742019-12-02 16:08:08 -08001121 dictCLevel = cLevel = -(int)fastLevel;
Yann Collet6a9b41b2018-03-11 19:56:48 -07001122 } else {
Yann Collet4b4de742019-12-02 16:08:08 -08001123 badusage(programName);
1124 CLEAN_RETURN(1);
Yann Collet6a9b41b2018-03-11 19:56:48 -07001125 }
Yann Colletb09f5932019-11-05 17:02:43 -08001126 } else if (*argument != 0) {
1127 /* Invalid character following --fast */
Yann Collet4b4de742019-12-02 16:08:08 -08001128 badusage(programName);
1129 CLEAN_RETURN(1);
Yann Colletb09f5932019-11-05 17:02:43 -08001130 } else {
1131 cLevel = -1; /* default for --fast */
Yann Collet6a9b41b2018-03-11 19:56:48 -07001132 }
Yann Colletb09f5932019-11-05 17:02:43 -08001133 continue;
1134 }
Yann Collet0fc07eb2018-09-22 17:21:39 -07001135#endif
Ahmed Abdellah779ea722019-10-15 07:49:13 +01001136
Yann Collet610c41f2020-07-20 17:41:32 -07001137 if (longCommandWArg(&argument, "--filelist")) {
1138 const char* listName;
1139 NEXT_FIELD(listName);
1140 UTIL_refFilename(file_of_names, listName);
Yann Colletb09f5932019-11-05 17:02:43 -08001141 continue;
Yann Collet743b33f2016-12-02 15:18:57 -08001142 }
1143
Yann Colletb09f5932019-11-05 17:02:43 -08001144 /* fall-through, will trigger bad_usage() later on */
1145 }
1146
1147 argument++;
1148 while (argument[0]!=0) {
Yann Collet7280c1d2020-07-17 13:09:23 -07001149
Yann Colletc8431422016-09-01 15:05:57 -07001150#ifndef ZSTD_NOCOMPRESS
Yann Colletb09f5932019-11-05 17:02:43 -08001151 /* compression Level */
1152 if ((*argument>='0') && (*argument<='9')) {
1153 dictCLevel = cLevel = (int)readU32FromChar(&argument);
1154 continue;
1155 }
Yann Colletc8431422016-09-01 15:05:57 -07001156#endif
Yann Colletfcf20872016-05-29 05:16:05 +02001157
Yann Colletb09f5932019-11-05 17:02:43 -08001158 switch(argument[0])
1159 {
1160 /* Display help */
Yann Collet4b4de742019-12-02 16:08:08 -08001161 case 'V': printVersion(); CLEAN_RETURN(0); /* Version Only */
Elliott Hughes44aba642023-09-12 20:18:59 +00001162 case 'H': usage_advanced(programName); CLEAN_RETURN(0);
1163 case 'h': usage(stdout, programName); CLEAN_RETURN(0);
Yann Colletfcf20872016-05-29 05:16:05 +02001164
Yann Colletb09f5932019-11-05 17:02:43 -08001165 /* Compress */
1166 case 'z': operation=zom_compress; argument++; break;
Yann Collet22de81e2016-10-28 13:58:31 -07001167
Yann Colletb09f5932019-11-05 17:02:43 -08001168 /* Decoding */
1169 case 'd':
Yann Collet2a826e52016-12-06 17:56:20 -08001170#ifndef ZSTD_NOBENCH
Yann Colletb09f5932019-11-05 17:02:43 -08001171 benchParams.mode = BMK_decodeOnly;
1172 if (operation==zom_bench) { argument++; break; } /* benchmark decode (hidden option) */
Yann Collet2a826e52016-12-06 17:56:20 -08001173#endif
Yann Colletb09f5932019-11-05 17:02:43 -08001174 operation=zom_decompress; argument++; break;
Yann Colletfcf20872016-05-29 05:16:05 +02001175
Yann Colletb09f5932019-11-05 17:02:43 -08001176 /* Force stdout, even if stdout==console */
Elliott Hughes44aba642023-09-12 20:18:59 +00001177 case 'c': forceStdout=1; outFileName=stdoutmark; removeSrcFile=0; argument++; break;
1178
1179 /* do not store filename - gzip compatibility - nothing to do */
1180 case 'n': argument++; break;
Dirk Müllere653e972022-02-08 21:32:42 +01001181
Yann Colletb09f5932019-11-05 17:02:43 -08001182 /* Use file content as dictionary */
Yann Collet610c41f2020-07-20 17:41:32 -07001183 case 'D': argument++; NEXT_FIELD(dictFileName); break;
Yann Colletfcf20872016-05-29 05:16:05 +02001184
Yann Colletb09f5932019-11-05 17:02:43 -08001185 /* Overwrite */
W. Felix Handte33f3e292021-05-04 16:24:46 -04001186 case 'f': FIO_overwriteMode(prefs); forceStdin=1; forceStdout=1; followLinks=1; allowBlockDevices=1; argument++; break;
Yann Colletfcf20872016-05-29 05:16:05 +02001187
Yann Colletb09f5932019-11-05 17:02:43 -08001188 /* Verbose mode */
1189 case 'v': g_displayLevel++; argument++; break;
Yann Colletfcf20872016-05-29 05:16:05 +02001190
Yann Colletb09f5932019-11-05 17:02:43 -08001191 /* Quiet mode */
1192 case 'q': g_displayLevel--; argument++; break;
Yann Collet2c4acda2016-06-02 17:05:50 +02001193
Yann Colletb09f5932019-11-05 17:02:43 -08001194 /* keep source file (default) */
Elliott Hughes44aba642023-09-12 20:18:59 +00001195 case 'k': removeSrcFile=0; argument++; break;
Yann Colletfcf20872016-05-29 05:16:05 +02001196
Yann Colletb09f5932019-11-05 17:02:43 -08001197 /* Checksum */
1198 case 'C': FIO_setChecksumFlag(prefs, 2); argument++; break;
Yann Colletfcf20872016-05-29 05:16:05 +02001199
Yann Colletb09f5932019-11-05 17:02:43 -08001200 /* test compressed file */
1201 case 't': operation=zom_test; argument++; break;
Yann Colletfcf20872016-05-29 05:16:05 +02001202
Yann Colletb09f5932019-11-05 17:02:43 -08001203 /* destination file name */
Yann Collet610c41f2020-07-20 17:41:32 -07001204 case 'o': argument++; NEXT_FIELD(outFileName); break;
Yann Collet7f86ae22019-10-17 15:27:25 -07001205
Bimba Shresthaf25a6e92020-01-10 14:25:24 -08001206 /* limit memory */
Yann Colletb09f5932019-11-05 17:02:43 -08001207 case 'M':
1208 argument++;
1209 memLimit = readU32FromChar(&argument);
1210 break;
1211 case 'l': operation=zom_list; argument++; break;
Yann Colletc4cc9bf2016-09-13 17:50:08 +02001212#ifdef UTIL_HAS_CREATEFILELIST
Yann Colletb09f5932019-11-05 17:02:43 -08001213 /* recursive */
1214 case 'r': recursive=1; argument++; break;
Yann Colletc4cc9bf2016-09-13 17:50:08 +02001215#endif
Yann Colletf9cac7a2016-07-04 18:16:16 +02001216
Yann Colletc8431422016-09-01 15:05:57 -07001217#ifndef ZSTD_NOBENCH
Yann Colletb09f5932019-11-05 17:02:43 -08001218 /* Benchmark */
1219 case 'b':
1220 operation=zom_bench;
1221 argument++;
1222 break;
Yann Colletf9cac7a2016-07-04 18:16:16 +02001223
Yann Colletb09f5932019-11-05 17:02:43 -08001224 /* range bench (benchmark only) */
1225 case 'e':
1226 /* compression Level */
1227 argument++;
1228 cLevelLast = (int)readU32FromChar(&argument);
1229 break;
Yann Colletf9cac7a2016-07-04 18:16:16 +02001230
Yann Colletb09f5932019-11-05 17:02:43 -08001231 /* Modify Nb Iterations (benchmark only) */
1232 case 'i':
1233 argument++;
1234 bench_nbSeconds = readU32FromChar(&argument);
1235 break;
Yann Colletfcf20872016-05-29 05:16:05 +02001236
Yann Colletb09f5932019-11-05 17:02:43 -08001237 /* cut input into blocks (benchmark only) */
1238 case 'B':
1239 argument++;
1240 blockSize = readU32FromChar(&argument);
1241 break;
Yann Collet74745262017-01-02 02:05:45 +01001242
Yann Colletb09f5932019-11-05 17:02:43 -08001243 /* benchmark files separately (hidden option) */
1244 case 'S':
1245 argument++;
1246 separateFiles = 1;
1247 break;
Yann Colletd898fb72017-11-17 00:22:55 -08001248
Yann Collet500014a2017-01-19 16:59:56 -08001249#endif /* ZSTD_NOBENCH */
1250
Yann Colletb09f5932019-11-05 17:02:43 -08001251 /* nb of threads (hidden option) */
1252 case 'T':
1253 argument++;
Elliott Hughes44aba642023-09-12 20:18:59 +00001254 nbWorkers = readU32FromChar(&argument);
George Lua8eea992018-06-19 10:58:22 -07001255 break;
1256
Yann Colletb09f5932019-11-05 17:02:43 -08001257 /* Dictionary Selection level */
1258 case 's':
1259 argument++;
1260 dictSelect = readU32FromChar(&argument);
1261 break;
1262
1263 /* Pause at the end (-p) or set an additional param (-p#) (hidden option) */
1264 case 'p': argument++;
1265#ifndef ZSTD_NOBENCH
1266 if ((*argument>='0') && (*argument<='9')) {
1267 benchParams.additionalParam = (int)readU32FromChar(&argument);
1268 } else
1269#endif
1270 main_pause=1;
1271 break;
1272
1273 /* Select compressibility of synthetic sample */
1274 case 'P':
Yann Collet610c41f2020-07-20 17:41:32 -07001275 argument++;
Yann Colletb09f5932019-11-05 17:02:43 -08001276 compressibility = (double)readU32FromChar(&argument) / 100;
Yann Collet610c41f2020-07-20 17:41:32 -07001277 break;
Yann Colletf9cac7a2016-07-04 18:16:16 +02001278
Yann Colletb09f5932019-11-05 17:02:43 -08001279 /* unknown command */
Yann Collet4b4de742019-12-02 16:08:08 -08001280 default : badusage(programName); CLEAN_RETURN(1);
Yann Colletb09f5932019-11-05 17:02:43 -08001281 }
Yann Colletfcf20872016-05-29 05:16:05 +02001282 }
Yann Colletb09f5932019-11-05 17:02:43 -08001283 continue;
1284 } /* if (argument[0]=='-') */
Yann Colletf9cac7a2016-07-04 18:16:16 +02001285
Yann Colletb09f5932019-11-05 17:02:43 -08001286 /* none of the above : add filename to list */
1287 UTIL_refFilename(filenames, argument);
Yann Colletfcf20872016-05-29 05:16:05 +02001288 }
1289
1290 /* Welcome message (if verbose) */
1291 DISPLAYLEVEL(3, WELCOME_MESSAGE);
1292
Yann Collet5cb11442018-02-13 14:56:35 -08001293#ifdef ZSTD_MULTITHREAD
Elliott Hughes44aba642023-09-12 20:18:59 +00001294 if ((operation==zom_decompress) && (!singleThread) && (nbWorkers > 1)) {
1295 DISPLAYLEVEL(2, "Warning : decompression does not support multi-threading\n");
1296 }
Yann Collet5cb11442018-02-13 14:56:35 -08001297 if ((nbWorkers==0) && (!singleThread)) {
Yann Collet209df522018-02-01 19:29:30 -08001298 /* automatically set # workers based on # of reported cpus */
Binh Vo6a46e382021-06-16 09:38:43 -04001299 if (defaultLogicalCores) {
Elliott Hughes44aba642023-09-12 20:18:59 +00001300 nbWorkers = (unsigned)UTIL_countLogicalCores();
Binh Vo6a46e382021-06-16 09:38:43 -04001301 DISPLAYLEVEL(3, "Note: %d logical core(s) detected \n", nbWorkers);
1302 } else {
Elliott Hughes44aba642023-09-12 20:18:59 +00001303 nbWorkers = (unsigned)UTIL_countPhysicalCores();
Binh Vo6a46e382021-06-16 09:38:43 -04001304 DISPLAYLEVEL(3, "Note: %d physical core(s) detected \n", nbWorkers);
1305 }
Sean Purcellafa48512017-04-13 12:28:28 -07001306 }
cyan4973f741fb82018-06-26 01:22:45 -07001307#else
Elliott Hughes44aba642023-09-12 20:18:59 +00001308 (void)singleThread; (void)nbWorkers; (void)defaultLogicalCores;
Yann Collet5cb11442018-02-13 14:56:35 -08001309#endif
Sean Purcell680e4e02017-03-23 11:52:09 -07001310
Sean Purcelldee08ca2017-03-23 12:09:35 -07001311 g_utilDisplayLevel = g_displayLevel;
W. Felix Handte464bfb02021-06-09 15:22:59 -04001312
1313#ifdef UTIL_HAS_CREATEFILELIST
Sean Purcell680e4e02017-03-23 11:52:09 -07001314 if (!followLinks) {
Yann Colletb09f5932019-11-05 17:02:43 -08001315 unsigned u, fileNamesNb;
1316 unsigned const nbFilenames = (unsigned)filenames->tableSize;
1317 for (u=0, fileNamesNb=0; u<nbFilenames; u++) {
Yann Colletc71bd452019-11-26 11:20:26 -08001318 if ( UTIL_isLink(filenames->fileNames[u])
1319 && !UTIL_isFIFO(filenames->fileNames[u])
Bimba Shrestha0b52d872019-10-25 14:06:50 -07001320 ) {
Yann Colletc71bd452019-11-26 11:20:26 -08001321 DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring \n", filenames->fileNames[u]);
Sean Purcell680e4e02017-03-23 11:52:09 -07001322 } else {
Yann Colletb09f5932019-11-05 17:02:43 -08001323 filenames->fileNames[fileNamesNb++] = filenames->fileNames[u];
Yann Colletb40eace2019-10-25 18:16:45 -07001324 } }
Yann Colletb09f5932019-11-05 17:02:43 -08001325 if (fileNamesNb == 0 && nbFilenames > 0) /* all names are eliminated */
Björn Ketelaars482b84f2019-02-12 00:07:32 +01001326 CLEAN_RETURN(1);
Yann Colletb09f5932019-11-05 17:02:43 -08001327 filenames->tableSize = fileNamesNb;
1328 } /* if (!followLinks) */
1329
1330 /* read names from a file */
1331 if (file_of_names->tableSize) {
1332 size_t const nbFileLists = file_of_names->tableSize;
1333 size_t flNb;
1334 for (flNb=0; flNb < nbFileLists; flNb++) {
1335 FileNamesTable* const fnt = UTIL_createFileNamesTable_fromFileName(file_of_names->fileNames[flNb]);
1336 if (fnt==NULL) {
1337 DISPLAYLEVEL(1, "zstd: error reading %s \n", file_of_names->fileNames[flNb]);
1338 CLEAN_RETURN(1);
1339 }
Yann Collet31a0abb2019-11-06 09:10:05 -08001340 filenames = UTIL_mergeFileNamesTable(filenames, fnt);
Yann Colletb09f5932019-11-05 17:02:43 -08001341 }
Sean Purcell680e4e02017-03-23 11:52:09 -07001342 }
Yann Colletb09f5932019-11-05 17:02:43 -08001343
Elliott Hughes44aba642023-09-12 20:18:59 +00001344 nbInputFileNames = filenames->tableSize; /* saving number of input files */
1345
Yann Collet0977f7e2016-09-21 12:24:43 +02001346 if (recursive) { /* at this stage, filenameTable is a list of paths, which can contain both files and directories */
Yann Collet31a0abb2019-11-06 09:10:05 -08001347 UTIL_expandFNT(&filenames, followLinks);
Yann Colletb09f5932019-11-05 17:02:43 -08001348 }
cyan4973f741fb82018-06-26 01:22:45 -07001349#else
1350 (void)followLinks;
Yann Colletfcf20872016-05-29 05:16:05 +02001351#endif
Yann Collet166645e2017-08-18 18:30:41 -07001352
Paul Cruzb3e62442017-06-15 17:46:49 -07001353 if (operation == zom_list) {
Yann Collet166645e2017-08-18 18:30:41 -07001354#ifndef ZSTD_NODECOMPRESS
Yann Colletb09f5932019-11-05 17:02:43 -08001355 int const ret = FIO_listMultipleFiles((unsigned)filenames->tableSize, filenames->fileNames, g_displayLevel);
Paul Cruzbda57752017-06-20 12:43:10 -07001356 CLEAN_RETURN(ret);
Yann Collet166645e2017-08-18 18:30:41 -07001357#else
Elliott Hughes44aba642023-09-12 20:18:59 +00001358 DISPLAYLEVEL(1, "file information is not supported \n");
Yann Collet166645e2017-08-18 18:30:41 -07001359 CLEAN_RETURN(1);
1360#endif
Paul Cruz901435e2017-06-05 14:45:31 -07001361 }
Yann Collet166645e2017-08-18 18:30:41 -07001362
Yann Colletfcf20872016-05-29 05:16:05 +02001363 /* Check if benchmark is selected */
Yann Collet22de81e2016-10-28 13:58:31 -07001364 if (operation==zom_bench) {
Yann Colletfcf20872016-05-29 05:16:05 +02001365#ifndef ZSTD_NOBENCH
Elliott Hughes44aba642023-09-12 20:18:59 +00001366 if (cType != FIO_zstdCompression) {
1367 DISPLAYLEVEL(1, "benchmark mode is only compatible with zstd format \n");
1368 CLEAN_RETURN(1);
1369 }
Yann Collet5203f012018-08-03 07:54:29 -07001370 benchParams.blockSize = blockSize;
Elliott Hughes44aba642023-09-12 20:18:59 +00001371 benchParams.nbWorkers = (int)nbWorkers;
Yann Collet7f86ae22019-10-17 15:27:25 -07001372 benchParams.realTime = (unsigned)setRealTimePrio;
Yann Collet5203f012018-08-03 07:54:29 -07001373 benchParams.nbSeconds = bench_nbSeconds;
1374 benchParams.ldmFlag = ldmFlag;
Yann Collet7f86ae22019-10-17 15:27:25 -07001375 benchParams.ldmMinMatch = (int)g_ldmMinMatch;
1376 benchParams.ldmHashLog = (int)g_ldmHashLog;
Elliott Hughes44aba642023-09-12 20:18:59 +00001377 benchParams.useRowMatchFinder = (int)useRowMatchFinder;
Stella Lau67d4a612017-09-02 21:10:36 -07001378 if (g_ldmBucketSizeLog != LDM_PARAM_DEFAULT) {
Yann Collet7f86ae22019-10-17 15:27:25 -07001379 benchParams.ldmBucketSizeLog = (int)g_ldmBucketSizeLog;
Stella Lau67d4a612017-09-02 21:10:36 -07001380 }
Yann Collet41c7d0b2018-11-21 14:36:57 -08001381 if (g_ldmHashRateLog != LDM_PARAM_DEFAULT) {
Yann Collet7f86ae22019-10-17 15:27:25 -07001382 benchParams.ldmHashRateLog = (int)g_ldmHashRateLog;
Stella Lau67d4a612017-09-02 21:10:36 -07001383 }
Nick Terrell19ca3fb2019-02-15 15:24:55 -08001384 benchParams.literalCompressionMode = literalCompressionMode;
George Lu0d1ee222018-06-15 16:21:08 -04001385
George Lua3c8b592018-06-18 15:06:31 -07001386 if (cLevel > ZSTD_maxCLevel()) cLevel = ZSTD_maxCLevel();
1387 if (cLevelLast > ZSTD_maxCLevel()) cLevelLast = ZSTD_maxCLevel();
1388 if (cLevelLast < cLevel) cLevelLast = cLevel;
Nick Terrell58b82192018-07-09 18:24:07 -07001389 if (cLevelLast > cLevel)
Yann Collet55affc02018-08-28 11:21:09 -07001390 DISPLAYLEVEL(3, "Benchmarking levels from %d to %d\n", cLevel, cLevelLast);
Yann Colletb09f5932019-11-05 17:02:43 -08001391 if (filenames->tableSize > 0) {
George Lua8eea992018-06-19 10:58:22 -07001392 if(separateFiles) {
1393 unsigned i;
Yann Colletb09f5932019-11-05 17:02:43 -08001394 for(i = 0; i < filenames->tableSize; i++) {
George Lua8eea992018-06-19 10:58:22 -07001395 int c;
Yann Colletb09f5932019-11-05 17:02:43 -08001396 DISPLAYLEVEL(3, "Benchmarking %s \n", filenames->fileNames[i]);
George Lua8eea992018-06-19 10:58:22 -07001397 for(c = cLevel; c <= cLevelLast; c++) {
Elliott Hughes44aba642023-09-12 20:18:59 +00001398 operationResult = BMK_benchFilesAdvanced(&filenames->fileNames[i], 1, dictFileName, c, &compressionParams, g_displayLevel, &benchParams);
Yann Collet1ead0c52019-10-25 16:36:59 -07001399 } }
George Lua8eea992018-06-19 10:58:22 -07001400 } else {
1401 for(; cLevel <= cLevelLast; cLevel++) {
Elliott Hughes44aba642023-09-12 20:18:59 +00001402 operationResult = BMK_benchFilesAdvanced(filenames->fileNames, (unsigned)filenames->tableSize, dictFileName, cLevel, &compressionParams, g_displayLevel, &benchParams);
Yann Collet1ead0c52019-10-25 16:36:59 -07001403 } }
George Lua3c8b592018-06-18 15:06:31 -07001404 } else {
1405 for(; cLevel <= cLevelLast; cLevel++) {
Elliott Hughes44aba642023-09-12 20:18:59 +00001406 operationResult = BMK_syntheticTest(cLevel, compressibility, &compressionParams, g_displayLevel, &benchParams);
Yann Collet1ead0c52019-10-25 16:36:59 -07001407 } }
George Lu0d1ee222018-06-15 16:21:08 -04001408
Yann Collet71f012e2017-12-01 17:42:46 -08001409#else
Yann Collet2fdab162018-08-03 08:30:01 -07001410 (void)bench_nbSeconds; (void)blockSize; (void)setRealTimePrio; (void)separateFiles; (void)compressibility;
Yann Colletfcf20872016-05-29 05:16:05 +02001411#endif
1412 goto _end;
1413 }
1414
1415 /* Check if dictionary builder is selected */
Yann Collet22de81e2016-10-28 13:58:31 -07001416 if (operation==zom_train) {
Yann Colletfcf20872016-05-29 05:16:05 +02001417#ifndef ZSTD_NODICT
Nick Terrell5b7fd7c2017-06-26 21:07:14 -07001418 ZDICT_params_t zParams;
1419 zParams.compressionLevel = dictCLevel;
Yann Collet7f86ae22019-10-17 15:27:25 -07001420 zParams.notificationLevel = (unsigned)g_displayLevel;
Nick Terrell5b7fd7c2017-06-26 21:07:14 -07001421 zParams.dictID = dictID;
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -07001422 if (dict == cover) {
Nick Terrellf376d472017-05-01 23:40:20 -07001423 int const optimize = !coverParams.k || !coverParams.d;
Yann Collet7f86ae22019-10-17 15:27:25 -07001424 coverParams.nbThreads = (unsigned)nbWorkers;
Nick Terrell5b7fd7c2017-06-26 21:07:14 -07001425 coverParams.zParams = zParams;
Elliot Gorokhovsky71c0c072021-12-10 16:19:40 -05001426 operationResult = DiB_trainFromFiles(outFileName, maxDictSize, filenames->fileNames, (int)filenames->tableSize, blockSize, NULL, &coverParams, NULL, optimize, memLimit);
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -07001427 } else if (dict == fastCover) {
1428 int const optimize = !fastCoverParams.k || !fastCoverParams.d;
Yann Collet7f86ae22019-10-17 15:27:25 -07001429 fastCoverParams.nbThreads = (unsigned)nbWorkers;
Jennifer Liu9d6ed9d2018-08-23 12:06:20 -07001430 fastCoverParams.zParams = zParams;
Elliot Gorokhovsky71c0c072021-12-10 16:19:40 -05001431 operationResult = DiB_trainFromFiles(outFileName, maxDictSize, filenames->fileNames, (int)filenames->tableSize, blockSize, NULL, NULL, &fastCoverParams, optimize, memLimit);
Nick Terrelldf8415c2016-12-31 21:08:24 -08001432 } else {
Nick Terrell5b7fd7c2017-06-26 21:07:14 -07001433 ZDICT_legacy_params_t dictParams;
Nick Terrelldf8415c2016-12-31 21:08:24 -08001434 memset(&dictParams, 0, sizeof(dictParams));
Nick Terrelldf8415c2016-12-31 21:08:24 -08001435 dictParams.selectivityLevel = dictSelect;
Nick Terrell5b7fd7c2017-06-26 21:07:14 -07001436 dictParams.zParams = zParams;
Elliot Gorokhovsky71c0c072021-12-10 16:19:40 -05001437 operationResult = DiB_trainFromFiles(outFileName, maxDictSize, filenames->fileNames, (int)filenames->tableSize, blockSize, &dictParams, NULL, NULL, 0, memLimit);
Nick Terrelldf8415c2016-12-31 21:08:24 -08001438 }
Yann Collet0250ac72018-09-24 00:52:19 -07001439#else
1440 (void)dictCLevel; (void)dictSelect; (void)dictID; (void)maxDictSize; /* not used when ZSTD_NODICT set */
1441 DISPLAYLEVEL(1, "training mode not available \n");
1442 operationResult = 1;
Yann Colletfcf20872016-05-29 05:16:05 +02001443#endif
1444 goto _end;
1445 }
1446
Nick Terrelld0b27482017-07-18 14:45:49 -07001447#ifndef ZSTD_NODECOMPRESS
Elliott Hughes44aba642023-09-12 20:18:59 +00001448 if (operation==zom_test) { FIO_setTestMode(prefs, 1); outFileName=nulmark; removeSrcFile=0; } /* test mode */
Nick Terrelld0b27482017-07-18 14:45:49 -07001449#endif
1450
Yann Colletfcf20872016-05-29 05:16:05 +02001451 /* No input filename ==> use stdin and stdout */
Elliott Hughes44aba642023-09-12 20:18:59 +00001452 if (filenames->tableSize == 0) {
1453 /* It is possible that the input
1454 was a number of empty directories. In this case
1455 stdin and stdout should not be used */
1456 if (nbInputFileNames > 0 ){
1457 DISPLAYLEVEL(1, "please provide correct input file(s) or non-empty directories -- ignored \n");
1458 CLEAN_RETURN(0);
1459 }
1460 UTIL_refFilename(filenames, stdinmark);
1461 }
1462
1463 if (filenames->tableSize == 1 && !strcmp(filenames->fileNames[0], stdinmark) && !outFileName)
Yann Collet6a9b41b2018-03-11 19:56:48 -07001464 outFileName = stdoutmark; /* when input is stdin, default output is stdout */
Yann Colletfcf20872016-05-29 05:16:05 +02001465
1466 /* Check if input/output defined as console; trigger an error in this case */
W. Felix Handte8b6a4b52021-01-11 17:53:20 -05001467 if (!forceStdin
Elliott Hughes44aba642023-09-12 20:18:59 +00001468 && (UTIL_searchFileNamesTable(filenames, stdinmark) != -1)
1469 && UTIL_isConsole(stdin) ) {
senhuang42ce568102020-09-28 12:15:18 -04001470 DISPLAYLEVEL(1, "stdin is a console, aborting\n");
Yann Collet4b4de742019-12-02 16:08:08 -08001471 CLEAN_RETURN(1);
1472 }
Elliott Hughes44aba642023-09-12 20:18:59 +00001473 if ( (!outFileName || !strcmp(outFileName, stdoutmark))
1474 && UTIL_isConsole(stdout)
1475 && (UTIL_searchFileNamesTable(filenames, stdinmark) != -1)
Yann Collet6a9b41b2018-03-11 19:56:48 -07001476 && !forceStdout
Yann Collet4b4de742019-12-02 16:08:08 -08001477 && operation!=zom_decompress ) {
senhuang42ce568102020-09-28 12:15:18 -04001478 DISPLAYLEVEL(1, "stdout is a console, aborting\n");
Yann Collet4b4de742019-12-02 16:08:08 -08001479 CLEAN_RETURN(1);
1480 }
Yann Colletfcf20872016-05-29 05:16:05 +02001481
inikep23cec872016-10-05 11:56:22 +02001482#ifndef ZSTD_NOCOMPRESS
Yann Colletfe0590f2016-08-12 18:04:15 +02001483 /* check compression level limits */
1484 { int const maxCLevel = ultra ? ZSTD_maxCLevel() : ZSTDCLI_CLEVEL_MAX;
1485 if (cLevel > maxCLevel) {
1486 DISPLAYLEVEL(2, "Warning : compression level higher than max, reduced to %i \n", maxCLevel);
1487 cLevel = maxCLevel;
1488 } }
inikep23cec872016-10-05 11:56:22 +02001489#endif
Yann Colletfe0590f2016-08-12 18:04:15 +02001490
Bimba Shrestha2f100192020-01-30 14:12:03 -08001491 if (showDefaultCParams) {
1492 if (operation == zom_decompress) {
Elliott Hughes44aba642023-09-12 20:18:59 +00001493 DISPLAYLEVEL(1, "error : can't use --show-default-cparams in decompression mode \n");
Bimba Shrestha2f100192020-01-30 14:12:03 -08001494 CLEAN_RETURN(1);
1495 }
Bimba Shrestha2f100192020-01-30 14:12:03 -08001496 }
1497
Bimba Shresthaf25a6e92020-01-10 14:25:24 -08001498 if (dictFileName != NULL && patchFromDictFileName != NULL) {
Elliott Hughes44aba642023-09-12 20:18:59 +00001499 DISPLAYLEVEL(1, "error : can't use -D and --patch-from=# at the same time \n");
Bimba Shresthaf25a6e92020-01-10 14:25:24 -08001500 CLEAN_RETURN(1);
1501 }
1502
Bimba Shrestha5b0a4522020-04-17 15:58:53 -05001503 if (patchFromDictFileName != NULL && filenames->tableSize > 1) {
Elliott Hughes44aba642023-09-12 20:18:59 +00001504 DISPLAYLEVEL(1, "error : can't use --patch-from=# on multiple files \n");
Bimba Shrestha5b0a4522020-04-17 15:58:53 -05001505 CLEAN_RETURN(1);
1506 }
Yann Collet3324e872021-01-07 10:37:27 -08001507
Elliott Hughes44aba642023-09-12 20:18:59 +00001508 /* No status message by default when output is stdout */
senhuang427259b252020-10-07 13:47:38 -04001509 hasStdout = outFileName && !strcmp(outFileName,stdoutmark);
Elliott Hughes44aba642023-09-12 20:18:59 +00001510 if (hasStdout && (g_displayLevel==2)) g_displayLevel=1;
Yann Collet8c85b292023-01-23 18:55:51 -08001511
Elliott Hughes44aba642023-09-12 20:18:59 +00001512 /* when stderr is not the console, do not pollute it with progress updates (unless requested) */
1513 if (!UTIL_isConsole(stderr) && (progress!=FIO_ps_always)) progress=FIO_ps_never;
1514 FIO_setProgressSetting(progress);
1515
1516 /* don't remove source files when output is stdout */;
1517 if (hasStdout && removeSrcFile) {
1518 DISPLAYLEVEL(3, "Note: src files are not removed when output is stdout \n");
1519 removeSrcFile = 0;
1520 }
1521 FIO_setRemoveSrcFile(prefs, removeSrcFile);
Yann Colletcee6bec2023-01-20 17:53:05 -08001522
Yann Colletfcf20872016-05-29 05:16:05 +02001523 /* IO Stream/File */
senhuang427259b252020-10-07 13:47:38 -04001524 FIO_setHasStdoutOutput(fCtx, hasStdout);
Yann Collet3324e872021-01-07 10:37:27 -08001525 FIO_setNbFilesTotal(fCtx, (int)filenames->tableSize);
senhuang42432186c2020-09-24 15:55:30 -04001526 FIO_determineHasStdinInput(fCtx, filenames);
Sean Purcell042ba122017-03-23 11:13:52 -07001527 FIO_setNotificationLevel(g_displayLevel);
W. Felix Handte33f3e292021-05-04 16:24:46 -04001528 FIO_setAllowBlockDevices(prefs, allowBlockDevices);
Bimba Shresthaf25a6e92020-01-10 14:25:24 -08001529 FIO_setPatchFromMode(prefs, patchFromDictFileName != NULL);
Elliott Hughes44aba642023-09-12 20:18:59 +00001530 FIO_setMMapDict(prefs, mmapDict);
Bimba Shresthaf25a6e92020-01-10 14:25:24 -08001531 if (memLimit == 0) {
1532 if (compressionParams.windowLog == 0) {
1533 memLimit = (U32)1 << g_defaultMaxWindowLog;
1534 } else {
1535 memLimit = (U32)1 << (compressionParams.windowLog & 31);
1536 } }
Bimba Shrestha5b0a4522020-04-17 15:58:53 -05001537 if (patchFromDictFileName != NULL)
1538 dictFileName = patchFromDictFileName;
Bimba Shresthaf25a6e92020-01-10 14:25:24 -08001539 FIO_setMemLimit(prefs, memLimit);
Yann Collet22de81e2016-10-28 13:58:31 -07001540 if (operation==zom_compress) {
Yann Collet0977f7e2016-09-21 12:24:43 +02001541#ifndef ZSTD_NOCOMPRESS
Elliott Hughes44aba642023-09-12 20:18:59 +00001542 FIO_setCompressionType(prefs, cType);
Bimba Shrestha6d8e7612020-03-09 14:12:52 -05001543 FIO_setContentSize(prefs, contentSize);
Elliott Hughes44aba642023-09-12 20:18:59 +00001544 FIO_setNbWorkers(prefs, (int)nbWorkers);
Yann Collet7f86ae22019-10-17 15:27:25 -07001545 FIO_setBlockSize(prefs, (int)blockSize);
1546 if (g_overlapLog!=OVERLAP_LOG_DEFAULT) FIO_setOverlapLog(prefs, (int)g_overlapLog);
1547 FIO_setLdmFlag(prefs, (unsigned)ldmFlag);
1548 FIO_setLdmHashLog(prefs, (int)g_ldmHashLog);
1549 FIO_setLdmMinMatch(prefs, (int)g_ldmMinMatch);
1550 if (g_ldmBucketSizeLog != LDM_PARAM_DEFAULT) FIO_setLdmBucketSizeLog(prefs, (int)g_ldmBucketSizeLog);
1551 if (g_ldmHashRateLog != LDM_PARAM_DEFAULT) FIO_setLdmHashRateLog(prefs, (int)g_ldmHashRateLog);
Elliott Hughes44aba642023-09-12 20:18:59 +00001552 FIO_setAdaptiveMode(prefs, adapt);
1553 FIO_setUseRowMatchFinder(prefs, (int)useRowMatchFinder);
Karl Ostmo5e220bf2019-01-22 17:31:13 -08001554 FIO_setAdaptMin(prefs, adaptMin);
1555 FIO_setAdaptMax(prefs, adaptMax);
1556 FIO_setRsyncable(prefs, rsyncable);
Nick Magerkoaf0c9502019-08-15 23:57:55 -07001557 FIO_setStreamSrcSize(prefs, streamSrcSize);
Ephraim Park90077012019-06-24 13:40:52 -07001558 FIO_setTargetCBlockSize(prefs, targetCBlockSize);
Nick Magerkodffbac52019-08-19 08:52:08 -07001559 FIO_setSrcSizeHint(prefs, srcSizeHint);
Nick Terrell19ca3fb2019-02-15 15:24:55 -08001560 FIO_setLiteralCompressionMode(prefs, literalCompressionMode);
Elliott Hughes44aba642023-09-12 20:18:59 +00001561 FIO_setSparseWrite(prefs, 0);
Yann Collet6c51bf42018-09-24 18:16:08 -07001562 if (adaptMin > cLevel) cLevel = adaptMin;
1563 if (adaptMax < cLevel) cLevel = adaptMax;
Stella Laua1f04d52017-09-01 14:52:51 -07001564
Bimba Shresthad1389ac2020-02-03 09:52:39 -08001565 /* Compare strategies constant with the ground truth */
1566 { ZSTD_bounds strategyBounds = ZSTD_cParam_getBounds(ZSTD_c_strategy);
W. Felix Handte376c26b2020-05-08 16:42:15 -04001567 assert(ZSTD_NB_STRATEGIES == strategyBounds.upperBound);
1568 (void)strategyBounds; }
Bimba Shrestha38d010e2020-01-31 10:47:17 -08001569
Kevin Svetlitski63fe6192021-11-05 12:48:13 -07001570 if (showDefaultCParams || g_displayLevel >= 4) {
Bimba Shrestha2f100192020-01-30 14:12:03 -08001571 size_t fileNb;
Bimba Shrestha2f100192020-01-30 14:12:03 -08001572 for (fileNb = 0; fileNb < (size_t)filenames->tableSize; fileNb++) {
Kevin Svetlitski63fe6192021-11-05 12:48:13 -07001573 if (showDefaultCParams)
1574 printDefaultCParams(filenames->fileNames[fileNb], dictFileName, cLevel);
1575 if (g_displayLevel >= 4)
1576 printActualCParams(filenames->fileNames[fileNb], dictFileName, cLevel, &compressionParams);
Bimba Shrestha2f100192020-01-30 14:12:03 -08001577 }
1578 }
1579
Kevin Svetlitski0665d4c2021-11-05 12:01:20 -07001580 if (g_displayLevel >= 4)
1581 FIO_displayCompressionParameters(prefs);
Yann Colletb09f5932019-11-05 17:02:43 -08001582 if ((filenames->tableSize==1) && outFileName)
senhuang4251234962020-09-07 13:13:05 -04001583 operationResult = FIO_compressFilename(fCtx, prefs, outFileName, filenames->fileNames[0], dictFileName, cLevel, compressionParams);
Yann Colletfcf20872016-05-29 05:16:05 +02001584 else
senhuang4251234962020-09-07 13:13:05 -04001585 operationResult = FIO_compressMultipleFilenames(fCtx, prefs, filenames->fileNames, outMirroredDirName, outDirName, outFileName, suffix, dictFileName, cLevel, compressionParams);
Yann Collet0977f7e2016-09-21 12:24:43 +02001586#else
Elliott Hughes44aba642023-09-12 20:18:59 +00001587 /* these variables are only used when compression mode is enabled */
1588 (void)contentSize; (void)suffix; (void)adapt; (void)rsyncable;
1589 (void)ultra; (void)cLevel; (void)ldmFlag; (void)literalCompressionMode;
1590 (void)targetCBlockSize; (void)streamSrcSize; (void)srcSizeHint;
1591 (void)ZSTD_strategyMap; (void)useRowMatchFinder; (void)cType;
1592 DISPLAYLEVEL(1, "Compression not supported \n");
Yann Colletfcf20872016-05-29 05:16:05 +02001593#endif
Yann Collet22de81e2016-10-28 13:58:31 -07001594 } else { /* decompression or test */
Yann Colletfcf20872016-05-29 05:16:05 +02001595#ifndef ZSTD_NODECOMPRESS
Yann Colletb09f5932019-11-05 17:02:43 -08001596 if (filenames->tableSize == 1 && outFileName) {
senhuang4251234962020-09-07 13:13:05 -04001597 operationResult = FIO_decompressFilename(fCtx, prefs, outFileName, filenames->fileNames[0], dictFileName);
Yann Collet1ead0c52019-10-25 16:36:59 -07001598 } else {
senhuang4251234962020-09-07 13:13:05 -04001599 operationResult = FIO_decompressMultipleFilenames(fCtx, prefs, filenames->fileNames, outMirroredDirName, outDirName, outFileName, dictFileName);
Yann Collet1ead0c52019-10-25 16:36:59 -07001600 }
Yann Colletfcf20872016-05-29 05:16:05 +02001601#else
Elliott Hughes44aba642023-09-12 20:18:59 +00001602 DISPLAYLEVEL(1, "Decompression not supported \n");
Yann Colletfcf20872016-05-29 05:16:05 +02001603#endif
1604 }
1605
1606_end:
Karl Ostmo5e220bf2019-01-22 17:31:13 -08001607 FIO_freePreferences(prefs);
senhuang4248bca102020-09-03 10:14:04 -04001608 FIO_freeContext(fCtx);
Yann Colletfcf20872016-05-29 05:16:05 +02001609 if (main_pause) waitEnter();
Yann Colletb09f5932019-11-05 17:02:43 -08001610 UTIL_freeFileNamesTable(filenames);
1611 UTIL_freeFileNamesTable(file_of_names);
Nick Terrell54a49982021-02-03 19:53:00 -08001612#ifndef ZSTD_NOTRACE
1613 TRACE_finish();
1614#endif
Yann Colletb40eace2019-10-25 18:16:45 -07001615
Yann Colletfcf20872016-05-29 05:16:05 +02001616 return operationResult;
1617}