summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/HCStreaming_ringBuffer.c6
-rw-r--r--examples/blockStreaming_doubleBuffer.c2
-rw-r--r--examples/blockStreaming_lineByLine.c4
-rw-r--r--examples/blockStreaming_ringBuffer.c2
-rw-r--r--examples/dictionaryRandomAccess.c2
-rw-r--r--examples/frameCompress.c105
-rw-r--r--examples/printVersion.c2
7 files changed, 62 insertions, 61 deletions
diff --git a/examples/HCStreaming_ringBuffer.c b/examples/HCStreaming_ringBuffer.c
index d49b267..a878577 100644
--- a/examples/HCStreaming_ringBuffer.c
+++ b/examples/HCStreaming_ringBuffer.c
@@ -1,12 +1,12 @@
// LZ4 HC streaming API example : ring buffer
-// Based on previous work from Takayuki Matsuoka
+// Based on a previous example by Takayuki Matsuoka
/**************************************
* Compiler Options
**************************************/
-#ifdef _MSC_VER /* Visual Studio */
-# define _CRT_SECURE_NO_WARNINGS /* for MSVC */
+#if defined(_MSC_VER) && (_MSC_VER <= 1800) /* Visual Studio <= 2013 */
+# define _CRT_SECURE_NO_WARNINGS
# define snprintf sprintf_s
#endif
diff --git a/examples/blockStreaming_doubleBuffer.c b/examples/blockStreaming_doubleBuffer.c
index d02f258..acb3455 100644
--- a/examples/blockStreaming_doubleBuffer.c
+++ b/examples/blockStreaming_doubleBuffer.c
@@ -2,7 +2,7 @@
// Copyright : Takayuki Matsuoka
-#ifdef _MSC_VER /* Visual Studio */
+#if defined(_MSC_VER) && (_MSC_VER <= 1800) /* Visual Studio <= 2013 */
# define _CRT_SECURE_NO_WARNINGS
# define snprintf sprintf_s
#endif
diff --git a/examples/blockStreaming_lineByLine.c b/examples/blockStreaming_lineByLine.c
index f449aa3..677c426 100644
--- a/examples/blockStreaming_lineByLine.c
+++ b/examples/blockStreaming_lineByLine.c
@@ -1,8 +1,8 @@
// LZ4 streaming API example : line-by-line logfile compression
-// Copyright : Takayuki Matsuoka
+// by Takayuki Matsuoka
-#ifdef _MSC_VER /* Visual Studio */
+#if defined(_MSC_VER) && (_MSC_VER <= 1800) /* Visual Studio <= 2013 */
# define _CRT_SECURE_NO_WARNINGS
# define snprintf sprintf_s
#endif
diff --git a/examples/blockStreaming_ringBuffer.c b/examples/blockStreaming_ringBuffer.c
index dec78af..0b6a3ce 100644
--- a/examples/blockStreaming_ringBuffer.c
+++ b/examples/blockStreaming_ringBuffer.c
@@ -5,7 +5,7 @@
/**************************************
* Compiler Options
**************************************/
-#ifdef _MSC_VER /* Visual Studio */
+#if defined(_MSC_VER) && (_MSC_VER <= 1800) /* Visual Studio <= 2013 */
# define _CRT_SECURE_NO_WARNINGS
# define snprintf sprintf_s
#endif
diff --git a/examples/dictionaryRandomAccess.c b/examples/dictionaryRandomAccess.c
index 6acf99b..291fd08 100644
--- a/examples/dictionaryRandomAccess.c
+++ b/examples/dictionaryRandomAccess.c
@@ -1,6 +1,6 @@
// LZ4 API example : Dictionary Random Access
-#ifdef _MSC_VER /* Visual Studio */
+#if defined(_MSC_VER) && (_MSC_VER <= 1800) /* Visual Studio <= 2013 */
# define _CRT_SECURE_NO_WARNINGS
# define snprintf sprintf_s
#endif
diff --git a/examples/frameCompress.c b/examples/frameCompress.c
index 80db90e..d66a8dc 100644
--- a/examples/frameCompress.c
+++ b/examples/frameCompress.c
@@ -21,17 +21,15 @@ static const LZ4F_preferences_t lz4_preferences = {
};
static size_t compress_file(FILE *in, FILE *out, size_t *size_in, size_t *size_out) {
- LZ4F_errorCode_t r;
+ size_t r=1; /* function result; 1 == error, default (early exit) */
LZ4F_compressionContext_t ctx;
char *src, *buf = NULL;
- size_t size, n, k, count_in = 0, count_out, offset = 0, frame_size;
+ size_t size, count_in = 0, count_out, offset = 0, frame_size;
- r = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION);
- if (LZ4F_isError(r)) {
+ if (LZ4F_isError( LZ4F_createCompressionContext(&ctx, LZ4F_VERSION) )) {
printf("Failed to create context: error %zu\n", r);
return 1;
}
- r = 1; /* function result; 1 == error, by default (early exit) */
src = malloc(BUF_SIZE);
if (!src) {
@@ -40,41 +38,45 @@ static size_t compress_file(FILE *in, FILE *out, size_t *size_in, size_t *size_o
}
frame_size = LZ4F_compressBound(BUF_SIZE, &lz4_preferences);
- size = frame_size + LZ4_HEADER_SIZE + LZ4_FOOTER_SIZE;
+ size = frame_size + LZ4_HEADER_SIZE + LZ4_FOOTER_SIZE;
buf = malloc(size);
if (!buf) {
printf("Not enough memory\n");
goto cleanup;
}
- n = offset = count_out = LZ4F_compressBegin(ctx, buf, size, &lz4_preferences);
- if (LZ4F_isError(n)) {
- printf("Failed to start compression: error %zu\n", n);
- goto cleanup;
+ { size_t const headerSize = LZ4F_compressBegin(ctx, buf, size, &lz4_preferences);
+ if (LZ4F_isError(headerSize)) {
+ printf("Failed to start compression: error %zu\n", headerSize);
+ goto cleanup;
+ }
+ offset = count_out = headerSize;
+ printf("Buffer size is %zu bytes, header size %zu bytes\n", size, headerSize);
}
- printf("Buffer size is %zu bytes, header size %zu bytes\n", size, n);
for (;;) {
- k = fread(src, 1, BUF_SIZE, in);
- if (k == 0)
+ size_t const readSize = fread(src, 1, BUF_SIZE, in);
+ if (readSize == 0)
break;
- count_in += k;
+ count_in += readSize;
- n = LZ4F_compressUpdate(ctx, buf + offset, size - offset, src, k, NULL);
- if (LZ4F_isError(n)) {
- printf("Compression failed: error %zu\n", n);
- goto cleanup;
+ { size_t const compressedSize = LZ4F_compressUpdate(ctx, buf + offset, size - offset, src, readSize, NULL);
+ if (LZ4F_isError(compressedSize)) {
+ printf("Compression failed: error %zu\n", compressedSize);
+ goto cleanup;
+ }
+ offset += compressedSize;
+ count_out += compressedSize;
}
- offset += n;
- count_out += n;
if (size - offset < frame_size + LZ4_FOOTER_SIZE) {
+ size_t writtenSize;
printf("Writing %zu bytes\n", offset);
- k = fwrite(buf, 1, offset, out);
- if (k < offset) {
- if (ferror(out))
+ writtenSize = fwrite(buf, 1, offset, out);
+ if (writtenSize < offset) {
+ if (ferror(out)) /* note : ferror() must follow fwrite */
printf("Write failed\n");
else
printf("Short write\n");
@@ -85,31 +87,31 @@ static size_t compress_file(FILE *in, FILE *out, size_t *size_in, size_t *size_o
}
}
- n = LZ4F_compressEnd(ctx, buf + offset, size - offset, NULL);
- if (LZ4F_isError(n)) {
- printf("Failed to end compression: error %zu\n", n);
- goto cleanup;
+ { size_t const compressedSize = LZ4F_compressEnd(ctx, buf + offset, size - offset, NULL);
+ if (LZ4F_isError(compressedSize)) {
+ printf("Failed to end compression: error %zu\n", compressedSize);
+ goto cleanup;
+ }
+ offset += compressedSize;
+ count_out += compressedSize;
}
- offset += n;
- count_out += n;
printf("Writing %zu bytes\n", offset);
-
- k = fwrite(buf, 1, offset, out);
- if (k < offset) {
- if (ferror(out))
- printf("Write failed\n");
- else
- printf("Short write\n");
- goto cleanup;
- }
+ { size_t const writtenSize = fwrite(buf, 1, offset, out);
+ if (writtenSize < offset) {
+ if (ferror(out))
+ printf("Write failed\n");
+ else
+ printf("Short write\n");
+ goto cleanup;
+ } }
*size_in = count_in;
*size_out = count_out;
- r = 0;
+ r = 0; /* success */
+
cleanup:
- if (ctx)
- LZ4F_freeCompressionContext(ctx);
+ LZ4F_freeCompressionContext(ctx); /* supports free on NULL */
free(src);
free(buf);
return r;
@@ -128,28 +130,27 @@ static size_t get_block_size(const LZ4F_frameInfo_t* info) {
}
}
-static size_t decompress_file(FILE *in, FILE *out) {
+static size_t decompress_file(FILE* in, FILE* out) {
void* const src = malloc(BUF_SIZE);
void* dst = NULL;
size_t dstCapacity = 0;
- LZ4F_dctx *dctx = NULL;
- size_t ret;
+ LZ4F_dctx* dctx = NULL;
+ size_t ret = 1;
/* Initialization */
if (!src) { perror("decompress_file(src)"); goto cleanup; }
- ret = LZ4F_createDecompressionContext(&dctx, 100);
- if (LZ4F_isError(ret)) {
- printf("LZ4F_dctx creation error: %s\n", LZ4F_getErrorName(ret));
- goto cleanup;
- }
+ { size_t const dctxStatus = LZ4F_createDecompressionContext(&dctx, 100);
+ if (LZ4F_isError(dctxStatus)) {
+ printf("LZ4F_dctx creation error: %s\n", LZ4F_getErrorName(dctxStatus));
+ goto cleanup;
+ } }
/* Decompression */
- ret = 1;
while (ret != 0) {
/* Load more input */
size_t srcSize = fread(src, 1, BUF_SIZE, in);
- void* srcPtr = src;
- void* srcEnd = srcPtr + srcSize;
+ const void* srcPtr = src;
+ const void* const srcEnd = srcPtr + srcSize;
if (srcSize == 0 || ferror(in)) {
printf("Decompress: not enough input or error reading file\n");
goto cleanup;
diff --git a/examples/printVersion.c b/examples/printVersion.c
index 8607139..7af318a 100644
--- a/examples/printVersion.c
+++ b/examples/printVersion.c
@@ -1,5 +1,5 @@
// LZ4 trivial example : print Library version number
-// Copyright : Takayuki Matsuoka & Yann Collet
+// by Takayuki Matsuoka
#include <stdio.h>