summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--main.c101
1 files changed, 56 insertions, 45 deletions
diff --git a/main.c b/main.c
index 6d0fcc1..722ebe3 100644
--- a/main.c
+++ b/main.c
@@ -28,7 +28,7 @@
#include <errno.h>
#include <fcntl.h>
-#include <limits.h> /* SSIZE_MAX */
+#include <limits.h> /* CHAR_BIT, SSIZE_MAX */
#include <signal.h>
#include <stdbool.h>
#include <stdint.h> /* SIZE_MAX */
@@ -114,18 +114,18 @@ static void show_help( void )
printf( "Pdlzip is a permissively licensed implementation of the lzip data\n"
"compressor, intended for those who can't distribute (or even use) GPL\n"
"licensed Free Software. The name of pdlzip comes from 'public domain lzip'.\n"
- "Pdlzip is written in C and is compatible with lzip 1.4 or newer.\n"
+ "Pdlzip is written in C.\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 is better than gzip and bzip2 from a data recovery\n"
- "perspective. 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"
"\nPdlzip is also able to decompress legacy lzma-alone (.lzma) files.\n"
"Lzma-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"
@@ -226,6 +226,9 @@ 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 )
+ { 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 )
{
@@ -268,7 +271,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;
@@ -277,7 +280,7 @@ static void show_header( const unsigned dictionary_size )
}
-/* separate numbers of 5 or more digits in groups of 3 digits using '_' */
+/* separate numbers of 6 or more digits in groups of 3 digits using '_' */
static const char * format_num3( unsigned long long num )
{
enum { buffers = 8, bufsize = 4 * sizeof num, n = 10 };
@@ -289,7 +292,7 @@ static const char * format_num3( unsigned long long num )
char * const buf = buffer[current++]; current %= buffers;
char * p = buf + bufsize - 1; /* fill the buffer backwards */
*p = 0; /* terminator */
- if( num > 1024 )
+ if( num > 9999 )
{
char prefix = 0; /* try binary first, then si */
for( i = 0; i < n && num != 0 && num % 1024 == 0; ++i )
@@ -300,7 +303,7 @@ static const char * format_num3( unsigned long long num )
{ num /= 1000; prefix = si_prefix[i]; }
if( prefix ) *(--p) = prefix;
}
- const bool split = num >= 10000;
+ const bool split = num >= 100000;
for( i = 0; ; )
{
@@ -335,7 +338,7 @@ static unsigned 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] )
@@ -458,7 +461,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: %s: Input file already has '%s' suffix.\n",
+ fprintf( stderr, "%s: %s: Input file already has '%s' suffix, ignored.\n",
program_name, name, known_extensions[eindex].from );
return -1;
}
@@ -469,9 +472,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 )
@@ -616,7 +619,7 @@ static int compress( const int infd, const struct Lzma_options * const
}
if( writeblock( outfd, header, Lh_size ) != Lh_size )
- { show_error( "Can't write output file", errno, false ); retval = 1; }
+ { show_error( "Write error", errno, false ); retval = 1; }
else
if( LzmaEnc_Encode( encoder ) != 0 )
{ Pp_show_msg( pp, "Encoder error." ); retval = 1; }
@@ -684,7 +687,7 @@ static int lzma_decode( uint64_t unpackSize, CLzmaDec *decoder, const int infd,
unsigned long long member_size = lzma_header_size, data_size = 0;
uint8_t outBuf[OUT_BUF_SIZE];
int outPos = 0;
- const bool thereIsSize = (unpackSize != (uint64_t)-1);
+ const bool thereIsSize = unpackSize != (uint64_t)-1;
for (;;)
{
@@ -711,7 +714,7 @@ static int lzma_decode( uint64_t unpackSize, CLzmaDec *decoder, const int infd,
unpackSize -= outProcessed;
if( outfd >= 0 && writeblock( outfd, outBuf, outPos ) != outPos )
- { show_error( "Can't write output file", errno, false ); return 1; }
+ { show_error( "Write error", errno, false ); return 1; }
data_size += outPos;
outPos = 0;
@@ -734,7 +737,7 @@ static int lzma_decode( uint64_t unpackSize, CLzmaDec *decoder, const int infd,
static int lzip_decode( CLzmaDec *decoder, const int infd,
struct Pretty_print * const pp, uint8_t inBuf[],
int * const inPos, int * const inSize,
- const unsigned dictionary_size )
+ const unsigned dictionary_size, bool * const data0p )
{
unsigned long long member_size = Lh_size, data_size = 0;
uint8_t outBuf[OUT_BUF_SIZE];
@@ -762,7 +765,7 @@ static int lzip_decode( CLzmaDec *decoder, const int infd,
outPos += outProcessed;
if( outfd >= 0 && writeblock( outfd, outBuf, outPos ) != outPos )
- { show_error( "Can't write output file", errno, false ); return 1; }
+ { show_error( "Write error", errno, false ); return 1; }
CRC32_update_buf( &crc, outBuf, outPos );
data_size += outPos;
@@ -830,15 +833,16 @@ static int lzip_decode( CLzmaDec *decoder, const int infd,
}
if( error ) return 2;
show_results( data_size, member_size, td_crc, dictionary_size, true );
+ *data0p = data_size == 0;
return 0;
}
}
}
-static int decompress( const int infd, struct Pretty_print * const pp,
- const bool ignore_trailing, const bool loose_trailing,
- const bool testing )
+static int decompress( const int infd, const struct Cl_options * const cl_opts,
+ struct Pretty_print * const pp,
+ const bool from_stdin, const bool testing )
{
uint64_t unpackSize = 0;
CLzmaDec decoder;
@@ -848,6 +852,7 @@ static int decompress( const int infd, struct Pretty_print * const pp,
bool lzip_mode = true;
bool first_member;
uint8_t raw_props[lzma_header_size];
+ bool empty = false, multi = false;
for( first_member = true; ; first_member = false )
{
@@ -867,7 +872,7 @@ static int decompress( const int infd, struct Pretty_print * const pp,
else if( Lh_check_prefix( header, size ) )
{ Pp_show_msg( pp, "Truncated header in multimember file." );
retval = 2; }
- else if( size > 0 && !ignore_trailing )
+ else if( size > 0 && !cl_opts->ignore_trailing )
{ Pp_show_msg( pp, trailing_msg ); retval = 2; }
break;
}
@@ -875,10 +880,10 @@ static int decompress( const int infd, struct Pretty_print * const pp,
{
if( !first_member )
{
- if( !loose_trailing && Lh_check_corrupt( header ) )
+ if( !cl_opts->loose_trailing && Lh_check_corrupt( header ) )
{ Pp_show_msg( pp, "Corrupt header in multimember file." );
retval = 2; }
- else if( !ignore_trailing )
+ else if( !cl_opts->ignore_trailing )
{ Pp_show_msg( pp, trailing_msg ); retval = 2; }
break;
}
@@ -929,19 +934,23 @@ static int decompress( const int infd, struct Pretty_print * const pp,
if( !LzmaDec_Init( &decoder, raw_props ) )
{ Pp_show_msg( pp, mem_msg ); return 1; }
+ bool data0 = false;
if( lzip_mode )
retval = lzip_decode( &decoder, infd, pp, inBuf, &inPos, &inSize,
- dictionary_size );
+ dictionary_size, &data0 );
else
retval = lzma_decode( unpackSize, &decoder, infd, inBuf, &inPos,
&inSize, dictionary_size, testing );
LzmaDec_Free(&decoder);
if( retval != 0 || !lzip_mode ) break;
+ if( !from_stdin ) { multi = !first_member; if( data0 ) empty = true; }
if( verbosity >= 2 )
{ fputs( testing ? "ok\n" : "done\n", stderr ); Pp_reset( pp ); }
}
if( lzip_mode && verbosity == 1 && retval == 0 )
fputs( testing ? "ok\n" : "done\n", stderr );
+ if( empty && multi && retval == 0 )
+ { show_file_error( pp->name, empty_msg, 0 ); retval = 2; }
return retval;
}
@@ -1038,10 +1047,10 @@ int main( const int argc, const char * const argv[] )
const char * default_output_filename = "";
enum Mode program_mode = m_compress;
int i;
+ struct Cl_options cl_opts; /* command-line options */
+ Cl_options_init( &cl_opts );
bool force = false;
- bool ignore_trailing = true;
bool keep_input_files = false;
- bool loose_trailing = false;
bool recompress = false;
bool to_stdout = false;
if( argc > 0 ) invocation_name = argv[0];
@@ -1077,7 +1086,7 @@ int main( const int argc, const char * const argv[] )
{ 'v', "verbose", ap_no },
{ 'V', "version", ap_no },
{ opt_lt, "loose-trailing", ap_no },
- { 0, 0, ap_no } };
+ { 0, 0, ap_no } };
CRC32_init();
@@ -1097,11 +1106,11 @@ int main( const int argc, const char * const argv[] )
const char * const arg = ap_argument( &parser, argind );
switch( code )
{
- case '0': case '1': case '2': case '3': case '4':
- case '5': case '6': case '7': case '8': case '9':
+ case '0': case '1': case '2': case '3': case '4': case '5':
+ case '6': case '7': case '8': case '9':
encoder_options = option_mapping[code-'0']; break;
- case 'a': ignore_trailing = false; break;
- case 'b': break;
+ case 'a': cl_opts.ignore_trailing = false; break;
+ case 'b': break; /* ignored */
case 'c': to_stdout = true; break;
case 'd': set_mode( &program_mode, m_decompress ); break;
case 'f': force = true; break;
@@ -1110,17 +1119,17 @@ int main( const int argc, const char * const argv[] )
case 'k': keep_input_files = true; break;
case 'm': encoder_options.match_len_limit =
getnum( arg, pn, min_match_len_limit, max_match_len ); break;
- case 'n': break;
+ case 'n': break; /* ignored */
case 'o': if( strcmp( arg, "-" ) == 0 ) to_stdout = true;
else { default_output_filename = arg; } break;
case 'q': verbosity = -1; break;
case 's': encoder_options.dictionary_size = get_dict_size( arg, pn );
break;
- case 'S': break;
+ case 'S': break; /* ignored */
case 't': set_mode( &program_mode, m_test ); break;
case 'v': if( verbosity < 4 ) ++verbosity; break;
case 'V': show_version(); return 0;
- case opt_lt: loose_trailing = true; break;
+ case opt_lt: cl_opts.loose_trailing = true; break;
default: internal_error( "uncaught option." );
}
} /* end process options */
@@ -1168,9 +1177,10 @@ int main( const int argc, const char * const argv[] )
{
const char * input_filename = "";
int infd;
+ const bool from_stdin = strcmp( filenames[i], "-" ) == 0;
Pp_set_name( &pp, filenames[i] );
- if( strcmp( filenames[i], "-" ) == 0 )
+ if( from_stdin )
{
if( stdin_used ) continue; else stdin_used = true;
infd = STDIN_FILENO;
@@ -1215,7 +1225,7 @@ int main( const int argc, const char * const argv[] )
if( program_mode == m_compress )
tmp = compress( infd, &encoder_options, &pp );
else
- tmp = decompress( infd, &pp, ignore_trailing, loose_trailing,
+ tmp = decompress( infd, &cl_opts, &pp, from_stdin,
program_mode == m_test );
if( close( infd ) != 0 )
{ show_file_error( pp.name, "Error closing input file", errno );
@@ -1243,6 +1253,7 @@ int main( const int argc, const char * const argv[] )
program_name, failed_tests,
( failed_tests == 1 ) ? "file" : "files" );
free( output_filename );
+ Pp_free( &pp );
free( filenames );
ap_free( &parser );
return retval;