Home | History | Annotate | Download | only in tests
      1 /**************************************
      2  * Compiler Options
      3  **************************************/
      4 #ifdef _MSC_VER    /* Visual Studio */
      5 #  define _CRT_SECURE_NO_WARNINGS // for MSVC
      6 #  define snprintf sprintf_s
      7 #endif
      8 #ifdef __GNUC__
      9 #  pragma GCC diagnostic ignored "-Wmissing-braces"   /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */
     10 #endif
     11 
     12 
     13 /**************************************
     14  * Includes
     15  **************************************/
     16 #include <stdio.h>
     17 #include <stdint.h>
     18 #include <stdlib.h>
     19 #include <string.h>
     20 #include "lz4.h"
     21 
     22 
     23 /* Returns non-zero on failure. */
     24 int test_compress(const char *input, int inSize, char *output, int outSize)
     25 {
     26     LZ4_stream_t lz4Stream_body = { 0 };
     27     LZ4_stream_t* lz4Stream = &lz4Stream_body;
     28 
     29     int inOffset = 0;
     30     int outOffset = 0;
     31 
     32     if (inSize & 3) return -1;
     33 
     34     while (inOffset < inSize) {
     35         const int length = inSize >> 2;
     36         if (inSize > 1024) return -2;
     37         if (outSize - (outOffset + 8) < LZ4_compressBound(length)) return -3;
     38         {
     39             const int outBytes = LZ4_compress_fast_continue(
     40                 lz4Stream, input + inOffset, output + outOffset + 8, length, outSize-outOffset, 1);
     41             if(outBytes <= 0) return -4;
     42             memcpy(output + outOffset, &length, 4); /* input length */
     43             memcpy(output + outOffset + 4, &outBytes, 4); /* output length */
     44             inOffset += length;
     45             outOffset += outBytes + 8;
     46         }
     47     }
     48     if (outOffset + 8 > outSize) return -5;
     49     memset(output + outOffset, 0, 4);
     50     memset(output + outOffset + 4, 0, 4);
     51     return 0;
     52 }
     53 
     54 /* Returns non-zero on failure. Not a safe function. */
     55 int test_decompress(const char *uncompressed, const char *compressed)
     56 {
     57     char outBufferA[1024];
     58     char spacing; /* So prefixEnd != dest */
     59     char outBufferB[1024];
     60     char *output = outBufferA;
     61     char *lastOutput = outBufferB;
     62     LZ4_streamDecode_t lz4StreamDecode_body = { 0 };
     63     LZ4_streamDecode_t* lz4StreamDecode = &lz4StreamDecode_body;
     64     int offset = 0;
     65     int unOffset = 0;
     66     int lastBytes = 0;
     67 
     68     (void)spacing;
     69 
     70     for(;;) {
     71         int32_t bytes;
     72         int32_t unBytes;
     73         /* Read uncompressed size and compressed size */
     74         memcpy(&unBytes, compressed + offset, 4);
     75         memcpy(&bytes, compressed + offset + 4, 4);
     76         offset += 8;
     77         /* Check if we reached end of stream or error */
     78         if(bytes == 0 && unBytes == 0) return 0;
     79         if(bytes <= 0 || unBytes <= 0 || unBytes > 1024) return 1;
     80 
     81         /* Put the last output in the dictionary */
     82         LZ4_setStreamDecode(lz4StreamDecode, lastOutput, lastBytes);
     83         /* Decompress */
     84         bytes = LZ4_decompress_fast_continue(
     85             lz4StreamDecode, compressed + offset, output, unBytes);
     86         if(bytes <= 0) return 2;
     87         /* Check result */
     88         {   int const r = memcmp(uncompressed + unOffset, output, unBytes);
     89             if (r) return 3;
     90         }
     91         { char* const tmp = output; output = lastOutput; lastOutput = tmp; }
     92         offset += bytes;
     93         unOffset += unBytes;
     94         lastBytes = unBytes;
     95     }
     96 }
     97 
     98 
     99 int main(int argc, char **argv)
    100 {
    101     char input[] =
    102         "Hello Hello Hello Hello Hello Hello Hello Hello!"
    103         "Hello Hello Hello Hello Hello Hello Hello Hello!"
    104         "Hello Hello Hello Hello Hello Hello Hello Hello!"
    105         "Hello Hello Hello Hello Hello Hello Hello Hello!"
    106         "Hello Hello Hello Hello Hello Hello Hello Hello!"
    107         "Hello Hello Hello Hello Hello Hello Hello Hello!"
    108         "Hello Hello Hello Hello Hello Hello Hello Hello!"
    109         "Hello Hello Hello Hello Hello Hello Hello Hello!"
    110         "Hello Hello Hello Hello Hello Hello Hello Hello!"
    111         "Hello Hello Hello Hello Hello Hello Hello Hello!"
    112         "Hello Hello Hello Hello Hello Hello Hello Hello!"
    113         "Hello Hello Hello Hello Hello Hello Hello Hello!"
    114         "Hello Hello Hello Hello Hello Hello Hello Hello!"
    115         "Hello Hello Hello Hello Hello Hello Hello Hello!"
    116         "Hello Hello Hello Hello Hello Hello Hello Hello!"
    117         "Hello Hello Hello Hello Hello Hello Hello Hello";
    118     char output[LZ4_COMPRESSBOUND(4096)];
    119     int r;
    120 
    121     (void)argc;
    122     (void)argv;
    123 
    124     if ((r = test_compress(input, sizeof(input), output, sizeof(output)))) {
    125         return r;
    126     }
    127     if ((r = test_decompress(input, output))) {
    128         return r;
    129     }
    130     return 0;
    131 }
    132