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