🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.0 KiB

  1. // example1.c - Demonstrates miniz.c's compress() and uncompress() functions (same as zlib's).
  2. // Public domain, May 15 2011, Rich Geldreich, richgel99@gmail.com. See "unlicense" statement at the end of tinfl.c.
  3. #include <stdio.h>
  4. #include "miniz.h"
  5. typedef unsigned char uint8;
  6. typedef unsigned short uint16;
  7. typedef unsigned int uint;
  8. // The string to compress.
  9. static const char *s_pStr = "Good morning Dr. Chandra. This is Hal. I am ready for my first lesson." \
  10. "Good morning Dr. Chandra. This is Hal. I am ready for my first lesson." \
  11. "Good morning Dr. Chandra. This is Hal. I am ready for my first lesson." \
  12. "Good morning Dr. Chandra. This is Hal. I am ready for my first lesson." \
  13. "Good morning Dr. Chandra. This is Hal. I am ready for my first lesson." \
  14. "Good morning Dr. Chandra. This is Hal. I am ready for my first lesson." \
  15. "Good morning Dr. Chandra. This is Hal. I am ready for my first lesson.";
  16. int main(int argc, char *argv[])
  17. {
  18. uint step = 0;
  19. int cmp_status;
  20. uLong src_len = (uLong)strlen(s_pStr);
  21. uLong cmp_len = compressBound(src_len);
  22. uLong uncomp_len = src_len;
  23. uint8 *pCmp, *pUncomp;
  24. uint total_succeeded = 0;
  25. (void)argc, (void)argv;
  26. printf("miniz.c version: %s\n", MZ_VERSION);
  27. do
  28. {
  29. // Allocate buffers to hold compressed and uncompressed data.
  30. pCmp = (mz_uint8 *)malloc((size_t)cmp_len);
  31. pUncomp = (mz_uint8 *)malloc((size_t)src_len);
  32. if ((!pCmp) || (!pUncomp))
  33. {
  34. printf("Out of memory!\n");
  35. return EXIT_FAILURE;
  36. }
  37. // Compress the string.
  38. cmp_status = compress(pCmp, &cmp_len, (const unsigned char *)s_pStr, src_len);
  39. if (cmp_status != Z_OK)
  40. {
  41. printf("compress() failed!\n");
  42. free(pCmp);
  43. free(pUncomp);
  44. return EXIT_FAILURE;
  45. }
  46. printf("Compressed from %u to %u bytes\n", (mz_uint32)src_len, (mz_uint32)cmp_len);
  47. if (step)
  48. {
  49. // Purposely corrupt the compressed data if fuzzy testing (this is a very crude fuzzy test).
  50. uint n = 1 + (rand() % 3);
  51. while (n--)
  52. {
  53. uint i = rand() % cmp_len;
  54. pCmp[i] ^= (rand() & 0xFF);
  55. }
  56. }
  57. // Decompress.
  58. cmp_status = uncompress(pUncomp, &uncomp_len, pCmp, cmp_len);
  59. total_succeeded += (cmp_status == Z_OK);
  60. if (step)
  61. {
  62. printf("Simple fuzzy test: step %u total_succeeded: %u\n", step, total_succeeded);
  63. }
  64. else
  65. {
  66. if (cmp_status != Z_OK)
  67. {
  68. printf("uncompress failed!\n");
  69. free(pCmp);
  70. free(pUncomp);
  71. return EXIT_FAILURE;
  72. }
  73. printf("Decompressed from %u to %u bytes\n", (mz_uint32)cmp_len, (mz_uint32)uncomp_len);
  74. // Ensure uncompress() returned the expected data.
  75. if ((uncomp_len != src_len) || (memcmp(pUncomp, s_pStr, (size_t)src_len)))
  76. {
  77. printf("Decompression failed!\n");
  78. free(pCmp);
  79. free(pUncomp);
  80. return EXIT_FAILURE;
  81. }
  82. }
  83. free(pCmp);
  84. free(pUncomp);
  85. step++;
  86. // Keep on fuzzy testing if there's a non-empty command line.
  87. } while (argc >= 2);
  88. printf("Success.\n");
  89. return EXIT_SUCCESS;
  90. }