summaryrefslogtreecommitdiffstats
path: root/decoder.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--decoder.h153
1 files changed, 74 insertions, 79 deletions
diff --git a/decoder.h b/decoder.h
index 118fe23..3b97014 100644
--- a/decoder.h
+++ b/decoder.h
@@ -1,5 +1,5 @@
/* Lunzip - Decompressor for lzip files
- Copyright (C) 2010, 2011, 2012, 2013 Antonio Diaz Diaz.
+ Copyright (C) 2010, 2011, 2012, 2013, 2014 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
@@ -180,9 +180,9 @@ static inline int Rd_decode_tree_reversed4( struct Range_decoder * const rdec,
Bit_model bm[] )
{
int model = 1;
- int symbol = 0;
- int bit = Rd_decode_bit( rdec, &bm[model] );
- model = (model << 1) + bit; symbol |= bit;
+ int symbol = Rd_decode_bit( rdec, &bm[model] );
+ int bit;
+ model = (model << 1) + symbol;
bit = Rd_decode_bit( rdec, &bm[model] );
model = (model << 1) + bit; symbol |= (bit << 1);
bit = Rd_decode_bit( rdec, &bm[model] );
@@ -196,8 +196,7 @@ static inline int Rd_decode_matched( struct Range_decoder * const rdec,
{
Bit_model * const bm1 = bm + 0x100;
int symbol = 1;
- int i;
- for( i = 7; i >= 0; --i )
+ while( symbol < 0x100 )
{
int match_bit, bit;
match_byte <<= 1;
@@ -211,7 +210,7 @@ static inline int Rd_decode_matched( struct Range_decoder * const rdec,
break;
}
}
- return symbol - 0x100;
+ return symbol & 0xFF;
}
static inline int Rd_decode_len( struct Range_decoder * const rdec,
@@ -231,7 +230,8 @@ static inline int Rd_decode_len( struct Range_decoder * const rdec,
struct LZ_decoder
{
unsigned long long partial_data_pos;
- int dictionary_size;
+ struct Range_decoder * rdec;
+ unsigned dictionary_size;
int buffer_size;
uint8_t * buffer; /* output buffer */
int pos; /* current pos in buffer */
@@ -250,128 +250,123 @@ struct LZ_decoder
Bit_model bm_dis[modeled_distances-end_dis_model];
Bit_model bm_align[dis_align_size];
- struct Range_decoder * rdec;
struct Len_model match_len_model;
struct Len_model rep_len_model;
};
-void LZd_flush_data( struct LZ_decoder * const decoder );
+void LZd_flush_data( struct LZ_decoder * const d );
-bool LZd_verify_trailer( struct LZ_decoder * const decoder,
+bool LZd_verify_trailer( struct LZ_decoder * const d,
struct Pretty_print * const pp );
int seek_read( const int fd, uint8_t * const buf, const int size,
const int offset );
-static inline uint8_t LZd_get_prev_byte( const struct LZ_decoder * const decoder )
+static inline uint8_t LZd_get_prev_byte( const struct LZ_decoder * const d )
{
- const int i =
- ( ( decoder->pos > 0 ) ? decoder->pos : decoder->buffer_size ) - 1;
- return decoder->buffer[i];
+ const int i = ( ( d->pos > 0 ) ? d->pos : d->buffer_size ) - 1;
+ return d->buffer[i];
}
-static inline uint8_t LZd_get_byte( const struct LZ_decoder * const decoder,
+static inline uint8_t LZd_get_byte( const struct LZ_decoder * const d,
const int distance )
{
uint8_t b;
- const int i = decoder->pos - distance - 1;
- if( i >= 0 ) b = decoder->buffer[i];
- else if( i + decoder->buffer_size >= decoder->pos )
- b = decoder->buffer[i+decoder->buffer_size];
- else if( seek_read( decoder->outfd, &b, 1, i - decoder->stream_pos ) != 1 )
+ const int i = d->pos - distance - 1;
+ if( i >= 0 ) b = d->buffer[i];
+ else if( i + d->buffer_size >= d->pos )
+ b = d->buffer[i+d->buffer_size];
+ else if( seek_read( d->outfd, &b, 1, i - d->stream_pos ) != 1 )
{ show_error( "Seek error", errno, false ); cleanup_and_fail( 1 ); }
return b;
}
-static inline void LZd_put_byte( struct LZ_decoder * const decoder,
- const uint8_t b )
+static inline void LZd_put_byte( struct LZ_decoder * const d, const uint8_t b )
{
- decoder->buffer[decoder->pos] = b;
- if( ++decoder->pos >= decoder->buffer_size ) LZd_flush_data( decoder );
+ d->buffer[d->pos] = b;
+ if( ++d->pos >= d->buffer_size ) LZd_flush_data( d );
}
-static inline void LZd_copy_block( struct LZ_decoder * const decoder,
+static inline void LZd_copy_block( struct LZ_decoder * const d,
const int distance, int len )
{
- int i = decoder->pos - distance - 1;
- if( i < 0 ) i += decoder->buffer_size;
- if( len < decoder->buffer_size - max( decoder->pos, i ) &&
- len <= abs( decoder->pos - i ) ) /* no wrap, no overlap */
+ int i = d->pos - distance - 1;
+ if( i < 0 ) i += d->buffer_size;
+ if( len < d->buffer_size - max( d->pos, i ) && len <= abs( d->pos - i ) )
{
- memcpy( decoder->buffer + decoder->pos, decoder->buffer + i, len );
- decoder->pos += len;
+ memcpy( d->buffer + d->pos, d->buffer + i, len ); /* no wrap, no overlap */
+ d->pos += len;
}
else for( ; len > 0; --len )
{
- decoder->buffer[decoder->pos] = decoder->buffer[i];
- if( ++decoder->pos >= decoder->buffer_size ) LZd_flush_data( decoder );
- if( ++i >= decoder->buffer_size ) i = 0;
+ d->buffer[d->pos] = d->buffer[i];
+ if( ++d->pos >= d->buffer_size ) LZd_flush_data( d );
+ if( ++i >= d->buffer_size ) i = 0;
}
}
-static inline void LZd_copy_block2( struct LZ_decoder * const decoder,
+static inline void LZd_copy_block2( struct LZ_decoder * const d,
const int distance, int len )
{
- if( distance < decoder->buffer_size ) /* block is in buffer */
- { LZd_copy_block( decoder, distance, len ); return; }
- if( len < decoder->buffer_size - decoder->pos ) /* no wrap */
+ if( distance < d->buffer_size ) /* block is in buffer */
+ { LZd_copy_block( d, distance, len ); return; }
+ if( len < d->buffer_size - d->pos ) /* no wrap */
{
- const int offset = decoder->pos - decoder->stream_pos - distance - 1;
+ const int offset = d->pos - d->stream_pos - distance - 1;
if( len <= -offset ) /* block is in file */
{
- if( seek_read( decoder->outfd, decoder->buffer + decoder->pos, len, offset ) != len )
+ if( seek_read( d->outfd, d->buffer + d->pos, len, offset ) != len )
{ show_error( "Seek error", errno, false ); cleanup_and_fail( 1 ); }
- decoder->pos += len;
+ d->pos += len;
return;
}
}
for( ; len > 0; --len )
- LZd_put_byte( decoder, LZd_get_byte( decoder, distance ) );
+ LZd_put_byte( d, LZd_get_byte( d, distance ) );
}
-static inline bool LZd_init( struct LZ_decoder * const decoder,
- const File_header header,
+static inline bool LZd_init( struct LZ_decoder * const d,
struct Range_decoder * const rde,
- const int buffer_size, const int ofd )
+ const int buffer_size,
+ const int dict_size, const int ofd )
{
- decoder->partial_data_pos = 0;
- decoder->dictionary_size = Fh_get_dictionary_size( header );
- decoder->buffer_size =
- min( buffer_size, max( 65536, decoder->dictionary_size ) );
- decoder->buffer = (uint8_t *)malloc( decoder->buffer_size );
- if( !decoder->buffer ) return false;
- decoder->pos = 0;
- decoder->stream_pos = 0;
- decoder->crc = 0xFFFFFFFFU;
- decoder->outfd = ofd;
-
- Bm_array_init( decoder->bm_literal[0], (1 << literal_context_bits) * 0x300 );
- Bm_array_init( decoder->bm_match[0], states * pos_states );
- Bm_array_init( decoder->bm_rep, states );
- Bm_array_init( decoder->bm_rep0, states );
- Bm_array_init( decoder->bm_rep1, states );
- Bm_array_init( decoder->bm_rep2, states );
- Bm_array_init( decoder->bm_len[0], states * pos_states );
- Bm_array_init( decoder->bm_dis_slot[0], len_states * (1 << dis_slot_bits) );
- Bm_array_init( decoder->bm_dis, modeled_distances - end_dis_model );
- Bm_array_init( decoder->bm_align, dis_align_size );
-
- decoder->rdec = rde;
- Lm_init( &decoder->match_len_model );
- Lm_init( &decoder->rep_len_model );
- decoder->buffer[decoder->buffer_size-1] = 0; /* prev_byte of first_byte */
+ d->partial_data_pos = 0;
+ d->rdec = rde;
+ d->dictionary_size = dict_size;
+ d->buffer_size = min( buffer_size, max( 65536, dict_size ) );
+ d->buffer = (uint8_t *)malloc( d->buffer_size );
+ if( !d->buffer ) return false;
+ d->pos = 0;
+ d->stream_pos = 0;
+ d->crc = 0xFFFFFFFFU;
+ d->outfd = ofd;
+
+ Bm_array_init( d->bm_literal[0], (1 << literal_context_bits) * 0x300 );
+ Bm_array_init( d->bm_match[0], states * pos_states );
+ Bm_array_init( d->bm_rep, states );
+ Bm_array_init( d->bm_rep0, states );
+ Bm_array_init( d->bm_rep1, states );
+ Bm_array_init( d->bm_rep2, states );
+ Bm_array_init( d->bm_len[0], states * pos_states );
+ Bm_array_init( d->bm_dis_slot[0], len_states * (1 << dis_slot_bits) );
+ Bm_array_init( d->bm_dis, modeled_distances - end_dis_model );
+ Bm_array_init( d->bm_align, dis_align_size );
+
+ Lm_init( &d->match_len_model );
+ Lm_init( &d->rep_len_model );
+ d->buffer[d->buffer_size-1] = 0; /* prev_byte of first_byte */
return true;
}
-static inline void LZd_free( struct LZ_decoder * const decoder )
- { free( decoder->buffer ); }
+static inline void LZd_free( struct LZ_decoder * const d )
+ { free( d->buffer ); }
-static inline unsigned LZd_crc( const struct LZ_decoder * const decoder )
- { return decoder->crc ^ 0xFFFFFFFFU; }
+static inline unsigned LZd_crc( const struct LZ_decoder * const d )
+ { return d->crc ^ 0xFFFFFFFFU; }
static inline unsigned long long
-LZd_data_position( const struct LZ_decoder * const decoder )
- { return decoder->partial_data_pos + decoder->pos; }
+LZd_data_position( const struct LZ_decoder * const d )
+ { return d->partial_data_pos + d->pos; }
-int LZd_decode_member( struct LZ_decoder * const decoder,
+int LZd_decode_member( struct LZ_decoder * const d,
struct Pretty_print * const pp );