1 // miniz_tester.cpp
2 // Note: This module is not intended to make a good example, or be used for anything other than testing.
3 // It's something quick I put together last year to help regression test miniz/tinfl under Linux/Win32/Mac. It's derived from LZHAM's test module.
4 #ifdef _MSC_VER
5 #pragma warning (disable:4127) // warning C4127: conditional expression is constant
6 #endif
7
8 #if defined(__GNUC__)
9 // Ensure we get the 64-bit variants of the CRT's file I/O calls
10 #ifndef _FILE_OFFSET_BITS
11 #define _FILE_OFFSET_BITS 64
12 #endif
13 #ifndef _LARGEFILE64_SOURCE
14 #define _LARGEFILE64_SOURCE 1
15 #endif
16 #endif
17
18 #include "miniz.h"
19 #include "miniz_zip.h"
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <assert.h>
24 #include <memory.h>
25 #include <stdarg.h>
26 #include <malloc.h>
27 #include <vector>
28 #include <string>
29 #include <limits.h>
30 #include <sys/stat.h>
31
32 #include "timer.h"
33
34 #define my_max(a,b) (((a) > (b)) ? (a) : (b))
35 #define my_min(a,b) (((a) < (b)) ? (a) : (b))
36
37 typedef unsigned char uint8;
38 typedef unsigned short uint16;
39 typedef unsigned int uint;
40
41 #define TDEFL_PRINT_OUTPUT_PROGRESS
42
43 #if defined(WIN32)
44 #define WIN32_LEAN_AND_MEAN
45 #include <windows.h>
46 #define FILE_STAT_STRUCT _stat
47 #define FILE_STAT _stat
48 #else
49 #include <unistd.h>
50 #define Sleep(ms) usleep(ms*1000)
51 #define _aligned_malloc(size, alignment) memalign(alignment, size)
52 #define _aligned_free free
53 #define fopen fopen64
54 #define _fseeki64 fseeko64
55 #define _ftelli64 ftello64
56 #define _stricmp strcasecmp
57 #define FILE_STAT_STRUCT stat64
58 #define FILE_STAT stat64
59 #endif
60
61 #ifdef WIN32
62 #define QUAD_INT_FMT "%I64u"
63 #else
64 #define QUAD_INT_FMT "%llu"
65 #endif
66
67 #ifdef _DEBUG
68 const bool g_is_debug = true;
69 #else
70 const bool g_is_debug = false;
71 #endif
72
73 typedef unsigned char uint8;
74 typedef unsigned int uint;
75 typedef unsigned int uint32;
76 typedef unsigned long long uint64;
77 typedef long long int64;
78
79 #define TDEFLTEST_COMP_INPUT_BUFFER_SIZE 1024*1024*2
80 #define TDEFLTEST_COMP_OUTPUT_BUFFER_SIZE 1024*1024*2
81 #define TDEFLTEST_DECOMP_INPUT_BUFFER_SIZE 1024*1024*2
82
83 static float s_max_small_comp_ratio, s_max_large_comp_ratio;
84
85 struct comp_options
86 {
comp_optionscomp_options87 comp_options() :
88 m_level(7),
89 m_unbuffered_decompression(false),
90 m_verify_compressed_data(false),
91 m_randomize_params(false),
92 m_randomize_buffer_sizes(false),
93 m_z_strat(Z_DEFAULT_STRATEGY),
94 m_random_z_flushing(false),
95 m_write_zlib_header(true),
96 m_archive_test(false),
97 m_write_archives(false)
98 {
99 }
100
printcomp_options101 void print()
102 {
103 printf("Level: %u\n", m_level);
104 printf("Write zlib header: %u\n", (uint)m_write_zlib_header);
105 printf("Unbuffered decompression: %u\n", (uint)m_unbuffered_decompression);
106 printf("Verify compressed data: %u\n", (uint)m_verify_compressed_data);
107 printf("Randomize parameters: %u\n", m_randomize_params);
108 printf("Randomize buffer sizes: %u\n", m_randomize_buffer_sizes);
109 printf("Deflate strategy: %u\n", m_z_strat);
110 printf("Random Z stream flushing: %u\n", m_random_z_flushing);
111 printf("Archive test: %u\n", m_archive_test);
112 printf("Write archives: %u\n", m_write_archives);
113 }
114
115 uint m_level;
116 bool m_unbuffered_decompression;
117 bool m_verify_compressed_data;
118 bool m_randomize_params;
119 bool m_randomize_buffer_sizes;
120 uint m_z_strat;
121 bool m_random_z_flushing;
122 bool m_write_zlib_header;
123 bool m_archive_test;
124 bool m_write_archives;
125 };
126
127 #define RND_SHR3(x) (x ^= (x << 17), x ^= (x >> 13), x ^= (x << 5))
128
129 #if 0
130 static void random_fill(uint8 *pDst, size_t len, uint32 x)
131 {
132 x ^= (x << 16);
133 if (!x) x++;
134
135 while (len)
136 {
137 RND_SHR3(x); uint64 l0 = x & 0xFFF;
138 RND_SHR3(x); uint64 l1 = x & 0xFFF;
139 RND_SHR3(x); uint64 l2 = x & 0xFFF;
140 RND_SHR3(x); uint c = x;
141
142 uint l = (uint)(((l0*l1*l2)/(16769025ULL) * 32) / 4095);
143 l = (uint)my_max(1,my_min(l, len));
144 len -= l;
145
146 while (l--)
147 {
148 *pDst++ = (uint8)c;
149 }
150
151 if (((int)x < 0) && len)
152 {
153 *pDst++ = 0;
154 len--;
155 }
156 }
157 }
158 #endif
159
print_usage()160 static void print_usage()
161 {
162 printf("Usage: [options] [mode] inpath/infile [outfile]\n");
163 printf("\n");
164 printf("Modes:\n");
165 printf("c - Compress \"infile\" to \"outfile\"\n");
166 printf("d - Decompress \"infile\" to \"outfile\"\n");
167 printf("a - Recursively compress all files under \"inpath\"\n");
168 printf("r - Archive decompression test\n");
169 printf("\n");
170 printf("Options:\n");
171 printf("-m[0-10] - Compression level: 0=fastest (Huffman only), 9=best (10=uber)\n");
172 printf("-u - Use unbuffered decompression on files that can fit into memory.\n");
173 printf(" Unbuffered decompression is faster, but may have more I/O overhead.\n");
174 printf("-v - Immediately decompress compressed file after compression for verification.\n");
175 printf("-z - Do not write zlib header\n");
176 printf("-r - Randomize parameters during recursive testing\n");
177 printf("-b - Randomize input/output buffer sizes\n");
178 printf("-h - Use random z_flushing\n");
179 printf("-x# - Set rand() seed to value\n");
180 printf("-t# - Set z_strategy to value [0-4]\n");
181 printf("-a - Create single-file archives instead of files during testing\n");
182 printf("-w - Test archive cloning\n");
183 }
184
print_error(const char * pMsg,...)185 static void print_error(const char *pMsg, ...)
186 {
187 char buf[1024];
188
189 va_list args;
190 va_start(args, pMsg);
191 vsnprintf(buf, sizeof(buf), pMsg, args);
192 va_end(args);
193
194 buf[sizeof(buf) - 1] = '\0';
195
196 fprintf(stderr, "Error: %s", buf);
197 }
198
open_file_with_retries(const char * pFilename,const char * pMode)199 static FILE* open_file_with_retries(const char *pFilename, const char* pMode)
200 {
201 const uint cNumRetries = 8;
202 for (uint i = 0; i < cNumRetries; i++)
203 {
204 FILE* pFile = fopen(pFilename, pMode);
205 if (pFile)
206 return pFile;
207 Sleep(250);
208 }
209 return NULL;
210 }
211
ensure_file_exists_and_is_readable(const char * pFilename)212 static bool ensure_file_exists_and_is_readable(const char *pFilename)
213 {
214 FILE *p = fopen(pFilename, "rb");
215 if (!p)
216 return false;
217
218 _fseeki64(p, 0, SEEK_END);
219 uint64 src_file_size = _ftelli64(p);
220 _fseeki64(p, 0, SEEK_SET);
221
222 if (src_file_size)
223 {
224 char buf[1];
225 if (fread(buf, 1, 1, p) != 1)
226 {
227 fclose(p);
228 return false;
229 }
230 }
231 fclose(p);
232 return true;
233 }
234
ensure_file_is_writable(const char * pFilename)235 static bool ensure_file_is_writable(const char *pFilename)
236 {
237 const int cNumRetries = 8;
238 for (int i = 0; i < cNumRetries; i++)
239 {
240 FILE *pFile = fopen(pFilename, "wb");
241 if (pFile)
242 {
243 fclose(pFile);
244 return true;
245 }
246 Sleep(250);
247 }
248 return false;
249 }
250
simple_test1(const comp_options & options)251 static int simple_test1(const comp_options &options)
252 {
253 (void)options;
254
255 size_t cmp_len = 0;
256
257 const char *p = "This is a test.This is a test.This is a test.1234567This is a test.This is a test.123456";
258 size_t uncomp_len = strlen(p);
259
260 void *pComp_data = tdefl_compress_mem_to_heap(p, uncomp_len, &cmp_len, TDEFL_WRITE_ZLIB_HEADER);
261 if (!pComp_data)
262 {
263 free(pComp_data);
264 print_error("Compression test failed!\n");
265 return EXIT_FAILURE;
266 }
267
268 printf("Uncompressed size: %u\nCompressed size: %u\n", (uint)uncomp_len, (uint)cmp_len);
269
270 size_t decomp_len = 0;
271 void *pDecomp_data = tinfl_decompress_mem_to_heap(pComp_data, cmp_len, &decomp_len, TINFL_FLAG_PARSE_ZLIB_HEADER);
272
273 if ((!pDecomp_data) || (decomp_len != uncomp_len) || (memcmp(pDecomp_data, p, uncomp_len)))
274 {
275 free(pComp_data);
276 free(pDecomp_data);
277 print_error("Compression test failed!\n");
278 return EXIT_FAILURE;
279 }
280
281 printf("Low-level API compression test succeeded.\n");
282
283 free(pComp_data);
284 free(pDecomp_data);
285
286 return EXIT_SUCCESS;
287 }
288
simple_test2(const comp_options & options)289 static int simple_test2(const comp_options &options)
290 {
291 (void)options;
292
293 uint8 cmp_buf[1024], decomp_buf[1024];
294 uLong cmp_len = sizeof(cmp_buf);
295
296 const char *p = "This is a test.This is a test.This is a test.1234567This is a test.This is a test.123456";
297 uLong uncomp_len = (uLong)strlen(p);
298
299 int status = compress(cmp_buf, &cmp_len, (const uint8*)p, uncomp_len);
300 if (status != Z_OK)
301 {
302 print_error("Compression test failed!\n");
303 return EXIT_FAILURE;
304 }
305
306 printf("Uncompressed size: %u\nCompressed size: %u\n", (uint)uncomp_len, (uint)cmp_len);
307
308 if (cmp_len > compressBound(uncomp_len))
309 {
310 print_error("compressBound() returned bogus result\n");
311 return EXIT_FAILURE;
312 }
313
314 uLong decomp_len = sizeof(decomp_buf);
315 status = uncompress(decomp_buf, &decomp_len, cmp_buf, cmp_len);;
316
317 if ((status != Z_OK) || (decomp_len != uncomp_len) || (memcmp(decomp_buf, p, uncomp_len)))
318 {
319 print_error("Compression test failed!\n");
320 return EXIT_FAILURE;
321 }
322
323 printf("zlib API compression test succeeded.\n");
324
325 return EXIT_SUCCESS;
326 }
327
compress_file_zlib(const char * pSrc_filename,const char * pDst_filename,const comp_options & options)328 static bool compress_file_zlib(const char* pSrc_filename, const char *pDst_filename, const comp_options &options)
329 {
330 printf("Testing: Streaming zlib compression\n");
331
332 FILE *pInFile = fopen(pSrc_filename, "rb");
333 if (!pInFile)
334 {
335 print_error("Unable to read file: %s\n", pSrc_filename);
336 return false;
337 }
338
339 FILE *pOutFile = fopen(pDst_filename, "wb");
340 if (!pOutFile)
341 {
342 print_error("Unable to create file: %s\n", pDst_filename);
343 return false;
344 }
345
346 _fseeki64(pInFile, 0, SEEK_END);
347 uint64 src_file_size = _ftelli64(pInFile);
348 _fseeki64(pInFile, 0, SEEK_SET);
349
350 fputc('D', pOutFile); fputc('E', pOutFile); fputc('F', pOutFile); fputc('0', pOutFile);
351 fputc(options.m_write_zlib_header, pOutFile);
352
353 for (uint i = 0; i < 8; i++)
354 fputc(static_cast<int>((src_file_size >> (i * 8)) & 0xFF), pOutFile);
355
356 uint cInBufSize = TDEFLTEST_COMP_INPUT_BUFFER_SIZE;
357 uint cOutBufSize = TDEFLTEST_COMP_OUTPUT_BUFFER_SIZE;
358 if (options.m_randomize_buffer_sizes)
359 {
360 cInBufSize = 1 + (rand() % 4096);
361 cOutBufSize = 1 + (rand() % 4096);
362 }
363 printf("Input buffer size: %u, Output buffer size: %u\n", cInBufSize, cOutBufSize);
364
365 uint8 *in_file_buf = static_cast<uint8*>(_aligned_malloc(cInBufSize, 16));
366 uint8 *out_file_buf = static_cast<uint8*>(_aligned_malloc(cOutBufSize, 16));
367 if ((!in_file_buf) || (!out_file_buf))
368 {
369 print_error("Out of memory!\n");
370 _aligned_free(in_file_buf);
371 _aligned_free(out_file_buf);
372 fclose(pInFile);
373 fclose(pOutFile);
374 return false;
375 }
376
377 uint64 src_bytes_left = src_file_size;
378
379 uint in_file_buf_size = 0;
380 uint in_file_buf_ofs = 0;
381
382 uint64 total_output_bytes = 0;
383
384 timer_ticks start_time = timer::get_ticks();
385
386 z_stream zstream;
387 memset(&zstream, 0, sizeof(zstream));
388
389 timer_ticks init_start_time = timer::get_ticks();
390 int status = deflateInit2(&zstream, options.m_level, Z_DEFLATED, options.m_write_zlib_header ? Z_DEFAULT_WINDOW_BITS : -Z_DEFAULT_WINDOW_BITS, 9, options.m_z_strat);
391 timer_ticks total_init_time = timer::get_ticks() - init_start_time;
392
393 if (status != Z_OK)
394 {
395 print_error("Failed initializing compressor!\n");
396 _aligned_free(in_file_buf);
397 _aligned_free(out_file_buf);
398 fclose(pInFile);
399 fclose(pOutFile);
400 return false;
401 }
402
403 printf("deflateInit2() took %3.3fms\n", timer::ticks_to_secs(total_init_time)*1000.0f);
404
405 uint32 x = my_max(1, (uint32)(src_file_size ^ (src_file_size >> 32)));
406
407 for ( ; ; )
408 {
409 if (src_file_size)
410 {
411 double total_elapsed_time = timer::ticks_to_secs(timer::get_ticks() - start_time);
412 double total_bytes_processed = static_cast<double>(src_file_size - src_bytes_left);
413 double comp_rate = (total_elapsed_time > 0.0f) ? total_bytes_processed / total_elapsed_time : 0.0f;
414
415 #ifdef TDEFL_PRINT_OUTPUT_PROGRESS
416 for (int i = 0; i < 15; i++)
417 printf("\b\b\b\b");
418 printf("Progress: %3.1f%%, Bytes Remaining: %3.1fMB, %3.3fMB/sec", (1.0f - (static_cast<float>(src_bytes_left) / src_file_size)) * 100.0f, src_bytes_left / 1048576.0f, comp_rate / (1024.0f * 1024.0f));
419 printf(" \b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
420 #endif
421 }
422
423 if (in_file_buf_ofs == in_file_buf_size)
424 {
425 in_file_buf_size = static_cast<uint>(my_min(cInBufSize, src_bytes_left));
426
427 if (fread(in_file_buf, 1, in_file_buf_size, pInFile) != in_file_buf_size)
428 {
429 printf("\n");
430 print_error("Failure reading from source file!\n");
431 _aligned_free(in_file_buf);
432 _aligned_free(out_file_buf);
433 fclose(pInFile);
434 fclose(pOutFile);
435 deflateEnd(&zstream);
436 return false;
437 }
438
439 src_bytes_left -= in_file_buf_size;
440
441 in_file_buf_ofs = 0;
442 }
443
444 zstream.next_in = &in_file_buf[in_file_buf_ofs];
445 zstream.avail_in = in_file_buf_size - in_file_buf_ofs;
446 zstream.next_out = out_file_buf;
447 zstream.avail_out = cOutBufSize;
448
449 int flush = !src_bytes_left ? Z_FINISH : Z_NO_FLUSH;
450 if ((flush == Z_NO_FLUSH) && (options.m_random_z_flushing))
451 {
452 RND_SHR3(x);
453 if ((x & 15) == 0)
454 {
455 RND_SHR3(x);
456 flush = (x & 31) ? Z_SYNC_FLUSH : Z_FULL_FLUSH;
457 }
458 }
459 status = deflate(&zstream, flush);
460
461 uint num_in_bytes = (in_file_buf_size - in_file_buf_ofs) - zstream.avail_in;
462 uint num_out_bytes = cOutBufSize - zstream.avail_out;
463 if (num_in_bytes)
464 {
465 in_file_buf_ofs += (uint)num_in_bytes;
466 assert(in_file_buf_ofs <= in_file_buf_size);
467 }
468
469 if (num_out_bytes)
470 {
471 if (fwrite(out_file_buf, 1, static_cast<uint>(num_out_bytes), pOutFile) != num_out_bytes)
472 {
473 printf("\n");
474 print_error("Failure writing to destination file!\n");
475 _aligned_free(in_file_buf);
476 _aligned_free(out_file_buf);
477 fclose(pInFile);
478 fclose(pOutFile);
479 deflateEnd(&zstream);
480 return false;
481 }
482
483 total_output_bytes += num_out_bytes;
484 }
485
486 if (status != Z_OK)
487 break;
488 }
489
490 #ifdef TDEFL_PRINT_OUTPUT_PROGRESS
491 for (int i = 0; i < 15; i++)
492 {
493 printf("\b\b\b\b \b\b\b\b");
494 }
495 #endif
496
497 src_bytes_left += (in_file_buf_size - in_file_buf_ofs);
498
499 uint32 adler32 = zstream.adler;
500 deflateEnd(&zstream);
501
502 timer_ticks end_time = timer::get_ticks();
503 double total_time = timer::ticks_to_secs(my_max(1, end_time - start_time));
504
505 uint64 cmp_file_size = _ftelli64(pOutFile);
506
507 _aligned_free(in_file_buf);
508 in_file_buf = NULL;
509 _aligned_free(out_file_buf);
510 out_file_buf = NULL;
511
512 fclose(pInFile);
513 pInFile = NULL;
514 fclose(pOutFile);
515 pOutFile = NULL;
516
517 if (status != Z_STREAM_END)
518 {
519 print_error("Compression failed with status %i\n", status);
520 return false;
521 }
522
523 if (src_bytes_left)
524 {
525 print_error("Compressor failed to consume entire input file!\n");
526 return false;
527 }
528
529 printf("Success\n");
530 printf("Input file size: " QUAD_INT_FMT ", Compressed file size: " QUAD_INT_FMT ", Ratio: %3.2f%%\n", src_file_size, cmp_file_size, src_file_size ? ((1.0f - (static_cast<float>(cmp_file_size) / src_file_size)) * 100.0f) : 0.0f);
531 printf("Compression time: %3.6f\nConsumption rate: %9.1f bytes/sec, Emission rate: %9.1f bytes/sec\n", total_time, src_file_size / total_time, cmp_file_size / total_time);
532 printf("Input file adler32: 0x%08X\n", adler32);
533 if (src_file_size)
534 {
535 if (src_file_size >= 256)
536 s_max_large_comp_ratio = my_max(s_max_large_comp_ratio, cmp_file_size / (float)src_file_size);
537 else
538 s_max_small_comp_ratio = my_max(s_max_small_comp_ratio, cmp_file_size / (float)src_file_size);
539 }
540 //printf("Max small comp ratio: %f, Max large comp ratio: %f\n", s_max_small_comp_ratio, s_max_large_comp_ratio);
541
542 return true;
543 }
544
decompress_file_zlib(const char * pSrc_filename,const char * pDst_filename,comp_options options)545 static bool decompress_file_zlib(const char* pSrc_filename, const char *pDst_filename, comp_options options)
546 {
547 FILE *pInFile = fopen(pSrc_filename, "rb");
548 if (!pInFile)
549 {
550 print_error("Unable to read file: %s\n", pSrc_filename);
551 return false;
552 }
553
554 _fseeki64(pInFile, 0, SEEK_END);
555 uint64 src_file_size = _ftelli64(pInFile);
556 _fseeki64(pInFile, 0, SEEK_SET);
557 if (src_file_size < (5+9))
558 {
559 print_error("Compressed file is too small!\n");
560 fclose(pInFile);
561 return false;
562 }
563
564 int h0 = fgetc(pInFile);
565 int h1 = fgetc(pInFile);
566 int h2 = fgetc(pInFile);
567 int h3 = fgetc(pInFile);
568 int zlib_header = fgetc(pInFile);
569 if ((h0 != 'D') | (h1 != 'E') || (h2 != 'F') || (h3 != '0'))
570 {
571 print_error("Unrecognized/invalid header in file: %s\n", pSrc_filename);
572 fclose(pInFile);
573 return false;
574 }
575
576 FILE *pOutFile = fopen(pDst_filename, "wb");
577 if (!pOutFile)
578 {
579 print_error("Unable to create file: %s\n", pDst_filename);
580 fclose(pInFile);
581 return false;
582 }
583
584 uint64 orig_file_size = 0;
585 for (uint i = 0; i < 8; i++)
586 orig_file_size |= (static_cast<uint64>(fgetc(pInFile)) << (i * 8));
587
588 int total_header_bytes = ftell(pInFile);
589
590 // Avoid running out of memory on large files when using unbuffered decompression.
591 if ((options.m_unbuffered_decompression) && (orig_file_size > 768*1024*1024))
592 {
593 printf("Output file is too large for unbuffered decompression - switching to streaming decompression.\n");
594 options.m_unbuffered_decompression = false;
595 }
596
597 if (options.m_unbuffered_decompression)
598 printf("Testing: Unbuffered decompression\n");
599 else
600 printf("Testing: Streaming decompression\n");
601
602 uint cInBufSize = options.m_unbuffered_decompression ? static_cast<uint>(src_file_size) : TDEFLTEST_DECOMP_INPUT_BUFFER_SIZE;
603 uint out_buf_size = options.m_unbuffered_decompression ? static_cast<uint>(orig_file_size) : TINFL_LZ_DICT_SIZE;
604
605 if ((options.m_randomize_buffer_sizes) && (!options.m_unbuffered_decompression))
606 {
607 cInBufSize = 1 + (rand() % 4096);
608 }
609
610 printf("Input buffer size: %u, Output buffer size: %u\n", cInBufSize, out_buf_size);
611
612 uint8 *in_file_buf = static_cast<uint8*>(_aligned_malloc(cInBufSize, 16));
613 uint8 *out_file_buf = static_cast<uint8*>(_aligned_malloc(out_buf_size, 16));
614 if ((!in_file_buf) || (!out_file_buf))
615 {
616 print_error("Failed allocating output buffer!\n");
617 _aligned_free(in_file_buf);
618 fclose(pInFile);
619 fclose(pOutFile);
620 return false;
621 }
622
623 uint64 src_bytes_left = src_file_size - total_header_bytes;
624 uint64 dst_bytes_left = orig_file_size;
625
626 uint in_file_buf_size = 0;
627 uint in_file_buf_ofs = 0;
628 uint out_file_buf_ofs = 0;
629
630 timer_ticks start_time = timer::get_ticks();
631 double decomp_only_time = 0;
632
633 z_stream zstream;
634 memset(&zstream, 0, sizeof(zstream));
635
636 timer_ticks init_start_time = timer::get_ticks();
637 int status = zlib_header ? inflateInit(&zstream) : inflateInit2(&zstream, -Z_DEFAULT_WINDOW_BITS);
638 timer_ticks total_init_time = timer::get_ticks() - init_start_time;
639 if (status != Z_OK)
640 {
641 print_error("Failed initializing decompressor!\n");
642 _aligned_free(in_file_buf);
643 _aligned_free(out_file_buf);
644 fclose(pInFile);
645 fclose(pOutFile);
646 return false;
647 }
648
649 printf("inflateInit() took %3.3fms\n", timer::ticks_to_secs(total_init_time)*1000.0f);
650
651 for ( ; ; )
652 {
653 if (in_file_buf_ofs == in_file_buf_size)
654 {
655 in_file_buf_size = static_cast<uint>(my_min(cInBufSize, src_bytes_left));
656
657 if (fread(in_file_buf, 1, in_file_buf_size, pInFile) != in_file_buf_size)
658 {
659 print_error("Failure reading from source file!\n");
660 _aligned_free(in_file_buf);
661 _aligned_free(out_file_buf);
662 deflateEnd(&zstream);
663 fclose(pInFile);
664 fclose(pOutFile);
665 return false;
666 }
667
668 src_bytes_left -= in_file_buf_size;
669
670 in_file_buf_ofs = 0;
671 }
672
673 uint num_in_bytes = (in_file_buf_size - in_file_buf_ofs);
674 uint num_out_bytes = (out_buf_size - out_file_buf_ofs);
675 zstream.next_in = in_file_buf + in_file_buf_ofs;
676 zstream.avail_in = num_in_bytes;
677 zstream.next_out = out_file_buf + out_file_buf_ofs;
678 zstream.avail_out = num_out_bytes;
679
680 {
681 timer decomp_only_timer;
682 decomp_only_timer.start();
683 status = inflate(&zstream, options.m_unbuffered_decompression ? Z_FINISH : Z_SYNC_FLUSH);
684 decomp_only_time += decomp_only_timer.get_elapsed_secs();
685 }
686 num_in_bytes -= zstream.avail_in;
687 num_out_bytes -= zstream.avail_out;
688
689 if (num_in_bytes)
690 {
691 in_file_buf_ofs += (uint)num_in_bytes;
692 assert(in_file_buf_ofs <= in_file_buf_size);
693 }
694
695 out_file_buf_ofs += (uint)num_out_bytes;
696
697 if ((out_file_buf_ofs == out_buf_size) || (status == Z_STREAM_END))
698 {
699 if (fwrite(out_file_buf, 1, static_cast<uint>(out_file_buf_ofs), pOutFile) != out_file_buf_ofs)
700 {
701 print_error("Failure writing to destination file!\n");
702 _aligned_free(in_file_buf);
703 _aligned_free(out_file_buf);
704 inflateEnd(&zstream);
705 fclose(pInFile);
706 fclose(pOutFile);
707 return false;
708 }
709 out_file_buf_ofs = 0;
710 }
711
712 if (num_out_bytes > dst_bytes_left)
713 {
714 print_error("Decompressor wrote too many bytes to destination file!\n");
715 _aligned_free(in_file_buf);
716 _aligned_free(out_file_buf);
717 inflateEnd(&zstream);
718 fclose(pInFile);
719 fclose(pOutFile);
720 return false;
721 }
722 dst_bytes_left -= num_out_bytes;
723
724 if (status != Z_OK)
725 break;
726 }
727 _aligned_free(in_file_buf);
728 in_file_buf = NULL;
729
730 _aligned_free(out_file_buf);
731 out_file_buf = NULL;
732
733 src_bytes_left += (in_file_buf_size - in_file_buf_ofs);
734
735 uint32 adler32 = zstream.adler;
736 inflateEnd(&zstream);
737
738 timer_ticks end_time = timer::get_ticks();
739 double total_time = timer::ticks_to_secs(my_max(1, end_time - start_time));
740
741 fclose(pInFile);
742 pInFile = NULL;
743
744 fclose(pOutFile);
745 pOutFile = NULL;
746
747 if (status != Z_STREAM_END)
748 {
749 print_error("Decompression FAILED with status %i\n", status);
750 return false;
751 }
752
753 if ((src_file_size < UINT_MAX) && (orig_file_size < UINT_MAX))
754 {
755 if ((((size_t)zstream.total_in + total_header_bytes) != src_file_size) || (zstream.total_out != orig_file_size))
756 {
757 print_error("Decompression FAILED to consume all input or write all expected output!\n");
758 return false;
759 }
760 }
761
762 if (dst_bytes_left)
763 {
764 print_error("Decompressor FAILED to output the entire output file!\n");
765 return false;
766 }
767
768 if (src_bytes_left)
769 {
770 print_error("Decompressor FAILED to read " QUAD_INT_FMT " bytes from input buffer\n", src_bytes_left);
771 }
772
773 printf("Success\n");
774 printf("Source file size: " QUAD_INT_FMT ", Decompressed file size: " QUAD_INT_FMT "\n", src_file_size, orig_file_size);
775 if (zlib_header) printf("Decompressed adler32: 0x%08X\n", adler32);
776 printf("Overall decompression time (decompression init+I/O+decompression): %3.6f\n Consumption rate: %9.1f bytes/sec, Decompression rate: %9.1f bytes/sec\n", total_time, src_file_size / total_time, orig_file_size / total_time);
777 printf("Decompression only time (not counting decompression init or I/O): %3.6f\n Consumption rate: %9.1f bytes/sec, Decompression rate: %9.1f bytes/sec\n", decomp_only_time, src_file_size / decomp_only_time, orig_file_size / decomp_only_time);
778
779 return true;
780 }
781
compare_files(const char * pFilename1,const char * pFilename2)782 static bool compare_files(const char *pFilename1, const char* pFilename2)
783 {
784 FILE* pFile1 = open_file_with_retries(pFilename1, "rb");
785 if (!pFile1)
786 {
787 print_error("Failed opening file: %s\n", pFilename1);
788 return false;
789 }
790
791 FILE* pFile2 = open_file_with_retries(pFilename2, "rb");
792 if (!pFile2)
793 {
794 print_error("Failed opening file: %s\n", pFilename2);
795 fclose(pFile1);
796 return false;
797 }
798
799 _fseeki64(pFile1, 0, SEEK_END);
800 int64 fileSize1 = _ftelli64(pFile1);
801 _fseeki64(pFile1, 0, SEEK_SET);
802
803 _fseeki64(pFile2, 0, SEEK_END);
804 int64 fileSize2 = _ftelli64(pFile2);
805 _fseeki64(pFile2, 0, SEEK_SET);
806
807 if (fileSize1 != fileSize2)
808 {
809 print_error("Files to compare are not the same size: %I64i vs. %I64i.\n", fileSize1, fileSize2);
810 fclose(pFile1);
811 fclose(pFile2);
812 return false;
813 }
814
815 const uint cBufSize = 1024 * 1024;
816 std::vector<uint8> buf1(cBufSize);
817 std::vector<uint8> buf2(cBufSize);
818
819 int64 bytes_remaining = fileSize1;
820 while (bytes_remaining)
821 {
822 const uint bytes_to_read = static_cast<uint>(my_min(cBufSize, bytes_remaining));
823
824 if (fread(&buf1.front(), bytes_to_read, 1, pFile1) != 1)
825 {
826 print_error("Failed reading from file: %s\n", pFilename1);
827 fclose(pFile1);
828 fclose(pFile2);
829 return false;
830 }
831
832 if (fread(&buf2.front(), bytes_to_read, 1, pFile2) != 1)
833 {
834 print_error("Failed reading from file: %s\n", pFilename2);
835 fclose(pFile1);
836 fclose(pFile2);
837 return false;
838 }
839
840 if (memcmp(&buf1.front(), &buf2.front(), bytes_to_read) != 0)
841 {
842 print_error("File data comparison failed!\n");
843 fclose(pFile1);
844 fclose(pFile2);
845 return false;
846 }
847
848 bytes_remaining -= bytes_to_read;
849 }
850
851 fclose(pFile1);
852 fclose(pFile2);
853 return true;
854 }
855
zip_create(const char * pZip_filename,const char * pSrc_filename)856 static bool zip_create(const char *pZip_filename, const char *pSrc_filename)
857 {
858 mz_zip_archive zip;
859 memset(&zip, 0, sizeof(zip));
860 if ((rand() % 100) >= 10)
861 zip.m_file_offset_alignment = 1 << (rand() & 15);
862 if (!mz_zip_writer_init_file(&zip, pZip_filename, 65537))
863 {
864 print_error("Failed creating zip archive \"%s\" (1)!\n", pZip_filename);
865 return false;
866 }
867
868 mz_bool success = MZ_TRUE;
869
870 const char *pStr = "This is a test!This is a test!This is a test!\n";
871 size_t comp_size;
872 void *pComp_data = tdefl_compress_mem_to_heap(pStr, strlen(pStr), &comp_size, 256);
873 success &= mz_zip_writer_add_mem_ex(&zip, "precomp.txt", pComp_data, comp_size, "Comment", (uint16)strlen("Comment"), MZ_ZIP_FLAG_COMPRESSED_DATA, strlen(pStr), mz_crc32(MZ_CRC32_INIT, (const uint8 *)pStr, strlen(pStr)));
874
875 success &= mz_zip_writer_add_mem(&zip, "cool/", NULL, 0, 0);
876
877 success &= mz_zip_writer_add_mem(&zip, "1.txt", pStr, strlen(pStr), 9);
878 int n = rand() & 4095;
879 for (int i = 0; i < n; i++)
880 {
881 char name[256], buf[256], comment[256];
882 sprintf(name, "t%u.txt", i);
883 sprintf(buf, "%u\n", i*5377);
884 sprintf(comment, "comment: %u\n", i);
885 success &= mz_zip_writer_add_mem_ex(&zip, name, buf, strlen(buf), comment, (uint16)strlen(comment), i % 10, 0, 0);
886 }
887
888 const char *pTestComment = "test comment";
889 success &= mz_zip_writer_add_file(&zip, "test.bin", pSrc_filename, pTestComment, (uint16)strlen(pTestComment), 9);
890
891 if (ensure_file_exists_and_is_readable("changelog.txt"))
892 success &= mz_zip_writer_add_file(&zip, "changelog.txt", "changelog.txt", "This is a comment", (uint16)strlen("This is a comment"), 9);
893
894 if (!success)
895 {
896 mz_zip_writer_end(&zip);
897 remove(pZip_filename);
898 print_error("Failed creating zip archive \"%s\" (2)!\n", pZip_filename);
899 return false;
900 }
901
902 if (!mz_zip_writer_finalize_archive(&zip))
903 {
904 mz_zip_writer_end(&zip);
905 remove(pZip_filename);
906 print_error("Failed creating zip archive \"%s\" (3)!\n", pZip_filename);
907 return false;
908 }
909
910 mz_zip_writer_end(&zip);
911
912 struct FILE_STAT_STRUCT stat_buf;
913 FILE_STAT(pZip_filename, &stat_buf);
914 uint64 actual_file_size = stat_buf.st_size;
915 if (zip.m_archive_size != actual_file_size)
916 {
917 print_error("Archive's actual size and zip archive object's size differ for file \"%s\"!\n", pZip_filename);
918 return false;
919 }
920
921 printf("Created zip file \"%s\", file size: " QUAD_INT_FMT "\n", pZip_filename, zip.m_archive_size);
922 return true;
923 }
924
zip_write_callback(void * pOpaque,mz_uint64 ofs,const void * pBuf,size_t n)925 static size_t zip_write_callback(void *pOpaque, mz_uint64 ofs, const void *pBuf, size_t n)
926 {
927 (void)pOpaque, (void)ofs, (void)pBuf, (void)n;
928 return n;
929 }
930
zip_extract(const char * pZip_filename,const char * pDst_filename)931 static bool zip_extract(const char *pZip_filename, const char *pDst_filename)
932 {
933 mz_zip_archive zip;
934 memset(&zip, 0, sizeof(zip));
935 if (!mz_zip_reader_init_file(&zip, pZip_filename, 0))
936 {
937 print_error("Failed opening zip archive \"%s\"!\n", pZip_filename);
938 return false;
939 }
940
941 int file_index = mz_zip_reader_locate_file(&zip, "test.bin", "test Comment", 0);
942 int alt_file_index = mz_zip_reader_locate_file(&zip, "test.bin", "test Comment e", 0);
943 if ((file_index < 0) || (alt_file_index >= 0))
944 {
945 print_error("Archive \"%s\" is missing test.bin file!\n", pZip_filename);
946 mz_zip_reader_end(&zip);
947 return false;
948 }
949
950 alt_file_index = mz_zip_reader_locate_file(&zip, "test.bin", NULL, 0);
951 if (alt_file_index != file_index)
952 {
953 print_error("mz_zip_reader_locate_file() failed!\n", pZip_filename);
954 mz_zip_reader_end(&zip);
955 return false;
956 }
957
958 if (!mz_zip_reader_extract_to_file(&zip, file_index, pDst_filename, 0))
959 {
960 print_error("Failed extracting test.bin from archive \"%s\" err: %s!\n", pZip_filename, mz_zip_get_error_string(mz_zip_get_last_error(&zip)));
961 mz_zip_reader_end(&zip);
962 return false;
963 }
964
965 for (uint i = 0; i < mz_zip_reader_get_num_files(&zip); i++)
966 {
967 mz_zip_archive_file_stat stat;
968 if (!mz_zip_reader_file_stat(&zip, i, &stat))
969 {
970 print_error("Failed testing archive -1 \"%s\" err: %s!\n", pZip_filename, mz_zip_get_error_string(mz_zip_get_last_error(&zip)));
971 mz_zip_reader_end(&zip);
972 return false;
973 }
974 //printf("\"%s\" %I64u %I64u\n", stat.m_filename, stat.m_comp_size, stat.m_uncomp_size);
975 size_t size = 0;
976
977 mz_bool status = mz_zip_reader_extract_to_callback(&zip, i, zip_write_callback, NULL, 0);
978 if (!status)
979 {
980 print_error("Failed testing archive -2 \"%s\" err: %s!\n", pZip_filename, mz_zip_get_error_string(mz_zip_get_last_error(&zip)));
981 mz_zip_reader_end(&zip);
982 return false;
983 }
984
985 if (stat.m_uncomp_size<100*1024*1024)
986 {
987 void *p = mz_zip_reader_extract_to_heap(&zip, i, &size, 0);
988 if (!p)
989 {
990 print_error("Failed testing archive -3 \"%s\" err: %s!\n", pZip_filename, mz_zip_get_error_string(mz_zip_get_last_error(&zip)));
991 mz_zip_reader_end(&zip);
992 return false;
993 }
994 free(p);
995 }
996 }
997 printf("Verified %u files\n", mz_zip_reader_get_num_files(&zip));
998
999 mz_zip_reader_end(&zip);
1000
1001 printf("Extracted file \"%s\"\n", pDst_filename);
1002 return true;
1003 }
1004
1005 typedef std::vector< std::string > string_array;
1006
1007 #if defined(WIN32)
find_files(std::string pathname,const std::string & filename,string_array & files,bool recursive,int depth=0)1008 static bool find_files(std::string pathname, const std::string &filename, string_array &files, bool recursive, int depth = 0)
1009 {
1010 if (!pathname.empty())
1011 {
1012 char c = pathname[pathname.size() - 1];
1013 if ((c != ':') && (c != '\\') && (c != '/'))
1014 pathname += "\\";
1015 }
1016
1017 WIN32_FIND_DATAA find_data;
1018
1019 HANDLE findHandle = FindFirstFileA((pathname + filename).c_str(), &find_data);
1020 if (findHandle == INVALID_HANDLE_VALUE)
1021 {
1022 HRESULT hres = GetLastError();
1023 if ((!depth) && (hres != NO_ERROR) && (hres != ERROR_FILE_NOT_FOUND))
1024 return false;
1025 }
1026 else
1027 {
1028 do
1029 {
1030 const bool is_directory = (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
1031 const bool is_system = (find_data.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) != 0;
1032 const bool is_hidden = false;//(find_data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0;
1033
1034 std::string filename(find_data.cFileName);
1035
1036 if ((!is_directory) && (!is_system) && (!is_hidden))
1037 files.push_back(pathname + filename);
1038
1039 } while (FindNextFileA(findHandle, &find_data));
1040
1041 FindClose(findHandle);
1042 }
1043
1044 if (recursive)
1045 {
1046 string_array paths;
1047
1048 HANDLE findHandle = FindFirstFileA((pathname + "*").c_str(), &find_data);
1049 if (findHandle != INVALID_HANDLE_VALUE)
1050 {
1051 do
1052 {
1053 const bool is_directory = (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
1054 const bool is_system = (find_data.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) != 0;
1055 const bool is_hidden = false;//(find_data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0;
1056
1057 std::string filename(find_data.cFileName);
1058
1059 if ((is_directory) && (!is_hidden) && (!is_system))
1060 paths.push_back(filename);
1061
1062 } while (FindNextFileA(findHandle, &find_data));
1063
1064 FindClose(findHandle);
1065
1066 for (uint i = 0; i < paths.size(); i++)
1067 {
1068 const std::string &path = paths[i];
1069 if (path[0] == '.')
1070 continue;
1071
1072 if (!find_files(pathname + path, filename, files, true, depth + 1))
1073 return false;
1074 }
1075 }
1076 }
1077
1078 return true;
1079 }
1080 #else
1081 #include <dirent.h>
1082 #include <fnmatch.h>
find_files(std::string pathname,const std::string & pattern,string_array & files,bool recursive,int depth=0)1083 static bool find_files(std::string pathname, const std::string &pattern, string_array &files, bool recursive, int depth = 0)
1084 {
1085 if (!pathname.empty())
1086 {
1087 char c = pathname[pathname.size() - 1];
1088 if ((c != ':') && (c != '\\') && (c != '/'))
1089 pathname += "/";
1090 }
1091
1092 DIR *dp = opendir(pathname.c_str());
1093
1094 if (!dp) {
1095 return depth ? true : false;
1096 }
1097
1098 string_array paths;
1099
1100 for ( ; ; )
1101 {
1102 struct dirent *ep = readdir(dp);
1103 if (!ep)
1104 break;
1105
1106 const bool is_directory = (ep->d_type & DT_DIR) != 0;
1107 const bool is_file = (ep->d_type & DT_REG) != 0;
1108
1109 if (ep->d_name[0] == '.')
1110 continue;
1111
1112 std::string filename(ep->d_name);
1113
1114 if (is_directory)
1115 {
1116 if (recursive)
1117 paths.push_back(filename);
1118 }
1119 else if (is_file)
1120 {
1121 if (0 == fnmatch(pattern.c_str(), filename.c_str(), 0))
1122 files.push_back(pathname + filename);
1123 }
1124 }
1125
1126 closedir(dp);
1127 dp = NULL;
1128
1129 if (recursive)
1130 {
1131 for (uint i = 0; i < paths.size(); i++)
1132 {
1133 const std::string &path = paths[i];
1134 if (!find_files(pathname + path, pattern, files, true, depth + 1))
1135 return false;
1136 }
1137 }
1138
1139 return true;
1140 }
1141 #endif
1142
test_recursive(const char * pPath,comp_options options)1143 static bool test_recursive(const char *pPath, comp_options options)
1144 {
1145 string_array files;
1146 if (!find_files(pPath, "*", files, true))
1147 {
1148 print_error("Failed finding files under path \"%s\"!\n", pPath);
1149 return false;
1150 }
1151
1152 uint total_files_compressed = 0;
1153 uint64 total_source_size = 0;
1154 uint64 total_comp_size = 0;
1155
1156 #ifdef WIN32
1157 MEMORYSTATUS initial_mem_status;
1158 GlobalMemoryStatus(&initial_mem_status);
1159 #endif
1160
1161 timer_ticks start_tick_count = timer::get_ticks();
1162
1163 const int first_file_index = 0;
1164
1165 uint unique_id = static_cast<uint>(timer::get_init_ticks());
1166 char cmp_file[256], decomp_file[256];
1167
1168 sprintf(cmp_file, "__comp_temp_%u__.tmp", unique_id);
1169 sprintf(decomp_file, "__decomp_temp_%u__.tmp", unique_id);
1170
1171 for (uint file_index = first_file_index; file_index < files.size(); file_index++)
1172 {
1173 const std::string &src_file = files[file_index];
1174
1175 printf("***** [%u of %u] Compressing file \"%s\" to \"%s\"\n", 1 + file_index, (uint)files.size(), src_file.c_str(), cmp_file);
1176
1177 if ((strstr(src_file.c_str(), "__comp_temp") != NULL) || (strstr(src_file.c_str(), "__decomp_temp") != NULL))
1178 {
1179 printf("Skipping temporary file \"%s\"\n", src_file.c_str());
1180 continue;
1181 }
1182
1183 FILE *pFile = fopen(src_file.c_str(), "rb");
1184 if (!pFile)
1185 {
1186 printf("Skipping unreadable file \"%s\"\n", src_file.c_str());
1187 continue;
1188 }
1189 _fseeki64(pFile, 0, SEEK_END);
1190 int64 src_file_size = _ftelli64(pFile);
1191
1192 if (src_file_size)
1193 {
1194 _fseeki64(pFile, 0, SEEK_SET);
1195 if (fgetc(pFile) == EOF)
1196 {
1197 printf("Skipping unreadable file \"%s\"\n", src_file.c_str());
1198 fclose(pFile);
1199 continue;
1200 }
1201 }
1202
1203 fclose(pFile);
1204
1205 if (!ensure_file_is_writable(cmp_file))
1206 {
1207 print_error("Unable to create file \"%s\"!\n", cmp_file);
1208 return false;
1209 }
1210
1211 comp_options file_options(options);
1212 if (options.m_randomize_params)
1213 {
1214 file_options.m_level = rand() % 11;
1215 file_options.m_unbuffered_decompression = (rand() & 1) != 0;
1216 file_options.m_z_strat = rand() % (Z_FIXED + 1);
1217 file_options.m_write_zlib_header = (rand() & 1) != 0;
1218 file_options.m_random_z_flushing = (rand() & 1) != 0;
1219 file_options.print();
1220 }
1221
1222 bool status;
1223 if (file_options.m_archive_test)
1224 {
1225 printf("Creating test archive with file \"%s\", size " QUAD_INT_FMT "\n", src_file.c_str(), src_file_size);
1226 status = zip_create(cmp_file, src_file.c_str());
1227 }
1228 else
1229 status = compress_file_zlib(src_file.c_str(), cmp_file, file_options);
1230 if (!status)
1231 {
1232 print_error("Failed compressing file \"%s\" to \"%s\"\n", src_file.c_str(), cmp_file);
1233 return false;
1234 }
1235
1236 if (file_options.m_verify_compressed_data)
1237 {
1238 printf("Decompressing file \"%s\" to \"%s\"\n", cmp_file, decomp_file);
1239
1240 if (!ensure_file_is_writable(decomp_file))
1241 {
1242 print_error("Unable to create file \"%s\"!\n", decomp_file);
1243 return false;
1244 }
1245
1246 if (file_options.m_archive_test)
1247 status = zip_extract(cmp_file, decomp_file);
1248 else
1249 status = decompress_file_zlib(cmp_file, decomp_file, file_options);
1250
1251 if (!status)
1252 {
1253 print_error("Failed decompressing file \"%s\" to \"%s\"\n", src_file.c_str(), decomp_file);
1254 return false;
1255 }
1256
1257 printf("Comparing file \"%s\" to \"%s\"\n", decomp_file, src_file.c_str());
1258
1259 if (!compare_files(decomp_file, src_file.c_str()))
1260 {
1261 print_error("Failed comparing decompressed file data while compressing \"%s\" to \"%s\"\n", src_file.c_str(), cmp_file);
1262 return false;
1263 }
1264 else
1265 {
1266 printf("Decompressed file compared OK to original file.\n");
1267 }
1268 }
1269
1270 int64 cmp_file_size = 0;
1271 pFile = fopen(cmp_file, "rb");
1272 if (pFile)
1273 {
1274 _fseeki64(pFile, 0, SEEK_END);
1275 cmp_file_size = _ftelli64(pFile);
1276 fclose(pFile);
1277 }
1278
1279 total_files_compressed++;
1280 total_source_size += src_file_size;
1281 total_comp_size += cmp_file_size;
1282
1283 #ifdef WIN32
1284 MEMORYSTATUS mem_status;
1285 GlobalMemoryStatus(&mem_status);
1286
1287 const int64 bytes_allocated = initial_mem_status.dwAvailVirtual- mem_status.dwAvailVirtual;
1288
1289 printf("Memory allocated relative to first file: %I64i\n", bytes_allocated);
1290 #endif
1291
1292 printf("\n");
1293 }
1294
1295 timer_ticks end_tick_count = timer::get_ticks();
1296
1297 double total_elapsed_time = timer::ticks_to_secs(end_tick_count - start_tick_count);
1298
1299 printf("Test successful: %f secs\n", total_elapsed_time);
1300 printf("Total files processed: %u\n", total_files_compressed);
1301 printf("Total source size: " QUAD_INT_FMT "\n", total_source_size);
1302 printf("Total compressed size: " QUAD_INT_FMT "\n", total_comp_size);
1303 printf("Max small comp ratio: %f, Max large comp ratio: %f\n", s_max_small_comp_ratio, s_max_large_comp_ratio);
1304
1305 remove(cmp_file);
1306 remove(decomp_file);
1307
1308 return true;
1309 }
1310
dummy_zip_file_write_callback(void * pOpaque,mz_uint64 ofs,const void * pBuf,size_t n)1311 static size_t dummy_zip_file_write_callback(void *pOpaque, mz_uint64 ofs, const void *pBuf, size_t n)
1312 {
1313 (void)ofs; (void)pBuf;
1314 uint32 *pCRC = (uint32*)pOpaque;
1315 *pCRC = mz_crc32(*pCRC, (const uint8*)pBuf, n);
1316 return n;
1317 }
1318
test_archives(const char * pPath,comp_options options)1319 static bool test_archives(const char *pPath, comp_options options)
1320 {
1321 (void)options;
1322
1323 string_array files;
1324 if (!find_files(pPath, "*.zip", files, true))
1325 {
1326 print_error("Failed finding files under path \"%s\"!\n", pPath);
1327 return false;
1328 }
1329
1330 uint total_archives = 0;
1331 uint64 total_bytes_processed = 0;
1332 uint64 total_files_processed = 0;
1333 uint total_errors = 0;
1334
1335 #ifdef WIN32
1336 MEMORYSTATUS initial_mem_status;
1337 GlobalMemoryStatus(&initial_mem_status);
1338 #endif
1339
1340 const int first_file_index = 0;
1341 uint unique_id = static_cast<uint>(timer::get_init_ticks());
1342 char cmp_file[256], decomp_file[256];
1343
1344 sprintf(decomp_file, "__decomp_temp_%u__.tmp", unique_id);
1345
1346 string_array failed_archives;
1347
1348 for (uint file_index = first_file_index; file_index < files.size(); file_index++)
1349 {
1350 const std::string &src_file = files[file_index];
1351
1352 printf("***** [%u of %u] Testing archive file \"%s\"\n", 1 + file_index, (uint)files.size(), src_file.c_str());
1353
1354 if ((strstr(src_file.c_str(), "__comp_temp") != NULL) || (strstr(src_file.c_str(), "__decomp_temp") != NULL))
1355 {
1356 printf("Skipping temporary file \"%s\"\n", src_file.c_str());
1357 continue;
1358 }
1359
1360 FILE *pFile = fopen(src_file.c_str(), "rb");
1361 if (!pFile)
1362 {
1363 printf("Skipping unreadable file \"%s\"\n", src_file.c_str());
1364 continue;
1365 }
1366 _fseeki64(pFile, 0, SEEK_END);
1367 int64 src_file_size = _ftelli64(pFile);
1368 fclose(pFile);
1369
1370 (void)src_file_size;
1371
1372 sprintf(cmp_file, "__comp_temp_%u__.zip", file_index);
1373
1374 mz_zip_archive src_archive;
1375 memset(&src_archive, 0, sizeof(src_archive));
1376
1377 if (!mz_zip_reader_init_file(&src_archive, src_file.c_str(), 0))
1378 {
1379 failed_archives.push_back(src_file);
1380
1381 print_error("Failed opening archive \"%s\"!\n", src_file.c_str());
1382 total_errors++;
1383 continue;
1384 }
1385
1386 mz_zip_archive dst_archive;
1387 memset(&dst_archive, 0, sizeof(dst_archive));
1388 if (options.m_write_archives)
1389 {
1390 if (!ensure_file_is_writable(cmp_file))
1391 {
1392 print_error("Unable to create file \"%s\"!\n", cmp_file);
1393 return false;
1394 }
1395
1396 if (!mz_zip_writer_init_file(&dst_archive, cmp_file, 0))
1397 {
1398 print_error("Failed creating archive \"%s\"!\n", cmp_file);
1399 total_errors++;
1400 continue;
1401 }
1402 }
1403
1404 int i;
1405 //for (i = 0; i < mz_zip_reader_get_num_files(&src_archive); i++)
1406 for (i = mz_zip_reader_get_num_files(&src_archive) - 1; i >= 0; --i)
1407 {
1408 if (mz_zip_reader_is_file_encrypted(&src_archive, i))
1409 continue;
1410
1411 mz_zip_archive_file_stat file_stat;
1412 bool status = mz_zip_reader_file_stat(&src_archive, i, &file_stat) != 0;
1413
1414 int locate_file_index = mz_zip_reader_locate_file(&src_archive, file_stat.m_filename, NULL, 0);
1415 if (locate_file_index != i)
1416 {
1417 mz_zip_archive_file_stat locate_file_stat;
1418 mz_zip_reader_file_stat(&src_archive, locate_file_index, &locate_file_stat);
1419 if (_stricmp(locate_file_stat.m_filename, file_stat.m_filename) != 0)
1420 {
1421 print_error("mz_zip_reader_locate_file() failed!\n");
1422 return false;
1423 }
1424 else
1425 {
1426 printf("Warning: Duplicate filenames in archive!\n");
1427 }
1428 }
1429
1430 if ((file_stat.m_method) && (file_stat.m_method != MZ_DEFLATED))
1431 continue;
1432
1433 if (status)
1434 {
1435 char name[260];
1436 mz_zip_reader_get_filename(&src_archive, i, name, sizeof(name));
1437
1438 size_t extracted_size = 0;
1439 void *p = mz_zip_reader_extract_file_to_heap(&src_archive, name, &extracted_size, 0);
1440 if (!p)
1441 status = false;
1442
1443 uint32 extracted_crc32 = MZ_CRC32_INIT;
1444 if (!mz_zip_reader_extract_file_to_callback(&src_archive, name, dummy_zip_file_write_callback, &extracted_crc32, 0))
1445 status = false;
1446
1447 if (mz_crc32(MZ_CRC32_INIT, (const uint8*)p, extracted_size) != extracted_crc32)
1448 status = false;
1449
1450 free(p);
1451
1452 if (options.m_write_archives)
1453 {
1454 if ((status) && (!mz_zip_writer_add_from_zip_reader(&dst_archive, &src_archive, i)))
1455 {
1456 print_error("Failed adding new file to archive \"%s\"!\n", cmp_file);
1457 status = false;
1458 }
1459 }
1460
1461 total_bytes_processed += file_stat.m_uncomp_size;
1462 total_files_processed++;
1463 }
1464
1465 if (!status)
1466 break;
1467 }
1468
1469 mz_zip_reader_end(&src_archive);
1470
1471 //if (i < mz_zip_reader_get_num_files(&src_archive))
1472 if (i >= 0)
1473 {
1474 failed_archives.push_back(src_file);
1475
1476 print_error("Failed processing archive \"%s\"!\n", src_file.c_str());
1477 total_errors++;
1478 }
1479
1480 if (options.m_write_archives)
1481 {
1482 if (!mz_zip_writer_finalize_archive(&dst_archive) || !mz_zip_writer_end(&dst_archive))
1483 {
1484 failed_archives.push_back(src_file);
1485
1486 print_error("Failed finalizing archive \"%s\"!\n", cmp_file);
1487 total_errors++;
1488 }
1489 }
1490
1491 total_archives++;
1492
1493 #ifdef WIN32
1494 MEMORYSTATUS mem_status;
1495 GlobalMemoryStatus(&mem_status);
1496
1497 const int64 bytes_allocated = initial_mem_status.dwAvailVirtual- mem_status.dwAvailVirtual;
1498
1499 printf("Memory allocated relative to first file: %I64i\n", bytes_allocated);
1500 #endif
1501
1502 printf("\n");
1503 }
1504
1505 printf("Total archives processed: %u\n", total_archives);
1506 printf("Total errors: %u\n", total_errors);
1507 printf("Total bytes processed: " QUAD_INT_FMT "\n", total_bytes_processed);
1508 printf("Total archive files processed: " QUAD_INT_FMT "\n", total_files_processed);
1509
1510 printf("List of failed archives:\n");
1511 for (uint i = 0; i < failed_archives.size(); ++i)
1512 printf("%s\n", failed_archives[i].c_str());
1513
1514 remove(cmp_file);
1515 remove(decomp_file);
1516
1517 return true;
1518 }
1519
main_internal(string_array cmd_line)1520 int main_internal(string_array cmd_line)
1521 {
1522 comp_options options;
1523
1524 if (!cmd_line.size())
1525 {
1526 print_usage();
1527 if (simple_test1(options) || simple_test2(options))
1528 return EXIT_FAILURE;
1529 return EXIT_SUCCESS;
1530 }
1531
1532 enum op_mode_t
1533 {
1534 OP_MODE_INVALID = -1,
1535 OP_MODE_COMPRESS = 0,
1536 OP_MODE_DECOMPRESS = 1,
1537 OP_MODE_ALL = 2,
1538 OP_MODE_ARCHIVES = 3
1539 };
1540
1541 op_mode_t op_mode = OP_MODE_INVALID;
1542
1543 for (int i = 0; i < (int)cmd_line.size(); i++)
1544 {
1545 const std::string &str = cmd_line[i];
1546 if (str[0] == '-')
1547 {
1548 if (str.size() < 2)
1549 {
1550 print_error("Invalid option: %s\n", str.c_str());
1551 return EXIT_FAILURE;
1552 }
1553 switch (tolower(str[1]))
1554 {
1555 case 'u':
1556 {
1557 options.m_unbuffered_decompression = true;
1558 break;
1559 }
1560 case 'm':
1561 {
1562 int comp_level = atoi(str.c_str() + 2);
1563 if ((comp_level < 0) || (comp_level > (int)10))
1564 {
1565 print_error("Invalid compression level: %s\n", str.c_str());
1566 return EXIT_FAILURE;
1567 }
1568
1569 options.m_level = comp_level;
1570 break;
1571 }
1572 case 'v':
1573 {
1574 options.m_verify_compressed_data = true;
1575 break;
1576 }
1577 case 'r':
1578 {
1579 options.m_randomize_params = true;
1580 break;
1581 }
1582 case 'b':
1583 {
1584 options.m_randomize_buffer_sizes = true;
1585 break;
1586 }
1587 case 'h':
1588 {
1589 options.m_random_z_flushing = true;
1590 break;
1591 }
1592 case 'x':
1593 {
1594 int seed = atoi(str.c_str() + 2);
1595 srand(seed);
1596 printf("Using random seed: %i\n", seed);
1597 break;
1598 }
1599 case 't':
1600 {
1601 options.m_z_strat = my_min(Z_FIXED, my_max(0, atoi(str.c_str() + 2)));
1602 break;
1603 }
1604 case 'z':
1605 {
1606 options.m_write_zlib_header = false;
1607 break;
1608 }
1609 case 'a':
1610 {
1611 options.m_archive_test = true;
1612 break;
1613 }
1614 case 'w':
1615 {
1616 options.m_write_archives = true;
1617 break;
1618 }
1619 default:
1620 {
1621 print_error("Invalid option: %s\n", str.c_str());
1622 return EXIT_FAILURE;
1623 }
1624 }
1625
1626 cmd_line.erase(cmd_line.begin() + i);
1627 i--;
1628
1629 continue;
1630 }
1631
1632 if (str.size() != 1)
1633 {
1634 print_error("Invalid mode: %s\n", str.c_str());
1635 return EXIT_FAILURE;
1636 }
1637 switch (tolower(str[0]))
1638 {
1639 case 'c':
1640 {
1641 op_mode = OP_MODE_COMPRESS;
1642 break;
1643 }
1644 case 'd':
1645 {
1646 op_mode = OP_MODE_DECOMPRESS;
1647 break;
1648 }
1649 case 'a':
1650 {
1651 op_mode = OP_MODE_ALL;
1652 break;
1653 }
1654 case 'r':
1655 {
1656 op_mode = OP_MODE_ARCHIVES;
1657 break;
1658 }
1659 default:
1660 {
1661 print_error("Invalid mode: %s\n", str.c_str());
1662 return EXIT_FAILURE;
1663 }
1664 }
1665 cmd_line.erase(cmd_line.begin() + i);
1666 break;
1667 }
1668
1669 if (op_mode == OP_MODE_INVALID)
1670 {
1671 print_error("No mode specified!\n");
1672 print_usage();
1673 return EXIT_FAILURE;
1674 }
1675
1676 printf("Using options:\n");
1677 options.print();
1678 printf("\n");
1679
1680 int exit_status = EXIT_FAILURE;
1681
1682 switch (op_mode)
1683 {
1684 case OP_MODE_COMPRESS:
1685 {
1686 if (cmd_line.size() < 2)
1687 {
1688 print_error("Must specify input and output filenames!\n");
1689 return EXIT_FAILURE;
1690 }
1691 else if (cmd_line.size() > 2)
1692 {
1693 print_error("Too many filenames!\n");
1694 return EXIT_FAILURE;
1695 }
1696
1697 const std::string &src_file = cmd_line[0];
1698 const std::string &cmp_file = cmd_line[1];
1699
1700 bool comp_result = compress_file_zlib(src_file.c_str(), cmp_file.c_str(), options);
1701 if (comp_result)
1702 exit_status = EXIT_SUCCESS;
1703
1704 if ((comp_result) && (options.m_verify_compressed_data))
1705 {
1706 char decomp_file[256];
1707
1708 sprintf(decomp_file, "__decomp_temp_%u__.tmp", (uint)timer::get_ms());
1709
1710 if (!decompress_file_zlib(cmp_file.c_str(), decomp_file, options))
1711 {
1712 print_error("Failed decompressing file \"%s\" to \"%s\"\n", cmp_file.c_str(), decomp_file);
1713 return EXIT_FAILURE;
1714 }
1715
1716 printf("Comparing file \"%s\" to \"%s\"\n", decomp_file, src_file.c_str());
1717
1718 if (!compare_files(decomp_file, src_file.c_str()))
1719 {
1720 print_error("Failed comparing decompressed file data while compressing \"%s\" to \"%s\"\n", src_file.c_str(), cmp_file.c_str());
1721 return EXIT_FAILURE;
1722 }
1723 else
1724 {
1725 printf("Decompressed file compared OK to original file.\n");
1726 }
1727
1728 remove(decomp_file);
1729 }
1730
1731 break;
1732 }
1733 case OP_MODE_DECOMPRESS:
1734 {
1735 if (cmd_line.size() < 2)
1736 {
1737 print_error("Must specify input and output filenames!\n");
1738 return EXIT_FAILURE;
1739 }
1740 else if (cmd_line.size() > 2)
1741 {
1742 print_error("Too many filenames!\n");
1743 return EXIT_FAILURE;
1744 }
1745 if (decompress_file_zlib(cmd_line[0].c_str(), cmd_line[1].c_str(), options))
1746 exit_status = EXIT_SUCCESS;
1747 break;
1748 }
1749 case OP_MODE_ALL:
1750 {
1751 if (!cmd_line.size())
1752 {
1753 print_error("No directory specified!\n");
1754 return EXIT_FAILURE;
1755 }
1756 else if (cmd_line.size() != 1)
1757 {
1758 print_error("Too many filenames!\n");
1759 return EXIT_FAILURE;
1760 }
1761 if (test_recursive(cmd_line[0].c_str(), options))
1762 exit_status = EXIT_SUCCESS;
1763 break;
1764 }
1765 case OP_MODE_ARCHIVES:
1766 {
1767 if (!cmd_line.size())
1768 {
1769 print_error("No directory specified!\n");
1770 return EXIT_FAILURE;
1771 }
1772 else if (cmd_line.size() != 1)
1773 {
1774 print_error("Too many filenames!\n");
1775 return EXIT_FAILURE;
1776 }
1777 if (test_archives(cmd_line[0].c_str(), options))
1778 exit_status = EXIT_SUCCESS;
1779 break;
1780 }
1781 default:
1782 {
1783 print_error("No mode specified!\n");
1784 print_usage();
1785 return EXIT_FAILURE;
1786 }
1787 }
1788
1789 return exit_status;
1790 }
1791
main(int argc,char * argv[])1792 int main(int argc, char *argv[])
1793 {
1794 #if defined(_WIN64) || defined(__LP64__) || defined(_LP64)
1795 printf("miniz.c x64 Command Line Test App - Compiled %s %s\n", __DATE__, __TIME__);
1796 #else
1797 printf("miniz.c x86 Command Line Test App - Compiled %s %s\n", __DATE__, __TIME__);
1798 #endif
1799 timer::get_ticks();
1800
1801 string_array cmd_line;
1802 for (int i = 1; i < argc; i++)
1803 cmd_line.push_back(std::string(argv[i]));
1804
1805 int exit_status = main_internal(cmd_line);
1806
1807 return exit_status;
1808 }
1809