summaryrefslogtreecommitdiffstats
path: root/range_dec.cc
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2021-01-25 13:52:00 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2021-01-25 13:52:20 +0000
commitf0bda7c287ad4012750724d1a836131e6498d9a9 (patch)
treec4a0925d3fbb0aef25524ccd6689e2595c8896b2 /range_dec.cc
parentReleasing debian version 1.21-9. (diff)
downloadlziprecover-f0bda7c287ad4012750724d1a836131e6498d9a9.tar.xz
lziprecover-f0bda7c287ad4012750724d1a836131e6498d9a9.zip
Merging upstream version 1.22.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'range_dec.cc')
-rw-r--r--range_dec.cc111
1 files changed, 62 insertions, 49 deletions
diff --git a/range_dec.cc b/range_dec.cc
index 78d586f..24ac5e8 100644
--- a/range_dec.cc
+++ b/range_dec.cc
@@ -1,18 +1,18 @@
-/* Lziprecover - Data recovery tool for the lzip format
- Copyright (C) 2009-2019 Antonio Diaz Diaz.
+/* Lziprecover - Data recovery tool for the lzip format
+ Copyright (C) 2009-2021 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
- the Free Software Foundation, either version 2 of the License, or
- (at your option) any later version.
+ 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
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _FILE_OFFSET_BITS 64
@@ -35,21 +35,21 @@
namespace {
-int decompress_member( const int infd, const Pretty_print & pp,
- const unsigned long long mpos,
- const unsigned long long outskip,
- const unsigned long long outend )
+bool decompress_member( const int infd, const Pretty_print & pp,
+ const unsigned long long mpos,
+ const unsigned long long outskip,
+ const unsigned long long outend )
{
Range_decoder rdec( infd );
Lzip_header header;
rdec.read_data( header.data, Lzip_header::size );
if( rdec.finished() ) // End Of File
- { pp( "File ends unexpectedly at member header." ); return 2; }
- if( !header.verify_magic() ) { pp( bad_magic_msg ); return 2; }
+ { pp( "File ends unexpectedly at member header." ); return false; }
+ if( !header.verify_magic() ) { pp( bad_magic_msg ); return false; }
if( !header.verify_version() )
- { pp( bad_version( header.version() ) ); return 2; }
+ { pp( bad_version( header.version() ) ); return false; }
const unsigned dictionary_size = header.dictionary_size();
- if( !isvalid_ds( dictionary_size ) ) { pp( bad_dict_msg ); return 2; }
+ if( !isvalid_ds( dictionary_size ) ) { pp( bad_dict_msg ); return false; }
if( verbosity >= 2 ) pp();
@@ -64,16 +64,27 @@ int decompress_member( const int infd, const Pretty_print & pp,
"File ends unexpectedly" : "Decoder error",
mpos + rdec.member_position() );
}
- return 2;
+ return false;
+ }
+ if( decoder.data_position() < outend - outskip )
+ {
+ if( verbosity >= 0 )
+ { pp(); std::fprintf( stderr,
+ "%sMember at pos %llu contains only %llu bytes of %llu requested.\n",
+ ( verbosity >= 2 ) ? "\n" : "", mpos,
+ decoder.data_position() - outskip, outend - outskip ); }
+ return false;
}
if( verbosity >= 2 ) std::fputs( "done\n", stderr );
- return 0;
+ return true;
}
+} // end namespace
+
const char * format_num( unsigned long long num,
- unsigned long long limit = -1ULL,
- const int set_prefix = 0 )
+ unsigned long long limit,
+ const int set_prefix )
{
const char * const si_prefix[8] =
{ "k", "M", "G", "T", "P", "E", "Z", "Y" };
@@ -85,20 +96,22 @@ const char * format_num( unsigned long long num,
static bool si = true;
if( set_prefix ) si = ( set_prefix > 0 );
+ unsigned long long den = 1;
const unsigned factor = ( si ? 1000 : 1024 );
char * const buf = buffer[current++]; current %= buffers;
const char * const * prefix = ( si ? si_prefix : binary_prefix );
const char * p = "";
- bool exact = ( num % factor == 0 );
- for( int i = 0; i < 8 && ( num > limit || ( exact && num >= factor ) ); ++i )
- { num /= factor; if( num % factor != 0 ) exact = false; p = prefix[i]; }
- snprintf( buf, bufsize, "%llu %s", num, p );
+ for( int i = 0; i < 8 && num / den >= factor && den * factor > den; ++i )
+ { if( num / den <= limit && num % ( den * factor ) != 0 ) break;
+ den *= factor; p = prefix[i]; }
+ if( num % den == 0 )
+ snprintf( buf, bufsize, "%llu %s", num / den, p );
+ else
+ snprintf( buf, bufsize, "%3.2f %s", (double)num / den, p );
return buf;
}
-} // end namespace
-
bool safe_seek( const int fd, const long long pos )
{
@@ -114,7 +127,8 @@ int range_decompress( const std::string & input_filename,
const bool to_stdout )
{
struct stat in_stats;
- const int infd = open_instream( input_filename.c_str(), &in_stats, true, true );
+ const int infd =
+ open_instream( input_filename.c_str(), &in_stats, false, true );
if( infd < 0 ) return 1;
const Lzip_index lzip_index( infd, ignore_trailing, loose_trailing,
@@ -123,30 +137,30 @@ int range_decompress( const std::string & input_filename,
{ show_file_error( input_filename.c_str(), lzip_index.error().c_str() );
return lzip_index.retval(); }
- if( range.end() > lzip_index.udata_size() )
- range.size( std::max( 0LL, lzip_index.udata_size() - range.pos() ) );
+ const long long udata_size = lzip_index.udata_size();
+ if( range.end() > udata_size )
+ range.size( std::max( 0LL, udata_size - range.pos() ) );
if( range.size() <= 0 )
- { show_file_error( input_filename.c_str(), "Nothing to do." ); return 0; }
+ { if( udata_size > 0 )
+ show_file_error( input_filename.c_str(), "Nothing to do." );
+ return 0; }
- if( to_stdout || default_output_filename.empty() )
- outfd = STDOUT_FILENO;
+ if( to_stdout || default_output_filename.empty() ) outfd = STDOUT_FILENO;
else
{
output_filename = default_output_filename;
set_signal_handler();
- if( !open_outstream( force, false, false, false ) )
- { close( infd ); return 1; }
+ if( !open_outstream( force, true, false, false ) ) return 1;
}
if( verbosity >= 1 )
- std::fprintf( stderr, "Decompressing range %sB to %sB (%sof %sBytes)\n",
+ std::fprintf( stderr, "Decompressing range %sB to %sB (%sB of %sBytes)\n",
format_num( range.pos() ),
format_num( range.pos() + range.size() ),
- format_num( range.size() ),
- format_num( lzip_index.udata_size() ) );
+ format_num( range.size() ), format_num( udata_size ) );
Pretty_print pp( input_filename );
- int retval = 0;
+ bool error = false;
for( long i = 0; i < lzip_index.members(); ++i )
{
const Block & db = lzip_index.dblock( i );
@@ -157,16 +171,15 @@ int range_decompress( const std::string & input_filename,
const long long outskip = std::max( 0LL, range.pos() - db.pos() );
const long long outend = std::min( db.size(), range.end() - db.pos() );
const long long mpos = lzip_index.mblock( i ).pos();
- if( !safe_seek( infd, mpos ) ) { retval = 1; break; }
- const int tmp = decompress_member( infd, pp, mpos, outskip, outend );
- if( tmp && ( tmp != 2 || !ignore_errors ) ) cleanup_and_fail( tmp );
- if( tmp > retval ) retval = tmp;
+ if( !safe_seek( infd, mpos ) ) cleanup_and_fail( 1 );
+ if( !decompress_member( infd, pp, mpos, outskip, outend ) )
+ { if( !ignore_errors ) cleanup_and_fail( 2 ); else error = true; }
pp.reset();
}
}
close( infd );
- retval = std::max( retval, close_outstream( &in_stats ) );
- if( verbosity >= 2 && retval == 0 )
+ if( close_outstream( &in_stats ) != 0 ) cleanup_and_fail( 1 );
+ if( verbosity >= 2 && !error )
std::fputs( "Byte range decompressed successfully.\n", stderr );
- return retval;
+ return 0; // either no error or ignored
}