kusano 7d535a
// LZ4 streaming API example : double buffer
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
enum {
kusano 7d535a
    BLOCK_BYTES = 1024 * 8,
kusano 7d535a
//  BLOCK_BYTES = 1024 * 64,
kusano 7d535a
};
kusano 7d535a
kusano 7d535a
kusano 7d535a
size_t write_int(FILE* fp, int 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, size_t arrayBytes) {
kusano 7d535a
    return fwrite(array, 1, arrayBytes, fp);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
size_t read_int(FILE* fp, int* i) {
kusano 7d535a
    return fread(i, sizeof(*i), 1, fp);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
size_t read_bin(FILE* fp, void* array, size_t 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;
kusano 7d535a
    LZ4_stream_t* lz4Stream = &lz4Stream_body;
kusano 7d535a
kusano 7d535a
    char inpBuf[2][BLOCK_BYTES];
kusano 7d535a
    int  inpBufIndex = 0;
kusano 7d535a
    
kusano 7d535a
    LZ4_resetStream(lz4Stream);
kusano 7d535a
kusano 7d535a
    for(;;) {
kusano 7d535a
        char* const inpPtr = inpBuf[inpBufIndex];
kusano 7d535a
        const int inpBytes = (int) read_bin(inpFp, inpPtr, BLOCK_BYTES);
kusano 7d535a
        if(0 == inpBytes) {
kusano 7d535a
            break;
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
        {
kusano 7d535a
            char cmpBuf[LZ4_COMPRESSBOUND(BLOCK_BYTES)];
kusano 7d535a
            const int cmpBytes = LZ4_compress_fast_continue(
kusano 7d535a
                lz4Stream, inpPtr, cmpBuf, inpBytes, sizeof(cmpBuf), 1);
kusano 7d535a
            if(cmpBytes <= 0) {
kusano 7d535a
                break;
kusano 7d535a
            }
kusano 7d535a
            write_int(outFp, cmpBytes);
kusano 7d535a
            write_bin(outFp, cmpBuf, (size_t) cmpBytes);
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
        inpBufIndex = (inpBufIndex + 1) % 2;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    write_int(outFp, 0);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
void test_decompress(FILE* outFp, FILE* inpFp)
kusano 7d535a
{
kusano 7d535a
    LZ4_streamDecode_t lz4StreamDecode_body;
kusano 7d535a
    LZ4_streamDecode_t* lz4StreamDecode = &lz4StreamDecode_body;
kusano 7d535a
kusano 7d535a
    char decBuf[2][BLOCK_BYTES];
kusano 7d535a
    int  decBufIndex = 0;
kusano 7d535a
kusano 7d535a
    LZ4_setStreamDecode(lz4StreamDecode, NULL, 0);
kusano 7d535a
kusano 7d535a
    for(;;) {
kusano 7d535a
        char cmpBuf[LZ4_COMPRESSBOUND(BLOCK_BYTES)];
kusano 7d535a
        int  cmpBytes = 0;
kusano 7d535a
kusano 7d535a
        {
kusano 7d535a
            const size_t readCount0 = read_int(inpFp, &cmpBytes);
kusano 7d535a
            if(readCount0 != 1 || cmpBytes <= 0) {
kusano 7d535a
                break;
kusano 7d535a
            }
kusano 7d535a
kusano 7d535a
            const size_t readCount1 = read_bin(inpFp, cmpBuf, (size_t) cmpBytes);
kusano 7d535a
            if(readCount1 != (size_t) cmpBytes) {
kusano 7d535a
                break;
kusano 7d535a
            }
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
        {
kusano 7d535a
            char* const decPtr = decBuf[decBufIndex];
kusano 7d535a
            const int decBytes = LZ4_decompress_safe_continue(
kusano 7d535a
                lz4StreamDecode, cmpBuf, decPtr, cmpBytes, BLOCK_BYTES);
kusano 7d535a
            if(decBytes <= 0) {
kusano 7d535a
                break;
kusano 7d535a
            }
kusano 7d535a
            write_bin(outFp, decPtr, (size_t) decBytes);
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
        decBufIndex = (decBufIndex + 1) % 2;
kusano 7d535a
    }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
int compare(FILE* fp0, FILE* fp1)
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 = read_bin(fp0, b0, sizeof(b0));
kusano 7d535a
        const size_t r1 = read_bin(fp1, b1, sizeof(b1));
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], BLOCK_BYTES);
kusano 7d535a
    snprintf(decFilename, 256, "%s.lz4s-%d.dec", argv[1], BLOCK_BYTES);
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
        printf("compress : %s -> %s\n", inpFilename, lz4Filename);
kusano 7d535a
        test_compress(outFp, inpFp);
kusano 7d535a
        printf("compress : done\n");
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
        printf("decompress : %s -> %s\n", lz4Filename, decFilename);
kusano 7d535a
        test_decompress(outFp, inpFp);
kusano 7d535a
        printf("decompress : done\n");
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
        printf("verify : %s <-> %s\n", inpFilename, decFilename);
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
}