2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
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.
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()
19 static void compressFile_orDie(const char* fname, const char* outName, int cLevel)
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.
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);
33 /* Create the context. */
34 ZSTD_CCtx* const cctx = ZSTD_createCCtx();
35 CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");
37 /* Set any parameters you want.
38 * Here we set the compression level, and enable the checksum.
40 CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, cLevel) );
41 CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1) );
43 /* This loop read from the input file, compresses that entire chunk,
44 * and writes all output produced to the output file.
46 size_t const toRead = buffInSize;
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.
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
61 ZSTD_inBuffer input = { buffIn, read, 0 };
64 /* Compress into the output buffer and write all of the output to
65 * the file so we can reuse the buffer next iteration.
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.
75 finished = lastChunk ? (remaining == 0) : (input.pos == input.size);
77 CHECK(input.pos == input.size,
78 "Impossible: zstd only returns 0 when the input is completely consumed!");
89 static char* createOutFilename_orDie(const char* filename)
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;
100 int main(int argc, const char** argv)
102 const char* const exeName = argv[0];
105 printf("wrong arguments\n");
107 printf("%s FILE\n", exeName);
111 const char* const inFilename = argv[1];
113 char* const outFilename = createOutFilename_orDie(inFilename);
114 compressFile_orDie(inFilename, outFilename, 1);
116 free(outFilename); /* not strictly required, since program execution stops there,
117 * but some static analyzer main complain otherwise */