blob: 26eda3441b7fb6453495f15094568350b974102e [file] [log] [blame]
Yann Collet394bdd72017-08-29 09:24:11 -07001/*
Nick Terrellac58c8d2020-03-26 15:19:05 -07002 * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
Yann Collet4ded9e52016-08-30 10:04:33 -07003 * All rights reserved.
4 *
Yann Collet394bdd72017-08-29 09:24:11 -07005 * 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).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Yann Collet4ded9e52016-08-30 10:04:33 -07009 */
Yann Collet9f9f1fc2016-08-12 18:56:27 +020010
Yann Collet9f9f1fc2016-08-12 18:56:27 +020011
Nick Terrell1d0c1702019-04-05 18:11:17 -070012#include <stdio.h> // fprintf
13#include <stdlib.h> // free
Yann Collet9f9f1fc2016-08-12 18:56:27 +020014#include <zstd.h> // presumes zstd library is installed
Nick Terrell1d0c1702019-04-05 18:11:17 -070015#include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()
Yann Collet9f9f1fc2016-08-12 18:56:27 +020016
17static void decompressFile_orDie(const char* fname)
18{
19 FILE* const fin = fopen_orDie(fname, "rb");
Yann Collet01c19922016-09-08 19:29:04 +020020 size_t const buffInSize = ZSTD_DStreamInSize();
Yann Collet9f9f1fc2016-08-12 18:56:27 +020021 void* const buffIn = malloc_orDie(buffInSize);
Yann Colletb94fcc82016-09-08 19:48:04 +020022 FILE* const fout = stdout;
Yann Collet01c19922016-09-08 19:29:04 +020023 size_t const buffOutSize = ZSTD_DStreamOutSize(); /* Guarantee to successfully flush at least one complete compressed block in all circumstances. */
Yann Collet9f9f1fc2016-08-12 18:56:27 +020024 void* const buffOut = malloc_orDie(buffOutSize);
Yann Collet9f9f1fc2016-08-12 18:56:27 +020025
Nick Terrellde589102019-04-01 17:25:26 -070026 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
27 CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
Yann Collet22de81e2016-10-28 13:58:31 -070028
Nick Terrellde589102019-04-01 17:25:26 -070029 /* 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.
34 */
35 size_t const toRead = buffInSize;
36 size_t read;
Jan Kasiaka8219902019-09-01 15:35:53 -040037 size_t lastRet = 0;
38 int isEmpty = 1;
Yann Collet01c19922016-09-08 19:29:04 +020039 while ( (read = fread_orDie(buffIn, toRead, fin)) ) {
Jan Kasiaka8219902019-09-01 15:35:53 -040040 isEmpty = 0;
Yann Collet20658792016-08-17 01:48:43 +020041 ZSTD_inBuffer input = { buffIn, read, 0 };
Nick Terrellde589102019-04-01 17:25:26 -070042 /* 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.
46 */
Yann Collet20658792016-08-17 01:48:43 +020047 while (input.pos < input.size) {
48 ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
Nick Terrellde589102019-04-01 17:25:26 -070049 /* 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
54 * error.
55 */
56 size_t const ret = ZSTD_decompressStream(dctx, &output , &input);
57 CHECK_ZSTD(ret);
Yann Collet01c19922016-09-08 19:29:04 +020058 fwrite_orDie(buffOut, output.pos, fout);
Jan Kasiaka8219902019-09-01 15:35:53 -040059 lastRet = ret;
Yann Collet9f9f1fc2016-08-12 18:56:27 +020060 }
61 }
62
Jan Kasiaka8219902019-09-01 15:35:53 -040063 if (isEmpty) {
64 fprintf(stderr, "input is empty\n");
65 exit(1);
66 }
67
68 if (lastRet != 0) {
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.
72 */
73 fprintf(stderr, "EOF before end of stream: %zu\n", lastRet);
74 exit(1);
75 }
76
Nick Terrellde589102019-04-01 17:25:26 -070077 ZSTD_freeDCtx(dctx);
Yann Collet9f9f1fc2016-08-12 18:56:27 +020078 fclose_orDie(fin);
Yann Collet01c19922016-09-08 19:29:04 +020079 fclose_orDie(fout);
Yann Collet9f9f1fc2016-08-12 18:56:27 +020080 free(buffIn);
81 free(buffOut);
82}
83
84
85int main(int argc, const char** argv)
86{
87 const char* const exeName = argv[0];
Yann Collet9f9f1fc2016-08-12 18:56:27 +020088
89 if (argc!=2) {
Yann Collet264c7332016-09-08 19:39:00 +020090 fprintf(stderr, "wrong arguments\n");
91 fprintf(stderr, "usage:\n");
92 fprintf(stderr, "%s FILE\n", exeName);
Yann Collet9f9f1fc2016-08-12 18:56:27 +020093 return 1;
94 }
95
niXman65e2cda2017-04-26 13:04:04 +030096 const char* const inFilename = argv[1];
97
Yann Collet9f9f1fc2016-08-12 18:56:27 +020098 decompressFile_orDie(inFilename);
Yann Collet9f9f1fc2016-08-12 18:56:27 +020099 return 0;
100}