2025-02-21 10:09:52 +01:00
|
|
|
/* Lziprecover - Data recovery tool for lzipped files
|
2025-02-21 11:11:34 +01:00
|
|
|
Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz.
|
2025-02-21 10:09:52 +01:00
|
|
|
|
|
|
|
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 3 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Range_decoder
|
|
|
|
{
|
|
|
|
enum { buffer_size = 16384 };
|
2025-02-21 11:11:34 +01:00
|
|
|
unsigned long long partial_member_pos;
|
2025-02-21 10:09:52 +01:00
|
|
|
uint8_t * const buffer; // input buffer
|
|
|
|
int pos; // current pos in buffer
|
|
|
|
int stream_pos; // when reached, a new block must be read
|
|
|
|
uint32_t code;
|
|
|
|
uint32_t range;
|
|
|
|
const int infd; // input file descriptor
|
|
|
|
bool at_stream_end;
|
|
|
|
|
|
|
|
bool read_block();
|
|
|
|
|
2025-02-21 10:13:18 +01:00
|
|
|
Range_decoder( const Range_decoder & ); // declared as private
|
|
|
|
void operator=( const Range_decoder & ); // declared as private
|
|
|
|
|
2025-02-21 10:09:52 +01:00
|
|
|
public:
|
2025-02-21 10:13:18 +01:00
|
|
|
explicit Range_decoder( const int ifd )
|
2025-02-21 10:09:52 +01:00
|
|
|
:
|
|
|
|
partial_member_pos( 0 ),
|
|
|
|
buffer( new uint8_t[buffer_size] ),
|
|
|
|
pos( 0 ),
|
|
|
|
stream_pos( 0 ),
|
|
|
|
code( 0 ),
|
|
|
|
range( 0xFFFFFFFFU ),
|
|
|
|
infd( ifd ),
|
2025-02-21 11:11:34 +01:00
|
|
|
at_stream_end( false )
|
|
|
|
{}
|
2025-02-21 10:09:52 +01:00
|
|
|
|
|
|
|
~Range_decoder() { delete[] buffer; }
|
|
|
|
|
2025-02-21 10:13:18 +01:00
|
|
|
bool code_is_zero() const { return ( code == 0 ); }
|
2025-02-21 10:09:52 +01:00
|
|
|
bool finished() { return pos >= stream_pos && !read_block(); }
|
2025-02-21 11:11:34 +01:00
|
|
|
unsigned long long member_position() const { return partial_member_pos + pos; }
|
2025-02-21 10:13:18 +01:00
|
|
|
void reset_member_position() { partial_member_pos = -pos; }
|
2025-02-21 10:09:52 +01:00
|
|
|
|
|
|
|
uint8_t get_byte()
|
|
|
|
{
|
2025-02-21 11:11:34 +01:00
|
|
|
if( finished() ) return 0xAA; // make code != 0
|
2025-02-21 10:09:52 +01:00
|
|
|
return buffer[pos++];
|
|
|
|
}
|
|
|
|
|
2025-02-21 11:11:34 +01:00
|
|
|
int read_data( uint8_t * const outbuf, const int size )
|
2025-02-21 10:13:18 +01:00
|
|
|
{
|
|
|
|
int rest = size;
|
|
|
|
while( rest > 0 && !finished() )
|
|
|
|
{
|
|
|
|
const int rd = std::min( rest, stream_pos - pos );
|
|
|
|
std::memcpy( outbuf + size - rest, buffer + pos, rd );
|
|
|
|
pos += rd;
|
|
|
|
rest -= rd;
|
|
|
|
}
|
2025-02-21 11:11:34 +01:00
|
|
|
return size - rest;
|
2025-02-21 10:13:18 +01:00
|
|
|
}
|
|
|
|
|
2025-02-21 10:09:52 +01:00
|
|
|
void load()
|
|
|
|
{
|
|
|
|
code = 0;
|
|
|
|
for( int i = 0; i < 5; ++i ) code = (code << 8) | get_byte();
|
2025-02-21 11:11:34 +01:00
|
|
|
range = 0xFFFFFFFFU;
|
2025-02-21 10:09:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void normalize()
|
|
|
|
{
|
|
|
|
if( range <= 0x00FFFFFFU )
|
|
|
|
{ range <<= 8; code = (code << 8) | get_byte(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
int decode( const int num_bits )
|
|
|
|
{
|
|
|
|
int symbol = 0;
|
|
|
|
for( int i = num_bits; i > 0; --i )
|
|
|
|
{
|
2025-02-21 11:11:34 +01:00
|
|
|
normalize();
|
|
|
|
range >>= 1;
|
|
|
|
// symbol <<= 1;
|
|
|
|
// if( code >= range ) { code -= range; symbol |= 1; }
|
|
|
|
const uint32_t mask = 0U - (code < range);
|
|
|
|
code -= range;
|
|
|
|
code += range & mask;
|
|
|
|
symbol = (symbol << 1) + (mask + 1);
|
2025-02-21 10:09:52 +01:00
|
|
|
}
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
int decode_bit( Bit_model & bm )
|
|
|
|
{
|
|
|
|
normalize();
|
|
|
|
const uint32_t bound = ( range >> bit_model_total_bits ) * bm.probability;
|
|
|
|
if( code < bound )
|
|
|
|
{
|
|
|
|
range = bound;
|
|
|
|
bm.probability += (bit_model_total - bm.probability) >> bit_model_move_bits;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
range -= bound;
|
|
|
|
code -= bound;
|
|
|
|
bm.probability -= bm.probability >> bit_model_move_bits;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int decode_tree( Bit_model bm[], const int num_bits )
|
|
|
|
{
|
2025-02-21 11:12:17 +01:00
|
|
|
int symbol = 1;
|
2025-02-21 10:09:52 +01:00
|
|
|
for( int i = num_bits; i > 0; --i )
|
2025-02-21 11:12:17 +01:00
|
|
|
symbol = ( symbol << 1 ) | decode_bit( bm[symbol] );
|
|
|
|
return symbol - (1 << num_bits);
|
2025-02-21 10:09:52 +01:00
|
|
|
}
|
|
|
|
|
2025-02-21 11:11:34 +01:00
|
|
|
int decode_tree6( Bit_model bm[] )
|
|
|
|
{
|
2025-02-21 11:12:17 +01:00
|
|
|
int symbol = 1;
|
|
|
|
symbol = ( symbol << 1 ) | decode_bit( bm[symbol] );
|
|
|
|
symbol = ( symbol << 1 ) | decode_bit( bm[symbol] );
|
|
|
|
symbol = ( symbol << 1 ) | decode_bit( bm[symbol] );
|
|
|
|
symbol = ( symbol << 1 ) | decode_bit( bm[symbol] );
|
|
|
|
symbol = ( symbol << 1 ) | decode_bit( bm[symbol] );
|
|
|
|
symbol = ( symbol << 1 ) | decode_bit( bm[symbol] );
|
|
|
|
return symbol - (1 << 6);
|
2025-02-21 11:11:34 +01:00
|
|
|
}
|
|
|
|
|
2025-02-21 10:09:52 +01:00
|
|
|
int decode_tree_reversed( Bit_model bm[], const int num_bits )
|
|
|
|
{
|
|
|
|
int model = 1;
|
|
|
|
int symbol = 0;
|
|
|
|
for( int i = 0; i < num_bits; ++i )
|
|
|
|
{
|
2025-02-21 11:11:34 +01:00
|
|
|
const bool bit = decode_bit( bm[model] );
|
2025-02-21 10:09:52 +01:00
|
|
|
model <<= 1;
|
2025-02-21 11:11:34 +01:00
|
|
|
if( bit ) { ++model; symbol |= (1 << i); }
|
2025-02-21 10:09:52 +01:00
|
|
|
}
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
2025-02-21 11:11:34 +01:00
|
|
|
int decode_tree_reversed4( Bit_model bm[] )
|
|
|
|
{
|
|
|
|
int model = 1;
|
|
|
|
int symbol = 0;
|
|
|
|
int bit = decode_bit( bm[model] );
|
|
|
|
model = (model << 1) + bit; symbol |= bit;
|
|
|
|
bit = decode_bit( bm[model] );
|
|
|
|
model = (model << 1) + bit; symbol |= (bit << 1);
|
|
|
|
bit = decode_bit( bm[model] );
|
|
|
|
model = (model << 1) + bit; symbol |= (bit << 2);
|
|
|
|
if( decode_bit( bm[model] ) ) symbol |= 8;
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
int decode_matched( Bit_model bm[], int match_byte )
|
2025-02-21 10:09:52 +01:00
|
|
|
{
|
|
|
|
Bit_model * const bm1 = bm + 0x100;
|
|
|
|
int symbol = 1;
|
|
|
|
for( int i = 7; i >= 0; --i )
|
|
|
|
{
|
2025-02-21 11:11:34 +01:00
|
|
|
match_byte <<= 1;
|
|
|
|
const int match_bit = match_byte & 0x100;
|
|
|
|
const int bit = decode_bit( bm1[match_bit+symbol] );
|
|
|
|
symbol = ( symbol << 1 ) + bit;
|
|
|
|
if( match_bit != bit << 8 )
|
2025-02-21 10:09:52 +01:00
|
|
|
{
|
2025-02-21 11:11:34 +01:00
|
|
|
while( symbol < 0x100 )
|
|
|
|
symbol = ( symbol << 1 ) + decode_bit( bm[symbol] );
|
2025-02-21 10:09:52 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2025-02-21 11:11:34 +01:00
|
|
|
return symbol - 0x100;
|
2025-02-21 10:09:52 +01:00
|
|
|
}
|
|
|
|
|
2025-02-21 11:12:17 +01:00
|
|
|
int decode_len( Len_model & lm, const int pos_state )
|
2025-02-21 10:09:52 +01:00
|
|
|
{
|
2025-02-21 11:12:17 +01:00
|
|
|
if( decode_bit( lm.choice1 ) == 0 )
|
|
|
|
return decode_tree( lm.bm_low[pos_state], len_low_bits );
|
|
|
|
if( decode_bit( lm.choice2 ) == 0 )
|
2025-02-21 10:09:52 +01:00
|
|
|
return len_low_symbols +
|
2025-02-21 11:12:17 +01:00
|
|
|
decode_tree( lm.bm_mid[pos_state], len_mid_bits );
|
2025-02-21 10:09:52 +01:00
|
|
|
return len_low_symbols + len_mid_symbols +
|
2025-02-21 11:12:17 +01:00
|
|
|
decode_tree( lm.bm_high, len_high_bits );
|
2025-02-21 10:09:52 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class LZ_decoder
|
|
|
|
{
|
2025-02-21 11:11:34 +01:00
|
|
|
const unsigned long long outskip;
|
|
|
|
const unsigned long long outend;
|
|
|
|
unsigned long long partial_data_pos;
|
2025-02-21 11:12:17 +01:00
|
|
|
Range_decoder & rdec;
|
2025-02-21 10:09:52 +01:00
|
|
|
const int dictionary_size;
|
|
|
|
const int buffer_size;
|
|
|
|
uint8_t * const buffer; // output buffer
|
|
|
|
int pos; // current pos in buffer
|
|
|
|
int stream_pos; // first byte not yet written to file
|
|
|
|
uint32_t crc_;
|
|
|
|
const int outfd; // output file descriptor
|
|
|
|
const int member_version;
|
|
|
|
|
2025-02-21 11:11:34 +01:00
|
|
|
unsigned long long stream_position() const { return partial_data_pos + stream_pos; }
|
2025-02-21 10:09:52 +01:00
|
|
|
void flush_data();
|
|
|
|
bool verify_trailer( const Pretty_print & pp ) const;
|
|
|
|
|
2025-02-21 10:13:18 +01:00
|
|
|
uint8_t get_prev_byte() const
|
2025-02-21 10:09:52 +01:00
|
|
|
{
|
|
|
|
const int i = ( ( pos > 0 ) ? pos : buffer_size ) - 1;
|
|
|
|
return buffer[i];
|
|
|
|
}
|
|
|
|
|
2025-02-21 10:13:18 +01:00
|
|
|
uint8_t get_byte( const int distance ) const
|
2025-02-21 10:09:52 +01:00
|
|
|
{
|
|
|
|
int i = pos - distance - 1;
|
|
|
|
if( i < 0 ) i += buffer_size;
|
|
|
|
return buffer[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
void put_byte( const uint8_t b )
|
|
|
|
{
|
|
|
|
buffer[pos] = b;
|
|
|
|
if( ++pos >= buffer_size ) flush_data();
|
|
|
|
}
|
|
|
|
|
|
|
|
void copy_block( const int distance, int len )
|
|
|
|
{
|
|
|
|
int i = pos - distance - 1;
|
|
|
|
if( i < 0 ) i += buffer_size;
|
|
|
|
if( len < buffer_size - std::max( pos, i ) && len <= std::abs( pos - i ) )
|
|
|
|
{
|
2025-02-21 11:11:34 +01:00
|
|
|
std::memcpy( buffer + pos, buffer + i, len ); // no wrap, no overlap
|
2025-02-21 10:09:52 +01:00
|
|
|
pos += len;
|
|
|
|
}
|
|
|
|
else for( ; len > 0; --len )
|
|
|
|
{
|
|
|
|
buffer[pos] = buffer[i];
|
|
|
|
if( ++pos >= buffer_size ) flush_data();
|
|
|
|
if( ++i >= buffer_size ) i = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-21 10:13:18 +01:00
|
|
|
LZ_decoder( const LZ_decoder & ); // declared as private
|
|
|
|
void operator=( const LZ_decoder & ); // declared as private
|
|
|
|
|
2025-02-21 10:09:52 +01:00
|
|
|
public:
|
2025-02-21 11:12:17 +01:00
|
|
|
LZ_decoder( const File_header & header, Range_decoder & rde, const int ofd,
|
2025-02-21 11:11:34 +01:00
|
|
|
const unsigned long long oskip = 0,
|
|
|
|
const unsigned long long oend = -1ULL )
|
2025-02-21 10:09:52 +01:00
|
|
|
:
|
2025-02-21 10:12:48 +01:00
|
|
|
outskip( oskip ),
|
|
|
|
outend( oend ),
|
2025-02-21 10:09:52 +01:00
|
|
|
partial_data_pos( 0 ),
|
2025-02-21 11:12:17 +01:00
|
|
|
rdec( rde ),
|
2025-02-21 10:09:52 +01:00
|
|
|
dictionary_size( header.dictionary_size() ),
|
|
|
|
buffer_size( std::max( 65536, dictionary_size ) ),
|
|
|
|
buffer( new uint8_t[buffer_size] ),
|
|
|
|
pos( 0 ),
|
|
|
|
stream_pos( 0 ),
|
|
|
|
crc_( 0xFFFFFFFFU ),
|
|
|
|
outfd( ofd ),
|
2025-02-21 11:11:34 +01:00
|
|
|
member_version( header.version() )
|
2025-02-21 10:09:52 +01:00
|
|
|
{ buffer[buffer_size-1] = 0; } // prev_byte of first_byte
|
|
|
|
|
|
|
|
~LZ_decoder() { delete[] buffer; }
|
|
|
|
|
2025-02-21 11:11:34 +01:00
|
|
|
unsigned crc() const { return crc_ ^ 0xFFFFFFFFU; }
|
2025-02-21 10:09:52 +01:00
|
|
|
|
2025-02-21 11:11:34 +01:00
|
|
|
unsigned long long data_position() const { return partial_data_pos + pos; }
|
2025-02-21 10:09:52 +01:00
|
|
|
|
|
|
|
int decode_member( const Pretty_print & pp );
|
|
|
|
};
|