Compress-Stream-Zstd

 view release on metacpan or  search on metacpan

ext/zstd/programs/zstdcli.c  view on Meta::CPAN

#endif
#ifndef ZSTD_NODICT
#  include "dibio.h"  /* ZDICT_cover_params_t, DiB_trainFromFiles() */
#endif
#ifndef ZSTD_NOTRACE
#  include "zstdcli_trace.h"
#endif
#include "../lib/zstd.h"  /* ZSTD_VERSION_STRING, ZSTD_minCLevel, ZSTD_maxCLevel */
#include "fileio_asyncio.h"


/*-************************************
*  Constants
**************************************/
#define COMPRESSOR_NAME "Zstandard CLI"
#ifndef ZSTD_VERSION
#  define ZSTD_VERSION "v" ZSTD_VERSION_STRING
#endif
#define AUTHOR "Yann Collet"
#define WELCOME_MESSAGE "*** %s (%i-bit) %s, by %s ***\n", COMPRESSOR_NAME, (int)(sizeof(size_t)*8), ZSTD_VERSION, AUTHOR

#define ZSTD_ZSTDMT "zstdmt"
#define ZSTD_UNZSTD "unzstd"
#define ZSTD_CAT "zstdcat"
#define ZSTD_ZCAT "zcat"
#define ZSTD_GZ "gzip"
#define ZSTD_GUNZIP "gunzip"
#define ZSTD_GZCAT "gzcat"
#define ZSTD_LZMA "lzma"
#define ZSTD_UNLZMA "unlzma"
#define ZSTD_XZ "xz"
#define ZSTD_UNXZ "unxz"
#define ZSTD_LZ4 "lz4"
#define ZSTD_UNLZ4 "unlz4"

#define KB *(1 <<10)
#define MB *(1 <<20)
#define GB *(1U<<30)

#define DISPLAY_LEVEL_DEFAULT 2

static const char*    g_defaultDictName = "dictionary";
static const unsigned g_defaultMaxDictSize = 110 KB;
static const int      g_defaultDictCLevel = 3;
static const unsigned g_defaultSelectivityLevel = 9;
static const unsigned g_defaultMaxWindowLog = 27;
#define OVERLAP_LOG_DEFAULT 9999
#define LDM_PARAM_DEFAULT 9999  /* Default for parameters where 0 is valid */
static U32 g_overlapLog = OVERLAP_LOG_DEFAULT;
static U32 g_ldmHashLog = 0;
static U32 g_ldmMinMatch = 0;
static U32 g_ldmHashRateLog = LDM_PARAM_DEFAULT;
static U32 g_ldmBucketSizeLog = LDM_PARAM_DEFAULT;


#define DEFAULT_ACCEL 1

typedef enum { cover, fastCover, legacy } dictType;

/*-************************************
*  Display Macros
**************************************/
#define DISPLAY_F(f, ...)    fprintf((f), __VA_ARGS__)
#define DISPLAYOUT(...)      DISPLAY_F(stdout, __VA_ARGS__)
#define DISPLAY(...)         DISPLAY_F(stderr, __VA_ARGS__)
#define DISPLAYLEVEL(l, ...) { if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } }
static int g_displayLevel = DISPLAY_LEVEL_DEFAULT;   /* 0 : no display,  1: errors,  2 : + result + interaction + warnings,  3 : + progression,  4 : + information */


/*-************************************
*  Check Version (when CLI linked to dynamic library)
**************************************/

/* Due to usage of experimental symbols and capabilities by the CLI,
 * the CLI must be linked against a dynamic library of same version */
static void checkLibVersion(void)
{
    if (strcmp(ZSTD_VERSION_STRING, ZSTD_versionString())) {
        DISPLAYLEVEL(1, "Error : incorrect library version (expecting : %s ; actual : %s ) \n",
                    ZSTD_VERSION_STRING, ZSTD_versionString());
        DISPLAYLEVEL(1, "Please update library to version %s, or use stand-alone zstd binary \n",
                    ZSTD_VERSION_STRING);
        exit(1);
    }
}


/*! exeNameMatch() :
    @return : a non-zero value if exeName matches test, excluding the extension
   */
static int exeNameMatch(const char* exeName, const char* test)
{
    return !strncmp(exeName, test, strlen(test)) &&
        (exeName[strlen(test)] == '\0' || exeName[strlen(test)] == '.');
}

/*-************************************
*  Command Line
**************************************/
/* print help either in `stderr` or `stdout` depending on originating request
 * error (badusage) => stderr
 * help (usage_advanced) => stdout
 */
static void usage(FILE* f, const char* programName)
{
    DISPLAY_F(f, "Compress or decompress the INPUT file(s); reads from STDIN if INPUT is `-` or not provided.\n\n");
    DISPLAY_F(f, "Usage: %s [OPTIONS...] [INPUT... | -] [-o OUTPUT]\n\n", programName);
    DISPLAY_F(f, "Options:\n");
    DISPLAY_F(f, "  -o OUTPUT                     Write output to a single file, OUTPUT.\n");
    DISPLAY_F(f, "  -k, --keep                    Preserve INPUT file(s). [Default] \n");
    DISPLAY_F(f, "  --rm                          Remove INPUT file(s) after successful (de)compression.\n");
#ifdef ZSTD_GZCOMPRESS
    if (exeNameMatch(programName, ZSTD_GZ)) {     /* behave like gzip */
        DISPLAY_F(f, "  -n, --no-name                 Do not store original filename when compressing.\n\n");
    }
#endif
    DISPLAY_F(f, "\n");
#ifndef ZSTD_NOCOMPRESS
    DISPLAY_F(f, "  -#                            Desired compression level, where `#` is a number between 1 and %d;\n", ZSTDCLI_CLEVEL_MAX);
    DISPLAY_F(f, "                                lower numbers provide faster compression, higher numbers yield\n");
    DISPLAY_F(f, "                                better compression ratios. [Default: %d]\n\n", ZSTDCLI_CLEVEL_DEFAULT);



( run in 0.594 second using v1.01-cache-2.11-cpan-df04353d9ac )