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