Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ rust-version = "1.82" # MSRV
libc = "0.2"
libbz2-rs-sys = { path = "libbz2-rs-sys/", default-features = false }

[profile.relwithdebinfo]
inherits = "release"
debug = true

[package]
name = "libbzip2-rs"
readme.workspace = true
Expand Down
10 changes: 5 additions & 5 deletions libbz2-rs-sys/src/bzlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,9 +541,9 @@ pub(crate) struct DState {
pub origPtr: i32,
pub tPos: u32,
pub nblock_used: i32,
pub unzftab: [i32; 256],
pub cftab: [i32; 257],
pub cftabCopy: [i32; 257],
pub unzftab: [u32; 256],
pub cftab: [u32; 257],
pub cftabCopy: [u32; 257],
pub tt: DSlice<u32>,
pub ll16: DSlice<u16>,
pub ll4: DSlice<u8>,
Expand Down Expand Up @@ -1484,7 +1484,7 @@ fn un_rle_obuf_to_output_fast(strm: &mut BzStream<DState>, s: &mut DState) -> bo
}

#[inline]
pub(crate) fn index_into_f(index: i32, cftab: &[i32; 257]) -> u8 {
pub(crate) fn index_into_f(index: u32, cftab: &[u32; 257]) -> u8 {
let mut nb = 0u16;
let mut na = 256;
loop {
Expand Down Expand Up @@ -1516,7 +1516,7 @@ macro_rules! BZ_GET_SMALL {
None => return true,
Some(&low_bits) => {
let high_bits = GET_LL4!($s, $s.tPos);
let tmp = index_into_f($s.tPos as i32, &$s.cftab);
let tmp = index_into_f($s.tPos, &$s.cftab);
$s.tPos = u32::from(low_bits) | high_bits << 16;
tmp
}
Expand Down
141 changes: 59 additions & 82 deletions libbz2-rs-sys/src/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,6 @@
}
use Block::*;

fn make_maps_d(s: &mut DState) {
s.nInUse = 0;
for (i, in_use) in s.inUse.iter().enumerate() {
if *in_use {
s.seqToUnseq[s.nInUse as usize] = i as u8;
s.nInUse += 1;
}
}
}

pub(crate) fn decompress(
strm: &mut BzStream<DState>,
s: &mut DState,
Expand Down Expand Up @@ -600,9 +590,9 @@
}

// mutable because they need to be reborrowed
let mut tt = s.tt.as_mut_slice();
let mut ll16 = s.ll16.as_mut_slice();
let mut ll4 = s.ll4.as_mut_slice();
let tt = s.tt.as_mut_slice();
let ll16 = s.ll16.as_mut_slice();
let ll4 = s.ll4.as_mut_slice();

'c_10064: loop {
match current_block {
Expand Down Expand Up @@ -752,75 +742,59 @@
current_block = Block24;
}
}
match current_block {
Block24 => {
if zn > 20 {

macro_rules! get_next_sym {
($next_block:ident) => {
if (zn > 20/* the longest code */) {
error!(BZ_DATA_ERROR);
} else if zvec <= s.limit[usize::from(gSel)][zn as usize] {
let index = zvec - s.base[usize::from(gSel)][zn as usize];
nextSym = match s.perm[usize::from(gSel)].get(index as usize) {
match s.perm[usize::from(gSel)].get(index as usize) {
Some(&nextSym) => nextSym,
None => error!(BZ_DATA_ERROR),
};
}
} else {
zn += 1;
current_block = BZ_X_MTF_6;
continue;
current_block = $next_block;
continue 'c_10064;
}
};
}

match current_block {
Block24 => {
nextSym = get_next_sym!(BZ_X_MTF_6);
current_block = Block40;
}
Block52 => {
if zn > 20 {
error!(BZ_DATA_ERROR);
} else if zvec <= s.limit[usize::from(gSel)][zn as usize] {
let index = zvec - s.base[usize::from(gSel)][zn as usize];
nextSym = match s.perm[usize::from(gSel)].get(index as usize) {
Some(&nextSym) => nextSym,
None => error!(BZ_DATA_ERROR),
};
if nextSym == BZ_RUNA || nextSym == BZ_RUNB {
current_block = Block46;
} else {
let uc = s.seqToUnseq[s.mtfa[s.mtfbase[0_usize] as usize] as usize];
s.unzftab[usize::from(uc)] += es as i32;
match s.smallDecompress {
DecompressMode::Small => {
match ll16.get_mut(nblock as usize..(nblock + es) as usize) {
Some(slice) => slice.fill(u16::from(uc)),
None => error!(BZ_DATA_ERROR),
};
nblock += es;
}
DecompressMode::Fast => {
match tt.get_mut(nblock as usize..(nblock + es) as usize) {
Some(slice) => slice.fill(u32::from(uc)),
None => error!(BZ_DATA_ERROR),
};
nblock += es;
}
nextSym = get_next_sym!(BZ_X_MTF_4);

if nextSym == BZ_RUNA || nextSym == BZ_RUNB {
current_block = Block46;
} else {
let uc = s.seqToUnseq[usize::from(s.mtfa[usize::from(s.mtfbase[0])])];
s.unzftab[usize::from(uc)] += es;
match s.smallDecompress {
DecompressMode::Small => {
match ll16.get_mut(nblock as usize..(nblock + es) as usize) {
Some(slice) => slice.fill(u16::from(uc)),
None => error!(BZ_DATA_ERROR),

Check warning on line 781 in libbz2-rs-sys/src/decompress.rs

View check run for this annotation

Codecov / codecov/patch

libbz2-rs-sys/src/decompress.rs#L781

Added line #L781 was not covered by tests
};
nblock += es;
}
DecompressMode::Fast => {
match tt.get_mut(nblock as usize..(nblock + es) as usize) {
Some(slice) => slice.fill(u32::from(uc)),
None => error!(BZ_DATA_ERROR),

Check warning on line 788 in libbz2-rs-sys/src/decompress.rs

View check run for this annotation

Codecov / codecov/patch

libbz2-rs-sys/src/decompress.rs#L788

Added line #L788 was not covered by tests
};
nblock += es;
}
current_block = Block40;
}
} else {
zn += 1;
current_block = BZ_X_MTF_4;
continue;
current_block = Block40;
}
}
Block56 => {
if zn > 20 {
error!(BZ_DATA_ERROR);
} else if zvec <= s.limit[usize::from(gSel)][zn as usize] {
let index = zvec - s.base[usize::from(gSel)][zn as usize];
nextSym = match s.perm[usize::from(gSel)].get(index as usize) {
Some(&nextSym) => nextSym,
None => error!(BZ_DATA_ERROR),
};
} else {
zn += 1;
current_block = BZ_X_MTF_2;
continue;
}
nextSym = get_next_sym!(BZ_X_MTF_2);
current_block = Block40;
}
_ => {}
Expand All @@ -836,10 +810,11 @@
error!(BZ_DATA_ERROR);
} else {
let uc = usize::from(initialize_mtfa(&mut s.mtfa, &mut s.mtfbase, nextSym));
s.unzftab[usize::from(s.seqToUnseq[uc])] += 1;
let index = s.seqToUnseq[uc];
s.unzftab[usize::from(index)] += 1;
match s.smallDecompress {
DecompressMode::Small => ll16[nblock as usize] = s.seqToUnseq[uc] as u16,
DecompressMode::Fast => tt[nblock as usize] = s.seqToUnseq[uc] as u32,
DecompressMode::Small => ll16[nblock as usize] = u16::from(index),
DecompressMode::Fast => tt[nblock as usize] = u32::from(index),
}
nblock += 1;
update_group_pos!(s);
Expand All @@ -853,15 +828,15 @@
if s.origPtr < 0 || s.origPtr >= nblock as i32 {
error!(BZ_DATA_ERROR);
} else {
if s.unzftab.iter().any(|e| !(0..=nblock as i32).contains(e)) {
if s.unzftab.iter().any(|e| !(0..=nblock).contains(e)) {
error!(BZ_DATA_ERROR);
}
s.cftab[0] = 0;
s.cftab[1..].copy_from_slice(&s.unzftab);
for i in 1..s.cftab.len() {
s.cftab[i] += s.cftab[i - 1];
}
if s.cftab.iter().any(|e| !(0..=nblock as i32).contains(e)) {
if s.cftab.iter().any(|e| !(0..=nblock).contains(e)) {
error!(BZ_DATA_ERROR);
}
// FIXME: use https://doc.rust-lang.org/std/primitive.slice.html#method.is_sorted
Expand All @@ -870,8 +845,8 @@
error!(BZ_DATA_ERROR);
}
s.state_out_len = 0;
s.state_out_ch = 0_u8;
s.calculatedBlockCRC = 0xffffffffu32;
s.state_out_ch = 0;
s.calculatedBlockCRC = u32::MAX;
s.state = State::BZ_X_OUTPUT;
if s.verbosity >= 2 {
debug_log!("rt+rld");
Expand Down Expand Up @@ -929,7 +904,7 @@
s.tPos = s.origPtr as u32;
s.nblock_used = 0;

s.k0 = index_into_f(s.tPos as i32, &s.cftab);
s.k0 = index_into_f(s.tPos, &s.cftab);
s.tPos = match ll16.get(s.tPos as usize) {
None => error!(BZ_DATA_ERROR),
Some(&low_bits) => {
Expand Down Expand Up @@ -1033,19 +1008,21 @@
}
}
Block18 => {
if i < 16 {
if s.inUse16[i as usize] {
if let Some(&in_use) = s.inUse16.get(i as usize) {
if in_use {
j = 0;
current_block = Block28;
continue;
}
} else {
make_maps_d(s);

// reborrow
tt = s.tt.as_mut_slice();
ll16 = s.ll16.as_mut_slice();
ll4 = s.ll4.as_mut_slice();
// inlined `make_maps_d`
s.nInUse = 0;
for (i, in_use) in s.inUse.iter().enumerate() {
if *in_use {
s.seqToUnseq[usize::from(s.nInUse)] = i as u8;
s.nInUse += 1;
}
}

if s.nInUse == 0 {
current_block = Block11;
Expand Down
Loading