2025-02-21 11:21:25 +01:00
|
|
|
/* Lziprecover - Data recovery tool for the lzip format
|
|
|
|
Copyright (C) 2009-2014 Antonio Diaz Diaz.
|
2025-02-21 10:09:52 +01:00
|
|
|
|
|
|
|
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
|
2025-02-21 11:20:57 +01:00
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
2025-02-21 10:09:52 +01:00
|
|
|
(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.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
#include <cerrno>
|
|
|
|
#include <climits>
|
|
|
|
#include <cstdio>
|
2025-02-21 11:20:27 +01:00
|
|
|
#include <cstdlib>
|
2025-02-21 10:09:52 +01:00
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <stdint.h>
|
2025-02-21 10:12:48 +01:00
|
|
|
#include <unistd.h>
|
2025-02-21 10:09:52 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "lzip.h"
|
2025-02-21 11:16:43 +01:00
|
|
|
#include "file_index.h"
|
2025-02-21 11:20:27 +01:00
|
|
|
#include "mtester.h"
|
2025-02-21 11:11:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
int seek_write( const int fd, const uint8_t * const buf, const int size,
|
|
|
|
const long long pos )
|
|
|
|
{
|
|
|
|
if( lseek( fd, pos, SEEK_SET ) == pos )
|
|
|
|
return writeblock( fd, buf, size );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-21 10:09:52 +01:00
|
|
|
int repair_file( const std::string & input_filename,
|
2025-02-21 11:11:34 +01:00
|
|
|
const std::string & output_filename, const int verbosity,
|
|
|
|
const bool force )
|
2025-02-21 10:09:52 +01:00
|
|
|
{
|
|
|
|
struct stat in_stats;
|
2025-02-21 11:19:58 +01:00
|
|
|
const int infd = open_instream( input_filename.c_str(), &in_stats, true, true );
|
2025-02-21 10:09:52 +01:00
|
|
|
if( infd < 0 ) return 1;
|
2025-02-21 11:16:43 +01:00
|
|
|
|
2025-02-21 11:19:58 +01:00
|
|
|
Pretty_print pp( input_filename, verbosity );
|
2025-02-21 11:16:43 +01:00
|
|
|
const File_index file_index( infd );
|
|
|
|
if( file_index.retval() != 0 )
|
2025-02-21 11:19:58 +01:00
|
|
|
{ pp( file_index.error().c_str() ); return file_index.retval(); }
|
2025-02-21 11:16:43 +01:00
|
|
|
|
|
|
|
int outfd = -1;
|
2025-02-21 11:20:27 +01:00
|
|
|
for( long i = 0; i < file_index.members(); ++i )
|
2025-02-21 10:09:52 +01:00
|
|
|
{
|
2025-02-21 11:16:43 +01:00
|
|
|
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( 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 ); }
|
|
|
|
|
2025-02-21 11:20:27 +01:00
|
|
|
if( verbosity >= 1 ) // damaged member found
|
2025-02-21 11:16:43 +01:00
|
|
|
{
|
2025-02-21 11:23:19 +01:00
|
|
|
std::printf( "Repairing member %ld of %ld (failure pos = %llu)\n",
|
|
|
|
i + 1, (long)file_index.members(), mpos + failure_pos );
|
2025-02-21 10:09:52 +01:00
|
|
|
std::fflush( stdout );
|
|
|
|
}
|
2025-02-21 11:20:27 +01:00
|
|
|
uint8_t * const mbuffer = read_member( infd, mpos, msize );
|
|
|
|
if( !mbuffer )
|
|
|
|
cleanup_and_fail( output_filename, outfd, 1 );
|
|
|
|
long pos = failure_pos;
|
2025-02-21 11:16:43 +01:00
|
|
|
bool done = false;
|
2025-02-21 11:23:19 +01:00
|
|
|
while( pos >= File_header::size && pos > failure_pos - 40000 && !done )
|
2025-02-21 10:09:52 +01:00
|
|
|
{
|
2025-02-21 11:20:27 +01:00
|
|
|
const long min_pos = std::max( (long)File_header::size, pos - 1000 );
|
|
|
|
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 )
|
2025-02-21 11:16:43 +01:00
|
|
|
{
|
2025-02-21 11:20:27 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2025-02-21 11:16:43 +01:00
|
|
|
}
|
2025-02-21 11:20:27 +01:00
|
|
|
delete master;
|
2025-02-21 11:16:43 +01:00
|
|
|
}
|
2025-02-21 11:20:27 +01:00
|
|
|
delete[] mbuffer;
|
2025-02-21 11:23:19 +01:00
|
|
|
if( verbosity >= 1 ) std::fputs( "\n", stdout );
|
2025-02-21 11:16:43 +01:00
|
|
|
if( !done )
|
|
|
|
{
|
2025-02-21 11:20:27 +01:00
|
|
|
show_error( "Can't repair input file. Error is probably larger than 1 byte." );
|
2025-02-21 11:16:43 +01:00
|
|
|
cleanup_and_fail( output_filename, outfd, 2 );
|
2025-02-21 10:09:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-21 11:16:43 +01:00
|
|
|
if( outfd < 0 )
|
|
|
|
{
|
|
|
|
if( verbosity >= 1 )
|
2025-02-21 11:23:19 +01:00
|
|
|
std::fputs( "Input file has no errors. Recovery is not needed.\n", stdout );
|
2025-02-21 11:16:43 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2025-02-21 10:09:52 +01:00
|
|
|
if( close( outfd ) != 0 )
|
|
|
|
{
|
|
|
|
show_error( "Error closing output file", errno );
|
|
|
|
cleanup_and_fail( output_filename, -1, 1 );
|
|
|
|
}
|
|
|
|
if( verbosity >= 1 )
|
2025-02-21 11:23:19 +01:00
|
|
|
std::fputs( "Copy of input file repaired successfully.\n", stdout );
|
2025-02-21 10:09:52 +01:00
|
|
|
return 0;
|
|
|
|
}
|