From 9e1481f1341f12a41b988d6dcb63e8256d1e54a3 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 7 Nov 2015 11:20:52 +0100 Subject: Adding upstream version 1.14~rc1. Signed-off-by: Daniel Baumann --- decoder.cc | 90 ++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 47 insertions(+), 43 deletions(-) (limited to 'decoder.cc') diff --git a/decoder.cc b/decoder.cc index fdb4ff9..5e26504 100644 --- a/decoder.cc +++ b/decoder.cc @@ -1,5 +1,5 @@ /* Lziprecover - Data recovery tool for lzipped files - Copyright (C) 2009, 2010, 2011, 2012 Antonio Diaz Diaz. + Copyright (C) 2009, 2010, 2011, 2012, 2013 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 @@ -34,19 +34,19 @@ const CRC32 crc32; -void Pretty_print::operator()( const char * const msg ) const +void Pretty_print::operator()( const char * const msg, FILE * const f ) const { if( verbosity_ >= 0 ) { if( first_post ) { first_post = false; - std::fprintf( stderr, " %s: ", name_.c_str() ); - for( unsigned int i = 0; i < longest_name - name_.size(); ++i ) - std::fprintf( stderr, " " ); - if( !msg ) std::fflush( stderr ); + std::fprintf( f, " %s: ", name_.c_str() ); + for( unsigned i = 0; i < longest_name - name_.size(); ++i ) + std::fprintf( f, " " ); + if( !msg ) std::fflush( f ); } - if( msg ) std::fprintf( stderr, "%s.\n", msg ); + if( msg ) std::fprintf( f, "%s.\n", msg ); } } @@ -60,13 +60,13 @@ int readblock( const int fd, uint8_t * const buf, const int size ) errno = 0; while( rest > 0 ) { - errno = 0; const int n = read( fd, buf + size - rest, rest ); if( n > 0 ) rest -= n; - else if( n == 0 ) break; + else if( n == 0 ) break; // EOF else if( errno != EINTR && errno != EAGAIN ) break; + errno = 0; } - return ( rest > 0 ) ? size - rest : size; + return size - rest; } @@ -79,12 +79,12 @@ int writeblock( const int fd, const uint8_t * const buf, const int size ) errno = 0; while( rest > 0 ) { - errno = 0; const int n = write( fd, buf + size - rest, rest ); if( n > 0 ) rest -= n; else if( n < 0 && errno != EINTR && errno != EAGAIN ) break; + errno = 0; } - return ( rest > 0 ) ? size - rest : size; + return size - rest; } @@ -110,9 +110,10 @@ void LZ_decoder::flush_data() crc32.update( crc_, buffer + stream_pos, size ); if( outfd >= 0 ) { - const long long i = std::max( 0LL, outskip - stream_position() ); + const unsigned long long sp = stream_position(); + const long long i = positive_diff( outskip, sp ); const long long s = - std::min( outend - stream_position(), (long long)size ) - i; + std::min( positive_diff( outend, sp ), (unsigned long long)size ) - i; if( s > 0 && writeblock( outfd, buffer + stream_pos + i, s ) != s ) throw Error( "Write error" ); } @@ -126,10 +127,11 @@ bool LZ_decoder::verify_trailer( const Pretty_print & pp ) const { File_trailer trailer; const int trailer_size = File_trailer::size( member_version ); - const long long member_size = range_decoder.member_position() + trailer_size; + const unsigned long long member_size = + range_decoder.member_position() + trailer_size; bool error = false; - const int size = range_decoder.read( trailer.data, trailer_size ); + int size = range_decoder.read_data( trailer.data, trailer_size ); if( size < trailer_size ) { error = true; @@ -139,9 +141,11 @@ bool LZ_decoder::verify_trailer( const Pretty_print & pp ) const std::fprintf( stderr, "Trailer truncated at trailer position %d;" " some checks may fail.\n", size ); } - for( int i = size; i < trailer_size; ++i ) trailer.data[i] = 0; + while( size < trailer_size ) trailer.data[size++] = 0; } + if( member_version == 0 ) trailer.member_size( member_size ); + if( !range_decoder.code_is_zero() ) { error = true; @@ -154,7 +158,7 @@ bool LZ_decoder::verify_trailer( const Pretty_print & pp ) const { pp(); std::fprintf( stderr, "CRC mismatch; trailer says %08X, data CRC is %08X.\n", - (unsigned int)trailer.data_crc(), (unsigned int)crc() ); + trailer.data_crc(), crc() ); } } if( trailer.data_size() != data_position() ) @@ -163,7 +167,7 @@ bool LZ_decoder::verify_trailer( const Pretty_print & pp ) const if( pp.verbosity() >= 0 ) { pp(); - std::fprintf( stderr, "Data size mismatch; trailer says %lld, data size is %lld (0x%llX).\n", + std::fprintf( stderr, "Data size mismatch; trailer says %llu, data size is %llu (0x%llX).\n", trailer.data_size(), data_position(), data_position() ); } } @@ -173,7 +177,7 @@ bool LZ_decoder::verify_trailer( const Pretty_print & pp ) const if( pp.verbosity() >= 0 ) { pp(); - std::fprintf( stderr, "Member size mismatch; trailer says %lld, member size is %lld (0x%llX).\n", + std::fprintf( stderr, "Member size mismatch; trailer says %llu, member size is %llu (0x%llX).\n", trailer.member_size(), member_size, member_size ); } } @@ -183,9 +187,8 @@ bool LZ_decoder::verify_trailer( const Pretty_print & pp ) const ( 8.0 * member_size ) / data_position(), 100.0 * ( 1.0 - ( (double)member_size / data_position() ) ) ); if( !error && pp.verbosity() >= 4 ) - std::fprintf( stderr, "data CRC %08X, data size %9lld, member size %8lld. ", - (unsigned int)trailer.data_crc(), trailer.data_size(), - trailer.member_size() ); + std::fprintf( stderr, "data CRC %08X, data size %9llu, member size %8llu. ", + trailer.data_crc(), trailer.data_size(), trailer.member_size() ); return !error; } @@ -194,6 +197,7 @@ bool LZ_decoder::verify_trailer( const Pretty_print & pp ) const // 3 = trailer error, 4 = unknown marker found. int LZ_decoder::decode_member( const Pretty_print & pp ) { + Bit_model bm_literal[1<> 1 ) - 1; rep0 = ( 2 | ( dis_slot & 1 ) ) << direct_bits; if( dis_slot < end_dis_model ) - rep0 += range_decoder.decode_tree_reversed( bm_dis + rep0 - dis_slot, direct_bits ); + rep0 += range_decoder.decode_tree_reversed( bm_dis + rep0 - dis_slot - 1, direct_bits ); else { rep0 += range_decoder.decode( direct_bits - dis_align_bits ) << dis_align_bits; - rep0 += range_decoder.decode_tree_reversed( bm_align, dis_align_bits ); + rep0 += range_decoder.decode_tree_reversed4( bm_align ); if( rep0 == 0xFFFFFFFFU ) // Marker found { rep0 = rep0_saved; @@ -301,11 +303,13 @@ int LZ_decoder::decode_member( const Pretty_print & pp ) } rep3 = rep2; rep2 = rep1; rep1 = rep0_saved; state.set_match(); - if( rep0 >= (unsigned int)dictionary_size || - ( rep0 >= (unsigned int)pos && !partial_data_pos ) ) + if( rep0 >= (unsigned)dictionary_size || + ( rep0 >= (unsigned)pos && !partial_data_pos ) ) { flush_data(); return 1; } } copy_block( rep0, len ); } } + flush_data(); + return 2; } -- cgit v1.2.3