"Fossies" - the Fresh Open Source Software Archive

Member "lzip-1.22-rc2/lzip_index.cc" (30 Apr 2020, 7922 Bytes) of package /linux/misc/lzip-1.22-rc2.tar.lz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "lzip_index.cc" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.21_vs_1.22-rc1.

    1 /* Lzip - LZMA lossless data compressor
    2    Copyright (C) 2008-2020 Antonio Diaz Diaz.
    3 
    4    This program is free software: you can redistribute it and/or modify
    5    it under the terms of the GNU General Public License as published by
    6    the Free Software Foundation, either version 2 of the License, or
    7    (at your option) any later version.
    8 
    9    This program is distributed in the hope that it will be useful,
   10    but WITHOUT ANY WARRANTY; without even the implied warranty of
   11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12    GNU General Public License for more details.
   13 
   14    You should have received a copy of the GNU General Public License
   15    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   16 */
   17 
   18 #define _FILE_OFFSET_BITS 64
   19 
   20 #include <algorithm>
   21 #include <cerrno>
   22 #include <cstdio>
   23 #include <cstring>
   24 #include <string>
   25 #include <vector>
   26 #include <stdint.h>
   27 #include <unistd.h>
   28 
   29 #include "lzip.h"
   30 #include "lzip_index.h"
   31 
   32 
   33 namespace {
   34 
   35 int seek_read( const int fd, uint8_t * const buf, const int size,
   36                const long long pos )
   37   {
   38   if( lseek( fd, pos, SEEK_SET ) == pos )
   39     return readblock( fd, buf, size );
   40   return 0;
   41   }
   42 
   43 } // end namespace
   44 
   45 
   46 bool Lzip_index::check_header_error( const Lzip_header & header )
   47   {
   48   if( !header.verify_magic() )
   49     { error_ = bad_magic_msg; retval_ = 2; return true; }
   50   if( !header.verify_version() )
   51     { error_ = bad_version( header.version() ); retval_ = 2; return true; }
   52   if( !isvalid_ds( header.dictionary_size() ) )
   53     { error_ = bad_dict_msg; retval_ = 2; return true; }
   54   return false;
   55   }
   56 
   57 void Lzip_index::set_errno_error( const char * const msg )
   58   {
   59   error_ = msg; error_ += std::strerror( errno );
   60   retval_ = 1;
   61   }
   62 
   63 void Lzip_index::set_num_error( const char * const msg, unsigned long long num )
   64   {
   65   char buf[80];
   66   snprintf( buf, sizeof buf, "%s%llu", msg, num );
   67   error_ = buf;
   68   retval_ = 2;
   69   }
   70 
   71 
   72 bool Lzip_index::read_header( const int fd, Lzip_header & header,
   73                               const long long pos )
   74   {
   75   if( seek_read( fd, header.data, Lzip_header::size, pos ) != Lzip_header::size )
   76     { set_errno_error( "Error reading member header: " ); return false; }
   77   return true;
   78   }
   79 
   80 
   81 // If successful, push last member and set pos to member header.
   82 bool Lzip_index::skip_trailing_data( const int fd, unsigned long long & pos,
   83                                      const bool ignore_trailing,
   84                                      const bool loose_trailing )
   85   {
   86   if( pos < min_member_size ) return false;
   87   enum { block_size = 16384,
   88          buffer_size = block_size + Lzip_trailer::size - 1 + Lzip_header::size };
   89   uint8_t buffer[buffer_size];
   90   int bsize = pos % block_size;         // total bytes in buffer
   91   if( bsize <= buffer_size - block_size ) bsize += block_size;
   92   int search_size = bsize;          // bytes to search for trailer
   93   int rd_size = bsize;              // bytes to read from file
   94   unsigned long long ipos = pos - rd_size;  // aligned to block_size
   95 
   96   while( true )
   97     {
   98     if( seek_read( fd, buffer, rd_size, ipos ) != rd_size )
   99       { set_errno_error( "Error seeking member trailer: " ); return false; }
  100     const uint8_t max_msb = ( ipos + search_size ) >> 56;
  101     for( int i = search_size; i >= Lzip_trailer::size; --i )
  102       if( buffer[i-1] <= max_msb )  // most significant byte of member_size
  103         {
  104         const Lzip_trailer & trailer =
  105           *(const Lzip_trailer *)( buffer + i - Lzip_trailer::size );
  106         const unsigned long long member_size = trailer.member_size();
  107         if( member_size == 0 )          // skip trailing zeros
  108           { while( i > Lzip_trailer::size && buffer[i-9] == 0 ) --i; continue; }
  109         if( member_size > ipos + i || !trailer.verify_consistency() )
  110           continue;
  111         Lzip_header header;
  112         if( !read_header( fd, header, ipos + i - member_size ) ) return false;
  113         if( !header.verify() ) continue;
  114         const Lzip_header & header2 = *(const Lzip_header *)( buffer + i );
  115         const bool full_h2 = bsize - i >= Lzip_header::size;
  116         if( header2.verify_prefix( bsize - i ) )    // last member
  117           {
  118           if( !full_h2 ) error_ = "Last member in input file is truncated.";
  119           else if( !check_header_error( header2 ) )
  120             error_ = "Last member in input file is truncated or corrupt.";
  121           retval_ = 2; return false;
  122           }
  123         if( !loose_trailing && full_h2 && header2.verify_corrupt() )
  124           { error_ = corrupt_mm_msg; retval_ = 2; return false; }
  125         if( !ignore_trailing )
  126           { error_ = trailing_msg; retval_ = 2; return false; }
  127         pos = ipos + i - member_size;
  128         const unsigned dictionary_size = header.dictionary_size();
  129         member_vector.push_back( Member( 0, trailer.data_size(), pos,
  130                                          member_size, dictionary_size ) );
  131         if( dictionary_size_ < dictionary_size )
  132           dictionary_size_ = dictionary_size;
  133         return true;
  134         }
  135     if( ipos == 0 )
  136       { set_num_error( "Bad trailer at pos ", pos - Lzip_trailer::size );
  137         return false; }
  138     bsize = buffer_size;
  139     search_size = bsize - Lzip_header::size;
  140     rd_size = block_size;
  141     ipos -= rd_size;
  142     std::memcpy( buffer + rd_size, buffer, buffer_size - rd_size );
  143     }
  144   }
  145 
  146 
  147 Lzip_index::Lzip_index( const int infd, const bool ignore_trailing,
  148                         const bool loose_trailing )
  149   : insize( lseek( infd, 0, SEEK_END ) ), retval_( 0 ), dictionary_size_( 0 )
  150   {
  151   if( insize < 0 )
  152     { set_errno_error( "Input file is not seekable: " ); return; }
  153   if( insize < min_member_size )
  154     { error_ = "Input file is too short."; retval_ = 2; return; }
  155   if( insize > INT64_MAX )
  156     { error_ = "Input file is too long (2^63 bytes or more).";
  157       retval_ = 2; return; }
  158 
  159   Lzip_header header;
  160   if( !read_header( infd, header, 0 ) ) return;
  161   if( check_header_error( header ) ) return;
  162 
  163   unsigned long long pos = insize;  // always points to a header or to EOF
  164   while( pos >= min_member_size )
  165     {
  166     Lzip_trailer trailer;
  167     if( seek_read( infd, trailer.data, Lzip_trailer::size,
  168                    pos - Lzip_trailer::size ) != Lzip_trailer::size )
  169       { set_errno_error( "Error reading member trailer: " ); break; }
  170     const unsigned long long member_size = trailer.member_size();
  171     if( member_size > pos || !trailer.verify_consistency() )    // bad trailer
  172       {
  173       if( member_vector.empty() )
  174         { if( skip_trailing_data( infd, pos, ignore_trailing, loose_trailing ) )
  175             continue; else return; }
  176       set_num_error( "Bad trailer at pos ", pos - Lzip_trailer::size );
  177       break;
  178       }
  179     if( !read_header( infd, header, pos - member_size ) ) break;
  180     if( !header.verify() )              // bad header
  181       {
  182       if( member_vector.empty() )
  183         { if( skip_trailing_data( infd, pos, ignore_trailing, loose_trailing ) )
  184             continue; else return; }
  185       set_num_error( "Bad header at pos ", pos - member_size );
  186       break;
  187       }
  188     pos -= member_size;
  189     const unsigned dictionary_size = header.dictionary_size();
  190     member_vector.push_back( Member( 0, trailer.data_size(), pos,
  191                                      member_size, dictionary_size ) );
  192     if( dictionary_size_ < dictionary_size )
  193       dictionary_size_ = dictionary_size;
  194     }
  195   if( pos != 0 || member_vector.empty() )
  196     {
  197     member_vector.clear();
  198     if( retval_ == 0 ) { error_ = "Can't create file index."; retval_ = 2; }
  199     return;
  200     }
  201   std::reverse( member_vector.begin(), member_vector.end() );
  202   for( unsigned long i = 0; ; ++i )
  203     {
  204     const long long end = member_vector[i].dblock.end();
  205     if( end < 0 || end > INT64_MAX )
  206       {
  207       member_vector.clear();
  208       error_ = "Data in input file is too long (2^63 bytes or more).";
  209       retval_ = 2; return;
  210       }
  211     if( i + 1 >= member_vector.size() ) break;
  212     member_vector[i+1].dblock.pos( end );
  213     }
  214   }