summaryrefslogtreecommitdiffstats
path: root/decoder.h
diff options
context:
space:
mode:
Diffstat (limited to 'decoder.h')
-rw-r--r--decoder.h134
1 files changed, 64 insertions, 70 deletions
diff --git a/decoder.h b/decoder.h
index af044d0..ec82667 100644
--- a/decoder.h
+++ b/decoder.h
@@ -1,5 +1,5 @@
/* Clzip - LZMA lossless data compressor
- 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
@@ -85,6 +85,7 @@ static inline void Rd_load( struct Range_decoder * const rdec )
for( i = 0; i < 5; ++i )
rdec->code = (rdec->code << 8) | Rd_get_byte( rdec );
rdec->range = 0xFFFFFFFFU;
+ rdec->code &= rdec->range; /* make sure that first byte is discarded */
}
static inline void Rd_normalize( struct Range_decoder * const rdec )
@@ -179,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] );
@@ -195,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;
@@ -210,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,
@@ -230,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 */
@@ -249,98 +250,91 @@ 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 );
-
-bool LZd_verify_trailer( struct LZ_decoder * const decoder,
- struct Pretty_print * const pp );
+void LZd_flush_data( struct LZ_decoder * const d );
-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 )
{
- int i = decoder->pos - distance - 1;
- if( i < 0 ) i += decoder->buffer_size;
- return decoder->buffer[i];
+ int i = d->pos - distance - 1;
+ if( i < 0 ) i += d->buffer_size;
+ return d->buffer[i];
}
-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 bool LZd_init( struct LZ_decoder * const decoder,
- const File_header header,
- struct Range_decoder * const rde, const int ofd )
+static inline bool LZd_init( struct LZ_decoder * const d,
+ struct Range_decoder * const rde,
+ const int dict_size, const int ofd )
{
- decoder->partial_data_pos = 0;
- decoder->dictionary_size = Fh_get_dictionary_size( header );
- decoder->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 = 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 );