summaryrefslogtreecommitdiffstats
path: root/decoder.c
diff options
context:
space:
mode:
authorDaniel Baumann <mail@daniel-baumann.ch>2015-11-06 11:35:51 +0000
committerDaniel Baumann <mail@daniel-baumann.ch>2015-11-06 11:35:51 +0000
commite697a57bc582b562dd0e0365479abf0bbfbb3f14 (patch)
treed186f0d655a47c6fe2388733427224d5fde2372a /decoder.c
parentAdding debian version 1.3-3. (diff)
downloadclzip-e697a57bc582b562dd0e0365479abf0bbfbb3f14.tar.xz
clzip-e697a57bc582b562dd0e0365479abf0bbfbb3f14.zip
Merging upstream version 1.4.
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to 'decoder.c')
-rw-r--r--decoder.c105
1 files changed, 65 insertions, 40 deletions
diff --git a/decoder.c b/decoder.c
index 02f7f0b..b40dafd 100644
--- a/decoder.c
+++ b/decoder.c
@@ -1,5 +1,5 @@
/* Clzip - Data compressor based on the LZMA algorithm
- Copyright (C) 2010, 2011, 2012 Antonio Diaz Diaz.
+ Copyright (C) 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
@@ -32,23 +32,40 @@
CRC32 crc32;
+void Pp_show_msg( struct Pretty_print * const pp, const char * const msg )
+ {
+ if( pp->verbosity >= 0 )
+ {
+ if( pp->first_post )
+ {
+ int i, len;
+ pp->first_post = false;
+ fprintf( stderr, " %s: ", pp->name );
+ len = pp->longest_name - strlen( pp->name );
+ for( i = 0; i < len; ++i ) fprintf( stderr, " " );
+ if( !msg ) fflush( stderr );
+ }
+ if( msg ) fprintf( stderr, "%s.\n", msg );
+ }
+ }
+
+
/* Returns the number of bytes really read.
If (returned value < size) and (errno == 0), means EOF was reached.
*/
int readblock( const int fd, uint8_t * const buf, const int size )
{
int rest = size;
- while( true )
+ errno = 0;
+ while( rest > 0 )
{
- int n;
- errno = 0;
- if( rest <= 0 ) break;
- n = read( fd, buf + size - rest, rest );
+ 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;
}
@@ -58,16 +75,15 @@ int readblock( const int fd, uint8_t * const buf, const int size )
int writeblock( const int fd, const uint8_t * const buf, const int size )
{
int rest = size;
- while( true )
+ errno = 0;
+ while( rest > 0 )
{
- int n;
- errno = 0;
- if( rest <= 0 ) break;
- n = write( fd, buf + size - rest, rest );
+ 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;
}
@@ -107,7 +123,7 @@ bool LZd_verify_trailer( struct LZ_decoder * const decoder,
{
File_trailer trailer;
const int trailer_size = Ft_versioned_size( decoder->member_version );
- const long long member_size =
+ const unsigned long long member_size =
Rd_member_position( decoder->range_decoder ) + trailer_size;
bool error = false;
@@ -123,7 +139,9 @@ bool LZd_verify_trailer( struct LZ_decoder * const decoder,
}
while( size < trailer_size ) trailer[size++] = 0;
}
+
if( decoder->member_version == 0 ) Ft_set_member_size( trailer, member_size );
+
if( decoder->range_decoder->code != 0 )
{
error = true;
@@ -136,8 +154,7 @@ bool LZd_verify_trailer( struct LZ_decoder * const decoder,
{
Pp_show_msg( pp, 0 );
fprintf( stderr, "CRC mismatch; trailer says %08X, data CRC is %08X.\n",
- (unsigned int)Ft_get_data_crc( trailer ),
- (unsigned int)LZd_crc( decoder ) );
+ Ft_get_data_crc( trailer ), LZd_crc( decoder ) );
}
}
if( Ft_get_data_size( trailer ) != LZd_data_position( decoder ) )
@@ -146,7 +163,7 @@ bool LZd_verify_trailer( struct LZ_decoder * const decoder,
if( pp->verbosity >= 0 )
{
Pp_show_msg( pp, 0 );
- fprintf( stderr, "Data size mismatch; trailer says %lld, data size is %lld (0x%llX).\n",
+ fprintf( stderr, "Data size mismatch; trailer says %llu, data size is %llu (0x%llX).\n",
Ft_get_data_size( trailer ), LZd_data_position( decoder ), LZd_data_position( decoder ) );
}
}
@@ -156,7 +173,7 @@ bool LZd_verify_trailer( struct LZ_decoder * const decoder,
if( pp->verbosity >= 0 )
{
Pp_show_msg( pp, 0 );
- fprintf( stderr, "Member size mismatch; trailer says %lld, member size is %lld (0x%llX).\n",
+ fprintf( stderr, "Member size mismatch; trailer says %llu, member size is %llu (0x%llX).\n",
Ft_get_member_size( trailer ), member_size, member_size );
}
}
@@ -166,8 +183,8 @@ bool LZd_verify_trailer( struct LZ_decoder * const decoder,
( 8.0 * member_size ) / LZd_data_position( decoder ),
100.0 * ( 1.0 - ( (double)member_size / LZd_data_position( decoder ) ) ) );
if( !error && pp->verbosity >= 4 )
- fprintf( stderr, "data CRC %08X, data size %9lld, member size %8lld. ",
- (unsigned int)Ft_get_data_crc( trailer ),
+ fprintf( stderr, "data CRC %08X, data size %9llu, member size %8llu. ",
+ Ft_get_data_crc( trailer ),
Ft_get_data_size( trailer ), Ft_get_member_size( trailer ) );
return !error;
}
@@ -178,10 +195,11 @@ bool LZd_verify_trailer( struct LZ_decoder * const decoder,
int LZd_decode_member( struct LZ_decoder * const decoder,
struct Pretty_print * const pp )
{
- unsigned int rep0 = 0; /* rep[0-3] latest four distances */
- unsigned int rep1 = 0; /* used for efficient coding of */
- unsigned int rep2 = 0; /* repeated distances */
- unsigned int rep3 = 0;
+ unsigned rep0 = 0; /* rep[0-3] latest four distances */
+ unsigned rep1 = 0; /* used for efficient coding of */
+ unsigned rep2 = 0; /* repeated distances */
+ unsigned rep3 = 0;
+
State state = 0;
Rd_load( decoder->range_decoder );
@@ -192,12 +210,17 @@ int LZd_decode_member( struct LZ_decoder * const decoder,
{
const uint8_t prev_byte = LZd_get_prev_byte( decoder );
if( St_is_char( state ) )
- LZd_put_byte( decoder, Lid_decode( &decoder->literal_decoder,
- decoder->range_decoder, prev_byte ) );
+ {
+ state -= ( state < 4 ) ? state : 3;
+ LZd_put_byte( decoder, Rd_decode_tree( decoder->range_decoder,
+ decoder->bm_literal[get_lit_state(prev_byte)], 8 ) );
+ }
else
- LZd_put_byte( decoder, Lid_decode_matched( &decoder->literal_decoder,
- decoder->range_decoder, prev_byte, LZd_get_byte( decoder, rep0 ) ) );
- St_set_char( &state );
+ {
+ state -= ( state < 10 ) ? 3 : 6;
+ LZd_put_byte( decoder, Rd_decode_matched( decoder->range_decoder,
+ decoder->bm_literal[get_lit_state(prev_byte)], LZd_get_byte( decoder, rep0 ) ) );
+ }
}
else
{
@@ -207,7 +230,7 @@ int LZd_decode_member( struct LZ_decoder * const decoder,
len = 0;
if( Rd_decode_bit( decoder->range_decoder, &decoder->bm_rep0[state] ) == 1 )
{
- unsigned int distance;
+ unsigned distance;
if( Rd_decode_bit( decoder->range_decoder, &decoder->bm_rep1[state] ) == 0 )
distance = rep1;
else
@@ -223,31 +246,33 @@ int LZd_decode_member( struct LZ_decoder * const decoder,
else
{
if( Rd_decode_bit( decoder->range_decoder, &decoder->bm_len[state][pos_state] ) == 0 )
- { St_set_short_rep( &state ); len = 1; }
+ { state = St_set_short_rep( state ); len = 1; }
}
if( len == 0 )
{
- St_set_rep( &state );
+ state = St_set_rep( state );
len = min_match_len + Led_decode( &decoder->rep_match_len_decoder, decoder->range_decoder, pos_state );
}
}
else
{
int dis_slot;
- const unsigned int rep0_saved = rep0;
+ const unsigned rep0_saved = rep0;
len = min_match_len + Led_decode( &decoder->len_decoder, decoder->range_decoder, pos_state );
- dis_slot = Rd_decode_tree( decoder->range_decoder, decoder->bm_dis_slot[get_dis_state(len)], dis_slot_bits );
+ dis_slot = Rd_decode_tree6( decoder->range_decoder, decoder->bm_dis_slot[get_dis_state(len)] );
if( dis_slot < start_dis_model ) rep0 = dis_slot;
else
{
const int direct_bits = ( dis_slot >> 1 ) - 1;
rep0 = ( 2 | ( dis_slot & 1 ) ) << direct_bits;
if( dis_slot < end_dis_model )
- rep0 += Rd_decode_tree_reversed( decoder->range_decoder, decoder->bm_dis + rep0 - dis_slot, direct_bits );
+ rep0 += Rd_decode_tree_reversed( decoder->range_decoder,
+ decoder->bm_dis + rep0 - dis_slot - 1,
+ direct_bits );
else
{
rep0 += Rd_decode( decoder->range_decoder, direct_bits - dis_align_bits ) << dis_align_bits;
- rep0 += Rd_decode_tree_reversed( decoder->range_decoder, decoder->bm_align, dis_align_bits );
+ rep0 += Rd_decode_tree_reversed4( decoder->range_decoder, decoder->bm_align );
if( rep0 == 0xFFFFFFFFU ) /* Marker found */
{
rep0 = rep0_saved;
@@ -271,9 +296,9 @@ int LZd_decode_member( struct LZ_decoder * const decoder,
}
}
rep3 = rep2; rep2 = rep1; rep1 = rep0_saved;
- St_set_match( &state );
- if( rep0 >= (unsigned int)decoder->dictionary_size ||
- ( rep0 >= (unsigned int)decoder->pos && !decoder->partial_data_pos ) )
+ state = St_set_match( state );
+ if( rep0 >= (unsigned)decoder->dictionary_size ||
+ ( rep0 >= (unsigned)decoder->pos && !decoder->partial_data_pos ) )
{ LZd_flush_data( decoder ); return 1; }
}
LZd_copy_block( decoder, rep0, len );