1
1
use crate :: decode:: lzbuffer:: { LzBuffer , LzCircularBuffer } ;
2
- use crate :: decode:: rangecoder;
3
- use crate :: decode:: rangecoder:: RangeDecoder ;
4
- use crate :: decompress:: Options ;
5
- use crate :: decompress:: UnpackedSize ;
2
+ use crate :: decode:: rangecoder:: { BitTree , LenDecoder , RangeDecoder } ;
3
+ use crate :: decompress:: { Options , UnpackedSize } ;
6
4
use crate :: error;
5
+ use crate :: util:: vec2d:: Vec2D ;
7
6
use byteorder:: { LittleEndian , ReadBytesExt } ;
8
7
use std:: io;
9
8
@@ -167,9 +166,9 @@ pub(crate) struct DecoderState {
167
166
partial_input_buf : std:: io:: Cursor < [ u8 ; MAX_REQUIRED_INPUT ] > ,
168
167
pub ( crate ) lzma_props : LzmaProperties ,
169
168
unpacked_size : Option < u64 > ,
170
- literal_probs : Vec < Vec < u16 > > ,
171
- pos_slot_decoder : Vec < rangecoder :: BitTree > ,
172
- align_decoder : rangecoder :: BitTree ,
169
+ literal_probs : Vec2D < u16 > ,
170
+ pos_slot_decoder : [ BitTree ; 4 ] ,
171
+ align_decoder : BitTree ,
173
172
pos_decoders : [ u16 ; 115 ] ,
174
173
is_match : [ u16 ; 192 ] , // true = LZ, false = literal
175
174
is_rep : [ u16 ; 12 ] ,
@@ -179,8 +178,8 @@ pub(crate) struct DecoderState {
179
178
is_rep_0long : [ u16 ; 192 ] ,
180
179
state : usize ,
181
180
rep : [ usize ; 4 ] ,
182
- len_decoder : rangecoder :: LenDecoder ,
183
- rep_len_decoder : rangecoder :: LenDecoder ,
181
+ len_decoder : LenDecoder ,
182
+ rep_len_decoder : LenDecoder ,
184
183
}
185
184
186
185
impl DecoderState {
@@ -190,9 +189,14 @@ impl DecoderState {
190
189
partial_input_buf : std:: io:: Cursor :: new ( [ 0 ; MAX_REQUIRED_INPUT ] ) ,
191
190
lzma_props,
192
191
unpacked_size,
193
- literal_probs : vec ! [ vec![ 0x400 ; 0x300 ] ; 1 << ( lzma_props. lc + lzma_props. lp) ] ,
194
- pos_slot_decoder : vec ! [ rangecoder:: BitTree :: new( 6 ) ; 4 ] ,
195
- align_decoder : rangecoder:: BitTree :: new ( 4 ) ,
192
+ literal_probs : Vec2D :: init ( 0x400 , ( 1 << ( lzma_props. lc + lzma_props. lp ) , 0x300 ) ) ,
193
+ pos_slot_decoder : [
194
+ BitTree :: new ( 6 ) ,
195
+ BitTree :: new ( 6 ) ,
196
+ BitTree :: new ( 6 ) ,
197
+ BitTree :: new ( 6 ) ,
198
+ ] ,
199
+ align_decoder : BitTree :: new ( 4 ) ,
196
200
pos_decoders : [ 0x400 ; 115 ] ,
197
201
is_match : [ 0x400 ; 192 ] ,
198
202
is_rep : [ 0x400 ; 12 ] ,
@@ -202,33 +206,34 @@ impl DecoderState {
202
206
is_rep_0long : [ 0x400 ; 192 ] ,
203
207
state : 0 ,
204
208
rep : [ 0 ; 4 ] ,
205
- len_decoder : rangecoder :: LenDecoder :: new ( ) ,
206
- rep_len_decoder : rangecoder :: LenDecoder :: new ( ) ,
209
+ len_decoder : LenDecoder :: new ( ) ,
210
+ rep_len_decoder : LenDecoder :: new ( ) ,
207
211
}
208
212
}
209
213
210
214
pub fn reset_state ( & mut self , new_props : LzmaProperties ) {
211
215
new_props. validate ( ) ;
212
216
if self . lzma_props . lc + self . lzma_props . lp == new_props. lc + new_props. lp {
213
217
// We can reset here by filling the existing buffer with 0x400.
214
- self . literal_probs . iter_mut ( ) . for_each ( |v| v . fill ( 0x400 ) )
218
+ self . literal_probs . fill ( 0x400 ) ;
215
219
} else {
216
220
// We need to reallocate because of the new size of `lc+lp`.
217
- self . literal_probs = vec ! [ vec! [ 0x400 ; 0x300 ] ; 1 << ( new_props. lc + new_props. lp) ] ;
221
+ self . literal_probs = Vec2D :: init ( 0x400 , ( 1 << ( new_props. lc + new_props. lp ) , 0x300 ) ) ;
218
222
}
219
223
220
224
self . lzma_props = new_props;
221
225
self . pos_slot_decoder . iter_mut ( ) . for_each ( |t| t. reset ( ) ) ;
222
226
self . align_decoder . reset ( ) ;
223
- self . pos_decoders . fill ( 0x400 ) ;
224
- self . is_match . fill ( 0x400 ) ;
225
- self . is_rep . fill ( 0x400 ) ;
226
- self . is_rep_g0 . fill ( 0x400 ) ;
227
- self . is_rep_g1 . fill ( 0x400 ) ;
228
- self . is_rep_g2 . fill ( 0x400 ) ;
229
- self . is_rep_0long . fill ( 0x400 ) ;
227
+ self . pos_decoders = [ 0x400 ; 115 ] ;
228
+ self . is_match = [ 0x400 ; 192 ] ;
229
+ self . is_rep = [ 0x400 ; 12 ] ;
230
+ self . is_rep_g0 = [ 0x400 ; 12 ] ;
231
+ self . is_rep_g1 = [ 0x400 ; 12 ] ;
232
+ self . is_rep_g2 = [ 0x400 ; 12 ] ;
233
+ self . is_rep_0long = [ 0x400 ; 192 ] ;
234
+ self . state = 0 ;
235
+ self . rep = [ 0 ; 4 ] ;
230
236
self . state = 0 ;
231
- self . rep . fill ( 0 ) ;
232
237
self . len_decoder . reset ( ) ;
233
238
self . rep_len_decoder . reset ( ) ;
234
239
}
@@ -240,7 +245,7 @@ impl DecoderState {
240
245
pub fn process < ' a , W : io:: Write , LZB : LzBuffer < W > , R : io:: BufRead > (
241
246
& mut self ,
242
247
output : & mut LZB ,
243
- rangecoder : & mut rangecoder :: RangeDecoder < ' a , R > ,
248
+ rangecoder : & mut RangeDecoder < ' a , R > ,
244
249
) -> error:: Result < ( ) > {
245
250
self . process_mode ( output, rangecoder, ProcessingMode :: Finish )
246
251
}
@@ -249,7 +254,7 @@ impl DecoderState {
249
254
pub fn process_stream < ' a , W : io:: Write , LZB : LzBuffer < W > , R : io:: BufRead > (
250
255
& mut self ,
251
256
output : & mut LZB ,
252
- rangecoder : & mut rangecoder :: RangeDecoder < ' a , R > ,
257
+ rangecoder : & mut RangeDecoder < ' a , R > ,
253
258
) -> error:: Result < ( ) > {
254
259
self . process_mode ( output, rangecoder, ProcessingMode :: Partial )
255
260
}
@@ -263,7 +268,7 @@ impl DecoderState {
263
268
fn process_next_inner < ' a , W : io:: Write , LZB : LzBuffer < W > , R : io:: BufRead > (
264
269
& mut self ,
265
270
output : & mut LZB ,
266
- rangecoder : & mut rangecoder :: RangeDecoder < ' a , R > ,
271
+ rangecoder : & mut RangeDecoder < ' a , R > ,
267
272
update : bool ,
268
273
) -> error:: Result < ProcessingStatus > {
269
274
let pos_state = output. len ( ) & ( ( 1 << self . lzma_props . pb ) - 1 ) ;
@@ -380,7 +385,7 @@ impl DecoderState {
380
385
fn process_next < ' a , W : io:: Write , LZB : LzBuffer < W > , R : io:: BufRead > (
381
386
& mut self ,
382
387
output : & mut LZB ,
383
- rangecoder : & mut rangecoder :: RangeDecoder < ' a , R > ,
388
+ rangecoder : & mut RangeDecoder < ' a , R > ,
384
389
) -> error:: Result < ProcessingStatus > {
385
390
self . process_next_inner ( output, rangecoder, true )
386
391
}
@@ -398,15 +403,15 @@ impl DecoderState {
398
403
code : u32 ,
399
404
) -> error:: Result < ( ) > {
400
405
let mut temp = std:: io:: Cursor :: new ( buf) ;
401
- let mut rangecoder = rangecoder :: RangeDecoder :: from_parts ( & mut temp, range, code) ;
406
+ let mut rangecoder = RangeDecoder :: from_parts ( & mut temp, range, code) ;
402
407
let _ = self . process_next_inner ( output, & mut rangecoder, false ) ?;
403
408
Ok ( ( ) )
404
409
}
405
410
406
411
/// Utility function to read data into the partial input buffer.
407
412
fn read_partial_input_buf < ' a , R : io:: BufRead > (
408
413
& mut self ,
409
- rangecoder : & mut rangecoder :: RangeDecoder < ' a , R > ,
414
+ rangecoder : & mut RangeDecoder < ' a , R > ,
410
415
) -> error:: Result < ( ) > {
411
416
// Fill as much of the tmp buffer as possible
412
417
let start = self . partial_input_buf . position ( ) as usize ;
@@ -420,7 +425,7 @@ impl DecoderState {
420
425
fn process_mode < ' a , W : io:: Write , LZB : LzBuffer < W > , R : io:: BufRead > (
421
426
& mut self ,
422
427
output : & mut LZB ,
423
- rangecoder : & mut rangecoder :: RangeDecoder < ' a , R > ,
428
+ rangecoder : & mut RangeDecoder < ' a , R > ,
424
429
mode : ProcessingMode ,
425
430
) -> error:: Result < ( ) > {
426
431
loop {
@@ -461,11 +466,8 @@ impl DecoderState {
461
466
// Run the decompressor on the tmp buffer
462
467
let mut tmp_reader =
463
468
io:: Cursor :: new ( & tmp[ ..self . partial_input_buf . position ( ) as usize ] ) ;
464
- let mut tmp_rangecoder = rangecoder:: RangeDecoder :: from_parts (
465
- & mut tmp_reader,
466
- rangecoder. range ,
467
- rangecoder. code ,
468
- ) ;
469
+ let mut tmp_rangecoder =
470
+ RangeDecoder :: from_parts ( & mut tmp_reader, rangecoder. range , rangecoder. code ) ;
469
471
let res = self . process_next ( output, & mut tmp_rangecoder) ?;
470
472
471
473
// Update the actual rangecoder
@@ -514,7 +516,7 @@ impl DecoderState {
514
516
fn decode_literal < ' a , W : io:: Write , LZB : LzBuffer < W > , R : io:: BufRead > (
515
517
& mut self ,
516
518
output : & mut LZB ,
517
- rangecoder : & mut rangecoder :: RangeDecoder < ' a , R > ,
519
+ rangecoder : & mut RangeDecoder < ' a , R > ,
518
520
update : bool ,
519
521
) -> error:: Result < u8 > {
520
522
let def_prev_byte = 0u8 ;
@@ -550,7 +552,7 @@ impl DecoderState {
550
552
551
553
fn decode_distance < ' a , R : io:: BufRead > (
552
554
& mut self ,
553
- rangecoder : & mut rangecoder :: RangeDecoder < ' a , R > ,
555
+ rangecoder : & mut RangeDecoder < ' a , R > ,
554
556
length : usize ,
555
557
update : bool ,
556
558
) -> error:: Result < usize > {
0 commit comments