Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This source code is licensed under the BSD-style license found in the |
| 6 | * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | */ |
Yann Collet | 5f53b03 | 2016-08-28 10:00:49 -0700 | [diff] [blame] | 9 | |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 10 | |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 11 | /*-************************************ |
Yann Collet | 2cac5b3 | 2016-07-13 14:15:08 +0200 | [diff] [blame] | 12 | * Tuning parameters |
| 13 | **************************************/ |
Yann Collet | 2b1a363 | 2016-07-13 15:16:00 +0200 | [diff] [blame] | 14 | #ifndef ZSTDCLI_CLEVEL_DEFAULT |
| 15 | # define ZSTDCLI_CLEVEL_DEFAULT 3 |
Yann Collet | 2cac5b3 | 2016-07-13 14:15:08 +0200 | [diff] [blame] | 16 | #endif |
| 17 | |
Yann Collet | fe0590f | 2016-08-12 18:04:15 +0200 | [diff] [blame] | 18 | #ifndef ZSTDCLI_CLEVEL_MAX |
Yann Collet | 0977f7e | 2016-09-21 12:24:43 +0200 | [diff] [blame] | 19 | # define ZSTDCLI_CLEVEL_MAX 19 /* when not using --ultra */ |
Yann Collet | fe0590f | 2016-08-12 18:04:15 +0200 | [diff] [blame] | 20 | #endif |
| 21 | |
Yann Collet | 2cac5b3 | 2016-07-13 14:15:08 +0200 | [diff] [blame] | 22 | |
Yann Collet | 8dafb1a | 2017-01-25 17:01:13 -0800 | [diff] [blame] | 23 | |
Yann Collet | 2cac5b3 | 2016-07-13 14:15:08 +0200 | [diff] [blame] | 24 | /*-************************************ |
Yann Collet | 52afb39 | 2016-11-16 08:50:54 -0800 | [diff] [blame] | 25 | * Dependencies |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 26 | **************************************/ |
Przemyslaw Skibinski | 7a8a03c | 2016-12-21 15:08:44 +0100 | [diff] [blame] | 27 | #include "platform.h" /* IS_CONSOLE, PLATFORM_POSIX_VERSION */ |
| 28 | #include "util.h" /* UTIL_HAS_CREATEFILELIST, UTIL_createFileList */ |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 29 | #include <string.h> /* strcmp, strlen */ |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 30 | #include <errno.h> /* errno */ |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 31 | #include "fileio.h" |
| 32 | #ifndef ZSTD_NOBENCH |
Przemyslaw Skibinski | 26306fc | 2016-11-03 11:38:01 +0100 | [diff] [blame] | 33 | # include "bench.h" /* BMK_benchFiles, BMK_SetNbSeconds */ |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 34 | #endif |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 35 | #ifndef ZSTD_NODICT |
| 36 | # include "dibio.h" |
| 37 | #endif |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 38 | #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel */ |
| 39 | #include "zstd.h" /* ZSTD_VERSION_STRING */ |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 40 | |
| 41 | |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 42 | /*-************************************ |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 43 | * Constants |
| 44 | **************************************/ |
| 45 | #define COMPRESSOR_NAME "zstd command line interface" |
| 46 | #ifndef ZSTD_VERSION |
| 47 | # define ZSTD_VERSION "v" ZSTD_VERSION_STRING |
| 48 | #endif |
| 49 | #define AUTHOR "Yann Collet" |
| 50 | #define WELCOME_MESSAGE "*** %s %i-bits %s, by %s ***\n", COMPRESSOR_NAME, (int)(sizeof(size_t)*8), ZSTD_VERSION, AUTHOR |
| 51 | |
Sean Purcell | 5a61f36 | 2017-04-14 11:33:04 -0700 | [diff] [blame] | 52 | #define ZSTD_ZSTDMT "zstdmt" |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 53 | #define ZSTD_UNZSTD "unzstd" |
Yann Collet | c09d16b | 2017-02-14 10:45:19 -0800 | [diff] [blame] | 54 | #define ZSTD_CAT "zstdcat" |
| 55 | #define ZSTD_GZ "gzip" |
| 56 | #define ZSTD_GUNZIP "gunzip" |
| 57 | #define ZSTD_GZCAT "gzcat" |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 58 | #define ZSTD_LZMA "lzma" |
| 59 | #define ZSTD_XZ "xz" |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 60 | |
| 61 | #define KB *(1 <<10) |
| 62 | #define MB *(1 <<20) |
| 63 | #define GB *(1U<<30) |
| 64 | |
David Lam | ac43e95 | 2016-08-12 14:49:05 -0700 | [diff] [blame] | 65 | #define DEFAULT_DISPLAY_LEVEL 2 |
| 66 | |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 67 | static const char* g_defaultDictName = "dictionary"; |
| 68 | static const unsigned g_defaultMaxDictSize = 110 KB; |
Yann Collet | c843142 | 2016-09-01 15:05:57 -0700 | [diff] [blame] | 69 | static const int g_defaultDictCLevel = 3; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 70 | static const unsigned g_defaultSelectivityLevel = 9; |
Yann Collet | 8d8513f | 2017-01-30 14:37:08 -0800 | [diff] [blame] | 71 | #define OVERLAP_LOG_DEFAULT 9999 |
| 72 | static U32 g_overlapLog = OVERLAP_LOG_DEFAULT; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 73 | |
| 74 | |
| 75 | /*-************************************ |
| 76 | * Display Macros |
| 77 | **************************************/ |
Sean Purcell | 042ba12 | 2017-03-23 11:13:52 -0700 | [diff] [blame] | 78 | #define DISPLAY(...) fprintf(g_displayOut, __VA_ARGS__) |
| 79 | #define DISPLAYLEVEL(l, ...) { if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } } |
| 80 | static int g_displayLevel = DEFAULT_DISPLAY_LEVEL; /* 0 : no display, 1: errors, 2 : + result + interaction + warnings, 3 : + progression, 4 : + information */ |
| 81 | static FILE* g_displayOut; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 82 | |
| 83 | |
| 84 | /*-************************************ |
| 85 | * Command Line |
| 86 | **************************************/ |
| 87 | static int usage(const char* programName) |
| 88 | { |
| 89 | DISPLAY( "Usage :\n"); |
| 90 | DISPLAY( " %s [args] [FILE(s)] [-o file]\n", programName); |
| 91 | DISPLAY( "\n"); |
| 92 | DISPLAY( "FILE : a filename\n"); |
| 93 | DISPLAY( " with no FILE, or when FILE is - , read standard input\n"); |
| 94 | DISPLAY( "Arguments :\n"); |
| 95 | #ifndef ZSTD_NOCOMPRESS |
inikep | 5a54870 | 2016-08-18 09:00:25 +0200 | [diff] [blame] | 96 | DISPLAY( " -# : # compression level (1-%d, default:%d) \n", ZSTDCLI_CLEVEL_MAX, ZSTDCLI_CLEVEL_DEFAULT); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 97 | #endif |
| 98 | #ifndef ZSTD_NODECOMPRESS |
| 99 | DISPLAY( " -d : decompression \n"); |
| 100 | #endif |
| 101 | DISPLAY( " -D file: use `file` as Dictionary \n"); |
| 102 | DISPLAY( " -o file: result stored into `file` (only if 1 input file) \n"); |
Sean Purcell | 680e4e0 | 2017-03-23 11:52:09 -0700 | [diff] [blame] | 103 | DISPLAY( " -f : overwrite output without prompting and (de)compress links \n"); |
Yann Collet | a7a5d77 | 2016-07-02 00:37:32 +0200 | [diff] [blame] | 104 | DISPLAY( "--rm : remove source file(s) after successful de/compression \n"); |
Yann Collet | d916c90 | 2016-07-04 00:42:58 +0200 | [diff] [blame] | 105 | DISPLAY( " -k : preserve source file(s) (default) \n"); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 106 | DISPLAY( " -h/-H : display help/long help and exit\n"); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int usage_advanced(const char* programName) |
| 111 | { |
| 112 | DISPLAY(WELCOME_MESSAGE); |
| 113 | usage(programName); |
| 114 | DISPLAY( "\n"); |
| 115 | DISPLAY( "Advanced arguments :\n"); |
| 116 | DISPLAY( " -V : display Version number and exit\n"); |
Yann Collet | 805c5a3 | 2017-04-01 00:36:31 -0700 | [diff] [blame] | 117 | DISPLAY( " -v : verbose mode; specify multiple times to increase verbosity\n"); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 118 | DISPLAY( " -q : suppress warnings; specify twice to suppress errors too\n"); |
| 119 | DISPLAY( " -c : force write to standard output, even if it is the console\n"); |
Paul Cruz | 901435e | 2017-06-05 14:45:31 -0700 | [diff] [blame] | 120 | DISPLAY( " -l : print information about zstd compressed files.\n"); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 121 | #ifndef ZSTD_NOCOMPRESS |
Yann Collet | fe0590f | 2016-08-12 18:04:15 +0200 | [diff] [blame] | 122 | DISPLAY( "--ultra : enable levels beyond %i, up to %i (requires more memory)\n", ZSTDCLI_CLEVEL_MAX, ZSTD_maxCLevel()); |
Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 123 | #ifdef ZSTD_MULTITHREAD |
| 124 | DISPLAY( " -T# : use # threads for compression (default:1) \n"); |
Yann Collet | 805c5a3 | 2017-04-01 00:36:31 -0700 | [diff] [blame] | 125 | DISPLAY( " -B# : select size of each job (default:0==automatic) \n"); |
| 126 | #endif |
| 127 | DISPLAY( "--no-dictID : don't write dictID into header (dictionary compression)\n"); |
| 128 | DISPLAY( "--[no-]check : integrity check (default:enabled) \n"); |
| 129 | #endif |
| 130 | #ifdef UTIL_HAS_CREATEFILELIST |
| 131 | DISPLAY( " -r : operate recursively on directories \n"); |
Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 132 | #endif |
Przemyslaw Skibinski | cfd4dc2 | 2017-02-08 15:17:55 +0100 | [diff] [blame] | 133 | #ifdef ZSTD_GZCOMPRESS |
Przemyslaw Skibinski | 4f9eaa7 | 2017-02-08 18:08:09 +0100 | [diff] [blame] | 134 | DISPLAY( "--format=gzip : compress files to the .gz format \n"); |
Przemyslaw Skibinski | cfd4dc2 | 2017-02-08 15:17:55 +0100 | [diff] [blame] | 135 | #endif |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 136 | #ifdef ZSTD_LZMACOMPRESS |
| 137 | DISPLAY( "--format=xz : compress files to the .xz format \n"); |
| 138 | DISPLAY( "--format=lzma : compress files to the .lzma format \n"); |
| 139 | #endif |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 140 | #ifdef ZSTD_LZ4COMPRESS |
| 141 | DISPLAY( "--format=lz4 : compress files to the .lz4 format \n"); |
| 142 | #endif |
Yann Collet | 6381e99 | 2016-05-31 02:29:45 +0200 | [diff] [blame] | 143 | #ifndef ZSTD_NODECOMPRESS |
Yann Collet | 2c4acda | 2016-06-02 17:05:50 +0200 | [diff] [blame] | 144 | DISPLAY( "--test : test compressed file integrity \n"); |
Nick Terrell | 96fe545 | 2017-03-31 15:16:43 -0700 | [diff] [blame] | 145 | #if ZSTD_SPARSE_DEFAULT |
Yann Collet | b09b12c | 2016-06-09 22:59:51 +0200 | [diff] [blame] | 146 | DISPLAY( "--[no-]sparse : sparse mode (default:enabled on file, disabled on stdout)\n"); |
Nick Terrell | 96fe545 | 2017-03-31 15:16:43 -0700 | [diff] [blame] | 147 | #else |
| 148 | DISPLAY( "--[no-]sparse : sparse mode (default:disabled)\n"); |
| 149 | #endif |
Yann Collet | 6381e99 | 2016-05-31 02:29:45 +0200 | [diff] [blame] | 150 | #endif |
Yann Collet | d4cda27 | 2016-10-14 13:13:13 -0700 | [diff] [blame] | 151 | DISPLAY( " -M# : Set a memory usage limit for decompression \n"); |
Paul Cruz | 9437cc7 | 2017-06-15 15:07:54 -0700 | [diff] [blame^] | 152 | DISPLAY( "--list : list information about a zstd compressed file \n"); |
Yann Collet | 6003894 | 2016-09-16 18:52:52 +0200 | [diff] [blame] | 153 | DISPLAY( "-- : All arguments after \"--\" are treated as files \n"); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 154 | #ifndef ZSTD_NODICT |
| 155 | DISPLAY( "\n"); |
| 156 | DISPLAY( "Dictionary builder :\n"); |
Yann Collet | b09b12c | 2016-06-09 22:59:51 +0200 | [diff] [blame] | 157 | DISPLAY( "--train ## : create a dictionary from a training set of files \n"); |
Nick Terrell | f376d47 | 2017-05-01 23:40:20 -0700 | [diff] [blame] | 158 | DISPLAY( "--train-cover[=k=#,d=#,steps=#] : use the cover algorithm with optional args\n"); |
| 159 | DISPLAY( "--train-legacy[=s=#] : use the legacy algorithm with selectivity (default: %u)\n", g_defaultSelectivityLevel); |
Yann Collet | b09b12c | 2016-06-09 22:59:51 +0200 | [diff] [blame] | 160 | DISPLAY( " -o file : `file` is dictionary name (default: %s) \n", g_defaultDictName); |
Yann Collet | 96aa301 | 2017-03-24 16:04:29 -0700 | [diff] [blame] | 161 | DISPLAY( "--maxdict=# : limit dictionary to specified size (default : %u) \n", g_defaultMaxDictSize); |
Yann Collet | 96aa301 | 2017-03-24 16:04:29 -0700 | [diff] [blame] | 162 | DISPLAY( "--dictID=# : force dictionary ID to specified value (default: random)\n"); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 163 | #endif |
| 164 | #ifndef ZSTD_NOBENCH |
| 165 | DISPLAY( "\n"); |
| 166 | DISPLAY( "Benchmark arguments :\n"); |
| 167 | DISPLAY( " -b# : benchmark file(s), using # compression level (default : 1) \n"); |
| 168 | DISPLAY( " -e# : test all compression levels from -bX to # (default: 1)\n"); |
Yann Collet | a9febe8 | 2016-08-01 13:37:17 +0200 | [diff] [blame] | 169 | DISPLAY( " -i# : minimum evaluation time in seconds (default : 3s)\n"); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 170 | DISPLAY( " -B# : cut file into independent blocks of size # (default: no block)\n"); |
Przemyslaw Skibinski | 40580ff | 2017-02-08 13:49:06 +0100 | [diff] [blame] | 171 | DISPLAY( "--priority=rt : set process priority to real-time\n"); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 172 | #endif |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static int badusage(const char* programName) |
| 177 | { |
| 178 | DISPLAYLEVEL(1, "Incorrect parameters\n"); |
Sean Purcell | 042ba12 | 2017-03-23 11:13:52 -0700 | [diff] [blame] | 179 | if (g_displayLevel >= 2) usage(programName); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 180 | return 1; |
| 181 | } |
| 182 | |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 183 | static void waitEnter(void) |
| 184 | { |
| 185 | int unused; |
| 186 | DISPLAY("Press enter to continue...\n"); |
| 187 | unused = getchar(); |
| 188 | (void)unused; |
| 189 | } |
| 190 | |
Sean Purcell | ef30af8 | 2017-03-24 17:06:09 -0700 | [diff] [blame] | 191 | static const char* lastNameFromPath(const char* path) |
| 192 | { |
| 193 | const char* name = path; |
| 194 | if (strrchr(name, '/')) name = strrchr(name, '/') + 1; |
| 195 | if (strrchr(name, '\\')) name = strrchr(name, '\\') + 1; /* windows */ |
| 196 | return name; |
| 197 | } |
| 198 | |
| 199 | /*! exeNameMatch() : |
| 200 | @return : a non-zero value if exeName matches test, excluding the extension |
| 201 | */ |
| 202 | static int exeNameMatch(const char* exeName, const char* test) |
| 203 | { |
| 204 | return !strncmp(exeName, test, strlen(test)) && |
| 205 | (exeName[strlen(test)] == '\0' || exeName[strlen(test)] == '.'); |
| 206 | } |
| 207 | |
Yann Collet | 1dd7961 | 2016-06-03 15:14:09 +0200 | [diff] [blame] | 208 | /*! readU32FromChar() : |
Yann Collet | d4cda27 | 2016-10-14 13:13:13 -0700 | [diff] [blame] | 209 | @return : unsigned integer value read from input in `char` format |
| 210 | allows and interprets K, KB, KiB, M, MB and MiB suffix. |
Yann Collet | 1dd7961 | 2016-06-03 15:14:09 +0200 | [diff] [blame] | 211 | Will also modify `*stringPtr`, advancing it to position where it stopped reading. |
Yann Collet | d4cda27 | 2016-10-14 13:13:13 -0700 | [diff] [blame] | 212 | Note : function result can overflow if digit string > MAX_UINT */ |
Yann Collet | 1dd7961 | 2016-06-03 15:14:09 +0200 | [diff] [blame] | 213 | static unsigned readU32FromChar(const char** stringPtr) |
| 214 | { |
| 215 | unsigned result = 0; |
| 216 | while ((**stringPtr >='0') && (**stringPtr <='9')) |
| 217 | result *= 10, result += **stringPtr - '0', (*stringPtr)++ ; |
Yann Collet | 33fdd09 | 2016-10-17 17:48:48 -0700 | [diff] [blame] | 218 | if ((**stringPtr=='K') || (**stringPtr=='M')) { |
| 219 | result <<= 10; |
| 220 | if (**stringPtr=='M') result <<= 10; |
| 221 | (*stringPtr)++ ; |
| 222 | if (**stringPtr=='i') (*stringPtr)++; |
| 223 | if (**stringPtr=='B') (*stringPtr)++; |
| 224 | } |
Yann Collet | 1dd7961 | 2016-06-03 15:14:09 +0200 | [diff] [blame] | 225 | return result; |
| 226 | } |
| 227 | |
Yann Collet | 743b33f | 2016-12-02 15:18:57 -0800 | [diff] [blame] | 228 | /** longCommandWArg() : |
Yann Collet | 6ccd37c | 2017-01-30 13:07:24 -0800 | [diff] [blame] | 229 | * check if *stringPtr is the same as longCommand. |
Yann Collet | 743b33f | 2016-12-02 15:18:57 -0800 | [diff] [blame] | 230 | * If yes, @return 1 and advances *stringPtr to the position which immediately follows longCommand. |
| 231 | * @return 0 and doesn't modify *stringPtr otherwise. |
| 232 | */ |
Yann Collet | 70077bc | 2016-10-14 14:41:17 -0700 | [diff] [blame] | 233 | static unsigned longCommandWArg(const char** stringPtr, const char* longCommand) |
| 234 | { |
| 235 | size_t const comSize = strlen(longCommand); |
Yann Collet | 743b33f | 2016-12-02 15:18:57 -0800 | [diff] [blame] | 236 | int const result = !strncmp(*stringPtr, longCommand, comSize); |
Yann Collet | 70077bc | 2016-10-14 14:41:17 -0700 | [diff] [blame] | 237 | if (result) *stringPtr += comSize; |
| 238 | return result; |
| 239 | } |
| 240 | |
Przemyslaw Skibinski | 671f28d | 2016-12-13 12:18:07 +0100 | [diff] [blame] | 241 | |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 242 | #ifndef ZSTD_NODICT |
| 243 | /** |
| 244 | * parseCoverParameters() : |
Nick Terrell | f376d47 | 2017-05-01 23:40:20 -0700 | [diff] [blame] | 245 | * reads cover parameters from *stringPtr (e.g. "--train-cover=k=48,d=8,steps=32") into *params |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 246 | * @return 1 means that cover parameters were correct |
| 247 | * @return 0 in case of malformed parameters |
| 248 | */ |
Nick Terrell | f376d47 | 2017-05-01 23:40:20 -0700 | [diff] [blame] | 249 | static unsigned parseCoverParameters(const char* stringPtr, COVER_params_t* params) |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 250 | { |
| 251 | memset(params, 0, sizeof(*params)); |
| 252 | for (; ;) { |
Nick Terrell | 3a1fefc | 2017-01-02 12:40:43 -0800 | [diff] [blame] | 253 | if (longCommandWArg(&stringPtr, "k=")) { params->k = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; } |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 254 | if (longCommandWArg(&stringPtr, "d=")) { params->d = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; } |
Nick Terrell | 3a1fefc | 2017-01-02 12:40:43 -0800 | [diff] [blame] | 255 | if (longCommandWArg(&stringPtr, "steps=")) { params->steps = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; } |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | if (stringPtr[0] != 0) return 0; |
Nick Terrell | f376d47 | 2017-05-01 23:40:20 -0700 | [diff] [blame] | 259 | DISPLAYLEVEL(4, "cover: k=%u\nd=%u\nsteps=%u\n", params->k, params->d, params->steps); |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 260 | return 1; |
| 261 | } |
Nick Terrell | f376d47 | 2017-05-01 23:40:20 -0700 | [diff] [blame] | 262 | |
| 263 | /** |
| 264 | * parseLegacyParameters() : |
| 265 | * reads legacy dictioanry builter parameters from *stringPtr (e.g. "--train-legacy=selectivity=8") into *selectivity |
| 266 | * @return 1 means that legacy dictionary builder parameters were correct |
| 267 | * @return 0 in case of malformed parameters |
| 268 | */ |
| 269 | static unsigned parseLegacyParameters(const char* stringPtr, unsigned* selectivity) |
| 270 | { |
| 271 | if (!longCommandWArg(&stringPtr, "s=") && !longCommandWArg(&stringPtr, "selectivity=")) { return 0; } |
| 272 | *selectivity = readU32FromChar(&stringPtr); |
| 273 | if (stringPtr[0] != 0) return 0; |
| 274 | DISPLAYLEVEL(4, "legacy: selectivity=%u\n", *selectivity); |
| 275 | return 1; |
| 276 | } |
| 277 | |
| 278 | static COVER_params_t defaultCoverParams(void) |
| 279 | { |
| 280 | COVER_params_t params; |
| 281 | memset(¶ms, 0, sizeof(params)); |
| 282 | params.d = 8; |
| 283 | params.steps = 4; |
| 284 | return params; |
| 285 | } |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 286 | #endif |
Yann Collet | 6ccd37c | 2017-01-30 13:07:24 -0800 | [diff] [blame] | 287 | |
| 288 | |
Przemyslaw Skibinski | 671f28d | 2016-12-13 12:18:07 +0100 | [diff] [blame] | 289 | /** parseCompressionParameters() : |
| 290 | * reads compression parameters from *stringPtr (e.g. "--zstd=wlog=23,clog=23,hlog=22,slog=6,slen=3,tlen=48,strat=6") into *params |
| 291 | * @return 1 means that compression parameters were correct |
| 292 | * @return 0 in case of malformed parameters |
| 293 | */ |
| 294 | static unsigned parseCompressionParameters(const char* stringPtr, ZSTD_compressionParameters* params) |
| 295 | { |
| 296 | for ( ; ;) { |
Przemyslaw Skibinski | 671f28d | 2016-12-13 12:18:07 +0100 | [diff] [blame] | 297 | if (longCommandWArg(&stringPtr, "windowLog=") || longCommandWArg(&stringPtr, "wlog=")) { params->windowLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; } |
| 298 | if (longCommandWArg(&stringPtr, "chainLog=") || longCommandWArg(&stringPtr, "clog=")) { params->chainLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; } |
| 299 | if (longCommandWArg(&stringPtr, "hashLog=") || longCommandWArg(&stringPtr, "hlog=")) { params->hashLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; } |
| 300 | if (longCommandWArg(&stringPtr, "searchLog=") || longCommandWArg(&stringPtr, "slog=")) { params->searchLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; } |
| 301 | if (longCommandWArg(&stringPtr, "searchLength=") || longCommandWArg(&stringPtr, "slen=")) { params->searchLength = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; } |
| 302 | if (longCommandWArg(&stringPtr, "targetLength=") || longCommandWArg(&stringPtr, "tlen=")) { params->targetLength = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; } |
Przemyslaw Skibinski | c71e552 | 2016-12-13 20:04:32 +0100 | [diff] [blame] | 303 | if (longCommandWArg(&stringPtr, "strategy=") || longCommandWArg(&stringPtr, "strat=")) { params->strategy = (ZSTD_strategy)(1 + readU32FromChar(&stringPtr)); if (stringPtr[0]==',') { stringPtr++; continue; } else break; } |
Yann Collet | 6ccd37c | 2017-01-30 13:07:24 -0800 | [diff] [blame] | 304 | if (longCommandWArg(&stringPtr, "overlapLog=") || longCommandWArg(&stringPtr, "ovlog=")) { g_overlapLog = readU32FromChar(&stringPtr); if (stringPtr[0]==',') { stringPtr++; continue; } else break; } |
Przemyslaw Skibinski | 671f28d | 2016-12-13 12:18:07 +0100 | [diff] [blame] | 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | if (stringPtr[0] != 0) return 0; /* check the end of string */ |
Przemyslaw Skibinski | 897b8bb | 2016-12-13 13:03:41 +0100 | [diff] [blame] | 309 | DISPLAYLEVEL(4, "windowLog=%d\nchainLog=%d\nhashLog=%d\nsearchLog=%d\n", params->windowLog, params->chainLog, params->hashLog, params->searchLog); |
| 310 | DISPLAYLEVEL(4, "searchLength=%d\ntargetLength=%d\nstrategy=%d\n", params->searchLength, params->targetLength, params->strategy); |
Przemyslaw Skibinski | 671f28d | 2016-12-13 12:18:07 +0100 | [diff] [blame] | 311 | return 1; |
| 312 | } |
| 313 | |
| 314 | |
Paul Cruz | 901435e | 2017-06-05 14:45:31 -0700 | [diff] [blame] | 315 | typedef enum { zom_compress, zom_decompress, zom_test, zom_bench, zom_train, zom_list } zstd_operation_mode; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 316 | |
| 317 | #define CLEAN_RETURN(i) { operationResult = (i); goto _end; } |
| 318 | |
Yann Collet | 0977f7e | 2016-09-21 12:24:43 +0200 | [diff] [blame] | 319 | int main(int argCount, const char* argv[]) |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 320 | { |
| 321 | int argNb, |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 322 | forceStdout=0, |
Sean Purcell | 680e4e0 | 2017-03-23 11:52:09 -0700 | [diff] [blame] | 323 | followLinks=0, |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 324 | main_pause=0, |
| 325 | nextEntryIsDictionary=0, |
| 326 | operationResult=0, |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 327 | nextArgumentIsOutFileName=0, |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 328 | nextArgumentIsMaxDict=0, |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 329 | nextArgumentIsDictID=0, |
Yann Collet | 743b33f | 2016-12-02 15:18:57 -0800 | [diff] [blame] | 330 | nextArgumentsAreFiles=0, |
Yann Collet | 27b5ac6 | 2016-09-21 14:20:56 +0200 | [diff] [blame] | 331 | ultra=0, |
Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 332 | lastCommand = 0, |
Przemyslaw Skibinski | d05014c | 2017-02-07 16:48:01 +0100 | [diff] [blame] | 333 | nbThreads = 1, |
| 334 | setRealTimePrio = 0; |
Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 335 | unsigned bench_nbSeconds = 3; /* would be better if this value was synchronized from bench */ |
| 336 | size_t blockSize = 0; |
Yann Collet | 22de81e | 2016-10-28 13:58:31 -0700 | [diff] [blame] | 337 | zstd_operation_mode operation = zom_compress; |
Przemyslaw Skibinski | 671f28d | 2016-12-13 12:18:07 +0100 | [diff] [blame] | 338 | ZSTD_compressionParameters compressionParams; |
Yann Collet | 4110534 | 2016-07-27 15:09:11 +0200 | [diff] [blame] | 339 | int cLevel = ZSTDCLI_CLEVEL_DEFAULT; |
| 340 | int cLevelLast = 1; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 341 | unsigned recursive = 0; |
Yann Collet | d4cda27 | 2016-10-14 13:13:13 -0700 | [diff] [blame] | 342 | unsigned memLimit = 0; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 343 | const char** filenameTable = (const char**)malloc(argCount * sizeof(const char*)); /* argCount >= 1 */ |
| 344 | unsigned filenameIdx = 0; |
| 345 | const char* programName = argv[0]; |
| 346 | const char* outFileName = NULL; |
| 347 | const char* dictFileName = NULL; |
Przemyslaw Skibinski | cb56306 | 2017-02-08 17:37:14 +0100 | [diff] [blame] | 348 | const char* suffix = ZSTD_EXTENSION; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 349 | unsigned maxDictSize = g_defaultMaxDictSize; |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 350 | unsigned dictID = 0; |
Yann Collet | 4110534 | 2016-07-27 15:09:11 +0200 | [diff] [blame] | 351 | int dictCLevel = g_defaultDictCLevel; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 352 | unsigned dictSelect = g_defaultSelectivityLevel; |
| 353 | #ifdef UTIL_HAS_CREATEFILELIST |
Yann Collet | 0977f7e | 2016-09-21 12:24:43 +0200 | [diff] [blame] | 354 | const char** extendedFileList = NULL; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 355 | char* fileNamesBuf = NULL; |
| 356 | unsigned fileNamesNb; |
| 357 | #endif |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 358 | #ifndef ZSTD_NODICT |
Nick Terrell | f376d47 | 2017-05-01 23:40:20 -0700 | [diff] [blame] | 359 | COVER_params_t coverParams = defaultCoverParams(); |
| 360 | int cover = 1; |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 361 | #endif |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 362 | |
| 363 | /* init */ |
Yann Collet | f3a1a84 | 2016-06-05 01:05:01 +0200 | [diff] [blame] | 364 | (void)recursive; (void)cLevelLast; /* not used when ZSTD_NOBENCH set */ |
Yann Collet | 407a11f | 2016-11-03 15:52:01 -0700 | [diff] [blame] | 365 | (void)dictCLevel; (void)dictSelect; (void)dictID; (void)maxDictSize; /* not used when ZSTD_NODICT set */ |
| 366 | (void)ultra; (void)cLevel; /* not used when ZSTD_NOCOMPRESS set */ |
| 367 | (void)memLimit; /* not used when ZSTD_NODECOMPRESS set */ |
Yann Collet | d916c90 | 2016-07-04 00:42:58 +0200 | [diff] [blame] | 368 | if (filenameTable==NULL) { DISPLAY("zstd: %s \n", strerror(errno)); exit(1); } |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 369 | filenameTable[0] = stdinmark; |
Sean Purcell | 042ba12 | 2017-03-23 11:13:52 -0700 | [diff] [blame] | 370 | g_displayOut = stderr; |
Sean Purcell | ef30af8 | 2017-03-24 17:06:09 -0700 | [diff] [blame] | 371 | |
| 372 | programName = lastNameFromPath(programName); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 373 | |
| 374 | /* preset behaviors */ |
Sean Purcell | 5a61f36 | 2017-04-14 11:33:04 -0700 | [diff] [blame] | 375 | if (exeNameMatch(programName, ZSTD_ZSTDMT)) nbThreads=0; |
Sean Purcell | ef30af8 | 2017-03-24 17:06:09 -0700 | [diff] [blame] | 376 | if (exeNameMatch(programName, ZSTD_UNZSTD)) operation=zom_decompress; |
| 377 | if (exeNameMatch(programName, ZSTD_CAT)) { operation=zom_decompress; forceStdout=1; FIO_overwriteMode(); outFileName=stdoutmark; g_displayLevel=1; } |
| 378 | if (exeNameMatch(programName, ZSTD_GZ)) { suffix = GZ_EXTENSION; FIO_setCompressionType(FIO_gzipCompression); FIO_setRemoveSrcFile(1); } /* behave like gzip */ |
| 379 | if (exeNameMatch(programName, ZSTD_GUNZIP)) { operation=zom_decompress; FIO_setRemoveSrcFile(1); } /* behave like gunzip */ |
| 380 | if (exeNameMatch(programName, ZSTD_GZCAT)) { operation=zom_decompress; forceStdout=1; FIO_overwriteMode(); outFileName=stdoutmark; g_displayLevel=1; } /* behave like gzcat */ |
| 381 | if (exeNameMatch(programName, ZSTD_LZMA)) { suffix = LZMA_EXTENSION; FIO_setCompressionType(FIO_lzmaCompression); FIO_setRemoveSrcFile(1); } /* behave like lzma */ |
| 382 | if (exeNameMatch(programName, ZSTD_XZ)) { suffix = XZ_EXTENSION; FIO_setCompressionType(FIO_xzCompression); FIO_setRemoveSrcFile(1); } /* behave like xz */ |
Przemyslaw Skibinski | 671f28d | 2016-12-13 12:18:07 +0100 | [diff] [blame] | 383 | memset(&compressionParams, 0, sizeof(compressionParams)); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 384 | |
| 385 | /* command switches */ |
Yann Collet | 1122349 | 2016-10-14 14:07:11 -0700 | [diff] [blame] | 386 | for (argNb=1; argNb<argCount; argNb++) { |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 387 | const char* argument = argv[argNb]; |
| 388 | if(!argument) continue; /* Protection if argument empty */ |
| 389 | |
Yann Collet | 743b33f | 2016-12-02 15:18:57 -0800 | [diff] [blame] | 390 | if (nextArgumentsAreFiles==0) { |
| 391 | /* "-" means stdin/stdout */ |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 392 | if (!strcmp(argument, "-")){ |
| 393 | if (!filenameIdx) { |
| 394 | filenameIdx=1, filenameTable[0]=stdinmark; |
| 395 | outFileName=stdoutmark; |
Sean Purcell | 042ba12 | 2017-03-23 11:13:52 -0700 | [diff] [blame] | 396 | g_displayLevel-=(g_displayLevel==2); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 397 | continue; |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 398 | } } |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 399 | |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 400 | /* Decode commands (note : aggregated commands are allowed) */ |
| 401 | if (argument[0]=='-') { |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 402 | |
Yann Collet | 743b33f | 2016-12-02 15:18:57 -0800 | [diff] [blame] | 403 | if (argument[1]=='-') { |
| 404 | /* long commands (--long-word) */ |
| 405 | if (!strcmp(argument, "--")) { nextArgumentsAreFiles=1; continue; } /* only file names allowed from now on */ |
Paul Cruz | 9437cc7 | 2017-06-15 15:07:54 -0700 | [diff] [blame^] | 406 | if (!strcmp(argument, "--list")) { operation=zom_list; continue; } |
Yann Collet | 743b33f | 2016-12-02 15:18:57 -0800 | [diff] [blame] | 407 | if (!strcmp(argument, "--compress")) { operation=zom_compress; continue; } |
| 408 | if (!strcmp(argument, "--decompress")) { operation=zom_decompress; continue; } |
| 409 | if (!strcmp(argument, "--uncompress")) { operation=zom_decompress; continue; } |
Sean Purcell | 680e4e0 | 2017-03-23 11:52:09 -0700 | [diff] [blame] | 410 | if (!strcmp(argument, "--force")) { FIO_overwriteMode(); forceStdout=1; followLinks=1; continue; } |
Sean Purcell | 042ba12 | 2017-03-23 11:13:52 -0700 | [diff] [blame] | 411 | if (!strcmp(argument, "--version")) { g_displayOut=stdout; DISPLAY(WELCOME_MESSAGE); CLEAN_RETURN(0); } |
| 412 | if (!strcmp(argument, "--help")) { g_displayOut=stdout; CLEAN_RETURN(usage_advanced(programName)); } |
| 413 | if (!strcmp(argument, "--verbose")) { g_displayLevel++; continue; } |
| 414 | if (!strcmp(argument, "--quiet")) { g_displayLevel--; continue; } |
| 415 | if (!strcmp(argument, "--stdout")) { forceStdout=1; outFileName=stdoutmark; g_displayLevel-=(g_displayLevel==2); continue; } |
Yann Collet | 743b33f | 2016-12-02 15:18:57 -0800 | [diff] [blame] | 416 | if (!strcmp(argument, "--ultra")) { ultra=1; continue; } |
| 417 | if (!strcmp(argument, "--check")) { FIO_setChecksumFlag(2); continue; } |
| 418 | if (!strcmp(argument, "--no-check")) { FIO_setChecksumFlag(0); continue; } |
| 419 | if (!strcmp(argument, "--sparse")) { FIO_setSparseWrite(2); continue; } |
| 420 | if (!strcmp(argument, "--no-sparse")) { FIO_setSparseWrite(0); continue; } |
| 421 | if (!strcmp(argument, "--test")) { operation=zom_test; continue; } |
| 422 | if (!strcmp(argument, "--train")) { operation=zom_train; outFileName=g_defaultDictName; continue; } |
Yann Collet | 96aa301 | 2017-03-24 16:04:29 -0700 | [diff] [blame] | 423 | if (!strcmp(argument, "--maxdict")) { nextArgumentIsMaxDict=1; lastCommand=1; continue; } /* kept available for compatibility with old syntax ; will be removed one day */ |
| 424 | if (!strcmp(argument, "--dictID")) { nextArgumentIsDictID=1; lastCommand=1; continue; } /* kept available for compatibility with old syntax ; will be removed one day */ |
Yann Collet | 743b33f | 2016-12-02 15:18:57 -0800 | [diff] [blame] | 425 | if (!strcmp(argument, "--no-dictID")) { FIO_setDictIDFlag(0); continue; } |
| 426 | if (!strcmp(argument, "--keep")) { FIO_setRemoveSrcFile(0); continue; } |
| 427 | if (!strcmp(argument, "--rm")) { FIO_setRemoveSrcFile(1); continue; } |
Przemyslaw Skibinski | 0665a35 | 2017-02-07 20:12:59 +0100 | [diff] [blame] | 428 | if (!strcmp(argument, "--priority=rt")) { setRealTimePrio = 1; continue; } |
Yann Collet | c1c040e | 2017-03-01 16:49:20 -0800 | [diff] [blame] | 429 | #ifdef ZSTD_GZCOMPRESS |
Przemyslaw Skibinski | 9704195 | 2017-02-14 09:47:29 +0100 | [diff] [blame] | 430 | if (!strcmp(argument, "--format=gzip")) { suffix = GZ_EXTENSION; FIO_setCompressionType(FIO_gzipCompression); continue; } |
Yann Collet | c1c040e | 2017-03-01 16:49:20 -0800 | [diff] [blame] | 431 | #endif |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 432 | #ifdef ZSTD_LZMACOMPRESS |
| 433 | if (!strcmp(argument, "--format=lzma")) { suffix = LZMA_EXTENSION; FIO_setCompressionType(FIO_lzmaCompression); continue; } |
| 434 | if (!strcmp(argument, "--format=xz")) { suffix = XZ_EXTENSION; FIO_setCompressionType(FIO_xzCompression); continue; } |
| 435 | #endif |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 436 | #ifdef ZSTD_LZ4COMPRESS |
| 437 | if (!strcmp(argument, "--format=lz4")) { suffix = LZ4_EXTENSION; FIO_setCompressionType(FIO_lz4Compression); continue; } |
| 438 | #endif |
Yann Collet | 743b33f | 2016-12-02 15:18:57 -0800 | [diff] [blame] | 439 | |
| 440 | /* long commands with arguments */ |
Yann Collet | c1c040e | 2017-03-01 16:49:20 -0800 | [diff] [blame] | 441 | #ifndef ZSTD_NODICT |
Nick Terrell | f376d47 | 2017-05-01 23:40:20 -0700 | [diff] [blame] | 442 | if (longCommandWArg(&argument, "--train-cover")) { |
| 443 | operation = zom_train; |
| 444 | outFileName = g_defaultDictName; |
| 445 | cover = 1; |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 446 | /* Allow optional arguments following an = */ |
| 447 | if (*argument == 0) { memset(&coverParams, 0, sizeof(coverParams)); } |
| 448 | else if (*argument++ != '=') { CLEAN_RETURN(badusage(programName)); } |
| 449 | else if (!parseCoverParameters(argument, &coverParams)) { CLEAN_RETURN(badusage(programName)); } |
| 450 | continue; |
| 451 | } |
Nick Terrell | f376d47 | 2017-05-01 23:40:20 -0700 | [diff] [blame] | 452 | if (longCommandWArg(&argument, "--train-legacy")) { |
| 453 | operation = zom_train; |
| 454 | outFileName = g_defaultDictName; |
| 455 | cover = 0; |
| 456 | /* Allow optional arguments following an = */ |
| 457 | if (*argument == 0) { continue; } |
| 458 | else if (*argument++ != '=') { CLEAN_RETURN(badusage(programName)); } |
| 459 | else if (!parseLegacyParameters(argument, &dictSelect)) { CLEAN_RETURN(badusage(programName)); } |
| 460 | continue; |
| 461 | } |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 462 | #endif |
Yann Collet | 230d7ac | 2017-04-21 11:38:13 -0700 | [diff] [blame] | 463 | if (longCommandWArg(&argument, "--threads=")) { nbThreads = readU32FromChar(&argument); continue; } |
Yann Collet | 743b33f | 2016-12-02 15:18:57 -0800 | [diff] [blame] | 464 | if (longCommandWArg(&argument, "--memlimit=")) { memLimit = readU32FromChar(&argument); continue; } |
| 465 | if (longCommandWArg(&argument, "--memory=")) { memLimit = readU32FromChar(&argument); continue; } |
| 466 | if (longCommandWArg(&argument, "--memlimit-decompress=")) { memLimit = readU32FromChar(&argument); continue; } |
Yann Collet | 6be2337 | 2017-01-30 11:17:26 -0800 | [diff] [blame] | 467 | if (longCommandWArg(&argument, "--block-size=")) { blockSize = readU32FromChar(&argument); continue; } |
Yann Collet | 96aa301 | 2017-03-24 16:04:29 -0700 | [diff] [blame] | 468 | if (longCommandWArg(&argument, "--maxdict=")) { maxDictSize = readU32FromChar(&argument); continue; } |
| 469 | if (longCommandWArg(&argument, "--dictID=")) { dictID = readU32FromChar(&argument); continue; } |
Przemyslaw Skibinski | 671f28d | 2016-12-13 12:18:07 +0100 | [diff] [blame] | 470 | if (longCommandWArg(&argument, "--zstd=")) { if (!parseCompressionParameters(argument, &compressionParams)) CLEAN_RETURN(badusage(programName)); continue; } |
Yann Collet | 743b33f | 2016-12-02 15:18:57 -0800 | [diff] [blame] | 471 | /* fall-through, will trigger bad_usage() later on */ |
| 472 | } |
| 473 | |
| 474 | argument++; |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 475 | while (argument[0]!=0) { |
Yann Collet | 27b5ac6 | 2016-09-21 14:20:56 +0200 | [diff] [blame] | 476 | if (lastCommand) { |
| 477 | DISPLAY("error : command must be followed by argument \n"); |
Przemyslaw Skibinski | 684858e | 2017-02-21 18:17:24 +0100 | [diff] [blame] | 478 | CLEAN_RETURN(1); |
Yann Collet | 27b5ac6 | 2016-09-21 14:20:56 +0200 | [diff] [blame] | 479 | } |
Yann Collet | c843142 | 2016-09-01 15:05:57 -0700 | [diff] [blame] | 480 | #ifndef ZSTD_NOCOMPRESS |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 481 | /* compression Level */ |
| 482 | if ((*argument>='0') && (*argument<='9')) { |
Yann Collet | fe0590f | 2016-08-12 18:04:15 +0200 | [diff] [blame] | 483 | dictCLevel = cLevel = readU32FromChar(&argument); |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 484 | continue; |
| 485 | } |
Yann Collet | c843142 | 2016-09-01 15:05:57 -0700 | [diff] [blame] | 486 | #endif |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 487 | |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 488 | switch(argument[0]) |
| 489 | { |
| 490 | /* Display help */ |
Sean Purcell | 042ba12 | 2017-03-23 11:13:52 -0700 | [diff] [blame] | 491 | case 'V': g_displayOut=stdout; DISPLAY(WELCOME_MESSAGE); CLEAN_RETURN(0); /* Version Only */ |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 492 | case 'H': |
Sean Purcell | 042ba12 | 2017-03-23 11:13:52 -0700 | [diff] [blame] | 493 | case 'h': g_displayOut=stdout; CLEAN_RETURN(usage_advanced(programName)); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 494 | |
Yann Collet | 22de81e | 2016-10-28 13:58:31 -0700 | [diff] [blame] | 495 | /* Compress */ |
| 496 | case 'z': operation=zom_compress; argument++; break; |
| 497 | |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 498 | /* Decoding */ |
Yann Collet | 2a826e5 | 2016-12-06 17:56:20 -0800 | [diff] [blame] | 499 | case 'd': |
| 500 | #ifndef ZSTD_NOBENCH |
Yann Collet | ab7a579 | 2016-12-28 16:11:09 +0100 | [diff] [blame] | 501 | if (operation==zom_bench) { BMK_setDecodeOnlyMode(1); argument++; break; } /* benchmark decode (hidden option) */ |
Yann Collet | 2a826e5 | 2016-12-06 17:56:20 -0800 | [diff] [blame] | 502 | #endif |
Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 503 | operation=zom_decompress; argument++; break; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 504 | |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 505 | /* Force stdout, even if stdout==console */ |
Yann Collet | fe0590f | 2016-08-12 18:04:15 +0200 | [diff] [blame] | 506 | case 'c': forceStdout=1; outFileName=stdoutmark; argument++; break; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 507 | |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 508 | /* Use file content as dictionary */ |
Yann Collet | 27b5ac6 | 2016-09-21 14:20:56 +0200 | [diff] [blame] | 509 | case 'D': nextEntryIsDictionary = 1; lastCommand = 1; argument++; break; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 510 | |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 511 | /* Overwrite */ |
Sean Purcell | 680e4e0 | 2017-03-23 11:52:09 -0700 | [diff] [blame] | 512 | case 'f': FIO_overwriteMode(); forceStdout=1; followLinks=1; argument++; break; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 513 | |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 514 | /* Verbose mode */ |
Sean Purcell | 042ba12 | 2017-03-23 11:13:52 -0700 | [diff] [blame] | 515 | case 'v': g_displayLevel++; argument++; break; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 516 | |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 517 | /* Quiet mode */ |
Sean Purcell | 042ba12 | 2017-03-23 11:13:52 -0700 | [diff] [blame] | 518 | case 'q': g_displayLevel--; argument++; break; |
Yann Collet | 2c4acda | 2016-06-02 17:05:50 +0200 | [diff] [blame] | 519 | |
Yann Collet | 2e63a87 | 2017-05-02 15:40:42 -0700 | [diff] [blame] | 520 | /* keep source file (default) */ |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 521 | case 'k': FIO_setRemoveSrcFile(0); argument++; break; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 522 | |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 523 | /* Checksum */ |
Yann Collet | 2e63a87 | 2017-05-02 15:40:42 -0700 | [diff] [blame] | 524 | case 'C': FIO_setChecksumFlag(2); argument++; break; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 525 | |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 526 | /* test compressed file */ |
Yann Collet | 22de81e | 2016-10-28 13:58:31 -0700 | [diff] [blame] | 527 | case 't': operation=zom_test; argument++; break; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 528 | |
Yann Collet | 2cac5b3 | 2016-07-13 14:15:08 +0200 | [diff] [blame] | 529 | /* destination file name */ |
Yann Collet | 27b5ac6 | 2016-09-21 14:20:56 +0200 | [diff] [blame] | 530 | case 'o': nextArgumentIsOutFileName=1; lastCommand=1; argument++; break; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 531 | |
Yann Collet | d4cda27 | 2016-10-14 13:13:13 -0700 | [diff] [blame] | 532 | /* limit decompression memory */ |
| 533 | case 'M': |
| 534 | argument++; |
| 535 | memLimit = readU32FromChar(&argument); |
| 536 | break; |
Paul Cruz | 901435e | 2017-06-05 14:45:31 -0700 | [diff] [blame] | 537 | case 'l': operation=zom_list; argument++; break; |
Yann Collet | c4cc9bf | 2016-09-13 17:50:08 +0200 | [diff] [blame] | 538 | #ifdef UTIL_HAS_CREATEFILELIST |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 539 | /* recursive */ |
| 540 | case 'r': recursive=1; argument++; break; |
Yann Collet | c4cc9bf | 2016-09-13 17:50:08 +0200 | [diff] [blame] | 541 | #endif |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 542 | |
Yann Collet | c843142 | 2016-09-01 15:05:57 -0700 | [diff] [blame] | 543 | #ifndef ZSTD_NOBENCH |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 544 | /* Benchmark */ |
Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 545 | case 'b': |
| 546 | operation=zom_bench; |
| 547 | argument++; |
| 548 | break; |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 549 | |
| 550 | /* range bench (benchmark only) */ |
| 551 | case 'e': |
Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 552 | /* compression Level */ |
| 553 | argument++; |
| 554 | cLevelLast = readU32FromChar(&argument); |
| 555 | break; |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 556 | |
| 557 | /* Modify Nb Iterations (benchmark only) */ |
| 558 | case 'i': |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 559 | argument++; |
Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 560 | bench_nbSeconds = readU32FromChar(&argument); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 561 | break; |
| 562 | |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 563 | /* cut input into blocks (benchmark only) */ |
| 564 | case 'B': |
| 565 | argument++; |
Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 566 | blockSize = readU32FromChar(&argument); |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 567 | break; |
Yann Collet | 7474526 | 2017-01-02 02:05:45 +0100 | [diff] [blame] | 568 | |
Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 569 | #endif /* ZSTD_NOBENCH */ |
| 570 | |
Yann Collet | 7474526 | 2017-01-02 02:05:45 +0100 | [diff] [blame] | 571 | /* nb of threads (hidden option) */ |
| 572 | case 'T': |
| 573 | argument++; |
Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 574 | nbThreads = readU32FromChar(&argument); |
Yann Collet | 7474526 | 2017-01-02 02:05:45 +0100 | [diff] [blame] | 575 | break; |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 576 | |
| 577 | /* Dictionary Selection level */ |
| 578 | case 's': |
| 579 | argument++; |
| 580 | dictSelect = readU32FromChar(&argument); |
| 581 | break; |
| 582 | |
| 583 | /* Pause at the end (-p) or set an additional param (-p#) (hidden option) */ |
| 584 | case 'p': argument++; |
Yann Collet | c843142 | 2016-09-01 15:05:57 -0700 | [diff] [blame] | 585 | #ifndef ZSTD_NOBENCH |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 586 | if ((*argument>='0') && (*argument<='9')) { |
| 587 | BMK_setAdditionalParam(readU32FromChar(&argument)); |
| 588 | } else |
Yann Collet | c843142 | 2016-09-01 15:05:57 -0700 | [diff] [blame] | 589 | #endif |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 590 | main_pause=1; |
| 591 | break; |
| 592 | /* unknown command */ |
| 593 | default : CLEAN_RETURN(badusage(programName)); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 594 | } |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 595 | } |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 596 | continue; |
| 597 | } /* if (argument[0]=='-') */ |
| 598 | |
Yann Collet | 96aa301 | 2017-03-24 16:04:29 -0700 | [diff] [blame] | 599 | if (nextArgumentIsMaxDict) { /* kept available for compatibility with old syntax ; will be removed one day */ |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 600 | nextArgumentIsMaxDict = 0; |
Yann Collet | 27b5ac6 | 2016-09-21 14:20:56 +0200 | [diff] [blame] | 601 | lastCommand = 0; |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 602 | maxDictSize = readU32FromChar(&argument); |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 603 | continue; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 604 | } |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 605 | |
Yann Collet | 96aa301 | 2017-03-24 16:04:29 -0700 | [diff] [blame] | 606 | if (nextArgumentIsDictID) { /* kept available for compatibility with old syntax ; will be removed one day */ |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 607 | nextArgumentIsDictID = 0; |
Yann Collet | 27b5ac6 | 2016-09-21 14:20:56 +0200 | [diff] [blame] | 608 | lastCommand = 0; |
Yann Collet | f9cac7a | 2016-07-04 18:16:16 +0200 | [diff] [blame] | 609 | dictID = readU32FromChar(&argument); |
| 610 | continue; |
| 611 | } |
| 612 | |
| 613 | } /* if (nextArgumentIsAFile==0) */ |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 614 | |
| 615 | if (nextEntryIsDictionary) { |
| 616 | nextEntryIsDictionary = 0; |
Yann Collet | 27b5ac6 | 2016-09-21 14:20:56 +0200 | [diff] [blame] | 617 | lastCommand = 0; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 618 | dictFileName = argument; |
| 619 | continue; |
| 620 | } |
| 621 | |
| 622 | if (nextArgumentIsOutFileName) { |
| 623 | nextArgumentIsOutFileName = 0; |
Yann Collet | 27b5ac6 | 2016-09-21 14:20:56 +0200 | [diff] [blame] | 624 | lastCommand = 0; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 625 | outFileName = argument; |
| 626 | if (!strcmp(outFileName, "-")) outFileName = stdoutmark; |
| 627 | continue; |
| 628 | } |
| 629 | |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 630 | /* add filename to list */ |
| 631 | filenameTable[filenameIdx++] = argument; |
| 632 | } |
| 633 | |
Przemyslaw Skibinski | 684858e | 2017-02-21 18:17:24 +0100 | [diff] [blame] | 634 | if (lastCommand) { DISPLAY("error : command must be followed by argument \n"); CLEAN_RETURN(1); } /* forgotten argument */ |
Yann Collet | 714464f | 2016-09-21 16:05:03 +0200 | [diff] [blame] | 635 | |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 636 | /* Welcome message (if verbose) */ |
| 637 | DISPLAYLEVEL(3, WELCOME_MESSAGE); |
Przemyslaw Skibinski | b0f3663 | 2016-12-16 15:41:18 +0100 | [diff] [blame] | 638 | #ifdef _POSIX_C_SOURCE |
| 639 | DISPLAYLEVEL(4, "_POSIX_C_SOURCE defined: %ldL\n", (long) _POSIX_C_SOURCE); |
| 640 | #endif |
| 641 | #ifdef _POSIX_VERSION |
| 642 | DISPLAYLEVEL(4, "_POSIX_VERSION defined: %ldL\n", (long) _POSIX_VERSION); |
| 643 | #endif |
| 644 | #ifdef PLATFORM_POSIX_VERSION |
| 645 | DISPLAYLEVEL(4, "PLATFORM_POSIX_VERSION defined: %ldL\n", (long) PLATFORM_POSIX_VERSION); |
| 646 | #endif |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 647 | |
Sean Purcell | afa4851 | 2017-04-13 12:28:28 -0700 | [diff] [blame] | 648 | if (nbThreads == 0) { |
| 649 | /* try to guess */ |
| 650 | nbThreads = UTIL_countPhysicalCores(); |
| 651 | DISPLAYLEVEL(3, "Note: %d physical core(s) detected\n", nbThreads); |
| 652 | } |
Sean Purcell | 680e4e0 | 2017-03-23 11:52:09 -0700 | [diff] [blame] | 653 | |
Sean Purcell | dee08ca | 2017-03-23 12:09:35 -0700 | [diff] [blame] | 654 | g_utilDisplayLevel = g_displayLevel; |
Sean Purcell | 680e4e0 | 2017-03-23 11:52:09 -0700 | [diff] [blame] | 655 | if (!followLinks) { |
| 656 | unsigned u; |
| 657 | for (u=0, fileNamesNb=0; u<filenameIdx; u++) { |
| 658 | if (UTIL_isLink(filenameTable[u])) { |
| 659 | DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", filenameTable[u]); |
| 660 | } else { |
| 661 | filenameTable[fileNamesNb++] = filenameTable[u]; |
| 662 | } |
| 663 | } |
| 664 | filenameIdx = fileNamesNb; |
| 665 | } |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 666 | #ifdef UTIL_HAS_CREATEFILELIST |
Yann Collet | 0977f7e | 2016-09-21 12:24:43 +0200 | [diff] [blame] | 667 | if (recursive) { /* at this stage, filenameTable is a list of paths, which can contain both files and directories */ |
Sean Purcell | 680e4e0 | 2017-03-23 11:52:09 -0700 | [diff] [blame] | 668 | extendedFileList = UTIL_createFileList(filenameTable, filenameIdx, &fileNamesBuf, &fileNamesNb, followLinks); |
Yann Collet | 0977f7e | 2016-09-21 12:24:43 +0200 | [diff] [blame] | 669 | if (extendedFileList) { |
Yann Collet | 415251c | 2016-08-01 14:26:49 +0200 | [diff] [blame] | 670 | unsigned u; |
Yann Collet | 0977f7e | 2016-09-21 12:24:43 +0200 | [diff] [blame] | 671 | for (u=0; u<fileNamesNb; u++) DISPLAYLEVEL(4, "%u %s\n", u, extendedFileList[u]); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 672 | free((void*)filenameTable); |
Yann Collet | 0977f7e | 2016-09-21 12:24:43 +0200 | [diff] [blame] | 673 | filenameTable = extendedFileList; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 674 | filenameIdx = fileNamesNb; |
| 675 | } |
| 676 | } |
| 677 | #endif |
Paul Cruz | 901435e | 2017-06-05 14:45:31 -0700 | [diff] [blame] | 678 | if(operation==zom_list){ |
Paul Cruz | e208992 | 2017-06-15 12:27:32 -0700 | [diff] [blame] | 679 | g_displayOut = stdout; |
Paul Cruz | 607ee9f | 2017-06-14 13:23:36 -0700 | [diff] [blame] | 680 | if(filenameIdx==0){ |
| 681 | DISPLAY("No files given\n"); |
| 682 | CLEAN_RETURN(0); |
| 683 | } |
Paul Cruz | 618a7b6 | 2017-06-12 16:53:50 -0700 | [diff] [blame] | 684 | DISPLAY("===========================================\n"); |
| 685 | DISPLAY("Printing information about compressed files\n"); |
| 686 | DISPLAY("===========================================\n"); |
Paul Cruz | 901435e | 2017-06-05 14:45:31 -0700 | [diff] [blame] | 687 | DISPLAY("Number of files listed: %d\n", filenameIdx); |
Paul Cruz | 607ee9f | 2017-06-14 13:23:36 -0700 | [diff] [blame] | 688 | { |
| 689 | unsigned u; |
| 690 | for(u=0; u<filenameIdx;u++){ |
| 691 | FIO_listFile(filenameTable[u],g_displayLevel); |
| 692 | } |
Paul Cruz | 901435e | 2017-06-05 14:45:31 -0700 | [diff] [blame] | 693 | } |
| 694 | CLEAN_RETURN(0); |
| 695 | } |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 696 | /* Check if benchmark is selected */ |
Yann Collet | 22de81e | 2016-10-28 13:58:31 -0700 | [diff] [blame] | 697 | if (operation==zom_bench) { |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 698 | #ifndef ZSTD_NOBENCH |
Sean Purcell | 042ba12 | 2017-03-23 11:13:52 -0700 | [diff] [blame] | 699 | BMK_setNotificationLevel(g_displayLevel); |
Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 700 | BMK_setBlockSize(blockSize); |
| 701 | BMK_setNbThreads(nbThreads); |
| 702 | BMK_setNbSeconds(bench_nbSeconds); |
Przemyslaw Skibinski | d05014c | 2017-02-07 16:48:01 +0100 | [diff] [blame] | 703 | BMK_benchFiles(filenameTable, filenameIdx, dictFileName, cLevel, cLevelLast, &compressionParams, setRealTimePrio); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 704 | #endif |
Yann Collet | 3dc85ba | 2017-02-02 16:19:09 -0800 | [diff] [blame] | 705 | (void)bench_nbSeconds; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 706 | goto _end; |
| 707 | } |
| 708 | |
| 709 | /* Check if dictionary builder is selected */ |
Yann Collet | 22de81e | 2016-10-28 13:58:31 -0700 | [diff] [blame] | 710 | if (operation==zom_train) { |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 711 | #ifndef ZSTD_NODICT |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 712 | if (cover) { |
Nick Terrell | f376d47 | 2017-05-01 23:40:20 -0700 | [diff] [blame] | 713 | int const optimize = !coverParams.k || !coverParams.d; |
Nick Terrell | 2fe9126 | 2017-01-27 11:56:02 -0800 | [diff] [blame] | 714 | coverParams.nbThreads = nbThreads; |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 715 | coverParams.compressionLevel = dictCLevel; |
Sean Purcell | 042ba12 | 2017-03-23 11:13:52 -0700 | [diff] [blame] | 716 | coverParams.notificationLevel = g_displayLevel; |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 717 | coverParams.dictID = dictID; |
Nick Terrell | f376d47 | 2017-05-01 23:40:20 -0700 | [diff] [blame] | 718 | operationResult = DiB_trainFromFiles(outFileName, maxDictSize, filenameTable, filenameIdx, NULL, &coverParams, optimize); |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 719 | } else { |
| 720 | ZDICT_params_t dictParams; |
| 721 | memset(&dictParams, 0, sizeof(dictParams)); |
| 722 | dictParams.compressionLevel = dictCLevel; |
| 723 | dictParams.selectivityLevel = dictSelect; |
Sean Purcell | 042ba12 | 2017-03-23 11:13:52 -0700 | [diff] [blame] | 724 | dictParams.notificationLevel = g_displayLevel; |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 725 | dictParams.dictID = dictID; |
Yann Collet | f332ece | 2017-03-23 16:24:02 -0700 | [diff] [blame] | 726 | operationResult = DiB_trainFromFiles(outFileName, maxDictSize, filenameTable, filenameIdx, &dictParams, NULL, 0); |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 727 | } |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 728 | #endif |
| 729 | goto _end; |
| 730 | } |
| 731 | |
| 732 | /* No input filename ==> use stdin and stdout */ |
Yann Collet | 87c18b2 | 2016-08-26 01:43:47 +0200 | [diff] [blame] | 733 | filenameIdx += !filenameIdx; /* filenameTable[0] is stdin by default */ |
| 734 | if (!strcmp(filenameTable[0], stdinmark) && !outFileName) outFileName = stdoutmark; /* when input is stdin, default output is stdout */ |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 735 | |
| 736 | /* Check if input/output defined as console; trigger an error in this case */ |
| 737 | if (!strcmp(filenameTable[0], stdinmark) && IS_CONSOLE(stdin) ) CLEAN_RETURN(badusage(programName)); |
Sean Purcell | b8e52d3 | 2017-03-16 16:06:03 -0700 | [diff] [blame] | 738 | if (outFileName && !strcmp(outFileName, stdoutmark) && IS_CONSOLE(stdout) && !strcmp(filenameTable[0], stdinmark) && !forceStdout && operation!=zom_decompress) |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 739 | CLEAN_RETURN(badusage(programName)); |
| 740 | |
| 741 | /* user-selected output filename, only possible with a single file */ |
| 742 | if (outFileName && strcmp(outFileName,stdoutmark) && strcmp(outFileName,nulmark) && (filenameIdx>1)) { |
| 743 | DISPLAY("Too many files (%u) on the command line. \n", filenameIdx); |
| 744 | CLEAN_RETURN(filenameIdx); |
| 745 | } |
| 746 | |
inikep | 23cec87 | 2016-10-05 11:56:22 +0200 | [diff] [blame] | 747 | #ifndef ZSTD_NOCOMPRESS |
Yann Collet | fe0590f | 2016-08-12 18:04:15 +0200 | [diff] [blame] | 748 | /* check compression level limits */ |
| 749 | { int const maxCLevel = ultra ? ZSTD_maxCLevel() : ZSTDCLI_CLEVEL_MAX; |
| 750 | if (cLevel > maxCLevel) { |
| 751 | DISPLAYLEVEL(2, "Warning : compression level higher than max, reduced to %i \n", maxCLevel); |
| 752 | cLevel = maxCLevel; |
| 753 | } } |
inikep | 23cec87 | 2016-10-05 11:56:22 +0200 | [diff] [blame] | 754 | #endif |
Yann Collet | fe0590f | 2016-08-12 18:04:15 +0200 | [diff] [blame] | 755 | |
Yann Collet | f22adae | 2017-01-19 13:46:30 -0800 | [diff] [blame] | 756 | /* No status message in pipe mode (stdin - stdout) or multi-files mode */ |
Sean Purcell | 042ba12 | 2017-03-23 11:13:52 -0700 | [diff] [blame] | 757 | if (!strcmp(filenameTable[0], stdinmark) && outFileName && !strcmp(outFileName,stdoutmark) && (g_displayLevel==2)) g_displayLevel=1; |
| 758 | if ((filenameIdx>1) & (g_displayLevel==2)) g_displayLevel=1; |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 759 | |
| 760 | /* IO Stream/File */ |
Sean Purcell | 042ba12 | 2017-03-23 11:13:52 -0700 | [diff] [blame] | 761 | FIO_setNotificationLevel(g_displayLevel); |
Yann Collet | 22de81e | 2016-10-28 13:58:31 -0700 | [diff] [blame] | 762 | if (operation==zom_compress) { |
Yann Collet | 0977f7e | 2016-09-21 12:24:43 +0200 | [diff] [blame] | 763 | #ifndef ZSTD_NOCOMPRESS |
Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 764 | FIO_setNbThreads(nbThreads); |
Yann Collet | 512cbe8 | 2017-01-24 17:02:26 -0800 | [diff] [blame] | 765 | FIO_setBlockSize((U32)blockSize); |
Yann Collet | 8d8513f | 2017-01-30 14:37:08 -0800 | [diff] [blame] | 766 | if (g_overlapLog!=OVERLAP_LOG_DEFAULT) FIO_setOverlapLog(g_overlapLog); |
Yann Collet | 993060e | 2016-09-21 16:46:08 +0200 | [diff] [blame] | 767 | if ((filenameIdx==1) && outFileName) |
Przemyslaw Skibinski | 8349d67 | 2016-12-13 13:24:59 +0100 | [diff] [blame] | 768 | operationResult = FIO_compressFilename(outFileName, filenameTable[0], dictFileName, cLevel, &compressionParams); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 769 | else |
Przemyslaw Skibinski | cb56306 | 2017-02-08 17:37:14 +0100 | [diff] [blame] | 770 | operationResult = FIO_compressMultipleFilenames(filenameTable, filenameIdx, outFileName ? outFileName : suffix, dictFileName, cLevel, &compressionParams); |
Yann Collet | 0977f7e | 2016-09-21 12:24:43 +0200 | [diff] [blame] | 771 | #else |
| 772 | DISPLAY("Compression not supported\n"); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 773 | #endif |
Yann Collet | 22de81e | 2016-10-28 13:58:31 -0700 | [diff] [blame] | 774 | } else { /* decompression or test */ |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 775 | #ifndef ZSTD_NODECOMPRESS |
Yann Collet | 22de81e | 2016-10-28 13:58:31 -0700 | [diff] [blame] | 776 | if (operation==zom_test) { outFileName=nulmark; FIO_setRemoveSrcFile(0); } /* test mode */ |
Yann Collet | d4cda27 | 2016-10-14 13:13:13 -0700 | [diff] [blame] | 777 | FIO_setMemLimit(memLimit); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 778 | if (filenameIdx==1 && outFileName) |
| 779 | operationResult = FIO_decompressFilename(outFileName, filenameTable[0], dictFileName); |
| 780 | else |
| 781 | operationResult = FIO_decompressMultipleFilenames(filenameTable, filenameIdx, outFileName ? outFileName : ZSTD_EXTENSION, dictFileName); |
| 782 | #else |
| 783 | DISPLAY("Decompression not supported\n"); |
| 784 | #endif |
| 785 | } |
| 786 | |
| 787 | _end: |
| 788 | if (main_pause) waitEnter(); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 789 | #ifdef UTIL_HAS_CREATEFILELIST |
Yann Collet | 0977f7e | 2016-09-21 12:24:43 +0200 | [diff] [blame] | 790 | if (extendedFileList) |
| 791 | UTIL_freeFileList(extendedFileList, fileNamesBuf); |
Yann Collet | fcf2087 | 2016-05-29 05:16:05 +0200 | [diff] [blame] | 792 | else |
| 793 | #endif |
| 794 | free((void*)filenameTable); |
| 795 | return operationResult; |
| 796 | } |