2017-08-29 16:24:11 +00:00
|
|
|
/*
|
2020-03-26 22:19:05 +00:00
|
|
|
* Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
|
2016-08-30 17:04:33 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2017-08-29 16:24:11 +00:00
|
|
|
* This source code is licensed under both the BSD-style license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
2017-09-08 07:09:23 +00:00
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
2016-08-30 17:04:33 +00:00
|
|
|
*/
|
2016-08-12 16:42:25 +00:00
|
|
|
|
|
|
|
|
2019-04-06 01:11:17 +00:00
|
|
|
#include <stdio.h> // printf
|
|
|
|
#include <stdlib.h> // free
|
|
|
|
#include <string.h> // memset, strcat, strlen
|
2016-08-12 16:42:25 +00:00
|
|
|
#include <zstd.h> // presumes zstd library is installed
|
2019-04-06 01:11:17 +00:00
|
|
|
#include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()
|
2016-08-12 16:42:25 +00:00
|
|
|
|
2019-03-22 21:57:23 +00:00
|
|
|
|
2016-08-12 16:42:25 +00:00
|
|
|
static void compressFile_orDie(const char* fname, const char* outName, int cLevel)
|
|
|
|
{
|
2019-03-22 21:57:23 +00:00
|
|
|
/* Open the input and output files. */
|
2016-08-12 16:42:25 +00:00
|
|
|
FILE* const fin = fopen_orDie(fname, "rb");
|
|
|
|
FILE* const fout = fopen_orDie(outName, "wb");
|
2019-03-22 21:57:23 +00:00
|
|
|
/* Create the input and output buffers.
|
|
|
|
* They may be any size, but we recommend using these functions to size them.
|
|
|
|
* Performance will only suffer significantly for very tiny buffers.
|
|
|
|
*/
|
|
|
|
size_t const buffInSize = ZSTD_CStreamInSize();
|
2016-08-12 16:42:25 +00:00
|
|
|
void* const buffIn = malloc_orDie(buffInSize);
|
2019-03-22 21:57:23 +00:00
|
|
|
size_t const buffOutSize = ZSTD_CStreamOutSize();
|
2016-08-12 16:42:25 +00:00
|
|
|
void* const buffOut = malloc_orDie(buffOutSize);
|
|
|
|
|
2019-03-22 21:57:23 +00:00
|
|
|
/* Create the context. */
|
|
|
|
ZSTD_CCtx* const cctx = ZSTD_createCCtx();
|
|
|
|
CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");
|
2016-08-12 16:42:25 +00:00
|
|
|
|
2019-03-22 21:57:23 +00:00
|
|
|
/* Set any parameters you want.
|
|
|
|
* Here we set the compression level, and enable the checksum.
|
|
|
|
*/
|
|
|
|
CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, cLevel) );
|
|
|
|
CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1) );
|
2020-07-08 10:09:00 +00:00
|
|
|
ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 4);
|
2019-03-22 21:57:23 +00:00
|
|
|
|
|
|
|
/* This loop read from the input file, compresses that entire chunk,
|
|
|
|
* and writes all output produced to the output file.
|
|
|
|
*/
|
|
|
|
size_t const toRead = buffInSize;
|
2019-09-01 19:35:53 +00:00
|
|
|
for (;;) {
|
|
|
|
size_t read = fread_orDie(buffIn, toRead, fin);
|
2019-03-22 21:57:23 +00:00
|
|
|
/* Select the flush mode.
|
|
|
|
* If the read may not be finished (read == toRead) we use
|
|
|
|
* ZSTD_e_continue. If this is the last chunk, we use ZSTD_e_end.
|
|
|
|
* Zstd optimizes the case where the first flush mode is ZSTD_e_end,
|
|
|
|
* since it knows it is compressing the entire source in one pass.
|
|
|
|
*/
|
|
|
|
int const lastChunk = (read < toRead);
|
|
|
|
ZSTD_EndDirective const mode = lastChunk ? ZSTD_e_end : ZSTD_e_continue;
|
|
|
|
/* Set the input buffer to what we just read.
|
|
|
|
* We compress until the input buffer is empty, each time flushing the
|
|
|
|
* output.
|
|
|
|
*/
|
2016-08-16 23:48:43 +00:00
|
|
|
ZSTD_inBuffer input = { buffIn, read, 0 };
|
2019-03-22 21:57:23 +00:00
|
|
|
int finished;
|
|
|
|
do {
|
|
|
|
/* Compress into the output buffer and write all of the output to
|
|
|
|
* the file so we can reuse the buffer next iteration.
|
|
|
|
*/
|
2016-08-16 23:48:43 +00:00
|
|
|
ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
|
2019-03-22 21:57:23 +00:00
|
|
|
size_t const remaining = ZSTD_compressStream2(cctx, &output , &input, mode);
|
|
|
|
CHECK_ZSTD(remaining);
|
2016-08-16 23:48:43 +00:00
|
|
|
fwrite_orDie(buffOut, output.pos, fout);
|
2019-03-22 21:57:23 +00:00
|
|
|
/* If we're on the last chunk we're finished when zstd returns 0,
|
|
|
|
* which means its consumed all the input AND finished the frame.
|
|
|
|
* Otherwise, we're finished when we've consumed all the input.
|
|
|
|
*/
|
|
|
|
finished = lastChunk ? (remaining == 0) : (input.pos == input.size);
|
|
|
|
} while (!finished);
|
2019-04-06 01:11:17 +00:00
|
|
|
CHECK(input.pos == input.size,
|
|
|
|
"Impossible: zstd only returns 0 when the input is completely consumed!");
|
2019-09-01 19:35:53 +00:00
|
|
|
|
|
|
|
if (lastChunk) {
|
|
|
|
break;
|
|
|
|
}
|
2016-08-12 16:42:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 21:57:23 +00:00
|
|
|
ZSTD_freeCCtx(cctx);
|
2016-08-12 16:42:25 +00:00
|
|
|
fclose_orDie(fout);
|
2019-03-22 21:57:23 +00:00
|
|
|
fclose_orDie(fin);
|
|
|
|
free(buffIn);
|
2016-08-12 16:42:25 +00:00
|
|
|
free(buffOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-16 21:40:47 +00:00
|
|
|
static char* createOutFilename_orDie(const char* filename)
|
2016-08-12 16:42:25 +00:00
|
|
|
{
|
|
|
|
size_t const inL = strlen(filename);
|
|
|
|
size_t const outL = inL + 5;
|
2018-08-16 21:40:47 +00:00
|
|
|
void* const outSpace = malloc_orDie(outL);
|
2016-08-12 16:42:25 +00:00
|
|
|
memset(outSpace, 0, outL);
|
|
|
|
strcat(outSpace, filename);
|
|
|
|
strcat(outSpace, ".zst");
|
2018-08-16 21:40:47 +00:00
|
|
|
return (char*)outSpace;
|
2016-08-12 16:42:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char** argv)
|
|
|
|
{
|
|
|
|
const char* const exeName = argv[0];
|
|
|
|
|
|
|
|
if (argc!=2) {
|
|
|
|
printf("wrong arguments\n");
|
|
|
|
printf("usage:\n");
|
|
|
|
printf("%s FILE\n", exeName);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-26 10:04:04 +00:00
|
|
|
const char* const inFilename = argv[1];
|
|
|
|
|
2018-08-16 21:40:47 +00:00
|
|
|
char* const outFilename = createOutFilename_orDie(inFilename);
|
2016-08-12 16:42:25 +00:00
|
|
|
compressFile_orDie(inFilename, outFilename, 1);
|
|
|
|
|
2018-08-16 21:40:47 +00:00
|
|
|
free(outFilename); /* not strictly required, since program execution stops there,
|
|
|
|
* but some static analyzer main complain otherwise */
|
2016-08-12 16:42:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|