diff options
Diffstat (limited to 'decoder.c')
-rw-r--r-- | decoder.c | 75 |
1 files changed, 36 insertions, 39 deletions
@@ -1,28 +1,28 @@ -/* Lzlib - Compression library for the lzip format - Copyright (C) 2009-2019 Antonio Diaz Diaz. +/* Lzlib - Compression library for the lzip format + Copyright (C) 2009-2024 Antonio Diaz Diaz. - 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 free software. Redistribution and use in source and + binary forms, with or without modification, are permitted provided + that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions, and the following disclaimer. - 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. + 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. - 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. + 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 int LZd_try_verify_trailer( struct LZ_decoder * const d ) +static int LZd_try_check_trailer( struct LZ_decoder * const d ) { Lzip_trailer trailer; - if( Rd_available_bytes( d->rdec ) < Lt_size && !d->rdec->at_stream_end ) - return 0; - d->verify_trailer_pending = false; + if( Rd_available_bytes( d->rdec ) < Lt_size ) + { if( !d->rdec->at_stream_end ) return 0; else return 2; } + d->check_trailer_pending = false; d->member_finished = true; if( Rd_read_data( d->rdec, trailer, Lt_size ) == Lt_size && @@ -40,41 +40,35 @@ static int LZd_decode_member( struct LZ_decoder * const d ) { struct Range_decoder * const rdec = d->rdec; State * const state = &d->state; - /* unsigned old_mpos = d->rdec->member_position; */ + unsigned old_mpos = rdec->member_position; if( d->member_finished ) return 0; - if( !Rd_try_reload( rdec, false ) ) + if( !Rd_try_reload( rdec ) ) { if( !rdec->at_stream_end ) return 0; else return 2; } - if( d->verify_trailer_pending ) return LZd_try_verify_trailer( d ); + if( d->check_trailer_pending ) return LZd_try_check_trailer( d ); while( !Rd_finished( rdec ) ) { - int len; - const int pos_state = LZd_data_position( d ) & pos_state_mask; - /* const unsigned mpos = d->rdec->member_position; + const unsigned mpos = rdec->member_position; if( mpos - old_mpos > rd_min_available_bytes ) return 5; - old_mpos = mpos; */ + old_mpos = mpos; if( !Rd_enough_available_bytes( rdec ) ) /* check unexpected EOF */ { if( !rdec->at_stream_end ) return 0; if( Cb_empty( &rdec->cb ) ) break; } /* decode until EOF */ if( !LZd_enough_free_bytes( d ) ) return 0; + const int pos_state = LZd_data_position( d ) & pos_state_mask; if( Rd_decode_bit( rdec, &d->bm_match[*state][pos_state] ) == 0 ) /* 1st bit */ { /* literal byte */ Bit_model * const bm = d->bm_literal[get_lit_state(LZd_peek_prev( d ))]; - if( St_is_char( *state ) ) - { - *state -= ( *state < 4 ) ? *state : 3; + if( ( *state = St_set_char( *state ) ) < 4 ) LZd_put_byte( d, Rd_decode_tree8( rdec, bm ) ); - } else - { - *state -= ( *state < 10 ) ? 3 : 6; LZd_put_byte( d, Rd_decode_matched( rdec, bm, LZd_peek( d, d->rep0 ) ) ); - } continue; } /* match or repeated match */ + 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 */ @@ -100,13 +94,12 @@ static int LZd_decode_member( struct LZ_decoder * const d ) d->rep0 = distance; } *state = St_set_rep( *state ); - len = min_match_len + Rd_decode_len( rdec, &d->rep_len_model, pos_state ); + len = Rd_decode_len( rdec, &d->rep_len_model, pos_state ); } else /* match */ { - unsigned distance; - len = min_match_len + Rd_decode_len( rdec, &d->match_len_model, pos_state ); - distance = Rd_decode_tree6( rdec, d->bm_dis_slot[get_len_state(len)] ); + len = Rd_decode_len( rdec, &d->match_len_model, pos_state ); + unsigned distance = Rd_decode_tree6( rdec, d->bm_dis_slot[get_len_state(len)] ); if( distance >= start_dis_model ) { const unsigned dis_slot = distance; @@ -123,15 +116,19 @@ static int LZd_decode_member( struct LZ_decoder * const d ) if( distance == 0xFFFFFFFFU ) /* marker found */ { Rd_normalize( rdec ); + const unsigned mpos = rdec->member_position; + if( mpos - old_mpos > rd_min_available_bytes ) return 5; + old_mpos = mpos; if( len == min_match_len ) /* End Of Stream marker */ { - d->verify_trailer_pending = true; - return LZd_try_verify_trailer( d ); + d->check_trailer_pending = true; + return LZd_try_check_trailer( d ); } if( len == min_match_len + 1 ) /* Sync Flush marker */ { - if( Rd_try_reload( rdec, true ) ) { /*old_mpos += 5;*/ continue; } - else { if( !rdec->at_stream_end ) return 0; else break; } + rdec->reload_pending = true; + if( Rd_try_reload( rdec ) ) continue; + if( !rdec->at_stream_end ) return 0; else break; } return 4; } |