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