1 /*
2  * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under both the BSD-style license (found in the
6  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7  * in the COPYING file in the root directory of this source tree).
8  * You may select, at your option, one of the above-listed licenses.
9  */
10 
11 
12 #include <stdio.h>     // printf
13 #include <stdlib.h>    // free
14 #include <string.h>    // memset, strcat, strlen
15 #include <zstd.h>      // presumes zstd library is installed
16 #include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()
17 
18 
19 static void compressFile_orDie(const char* fname, const char* outName, int cLevel)
20 {
21     /* Open the input and output files. */
22     FILE* const fin  = fopen_orDie(fname, "rb");
23     FILE* const fout = fopen_orDie(outName, "wb");
24     /* Create the input and output buffers.
25      * They may be any size, but we recommend using these functions to size them.
26      * Performance will only suffer significantly for very tiny buffers.
27      */
28     size_t const buffInSize = ZSTD_CStreamInSize();
29     void*  const buffIn  = malloc_orDie(buffInSize);
30     size_t const buffOutSize = ZSTD_CStreamOutSize();
31     void*  const buffOut = malloc_orDie(buffOutSize);
32 
33     /* Create the context. */
34     ZSTD_CCtx* const cctx = ZSTD_createCCtx();
35     CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");
36 
37     /* Set any parameters you want.
38      * Here we set the compression level, and enable the checksum.
39      */
40     CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, cLevel) );
41     CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1) );
42 
43     /* This loop read from the input file, compresses that entire chunk,
44      * and writes all output produced to the output file.
45      */
46     size_t const toRead = buffInSize;
47     size_t read;
48     while ((read = fread_orDie(buffIn, toRead, fin))) {
49         /* Select the flush mode.
50          * If the read may not be finished (read == toRead) we use
51          * ZSTD_e_continue. If this is the last chunk, we use ZSTD_e_end.
52          * Zstd optimizes the case where the first flush mode is ZSTD_e_end,
53          * since it knows it is compressing the entire source in one pass.
54          */
55         int const lastChunk = (read < toRead);
56         ZSTD_EndDirective const mode = lastChunk ? ZSTD_e_end : ZSTD_e_continue;
57         /* Set the input buffer to what we just read.
58          * We compress until the input buffer is empty, each time flushing the
59          * output.
60          */
61         ZSTD_inBuffer input = { buffIn, read, 0 };
62         int finished;
63         do {
64             /* Compress into the output buffer and write all of the output to
65              * the file so we can reuse the buffer next iteration.
66              */
67             ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
68             size_t const remaining = ZSTD_compressStream2(cctx, &output , &input, mode);
69             CHECK_ZSTD(remaining);
70             fwrite_orDie(buffOut, output.pos, fout);
71             /* If we're on the last chunk we're finished when zstd returns 0,
72              * which means its consumed all the input AND finished the frame.
73              * Otherwise, we're finished when we've consumed all the input.
74              */
75             finished = lastChunk ? (remaining == 0) : (input.pos == input.size);
76         } while (!finished);
77         CHECK(input.pos == input.size,
78               "Impossible: zstd only returns 0 when the input is completely consumed!");
79     }
80 
81     ZSTD_freeCCtx(cctx);
82     fclose_orDie(fout);
83     fclose_orDie(fin);
84     free(buffIn);
85     free(buffOut);
86 }
87 
88 
89 static char* createOutFilename_orDie(const char* filename)
90 {
91     size_t const inL = strlen(filename);
92     size_t const outL = inL + 5;
93     void* const outSpace = malloc_orDie(outL);
94     memset(outSpace, 0, outL);
95     strcat(outSpace, filename);
96     strcat(outSpace, ".zst");
97     return (char*)outSpace;
98 }
99 
100 int main(int argc, const char** argv)
101 {
102     const char* const exeName = argv[0];
103 
104     if (argc!=2) {
105         printf("wrong arguments\n");
106         printf("usage:\n");
107         printf("%s FILE\n", exeName);
108         return 1;
109     }
110 
111     const char* const inFilename = argv[1];
112 
113     char* const outFilename = createOutFilename_orDie(inFilename);
114     compressFile_orDie(inFilename, outFilename, 1);
115 
116     free(outFilename);   /* not strictly required, since program execution stops there,
117                           * but some static analyzer main complain otherwise */
118     return 0;
119 }
120