1*22ce4affSfengbojiang /*
2*22ce4affSfengbojiang  * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3*22ce4affSfengbojiang  * All rights reserved.
4*22ce4affSfengbojiang  *
5*22ce4affSfengbojiang  * This source code is licensed under both the BSD-style license (found in the
6*22ce4affSfengbojiang  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*22ce4affSfengbojiang  * in the COPYING file in the root directory of this source tree).
8*22ce4affSfengbojiang  * You may select, at your option, one of the above-listed licenses.
9*22ce4affSfengbojiang  */
10*22ce4affSfengbojiang 
11*22ce4affSfengbojiang 
12*22ce4affSfengbojiang #include <stdio.h>     // fprintf
13*22ce4affSfengbojiang #include <stdlib.h>    // free
14*22ce4affSfengbojiang #include <zstd.h>      // presumes zstd library is installed
15*22ce4affSfengbojiang #include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()
16*22ce4affSfengbojiang 
decompressFile_orDie(const char * fname)17*22ce4affSfengbojiang static void decompressFile_orDie(const char* fname)
18*22ce4affSfengbojiang {
19*22ce4affSfengbojiang     FILE* const fin  = fopen_orDie(fname, "rb");
20*22ce4affSfengbojiang     size_t const buffInSize = ZSTD_DStreamInSize();
21*22ce4affSfengbojiang     void*  const buffIn  = malloc_orDie(buffInSize);
22*22ce4affSfengbojiang     FILE* const fout = stdout;
23*22ce4affSfengbojiang     size_t const buffOutSize = ZSTD_DStreamOutSize();  /* Guarantee to successfully flush at least one complete compressed block in all circumstances. */
24*22ce4affSfengbojiang     void*  const buffOut = malloc_orDie(buffOutSize);
25*22ce4affSfengbojiang 
26*22ce4affSfengbojiang     ZSTD_DCtx* const dctx = ZSTD_createDCtx();
27*22ce4affSfengbojiang     CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
28*22ce4affSfengbojiang 
29*22ce4affSfengbojiang     /* This loop assumes that the input file is one or more concatenated zstd
30*22ce4affSfengbojiang      * streams. This example won't work if there is trailing non-zstd data at
31*22ce4affSfengbojiang      * the end, but streaming decompression in general handles this case.
32*22ce4affSfengbojiang      * ZSTD_decompressStream() returns 0 exactly when the frame is completed,
33*22ce4affSfengbojiang      * and doesn't consume input after the frame.
34*22ce4affSfengbojiang      */
35*22ce4affSfengbojiang     size_t const toRead = buffInSize;
36*22ce4affSfengbojiang     size_t read;
37*22ce4affSfengbojiang     size_t lastRet = 0;
38*22ce4affSfengbojiang     int isEmpty = 1;
39*22ce4affSfengbojiang     while ( (read = fread_orDie(buffIn, toRead, fin)) ) {
40*22ce4affSfengbojiang         isEmpty = 0;
41*22ce4affSfengbojiang         ZSTD_inBuffer input = { buffIn, read, 0 };
42*22ce4affSfengbojiang         /* Given a valid frame, zstd won't consume the last byte of the frame
43*22ce4affSfengbojiang          * until it has flushed all of the decompressed data of the frame.
44*22ce4affSfengbojiang          * Therefore, instead of checking if the return code is 0, we can
45*22ce4affSfengbojiang          * decompress just check if input.pos < input.size.
46*22ce4affSfengbojiang          */
47*22ce4affSfengbojiang         while (input.pos < input.size) {
48*22ce4affSfengbojiang             ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
49*22ce4affSfengbojiang             /* The return code is zero if the frame is complete, but there may
50*22ce4affSfengbojiang              * be multiple frames concatenated together. Zstd will automatically
51*22ce4affSfengbojiang              * reset the context when a frame is complete. Still, calling
52*22ce4affSfengbojiang              * ZSTD_DCtx_reset() can be useful to reset the context to a clean
53*22ce4affSfengbojiang              * state, for instance if the last decompression call returned an
54*22ce4affSfengbojiang              * error.
55*22ce4affSfengbojiang              */
56*22ce4affSfengbojiang             size_t const ret = ZSTD_decompressStream(dctx, &output , &input);
57*22ce4affSfengbojiang             CHECK_ZSTD(ret);
58*22ce4affSfengbojiang             fwrite_orDie(buffOut, output.pos, fout);
59*22ce4affSfengbojiang             lastRet = ret;
60*22ce4affSfengbojiang         }
61*22ce4affSfengbojiang     }
62*22ce4affSfengbojiang 
63*22ce4affSfengbojiang     if (isEmpty) {
64*22ce4affSfengbojiang         fprintf(stderr, "input is empty\n");
65*22ce4affSfengbojiang         exit(1);
66*22ce4affSfengbojiang     }
67*22ce4affSfengbojiang 
68*22ce4affSfengbojiang     if (lastRet != 0) {
69*22ce4affSfengbojiang         /* The last return value from ZSTD_decompressStream did not end on a
70*22ce4affSfengbojiang          * frame, but we reached the end of the file! We assume this is an
71*22ce4affSfengbojiang          * error, and the input was truncated.
72*22ce4affSfengbojiang          */
73*22ce4affSfengbojiang         fprintf(stderr, "EOF before end of stream: %zu\n", lastRet);
74*22ce4affSfengbojiang         exit(1);
75*22ce4affSfengbojiang     }
76*22ce4affSfengbojiang 
77*22ce4affSfengbojiang     ZSTD_freeDCtx(dctx);
78*22ce4affSfengbojiang     fclose_orDie(fin);
79*22ce4affSfengbojiang     fclose_orDie(fout);
80*22ce4affSfengbojiang     free(buffIn);
81*22ce4affSfengbojiang     free(buffOut);
82*22ce4affSfengbojiang }
83*22ce4affSfengbojiang 
84*22ce4affSfengbojiang 
main(int argc,const char ** argv)85*22ce4affSfengbojiang int main(int argc, const char** argv)
86*22ce4affSfengbojiang {
87*22ce4affSfengbojiang     const char* const exeName = argv[0];
88*22ce4affSfengbojiang 
89*22ce4affSfengbojiang     if (argc!=2) {
90*22ce4affSfengbojiang         fprintf(stderr, "wrong arguments\n");
91*22ce4affSfengbojiang         fprintf(stderr, "usage:\n");
92*22ce4affSfengbojiang         fprintf(stderr, "%s FILE\n", exeName);
93*22ce4affSfengbojiang         return 1;
94*22ce4affSfengbojiang     }
95*22ce4affSfengbojiang 
96*22ce4affSfengbojiang     const char* const inFilename = argv[1];
97*22ce4affSfengbojiang 
98*22ce4affSfengbojiang     decompressFile_orDie(inFilename);
99*22ce4affSfengbojiang     return 0;
100*22ce4affSfengbojiang }
101