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