summaryrefslogtreecommitdiffstats
path: root/main_common.cc
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 14:25:32 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 14:25:32 +0000
commit4e4f4b70c82887b4be905e55def870047fb0f4e7 (patch)
tree553f2f499192b3fa71f01f814120799aeab8a242 /main_common.cc
parentInitial commit. (diff)
downloadlziprecover-4e4f4b70c82887b4be905e55def870047fb0f4e7.tar.xz
lziprecover-4e4f4b70c82887b4be905e55def870047fb0f4e7.zip
Adding upstream version 1.23.upstream/1.23upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'main_common.cc')
-rw-r--r--main_common.cc196
1 files changed, 196 insertions, 0 deletions
diff --git a/main_common.cc b/main_common.cc
new file mode 100644
index 0000000..8f56a13
--- /dev/null
+++ b/main_common.cc
@@ -0,0 +1,196 @@
+/* Lziprecover - Data recovery tool for the lzip format
+ Copyright (C) 2009-2022 Antonio Diaz Diaz.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+namespace {
+
+const char * const program_year = "2022";
+const char * const mem_msg = "Not enough memory.";
+
+void show_version()
+ {
+ std::printf( "%s %s\n", program_name, PROGVERSION );
+ std::printf( "Copyright (C) %s Antonio Diaz Diaz.\n", program_year );
+ std::printf( "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n"
+ "This is free software: you are free to change and redistribute it.\n"
+ "There is NO WARRANTY, to the extent permitted by law.\n" );
+ }
+
+
+// separate large numbers >= 100_000 in groups of 3 digits using '_'
+const char * format_num3( long long num )
+ {
+ const char * const si_prefix = "kMGTPEZY";
+ const char * const binary_prefix = "KMGTPEZY";
+ enum { buffers = 8, bufsize = 4 * sizeof (long long) };
+ static char buffer[buffers][bufsize]; // circle of static buffers for printf
+ static int current = 0;
+
+ char * const buf = buffer[current++]; current %= buffers;
+ char * p = buf + bufsize - 1; // fill the buffer backwards
+ *p = 0; // terminator
+ const bool negative = num < 0;
+ if( negative ) num = -num;
+ if( num > 1024 )
+ {
+ char prefix = 0; // try binary first, then si
+ for( int i = 0; i < 8 && num >= 1024 && num % 1024 == 0; ++i )
+ { num /= 1024; prefix = binary_prefix[i]; }
+ if( prefix ) *(--p) = 'i';
+ else
+ for( int i = 0; i < 8 && num >= 1000 && num % 1000 == 0; ++i )
+ { num /= 1000; prefix = si_prefix[i]; }
+ if( prefix ) *(--p) = prefix;
+ }
+ const bool split = num >= 100000;
+
+ for( int i = 0; ; )
+ {
+ *(--p) = num % 10 + '0'; num /= 10; if( num == 0 ) break;
+ if( split && ++i >= 3 ) { i = 0; *(--p) = '_'; }
+ }
+ if( negative ) *(--p) = '-';
+ return p;
+ }
+
+
+// Recognized formats: <num>[YZEPTGM][i][Bs], <num>k[Bs], <num>Ki[Bs]
+//
+long long getnum( const char * const arg, const char * const option_name,
+ const int hardbs, const long long llimit = -LLONG_MAX,
+ const long long ulimit = LLONG_MAX,
+ const char ** const tailp = 0 )
+ {
+ char * tail;
+ errno = 0;
+ long long result = strtoll( arg, &tail, 0 );
+ if( tail == arg )
+ {
+ if( verbosity >= 0 )
+ std::fprintf( stderr, "%s: Bad or missing numerical argument in "
+ "option '%s'.\n", program_name, option_name );
+ std::exit( 1 );
+ }
+
+ if( !errno && tail[0] )
+ {
+ char * const p = tail++;
+ int factor = 1000; // default factor
+ int exponent = -1; // -1 = bad multiplier
+ char usuf = 0; // 'B' or 's' unit suffix is present
+ switch( *p )
+ {
+ case 'Y': exponent = 8; break;
+ case 'Z': exponent = 7; break;
+ case 'E': exponent = 6; break;
+ case 'P': exponent = 5; break;
+ case 'T': exponent = 4; break;
+ case 'G': exponent = 3; break;
+ case 'M': exponent = 2; break;
+ case 'K': if( tail[0] == 'i' ) { ++tail; factor = 1024; exponent = 1; } break;
+ case 'k': if( tail[0] != 'i' ) exponent = 1; break;
+ case 'B':
+ case 's': usuf = *p; exponent = 0; break;
+ default : if( tailp ) { tail = p; exponent = 0; }
+ }
+ if( exponent > 1 && tail[0] == 'i' ) { ++tail; factor = 1024; }
+ if( exponent > 0 && usuf == 0 && ( tail[0] == 'B' || tail[0] == 's' ) )
+ { usuf = tail[0]; ++tail; }
+ if( exponent < 0 || ( usuf == 's' && hardbs <= 0 ) ||
+ ( !tailp && tail[0] != 0 ) )
+ {
+ if( verbosity >= 0 )
+ std::fprintf( stderr, "%s: Bad multiplier in numerical argument of "
+ "option '%s'.\n", program_name, option_name );
+ std::exit( 1 );
+ }
+ for( int i = 0; i < exponent; ++i )
+ {
+ if( LLONG_MAX / factor >= llabs( result ) ) result *= factor;
+ else { errno = ERANGE; break; }
+ }
+ if( usuf == 's' )
+ {
+ if( LLONG_MAX / hardbs >= llabs( result ) ) result *= hardbs;
+ else errno = ERANGE;
+ }
+ }
+ if( !errno && ( result < llimit || result > ulimit ) ) errno = ERANGE;
+ if( errno )
+ {
+ if( verbosity >= 0 )
+ std::fprintf( stderr, "%s: Numerical argument out of limits [%s,%s] "
+ "in option '%s'.\n", program_name, format_num3( llimit ),
+ format_num3( ulimit ), option_name );
+ std::exit( 1 );
+ }
+ if( tailp ) *tailp = tail;
+ return result;
+ }
+
+} // end namespace
+
+
+// Recognized formats: <pos>,<value> <pos>,+<value> <pos>,f<value>
+//
+void Bad_byte::parse_bb( const char * const arg, const char * const pn )
+ {
+ option_name = pn;
+ const char * tail;
+ pos = getnum( arg, option_name, 0, 0, LLONG_MAX, &tail );
+ if( tail[0] != ',' )
+ {
+ if( verbosity >= 0 )
+ std::fprintf( stderr, "%s: Bad separator between <pos> and <val> in "
+ "argument of option '%s'.\n", program_name, option_name );
+ std::exit( 1 );
+ }
+ if( tail[1] == '+' ) { ++tail; mode = delta; }
+ else if( tail[1] == 'f' ) { ++tail; mode = flip; }
+ else mode = literal;
+ value = getnum( tail + 1, option_name, 0, 0, 255 );
+ }
+
+
+void show_error( const char * const msg, const int errcode, const bool help )
+ {
+ if( verbosity < 0 ) return;
+ if( msg && msg[0] )
+ std::fprintf( stderr, "%s: %s%s%s\n", program_name, msg,
+ ( errcode > 0 ) ? ": " : "",
+ ( errcode > 0 ) ? std::strerror( errcode ) : "" );
+ if( help )
+ std::fprintf( stderr, "Try '%s --help' for more information.\n",
+ invocation_name );
+ }
+
+
+void show_file_error( const char * const filename, const char * const msg,
+ const int errcode )
+ {
+ if( verbosity >= 0 )
+ std::fprintf( stderr, "%s: %s: %s%s%s\n", program_name, filename, msg,
+ ( errcode > 0 ) ? ": " : "",
+ ( errcode > 0 ) ? std::strerror( errcode ) : "" );
+ }
+
+
+void internal_error( const char * const msg )
+ {
+ if( verbosity >= 0 )
+ std::fprintf( stderr, "%s: internal error: %s\n", program_name, msg );
+ std::exit( 3 );
+ }