1*22ce4affSfengbojiang /*
2*22ce4affSfengbojiang * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3*22ce4affSfengbojiang * All rights reserved.
4*22ce4affSfengbojiang *
5*22ce4affSfengbojiang * This source code is licensed under both the BSD-style license (found in the
6*22ce4affSfengbojiang * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*22ce4affSfengbojiang * in the COPYING file in the root directory of this source tree).
8*22ce4affSfengbojiang * You may select, at your option, one of the above-listed licenses.
9*22ce4affSfengbojiang */
10*22ce4affSfengbojiang
11*22ce4affSfengbojiang
12*22ce4affSfengbojiang
13*22ce4affSfengbojiang /* **************************************
14*22ce4affSfengbojiang * Compiler Warnings
15*22ce4affSfengbojiang ****************************************/
16*22ce4affSfengbojiang #ifdef _MSC_VER
17*22ce4affSfengbojiang # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
18*22ce4affSfengbojiang #endif
19*22ce4affSfengbojiang
20*22ce4affSfengbojiang
21*22ce4affSfengbojiang /*-*************************************
22*22ce4affSfengbojiang * Includes
23*22ce4affSfengbojiang ***************************************/
24*22ce4affSfengbojiang #include "platform.h" /* Large Files support */
25*22ce4affSfengbojiang #include "util.h" /* UTIL_getFileSize, UTIL_getTotalFileSize */
26*22ce4affSfengbojiang #include <stdlib.h> /* malloc, free */
27*22ce4affSfengbojiang #include <string.h> /* memset */
28*22ce4affSfengbojiang #include <stdio.h> /* fprintf, fopen, ftello64 */
29*22ce4affSfengbojiang #include <errno.h> /* errno */
30*22ce4affSfengbojiang #include <assert.h>
31*22ce4affSfengbojiang
32*22ce4affSfengbojiang #include "timefn.h" /* UTIL_time_t, UTIL_clockSpanMicro, UTIL_getTime */
33*22ce4affSfengbojiang #include "../lib/common/mem.h" /* read */
34*22ce4affSfengbojiang #include "../lib/common/error_private.h"
35*22ce4affSfengbojiang #include "dibio.h"
36*22ce4affSfengbojiang
37*22ce4affSfengbojiang
38*22ce4affSfengbojiang /*-*************************************
39*22ce4affSfengbojiang * Constants
40*22ce4affSfengbojiang ***************************************/
41*22ce4affSfengbojiang #define KB *(1 <<10)
42*22ce4affSfengbojiang #define MB *(1 <<20)
43*22ce4affSfengbojiang #define GB *(1U<<30)
44*22ce4affSfengbojiang
45*22ce4affSfengbojiang #define SAMPLESIZE_MAX (128 KB)
46*22ce4affSfengbojiang #define MEMMULT 11 /* rough estimation : memory cost to analyze 1 byte of sample */
47*22ce4affSfengbojiang #define COVER_MEMMULT 9 /* rough estimation : memory cost to analyze 1 byte of sample */
48*22ce4affSfengbojiang #define FASTCOVER_MEMMULT 1 /* rough estimation : memory cost to analyze 1 byte of sample */
49*22ce4affSfengbojiang static const size_t g_maxMemory = (sizeof(size_t) == 4) ? (2 GB - 64 MB) : ((size_t)(512 MB) << sizeof(size_t));
50*22ce4affSfengbojiang
51*22ce4affSfengbojiang #define NOISELENGTH 32
52*22ce4affSfengbojiang
53*22ce4affSfengbojiang
54*22ce4affSfengbojiang /*-*************************************
55*22ce4affSfengbojiang * Console display
56*22ce4affSfengbojiang ***************************************/
57*22ce4affSfengbojiang #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
58*22ce4affSfengbojiang #define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); }
59*22ce4affSfengbojiang
60*22ce4affSfengbojiang static const U64 g_refreshRate = SEC_TO_MICRO / 6;
61*22ce4affSfengbojiang static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
62*22ce4affSfengbojiang
63*22ce4affSfengbojiang #define DISPLAYUPDATE(l, ...) { if (displayLevel>=l) { \
64*22ce4affSfengbojiang if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (displayLevel>=4)) \
65*22ce4affSfengbojiang { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \
66*22ce4affSfengbojiang if (displayLevel>=4) fflush(stderr); } } }
67*22ce4affSfengbojiang
68*22ce4affSfengbojiang /*-*************************************
69*22ce4affSfengbojiang * Exceptions
70*22ce4affSfengbojiang ***************************************/
71*22ce4affSfengbojiang #ifndef DEBUG
72*22ce4affSfengbojiang # define DEBUG 0
73*22ce4affSfengbojiang #endif
74*22ce4affSfengbojiang #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
75*22ce4affSfengbojiang #define EXM_THROW(error, ...) \
76*22ce4affSfengbojiang { \
77*22ce4affSfengbojiang DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
78*22ce4affSfengbojiang DISPLAY("Error %i : ", error); \
79*22ce4affSfengbojiang DISPLAY(__VA_ARGS__); \
80*22ce4affSfengbojiang DISPLAY("\n"); \
81*22ce4affSfengbojiang exit(error); \
82*22ce4affSfengbojiang }
83*22ce4affSfengbojiang
84*22ce4affSfengbojiang
85*22ce4affSfengbojiang /* ********************************************************
86*22ce4affSfengbojiang * Helper functions
87*22ce4affSfengbojiang **********************************************************/
88*22ce4affSfengbojiang #undef MIN
89*22ce4affSfengbojiang #define MIN(a,b) ((a) < (b) ? (a) : (b))
90*22ce4affSfengbojiang
91*22ce4affSfengbojiang
92*22ce4affSfengbojiang /* ********************************************************
93*22ce4affSfengbojiang * File related operations
94*22ce4affSfengbojiang **********************************************************/
95*22ce4affSfengbojiang /** DiB_loadFiles() :
96*22ce4affSfengbojiang * load samples from files listed in fileNamesTable into buffer.
97*22ce4affSfengbojiang * works even if buffer is too small to load all samples.
98*22ce4affSfengbojiang * Also provides the size of each sample into sampleSizes table
99*22ce4affSfengbojiang * which must be sized correctly, using DiB_fileStats().
100*22ce4affSfengbojiang * @return : nb of samples effectively loaded into `buffer`
101*22ce4affSfengbojiang * *bufferSizePtr is modified, it provides the amount data loaded within buffer.
102*22ce4affSfengbojiang * sampleSizes is filled with the size of each sample.
103*22ce4affSfengbojiang */
DiB_loadFiles(void * buffer,size_t * bufferSizePtr,size_t * sampleSizes,unsigned sstSize,const char ** fileNamesTable,unsigned nbFiles,size_t targetChunkSize,unsigned displayLevel)104*22ce4affSfengbojiang static unsigned DiB_loadFiles(void* buffer, size_t* bufferSizePtr,
105*22ce4affSfengbojiang size_t* sampleSizes, unsigned sstSize,
106*22ce4affSfengbojiang const char** fileNamesTable, unsigned nbFiles, size_t targetChunkSize,
107*22ce4affSfengbojiang unsigned displayLevel)
108*22ce4affSfengbojiang {
109*22ce4affSfengbojiang char* const buff = (char*)buffer;
110*22ce4affSfengbojiang size_t pos = 0;
111*22ce4affSfengbojiang unsigned nbLoadedChunks = 0, fileIndex;
112*22ce4affSfengbojiang
113*22ce4affSfengbojiang for (fileIndex=0; fileIndex<nbFiles; fileIndex++) {
114*22ce4affSfengbojiang const char* const fileName = fileNamesTable[fileIndex];
115*22ce4affSfengbojiang unsigned long long const fs64 = UTIL_getFileSize(fileName);
116*22ce4affSfengbojiang unsigned long long remainingToLoad = (fs64 == UTIL_FILESIZE_UNKNOWN) ? 0 : fs64;
117*22ce4affSfengbojiang U32 const nbChunks = targetChunkSize ? (U32)((fs64 + (targetChunkSize-1)) / targetChunkSize) : 1;
118*22ce4affSfengbojiang U64 const chunkSize = targetChunkSize ? MIN(targetChunkSize, fs64) : fs64;
119*22ce4affSfengbojiang size_t const maxChunkSize = (size_t)MIN(chunkSize, SAMPLESIZE_MAX);
120*22ce4affSfengbojiang U32 cnb;
121*22ce4affSfengbojiang FILE* const f = fopen(fileName, "rb");
122*22ce4affSfengbojiang if (f==NULL) EXM_THROW(10, "zstd: dictBuilder: %s %s ", fileName, strerror(errno));
123*22ce4affSfengbojiang DISPLAYUPDATE(2, "Loading %s... \r", fileName);
124*22ce4affSfengbojiang for (cnb=0; cnb<nbChunks; cnb++) {
125*22ce4affSfengbojiang size_t const toLoad = (size_t)MIN(maxChunkSize, remainingToLoad);
126*22ce4affSfengbojiang if (toLoad > *bufferSizePtr-pos) break;
127*22ce4affSfengbojiang { size_t const readSize = fread(buff+pos, 1, toLoad, f);
128*22ce4affSfengbojiang if (readSize != toLoad) EXM_THROW(11, "Pb reading %s", fileName);
129*22ce4affSfengbojiang pos += readSize;
130*22ce4affSfengbojiang sampleSizes[nbLoadedChunks++] = toLoad;
131*22ce4affSfengbojiang remainingToLoad -= targetChunkSize;
132*22ce4affSfengbojiang if (nbLoadedChunks == sstSize) { /* no more space left in sampleSizes table */
133*22ce4affSfengbojiang fileIndex = nbFiles; /* stop there */
134*22ce4affSfengbojiang break;
135*22ce4affSfengbojiang }
136*22ce4affSfengbojiang if (toLoad < targetChunkSize) {
137*22ce4affSfengbojiang fseek(f, (long)(targetChunkSize - toLoad), SEEK_CUR);
138*22ce4affSfengbojiang } } }
139*22ce4affSfengbojiang fclose(f);
140*22ce4affSfengbojiang }
141*22ce4affSfengbojiang DISPLAYLEVEL(2, "\r%79s\r", "");
142*22ce4affSfengbojiang *bufferSizePtr = pos;
143*22ce4affSfengbojiang DISPLAYLEVEL(4, "loaded : %u KB \n", (unsigned)(pos >> 10))
144*22ce4affSfengbojiang return nbLoadedChunks;
145*22ce4affSfengbojiang }
146*22ce4affSfengbojiang
147*22ce4affSfengbojiang #define DiB_rotl32(x,r) ((x << r) | (x >> (32 - r)))
DiB_rand(U32 * src)148*22ce4affSfengbojiang static U32 DiB_rand(U32* src)
149*22ce4affSfengbojiang {
150*22ce4affSfengbojiang static const U32 prime1 = 2654435761U;
151*22ce4affSfengbojiang static const U32 prime2 = 2246822519U;
152*22ce4affSfengbojiang U32 rand32 = *src;
153*22ce4affSfengbojiang rand32 *= prime1;
154*22ce4affSfengbojiang rand32 ^= prime2;
155*22ce4affSfengbojiang rand32 = DiB_rotl32(rand32, 13);
156*22ce4affSfengbojiang *src = rand32;
157*22ce4affSfengbojiang return rand32 >> 5;
158*22ce4affSfengbojiang }
159*22ce4affSfengbojiang
160*22ce4affSfengbojiang /* DiB_shuffle() :
161*22ce4affSfengbojiang * shuffle a table of file names in a semi-random way
162*22ce4affSfengbojiang * It improves dictionary quality by reducing "locality" impact, so if sample set is very large,
163*22ce4affSfengbojiang * it will load random elements from it, instead of just the first ones. */
DiB_shuffle(const char ** fileNamesTable,unsigned nbFiles)164*22ce4affSfengbojiang static void DiB_shuffle(const char** fileNamesTable, unsigned nbFiles) {
165*22ce4affSfengbojiang U32 seed = 0xFD2FB528;
166*22ce4affSfengbojiang unsigned i;
167*22ce4affSfengbojiang assert(nbFiles >= 1);
168*22ce4affSfengbojiang for (i = nbFiles - 1; i > 0; --i) {
169*22ce4affSfengbojiang unsigned const j = DiB_rand(&seed) % (i + 1);
170*22ce4affSfengbojiang const char* const tmp = fileNamesTable[j];
171*22ce4affSfengbojiang fileNamesTable[j] = fileNamesTable[i];
172*22ce4affSfengbojiang fileNamesTable[i] = tmp;
173*22ce4affSfengbojiang }
174*22ce4affSfengbojiang }
175*22ce4affSfengbojiang
176*22ce4affSfengbojiang
177*22ce4affSfengbojiang /*-********************************************************
178*22ce4affSfengbojiang * Dictionary training functions
179*22ce4affSfengbojiang **********************************************************/
DiB_findMaxMem(unsigned long long requiredMem)180*22ce4affSfengbojiang static size_t DiB_findMaxMem(unsigned long long requiredMem)
181*22ce4affSfengbojiang {
182*22ce4affSfengbojiang size_t const step = 8 MB;
183*22ce4affSfengbojiang void* testmem = NULL;
184*22ce4affSfengbojiang
185*22ce4affSfengbojiang requiredMem = (((requiredMem >> 23) + 1) << 23);
186*22ce4affSfengbojiang requiredMem += step;
187*22ce4affSfengbojiang if (requiredMem > g_maxMemory) requiredMem = g_maxMemory;
188*22ce4affSfengbojiang
189*22ce4affSfengbojiang while (!testmem) {
190*22ce4affSfengbojiang testmem = malloc((size_t)requiredMem);
191*22ce4affSfengbojiang requiredMem -= step;
192*22ce4affSfengbojiang }
193*22ce4affSfengbojiang
194*22ce4affSfengbojiang free(testmem);
195*22ce4affSfengbojiang return (size_t)requiredMem;
196*22ce4affSfengbojiang }
197*22ce4affSfengbojiang
198*22ce4affSfengbojiang
DiB_fillNoise(void * buffer,size_t length)199*22ce4affSfengbojiang static void DiB_fillNoise(void* buffer, size_t length)
200*22ce4affSfengbojiang {
201*22ce4affSfengbojiang unsigned const prime1 = 2654435761U;
202*22ce4affSfengbojiang unsigned const prime2 = 2246822519U;
203*22ce4affSfengbojiang unsigned acc = prime1;
204*22ce4affSfengbojiang size_t p=0;
205*22ce4affSfengbojiang
206*22ce4affSfengbojiang for (p=0; p<length; p++) {
207*22ce4affSfengbojiang acc *= prime2;
208*22ce4affSfengbojiang ((unsigned char*)buffer)[p] = (unsigned char)(acc >> 21);
209*22ce4affSfengbojiang }
210*22ce4affSfengbojiang }
211*22ce4affSfengbojiang
212*22ce4affSfengbojiang
DiB_saveDict(const char * dictFileName,const void * buff,size_t buffSize)213*22ce4affSfengbojiang static void DiB_saveDict(const char* dictFileName,
214*22ce4affSfengbojiang const void* buff, size_t buffSize)
215*22ce4affSfengbojiang {
216*22ce4affSfengbojiang FILE* const f = fopen(dictFileName, "wb");
217*22ce4affSfengbojiang if (f==NULL) EXM_THROW(3, "cannot open %s ", dictFileName);
218*22ce4affSfengbojiang
219*22ce4affSfengbojiang { size_t const n = fwrite(buff, 1, buffSize, f);
220*22ce4affSfengbojiang if (n!=buffSize) EXM_THROW(4, "%s : write error", dictFileName) }
221*22ce4affSfengbojiang
222*22ce4affSfengbojiang { size_t const n = (size_t)fclose(f);
223*22ce4affSfengbojiang if (n!=0) EXM_THROW(5, "%s : flush error", dictFileName) }
224*22ce4affSfengbojiang }
225*22ce4affSfengbojiang
226*22ce4affSfengbojiang
227*22ce4affSfengbojiang typedef struct {
228*22ce4affSfengbojiang U64 totalSizeToLoad;
229*22ce4affSfengbojiang unsigned oneSampleTooLarge;
230*22ce4affSfengbojiang unsigned nbSamples;
231*22ce4affSfengbojiang } fileStats;
232*22ce4affSfengbojiang
233*22ce4affSfengbojiang /*! DiB_fileStats() :
234*22ce4affSfengbojiang * Given a list of files, and a chunkSize (0 == no chunk, whole files)
235*22ce4affSfengbojiang * provides the amount of data to be loaded and the resulting nb of samples.
236*22ce4affSfengbojiang * This is useful primarily for allocation purpose => sample buffer, and sample sizes table.
237*22ce4affSfengbojiang */
DiB_fileStats(const char ** fileNamesTable,unsigned nbFiles,size_t chunkSize,unsigned displayLevel)238*22ce4affSfengbojiang static fileStats DiB_fileStats(const char** fileNamesTable, unsigned nbFiles, size_t chunkSize, unsigned displayLevel)
239*22ce4affSfengbojiang {
240*22ce4affSfengbojiang fileStats fs;
241*22ce4affSfengbojiang unsigned n;
242*22ce4affSfengbojiang memset(&fs, 0, sizeof(fs));
243*22ce4affSfengbojiang for (n=0; n<nbFiles; n++) {
244*22ce4affSfengbojiang U64 const fileSize = UTIL_getFileSize(fileNamesTable[n]);
245*22ce4affSfengbojiang U64 const srcSize = (fileSize == UTIL_FILESIZE_UNKNOWN) ? 0 : fileSize;
246*22ce4affSfengbojiang U32 const nbSamples = (U32)(chunkSize ? (srcSize + (chunkSize-1)) / chunkSize : 1);
247*22ce4affSfengbojiang U64 const chunkToLoad = chunkSize ? MIN(chunkSize, srcSize) : srcSize;
248*22ce4affSfengbojiang size_t const cappedChunkSize = (size_t)MIN(chunkToLoad, SAMPLESIZE_MAX);
249*22ce4affSfengbojiang fs.totalSizeToLoad += cappedChunkSize * nbSamples;
250*22ce4affSfengbojiang fs.oneSampleTooLarge |= (chunkSize > 2*SAMPLESIZE_MAX);
251*22ce4affSfengbojiang fs.nbSamples += nbSamples;
252*22ce4affSfengbojiang }
253*22ce4affSfengbojiang DISPLAYLEVEL(4, "Preparing to load : %u KB \n", (unsigned)(fs.totalSizeToLoad >> 10));
254*22ce4affSfengbojiang return fs;
255*22ce4affSfengbojiang }
256*22ce4affSfengbojiang
257*22ce4affSfengbojiang
258*22ce4affSfengbojiang /*! ZDICT_trainFromBuffer_unsafe_legacy() :
259*22ce4affSfengbojiang Strictly Internal use only !!
260*22ce4affSfengbojiang Same as ZDICT_trainFromBuffer_legacy(), but does not control `samplesBuffer`.
261*22ce4affSfengbojiang `samplesBuffer` must be followed by noisy guard band to avoid out-of-buffer reads.
262*22ce4affSfengbojiang @return : size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
263*22ce4affSfengbojiang or an error code.
264*22ce4affSfengbojiang */
265*22ce4affSfengbojiang size_t ZDICT_trainFromBuffer_unsafe_legacy(void* dictBuffer, size_t dictBufferCapacity,
266*22ce4affSfengbojiang const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples,
267*22ce4affSfengbojiang ZDICT_legacy_params_t parameters);
268*22ce4affSfengbojiang
269*22ce4affSfengbojiang
DiB_trainFromFiles(const char * dictFileName,unsigned maxDictSize,const char ** fileNamesTable,unsigned nbFiles,size_t chunkSize,ZDICT_legacy_params_t * params,ZDICT_cover_params_t * coverParams,ZDICT_fastCover_params_t * fastCoverParams,int optimize)270*22ce4affSfengbojiang int DiB_trainFromFiles(const char* dictFileName, unsigned maxDictSize,
271*22ce4affSfengbojiang const char** fileNamesTable, unsigned nbFiles, size_t chunkSize,
272*22ce4affSfengbojiang ZDICT_legacy_params_t* params, ZDICT_cover_params_t* coverParams,
273*22ce4affSfengbojiang ZDICT_fastCover_params_t* fastCoverParams, int optimize)
274*22ce4affSfengbojiang {
275*22ce4affSfengbojiang unsigned const displayLevel = params ? params->zParams.notificationLevel :
276*22ce4affSfengbojiang coverParams ? coverParams->zParams.notificationLevel :
277*22ce4affSfengbojiang fastCoverParams ? fastCoverParams->zParams.notificationLevel :
278*22ce4affSfengbojiang 0; /* should never happen */
279*22ce4affSfengbojiang void* const dictBuffer = malloc(maxDictSize);
280*22ce4affSfengbojiang fileStats const fs = DiB_fileStats(fileNamesTable, nbFiles, chunkSize, displayLevel);
281*22ce4affSfengbojiang size_t* const sampleSizes = (size_t*)malloc(fs.nbSamples * sizeof(size_t));
282*22ce4affSfengbojiang size_t const memMult = params ? MEMMULT :
283*22ce4affSfengbojiang coverParams ? COVER_MEMMULT:
284*22ce4affSfengbojiang FASTCOVER_MEMMULT;
285*22ce4affSfengbojiang size_t const maxMem = DiB_findMaxMem(fs.totalSizeToLoad * memMult) / memMult;
286*22ce4affSfengbojiang size_t loadedSize = (size_t) MIN ((unsigned long long)maxMem, fs.totalSizeToLoad);
287*22ce4affSfengbojiang void* const srcBuffer = malloc(loadedSize+NOISELENGTH);
288*22ce4affSfengbojiang int result = 0;
289*22ce4affSfengbojiang
290*22ce4affSfengbojiang /* Checks */
291*22ce4affSfengbojiang if ((!sampleSizes) || (!srcBuffer) || (!dictBuffer))
292*22ce4affSfengbojiang EXM_THROW(12, "not enough memory for DiB_trainFiles"); /* should not happen */
293*22ce4affSfengbojiang if (fs.oneSampleTooLarge) {
294*22ce4affSfengbojiang DISPLAYLEVEL(2, "! Warning : some sample(s) are very large \n");
295*22ce4affSfengbojiang DISPLAYLEVEL(2, "! Note that dictionary is only useful for small samples. \n");
296*22ce4affSfengbojiang DISPLAYLEVEL(2, "! As a consequence, only the first %u bytes of each sample are loaded \n", SAMPLESIZE_MAX);
297*22ce4affSfengbojiang }
298*22ce4affSfengbojiang if (fs.nbSamples < 5) {
299*22ce4affSfengbojiang DISPLAYLEVEL(2, "! Warning : nb of samples too low for proper processing ! \n");
300*22ce4affSfengbojiang DISPLAYLEVEL(2, "! Please provide _one file per sample_. \n");
301*22ce4affSfengbojiang DISPLAYLEVEL(2, "! Alternatively, split files into fixed-size blocks representative of samples, with -B# \n");
302*22ce4affSfengbojiang EXM_THROW(14, "nb of samples too low"); /* we now clearly forbid this case */
303*22ce4affSfengbojiang }
304*22ce4affSfengbojiang if (fs.totalSizeToLoad < (unsigned long long)maxDictSize * 8) {
305*22ce4affSfengbojiang DISPLAYLEVEL(2, "! Warning : data size of samples too small for target dictionary size \n");
306*22ce4affSfengbojiang DISPLAYLEVEL(2, "! Samples should be about 100x larger than target dictionary size \n");
307*22ce4affSfengbojiang }
308*22ce4affSfengbojiang
309*22ce4affSfengbojiang /* init */
310*22ce4affSfengbojiang if (loadedSize < fs.totalSizeToLoad)
311*22ce4affSfengbojiang DISPLAYLEVEL(1, "Not enough memory; training on %u MB only...\n", (unsigned)(loadedSize >> 20));
312*22ce4affSfengbojiang
313*22ce4affSfengbojiang /* Load input buffer */
314*22ce4affSfengbojiang DISPLAYLEVEL(3, "Shuffling input files\n");
315*22ce4affSfengbojiang DiB_shuffle(fileNamesTable, nbFiles);
316*22ce4affSfengbojiang
317*22ce4affSfengbojiang DiB_loadFiles(srcBuffer, &loadedSize, sampleSizes, fs.nbSamples, fileNamesTable, nbFiles, chunkSize, displayLevel);
318*22ce4affSfengbojiang
319*22ce4affSfengbojiang { size_t dictSize;
320*22ce4affSfengbojiang if (params) {
321*22ce4affSfengbojiang DiB_fillNoise((char*)srcBuffer + loadedSize, NOISELENGTH); /* guard band, for end of buffer condition */
322*22ce4affSfengbojiang dictSize = ZDICT_trainFromBuffer_unsafe_legacy(dictBuffer, maxDictSize,
323*22ce4affSfengbojiang srcBuffer, sampleSizes, fs.nbSamples,
324*22ce4affSfengbojiang *params);
325*22ce4affSfengbojiang } else if (coverParams) {
326*22ce4affSfengbojiang if (optimize) {
327*22ce4affSfengbojiang dictSize = ZDICT_optimizeTrainFromBuffer_cover(dictBuffer, maxDictSize,
328*22ce4affSfengbojiang srcBuffer, sampleSizes, fs.nbSamples,
329*22ce4affSfengbojiang coverParams);
330*22ce4affSfengbojiang if (!ZDICT_isError(dictSize)) {
331*22ce4affSfengbojiang unsigned splitPercentage = (unsigned)(coverParams->splitPoint * 100);
332*22ce4affSfengbojiang DISPLAYLEVEL(2, "k=%u\nd=%u\nsteps=%u\nsplit=%u\n", coverParams->k, coverParams->d,
333*22ce4affSfengbojiang coverParams->steps, splitPercentage);
334*22ce4affSfengbojiang }
335*22ce4affSfengbojiang } else {
336*22ce4affSfengbojiang dictSize = ZDICT_trainFromBuffer_cover(dictBuffer, maxDictSize, srcBuffer,
337*22ce4affSfengbojiang sampleSizes, fs.nbSamples, *coverParams);
338*22ce4affSfengbojiang }
339*22ce4affSfengbojiang } else {
340*22ce4affSfengbojiang assert(fastCoverParams != NULL);
341*22ce4affSfengbojiang if (optimize) {
342*22ce4affSfengbojiang dictSize = ZDICT_optimizeTrainFromBuffer_fastCover(dictBuffer, maxDictSize,
343*22ce4affSfengbojiang srcBuffer, sampleSizes, fs.nbSamples,
344*22ce4affSfengbojiang fastCoverParams);
345*22ce4affSfengbojiang if (!ZDICT_isError(dictSize)) {
346*22ce4affSfengbojiang unsigned splitPercentage = (unsigned)(fastCoverParams->splitPoint * 100);
347*22ce4affSfengbojiang DISPLAYLEVEL(2, "k=%u\nd=%u\nf=%u\nsteps=%u\nsplit=%u\naccel=%u\n", fastCoverParams->k,
348*22ce4affSfengbojiang fastCoverParams->d, fastCoverParams->f, fastCoverParams->steps, splitPercentage,
349*22ce4affSfengbojiang fastCoverParams->accel);
350*22ce4affSfengbojiang }
351*22ce4affSfengbojiang } else {
352*22ce4affSfengbojiang dictSize = ZDICT_trainFromBuffer_fastCover(dictBuffer, maxDictSize, srcBuffer,
353*22ce4affSfengbojiang sampleSizes, fs.nbSamples, *fastCoverParams);
354*22ce4affSfengbojiang }
355*22ce4affSfengbojiang }
356*22ce4affSfengbojiang if (ZDICT_isError(dictSize)) {
357*22ce4affSfengbojiang DISPLAYLEVEL(1, "dictionary training failed : %s \n", ZDICT_getErrorName(dictSize)); /* should not happen */
358*22ce4affSfengbojiang result = 1;
359*22ce4affSfengbojiang goto _cleanup;
360*22ce4affSfengbojiang }
361*22ce4affSfengbojiang /* save dict */
362*22ce4affSfengbojiang DISPLAYLEVEL(2, "Save dictionary of size %u into file %s \n", (unsigned)dictSize, dictFileName);
363*22ce4affSfengbojiang DiB_saveDict(dictFileName, dictBuffer, dictSize);
364*22ce4affSfengbojiang }
365*22ce4affSfengbojiang
366*22ce4affSfengbojiang /* clean up */
367*22ce4affSfengbojiang _cleanup:
368*22ce4affSfengbojiang free(srcBuffer);
369*22ce4affSfengbojiang free(sampleSizes);
370*22ce4affSfengbojiang free(dictBuffer);
371*22ce4affSfengbojiang return result;
372*22ce4affSfengbojiang }
373