From f6e0a844e295b58ae820dde5f12c557dc42c3624 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 7 Nov 2015 16:11:56 +0100 Subject: Merging upstream version 0.4. Signed-off-by: Daniel Baumann --- main.cc | 128 +++------------------------------------------------------------- 1 file changed, 5 insertions(+), 123 deletions(-) (limited to 'main.cc') diff --git a/main.cc b/main.cc index f3f12e0..db70e00 100644 --- a/main.cc +++ b/main.cc @@ -1,4 +1,4 @@ -/* Plzip - A parallel version of the lzip data compressor +/* Plzip - A parallel compressor compatible with lzip Copyright (C) 2009 Laszlo Ersek. Copyright (C) 2009, 2010 Antonio Diaz Diaz. @@ -58,8 +58,6 @@ #define ULLONG_MAX 0xFFFFFFFFFFFFFFFFULL #endif -void internal_error( const char * msg ); - namespace { @@ -93,44 +91,10 @@ bool delete_output_on_interrupt = false; pthread_t main_thread; pid_t main_thread_pid; -class Pretty_print - { - const char * const stdin_name; - const unsigned int stdin_name_len; - unsigned int longest_name; - std::string name_; - mutable bool first_post; - -public: - Pretty_print( const std::vector< std::string > & filenames ) - : stdin_name( "(stdin)" ), stdin_name_len( std::strlen( stdin_name ) ), - longest_name( 0 ), first_post( false ) - { - for( unsigned int i = 0; i < filenames.size(); ++i ) - { - const std::string & s = filenames[i]; - const unsigned int len = ( ( s == "-" ) ? stdin_name_len : s.size() ); - if( len > longest_name ) longest_name = len; - } - if( longest_name == 0 ) longest_name = stdin_name_len; - } - - void set_name( const std::string & filename ) - { - if( filename.size() && filename != "-" ) name_ = filename; - else name_ = stdin_name; - first_post = true; - } - - void reset() const throw() { if( name_.size() ) first_post = true; } - const char * name() const throw() { return name_.c_str(); } - void operator()( const char * const msg = 0 ) const throw(); - }; - void show_help() throw() { - std::printf( "%s - A parallel version of the lzip data compressor.\n", Program_name ); + std::printf( "%s - A parallel compressor compatible with lzip.\n", Program_name ); std::printf( "\nUsage: %s [options] [files]\n", invocation_name ); std::printf( "\nOptions:\n" ); std::printf( " -h, --help display this help and exit\n" ); @@ -154,7 +118,7 @@ void show_help() throw() std::printf( " --best alias for -9\n" ); if( verbosity > 0 ) { - std::printf( " -D, --debug= (0-3) print debug statistics to stderr\n" ); + std::printf( " -D, --debug= (0-1) print debug statistics to stderr\n" ); } std::printf( "If no file names are given, %s compresses or decompresses\n", program_name ); std::printf( "from standard input to standard output.\n" ); @@ -170,6 +134,7 @@ void show_version() throw() std::printf( "%s %s\n", Program_name, PROGVERSION ); std::printf( "Copyright (C) 2009 Laszlo Ersek.\n" ); std::printf( "Copyright (C) %s Antonio Diaz Diaz.\n", program_year ); + std::printf( "Using Lzlib %s\n", LZ_version() ); std::printf( "License GPLv3+: GNU GPL version 3 or later \n" ); std::printf( "This is free software: you are free to change and redistribute it.\n" ); std::printf( "There is NO WARRANTY, to the extent permitted by law.\n" ); @@ -413,89 +378,6 @@ void close_and_set_permissions( const struct stat * const in_statsp ) } -int do_decompress( LZ_Decoder * const decoder, const int inhandle, - const Pretty_print & pp, const bool testing ) - { - const int in_buffer_size = 65536, out_buffer_size = 8 * in_buffer_size; - uint8_t in_buffer[in_buffer_size], out_buffer[out_buffer_size]; - - while( true ) - { - int in_size = std::min( LZ_decompress_write_size( decoder ), in_buffer_size ); - if( in_size > 0 ) - { - const int max_in_size = in_size; - in_size = readblock( inhandle, in_buffer, max_in_size ); - if( in_size != max_in_size && errno ) - { pp(); show_error( "read error", errno ); return 1; } - if( in_size == 0 ) LZ_decompress_finish( decoder ); - else if( in_size != LZ_decompress_write( decoder, in_buffer, in_size ) ) - internal_error( "library error (LZ_decompress_write)" ); - } - int out_size = LZ_decompress_read( decoder, out_buffer, out_buffer_size ); -// std::fprintf( stderr, "%5d in_size, %6d out_size.\n", in_size, out_size ); - if( out_size < 0 ) - { - const LZ_Errno lz_errno = LZ_decompress_errno( decoder ); - if( lz_errno == LZ_header_error ) - { - if( LZ_decompress_total_out_size( decoder ) > 0 ) - break; // trailing garbage - pp( "error reading member header" ); - return 1; - } - if( lz_errno == LZ_mem_error ) - { - pp( "not enough memory. Find a machine with more memory" ); - return 1; - } - pp(); - if( lz_errno == LZ_unexpected_eof ) - { - if( verbosity >= 0 ) - std::fprintf( stderr, "file ends unexpectedly at pos %lld\n", - LZ_decompress_total_in_size( decoder ) ); - return 2; - } - if( verbosity >= 0 ) - std::fprintf( stderr, "LZ_decompress_read error: %s.\n", - LZ_strerror( LZ_decompress_errno( decoder ) ) ); - return 1; - } - else if( out_size > 0 && outhandle >= 0 ) - { - const int wr = writeblock( outhandle, out_buffer, out_size ); - if( wr != out_size ) - { pp(); show_error( "write error", errno ); return 1; } - } - if( LZ_decompress_finished( decoder ) == 1 ) break; - if( in_size == 0 && out_size == 0 ) - internal_error( "library error (LZ_decompress_read)" ); - } - if( verbosity >= 1 ) - { if( testing ) std::fprintf( stderr, "ok\n" ); - else std::fprintf( stderr, "done\n" ); } - return 0; - } - - -int decompress( const int inhandle, const Pretty_print & pp, - const bool testing ) - { - LZ_Decoder * const decoder = LZ_decompress_open(); - int retval; - - if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok ) - { - pp( "not enough memory" ); - retval = 1; - } - else retval = do_decompress( decoder, inhandle, pp, testing ); - LZ_decompress_close( decoder ); - return retval; - } - - extern "C" void signal_handler( int sig ) throw() { if( !pthread_equal( pthread_self(), main_thread ) ) @@ -810,7 +692,7 @@ int main( const int argc, const char * argv[] ) encoder_options.match_len_limit, num_workers, num_slots, inhandle, outhandle, debug_level ); else - tmp = decompress( inhandle, pp, program_mode == m_test ); + tmp = decompress( inhandle, outhandle, pp, program_mode == m_test ); if( tmp > retval ) retval = tmp; if( tmp && program_mode != m_test ) cleanup_and_fail( retval ); -- cgit v1.2.3