648db22b |
1 | /* |
2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | * All rights reserved. |
4 | * |
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. |
9 | */ |
10 | |
11 | |
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() |
17 | |
18 | static void compressFile_orDie(const char* fname, const char* outName, int cLevel, |
19 | int nbThreads) |
20 | { |
21 | fprintf (stderr, "Starting compression of %s with level %d, using %d threads\n", |
22 | fname, cLevel, nbThreads); |
23 | |
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. |
30 | */ |
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); |
35 | |
36 | /* Create the context. */ |
37 | ZSTD_CCtx* const cctx = ZSTD_createCCtx(); |
38 | CHECK(cctx != NULL, "ZSTD_createCCtx() failed!"); |
39 | |
40 | /* 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) ); |
f535537f |
45 | 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 | } |
648db22b |
52 | |
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; |
57 | for (;;) { |
58 | size_t read = fread_orDie(buffIn, toRead, fin); |
59 | /* 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 | */ |
71 | ZSTD_inBuffer input = { buffIn, read, 0 }; |
72 | 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 | */ |
77 | ZSTD_outBuffer output = { buffOut, buffOutSize, 0 }; |
78 | size_t const remaining = ZSTD_compressStream2(cctx, &output , &input, mode); |
79 | CHECK_ZSTD(remaining); |
80 | fwrite_orDie(buffOut, output.pos, fout); |
81 | /* 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); |
87 | CHECK(input.pos == input.size, |
88 | "Impossible: zstd only returns 0 when the input is completely consumed!"); |
89 | |
90 | if (lastChunk) { |
91 | break; |
92 | } |
93 | } |
94 | |
95 | ZSTD_freeCCtx(cctx); |
96 | fclose_orDie(fout); |
97 | fclose_orDie(fin); |
98 | free(buffIn); |
99 | free(buffOut); |
100 | } |
101 | |
102 | |
103 | static char* createOutFilename_orDie(const char* filename) |
104 | { |
105 | size_t const inL = strlen(filename); |
106 | size_t const outL = inL + 5; |
107 | void* const outSpace = malloc_orDie(outL); |
108 | memset(outSpace, 0, outL); |
109 | strcat(outSpace, filename); |
110 | strcat(outSpace, ".zst"); |
111 | return (char*)outSpace; |
112 | } |
113 | |
114 | int main(int argc, const char** argv) |
115 | { |
116 | const char* const exeName = argv[0]; |
117 | |
118 | if (argc < 2) { |
119 | printf("wrong arguments\n"); |
120 | printf("usage:\n"); |
121 | printf("%s FILE [LEVEL] [THREADS]\n", exeName); |
122 | return 1; |
123 | } |
124 | |
125 | int cLevel = 1; |
f535537f |
126 | int nbThreads = 1; |
648db22b |
127 | |
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 | |
138 | const char* const inFilename = argv[1]; |
139 | |
140 | char* const outFilename = createOutFilename_orDie(inFilename); |
141 | compressFile_orDie(inFilename, outFilename, cLevel, nbThreads); |
142 | |
143 | free(outFilename); /* not strictly required, since program execution stops there, |
144 | * but some static analyzer may complain otherwise */ |
145 | return 0; |
146 | } |