summaryrefslogtreecommitdiffstats
path: root/examples/HCStreaming_ringBuffer.c
diff options
context:
space:
mode:
authorYann Collet <yann.collet.73@gmail.com>2014-10-25 19:52:10 (GMT)
committerYann Collet <yann.collet.73@gmail.com>2014-10-25 19:52:10 (GMT)
commit2b421e97d4e7cfbefdc007bf30133b0de7e7e14e (patch)
tree7267d51213e3d1abaf853a80491db38f3d8f82c8 /examples/HCStreaming_ringBuffer.c
parente2c84118f52cefe48fd2f751e66ad3ecd904f7b9 (diff)
downloadlz4-2b421e97d4e7cfbefdc007bf30133b0de7e7e14e.zip
lz4-2b421e97d4e7cfbefdc007bf30133b0de7e7e14e.tar.gz
lz4-2b421e97d4e7cfbefdc007bf30133b0de7e7e14e.tar.bz2
HC streaming : support small ringbuffer scenarios
Diffstat (limited to 'examples/HCStreaming_ringBuffer.c')
-rwxr-xr-xexamples/HCStreaming_ringBuffer.c90
1 files changed, 63 insertions, 27 deletions
diff --git a/examples/HCStreaming_ringBuffer.c b/examples/HCStreaming_ringBuffer.c
index 3532fa3..53e14ca 100755
--- a/examples/HCStreaming_ringBuffer.c
+++ b/examples/HCStreaming_ringBuffer.c
@@ -3,7 +3,7 @@
/**************************************
- Compiler Options
+Compiler Options
**************************************/
#ifdef _MSC_VER /* Visual Studio */
# define _CRT_SECURE_NO_WARNINGS // for MSVC
@@ -18,7 +18,7 @@
/**************************************
- Includes
+Includes
**************************************/
#include "lz4hc.h"
#include "lz4.h"
@@ -30,8 +30,8 @@
enum {
MESSAGE_MAX_BYTES = 1024,
- RING_BUFFER_BYTES = 1024 * 256 + MESSAGE_MAX_BYTES,
- DICT_BYTES = 65536,
+ RING_BUFFER_BYTES = 1024 * 8 + MESSAGE_MAX_BYTES,
+ DEC_BUFFER_BYTES = RING_BUFFER_BYTES + MESSAGE_MAX_BYTES
};
@@ -59,9 +59,10 @@ void test_compress(FILE* outFp, FILE* inpFp)
static char inpBuf[RING_BUFFER_BYTES];
int inpOffset = 0;
+ unsigned done = 0;
for(;;)
- {
+ {
// 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;
@@ -71,11 +72,13 @@ void test_compress(FILE* outFp, FILE* inpFp)
{
char cmpBuf[LZ4_COMPRESSBOUND(MESSAGE_MAX_BYTES)];
const int cmpBytes = LZ4_compressHC_continue(lz4Stream, inpPtr, cmpBuf, inpBytes);
+
if(cmpBytes <= 0) break;
write_int32(outFp, cmpBytes);
write_bin(outFp, cmpBuf, cmpBytes);
inpOffset += inpBytes;
+ done += inpBytes;
// Wraparound the ringbuffer offset
if(inpOffset >= RING_BUFFER_BYTES - MESSAGE_MAX_BYTES)
@@ -89,22 +92,26 @@ void test_compress(FILE* outFp, FILE* inpFp)
void test_decompress(FILE* outFp, FILE* inpFp)
{
- static char decBuf[RING_BUFFER_BYTES];
+ static char decBuf[DEC_BUFFER_BYTES];
int decOffset = 0;
LZ4_streamDecode_t lz4StreamDecode_body = { 0 };
LZ4_streamDecode_t* lz4StreamDecode = &lz4StreamDecode_body;
+ unsigned done = 0;
- for(;;) {
- int cmpBytes = 0;
+ for(;;)
+ {
+ int cmpBytes = 0;
char cmpBuf[LZ4_COMPRESSBOUND(MESSAGE_MAX_BYTES)];
{
const size_t r0 = read_int32(inpFp, &cmpBytes);
- size_t r1;
- if(r0 != 1 || cmpBytes <= 0) break;
+ size_t r1;
+ if(r0 != 1 || cmpBytes <= 0)
+ break;
r1 = read_bin(inpFp, cmpBuf, cmpBytes);
- if(r1 != (size_t) cmpBytes) break;
+ if(r1 != (size_t) cmpBytes)
+ break;
}
{
@@ -112,38 +119,56 @@ void test_decompress(FILE* outFp, FILE* inpFp)
const int decBytes = LZ4_decompress_safe_continue(
lz4StreamDecode, cmpBuf, decPtr, cmpBytes, MESSAGE_MAX_BYTES);
if(decBytes <= 0)
- break;
+ break;
+
+ done += decBytes;
decOffset += decBytes;
write_bin(outFp, decPtr, decBytes);
// Wraparound the ringbuffer offset
- if(decOffset >= RING_BUFFER_BYTES - MESSAGE_MAX_BYTES) decOffset = 0;
+ if(decOffset >= DEC_BUFFER_BYTES - MESSAGE_MAX_BYTES)
+ decOffset = 0;
}
}
}
-int compare(FILE* f0, FILE* f1)
+// Compare 2 files content
+// return 0 if identical
+// return ByteNb>0 if different
+size_t compare(FILE* f0, FILE* f1)
{
- int result = 0;
+ size_t result = 1;
- while(0 == result) {
+ for (;;)
+ {
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);
- result = (int) r0 - (int) r1;
+ if ((r0==0) && (r1==0)) return 0; // success
- if(0 == r0 || 0 == r1) {
- break;
+ if (r0 != r1)
+ {
+ size_t smallest = r0;
+ if (r1<r0) smallest = r1;
+ result += smallest;
+ return result;
}
- if(0 == result) {
- result = memcmp(b0, b1, r0);
+
+ if (memcmp(b0, b1, r0))
+ {
+ unsigned errorPos = 0;
+ while ((errorPos < r0) && (b0[errorPos]==b1[errorPos])) errorPos++;
+ result += errorPos;
+ return result;
}
+
+ result += sizeof(b0);
}
- return result;
+ return 1;
}
@@ -152,15 +177,20 @@ int main(int argc, char** argv)
char inpFilename[256] = { 0 };
char lz4Filename[256] = { 0 };
char decFilename[256] = { 0 };
+ unsigned fileID = 1;
+ unsigned pause = 0;
+
if(argc < 2) {
printf("Please specify input filename\n");
return 0;
}
- snprintf(inpFilename, 256, "%s", argv[1]);
- snprintf(lz4Filename, 256, "%s.lz4s-%d", argv[1], 0);
- snprintf(decFilename, 256, "%s.lz4s-%d.dec", argv[1], 0);
+ 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);
printf("inp = [%s]\n", inpFilename);
printf("lz4 = [%s]\n", lz4Filename);
@@ -193,16 +223,22 @@ int main(int argc, char** argv)
FILE* inpFp = fopen(inpFilename, "rb");
FILE* decFp = fopen(decFilename, "rb");
- const int cmp = compare(inpFp, decFp);
+ const size_t cmp = compare(inpFp, decFp);
if(0 == cmp) {
printf("Verify : OK\n");
} else {
- printf("Verify : NG\n");
+ printf("Verify : NG : error at pos %u\n", (unsigned)cmp-1);
}
fclose(decFp);
fclose(inpFp);
}
+ if (pause)
+ {
+ printf("Press enter to continue ...\n");
+ getchar();
+ }
+
return 0;
}