diff options
author | Daniel Baumann <mail@daniel-baumann.ch> | 2015-11-07 10:09:39 +0000 |
---|---|---|
committer | Daniel Baumann <mail@daniel-baumann.ch> | 2015-11-07 10:09:39 +0000 |
commit | e0f3176a18dc8b14ca249e80f84bc0c36b1d5579 (patch) | |
tree | ca01d00df2874401078a37d493dfdf587ad9c514 /main.cc | |
parent | Adding debian version 1.17-1. (diff) | |
download | lzip-e0f3176a18dc8b14ca249e80f84bc0c36b1d5579.tar.xz lzip-e0f3176a18dc8b14ca249e80f84bc0c36b1d5579.zip |
Merging upstream version 1.18~pre1.
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to 'main.cc')
-rw-r--r-- | main.cc | 68 |
1 files changed, 40 insertions, 28 deletions
@@ -24,6 +24,7 @@ #define _FILE_OFFSET_BITS 64 #include <algorithm> +#include <cctype> #include <cerrno> #include <climits> #include <csignal> @@ -104,6 +105,7 @@ void show_help() std::printf( "\nOptions:\n" " -h, --help display this help and exit\n" " -V, --version output version information and exit\n" + " -a, --trailing-error exit with error status if trailing data\n" " -b, --member-size=<bytes> set member size limit in bytes\n" " -c, --stdout send output to standard output\n" " -d, --decompress decompress\n" @@ -275,7 +277,7 @@ int open_instream( const char * const name, struct stat * const in_statsp, const bool can_read = ( i == 0 && ( S_ISBLK( mode ) || S_ISCHR( mode ) || S_ISFIFO( mode ) || S_ISSOCK( mode ) ) ); - const bool no_ofile = to_stdout || program_mode == m_test; + const bool no_ofile = ( to_stdout || program_mode == m_test ); if( i != 0 || ( !S_ISREG( mode ) && ( !can_read || !no_ofile ) ) ) { if( verbosity >= 0 ) @@ -501,36 +503,42 @@ unsigned char xdigit( const int value ) } -void show_trailing_garbage( const uint8_t * const data, const int size, - const Pretty_print & pp, const bool all ) +bool show_trailing_data( const uint8_t * const data, const int size, + const Pretty_print & pp, const bool all, + const bool ignore_garbage ) { - std::string garbage_msg; - if( !all ) garbage_msg = "first bytes of "; - garbage_msg += "trailing garbage found = "; - bool text = true; - for( int i = 0; i < size; ++i ) - if( !std::isprint( data[i] ) ) { text = false; break; } - if( text ) - { - garbage_msg += '\''; - garbage_msg.append( (const char *)data, size ); - garbage_msg += '\''; - } - else + if( verbosity >= 4 || !ignore_garbage ) { + std::string msg; + if( !all ) msg = "first bytes of "; + msg += "trailing data = "; + bool text = true; for( int i = 0; i < size; ++i ) + if( !std::isprint( data[i] ) ) { text = false; break; } + if( text ) + { + msg += '\''; + msg.append( (const char *)data, size ); + msg += '\''; + } + else { - if( i > 0 ) garbage_msg += ' '; - garbage_msg += xdigit( data[i] >> 4 ); - garbage_msg += xdigit( data[i] & 0x0F ); + for( int i = 0; i < size; ++i ) + { + if( i > 0 ) msg += ' '; + msg += xdigit( data[i] >> 4 ); + msg += xdigit( data[i] & 0x0F ); + } } + pp( msg.c_str() ); + if( !ignore_garbage ) show_error( "Trailing data not allowed." ); } - garbage_msg += '.'; - pp( garbage_msg.c_str() ); + return ignore_garbage; } -int decompress( const int infd, const Pretty_print & pp, const bool testing ) +int decompress( const int infd, const Pretty_print & pp, + const bool ignore_garbage, const bool testing ) { int retval = 0; @@ -546,16 +554,17 @@ int decompress( const int infd, const Pretty_print & pp, const bool testing ) { if( first_member ) { pp( "File ends unexpectedly at member header." ); retval = 2; } - else if( verbosity >= 4 && size > 0 ) - show_trailing_garbage( header.data, size, pp, true ); + else if( size > 0 && !show_trailing_data( header.data, size, pp, + true, ignore_garbage ) ) + retval = 2; break; } if( !header.verify_magic() ) { if( first_member ) { pp( "Bad magic number (file not in lzip format)." ); retval = 2; } - else if( verbosity >= 4 ) - show_trailing_garbage( header.data, size, pp, false ); + else if( !show_trailing_data( header.data, size, pp, false, ignore_garbage ) ) + retval = 2; break; } if( !header.verify_version() ) @@ -699,6 +708,7 @@ int main( const int argc, const char * const argv[] ) int infd = -1; Mode program_mode = m_compress; bool force = false; + bool ignore_garbage = true; bool keep_input_files = false; bool recompress = false; bool to_stdout = false; @@ -717,6 +727,7 @@ int main( const int argc, const char * const argv[] ) { '7', 0, Arg_parser::no }, { '8', 0, Arg_parser::no }, { '9', "best", Arg_parser::no }, + { 'a', "trailing-error", Arg_parser::no }, { 'b', "member-size", Arg_parser::yes }, { 'c', "stdout", Arg_parser::no }, { 'd', "decompress", Arg_parser::no }, @@ -751,6 +762,7 @@ int main( const int argc, const char * const argv[] ) case '5': case '6': case '7': case '8': case '9': zero = ( code == '0' ); encoder_options = option_mapping[code-'0']; break; + case 'a': ignore_garbage = false; break; case 'b': member_size = getnum( arg.c_str(), 100000, max_member_size ); break; case 'c': to_stdout = true; break; @@ -801,7 +813,7 @@ int main( const int argc, const char * const argv[] ) ( filenames_given || default_output_filename.size() ) ) set_signals(); - Pretty_print pp( filenames ); + Pretty_print pp( filenames, verbosity ); int retval = 0; for( unsigned i = 0; i < filenames.size(); ++i ) @@ -869,7 +881,7 @@ int main( const int argc, const char * const argv[] ) tmp = compress( member_size, volume_size, infd, encoder_options, pp, in_statsp, zero ); else - tmp = decompress( infd, pp, program_mode == m_test ); + tmp = decompress( infd, pp, ignore_garbage, program_mode == m_test ); if( tmp > retval ) retval = tmp; if( tmp && program_mode != m_test ) cleanup_and_fail( retval ); |