simple_buffer.c 5.5 KB

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