2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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()
18 static void compressFile_orDie(const char* fname, const char* outName, int cLevel,
21 fprintf (stderr, "Starting compression of %s with level %d, using %d threads\n",
22 fname, cLevel, nbThreads);
24 /* Open the input and output files. */
25 FILE* const fin = fopen_orDie(fname, "rb");
26 FILE* const fout = fopen_orDie(outName, "wb");
27 /* Create the input and output buffers.
28 * They may be any size, but we recommend using these functions to size them.
29 * Performance will only suffer significantly for very tiny buffers.
31 size_t const buffInSize = ZSTD_CStreamInSize();
32 void* const buffIn = malloc_orDie(buffInSize);
33 size_t const buffOutSize = ZSTD_CStreamOutSize();
34 void* const buffOut = malloc_orDie(buffOutSize);
36 /* Create the context. */
37 ZSTD_CCtx* const cctx = ZSTD_createCCtx();
38 CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");
40 /* Set any parameters you want.
41 * Here we set the compression level, and enable the checksum.
43 CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, cLevel) );
44 CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1) );
45 ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, nbThreads);
47 /* This loop read from the input file, compresses that entire chunk,
48 * and writes all output produced to the output file.
50 size_t const toRead = buffInSize;
52 size_t read = fread_orDie(buffIn, toRead, fin);
53 /* Select the flush mode.
54 * If the read may not be finished (read == toRead) we use
55 * ZSTD_e_continue. If this is the last chunk, we use ZSTD_e_end.
56 * Zstd optimizes the case where the first flush mode is ZSTD_e_end,
57 * since it knows it is compressing the entire source in one pass.
59 int const lastChunk = (read < toRead);
60 ZSTD_EndDirective const mode = lastChunk ? ZSTD_e_end : ZSTD_e_continue;
61 /* Set the input buffer to what we just read.
62 * We compress until the input buffer is empty, each time flushing the
65 ZSTD_inBuffer input = { buffIn, read, 0 };
68 /* Compress into the output buffer and write all of the output to
69 * the file so we can reuse the buffer next iteration.
71 ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
72 size_t const remaining = ZSTD_compressStream2(cctx, &output , &input, mode);
73 CHECK_ZSTD(remaining);
74 fwrite_orDie(buffOut, output.pos, fout);
75 /* If we're on the last chunk we're finished when zstd returns 0,
76 * which means its consumed all the input AND finished the frame.
77 * Otherwise, we're finished when we've consumed all the input.
79 finished = lastChunk ? (remaining == 0) : (input.pos == input.size);
81 CHECK(input.pos == input.size,
82 "Impossible: zstd only returns 0 when the input is completely consumed!");
97 static char* createOutFilename_orDie(const char* filename)
99 size_t const inL = strlen(filename);
100 size_t const outL = inL + 5;
101 void* const outSpace = malloc_orDie(outL);
102 memset(outSpace, 0, outL);
103 strcat(outSpace, filename);
104 strcat(outSpace, ".zst");
105 return (char*)outSpace;
108 int main(int argc, const char** argv)
110 const char* const exeName = argv[0];
113 printf("wrong arguments\n");
115 printf("%s FILE [LEVEL] [THREADS]\n", exeName);
123 cLevel = atoi (argv[2]);
124 CHECK(cLevel != 0, "can't parse LEVEL!");
128 nbThreads = atoi (argv[3]);
129 CHECK(nbThreads != 0, "can't parse THREADS!");
132 const char* const inFilename = argv[1];
134 char* const outFilename = createOutFilename_orDie(inFilename);
135 compressFile_orDie(inFilename, outFilename, cLevel, nbThreads);
137 free(outFilename); /* not strictly required, since program execution stops there,
138 * but some static analyzer may complain otherwise */