2025-02-20 21:31:06 +01:00
|
|
|
/* Lzlib - Compression library for the lzip format
|
2025-02-20 21:35:19 +01:00
|
|
|
Copyright (C) 2009-2025 Antonio Diaz Diaz.
|
2025-02-20 19:59:10 +01:00
|
|
|
|
2025-02-20 21:31:06 +01:00
|
|
|
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:
|
2025-02-20 19:59:10 +01:00
|
|
|
|
2025-02-20 21:31:06 +01:00
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions, and the following disclaimer.
|
2025-02-20 19:59:10 +01:00
|
|
|
|
2025-02-20 21:31:06 +01:00
|
|
|
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.
|
2025-02-20 19:59:10 +01:00
|
|
|
|
2025-02-20 21:31:06 +01:00
|
|
|
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.
|
2025-02-20 19:59:10 +01:00
|
|
|
*/
|
|
|
|
|
2025-02-20 21:34:58 +01:00
|
|
|
static int LZd_try_check_trailer( LZ_decoder * const d )
|
2025-02-20 19:59:10 +01:00
|
|
|
{
|
2025-02-20 21:28:08 +01:00
|
|
|
Lzip_trailer trailer;
|
2025-02-20 21:31:06 +01:00
|
|
|
if( Rd_available_bytes( d->rdec ) < Lt_size )
|
|
|
|
{ if( !d->rdec->at_stream_end ) return 0; else return 2; }
|
2025-02-20 21:33:45 +01:00
|
|
|
d->check_trailer_pending = false;
|
2025-02-20 21:23:49 +01:00
|
|
|
d->member_finished = true;
|
2025-02-20 19:59:10 +01:00
|
|
|
|
2025-02-20 21:28:08 +01:00
|
|
|
if( Rd_read_data( d->rdec, trailer, Lt_size ) == Lt_size &&
|
|
|
|
Lt_get_data_crc( trailer ) == LZd_crc( d ) &&
|
|
|
|
Lt_get_data_size( trailer ) == LZd_data_position( d ) &&
|
|
|
|
Lt_get_member_size( trailer ) == d->rdec->member_position ) return 0;
|
2025-02-20 21:23:49 +01:00
|
|
|
return 3;
|
2025-02-20 19:59:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Return value: 0 = OK, 1 = decoder error, 2 = unexpected EOF,
|
2025-02-20 21:21:14 +01:00
|
|
|
3 = trailer error, 4 = unknown marker found,
|
2025-02-20 21:34:33 +01:00
|
|
|
5 = nonzero first LZMA byte found, 6 = library error. */
|
2025-02-20 21:34:58 +01:00
|
|
|
static int LZd_decode_member( LZ_decoder * const d )
|
2025-02-20 19:59:10 +01:00
|
|
|
{
|
2025-02-20 21:34:58 +01:00
|
|
|
Range_decoder * const rdec = d->rdec;
|
2025-02-20 20:21:13 +01:00
|
|
|
State * const state = &d->state;
|
2025-02-20 21:34:11 +01:00
|
|
|
unsigned old_mpos = rdec->member_position;
|
2025-02-20 20:18:02 +01:00
|
|
|
|
2025-02-20 20:21:13 +01:00
|
|
|
if( d->member_finished ) return 0;
|
2025-02-20 21:34:33 +01:00
|
|
|
const int tmp = Rd_try_reload( rdec );
|
|
|
|
if( tmp > 1 ) return 5;
|
|
|
|
if( !tmp ) { if( !rdec->at_stream_end ) return 0; else return 2; }
|
2025-02-20 21:33:45 +01:00
|
|
|
if( d->check_trailer_pending ) return LZd_try_check_trailer( d );
|
2025-02-20 19:59:10 +01:00
|
|
|
|
2025-02-20 20:18:02 +01:00
|
|
|
while( !Rd_finished( rdec ) )
|
2025-02-20 19:59:10 +01:00
|
|
|
{
|
2025-02-20 21:34:11 +01:00
|
|
|
const unsigned mpos = rdec->member_position;
|
2025-02-20 21:34:33 +01:00
|
|
|
if( mpos - old_mpos > rd_min_available_bytes ) return 6;
|
2025-02-20 21:34:11 +01:00
|
|
|
old_mpos = mpos;
|
2025-02-20 21:28:08 +01:00
|
|
|
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 */
|
2025-02-20 21:21:14 +01:00
|
|
|
if( !LZd_enough_free_bytes( d ) ) return 0;
|
2025-02-20 21:32:02 +01:00
|
|
|
const int pos_state = LZd_data_position( d ) & pos_state_mask;
|
2025-02-20 21:28:08 +01:00
|
|
|
if( Rd_decode_bit( rdec, &d->bm_match[*state][pos_state] ) == 0 ) /* 1st bit */
|
2025-02-20 19:59:10 +01:00
|
|
|
{
|
2025-02-20 21:28:08 +01:00
|
|
|
/* literal byte */
|
2025-02-20 21:23:49 +01:00
|
|
|
Bit_model * const bm = d->bm_literal[get_lit_state(LZd_peek_prev( d ))];
|
2025-02-20 21:32:02 +01:00
|
|
|
if( ( *state = St_set_char( *state ) ) < 4 )
|
2025-02-20 21:23:49 +01:00
|
|
|
LZd_put_byte( d, Rd_decode_tree8( rdec, bm ) );
|
2025-02-20 19:59:10 +01:00
|
|
|
else
|
2025-02-20 21:23:49 +01:00
|
|
|
LZd_put_byte( d, Rd_decode_matched( rdec, bm, LZd_peek( d, d->rep0 ) ) );
|
2025-02-20 21:28:08 +01:00
|
|
|
continue;
|
2025-02-20 19:59:10 +01:00
|
|
|
}
|
2025-02-20 21:28:08 +01:00
|
|
|
/* match or repeated match */
|
2025-02-20 21:32:02 +01:00
|
|
|
int len;
|
2025-02-20 21:28:08 +01:00
|
|
|
if( Rd_decode_bit( rdec, &d->bm_rep[*state] ) != 0 ) /* 2nd bit */
|
2025-02-20 19:59:10 +01:00
|
|
|
{
|
2025-02-20 21:28:08 +01:00
|
|
|
if( Rd_decode_bit( rdec, &d->bm_rep0[*state] ) == 0 ) /* 3rd bit */
|
2025-02-20 19:59:10 +01:00
|
|
|
{
|
2025-02-20 21:28:08 +01:00
|
|
|
if( Rd_decode_bit( rdec, &d->bm_len[*state][pos_state] ) == 0 ) /* 4th bit */
|
2025-02-20 21:35:19 +01:00
|
|
|
{ *state = St_set_shortrep( *state );
|
2025-02-20 21:28:08 +01:00
|
|
|
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 */
|
|
|
|
distance = d->rep1;
|
2025-02-20 21:23:49 +01:00
|
|
|
else
|
2025-02-20 19:59:10 +01:00
|
|
|
{
|
2025-02-20 21:28:08 +01:00
|
|
|
if( Rd_decode_bit( rdec, &d->bm_rep2[*state] ) == 0 ) /* 5th bit */
|
|
|
|
distance = d->rep2;
|
2025-02-20 19:59:10 +01:00
|
|
|
else
|
2025-02-20 21:28:08 +01:00
|
|
|
{ distance = d->rep3; d->rep3 = d->rep2; }
|
|
|
|
d->rep2 = d->rep1;
|
2025-02-20 19:59:10 +01:00
|
|
|
}
|
2025-02-20 21:28:08 +01:00
|
|
|
d->rep1 = d->rep0;
|
|
|
|
d->rep0 = distance;
|
2025-02-20 19:59:10 +01:00
|
|
|
}
|
2025-02-20 21:28:08 +01:00
|
|
|
*state = St_set_rep( *state );
|
2025-02-20 21:32:02 +01:00
|
|
|
len = Rd_decode_len( rdec, &d->rep_len_model, pos_state );
|
2025-02-20 21:28:08 +01:00
|
|
|
}
|
|
|
|
else /* match */
|
|
|
|
{
|
2025-02-20 21:32:02 +01:00
|
|
|
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)] );
|
2025-02-20 21:28:08 +01:00
|
|
|
if( distance >= start_dis_model )
|
2025-02-20 19:59:10 +01:00
|
|
|
{
|
2025-02-20 21:28:08 +01:00
|
|
|
const unsigned dis_slot = distance;
|
|
|
|
const int direct_bits = ( dis_slot >> 1 ) - 1;
|
|
|
|
distance = ( 2 | ( dis_slot & 1 ) ) << direct_bits;
|
|
|
|
if( dis_slot < end_dis_model )
|
|
|
|
distance += Rd_decode_tree_reversed( rdec,
|
|
|
|
d->bm_dis + ( distance - dis_slot ), direct_bits );
|
|
|
|
else
|
2025-02-20 19:59:10 +01:00
|
|
|
{
|
2025-02-20 21:28:08 +01:00
|
|
|
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 */
|
2025-02-20 19:59:10 +01:00
|
|
|
{
|
2025-02-20 21:28:08 +01:00
|
|
|
Rd_normalize( rdec );
|
2025-02-20 21:34:11 +01:00
|
|
|
const unsigned mpos = rdec->member_position;
|
2025-02-20 21:34:33 +01:00
|
|
|
if( mpos - old_mpos > rd_min_available_bytes ) return 6;
|
2025-02-20 21:34:11 +01:00
|
|
|
old_mpos = mpos;
|
2025-02-20 21:28:08 +01:00
|
|
|
if( len == min_match_len ) /* End Of Stream marker */
|
|
|
|
{
|
2025-02-20 21:33:45 +01:00
|
|
|
d->check_trailer_pending = true;
|
|
|
|
return LZd_try_check_trailer( d );
|
2025-02-20 21:28:08 +01:00
|
|
|
}
|
|
|
|
if( len == min_match_len + 1 ) /* Sync Flush marker */
|
2025-02-20 19:59:10 +01:00
|
|
|
{
|
2025-02-20 21:31:06 +01:00
|
|
|
rdec->reload_pending = true;
|
2025-02-20 21:34:33 +01:00
|
|
|
const int tmp = Rd_try_reload( rdec );
|
|
|
|
if( tmp > 1 ) return 5;
|
|
|
|
if( tmp ) continue;
|
2025-02-20 21:33:45 +01:00
|
|
|
if( !rdec->at_stream_end ) return 0; else break;
|
2025-02-20 19:59:10 +01:00
|
|
|
}
|
2025-02-20 21:28:08 +01:00
|
|
|
return 4;
|
2025-02-20 19:59:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-02-20 21:28:08 +01:00
|
|
|
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;
|
2025-02-20 19:59:10 +01:00
|
|
|
}
|
2025-02-20 21:28:08 +01:00
|
|
|
LZd_copy_block( d, d->rep0, len );
|
2025-02-20 19:59:10 +01:00
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|