diff options
Diffstat (limited to 'minilzip.c')
-rw-r--r-- | minilzip.c | 109 |
1 files changed, 55 insertions, 54 deletions
@@ -99,13 +99,13 @@ static const struct { const char * from; const char * to; } known_extensions[] = { ".tlz", ".tar" }, { 0, 0 } }; -struct Lzma_options +typedef struct Lzma_options { int dictionary_size; /* 4 KiB .. 512 MiB */ int match_len_limit; /* 5 .. 273 */ - }; + } Lzma_options; -enum Mode { m_compress, m_decompress, m_test }; +typedef enum Mode { m_compress, m_decompress, m_test } Mode; /* Variables used in signal handler context. They are not declared volatile because the handler never returns. */ @@ -116,25 +116,26 @@ static bool delete_output_on_interrupt = false; static void show_help( void ) { - printf( "Minilzip is a test program for the compression library lzlib, compatible\n" - "(interoperable) with lzip 1.4 or newer.\n" + printf( "Minilzip is a test program for the compression library lzlib. Minilzip is\n" + "not intended to be installed because lzip has more features, but minilzip is\n" + "well tested and you can use it as your main compressor if so you wish.\n" "\nLzip is a lossless data compressor with a user interface similar to the one\n" - "of gzip or bzip2. Lzip uses a simplified form of the 'Lempel-Ziv-Markov\n" - "chain-Algorithm' (LZMA) stream format to maximize interoperability. The\n" - "maximum dictionary size is 512 MiB so that any lzip file can be decompressed\n" - "on 32-bit machines. Lzip provides accurate and robust 3-factor integrity\n" - "checking. Lzip can compress about as fast as gzip (lzip -0) or compress most\n" - "files more than bzip2 (lzip -9). Decompression speed is intermediate between\n" - "gzip and bzip2. Lzip provides better data recovery capabilities than gzip\n" - "and bzip2. Lzip has been designed, written, and tested with great care to\n" - "replace gzip and bzip2 as the standard general-purpose compressed format for\n" - "Unix-like systems.\n" + "of gzip or bzip2. Lzip uses a simplified form of LZMA (Lempel-Ziv-Markov\n" + "chain-Algorithm) designed to achieve complete interoperability between\n" + "implementations. The maximum dictionary size is 512 MiB so that any lzip\n" + "file can be decompressed on 32-bit machines. Lzip provides accurate and\n" + "robust 3-factor integrity checking. 'lzip -0' compresses about as fast as\n" + "gzip, while 'lzip -9' compresses most files more than bzip2. Decompression\n" + "speed is intermediate between gzip and bzip2. Lzip provides better data\n" + "recovery capabilities than gzip and bzip2. Lzip has been designed, written,\n" + "and tested with great care to replace gzip and bzip2 as general-purpose\n" + "compressed format for Unix-like systems.\n" "\nUsage: %s [options] [files]\n", invocation_name ); 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" + " -b, --member-size=<bytes> set member size limit of multimember files\n" " -c, --stdout write to standard output, keep input files\n" " -d, --decompress decompress, test compressed file integrity\n" " -f, --force overwrite existing output files\n" @@ -264,16 +265,16 @@ static void * resize_buffer( void * buf, const unsigned min_size ) } -struct Pretty_print +typedef struct Pretty_print { const char * name; char * padded_name; const char * stdin_name; unsigned longest_name; bool first_post; - }; + } Pretty_print; -static void Pp_init( struct Pretty_print * const pp, +static void Pp_init( Pretty_print * const pp, const char * const filenames[], const int num_filenames ) { pp->name = 0; @@ -294,11 +295,10 @@ static void Pp_init( struct Pretty_print * const pp, if( pp->longest_name == 0 ) pp->longest_name = stdin_name_len; } -void Pp_free( struct Pretty_print * const pp ) +void Pp_free( Pretty_print * const pp ) { if( pp->padded_name ) { free( pp->padded_name ); pp->padded_name = 0; } } -static void Pp_set_name( struct Pretty_print * const pp, - const char * const filename ) +static void Pp_set_name( Pretty_print * const pp, const char * const filename ) { unsigned name_len, padded_name_len, i = 0; @@ -316,10 +316,10 @@ static void Pp_set_name( struct Pretty_print * const pp, pp->first_post = true; } -static void Pp_reset( struct Pretty_print * const pp ) +static void Pp_reset( Pretty_print * const pp ) { if( pp->name && pp->name[0] ) pp->first_post = true; } -static void Pp_show_msg( struct Pretty_print * const pp, const char * const msg ) +static void Pp_show_msg( Pretty_print * const pp, const char * const msg ) { if( verbosity < 0 ) return; if( pp->first_post ) @@ -339,7 +339,7 @@ static void show_header( const unsigned dictionary_size ) const char * p = ""; const char * np = " "; unsigned num = dictionary_size; - bool exact = ( num % factor == 0 ); + bool exact = num % factor == 0; int i; for( i = 0; i < n && ( num > 9999 || ( exact && num >= factor ) ); ++i ) { num /= factor; if( num % factor != 0 ) exact = false; @@ -406,7 +406,7 @@ static unsigned long long getnum( const char * const arg, if( !errno && tail[0] ) { - const unsigned factor = ( tail[1] == 'i' ) ? 1024 : 1000; + const unsigned factor = (tail[1] == 'i') ? 1024 : 1000; int exponent = 0; /* 0 = bad multiplier */ int i; switch( tail[0] ) @@ -459,7 +459,7 @@ static int get_dict_size( const char * const arg, const char * const option_name } -static void set_mode( enum Mode * const program_modep, const enum Mode new_mode ) +static void set_mode( Mode * const program_modep, const Mode new_mode ) { if( *program_modep != m_compress && *program_modep != new_mode ) { @@ -524,7 +524,7 @@ static void set_d_outname( const char * const name, const int eindex ) static int open_instream( const char * const name, struct stat * const in_statsp, - const enum Mode program_mode, const int eindex, + const Mode program_mode, const int eindex, const bool one_to_one, const bool recompress ) { if( program_mode == m_compress && !recompress && eindex >= 0 ) @@ -541,9 +541,9 @@ static int open_instream( const char * const name, struct stat * const in_statsp { const int i = fstat( infd, in_statsp ); const mode_t mode = in_statsp->st_mode; - const bool can_read = ( i == 0 && - ( S_ISBLK( mode ) || S_ISCHR( mode ) || - S_ISFIFO( mode ) || S_ISSOCK( mode ) ) ); + const bool can_read = i == 0 && + ( S_ISBLK( mode ) || S_ISCHR( mode ) || + S_ISFIFO( mode ) || S_ISSOCK( mode ) ); if( i != 0 || ( !S_ISREG( mode ) && ( !can_read || one_to_one ) ) ) { if( verbosity >= 0 ) @@ -609,7 +609,7 @@ static void signal_handler( int sig ) static bool check_tty_in( const char * const input_filename, const int infd, - const enum Mode program_mode, int * const retval ) + const Mode program_mode, int * const retval ) { if( ( program_mode == m_decompress || program_mode == m_test ) && isatty( infd ) ) /* for example /dev/tty */ @@ -621,7 +621,7 @@ static bool check_tty_in( const char * const input_filename, const int infd, return true; } -static bool check_tty_out( const enum Mode program_mode ) +static bool check_tty_out( const Mode program_mode ) { if( program_mode == m_compress && isatty( outfd ) ) { show_file_error( output_filename[0] ? @@ -717,10 +717,10 @@ static bool next_filename( void ) } -static int do_compress( struct LZ_Encoder * const encoder, +static int do_compress( LZ_Encoder * const encoder, const unsigned long long member_size, const unsigned long long volume_size, const int infd, - struct Pretty_print * const pp, + Pretty_print * const pp, const struct stat * const in_statsp ) { unsigned long long partial_volume_size = 0; @@ -819,11 +819,11 @@ static int do_compress( struct LZ_Encoder * const encoder, static int compress( const unsigned long long member_size, const unsigned long long volume_size, const int infd, - const struct Lzma_options * const encoder_options, - struct Pretty_print * const pp, + const Lzma_options * const encoder_options, + Pretty_print * const pp, const struct stat * const in_statsp ) { - struct LZ_Encoder * const encoder = + LZ_Encoder * const encoder = LZ_compress_open( encoder_options->dictionary_size, encoder_options->match_len_limit, ( volume_size > 0 ) ? min( member_size, volume_size ) : member_size ); @@ -844,8 +844,8 @@ static int compress( const unsigned long long member_size, } -static int do_decompress( struct LZ_Decoder * const decoder, const int infd, - struct Pretty_print * const pp, const bool from_stdin, +static int do_decompress( LZ_Decoder * const decoder, const int infd, + Pretty_print * const pp, const bool from_stdin, const bool ignore_trailing, const bool loose_trailing, const bool testing ) { @@ -853,7 +853,7 @@ static int do_decompress( struct LZ_Decoder * const decoder, const int infd, uint8_t buffer[buffer_size]; /* read/write buffer */ unsigned long long total_in = 0; /* to detect library stall */ bool first_member; - bool empty = false, nonempty = false; + bool empty = false, multi = false; for( first_member = true; ; ) { @@ -894,10 +894,11 @@ static int do_decompress( struct LZ_Decoder * const decoder, const int infd, { const unsigned long long data_size = LZ_decompress_data_position( decoder ); if( !from_stdin ) - { if( data_size == 0 ) empty = true; else nonempty = true; } + { multi = !first_member; if( data_size == 0 ) empty = true; } if( verbosity >= 1 ) { - const unsigned long long member_size = LZ_decompress_member_position( decoder ); + const unsigned long long member_size = + LZ_decompress_member_position( decoder ); if( verbosity >= 2 || ( verbosity == 1 && first_member ) ) Pp_show_msg( pp, 0 ); if( verbosity >= 2 ) @@ -925,7 +926,7 @@ static int do_decompress( struct LZ_Decoder * const decoder, const int infd, if( out_size < 0 || ( first_member && out_size == 0 ) ) { const unsigned long long member_pos = LZ_decompress_member_position( decoder ); - const enum LZ_Errno lz_errno = LZ_decompress_errno( decoder ); + const LZ_Errno lz_errno = LZ_decompress_errno( decoder ); if( lz_errno == LZ_library_error ) internal_error( "library error (LZ_decompress_read)." ); if( member_pos <= 6 ) @@ -988,17 +989,17 @@ static int do_decompress( struct LZ_Decoder * const decoder, const int infd, } } if( verbosity == 1 ) fputs( testing ? "ok\n" : "done\n", stderr ); - if( empty && nonempty ) + if( empty && multi ) { show_file_error( pp->name, "Empty member not allowed.", 0 ); return 2; } return 0; } -static int decompress( const int infd, struct Pretty_print * const pp, +static int decompress( const int infd, Pretty_print * const pp, const bool from_stdin, const bool ignore_trailing, const bool loose_trailing, const bool testing ) { - struct LZ_Decoder * const decoder = LZ_decompress_open(); + LZ_Decoder * const decoder = LZ_decompress_open(); int retval; if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok ) @@ -1046,7 +1047,7 @@ int main( const int argc, const char * const argv[] ) { /* Mapping from gzip/bzip2 style 0..9 compression levels to the corresponding LZMA compression parameters. */ - const struct Lzma_options option_mapping[] = + const Lzma_options option_mapping[] = { { 65535, 16 }, /* -0 (65535,16 chooses fast encoder) */ { 1 << 20, 5 }, /* -1 */ @@ -1058,14 +1059,13 @@ int main( const int argc, const char * const argv[] ) { 1 << 24, 68 }, /* -7 */ { 3 << 23, 132 }, /* -8 */ { 1 << 25, 273 } }; /* -9 */ - struct Lzma_options encoder_options = option_mapping[6]; /* default = "-6" */ + Lzma_options encoder_options = option_mapping[6]; /* default = "-6" */ const unsigned long long max_member_size = 0x0008000000000000ULL; /* 2 PiB */ const unsigned long long max_volume_size = 0x4000000000000000ULL; /* 4 EiB */ unsigned long long member_size = max_member_size; unsigned long long volume_size = 0; const char * default_output_filename = ""; - enum Mode program_mode = m_compress; - int i; + Mode program_mode = m_compress; bool force = false; bool ignore_trailing = true; bool keep_input_files = false; @@ -1075,7 +1075,7 @@ int main( const int argc, const char * const argv[] ) if( argc > 0 ) invocation_name = argv[0]; enum { opt_chk = 256, opt_lt }; - const struct ap_Option options[] = + const ap_Option options[] = { { '0', "fast", ap_no }, { '1', 0, ap_no }, @@ -1109,7 +1109,7 @@ int main( const int argc, const char * const argv[] ) { 0, 0, ap_no } }; /* static because valgrind complains and memory management in C sucks */ - static struct Arg_parser parser; + static Arg_parser parser; if( !ap_init( &parser, argc, argv, options, 0 ) ) { show_error( mem_msg, 0, false ); return 1; } if( ap_error( &parser ) ) /* bad option */ @@ -1179,6 +1179,7 @@ int main( const int argc, const char * const argv[] ) filenames = resize_buffer( filenames, num_filenames * sizeof filenames[0] ); filenames[0] = "-"; + int i; bool filenames_given = false; for( i = 0; argind + i < ap_arguments( &parser ); ++i ) { @@ -1208,7 +1209,7 @@ int main( const int argc, const char * const argv[] ) if( !to_stdout && program_mode != m_test && ( filenames_given || to_file ) ) set_signals( signal_handler ); - static struct Pretty_print pp; + static Pretty_print pp; Pp_init( &pp, filenames, num_filenames ); int failed_tests = 0; |