kusano 7d535a
// LZ4 streaming API example : line-by-line logfile compression
kusano 7d535a
// Copyright : Takayuki Matsuoka
kusano 7d535a
kusano 7d535a
kusano 7d535a
#ifdef _MSC_VER    /* Visual Studio */
kusano 7d535a
#  define _CRT_SECURE_NO_WARNINGS
kusano 7d535a
#  define snprintf sprintf_s
kusano 7d535a
#endif
kusano 7d535a
#include "lz4.h"
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
kusano 7d535a
static size_t write_uint16(FILE* fp, uint16_t i)
kusano 7d535a
{
kusano 7d535a
    return fwrite(&i, sizeof(i), 1, fp);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static size_t write_bin(FILE* fp, const void* array, int arrayBytes)
kusano 7d535a
{
kusano 7d535a
    return fwrite(array, 1, arrayBytes, fp);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static size_t read_uint16(FILE* fp, uint16_t* i)
kusano 7d535a
{
kusano 7d535a
    return fread(i, sizeof(*i), 1, fp);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static size_t read_bin(FILE* fp, void* array, int arrayBytes)
kusano 7d535a
{
kusano 7d535a
    return fread(array, 1, arrayBytes, fp);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
static void test_compress(
kusano 7d535a
    FILE* outFp,
kusano 7d535a
    FILE* inpFp,
kusano 7d535a
    size_t messageMaxBytes,
kusano 7d535a
    size_t ringBufferBytes)
kusano 7d535a
{
kusano 7d535a
    LZ4_stream_t* const lz4Stream = LZ4_createStream();
kusano 7d535a
    const size_t cmpBufBytes = LZ4_COMPRESSBOUND(messageMaxBytes);
kusano 7d535a
    char* const cmpBuf = (char*) malloc(cmpBufBytes);
kusano 7d535a
    char* const inpBuf = (char*) malloc(ringBufferBytes);
kusano 7d535a
    int inpOffset = 0;
kusano 7d535a
kusano 7d535a
    for ( ; ; )
kusano 7d535a
    {
kusano 7d535a
        char* const inpPtr = &inpBuf[inpOffset];
kusano 7d535a
kusano 7d535a
#if 0
kusano 7d535a
        // Read random length data to the ring buffer.
kusano 7d535a
        const int randomLength = (rand() % messageMaxBytes) + 1;
kusano 7d535a
        const int inpBytes = (int) read_bin(inpFp, inpPtr, randomLength);
kusano 7d535a
        if (0 == inpBytes) break;
kusano 7d535a
#else
kusano 7d535a
        // Read line to the ring buffer.
kusano 7d535a
        int inpBytes = 0;
kusano 7d535a
        if (!fgets(inpPtr, (int) messageMaxBytes, inpFp))
kusano 7d535a
            break;
kusano 7d535a
        inpBytes = (int) strlen(inpPtr);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
        {
kusano 7d535a
            const int cmpBytes = LZ4_compress_fast_continue(
kusano 7d535a
                lz4Stream, inpPtr, cmpBuf, inpBytes, cmpBufBytes, 1);
kusano 7d535a
            if (cmpBytes <= 0) break;
kusano 7d535a
            write_uint16(outFp, (uint16_t) cmpBytes);
kusano 7d535a
            write_bin(outFp, cmpBuf, cmpBytes);
kusano 7d535a
kusano 7d535a
            // Add and wraparound the ringbuffer offset
kusano 7d535a
            inpOffset += inpBytes;
kusano 7d535a
            if ((size_t)inpOffset >= ringBufferBytes - messageMaxBytes) inpOffset = 0;
kusano 7d535a
        }
kusano 7d535a
    }
kusano 7d535a
    write_uint16(outFp, 0);
kusano 7d535a
kusano 7d535a
    free(inpBuf);
kusano 7d535a
    free(cmpBuf);
kusano 7d535a
    LZ4_freeStream(lz4Stream);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
static void test_decompress(
kusano 7d535a
    FILE* outFp,
kusano 7d535a
    FILE* inpFp,
kusano 7d535a
    size_t messageMaxBytes,
kusano 7d535a
    size_t ringBufferBytes)
kusano 7d535a
{
kusano 7d535a
    LZ4_streamDecode_t* const lz4StreamDecode = LZ4_createStreamDecode();
kusano 7d535a
    char* const cmpBuf = (char*) malloc(LZ4_COMPRESSBOUND(messageMaxBytes));
kusano 7d535a
    char* const decBuf = (char*) malloc(ringBufferBytes);
kusano 7d535a
    int decOffset = 0;
kusano 7d535a
kusano 7d535a
    for ( ; ; )
kusano 7d535a
    {
kusano 7d535a
        uint16_t cmpBytes = 0;
kusano 7d535a
kusano 7d535a
        if (read_uint16(inpFp, &cmpBytes) != 1) break;
kusano 7d535a
        if (cmpBytes <= 0) break;
kusano 7d535a
        if (read_bin(inpFp, cmpBuf, cmpBytes) != cmpBytes) break;
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, (int) messageMaxBytes);
kusano 7d535a
            if (decBytes <= 0) break;
kusano 7d535a
            write_bin(outFp, decPtr, decBytes);
kusano 7d535a
kusano 7d535a
            // Add and wraparound the ringbuffer offset
kusano 7d535a
            decOffset += decBytes;
kusano 7d535a
            if ((size_t)decOffset >= ringBufferBytes - messageMaxBytes) decOffset = 0;
kusano 7d535a
        }
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    free(decBuf);
kusano 7d535a
    free(cmpBuf);
kusano 7d535a
    LZ4_freeStreamDecode(lz4StreamDecode);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
static int compare(FILE* f0, FILE* f1)
kusano 7d535a
{
kusano 7d535a
    int result = 0;
kusano 7d535a
    const size_t tempBufferBytes = 65536;
kusano 7d535a
    char* const b0 = (char*) malloc(tempBufferBytes);
kusano 7d535a
    char* const b1 = (char*) malloc(tempBufferBytes);
kusano 7d535a
kusano 7d535a
    while(0 == result)
kusano 7d535a
    {
kusano 7d535a
        const size_t r0 = fread(b0, 1, tempBufferBytes, f0);
kusano 7d535a
        const size_t r1 = fread(b1, 1, tempBufferBytes, f1);
kusano 7d535a
kusano 7d535a
        result = (int) r0 - (int) r1;
kusano 7d535a
kusano 7d535a
        if (0 == r0 || 0 == r1) break;
kusano 7d535a
        if (0 == result) result = memcmp(b0, b1, r0);
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    free(b1);
kusano 7d535a
    free(b0);
kusano 7d535a
    return result;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
int main(int argc, char* argv[])
kusano 7d535a
{
kusano 7d535a
    enum {
kusano 7d535a
        MESSAGE_MAX_BYTES   = 1024,
kusano 7d535a
        RING_BUFFER_BYTES   = 1024 * 256 + MESSAGE_MAX_BYTES,
kusano 7d535a
    };
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
    {
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", argv[1]);
kusano 7d535a
    snprintf(decFilename, 256, "%s.lz4s.dec", argv[1]);
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, MESSAGE_MAX_BYTES, RING_BUFFER_BYTES);
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, MESSAGE_MAX_BYTES, RING_BUFFER_BYTES);
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
        fclose(decFp);
kusano 7d535a
        fclose(inpFp);
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    return 0;
kusano 7d535a
}