diff options
author | Daniel Baumann <mail@daniel-baumann.ch> | 2015-11-06 11:33:36 +0000 |
---|---|---|
committer | Daniel Baumann <mail@daniel-baumann.ch> | 2015-11-06 11:33:36 +0000 |
commit | 8b42f93e9b42a93caddaa897a237acb5acbf3231 (patch) | |
tree | 0385d43238b978b7be02fb442fb64fadc92ad7dc /main.c | |
parent | Adding upstream version 1.2. (diff) | |
download | clzip-8b42f93e9b42a93caddaa897a237acb5acbf3231.tar.xz clzip-8b42f93e9b42a93caddaa897a237acb5acbf3231.zip |
Adding upstream version 1.3.upstream/1.3
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to '')
-rw-r--r-- | main.c | 189 |
1 files changed, 103 insertions, 86 deletions
@@ -1,5 +1,5 @@ /* Clzip - Data compressor based on the LZMA algorithm - Copyright (C) 2010, 2011 Antonio Diaz Diaz. + Copyright (C) 2010, 2011, 2012 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 @@ -24,14 +24,14 @@ #define _FILE_OFFSET_BITS 64 #include <errno.h> +#include <fcntl.h> #include <limits.h> #include <signal.h> -#include <stdio.h> #include <stdbool.h> +#include <stdint.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> -#include <fcntl.h> -#include <stdint.h> #include <unistd.h> #include <utime.h> #include <sys/stat.h> @@ -74,7 +74,7 @@ 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 = "2011"; +const char * const program_year = "2012"; const char * invocation_name = 0; #ifdef O_BINARY @@ -99,11 +99,13 @@ enum Mode { m_compress, m_decompress, m_test }; char * output_filename = 0; int outfd = -1; int verbosity = 0; +const mode_t usr_rw = S_IRUSR | S_IWUSR; +const mode_t all_rw = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; mode_t outfd_mode = S_IRUSR | S_IWUSR; bool delete_output_on_interrupt = false; -/* assure at least a minimum size for buffer `buf' */ +/* 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 ); @@ -121,31 +123,35 @@ static void show_help() { printf( "%s - Data compressor based on the LZMA algorithm.\n", Program_name ); printf( "\nUsage: %s [options] [files]\n", invocation_name ); - printf( "\nOptions:\n" ); - printf( " -h, --help display this help and exit\n" ); - printf( " -V, --version output version information and exit\n" ); - printf( " -b, --member-size=<n> set member size limit in bytes\n" ); - printf( " -c, --stdout send output to standard output\n" ); - printf( " -d, --decompress decompress\n" ); - printf( " -f, --force overwrite existing output files\n" ); - printf( " -F, --recompress force recompression of compressed files\n" ); - printf( " -k, --keep keep (don't delete) input files\n" ); - printf( " -m, --match-length=<n> set match length limit in bytes [36]\n" ); - printf( " -o, --output=<file> if reading stdin, place the output into <file>\n" ); - printf( " -q, --quiet suppress all messages\n" ); - printf( " -s, --dictionary-size=<n> set dictionary size limit in bytes [8MiB]\n" ); - printf( " -S, --volume-size=<n> set volume size limit in bytes\n" ); - printf( " -t, --test test compressed file integrity\n" ); - printf( " -v, --verbose be verbose (a 2nd -v gives more)\n" ); - printf( " -1 .. -9 set compression level [default 6]\n" ); - printf( " --fast alias for -1\n" ); - printf( " --best alias for -9\n" ); - printf( "If no file names are given, %s compresses or decompresses\n", program_name ); - printf( "from standard input to standard output.\n" ); - printf( "Numbers may be followed by a multiplier: k = kB = 10^3 = 1000,\n" ); - printf( "Ki = KiB = 2^10 = 1024, M = 10^6, Mi = 2^20, G = 10^9, Gi = 2^30, etc...\n" ); - printf( "\nReport bugs to lzip-bug@nongnu.org\n" ); - printf( "Clzip home page: http://www.nongnu.org/lzip/clzip.html\n" ); + printf( "\nOptions:\n" + " -h, --help display this help and exit\n" + " -V, --version output version information and exit\n" + " -b, --member-size=<bytes> set member size limit in bytes\n" + " -c, --stdout send output to standard output\n" + " -d, --decompress decompress\n" + " -f, --force overwrite existing output files\n" + " -F, --recompress force recompression of compressed files\n" + " -k, --keep keep (don't delete) input files\n" + " -m, --match-length=<bytes> set match length limit in bytes [36]\n" + " -o, --output=<file> if reading stdin, place the output into <file>\n" + " -q, --quiet suppress all messages\n" + " -s, --dictionary-size=<bytes> set dictionary size limit in bytes [8MiB]\n" + " -S, --volume-size=<bytes> set volume size limit in bytes\n" + " -t, --test test compressed file integrity\n" + " -v, --verbose be verbose (a 2nd -v gives more)\n" + " -1 .. -9 set compression level [default 6]\n" + " --fast alias for -1\n" + " --best alias for -9\n" + "If no file names are given, clzip compresses or decompresses\n" + "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" + "The bidimensional parameter space of LZMA can't be mapped to a linear\n" + "scale optimal for all files. If your files are large, very repetitive,\n" + "etc, you may need to use the --match-length and --dictionary-size\n" + "options directly to achieve optimal performance.\n" + "\nReport bugs to lzip-bug@nongnu.org\n" + "Clzip home page: http://www.nongnu.org/lzip/clzip.html\n" ); } @@ -153,9 +159,9 @@ static void show_version() { printf( "%s %s\n", Program_name, PROGVERSION ); printf( "Copyright (C) %s Antonio Diaz Diaz.\n", program_year ); - printf( "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n" ); - printf( "This is free software: you are free to change and redistribute it.\n" ); - printf( "There is NO WARRANTY, to the extent permitted by law.\n" ); + printf( "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n" + "This is free software: you are free to change and redistribute it.\n" + "There is NO WARRANTY, to the extent permitted by law.\n" ); } @@ -166,11 +172,12 @@ static const char * format_num( long long num ) enum { buf_size = 16, factor = 1024 }; static char buf[buf_size]; const char *p = ""; + bool exact = ( num % factor == 0 ); int i; for( i = 0; i < 8 && ( llabs( num ) > 9999 || - ( llabs( num ) >= factor && num % factor == 0 ) ); ++i ) - { num /= factor; p = prefix[i]; } + ( 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; } @@ -251,7 +258,7 @@ static int open_instream( const char * const name, struct stat * const in_statsp if( program_mode == m_compress && !recompress && eindex >= 0 ) { if( verbosity >= 0 ) - fprintf( stderr, "%s: Input file `%s' already has `%s' suffix.\n", + fprintf( stderr, "%s: Input file '%s' already has '%s' suffix.\n", program_name, name, known_extensions[eindex].from ); } else @@ -260,7 +267,7 @@ static int open_instream( const char * const name, struct stat * const in_statsp if( infd < 0 ) { if( verbosity >= 0 ) - fprintf( stderr, "%s: Can't open input file `%s': %s.\n", + fprintf( stderr, "%s: Can't open input file '%s': %s.\n", program_name, name, strerror( errno ) ); } else @@ -273,10 +280,10 @@ static int open_instream( const char * const name, struct stat * const in_statsp if( i != 0 || ( !S_ISREG( mode ) && ( !to_stdout || !can_read ) ) ) { if( verbosity >= 0 ) - fprintf( stderr, "%s: Input file `%s' is not a regular file%s.\n", + fprintf( stderr, "%s: Input file '%s' is not a regular file%s.\n", program_name, name, ( can_read && !to_stdout ) ? - " and `--stdout' was not specified" : "" ); + " and '--stdout' was not specified" : "" ); close( infd ); infd = -1; } @@ -329,7 +336,7 @@ static void set_d_outname( const char * const name, const int i ) strcpy( output_filename, name ); strcat( output_filename, ".out" ); if( verbosity >= 1 ) - fprintf( stderr, "%s: Can't guess original name for `%s' -- using `%s'.\n", + fprintf( stderr, "%s: Can't guess original name for '%s' -- using '%s'.\n", program_name, name, output_filename ); } @@ -343,10 +350,10 @@ static bool open_outstream( const bool force ) if( outfd < 0 && verbosity >= 0 ) { if( errno == EEXIST ) - fprintf( stderr, "%s: Output file %s already exists, skipping.\n", + fprintf( stderr, "%s: Output file '%s' already exists, skipping.\n", program_name, output_filename ); else - fprintf( stderr, "%s: Can't create output file `%s': %s.\n", + fprintf( stderr, "%s: Can't create output file '%s': %s.\n", program_name, output_filename, strerror( errno ) ); } return ( outfd >= 0 ); @@ -376,7 +383,7 @@ void cleanup_and_fail( const int retval ) { delete_output_on_interrupt = false; if( verbosity >= 0 ) - fprintf( stderr, "%s: Deleting output file `%s', if it exists.\n", + fprintf( stderr, "%s: Deleting output file '%s', if it exists.\n", program_name, output_filename ); if( outfd >= 0 ) { close( outfd ); outfd = -1; } if( remove( output_filename ) != 0 && errno != ENOENT ) @@ -389,31 +396,26 @@ void cleanup_and_fail( const int retval ) /* Set permissions, owner and times. */ static void close_and_set_permissions( const struct stat * const in_statsp ) { - bool error = false; + bool warning = false; if( in_statsp ) { + /* fchown will in many cases return with EPERM, which can be safely ignored. */ if( ( fchown( outfd, in_statsp->st_uid, in_statsp->st_gid ) != 0 && errno != EPERM ) || - fchmod( outfd, in_statsp->st_mode ) != 0 ) - error = true; - /* fchown will in many cases return with EPERM, which can be safely ignored. */ + fchmod( outfd, in_statsp->st_mode ) != 0 ) warning = true; } - if( close( outfd ) == 0 ) outfd = -1; - else cleanup_and_fail( 1 ); + if( close( outfd ) != 0 ) cleanup_and_fail( 1 ); + outfd = -1; delete_output_on_interrupt = false; - if( !in_statsp ) return; - if( !error ) + if( in_statsp ) { struct utimbuf t; t.actime = in_statsp->st_atime; t.modtime = in_statsp->st_mtime; - if( utime( output_filename, &t ) != 0 ) error = true; + if( utime( output_filename, &t ) != 0 ) warning = true; } - if( error ) - { + if( warning && verbosity >= 1 ) show_error( "Can't change output file attributes.", 0, false ); - cleanup_and_fail( 1 ); - } } @@ -449,21 +451,29 @@ static int compress( const long long member_size, const long long volume_size, encoder_options->match_len_limit > max_match_len ) internal_error( "invalid argument to encoder" ); - Mf_init( &matchfinder, Fh_get_dictionary_size( header ), - encoder_options->match_len_limit, infd ); - Fh_set_dictionary_size( header, Mf_dictionary_size( &matchfinder ) ); + if( !Mf_init( &matchfinder, Fh_get_dictionary_size( header ), + encoder_options->match_len_limit, infd ) ) + { + Pp_show_msg( pp, "Not enough memory. Try a smaller dictionary size" ); + return 1; + } + Fh_set_dictionary_size( header, matchfinder.dictionary_size ); while( true ) /* encode one member per iteration */ { struct LZ_encoder encoder; const long long size = min( member_size, volume_size - partial_volume_size ); - LZe_init( &encoder, &matchfinder, header, outfd ); + if( !LZe_init( &encoder, &matchfinder, header, outfd ) ) + { + show_error( "Not enough memory. Try a smaller dictionary size.", 0, false ); + cleanup_and_fail( 1 ); + } if( !LZe_encode_member( &encoder, size ) ) { Pp_show_msg( pp, "Encoder error" ); retval = 1; break; } in_size += Mf_data_position( &matchfinder ); - out_size += LZe_member_position( &encoder ); - partial_volume_size += LZe_member_position( &encoder ); + 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 ) @@ -484,7 +494,7 @@ 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 ) - fprintf( stderr, "No data compressed.\n" ); + fprintf( stderr, " no data compressed.\n" ); else fprintf( stderr, "%6.3f:1, %6.3f bits/byte, " "%5.2f%% saved, %lld in, %lld out.\n", @@ -503,17 +513,20 @@ static int decompress( const int infd, struct Pretty_print * const pp, { long long partial_file_pos = 0; struct Range_decoder rdec; - int retval = 0, i, result; + int retval = 0, result; bool first_member; - Rd_init( &rdec, infd ); + if( !Rd_init( &rdec, infd ) ) + { + show_error( "Not enough memory. Find a machine with more memory.", 0, false ); + cleanup_and_fail( 1 ); + } for( first_member = true; ; first_member = false, Pp_reset( pp ) ) { File_header header; struct LZ_decoder decoder; Rd_reset_member_position( &rdec ); - for( i = 0; i < Fh_size; ++i ) - header[i] = Rd_get_byte( &rdec ); + Rd_read_data( &rdec, header, Fh_size ); if( Rd_finished( &rdec ) ) /* End Of File */ { if( first_member ) @@ -547,8 +560,12 @@ static int decompress( const int infd, struct Pretty_print * const pp, Fh_version( header ), format_num( Fh_get_dictionary_size( header ) ) ); } - LZd_init( &decoder, header, &rdec, outfd ); + if( !LZd_init( &decoder, header, &rdec, outfd ) ) + { + show_error( "Not enough memory. Find a machine with more memory.", 0, false ); + cleanup_and_fail( 1 ); + } result = LZd_decode_member( &decoder, pp ); partial_file_pos += Rd_member_position( &rdec ); LZd_free( &decoder ); @@ -598,7 +615,7 @@ void Pp_init( struct Pretty_print * const pp, const char * const filenames[], { unsigned int stdin_name_len; int i; - pp->name_ = 0; + pp->name = 0; pp->stdin_name = "(stdin)"; pp->longest_name = 0; pp->verbosity = v; @@ -623,8 +640,8 @@ void Pp_show_msg( struct Pretty_print * const pp, const char * const msg ) { int i, len; pp->first_post = false; - fprintf( stderr, " %s: ", pp->name_ ); - len = pp->longest_name - strlen( pp->name_ ); + fprintf( stderr, " %s: ", pp->name ); + len = pp->longest_name - strlen( pp->name ); for( i = 0; i < len; ++i ) fprintf( stderr, " " ); if( !msg ) fflush( stderr ); } @@ -644,7 +661,7 @@ void show_error( const char * const msg, const int errcode, const bool help ) fprintf( stderr, "\n" ); } if( help && invocation_name && invocation_name[0] ) - fprintf( stderr, "Try `%s --help' for more information.\n", + fprintf( stderr, "Try '%s --help' for more information.\n", invocation_name ); } } @@ -708,7 +725,6 @@ int main( const int argc, const char * const argv[] ) { 'b', "member-size", ap_yes }, { 'c', "stdout", ap_no }, { 'd', "decompress", ap_no }, - { 'e', "extreme", ap_no }, { 'f', "force", ap_no }, { 'F', "recompress", ap_no }, { 'h', "help", ap_no }, @@ -727,6 +743,7 @@ int main( const int argc, const char * const argv[] ) invocation_name = argv[0]; CRC32_init(); + if( !ap_init( &parser, argc, argv, options, 0 ) ) { show_error( "Memory exhausted.", 0, false ); return 1; } if( ap_error( &parser ) ) /* bad option */ @@ -746,7 +763,6 @@ int main( const int argc, const char * const argv[] ) case 'b': member_size = getnum( arg, 100000, LLONG_MAX / 2 ); break; case 'c': to_stdout = true; break; case 'd': program_mode = m_decompress; break; - case 'e': break; /* ignored by now */ case 'f': force = true; break; case 'F': recompress = true; break; case 'h': show_help(); return 0; @@ -766,10 +782,18 @@ int main( const int argc, const char * const argv[] ) } /* end process options */ #if defined(__MSVCRT__) || defined(__OS2__) - _setmode( STDIN_FILENO, O_BINARY ); - _setmode( STDOUT_FILENO, O_BINARY ); + _fsetmode( stdin, "b" ); + _fsetmode( stdout, "b" ); #endif + if( program_mode == m_test ) + outfd = -1; + else if( program_mode == m_compress ) + { + Dis_slots_init(); + Prob_prices_init(); + } + for( ; argind < ap_arguments( &parser ); ++argind ) { if( strcmp( ap_argument( &parser, argind ), "-" ) ) @@ -790,13 +814,6 @@ int main( const int argc, const char * const argv[] ) set_signals(); Pp_init( &pp, filenames, num_filenames, verbosity ); - if( program_mode == m_test ) - outfd = -1; - else if( program_mode == m_compress ) - { - Dis_slots_init(); - Prob_prices_init(); - } output_filename = resize_buffer( output_filename, 1 ); for( i = 0; i < num_filenames; ++i ) @@ -824,7 +841,7 @@ int main( const int argc, const char * const argv[] ) strlen( default_output_filename ) + 1 ); strcpy( output_filename, default_output_filename ); } - outfd_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; + outfd_mode = all_rw; if( !open_outstream( force ) ) { if( outfd == -1 && retval < 1 ) retval = 1; @@ -849,7 +866,7 @@ int main( const int argc, const char * const argv[] ) if( program_mode == m_compress ) set_c_outname( input_filename, volume_size != LLONG_MAX ); else set_d_outname( input_filename, eindex ); - outfd_mode = S_IRUSR | S_IWUSR; + outfd_mode = usr_rw; if( !open_outstream( force ) ) { if( outfd == -1 && retval < 1 ) retval = 1; |