test_miniz.c 3.7 KB

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