kusano 7d535a
// LZ4 streaming API example : ring buffer
kusano 7d535a
// Based on sample code from Takayuki Matsuoka
kusano 7d535a
kusano 7d535a
kusano 7d535a
/**************************************
kusano 7d535a
 * Compiler Options
kusano 7d535a
 **************************************/
kusano 7d535a
#ifdef _MSC_VER    /* Visual Studio */
kusano 7d535a
#  define _CRT_SECURE_NO_WARNINGS // for MSVC
kusano 7d535a
#  define snprintf sprintf_s
kusano 7d535a
#endif
kusano 7d535a
#ifdef __GNUC__
kusano 7d535a
#  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) */
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/**************************************
kusano 7d535a
 * Includes
kusano 7d535a
 **************************************/
kusano 7d535a
#include <stdio.h></stdio.h>
kusano 7d535a
#include <stdint.h></stdint.h>
kusano 7d535a
#include <stdlib.h></stdlib.h>
kusano 7d535a
#include <string.h></string.h>
kusano 7d535a
#include "lz4.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
enum {
kusano 7d535a
    MESSAGE_MAX_BYTES   = 1024,
kusano 7d535a
    RING_BUFFER_BYTES   = 1024 * 8 + MESSAGE_MAX_BYTES,
kusano 7d535a
    DECODE_RING_BUFFER  = RING_BUFFER_BYTES + MESSAGE_MAX_BYTES   // Intentionally larger, to test unsynchronized ring buffers
kusano 7d535a
};
kusano 7d535a
kusano 7d535a
kusano 7d535a
size_t write_int32(FILE* fp, int32_t i) {
kusano 7d535a
    return fwrite(&i, sizeof(i), 1, fp);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
size_t write_bin(FILE* fp, const void* array, int arrayBytes) {
kusano 7d535a
    return fwrite(array, 1, arrayBytes, fp);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
size_t read_int32(FILE* fp, int32_t* i) {
kusano 7d535a
    return fread(i, sizeof(*i), 1, fp);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
size_t read_bin(FILE* fp, void* array, int arrayBytes) {
kusano 7d535a
    return fread(array, 1, arrayBytes, fp);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
void test_compress(FILE* outFp, FILE* inpFp)
kusano 7d535a
{
kusano 7d535a
    LZ4_stream_t lz4Stream_body = { 0 };
kusano 7d535a
    LZ4_stream_t* lz4Stream = &lz4Stream_body;
kusano 7d535a
kusano 7d535a
    static char inpBuf[RING_BUFFER_BYTES];
kusano 7d535a
    int inpOffset = 0;
kusano 7d535a
kusano 7d535a
    for(;;) {
kusano 7d535a
        // Read random length ([1,MESSAGE_MAX_BYTES]) data to the ring buffer.
kusano 7d535a
        char* const inpPtr = &inpBuf[inpOffset];
kusano 7d535a
        const int randomLength = (rand() % MESSAGE_MAX_BYTES) + 1;
kusano 7d535a
        const int inpBytes = (int) read_bin(inpFp, inpPtr, randomLength);
kusano 7d535a
        if (0 == inpBytes) break;
kusano 7d535a
kusano 7d535a
        {
kusano 7d535a
            char cmpBuf[LZ4_COMPRESSBOUND(MESSAGE_MAX_BYTES)];
kusano 7d535a
            const int cmpBytes = LZ4_compress_continue(lz4Stream, inpPtr, cmpBuf, inpBytes);
kusano 7d535a
            if(cmpBytes <= 0) break;
kusano 7d535a
            write_int32(outFp, cmpBytes);
kusano 7d535a
            write_bin(outFp, cmpBuf, cmpBytes);
kusano 7d535a
kusano 7d535a
            inpOffset += inpBytes;
kusano 7d535a
kusano 7d535a
            // Wraparound the ringbuffer offset
kusano 7d535a
            if(inpOffset >= RING_BUFFER_BYTES - MESSAGE_MAX_BYTES) inpOffset = 0;
kusano 7d535a
        }
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    write_int32(outFp, 0);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
void test_decompress(FILE* outFp, FILE* inpFp)
kusano 7d535a
{
kusano 7d535a
    static char decBuf[DECODE_RING_BUFFER];
kusano 7d535a
    int   decOffset    = 0;
kusano 7d535a
    LZ4_streamDecode_t lz4StreamDecode_body = { 0 };
kusano 7d535a
    LZ4_streamDecode_t* lz4StreamDecode = &lz4StreamDecode_body;
kusano 7d535a
kusano 7d535a
    for(;;) {
kusano 7d535a
        int cmpBytes = 0;
kusano 7d535a
        char cmpBuf[LZ4_COMPRESSBOUND(MESSAGE_MAX_BYTES)];
kusano 7d535a
kusano 7d535a
        {
kusano 7d535a
            const size_t r0 = read_int32(inpFp, &cmpBytes);
kusano 7d535a
            if(r0 != 1 || cmpBytes <= 0) break;
kusano 7d535a
kusano 7d535a
            const size_t r1 = read_bin(inpFp, cmpBuf, cmpBytes);
kusano 7d535a
            if(r1 != (size_t) cmpBytes) break;
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
        {
kusano 7d535a
            char* const decPtr = &decBuf[decOffset];
kusano 7d535a
            const int decBytes = LZ4_decompress_safe_continue(
kusano 7d535a
                lz4StreamDecode, cmpBuf, decPtr, cmpBytes, MESSAGE_MAX_BYTES);
kusano 7d535a
            if(decBytes <= 0) break;
kusano 7d535a
            decOffset += decBytes;
kusano 7d535a
            write_bin(outFp, decPtr, decBytes);
kusano 7d535a
kusano 7d535a
            // Wraparound the ringbuffer offset
kusano 7d535a
            if(decOffset >= DECODE_RING_BUFFER - MESSAGE_MAX_BYTES) decOffset = 0;
kusano 7d535a
        }
kusano 7d535a
    }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
int compare(FILE* f0, FILE* f1)
kusano 7d535a
{
kusano 7d535a
    int result = 0;
kusano 7d535a
kusano 7d535a
    while(0 == result) {
kusano 7d535a
        char b0[65536];
kusano 7d535a
        char b1[65536];
kusano 7d535a
        const size_t r0 = fread(b0, 1, sizeof(b0), f0);
kusano 7d535a
        const size_t r1 = fread(b1, 1, sizeof(b1), f1);
kusano 7d535a
kusano 7d535a
        result = (int) r0 - (int) r1;
kusano 7d535a
kusano 7d535a
        if(0 == r0 || 0 == r1) {
kusano 7d535a
            break;
kusano 7d535a
        }
kusano 7d535a
        if(0 == result) {
kusano 7d535a
            result = memcmp(b0, b1, r0);
kusano 7d535a
        }
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    return result;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
int main(int argc, char** argv)
kusano 7d535a
{
kusano 7d535a
    char inpFilename[256] = { 0 };
kusano 7d535a
    char lz4Filename[256] = { 0 };
kusano 7d535a
    char decFilename[256] = { 0 };
kusano 7d535a
kusano 7d535a
    if(argc < 2) {
kusano 7d535a
        printf("Please specify input filename\n");
kusano 7d535a
        return 0;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    snprintf(inpFilename, 256, "%s", argv[1]);
kusano 7d535a
    snprintf(lz4Filename, 256, "%s.lz4s-%d", argv[1], 0);
kusano 7d535a
    snprintf(decFilename, 256, "%s.lz4s-%d.dec", argv[1], 0);
kusano 7d535a
kusano 7d535a
    printf("inp = [%s]\n", inpFilename);
kusano 7d535a
    printf("lz4 = [%s]\n", lz4Filename);
kusano 7d535a
    printf("dec = [%s]\n", decFilename);
kusano 7d535a
kusano 7d535a
    // compress
kusano 7d535a
    {
kusano 7d535a
        FILE* inpFp = fopen(inpFilename, "rb");
kusano 7d535a
        FILE* outFp = fopen(lz4Filename, "wb");
kusano 7d535a
kusano 7d535a
        test_compress(outFp, inpFp);
kusano 7d535a
kusano 7d535a
        fclose(outFp);
kusano 7d535a
        fclose(inpFp);
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    // decompress
kusano 7d535a
    {
kusano 7d535a
        FILE* inpFp = fopen(lz4Filename, "rb");
kusano 7d535a
        FILE* outFp = fopen(decFilename, "wb");
kusano 7d535a
kusano 7d535a
        test_decompress(outFp, inpFp);
kusano 7d535a
kusano 7d535a
        fclose(outFp);
kusano 7d535a
        fclose(inpFp);
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    // verify
kusano 7d535a
    {
kusano 7d535a
        FILE* inpFp = fopen(inpFilename, "rb");
kusano 7d535a
        FILE* decFp = fopen(decFilename, "rb");
kusano 7d535a
kusano 7d535a
        const int cmp = compare(inpFp, decFp);
kusano 7d535a
        if(0 == cmp) {
kusano 7d535a
            printf("Verify : OK\n");
kusano 7d535a
        } else {
kusano 7d535a
            printf("Verify : NG\n");
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
        fclose(decFp);
kusano 7d535a
        fclose(inpFp);
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    return 0;
kusano 7d535a
}