1 /*
2 * simple_buffer.c
3 * Copyright : Kyle Harper
4 * License : Follows same licensing as the lz4.c/lz4.h program at any given time. Currently, BSD 2.
5 * Description: Example program to demonstrate the basic usage of the compress/decompress functions within lz4.c/lz4.h.
6 * The functions you'll likely want are LZ4_compress_default and LZ4_decompress_safe. Both of these are documented in
7 * the lz4.h header file; I recommend reading them.
8 */
9
10 /* Includes, for Power! */
11 #include "lz4.h" // This is all that is required to expose the prototypes for basic compression and decompression.
12 #include <stdio.h> // For printf()
13 #include <string.h> // For memcmp()
14 #include <stdlib.h> // For exit()
15
16 /*
17 * Easy show-error-and-bail function.
18 */
run_screaming(const char * message,const int code)19 void run_screaming(const char *message, const int code) {
20 printf("%s\n", message);
21 exit(code);
22 return;
23 }
24
25
26 /*
27 * main
28 */
main(void)29 int main(void) {
30 /* Introduction */
31 // Below we will have a Compression and Decompression section to demonstrate. There are a few important notes before we start:
32 // 1) The return codes of LZ4_ functions are important. Read lz4.h if you're unsure what a given code means.
33 // 2) LZ4 uses char* pointers in all LZ4_ functions. This is baked into the API and probably not going to change. If your
34 // program uses pointers that are unsigned char*, void*, or otherwise different you may need to do some casting or set the
35 // right -W compiler flags to ignore those warnings (e.g.: -Wno-pointer-sign).
36
37 /* Compression */
38 // We'll store some text into a variable pointed to by *src to be compressed later.
39 const char *src = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
40 // The compression function needs to know how many bytes of exist. Since we're using a string, we can use strlen() + 1 (for \0).
41 const size_t src_size = strlen(src) + 1;
42 // LZ4 provides a function that will tell you the maximum size of compressed output based on input data via LZ4_compressBound().
43 const size_t max_dst_size = LZ4_compressBound(src_size);
44 // We will use that size for our destination boundary when allocating space.
45 char *compressed_data = malloc(max_dst_size);
46 if (compressed_data == NULL)
47 run_screaming("Failed to allocate memory for *compressed_data.", 1);
48 // That's all the information and preparation LZ4 needs to compress *src into *compressed_data. Invoke LZ4_compress_default now
49 // with our size values and pointers to our memory locations. Save the return value for error checking.
50 int return_value = 0;
51 return_value = LZ4_compress_default(src, compressed_data, src_size, max_dst_size);
52 // Check return_value to determine what happened.
53 if (return_value < 0)
54 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);
55 if (return_value == 0)
56 run_screaming("A result of 0 means compression worked, but was stopped because the destination buffer couldn't hold all the information.", 1);
57 if (return_value > 0)
58 printf("We successfully compressed some data!\n");
59 // Not only does a positive return_value mean success, the value returned == the number of bytes required. You can use this to
60 // realloc() *compress_data to free up memory, if desired. We'll do so just to demonstrate the concept.
61 const size_t compressed_data_size = return_value;
62 compressed_data = (char *)realloc(compressed_data, compressed_data_size);
63 if (compressed_data == NULL)
64 run_screaming("Failed to re-alloc memory for compressed_data. Sad :(", 1);
65
66 /* Decompression */
67 // Now that we've successfully compressed the information from *src to *compressed_data, let's do the opposite! We'll create a
68 // *new_src location of size src_size since we know that value.
69 char *new_src = malloc(src_size);
70 if (new_src == NULL)
71 run_screaming("Failed to allocate memory for *new_src.", 1);
72 // The LZ4_decompress_safe function needs to know where the compressed data is, how many bytes long it is, where the new_src
73 // memory location is, and how large the new_src (uncompressed) output will be. Again, save the return_value.
74 return_value = LZ4_decompress_safe(compressed_data, new_src, compressed_data_size, src_size);
75 if (return_value < 0)
76 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);
77 if (return_value == 0)
78 run_screaming("I'm not sure this function can ever return 0. Documentation in lz4.h doesn't indicate so.", 1);
79 if (return_value > 0)
80 printf("We successfully decompressed some data!\n");
81 // Not only does a positive return value mean success, the value returned == the number of bytes read from the compressed_data
82 // stream. I'm not sure there's ever a time you'll need to know this in most cases...
83
84 /* Validation */
85 // We should be able to compare our original *src with our *new_src and be byte-for-byte identical.
86 if (memcmp(src, new_src, src_size) != 0)
87 run_screaming("Validation failed. *src and *new_src are not identical.", 1);
88 printf("Validation done. The string we ended up with is:\n%s\n", new_src);
89 return 0;
90 }
91