Path: blob/main/sys/contrib/zstd/examples/dictionary_decompression.c
48249 views
/*1* Copyright (c) Yann Collet, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/91011#include <stdio.h> // printf12#include <stdlib.h> // free13#include <zstd.h> // presumes zstd library is installed14#include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()1516/* createDict() :17`dictFileName` is supposed to have been created using `zstd --train` */18static ZSTD_DDict* createDict_orDie(const char* dictFileName)19{20size_t dictSize;21printf("loading dictionary %s \n", dictFileName);22void* const dictBuffer = mallocAndLoadFile_orDie(dictFileName, &dictSize);23ZSTD_DDict* const ddict = ZSTD_createDDict(dictBuffer, dictSize);24CHECK(ddict != NULL, "ZSTD_createDDict() failed!");25free(dictBuffer);26return ddict;27}2829static void decompress(const char* fname, const ZSTD_DDict* ddict)30{31size_t cSize;32void* const cBuff = mallocAndLoadFile_orDie(fname, &cSize);33/* Read the content size from the frame header. For simplicity we require34* that it is always present. By default, zstd will write the content size35* in the header when it is known. If you can't guarantee that the frame36* content size is always written into the header, either use streaming37* decompression, or ZSTD_decompressBound().38*/39unsigned long long const rSize = ZSTD_getFrameContentSize(cBuff, cSize);40CHECK(rSize != ZSTD_CONTENTSIZE_ERROR, "%s: not compressed by zstd!", fname);41CHECK(rSize != ZSTD_CONTENTSIZE_UNKNOWN, "%s: original size unknown!", fname);42void* const rBuff = malloc_orDie((size_t)rSize);4344/* Check that the dictionary ID matches.45* If a non-zstd dictionary is used, then both will be zero.46* By default zstd always writes the dictionary ID into the frame.47* Zstd will check if there is a dictionary ID mismatch as well.48*/49unsigned const expectedDictID = ZSTD_getDictID_fromDDict(ddict);50unsigned const actualDictID = ZSTD_getDictID_fromFrame(cBuff, cSize);51CHECK(actualDictID == expectedDictID,52"DictID mismatch: expected %u got %u",53expectedDictID,54actualDictID);5556/* Decompress using the dictionary.57* If you need to control the decompression parameters, then use the58* advanced API: ZSTD_DCtx_setParameter(), ZSTD_DCtx_refDDict(), and59* ZSTD_decompressDCtx().60*/61ZSTD_DCtx* const dctx = ZSTD_createDCtx();62CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");63size_t const dSize = ZSTD_decompress_usingDDict(dctx, rBuff, rSize, cBuff, cSize, ddict);64CHECK_ZSTD(dSize);65/* When zstd knows the content size, it will error if it doesn't match. */66CHECK(dSize == rSize, "Impossible because zstd will check this condition!");6768/* success */69printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize);7071ZSTD_freeDCtx(dctx);72free(rBuff);73free(cBuff);74}757677int main(int argc, const char** argv)78{79const char* const exeName = argv[0];8081if (argc<3) {82printf("wrong arguments\n");83printf("usage:\n");84printf("%s [FILES] dictionary\n", exeName);85return 1;86}8788/* load dictionary only once */89const char* const dictName = argv[argc-1];90ZSTD_DDict* const dictPtr = createDict_orDie(dictName);9192int u;93for (u=1; u<argc-1; u++) decompress(argv[u], dictPtr);9495ZSTD_freeDDict(dictPtr);96printf("All %u files correctly decoded (in memory) \n", argc-2);97return 0;98}99100101