This function compresses the input file using the Zstandard (zstd) algorithm and writes the compressed data to the output file.
229 size_t const buffInSize = ZSTD_CStreamInSize();
230 void*
const buffIn = malloc(buffInSize);
231 size_t const buffOutSize = ZSTD_CStreamOutSize();
232 void*
const buffOut = malloc(buffOutSize);
234 fseek(_infile, 0, SEEK_END);
235 size_t _total_size = ftell(_infile);
236 fseek(_infile, 0, SEEK_SET);
238 uint32_t _crc32 = crc32(0L, Z_NULL, 0);
241 ZSTD_CCtx*
const cctx = ZSTD_createCCtx();
243 uint32_t thread_count = get_num_threads();
247 ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1);
248 ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, 1);
249 ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 27);
250 ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, thread_count);
251 ZSTD_CCtx_setParameter(cctx, ZSTD_c_jobSize, buffInSize * thread_count);
256 size_t _total_readed = 0ull;
257 size_t const toRead = buffInSize;
260 size_t read = _freadb(buffIn, 1, toRead, _infile);
261 _total_readed += read;
262 _crc32 = crc32(_crc32, buffIn, read);
265 int const lastChunk = (read < toRead);
266 ZSTD_EndDirective
const mode = lastChunk ? ZSTD_e_end : ZSTD_e_continue;
268 ZSTD_inBuffer input = { buffIn, read, 0 };
272 ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
273 size_t const remaining = ZSTD_compressStream2(cctx, &output, &input, mode);
275 _fwriteb(buffOut, 1, output.pos, _outfile);
276 finished = lastChunk ? (remaining == 0) : (input.pos == input.size);
278 assert(input.pos == input.size &&
"Impossible: zstd only returns 0 when the input is completely consumed!");