2025-02-21 11:17:33 +01:00
|
|
|
/* Lziprecover - Data recovery tool for lzip files
|
2025-02-21 10:16:51 +01:00
|
|
|
Copyright (C) 2009, 2010, 2011, 2012, 2013 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
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INT64_MAX
|
|
|
|
#define INT64_MAX 0x7FFFFFFFFFFFFFFFLL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
class Block
|
|
|
|
{
|
|
|
|
long long pos_, size_; // pos + size <= INT64_MAX
|
|
|
|
|
|
|
|
public:
|
2025-02-21 11:16:33 +01:00
|
|
|
Block( const long long p, const long long s )
|
|
|
|
: pos_( p ), size_( s ) {}
|
2025-02-21 10:16:51 +01:00
|
|
|
|
|
|
|
long long pos() const { return pos_; }
|
|
|
|
long long size() const { return size_; }
|
|
|
|
long long end() const { return pos_ + size_; }
|
|
|
|
|
|
|
|
void pos( const long long p ) { pos_ = p; }
|
|
|
|
void size( const long long s ) { size_ = s; }
|
|
|
|
|
2025-02-21 11:16:33 +01:00
|
|
|
bool operator==( const Block & b ) const
|
|
|
|
{ return pos_ == b.pos_ && size_ == b.size_; }
|
|
|
|
bool operator!=( const Block & b ) const
|
|
|
|
{ return pos_ != b.pos_ || size_ != b.size_; }
|
|
|
|
|
|
|
|
bool operator<( const Block & b ) const { return pos_ < b.pos_; }
|
|
|
|
|
2025-02-21 10:16:51 +01:00
|
|
|
bool overlaps( const Block & b ) const
|
|
|
|
{ return ( pos_ < b.end() && b.pos_ < end() ); }
|
|
|
|
void shift( Block & b ) { ++size_; ++b.pos_; --b.size_; }
|
2025-02-21 11:16:33 +01:00
|
|
|
Block split( const long long pos );
|
2025-02-21 10:16:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class File_index
|
|
|
|
{
|
|
|
|
struct Member
|
|
|
|
{
|
|
|
|
Block dblock, mblock; // data block, member block
|
|
|
|
|
|
|
|
Member( const long long dp, const long long ds,
|
|
|
|
const long long mp, const long long ms )
|
|
|
|
: dblock( dp, ds ), mblock( mp, ms ) {}
|
2025-02-21 11:16:33 +01:00
|
|
|
|
|
|
|
bool operator==( const Member & m ) const { return ( mblock == m.mblock ); }
|
|
|
|
bool operator!=( const Member & m ) const { return ( mblock != m.mblock ); }
|
2025-02-21 10:16:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
std::vector< Member > member_vector;
|
|
|
|
std::string error_;
|
2025-02-21 11:16:33 +01:00
|
|
|
long long isize;
|
2025-02-21 10:16:51 +01:00
|
|
|
int retval_;
|
|
|
|
|
|
|
|
public:
|
2025-02-21 11:16:33 +01:00
|
|
|
File_index() : error_( "No index." ), isize( 0 ), retval_( 2 ) {}
|
|
|
|
explicit File_index( const int infd );
|
|
|
|
File_index( const std::vector< int > & infd_vector, const long long fsize );
|
2025-02-21 10:16:51 +01:00
|
|
|
|
2025-02-21 11:16:33 +01:00
|
|
|
int members() const { return member_vector.size(); }
|
2025-02-21 10:16:51 +01:00
|
|
|
const std::string & error() const { return error_; }
|
|
|
|
int retval() const { return retval_; }
|
|
|
|
|
2025-02-21 11:16:33 +01:00
|
|
|
bool operator==( const File_index & fi ) const
|
|
|
|
{
|
|
|
|
if( retval_ || fi.retval_ || isize != fi.isize ||
|
|
|
|
member_vector.size() != fi.member_vector.size() ) return false;
|
|
|
|
for( unsigned i = 0; i < member_vector.size(); ++i )
|
|
|
|
if( member_vector[i] != fi.member_vector[i] ) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool operator!=( const File_index & fi ) const { return !( *this == fi ); }
|
|
|
|
|
2025-02-21 10:16:51 +01:00
|
|
|
long long data_end() const
|
|
|
|
{ if( member_vector.size() ) return member_vector.back().dblock.end();
|
|
|
|
else return 0; }
|
|
|
|
|
|
|
|
long long file_end() const
|
|
|
|
{ if( member_vector.size() ) return member_vector.back().mblock.end();
|
|
|
|
else return 0; }
|
|
|
|
|
2025-02-21 11:16:33 +01:00
|
|
|
// total size including trailing garbage (if any)
|
|
|
|
long long file_size() const
|
|
|
|
{ if( isize >= 0 ) return isize; else return 0; }
|
|
|
|
|
2025-02-21 10:16:51 +01:00
|
|
|
const Block & dblock( const int i ) const
|
|
|
|
{ return member_vector[i].dblock; }
|
|
|
|
const Block & mblock( const int i ) const
|
|
|
|
{ return member_vector[i].mblock; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const char * format_num( unsigned long long num,
|
|
|
|
unsigned long long limit = -1ULL,
|
|
|
|
const int set_prefix = 0 );
|