Home | History | Annotate | Download | only in tests
      1 /*
      2     fuzzer.c - Fuzzer test tool for LZ4
      3     Copyright (C) Yann Collet 2012-2016
      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 
     36 /*-************************************
     37 *  Dependencies
     38 **************************************/
     39 #include "platform.h"   /* _CRT_SECURE_NO_WARNINGS */
     40 #include "util.h"       /* U32 */
     41 #include <stdlib.h>
     42 #include <stdio.h>      /* fgets, sscanf */
     43 #include <string.h>     /* strcmp */
     44 #include <time.h>       /* clock_t, clock, CLOCKS_PER_SEC */
     45 #include "lz4hc.h"
     46 #define XXH_STATIC_LINKING_ONLY
     47 #include "xxhash.h"
     48 
     49 
     50 /*-************************************
     51 *  Basic Types
     52 **************************************/
     53 #if !defined(__cplusplus) && !(defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
     54 typedef size_t uintptr_t;   /* true on most systems, except OpenVMS-64 (which doesn't need address overflow test) */
     55 #endif
     56 
     57 
     58 /*-************************************
     59 *  Constants
     60 **************************************/
     61 #define NB_ATTEMPTS (1<<16)
     62 #define COMPRESSIBLE_NOISE_LENGTH (1 << 21)
     63 #define FUZ_MAX_BLOCK_SIZE (1 << 17)
     64 #define FUZ_MAX_DICT_SIZE  (1 << 15)
     65 #define FUZ_COMPRESSIBILITY_DEFAULT 60
     66 #define PRIME1   2654435761U
     67 #define PRIME2   2246822519U
     68 #define PRIME3   3266489917U
     69 
     70 #define KB *(1U<<10)
     71 #define MB *(1U<<20)
     72 #define GB *(1U<<30)
     73 
     74 
     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 
     82 #define MIN(a,b)   ( (a) < (b) ? (a) : (b) )
     83 
     84 
     85 /*-*******************************************************
     86 *  Fuzzer functions
     87 *********************************************************/
     88 static clock_t FUZ_GetClockSpan(clock_t clockStart)
     89 {
     90     return clock() - clockStart;   /* works even if overflow; max span ~ 30mn */
     91 }
     92 
     93 static void FUZ_displayUpdate(unsigned testNb)
     94 {
     95     static clock_t g_time = 0;
     96     static const clock_t g_refreshRate = CLOCKS_PER_SEC / 5;
     97     if ((FUZ_GetClockSpan(g_time) > g_refreshRate) || (g_displayLevel>=4)) {
     98         g_time = clock();
     99         DISPLAY("\r%5u   ", testNb);
    100         fflush(stdout);
    101     }
    102 }
    103 
    104 static U32 FUZ_rotl32(U32 u32, U32 nbBits)
    105 {
    106     return ((u32 << nbBits) | (u32 >> (32 - nbBits)));
    107 }
    108 
    109 static U32 FUZ_rand(U32* src)
    110 {
    111     U32 rand32 = *src;
    112     rand32 *= PRIME1;
    113     rand32 ^= PRIME2;
    114     rand32  = FUZ_rotl32(rand32, 13);
    115     *src = rand32;
    116     return rand32;
    117 }
    118 
    119 
    120 #define FUZ_RAND15BITS  ((FUZ_rand(seed) >> 3) & 32767)
    121 #define FUZ_RANDLENGTH  ( ((FUZ_rand(seed) >> 7) & 3) ? (FUZ_rand(seed) % 15) : (FUZ_rand(seed) % 510) + 15)
    122 static void FUZ_fillCompressibleNoiseBuffer(void* buffer, size_t bufferSize, double proba, U32* seed)
    123 {
    124     BYTE* const BBuffer = (BYTE*)buffer;
    125     size_t pos = 0;
    126     U32 const P32 = (U32)(32768 * proba);
    127 
    128     /* First Bytes */
    129     while (pos < 20)
    130         BBuffer[pos++] = (BYTE)(FUZ_rand(seed));
    131 
    132     while (pos < bufferSize) {
    133         /* Select : Literal (noise) or copy (within 64K) */
    134         if (FUZ_RAND15BITS < P32) {
    135             /* Copy (within 64K) */
    136             size_t const length = FUZ_RANDLENGTH + 4;
    137             size_t const d = MIN(pos+length, bufferSize);
    138             size_t match;
    139             size_t offset = FUZ_RAND15BITS + 1;
    140             while (offset > pos) offset >>= 1;
    141             match = pos - offset;
    142             while (pos < d) BBuffer[pos++] = BBuffer[match++];
    143         } else {
    144             /* Literal (noise) */
    145             size_t const length = FUZ_RANDLENGTH;
    146             size_t const d = MIN(pos+length, bufferSize);
    147             while (pos < d) BBuffer[pos++] = (BYTE)(FUZ_rand(seed) >> 5);
    148         }
    149     }
    150 }
    151 
    152 
    153 #define MAX_NB_BUFF_I134 150
    154 #define BLOCKSIZE_I134   (32 MB)
    155 /*! FUZ_AddressOverflow() :
    156 *   Aggressively pushes memory allocation limits,
    157 *   and generates patterns which create address space overflow.
    158 *   only possible in 32-bits mode */
    159 static int FUZ_AddressOverflow(void)
    160 {
    161     char* buffers[MAX_NB_BUFF_I134+1];
    162     int nbBuff=0;
    163     int highAddress = 0;
    164 
    165     DISPLAY("Overflow tests : ");
    166 
    167     /* Only possible in 32-bits */
    168     if (sizeof(void*)==8) {
    169         DISPLAY("64 bits mode : no overflow \n");
    170         fflush(stdout);
    171         return 0;
    172     }
    173 
    174     buffers[0] = (char*)malloc(BLOCKSIZE_I134);
    175     buffers[1] = (char*)malloc(BLOCKSIZE_I134);
    176     if ((!buffers[0]) || (!buffers[1])) {
    177         free(buffers[0]); free(buffers[1]);
    178         DISPLAY("not enough memory for tests \n");
    179         return 0;
    180     }
    181 
    182     for (nbBuff=2; nbBuff < MAX_NB_BUFF_I134; nbBuff++) {
    183         DISPLAY("%3i \b\b\b\b", nbBuff); fflush(stdout);
    184         buffers[nbBuff] = (char*)malloc(BLOCKSIZE_I134);
    185         if (buffers[nbBuff]==NULL) goto _endOfTests;
    186 
    187         if (((uintptr_t)buffers[nbBuff] > (uintptr_t)0x80000000) && (!highAddress)) {
    188             DISPLAY("high address detected : ");
    189             fflush(stdout);
    190             highAddress=1;
    191         }
    192 
    193         {   size_t const sizeToGenerateOverflow = (size_t)(- ((uintptr_t)buffers[nbBuff-1]) + 512);
    194             unsigned const nbOf255 = (unsigned)((sizeToGenerateOverflow / 255) + 1);
    195             char* const input = buffers[nbBuff-1];
    196             char* output = buffers[nbBuff];
    197             int r;
    198             input[0] = (char)0xF0;   /* Literal length overflow */
    199             input[1] = (char)0xFF;
    200             input[2] = (char)0xFF;
    201             input[3] = (char)0xFF;
    202             { unsigned u; for(u = 4; u <= nbOf255+4; u++) input[u] = (char)0xff; }
    203             r = LZ4_decompress_safe(input, output, nbOf255+64, BLOCKSIZE_I134);
    204             if (r>0) { DISPLAY("LZ4_decompress_safe = %i \n", r); goto _overflowError; }
    205             input[0] = (char)0x1F;   /* Match length overflow */
    206             input[1] = (char)0x01;
    207             input[2] = (char)0x01;
    208             input[3] = (char)0x00;
    209             r = LZ4_decompress_safe(input, output, nbOf255+64, BLOCKSIZE_I134);
    210             if (r>0) { DISPLAY("LZ4_decompress_safe = %i \n", r); goto _overflowError; }
    211 
    212             output = buffers[nbBuff-2];   /* Reverse in/out pointer order */
    213             input[0] = (char)0xF0;   /* Literal length overflow */
    214             input[1] = (char)0xFF;
    215             input[2] = (char)0xFF;
    216             input[3] = (char)0xFF;
    217             r = LZ4_decompress_safe(input, output, nbOf255+64, BLOCKSIZE_I134);
    218             if (r>0) goto _overflowError;
    219             input[0] = (char)0x1F;   /* Match length overflow */
    220             input[1] = (char)0x01;
    221             input[2] = (char)0x01;
    222             input[3] = (char)0x00;
    223             r = LZ4_decompress_safe(input, output, nbOf255+64, BLOCKSIZE_I134);
    224             if (r>0) goto _overflowError;
    225         }
    226     }
    227 
    228     nbBuff++;
    229 _endOfTests:
    230     { int i; for (i=0 ; i<nbBuff; i++) free(buffers[i]); }
    231     if (!highAddress) DISPLAY("high address not possible \n");
    232     else DISPLAY("all overflows correctly detected \n");
    233     return 0;
    234 
    235 _overflowError:
    236     DISPLAY("Address space overflow error !! \n");
    237     exit(1);
    238 }
    239 
    240 
    241 /*! FUZ_findDiff() :
    242 *   find the first different byte between buff1 and buff2.
    243 *   presumes buff1 != buff2.
    244 *   presumes a difference exists before end of either buffer.
    245 *   Typically invoked after a checksum mismatch.
    246 */
    247 static void FUZ_findDiff(const void* buff1, const void* buff2)
    248 {
    249     const BYTE* const b1 = (const BYTE*)buff1;
    250     const BYTE* const b2 = (const BYTE*)buff2;
    251     size_t u = 0;
    252     while (b1[u]==b2[u]) u++;
    253     DISPLAY("Wrong Byte at position %u \n", (unsigned)u);
    254 }
    255 
    256 
    257 static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double compressibility, U32 duration_s)
    258 {
    259     unsigned long long bytes = 0;
    260     unsigned long long cbytes = 0;
    261     unsigned long long hcbytes = 0;
    262     unsigned long long ccbytes = 0;
    263     void* const CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH);
    264     size_t const compressedBufferSize = LZ4_compressBound(FUZ_MAX_BLOCK_SIZE);
    265     char* const compressedBuffer = (char*)malloc(compressedBufferSize);
    266     char* const decodedBuffer = (char*)malloc(FUZ_MAX_DICT_SIZE + FUZ_MAX_BLOCK_SIZE);
    267     void* const stateLZ4   = malloc(LZ4_sizeofState());
    268     void* const stateLZ4HC = malloc(LZ4_sizeofStateHC());
    269     LZ4_stream_t LZ4dict;
    270     LZ4_streamHC_t LZ4dictHC;
    271     U32 coreRandState = seed;
    272     clock_t const clockStart = clock();
    273     clock_t const clockDuration = (clock_t)duration_s * CLOCKS_PER_SEC;
    274     int result = 0;
    275     unsigned cycleNb;
    276 
    277 #   define FUZ_CHECKTEST(cond, ...) if (cond) { printf("Test %u : ", testNb); printf(__VA_ARGS__); \
    278                                                 printf(" (seed %u, cycle %u) \n", seed, cycleNb); goto _output_error; }
    279 #   define FUZ_DISPLAYTEST          { testNb++; g_displayLevel>=4 ? printf("%2u\b\b", testNb), fflush(stdout) : 0; }
    280 
    281 
    282     /* init */
    283     if(!CNBuffer || !compressedBuffer || !decodedBuffer) {
    284         DISPLAY("Not enough memory to start fuzzer tests");
    285         goto _output_error;
    286     }
    287     memset(&LZ4dict, 0, sizeof(LZ4dict));
    288     {   U32 randState = coreRandState ^ PRIME3;
    289         FUZ_fillCompressibleNoiseBuffer(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, &randState);
    290     }
    291 
    292     /* move to startCycle */
    293     for (cycleNb = 0; cycleNb < startCycle; cycleNb++)
    294         (void) FUZ_rand(&coreRandState);   /* sync coreRandState */
    295 
    296     /* Main test loop */
    297     for (cycleNb = startCycle;
    298         (cycleNb < nbCycles) || (FUZ_GetClockSpan(clockStart) < clockDuration);
    299         cycleNb++) {
    300         U32 testNb = 0;
    301         U32 randState = FUZ_rand(&coreRandState) ^ PRIME3;
    302         int const blockSize  = (FUZ_rand(&randState) % (FUZ_MAX_BLOCK_SIZE-1)) + 1;
    303         int const blockStart = FUZ_rand(&randState) % (COMPRESSIBLE_NOISE_LENGTH - blockSize);
    304         int const dictSizeRand = FUZ_rand(&randState) % FUZ_MAX_DICT_SIZE;
    305         int const dictSize = MIN(dictSizeRand, blockStart);
    306         int const compressionLevel = FUZ_rand(&randState) % (LZ4HC_CLEVEL_MAX+1);
    307         char* const block = ((char*)CNBuffer) + blockStart;
    308         const char* dict = block - dictSize;
    309         int compressedSize, HCcompressedSize;
    310         int blockContinueCompressedSize;
    311         U32 const crcOrig = XXH32(block, blockSize, 0);
    312         U32 crcCheck;
    313         int ret;
    314 
    315         FUZ_displayUpdate(cycleNb);
    316 
    317         /* Compression tests */
    318 
    319         /* Test compression destSize */
    320         FUZ_DISPLAYTEST;
    321         {   int srcSize = blockSize;
    322             int const targetSize = srcSize * ((FUZ_rand(&randState) & 127)+1) >> 7;
    323             char endCheck = FUZ_rand(&randState) & 255;
    324             compressedBuffer[targetSize] = endCheck;
    325             ret = LZ4_compress_destSize(block, compressedBuffer, &srcSize, targetSize);
    326             FUZ_CHECKTEST(ret > targetSize, "LZ4_compress_destSize() result larger than dst buffer !");
    327             FUZ_CHECKTEST(compressedBuffer[targetSize] != endCheck, "LZ4_compress_destSize() overwrite dst buffer !");
    328             FUZ_CHECKTEST(srcSize > blockSize, "LZ4_compress_destSize() fed more than src buffer !");
    329             DISPLAYLEVEL(5, "destSize : %7i/%7i; content%7i/%7i ", ret, targetSize, srcSize, blockSize);
    330             if (targetSize>0) {
    331                 /* check correctness */
    332                 U32 const crcBase = XXH32(block, srcSize, 0);
    333                 char const canary = FUZ_rand(&randState) & 255;
    334                 FUZ_CHECKTEST((ret==0), "LZ4_compress_destSize() compression failed");
    335                 FUZ_DISPLAYTEST;
    336                 compressedSize = ret;
    337                 decodedBuffer[srcSize] = canary;
    338                 ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize, srcSize);
    339                 FUZ_CHECKTEST(ret<0, "LZ4_decompress_safe() failed on data compressed by LZ4_compress_destSize");
    340                 FUZ_CHECKTEST(ret!=srcSize, "LZ4_decompress_safe() failed : did not fully decompressed data");
    341                 FUZ_CHECKTEST(decodedBuffer[srcSize] != canary, "LZ4_decompress_safe() overwrite dst buffer !");
    342                 { U32 const crcDec = XXH32(decodedBuffer, srcSize, 0);
    343                   FUZ_CHECKTEST(crcDec!=crcBase, "LZ4_decompress_safe() corrupted decoded data"); }
    344 
    345                 DISPLAYLEVEL(5, " OK \n");
    346             }
    347             else
    348                 DISPLAYLEVEL(5, " \n");
    349         }
    350 
    351         /* Test compression HC */
    352         FUZ_DISPLAYTEST;
    353         ret = LZ4_compress_HC(block, compressedBuffer, blockSize, (int)compressedBufferSize, compressionLevel);
    354         FUZ_CHECKTEST(ret==0, "LZ4_compressHC() failed");
    355         HCcompressedSize = ret;
    356 
    357         /* Test compression HC using external state */
    358         FUZ_DISPLAYTEST;
    359         ret = LZ4_compress_HC_extStateHC(stateLZ4HC, block, compressedBuffer, blockSize, (int)compressedBufferSize, compressionLevel);
    360         FUZ_CHECKTEST(ret==0, "LZ4_compressHC_withStateHC() failed");
    361 
    362         /* Test compression using external state */
    363         FUZ_DISPLAYTEST;
    364         ret = LZ4_compress_fast_extState(stateLZ4, block, compressedBuffer, blockSize, (int)compressedBufferSize, 8);
    365         FUZ_CHECKTEST(ret==0, "LZ4_compress_withState() failed");
    366 
    367         /* Test compression */
    368         FUZ_DISPLAYTEST;
    369         ret = LZ4_compress_default(block, compressedBuffer, blockSize, (int)compressedBufferSize);
    370         FUZ_CHECKTEST(ret==0, "LZ4_compress() failed");
    371         compressedSize = ret;
    372 
    373         /* Decompression tests */
    374 
    375         /* Test decoding with output size being exactly what's necessary => must work */
    376         FUZ_DISPLAYTEST;
    377         ret = LZ4_decompress_fast(compressedBuffer, decodedBuffer, blockSize);
    378         FUZ_CHECKTEST(ret<0, "LZ4_decompress_fast failed despite correct space");
    379         FUZ_CHECKTEST(ret!=compressedSize, "LZ4_decompress_fast failed : did not fully read compressed data");
    380         crcCheck = XXH32(decodedBuffer, blockSize, 0);
    381         FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_fast corrupted decoded data");
    382 
    383         /* Test decoding with one byte missing => must fail */
    384         FUZ_DISPLAYTEST;
    385         decodedBuffer[blockSize-1] = 0;
    386         ret = LZ4_decompress_fast(compressedBuffer, decodedBuffer, blockSize-1);
    387         FUZ_CHECKTEST(ret>=0, "LZ4_decompress_fast should have failed, due to Output Size being too small");
    388         FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_fast overrun specified output buffer");
    389 
    390         /* Test decoding with one byte too much => must fail */
    391         FUZ_DISPLAYTEST;
    392         ret = LZ4_decompress_fast(compressedBuffer, decodedBuffer, blockSize+1);
    393         FUZ_CHECKTEST(ret>=0, "LZ4_decompress_fast should have failed, due to Output Size being too large");
    394 
    395         /* Test decoding with output size exactly what's necessary => must work */
    396         FUZ_DISPLAYTEST;
    397         decodedBuffer[blockSize] = 0;
    398         ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize, blockSize);
    399         FUZ_CHECKTEST(ret<0, "LZ4_decompress_safe failed despite sufficient space");
    400         FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe did not regenerate original data");
    401         FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe overrun specified output buffer size");
    402         crcCheck = XXH32(decodedBuffer, blockSize, 0);
    403         FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe corrupted decoded data");
    404 
    405         // Test decoding with more than enough output size => must work
    406         FUZ_DISPLAYTEST;
    407         decodedBuffer[blockSize] = 0;
    408         decodedBuffer[blockSize+1] = 0;
    409         ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize, blockSize+1);
    410         FUZ_CHECKTEST(ret<0, "LZ4_decompress_safe failed despite amply sufficient space");
    411         FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe did not regenerate original data");
    412         //FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe wrote more than (unknown) target size");   // well, is that an issue ?
    413         FUZ_CHECKTEST(decodedBuffer[blockSize+1], "LZ4_decompress_safe overrun specified output buffer size");
    414         crcCheck = XXH32(decodedBuffer, blockSize, 0);
    415         FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe corrupted decoded data");
    416 
    417         // Test decoding with output size being one byte too short => must fail
    418         FUZ_DISPLAYTEST;
    419         decodedBuffer[blockSize-1] = 0;
    420         ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize, blockSize-1);
    421         FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe should have failed, due to Output Size being one byte too short");
    422         FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_safe overrun specified output buffer size");
    423 
    424         // Test decoding with output size being 10 bytes too short => must fail
    425         FUZ_DISPLAYTEST;
    426         if (blockSize>10)
    427         {
    428             decodedBuffer[blockSize-10] = 0;
    429             ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize, blockSize-10);
    430             FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe should have failed, due to Output Size being 10 bytes too short");
    431             FUZ_CHECKTEST(decodedBuffer[blockSize-10], "LZ4_decompress_safe overrun specified output buffer size");
    432         }
    433 
    434         // Test decoding with input size being one byte too short => must fail
    435         FUZ_DISPLAYTEST;
    436         ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize-1, blockSize);
    437         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);
    438 
    439         // Test decoding with input size being one byte too large => must fail
    440         FUZ_DISPLAYTEST;
    441         decodedBuffer[blockSize] = 0;
    442         ret = LZ4_decompress_safe(compressedBuffer, decodedBuffer, compressedSize+1, blockSize);
    443         FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe should have failed, due to input size being too large");
    444         FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe overrun specified output buffer size");
    445 
    446         // Test partial decoding with target output size being max/2 => must work
    447         FUZ_DISPLAYTEST;
    448         ret = LZ4_decompress_safe_partial(compressedBuffer, decodedBuffer, compressedSize, blockSize/2, blockSize);
    449         FUZ_CHECKTEST(ret<0, "LZ4_decompress_safe_partial failed despite sufficient space");
    450 
    451         // Test partial decoding with target output size being just below max => must work
    452         FUZ_DISPLAYTEST;
    453         ret = LZ4_decompress_safe_partial(compressedBuffer, decodedBuffer, compressedSize, blockSize-3, blockSize);
    454         FUZ_CHECKTEST(ret<0, "LZ4_decompress_safe_partial failed despite sufficient space");
    455 
    456         /* Test Compression with limited output size */
    457 
    458         /* Test compression with output size being exactly what's necessary (should work) */
    459         FUZ_DISPLAYTEST;
    460         ret = LZ4_compress_default(block, compressedBuffer, blockSize, compressedSize);
    461         FUZ_CHECKTEST(ret==0, "LZ4_compress_limitedOutput() failed despite sufficient space");
    462 
    463         /* Test compression with output size being exactly what's necessary and external state (should work) */
    464         FUZ_DISPLAYTEST;
    465         ret = LZ4_compress_fast_extState(stateLZ4, block, compressedBuffer, blockSize, compressedSize, 1);
    466         FUZ_CHECKTEST(ret==0, "LZ4_compress_limitedOutput_withState() failed despite sufficient space");
    467 
    468         /* Test HC compression with output size being exactly what's necessary (should work) */
    469         FUZ_DISPLAYTEST;
    470         ret = LZ4_compress_HC(block, compressedBuffer, blockSize, HCcompressedSize, compressionLevel);
    471         FUZ_CHECKTEST(ret==0, "LZ4_compressHC_limitedOutput() failed despite sufficient space");
    472 
    473         /* Test HC compression with output size being exactly what's necessary (should work) */
    474         FUZ_DISPLAYTEST;
    475         ret = LZ4_compress_HC_extStateHC(stateLZ4HC, block, compressedBuffer, blockSize, HCcompressedSize, compressionLevel);
    476         FUZ_CHECKTEST(ret==0, "LZ4_compressHC_limitedOutput_withStateHC() failed despite sufficient space");
    477 
    478         /* Test compression with missing bytes into output buffer => must fail */
    479         FUZ_DISPLAYTEST;
    480         {   int missingBytes = (FUZ_rand(&randState) % 0x3F) + 1;
    481             if (missingBytes >= compressedSize) missingBytes = compressedSize-1;
    482             missingBytes += !missingBytes;   /* avoid special case missingBytes==0 */
    483             compressedBuffer[compressedSize-missingBytes] = 0;
    484             ret = LZ4_compress_default(block, compressedBuffer, blockSize, compressedSize-missingBytes);
    485             FUZ_CHECKTEST(ret, "LZ4_compress_limitedOutput should have failed (output buffer too small by %i byte)", missingBytes);
    486             FUZ_CHECKTEST(compressedBuffer[compressedSize-missingBytes], "LZ4_compress_limitedOutput overran output buffer ! (%i missingBytes)", missingBytes)
    487         }
    488 
    489         /* Test HC compression with missing bytes into output buffer => must fail */
    490         FUZ_DISPLAYTEST;
    491         {   int missingBytes = (FUZ_rand(&randState) % 0x3F) + 1;
    492             if (missingBytes >= HCcompressedSize) missingBytes = HCcompressedSize-1;
    493             missingBytes += !missingBytes;   /* avoid special case missingBytes==0 */
    494             compressedBuffer[HCcompressedSize-missingBytes] = 0;
    495             ret = LZ4_compress_HC(block, compressedBuffer, blockSize, HCcompressedSize-missingBytes, compressionLevel);
    496             FUZ_CHECKTEST(ret, "LZ4_compressHC_limitedOutput should have failed (output buffer too small by %i byte)", missingBytes);
    497             FUZ_CHECKTEST(compressedBuffer[HCcompressedSize-missingBytes], "LZ4_compressHC_limitedOutput overran output buffer ! (%i missingBytes)", missingBytes)
    498         }
    499 
    500 
    501         /*-******************/
    502         /* Dictionary tests */
    503         /*-******************/
    504 
    505         /* Compress using dictionary */
    506         FUZ_DISPLAYTEST;
    507         {   LZ4_stream_t LZ4_stream;
    508             LZ4_resetStream(&LZ4_stream);
    509             LZ4_compress_fast_continue (&LZ4_stream, dict, compressedBuffer, dictSize, (int)compressedBufferSize, 1);   /* Just to fill hash tables */
    510             blockContinueCompressedSize = LZ4_compress_fast_continue (&LZ4_stream, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1);
    511             FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_continue failed");
    512         }
    513 
    514         /* Decompress with dictionary as prefix */
    515         FUZ_DISPLAYTEST;
    516         memcpy(decodedBuffer, dict, dictSize);
    517         ret = LZ4_decompress_fast_usingDict(compressedBuffer, decodedBuffer+dictSize, blockSize, decodedBuffer, dictSize);
    518         FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_decompress_fast_withPrefix64k did not read all compressed block input");
    519         crcCheck = XXH32(decodedBuffer+dictSize, blockSize, 0);
    520         if (crcCheck!=crcOrig) {
    521             int i=0;
    522             while (block[i]==decodedBuffer[i]) i++;
    523             printf("Wrong Byte at position %i/%i\n", i, blockSize);
    524 
    525         }
    526         FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_fast_withPrefix64k corrupted decoded data (dict %i)", dictSize);
    527 
    528         FUZ_DISPLAYTEST;
    529         ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer+dictSize, blockContinueCompressedSize, blockSize, decodedBuffer, dictSize);
    530         FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data");
    531         crcCheck = XXH32(decodedBuffer+dictSize, blockSize, 0);
    532         FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_usingDict corrupted decoded data");
    533 
    534         /* Compress using External dictionary */
    535         FUZ_DISPLAYTEST;
    536         dict -= (FUZ_rand(&randState) & 0xF) + 1;   /* Separation, so it is an ExtDict */
    537         if (dict < (char*)CNBuffer) dict = (char*)CNBuffer;
    538         LZ4_loadDict(&LZ4dict, dict, dictSize);
    539         blockContinueCompressedSize = LZ4_compress_fast_continue(&LZ4dict, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1);
    540         FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compress_continue failed");
    541 
    542         FUZ_DISPLAYTEST;
    543         LZ4_loadDict(&LZ4dict, dict, dictSize);
    544         ret = LZ4_compress_fast_continue(&LZ4dict, block, compressedBuffer, blockSize, blockContinueCompressedSize-1, 1);
    545         FUZ_CHECKTEST(ret>0, "LZ4_compress_limitedOutput_continue using ExtDict should fail : one missing byte for output buffer : %i written, %i buffer", ret, blockContinueCompressedSize);
    546 
    547         FUZ_DISPLAYTEST;
    548         LZ4_loadDict(&LZ4dict, dict, dictSize);
    549         ret = LZ4_compress_fast_continue(&LZ4dict, block, compressedBuffer, blockSize, blockContinueCompressedSize, 1);
    550         FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_limitedOutput_compressed size is different (%i != %i)", ret, blockContinueCompressedSize);
    551         FUZ_CHECKTEST(ret<=0, "LZ4_compress_limitedOutput_continue should work : enough size available within output buffer");
    552 
    553         /* Decompress with dictionary as external */
    554         FUZ_DISPLAYTEST;
    555         decodedBuffer[blockSize] = 0;
    556         ret = LZ4_decompress_fast_usingDict(compressedBuffer, decodedBuffer, blockSize, dict, dictSize);
    557         FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_decompress_fast_usingDict did not read all compressed block input");
    558         FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_fast_usingDict overrun specified output buffer size");
    559         crcCheck = XXH32(decodedBuffer, blockSize, 0);
    560         if (crcCheck!=crcOrig) FUZ_findDiff(block, decodedBuffer);
    561         FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_fast_usingDict corrupted decoded data (dict %i)", dictSize);
    562 
    563         FUZ_DISPLAYTEST;
    564         decodedBuffer[blockSize] = 0;
    565         ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize, dict, dictSize);
    566         FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data");
    567         FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size");
    568         crcCheck = XXH32(decodedBuffer, blockSize, 0);
    569         FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_usingDict corrupted decoded data");
    570 
    571         FUZ_DISPLAYTEST;
    572         decodedBuffer[blockSize-1] = 0;
    573         ret = LZ4_decompress_fast_usingDict(compressedBuffer, decodedBuffer, blockSize-1, dict, dictSize);
    574         FUZ_CHECKTEST(ret>=0, "LZ4_decompress_fast_usingDict should have failed : wrong original size (-1 byte)");
    575         FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_fast_usingDict overrun specified output buffer size");
    576 
    577         FUZ_DISPLAYTEST;
    578         decodedBuffer[blockSize-1] = 0;
    579         ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize-1, dict, dictSize);
    580         FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe_usingDict should have failed : not enough output size (-1 byte)");
    581         FUZ_CHECKTEST(decodedBuffer[blockSize-1], "LZ4_decompress_safe_usingDict overrun specified output buffer size");
    582 
    583         FUZ_DISPLAYTEST;
    584         {   U32 const missingBytes = (FUZ_rand(&randState) & 0xF) + 2;
    585             if ((U32)blockSize > missingBytes) {
    586                 decodedBuffer[blockSize-missingBytes] = 0;
    587                 ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize-missingBytes, dict, dictSize);
    588                 FUZ_CHECKTEST(ret>=0, "LZ4_decompress_safe_usingDict should have failed : output buffer too small (-%u byte)", missingBytes);
    589                 FUZ_CHECKTEST(decodedBuffer[blockSize-missingBytes], "LZ4_decompress_safe_usingDict overrun specified output buffer size (-%u byte) (blockSize=%i)", missingBytes, blockSize);
    590         }   }
    591 
    592         /* Compress HC using External dictionary */
    593         FUZ_DISPLAYTEST;
    594         dict -= (FUZ_rand(&randState) & 7);    /* even bigger separation */
    595         if (dict < (char*)CNBuffer) dict = (char*)CNBuffer;
    596         LZ4_resetStreamHC (&LZ4dictHC, compressionLevel);
    597         LZ4_loadDictHC(&LZ4dictHC, dict, dictSize);
    598         blockContinueCompressedSize = LZ4_compress_HC_continue(&LZ4dictHC, block, compressedBuffer, blockSize, (int)compressedBufferSize);
    599         FUZ_CHECKTEST(blockContinueCompressedSize==0, "LZ4_compressHC_continue failed");
    600 
    601         FUZ_DISPLAYTEST;
    602         LZ4_loadDictHC(&LZ4dictHC, dict, dictSize);
    603         ret = LZ4_compress_HC_continue(&LZ4dictHC, block, compressedBuffer, blockSize, blockContinueCompressedSize-1);
    604         FUZ_CHECKTEST(ret>0, "LZ4_compressHC_limitedOutput_continue using ExtDict should fail : one missing byte for output buffer (%i != %i)", ret, blockContinueCompressedSize);
    605 
    606         FUZ_DISPLAYTEST;
    607         LZ4_loadDictHC(&LZ4dictHC, dict, dictSize);
    608         ret = LZ4_compress_HC_continue(&LZ4dictHC, block, compressedBuffer, blockSize, blockContinueCompressedSize);
    609         FUZ_CHECKTEST(ret!=blockContinueCompressedSize, "LZ4_compress_limitedOutput_compressed size is different (%i != %i)", ret, blockContinueCompressedSize);
    610         FUZ_CHECKTEST(ret<=0, "LZ4_compress_limitedOutput_continue should work : enough size available within output buffer");
    611 
    612         FUZ_DISPLAYTEST;
    613         decodedBuffer[blockSize] = 0;
    614         ret = LZ4_decompress_safe_usingDict(compressedBuffer, decodedBuffer, blockContinueCompressedSize, blockSize, dict, dictSize);
    615         FUZ_CHECKTEST(ret!=blockSize, "LZ4_decompress_safe_usingDict did not regenerate original data");
    616         FUZ_CHECKTEST(decodedBuffer[blockSize], "LZ4_decompress_safe_usingDict overrun specified output buffer size")
    617             crcCheck = XXH32(decodedBuffer, blockSize, 0);
    618         if (crcCheck!=crcOrig)
    619             FUZ_findDiff(block, decodedBuffer);
    620         FUZ_CHECKTEST(crcCheck!=crcOrig, "LZ4_decompress_safe_usingDict corrupted decoded data");
    621 
    622         /* ***** End of tests *** */
    623         /* Fill stats */
    624         bytes += blockSize;
    625         cbytes += compressedSize;
    626         hcbytes += HCcompressedSize;
    627         ccbytes += blockContinueCompressedSize;
    628     }
    629 
    630     if (nbCycles<=1) nbCycles = cycleNb;   /* end by time */
    631     bytes += !bytes;   /* avoid division by 0 */
    632     printf("\r%7u /%7u   - ", cycleNb, nbCycles);
    633     printf("all tests completed successfully \n");
    634     printf("compression ratio: %0.3f%%\n", (double)cbytes/bytes*100);
    635     printf("HC compression ratio: %0.3f%%\n", (double)hcbytes/bytes*100);
    636     printf("ratio with dict: %0.3f%%\n", (double)ccbytes/bytes*100);
    637 
    638     /* release memory */
    639     {
    640 _exit:
    641         free(CNBuffer);
    642         free(compressedBuffer);
    643         free(decodedBuffer);
    644         free(stateLZ4);
    645         free(stateLZ4HC);
    646         return result;
    647 
    648 _output_error:
    649         result = 1;
    650         goto _exit;
    651     }
    652 }
    653 
    654 
    655 #define testInputSize (192 KB)
    656 #define testCompressedSize (128 KB)
    657 #define ringBufferSize (8 KB)
    658 
    659 static void FUZ_unitTests(int compressionLevel)
    660 {
    661     const unsigned testNb = 0;
    662     const unsigned seed   = 0;
    663     const unsigned cycleNb= 0;
    664     char testInput[testInputSize];
    665     char testCompressed[testCompressedSize];
    666     char testVerify[testInputSize];
    667     char ringBuffer[ringBufferSize];
    668     U32 randState = 1;
    669 
    670     /* Init */
    671     FUZ_fillCompressibleNoiseBuffer(testInput, testInputSize, 0.50, &randState);
    672 
    673     /* 32-bits address space overflow test */
    674     FUZ_AddressOverflow();
    675 
    676     /* LZ4 streaming tests */
    677     {   LZ4_stream_t* statePtr;
    678         LZ4_stream_t  streamingState;
    679         U64 crcOrig;
    680         int result;
    681 
    682         /* Allocation test */
    683         statePtr = LZ4_createStream();
    684         FUZ_CHECKTEST(statePtr==NULL, "LZ4_createStream() allocation failed");
    685         LZ4_freeStream(statePtr);
    686 
    687         /* simple compression test */
    688         crcOrig = XXH64(testInput, testCompressedSize, 0);
    689         LZ4_resetStream(&streamingState);
    690         result = LZ4_compress_fast_continue(&streamingState, testInput, testCompressed, testCompressedSize, testCompressedSize-1, 1);
    691         FUZ_CHECKTEST(result==0, "LZ4_compress_limitedOutput_continue() compression failed");
    692 
    693         result = LZ4_decompress_safe(testCompressed, testVerify, result, testCompressedSize);
    694         FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed");
    695         { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0);
    696           FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); }
    697 
    698         /* ring buffer test */
    699         {   XXH64_state_t xxhOrig;
    700             XXH64_state_t xxhNew;
    701             LZ4_streamDecode_t decodeState;
    702             const U32 maxMessageSizeLog = 10;
    703             const U32 maxMessageSizeMask = (1<<maxMessageSizeLog) - 1;
    704             U32 messageSize = (FUZ_rand(&randState) & maxMessageSizeMask) + 1;
    705             U32 iNext = 0;
    706             U32 rNext = 0;
    707             U32 dNext = 0;
    708             const U32 dBufferSize = ringBufferSize + maxMessageSizeMask;
    709 
    710             XXH64_reset(&xxhOrig, 0);
    711             XXH64_reset(&xxhNew, 0);
    712             LZ4_resetStream(&streamingState);
    713             LZ4_setStreamDecode(&decodeState, NULL, 0);
    714 
    715             while (iNext + messageSize < testCompressedSize) {
    716                 XXH64_update(&xxhOrig, testInput + iNext, messageSize);
    717                 crcOrig = XXH64_digest(&xxhOrig);
    718 
    719                 memcpy (ringBuffer + rNext, testInput + iNext, messageSize);
    720                 result = LZ4_compress_fast_continue(&streamingState, ringBuffer + rNext, testCompressed, messageSize, testCompressedSize-ringBufferSize, 1);
    721                 FUZ_CHECKTEST(result==0, "LZ4_compress_limitedOutput_continue() compression failed");
    722 
    723                 result = LZ4_decompress_safe_continue(&decodeState, testCompressed, testVerify + dNext, result, messageSize);
    724                 FUZ_CHECKTEST(result!=(int)messageSize, "ringBuffer : LZ4_decompress_safe() test failed");
    725 
    726                 XXH64_update(&xxhNew, testVerify + dNext, messageSize);
    727                 { U64 const crcNew = XXH64_digest(&xxhNew);
    728                   FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); }
    729 
    730                 /* prepare next message */
    731                 iNext += messageSize;
    732                 rNext += messageSize;
    733                 dNext += messageSize;
    734                 messageSize = (FUZ_rand(&randState) & maxMessageSizeMask) + 1;
    735                 if (rNext + messageSize > ringBufferSize) rNext = 0;
    736                 if (dNext + messageSize > dBufferSize) dNext = 0;
    737             }
    738         }
    739     }
    740 
    741     /* LZ4 HC streaming tests */
    742     {   LZ4_streamHC_t* sp;
    743         LZ4_streamHC_t  sHC;
    744         U64 crcOrig;
    745         int result;
    746 
    747         /* Allocation test */
    748         sp = LZ4_createStreamHC();
    749         FUZ_CHECKTEST(sp==NULL, "LZ4_createStreamHC() allocation failed");
    750         LZ4_freeStreamHC(sp);
    751 
    752         /* simple HC compression test */
    753         crcOrig = XXH64(testInput, testCompressedSize, 0);
    754         LZ4_resetStreamHC(&sHC, compressionLevel);
    755         result = LZ4_compress_HC_continue(&sHC, testInput, testCompressed, testCompressedSize, testCompressedSize-1);
    756         FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() compression failed");
    757 
    758         result = LZ4_decompress_safe(testCompressed, testVerify, result, testCompressedSize);
    759         FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() decompression failed");
    760         { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0);
    761           FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); }
    762 
    763         /* simple dictionary HC compression test */
    764         crcOrig = XXH64(testInput + 64 KB, testCompressedSize, 0);
    765         LZ4_resetStreamHC(&sHC, compressionLevel);
    766         LZ4_loadDictHC(&sHC, testInput, 64 KB);
    767         result = LZ4_compress_HC_continue(&sHC, testInput + 64 KB, testCompressed, testCompressedSize, testCompressedSize-1);
    768         FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result);
    769 
    770         result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result, testCompressedSize, testInput, 64 KB);
    771         FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe() simple dictionary decompression test failed");
    772         { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0);
    773           FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() simple dictionary decompression test : corruption"); }
    774 
    775         /* multiple HC compression test with dictionary */
    776         {   int result1, result2;
    777             int segSize = testCompressedSize / 2;
    778             crcOrig = XXH64(testInput + segSize, testCompressedSize, 0);
    779             LZ4_resetStreamHC(&sHC, compressionLevel);
    780             LZ4_loadDictHC(&sHC, testInput, segSize);
    781             result1 = LZ4_compress_HC_continue(&sHC, testInput + segSize, testCompressed, segSize, segSize -1);
    782             FUZ_CHECKTEST(result1==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result1);
    783             result2 = LZ4_compress_HC_continue(&sHC, testInput + 2*segSize, testCompressed+result1, segSize, segSize-1);
    784             FUZ_CHECKTEST(result2==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result2);
    785 
    786             result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result1, segSize, testInput, segSize);
    787             FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe() dictionary decompression part 1 failed");
    788             result = LZ4_decompress_safe_usingDict(testCompressed+result1, testVerify+segSize, result2, segSize, testInput, 2*segSize);
    789             FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe() dictionary decompression part 2 failed");
    790             { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0);
    791               FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() dictionary decompression corruption"); }
    792         }
    793 
    794         /* remote dictionary HC compression test */
    795         crcOrig = XXH64(testInput + 64 KB, testCompressedSize, 0);
    796         LZ4_resetStreamHC(&sHC, compressionLevel);
    797         LZ4_loadDictHC(&sHC, testInput, 32 KB);
    798         result = LZ4_compress_HC_continue(&sHC, testInput + 64 KB, testCompressed, testCompressedSize, testCompressedSize-1);
    799         FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() remote dictionary failed : result = %i", result);
    800 
    801         result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result, testCompressedSize, testInput, 32 KB);
    802         FUZ_CHECKTEST(result!=(int)testCompressedSize, "LZ4_decompress_safe_usingDict() decompression failed following remote dictionary HC compression test");
    803         { U64 const crcNew = XXH64(testVerify, testCompressedSize, 0);
    804           FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_usingDict() decompression corruption"); }
    805 
    806         /* multiple HC compression with ext. dictionary */
    807         {   XXH64_state_t crcOrigState;
    808             XXH64_state_t crcNewState;
    809             const char* dict = testInput + 3;
    810             int dictSize = (FUZ_rand(&randState) & 8191);
    811             char* dst = testVerify;
    812 
    813             size_t segStart = dictSize + 7;
    814             int segSize = (FUZ_rand(&randState) & 8191);
    815             int segNb = 1;
    816 
    817             LZ4_resetStreamHC(&sHC, compressionLevel);
    818             LZ4_loadDictHC(&sHC, dict, dictSize);
    819 
    820             XXH64_reset(&crcOrigState, 0);
    821             XXH64_reset(&crcNewState, 0);
    822 
    823             while (segStart + segSize < testInputSize) {
    824                 XXH64_update(&crcOrigState, testInput + segStart, segSize);
    825                 crcOrig = XXH64_digest(&crcOrigState);
    826                 result = LZ4_compress_HC_continue(&sHC, testInput + segStart, testCompressed, segSize, LZ4_compressBound(segSize));
    827                 FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result);
    828 
    829                 result = LZ4_decompress_safe_usingDict(testCompressed, dst, result, segSize, dict, dictSize);
    830                 FUZ_CHECKTEST(result!=segSize, "LZ4_decompress_safe_usingDict() dictionary decompression part %i failed", segNb);
    831                 XXH64_update(&crcNewState, dst, segSize);
    832                 {   U64 const crcNew = XXH64_digest(&crcNewState);
    833                     if (crcOrig != crcNew) FUZ_findDiff(dst, testInput+segStart);
    834                     FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_usingDict() part %i corruption", segNb);
    835                 }
    836 
    837                 dict = dst;
    838                 //dict = testInput + segStart;
    839                 dictSize = segSize;
    840 
    841                 dst += segSize + 1;
    842                 segNb ++;
    843 
    844                 segStart += segSize + (FUZ_rand(&randState) & 0xF) + 1;
    845                 segSize = (FUZ_rand(&randState) & 8191);
    846             }
    847         }
    848 
    849         /* ring buffer test */
    850         {   XXH64_state_t xxhOrig;
    851             XXH64_state_t xxhNew;
    852             LZ4_streamDecode_t decodeState;
    853             const U32 maxMessageSizeLog = 10;
    854             const U32 maxMessageSizeMask = (1<<maxMessageSizeLog) - 1;
    855             U32 messageSize = (FUZ_rand(&randState) & maxMessageSizeMask) + 1;
    856             U32 iNext = 0;
    857             U32 rNext = 0;
    858             U32 dNext = 0;
    859             const U32 dBufferSize = ringBufferSize + maxMessageSizeMask;
    860 
    861             XXH64_reset(&xxhOrig, 0);
    862             XXH64_reset(&xxhNew, 0);
    863             LZ4_resetStreamHC(&sHC, compressionLevel);
    864             LZ4_setStreamDecode(&decodeState, NULL, 0);
    865 
    866             while (iNext + messageSize < testCompressedSize) {
    867                 XXH64_update(&xxhOrig, testInput + iNext, messageSize);
    868                 crcOrig = XXH64_digest(&xxhOrig);
    869 
    870                 memcpy (ringBuffer + rNext, testInput + iNext, messageSize);
    871                 result = LZ4_compress_HC_continue(&sHC, ringBuffer + rNext, testCompressed, messageSize, testCompressedSize-ringBufferSize);
    872                 FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() compression failed");
    873 
    874                 result = LZ4_decompress_safe_continue(&decodeState, testCompressed, testVerify + dNext, result, messageSize);
    875                 FUZ_CHECKTEST(result!=(int)messageSize, "ringBuffer : LZ4_decompress_safe() test failed");
    876 
    877                 XXH64_update(&xxhNew, testVerify + dNext, messageSize);
    878                 { U64 const crcNew = XXH64_digest(&xxhNew);
    879                   FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); }
    880 
    881                 /* prepare next message */
    882                 iNext += messageSize;
    883                 rNext += messageSize;
    884                 dNext += messageSize;
    885                 messageSize = (FUZ_rand(&randState) & maxMessageSizeMask) + 1;
    886                 if (rNext + messageSize > ringBufferSize) rNext = 0;
    887                 if (dNext + messageSize > dBufferSize) dNext = 0;
    888             }
    889         }
    890 
    891         /* small decoder-side ring buffer test */
    892         {   XXH64_state_t xxhOrig;
    893             XXH64_state_t xxhNew;
    894             LZ4_streamDecode_t decodeState;
    895             const U32 maxMessageSizeLog = 12;
    896             const U32 maxMessageSizeMask = (1<<maxMessageSizeLog) - 1;
    897             U32 messageSize;
    898             U32 totalMessageSize = 0;
    899             U32 iNext = 0;
    900             U32 dNext = 0;
    901             const U32 dBufferSize = 64 KB;
    902 
    903             XXH64_reset(&xxhOrig, 0);
    904             XXH64_reset(&xxhNew, 0);
    905             LZ4_resetStreamHC(&sHC, compressionLevel);
    906             LZ4_setStreamDecode(&decodeState, NULL, 0);
    907 
    908 #define BSIZE1 65537
    909 #define BSIZE2 16435
    910 
    911             /* first block */
    912 
    913                 messageSize = BSIZE1;
    914                 XXH64_update(&xxhOrig, testInput + iNext, messageSize);
    915                 crcOrig = XXH64_digest(&xxhOrig);
    916 
    917                 result = LZ4_compress_HC_continue(&sHC, testInput + iNext, testCompressed, messageSize, testCompressedSize-ringBufferSize);
    918                 FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() compression failed");
    919 
    920                 result = LZ4_decompress_safe_continue(&decodeState, testCompressed, testVerify + dNext, result, messageSize);
    921                 FUZ_CHECKTEST(result!=(int)messageSize, "64K D.ringBuffer : LZ4_decompress_safe() test failed");
    922 
    923                 XXH64_update(&xxhNew, testVerify + dNext, messageSize);
    924                 { U64 const crcNew = XXH64_digest(&xxhNew);
    925                   FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); }
    926 
    927                 /* prepare next message */
    928                 dNext += messageSize;
    929                 totalMessageSize += messageSize;
    930                 messageSize = BSIZE2;
    931                 iNext = 132000;
    932                 memcpy(testInput + iNext, testInput + 8, messageSize);
    933                 if (dNext > dBufferSize) dNext = 0;
    934 
    935             while (totalMessageSize < 9 MB) {
    936                 XXH64_update(&xxhOrig, testInput + iNext, messageSize);
    937                 crcOrig = XXH64_digest(&xxhOrig);
    938 
    939                 result = LZ4_compress_HC_continue(&sHC, testInput + iNext, testCompressed, messageSize, testCompressedSize-ringBufferSize);
    940                 FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() compression failed");
    941 
    942                 result = LZ4_decompress_safe_continue(&decodeState, testCompressed, testVerify + dNext, result, messageSize);
    943                 FUZ_CHECKTEST(result!=(int)messageSize, "64K D.ringBuffer : LZ4_decompress_safe() test failed");
    944 
    945                 XXH64_update(&xxhNew, testVerify + dNext, messageSize);
    946                 {   U64 const crcNew = XXH64_digest(&xxhNew);
    947                     if (crcOrig != crcNew) FUZ_findDiff(testInput + iNext, testVerify + dNext);
    948                     FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption during small decoder-side ring buffer test");
    949                 }
    950                 /* prepare next message */
    951                 dNext += messageSize;
    952                 totalMessageSize += messageSize;
    953                 messageSize = (FUZ_rand(&randState) & maxMessageSizeMask) + 1;
    954                 iNext = (FUZ_rand(&randState) & 65535);
    955                 if (dNext > dBufferSize) dNext = 0;
    956             }
    957         }
    958     }
    959 
    960     printf("All unit tests completed successfully compressionLevel=%d \n", compressionLevel);
    961     return;
    962 _output_error:
    963     exit(1);
    964 }
    965 
    966 
    967 static int FUZ_usage(const char* programName)
    968 {
    969     DISPLAY( "Usage :\n");
    970     DISPLAY( "      %s [args]\n", programName);
    971     DISPLAY( "\n");
    972     DISPLAY( "Arguments :\n");
    973     DISPLAY( " -i#    : Nb of tests (default:%i) \n", NB_ATTEMPTS);
    974     DISPLAY( " -T#    : Duration of tests, in seconds (default: use Nb of tests) \n");
    975     DISPLAY( " -s#    : Select seed (default:prompt user)\n");
    976     DISPLAY( " -t#    : Select starting test number (default:0)\n");
    977     DISPLAY( " -P#    : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
    978     DISPLAY( " -v     : verbose\n");
    979     DISPLAY( " -p     : pause at the end\n");
    980     DISPLAY( " -h     : display help and exit\n");
    981     return 0;
    982 }
    983 
    984 
    985 int main(int argc, const char** argv)
    986 {
    987     U32 seed = 0;
    988     int seedset = 0;
    989     int argNb;
    990     int nbTests = NB_ATTEMPTS;
    991     int testNb = 0;
    992     int proba = FUZ_COMPRESSIBILITY_DEFAULT;
    993     int use_pause = 0;
    994     const char* programName = argv[0];
    995     U32 duration = 0;
    996 
    997     /* Check command line */
    998     for(argNb=1; argNb<argc; argNb++) {
    999         const char* argument = argv[argNb];
   1000 
   1001         if(!argument) continue;   // Protection if argument empty
   1002 
   1003         // Decode command (note : aggregated commands are allowed)
   1004         if (argument[0]=='-') {
   1005             if (!strcmp(argument, "--no-prompt")) { use_pause=0; seedset=1; g_displayLevel=1; continue; }
   1006             argument++;
   1007 
   1008             while (*argument!=0) {
   1009                 switch(*argument)
   1010                 {
   1011                 case 'h':   /* display help */
   1012                     return FUZ_usage(programName);
   1013 
   1014                 case 'v':   /* verbose mode */
   1015                     argument++;
   1016                     g_displayLevel++;
   1017                     break;
   1018 
   1019                 case 'p':   /* pause at the end */
   1020                     argument++;
   1021                     use_pause=1;
   1022                     break;
   1023 
   1024                 case 'i':
   1025                     argument++;
   1026                     nbTests = 0; duration = 0;
   1027                     while ((*argument>='0') && (*argument<='9')) {
   1028                         nbTests *= 10;
   1029                         nbTests += *argument - '0';
   1030                         argument++;
   1031                     }
   1032                     break;
   1033 
   1034                 case 'T':
   1035                     argument++;
   1036                     nbTests = 0; duration = 0;
   1037                     for (;;) {
   1038                         switch(*argument)
   1039                         {
   1040                             case 'm': duration *= 60; argument++; continue;
   1041                             case 's':
   1042                             case 'n': argument++; continue;
   1043                             case '0':
   1044                             case '1':
   1045                             case '2':
   1046                             case '3':
   1047                             case '4':
   1048                             case '5':
   1049                             case '6':
   1050                             case '7':
   1051                             case '8':
   1052                             case '9': duration *= 10; duration += *argument++ - '0'; continue;
   1053                         }
   1054                         break;
   1055                     }
   1056                     break;
   1057 
   1058                 case 's':
   1059                     argument++;
   1060                     seed=0; seedset=1;
   1061                     while ((*argument>='0') && (*argument<='9')) {
   1062                         seed *= 10;
   1063                         seed += *argument - '0';
   1064                         argument++;
   1065                     }
   1066                     break;
   1067 
   1068                 case 't':   /* select starting test nb */
   1069                     argument++;
   1070                     testNb=0;
   1071                     while ((*argument>='0') && (*argument<='9')) {
   1072                         testNb *= 10;
   1073                         testNb += *argument - '0';
   1074                         argument++;
   1075                     }
   1076                     break;
   1077 
   1078                 case 'P':  /* change probability */
   1079                     argument++;
   1080                     proba=0;
   1081                     while ((*argument>='0') && (*argument<='9')) {
   1082                         proba *= 10;
   1083                         proba += *argument - '0';
   1084                         argument++;
   1085                     }
   1086                     if (proba<0) proba=0;
   1087                     if (proba>100) proba=100;
   1088                     break;
   1089                 default: ;
   1090                 }
   1091             }
   1092         }
   1093     }
   1094 
   1095     printf("Starting LZ4 fuzzer (%i-bits, v%s)\n", (int)(sizeof(size_t)*8), LZ4_versionString());
   1096 
   1097     if (!seedset) {
   1098         time_t const t = time(NULL);
   1099         U32 const h = XXH32(&t, sizeof(t), 1);
   1100         seed = h % 10000;
   1101     }
   1102     printf("Seed = %u\n", seed);
   1103 
   1104     if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) printf("Compressibility : %i%%\n", proba);
   1105 
   1106     if ((seedset==0) && (testNb==0)) { FUZ_unitTests(LZ4HC_CLEVEL_DEFAULT); FUZ_unitTests(LZ4HC_CLEVEL_OPT_MIN); }
   1107 
   1108     if (nbTests<=0) nbTests=1;
   1109 
   1110     {   int const result = FUZ_test(seed, nbTests, testNb, ((double)proba) / 100, duration);
   1111         if (use_pause) {
   1112             DISPLAY("press enter ... \n");
   1113             (void)getchar();
   1114         }
   1115         return result;
   1116     }
   1117 }
   1118