1*22ce4affSfengbojiang /*
2*22ce4affSfengbojiang * Copyright (c) 2016-2020 Yann Collet, Facebook, Inc.
3*22ce4affSfengbojiang * All rights reserved.
4*22ce4affSfengbojiang *
5*22ce4affSfengbojiang * This source code is licensed under both the BSD-style license (found in the
6*22ce4affSfengbojiang * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*22ce4affSfengbojiang * in the COPYING file in the root directory of this source tree).
8*22ce4affSfengbojiang * You may select, at your option, one of the above-listed licenses.
9*22ce4affSfengbojiang */
10*22ce4affSfengbojiang #include <stdio.h> // printf
11*22ce4affSfengbojiang #include <stdlib.h> // free
12*22ce4affSfengbojiang #include <string.h> // memset, strcat
13*22ce4affSfengbojiang #include <zstd.h> // presumes zstd library is installed
14*22ce4affSfengbojiang #include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()
15*22ce4affSfengbojiang
16*22ce4affSfengbojiang /* createDict() :
17*22ce4affSfengbojiang `dictFileName` is supposed to have been created using `zstd --train` */
createCDict_orDie(const char * dictFileName,int cLevel)18*22ce4affSfengbojiang static ZSTD_CDict* createCDict_orDie(const char* dictFileName, int cLevel)
19*22ce4affSfengbojiang {
20*22ce4affSfengbojiang size_t dictSize;
21*22ce4affSfengbojiang printf("loading dictionary %s \n", dictFileName);
22*22ce4affSfengbojiang void* const dictBuffer = mallocAndLoadFile_orDie(dictFileName, &dictSize);
23*22ce4affSfengbojiang ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, cLevel);
24*22ce4affSfengbojiang CHECK(cdict != NULL, "ZSTD_createCDict() failed!");
25*22ce4affSfengbojiang free(dictBuffer);
26*22ce4affSfengbojiang return cdict;
27*22ce4affSfengbojiang }
28*22ce4affSfengbojiang
29*22ce4affSfengbojiang
compress(const char * fname,const char * oname,const ZSTD_CDict * cdict)30*22ce4affSfengbojiang static void compress(const char* fname, const char* oname, const ZSTD_CDict* cdict)
31*22ce4affSfengbojiang {
32*22ce4affSfengbojiang size_t fSize;
33*22ce4affSfengbojiang void* const fBuff = mallocAndLoadFile_orDie(fname, &fSize);
34*22ce4affSfengbojiang size_t const cBuffSize = ZSTD_compressBound(fSize);
35*22ce4affSfengbojiang void* const cBuff = malloc_orDie(cBuffSize);
36*22ce4affSfengbojiang
37*22ce4affSfengbojiang /* Compress using the dictionary.
38*22ce4affSfengbojiang * This function writes the dictionary id, and content size into the header.
39*22ce4affSfengbojiang * But, it doesn't use a checksum. You can control these options using the
40*22ce4affSfengbojiang * advanced API: ZSTD_CCtx_setParameter(), ZSTD_CCtx_refCDict(),
41*22ce4affSfengbojiang * and ZSTD_compress2().
42*22ce4affSfengbojiang */
43*22ce4affSfengbojiang ZSTD_CCtx* const cctx = ZSTD_createCCtx();
44*22ce4affSfengbojiang CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");
45*22ce4affSfengbojiang size_t const cSize = ZSTD_compress_usingCDict(cctx, cBuff, cBuffSize, fBuff, fSize, cdict);
46*22ce4affSfengbojiang CHECK_ZSTD(cSize);
47*22ce4affSfengbojiang
48*22ce4affSfengbojiang saveFile_orDie(oname, cBuff, cSize);
49*22ce4affSfengbojiang
50*22ce4affSfengbojiang /* success */
51*22ce4affSfengbojiang printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname);
52*22ce4affSfengbojiang
53*22ce4affSfengbojiang ZSTD_freeCCtx(cctx); /* never fails */
54*22ce4affSfengbojiang free(fBuff);
55*22ce4affSfengbojiang free(cBuff);
56*22ce4affSfengbojiang }
57*22ce4affSfengbojiang
58*22ce4affSfengbojiang
createOutFilename_orDie(const char * filename)59*22ce4affSfengbojiang static char* createOutFilename_orDie(const char* filename)
60*22ce4affSfengbojiang {
61*22ce4affSfengbojiang size_t const inL = strlen(filename);
62*22ce4affSfengbojiang size_t const outL = inL + 5;
63*22ce4affSfengbojiang void* outSpace = malloc_orDie(outL);
64*22ce4affSfengbojiang memset(outSpace, 0, outL);
65*22ce4affSfengbojiang strcat(outSpace, filename);
66*22ce4affSfengbojiang strcat(outSpace, ".zst");
67*22ce4affSfengbojiang return (char*)outSpace;
68*22ce4affSfengbojiang }
69*22ce4affSfengbojiang
main(int argc,const char ** argv)70*22ce4affSfengbojiang int main(int argc, const char** argv)
71*22ce4affSfengbojiang {
72*22ce4affSfengbojiang const char* const exeName = argv[0];
73*22ce4affSfengbojiang int const cLevel = 3;
74*22ce4affSfengbojiang
75*22ce4affSfengbojiang if (argc<3) {
76*22ce4affSfengbojiang fprintf(stderr, "wrong arguments\n");
77*22ce4affSfengbojiang fprintf(stderr, "usage:\n");
78*22ce4affSfengbojiang fprintf(stderr, "%s [FILES] dictionary\n", exeName);
79*22ce4affSfengbojiang return 1;
80*22ce4affSfengbojiang }
81*22ce4affSfengbojiang
82*22ce4affSfengbojiang /* load dictionary only once */
83*22ce4affSfengbojiang const char* const dictName = argv[argc-1];
84*22ce4affSfengbojiang ZSTD_CDict* const dictPtr = createCDict_orDie(dictName, cLevel);
85*22ce4affSfengbojiang
86*22ce4affSfengbojiang int u;
87*22ce4affSfengbojiang for (u=1; u<argc-1; u++) {
88*22ce4affSfengbojiang const char* inFilename = argv[u];
89*22ce4affSfengbojiang char* const outFilename = createOutFilename_orDie(inFilename);
90*22ce4affSfengbojiang compress(inFilename, outFilename, dictPtr);
91*22ce4affSfengbojiang free(outFilename);
92*22ce4affSfengbojiang }
93*22ce4affSfengbojiang
94*22ce4affSfengbojiang ZSTD_freeCDict(dictPtr);
95*22ce4affSfengbojiang printf("All %u files compressed. \n", argc-2);
96*22ce4affSfengbojiang return 0;
97*22ce4affSfengbojiang }
98