diff options
Diffstat (limited to '')
-rw-r--r-- | main.c | 213 |
1 files changed, 123 insertions, 90 deletions
@@ -1,5 +1,5 @@ /* Lunzip - Decompressor for the lzip format - Copyright (C) 2010-2016 Antonio Diaz Diaz. + Copyright (C) 2010-2017 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 @@ -67,14 +67,16 @@ int verbosity = 0; const char * const Program_name = "Lunzip"; const char * const program_name = "lunzip"; -const char * const program_year = "2016"; +const char * const program_year = "2017"; const char * invocation_name = 0; -struct { const char * from; const char * to; } const known_extensions[] = { +const struct { const char * from; const char * to; } known_extensions[] = { { ".lz", "" }, { ".tlz", ".tar" }, { 0, 0 } }; +enum Mode { m_compress, m_decompress, m_list, m_test }; + char * output_filename = 0; int outfd = -1; bool delete_output_on_interrupt = false; @@ -105,6 +107,7 @@ static void show_help( void ) " -d, --decompress decompress (this is the default)\n" " -f, --force overwrite existing output files\n" " -k, --keep keep (don't delete) input files\n" + " -l, --list print (un)compressed file sizes\n" " -o, --output=<file> if reading standard input, write to <file>\n" " -q, --quiet suppress all messages\n" " -t, --test test compressed file integrity\n" @@ -114,6 +117,8 @@ static void show_help( void ) "from standard input to standard output.\n" "Numbers may be followed by a multiplier: k = kB = 10^3 = 1000,\n" "Ki = KiB = 2^10 = 1024, M = 10^6, Mi = 2^20, G = 10^9, Gi = 2^30, etc...\n" + "Buffer sizes 12 to 29 are interpreted as powers of two, meaning 2^12\n" + "to 2^29 bytes.\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" @@ -133,23 +138,38 @@ static void show_version( void ) } +const char * bad_version( const unsigned version ) + { + static char buf[80]; + snprintf( buf, sizeof buf, "Version %u member format not supported.", + version ); + return buf; + } + + +const char * format_ds( const unsigned dictionary_size ) + { + enum { bufsize = 16, factor = 1024 }; + static char buf[bufsize]; + const char * const prefix[8] = + { "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" }; + const char * p = ""; + const char * np = " "; + unsigned num = dictionary_size, i; + bool exact = ( num % factor == 0 ); + + for( i = 0; i < 8 && ( num > 9999 || ( exact && num >= factor ) ); ++i ) + { num /= factor; if( num % factor != 0 ) exact = false; + p = prefix[i]; np = ""; } + snprintf( buf, bufsize, "%s%4u %sB", np, num, p ); + return buf; + } + + static void show_header( const unsigned dictionary_size ) { if( verbosity >= 3 ) - { - const char * const prefix[8] = - { "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" }; - enum { factor = 1024 }; - const char * p = ""; - const char * np = " "; - unsigned num = dictionary_size, i; - bool exact = ( num % factor == 0 ); - - for( i = 0; i < 8 && ( num > 9999 || ( exact && num >= factor ) ); ++i ) - { num /= factor; if( num % factor != 0 ) exact = false; - p = prefix[i]; np = ""; } - fprintf( stderr, "dictionary size %s%4u %sB. ", np, num, p ); - } + fprintf( stderr, "dictionary %s. ", format_ds( dictionary_size ) ); } @@ -169,7 +189,7 @@ static unsigned long getnum( const char * const ptr, if( !errno && tail[0] ) { - const int factor = ( tail[1] == 'i' ) ? 1024 : 1000; + const unsigned factor = ( tail[1] == 'i' ) ? 1024 : 1000; int exponent = 0; /* 0 = bad multiplier */ int i; switch( tail[0] ) @@ -208,7 +228,7 @@ static unsigned long getnum( const char * const ptr, static int get_dict_size( const char * const arg ) { char * tail; - const int bits = strtol( arg, &tail, 0 ); + const long bits = strtol( arg, &tail, 0 ); if( bits >= min_dictionary_bits && bits <= max_dictionary_bits && *tail == 0 ) return ( 1 << bits ); @@ -216,37 +236,44 @@ static int get_dict_size( const char * const arg ) } +void set_mode( enum Mode * const program_modep, const enum Mode new_mode ) + { + if( *program_modep != m_compress && *program_modep != new_mode ) + { + show_error( "Only one operation can be specified.", 0, true ); + exit( 1 ); + } + *program_modep = new_mode; + } + + static int extension_index( const char * const name ) { - int i; - for( i = 0; known_extensions[i].from; ++i ) + int eindex; + for( eindex = 0; known_extensions[eindex].from; ++eindex ) { - const char * const ext = known_extensions[i].from; + const char * const ext = known_extensions[eindex].from; const unsigned name_len = strlen( name ); const unsigned ext_len = strlen( ext ); if( name_len > ext_len && strncmp( name + name_len - ext_len, ext, ext_len ) == 0 ) - return i; + return eindex; } return -1; } -static int open_instream( const char * const name, struct stat * const in_statsp, - const bool no_ofile ) +int open_instream( const char * const name, struct stat * const in_statsp, + const bool no_ofile, const bool reg_only ) { int infd = open( name, O_RDONLY | O_BINARY ); if( infd < 0 ) - { - if( verbosity >= 0 ) - fprintf( stderr, "%s: Can't open input file '%s': %s\n", - program_name, name, strerror( errno ) ); - } + show_file_error( name, "Can't open input file", errno ); else { const int i = fstat( infd, in_statsp ); const mode_t mode = in_statsp->st_mode; - const bool can_read = ( i == 0 && + const bool can_read = ( i == 0 && !reg_only && ( S_ISBLK( mode ) || S_ISCHR( mode ) || S_ISFIFO( mode ) || S_ISSOCK( mode ) ) ); if( i != 0 || ( !S_ISREG( mode ) && ( !can_read || !no_ofile ) ) ) @@ -265,7 +292,7 @@ static int open_instream( const char * const name, struct stat * const in_statsp /* assure at least a minimum size for buffer 'buf' */ -static void * resize_buffer( void * buf, const int min_size ) +void * resize_buffer( void * buf, const unsigned min_size ) { if( buf ) buf = realloc( buf, min_size ); else buf = malloc( min_size ); @@ -278,19 +305,19 @@ static void * resize_buffer( void * buf, const int min_size ) } -static void set_d_outname( const char * const name, const int i ) +static void set_d_outname( const char * const name, const int eindex ) { const unsigned name_len = strlen( name ); - if( i >= 0 ) + if( eindex >= 0 ) { - const char * const from = known_extensions[i].from; + const char * const from = known_extensions[eindex].from; const unsigned from_len = strlen( from ); if( name_len > from_len ) { output_filename = resize_buffer( output_filename, name_len + - strlen( known_extensions[0].to ) + 1 ); + strlen( known_extensions[eindex].to ) + 1 ); strcpy( output_filename, name ); - strcpy( output_filename + name_len - from_len, known_extensions[i].to ); + strcpy( output_filename + name_len - from_len, known_extensions[eindex].to ); return; } } @@ -376,10 +403,10 @@ static void close_and_set_permissions( const struct stat * const in_statsp ) } -static unsigned char xdigit( const int value ) +static unsigned char xdigit( const unsigned value ) { - if( value >= 0 && value <= 9 ) return '0' + value; - if( value >= 10 && value <= 15 ) return 'A' + value - 10; + if( value <= 9 ) return '0' + value; + if( value <= 15 ) return 'A' + value - 10; return 0; } @@ -392,35 +419,28 @@ static bool show_trailing_data( const uint8_t * const data, const int size, { int i; char buf[80]; - int len = snprintf( buf, sizeof buf, "%strailing data = ", - all ? "" : "first bytes of " ); - bool text = true; - for( i = 0; i < size; ++i ) - if( !isprint( data[i] ) ) { text = false; break; } - if( text ) + unsigned len = max( 0, snprintf( buf, sizeof buf, "%strailing data = ", + all ? "" : "first bytes of " ) ); + for( i = 0; i < size && len + 2 < sizeof buf; ++i ) { - if( len > 0 && len < (int)sizeof buf ) - snprintf( buf + len, sizeof buf - len, "'%.*s'", size, (const char *)data ); - } - else - { - for( i = 0; i < size && len > 0 && len + 3 < (int)sizeof buf; ++i ) - { - if( i > 0 ) buf[len++] = ' '; - buf[len++] = xdigit( data[i] >> 4 ); - buf[len++] = xdigit( data[i] & 0x0F ); - buf[len] = 0; - } + buf[len++] = xdigit( data[i] >> 4 ); + buf[len++] = xdigit( data[i] & 0x0F ); + buf[len++] = ' '; } + if( len < sizeof buf ) buf[len++] = '\''; + for( i = 0; i < size && len < sizeof buf; ++i ) + { if( isprint( data[i] ) ) buf[len++] = data[i]; else buf[len++] = '.'; } + if( len < sizeof buf ) buf[len++] = '\''; + if( len < sizeof buf ) buf[len] = 0; else buf[sizeof buf - 1] = 0; Pp_show_msg( pp, buf ); - if( !ignore_trailing ) show_error( "Trailing data not allowed.", 0, false ); + if( !ignore_trailing ) show_file_error( pp->name, trailing_msg, 0 ); } return ignore_trailing; } static int decompress( const int infd, struct Pretty_print * const pp, - const int buffer_size, + const unsigned buffer_size, const bool ignore_trailing, const bool testing ) { unsigned long long partial_file_pos = 0; @@ -454,25 +474,19 @@ static int decompress( const int infd, struct Pretty_print * const pp, if( !Fh_verify_magic( header ) ) { if( first_member ) - { Pp_show_msg( pp, "Bad magic number (file not in lzip format)." ); - retval = 2; } + { show_file_error( pp->name, bad_magic_msg, 0 ); retval = 2; } else if( !show_trailing_data( header, size, pp, false, ignore_trailing ) ) retval = 2; break; } if( !Fh_verify_version( header ) ) { - if( verbosity >= 0 ) - { Pp_show_msg( pp, 0 ); - fprintf( stderr, "Version %d member format not supported.\n", - Fh_version( header ) ); } + Pp_show_msg( pp, bad_version( Fh_version( header ) ) ); retval = 2; break; } dictionary_size = Fh_get_dictionary_size( header ); - if( dictionary_size < min_dictionary_size || - dictionary_size > max_dictionary_size ) - { Pp_show_msg( pp, "Invalid dictionary size in member header." ); - retval = 2; break; } + if( !isvalid_ds( dictionary_size ) ) + { Pp_show_msg( pp, bad_dict_msg ); retval = 2; break; } if( verbosity >= 2 || ( verbosity == 1 && first_member ) ) { Pp_show_msg( pp, 0 ); show_header( dictionary_size ); } @@ -536,6 +550,16 @@ void show_error( const char * const msg, const int errcode, const bool help ) } +void show_file_error( const char * const filename, const char * const msg, + const int errcode ) + { + if( verbosity < 0 ) return; + fprintf( stderr, "%s: %s: %s", program_name, filename, msg ); + if( errcode > 0 ) fprintf( stderr, ": %s", strerror( errcode ) ); + fputc( '\n', stderr ); + } + + void internal_error( const char * const msg ) { if( verbosity >= 0 ) @@ -546,12 +570,12 @@ void internal_error( const char * const msg ) int main( const int argc, const char * const argv[] ) { - const char * input_filename = ""; const char * default_output_filename = ""; const char ** filenames = 0; int num_filenames = 0; - int buffer_size = max_dictionary_size; + unsigned buffer_size = max_dictionary_size; int infd = -1; + enum Mode program_mode = m_compress; int argind = 0; int retval = 0; int i; @@ -560,7 +584,6 @@ int main( const int argc, const char * const argv[] ) bool ignore_trailing = true; bool keep_input_files = false; bool stdin_used = false; - bool testing = false; bool to_stdout = false; struct Pretty_print pp; @@ -572,6 +595,7 @@ int main( const int argc, const char * const argv[] ) { 'f', "force", ap_no }, { 'h', "help", ap_no }, { 'k', "keep", ap_no }, + { 'l', "list", ap_no }, { 'n', "threads", ap_yes }, { 'o', "output", ap_yes }, { 'q', "quiet", ap_no }, @@ -600,14 +624,15 @@ int main( const int argc, const char * const argv[] ) { case 'a': ignore_trailing = false; break; case 'c': to_stdout = true; break; - case 'd': testing = false; break; + case 'd': set_mode( &program_mode, m_decompress ); break; case 'f': force = true; break; case 'h': show_help(); return 0; case 'k': keep_input_files = true; break; + case 'l': set_mode( &program_mode, m_list ); break; case 'n': break; case 'o': default_output_filename = arg; break; case 'q': verbosity = -1; break; - case 't': testing = true; break; + case 't': set_mode( &program_mode, m_test ); break; case 'u': buffer_size = get_dict_size( arg ); break; case 'v': if( verbosity < 4 ) ++verbosity; break; case 'V': show_version(); return 0; @@ -620,9 +645,6 @@ int main( const int argc, const char * const argv[] ) setmode( STDOUT_FILENO, O_BINARY ); #endif - if( testing ) - outfd = -1; - num_filenames = max( 1, ap_arguments( &parser ) - argind ); filenames = resize_buffer( filenames, num_filenames * sizeof filenames[0] ); filenames[0] = "-"; @@ -633,10 +655,18 @@ int main( const int argc, const char * const argv[] ) if( strcmp( filenames[i], "-" ) != 0 ) filenames_given = true; } + if( program_mode == m_list ) + return list_files( filenames, num_filenames, ignore_trailing ); + + if( program_mode == m_test ) + outfd = -1; + else if( program_mode == m_compress ) + program_mode = m_decompress; /* default mode */ + if( buffer_size < max_dictionary_size ) { bool from_stdin = false; - if( to_stdout || testing ) + if( to_stdout || program_mode == m_test ) { show_error( "'--buffer-size' is incompatible with '--stdout' and '--test'.", 0, false ); return 1; } for( i = 0; i < num_filenames; ++i ) @@ -647,7 +677,7 @@ int main( const int argc, const char * const argv[] ) " with a reduced buffer size.", 0, false ); return 1; } } - if( !to_stdout && !testing && + if( !to_stdout && program_mode != m_test && ( filenames_given || default_output_filename[0] ) ) set_signals(); @@ -656,6 +686,7 @@ int main( const int argc, const char * const argv[] ) output_filename = resize_buffer( output_filename, 1 ); for( i = 0; i < num_filenames; ++i ) { + const char * input_filename = ""; int tmp; struct stat in_stats; const struct stat * in_statsp; @@ -664,16 +695,15 @@ int main( const int argc, const char * const argv[] ) if( !filenames[i][0] || strcmp( filenames[i], "-" ) == 0 ) { if( stdin_used ) continue; else stdin_used = true; - input_filename = ""; infd = STDIN_FILENO; - if( !testing ) + if( program_mode != m_test ) { if( to_stdout || !default_output_filename[0] ) outfd = STDOUT_FILENO; else { output_filename = resize_buffer( output_filename, - strlen( default_output_filename ) + 1 ); + strlen( default_output_filename ) + 1 ); strcpy( output_filename, default_output_filename ); if( !open_outstream( force, true ) ) { @@ -687,9 +717,10 @@ int main( const int argc, const char * const argv[] ) else { input_filename = filenames[i]; - infd = open_instream( input_filename, &in_stats, to_stdout || testing ); + infd = open_instream( input_filename, &in_stats, + to_stdout || program_mode == m_test, false ); if( infd < 0 ) { if( retval < 1 ) retval = 1; continue; } - if( !testing ) + if( program_mode != m_test ) { if( to_stdout ) outfd = STDOUT_FILENO; else @@ -705,10 +736,13 @@ int main( const int argc, const char * const argv[] ) } } + Pp_set_name( &pp, input_filename ); if( isatty( infd ) ) { - show_error( "I won't read compressed data from a terminal.", 0, true ); + show_file_error( pp.name, + "I won't read compressed data from a terminal.", 0 ); if( retval < 1 ) retval = 1; + if( program_mode == m_test ) { close( infd ); infd = -1; continue; } cleanup_and_fail( retval ); } @@ -727,17 +761,16 @@ int main( const int argc, const char * const argv[] ) } in_statsp = input_filename[0] ? &in_stats : 0; - Pp_set_name( &pp, input_filename ); - tmp = decompress( infd, &pp, buffer_size, ignore_trailing, testing ); + tmp = decompress( infd, &pp, buffer_size, ignore_trailing, program_mode == m_test ); if( tmp > retval ) retval = tmp; - if( tmp && !testing ) cleanup_and_fail( retval ); + if( tmp && program_mode != m_test ) cleanup_and_fail( retval ); if( delete_output_on_interrupt ) close_and_set_permissions( in_statsp ); if( input_filename[0] ) { close( infd ); infd = -1; - if( !keep_input_files && !to_stdout && !testing ) + if( !keep_input_files && !to_stdout && program_mode != m_test ) remove( input_filename ); } } |