2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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.
12 #include <stdio.h> // fprintf
13 #include <stdlib.h> // free
14 #include <zstd.h> // presumes zstd library is installed
15 #include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()
17 static void decompressFile_orDie(const char* fname)
19 FILE* const fin = fopen_orDie(fname, "rb");
20 size_t const buffInSize = ZSTD_DStreamInSize();
21 void* const buffIn = malloc_orDie(buffInSize);
22 FILE* const fout = stdout;
23 size_t const buffOutSize = ZSTD_DStreamOutSize(); /* Guarantee to successfully flush at least one complete compressed block in all circumstances. */
24 void* const buffOut = malloc_orDie(buffOutSize);
26 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
27 CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
29 /* This loop assumes that the input file is one or more concatenated zstd
30 * streams. This example won't work if there is trailing non-zstd data at
31 * the end, but streaming decompression in general handles this case.
32 * ZSTD_decompressStream() returns 0 exactly when the frame is completed,
33 * and doesn't consume input after the frame.
35 size_t const toRead = buffInSize;
39 while ( (read = fread_orDie(buffIn, toRead, fin)) ) {
41 ZSTD_inBuffer input = { buffIn, read, 0 };
42 /* Given a valid frame, zstd won't consume the last byte of the frame
43 * until it has flushed all of the decompressed data of the frame.
44 * Therefore, instead of checking if the return code is 0, we can
45 * decompress just check if input.pos < input.size.
47 while (input.pos < input.size) {
48 ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
49 /* The return code is zero if the frame is complete, but there may
50 * be multiple frames concatenated together. Zstd will automatically
51 * reset the context when a frame is complete. Still, calling
52 * ZSTD_DCtx_reset() can be useful to reset the context to a clean
53 * state, for instance if the last decompression call returned an
56 size_t const ret = ZSTD_decompressStream(dctx, &output , &input);
58 fwrite_orDie(buffOut, output.pos, fout);
64 fprintf(stderr, "input is empty\n");
69 /* The last return value from ZSTD_decompressStream did not end on a
70 * frame, but we reached the end of the file! We assume this is an
71 * error, and the input was truncated.
73 fprintf(stderr, "EOF before end of stream: %zu\n", lastRet);
85 int main(int argc, const char** argv)
87 const char* const exeName = argv[0];
90 fprintf(stderr, "wrong arguments\n");
91 fprintf(stderr, "usage:\n");
92 fprintf(stderr, "%s FILE\n", exeName);
96 const char* const inFilename = argv[1];
98 decompressFile_orDie(inFilename);