diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-01-23 05:49:16 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-01-23 05:50:00 +0000 |
commit | 12326d715eafc241fcbd076d078cdc6171c98076 (patch) | |
tree | df1f839e27e78ffa62b039c14257e6eeca474bad /main.c | |
parent | Releasing debian version 0.7-7. (diff) | |
download | xlunzip-12326d715eafc241fcbd076d078cdc6171c98076.tar.xz xlunzip-12326d715eafc241fcbd076d078cdc6171c98076.zip |
Merging upstream version 0.8.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 245 |
1 files changed, 145 insertions, 100 deletions
@@ -1,5 +1,5 @@ /* Xlunzip - Test tool for the lzip_decompress linux module - Copyright (C) 2016-2021 Antonio Diaz Diaz. + Copyright (C) 2016-2024 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 @@ -16,9 +16,9 @@ */ /* Exit status: 0 for a normal exit, 1 for environmental problems - (file not found, invalid flags, I/O errors, etc), 2 to indicate a - corrupt or invalid input file, 3 for an internal consistency error - (eg, bug) which caused xlunzip to panic. + (file not found, invalid command-line options, I/O errors, etc), 2 to + indicate a corrupt or invalid input file, 3 for an internal consistency + error (e.g., bug) which caused xlunzip to panic. */ #define _FILE_OFFSET_BITS 64 @@ -26,19 +26,19 @@ #include <ctype.h> #include <errno.h> #include <fcntl.h> -#include <limits.h> +#include <limits.h> /* SSIZE_MAX */ #include <signal.h> #include <stdbool.h> -#include <stdint.h> +#include <stdint.h> /* SIZE_MAX */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <utime.h> #include <sys/stat.h> -#if defined(__MSVCRT__) || defined(__OS2__) || defined(__DJGPP__) +#if defined __MSVCRT__ || defined __OS2__ || defined __DJGPP__ #include <io.h> -#if defined(__MSVCRT__) +#if defined __MSVCRT__ #define fchmod(x,y) 0 #define fchown(x,y,z) 0 #define SIGHUP SIGTERM @@ -50,7 +50,7 @@ #define S_IWOTH 0 #endif #endif -#if defined(__DJGPP__) +#if defined __DJGPP__ #define S_ISSOCK(x) 0 #define S_ISVTX 0 #endif @@ -69,13 +69,18 @@ #error "Environments where CHAR_BIT != 8 are not supported." #endif +#if ( defined SIZE_MAX && SIZE_MAX < ULONG_MAX ) || \ + ( defined SSIZE_MAX && SSIZE_MAX < LONG_MAX ) +#error "Environments where 'size_t' is narrower than 'long' are not supported." +#endif + static int verbosity = 0; static void cleanup_and_fail( const int retval ); static void show_error( const char * const msg, const int errcode, const bool help ); static const char * const program_name = "xlunzip"; -static const char * const program_year = "2021"; +static const char * const program_year = "2024"; static const char * invocation_name = "xlunzip"; /* default value */ static const struct { const char * from; const char * to; } known_extensions[] = { @@ -83,8 +88,6 @@ static const struct { const char * from; const char * to; } known_extensions[] = { ".tlz", ".tar" }, { 0, 0 } }; -static int infd = -1; /* needed by the fill function */ - /* Variables used in signal handler context. They are not declared volatile because the handler never returns. */ static char * output_filename = 0; @@ -98,7 +101,7 @@ static void show_help( void ) "linux. Xlunzip is similar to lunzip, but it uses the lzip_decompress linux\n" "module as a backend. Xlunzip tests the module for stream, buffer-to-buffer,\n" "and mixed decompression modes, including in-place decompression (using the\n" - "same buffer for input and output). You can use xlunzip to verify that the\n" + "same buffer for input and output). You can use xlunzip to check that the\n" "module produces correct results when decompressing single member files,\n" "multimember files, or the concatenation of two or more compressed files.\n" "Xlunzip can be used with unzcrash to test the robustness of the module to\n" @@ -132,10 +135,10 @@ static void show_help( void ) "Ki = KiB = 2^10 = 1024, M = 10^6, Mi = 2^20, G = 10^9, Gi = 2^30, etc...\n" "\nTo extract all the files from archive 'foo.tar.lz', use the commands\n" "'tar -xf foo.tar.lz' or 'xlunzip -cd foo.tar.lz | tar -xf -'.\n" - "\nExit status: 0 for a normal exit, 1 for environmental problems (file\n" - "not found, invalid flags, I/O errors, etc), 2 to indicate a corrupt or\n" - "invalid input file, 3 for an internal consistency error (eg, bug) which\n" - "caused xlunzip to panic.\n" + "\nExit status: 0 for a normal exit, 1 for environmental problems\n" + "(file not found, invalid command-line options, I/O errors, etc), 2 to\n" + "indicate a corrupt or invalid input file, 3 for an internal consistency\n" + "error (e.g., bug) which caused xlunzip to panic.\n" "\nReport bugs to lzip-bug@nongnu.org\n" "Xlunzip home page: http://www.nongnu.org/lzip/xlunzip.html\n" ); } @@ -164,8 +167,6 @@ static void * resize_buffer( void * buf, const unsigned min_size ) static void Pp_init( struct Pretty_print * const pp, const char * const filenames[], const int num_filenames ) { - unsigned stdin_name_len; - int i; pp->name = 0; pp->padded_name = 0; pp->stdin_name = "(stdin)"; @@ -173,7 +174,8 @@ static void Pp_init( struct Pretty_print * const pp, pp->first_post = false; if( verbosity <= 0 ) return; - stdin_name_len = strlen( pp->stdin_name ); + const unsigned stdin_name_len = strlen( pp->stdin_name ); + int i; for( i = 0; i < num_filenames; ++i ) { const char * const s = filenames[i]; @@ -204,32 +206,72 @@ static void Pp_set_name( struct Pretty_print * const pp, static void Pp_show_msg( struct Pretty_print * const pp, const char * const msg ) { - if( verbosity >= 0 ) + if( verbosity < 0 ) return; + if( pp->first_post ) { - if( pp->first_post ) - { - pp->first_post = false; - fputs( pp->padded_name, stderr ); - if( !msg ) fflush( stderr ); - } - if( msg ) fprintf( stderr, "%s\n", msg ); + pp->first_post = false; + fputs( pp->padded_name, stderr ); + if( !msg ) fflush( stderr ); } + if( msg ) fprintf( stderr, "%s\n", msg ); } -static unsigned long getnum( const char * const ptr, +/* separate numbers of 5 or more digits in groups of 3 digits using '_' */ +static const char * format_num3( unsigned long long num ) + { + enum { buffers = 8, bufsize = 4 * sizeof num, n = 10 }; + const char * const si_prefix = "kMGTPEZYRQ"; + const char * const binary_prefix = "KMGTPEZYRQ"; + static char buffer[buffers][bufsize]; /* circle of static buffers for printf */ + static int current = 0; + int i; + char * const buf = buffer[current++]; current %= buffers; + char * p = buf + bufsize - 1; /* fill the buffer backwards */ + *p = 0; /* terminator */ + if( num > 1024 ) + { + char prefix = 0; /* try binary first, then si */ + for( i = 0; i < n && num != 0 && num % 1024 == 0; ++i ) + { num /= 1024; prefix = binary_prefix[i]; } + if( prefix ) *(--p) = 'i'; + else + for( i = 0; i < n && num != 0 && num % 1000 == 0; ++i ) + { num /= 1000; prefix = si_prefix[i]; } + if( prefix ) *(--p) = prefix; + } + const bool split = num >= 10000; + + for( i = 0; ; ) + { + *(--p) = num % 10 + '0'; num /= 10; if( num == 0 ) break; + if( split && ++i >= 3 ) { i = 0; *(--p) = '_'; } + } + return p; + } + + +void show_option_error( const char * const arg, const char * const msg, + const char * const option_name ) + { + if( verbosity >= 0 ) + fprintf( stderr, "%s: '%s': %s option '%s'.\n", + program_name, arg, msg, option_name ); + } + + +/* Recognized formats: <num>k, <num>Ki, <num>[MGTPEZYRQ][i] */ +static unsigned long getnum( const char * const arg, + const char * const option_name, const unsigned long llimit, const unsigned long ulimit ) { - unsigned long result; char * tail; errno = 0; - result = strtoul( ptr, &tail, 0 ); - if( tail == ptr ) - { - show_error( "Bad or missing numerical argument.", 0, true ); - exit( 1 ); - } + unsigned long result = strtoul( arg, &tail, 0 ); + if( tail == arg ) + { show_option_error( arg, "Bad or missing numerical argument in", + option_name ); exit( 1 ); } if( !errno && tail[0] ) { @@ -238,6 +280,8 @@ static unsigned long getnum( const char * const ptr, int i; switch( tail[0] ) { + case 'Q': exponent = 10; break; + case 'R': exponent = 9; break; case 'Y': exponent = 8; break; case 'Z': exponent = 7; break; case 'E': exponent = 6; break; @@ -249,10 +293,8 @@ static unsigned long getnum( const char * const ptr, case 'k': if( factor == 1000 ) exponent = 1; break; } if( exponent <= 0 ) - { - show_error( "Bad multiplier in numerical argument.", 0, true ); - exit( 1 ); - } + { show_option_error( arg, "Bad multiplier in numerical argument of", + option_name ); exit( 1 ); } for( i = 0; i < exponent; ++i ) { if( ulimit / factor >= result ) result *= factor; @@ -262,7 +304,10 @@ static unsigned long getnum( const char * const ptr, if( !errno && ( result < llimit || result > ulimit ) ) errno = ERANGE; if( errno ) { - show_error( "Numerical argument out of limits.", 0, false ); + if( verbosity >= 0 ) + fprintf( stderr, "%s: '%s': Value out of limits [%s,%s] in " + "option '%s'.\n", program_name, arg, format_num3( llimit ), + format_num3( ulimit ), option_name ); exit( 1 ); } return result; @@ -305,7 +350,7 @@ static void set_d_outname( const char * const name, const int eindex ) strcpy( output_filename, name ); strcat( output_filename, ".out" ); if( verbosity >= 1 ) - fprintf( stderr, "%s: Can't guess original name for '%s' -- using '%s'\n", + fprintf( stderr, "%s: %s: Can't guess original name -- using '%s'\n", program_name, name, output_filename ); } @@ -326,9 +371,9 @@ static int open_instream( const char * const name, struct stat * const in_statsp if( i != 0 || ( !S_ISREG( mode ) && ( !can_read || one_to_one ) ) ) { if( verbosity >= 0 ) - fprintf( stderr, "%s: Input file '%s' is not a regular file%s.\n", + fprintf( stderr, "%s: %s: Input file is not a regular file%s.\n", program_name, name, ( can_read && one_to_one ) ? - ",\n and neither '-c' nor '-o' were specified" : "" ); + ",\n and neither '-c' nor '-o' were specified" : "" ); close( infd ); infd = -1; } @@ -347,16 +392,12 @@ static bool open_outstream( const bool force, const bool protect ) outfd = open( output_filename, flags, outfd_mode ); if( outfd >= 0 ) delete_output_on_interrupt = true; - else if( verbosity >= 0 ) - { - if( errno == EEXIST ) - fprintf( stderr, "%s: Output file '%s' already exists, skipping.\n", - program_name, output_filename ); - else - fprintf( stderr, "%s: Can't create output file '%s': %s\n", - program_name, output_filename, strerror( errno ) ); - } - return ( outfd >= 0 ); + else if( errno == EEXIST ) + show_file_error( output_filename, + "Output file already exists, skipping.", 0 ); + else + show_file_error( output_filename, "Can't create output file", errno ); + return outfd >= 0; } @@ -374,12 +415,10 @@ static void cleanup_and_fail( const int retval ) if( delete_output_on_interrupt ) { delete_output_on_interrupt = false; - if( verbosity >= 0 ) - fprintf( stderr, "%s: Deleting output file '%s', if it exists.\n", - program_name, output_filename ); + show_file_error( output_filename, "Deleting output file, if it exists.", 0 ); if( outfd >= 0 ) { close( outfd ); outfd = -1; } if( remove( output_filename ) != 0 && errno != ENOENT ) - show_error( "WARNING: deletion of output file (apparently) failed.", 0, false ); + show_error( "warning: deletion of output file failed", errno, false ); } exit( retval ); } @@ -399,7 +438,7 @@ static bool check_tty_in( const char * const input_filename, const int infd, if( isatty( infd ) ) /* for example /dev/tty */ { show_file_error( input_filename, "I won't read compressed data from a terminal.", 0 ); - close( infd ); set_retval( retval, 1 ); + close( infd ); set_retval( retval, 2 ); if( !testing ) cleanup_and_fail( *retval ); return false; } return true; @@ -413,7 +452,7 @@ static void close_and_set_permissions( const struct stat * const in_statsp ) if( in_statsp ) { const mode_t mode = in_statsp->st_mode; - /* fchown will in many cases return with EPERM, which can be safely ignored. */ + /* fchown in many cases returns with EPERM, which can be safely ignored. */ if( fchown( outfd, in_statsp->st_uid, in_statsp->st_gid ) == 0 ) { if( fchmod( outfd, mode ) != 0 ) warning = true; } else @@ -422,10 +461,8 @@ static void close_and_set_permissions( const struct stat * const in_statsp ) warning = true; } if( close( outfd ) != 0 ) - { - show_error( "Error closing output file", errno, false ); - cleanup_and_fail( 1 ); - } + { show_file_error( output_filename, "Error closing output file", errno ); + cleanup_and_fail( 1 ); } outfd = -1; delete_output_on_interrupt = false; if( in_statsp ) @@ -436,7 +473,8 @@ static void close_and_set_permissions( const struct stat * const in_statsp ) if( utime( output_filename, &t ) != 0 ) warning = true; } if( warning && verbosity >= 1 ) - show_error( "Can't change output file attributes.", 0, false ); + show_file_error( output_filename, + "warning: can't change output file attributes", errno ); } @@ -462,13 +500,15 @@ int convert_retval( const int retval ) } +static int global_infd = -1; /* needed by the fill function */ + static long fill( void * buf, unsigned long size ) { unsigned long sz = 0; errno = 0; while( sz < size ) { - const int n = read( infd, (uint8_t *)buf + sz, min( 1UL << 20, size - sz ) ); + const int n = read( global_infd, (uint8_t *)buf + sz, min( 1UL << 20, size - sz ) ); if( n > 0 ) sz += n; else if( n == 0 ) break; /* EOF */ else if( errno != EINTR ) break; @@ -495,9 +535,9 @@ static const char * global_name; /* copy of filename for 'error' */ static void error(char *x) { show_file_error( global_name, x, 0 ); } -static int decompress( struct Pretty_print * const pp, const long cl_insize, - const long cl_outsize, const bool nofill, - const bool noflush, const bool testing ) +static int decompress( const int infd, struct Pretty_print * const pp, + const long cl_insize, const long cl_outsize, + const bool nofill, const bool noflush, const bool testing ) { long in_len = cl_insize; uint8_t * const inbuf = (in_len > 0) ? malloc( in_len ) : 0; @@ -508,17 +548,20 @@ static int decompress( struct Pretty_print * const pp, const long cl_insize, if( ( in_len > 0 && !inbuf ) || ( out_size > 0 && !outbuf ) ) { show_error( mem_msg, 0, false ); return 1; } + global_infd = infd; if( inbuf ) { const long len = fill( inbuf, in_len ); if( len < in_len ) - { if( errno ) { show_file_error( pp->name, "Read error", errno ); return 1; } + { if( errno ) { show_file_error( pp->name, "Read error", errno ); + global_infd = -1; return 1; } in_len = len; } } global_name = pp->name; retval = convert_retval( __lunzip( inbuf, in_len, nofill ? 0 : fill, noflush ? 0 : flush, outbuf, out_size, &in_pos, &out_pos, error ) ); + global_infd = -1; if( retval ) return retval; if( outbuf && noflush ) { @@ -586,25 +629,17 @@ static void internal_error( const char * const msg ) int main( const int argc, const char * const argv[] ) { const char * default_output_filename = ""; - static struct Arg_parser parser; /* static because valgrind complains */ - static struct Pretty_print pp; /* and memory management in C sucks */ - static const char ** filenames = 0; long cl_insize = 0; long cl_outsize = 0; - int num_filenames = 0; - int argind = 0; - int failed_tests = 0; - int retval = 0; int i; - bool filenames_given = false; bool force = false; bool in_place = false; bool keep_input_files = false; bool nofill = false; bool noflush = false; - bool stdin_used = false; bool testing = false; bool to_stdout = false; + if( argc > 0 ) invocation_name = argv[0]; enum { opt_insize = 256, opt_outsize, opt_nofill, opt_noflush }; const struct ap_Option options[] = @@ -621,24 +656,26 @@ int main( const int argc, const char * const argv[] ) { 't', "test", ap_no }, { 'v', "verbose", ap_no }, { 'V', "version", ap_no }, - { opt_insize, "insize", ap_maybe }, + { opt_insize, "insize", ap_maybe }, { opt_outsize, "outsize", ap_maybe }, - { opt_nofill, "nofill", ap_no }, + { opt_nofill, "nofill", ap_no }, { opt_noflush, "noflush", ap_no }, - { 0 , 0, ap_no } }; - - if( argc > 0 ) invocation_name = argv[0]; + { 0, 0, ap_no } }; + /* static because valgrind complains and memory management in C sucks */ + static struct Arg_parser parser; if( !ap_init( &parser, argc, argv, options, 0 ) ) { show_error( mem_msg, 0, false ); return 1; } if( ap_error( &parser ) ) /* bad option */ { show_error( ap_error( &parser ), 0, true ); return 1; } + int argind = 0; for( ; argind < ap_arguments( &parser ); ++argind ) { const int code = ap_code( &parser, argind ); - const char * const arg = ap_argument( &parser, argind ); if( !code ) break; /* no more options */ + const char * const pn = ap_parsed_name( &parser, argind ); + const char * const arg = ap_argument( &parser, argind ); switch( code ) { case 'c': to_stdout = true; break; @@ -655,25 +692,27 @@ int main( const int argc, const char * const argv[] ) case 'v': if( verbosity < 4 ) ++verbosity; break; case 'V': show_version(); return 0; case opt_insize: - cl_insize = arg[0] ? getnum( arg, 1, LONG_MAX ) : 16384; break; + cl_insize = arg[0] ? getnum( arg, pn, 1, LONG_MAX ) : 16384; break; case opt_outsize: cl_outsize = arg[0] ? - getnum( arg, min_dictionary_size, LONG_MAX ) : max_dictionary_size; + getnum( arg, pn, min_dictionary_size, LONG_MAX ) : max_dictionary_size; break; case opt_nofill: nofill = true; break; case opt_noflush: noflush = true; break; - default : internal_error( "uncaught option." ); + default: internal_error( "uncaught option." ); } } /* end process options */ -#if defined(__MSVCRT__) || defined(__OS2__) || defined(__DJGPP__) +#if defined __MSVCRT__ || defined __OS2__ || defined __DJGPP__ setmode( STDIN_FILENO, O_BINARY ); setmode( STDOUT_FILENO, O_BINARY ); #endif - num_filenames = max( 1, ap_arguments( &parser ) - argind ); + static const char ** filenames = 0; + int num_filenames = max( 1, ap_arguments( &parser ) - argind ); filenames = resize_buffer( filenames, num_filenames * sizeof filenames[0] ); filenames[0] = "-"; + bool filenames_given = false; for( i = 0; argind + i < ap_arguments( &parser ); ++i ) { filenames[i] = ap_argument( &parser, argind + i ); @@ -692,15 +731,18 @@ int main( const int argc, const char * const argv[] ) if( !to_stdout && !testing && ( filenames_given || to_file ) ) set_signals( signal_handler ); + static struct Pretty_print pp; Pp_init( &pp, filenames, num_filenames ); + int failed_tests = 0; + int retval = 0; const bool one_to_one = !to_stdout && !testing && !to_file; + bool stdin_used = false; + struct stat in_stats; for( i = 0; i < num_filenames; ++i ) { const char * input_filename = ""; - int tmp; - struct stat in_stats; - const struct stat * in_statsp; + int infd; Pp_set_name( &pp, filenames[i] ); if( strcmp( filenames[i], "-" ) == 0 ) @@ -716,15 +758,15 @@ int main( const int argc, const char * const argv[] ) infd = open_instream( input_filename, &in_stats, one_to_one ); if( infd < 0 ) { set_retval( &retval, 1 ); continue; } if( !check_tty_in( pp.name, infd, testing, &retval ) ) continue; - if( one_to_one ) /* open outfd after verifying infd */ + if( one_to_one ) /* open outfd after checking infd */ { set_d_outname( input_filename, extension_index( input_filename ) ); if( !open_outstream( force, true ) ) - { close( infd ); infd = -1; set_retval( &retval, 1 ); continue; } + { close( infd ); set_retval( &retval, 1 ); continue; } } } - if( to_file && outfd < 0 ) /* open outfd after verifying infd */ + if( to_file && outfd < 0 ) /* open outfd after checking infd */ { output_filename = resize_buffer( output_filename, strlen( default_output_filename ) + 1 ); @@ -732,15 +774,16 @@ int main( const int argc, const char * const argv[] ) if( !open_outstream( force, false ) ) return 1; } - in_statsp = ( input_filename[0] && one_to_one ) ? &in_stats : 0; + const struct stat * const in_statsp = + ( input_filename[0] && one_to_one ) ? &in_stats : 0; + int tmp; if( in_place ) tmp = decompress_in_place( infd, &pp, testing ); else - tmp = decompress( &pp, cl_insize, cl_outsize, nofill, noflush, testing ); + tmp = decompress( infd, &pp, cl_insize, cl_outsize, nofill, noflush, testing ); if( close( infd ) != 0 ) { show_file_error( pp.name, "Error closing input file", errno ); set_retval( &tmp, 1 ); } - infd = -1; set_retval( &retval, tmp ); if( tmp ) { if( !testing ) cleanup_and_fail( retval ); @@ -751,7 +794,9 @@ int main( const int argc, const char * const argv[] ) if( input_filename[0] && !keep_input_files && one_to_one ) remove( input_filename ); } - if( delete_output_on_interrupt ) close_and_set_permissions( 0 ); /* -o */ + if( delete_output_on_interrupt ) /* -o */ + close_and_set_permissions( ( retval == 0 && !stdin_used && + filenames_given && num_filenames == 1 ) ? &in_stats : 0 ); else if( outfd >= 0 && close( outfd ) != 0 ) /* -c */ { show_error( "Error closing stdout", errno, false ); |