Skip to content

Commit

Permalink
Fully support building in no_std configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Nov 12, 2024
1 parent dee3698 commit 4747688
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 33 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ jobs:
tool: cargo-nextest
- name: cargo build
run: cargo build --target ${{matrix.target}} ${{ matrix.features }}
- name: cargo build (no_std)
run: cargo rustc --target ${{matrix.target}} -p libbzip2-rs-sys --lib --no-default-features --crate-type rlib
env:
RUSTFLAGS: -Aunused_variables -Aunused_assignments
- name: cargo nextest # reports segfaults in a helpful way
run: cargo nextest run --target ${{matrix.target}} ${{ matrix.features }} --no-fail-fast --workspace
env:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ libbzip2-rs-sys = { path = "libbzip2-rs-sys/", default-features = false }

[dependencies]
libc = "0.2"
libbzip2-rs-sys = { path = "libbzip2-rs-sys/", default-features = false }
libbzip2-rs-sys = { path = "libbzip2-rs-sys/" }

[dev-dependencies]
tempfile = "3.13.0"
Expand Down
30 changes: 20 additions & 10 deletions blocksort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ fn fallbackSort(
initial fmap and initial BH bits.
--*/
if verb >= 4 {
eprintln!(" bucket sorting ...");
#[cfg(feature = "std")]
std::eprintln!(" bucket sorting ...");
}

{
Expand Down Expand Up @@ -284,7 +285,8 @@ fn fallbackSort(
H = 1;
loop {
if verb >= 4 {
eprint!(" depth {:>6} has ", H);
#[cfg(feature = "std")]
std::eprint!(" depth {:>6} has ", H);
}
j = 0;
i = 0;
Expand Down Expand Up @@ -352,7 +354,8 @@ fn fallbackSort(
}
}
if verb >= 4 {
eprintln!("{:>6} unresolved strings", nNotDone);
#[cfg(feature = "std")]
std::eprintln!("{:>6} unresolved strings", nNotDone);
}
H *= 2;
if H > nblock || nNotDone == 0 {
Expand All @@ -361,7 +364,8 @@ fn fallbackSort(
}

if verb >= 4 {
eprintln!(" reconstructing block ...");
#[cfg(feature = "std")]
std::eprintln!(" reconstructing block ...");
}

{
Expand Down Expand Up @@ -959,7 +963,8 @@ fn mainSort(
let mut numQSorted: i32;
let mut s: u16;
if verb >= 4 as c_int {
eprintln!(" main sort initialise ...");
#[cfg(feature = "std")]
std::eprintln!(" main sort initialise ...");
}

/*-- set up the 2-byte frequency table --*/
Expand Down Expand Up @@ -994,7 +999,8 @@ fn mainSort(
}

if verb >= 4 as c_int {
eprintln!(" bucket sorting ...");
#[cfg(feature = "std")]
std::eprintln!(" bucket sorting ...");
}

/*-- Complete the initial radix sort --*/
Expand Down Expand Up @@ -1114,7 +1120,8 @@ fn mainSort(

if hi > lo {
if verb >= 4 as c_int {
eprintln!(
#[cfg(feature = "std")]
std::eprintln!(
" qsort [{:#x}, {:#x}] done {} this {}",
ss,
j,
Expand Down Expand Up @@ -1258,7 +1265,8 @@ fn mainSort(
}
}
if verb >= 4 as c_int {
eprintln!(
#[cfg(feature = "std")]
std::eprintln!(
" {} pointers, {} sorted, {} scanned",
nblock,
numQSorted,
Expand Down Expand Up @@ -1332,7 +1340,8 @@ fn BZ2_blockSortHelp(
);

if verbosity >= 3 {
eprintln!(
#[cfg(feature = "std")]
std::eprintln!(
" {} work, {} block, ratio {:5.2}",
budgetInit - budget,
nblock,
Expand All @@ -1342,7 +1351,8 @@ fn BZ2_blockSortHelp(

if budget < 0 {
if verbosity >= 2 as c_int {
eprintln!(" too repetitive; using fallback sorting algorithm");
#[cfg(feature = "std")]
std::eprintln!(" too repetitive; using fallback sorting algorithm");
}

fallbackSort(ptr, arr2, ftab, nblock as i32, verbosity);
Expand Down
15 changes: 10 additions & 5 deletions bzlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1648,13 +1648,16 @@ unsafe fn BZ2_bzDecompressHelp(strm: &mut bz_stream) -> ReturnCode {
if s.nblock_used == s.save_nblock + 1 && s.state_out_len == 0 {
s.calculatedBlockCRC = !s.calculatedBlockCRC;
if s.verbosity >= 3 {
eprint!(
#[cfg(feature = "std")]
std::eprint!(
" {{{:#08x}, {:#08x}}}",
s.storedBlockCRC, s.calculatedBlockCRC,
s.storedBlockCRC,
s.calculatedBlockCRC,
);
}
if s.verbosity >= 2 {
eprint!("]");
#[cfg(feature = "std")]
std::eprint!("]");
}
if s.calculatedBlockCRC != s.storedBlockCRC {
return ReturnCode::BZ_DATA_ERROR;
Expand All @@ -1672,9 +1675,11 @@ unsafe fn BZ2_bzDecompressHelp(strm: &mut bz_stream) -> ReturnCode {
_ => match BZ2_decompress(strm, s) {
ReturnCode::BZ_STREAM_END => {
if s.verbosity >= 3 {
eprint!(
#[cfg(feature = "std")]
std::eprint!(
"\n combined CRCs: stored = {:#08x}, computed = {:#08x}",
s.storedCombinedCRC, s.calculatedCombinedCRC,
s.storedCombinedCRC,
s.calculatedCombinedCRC,
);
}
if s.calculatedCombinedCRC != s.storedCombinedCRC {
Expand Down
11 changes: 10 additions & 1 deletion c2rust-lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#![no_std]
#![allow(non_snake_case)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::missing_safety_doc)] // FIXME remove once everything has safety docs
#![allow(clippy::needless_range_loop)] // FIXME remove once all instances are fixed

//! A drop-in compatible rust implementation of bzip2

#[cfg(feature = "std")]
extern crate std;

use core::ffi::c_int;

mod blocksort;
Expand Down Expand Up @@ -71,8 +75,13 @@ pub(crate) use libbzip2_rs_sys_version;
macro_rules! assert_h {
($condition:expr, $errcode:expr) => {{
if !$condition {
eprint!("{}", $crate::AssertFail($errcode));
#[cfg(feature = "std")]
std::eprint!("{}", $crate::AssertFail($errcode));
#[cfg(feature = "std")]
std::process::exit(3);

#[cfg(not(feature = "std"))]
panic!("{}", $crate::AssertFail($errcode));
}
}};
}
Expand Down
42 changes: 29 additions & 13 deletions compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,12 @@ fn sendMTFValues(s: &mut EState) {
let mtfv = s.arr1.mtfv();

if s.verbosity >= 3 {
eprintln!(
#[cfg(feature = "std")]
std::eprintln!(
" {} in block, {} after MTF & 1-2 coding, {}+2 syms in use",
s.nblock, s.nMTF, s.nInUse,
s.nblock,
s.nMTF,
s.nInUse,
);
}

Expand Down Expand Up @@ -283,7 +286,8 @@ fn sendMTFValues(s: &mut EState) {
}

if s.verbosity >= 3 {
eprintln!(
#[cfg(feature = "std")]
std::eprintln!(
" initial group {}, [{} .. {}], has {} syms ({:4.1}%)",
nPart,
gs,
Expand Down Expand Up @@ -444,15 +448,18 @@ fn sendMTFValues(s: &mut EState) {
}

if s.verbosity >= 3 {
eprint!(
#[cfg(feature = "std")]
std::eprint!(
" pass {}: size is {}, grp uses are ",
iter + 1,
totc / 8,
);
for t in 0..nGroups {
eprint!("{} ", fave[t],);
#[cfg(feature = "std")]
std::eprint!("{} ", fave[t],);
}
eprintln!();
#[cfg(feature = "std")]
std::eprintln!();
}

/*--
Expand Down Expand Up @@ -532,7 +539,8 @@ fn sendMTFValues(s: &mut EState) {
}
}
if s.verbosity >= 3 {
eprint!(" bytes: mapping {}, ", writer.num_z as i32 - nBytes,);
#[cfg(feature = "std")]
std::eprint!(" bytes: mapping {}, ", writer.num_z as i32 - nBytes,);
}
}

Expand All @@ -548,7 +556,8 @@ fn sendMTFValues(s: &mut EState) {
writer.write(1, 0);
}
if s.verbosity >= 3 {
eprint!("selectors {}, ", writer.num_z as i32 - nBytes);
#[cfg(feature = "std")]
std::eprint!("selectors {}, ", writer.num_z as i32 - nBytes);
}

/*--- Now the coding tables. ---*/
Expand All @@ -570,7 +579,8 @@ fn sendMTFValues(s: &mut EState) {
}
}
if s.verbosity >= 3 {
eprint!("code lengths {}, ", writer.num_z as i32 - nBytes);
#[cfg(feature = "std")]
std::eprint!("code lengths {}, ", writer.num_z as i32 - nBytes);
}

/*--- And finally, the block data proper ---*/
Expand Down Expand Up @@ -631,7 +641,8 @@ fn sendMTFValues(s: &mut EState) {
assert_h!(selCtr == nSelectors, 3007);

if s.verbosity >= 3 {
eprintln!("codes {}", writer.num_z as i32 - nBytes);
#[cfg(feature = "std")]
std::eprintln!("codes {}", writer.num_z as i32 - nBytes);
}
}

Expand All @@ -645,9 +656,13 @@ pub fn BZ2_compressBlock(s: &mut EState, is_last_block: bool) {
}

if s.verbosity >= 2 {
eprintln!(
#[cfg(feature = "std")]
std::eprintln!(
" block {}: crc = 0x{:08x}, combined CRC = 0x{:08x}, size = {}",
s.blockNo, s.blockCRC, s.combinedCRC, s.nblock,
s.blockNo,
s.blockCRC,
s.combinedCRC,
s.nblock,
);
}

Expand Down Expand Up @@ -713,7 +728,8 @@ pub fn BZ2_compressBlock(s: &mut EState, is_last_block: bool) {
writer.write_u32(s.combinedCRC);

if s.verbosity >= 2 {
eprint!(" final combined CRC = 0x{:08x}\n ", s.combinedCRC);
#[cfg(feature = "std")]
std::eprint!(" final combined CRC = 0x{:08x}\n ", s.combinedCRC);
}

writer.finish();
Expand Down
6 changes: 4 additions & 2 deletions decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,8 @@ pub fn BZ2_decompress(strm: &mut bz_stream, s: &mut DState) -> ReturnCode {

s.currBlockNo += 1;
if s.verbosity >= 2 {
eprint!("\n [{}: huff+mtf ", s.currBlockNo);
#[cfg(feature = "std")]
std::eprint!("\n [{}: huff+mtf ", s.currBlockNo);
}
s.storedBlockCRC = 0_u32;
current_block = BZ_X_BCRC_1;
Expand Down Expand Up @@ -1062,7 +1063,8 @@ pub fn BZ2_decompress(strm: &mut bz_stream, s: &mut DState) -> ReturnCode {
s.calculatedBlockCRC = 0xffffffffu32;
s.state = State::BZ_X_OUTPUT;
if s.verbosity >= 2 {
eprint!("rt+rld");
#[cfg(feature = "std")]
std::eprint!("rt+rld");
}
match s.smallDecompress {
DecompressMode::Small => {
Expand Down
2 changes: 1 addition & 1 deletion test-libbzip2-rs-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ edition.workspace = true

[dependencies]
bzip2-sys = { version = "0.1.11", features = ["static"] }
libbzip2-rs-sys = { workspace = true, default-features = false, features = ["testing-prefix"] }
libbzip2-rs-sys = { workspace = true, default-features = true, features = ["testing-prefix"] }
libc.workspace = true

0 comments on commit 4747688

Please sign in to comment.