blob: e6c999964a24c4f996045617271f49ab37847808 [file] [log] [blame]
Yann Collet394bdd72017-08-29 09:24:11 -07001/*
Elliott Hughes44aba642023-09-12 20:18:59 +00002 * Copyright (c) Meta Platforms, Inc. and affiliates.
Yann Collet4ded9e52016-08-30 10:04:33 -07003 * All rights reserved.
4 *
Yann Collet394bdd72017-08-29 09:24:11 -07005 * 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).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Yann Collet4ded9e52016-08-30 10:04:33 -07009 */
Yann Collet0809a882016-07-09 18:25:10 +020010
Yann Collet0809a882016-07-09 18:25:10 +020011
Yann Collet974f52f2016-07-07 14:08:00 +020012#include <stdio.h> // printf
Nick Terrell1d0c1702019-04-05 18:11:17 -070013#include <stdlib.h> // free
Yann Collet0809a882016-07-09 18:25:10 +020014#include <zstd.h> // presumes zstd library is installed
Nick Terrell1d0c1702019-04-05 18:11:17 -070015#include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()
Yann Collet974f52f2016-07-07 14:08:00 +020016
Yann Collete66708d2016-07-09 22:56:12 +020017/* createDict() :
18 `dictFileName` is supposed to have been created using `zstd --train` */
Yann Collet373d4f92016-08-01 17:36:11 +020019static ZSTD_DDict* createDict_orDie(const char* dictFileName)
Yann Collet974f52f2016-07-07 14:08:00 +020020{
21 size_t dictSize;
Yann Collet5e80dd32016-07-13 17:38:39 +020022 printf("loading dictionary %s \n", dictFileName);
Yi Jinbc4dc602018-12-17 16:54:55 -080023 void* const dictBuffer = mallocAndLoadFile_orDie(dictFileName, &dictSize);
Yann Collet373d4f92016-08-01 17:36:11 +020024 ZSTD_DDict* const ddict = ZSTD_createDDict(dictBuffer, dictSize);
Nick Terrell1d0c1702019-04-05 18:11:17 -070025 CHECK(ddict != NULL, "ZSTD_createDDict() failed!");
Yann Collet974f52f2016-07-07 14:08:00 +020026 free(dictBuffer);
27 return ddict;
28}
29
Yann Collet974f52f2016-07-07 14:08:00 +020030static void decompress(const char* fname, const ZSTD_DDict* ddict)
31{
32 size_t cSize;
Yi Jinbc4dc602018-12-17 16:54:55 -080033 void* const cBuff = mallocAndLoadFile_orDie(fname, &cSize);
Nick Terrell1d0c1702019-04-05 18:11:17 -070034 /* 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().
39 */
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);
Yann Colleta2664642016-09-09 19:33:56 +020043 void* const rBuff = malloc_orDie((size_t)rSize);
Yann Collet974f52f2016-07-07 14:08:00 +020044
Nick Terrell1d0c1702019-04-05 18:11:17 -070045 /* 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.
49 */
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",
54 expectedDictID,
55 actualDictID);
56
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().
61 */
Yann Collet974f52f2016-07-07 14:08:00 +020062 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
Nick Terrell1d0c1702019-04-05 18:11:17 -070063 CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
Yann Collet974f52f2016-07-07 14:08:00 +020064 size_t const dSize = ZSTD_decompress_usingDDict(dctx, rBuff, rSize, cBuff, cSize, ddict);
Nick Terrell1d0c1702019-04-05 18:11:17 -070065 CHECK_ZSTD(dSize);
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!");
Yann Collet974f52f2016-07-07 14:08:00 +020068
69 /* success */
70 printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize);
71
72 ZSTD_freeDCtx(dctx);
73 free(rBuff);
74 free(cBuff);
75}
76
77
78int main(int argc, const char** argv)
79{
80 const char* const exeName = argv[0];
81
82 if (argc<3) {
83 printf("wrong arguments\n");
84 printf("usage:\n");
85 printf("%s [FILES] dictionary\n", exeName);
86 return 1;
87 }
88
89 /* load dictionary only once */
90 const char* const dictName = argv[argc-1];
Yann Collet373d4f92016-08-01 17:36:11 +020091 ZSTD_DDict* const dictPtr = createDict_orDie(dictName);
Yann Collet974f52f2016-07-07 14:08:00 +020092
93 int u;
94 for (u=1; u<argc-1; u++) decompress(argv[u], dictPtr);
95
Yann Collet373d4f92016-08-01 17:36:11 +020096 ZSTD_freeDDict(dictPtr);
Yann Colletcadd7cd2016-07-15 18:52:37 +020097 printf("All %u files correctly decoded (in memory) \n", argc-2);
Yann Collet07639052016-08-03 01:57:57 +020098 return 0;
Yann Collet974f52f2016-07-07 14:08:00 +020099}