zdict.c 43 KB

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