blob: ed0a3a69cd9afaa57ec8eb6c6846886f42390179 [file] [log] [blame]
Yann Collet394bdd72017-08-29 09:24:11 -07001/*
Elliott Hughes44aba642023-09-12 20:18:59 +00002 * 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) );
Martin Liska926d4702021-10-04 08:23:57 +020045 ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, nbThreads);
Nick Terrellf5cbee92019-03-22 14:57:23 -070046
47 /* This loop read from the input file, compresses that entire chunk,
48 * and writes all output produced to the output file.
49 */
50 size_t const toRead = buffInSize;
Jan Kasiaka8219902019-09-01 15:35:53 -040051 for (;;) {
52 size_t read = fread_orDie(buffIn, toRead, fin);
Nick Terrellf5cbee92019-03-22 14:57:23 -070053 /* 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.
58 */
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
63 * output.
64 */
Yann Collet20658792016-08-17 01:48:43 +020065 ZSTD_inBuffer input = { buffIn, read, 0 };
Nick Terrellf5cbee92019-03-22 14:57:23 -070066 int finished;
67 do {
68 /* Compress into the output buffer and write all of the output to
69 * the file so we can reuse the buffer next iteration.
70 */
Yann Collet20658792016-08-17 01:48:43 +020071 ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
Nick Terrellf5cbee92019-03-22 14:57:23 -070072 size_t const remaining = ZSTD_compressStream2(cctx, &output , &input, mode);
73 CHECK_ZSTD(remaining);
Yann Collet20658792016-08-17 01:48:43 +020074 fwrite_orDie(buffOut, output.pos, fout);
Nick Terrellf5cbee92019-03-22 14:57:23 -070075 /* 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.
78 */
79 finished = lastChunk ? (remaining == 0) : (input.pos == input.size);
80 } while (!finished);
Nick Terrell1d0c1702019-04-05 18:11:17 -070081 CHECK(input.pos == input.size,
82 "Impossible: zstd only returns 0 when the input is completely consumed!");
Jan Kasiaka8219902019-09-01 15:35:53 -040083
84 if (lastChunk) {
85 break;
86 }
Yann Collet553b2132016-08-12 18:42:25 +020087 }
88
Nick Terrellf5cbee92019-03-22 14:57:23 -070089 ZSTD_freeCCtx(cctx);
Yann Collet553b2132016-08-12 18:42:25 +020090 fclose_orDie(fout);
Nick Terrellf5cbee92019-03-22 14:57:23 -070091 fclose_orDie(fin);
92 free(buffIn);
Yann Collet553b2132016-08-12 18:42:25 +020093 free(buffOut);
94}
95
96
Yann Collet1515f0b2018-08-16 14:40:47 -070097static char* createOutFilename_orDie(const char* filename)
Yann Collet553b2132016-08-12 18:42:25 +020098{
99 size_t const inL = strlen(filename);
100 size_t const outL = inL + 5;
Yann Collet1515f0b2018-08-16 14:40:47 -0700101 void* const outSpace = malloc_orDie(outL);
Yann Collet553b2132016-08-12 18:42:25 +0200102 memset(outSpace, 0, outL);
103 strcat(outSpace, filename);
104 strcat(outSpace, ".zst");
Yann Collet1515f0b2018-08-16 14:40:47 -0700105 return (char*)outSpace;
Yann Collet553b2132016-08-12 18:42:25 +0200106}
107
108int main(int argc, const char** argv)
109{
110 const char* const exeName = argv[0];
Yann Collet553b2132016-08-12 18:42:25 +0200111
Martin Liska926d4702021-10-04 08:23:57 +0200112 if (argc < 2) {
Yann Collet553b2132016-08-12 18:42:25 +0200113 printf("wrong arguments\n");
114 printf("usage:\n");
Martin Liska926d4702021-10-04 08:23:57 +0200115 printf("%s FILE [LEVEL] [THREADS]\n", exeName);
Yann Collet553b2132016-08-12 18:42:25 +0200116 return 1;
117 }
118
Martin Liska926d4702021-10-04 08:23:57 +0200119 int cLevel = 1;
120 int nbThreads = 4;
121
122 if (argc >= 3) {
123 cLevel = atoi (argv[2]);
124 CHECK(cLevel != 0, "can't parse LEVEL!");
125 }
126
127 if (argc >= 4) {
128 nbThreads = atoi (argv[3]);
129 CHECK(nbThreads != 0, "can't parse THREADS!");
130 }
131
niXman65e2cda2017-04-26 13:04:04 +0300132 const char* const inFilename = argv[1];
133
Yann Collet1515f0b2018-08-16 14:40:47 -0700134 char* const outFilename = createOutFilename_orDie(inFilename);
Martin Liska926d4702021-10-04 08:23:57 +0200135 compressFile_orDie(inFilename, outFilename, cLevel, nbThreads);
Yann Collet553b2132016-08-12 18:42:25 +0200136
Yann Collet1515f0b2018-08-16 14:40:47 -0700137 free(outFilename); /* not strictly required, since program execution stops there,
Adrian Castroe0f9dc02021-12-11 12:02:23 +0100138 * but some static analyzer may complain otherwise */
Yann Collet553b2132016-08-12 18:42:25 +0200139 return 0;
140}