2025-02-21 11:30:52 +01:00
|
|
|
/* Lziprecover - Data recovery tool for the lzip format
|
2025-02-21 11:32:35 +01:00
|
|
|
Copyright (C) 2009-2024 Antonio Diaz Diaz.
|
2025-02-21 10:16:51 +01:00
|
|
|
|
2025-02-21 11:30: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
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2025-02-21 10:16:51 +01:00
|
|
|
|
2025-02-21 11:30:52 +01:00
|
|
|
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.
|
2025-02-21 10:16:51 +01:00
|
|
|
|
2025-02-21 11:30:52 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2025-02-21 10:16:51 +01:00
|
|
|
*/
|
|
|
|
|
2025-02-21 11:29:28 +01:00
|
|
|
class Lzip_index
|
2025-02-21 10:16:51 +01:00
|
|
|
{
|
|
|
|
struct Member
|
|
|
|
{
|
|
|
|
Block dblock, mblock; // data block, member block
|
2025-02-21 11:25:51 +01:00
|
|
|
unsigned dictionary_size;
|
2025-02-21 10:16:51 +01:00
|
|
|
|
2025-02-21 11:32:14 +01:00
|
|
|
Member( const long long dpos, const long long dsize,
|
|
|
|
const long long mpos, const long long msize,
|
|
|
|
const unsigned dict_size )
|
|
|
|
: dblock( dpos, dsize ), mblock( mpos, msize ),
|
|
|
|
dictionary_size( dict_size ) {}
|
2025-02-21 11:16:33 +01:00
|
|
|
|
2025-02-21 11:33:08 +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
|
|
|
};
|
|
|
|
|
2025-02-21 11:30:52 +01:00
|
|
|
// member_vector only contains members with a valid header.
|
2025-02-21 11:29:28 +01:00
|
|
|
// Garbage between members is represented by gaps between mblocks.
|
2025-02-21 10:16:51 +01:00
|
|
|
std::vector< Member > member_vector;
|
|
|
|
std::string error_;
|
2025-02-21 11:29:28 +01:00
|
|
|
long long insize;
|
2025-02-21 10:16:51 +01:00
|
|
|
int retval_;
|
2025-02-21 11:30:52 +01:00
|
|
|
unsigned dictionary_size_; // largest dictionary size in the file
|
2025-02-21 10:16:51 +01:00
|
|
|
|
2025-02-21 11:32:35 +01:00
|
|
|
bool check_header( const Lzip_header & header, const bool ignore_bad_ds );
|
2025-02-21 11:19:49 +01:00
|
|
|
void set_errno_error( const char * const msg );
|
2025-02-21 11:27:50 +01:00
|
|
|
void set_num_error( const char * const msg, unsigned long long num );
|
2025-02-21 11:33:08 +01:00
|
|
|
bool read_header( const int fd, Lzip_header & header, const long long pos );
|
2025-02-21 11:30:52 +01:00
|
|
|
bool read_trailer( const int fd, Lzip_trailer & trailer,
|
|
|
|
const long long pos );
|
|
|
|
bool skip_gap( const int fd, unsigned long long & pos,
|
2025-02-21 11:32:14 +01:00
|
|
|
const Cl_options & cl_opts,
|
2025-02-21 11:30:52 +01:00
|
|
|
const bool ignore_bad_ds, const bool ignore_gaps );
|
2025-02-21 11:19:49 +01:00
|
|
|
|
2025-02-21 10:16:51 +01:00
|
|
|
public:
|
2025-02-21 11:30:52 +01:00
|
|
|
Lzip_index()
|
2025-02-21 11:32:35 +01:00
|
|
|
: error_( "No index" ), insize( 0 ), retval_( 2 ), dictionary_size_( 0 ) {}
|
2025-02-21 11:32:14 +01:00
|
|
|
Lzip_index( const int infd, const Cl_options & cl_opts,
|
|
|
|
const bool ignore_bad_ds = false, const bool ignore_gaps = false,
|
|
|
|
const long long max_pos = 0 );
|
2025-02-21 11:29:28 +01:00
|
|
|
Lzip_index( const std::vector< int > & infd_vector, const long long fsize );
|
2025-02-21 10:16:51 +01:00
|
|
|
|
2025-02-21 11:20:21 +01:00
|
|
|
long members() const { return member_vector.size(); }
|
2025-02-21 11:29:28 +01:00
|
|
|
long blocks( const bool count_tdata ) const; // members + gaps [+ tdata]
|
2025-02-21 10:16:51 +01:00
|
|
|
const std::string & error() const { return error_; }
|
|
|
|
int retval() const { return retval_; }
|
2025-02-21 11:30:52 +01:00
|
|
|
unsigned dictionary_size() const { return dictionary_size_; }
|
2025-02-21 10:16:51 +01:00
|
|
|
|
2025-02-21 11:33:20 +01:00
|
|
|
bool multi_empty() const // multimember file with empty member(s)
|
|
|
|
{
|
|
|
|
if( member_vector.size() > 1 )
|
|
|
|
for( unsigned long i = 0; i < member_vector.size(); ++i )
|
|
|
|
if( member_vector[i].dblock.size() == 0 ) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-02-21 11:29:28 +01:00
|
|
|
bool operator==( const Lzip_index & li ) const
|
2025-02-21 11:16:33 +01:00
|
|
|
{
|
2025-02-21 11:29:28 +01:00
|
|
|
if( retval_ || li.retval_ || insize != li.insize ||
|
|
|
|
member_vector.size() != li.member_vector.size() ) return false;
|
2025-02-21 11:20:21 +01:00
|
|
|
for( unsigned long i = 0; i < member_vector.size(); ++i )
|
2025-02-21 11:29:28 +01:00
|
|
|
if( member_vector[i] != li.member_vector[i] ) return false;
|
2025-02-21 11:16:33 +01:00
|
|
|
return true;
|
|
|
|
}
|
2025-02-21 11:29:28 +01:00
|
|
|
bool operator!=( const Lzip_index & li ) const { return !( *this == li ); }
|
2025-02-21 11:16:33 +01:00
|
|
|
|
2025-02-21 11:27:50 +01:00
|
|
|
long long udata_size() const
|
|
|
|
{ if( member_vector.empty() ) return 0;
|
|
|
|
return member_vector.back().dblock.end(); }
|
2025-02-21 10:16:51 +01:00
|
|
|
|
2025-02-21 11:27:50 +01:00
|
|
|
long long cdata_size() const
|
|
|
|
{ if( member_vector.empty() ) return 0;
|
|
|
|
return member_vector.back().mblock.end(); }
|
2025-02-21 10:16:51 +01:00
|
|
|
|
2025-02-21 11:25:51 +01:00
|
|
|
// total size including trailing data (if any)
|
2025-02-21 11:16:33 +01:00
|
|
|
long long file_size() const
|
2025-02-21 11:29:28 +01:00
|
|
|
{ if( insize >= 0 ) return insize; else return 0; }
|
2025-02-21 11:16:33 +01:00
|
|
|
|
2025-02-21 11:20:21 +01:00
|
|
|
const Block & dblock( const long i ) const
|
2025-02-21 10:16:51 +01:00
|
|
|
{ return member_vector[i].dblock; }
|
2025-02-21 11:20:21 +01:00
|
|
|
const Block & mblock( const long i ) const
|
2025-02-21 10:16:51 +01:00
|
|
|
{ return member_vector[i].mblock; }
|
2025-02-21 11:25:51 +01:00
|
|
|
unsigned dictionary_size( const long i ) const
|
|
|
|
{ return member_vector[i].dictionary_size; }
|
2025-02-21 10:16:51 +01:00
|
|
|
};
|
2025-02-21 11:33:08 +01:00
|
|
|
|
|
|
|
int seek_read( const int fd, uint8_t * const buf, const int size,
|
|
|
|
const long long pos );
|