|
kusano |
7d535a |
// LZ4 HC streaming API example : ring buffer
|
|
kusano |
7d535a |
// Based on previous work 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 |
|
|
kusano |
7d535a |
#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
|
|
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 "lz4hc.h"
|
|
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 |
MESSAGE_MAX_BYTES = 1024,
|
|
kusano |
7d535a |
RING_BUFFER_BYTES = 1024 * 8 + MESSAGE_MAX_BYTES,
|
|
kusano |
7d535a |
DEC_BUFFER_BYTES = 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_streamHC_t lz4Stream_body = { 0 };
|
|
kusano |
7d535a |
LZ4_streamHC_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 |
{
|
|
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_compressHC_continue(lz4Stream, inpPtr, cmpBuf, inpBytes);
|
|
kusano |
7d535a |
|
|
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)
|
|
kusano |
7d535a |
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[DEC_BUFFER_BYTES];
|
|
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 |
{
|
|
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 |
size_t r1;
|
|
kusano |
7d535a |
if(r0 != 1 || cmpBytes <= 0)
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r1 = read_bin(inpFp, cmpBuf, cmpBytes);
|
|
kusano |
7d535a |
if(r1 != (size_t) cmpBytes)
|
|
kusano |
7d535a |
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)
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
decOffset += decBytes;
|
|
kusano |
7d535a |
write_bin(outFp, decPtr, decBytes);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
// Wraparound the ringbuffer offset
|
|
kusano |
7d535a |
if(decOffset >= DEC_BUFFER_BYTES - MESSAGE_MAX_BYTES)
|
|
kusano |
7d535a |
decOffset = 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
// Compare 2 files content
|
|
kusano |
7d535a |
// return 0 if identical
|
|
kusano |
7d535a |
// return ByteNb>0 if different
|
|
kusano |
7d535a |
size_t compare(FILE* f0, FILE* f1)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
size_t result = 1;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
for (;;)
|
|
kusano |
7d535a |
{
|
|
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 |
if ((r0==0) && (r1==0)) return 0; // success
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (r0 != r1)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
size_t smallest = r0;
|
|
kusano |
7d535a |
if (r1
|
|
kusano |
7d535a |
result += smallest;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (memcmp(b0, b1, r0))
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
unsigned errorPos = 0;
|
|
kusano |
7d535a |
while ((errorPos < r0) && (b0[errorPos]==b1[errorPos])) errorPos++;
|
|
kusano |
7d535a |
result += errorPos;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
result += sizeof(b0);
|
|
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 |
unsigned fileID = 1;
|
|
kusano |
7d535a |
unsigned pause = 0;
|
|
kusano |
7d535a |
|
|
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 |
if (!strcmp(argv[1], "-p")) pause = 1, fileID = 2;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
snprintf(inpFilename, 256, "%s", argv[fileID]);
|
|
kusano |
7d535a |
snprintf(lz4Filename, 256, "%s.lz4s-%d", argv[fileID], 9);
|
|
kusano |
7d535a |
snprintf(decFilename, 256, "%s.lz4s-%d.dec", argv[fileID], 9);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
printf("input = [%s]\n", inpFilename);
|
|
kusano |
7d535a |
printf("lz4 = [%s]\n", lz4Filename);
|
|
kusano |
7d535a |
printf("decoded = [%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 size_t cmp = compare(inpFp, decFp);
|
|
kusano |
7d535a |
if(0 == cmp) {
|
|
kusano |
7d535a |
printf("Verify : OK\n");
|
|
kusano |
7d535a |
} else {
|
|
kusano |
7d535a |
printf("Verify : NG : error at pos %u\n", (unsigned)cmp-1);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
fclose(decFp);
|
|
kusano |
7d535a |
fclose(inpFp);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (pause)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
printf("Press enter to continue ...\n");
|
|
kusano |
7d535a |
getchar();
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
}
|