summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-01-23 05:40:38 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-01-23 05:40:38 +0000
commitb3b8c8ad67fc70d8d047e07e5768997882ec6e97 (patch)
treeb1b7b3a7a4ce69b1467a0e0882b19ce50e6a759a /main.c
parentReleasing debian version 1.12-6. (diff)
downloadpdlzip-b3b8c8ad67fc70d8d047e07e5768997882ec6e97.tar.xz
pdlzip-b3b8c8ad67fc70d8d047e07e5768997882ec6e97.zip
Merging upstream version 1.13~rc1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'main.c')
-rw-r--r--main.c177
1 files changed, 87 insertions, 90 deletions
diff --git a/main.c b/main.c
index b0fab06..0d3b659 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-2022 Antonio Diaz Diaz.
+ Copyright (C) 2010-2023 Antonio Diaz Diaz.
This program is free software. Redistribution and use in source and
binary forms, with or without modification, are permitted provided
@@ -19,19 +19,19 @@
*/
/*
Exit status: 0 for a normal exit, 1 for environmental problems
- (file not found, invalid flags, I/O errors, etc), 2 to indicate a
- corrupt or invalid input file, 3 for an internal consistency error
- (e.g., bug) which caused pdlzip to panic.
+ (file not found, invalid command-line options, I/O errors, etc), 2 to
+ indicate a corrupt or invalid input file, 3 for an internal consistency
+ error (e.g., bug) which caused pdlzip to panic.
*/
#define _FILE_OFFSET_BITS 64
#include <errno.h>
#include <fcntl.h>
-#include <limits.h>
+#include <limits.h> /* SSIZE_MAX */
#include <signal.h>
#include <stdbool.h>
-#include <stdint.h>
+#include <stdint.h> /* SIZE_MAX */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -85,7 +85,7 @@ static void show_file_error( const char * const filename,
static void internal_error( const char * const msg );
static const char * const program_name = "pdlzip";
-static const char * const program_year = "2022";
+static const char * const program_year = "2023";
static const char * invocation_name = "pdlzip"; /* default value */
static const struct { const char * from; const char * to; } known_extensions[] = {
@@ -117,14 +117,15 @@ static void show_help( void )
"Pdlzip is written in C and is (hope)fully compatible with lzip 1.4 or newer.\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 and provides a 3 factor integrity\n"
- "checking to maximize interoperability and optimize safety. Lzip can compress\n"
- "about as fast as gzip (lzip -0) or compress most files more than bzip2\n"
- "(lzip -9). Decompression speed is intermediate between gzip and bzip2.\n"
- "Lzip is better than gzip and bzip2 from a data recovery perspective. Lzip\n"
- "has been designed, written, and tested with great care to replace gzip and\n"
- "bzip2 as the standard general-purpose compressed format for unix-like\n"
- "systems.\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"
"\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"
@@ -136,7 +137,7 @@ static void show_help( void )
" -V, --version output version information and exit\n"
" -a, --trailing-error exit with error status if trailing data\n"
" -c, --stdout write to standard output, keep input files\n"
- " -d, --decompress decompress\n"
+ " -d, --decompress decompress, test compressed file integrity\n"
" -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"
@@ -154,19 +155,19 @@ static void show_help( void )
"decompresses from standard input to standard output.\n"
"Numbers may be followed by a multiplier: k = kB = 10^3 = 1000,\n"
"Ki = KiB = 2^10 = 1024, M = 10^6, Mi = 2^20, G = 10^9, Gi = 2^30, etc...\n"
- "Dictionary sizes 12 to 27 are interpreted as powers of two, meaning 2^12\n"
- "to 2^27 bytes.\n"
- "\nThe 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 options --dictionary-size and --match-length\n"
- "directly to achieve optimal performance. For example, -9m64 usually\n"
- "compresses executables more (and faster) than -9.\n"
+ "Dictionary sizes 12 to 27 are interpreted as powers of two, meaning 2^12 to\n"
+ "2^27 bytes.\n"
+ "\nThe bidimensional parameter space of LZMA can't be mapped to a linear scale\n"
+ "optimal for all files. If your files are large, very repetitive, etc, you\n"
+ "may need to use the options --dictionary-size and --match-length directly\n"
+ "to achieve optimal performance. For example, -9m64 usually compresses\n"
+ "executables more (and faster) than -9.\n"
"\nTo extract all the files from archive 'foo.tar.lz', use the commands\n"
"'tar -xf foo.tar.lz' or 'pdlzip -cd foo.tar.lz | tar -xf -'.\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 (e.g., bug) which\n"
- "caused pdlzip to panic.\n"
+ "\nExit status: 0 for a normal exit, 1 for environmental problems\n"
+ "(file not found, invalid command-line options, I/O errors, etc), 2 to\n"
+ "indicate a corrupt or invalid input file, 3 for an internal consistency\n"
+ "error (e.g., bug) which caused pdlzip to panic.\n"
"\nPdlzip includes public domain compression/decompression code from the LZMA\n"
"SDK (Software Development Kit) written by Igor Pavlov.\n"
"\nReport bugs to lzip-bug@nongnu.org\n"
@@ -262,27 +263,26 @@ static void Pp_show_msg( struct Pretty_print * const pp, const char * const msg
static void show_header( const unsigned dictionary_size )
{
- enum { factor = 1024 };
- const char * const prefix[8] =
- { "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" };
+ enum { factor = 1024, n = 3 };
+ const char * const prefix[n] = { "Ki", "Mi", "Gi" };
const char * p = "";
const char * np = " ";
unsigned num = dictionary_size;
bool exact = ( num % factor == 0 );
- int i; for( i = 0; i < 8 && ( num > 9999 || ( exact && num >= factor ) ); ++i )
+ int i; for( i = 0; i < n && ( num > 9999 || ( exact && num >= factor ) ); ++i )
{ num /= factor; if( num % factor != 0 ) exact = false;
p = prefix[i]; np = ""; }
fprintf( stderr, "dict %s%4u %sB, ", np, num, p );
}
-/* separate large numbers >= 100_000 in groups of 3 digits using '_' */
+/* separate numbers of 5 or more digits in groups of 3 digits using '_' */
static const char * format_num3( unsigned long long num )
{
- const char * const si_prefix = "kMGTPEZY";
- const char * const binary_prefix = "KMGTPEZY";
- enum { buffers = 8, bufsize = 4 * sizeof (long long) };
+ enum { buffers = 8, bufsize = 4 * sizeof num, n = 10 };
+ const char * const si_prefix = "kMGTPEZYRQ";
+ const char * const binary_prefix = "KMGTPEZYRQ";
static char buffer[buffers][bufsize]; /* circle of static buffers for printf */
static int current = 0;
int i;
@@ -292,15 +292,15 @@ static const char * format_num3( unsigned long long num )
if( num > 1024 )
{
char prefix = 0; /* try binary first, then si */
- for( i = 0; i < 8 && num >= 1024 && num % 1024 == 0; ++i )
+ for( i = 0; i < n && num != 0 && num % 1024 == 0; ++i )
{ num /= 1024; prefix = binary_prefix[i]; }
if( prefix ) *(--p) = 'i';
else
- for( i = 0; i < 8 && num >= 1000 && num % 1000 == 0; ++i )
+ for( i = 0; i < n && num != 0 && num % 1000 == 0; ++i )
{ num /= 1000; prefix = si_prefix[i]; }
if( prefix ) *(--p) = prefix;
}
- const bool split = num >= 100000;
+ const bool split = num >= 10000;
for( i = 0; ; )
{
@@ -311,6 +311,16 @@ static const char * format_num3( unsigned long long num )
}
+void show_option_error( const char * const arg, const char * const msg,
+ const char * const option_name )
+ {
+ if( verbosity >= 0 )
+ fprintf( stderr, "%s: '%s': %s option '%s'.\n",
+ program_name, arg, msg, option_name );
+ }
+
+
+/* Recognized formats: <num>k, <num>Ki, <num>[MGTPEZYRQ][i] */
static unsigned long getnum( const char * const arg,
const char * const option_name,
const unsigned long llimit,
@@ -320,12 +330,8 @@ static unsigned long getnum( const char * const arg,
errno = 0;
unsigned long result = strtoul( arg, &tail, 0 );
if( tail == arg )
- {
- if( verbosity >= 0 )
- fprintf( stderr, "%s: Bad or missing numerical argument in "
- "option '%s'.\n", program_name, option_name );
- exit( 1 );
- }
+ { show_option_error( arg, "Bad or missing numerical argument in",
+ option_name ); exit( 1 ); }
if( !errno && tail[0] )
{
@@ -334,6 +340,8 @@ static unsigned long getnum( const char * const arg,
int i;
switch( tail[0] )
{
+ case 'Q': exponent = 10; break;
+ case 'R': exponent = 9; break;
case 'Y': exponent = 8; break;
case 'Z': exponent = 7; break;
case 'E': exponent = 6; break;
@@ -345,12 +353,8 @@ static unsigned long getnum( const char * const arg,
case 'k': if( factor == 1000 ) exponent = 1; break;
}
if( exponent <= 0 )
- {
- if( verbosity >= 0 )
- fprintf( stderr, "%s: Bad multiplier in numerical argument of "
- "option '%s'.\n", program_name, option_name );
- exit( 1 );
- }
+ { show_option_error( arg, "Bad multiplier in numerical argument of",
+ option_name ); exit( 1 ); }
for( i = 0; i < exponent; ++i )
{
if( ulimit / factor >= result ) result *= factor;
@@ -361,8 +365,8 @@ static unsigned long getnum( const char * const arg,
if( errno )
{
if( verbosity >= 0 )
- fprintf( stderr, "%s: Numerical argument out of limits [%s,%s] "
- "in option '%s'.\n", program_name, format_num3( llimit ),
+ fprintf( stderr, "%s: '%s': Value out of limits [%s,%s] in "
+ "option '%s'.\n", program_name, arg, format_num3( llimit ),
format_num3( ulimit ), option_name );
exit( 1 );
}
@@ -442,7 +446,7 @@ static void set_d_outname( const char * const name, const int eindex )
strcpy( output_filename, name );
strcat( output_filename, ".out" );
if( verbosity >= 1 )
- fprintf( stderr, "%s: Can't guess original name for '%s' -- using '%s'\n",
+ fprintf( stderr, "%s: %s: Can't guess original name -- using '%s'\n",
program_name, name, output_filename );
}
@@ -454,7 +458,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: Input file '%s' already has '%s' suffix.\n",
+ fprintf( stderr, "%s: %s: Input file already has '%s' suffix.\n",
program_name, name, known_extensions[eindex].from );
return -1;
}
@@ -471,9 +475,9 @@ static int open_instream( const char * const name, struct stat * const in_statsp
if( i != 0 || ( !S_ISREG( mode ) && ( !can_read || one_to_one ) ) )
{
if( verbosity >= 0 )
- fprintf( stderr, "%s: Input file '%s' is not a regular file%s.\n",
+ fprintf( stderr, "%s: %s: Input file is not a regular file%s.\n",
program_name, name, ( can_read && one_to_one ) ?
- ",\n and neither '-c' nor '-o' were specified" : "" );
+ ",\n and neither '-c' nor '-o' were specified" : "" );
close( infd );
infd = -1;
}
@@ -492,16 +496,12 @@ static bool open_outstream( const bool force, const bool protect )
outfd = open( output_filename, flags, outfd_mode );
if( outfd >= 0 ) delete_output_on_interrupt = true;
- else if( verbosity >= 0 )
- {
- if( errno == EEXIST )
- fprintf( stderr, "%s: Output file '%s' already exists, skipping.\n",
- program_name, output_filename );
- else
- fprintf( stderr, "%s: Can't create output file '%s': %s\n",
- program_name, output_filename, strerror( errno ) );
- }
- return ( outfd >= 0 );
+ else if( errno == EEXIST )
+ show_file_error( output_filename,
+ "Output file already exists, skipping.", 0 );
+ else
+ show_file_error( output_filename, "Can't create output file", errno );
+ return outfd >= 0;
}
@@ -519,12 +519,10 @@ static void cleanup_and_fail( const int retval )
if( delete_output_on_interrupt )
{
delete_output_on_interrupt = false;
- if( verbosity >= 0 )
- fprintf( stderr, "%s: Deleting output file '%s', if it exists.\n",
- program_name, output_filename );
+ show_file_error( output_filename, "Deleting output file, if it exists.", 0 );
if( outfd >= 0 ) { close( outfd ); outfd = -1; }
if( remove( output_filename ) != 0 && errno != ENOENT )
- show_error( "WARNING: deletion of output file (apparently) failed.", 0, false );
+ show_error( "warning: deletion of output file failed", errno, false );
}
exit( retval );
}
@@ -578,10 +576,8 @@ static void close_and_set_permissions( const struct stat * const in_statsp )
warning = true;
}
if( close( outfd ) != 0 )
- {
- show_error( "Error closing output file", errno, false );
- cleanup_and_fail( 1 );
- }
+ { show_file_error( output_filename, "Error closing output file", errno );
+ cleanup_and_fail( 1 ); }
outfd = -1;
delete_output_on_interrupt = false;
if( in_statsp )
@@ -592,12 +588,13 @@ static void close_and_set_permissions( const struct stat * const in_statsp )
if( utime( output_filename, &t ) != 0 ) warning = true;
}
if( warning && verbosity >= 1 )
- show_error( "Can't change output file attributes.", 0, false );
+ show_file_error( output_filename,
+ "warning: can't change output file attributes", errno );
}
-static int compress( const struct Lzma_options * const encoder_options,
- struct Pretty_print * const pp, const int infd )
+static int compress( const int infd, const struct Lzma_options * const
+ encoder_options, struct Pretty_print * const pp )
{
int retval = 0;
CLzmaEncHandle encoder = 0;
@@ -855,7 +852,7 @@ static int decompress( const int infd, struct Pretty_print * const pp,
for( first_member = true; ; first_member = false )
{
int i;
- unsigned dictionary_size = 0; /* keep gcc 3.3.6 happy */
+ unsigned dictionary_size = 0; /* keep gcc 3.3.6 quiet */
Lzip_header header;
if( inSize - inPos < lzma_header_size &&
!read_inbuf( infd, inBuf, &inPos, &inSize ) ) return 1;
@@ -867,18 +864,18 @@ static int decompress( const int infd, struct Pretty_print * const pp,
if( first_member )
{ show_file_error( pp->name, "File ends unexpectedly at member header.", 0 );
retval = 2; }
- else if( Lh_verify_prefix( header, size ) )
+ else if( Lh_check_prefix( header, size ) )
{ Pp_show_msg( pp, "Truncated header in multimember file." );
retval = 2; }
else if( size > 0 && !ignore_trailing )
{ Pp_show_msg( pp, trailing_msg ); retval = 2; }
break;
}
- if( !Lh_verify_magic( header ) )
+ if( !Lh_check_magic( header ) )
{
if( !first_member )
{
- if( !loose_trailing && Lh_verify_corrupt( header ) )
+ if( !loose_trailing && Lh_check_corrupt( header ) )
{ Pp_show_msg( pp, "Corrupt header in multimember file." );
retval = 2; }
else if( !ignore_trailing )
@@ -909,7 +906,7 @@ static int decompress( const int infd, struct Pretty_print * const pp,
}
if( lzip_mode )
{
- if( !Lh_verify_version( header ) )
+ if( !Lh_check_version( header ) )
{
if( verbosity >= 0 )
{ Pp_show_msg( pp, 0 );
@@ -1023,8 +1020,8 @@ static void internal_error( const char * const msg )
int main( const int argc, const char * const argv[] )
{
- /* Mapping from gzip/bzip2 style 1..9 compression modes
- to the corresponding LZMA compression modes. */
+ /* Mapping from gzip/bzip2 style 0..9 compression levels to the
+ corresponding LZMA compression parameters. */
const struct Lzma_options option_mapping[] =
{
{ 1 << 16, 5 }, /* -0 */
@@ -1124,7 +1121,7 @@ int main( const int argc, const char * const argv[] )
case 'v': if( verbosity < 4 ) ++verbosity; break;
case 'V': show_version(); return 0;
case opt_lt: loose_trailing = true; break;
- default : internal_error( "uncaught option." );
+ default: internal_error( "uncaught option." );
}
} /* end process options */
@@ -1187,7 +1184,7 @@ int main( const int argc, const char * const argv[] )
eindex, one_to_one, recompress );
if( infd < 0 ) { set_retval( &retval, 1 ); continue; }
if( !check_tty_in( pp.name, infd, program_mode, &retval ) ) continue;
- if( one_to_one ) /* open outfd after verifying infd */
+ if( one_to_one ) /* open outfd after checking infd */
{
if( program_mode == m_compress )
set_c_outname( input_filename, true, true );
@@ -1200,7 +1197,7 @@ int main( const int argc, const char * const argv[] )
if( one_to_one && !check_tty_out( program_mode ) )
{ set_retval( &retval, 1 ); return retval; } /* don't delete a tty */
- if( to_file && outfd < 0 ) /* open outfd after verifying infd */
+ if( to_file && outfd < 0 ) /* open outfd after checking infd */
{
if( program_mode == m_compress ) set_c_outname( default_output_filename,
filenames_given, false );
@@ -1216,10 +1213,10 @@ int main( const int argc, const char * const argv[] )
( input_filename[0] && one_to_one ) ? &in_stats : 0;
int tmp;
if( program_mode == m_compress )
- tmp = compress( &encoder_options, &pp, infd );
+ tmp = compress( infd, &encoder_options, &pp );
else
- tmp = decompress( infd, &pp, ignore_trailing,
- loose_trailing, program_mode == m_test );
+ tmp = decompress( infd, &pp, ignore_trailing, loose_trailing,
+ program_mode == m_test );
if( close( infd ) != 0 )
{ show_file_error( pp.name, "Error closing input file", errno );
set_retval( &tmp, 1 ); }