Home | History | Annotate | Download | only in examples
      1 // LZ4 streaming API example : ring buffer
      2 // Based on sample code from Takayuki Matsuoka
      3 
      4 
      5 /**************************************
      6  * Compiler Options
      7  **************************************/
      8 #ifdef _MSC_VER    /* Visual Studio */
      9 #  define _CRT_SECURE_NO_WARNINGS // for MSVC
     10 #  define snprintf sprintf_s
     11 #endif
     12 #ifdef __GNUC__
     13 #  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) */
     14 #endif
     15 
     16 
     17 /**************************************
     18  * Includes
     19  **************************************/
     20 #include <stdio.h>
     21 #include <stdint.h>
     22 #include <stdlib.h>
     23 #include <string.h>
     24 #include "lz4.h"
     25 
     26 
     27 enum {
     28     MESSAGE_MAX_BYTES   = 1024,
     29     RING_BUFFER_BYTES   = 1024 * 8 + MESSAGE_MAX_BYTES,
     30     DECODE_RING_BUFFER  = RING_BUFFER_BYTES + MESSAGE_MAX_BYTES   // Intentionally larger, to test unsynchronized ring buffers
     31 };
     32 
     33 
     34 size_t write_int32(FILE* fp, int32_t i) {
     35     return fwrite(&i, sizeof(i), 1, fp);
     36 }
     37 
     38 size_t write_bin(FILE* fp, const void* array, int arrayBytes) {
     39     return fwrite(array, 1, arrayBytes, fp);
     40 }
     41 
     42 size_t read_int32(FILE* fp, int32_t* i) {
     43     return fread(i, sizeof(*i), 1, fp);
     44 }
     45 
     46 size_t read_bin(FILE* fp, void* array, int arrayBytes) {
     47     return fread(array, 1, arrayBytes, fp);
     48 }
     49 
     50 
     51 void test_compress(FILE* outFp, FILE* inpFp)
     52 {
     53     LZ4_stream_t lz4Stream_body = { 0 };
     54     LZ4_stream_t* lz4Stream = &lz4Stream_body;
     55 
     56     static char inpBuf[RING_BUFFER_BYTES];
     57     int inpOffset = 0;
     58 
     59     for(;;) {
     60         // Read random length ([1,MESSAGE_MAX_BYTES]) data to the ring buffer.
     61         char* const inpPtr = &inpBuf[inpOffset];
     62         const int randomLength = (rand() % MESSAGE_MAX_BYTES) + 1;
     63         const int inpBytes = (int) read_bin(inpFp, inpPtr, randomLength);
     64         if (0 == inpBytes) break;
     65 
     66         {
     67 #define CMPBUFSIZE (LZ4_COMPRESSBOUND(MESSAGE_MAX_BYTES))
     68             char cmpBuf[CMPBUFSIZE];
     69             const int cmpBytes = LZ4_compress_fast_continue(lz4Stream, inpPtr, cmpBuf, inpBytes, CMPBUFSIZE, 0);
     70             if(cmpBytes <= 0) break;
     71             write_int32(outFp, cmpBytes);
     72             write_bin(outFp, cmpBuf, cmpBytes);
     73 
     74             inpOffset += inpBytes;
     75 
     76             // Wraparound the ringbuffer offset
     77             if(inpOffset >= RING_BUFFER_BYTES - MESSAGE_MAX_BYTES) inpOffset = 0;
     78         }
     79     }
     80 
     81     write_int32(outFp, 0);
     82 }
     83 
     84 
     85 void test_decompress(FILE* outFp, FILE* inpFp)
     86 {
     87     static char decBuf[DECODE_RING_BUFFER];
     88     int   decOffset    = 0;
     89     LZ4_streamDecode_t lz4StreamDecode_body = { 0 };
     90     LZ4_streamDecode_t* lz4StreamDecode = &lz4StreamDecode_body;
     91 
     92     for(;;) {
     93         int cmpBytes = 0;
     94         char cmpBuf[CMPBUFSIZE];
     95 
     96         {
     97             const size_t r0 = read_int32(inpFp, &cmpBytes);
     98             if(r0 != 1 || cmpBytes <= 0) break;
     99 
    100             const size_t r1 = read_bin(inpFp, cmpBuf, cmpBytes);
    101             if(r1 != (size_t) cmpBytes) break;
    102         }
    103 
    104         {
    105             char* const decPtr = &decBuf[decOffset];
    106             const int decBytes = LZ4_decompress_safe_continue(
    107                 lz4StreamDecode, cmpBuf, decPtr, cmpBytes, MESSAGE_MAX_BYTES);
    108             if(decBytes <= 0) break;
    109             decOffset += decBytes;
    110             write_bin(outFp, decPtr, decBytes);
    111 
    112             // Wraparound the ringbuffer offset
    113             if(decOffset >= DECODE_RING_BUFFER - MESSAGE_MAX_BYTES) decOffset = 0;
    114         }
    115     }
    116 }
    117 
    118 
    119 int compare(FILE* f0, FILE* f1)
    120 {
    121     int result = 0;
    122 
    123     while(0 == result) {
    124         char b0[65536];
    125         char b1[65536];
    126         const size_t r0 = fread(b0, 1, sizeof(b0), f0);
    127         const size_t r1 = fread(b1, 1, sizeof(b1), f1);
    128 
    129         result = (int) r0 - (int) r1;
    130 
    131         if(0 == r0 || 0 == r1) {
    132             break;
    133         }
    134         if(0 == result) {
    135             result = memcmp(b0, b1, r0);
    136         }
    137     }
    138 
    139     return result;
    140 }
    141 
    142 
    143 int main(int argc, char** argv)
    144 {
    145     char inpFilename[256] = { 0 };
    146     char lz4Filename[256] = { 0 };
    147     char decFilename[256] = { 0 };
    148 
    149     if(argc < 2) {
    150         printf("Please specify input filename\n");
    151         return 0;
    152     }
    153 
    154     snprintf(inpFilename, 256, "%s", argv[1]);
    155     snprintf(lz4Filename, 256, "%s.lz4s-%d", argv[1], 0);
    156     snprintf(decFilename, 256, "%s.lz4s-%d.dec", argv[1], 0);
    157 
    158     printf("inp = [%s]\n", inpFilename);
    159     printf("lz4 = [%s]\n", lz4Filename);
    160     printf("dec = [%s]\n", decFilename);
    161 
    162     // compress
    163     {
    164         FILE* inpFp = fopen(inpFilename, "rb");
    165         FILE* outFp = fopen(lz4Filename, "wb");
    166 
    167         test_compress(outFp, inpFp);
    168 
    169         fclose(outFp);
    170         fclose(inpFp);
    171     }
    172 
    173     // decompress
    174     {
    175         FILE* inpFp = fopen(lz4Filename, "rb");
    176         FILE* outFp = fopen(decFilename, "wb");
    177 
    178         test_decompress(outFp, inpFp);
    179 
    180         fclose(outFp);
    181         fclose(inpFp);
    182     }
    183 
    184     // verify
    185     {
    186         FILE* inpFp = fopen(inpFilename, "rb");
    187         FILE* decFp = fopen(decFilename, "rb");
    188 
    189         const int cmp = compare(inpFp, decFp);
    190         if(0 == cmp) {
    191             printf("Verify : OK\n");
    192         } else {
    193             printf("Verify : NG\n");
    194         }
    195 
    196         fclose(decFp);
    197         fclose(inpFp);
    198     }
    199 
    200     return 0;
    201 }
    202