blob: 063aa82a2946a8fa384cb9a0a30d9f21afaeacf8 [file] [log] [blame]
Yann Collet394bdd72017-08-29 09:24:11 -07001/*
W. Felix Handte5d693cc2022-12-20 12:49:47 -05002 * Copyright (c) Meta Platforms, Inc. and affiliates.
Yann Collet4ded9e52016-08-30 10:04:33 -07003 * All rights reserved.
4 *
Yann Collet394bdd72017-08-29 09:24:11 -07005 * 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).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Yann Collet4ded9e52016-08-30 10:04:33 -07009 */
Yann Collet553b2132016-08-12 18:42:25 +020010
Yann Collet553b2132016-08-12 18:42:25 +020011
Nick Terrell1d0c1702019-04-05 18:11:17 -070012#include <stdio.h> // printf
13#include <stdlib.h> // free
14#include <string.h> // memset, strcat, strlen
Yann Collet553b2132016-08-12 18:42:25 +020015#include <zstd.h> // presumes zstd library is installed
Nick Terrell1d0c1702019-04-05 18:11:17 -070016#include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()
Yann Collet553b2132016-08-12 18:42:25 +020017
Martin Liska926d4702021-10-04 08:23:57 +020018static void compressFile_orDie(const char* fname, const char* outName, int cLevel,
19 int nbThreads)
Yann Collet553b2132016-08-12 18:42:25 +020020{
Martin Liska926d4702021-10-04 08:23:57 +020021 fprintf (stderr, "Starting compression of %s with level %d, using %d threads\n",
22 fname, cLevel, nbThreads);
23
Nick Terrellf5cbee92019-03-22 14:57:23 -070024 /* Open the input and output files. */
Yann Collet553b2132016-08-12 18:42:25 +020025 FILE* const fin = fopen_orDie(fname, "rb");
26 FILE* const fout = fopen_orDie(outName, "wb");
Nick Terrellf5cbee92019-03-22 14:57:23 -070027 /* 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.
30 */
31 size_t const buffInSize = ZSTD_CStreamInSize();
Yann Collet553b2132016-08-12 18:42:25 +020032 void* const buffIn = malloc_orDie(buffInSize);
Nick Terrellf5cbee92019-03-22 14:57:23 -070033 size_t const buffOutSize = ZSTD_CStreamOutSize();
Yann Collet553b2132016-08-12 18:42:25 +020034 void* const buffOut = malloc_orDie(buffOutSize);
Yann Collet553b2132016-08-12 18:42:25 +020035
Nick Terrellf5cbee92019-03-22 14:57:23 -070036 /* Create the context. */
37 ZSTD_CCtx* const cctx = ZSTD_createCCtx();
38 CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");
Yann Collet553b2132016-08-12 18:42:25 +020039
Nick Terrellf5cbee92019-03-22 14:57:23 -070040 /* Set any parameters you want.
41 * Here we set the compression level, and enable the checksum.
42 */
43 CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, cLevel) );
44 CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1) );
Yann Collet6ec18ae2023-04-26 12:45:23 -070045 if (nbThreads > 1) {
46 size_t const r = ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, nbThreads);
47 if (ZSTD_isError(r)) {
48 fprintf (stderr, "Note: the linked libzstd library doesn't support multithreading. "
49 "Reverting to single-thread mode. \n");
50 }
51 }
Nick Terrellf5cbee92019-03-22 14:57:23 -070052
53 /* This loop read from the input file, compresses that entire chunk,
54 * and writes all output produced to the output file.
55 */
56 size_t const toRead = buffInSize;
Jan Kasiaka8219902019-09-01 15:35:53 -040057 for (;;) {
58 size_t read = fread_orDie(buffIn, toRead, fin);
Nick Terrellf5cbee92019-03-22 14:57:23 -070059 /* Select the flush mode.
60 * If the read may not be finished (read == toRead) we use
61 * ZSTD_e_continue. If this is the last chunk, we use ZSTD_e_end.
62 * Zstd optimizes the case where the first flush mode is ZSTD_e_end,
63 * since it knows it is compressing the entire source in one pass.
64 */
65 int const lastChunk = (read < toRead);
66 ZSTD_EndDirective const mode = lastChunk ? ZSTD_e_end : ZSTD_e_continue;
67 /* Set the input buffer to what we just read.
68 * We compress until the input buffer is empty, each time flushing the
69 * output.
70 */
Yann Collet20658792016-08-17 01:48:43 +020071 ZSTD_inBuffer input = { buffIn, read, 0 };
Nick Terrellf5cbee92019-03-22 14:57:23 -070072 int finished;
73 do {
74 /* Compress into the output buffer and write all of the output to
75 * the file so we can reuse the buffer next iteration.
76 */
Yann Collet20658792016-08-17 01:48:43 +020077 ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
Nick Terrellf5cbee92019-03-22 14:57:23 -070078 size_t const remaining = ZSTD_compressStream2(cctx, &output , &input, mode);
79 CHECK_ZSTD(remaining);
Yann Collet20658792016-08-17 01:48:43 +020080 fwrite_orDie(buffOut, output.pos, fout);
Nick Terrellf5cbee92019-03-22 14:57:23 -070081 /* If we're on the last chunk we're finished when zstd returns 0,
82 * which means its consumed all the input AND finished the frame.
83 * Otherwise, we're finished when we've consumed all the input.
84 */
85 finished = lastChunk ? (remaining == 0) : (input.pos == input.size);
86 } while (!finished);
Nick Terrell1d0c1702019-04-05 18:11:17 -070087 CHECK(input.pos == input.size,
88 "Impossible: zstd only returns 0 when the input is completely consumed!");
Jan Kasiaka8219902019-09-01 15:35:53 -040089
90 if (lastChunk) {
91 break;
92 }
Yann Collet553b2132016-08-12 18:42:25 +020093 }
94
Nick Terrellf5cbee92019-03-22 14:57:23 -070095 ZSTD_freeCCtx(cctx);
Yann Collet553b2132016-08-12 18:42:25 +020096 fclose_orDie(fout);
Nick Terrellf5cbee92019-03-22 14:57:23 -070097 fclose_orDie(fin);
98 free(buffIn);
Yann Collet553b2132016-08-12 18:42:25 +020099 free(buffOut);
100}
101
102
Yann Collet1515f0b2018-08-16 14:40:47 -0700103static char* createOutFilename_orDie(const char* filename)
Yann Collet553b2132016-08-12 18:42:25 +0200104{
105 size_t const inL = strlen(filename);
106 size_t const outL = inL + 5;
Yann Collet1515f0b2018-08-16 14:40:47 -0700107 void* const outSpace = malloc_orDie(outL);
Yann Collet553b2132016-08-12 18:42:25 +0200108 memset(outSpace, 0, outL);
109 strcat(outSpace, filename);
110 strcat(outSpace, ".zst");
Yann Collet1515f0b2018-08-16 14:40:47 -0700111 return (char*)outSpace;
Yann Collet553b2132016-08-12 18:42:25 +0200112}
113
114int main(int argc, const char** argv)
115{
116 const char* const exeName = argv[0];
Yann Collet553b2132016-08-12 18:42:25 +0200117
Martin Liska926d4702021-10-04 08:23:57 +0200118 if (argc < 2) {
Yann Collet553b2132016-08-12 18:42:25 +0200119 printf("wrong arguments\n");
120 printf("usage:\n");
Martin Liska926d4702021-10-04 08:23:57 +0200121 printf("%s FILE [LEVEL] [THREADS]\n", exeName);
Yann Collet553b2132016-08-12 18:42:25 +0200122 return 1;
123 }
124
Martin Liska926d4702021-10-04 08:23:57 +0200125 int cLevel = 1;
Yann Collet6ec18ae2023-04-26 12:45:23 -0700126 int nbThreads = 1;
Martin Liska926d4702021-10-04 08:23:57 +0200127
128 if (argc >= 3) {
129 cLevel = atoi (argv[2]);
130 CHECK(cLevel != 0, "can't parse LEVEL!");
131 }
132
133 if (argc >= 4) {
134 nbThreads = atoi (argv[3]);
135 CHECK(nbThreads != 0, "can't parse THREADS!");
136 }
137
niXman65e2cda2017-04-26 13:04:04 +0300138 const char* const inFilename = argv[1];
139
Yann Collet1515f0b2018-08-16 14:40:47 -0700140 char* const outFilename = createOutFilename_orDie(inFilename);
Martin Liska926d4702021-10-04 08:23:57 +0200141 compressFile_orDie(inFilename, outFilename, cLevel, nbThreads);
Yann Collet553b2132016-08-12 18:42:25 +0200142
Yann Collet1515f0b2018-08-16 14:40:47 -0700143 free(outFilename); /* not strictly required, since program execution stops there,
Adrian Castroe0f9dc02021-12-11 12:02:23 +0100144 * but some static analyzer may complain otherwise */
Yann Collet553b2132016-08-12 18:42:25 +0200145 return 0;
146}