diff options
Diffstat (limited to 'decoder.h')
-rw-r--r-- | decoder.h | 136 |
1 files changed, 73 insertions, 63 deletions
@@ -1,4 +1,4 @@ -/* Clzip - A data compressor based on the LZMA algorithm +/* Clzip - Data compressor based on the LZMA algorithm Copyright (C) 2010 Antonio Diaz Diaz. This program is free software: you can redistribute it and/or modify @@ -15,36 +15,36 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ -enum { Rd_buffer_size = 16384 }; +enum { rd_buffer_size = 16384 }; struct Range_decoder { long long partial_member_pos; - uint8_t * buffer; // input buffer - int pos; - int stream_pos; // when reached, a new block must be read + uint8_t * buffer; /* input buffer */ + int pos; /* current pos in buffer */ + int stream_pos; /* when reached, a new block must be read */ uint32_t code; uint32_t range; - int infd_; // input file descriptor + int infd; /* input file descriptor */ bool at_stream_end; }; bool Rd_read_block( struct Range_decoder * const rdec ); -static inline void Rd_init( struct Range_decoder * const rdec, const int infd ) +static inline void Rd_init( struct Range_decoder * const rdec, const int ifd ) { rdec->partial_member_pos = 0; - rdec->buffer = (uint8_t *)malloc( Rd_buffer_size ); + rdec->buffer = (uint8_t *)malloc( rd_buffer_size ); if( !rdec->buffer ) { - show_error( "not enough memory. Find a machine with more memory", 0, false ); + show_error( "Not enough memory. Find a machine with more memory.", 0, false ); cleanup_and_fail( 1 ); } rdec->pos = 0; rdec->stream_pos = 0; rdec->code = 0; rdec->range = 0xFFFFFFFFU; - rdec->infd_ = infd; + rdec->infd = ifd; rdec->at_stream_end = false; } @@ -55,7 +55,7 @@ static inline bool Rd_code_is_zero( struct Range_decoder * const rdec ) { return ( rdec->code == 0 ); } static inline bool Rd_finished( struct Range_decoder * const rdec ) - { return rdec->at_stream_end && rdec->pos >= rdec->stream_pos; } + { return rdec->pos >= rdec->stream_pos && !Rd_read_block( rdec ); } static inline long long Rd_member_position( struct Range_decoder * const rdec ) { return rdec->partial_member_pos + rdec->pos; } @@ -65,15 +65,16 @@ static inline void Rd_reset_member_position( struct Range_decoder * const rdec ) static inline uint8_t Rd_get_byte( struct Range_decoder * const rdec ) { - if( rdec->pos >= rdec->stream_pos && !Rd_read_block( rdec ) ) return 0; + if( Rd_finished( rdec ) ) return 0; return rdec->buffer[rdec->pos++]; } static inline void Rd_load( struct Range_decoder * const rdec ) { + int i; rdec->code = 0; rdec->range = 0xFFFFFFFFU; - for( int i = 0; i < 5; ++i ) + for( i = 0; i < 5; ++i ) rdec->code = (rdec->code << 8) | Rd_get_byte( rdec ); } @@ -90,7 +91,8 @@ static inline int Rd_decode( struct Range_decoder * const rdec, const int num_bits ) { int symbol = 0; - for( int i = num_bits; i > 0; --i ) + int i; + for( i = num_bits; i > 0; --i ) { symbol <<= 1; if( rdec->range <= 0x00FFFFFFU ) @@ -113,9 +115,9 @@ static inline int Rd_decode( struct Range_decoder * const rdec, static inline int Rd_decode_bit( struct Range_decoder * const rdec, Bit_model * const probability ) { + uint32_t bound; Rd_normalize( rdec ); - const uint32_t bound = ( rdec->range >> bit_model_total_bits ) * - *probability; + bound = ( rdec->range >> bit_model_total_bits ) * *probability; if( rdec->code < bound ) { rdec->range = bound; @@ -135,7 +137,8 @@ static inline int Rd_decode_tree( struct Range_decoder * const rdec, Bit_model bm[], const int num_bits ) { int model = 1; - for( int i = num_bits; i > 0; --i ) + int i; + for( i = num_bits; i > 0; --i ) model = ( model << 1 ) | Rd_decode_bit( rdec, &bm[model] ); return model - (1 << num_bits); } @@ -145,7 +148,8 @@ static inline int Rd_decode_tree_reversed( struct Range_decoder * const rdec, { int model = 1; int symbol = 0; - for( int i = 0; i < num_bits; ++i ) + int i; + for( i = 0; i < num_bits; ++i ) { const int bit = Rd_decode_bit( rdec, &bm[model] ); model <<= 1; @@ -159,7 +163,8 @@ static inline int Rd_decode_matched( struct Range_decoder * const rdec, { Bit_model * const bm1 = bm + 0x100; int symbol = 1; - for( int i = 7; i >= 0; --i ) + int i; + for( i = 7; i >= 0; --i ) { const int match_bit = ( match_byte >> i ) & 1; const int bit = Rd_decode_bit( rdec, &bm1[(match_bit<<8)+symbol] ); @@ -186,15 +191,16 @@ struct Len_decoder static inline void Led_init( struct Len_decoder * const len_decoder ) { + int i, j; Bm_init( &len_decoder->choice1 ); Bm_init( &len_decoder->choice2 ); - for( int i = 0; i < pos_states; ++i ) - for( int j = 0; j < len_low_symbols; ++j ) + for( i = 0; i < pos_states; ++i ) + for( j = 0; j < len_low_symbols; ++j ) Bm_init( &len_decoder->bm_low[i][j] ); - for( int i = 0; i < pos_states; ++i ) - for( int j = 0; j < len_mid_symbols; ++j ) + for( i = 0; i < pos_states; ++i ) + for( j = 0; j < len_mid_symbols; ++j ) Bm_init( &len_decoder->bm_mid[i][j] ); - for( int i = 0; i < len_high_symbols; ++i ) + for( i = 0; i < len_high_symbols; ++i ) Bm_init( &len_decoder->bm_high[i] ); } @@ -219,8 +225,9 @@ struct Literal_decoder static inline void Lid_init( struct Literal_decoder * const literal_decoder ) { - for( int i = 0; i < 1<<literal_context_bits; ++i ) - for( int j = 0; j < 0x300; ++j ) + int i, j; + for( i = 0; i < 1<<literal_context_bits; ++i ) + for( j = 0; j < 0x300; ++j ) Bm_init( &literal_decoder->bm_literal[i][j] ); } @@ -242,23 +249,23 @@ static inline uint8_t Lid_decode_matched( struct Literal_decoder * const literal struct LZ_decoder { long long partial_data_pos; - int member_version; int dictionary_size; int buffer_size; - uint8_t * buffer; - int pos; - int stream_pos; // first byte not yet written to file + uint8_t * buffer; /* output buffer */ + int pos; /* current pos in buffer */ + int stream_pos; /* first byte not yet written to file */ uint32_t crc_; - int outfd_; // output file descriptor - - Bit_model bm_match[St_states][pos_states]; - Bit_model bm_rep[St_states]; - Bit_model bm_rep0[St_states]; - Bit_model bm_rep1[St_states]; - Bit_model bm_rep2[St_states]; - Bit_model bm_len[St_states][pos_states]; + int outfd; /* output file descriptor */ + int member_version; + + Bit_model bm_match[states][pos_states]; + Bit_model bm_rep[states]; + Bit_model bm_rep0[states]; + Bit_model bm_rep1[states]; + Bit_model bm_rep2[states]; + Bit_model bm_len[states][pos_states]; Bit_model bm_dis_slot[max_dis_states][1<<dis_slot_bits]; - Bit_model bm_dis[modeled_distances-end_dis_model]; + Bit_model bm_dis[modeled_distances-end_dis_model+1]; Bit_model bm_align[dis_align_size]; struct Range_decoder * range_decoder; @@ -269,6 +276,16 @@ struct LZ_decoder void LZd_flush_data( struct LZ_decoder * const decoder ); +bool LZd_verify_trailer( struct LZ_decoder * const decoder, + struct Pretty_print * const pp ); + +static inline uint8_t LZd_get_prev_byte( struct LZ_decoder * const decoder ) + { + const int i = + ( ( decoder->pos > 0 ) ? decoder->pos : decoder->buffer_size ) - 1; + return decoder->buffer[i]; + } + static inline uint8_t LZd_get_byte( struct LZ_decoder * const decoder, const int distance ) { @@ -281,8 +298,7 @@ static inline void LZd_put_byte( struct LZ_decoder * const decoder, const uint8_t b ) { decoder->buffer[decoder->pos] = b; - if( ++decoder->pos >= decoder->buffer_size ) - LZd_flush_data( decoder ); + if( ++decoder->pos >= decoder->buffer_size ) LZd_flush_data( decoder ); } static inline void LZd_copy_block( struct LZ_decoder * const decoder, @@ -299,37 +315,34 @@ static inline void LZd_copy_block( struct LZ_decoder * const decoder, else for( ; len > 0 ; --len ) { decoder->buffer[decoder->pos] = decoder->buffer[i]; - if( ++decoder->pos >= decoder->buffer_size ) - LZd_flush_data( decoder ); + if( ++decoder->pos >= decoder->buffer_size ) LZd_flush_data( decoder ); if( ++i >= decoder->buffer_size ) i = 0; } } -bool LZd_verify_trailer( struct LZ_decoder * const decoder, - struct Pretty_print * const pp ); - static inline void LZd_init( struct LZ_decoder * const decoder, const File_header header, - struct Range_decoder * const rdec, const int outfd ) + struct Range_decoder * const rdec, const int ofd ) { + int i, j; decoder->partial_data_pos = 0; - decoder->member_version = Fh_version( header ); 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 ) { - show_error( "not enough memory. Find a machine with more memory", 0, false ); + show_error( "Not enough memory. Find a machine with more memory.", 0, false ); cleanup_and_fail( 1 ); } decoder->pos = 0; decoder->stream_pos = 0; decoder->crc_ = 0xFFFFFFFFU; - decoder->outfd_ = outfd; + decoder->outfd = ofd; + decoder->member_version = Fh_version( header ); - for( int i = 0; i < St_states; ++i ) + for( i = 0; i < states; ++i ) { - for( int j = 0; j < pos_states; ++j ) + for( j = 0; j < pos_states; ++j ) { Bm_init( &decoder->bm_match[i][j] ); Bm_init( &decoder->bm_len[i][j] ); @@ -339,19 +352,19 @@ static inline void LZd_init( struct LZ_decoder * const decoder, Bm_init( &decoder->bm_rep1[i] ); Bm_init( &decoder->bm_rep2[i] ); } - for( int i = 0; i < max_dis_states; ++i ) - for( int j = 0; j < 1<<dis_slot_bits; ++j ) + for( i = 0; i < max_dis_states; ++i ) + for( j = 0; j < 1<<dis_slot_bits; ++j ) Bm_init( &decoder->bm_dis_slot[i][j] ); - for( int i = 0; i < modeled_distances-end_dis_model; ++i ) + for( i = 0; i < modeled_distances-end_dis_model+1; ++i ) Bm_init( &decoder->bm_dis[i] ); - for( int i = 0; i < dis_align_size; ++i ) + for( i = 0; i < dis_align_size; ++i ) Bm_init( &decoder->bm_align[i] ); decoder->range_decoder = rdec; Led_init( &decoder->len_decoder ); Led_init( &decoder->rep_match_len_decoder ); Lid_init( &decoder->literal_decoder ); - decoder->buffer[decoder->buffer_size-1] = 0; // prev_byte of first_byte + decoder->buffer[decoder->buffer_size-1] = 0; /* prev_byte of first_byte */ } static inline void LZd_free( struct LZ_decoder * const decoder ) @@ -360,11 +373,8 @@ static inline void LZd_free( struct LZ_decoder * const decoder ) static inline uint32_t LZd_crc( struct LZ_decoder * const decoder ) { return decoder->crc_ ^ 0xFFFFFFFFU; } -int LZd_decode_member( struct LZ_decoder * const decoder, - struct Pretty_print * const pp ); - -static inline long long LZd_member_position( struct LZ_decoder * const decoder ) - { return Rd_member_position( decoder->range_decoder ); } - static inline long long LZd_data_position( struct LZ_decoder * const decoder ) { return decoder->partial_data_pos + decoder->pos; } + +int LZd_decode_member( struct LZ_decoder * const decoder, + struct Pretty_print * const pp ); |