/* * This function is called by the options parsing code in mksquashfs.c * to parse any -X compressor option. * * This function returns: * >=0 (number of additional args parsed) on success * -1 if the option was unrecognised, or * -2 if the option was recognised, but otherwise bad in * some way (e.g. invalid parameter) * * Note: this function sets internal compressor state, but does not * pass back the results of the parsing other than success/failure. * The zstd_dump_options() function is called later to get the options in * a format suitable for writing to the filesystem. */ static int zstd_options(char *argv[], int argc) { if (strcmp(argv[0], "-Xcompression-level") == 0) { if (argc < 2) { fprintf(stderr, "zstd: -Xcompression-level missing " "compression level\n"); fprintf(stderr, "zstd: -Xcompression-level it should " "be 1 <= n <= %d\n", ZSTD_maxCLevel()); goto failed; } compression_level = atoi(argv[1]); if (compression_level < 1 || compression_level > ZSTD_maxCLevel()) { fprintf(stderr, "zstd: -Xcompression-level invalid, it " "should be 1 <= n <= %d\n", ZSTD_maxCLevel()); goto failed; } return 1; } return -1; failed: return -2; }
/* * This function is a helper specifically for the append mode of * mksquashfs. Its purpose is to set the internal compressor state * to the stored compressor options in the passed compressor options * structure. * * In effect this function sets up the compressor options * to the same state they were when the filesystem was originally * generated, this is to ensure on appending, the compressor uses * the same compression options that were used to generate the * original filesystem. * * Note, even if there are no compressor options, this function is still * called with an empty compressor structure (size == 0), to explicitly * set the default options, this is to ensure any user supplied * -X options on the appending mksquashfs command line are over-ridden. * * This function returns 0 on sucessful extraction of options, and -1 on error. */ static int zstd_extract_options(int block_size, void *buffer, int size) { struct zstd_comp_opts *comp_opts = buffer; if (size == 0) { /* Set default values */ compression_level = ZSTD_DEFAULT_COMPRESSION_LEVEL; return 0; } /* we expect a comp_opts structure of sufficient size to be present */ if (size < sizeof(*comp_opts)) goto failed; SQUASHFS_INSWAP_COMP_OPTS(comp_opts); if (comp_opts->compression_level < 1 || comp_opts->compression_level > ZSTD_maxCLevel()) { fprintf(stderr, "zstd: bad compression level in compression " "options structure\n"); goto failed; } compression_level = comp_opts->compression_level; return 0; failed: fprintf(stderr, "zstd: error reading stored compressor options from " "filesystem!\n"); return -1; }
static void zstd_display_options(void *buffer, int size) { struct zstd_comp_opts *comp_opts = buffer; /* we expect a comp_opts structure of sufficient size to be present */ if (size < sizeof(*comp_opts)) goto failed; SQUASHFS_INSWAP_COMP_OPTS(comp_opts); if (comp_opts->compression_level < 1 || comp_opts->compression_level > ZSTD_maxCLevel()) { fprintf(stderr, "zstd: bad compression level in compression " "options structure\n"); goto failed; } printf("\tcompression-level %d\n", comp_opts->compression_level); return; failed: fprintf(stderr, "zstd: error reading stored compressor options from " "filesystem!\n"); }
void registerCodecZSTD(CompressionCodecFactory & factory) { UInt8 method_code = UInt8(CompressionMethodByte::ZSTD); factory.registerCompressionCodec("ZSTD", method_code, [&](const ASTPtr & arguments) -> CompressionCodecPtr { int level = CompressionCodecZSTD::ZSTD_DEFAULT_LEVEL; if (arguments && !arguments->children.empty()) { if (arguments->children.size() > 1) throw Exception("ZSTD codec must have 1 parameter, given " + std::to_string(arguments->children.size()), ErrorCodes::ILLEGAL_SYNTAX_FOR_CODEC_TYPE); const auto children = arguments->children; const auto * literal = children[0]->as<ASTLiteral>(); level = literal->value.safeGet<UInt64>(); if (level > ZSTD_maxCLevel()) throw Exception("ZSTD codec can't have level more that " + toString(ZSTD_maxCLevel()) + ", given " + toString(level), ErrorCodes::ILLEGAL_CODEC_PARAMETER); } return std::make_shared<CompressionCodecZSTD>(level); }); }
ZSTDCodec::ZSTDCodec(int level, CodecType type) : Codec(type) { DCHECK(type == CodecType::ZSTD); switch (level) { case COMPRESSION_LEVEL_FASTEST: level = 1; break; case COMPRESSION_LEVEL_DEFAULT: level = 1; break; case COMPRESSION_LEVEL_BEST: level = 19; break; } if (level < 1 || level > ZSTD_maxCLevel()) { throw std::invalid_argument( to<std::string>("ZSTD: invalid level: ", level)); } level_ = level; }
/*-************************************ * Command Line **************************************/ static int usage(const char* programName) { DISPLAY( "Usage :\n"); DISPLAY( " %s [args] [FILE(s)] [-o file]\n", programName); DISPLAY( "\n"); DISPLAY( "FILE : a filename\n"); DISPLAY( " with no FILE, or when FILE is - , read standard input\n"); DISPLAY( "Arguments :\n"); #ifndef ZSTD_NOCOMPRESS DISPLAY( " -# : # compression level (1-%u, default:1) \n", ZSTD_maxCLevel()); #endif #ifndef ZSTD_NODECOMPRESS DISPLAY( " -d : decompression \n"); #endif DISPLAY( " -D file: use `file` as Dictionary \n"); DISPLAY( " -o file: result stored into `file` (only if 1 input file) \n"); DISPLAY( " -f : overwrite output without prompting \n"); DISPLAY( " -h/-H : display help/long help and exit\n"); return 0; }
static int LERCVSetField(TIFF* tif, uint32 tag, va_list ap) { static const char module[] = "LERCVSetField"; LERCState* sp = LState(tif); switch (tag) { case TIFFTAG_LERC_PARAMETERS: { uint32 count = va_arg(ap, int); int* params = va_arg(ap, int*); if( count < 2 ) { TIFFErrorExt(tif->tif_clientdata, module, "Invalid count for LercParameters: %u", count); return 0; } sp->lerc_version = params[0]; sp->additional_compression = params[1]; return LERCVSetFieldBase(tif, TIFFTAG_LERC_PARAMETERS, count, params); } case TIFFTAG_LERC_MAXZERROR: sp->maxzerror = va_arg(ap, double); return 1; case TIFFTAG_LERC_VERSION: { int params[2] = {0, 0}; int version = va_arg(ap, int); if( version != LERC_VERSION_2_4 ) { TIFFErrorExt(tif->tif_clientdata, module, "Invalid value for LercVersion: %d", version); return 0; } sp->lerc_version = version; params[0] = sp->lerc_version; params[1] = sp->additional_compression; return LERCVSetFieldBase(tif, TIFFTAG_LERC_PARAMETERS, 2, params); } case TIFFTAG_LERC_ADD_COMPRESSION: { int params[2] = {0, 0}; int additional_compression = va_arg(ap, int); #ifndef ZSTD_SUPPORT if( additional_compression == LERC_ADD_COMPRESSION_ZSTD ) { TIFFErrorExt(tif->tif_clientdata, module, "LERC_ZSTD requested, but ZSTD not available"); return 0; } #endif if( additional_compression != LERC_ADD_COMPRESSION_NONE && additional_compression != LERC_ADD_COMPRESSION_DEFLATE && additional_compression != LERC_ADD_COMPRESSION_ZSTD ) { TIFFErrorExt(tif->tif_clientdata, module, "Invalid value for LercAdditionalCompression: %d", additional_compression); return 0; } sp->additional_compression = additional_compression; params[0] = sp->lerc_version; params[1] = sp->additional_compression; return LERCVSetFieldBase(tif, TIFFTAG_LERC_PARAMETERS, 2, params); } #ifdef ZSTD_SUPPORT case TIFFTAG_ZSTD_LEVEL: { sp->zstd_compress_level = (int) va_arg(ap, int); if( sp->zstd_compress_level <= 0 || sp->zstd_compress_level > ZSTD_maxCLevel() ) { TIFFWarningExt(tif->tif_clientdata, module, "ZSTD_LEVEL should be between 1 and %d", ZSTD_maxCLevel()); } return 1; } #endif case TIFFTAG_ZIPQUALITY: { sp->zipquality = (int) va_arg(ap, int); return (1); } default: return (*sp->vsetparent)(tif, tag, ap); } /*NOTREACHED*/ }
int main(int argCount, const char* argv[]) { int argNb, forceStdout=0, followLinks=0, main_pause=0, nextEntryIsDictionary=0, operationResult=0, nextArgumentIsOutFileName=0, nextArgumentIsMaxDict=0, nextArgumentIsDictID=0, nextArgumentsAreFiles=0, ultra=0, lastCommand = 0, nbThreads = 1, setRealTimePrio = 0, separateFiles = 0, ldmFlag = 0; unsigned bench_nbSeconds = 3; /* would be better if this value was synchronized from bench */ size_t blockSize = 0; zstd_operation_mode operation = zom_compress; ZSTD_compressionParameters compressionParams; int cLevel = ZSTDCLI_CLEVEL_DEFAULT; int cLevelLast = 1; unsigned recursive = 0; unsigned memLimit = 0; const char** filenameTable = (const char**)malloc(argCount * sizeof(const char*)); /* argCount >= 1 */ unsigned filenameIdx = 0; const char* programName = argv[0]; const char* outFileName = NULL; const char* dictFileName = NULL; const char* suffix = ZSTD_EXTENSION; unsigned maxDictSize = g_defaultMaxDictSize; unsigned dictID = 0; int dictCLevel = g_defaultDictCLevel; unsigned dictSelect = g_defaultSelectivityLevel; #ifdef UTIL_HAS_CREATEFILELIST const char** extendedFileList = NULL; char* fileNamesBuf = NULL; unsigned fileNamesNb; #endif #ifndef ZSTD_NODICT ZDICT_cover_params_t coverParams = defaultCoverParams(); int cover = 1; #endif /* init */ (void)recursive; (void)cLevelLast; /* not used when ZSTD_NOBENCH set */ (void)dictCLevel; (void)dictSelect; (void)dictID; (void)maxDictSize; /* not used when ZSTD_NODICT set */ (void)ultra; (void)cLevel; (void)ldmFlag; /* not used when ZSTD_NOCOMPRESS set */ (void)memLimit; /* not used when ZSTD_NODECOMPRESS set */ if (filenameTable==NULL) { DISPLAY("zstd: %s \n", strerror(errno)); exit(1); } filenameTable[0] = stdinmark; g_displayOut = stderr; programName = lastNameFromPath(programName); /* preset behaviors */ if (exeNameMatch(programName, ZSTD_ZSTDMT)) nbThreads=0; if (exeNameMatch(programName, ZSTD_UNZSTD)) operation=zom_decompress; if (exeNameMatch(programName, ZSTD_CAT)) { operation=zom_decompress; forceStdout=1; FIO_overwriteMode(); outFileName=stdoutmark; g_displayLevel=1; } if (exeNameMatch(programName, ZSTD_GZ)) { suffix = GZ_EXTENSION; FIO_setCompressionType(FIO_gzipCompression); FIO_setRemoveSrcFile(1); } /* behave like gzip */ if (exeNameMatch(programName, ZSTD_GUNZIP)) { operation=zom_decompress; FIO_setRemoveSrcFile(1); } /* behave like gunzip */ if (exeNameMatch(programName, ZSTD_GZCAT)) { operation=zom_decompress; forceStdout=1; FIO_overwriteMode(); outFileName=stdoutmark; g_displayLevel=1; } /* behave like gzcat */ if (exeNameMatch(programName, ZSTD_LZMA)) { suffix = LZMA_EXTENSION; FIO_setCompressionType(FIO_lzmaCompression); FIO_setRemoveSrcFile(1); } /* behave like lzma */ if (exeNameMatch(programName, ZSTD_UNLZMA)) { operation=zom_decompress; FIO_setCompressionType(FIO_lzmaCompression); FIO_setRemoveSrcFile(1); } /* behave like unlzma */ if (exeNameMatch(programName, ZSTD_XZ)) { suffix = XZ_EXTENSION; FIO_setCompressionType(FIO_xzCompression); FIO_setRemoveSrcFile(1); } /* behave like xz */ if (exeNameMatch(programName, ZSTD_UNXZ)) { operation=zom_decompress; FIO_setCompressionType(FIO_xzCompression); FIO_setRemoveSrcFile(1); } /* behave like unxz */ if (exeNameMatch(programName, ZSTD_LZ4)) { suffix = LZ4_EXTENSION; FIO_setCompressionType(FIO_lz4Compression); FIO_setRemoveSrcFile(1); } /* behave like xz */ if (exeNameMatch(programName, ZSTD_UNLZ4)) { operation=zom_decompress; FIO_setCompressionType(FIO_lz4Compression); FIO_setRemoveSrcFile(1); } /* behave like unxz */ memset(&compressionParams, 0, sizeof(compressionParams)); /* command switches */ for (argNb=1; argNb<argCount; argNb++) { const char* argument = argv[argNb]; if(!argument) continue; /* Protection if argument empty */ if (nextArgumentsAreFiles==0) { /* "-" means stdin/stdout */ if (!strcmp(argument, "-")){ if (!filenameIdx) { filenameIdx=1, filenameTable[0]=stdinmark; outFileName=stdoutmark; g_displayLevel-=(g_displayLevel==2); continue; } } /* Decode commands (note : aggregated commands are allowed) */ if (argument[0]=='-') { if (argument[1]=='-') { /* long commands (--long-word) */ if (!strcmp(argument, "--")) { nextArgumentsAreFiles=1; continue; } /* only file names allowed from now on */ if (!strcmp(argument, "--list")) { operation=zom_list; continue; } if (!strcmp(argument, "--compress")) { operation=zom_compress; continue; } if (!strcmp(argument, "--decompress")) { operation=zom_decompress; continue; } if (!strcmp(argument, "--uncompress")) { operation=zom_decompress; continue; } if (!strcmp(argument, "--force")) { FIO_overwriteMode(); forceStdout=1; followLinks=1; continue; } if (!strcmp(argument, "--version")) { g_displayOut=stdout; DISPLAY(WELCOME_MESSAGE); CLEAN_RETURN(0); } if (!strcmp(argument, "--help")) { g_displayOut=stdout; CLEAN_RETURN(usage_advanced(programName)); } if (!strcmp(argument, "--verbose")) { g_displayLevel++; continue; } if (!strcmp(argument, "--quiet")) { g_displayLevel--; continue; } if (!strcmp(argument, "--stdout")) { forceStdout=1; outFileName=stdoutmark; g_displayLevel-=(g_displayLevel==2); continue; } if (!strcmp(argument, "--ultra")) { ultra=1; continue; } if (!strcmp(argument, "--check")) { FIO_setChecksumFlag(2); continue; } if (!strcmp(argument, "--no-check")) { FIO_setChecksumFlag(0); continue; } if (!strcmp(argument, "--sparse")) { FIO_setSparseWrite(2); continue; } if (!strcmp(argument, "--no-sparse")) { FIO_setSparseWrite(0); continue; } if (!strcmp(argument, "--test")) { operation=zom_test; continue; } if (!strcmp(argument, "--train")) { operation=zom_train; outFileName=g_defaultDictName; continue; } if (!strcmp(argument, "--maxdict")) { nextArgumentIsMaxDict=1; lastCommand=1; continue; } /* kept available for compatibility with old syntax ; will be removed one day */ if (!strcmp(argument, "--dictID")) { nextArgumentIsDictID=1; lastCommand=1; continue; } /* kept available for compatibility with old syntax ; will be removed one day */ if (!strcmp(argument, "--no-dictID")) { FIO_setDictIDFlag(0); continue; } if (!strcmp(argument, "--keep")) { FIO_setRemoveSrcFile(0); continue; } if (!strcmp(argument, "--rm")) { FIO_setRemoveSrcFile(1); continue; } if (!strcmp(argument, "--priority=rt")) { setRealTimePrio = 1; continue; } #ifdef ZSTD_GZCOMPRESS if (!strcmp(argument, "--format=gzip")) { suffix = GZ_EXTENSION; FIO_setCompressionType(FIO_gzipCompression); continue; } #endif #ifdef ZSTD_LZMACOMPRESS if (!strcmp(argument, "--format=lzma")) { suffix = LZMA_EXTENSION; FIO_setCompressionType(FIO_lzmaCompression); continue; } if (!strcmp(argument, "--format=xz")) { suffix = XZ_EXTENSION; FIO_setCompressionType(FIO_xzCompression); continue; } #endif #ifdef ZSTD_LZ4COMPRESS if (!strcmp(argument, "--format=lz4")) { suffix = LZ4_EXTENSION; FIO_setCompressionType(FIO_lz4Compression); continue; } #endif /* long commands with arguments */ #ifndef ZSTD_NODICT if (longCommandWArg(&argument, "--train-cover")) { operation = zom_train; outFileName = g_defaultDictName; cover = 1; /* Allow optional arguments following an = */ if (*argument == 0) { memset(&coverParams, 0, sizeof(coverParams)); } else if (*argument++ != '=') { CLEAN_RETURN(badusage(programName)); } else if (!parseCoverParameters(argument, &coverParams)) { CLEAN_RETURN(badusage(programName)); } continue; } if (longCommandWArg(&argument, "--train-legacy")) { operation = zom_train; outFileName = g_defaultDictName; cover = 0; /* Allow optional arguments following an = */ if (*argument == 0) { continue; } else if (*argument++ != '=') { CLEAN_RETURN(badusage(programName)); } else if (!parseLegacyParameters(argument, &dictSelect)) { CLEAN_RETURN(badusage(programName)); } continue; } #endif if (longCommandWArg(&argument, "--threads=")) { nbThreads = readU32FromChar(&argument); continue; } if (longCommandWArg(&argument, "--memlimit=")) { memLimit = readU32FromChar(&argument); continue; } if (longCommandWArg(&argument, "--memory=")) { memLimit = readU32FromChar(&argument); continue; } if (longCommandWArg(&argument, "--memlimit-decompress=")) { memLimit = readU32FromChar(&argument); continue; } if (longCommandWArg(&argument, "--block-size=")) { blockSize = readU32FromChar(&argument); continue; } if (longCommandWArg(&argument, "--maxdict=")) { maxDictSize = readU32FromChar(&argument); continue; } if (longCommandWArg(&argument, "--dictID=")) { dictID = readU32FromChar(&argument); continue; } if (longCommandWArg(&argument, "--zstd=")) { if (!parseCompressionParameters(argument, &compressionParams)) CLEAN_RETURN(badusage(programName)); continue; } if (longCommandWArg(&argument, "--long")) { unsigned ldmWindowLog = 0; ldmFlag = 1; /* Parse optional window log */ if (*argument == '=') { ++argument; ldmWindowLog = readU32FromChar(&argument); } else if (*argument != 0) { /* Invalid character following --long */ CLEAN_RETURN(badusage(programName)); } /* Only set windowLog if not already set by --zstd */ if (compressionParams.windowLog == 0) compressionParams.windowLog = ldmWindowLog; continue; } /* fall-through, will trigger bad_usage() later on */ } argument++; while (argument[0]!=0) { if (lastCommand) { DISPLAY("error : command must be followed by argument \n"); CLEAN_RETURN(1); } #ifndef ZSTD_NOCOMPRESS /* compression Level */ if ((*argument>='0') && (*argument<='9')) { dictCLevel = cLevel = readU32FromChar(&argument); continue; } #endif switch(argument[0]) { /* Display help */ case 'V': g_displayOut=stdout; printVersion(); CLEAN_RETURN(0); /* Version Only */ case 'H': case 'h': g_displayOut=stdout; CLEAN_RETURN(usage_advanced(programName)); /* Compress */ case 'z': operation=zom_compress; argument++; break; /* Decoding */ case 'd': #ifndef ZSTD_NOBENCH if (operation==zom_bench) { BMK_setDecodeOnlyMode(1); argument++; break; } /* benchmark decode (hidden option) */ #endif operation=zom_decompress; argument++; break; /* Force stdout, even if stdout==console */ case 'c': forceStdout=1; outFileName=stdoutmark; argument++; break; /* Use file content as dictionary */ case 'D': nextEntryIsDictionary = 1; lastCommand = 1; argument++; break; /* Overwrite */ case 'f': FIO_overwriteMode(); forceStdout=1; followLinks=1; argument++; break; /* Verbose mode */ case 'v': g_displayLevel++; argument++; break; /* Quiet mode */ case 'q': g_displayLevel--; argument++; break; /* keep source file (default) */ case 'k': FIO_setRemoveSrcFile(0); argument++; break; /* Checksum */ case 'C': FIO_setChecksumFlag(2); argument++; break; /* test compressed file */ case 't': operation=zom_test; argument++; break; /* destination file name */ case 'o': nextArgumentIsOutFileName=1; lastCommand=1; argument++; break; /* limit decompression memory */ case 'M': argument++; memLimit = readU32FromChar(&argument); break; case 'l': operation=zom_list; argument++; break; #ifdef UTIL_HAS_CREATEFILELIST /* recursive */ case 'r': recursive=1; argument++; break; #endif #ifndef ZSTD_NOBENCH /* Benchmark */ case 'b': operation=zom_bench; argument++; break; /* range bench (benchmark only) */ case 'e': /* compression Level */ argument++; cLevelLast = readU32FromChar(&argument); break; /* Modify Nb Iterations (benchmark only) */ case 'i': argument++; bench_nbSeconds = readU32FromChar(&argument); break; /* cut input into blocks (benchmark only) */ case 'B': argument++; blockSize = readU32FromChar(&argument); break; /* benchmark files separately (hidden option) */ case 'S': argument++; separateFiles = 1; break; #endif /* ZSTD_NOBENCH */ /* nb of threads (hidden option) */ case 'T': argument++; nbThreads = readU32FromChar(&argument); break; /* Dictionary Selection level */ case 's': argument++; dictSelect = readU32FromChar(&argument); break; /* Pause at the end (-p) or set an additional param (-p#) (hidden option) */ case 'p': argument++; #ifndef ZSTD_NOBENCH if ((*argument>='0') && (*argument<='9')) { BMK_setAdditionalParam(readU32FromChar(&argument)); } else #endif main_pause=1; break; /* unknown command */ default : CLEAN_RETURN(badusage(programName)); } } continue; } /* if (argument[0]=='-') */ if (nextArgumentIsMaxDict) { /* kept available for compatibility with old syntax ; will be removed one day */ nextArgumentIsMaxDict = 0; lastCommand = 0; maxDictSize = readU32FromChar(&argument); continue; } if (nextArgumentIsDictID) { /* kept available for compatibility with old syntax ; will be removed one day */ nextArgumentIsDictID = 0; lastCommand = 0; dictID = readU32FromChar(&argument); continue; } } /* if (nextArgumentIsAFile==0) */ if (nextEntryIsDictionary) { nextEntryIsDictionary = 0; lastCommand = 0; dictFileName = argument; continue; } if (nextArgumentIsOutFileName) { nextArgumentIsOutFileName = 0; lastCommand = 0; outFileName = argument; if (!strcmp(outFileName, "-")) outFileName = stdoutmark; continue; } /* add filename to list */ filenameTable[filenameIdx++] = argument; } if (lastCommand) { /* forgotten argument */ DISPLAY("error : command must be followed by argument \n"); CLEAN_RETURN(1); } /* Welcome message (if verbose) */ DISPLAYLEVEL(3, WELCOME_MESSAGE); if (nbThreads == 0) { /* try to guess */ nbThreads = UTIL_countPhysicalCores(); DISPLAYLEVEL(3, "Note: %d physical core(s) detected \n", nbThreads); } g_utilDisplayLevel = g_displayLevel; if (!followLinks) { unsigned u; for (u=0, fileNamesNb=0; u<filenameIdx; u++) { if (UTIL_isLink(filenameTable[u])) { DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", filenameTable[u]); } else { filenameTable[fileNamesNb++] = filenameTable[u]; } } filenameIdx = fileNamesNb; } #ifdef UTIL_HAS_CREATEFILELIST if (recursive) { /* at this stage, filenameTable is a list of paths, which can contain both files and directories */ extendedFileList = UTIL_createFileList(filenameTable, filenameIdx, &fileNamesBuf, &fileNamesNb, followLinks); if (extendedFileList) { unsigned u; for (u=0; u<fileNamesNb; u++) DISPLAYLEVEL(4, "%u %s\n", u, extendedFileList[u]); free((void*)filenameTable); filenameTable = extendedFileList; filenameIdx = fileNamesNb; } } #endif if (operation == zom_list) { #ifndef ZSTD_NODECOMPRESS int const ret = FIO_listMultipleFiles(filenameIdx, filenameTable, g_displayLevel); CLEAN_RETURN(ret); #else DISPLAY("file information is not supported \n"); CLEAN_RETURN(1); #endif } /* Check if benchmark is selected */ if (operation==zom_bench) { #ifndef ZSTD_NOBENCH BMK_setNotificationLevel(g_displayLevel); BMK_setSeparateFiles(separateFiles); BMK_setBlockSize(blockSize); BMK_setNbThreads(nbThreads); BMK_setRealTime(setRealTimePrio); BMK_setNbSeconds(bench_nbSeconds); BMK_setLdmFlag(ldmFlag); BMK_setLdmMinMatch(g_ldmMinMatch); BMK_setLdmHashLog(g_ldmHashLog); if (g_ldmBucketSizeLog != LDM_PARAM_DEFAULT) { BMK_setLdmBucketSizeLog(g_ldmBucketSizeLog); } if (g_ldmHashEveryLog != LDM_PARAM_DEFAULT) { BMK_setLdmHashEveryLog(g_ldmHashEveryLog); } BMK_benchFiles(filenameTable, filenameIdx, dictFileName, cLevel, cLevelLast, &compressionParams); #else (void)bench_nbSeconds; (void)blockSize; (void)setRealTimePrio; (void)separateFiles; #endif goto _end; } /* Check if dictionary builder is selected */ if (operation==zom_train) { #ifndef ZSTD_NODICT ZDICT_params_t zParams; zParams.compressionLevel = dictCLevel; zParams.notificationLevel = g_displayLevel; zParams.dictID = dictID; if (cover) { int const optimize = !coverParams.k || !coverParams.d; coverParams.nbThreads = nbThreads; coverParams.zParams = zParams; operationResult = DiB_trainFromFiles(outFileName, maxDictSize, filenameTable, filenameIdx, blockSize, NULL, &coverParams, optimize); } else { ZDICT_legacy_params_t dictParams; memset(&dictParams, 0, sizeof(dictParams)); dictParams.selectivityLevel = dictSelect; dictParams.zParams = zParams; operationResult = DiB_trainFromFiles(outFileName, maxDictSize, filenameTable, filenameIdx, blockSize, &dictParams, NULL, 0); } #endif goto _end; } #ifndef ZSTD_NODECOMPRESS if (operation==zom_test) { outFileName=nulmark; FIO_setRemoveSrcFile(0); } /* test mode */ #endif /* No input filename ==> use stdin and stdout */ filenameIdx += !filenameIdx; /* filenameTable[0] is stdin by default */ if (!strcmp(filenameTable[0], stdinmark) && !outFileName) outFileName = stdoutmark; /* when input is stdin, default output is stdout */ /* Check if input/output defined as console; trigger an error in this case */ if (!strcmp(filenameTable[0], stdinmark) && IS_CONSOLE(stdin) ) CLEAN_RETURN(badusage(programName)); if (outFileName && !strcmp(outFileName, stdoutmark) && IS_CONSOLE(stdout) && !strcmp(filenameTable[0], stdinmark) && !forceStdout && operation!=zom_decompress) CLEAN_RETURN(badusage(programName)); #ifndef ZSTD_NOCOMPRESS /* check compression level limits */ { int const maxCLevel = ultra ? ZSTD_maxCLevel() : ZSTDCLI_CLEVEL_MAX; if (cLevel > maxCLevel) { DISPLAYLEVEL(2, "Warning : compression level higher than max, reduced to %i \n", maxCLevel); cLevel = maxCLevel; } } #endif /* No status message in pipe mode (stdin - stdout) or multi-files mode */ if (!strcmp(filenameTable[0], stdinmark) && outFileName && !strcmp(outFileName,stdoutmark) && (g_displayLevel==2)) g_displayLevel=1; if ((filenameIdx>1) & (g_displayLevel==2)) g_displayLevel=1; /* IO Stream/File */ FIO_setNotificationLevel(g_displayLevel); if (operation==zom_compress) { #ifndef ZSTD_NOCOMPRESS FIO_setNbThreads(nbThreads); FIO_setBlockSize((U32)blockSize); FIO_setLdmFlag(ldmFlag); FIO_setLdmHashLog(g_ldmHashLog); FIO_setLdmMinMatch(g_ldmMinMatch); if (g_ldmBucketSizeLog != LDM_PARAM_DEFAULT) { FIO_setLdmBucketSizeLog(g_ldmBucketSizeLog); } if (g_ldmHashEveryLog != LDM_PARAM_DEFAULT) { FIO_setLdmHashEveryLog(g_ldmHashEveryLog); } if (g_overlapLog!=OVERLAP_LOG_DEFAULT) FIO_setOverlapLog(g_overlapLog); if ((filenameIdx==1) && outFileName) operationResult = FIO_compressFilename(outFileName, filenameTable[0], dictFileName, cLevel, &compressionParams); else operationResult = FIO_compressMultipleFilenames(filenameTable, filenameIdx, outFileName, suffix, dictFileName, cLevel, &compressionParams); #else (void)suffix; DISPLAY("Compression not supported\n"); #endif } else { /* decompression or test */ #ifndef ZSTD_NODECOMPRESS if (memLimit == 0) { if (compressionParams.windowLog == 0) memLimit = (U32)1 << g_defaultMaxWindowLog; else { memLimit = (U32)1 << (compressionParams.windowLog & 31); } } FIO_setMemLimit(memLimit); if (filenameIdx==1 && outFileName) operationResult = FIO_decompressFilename(outFileName, filenameTable[0], dictFileName); else operationResult = FIO_decompressMultipleFilenames(filenameTable, filenameIdx, outFileName, dictFileName); #else DISPLAY("Decompression not supported\n"); #endif } _end: if (main_pause) waitEnter(); #ifdef UTIL_HAS_CREATEFILELIST if (extendedFileList) UTIL_freeFileList(extendedFileList, fileNamesBuf); else #endif free((void*)filenameTable); return operationResult; }
static int usage_advanced(const char* programName) { DISPLAY(WELCOME_MESSAGE); usage(programName); DISPLAY( "\n"); DISPLAY( "Advanced arguments : \n"); DISPLAY( " -V : display Version number and exit \n"); DISPLAY( " -v : verbose mode; specify multiple times to increase verbosity\n"); DISPLAY( " -q : suppress warnings; specify twice to suppress errors too\n"); DISPLAY( " -c : force write to standard output, even if it is the console\n"); DISPLAY( " -l : print information about zstd compressed files \n"); #ifndef ZSTD_NOCOMPRESS DISPLAY( "--ultra : enable levels beyond %i, up to %i (requires more memory)\n", ZSTDCLI_CLEVEL_MAX, ZSTD_maxCLevel()); DISPLAY( "--long[=#] : enable long distance matching with given window log (default : %u)\n", g_defaultMaxWindowLog); #ifdef ZSTD_MULTITHREAD DISPLAY( " -T# : use # threads for compression (default:1) \n"); DISPLAY( " -B# : select size of each job (default:0==automatic) \n"); #endif DISPLAY( "--no-dictID : don't write dictID into header (dictionary compression)\n"); DISPLAY( "--[no-]check : integrity check (default:enabled) \n"); #endif #ifdef UTIL_HAS_CREATEFILELIST DISPLAY( " -r : operate recursively on directories \n"); #endif #ifdef ZSTD_GZCOMPRESS DISPLAY( "--format=gzip : compress files to the .gz format \n"); #endif #ifdef ZSTD_LZMACOMPRESS DISPLAY( "--format=xz : compress files to the .xz format \n"); DISPLAY( "--format=lzma : compress files to the .lzma format \n"); #endif #ifdef ZSTD_LZ4COMPRESS DISPLAY( "--format=lz4 : compress files to the .lz4 format \n"); #endif #ifndef ZSTD_NODECOMPRESS DISPLAY( "--test : test compressed file integrity \n"); #if ZSTD_SPARSE_DEFAULT DISPLAY( "--[no-]sparse : sparse mode (default:enabled on file, disabled on stdout)\n"); #else DISPLAY( "--[no-]sparse : sparse mode (default:disabled)\n"); #endif #endif DISPLAY( " -M# : Set a memory usage limit for decompression \n"); DISPLAY( "-- : All arguments after \"--\" are treated as files \n"); #ifndef ZSTD_NODICT DISPLAY( "\n"); DISPLAY( "Dictionary builder : \n"); DISPLAY( "--train ## : create a dictionary from a training set of files \n"); DISPLAY( "--train-cover[=k=#,d=#,steps=#] : use the cover algorithm with optional args\n"); DISPLAY( "--train-legacy[=s=#] : use the legacy algorithm with selectivity (default: %u)\n", g_defaultSelectivityLevel); DISPLAY( " -o file : `file` is dictionary name (default: %s) \n", g_defaultDictName); DISPLAY( "--maxdict=# : limit dictionary to specified size (default : %u) \n", g_defaultMaxDictSize); DISPLAY( "--dictID=# : force dictionary ID to specified value (default: random)\n"); #endif #ifndef ZSTD_NOBENCH DISPLAY( "\n"); DISPLAY( "Benchmark arguments : \n"); DISPLAY( " -b# : benchmark file(s), using # compression level (default : 1) \n"); DISPLAY( " -e# : test all compression levels from -bX to # (default: 1)\n"); DISPLAY( " -i# : minimum evaluation time in seconds (default : 3s) \n"); DISPLAY( " -B# : cut file into independent blocks of size # (default: no block)\n"); DISPLAY( "--priority=rt : set process priority to real-time \n"); #endif return 0; }
int main(int argCount, const char** argv) { int argNb, bench=0, decode=0, testmode=0, forceStdout=0, main_pause=0, nextEntryIsDictionary=0, operationResult=0, dictBuild=0, nextArgumentIsOutFileName=0, nextArgumentIsMaxDict=0, nextArgumentIsDictID=0, nextArgumentIsFile=0; int cLevel = ZSTDCLI_CLEVEL_DEFAULT; int cLevelLast = 1; unsigned recursive = 0; const char** filenameTable = (const char**)malloc(argCount * sizeof(const char*)); /* argCount >= 1 */ unsigned filenameIdx = 0; const char* programName = argv[0]; const char* outFileName = NULL; const char* dictFileName = NULL; char* dynNameSpace = NULL; unsigned maxDictSize = g_defaultMaxDictSize; unsigned dictID = 0; int dictCLevel = g_defaultDictCLevel; unsigned dictSelect = g_defaultSelectivityLevel; #ifdef UTIL_HAS_CREATEFILELIST const char** fileNamesTable = NULL; char* fileNamesBuf = NULL; unsigned fileNamesNb; #endif /* init */ (void)recursive; (void)cLevelLast; /* not used when ZSTD_NOBENCH set */ (void)dictCLevel; (void)dictSelect; (void)dictID; /* not used when ZSTD_NODICT set */ (void)decode; (void)cLevel; /* not used when ZSTD_NOCOMPRESS set */ if (filenameTable==NULL) { DISPLAY("zstd: %s \n", strerror(errno)); exit(1); } filenameTable[0] = stdinmark; displayOut = stderr; /* Pick out program name from path. Don't rely on stdlib because of conflicting behavior */ { size_t pos; for (pos = (int)strlen(programName); pos > 0; pos--) { if (programName[pos] == '/') { pos++; break; } } programName += pos; } /* preset behaviors */ if (!strcmp(programName, ZSTD_UNZSTD)) decode=1; if (!strcmp(programName, ZSTD_CAT)) { decode=1; forceStdout=1; displayLevel=1; outFileName=stdoutmark; } /* command switches */ for(argNb=1; argNb<argCount; argNb++) { const char* argument = argv[argNb]; if(!argument) continue; /* Protection if argument empty */ if (nextArgumentIsFile==0) { /* long commands (--long-word) */ if (!strcmp(argument, "--")) { nextArgumentIsFile=1; continue; } if (!strcmp(argument, "--decompress")) { decode=1; continue; } if (!strcmp(argument, "--force")) { FIO_overwriteMode(); continue; } if (!strcmp(argument, "--version")) { displayOut=stdout; DISPLAY(WELCOME_MESSAGE); CLEAN_RETURN(0); } if (!strcmp(argument, "--help")) { displayOut=stdout; CLEAN_RETURN(usage_advanced(programName)); } if (!strcmp(argument, "--verbose")) { displayLevel++; continue; } if (!strcmp(argument, "--quiet")) { displayLevel--; continue; } if (!strcmp(argument, "--stdout")) { forceStdout=1; outFileName=stdoutmark; displayLevel-=(displayLevel==2); continue; } if (!strcmp(argument, "--ultra")) { FIO_setMaxWLog(0); continue; } if (!strcmp(argument, "--check")) { FIO_setChecksumFlag(2); continue; } if (!strcmp(argument, "--no-check")) { FIO_setChecksumFlag(0); continue; } if (!strcmp(argument, "--no-dictID")) { FIO_setDictIDFlag(0); continue; } if (!strcmp(argument, "--sparse")) { FIO_setSparseWrite(2); continue; } if (!strcmp(argument, "--no-sparse")) { FIO_setSparseWrite(0); continue; } if (!strcmp(argument, "--test")) { testmode=1; decode=1; continue; } if (!strcmp(argument, "--train")) { dictBuild=1; outFileName=g_defaultDictName; continue; } if (!strcmp(argument, "--maxdict")) { nextArgumentIsMaxDict=1; continue; } if (!strcmp(argument, "--dictID")) { nextArgumentIsDictID=1; continue; } if (!strcmp(argument, "--keep")) { FIO_setRemoveSrcFile(0); continue; } if (!strcmp(argument, "--rm")) { FIO_setRemoveSrcFile(1); continue; } /* '-' means stdin/stdout */ if (!strcmp(argument, "-")){ if (!filenameIdx) { filenameIdx=1, filenameTable[0]=stdinmark; outFileName=stdoutmark; displayLevel-=(displayLevel==2); continue; } } /* Decode commands (note : aggregated commands are allowed) */ if (argument[0]=='-') { argument++; while (argument[0]!=0) { #ifndef ZSTD_NOCOMPRESS /* compression Level */ if ((*argument>='0') && (*argument<='9')) { cLevel = readU32FromChar(&argument); dictCLevel = cLevel; if (dictCLevel > ZSTD_maxCLevel()) CLEAN_RETURN(badusage(programName)); continue; } #endif switch(argument[0]) { /* Display help */ case 'V': displayOut=stdout; DISPLAY(WELCOME_MESSAGE); CLEAN_RETURN(0); /* Version Only */ case 'H': case 'h': displayOut=stdout; CLEAN_RETURN(usage_advanced(programName)); /* Decoding */ case 'd': decode=1; argument++; break; /* Force stdout, even if stdout==console */ case 'c': forceStdout=1; outFileName=stdoutmark; displayLevel-=(displayLevel==2); argument++; break; /* Use file content as dictionary */ case 'D': nextEntryIsDictionary = 1; argument++; break; /* Overwrite */ case 'f': FIO_overwriteMode(); forceStdout=1; argument++; break; /* Verbose mode */ case 'v': displayLevel++; argument++; break; /* Quiet mode */ case 'q': displayLevel--; argument++; break; /* keep source file (default); for gzip/xz compatibility */ case 'k': FIO_setRemoveSrcFile(0); argument++; break; /* Checksum */ case 'C': argument++; FIO_setChecksumFlag(2); break; /* test compressed file */ case 't': testmode=1; decode=1; argument++; break; /* destination file name */ case 'o': nextArgumentIsOutFileName=1; argument++; break; /* recursive */ case 'r': recursive=1; argument++; break; #ifndef ZSTD_NOBENCH /* Benchmark */ case 'b': bench=1; argument++; break; /* range bench (benchmark only) */ case 'e': /* compression Level */ argument++; cLevelLast = readU32FromChar(&argument); break; /* Modify Nb Iterations (benchmark only) */ case 'i': argument++; { U32 const iters = readU32FromChar(&argument); BMK_setNotificationLevel(displayLevel); BMK_SetNbIterations(iters); } break; /* cut input into blocks (benchmark only) */ case 'B': argument++; { size_t bSize = readU32FromChar(&argument); if (toupper(*argument)=='K') bSize<<=10, argument++; /* allows using KB notation */ if (toupper(*argument)=='M') bSize<<=20, argument++; if (toupper(*argument)=='B') argument++; BMK_setNotificationLevel(displayLevel); BMK_SetBlockSize(bSize); } break; #endif /* ZSTD_NOBENCH */ /* Dictionary Selection level */ case 's': argument++; dictSelect = readU32FromChar(&argument); break; /* Pause at the end (-p) or set an additional param (-p#) (hidden option) */ case 'p': argument++; #ifndef ZSTD_NOBENCH if ((*argument>='0') && (*argument<='9')) { BMK_setAdditionalParam(readU32FromChar(&argument)); } else #endif main_pause=1; break; /* unknown command */ default : CLEAN_RETURN(badusage(programName)); } } continue; } /* if (argument[0]=='-') */ if (nextArgumentIsMaxDict) { nextArgumentIsMaxDict = 0; maxDictSize = readU32FromChar(&argument); if (toupper(*argument)=='K') maxDictSize <<= 10; if (toupper(*argument)=='M') maxDictSize <<= 20; continue; } if (nextArgumentIsDictID) { nextArgumentIsDictID = 0; dictID = readU32FromChar(&argument); continue; } } /* if (nextArgumentIsAFile==0) */ if (nextEntryIsDictionary) { nextEntryIsDictionary = 0; dictFileName = argument; continue; } if (nextArgumentIsOutFileName) { nextArgumentIsOutFileName = 0; outFileName = argument; if (!strcmp(outFileName, "-")) outFileName = stdoutmark; continue; } /* add filename to list */ filenameTable[filenameIdx++] = argument; } /* Welcome message (if verbose) */ DISPLAYLEVEL(3, WELCOME_MESSAGE); #ifdef UTIL_HAS_CREATEFILELIST if (recursive) { fileNamesTable = UTIL_createFileList(filenameTable, filenameIdx, &fileNamesBuf, &fileNamesNb); if (fileNamesTable) { unsigned i; for (i=0; i<fileNamesNb; i++) DISPLAYLEVEL(4, "%d %s\n", i, fileNamesTable[i]); free((void*)filenameTable); filenameTable = fileNamesTable; filenameIdx = fileNamesNb; } } #endif /* Check if benchmark is selected */ if (bench) { #ifndef ZSTD_NOBENCH BMK_setNotificationLevel(displayLevel); BMK_benchFiles(filenameTable, filenameIdx, dictFileName, cLevel, cLevelLast); #endif goto _end; } /* Check if dictionary builder is selected */ if (dictBuild) { #ifndef ZSTD_NODICT ZDICT_params_t dictParams; memset(&dictParams, 0, sizeof(dictParams)); dictParams.compressionLevel = dictCLevel; dictParams.selectivityLevel = dictSelect; dictParams.notificationLevel = displayLevel; dictParams.dictID = dictID; DiB_trainFromFiles(outFileName, maxDictSize, filenameTable, filenameIdx, dictParams); #endif goto _end; } /* No input filename ==> use stdin and stdout */ filenameIdx += !filenameIdx; /*< default input is stdin */ if (!strcmp(filenameTable[0], stdinmark) && !outFileName) outFileName = stdoutmark; /*< when input is stdin, default output is stdout */ /* Check if input/output defined as console; trigger an error in this case */ if (!strcmp(filenameTable[0], stdinmark) && IS_CONSOLE(stdin) ) CLEAN_RETURN(badusage(programName)); if (outFileName && !strcmp(outFileName, stdoutmark) && IS_CONSOLE(stdout) && !(forceStdout && decode)) CLEAN_RETURN(badusage(programName)); /* user-selected output filename, only possible with a single file */ if (outFileName && strcmp(outFileName,stdoutmark) && strcmp(outFileName,nulmark) && (filenameIdx>1)) { DISPLAY("Too many files (%u) on the command line. \n", filenameIdx); CLEAN_RETURN(filenameIdx); } /* No warning message in pipe mode (stdin + stdout) or multiple mode */ if (!strcmp(filenameTable[0], stdinmark) && outFileName && !strcmp(outFileName,stdoutmark) && (displayLevel==2)) displayLevel=1; if ((filenameIdx>1) & (displayLevel==2)) displayLevel=1; /* IO Stream/File */ FIO_setNotificationLevel(displayLevel); #ifndef ZSTD_NOCOMPRESS if (!decode) { if (filenameIdx==1 && outFileName) operationResult = FIO_compressFilename(outFileName, filenameTable[0], dictFileName, cLevel); else operationResult = FIO_compressMultipleFilenames(filenameTable, filenameIdx, outFileName ? outFileName : ZSTD_EXTENSION, dictFileName, cLevel); } else #endif { /* decompression */ #ifndef ZSTD_NODECOMPRESS if (testmode) { outFileName=nulmark; FIO_setRemoveSrcFile(0); } /* test mode */ if (filenameIdx==1 && outFileName) operationResult = FIO_decompressFilename(outFileName, filenameTable[0], dictFileName); else operationResult = FIO_decompressMultipleFilenames(filenameTable, filenameIdx, outFileName ? outFileName : ZSTD_EXTENSION, dictFileName); #else DISPLAY("Decompression not supported\n"); #endif } _end: if (main_pause) waitEnter(); free(dynNameSpace); #ifdef UTIL_HAS_CREATEFILELIST if (fileNamesTable) UTIL_freeFileList(fileNamesTable, fileNamesBuf); else #endif free((void*)filenameTable); return operationResult; }
static int usage_advanced(const char* programName) { DISPLAY(WELCOME_MESSAGE); usage(programName); DISPLAY( "\n"); DISPLAY( "Advanced arguments :\n"); DISPLAY( " -V : display Version number and exit\n"); DISPLAY( " -v : verbose mode; specify multiple times to increase log level (default:%d)\n", DEFAULT_DISPLAY_LEVEL); DISPLAY( " -q : suppress warnings; specify twice to suppress errors too\n"); DISPLAY( " -c : force write to standard output, even if it is the console\n"); #ifdef UTIL_HAS_CREATEFILELIST DISPLAY( " -r : operate recursively on directories\n"); #endif #ifndef ZSTD_NOCOMPRESS DISPLAY( "--ultra : enable levels beyond %i, up to %i (requires more memory)\n", ZSTDCLI_CLEVEL_MAX, ZSTD_maxCLevel()); DISPLAY( "--no-dictID : don't write dictID into header (dictionary compression)\n"); DISPLAY( "--[no-]check : integrity check (default:enabled)\n"); #endif #ifndef ZSTD_NODECOMPRESS DISPLAY( "--test : test compressed file integrity \n"); DISPLAY( "--[no-]sparse : sparse mode (default:enabled on file, disabled on stdout)\n"); #endif DISPLAY( "-- : All arguments after \"--\" are treated as files \n"); #ifndef ZSTD_NODICT DISPLAY( "\n"); DISPLAY( "Dictionary builder :\n"); DISPLAY( "--train ## : create a dictionary from a training set of files \n"); DISPLAY( " -o file : `file` is dictionary name (default: %s) \n", g_defaultDictName); DISPLAY( "--maxdict ## : limit dictionary to specified size (default : %u) \n", g_defaultMaxDictSize); DISPLAY( " -s# : dictionary selectivity level (default: %u)\n", g_defaultSelectivityLevel); DISPLAY( "--dictID ## : force dictionary ID to specified value (default: random)\n"); #endif #ifndef ZSTD_NOBENCH DISPLAY( "\n"); DISPLAY( "Benchmark arguments :\n"); DISPLAY( " -b# : benchmark file(s), using # compression level (default : 1) \n"); DISPLAY( " -e# : test all compression levels from -bX to # (default: 1)\n"); DISPLAY( " -i# : minimum evaluation time in seconds (default : 3s)\n"); DISPLAY( " -B# : cut file into independent blocks of size # (default: no block)\n"); #endif return 0; }
static void zstd_usage(void) { fprintf(stderr, "\t -Xcompression-level <compression-level>\n"); fprintf(stderr, "\t\t<compression-level> should be 1 .. %d (default " "%d)\n", ZSTD_maxCLevel(), ZSTD_DEFAULT_COMPRESSION_LEVEL); }
Options::Status Options::parse(int argc, const char **argv) { bool test = false; bool recursive = false; bool ultra = false; bool forceStdout = false; // Local copy of input files, which are pointers into argv. std::vector<const char *> localInputFiles; for (int i = 1; i < argc; ++i) { const char *arg = argv[i]; // Protect against empty arguments if (arg[0] == 0) { continue; } // Everything after "--" is an input file if (!std::strcmp(arg, "--")) { ++i; std::copy(argv + i, argv + argc, std::back_inserter(localInputFiles)); break; } // Long arguments that don't have a short option { bool isLongOption = true; if (!std::strcmp(arg, "--rm")) { keepSource = false; } else if (!std::strcmp(arg, "--ultra")) { ultra = true; maxWindowLog = 0; } else if (!std::strcmp(arg, "--no-check")) { checksum = false; } else if (!std::strcmp(arg, "--sparse")) { writeMode = WriteMode::Sparse; notSupported("Sparse mode"); return Status::Failure; } else if (!std::strcmp(arg, "--no-sparse")) { writeMode = WriteMode::Regular; notSupported("Sparse mode"); return Status::Failure; } else if (!std::strcmp(arg, "--dictID")) { notSupported(arg); return Status::Failure; } else if (!std::strcmp(arg, "--no-dictID")) { notSupported(arg); return Status::Failure; } else { isLongOption = false; } if (isLongOption) { continue; } } // Arguments with a short option simply set their short option. const char *options = nullptr; if (!std::strcmp(arg, "--processes")) { options = "p"; } else if (!std::strcmp(arg, "--version")) { options = "V"; } else if (!std::strcmp(arg, "--help")) { options = "h"; } else if (!std::strcmp(arg, "--decompress")) { options = "d"; } else if (!std::strcmp(arg, "--force")) { options = "f"; } else if (!std::strcmp(arg, "--stdout")) { options = "c"; } else if (!std::strcmp(arg, "--keep")) { options = "k"; } else if (!std::strcmp(arg, "--verbose")) { options = "v"; } else if (!std::strcmp(arg, "--quiet")) { options = "q"; } else if (!std::strcmp(arg, "--check")) { options = "C"; } else if (!std::strcmp(arg, "--test")) { options = "t"; } else if (arg[0] == '-' && arg[1] != 0) { options = arg + 1; } else { localInputFiles.emplace_back(arg); continue; } assert(options != nullptr); bool finished = false; while (!finished && *options != 0) { // Parse the compression level if (*options >= '0' && *options <= '9') { compressionLevel = parseUnsigned(&options); continue; } switch (*options) { case 'h': case 'H': usage(); return Status::Message; case 'V': std::fprintf(stderr, "PZSTD version: %s.\n", ZSTD_VERSION_STRING); return Status::Message; case 'p': { finished = true; const char *optionArgument = getArgument(options, argv, i, argc); if (optionArgument == nullptr) { return Status::Failure; } if (*optionArgument < '0' || *optionArgument > '9') { std::fprintf(stderr, "Option -p expects a number, but %s provided\n", optionArgument); return Status::Failure; } numThreads = parseUnsigned(&optionArgument); if (*optionArgument != 0) { std::fprintf(stderr, "Option -p expects a number, but %u%s provided\n", numThreads, optionArgument); return Status::Failure; } break; } case 'o': { finished = true; const char *optionArgument = getArgument(options, argv, i, argc); if (optionArgument == nullptr) { return Status::Failure; } outputFile = optionArgument; break; } case 'C': checksum = true; break; case 'k': keepSource = true; break; case 'd': decompress = true; break; case 'f': overwrite = true; forceStdout = true; break; case 't': test = true; decompress = true; break; #ifdef UTIL_HAS_CREATEFILELIST case 'r': recursive = true; break; #endif case 'c': outputFile = kStdOut; forceStdout = true; break; case 'v': ++verbosity; break; case 'q': --verbosity; // Ignore them for now break; // Unsupported options from Zstd case 'D': case 's': notSupported("Zstd dictionaries."); return Status::Failure; case 'b': case 'e': case 'i': case 'B': notSupported("Zstd benchmarking options."); return Status::Failure; default: std::fprintf(stderr, "Invalid argument: %s\n", arg); return Status::Failure; } if (!finished) { ++options; } } // while (*options != 0); } // for (int i = 1; i < argc; ++i); // Input file defaults to standard input if not provided. if (localInputFiles.empty()) { localInputFiles.emplace_back(kStdIn); } // Check validity of input files if (localInputFiles.size() > 1) { const auto it = std::find(localInputFiles.begin(), localInputFiles.end(), std::string{kStdIn}); if (it != localInputFiles.end()) { std::fprintf( stderr, "Cannot specify standard input when handling multiple files\n"); return Status::Failure; } } if (localInputFiles.size() > 1 || recursive) { if (!outputFile.empty() && outputFile != nullOutput) { std::fprintf( stderr, "Cannot specify an output file when handling multiple inputs\n"); return Status::Failure; } } // Translate input files/directories into files to (de)compress if (recursive) { char *scratchBuffer = nullptr; unsigned numFiles = 0; const char **files = UTIL_createFileList(localInputFiles.data(), localInputFiles.size(), &scratchBuffer, &numFiles); if (files == nullptr) { std::fprintf(stderr, "Error traversing directories\n"); return Status::Failure; } auto guard = makeScopeGuard([&] { UTIL_freeFileList(files, scratchBuffer); }); if (numFiles == 0) { std::fprintf(stderr, "No files found\n"); return Status::Failure; } inputFiles.resize(numFiles); std::copy(files, files + numFiles, inputFiles.begin()); } else { inputFiles.resize(localInputFiles.size()); std::copy(localInputFiles.begin(), localInputFiles.end(), inputFiles.begin()); } localInputFiles.clear(); assert(!inputFiles.empty()); // If reading from standard input, default to standard output if (inputFiles[0] == kStdIn && outputFile.empty()) { assert(inputFiles.size() == 1); outputFile = "-"; } if (inputFiles[0] == kStdIn && IS_CONSOLE(stdin)) { assert(inputFiles.size() == 1); std::fprintf(stderr, "Cannot read input from interactive console\n"); return Status::Failure; } if (outputFile == "-" && IS_CONSOLE(stdout) && !(forceStdout && decompress)) { std::fprintf(stderr, "Will not write to console stdout unless -c or -f is " "specified and decompressing\n"); return Status::Failure; } // Check compression level { unsigned maxCLevel = ultra ? ZSTD_maxCLevel() : kMaxNonUltraCompressionLevel; if (compressionLevel > maxCLevel || compressionLevel == 0) { std::fprintf(stderr, "Invalid compression level %u.\n", compressionLevel); return Status::Failure; } } // Check that numThreads is set if (numThreads == 0) { std::fprintf(stderr, "Invalid arguments: # of threads not specified " "and unable to determine hardware concurrency.\n"); return Status::Failure; } // Modify verbosity // If we are piping input and output, turn off interaction if (inputFiles[0] == kStdIn && outputFile == kStdOut && verbosity == 2) { verbosity = 1; } // If we are in multi-file mode, turn off interaction if (inputFiles.size() > 1 && verbosity == 2) { verbosity = 1; } // Set options for test mode if (test) { outputFile = nullOutput; keepSource = true; } return Status::Success; }
size_t ZSTDMT_compressCCtx(ZSTDMT_CCtx* mtctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize, int compressionLevel) { ZSTD_parameters params = ZSTD_getParams(compressionLevel, srcSize, 0); U32 const overlapLog = (compressionLevel >= ZSTD_maxCLevel()) ? 0 : 3; size_t const overlapSize = (size_t)1 << (params.cParams.windowLog - overlapLog); size_t const chunkTargetSize = (size_t)1 << (params.cParams.windowLog + 2); unsigned const nbChunksMax = (unsigned)(srcSize / chunkTargetSize) + 1; unsigned nbChunks = MIN(nbChunksMax, mtctx->nbThreads); size_t const proposedChunkSize = (srcSize + (nbChunks-1)) / nbChunks; size_t const avgChunkSize = ((proposedChunkSize & 0x1FFFF) < 0xFFFF) ? proposedChunkSize + 0xFFFF : proposedChunkSize; /* avoid too small last block */ size_t remainingSrcSize = srcSize; const char* const srcStart = (const char*)src; unsigned const compressWithinDst = (dstCapacity >= ZSTD_compressBound(srcSize)) ? nbChunks : (unsigned)(dstCapacity / ZSTD_compressBound(avgChunkSize)); /* presumes avgChunkSize >= 256 KB, which should be the case */ size_t frameStartPos = 0, dstBufferPos = 0; DEBUGLOG(3, "windowLog : %2u => chunkTargetSize : %u bytes ", params.cParams.windowLog, (U32)chunkTargetSize); DEBUGLOG(2, "nbChunks : %2u (chunkSize : %u bytes) ", nbChunks, (U32)avgChunkSize); params.fParams.contentSizeFlag = 1; if (nbChunks==1) { /* fallback to single-thread mode */ ZSTD_CCtx* const cctx = mtctx->cctxPool->cctx[0]; return ZSTD_compressCCtx(cctx, dst, dstCapacity, src, srcSize, compressionLevel); } { unsigned u; for (u=0; u<nbChunks; u++) { size_t const chunkSize = MIN(remainingSrcSize, avgChunkSize); size_t const dstBufferCapacity = ZSTD_compressBound(chunkSize); buffer_t const dstAsBuffer = { (char*)dst + dstBufferPos, dstBufferCapacity }; buffer_t const dstBuffer = u < compressWithinDst ? dstAsBuffer : ZSTDMT_getBuffer(mtctx->buffPool, dstBufferCapacity); ZSTD_CCtx* const cctx = ZSTDMT_getCCtx(mtctx->cctxPool); size_t dictSize = u ? overlapSize : 0; if ((cctx==NULL) || (dstBuffer.start==NULL)) { mtctx->jobs[u].cSize = ERROR(memory_allocation); /* job result */ mtctx->jobs[u].jobCompleted = 1; nbChunks = u+1; break; /* let's wait for previous jobs to complete, but don't start new ones */ } mtctx->jobs[u].srcStart = srcStart + frameStartPos - dictSize; mtctx->jobs[u].dictSize = dictSize; mtctx->jobs[u].srcSize = chunkSize; mtctx->jobs[u].fullFrameSize = srcSize; mtctx->jobs[u].params = params; mtctx->jobs[u].dstBuff = dstBuffer; mtctx->jobs[u].cctx = cctx; mtctx->jobs[u].firstChunk = (u==0); mtctx->jobs[u].lastChunk = (u==nbChunks-1); mtctx->jobs[u].jobCompleted = 0; mtctx->jobs[u].jobCompleted_mutex = &mtctx->jobCompleted_mutex; mtctx->jobs[u].jobCompleted_cond = &mtctx->jobCompleted_cond; DEBUGLOG(3, "posting job %u (%u bytes)", u, (U32)chunkSize); DEBUG_PRINTHEX(3, mtctx->jobs[u].srcStart, 12); POOL_add(mtctx->factory, ZSTDMT_compressChunk, &mtctx->jobs[u]); frameStartPos += chunkSize; dstBufferPos += dstBufferCapacity; remainingSrcSize -= chunkSize; } } /* note : since nbChunks <= nbThreads, all jobs should be running immediately in parallel */ { unsigned chunkID; size_t error = 0, dstPos = 0; for (chunkID=0; chunkID<nbChunks; chunkID++) { DEBUGLOG(3, "waiting for chunk %u ", chunkID); PTHREAD_MUTEX_LOCK(&mtctx->jobCompleted_mutex); while (mtctx->jobs[chunkID].jobCompleted==0) { DEBUGLOG(4, "waiting for jobCompleted signal from chunk %u", chunkID); pthread_cond_wait(&mtctx->jobCompleted_cond, &mtctx->jobCompleted_mutex); } pthread_mutex_unlock(&mtctx->jobCompleted_mutex); DEBUGLOG(3, "ready to write chunk %u ", chunkID); ZSTDMT_releaseCCtx(mtctx->cctxPool, mtctx->jobs[chunkID].cctx); mtctx->jobs[chunkID].cctx = NULL; mtctx->jobs[chunkID].srcStart = NULL; { size_t const cSize = mtctx->jobs[chunkID].cSize; if (ZSTD_isError(cSize)) error = cSize; if ((!error) && (dstPos + cSize > dstCapacity)) error = ERROR(dstSize_tooSmall); if (chunkID) { /* note : chunk 0 is already written directly into dst */ if (!error) memmove((char*)dst + dstPos, mtctx->jobs[chunkID].dstBuff.start, cSize); /* may overlap if chunk decompressed within dst */ if (chunkID >= compressWithinDst) /* otherwise, it decompresses within dst */ ZSTDMT_releaseBuffer(mtctx->buffPool, mtctx->jobs[chunkID].dstBuff); mtctx->jobs[chunkID].dstBuff = g_nullBuffer; } dstPos += cSize ; } } if (!error) DEBUGLOG(3, "compressed size : %u ", (U32)dstPos); return error ? error : dstPos; } }