1
0
Fork 0

Adding upstream version 1.1.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-17 21:57:03 +01:00
parent 96d5caab8e
commit 9c0f4fe9b6
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
15 changed files with 284 additions and 262 deletions

175
main.c
View file

@ -1,5 +1,5 @@
/* Lunzip - Decompressor for lzip files
Copyright (C) 2010, 2011 Antonio Diaz Diaz.
Copyright (C) 2010, 2011, 2012 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
@ -24,14 +24,14 @@
#define _FILE_OFFSET_BITS 64
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <stdint.h>
#include <unistd.h>
#include <utime.h>
#include <sys/stat.h>
@ -63,7 +63,7 @@ long long int llabs( long long int number );
const char * const Program_name = "Lunzip";
const char * const program_name = "lunzip";
const char * const program_year = "2011";
const char * const program_year = "2012";
const char * invocation_name = 0;
#ifdef O_BINARY
@ -80,11 +80,13 @@ struct { const char * from; const char * to; } const known_extensions[] = {
char * output_filename = 0;
int outfd = -1;
int verbosity = 0;
const mode_t usr_rw = S_IRUSR | S_IWUSR;
const mode_t all_rw = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
mode_t outfd_mode = S_IRUSR | S_IWUSR;
bool delete_output_on_interrupt = false;
/* assure at least a minimum size for buffer `buf' */
/* assure at least a minimum size for buffer 'buf' */
static void * resize_buffer( void * buf, const int min_size )
{
if( buf ) buf = realloc( buf, min_size );
@ -102,21 +104,21 @@ static void show_help()
{
printf( "%s - Decompressor for lzip files.\n", Program_name );
printf( "\nUsage: %s [options] [files]\n", invocation_name );
printf( "\nOptions:\n" );
printf( " -h, --help display this help and exit\n" );
printf( " -V, --version output version information and exit\n" );
printf( " -c, --stdout send output to standard output\n" );
printf( " -d, --decompress decompress (this is the default)\n" );
printf( " -f, --force overwrite existing output files\n" );
printf( " -k, --keep keep (don't delete) input files\n" );
printf( " -o, --output=<file> if reading stdin, place the output into <file>\n" );
printf( " -q, --quiet suppress all messages\n" );
printf( " -t, --test test compressed file integrity\n" );
printf( " -v, --verbose be verbose (a 2nd -v gives more)\n" );
printf( "If no file names are given, %s decompresses from standard input to\n", program_name );
printf( "standard output.\n" );
printf( "\nReport bugs to lzip-bug@nongnu.org\n" );
printf( "Lunzip home page: http://www.nongnu.org/lzip/lunzip.html\n" );
printf( "\nOptions:\n"
" -h, --help display this help and exit\n"
" -V, --version output version information and exit\n"
" -c, --stdout send output to standard output\n"
" -d, --decompress decompress (this is the default)\n"
" -f, --force overwrite existing output files\n"
" -k, --keep keep (don't delete) input files\n"
" -o, --output=<file> if reading stdin, place the output into <file>\n"
" -q, --quiet suppress all messages\n"
" -t, --test test compressed file integrity\n"
" -v, --verbose be verbose (a 2nd -v gives more)\n"
"If no file names are given, lunzip decompresses from standard input to\n"
"standard output.\n"
"\nReport bugs to lzip-bug@nongnu.org\n"
"Lunzip home page: http://www.nongnu.org/lzip/lunzip.html\n" );
}
@ -124,9 +126,9 @@ static void show_version()
{
printf( "%s %s\n", Program_name, PROGVERSION );
printf( "Copyright (C) %s Antonio Diaz Diaz.\n", program_year );
printf( "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n" );
printf( "This is free software: you are free to change and redistribute it.\n" );
printf( "There is NO WARRANTY, to the extent permitted by law.\n" );
printf( "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n" );
}
@ -137,11 +139,12 @@ static const char * format_num( long long num )
enum { buf_size = 16, factor = 1024 };
static char buf[buf_size];
const char *p = "";
bool exact = ( num % factor == 0 );
int i;
for( i = 0; i < 8 && ( llabs( num ) > 9999 ||
( llabs( num ) >= factor && num % factor == 0 ) ); ++i )
{ num /= factor; p = prefix[i]; }
( exact && llabs( num ) >= factor ) ); ++i )
{ num /= factor; if( num % factor != 0 ) exact = false; p = prefix[i]; }
snprintf( buf, buf_size, "%lld %s", num, p );
return buf;
}
@ -155,7 +158,7 @@ static int open_instream( const char * const name,
if( infd < 0 )
{
if( verbosity >= 0 )
fprintf( stderr, "%s: Can't open input file `%s': %s.\n",
fprintf( stderr, "%s: Can't open input file '%s': %s.\n",
program_name, name, strerror( errno ) );
}
else
@ -168,10 +171,10 @@ static int open_instream( const char * const name,
if( i != 0 || ( !S_ISREG( mode ) && ( !to_stdout || !can_read ) ) )
{
if( verbosity >= 0 )
fprintf( stderr, "%s: Input file `%s' is not a regular file%s.\n",
fprintf( stderr, "%s: Input file '%s' is not a regular file%s.\n",
program_name, name,
( can_read && !to_stdout ) ?
" and `--stdout' was not specified" : "" );
" and '--stdout' was not specified" : "" );
close( infd );
infd = -1;
}
@ -213,8 +216,8 @@ static void set_d_outname( const char * const name )
output_filename = resize_buffer( output_filename, strlen( name ) + 4 + 1 );
strcpy( output_filename, name );
strcat( output_filename, ".out" );
if( verbosity >= 0 )
fprintf( stderr, "%s: Can't guess original name for `%s' -- using `%s'.\n",
if( verbosity >= 1 )
fprintf( stderr, "%s: Can't guess original name for '%s' -- using '%s'.\n",
program_name, name, output_filename );
}
@ -228,10 +231,10 @@ static bool open_outstream( const bool force )
if( outfd < 0 && verbosity >= 0 )
{
if( errno == EEXIST )
fprintf( stderr, "%s: Output file %s already exists, skipping.\n",
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",
fprintf( stderr, "%s: Can't create output file '%s': %s.\n",
program_name, output_filename, strerror( errno ) );
}
return ( outfd >= 0 );
@ -244,10 +247,10 @@ void cleanup_and_fail( const int retval )
{
delete_output_on_interrupt = false;
if( verbosity >= 0 )
fprintf( stderr, "%s: Deleting output file `%s', if it exists.\n",
fprintf( stderr, "%s: Deleting output file '%s', if it exists.\n",
program_name, output_filename );
if( outfd >= 0 ) { close( outfd ); outfd = -1; }
if( remove( output_filename ) != 0 )
if( remove( output_filename ) != 0 && errno != ENOENT )
show_error( "WARNING: deletion of output file (apparently) failed.", 0, false );
}
exit( retval );
@ -257,31 +260,26 @@ void cleanup_and_fail( const int retval )
/* Set permissions, owner and times. */
static void close_and_set_permissions( const struct stat * const in_statsp )
{
bool error = false;
bool warning = false;
if( in_statsp )
{
/* fchown will in many cases return with EPERM, which can be safely ignored. */
if( ( fchown( outfd, in_statsp->st_uid, in_statsp->st_gid ) != 0 &&
errno != EPERM ) ||
fchmod( outfd, in_statsp->st_mode ) != 0 )
error = true;
/* fchown will in many cases return with EPERM, which can be safely ignored. */
fchmod( outfd, in_statsp->st_mode ) != 0 ) warning = true;
}
if( close( outfd ) == 0 ) outfd = -1;
else cleanup_and_fail( 1 );
if( close( outfd ) != 0 ) cleanup_and_fail( 1 );
outfd = -1;
delete_output_on_interrupt = false;
if( !in_statsp ) return;
if( !error )
if( in_statsp )
{
struct utimbuf t;
t.actime = in_statsp->st_atime;
t.modtime = in_statsp->st_mtime;
if( utime( output_filename, &t ) != 0 ) error = true;
if( utime( output_filename, &t ) != 0 ) warning = true;
}
if( error )
{
if( warning && verbosity >= 1 )
show_error( "Can't change output file attributes.", 0, false );
cleanup_and_fail( 1 );
}
}
@ -290,17 +288,20 @@ static int decompress( const int infd, struct Pretty_print * const pp,
{
long long partial_file_pos = 0;
struct Range_decoder rdec;
int retval = 0, i, result;
int retval = 0, result;
bool first_member;
Rd_init( &rdec, infd );
if( !Rd_init( &rdec, infd ) )
{
show_error( "Not enough memory. Find a machine with more memory.", 0, false );
cleanup_and_fail( 1 );
}
for( first_member = true; ; first_member = false, Pp_reset( pp ) )
{
File_header header;
struct LZ_decoder decoder;
Rd_reset_member_position( &rdec );
for( i = 0; i < Fh_size; ++i )
header[i] = Rd_get_byte( &rdec );
Rd_read_data( &rdec, header, Fh_size );
if( Rd_finished( &rdec ) ) /* End Of File */
{
if( first_member )
@ -326,7 +327,7 @@ static int decompress( const int infd, struct Pretty_print * const pp,
{ Pp_show_msg( pp, "Invalid dictionary size in member header" );
retval = 2; break; }
if( verbosity >= 1 )
if( verbosity >= 2 || ( verbosity == 1 && first_member ) )
{
Pp_show_msg( pp, 0 );
if( verbosity >= 2 )
@ -334,8 +335,12 @@ static int decompress( const int infd, struct Pretty_print * const pp,
Fh_version( header ),
format_num( Fh_get_dictionary_size( header ) ) );
}
LZd_init( &decoder, header, &rdec, outfd );
if( !LZd_init( &decoder, header, &rdec, outfd ) )
{
show_error( "Not enough memory. Find a machine with more memory.", 0, false );
cleanup_and_fail( 1 );
}
result = LZd_decode_member( &decoder, pp );
partial_file_pos += Rd_member_position( &rdec );
LZd_free( &decoder );
@ -352,11 +357,14 @@ static int decompress( const int infd, struct Pretty_print * const pp,
}
retval = 2; break;
}
if( verbosity >= 1 )
if( verbosity >= 2 )
{ if( testing ) fprintf( stderr, "ok\n" );
else fprintf( stderr, "done\n" ); }
}
Rd_free( &rdec );
if( verbosity == 1 && retval == 0 )
{ if( testing ) fprintf( stderr, "ok\n" );
else fprintf( stderr, "done\n" ); }
return retval;
}
@ -382,7 +390,7 @@ void Pp_init( struct Pretty_print * const pp, const char * const filenames[],
{
unsigned int stdin_name_len;
int i;
pp->name_ = 0;
pp->name = 0;
pp->stdin_name = "(stdin)";
pp->longest_name = 0;
pp->verbosity = v;
@ -407,8 +415,8 @@ void Pp_show_msg( struct Pretty_print * const pp, const char * const msg )
{
int i, len;
pp->first_post = false;
fprintf( stderr, " %s: ", pp->name_ );
len = pp->longest_name - strlen( pp->name_ );
fprintf( stderr, " %s: ", pp->name );
len = pp->longest_name - strlen( pp->name );
for( i = 0; i < len; ++i ) fprintf( stderr, " " );
if( !msg ) fflush( stderr );
}
@ -428,7 +436,7 @@ void show_error( const char * const msg, const int errcode, const bool help )
fprintf( stderr, "\n" );
}
if( help && invocation_name && invocation_name[0] )
fprintf( stderr, "Try `%s --help' for more information.\n",
fprintf( stderr, "Try '%s --help' for more information.\n",
invocation_name );
}
}
@ -442,45 +450,6 @@ void internal_error( const char * const msg )
}
/* Returns the number of bytes really read.
If (returned value < size) and (errno == 0), means EOF was reached.
*/
int readblock( const int fd, uint8_t * const buf, const int size )
{
int rest = size;
while( true )
{
int n;
errno = 0;
if( rest <= 0 ) break;
n = read( fd, buf + size - rest, rest );
if( n > 0 ) rest -= n;
else if( n == 0 ) break;
else if( errno != EINTR && errno != EAGAIN ) break;
}
return ( rest > 0 ) ? size - rest : size;
}
/* Returns the number of bytes really written.
If (returned value < size), it is always an error.
*/
int writeblock( const int fd, const uint8_t * const buf, const int size )
{
int rest = size;
while( true )
{
int n;
errno = 0;
if( rest <= 0 ) break;
n = write( fd, buf + size - rest, rest );
if( n > 0 ) rest -= n;
else if( errno && errno != EINTR && errno != EAGAIN ) break;
}
return ( rest > 0 ) ? size - rest : size;
}
int main( const int argc, const char * const argv[] )
{
const char * input_filename = "";
@ -516,6 +485,7 @@ int main( const int argc, const char * const argv[] )
invocation_name = argv[0];
CRC32_init();
if( !ap_init( &parser, argc, argv, options, 0 ) )
{ show_error( "Memory exhausted.", 0, false ); return 1; }
if( ap_error( &parser ) ) /* bad option */
@ -547,6 +517,9 @@ int main( const int argc, const char * const argv[] )
_fsetmode( stdout, "b" );
#endif
if( testing )
outfd = -1;
for( ; argind < ap_arguments( &parser ); ++argind )
{
if( strcmp( ap_argument( &parser, argind ), "-" ) )
@ -567,8 +540,6 @@ int main( const int argc, const char * const argv[] )
set_signals();
Pp_init( &pp, filenames, num_filenames, verbosity );
if( testing )
outfd = -1;
output_filename = resize_buffer( output_filename, 1 );
for( i = 0; i < num_filenames; ++i )
@ -591,7 +562,7 @@ int main( const int argc, const char * const argv[] )
output_filename = resize_buffer( output_filename,
strlen( default_output_filename ) + 1 );
strcpy( output_filename, default_output_filename );
outfd_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
outfd_mode = all_rw;
if( !open_outstream( force ) )
{
if( outfd == -1 && retval < 1 ) retval = 1;
@ -612,7 +583,7 @@ int main( const int argc, const char * const argv[] )
else
{
set_d_outname( input_filename );
outfd_mode = S_IRUSR | S_IWUSR;
outfd_mode = usr_rw;
if( !open_outstream( force ) )
{
if( outfd == -1 && retval < 1 ) retval = 1;