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