summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2017-05-07 15:49:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2017-05-07 15:49:03 +0000
commit1adf9f41888bd0630227a02cf387f6b5d9a13227 (patch)
tree6eb2672c16756c89f04790466061ce12e9b46f86 /main.c
parentAdding upstream version 1.8. (diff)
downloadclzip-1adf9f41888bd0630227a02cf387f6b5d9a13227.tar.xz
clzip-1adf9f41888bd0630227a02cf387f6b5d9a13227.zip
Adding upstream version 1.9.upstream/1.9
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--main.c267
1 files changed, 149 insertions, 118 deletions
diff --git a/main.c b/main.c
index ecf8dd8..1af09f3 100644
--- a/main.c
+++ b/main.c
@@ -1,5 +1,5 @@
/* Clzip - LZMA lossless data compressor
- Copyright (C) 2010-2016 Antonio Diaz Diaz.
+ Copyright (C) 2010-2017 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
@@ -71,10 +71,10 @@ int verbosity = 0;
const char * const Program_name = "Clzip";
const char * const program_name = "clzip";
-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" },
{ 0, 0 } };
@@ -85,7 +85,7 @@ struct Lzma_options
int match_len_limit; /* 5 .. 273 */
};
-enum Mode { m_compress, m_decompress, m_test };
+enum Mode { m_compress, m_decompress, m_list, m_test };
char * output_filename = 0;
int outfd = -1;
@@ -106,6 +106,7 @@ static void show_help( void )
" -f, --force overwrite existing output files\n"
" -F, --recompress force re-compression of compressed files\n"
" -k, --keep keep (don't delete) input files\n"
+ " -l, --list print (un)compressed file sizes\n"
" -m, --match-length=<bytes> set match length limit in bytes [36]\n"
" -o, --output=<file> if reading standard input, write to <file>\n"
" -q, --quiet suppress all messages\n"
@@ -145,23 +146,38 @@ static void show_version( void )
}
+const char * bad_version( const unsigned version )
+ {
+ static char buf[80];
+ snprintf( buf, sizeof buf, "Version %u member format not supported.",
+ version );
+ return buf;
+ }
+
+
+const char * format_ds( const unsigned dictionary_size )
+ {
+ enum { bufsize = 16, factor = 1024 };
+ static char buf[bufsize];
+ const char * const prefix[8] =
+ { "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" };
+ 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 = ""; }
+ snprintf( buf, bufsize, "%s%4u %sB", np, num, p );
+ return buf;
+ }
+
+
static void show_header( const unsigned dictionary_size )
{
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 );
- }
+ fprintf( stderr, "dictionary %s. ", format_ds( dictionary_size ) );
}
@@ -181,8 +197,8 @@ static unsigned long long getnum( const char * const ptr,
if( !errno && tail[0] )
{
- const int factor = ( tail[1] == 'i' ) ? 1024 : 1000;
- int exponent = 0; /* 0 = bad multiplier */
+ const unsigned factor = ( tail[1] == 'i' ) ? 1024 : 1000;
+ int exponent = 0; /* 0 = bad multiplier */
int i;
switch( tail[0] )
{
@@ -220,7 +236,7 @@ static unsigned long 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 )
return ( 1 << bits );
@@ -228,68 +244,79 @@ static int get_dict_size( const char * const arg )
}
+void set_mode( enum Mode * const program_modep, const enum Mode new_mode )
+ {
+ if( *program_modep != m_compress && *program_modep != new_mode )
+ {
+ show_error( "Only one operation can be specified.", 0, true );
+ exit( 1 );
+ }
+ *program_modep = new_mode;
+ }
+
+
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;
}
-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 )
+int open_instream( const char * const name, struct stat * const in_statsp,
+ const bool no_ofile, const bool reg_only )
{
- int infd = -1;
- if( program_mode == m_compress && !recompress && eindex >= 0 )
- {
- if( verbosity >= 0 )
- fprintf( stderr, "%s: Input file '%s' already has '%s' suffix.\n",
- program_name, name, known_extensions[eindex].from );
- }
+ int infd = open( name, O_RDONLY | O_BINARY );
+ if( infd < 0 )
+ show_file_error( name, "Can't open input file", errno );
else
{
- infd = open( name, O_RDONLY | O_BINARY );
- if( infd < 0 )
+ const int i = fstat( infd, in_statsp );
+ const mode_t mode = in_statsp->st_mode;
+ const bool can_read = ( i == 0 && !reg_only &&
+ ( S_ISBLK( mode ) || S_ISCHR( mode ) ||
+ S_ISFIFO( mode ) || S_ISSOCK( mode ) ) );
+ if( i != 0 || ( !S_ISREG( mode ) && ( !can_read || !no_ofile ) ) )
{
if( verbosity >= 0 )
- fprintf( stderr, "%s: Can't open input file '%s': %s\n",
- program_name, name, strerror( errno ) );
- }
- else
- {
- 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 no_ofile = ( to_stdout || program_mode == m_test );
- if( i != 0 || ( !S_ISREG( mode ) && ( !can_read || !no_ofile ) ) )
- {
- if( verbosity >= 0 )
- fprintf( stderr, "%s: Input file '%s' is not a regular file%s.\n",
- program_name, name,
- ( can_read && !no_ofile ) ?
- ",\n and '--stdout' was not specified" : "" );
- close( infd );
- infd = -1;
- }
+ fprintf( stderr, "%s: Input file '%s' is not a regular file%s.\n",
+ program_name, name,
+ ( can_read && !no_ofile ) ?
+ ",\n and '--stdout' was not specified" : "" );
+ close( infd );
+ infd = -1;
}
}
return infd;
}
+static int open_instream2( const char * const name, struct stat * const in_statsp,
+ const enum Mode program_mode, const int eindex,
+ const bool recompress, const bool to_stdout )
+ {
+ const bool no_ofile = ( to_stdout || program_mode == m_test );
+ if( program_mode == m_compress && !recompress && eindex >= 0 )
+ {
+ if( verbosity >= 0 )
+ fprintf( stderr, "%s: Input file '%s' already has '%s' suffix.\n",
+ program_name, name, known_extensions[eindex].from );
+ return -1;
+ }
+ return open_instream( name, in_statsp, no_ofile, false );
+ }
+
+
/* assure at least a minimum size for buffer 'buf' */
-static void * resize_buffer( void * buf, const int min_size )
+void * resize_buffer( void * buf, const unsigned min_size )
{
if( buf ) buf = realloc( buf, min_size );
else buf = malloc( min_size );
@@ -312,19 +339,19 @@ static void set_c_outname( const char * const name, const bool multifile )
}
-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;
}
}
@@ -360,7 +387,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 ) )
{
@@ -370,7 +398,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;
@@ -467,7 +496,7 @@ static int compress( const unsigned long long member_size,
bool error = false;
if( zero )
{
- encoder.fe = (struct FLZ_encoder *)malloc( sizeof (struct FLZ_encoder) );
+ encoder.fe = (struct FLZ_encoder *)malloc( sizeof *encoder.fe );
if( !encoder.fe || !FLZe_init( encoder.fe, infd, outfd ) ) error = true;
else encoder.eb = &encoder.fe->eb;
}
@@ -477,7 +506,7 @@ static int compress( const unsigned long long member_size,
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 )
- encoder.e = (struct LZ_encoder *)malloc( sizeof (struct LZ_encoder) );
+ encoder.e = (struct LZ_encoder *)malloc( sizeof *encoder.e );
else internal_error( "invalid argument to encoder." );
if( !encoder.e || !LZe_init( encoder.e, Fh_get_dictionary_size( header ),
encoder_options->match_len_limit, infd, outfd ) )
@@ -538,10 +567,10 @@ static int compress( const unsigned long long member_size,
}
-static unsigned char xdigit( const int value )
+static unsigned char xdigit( const unsigned value )
{
- if( value >= 0 && value <= 9 ) return '0' + value;
- if( value >= 10 && value <= 15 ) return 'A' + value - 10;
+ if( value <= 9 ) return '0' + value;
+ if( value <= 15 ) return 'A' + value - 10;
return 0;
}
@@ -554,28 +583,21 @@ static bool show_trailing_data( const uint8_t * const data, const int size,
{
int i;
char buf[80];
- int len = snprintf( buf, sizeof buf, "%strailing data = ",
- all ? "" : "first bytes of " );
- bool text = true;
- for( i = 0; i < size; ++i )
- if( !isprint( data[i] ) ) { text = false; break; }
- if( text )
+ unsigned len = max( 0, snprintf( buf, sizeof buf, "%strailing data = ",
+ all ? "" : "first bytes of " ) );
+ for( i = 0; i < size && len + 2 < sizeof buf; ++i )
{
- if( len > 0 && len < (int)sizeof buf )
- snprintf( buf + len, sizeof buf - len, "'%.*s'", size, (const char *)data );
- }
- else
- {
- for( i = 0; i < size && len > 0 && len + 3 < (int)sizeof buf; ++i )
- {
- if( i > 0 ) buf[len++] = ' ';
- buf[len++] = xdigit( data[i] >> 4 );
- buf[len++] = xdigit( data[i] & 0x0F );
- buf[len] = 0;
- }
+ buf[len++] = xdigit( data[i] >> 4 );
+ buf[len++] = xdigit( data[i] & 0x0F );
+ buf[len++] = ' ';
}
+ if( len < sizeof buf ) buf[len++] = '\'';
+ for( i = 0; i < size && len < sizeof buf; ++i )
+ { if( isprint( data[i] ) ) buf[len++] = data[i]; else buf[len++] = '.'; }
+ if( len < sizeof buf ) buf[len++] = '\'';
+ if( len < sizeof buf ) buf[len] = 0; else buf[sizeof buf - 1] = 0;
Pp_show_msg( pp, buf );
- if( !ignore_trailing ) show_error( "Trailing data not allowed.", 0, false );
+ if( !ignore_trailing ) show_file_error( pp->name, trailing_msg, 0 );
}
return ignore_trailing;
}
@@ -615,24 +637,19 @@ static int decompress( const int infd, struct Pretty_print * const pp,
if( !Fh_verify_magic( header ) )
{
if( first_member )
- { Pp_show_msg( pp, "Bad magic number (file not in lzip format)." );
- retval = 2; }
+ { show_file_error( pp->name, bad_magic_msg, 0 ); retval = 2; }
else if( !show_trailing_data( header, size, pp, false, ignore_trailing ) )
retval = 2;
break;
}
if( !Fh_verify_version( header ) )
{
- if( verbosity >= 0 )
- { Pp_show_msg( pp, 0 );
- fprintf( stderr, "Version %d member format not supported.\n",
- Fh_version( header ) ); }
+ Pp_show_msg( pp, bad_version( Fh_version( header ) ) );
retval = 2; break;
}
dictionary_size = Fh_get_dictionary_size( header );
if( !isvalid_ds( dictionary_size ) )
- { Pp_show_msg( pp, "Invalid dictionary size in member header." );
- retval = 2; break; }
+ { Pp_show_msg( pp, bad_dict_msg ); retval = 2; break; }
if( verbosity >= 2 || ( verbosity == 1 && first_member ) )
{ Pp_show_msg( pp, 0 ); show_header( dictionary_size ); }
@@ -693,6 +710,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 )
@@ -746,7 +773,6 @@ int main( const int argc, const char * const argv[] )
const unsigned long long max_volume_size = 0x4000000000000000ULL;
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;
int num_filenames = 0;
@@ -759,8 +785,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;
bool zero = false;
struct Pretty_print pp;
@@ -785,6 +811,7 @@ int main( const int argc, const char * const argv[] )
{ 'F', "recompress", ap_no },
{ 'h', "help", ap_no },
{ 'k', "keep", ap_no },
+ { 'l', "list", ap_no },
{ 'm', "match-length", ap_yes },
{ 'n', "threads", ap_yes },
{ 'o', "output", ap_yes },
@@ -794,7 +821,7 @@ int main( const int argc, const char * const argv[] )
{ 't', "test", ap_no },
{ 'v', "verbose", ap_no },
{ 'V', "version", ap_no },
- { 0 , 0, ap_no } };
+ { 0 , 0, ap_no } };
struct Arg_parser parser;
@@ -820,11 +847,12 @@ int main( const int argc, const char * const argv[] )
case 'a': ignore_trailing = false; 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 'd': set_mode( &program_mode, m_decompress ); break;
case 'f': force = true; break;
case 'F': recompress = true; break;
case 'h': show_help(); return 0;
case 'k': keep_input_files = true; break;
+ case 'l': set_mode( &program_mode, m_list ); break;
case 'm': encoder_options.match_len_limit =
getnum( arg, min_match_len_limit, max_match_len );
zero = false; break;
@@ -834,7 +862,7 @@ int main( const int argc, const char * const argv[] )
case 's': encoder_options.dictionary_size = get_dict_size( arg );
zero = false; break;
case 'S': volume_size = getnum( arg, 100000, max_volume_size ); break;
- case 't': program_mode = m_test; break;
+ case 't': set_mode( &program_mode, m_test ); break;
case 'v': if( verbosity < 4 ) ++verbosity; break;
case 'V': show_version(); return 0;
default : internal_error( "uncaught option." );
@@ -846,14 +874,6 @@ int main( const int argc, const char * const argv[] )
setmode( STDOUT_FILENO, O_BINARY );
#endif
- if( program_mode == m_test )
- outfd = -1;
- else if( program_mode == m_compress )
- {
- Dis_slots_init();
- Prob_prices_init();
- }
-
num_filenames = max( 1, ap_arguments( &parser ) - argind );
filenames = resize_buffer( filenames, num_filenames * sizeof filenames[0] );
filenames[0] = "-";
@@ -864,6 +884,17 @@ int main( const int argc, const char * const argv[] )
if( strcmp( filenames[i], "-" ) != 0 ) filenames_given = true;
}
+ if( program_mode == m_list )
+ return list_files( filenames, num_filenames, ignore_trailing );
+
+ if( program_mode == m_test )
+ outfd = -1;
+ else if( program_mode == m_compress )
+ {
+ Dis_slots_init();
+ Prob_prices_init();
+ }
+
if( !to_stdout && program_mode != m_test &&
( filenames_given || default_output_filename[0] ) )
set_signals();
@@ -873,6 +904,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;
@@ -881,7 +913,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 )
{
@@ -908,10 +939,9 @@ int main( const int argc, const char * const argv[] )
}
else
{
- const int eindex = extension_index( filenames[i] );
- input_filename = filenames[i];
- infd = open_instream( input_filename, &in_stats, program_mode,
- eindex, recompress, to_stdout );
+ const int eindex = extension_index( input_filename = filenames[i] );
+ infd = open_instream2( input_filename, &in_stats, program_mode,
+ eindex, recompress, to_stdout );
if( infd < 0 ) { if( retval < 1 ) retval = 1; continue; }
if( program_mode != m_test )
{
@@ -931,14 +961,15 @@ 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( member_size, volume_size, infd, &encoder_options, &pp,
in_statsp, zero );