forked from imLinguin/lzham_codec
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlzhamtest.cpp
1732 lines (1470 loc) · 53.6 KB
/
lzhamtest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// File: lzhamtest.cpp
// This is a small app primarily designed to test the codec. Sorry, it doesn't really make the best sample, it's just a test harness.
// I also test LZHAM in a private branch of 7zip, mostly to ensure the streaming API is solid. Please contact me if you would like this branch.
// See the decompress_file() function to see how to use the decompression API, and the compress_file() function for the compression API.
// See include/lzham.h for documentation on the public LZHAM API.
// Tested on Windows, Linux, and OSX. On iOS, I use a small "Hello World" test app to test the codec.
// LZHAM is in the Public Domain. Please see the Public Domain declaration at the end of include/lzham.h
#if defined(__GNUC__)
#define _FILE_OFFSET_BITS 64
#endif
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <memory.h>
#include <stdarg.h>
#if !defined(__APPLE__) && !defined(__FreeBSD__)
#include <malloc.h>
#endif
#include <vector>
#include <string>
#include "timer.h"
#define my_max(a,b) (((a) > (b)) ? (a) : (b))
#define my_min(a,b) (((a) < (b)) ? (a) : (b))
#define LZHAM_PRINT_OUTPUT_PROGRESS
// Note: lzham can be used as static libs, or as a DLL. The default (out of the box) configuration under Win32 loads the DLL.
// To test lzham as a static library under Win32, set LZHAM_USE_LZHAM_DLL to 0, add ../lzhamcomp and ../lzhamdecomp to the additional inc paths, and link with the lzhamcomp and lzhamdecomp libs.
// On non-Win32 platforms, this test app currently assumes static libs.
#ifdef _XBOX
#include <xtl.h>
#include <xbdm.h>
#define LZHAM_USE_LZHAM_DLL 1
#elif defined(WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define LZHAM_USE_LZHAM_DLL 1
#elif defined(__APPLE__)
#include <unistd.h>
#define Sleep(ms) usleep(ms*1000)
#define _aligned_malloc(size, alignment) malloc(size)
#define _aligned_free free
#define fopen fopen
#define _fseeki64 fseek
#define _ftelli64 ftell
#elif defined(__FreeBSD__)
#include <unistd.h>
#define Sleep(ms) usleep(ms*1000)
#define _aligned_malloc(size, alignment) aligned_alloc(alignment, size)
#define _aligned_free free
#define fopen fopen
#define _fseeki64 fseeko
#define _ftelli64 ftello
#else
#include <unistd.h>
#define Sleep(ms) usleep(ms*1000)
#define _aligned_malloc(size, alignment) memalign(alignment, size)
#define _aligned_free free
#define fopen fopen64
#define _fseeki64 fseeko64
#define _ftelli64 ftello64
#endif
#ifdef WIN32
#define QUAD_INT_FMT "%I64u"
#else
#define QUAD_INT_FMT "%llu"
#endif
#if LZHAM_USE_LZHAM_DLL
#include "lzham_dynamic_lib.h"
#else
#include "lzham_static_lib.h"
#endif
#ifdef _DEBUG
const bool g_is_debug = true;
#else
const bool g_is_debug = false;
#endif
typedef unsigned char uint8;
typedef unsigned int uint;
typedef unsigned int uint32;
#ifdef __GNUC__
typedef unsigned long long uint64;
typedef long long int64;
#else
typedef unsigned __int64 uint64;
typedef signed __int64 int64;
#endif
#ifdef LZHAM_64BIT
#define LZHAMTEST_MAX_POSSIBLE_DICT_SIZE LZHAM_MAX_DICT_SIZE_LOG2_X64
// 256MB default dictionary size under x64 (max is 512MB, but this requires more than 4GB of physical memory without thrashing)
#define LZHAMTEST_DEFAULT_DICT_SIZE 28
#else
#define LZHAMTEST_MAX_POSSIBLE_DICT_SIZE LZHAM_MAX_DICT_SIZE_LOG2_X86
// 64MB default dictionary size under x86
#define LZHAMTEST_DEFAULT_DICT_SIZE LZHAM_MAX_DICT_SIZE_LOG2_X86
#endif
#if 1
#define LZHAMTEST_COMP_INPUT_BUFFER_SIZE 65536*4
#define LZHAMTEST_COMP_OUTPUT_BUFFER_SIZE 65536*4
#define LZHAMTEST_DECOMP_INPUT_BUFFER_SIZE 65536*4
#define LZHAMTEST_DECOMP_OUTPUT_BUFFER_SIZE 65536*4
#else
#define LZHAMTEST_COMP_INPUT_BUFFER_SIZE 1
#define LZHAMTEST_COMP_OUTPUT_BUFFER_SIZE 1
#define LZHAMTEST_DECOMP_INPUT_BUFFER_SIZE 1
#define LZHAMTEST_DECOMP_OUTPUT_BUFFER_SIZE 1
#endif
#define LZHAMTEST_NO_RANDOM_EXTREME_PARSING 1
struct comp_options
{
comp_options() :
m_comp_level(LZHAM_COMP_LEVEL_UBER),
m_dict_size_log2(LZHAMTEST_DEFAULT_DICT_SIZE),
m_compute_adler32_during_decomp(true),
m_max_helper_threads(0),
m_unbuffered_decompression(false),
m_verify_compressed_data(false),
m_randomize_params(false),
m_extreme_parsing(false),
m_deterministic_parsing(false),
m_tradeoff_decomp_rate_for_comp_ratio(false),
m_test_compressor_reinit(false),
m_table_update_rate(LZHAM_DEFAULT_TABLE_UPDATE_RATE)
{
}
void print()
{
printf("Comp level: %u\n", m_comp_level);
printf("Dict size: %i (%u bytes)\n", m_dict_size_log2, 1 << m_dict_size_log2);
printf("Compute adler32 during decompression: %u\n", (uint)m_compute_adler32_during_decomp);
printf("Max helper threads: %i\n", m_max_helper_threads);
printf("Unbuffered decompression: %u\n", (uint)m_unbuffered_decompression);
printf("Verify compressed data: %u\n", (uint)m_verify_compressed_data);
printf("Extreme parsing: %u\n", (uint)m_extreme_parsing);
printf("Randomize parameters: %u\n", m_randomize_params);
printf("Deterministic parsing: %u\n", m_deterministic_parsing);
printf("Trade off decompression rate for compression ratio: %u\n", m_tradeoff_decomp_rate_for_comp_ratio);
printf("Test compressor reinit: %u\n", m_test_compressor_reinit);
printf("Table update speed: %u\n", m_table_update_rate);
}
lzham_compress_level m_comp_level;
int m_dict_size_log2;
bool m_compute_adler32_during_decomp;
int m_max_helper_threads; // -1 = try to auto-detect
bool m_unbuffered_decompression;
bool m_verify_compressed_data;
bool m_randomize_params;
bool m_extreme_parsing;
bool m_deterministic_parsing;
bool m_tradeoff_decomp_rate_for_comp_ratio;
bool m_test_compressor_reinit;
uint m_table_update_rate;
};
static void print_usage()
{
printf("Usage: [options] [mode] inpath/infile [outfile]\n");
printf("\n");
printf("Modes:\n");
printf("c - Compress \"infile\" to \"outfile\"\n");
printf("d - Decompress \"infile\" to \"outfile\"\n");
printf("a - Recursively compress all files under \"inpath\"\n");
printf("\n");
printf("Options:\n");
printf("-m[0-4] - Compression level: 0=fastest, 1=faster, 2=default, 3=better, 4=uber\n");
printf(" Default is uber (4).\n");
printf("-d[15-29] - Set log2 dictionary size, max. is 26 on x86 platforms, 29 on x64.\n");
printf(" Default is 26 (64MB) on x86, 28 (256MB) on x64.\n");
printf("-c - Do not compute or verify adler32 checksum during decompression (faster).\n");
printf("-u - Use unbuffered decompression on files that can fit into memory.\n");
printf(" Unbuffered decompression is faster, but may have more I/O overhead.\n");
printf("-t[0-64] - Number of extra compression helper threads. Default=# CPU's-1.\n");
printf(" Note: The total number of threads will be 1 + num_helper_threads,\n");
printf(" because the main thread is counted separately.\n");
printf("-v - Immediately decompress compressed file after compression for verification.\n");
printf("-x - Extreme parsing, for slight compression gain (Uber only, MUCH slower).\n");
printf("-o - Permit the compressor to trade off decompression rate for higher ratios.\n");
printf(" Note: This flag can drop the decompression rate by 30%% or more.\n");
printf("-e - Enable deterministic parsing for slightly higher compression and\n");
printf(" predictable output files when enabled, but less scalability.\n");
printf(" The default is disabled, so the generated output data may slightly vary\n");
printf(" between runs when multithreaded compression is enabled.\n");
printf("-afilename Enable delta compression using the specified seed file.\n");
printf(" The same seed file MUST be used for compression/decompression.\n");
printf("-r - Use randomized parameters for each file.\n");
printf("-h[0-%u] - Set Huffman table update frequency. 0=Internal def, Def=%u, higher=faster.\n", LZHAM_FASTEST_TABLE_UPDATE_RATE, LZHAM_DEFAULT_TABLE_UPDATE_RATE);
printf(" Lower settings=slower decompression, but higher ratio. Note 1=impractically slow.\n");
}
static void print_error(const char *pMsg, ...)
{
char buf[1024];
va_list args;
va_start(args, pMsg);
vsnprintf(buf, sizeof(buf), pMsg, args);
va_end(args);
buf[sizeof(buf) - 1] = '\0';
fprintf(stderr, "Error: %s", buf);
}
static FILE* open_file_with_retries(const char *pFilename, const char* pMode)
{
const uint cNumRetries = 8;
for (uint i = 0; i < cNumRetries; i++)
{
FILE* pFile = fopen(pFilename, pMode);
if (pFile)
return pFile;
Sleep(250);
}
return NULL;
}
static bool ensure_file_is_writable(const char *pFilename)
{
const int cNumRetries = 8;
for (int i = 0; i < cNumRetries; i++)
{
FILE *pFile = fopen(pFilename, "wb");
if (pFile)
{
fclose(pFile);
return true;
}
Sleep(250);
}
return false;
}
static int simple_test(ilzham &lzham_dll, const comp_options &options)
{
printf("\n");
printf("LZHAM simple memory to memory compression test\n");
lzham_compress_params comp_params;
memset(&comp_params, 0, sizeof(comp_params));
comp_params.m_struct_size = sizeof(comp_params);
comp_params.m_dict_size_log2 = options.m_dict_size_log2;
comp_params.m_level = options.m_comp_level;
comp_params.m_max_helper_threads = 1;
lzham_uint8 cmp_buf[1024];
size_t cmp_len = sizeof(cmp_buf);
const char *p = "This is a test.This is a test.This is a test.1234567This is a test.This is a test.123456";
size_t uncomp_len = strlen(p);
lzham_uint32 comp_adler32 = 0;
lzham_compress_status_t comp_status = lzham_dll.lzham_compress_memory(&comp_params, cmp_buf, &cmp_len, (const lzham_uint8 *)p, uncomp_len, &comp_adler32);
if (comp_status != LZHAM_COMP_STATUS_SUCCESS)
{
print_error("Compression test failed with status %i!\n", comp_status);
return EXIT_FAILURE;
}
printf("Uncompressed size: %u\nCompressed size: %u\n", (uint)uncomp_len, (uint)cmp_len);
lzham_decompress_params decomp_params;
memset(&decomp_params, 0, sizeof(decomp_params));
decomp_params.m_struct_size = sizeof(decomp_params);
decomp_params.m_dict_size_log2 = options.m_dict_size_log2;
if (options.m_compute_adler32_during_decomp)
decomp_params.m_decompress_flags |= LZHAM_DECOMP_FLAG_COMPUTE_ADLER32;
lzham_uint8 decomp_buf[1024];
size_t decomp_size = sizeof(decomp_buf);
lzham_uint32 decomp_adler32 = 0;
lzham_decompress_status_t decomp_status = lzham_dll.lzham_decompress_memory(&decomp_params, decomp_buf, &decomp_size, cmp_buf, cmp_len, &decomp_adler32);
if (decomp_status != LZHAM_DECOMP_STATUS_SUCCESS)
{
print_error("Compression test failed with status %i!\n", decomp_status);
return EXIT_FAILURE;
}
if ((comp_adler32 != decomp_adler32) || (decomp_size != uncomp_len) || (memcmp(decomp_buf, p, uncomp_len)))
{
print_error("Compression test failed!\n");
return EXIT_FAILURE;
}
printf("Compression test succeeded.\n");
return EXIT_SUCCESS;
}
static bool read_seed_file(const char *pSeed_filename, lzham_uint32 &num_seed_bytes, const void *&pSeed_bytes, uint dict_size_log2)
{
num_seed_bytes = 0;
pSeed_bytes = NULL;
if (pSeed_filename)
{
FILE *pSeed_file = fopen(pSeed_filename, "rb");
if (!pSeed_file)
{
print_error("Unable to open file: %s\n", pSeed_filename);
return false;
}
_fseeki64(pSeed_file, 0, SEEK_END);
uint64 seed_file_size = _ftelli64(pSeed_file);
_fseeki64(pSeed_file, 0, SEEK_SET);
lzham_uint32 seed_size = (lzham_uint32)my_min(seed_file_size, 1ULL << dict_size_log2);
if (seed_size)
{
pSeed_bytes = _aligned_malloc(seed_size, 16);
if (pSeed_bytes)
{
num_seed_bytes = (lzham_uint32)seed_size;
if (fread((void*)pSeed_bytes, 1, seed_size, pSeed_file) != seed_size)
{
print_error("Failed reading file!\n");
num_seed_bytes = 0;
_aligned_free((void*)pSeed_bytes);
pSeed_bytes = NULL;
fclose(pSeed_file);
return false;
}
}
}
fclose(pSeed_file);
printf("Read File \"%s\", Size: %u bytes\n", pSeed_filename, seed_size);
}
return true;
}
static bool compress_file(ilzham &lzham_dll, const char* pSrc_filename, const char *pDst_filename, const comp_options &options, const char *pSeed_filename, float *pTotal_comp_time = NULL)
{
if (pTotal_comp_time)
*pTotal_comp_time = 0;
printf("Testing: Streaming compression\n");
FILE *pInFile = fopen(pSrc_filename, "rb");
if (!pInFile)
{
print_error("Unable to read file: %s\n", pSrc_filename);
return false;
}
FILE *pOutFile = fopen(pDst_filename, "wb");
if (!pOutFile)
{
print_error("Unable to create file: %s\n", pDst_filename);
return false;
}
_fseeki64(pInFile, 0, SEEK_END);
uint64 src_file_size = _ftelli64(pInFile);
_fseeki64(pInFile, 0, SEEK_SET);
fputc('L', pOutFile);
fputc('Z', pOutFile);
fputc('H', pOutFile);
fputc('0', pOutFile);
fputc(options.m_dict_size_log2, pOutFile);
for (uint i = 0; i < 8; i++)
{
fputc(static_cast<int>((src_file_size >> (i * 8)) & 0xFF), pOutFile);
}
uint64 cmp_file_header_size = _ftelli64(pOutFile);
const uint cInBufSize = LZHAMTEST_COMP_INPUT_BUFFER_SIZE;
const uint cOutBufSize = LZHAMTEST_COMP_OUTPUT_BUFFER_SIZE;
uint8 *in_file_buf = static_cast<uint8*>(_aligned_malloc(cInBufSize, 16));
uint8 *out_file_buf = static_cast<uint8*>(_aligned_malloc(cOutBufSize, 16));
if ((!in_file_buf) || (!out_file_buf))
{
print_error("Out of memory!\n");
_aligned_free(in_file_buf);
_aligned_free(out_file_buf);
fclose(pInFile);
fclose(pOutFile);
return false;
}
uint64 src_bytes_left = src_file_size;
uint in_file_buf_size = 0;
uint in_file_buf_ofs = 0;
uint64 total_output_bytes = 0;
timer_ticks start_time = timer::get_ticks();
lzham_compress_params params;
memset(¶ms, 0, sizeof(params));
params.m_struct_size = sizeof(lzham_compress_params);
params.m_dict_size_log2 = options.m_dict_size_log2;
params.m_max_helper_threads = options.m_max_helper_threads;
params.m_level = options.m_comp_level;
if (options.m_extreme_parsing)
params.m_compress_flags |= LZHAM_COMP_FLAG_EXTREME_PARSING;
if (options.m_deterministic_parsing)
params.m_compress_flags |= LZHAM_COMP_FLAG_DETERMINISTIC_PARSING;
if (options.m_tradeoff_decomp_rate_for_comp_ratio)
params.m_compress_flags |= LZHAM_COMP_FLAG_TRADEOFF_DECOMPRESSION_RATE_FOR_COMP_RATIO;
params.m_table_update_rate = options.m_table_update_rate;
if (pSeed_filename)
{
if (!read_seed_file(pSeed_filename, params.m_num_seed_bytes, params.m_pSeed_bytes, params.m_dict_size_log2))
{
_aligned_free(in_file_buf);
_aligned_free(out_file_buf);
fclose(pInFile);
fclose(pOutFile);
return false;
}
}
timer_ticks init_start_time = timer::get_ticks();
lzham_compress_state_ptr pComp_state = lzham_dll.lzham_compress_init(¶ms);
timer_ticks total_init_time = timer::get_ticks() - init_start_time;
float total_comp_time = (float)timer::ticks_to_secs(total_init_time);
if ((pComp_state) && (options.m_test_compressor_reinit))
{
if (!lzham_dll.lzham_compress_reinit(pComp_state))
{
lzham_dll.lzham_compress_deinit(pComp_state);
pComp_state = NULL;
}
}
if (!pComp_state)
{
print_error("Failed initializing compressor!\n");
_aligned_free(in_file_buf);
_aligned_free(out_file_buf);
fclose(pInFile);
fclose(pOutFile);
_aligned_free((void*)params.m_pSeed_bytes);
return false;
}
printf("lzham_compress_init took %3.3fms\n", timer::ticks_to_secs(total_init_time)*1000.0f);
lzham_compress_status_t status = LZHAM_COMP_STATUS_FAILED;
// Performs 1 pass normally, or 2 passes to test compressor reinitialization (with a reinit in between the passes).
uint total_passes = options.m_test_compressor_reinit ? 2 : 1;
for (uint pass = 0; pass < total_passes; ++pass)
{
for ( ; ; )
{
if (src_file_size)
{
double total_elapsed_time = timer::ticks_to_secs(timer::get_ticks() - start_time);
double total_bytes_processed = static_cast<double>(src_file_size - src_bytes_left);
double comp_rate = (total_elapsed_time > 0.0f) ? total_bytes_processed / total_elapsed_time : 0.0f;
#ifdef LZHAM_PRINT_OUTPUT_PROGRESS
for (int i = 0; i < 15; i++)
printf("\b\b\b\b");
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));
printf(" \b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
#endif
}
if (in_file_buf_ofs == in_file_buf_size)
{
in_file_buf_size = static_cast<uint>(my_min(cInBufSize, src_bytes_left));
if (fread(in_file_buf, 1, in_file_buf_size, pInFile) != in_file_buf_size)
{
printf("\n");
print_error("Failure reading from source file!\n");
_aligned_free(in_file_buf);
_aligned_free(out_file_buf);
fclose(pInFile);
fclose(pOutFile);
_aligned_free((void*)params.m_pSeed_bytes);
lzham_dll.lzham_compress_deinit(pComp_state);
return false;
}
src_bytes_left -= in_file_buf_size;
in_file_buf_ofs = 0;
}
uint8 *pIn_bytes = &in_file_buf[in_file_buf_ofs];
size_t num_in_bytes = in_file_buf_size - in_file_buf_ofs;
uint8* pOut_bytes = out_file_buf;
size_t out_num_bytes = cOutBufSize;
timer_ticks comp_start_time = timer::get_ticks();
status = lzham_dll.lzham_compress(pComp_state, pIn_bytes, &num_in_bytes, pOut_bytes, &out_num_bytes, src_bytes_left == 0);
timer_ticks comp_time = timer::get_ticks() - comp_start_time;
total_comp_time += (float)timer::ticks_to_secs(comp_time);
if (num_in_bytes)
{
in_file_buf_ofs += (uint)num_in_bytes;
assert(in_file_buf_ofs <= in_file_buf_size);
}
if (out_num_bytes)
{
if (fwrite(out_file_buf, 1, static_cast<uint>(out_num_bytes), pOutFile) != out_num_bytes)
{
printf("\n");
print_error("Failure writing to destination file!\n");
_aligned_free(in_file_buf);
_aligned_free(out_file_buf);
fclose(pInFile);
fclose(pOutFile);
_aligned_free((void*)params.m_pSeed_bytes);
lzham_dll.lzham_compress_deinit(pComp_state);
return false;
}
total_output_bytes += out_num_bytes;
}
if (status >= LZHAM_COMP_STATUS_FIRST_SUCCESS_OR_FAILURE_CODE)
break;
}
#ifdef LZHAM_PRINT_OUTPUT_PROGRESS
for (int i = 0; i < 15; i++)
{
printf("\b\b\b\b \b\b\b\b");
}
#endif
if ((pass == 0) && (total_passes == 2))
{
printf("\n");
uint64 cmp_file_size = _ftelli64(pOutFile);
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);
init_start_time = timer::get_ticks();
if (!lzham_dll.lzham_compress_reinit(pComp_state))
{
print_error("Failed reinitializing compressor!\n");
_aligned_free(in_file_buf);
_aligned_free(out_file_buf);
fclose(pInFile);
fclose(pOutFile);
_aligned_free((void*)params.m_pSeed_bytes);
lzham_dll.lzham_compress_deinit(pComp_state);
return false;
}
total_init_time = timer::get_ticks() - init_start_time;
printf("lzham_compress_reinit took %3.3fms\n", timer::ticks_to_secs(total_init_time)*1000.0f);
fseek(pInFile, 0, SEEK_SET);
fseek(pOutFile, static_cast<long>(cmp_file_header_size), SEEK_SET);
src_bytes_left = src_file_size;
in_file_buf_size = 0;
in_file_buf_ofs = 0;
total_output_bytes = 0;
}
}
src_bytes_left += (in_file_buf_size - in_file_buf_ofs);
timer_ticks deinit_start_time = timer::get_ticks();
uint32 adler32 = lzham_dll.lzham_compress_deinit(pComp_state);
timer_ticks total_deinit_time = timer::get_ticks() - deinit_start_time;
total_comp_time += (float)timer::ticks_to_secs(total_deinit_time);
if (pTotal_comp_time)
*pTotal_comp_time = total_comp_time;
pComp_state = NULL;
timer_ticks end_time = timer::get_ticks();
double total_time = timer::ticks_to_secs(my_max(1, end_time - start_time));
uint64 cmp_file_size = _ftelli64(pOutFile);
_aligned_free(in_file_buf);
in_file_buf = NULL;
_aligned_free(out_file_buf);
out_file_buf = NULL;
_aligned_free((void*)params.m_pSeed_bytes);
params.m_pSeed_bytes = NULL;
fclose(pInFile);
pInFile = NULL;
fclose(pOutFile);
pOutFile = NULL;
if (status != LZHAM_COMP_STATUS_SUCCESS)
{
print_error("Compression failed with status %i\n", status);
return false;
}
if (src_bytes_left)
{
print_error("Compressor failed to consume entire input file!\n");
return false;
}
printf("Success\n");
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);
printf("Compression-only time: %3.6f\nConsumption rate: %9.1f bytes/sec, Emission rate: %9.1f bytes/sec\n", total_comp_time, src_file_size / total_comp_time, cmp_file_size / total_comp_time);
printf("Total 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);
printf("Input file adler32: 0x%08X\n", adler32);
return true;
}
static bool decompress_file(ilzham &lzham_dll, const char* pSrc_filename, const char *pDst_filename, comp_options options, const char *pSeed_filename, float *pTotal_decomp_time = NULL)
{
FILE *pInFile = fopen(pSrc_filename, "rb");
if (!pInFile)
{
print_error("Unable to read file: %s\n", pSrc_filename);
return false;
}
_fseeki64(pInFile, 0, SEEK_END);
uint64 src_file_size = _ftelli64(pInFile);
_fseeki64(pInFile, 0, SEEK_SET);
if (src_file_size < (5+9))
{
print_error("Compressed file is too small!\n");
fclose(pInFile);
return false;
}
int h0 = fgetc(pInFile);
int h1 = fgetc(pInFile);
int h2 = fgetc(pInFile);
int h3 = fgetc(pInFile);
int dict_size = fgetc(pInFile);
if ((h0 != 'L') | (h1 != 'Z') || (h2 != 'H') || (h3 != '0'))
{
print_error("Unrecognized/invalid header in file: %s\n", pSrc_filename);
fclose(pInFile);
return false;
}
if ((dict_size < LZHAM_MIN_DICT_SIZE_LOG2) || (dict_size > LZHAM_MAX_DICT_SIZE_LOG2_X64))
{
print_error("Unrecognized/invalid header in file: %s\n", pSrc_filename);
fclose(pInFile);
return false;
}
FILE *pOutFile = fopen(pDst_filename, "wb");
if (!pOutFile)
{
print_error("Unable to create file: %s\n", pDst_filename);
fclose(pInFile);
return false;
}
uint64 orig_file_size = 0;
for (uint i = 0; i < 8; i++)
{
orig_file_size |= (static_cast<uint64>(fgetc(pInFile)) << (i * 8));
}
int total_header_bytes = static_cast<int>(ftell(pInFile));
// Avoid running out of memory on large files when using unbuffered decompression.
#ifdef _XBOX
if ((options.m_unbuffered_decompression) && (orig_file_size > 128*1024*1024))
#else
if ((options.m_unbuffered_decompression) && (orig_file_size > 256*1024*1024))
#endif
{
printf("Output file is too large for unbuffered decompression - switching to streaming decompression.\n");
options.m_unbuffered_decompression = false;
}
if (options.m_unbuffered_decompression)
printf("Testing: Unbuffered decompression\n");
else
printf("Testing: Streaming decompression\n");
const uint cInBufSize = LZHAMTEST_DECOMP_INPUT_BUFFER_SIZE;
uint8 *in_file_buf = static_cast<uint8*>(_aligned_malloc(cInBufSize, 16));
uint out_buf_size = options.m_unbuffered_decompression ? static_cast<uint>(orig_file_size) : LZHAMTEST_DECOMP_OUTPUT_BUFFER_SIZE;
uint8 *out_file_buf = static_cast<uint8*>(_aligned_malloc(out_buf_size, 16));
if (!out_file_buf)
{
print_error("Failed allocating output buffer!\n");
_aligned_free(in_file_buf);
fclose(pInFile);
fclose(pOutFile);
return false;
}
uint64 src_bytes_left = src_file_size - total_header_bytes;
uint64 dst_bytes_left = orig_file_size;
uint in_file_buf_size = 0;
uint in_file_buf_ofs = 0;
lzham_decompress_params params;
memset(¶ms, 0, sizeof(params));
params.m_struct_size = sizeof(lzham_decompress_params);
params.m_dict_size_log2 = dict_size;
if (options.m_compute_adler32_during_decomp)
params.m_decompress_flags |= LZHAM_DECOMP_FLAG_COMPUTE_ADLER32;
if (options.m_unbuffered_decompression)
params.m_decompress_flags |= LZHAM_DECOMP_FLAG_OUTPUT_UNBUFFERED;
params.m_table_update_rate = options.m_table_update_rate;
timer_ticks start_time = timer::get_ticks();
double decomp_only_time = 0;
if (pSeed_filename)
{
if (!read_seed_file(pSeed_filename, params.m_num_seed_bytes, params.m_pSeed_bytes, params.m_dict_size_log2))
{
_aligned_free(in_file_buf);
_aligned_free(out_file_buf);
fclose(pInFile);
fclose(pOutFile);
return false;
}
}
timer_ticks init_start_time = timer::get_ticks();
lzham_decompress_state_ptr pDecomp_state = lzham_dll.lzham_decompress_init(¶ms);
timer_ticks total_init_time = timer::get_ticks() - init_start_time;
if (!pDecomp_state)
{
print_error("Failed initializing decompressor!\n");
_aligned_free(in_file_buf);
_aligned_free(out_file_buf);
_aligned_free((void*)params.m_pSeed_bytes);
fclose(pInFile);
fclose(pOutFile);
return false;
}
if (pTotal_decomp_time)
*pTotal_decomp_time += (float)timer::ticks_to_secs(total_init_time);
printf("lzham_decompress_init took %3.3fms\n", timer::ticks_to_secs(total_init_time)*1000.0f);
lzham_decompress_status_t status;
for ( ; ; )
{
if (in_file_buf_ofs == in_file_buf_size)
{
in_file_buf_size = static_cast<uint>(my_min(cInBufSize, src_bytes_left));
if (fread(in_file_buf, 1, in_file_buf_size, pInFile) != in_file_buf_size)
{
print_error("Failure reading from source file!\n");
_aligned_free(in_file_buf);
_aligned_free(out_file_buf);
_aligned_free((void*)params.m_pSeed_bytes);
lzham_dll.lzham_decompress_deinit(pDecomp_state);
fclose(pInFile);
fclose(pOutFile);
return false;
}
src_bytes_left -= in_file_buf_size;
in_file_buf_ofs = 0;
}
uint8 *pIn_bytes = &in_file_buf[in_file_buf_ofs];
size_t num_in_bytes = in_file_buf_size - in_file_buf_ofs;
uint8* pOut_bytes = out_file_buf;
size_t out_num_bytes = out_buf_size;
{
timer decomp_only_timer;
decomp_only_timer.start();
status = lzham_dll.lzham_decompress(pDecomp_state, pIn_bytes, &num_in_bytes, pOut_bytes, &out_num_bytes, src_bytes_left == 0);
double secs = decomp_only_timer.get_elapsed_secs();
decomp_only_time += secs;
if (pTotal_decomp_time)
*pTotal_decomp_time += (float)secs;
}
if (num_in_bytes)
{
in_file_buf_ofs += (uint)num_in_bytes;
assert(in_file_buf_ofs <= in_file_buf_size);
}
if (out_num_bytes)
{
if (fwrite(out_file_buf, 1, static_cast<uint>(out_num_bytes), pOutFile) != out_num_bytes)
{
print_error("Failure writing to destination file!\n");
_aligned_free(in_file_buf);
_aligned_free(out_file_buf);
_aligned_free((void*)params.m_pSeed_bytes);
lzham_dll.lzham_decompress_deinit(pDecomp_state);
fclose(pInFile);
fclose(pOutFile);
return false;
}
if (out_num_bytes > dst_bytes_left)
{
print_error("Decompressor wrote too many bytes to destination file!\n");
_aligned_free(in_file_buf);
_aligned_free(out_file_buf);
_aligned_free((void*)params.m_pSeed_bytes);
lzham_dll.lzham_decompress_deinit(pDecomp_state);
fclose(pInFile);
fclose(pOutFile);
return false;
}
dst_bytes_left -= out_num_bytes;
}
if (status >= LZHAM_DECOMP_STATUS_FIRST_SUCCESS_OR_FAILURE_CODE)
break;
}
_aligned_free(in_file_buf);
in_file_buf = NULL;
_aligned_free(out_file_buf);
out_file_buf = NULL;
_aligned_free((void*)params.m_pSeed_bytes);
params.m_pSeed_bytes = NULL;
src_bytes_left += (in_file_buf_size - in_file_buf_ofs);
timer deinit_timer;
deinit_timer.start();
uint32 adler32 = lzham_dll.lzham_decompress_deinit(pDecomp_state);
pDecomp_state = NULL;
if (pTotal_decomp_time)
*pTotal_decomp_time += (float)deinit_timer.get_elapsed_secs();
timer_ticks end_time = timer::get_ticks();
double total_time = timer::ticks_to_secs(my_max(1, end_time - start_time));
fclose(pInFile);
pInFile = NULL;
fclose(pOutFile);
pOutFile = NULL;
if (status != LZHAM_DECOMP_STATUS_SUCCESS)
{
print_error("Decompression FAILED with status %i\n", status);
return false;
}
if (dst_bytes_left)
{
print_error("Decompressor FAILED to output the entire output file!\n");
return false;
}
if (src_bytes_left)
{
print_error("Decompressor FAILED to read " QUAD_INT_FMT " bytes from input buffer (ignore if testing reinit() because we don't truncate the output file between passes)\n", src_bytes_left);
}
printf("Success\n");
printf("Source file size: " QUAD_INT_FMT ", Decompressed file size: " QUAD_INT_FMT "\n", src_file_size, orig_file_size);
printf("Decompressed adler32: 0x%08X\n", adler32);
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);
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);
return true;
}
static bool compare_files(const char *pFilename1, const char* pFilename2)
{
FILE* pFile1 = open_file_with_retries(pFilename1, "rb");
if (!pFile1)
{
print_error("Failed opening file: %s\n", pFilename1);
return false;
}
FILE* pFile2 = open_file_with_retries(pFilename2, "rb");
if (!pFile2)
{
print_error("Failed opening file: %s\n", pFilename2);
fclose(pFile1);
return false;
}
fseek(pFile1, 0, SEEK_END);
int64 fileSize1 = _ftelli64(pFile1);
fseek(pFile1, 0, SEEK_SET);
fseek(pFile2, 0, SEEK_END);
int64 fileSize2 = _ftelli64(pFile2);
fseek(pFile2, 0, SEEK_SET);
if (fileSize1 != fileSize2)
{
print_error("Files to compare are not the same size: %I64i vs. %I64i.\n", fileSize1, fileSize2);
fclose(pFile1);
fclose(pFile2);
return false;
}
const uint cBufSize = 1024 * 1024;
std::vector<uint8> buf1(cBufSize);
std::vector<uint8> buf2(cBufSize);
int64 bytes_remaining = fileSize1;
while (bytes_remaining)
{
const uint bytes_to_read = static_cast<uint>(my_min(cBufSize, bytes_remaining));
if (fread(&buf1.front(), bytes_to_read, 1, pFile1) != 1)
{
print_error("Failed reading from file: %s\n", pFilename1);
fclose(pFile1);
fclose(pFile2);
return false;
}
if (fread(&buf2.front(), bytes_to_read, 1, pFile2) != 1)
{
print_error("Failed reading from file: %s\n", pFilename2);
fclose(pFile1);
fclose(pFile2);
return false;
}
if (memcmp(&buf1.front(), &buf2.front(), bytes_to_read) != 0)
{
print_error("File data comparison failed!\n");
fclose(pFile1);
fclose(pFile2);
return false;
}
bytes_remaining -= bytes_to_read;
}
fclose(pFile1);
fclose(pFile2);
return true;
}
typedef std::vector< std::string > string_array;
#if defined(WIN32) || defined(_XBOX)
static bool find_files(std::string pathname, const std::string &filename, string_array &files, bool recursive, int level = 0)
{
if (!pathname.empty())
{
char c = pathname[pathname.size() - 1];
if ((c != ':') && (c != '\\') && (c != '/'))
pathname += "\\";
}
WIN32_FIND_DATAA find_data;
HANDLE findHandle = FindFirstFileA((pathname + filename).c_str(), &find_data);
if (findHandle == INVALID_HANDLE_VALUE)
{
HRESULT hres = GetLastError();
if ((level == 0) && (hres != NO_ERROR) && (hres != ERROR_FILE_NOT_FOUND))
return false;
}
else
{