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> // printf
13 #include <stdlib.h> // free
14 #include <zstd.h> // presumes zstd library is installed
15 #include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()
18 `dictFileName` is supposed to have been created using `zstd --train` */
19 static ZSTD_DDict* createDict_orDie(const char* dictFileName)
22 printf("loading dictionary %s \n", dictFileName);
23 void* const dictBuffer = mallocAndLoadFile_orDie(dictFileName, &dictSize);
24 ZSTD_DDict* const ddict = ZSTD_createDDict(dictBuffer, dictSize);
25 CHECK(ddict != NULL, "ZSTD_createDDict() failed!");
30 static void decompress(const char* fname, const ZSTD_DDict* ddict)
33 void* const cBuff = mallocAndLoadFile_orDie(fname, &cSize);
34 /* Read the content size from the frame header. For simplicity we require
35 * that it is always present. By default, zstd will write the content size
36 * in the header when it is known. If you can't guarantee that the frame
37 * content size is always written into the header, either use streaming
38 * decompression, or ZSTD_decompressBound().
40 unsigned long long const rSize = ZSTD_getFrameContentSize(cBuff, cSize);
41 CHECK(rSize != ZSTD_CONTENTSIZE_ERROR, "%s: not compressed by zstd!", fname);
42 CHECK(rSize != ZSTD_CONTENTSIZE_UNKNOWN, "%s: original size unknown!", fname);
43 void* const rBuff = malloc_orDie((size_t)rSize);
45 /* Check that the dictionary ID matches.
46 * If a non-zstd dictionary is used, then both will be zero.
47 * By default zstd always writes the dictionary ID into the frame.
48 * Zstd will check if there is a dictionary ID mismatch as well.
50 unsigned const expectedDictID = ZSTD_getDictID_fromDDict(ddict);
51 unsigned const actualDictID = ZSTD_getDictID_fromFrame(cBuff, cSize);
52 CHECK(actualDictID == expectedDictID,
53 "DictID mismatch: expected %u got %u",
57 /* Decompress using the dictionary.
58 * If you need to control the decompression parameters, then use the
59 * advanced API: ZSTD_DCtx_setParameter(), ZSTD_DCtx_refDDict(), and
60 * ZSTD_decompressDCtx().
62 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
63 CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
64 size_t const dSize = ZSTD_decompress_usingDDict(dctx, rBuff, rSize, cBuff, cSize, ddict);
66 /* When zstd knows the content size, it will error if it doesn't match. */
67 CHECK(dSize == rSize, "Impossible because zstd will check this condition!");
70 printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize);
78 int main(int argc, const char** argv)
80 const char* const exeName = argv[0];
83 printf("wrong arguments\n");
85 printf("%s [FILES] dictionary\n", exeName);
89 /* load dictionary only once */
90 const char* const dictName = argv[argc-1];
91 ZSTD_DDict* const dictPtr = createDict_orDie(dictName);
94 for (u=1; u<argc-1; u++) decompress(argv[u], dictPtr);
96 ZSTD_freeDDict(dictPtr);
97 printf("All %u files correctly decoded (in memory) \n", argc-2);