The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/contrib/zstd/examples/dictionary_decompression.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) Yann Collet, Facebook, Inc.
    3  * All rights reserved.
    4  *
    5  * 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).
    8  * You may select, at your option, one of the above-listed licenses.
    9  */
   10 
   11 
   12 #include <stdio.h>     // printf
   13 #include <stdlib.h>    // free
   14 #include <zstd.h>      // presumes zstd library is installed
   15 #include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()
   16 
   17 /* createDict() :
   18    `dictFileName` is supposed to have been created using `zstd --train` */
   19 static ZSTD_DDict* createDict_orDie(const char* dictFileName)
   20 {
   21     size_t dictSize;
   22     printf("loading dictionary %s \n", dictFileName);
   23     void* const dictBuffer = mallocAndLoadFile_orDie(dictFileName, &dictSize);
   24     ZSTD_DDict* const ddict = ZSTD_createDDict(dictBuffer, dictSize);
   25     CHECK(ddict != NULL, "ZSTD_createDDict() failed!");
   26     free(dictBuffer);
   27     return ddict;
   28 }
   29 
   30 static void decompress(const char* fname, const ZSTD_DDict* ddict)
   31 {
   32     size_t cSize;
   33     void* const cBuff = mallocAndLoadFile_orDie(fname, &cSize);
   34     /* 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);
   43     void* const rBuff = malloc_orDie((size_t)rSize);
   44 
   45     /* 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      */
   62     ZSTD_DCtx* const dctx = ZSTD_createDCtx();
   63     CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
   64     size_t const dSize = ZSTD_decompress_usingDDict(dctx, rBuff, rSize, cBuff, cSize, ddict);
   65     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!");
   68 
   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 
   78 int 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];
   91     ZSTD_DDict* const dictPtr = createDict_orDie(dictName);
   92 
   93     int u;
   94     for (u=1; u<argc-1; u++) decompress(argv[u], dictPtr);
   95 
   96     ZSTD_freeDDict(dictPtr);
   97     printf("All %u files correctly decoded (in memory) \n", argc-2);
   98     return 0;
   99 }

Cache object: 91f3e96d04ec5c4d6c3b8209a4c5a266


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.