From 5eb7413d6f5215677f16058735ea53bd543defb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Comerci?= Date: Tue, 16 Aug 2022 23:12:06 -0300 Subject: [PATCH 1/2] Initial changes for stdin/stdout support --- precomp.cpp | 108 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 75 insertions(+), 33 deletions(-) diff --git a/precomp.cpp b/precomp.cpp index b37224e..756f32f 100644 --- a/precomp.cpp +++ b/precomp.cpp @@ -92,6 +92,18 @@ using namespace std; +// This I shamelessly lifted from https://web.archive.org/web/20090907131154/http://www.cs.toronto.edu:80/~ramona/cosmin/TA/prog/sysconf/ +// (credit to this StackOverflow answer for pointing me to it https://stackoverflow.com/a/1613677) +// It allows us to portably (at least for Windows/Linux/Mac) set a std stream as binary +#define STDIN 0 +#define STDOUT 1 +#define STDERR 2 +#ifndef __unix +# define SET_BINARY_MODE(handle) setmode(handle, O_BINARY) +#else +# define SET_BINARY_MODE(handle) ((void)0) +#endif + #include "contrib/bzip2/bzlib.h" #include "contrib/giflib/precomp_gif.h" #include "contrib/packjpg/precomp_jpg.h" @@ -125,6 +137,8 @@ int cb; // "checkbuf" unsigned char in[CHUNK]; unsigned char out[CHUNK]; +bool header_already_read = false; + // name of temporary files char metatempfile[18] = "~temp00000000.dat"; char tempfile0[19] = "~temp000000000.dat"; @@ -1090,7 +1104,7 @@ int init(int argc, char* argv[]) { // dot in output file name? If not, use .pcf extension char* dot_at_pos = strrchr(output_file_name, '.'); - if ((dot_at_pos == NULL) || ((backslash_at_pos != NULL) && (backslash_at_pos > dot_at_pos))) { + if (strcmp(output_file_name, "stdout") != 0 && ((dot_at_pos == NULL) || ((backslash_at_pos != NULL) && (backslash_at_pos > dot_at_pos)))) { strcpy(output_file_name + strlen(argv[i]) - 2, ".pcf"); appended_pcf = true; } @@ -1139,13 +1153,24 @@ int init(int argc, char* argv[]) { input_file_given = true; input_file_name = argv[i]; - fin_length = fileSize64(argv[i]); + if (strcmp(input_file_name, "stdin") == 0) { + if (operation != P_DECOMPRESS) { + print_to_console("ERROR: Reading from stdin only supported for recompressing.\n"); + exit(1); + } + // Read binary from stdin + SET_BINARY_MODE(STDIN); + fin = stdin; + } + else { + fin_length = fileSize64(argv[i]); - fin = fopen(argv[i],"rb"); - if (fin == NULL) { - printf("ERROR: Input file \"%s\" doesn't exist\n", input_file_name); + fin = fopen(argv[i], "rb"); + if (fin == NULL) { + printf("ERROR: Input file \"%s\" doesn't exist\n", input_file_name); - exit(1); + exit(1); + } } // output file given? If not, use input filename with .pcf extension @@ -1255,24 +1280,31 @@ int init(int argc, char* argv[]) { read_header(); } - if (file_exists(output_file_name)) { - printf("Output file \"%s\" exists. Overwrite (y/n)? ", output_file_name); - char ch = get_char_with_echo(); - if ((ch != 'Y') && (ch != 'y')) { - printf("\n"); - exit(0); - } else { - #ifndef __unix - printf("\n\n"); - #else - printf("\n"); - #endif - } + if (output_file_given && strcmp(output_file_name, "stdout") == 0) { + // Write binary to stdout + SET_BINARY_MODE(STDOUT); + fout = stdout; } - fout = fopen(output_file_name,"wb"); - if (fout == NULL) { - printf("ERROR: Can't create output file \"%s\"\n", output_file_name); - exit(1); + else { + if (file_exists(output_file_name)) { + printf("Output file \"%s\" exists. Overwrite (y/n)? ", output_file_name); + char ch = get_char_with_echo(); + if ((ch != 'Y') && (ch != 'y')) { + printf("\n"); + exit(0); + } else { + #ifndef __unix + printf("\n\n"); + #else + printf("\n"); + #endif + } + } + fout = fopen(output_file_name,"wb"); + if (fout == NULL) { + printf("ERROR: Can't create output file \"%s\"\n", output_file_name); + exit(1); + } } printf("Input file: %s\n",input_file_name); @@ -2374,14 +2406,17 @@ void denit_compress() { } #ifndef PRECOMPDLL - long long fout_length = fileSize64(output_file_name); - if (recursion_depth == 0) { - if (!DEBUG_MODE) { - printf("%s", string(14,'\b').c_str()); - cout << "100.00% - New size: " << fout_length << " instead of " << fin_length << " " << endl; - } else { - cout << "New size: " << fout_length << " instead of " << fin_length << " " << endl; - } + if (strcmp(output_file_name, "stdout") != 0) { + long long fout_length = fileSize64(output_file_name); + if (recursion_depth == 0) { + if (!DEBUG_MODE) { + printf("%s", string(14, '\b').c_str()); + cout << "100.00% - New size: " << fout_length << " instead of " << fin_length << " " << endl; + } + else { + cout << "New size: " << fout_length << " instead of " << fin_length << " " << endl; + } + } } #else if (recursion_depth == 0) { @@ -4571,7 +4606,9 @@ void decompress_file() { fin_pos = tell_64(fin); -while (fin_pos < fin_length) { + auto otf_none_end_check = [](){ return compression_otf_method == OTF_NONE && (feof(fin) || ferror(fin)); }; + +while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress_otf_end) { if ((recursion_depth == 0) && (!DEBUG_MODE)) { float percent = (fin_pos / (float)fin_length) * 100; @@ -4579,6 +4616,7 @@ while (fin_pos < fin_length) { } unsigned char header1 = fin_fgetc(); + if (otf_none_end_check()) break; if (header1 == 0) { // uncompressed data long long uncompressed_data_length; uncompressed_data_length = fin_fget_vlint(); @@ -5390,6 +5428,7 @@ void write_header() { #ifdef COMFORT bool check_for_pcf_file() { + if (header_already_read) return true; seek_64(fin, 0); fread(in, 1, 3, fin); @@ -5429,12 +5468,13 @@ bool check_for_pcf_file() { strcpy(output_file_name, header_filename.c_str()); } + header_already_read = true; return true; } #endif void read_header() { - seek_64(fin, 0); + if (header_already_read) return; fread(in, 1, 3, fin); if ((in[0] == 'P') && (in[1] == 'C') && (in[2] == 'F')) { @@ -5465,6 +5505,8 @@ void read_header() { output_file_name = new char[strlen(header_filename.c_str()) + 1]; strcpy(output_file_name, header_filename.c_str()); } + + header_already_read = true; } void convert_header() { From 2112c153b1b718ad8948165f751474858cfe4549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Comerci?= Date: Wed, 17 Aug 2022 01:44:46 -0300 Subject: [PATCH 2/2] Changed printf usages to the new print_to_console that allows us to print to the console even when stdout is redirected --- precomp.cpp | 901 +++++++++++++++++++++++++++------------------------- 1 file changed, 470 insertions(+), 431 deletions(-) diff --git a/precomp.cpp b/precomp.cpp index 756f32f..1c380c0 100644 --- a/precomp.cpp +++ b/precomp.cpp @@ -67,6 +67,7 @@ #include #include #include +#include #ifdef MINGW #ifndef _GLIBCXX_HAS_GTHREADS #include "contrib\mingw_std_threads\mingw.thread.h" @@ -352,6 +353,37 @@ enum { D_BRUTE = 254, }; +#ifndef _WIN32 +int ttyfd = -1; +#endif + +#include +// This is to be able to print to the console during stdout mode, as prints would get mixed with actual data otherwise, and not be displayed anyways +void print_to_console(std::string format) { +#ifdef _WIN32 + for (char chr : format) { + putch(chr); + } +#else + if (ttyfd < 0) + ttyfd = open("/dev/tty", O_RDWR); + write(ttyfd, format.c_str(), format.length()); +#endif +} + +template< typename... Args > +void print_to_console(const char* format, Args... args) { + int length = std::snprintf(nullptr, 0, format, args...); + assert(length >= 0); + + char* buf = new char[length + 1]; + std::snprintf(buf, length + 1, format, args...); + + std::string str(buf); + delete[] buf; + print_to_console(str); +} + // Precomp DLL things #ifdef PRECOMPDLL @@ -575,14 +607,14 @@ bool parseSwitch(bool& val, const char* c, const char* ref) { val = false; return true; } - printf("ERROR: Only + or - for this switch (%s) allowed\n", c); + print_to_console("ERROR: Only + or - for this switch (%s) allowed\n", c); exit(1); return false; } int parseInt(const char*& c, const char* context, int too_big_error_code = 0) { if (*c < '0' || *c > '9') { - printf("ERROR: Number needed to set %s\n", context); + print_to_console("ERROR: Number needed to set %s\n", context); exit(1); } int val = *c++ - '0'; @@ -591,7 +623,7 @@ int parseInt(const char*& c, const char* context, int too_big_error_code = 0) { if (too_big_error_code != 0) { error(too_big_error_code); } - printf("ERROR: Number too big for %s\n", context); + print_to_console("ERROR: Number too big for %s\n", context); exit(1); } val = val * 10 + *c++ - '0'; @@ -601,7 +633,7 @@ int parseInt(const char*& c, const char* context, int too_big_error_code = 0) { int parseIntUntilEnd(const char* c, const char* context, int too_big_error_code = 0) { for (int i = 0; c[i]; ++i) { if (c[i] < '0' || c[i] > '9') { - printf("ERROR: Only numbers allowed for %s\n", context); + print_to_console("ERROR: Only numbers allowed for %s\n", context); exit(1); } } @@ -610,7 +642,7 @@ int parseIntUntilEnd(const char* c, const char* context, int too_big_error_code } int64_t parseInt64(const char*& c, const char* context, int too_big_error_code = 0) { if (*c < '0' || *c > '9') { - printf("ERROR: Number needed to set %s\n", context); + print_to_console("ERROR: Number needed to set %s\n", context); exit(1); } int64_t val = *c++ - '0'; @@ -619,7 +651,7 @@ int64_t parseInt64(const char*& c, const char* context, int too_big_error_code = if (too_big_error_code != 0) { error(too_big_error_code); } - printf("ERROR: Number too big for %s\n", context); + print_to_console("ERROR: Number too big for %s\n", context); exit(1); } val = val * 10 + *c++ - '0'; @@ -629,7 +661,7 @@ int64_t parseInt64(const char*& c, const char* context, int too_big_error_code = int64_t parseInt64UntilEnd(const char* c, const char* context, int too_big_error_code = 0) { for (int i = 0; c[i]; ++i) { if (c[i] < '0' || c[i] > '9') { - printf("ERROR: Only numbers allowed for %s\n", context); + print_to_console("ERROR: Only numbers allowed for %s\n", context); exit(1); } } @@ -643,15 +675,15 @@ int init(int argc, char* argv[]) { int i, j; bool appended_pcf = false; - printf("\n"); + print_to_console("\n"); if (V_MINOR2 == 0) { - printf("Precomp v%i.%i %s %s - %s version",V_MAJOR,V_MINOR,V_OS,V_BIT,V_STATE); + print_to_console("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); + print_to_console("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-2021 by Christian Schneider\n"); - printf(" preflate v0.3.5 support - Copyright 2018 by Dirk Steinke\n\n"); + print_to_console(" - %s\n",V_MSG); + print_to_console("Free for non-commercial use - Copyright 2006-2021 by Christian Schneider\n"); + print_to_console(" preflate v0.3.5 support - Copyright 2018 by Dirk Steinke\n\n"); // init compression and memory level count bool use_zlib_level[81]; @@ -745,7 +777,7 @@ int init(int argc, char* argv[]) { } else if (!parseSwitch(use_brunsli, argv[i] + 1, "brunsli") && !parseSwitch(use_brotli, argv[i] + 1, "brotli")) { - printf("ERROR: Unknown switch \"%s\"\n", argv[i]); + print_to_console("ERROR: Unknown switch \"%s\"\n", argv[i]); exit(1); } break; @@ -772,7 +804,7 @@ int init(int argc, char* argv[]) { int lclp = (otf_xz_extra_params.lc != 0 ? otf_xz_extra_params.lc - 1 : LZMA_LC_DEFAULT) + (otf_xz_extra_params.lp != 0 ? otf_xz_extra_params.lp - 1 : LZMA_LP_DEFAULT); if (lclp < LZMA_LCLP_MIN || lclp > LZMA_LCLP_MAX) { - printf("sum of LZMA lc (default %d) and lp (default %d) must be inside %d..%d\n", + print_to_console("sum of LZMA lc (default %d) and lp (default %d) must be inside %d..%d\n", LZMA_LC_DEFAULT, LZMA_LP_DEFAULT, LZMA_LCLP_MIN, LZMA_LCLP_MAX); exit(1); } @@ -781,12 +813,12 @@ int init(int argc, char* argv[]) { int lclp = (otf_xz_extra_params.lc != 0 ? otf_xz_extra_params.lc - 1 : LZMA_LC_DEFAULT) + (otf_xz_extra_params.lp != 0 ? otf_xz_extra_params.lp - 1 : LZMA_LP_DEFAULT); if (lclp < LZMA_LCLP_MIN || lclp > LZMA_LCLP_MAX) { - printf("sum of LZMA lc (default %d) and lp (default %d) must be inside %d..%d\n", + print_to_console("sum of LZMA lc (default %d) and lp (default %d) must be inside %d..%d\n", LZMA_LC_DEFAULT, LZMA_LP_DEFAULT, LZMA_LCLP_MIN, LZMA_LCLP_MAX); exit(1); } } else { - printf("ERROR: Unknown switch \"%s\"\n", argv[i]); + print_to_console("ERROR: Unknown switch \"%s\"\n", argv[i]); exit(1); } } else if (toupper(argv[i][2]) == 'P') { @@ -794,12 +826,12 @@ int init(int argc, char* argv[]) { otf_xz_extra_params.pb = 1 + parseIntUntilEnd(argv[i] + 4, "LZMA position bits"); int pb = otf_xz_extra_params.pb != 0 ? otf_xz_extra_params.pb - 1 : LZMA_PB_DEFAULT; if (pb < LZMA_PB_MIN || pb > LZMA_PB_MAX) { - printf("LZMA pb (default %d) must be inside %d..%d\n", + print_to_console("LZMA pb (default %d) must be inside %d..%d\n", LZMA_PB_DEFAULT, LZMA_PB_MIN, LZMA_PB_MAX); exit(1); } } else { - printf("ERROR: Unknown switch \"%s\"\n", argv[i]); + print_to_console("ERROR: Unknown switch \"%s\"\n", argv[i]); exit(1); } } else if (toupper(argv[i][2]) == 'F') { // LZMA filters @@ -811,13 +843,13 @@ int init(int argc, char* argv[]) { break; case '-': if (argv[i][4] != 0) { - printf("ERROR: \"-lf-\" must not be followed by anything\n"); + print_to_console("ERROR: \"-lf-\" must not be followed by anything\n"); exit(1); break; } break; default: - printf("ERROR: Only + or - after \"-lf\" allowed\n"); + print_to_console("ERROR: Only + or - after \"-lf\" allowed\n"); exit(1); break; } @@ -848,7 +880,7 @@ int init(int argc, char* argv[]) { argindex++; char nextchar = argv[i][argindex]; if ((nextchar < '0') || (nextchar > '9')) { - printf("ERROR: LZMA delta filter must be followed by a distance (%d..%d)\n", + print_to_console("ERROR: LZMA delta filter must be followed by a distance (%d..%d)\n", LZMA_DELTA_DIST_MIN, LZMA_DELTA_DIST_MAX); exit(1); } @@ -860,7 +892,7 @@ int init(int argc, char* argv[]) { } if (otf_xz_extra_params.filter_delta_distance < LZMA_DELTA_DIST_MIN || otf_xz_extra_params.filter_delta_distance > LZMA_DELTA_DIST_MAX) { - printf("ERROR: LZMA delta filter distance must be in range %d..%d\n", + print_to_console("ERROR: LZMA delta filter distance must be in range %d..%d\n", LZMA_DELTA_DIST_MIN, LZMA_DELTA_DIST_MAX); exit(1); } @@ -868,13 +900,13 @@ int init(int argc, char* argv[]) { } break; default: - printf("ERROR: Unknown LZMA filter type \"%c\"\n", argv[i][argindex]); + print_to_console("ERROR: Unknown LZMA filter type \"%c\"\n", argv[i][argindex]); exit(1); break; } otf_xz_filter_used_count++; if (otf_xz_filter_used_count > LZMA_FILTERS_MAX - 1) { - printf("ERROR: Only up to %d LZMA filters can be used at the same time\n", + print_to_console("ERROR: Only up to %d LZMA filters can be used at the same time\n", LZMA_FILTERS_MAX - 1); exit(1); } @@ -883,7 +915,7 @@ int init(int argc, char* argv[]) { lzma_filters_set = true; break; } else { - printf("ERROR: Unknown switch \"%s\"\n", argv[i]); + print_to_console("ERROR: Unknown switch \"%s\"\n", argv[i]); exit(1); } break; @@ -897,12 +929,12 @@ int init(int argc, char* argv[]) { if (parsePrefixText(argv[i] + 1, "pfmeta")) { int mbsize = parseIntUntilEnd(argv[i] + 7, "preflate meta block size"); if (mbsize >= INT_MAX / 1024) { - printf("preflate meta block size set too big\n"); + print_to_console("preflate meta block size set too big\n"); exit(1); } preflate_meta_block_size = mbsize * 1024; } else { - printf("ERROR: Unknown switch \"%s\"\n", argv[i]); + print_to_console("ERROR: Unknown switch \"%s\"\n", argv[i]); exit(1); } } @@ -939,7 +971,7 @@ int init(int argc, char* argv[]) { set_to = false; break; default: - printf("ERROR: Only + or - for type switch allowed\n"); + print_to_console("ERROR: Only + or - for type switch allowed\n"); exit(1); break; } @@ -976,7 +1008,7 @@ int init(int argc, char* argv[]) { use_bzip2 = set_to; break; default: - printf("ERROR: Invalid compression type %c\n", argv[i][j]); + print_to_console("ERROR: Invalid compression type %c\n", argv[i][j]); exit(1); break; } @@ -996,12 +1028,12 @@ int init(int argc, char* argv[]) { compression_otf_method = OTF_XZ_MT; break; default: - printf("ERROR: Invalid compression method %c\n", argv[i][2]); + print_to_console("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]); + print_to_console("ERROR: Unknown switch \"%s\"\n", argv[i]); exit(1); } show_lzma_progress = (compression_otf_method == OTF_XZ_MT); @@ -1020,12 +1052,12 @@ int init(int argc, char* argv[]) { conversion_to_method = OTF_XZ_MT; break; default: - printf("ERROR: Invalid conversion method %c\n", argv[i][2]); + print_to_console("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]); + print_to_console("ERROR: Unknown switch \"%s\"\n", argv[i]); exit(1); } operation = P_CONVERT; @@ -1035,7 +1067,7 @@ int init(int argc, char* argv[]) { { DEBUG_MODE = true; if (argv[i][2] != 0) { // Extra Parameters? - printf("ERROR: Unknown switch \"%s\"\n", argv[i]); + print_to_console("ERROR: Unknown switch \"%s\"\n", argv[i]); exit(1); } break; @@ -1044,7 +1076,7 @@ int init(int argc, char* argv[]) { { operation = P_DECOMPRESS; if (argv[i][2] != 0) { // Extra Parameters? - printf("ERROR: Unknown switch \"%s\"\n", argv[i]); + print_to_console("ERROR: Unknown switch \"%s\"\n", argv[i]); exit(1); } break; @@ -1061,12 +1093,12 @@ int init(int argc, char* argv[]) { for (j = 0; j < ((int)strlen(argv[i])-3); j += 3) { if ((j+5) < (int)strlen(argv[i])) { if (argv[i][j+5] != ',') { - printf("ERROR: zLib levels have to be separated with commas\n"); + print_to_console("ERROR: zLib levels have to be separated with commas\n"); exit(1); } } if ((j+4) >= (int)strlen(argv[i])) { - printf("ERROR: Last zLib level is incomplete\n"); + print_to_console("ERROR: Last zLib level is incomplete\n"); exit(1); } int comp_level_to_use = (char(argv[i][j+3]) - '1'); @@ -1075,13 +1107,13 @@ int init(int argc, char* argv[]) { && ((mem_level_to_use >= 0) && (mem_level_to_use <= 8))) { use_zlib_level[comp_level_to_use + mem_level_to_use * 9] = true; } else { - printf("ERROR: Invalid zlib level %c%c\n", argv[i][j+3], argv[i][j+4]); + print_to_console("ERROR: Invalid zlib level %c%c\n", argv[i][j+3], argv[i][j+4]); exit(1); } } break; } else { - printf("ERROR: Unknown switch \"%s\"\n", argv[i]); + print_to_console("ERROR: Unknown switch \"%s\"\n", argv[i]); exit(1); } } @@ -1115,7 +1147,7 @@ int init(int argc, char* argv[]) { case 'M': { if (!parseSwitch(use_mjpeg, argv[i] + 1, "mjpeg")) { - printf("ERROR: Unknown switch \"%s\"\n", argv[i]); + print_to_console("ERROR: Unknown switch \"%s\"\n", argv[i]); exit(1); } break; @@ -1125,7 +1157,7 @@ int init(int argc, char* argv[]) { { fast_mode = true; if (argv[i][2] != 0) { // Extra Parameters? - printf("ERROR: Unknown switch \"%s\"\n", argv[i]); + print_to_console("ERROR: Unknown switch \"%s\"\n", argv[i]); exit(1); } break; @@ -1134,14 +1166,14 @@ int init(int argc, char* argv[]) { { preserve_extension = true; if (argv[i][2] != 0) { // Extra Parameters? - printf("ERROR: Unknown switch \"%s\"\n", argv[i]); + print_to_console("ERROR: Unknown switch \"%s\"\n", argv[i]); exit(1); } break; } default: { - printf("ERROR: Unknown switch \"%s\"\n", argv[i]); + print_to_console("ERROR: Unknown switch \"%s\"\n", argv[i]); exit(1); } } @@ -1167,7 +1199,7 @@ int init(int argc, char* argv[]) { fin = fopen(argv[i], "rb"); if (fin == NULL) { - printf("ERROR: Input file \"%s\" doesn't exist\n", input_file_name); + print_to_console("ERROR: Input file \"%s\" doesn't exist\n", input_file_name); exit(1); } @@ -1196,7 +1228,7 @@ int init(int argc, char* argv[]) { } output_file_given = true; } else if ((!output_file_given) && (operation == P_CONVERT)) { - printf("ERROR: Please specify an output file for conversion\n"); + print_to_console("ERROR: Please specify an output file for conversion\n"); exit(1); } @@ -1206,68 +1238,68 @@ int init(int argc, char* argv[]) { } if (!valid_syntax) { - printf("Usage: precomp [-switches] input_file\n\n"); + print_to_console("Usage: precomp [-switches] input_file\n\n"); if (long_help) { - printf("Switches (and their ):\n"); + print_to_console("Switches (and their ):\n"); } else { - printf("Common switches (and their ):\n"); - } - printf(" r \"Recompress\" PCF file (restore original file)\n"); - printf(" o[filename] Write output to [filename] <[input_file].pcf or file in header>\n"); - printf(" e preserve original extension of input name for output name \n"); - printf(" c[lbn] Compression method to use, l = lzma2, b = bZip2, n = none \n"); - printf(" lm[amount] Set maximal LZMA memory in MiB <%i>\n", lzma_max_memory_default()); - printf(" lt[count] Set LZMA thread count \n", auto_detected_thread_count()); + print_to_console("Common switches (and their ):\n"); + } + print_to_console(" r \"Recompress\" PCF file (restore original file)\n"); + print_to_console(" o[filename] Write output to [filename] <[input_file].pcf or file in header>\n"); + print_to_console(" e preserve original extension of input name for output name \n"); + print_to_console(" c[lbn] Compression method to use, l = lzma2, b = bZip2, n = none \n"); + print_to_console(" lm[amount] Set maximal LZMA memory in MiB <%i>\n", lzma_max_memory_default()); + print_to_console(" lt[count] Set LZMA thread count \n", auto_detected_thread_count()); if (long_help) { - printf(" lf[+-][xpiatsd] Set LZMA filters (up to %d of them can be combined) \n", + print_to_console(" lf[+-][xpiatsd] Set LZMA filters (up to %d of them can be combined) \n", LZMA_FILTERS_MAX - 1); - printf(" lf+[xpiatsd] = enable these filters, lf- = disable all\n"); - printf(" X = x86, P = PowerPC, I = IA-64, A = ARM, T = ARM-Thumb\n"); - printf(" S = SPARC, D = delta (must be followed by distance %d..%d)\n", + print_to_console(" lf+[xpiatsd] = enable these filters, lf- = disable all\n"); + print_to_console(" X = x86, P = PowerPC, I = IA-64, A = ARM, T = ARM-Thumb\n"); + print_to_console(" S = SPARC, D = delta (must be followed by distance %d..%d)\n", LZMA_DELTA_DIST_MIN, LZMA_DELTA_DIST_MAX); - printf(" llc[bits] Set LZMA literal context bits <%d>\n", LZMA_LC_DEFAULT); - printf(" llp[bits] Set LZMA literal position bits <%d>\n", LZMA_LP_DEFAULT); - printf(" The sum of lc and lp must be inside %d..%d\n", LZMA_LCLP_MIN, LZMA_LCLP_MAX); - printf(" lpb[bits] Set LZMA position bits, must be inside %d..%d <%d>\n", + print_to_console(" llc[bits] Set LZMA literal context bits <%d>\n", LZMA_LC_DEFAULT); + print_to_console(" llp[bits] Set LZMA literal position bits <%d>\n", LZMA_LP_DEFAULT); + print_to_console(" The sum of lc and lp must be inside %d..%d\n", LZMA_LCLP_MIN, LZMA_LCLP_MAX); + print_to_console(" lpb[bits] Set LZMA position bits, must be inside %d..%d <%d>\n", LZMA_PB_MIN, LZMA_PB_MAX, LZMA_PB_DEFAULT); } else { - printf(" lf[+-][xpiatsd] Set LZMA filters (up to 3, see long help for details) \n"); + print_to_console(" lf[+-][xpiatsd] Set LZMA filters (up to 3, see long help for details) \n"); } - printf(" n[lbn] Convert a PCF file to this compression (same as above) \n"); - printf(" v Verbose (debug) mode \n"); - printf(" d[depth] Set maximal recursion depth <10>\n"); - //printf(" zl[1..9][1..9] zLib levels to try for compression (comma separated) \n"); + print_to_console(" n[lbn] Convert a PCF file to this compression (same as above) \n"); + print_to_console(" v Verbose (debug) mode \n"); + print_to_console(" d[depth] Set maximal recursion depth <10>\n"); + //print_to_console(" zl[1..9][1..9] zLib levels to try for compression (comma separated) \n"); if (long_help) { - printf(" pfmeta[amount] Split deflate streams into meta blocks of this size in KiB <2048>\n"); - printf(" pfverify Force preflate to verify its generated reconstruction data\n"); + print_to_console(" pfmeta[amount] Split deflate streams into meta blocks of this size in KiB <2048>\n"); + print_to_console(" pfverify Force preflate to verify its generated reconstruction data\n"); } - printf(" intense Detect raw zLib headers, too. Slower and more sensitive \n"); + print_to_console(" intense Detect raw zLib headers, too. Slower and more sensitive \n"); if (long_help) { - printf(" brute Brute force zLib detection. VERY Slow and most sensitive \n"); + print_to_console(" brute Brute force zLib detection. VERY Slow and most sensitive \n"); } - printf(" t[+-][pzgnfjsmb3] Compression type switch \n"); - printf(" t+ = enable these types only, t- = enable all types except these\n"); - printf(" P = PDF, Z = ZIP, G = GZip, N = PNG, F = GIF, J = JPG\n"); - printf(" S = SWF, M = MIME Base64, B = bZip2, 3 = MP3\n"); + print_to_console(" t[+-][pzgnfjsmb3] Compression type switch \n"); + print_to_console(" t+ = enable these types only, t- = enable all types except these\n"); + print_to_console(" P = PDF, Z = ZIP, G = GZip, N = PNG, F = GIF, J = JPG\n"); + print_to_console(" S = SWF, M = MIME Base64, B = bZip2, 3 = MP3\n"); if (!long_help) { - printf(" longhelp Show long help\n"); + print_to_console(" longhelp Show long help\n"); } else { - printf(" f Fast mode, use first found compression lvl for all streams \n"); - printf(" i[pos] Ignore stream at input file position [pos] \n"); - printf(" s[size] Set minimal identical byte size to [size] <4 (64 intense mode)>\n"); - printf(" pdfbmp[+-] Wrap a BMP header around PDF images \n"); - printf(" progonly[+-] Recompress progressive JPGs only (useful for PAQ) \n"); - printf(" mjpeg[+-] Insert huffman table for MJPEG recompression \n"); - printf(" brunsli[+-] Prefer brunsli to packJPG for JPG streams \n"); - printf(" brotli[+-] Use brotli to compress metadata in JPG streams \n"); - printf(" packjpg[+-] Use packJPG for JPG streams and fallback if brunsli fails \n"); - printf("\n"); - printf(" You can use an optional number following -intense and -brute to set a\n"); - printf(" limit for how deep in recursion they should be used. E.g. -intense0 means\n"); - printf(" that intense mode will be used but not in recursion, -intense2 that only\n"); - printf(" streams up to recursion depth 2 will be treated intense (3 or higher in\n"); - printf(" this case won't). Using a sensible setting here can save you some time.\n"); + print_to_console(" f Fast mode, use first found compression lvl for all streams \n"); + print_to_console(" i[pos] Ignore stream at input file position [pos] \n"); + print_to_console(" s[size] Set minimal identical byte size to [size] <4 (64 intense mode)>\n"); + print_to_console(" pdfbmp[+-] Wrap a BMP header around PDF images \n"); + print_to_console(" progonly[+-] Recompress progressive JPGs only (useful for PAQ) \n"); + print_to_console(" mjpeg[+-] Insert huffman table for MJPEG recompression \n"); + print_to_console(" brunsli[+-] Prefer brunsli to packJPG for JPG streams \n"); + print_to_console(" brotli[+-] Use brotli to compress metadata in JPG streams \n"); + print_to_console(" packjpg[+-] Use packJPG for JPG streams and fallback if brunsli fails \n"); + print_to_console("\n"); + print_to_console(" You can use an optional number following -intense and -brute to set a\n"); + print_to_console(" limit for how deep in recursion they should be used. E.g. -intense0 means\n"); + print_to_console(" that intense mode will be used but not in recursion, -intense2 that only\n"); + print_to_console(" streams up to recursion depth 2 will be treated intense (3 or higher in\n"); + print_to_console(" this case won't). Using a sensible setting here can save you some time.\n"); } exit(1); @@ -1287,40 +1319,40 @@ int init(int argc, char* argv[]) { } else { if (file_exists(output_file_name)) { - printf("Output file \"%s\" exists. Overwrite (y/n)? ", output_file_name); + print_to_console("Output file \"%s\" exists. Overwrite (y/n)? ", output_file_name); char ch = get_char_with_echo(); if ((ch != 'Y') && (ch != 'y')) { - printf("\n"); + print_to_console("\n"); exit(0); } else { #ifndef __unix - printf("\n\n"); + print_to_console("\n\n"); #else - printf("\n"); + print_to_console("\n"); #endif } } fout = fopen(output_file_name,"wb"); if (fout == NULL) { - printf("ERROR: Can't create output file \"%s\"\n", output_file_name); + print_to_console("ERROR: Can't create output file \"%s\"\n", output_file_name); exit(1); } } - printf("Input file: %s\n",input_file_name); - printf("Output file: %s\n\n",output_file_name); + print_to_console("Input file: %s\n",input_file_name); + print_to_console("Output file: %s\n\n",output_file_name); if (DEBUG_MODE) { if (min_ident_size_set) { - printf("\n"); - printf("Minimal ident size set to %i bytes\n", min_ident_size); + print_to_console("\n"); + print_to_console("Minimal ident size set to %i bytes\n", min_ident_size); } if (ignore_list_len > 0) { - printf("\n"); - printf("Ignore position list:\n"); + print_to_console("\n"); + print_to_console("Ignore position list:\n"); for (j = 0; j < ignore_list_len; j++) { cout << ignore_list[j] << endl; } - printf("\n"); + print_to_console("\n"); } } @@ -1358,14 +1390,14 @@ int init_comfort(int argc, char* argv[]) { bool lzma_filters_set = false; bool preserve_extension = false; - printf("\n"); + print_to_console("\n"); if (V_MINOR2 == 0) { - printf("Precomp Comfort v%i.%i %s %s - %s version",V_MAJOR,V_MINOR,V_OS,V_BIT,V_STATE); + print_to_console("Precomp Comfort v%i.%i %s %s - %s version",V_MAJOR,V_MINOR,V_OS,V_BIT,V_STATE); } else { - printf("Precomp Comfort v%i.%i.%i %s %s - %s version",V_MAJOR,V_MINOR,V_MINOR2,V_OS,V_BIT,V_STATE); + print_to_console("Precomp Comfort 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-2021 by Christian Schneider\n\n"); + print_to_console(" - %s\n",V_MSG); + print_to_console("Free for non-commercial use - Copyright 2006-2021 by Christian Schneider\n\n"); // init compression and memory level count bool use_zlib_level[81]; @@ -1390,9 +1422,9 @@ int init_comfort(int argc, char* argv[]) { // parse parameters (should be input file only) if (argc == 1) { - printf("Usage:\n"); - printf("Drag and drop a file on the executable to precompress/restore it.\n"); - printf("Edit INI file for parameters.\n"); + print_to_console("Usage:\n"); + print_to_console("Drag and drop a file on the executable to precompress/restore it.\n"); + print_to_console("Edit INI file for parameters.\n"); wait_for_key(); exit(1); } @@ -1405,7 +1437,7 @@ int init_comfort(int argc, char* argv[]) { fin = fopen(input_file_name,"rb"); if (fin == NULL) { - printf("ERROR: Input file \"%s\" doesn't exist\n", input_file_name); + print_to_console("ERROR: Input file \"%s\" doesn't exist\n", input_file_name); wait_for_key(); exit(1); } @@ -1428,12 +1460,12 @@ int init_comfort(int argc, char* argv[]) { // truncate to get directory of executable only char* lastslash = strrchr(precomf_ini, PATH_DELIM) + 1; strcpy(lastslash, "precomf.ini"); - printf("INI file: %s\n", precomf_ini); + print_to_console("INI file: %s\n", precomf_ini); if (!file_exists(precomf_ini)) { - printf("INI file not found. Create it (y/n)?"); + print_to_console("INI file not found. Create it (y/n)?"); char ch = getche(); - printf("\n"); + print_to_console("\n"); if ((ch != 'Y') && (ch != 'y')) { wait_for_key(); exit(1); @@ -1591,25 +1623,25 @@ int init_comfort(int argc, char* argv[]) { min_ident_size = ident_size; min_ident_size_set = true; - printf("INI: Set minimal identical byte size to %i\n", min_ident_size); + print_to_console("INI: Set minimal identical byte size to %i\n", min_ident_size); valid_param = true; } if (strcmp(param, "verbose") == 0) { if (strcmp(value, "off") == 0) { - printf("INI: Disabled verbose mode\n"); + print_to_console("INI: Disabled verbose mode\n"); valid_param = true; } if (strcmp(value, "on") == 0) { - printf("INI: Enabled verbose mode\n"); + print_to_console("INI: Enabled verbose mode\n"); DEBUG_MODE = true; valid_param = true; } if (!valid_param) { - printf("ERROR: Invalid verbose value: %s\n", value); + print_to_console("ERROR: Invalid verbose value: %s\n", value); wait_for_key(); exit(1); } @@ -1617,19 +1649,19 @@ int init_comfort(int argc, char* argv[]) { if (strcmp(param, "compression_method") == 0) { if (strcmp(value, "0") == 0) { - printf("INI: Using no compression method\n"); + print_to_console("INI: Using no compression method\n"); compression_otf_method = OTF_NONE; valid_param = true; } if (strcmp(value, "1") == 0) { - printf("INI: Using bZip2 compression method\n"); + print_to_console("INI: Using bZip2 compression method\n"); compression_otf_method = OTF_BZIP2; valid_param = true; } if (strcmp(value, "2") == 0) { - printf("INI: Using lzma2 multithreaded compression method\n"); + print_to_console("INI: Using lzma2 multithreaded compression method\n"); compression_otf_method = OTF_XZ_MT; valid_param = true; } @@ -1637,7 +1669,7 @@ int init_comfort(int argc, char* argv[]) { show_lzma_progress = (compression_otf_method == OTF_XZ_MT); if (!valid_param) { - printf("ERROR: Invalid compression method value: %s\n", value); + print_to_console("ERROR: Invalid compression method value: %s\n", value); wait_for_key(); exit(1); } @@ -1658,7 +1690,7 @@ int init_comfort(int argc, char* argv[]) { lzma_max_memory_set = true; if (compression_otf_max_memory > 0) { - printf("INI: Set LZMA maximal memory to %i MiB\n", (int)compression_otf_max_memory); + print_to_console("INI: Set LZMA maximal memory to %i MiB\n", (int)compression_otf_max_memory); } valid_param = true; @@ -1679,7 +1711,7 @@ int init_comfort(int argc, char* argv[]) { lzma_thread_count_set = true; if (compression_otf_thread_count > 0) { - printf("INI: Set LZMA thread count to %i\n", compression_otf_thread_count); + print_to_console("INI: Set LZMA thread count to %i\n", compression_otf_thread_count); } valid_param = true; @@ -1715,7 +1747,7 @@ int init_comfort(int argc, char* argv[]) { j++; char nextchar = value[j]; if ((nextchar < '0') || (nextchar > '9')) { - printf("ERROR: LZMA delta filter must be followed by a distance (%d..%d)\n", + print_to_console("ERROR: LZMA delta filter must be followed by a distance (%d..%d)\n", LZMA_DELTA_DIST_MIN, LZMA_DELTA_DIST_MAX); wait_for_key(); exit(1); @@ -1728,7 +1760,7 @@ int init_comfort(int argc, char* argv[]) { } if (otf_xz_extra_params.filter_delta_distance < LZMA_DELTA_DIST_MIN || otf_xz_extra_params.filter_delta_distance > LZMA_DELTA_DIST_MAX) { - printf("ERROR: LZMA delta filter distance must be in range %d..%d\n", + print_to_console("ERROR: LZMA delta filter distance must be in range %d..%d\n", LZMA_DELTA_DIST_MIN, LZMA_DELTA_DIST_MAX); wait_for_key(); exit(1); @@ -1737,14 +1769,14 @@ int init_comfort(int argc, char* argv[]) { break; } default: - printf("ERROR: Unknown LZMA filter type \"%c\"\n", value[j]); + print_to_console("ERROR: Unknown LZMA filter type \"%c\"\n", value[j]); wait_for_key(); 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"); + print_to_console("ERROR: Only up to 3 LZMA filters can be used at the same time\n"); wait_for_key(); exit(1); } @@ -1756,18 +1788,18 @@ int init_comfort(int argc, char* argv[]) { if (strcmp(param, "fast_mode") == 0) { if (strcmp(value, "off") == 0) { - printf("INI: Disabled fast mode\n"); + print_to_console("INI: Disabled fast mode\n"); valid_param = true; } if (strcmp(value, "on") == 0) { - printf("INI: Enabled fast mode\n"); + print_to_console("INI: Enabled fast mode\n"); fast_mode = true; valid_param = true; } if (!valid_param) { - printf("ERROR: Invalid fast mode value: %s\n", value); + print_to_console("ERROR: Invalid fast mode value: %s\n", value); wait_for_key(); exit(1); } @@ -1775,13 +1807,13 @@ int init_comfort(int argc, char* argv[]) { // future note: params should be in lowercase for comparisons here only if (strcmp(param,"preserve_extension") == 0) { if (strcmp(value,"off") == 0) { - printf("INI: Not preserve extension\n"); + print_to_console("INI: Not preserve extension\n"); preserve_extension = false; } else if (strcmp(value,"on") == 0) { - printf("INI: Preserve extension\n"); + print_to_console("INI: Preserve extension\n"); preserve_extension = true; } else { - printf("ERROR: Invalid Preserve extension mode: %s\n",value); + print_to_console("ERROR: Invalid Preserve extension mode: %s\n",value); wait_for_key(); exit(1); } @@ -1790,18 +1822,18 @@ int init_comfort(int argc, char* argv[]) { if (strcmp(param, "intense_mode") == 0) { if (strcmp(value, "off") == 0) { - printf("INI: Disabled intense mode\n"); + print_to_console("INI: Disabled intense mode\n"); valid_param = true; } if (strcmp(value, "on") == 0) { - printf("INI: Enabled intense mode\n"); + print_to_console("INI: Enabled intense mode\n"); intense_mode = true; valid_param = true; } if (!valid_param) { - printf("ERROR: Invalid intense mode value: %s\n", value); + print_to_console("ERROR: Invalid intense mode value: %s\n", value); wait_for_key(); exit(1); } @@ -1809,18 +1841,18 @@ int init_comfort(int argc, char* argv[]) { if (strcmp(param, "brute_mode") == 0) { if (strcmp(value, "off") == 0) { - printf("INI: Disabled brute mode\n"); + print_to_console("INI: Disabled brute mode\n"); valid_param = true; } if (strcmp(value, "on") == 0) { - printf("INI: Enabled brute mode\n"); + print_to_console("INI: Enabled brute mode\n"); brute_mode = true; valid_param = true; } if (!valid_param) { - printf("ERROR: Invalid brute mode value: %s\n", value); + print_to_console("ERROR: Invalid brute mode value: %s\n", value); wait_for_key(); exit(1); } @@ -1828,19 +1860,19 @@ int init_comfort(int argc, char* argv[]) { if (strcmp(param, "pdf_bmp_mode") == 0) { if (strcmp(value, "off") == 0) { - printf("INI: Disabled PDF BMP mode\n"); + print_to_console("INI: Disabled PDF BMP mode\n"); pdf_bmp_mode = false; valid_param = true; } if (strcmp(value, "on") == 0) { - printf("INI: Enabled PDF BMP mode\n"); + print_to_console("INI: Enabled PDF BMP mode\n"); pdf_bmp_mode = true; valid_param = true; } if (!valid_param) { - printf("ERROR: Invalid PDF BMP mode value: %s\n", value); + print_to_console("ERROR: Invalid PDF BMP mode value: %s\n", value); wait_for_key(); exit(1); } @@ -1848,19 +1880,19 @@ int init_comfort(int argc, char* argv[]) { if (strcmp(param, "jpg_progressive_only") == 0) { if (strcmp(value, "off") == 0) { - printf("INI: Disabled progressive only JPG mode\n"); + print_to_console("INI: Disabled progressive only JPG mode\n"); prog_only = false; valid_param = true; } if (strcmp(value, "on") == 0) { - printf("INI: Enabled progressive only JPG mode\n"); + print_to_console("INI: Enabled progressive only JPG mode\n"); prog_only = true; valid_param = true; } if (!valid_param) { - printf("ERROR: Invalid progressive only JPG mode value: %s\n", value); + print_to_console("ERROR: Invalid progressive only JPG mode value: %s\n", value); wait_for_key(); exit(1); } @@ -1868,19 +1900,19 @@ int init_comfort(int argc, char* argv[]) { if (strcmp(param, "mjpeg_recompression") == 0) { if (strcmp(value, "off") == 0) { - printf("INI: Disabled MJPEG recompression\n"); + print_to_console("INI: Disabled MJPEG recompression\n"); use_mjpeg = false; valid_param = true; } if (strcmp(value, "on") == 0) { - printf("INI: Enabled MJPEG recompression\n"); + print_to_console("INI: Enabled MJPEG recompression\n"); use_mjpeg = true; valid_param = true; } if (!valid_param) { - printf("ERROR: Invalid MJPEG recompression value: %s\n", value); + print_to_console("ERROR: Invalid MJPEG recompression value: %s\n", value); wait_for_key(); exit(1); } @@ -1888,19 +1920,19 @@ int init_comfort(int argc, char* argv[]) { if (strcmp(param, "jpg_brunsli") == 0) { if (strcmp(value, "off") == 0) { - printf("INI: Disabled brunsli for JPG commpression\n"); + print_to_console("INI: Disabled brunsli for JPG commpression\n"); use_brunsli = false; valid_param = true; } if (strcmp(value, "on") == 0) { - printf("INI: Enabled brunsli for JPG compression\n"); + print_to_console("INI: Enabled brunsli for JPG compression\n"); use_brunsli = true; valid_param = true; } if (!valid_param) { - printf("ERROR: Invalid brunsli compression value: %s\n", value); + print_to_console("ERROR: Invalid brunsli compression value: %s\n", value); wait_for_key(); exit(1); } @@ -1908,19 +1940,19 @@ int init_comfort(int argc, char* argv[]) { if (strcmp(param, "jpg_brotli") == 0) { if (strcmp(value, "off") == 0) { - printf("INI: Disabled brotli for JPG metadata compression\n"); + print_to_console("INI: Disabled brotli for JPG metadata compression\n"); use_brotli = false; valid_param = true; } if (strcmp(value, "on") == 0) { - printf("INI: Enabled brotli for JPG metadata compression\n"); + print_to_console("INI: Enabled brotli for JPG metadata compression\n"); use_brotli = true; valid_param = true; } if (!valid_param) { - printf("ERROR: Invalid brotli for metadata compression value: %s\n", value); + print_to_console("ERROR: Invalid brotli for metadata compression value: %s\n", value); wait_for_key(); exit(1); } @@ -1928,19 +1960,19 @@ int init_comfort(int argc, char* argv[]) { if (strcmp(param, "jpg_packjpg") == 0) { if (strcmp(value, "off") == 0) { - printf("INI: Disabled packJPG for JPG compression\n"); + print_to_console("INI: Disabled packJPG for JPG compression\n"); use_brotli = false; valid_param = true; } if (strcmp(value, "on") == 0) { - printf("INI: Enabled packJPG for JPG compression\n"); + print_to_console("INI: Enabled packJPG for JPG compression\n"); use_brotli = true; valid_param = true; } if (!valid_param) { - printf("ERROR: Invalid packJPG for JPG compression value: %s\n", value); + print_to_console("ERROR: Invalid packJPG for JPG compression value: %s\n", value); wait_for_key(); exit(1); } @@ -1948,7 +1980,7 @@ int init_comfort(int argc, char* argv[]) { if (strcmp(param, "compression_types_enable") == 0) { if (compression_type_line_used) { - printf("ERROR: Both Compression_types_enable and Compression_types_disable used.\n"); + print_to_console("ERROR: Both Compression_types_enable and Compression_types_disable used.\n"); wait_for_key(); exit(1); } @@ -1998,70 +2030,70 @@ int init_comfort(int argc, char* argv[]) { use_bzip2 = true; break; default: - printf("ERROR: Invalid compression type %c\n", value[j]); + print_to_console("ERROR: Invalid compression type %c\n", value[j]); exit(1); break; } } if (use_pdf) { - printf("INI: PDF compression enabled\n"); + print_to_console("INI: PDF compression enabled\n"); } else { - printf("INI: PDF compression disabled\n"); + print_to_console("INI: PDF compression disabled\n"); } if (use_zip) { - printf("INI: ZIP compression enabled\n"); + print_to_console("INI: ZIP compression enabled\n"); } else { - printf("INI: ZIP compression disabled\n"); + print_to_console("INI: ZIP compression disabled\n"); } if (use_gzip) { - printf("INI: GZip compression enabled\n"); + print_to_console("INI: GZip compression enabled\n"); } else { - printf("INI: GZip compression disabled\n"); + print_to_console("INI: GZip compression disabled\n"); } if (use_png) { - printf("INI: PNG compression enabled\n"); + print_to_console("INI: PNG compression enabled\n"); } else { - printf("INI: PNG compression disabled\n"); + print_to_console("INI: PNG compression disabled\n"); } if (use_gif) { - printf("INI: GIF compression enabled\n"); + print_to_console("INI: GIF compression enabled\n"); } else { - printf("INI: GIF compression disabled\n"); + print_to_console("INI: GIF compression disabled\n"); } if (use_jpg) { - printf("INI: JPG compression enabled\n"); + print_to_console("INI: JPG compression enabled\n"); } else { - printf("INI: JPG compression disabled\n"); + print_to_console("INI: JPG compression disabled\n"); } if (use_mp3) { - printf("INI: MP3 compression enabled\n"); + print_to_console("INI: MP3 compression enabled\n"); } else { - printf("INI: MP3 compression disabled\n"); + print_to_console("INI: MP3 compression disabled\n"); } if (use_swf) { - printf("INI: SWF compression enabled\n"); + print_to_console("INI: SWF compression enabled\n"); } else { - printf("INI: SWF compression disabled\n"); + print_to_console("INI: SWF compression disabled\n"); } if (use_base64) { - printf("INI: Base64 compression enabled\n"); + print_to_console("INI: Base64 compression enabled\n"); } else { - printf("INI: Base64 compression disabled\n"); + print_to_console("INI: Base64 compression disabled\n"); } if (use_bzip2) { - printf("INI: bZip2 compression enabled\n"); + print_to_console("INI: bZip2 compression enabled\n"); } else { - printf("INI: bZip2 compression disabled\n"); + print_to_console("INI: bZip2 compression disabled\n"); } valid_param = true; @@ -2069,7 +2101,7 @@ int init_comfort(int argc, char* argv[]) { if (strcmp(param, "compression_types_disable") == 0) { if (compression_type_line_used) { - printf("ERROR: Both Compression_types_enable and Compression_types_disable used.\n"); + print_to_console("ERROR: Both Compression_types_enable and Compression_types_disable used.\n"); wait_for_key(); exit(1); } @@ -2119,70 +2151,70 @@ int init_comfort(int argc, char* argv[]) { use_bzip2 = false; break; default: - printf("ERROR: Invalid compression type %c\n", value[j]); + print_to_console("ERROR: Invalid compression type %c\n", value[j]); exit(1); break; } } if (use_pdf) { - printf("INI: PDF compression enabled\n"); + print_to_console("INI: PDF compression enabled\n"); } else { - printf("INI: PDF compression disabled\n"); + print_to_console("INI: PDF compression disabled\n"); } if (use_zip) { - printf("INI: ZIP compression enabled\n"); + print_to_console("INI: ZIP compression enabled\n"); } else { - printf("INI: ZIP compression disabled\n"); + print_to_console("INI: ZIP compression disabled\n"); } if (use_gzip) { - printf("INI: GZip compression enabled\n"); + print_to_console("INI: GZip compression enabled\n"); } else { - printf("INI: GZip compression disabled\n"); + print_to_console("INI: GZip compression disabled\n"); } if (use_png) { - printf("INI: PNG compression enabled\n"); + print_to_console("INI: PNG compression enabled\n"); } else { - printf("INI: PNG compression disabled\n"); + print_to_console("INI: PNG compression disabled\n"); } if (use_gif) { - printf("INI: GIF compression enabled\n"); + print_to_console("INI: GIF compression enabled\n"); } else { - printf("INI: GIF compression disabled\n"); + print_to_console("INI: GIF compression disabled\n"); } if (use_jpg) { - printf("INI: JPG compression enabled\n"); + print_to_console("INI: JPG compression enabled\n"); } else { - printf("INI: JPG compression disabled\n"); + print_to_console("INI: JPG compression disabled\n"); } if (use_mp3) { - printf("INI: MP3 compression enabled\n"); + print_to_console("INI: MP3 compression enabled\n"); } else { - printf("INI: MP3 compression disabled\n"); + print_to_console("INI: MP3 compression disabled\n"); } if (use_swf) { - printf("INI: SWF compression enabled\n"); + print_to_console("INI: SWF compression enabled\n"); } else { - printf("INI: SWF compression disabled\n"); + print_to_console("INI: SWF compression disabled\n"); } if (use_base64) { - printf("INI: Base64 compression enabled\n"); + print_to_console("INI: Base64 compression enabled\n"); } else { - printf("INI: Base64 compression disabled\n"); + print_to_console("INI: Base64 compression disabled\n"); } if (use_bzip2) { - printf("INI: bZip2 compression enabled\n"); + print_to_console("INI: bZip2 compression enabled\n"); } else { - printf("INI: bZip2 compression disabled\n"); + print_to_console("INI: bZip2 compression disabled\n"); } valid_param = true; @@ -2199,12 +2231,12 @@ int init_comfort(int argc, char* argv[]) { for (j = 0; j < ((int)strlen(value)); j += 3) { if ((j+2) < (int)strlen(value)) { if (value[j+2] != ',') { - printf("ERROR: zLib levels have to be separated with commas\n"); + print_to_console("ERROR: zLib levels have to be separated with commas\n"); exit(1); } } if ((j+1) >= (int)strlen(value)) { - printf("ERROR: Last zLib level is incomplete\n"); + print_to_console("ERROR: Last zLib level is incomplete\n"); exit(1); } int comp_level_to_use = (char(value[j]) - '1'); @@ -2213,13 +2245,13 @@ int init_comfort(int argc, char* argv[]) { && ((mem_level_to_use >= 0) && (mem_level_to_use <= 8))) { use_zlib_level[comp_level_to_use + mem_level_to_use * 9] = true; } else { - printf("ERROR: Invalid zlib level %c%c\n", value[j], value[j+1]); + print_to_console("ERROR: Invalid zlib level %c%c\n", value[j], value[j+1]); wait_for_key(); exit(1); } } - printf("INI: Set zLib levels\n"); + print_to_console("INI: Set zLib levels\n"); valid_param = true; } @@ -2241,7 +2273,7 @@ int init_comfort(int argc, char* argv[]) { max_recursion_depth = max_recursion_d; recursion_depth_set = true; - printf("INI: Set maximal recursion depth to %i\n", max_recursion_depth); + print_to_console("INI: Set maximal recursion depth to %i\n", max_recursion_depth); valid_param = true; } @@ -2282,7 +2314,7 @@ int init_comfort(int argc, char* argv[]) { case ' ': break; default: - printf("ERROR: Invalid char in ignore_positions: %c\n", value[j]); + print_to_console("ERROR: Invalid char in ignore_positions: %c\n", value[j]); wait_for_key(); exit(1); } @@ -2294,7 +2326,7 @@ int init_comfort(int argc, char* argv[]) { } if (print_ignore_positions_message) { - printf("INI: Set ignore positions\n"); + print_to_console("INI: Set ignore positions\n"); print_ignore_positions_message = false; } @@ -2302,7 +2334,7 @@ int init_comfort(int argc, char* argv[]) { } if (!valid_param) { - printf("ERROR: Invalid INI parameter: %s\n", param); + print_to_console("ERROR: Invalid INI parameter: %s\n", param); wait_for_key(); exit(1); } @@ -2335,39 +2367,39 @@ int init_comfort(int argc, char* argv[]) { } if (file_exists(output_file_name)) { - printf("Output file \"%s\" exists. Overwrite (y/n)? ", output_file_name); + print_to_console("Output file \"%s\" exists. Overwrite (y/n)? ", output_file_name); char ch = getche(); if ((ch != 'Y') && (ch != 'y')) { - printf("\n"); + print_to_console("\n"); wait_for_key(); exit(0); } else { - printf("\n\n"); + print_to_console("\n\n"); } } else { - printf("\n"); + print_to_console("\n"); } fout = fopen(output_file_name,"wb"); if (fout == NULL) { - printf("ERROR: Can't create output file \"%s\"\n", output_file_name); + print_to_console("ERROR: Can't create output file \"%s\"\n", output_file_name); wait_for_key(); exit(1); } - printf("Input file: %s\n",input_file_name); - printf("Output file: %s\n\n",output_file_name); + print_to_console("Input file: %s\n",input_file_name); + print_to_console("Output file: %s\n\n",output_file_name); if (DEBUG_MODE) { if (min_ident_size_set) { - printf("\n"); - printf("Minimal ident size set to %i bytes\n", min_ident_size); + print_to_console("\n"); + print_to_console("Minimal ident size set to %i bytes\n", min_ident_size); } if (ignore_list_len > 0) { - printf("\n"); - printf("Ignore position list:\n"); + print_to_console("\n"); + print_to_console("Ignore position list:\n"); for (i = 0; i < ignore_list_len; i++) { cout << ignore_list[j] << endl; } - printf("\n"); + print_to_console("\n"); } } @@ -2402,27 +2434,28 @@ void denit_compress() { safe_fclose(&fout); if ((recursion_depth == 0) && (!DEBUG_MODE) && show_lzma_progress && (old_lzma_progress_text_length > -1)) { - printf("%s", string(old_lzma_progress_text_length, '\b').c_str()); // backspaces to remove old lzma progress text + print_to_console("%s", string(old_lzma_progress_text_length, '\b').c_str()); // backspaces to remove old lzma progress text } #ifndef PRECOMPDLL if (strcmp(output_file_name, "stdout") != 0) { long long fout_length = fileSize64(output_file_name); if (recursion_depth == 0) { + std::string result_print = "New size: " + std::to_string(fout_length) + " instead of " + std::to_string(fin_length) + " \n"; if (!DEBUG_MODE) { - printf("%s", string(14, '\b').c_str()); - cout << "100.00% - New size: " << fout_length << " instead of " << fin_length << " " << endl; + print_to_console("%s", string(14, '\b').c_str()); + print_to_console("100.00% - " + result_print); } else { - cout << "New size: " << fout_length << " instead of " << fin_length << " " << endl; + print_to_console(result_print); } } } #else if (recursion_depth == 0) { if (!DEBUG_MODE) { - printf(string(14,'\b').c_str()); - printf("100.00%% - "); + print_to_console(string(14,'\b').c_str()); + print_to_console("100.00%% - "); printf_time(get_time_ms() - start_time); } } @@ -2430,31 +2463,31 @@ void denit_compress() { #ifndef PRECOMPDLL if (recursion_depth == 0) { - printf("\nDone.\n"); + print_to_console("\nDone.\n"); printf_time(get_time_ms() - start_time); // statistics - printf("\nRecompressed streams: %i/%i\n", recompressed_streams_count, decompressed_streams_count); + print_to_console("\nRecompressed streams: %i/%i\n", recompressed_streams_count, decompressed_streams_count); if ((recompressed_streams_count > 0) || (decompressed_streams_count > 0)) { - if ((use_pdf) && ((recompressed_pdf_count > 0) || (decompressed_pdf_count > 0))) printf("PDF streams: %i/%i\n", recompressed_pdf_count, decompressed_pdf_count); + if ((use_pdf) && ((recompressed_pdf_count > 0) || (decompressed_pdf_count > 0))) print_to_console("PDF streams: %i/%i\n", recompressed_pdf_count, decompressed_pdf_count); if (pdf_bmp_mode) { - if ((use_pdf) && ((recompressed_pdf_count_8_bit > 0) || (decompressed_pdf_count_8_bit > 0))) printf("PDF image streams (8-bit): %i/%i\n", recompressed_pdf_count_8_bit, decompressed_pdf_count_8_bit); - if ((use_pdf) && ((recompressed_pdf_count_24_bit > 0) || (decompressed_pdf_count_24_bit > 0))) printf("PDF image streams (24-bit): %i/%i\n", recompressed_pdf_count_24_bit, decompressed_pdf_count_24_bit); - } - if ((use_zip) && ((recompressed_zip_count > 0) || (decompressed_zip_count > 0))) printf("ZIP streams: %i/%i\n", recompressed_zip_count, decompressed_zip_count); - if ((use_gzip) && ((recompressed_gzip_count > 0) || (decompressed_gzip_count > 0))) printf("GZip streams: %i/%i\n", recompressed_gzip_count, decompressed_gzip_count); - if ((use_png) && ((recompressed_png_count > 0) || (decompressed_png_count > 0))) printf("PNG streams: %i/%i\n", recompressed_png_count, decompressed_png_count); - if ((use_png) && ((recompressed_png_multi_count > 0) || (decompressed_png_multi_count > 0))) printf("PNG streams (multi): %i/%i\n", recompressed_png_multi_count, decompressed_png_multi_count); - if ((use_gif) && ((recompressed_gif_count > 0) || (decompressed_gif_count > 0))) printf("GIF streams: %i/%i\n", recompressed_gif_count, decompressed_gif_count); - if ((use_jpg) && ((recompressed_jpg_count > 0) || (decompressed_jpg_count > 0))) printf("JPG streams: %i/%i\n", recompressed_jpg_count, decompressed_jpg_count); - if ((use_jpg) && ((recompressed_jpg_prog_count > 0) || (decompressed_jpg_prog_count > 0))) printf("JPG streams (progressive): %i/%i\n", recompressed_jpg_prog_count, decompressed_jpg_prog_count); - if ((use_mp3) && ((recompressed_mp3_count > 0) || (decompressed_mp3_count > 0))) printf("MP3 streams: %i/%i\n", recompressed_mp3_count, decompressed_mp3_count); - if ((use_swf) && ((recompressed_swf_count > 0) || (decompressed_swf_count > 0))) printf("SWF streams: %i/%i\n", recompressed_swf_count, decompressed_swf_count); - if ((use_base64) && ((recompressed_base64_count > 0) || (decompressed_base64_count > 0))) printf("Base64 streams: %i/%i\n", recompressed_base64_count, decompressed_base64_count); - if ((use_bzip2) && ((recompressed_bzip2_count > 0) || (decompressed_bzip2_count > 0))) printf("bZip2 streams: %i/%i\n", recompressed_bzip2_count, decompressed_bzip2_count); - if ((intense_mode) && ((recompressed_zlib_count > 0) || (decompressed_zlib_count > 0))) printf("zLib streams (intense mode): %i/%i\n", recompressed_zlib_count, decompressed_zlib_count); - if ((brute_mode) && ((recompressed_brute_count > 0) || (decompressed_brute_count > 0))) printf("Brute mode streams: %i/%i\n", recompressed_brute_count, decompressed_brute_count); + if ((use_pdf) && ((recompressed_pdf_count_8_bit > 0) || (decompressed_pdf_count_8_bit > 0))) print_to_console("PDF image streams (8-bit): %i/%i\n", recompressed_pdf_count_8_bit, decompressed_pdf_count_8_bit); + if ((use_pdf) && ((recompressed_pdf_count_24_bit > 0) || (decompressed_pdf_count_24_bit > 0))) print_to_console("PDF image streams (24-bit): %i/%i\n", recompressed_pdf_count_24_bit, decompressed_pdf_count_24_bit); + } + if ((use_zip) && ((recompressed_zip_count > 0) || (decompressed_zip_count > 0))) print_to_console("ZIP streams: %i/%i\n", recompressed_zip_count, decompressed_zip_count); + if ((use_gzip) && ((recompressed_gzip_count > 0) || (decompressed_gzip_count > 0))) print_to_console("GZip streams: %i/%i\n", recompressed_gzip_count, decompressed_gzip_count); + if ((use_png) && ((recompressed_png_count > 0) || (decompressed_png_count > 0))) print_to_console("PNG streams: %i/%i\n", recompressed_png_count, decompressed_png_count); + if ((use_png) && ((recompressed_png_multi_count > 0) || (decompressed_png_multi_count > 0))) print_to_console("PNG streams (multi): %i/%i\n", recompressed_png_multi_count, decompressed_png_multi_count); + if ((use_gif) && ((recompressed_gif_count > 0) || (decompressed_gif_count > 0))) print_to_console("GIF streams: %i/%i\n", recompressed_gif_count, decompressed_gif_count); + if ((use_jpg) && ((recompressed_jpg_count > 0) || (decompressed_jpg_count > 0))) print_to_console("JPG streams: %i/%i\n", recompressed_jpg_count, decompressed_jpg_count); + if ((use_jpg) && ((recompressed_jpg_prog_count > 0) || (decompressed_jpg_prog_count > 0))) print_to_console("JPG streams (progressive): %i/%i\n", recompressed_jpg_prog_count, decompressed_jpg_prog_count); + if ((use_mp3) && ((recompressed_mp3_count > 0) || (decompressed_mp3_count > 0))) print_to_console("MP3 streams: %i/%i\n", recompressed_mp3_count, decompressed_mp3_count); + if ((use_swf) && ((recompressed_swf_count > 0) || (decompressed_swf_count > 0))) print_to_console("SWF streams: %i/%i\n", recompressed_swf_count, decompressed_swf_count); + if ((use_base64) && ((recompressed_base64_count > 0) || (decompressed_base64_count > 0))) print_to_console("Base64 streams: %i/%i\n", recompressed_base64_count, decompressed_base64_count); + if ((use_bzip2) && ((recompressed_bzip2_count > 0) || (decompressed_bzip2_count > 0))) print_to_console("bZip2 streams: %i/%i\n", recompressed_bzip2_count, decompressed_bzip2_count); + if ((intense_mode) && ((recompressed_zlib_count > 0) || (decompressed_zlib_count > 0))) print_to_console("zLib streams (intense mode): %i/%i\n", recompressed_zlib_count, decompressed_zlib_count); + if ((brute_mode) && ((recompressed_brute_count > 0) || (decompressed_brute_count > 0))) print_to_console("Brute mode streams: %i/%i\n", recompressed_brute_count, decompressed_brute_count); } if (!level_switch_used) show_used_levels(); @@ -2484,17 +2517,17 @@ void denit_decompress() { #ifndef PRECOMPDLL if (recursion_depth == 0) { if (!DEBUG_MODE) { - printf("%s", string(14,'\b').c_str()); - printf("100.00%%\n"); + print_to_console("%s", string(14,'\b').c_str()); + print_to_console("100.00%%\n"); } - printf("\nDone.\n"); + print_to_console("\nDone.\n"); printf_time(get_time_ms() - start_time); } #else if (recursion_depth == 0) { if (!DEBUG_MODE) { - printf(string(14,'\b').c_str()); - printf("100.00%% - "); + print_to_console(string(14,'\b').c_str()); + print_to_console("100.00%% - "); printf_time(get_time_ms() - start_time); } } @@ -2521,26 +2554,30 @@ void denit_convert() { safe_fclose(&fout); if ((!DEBUG_MODE) && show_lzma_progress && (conversion_to_method == OTF_XZ_MT) && (old_lzma_progress_text_length > -1)) { - printf("%s", string(old_lzma_progress_text_length, '\b').c_str()); // backspaces to remove old lzma progress text + print_to_console("%s", string(old_lzma_progress_text_length, '\b').c_str()); // backspaces to remove old lzma progress text } - long long fout_length = fileSize64(output_file_name); - #ifndef PRECOMPDLL - if (!DEBUG_MODE) { - printf("%s", string(14,'\b').c_str()); - cout << "100.00% - New size: " << fout_length << " instead of " << fin_length << " " << endl; - } else { - cout << "New size: " << fout_length << " instead of " << fin_length << " " << endl; - } - printf("\nDone.\n"); - printf_time(get_time_ms() - start_time); - #else - if (!DEBUG_MODE) { - printf(string(14,'\b').c_str()); - cout << "100.00% - New size: " << fout_length << " instead of " << fin_length << " " << endl; - printf_time(get_time_ms() - start_time); - } - #endif + if (strcmp(output_file_name, "stdout") != 0) { + long long fout_length = fileSize64(output_file_name); + std::string result_print = "New size: " + std::to_string(fout_length) + " instead of " + std::to_string(fin_length) + " \n"; +#ifndef PRECOMPDLL + if (!DEBUG_MODE) { + print_to_console("%s", string(14, '\b').c_str()); + print_to_console("100.00% - " + result_print); + } + else { + print_to_console(result_print); + } + print_to_console("\nDone.\n"); + printf_time(get_time_ms() - start_time); +#else + if (!DEBUG_MODE) { + print_to_console(string(14, '\b').c_str()); + print_to_console(result_print); + printf_time(get_time_ms() - start_time); + } +#endif + } denit(); } @@ -3348,13 +3385,13 @@ void debug_sums(const recompress_deflate_result& rdres) { sum_uncompressed += rdres.uncompressed_stream_size; sum_expansion += rdres.uncompressed_stream_size - rdres.compressed_stream_size; sum_recon += rdres.recon_data.size(); - //printf("deflate sums: c %I64d, u %I64d, x %I64d, r %I64d, i %I64d, o %I64d\n", + //print_to_console("deflate sums: c %I64d, u %I64d, x %I64d, r %I64d, i %I64d, o %I64d\n", // sum_compressed, sum_uncompressed, sum_expansion, sum_recon, (uint64_t)tell_64(fin), (uint64_t)tell_64(fout)); } } void debug_pos() { if (DEBUG_MODE) { - //printf("deflate pos: i %I64d, o %I64d\n", (uint64_t)tell_64(fin), (uint64_t)tell_64(fout)); + //print_to_console("deflate pos: i %I64d, o %I64d\n", (uint64_t)tell_64(fin), (uint64_t)tell_64(fout)); } } void try_decompression_pdf(int windowbits, int pdf_header_length, int img_width, int img_height, int img_bpc) { @@ -3387,14 +3424,14 @@ void try_decompression_pdf(int windowbits, int pdf_header_length, int img_width, if (rdres.uncompressed_stream_size == (img_width * img_height)) { bmp_header_type = 1; if (DEBUG_MODE) { - printf("Image size did match (8 bit)\n"); + print_to_console("Image size did match (8 bit)\n"); } recompressed_pdf_count_8_bit++; recompressed_pdf_count--; } else if (rdres.uncompressed_stream_size == (img_width * img_height * 3)) { bmp_header_type = 2; if (DEBUG_MODE) { - printf("Image size did match (24 bit)\n"); + print_to_console("Image size did match (24 bit)\n"); } decompressed_pdf_count_8_bit--; decompressed_pdf_count_24_bit++; @@ -3402,7 +3439,7 @@ void try_decompression_pdf(int windowbits, int pdf_header_length, int img_width, recompressed_pdf_count--; } else { if (DEBUG_MODE) { - printf("Image size didn't match with stream size\n"); + print_to_console("Image size didn't match with stream size\n"); } decompressed_pdf_count_8_bit--; decompressed_pdf_count++; @@ -3545,7 +3582,7 @@ void try_decompression_pdf(int windowbits, int pdf_header_length, int img_width, if (intense_mode_is_active()) intense_ignore_offsets->insert(input_file_pos - 2); if (brute_mode_is_active()) brute_ignore_offsets->insert(input_file_pos); if (DEBUG_MODE) { - printf("No matches\n"); + print_to_console("No matches\n"); } } } @@ -3609,7 +3646,7 @@ void try_decompression_deflate_type(unsigned& dcounter, unsigned& rcounter, if (type == D_SWF && intense_mode_is_active()) intense_ignore_offsets->insert(input_file_pos - 2); if (type != D_BRUTE && brute_mode_is_active()) brute_ignore_offsets->insert(input_file_pos); if (DEBUG_MODE) { - printf("No matches\n"); + print_to_console("No matches\n"); } } @@ -3626,23 +3663,23 @@ void show_used_levels() { if (!anything_was_used) { if (!non_zlib_was_used) { if (compression_otf_method == OTF_NONE) { - printf("\nNone of the given compression and memory levels could be used.\n"); - printf("There will be no gain compressing the output file.\n"); + print_to_console("\nNone of the given compression and memory levels could be used.\n"); + print_to_console("There will be no gain compressing the output file.\n"); } } else { if ((!max_recursion_depth_reached) && (max_recursion_depth_used != max_recursion_depth)) { #ifdef COMFORT - printf("\nYou can speed up Precomp for THIS FILE with these INI parameters:\n"); - printf("Maximal_Recursion_Depth="); + print_to_console("\nYou can speed up Precomp for THIS FILE with these INI parameters:\n"); + print_to_console("Maximal_Recursion_Depth="); #else - printf("\nYou can speed up Precomp for THIS FILE with these parameters:\n"); - printf("-d"); + print_to_console("\nYou can speed up Precomp for THIS FILE with these parameters:\n"); + print_to_console("-d"); #endif - printf("%i\n", max_recursion_depth_used); + print_to_console("%i\n", max_recursion_depth_used); } } if (max_recursion_depth_reached) { - printf("\nMaximal recursion depth %i reached, increasing it could give better results.\n", max_recursion_depth); + print_to_console("\nMaximal recursion depth %i reached, increasing it could give better results.\n", max_recursion_depth); } return; } @@ -3650,11 +3687,11 @@ void show_used_levels() { int i, i_sort; int level_count = 0; #ifdef COMFORT - printf("\nYou can speed up Precomp for THIS FILE with these INI parameters:\n"); - printf("zLib_Levels="); + print_to_console("\nYou can speed up Precomp for THIS FILE with these INI parameters:\n"); + print_to_console("zLib_Levels="); #else - printf("\nYou can speed up Precomp for THIS FILE with these parameters:\n"); - printf("-zl"); + print_to_console("\nYou can speed up Precomp for THIS FILE with these parameters:\n"); + print_to_console("-zl"); #endif bool first_one = true; @@ -3662,11 +3699,11 @@ void show_used_levels() { i_sort = (i % 9) * 9 + (i / 9); // to get the displayed levels sorted if (zlib_level_was_used[i_sort]) { if (!first_one) { - printf(","); + print_to_console(","); } else { first_one = false; } - printf("%i%i", (i_sort%9) + 1, (i_sort/9) + 1); + print_to_console("%i%i", (i_sort%9) + 1, (i_sort/9) + 1); level_count++; } } @@ -3684,28 +3721,28 @@ void show_used_levels() { if (((use_mp3) && ((recompressed_mp3_count == 0) && (decompressed_mp3_count > 0)))) disable_methods += '3'; if ( disable_methods.length() > 0 ) { #ifdef COMFORT - printf("\nCompression_Types_Disable=%s",disable_methods.c_str()); + print_to_console("\nCompression_Types_Disable=%s",disable_methods.c_str()); #else - printf(" -t-%s",disable_methods.c_str()); + print_to_console(" -t-%s",disable_methods.c_str()); #endif } if (max_recursion_depth_reached) { - printf("\n\nMaximal recursion depth %i reached, increasing it could give better results.\n", max_recursion_depth); + print_to_console("\n\nMaximal recursion depth %i reached, increasing it could give better results.\n", max_recursion_depth); } else if (max_recursion_depth_used != max_recursion_depth) { #ifdef COMFORT - printf("\nMaximal_Recursion_Depth="); + print_to_console("\nMaximal_Recursion_Depth="); #else - printf(" -d"); + print_to_console(" -d"); #endif - printf("%i", max_recursion_depth_used); + print_to_console("%i", max_recursion_depth_used); } if ((level_count == 1) && (!fast_mode)) { - printf("\n\nFast mode does exactly the same for this file, only faster.\n"); + print_to_console("\n\nFast mode does exactly the same for this file, only faster.\n"); } - printf("\n"); + print_to_console("\n"); } bool compress_file(float min_percent, float max_percent) { @@ -3767,7 +3804,7 @@ bool compress_file(float min_percent, float max_percent) { // local file header? if ((in_buf[cb + 2] == 3) && (in_buf[cb + 3] == 4)) { if (DEBUG_MODE) { - printf("ZIP header detected\n"); + print_to_console("ZIP header detected\n"); print_debug_percent(); cout << "ZIP header detected at position " << input_file_pos << endl; } @@ -3776,10 +3813,10 @@ bool compress_file(float min_percent, float max_percent) { unsigned int filename_length = (in_buf[cb + 27] << 8) + in_buf[cb + 26]; unsigned int extra_field_length = (in_buf[cb + 29] << 8) + in_buf[cb + 28]; if (DEBUG_MODE) { - printf("compressed size: %i\n", compressed_size); - printf("uncompressed size: %i\n", uncompressed_size); - printf("file name length: %i\n", filename_length); - printf("extra field length: %i\n", extra_field_length); + print_to_console("compressed size: %i\n", compressed_size); + print_to_console("uncompressed size: %i\n", uncompressed_size); + print_to_console("file name length: %i\n", filename_length); + print_to_console("extra field length: %i\n", extra_field_length); } if ((filename_length + extra_field_length) <= CHECKBUF_SIZE @@ -3984,7 +4021,7 @@ bool compress_file(float min_percent, float max_percent) { if ((width_val != 0) && (height_val != 0) && (bpc_val != 0)) { if (DEBUG_MODE) { - printf("Possible image in PDF found: %i * %i, %i bit\n", width_val, height_val, bpc_val); + print_to_console("Possible image in PDF found: %i * %i, %i bit\n", width_val, height_val, bpc_val); } } } @@ -4375,7 +4412,7 @@ bool compress_file(float min_percent, float max_percent) { if (DEBUG_MODE) { print_debug_percent(); cout << "Unsupported MP3 type found at position " << saved_input_file_pos << ", length " << mp3_length << endl; - printf ("Type: %s\n", filetype_description[type]); + print_to_console("Type: %s\n", filetype_description[type]); } } } @@ -4645,8 +4682,8 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress debug_deflate_reconstruct(rdres, "PDF", hdr_length, 0); if (DEBUG_MODE) { - if (bmp_c == 1) printf("Skipping BMP header (8-Bit)\n"); - if (bmp_c == 2) printf("Skipping BMP header (24-Bit)\n"); + if (bmp_c == 1) print_to_console("Skipping BMP header (8-Bit)\n"); + if (bmp_c == 2) print_to_console("Skipping BMP header (24-Bit)\n"); } // read BMP header @@ -4676,7 +4713,7 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress skip_part = (-bmp_width) & 3; } if (!try_reconstructing_deflate_skip(fin, fout, rdres, read_part, skip_part)) { - printf("Error recompressing data!"); + print_to_console("Error recompressing data!"); exit(0); } break; @@ -4694,7 +4731,7 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress debug_deflate_reconstruct(rdres, "ZIP", hdr_length, recursion_data_length); if (!ok) { - printf("Error recompressing data!"); + print_to_console("Error recompressing data!"); exit(0); } break; @@ -4710,7 +4747,7 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress debug_deflate_reconstruct(rdres, "GZIP", hdr_length, recursion_data_length); if (!ok) { - printf("Error recompressing data!"); + print_to_console("Error recompressing data!"); exit(0); } break; @@ -4729,7 +4766,7 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress debug_deflate_reconstruct(rdres, "PNG", hdr_length, 0); if (!try_reconstructing_deflate(fin, fout, rdres)) { - printf("Error recompressing data!"); + print_to_console("Error recompressing data!"); exit(0); } debug_pos(); @@ -4765,7 +4802,7 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress debug_deflate_reconstruct(rdres, "PNG multi", hdr_length, 0); if (!try_reconstructing_deflate_multipng(fin, fout, rdres, idat_count, idat_crcs, idat_lengths)) { - printf("Error recompressing data!"); + print_to_console("Error recompressing data!"); exit(0); } debug_pos(); @@ -4778,7 +4815,7 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress case D_GIF: { // GIF recompression if (DEBUG_MODE) { - printf("Decompressed data - GIF\n"); + print_to_console("Decompressed data - GIF\n"); } unsigned char block_size = 255; @@ -4794,7 +4831,7 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress gDiff.GIFDiff = (unsigned char*)malloc(gDiff.GIFDiffIndex * sizeof(unsigned char)); own_fread(gDiff.GIFDiff, 1, gDiff.GIFDiffIndex, fin); if (DEBUG_MODE) { - printf("Diff bytes were used: %i bytes\n", gDiff.GIFDiffIndex); + print_to_console("Diff bytes were used: %i bytes\n", gDiff.GIFDiffIndex); } gDiff.GIFDiffSize = gDiff.GIFDiffIndex; gDiff.GIFDiffIndex = 0; @@ -4835,7 +4872,7 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress if (recompress_success_needed) { if (!recompress_success) { - printf("Error recompressing data!"); + print_to_console("Error recompressing data!"); GifDiffFree(&gDiff); exit(0); } @@ -4877,7 +4914,7 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress case D_JPG: { // JPG recompression if (DEBUG_MODE) { - printf("Decompressed data - JPG\n"); + print_to_console("Decompressed data - JPG\n"); } bool mjpg_dht_used = ((header1 & 4) == 4); @@ -4938,8 +4975,8 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress } if (!recompress_success) { - if (DEBUG_MODE) printf ("packJPG error: %s\n", recompress_msg); - printf("Error recompressing data!"); + if (DEBUG_MODE) print_to_console("packJPG error: %s\n", recompress_msg); + print_to_console("Error recompressing data!"); exit(1); } @@ -4980,7 +5017,7 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress } if ((!found_ffda) || ((ffda_pos - 1 - MJPGDHT_LEN) < 0)) { - printf("ERROR: Motion JPG stream corrupted\n"); + print_to_console("ERROR: Motion JPG stream corrupted\n"); exit(1); } @@ -5027,7 +5064,7 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress debug_deflate_reconstruct(rdres, "SWF", hdr_length, recursion_data_length); if (!ok) { - printf("Error recompressing data!"); + print_to_console("Error recompressing data!"); exit(0); } break; @@ -5035,7 +5072,7 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress case D_BASE64: { // Base64 recompression if (DEBUG_MODE) { - printf("Decompressed data - Base64\n"); + print_to_console("Decompressed data - Base64\n"); } int line_case = (header1 >> 2) & 3; @@ -5045,7 +5082,7 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress int base64_header_length = fin_fget_vlint(); if (DEBUG_MODE) { - printf("Base64 header length: %i\n", base64_header_length); + print_to_console("Base64 header length: %i\n", base64_header_length); } own_fread(in, 1, base64_header_length, fin); fputc(*(in) + 1, fout); // first char was decreased @@ -5102,7 +5139,7 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress case D_BZIP2: { // bZip2 recompression if (DEBUG_MODE) { - printf("Decompressed data - bZip2\n"); + print_to_console("Decompressed data - bZip2\n"); } unsigned char header2 = fin_fgetc(); @@ -5112,7 +5149,7 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress int level = header2; if (DEBUG_MODE) { - printf("Compression level: %i\n", level); + print_to_console("Compression level: %i\n", level); } // read penalty bytes @@ -5150,7 +5187,7 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress } if (retval != BZ_OK) { - printf("Error recompressing data!"); + print_to_console("Error recompressing data!"); cout << "retval = " << retval << endl; exit(0); } @@ -5177,7 +5214,7 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress case D_MP3: { // MP3 recompression if (DEBUG_MODE) { - printf("Decompressed data - MP3\n"); + print_to_console("Decompressed data - MP3\n"); } long long recompressed_data_length = fin_fget_vlint(); @@ -5216,8 +5253,8 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress } if (!recompress_success) { - if (DEBUG_MODE) printf ("packMP3 error: %s\n", recompress_msg); - printf("Error recompressing data!"); + if (DEBUG_MODE) print_to_console("packMP3 error: %s\n", recompress_msg); + print_to_console("Error recompressing data!"); exit(1); } @@ -5247,7 +5284,7 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress debug_deflate_reconstruct(rdres, "brute mode", hdr_length, recursion_data_length); if (!ok) { - printf("Error recompressing data!"); + print_to_console("Error recompressing data!"); exit(0); } break; @@ -5261,13 +5298,13 @@ while (!otf_none_end_check() || compression_otf_method != OTF_NONE && decompress debug_deflate_reconstruct(rdres, "raw zLib", hdr_length, recursion_data_length); if (!ok) { - printf("Error recompressing data!"); + print_to_console("Error recompressing data!"); exit(0); } break; } default: - printf("ERROR: Unsupported stream type %i\n", headertype); + print_to_console("ERROR: Unsupported stream type %i\n", headertype); exit(0); } @@ -5386,7 +5423,7 @@ void try_recompress_bzip2(FILE* origfile, int level, long long& compressed_strea } } else { if (DEBUG_MODE) { - printf("Not enough identical recompressed bytes\n"); + print_to_console("Not enough identical recompressed bytes\n"); } } } @@ -5440,8 +5477,8 @@ bool check_for_pcf_file() { fread(in, 1, 3, fin); if ((in[0] == V_MAJOR) && (in[1] == V_MINOR) && (in[2] == V_MINOR2)) { } else { - printf("Input file %s was made with a different Precomp version\n", input_file_name); - printf("PCF version info: %i.%i.%i\n", in[0], in[1], in[2]); + print_to_console("Input file %s was made with a different Precomp version\n", input_file_name); + print_to_console("PCF version info: %i.%i.%i\n", in[0], in[1], in[2]); exit(1); } @@ -5479,15 +5516,15 @@ void read_header() { fread(in, 1, 3, fin); if ((in[0] == 'P') && (in[1] == 'C') && (in[2] == 'F')) { } else { - printf("Input file %s has no valid PCF header\n", input_file_name); + print_to_console("Input file %s has no valid PCF header\n", input_file_name); exit(1); } fread(in, 1, 3, fin); if ((in[0] == V_MAJOR) && (in[1] == V_MINOR) && (in[2] == V_MINOR2)) { } else { - printf("Input file %s was made with a different Precomp version\n", input_file_name); - printf("PCF version info: %i.%i.%i\n", in[0], in[1], in[2]); + print_to_console("Input file %s was made with a different Precomp version\n", input_file_name); + print_to_console("PCF version info: %i.%i.%i\n", in[0], in[1], in[2]); exit(1); } @@ -5515,7 +5552,7 @@ void convert_header() { fread(in, 1, 3, fin); if ((in[0] == 'P') && (in[1] == 'C') && (in[2] == 'F')) { } else { - printf("Input file %s has no valid PCF header\n", input_file_name); + print_to_console("Input file %s has no valid PCF header\n", input_file_name); exit(1); } fwrite(in, 1, 3, fout); @@ -5523,8 +5560,8 @@ void convert_header() { fread(in, 1, 3, fin); if ((in[0] == V_MAJOR) && (in[1] == V_MINOR) && (in[2] == V_MINOR2)) { } else { - printf("Input file %s was made with a different Precomp version\n", input_file_name); - printf("PCF version info: %i.%i.%i\n", in[0], in[1], in[2]); + print_to_console("Input file %s was made with a different Precomp version\n", input_file_name); + print_to_console("PCF version info: %i.%i.%i\n", in[0], in[1], in[2]); exit(1); } fwrite(in, 1, 3, fout); @@ -5532,7 +5569,7 @@ void convert_header() { fread(in, 1, 1, fin); conversion_from_method = in[0]; if (conversion_from_method == conversion_to_method) { - printf("Input file doesn't need to be converted\n"); + print_to_console("Input file doesn't need to be converted\n"); exit(1); } in[0] = conversion_to_method; @@ -5666,7 +5703,7 @@ size_t own_fwrite(const void *ptr, size_t size, size_t count, FILE* stream, bool } } while (otf_bz2_stream_c.avail_out == 0); if (ret < 0) { - printf("ERROR: bZip2 compression failed - return value %i\n", ret); + print_to_console("ERROR: bZip2 compression failed - return value %i\n", ret); exit(1); } result = size * count; @@ -5705,7 +5742,7 @@ size_t own_fwrite(const void *ptr, size_t size, size_t count, FILE* stream, bool break; } - printf("ERROR: liblzma error: %s (error code %u)\n", msg, ret); + print_to_console("ERROR: liblzma error: %s (error code %u)\n", msg, ret); #ifdef COMFORT wait_for_key(); #endif // COMFORT @@ -5760,7 +5797,7 @@ size_t own_fread(void *ptr, size_t size, size_t count, FILE* stream) { ret = BZ2_bzDecompress(&otf_bz2_stream_d); if ((ret != BZ_OK) && (ret != BZ_STREAM_END)) { (void)BZ2_bzDecompressEnd(&otf_bz2_stream_d); - printf("ERROR: bZip2 stream corrupted - return value %i\n", ret); + print_to_console("ERROR: bZip2 stream corrupted - return value %i\n", ret); exit(1); } @@ -5786,7 +5823,7 @@ size_t own_fread(void *ptr, size_t size, size_t count, FILE* stream) { otf_xz_stream_d.avail_in = fread(otf_in, 1, CHUNK, fin); if (ferror(fin)) { - printf("ERROR: Could not read input file\n"); + print_to_console("ERROR: Could not read input file\n"); exit(1); } } @@ -5819,7 +5856,7 @@ size_t own_fread(void *ptr, size_t size, size_t count, FILE* stream) { break; } - printf("ERROR: liblzma error: %s (error code %u)\n", msg, ret); + print_to_console("ERROR: liblzma error: %s (error code %u)\n", msg, ret); #ifdef COMFORT wait_for_key(); #endif // COMFORT @@ -6002,7 +6039,7 @@ void try_decompression_png (int windowbits) { if (intense_mode_is_active()) intense_ignore_offsets->insert(input_file_pos - 2); if (brute_mode_is_active()) brute_ignore_offsets->insert(input_file_pos); if (DEBUG_MODE) { - printf("No matches\n"); + print_to_console("No matches\n"); } } } @@ -6093,7 +6130,7 @@ void try_decompression_png_multi(FILE* fpng, int windowbits) { if (intense_mode_is_active()) intense_ignore_offsets->insert(input_file_pos - 2); if (brute_mode_is_active()) brute_ignore_offsets->insert(input_file_pos); if (DEBUG_MODE) { - printf("No matches\n"); + print_to_console("No matches\n"); } } } @@ -6499,11 +6536,11 @@ void try_decompression_gif(unsigned char version[5]) { if (best_identical_bytes < gif_length) { if (DEBUG_MODE) { - printf ("Recompression failed\n"); + print_to_console("Recompression failed\n"); } } else { if (DEBUG_MODE) { - printf ("Recompression successful\n"); + print_to_console("Recompression successful\n"); } recompress_success_needed = true; @@ -6537,7 +6574,7 @@ void try_decompression_gif(unsigned char version[5]) { fout_fput_vlint(gDiff.GIFDiffIndex); if(DEBUG_MODE) { if (gDiff.GIFDiffIndex > 0) - printf("Diff bytes were used: %i bytes\n", gDiff.GIFDiffIndex); + print_to_console("Diff bytes were used: %i bytes\n", gDiff.GIFDiffIndex); } for (int dbc = 0; dbc < gDiff.GIFDiffIndex; dbc++) { fout_fputc(gDiff.GIFDiff[dbc]); @@ -6546,7 +6583,7 @@ void try_decompression_gif(unsigned char version[5]) { // store penalty bytes, if any if (best_penalty_bytes_len != 0) { if (DEBUG_MODE) { - printf("Penalty bytes were used: %i bytes\n", best_penalty_bytes_len); + print_to_console("Penalty bytes were used: %i bytes\n", best_penalty_bytes_len); } fout_fput_vlint(best_penalty_bytes_len); @@ -6574,7 +6611,7 @@ void try_decompression_gif(unsigned char version[5]) { } else { if (DEBUG_MODE) { - printf ("No matches\n"); + print_to_console("No matches\n"); } safe_fclose(&frecomp); @@ -6594,10 +6631,10 @@ void try_decompression_gif(unsigned char version[5]) { void packjpg_mp3_dll_msg() { - printf("Using packJPG for JPG recompression, packMP3 for MP3 recompression.\n"); - printf("%s\n", pjglib_version_info()); - printf("%s\n", pmplib_version_info()); - printf("More about packJPG and packMP3 here: http://www.matthiasstirner.com\n\n"); + print_to_console("Using packJPG for JPG recompression, packMP3 for MP3 recompression.\n"); + print_to_console("%s\n", pjglib_version_info()); + print_to_console("%s\n", pmplib_version_info()); + print_to_console("More about packJPG and packMP3 here: http://www.matthiasstirner.com\n\n"); } @@ -6606,14 +6643,14 @@ void try_decompression_jpg (long long jpg_length, bool progressive_jpg) { if (DEBUG_MODE) { print_debug_percent(); if (progressive_jpg) { - printf ("Possible JPG (progressive) found at position "); + print_to_console("Possible JPG (progressive) found at position "); } else { - printf ("Possible JPG found at position "); + print_to_console("Possible JPG found at position "); } cout << saved_input_file_pos << ", length " << jpg_length << endl; // do not recompress non-progressive JPGs when prog_only is set if ((!progressive_jpg) && (prog_only)) { - printf("Skipping (only progressive JPGs mode set)\n"); + print_to_console("Skipping (only progressive JPGs mode set)\n"); } } @@ -6640,7 +6677,7 @@ void try_decompression_jpg (long long jpg_length, bool progressive_jpg) { if (use_brunsli) { if (DEBUG_MODE) { - printf("Trying to compress using brunsli...\n"); + print_to_console("Trying to compress using brunsli...\n"); } brunsli::JPEGData jpegData; if (brunsli::ReadJpeg(jpg_mem_in, jpg_length, brunsli::JPEG_READ_ALL, &jpegData)) { @@ -6658,7 +6695,7 @@ void try_decompression_jpg (long long jpg_length, bool progressive_jpg) { } else { if (jpegData.error == brunsli::JPEGReadError::HUFFMAN_TABLE_NOT_FOUND) { - if (DEBUG_MODE) printf("huffman table missing, trying to use Motion JPEG DHT\n"); + if (DEBUG_MODE) print_to_console("huffman table missing, trying to use Motion JPEG DHT\n"); // search 0xFF 0xDA, insert MJPGDHT (MJPGDHT_LEN bytes) bool found_ffda = false; bool found_ff = false; @@ -6709,9 +6746,9 @@ void try_decompression_jpg (long long jpg_length, bool progressive_jpg) { } if (DEBUG_MODE && !brunsli_success) { if (use_packjpg_fallback) { - printf("Brunsli compression failed, using packJPG fallback...\n"); + print_to_console("Brunsli compression failed, using packJPG fallback...\n"); } else { - printf("Brunsli compression failed\n"); + print_to_console("Brunsli compression failed\n"); } } } @@ -6724,7 +6761,7 @@ void try_decompression_jpg (long long jpg_length, bool progressive_jpg) { } } else if (use_packjpg_fallback) { // large stream => use temporary files if (DEBUG_MODE) { - printf("JPG too large for brunsli, using packJPG fallback...\n"); + print_to_console("JPG too large for brunsli, using packJPG fallback...\n"); } // try to decompress at current position fjpg = tryOpen(tempfile0,"wb"); @@ -6745,7 +6782,7 @@ void try_decompression_jpg (long long jpg_length, bool progressive_jpg) { } if ((!recompress_success) && (strncmp(recompress_msg, "huffman table missing", 21) == 0) && (use_mjpeg) && (use_packjpg_fallback)) { - if (DEBUG_MODE) printf ("huffman table missing, trying to use Motion JPEG DHT\n"); + if (DEBUG_MODE) print_to_console("huffman table missing, trying to use Motion JPEG DHT\n"); // search 0xFF 0xDA, insert MJPGDHT (MJPGDHT_LEN bytes) bool found_ffda = false; bool found_ff = false; @@ -6808,7 +6845,7 @@ void try_decompression_jpg (long long jpg_length, bool progressive_jpg) { } if ((!recompress_success) && (use_packjpg_fallback)) { - if (DEBUG_MODE) printf ("packJPG error: %s\n", recompress_msg); + if (DEBUG_MODE) print_to_console("packJPG error: %s\n", recompress_msg); } if (!in_memory) { @@ -6880,7 +6917,7 @@ void try_decompression_jpg (long long jpg_length, bool progressive_jpg) { } else { if (DEBUG_MODE) { - printf("No matches\n"); + print_to_console("No matches\n"); } } @@ -6932,7 +6969,7 @@ void try_decompression_mp3 (long long mp3_length) { if ((pos > 0) && (pos < mp3_length)) { mp3_length = pos; - if (DEBUG_MODE) printf ("Too much garbage data at the end, retry with new length %i\n", pos); + if (DEBUG_MODE) print_to_console("Too much garbage data at the end, retry with new length %i\n", pos); if (in_memory) { pmplib_init_streams(mp3_mem_in, 1, mp3_length, mp3_mem_out, 1); @@ -6977,7 +7014,7 @@ void try_decompression_mp3 (long long mp3_length) { decompressed_mp3_count++; if (!recompress_success) { - if (DEBUG_MODE) printf ("packMP3 error: %s\n", recompress_msg); + if (DEBUG_MODE) print_to_console("packMP3 error: %s\n", recompress_msg); } if (!in_memory) { @@ -7041,7 +7078,7 @@ void try_decompression_mp3 (long long mp3_length) { } else { if (DEBUG_MODE) { - printf("No matches\n"); + print_to_console("No matches\n"); } } @@ -7196,7 +7233,7 @@ void try_decompression_bzip2(int compression_level) { // store penalty bytes, if any if (best_penalty_bytes_len != 0) { if (DEBUG_MODE) { - printf("Penalty bytes were used: %i bytes\n", best_penalty_bytes_len); + print_to_console("Penalty bytes were used: %i bytes\n", best_penalty_bytes_len); } fout_fput_vlint(best_penalty_bytes_len); for (int pbc = 0; pbc < best_penalty_bytes_len; pbc++) { @@ -7228,7 +7265,7 @@ void try_decompression_bzip2(int compression_level) { } else { if (DEBUG_MODE) { - printf("No matches\n"); + print_to_console("No matches\n"); } } @@ -7557,7 +7594,7 @@ void try_decompression_base64(int base64_header_length) { cb += identical_bytes_decomp - 1; } else { if (DEBUG_MODE) { - printf("No match\n"); + print_to_console("No match\n"); } } @@ -7567,7 +7604,7 @@ void try_decompression_base64(int base64_header_length) { #ifdef COMFORT void wait_for_key() { - printf("\nPress any key to continue\n"); + print_to_console("\nPress any key to continue\n"); // wait for key do { Sleep(55); // lower CPU cost @@ -7576,60 +7613,60 @@ void wait_for_key() { #endif void error(int error_nr) { - printf("\nERROR %i: ", error_nr); + print_to_console("\nERROR %i: ", error_nr); switch (error_nr) { case ERR_IGNORE_POS_TOO_BIG: - printf("Ignore position too big"); + print_to_console("Ignore position too big"); break; case ERR_IDENTICAL_BYTE_SIZE_TOO_BIG: - printf("Identical bytes size bigger than 4 GB"); + print_to_console("Identical bytes size bigger than 4 GB"); break; case ERR_ONLY_SET_MIN_SIZE_ONCE: - printf("Minimal identical size can only be set once"); + print_to_console("Minimal identical size can only be set once"); break; case ERR_MORE_THAN_ONE_OUTPUT_FILE: - printf("More than one output file given"); + print_to_console("More than one output file given"); break; case ERR_MORE_THAN_ONE_INPUT_FILE: - printf("More than one input file given"); + print_to_console("More than one input file given"); break; case ERR_DONT_USE_SPACE: - printf("Please don't use a space between the -o switch and the output filename"); + print_to_console("Please don't use a space between the -o switch and the output filename"); break; case ERR_TEMP_FILE_DISAPPEARED: - printf("Temporary file %s disappeared", tempfile1); + print_to_console("Temporary file %s disappeared", tempfile1); break; case ERR_DISK_FULL: - printf("There is not enough space on disk"); + print_to_console("There is not enough space on disk"); // delete output file safe_fclose(&fout); remove(output_file_name); break; case ERR_RECURSION_DEPTH_TOO_BIG: - printf("Recursion depth too big"); + print_to_console("Recursion depth too big"); break; case ERR_ONLY_SET_RECURSION_DEPTH_ONCE: - printf("Recursion depth can only be set once"); + print_to_console("Recursion depth can only be set once"); break; case ERR_CTRL_C: - printf("CTRL-C detected"); + print_to_console("CTRL-C detected"); break; case ERR_INTENSE_MODE_LIMIT_TOO_BIG: - printf("Intense mode level limit too big"); + print_to_console("Intense mode level limit too big"); break; case ERR_BRUTE_MODE_LIMIT_TOO_BIG: - printf("Brute mode level limit too big"); + print_to_console("Brute mode level limit too big"); break; case ERR_ONLY_SET_LZMA_MEMORY_ONCE: - printf("LZMA maximal memory can only be set once"); + print_to_console("LZMA maximal memory can only be set once"); break; case ERR_ONLY_SET_LZMA_THREAD_ONCE: - printf("LZMA thread count can only be set once"); + print_to_console("LZMA thread count can only be set once"); break; default: - printf("Unknown error"); + print_to_console("Unknown error"); } - printf("\n"); + print_to_console("\n"); #ifdef COMFORT wait_for_key(); @@ -7650,13 +7687,13 @@ FILE* tryOpen(const char* filename, const char* mode) { fptr = fopen(filename,mode); } if (fptr == NULL) { - printf("ERROR: Access denied for %s\n", filename); + print_to_console("ERROR: Access denied for %s\n", filename); exit(1); } if (DEBUG_MODE) { - printf("Access problem for %s\n", filename); - printf("Time for getting access: %li ms\n", (long)(get_time_ms() - timeoutstart)); + print_to_console("Access problem for %s\n", filename); + print_to_console("Time for getting access: %li ms\n", (long)(get_time_ms() - timeoutstart)); } return fptr; } @@ -7685,7 +7722,7 @@ long long fileSize64(char* filename) { s2 = GetFileSize(h, &s1); if (GetLastError() != NO_ERROR) { - printf("ERROR: Could not get file size of file %s\n", filename); + print_to_console("ERROR: Could not get file size of file %s\n", filename); CloseHandle(h); exit(0); @@ -7955,7 +7992,7 @@ recursion_result recursion_compress(long long compressed_bytes, long long decomp fin_length = fileSize64(tempfile1); fin = fopen(tempfile1, "rb"); if (fin == NULL) { - printf("ERROR: Recursion input file \"%s\" doesn't exist\n", tempfile1); + print_to_console("ERROR: Recursion input file \"%s\" doesn't exist\n", tempfile1); exit(0); } @@ -7993,7 +8030,7 @@ recursion_result recursion_compress(long long compressed_bytes, long long decomp recursion_depth++; if (DEBUG_MODE) { - printf("Recursion start - new recursion depth %i\n", recursion_depth); + print_to_console("Recursion start - new recursion depth %i\n", recursion_depth); } tmp_r.success = compress_file(recursion_min_percent, recursion_max_percent); @@ -8022,11 +8059,11 @@ recursion_result recursion_compress(long long compressed_bytes, long long decomp if (DEBUG_MODE) { if (tmp_r.success) { - printf("Recursion streams found\n"); + print_to_console("Recursion streams found\n"); } else { - printf("No recursion streams found\n"); + print_to_console("No recursion streams found\n"); } - printf("Recursion end - back to recursion depth %i\n", recursion_depth); + print_to_console("Recursion end - back to recursion depth %i\n", recursion_depth); } if (!tmp_r.success) { @@ -8064,7 +8101,7 @@ recursion_result recursion_decompress(long long recursion_data_length) { fin_length = fileSize64(tempfile1); fin = fopen(tempfile1, "rb"); if (fin == NULL) { - printf("ERROR: Recursion input file \"%s\" doesn't exist\n", tempfile1); + print_to_console("ERROR: Recursion input file \"%s\" doesn't exist\n", tempfile1); exit(0); } @@ -8088,7 +8125,7 @@ recursion_result recursion_decompress(long long recursion_data_length) { recursion_depth++; if (DEBUG_MODE) { - printf("Recursion start - new recursion depth %i\n", recursion_depth); + print_to_console("Recursion start - new recursion depth %i\n", recursion_depth); } decompress_file(); @@ -8102,7 +8139,7 @@ recursion_result recursion_decompress(long long recursion_data_length) { recursion_pop(); if (DEBUG_MODE) { - printf("Recursion end - back to recursion depth %i\n", recursion_depth); + print_to_console("Recursion end - back to recursion depth %i\n", recursion_depth); } // get recursion file size @@ -8325,7 +8362,7 @@ void init_compress_otf() { otf_bz2_stream_c.bzfree = NULL; otf_bz2_stream_c.opaque = NULL; if (BZ2_bzCompressInit(&otf_bz2_stream_c, 9, 0, 0) != BZ_OK) { - printf("ERROR: bZip2 init failed\n"); + print_to_console("ERROR: bZip2 init failed\n"); exit(1); } break; @@ -8344,7 +8381,7 @@ void init_compress_otf() { } if (!init_encoder_mt(&otf_xz_stream_c, threads, max_memory, memory_usage, block_size, otf_xz_extra_params)) { - printf("ERROR: xz Multi-Threaded init failed\n"); + print_to_console("ERROR: xz Multi-Threaded init failed\n"); exit(1); } @@ -8352,7 +8389,9 @@ void init_compress_otf() { if (threads > 1) { plural = "s"; } - cout << "Compressing with LZMA, " << threads << " thread" << plural.c_str() << ", memory usage: " << memory_usage / (1024 * 1024) << " MiB, block size: " << block_size / (1024 * 1024) << " MiB" << endl << endl; + print_to_console( + "Compressing with LZMA, " + std::to_string(threads) + plural + ", memory usage: " + std::to_string(memory_usage / (1024 * 1024)) + " MiB, block size: " + std::to_string(block_size / (1024 * 1024)) + " MiB\n\n" + ); break; } } @@ -8415,14 +8454,14 @@ void init_decompress_otf() { otf_bz2_stream_d.avail_in = 0; otf_bz2_stream_d.next_in = NULL; if (BZ2_bzDecompressInit(&otf_bz2_stream_d, 0, 0) != BZ_OK) { - printf("ERROR: bZip2 init failed\n"); + print_to_console("ERROR: bZip2 init failed\n"); exit(1); } break; } case OTF_XZ_MT: { if (!init_decoder(&otf_xz_stream_d)) { - printf("ERROR: liblzma init failed\n"); + print_to_console("ERROR: liblzma init failed\n"); exit(1); } } @@ -8460,17 +8499,17 @@ long long get_time_ms() { // nice time output, input t in ms // 2^32 ms maximum, so will display incorrect negative values after about 49 days void printf_time(long long t) { - printf("Time: "); + print_to_console("Time: "); if (t < 1000) { // several milliseconds - printf("%li millisecond(s)\n", (long)t); + print_to_console("%li millisecond(s)\n", (long)t); } else if (t < 1000*60) { // several seconds - printf("%li second(s), %li millisecond(s)\n", (long)(t / 1000), (long)(t % 1000)); + print_to_console("%li second(s), %li millisecond(s)\n", (long)(t / 1000), (long)(t % 1000)); } else if (t < 1000*60*60) { // several minutes - printf("%li minute(s), %li second(s)\n", (long)(t / (1000*60)), (long)((t / 1000) % 60)); + print_to_console("%li minute(s), %li second(s)\n", (long)(t / (1000*60)), (long)((t / 1000) % 60)); } else if (t < 1000*60*60*24) { // several hours - printf("%li hour(s), %li minute(s), %li second(s)\n", (long)(t / (1000*60*60)), (long)((t / (1000*60)) % 60), (long)((t / 1000) % 60)); + print_to_console("%li hour(s), %li minute(s), %li second(s)\n", (long)(t / (1000*60*60)), (long)((t / (1000*60)) % 60), (long)((t / 1000) % 60)); } else { - printf("%li day(s), %li hour(s), %li minute(s)\n", (long)(t / (1000*60*60*24)), (long)((t / (1000*60*60)) % 24), (long)((t / (1000*60)) % 60)); + print_to_console("%li day(s), %li hour(s), %li minute(s)\n", (long)(t / (1000*60*60*24)), (long)((t / (1000*60*60)) % 24), (long)((t / (1000*60)) % 60)); } } @@ -8492,24 +8531,24 @@ void print_work_sign(bool with_backspace) { if ((get_time_ms() - work_sign_start_time) >= 250) { work_sign_var = (work_sign_var + 1) % 4; work_sign_start_time = get_time_ms(); - if (with_backspace) printf("\b\b\b\b\b\b"); - printf("%c ", work_signs[work_sign_var]); + if (with_backspace) print_to_console("\b\b\b\b\b\b"); + print_to_console("%c ", work_signs[work_sign_var]); fflush(stdout); } else if (!with_backspace) { - printf("%c ", work_signs[work_sign_var]); + print_to_console("%c ", work_signs[work_sign_var]); fflush(stdout); } } } void print_debug_percent() { - printf("(%.2f%%) ", (input_file_pos / (float)fin_length) * (global_max_percent - global_min_percent) + global_min_percent); + print_to_console("(%.2f%%) ", (input_file_pos / (float)fin_length) * (global_max_percent - global_min_percent) + global_min_percent); } void show_progress(float percent, bool use_backspaces, bool check_time) { if (!check_time || ((get_time_ms() - sec_time) >= 250)) { if (use_backspaces) { - printf("%s", string(6,'\b').c_str()); // backspace to remove work sign and 5 extra spaces + print_to_console("%s", string(6,'\b').c_str()); // backspace to remove work sign and 5 extra spaces } bool new_lzma_text = false; @@ -8518,19 +8557,19 @@ void show_progress(float percent, bool use_backspaces, bool check_time) { if ((snprintf_ret > -1) && (snprintf_ret < 70)) { new_lzma_text = true; if ((old_lzma_progress_text_length > -1) && (use_backspaces)) { - printf("%s", string(old_lzma_progress_text_length, '\b').c_str()); // backspaces to remove old lzma progress text + print_to_console("%s", string(old_lzma_progress_text_length, '\b').c_str()); // backspaces to remove old lzma progress text } old_lzma_progress_text_length = snprintf_ret; } } if (use_backspaces) { - printf("%s", string(8,'\b').c_str()); // backspaces to remove output from %6.2f% + print_to_console("%s", string(8,'\b').c_str()); // backspaces to remove output from %6.2f% } - printf("%6.2f%% ", percent); + print_to_console("%6.2f%% ", percent); if (new_lzma_text) { - printf("%s", lzma_progress_text); + print_to_console("%s", lzma_progress_text); } print_work_sign(false); @@ -8539,9 +8578,9 @@ void show_progress(float percent, bool use_backspaces, bool check_time) { } void ctrl_c_handler(int sig) { - printf("\n\nCTRL-C detected\n"); + print_to_console("\n\nCTRL-C detected\n"); if (tempfilelist_count > 0) { - printf("Removing temporary files...\n"); + print_to_console("Removing temporary files...\n"); for (int i = 0; i < tempfilelist_count; i++) { if ((tempfilelist[i * 20]) == '~') { // just to be safe remove(tempfilelist + i * 20);