summaryrefslogtreecommitdiffstats
path: root/decoder.cc
diff options
context:
space:
mode:
Diffstat (limited to 'decoder.cc')
-rw-r--r--decoder.cc33
1 files changed, 21 insertions, 12 deletions
diff --git a/decoder.cc b/decoder.cc
index aeac2cd..0cb6188 100644
--- a/decoder.cc
+++ b/decoder.cc
@@ -17,6 +17,7 @@
#define _FILE_OFFSET_BITS 64
+#include <algorithm>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
@@ -45,16 +46,15 @@ bool Input_buffer::read_block()
void LZ_decoder::flush_data()
{
- if( !member_finished )
+ const int size = pos - stream_pos;
+ if( size > 0 )
{
- crc32.update( crc_, buffer, pos );
- if( odes_ >= 0 )
- {
- const int wr = writeblock( odes_, (char *)buffer, pos );
- if( wr != pos ) throw Error( "write error" );
- }
+ crc32.update( crc_, buffer + stream_pos, size );
+ if( odes_ >= 0 &&
+ writeblock( odes_, (char *)buffer + stream_pos, size ) != size )
+ throw Error( "write error" );
if( pos >= buffer_size ) { partial_data_pos += pos; pos = 0; }
- else member_finished = true;
+ stream_pos = pos;
}
}
@@ -180,9 +180,8 @@ int LZ_decoder::decode_member( const Pretty_print & pp )
}
else
{
- rep3 = rep2; rep2 = rep1; rep1 = rep0;
+ unsigned int rep0_saved = rep0;
len = min_match_len + len_decoder.decode( range_decoder, pos_state );
- state.set_match();
const int dis_slot = range_decoder.decode_tree( bm_dis_slot[get_dis_state(len)], dis_slot_bits );
if( dis_slot < start_dis_model ) rep0 = dis_slot;
else
@@ -197,10 +196,17 @@ int LZ_decoder::decode_member( const Pretty_print & pp )
rep0 += range_decoder.decode_tree_reversed( bm_align, dis_align_bits );
if( rep0 == 0xFFFFFFFF ) // Marker found
{
+ rep0 = rep0_saved;
range_decoder.normalize();
flush_data();
if( len == min_match_len ) // End Of Stream marker
- { if( verify_trailer( pp ) ) return 0; else return 3; }
+ {
+ if( verify_trailer( pp ) ) return 0; else return 3;
+ }
+ if( len == min_match_len + 1 ) // Sync Flush marker
+ {
+ range_decoder.reload(); continue;
+ }
if( verbosity >= 0 )
{
pp();
@@ -208,9 +214,12 @@ int LZ_decoder::decode_member( const Pretty_print & pp )
}
return 4;
}
- if( rep0 >= (unsigned int)buffer_size ) { flush_data(); return 1; }
+ if( rep0 >= (unsigned int)dictionary_size )
+ { flush_data(); return 1; }
}
}
+ rep3 = rep2; rep2 = rep1; rep1 = rep0_saved;
+ state.set_match();
}
copy_block( rep0, len );
prev_byte = get_byte( 0 );