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 #include <stdio.h>     // printf
12*22ce4affSfengbojiang #include <stdlib.h>    // free
13*22ce4affSfengbojiang #include <zstd.h>      // presumes zstd library is installed
14*22ce4affSfengbojiang #include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()
15*22ce4affSfengbojiang 
decompress(const char * fname)16*22ce4affSfengbojiang static void decompress(const char* fname)
17*22ce4affSfengbojiang {
18*22ce4affSfengbojiang     size_t cSize;
19*22ce4affSfengbojiang     void* const cBuff = mallocAndLoadFile_orDie(fname, &cSize);
20*22ce4affSfengbojiang     /* Read the content size from the frame header. For simplicity we require
21*22ce4affSfengbojiang      * that it is always present. By default, zstd will write the content size
22*22ce4affSfengbojiang      * in the header when it is known. If you can't guarantee that the frame
23*22ce4affSfengbojiang      * content size is always written into the header, either use streaming
24*22ce4affSfengbojiang      * decompression, or ZSTD_decompressBound().
25*22ce4affSfengbojiang      */
26*22ce4affSfengbojiang     unsigned long long const rSize = ZSTD_getFrameContentSize(cBuff, cSize);
27*22ce4affSfengbojiang     CHECK(rSize != ZSTD_CONTENTSIZE_ERROR, "%s: not compressed by zstd!", fname);
28*22ce4affSfengbojiang     CHECK(rSize != ZSTD_CONTENTSIZE_UNKNOWN, "%s: original size unknown!", fname);
29*22ce4affSfengbojiang 
30*22ce4affSfengbojiang     void* const rBuff = malloc_orDie((size_t)rSize);
31*22ce4affSfengbojiang 
32*22ce4affSfengbojiang     /* Decompress.
33*22ce4affSfengbojiang      * If you are doing many decompressions, you may want to reuse the context
34*22ce4affSfengbojiang      * and use ZSTD_decompressDCtx(). If you want to set advanced parameters,
35*22ce4affSfengbojiang      * use ZSTD_DCtx_setParameter().
36*22ce4affSfengbojiang      */
37*22ce4affSfengbojiang     size_t const dSize = ZSTD_decompress(rBuff, rSize, cBuff, cSize);
38*22ce4affSfengbojiang     CHECK_ZSTD(dSize);
39*22ce4affSfengbojiang     /* When zstd knows the content size, it will error if it doesn't match. */
40*22ce4affSfengbojiang     CHECK(dSize == rSize, "Impossible because zstd will check this condition!");
41*22ce4affSfengbojiang 
42*22ce4affSfengbojiang     /* success */
43*22ce4affSfengbojiang     printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize);
44*22ce4affSfengbojiang 
45*22ce4affSfengbojiang     free(rBuff);
46*22ce4affSfengbojiang     free(cBuff);
47*22ce4affSfengbojiang }
48*22ce4affSfengbojiang 
main(int argc,const char ** argv)49*22ce4affSfengbojiang int main(int argc, const char** argv)
50*22ce4affSfengbojiang {
51*22ce4affSfengbojiang     const char* const exeName = argv[0];
52*22ce4affSfengbojiang 
53*22ce4affSfengbojiang     if (argc!=2) {
54*22ce4affSfengbojiang         printf("wrong arguments\n");
55*22ce4affSfengbojiang         printf("usage:\n");
56*22ce4affSfengbojiang         printf("%s FILE\n", exeName);
57*22ce4affSfengbojiang         return 1;
58*22ce4affSfengbojiang     }
59*22ce4affSfengbojiang 
60*22ce4affSfengbojiang     decompress(argv[1]);
61*22ce4affSfengbojiang 
62*22ce4affSfengbojiang     printf("%s correctly decoded (in memory). \n", argv[1]);
63*22ce4affSfengbojiang 
64*22ce4affSfengbojiang     return 0;
65*22ce4affSfengbojiang }
66