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 12*3117ece4Schristos /*-************************************** 13*3117ece4Schristos * Tuning parameters 14*3117ece4Schristos ****************************************/ 15*3117ece4Schristos #define MINRATIO 4 /* minimum nb of apparition to be selected in dictionary */ 16*3117ece4Schristos #define ZDICT_MAX_SAMPLES_SIZE (2000U << 20) 17*3117ece4Schristos #define ZDICT_MIN_SAMPLES_SIZE (ZDICT_CONTENTSIZE_MIN * MINRATIO) 18*3117ece4Schristos 19*3117ece4Schristos 20*3117ece4Schristos /*-************************************** 21*3117ece4Schristos * Compiler Options 22*3117ece4Schristos ****************************************/ 23*3117ece4Schristos /* Unix Large Files support (>4GB) */ 24*3117ece4Schristos #define _FILE_OFFSET_BITS 64 25*3117ece4Schristos #if (defined(__sun__) && (!defined(__LP64__))) /* Sun Solaris 32-bits requires specific definitions */ 26*3117ece4Schristos # ifndef _LARGEFILE_SOURCE 27*3117ece4Schristos # define _LARGEFILE_SOURCE 28*3117ece4Schristos # endif 29*3117ece4Schristos #elif ! defined(__LP64__) /* No point defining Large file for 64 bit */ 30*3117ece4Schristos # ifndef _LARGEFILE64_SOURCE 31*3117ece4Schristos # define _LARGEFILE64_SOURCE 32*3117ece4Schristos # endif 33*3117ece4Schristos #endif 34*3117ece4Schristos 35*3117ece4Schristos 36*3117ece4Schristos /*-************************************* 37*3117ece4Schristos * Dependencies 38*3117ece4Schristos ***************************************/ 39*3117ece4Schristos #include <stdlib.h> /* malloc, free */ 40*3117ece4Schristos #include <string.h> /* memset */ 41*3117ece4Schristos #include <stdio.h> /* fprintf, fopen, ftello64 */ 42*3117ece4Schristos #include <time.h> /* clock */ 43*3117ece4Schristos 44*3117ece4Schristos #ifndef ZDICT_STATIC_LINKING_ONLY 45*3117ece4Schristos # define ZDICT_STATIC_LINKING_ONLY 46*3117ece4Schristos #endif 47*3117ece4Schristos 48*3117ece4Schristos #include "../common/mem.h" /* read */ 49*3117ece4Schristos #include "../common/fse.h" /* FSE_normalizeCount, FSE_writeNCount */ 50*3117ece4Schristos #include "../common/huf.h" /* HUF_buildCTable, HUF_writeCTable */ 51*3117ece4Schristos #include "../common/zstd_internal.h" /* includes zstd.h */ 52*3117ece4Schristos #include "../common/xxhash.h" /* XXH64 */ 53*3117ece4Schristos #include "../compress/zstd_compress_internal.h" /* ZSTD_loadCEntropy() */ 54*3117ece4Schristos #include "../zdict.h" 55*3117ece4Schristos #include "divsufsort.h" 56*3117ece4Schristos #include "../common/bits.h" /* ZSTD_NbCommonBytes */ 57*3117ece4Schristos 58*3117ece4Schristos 59*3117ece4Schristos /*-************************************* 60*3117ece4Schristos * Constants 61*3117ece4Schristos ***************************************/ 62*3117ece4Schristos #define KB *(1 <<10) 63*3117ece4Schristos #define MB *(1 <<20) 64*3117ece4Schristos #define GB *(1U<<30) 65*3117ece4Schristos 66*3117ece4Schristos #define DICTLISTSIZE_DEFAULT 10000 67*3117ece4Schristos 68*3117ece4Schristos #define NOISELENGTH 32 69*3117ece4Schristos 70*3117ece4Schristos static const U32 g_selectivity_default = 9; 71*3117ece4Schristos 72*3117ece4Schristos 73*3117ece4Schristos /*-************************************* 74*3117ece4Schristos * Console display 75*3117ece4Schristos ***************************************/ 76*3117ece4Schristos #undef DISPLAY 77*3117ece4Schristos #define DISPLAY(...) do { fprintf(stderr, __VA_ARGS__); fflush( stderr ); } while (0) 78*3117ece4Schristos #undef DISPLAYLEVEL 79*3117ece4Schristos #define DISPLAYLEVEL(l, ...) do { if (notificationLevel>=l) { DISPLAY(__VA_ARGS__); } } while (0) /* 0 : no display; 1: errors; 2: default; 3: details; 4: debug */ 80*3117ece4Schristos 81*3117ece4Schristos static clock_t ZDICT_clockSpan(clock_t nPrevious) { return clock() - nPrevious; } 82*3117ece4Schristos 83*3117ece4Schristos static void ZDICT_printHex(const void* ptr, size_t length) 84*3117ece4Schristos { 85*3117ece4Schristos const BYTE* const b = (const BYTE*)ptr; 86*3117ece4Schristos size_t u; 87*3117ece4Schristos for (u=0; u<length; u++) { 88*3117ece4Schristos BYTE c = b[u]; 89*3117ece4Schristos if (c<32 || c>126) c = '.'; /* non-printable char */ 90*3117ece4Schristos DISPLAY("%c", c); 91*3117ece4Schristos } 92*3117ece4Schristos } 93*3117ece4Schristos 94*3117ece4Schristos 95*3117ece4Schristos /*-******************************************************** 96*3117ece4Schristos * Helper functions 97*3117ece4Schristos **********************************************************/ 98*3117ece4Schristos unsigned ZDICT_isError(size_t errorCode) { return ERR_isError(errorCode); } 99*3117ece4Schristos 100*3117ece4Schristos const char* ZDICT_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); } 101*3117ece4Schristos 102*3117ece4Schristos unsigned ZDICT_getDictID(const void* dictBuffer, size_t dictSize) 103*3117ece4Schristos { 104*3117ece4Schristos if (dictSize < 8) return 0; 105*3117ece4Schristos if (MEM_readLE32(dictBuffer) != ZSTD_MAGIC_DICTIONARY) return 0; 106*3117ece4Schristos return MEM_readLE32((const char*)dictBuffer + 4); 107*3117ece4Schristos } 108*3117ece4Schristos 109*3117ece4Schristos size_t ZDICT_getDictHeaderSize(const void* dictBuffer, size_t dictSize) 110*3117ece4Schristos { 111*3117ece4Schristos size_t headerSize; 112*3117ece4Schristos if (dictSize <= 8 || MEM_readLE32(dictBuffer) != ZSTD_MAGIC_DICTIONARY) return ERROR(dictionary_corrupted); 113*3117ece4Schristos 114*3117ece4Schristos { ZSTD_compressedBlockState_t* bs = (ZSTD_compressedBlockState_t*)malloc(sizeof(ZSTD_compressedBlockState_t)); 115*3117ece4Schristos U32* wksp = (U32*)malloc(HUF_WORKSPACE_SIZE); 116*3117ece4Schristos if (!bs || !wksp) { 117*3117ece4Schristos headerSize = ERROR(memory_allocation); 118*3117ece4Schristos } else { 119*3117ece4Schristos ZSTD_reset_compressedBlockState(bs); 120*3117ece4Schristos headerSize = ZSTD_loadCEntropy(bs, wksp, dictBuffer, dictSize); 121*3117ece4Schristos } 122*3117ece4Schristos 123*3117ece4Schristos free(bs); 124*3117ece4Schristos free(wksp); 125*3117ece4Schristos } 126*3117ece4Schristos 127*3117ece4Schristos return headerSize; 128*3117ece4Schristos } 129*3117ece4Schristos 130*3117ece4Schristos /*-******************************************************** 131*3117ece4Schristos * Dictionary training functions 132*3117ece4Schristos **********************************************************/ 133*3117ece4Schristos /*! ZDICT_count() : 134*3117ece4Schristos Count the nb of common bytes between 2 pointers. 135*3117ece4Schristos Note : this function presumes end of buffer followed by noisy guard band. 136*3117ece4Schristos */ 137*3117ece4Schristos static size_t ZDICT_count(const void* pIn, const void* pMatch) 138*3117ece4Schristos { 139*3117ece4Schristos const char* const pStart = (const char*)pIn; 140*3117ece4Schristos for (;;) { 141*3117ece4Schristos size_t const diff = MEM_readST(pMatch) ^ MEM_readST(pIn); 142*3117ece4Schristos if (!diff) { 143*3117ece4Schristos pIn = (const char*)pIn+sizeof(size_t); 144*3117ece4Schristos pMatch = (const char*)pMatch+sizeof(size_t); 145*3117ece4Schristos continue; 146*3117ece4Schristos } 147*3117ece4Schristos pIn = (const char*)pIn+ZSTD_NbCommonBytes(diff); 148*3117ece4Schristos return (size_t)((const char*)pIn - pStart); 149*3117ece4Schristos } 150*3117ece4Schristos } 151*3117ece4Schristos 152*3117ece4Schristos 153*3117ece4Schristos typedef struct { 154*3117ece4Schristos U32 pos; 155*3117ece4Schristos U32 length; 156*3117ece4Schristos U32 savings; 157*3117ece4Schristos } dictItem; 158*3117ece4Schristos 159*3117ece4Schristos static void ZDICT_initDictItem(dictItem* d) 160*3117ece4Schristos { 161*3117ece4Schristos d->pos = 1; 162*3117ece4Schristos d->length = 0; 163*3117ece4Schristos d->savings = (U32)(-1); 164*3117ece4Schristos } 165*3117ece4Schristos 166*3117ece4Schristos 167*3117ece4Schristos #define LLIMIT 64 /* heuristic determined experimentally */ 168*3117ece4Schristos #define MINMATCHLENGTH 7 /* heuristic determined experimentally */ 169*3117ece4Schristos static dictItem ZDICT_analyzePos( 170*3117ece4Schristos BYTE* doneMarks, 171*3117ece4Schristos const int* suffix, U32 start, 172*3117ece4Schristos const void* buffer, U32 minRatio, U32 notificationLevel) 173*3117ece4Schristos { 174*3117ece4Schristos U32 lengthList[LLIMIT] = {0}; 175*3117ece4Schristos U32 cumulLength[LLIMIT] = {0}; 176*3117ece4Schristos U32 savings[LLIMIT] = {0}; 177*3117ece4Schristos const BYTE* b = (const BYTE*)buffer; 178*3117ece4Schristos size_t maxLength = LLIMIT; 179*3117ece4Schristos size_t pos = (size_t)suffix[start]; 180*3117ece4Schristos U32 end = start; 181*3117ece4Schristos dictItem solution; 182*3117ece4Schristos 183*3117ece4Schristos /* init */ 184*3117ece4Schristos memset(&solution, 0, sizeof(solution)); 185*3117ece4Schristos doneMarks[pos] = 1; 186*3117ece4Schristos 187*3117ece4Schristos /* trivial repetition cases */ 188*3117ece4Schristos if ( (MEM_read16(b+pos+0) == MEM_read16(b+pos+2)) 189*3117ece4Schristos ||(MEM_read16(b+pos+1) == MEM_read16(b+pos+3)) 190*3117ece4Schristos ||(MEM_read16(b+pos+2) == MEM_read16(b+pos+4)) ) { 191*3117ece4Schristos /* skip and mark segment */ 192*3117ece4Schristos U16 const pattern16 = MEM_read16(b+pos+4); 193*3117ece4Schristos U32 u, patternEnd = 6; 194*3117ece4Schristos while (MEM_read16(b+pos+patternEnd) == pattern16) patternEnd+=2 ; 195*3117ece4Schristos if (b[pos+patternEnd] == b[pos+patternEnd-1]) patternEnd++; 196*3117ece4Schristos for (u=1; u<patternEnd; u++) 197*3117ece4Schristos doneMarks[pos+u] = 1; 198*3117ece4Schristos return solution; 199*3117ece4Schristos } 200*3117ece4Schristos 201*3117ece4Schristos /* look forward */ 202*3117ece4Schristos { size_t length; 203*3117ece4Schristos do { 204*3117ece4Schristos end++; 205*3117ece4Schristos length = ZDICT_count(b + pos, b + suffix[end]); 206*3117ece4Schristos } while (length >= MINMATCHLENGTH); 207*3117ece4Schristos } 208*3117ece4Schristos 209*3117ece4Schristos /* look backward */ 210*3117ece4Schristos { size_t length; 211*3117ece4Schristos do { 212*3117ece4Schristos length = ZDICT_count(b + pos, b + *(suffix+start-1)); 213*3117ece4Schristos if (length >=MINMATCHLENGTH) start--; 214*3117ece4Schristos } while(length >= MINMATCHLENGTH); 215*3117ece4Schristos } 216*3117ece4Schristos 217*3117ece4Schristos /* exit if not found a minimum nb of repetitions */ 218*3117ece4Schristos if (end-start < minRatio) { 219*3117ece4Schristos U32 idx; 220*3117ece4Schristos for(idx=start; idx<end; idx++) 221*3117ece4Schristos doneMarks[suffix[idx]] = 1; 222*3117ece4Schristos return solution; 223*3117ece4Schristos } 224*3117ece4Schristos 225*3117ece4Schristos { int i; 226*3117ece4Schristos U32 mml; 227*3117ece4Schristos U32 refinedStart = start; 228*3117ece4Schristos U32 refinedEnd = end; 229*3117ece4Schristos 230*3117ece4Schristos DISPLAYLEVEL(4, "\n"); 231*3117ece4Schristos DISPLAYLEVEL(4, "found %3u matches of length >= %i at pos %7u ", (unsigned)(end-start), MINMATCHLENGTH, (unsigned)pos); 232*3117ece4Schristos DISPLAYLEVEL(4, "\n"); 233*3117ece4Schristos 234*3117ece4Schristos for (mml = MINMATCHLENGTH ; ; mml++) { 235*3117ece4Schristos BYTE currentChar = 0; 236*3117ece4Schristos U32 currentCount = 0; 237*3117ece4Schristos U32 currentID = refinedStart; 238*3117ece4Schristos U32 id; 239*3117ece4Schristos U32 selectedCount = 0; 240*3117ece4Schristos U32 selectedID = currentID; 241*3117ece4Schristos for (id =refinedStart; id < refinedEnd; id++) { 242*3117ece4Schristos if (b[suffix[id] + mml] != currentChar) { 243*3117ece4Schristos if (currentCount > selectedCount) { 244*3117ece4Schristos selectedCount = currentCount; 245*3117ece4Schristos selectedID = currentID; 246*3117ece4Schristos } 247*3117ece4Schristos currentID = id; 248*3117ece4Schristos currentChar = b[ suffix[id] + mml]; 249*3117ece4Schristos currentCount = 0; 250*3117ece4Schristos } 251*3117ece4Schristos currentCount ++; 252*3117ece4Schristos } 253*3117ece4Schristos if (currentCount > selectedCount) { /* for last */ 254*3117ece4Schristos selectedCount = currentCount; 255*3117ece4Schristos selectedID = currentID; 256*3117ece4Schristos } 257*3117ece4Schristos 258*3117ece4Schristos if (selectedCount < minRatio) 259*3117ece4Schristos break; 260*3117ece4Schristos refinedStart = selectedID; 261*3117ece4Schristos refinedEnd = refinedStart + selectedCount; 262*3117ece4Schristos } 263*3117ece4Schristos 264*3117ece4Schristos /* evaluate gain based on new dict */ 265*3117ece4Schristos start = refinedStart; 266*3117ece4Schristos pos = suffix[refinedStart]; 267*3117ece4Schristos end = start; 268*3117ece4Schristos memset(lengthList, 0, sizeof(lengthList)); 269*3117ece4Schristos 270*3117ece4Schristos /* look forward */ 271*3117ece4Schristos { size_t length; 272*3117ece4Schristos do { 273*3117ece4Schristos end++; 274*3117ece4Schristos length = ZDICT_count(b + pos, b + suffix[end]); 275*3117ece4Schristos if (length >= LLIMIT) length = LLIMIT-1; 276*3117ece4Schristos lengthList[length]++; 277*3117ece4Schristos } while (length >=MINMATCHLENGTH); 278*3117ece4Schristos } 279*3117ece4Schristos 280*3117ece4Schristos /* look backward */ 281*3117ece4Schristos { size_t length = MINMATCHLENGTH; 282*3117ece4Schristos while ((length >= MINMATCHLENGTH) & (start > 0)) { 283*3117ece4Schristos length = ZDICT_count(b + pos, b + suffix[start - 1]); 284*3117ece4Schristos if (length >= LLIMIT) length = LLIMIT - 1; 285*3117ece4Schristos lengthList[length]++; 286*3117ece4Schristos if (length >= MINMATCHLENGTH) start--; 287*3117ece4Schristos } 288*3117ece4Schristos } 289*3117ece4Schristos 290*3117ece4Schristos /* largest useful length */ 291*3117ece4Schristos memset(cumulLength, 0, sizeof(cumulLength)); 292*3117ece4Schristos cumulLength[maxLength-1] = lengthList[maxLength-1]; 293*3117ece4Schristos for (i=(int)(maxLength-2); i>=0; i--) 294*3117ece4Schristos cumulLength[i] = cumulLength[i+1] + lengthList[i]; 295*3117ece4Schristos 296*3117ece4Schristos for (i=LLIMIT-1; i>=MINMATCHLENGTH; i--) if (cumulLength[i]>=minRatio) break; 297*3117ece4Schristos maxLength = i; 298*3117ece4Schristos 299*3117ece4Schristos /* reduce maxLength in case of final into repetitive data */ 300*3117ece4Schristos { U32 l = (U32)maxLength; 301*3117ece4Schristos BYTE const c = b[pos + maxLength-1]; 302*3117ece4Schristos while (b[pos+l-2]==c) l--; 303*3117ece4Schristos maxLength = l; 304*3117ece4Schristos } 305*3117ece4Schristos if (maxLength < MINMATCHLENGTH) return solution; /* skip : no long-enough solution */ 306*3117ece4Schristos 307*3117ece4Schristos /* calculate savings */ 308*3117ece4Schristos savings[5] = 0; 309*3117ece4Schristos for (i=MINMATCHLENGTH; i<=(int)maxLength; i++) 310*3117ece4Schristos savings[i] = savings[i-1] + (lengthList[i] * (i-3)); 311*3117ece4Schristos 312*3117ece4Schristos DISPLAYLEVEL(4, "Selected dict at position %u, of length %u : saves %u (ratio: %.2f) \n", 313*3117ece4Schristos (unsigned)pos, (unsigned)maxLength, (unsigned)savings[maxLength], (double)savings[maxLength] / (double)maxLength); 314*3117ece4Schristos 315*3117ece4Schristos solution.pos = (U32)pos; 316*3117ece4Schristos solution.length = (U32)maxLength; 317*3117ece4Schristos solution.savings = savings[maxLength]; 318*3117ece4Schristos 319*3117ece4Schristos /* mark positions done */ 320*3117ece4Schristos { U32 id; 321*3117ece4Schristos for (id=start; id<end; id++) { 322*3117ece4Schristos U32 p, pEnd, length; 323*3117ece4Schristos U32 const testedPos = (U32)suffix[id]; 324*3117ece4Schristos if (testedPos == pos) 325*3117ece4Schristos length = solution.length; 326*3117ece4Schristos else { 327*3117ece4Schristos length = (U32)ZDICT_count(b+pos, b+testedPos); 328*3117ece4Schristos if (length > solution.length) length = solution.length; 329*3117ece4Schristos } 330*3117ece4Schristos pEnd = (U32)(testedPos + length); 331*3117ece4Schristos for (p=testedPos; p<pEnd; p++) 332*3117ece4Schristos doneMarks[p] = 1; 333*3117ece4Schristos } } } 334*3117ece4Schristos 335*3117ece4Schristos return solution; 336*3117ece4Schristos } 337*3117ece4Schristos 338*3117ece4Schristos 339*3117ece4Schristos static int isIncluded(const void* in, const void* container, size_t length) 340*3117ece4Schristos { 341*3117ece4Schristos const char* const ip = (const char*) in; 342*3117ece4Schristos const char* const into = (const char*) container; 343*3117ece4Schristos size_t u; 344*3117ece4Schristos 345*3117ece4Schristos for (u=0; u<length; u++) { /* works because end of buffer is a noisy guard band */ 346*3117ece4Schristos if (ip[u] != into[u]) break; 347*3117ece4Schristos } 348*3117ece4Schristos 349*3117ece4Schristos return u==length; 350*3117ece4Schristos } 351*3117ece4Schristos 352*3117ece4Schristos /*! ZDICT_tryMerge() : 353*3117ece4Schristos check if dictItem can be merged, do it if possible 354*3117ece4Schristos @return : id of destination elt, 0 if not merged 355*3117ece4Schristos */ 356*3117ece4Schristos static U32 ZDICT_tryMerge(dictItem* table, dictItem elt, U32 eltNbToSkip, const void* buffer) 357*3117ece4Schristos { 358*3117ece4Schristos const U32 tableSize = table->pos; 359*3117ece4Schristos const U32 eltEnd = elt.pos + elt.length; 360*3117ece4Schristos const char* const buf = (const char*) buffer; 361*3117ece4Schristos 362*3117ece4Schristos /* tail overlap */ 363*3117ece4Schristos U32 u; for (u=1; u<tableSize; u++) { 364*3117ece4Schristos if (u==eltNbToSkip) continue; 365*3117ece4Schristos if ((table[u].pos > elt.pos) && (table[u].pos <= eltEnd)) { /* overlap, existing > new */ 366*3117ece4Schristos /* append */ 367*3117ece4Schristos U32 const addedLength = table[u].pos - elt.pos; 368*3117ece4Schristos table[u].length += addedLength; 369*3117ece4Schristos table[u].pos = elt.pos; 370*3117ece4Schristos table[u].savings += elt.savings * addedLength / elt.length; /* rough approx */ 371*3117ece4Schristos table[u].savings += elt.length / 8; /* rough approx bonus */ 372*3117ece4Schristos elt = table[u]; 373*3117ece4Schristos /* sort : improve rank */ 374*3117ece4Schristos while ((u>1) && (table[u-1].savings < elt.savings)) 375*3117ece4Schristos table[u] = table[u-1], u--; 376*3117ece4Schristos table[u] = elt; 377*3117ece4Schristos return u; 378*3117ece4Schristos } } 379*3117ece4Schristos 380*3117ece4Schristos /* front overlap */ 381*3117ece4Schristos for (u=1; u<tableSize; u++) { 382*3117ece4Schristos if (u==eltNbToSkip) continue; 383*3117ece4Schristos 384*3117ece4Schristos if ((table[u].pos + table[u].length >= elt.pos) && (table[u].pos < elt.pos)) { /* overlap, existing < new */ 385*3117ece4Schristos /* append */ 386*3117ece4Schristos int const addedLength = (int)eltEnd - (int)(table[u].pos + table[u].length); 387*3117ece4Schristos table[u].savings += elt.length / 8; /* rough approx bonus */ 388*3117ece4Schristos if (addedLength > 0) { /* otherwise, elt fully included into existing */ 389*3117ece4Schristos table[u].length += addedLength; 390*3117ece4Schristos table[u].savings += elt.savings * addedLength / elt.length; /* rough approx */ 391*3117ece4Schristos } 392*3117ece4Schristos /* sort : improve rank */ 393*3117ece4Schristos elt = table[u]; 394*3117ece4Schristos while ((u>1) && (table[u-1].savings < elt.savings)) 395*3117ece4Schristos table[u] = table[u-1], u--; 396*3117ece4Schristos table[u] = elt; 397*3117ece4Schristos return u; 398*3117ece4Schristos } 399*3117ece4Schristos 400*3117ece4Schristos if (MEM_read64(buf + table[u].pos) == MEM_read64(buf + elt.pos + 1)) { 401*3117ece4Schristos if (isIncluded(buf + table[u].pos, buf + elt.pos + 1, table[u].length)) { 402*3117ece4Schristos size_t const addedLength = MAX( (int)elt.length - (int)table[u].length , 1 ); 403*3117ece4Schristos table[u].pos = elt.pos; 404*3117ece4Schristos table[u].savings += (U32)(elt.savings * addedLength / elt.length); 405*3117ece4Schristos table[u].length = MIN(elt.length, table[u].length + 1); 406*3117ece4Schristos return u; 407*3117ece4Schristos } 408*3117ece4Schristos } 409*3117ece4Schristos } 410*3117ece4Schristos 411*3117ece4Schristos return 0; 412*3117ece4Schristos } 413*3117ece4Schristos 414*3117ece4Schristos 415*3117ece4Schristos static void ZDICT_removeDictItem(dictItem* table, U32 id) 416*3117ece4Schristos { 417*3117ece4Schristos /* convention : table[0].pos stores nb of elts */ 418*3117ece4Schristos U32 const max = table[0].pos; 419*3117ece4Schristos U32 u; 420*3117ece4Schristos if (!id) return; /* protection, should never happen */ 421*3117ece4Schristos for (u=id; u<max-1; u++) 422*3117ece4Schristos table[u] = table[u+1]; 423*3117ece4Schristos table->pos--; 424*3117ece4Schristos } 425*3117ece4Schristos 426*3117ece4Schristos 427*3117ece4Schristos static void ZDICT_insertDictItem(dictItem* table, U32 maxSize, dictItem elt, const void* buffer) 428*3117ece4Schristos { 429*3117ece4Schristos /* merge if possible */ 430*3117ece4Schristos U32 mergeId = ZDICT_tryMerge(table, elt, 0, buffer); 431*3117ece4Schristos if (mergeId) { 432*3117ece4Schristos U32 newMerge = 1; 433*3117ece4Schristos while (newMerge) { 434*3117ece4Schristos newMerge = ZDICT_tryMerge(table, table[mergeId], mergeId, buffer); 435*3117ece4Schristos if (newMerge) ZDICT_removeDictItem(table, mergeId); 436*3117ece4Schristos mergeId = newMerge; 437*3117ece4Schristos } 438*3117ece4Schristos return; 439*3117ece4Schristos } 440*3117ece4Schristos 441*3117ece4Schristos /* insert */ 442*3117ece4Schristos { U32 current; 443*3117ece4Schristos U32 nextElt = table->pos; 444*3117ece4Schristos if (nextElt >= maxSize) nextElt = maxSize-1; 445*3117ece4Schristos current = nextElt-1; 446*3117ece4Schristos while (table[current].savings < elt.savings) { 447*3117ece4Schristos table[current+1] = table[current]; 448*3117ece4Schristos current--; 449*3117ece4Schristos } 450*3117ece4Schristos table[current+1] = elt; 451*3117ece4Schristos table->pos = nextElt+1; 452*3117ece4Schristos } 453*3117ece4Schristos } 454*3117ece4Schristos 455*3117ece4Schristos 456*3117ece4Schristos static U32 ZDICT_dictSize(const dictItem* dictList) 457*3117ece4Schristos { 458*3117ece4Schristos U32 u, dictSize = 0; 459*3117ece4Schristos for (u=1; u<dictList[0].pos; u++) 460*3117ece4Schristos dictSize += dictList[u].length; 461*3117ece4Schristos return dictSize; 462*3117ece4Schristos } 463*3117ece4Schristos 464*3117ece4Schristos 465*3117ece4Schristos static size_t ZDICT_trainBuffer_legacy(dictItem* dictList, U32 dictListSize, 466*3117ece4Schristos const void* const buffer, size_t bufferSize, /* buffer must end with noisy guard band */ 467*3117ece4Schristos const size_t* fileSizes, unsigned nbFiles, 468*3117ece4Schristos unsigned minRatio, U32 notificationLevel) 469*3117ece4Schristos { 470*3117ece4Schristos int* const suffix0 = (int*)malloc((bufferSize+2)*sizeof(*suffix0)); 471*3117ece4Schristos int* const suffix = suffix0+1; 472*3117ece4Schristos U32* reverseSuffix = (U32*)malloc((bufferSize)*sizeof(*reverseSuffix)); 473*3117ece4Schristos BYTE* doneMarks = (BYTE*)malloc((bufferSize+16)*sizeof(*doneMarks)); /* +16 for overflow security */ 474*3117ece4Schristos U32* filePos = (U32*)malloc(nbFiles * sizeof(*filePos)); 475*3117ece4Schristos size_t result = 0; 476*3117ece4Schristos clock_t displayClock = 0; 477*3117ece4Schristos clock_t const refreshRate = CLOCKS_PER_SEC * 3 / 10; 478*3117ece4Schristos 479*3117ece4Schristos # undef DISPLAYUPDATE 480*3117ece4Schristos # define DISPLAYUPDATE(l, ...) \ 481*3117ece4Schristos do { \ 482*3117ece4Schristos if (notificationLevel>=l) { \ 483*3117ece4Schristos if (ZDICT_clockSpan(displayClock) > refreshRate) { \ 484*3117ece4Schristos displayClock = clock(); \ 485*3117ece4Schristos DISPLAY(__VA_ARGS__); \ 486*3117ece4Schristos } \ 487*3117ece4Schristos if (notificationLevel>=4) fflush(stderr); \ 488*3117ece4Schristos } \ 489*3117ece4Schristos } while (0) 490*3117ece4Schristos 491*3117ece4Schristos /* init */ 492*3117ece4Schristos DISPLAYLEVEL(2, "\r%70s\r", ""); /* clean display line */ 493*3117ece4Schristos if (!suffix0 || !reverseSuffix || !doneMarks || !filePos) { 494*3117ece4Schristos result = ERROR(memory_allocation); 495*3117ece4Schristos goto _cleanup; 496*3117ece4Schristos } 497*3117ece4Schristos if (minRatio < MINRATIO) minRatio = MINRATIO; 498*3117ece4Schristos memset(doneMarks, 0, bufferSize+16); 499*3117ece4Schristos 500*3117ece4Schristos /* limit sample set size (divsufsort limitation)*/ 501*3117ece4Schristos if (bufferSize > ZDICT_MAX_SAMPLES_SIZE) DISPLAYLEVEL(3, "sample set too large : reduced to %u MB ...\n", (unsigned)(ZDICT_MAX_SAMPLES_SIZE>>20)); 502*3117ece4Schristos while (bufferSize > ZDICT_MAX_SAMPLES_SIZE) bufferSize -= fileSizes[--nbFiles]; 503*3117ece4Schristos 504*3117ece4Schristos /* sort */ 505*3117ece4Schristos DISPLAYLEVEL(2, "sorting %u files of total size %u MB ...\n", nbFiles, (unsigned)(bufferSize>>20)); 506*3117ece4Schristos { int const divSuftSortResult = divsufsort((const unsigned char*)buffer, suffix, (int)bufferSize, 0); 507*3117ece4Schristos if (divSuftSortResult != 0) { result = ERROR(GENERIC); goto _cleanup; } 508*3117ece4Schristos } 509*3117ece4Schristos suffix[bufferSize] = (int)bufferSize; /* leads into noise */ 510*3117ece4Schristos suffix0[0] = (int)bufferSize; /* leads into noise */ 511*3117ece4Schristos /* build reverse suffix sort */ 512*3117ece4Schristos { size_t pos; 513*3117ece4Schristos for (pos=0; pos < bufferSize; pos++) 514*3117ece4Schristos reverseSuffix[suffix[pos]] = (U32)pos; 515*3117ece4Schristos /* note filePos tracks borders between samples. 516*3117ece4Schristos It's not used at this stage, but planned to become useful in a later update */ 517*3117ece4Schristos filePos[0] = 0; 518*3117ece4Schristos for (pos=1; pos<nbFiles; pos++) 519*3117ece4Schristos filePos[pos] = (U32)(filePos[pos-1] + fileSizes[pos-1]); 520*3117ece4Schristos } 521*3117ece4Schristos 522*3117ece4Schristos DISPLAYLEVEL(2, "finding patterns ... \n"); 523*3117ece4Schristos DISPLAYLEVEL(3, "minimum ratio : %u \n", minRatio); 524*3117ece4Schristos 525*3117ece4Schristos { U32 cursor; for (cursor=0; cursor < bufferSize; ) { 526*3117ece4Schristos dictItem solution; 527*3117ece4Schristos if (doneMarks[cursor]) { cursor++; continue; } 528*3117ece4Schristos solution = ZDICT_analyzePos(doneMarks, suffix, reverseSuffix[cursor], buffer, minRatio, notificationLevel); 529*3117ece4Schristos if (solution.length==0) { cursor++; continue; } 530*3117ece4Schristos ZDICT_insertDictItem(dictList, dictListSize, solution, buffer); 531*3117ece4Schristos cursor += solution.length; 532*3117ece4Schristos DISPLAYUPDATE(2, "\r%4.2f %% \r", (double)cursor / (double)bufferSize * 100.0); 533*3117ece4Schristos } } 534*3117ece4Schristos 535*3117ece4Schristos _cleanup: 536*3117ece4Schristos free(suffix0); 537*3117ece4Schristos free(reverseSuffix); 538*3117ece4Schristos free(doneMarks); 539*3117ece4Schristos free(filePos); 540*3117ece4Schristos return result; 541*3117ece4Schristos } 542*3117ece4Schristos 543*3117ece4Schristos 544*3117ece4Schristos static void ZDICT_fillNoise(void* buffer, size_t length) 545*3117ece4Schristos { 546*3117ece4Schristos unsigned const prime1 = 2654435761U; 547*3117ece4Schristos unsigned const prime2 = 2246822519U; 548*3117ece4Schristos unsigned acc = prime1; 549*3117ece4Schristos size_t p=0; 550*3117ece4Schristos for (p=0; p<length; p++) { 551*3117ece4Schristos acc *= prime2; 552*3117ece4Schristos ((unsigned char*)buffer)[p] = (unsigned char)(acc >> 21); 553*3117ece4Schristos } 554*3117ece4Schristos } 555*3117ece4Schristos 556*3117ece4Schristos 557*3117ece4Schristos typedef struct 558*3117ece4Schristos { 559*3117ece4Schristos ZSTD_CDict* dict; /* dictionary */ 560*3117ece4Schristos ZSTD_CCtx* zc; /* working context */ 561*3117ece4Schristos void* workPlace; /* must be ZSTD_BLOCKSIZE_MAX allocated */ 562*3117ece4Schristos } EStats_ress_t; 563*3117ece4Schristos 564*3117ece4Schristos #define MAXREPOFFSET 1024 565*3117ece4Schristos 566*3117ece4Schristos static void ZDICT_countEStats(EStats_ress_t esr, const ZSTD_parameters* params, 567*3117ece4Schristos unsigned* countLit, unsigned* offsetcodeCount, unsigned* matchlengthCount, unsigned* litlengthCount, U32* repOffsets, 568*3117ece4Schristos const void* src, size_t srcSize, 569*3117ece4Schristos U32 notificationLevel) 570*3117ece4Schristos { 571*3117ece4Schristos size_t const blockSizeMax = MIN (ZSTD_BLOCKSIZE_MAX, 1 << params->cParams.windowLog); 572*3117ece4Schristos size_t cSize; 573*3117ece4Schristos 574*3117ece4Schristos if (srcSize > blockSizeMax) srcSize = blockSizeMax; /* protection vs large samples */ 575*3117ece4Schristos { size_t const errorCode = ZSTD_compressBegin_usingCDict_deprecated(esr.zc, esr.dict); 576*3117ece4Schristos if (ZSTD_isError(errorCode)) { DISPLAYLEVEL(1, "warning : ZSTD_compressBegin_usingCDict failed \n"); return; } 577*3117ece4Schristos 578*3117ece4Schristos } 579*3117ece4Schristos cSize = ZSTD_compressBlock_deprecated(esr.zc, esr.workPlace, ZSTD_BLOCKSIZE_MAX, src, srcSize); 580*3117ece4Schristos if (ZSTD_isError(cSize)) { DISPLAYLEVEL(3, "warning : could not compress sample size %u \n", (unsigned)srcSize); return; } 581*3117ece4Schristos 582*3117ece4Schristos if (cSize) { /* if == 0; block is not compressible */ 583*3117ece4Schristos const seqStore_t* const seqStorePtr = ZSTD_getSeqStore(esr.zc); 584*3117ece4Schristos 585*3117ece4Schristos /* literals stats */ 586*3117ece4Schristos { const BYTE* bytePtr; 587*3117ece4Schristos for(bytePtr = seqStorePtr->litStart; bytePtr < seqStorePtr->lit; bytePtr++) 588*3117ece4Schristos countLit[*bytePtr]++; 589*3117ece4Schristos } 590*3117ece4Schristos 591*3117ece4Schristos /* seqStats */ 592*3117ece4Schristos { U32 const nbSeq = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart); 593*3117ece4Schristos ZSTD_seqToCodes(seqStorePtr); 594*3117ece4Schristos 595*3117ece4Schristos { const BYTE* codePtr = seqStorePtr->ofCode; 596*3117ece4Schristos U32 u; 597*3117ece4Schristos for (u=0; u<nbSeq; u++) offsetcodeCount[codePtr[u]]++; 598*3117ece4Schristos } 599*3117ece4Schristos 600*3117ece4Schristos { const BYTE* codePtr = seqStorePtr->mlCode; 601*3117ece4Schristos U32 u; 602*3117ece4Schristos for (u=0; u<nbSeq; u++) matchlengthCount[codePtr[u]]++; 603*3117ece4Schristos } 604*3117ece4Schristos 605*3117ece4Schristos { const BYTE* codePtr = seqStorePtr->llCode; 606*3117ece4Schristos U32 u; 607*3117ece4Schristos for (u=0; u<nbSeq; u++) litlengthCount[codePtr[u]]++; 608*3117ece4Schristos } 609*3117ece4Schristos 610*3117ece4Schristos if (nbSeq >= 2) { /* rep offsets */ 611*3117ece4Schristos const seqDef* const seq = seqStorePtr->sequencesStart; 612*3117ece4Schristos U32 offset1 = seq[0].offBase - ZSTD_REP_NUM; 613*3117ece4Schristos U32 offset2 = seq[1].offBase - ZSTD_REP_NUM; 614*3117ece4Schristos if (offset1 >= MAXREPOFFSET) offset1 = 0; 615*3117ece4Schristos if (offset2 >= MAXREPOFFSET) offset2 = 0; 616*3117ece4Schristos repOffsets[offset1] += 3; 617*3117ece4Schristos repOffsets[offset2] += 1; 618*3117ece4Schristos } } } 619*3117ece4Schristos } 620*3117ece4Schristos 621*3117ece4Schristos static size_t ZDICT_totalSampleSize(const size_t* fileSizes, unsigned nbFiles) 622*3117ece4Schristos { 623*3117ece4Schristos size_t total=0; 624*3117ece4Schristos unsigned u; 625*3117ece4Schristos for (u=0; u<nbFiles; u++) total += fileSizes[u]; 626*3117ece4Schristos return total; 627*3117ece4Schristos } 628*3117ece4Schristos 629*3117ece4Schristos typedef struct { U32 offset; U32 count; } offsetCount_t; 630*3117ece4Schristos 631*3117ece4Schristos static void ZDICT_insertSortCount(offsetCount_t table[ZSTD_REP_NUM+1], U32 val, U32 count) 632*3117ece4Schristos { 633*3117ece4Schristos U32 u; 634*3117ece4Schristos table[ZSTD_REP_NUM].offset = val; 635*3117ece4Schristos table[ZSTD_REP_NUM].count = count; 636*3117ece4Schristos for (u=ZSTD_REP_NUM; u>0; u--) { 637*3117ece4Schristos offsetCount_t tmp; 638*3117ece4Schristos if (table[u-1].count >= table[u].count) break; 639*3117ece4Schristos tmp = table[u-1]; 640*3117ece4Schristos table[u-1] = table[u]; 641*3117ece4Schristos table[u] = tmp; 642*3117ece4Schristos } 643*3117ece4Schristos } 644*3117ece4Schristos 645*3117ece4Schristos /* ZDICT_flatLit() : 646*3117ece4Schristos * rewrite `countLit` to contain a mostly flat but still compressible distribution of literals. 647*3117ece4Schristos * necessary to avoid generating a non-compressible distribution that HUF_writeCTable() cannot encode. 648*3117ece4Schristos */ 649*3117ece4Schristos static void ZDICT_flatLit(unsigned* countLit) 650*3117ece4Schristos { 651*3117ece4Schristos int u; 652*3117ece4Schristos for (u=1; u<256; u++) countLit[u] = 2; 653*3117ece4Schristos countLit[0] = 4; 654*3117ece4Schristos countLit[253] = 1; 655*3117ece4Schristos countLit[254] = 1; 656*3117ece4Schristos } 657*3117ece4Schristos 658*3117ece4Schristos #define OFFCODE_MAX 30 /* only applicable to first block */ 659*3117ece4Schristos static size_t ZDICT_analyzeEntropy(void* dstBuffer, size_t maxDstSize, 660*3117ece4Schristos int compressionLevel, 661*3117ece4Schristos const void* srcBuffer, const size_t* fileSizes, unsigned nbFiles, 662*3117ece4Schristos const void* dictBuffer, size_t dictBufferSize, 663*3117ece4Schristos unsigned notificationLevel) 664*3117ece4Schristos { 665*3117ece4Schristos unsigned countLit[256]; 666*3117ece4Schristos HUF_CREATE_STATIC_CTABLE(hufTable, 255); 667*3117ece4Schristos unsigned offcodeCount[OFFCODE_MAX+1]; 668*3117ece4Schristos short offcodeNCount[OFFCODE_MAX+1]; 669*3117ece4Schristos U32 offcodeMax = ZSTD_highbit32((U32)(dictBufferSize + 128 KB)); 670*3117ece4Schristos unsigned matchLengthCount[MaxML+1]; 671*3117ece4Schristos short matchLengthNCount[MaxML+1]; 672*3117ece4Schristos unsigned litLengthCount[MaxLL+1]; 673*3117ece4Schristos short litLengthNCount[MaxLL+1]; 674*3117ece4Schristos U32 repOffset[MAXREPOFFSET]; 675*3117ece4Schristos offsetCount_t bestRepOffset[ZSTD_REP_NUM+1]; 676*3117ece4Schristos EStats_ress_t esr = { NULL, NULL, NULL }; 677*3117ece4Schristos ZSTD_parameters params; 678*3117ece4Schristos U32 u, huffLog = 11, Offlog = OffFSELog, mlLog = MLFSELog, llLog = LLFSELog, total; 679*3117ece4Schristos size_t pos = 0, errorCode; 680*3117ece4Schristos size_t eSize = 0; 681*3117ece4Schristos size_t const totalSrcSize = ZDICT_totalSampleSize(fileSizes, nbFiles); 682*3117ece4Schristos size_t const averageSampleSize = totalSrcSize / (nbFiles + !nbFiles); 683*3117ece4Schristos BYTE* dstPtr = (BYTE*)dstBuffer; 684*3117ece4Schristos U32 wksp[HUF_CTABLE_WORKSPACE_SIZE_U32]; 685*3117ece4Schristos 686*3117ece4Schristos /* init */ 687*3117ece4Schristos DEBUGLOG(4, "ZDICT_analyzeEntropy"); 688*3117ece4Schristos if (offcodeMax>OFFCODE_MAX) { eSize = ERROR(dictionaryCreation_failed); goto _cleanup; } /* too large dictionary */ 689*3117ece4Schristos for (u=0; u<256; u++) countLit[u] = 1; /* any character must be described */ 690*3117ece4Schristos for (u=0; u<=offcodeMax; u++) offcodeCount[u] = 1; 691*3117ece4Schristos for (u=0; u<=MaxML; u++) matchLengthCount[u] = 1; 692*3117ece4Schristos for (u=0; u<=MaxLL; u++) litLengthCount[u] = 1; 693*3117ece4Schristos memset(repOffset, 0, sizeof(repOffset)); 694*3117ece4Schristos repOffset[1] = repOffset[4] = repOffset[8] = 1; 695*3117ece4Schristos memset(bestRepOffset, 0, sizeof(bestRepOffset)); 696*3117ece4Schristos if (compressionLevel==0) compressionLevel = ZSTD_CLEVEL_DEFAULT; 697*3117ece4Schristos params = ZSTD_getParams(compressionLevel, averageSampleSize, dictBufferSize); 698*3117ece4Schristos 699*3117ece4Schristos esr.dict = ZSTD_createCDict_advanced(dictBuffer, dictBufferSize, ZSTD_dlm_byRef, ZSTD_dct_rawContent, params.cParams, ZSTD_defaultCMem); 700*3117ece4Schristos esr.zc = ZSTD_createCCtx(); 701*3117ece4Schristos esr.workPlace = malloc(ZSTD_BLOCKSIZE_MAX); 702*3117ece4Schristos if (!esr.dict || !esr.zc || !esr.workPlace) { 703*3117ece4Schristos eSize = ERROR(memory_allocation); 704*3117ece4Schristos DISPLAYLEVEL(1, "Not enough memory \n"); 705*3117ece4Schristos goto _cleanup; 706*3117ece4Schristos } 707*3117ece4Schristos 708*3117ece4Schristos /* collect stats on all samples */ 709*3117ece4Schristos for (u=0; u<nbFiles; u++) { 710*3117ece4Schristos ZDICT_countEStats(esr, ¶ms, 711*3117ece4Schristos countLit, offcodeCount, matchLengthCount, litLengthCount, repOffset, 712*3117ece4Schristos (const char*)srcBuffer + pos, fileSizes[u], 713*3117ece4Schristos notificationLevel); 714*3117ece4Schristos pos += fileSizes[u]; 715*3117ece4Schristos } 716*3117ece4Schristos 717*3117ece4Schristos if (notificationLevel >= 4) { 718*3117ece4Schristos /* writeStats */ 719*3117ece4Schristos DISPLAYLEVEL(4, "Offset Code Frequencies : \n"); 720*3117ece4Schristos for (u=0; u<=offcodeMax; u++) { 721*3117ece4Schristos DISPLAYLEVEL(4, "%2u :%7u \n", u, offcodeCount[u]); 722*3117ece4Schristos } } 723*3117ece4Schristos 724*3117ece4Schristos /* analyze, build stats, starting with literals */ 725*3117ece4Schristos { size_t maxNbBits = HUF_buildCTable_wksp(hufTable, countLit, 255, huffLog, wksp, sizeof(wksp)); 726*3117ece4Schristos if (HUF_isError(maxNbBits)) { 727*3117ece4Schristos eSize = maxNbBits; 728*3117ece4Schristos DISPLAYLEVEL(1, " HUF_buildCTable error \n"); 729*3117ece4Schristos goto _cleanup; 730*3117ece4Schristos } 731*3117ece4Schristos if (maxNbBits==8) { /* not compressible : will fail on HUF_writeCTable() */ 732*3117ece4Schristos DISPLAYLEVEL(2, "warning : pathological dataset : literals are not compressible : samples are noisy or too regular \n"); 733*3117ece4Schristos ZDICT_flatLit(countLit); /* replace distribution by a fake "mostly flat but still compressible" distribution, that HUF_writeCTable() can encode */ 734*3117ece4Schristos maxNbBits = HUF_buildCTable_wksp(hufTable, countLit, 255, huffLog, wksp, sizeof(wksp)); 735*3117ece4Schristos assert(maxNbBits==9); 736*3117ece4Schristos } 737*3117ece4Schristos huffLog = (U32)maxNbBits; 738*3117ece4Schristos } 739*3117ece4Schristos 740*3117ece4Schristos /* looking for most common first offsets */ 741*3117ece4Schristos { U32 offset; 742*3117ece4Schristos for (offset=1; offset<MAXREPOFFSET; offset++) 743*3117ece4Schristos ZDICT_insertSortCount(bestRepOffset, offset, repOffset[offset]); 744*3117ece4Schristos } 745*3117ece4Schristos /* note : the result of this phase should be used to better appreciate the impact on statistics */ 746*3117ece4Schristos 747*3117ece4Schristos total=0; for (u=0; u<=offcodeMax; u++) total+=offcodeCount[u]; 748*3117ece4Schristos errorCode = FSE_normalizeCount(offcodeNCount, Offlog, offcodeCount, total, offcodeMax, /* useLowProbCount */ 1); 749*3117ece4Schristos if (FSE_isError(errorCode)) { 750*3117ece4Schristos eSize = errorCode; 751*3117ece4Schristos DISPLAYLEVEL(1, "FSE_normalizeCount error with offcodeCount \n"); 752*3117ece4Schristos goto _cleanup; 753*3117ece4Schristos } 754*3117ece4Schristos Offlog = (U32)errorCode; 755*3117ece4Schristos 756*3117ece4Schristos total=0; for (u=0; u<=MaxML; u++) total+=matchLengthCount[u]; 757*3117ece4Schristos errorCode = FSE_normalizeCount(matchLengthNCount, mlLog, matchLengthCount, total, MaxML, /* useLowProbCount */ 1); 758*3117ece4Schristos if (FSE_isError(errorCode)) { 759*3117ece4Schristos eSize = errorCode; 760*3117ece4Schristos DISPLAYLEVEL(1, "FSE_normalizeCount error with matchLengthCount \n"); 761*3117ece4Schristos goto _cleanup; 762*3117ece4Schristos } 763*3117ece4Schristos mlLog = (U32)errorCode; 764*3117ece4Schristos 765*3117ece4Schristos total=0; for (u=0; u<=MaxLL; u++) total+=litLengthCount[u]; 766*3117ece4Schristos errorCode = FSE_normalizeCount(litLengthNCount, llLog, litLengthCount, total, MaxLL, /* useLowProbCount */ 1); 767*3117ece4Schristos if (FSE_isError(errorCode)) { 768*3117ece4Schristos eSize = errorCode; 769*3117ece4Schristos DISPLAYLEVEL(1, "FSE_normalizeCount error with litLengthCount \n"); 770*3117ece4Schristos goto _cleanup; 771*3117ece4Schristos } 772*3117ece4Schristos llLog = (U32)errorCode; 773*3117ece4Schristos 774*3117ece4Schristos /* write result to buffer */ 775*3117ece4Schristos { size_t const hhSize = HUF_writeCTable_wksp(dstPtr, maxDstSize, hufTable, 255, huffLog, wksp, sizeof(wksp)); 776*3117ece4Schristos if (HUF_isError(hhSize)) { 777*3117ece4Schristos eSize = hhSize; 778*3117ece4Schristos DISPLAYLEVEL(1, "HUF_writeCTable error \n"); 779*3117ece4Schristos goto _cleanup; 780*3117ece4Schristos } 781*3117ece4Schristos dstPtr += hhSize; 782*3117ece4Schristos maxDstSize -= hhSize; 783*3117ece4Schristos eSize += hhSize; 784*3117ece4Schristos } 785*3117ece4Schristos 786*3117ece4Schristos { size_t const ohSize = FSE_writeNCount(dstPtr, maxDstSize, offcodeNCount, OFFCODE_MAX, Offlog); 787*3117ece4Schristos if (FSE_isError(ohSize)) { 788*3117ece4Schristos eSize = ohSize; 789*3117ece4Schristos DISPLAYLEVEL(1, "FSE_writeNCount error with offcodeNCount \n"); 790*3117ece4Schristos goto _cleanup; 791*3117ece4Schristos } 792*3117ece4Schristos dstPtr += ohSize; 793*3117ece4Schristos maxDstSize -= ohSize; 794*3117ece4Schristos eSize += ohSize; 795*3117ece4Schristos } 796*3117ece4Schristos 797*3117ece4Schristos { size_t const mhSize = FSE_writeNCount(dstPtr, maxDstSize, matchLengthNCount, MaxML, mlLog); 798*3117ece4Schristos if (FSE_isError(mhSize)) { 799*3117ece4Schristos eSize = mhSize; 800*3117ece4Schristos DISPLAYLEVEL(1, "FSE_writeNCount error with matchLengthNCount \n"); 801*3117ece4Schristos goto _cleanup; 802*3117ece4Schristos } 803*3117ece4Schristos dstPtr += mhSize; 804*3117ece4Schristos maxDstSize -= mhSize; 805*3117ece4Schristos eSize += mhSize; 806*3117ece4Schristos } 807*3117ece4Schristos 808*3117ece4Schristos { size_t const lhSize = FSE_writeNCount(dstPtr, maxDstSize, litLengthNCount, MaxLL, llLog); 809*3117ece4Schristos if (FSE_isError(lhSize)) { 810*3117ece4Schristos eSize = lhSize; 811*3117ece4Schristos DISPLAYLEVEL(1, "FSE_writeNCount error with litlengthNCount \n"); 812*3117ece4Schristos goto _cleanup; 813*3117ece4Schristos } 814*3117ece4Schristos dstPtr += lhSize; 815*3117ece4Schristos maxDstSize -= lhSize; 816*3117ece4Schristos eSize += lhSize; 817*3117ece4Schristos } 818*3117ece4Schristos 819*3117ece4Schristos if (maxDstSize<12) { 820*3117ece4Schristos eSize = ERROR(dstSize_tooSmall); 821*3117ece4Schristos DISPLAYLEVEL(1, "not enough space to write RepOffsets \n"); 822*3117ece4Schristos goto _cleanup; 823*3117ece4Schristos } 824*3117ece4Schristos # if 0 825*3117ece4Schristos MEM_writeLE32(dstPtr+0, bestRepOffset[0].offset); 826*3117ece4Schristos MEM_writeLE32(dstPtr+4, bestRepOffset[1].offset); 827*3117ece4Schristos MEM_writeLE32(dstPtr+8, bestRepOffset[2].offset); 828*3117ece4Schristos #else 829*3117ece4Schristos /* at this stage, we don't use the result of "most common first offset", 830*3117ece4Schristos * as the impact of statistics is not properly evaluated */ 831*3117ece4Schristos MEM_writeLE32(dstPtr+0, repStartValue[0]); 832*3117ece4Schristos MEM_writeLE32(dstPtr+4, repStartValue[1]); 833*3117ece4Schristos MEM_writeLE32(dstPtr+8, repStartValue[2]); 834*3117ece4Schristos #endif 835*3117ece4Schristos eSize += 12; 836*3117ece4Schristos 837*3117ece4Schristos _cleanup: 838*3117ece4Schristos ZSTD_freeCDict(esr.dict); 839*3117ece4Schristos ZSTD_freeCCtx(esr.zc); 840*3117ece4Schristos free(esr.workPlace); 841*3117ece4Schristos 842*3117ece4Schristos return eSize; 843*3117ece4Schristos } 844*3117ece4Schristos 845*3117ece4Schristos 846*3117ece4Schristos /** 847*3117ece4Schristos * @returns the maximum repcode value 848*3117ece4Schristos */ 849*3117ece4Schristos static U32 ZDICT_maxRep(U32 const reps[ZSTD_REP_NUM]) 850*3117ece4Schristos { 851*3117ece4Schristos U32 maxRep = reps[0]; 852*3117ece4Schristos int r; 853*3117ece4Schristos for (r = 1; r < ZSTD_REP_NUM; ++r) 854*3117ece4Schristos maxRep = MAX(maxRep, reps[r]); 855*3117ece4Schristos return maxRep; 856*3117ece4Schristos } 857*3117ece4Schristos 858*3117ece4Schristos size_t ZDICT_finalizeDictionary(void* dictBuffer, size_t dictBufferCapacity, 859*3117ece4Schristos const void* customDictContent, size_t dictContentSize, 860*3117ece4Schristos const void* samplesBuffer, const size_t* samplesSizes, 861*3117ece4Schristos unsigned nbSamples, ZDICT_params_t params) 862*3117ece4Schristos { 863*3117ece4Schristos size_t hSize; 864*3117ece4Schristos #define HBUFFSIZE 256 /* should prove large enough for all entropy headers */ 865*3117ece4Schristos BYTE header[HBUFFSIZE]; 866*3117ece4Schristos int const compressionLevel = (params.compressionLevel == 0) ? ZSTD_CLEVEL_DEFAULT : params.compressionLevel; 867*3117ece4Schristos U32 const notificationLevel = params.notificationLevel; 868*3117ece4Schristos /* The final dictionary content must be at least as large as the largest repcode */ 869*3117ece4Schristos size_t const minContentSize = (size_t)ZDICT_maxRep(repStartValue); 870*3117ece4Schristos size_t paddingSize; 871*3117ece4Schristos 872*3117ece4Schristos /* check conditions */ 873*3117ece4Schristos DEBUGLOG(4, "ZDICT_finalizeDictionary"); 874*3117ece4Schristos if (dictBufferCapacity < dictContentSize) return ERROR(dstSize_tooSmall); 875*3117ece4Schristos if (dictBufferCapacity < ZDICT_DICTSIZE_MIN) return ERROR(dstSize_tooSmall); 876*3117ece4Schristos 877*3117ece4Schristos /* dictionary header */ 878*3117ece4Schristos MEM_writeLE32(header, ZSTD_MAGIC_DICTIONARY); 879*3117ece4Schristos { U64 const randomID = XXH64(customDictContent, dictContentSize, 0); 880*3117ece4Schristos U32 const compliantID = (randomID % ((1U<<31)-32768)) + 32768; 881*3117ece4Schristos U32 const dictID = params.dictID ? params.dictID : compliantID; 882*3117ece4Schristos MEM_writeLE32(header+4, dictID); 883*3117ece4Schristos } 884*3117ece4Schristos hSize = 8; 885*3117ece4Schristos 886*3117ece4Schristos /* entropy tables */ 887*3117ece4Schristos DISPLAYLEVEL(2, "\r%70s\r", ""); /* clean display line */ 888*3117ece4Schristos DISPLAYLEVEL(2, "statistics ... \n"); 889*3117ece4Schristos { size_t const eSize = ZDICT_analyzeEntropy(header+hSize, HBUFFSIZE-hSize, 890*3117ece4Schristos compressionLevel, 891*3117ece4Schristos samplesBuffer, samplesSizes, nbSamples, 892*3117ece4Schristos customDictContent, dictContentSize, 893*3117ece4Schristos notificationLevel); 894*3117ece4Schristos if (ZDICT_isError(eSize)) return eSize; 895*3117ece4Schristos hSize += eSize; 896*3117ece4Schristos } 897*3117ece4Schristos 898*3117ece4Schristos /* Shrink the content size if it doesn't fit in the buffer */ 899*3117ece4Schristos if (hSize + dictContentSize > dictBufferCapacity) { 900*3117ece4Schristos dictContentSize = dictBufferCapacity - hSize; 901*3117ece4Schristos } 902*3117ece4Schristos 903*3117ece4Schristos /* Pad the dictionary content with zeros if it is too small */ 904*3117ece4Schristos if (dictContentSize < minContentSize) { 905*3117ece4Schristos RETURN_ERROR_IF(hSize + minContentSize > dictBufferCapacity, dstSize_tooSmall, 906*3117ece4Schristos "dictBufferCapacity too small to fit max repcode"); 907*3117ece4Schristos paddingSize = minContentSize - dictContentSize; 908*3117ece4Schristos } else { 909*3117ece4Schristos paddingSize = 0; 910*3117ece4Schristos } 911*3117ece4Schristos 912*3117ece4Schristos { 913*3117ece4Schristos size_t const dictSize = hSize + paddingSize + dictContentSize; 914*3117ece4Schristos 915*3117ece4Schristos /* The dictionary consists of the header, optional padding, and the content. 916*3117ece4Schristos * The padding comes before the content because the "best" position in the 917*3117ece4Schristos * dictionary is the last byte. 918*3117ece4Schristos */ 919*3117ece4Schristos BYTE* const outDictHeader = (BYTE*)dictBuffer; 920*3117ece4Schristos BYTE* const outDictPadding = outDictHeader + hSize; 921*3117ece4Schristos BYTE* const outDictContent = outDictPadding + paddingSize; 922*3117ece4Schristos 923*3117ece4Schristos assert(dictSize <= dictBufferCapacity); 924*3117ece4Schristos assert(outDictContent + dictContentSize == (BYTE*)dictBuffer + dictSize); 925*3117ece4Schristos 926*3117ece4Schristos /* First copy the customDictContent into its final location. 927*3117ece4Schristos * `customDictContent` and `dictBuffer` may overlap, so we must 928*3117ece4Schristos * do this before any other writes into the output buffer. 929*3117ece4Schristos * Then copy the header & padding into the output buffer. 930*3117ece4Schristos */ 931*3117ece4Schristos memmove(outDictContent, customDictContent, dictContentSize); 932*3117ece4Schristos memcpy(outDictHeader, header, hSize); 933*3117ece4Schristos memset(outDictPadding, 0, paddingSize); 934*3117ece4Schristos 935*3117ece4Schristos return dictSize; 936*3117ece4Schristos } 937*3117ece4Schristos } 938*3117ece4Schristos 939*3117ece4Schristos 940*3117ece4Schristos static size_t ZDICT_addEntropyTablesFromBuffer_advanced( 941*3117ece4Schristos void* dictBuffer, size_t dictContentSize, size_t dictBufferCapacity, 942*3117ece4Schristos const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples, 943*3117ece4Schristos ZDICT_params_t params) 944*3117ece4Schristos { 945*3117ece4Schristos int const compressionLevel = (params.compressionLevel == 0) ? ZSTD_CLEVEL_DEFAULT : params.compressionLevel; 946*3117ece4Schristos U32 const notificationLevel = params.notificationLevel; 947*3117ece4Schristos size_t hSize = 8; 948*3117ece4Schristos 949*3117ece4Schristos /* calculate entropy tables */ 950*3117ece4Schristos DISPLAYLEVEL(2, "\r%70s\r", ""); /* clean display line */ 951*3117ece4Schristos DISPLAYLEVEL(2, "statistics ... \n"); 952*3117ece4Schristos { size_t const eSize = ZDICT_analyzeEntropy((char*)dictBuffer+hSize, dictBufferCapacity-hSize, 953*3117ece4Schristos compressionLevel, 954*3117ece4Schristos samplesBuffer, samplesSizes, nbSamples, 955*3117ece4Schristos (char*)dictBuffer + dictBufferCapacity - dictContentSize, dictContentSize, 956*3117ece4Schristos notificationLevel); 957*3117ece4Schristos if (ZDICT_isError(eSize)) return eSize; 958*3117ece4Schristos hSize += eSize; 959*3117ece4Schristos } 960*3117ece4Schristos 961*3117ece4Schristos /* add dictionary header (after entropy tables) */ 962*3117ece4Schristos MEM_writeLE32(dictBuffer, ZSTD_MAGIC_DICTIONARY); 963*3117ece4Schristos { U64 const randomID = XXH64((char*)dictBuffer + dictBufferCapacity - dictContentSize, dictContentSize, 0); 964*3117ece4Schristos U32 const compliantID = (randomID % ((1U<<31)-32768)) + 32768; 965*3117ece4Schristos U32 const dictID = params.dictID ? params.dictID : compliantID; 966*3117ece4Schristos MEM_writeLE32((char*)dictBuffer+4, dictID); 967*3117ece4Schristos } 968*3117ece4Schristos 969*3117ece4Schristos if (hSize + dictContentSize < dictBufferCapacity) 970*3117ece4Schristos memmove((char*)dictBuffer + hSize, (char*)dictBuffer + dictBufferCapacity - dictContentSize, dictContentSize); 971*3117ece4Schristos return MIN(dictBufferCapacity, hSize+dictContentSize); 972*3117ece4Schristos } 973*3117ece4Schristos 974*3117ece4Schristos /*! ZDICT_trainFromBuffer_unsafe_legacy() : 975*3117ece4Schristos * Warning : `samplesBuffer` must be followed by noisy guard band !!! 976*3117ece4Schristos * @return : size of dictionary, or an error code which can be tested with ZDICT_isError() 977*3117ece4Schristos */ 978*3117ece4Schristos static size_t ZDICT_trainFromBuffer_unsafe_legacy( 979*3117ece4Schristos void* dictBuffer, size_t maxDictSize, 980*3117ece4Schristos const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples, 981*3117ece4Schristos ZDICT_legacy_params_t params) 982*3117ece4Schristos { 983*3117ece4Schristos U32 const dictListSize = MAX(MAX(DICTLISTSIZE_DEFAULT, nbSamples), (U32)(maxDictSize/16)); 984*3117ece4Schristos dictItem* const dictList = (dictItem*)malloc(dictListSize * sizeof(*dictList)); 985*3117ece4Schristos unsigned const selectivity = params.selectivityLevel == 0 ? g_selectivity_default : params.selectivityLevel; 986*3117ece4Schristos unsigned const minRep = (selectivity > 30) ? MINRATIO : nbSamples >> selectivity; 987*3117ece4Schristos size_t const targetDictSize = maxDictSize; 988*3117ece4Schristos size_t const samplesBuffSize = ZDICT_totalSampleSize(samplesSizes, nbSamples); 989*3117ece4Schristos size_t dictSize = 0; 990*3117ece4Schristos U32 const notificationLevel = params.zParams.notificationLevel; 991*3117ece4Schristos 992*3117ece4Schristos /* checks */ 993*3117ece4Schristos if (!dictList) return ERROR(memory_allocation); 994*3117ece4Schristos if (maxDictSize < ZDICT_DICTSIZE_MIN) { free(dictList); return ERROR(dstSize_tooSmall); } /* requested dictionary size is too small */ 995*3117ece4Schristos if (samplesBuffSize < ZDICT_MIN_SAMPLES_SIZE) { free(dictList); return ERROR(dictionaryCreation_failed); } /* not enough source to create dictionary */ 996*3117ece4Schristos 997*3117ece4Schristos /* init */ 998*3117ece4Schristos ZDICT_initDictItem(dictList); 999*3117ece4Schristos 1000*3117ece4Schristos /* build dictionary */ 1001*3117ece4Schristos ZDICT_trainBuffer_legacy(dictList, dictListSize, 1002*3117ece4Schristos samplesBuffer, samplesBuffSize, 1003*3117ece4Schristos samplesSizes, nbSamples, 1004*3117ece4Schristos minRep, notificationLevel); 1005*3117ece4Schristos 1006*3117ece4Schristos /* display best matches */ 1007*3117ece4Schristos if (params.zParams.notificationLevel>= 3) { 1008*3117ece4Schristos unsigned const nb = MIN(25, dictList[0].pos); 1009*3117ece4Schristos unsigned const dictContentSize = ZDICT_dictSize(dictList); 1010*3117ece4Schristos unsigned u; 1011*3117ece4Schristos DISPLAYLEVEL(3, "\n %u segments found, of total size %u \n", (unsigned)dictList[0].pos-1, dictContentSize); 1012*3117ece4Schristos DISPLAYLEVEL(3, "list %u best segments \n", nb-1); 1013*3117ece4Schristos for (u=1; u<nb; u++) { 1014*3117ece4Schristos unsigned const pos = dictList[u].pos; 1015*3117ece4Schristos unsigned const length = dictList[u].length; 1016*3117ece4Schristos U32 const printedLength = MIN(40, length); 1017*3117ece4Schristos if ((pos > samplesBuffSize) || ((pos + length) > samplesBuffSize)) { 1018*3117ece4Schristos free(dictList); 1019*3117ece4Schristos return ERROR(GENERIC); /* should never happen */ 1020*3117ece4Schristos } 1021*3117ece4Schristos DISPLAYLEVEL(3, "%3u:%3u bytes at pos %8u, savings %7u bytes |", 1022*3117ece4Schristos u, length, pos, (unsigned)dictList[u].savings); 1023*3117ece4Schristos ZDICT_printHex((const char*)samplesBuffer+pos, printedLength); 1024*3117ece4Schristos DISPLAYLEVEL(3, "| \n"); 1025*3117ece4Schristos } } 1026*3117ece4Schristos 1027*3117ece4Schristos 1028*3117ece4Schristos /* create dictionary */ 1029*3117ece4Schristos { unsigned dictContentSize = ZDICT_dictSize(dictList); 1030*3117ece4Schristos if (dictContentSize < ZDICT_CONTENTSIZE_MIN) { free(dictList); return ERROR(dictionaryCreation_failed); } /* dictionary content too small */ 1031*3117ece4Schristos if (dictContentSize < targetDictSize/4) { 1032*3117ece4Schristos DISPLAYLEVEL(2, "! warning : selected content significantly smaller than requested (%u < %u) \n", dictContentSize, (unsigned)maxDictSize); 1033*3117ece4Schristos if (samplesBuffSize < 10 * targetDictSize) 1034*3117ece4Schristos DISPLAYLEVEL(2, "! consider increasing the number of samples (total size : %u MB)\n", (unsigned)(samplesBuffSize>>20)); 1035*3117ece4Schristos if (minRep > MINRATIO) { 1036*3117ece4Schristos DISPLAYLEVEL(2, "! consider increasing selectivity to produce larger dictionary (-s%u) \n", selectivity+1); 1037*3117ece4Schristos DISPLAYLEVEL(2, "! note : larger dictionaries are not necessarily better, test its efficiency on samples \n"); 1038*3117ece4Schristos } 1039*3117ece4Schristos } 1040*3117ece4Schristos 1041*3117ece4Schristos if ((dictContentSize > targetDictSize*3) && (nbSamples > 2*MINRATIO) && (selectivity>1)) { 1042*3117ece4Schristos unsigned proposedSelectivity = selectivity-1; 1043*3117ece4Schristos while ((nbSamples >> proposedSelectivity) <= MINRATIO) { proposedSelectivity--; } 1044*3117ece4Schristos DISPLAYLEVEL(2, "! note : calculated dictionary significantly larger than requested (%u > %u) \n", dictContentSize, (unsigned)maxDictSize); 1045*3117ece4Schristos DISPLAYLEVEL(2, "! consider increasing dictionary size, or produce denser dictionary (-s%u) \n", proposedSelectivity); 1046*3117ece4Schristos DISPLAYLEVEL(2, "! always test dictionary efficiency on real samples \n"); 1047*3117ece4Schristos } 1048*3117ece4Schristos 1049*3117ece4Schristos /* limit dictionary size */ 1050*3117ece4Schristos { U32 const max = dictList->pos; /* convention : nb of useful elts within dictList */ 1051*3117ece4Schristos U32 currentSize = 0; 1052*3117ece4Schristos U32 n; for (n=1; n<max; n++) { 1053*3117ece4Schristos currentSize += dictList[n].length; 1054*3117ece4Schristos if (currentSize > targetDictSize) { currentSize -= dictList[n].length; break; } 1055*3117ece4Schristos } 1056*3117ece4Schristos dictList->pos = n; 1057*3117ece4Schristos dictContentSize = currentSize; 1058*3117ece4Schristos } 1059*3117ece4Schristos 1060*3117ece4Schristos /* build dict content */ 1061*3117ece4Schristos { U32 u; 1062*3117ece4Schristos BYTE* ptr = (BYTE*)dictBuffer + maxDictSize; 1063*3117ece4Schristos for (u=1; u<dictList->pos; u++) { 1064*3117ece4Schristos U32 l = dictList[u].length; 1065*3117ece4Schristos ptr -= l; 1066*3117ece4Schristos if (ptr<(BYTE*)dictBuffer) { free(dictList); return ERROR(GENERIC); } /* should not happen */ 1067*3117ece4Schristos memcpy(ptr, (const char*)samplesBuffer+dictList[u].pos, l); 1068*3117ece4Schristos } } 1069*3117ece4Schristos 1070*3117ece4Schristos dictSize = ZDICT_addEntropyTablesFromBuffer_advanced(dictBuffer, dictContentSize, maxDictSize, 1071*3117ece4Schristos samplesBuffer, samplesSizes, nbSamples, 1072*3117ece4Schristos params.zParams); 1073*3117ece4Schristos } 1074*3117ece4Schristos 1075*3117ece4Schristos /* clean up */ 1076*3117ece4Schristos free(dictList); 1077*3117ece4Schristos return dictSize; 1078*3117ece4Schristos } 1079*3117ece4Schristos 1080*3117ece4Schristos 1081*3117ece4Schristos /* ZDICT_trainFromBuffer_legacy() : 1082*3117ece4Schristos * issue : samplesBuffer need to be followed by a noisy guard band. 1083*3117ece4Schristos * work around : duplicate the buffer, and add the noise */ 1084*3117ece4Schristos size_t ZDICT_trainFromBuffer_legacy(void* dictBuffer, size_t dictBufferCapacity, 1085*3117ece4Schristos const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples, 1086*3117ece4Schristos ZDICT_legacy_params_t params) 1087*3117ece4Schristos { 1088*3117ece4Schristos size_t result; 1089*3117ece4Schristos void* newBuff; 1090*3117ece4Schristos size_t const sBuffSize = ZDICT_totalSampleSize(samplesSizes, nbSamples); 1091*3117ece4Schristos if (sBuffSize < ZDICT_MIN_SAMPLES_SIZE) return 0; /* not enough content => no dictionary */ 1092*3117ece4Schristos 1093*3117ece4Schristos newBuff = malloc(sBuffSize + NOISELENGTH); 1094*3117ece4Schristos if (!newBuff) return ERROR(memory_allocation); 1095*3117ece4Schristos 1096*3117ece4Schristos memcpy(newBuff, samplesBuffer, sBuffSize); 1097*3117ece4Schristos ZDICT_fillNoise((char*)newBuff + sBuffSize, NOISELENGTH); /* guard band, for end of buffer condition */ 1098*3117ece4Schristos 1099*3117ece4Schristos result = 1100*3117ece4Schristos ZDICT_trainFromBuffer_unsafe_legacy(dictBuffer, dictBufferCapacity, newBuff, 1101*3117ece4Schristos samplesSizes, nbSamples, params); 1102*3117ece4Schristos free(newBuff); 1103*3117ece4Schristos return result; 1104*3117ece4Schristos } 1105*3117ece4Schristos 1106*3117ece4Schristos 1107*3117ece4Schristos size_t ZDICT_trainFromBuffer(void* dictBuffer, size_t dictBufferCapacity, 1108*3117ece4Schristos const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples) 1109*3117ece4Schristos { 1110*3117ece4Schristos ZDICT_fastCover_params_t params; 1111*3117ece4Schristos DEBUGLOG(3, "ZDICT_trainFromBuffer"); 1112*3117ece4Schristos memset(¶ms, 0, sizeof(params)); 1113*3117ece4Schristos params.d = 8; 1114*3117ece4Schristos params.steps = 4; 1115*3117ece4Schristos /* Use default level since no compression level information is available */ 1116*3117ece4Schristos params.zParams.compressionLevel = ZSTD_CLEVEL_DEFAULT; 1117*3117ece4Schristos #if defined(DEBUGLEVEL) && (DEBUGLEVEL>=1) 1118*3117ece4Schristos params.zParams.notificationLevel = DEBUGLEVEL; 1119*3117ece4Schristos #endif 1120*3117ece4Schristos return ZDICT_optimizeTrainFromBuffer_fastCover(dictBuffer, dictBufferCapacity, 1121*3117ece4Schristos samplesBuffer, samplesSizes, nbSamples, 1122*3117ece4Schristos ¶ms); 1123*3117ece4Schristos } 1124*3117ece4Schristos 1125*3117ece4Schristos size_t ZDICT_addEntropyTablesFromBuffer(void* dictBuffer, size_t dictContentSize, size_t dictBufferCapacity, 1126*3117ece4Schristos const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples) 1127*3117ece4Schristos { 1128*3117ece4Schristos ZDICT_params_t params; 1129*3117ece4Schristos memset(¶ms, 0, sizeof(params)); 1130*3117ece4Schristos return ZDICT_addEntropyTablesFromBuffer_advanced(dictBuffer, dictContentSize, dictBufferCapacity, 1131*3117ece4Schristos samplesBuffer, samplesSizes, nbSamples, 1132*3117ece4Schristos params); 1133*3117ece4Schristos } 1134