diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-06-16 11:13:24 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-06-16 11:13:24 +0000 |
commit | d7c278fca708bc2c4badaeac041ecb136a4ef955 (patch) | |
tree | 5b163b83e6e3171a311f67187aa75c0f96f98f13 /lzip_index.cc | |
parent | Adding upstream version 1.23. (diff) | |
download | lziprecover-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-- | lzip_index.cc | 92 |
1 files changed, 53 insertions, 39 deletions
diff --git a/lzip_index.cc b/lzip_index.cc index eff4d05..1614dde 100644 --- a/lzip_index.cc +++ b/lzip_index.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 @@ -40,11 +40,12 @@ 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 ignore_bad_ds ) + const bool first, const bool ignore_bad_ds ) { - if( !header.verify_magic() ) - { error_ = bad_magic_msg; retval_ = 2; return true; } - if( !header.verify_version() ) + if( !header.check_magic() ) + { error_ = bad_magic_msg; retval_ = 2; if( first ) bad_magic_ = true; + return true; } + if( !header.check_version() ) { error_ = bad_version( header.version() ); retval_ = 2; return true; } if( !ignore_bad_ds && !isvalid_ds( header.dictionary_size() ) ) { error_ = bad_dict_msg; retval_ = 2; return true; } @@ -67,10 +68,13 @@ 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 long long pos, const bool ignore_marking ) { if( seek_read( fd, header.data, Lzip_header::size, pos ) != Lzip_header::size ) { set_errno_error( "Error reading member header: " ); return false; } + uint8_t byte; + if( !ignore_marking && readblock( fd, &byte, 1 ) == 1 && byte != 0 ) + { error_ = marking_msg; retval_ = 2; return false; } return true; } @@ -88,8 +92,8 @@ bool Lzip_index::read_trailer( const int fd, Lzip_trailer & trailer, 'ignore_gaps' also ignores format errors and a truncated last member. If successful, push member preceding gap and set pos to member header. */ bool Lzip_index::skip_gap( const int fd, unsigned long long & pos, - const bool ignore_trailing, const bool loose_trailing, - const bool ignore_bad_ds, const bool ignore_gaps ) + const Cl_options & cl_opts, + const bool ignore_bad_ds, const bool ignore_gaps ) { if( pos < min_member_size ) { @@ -118,19 +122,20 @@ bool Lzip_index::skip_gap( const int fd, unsigned long long & pos, 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.verify_consistency() ) + if( member_size > ipos + i || !trailer.check_consistency() ) continue; Lzip_header header; - if( !read_header( fd, header, ipos + i - member_size ) ) return false; - if( !header.verify( ignore_bad_ds ) ) continue; + 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; - if( header2.verify_prefix( bsize - i ) ) // next header + 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, ignore_bad_ds ) ) + else if( !check_header_error( header2, false, ignore_bad_ds ) ) error_ = "Last member in input file is truncated or corrupt."; retval_ = 2; return false; } @@ -144,15 +149,18 @@ bool Lzip_index::skip_gap( const int fd, unsigned long long & pos, } if( !ignore_gaps && member_vector.empty() ) { - if( !loose_trailing && full_h2 && header2.verify_corrupt() ) + if( !cl_opts.loose_trailing && full_h2 && header2.check_corrupt() ) { error_ = corrupt_mm_msg; retval_ = 2; return false; } - if( !ignore_trailing ) + 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; } const unsigned dictionary_size = header.dictionary_size(); - member_vector.push_back( Member( 0, trailer.data_size(), pos, - member_size, dictionary_size ) ); + member_vector.push_back( Member( 0, data_size, pos, member_size, + dictionary_size ) ); if( dictionary_size_ < dictionary_size ) dictionary_size_ = dictionary_size; return true; @@ -179,10 +187,11 @@ bool Lzip_index::skip_gap( const int fd, unsigned long long & pos, } -Lzip_index::Lzip_index( const int infd, const bool ignore_trailing, - const bool loose_trailing, 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 ) +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 ) { if( insize < 0 ) { set_errno_error( "Input file is not seekable: " ); return; } @@ -193,8 +202,8 @@ Lzip_index::Lzip_index( const int infd, const bool ignore_trailing, retval_ = 2; return; } Lzip_header header; - if( !read_header( infd, header, 0 ) ) return; - if( check_header_error( header, ignore_bad_ds ) ) return; + if( !read_header( infd, header, 0, cl_opts.ignore_marking ) ) return; + if( check_header_error( header, true, 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; @@ -203,36 +212,40 @@ Lzip_index::Lzip_index( const int infd, const bool ignore_trailing, Lzip_trailer trailer; if( !read_trailer( infd, trailer, pos ) ) break; const unsigned long long member_size = trailer.member_size(); - // if gaps are being ignored, verify consistency of last trailer only. + // if gaps are being ignored, check consistency of last trailer only. if( member_size > pos || member_size < min_member_size || ( ( !ignore_gaps || member_vector.empty() ) && - !trailer.verify_consistency() ) ) // bad trailer + !trailer.check_consistency() ) ) // bad trailer { if( ignore_gaps || member_vector.empty() ) - { if( skip_gap( infd, pos, ignore_trailing, loose_trailing, - ignore_bad_ds, ignore_gaps ) ) continue; else return; } + { 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; } - if( !read_header( infd, header, pos - member_size ) ) break; - if( !header.verify( ignore_bad_ds ) ) // bad header + if( !read_header( infd, header, pos - member_size, cl_opts.ignore_marking ) ) + break; + if( !header.check( ignore_bad_ds ) ) // bad header { if( ignore_gaps || member_vector.empty() ) - { if( skip_gap( infd, pos, ignore_trailing, loose_trailing, - ignore_bad_ds, ignore_gaps ) ) continue; else return; } + { 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; } 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; } const unsigned dictionary_size = header.dictionary_size(); - member_vector.push_back( Member( 0, trailer.data_size(), pos, - member_size, dictionary_size ) ); + member_vector.push_back( Member( 0, data_size, pos, member_size, + dictionary_size ) ); if( dictionary_size_ < dictionary_size ) dictionary_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 ) || - member_vector.empty() ) + member_vector.empty() || retval_ != 0 ) { member_vector.clear(); if( retval_ == 0 ) { error_ = "Can't create file index."; retval_ = 2; } @@ -259,7 +272,8 @@ Lzip_index::Lzip_index( const int infd, const bool ignore_trailing, // 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 ) // DS not used + : insize( fsize ), retval_( 0 ), + dictionary_size_( 0 ), bad_magic_( false ) // DS not used { if( insize < 0 ) { set_errno_error( "Input file is not seekable: " ); return; } @@ -276,7 +290,7 @@ Lzip_index::Lzip_index( const std::vector< int > & infd_vector, { const int infd = infd_vector[i]; if( !read_header( infd, header, 0 ) ) return; - if( header.verify_magic() && header.verify_version() ) done = true; + if( header.check_magic() && header.check_version() ) done = true; } if( !done ) { error_ = bad_magic_msg; retval_ = 2; return; } @@ -292,12 +306,12 @@ Lzip_index::Lzip_index( const std::vector< int > & infd_vector, const int tfd = infd_vector[it]; if( !read_trailer( tfd, trailer, pos ) ) goto error; member_size = trailer.member_size(); - if( member_size <= (unsigned long long)pos && trailer.verify_consistency() ) + if( member_size <= (unsigned long long)pos && trailer.check_consistency() ) for( int ih = 0; ih < files && !done; ++ih ) { const int hfd = infd_vector[ih]; if( !read_header( hfd, header, pos - member_size ) ) goto error; - if( header.verify_magic() && header.verify_version() ) done = true; + if( header.check_magic() && header.check_version() ) done = true; } } if( !done ) @@ -313,7 +327,7 @@ Lzip_index::Lzip_index( const std::vector< int > & infd_vector, { const int infd = infd_vector[i]; if( seek_read( infd, header.data, size, pos ) == size && - header.verify_prefix( size ) ) + header.check_prefix( size ) ) { error_ = "Last member in input file is truncated or corrupt."; retval_ = 2; goto error; |