summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Collet <cyan@fb.com>2017-06-08 19:51:56 (GMT)
committerYann Collet <cyan@fb.com>2017-06-08 19:51:56 (GMT)
commit775e63ee01af461d20a607f658ca3795ff738157 (patch)
treee8c5022408a9cb7574c3ffd1765e63ca23232825
parent7e15e240aba842020a2f6e86f35e71cbacdf237d (diff)
downloadlz4-775e63ee01af461d20a607f658ca3795ff738157.zip
lz4-775e63ee01af461d20a607f658ca3795ff738157.tar.gz
lz4-775e63ee01af461d20a607f658ca3795ff738157.tar.bz2
refactored simple_buffer.c example (#363)
-rw-r--r--examples/.gitignore1
-rw-r--r--examples/Makefile11
-rw-r--r--examples/simple_buffer.c81
3 files changed, 50 insertions, 43 deletions
diff --git a/examples/.gitignore b/examples/.gitignore
index 3ceb90d..5abeef6 100644
--- a/examples/.gitignore
+++ b/examples/.gitignore
@@ -6,4 +6,5 @@
/ringBufferHC
/lineCompress
/frameCompress
+/simpleBuffer
/*.exe
diff --git a/examples/Makefile b/examples/Makefile
index aad713b..7647f16 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -27,9 +27,10 @@
# kindly provided by Takayuki Matsuoka
# ##########################################################################
-CFLAGS ?= -O3
-CFLAGS += -std=gnu99 -Wall -Wextra -Wundef -Wshadow -Wcast-align -Wstrict-prototypes
-FLAGS := -I../lib $(CPPFLAGS) $(CFLAGS) $(LDFLAGS)
+CPPFLAGS += -I../lib
+CFLAGS ?= -O3
+CFLAGS += -std=gnu99 -Wall -Wextra -Wundef -Wshadow -Wcast-align -Wstrict-prototypes
+FLAGS := $(CPPFLAGS) $(CFLAGS) $(LDFLAGS)
TESTFILE= Makefile
LZ4DIR := ../lib
@@ -48,7 +49,8 @@ endif
default: all
-all: printVersion doubleBuffer dictionaryRandomAccess ringBuffer ringBufferHC lineCompress frameCompress
+all: printVersion doubleBuffer dictionaryRandomAccess ringBuffer ringBufferHC \
+ lineCompress frameCompress simpleBuffer
printVersion: $(LZ4DIR)/lz4.c printVersion.c
$(CC) $(FLAGS) $^ -o $@$(EXT)
@@ -85,6 +87,7 @@ test : all
./ringBufferHC$(EXT) $(TESTFILE)
./lineCompress$(EXT) $(TESTFILE)
./frameCompress$(EXT) $(TESTFILE)
+ ./simpleBuffer$(EXT)
$(LZ4) -vt $(TESTFILE).lz4
clean:
diff --git a/examples/simple_buffer.c b/examples/simple_buffer.c
index f531d48..ee29148 100644
--- a/examples/simple_buffer.c
+++ b/examples/simple_buffer.c
@@ -8,10 +8,10 @@
*/
/* Includes, for Power! */
-#include "lz4.h" // This is all that is required to expose the prototypes for basic compression and decompression.
-#include <stdio.h> // For printf()
-#include <string.h> // For memcmp()
-#include <stdlib.h> // For exit()
+#include "lz4.h" // This is all that is required to expose the prototypes for basic compression and decompression.
+#include <stdio.h> // For printf()
+#include <string.h> // For memcmp()
+#include <stdlib.h> // For exit()
/*
* Easy show-error-and-bail function.
@@ -28,37 +28,39 @@ void run_screaming(const char *message, const int code) {
*/
int main(void) {
/* Introduction */
- // Below we will have a Compression and Decompression section to demonstrate. There are a few important notes before we start:
- // 1) The return codes of LZ4_ functions are important. Read lz4.h if you're unsure what a given code means.
- // 2) LZ4 uses char* pointers in all LZ4_ functions. This is baked into the API and probably not going to change. If your
- // program uses pointers that are unsigned char*, void*, or otherwise different you may need to do some casting or set the
- // right -W compiler flags to ignore those warnings (e.g.: -Wno-pointer-sign).
+ // Below we will have a Compression and Decompression section to demonstrate.
+ // There are a few important notes before we start:
+ // 1) The return codes of LZ4_ functions are important.
+ // Read lz4.h if you're unsure what a given code means.
+ // 2) LZ4 uses char* pointers in all LZ4_ functions.
+ // This is baked into the API and probably not going to change.
+ // If your program uses pointers that are unsigned char*, void*, or otherwise different,
+ // you may need to do some casting or set the right -W compiler flags to ignore those warnings (e.g.: -Wno-pointer-sign).
/* Compression */
// We'll store some text into a variable pointed to by *src to be compressed later.
- const char *src = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
- // The compression function needs to know how many bytes of exist. Since we're using a string, we can use strlen() + 1 (for \0).
- const size_t src_size = strlen(src) + 1;
+ const char* const src = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
+ // The compression function needs to know how many bytes exist. Since we're using a string, we can use strlen() + 1 (for \0).
+ const int src_size = (int)(strlen(src) + 1);
// LZ4 provides a function that will tell you the maximum size of compressed output based on input data via LZ4_compressBound().
- const size_t max_dst_size = LZ4_compressBound(src_size);
+ const int max_dst_size = LZ4_compressBound(src_size);
// We will use that size for our destination boundary when allocating space.
- char *compressed_data = malloc(max_dst_size);
+ char* compressed_data = malloc(max_dst_size);
if (compressed_data == NULL)
run_screaming("Failed to allocate memory for *compressed_data.", 1);
- // That's all the information and preparation LZ4 needs to compress *src into *compressed_data. Invoke LZ4_compress_default now
- // with our size values and pointers to our memory locations. Save the return value for error checking.
- int return_value = 0;
- return_value = LZ4_compress_default(src, compressed_data, src_size, max_dst_size);
+ // That's all the information and preparation LZ4 needs to compress *src into *compressed_data.
+ // Invoke LZ4_compress_default now with our size values and pointers to our memory locations.
+ // Save the return value for error checking.
+ const int compressed_data_size = LZ4_compress_default(src, compressed_data, src_size, max_dst_size);
// Check return_value to determine what happened.
- if (return_value < 0)
- run_screaming("A negative result from LZ4_compress_default indicates a failure trying to compress the data. See exit code (echo $?) for value returned.", return_value);
- if (return_value == 0)
+ if (compressed_data_size < 0)
+ run_screaming("A negative result from LZ4_compress_default indicates a failure trying to compress the data. See exit code (echo $?) for value returned.", compressed_data_size);
+ if (compressed_data_size == 0)
run_screaming("A result of 0 means compression worked, but was stopped because the destination buffer couldn't hold all the information.", 1);
- if (return_value > 0)
+ if (compressed_data_size > 0)
printf("We successfully compressed some data!\n");
- // Not only does a positive return_value mean success, the value returned == the number of bytes required. You can use this to
- // realloc() *compress_data to free up memory, if desired. We'll do so just to demonstrate the concept.
- const size_t compressed_data_size = return_value;
+ // Not only does a positive return_value mean success, the value returned == the number of bytes required.
+ // You can use this to realloc() *compress_data to free up memory, if desired. We'll do so just to demonstrate the concept.
compressed_data = (char *)realloc(compressed_data, compressed_data_size);
if (compressed_data == NULL)
run_screaming("Failed to re-alloc memory for compressed_data. Sad :(", 1);
@@ -66,25 +68,26 @@ int main(void) {
/* Decompression */
// Now that we've successfully compressed the information from *src to *compressed_data, let's do the opposite! We'll create a
// *new_src location of size src_size since we know that value.
- char *new_src = malloc(src_size);
- if (new_src == NULL)
- run_screaming("Failed to allocate memory for *new_src.", 1);
- // The LZ4_decompress_safe function needs to know where the compressed data is, how many bytes long it is, where the new_src
- // memory location is, and how large the new_src (uncompressed) output will be. Again, save the return_value.
- return_value = LZ4_decompress_safe(compressed_data, new_src, compressed_data_size, src_size);
- if (return_value < 0)
- run_screaming("A negative result from LZ4_decompress_fast indicates a failure trying to decompress the data. See exit code (echo $?) for value returned.", return_value);
- if (return_value == 0)
+ char* const regen_buffer = malloc(src_size);
+ if (regen_buffer == NULL)
+ run_screaming("Failed to allocate memory for *regen_buffer.", 1);
+ // The LZ4_decompress_safe function needs to know where the compressed data is, how many bytes long it is,
+ // where the regen_buffer memory location is, and how large regen_buffer (uncompressed) output will be.
+ // Again, save the return_value.
+ const int decompressed_size = LZ4_decompress_safe(compressed_data, regen_buffer, compressed_data_size, src_size);
+ if (decompressed_size < 0)
+ run_screaming("A negative result from LZ4_decompress_safe indicates a failure trying to decompress the data. See exit code (echo $?) for value returned.", decompressed_size);
+ if (decompressed_size == 0)
run_screaming("I'm not sure this function can ever return 0. Documentation in lz4.h doesn't indicate so.", 1);
- if (return_value > 0)
+ if (decompressed_size > 0)
printf("We successfully decompressed some data!\n");
- // Not only does a positive return value mean success, the value returned == the number of bytes read from the compressed_data
- // stream. I'm not sure there's ever a time you'll need to know this in most cases...
+ // Not only does a positive return value mean success,
+ // value returned == number of bytes regenerated from compressed_data stream.
/* Validation */
// We should be able to compare our original *src with our *new_src and be byte-for-byte identical.
- if (memcmp(src, new_src, src_size) != 0)
+ if (memcmp(src, regen_buffer, src_size) != 0)
run_screaming("Validation failed. *src and *new_src are not identical.", 1);
- printf("Validation done. The string we ended up with is:\n%s\n", new_src);
+ printf("Validation done. The string we ended up with is:\n%s\n", regen_buffer);
return 0;
}