summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c92
1 files changed, 48 insertions, 44 deletions
diff --git a/main.c b/main.c
index ffbc721..1c9d29c 100644
--- a/main.c
+++ b/main.c
@@ -1,5 +1,5 @@
/* Minilzip - Test program for the lzlib library
- Copyright (C) 2009-2014 Antonio Diaz Diaz.
+ Copyright (C) 2009-2015 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
@@ -76,7 +76,7 @@ void internal_error( const char * const msg );
const char * const Program_name = "Minilzip";
const char * const program_name = "minilzip";
-const char * const program_year = "2014";
+const char * const program_year = "2015";
const char * invocation_name = 0;
struct { const char * from; const char * to; } const known_extensions[] = {
@@ -179,8 +179,8 @@ static void show_help( void )
" -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"
+ " -0 .. -9 set compression level [default 6]\n"
+ " --fast alias for -0\n"
" --best alias for -9\n"
"If no file names are given, minilzip compresses or decompresses\n"
"from standard input to standard output.\n"
@@ -189,8 +189,7 @@ static void show_help( void )
"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. For example, -9m64\n"
- "usually compresses executables more (and faster) than -9.\n"
+ "options directly to achieve optimal performance.\n"
"\nExit status: 0 for a normal exit, 1 for environmental problems (file\n"
"not found, invalid flags, I/O errors, etc), 2 to indicate a corrupt or\n"
"invalid input file, 3 for an internal consistency error (eg, bug) which\n"
@@ -213,18 +212,21 @@ static void show_version( void )
static void show_header( const unsigned dictionary_size )
{
- const char * const prefix[8] =
- { "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" };
- enum { factor = 1024 };
- const char * p = "";
- const char * np = " ";
- unsigned num = dictionary_size, i;
- bool exact = ( num % factor == 0 );
-
- 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, "dictionary size %s%4u %sB. ", np, num, p );
+ if( verbosity >= 3 )
+ {
+ const char * const prefix[8] =
+ { "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" };
+ enum { factor = 1024 };
+ const char * p = "";
+ const char * np = " ";
+ unsigned num = dictionary_size, i;
+ bool exact = ( num % factor == 0 );
+
+ 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, "dictionary size %s%4u %sB. ", np, num, p );
+ }
}
@@ -301,8 +303,10 @@ static int extension_index( const char * const name )
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 )
+ const unsigned name_len = strlen( name );
+ const unsigned ext_len = strlen( ext );
+ if( name_len > ext_len &&
+ strncmp( name + name_len - ext_len, ext, ext_len ) == 0 )
return i;
}
return -1;
@@ -379,20 +383,21 @@ static void set_c_outname( const char * const name, const bool multifile )
static void set_d_outname( const char * const name, const int i )
{
+ const unsigned name_len = strlen( name );
if( i >= 0 )
{
const char * const from = known_extensions[i].from;
- if( strlen( name ) > strlen( from ) )
+ const unsigned from_len = strlen( from );
+ if( name_len > from_len )
{
- output_filename = resize_buffer( output_filename, strlen( name ) +
+ output_filename = resize_buffer( output_filename, name_len +
strlen( known_extensions[0].to ) + 1 );
strcpy( output_filename, name );
- strcpy( output_filename + strlen( name ) - strlen( from ),
- known_extensions[i].to );
+ strcpy( output_filename + name_len - from_len, known_extensions[i].to );
return;
}
}
- output_filename = resize_buffer( output_filename, strlen( name ) + 4 + 1 );
+ output_filename = resize_buffer( output_filename, name_len + 4 + 1 );
strcpy( output_filename, name );
strcat( output_filename, ".out" );
if( verbosity >= 1 )
@@ -422,7 +427,7 @@ static bool open_outstream( const bool force )
static bool check_tty( const int infd, const enum Mode program_mode )
{
- if( program_mode == m_compress && outfd >= 0 && isatty( outfd ) )
+ if( program_mode == m_compress && isatty( outfd ) )
{
show_error( "I won't write compressed data to a terminal.", 0, true );
return false;
@@ -522,11 +527,11 @@ static int writeblock( const int fd, const uint8_t * const buf, const int size )
static bool next_filename( void )
{
- const unsigned len = strlen( known_extensions[0].from );
+ const unsigned name_len = strlen( output_filename );
+ const unsigned ext_len = strlen( known_extensions[0].from );
int i, j;
-
- if( strlen( output_filename ) >= len + 5 ) /* "*00001.lz" */
- for( i = strlen( output_filename ) - len - 1, j = 0; j < 5; --i, ++j )
+ if( name_len >= ext_len + 5 ) /* "*00001.lz" */
+ for( i = name_len - ext_len - 1, j = 0; j < 5; --i, ++j )
{
if( output_filename[i] < '9' ) { ++output_filename[i]; return true; }
else output_filename[i] = '0';
@@ -635,11 +640,11 @@ static int do_compress( struct LZ_Encoder * const encoder,
}
-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 )
+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 struct stat * const in_statsp )
{
struct LZ_Encoder * const encoder =
LZ_compress_open( encoder_options->dictionary_size,
@@ -662,8 +667,8 @@ int compress( const unsigned long long member_size,
}
-int do_decompress( struct LZ_Decoder * const decoder, const int infd,
- struct Pretty_print * const pp, const bool testing )
+static int do_decompress( struct LZ_Decoder * const decoder, const int infd,
+ struct Pretty_print * const pp, const bool testing )
{
enum { buffer_size = 65536 };
uint8_t buffer[buffer_size];
@@ -710,8 +715,7 @@ int do_decompress( struct LZ_Decoder * const decoder, const int infd,
const unsigned long long data_position = LZ_decompress_data_position( decoder );
const unsigned long long member_size = LZ_decompress_member_position( decoder );
Pp_show_msg( pp, 0 );
- if( verbosity >= 3 )
- show_header( LZ_decompress_dictionary_size( decoder ) );
+ show_header( LZ_decompress_dictionary_size( decoder ) );
if( verbosity >= 2 && data_position > 0 && member_size > 0 )
fprintf( stderr, "%6.3f:1, %6.3f bits/byte, %5.2f%% saved. ",
(double)data_position / member_size,
@@ -759,8 +763,8 @@ int do_decompress( struct LZ_Decoder * const decoder, const int infd,
}
-int decompress( const int infd, struct Pretty_print * const pp,
- const bool testing )
+static int decompress( const int infd, struct Pretty_print * const pp,
+ const bool testing )
{
struct LZ_Decoder * const decoder = LZ_decompress_open();
int retval;
@@ -821,7 +825,7 @@ int main( const int argc, const char * const argv[] )
to the corresponding LZMA compression modes. */
const struct Lzma_options option_mapping[] =
{
- { 1 << 20, 5 }, /* -0 */
+ { 65535, 16 }, /* -0 */
{ 1 << 20, 5 }, /* -1 */
{ 3 << 19, 6 }, /* -2 */
{ 1 << 21, 8 }, /* -3 */
@@ -1022,8 +1026,8 @@ int main( const int argc, const char * const argv[] )
in_statsp = input_filename[0] ? &in_stats : 0;
Pp_set_name( &pp, input_filename );
if( program_mode == m_compress )
- tmp = compress( member_size, volume_size, &encoder_options, infd,
- &pp, in_statsp );
+ tmp = compress( member_size, volume_size, infd, &encoder_options, &pp,
+ in_statsp );
else
tmp = decompress( infd, &pp, program_mode == m_test );
if( tmp > retval ) retval = tmp;