fuzzer.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890
  1. /*
  2. fuzzer.c - Fuzzer test tool for LZ4
  3. Copyright (C) Yann Collet 2012-2020
  4. GPL v2 License
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. You can contact the author at :
  17. - LZ4 homepage : http://www.lz4.org
  18. - LZ4 source repo : https://github.com/lz4/lz4
  19. */
  20. /*-************************************
  21. * Compiler options
  22. **************************************/
  23. #ifdef _MSC_VER /* Visual Studio */
  24. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  25. # pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */
  26. # pragma warning(disable : 4310) /* disable: C4310: constant char value > 127 */
  27. # pragma warning(disable : 26451) /* disable: C26451: Arithmetic overflow */
  28. #endif
  29. /*-************************************
  30. * Dependencies
  31. **************************************/
  32. #if defined(__unix__) && !defined(_AIX) /* must be included before platform.h for MAP_ANONYMOUS */
  33. # undef _GNU_SOURCE /* in case it's already defined */
  34. # define _GNU_SOURCE /* MAP_ANONYMOUS even in -std=c99 mode */
  35. # include <sys/mman.h> /* mmap */
  36. #endif
  37. #include "platform.h" /* _CRT_SECURE_NO_WARNINGS */
  38. #include "util.h" /* U32 */
  39. #include <stdlib.h>
  40. #include <stdio.h> /* fgets, sscanf */
  41. #include <string.h> /* strcmp */
  42. #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
  43. #include <assert.h>
  44. #include <limits.h> /* INT_MAX */
  45. #if defined(_AIX)
  46. # include <sys/mman.h> /* mmap */
  47. #endif
  48. #define LZ4_DISABLE_DEPRECATE_WARNINGS /* LZ4_decompress_fast */
  49. #define LZ4_STATIC_LINKING_ONLY
  50. #include "lz4.h"
  51. #define LZ4_HC_STATIC_LINKING_ONLY
  52. #include "lz4hc.h"
  53. #define XXH_STATIC_LINKING_ONLY
  54. #include "xxhash.h"
  55. /*-************************************
  56. * Basic Types
  57. **************************************/
  58. #if !defined(__cplusplus) && !(defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
  59. typedef size_t uintptr_t; /* true on most systems, except OpenVMS-64 (which doesn't need address overflow test) */
  60. #endif
  61. /*-************************************
  62. * Constants
  63. **************************************/
  64. #define NB_ATTEMPTS (1<<16)
  65. #define COMPRESSIBLE_NOISE_LENGTH (1 << 21)
  66. #define FUZ_MAX_BLOCK_SIZE (1 << 17)
  67. #define FUZ_MAX_DICT_SIZE (1 << 15)
  68. #define FUZ_COMPRESSIBILITY_DEFAULT 60
  69. #define PRIME1 2654435761U
  70. #define PRIME2 2246822519U
  71. #define PRIME3 3266489917U
  72. #define KB *(1U<<10)
  73. #define MB *(1U<<20)
  74. #define GB *(1U<<30)
  75. /*-***************************************
  76. * Macros
  77. *****************************************/
  78. #define DISPLAY(...) fprintf(stdout, __VA_ARGS__)
  79. #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
  80. static int g_displayLevel = 2;
  81. #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
  82. /*-*******************************************************
  83. * Fuzzer functions
  84. *********************************************************/
  85. static clock_t FUZ_GetClockSpan(clock_t clockStart)
  86. {
  87. return clock() - clockStart; /* works even if overflow; max span ~ 30mn */
  88. }
  89. static void FUZ_displayUpdate(unsigned testNb)
  90. {
  91. static clock_t g_time = 0;
  92. static const clock_t g_refreshRate = CLOCKS_PER_SEC / 5;
  93. if ((FUZ_GetClockSpan(g_time) > g_refreshRate) || (g_displayLevel>=4)) {
  94. g_time = clock();
  95. DISPLAY("\r%5u ", testNb);
  96. fflush(stdout);
  97. }
  98. }
  99. static U32 FUZ_rotl32(U32 u32, U32 nbBits)
  100. {
  101. return ((u32 << nbBits) | (u32 >> (32 - nbBits)));
  102. }
  103. static U32 FUZ_highbit32(U32 v32)
  104. {
  105. unsigned nbBits = 0;
  106. if (v32==0) return 0;
  107. while (v32) { v32 >>= 1; nbBits++; }
  108. return nbBits;
  109. }
  110. static U32 FUZ_rand(U32* src)
  111. {
  112. U32 rand32 = *src;
  113. rand32 *= PRIME1;
  114. rand32 ^= PRIME2;
  115. rand32 = FUZ_rotl32(rand32, 13);
  116. *src = rand32;
  117. return rand32;
  118. }
  119. #define FUZ_RAND15BITS ((FUZ_rand(seed) >> 3) & 32767)
  120. #define FUZ_RANDLENGTH ( ((FUZ_rand(seed) >> 7) & 3) ? (FUZ_rand(seed) % 15) : (FUZ_rand(seed) % 510) + 15)
  121. static void FUZ_fillCompressibleNoiseBuffer(void* buffer, size_t bufferSize, double proba, U32* seed)
  122. {
  123. BYTE* const BBuffer = (BYTE*)buffer;
  124. size_t pos = 0;
  125. U32 const P32 = (U32)(32768 * proba);
  126. /* First Bytes */
  127. while (pos < 20)
  128. BBuffer[pos++] = (BYTE)(FUZ_rand(seed));
  129. while (pos < bufferSize) {
  130. /* Select : Literal (noise) or copy (within 64K) */
  131. if (FUZ_RAND15BITS < P32) {
  132. /* Copy (within 64K) */
  133. size_t const length = (size_t)FUZ_RANDLENGTH + 4;
  134. size_t const d = MIN(pos+length, bufferSize);
  135. size_t match;
  136. size_t offset = (size_t)FUZ_RAND15BITS + 1;
  137. while (offset > pos) offset >>= 1;
  138. match = pos - offset;
  139. while (pos < d) BBuffer[pos++] = BBuffer[match++];
  140. } else {
  141. /* Literal (noise) */
  142. size_t const length = FUZ_RANDLENGTH;
  143. size_t const d = MIN(pos+length, bufferSize);
  144. while (pos < d) BBuffer[pos++] = (BYTE)(FUZ_rand(seed) >> 5);
  145. }
  146. }
  147. }
  148. #define MAX_NB_BUFF_I134 150
  149. #define BLOCKSIZE_I134 (32 MB)
  150. /*! FUZ_AddressOverflow() :
  151. * Aggressively pushes memory allocation limits,
  152. * and generates patterns which create address space overflow.
  153. * only possible in 32-bits mode */
  154. static int FUZ_AddressOverflow(void)
  155. {
  156. char* buffers[MAX_NB_BUFF_I134+1];
  157. int nbBuff=0;
  158. int highAddress = 0;
  159. DISPLAY("Overflow tests : ");
  160. /* Only possible in 32-bits */
  161. if (sizeof(void*)==8) {
  162. DISPLAY("64 bits mode : no overflow \n");
  163. fflush(stdout);
  164. return 0;
  165. }
  166. buffers[0] = (char*)malloc(BLOCKSIZE_I134);
  167. buffers[1] = (char*)malloc(BLOCKSIZE_I134);
  168. if ((!buffers[0]) || (!buffers[1])) {
  169. free(buffers[0]); free(buffers[1]);
  170. DISPLAY("not enough memory for tests \n");
  171. return 0;
  172. }
  173. for (nbBuff=2; nbBuff < MAX_NB_BUFF_I134; nbBuff++) {
  174. DISPLAY("%3i \b\b\b\b", nbBuff); fflush(stdout);
  175. buffers[nbBuff] = (char*)malloc(BLOCKSIZE_I134);
  176. if (buffers[nbBuff]==NULL) goto _endOfTests;
  177. if (((uintptr_t)buffers[nbBuff] > (uintptr_t)0x80000000) && (!highAddress)) {
  178. DISPLAY("high address detected : ");
  179. fflush(stdout);
  180. highAddress=1;
  181. }
  182. { size_t const sizeToGenerateOverflow = (size_t)(- ((uintptr_t)buffers[nbBuff-1]) + 512);
  183. int const nbOf255 = (int)((sizeToGenerateOverflow / 255) + 1);
  184. char* const input = buffers[nbBuff-1];
  185. char* output = buffers[nbBuff];
  186. int r;
  187. input[0] = (char)0xF0; /* Literal length overflow */
  188. input[1] = (char)0xFF;
  189. input[2] = (char)0xFF;
  190. input[3] = (char)0xFF;
  191. { int u; for(u = 4; u <= nbOf255+4; u++) input[u] = (char)0xff; }
  192. r = LZ4_decompress_safe(input, output, nbOf255+64, BLOCKSIZE_I134);
  193. if (r>0) { DISPLAY("LZ4_decompress_safe = %i \n", r); goto _overflowError; }
  194. input[0] = (char)0x1F; /* Match length overflow */
  195. input[1] = (char)0x01;
  196. input[2] = (char)0x01;
  197. input[3] = (char)0x00;
  198. r = LZ4_decompress_safe(input, output, nbOf255+64, BLOCKSIZE_I134);
  199. if (r>0) { DISPLAY("LZ4_decompress_safe = %i \n", r); goto _overflowError; }
  200. output = buffers[nbBuff-2]; /* Reverse in/out pointer order */
  201. input[0] = (char)0xF0; /* Literal length overflow */
  202. input[1] = (char)0xFF;
  203. input[2] = (char)0xFF;
  204. input[3] = (char)0xFF;
  205. r = LZ4_decompress_safe(input, output, nbOf255+64, BLOCKSIZE_I134);
  206. if (r>0) goto _overflowError;
  207. input[0] = (char)0x1F; /* Match length overflow */
  208. input[1] = (char)0x01;
  209. input[2] = (char)0x01;
  210. input[3] = (char)0x00;
  211. r = LZ4_decompress_safe(input, output, nbOf255+64, BLOCKSIZE_I134);
  212. if (r>0) goto _overflowError;
  213. }
  214. }
  215. nbBuff++;
  216. _endOfTests:
  217. { int i; for (i=0 ; i<nbBuff; i++) free(buffers[i]); }
  218. if (!highAddress) DISPLAY("high address not possible \n");
  219. else DISPLAY("all overflows correctly detected \n");
  220. return 0;
  221. _overflowError:
  222. DISPLAY("Address space overflow error !! \n");
  223. exit(1);
  224. }
  225. #ifdef __unix__ /* is expected to be triggered on linux+gcc */
  226. static void* FUZ_createLowAddr(size_t size)
  227. {
  228. void* const lowBuff = mmap((void*)(0x1000), size,
  229. PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
  230. -1, 0);
  231. DISPLAYLEVEL(2, "generating low buffer at address %p \n", lowBuff);
  232. return lowBuff;
  233. }
  234. static void FUZ_freeLowAddr(void* buffer, size_t size)
  235. {
  236. if (munmap(buffer, size)) {
  237. perror("fuzzer: freeing low address buffer");
  238. abort();
  239. }
  240. }
  241. #else
  242. static void* FUZ_createLowAddr(size_t size)
  243. {
  244. return malloc(size);
  245. }
  246. static void FUZ_freeLowAddr(void* buffer, size_t size)
  247. {
  248. (void)size;
  249. free(buffer);
  250. }
  251. #endif
  252. /*! FUZ_findDiff() :
  253. * find the first different byte between buff1 and buff2.
  254. * presumes buff1 != buff2.
  255. * presumes a difference exists before end of either buffer.
  256. * Typically invoked after a checksum mismatch.
  257. */
  258. static void FUZ_findDiff(const void* buff1, const void* buff2)
  259. {
  260. const BYTE* const b1 = (const BYTE*)buff1;
  261. const BYTE* const b2 = (const BYTE*)buff2;
  262. size_t u = 0;
  263. while (b1[u]==b2[u]) u++;
  264. DISPLAY("\nWrong Byte at position %u \n", (unsigned)u);
  265. }
  266. static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double compressibility, U32 duration_s)
  267. {
  268. unsigned long long bytes = 0;
  269. unsigned long long cbytes = 0;
  270. unsigned long long hcbytes = 0;
  271. unsigned long long ccbytes = 0;
  272. void* const CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH);
  273. size_t const compressedBufferSize = (size_t)LZ4_compressBound(FUZ_MAX_BLOCK_SIZE);
  274. char* const compressedBuffer = (char*)malloc(compressedBufferSize);
  275. char* const decodedBuffer = (char*)malloc(FUZ_MAX_DICT_SIZE + FUZ_MAX_BLOCK_SIZE);
  276. size_t const labSize = 96 KB;
  277. void* const lowAddrBuffer = FUZ_createLowAddr(labSize);
  278. void* const stateLZ4 = malloc((size_t)LZ4_sizeofState());
  279. void* const stateLZ4HC = malloc((size_t)LZ4_sizeofStateHC());
  280. LZ4_stream_t LZ4dictBody;
  281. LZ4_streamHC_t* LZ4dictHC = LZ4_createStreamHC();
  282. U32 coreRandState = seed;
  283. clock_t const clockStart = clock();
  284. clock_t const clockDuration = (clock_t)duration_s * CLOCKS_PER_SEC;
  285. int result = 0;
  286. unsigned cycleNb;
  287. # define EXIT_MSG(...) { \
  288. printf("Test %u : ", testNb); printf(__VA_ARGS__); \
  289. printf(" (seed %u, cycle %u) \n", seed, cycleNb); \
  290. exit(1); \
  291. }
  292. # define FUZ_CHECKTEST(cond, ...) if (cond) { EXIT_MSG(__VA_ARGS__) }
  293. # define FUZ_DISPLAYTEST(...) { \
  294. testNb++; \
  295. if (g_displayLevel>=4) { \
  296. printf("\r%4u - %2u :", cycleNb, testNb); \
  297. printf(" " __VA_ARGS__); \
  298. printf(" "); \
  299. fflush(stdout); \
  300. } }
  301. /* init */
  302. if(!CNBuffer || !compressedBuffer || !decodedBuffer || !LZ4dictHC) {
  303. DISPLAY("Not enough memory to start fuzzer tests");
  304. exit(1);
  305. }
  306. if ( LZ4_initStream(&LZ4dictBody, sizeof(LZ4dictBody)) == NULL) abort();
  307. { U32 randState = coreRandState ^ PRIME3;
  308. FUZ_fillCompressibleNoiseBuffer(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, &randState);
  309. }
  310. /* move to startCycle */
  311. for (cycleNb = 0; cycleNb < startCycle; cycleNb++)
  312. (void) FUZ_rand(&coreRandState); /* sync coreRandState */
  313. /* Main test loop */
  314. for (cycleNb = startCycle;
  315. (cycleNb < nbCycles) || (FUZ_GetClockSpan(clockStart) < clockDuration);
  316. cycleNb++) {
  317. U32 testNb = 0;
  318. U32 randState = FUZ_rand(&coreRandState) ^ PRIME3;
  319. int const blockSize = (FUZ_rand(&randState) % (FUZ_MAX_BLOCK_SIZE-1)) + 1;
  320. int const blockStart = (int)(FUZ_rand(&randState) % (U32)(COMPRESSIBLE_NOISE_LENGTH - blockSize - 1)) + 1;
  321. int const dictSizeRand = FUZ_rand(&randState) % FUZ_MAX_DICT_SIZE;
  322. int const dictSize = MIN(dictSizeRand, blockStart - 1);
  323. int const compressionLevel = FUZ_rand(&randState) % (LZ4HC_CLEVEL_MAX+1);
  324. const char* block = ((char*)CNBuffer) + blockStart;
  325. const char* dict = block - dictSize;
  326. int compressedSize, HCcompressedSize;
  327. int blockContinueCompressedSize;
  328. U32 const crcOrig = XXH32(block, (size_t)blockSize, 0);
  329. int ret;
  330. FUZ_displayUpdate(cycleNb);
  331. /* Compression tests */
  332. if ( ((FUZ_rand(&randState) & 63) == 2)
  333. && ((size_t)blockSize < labSize) ) {
  334. memcpy(lowAddrBuffer, block, blockSize);
  335. block = (const char*)lowAddrBuffer;
  336. }
  337. /* Test compression destSize */
  338. FUZ_DISPLAYTEST("test LZ4_compress_destSize()");
  339. { int cSize, srcSize = blockSize;
  340. int const targetSize = srcSize * (int)((FUZ_rand(&randState) & 127)+1) >> 7;
  341. char const endCheck = (char)(FUZ_rand(&randState) & 255);
  342. compressedBuffer[targetSize] = endCheck;
  343. cSize = LZ4_compress_destSize(block, compressedBuffer, &srcSize, targetSize);
  344. FUZ_CHECKTEST(cSize > targetSize, "LZ4_compress_destSize() result larger than dst buffer !");
  345. FUZ_CHECKTEST(compressedBuffer[targetSize] != endCheck, "LZ4_compress_destSize() overwrite dst buffer !");
  346. FUZ_CHECKTEST(srcSize > blockSize, "LZ4_compress_destSize() read more than src buffer !");
  347. DISPLAYLEVEL(5, "destSize : %7i/%7i; content%7i/%7i ", cSize, targetSize, srcSize, blockSize);
  348. if (targetSize>0) {
  349. /* check correctness */
  350. U32 const crcBase = XXH32(block, (size_t)srcSize, 0);
  351. char const canary = (char)(FUZ_rand(&randState) & 255);
  352. FUZ_CHECKTEST((cSize==0), "LZ4_compress_destSize() compression failed");
  353. FUZ_DISPLAYTEST();
  354. decodedBuffer[srcSize] = canary;
  355. { int const dSize = LZ4_decompress_safe(compressedBuffer, decodedBuffer, cSize, srcSize);
  356. FUZ_CHECKTEST(dSize<0, "LZ4_decompress_safe() failed on data compressed by LZ4_compress_destSize");
  357. FUZ_CHECKTEST(dSize!=srcSize, "LZ4_decompress_safe() failed : did not fully decompressed data");
  358. }
  359. FUZ_CHECKTEST(decodedBuffer[srcSize] != canary, "LZ4_decompress_safe() overwrite dst buffer !");
  360. { U32 const crcDec = XXH32(decodedBuffer, (size_t)srcSize, 0);
  361. FUZ_CHECKTEST(crcDec!=crcBase, "LZ4_decompress_safe() corrupted decoded data");
  362. } }
  363. DISPLAYLEVEL(5, " OK \n");
  364. }
  365. /* Test compression HC destSize */
  366. FUZ_DISPLAYTEST("test LZ4_compress_HC_destSize()");
  367. { int cSize, srcSize = blockSize;
  368. int const targetSize = srcSize * (int)((FUZ_rand(&randState) & 127)+1) >> 7;
  369. char const endCheck = (char)(FUZ_rand(&randState) & 255);
  370. void* const ctx = LZ4_createHC(block);
  371. FUZ_CHECKTEST(ctx==NULL, "LZ4_createHC() allocation failed");
  372. compressedBuffer[targetSize] = endCheck;
  373. cSize = LZ4_compress_HC_destSize(ctx, block, compressedBuffer, &srcSize, targetSize, compressionLevel);
  374. DISPLAYLEVEL(5, "LZ4_compress_HC_destSize(%i): destSize : %7i/%7i; content%7i/%7i ",
  375. compressionLevel, cSize, targetSize, srcSize, blockSize);
  376. LZ4_freeHC(ctx);
  377. FUZ_CHECKTEST(cSize > targetSize, "LZ4_compress_HC_destSize() result larger than dst buffer !");
  378. FUZ_CHECKTEST(compressedBuffer[targetSize] != endCheck, "LZ4_compress_HC_destSize() overwrite dst buffer !");
  379. FUZ_CHECKTEST(srcSize > blockSize, "LZ4_compress_HC_destSize() fed more than src buffer !");
  380. if (targetSize>0) {
  381. /* check correctness */
  382. U32 const crcBase = XXH32(block, (size_t)srcSize, 0);
  383. char const canary = (char)(FUZ_rand(&randState) & 255);
  384. FUZ_CHECKTEST((cSize==0), "LZ4_compress_HC_destSize() compression failed");
  385. FUZ_DISPLAYTEST();
  386. decodedBuffer[srcSize] = canary;
  387. { int const dSize = LZ4_decompress_safe(compressedBuffer, decodedBuffer, cSize, srcSize);
  388. FUZ_CHECKTEST(dSize<0, "LZ4_decompress_safe failed (%i) on data compressed by LZ4_compressHC_destSize", dSize);
  389. FUZ_CHECKTEST(dSize!=srcSize, "LZ4_decompress_safe failed : decompressed %i bytes, was supposed to decompress %i bytes", dSize, srcSize);
  390. }
  391. FUZ_CHECKTEST(decodedBuffer[srcSize] != canary, "LZ4_decompress_safe overwrite dst buffer !");
  392. { U32 const crcDec = XXH32(decodedBuffer, (size_t)srcSize, 0);
  393. FUZ_CHECKTEST(crcDec!=crcBase, "LZ4_decompress_safe() corrupted decoded data");
  394. } }
  395. DISPLAYLEVEL(5, " OK \n");
  396. }
  397. /* Test compression HC */
  398. FUZ_DISPLAYTEST("test LZ4_compress_HC()");
  399. HCcompressedSize = LZ4_compress_HC(block, compressedBuffer, blockSize, (int)compressedBufferSize, compressionLevel);
  400. FUZ_CHECKTEST(HCcompressedSize==0, "LZ4_compress_HC() failed");
  401. /* Test compression HC using external state */
  402. FUZ_DISPLAYTEST("test LZ4_compress_HC_extStateHC()");
  403. { int const r = LZ4_compress_HC_extStateHC(stateLZ4HC, block, compressedBuffer, blockSize, (int)compressedBufferSize, compressionLevel);
  404. FUZ_CHECKTEST(r==0, "LZ4_compress_HC_extStateHC() failed")
  405. }
  406. /* Test compression HC using fast reset external state */
  407. FUZ_DISPLAYTEST("test LZ4_compress_HC_extStateHC_fastReset()");
  408. { int const r = LZ4_compress_HC_extStateHC_fastReset(stateLZ4HC, block, compressedBuffer, blockSize, (int)compressedBufferSize, compressionLevel);
  409. FUZ_CHECKTEST(r==0, "LZ4_compress_HC_extStateHC_fastReset() failed");
  410. }
  411. /* Test compression using external state */
  412. FUZ_DISPLAYTEST("test LZ4_compress_fast_extState()");
  413. { int const r = LZ4_compress_fast_extState(stateLZ4, block, compressedBuffer, blockSize, (int)compressedBufferSize, 8);
  414. FUZ_CHECKTEST(r==0, "LZ4_compress_fast_extState() failed"); }
  415. /* Test compression using fast reset external state*/
  416. FUZ_DISPLAYTEST();
  417. { int const r = LZ4_compress_fast_extState_fastReset(stateLZ4, block, compressedBuffer, blockSize, (int)compressedBufferSize, 8);
  418. FUZ_CHECKTEST(r==0, "LZ4_compress_fast_extState_fastReset() failed"); }
  419. /* Test compression */
  420. FUZ_DISPLAYTEST("test LZ4_compress_default()");
  421. compressedSize = LZ4_compress_default(block, compressedBuffer, blockSize, (int)compressedBufferSize);
  422. FUZ_CHECKTEST(compressedSize<=0, "LZ4_compress_default() failed");
  423. /* Decompression tests */
  424. /* Test decompress_fast() with input buffer size exactly correct => must not read out of bound */
  425. { char* const cBuffer_exact = (char*)malloc((size_t)compressedSize);
  426. assert(cBuffer_exact != NULL);
  427. assert(compressedSize <= (int)compressedBufferSize);
  428. #if defined(_MSC_VER) && (_MSC_VER <= 1933) /* MSVC 2022 ver 17.3 or earlier */
  429. # pragma warning(push)
  430. # pragma warning(disable : 6385) /* lz4\tests\fuzzer.c(497): warning C6385: Reading invalid data from 'compressedBuffer'. */
  431. #endif
  432. memcpy(cBuffer_exact, compressedBuffer, compressedSize);
  433. #if defined(_MSC_VER) && (_MSC_VER <= 1933) /* MSVC 2022 ver 17.3 or earlier */
  434. # pragma warning(pop)
  435. #endif
  436. /* Test decoding with output size exactly correct => must work */
  437. FUZ_DISPLAYTEST("LZ4_decompress_fast() with exact output buffer");
  438. { int const r = LZ4_decompress_fast(cBuffer_exact, decodedBuffer, blockSize);
  439. FUZ_CHECKTEST(r<0, "LZ4_decompress_fast failed despite correct space");
  440. FUZ_CHECKTEST(r!=compressedSize, "LZ4_decompress_fast failed : did not fully read compressed data");
  441. }
  442. { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0);
  443. FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_fast corrupted decoded data");
  444. }
  445. /* Test decoding with one byte missing => must fail */
  446. FUZ_DISPLAYTEST("LZ4_decompress_fast() with output buffer 1-byte too short");
  447. decodedBuffer[blockSize-1] = 0;
  448. { int const r = LZ4_decompress_fast(cBuffer_exact, decodedBuffer, blockSize-1);
  449. FUZ_CHECKTEST(r>=0, "LZ4_decompress_fast should have failed, due to Output Size being too small");
  450. }
  451. FUZ_CHECKTEST(decodedBuffer[blockSize-1]!=0, "LZ4_decompress_fast overrun specified output buffer");
  452. /* Test decoding with one byte too much => must fail */
  453. FUZ_DISPLAYTEST();
  454. { int const r = LZ4_decompress_fast(cBuffer_exact, decodedBuffer, blockSize+1);
  455. FUZ_CHECKTEST(r>=0, "LZ4_decompress_fast should have failed, due to Output Size being too large");
  456. }
  457. /* Test decoding with output size exactly what's necessary => must work */
  458. FUZ_DISPLAYTEST();
  459. decodedBuffer[blockSize] = 0;
  460. { int const r = LZ4_decompress_safe(cBuffer_exact, decodedBuffer, compressedSize, blockSize);
  461. FUZ_CHECKTEST(r<0, "LZ4_decompress_safe failed despite sufficient space");
  462. FUZ_CHECKTEST(r!=blockSize, "LZ4_decompress_safe did not regenerate original data");
  463. }
  464. FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe overrun specified output buffer size");
  465. { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0);
  466. FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe corrupted decoded data");
  467. }
  468. /* Test decoding with more than enough output size => must work */
  469. FUZ_DISPLAYTEST();
  470. decodedBuffer[blockSize] = 0;
  471. decodedBuffer[blockSize+1] = 0;
  472. { int const r = LZ4_decompress_safe(cBuffer_exact, decodedBuffer, compressedSize, blockSize+1);
  473. FUZ_CHECKTEST(r<0, "LZ4_decompress_safe failed despite amply sufficient space");
  474. FUZ_CHECKTEST(r!=blockSize, "LZ4_decompress_safe did not regenerate original data");
  475. }
  476. FUZ_CHECKTEST(decodedBuffer[blockSize+1], "LZ4_decompress_safe overrun specified output buffer size");
  477. { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0);
  478. FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe corrupted decoded data");
  479. }
  480. /* Test decoding with output size being one byte too short => must fail */
  481. FUZ_DISPLAYTEST();
  482. decodedBuffer[blockSize-1] = 0;
  483. { int const r = LZ4_decompress_safe(cBuffer_exact, decodedBuffer, compressedSize, blockSize-1);
  484. FUZ_CHECKTEST(r>=0, "LZ4_decompress_safe should have failed, due to Output Size being one byte too short");
  485. }
  486. FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_safe overrun specified output buffer size");
  487. /* Test decoding with output size being 10 bytes too short => must fail */
  488. FUZ_DISPLAYTEST();
  489. if (blockSize>10) {
  490. decodedBuffer[blockSize-10] = 0;
  491. { int const r = LZ4_decompress_safe(cBuffer_exact, decodedBuffer, compressedSize, blockSize-10);
  492. FUZ_CHECKTEST(r>=0, "LZ4_decompress_safe should have failed, due to Output Size being 10 bytes too short");
  493. }
  494. FUZ_CHECKTEST(decodedBuffer[blockSize-10], "LZ4_decompress_safe overrun specified output buffer size");
  495. }
  496. /* noisy src decompression test */
  497. /* insert noise into src */
  498. { U32 const maxNbBits = FUZ_highbit32((U32)compressedSize);
  499. size_t pos = 0;
  500. for (;;) {
  501. /* keep some original src */
  502. { U32 const nbBits = FUZ_rand(&randState) % maxNbBits;
  503. size_t const mask = (1ULL <<nbBits) - 1;
  504. size_t const skipLength = FUZ_rand(&randState) & mask;
  505. pos += skipLength;
  506. }
  507. if (pos >= (size_t)compressedSize) break;
  508. /* add noise */
  509. { U32 const nbBitsCodes = FUZ_rand(&randState) % maxNbBits;
  510. U32 const nbBits = nbBitsCodes ? nbBitsCodes-1 : 0;
  511. size_t const mask = (1ULL <<nbBits) - 1;
  512. size_t const rNoiseLength = (FUZ_rand(&randState) & mask) + 1;
  513. size_t const noiseLength = MIN(rNoiseLength, (size_t)compressedSize-pos);
  514. size_t const noiseStart = FUZ_rand(&randState) % (COMPRESSIBLE_NOISE_LENGTH - noiseLength);
  515. memcpy(cBuffer_exact + pos, (const char*)CNBuffer + noiseStart, noiseLength);
  516. pos += noiseLength;
  517. } } }
  518. /* decompress noisy source */
  519. FUZ_DISPLAYTEST("decompress noisy source ");
  520. { U32 const endMark = 0xA9B1C3D6;
  521. memcpy(decodedBuffer+blockSize, &endMark, sizeof(endMark));
  522. { int const decompressResult = LZ4_decompress_safe(cBuffer_exact, decodedBuffer, compressedSize, blockSize);
  523. /* result *may* be an unlikely success, but even then, it must strictly respect dst buffer boundaries */
  524. FUZ_CHECKTEST(decompressResult > blockSize, "LZ4_decompress_safe on noisy src : result is too large : %u > %u (dst buffer)", (unsigned)decompressResult, (unsigned)blockSize);
  525. }
  526. { U32 endCheck; memcpy(&endCheck, decodedBuffer+blockSize, sizeof(endCheck));
  527. FUZ_CHECKTEST(endMark!=endCheck, "LZ4_decompress_safe on noisy src : dst buffer overflow");
  528. } } /* noisy src decompression test */
  529. free(cBuffer_exact);
  530. }
  531. /* Test decoding with input size being one byte too short => must fail */
  532. FUZ_DISPLAYTEST();
  533. { int const r = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize-1, blockSize);
  534. FUZ_CHECKTEST(r>=0, "LZ4_decompress_safe should have failed, due to input size being one byte too short (blockSize=%i, result=%i, compressedSize=%i)", blockSize, r, compressedSize);
  535. }
  536. /* Test decoding with input size being one byte too large => must fail */
  537. FUZ_DISPLAYTEST();
  538. decodedBuffer[blockSize] = 0;
  539. { int const r = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize+1, blockSize);
  540. FUZ_CHECKTEST(r>=0, "LZ4_decompress_safe should have failed, due to input size being too large");
  541. }
  542. FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe overrun specified output buffer size");
  543. /* Test partial decoding => must work */
  544. FUZ_DISPLAYTEST("test LZ4_decompress_safe_partial");
  545. { size_t const missingOutBytes = FUZ_rand(&randState) % (unsigned)blockSize;
  546. int const targetSize = (int)((size_t)blockSize - missingOutBytes);
  547. size_t const extraneousInBytes = FUZ_rand(&randState) % 2;
  548. int const inCSize = (int)((size_t)compressedSize + extraneousInBytes);
  549. char const sentinel = decodedBuffer[targetSize] = block[targetSize] ^ 0x5A;
  550. int const decResult = LZ4_decompress_safe_partial(compressedBuffer, decodedBuffer, inCSize, targetSize, blockSize);
  551. FUZ_CHECKTEST(decResult<0, "LZ4_decompress_safe_partial failed despite valid input data (error:%i)", decResult);
  552. FUZ_CHECKTEST(decResult != targetSize, "LZ4_decompress_safe_partial did not regenerated required amount of data (%i < %i <= %i)", decResult, targetSize, blockSize);
  553. FUZ_CHECKTEST(decodedBuffer[targetSize] != sentinel, "LZ4_decompress_safe_partial overwrite beyond requested size (though %i <= %i <= %i)", decResult, targetSize, blockSize);
  554. FUZ_CHECKTEST(memcmp(block, decodedBuffer, (size_t)targetSize), "LZ4_decompress_safe_partial: corruption detected in regenerated data");
  555. }
  556. /* Partial decompression using dictionary. */
  557. FUZ_DISPLAYTEST("test LZ4_decompress_safe_partial_usingDict using no dict");
  558. { size_t const missingOutBytes = FUZ_rand(&randState) % (unsigned)blockSize;
  559. int const targetSize = (int)((size_t)blockSize - missingOutBytes);
  560. size_t const extraneousInBytes = FUZ_rand(&randState) % 2;
  561. int const inCSize = (int)((size_t)compressedSize + extraneousInBytes);
  562. char const sentinel = decodedBuffer[targetSize] = block[targetSize] ^ 0x5A;
  563. int const decResult = LZ4_decompress_safe_partial_usingDict(compressedBuffer, decodedBuffer, inCSize, targetSize, blockSize, NULL, 0);
  564. FUZ_CHECKTEST(decResult<0, "LZ4_decompress_safe_partial_usingDict failed despite valid input data (error:%i)", decResult);
  565. FUZ_CHECKTEST(decResult != targetSize, "LZ4_decompress_safe_partial_usingDict did not regenerated required amount of data (%i < %i <= %i)", decResult, targetSize, blockSize);
  566. FUZ_CHECKTEST(decodedBuffer[targetSize] != sentinel, "LZ4_decompress_safe_partial_usingDict overwrite beyond requested size (though %i <= %i <= %i)", decResult, targetSize, blockSize);
  567. FUZ_CHECKTEST(memcmp(block, decodedBuffer, (size_t)targetSize), "LZ4_decompress_safe_partial_usingDict: corruption detected in regenerated data");
  568. }
  569. FUZ_DISPLAYTEST("test LZ4_decompress_safe_partial_usingDict() using prefix as dict");
  570. { size_t const missingOutBytes = FUZ_rand(&randState) % (unsigned)blockSize;
  571. int const targetSize = (int)((size_t)blockSize - missingOutBytes);
  572. size_t const extraneousInBytes = FUZ_rand(&randState) % 2;
  573. int const inCSize = (int)((size_t)compressedSize + extraneousInBytes);
  574. char const sentinel = decodedBuffer[targetSize] = block[targetSize] ^ 0x5A;
  575. int const decResult = LZ4_decompress_safe_partial_usingDict(compressedBuffer, decodedBuffer, inCSize, targetSize, blockSize, decodedBuffer, dictSize);
  576. FUZ_CHECKTEST(decResult<0, "LZ4_decompress_safe_partial_usingDict failed despite valid input data (error:%i)", decResult);
  577. FUZ_CHECKTEST(decResult != targetSize, "LZ4_decompress_safe_partial_usingDict did not regenerated required amount of data (%i < %i <= %i)", decResult, targetSize, blockSize);
  578. FUZ_CHECKTEST(decodedBuffer[targetSize] != sentinel, "LZ4_decompress_safe_partial_usingDict overwrite beyond requested size (though %i <= %i <= %i)", decResult, targetSize, blockSize);
  579. FUZ_CHECKTEST(memcmp(block, decodedBuffer, (size_t)targetSize), "LZ4_decompress_safe_partial_usingDict: corruption detected in regenerated data");
  580. }
  581. FUZ_DISPLAYTEST("test LZ4_decompress_safe_partial_usingDict() using external dict");
  582. { size_t const missingOutBytes = FUZ_rand(&randState) % (unsigned)blockSize;
  583. int const targetSize = (int)((size_t)blockSize - missingOutBytes);
  584. size_t const extraneousInBytes = FUZ_rand(&randState) % 2;
  585. int const inCSize = (int)((size_t)compressedSize + extraneousInBytes);
  586. char const sentinel = decodedBuffer[targetSize] = block[targetSize] ^ 0x5A;
  587. int const decResult = LZ4_decompress_safe_partial_usingDict(compressedBuffer, decodedBuffer, inCSize, targetSize, blockSize, dict, dictSize);
  588. FUZ_CHECKTEST(decResult<0, "LZ4_decompress_safe_partial_usingDict failed despite valid input data (error:%i)", decResult);
  589. FUZ_CHECKTEST(decResult != targetSize, "LZ4_decompress_safe_partial_usingDict did not regenerated required amount of data (%i < %i <= %i)", decResult, targetSize, blockSize);
  590. FUZ_CHECKTEST(decodedBuffer[targetSize] != sentinel, "LZ4_decompress_safe_partial_usingDict overwrite beyond requested size (though %i <= %i <= %i)", decResult, targetSize, blockSize);
  591. FUZ_CHECKTEST(memcmp(block, decodedBuffer, (size_t)targetSize), "LZ4_decompress_safe_partial_usingDict: corruption detected in regenerated data");
  592. }
  593. /* Test Compression with limited output size */
  594. /* Test compression with output size being exactly what's necessary (should work) */
  595. FUZ_DISPLAYTEST("test LZ4_compress_default() with output buffer just the right size");
  596. ret = LZ4_compress_default(block, compressedBuffer, blockSize, compressedSize);
  597. FUZ_CHECKTEST(ret==0, "LZ4_compress_default() failed despite sufficient space");
  598. /* Test compression with output size being exactly what's necessary and external state (should work) */
  599. FUZ_DISPLAYTEST("test LZ4_compress_fast_extState() with output buffer just the right size");
  600. ret = LZ4_compress_fast_extState(stateLZ4, block, compressedBuffer, blockSize, compressedSize, 1);
  601. FUZ_CHECKTEST(ret==0, "LZ4_compress_fast_extState() failed despite sufficient space");
  602. /* Test HC compression with output size being exactly what's necessary (should work) */
  603. FUZ_DISPLAYTEST("test LZ4_compress_HC() with output buffer just the right size");
  604. ret = LZ4_compress_HC(block, compressedBuffer, blockSize, HCcompressedSize, compressionLevel);
  605. FUZ_CHECKTEST(ret==0, "LZ4_compress_HC() failed despite sufficient space");
  606. /* Test HC compression with output size being exactly what's necessary (should work) */
  607. FUZ_DISPLAYTEST("test LZ4_compress_HC_extStateHC() with output buffer just the right size");
  608. ret = LZ4_compress_HC_extStateHC(stateLZ4HC, block, compressedBuffer, blockSize, HCcompressedSize, compressionLevel);
  609. FUZ_CHECKTEST(ret==0, "LZ4_compress_HC_extStateHC() failed despite sufficient space");
  610. /* Test compression with missing bytes into output buffer => must fail */
  611. FUZ_DISPLAYTEST("test LZ4_compress_default() with output buffer a bit too short");
  612. { int missingBytes = (FUZ_rand(&randState) % 0x3F) + 1;
  613. if (missingBytes >= compressedSize) missingBytes = compressedSize-1;
  614. missingBytes += !missingBytes; /* avoid special case missingBytes==0 */
  615. compressedBuffer[compressedSize-missingBytes] = 0;
  616. { int const cSize = LZ4_compress_default(block, compressedBuffer, blockSize, compressedSize-missingBytes);
  617. FUZ_CHECKTEST(cSize, "LZ4_compress_default should have failed (output buffer too small by %i byte)", missingBytes);
  618. }
  619. FUZ_CHECKTEST(compressedBuffer[compressedSize-missingBytes], "LZ4_compress_default overran output buffer ! (%i missingBytes)", missingBytes)
  620. }
  621. /* Test HC compression with missing bytes into output buffer => must fail */
  622. FUZ_DISPLAYTEST("test LZ4_compress_HC() with output buffer a bit too short");
  623. { int missingBytes = (FUZ_rand(&randState) % 0x3F) + 1;
  624. if (missingBytes >= HCcompressedSize) missingBytes = HCcompressedSize-1;
  625. missingBytes += !missingBytes; /* avoid special case missingBytes==0 */
  626. compressedBuffer[HCcompressedSize-missingBytes] = 0;
  627. { int const hcSize = LZ4_compress_HC(block, compressedBuffer, blockSize, HCcompressedSize-missingBytes, compressionLevel);
  628. FUZ_CHECKTEST(hcSize, "LZ4_compress_HC should have failed (output buffer too small by %i byte)", missingBytes);
  629. }
  630. FUZ_CHECKTEST(compressedBuffer[HCcompressedSize-missingBytes], "LZ4_compress_HC overran output buffer ! (%i missingBytes)", missingBytes)
  631. }
  632. /*-******************/
  633. /* Dictionary tests */
  634. /*-******************/
  635. /* Compress using dictionary */
  636. FUZ_DISPLAYTEST("test LZ4_compress_fast_continue() with dictionary of size %i", dictSize);
  637. { LZ4_stream_t LZ4_stream;
  638. LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream));
  639. LZ4_compress_fast_continue (&LZ4_stream, dict, compressedBuffer, dictSize, (int)compressedBufferSize, 1); /* Just to fill hash tables */
  640. blockContinueCompressedSize = LZ4_compress_fast_continue (&LZ4_stream, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1);
  641. FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_fast_continue failed");
  642. }
  643. /* Decompress with dictionary as prefix */
  644. FUZ_DISPLAYTEST("test LZ4_decompress_fast_usingDict() with dictionary as prefix");
  645. memcpy(decodedBuffer, dict, dictSize);
  646. ret = LZ4_decompress_fast_usingDict(compressedBuffer, decodedBuffer+dictSize, blockSize, decodedBuffer, dictSize);
  647. FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_decompress_fast_usingDict did not read all compressed block input");
  648. { U32 const crcCheck = XXH32(decodedBuffer+dictSize, (size_t)blockSize, 0);
  649. if (crcCheck!=crcOrig) {
  650. FUZ_findDiff(block, decodedBuffer);
  651. EXIT_MSG("LZ4_decompress_fast_usingDict corrupted decoded data (dict %i)", dictSize);
  652. } }
  653. FUZ_DISPLAYTEST("test LZ4_decompress_safe_usingDict()");
  654. ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer+dictSize, blockContinueCompressedSize, blockSize, decodedBuffer, dictSize);
  655. FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data");
  656. { U32 const crcCheck = XXH32(decodedBuffer+dictSize, (size_t)blockSize, 0);
  657. FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_usingDict corrupted decoded data");
  658. }
  659. /* Compress using External dictionary */
  660. FUZ_DISPLAYTEST("test LZ4_compress_fast_continue(), with non-contiguous dictionary");
  661. dict -= (size_t)(FUZ_rand(&randState) & 0xF) + 1; /* create space, so now dictionary is an ExtDict */
  662. if (dict < (char*)CNBuffer) dict = (char*)CNBuffer;
  663. LZ4_loadDict(&LZ4dictBody, dict, dictSize);
  664. blockContinueCompressedSize = LZ4_compress_fast_continue(&LZ4dictBody, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1);
  665. FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_fast_continue failed");
  666. FUZ_DISPLAYTEST("LZ4_compress_fast_continue() with dictionary and output buffer too short by one byte");
  667. LZ4_loadDict(&LZ4dictBody, dict, dictSize);
  668. ret = LZ4_compress_fast_continue(&LZ4dictBody, block, compressedBuffer, blockSize, blockContinueCompressedSize-1, 1);
  669. FUZ_CHECKTEST(ret>0, "LZ4_compress_fast_continue using ExtDict should fail : one missing byte for output buffer : %i written, %i buffer", ret, blockContinueCompressedSize);
  670. FUZ_DISPLAYTEST("test LZ4_compress_fast_continue() with dictionary loaded with LZ4_loadDict()");
  671. DISPLAYLEVEL(5, " compress %i bytes from buffer(%p) into dst(%p) using dict(%p) of size %i \n",
  672. blockSize, (const void *)block, (void *)decodedBuffer, (const void *)dict, dictSize);
  673. LZ4_loadDict(&LZ4dictBody, dict, dictSize);
  674. ret = LZ4_compress_fast_continue(&LZ4dictBody, block, compressedBuffer, blockSize, blockContinueCompressedSize, 1);
  675. FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_limitedOutput_compressed size is different (%i != %i)", ret, blockContinueCompressedSize);
  676. FUZ_CHECKTEST(ret<=0, "LZ4_compress_fast_continue should work : enough size available within output buffer");
  677. /* Decompress with dictionary as external */
  678. FUZ_DISPLAYTEST("test LZ4_decompress_fast_usingDict() with dictionary as extDict");
  679. DISPLAYLEVEL(5, " decoding %i bytes from buffer(%p) using dict(%p) of size %i \n",
  680. blockSize, (void *)decodedBuffer, (const void *)dict, dictSize);
  681. decodedBuffer[blockSize] = 0;
  682. ret = LZ4_decompress_fast_usingDict(compressedBuffer, decodedBuffer, blockSize, dict, dictSize);
  683. FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_decompress_fast_usingDict did not read all compressed block input");
  684. FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_fast_usingDict overrun specified output buffer size");
  685. { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0);
  686. if (crcCheck!=crcOrig) {
  687. FUZ_findDiff(block, decodedBuffer);
  688. EXIT_MSG("LZ4_decompress_fast_usingDict corrupted decoded data (dict %i)", dictSize);
  689. } }
  690. FUZ_DISPLAYTEST();
  691. decodedBuffer[blockSize] = 0;
  692. ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize, dict, dictSize);
  693. FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data");
  694. FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size");
  695. { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0);
  696. FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_usingDict corrupted decoded data");
  697. }
  698. FUZ_DISPLAYTEST();
  699. decodedBuffer[blockSize-1] = 0;
  700. ret = LZ4_decompress_fast_usingDict(compressedBuffer, decodedBuffer, blockSize-1, dict, dictSize);
  701. FUZ_CHECKTEST(ret>=0, "LZ4_decompress_fast_usingDict should have failed : wrong original size (-1 byte)");
  702. FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_fast_usingDict overrun specified output buffer size");
  703. FUZ_DISPLAYTEST();
  704. decodedBuffer[blockSize-1] = 0;
  705. ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize-1, dict, dictSize);
  706. FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe_usingDict should have failed : not enough output size (-1 byte)");
  707. FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_safe_usingDict overrun specified output buffer size");
  708. FUZ_DISPLAYTEST();
  709. { int const missingBytes = (FUZ_rand(&randState) & 0xF) + 2;
  710. if (blockSize > missingBytes) {
  711. decodedBuffer[blockSize-missingBytes] = 0;
  712. ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize-missingBytes, dict, dictSize);
  713. FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe_usingDict should have failed : output buffer too small (-%i byte)", missingBytes);
  714. FUZ_CHECKTEST(decodedBuffer[blockSize-missingBytes], "LZ4_decompress_safe_usingDict overrun specified output buffer size (-%i byte) (blockSize=%i)", missingBytes, blockSize);
  715. } }
  716. /* Compress using external dictionary stream */
  717. { LZ4_stream_t LZ4_stream;
  718. int expectedSize;
  719. U32 expectedCrc;
  720. FUZ_DISPLAYTEST("LZ4_compress_fast_continue() after LZ4_loadDict()");
  721. LZ4_loadDict(&LZ4dictBody, dict, dictSize);
  722. expectedSize = LZ4_compress_fast_continue(&LZ4dictBody, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1);
  723. FUZ_CHECKTEST(expectedSize<=0, "LZ4_compress_fast_continue reference compression for extDictCtx should have succeeded");
  724. expectedCrc = XXH32(compressedBuffer, (size_t)expectedSize, 0);
  725. FUZ_DISPLAYTEST("LZ4_compress_fast_continue() after LZ4_attach_dictionary()");
  726. LZ4_loadDict(&LZ4dictBody, dict, dictSize);
  727. LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream));
  728. LZ4_attach_dictionary(&LZ4_stream, &LZ4dictBody);
  729. blockContinueCompressedSize = LZ4_compress_fast_continue(&LZ4_stream, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1);
  730. FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_fast_continue using extDictCtx failed");
  731. /* In the future, it might be desirable to let extDictCtx mode's
  732. * output diverge from the output generated by regular extDict mode.
  733. * Until that time, this comparison serves as a good regression
  734. * test.
  735. */
  736. FUZ_CHECKTEST(blockContinueCompressedSize != expectedSize, "LZ4_compress_fast_continue using extDictCtx produced different-sized output (%d expected vs %d actual)", expectedSize, blockContinueCompressedSize);
  737. FUZ_CHECKTEST(XXH32(compressedBuffer, (size_t)blockContinueCompressedSize, 0) != expectedCrc, "LZ4_compress_fast_continue using extDictCtx produced different output");
  738. FUZ_DISPLAYTEST("LZ4_compress_fast_continue() after LZ4_attach_dictionary(), but output buffer is 1 byte too short");
  739. LZ4_resetStream_fast(&LZ4_stream);
  740. LZ4_attach_dictionary(&LZ4_stream, &LZ4dictBody);
  741. ret = LZ4_compress_fast_continue(&LZ4_stream, block, compressedBuffer, blockSize, blockContinueCompressedSize-1, 1);
  742. FUZ_CHECKTEST(ret>0, "LZ4_compress_fast_continue using extDictCtx should fail : one missing byte for output buffer : %i written, %i buffer", ret, blockContinueCompressedSize);
  743. /* note : context is no longer dirty after a failed compressed block */
  744. FUZ_DISPLAYTEST();
  745. LZ4_resetStream_fast(&LZ4_stream);
  746. LZ4_attach_dictionary(&LZ4_stream, &LZ4dictBody);
  747. ret = LZ4_compress_fast_continue(&LZ4_stream, block, compressedBuffer, blockSize, blockContinueCompressedSize, 1);
  748. FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_limitedOutput_compressed size is different (%i != %i)", ret, blockContinueCompressedSize);
  749. FUZ_CHECKTEST(ret<=0, "LZ4_compress_fast_continue using extDictCtx should work : enough size available within output buffer");
  750. FUZ_CHECKTEST(ret != expectedSize, "LZ4_compress_fast_continue using extDictCtx produced different-sized output");
  751. FUZ_CHECKTEST(XXH32(compressedBuffer, (size_t)ret, 0) != expectedCrc, "LZ4_compress_fast_continue using extDictCtx produced different output");
  752. FUZ_DISPLAYTEST();
  753. LZ4_resetStream_fast(&LZ4_stream);
  754. LZ4_attach_dictionary(&LZ4_stream, &LZ4dictBody);
  755. ret = LZ4_compress_fast_continue(&LZ4_stream, block, compressedBuffer, blockSize, blockContinueCompressedSize, 1);
  756. FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_limitedOutput_compressed size is different (%i != %i)", ret, blockContinueCompressedSize);
  757. FUZ_CHECKTEST(ret<=0, "LZ4_compress_fast_continue using extDictCtx with re-used context should work : enough size available within output buffer");
  758. FUZ_CHECKTEST(ret != expectedSize, "LZ4_compress_fast_continue using extDictCtx produced different-sized output");
  759. FUZ_CHECKTEST(XXH32(compressedBuffer, (size_t)ret, 0) != expectedCrc, "LZ4_compress_fast_continue using extDictCtx produced different output");
  760. }
  761. /* Decompress with dictionary as external */
  762. FUZ_DISPLAYTEST();
  763. decodedBuffer[blockSize] = 0;
  764. ret = LZ4_decompress_fast_usingDict(compressedBuffer, decodedBuffer, blockSize, dict, dictSize);
  765. FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_decompress_fast_usingDict did not read all compressed block input");
  766. FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_fast_usingDict overrun specified output buffer size");
  767. { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0);
  768. if (crcCheck!=crcOrig) {
  769. FUZ_findDiff(block, decodedBuffer);
  770. EXIT_MSG("LZ4_decompress_fast_usingDict corrupted decoded data (dict %i)", dictSize);
  771. } }
  772. FUZ_DISPLAYTEST();
  773. decodedBuffer[blockSize] = 0;
  774. ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize, dict, dictSize);
  775. FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data");
  776. FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size");
  777. { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0);
  778. FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_usingDict corrupted decoded data");
  779. }
  780. FUZ_DISPLAYTEST();
  781. decodedBuffer[blockSize-1] = 0;
  782. ret = LZ4_decompress_fast_usingDict(compressedBuffer, decodedBuffer, blockSize-1, dict, dictSize);
  783. FUZ_CHECKTEST(ret>=0, "LZ4_decompress_fast_usingDict should have failed : wrong original size (-1 byte)");
  784. FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_fast_usingDict overrun specified output buffer size");
  785. FUZ_DISPLAYTEST();
  786. decodedBuffer[blockSize-1] = 0;
  787. ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize-1, dict, dictSize);
  788. FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe_usingDict should have failed : not enough output size (-1 byte)");
  789. FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_safe_usingDict overrun specified output buffer size");
  790. FUZ_DISPLAYTEST("LZ4_decompress_safe_usingDict with a too small output buffer");
  791. { int const missingBytes = (FUZ_rand(&randState) & 0xF) + 2;
  792. if (blockSize > missingBytes) {
  793. decodedBuffer[blockSize-missingBytes] = 0;
  794. ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize-missingBytes, dict, dictSize);
  795. FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe_usingDict should have failed : output buffer too small (-%i byte)", missingBytes);
  796. FUZ_CHECKTEST(decodedBuffer[blockSize-missingBytes], "LZ4_decompress_safe_usingDict overrun specified output buffer size (-%i byte) (blockSize=%i)", missingBytes, blockSize);
  797. } }
  798. /* Compress HC using External dictionary */
  799. FUZ_DISPLAYTEST("LZ4_compress_HC_continue with an external dictionary");
  800. dict -= (FUZ_rand(&randState) & 7); /* even bigger separation */
  801. if (dict < (char*)CNBuffer) dict = (char*)CNBuffer;
  802. LZ4_loadDictHC(LZ4dictHC, dict, dictSize);
  803. LZ4_setCompressionLevel (LZ4dictHC, compressionLevel);
  804. blockContinueCompressedSize = LZ4_compress_HC_continue(LZ4dictHC, block, compressedBuffer, blockSize, (int)compressedBufferSize);
  805. FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_HC_continue failed");
  806. FUZ_CHECKTEST(LZ4dictHC->internal_donotuse.dirty, "Context should be clean");
  807. FUZ_DISPLAYTEST("LZ4_compress_HC_continue with same external dictionary, but output buffer 1 byte too short");
  808. LZ4_loadDictHC(LZ4dictHC, dict, dictSize);
  809. ret = LZ4_compress_HC_continue(LZ4dictHC, block, compressedBuffer, blockSize, blockContinueCompressedSize-1);
  810. FUZ_CHECKTEST(ret>0, "LZ4_compress_HC_continue using ExtDict should fail : one missing byte for output buffer (expected %i, but result=%i)", blockContinueCompressedSize, ret);
  811. /* note : context is no longer dirty after a failed compressed block */
  812. FUZ_DISPLAYTEST("LZ4_compress_HC_continue with same external dictionary, and output buffer exactly the right size");
  813. LZ4_loadDictHC(LZ4dictHC, dict, dictSize);
  814. ret = LZ4_compress_HC_continue(LZ4dictHC, block, compressedBuffer, blockSize, blockContinueCompressedSize);
  815. FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_HC_continue size is different : ret(%i) != expected(%i)", ret, blockContinueCompressedSize);
  816. FUZ_CHECKTEST(ret<=0, "LZ4_compress_HC_continue should work : enough size available within output buffer");
  817. FUZ_CHECKTEST(LZ4dictHC->internal_donotuse.dirty, "Context should be clean");
  818. FUZ_DISPLAYTEST();
  819. decodedBuffer[blockSize] = 0;
  820. ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize, dict, dictSize);
  821. FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data");
  822. FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size");
  823. { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0);
  824. if (crcCheck!=crcOrig) {
  825. FUZ_findDiff(block, decodedBuffer);
  826. EXIT_MSG("LZ4_decompress_safe_usingDict corrupted decoded data");
  827. } }
  828. /* Compress HC using external dictionary stream */
  829. FUZ_DISPLAYTEST();
  830. { LZ4_streamHC_t* const LZ4_streamHC = LZ4_createStreamHC();
  831. LZ4_loadDictHC(LZ4dictHC, dict, dictSize);
  832. LZ4_attach_HC_dictionary(LZ4_streamHC, LZ4dictHC);
  833. LZ4_setCompressionLevel (LZ4_streamHC, compressionLevel);
  834. blockContinueCompressedSize = LZ4_compress_HC_continue(LZ4_streamHC, block, compressedBuffer, blockSize, (int)compressedBufferSize);
  835. FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_HC_continue with ExtDictCtx failed");
  836. FUZ_CHECKTEST(LZ4_streamHC->internal_donotuse.dirty, "Context should be clean");
  837. FUZ_DISPLAYTEST();
  838. LZ4_resetStreamHC_fast (LZ4_streamHC, compressionLevel);
  839. LZ4_attach_HC_dictionary(LZ4_streamHC, LZ4dictHC);
  840. ret = LZ4_compress_HC_continue(LZ4_streamHC, block, compressedBuffer, blockSize, blockContinueCompressedSize-1);
  841. FUZ_CHECKTEST(ret>0, "LZ4_compress_HC_continue using ExtDictCtx should fail : one missing byte for output buffer (%i != %i)", ret, blockContinueCompressedSize);
  842. /* note : context is no longer dirty after a failed compressed block */
  843. FUZ_DISPLAYTEST();
  844. LZ4_resetStreamHC_fast (LZ4_streamHC, compressionLevel);
  845. LZ4_attach_HC_dictionary(LZ4_streamHC, LZ4dictHC);
  846. ret = LZ4_compress_HC_continue(LZ4_streamHC, block, compressedBuffer, blockSize, blockContinueCompressedSize);
  847. FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_HC_continue using ExtDictCtx size is different (%i != %i)", ret, blockContinueCompressedSize);
  848. FUZ_CHECKTEST(ret<=0, "LZ4_compress_HC_continue using ExtDictCtx should work : enough size available within output buffer");
  849. FUZ_CHECKTEST(LZ4_streamHC->internal_donotuse.dirty, "Context should be clean");
  850. FUZ_DISPLAYTEST();
  851. LZ4_resetStreamHC_fast (LZ4_streamHC, compressionLevel);
  852. LZ4_attach_HC_dictionary(LZ4_streamHC, LZ4dictHC);
  853. ret = LZ4_compress_HC_continue(LZ4_streamHC, block, compressedBuffer, blockSize, blockContinueCompressedSize);
  854. FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_HC_continue using ExtDictCtx and fast reset size is different (%i != %i)",
  855. ret, blockContinueCompressedSize);
  856. FUZ_CHECKTEST(ret<=0, "LZ4_compress_HC_continue using ExtDictCtx and fast reset should work : enough size available within output buffer");
  857. FUZ_CHECKTEST(LZ4_streamHC->internal_donotuse.dirty, "Context should be clean");
  858. LZ4_freeStreamHC(LZ4_streamHC);
  859. }
  860. FUZ_DISPLAYTEST();
  861. decodedBuffer[blockSize] = 0;
  862. ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize, dict, dictSize);
  863. FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data");
  864. FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size");
  865. { U32 const crcCheck = XXH32(decodedBuffer, (size_t)blockSize, 0);
  866. if (crcCheck!=crcOrig) {
  867. FUZ_findDiff(block, decodedBuffer);
  868. EXIT_MSG("LZ4_decompress_safe_usingDict corrupted decoded data");
  869. } }
  870. /* Compress HC continue destSize */
  871. FUZ_DISPLAYTEST();
  872. { int const availableSpace = (int)(FUZ_rand(&randState) % (U32)blockSize) + 5;
  873. int consumedSize = blockSize;
  874. FUZ_DISPLAYTEST();
  875. LZ4_loadDictHC(LZ4dictHC, dict, dictSize);
  876. LZ4_setCompressionLevel(LZ4dictHC, compressionLevel);
  877. blockContinueCompressedSize = LZ4_compress_HC_continue_destSize(LZ4dictHC, block, compressedBuffer, &consumedSize, availableSpace);
  878. DISPLAYLEVEL(5, " LZ4_compress_HC_continue_destSize : compressed %6i/%6i into %6i/%6i at cLevel=%i \n",
  879. consumedSize, blockSize, blockContinueCompressedSize, availableSpace, compressionLevel);
  880. FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_HC_continue_destSize failed");
  881. FUZ_CHECKTEST(blockContinueCompressedSize > availableSpace, "LZ4_compress_HC_continue_destSize write overflow");
  882. FUZ_CHECKTEST(consumedSize > blockSize, "LZ4_compress_HC_continue_destSize read overflow");
  883. FUZ_DISPLAYTEST();
  884. decodedBuffer[consumedSize] = 0;
  885. ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, consumedSize, dict, dictSize);
  886. FUZ_CHECKTEST(ret != consumedSize, "LZ4_decompress_safe_usingDict regenerated %i bytes (%i expected)", ret, consumedSize);
  887. FUZ_CHECKTEST(decodedBuffer[consumedSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size")
  888. { U32 const crcSrc = XXH32(block, (size_t)consumedSize, 0);
  889. U32 const crcDst = XXH32(decodedBuffer, (size_t)consumedSize, 0);
  890. if (crcSrc!=crcDst) {
  891. FUZ_findDiff(block, decodedBuffer);
  892. EXIT_MSG("LZ4_decompress_safe_usingDict corrupted decoded data");
  893. } }
  894. }
  895. /* ***** End of tests *** */
  896. /* Fill stats */
  897. assert(blockSize >= 0);
  898. bytes += (unsigned)blockSize;
  899. assert(compressedSize >= 0);
  900. cbytes += (unsigned)compressedSize;
  901. assert(HCcompressedSize >= 0);
  902. hcbytes += (unsigned)HCcompressedSize;
  903. assert(blockContinueCompressedSize >= 0);
  904. ccbytes += (unsigned)blockContinueCompressedSize;
  905. }
  906. if (nbCycles<=1) nbCycles = cycleNb; /* end by time */
  907. bytes += !bytes; /* avoid division by 0 */
  908. printf("\r%7u /%7u - ", cycleNb, nbCycles);
  909. printf("all tests completed successfully \n");
  910. printf("compression ratio: %0.3f%%\n", (double)cbytes/bytes*100);
  911. printf("HC compression ratio: %0.3f%%\n", (double)hcbytes/bytes*100);
  912. printf("ratio with dict: %0.3f%%\n", (double)ccbytes/bytes*100);
  913. /* release memory */
  914. free(CNBuffer);
  915. free(compressedBuffer);
  916. free(decodedBuffer);
  917. FUZ_freeLowAddr(lowAddrBuffer, labSize);
  918. LZ4_freeStreamHC(LZ4dictHC);
  919. free(stateLZ4);
  920. free(stateLZ4HC);
  921. return result;
  922. }
  923. #define testInputSize (196 KB)
  924. #define testCompressedSize (130 KB)
  925. #define ringBufferSize (8 KB)
  926. static void FUZ_unitTests(int compressionLevel)
  927. {
  928. const unsigned testNb = 0;
  929. const unsigned seed = 0;
  930. const unsigned cycleNb= 0;
  931. char* testInput = (char*)malloc(testInputSize);
  932. char* testCompressed = (char*)malloc(testCompressedSize);
  933. char* testVerify = (char*)malloc(testInputSize);
  934. char ringBuffer[ringBufferSize] = {0};
  935. U32 randState = 1;
  936. /* Init */
  937. if (!testInput || !testCompressed || !testVerify) {
  938. EXIT_MSG("not enough memory for FUZ_unitTests");
  939. }
  940. FUZ_fillCompressibleNoiseBuffer(testInput, testInputSize, 0.50, &randState);
  941. /* 32-bits address space overflow test */
  942. FUZ_AddressOverflow();
  943. /* Test decoding with empty input */
  944. DISPLAYLEVEL(3, "LZ4_decompress_safe() with empty input \n");
  945. LZ4_decompress_safe(testCompressed, testVerify, 0, testInputSize);
  946. /* Test decoding with a one byte input */
  947. DISPLAYLEVEL(3, "LZ4_decompress_safe() with one byte input \n");
  948. { char const tmp = (char)0xFF;
  949. LZ4_decompress_safe(&tmp, testVerify, 1, testInputSize);
  950. }
  951. /* Test decoding shortcut edge case */
  952. DISPLAYLEVEL(3, "LZ4_decompress_safe() with shortcut edge case \n");
  953. { char tmp[17];
  954. /* 14 bytes of literals, followed by a 14 byte match.
  955. * Should not read beyond the end of the buffer.
  956. * See https://github.com/lz4/lz4/issues/508. */
  957. *tmp = (char)0xEE;
  958. memset(tmp + 1, 0, 14);
  959. tmp[15] = 14;
  960. tmp[16] = 0;
  961. { int const r = LZ4_decompress_safe(tmp, testVerify, sizeof(tmp), testInputSize);
  962. FUZ_CHECKTEST(r >= 0, "LZ4_decompress_safe() should fail");
  963. } }
  964. /* to be tested with undefined sanitizer */
  965. DISPLAYLEVEL(3, "LZ4_compress_default() with NULL input:");
  966. { int const maxCSize = LZ4_compressBound(0);
  967. int const cSize = LZ4_compress_default(NULL, testCompressed, 0, maxCSize);
  968. FUZ_CHECKTEST(!(cSize==1 && testCompressed[0]==0),
  969. "compressing empty should give byte 0"
  970. " (maxCSize == %i) (cSize == %i) (byte == 0x%02X)",
  971. maxCSize, cSize, testCompressed[0]);
  972. }
  973. DISPLAYLEVEL(3, " OK \n");
  974. DISPLAYLEVEL(3, "LZ4_compress_default() with both NULL input and output:");
  975. { int const cSize = LZ4_compress_default(NULL, NULL, 0, 0);
  976. FUZ_CHECKTEST(cSize != 0,
  977. "compressing into NULL must fail"
  978. " (cSize == %i != 0)", cSize);
  979. }
  980. DISPLAYLEVEL(3, " OK \n");
  981. /* in-place compression test */
  982. DISPLAYLEVEL(3, "in-place compression using LZ4_compress_default() :");
  983. { int const sampleSize = 65 KB;
  984. int const maxCSize = LZ4_COMPRESSBOUND(sampleSize);
  985. int const outSize = LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCSize);
  986. int const startInputIndex = outSize - sampleSize;
  987. char* const startInput = testCompressed + startInputIndex;
  988. XXH32_hash_t const crcOrig = XXH32(testInput, sampleSize, 0);
  989. int cSize;
  990. assert(outSize < (int)testCompressedSize);
  991. memcpy(startInput, testInput, sampleSize); /* copy at end of buffer */
  992. /* compress in-place */
  993. cSize = LZ4_compress_default(startInput, testCompressed, sampleSize, maxCSize);
  994. assert(cSize != 0); /* ensure compression is successful */
  995. assert(maxCSize < INT_MAX);
  996. assert(cSize <= maxCSize);
  997. /* decompress and verify */
  998. { int const dSize = LZ4_decompress_safe(testCompressed, testVerify, cSize, testInputSize);
  999. assert(dSize == sampleSize); /* correct size */
  1000. { XXH32_hash_t const crcCheck = XXH32(testVerify, (size_t)dSize, 0);
  1001. FUZ_CHECKTEST(crcCheck != crcOrig, "LZ4_decompress_safe decompression corruption");
  1002. } } }
  1003. DISPLAYLEVEL(3, " OK \n");
  1004. /* in-place decompression test */
  1005. DISPLAYLEVEL(3, "in-place decompression, limit case:");
  1006. { int const sampleSize = 65 KB;
  1007. FUZ_fillCompressibleNoiseBuffer(testInput, sampleSize, 0.0, &randState);
  1008. memset(testInput, 0, 267); /* calculated exactly so that compressedSize == originalSize-1 */
  1009. { XXH64_hash_t const crcOrig = XXH64(testInput, sampleSize, 0);
  1010. int const cSize = LZ4_compress_default(testInput, testCompressed, sampleSize, testCompressedSize);
  1011. assert(cSize == sampleSize - 1); /* worst case for in-place decompression */
  1012. { int const bufferSize = LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(sampleSize);
  1013. int const startInputIndex = bufferSize - cSize;
  1014. char* const startInput = testVerify + startInputIndex;
  1015. memcpy(startInput, testCompressed, cSize);
  1016. /* decompress and verify */
  1017. { int const dSize = LZ4_decompress_safe(startInput, testVerify, cSize, sampleSize);
  1018. assert(dSize == sampleSize); /* correct size */
  1019. { XXH64_hash_t const crcCheck = XXH64(testVerify, (size_t)dSize, 0);
  1020. FUZ_CHECKTEST(crcCheck != crcOrig, "LZ4_decompress_safe decompression corruption");
  1021. } } } } }
  1022. DISPLAYLEVEL(3, " OK \n");
  1023. DISPLAYLEVEL(3, "LZ4_initStream with multiple valid alignments : ");
  1024. { typedef struct {
  1025. LZ4_stream_t state1;
  1026. LZ4_stream_t state2;
  1027. char c;
  1028. LZ4_stream_t state3;
  1029. } shct;
  1030. shct* const shc = (shct*)malloc(sizeof(*shc));
  1031. assert(shc != NULL);
  1032. memset(shc, 0, sizeof(*shc));
  1033. DISPLAYLEVEL(4, "state1(%p) state2(%p) state3(%p) LZ4_stream_t size(0x%x): ",
  1034. (void*)&(shc->state1), (void*)&(shc->state2), (void*)&(shc->state3), (unsigned)sizeof(LZ4_stream_t));
  1035. FUZ_CHECKTEST( LZ4_initStream(&(shc->state1), sizeof(shc->state1)) == NULL, "state1 (%p) failed init", (void*)&(shc->state1) );
  1036. FUZ_CHECKTEST( LZ4_initStream(&(shc->state2), sizeof(shc->state2)) == NULL, "state2 (%p) failed init", (void*)&(shc->state2) );
  1037. FUZ_CHECKTEST( LZ4_initStream(&(shc->state3), sizeof(shc->state3)) == NULL, "state3 (%p) failed init", (void*)&(shc->state3) );
  1038. FUZ_CHECKTEST( LZ4_initStream((char*)&(shc->state1) + 1, sizeof(shc->state1)) != NULL,
  1039. "hc1+1 (%p) init must fail, due to bad alignment", (void*)((char*)&(shc->state1) + 1) );
  1040. free(shc);
  1041. }
  1042. DISPLAYLEVEL(3, "all inits OK \n");
  1043. /* Allocation test */
  1044. { LZ4_stream_t* const statePtr = LZ4_createStream();
  1045. FUZ_CHECKTEST(statePtr==NULL, "LZ4_createStream() allocation failed");
  1046. LZ4_freeStream(statePtr);
  1047. }
  1048. /* LZ4 streaming tests */
  1049. { LZ4_stream_t streamingState;
  1050. /* simple compression test */
  1051. LZ4_initStream(&streamingState, sizeof(streamingState));
  1052. { int const cs = LZ4_compress_fast_continue(&streamingState, testInput, testCompressed, testCompressedSize, testCompressedSize-1, 1);
  1053. FUZ_CHECKTEST(cs==0, "LZ4_compress_fast_continue() compression failed!");
  1054. { int const r = LZ4_decompress_safe(testCompressed, testVerify, cs, testCompressedSize);
  1055. FUZ_CHECKTEST(r!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed");
  1056. } }
  1057. { U64 const crcOrig = XXH64(testInput, testCompressedSize, 0);
  1058. U64 const crcNew = XXH64(testVerify, testCompressedSize, 0);
  1059. FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption");
  1060. }
  1061. /* early saveDict */
  1062. DISPLAYLEVEL(3, "saveDict (right after init) : ");
  1063. { LZ4_stream_t* const ctx = LZ4_initStream(&streamingState, sizeof(streamingState));
  1064. assert(ctx != NULL); /* ensure init is successful */
  1065. /* Check access violation with asan */
  1066. FUZ_CHECKTEST( LZ4_saveDict(ctx, NULL, 0) != 0,
  1067. "LZ4_saveDict() can't save anything into (NULL,0)");
  1068. /* Check access violation with asan */
  1069. { char tmp_buffer[240] = { 0 };
  1070. FUZ_CHECKTEST( LZ4_saveDict(ctx, tmp_buffer, sizeof(tmp_buffer)) != 0,
  1071. "LZ4_saveDict() can't save anything since compression hasn't started");
  1072. } }
  1073. DISPLAYLEVEL(3, "OK \n");
  1074. /* ring buffer test */
  1075. { XXH64_state_t xxhOrig;
  1076. XXH64_state_t xxhNewSafe, xxhNewFast;
  1077. LZ4_streamDecode_t decodeStateSafe, decodeStateFast;
  1078. const U32 maxMessageSizeLog = 10;
  1079. const U32 maxMessageSizeMask = (1<<maxMessageSizeLog) - 1;
  1080. U32 messageSize = (FUZ_rand(&randState) & maxMessageSizeMask) + 1;
  1081. U32 iNext = 0;
  1082. U32 rNext = 0;
  1083. U32 dNext = 0;
  1084. const U32 dBufferSize = ringBufferSize + maxMessageSizeMask;
  1085. XXH64_reset(&xxhOrig, 0);
  1086. XXH64_reset(&xxhNewSafe, 0);
  1087. XXH64_reset(&xxhNewFast, 0);
  1088. LZ4_resetStream_fast(&streamingState);
  1089. LZ4_setStreamDecode(&decodeStateSafe, NULL, 0);
  1090. LZ4_setStreamDecode(&decodeStateFast, NULL, 0);
  1091. while (iNext + messageSize < testCompressedSize) {
  1092. int compressedSize; U64 crcOrig;
  1093. XXH64_update(&xxhOrig, testInput + iNext, messageSize);
  1094. crcOrig = XXH64_digest(&xxhOrig);
  1095. memcpy (ringBuffer + rNext, testInput + iNext, messageSize);
  1096. compressedSize = LZ4_compress_fast_continue(&streamingState, ringBuffer + rNext, testCompressed, (int)messageSize, testCompressedSize-ringBufferSize, 1);
  1097. FUZ_CHECKTEST(compressedSize==0, "LZ4_compress_fast_continue() compression failed");
  1098. { int const r = LZ4_decompress_safe_continue(&decodeStateSafe, testCompressed, testVerify + dNext, compressedSize, (int)messageSize);
  1099. FUZ_CHECKTEST(r!=(int)messageSize, "ringBuffer : LZ4_decompress_safe_continue() test failed"); }
  1100. XXH64_update(&xxhNewSafe, testVerify + dNext, messageSize);
  1101. { U64 const crcNew = XXH64_digest(&xxhNewSafe);
  1102. FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_continue() decompression corruption"); }
  1103. { int const r = LZ4_decompress_fast_continue(&decodeStateFast, testCompressed, testVerify + dNext, (int)messageSize);
  1104. FUZ_CHECKTEST(r!=compressedSize, "ringBuffer : LZ4_decompress_fast_continue() test failed"); }
  1105. XXH64_update(&xxhNewFast, testVerify + dNext, messageSize);
  1106. { U64 const crcNew = XXH64_digest(&xxhNewFast);
  1107. FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_fast_continue() decompression corruption"); }
  1108. /* prepare next message */
  1109. iNext += messageSize;
  1110. rNext += messageSize;
  1111. dNext += messageSize;
  1112. messageSize = (FUZ_rand(&randState) & maxMessageSizeMask) + 1;
  1113. if (rNext + messageSize > ringBufferSize) rNext = 0;
  1114. if (dNext + messageSize > dBufferSize) dNext = 0;
  1115. } }
  1116. }
  1117. DISPLAYLEVEL(3, "LZ4_initStreamHC with multiple valid alignments : ");
  1118. { typedef struct {
  1119. LZ4_streamHC_t hc1;
  1120. LZ4_streamHC_t hc2;
  1121. char c;
  1122. LZ4_streamHC_t hc3;
  1123. } shct;
  1124. shct* const shc = (shct*)malloc(sizeof(*shc));
  1125. assert(shc != NULL);
  1126. memset(shc, 0, sizeof(*shc));
  1127. DISPLAYLEVEL(4, "hc1(%p) hc2(%p) hc3(%p) size(0x%x): ",
  1128. (void*)&(shc->hc1), (void*)&(shc->hc2), (void*)&(shc->hc3),
  1129. (unsigned)sizeof(LZ4_streamHC_t));
  1130. FUZ_CHECKTEST( LZ4_initStreamHC(&(shc->hc1), sizeof(shc->hc1)) == NULL, "hc1 (%p) failed init", (void*)&(shc->hc1) );
  1131. FUZ_CHECKTEST( LZ4_initStreamHC(&(shc->hc2), sizeof(shc->hc2)) == NULL, "hc2 (%p) failed init", (void*)&(shc->hc2) );
  1132. FUZ_CHECKTEST( LZ4_initStreamHC(&(shc->hc3), sizeof(shc->hc3)) == NULL, "hc3 (%p) failed init", (void*)&(shc->hc3) );
  1133. FUZ_CHECKTEST( LZ4_initStreamHC((char*)&(shc->hc1) + 1, sizeof(shc->hc1)) != NULL,
  1134. "hc1+1 (%p) init must fail, due to bad alignment", (void*)((char*)&(shc->hc1) + 1) );
  1135. free(shc);
  1136. }
  1137. DISPLAYLEVEL(3, "all inits OK \n");
  1138. /* LZ4 HC streaming tests */
  1139. { LZ4_streamHC_t sHC; /* statically allocated */
  1140. int result;
  1141. LZ4_initStreamHC(&sHC, sizeof(sHC));
  1142. /* Allocation test */
  1143. DISPLAYLEVEL(3, "Basic HC allocation : ");
  1144. { LZ4_streamHC_t* const sp = LZ4_createStreamHC();
  1145. FUZ_CHECKTEST(sp==NULL, "LZ4_createStreamHC() allocation failed");
  1146. LZ4_freeStreamHC(sp);
  1147. }
  1148. DISPLAYLEVEL(3, "OK \n");
  1149. /* simple HC compression test */
  1150. DISPLAYLEVEL(3, "Simple HC round-trip : ");
  1151. { U64 const crc64 = XXH64(testInput, testCompressedSize, 0);
  1152. LZ4_setCompressionLevel(&sHC, compressionLevel);
  1153. result = LZ4_compress_HC_continue(&sHC, testInput, testCompressed, testCompressedSize, testCompressedSize-1);
  1154. FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() compression failed");
  1155. FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean");
  1156. result = LZ4_decompress_safe(testCompressed, testVerify, result, testCompressedSize);
  1157. FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed");
  1158. { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0);
  1159. FUZ_CHECKTEST(crc64!=crcNew, "LZ4_decompress_safe() decompression corruption");
  1160. } }
  1161. DISPLAYLEVEL(3, "OK \n");
  1162. /* saveDictHC test #926 */
  1163. DISPLAYLEVEL(3, "saveDictHC test #926 : ");
  1164. { LZ4_streamHC_t* const ctx = LZ4_initStreamHC(&sHC, sizeof(sHC));
  1165. assert(ctx != NULL); /* ensure init is successful */
  1166. /* Check access violation with asan */
  1167. FUZ_CHECKTEST( LZ4_saveDictHC(ctx, NULL, 0) != 0,
  1168. "LZ4_saveDictHC() can't save anything into (NULL,0)");
  1169. /* Check access violation with asan */
  1170. { char tmp_buffer[240] = { 0 };
  1171. FUZ_CHECKTEST( LZ4_saveDictHC(ctx, tmp_buffer, sizeof(tmp_buffer)) != 0,
  1172. "LZ4_saveDictHC() can't save anything since compression hasn't started");
  1173. } }
  1174. DISPLAYLEVEL(3, "OK \n");
  1175. /* long sequence test */
  1176. DISPLAYLEVEL(3, "Long sequence HC_destSize test : ");
  1177. { size_t const blockSize = 1 MB;
  1178. size_t const targetSize = 4116; /* size carefully selected to trigger an overflow */
  1179. void* const block = malloc(blockSize);
  1180. void* const dstBlock = malloc(targetSize+1);
  1181. BYTE const sentinel = 101;
  1182. int srcSize;
  1183. assert(block != NULL); assert(dstBlock != NULL);
  1184. memset(block, 0, blockSize);
  1185. ((char*)dstBlock)[targetSize] = sentinel;
  1186. LZ4_resetStreamHC_fast(&sHC, 3);
  1187. assert(blockSize < INT_MAX);
  1188. srcSize = (int)blockSize;
  1189. assert(targetSize < INT_MAX);
  1190. result = LZ4_compress_HC_destSize(&sHC, (const char*)block, (char*)dstBlock, &srcSize, (int)targetSize, 3);
  1191. DISPLAYLEVEL(4, "cSize=%i; readSize=%i; ", result, srcSize);
  1192. FUZ_CHECKTEST(result != 4116, "LZ4_compress_HC_destSize() : "
  1193. "compression (%i->%i) must fill dstBuffer (%i) exactly",
  1194. srcSize, result, (int)targetSize);
  1195. FUZ_CHECKTEST(((char*)dstBlock)[targetSize] != sentinel,
  1196. "LZ4_compress_HC_destSize() overwrites dst buffer");
  1197. FUZ_CHECKTEST(srcSize < 1045000, "LZ4_compress_HC_destSize() doesn't compress enough"
  1198. " (%i -> %i , expected > %i)", srcSize, result, 1045000);
  1199. LZ4_resetStreamHC_fast(&sHC, 3); /* make sure the context is clean after the test */
  1200. free(block);
  1201. free(dstBlock);
  1202. }
  1203. DISPLAYLEVEL(3, " OK \n");
  1204. /* simple dictionary HC compression test */
  1205. DISPLAYLEVEL(3, "HC dictionary compression test : ");
  1206. { U64 const crc64 = XXH64(testInput + 64 KB, testCompressedSize, 0);
  1207. LZ4_resetStreamHC_fast(&sHC, compressionLevel);
  1208. LZ4_loadDictHC(&sHC, testInput, 64 KB);
  1209. { int const cSize = LZ4_compress_HC_continue(&sHC, testInput + 64 KB, testCompressed, testCompressedSize, testCompressedSize-1);
  1210. FUZ_CHECKTEST(cSize==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : @return = %i", cSize);
  1211. FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean");
  1212. { int const dSize = LZ4_decompress_safe_usingDict(testCompressed, testVerify, cSize, testCompressedSize, testInput, 64 KB);
  1213. FUZ_CHECKTEST(dSize!=(int)testCompressedSize, "LZ4_decompress_safe() simple dictionary decompression test failed");
  1214. } }
  1215. { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0);
  1216. FUZ_CHECKTEST(crc64!=crcNew, "LZ4_decompress_safe() simple dictionary decompression test : corruption");
  1217. } }
  1218. DISPLAYLEVEL(3, " OK \n");
  1219. /* multiple HC compression test with dictionary */
  1220. { int result1, result2;
  1221. int segSize = testCompressedSize / 2;
  1222. XXH64_hash_t const crc64 = ( (void)assert((unsigned)segSize + testCompressedSize < testInputSize) ,
  1223. XXH64(testInput + segSize, testCompressedSize, 0) );
  1224. LZ4_resetStreamHC_fast(&sHC, compressionLevel);
  1225. LZ4_loadDictHC(&sHC, testInput, segSize);
  1226. result1 = LZ4_compress_HC_continue(&sHC, testInput + segSize, testCompressed, segSize, segSize -1);
  1227. FUZ_CHECKTEST(result1==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result1);
  1228. FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean");
  1229. result2 = LZ4_compress_HC_continue(&sHC, testInput + 2*(size_t)segSize, testCompressed+result1, segSize, segSize-1);
  1230. FUZ_CHECKTEST(result2==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result2);
  1231. FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean");
  1232. result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result1, segSize, testInput, segSize);
  1233. FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe() dictionary decompression part 1 failed");
  1234. result = LZ4_decompress_safe_usingDict(testCompressed+result1, testVerify+segSize, result2, segSize, testInput, 2*segSize);
  1235. FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe() dictionary decompression part 2 failed");
  1236. { XXH64_hash_t const crcNew = XXH64(testVerify, testCompressedSize, 0);
  1237. FUZ_CHECKTEST(crc64!=crcNew, "LZ4_decompress_safe() dictionary decompression corruption");
  1238. } }
  1239. /* remote dictionary HC compression test */
  1240. { U64 const crc64 = XXH64(testInput + 64 KB, testCompressedSize, 0);
  1241. LZ4_resetStreamHC_fast(&sHC, compressionLevel);
  1242. LZ4_loadDictHC(&sHC, testInput, 32 KB);
  1243. result = LZ4_compress_HC_continue(&sHC, testInput + 64 KB, testCompressed, testCompressedSize, testCompressedSize-1);
  1244. FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() remote dictionary failed : result = %i", result);
  1245. FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean");
  1246. result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result, testCompressedSize, testInput, 32 KB);
  1247. FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe_usingDict() decompression failed following remote dictionary HC compression test");
  1248. { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0);
  1249. FUZ_CHECKTEST(crc64!=crcNew, "LZ4_decompress_safe_usingDict() decompression corruption");
  1250. } }
  1251. /* multiple HC compression with ext. dictionary */
  1252. { XXH64_state_t crcOrigState;
  1253. XXH64_state_t crcNewState;
  1254. const char* dict = testInput + 3;
  1255. size_t dictSize = (FUZ_rand(&randState) & 8191);
  1256. char* dst = testVerify;
  1257. size_t segStart = dictSize + 7;
  1258. size_t segSize = (FUZ_rand(&randState) & 8191);
  1259. int segNb = 1;
  1260. LZ4_resetStreamHC_fast(&sHC, compressionLevel);
  1261. LZ4_loadDictHC(&sHC, dict, (int)dictSize);
  1262. XXH64_reset(&crcOrigState, 0);
  1263. XXH64_reset(&crcNewState, 0);
  1264. while (segStart + segSize < testInputSize) {
  1265. XXH64_hash_t crcOrig;
  1266. XXH64_update(&crcOrigState, testInput + segStart, segSize);
  1267. crcOrig = XXH64_digest(&crcOrigState);
  1268. assert(segSize <= INT_MAX);
  1269. result = LZ4_compress_HC_continue(&sHC, testInput + segStart, testCompressed, (int)segSize, LZ4_compressBound((int)segSize));
  1270. FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result);
  1271. FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean");
  1272. result = LZ4_decompress_safe_usingDict(testCompressed, dst, result, (int)segSize, dict, (int)dictSize);
  1273. FUZ_CHECKTEST(result!=(int)segSize, "LZ4_decompress_safe_usingDict() dictionary decompression part %i failed", (int)segNb);
  1274. XXH64_update(&crcNewState, dst, segSize);
  1275. { U64 const crcNew = XXH64_digest(&crcNewState);
  1276. if (crcOrig != crcNew) FUZ_findDiff(dst, testInput+segStart);
  1277. FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_usingDict() part %i corruption", segNb);
  1278. }
  1279. dict = dst;
  1280. dictSize = segSize;
  1281. dst += segSize + 1;
  1282. segNb ++;
  1283. segStart += segSize + (FUZ_rand(&randState) & 0xF) + 1;
  1284. segSize = (FUZ_rand(&randState) & 8191);
  1285. } }
  1286. /* ring buffer test */
  1287. { XXH64_state_t xxhOrig;
  1288. XXH64_state_t xxhNewSafe, xxhNewFast;
  1289. LZ4_streamDecode_t decodeStateSafe, decodeStateFast;
  1290. const U32 maxMessageSizeLog = 10;
  1291. const U32 maxMessageSizeMask = (1<<maxMessageSizeLog) - 1;
  1292. U32 messageSize = (FUZ_rand(&randState) & maxMessageSizeMask) + 1;
  1293. U32 iNext = 0;
  1294. U32 rNext = 0;
  1295. U32 dNext = 0;
  1296. const U32 dBufferSize = ringBufferSize + maxMessageSizeMask;
  1297. XXH64_reset(&xxhOrig, 0);
  1298. XXH64_reset(&xxhNewSafe, 0);
  1299. XXH64_reset(&xxhNewFast, 0);
  1300. LZ4_resetStreamHC_fast(&sHC, compressionLevel);
  1301. LZ4_setStreamDecode(&decodeStateSafe, NULL, 0);
  1302. LZ4_setStreamDecode(&decodeStateFast, NULL, 0);
  1303. while (iNext + messageSize < testCompressedSize) {
  1304. int compressedSize;
  1305. XXH64_hash_t crcOrig;
  1306. XXH64_update(&xxhOrig, testInput + iNext, messageSize);
  1307. crcOrig = XXH64_digest(&xxhOrig);
  1308. memcpy (ringBuffer + rNext, testInput + iNext, messageSize);
  1309. assert(messageSize < INT_MAX);
  1310. compressedSize = LZ4_compress_HC_continue(&sHC, ringBuffer + rNext, testCompressed, (int)messageSize, testCompressedSize-ringBufferSize);
  1311. FUZ_CHECKTEST(compressedSize==0, "LZ4_compress_HC_continue() compression failed");
  1312. FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean");
  1313. assert(messageSize < INT_MAX);
  1314. result = LZ4_decompress_safe_continue(&decodeStateSafe, testCompressed, testVerify + dNext, compressedSize, (int)messageSize);
  1315. FUZ_CHECKTEST(result!=(int)messageSize, "ringBuffer : LZ4_decompress_safe_continue() test failed");
  1316. XXH64_update(&xxhNewSafe, testVerify + dNext, messageSize);
  1317. { XXH64_hash_t const crcNew = XXH64_digest(&xxhNewSafe);
  1318. FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_continue() decompression corruption"); }
  1319. assert(messageSize < INT_MAX);
  1320. result = LZ4_decompress_fast_continue(&decodeStateFast, testCompressed, testVerify + dNext, (int)messageSize);
  1321. FUZ_CHECKTEST(result!=compressedSize, "ringBuffer : LZ4_decompress_fast_continue() test failed");
  1322. XXH64_update(&xxhNewFast, testVerify + dNext, messageSize);
  1323. { XXH64_hash_t const crcNew = XXH64_digest(&xxhNewFast);
  1324. FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_fast_continue() decompression corruption"); }
  1325. /* prepare next message */
  1326. iNext += messageSize;
  1327. rNext += messageSize;
  1328. dNext += messageSize;
  1329. messageSize = (FUZ_rand(&randState) & maxMessageSizeMask) + 1;
  1330. if (rNext + messageSize > ringBufferSize) rNext = 0;
  1331. if (dNext + messageSize > dBufferSize) dNext = 0;
  1332. }
  1333. }
  1334. /* Ring buffer test : Non synchronized decoder */
  1335. /* This test uses minimum amount of memory required to setup a decoding ring buffer
  1336. * while being unsynchronized with encoder
  1337. * (no assumption done on how the data is encoded, it just follows LZ4 format specification).
  1338. * This size is documented in lz4.h, and is LZ4_decoderRingBufferSize(maxBlockSize).
  1339. */
  1340. { XXH64_state_t xxhOrig;
  1341. XXH64_state_t xxhNewSafe, xxhNewFast;
  1342. XXH64_hash_t crcOrig;
  1343. LZ4_streamDecode_t decodeStateSafe, decodeStateFast;
  1344. const int maxMessageSizeLog = 12;
  1345. const int maxMessageSize = 1 << maxMessageSizeLog;
  1346. const int maxMessageSizeMask = maxMessageSize - 1;
  1347. int messageSize;
  1348. U32 totalMessageSize = 0;
  1349. const int dBufferSize = LZ4_decoderRingBufferSize(maxMessageSize);
  1350. char* const ringBufferSafe = testVerify;
  1351. char* const ringBufferFast = testVerify + dBufferSize + 1; /* used by LZ4_decompress_fast_continue */
  1352. int iNext = 0;
  1353. int dNext = 0;
  1354. int compressedSize;
  1355. assert((size_t)dBufferSize * 2 + 1 < testInputSize); /* space used by ringBufferSafe and ringBufferFast */
  1356. XXH64_reset(&xxhOrig, 0);
  1357. XXH64_reset(&xxhNewSafe, 0);
  1358. XXH64_reset(&xxhNewFast, 0);
  1359. LZ4_resetStreamHC_fast(&sHC, compressionLevel);
  1360. LZ4_setStreamDecode(&decodeStateSafe, NULL, 0);
  1361. LZ4_setStreamDecode(&decodeStateFast, NULL, 0);
  1362. #define BSIZE1 (dBufferSize - (maxMessageSize-1))
  1363. /* first block */
  1364. messageSize = BSIZE1; /* note : we cheat a bit here, in theory no message should be > maxMessageSize. We just want to fill the decoding ring buffer once. */
  1365. XXH64_update(&xxhOrig, testInput + iNext, (size_t)messageSize);
  1366. crcOrig = XXH64_digest(&xxhOrig);
  1367. compressedSize = LZ4_compress_HC_continue(&sHC, testInput + iNext, testCompressed, messageSize, testCompressedSize-ringBufferSize);
  1368. FUZ_CHECKTEST(compressedSize==0, "LZ4_compress_HC_continue() compression failed");
  1369. FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean");
  1370. result = LZ4_decompress_safe_continue(&decodeStateSafe, testCompressed, ringBufferSafe + dNext, compressedSize, messageSize);
  1371. FUZ_CHECKTEST(result!=messageSize, "64K D.ringBuffer : LZ4_decompress_safe_continue() test failed");
  1372. XXH64_update(&xxhNewSafe, ringBufferSafe + dNext, (size_t)messageSize);
  1373. { U64 const crcNew = XXH64_digest(&xxhNewSafe);
  1374. FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_continue() decompression corruption"); }
  1375. result = LZ4_decompress_fast_continue(&decodeStateFast, testCompressed, ringBufferFast + dNext, messageSize);
  1376. FUZ_CHECKTEST(result!=compressedSize, "64K D.ringBuffer : LZ4_decompress_fast_continue() test failed");
  1377. XXH64_update(&xxhNewFast, ringBufferFast + dNext, (size_t)messageSize);
  1378. { U64 const crcNew = XXH64_digest(&xxhNewFast);
  1379. FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_fast_continue() decompression corruption"); }
  1380. /* prepare second message */
  1381. dNext += messageSize;
  1382. assert(messageSize >= 0);
  1383. totalMessageSize += (unsigned)messageSize;
  1384. messageSize = maxMessageSize;
  1385. iNext = BSIZE1+1;
  1386. assert(BSIZE1 >= 65535);
  1387. memcpy(testInput + iNext, testInput + (BSIZE1-65535), messageSize); /* will generate a match at max distance == 65535 */
  1388. FUZ_CHECKTEST(dNext+messageSize <= dBufferSize, "Ring buffer test : second message should require restarting from beginning");
  1389. dNext = 0;
  1390. while (totalMessageSize < 9 MB) {
  1391. XXH64_update(&xxhOrig, testInput + iNext, (size_t)messageSize);
  1392. crcOrig = XXH64_digest(&xxhOrig);
  1393. compressedSize = LZ4_compress_HC_continue(&sHC, testInput + iNext, testCompressed, messageSize, testCompressedSize-ringBufferSize);
  1394. FUZ_CHECKTEST(compressedSize==0, "LZ4_compress_HC_continue() compression failed");
  1395. FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean");
  1396. DISPLAYLEVEL(5, "compressed %i bytes to %i bytes \n", messageSize, compressedSize);
  1397. /* test LZ4_decompress_safe_continue */
  1398. assert(dNext < dBufferSize);
  1399. assert(dBufferSize - dNext >= maxMessageSize);
  1400. result = LZ4_decompress_safe_continue(&decodeStateSafe,
  1401. testCompressed, ringBufferSafe + dNext,
  1402. compressedSize, dBufferSize - dNext); /* works without knowing messageSize, under assumption that messageSize <= maxMessageSize */
  1403. FUZ_CHECKTEST(result!=messageSize, "D.ringBuffer : LZ4_decompress_safe_continue() test failed");
  1404. XXH64_update(&xxhNewSafe, ringBufferSafe + dNext, (size_t)messageSize);
  1405. { U64 const crcNew = XXH64_digest(&xxhNewSafe);
  1406. if (crcOrig != crcNew) FUZ_findDiff(testInput + iNext, ringBufferSafe + dNext);
  1407. FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_continue() decompression corruption during D.ringBuffer test");
  1408. }
  1409. /* test LZ4_decompress_fast_continue in its own buffer ringBufferFast */
  1410. result = LZ4_decompress_fast_continue(&decodeStateFast, testCompressed, ringBufferFast + dNext, messageSize);
  1411. FUZ_CHECKTEST(result!=compressedSize, "D.ringBuffer : LZ4_decompress_fast_continue() test failed");
  1412. XXH64_update(&xxhNewFast, ringBufferFast + dNext, (size_t)messageSize);
  1413. { U64 const crcNew = XXH64_digest(&xxhNewFast);
  1414. if (crcOrig != crcNew) FUZ_findDiff(testInput + iNext, ringBufferFast + dNext);
  1415. FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_fast_continue() decompression corruption during D.ringBuffer test");
  1416. }
  1417. /* prepare next message */
  1418. dNext += messageSize;
  1419. assert(messageSize >= 0);
  1420. totalMessageSize += (unsigned)messageSize;
  1421. messageSize = (FUZ_rand(&randState) & maxMessageSizeMask) + 1;
  1422. iNext = (FUZ_rand(&randState) & 65535);
  1423. if (dNext + maxMessageSize > dBufferSize) dNext = 0;
  1424. }
  1425. } /* Ring buffer test : Non synchronized decoder */
  1426. }
  1427. DISPLAYLEVEL(3, "LZ4_compress_HC_destSize : ");
  1428. /* encode congenerical sequence test for HC compressors */
  1429. { LZ4_streamHC_t* const sHC = LZ4_createStreamHC();
  1430. int const src_buf_size = 3 MB;
  1431. int const dst_buf_size = 6 KB;
  1432. int const payload = 0;
  1433. int const dst_step = 43;
  1434. int const dst_min_len = 33 + (FUZ_rand(&randState) % dst_step);
  1435. int const dst_max_len = 5000;
  1436. int slen, dlen;
  1437. char* sbuf1 = (char*)malloc(src_buf_size + 1);
  1438. char* sbuf2 = (char*)malloc(src_buf_size + 1);
  1439. char* dbuf1 = (char*)malloc(dst_buf_size + 1);
  1440. char* dbuf2 = (char*)malloc(dst_buf_size + 1);
  1441. assert(sHC != NULL);
  1442. assert(dst_buf_size > dst_max_len);
  1443. if (!sbuf1 || !sbuf2 || !dbuf1 || !dbuf2) {
  1444. EXIT_MSG("not enough memory for FUZ_unitTests (destSize)");
  1445. }
  1446. for (dlen = dst_min_len; dlen <= dst_max_len; dlen += dst_step) {
  1447. int src_len = (dlen - 10)*255 + 24;
  1448. if (src_len + 10 >= src_buf_size) break; /* END of check */
  1449. for (slen = src_len - 3; slen <= src_len + 3; slen++) {
  1450. int srcsz1, srcsz2;
  1451. int dsz1, dsz2;
  1452. int res1, res2;
  1453. char const endchk = (char)0x88;
  1454. DISPLAYLEVEL(5, "slen = %i, ", slen);
  1455. srcsz1 = slen;
  1456. memset(sbuf1, payload, slen);
  1457. memset(dbuf1, 0, dlen);
  1458. dbuf1[dlen] = endchk;
  1459. dsz1 = LZ4_compress_destSize(sbuf1, dbuf1, &srcsz1, dlen);
  1460. DISPLAYLEVEL(5, "LZ4_compress_destSize: %i bytes compressed into %i bytes, ", srcsz1, dsz1);
  1461. DISPLAYLEVEL(5, "last token : 0x%0X, ", dbuf1[dsz1 - 6]);
  1462. DISPLAYLEVEL(5, "last ML extra lenbyte : 0x%0X, \n", dbuf1[dsz1 - 7]);
  1463. FUZ_CHECKTEST(dbuf1[dlen] != endchk, "LZ4_compress_destSize() overwrite dst buffer !");
  1464. FUZ_CHECKTEST(dsz1 <= 0, "LZ4_compress_destSize() compression failed");
  1465. FUZ_CHECKTEST(dsz1 > dlen, "LZ4_compress_destSize() result larger than dst buffer !");
  1466. FUZ_CHECKTEST(srcsz1 > slen, "LZ4_compress_destSize() read more than src buffer !");
  1467. res1 = LZ4_decompress_safe(dbuf1, sbuf1, dsz1, src_buf_size);
  1468. FUZ_CHECKTEST(res1 != srcsz1, "LZ4_compress_destSize() decompression failed!");
  1469. srcsz2 = slen;
  1470. memset(sbuf2, payload, slen);
  1471. memset(dbuf2, 0, dlen);
  1472. dbuf2[dlen] = endchk;
  1473. LZ4_resetStreamHC(sHC, compressionLevel);
  1474. dsz2 = LZ4_compress_HC_destSize(sHC, sbuf2, dbuf2, &srcsz2, dlen, compressionLevel);
  1475. DISPLAYLEVEL(5, "LZ4_compress_HC_destSize: %i bytes compressed into %i bytes, ", srcsz2, dsz2);
  1476. DISPLAYLEVEL(5, "last token : 0x%0X, ", dbuf2[dsz2 - 6]);
  1477. DISPLAYLEVEL(5, "last ML extra lenbyte : 0x%0X, \n", dbuf2[dsz2 - 7]);
  1478. FUZ_CHECKTEST(dbuf2[dlen] != endchk, "LZ4_compress_HC_destSize() overwrite dst buffer !");
  1479. FUZ_CHECKTEST(dsz2 <= 0, "LZ4_compress_HC_destSize() compression failed");
  1480. FUZ_CHECKTEST(dsz2 > dlen, "LZ4_compress_HC_destSize() result larger than dst buffer !");
  1481. FUZ_CHECKTEST(srcsz2 > slen, "LZ4_compress_HC_destSize() read more than src buffer !");
  1482. FUZ_CHECKTEST(dsz2 != dsz1, "LZ4_compress_HC_destSize() return incorrect result !");
  1483. FUZ_CHECKTEST(srcsz2 != srcsz1, "LZ4_compress_HC_destSize() return incorrect src buffer size "
  1484. ": srcsz2(%i) != srcsz1(%i)", srcsz2, srcsz1);
  1485. FUZ_CHECKTEST(memcmp(dbuf2, dbuf1, (size_t)dsz2), "LZ4_compress_HC_destSize() return incorrect data into dst buffer !");
  1486. res2 = LZ4_decompress_safe(dbuf2, sbuf1, dsz2, src_buf_size);
  1487. FUZ_CHECKTEST(res2 != srcsz1, "LZ4_compress_HC_destSize() decompression failed!");
  1488. FUZ_CHECKTEST(memcmp(sbuf1, sbuf2, (size_t)res2), "LZ4_compress_HC_destSize() decompression corruption!");
  1489. }
  1490. }
  1491. LZ4_freeStreamHC(sHC);
  1492. free(sbuf1);
  1493. free(sbuf2);
  1494. free(dbuf1);
  1495. free(dbuf2);
  1496. }
  1497. DISPLAYLEVEL(3, " OK \n");
  1498. /* clean up */
  1499. free(testInput);
  1500. free(testCompressed);
  1501. free(testVerify);
  1502. printf("All unit tests completed successfully compressionLevel=%d \n", compressionLevel);
  1503. return;
  1504. }
  1505. /* =======================================
  1506. * CLI
  1507. * ======================================= */
  1508. static int FUZ_usage(const char* programName)
  1509. {
  1510. DISPLAY( "Usage :\n");
  1511. DISPLAY( " %s [args]\n", programName);
  1512. DISPLAY( "\n");
  1513. DISPLAY( "Arguments :\n");
  1514. DISPLAY( " -i# : Nb of tests (default:%i) \n", NB_ATTEMPTS);
  1515. DISPLAY( " -T# : Duration of tests, in seconds (default: use Nb of tests) \n");
  1516. DISPLAY( " -s# : Select seed (default:prompt user)\n");
  1517. DISPLAY( " -t# : Select starting test number (default:0)\n");
  1518. DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
  1519. DISPLAY( " -v : verbose\n");
  1520. DISPLAY( " -p : pause at the end\n");
  1521. DISPLAY( " -h : display help and exit\n");
  1522. return 0;
  1523. }
  1524. int main(int argc, const char** argv)
  1525. {
  1526. U32 seed = 0;
  1527. int seedset = 0;
  1528. int argNb;
  1529. unsigned nbTests = NB_ATTEMPTS;
  1530. unsigned testNb = 0;
  1531. int proba = FUZ_COMPRESSIBILITY_DEFAULT;
  1532. int use_pause = 0;
  1533. const char* programName = argv[0];
  1534. U32 duration = 0;
  1535. /* Check command line */
  1536. for(argNb=1; argNb<argc; argNb++) {
  1537. const char* argument = argv[argNb];
  1538. if(!argument) continue; // Protection if argument empty
  1539. // Decode command (note : aggregated commands are allowed)
  1540. if (argument[0]=='-') {
  1541. if (!strcmp(argument, "--no-prompt")) { use_pause=0; seedset=1; g_displayLevel=1; continue; }
  1542. argument++;
  1543. while (*argument!=0) {
  1544. switch(*argument)
  1545. {
  1546. case 'h': /* display help */
  1547. return FUZ_usage(programName);
  1548. case 'v': /* verbose mode */
  1549. g_displayLevel++;
  1550. argument++;
  1551. break;
  1552. case 'p': /* pause at the end */
  1553. use_pause=1;
  1554. argument++;
  1555. break;
  1556. case 'i':
  1557. argument++;
  1558. nbTests = 0; duration = 0;
  1559. while ((*argument>='0') && (*argument<='9')) {
  1560. nbTests *= 10;
  1561. nbTests += (unsigned)(*argument - '0');
  1562. argument++;
  1563. }
  1564. break;
  1565. case 'T':
  1566. argument++;
  1567. nbTests = 0; duration = 0;
  1568. for (;;) {
  1569. switch(*argument)
  1570. {
  1571. case 'm': duration *= 60; argument++; continue;
  1572. case 's':
  1573. case 'n': argument++; continue;
  1574. case '0':
  1575. case '1':
  1576. case '2':
  1577. case '3':
  1578. case '4':
  1579. case '5':
  1580. case '6':
  1581. case '7':
  1582. case '8':
  1583. case '9': duration *= 10; duration += (U32)(*argument++ - '0'); continue;
  1584. }
  1585. break;
  1586. }
  1587. break;
  1588. case 's':
  1589. argument++;
  1590. seed=0; seedset=1;
  1591. while ((*argument>='0') && (*argument<='9')) {
  1592. seed *= 10;
  1593. seed += (U32)(*argument - '0');
  1594. argument++;
  1595. }
  1596. break;
  1597. case 't': /* select starting test nb */
  1598. argument++;
  1599. testNb=0;
  1600. while ((*argument>='0') && (*argument<='9')) {
  1601. testNb *= 10;
  1602. testNb += (unsigned)(*argument - '0');
  1603. argument++;
  1604. }
  1605. break;
  1606. case 'P': /* change probability */
  1607. argument++;
  1608. proba=0;
  1609. while ((*argument>='0') && (*argument<='9')) {
  1610. proba *= 10;
  1611. proba += *argument - '0';
  1612. argument++;
  1613. }
  1614. if (proba<0) proba=0;
  1615. if (proba>100) proba=100;
  1616. break;
  1617. default: ;
  1618. }
  1619. }
  1620. }
  1621. }
  1622. printf("Starting LZ4 fuzzer (%i-bits, v%s)\n", (int)(sizeof(size_t)*8), LZ4_versionString());
  1623. if (!seedset) {
  1624. time_t const t = time(NULL);
  1625. U32 const h = XXH32(&t, sizeof(t), 1);
  1626. seed = h % 10000;
  1627. }
  1628. printf("Seed = %u\n", seed);
  1629. if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) printf("Compressibility : %i%%\n", proba);
  1630. if ((seedset==0) && (testNb==0)) { FUZ_unitTests(LZ4HC_CLEVEL_DEFAULT); FUZ_unitTests(LZ4HC_CLEVEL_OPT_MIN); }
  1631. nbTests += (nbTests==0); /* avoid zero */
  1632. { int const result = FUZ_test(seed, nbTests, testNb, ((double)proba) / 100, duration);
  1633. if (use_pause) {
  1634. DISPLAY("press enter ... \n");
  1635. (void)getchar();
  1636. }
  1637. return result;
  1638. }
  1639. }