xref: /freebsd-src/sys/contrib/zstd/examples/dictionary_compression.c (revision 2b9c00cb6bd9392645dc8afca59cf57c42df4e2d)
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 #include <stdio.h>     // printf
11*2b9c00cbSConrad Meyer #include <stdlib.h>    // free
12*2b9c00cbSConrad Meyer #include <string.h>    // memset, strcat
13*2b9c00cbSConrad Meyer #include <zstd.h>      // presumes zstd library is installed
14*2b9c00cbSConrad Meyer #include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()
15*2b9c00cbSConrad Meyer 
16*2b9c00cbSConrad Meyer /* createDict() :
17*2b9c00cbSConrad Meyer    `dictFileName` is supposed to have been created using `zstd --train` */
18*2b9c00cbSConrad Meyer static ZSTD_CDict* createCDict_orDie(const char* dictFileName, int cLevel)
19*2b9c00cbSConrad Meyer {
20*2b9c00cbSConrad Meyer     size_t dictSize;
21*2b9c00cbSConrad Meyer     printf("loading dictionary %s \n", dictFileName);
22*2b9c00cbSConrad Meyer     void* const dictBuffer = mallocAndLoadFile_orDie(dictFileName, &dictSize);
23*2b9c00cbSConrad Meyer     ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, cLevel);
24*2b9c00cbSConrad Meyer     CHECK(cdict != NULL, "ZSTD_createCDict() failed!");
25*2b9c00cbSConrad Meyer     free(dictBuffer);
26*2b9c00cbSConrad Meyer     return cdict;
27*2b9c00cbSConrad Meyer }
28*2b9c00cbSConrad Meyer 
29*2b9c00cbSConrad Meyer 
30*2b9c00cbSConrad Meyer static void compress(const char* fname, const char* oname, const ZSTD_CDict* cdict)
31*2b9c00cbSConrad Meyer {
32*2b9c00cbSConrad Meyer     size_t fSize;
33*2b9c00cbSConrad Meyer     void* const fBuff = mallocAndLoadFile_orDie(fname, &fSize);
34*2b9c00cbSConrad Meyer     size_t const cBuffSize = ZSTD_compressBound(fSize);
35*2b9c00cbSConrad Meyer     void* const cBuff = malloc_orDie(cBuffSize);
36*2b9c00cbSConrad Meyer 
37*2b9c00cbSConrad Meyer     /* Compress using the dictionary.
38*2b9c00cbSConrad Meyer      * This function writes the dictionary id, and content size into the header.
39*2b9c00cbSConrad Meyer      * But, it doesn't use a checksum. You can control these options using the
40*2b9c00cbSConrad Meyer      * advanced API: ZSTD_CCtx_setParameter(), ZSTD_CCtx_refCDict(),
41*2b9c00cbSConrad Meyer      * and ZSTD_compress2().
42*2b9c00cbSConrad Meyer      */
43*2b9c00cbSConrad Meyer     ZSTD_CCtx* const cctx = ZSTD_createCCtx();
44*2b9c00cbSConrad Meyer     CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");
45*2b9c00cbSConrad Meyer     size_t const cSize = ZSTD_compress_usingCDict(cctx, cBuff, cBuffSize, fBuff, fSize, cdict);
46*2b9c00cbSConrad Meyer     CHECK_ZSTD(cSize);
47*2b9c00cbSConrad Meyer 
48*2b9c00cbSConrad Meyer     saveFile_orDie(oname, cBuff, cSize);
49*2b9c00cbSConrad Meyer 
50*2b9c00cbSConrad Meyer     /* success */
51*2b9c00cbSConrad Meyer     printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname);
52*2b9c00cbSConrad Meyer 
53*2b9c00cbSConrad Meyer     ZSTD_freeCCtx(cctx);   /* never fails */
54*2b9c00cbSConrad Meyer     free(fBuff);
55*2b9c00cbSConrad Meyer     free(cBuff);
56*2b9c00cbSConrad Meyer }
57*2b9c00cbSConrad Meyer 
58*2b9c00cbSConrad Meyer 
59*2b9c00cbSConrad Meyer static char* createOutFilename_orDie(const char* filename)
60*2b9c00cbSConrad Meyer {
61*2b9c00cbSConrad Meyer     size_t const inL = strlen(filename);
62*2b9c00cbSConrad Meyer     size_t const outL = inL + 5;
63*2b9c00cbSConrad Meyer     void* outSpace = malloc_orDie(outL);
64*2b9c00cbSConrad Meyer     memset(outSpace, 0, outL);
65*2b9c00cbSConrad Meyer     strcat(outSpace, filename);
66*2b9c00cbSConrad Meyer     strcat(outSpace, ".zst");
67*2b9c00cbSConrad Meyer     return (char*)outSpace;
68*2b9c00cbSConrad Meyer }
69*2b9c00cbSConrad Meyer 
70*2b9c00cbSConrad Meyer int main(int argc, const char** argv)
71*2b9c00cbSConrad Meyer {
72*2b9c00cbSConrad Meyer     const char* const exeName = argv[0];
73*2b9c00cbSConrad Meyer     int const cLevel = 3;
74*2b9c00cbSConrad Meyer 
75*2b9c00cbSConrad Meyer     if (argc<3) {
76*2b9c00cbSConrad Meyer         fprintf(stderr, "wrong arguments\n");
77*2b9c00cbSConrad Meyer         fprintf(stderr, "usage:\n");
78*2b9c00cbSConrad Meyer         fprintf(stderr, "%s [FILES] dictionary\n", exeName);
79*2b9c00cbSConrad Meyer         return 1;
80*2b9c00cbSConrad Meyer     }
81*2b9c00cbSConrad Meyer 
82*2b9c00cbSConrad Meyer     /* load dictionary only once */
83*2b9c00cbSConrad Meyer     const char* const dictName = argv[argc-1];
84*2b9c00cbSConrad Meyer     ZSTD_CDict* const dictPtr = createCDict_orDie(dictName, cLevel);
85*2b9c00cbSConrad Meyer 
86*2b9c00cbSConrad Meyer     int u;
87*2b9c00cbSConrad Meyer     for (u=1; u<argc-1; u++) {
88*2b9c00cbSConrad Meyer         const char* inFilename = argv[u];
89*2b9c00cbSConrad Meyer         char* const outFilename = createOutFilename_orDie(inFilename);
90*2b9c00cbSConrad Meyer         compress(inFilename, outFilename, dictPtr);
91*2b9c00cbSConrad Meyer         free(outFilename);
92*2b9c00cbSConrad Meyer     }
93*2b9c00cbSConrad Meyer 
94*2b9c00cbSConrad Meyer     ZSTD_freeCDict(dictPtr);
95*2b9c00cbSConrad Meyer     printf("All %u files compressed. \n", argc-2);
96*2b9c00cbSConrad Meyer     return 0;
97*2b9c00cbSConrad Meyer }
98