summaryrefslogtreecommitdiffstats
path: root/decoder.c
diff options
context:
space:
mode:
Diffstat (limited to 'decoder.c')
-rw-r--r--decoder.c51
1 files changed, 22 insertions, 29 deletions
diff --git a/decoder.c b/decoder.c
index 3511f9a..20364c0 100644
--- a/decoder.c
+++ b/decoder.c
@@ -1,5 +1,5 @@
/* Lunzip - Decompressor for the lzip format
- Copyright (C) 2010-2015 Antonio Diaz Diaz.
+ Copyright (C) 2010-2016 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
@@ -38,10 +38,11 @@ void Pp_show_msg( struct Pretty_print * const pp, const char * const msg )
{
if( pp->first_post )
{
- int i, len = pp->longest_name - strlen( pp->name );
+ unsigned i;
pp->first_post = false;
fprintf( stderr, " %s: ", pp->name );
- for( i = 0; i < len; ++i ) fputc( ' ', stderr );
+ for( i = strlen( pp->name ); i < pp->longest_name; ++i )
+ fputc( ' ', stderr );
if( !msg ) fflush( stderr );
}
if( msg ) fprintf( stderr, "%s\n", msg );
@@ -120,7 +121,8 @@ void LZd_flush_data( struct LZ_decoder * const d )
writeblock( d->outfd, d->buffer + d->stream_pos, size ) != size )
{ show_error( "Write error", errno, false ); cleanup_and_fail( 1 ); }
if( d->pos >= d->buffer_size )
- { d->partial_data_pos += d->pos; d->pos = 0; }
+ { d->partial_data_pos += d->pos; d->pos = 0;
+ if( d->partial_data_pos >= d->dictionary_size ) d->pos_wrapped = true; }
d->stream_pos = d->pos;
}
}
@@ -130,13 +132,11 @@ static bool LZd_verify_trailer( struct LZ_decoder * const d,
struct Pretty_print * const pp )
{
File_trailer trailer;
- const unsigned long long member_size = Rd_member_position( d->rdec ) + Ft_size;
- unsigned long long trailer_data_size;
- unsigned long long trailer_member_size;
- unsigned trailer_crc;
+ int size = Rd_read_data( d->rdec, trailer, Ft_size );
+ const unsigned long long data_size = LZd_data_position( d );
+ const unsigned long long member_size = Rd_member_position( d->rdec );
bool error = false;
- int size = Rd_read_data( d->rdec, trailer, Ft_size );
if( size < Ft_size )
{
error = true;
@@ -149,52 +149,44 @@ static bool LZd_verify_trailer( struct LZ_decoder * const d,
while( size < Ft_size ) trailer[size++] = 0;
}
- if( d->rdec->code != 0 )
- {
- error = true;
- Pp_show_msg( pp, "Range decoder final code is not zero." );
- }
- trailer_crc = Ft_get_data_crc( trailer );
- if( trailer_crc != LZd_crc( d ) )
+ if( Ft_get_data_crc( trailer ) != LZd_crc( d ) )
{
error = true;
if( verbosity >= 0 )
{
Pp_show_msg( pp, 0 );
fprintf( stderr, "CRC mismatch; trailer says %08X, data CRC is %08X\n",
- trailer_crc, LZd_crc( d ) );
+ Ft_get_data_crc( trailer ), LZd_crc( d ) );
}
}
- trailer_data_size = Ft_get_data_size( trailer );
- if( trailer_data_size != LZd_data_position( d ) )
+ if( Ft_get_data_size( trailer ) != data_size )
{
error = true;
if( verbosity >= 0 )
{
Pp_show_msg( pp, 0 );
fprintf( stderr, "Data size mismatch; trailer says %llu, data size is %llu (0x%llX)\n",
- trailer_data_size, LZd_data_position( d ), LZd_data_position( d ) );
+ Ft_get_data_size( trailer ), data_size, data_size );
}
}
- trailer_member_size = Ft_get_member_size( trailer );
- if( trailer_member_size != member_size )
+ if( Ft_get_member_size( trailer ) != member_size )
{
error = true;
if( verbosity >= 0 )
{
Pp_show_msg( pp, 0 );
fprintf( stderr, "Member size mismatch; trailer says %llu, member size is %llu (0x%llX)\n",
- trailer_member_size, member_size, member_size );
+ Ft_get_member_size( trailer ), member_size, member_size );
}
}
- if( !error && verbosity >= 2 && LZd_data_position( d ) > 0 && member_size > 0 )
+ if( !error && verbosity >= 2 && data_size > 0 && member_size > 0 )
fprintf( stderr, "%6.3f:1, %6.3f bits/byte, %5.2f%% saved. ",
- (double)LZd_data_position( d ) / member_size,
- ( 8.0 * member_size ) / LZd_data_position( d ),
- 100.0 * ( 1.0 - ( (double)member_size / LZd_data_position( d ) ) ) );
+ (double)data_size / member_size,
+ ( 8.0 * member_size ) / data_size,
+ 100.0 * ( 1.0 - ( (double)member_size / data_size ) ) );
if( !error && verbosity >= 4 )
fprintf( stderr, "data CRC %08X, data size %9llu, member size %8llu. ",
- trailer_crc, trailer_data_size, trailer_member_size );
+ LZd_crc( d ), data_size, member_size );
return !error;
}
@@ -308,7 +300,8 @@ int LZd_decode_member( struct LZ_decoder * const d,
}
rep3 = rep2; rep2 = rep1; rep1 = rep0_saved;
state = St_set_match( state );
- if( rep0 >= d->dictionary_size || rep0 >= LZd_data_position( d ) )
+ if( rep0 >= d->dictionary_size ||
+ ( rep0 >= LZd_data_position( d ) && !d->pos_wrapped ) )
{ LZd_flush_data( d ); return 1; }
}
copy_block( d, rep0, len );