summaryrefslogtreecommitdiffstats
path: root/byte_repair.cc
diff options
context:
space:
mode:
Diffstat (limited to 'byte_repair.cc')
-rw-r--r--byte_repair.cc27
1 files changed, 14 insertions, 13 deletions
diff --git a/byte_repair.cc b/byte_repair.cc
index 3e92ca4..370738b 100644
--- a/byte_repair.cc
+++ b/byte_repair.cc
@@ -1,5 +1,5 @@
/* Lziprecover - Data recovery tool for the lzip format
- Copyright (C) 2009-2023 Antonio Diaz Diaz.
+ Copyright (C) 2009-2024 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
@@ -64,7 +64,7 @@ int repair_dictionary_size( uint8_t * const mbuffer, const long msize )
Lzip_header & header = *(Lzip_header *)mbuffer;
unsigned dictionary_size = header.dictionary_size();
const Lzip_trailer & trailer =
- *(const Lzip_trailer *)( mbuffer + msize - Lzip_trailer::size );
+ *(const Lzip_trailer *)( mbuffer + msize - trailer.size );
const unsigned long long data_size = trailer.data_size();
const bool valid_ds = isvalid_ds( dictionary_size );
if( valid_ds && dictionary_size >= data_size ) return 0; // can't be bad
@@ -195,8 +195,9 @@ int byte_repair( const std::string & input_filename,
{ show_file_error( filename, lzip_index.error().c_str() );
return lzip_index.retval(); }
- output_filename = default_output_filename.empty() ?
- insert_fixed( input_filename ) : default_output_filename;
+ const bool to_file = default_output_filename.size();
+ output_filename =
+ to_file ? default_output_filename : insert_fixed( input_filename );
if( !force && output_file_exists() ) return 1;
outfd = -1;
for( long i = 0; i < lzip_index.members(); ++i )
@@ -226,10 +227,10 @@ int byte_repair( const std::string & input_filename,
{
pos = repair_dictionary_size( mbuffer, msize );
if( pos == 0 )
- pos = repair_member( mbuffer, mpos, msize, Lzip_header::size + 1,
- Lzip_header::size + 6, dictionary_size, terminator );
+ pos = repair_member( mbuffer, mpos, msize, header.size + 1,
+ header.size + 6, dictionary_size, terminator );
if( pos == 0 )
- pos = repair_member( mbuffer, mpos, msize, Lzip_header::size + 7,
+ pos = repair_member( mbuffer, mpos, msize, header.size + 7,
failure_pos, dictionary_size, terminator );
print_pending_newline( terminator );
}
@@ -241,7 +242,7 @@ int byte_repair( const std::string & input_filename,
{
if( !safe_seek( infd, 0, filename ) ) return 1;
set_signal_handler();
- if( !open_outstream( true, true ) ) return 1;
+ if( !open_outstream( true, true, false, true, to_file ) ) return 1;
if( !copy_file( infd, outfd ) ) // copy whole file
cleanup_and_fail( 1 );
}
@@ -263,7 +264,7 @@ int byte_repair( const std::string & input_filename,
std::fputs( "Input file has no errors. Recovery is not needed.\n", stdout );
return 0;
}
- if( close_outstream( &in_stats ) != 0 ) return 1;
+ if( !close_outstream( &in_stats ) ) return 1;
if( verbosity >= 1 )
std::fputs( "Copy of input file repaired successfully.\n", stdout );
return 0;
@@ -416,10 +417,10 @@ int debug_byte_repair( const char * const input_filename,
if( failure_pos >= msize ) failure_pos = msize - 1;
long pos = repair_dictionary_size( mbuffer, msize );
if( pos == 0 )
- pos = repair_member( mbuffer, mpos, msize, Lzip_header::size + 1,
- Lzip_header::size + 6, dictionary_size, terminator );
+ pos = repair_member( mbuffer, mpos, msize, header.size + 1,
+ header.size + 6, dictionary_size, terminator );
if( pos == 0 )
- pos = repair_member( mbuffer, mpos, msize, Lzip_header::size + 7,
+ pos = repair_member( mbuffer, mpos, msize, header.size + 7,
failure_pos, dictionary_size, terminator );
print_pending_newline( terminator );
delete[] mbuffer;
@@ -512,7 +513,7 @@ int debug_decompress( const char * const input_filename,
std::fputc( '\n', stdout );
}
- retval = std::max( retval, close_outstream( &in_stats ) );
+ if( !close_outstream( &in_stats ) && retval == 0 ) retval = 1;
if( verbosity >= 1 && show_packets && retval == 0 )
std::fputs( "Done.\n", stdout );
return retval;