diff options
Diffstat (limited to '')
-rw-r--r-- | decoder.c | 120 |
1 files changed, 50 insertions, 70 deletions
@@ -1,41 +1,35 @@ /* Lzlib - Compression library for the lzip format - Copyright (C) 2009-2016 Antonio Diaz Diaz. + Copyright (C) 2009-2017 Antonio Diaz Diaz. - This library is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 2 of the License, or - (at your option) any later version. + This library is free software. Redistribution and use in source and + binary forms, with or without modification, are permitted provided + that the following conditions are met: - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. - You should have received a copy of the GNU General Public License - along with this library. If not, see <http://www.gnu.org/licenses/>. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. - As a special exception, you may use this file as part of a free - software library without restriction. Specifically, if other files - instantiate templates or use macros or inline functions from this - file, or you compile this file and link it with other files to - produce an executable, this file does not by itself cause the - resulting executable to be covered by the GNU General Public - License. This exception does not however invalidate any other - reasons why the executable file might be covered by the GNU General - Public License. + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ -static bool LZd_verify_trailer( struct LZ_decoder * const d ) +static int LZd_try_verify_trailer( struct LZ_decoder * const d ) { File_trailer trailer; - int size = Rd_read_data( d->rdec, trailer, Ft_size ); - - if( size < Ft_size ) - return false; + if( Rd_available_bytes( d->rdec ) < Ft_size && !d->rdec->at_stream_end ) + return 0; + d->verify_trailer_pending = false; + d->member_finished = true; - return ( Ft_get_data_crc( trailer ) == LZd_crc( d ) && - Ft_get_data_size( trailer ) == LZd_data_position( d ) && - Ft_get_member_size( trailer ) == d->rdec->member_position ); + if( Rd_read_data( d->rdec, trailer, Ft_size ) == Ft_size && + Ft_get_data_crc( trailer ) == LZd_crc( d ) && + Ft_get_data_size( trailer ) == LZd_data_position( d ) && + Ft_get_member_size( trailer ) == d->rdec->member_position ) return 0; + return 3; } @@ -46,24 +40,17 @@ static int LZd_decode_member( struct LZ_decoder * const d ) { struct Range_decoder * const rdec = d->rdec; State * const state = &d->state; -/* unsigned long long old_mpos = d->rdec->member_position; */ + /* unsigned old_mpos = d->rdec->member_position; */ if( d->member_finished ) return 0; if( !Rd_try_reload( rdec, false ) ) { if( !rdec->at_stream_end ) return 0; else return 2; } - if( d->verify_trailer_pending ) - { - if( Rd_available_bytes( rdec ) < Ft_size && !rdec->at_stream_end ) - return 0; - d->verify_trailer_pending = false; - d->member_finished = true; - if( LZd_verify_trailer( d ) ) return 0; else return 3; - } + if( d->verify_trailer_pending ) return LZd_try_verify_trailer( d ); while( !Rd_finished( rdec ) ) { const int pos_state = LZd_data_position( d ) & pos_state_mask; -/* const unsigned long long mpos = d->rdec->member_position; + /* const unsigned mpos = d->rdec->member_position; if( mpos - old_mpos > rd_min_available_bytes ) return 5; old_mpos = mpos; */ if( !Rd_enough_available_bytes( rdec ) ) /* check unexpected eof */ @@ -71,19 +58,16 @@ static int LZd_decode_member( struct LZ_decoder * const d ) if( !LZd_enough_free_bytes( d ) ) return 0; if( Rd_decode_bit( rdec, &d->bm_match[*state][pos_state] ) == 0 ) /* 1st bit */ { - const uint8_t prev_byte = LZd_peek_prev( d ); + Bit_model * const bm = d->bm_literal[get_lit_state(LZd_peek_prev( d ))]; if( St_is_char( *state ) ) { *state -= ( *state < 4 ) ? *state : 3; - LZd_put_byte( d, Rd_decode_tree( rdec, - d->bm_literal[get_lit_state(prev_byte)], 8 ) ); + LZd_put_byte( d, Rd_decode_tree8( rdec, bm ) ); } else { *state -= ( *state < 10 ) ? 3 : 6; - LZd_put_byte( d, Rd_decode_matched( rdec, - d->bm_literal[get_lit_state(prev_byte)], - LZd_peek( d, d->rep0 ) ) ); + LZd_put_byte( d, Rd_decode_matched( rdec, bm, LZd_peek( d, d->rep0 ) ) ); } } else /* match or repeated match */ @@ -91,7 +75,13 @@ static int LZd_decode_member( struct LZ_decoder * const d ) int len; if( Rd_decode_bit( rdec, &d->bm_rep[*state] ) != 0 ) /* 2nd bit */ { - if( Rd_decode_bit( rdec, &d->bm_rep0[*state] ) != 0 ) /* 3rd bit */ + if( Rd_decode_bit( rdec, &d->bm_rep0[*state] ) == 0 ) /* 3rd bit */ + { + if( Rd_decode_bit( rdec, &d->bm_len[*state][pos_state] ) == 0 ) /* 4th bit */ + { *state = St_set_short_rep( *state ); + LZd_put_byte( d, LZd_peek( d, d->rep0 ) ); continue; } + } + else { unsigned distance; if( Rd_decode_bit( rdec, &d->bm_rep1[*state] ) == 0 ) /* 4th bit */ @@ -107,43 +97,34 @@ static int LZd_decode_member( struct LZ_decoder * const d ) d->rep1 = d->rep0; d->rep0 = distance; } - else - { - if( Rd_decode_bit( rdec, &d->bm_len[*state][pos_state] ) == 0 ) /* 4th bit */ - { *state = St_set_short_rep( *state ); - LZd_put_byte( d, LZd_peek( d, d->rep0 ) ); continue; } - } *state = St_set_rep( *state ); len = min_match_len + Rd_decode_len( rdec, &d->rep_len_model, pos_state ); } else /* match */ { - const unsigned rep0_saved = d->rep0; - int dis_slot; + unsigned distance; len = min_match_len + Rd_decode_len( rdec, &d->match_len_model, pos_state ); - dis_slot = Rd_decode_tree6( rdec, d->bm_dis_slot[get_len_state(len)] ); - if( dis_slot < start_dis_model ) d->rep0 = dis_slot; - else + distance = Rd_decode_tree6( rdec, d->bm_dis_slot[get_len_state(len)] ); + if( distance >= start_dis_model ) { + const unsigned dis_slot = distance; const int direct_bits = ( dis_slot >> 1 ) - 1; - d->rep0 = ( 2 | ( dis_slot & 1 ) ) << direct_bits; + distance = ( 2 | ( dis_slot & 1 ) ) << direct_bits; if( dis_slot < end_dis_model ) - d->rep0 += Rd_decode_tree_reversed( rdec, - d->bm_dis + d->rep0 - dis_slot - 1, direct_bits ); + distance += Rd_decode_tree_reversed( rdec, + d->bm_dis + ( distance - dis_slot ), direct_bits ); else { - d->rep0 += Rd_decode( rdec, direct_bits - dis_align_bits ) << dis_align_bits; - d->rep0 += Rd_decode_tree_reversed4( rdec, d->bm_align ); - if( d->rep0 == 0xFFFFFFFFU ) /* marker found */ + distance += + Rd_decode( rdec, direct_bits - dis_align_bits ) << dis_align_bits; + distance += Rd_decode_tree_reversed4( rdec, d->bm_align ); + if( distance == 0xFFFFFFFFU ) /* marker found */ { - d->rep0 = rep0_saved; Rd_normalize( rdec ); if( len == min_match_len ) /* End Of Stream marker */ { - if( Rd_available_bytes( rdec ) < Ft_size && !rdec->at_stream_end ) - { d->verify_trailer_pending = true; return 0; } - d->member_finished = true; - if( LZd_verify_trailer( d ) ) return 0; else return 3; + d->verify_trailer_pending = true; + return LZd_try_verify_trailer( d ); } if( len == min_match_len + 1 ) /* Sync Flush marker */ { @@ -154,11 +135,10 @@ static int LZd_decode_member( struct LZ_decoder * const d ) } } } - d->rep3 = d->rep2; d->rep2 = d->rep1; d->rep1 = rep0_saved; + d->rep3 = d->rep2; d->rep2 = d->rep1; d->rep1 = d->rep0; d->rep0 = distance; *state = St_set_match( *state ); if( d->rep0 >= d->dictionary_size || - ( d->rep0 >= d->cb.put && !d->pos_wrapped ) ) - return 1; + ( d->rep0 >= d->cb.put && !d->pos_wrapped ) ) return 1; } LZd_copy_block( d, d->rep0, len ); } |