Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This source code is licensed under the BSD-style license found in the |
| 6 | * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 9 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 10 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 11 | |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 12 | /* ************************************** |
| 13 | * Compiler Warnings |
| 14 | ****************************************/ |
| 15 | #ifdef _MSC_VER |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 16 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 17 | #endif |
| 18 | |
| 19 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 20 | /*-************************************* |
| 21 | * Includes |
| 22 | ***************************************/ |
Przemyslaw Skibinski | 7a8a03c | 2016-12-21 15:08:44 +0100 | [diff] [blame] | 23 | #include "platform.h" /* Large Files support */ |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 24 | #include "util.h" /* UTIL_getFileSize, UTIL_getTotalFileSize */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 25 | #include <stdlib.h> /* malloc, free */ |
| 26 | #include <string.h> /* memset */ |
| 27 | #include <stdio.h> /* fprintf, fopen, ftello64 */ |
inikep | 3733797 | 2016-05-10 14:22:55 +0200 | [diff] [blame] | 28 | #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */ |
Yann Collet | a3d03a3 | 2016-07-06 16:27:17 +0200 | [diff] [blame] | 29 | #include <errno.h> /* errno */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 30 | |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 31 | #include "mem.h" /* read */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 32 | #include "error_private.h" |
inikep | 23a0889 | 2016-04-22 12:43:18 +0200 | [diff] [blame] | 33 | #include "dibio.h" |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 34 | |
| 35 | |
| 36 | /*-************************************* |
| 37 | * Constants |
| 38 | ***************************************/ |
| 39 | #define KB *(1 <<10) |
| 40 | #define MB *(1 <<20) |
| 41 | #define GB *(1U<<30) |
| 42 | |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 43 | #define SAMPLESIZE_MAX (128 KB) |
| 44 | #define MEMMULT 11 /* rough estimation : memory cost to analyze 1 byte of sample */ |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 45 | #define COVER_MEMMULT 9 /* rough estimation : memory cost to analyze 1 byte of sample */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 46 | static const size_t maxMemory = (sizeof(size_t) == 4) ? (2 GB - 64 MB) : ((size_t)(512 MB) << sizeof(size_t)); |
| 47 | |
| 48 | #define NOISELENGTH 32 |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 49 | |
| 50 | |
| 51 | /*-************************************* |
| 52 | * Console display |
| 53 | ***************************************/ |
| 54 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 55 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 56 | static unsigned g_displayLevel = 0; /* 0 : no display; 1: errors; 2: default; 4: full information */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 57 | |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 58 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 59 | if ((DIB_clockSpan(g_time) > refreshRate) || (g_displayLevel>=4)) \ |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 60 | { g_time = clock(); DISPLAY(__VA_ARGS__); \ |
| 61 | if (g_displayLevel>=4) fflush(stdout); } } |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 62 | static const clock_t refreshRate = CLOCKS_PER_SEC * 2 / 10; |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 63 | static clock_t g_time = 0; |
| 64 | |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 65 | static clock_t DIB_clockSpan(clock_t nPrevious) { return clock() - nPrevious; } |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 66 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 67 | |
| 68 | /*-************************************* |
| 69 | * Exceptions |
| 70 | ***************************************/ |
| 71 | #ifndef DEBUG |
| 72 | # define DEBUG 0 |
| 73 | #endif |
| 74 | #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); |
| 75 | #define EXM_THROW(error, ...) \ |
| 76 | { \ |
| 77 | DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ |
| 78 | DISPLAYLEVEL(1, "Error %i : ", error); \ |
| 79 | DISPLAYLEVEL(1, __VA_ARGS__); \ |
| 80 | DISPLAYLEVEL(1, "\n"); \ |
| 81 | exit(error); \ |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /* ******************************************************** |
| 86 | * Helper functions |
| 87 | **********************************************************/ |
| 88 | unsigned DiB_isError(size_t errorCode) { return ERR_isError(errorCode); } |
| 89 | |
| 90 | const char* DiB_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); } |
| 91 | |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 92 | #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) |
| 93 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 94 | |
| 95 | /* ******************************************************** |
| 96 | * File related operations |
| 97 | **********************************************************/ |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 98 | /** DiB_loadFiles() : |
| 99 | * @return : nb of files effectively loaded into `buffer` */ |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 100 | static unsigned DiB_loadFiles(void* buffer, size_t* bufferSizePtr, |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 101 | size_t* fileSizes, |
| 102 | const char** fileNamesTable, unsigned nbFiles) |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 103 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 104 | char* const buff = (char*)buffer; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 105 | size_t pos = 0; |
| 106 | unsigned n; |
| 107 | |
| 108 | for (n=0; n<nbFiles; n++) { |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 109 | const char* const fileName = fileNamesTable[n]; |
| 110 | unsigned long long const fs64 = UTIL_getFileSize(fileName); |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 111 | size_t const fileSize = (size_t) MIN(fs64, SAMPLESIZE_MAX); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 112 | if (fileSize > *bufferSizePtr-pos) break; |
| 113 | { FILE* const f = fopen(fileName, "rb"); |
| 114 | if (f==NULL) EXM_THROW(10, "zstd: dictBuilder: %s %s ", fileName, strerror(errno)); |
| 115 | DISPLAYUPDATE(2, "Loading %s... \r", fileName); |
| 116 | { size_t const readSize = fread(buff+pos, 1, fileSize, f); |
| 117 | if (readSize != fileSize) EXM_THROW(11, "Pb reading %s", fileName); |
| 118 | pos += readSize; } |
| 119 | fileSizes[n] = fileSize; |
| 120 | fclose(f); |
| 121 | } } |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 122 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 123 | *bufferSizePtr = pos; |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 124 | return n; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 127 | #define DiB_rotl32(x,r) ((x << r) | (x >> (32 - r))) |
| 128 | static U32 DiB_rand(U32* src) |
| 129 | { |
| 130 | static const U32 prime1 = 2654435761U; |
| 131 | static const U32 prime2 = 2246822519U; |
| 132 | U32 rand32 = *src; |
| 133 | rand32 *= prime1; |
| 134 | rand32 ^= prime2; |
| 135 | rand32 = DiB_rotl32(rand32, 13); |
| 136 | *src = rand32; |
| 137 | return rand32 >> 5; |
| 138 | } |
| 139 | |
| 140 | static void DiB_shuffle(const char** fileNamesTable, unsigned nbFiles) { |
| 141 | /* Initialize the pseudorandom number generator */ |
| 142 | U32 seed = 0xFD2FB528; |
| 143 | unsigned i; |
| 144 | for (i = nbFiles - 1; i > 0; --i) { |
| 145 | unsigned const j = DiB_rand(&seed) % (i + 1); |
| 146 | const char* tmp = fileNamesTable[j]; |
| 147 | fileNamesTable[j] = fileNamesTable[i]; |
| 148 | fileNamesTable[i] = tmp; |
| 149 | } |
| 150 | } |
| 151 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 152 | |
| 153 | /*-******************************************************** |
| 154 | * Dictionary training functions |
| 155 | **********************************************************/ |
| 156 | static size_t DiB_findMaxMem(unsigned long long requiredMem) |
| 157 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 158 | size_t const step = 8 MB; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 159 | void* testmem = NULL; |
| 160 | |
| 161 | requiredMem = (((requiredMem >> 23) + 1) << 23); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 162 | requiredMem += step; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 163 | if (requiredMem > maxMemory) requiredMem = maxMemory; |
| 164 | |
| 165 | while (!testmem) { |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 166 | testmem = malloc((size_t)requiredMem); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 167 | requiredMem -= step; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | free(testmem); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 171 | return (size_t)requiredMem; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | |
| 175 | static void DiB_fillNoise(void* buffer, size_t length) |
| 176 | { |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 177 | unsigned const prime1 = 2654435761U; |
| 178 | unsigned const prime2 = 2246822519U; |
| 179 | unsigned acc = prime1; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 180 | size_t p=0;; |
| 181 | |
| 182 | for (p=0; p<length; p++) { |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 183 | acc *= prime2; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 184 | ((unsigned char*)buffer)[p] = (unsigned char)(acc >> 21); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | |
| 189 | static void DiB_saveDict(const char* dictFileName, |
| 190 | const void* buff, size_t buffSize) |
| 191 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 192 | FILE* const f = fopen(dictFileName, "wb"); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 193 | if (f==NULL) EXM_THROW(3, "cannot open %s ", dictFileName); |
| 194 | |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 195 | { size_t const n = fwrite(buff, 1, buffSize, f); |
| 196 | if (n!=buffSize) EXM_THROW(4, "%s : write error", dictFileName) } |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 197 | |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 198 | { size_t const n = (size_t)fclose(f); |
| 199 | if (n!=0) EXM_THROW(5, "%s : flush error", dictFileName) } |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 203 | static int g_tooLargeSamples = 0; |
| 204 | static U64 DiB_getTotalCappedFileSize(const char** fileNamesTable, unsigned nbFiles) |
| 205 | { |
| 206 | U64 total = 0; |
| 207 | unsigned n; |
| 208 | for (n=0; n<nbFiles; n++) { |
| 209 | U64 const fileSize = UTIL_getFileSize(fileNamesTable[n]); |
| 210 | U64 const cappedFileSize = MIN(fileSize, SAMPLESIZE_MAX); |
| 211 | total += cappedFileSize; |
| 212 | g_tooLargeSamples |= (fileSize > 2*SAMPLESIZE_MAX); |
| 213 | } |
| 214 | return total; |
| 215 | } |
| 216 | |
| 217 | |
Yann Collet | 6f3acba | 2016-02-12 20:19:48 +0100 | [diff] [blame] | 218 | /*! ZDICT_trainFromBuffer_unsafe() : |
| 219 | Strictly Internal use only !! |
| 220 | Same as ZDICT_trainFromBuffer_advanced(), but does not control `samplesBuffer`. |
| 221 | `samplesBuffer` must be followed by noisy guard band to avoid out-of-buffer reads. |
| 222 | @return : size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) |
| 223 | or an error code. |
| 224 | */ |
| 225 | size_t ZDICT_trainFromBuffer_unsafe(void* dictBuffer, size_t dictBufferCapacity, |
| 226 | const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples, |
| 227 | ZDICT_params_t parameters); |
| 228 | |
| 229 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 230 | int DiB_trainFromFiles(const char* dictFileName, unsigned maxDictSize, |
| 231 | const char** fileNamesTable, unsigned nbFiles, |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 232 | ZDICT_params_t *params, COVER_params_t *coverParams, |
| 233 | int optimizeCover) |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 234 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 235 | void* const dictBuffer = malloc(maxDictSize); |
| 236 | size_t* const fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t)); |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 237 | unsigned long long const totalSizeToLoad = DiB_getTotalCappedFileSize(fileNamesTable, nbFiles); |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 238 | size_t const maxMem = DiB_findMaxMem(totalSizeToLoad * MEMMULT) / MEMMULT; |
Yann Collet | d46ecb5 | 2016-12-17 16:28:12 +0100 | [diff] [blame] | 239 | size_t benchedSize = (size_t) MIN ((unsigned long long)maxMem, totalSizeToLoad); |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 240 | void* const srcBuffer = malloc(benchedSize+NOISELENGTH); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 241 | int result = 0; |
| 242 | |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 243 | /* Checks */ |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 244 | if (params) g_displayLevel = params->notificationLevel; |
| 245 | else if (coverParams) g_displayLevel = coverParams->notificationLevel; |
| 246 | else EXM_THROW(13, "Neither dictionary algorith selected"); /* should not happen */ |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 247 | if ((!fileSizes) || (!srcBuffer) || (!dictBuffer)) EXM_THROW(12, "not enough memory for DiB_trainFiles"); /* should not happen */ |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame] | 248 | if (g_tooLargeSamples) { |
| 249 | DISPLAYLEVEL(2, "! Warning : some samples are very large \n"); |
| 250 | DISPLAYLEVEL(2, "! Note that dictionary is only useful for small files or beginning of large files. \n"); |
| 251 | DISPLAYLEVEL(2, "! As a consequence, only the first %u bytes of each file are loaded \n", SAMPLESIZE_MAX); |
| 252 | } |
| 253 | if ((nbFiles < 5) || (totalSizeToLoad < 9 * (unsigned long long)maxDictSize)) { |
Yann Collet | 49d105c | 2016-08-18 15:02:11 +0200 | [diff] [blame] | 254 | DISPLAYLEVEL(2, "! Warning : nb of samples too low for proper processing ! \n"); |
| 255 | DISPLAYLEVEL(2, "! Please provide _one file per sample_. \n"); |
| 256 | DISPLAYLEVEL(2, "! Do not concatenate samples together into a single file, \n"); |
| 257 | DISPLAYLEVEL(2, "! as dictBuilder will be unable to find the beginning of each sample, \n"); |
| 258 | DISPLAYLEVEL(2, "! resulting in poor dictionary quality. \n"); |
Yann Collet | dd25a27 | 2016-07-27 12:35:29 +0200 | [diff] [blame] | 259 | } |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 260 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 261 | /* init */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 262 | if (benchedSize < totalSizeToLoad) |
| 263 | DISPLAYLEVEL(1, "Not enough memory; training on %u MB only...\n", (unsigned)(benchedSize >> 20)); |
| 264 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 265 | /* Load input buffer */ |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 266 | DISPLAYLEVEL(3, "Shuffling input files\n"); |
| 267 | DiB_shuffle(fileNamesTable, nbFiles); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 268 | nbFiles = DiB_loadFiles(srcBuffer, &benchedSize, fileSizes, fileNamesTable, nbFiles); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 269 | |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 270 | { |
| 271 | size_t dictSize; |
| 272 | if (params) { |
| 273 | DiB_fillNoise((char*)srcBuffer + benchedSize, NOISELENGTH); /* guard band, for end of buffer condition */ |
| 274 | dictSize = ZDICT_trainFromBuffer_unsafe(dictBuffer, maxDictSize, |
| 275 | srcBuffer, fileSizes, nbFiles, |
| 276 | *params); |
| 277 | } else if (optimizeCover) { |
| 278 | dictSize = COVER_optimizeTrainFromBuffer( |
| 279 | dictBuffer, maxDictSize, srcBuffer, fileSizes, nbFiles, |
| 280 | coverParams); |
| 281 | if (!ZDICT_isError(dictSize)) { |
Nick Terrell | 3a1fefc | 2017-01-02 12:40:43 -0800 | [diff] [blame^] | 282 | DISPLAYLEVEL(2, "k=%u\nd=%u\nsteps=%u\n", coverParams->k, coverParams->d, coverParams->steps); |
Nick Terrell | df8415c | 2016-12-31 21:08:24 -0800 | [diff] [blame] | 283 | } |
| 284 | } else { |
| 285 | dictSize = COVER_trainFromBuffer(dictBuffer, maxDictSize, |
| 286 | srcBuffer, fileSizes, nbFiles, |
| 287 | *coverParams); |
| 288 | } |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 289 | if (ZDICT_isError(dictSize)) { |
| 290 | DISPLAYLEVEL(1, "dictionary training failed : %s \n", ZDICT_getErrorName(dictSize)); /* should not happen */ |
| 291 | result = 1; |
| 292 | goto _cleanup; |
| 293 | } |
| 294 | /* save dict */ |
| 295 | DISPLAYLEVEL(2, "Save dictionary of size %u into file %s \n", (U32)dictSize, dictFileName); |
| 296 | DiB_saveDict(dictFileName, dictBuffer, dictSize); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 297 | } |
| 298 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 299 | /* clean up */ |
| 300 | _cleanup: |
| 301 | free(srcBuffer); |
| 302 | free(dictBuffer); |
| 303 | free(fileSizes); |
| 304 | return result; |
| 305 | } |