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 
11*22ce4affSfengbojiang #include <stdio.h>     // printf
12*22ce4affSfengbojiang #include <stdlib.h>    // free
13*22ce4affSfengbojiang #include <string.h>    // strlen, strcat, memset
14*22ce4affSfengbojiang #include <zstd.h>      // presumes zstd library is installed
15*22ce4affSfengbojiang #include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()
16*22ce4affSfengbojiang 
compress_orDie(const char * fname,const char * oname)17*22ce4affSfengbojiang static void compress_orDie(const char* fname, const char* oname)
18*22ce4affSfengbojiang {
19*22ce4affSfengbojiang     size_t fSize;
20*22ce4affSfengbojiang     void* const fBuff = mallocAndLoadFile_orDie(fname, &fSize);
21*22ce4affSfengbojiang     size_t const cBuffSize = ZSTD_compressBound(fSize);
22*22ce4affSfengbojiang     void* const cBuff = malloc_orDie(cBuffSize);
23*22ce4affSfengbojiang 
24*22ce4affSfengbojiang     /* Compress.
25*22ce4affSfengbojiang      * If you are doing many compressions, you may want to reuse the context.
26*22ce4affSfengbojiang      * See the multiple_simple_compression.c example.
27*22ce4affSfengbojiang      */
28*22ce4affSfengbojiang     size_t const cSize = ZSTD_compress(cBuff, cBuffSize, fBuff, fSize, 1);
29*22ce4affSfengbojiang     CHECK_ZSTD(cSize);
30*22ce4affSfengbojiang 
31*22ce4affSfengbojiang     saveFile_orDie(oname, cBuff, cSize);
32*22ce4affSfengbojiang 
33*22ce4affSfengbojiang     /* success */
34*22ce4affSfengbojiang     printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname);
35*22ce4affSfengbojiang 
36*22ce4affSfengbojiang     free(fBuff);
37*22ce4affSfengbojiang     free(cBuff);
38*22ce4affSfengbojiang }
39*22ce4affSfengbojiang 
createOutFilename_orDie(const char * filename)40*22ce4affSfengbojiang static char* createOutFilename_orDie(const char* filename)
41*22ce4affSfengbojiang {
42*22ce4affSfengbojiang     size_t const inL = strlen(filename);
43*22ce4affSfengbojiang     size_t const outL = inL + 5;
44*22ce4affSfengbojiang     void* const outSpace = malloc_orDie(outL);
45*22ce4affSfengbojiang     memset(outSpace, 0, outL);
46*22ce4affSfengbojiang     strcat(outSpace, filename);
47*22ce4affSfengbojiang     strcat(outSpace, ".zst");
48*22ce4affSfengbojiang     return (char*)outSpace;
49*22ce4affSfengbojiang }
50*22ce4affSfengbojiang 
main(int argc,const char ** argv)51*22ce4affSfengbojiang int main(int argc, const char** argv)
52*22ce4affSfengbojiang {
53*22ce4affSfengbojiang     const char* const exeName = argv[0];
54*22ce4affSfengbojiang 
55*22ce4affSfengbojiang     if (argc!=2) {
56*22ce4affSfengbojiang         printf("wrong arguments\n");
57*22ce4affSfengbojiang         printf("usage:\n");
58*22ce4affSfengbojiang         printf("%s FILE\n", exeName);
59*22ce4affSfengbojiang         return 1;
60*22ce4affSfengbojiang     }
61*22ce4affSfengbojiang 
62*22ce4affSfengbojiang     const char* const inFilename = argv[1];
63*22ce4affSfengbojiang 
64*22ce4affSfengbojiang     char* const outFilename = createOutFilename_orDie(inFilename);
65*22ce4affSfengbojiang     compress_orDie(inFilename, outFilename);
66*22ce4affSfengbojiang     free(outFilename);
67*22ce4affSfengbojiang     return 0;
68*22ce4affSfengbojiang }
69