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