1 // example5.c - Demonstrates how to use miniz.c's low-level tdefl_compress() and tinfl_inflate() API's for simple file to file compression/decompression.
2 // The low-level API's are the fastest, make no use of dynamic memory allocation, and are the most flexible functions exposed by miniz.c.
3 // Public domain, April 11 2012, Rich Geldreich, richgel99@gmail.com. See "unlicense" statement at the end of tinfl.c.
4 // For simplicity, this example is limited to files smaller than 4GB, but this is not a limitation of miniz.c.
5 
6 // Purposely disable a whole bunch of stuff this low-level example doesn't use.
7 #define MINIZ_NO_STDIO
8 #define MINIZ_NO_ARCHIVE_APIS
9 #define MINIZ_NO_TIME
10 #define MINIZ_NO_ZLIB_APIS
11 #define MINIZ_NO_MALLOC
12 #include "miniz.h"
13 
14 // Now include stdio.h because this test uses fopen(), etc. (but we still don't want miniz.c's stdio stuff, for testing).
15 #include <stdio.h>
16 #include <limits.h>
17 
18 typedef unsigned char uint8;
19 typedef unsigned short uint16;
20 typedef unsigned int uint;
21 
22 #define my_max(a,b) (((a) > (b)) ? (a) : (b))
23 #define my_min(a,b) (((a) < (b)) ? (a) : (b))
24 
25 // IN_BUF_SIZE is the size of the file read buffer.
26 // IN_BUF_SIZE must be >= 1
27 #define IN_BUF_SIZE (1024*512)
28 static uint8 s_inbuf[IN_BUF_SIZE];
29 
30 // COMP_OUT_BUF_SIZE is the size of the output buffer used during compression.
31 // COMP_OUT_BUF_SIZE must be >= 1 and <= OUT_BUF_SIZE
32 #define COMP_OUT_BUF_SIZE (1024*512)
33 
34 // OUT_BUF_SIZE is the size of the output buffer used during decompression.
35 // OUT_BUF_SIZE must be a power of 2 >= TINFL_LZ_DICT_SIZE (because the low-level decompressor not only writes, but reads from the output buffer as it decompresses)
36 //#define OUT_BUF_SIZE (TINFL_LZ_DICT_SIZE)
37 #define OUT_BUF_SIZE (1024*512)
38 static uint8 s_outbuf[OUT_BUF_SIZE];
39 
40 // tdefl_compressor contains all the state needed by the low-level compressor so it's a pretty big struct (~300k).
41 // This example makes it a global vs. putting it on the stack, of course in real-world usage you'll probably malloc() or new it.
42 tdefl_compressor g_deflator;
43 
main(int argc,char * argv[])44 int main(int argc, char *argv[])
45 {
46    const char *pMode;
47    FILE *pInfile, *pOutfile;
48    uint infile_size;
49    int level = 9;
50    int p = 1;
51    const char *pSrc_filename;
52    const char *pDst_filename;
53    const void *next_in = s_inbuf;
54    size_t avail_in = 0;
55    void *next_out = s_outbuf;
56    size_t avail_out = OUT_BUF_SIZE;
57    size_t total_in = 0, total_out = 0;
58    long file_loc;
59 
60    assert(COMP_OUT_BUF_SIZE <= OUT_BUF_SIZE);
61 
62    printf("miniz.c example5 (demonstrates tinfl/tdefl)\n");
63 
64    if (argc < 4)
65    {
66       printf("File to file compression/decompression using the low-level tinfl/tdefl API's.\n");
67       printf("Usage: example5 [options] [mode:c or d] infile outfile\n");
68       printf("\nModes:\n");
69       printf("c - Compresses file infile to a zlib stream in file outfile\n");
70       printf("d - Decompress zlib stream in file infile to file outfile\n");
71       printf("\nOptions:\n");
72       printf("-l[0-10] - Compression level, higher values are slower, 0 is none.\n");
73       return EXIT_FAILURE;
74    }
75 
76    while ((p < argc) && (argv[p][0] == '-'))
77    {
78       switch (argv[p][1])
79       {
80          case 'l':
81          {
82             level = atoi(&argv[1][2]);
83             if ((level < 0) || (level > 10))
84             {
85                printf("Invalid level!\n");
86                return EXIT_FAILURE;
87             }
88             break;
89          }
90          default:
91          {
92             printf("Invalid option: %s\n", argv[p]);
93             return EXIT_FAILURE;
94          }
95       }
96       p++;
97    }
98 
99    if ((argc - p) < 3)
100    {
101       printf("Must specify mode, input filename, and output filename after options!\n");
102       return EXIT_FAILURE;
103    }
104    else if ((argc - p) > 3)
105    {
106       printf("Too many filenames!\n");
107       return EXIT_FAILURE;
108    }
109 
110    pMode = argv[p++];
111    if (!strchr("cCdD", pMode[0]))
112    {
113       printf("Invalid mode!\n");
114       return EXIT_FAILURE;
115    }
116 
117    pSrc_filename = argv[p++];
118    pDst_filename = argv[p++];
119 
120    printf("Mode: %c, Level: %u\nInput File: \"%s\"\nOutput File: \"%s\"\n", pMode[0], level, pSrc_filename, pDst_filename);
121 
122    // Open input file.
123    pInfile = fopen(pSrc_filename, "rb");
124    if (!pInfile)
125    {
126       printf("Failed opening input file!\n");
127       return EXIT_FAILURE;
128    }
129 
130    // Determine input file's size.
131    fseek(pInfile, 0, SEEK_END);
132    file_loc = ftell(pInfile);
133    fseek(pInfile, 0, SEEK_SET);
134 
135    if ((file_loc < 0) || (file_loc > INT_MAX))
136    {
137       // This is not a limitation of miniz or tinfl, but this example.
138       printf("File is too large to be processed by this example.\n");
139       return EXIT_FAILURE;
140    }
141 
142    infile_size = (uint)file_loc;
143 
144    // Open output file.
145    pOutfile = fopen(pDst_filename, "wb");
146    if (!pOutfile)
147    {
148       printf("Failed opening output file!\n");
149       return EXIT_FAILURE;
150    }
151 
152    printf("Input file size: %u\n", infile_size);
153 
154    if ((pMode[0] == 'c') || (pMode[0] == 'C'))
155    {
156       // The number of dictionary probes to use at each compression level (0-10). 0=implies fastest/minimal possible probing.
157       static const mz_uint s_tdefl_num_probes[11] = { 0, 1, 6, 32,  16, 32, 128, 256,  512, 768, 1500 };
158 
159       tdefl_status status;
160       uint infile_remaining = infile_size;
161 
162       // create tdefl() compatible flags (we have to compose the low-level flags ourselves, or use tdefl_create_comp_flags_from_zip_params() but that means MINIZ_NO_ZLIB_APIS can't be defined).
163       mz_uint comp_flags = TDEFL_WRITE_ZLIB_HEADER | s_tdefl_num_probes[MZ_MIN(10, level)] | ((level <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0);
164       if (!level)
165          comp_flags |= TDEFL_FORCE_ALL_RAW_BLOCKS;
166 
167       // Initialize the low-level compressor.
168       status = tdefl_init(&g_deflator, NULL, NULL, comp_flags);
169       if (status != TDEFL_STATUS_OKAY)
170       {
171          printf("tdefl_init() failed!\n");
172          return EXIT_FAILURE;
173       }
174 
175       avail_out = COMP_OUT_BUF_SIZE;
176 
177       // Compression.
178       for ( ; ; )
179       {
180          size_t in_bytes, out_bytes;
181 
182          if (!avail_in)
183          {
184             // Input buffer is empty, so read more bytes from input file.
185             uint n = my_min(IN_BUF_SIZE, infile_remaining);
186 
187             if (fread(s_inbuf, 1, n, pInfile) != n)
188             {
189                printf("Failed reading from input file!\n");
190                return EXIT_FAILURE;
191             }
192 
193             next_in = s_inbuf;
194             avail_in = n;
195 
196             infile_remaining -= n;
197             //printf("Input bytes remaining: %u\n", infile_remaining);
198          }
199 
200          in_bytes = avail_in;
201          out_bytes = avail_out;
202          // Compress as much of the input as possible (or all of it) to the output buffer.
203          status = tdefl_compress(&g_deflator, next_in, &in_bytes, next_out, &out_bytes, infile_remaining ? TDEFL_NO_FLUSH : TDEFL_FINISH);
204 
205          next_in = (const char *)next_in + in_bytes;
206          avail_in -= in_bytes;
207          total_in += in_bytes;
208 
209          next_out = (char *)next_out + out_bytes;
210          avail_out -= out_bytes;
211          total_out += out_bytes;
212 
213          if ((status != TDEFL_STATUS_OKAY) || (!avail_out))
214          {
215             // Output buffer is full, or compression is done or failed, so write buffer to output file.
216             uint n = COMP_OUT_BUF_SIZE - (uint)avail_out;
217             if (fwrite(s_outbuf, 1, n, pOutfile) != n)
218             {
219                printf("Failed writing to output file!\n");
220                return EXIT_FAILURE;
221             }
222             next_out = s_outbuf;
223             avail_out = COMP_OUT_BUF_SIZE;
224          }
225 
226          if (status == TDEFL_STATUS_DONE)
227          {
228             // Compression completed successfully.
229             break;
230          }
231          else if (status != TDEFL_STATUS_OKAY)
232          {
233             // Compression somehow failed.
234             printf("tdefl_compress() failed with status %i!\n", status);
235             return EXIT_FAILURE;
236          }
237       }
238    }
239    else if ((pMode[0] == 'd') || (pMode[0] == 'D'))
240    {
241       // Decompression.
242       uint infile_remaining = infile_size;
243 
244       tinfl_decompressor inflator;
245       tinfl_init(&inflator);
246 
247       for ( ; ; )
248       {
249          size_t in_bytes, out_bytes;
250          tinfl_status status;
251          if (!avail_in)
252          {
253             // Input buffer is empty, so read more bytes from input file.
254             uint n = my_min(IN_BUF_SIZE, infile_remaining);
255 
256             if (fread(s_inbuf, 1, n, pInfile) != n)
257             {
258                printf("Failed reading from input file!\n");
259                return EXIT_FAILURE;
260             }
261 
262             next_in = s_inbuf;
263             avail_in = n;
264 
265             infile_remaining -= n;
266          }
267 
268          in_bytes = avail_in;
269          out_bytes = avail_out;
270          status = tinfl_decompress(&inflator, (const mz_uint8 *)next_in, &in_bytes, s_outbuf, (mz_uint8 *)next_out, &out_bytes, (infile_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0) | TINFL_FLAG_PARSE_ZLIB_HEADER);
271 
272          avail_in -= in_bytes;
273          next_in = (const mz_uint8 *)next_in + in_bytes;
274          total_in += in_bytes;
275 
276          avail_out -= out_bytes;
277          next_out = (mz_uint8 *)next_out + out_bytes;
278          total_out += out_bytes;
279 
280          if ((status <= TINFL_STATUS_DONE) || (!avail_out))
281          {
282             // Output buffer is full, or decompression is done, so write buffer to output file.
283             uint n = OUT_BUF_SIZE - (uint)avail_out;
284             if (fwrite(s_outbuf, 1, n, pOutfile) != n)
285             {
286                printf("Failed writing to output file!\n");
287                return EXIT_FAILURE;
288             }
289             next_out = s_outbuf;
290             avail_out = OUT_BUF_SIZE;
291          }
292 
293          // If status is <= TINFL_STATUS_DONE then either decompression is done or something went wrong.
294          if (status <= TINFL_STATUS_DONE)
295          {
296             if (status == TINFL_STATUS_DONE)
297             {
298                // Decompression completed successfully.
299                break;
300             }
301             else
302             {
303                // Decompression failed.
304                printf("tinfl_decompress() failed with status %i!\n", status);
305                return EXIT_FAILURE;
306             }
307          }
308       }
309    }
310    else
311    {
312       printf("Invalid mode!\n");
313       return EXIT_FAILURE;
314    }
315 
316    fclose(pInfile);
317    if (EOF == fclose(pOutfile))
318    {
319       printf("Failed writing to output file!\n");
320       return EXIT_FAILURE;
321    }
322 
323    printf("Total input bytes: %u\n", (mz_uint32)total_in);
324    printf("Total output bytes: %u\n", (mz_uint32)total_out);
325    printf("Success.\n");
326    return EXIT_SUCCESS;
327 }
328