2017-08-29 16:24:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016-present, 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:56:27 +00:00
|
|
|
|
|
|
|
|
2019-04-06 01:11:17 +00:00
|
|
|
#include <stdio.h> // fprintf
|
|
|
|
#include <stdlib.h> // free
|
2016-08-12 16:56:27 +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:56:27 +00:00
|
|
|
|
|
|
|
static void decompressFile_orDie(const char* fname)
|
|
|
|
{
|
|
|
|
FILE* const fin = fopen_orDie(fname, "rb");
|
2016-09-08 17:29:04 +00:00
|
|
|
size_t const buffInSize = ZSTD_DStreamInSize();
|
2016-08-12 16:56:27 +00:00
|
|
|
void* const buffIn = malloc_orDie(buffInSize);
|
2016-09-08 17:48:04 +00:00
|
|
|
FILE* const fout = stdout;
|
2016-09-08 17:29:04 +00:00
|
|
|
size_t const buffOutSize = ZSTD_DStreamOutSize(); /* Guarantee to successfully flush at least one complete compressed block in all circumstances. */
|
2016-08-12 16:56:27 +00:00
|
|
|
void* const buffOut = malloc_orDie(buffOutSize);
|
|
|
|
|
2019-04-02 00:25:26 +00:00
|
|
|
ZSTD_DCtx* const dctx = ZSTD_createDCtx();
|
|
|
|
CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
|
2016-10-28 20:58:31 +00:00
|
|
|
|
2019-04-02 00:25:26 +00:00
|
|
|
/* This loop assumes that the input file is one or more concatenated zstd
|
|
|
|
* streams. This example won't work if there is trailing non-zstd data at
|
|
|
|
* the end, but streaming decompression in general handles this case.
|
|
|
|
* ZSTD_decompressStream() returns 0 exactly when the frame is completed,
|
|
|
|
* and doesn't consume input after the frame.
|
|
|
|
*/
|
|
|
|
size_t const toRead = buffInSize;
|
|
|
|
size_t read;
|
2016-09-08 17:29:04 +00:00
|
|
|
while ( (read = fread_orDie(buffIn, toRead, fin)) ) {
|
2016-08-16 23:48:43 +00:00
|
|
|
ZSTD_inBuffer input = { buffIn, read, 0 };
|
2019-04-02 00:25:26 +00:00
|
|
|
/* Given a valid frame, zstd won't consume the last byte of the frame
|
|
|
|
* until it has flushed all of the decompressed data of the frame.
|
|
|
|
* Therefore, instead of checking if the return code is 0, we can
|
|
|
|
* decompress just check if input.pos < input.size.
|
|
|
|
*/
|
2016-08-16 23:48:43 +00:00
|
|
|
while (input.pos < input.size) {
|
|
|
|
ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
|
2019-04-02 00:25:26 +00:00
|
|
|
/* The return code is zero if the frame is complete, but there may
|
|
|
|
* be multiple frames concatenated together. Zstd will automatically
|
|
|
|
* reset the context when a frame is complete. Still, calling
|
|
|
|
* ZSTD_DCtx_reset() can be useful to reset the context to a clean
|
|
|
|
* state, for instance if the last decompression call returned an
|
|
|
|
* error.
|
|
|
|
*/
|
|
|
|
size_t const ret = ZSTD_decompressStream(dctx, &output , &input);
|
|
|
|
CHECK_ZSTD(ret);
|
2016-09-08 17:29:04 +00:00
|
|
|
fwrite_orDie(buffOut, output.pos, fout);
|
2016-08-12 16:56:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 00:25:26 +00:00
|
|
|
ZSTD_freeDCtx(dctx);
|
2016-08-12 16:56:27 +00:00
|
|
|
fclose_orDie(fin);
|
2016-09-08 17:29:04 +00:00
|
|
|
fclose_orDie(fout);
|
2016-08-12 16:56:27 +00:00
|
|
|
free(buffIn);
|
|
|
|
free(buffOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, const char** argv)
|
|
|
|
{
|
|
|
|
const char* const exeName = argv[0];
|
|
|
|
|
|
|
|
if (argc!=2) {
|
2016-09-08 17:39:00 +00:00
|
|
|
fprintf(stderr, "wrong arguments\n");
|
|
|
|
fprintf(stderr, "usage:\n");
|
|
|
|
fprintf(stderr, "%s FILE\n", exeName);
|
2016-08-12 16:56:27 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-26 10:04:04 +00:00
|
|
|
const char* const inFilename = argv[1];
|
|
|
|
|
2016-08-12 16:56:27 +00:00
|
|
|
decompressFile_orDie(inFilename);
|
|
|
|
return 0;
|
|
|
|
}
|