You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

zdict.c 41KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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 (register 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 length;
  182. size_t maxLength = LLIMIT;
  183. size_t pos = suffix[start];
  184. U32 end = start;
  185. dictItem solution;
  186. /* init */
  187. memset(&solution, 0, sizeof(solution));
  188. doneMarks[pos] = 1;
  189. /* trivial repetition cases */
  190. if ( (MEM_read16(b+pos+0) == MEM_read16(b+pos+2))
  191. ||(MEM_read16(b+pos+1) == MEM_read16(b+pos+3))
  192. ||(MEM_read16(b+pos+2) == MEM_read16(b+pos+4)) ) {
  193. /* skip and mark segment */
  194. U16 u16 = MEM_read16(b+pos+4);
  195. U32 u, e = 6;
  196. while (MEM_read16(b+pos+e) == u16) e+=2 ;
  197. if (b[pos+e] == b[pos+e-1]) e++;
  198. for (u=1; u<e; u++)
  199. doneMarks[pos+u] = 1;
  200. return solution;
  201. }
  202. /* look forward */
  203. do {
  204. end++;
  205. length = ZDICT_count(b + pos, b + suffix[end]);
  206. } while (length >=MINMATCHLENGTH);
  207. /* look backward */
  208. do {
  209. length = ZDICT_count(b + pos, b + *(suffix+start-1));
  210. if (length >=MINMATCHLENGTH) start--;
  211. } while(length >= MINMATCHLENGTH);
  212. /* exit if not found a minimum nb of repetitions */
  213. if (end-start < minRatio) {
  214. U32 idx;
  215. for(idx=start; idx<end; idx++)
  216. doneMarks[suffix[idx]] = 1;
  217. return solution;
  218. }
  219. { int i;
  220. U32 searchLength;
  221. U32 refinedStart = start;
  222. U32 refinedEnd = end;
  223. DISPLAYLEVEL(4, "\n");
  224. DISPLAYLEVEL(4, "found %3u matches of length >= %i at pos %7u ", (U32)(end-start), MINMATCHLENGTH, (U32)pos);
  225. DISPLAYLEVEL(4, "\n");
  226. for (searchLength = MINMATCHLENGTH ; ; searchLength++) {
  227. BYTE currentChar = 0;
  228. U32 currentCount = 0;
  229. U32 currentID = refinedStart;
  230. U32 id;
  231. U32 selectedCount = 0;
  232. U32 selectedID = currentID;
  233. for (id =refinedStart; id < refinedEnd; id++) {
  234. if (b[ suffix[id] + searchLength] != currentChar) {
  235. if (currentCount > selectedCount) {
  236. selectedCount = currentCount;
  237. selectedID = currentID;
  238. }
  239. currentID = id;
  240. currentChar = b[ suffix[id] + searchLength];
  241. currentCount = 0;
  242. }
  243. currentCount ++;
  244. }
  245. if (currentCount > selectedCount) { /* for last */
  246. selectedCount = currentCount;
  247. selectedID = currentID;
  248. }
  249. if (selectedCount < minRatio)
  250. break;
  251. refinedStart = selectedID;
  252. refinedEnd = refinedStart + selectedCount;
  253. }
  254. /* evaluate gain based on new ref */
  255. start = refinedStart;
  256. pos = suffix[refinedStart];
  257. end = start;
  258. memset(lengthList, 0, sizeof(lengthList));
  259. /* look forward */
  260. do {
  261. end++;
  262. length = ZDICT_count(b + pos, b + suffix[end]);
  263. if (length >= LLIMIT) length = LLIMIT-1;
  264. lengthList[length]++;
  265. } while (length >=MINMATCHLENGTH);
  266. /* look backward */
  267. length = MINMATCHLENGTH;
  268. while ((length >= MINMATCHLENGTH) & (start > 0)) {
  269. length = ZDICT_count(b + pos, b + suffix[start - 1]);
  270. if (length >= LLIMIT) length = LLIMIT - 1;
  271. lengthList[length]++;
  272. if (length >= MINMATCHLENGTH) start--;
  273. }
  274. /* largest useful length */
  275. memset(cumulLength, 0, sizeof(cumulLength));
  276. cumulLength[maxLength-1] = lengthList[maxLength-1];
  277. for (i=(int)(maxLength-2); i>=0; i--)
  278. cumulLength[i] = cumulLength[i+1] + lengthList[i];
  279. for (i=LLIMIT-1; i>=MINMATCHLENGTH; i--) if (cumulLength[i]>=minRatio) break;
  280. maxLength = i;
  281. /* reduce maxLength in case of final into repetitive data */
  282. { U32 l = (U32)maxLength;
  283. BYTE const c = b[pos + maxLength-1];
  284. while (b[pos+l-2]==c) l--;
  285. maxLength = l;
  286. }
  287. if (maxLength < MINMATCHLENGTH) return solution; /* skip : no long-enough solution */
  288. /* calculate savings */
  289. savings[5] = 0;
  290. for (i=MINMATCHLENGTH; i<=(int)maxLength; i++)
  291. savings[i] = savings[i-1] + (lengthList[i] * (i-3));
  292. DISPLAYLEVEL(4, "Selected ref at position %u, of length %u : saves %u (ratio: %.2f) \n",
  293. (U32)pos, (U32)maxLength, savings[maxLength], (double)savings[maxLength] / maxLength);
  294. solution.pos = (U32)pos;
  295. solution.length = (U32)maxLength;
  296. solution.savings = savings[maxLength];
  297. /* mark positions done */
  298. { U32 id;
  299. for (id=start; id<end; id++) {
  300. U32 p, pEnd;
  301. U32 const testedPos = suffix[id];
  302. if (testedPos == pos)
  303. length = solution.length;
  304. else {
  305. length = ZDICT_count(b+pos, b+testedPos);
  306. if (length > solution.length) length = solution.length;
  307. }
  308. pEnd = (U32)(testedPos + length);
  309. for (p=testedPos; p<pEnd; p++)
  310. doneMarks[p] = 1;
  311. } } }
  312. return solution;
  313. }
  314. static int isIncluded(const void* in, const void* container, size_t length)
  315. {
  316. const char* const ip = (const char*) in;
  317. const char* const into = (const char*) container;
  318. size_t u;
  319. for (u=0; u<length; u++) { /* works because end of buffer is a noisy guard band */
  320. if (ip[u] != into[u]) break;
  321. }
  322. return u==length;
  323. }
  324. /*! ZDICT_tryMerge() :
  325. check if dictItem can be merged, do it if possible
  326. @return : id of destination elt, 0 if not merged
  327. */
  328. static U32 ZDICT_tryMerge(dictItem* table, dictItem elt, U32 eltNbToSkip, const void* buffer)
  329. {
  330. const U32 tableSize = table->pos;
  331. const U32 eltEnd = elt.pos + elt.length;
  332. const char* const buf = (const char*) buffer;
  333. /* tail overlap */
  334. U32 u; for (u=1; u<tableSize; u++) {
  335. if (u==eltNbToSkip) continue;
  336. if ((table[u].pos > elt.pos) && (table[u].pos <= eltEnd)) { /* overlap, existing > new */
  337. /* append */
  338. U32 const addedLength = table[u].pos - elt.pos;
  339. table[u].length += addedLength;
  340. table[u].pos = elt.pos;
  341. table[u].savings += elt.savings * addedLength / elt.length; /* rough approx */
  342. table[u].savings += elt.length / 8; /* rough approx bonus */
  343. elt = table[u];
  344. /* sort : improve rank */
  345. while ((u>1) && (table[u-1].savings < elt.savings))
  346. table[u] = table[u-1], u--;
  347. table[u] = elt;
  348. return u;
  349. } }
  350. /* front overlap */
  351. for (u=1; u<tableSize; u++) {
  352. if (u==eltNbToSkip) continue;
  353. if ((table[u].pos + table[u].length >= elt.pos) && (table[u].pos < elt.pos)) { /* overlap, existing < new */
  354. /* append */
  355. int const addedLength = (int)eltEnd - (table[u].pos + table[u].length);
  356. table[u].savings += elt.length / 8; /* rough approx bonus */
  357. if (addedLength > 0) { /* otherwise, elt fully included into existing */
  358. table[u].length += addedLength;
  359. table[u].savings += elt.savings * addedLength / elt.length; /* rough approx */
  360. }
  361. /* sort : improve rank */
  362. elt = table[u];
  363. while ((u>1) && (table[u-1].savings < elt.savings))
  364. table[u] = table[u-1], u--;
  365. table[u] = elt;
  366. return u;
  367. }
  368. if (MEM_read64(buf + table[u].pos) == MEM_read64(buf + elt.pos + 1)) {
  369. if (isIncluded(buf + table[u].pos, buf + elt.pos + 1, table[u].length)) {
  370. size_t const addedLength = MAX( (int)elt.length - (int)table[u].length , 1 );
  371. table[u].pos = elt.pos;
  372. table[u].savings += (U32)(elt.savings * addedLength / elt.length);
  373. table[u].length = MIN(elt.length, table[u].length + 1);
  374. return u;
  375. }
  376. }
  377. }
  378. return 0;
  379. }
  380. static void ZDICT_removeDictItem(dictItem* table, U32 id)
  381. {
  382. /* convention : table[0].pos stores nb of elts */
  383. U32 const max = table[0].pos;
  384. U32 u;
  385. if (!id) return; /* protection, should never happen */
  386. for (u=id; u<max-1; u++)
  387. table[u] = table[u+1];
  388. table->pos--;
  389. }
  390. static void ZDICT_insertDictItem(dictItem* table, U32 maxSize, dictItem elt, const void* buffer)
  391. {
  392. /* merge if possible */
  393. U32 mergeId = ZDICT_tryMerge(table, elt, 0, buffer);
  394. if (mergeId) {
  395. U32 newMerge = 1;
  396. while (newMerge) {
  397. newMerge = ZDICT_tryMerge(table, table[mergeId], mergeId, buffer);
  398. if (newMerge) ZDICT_removeDictItem(table, mergeId);
  399. mergeId = newMerge;
  400. }
  401. return;
  402. }
  403. /* insert */
  404. { U32 current;
  405. U32 nextElt = table->pos;
  406. if (nextElt >= maxSize) nextElt = maxSize-1;
  407. current = nextElt-1;
  408. while (table[current].savings < elt.savings) {
  409. table[current+1] = table[current];
  410. current--;
  411. }
  412. table[current+1] = elt;
  413. table->pos = nextElt+1;
  414. }
  415. }
  416. static U32 ZDICT_dictSize(const dictItem* dictList)
  417. {
  418. U32 u, dictSize = 0;
  419. for (u=1; u<dictList[0].pos; u++)
  420. dictSize += dictList[u].length;
  421. return dictSize;
  422. }
  423. static size_t ZDICT_trainBuffer_legacy(dictItem* dictList, U32 dictListSize,
  424. const void* const buffer, size_t bufferSize, /* buffer must end with noisy guard band */
  425. const size_t* fileSizes, unsigned nbFiles,
  426. U32 minRatio, U32 notificationLevel)
  427. {
  428. int* const suffix0 = (int*)malloc((bufferSize+2)*sizeof(*suffix0));
  429. int* const suffix = suffix0+1;
  430. U32* reverseSuffix = (U32*)malloc((bufferSize)*sizeof(*reverseSuffix));
  431. BYTE* doneMarks = (BYTE*)malloc((bufferSize+16)*sizeof(*doneMarks)); /* +16 for overflow security */
  432. U32* filePos = (U32*)malloc(nbFiles * sizeof(*filePos));
  433. size_t result = 0;
  434. clock_t displayClock = 0;
  435. clock_t const refreshRate = CLOCKS_PER_SEC * 3 / 10;
  436. # define DISPLAYUPDATE(l, ...) if (notificationLevel>=l) { \
  437. if (ZDICT_clockSpan(displayClock) > refreshRate) \
  438. { displayClock = clock(); DISPLAY(__VA_ARGS__); \
  439. if (notificationLevel>=4) fflush(stderr); } }
  440. /* init */
  441. DISPLAYLEVEL(2, "\r%70s\r", ""); /* clean display line */
  442. if (!suffix0 || !reverseSuffix || !doneMarks || !filePos) {
  443. result = ERROR(memory_allocation);
  444. goto _cleanup;
  445. }
  446. if (minRatio < MINRATIO) minRatio = MINRATIO;
  447. memset(doneMarks, 0, bufferSize+16);
  448. /* limit sample set size (divsufsort limitation)*/
  449. if (bufferSize > ZDICT_MAX_SAMPLES_SIZE) DISPLAYLEVEL(3, "sample set too large : reduced to %u MB ...\n", (U32)(ZDICT_MAX_SAMPLES_SIZE>>20));
  450. while (bufferSize > ZDICT_MAX_SAMPLES_SIZE) bufferSize -= fileSizes[--nbFiles];
  451. /* sort */
  452. DISPLAYLEVEL(2, "sorting %u files of total size %u MB ...\n", nbFiles, (U32)(bufferSize>>20));
  453. { int const divSuftSortResult = divsufsort((const unsigned char*)buffer, suffix, (int)bufferSize, 0);
  454. if (divSuftSortResult != 0) { result = ERROR(GENERIC); goto _cleanup; }
  455. }
  456. suffix[bufferSize] = (int)bufferSize; /* leads into noise */
  457. suffix0[0] = (int)bufferSize; /* leads into noise */
  458. /* build reverse suffix sort */
  459. { size_t pos;
  460. for (pos=0; pos < bufferSize; pos++)
  461. reverseSuffix[suffix[pos]] = (U32)pos;
  462. /* note filePos tracks borders between samples.
  463. It's not used at this stage, but planned to become useful in a later update */
  464. filePos[0] = 0;
  465. for (pos=1; pos<nbFiles; pos++)
  466. filePos[pos] = (U32)(filePos[pos-1] + fileSizes[pos-1]);
  467. }
  468. DISPLAYLEVEL(2, "finding patterns ... \n");
  469. DISPLAYLEVEL(3, "minimum ratio : %u \n", minRatio);
  470. { U32 cursor; for (cursor=0; cursor < bufferSize; ) {
  471. dictItem solution;
  472. if (doneMarks[cursor]) { cursor++; continue; }
  473. solution = ZDICT_analyzePos(doneMarks, suffix, reverseSuffix[cursor], buffer, minRatio, notificationLevel);
  474. if (solution.length==0) { cursor++; continue; }
  475. ZDICT_insertDictItem(dictList, dictListSize, solution, buffer);
  476. cursor += solution.length;
  477. DISPLAYUPDATE(2, "\r%4.2f %% \r", (double)cursor / bufferSize * 100);
  478. } }
  479. _cleanup:
  480. free(suffix0);
  481. free(reverseSuffix);
  482. free(doneMarks);
  483. free(filePos);
  484. return result;
  485. }
  486. static void ZDICT_fillNoise(void* buffer, size_t length)
  487. {
  488. unsigned const prime1 = 2654435761U;
  489. unsigned const prime2 = 2246822519U;
  490. unsigned acc = prime1;
  491. size_t p=0;;
  492. for (p=0; p<length; p++) {
  493. acc *= prime2;
  494. ((unsigned char*)buffer)[p] = (unsigned char)(acc >> 21);
  495. }
  496. }
  497. typedef struct
  498. {
  499. ZSTD_CCtx* ref;
  500. ZSTD_CCtx* zc;
  501. void* workPlace; /* must be ZSTD_BLOCKSIZE_MAX allocated */
  502. } EStats_ress_t;
  503. #define MAXREPOFFSET 1024
  504. static void ZDICT_countEStats(EStats_ress_t esr, ZSTD_parameters params,
  505. U32* countLit, U32* offsetcodeCount, U32* matchlengthCount, U32* litlengthCount, U32* repOffsets,
  506. const void* src, size_t srcSize, U32 notificationLevel)
  507. {
  508. size_t const blockSizeMax = MIN (ZSTD_BLOCKSIZE_MAX, 1 << params.cParams.windowLog);
  509. size_t cSize;
  510. if (srcSize > blockSizeMax) srcSize = blockSizeMax; /* protection vs large samples */
  511. { size_t const errorCode = ZSTD_copyCCtx(esr.zc, esr.ref, 0);
  512. if (ZSTD_isError(errorCode)) { DISPLAYLEVEL(1, "warning : ZSTD_copyCCtx failed \n"); return; }
  513. }
  514. cSize = ZSTD_compressBlock(esr.zc, esr.workPlace, ZSTD_BLOCKSIZE_MAX, src, srcSize);
  515. if (ZSTD_isError(cSize)) { DISPLAYLEVEL(3, "warning : could not compress sample size %u \n", (U32)srcSize); return; }
  516. if (cSize) { /* if == 0; block is not compressible */
  517. const seqStore_t* seqStorePtr = ZSTD_getSeqStore(esr.zc);
  518. /* literals stats */
  519. { const BYTE* bytePtr;
  520. for(bytePtr = seqStorePtr->litStart; bytePtr < seqStorePtr->lit; bytePtr++)
  521. countLit[*bytePtr]++;
  522. }
  523. /* seqStats */
  524. { U32 const nbSeq = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
  525. ZSTD_seqToCodes(seqStorePtr);
  526. { const BYTE* codePtr = seqStorePtr->ofCode;
  527. U32 u;
  528. for (u=0; u<nbSeq; u++) offsetcodeCount[codePtr[u]]++;
  529. }
  530. { const BYTE* codePtr = seqStorePtr->mlCode;
  531. U32 u;
  532. for (u=0; u<nbSeq; u++) matchlengthCount[codePtr[u]]++;
  533. }
  534. { const BYTE* codePtr = seqStorePtr->llCode;
  535. U32 u;
  536. for (u=0; u<nbSeq; u++) litlengthCount[codePtr[u]]++;
  537. }
  538. if (nbSeq >= 2) { /* rep offsets */
  539. const seqDef* const seq = seqStorePtr->sequencesStart;
  540. U32 offset1 = seq[0].offset - 3;
  541. U32 offset2 = seq[1].offset - 3;
  542. if (offset1 >= MAXREPOFFSET) offset1 = 0;
  543. if (offset2 >= MAXREPOFFSET) offset2 = 0;
  544. repOffsets[offset1] += 3;
  545. repOffsets[offset2] += 1;
  546. } } }
  547. }
  548. static size_t ZDICT_totalSampleSize(const size_t* fileSizes, unsigned nbFiles)
  549. {
  550. size_t total=0;
  551. unsigned u;
  552. for (u=0; u<nbFiles; u++) total += fileSizes[u];
  553. return total;
  554. }
  555. typedef struct { U32 offset; U32 count; } offsetCount_t;
  556. static void ZDICT_insertSortCount(offsetCount_t table[ZSTD_REP_NUM+1], U32 val, U32 count)
  557. {
  558. U32 u;
  559. table[ZSTD_REP_NUM].offset = val;
  560. table[ZSTD_REP_NUM].count = count;
  561. for (u=ZSTD_REP_NUM; u>0; u--) {
  562. offsetCount_t tmp;
  563. if (table[u-1].count >= table[u].count) break;
  564. tmp = table[u-1];
  565. table[u-1] = table[u];
  566. table[u] = tmp;
  567. }
  568. }
  569. #define OFFCODE_MAX 30 /* only applicable to first block */
  570. static size_t ZDICT_analyzeEntropy(void* dstBuffer, size_t maxDstSize,
  571. unsigned compressionLevel,
  572. const void* srcBuffer, const size_t* fileSizes, unsigned nbFiles,
  573. const void* dictBuffer, size_t dictBufferSize,
  574. unsigned notificationLevel)
  575. {
  576. U32 countLit[256];
  577. HUF_CREATE_STATIC_CTABLE(hufTable, 255);
  578. U32 offcodeCount[OFFCODE_MAX+1];
  579. short offcodeNCount[OFFCODE_MAX+1];
  580. U32 offcodeMax = ZSTD_highbit32((U32)(dictBufferSize + 128 KB));
  581. U32 matchLengthCount[MaxML+1];
  582. short matchLengthNCount[MaxML+1];
  583. U32 litLengthCount[MaxLL+1];
  584. short litLengthNCount[MaxLL+1];
  585. U32 repOffset[MAXREPOFFSET];
  586. offsetCount_t bestRepOffset[ZSTD_REP_NUM+1];
  587. EStats_ress_t esr;
  588. ZSTD_parameters params;
  589. U32 u, huffLog = 11, Offlog = OffFSELog, mlLog = MLFSELog, llLog = LLFSELog, total;
  590. size_t pos = 0, errorCode;
  591. size_t eSize = 0;
  592. size_t const totalSrcSize = ZDICT_totalSampleSize(fileSizes, nbFiles);
  593. size_t const averageSampleSize = totalSrcSize / (nbFiles + !nbFiles);
  594. BYTE* dstPtr = (BYTE*)dstBuffer;
  595. /* init */
  596. esr.ref = ZSTD_createCCtx();
  597. esr.zc = ZSTD_createCCtx();
  598. esr.workPlace = malloc(ZSTD_BLOCKSIZE_MAX);
  599. if (!esr.ref || !esr.zc || !esr.workPlace) {
  600. eSize = ERROR(memory_allocation);
  601. DISPLAYLEVEL(1, "Not enough memory \n");
  602. goto _cleanup;
  603. }
  604. if (offcodeMax>OFFCODE_MAX) { eSize = ERROR(dictionaryCreation_failed); goto _cleanup; } /* too large dictionary */
  605. for (u=0; u<256; u++) countLit[u] = 1; /* any character must be described */
  606. for (u=0; u<=offcodeMax; u++) offcodeCount[u] = 1;
  607. for (u=0; u<=MaxML; u++) matchLengthCount[u] = 1;
  608. for (u=0; u<=MaxLL; u++) litLengthCount[u] = 1;
  609. memset(repOffset, 0, sizeof(repOffset));
  610. repOffset[1] = repOffset[4] = repOffset[8] = 1;
  611. memset(bestRepOffset, 0, sizeof(bestRepOffset));
  612. if (compressionLevel<=0) compressionLevel = g_compressionLevel_default;
  613. params = ZSTD_getParams(compressionLevel, averageSampleSize, dictBufferSize);
  614. { size_t const beginResult = ZSTD_compressBegin_advanced(esr.ref, dictBuffer, dictBufferSize, params, 0);
  615. if (ZSTD_isError(beginResult)) {
  616. DISPLAYLEVEL(1, "error : ZSTD_compressBegin_advanced() failed : %s \n", ZSTD_getErrorName(beginResult));
  617. eSize = ERROR(GENERIC);
  618. goto _cleanup;
  619. } }
  620. /* collect stats on all files */
  621. for (u=0; u<nbFiles; u++) {
  622. ZDICT_countEStats(esr, params,
  623. countLit, offcodeCount, matchLengthCount, litLengthCount, repOffset,
  624. (const char*)srcBuffer + pos, fileSizes[u],
  625. notificationLevel);
  626. pos += fileSizes[u];
  627. }
  628. /* analyze */
  629. errorCode = HUF_buildCTable (hufTable, countLit, 255, huffLog);
  630. if (HUF_isError(errorCode)) {
  631. eSize = ERROR(GENERIC);
  632. DISPLAYLEVEL(1, "HUF_buildCTable error \n");
  633. goto _cleanup;
  634. }
  635. huffLog = (U32)errorCode;
  636. /* looking for most common first offsets */
  637. { U32 offset;
  638. for (offset=1; offset<MAXREPOFFSET; offset++)
  639. ZDICT_insertSortCount(bestRepOffset, offset, repOffset[offset]);
  640. }
  641. /* note : the result of this phase should be used to better appreciate the impact on statistics */
  642. total=0; for (u=0; u<=offcodeMax; u++) total+=offcodeCount[u];
  643. errorCode = FSE_normalizeCount(offcodeNCount, Offlog, offcodeCount, total, offcodeMax);
  644. if (FSE_isError(errorCode)) {
  645. eSize = ERROR(GENERIC);
  646. DISPLAYLEVEL(1, "FSE_normalizeCount error with offcodeCount \n");
  647. goto _cleanup;
  648. }
  649. Offlog = (U32)errorCode;
  650. total=0; for (u=0; u<=MaxML; u++) total+=matchLengthCount[u];
  651. errorCode = FSE_normalizeCount(matchLengthNCount, mlLog, matchLengthCount, total, MaxML);
  652. if (FSE_isError(errorCode)) {
  653. eSize = ERROR(GENERIC);
  654. DISPLAYLEVEL(1, "FSE_normalizeCount error with matchLengthCount \n");
  655. goto _cleanup;
  656. }
  657. mlLog = (U32)errorCode;
  658. total=0; for (u=0; u<=MaxLL; u++) total+=litLengthCount[u];
  659. errorCode = FSE_normalizeCount(litLengthNCount, llLog, litLengthCount, total, MaxLL);
  660. if (FSE_isError(errorCode)) {
  661. eSize = ERROR(GENERIC);
  662. DISPLAYLEVEL(1, "FSE_normalizeCount error with litLengthCount \n");
  663. goto _cleanup;
  664. }
  665. llLog = (U32)errorCode;
  666. /* write result to buffer */
  667. { size_t const hhSize = HUF_writeCTable(dstPtr, maxDstSize, hufTable, 255, huffLog);
  668. if (HUF_isError(hhSize)) {
  669. eSize = ERROR(GENERIC);
  670. DISPLAYLEVEL(1, "HUF_writeCTable error \n");
  671. goto _cleanup;
  672. }
  673. dstPtr += hhSize;
  674. maxDstSize -= hhSize;
  675. eSize += hhSize;
  676. }
  677. { size_t const ohSize = FSE_writeNCount(dstPtr, maxDstSize, offcodeNCount, OFFCODE_MAX, Offlog);
  678. if (FSE_isError(ohSize)) {
  679. eSize = ERROR(GENERIC);
  680. DISPLAYLEVEL(1, "FSE_writeNCount error with offcodeNCount \n");
  681. goto _cleanup;
  682. }
  683. dstPtr += ohSize;
  684. maxDstSize -= ohSize;
  685. eSize += ohSize;
  686. }
  687. { size_t const mhSize = FSE_writeNCount(dstPtr, maxDstSize, matchLengthNCount, MaxML, mlLog);
  688. if (FSE_isError(mhSize)) {
  689. eSize = ERROR(GENERIC);
  690. DISPLAYLEVEL(1, "FSE_writeNCount error with matchLengthNCount \n");
  691. goto _cleanup;
  692. }
  693. dstPtr += mhSize;
  694. maxDstSize -= mhSize;
  695. eSize += mhSize;
  696. }
  697. { size_t const lhSize = FSE_writeNCount(dstPtr, maxDstSize, litLengthNCount, MaxLL, llLog);
  698. if (FSE_isError(lhSize)) {
  699. eSize = ERROR(GENERIC);
  700. DISPLAYLEVEL(1, "FSE_writeNCount error with litlengthNCount \n");
  701. goto _cleanup;
  702. }
  703. dstPtr += lhSize;
  704. maxDstSize -= lhSize;
  705. eSize += lhSize;
  706. }
  707. if (maxDstSize<12) {
  708. eSize = ERROR(GENERIC);
  709. DISPLAYLEVEL(1, "not enough space to write RepOffsets \n");
  710. goto _cleanup;
  711. }
  712. # if 0
  713. MEM_writeLE32(dstPtr+0, bestRepOffset[0].offset);
  714. MEM_writeLE32(dstPtr+4, bestRepOffset[1].offset);
  715. MEM_writeLE32(dstPtr+8, bestRepOffset[2].offset);
  716. #else
  717. /* at this stage, we don't use the result of "most common first offset",
  718. as the impact of statistics is not properly evaluated */
  719. MEM_writeLE32(dstPtr+0, repStartValue[0]);
  720. MEM_writeLE32(dstPtr+4, repStartValue[1]);
  721. MEM_writeLE32(dstPtr+8, repStartValue[2]);
  722. #endif
  723. eSize += 12;
  724. _cleanup:
  725. ZSTD_freeCCtx(esr.ref);
  726. ZSTD_freeCCtx(esr.zc);
  727. free(esr.workPlace);
  728. return eSize;
  729. }
  730. size_t ZDICT_finalizeDictionary(void* dictBuffer, size_t dictBufferCapacity,
  731. const void* customDictContent, size_t dictContentSize,
  732. const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples,
  733. ZDICT_params_t params)
  734. {
  735. size_t hSize;
  736. #define HBUFFSIZE 256 /* should prove large enough for all entropy headers */
  737. BYTE header[HBUFFSIZE];
  738. int const compressionLevel = (params.compressionLevel <= 0) ? g_compressionLevel_default : params.compressionLevel;
  739. U32 const notificationLevel = params.notificationLevel;
  740. /* check conditions */
  741. if (dictBufferCapacity < dictContentSize) return ERROR(dstSize_tooSmall);
  742. if (dictContentSize < ZDICT_CONTENTSIZE_MIN) return ERROR(srcSize_wrong);
  743. if (dictBufferCapacity < ZDICT_DICTSIZE_MIN) return ERROR(dstSize_tooSmall);
  744. /* dictionary header */
  745. MEM_writeLE32(header, ZSTD_MAGIC_DICTIONARY);
  746. { U64 const randomID = XXH64(customDictContent, dictContentSize, 0);
  747. U32 const compliantID = (randomID % ((1U<<31)-32768)) + 32768;
  748. U32 const dictID = params.dictID ? params.dictID : compliantID;
  749. MEM_writeLE32(header+4, dictID);
  750. }
  751. hSize = 8;
  752. /* entropy tables */
  753. DISPLAYLEVEL(2, "\r%70s\r", ""); /* clean display line */
  754. DISPLAYLEVEL(2, "statistics ... \n");
  755. { size_t const eSize = ZDICT_analyzeEntropy(header+hSize, HBUFFSIZE-hSize,
  756. compressionLevel,
  757. samplesBuffer, samplesSizes, nbSamples,
  758. customDictContent, dictContentSize,
  759. notificationLevel);
  760. if (ZDICT_isError(eSize)) return eSize;
  761. hSize += eSize;
  762. }
  763. /* copy elements in final buffer ; note : src and dst buffer can overlap */
  764. if (hSize + dictContentSize > dictBufferCapacity) dictContentSize = dictBufferCapacity - hSize;
  765. { size_t const dictSize = hSize + dictContentSize;
  766. char* dictEnd = (char*)dictBuffer + dictSize;
  767. memmove(dictEnd - dictContentSize, customDictContent, dictContentSize);
  768. memcpy(dictBuffer, header, hSize);
  769. return dictSize;
  770. }
  771. }
  772. size_t ZDICT_addEntropyTablesFromBuffer_advanced(void* dictBuffer, size_t dictContentSize, size_t dictBufferCapacity,
  773. const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples,
  774. ZDICT_params_t params)
  775. {
  776. int const compressionLevel = (params.compressionLevel <= 0) ? g_compressionLevel_default : params.compressionLevel;
  777. U32 const notificationLevel = params.notificationLevel;
  778. size_t hSize = 8;
  779. /* calculate entropy tables */
  780. DISPLAYLEVEL(2, "\r%70s\r", ""); /* clean display line */
  781. DISPLAYLEVEL(2, "statistics ... \n");
  782. { size_t const eSize = ZDICT_analyzeEntropy((char*)dictBuffer+hSize, dictBufferCapacity-hSize,
  783. compressionLevel,
  784. samplesBuffer, samplesSizes, nbSamples,
  785. (char*)dictBuffer + dictBufferCapacity - dictContentSize, dictContentSize,
  786. notificationLevel);
  787. if (ZDICT_isError(eSize)) return eSize;
  788. hSize += eSize;
  789. }
  790. /* add dictionary header (after entropy tables) */
  791. MEM_writeLE32(dictBuffer, ZSTD_MAGIC_DICTIONARY);
  792. { U64 const randomID = XXH64((char*)dictBuffer + dictBufferCapacity - dictContentSize, dictContentSize, 0);
  793. U32 const compliantID = (randomID % ((1U<<31)-32768)) + 32768;
  794. U32 const dictID = params.dictID ? params.dictID : compliantID;
  795. MEM_writeLE32((char*)dictBuffer+4, dictID);
  796. }
  797. if (hSize + dictContentSize < dictBufferCapacity)
  798. memmove((char*)dictBuffer + hSize, (char*)dictBuffer + dictBufferCapacity - dictContentSize, dictContentSize);
  799. return MIN(dictBufferCapacity, hSize+dictContentSize);
  800. }
  801. /*! ZDICT_trainFromBuffer_unsafe_legacy() :
  802. * Warning : `samplesBuffer` must be followed by noisy guard band.
  803. * @return : size of dictionary, or an error code which can be tested with ZDICT_isError()
  804. */
  805. size_t ZDICT_trainFromBuffer_unsafe_legacy(
  806. void* dictBuffer, size_t maxDictSize,
  807. const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples,
  808. ZDICT_legacy_params_t params)
  809. {
  810. U32 const dictListSize = MAX(MAX(DICTLISTSIZE_DEFAULT, nbSamples), (U32)(maxDictSize/16));
  811. dictItem* const dictList = (dictItem*)malloc(dictListSize * sizeof(*dictList));
  812. unsigned const selectivity = params.selectivityLevel == 0 ? g_selectivity_default : params.selectivityLevel;
  813. unsigned const minRep = (selectivity > 30) ? MINRATIO : nbSamples >> selectivity;
  814. size_t const targetDictSize = maxDictSize;
  815. size_t const samplesBuffSize = ZDICT_totalSampleSize(samplesSizes, nbSamples);
  816. size_t dictSize = 0;
  817. U32 const notificationLevel = params.zParams.notificationLevel;
  818. /* checks */
  819. if (!dictList) return ERROR(memory_allocation);
  820. if (maxDictSize < ZDICT_DICTSIZE_MIN) { free(dictList); return ERROR(dstSize_tooSmall); } /* requested dictionary size is too small */
  821. if (samplesBuffSize < ZDICT_MIN_SAMPLES_SIZE) { free(dictList); return ERROR(dictionaryCreation_failed); } /* not enough source to create dictionary */
  822. /* init */
  823. ZDICT_initDictItem(dictList);
  824. /* build dictionary */
  825. ZDICT_trainBuffer_legacy(dictList, dictListSize,
  826. samplesBuffer, samplesBuffSize,
  827. samplesSizes, nbSamples,
  828. minRep, notificationLevel);
  829. /* display best matches */
  830. if (params.zParams.notificationLevel>= 3) {
  831. U32 const nb = MIN(25, dictList[0].pos);
  832. U32 const dictContentSize = ZDICT_dictSize(dictList);
  833. U32 u;
  834. DISPLAYLEVEL(3, "\n %u segments found, of total size %u \n", dictList[0].pos-1, dictContentSize);
  835. DISPLAYLEVEL(3, "list %u best segments \n", nb-1);
  836. for (u=1; u<nb; u++) {
  837. U32 const pos = dictList[u].pos;
  838. U32 const length = dictList[u].length;
  839. U32 const printedLength = MIN(40, length);
  840. if ((pos > samplesBuffSize) || ((pos + length) > samplesBuffSize))
  841. return ERROR(GENERIC); /* should never happen */
  842. DISPLAYLEVEL(3, "%3u:%3u bytes at pos %8u, savings %7u bytes |",
  843. u, length, pos, dictList[u].savings);
  844. ZDICT_printHex((const char*)samplesBuffer+pos, printedLength);
  845. DISPLAYLEVEL(3, "| \n");
  846. } }
  847. /* create dictionary */
  848. { U32 dictContentSize = ZDICT_dictSize(dictList);
  849. if (dictContentSize < ZDICT_CONTENTSIZE_MIN) { free(dictList); return ERROR(dictionaryCreation_failed); } /* dictionary content too small */
  850. if (dictContentSize < targetDictSize/4) {
  851. DISPLAYLEVEL(2, "! warning : selected content significantly smaller than requested (%u < %u) \n", dictContentSize, (U32)maxDictSize);
  852. if (samplesBuffSize < 10 * targetDictSize)
  853. DISPLAYLEVEL(2, "! consider increasing the number of samples (total size : %u MB)\n", (U32)(samplesBuffSize>>20));
  854. if (minRep > MINRATIO) {
  855. DISPLAYLEVEL(2, "! consider increasing selectivity to produce larger dictionary (-s%u) \n", selectivity+1);
  856. DISPLAYLEVEL(2, "! note : larger dictionaries are not necessarily better, test its efficiency on samples \n");
  857. }
  858. }
  859. if ((dictContentSize > targetDictSize*3) && (nbSamples > 2*MINRATIO) && (selectivity>1)) {
  860. U32 proposedSelectivity = selectivity-1;
  861. while ((nbSamples >> proposedSelectivity) <= MINRATIO) { proposedSelectivity--; }
  862. DISPLAYLEVEL(2, "! note : calculated dictionary significantly larger than requested (%u > %u) \n", dictContentSize, (U32)maxDictSize);
  863. DISPLAYLEVEL(2, "! consider increasing dictionary size, or produce denser dictionary (-s%u) \n", proposedSelectivity);
  864. DISPLAYLEVEL(2, "! always test dictionary efficiency on real samples \n");
  865. }
  866. /* limit dictionary size */
  867. { U32 const max = dictList->pos; /* convention : nb of useful elts within dictList */
  868. U32 currentSize = 0;
  869. U32 n; for (n=1; n<max; n++) {
  870. currentSize += dictList[n].length;
  871. if (currentSize > targetDictSize) { currentSize -= dictList[n].length; break; }
  872. }
  873. dictList->pos = n;
  874. dictContentSize = currentSize;
  875. }
  876. /* build dict content */
  877. { U32 u;
  878. BYTE* ptr = (BYTE*)dictBuffer + maxDictSize;
  879. for (u=1; u<dictList->pos; u++) {
  880. U32 l = dictList[u].length;
  881. ptr -= l;
  882. if (ptr<(BYTE*)dictBuffer) { free(dictList); return ERROR(GENERIC); } /* should not happen */
  883. memcpy(ptr, (const char*)samplesBuffer+dictList[u].pos, l);
  884. } }
  885. dictSize = ZDICT_addEntropyTablesFromBuffer_advanced(dictBuffer, dictContentSize, maxDictSize,
  886. samplesBuffer, samplesSizes, nbSamples,
  887. params.zParams);
  888. }
  889. /* clean up */
  890. free(dictList);
  891. return dictSize;
  892. }
  893. /* issue : samplesBuffer need to be followed by a noisy guard band.
  894. * work around : duplicate the buffer, and add the noise */
  895. size_t ZDICT_trainFromBuffer_legacy(void* dictBuffer, size_t dictBufferCapacity,
  896. const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples,
  897. ZDICT_legacy_params_t params)
  898. {
  899. size_t result;
  900. void* newBuff;
  901. size_t const sBuffSize = ZDICT_totalSampleSize(samplesSizes, nbSamples);
  902. if (sBuffSize < ZDICT_MIN_SAMPLES_SIZE) return 0; /* not enough content => no dictionary */
  903. newBuff = malloc(sBuffSize + NOISELENGTH);
  904. if (!newBuff) return ERROR(memory_allocation);
  905. memcpy(newBuff, samplesBuffer, sBuffSize);
  906. ZDICT_fillNoise((char*)newBuff + sBuffSize, NOISELENGTH); /* guard band, for end of buffer condition */
  907. result =
  908. ZDICT_trainFromBuffer_unsafe_legacy(dictBuffer, dictBufferCapacity, newBuff,
  909. samplesSizes, nbSamples, params);
  910. free(newBuff);
  911. return result;
  912. }
  913. size_t ZDICT_trainFromBuffer(void* dictBuffer, size_t dictBufferCapacity,
  914. const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples)
  915. {
  916. ZDICT_cover_params_t params;
  917. memset(&params, 0, sizeof(params));
  918. params.d = 8;
  919. params.steps = 4;
  920. /* Default to level 6 since no compression level information is avaialble */
  921. params.zParams.compressionLevel = 6;
  922. return ZDICT_optimizeTrainFromBuffer_cover(dictBuffer, dictBufferCapacity,
  923. samplesBuffer, samplesSizes,
  924. nbSamples, &params);
  925. }
  926. size_t ZDICT_addEntropyTablesFromBuffer(void* dictBuffer, size_t dictContentSize, size_t dictBufferCapacity,
  927. const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples)
  928. {
  929. ZDICT_params_t params;
  930. memset(&params, 0, sizeof(params));
  931. return ZDICT_addEntropyTablesFromBuffer_advanced(dictBuffer, dictContentSize, dictBufferCapacity,
  932. samplesBuffer, samplesSizes, nbSamples,
  933. params);
  934. }