summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--main.c239
1 files changed, 154 insertions, 85 deletions
diff --git a/main.c b/main.c
index 47a72d6..9a14137 100644
--- a/main.c
+++ b/main.c
@@ -1,5 +1,5 @@
/* Lunzip - Decompressor for the lzip format
- Copyright (C) 2010-2017 Antonio Diaz Diaz.
+ Copyright (C) 2010-2018 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,7 +67,7 @@ int verbosity = 0;
const char * const Program_name = "Lunzip";
const char * const program_name = "lunzip";
-const char * const program_year = "2017";
+const char * const program_year = "2018";
const char * invocation_name = 0;
const struct { const char * from; const char * to; } known_extensions[] = {
@@ -113,6 +113,7 @@ static void show_help( void )
" -t, --test test compressed file integrity\n"
" -u, --buffer-size=<bytes> set output buffer size in bytes\n"
" -v, --verbose be verbose (a 2nd -v gives more)\n"
+ " --loose-trailing allow trailing data seeming corrupt header\n"
"If no file names are given, or if a file is '-', lunzip decompresses\n"
"from standard input to standard output.\n"
"Numbers may be followed by a multiplier: k = kB = 10^3 = 1000,\n"
@@ -138,6 +139,35 @@ static void show_version( void )
}
+/* assure at least a minimum size for buffer 'buf' */
+void * resize_buffer( void * buf, const unsigned min_size )
+ {
+ if( buf ) buf = realloc( buf, min_size );
+ else buf = malloc( min_size );
+ if( !buf )
+ {
+ show_error( "Not enough memory.", 0, false );
+ cleanup_and_fail( 1 );
+ }
+ return buf;
+ }
+
+
+void Pp_show_msg( struct Pretty_print * const pp, const char * const msg )
+ {
+ if( verbosity >= 0 )
+ {
+ if( pp->first_post )
+ {
+ pp->first_post = false;
+ fputs( pp->padded_name, stderr );
+ if( !msg ) fflush( stderr );
+ }
+ if( msg ) fprintf( stderr, "%s\n", msg );
+ }
+ }
+
+
const char * bad_version( const unsigned version )
{
static char buf[80];
@@ -155,10 +185,10 @@ const char * format_ds( const unsigned dictionary_size )
{ "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" };
const char * p = "";
const char * np = " ";
- unsigned num = dictionary_size, i;
+ unsigned num = dictionary_size;
bool exact = ( num % factor == 0 );
- for( i = 0; i < 8 && ( num > 9999 || ( exact && num >= factor ) ); ++i )
+ int i; 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 );
@@ -166,10 +196,9 @@ const char * format_ds( const unsigned dictionary_size )
}
-static void show_header( const unsigned dictionary_size )
+void show_header( const unsigned dictionary_size )
{
- if( verbosity >= 3 )
- fprintf( stderr, "dictionary %s. ", format_ds( dictionary_size ) );
+ fprintf( stderr, "dictionary %s, ", format_ds( dictionary_size ) );
}
@@ -263,6 +292,31 @@ static int extension_index( const char * const name )
}
+static void set_d_outname( const char * const name, const int eindex )
+ {
+ const unsigned name_len = strlen( name );
+ if( eindex >= 0 )
+ {
+ 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[eindex].to ) + 1 );
+ strcpy( output_filename, name );
+ strcpy( output_filename + name_len - from_len, known_extensions[eindex].to );
+ return;
+ }
+ }
+ output_filename = resize_buffer( output_filename, name_len + 4 + 1 );
+ strcpy( output_filename, name );
+ strcat( output_filename, ".out" );
+ if( verbosity >= 1 )
+ fprintf( stderr, "%s: Can't guess original name for '%s' -- using '%s'\n",
+ program_name, name, output_filename );
+ }
+
+
int open_instream( const char * const name, struct stat * const in_statsp,
const bool no_ofile, const bool reg_only )
{
@@ -291,45 +345,6 @@ int open_instream( const char * const name, struct stat * const in_statsp,
}
-/* assure at least a minimum size for buffer 'buf' */
-void * resize_buffer( void * buf, const unsigned min_size )
- {
- if( buf ) buf = realloc( buf, min_size );
- else buf = malloc( min_size );
- if( !buf )
- {
- show_error( "Not enough memory.", 0, false );
- cleanup_and_fail( 1 );
- }
- return buf;
- }
-
-
-static void set_d_outname( const char * const name, const int eindex )
- {
- const unsigned name_len = strlen( name );
- if( eindex >= 0 )
- {
- 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[eindex].to ) + 1 );
- strcpy( output_filename, name );
- strcpy( output_filename + name_len - from_len, known_extensions[eindex].to );
- return;
- }
- }
- output_filename = resize_buffer( output_filename, name_len + 4 + 1 );
- strcpy( output_filename, name );
- strcat( output_filename, ".out" );
- if( verbosity >= 1 )
- fprintf( stderr, "%s: Can't guess original name for '%s' -- using '%s'\n",
- program_name, name, output_filename );
- }
-
-
static bool open_outstream( const bool force, const bool from_stdin )
{
const mode_t usr_rw = S_IRUSR | S_IWUSR;
@@ -413,9 +428,9 @@ static unsigned char xdigit( const unsigned value )
static bool show_trailing_data( const uint8_t * const data, const int size,
struct Pretty_print * const pp, const bool all,
- const bool ignore_trailing )
+ const int ignore_trailing ) /* -1 = show */
{
- if( verbosity >= 4 || !ignore_trailing )
+ if( verbosity >= 4 || ignore_trailing <= 0 )
{
int i;
char buf[80];
@@ -433,15 +448,16 @@ static bool show_trailing_data( const uint8_t * const data, const int size,
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_file_error( pp->name, trailing_msg, 0 );
+ if( ignore_trailing == 0 ) show_file_error( pp->name, trailing_msg, 0 );
}
- return ignore_trailing;
+ return ( ignore_trailing > 0 );
}
-static int decompress( const int infd, struct Pretty_print * const pp,
- const unsigned buffer_size,
- const bool ignore_trailing, const bool testing )
+static int decompress( const unsigned long long cfile_size, const int infd,
+ struct Pretty_print * const pp, const unsigned buffer_size,
+ const bool ignore_trailing, const bool loose_trailing,
+ const bool testing )
{
unsigned long long partial_file_pos = 0;
struct Range_decoder rdec;
@@ -463,8 +479,12 @@ static int decompress( const int infd, struct Pretty_print * const pp,
size = Rd_read_data( &rdec, header, Fh_size );
if( Rd_finished( &rdec ) ) /* End Of File */
{
- if( first_member || Fh_verify_prefix( header, size ) )
- { Pp_show_msg( pp, "File ends unexpectedly at member header." );
+ if( first_member )
+ { show_file_error( pp->name, "File ends unexpectedly at member header.", 0 );
+ retval = 2; }
+ else if( Fh_verify_prefix( header, size ) )
+ { Pp_show_msg( pp, "Truncated header in multimember file." );
+ show_trailing_data( header, size, pp, true, -1 );
retval = 2; }
else if( size > 0 && !show_trailing_data( header, size, pp,
true, ignore_trailing ) )
@@ -475,27 +495,30 @@ static int decompress( const int infd, struct Pretty_print * const pp,
{
if( first_member )
{ show_file_error( pp->name, bad_magic_msg, 0 ); retval = 2; }
+ else if( !loose_trailing && Fh_verify_corrupt( header ) )
+ { Pp_show_msg( pp, corrupt_mm_msg );
+ show_trailing_data( header, size, pp, false, -1 );
+ retval = 2; }
else if( !show_trailing_data( header, size, pp, false, ignore_trailing ) )
retval = 2;
break;
}
if( !Fh_verify_version( header ) )
- {
- Pp_show_msg( pp, bad_version( Fh_version( header ) ) );
- retval = 2; break;
- }
+ { Pp_show_msg( pp, bad_version( Fh_version( header ) ) );
+ retval = 2; break; }
dictionary_size = Fh_get_dictionary_size( header );
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 ); }
+ Pp_show_msg( pp, 0 );
if( !LZd_init( &decoder, &rdec, buffer_size, dictionary_size, outfd ) )
{
Pp_show_msg( pp, "Not enough memory. Try a smaller output buffer size." );
retval = 1; break;
}
+ show_dprogress( cfile_size, partial_file_pos, &rdec, pp ); /* init */
result = LZd_decode_member( &decoder, pp );
partial_file_pos += Rd_member_position( &rdec );
LZd_free( &decoder );
@@ -568,42 +591,76 @@ void internal_error( const char * const msg )
}
+void show_dprogress( const unsigned long long cfile_size,
+ const unsigned long long partial_size,
+ const struct Range_decoder * const d,
+ struct Pretty_print * const p )
+ {
+ static unsigned long long csize = 0; /* file_size / 100 */
+ static unsigned long long psize = 0;
+ static const struct Range_decoder * rdec = 0;
+ static struct Pretty_print * pp = 0;
+ static int counter = 0;
+ static bool enabled = true;
+
+ if( !enabled ) return;
+ if( p ) /* initialize static vars */
+ {
+ if( verbosity < 2 || !isatty( STDERR_FILENO ) ) { enabled = false; return; }
+ csize = cfile_size; psize = partial_size; rdec = d; pp = p; counter = 0;
+ }
+ if( rdec && pp && --counter <= 0 )
+ {
+ const unsigned long long pos = psize + Rd_member_position( rdec );
+ counter = 7; /* update display every 114688 bytes */
+ if( csize > 0 )
+ fprintf( stderr, "%4llu%% %.1f MB\r", pos / csize, pos / 1000000.0 );
+ else
+ fprintf( stderr, " %.1f MB\r", pos / 1000000.0 );
+ Pp_reset( pp ); Pp_show_msg( pp, 0 ); /* restore cursor position */
+ }
+ }
+
+
int main( const int argc, const char * const argv[] )
{
const char * default_output_filename = "";
const char ** filenames = 0;
int num_filenames = 0;
unsigned buffer_size = max_dictionary_size;
- int infd = -1;
enum Mode program_mode = m_compress;
int argind = 0;
+ int failed_tests = 0;
int retval = 0;
int i;
bool filenames_given = false;
bool force = false;
bool ignore_trailing = true;
bool keep_input_files = false;
+ bool loose_trailing = false;
bool stdin_used = false;
bool to_stdout = false;
struct Pretty_print pp;
+ enum { opt_lt = 256 };
const struct ap_Option options[] =
{
- { 'a', "trailing-error", ap_no },
- { 'c', "stdout", ap_no },
- { 'd', "decompress", ap_no },
- { '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 },
- { 't', "test", ap_no },
- { 'u', "buffer-size", ap_yes },
- { 'v', "verbose", ap_no },
- { 'V', "version", ap_no },
- { 0 , 0, ap_no } };
+ { 'a', "trailing-error", ap_no },
+ { 'c', "stdout", ap_no },
+ { 'd', "decompress", ap_no },
+ { '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 },
+ { 't', "test", ap_no },
+ { 'u', "buffer-size", ap_yes },
+ { 'v', "verbose", ap_no },
+ { 'V', "version", ap_no },
+ { opt_lt, "loose-trailing", ap_no },
+ { 0 , 0, ap_no } };
struct Arg_parser parser;
@@ -636,6 +693,7 @@ int main( const int argc, const char * const argv[] )
case 'u': buffer_size = get_dict_size( arg ); break;
case 'v': if( verbosity < 4 ) ++verbosity; break;
case 'V': show_version(); return 0;
+ case opt_lt: loose_trailing = true; break;
default : internal_error( "uncaught option." );
}
} /* end process options */
@@ -656,7 +714,7 @@ int main( const int argc, const char * const argv[] )
}
if( program_mode == m_list )
- return list_files( filenames, num_filenames, ignore_trailing );
+ return list_files( filenames, num_filenames, ignore_trailing, loose_trailing );
if( program_mode == m_test )
outfd = -1;
@@ -681,12 +739,14 @@ int main( const int argc, const char * const argv[] )
( filenames_given || default_output_filename[0] ) )
set_signals();
- Pp_init( &pp, filenames, num_filenames, verbosity );
+ Pp_init( &pp, filenames, num_filenames );
output_filename = resize_buffer( output_filename, 1 );
for( i = 0; i < num_filenames; ++i )
{
+ unsigned long long cfile_size;
const char * input_filename = "";
+ int infd;
int tmp;
struct stat in_stats;
const struct stat * in_statsp;
@@ -708,7 +768,7 @@ int main( const int argc, const char * const argv[] )
if( !open_outstream( force, true ) )
{
if( retval < 1 ) retval = 1;
- close( infd ); infd = -1;
+ close( infd );
continue;
}
}
@@ -729,7 +789,7 @@ int main( const int argc, const char * const argv[] )
if( !open_outstream( force, false ) )
{
if( retval < 1 ) retval = 1;
- close( infd ); infd = -1;
+ close( infd );
continue;
}
}
@@ -742,7 +802,7 @@ int main( const int argc, const char * const argv[] )
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; }
+ if( program_mode == m_test ) { close( infd ); continue; }
cleanup_and_fail( retval );
}
@@ -761,24 +821,33 @@ int main( const int argc, const char * const argv[] )
}
in_statsp = input_filename[0] ? &in_stats : 0;
- tmp = decompress( infd, &pp, buffer_size, ignore_trailing, program_mode == m_test );
+ cfile_size = ( in_statsp && S_ISREG( in_statsp->st_mode ) ) ?
+ ( in_statsp->st_size + 99 ) / 100 : 0;
+ tmp = decompress( cfile_size, infd, &pp, buffer_size, ignore_trailing,
+ loose_trailing, program_mode == m_test );
if( tmp > retval ) retval = tmp;
- if( tmp && program_mode != m_test ) cleanup_and_fail( retval );
+ if( tmp )
+ { if( program_mode != m_test ) cleanup_and_fail( retval );
+ else ++failed_tests; }
if( delete_output_on_interrupt )
close_and_set_permissions( in_statsp );
if( input_filename[0] )
{
- close( infd ); infd = -1;
+ close( infd );
if( !keep_input_files && !to_stdout && program_mode != m_test )
remove( input_filename );
}
}
if( outfd >= 0 && close( outfd ) != 0 )
{
- show_error( "Can't close stdout", errno, false );
+ show_error( "Error closing stdout", errno, false );
if( retval < 1 ) retval = 1;
}
+ if( failed_tests > 0 && verbosity >= 1 && num_filenames > 1 )
+ fprintf( stderr, "%s: warning: %d %s failed the test.\n",
+ program_name, failed_tests,
+ ( failed_tests == 1 ) ? "file" : "files" );
free( output_filename );
free( filenames );
ap_free( &parser );