diff options
author | Daniel Baumann <mail@daniel-baumann.ch> | 2015-11-06 11:35:42 +0000 |
---|---|---|
committer | Daniel Baumann <mail@daniel-baumann.ch> | 2015-11-06 11:35:42 +0000 |
commit | d3e2a87dc1d276ec425b2da35258a3b5ce9a9efc (patch) | |
tree | d71cfedff1229d64bb0152feda77a2be5bde94a3 /main.c | |
parent | Adding upstream version 1.3. (diff) | |
download | clzip-d3e2a87dc1d276ec425b2da35258a3b5ce9a9efc.tar.xz clzip-d3e2a87dc1d276ec425b2da35258a3b5ce9a9efc.zip |
Adding upstream version 1.4.upstream/1.4
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 255 |
1 files changed, 111 insertions, 144 deletions
@@ -1,5 +1,5 @@ /* Clzip - Data compressor based on the LZMA algorithm - Copyright (C) 2010, 2011, 2012 Antonio Diaz Diaz. + Copyright (C) 2010, 2011, 2012, 2013 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 @@ -39,6 +39,7 @@ #include <io.h> #define fchmod(x,y) 0 #define fchown(x,y,z) 0 +#define strtoull strtoul #define SIGHUP SIGTERM #define S_ISSOCK(x) 0 #define S_IRGRP 0 @@ -59,22 +60,10 @@ #error "Environments where CHAR_BIT != 8 are not supported." #endif -#ifndef LLONG_MAX -#define LLONG_MAX 0x7FFFFFFFFFFFFFFFLL -#endif -#ifndef LLONG_MIN -#define LLONG_MIN (-LLONG_MAX - 1LL) -#endif -#ifndef ULLONG_MAX -#define ULLONG_MAX 0xFFFFFFFFFFFFFFFFULL -#endif - -long long int llabs( long long int number ); - const char * const Program_name = "Clzip"; const char * const program_name = "clzip"; -const char * const program_year = "2012"; +const char * const program_year = "2013"; const char * invocation_name = 0; #ifdef O_BINARY @@ -95,6 +84,8 @@ struct Lzma_options }; enum Mode { m_compress, m_decompress, m_test }; +const unsigned long long max_member_size = 0x1000000000000000ULL; +const unsigned long long max_volume_size = 0x7FFFFFFFFFFFFFFFULL; char * output_filename = 0; int outfd = -1; @@ -105,21 +96,7 @@ mode_t outfd_mode = S_IRUSR | S_IWUSR; bool delete_output_on_interrupt = false; -/* assure at least a minimum size for buffer 'buf' */ -static void * resize_buffer( void * buf, const int 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 show_help() +static void show_help( void ) { printf( "%s - Data compressor based on the LZMA algorithm.\n", Program_name ); printf( "\nUsage: %s [options] [files]\n", invocation_name ); @@ -155,7 +132,7 @@ static void show_help() } -static void show_version() +static void show_version( void ) { printf( "%s %s\n", Program_name, PROGVERSION ); printf( "Copyright (C) %s Antonio Diaz Diaz.\n", program_year ); @@ -165,31 +142,32 @@ static void show_version() } -static const char * format_num( long long num ) +void show_header( const File_header header ) { const char * const prefix[8] = { "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" }; - enum { buf_size = 16, factor = 1024 }; - static char buf[buf_size]; - const char *p = ""; + enum { factor = 1024 }; + const char * p = ""; + const char * np = " "; + unsigned num = Fh_get_dictionary_size( header ), i; bool exact = ( num % factor == 0 ); - int i; - for( i = 0; i < 8 && ( llabs( num ) > 9999 || - ( exact && llabs( num ) >= factor ) ); ++i ) - { num /= factor; if( num % factor != 0 ) exact = false; p = prefix[i]; } - snprintf( buf, buf_size, "%lld %s", num, p ); - return buf; + 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, "version %d, dictionary size %s%4u %sB. ", + Fh_version( header ), np, num, p ); } -static long long getnum( const char * const ptr, - const long long llimit, const long long ulimit ) +static unsigned long long getnum( const char * const ptr, + const unsigned long long llimit, + const unsigned long long ulimit ) { - long long result; - char *tail; + unsigned long long result; + char * tail; errno = 0; - result = strtoll( ptr, &tail, 0 ); + result = strtoull( ptr, &tail, 0 ); if( tail == ptr ) { show_error( "Bad or missing numerical argument.", 0, true ); @@ -199,8 +177,7 @@ static long long getnum( const char * const ptr, if( !errno && tail[0] ) { int factor = ( tail[1] == 'i' ) ? 1024 : 1000; - int exponent = 0; - int i; + int exponent = 0, i; bool bad_multiplier = false; switch( tail[0] ) { @@ -225,7 +202,7 @@ static long long getnum( const char * const ptr, } for( i = 0; i < exponent; ++i ) { - if( LLONG_MAX / factor >= llabs( result ) ) result *= factor; + if( ulimit / factor >= result ) result *= factor; else { errno = ERANGE; break; } } } @@ -241,7 +218,7 @@ static long long getnum( const char * const ptr, static int get_dict_size( const char * const arg ) { - char *tail; + char * tail; int bits = strtol( arg, &tail, 0 ); if( bits >= min_dictionary_bits && bits <= max_dictionary_bits && *tail == 0 ) @@ -250,6 +227,20 @@ static int get_dict_size( const char * const arg ) } +static int extension_index( const char * const name ) + { + int i; + for( i = 0; known_extensions[i].from; ++i ) + { + const char * const ext = known_extensions[i].from; + if( strlen( name ) > strlen( ext ) && + strncmp( name + strlen( name ) - strlen( ext ), ext, strlen( ext ) ) == 0 ) + return i; + } + return -1; + } + + static int open_instream( const char * const name, struct stat * const in_statsp, const enum Mode program_mode, const int eindex, const bool recompress, const bool to_stdout ) @@ -293,6 +284,20 @@ 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 ) + { + 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_c_outname( const char * const name, const bool multifile ) { output_filename = resize_buffer( output_filename, strlen( name ) + 5 + @@ -303,20 +308,6 @@ static void set_c_outname( const char * const name, const bool multifile ) } -static int extension_index( const char * const name ) - { - int i; - for( i = 0; known_extensions[i].from; ++i ) - { - const char * const ext = known_extensions[i].from; - if( strlen( name ) > strlen( ext ) && - strncmp( name + strlen( name ) - strlen( ext ), ext, strlen( ext ) ) == 0 ) - return i; - } - return -1; - } - - static void set_d_outname( const char * const name, const int i ) { if( i >= 0 ) @@ -419,9 +410,9 @@ static void close_and_set_permissions( const struct stat * const in_statsp ) } -static bool next_filename() +static bool next_filename( void ) { - const unsigned int len = strlen( known_extensions[0].from ); + const unsigned len = strlen( known_extensions[0].from ); int i, j; if( strlen( output_filename ) >= len + 5 ) /* "*00001.lz" */ @@ -434,18 +425,19 @@ static bool next_filename() } -static int compress( const long long member_size, const long long volume_size, +static int compress( const unsigned long long member_size, + const unsigned long long volume_size, const struct Lzma_options * const encoder_options, const int infd, struct Pretty_print * const pp, const struct stat * const in_statsp ) { - long long in_size = 0, out_size = 0, partial_volume_size = 0; + unsigned long long in_size = 0, out_size = 0, partial_volume_size = 0; int retval = 0; - File_header header; struct Matchfinder matchfinder; + File_header header; + Fh_set_magic( header ); if( verbosity >= 1 ) Pp_show_msg( pp, 0 ); - Fh_set_magic( header ); if( !Fh_set_dictionary_size( header, encoder_options->dictionary_size ) || encoder_options->match_len_limit < min_match_len_limit || encoder_options->match_len_limit > max_match_len ) @@ -459,11 +451,11 @@ static int compress( const long long member_size, const long long volume_size, } Fh_set_dictionary_size( header, matchfinder.dictionary_size ); - while( true ) /* encode one member per iteration */ + while( true ) /* encode one member per iteration */ { struct LZ_encoder encoder; - const long long size = - min( member_size, volume_size - partial_volume_size ); + const unsigned long long size = ( ( volume_size > 0 ) ? + min( member_size, volume_size - partial_volume_size ) : member_size ); if( !LZe_init( &encoder, &matchfinder, header, outfd ) ) { show_error( "Not enough memory. Try a smaller dictionary size.", 0, false ); @@ -473,19 +465,22 @@ static int compress( const long long member_size, const long long volume_size, { Pp_show_msg( pp, "Encoder error" ); retval = 1; break; } in_size += Mf_data_position( &matchfinder ); out_size += Re_member_position( &encoder.range_encoder ); - partial_volume_size += Re_member_position( &encoder.range_encoder ); LZe_free( &encoder ); if( Mf_finished( &matchfinder ) ) break; - if( partial_volume_size >= volume_size - min_dictionary_size ) + if( volume_size > 0 ) { - partial_volume_size = 0; - if( delete_output_on_interrupt ) + partial_volume_size += Re_member_position( &encoder.range_encoder ); + if( partial_volume_size >= volume_size - min_dictionary_size ) { - close_and_set_permissions( in_statsp ); - if( !next_filename() ) - { Pp_show_msg( pp, "Too many volume files" ); retval = 1; break; } - if( !open_outstream( true ) ) { retval = 1; break; } - delete_output_on_interrupt = true; + partial_volume_size = 0; + if( delete_output_on_interrupt ) + { + close_and_set_permissions( in_statsp ); + if( !next_filename() ) + { Pp_show_msg( pp, "Too many volume files" ); retval = 1; break; } + if( !open_outstream( true ) ) { retval = 1; break; } + delete_output_on_interrupt = true; + } } } Mf_reset( &matchfinder ); @@ -493,11 +488,11 @@ static int compress( const long long member_size, const long long volume_size, if( retval == 0 && verbosity >= 1 ) { - if( in_size <= 0 || out_size <= 0 ) + if( in_size == 0 || out_size == 0 ) fprintf( stderr, " no data compressed.\n" ); else fprintf( stderr, "%6.3f:1, %6.3f bits/byte, " - "%5.2f%% saved, %lld in, %lld out.\n", + "%5.2f%% saved, %llu in, %llu out.\n", (double)in_size / out_size, ( 8.0 * out_size ) / in_size, 100.0 * ( 1.0 - ( (double)out_size / in_size ) ), @@ -511,9 +506,9 @@ static int compress( const long long member_size, const long long volume_size, static int decompress( const int infd, struct Pretty_print * const pp, const bool testing ) { - long long partial_file_pos = 0; + unsigned long long partial_file_pos = 0; struct Range_decoder rdec; - int retval = 0, result; + int retval = 0; bool first_member; if( !Rd_init( &rdec, infd ) ) { @@ -521,13 +516,14 @@ static int decompress( const int infd, struct Pretty_print * const pp, cleanup_and_fail( 1 ); } - for( first_member = true; ; first_member = false, Pp_reset( pp ) ) + for( first_member = true; ; first_member = false ) { + int result; File_header header; struct LZ_decoder decoder; Rd_reset_member_position( &rdec ); Rd_read_data( &rdec, header, Fh_size ); - if( Rd_finished( &rdec ) ) /* End Of File */ + if( Rd_finished( &rdec ) ) /* End Of File */ { if( first_member ) { Pp_show_msg( pp, "Error reading member header" ); retval = 1; } @@ -553,13 +549,7 @@ static int decompress( const int infd, struct Pretty_print * const pp, retval = 2; break; } if( verbosity >= 2 || ( verbosity == 1 && first_member ) ) - { - Pp_show_msg( pp, 0 ); - if( verbosity >= 2 ) - fprintf( stderr, "version %d, dictionary size %7sB. ", - Fh_version( header ), - format_num( Fh_get_dictionary_size( header ) ) ); - } + { Pp_show_msg( pp, 0 ); if( verbosity >= 2 ) show_header( header ); } if( !LZd_init( &decoder, header, &rdec, outfd ) ) { @@ -575,16 +565,16 @@ static int decompress( const int infd, struct Pretty_print * const pp, { Pp_show_msg( pp, 0 ); if( result == 2 ) - fprintf( stderr, "File ends unexpectedly at pos %lld\n", + fprintf( stderr, "File ends unexpectedly at pos %llu\n", partial_file_pos ); else - fprintf( stderr, "Decoder error at pos %lld\n", partial_file_pos ); + fprintf( stderr, "Decoder error at pos %llu\n", partial_file_pos ); } retval = 2; break; } if( verbosity >= 2 ) { if( testing ) fprintf( stderr, "ok\n" ); - else fprintf( stderr, "done\n" ); } + else fprintf( stderr, "done\n" ); Pp_reset( pp ); } } Rd_free( &rdec ); if( verbosity == 1 && retval == 0 ) @@ -596,13 +586,13 @@ static int decompress( const int infd, struct Pretty_print * const pp, void signal_handler( int sig ) { - sig = 0; /* keep compiler happy */ + if( sig ) {} /* keep compiler happy */ show_error( "Control-C or similar caught, quitting.", 0, false ); cleanup_and_fail( 1 ); } -static void set_signals() +static void set_signals( void ) { signal( SIGHUP, signal_handler ); signal( SIGINT, signal_handler ); @@ -613,7 +603,7 @@ static void set_signals() void Pp_init( struct Pretty_print * const pp, const char * const filenames[], const int num_filenames, const int v ) { - unsigned int stdin_name_len; + unsigned stdin_name_len; int i; pp->name = 0; pp->stdin_name = "(stdin)"; @@ -625,31 +615,13 @@ void Pp_init( struct Pretty_print * const pp, const char * const filenames[], for( i = 0; i < num_filenames; ++i ) { const char * const s = filenames[i]; - const int len = ( !strcmp( s, "-" ) ? stdin_name_len : strlen( s ) ); + const int len = ( (strcmp( s, "-" ) == 0) ? stdin_name_len : strlen( s ) ); if( len > pp->longest_name ) pp->longest_name = len; } if( pp->longest_name == 0 ) pp->longest_name = stdin_name_len; } -void Pp_show_msg( struct Pretty_print * const pp, const char * const msg ) - { - if( verbosity >= 0 ) - { - if( pp->first_post ) - { - int i, len; - pp->first_post = false; - fprintf( stderr, " %s: ", pp->name ); - len = pp->longest_name - strlen( pp->name ); - for( i = 0; i < len; ++i ) fprintf( stderr, " " ); - if( !msg ) fflush( stderr ); - } - if( msg ) fprintf( stderr, "%s.\n", msg ); - } - } - - void show_error( const char * const msg, const int errcode, const bool help ) { if( verbosity >= 0 ) @@ -660,7 +632,7 @@ void show_error( const char * const msg, const int errcode, const bool help ) if( errcode > 0 ) fprintf( stderr, ": %s", strerror( errcode ) ); fprintf( stderr, "\n" ); } - if( help && invocation_name && invocation_name[0] ) + if( help ) fprintf( stderr, "Try '%s --help' for more information.\n", invocation_name ); } @@ -692,8 +664,8 @@ int main( const int argc, const char * const argv[] ) { 3 << 23, 132 }, /* -8 */ { 1 << 25, 273 } }; /* -9 */ struct Lzma_options encoder_options = option_mapping[6]; /* default = "-6" */ - long long member_size = LLONG_MAX; - long long volume_size = LLONG_MAX; + unsigned long long member_size = max_member_size; + unsigned long long volume_size = 0; const char * input_filename = ""; const char * default_output_filename = ""; const char ** filenames = 0; @@ -753,14 +725,14 @@ int main( const int argc, const char * const argv[] ) { const int code = ap_code( &parser, argind ); const char * const arg = ap_argument( &parser, argind ); - if( !code ) break; /* no more options */ + if( !code ) break; /* no more options */ switch( code ) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': encoder_options = option_mapping[code-'0']; break; - case 'b': member_size = getnum( arg, 100000, LLONG_MAX / 2 ); break; + case 'b': member_size = getnum( arg, 100000, max_member_size ); break; case 'c': to_stdout = true; break; case 'd': program_mode = m_decompress; break; case 'f': force = true; break; @@ -773,7 +745,7 @@ int main( const int argc, const char * const argv[] ) case 'q': verbosity = -1; break; case 's': encoder_options.dictionary_size = get_dict_size( arg ); break; - case 'S': volume_size = getnum( arg, 100000, LLONG_MAX / 2 ); break; + case 'S': volume_size = getnum( arg, 100000, max_volume_size ); break; case 't': program_mode = m_test; break; case 'v': if( verbosity < 4 ) ++verbosity; break; case 'V': show_version(); return 0; @@ -782,8 +754,8 @@ int main( const int argc, const char * const argv[] ) } /* end process options */ #if defined(__MSVCRT__) || defined(__OS2__) - _fsetmode( stdin, "b" ); - _fsetmode( stdout, "b" ); + setmode( STDIN_FILENO, O_BINARY ); + setmode( STDOUT_FILENO, O_BINARY ); #endif if( program_mode == m_test ) @@ -794,21 +766,16 @@ int main( const int argc, const char * const argv[] ) Prob_prices_init(); } - for( ; argind < ap_arguments( &parser ); ++argind ) - { - if( strcmp( ap_argument( &parser, argind ), "-" ) ) - filenames_given = true; - ++num_filenames; - filenames = resize_buffer( filenames, num_filenames * sizeof (char *) ); - filenames[num_filenames-1] = ap_argument( &parser, argind ); - } + num_filenames = max( 1, ap_arguments( &parser ) - argind ); + filenames = resize_buffer( filenames, num_filenames * sizeof filenames[0] ); + filenames[0] = "-"; - if( num_filenames == 0 ) + for( i = 0; argind + i < ap_arguments( &parser ); ++i ) { - ++num_filenames; - filenames = resize_buffer( filenames, sizeof (char *) ); - filenames[num_filenames-1] = "-"; + filenames[i] = ap_argument( &parser, argind + i ); + if( strcmp( filenames[i], "-" ) != 0 ) filenames_given = true; } + if( !to_stdout && program_mode != m_test && ( filenames_given || default_output_filename[0] ) ) set_signals(); @@ -823,7 +790,7 @@ int main( const int argc, const char * const argv[] ) const struct stat * in_statsp; output_filename[0] = 0; - if( !filenames[i][0] || !strcmp( filenames[i], "-" ) ) + if( !filenames[i][0] || strcmp( filenames[i], "-" ) == 0 ) { input_filename = ""; infd = STDIN_FILENO; @@ -834,7 +801,7 @@ int main( const int argc, const char * const argv[] ) else { if( program_mode == m_compress ) - set_c_outname( default_output_filename, volume_size != LLONG_MAX ); + set_c_outname( default_output_filename, volume_size > 0 ); else { output_filename = resize_buffer( output_filename, @@ -844,7 +811,7 @@ int main( const int argc, const char * const argv[] ) outfd_mode = all_rw; if( !open_outstream( force ) ) { - if( outfd == -1 && retval < 1 ) retval = 1; + if( retval < 1 ) retval = 1; close( infd ); infd = -1; continue; } @@ -864,12 +831,12 @@ int main( const int argc, const char * const argv[] ) else { if( program_mode == m_compress ) - set_c_outname( input_filename, volume_size != LLONG_MAX ); + set_c_outname( input_filename, volume_size > 0 ); else set_d_outname( input_filename, eindex ); outfd_mode = usr_rw; if( !open_outstream( force ) ) { - if( outfd == -1 && retval < 1 ) retval = 1; + if( retval < 1 ) retval = 1; close( infd ); infd = -1; continue; } |