2014-10-22 07:07:56 +00:00
|
|
|
// LZ4 HC streaming API example : ring buffer
|
2017-11-25 01:18:46 +00:00
|
|
|
// Based on a previous example by Takayuki Matsuoka
|
2014-10-22 07:07:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**************************************
|
2014-11-03 08:29:45 +00:00
|
|
|
* Compiler Options
|
|
|
|
**************************************/
|
2017-11-25 01:18:46 +00:00
|
|
|
#if defined(_MSC_VER) && (_MSC_VER <= 1800) /* Visual Studio <= 2013 */
|
|
|
|
# define _CRT_SECURE_NO_WARNINGS
|
2014-10-22 07:07:56 +00:00
|
|
|
# define snprintf sprintf_s
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
|
|
|
|
#ifdef __GNUC__
|
|
|
|
# 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) */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/**************************************
|
2014-11-03 08:29:45 +00:00
|
|
|
* Includes
|
|
|
|
**************************************/
|
2014-10-22 07:07:56 +00:00
|
|
|
#include "lz4hc.h"
|
|
|
|
#include "lz4.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
enum {
|
|
|
|
MESSAGE_MAX_BYTES = 1024,
|
2014-10-25 19:52:10 +00:00
|
|
|
RING_BUFFER_BYTES = 1024 * 8 + MESSAGE_MAX_BYTES,
|
2014-11-04 09:04:37 +00:00
|
|
|
DEC_BUFFER_BYTES = RING_BUFFER_BYTES + MESSAGE_MAX_BYTES // Intentionally larger to test unsynchronized ring buffers
|
2014-10-22 07:07:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
size_t write_int32(FILE* fp, int32_t i) {
|
|
|
|
return fwrite(&i, sizeof(i), 1, fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t write_bin(FILE* fp, const void* array, int arrayBytes) {
|
|
|
|
return fwrite(array, 1, arrayBytes, fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t read_int32(FILE* fp, int32_t* i) {
|
|
|
|
return fread(i, sizeof(*i), 1, fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t read_bin(FILE* fp, void* array, int arrayBytes) {
|
|
|
|
return fread(array, 1, arrayBytes, fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void test_compress(FILE* outFp, FILE* inpFp)
|
|
|
|
{
|
|
|
|
LZ4_streamHC_t lz4Stream_body = { 0 };
|
|
|
|
LZ4_streamHC_t* lz4Stream = &lz4Stream_body;
|
|
|
|
|
|
|
|
static char inpBuf[RING_BUFFER_BYTES];
|
|
|
|
int inpOffset = 0;
|
|
|
|
|
2016-11-22 03:26:49 +00:00
|
|
|
for(;;) {
|
2014-10-22 07:07:56 +00:00
|
|
|
// Read random length ([1,MESSAGE_MAX_BYTES]) data to the ring buffer.
|
|
|
|
char* const inpPtr = &inpBuf[inpOffset];
|
|
|
|
const int randomLength = (rand() % MESSAGE_MAX_BYTES) + 1;
|
|
|
|
const int inpBytes = (int) read_bin(inpFp, inpPtr, randomLength);
|
|
|
|
if (0 == inpBytes) break;
|
|
|
|
|
2016-11-21 23:00:50 +00:00
|
|
|
#define CMPBUFSIZE (LZ4_COMPRESSBOUND(MESSAGE_MAX_BYTES))
|
2016-11-22 03:26:49 +00:00
|
|
|
{ char cmpBuf[CMPBUFSIZE];
|
2016-11-21 23:00:50 +00:00
|
|
|
const int cmpBytes = LZ4_compress_HC_continue(lz4Stream, inpPtr, cmpBuf, inpBytes, CMPBUFSIZE);
|
2014-10-25 19:52:10 +00:00
|
|
|
|
2014-10-22 07:07:56 +00:00
|
|
|
if(cmpBytes <= 0) break;
|
|
|
|
write_int32(outFp, cmpBytes);
|
|
|
|
write_bin(outFp, cmpBuf, cmpBytes);
|
|
|
|
|
|
|
|
inpOffset += inpBytes;
|
|
|
|
|
|
|
|
// Wraparound the ringbuffer offset
|
|
|
|
if(inpOffset >= RING_BUFFER_BYTES - MESSAGE_MAX_BYTES)
|
|
|
|
inpOffset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
write_int32(outFp, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void test_decompress(FILE* outFp, FILE* inpFp)
|
|
|
|
{
|
2014-10-25 19:52:10 +00:00
|
|
|
static char decBuf[DEC_BUFFER_BYTES];
|
2014-10-22 07:07:56 +00:00
|
|
|
int decOffset = 0;
|
|
|
|
LZ4_streamDecode_t lz4StreamDecode_body = { 0 };
|
|
|
|
LZ4_streamDecode_t* lz4StreamDecode = &lz4StreamDecode_body;
|
|
|
|
|
2016-11-22 03:26:49 +00:00
|
|
|
for(;;) {
|
2014-10-25 19:52:10 +00:00
|
|
|
int cmpBytes = 0;
|
2016-11-21 23:00:50 +00:00
|
|
|
char cmpBuf[CMPBUFSIZE];
|
2014-10-22 07:07:56 +00:00
|
|
|
|
2016-11-22 03:26:49 +00:00
|
|
|
{ const size_t r0 = read_int32(inpFp, &cmpBytes);
|
2014-10-25 19:52:10 +00:00
|
|
|
size_t r1;
|
2014-11-03 08:29:45 +00:00
|
|
|
if(r0 != 1 || cmpBytes <= 0)
|
2014-10-25 19:52:10 +00:00
|
|
|
break;
|
2014-10-22 07:07:56 +00:00
|
|
|
|
|
|
|
r1 = read_bin(inpFp, cmpBuf, cmpBytes);
|
2014-11-03 08:29:45 +00:00
|
|
|
if(r1 != (size_t) cmpBytes)
|
2014-10-25 19:52:10 +00:00
|
|
|
break;
|
2014-10-22 07:07:56 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 03:26:49 +00:00
|
|
|
{ char* const decPtr = &decBuf[decOffset];
|
2014-10-22 07:07:56 +00:00
|
|
|
const int decBytes = LZ4_decompress_safe_continue(
|
|
|
|
lz4StreamDecode, cmpBuf, decPtr, cmpBytes, MESSAGE_MAX_BYTES);
|
2014-11-03 08:29:45 +00:00
|
|
|
if(decBytes <= 0)
|
2014-10-25 19:52:10 +00:00
|
|
|
break;
|
|
|
|
|
2014-10-22 07:07:56 +00:00
|
|
|
decOffset += decBytes;
|
|
|
|
write_bin(outFp, decPtr, decBytes);
|
|
|
|
|
|
|
|
// Wraparound the ringbuffer offset
|
2014-11-03 08:29:45 +00:00
|
|
|
if(decOffset >= DEC_BUFFER_BYTES - MESSAGE_MAX_BYTES)
|
2014-10-25 19:52:10 +00:00
|
|
|
decOffset = 0;
|
2014-10-22 07:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-25 19:52:10 +00:00
|
|
|
// Compare 2 files content
|
|
|
|
// return 0 if identical
|
|
|
|
// return ByteNb>0 if different
|
|
|
|
size_t compare(FILE* f0, FILE* f1)
|
2014-10-22 07:07:56 +00:00
|
|
|
{
|
2014-10-25 19:52:10 +00:00
|
|
|
size_t result = 1;
|
2014-10-22 07:07:56 +00:00
|
|
|
|
2016-11-22 03:26:49 +00:00
|
|
|
for (;;) {
|
2014-10-22 07:07:56 +00:00
|
|
|
char b0[65536];
|
|
|
|
char b1[65536];
|
|
|
|
const size_t r0 = fread(b0, 1, sizeof(b0), f0);
|
|
|
|
const size_t r1 = fread(b1, 1, sizeof(b1), f1);
|
|
|
|
|
2014-10-25 19:52:10 +00:00
|
|
|
if ((r0==0) && (r1==0)) return 0; // success
|
2014-10-22 07:07:56 +00:00
|
|
|
|
2016-11-22 03:26:49 +00:00
|
|
|
if (r0 != r1) {
|
2014-10-25 19:52:10 +00:00
|
|
|
size_t smallest = r0;
|
|
|
|
if (r1<r0) smallest = r1;
|
|
|
|
result += smallest;
|
2014-12-13 10:49:55 +00:00
|
|
|
break;
|
2014-10-22 07:07:56 +00:00
|
|
|
}
|
2014-10-25 19:52:10 +00:00
|
|
|
|
2016-11-22 03:26:49 +00:00
|
|
|
if (memcmp(b0, b1, r0)) {
|
2014-10-25 19:52:10 +00:00
|
|
|
unsigned errorPos = 0;
|
|
|
|
while ((errorPos < r0) && (b0[errorPos]==b1[errorPos])) errorPos++;
|
|
|
|
result += errorPos;
|
2014-12-13 10:49:55 +00:00
|
|
|
break;
|
2014-10-22 07:07:56 +00:00
|
|
|
}
|
2014-10-25 19:52:10 +00:00
|
|
|
|
|
|
|
result += sizeof(b0);
|
2014-10-22 07:07:56 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 10:49:55 +00:00
|
|
|
return result;
|
2014-10-22 07:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-22 03:26:49 +00:00
|
|
|
int main(int argc, const char** argv)
|
2014-10-22 07:07:56 +00:00
|
|
|
{
|
|
|
|
char inpFilename[256] = { 0 };
|
|
|
|
char lz4Filename[256] = { 0 };
|
|
|
|
char decFilename[256] = { 0 };
|
2014-10-25 19:52:10 +00:00
|
|
|
unsigned fileID = 1;
|
|
|
|
unsigned pause = 0;
|
|
|
|
|
2014-10-22 07:07:56 +00:00
|
|
|
|
|
|
|
if(argc < 2) {
|
|
|
|
printf("Please specify input filename\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-25 19:52:10 +00:00
|
|
|
if (!strcmp(argv[1], "-p")) pause = 1, fileID = 2;
|
|
|
|
|
|
|
|
snprintf(inpFilename, 256, "%s", argv[fileID]);
|
|
|
|
snprintf(lz4Filename, 256, "%s.lz4s-%d", argv[fileID], 9);
|
|
|
|
snprintf(decFilename, 256, "%s.lz4s-%d.dec", argv[fileID], 9);
|
2014-10-22 07:07:56 +00:00
|
|
|
|
2014-11-03 08:29:45 +00:00
|
|
|
printf("input = [%s]\n", inpFilename);
|
|
|
|
printf("lz4 = [%s]\n", lz4Filename);
|
|
|
|
printf("decoded = [%s]\n", decFilename);
|
2014-10-22 07:07:56 +00:00
|
|
|
|
|
|
|
// compress
|
2016-11-22 03:26:49 +00:00
|
|
|
{ FILE* const inpFp = fopen(inpFilename, "rb");
|
|
|
|
FILE* const outFp = fopen(lz4Filename, "wb");
|
2014-10-22 07:07:56 +00:00
|
|
|
|
|
|
|
test_compress(outFp, inpFp);
|
|
|
|
|
|
|
|
fclose(outFp);
|
|
|
|
fclose(inpFp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// decompress
|
2016-11-22 03:26:49 +00:00
|
|
|
{ FILE* const inpFp = fopen(lz4Filename, "rb");
|
|
|
|
FILE* const outFp = fopen(decFilename, "wb");
|
2014-10-22 07:07:56 +00:00
|
|
|
|
|
|
|
test_decompress(outFp, inpFp);
|
|
|
|
|
|
|
|
fclose(outFp);
|
|
|
|
fclose(inpFp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify
|
2016-11-22 03:26:49 +00:00
|
|
|
{ FILE* const inpFp = fopen(inpFilename, "rb");
|
|
|
|
FILE* const decFp = fopen(decFilename, "rb");
|
2014-10-22 07:07:56 +00:00
|
|
|
|
2014-10-25 19:52:10 +00:00
|
|
|
const size_t cmp = compare(inpFp, decFp);
|
2014-10-22 07:07:56 +00:00
|
|
|
if(0 == cmp) {
|
|
|
|
printf("Verify : OK\n");
|
|
|
|
} else {
|
2014-10-25 19:52:10 +00:00
|
|
|
printf("Verify : NG : error at pos %u\n", (unsigned)cmp-1);
|
2014-10-22 07:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(decFp);
|
|
|
|
fclose(inpFp);
|
|
|
|
}
|
|
|
|
|
2016-11-22 03:26:49 +00:00
|
|
|
if (pause) {
|
|
|
|
int unused;
|
2014-10-25 19:52:10 +00:00
|
|
|
printf("Press enter to continue ...\n");
|
2016-11-22 03:26:49 +00:00
|
|
|
unused = getchar(); (void)unused; /* silence static analyzer */
|
2014-10-25 19:52:10 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 07:07:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|