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> // printf
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
17*22ce4affSfengbojiang /* createDict() :
18*22ce4affSfengbojiang `dictFileName` is supposed to have been created using `zstd --train` */
createDict_orDie(const char * dictFileName)19*22ce4affSfengbojiang static ZSTD_DDict* createDict_orDie(const char* dictFileName)
20*22ce4affSfengbojiang {
21*22ce4affSfengbojiang size_t dictSize;
22*22ce4affSfengbojiang printf("loading dictionary %s \n", dictFileName);
23*22ce4affSfengbojiang void* const dictBuffer = mallocAndLoadFile_orDie(dictFileName, &dictSize);
24*22ce4affSfengbojiang ZSTD_DDict* const ddict = ZSTD_createDDict(dictBuffer, dictSize);
25*22ce4affSfengbojiang CHECK(ddict != NULL, "ZSTD_createDDict() failed!");
26*22ce4affSfengbojiang free(dictBuffer);
27*22ce4affSfengbojiang return ddict;
28*22ce4affSfengbojiang }
29*22ce4affSfengbojiang
decompress(const char * fname,const ZSTD_DDict * ddict)30*22ce4affSfengbojiang static void decompress(const char* fname, const ZSTD_DDict* ddict)
31*22ce4affSfengbojiang {
32*22ce4affSfengbojiang size_t cSize;
33*22ce4affSfengbojiang void* const cBuff = mallocAndLoadFile_orDie(fname, &cSize);
34*22ce4affSfengbojiang /* Read the content size from the frame header. For simplicity we require
35*22ce4affSfengbojiang * that it is always present. By default, zstd will write the content size
36*22ce4affSfengbojiang * in the header when it is known. If you can't guarantee that the frame
37*22ce4affSfengbojiang * content size is always written into the header, either use streaming
38*22ce4affSfengbojiang * decompression, or ZSTD_decompressBound().
39*22ce4affSfengbojiang */
40*22ce4affSfengbojiang unsigned long long const rSize = ZSTD_getFrameContentSize(cBuff, cSize);
41*22ce4affSfengbojiang CHECK(rSize != ZSTD_CONTENTSIZE_ERROR, "%s: not compressed by zstd!", fname);
42*22ce4affSfengbojiang CHECK(rSize != ZSTD_CONTENTSIZE_UNKNOWN, "%s: original size unknown!", fname);
43*22ce4affSfengbojiang void* const rBuff = malloc_orDie((size_t)rSize);
44*22ce4affSfengbojiang
45*22ce4affSfengbojiang /* Check that the dictionary ID matches.
46*22ce4affSfengbojiang * If a non-zstd dictionary is used, then both will be zero.
47*22ce4affSfengbojiang * By default zstd always writes the dictionary ID into the frame.
48*22ce4affSfengbojiang * Zstd will check if there is a dictionary ID mismatch as well.
49*22ce4affSfengbojiang */
50*22ce4affSfengbojiang unsigned const expectedDictID = ZSTD_getDictID_fromDDict(ddict);
51*22ce4affSfengbojiang unsigned const actualDictID = ZSTD_getDictID_fromFrame(cBuff, cSize);
52*22ce4affSfengbojiang CHECK(actualDictID == expectedDictID,
53*22ce4affSfengbojiang "DictID mismatch: expected %u got %u",
54*22ce4affSfengbojiang expectedDictID,
55*22ce4affSfengbojiang actualDictID);
56*22ce4affSfengbojiang
57*22ce4affSfengbojiang /* Decompress using the dictionary.
58*22ce4affSfengbojiang * If you need to control the decompression parameters, then use the
59*22ce4affSfengbojiang * advanced API: ZSTD_DCtx_setParameter(), ZSTD_DCtx_refDDict(), and
60*22ce4affSfengbojiang * ZSTD_decompressDCtx().
61*22ce4affSfengbojiang */
62*22ce4affSfengbojiang ZSTD_DCtx* const dctx = ZSTD_createDCtx();
63*22ce4affSfengbojiang CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
64*22ce4affSfengbojiang size_t const dSize = ZSTD_decompress_usingDDict(dctx, rBuff, rSize, cBuff, cSize, ddict);
65*22ce4affSfengbojiang CHECK_ZSTD(dSize);
66*22ce4affSfengbojiang /* When zstd knows the content size, it will error if it doesn't match. */
67*22ce4affSfengbojiang CHECK(dSize == rSize, "Impossible because zstd will check this condition!");
68*22ce4affSfengbojiang
69*22ce4affSfengbojiang /* success */
70*22ce4affSfengbojiang printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize);
71*22ce4affSfengbojiang
72*22ce4affSfengbojiang ZSTD_freeDCtx(dctx);
73*22ce4affSfengbojiang free(rBuff);
74*22ce4affSfengbojiang free(cBuff);
75*22ce4affSfengbojiang }
76*22ce4affSfengbojiang
77*22ce4affSfengbojiang
main(int argc,const char ** argv)78*22ce4affSfengbojiang int main(int argc, const char** argv)
79*22ce4affSfengbojiang {
80*22ce4affSfengbojiang const char* const exeName = argv[0];
81*22ce4affSfengbojiang
82*22ce4affSfengbojiang if (argc<3) {
83*22ce4affSfengbojiang printf("wrong arguments\n");
84*22ce4affSfengbojiang printf("usage:\n");
85*22ce4affSfengbojiang printf("%s [FILES] dictionary\n", exeName);
86*22ce4affSfengbojiang return 1;
87*22ce4affSfengbojiang }
88*22ce4affSfengbojiang
89*22ce4affSfengbojiang /* load dictionary only once */
90*22ce4affSfengbojiang const char* const dictName = argv[argc-1];
91*22ce4affSfengbojiang ZSTD_DDict* const dictPtr = createDict_orDie(dictName);
92*22ce4affSfengbojiang
93*22ce4affSfengbojiang int u;
94*22ce4affSfengbojiang for (u=1; u<argc-1; u++) decompress(argv[u], dictPtr);
95*22ce4affSfengbojiang
96*22ce4affSfengbojiang ZSTD_freeDDict(dictPtr);
97*22ce4affSfengbojiang printf("All %u files correctly decoded (in memory) \n", argc-2);
98*22ce4affSfengbojiang return 0;
99*22ce4affSfengbojiang }
100