xref: /netbsd-src/external/bsd/zstd/dist/examples/dictionary_decompression.c (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
1*3117ece4Schristos /*
2*3117ece4Schristos  * Copyright (c) Meta Platforms, Inc. and affiliates.
3*3117ece4Schristos  * All rights reserved.
4*3117ece4Schristos  *
5*3117ece4Schristos  * This source code is licensed under both the BSD-style license (found in the
6*3117ece4Schristos  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*3117ece4Schristos  * in the COPYING file in the root directory of this source tree).
8*3117ece4Schristos  * You may select, at your option, one of the above-listed licenses.
9*3117ece4Schristos  */
10*3117ece4Schristos 
11*3117ece4Schristos 
12*3117ece4Schristos #include <stdio.h>     // printf
13*3117ece4Schristos #include <stdlib.h>    // free
14*3117ece4Schristos #include <zstd.h>      // presumes zstd library is installed
15*3117ece4Schristos #include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()
16*3117ece4Schristos 
17*3117ece4Schristos /* createDict() :
18*3117ece4Schristos    `dictFileName` is supposed to have been created using `zstd --train` */
19*3117ece4Schristos static ZSTD_DDict* createDict_orDie(const char* dictFileName)
20*3117ece4Schristos {
21*3117ece4Schristos     size_t dictSize;
22*3117ece4Schristos     printf("loading dictionary %s \n", dictFileName);
23*3117ece4Schristos     void* const dictBuffer = mallocAndLoadFile_orDie(dictFileName, &dictSize);
24*3117ece4Schristos     ZSTD_DDict* const ddict = ZSTD_createDDict(dictBuffer, dictSize);
25*3117ece4Schristos     CHECK(ddict != NULL, "ZSTD_createDDict() failed!");
26*3117ece4Schristos     free(dictBuffer);
27*3117ece4Schristos     return ddict;
28*3117ece4Schristos }
29*3117ece4Schristos 
30*3117ece4Schristos static void decompress(const char* fname, const ZSTD_DDict* ddict)
31*3117ece4Schristos {
32*3117ece4Schristos     size_t cSize;
33*3117ece4Schristos     void* const cBuff = mallocAndLoadFile_orDie(fname, &cSize);
34*3117ece4Schristos     /* Read the content size from the frame header. For simplicity we require
35*3117ece4Schristos      * that it is always present. By default, zstd will write the content size
36*3117ece4Schristos      * in the header when it is known. If you can't guarantee that the frame
37*3117ece4Schristos      * content size is always written into the header, either use streaming
38*3117ece4Schristos      * decompression, or ZSTD_decompressBound().
39*3117ece4Schristos      */
40*3117ece4Schristos     unsigned long long const rSize = ZSTD_getFrameContentSize(cBuff, cSize);
41*3117ece4Schristos     CHECK(rSize != ZSTD_CONTENTSIZE_ERROR, "%s: not compressed by zstd!", fname);
42*3117ece4Schristos     CHECK(rSize != ZSTD_CONTENTSIZE_UNKNOWN, "%s: original size unknown!", fname);
43*3117ece4Schristos     void* const rBuff = malloc_orDie((size_t)rSize);
44*3117ece4Schristos 
45*3117ece4Schristos     /* Check that the dictionary ID matches.
46*3117ece4Schristos      * If a non-zstd dictionary is used, then both will be zero.
47*3117ece4Schristos      * By default zstd always writes the dictionary ID into the frame.
48*3117ece4Schristos      * Zstd will check if there is a dictionary ID mismatch as well.
49*3117ece4Schristos      */
50*3117ece4Schristos     unsigned const expectedDictID = ZSTD_getDictID_fromDDict(ddict);
51*3117ece4Schristos     unsigned const actualDictID = ZSTD_getDictID_fromFrame(cBuff, cSize);
52*3117ece4Schristos     CHECK(actualDictID == expectedDictID,
53*3117ece4Schristos           "DictID mismatch: expected %u got %u",
54*3117ece4Schristos           expectedDictID,
55*3117ece4Schristos           actualDictID);
56*3117ece4Schristos 
57*3117ece4Schristos     /* Decompress using the dictionary.
58*3117ece4Schristos      * If you need to control the decompression parameters, then use the
59*3117ece4Schristos      * advanced API: ZSTD_DCtx_setParameter(), ZSTD_DCtx_refDDict(), and
60*3117ece4Schristos      * ZSTD_decompressDCtx().
61*3117ece4Schristos      */
62*3117ece4Schristos     ZSTD_DCtx* const dctx = ZSTD_createDCtx();
63*3117ece4Schristos     CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
64*3117ece4Schristos     size_t const dSize = ZSTD_decompress_usingDDict(dctx, rBuff, rSize, cBuff, cSize, ddict);
65*3117ece4Schristos     CHECK_ZSTD(dSize);
66*3117ece4Schristos     /* When zstd knows the content size, it will error if it doesn't match. */
67*3117ece4Schristos     CHECK(dSize == rSize, "Impossible because zstd will check this condition!");
68*3117ece4Schristos 
69*3117ece4Schristos     /* success */
70*3117ece4Schristos     printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize);
71*3117ece4Schristos 
72*3117ece4Schristos     ZSTD_freeDCtx(dctx);
73*3117ece4Schristos     free(rBuff);
74*3117ece4Schristos     free(cBuff);
75*3117ece4Schristos }
76*3117ece4Schristos 
77*3117ece4Schristos 
78*3117ece4Schristos int main(int argc, const char** argv)
79*3117ece4Schristos {
80*3117ece4Schristos     const char* const exeName = argv[0];
81*3117ece4Schristos 
82*3117ece4Schristos     if (argc<3) {
83*3117ece4Schristos         printf("wrong arguments\n");
84*3117ece4Schristos         printf("usage:\n");
85*3117ece4Schristos         printf("%s [FILES] dictionary\n", exeName);
86*3117ece4Schristos         return 1;
87*3117ece4Schristos     }
88*3117ece4Schristos 
89*3117ece4Schristos     /* load dictionary only once */
90*3117ece4Schristos     const char* const dictName = argv[argc-1];
91*3117ece4Schristos     ZSTD_DDict* const dictPtr = createDict_orDie(dictName);
92*3117ece4Schristos 
93*3117ece4Schristos     int u;
94*3117ece4Schristos     for (u=1; u<argc-1; u++) decompress(argv[u], dictPtr);
95*3117ece4Schristos 
96*3117ece4Schristos     ZSTD_freeDDict(dictPtr);
97*3117ece4Schristos     printf("All %u files correctly decoded (in memory) \n", argc-2);
98*3117ece4Schristos     return 0;
99*3117ece4Schristos }
100