summaryrefslogtreecommitdiffstats
path: root/byte_repair.cc
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-06-16 11:13:24 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-06-16 11:13:24 +0000
commitd7c278fca708bc2c4badaeac041ecb136a4ef955 (patch)
tree5b163b83e6e3171a311f67187aa75c0f96f98f13 /byte_repair.cc
parentAdding upstream version 1.23. (diff)
downloadlziprecover-d7c278fca708bc2c4badaeac041ecb136a4ef955.tar.xz
lziprecover-d7c278fca708bc2c4badaeac041ecb136a4ef955.zip
Adding upstream version 1.24~pre1.upstream/1.24_pre1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--byte_repair.cc (renamed from repair.cc)106
1 files changed, 54 insertions, 52 deletions
diff --git a/repair.cc b/byte_repair.cc
index c49fbdb..3e92ca4 100644
--- a/repair.cc
+++ b/byte_repair.cc
@@ -1,5 +1,5 @@
/* Lziprecover - Data recovery tool for the lzip format
- Copyright (C) 2009-2022 Antonio Diaz Diaz.
+ Copyright (C) 2009-2023 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
@@ -43,7 +43,7 @@ void print_pending_newline( const char terminator )
pending_newline = false; }
-bool gross_damage( const long long msize, const uint8_t * const mbuffer )
+bool gross_damage( const uint8_t * const mbuffer, const long msize )
{
enum { maxlen = 7 }; // max number of consecutive identical bytes
long i = Lzip_header::size;
@@ -59,9 +59,8 @@ bool gross_damage( const long long msize, const uint8_t * const mbuffer )
// Return value: 0 = no change, 5 = repaired pos
-int repair_dictionary_size( const long long msize, uint8_t * const mbuffer )
+int repair_dictionary_size( uint8_t * const mbuffer, const long msize )
{
- const unsigned long long dictionary_size_9 = 1 << 25; // dict size of opt -9
Lzip_header & header = *(Lzip_header *)mbuffer;
unsigned dictionary_size = header.dictionary_size();
const Lzip_trailer & trailer =
@@ -70,6 +69,7 @@ int repair_dictionary_size( const long long msize, uint8_t * const mbuffer )
const bool valid_ds = isvalid_ds( dictionary_size );
if( valid_ds && dictionary_size >= data_size ) return 0; // can't be bad
+ const unsigned long long dictionary_size_9 = 1 << 25; // dict size of opt -9
if( !valid_ds || dictionary_size < dictionary_size_9 )
{
dictionary_size = std::min( data_size, dictionary_size_9 );
@@ -118,9 +118,9 @@ bool test_member_rest( const LZ_mtester & master, uint8_t * const buffer2,
}
-// 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,
+// Return value: -1 = master failed, 0 = begin reached, > 0 = repaired pos
+long repair_member( uint8_t * const mbuffer, const long long mpos,
+ const long msize, const long begin, const long end,
const unsigned dictionary_size, const char terminator )
{
uint8_t * const buffer2 = new uint8_t[dictionary_size];
@@ -155,8 +155,8 @@ long repair_member( const long long mpos, const long long msize,
} // end namespace
-long long seek_write( const int fd, const uint8_t * const buf,
- const long long size, const long long pos )
+long seek_write( const int fd, const uint8_t * const buf, const long size,
+ const long long pos )
{
if( lseek( fd, pos, SEEK_SET ) == pos )
return writeblock( fd, buf, size );
@@ -165,43 +165,45 @@ long long seek_write( const int fd, const uint8_t * const buf,
uint8_t * read_member( const int infd, const long long mpos,
- const long long msize )
+ const long long msize, const char * const filename )
{
if( msize <= 0 || msize > LONG_MAX )
- { show_error( "Member is larger than LONG_MAX." ); return 0; }
- if( !safe_seek( infd, mpos ) ) return 0;
+ { show_file_error( filename,
+ "Input file contains member larger than LONG_MAX." ); return 0; }
+ if( !safe_seek( infd, mpos, filename ) ) return 0;
uint8_t * const buffer = new uint8_t[msize];
if( readblock( infd, buffer, msize ) != msize )
- { show_error( "Error reading input file", errno );
+ { show_file_error( filename, "Error reading input file", errno );
delete[] buffer; return 0; }
return buffer;
}
-int repair_file( const std::string & input_filename,
+int byte_repair( const std::string & input_filename,
const std::string & default_output_filename,
+ const Cl_options & cl_opts,
const char terminator, const bool force )
{
+ const char * const filename = input_filename.c_str();
struct stat in_stats;
- const int infd =
- open_instream( input_filename.c_str(), &in_stats, false, true );
+ const int infd = open_instream( filename, &in_stats, false, true );
if( infd < 0 ) return 1;
- const Lzip_index lzip_index( infd, true, true, true );
+ const Lzip_index lzip_index( infd, cl_opts, true );
if( lzip_index.retval() != 0 )
- { show_file_error( input_filename.c_str(), lzip_index.error().c_str() );
+ { 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;
- if( !force && file_exists( output_filename ) ) return 1;
+ if( !force && output_file_exists() ) return 1;
outfd = -1;
for( long i = 0; i < lzip_index.members(); ++i )
{
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 );
+ if( !safe_seek( infd, mpos, filename ) ) cleanup_and_fail( 1 );
long long failure_pos = 0;
if( test_member_from_file( infd, msize, &failure_pos ) == 0 ) continue;
if( failure_pos < Lzip_header::size ) // End Of File
@@ -215,19 +217,19 @@ int repair_file( const std::string & input_filename,
i + 1, lzip_index.members(), mpos + failure_pos );
std::fflush( stdout );
}
- uint8_t * const mbuffer = read_member( infd, mpos, msize );
+ uint8_t * const mbuffer = read_member( infd, mpos, msize, filename );
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 ) )
+ if( !gross_damage( mbuffer, msize ) )
{
- pos = repair_dictionary_size( msize, mbuffer );
+ pos = repair_dictionary_size( mbuffer, msize );
if( pos == 0 )
- pos = repair_member( mpos, msize, mbuffer, Lzip_header::size + 1,
+ pos = repair_member( mbuffer, mpos, msize, Lzip_header::size + 1,
Lzip_header::size + 6, dictionary_size, terminator );
if( pos == 0 )
- pos = repair_member( mpos, msize, mbuffer, Lzip_header::size + 7,
+ pos = repair_member( mbuffer, mpos, msize, Lzip_header::size + 7,
failure_pos, dictionary_size, terminator );
print_pending_newline( terminator );
}
@@ -237,7 +239,7 @@ int repair_file( const std::string & input_filename,
{
if( outfd < 0 ) // first damaged member repaired
{
- if( !safe_seek( infd, 0 ) ) return 1;
+ if( !safe_seek( infd, 0, filename ) ) return 1;
set_signal_handler();
if( !open_outstream( true, true ) ) return 1;
if( !copy_file( infd, outfd ) ) // copy whole file
@@ -268,23 +270,23 @@ int repair_file( const std::string & input_filename,
}
-int debug_delay( const std::string & input_filename, Block range,
+int debug_delay( const char * const input_filename,
+ const Cl_options & cl_opts, Block range,
const char terminator )
{
struct stat in_stats; // not used
- const int infd =
- open_instream( input_filename.c_str(), &in_stats, false, true );
+ const int infd = open_instream( input_filename, &in_stats, false, true );
if( infd < 0 ) return 1;
- const Lzip_index lzip_index( infd, true, true );
+ const Lzip_index lzip_index( infd, cl_opts );
if( lzip_index.retval() != 0 )
- { show_file_error( input_filename.c_str(), lzip_index.error().c_str() );
+ { show_file_error( input_filename, lzip_index.error().c_str() );
return lzip_index.retval(); }
if( range.end() > lzip_index.cdata_size() )
range.size( std::max( 0LL, lzip_index.cdata_size() - range.pos() ) );
if( range.size() <= 0 )
- { show_file_error( input_filename.c_str(), "Nothing to do." ); return 0; }
+ { show_file_error( input_filename, "Nothing to do." ); return 0; }
for( long i = 0; i < lzip_index.members(); ++i )
{
@@ -299,7 +301,7 @@ int debug_delay( const std::string & input_filename, Block range,
i + 1, lzip_index.members(), mpos, msize );
std::fflush( stdout );
}
- uint8_t * const mbuffer = read_member( infd, mpos, msize );
+ uint8_t * const mbuffer = read_member( infd, mpos, msize, input_filename );
if( !mbuffer ) return 1;
uint8_t * const buffer2 = new uint8_t[dictionary_size];
long pos = std::max( range.pos() - mpos, Lzip_header::size + 1LL );
@@ -350,30 +352,30 @@ int debug_delay( const std::string & input_filename, Block range,
}
-int debug_repair( const std::string & input_filename,
- const Bad_byte & bad_byte, const char terminator )
+int debug_byte_repair( const char * const input_filename,
+ const Cl_options & cl_opts, 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, false, true );
+ const int infd = open_instream( input_filename, &in_stats, false, true );
if( infd < 0 ) return 1;
- const Lzip_index lzip_index( infd, true, true );
+ const Lzip_index lzip_index( infd, cl_opts );
if( lzip_index.retval() != 0 )
- { show_file_error( input_filename.c_str(), lzip_index.error().c_str() );
+ { show_file_error( input_filename, lzip_index.error().c_str() );
return lzip_index.retval(); }
long idx = 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; }
+ { show_file_error( input_filename, "Nothing to do." ); return 0; }
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( !safe_seek( infd, mpos, input_filename ) ) return 1;
if( test_member_from_file( infd, msize, &failure_pos ) != 0 )
{
if( verbosity >= 0 )
@@ -382,7 +384,7 @@ int debug_repair( const std::string & input_filename,
return 2;
}
}
- uint8_t * const mbuffer = read_member( infd, mpos, msize );
+ uint8_t * const mbuffer = read_member( infd, mpos, msize, input_filename );
if( !mbuffer ) return 1;
const Lzip_header & header = *(const Lzip_header *)mbuffer;
const unsigned dictionary_size = header.dictionary_size();
@@ -412,12 +414,12 @@ int debug_repair( const std::string & input_filename,
std::fflush( stdout );
}
if( failure_pos >= msize ) failure_pos = msize - 1;
- long pos = repair_dictionary_size( msize, mbuffer );
+ long pos = repair_dictionary_size( mbuffer, msize );
if( pos == 0 )
- pos = repair_member( mpos, msize, mbuffer, Lzip_header::size + 1,
+ pos = repair_member( mbuffer, mpos, msize, Lzip_header::size + 1,
Lzip_header::size + 6, dictionary_size, terminator );
if( pos == 0 )
- pos = repair_member( mpos, msize, mbuffer, Lzip_header::size + 7,
+ pos = repair_member( mbuffer, mpos, msize, Lzip_header::size + 7,
failure_pos, dictionary_size, terminator );
print_pending_newline( terminator );
delete[] mbuffer;
@@ -439,17 +441,17 @@ int debug_repair( const std::string & input_filename,
include the 5 bytes read by rdec.load).
if bad_byte.pos >= cdata_size, bad_byte is ignored.
*/
-int debug_decompress( const std::string & input_filename,
- const Bad_byte & bad_byte, const bool show_packets )
+int debug_decompress( const char * const input_filename,
+ const Cl_options & cl_opts, const Bad_byte & bad_byte,
+ const bool show_packets )
{
struct stat in_stats;
- const int infd =
- open_instream( input_filename.c_str(), &in_stats, false, true );
+ const int infd = open_instream( input_filename, &in_stats, false, true );
if( infd < 0 ) return 1;
- const Lzip_index lzip_index( infd, true, true );
+ const Lzip_index lzip_index( infd, cl_opts );
if( lzip_index.retval() != 0 )
- { show_file_error( input_filename.c_str(), lzip_index.error().c_str() );
+ { show_file_error( input_filename, lzip_index.error().c_str() );
return lzip_index.retval(); }
outfd = show_packets ? -1 : STDOUT_FILENO;
@@ -466,7 +468,7 @@ int debug_decompress( const std::string & input_filename,
i + 1, lzip_index.members(), mpos, msize );
if( !isvalid_ds( dictionary_size ) )
{ show_error( bad_dict_msg ); retval = 2; break; }
- uint8_t * const mbuffer = read_member( infd, mpos, msize );
+ uint8_t * const mbuffer = read_member( infd, mpos, msize, input_filename );
if( !mbuffer ) { retval = 1; break; }
if( bad_byte.pos >= 0 && lzip_index.mblock( i ).includes( bad_byte.pos ) )
{