diff options
Diffstat (limited to '')
-rw-r--r-- | archive_reader.cc | 101 |
1 files changed, 55 insertions, 46 deletions
diff --git a/archive_reader.cc b/archive_reader.cc index a146156..8ad315d 100644 --- a/archive_reader.cc +++ b/archive_reader.cc @@ -30,6 +30,8 @@ namespace { +const char * const rdaerr_msg = "Error reading archive"; + /* Return the number of bytes really read. If (value returned < size) and (errno == 0), means EOF was reached. */ @@ -49,51 +51,61 @@ int preadblock( const int fd, uint8_t * const buf, const int size, return sz; } +int non_tty_infd( const std::string & archive_name, const char * const namep ) + { + int infd = archive_name.empty() ? STDIN_FILENO : open_instream( archive_name ); + if( infd >= 0 && isatty( infd ) ) // for example /dev/tty + { show_file_error( namep, archive_name.empty() ? + "I won't read archive data from a terminal (missing -f option?)" : + "I won't read archive data from a terminal." ); + close( infd ); infd = -1; } + return infd; + } + -/* Return the number of bytes really written. - If (value returned < size), it is always an error. -*//* -int pwriteblock( const int fd, const uint8_t * const buf, const int size, - const long long pos ) +void xLZ_decompress_write( LZ_Decoder * const decoder, + const uint8_t * const buffer, const int size ) { - int sz = 0; - errno = 0; - while( sz < size ) - { - const int n = pwrite( fd, buf + sz, size - sz, pos + sz ); - if( n > 0 ) sz += n; - else if( n < 0 && errno != EINTR ) break; - errno = 0; - } - return sz; + if( LZ_decompress_write( decoder, buffer, size ) != size ) + internal_error( "library error (LZ_decompress_write)." ); } -*/ } // end namespace +Archive_descriptor::Archive_descriptor( const std::string & archive_name ) + : name( archive_name ), namep( name.empty() ? "(stdin)" : name.c_str() ), + infd( non_tty_infd( archive_name, namep ) ), + lzip_index( infd, true, false ), + seekable( lseek( infd, 0, SEEK_SET ) == 0 ), + indexed( seekable && lzip_index.retval() == 0 ) {} + + int Archive_reader_base::parse_records( Extended & extended, const Tar_header header, Resizable_buffer & rbuf, + const char * const default_msg, 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( rbuf.u8(), bufsize ); + if( edsize <= 0 ) return err( 2, misrec_msg ); // no extended records + if( edsize >= 1LL << 33 || bufsize >= INT_MAX ) + return err( -2, longrec_msg ); // records too long + if( !rbuf.resize( bufsize ) ) return err( -1, mem_msg ); + e_msg_ = ""; e_code_ = 0; + int retval = read( rbuf.u8(), bufsize ); // extended records buffer if( retval == 0 && !extended.parse( rbuf(), edsize, permissive ) ) retval = 2; + if( retval && !*e_msg_ ) e_msg_ = default_msg; return retval; } /* Read 'size' uncompressed bytes, decompressing the input if needed. - Return value: 0 = OK, 1 = damaged member, 2 = fatal error. */ + Return value: 0 = OK, 1 = OOM or read error, 2 = EOF or invalid data. */ int Archive_reader::read( uint8_t * const buf, const int size ) { - if( fatal_ ) return err( 2 ); if( first_read ) // check format { first_read = false; @@ -102,32 +114,30 @@ int Archive_reader::read( uint8_t * const buf, const int 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 ); + if( rd != size && errno ) return err( -1, rdaerr_msg, 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 = + const bool iseoa = ( !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 + if( !islz && !istar && !iseoa ) // 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 ); + else return err( 2 ); } if( !maybe_lz ) // uncompressed { if( rd == size ) return 0; - return err( 2, "EOF reading archive", 0, rd ); } + 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)." ); + { LZ_decompress_close( decoder ); decoder = 0; return err( -1, mem_msg ); } + xLZ_decompress_write( decoder, buf, rd ); 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 : "" ); @@ -136,7 +146,7 @@ int Archive_reader::read( uint8_t * const buf, const int size ) if( !decoder ) // uncompressed { const int rd = readblock( ad.infd, buf, size ); - if( rd == size ) return 0; else return err( 2, end_msg, 0, rd ); + if( rd == size ) return 0; else return err( -2, end_msg, 0, rd ); } const int ibuf_size = 16384; uint8_t ibuf[ibuf_size]; @@ -153,23 +163,22 @@ int Archive_reader::read( uint8_t * const buf, const int size ) 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. " + return err( 2, "", 0, sz, true ); + return err( -1, "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 ); } + { 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)." ); + xLZ_decompress_write( decoder, ibuf, rd ); if( rd < rsize ) { at_eof = true; LZ_decompress_finish( decoder ); - if( errno ) return err( 2, "Error reading archive.", errno, sz ); + if( errno ) return err( -1, rdaerr_msg, errno, sz ); } } } @@ -179,9 +188,9 @@ int Archive_reader::read( uint8_t * const buf, const int size ) int Archive_reader::skip_member( const Extended & extended ) { + if( extended.file_size() <= 0 ) return 0; long long rest = round_up( extended.file_size() ); // size + padding - if( uncompressed_seekable && lseek( ad.infd, rest, SEEK_CUR ) > 0 ) - return 0; + 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 @@ -206,7 +215,7 @@ void Archive_reader_i::set_member( const long i ) /* Read 'size' decompressed bytes from the archive. - Return value: 0 = OK, 1 = damaged member, 2 = fatal error. */ + Return value: 0 = OK, 1 = OOM or read error, 2 = EOF or invalid data. */ int Archive_reader_i::read( uint8_t * const buf, const int size ) { int sz = 0; @@ -215,9 +224,9 @@ int Archive_reader_i::read( uint8_t * const buf, const int size ) { const int rd = LZ_decompress_read( decoder, buf + sz, size - sz ); if( rd < 0 ) - return err( 1, LZ_strerror( LZ_decompress_errno( decoder ) ), 0, sz ); + return err( 2, LZ_strerror( LZ_decompress_errno( decoder ) ), 0, sz ); if( rd == 0 && LZ_decompress_finished( decoder ) == 1 ) - return err( 2, end_msg, 0, sz ); + return err( -2, end_msg, 0, sz ); sz += rd; data_pos_ += rd; if( sz < size && LZ_decompress_write_size( decoder ) > 0 ) { @@ -232,13 +241,12 @@ int Archive_reader_i::read( uint8_t * const buf, const int size ) 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)." ); + xLZ_decompress_write( decoder, ibuf, rd ); archive_pos += rd; if( rd < rsize ) { LZ_decompress_finish( decoder ); - if( errno ) return err( 2, "Error reading archive", 0, sz ); + if( errno ) return err( -1, rdaerr_msg, errno, sz ); } } } @@ -249,6 +257,7 @@ int Archive_reader_i::read( uint8_t * const buf, const int size ) int Archive_reader_i::skip_member( const Extended & extended ) { + if( extended.file_size() <= 0 ) return 0; 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; |