git subrepo pull (merge) --force deps/libchdr
[pcsx_rearmed.git] / deps / libchdr / deps / zstd-1.5.5 / examples / dictionary_compression.c
CommitLineData
648db22b 1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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/* This example deals with Dictionary compression,
12 * its counterpart is `examples/dictionary_decompression.c` .
13 * These examples presume that a dictionary already exists.
14 * The main method to create a dictionary is `zstd --train`,
15 * look at the CLI documentation for details.
16 * Another possible method is to employ dictionary training API,
17 * published in `lib/zdict.h` .
18**/
19
20#include <stdio.h> // printf
21#include <stdlib.h> // free
22#include <string.h> // memset, strcat
23#include <zstd.h> // presumes zstd library is installed
24#include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()
25
26/* createDict() :
27** `dictFileName` is supposed already created using `zstd --train` */
28static ZSTD_CDict* createCDict_orDie(const char* dictFileName, int cLevel)
29{
30 size_t dictSize;
31 printf("loading dictionary %s \n", dictFileName);
32 void* const dictBuffer = mallocAndLoadFile_orDie(dictFileName, &dictSize);
33 ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, cLevel);
34 CHECK(cdict != NULL, "ZSTD_createCDict() failed!");
35 free(dictBuffer);
36 return cdict;
37}
38
39
40static void compress(const char* fname, const char* oname, const ZSTD_CDict* cdict)
41{
42 size_t fSize;
43 void* const fBuff = mallocAndLoadFile_orDie(fname, &fSize);
44 size_t const cBuffSize = ZSTD_compressBound(fSize);
45 void* const cBuff = malloc_orDie(cBuffSize);
46
47 /* Compress using the dictionary.
48 * This function writes the dictionary id, and content size into the header.
49 * But, it doesn't use a checksum. You can control these options using the
50 * advanced API: ZSTD_CCtx_setParameter(), ZSTD_CCtx_refCDict(),
51 * and ZSTD_compress2().
52 */
53 ZSTD_CCtx* const cctx = ZSTD_createCCtx();
54 CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");
55 size_t const cSize = ZSTD_compress_usingCDict(cctx, cBuff, cBuffSize, fBuff, fSize, cdict);
56 CHECK_ZSTD(cSize);
57
58 saveFile_orDie(oname, cBuff, cSize);
59
60 /* success */
61 printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname);
62
63 ZSTD_freeCCtx(cctx); /* never fails */
64 free(fBuff);
65 free(cBuff);
66}
67
68
69static char* createOutFilename_orDie(const char* filename)
70{
71 size_t const inL = strlen(filename);
72 size_t const outL = inL + 5;
73 void* outSpace = malloc_orDie(outL);
74 memset(outSpace, 0, outL);
75 strcat(outSpace, filename);
76 strcat(outSpace, ".zst");
77 return (char*)outSpace;
78}
79
80int main(int argc, const char** argv)
81{
82 const char* const exeName = argv[0];
83 int const cLevel = 3;
84
85 if (argc<3) {
86 fprintf(stderr, "wrong arguments\n");
87 fprintf(stderr, "usage:\n");
88 fprintf(stderr, "%s [FILES] dictionary\n", exeName);
89 return 1;
90 }
91
92 /* load dictionary only once */
93 const char* const dictName = argv[argc-1];
94 ZSTD_CDict* const dictPtr = createCDict_orDie(dictName, cLevel);
95
96 int u;
97 for (u=1; u<argc-1; u++) {
98 const char* inFilename = argv[u];
99 char* const outFilename = createOutFilename_orDie(inFilename);
100 compress(inFilename, outFilename, dictPtr);
101 free(outFilename);
102 }
103
104 ZSTD_freeCDict(dictPtr);
105 printf("All %u files compressed. \n", argc-2);
106 return 0;
107}