summaryrefslogtreecommitdiffstats
path: root/repair.cc
diff options
context:
space:
mode:
authorDaniel Baumann <mail@daniel-baumann.ch>2015-11-07 11:52:32 +0000
committerDaniel Baumann <mail@daniel-baumann.ch>2015-11-07 11:52:32 +0000
commitde06b1d957bdad592fff87e845d27d40cfcbd694 (patch)
tree38d1a64462988c92f82b12d2ea3dca6864006003 /repair.cc
parentAdding upstream version 1.17. (diff)
downloadlziprecover-de06b1d957bdad592fff87e845d27d40cfcbd694.tar.xz
lziprecover-de06b1d957bdad592fff87e845d27d40cfcbd694.zip
Adding upstream version 1.18~pre1.upstream/1.18_pre1
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to '')
-rw-r--r--repair.cc234
1 files changed, 152 insertions, 82 deletions
diff --git a/repair.cc b/repair.cc
index 25de7c1..e83b843 100644
--- a/repair.cc
+++ b/repair.cc
@@ -36,6 +36,23 @@
namespace {
+bool gross_damage( const long long msize, const uint8_t * const mbuffer )
+ {
+ enum { maxlen = 6 }; // max number of consecutive identical bytes
+ long i = File_header::size;
+ const long end = msize - File_trailer::size - maxlen;
+ uint8_t byte;
+ while( i < end )
+ {
+ byte = mbuffer[i];
+ int len = 0; // does not count the first byte
+ while( mbuffer[++i] == byte && ++len < maxlen ) {}
+ if( len >= maxlen ) return true;
+ }
+ return false;
+ }
+
+
int seek_write( const int fd, const uint8_t * const buf, const int size,
const long long pos )
{
@@ -44,6 +61,35 @@ int seek_write( const int fd, const uint8_t * const buf, const int size,
return 0;
}
+// Return value: -1 = master failed, 0 = begin reached, >0 = repaired pos
+long repair_member( const long long mpos, const long long msize,
+ uint8_t * const mbuffer, const long begin, const long end,
+ const int verbosity )
+ {
+ for( long pos = end; pos >= begin && pos > end - 50000; )
+ {
+ const long min_pos = std::max( begin, pos - 100 );
+ const LZ_mtester * master = prepare_master( mbuffer, msize, min_pos - 16 );
+ if( !master ) return -1;
+ for( ; pos >= min_pos; --pos )
+ {
+ if( verbosity >= 1 )
+ {
+ std::printf( "Trying position %llu \r", mpos + pos );
+ std::fflush( stdout );
+ }
+ for( int j = 0; j < 255; ++j )
+ {
+ ++mbuffer[pos];
+ if( test_member_rest( *master ) ) { delete master; return pos; }
+ }
+ ++mbuffer[pos];
+ }
+ delete master;
+ }
+ return 0;
+ }
+
} // end namespace
@@ -51,7 +97,7 @@ int repair_file( const std::string & input_filename,
const std::string & output_filename, const int verbosity,
const bool force )
{
- struct stat in_stats;
+ struct stat in_stats; // not used
const int infd = open_instream( input_filename.c_str(), &in_stats, true, true );
if( infd < 0 ) return 1;
@@ -60,6 +106,7 @@ int repair_file( const std::string & input_filename,
if( file_index.retval() != 0 )
{ pp( file_index.error().c_str() ); return file_index.retval(); }
+ if( !force && file_exists( output_filename ) ) return 1;
int outfd = -1;
for( long i = 0; i < file_index.members(); ++i )
{
@@ -69,7 +116,6 @@ int repair_file( const std::string & input_filename,
cleanup_and_fail( output_filename, outfd, 1 );
long long failure_pos = 0;
if( try_decompress_member( infd, msize, &failure_pos ) ) continue;
- if( failure_pos >= msize - 8 ) failure_pos = msize - 8 - 1;
if( failure_pos < File_header::size )
{ show_error( "Can't repair error in input file." );
cleanup_and_fail( output_filename, outfd, 2 ); }
@@ -80,51 +126,38 @@ int repair_file( const std::string & input_filename,
i + 1, file_index.members(), mpos + failure_pos );
std::fflush( stdout );
}
+ if( failure_pos >= msize - 8 ) failure_pos = msize - 8 - 1;
uint8_t * const mbuffer = read_member( infd, mpos, msize );
if( !mbuffer )
cleanup_and_fail( output_filename, outfd, 1 );
- long pos = failure_pos;
- bool done = false;
- while( pos >= File_header::size && pos > failure_pos - 50000 && !done )
+ long pos = 0;
+ if( !gross_damage( msize, mbuffer ) )
{
- const long min_pos = std::max( (long)File_header::size, pos - 100 );
- const LZ_mtester * master = prepare_master( mbuffer, msize, min_pos - 16 );
- if( !master )
- cleanup_and_fail( output_filename, outfd, 1 );
- for( ; pos >= min_pos && !done; --pos )
+ pos = repair_member( mpos, msize, mbuffer, File_header::size + 1,
+ File_header::size + 2, verbosity );
+ if( pos == 0 )
+ pos = repair_member( mpos, msize, mbuffer, File_header::size + 3,
+ failure_pos, verbosity );
+ }
+ if( pos < 0 )
+ cleanup_and_fail( output_filename, outfd, 1 );
+ if( pos > 0 )
+ {
+ if( outfd < 0 ) // first damaged member repaired
{
- if( verbosity >= 1 )
- {
- std::printf( "Trying position %llu \r", mpos + pos );
- std::fflush( stdout );
- }
- for( int j = 0; j < 256; ++j )
- {
- ++mbuffer[pos];
- if( j == 255 ) break;
- if( test_member_rest( *master ) )
- {
- done = true;
- if( outfd < 0 ) // first damaged member repaired
- {
- if( !safe_seek( infd, 0 ) ) return 1;
- outfd = open_outstream_rw( output_filename, force );
- if( outfd < 0 ) { close( infd ); return 1; }
- if( !copy_file( infd, outfd ) ) // copy whole file
- cleanup_and_fail( output_filename, outfd, 1 );
- }
- if( seek_write( outfd, mbuffer + pos, 1, mpos + pos ) != 1 )
- { show_error( "Error writing output file", errno );
- cleanup_and_fail( output_filename, outfd, 1 ); }
- break;
- }
- }
+ if( !safe_seek( infd, 0 ) ) return 1;
+ outfd = open_outstream_rw( output_filename, true );
+ if( outfd < 0 ) { close( infd ); return 1; }
+ if( !copy_file( infd, outfd ) ) // copy whole file
+ cleanup_and_fail( output_filename, outfd, 1 );
}
- delete master;
+ if( seek_write( outfd, mbuffer + pos, 1, mpos + pos ) != 1 )
+ { show_error( "Error writing output file", errno );
+ cleanup_and_fail( output_filename, outfd, 1 ); }
}
delete[] mbuffer;
- if( verbosity >= 1 ) std::fputs( "\n", stdout );
- if( !done )
+ if( verbosity >= 1 ) std::fputc( '\n', stdout );
+ if( pos == 0 )
{
show_error( "Can't repair input file. Error is probably larger than 1 byte." );
cleanup_and_fail( output_filename, outfd, 2 );
@@ -151,7 +184,7 @@ int repair_file( const std::string & input_filename,
int debug_delay( const std::string & input_filename, Block range,
const int verbosity )
{
- struct stat in_stats;
+ struct stat in_stats; // not used
const int infd = open_instream( input_filename.c_str(), &in_stats, true, true );
if( infd < 0 ) return 1;
@@ -171,15 +204,14 @@ int debug_delay( const std::string & input_filename, Block range,
if( !range.overlaps( mb ) ) continue;
const long long mpos = file_index.mblock( i ).pos();
const long long msize = file_index.mblock( i ).size();
- if( verbosity >= 1 ) // damaged member found
+ if( verbosity >= 1 )
{
std::printf( "Finding max delay in member %ld of %ld (mpos = %llu, msize = %llu)\n",
i + 1, file_index.members(), mpos, msize );
std::fflush( stdout );
}
uint8_t * const mbuffer = read_member( infd, mpos, msize );
- if( !mbuffer )
- { show_error( "Can't read member." ); return 1; }
+ if( !mbuffer ) return 1;
long pos = std::max( range.pos() - mpos, File_header::size + 1LL );
const long end = std::min( range.end() - mpos, msize );
long max_delay = 0;
@@ -217,7 +249,7 @@ int debug_delay( const std::string & input_filename, Block range,
delete master;
}
delete[] mbuffer;
- if( verbosity >= 1 ) std::fputs( "\n", stdout );
+ if( verbosity >= 1 ) std::fputc( '\n', stdout );
}
if( verbosity >= 1 ) std::fputs( "Done.\n", stdout );
@@ -228,7 +260,7 @@ 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 )
{
- struct stat in_stats;
+ struct stat in_stats; // not used
const int infd = open_instream( input_filename.c_str(), &in_stats, true, true );
if( infd < 0 ) return 1;
@@ -247,8 +279,7 @@ int debug_repair( const std::string & input_filename, const long long bad_pos,
const long long msize = file_index.mblock( idx ).size();
{
long long failure_pos = 0;
- if( !safe_seek( infd, mpos ) )
- { show_error( "Can't seek to member." ); return 1; }
+ if( !safe_seek( infd, mpos ) ) return 1;
if( !try_decompress_member( infd, msize, &failure_pos ) )
{
if( verbosity >= 0 )
@@ -258,62 +289,101 @@ int debug_repair( const std::string & input_filename, const long long bad_pos,
}
}
uint8_t * const mbuffer = read_member( infd, mpos, msize );
- if( !mbuffer )
- { show_error( "Can't read member." ); return 1; }
- const uint8_t good_value = mbuffer[bad_pos];
- mbuffer[bad_pos] = bad_value;
+ if( !mbuffer ) return 1;
+ const uint8_t good_value = mbuffer[bad_pos-mpos];
+ mbuffer[bad_pos-mpos] = bad_value;
long failure_pos = 0;
{
const LZ_mtester * master = prepare_master( mbuffer, msize, 0 );
if( !master )
- { show_error( "Can't prepare master." ); return 1; }
+ { show_error( "Can't prepare master." ); delete[] mbuffer; return 1; }
if( test_member_rest( *master, &failure_pos ) )
{
if( verbosity >= 1 )
std::fputs( "Member decompressed with no errors.\n", stdout );
+ delete master;
+ delete[] mbuffer;
return 0;
}
+ delete master;
if( verbosity >= 1 )
{
std::printf( "Test repairing member %ld of %ld (mpos = %llu, msize = %llu)\n"
" (damage pos = %llu (0x%02X->0x%02X), failure pos = %llu)\n",
idx + 1, file_index.members(), mpos, msize,
- mpos + bad_pos, good_value, bad_value, mpos + failure_pos );
+ bad_pos, good_value, bad_value, mpos + failure_pos );
std::fflush( stdout );
}
}
- long pos = failure_pos;
- bool done = false;
- while( pos >= File_header::size && pos > failure_pos - 50000 && !done )
+ if( failure_pos >= msize ) failure_pos = msize - 1;
+ long pos = repair_member( mpos, msize, mbuffer, File_header::size + 1,
+ File_header::size + 2, verbosity );
+ if( pos == 0 )
+ pos = repair_member( mpos, msize, mbuffer, File_header::size + 3,
+ failure_pos, verbosity );
+ 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 );
+ return 0;
+ }
+
+
+int debug_show_packets( const std::string & input_filename,
+ const long long bad_pos, const int verbosity,
+ const uint8_t bad_value )
+ {
+ 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 );
+ if( file_index.retval() != 0 )
+ { pp( file_index.error().c_str() ); return file_index.retval(); }
+
+ int retval = 0;
+ for( long i = 0; i < file_index.members(); ++i )
{
- const long min_pos = std::max( (long)File_header::size, pos - 100 );
- const LZ_mtester * master = prepare_master( mbuffer, msize, min_pos - 16 );
- if( !master )
- { show_error( "Can't prepare master." ); return 1; }
- for( ; pos >= min_pos && !done; --pos )
+ const long long dpos = file_index.dblock( i ).pos();
+ const long long mpos = file_index.mblock( i ).pos();
+ const long long msize = file_index.mblock( i ).size();
+ if( verbosity >= 1 )
+ std::printf( "Decoding LZMA packets in member %ld of %ld (mpos = %llu, msize = %llu)\n"
+ " mpos dpos\n",
+ i + 1, file_index.members(), mpos, msize );
+ uint8_t * const mbuffer = read_member( infd, mpos, msize );
+ if( !mbuffer ) return 1;
+ const File_header & header = *(File_header *)mbuffer;
+ const unsigned dictionary_size = header.dictionary_size();
+ if( !header.verify_magic() || !header.verify_version() ||
+ dictionary_size < min_dictionary_size ||
+ dictionary_size > max_dictionary_size )
+ { show_error( "Header error." ); return 2; }
+ if( bad_pos >= 0 && file_index.mblock( i ).includes( bad_pos ) )
{
if( verbosity >= 1 )
- {
- std::printf( "Trying position %llu \r", mpos + pos );
- std::fflush( stdout );
- }
- for( int j = 0; j < 256; ++j )
- {
- ++mbuffer[pos];
- if( j == 255 ) break;
- if( test_member_rest( *master ) ) { done = true; break; }
- }
+ 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;
}
- delete master;
- }
- delete[] mbuffer;
- if( verbosity >= 1 ) std::fputs( "\n", stdout );
- if( !done )
- {
- show_error( "Can't repair input file. There is a bug somewhere." );
- return 3;
+ LZ_mtester mtester( mbuffer, msize, dictionary_size );
+ const int result = mtester.debug_decode_member( dpos, mpos, true );
+ delete[] mbuffer;
+ if( result != 0 )
+ {
+ if( verbosity >= 0 && result <= 2 )
+ std::printf( "%s at pos %llu\n", ( result == 2 ) ?
+ "File ends unexpectedly" : "Decoder error",
+ mpos + mtester.member_position() );
+ retval = 2; break;
+ }
+ if( i + 1 < file_index.members() ) std::fputc( '\n', stdout );
}
- if( verbosity >= 1 )
- std::fputs( "Member repaired successfully.\n", stdout );
- return 0;
+
+ if( verbosity >= 1 ) std::fputs( "Done.\n", stdout );
+ return retval;
}