summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c99
1 files changed, 54 insertions, 45 deletions
diff --git a/main.c b/main.c
index 5e09fe0..de8e12e 100644
--- a/main.c
+++ b/main.c
@@ -1,6 +1,6 @@
/* Pdlzip - LZMA lossless data compressor
2009-08-14 : Igor Pavlov : Public domain
- Copyright (C) 2010-2016 Antonio Diaz Diaz.
+ Copyright (C) 2010-2017 Antonio Diaz Diaz.
This program is free software. Redistribution and use in source and
binary forms, with or without modification, are permitted provided
@@ -70,10 +70,10 @@ int verbosity = 0;
const char * const Program_name = "Pdlzip";
const char * const program_name = "pdlzip";
-const char * const program_year = "2016";
+const char * const program_year = "2017";
const char * invocation_name = 0;
-struct { const char * from; const char * to; } const known_extensions[] = {
+const struct { const char * from; const char * to; } known_extensions[] = {
{ ".lz", "" },
{ ".tlz", ".tar" },
{ ".lzma", "" },
@@ -98,7 +98,7 @@ static void show_help( void )
printf( "compressor also able to decompress legacy lzma-alone (.lzma) files.\n"
"\nLzma-alone is a very bad format; it is essentially a raw LZMA stream.\n"
"If you keep any lzma-alone files, it is advisable to recompress them to\n"
- "lzip format. Lziprecover can convert lzma-alone files to lzip format\n"
+ "lzip format. Lziprecover can convert some lzma-alone files to lzip format\n"
"without recompressing.\n"
"\nUsage: %s [options] [files]\n", invocation_name );
printf( "\nOptions:\n"
@@ -154,9 +154,9 @@ static void show_header( const unsigned dictionary_size )
{
if( verbosity >= 3 )
{
+ enum { factor = 1024 };
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;
@@ -165,7 +165,7 @@ static void show_header( const unsigned dictionary_size )
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 );
+ fprintf( stderr, "dictionary %s%4u %sB. ", np, num, p );
}
}
@@ -186,7 +186,7 @@ static unsigned long getnum( const char * const ptr,
if( !errno && tail[0] )
{
- const int 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] )
@@ -225,25 +225,25 @@ static unsigned long getnum( const char * const ptr,
static int get_dict_size( const char * const arg )
{
char * tail;
- const int bits = strtol( arg, &tail, 0 );
+ const long bits = strtol( arg, &tail, 0 );
if( bits >= min_dictionary_bits &&
- bits <= max_dictionary_bits && *tail == 0 )
+ bits <= max_dictionary_bits_c && *tail == 0 )
return ( 1 << bits );
- return getnum( arg, min_dictionary_size, max_dictionary_size );
+ return getnum( arg, min_dictionary_size, max_dictionary_size_c );
}
static int extension_index( const char * const name )
{
- int i;
- for( i = 0; known_extensions[i].from; ++i )
+ int eindex;
+ for( eindex = 0; known_extensions[eindex].from; ++eindex )
{
- const char * const ext = known_extensions[i].from;
+ const char * const ext = known_extensions[eindex].from;
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 eindex;
}
return -1;
}
@@ -264,11 +264,7 @@ static int open_instream( const char * const name, struct stat * const in_statsp
{
infd = open( name, O_RDONLY | O_BINARY );
if( infd < 0 )
- {
- if( verbosity >= 0 )
- fprintf( stderr, "%s: Can't open input file '%s': %s\n",
- program_name, name, strerror( errno ) );
- }
+ show_file_error( name, "Can't open input file", errno );
else
{
const int i = fstat( infd, in_statsp );
@@ -294,7 +290,7 @@ 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 )
+static void * resize_buffer( void * buf, const unsigned min_size )
{
if( buf ) buf = realloc( buf, min_size );
else buf = malloc( min_size );
@@ -316,19 +312,19 @@ static void set_c_outname( const char * const name )
}
-static void set_d_outname( const char * const name, const int i )
+static void set_d_outname( const char * const name, const int eindex )
{
const unsigned name_len = strlen( name );
- if( i >= 0 )
+ if( eindex >= 0 )
{
- const char * const from = known_extensions[i].from;
+ const char * const from = known_extensions[eindex].from;
const unsigned from_len = strlen( from );
if( name_len > from_len )
{
output_filename = resize_buffer( output_filename, name_len +
- strlen( known_extensions[0].to ) + 1 );
+ strlen( known_extensions[eindex].to ) + 1 );
strcpy( output_filename, name );
- strcpy( output_filename + name_len - from_len, known_extensions[i].to );
+ strcpy( output_filename + name_len - from_len, known_extensions[eindex].to );
return;
}
}
@@ -364,7 +360,8 @@ static bool open_outstream( const bool force, const bool from_stdin )
}
-static bool check_tty( const int infd, const enum Mode program_mode )
+static bool check_tty( const char * const input_filename, const int infd,
+ const enum Mode program_mode )
{
if( program_mode == m_compress && isatty( outfd ) )
{
@@ -374,7 +371,8 @@ static bool check_tty( const int infd, const enum Mode program_mode )
if( ( program_mode == m_decompress || program_mode == m_test ) &&
isatty( infd ) )
{
- show_error( "I won't read compressed data from a terminal.", 0, true );
+ show_file_error( input_filename,
+ "I won't read compressed data from a terminal.", 0 );
return false;
}
return true;
@@ -432,7 +430,7 @@ static void close_and_set_permissions( const struct stat * const in_statsp )
static int compress( const struct Lzma_options * const encoder_options,
- const int infd, struct Pretty_print * const pp )
+ struct Pretty_print * const pp, const int infd )
{
int retval = 0;
CLzmaEncHandle encoder = 0;
@@ -604,9 +602,8 @@ static int lzip_decode( CLzmaDec *decoder, const int infd, uint8_t inBuf[],
{
error = true;
if( verbosity >= 0 )
- fprintf( stderr, "Trailer truncated at trailer position %u;"
- " some checks may fail.\n",
- (unsigned)(*inSize - *inPos) );
+ fprintf( stderr, "Trailer truncated at trailer position %d;"
+ " some checks may fail.\n", *inSize - *inPos );
for( i = *inSize - *inPos; i < Ft_size; ++i )
inBuf[*inPos+i] = 0;
}
@@ -641,7 +638,7 @@ static int lzip_decode( CLzmaDec *decoder, const int infd, uint8_t inBuf[],
( 8.0 * total_in ) / total_out,
100.0 * ( 1.0 - ( (double)total_in / total_out ) ) );
if( !error && verbosity >= 4 )
- fprintf( stderr, "data CRC %08X, data size %9llu, member size %8llu. ",
+ fprintf( stderr, "CRC %08X, decompressed %9llu, compressed %8llu. ",
crc, total_out, total_in );
if( error ) return 2;
return 0;
@@ -677,6 +674,8 @@ static int decompress( const int infd, struct Pretty_print * const pp,
if( first_member || Fh_verify_prefix( header, size ) )
{ Pp_show_msg( pp, "File ends unexpectedly at member header." );
retval = 2; }
+ else if( size > 0 && !ignore_trailing )
+ { show_file_error( pp->name, trailing_msg, 0 ); retval = 2; }
break;
}
if( !Fh_verify_magic( header ) )
@@ -684,7 +683,7 @@ static int decompress( const int infd, struct Pretty_print * const pp,
if( !first_member )
{
if( !ignore_trailing )
- { Pp_show_msg( pp, "Trailing data not allowed." ); retval = 2; }
+ { show_file_error( pp->name, trailing_msg, 0 ); retval = 2; }
break;
}
if( inSize - inPos >= lzma_header_size - Fh_size ) /* try lzma-alone */
@@ -704,7 +703,8 @@ static int decompress( const int infd, struct Pretty_print * const pp,
}
if( lzip_mode )
{
- Pp_show_msg( pp, "Bad magic number (file not in lzip format)." );
+ show_file_error( pp->name,
+ "Bad magic number (file not in lzip format).", 0 );
retval = 2; break;
}
}
@@ -839,6 +839,16 @@ void show_error( const char * const msg, const int errcode, const bool help )
}
+void show_file_error( const char * const filename, const char * const msg,
+ const int errcode )
+ {
+ if( verbosity < 0 ) return;
+ fprintf( stderr, "%s: %s: %s", program_name, filename, msg );
+ if( errcode > 0 ) fprintf( stderr, ": %s", strerror( errcode ) );
+ fputc( '\n', stderr );
+ }
+
+
void internal_error( const char * const msg )
{
if( verbosity >= 0 )
@@ -864,7 +874,6 @@ 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" */
- const char * input_filename = "";
const char * default_output_filename = "";
const char ** filenames = 0;
int num_filenames = 0;
@@ -877,8 +886,8 @@ int main( const int argc, const char * const argv[] )
bool force = false;
bool ignore_trailing = true;
bool keep_input_files = false;
- bool stdin_used = false;
bool recompress = false;
+ bool stdin_used = false;
bool to_stdout = false;
struct Pretty_print pp;
@@ -961,9 +970,6 @@ int main( const int argc, const char * const argv[] )
setmode( STDOUT_FILENO, O_BINARY );
#endif
- if( program_mode == m_test )
- outfd = -1;
-
num_filenames = max( 1, ap_arguments( &parser ) - argind );
filenames = resize_buffer( filenames, num_filenames * sizeof filenames[0] );
filenames[0] = "-";
@@ -974,6 +980,9 @@ int main( const int argc, const char * const argv[] )
if( strcmp( filenames[i], "-" ) != 0 ) filenames_given = true;
}
+ if( program_mode == m_test )
+ outfd = -1;
+
if( !to_stdout && program_mode != m_test &&
( filenames_given || default_output_filename[0] ) )
set_signals();
@@ -983,6 +992,7 @@ int main( const int argc, const char * const argv[] )
output_filename = resize_buffer( output_filename, 1 );
for( i = 0; i < num_filenames; ++i )
{
+ const char * input_filename = "";
int tmp;
struct stat in_stats;
const struct stat * in_statsp;
@@ -991,7 +1001,6 @@ int main( const int argc, const char * const argv[] )
if( !filenames[i][0] || strcmp( filenames[i], "-" ) == 0 )
{
if( stdin_used ) continue; else stdin_used = true;
- input_filename = "";
infd = STDIN_FILENO;
if( program_mode != m_test )
{
@@ -1018,8 +1027,7 @@ int main( const int argc, const char * const argv[] )
}
else
{
- const int eindex = extension_index( filenames[i] );
- input_filename = filenames[i];
+ const int eindex = extension_index( input_filename = filenames[i] );
infd = open_instream( input_filename, &in_stats, program_mode,
eindex, recompress, to_stdout );
if( infd < 0 ) { if( retval < 1 ) retval = 1; continue; }
@@ -1041,16 +1049,17 @@ int main( const int argc, const char * const argv[] )
}
}
- if( !check_tty( infd, program_mode ) )
+ Pp_set_name( &pp, input_filename );
+ if( !check_tty( pp.name, infd, program_mode ) )
{
if( retval < 1 ) retval = 1;
+ if( program_mode == m_test ) { close( infd ); infd = -1; continue; }
cleanup_and_fail( retval );
}
in_statsp = input_filename[0] ? &in_stats : 0;
- Pp_set_name( &pp, input_filename );
if( program_mode == m_compress )
- tmp = compress( &encoder_options, infd, &pp );
+ tmp = compress( &encoder_options, &pp, infd );
else
tmp = decompress( infd, &pp, ignore_trailing, program_mode == m_test );
if( tmp > retval ) retval = tmp;