summaryrefslogtreecommitdiffstats
path: root/repair.cc
diff options
context:
space:
mode:
Diffstat (limited to 'repair.cc')
-rw-r--r--repair.cc94
1 files changed, 54 insertions, 40 deletions
diff --git a/repair.cc b/repair.cc
index 98a43a4..3048987 100644
--- a/repair.cc
+++ b/repair.cc
@@ -1,5 +1,5 @@
/* Lziprecover - Data recovery tool for the lzip format
- Copyright (C) 2009-2016 Antonio Diaz Diaz.
+ Copyright (C) 2009-2017 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
@@ -37,6 +37,12 @@
namespace {
+bool pending_newline = false;
+
+void print_pending_newline()
+ { if( pending_newline ) std::fputc( '\n', stdout ); pending_newline = false; }
+
+
bool gross_damage( const long long msize, const uint8_t * const mbuffer )
{
enum { maxlen = 6 }; // max number of consecutive identical bytes
@@ -68,7 +74,7 @@ int repair_dictionary_size( const long long msize, uint8_t * const mbuffer )
File_header & header = *(File_header *)mbuffer;
unsigned dictionary_size = header.dictionary_size();
File_trailer & trailer =
- *(File_trailer *)( mbuffer + msize - File_trailer::size );
+ *(File_trailer *)( mbuffer + msize - File_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
@@ -112,10 +118,10 @@ long repair_member( const long long mpos, const long long msize,
if( !master ) return -1;
for( ; pos >= min_pos; --pos )
{
- if( verbosity >= 1 )
+ if( verbosity >= 2 )
{
- std::printf( "Trying position %llu \r", mpos + pos );
- std::fflush( stdout );
+ std::printf( " Trying position %llu \r", mpos + pos );
+ std::fflush( stdout ); pending_newline = true;
}
for( int j = 0; j < 255; ++j )
{
@@ -141,9 +147,10 @@ int repair_file( const std::string & input_filename,
if( infd < 0 ) return 1;
Pretty_print pp( input_filename, verbosity );
- const File_index file_index( infd );
+ const File_index file_index( infd, true, true );
if( file_index.retval() != 0 )
- { pp( file_index.error().c_str() ); return file_index.retval(); }
+ { show_file_error( input_filename.c_str(), file_index.error().c_str() );
+ return file_index.retval(); }
output_filename = default_output_filename.empty() ?
insert_fixed( input_filename ) : default_output_filename;
@@ -161,7 +168,7 @@ int repair_file( const std::string & input_filename,
{ show_error( "Can't repair error in input file." );
cleanup_and_fail( 2 ); }
- if( verbosity >= 1 ) // damaged member found
+ if( verbosity >= 2 ) // damaged member found
{
std::printf( "Repairing member %ld of %ld (failure pos = %llu)\n",
i + 1, file_index.members(), mpos + failure_pos );
@@ -183,6 +190,7 @@ int repair_file( const std::string & input_filename,
if( pos == 0 )
pos = repair_member( mpos, msize, mbuffer, File_header::size + 6,
failure_pos, dictionary_size, verbosity );
+ print_pending_newline();
}
if( pos < 0 )
cleanup_and_fail( 1 );
@@ -200,7 +208,6 @@ int repair_file( const std::string & input_filename,
cleanup_and_fail( 1 ); }
}
delete[] mbuffer;
- if( verbosity >= 1 ) std::fputc( '\n', stdout );
if( pos == 0 )
{
show_error( "Can't repair input file. Error is probably larger than 1 byte." );
@@ -229,12 +236,13 @@ int debug_delay( const std::string & input_filename, Block range,
if( infd < 0 ) return 1;
Pretty_print pp( input_filename, verbosity );
- const File_index file_index( infd );
+ const File_index file_index( infd, false, true );
if( file_index.retval() != 0 )
- { pp( file_index.error().c_str() ); return file_index.retval(); }
+ { show_file_error( input_filename.c_str(), file_index.error().c_str() );
+ return file_index.retval(); }
- if( range.end() > file_index.file_end() )
- range.size( std::max( 0LL, file_index.file_end() - range.pos() ) );
+ if( range.end() > file_index.cdata_size() )
+ range.size( std::max( 0LL, file_index.cdata_size() - range.pos() ) );
if( range.size() <= 0 )
{ if( verbosity >= 0 ) pp( "Nothing to do." ); return 0; }
@@ -245,7 +253,7 @@ int debug_delay( const std::string & input_filename, Block range,
const long long mpos = file_index.mblock( i ).pos();
const long long msize = file_index.mblock( i ).size();
const unsigned dictionary_size = file_index.dictionary_size( i );
- if( verbosity >= 1 )
+ if( verbosity >= 2 )
{
std::printf( "Finding max delay in member %ld of %ld (mpos = %llu, msize = %llu)\n",
i + 1, file_index.members(), mpos, msize );
@@ -266,10 +274,10 @@ int debug_delay( const std::string & input_filename, Block range,
const long partial_end = std::min( pos + 100, end );
for( ; pos < partial_end; ++pos )
{
- if( verbosity >= 1 )
+ if( verbosity >= 2 )
{
- std::printf( "Delays in position %llu \r", mpos + pos );
- std::fflush( stdout );
+ std::printf( " Delays at position %llu \r", mpos + pos );
+ std::fflush( stdout ); pending_newline = true;
}
int value = -1;
for( int j = 0; j < 256; ++j )
@@ -281,18 +289,18 @@ int debug_delay( const std::string & input_filename, Block range,
const long delay = failure_pos - pos;
if( delay > max_delay ) { max_delay = delay; value = mbuffer[pos]; }
}
- if( value >= 0 && verbosity >= 0 )
+ if( value >= 0 && verbosity >= 2 )
{
- std::printf( "New max delay %lu at position %llu (0x%02X)\n",
+ std::printf( " New max delay %lu at position %llu (0x%02X)\n",
max_delay, mpos + pos, value );
- std::fflush( stdout );
+ std::fflush( stdout ); pending_newline = false;
}
if( pos + max_delay >= msize ) { pos = end; break; }
}
delete master;
}
delete[] mbuffer;
- if( verbosity >= 1 ) std::fputc( '\n', stdout );
+ print_pending_newline();
}
if( verbosity >= 1 ) std::fputs( "Done.\n", stdout );
@@ -300,21 +308,22 @@ int debug_delay( const std::string & input_filename, Block range,
}
-int debug_repair( const std::string & input_filename, const long long bad_pos,
- const int verbosity, const uint8_t bad_value )
+int debug_repair( const std::string & input_filename,
+ const Bad_byte & bad_byte, const int verbosity )
{
struct stat in_stats; // not used
const int infd = open_instream( input_filename.c_str(), &in_stats, true, true );
if( infd < 0 ) return 1;
Pretty_print pp( input_filename, verbosity );
- const File_index file_index( infd );
+ const File_index file_index( infd, false, true );
if( file_index.retval() != 0 )
- { pp( file_index.error().c_str() ); return file_index.retval(); }
+ { show_file_error( input_filename.c_str(), file_index.error().c_str() );
+ return file_index.retval(); }
long idx = 0;
for( ; idx < file_index.members(); ++idx )
- if( file_index.mblock( idx ).includes( bad_pos ) ) break;
+ if( file_index.mblock( idx ).includes( bad_byte.pos ) ) break;
if( idx >= file_index.members() )
{ if( verbosity >= 0 ) pp( "Nothing to do." ); return 0; }
@@ -335,10 +344,11 @@ int debug_repair( const std::string & input_filename, const long long bad_pos,
if( !mbuffer ) return 1;
const File_header & header = *(File_header *)mbuffer;
const unsigned dictionary_size = header.dictionary_size();
- const uint8_t good_value = mbuffer[bad_pos-mpos];
- mbuffer[bad_pos-mpos] = bad_value;
+ const uint8_t good_value = mbuffer[bad_byte.pos-mpos];
+ const uint8_t bad_value = bad_byte( good_value );
+ mbuffer[bad_byte.pos-mpos] = bad_value;
long failure_pos = 0;
- if( bad_pos != 5 || isvalid_ds( header.dictionary_size() ) )
+ if( bad_byte.pos != 5 || isvalid_ds( header.dictionary_size() ) )
{
const LZ_mtester * master =
prepare_master( mbuffer, msize, 0, header.dictionary_size() );
@@ -354,12 +364,13 @@ int debug_repair( const std::string & input_filename, const long long bad_pos,
}
delete master;
}
- if( verbosity >= 1 )
+ if( verbosity >= 2 )
{
std::printf( "Test repairing member %ld of %ld (mpos = %llu, msize = %llu)\n"
- " (damage pos = %llu (0x%02X->0x%02X), failure pos = %llu)\n",
+ " (damage pos = %llu (0x%02X->0x%02X), failure pos = %llu, delay = %lld )\n",
idx + 1, file_index.members(), mpos, msize,
- bad_pos, good_value, bad_value, mpos + failure_pos );
+ bad_byte.pos, good_value, bad_value, mpos + failure_pos,
+ mpos + failure_pos - bad_byte.pos );
std::fflush( stdout );
}
if( failure_pos >= msize ) failure_pos = msize - 1;
@@ -370,10 +381,10 @@ int debug_repair( const std::string & input_filename, const long long bad_pos,
if( pos == 0 )
pos = repair_member( mpos, msize, mbuffer, File_header::size + 6,
failure_pos, dictionary_size, verbosity );
+ print_pending_newline();
delete[] mbuffer;
if( pos < 0 )
{ show_error( "Can't prepare master." ); return 1; }
- if( verbosity >= 1 ) std::fputc( '\n', stdout );
if( pos == 0 ) internal_error( "can't repair input file." );
if( verbosity >= 1 )
std::fputs( "Member repaired successfully.\n", stdout );
@@ -382,17 +393,18 @@ int debug_repair( const std::string & input_filename, const long long bad_pos,
int debug_decompress( const std::string & input_filename,
- const long long bad_pos, const int verbosity,
- const uint8_t bad_value, const bool show_packets )
+ const Bad_byte & bad_byte, const int verbosity,
+ const bool show_packets )
{
struct stat in_stats;
const int infd = open_instream( input_filename.c_str(), &in_stats, true, true );
if( infd < 0 ) return 1;
Pretty_print pp( input_filename, verbosity );
- const File_index file_index( infd );
+ const File_index file_index( infd, false, true );
if( file_index.retval() != 0 )
- { pp( file_index.error().c_str() ); return file_index.retval(); }
+ { show_file_error( input_filename.c_str(), file_index.error().c_str() );
+ return file_index.retval(); }
outfd = show_packets ? -1 : STDOUT_FILENO;
int retval = 0;
@@ -411,12 +423,14 @@ int debug_decompress( const std::string & input_filename,
retval = 2; break; }
uint8_t * const mbuffer = read_member( infd, mpos, msize );
if( !mbuffer ) { retval = 1; break; }
- if( bad_pos >= 0 && file_index.mblock( i ).includes( bad_pos ) )
+ if( bad_byte.pos >= 0 && file_index.mblock( i ).includes( bad_byte.pos ) )
{
+ const uint8_t good_value = mbuffer[bad_byte.pos-mpos];
+ const uint8_t bad_value = bad_byte( good_value );
+ mbuffer[bad_byte.pos-mpos] = bad_value;
if( verbosity >= 1 && show_packets )
std::printf( "Byte at pos %llu changed from 0x%02X to 0x%02X\n",
- bad_pos, mbuffer[bad_pos-mpos], bad_value );
- mbuffer[bad_pos-mpos] = bad_value;
+ bad_byte.pos, good_value, bad_value );
}
LZ_mtester mtester( mbuffer, msize, dictionary_size, outfd );
const int result = mtester.debug_decode_member( dpos, mpos, show_packets );