forked from schnaader/precomp-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
precomp.cpp
9725 lines (8105 loc) · 312 KB
/
precomp.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
/* Copyright 2006-2018 Christian Schneider
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#ifdef PRECOMPDLL
#define DLL __declspec(dllexport)
#endif
// version information
#define V_MAJOR 0
#define V_MINOR 4
#define V_MINOR2 7
//#define V_STATE "ALPHA"
#define V_STATE "DEVELOPMENT"
#define V_MSG "USE FOR TESTING ONLY"
//#define V_MSG "USE AT YOUR OWN RISK!"
#ifdef UNIX
#define V_OS "Unix"
#else
#define V_OS "Windows"
#endif
#ifdef BIT64
#define V_BIT "64-bit"
#else
#define V_BIT "32-bit"
#endif
// batch error levels
#define RETURN_NOTHING_DECOMPRESSED 2
#define ERR_DISK_FULL 3
#define ERR_TEMP_FILE_DISAPPEARED 4
#define ERR_IGNORE_POS_TOO_BIG 5
#define ERR_IDENTICAL_BYTE_SIZE_TOO_BIG 6
#define ERR_RECURSION_DEPTH_TOO_BIG 7
#define ERR_ONLY_SET_RECURSION_DEPTH_ONCE 8
#define ERR_ONLY_SET_MIN_SIZE_ONCE 9
#define ERR_DONT_USE_SPACE 10
#define ERR_MORE_THAN_ONE_OUTPUT_FILE 11
#define ERR_MORE_THAN_ONE_INPUT_FILE 12
#define ERR_CTRL_C 13
#define ERR_INTENSE_MODE_LIMIT_TOO_BIG 14
#define ERR_BRUTE_MODE_LIMIT_TOO_BIG 15
#define ERR_ONLY_SET_LZMA_MEMORY_ONCE 16
#define ERR_ONLY_SET_LZMA_THREAD_ONCE 17
#define ERR_ONLY_SET_LZMA_FILTERS_ONCE 18
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <fstream>
#include <sstream>
#include <string>
#include <signal.h>
#include <thread>
#include <set>
#ifdef MINGW
#ifndef _GLIBCXX_HAS_GTHREADS
#include "contrib\mingw_std_threads\mingw.thread.h"
#endif // _GLIBCXX_HAS_GTHREADS
#endif
#ifdef _MSC_VER
#include <io.h>
#define ftruncate _chsize
#else
#include <unistd.h>
#endif
#ifndef UNIX
#include <conio.h>
#include <windows.h>
#define PATH_DELIM '\\'
#else
#include <time.h>
#include <sys/time.h>
#include <errno.h>
#define PATH_DELIM '/'
#endif
using namespace std;
#include "contrib/bzip2/bzlib.h"
#include "contrib/giflib/precomp_gif.h"
#include "contrib/packjpg/precomp_jpg.h"
#include "contrib/packmp3/precomp_mp3.h"
#include "contrib/zlib/zlib.h"
#define CHUNK 262144 // 256 KB buffersize
#define DIV3CHUNK 262143 // DIV3CHUNK is a bit smaller/larger than CHUNK, so that DIV3CHUNK mod 3 = 0
#define CHECKBUF_SIZE 4096
#define COPY_BUF_SIZE 512
#define FAST_COPY_WORK_SIGN_DIST 64 // update work sign after (FAST_COPY_WORK_SIGN_DIST * COPY_BUF_SIZE) bytes
#define COMP_CHUNK 512
#define IN_BUF_SIZE 65536 //input buffer
#define PENALTY_BYTES_TOLERANCE 160
#define IDENTICAL_COMPRESSED_BYTES_TOLERANCE 32
#define MAX_IO_BUFFER_SIZE 64 * 1024 * 1024
unsigned char* decomp_io_buf = NULL;
unsigned char copybuf[COPY_BUF_SIZE];
unsigned char in_buf[IN_BUF_SIZE];
long long in_buf_pos;
int cb; // "checkbuf"
unsigned char in[CHUNK];
unsigned char out[CHUNK];
// name of temporary files
char metatempfile[18] = "~temp00000000.dat";
char tempfile0[19] = "~temp000000000.dat";
char tempfile1[19] = "~temp000000001.dat";
char tempfile2[19] = "~temp000000002.dat";
char tempfile3[19] = "~temp000000003.dat";
char* tempfilelist;
int tempfilelist_count = 0;
#include "precomp.h"
static char work_signs[5] = "|/-\\";
int work_sign_var = 0;
long long work_sign_start_time = get_time_ms();
// recursion
int recursion_stack_size = 0;
int recursion_depth = 0;
int max_recursion_depth = 10;
int max_recursion_depth_used = 0;
bool max_recursion_depth_reached = false;
unsigned char* recursion_stack = NULL;
void recursion_stack_push(void* var, int var_size);
void recursion_stack_pop(void* var, int var_size);
void recursion_push();
void recursion_pop();
float global_min_percent = 0;
float global_max_percent = 100;
// compression-on-the-fly
unsigned char otf_in[CHUNK];
unsigned char otf_out[CHUNK];
#include "contrib/liblzma/precomp_xz.h"
lzma_stream otf_xz_stream_c = LZMA_STREAM_INIT, otf_xz_stream_d = LZMA_STREAM_INIT;
bool otf_xz_filter_enabled[6];
bool otf_xz_filter_delta_enabled = false;
int otf_xz_filter_delta_distance = 0;
int otf_xz_filter_used_count = 0;
int compression_otf_method = OTF_XZ_MT;
uint64_t compression_otf_max_memory = 0;
int compression_otf_thread_count = 0;
int conversion_from_method;
int conversion_to_method;
bool decompress_otf_end = false;
bz_stream otf_bz2_stream_c, otf_bz2_stream_d;
FILE* fin = NULL;
FILE* fout = NULL;
FILE* ftempout = NULL;
FILE* frecomp = NULL;
FILE* fdecomp = NULL;
FILE* fpack = NULL;
FILE* fpng = NULL;
FILE* fjpg = NULL;
FILE* fmp3 = NULL;
long long retval;
long long input_file_pos;
bool DEBUG_MODE = false;
bool compressed_data_found;
bool uncompressed_data_in_work;
long long uncompressed_length = -1;
long long uncompressed_pos;
long long uncompressed_start;
char* input_file_name = NULL;
char* output_file_name = NULL;
long long start_time, sec_time;
long long fin_length;
long long uncompressed_bytes_written = 0;
long long uncompressed_bytes_total = 0;
bool show_lzma_progress = true;
char lzma_progress_text[70];
int old_lzma_progress_text_length = -1;
int lzma_mib_total = 0, lzma_mib_written = 0;
int comp_mem_level_count[81];
zLibMTF MTF;
bool zlib_level_was_used[81];
bool anything_was_used;
bool level_switch_used = false;
bool non_zlib_was_used;
// statistics
unsigned int recompressed_streams_count = 0;
unsigned int recompressed_pdf_count = 0;
unsigned int recompressed_pdf_count_8_bit = 0;
unsigned int recompressed_pdf_count_24_bit = 0;
unsigned int recompressed_zip_count = 0;
unsigned int recompressed_gzip_count = 0;
unsigned int recompressed_png_count = 0;
unsigned int recompressed_png_multi_count = 0;
unsigned int recompressed_gif_count = 0;
unsigned int recompressed_jpg_count = 0;
unsigned int recompressed_jpg_prog_count = 0;
unsigned int recompressed_mp3_count = 0;
unsigned int recompressed_swf_count = 0;
unsigned int recompressed_base64_count = 0;
unsigned int recompressed_bzip2_count = 0;
unsigned int recompressed_zlib_count = 0; // intense mode
unsigned int recompressed_brute_count = 0; // brute mode
unsigned int decompressed_streams_count = 0;
unsigned int decompressed_pdf_count = 0;
unsigned int decompressed_pdf_count_8_bit = 0;
unsigned int decompressed_pdf_count_24_bit = 0;
unsigned int decompressed_zip_count = 0;
unsigned int decompressed_gzip_count = 0;
unsigned int decompressed_png_count = 0;
unsigned int decompressed_png_multi_count = 0;
unsigned int decompressed_gif_count = 0;
unsigned int decompressed_jpg_count = 0;
unsigned int decompressed_jpg_prog_count = 0;
unsigned int decompressed_mp3_count = 0;
unsigned int decompressed_swf_count = 0;
unsigned int decompressed_base64_count = 0;
unsigned int decompressed_bzip2_count = 0;
unsigned int decompressed_zlib_count = 0; // intense mode
unsigned int decompressed_brute_count = 0; // brute mode
#define P_NONE 0
#define P_COMPRESS 1
#define P_DECOMPRESS 2
#define P_CONVERT 3
int comp_decomp_state = P_NONE;
// penalty bytes
#define MAX_PENALTY_BYTES 16384
#ifndef PRECOMPDLL
char* penalty_bytes = new char[MAX_PENALTY_BYTES];
char* local_penalty_bytes = new char[MAX_PENALTY_BYTES];
char* best_penalty_bytes = new char[MAX_PENALTY_BYTES];
#else
char* penalty_bytes;
char* local_penalty_bytes;
char* best_penalty_bytes;
#endif
int penalty_bytes_len = 0;
long long* ignore_list = NULL; // positions to ignore
int ignore_list_len = 0;
long long saved_input_file_pos, saved_cb;
int min_ident_size = 4;
int min_ident_size_intense_brute_mode = 64;
set<long long>* intense_ignore_offsets = NULL;
set<long long>* brute_ignore_offsets = NULL;
unsigned char zlib_header[2];
unsigned int* idat_lengths = NULL;
unsigned int* idat_crcs = NULL;
int idat_count;
long long suppress_mp3_type_until[16];
long long suppress_mp3_big_value_pairs_sum;
long long mp3_parsing_cache_second_frame;
long long mp3_parsing_cache_n;
long long mp3_parsing_cache_mp3_length;
bool fast_mode = false;
bool intense_mode = false;
bool brute_mode = false;
bool pdf_bmp_mode = false;
bool prog_only = false;
bool use_mjpeg = true;
int intense_mode_depth_limit = -1;
int brute_mode_depth_limit = -1;
// compression type bools
bool use_pdf = true;
bool use_zip = true;
bool use_gzip = true;
bool use_png = true;
bool use_gif = true;
bool use_jpg = true;
bool use_mp3 = true;
bool use_swf = true;
bool use_base64 = true;
bool use_bzip2 = true;
// Precomp DLL things
#ifdef PRECOMPDLL
#include "precomp_dll.h"
// get copyright message
// msg = Buffer for error messages (256 bytes buffer size are enough)
DLL void get_copyright_msg(char* msg) {
if (V_MINOR2 == 0) {
sprintf(msg, "Precomp DLL v%i.%i (c) 2006-2018 by Christian Schneider",V_MAJOR,V_MINOR);
} else {
sprintf(msg, "Precomp DLL v%i.%i.%i (c) 2006-2018 by Christian Schneider",V_MAJOR,V_MINOR,V_MINOR2);
}
}
void setSwitches(Switches switches) {
compression_otf_method = switches.compression_method;
show_lzma_progress = (compression_otf_method == OTF_XZ_MT);
ignore_list = switches.ignore_list;
ignore_list_len = switches.ignore_list_len;
intense_mode = switches.intense_mode;
fast_mode = switches.fast_mode;
brute_mode = switches.brute_mode;
pdf_bmp_mode = switches.pdf_bmp_mode;
prog_only = switches.prog_only;
DEBUG_MODE = switches.debug_mode;
min_ident_size = switches.min_ident_size;
compression_otf_max_memory = switches.compression_otf_max_memory;
compression_otf_thread_count = switches.compression_otf_thread_count;
use_pdf = switches.use_pdf;
use_zip = switches.use_zip;
use_gzip = switches.use_gzip;
use_png = switches.use_png;
use_gif = switches.use_gif;
use_jpg = switches.use_jpg;
use_mp3 = switches.use_mp3;
use_swf = switches.use_swf;
use_base64 = switches.use_base64;
use_bzip2 = switches.use_bzip2;
use_mjpeg = switches.use_mjpeg;
if (switches.level_switch) {
for (int i = 0; i < 81; i++) {
if (switches.use_zlib_level[i]) {
comp_mem_level_count[i] = 0;
} else {
comp_mem_level_count[i] = -1;
}
}
level_switch_used = true;
}
}
// precompress a file
// in_file = input filename
// out_file = output filename
// msg = Buffer for error messages (256 bytes buffer size are enough)
DLL bool precompress_file(char* in_file, char* out_file, char* msg, Switches switches) {
// init compression and memory level count
for (int i = 0; i < 81; i++) {
comp_mem_level_count[i] = 0;
zlib_level_was_used[i] = false;
}
fin_length = fileSize64(in_file);
fin = fopen(in_file, "rb");
if (fin == NULL) {
sprintf(msg, "ERROR: Input file \"%s\" doesn't exist", in_file);
return false;
}
fout = fopen(out_file, "wb");
if (fout == NULL) {
sprintf(msg, "ERROR: Can't create output file \"%s\"", out_file);
return false;
}
setSwitches(switches);
input_file_name = new char[strlen(in_file)+1];
strcpy(input_file_name, in_file);
output_file_name = new char[strlen(out_file)+1];
strcpy(output_file_name, out_file);
start_time = get_time_ms();
penalty_bytes = new char[MAX_PENALTY_BYTES];
local_penalty_bytes = new char[MAX_PENALTY_BYTES];
best_penalty_bytes = new char[MAX_PENALTY_BYTES];
compress_file();
return true;
}
// recompress a file
// in_file = input filename
// out_file = output filename
// msg = Buffer for error messages (256 bytes buffer size are enough)
DLL bool recompress_file(char* in_file, char* out_file, char* msg, Switches switches) {
// init compression and memory level count
for (int i = 0; i < 81; i++) {
comp_mem_level_count[i] = 0;
zlib_level_was_used[i] = false;
}
fin_length = fileSize64(in_file);
fin = fopen(in_file, "rb");
if (fin == NULL) {
sprintf(msg, "ERROR: Input file \"%s\" doesn't exist", in_file);
return false;
}
fout = fopen(out_file, "wb");
if (fout == NULL) {
sprintf(msg, "ERROR: Can't create output file \"%s\"", out_file);
return false;
}
setSwitches(switches);
input_file_name = new char[strlen(in_file)+1];
strcpy(input_file_name, in_file);
output_file_name = new char[strlen(out_file)+1];
strcpy(output_file_name, out_file);
start_time = get_time_ms();
penalty_bytes = new char[MAX_PENALTY_BYTES];
local_penalty_bytes = new char[MAX_PENALTY_BYTES];
best_penalty_bytes = new char[MAX_PENALTY_BYTES];
decompress_file();
return true;
}
// test if a file contains streams that can be precompressed
DLL bool file_precompressable(char* in, char* msg) {
return false;
}
#else
int main(int argc, char* argv[])
{
int return_errorlevel = 0;
// register CTRL-C handler
(void) signal(SIGINT, ctrl_c_handler);
#ifndef COMFORT
switch (init(argc, argv)) {
#else
switch (init_comfort(argc, argv)) {
#endif
case P_COMPRESS:
{
start_time = get_time_ms();
if (!compress_file()) { // none of the streams could be decompressed
return_errorlevel = RETURN_NOTHING_DECOMPRESSED;
}
break;
}
case P_DECOMPRESS:
{
start_time = get_time_ms();
decompress_file();
break;
}
case P_CONVERT:
{
start_time = get_time_ms();
convert_file();
break;
}
}
#ifdef COMFORT
wait_for_key();
#endif
return return_errorlevel;
}
#endif
#ifndef PRECOMPDLL
#ifndef COMFORT
int init(int argc, char* argv[]) {
int i, j;
bool appended_pcf = false;
printf("\n");
if (V_MINOR2 == 0) {
printf("Precomp v%i.%i %s %s - %s version",V_MAJOR,V_MINOR,V_OS,V_BIT,V_STATE);
} else {
printf("Precomp v%i.%i.%i %s %s - %s version",V_MAJOR,V_MINOR,V_MINOR2,V_OS,V_BIT,V_STATE);
}
printf(" - %s\n",V_MSG);
printf("Free for non-commercial use - Copyright 2006-2018 by Christian Schneider\n\n");
// init compression and memory level count
bool use_zlib_level[81];
for (i = 0; i < 81; i++) {
comp_mem_level_count[i] = 0;
zlib_level_was_used[i] = false;
use_zlib_level[i] = true;
}
// init MP3 suppression
for (i = 0; i < 16; i++) {
suppress_mp3_type_until[i] = -1;
}
suppress_mp3_big_value_pairs_sum = -1;
mp3_parsing_cache_second_frame = -1;
// init LZMA filters
for (int i = 0; i < 6; i++) {
otf_xz_filter_enabled[i] = false;
}
bool valid_syntax = false;
bool input_file_given = false;
bool output_file_given = false;
int operation = P_COMPRESS;
bool parse_on = true;
bool level_switch = false;
bool min_ident_size_set = false;
bool recursion_depth_set = false;
bool lzma_max_memory_set = false;
bool lzma_thread_count_set = false;
bool lzma_filters_set = false;
bool long_help = false;
bool preserve_extension = false;
for (i = 1; (i < argc) && (parse_on); i++) {
if (argv[i][0] == '-') { // switch
if (input_file_given) {
valid_syntax = false;
parse_on = false;
break;
}
switch (toupper(argv[i][1])) {
case 0:
{
valid_syntax = false;
parse_on = false;
break;
}
case 'I':
{
if (toupper(argv[i][2]) == 'N') { // intense mode
if ((toupper(argv[i][3]) == 'T') && (toupper(argv[i][4]) == 'E')
&& (toupper(argv[i][5]) == 'N') && (toupper(argv[i][6]) == 'S') && (toupper(argv[i][7]) == 'E')) {
intense_mode = true;
if (strlen(argv[i]) > 8) {
int intense_mode_limit = 0;
int multiplicator = 1;
for (j = strlen(argv[i]) - 9; j >= 0; j--) {
if ((argv[i][j+8] < '0') || (argv[i][j+8] > '9')) {
printf("ERROR: Only numbers allowed for intense mode level limit\n");
exit(1);
}
intense_mode_limit += ((long long)(argv[i][j+8])-'0') * multiplicator;
if ((multiplicator * 10) < multiplicator) {
error(ERR_INTENSE_MODE_LIMIT_TOO_BIG);
}
multiplicator *= 10;
}
intense_mode_depth_limit = intense_mode_limit;
}
} else {
printf("ERROR: Unknown switch \"%s\"\n", argv[i]);
exit(1);
}
} else {
long long ignore_pos = 0;
long long multiplicator = 1;
for (j = (strlen(argv[i])-3); j >= 0; j--) {
if ((argv[i][j+2] < '0') || (argv[i][j+2] > '9')) {
printf("ERROR: Only numbers allowed for ignore position\n");
exit(1);
}
ignore_pos += ((long long)(argv[i][j+2])-'0') * multiplicator;
if ((multiplicator * 10) < multiplicator) {
error(ERR_IGNORE_POS_TOO_BIG);
}
multiplicator *= 10;
}
ignore_list = (long long*)realloc(ignore_list, (ignore_list_len + 1) * sizeof(long long));
ignore_list[ignore_list_len] = ignore_pos;
ignore_list_len++;
}
break;
}
case 'D':
{
if (recursion_depth_set) {
error(ERR_ONLY_SET_RECURSION_DEPTH_ONCE);
}
unsigned int max_recursion_d = 0;
unsigned int multiplicator = 1;
for (j = (strlen(argv[i])-3); j >= 0; j--) {
if ((argv[i][j+2] < '0') || (argv[i][j+2] > '9')) {
printf("ERROR: Only numbers allowed for maximal recursion depth\n");
exit(1);
}
max_recursion_d += ((unsigned int)(argv[i][j+2])-'0') * multiplicator;
if ((multiplicator * 10) < multiplicator) {
error(ERR_RECURSION_DEPTH_TOO_BIG);
}
multiplicator *= 10;
}
max_recursion_depth = max_recursion_d;
recursion_depth_set = true;
break;
}
case 'S':
{
if (min_ident_size_set) {
error(ERR_ONLY_SET_MIN_SIZE_ONCE);
}
if (strlen(argv[i]) == 2) {
printf("ERROR: Number needed to set minimal identical byte size\n");
exit(1);
}
unsigned int ident_size = 0;
unsigned int multiplicator = 1;
for (j = (strlen(argv[i]) - 3); j >= 0; j--) {
if ((argv[i][j+2] < '0') || (argv[i][j+2] > '9')) {
printf("ERROR: Only numbers allowed for minimal identical byte size\n");
exit(1);
}
ident_size += ((unsigned int)(argv[i][j+2])-'0') * multiplicator;
if ((multiplicator * 10) < multiplicator) {
error(ERR_IDENTICAL_BYTE_SIZE_TOO_BIG);
}
multiplicator *= 10;
}
min_ident_size = ident_size;
min_ident_size_set = true;
break;
}
case 'B':
{
if ((toupper(argv[i][2]) == 'R') && (toupper(argv[i][3]) == 'U') && (toupper(argv[i][4]) == 'T') && (toupper(argv[i][5]) == 'E')) {
brute_mode = true;
if (strlen(argv[i]) > 6) {
int brute_mode_limit = 0;
int multiplicator = 1;
for (j = strlen(argv[i]) - 7; j >= 0; j--) {
if ((argv[i][j+6] < '0') || (argv[i][j+6] > '9')) {
printf("ERROR: Only numbers allowed for brute mode level limit\n");
exit(1);
}
brute_mode_limit += ((long long)(argv[i][j+6])-'0') * multiplicator;
if ((multiplicator * 10) < multiplicator) {
error(ERR_INTENSE_MODE_LIMIT_TOO_BIG);
}
multiplicator *= 10;
}
brute_mode_depth_limit = brute_mode_limit;
}
} else {
printf("ERROR: Unknown switch \"%s\"\n", argv[i]);
exit(1);
}
break;
}
case 'L':
{
if ((toupper(argv[i][2]) == 'O') && (toupper(argv[i][3]) == 'N') && (toupper(argv[i][4]) == 'G')
&& (toupper(argv[i][5]) == 'H') && (toupper(argv[i][6]) == 'E') && (toupper(argv[i][7]) == 'L')
&& (toupper(argv[i][8]) == 'P')) {
long_help = true;
} else if (toupper(argv[i][2]) == 'M') { // LZMA max. memory
if (lzma_max_memory_set) {
error(ERR_ONLY_SET_LZMA_MEMORY_ONCE);
}
int multiplicator = 1;
for (j = (strlen(argv[i]) - 4); j >= 0; j--) {
if ((argv[i][j+3] < '0') || (argv[i][j+3] > '9')) {
printf("ERROR: Only numbers allowed for LZMA maximal memory\n");
exit(1);
}
compression_otf_max_memory += ((unsigned int)(argv[i][j+3])-'0') * multiplicator;
if ((multiplicator * 10) < multiplicator) {
exit(1);
}
multiplicator *= 10;
}
lzma_max_memory_set = true;
} else if (toupper(argv[i][2]) == 'T') { // LZMA thread count
if (lzma_thread_count_set) {
error(ERR_ONLY_SET_LZMA_THREAD_ONCE);
}
int multiplicator = 1;
for (j = (strlen(argv[i]) - 4); j >= 0; j--) {
if ((argv[i][j + 3] < '0') || (argv[i][j + 3] > '9')) {
printf("ERROR: Only numbers allowed for LZMA thread count\n");
exit(1);
}
compression_otf_thread_count += ((unsigned int)(argv[i][j + 3]) - '0') * multiplicator;
if ((multiplicator * 10) < multiplicator) {
exit(1);
}
multiplicator *= 10;
}
lzma_thread_count_set = true;
} else if (toupper(argv[i][2]) == 'F') { // LZMA filters
if (lzma_filters_set) {
error(ERR_ONLY_SET_LZMA_FILTERS_ONCE);
}
switch (argv[i][3]) {
case '+':
break;
case '-':
if (argv[i][4] != 0) {
printf("ERROR: \"-lf-\" must not be followed by anything\n");
exit(1);
break;
}
break;
default:
printf("ERROR: Only + or - after \"-lf\" allowed\n");
exit(1);
break;
}
int argindex = 4;
while (argv[i][argindex] != 0) {
switch (toupper(argv[i][argindex])) {
case 'X':
otf_xz_filter_enabled[0] = true;
break;
case 'P':
otf_xz_filter_enabled[1] = true;
break;
case 'I':
otf_xz_filter_enabled[2] = true;
break;
case 'A':
otf_xz_filter_enabled[3] = true;
break;
case 'T':
otf_xz_filter_enabled[4] = true;
break;
case 'S':
otf_xz_filter_enabled[5] = true;
break;
case 'D':
{
argindex++;
char nextchar = argv[i][argindex];
if ((nextchar < '0') || (nextchar > '9')) {
printf("ERROR: LZMA delta filter must be followed by a distance (1..256)\n");
exit(1);
}
otf_xz_filter_delta_enabled = true;
while ((argv[i][argindex] > '0') && (argv[i][argindex] < '9')) {
otf_xz_filter_delta_distance *= 10;
otf_xz_filter_delta_distance += (argv[i][argindex] - '0');
argindex++;
}
if ((otf_xz_filter_delta_distance < 1) || (otf_xz_filter_delta_distance > 256)) {
printf("ERROR: LZMA delta filter distance must be in range 1..256\n");
exit(1);
}
argindex--;
}
break;
default:
printf("ERROR: Unknown LZMA filter type \"%c\"\n", argv[i][argindex]);
exit(1);
break;
}
otf_xz_filter_used_count++;
if (otf_xz_filter_used_count > 3) {
printf("ERROR: Only up to 3 LZMA filters can be used at the same time\n");
exit(1);
}
argindex++;
}
lzma_filters_set = true;
break;
} else {
printf("ERROR: Unknown switch \"%s\"\n", argv[i]);
exit(1);
}
break;
}
case 'P':
{
if ((toupper(argv[i][2]) == 'D') && (toupper(argv[i][3]) == 'F') && (toupper(argv[i][4]) == 'B') && (toupper(argv[i][5]) == 'M') && (toupper(argv[i][6]) == 'P')) {
switch (argv[i][7]) {
case '+':
pdf_bmp_mode = true;
break;
case '-':
pdf_bmp_mode = false;
break;
default:
printf("ERROR: Only + or - for this switch allowed\n");
exit(1);
break;
}
if (argv[i][8] != 0) {
printf("ERROR: Unknown switch \"%s\"\n", argv[i]);
exit(1);
}
} else if ((toupper(argv[i][2]) == 'R') && (toupper(argv[i][3]) == 'O') && (toupper(argv[i][4]) == 'G') && (toupper(argv[i][5]) == 'O') && (toupper(argv[i][6]) == 'N') && (toupper(argv[i][7]) == 'L') && (toupper(argv[i][8]) == 'Y')) {
switch (argv[i][9]) {
case '+':
prog_only = true;
break;
case '-':
prog_only = false;
break;
default:
printf("ERROR: Only + or - for this switch allowed\n");
exit(1);
break;
}
if (argv[i][8] != 0) {
printf("ERROR: Unknown switch \"%s\"\n", argv[i]);
exit(1);
}
} else {
printf("ERROR: Unknown switch \"%s\"\n", argv[i]);
exit(1);
}
break;
}
case 'T':
{
bool set_to;
switch (argv[i][2]) {
case '+':
use_pdf = false;
use_zip = false;
use_gzip = false;
use_png = false;
use_gif = false;
use_jpg = false;
use_mp3 = false;
use_swf = false;
use_base64 = false;
use_bzip2 = false;
set_to = true;
break;
case '-':
use_pdf = true;
use_zip = true;
use_gzip = true;
use_png = true;
use_gif = true;
use_jpg = true;
use_mp3 = true;
use_swf = true;
use_base64 = true;
use_bzip2 = true;
set_to = false;
break;
default:
printf("ERROR: Only + or - for type switch allowed\n");
exit(1);
break;
}
for (j = 3; j < (int)strlen(argv[i]); j++) {
switch (toupper(argv[i][j])) {
case 'P': // PDF
use_pdf = set_to;
break;
case 'Z': // ZIP
use_zip = set_to;
break;
case 'G': // GZip
use_gzip = set_to;
break;
case 'N': // PNG
use_png = set_to;
break;
case 'F': // GIF
use_gif = set_to;
break;
case 'J': // JPG
use_jpg = set_to;
break;
case '3': // MP3
use_mp3 = set_to;
break;
case 'S': // SWF
use_swf = set_to;
break;
case 'M': // MIME Base64
use_base64 = set_to;
break;
case 'B': // bZip2
use_bzip2 = set_to;
break;
default:
printf("ERROR: Invalid compression type %c\n", argv[i][j]);
exit(1);
break;
}
}
break;
}
case 'C':
{
switch (toupper(argv[i][2])) {
case 'N': // no compression
compression_otf_method = OTF_NONE;
break;
case 'B': // bZip2
compression_otf_method = OTF_BZIP2;
break;
case 'L': // lzma2 multithreaded
compression_otf_method = OTF_XZ_MT;
break;
default:
printf("ERROR: Invalid compression method %c\n", argv[i][2]);
exit(1);
break;
}
if (argv[i][3] != 0) { // Extra Parameters?
printf("ERROR: Unknown switch \"%s\"\n", argv[i]);
exit(1);
}
show_lzma_progress = (compression_otf_method == OTF_XZ_MT);
break;
}
case 'N':
{
switch (toupper(argv[i][2])) {
case 'N': // no compression
conversion_to_method = OTF_NONE;
break;
case 'B': // bZip2
conversion_to_method = OTF_BZIP2;
break;
case 'L': // lzma2 multithreaded
conversion_to_method = OTF_XZ_MT;
break;
default:
printf("ERROR: Invalid conversion method %c\n", argv[i][2]);
exit(1);
break;
}
if (argv[i][3] != 0) { // Extra Parameters?
printf("ERROR: Unknown switch \"%s\"\n", argv[i]);
exit(1);
}
operation = P_CONVERT;
break;
}
case 'V':
{
DEBUG_MODE = true;
if (argv[i][2] != 0) { // Extra Parameters?
printf("ERROR: Unknown switch \"%s\"\n", argv[i]);
exit(1);
}
break;
}
case 'R':
{
operation = P_DECOMPRESS;
if (argv[i][2] != 0) { // Extra Parameters?
printf("ERROR: Unknown switch \"%s\"\n", argv[i]);
exit(1);
}
break;
}
case 'Z':
{
if (toupper(argv[i][2]) == 'L') {