summaryrefslogtreecommitdiffstats
path: root/archive_reader.cc
blob: 496c33b541968e45c2de6085cbb19acfecc887a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* Tarlz - Archiver with multimember lzip compression
   Copyright (C) 2013-2020 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 2 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/>.
*/

#define _FILE_OFFSET_BITS 64

#include <algorithm>
#include <cerrno>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <stdint.h>
#include <unistd.h>
#include <lzlib.h>

#include "tarlz.h"
#include "lzip_index.h"
#include "archive_reader.h"


int Archive_reader_base::parse_records( Extended & extended,
                                        const Tar_header header,
                                        Resizable_buffer & rbuf,
                                        const bool permissive )
  {
  const long long edsize = parse_octal( header + size_o, size_l );
  const long long bufsize = round_up( edsize );
  if( edsize <= 0 || edsize >= 1LL << 33 || bufsize >= INT_MAX )
    return 1;					// overflow or no extended data
  if( !rbuf.resize( bufsize ) ) return 1;	// extended records buffer
  int retval = read( (uint8_t *)rbuf(), bufsize );
  if( retval == 0 && !extended.parse( rbuf(), edsize, permissive ) )
    retval = 2;
  return retval;
  }


/* Read 'size' uncompressed bytes, decompressing the input if needed.
   Return value: 0 = OK, 1 = damaged member, 2 = fatal error. */
int Archive_reader::read( uint8_t * const buf, const int size )
  {
  if( fatal_ ) return err( 2 );
  if( first_read )					// check format
    {
    first_read = false;
    uncompressed_seekable = ad.seekable && !ad.indexed &&
                            ad.lzip_index.file_size() > 3 * header_size;
    if( size != header_size )
      internal_error( "size != header_size on first call." );
    const int rd = readblock( ad.infd, buf, size );
    if( rd != size && errno )
      return err( 2, "Error reading archive", errno, rd );
    const Lzip_header & header = (*(const Lzip_header *)buf);
    const bool islz = ( rd >= min_member_size && header.verify_magic() &&
                        header.verify_version() &&
                        isvalid_ds( header.dictionary_size() ) );
    const bool istar = ( rd == size && verify_ustar_chksum( buf ) );
    const bool iseof =
      ( !islz && !istar && rd == size && block_is_zero( buf, size ) );
    bool maybe_lz = islz;			// maybe corrupt tar.lz
    if( !islz && !istar && !iseof )		// corrupt or invalid format
      {
      const bool lz_ext = has_lz_ext( ad.name );
      show_file_error( ad.namep, lz_ext ? posix_lz_msg : posix_msg );
      if( lz_ext && rd >= min_member_size ) maybe_lz = true;
      else return err( 1 );
      }
    if( !maybe_lz )					// uncompressed
      { if( rd == size ) return 0;
        return err( 2, "EOF reading archive", 0, rd ); }
    uncompressed_seekable = false;			// compressed
    decoder = LZ_decompress_open();
    if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok )
      return err( 2, mem_msg );
    if( LZ_decompress_write( decoder, buf, rd ) != rd )
      internal_error( "library error (LZ_decompress_write)." );
    const int ret = read( buf, size ); if( ret != 0 ) return ret;
    if( verify_ustar_chksum( buf ) || block_is_zero( buf, size ) ) return 0;
    return err( 2, islz ? posix_lz_msg : "" );
    }

  if( !decoder )					// uncompressed
    {
    const int rd = readblock( ad.infd, buf, size );
    if( rd == size ) return 0; else return err( 2, end_msg, 0, rd );
    }
  const int ibuf_size = 16384;
  uint8_t ibuf[ibuf_size];
  int sz = 0;
  while( sz < size )
    {
    const int rd = LZ_decompress_read( decoder, buf + sz, size - sz );
    if( rd < 0 )
      {
      const unsigned long long old_pos = LZ_decompress_total_in_size( decoder );
      if( LZ_decompress_sync_to_member( decoder ) < 0 )
        internal_error( "library error (LZ_decompress_sync_to_member)." );
      e_skip_ = true; set_error_status( 2 );
      const unsigned long long new_pos = LZ_decompress_total_in_size( decoder );
      // lzlib < 1.8 does not update total_in_size when syncing to member
      if( new_pos >= old_pos && new_pos < LLONG_MAX )
        return err( 1, "", 0, sz, true );
      return err( 2, "Skipping to next header failed. "
                     "Lzlib 1.8 or newer required.", 0, sz );
      }
    if( rd == 0 && LZ_decompress_finished( decoder ) == 1 )
      { return err( 2, end_msg, 0, sz ); }
    sz += rd;
    if( sz < size && !at_eof && LZ_decompress_write_size( decoder ) > 0 )
      {
      const int rsize = std::min( ibuf_size, LZ_decompress_write_size( decoder ) );
      const int rd = readblock( ad.infd, ibuf, rsize );
      if( LZ_decompress_write( decoder, ibuf, rd ) != rd )
        internal_error( "library error (LZ_decompress_write)." );
      if( rd < rsize )
        {
        at_eof = true; LZ_decompress_finish( decoder );
        if( errno ) return err( 2, "Error reading archive.", errno, sz );
        }
      }
    }
  return 0;
  }


