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