xref: /aosp_15_r20/external/zstd/lib/zdict.h (revision 01826a4963a0d8a59bc3812d29bdf0fb76416722)
1*01826a49SYabin Cui /*
2*01826a49SYabin Cui  * Copyright (c) Meta Platforms, Inc. and affiliates.
3*01826a49SYabin Cui  * All rights reserved.
4*01826a49SYabin Cui  *
5*01826a49SYabin Cui  * This source code is licensed under both the BSD-style license (found in the
6*01826a49SYabin Cui  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*01826a49SYabin Cui  * in the COPYING file in the root directory of this source tree).
8*01826a49SYabin Cui  * You may select, at your option, one of the above-listed licenses.
9*01826a49SYabin Cui  */
10*01826a49SYabin Cui 
11*01826a49SYabin Cui #if defined (__cplusplus)
12*01826a49SYabin Cui extern "C" {
13*01826a49SYabin Cui #endif
14*01826a49SYabin Cui 
15*01826a49SYabin Cui #ifndef ZSTD_ZDICT_H
16*01826a49SYabin Cui #define ZSTD_ZDICT_H
17*01826a49SYabin Cui 
18*01826a49SYabin Cui /*======  Dependencies  ======*/
19*01826a49SYabin Cui #include <stddef.h>  /* size_t */
20*01826a49SYabin Cui 
21*01826a49SYabin Cui 
22*01826a49SYabin Cui /* =====   ZDICTLIB_API : control library symbols visibility   ===== */
23*01826a49SYabin Cui #ifndef ZDICTLIB_VISIBLE
24*01826a49SYabin Cui    /* Backwards compatibility with old macro name */
25*01826a49SYabin Cui #  ifdef ZDICTLIB_VISIBILITY
26*01826a49SYabin Cui #    define ZDICTLIB_VISIBLE ZDICTLIB_VISIBILITY
27*01826a49SYabin Cui #  elif defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__MINGW32__)
28*01826a49SYabin Cui #    define ZDICTLIB_VISIBLE __attribute__ ((visibility ("default")))
29*01826a49SYabin Cui #  else
30*01826a49SYabin Cui #    define ZDICTLIB_VISIBLE
31*01826a49SYabin Cui #  endif
32*01826a49SYabin Cui #endif
33*01826a49SYabin Cui 
34*01826a49SYabin Cui #ifndef ZDICTLIB_HIDDEN
35*01826a49SYabin Cui #  if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__MINGW32__)
36*01826a49SYabin Cui #    define ZDICTLIB_HIDDEN __attribute__ ((visibility ("hidden")))
37*01826a49SYabin Cui #  else
38*01826a49SYabin Cui #    define ZDICTLIB_HIDDEN
39*01826a49SYabin Cui #  endif
40*01826a49SYabin Cui #endif
41*01826a49SYabin Cui 
42*01826a49SYabin Cui #if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1)
43*01826a49SYabin Cui #  define ZDICTLIB_API __declspec(dllexport) ZDICTLIB_VISIBLE
44*01826a49SYabin Cui #elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1)
45*01826a49SYabin Cui #  define ZDICTLIB_API __declspec(dllimport) ZDICTLIB_VISIBLE /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/
46*01826a49SYabin Cui #else
47*01826a49SYabin Cui #  define ZDICTLIB_API ZDICTLIB_VISIBLE
48*01826a49SYabin Cui #endif
49*01826a49SYabin Cui 
50*01826a49SYabin Cui /*******************************************************************************
51*01826a49SYabin Cui  * Zstd dictionary builder
52*01826a49SYabin Cui  *
53*01826a49SYabin Cui  * FAQ
54*01826a49SYabin Cui  * ===
55*01826a49SYabin Cui  * Why should I use a dictionary?
56*01826a49SYabin Cui  * ------------------------------
57*01826a49SYabin Cui  *
58*01826a49SYabin Cui  * Zstd can use dictionaries to improve compression ratio of small data.
59*01826a49SYabin Cui  * Traditionally small files don't compress well because there is very little
60*01826a49SYabin Cui  * repetition in a single sample, since it is small. But, if you are compressing
61*01826a49SYabin Cui  * many similar files, like a bunch of JSON records that share the same
62*01826a49SYabin Cui  * structure, you can train a dictionary on ahead of time on some samples of
63*01826a49SYabin Cui  * these files. Then, zstd can use the dictionary to find repetitions that are
64*01826a49SYabin Cui  * present across samples. This can vastly improve compression ratio.
65*01826a49SYabin Cui  *
66*01826a49SYabin Cui  * When is a dictionary useful?
67*01826a49SYabin Cui  * ----------------------------
68*01826a49SYabin Cui  *
69*01826a49SYabin Cui  * Dictionaries are useful when compressing many small files that are similar.
70*01826a49SYabin Cui  * The larger a file is, the less benefit a dictionary will have. Generally,
71*01826a49SYabin Cui  * we don't expect dictionary compression to be effective past 100KB. And the
72*01826a49SYabin Cui  * smaller a file is, the more we would expect the dictionary to help.
73*01826a49SYabin Cui  *
74*01826a49SYabin Cui  * How do I use a dictionary?
75*01826a49SYabin Cui  * --------------------------
76*01826a49SYabin Cui  *
77*01826a49SYabin Cui  * Simply pass the dictionary to the zstd compressor with
78*01826a49SYabin Cui  * `ZSTD_CCtx_loadDictionary()`. The same dictionary must then be passed to
79*01826a49SYabin Cui  * the decompressor, using `ZSTD_DCtx_loadDictionary()`. There are other
80*01826a49SYabin Cui  * more advanced functions that allow selecting some options, see zstd.h for
81*01826a49SYabin Cui  * complete documentation.
82*01826a49SYabin Cui  *
83*01826a49SYabin Cui  * What is a zstd dictionary?
84*01826a49SYabin Cui  * --------------------------
85*01826a49SYabin Cui  *
86*01826a49SYabin Cui  * A zstd dictionary has two pieces: Its header, and its content. The header
87*01826a49SYabin Cui  * contains a magic number, the dictionary ID, and entropy tables. These
88*01826a49SYabin Cui  * entropy tables allow zstd to save on header costs in the compressed file,
89*01826a49SYabin Cui  * which really matters for small data. The content is just bytes, which are
90*01826a49SYabin Cui  * repeated content that is common across many samples.
91*01826a49SYabin Cui  *
92*01826a49SYabin Cui  * What is a raw content dictionary?
93*01826a49SYabin Cui  * ---------------------------------
94*01826a49SYabin Cui  *
95*01826a49SYabin Cui  * A raw content dictionary is just bytes. It doesn't have a zstd dictionary
96*01826a49SYabin Cui  * header, a dictionary ID, or entropy tables. Any buffer is a valid raw
97*01826a49SYabin Cui  * content dictionary.
98*01826a49SYabin Cui  *
99*01826a49SYabin Cui  * How do I train a dictionary?
100*01826a49SYabin Cui  * ----------------------------
101*01826a49SYabin Cui  *
102*01826a49SYabin Cui  * Gather samples from your use case. These samples should be similar to each
103*01826a49SYabin Cui  * other. If you have several use cases, you could try to train one dictionary
104*01826a49SYabin Cui  * per use case.
105*01826a49SYabin Cui  *
106*01826a49SYabin Cui  * Pass those samples to `ZDICT_trainFromBuffer()` and that will train your
107*01826a49SYabin Cui  * dictionary. There are a few advanced versions of this function, but this
108*01826a49SYabin Cui  * is a great starting point. If you want to further tune your dictionary
109*01826a49SYabin Cui  * you could try `ZDICT_optimizeTrainFromBuffer_cover()`. If that is too slow
110*01826a49SYabin Cui  * you can try `ZDICT_optimizeTrainFromBuffer_fastCover()`.
111*01826a49SYabin Cui  *
112*01826a49SYabin Cui  * If the dictionary training function fails, that is likely because you
113*01826a49SYabin Cui  * either passed too few samples, or a dictionary would not be effective
114*01826a49SYabin Cui  * for your data. Look at the messages that the dictionary trainer printed,
115*01826a49SYabin Cui  * if it doesn't say too few samples, then a dictionary would not be effective.
116*01826a49SYabin Cui  *
117*01826a49SYabin Cui  * How large should my dictionary be?
118*01826a49SYabin Cui  * ----------------------------------
119*01826a49SYabin Cui  *
120*01826a49SYabin Cui  * A reasonable dictionary size, the `dictBufferCapacity`, is about 100KB.
121*01826a49SYabin Cui  * The zstd CLI defaults to a 110KB dictionary. You likely don't need a
122*01826a49SYabin Cui  * dictionary larger than that. But, most use cases can get away with a
123*01826a49SYabin Cui  * smaller dictionary. The advanced dictionary builders can automatically
124*01826a49SYabin Cui  * shrink the dictionary for you, and select the smallest size that doesn't
125*01826a49SYabin Cui  * hurt compression ratio too much. See the `shrinkDict` parameter.
126*01826a49SYabin Cui  * A smaller dictionary can save memory, and potentially speed up
127*01826a49SYabin Cui  * compression.
128*01826a49SYabin Cui  *
129*01826a49SYabin Cui  * How many samples should I provide to the dictionary builder?
130*01826a49SYabin Cui  * ------------------------------------------------------------
131*01826a49SYabin Cui  *
132*01826a49SYabin Cui  * We generally recommend passing ~100x the size of the dictionary
133*01826a49SYabin Cui  * in samples. A few thousand should suffice. Having too few samples
134*01826a49SYabin Cui  * can hurt the dictionaries effectiveness. Having more samples will
135*01826a49SYabin Cui  * only improve the dictionaries effectiveness. But having too many
136*01826a49SYabin Cui  * samples can slow down the dictionary builder.
137*01826a49SYabin Cui  *
138*01826a49SYabin Cui  * How do I determine if a dictionary will be effective?
139*01826a49SYabin Cui  * -----------------------------------------------------
140*01826a49SYabin Cui  *
141*01826a49SYabin Cui  * Simply train a dictionary and try it out. You can use zstd's built in
142*01826a49SYabin Cui  * benchmarking tool to test the dictionary effectiveness.
143*01826a49SYabin Cui  *
144*01826a49SYabin Cui  *   # Benchmark levels 1-3 without a dictionary
145*01826a49SYabin Cui  *   zstd -b1e3 -r /path/to/my/files
146*01826a49SYabin Cui  *   # Benchmark levels 1-3 with a dictionary
147*01826a49SYabin Cui  *   zstd -b1e3 -r /path/to/my/files -D /path/to/my/dictionary
148*01826a49SYabin Cui  *
149*01826a49SYabin Cui  * When should I retrain a dictionary?
150*01826a49SYabin Cui  * -----------------------------------
151*01826a49SYabin Cui  *
152*01826a49SYabin Cui  * You should retrain a dictionary when its effectiveness drops. Dictionary
153*01826a49SYabin Cui  * effectiveness drops as the data you are compressing changes. Generally, we do
154*01826a49SYabin Cui  * expect dictionaries to "decay" over time, as your data changes, but the rate
155*01826a49SYabin Cui  * at which they decay depends on your use case. Internally, we regularly
156*01826a49SYabin Cui  * retrain dictionaries, and if the new dictionary performs significantly
157*01826a49SYabin Cui  * better than the old dictionary, we will ship the new dictionary.
158*01826a49SYabin Cui  *
159*01826a49SYabin Cui  * I have a raw content dictionary, how do I turn it into a zstd dictionary?
160*01826a49SYabin Cui  * -------------------------------------------------------------------------
161*01826a49SYabin Cui  *
162*01826a49SYabin Cui  * If you have a raw content dictionary, e.g. by manually constructing it, or
163*01826a49SYabin Cui  * using a third-party dictionary builder, you can turn it into a zstd
164*01826a49SYabin Cui  * dictionary by using `ZDICT_finalizeDictionary()`. You'll also have to
165*01826a49SYabin Cui  * provide some samples of the data. It will add the zstd header to the
166*01826a49SYabin Cui  * raw content, which contains a dictionary ID and entropy tables, which
167*01826a49SYabin Cui  * will improve compression ratio, and allow zstd to write the dictionary ID
168*01826a49SYabin Cui  * into the frame, if you so choose.
169*01826a49SYabin Cui  *
170*01826a49SYabin Cui  * Do I have to use zstd's dictionary builder?
171*01826a49SYabin Cui  * -------------------------------------------
172*01826a49SYabin Cui  *
173*01826a49SYabin Cui  * No! You can construct dictionary content however you please, it is just
174*01826a49SYabin Cui  * bytes. It will always be valid as a raw content dictionary. If you want
175*01826a49SYabin Cui  * a zstd dictionary, which can improve compression ratio, use
176*01826a49SYabin Cui  * `ZDICT_finalizeDictionary()`.
177*01826a49SYabin Cui  *
178*01826a49SYabin Cui  * What is the attack surface of a zstd dictionary?
179*01826a49SYabin Cui  * ------------------------------------------------
180*01826a49SYabin Cui  *
181*01826a49SYabin Cui  * Zstd is heavily fuzz tested, including loading fuzzed dictionaries, so
182*01826a49SYabin Cui  * zstd should never crash, or access out-of-bounds memory no matter what
183*01826a49SYabin Cui  * the dictionary is. However, if an attacker can control the dictionary
184*01826a49SYabin Cui  * during decompression, they can cause zstd to generate arbitrary bytes,
185*01826a49SYabin Cui  * just like if they controlled the compressed data.
186*01826a49SYabin Cui  *
187*01826a49SYabin Cui  ******************************************************************************/
188*01826a49SYabin Cui 
189*01826a49SYabin Cui 
190*01826a49SYabin Cui /*! ZDICT_trainFromBuffer():
191*01826a49SYabin Cui  *  Train a dictionary from an array of samples.
192*01826a49SYabin Cui  *  Redirect towards ZDICT_optimizeTrainFromBuffer_fastCover() single-threaded, with d=8, steps=4,
193*01826a49SYabin Cui  *  f=20, and accel=1.
194*01826a49SYabin Cui  *  Samples must be stored concatenated in a single flat buffer `samplesBuffer`,
195*01826a49SYabin Cui  *  supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order.
196*01826a49SYabin Cui  *  The resulting dictionary will be saved into `dictBuffer`.
197*01826a49SYabin Cui  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
198*01826a49SYabin Cui  *          or an error code, which can be tested with ZDICT_isError().
199*01826a49SYabin Cui  *  Note:  Dictionary training will fail if there are not enough samples to construct a
200*01826a49SYabin Cui  *         dictionary, or if most of the samples are too small (< 8 bytes being the lower limit).
201*01826a49SYabin Cui  *         If dictionary training fails, you should use zstd without a dictionary, as the dictionary
202*01826a49SYabin Cui  *         would've been ineffective anyways. If you believe your samples would benefit from a dictionary
203*01826a49SYabin Cui  *         please open an issue with details, and we can look into it.
204*01826a49SYabin Cui  *  Note: ZDICT_trainFromBuffer()'s memory usage is about 6 MB.
205*01826a49SYabin Cui  *  Tips: In general, a reasonable dictionary has a size of ~ 100 KB.
206*01826a49SYabin Cui  *        It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`.
207*01826a49SYabin Cui  *        In general, it's recommended to provide a few thousands samples, though this can vary a lot.
208*01826a49SYabin Cui  *        It's recommended that total size of all samples be about ~x100 times the target size of dictionary.
209*01826a49SYabin Cui  */
210*01826a49SYabin Cui ZDICTLIB_API size_t ZDICT_trainFromBuffer(void* dictBuffer, size_t dictBufferCapacity,
211*01826a49SYabin Cui                                     const void* samplesBuffer,
212*01826a49SYabin Cui                                     const size_t* samplesSizes, unsigned nbSamples);
213*01826a49SYabin Cui 
214*01826a49SYabin Cui typedef struct {
215*01826a49SYabin Cui     int      compressionLevel;   /**< optimize for a specific zstd compression level; 0 means default */
216*01826a49SYabin Cui     unsigned notificationLevel;  /**< Write log to stderr; 0 = none (default); 1 = errors; 2 = progression; 3 = details; 4 = debug; */
217*01826a49SYabin Cui     unsigned dictID;             /**< force dictID value; 0 means auto mode (32-bits random value)
218*01826a49SYabin Cui                                   *   NOTE: The zstd format reserves some dictionary IDs for future use.
219*01826a49SYabin Cui                                   *         You may use them in private settings, but be warned that they
220*01826a49SYabin Cui                                   *         may be used by zstd in a public dictionary registry in the future.
221*01826a49SYabin Cui                                   *         These dictionary IDs are:
222*01826a49SYabin Cui                                   *           - low range  : <= 32767
223*01826a49SYabin Cui                                   *           - high range : >= (2^31)
224*01826a49SYabin Cui                                   */
225*01826a49SYabin Cui } ZDICT_params_t;
226*01826a49SYabin Cui 
227*01826a49SYabin Cui /*! ZDICT_finalizeDictionary():
228*01826a49SYabin Cui  * Given a custom content as a basis for dictionary, and a set of samples,
229*01826a49SYabin Cui  * finalize dictionary by adding headers and statistics according to the zstd
230*01826a49SYabin Cui  * dictionary format.
231*01826a49SYabin Cui  *
232*01826a49SYabin Cui  * Samples must be stored concatenated in a flat buffer `samplesBuffer`,
233*01826a49SYabin Cui  * supplied with an array of sizes `samplesSizes`, providing the size of each
234*01826a49SYabin Cui  * sample in order. The samples are used to construct the statistics, so they
235*01826a49SYabin Cui  * should be representative of what you will compress with this dictionary.
236*01826a49SYabin Cui  *
237*01826a49SYabin Cui  * The compression level can be set in `parameters`. You should pass the
238*01826a49SYabin Cui  * compression level you expect to use in production. The statistics for each
239*01826a49SYabin Cui  * compression level differ, so tuning the dictionary for the compression level
240*01826a49SYabin Cui  * can help quite a bit.
241*01826a49SYabin Cui  *
242*01826a49SYabin Cui  * You can set an explicit dictionary ID in `parameters`, or allow us to pick
243*01826a49SYabin Cui  * a random dictionary ID for you, but we can't guarantee no collisions.
244*01826a49SYabin Cui  *
245*01826a49SYabin Cui  * The dstDictBuffer and the dictContent may overlap, and the content will be
246*01826a49SYabin Cui  * appended to the end of the header. If the header + the content doesn't fit in
247*01826a49SYabin Cui  * maxDictSize the beginning of the content is truncated to make room, since it
248*01826a49SYabin Cui  * is presumed that the most profitable content is at the end of the dictionary,
249*01826a49SYabin Cui  * since that is the cheapest to reference.
250*01826a49SYabin Cui  *
251*01826a49SYabin Cui  * `maxDictSize` must be >= max(dictContentSize, ZSTD_DICTSIZE_MIN).
252*01826a49SYabin Cui  *
253*01826a49SYabin Cui  * @return: size of dictionary stored into `dstDictBuffer` (<= `maxDictSize`),
254*01826a49SYabin Cui  *          or an error code, which can be tested by ZDICT_isError().
255*01826a49SYabin Cui  * Note: ZDICT_finalizeDictionary() will push notifications into stderr if
256*01826a49SYabin Cui  *       instructed to, using notificationLevel>0.
257*01826a49SYabin Cui  * NOTE: This function currently may fail in several edge cases including:
258*01826a49SYabin Cui  *         * Not enough samples
259*01826a49SYabin Cui  *         * Samples are uncompressible
260*01826a49SYabin Cui  *         * Samples are all exactly the same
261*01826a49SYabin Cui  */
262*01826a49SYabin Cui ZDICTLIB_API size_t ZDICT_finalizeDictionary(void* dstDictBuffer, size_t maxDictSize,
263*01826a49SYabin Cui                                 const void* dictContent, size_t dictContentSize,
264*01826a49SYabin Cui                                 const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples,
265*01826a49SYabin Cui                                 ZDICT_params_t parameters);
266*01826a49SYabin Cui 
267*01826a49SYabin Cui 
268*01826a49SYabin Cui /*======   Helper functions   ======*/
269*01826a49SYabin Cui ZDICTLIB_API unsigned ZDICT_getDictID(const void* dictBuffer, size_t dictSize);  /**< extracts dictID; @return zero if error (not a valid dictionary) */
270*01826a49SYabin Cui ZDICTLIB_API size_t ZDICT_getDictHeaderSize(const void* dictBuffer, size_t dictSize);  /* returns dict header size; returns a ZSTD error code on failure */
271*01826a49SYabin Cui ZDICTLIB_API unsigned ZDICT_isError(size_t errorCode);
272*01826a49SYabin Cui ZDICTLIB_API const char* ZDICT_getErrorName(size_t errorCode);
273*01826a49SYabin Cui 
274*01826a49SYabin Cui #endif   /* ZSTD_ZDICT_H */
275*01826a49SYabin Cui 
276*01826a49SYabin Cui #if defined(ZDICT_STATIC_LINKING_ONLY) && !defined(ZSTD_ZDICT_H_STATIC)
277*01826a49SYabin Cui #define ZSTD_ZDICT_H_STATIC
278*01826a49SYabin Cui 
279*01826a49SYabin Cui /* This can be overridden externally to hide static symbols. */
280*01826a49SYabin Cui #ifndef ZDICTLIB_STATIC_API
281*01826a49SYabin Cui #  if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1)
282*01826a49SYabin Cui #    define ZDICTLIB_STATIC_API __declspec(dllexport) ZDICTLIB_VISIBLE
283*01826a49SYabin Cui #  elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1)
284*01826a49SYabin Cui #    define ZDICTLIB_STATIC_API __declspec(dllimport) ZDICTLIB_VISIBLE
285*01826a49SYabin Cui #  else
286*01826a49SYabin Cui #    define ZDICTLIB_STATIC_API ZDICTLIB_VISIBLE
287*01826a49SYabin Cui #  endif
288*01826a49SYabin Cui #endif
289*01826a49SYabin Cui 
290*01826a49SYabin Cui /* ====================================================================================
291*01826a49SYabin Cui  * The definitions in this section are considered experimental.
292*01826a49SYabin Cui  * They should never be used with a dynamic library, as they may change in the future.
293*01826a49SYabin Cui  * They are provided for advanced usages.
294*01826a49SYabin Cui  * Use them only in association with static linking.
295*01826a49SYabin Cui  * ==================================================================================== */
296*01826a49SYabin Cui 
297*01826a49SYabin Cui #define ZDICT_DICTSIZE_MIN    256
298*01826a49SYabin Cui /* Deprecated: Remove in v1.6.0 */
299*01826a49SYabin Cui #define ZDICT_CONTENTSIZE_MIN 128
300*01826a49SYabin Cui 
301*01826a49SYabin Cui /*! ZDICT_cover_params_t:
302*01826a49SYabin Cui  *  k and d are the only required parameters.
303*01826a49SYabin Cui  *  For others, value 0 means default.
304*01826a49SYabin Cui  */
305*01826a49SYabin Cui typedef struct {
306*01826a49SYabin Cui     unsigned k;                  /* Segment size : constraint: 0 < k : Reasonable range [16, 2048+] */
307*01826a49SYabin Cui     unsigned d;                  /* dmer size : constraint: 0 < d <= k : Reasonable range [6, 16] */
308*01826a49SYabin Cui     unsigned steps;              /* Number of steps : Only used for optimization : 0 means default (40) : Higher means more parameters checked */
309*01826a49SYabin Cui     unsigned nbThreads;          /* Number of threads : constraint: 0 < nbThreads : 1 means single-threaded : Only used for optimization : Ignored if ZSTD_MULTITHREAD is not defined */
310*01826a49SYabin Cui     double splitPoint;           /* Percentage of samples used for training: Only used for optimization : the first nbSamples * splitPoint samples will be used to training, the last nbSamples * (1 - splitPoint) samples will be used for testing, 0 means default (1.0), 1.0 when all samples are used for both training and testing */
311*01826a49SYabin Cui     unsigned shrinkDict;         /* Train dictionaries to shrink in size starting from the minimum size and selects the smallest dictionary that is shrinkDictMaxRegression% worse than the largest dictionary. 0 means no shrinking and 1 means shrinking  */
312*01826a49SYabin Cui     unsigned shrinkDictMaxRegression; /* Sets shrinkDictMaxRegression so that a smaller dictionary can be at worse shrinkDictMaxRegression% worse than the max dict size dictionary. */
313*01826a49SYabin Cui     ZDICT_params_t zParams;
314*01826a49SYabin Cui } ZDICT_cover_params_t;
315*01826a49SYabin Cui 
316*01826a49SYabin Cui typedef struct {
317*01826a49SYabin Cui     unsigned k;                  /* Segment size : constraint: 0 < k : Reasonable range [16, 2048+] */
318*01826a49SYabin Cui     unsigned d;                  /* dmer size : constraint: 0 < d <= k : Reasonable range [6, 16] */
319*01826a49SYabin Cui     unsigned f;                  /* log of size of frequency array : constraint: 0 < f <= 31 : 1 means default(20)*/
320*01826a49SYabin Cui     unsigned steps;              /* Number of steps : Only used for optimization : 0 means default (40) : Higher means more parameters checked */
321*01826a49SYabin Cui     unsigned nbThreads;          /* Number of threads : constraint: 0 < nbThreads : 1 means single-threaded : Only used for optimization : Ignored if ZSTD_MULTITHREAD is not defined */
322*01826a49SYabin Cui     double splitPoint;           /* Percentage of samples used for training: Only used for optimization : the first nbSamples * splitPoint samples will be used to training, the last nbSamples * (1 - splitPoint) samples will be used for testing, 0 means default (0.75), 1.0 when all samples are used for both training and testing */
323*01826a49SYabin Cui     unsigned accel;              /* Acceleration level: constraint: 0 < accel <= 10, higher means faster and less accurate, 0 means default(1) */
324*01826a49SYabin Cui     unsigned shrinkDict;         /* Train dictionaries to shrink in size starting from the minimum size and selects the smallest dictionary that is shrinkDictMaxRegression% worse than the largest dictionary. 0 means no shrinking and 1 means shrinking  */
325*01826a49SYabin Cui     unsigned shrinkDictMaxRegression; /* Sets shrinkDictMaxRegression so that a smaller dictionary can be at worse shrinkDictMaxRegression% worse than the max dict size dictionary. */
326*01826a49SYabin Cui 
327*01826a49SYabin Cui     ZDICT_params_t zParams;
328*01826a49SYabin Cui } ZDICT_fastCover_params_t;
329*01826a49SYabin Cui 
330*01826a49SYabin Cui /*! ZDICT_trainFromBuffer_cover():
331*01826a49SYabin Cui  *  Train a dictionary from an array of samples using the COVER algorithm.
332*01826a49SYabin Cui  *  Samples must be stored concatenated in a single flat buffer `samplesBuffer`,
333*01826a49SYabin Cui  *  supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order.
334*01826a49SYabin Cui  *  The resulting dictionary will be saved into `dictBuffer`.
335*01826a49SYabin Cui  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
336*01826a49SYabin Cui  *          or an error code, which can be tested with ZDICT_isError().
337*01826a49SYabin Cui  *          See ZDICT_trainFromBuffer() for details on failure modes.
338*01826a49SYabin Cui  *  Note: ZDICT_trainFromBuffer_cover() requires about 9 bytes of memory for each input byte.
339*01826a49SYabin Cui  *  Tips: In general, a reasonable dictionary has a size of ~ 100 KB.
340*01826a49SYabin Cui  *        It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`.
341*01826a49SYabin Cui  *        In general, it's recommended to provide a few thousands samples, though this can vary a lot.
342*01826a49SYabin Cui  *        It's recommended that total size of all samples be about ~x100 times the target size of dictionary.
343*01826a49SYabin Cui  */
344*01826a49SYabin Cui ZDICTLIB_STATIC_API size_t ZDICT_trainFromBuffer_cover(
345*01826a49SYabin Cui           void *dictBuffer, size_t dictBufferCapacity,
346*01826a49SYabin Cui     const void *samplesBuffer, const size_t *samplesSizes, unsigned nbSamples,
347*01826a49SYabin Cui           ZDICT_cover_params_t parameters);
348*01826a49SYabin Cui 
349*01826a49SYabin Cui /*! ZDICT_optimizeTrainFromBuffer_cover():
350*01826a49SYabin Cui  * The same requirements as above hold for all the parameters except `parameters`.
351*01826a49SYabin Cui  * This function tries many parameter combinations and picks the best parameters.
352*01826a49SYabin Cui  * `*parameters` is filled with the best parameters found,
353*01826a49SYabin Cui  * dictionary constructed with those parameters is stored in `dictBuffer`.
354*01826a49SYabin Cui  *
355*01826a49SYabin Cui  * All of the parameters d, k, steps are optional.
356*01826a49SYabin Cui  * If d is non-zero then we don't check multiple values of d, otherwise we check d = {6, 8}.
357*01826a49SYabin Cui  * if steps is zero it defaults to its default value.
358*01826a49SYabin Cui  * If k is non-zero then we don't check multiple values of k, otherwise we check steps values in [50, 2000].
359*01826a49SYabin Cui  *
360*01826a49SYabin Cui  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
361*01826a49SYabin Cui  *          or an error code, which can be tested with ZDICT_isError().
362*01826a49SYabin Cui  *          On success `*parameters` contains the parameters selected.
363*01826a49SYabin Cui  *          See ZDICT_trainFromBuffer() for details on failure modes.
364*01826a49SYabin Cui  * Note: ZDICT_optimizeTrainFromBuffer_cover() requires about 8 bytes of memory for each input byte and additionally another 5 bytes of memory for each byte of memory for each thread.
365*01826a49SYabin Cui  */
366*01826a49SYabin Cui ZDICTLIB_STATIC_API size_t ZDICT_optimizeTrainFromBuffer_cover(
367*01826a49SYabin Cui           void* dictBuffer, size_t dictBufferCapacity,
368*01826a49SYabin Cui     const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples,
369*01826a49SYabin Cui           ZDICT_cover_params_t* parameters);
370*01826a49SYabin Cui 
371*01826a49SYabin Cui /*! ZDICT_trainFromBuffer_fastCover():
372*01826a49SYabin Cui  *  Train a dictionary from an array of samples using a modified version of COVER algorithm.
373*01826a49SYabin Cui  *  Samples must be stored concatenated in a single flat buffer `samplesBuffer`,
374*01826a49SYabin Cui  *  supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order.
375*01826a49SYabin Cui  *  d and k are required.
376*01826a49SYabin Cui  *  All other parameters are optional, will use default values if not provided
377*01826a49SYabin Cui  *  The resulting dictionary will be saved into `dictBuffer`.
378*01826a49SYabin Cui  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
379*01826a49SYabin Cui  *          or an error code, which can be tested with ZDICT_isError().
380*01826a49SYabin Cui  *          See ZDICT_trainFromBuffer() for details on failure modes.
381*01826a49SYabin Cui  *  Note: ZDICT_trainFromBuffer_fastCover() requires 6 * 2^f bytes of memory.
382*01826a49SYabin Cui  *  Tips: In general, a reasonable dictionary has a size of ~ 100 KB.
383*01826a49SYabin Cui  *        It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`.
384*01826a49SYabin Cui  *        In general, it's recommended to provide a few thousands samples, though this can vary a lot.
385*01826a49SYabin Cui  *        It's recommended that total size of all samples be about ~x100 times the target size of dictionary.
386*01826a49SYabin Cui  */
387*01826a49SYabin Cui ZDICTLIB_STATIC_API size_t ZDICT_trainFromBuffer_fastCover(void *dictBuffer,
388*01826a49SYabin Cui                     size_t dictBufferCapacity, const void *samplesBuffer,
389*01826a49SYabin Cui                     const size_t *samplesSizes, unsigned nbSamples,
390*01826a49SYabin Cui                     ZDICT_fastCover_params_t parameters);
391*01826a49SYabin Cui 
392*01826a49SYabin Cui /*! ZDICT_optimizeTrainFromBuffer_fastCover():
393*01826a49SYabin Cui  * The same requirements as above hold for all the parameters except `parameters`.
394*01826a49SYabin Cui  * This function tries many parameter combinations (specifically, k and d combinations)
395*01826a49SYabin Cui  * and picks the best parameters. `*parameters` is filled with the best parameters found,
396*01826a49SYabin Cui  * dictionary constructed with those parameters is stored in `dictBuffer`.
397*01826a49SYabin Cui  * All of the parameters d, k, steps, f, and accel are optional.
398*01826a49SYabin Cui  * If d is non-zero then we don't check multiple values of d, otherwise we check d = {6, 8}.
399*01826a49SYabin Cui  * if steps is zero it defaults to its default value.
400*01826a49SYabin Cui  * If k is non-zero then we don't check multiple values of k, otherwise we check steps values in [50, 2000].
401*01826a49SYabin Cui  * If f is zero, default value of 20 is used.
402*01826a49SYabin Cui  * If accel is zero, default value of 1 is used.
403*01826a49SYabin Cui  *
404*01826a49SYabin Cui  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
405*01826a49SYabin Cui  *          or an error code, which can be tested with ZDICT_isError().
406*01826a49SYabin Cui  *          On success `*parameters` contains the parameters selected.
407*01826a49SYabin Cui  *          See ZDICT_trainFromBuffer() for details on failure modes.
408*01826a49SYabin Cui  * Note: ZDICT_optimizeTrainFromBuffer_fastCover() requires about 6 * 2^f bytes of memory for each thread.
409*01826a49SYabin Cui  */
410*01826a49SYabin Cui ZDICTLIB_STATIC_API size_t ZDICT_optimizeTrainFromBuffer_fastCover(void* dictBuffer,
411*01826a49SYabin Cui                     size_t dictBufferCapacity, const void* samplesBuffer,
412*01826a49SYabin Cui                     const size_t* samplesSizes, unsigned nbSamples,
413*01826a49SYabin Cui                     ZDICT_fastCover_params_t* parameters);
414*01826a49SYabin Cui 
415*01826a49SYabin Cui typedef struct {
416*01826a49SYabin Cui     unsigned selectivityLevel;   /* 0 means default; larger => select more => larger dictionary */
417*01826a49SYabin Cui     ZDICT_params_t zParams;
418*01826a49SYabin Cui } ZDICT_legacy_params_t;
419*01826a49SYabin Cui 
420*01826a49SYabin Cui /*! ZDICT_trainFromBuffer_legacy():
421*01826a49SYabin Cui  *  Train a dictionary from an array of samples.
422*01826a49SYabin Cui  *  Samples must be stored concatenated in a single flat buffer `samplesBuffer`,
423*01826a49SYabin Cui  *  supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order.
424*01826a49SYabin Cui  *  The resulting dictionary will be saved into `dictBuffer`.
425*01826a49SYabin Cui  * `parameters` is optional and can be provided with values set to 0 to mean "default".
426*01826a49SYabin Cui  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
427*01826a49SYabin Cui  *          or an error code, which can be tested with ZDICT_isError().
428*01826a49SYabin Cui  *          See ZDICT_trainFromBuffer() for details on failure modes.
429*01826a49SYabin Cui  *  Tips: In general, a reasonable dictionary has a size of ~ 100 KB.
430*01826a49SYabin Cui  *        It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`.
431*01826a49SYabin Cui  *        In general, it's recommended to provide a few thousands samples, though this can vary a lot.
432*01826a49SYabin Cui  *        It's recommended that total size of all samples be about ~x100 times the target size of dictionary.
433*01826a49SYabin Cui  *  Note: ZDICT_trainFromBuffer_legacy() will send notifications into stderr if instructed to, using notificationLevel>0.
434*01826a49SYabin Cui  */
435*01826a49SYabin Cui ZDICTLIB_STATIC_API size_t ZDICT_trainFromBuffer_legacy(
436*01826a49SYabin Cui     void* dictBuffer, size_t dictBufferCapacity,
437*01826a49SYabin Cui     const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples,
438*01826a49SYabin Cui     ZDICT_legacy_params_t parameters);
439*01826a49SYabin Cui 
440*01826a49SYabin Cui 
441*01826a49SYabin Cui /* Deprecation warnings */
442*01826a49SYabin Cui /* It is generally possible to disable deprecation warnings from compiler,
443*01826a49SYabin Cui    for example with -Wno-deprecated-declarations for gcc
444*01826a49SYabin Cui    or _CRT_SECURE_NO_WARNINGS in Visual.
445*01826a49SYabin Cui    Otherwise, it's also possible to manually define ZDICT_DISABLE_DEPRECATE_WARNINGS */
446*01826a49SYabin Cui #ifdef ZDICT_DISABLE_DEPRECATE_WARNINGS
447*01826a49SYabin Cui #  define ZDICT_DEPRECATED(message) /* disable deprecation warnings */
448*01826a49SYabin Cui #else
449*01826a49SYabin Cui #  define ZDICT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
450*01826a49SYabin Cui #  if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */
451*01826a49SYabin Cui #    define ZDICT_DEPRECATED(message) [[deprecated(message)]]
452*01826a49SYabin Cui #  elif defined(__clang__) || (ZDICT_GCC_VERSION >= 405)
453*01826a49SYabin Cui #    define ZDICT_DEPRECATED(message) __attribute__((deprecated(message)))
454*01826a49SYabin Cui #  elif (ZDICT_GCC_VERSION >= 301)
455*01826a49SYabin Cui #    define ZDICT_DEPRECATED(message) __attribute__((deprecated))
456*01826a49SYabin Cui #  elif defined(_MSC_VER)
457*01826a49SYabin Cui #    define ZDICT_DEPRECATED(message) __declspec(deprecated(message))
458*01826a49SYabin Cui #  else
459*01826a49SYabin Cui #    pragma message("WARNING: You need to implement ZDICT_DEPRECATED for this compiler")
460*01826a49SYabin Cui #    define ZDICT_DEPRECATED(message)
461*01826a49SYabin Cui #  endif
462*01826a49SYabin Cui #endif /* ZDICT_DISABLE_DEPRECATE_WARNINGS */
463*01826a49SYabin Cui 
464*01826a49SYabin Cui ZDICT_DEPRECATED("use ZDICT_finalizeDictionary() instead")
465*01826a49SYabin Cui ZDICTLIB_STATIC_API
466*01826a49SYabin Cui size_t ZDICT_addEntropyTablesFromBuffer(void* dictBuffer, size_t dictContentSize, size_t dictBufferCapacity,
467*01826a49SYabin Cui                                   const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples);
468*01826a49SYabin Cui 
469*01826a49SYabin Cui 
470*01826a49SYabin Cui #endif   /* ZSTD_ZDICT_H_STATIC */
471*01826a49SYabin Cui 
472*01826a49SYabin Cui #if defined (__cplusplus)
473*01826a49SYabin Cui }
474*01826a49SYabin Cui #endif
475