diff options
Diffstat (limited to 'repair.cc')
-rw-r--r-- | repair.cc | 214 |
1 files changed, 123 insertions, 91 deletions
@@ -1,5 +1,5 @@ /* Lziprecover - Data recovery tool for the lzip format - Copyright (C) 2009-2018 Antonio Diaz Diaz. + Copyright (C) 2009-2019 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 @@ -31,23 +31,38 @@ #include "lzip.h" #include "mtester.h" -#include "block.h" -#include "file_index.h" +#include "lzip_index.h" namespace { bool pending_newline = false; -void print_pending_newline() - { if( pending_newline ) std::fputc( '\n', stdout ); pending_newline = false; } +void print_pending_newline( const char terminator ) + { if( pending_newline && terminator != '\n' ) std::fputc( '\n', stdout ); + pending_newline = false; } + + +uint8_t * read_member( const int infd, const long long mpos, + const long long msize ) + { + if( msize <= 0 || msize > LONG_MAX ) + { show_error( "Member is larger than LONG_MAX." ); return 0; } + if( !safe_seek( infd, mpos ) ) return 0; + uint8_t * const buffer = new uint8_t[msize]; + + if( readblock( infd, buffer, msize ) != msize ) + { show_error( "Error reading input file", errno ); + delete[] buffer; return 0; } + return buffer; + } 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; + long i = Lzip_header::size; + const long end = msize - Lzip_trailer::size - maxlen; while( i < end ) { const uint8_t byte = mbuffer[i]; @@ -71,10 +86,10 @@ int seek_write( const int fd, const uint8_t * const buf, const int size, int repair_dictionary_size( const long long msize, uint8_t * const mbuffer ) { enum { dictionary_size_9 = 1 << 25 }; // dictionary size of option -9 - File_header & header = *(File_header *)mbuffer; + Lzip_header & header = *(Lzip_header *)mbuffer; unsigned dictionary_size = header.dictionary_size(); - File_trailer & trailer = - *(File_trailer *)( mbuffer + msize - File_trailer::size ); + const Lzip_trailer & trailer = + *(const Lzip_trailer *)( mbuffer + msize - Lzip_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 @@ -104,10 +119,33 @@ int repair_dictionary_size( const long long msize, uint8_t * const mbuffer ) } +const LZ_mtester * prepare_master( const uint8_t * const buffer, + const long buffer_size, + const unsigned long pos_limit, + const unsigned dictionary_size ) + { + LZ_mtester * const master = + new LZ_mtester( buffer, buffer_size, dictionary_size ); + if( master->test_member( pos_limit ) == -1 ) return master; + delete master; + return 0; + } + + +bool test_member_rest( const LZ_mtester & master, long * const failure_posp = 0 ) + { + LZ_mtester mtester( master ); + mtester.duplicate_buffer(); + if( mtester.test_member() == 0 && mtester.finished() ) return true; + if( failure_posp ) *failure_posp = mtester.member_position(); + return false; + } + + // 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 unsigned dictionary_size ) + const unsigned dictionary_size, const char terminator ) { for( long pos = end; pos >= begin && pos > end - 50000; ) { @@ -120,7 +158,7 @@ long repair_member( const long long mpos, const long long msize, { if( verbosity >= 2 ) { - std::printf( " Trying position %llu \r", mpos + pos ); + std::printf( " Trying position %llu %c", mpos + pos, terminator ); std::fflush( stdout ); pending_newline = true; } for( int j = 0; j < 255; ++j ) @@ -140,65 +178,62 @@ long repair_member( const long long mpos, const long long msize, int repair_file( const std::string & input_filename, const std::string & default_output_filename, - const bool force ) + const bool force, const char terminator ) { 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 ); - const File_index file_index( infd, true, true, true ); - if( file_index.retval() != 0 ) - { show_file_error( input_filename.c_str(), file_index.error().c_str() ); - return file_index.retval(); } + const Lzip_index lzip_index( infd, true, true, true ); + if( lzip_index.retval() != 0 ) + { show_file_error( input_filename.c_str(), lzip_index.error().c_str() ); + return lzip_index.retval(); } output_filename = default_output_filename.empty() ? insert_fixed( input_filename ) : default_output_filename; if( !force && file_exists( output_filename ) ) return 1; outfd = -1; - for( long i = 0; i < file_index.members(); ++i ) + for( long i = 0; i < lzip_index.members(); ++i ) { - const long long mpos = file_index.mblock( i ).pos(); - const long long msize = file_index.mblock( i ).size(); - if( !safe_seek( infd, mpos ) ) - cleanup_and_fail( 1 ); + const long long mpos = lzip_index.mblock( i ).pos(); + const long long msize = lzip_index.mblock( i ).size(); + if( !safe_seek( infd, mpos ) ) cleanup_and_fail( 1 ); long long failure_pos = 0; - if( test_member_from_file( infd, msize, &failure_pos ) ) continue; - if( failure_pos < File_header::size ) // End Of File + if( test_member_from_file( infd, msize, &failure_pos ) == 0 ) continue; + if( failure_pos < Lzip_header::size ) // End Of File { show_error( "Can't repair error in input file." ); cleanup_and_fail( 2 ); } + if( failure_pos >= msize - 8 ) failure_pos = msize - 8 - 1; 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 ); + i + 1, lzip_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( 1 ); - const File_header & header = *(File_header *)mbuffer; + if( !mbuffer ) cleanup_and_fail( 1 ); + const Lzip_header & header = *(const Lzip_header *)mbuffer; const unsigned dictionary_size = header.dictionary_size(); long pos = 0; if( !gross_damage( msize, mbuffer ) ) { pos = repair_dictionary_size( msize, mbuffer ); if( pos == 0 ) - pos = repair_member( mpos, msize, mbuffer, File_header::size + 1, - File_header::size + 5, dictionary_size ); + pos = repair_member( mpos, msize, mbuffer, Lzip_header::size + 1, + Lzip_header::size + 5, dictionary_size, terminator ); if( pos == 0 ) - pos = repair_member( mpos, msize, mbuffer, File_header::size + 6, - failure_pos, dictionary_size ); - print_pending_newline(); + pos = repair_member( mpos, msize, mbuffer, Lzip_header::size + 6, + failure_pos, dictionary_size, terminator ); + print_pending_newline( terminator ); } - if( pos < 0 ) - cleanup_and_fail( 1 ); + if( pos < 0 ) cleanup_and_fail( 1 ); if( pos > 0 ) { if( outfd < 0 ) // first damaged member repaired { if( !safe_seek( infd, 0 ) ) return 1; + set_signal_handler(); if( !open_outstream( true, false ) ) { close( infd ); return 1; } if( !copy_file( infd, outfd ) ) // copy whole file cleanup_and_fail( 1 ); @@ -228,39 +263,39 @@ int repair_file( const std::string & input_filename, } -int debug_delay( const std::string & input_filename, Block range ) +int debug_delay( const std::string & input_filename, Block range, + const char terminator ) { 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 ); - const File_index file_index( infd, false, true, true ); - if( file_index.retval() != 0 ) - { show_file_error( input_filename.c_str(), file_index.error().c_str() ); - return file_index.retval(); } + const Lzip_index lzip_index( infd, true, true ); + if( lzip_index.retval() != 0 ) + { show_file_error( input_filename.c_str(), lzip_index.error().c_str() ); + return lzip_index.retval(); } - if( range.end() > file_index.cdata_size() ) - range.size( std::max( 0LL, file_index.cdata_size() - range.pos() ) ); + if( range.end() > lzip_index.cdata_size() ) + range.size( std::max( 0LL, lzip_index.cdata_size() - range.pos() ) ); if( range.size() <= 0 ) - { pp( "Nothing to do." ); return 0; } + { show_file_error( input_filename.c_str(), "Nothing to do." ); return 0; } - for( long i = 0; i < file_index.members(); ++i ) + for( long i = 0; i < lzip_index.members(); ++i ) { - const Block & mb = file_index.mblock( i ); + const Block & mb = lzip_index.mblock( i ); if( !range.overlaps( mb ) ) continue; - 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 ); + const long long mpos = lzip_index.mblock( i ).pos(); + const long long msize = lzip_index.mblock( i ).size(); + const unsigned dictionary_size = lzip_index.dictionary_size( i ); 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 ); + i + 1, lzip_index.members(), mpos, msize ); std::fflush( stdout ); } uint8_t * const mbuffer = read_member( infd, mpos, msize ); if( !mbuffer ) return 1; - long pos = std::max( range.pos() - mpos, File_header::size + 1LL ); + long pos = std::max( range.pos() - mpos, Lzip_header::size + 1LL ); const long end = std::min( range.end() - mpos, msize ); long max_delay = 0; while( pos < end ) @@ -275,7 +310,7 @@ int debug_delay( const std::string & input_filename, Block range ) { if( verbosity >= 2 ) { - std::printf( " Delays at position %llu \r", mpos + pos ); + std::printf( " Delays at position %llu %c", mpos + pos, terminator ); std::fflush( stdout ); pending_newline = true; } int value = -1; @@ -299,7 +334,7 @@ int debug_delay( const std::string & input_filename, Block range ) delete master; } delete[] mbuffer; - print_pending_newline(); + print_pending_newline( terminator ); } if( verbosity >= 1 ) std::fputs( "Done.\n", stdout ); @@ -308,40 +343,39 @@ int debug_delay( const std::string & input_filename, Block range ) int debug_repair( const std::string & input_filename, - const Bad_byte & bad_byte ) + const Bad_byte & bad_byte, const char terminator ) { 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 ); - const File_index file_index( infd, false, true, true ); - if( file_index.retval() != 0 ) - { show_file_error( input_filename.c_str(), file_index.error().c_str() ); - return file_index.retval(); } + const Lzip_index lzip_index( infd, true, true ); + if( lzip_index.retval() != 0 ) + { show_file_error( input_filename.c_str(), lzip_index.error().c_str() ); + return lzip_index.retval(); } long idx = 0; - for( ; idx < file_index.members(); ++idx ) - if( file_index.mblock( idx ).includes( bad_byte.pos ) ) break; - if( idx >= file_index.members() ) - { pp( "Nothing to do." ); return 0; } + for( ; idx < lzip_index.members(); ++idx ) + if( lzip_index.mblock( idx ).includes( bad_byte.pos ) ) break; + if( idx >= lzip_index.members() ) + { show_file_error( input_filename.c_str(), "Nothing to do." ); return 0; } - const long long mpos = file_index.mblock( idx ).pos(); - const long long msize = file_index.mblock( idx ).size(); + const long long mpos = lzip_index.mblock( idx ).pos(); + const long long msize = lzip_index.mblock( idx ).size(); { long long failure_pos = 0; if( !safe_seek( infd, mpos ) ) return 1; - if( !test_member_from_file( infd, msize, &failure_pos ) ) + if( test_member_from_file( infd, msize, &failure_pos ) != 0 ) { if( verbosity >= 0 ) std::printf( "Member %ld of %ld already damaged (failure pos = %llu)\n", - idx + 1, file_index.members(), mpos + failure_pos ); + idx + 1, lzip_index.members(), mpos + failure_pos ); return 1; } } uint8_t * const mbuffer = read_member( infd, mpos, msize ); if( !mbuffer ) return 1; - const File_header & header = *(File_header *)mbuffer; + const Lzip_header & header = *(const Lzip_header *)mbuffer; const unsigned dictionary_size = header.dictionary_size(); const uint8_t good_value = mbuffer[bad_byte.pos-mpos]; const uint8_t bad_value = bad_byte( good_value ); @@ -367,7 +401,7 @@ int debug_repair( const std::string & input_filename, { std::printf( "Test repairing member %ld of %ld (mpos = %llu, msize = %llu)\n" " (damage pos = %llu (0x%02X->0x%02X), failure pos = %llu, delay = %lld )\n", - idx + 1, file_index.members(), mpos, msize, + idx + 1, lzip_index.members(), mpos, msize, bad_byte.pos, good_value, bad_value, mpos + failure_pos, mpos + failure_pos - bad_byte.pos ); std::fflush( stdout ); @@ -375,12 +409,12 @@ int debug_repair( const std::string & input_filename, if( failure_pos >= msize ) failure_pos = msize - 1; long pos = repair_dictionary_size( msize, mbuffer ); if( pos == 0 ) - pos = repair_member( mpos, msize, mbuffer, File_header::size + 1, - File_header::size + 5, dictionary_size ); + pos = repair_member( mpos, msize, mbuffer, Lzip_header::size + 1, + Lzip_header::size + 5, dictionary_size, terminator ); if( pos == 0 ) - pos = repair_member( mpos, msize, mbuffer, File_header::size + 6, - failure_pos, dictionary_size ); - print_pending_newline(); + pos = repair_member( mpos, msize, mbuffer, Lzip_header::size + 6, + failure_pos, dictionary_size, terminator ); + print_pending_newline( terminator ); delete[] mbuffer; if( pos < 0 ) { show_error( "Can't prepare master." ); return 1; } @@ -398,30 +432,28 @@ int debug_decompress( const std::string & input_filename, const int infd = open_instream( input_filename.c_str(), &in_stats, true, true ); if( infd < 0 ) return 1; - Pretty_print pp( input_filename ); - const File_index file_index( infd, false, true, true ); - if( file_index.retval() != 0 ) - { show_file_error( input_filename.c_str(), file_index.error().c_str() ); - return file_index.retval(); } + const Lzip_index lzip_index( infd, true, true ); + if( lzip_index.retval() != 0 ) + { show_file_error( input_filename.c_str(), lzip_index.error().c_str() ); + return lzip_index.retval(); } outfd = show_packets ? -1 : STDOUT_FILENO; int retval = 0; - for( long i = 0; i < file_index.members(); ++i ) + for( long i = 0; i < lzip_index.members(); ++i ) { - 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(); - const unsigned dictionary_size = file_index.dictionary_size( i ); + const long long dpos = lzip_index.dblock( i ).pos(); + const long long mpos = lzip_index.mblock( i ).pos(); + const long long msize = lzip_index.mblock( i ).size(); + const unsigned dictionary_size = lzip_index.dictionary_size( i ); if( verbosity >= 1 && show_packets ) 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 ); + i + 1, lzip_index.members(), mpos, msize ); if( !isvalid_ds( dictionary_size ) ) - { show_error( "Invalid dictionary size in member header." ); - retval = 2; break; } + { show_error( bad_dict_msg ); retval = 2; break; } uint8_t * const mbuffer = read_member( infd, mpos, msize ); if( !mbuffer ) { retval = 1; break; } - if( bad_byte.pos >= 0 && file_index.mblock( i ).includes( bad_byte.pos ) ) + if( bad_byte.pos >= 0 && lzip_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 ); @@ -441,7 +473,7 @@ int debug_decompress( const std::string & input_filename, mpos + mtester.member_position() ); retval = 2; break; } - if( i + 1 < file_index.members() && show_packets ) + if( i + 1 < lzip_index.members() && show_packets ) std::fputc( '\n', stdout ); } |