Skip to content

Commit 1c3ddbd

Browse files
committed
read coding tables in one go if sufficient input
1 parent d5d181b commit 1c3ddbd

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

libbz2-rs-sys/src/decompress.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,9 +1009,40 @@ pub(crate) fn decompress(
10091009
s.selector[i] = pos[0];
10101010
}
10111011

1012-
t = 0;
1013-
current_block = Block35;
1014-
break;
1012+
// try to read the coding tables in one go if there is sufficient input
1013+
let bits_needed =
1014+
usize::from(nGroups) * (5 + (usize::from(alphaSize) * 2));
1015+
let bytes_needed = bits_needed.div_ceil(8);
1016+
1017+
if strm.avail_in as usize >= bytes_needed {
1018+
for t in 0..nGroups {
1019+
let mut curr = GET_BITS!(strm, s, 5);
1020+
for i in 0..alphaSize {
1021+
loop {
1022+
if !(1..=20).contains(&curr) {
1023+
error!(BZ_DATA_ERROR);
1024+
}
1025+
if !GET_BIT!(strm, s) {
1026+
break;
1027+
};
1028+
match GET_BIT!(strm, s) {
1029+
false => curr += 1,
1030+
true => curr -= 1,
1031+
}
1032+
}
1033+
1034+
s.len[usize::from(t)][usize::from(i)] = curr as u8;
1035+
}
1036+
}
1037+
1038+
t = nGroups;
1039+
current_block = Block35;
1040+
break;
1041+
} else {
1042+
t = 0;
1043+
current_block = Block35;
1044+
break;
1045+
}
10151046
}
10161047
}
10171048
Block18 => {

0 commit comments

Comments
 (0)