127b27ec6Sopenharmony_ci/*
227b27ec6Sopenharmony_ci * simple_buffer.c
327b27ec6Sopenharmony_ci * Copyright  : Kyle Harper
427b27ec6Sopenharmony_ci * License    : Follows same licensing as the lz4.c/lz4.h program at any given time.  Currently, BSD 2.
527b27ec6Sopenharmony_ci * Description: Example program to demonstrate the basic usage of the compress/decompress functions within lz4.c/lz4.h.
627b27ec6Sopenharmony_ci *              The functions you'll likely want are LZ4_compress_default and LZ4_decompress_safe.
727b27ec6Sopenharmony_ci *              Both of these are documented in the lz4.h header file; I recommend reading them.
827b27ec6Sopenharmony_ci */
927b27ec6Sopenharmony_ci
1027b27ec6Sopenharmony_ci/* Dependencies */
1127b27ec6Sopenharmony_ci#include <stdio.h>   // For printf()
1227b27ec6Sopenharmony_ci#include <string.h>  // For memcmp()
1327b27ec6Sopenharmony_ci#include <stdlib.h>  // For exit()
1427b27ec6Sopenharmony_ci#include "lz4.h"     // This is all that is required to expose the prototypes for basic compression and decompression.
1527b27ec6Sopenharmony_ci
1627b27ec6Sopenharmony_ci/*
1727b27ec6Sopenharmony_ci * Simple show-error-and-bail function.
1827b27ec6Sopenharmony_ci */
1927b27ec6Sopenharmony_civoid run_screaming(const char* message, const int code) {
2027b27ec6Sopenharmony_ci  printf("%s \n", message);
2127b27ec6Sopenharmony_ci  exit(code);
2227b27ec6Sopenharmony_ci}
2327b27ec6Sopenharmony_ci
2427b27ec6Sopenharmony_ci
2527b27ec6Sopenharmony_ci/*
2627b27ec6Sopenharmony_ci * main
2727b27ec6Sopenharmony_ci */
2827b27ec6Sopenharmony_ciint main(void) {
2927b27ec6Sopenharmony_ci  /* Introduction */
3027b27ec6Sopenharmony_ci  // Below we will have a Compression and Decompression section to demonstrate.
3127b27ec6Sopenharmony_ci  // There are a few important notes before we start:
3227b27ec6Sopenharmony_ci  //   1) The return codes of LZ4_ functions are important.
3327b27ec6Sopenharmony_ci  //      Read lz4.h if you're unsure what a given code means.
3427b27ec6Sopenharmony_ci  //   2) LZ4 uses char* pointers in all LZ4_ functions.
3527b27ec6Sopenharmony_ci  //      This is baked into the API and not going to change, for consistency.
3627b27ec6Sopenharmony_ci  //      If your program uses different pointer types,
3727b27ec6Sopenharmony_ci  //      you may need to do some casting or set the right -Wno compiler flags to ignore those warnings (e.g.: -Wno-pointer-sign).
3827b27ec6Sopenharmony_ci
3927b27ec6Sopenharmony_ci  /* Compression */
4027b27ec6Sopenharmony_ci  // We'll store some text into a variable pointed to by *src to be compressed later.
4127b27ec6Sopenharmony_ci  const char* const src = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor site amat.";
4227b27ec6Sopenharmony_ci  // The compression function needs to know how many bytes exist.  Since we're using a string, we can use strlen() + 1 (for \0).
4327b27ec6Sopenharmony_ci  const int src_size = (int)(strlen(src) + 1);
4427b27ec6Sopenharmony_ci  // LZ4 provides a function that will tell you the maximum size of compressed output based on input data via LZ4_compressBound().
4527b27ec6Sopenharmony_ci  const int max_dst_size = LZ4_compressBound(src_size);
4627b27ec6Sopenharmony_ci  // We will use that size for our destination boundary when allocating space.
4727b27ec6Sopenharmony_ci  char* compressed_data = (char*)malloc((size_t)max_dst_size);
4827b27ec6Sopenharmony_ci  if (compressed_data == NULL)
4927b27ec6Sopenharmony_ci    run_screaming("Failed to allocate memory for *compressed_data.", 1);
5027b27ec6Sopenharmony_ci  // That's all the information and preparation LZ4 needs to compress *src into* compressed_data.
5127b27ec6Sopenharmony_ci  // Invoke LZ4_compress_default now with our size values and pointers to our memory locations.
5227b27ec6Sopenharmony_ci  // Save the return value for error checking.
5327b27ec6Sopenharmony_ci  const int compressed_data_size = LZ4_compress_default(src, compressed_data, src_size, max_dst_size);
5427b27ec6Sopenharmony_ci  // Check return_value to determine what happened.
5527b27ec6Sopenharmony_ci  if (compressed_data_size <= 0)
5627b27ec6Sopenharmony_ci    run_screaming("A 0 or negative result from LZ4_compress_default() indicates a failure trying to compress the data. ", 1);
5727b27ec6Sopenharmony_ci  if (compressed_data_size > 0)
5827b27ec6Sopenharmony_ci    printf("We successfully compressed some data! Ratio: %.2f\n",
5927b27ec6Sopenharmony_ci        (float) compressed_data_size/src_size);
6027b27ec6Sopenharmony_ci  // Not only does a positive return_value mean success, the value returned == the number of bytes required.
6127b27ec6Sopenharmony_ci  // You can use this to realloc() *compress_data to free up memory, if desired.  We'll do so just to demonstrate the concept.
6227b27ec6Sopenharmony_ci  compressed_data = (char *)realloc(compressed_data, (size_t)compressed_data_size);
6327b27ec6Sopenharmony_ci  if (compressed_data == NULL)
6427b27ec6Sopenharmony_ci    run_screaming("Failed to re-alloc memory for compressed_data.  Sad :(", 1);
6527b27ec6Sopenharmony_ci
6627b27ec6Sopenharmony_ci
6727b27ec6Sopenharmony_ci  /* Decompression */
6827b27ec6Sopenharmony_ci  // Now that we've successfully compressed the information from *src to *compressed_data, let's do the opposite!
6927b27ec6Sopenharmony_ci  // The decompression will need to know the compressed size, and an upper bound of the decompressed size.
7027b27ec6Sopenharmony_ci  // In this example, we just re-use this information from previous section,
7127b27ec6Sopenharmony_ci  // but in a real-world scenario, metadata must be transmitted to the decompression side.
7227b27ec6Sopenharmony_ci  // Each implementation is in charge of this part. Oftentimes, it adds some header of its own.
7327b27ec6Sopenharmony_ci  // Sometimes, the metadata can be extracted from the local context.
7427b27ec6Sopenharmony_ci
7527b27ec6Sopenharmony_ci  // First, let's create a *new_src location of size src_size since we know that value.
7627b27ec6Sopenharmony_ci  char* const regen_buffer = (char*)malloc(src_size);
7727b27ec6Sopenharmony_ci  if (regen_buffer == NULL)
7827b27ec6Sopenharmony_ci    run_screaming("Failed to allocate memory for *regen_buffer.", 1);
7927b27ec6Sopenharmony_ci  // The LZ4_decompress_safe function needs to know where the compressed data is, how many bytes long it is,
8027b27ec6Sopenharmony_ci  // where the regen_buffer memory location is, and how large regen_buffer (uncompressed) output will be.
8127b27ec6Sopenharmony_ci  // Again, save the return_value.
8227b27ec6Sopenharmony_ci  const int decompressed_size = LZ4_decompress_safe(compressed_data, regen_buffer, compressed_data_size, src_size);
8327b27ec6Sopenharmony_ci  free(compressed_data);   /* no longer useful */
8427b27ec6Sopenharmony_ci  if (decompressed_size < 0)
8527b27ec6Sopenharmony_ci    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);
8627b27ec6Sopenharmony_ci  if (decompressed_size >= 0)
8727b27ec6Sopenharmony_ci    printf("We successfully decompressed some data!\n");
8827b27ec6Sopenharmony_ci  // Not only does a positive return value mean success,
8927b27ec6Sopenharmony_ci  // value returned == number of bytes regenerated from compressed_data stream.
9027b27ec6Sopenharmony_ci  if (decompressed_size != src_size)
9127b27ec6Sopenharmony_ci    run_screaming("Decompressed data is different from original! \n", 1);
9227b27ec6Sopenharmony_ci
9327b27ec6Sopenharmony_ci  /* Validation */
9427b27ec6Sopenharmony_ci  // We should be able to compare our original *src with our *new_src and be byte-for-byte identical.
9527b27ec6Sopenharmony_ci  if (memcmp(src, regen_buffer, src_size) != 0)
9627b27ec6Sopenharmony_ci    run_screaming("Validation failed.  *src and *new_src are not identical.", 1);
9727b27ec6Sopenharmony_ci  printf("Validation done. The string we ended up with is:\n%s\n", regen_buffer);
9827b27ec6Sopenharmony_ci  return 0;
9927b27ec6Sopenharmony_ci}
100