12b9c00cbSConrad Meyer /*
2*5ff13fbcSAllan Jude * Copyright (c) Yann Collet, Facebook, Inc.
32b9c00cbSConrad Meyer * All rights reserved.
42b9c00cbSConrad Meyer *
52b9c00cbSConrad Meyer * This source code is licensed under both the BSD-style license (found in the
62b9c00cbSConrad Meyer * LICENSE file in the root directory of this source tree) and the GPLv2 (found
72b9c00cbSConrad Meyer * in the COPYING file in the root directory of this source tree).
82b9c00cbSConrad Meyer * You may select, at your option, one of the above-listed licenses.
92b9c00cbSConrad Meyer */
102b9c00cbSConrad Meyer
112b9c00cbSConrad Meyer
122b9c00cbSConrad Meyer #include <stdio.h> // printf
132b9c00cbSConrad Meyer #include <stdlib.h> // free
142b9c00cbSConrad Meyer #include <zstd.h> // presumes zstd library is installed
152b9c00cbSConrad Meyer #include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()
162b9c00cbSConrad Meyer
172b9c00cbSConrad Meyer /* createDict() :
182b9c00cbSConrad Meyer `dictFileName` is supposed to have been created using `zstd --train` */
createDict_orDie(const char * dictFileName)192b9c00cbSConrad Meyer static ZSTD_DDict* createDict_orDie(const char* dictFileName)
202b9c00cbSConrad Meyer {
212b9c00cbSConrad Meyer size_t dictSize;
222b9c00cbSConrad Meyer printf("loading dictionary %s \n", dictFileName);
232b9c00cbSConrad Meyer void* const dictBuffer = mallocAndLoadFile_orDie(dictFileName, &dictSize);
242b9c00cbSConrad Meyer ZSTD_DDict* const ddict = ZSTD_createDDict(dictBuffer, dictSize);
252b9c00cbSConrad Meyer CHECK(ddict != NULL, "ZSTD_createDDict() failed!");
262b9c00cbSConrad Meyer free(dictBuffer);
272b9c00cbSConrad Meyer return ddict;
282b9c00cbSConrad Meyer }
292b9c00cbSConrad Meyer
decompress(const char * fname,const ZSTD_DDict * ddict)302b9c00cbSConrad Meyer static void decompress(const char* fname, const ZSTD_DDict* ddict)
312b9c00cbSConrad Meyer {
322b9c00cbSConrad Meyer size_t cSize;
332b9c00cbSConrad Meyer void* const cBuff = mallocAndLoadFile_orDie(fname, &cSize);
342b9c00cbSConrad Meyer /* Read the content size from the frame header. For simplicity we require
352b9c00cbSConrad Meyer * that it is always present. By default, zstd will write the content size
362b9c00cbSConrad Meyer * in the header when it is known. If you can't guarantee that the frame
372b9c00cbSConrad Meyer * content size is always written into the header, either use streaming
382b9c00cbSConrad Meyer * decompression, or ZSTD_decompressBound().
392b9c00cbSConrad Meyer */
402b9c00cbSConrad Meyer unsigned long long const rSize = ZSTD_getFrameContentSize(cBuff, cSize);
412b9c00cbSConrad Meyer CHECK(rSize != ZSTD_CONTENTSIZE_ERROR, "%s: not compressed by zstd!", fname);
422b9c00cbSConrad Meyer CHECK(rSize != ZSTD_CONTENTSIZE_UNKNOWN, "%s: original size unknown!", fname);
432b9c00cbSConrad Meyer void* const rBuff = malloc_orDie((size_t)rSize);
442b9c00cbSConrad Meyer
452b9c00cbSConrad Meyer /* Check that the dictionary ID matches.
462b9c00cbSConrad Meyer * If a non-zstd dictionary is used, then both will be zero.
472b9c00cbSConrad Meyer * By default zstd always writes the dictionary ID into the frame.
482b9c00cbSConrad Meyer * Zstd will check if there is a dictionary ID mismatch as well.
492b9c00cbSConrad Meyer */
502b9c00cbSConrad Meyer unsigned const expectedDictID = ZSTD_getDictID_fromDDict(ddict);
512b9c00cbSConrad Meyer unsigned const actualDictID = ZSTD_getDictID_fromFrame(cBuff, cSize);
522b9c00cbSConrad Meyer CHECK(actualDictID == expectedDictID,
532b9c00cbSConrad Meyer "DictID mismatch: expected %u got %u",
542b9c00cbSConrad Meyer expectedDictID,
552b9c00cbSConrad Meyer actualDictID);
562b9c00cbSConrad Meyer
572b9c00cbSConrad Meyer /* Decompress using the dictionary.
582b9c00cbSConrad Meyer * If you need to control the decompression parameters, then use the
592b9c00cbSConrad Meyer * advanced API: ZSTD_DCtx_setParameter(), ZSTD_DCtx_refDDict(), and
602b9c00cbSConrad Meyer * ZSTD_decompressDCtx().
612b9c00cbSConrad Meyer */
622b9c00cbSConrad Meyer ZSTD_DCtx* const dctx = ZSTD_createDCtx();
632b9c00cbSConrad Meyer CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
642b9c00cbSConrad Meyer size_t const dSize = ZSTD_decompress_usingDDict(dctx, rBuff, rSize, cBuff, cSize, ddict);
652b9c00cbSConrad Meyer CHECK_ZSTD(dSize);
662b9c00cbSConrad Meyer /* When zstd knows the content size, it will error if it doesn't match. */
672b9c00cbSConrad Meyer CHECK(dSize == rSize, "Impossible because zstd will check this condition!");
682b9c00cbSConrad Meyer
692b9c00cbSConrad Meyer /* success */
702b9c00cbSConrad Meyer printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize);
712b9c00cbSConrad Meyer
722b9c00cbSConrad Meyer ZSTD_freeDCtx(dctx);
732b9c00cbSConrad Meyer free(rBuff);
742b9c00cbSConrad Meyer free(cBuff);
752b9c00cbSConrad Meyer }
762b9c00cbSConrad Meyer
772b9c00cbSConrad Meyer
main(int argc,const char ** argv)782b9c00cbSConrad Meyer int main(int argc, const char** argv)
792b9c00cbSConrad Meyer {
802b9c00cbSConrad Meyer const char* const exeName = argv[0];
812b9c00cbSConrad Meyer
822b9c00cbSConrad Meyer if (argc<3) {
832b9c00cbSConrad Meyer printf("wrong arguments\n");
842b9c00cbSConrad Meyer printf("usage:\n");
852b9c00cbSConrad Meyer printf("%s [FILES] dictionary\n", exeName);
862b9c00cbSConrad Meyer return 1;
872b9c00cbSConrad Meyer }
882b9c00cbSConrad Meyer
892b9c00cbSConrad Meyer /* load dictionary only once */
902b9c00cbSConrad Meyer const char* const dictName = argv[argc-1];
912b9c00cbSConrad Meyer ZSTD_DDict* const dictPtr = createDict_orDie(dictName);
922b9c00cbSConrad Meyer
932b9c00cbSConrad Meyer int u;
942b9c00cbSConrad Meyer for (u=1; u<argc-1; u++) decompress(argv[u], dictPtr);
952b9c00cbSConrad Meyer
962b9c00cbSConrad Meyer ZSTD_freeDDict(dictPtr);
972b9c00cbSConrad Meyer printf("All %u files correctly decoded (in memory) \n", argc-2);
982b9c00cbSConrad Meyer return 0;
992b9c00cbSConrad Meyer }
100