int Archive_reader::skip_member( const Extended & extended )
  {
  long long rest = round_up( extended.file_size() );	// size + padding
  if( uncompressed_seekable && lseek( ad.infd, rest, SEEK_CUR ) > 0 )
    return 0;
  const int bufsize = 32 * header_size;
  uint8_t buf[bufsize];
  while( rest > 0 )				// skip tar member
    {
    const int rsize = ( rest >= bufsize ) ? bufsize : rest;
    const int ret = read( buf, rsize );
    if( ret != 0 ) return ret;
    rest -= rsize;
    }
  return 0;
  }


void Archive_reader_i::set_member( const long i )
  {
  LZ_decompress_reset( decoder );		// prepare for new member
  data_pos_ = ad.lzip_index.dblock( i ).pos();
  mdata_end = ad.lzip_index.dblock( i ).end();
  archive_pos = ad.lzip_index.mblock( i ).pos();
  member_id = i;
  }


/* Read 'size' decompressed bytes from the archive.
   Return value: 0 = OK, 1 = damaged member, 2 = fatal error. */
int Archive_reader_i::read( uint8_t * const buf, const int size )
  {
  int sz = 0;

  while( sz < size )
    {
    const int rd = LZ_decompress_read( decoder, buf + sz, size - sz );
    if( rd < 0 )
      return err( 1, LZ_strerror( LZ_decompress_errno( decoder ) ) );
    if( rd == 0 && LZ_decompress_finished( decoder ) == 1 )
      return err( 2, end_msg );
    sz += rd; data_pos_ += rd;
    if( sz < size && LZ_decompress_write_size( decoder ) > 0 )
      {
      const long long ibuf_size = 16384;
      uint8_t ibuf[ibuf_size];
      const long long member_end = ad.lzip_index.mblock( member_id ).end();
      const long long rest = ( ( archive_pos < member_end ) ?
        member_end : ad.lzip_index.cdata_size() ) - archive_pos;
      const int rsize = std::min( LZ_decompress_write_size( decoder ),
                                  (int)std::min( ibuf_size, rest ) );
      if( rsize <= 0 ) LZ_decompress_finish( decoder );
      else
        {
        const int rd = preadblock( ad.infd, ibuf, rsize, archive_pos );
        if( LZ_decompress_write( decoder, ibuf, rd ) != rd )
          internal_error( "library error (LZ_decompress_write)." );
        archive_pos += rd;
        if( rd < rsize )
          {
          LZ_decompress_finish( decoder );
          if( errno ) return err( 2, "Error reading archive" );
          }
        }
      }
    }
  return 0;
  }


int Archive_reader_i::skip_member( const Extended & extended )
  {
  long long rest = round_up( extended.file_size() );	// size + padding
  if( data_pos_ + rest == mdata_end ) { data_pos_ = mdata_end; return 0; }
  const int bufsize = 32 * header_size;
  uint8_t buf[bufsize];
  while( rest > 0 )				// skip tar member
    {
    const int rsize = ( rest >= bufsize ) ? bufsize : rest;
    const int ret = read( buf, rsize );
    if( ret != 0 ) return ret;
    rest -= rsize;
    }
  return 0;
  }