summaryrefslogtreecommitdiffstats
path: root/lzip_index.cc
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lzip_index.cc64
1 files changed, 29 insertions, 35 deletions
diff --git a/lzip_index.cc b/lzip_index.cc
index 1614dde..459338e 100644
--- a/lzip_index.cc
+++ b/lzip_index.cc
@@ -1,5 +1,5 @@
/* Lziprecover - Data recovery tool for the lzip format
- Copyright (C) 2009-2023 Antonio Diaz Diaz.
+ Copyright (C) 2009-2024 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
@@ -39,17 +39,16 @@ int seek_read( const int fd, uint8_t * const buf, const int size,
}
-bool Lzip_index::check_header_error( const Lzip_header & header,
- const bool first, const bool ignore_bad_ds )
+bool Lzip_index::check_header( const Lzip_header & header,
+ const bool ignore_bad_ds )
{
if( !header.check_magic() )
- { error_ = bad_magic_msg; retval_ = 2; if( first ) bad_magic_ = true;
- return true; }
+ { error_ = bad_magic_msg; retval_ = 2; return false; }
if( !header.check_version() )
- { error_ = bad_version( header.version() ); retval_ = 2; return true; }
+ { error_ = bad_version( header.version() ); retval_ = 2; return false; }
if( !ignore_bad_ds && !isvalid_ds( header.dictionary_size() ) )
- { error_ = bad_dict_msg; retval_ = 2; return true; }
- return false;
+ { error_ = bad_dict_msg; retval_ = 2; return false; }
+ return true;
}
void Lzip_index::set_errno_error( const char * const msg )
@@ -70,7 +69,7 @@ void Lzip_index::set_num_error( const char * const msg, unsigned long long num )
bool Lzip_index::read_header( const int fd, Lzip_header & header,
const long long pos, const bool ignore_marking )
{
- if( seek_read( fd, header.data, Lzip_header::size, pos ) != Lzip_header::size )
+ if( seek_read( fd, header.data, header.size, pos ) != header.size )
{ set_errno_error( "Error reading member header: " ); return false; }
uint8_t byte;
if( !ignore_marking && readblock( fd, &byte, 1 ) == 1 && byte != 0 )
@@ -81,8 +80,8 @@ bool Lzip_index::read_header( const int fd, Lzip_header & header,
bool Lzip_index::read_trailer( const int fd, Lzip_trailer & trailer,
const long long pos )
{
- if( seek_read( fd, trailer.data, Lzip_trailer::size,
- pos - Lzip_trailer::size ) != Lzip_trailer::size )
+ if( seek_read( fd, trailer.data, trailer.size, pos - trailer.size ) !=
+ trailer.size )
{ set_errno_error( "Error reading member trailer: " ); return false; }
return true;
}
@@ -118,24 +117,23 @@ bool Lzip_index::skip_gap( const int fd, unsigned long long & pos,
if( buffer[i-1] <= max_msb ) // most significant byte of member_size
{
const Lzip_trailer & trailer =
- *(const Lzip_trailer *)( buffer + i - Lzip_trailer::size );
+ *(const Lzip_trailer *)( buffer + i - trailer.size );
const unsigned long long member_size = trailer.member_size();
if( member_size == 0 ) // skip trailing zeros
- { while( i > Lzip_trailer::size && buffer[i-9] == 0 ) --i; continue; }
- if( member_size > ipos + i || !trailer.check_consistency() )
- continue;
+ { while( i > trailer.size && buffer[i-9] == 0 ) --i; continue; }
+ if( member_size > ipos + i || !trailer.check_consistency() ) continue;
Lzip_header header;
if( !read_header( fd, header, ipos + i - member_size,
cl_opts.ignore_marking ) ) return false;
if( !header.check( ignore_bad_ds ) ) continue;
const Lzip_header & header2 = *(const Lzip_header *)( buffer + i );
- const bool full_h2 = bsize - i >= Lzip_header::size;
+ const bool full_h2 = bsize - i >= header.size;
if( header2.check_prefix( bsize - i ) ) // next header
{
if( !ignore_gaps && member_vector.empty() ) // last member
{
if( !full_h2 ) error_ = "Last member in input file is truncated.";
- else if( !check_header_error( header2, false, ignore_bad_ds ) )
+ else if( check_header( header2, ignore_bad_ds ) )
error_ = "Last member in input file is truncated or corrupt.";
retval_ = 2; return false;
}
@@ -154,15 +152,15 @@ bool Lzip_index::skip_gap( const int fd, unsigned long long & pos,
if( !cl_opts.ignore_trailing )
{ error_ = trailing_msg; retval_ = 2; return false; }
}
- pos = ipos + i - member_size;
const unsigned long long data_size = trailer.data_size();
if( !cl_opts.ignore_empty && data_size == 0 )
{ error_ = empty_msg; retval_ = 2; return false; }
+ pos = ipos + i - member_size; // good member
const unsigned dictionary_size = header.dictionary_size();
- member_vector.push_back( Member( 0, data_size, pos, member_size,
- dictionary_size ) );
if( dictionary_size_ < dictionary_size )
dictionary_size_ = dictionary_size;
+ member_vector.push_back( Member( 0, data_size, pos, member_size,
+ dictionary_size ) );
return true;
}
if( ipos == 0 )
@@ -190,8 +188,7 @@ bool Lzip_index::skip_gap( const int fd, unsigned long long & pos,
Lzip_index::Lzip_index( const int infd, const Cl_options & cl_opts,
const bool ignore_bad_ds, const bool ignore_gaps,
const long long max_pos )
- : insize( lseek( infd, 0, SEEK_END ) ), retval_( 0 ),
- dictionary_size_( 0 ), bad_magic_( false )
+ : insize( lseek( infd, 0, SEEK_END ) ), retval_( 0 ), dictionary_size_( 0 )
{
if( insize < 0 )
{ set_errno_error( "Input file is not seekable: " ); return; }
@@ -202,8 +199,8 @@ Lzip_index::Lzip_index( const int infd, const Cl_options & cl_opts,
retval_ = 2; return; }
Lzip_header header;
- if( !read_header( infd, header, 0, cl_opts.ignore_marking ) ) return;
- if( check_header_error( header, true, ignore_bad_ds ) ) return;
+ if( !read_header( infd, header, 0, cl_opts.ignore_marking ) ||
+ !check_header( header, ignore_bad_ds ) ) return;
// pos always points to a header or to ( EOF || max_pos )
unsigned long long pos = ( max_pos > 0 ) ? max_pos : insize;
@@ -220,8 +217,7 @@ Lzip_index::Lzip_index( const int infd, const Cl_options & cl_opts,
if( ignore_gaps || member_vector.empty() )
{ if( skip_gap( infd, pos, cl_opts, ignore_bad_ds, ignore_gaps ) )
continue; else return; }
- set_num_error( "Bad trailer at pos ", pos - Lzip_trailer::size );
- break;
+ set_num_error( "Bad trailer at pos ", pos - trailer.size ); break;
}
if( !read_header( infd, header, pos - member_size, cl_opts.ignore_marking ) )
break;
@@ -230,18 +226,17 @@ Lzip_index::Lzip_index( const int infd, const Cl_options & cl_opts,
if( ignore_gaps || member_vector.empty() )
{ if( skip_gap( infd, pos, cl_opts, ignore_bad_ds, ignore_gaps ) )
continue; else return; }
- set_num_error( "Bad header at pos ", pos - member_size );
- break;
+ set_num_error( "Bad header at pos ", pos - member_size ); break;
}
- pos -= member_size;
const unsigned long long data_size = trailer.data_size();
if( !cl_opts.ignore_empty && data_size == 0 )
{ error_ = empty_msg; retval_ = 2; break; }
+ pos -= member_size; // good member
const unsigned dictionary_size = header.dictionary_size();
- member_vector.push_back( Member( 0, data_size, pos, member_size,
- dictionary_size ) );
if( dictionary_size_ < dictionary_size )
dictionary_size_ = dictionary_size;
+ member_vector.push_back( Member( 0, data_size, pos, member_size,
+ dictionary_size ) );
}
// block at pos == 0 must be a member unless shorter than min_member_size
if( pos >= min_member_size || ( pos != 0 && !ignore_gaps ) ||
@@ -272,8 +267,7 @@ Lzip_index::Lzip_index( const int infd, const Cl_options & cl_opts,
// All files in 'infd_vector' must be at least 'fsize' bytes long.
Lzip_index::Lzip_index( const std::vector< int > & infd_vector,
const long long fsize )
- : insize( fsize ), retval_( 0 ),
- dictionary_size_( 0 ), bad_magic_( false ) // DS not used
+ : insize( fsize ), retval_( 0 ), dictionary_size_( 0 ) // DS not used
{
if( insize < 0 )
{ set_errno_error( "Input file is not seekable: " ); return; }
@@ -322,7 +316,7 @@ Lzip_index::Lzip_index( const std::vector< int > & infd_vector,
}
if( member_vector.empty() && insize > pos )
{
- const int size = std::min( (long long)Lzip_header::size, insize - pos );
+ const int size = std::min( (long long)header.size, insize - pos );
for( int i = 0; i < files; ++i )
{
const int infd = infd_vector[i];
@@ -339,7 +333,7 @@ Lzip_index::Lzip_index( const std::vector< int > & infd_vector,
member_size, 0 ) );
}
error:
- if( pos != 0 || member_vector.empty() )
+ if( pos != 0 || member_vector.empty() || retval_ != 0 )
{
member_vector.clear();
if( retval_ == 0 ) { error_ = "Can't create file index."; retval_ = 2; }