summaryrefslogtreecommitdiffstats
path: root/decoder.h
diff options
context:
space:
mode:
Diffstat (limited to 'decoder.h')
-rw-r--r--decoder.h119
1 files changed, 64 insertions, 55 deletions
diff --git a/decoder.h b/decoder.h
index 8bc64bc..cba634b 100644
--- a/decoder.h
+++ b/decoder.h
@@ -1,5 +1,5 @@
/* Lziprecover - Data recovery tool for the lzip format
- Copyright (C) 2009-2016 Antonio Diaz Diaz.
+ Copyright (C) 2009-2017 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
@@ -50,7 +50,9 @@ public:
unsigned get_code() const { return code; }
bool finished() { return pos >= stream_pos && !read_block(); }
unsigned long long member_position() const { return partial_member_pos + pos; }
- void reset_member_position() { partial_member_pos = -pos; }
+
+ void reset_member_position()
+ { partial_member_pos = 0; partial_member_pos -= pos; }
uint8_t get_byte()
{
@@ -61,15 +63,15 @@ public:
int read_data( uint8_t * const outbuf, const int size )
{
- int rest = size;
- while( rest > 0 && !finished() )
+ int sz = 0;
+ while( sz < size && !finished() )
{
- const int rd = std::min( rest, stream_pos - pos );
- std::memcpy( outbuf + size - rest, buffer + pos, rd );
+ const int rd = std::min( size - sz, stream_pos - pos );
+ std::memcpy( outbuf + sz, buffer + pos, rd );
pos += rd;
- rest -= rd;
+ sz += rd;
}
- return size - rest;
+ return sz;
}
void load()
@@ -86,24 +88,23 @@ public:
{ range <<= 8; code = (code << 8) | get_byte(); }
}
- int decode( const int num_bits )
+ unsigned decode( const int num_bits )
{
- int symbol = 0;
+ unsigned symbol = 0;
for( int i = num_bits; i > 0; --i )
{
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);
+ const bool bit = ( code >= range );
+ symbol = ( symbol << 1 ) + bit;
+ code -= range & ( 0U - bit );
}
return symbol;
}
- int decode_bit( Bit_model & bm )
+ unsigned decode_bit( Bit_model & bm )
{
normalize();
const uint32_t bound = ( range >> bit_model_total_bits ) * bm.probability;
@@ -122,18 +123,18 @@ public:
}
}
- int decode_tree3( Bit_model bm[] )
+ unsigned decode_tree3( Bit_model bm[] )
{
- int symbol = 1;
+ unsigned symbol = 1;
symbol = ( symbol << 1 ) | decode_bit( bm[symbol] );
symbol = ( symbol << 1 ) | decode_bit( bm[symbol] );
symbol = ( symbol << 1 ) | decode_bit( bm[symbol] );
return symbol & 7;
}
- int decode_tree6( Bit_model bm[] )
+ unsigned decode_tree6( Bit_model bm[] )
{
- int symbol = 1;
+ unsigned symbol = 1;
symbol = ( symbol << 1 ) | decode_bit( bm[symbol] );
symbol = ( symbol << 1 ) | decode_bit( bm[symbol] );
symbol = ( symbol << 1 ) | decode_bit( bm[symbol] );
@@ -143,49 +144,47 @@ public:
return symbol & 0x3F;
}
- int decode_tree8( Bit_model bm[] )
+ unsigned decode_tree8( Bit_model bm[] )
{
- int symbol = 1;
- while( symbol < 0x100 )
+ unsigned symbol = 1;
+ for( int i = 0; i < 8; ++i )
symbol = ( symbol << 1 ) | decode_bit( bm[symbol] );
return symbol & 0xFF;
}
- int decode_tree_reversed( Bit_model bm[], const int num_bits )
+ unsigned decode_tree_reversed( Bit_model bm[], const int num_bits )
{
- int model = 1;
- int symbol = 0;
+ unsigned model = 1;
+ unsigned symbol = 0;
for( int i = 0; i < num_bits; ++i )
{
- const bool bit = decode_bit( bm[model] );
- model <<= 1;
- if( bit ) { ++model; symbol |= (1 << i); }
+ const unsigned bit = decode_bit( bm[model] );
+ model = ( model << 1 ) + bit;
+ symbol |= ( bit << i );
}
return symbol;
}
- int decode_tree_reversed4( Bit_model bm[] )
+ unsigned decode_tree_reversed4( Bit_model bm[] )
{
- int model = 1;
- int symbol = decode_bit( bm[model] );
- model = (model << 1) + symbol;
- int bit = decode_bit( bm[model] );
- model = (model << 1) + bit; symbol |= (bit << 1);
+ unsigned symbol = decode_bit( bm[1] );
+ unsigned model = 2 + symbol;
+ unsigned 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;
+ model = ( model << 1 ) + bit; symbol |= ( bit << 2 );
+ symbol |= ( decode_bit( bm[model] ) << 3 );
return symbol;
}
- int decode_matched( Bit_model bm[], int match_byte )
+ unsigned decode_matched( Bit_model bm[], unsigned match_byte )
{
Bit_model * const bm1 = bm + 0x100;
- int symbol = 1;
+ unsigned symbol = 1;
while( symbol < 0x100 )
{
- match_byte <<= 1;
- const int match_bit = match_byte & 0x100;
- const int bit = decode_bit( bm1[match_bit+symbol] );
+ const unsigned match_bit = ( match_byte <<= 1 ) & 0x100;
+ const unsigned bit = decode_bit( bm1[match_bit+symbol] );
symbol = ( symbol << 1 ) | bit;
if( match_bit != bit << 8 )
{
@@ -197,7 +196,7 @@ public:
return symbol & 0xFF;
}
- int decode_len( Len_model & lm, const int pos_state )
+ unsigned decode_len( Len_model & lm, const int pos_state )
{
if( decode_bit( lm.choice1 ) == 0 )
return decode_tree3( lm.bm_low[pos_state] );
@@ -229,14 +228,15 @@ class LZ_decoder
uint8_t peek_prev() const
{
- const unsigned i = ( ( pos > 0 ) ? pos : dictionary_size ) - 1;
- return buffer[i];
+ if( pos > 0 ) return buffer[pos-1];
+ if( pos_wrapped ) return buffer[dictionary_size-1];
+ return 0; // prev_byte of first byte
}
uint8_t peek( const unsigned distance ) const
{
- unsigned i = pos - distance - 1;
- if( pos <= distance ) i += dictionary_size;
+ const unsigned i = ( ( pos > distance ) ? 0 : dictionary_size ) +
+ pos - distance - 1;
return buffer[i];
}
@@ -248,17 +248,26 @@ class LZ_decoder
void copy_block( const unsigned distance, unsigned len )
{
- unsigned i = pos - distance - 1;
- bool fast;
- if( pos <= distance )
- { i += dictionary_size;
- fast = ( len <= dictionary_size - i && len <= i - pos ); }
+ unsigned lpos = pos, i = lpos - distance - 1;
+ bool fast, fast2;
+ if( lpos > distance )
+ {
+ fast = ( len < dictionary_size - lpos );
+ fast2 = ( fast && len <= lpos - i );
+ }
else
- fast = ( len < dictionary_size - pos && len <= pos - i );
- if( fast ) // no wrap, no overlap
{
- std::memcpy( buffer + pos, buffer + i, len );
+ i += dictionary_size;
+ fast = ( len < dictionary_size - i ); // (i == pos) may happen
+ fast2 = ( fast && len <= i - lpos );
+ }
+ if( fast ) // no wrap
+ {
pos += len;
+ if( fast2 ) // no wrap, no overlap
+ std::memcpy( buffer + lpos, buffer + i, len );
+ else
+ for( ; len > 0; --len ) buffer[lpos++] = buffer[i++];
}
else for( ; len > 0; --len )
{
@@ -287,7 +296,7 @@ public:
crc_( 0xFFFFFFFFU ),
outfd( ofd ),
pos_wrapped( false )
- { buffer[dictionary_size-1] = 0; } // prev_byte of first byte
+ {}
~LZ_decoder() { delete[] buffer; }