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