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