diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 11793551..51199120 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,7 +10,7 @@ jobs: matrix: include: - os: ubuntu-latest - rust: '1.63' # msrv + rust: '1.88' # msrv - os: ubuntu-latest rust: stable - os: ubuntu-latest @@ -59,7 +59,7 @@ jobs: run: echo LZMA_API_STATIC=1 >> $GITHUB_ENV shell: bash - name: copy Cargo.lock for msrv check - if: matrix.rust == '1.63' + if: matrix.rust == '1.85' run: | cp Cargo.lock.msrv Cargo.lock - run: | diff --git a/.github/workflows/webassembly.yml b/.github/workflows/webassembly.yml index a902a299..7a944ce3 100644 --- a/.github/workflows/webassembly.yml +++ b/.github/workflows/webassembly.yml @@ -49,7 +49,7 @@ jobs: name: Run test run: | curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash - cargo binstall -y --force wasm-bindgen-cli@0.2.115 + cargo binstall -y --force wasm-bindgen-cli@0.2.118 cargo test --locked --target ${{ matrix.target }} --features wasm --release env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # For ease GitHub API rate limits diff --git a/Cargo.lock.msrv b/Cargo.lock.msrv index 2e3800b0..e2741b0c 100644 --- a/Cargo.lock.msrv +++ b/Cargo.lock.msrv @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "aho-corasick" @@ -105,9 +105,9 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cc" -version = "1.2.57" +version = "1.2.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a0dd1ca384932ff3641c8718a02769f1698e7563dc6974ffd03346116310423" +checksum = "43c5703da9466b66a946814e1adf53ea2c90f10063b86290cc9eb67ce3478a20" dependencies = [ "find-msvc-tools", "jobserver", @@ -150,9 +150,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.6.0" +version = "4.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b193af5b67834b676abd72466a96c1024e6a6ad978a1f484bd90b85c94041351" +checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51" dependencies = [ "clap_builder", ] @@ -242,9 +242,9 @@ checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" [[package]] name = "ctest" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd36db8091f50991402feeb406738b3369ef3b78f0377cd9be8bdb6780424dd" +checksum = "5133f4aff0fba71879344d2999f9d5c953f2df01642db17f1025c909aaa68d8c" dependencies = [ "askama", "cc", @@ -364,9 +364,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.17" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" [[package]] name = "jobserver" @@ -380,9 +380,9 @@ dependencies = [ [[package]] name = "js-sys" -version = "0.3.92" +version = "0.3.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc4c90f45aa2e6eacbe8645f77fdea542ac97a494bcd117a67df9ff4d611f995" +checksum = "2964e92d1d9dc3364cae4d718d93f227e3abb088e747d92e0395bfdedf1c12ca" dependencies = [ "cfg-if", "futures-util", @@ -392,9 +392,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.183" +version = "0.2.185" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5b646652bf6661599e1da8901b3b9522896f01e736bad5f723fe7a3a27f899d" +checksum = "52ff2c0fe9bc6cb6b14a0592c2ff4fa9ceb83eea9db979b0487cd054946a2b8f" [[package]] name = "liblzma" @@ -514,9 +514,9 @@ checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" [[package]] name = "pkg-config" -version = "0.3.32" +version = "0.3.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" +checksum = "19f132c84eca552bf34cab8ec81f1c1dcc229b811638f9d283dceabe58c5569e" [[package]] name = "plotters" @@ -592,9 +592,9 @@ checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" [[package]] name = "rand" -version = "0.8.5" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +checksum = "5ca0ecfa931c29007047d1bc58e623ab12e5590e8c7cc53200d5202b69266d8a" dependencies = [ "libc", "rand_chacha", @@ -622,9 +622,9 @@ dependencies = [ [[package]] name = "rayon" -version = "1.11.0" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "368f01d005bf8fd9b1206fb6fa653e6c4a81ceb1466406b81792d87c5677a58f" +checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d" dependencies = [ "either", "rayon-core", @@ -829,9 +829,9 @@ dependencies = [ [[package]] name = "wasm-bindgen" -version = "0.2.115" +version = "0.2.118" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6523d69017b7633e396a89c5efab138161ed5aafcbc8d3e5c5a42ae38f50495a" +checksum = "0bf938a0bacb0469e83c1e148908bd7d5a6010354cf4fb73279b7447422e3a89" dependencies = [ "cfg-if", "once_cell", @@ -842,9 +842,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.65" +version = "0.4.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d1faf851e778dfa54db7cd438b70758eba9755cb47403f3496edd7c8fc212f0" +checksum = "f371d383f2fb139252e0bfac3b81b265689bf45b6874af544ffa4c975ac1ebf8" dependencies = [ "js-sys", "wasm-bindgen", @@ -852,9 +852,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.115" +version = "0.2.118" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e3a6c758eb2f701ed3d052ff5737f5bfe6614326ea7f3bbac7156192dc32e67" +checksum = "eeff24f84126c0ec2db7a449f0c2ec963c6a49efe0698c4242929da037ca28ed" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -862,9 +862,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.115" +version = "0.2.118" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "921de2737904886b52bcbb237301552d05969a6f9c40d261eb0533c8b055fedf" +checksum = "9d08065faf983b2b80a79fd87d8254c409281cf7de75fc4b773019824196c904" dependencies = [ "bumpalo", "proc-macro2", @@ -875,18 +875,18 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.115" +version = "0.2.118" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a93e946af942b58934c604527337bad9ae33ba1d5c6900bbb41c2c07c2364a93" +checksum = "5fd04d9e306f1907bd13c6361b5c6bfc7b3b3c095ed3f8a9246390f8dbdee129" dependencies = [ "unicode-ident", ] [[package]] name = "wasm-bindgen-test" -version = "0.3.65" +version = "0.3.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1138411301a026d6662dc44e7076a74dbaa76a369312275eea5dee4d7dc68c7c" +checksum = "6bb55e2540ad1c56eec35fd63e2aea15f83b11ce487fd2de9ad11578dfc047ea" dependencies = [ "async-trait", "cast", @@ -906,9 +906,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-test-macro" -version = "0.3.65" +version = "0.3.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "186ddfe8383ba7ae7927bae3bb7343fd1f03ba2dbaf1474410f0d831131c269b" +checksum = "caf0ca1bd612b988616bac1ab34c4e4290ef18f7148a1d8b7f31c150080e9295" dependencies = [ "proc-macro2", "quote", @@ -917,15 +917,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-test-shared" -version = "0.2.115" +version = "0.2.118" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f032e076ceb8d36d5921c6cef5bf447f2ca2bbd5439ce1683d68d1c99cc2be16" +checksum = "23cda5ecc67248c48d3e705d3e03e00af905769b78b9d2a1678b663b8b9d4472" [[package]] name = "web-sys" -version = "0.3.92" +version = "0.3.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84cde8507f4d7cfcb1185b8cb5890c494ffea65edbe1ba82cfd63661c805ed94" +checksum = "4f2dfbb17949fa2088e5d39408c48368947b86f7834484e87b73de55bc14d97d" dependencies = [ "js-sys", "wasm-bindgen", @@ -989,18 +989,18 @@ dependencies = [ [[package]] name = "zerocopy" -version = "0.8.42" +version = "0.8.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2578b716f8a7a858b7f02d5bd870c14bf4ddbbcf3a4c05414ba6503640505e3" +checksum = "eed437bf9d6692032087e337407a86f04cd8d6a16a37199ed57949d415bd68e9" dependencies = [ "zerocopy-derive", ] [[package]] name = "zerocopy-derive" -version = "0.8.42" +version = "0.8.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e6cc098ea4d3bd6246687de65af3f920c430e236bee1e3bf2e441463f08a02f" +checksum = "70e3cd084b1788766f53af483dd21f93881ff30d7320490ec3ef7526d203bad4" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 1f1aac90..c2e7e9dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ in-memory encoding/decoding. forked from xz2. """ categories = ["compression", "api-bindings"] edition = "2021" -rust-version = "1.63" +rust-version = "1.88" exclude = [".github/"] [workspace] @@ -51,7 +51,7 @@ wasm-bindgen-test = "0.3" default = ["xz"] xz = ["dep:xz"] xz-sys = ["dep:xz-sys"] -liblzma-sys = ["dep:liblzma-sys", "liblzma-sys/bindgen"] +liblzma-sys = ["dep:liblzma-sys"] static = ["liblzma-sys?/static"] parallel = ["xz-sys?/parallel", "liblzma-sys?/parallel", "num_cpus"] bindgen = ["liblzma-sys?/bindgen"] # only affects liblzma-sys @@ -64,3 +64,6 @@ thin-lto = ["liblzma-sys?/thin-lto"] [package.metadata.docs.rs] features = ["parallel"] + +[lints.rust] +unsafe_op_in_unsafe_fn = "allow" diff --git a/benches/backend_comparison.rs b/benches/backend_comparison.rs index bb9c6bed..41d97506 100644 --- a/benches/backend_comparison.rs +++ b/benches/backend_comparison.rs @@ -16,21 +16,22 @@ compile_error!("backend_comparison bench requires `xz`, `xz-sys`, or `liblzma-sy #[cfg(feature = "liblzma-sys")] use liblzma_sys::{ lzma_crc32, lzma_crc64, lzma_easy_buffer_encode, lzma_stream_buffer_bound, - lzma_stream_buffer_decode, LZMA_CHECK_CRC64, + lzma_stream_buffer_decode, LZMA_CHECK_CRC64, LZMA_OK, }; #[cfg(feature = "xz")] -use xz::check::{crc32_fast::lzma_crc32, crc64_fast::lzma_crc64}; -#[cfg(feature = "xz")] -use xz::common::{ - easy_buffer_encoder::lzma_easy_buffer_encode, stream_buffer_decoder::lzma_stream_buffer_decode, - stream_buffer_encoder::lzma_stream_buffer_bound, +use xz::{ + check::{crc32_fast::lzma_crc32, crc64_fast::lzma_crc64}, + common::{ + easy_buffer_encoder::lzma_easy_buffer_encode, + stream_buffer_decoder::lzma_stream_buffer_decode, + stream_buffer_encoder::lzma_stream_buffer_bound, + }, + types::{LZMA_CHECK_CRC64, LZMA_OK}, }; -#[cfg(feature = "xz")] -use xz::types::LZMA_CHECK_CRC64; #[cfg(feature = "xz-sys")] use xz_sys::{ lzma_crc32, lzma_crc64, lzma_easy_buffer_encode, lzma_stream_buffer_bound, - lzma_stream_buffer_decode, LZMA_CHECK_CRC64, + lzma_stream_buffer_decode, LZMA_CHECK_CRC64, LZMA_OK, }; #[cfg(feature = "xz")] @@ -56,7 +57,7 @@ unsafe fn backend_encode(input: &[u8]) -> Vec { let bound = lzma_stream_buffer_bound(input.len()); let mut out = vec![0u8; bound]; let mut out_pos: usize = 0; - lzma_easy_buffer_encode( + let ret = lzma_easy_buffer_encode( 6, LZMA_CHECK_CRC64, ptr::null(), @@ -66,6 +67,7 @@ unsafe fn backend_encode(input: &[u8]) -> Vec { &mut out_pos, out.len(), ); + assert_eq!(ret, LZMA_OK, "{BACKEND_NAME} encode failed with {ret}"); out.truncate(out_pos); out } @@ -75,7 +77,7 @@ unsafe fn backend_decode(compressed: &[u8], out_size: usize) -> Vec { let mut memlimit = u64::MAX; let mut in_pos = 0usize; let mut out_pos = 0usize; - lzma_stream_buffer_decode( + let ret = lzma_stream_buffer_decode( &mut memlimit, 0, ptr::null(), @@ -86,6 +88,17 @@ unsafe fn backend_decode(compressed: &[u8], out_size: usize) -> Vec { &mut out_pos, out.len(), ); + assert_eq!(ret, LZMA_OK, "{BACKEND_NAME} decode failed with {ret}"); + assert_eq!( + in_pos, + compressed.len(), + "{BACKEND_NAME} decode left trailing input: consumed {in_pos} of {} bytes", + compressed.len() + ); + assert_eq!( + out_pos, out_size, + "{BACKEND_NAME} decode produced {out_pos} bytes, expected {out_size}" + ); out.truncate(out_pos); out } diff --git a/docs/performance-workflow.md b/docs/performance-workflow.md index 398138dc..f809c317 100644 --- a/docs/performance-workflow.md +++ b/docs/performance-workflow.md @@ -34,6 +34,17 @@ Use `hyperfine` to compare end-to-end wall clock time of the deterministic root scripts/compare_backends.sh --runs 10 --warmup 2 ``` +For the full no-regression gate across the standard root, systest, focused, and +API workloads, use the trimmed comparison wrapper: + +```bash +scripts/compare_all_trimmed.sh +``` + +It measures each Rust/C pair five times, discards the fastest and slowest runs, +averages the remaining runs, and exits with failure if Rust is slower than C +after trimming. Reports are written under `target/perf-results/trimmed/`. + This writes: - `target/perf-results/root-tests.json` @@ -47,23 +58,27 @@ The root bundle intentionally skips QuickCheck-based unit tests because they gen ## 3. Compare focused workloads -Use `perf-probe`, a small standalone binary crate that links exactly one backend at a time. The comparison scripts use the direct `xz` backend by default; `xz-sys` remains available for ABI-shell checks. +Use `perf-probe`, a small standalone binary crate that links exactly one backend at a time. `scripts/compare_workloads.sh` compares all three backends in separate processes: direct `xz`, `xz-sys`, and vendored C `liblzma-sys`. Examples: ```bash scripts/compare_workloads.sh encode --input-kind random --size 1048576 --iters 20 --warmup 3 scripts/compare_workloads.sh decode --input-kind random --size 1048576 --iters 50 --warmup 5 -scripts/compare_workloads.sh size --input-kind random --size 1048576 --iters 400 --warmup 40 +scripts/compare_workloads.sh size --input-kind random --size 1048576 --iters 2000000 --warmup 200000 scripts/compare_workloads.sh crc64 --size 16777216 --iters 400 --warmup 20 scripts/compare_workloads.sh crc64 --size 1048576 --chunk-size 16 --iters 400 --warmup 40 ``` This writes workload-specific `hyperfine` reports under `target/perf-results/`. -For tiny inputs, increase `--iters` until one benchmarked command takes at least around a -second. Otherwise process startup noise can dominate the comparison even when the actual -backend work is near parity. +For tiny inputs, increase `--iters` until one benchmarked command takes at least a few +hundred milliseconds. Otherwise process startup noise can dominate the comparison even when +the actual backend work is near parity. The `size` workload is especially small, so it +still needs millions of in-process iterations; start around `--iters 2000000 --warmup +200000` for a 1 MiB random input and scale up only if the results remain noisy. The +comparison scripts also pre-generate its compressed input so the one-time encode setup +doesn't hide the `uncompressed_size()` path. There is still a criterion bench in [`benches/backend_comparison.rs`](../benches/backend_comparison.rs), but it now measures one backend per run. Use it only with exactly one backend feature enabled. @@ -71,7 +86,7 @@ For high-level API regressions, compare the root crate against the upstream XZ c ```bash scripts/compare_api_workloads.sh standard-files --mode all --iters 200 --warmup 20 -scripts/compare_api_workloads.sh standard-files --mode good --iters 400 --warmup 40 +scripts/compare_api_workloads.sh standard-files --mode good --iters 1000 --warmup 100 scripts/compare_api_workloads.sh standard-files --mode good --name-pattern delta --iters 400 --warmup 40 scripts/compare_api_workloads.sh qc --mode both --cases 128 --max-size 4096 --iters 200 --warmup 20 scripts/compare_api_workloads.sh bufread-trailing --mode both --input-size 1024 --trailing-size 123 --iters 1000 --warmup 100 @@ -82,6 +97,11 @@ This uses [`examples/standard_files_probe.rs`](../examples/standard_files_probe. - `target/perf-results/api-standard-files.json` - `target/perf-results/api-standard-files.md` +The `good` subset is small enough that a few hundred iterations can still leave +too much wall-clock noise. Prefer around `--iters 1000 --warmup 100` when +comparing `--mode good` so the in-process work dominates process scheduling +variance. + The `qc` workload uses [`examples/qc_probe.rs`](../examples/qc_probe.rs) to reproduce the small-input repeated round-trip pattern from the root crate tests. This is useful when overall regressions show up in `root-tests` even though large encode/decode probes look diff --git a/liblzma-sys/Cargo.toml b/liblzma-sys/Cargo.toml index b10d5702..b67d91d4 100644 --- a/liblzma-sys/Cargo.toml +++ b/liblzma-sys/Cargo.toml @@ -17,7 +17,7 @@ High level Rust bindings are available in the `liblzma` crate. """ categories = ["external-ffi-bindings"] edition = "2021" -rust-version = "1.63" +rust-version = "1.88" exclude = ["xz/.github/", "xz/tests/files/*"] [dependencies] diff --git a/liblzma-sys/src/bindgen.rs b/liblzma-sys/src/bindgen.rs index 292cb145..2ffa7c5a 100644 --- a/liblzma-sys/src/bindgen.rs +++ b/liblzma-sys/src/bindgen.rs @@ -502,7 +502,7 @@ pub type lzma_delta_type = ::std::os::raw::c_uint; #[repr(C)] #[derive(Debug, Copy, Clone)] pub struct lzma_options_delta { - pub type_: lzma_delta_type, + pub r#type: lzma_delta_type, pub dist: u32, pub reserved_int1: u32, pub reserved_int2: u32, @@ -526,9 +526,9 @@ fn bindgen_test_layout_lzma_options_delta() { "Alignment of lzma_options_delta" ); assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).type_) as usize - ptr as usize }, + unsafe { ::std::ptr::addr_of!((*ptr).r#type) as usize - ptr as usize }, 0usize, - "Offset of field: lzma_options_delta::type_" + "Offset of field: lzma_options_delta::type" ); assert_eq!( unsafe { ::std::ptr::addr_of!((*ptr).dist) as usize - ptr as usize }, diff --git a/liblzma-sys/src/manual.rs b/liblzma-sys/src/manual.rs index cbe1fb3a..52bfa7e7 100644 --- a/liblzma-sys/src/manual.rs +++ b/liblzma-sys/src/manual.rs @@ -75,6 +75,7 @@ pub const LZMA_PB_DEFAULT: u32 = 2; pub const LZMA_BACKWARD_SIZE_MIN: lzma_vli = 4; pub const LZMA_BACKWARD_SIZE_MAX: lzma_vli = 1 << 34; +pub const LZMA_STREAM_HEADER_SIZE: u32 = 12; pub const LZMA_VLI_MAX: lzma_vli = u64::MAX / 2; pub const LZMA_VLI_UNKNOWN: lzma_vli = u64::MAX; @@ -99,6 +100,7 @@ pub struct lzma_allocator { pub opaque: *mut c_void, } +pub enum lzma_index {} pub enum lzma_internal {} #[repr(C)] @@ -344,6 +346,17 @@ extern "C" { b: *const lzma_stream_flags, ) -> lzma_ret; + pub fn lzma_index_buffer_decode( + i: *mut *mut lzma_index, + memlimit: *mut u64, + allocator: *const lzma_allocator, + input: *const u8, + in_pos: *mut size_t, + in_size: size_t, + ) -> lzma_ret; + pub fn lzma_index_uncompressed_size(i: *const lzma_index) -> lzma_vli; + pub fn lzma_index_end(i: *mut lzma_index, allocator: *const lzma_allocator); + pub fn lzma_version_number() -> u32; pub fn lzma_version_string() -> *const c_char; diff --git a/perf-probe/Cargo.toml b/perf-probe/Cargo.toml index 9cde3646..81b80ccb 100644 --- a/perf-probe/Cargo.toml +++ b/perf-probe/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "perf-probe" version = "0.1.0" -edition = "2021" +edition = "2024" publish = false [features] diff --git a/perf-probe/src/main.rs b/perf-probe/src/main.rs index 37cd579e..d57c5ad4 100644 --- a/perf-probe/src/main.rs +++ b/perf-probe/src/main.rs @@ -18,10 +18,10 @@ use std::time::{Duration, Instant}; #[cfg(feature = "liblzma-sys")] use liblzma_sys::{ - lzma_crc32, lzma_crc64, lzma_easy_buffer_encode, lzma_index as BackendIndex, - lzma_index_buffer_decode, lzma_index_end, lzma_index_uncompressed_size, - lzma_stream_buffer_bound, lzma_stream_buffer_decode, lzma_stream_flags as BackendStreamFlags, - lzma_stream_footer_decode, LZMA_CHECK_CRC64, LZMA_OK, LZMA_STREAM_HEADER_SIZE, + LZMA_CHECK_CRC64, LZMA_OK, LZMA_STREAM_HEADER_SIZE, lzma_crc32, lzma_crc64, + lzma_easy_buffer_encode, lzma_index as BackendIndex, lzma_index_buffer_decode, lzma_index_end, + lzma_index_uncompressed_size, lzma_stream_buffer_bound, lzma_stream_buffer_decode, + lzma_stream_flags as BackendStreamFlags, lzma_stream_footer_decode, }; #[cfg(feature = "xz")] use xz::check::{crc32_fast::lzma_crc32, crc64_fast::lzma_crc64}; @@ -36,15 +36,15 @@ use xz::common::{ }; #[cfg(feature = "xz")] use xz::types::{ - lzma_index as BackendIndex, lzma_stream_flags as BackendStreamFlags, LZMA_CHECK_CRC64, LZMA_OK, - LZMA_STREAM_HEADER_SIZE, + LZMA_CHECK_CRC64, LZMA_OK, LZMA_STREAM_HEADER_SIZE, lzma_index as BackendIndex, + lzma_stream_flags as BackendStreamFlags, }; #[cfg(feature = "xz-sys")] use xz_sys::{ - lzma_crc32, lzma_crc64, lzma_easy_buffer_encode, lzma_index as BackendIndex, - lzma_index_buffer_decode, lzma_index_end, lzma_index_uncompressed_size, - lzma_stream_buffer_bound, lzma_stream_buffer_decode, lzma_stream_flags as BackendStreamFlags, - lzma_stream_footer_decode, LZMA_CHECK_CRC64, LZMA_OK, LZMA_STREAM_HEADER_SIZE, + LZMA_CHECK_CRC64, LZMA_OK, LZMA_STREAM_HEADER_SIZE, lzma_crc32, lzma_crc64, + lzma_easy_buffer_encode, lzma_index as BackendIndex, lzma_index_buffer_decode, lzma_index_end, + lzma_index_uncompressed_size, lzma_stream_buffer_bound, lzma_stream_buffer_decode, + lzma_stream_flags as BackendStreamFlags, lzma_stream_footer_decode, }; #[cfg(feature = "xz")] @@ -225,7 +225,7 @@ fn usage() -> String { let mut message = String::new(); message.push_str("Usage:\n"); message.push_str( - " cargo run -p perf-probe --release --no-default-features --features -- \\\n", + " cargo run -p perf-probe --release --no-default-features --features -- \\\n", ); message.push_str(" --workload [options]\n\n"); message.push_str("Options:\n"); @@ -400,7 +400,8 @@ fn run_size(config: &Config) { ); let measurement = measure(compressed.len(), config.iters, config.warmup, || unsafe { - backend_uncompressed_size(&compressed) + let input = black_box(compressed.as_slice()); + backend_uncompressed_size(input) }); print_measurement(&measurement, config.iters); } @@ -488,7 +489,10 @@ fn fold_bytes(len: usize, data: &[u8]) -> u64 { } unsafe fn backend_encode(input: &[u8], preset: u32) -> Vec { + #[cfg(feature = "xz")] let bound = lzma_stream_buffer_bound(input.len()); + #[cfg(any(feature = "xz-sys", feature = "liblzma-sys"))] + let bound = unsafe { lzma_stream_buffer_bound(input.len()) }; let mut out = vec![0u8; bound]; let mut out_pos: usize = 0; let ret = unsafe { @@ -527,6 +531,16 @@ unsafe fn backend_decode(compressed: &[u8], out_size: usize) -> Vec { ) }; assert_eq!(ret, LZMA_OK, "{BACKEND_NAME} decode failed with {ret}"); + assert_eq!( + in_pos, + compressed.len(), + "{BACKEND_NAME} decode left trailing input: consumed {in_pos} of {} bytes", + compressed.len() + ); + assert_eq!( + out_pos, out_size, + "{BACKEND_NAME} decode produced {out_pos} bytes, expected {out_size}" + ); out.truncate(out_pos); out } diff --git a/scripts/compare_all_trimmed.sh b/scripts/compare_all_trimmed.sh new file mode 100755 index 00000000..bf3dc2b3 --- /dev/null +++ b/scripts/compare_all_trimmed.sh @@ -0,0 +1,319 @@ +#!/usr/bin/env bash +set -euo pipefail + +RUNS=5 +WARMUP=2 +ROOT_REPEATS=5 +RESULTS_DIR="target/perf-results/trimmed" +TOLERANCE="0.0" + +while [[ $# -gt 0 ]]; do + case "$1" in + --runs) + RUNS="$2" + shift 2 + ;; + --warmup) + WARMUP="$2" + shift 2 + ;; + --root-repeats) + ROOT_REPEATS="$2" + shift 2 + ;; + --results-dir) + RESULTS_DIR="$2" + shift 2 + ;; + --tolerance) + TOLERANCE="$2" + shift 2 + ;; + --help|-h) + cat <<'EOF' +Usage: scripts/compare_all_trimmed.sh [options] + +Run the standard C-vs-Rust backend comparison set. Each pair is measured +five times by default; the fastest and slowest runs are discarded and the +remaining runs are averaged. The script exits with failure if any Rust +measurement is slower than C after trimming. + +Options: + --runs Number of measured runs per pair (default: 5) + --warmup Hyperfine warmup runs per pair (default: 2) + --root-repeats Repeat root test binaries inside each timed command (default: 5) + --results-dir Output directory (default: target/perf-results/trimmed) + --tolerance Allowed Rust slowdown fraction before failure (default: 0.0) +EOF + exit 0 + ;; + *) + echo "unknown argument: $1" >&2 + exit 2 + ;; + esac +done + +if [[ "$RUNS" -lt 3 ]]; then + echo "--runs must be at least 3 so min/max can be discarded" >&2 + exit 2 +fi + +command -v hyperfine >/dev/null 2>&1 || { + echo "hyperfine is required" >&2 + exit 1 +} + +command -v python3 >/dev/null 2>&1 || { + echo "python3 is required" >&2 + exit 1 +} + +mkdir -p "$RESULTS_DIR" +SUMMARY_TSV="$RESULTS_DIR/benchmarks.tsv" +: >"$SUMMARY_TSV" + +RUST_TARGET="target/perf-rust" +C_TARGET="target/perf-c" + +PROBE_RUST_BIN="$RUST_TARGET/release/perf-probe" +PROBE_C_BIN="$C_TARGET/release/perf-probe" + +API_RUST_EXAMPLES="$RUST_TARGET/release/examples" +API_C_EXAMPLES="$C_TARGET/release/examples" + +record_pair() { + local stem="$1" + local label="$2" + local rust_name="$3" + local rust_cmd="$4" + local c_name="$5" + local c_cmd="$6" + + printf '%s\t%s\t%s\t%s\n' "$stem" "$label" "$rust_name" "$c_name" >>"$SUMMARY_TSV" + + hyperfine \ + --shell=none \ + --runs "$RUNS" \ + --warmup "$WARMUP" \ + --export-json "$RESULTS_DIR/${stem}.json" \ + --export-markdown "$RESULTS_DIR/${stem}.md" \ + --command-name "$rust_name" \ + "$rust_cmd" \ + --command-name "$c_name" \ + "$c_cmd" +} + +echo "prebuilding root test binaries..." +env CARGO_TARGET_DIR="$RUST_TARGET" \ + cargo test -p liblzma --release --no-default-features --features xz --lib --tests --no-run >/dev/null +env LZMA_API_STATIC=1 CARGO_TARGET_DIR="$C_TARGET" \ + cargo test --release --no-default-features --features liblzma-sys --lib --tests --no-run >/dev/null + +echo "prebuilding systest binaries..." +env CARGO_TARGET_DIR="$RUST_TARGET" \ + cargo test -p systest --release --no-default-features --features xz-sys --no-run >/dev/null +env LZMA_API_STATIC=1 CARGO_TARGET_DIR="$C_TARGET" \ + cargo test -p systest --release --no-default-features --features liblzma-sys --no-run >/dev/null + +echo "prebuilding focused workload probes..." +env CARGO_TARGET_DIR="$RUST_TARGET" \ + cargo build -p perf-probe --release --no-default-features --features xz >/dev/null +env LZMA_API_STATIC=1 CARGO_TARGET_DIR="$C_TARGET" \ + cargo build -p perf-probe --release --no-default-features --features liblzma-sys >/dev/null + +echo "prebuilding API workload probes..." +for example in standard_files_probe qc_probe bufread_trailing_probe; do + env CARGO_TARGET_DIR="$RUST_TARGET" \ + cargo build --example "$example" --release --no-default-features --features xz >/dev/null + env LZMA_API_STATIC=1 CARGO_TARGET_DIR="$C_TARGET" \ + cargo build --example "$example" --release --no-default-features --features liblzma-sys >/dev/null +done + +DECODE_INPUT="$RESULTS_DIR/decode-input-1048576.xz" +"$PROBE_C_BIN" \ + --workload encode \ + --input-kind random \ + --size 1048576 \ + --iters 1 \ + --warmup 0 \ + --save-output "$DECODE_INPUT" >/dev/null + +record_pair \ + root-tests \ + "root test bundle" \ + xz-tests \ + "./scripts/run_root_test_bins.sh $RUST_TARGET $ROOT_REPEATS" \ + c-tests \ + "./scripts/run_root_test_bins.sh $C_TARGET $ROOT_REPEATS" + +record_pair \ + systest \ + "systest" \ + xz-sys-systest \ + "env CARGO_TARGET_DIR=$RUST_TARGET cargo test -p systest --release --no-default-features --features xz-sys -- --test-threads=1" \ + c-systest \ + "env LZMA_API_STATIC=1 CARGO_TARGET_DIR=$C_TARGET cargo test -p systest --release --no-default-features --features liblzma-sys -- --test-threads=1" + +record_pair \ + encode \ + "focused encode random 1MiB" \ + xz \ + "$PROBE_RUST_BIN --workload encode --input-kind random --size 1048576 --iters 300 --warmup 20" \ + c \ + "$PROBE_C_BIN --workload encode --input-kind random --size 1048576 --iters 300 --warmup 20" + +record_pair \ + decode \ + "focused decode random 1MiB" \ + xz \ + "$PROBE_RUST_BIN --workload decode --compressed-input $DECODE_INPUT --expected-size 1048576 --iters 800 --warmup 80" \ + c \ + "$PROBE_C_BIN --workload decode --compressed-input $DECODE_INPUT --expected-size 1048576 --iters 800 --warmup 80" + +record_pair \ + size \ + "focused uncompressed_size random 1MiB" \ + xz \ + "$PROBE_RUST_BIN --workload size --compressed-input $DECODE_INPUT --expected-size 1048576 --iters 2000000 --warmup 200000" \ + c \ + "$PROBE_C_BIN --workload size --compressed-input $DECODE_INPUT --expected-size 1048576 --iters 2000000 --warmup 200000" + +record_pair \ + crc32 \ + "focused crc32 16MiB" \ + xz \ + "$PROBE_RUST_BIN --workload crc32 --size 16777216 --iters 300 --warmup 20" \ + c \ + "$PROBE_C_BIN --workload crc32 --size 16777216 --iters 300 --warmup 20" + +record_pair \ + crc64-large \ + "focused crc64 16MiB" \ + xz \ + "$PROBE_RUST_BIN --workload crc64 --size 16777216 --iters 400 --warmup 20" \ + c \ + "$PROBE_C_BIN --workload crc64 --size 16777216 --iters 400 --warmup 20" + +record_pair \ + crc64-smallchunk \ + "focused crc64 1MiB chunk 16" \ + xz \ + "$PROBE_RUST_BIN --workload crc64 --size 1048576 --chunk-size 16 --iters 400 --warmup 40" \ + c \ + "$PROBE_C_BIN --workload crc64 --size 1048576 --chunk-size 16 --iters 400 --warmup 40" + +record_pair \ + api-standard-files-all \ + "API standard files all" \ + xz \ + "$API_RUST_EXAMPLES/standard_files_probe --mode all --iters 200 --warmup 20" \ + c \ + "$API_C_EXAMPLES/standard_files_probe --mode all --iters 200 --warmup 20" + +record_pair \ + api-standard-files-good \ + "API standard files good" \ + xz \ + "$API_RUST_EXAMPLES/standard_files_probe --mode good --iters 1000 --warmup 100" \ + c \ + "$API_C_EXAMPLES/standard_files_probe --mode good --iters 1000 --warmup 100" + +record_pair \ + api-standard-files-delta \ + "API standard files good delta" \ + xz \ + "$API_RUST_EXAMPLES/standard_files_probe --mode good --name-pattern delta --iters 400 --warmup 40" \ + c \ + "$API_C_EXAMPLES/standard_files_probe --mode good --name-pattern delta --iters 400 --warmup 40" + +record_pair \ + api-qc \ + "API qc both" \ + xz \ + "$API_RUST_EXAMPLES/qc_probe --mode both --cases 128 --max-size 4096 --iters 200 --warmup 20" \ + c \ + "$API_C_EXAMPLES/qc_probe --mode both --cases 128 --max-size 4096 --iters 200 --warmup 20" + +record_pair \ + api-bufread-trailing \ + "API bufread trailing" \ + xz \ + "$API_RUST_EXAMPLES/bufread_trailing_probe --mode both --input-size 1024 --trailing-size 123 --iters 1000 --warmup 100" \ + c \ + "$API_C_EXAMPLES/bufread_trailing_probe --mode both --input-size 1024 --trailing-size 123 --iters 1000 --warmup 100" + +python3 - "$RESULTS_DIR" "$SUMMARY_TSV" "$TOLERANCE" <<'PY' +import json +import sys +from pathlib import Path + +results_dir = Path(sys.argv[1]) +summary_tsv = Path(sys.argv[2]) +tolerance = float(sys.argv[3]) + + +def trimmed_mean(times): + values = sorted(float(t) for t in times) + if len(values) >= 3: + values = values[1:-1] + return sum(values) / len(values) + + +rows = [] +failed = [] + +for line in summary_tsv.read_text().splitlines(): + stem, label, rust_name, c_name = line.split("\t") + data = json.loads((results_dir / f"{stem}.json").read_text()) + by_name = {entry["command"]: entry for entry in data["results"]} + rust = trimmed_mean(by_name[rust_name]["times"]) + c = trimmed_mean(by_name[c_name]["times"]) + ratio = c / rust + allowed = c * (1.0 + tolerance) + status = "PASS" if rust <= allowed else "FAIL" + if status == "FAIL": + failed.append(label) + rows.append((label, rust, c, ratio, status)) + +md = results_dir / "trimmed-summary.md" +json_path = results_dir / "trimmed-summary.json" + +with md.open("w") as f: + f.write("# Trimmed Backend Comparison\n\n") + f.write(f"- measured runs per pair: {len(by_name[rust_name]['times'])}\n") + f.write("- aggregation: discard min and max, average remaining runs\n") + f.write(f"- allowed Rust slowdown tolerance: {tolerance:.3f}\n\n") + f.write("| workload | Rust trimmed mean | C trimmed mean | C/Rust ratio | status |\n") + f.write("|---|---:|---:|---:|---|\n") + for label, rust, c, ratio, status in rows: + f.write( + f"| {label} | {rust:.6f}s | {c:.6f}s | {ratio:.3f}x | {status} |\n" + ) + +json_path.write_text( + json.dumps( + [ + { + "workload": label, + "rust_trimmed_mean_seconds": rust, + "c_trimmed_mean_seconds": c, + "c_over_rust_ratio": ratio, + "status": status, + } + for label, rust, c, ratio, status in rows + ], + indent=2, + ) + + "\n" +) + +print(f"Summary written to {md}") +print(f"Machine-readable summary written to {json_path}") + +if failed: + print("Rust was slower than C after trimming for:", file=sys.stderr) + for label in failed: + print(f" - {label}", file=sys.stderr) + sys.exit(1) +PY diff --git a/scripts/compare_api_workloads.sh b/scripts/compare_api_workloads.sh index 014f70ce..19a5cdc7 100755 --- a/scripts/compare_api_workloads.sh +++ b/scripts/compare_api_workloads.sh @@ -7,7 +7,7 @@ Usage: scripts/compare_api_workloads.sh [ex Examples: scripts/compare_api_workloads.sh standard-files --mode all --iters 200 --warmup 20 - scripts/compare_api_workloads.sh standard-files --mode good --iters 400 --warmup 40 + scripts/compare_api_workloads.sh standard-files --mode good --iters 1000 --warmup 100 scripts/compare_api_workloads.sh standard-files --mode good --name-pattern delta --iters 400 --warmup 40 scripts/compare_api_workloads.sh qc --mode both --cases 128 --max-size 4096 --iters 200 --warmup 20 scripts/compare_api_workloads.sh bufread-trailing --mode both --input-size 1024 --trailing-size 123 --iters 1000 --warmup 100 @@ -50,7 +50,7 @@ esac RUST_BIN="$RUST_TARGET/release/examples/$EXAMPLE_NAME" C_BIN="$C_TARGET/release/examples/$EXAMPLE_NAME" -env CARGO_TARGET_DIR="$RUST_TARGET" cargo build --example "$EXAMPLE_NAME" --release >/dev/null +env CARGO_TARGET_DIR="$RUST_TARGET" cargo build --example "$EXAMPLE_NAME" --release --no-default-features --features xz >/dev/null env LZMA_API_STATIC=1 CARGO_TARGET_DIR="$C_TARGET" cargo build --example "$EXAMPLE_NAME" --release --no-default-features --features liblzma-sys >/dev/null hyperfine \ diff --git a/scripts/compare_backends.sh b/scripts/compare_backends.sh index fb5628aa..f16fb023 100755 --- a/scripts/compare_backends.sh +++ b/scripts/compare_backends.sh @@ -70,7 +70,7 @@ SYSTEST_RUST_CMD="env CARGO_TARGET_DIR=$SYSTEST_RUST_TARGET cargo test -p systes SYSTEST_C_CMD="env LZMA_API_STATIC=1 CARGO_TARGET_DIR=$SYSTEST_C_TARGET cargo test -p systest --release --no-default-features --features liblzma-sys -- --test-threads=1" echo "prebuilding root test binaries..." -env CARGO_TARGET_DIR="$ROOT_RUST_TARGET" cargo test --release --no-run >/dev/null +env CARGO_TARGET_DIR="$ROOT_RUST_TARGET" cargo test -p liblzma --release --no-default-features --features xz --no-run >/dev/null env LZMA_API_STATIC=1 CARGO_TARGET_DIR="$ROOT_C_TARGET" cargo test --release --no-default-features --features liblzma-sys --no-run >/dev/null hyperfine \ diff --git a/scripts/compare_workloads.sh b/scripts/compare_workloads.sh index db7d44e5..ae162c68 100755 --- a/scripts/compare_workloads.sh +++ b/scripts/compare_workloads.sh @@ -8,7 +8,7 @@ Usage: scripts/compare_workloads.sh [perf-probe Examples: scripts/compare_workloads.sh encode --input-kind random --size 1048576 --iters 20 --warmup 3 scripts/compare_workloads.sh decode --input-kind random --size 1048576 --iters 50 --warmup 5 - scripts/compare_workloads.sh size --input-kind random --size 1048576 --iters 400 --warmup 40 + scripts/compare_workloads.sh size --input-kind random --size 1048576 --iters 2000000 --warmup 200000 EOF exit 2 fi @@ -26,47 +26,89 @@ mkdir -p "$RESULTS_DIR" RAW_ARGS=("$@") RUST_TARGET="target/perf-probe-rust" +RUST_SYS_TARGET="target/perf-probe-rust-sys" C_TARGET="target/perf-probe-c" RUST_BIN="$RUST_TARGET/release/perf-probe" +RUST_SYS_BIN="$RUST_SYS_TARGET/release/perf-probe" C_BIN="$C_TARGET/release/perf-probe" env CARGO_TARGET_DIR="$RUST_TARGET" cargo build -p perf-probe --release --no-default-features --features xz >/dev/null +env CARGO_TARGET_DIR="$RUST_SYS_TARGET" cargo build -p perf-probe --release --no-default-features --features xz-sys >/dev/null env LZMA_API_STATIC=1 CARGO_TARGET_DIR="$C_TARGET" cargo build -p perf-probe --release --no-default-features --features liblzma-sys >/dev/null -if [[ "$WORKLOAD" == "decode" ]]; then +if [[ "$WORKLOAD" == "decode" || "$WORKLOAD" == "size" ]]; then SIZE="" + COMPRESSED_INPUT="" + EXPECTED_SIZE="" + PASSTHROUGH_ARGS=() for ((i = 0; i < ${#RAW_ARGS[@]}; i++)); do - if [[ "${RAW_ARGS[$i]}" == "--size" ]]; then - SIZE="${RAW_ARGS[$((i + 1))]}" - break - fi + case "${RAW_ARGS[$i]}" in + --size) + SIZE="${RAW_ARGS[$((i + 1))]}" + ((i += 1)) + ;; + --compressed-input) + COMPRESSED_INPUT="${RAW_ARGS[$((i + 1))]}" + ((i += 1)) + ;; + --expected-size) + EXPECTED_SIZE="${RAW_ARGS[$((i + 1))]}" + ((i += 1)) + ;; + --input-kind) + ((i += 1)) + ;; + *) + PASSTHROUGH_ARGS+=("${RAW_ARGS[$i]}") + ;; + esac done - if [[ -z "$SIZE" ]]; then - echo "decode comparison currently requires --size so expected output size is known" >&2 + + if [[ -z "$COMPRESSED_INPUT" ]]; then + if [[ -z "$SIZE" ]]; then + echo "$WORKLOAD comparison currently requires --size so expected output size is known" >&2 + exit 2 + fi + + COMPRESSED_INPUT="$RESULTS_DIR/${WORKLOAD}-input-${SIZE}.xz" + "$C_BIN" \ + --workload encode "${RAW_ARGS[@]}" --iters 1 --warmup 0 --save-output "$COMPRESSED_INPUT" >/dev/null + EXPECTED_SIZE="$SIZE" + elif [[ -z "$EXPECTED_SIZE" ]]; then + echo "$WORKLOAD comparison currently requires --expected-size with --compressed-input" >&2 exit 2 fi - COMPRESSED_INPUT="$RESULTS_DIR/decode-input-${SIZE}.xz" - "$C_BIN" \ - --workload encode "${RAW_ARGS[@]}" --iters 1 --warmup 0 --save-output "$COMPRESSED_INPUT" >/dev/null - RUST_CMD=( "$RUST_BIN" - --workload decode + --workload "$WORKLOAD" --compressed-input "$COMPRESSED_INPUT" - --expected-size "$SIZE" + --expected-size "$EXPECTED_SIZE" + "${PASSTHROUGH_ARGS[@]}" + ) + RUST_SYS_CMD=( + "$RUST_SYS_BIN" + --workload "$WORKLOAD" + --compressed-input "$COMPRESSED_INPUT" + --expected-size "$EXPECTED_SIZE" + "${PASSTHROUGH_ARGS[@]}" ) C_CMD=( "$C_BIN" - --workload decode + --workload "$WORKLOAD" --compressed-input "$COMPRESSED_INPUT" - --expected-size "$SIZE" + --expected-size "$EXPECTED_SIZE" + "${PASSTHROUGH_ARGS[@]}" ) else RUST_CMD=( "$RUST_BIN" --workload "$WORKLOAD" "${RAW_ARGS[@]}" ) + RUST_SYS_CMD=( + "$RUST_SYS_BIN" + --workload "$WORKLOAD" "${RAW_ARGS[@]}" + ) C_CMD=( "$C_BIN" --workload "$WORKLOAD" "${RAW_ARGS[@]}" @@ -80,6 +122,8 @@ hyperfine \ --export-markdown "$RESULTS_DIR/${WORKLOAD}.md" \ --command-name xz \ "${RUST_CMD[*]}" \ + --command-name xz-sys \ + "${RUST_SYS_CMD[*]}" \ --command-name c \ "${C_CMD[*]}" diff --git a/scripts/profile_backend.sh b/scripts/profile_backend.sh index ee74f222..5bcab7eb 100755 --- a/scripts/profile_backend.sh +++ b/scripts/profile_backend.sh @@ -3,11 +3,10 @@ set -euo pipefail if [[ $# -lt 2 ]]; then cat <<'EOF' >&2 -Usage: scripts/profile_backend.sh [backend_probe args...] +Usage: scripts/profile_backend.sh [backend_probe args...] Examples: scripts/profile_backend.sh xz decode --size 1048576 --iters 500 --warmup 50 - scripts/profile_backend.sh both encode --input-kind random --size 8388608 scripts/profile_backend.sh xz size --input-kind random --size 1048576 --iters 800 --warmup 80 Environment: @@ -40,10 +39,6 @@ case "$BACKEND" in TARGET_DIR="target/profile-bench-xz-sys" BACKEND_ENV=() ;; - both) - echo "profile_backend.sh profiles one backend at a time; use xz, xz-sys, or liblzma-sys" >&2 - exit 2 - ;; *) echo "unknown backend: $BACKEND" >&2 exit 2 diff --git a/systest/Cargo.toml b/systest/Cargo.toml index fd393d7b..a1c35a56 100644 --- a/systest/Cargo.toml +++ b/systest/Cargo.toml @@ -12,7 +12,7 @@ xz-sys = { path = "../xz-sys", optional = true } libc = "0.2" [build-dependencies] -ctest = "0.5" +ctest = "0.5.1" [features] liblzma-sys = ["dep:liblzma-sys"] diff --git a/systest/build.rs b/systest/build.rs index 5e28d86a..227deb74 100644 --- a/systest/build.rs +++ b/systest/build.rs @@ -7,6 +7,13 @@ fn target_deps_dir() -> PathBuf { let workspace_root = manifest_dir.parent().unwrap(); let target_root = env::var_os("CARGO_TARGET_DIR") .map(PathBuf::from) + .map(|path| { + if path.is_relative() { + workspace_root.join(path) + } else { + path + } + }) .unwrap_or_else(|| workspace_root.join("target")); let profile = env::var("PROFILE").unwrap(); let target = env::var("TARGET").unwrap(); @@ -111,18 +118,13 @@ fn main() { cfg.header("lzma.h"); cfg.rename_struct_ty(|ty| Some(ty.to_string())); cfg.rename_union_ty(|ty| Some(ty.to_string())); - cfg.rename_struct_field(|s, field| { - if s.ident() == "lzma_options_delta" && field.ident() == "type_" { - Some("type".to_string()) - } else { - None - } - }); cfg.define("LZMA_API_STATIC", None); cfg.skip_struct(move |s| { - use_bindgen && (s.ident().ends_with("_s") || s.ident().contains("__bindgen_ty_")) + (use_bindgen && (s.ident().ends_with("_s") || s.ident().contains("__bindgen_ty_"))) + || (use_rs_sys && matches!(s.ident(), "StaticAllocator")) }); cfg.skip_union(move |u| use_bindgen && u.ident().contains("__bindgen_ty_")); + cfg.skip_static(move |s| use_rs_sys && matches!(s.ident(), "C_ALLOCATOR")); cfg.skip_struct_field(move |s, field| { use_bindgen && s.ident() == "lzma_index_iter" @@ -134,14 +136,15 @@ fn main() { && matches!(field.ident(), "stream" | "block" | "internal") }); cfg.skip_fn(move |f| { - use_bindgen + (use_bindgen && !use_parallel && matches!( f.ident(), "lzma_stream_decoder_mt" | "lzma_stream_encoder_mt" | "lzma_stream_encoder_mt_memusage" - ) + )) + || (use_rs_sys && matches!(f.ident(), "malloc" | "calloc" | "free")) }); cfg.skip_alias(move |n| { matches!(n.ident(), "__enum_ty" | "c_enum" | "lzma_reserved_enum") @@ -183,6 +186,5 @@ fn main() { &path_externs, ); env::set_var("RUSTC", &rustc_wrapper); - ctest::generate_test(&mut cfg, rust_api, "all.rs").unwrap(); } diff --git a/xz-sys/Cargo.toml b/xz-sys/Cargo.toml index 2ec9c90a..00329284 100644 --- a/xz-sys/Cargo.toml +++ b/xz-sys/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "xz-sys" version = "0.1.0" -edition = "2021" +edition = "2024" license = "MIT OR Apache-2.0" description = "liblzma-sys compatible API layer backed by pure Rust xz" diff --git a/xz-sys/src/c_allocator.rs b/xz-sys/src/c_allocator.rs new file mode 100644 index 00000000..9ea6b07f --- /dev/null +++ b/xz-sys/src/c_allocator.rs @@ -0,0 +1,114 @@ +#[cfg(not(all(target_family = "wasm", target_os = "unknown")))] +use libc::c_void; +#[cfg(not(all(target_family = "wasm", target_os = "unknown")))] +use libc::size_t; +#[cfg(all(target_family = "wasm", target_os = "unknown"))] +use std::alloc::{Layout, alloc, alloc_zeroed, dealloc}; +#[cfg(all(target_family = "wasm", target_os = "unknown"))] +use std::os::raw::c_void; + +use crate::{lzma_allocator, lzma_stream}; + +#[cfg(all(target_family = "wasm", target_os = "unknown"))] +const C_ALLOC_ALIGN: usize = 16; +#[cfg(all(target_family = "wasm", target_os = "unknown"))] +const C_ALLOC_HEADER_SIZE: usize = 16; + +#[cfg(not(all(target_family = "wasm", target_os = "unknown")))] +unsafe extern "C" { + fn malloc(__size: size_t) -> *mut c_void; + #[allow(dead_code)] + fn calloc(__count: size_t, __size: size_t) -> *mut c_void; + fn free(__ptr: *mut c_void); +} + +#[cfg(all(target_family = "wasm", target_os = "unknown"))] +fn c_alloc_layout(size: usize) -> Option { + let total_size = size.checked_add(C_ALLOC_HEADER_SIZE)?; + Layout::from_size_align(total_size, C_ALLOC_ALIGN).ok() +} + +#[cfg(all(target_family = "wasm", target_os = "unknown"))] +fn malloc(size: size_t) -> *mut c_void { + let Some(layout) = c_alloc_layout(size as usize) else { + return core::ptr::null_mut(); + }; + let base = unsafe { alloc(layout) }; + if base.is_null() { + return core::ptr::null_mut(); + } + unsafe { + *(base as *mut usize) = layout.size(); + base.add(C_ALLOC_HEADER_SIZE) as *mut c_void + } +} + +#[cfg(all(target_family = "wasm", target_os = "unknown"))] +fn calloc(count: size_t, size: size_t) -> *mut c_void { + let Some(size) = (count as usize).checked_mul(size as usize) else { + return core::ptr::null_mut(); + }; + let Some(layout) = c_alloc_layout(size) else { + return core::ptr::null_mut(); + }; + let base = unsafe { alloc_zeroed(layout) }; + if base.is_null() { + return core::ptr::null_mut(); + } + unsafe { + *(base as *mut usize) = layout.size(); + base.add(C_ALLOC_HEADER_SIZE) as *mut c_void + } +} + +#[cfg(all(target_family = "wasm", target_os = "unknown"))] +unsafe fn free(ptr: *mut c_void) { + if ptr.is_null() { + return; + } + let base = (ptr as *mut u8).sub(C_ALLOC_HEADER_SIZE); + let total_size = *(base as *const usize); + let layout = Layout::from_size_align_unchecked(total_size, C_ALLOC_ALIGN); + dealloc(base, layout); +} + +unsafe fn lzma_c_alloc(_opaque: *mut c_void, nmemb: size_t, size: size_t) -> *mut c_void { + let Some(size) = (nmemb as usize).checked_mul(size as usize) else { + return core::ptr::null_mut(); + }; + let size = if size == 0 { 1 } else { size }; + malloc(size as size_t) +} + +unsafe fn lzma_c_free(_opaque: *mut c_void, ptr: *mut c_void) { + free(ptr); +} + +#[repr(transparent)] +struct StaticAllocator(lzma_allocator); + +unsafe impl Sync for StaticAllocator {} + +static C_ALLOCATOR: StaticAllocator = StaticAllocator(lzma_allocator { + alloc: Some(lzma_c_alloc), + free: Some(lzma_c_free), + opaque: core::ptr::null_mut(), +}); + +fn c_allocator_ptr() -> *const lzma_allocator { + &raw const C_ALLOCATOR.0 +} + +pub(crate) fn normalize_c_allocator(allocator: *const lzma_allocator) -> *const lzma_allocator { + if allocator.is_null() { + c_allocator_ptr() + } else { + allocator + } +} + +pub(crate) unsafe fn normalize_c_stream_allocator(strm: *mut lzma_stream) { + if !strm.is_null() && (*strm).allocator.is_null() { + (*strm).allocator = c_allocator_ptr(); + } +} diff --git a/xz-sys/src/lib.rs b/xz-sys/src/lib.rs index 393a2013..0faba493 100644 --- a/xz-sys/src/lib.rs +++ b/xz-sys/src/lib.rs @@ -3,18 +3,21 @@ //! Re-exports symbols from xz with the same names and signatures //! as liblzma-sys, enabling drop-in replacement. //! -//! Because c2rust generates per-file type definitions, this layer provides -//! canonical types and thin wrapper functions that cast between structurally -//! identical `#[repr(C)]` types. - #![allow( non_camel_case_types, non_snake_case, non_upper_case_globals, + unsafe_op_in_unsafe_fn, unused_imports, clippy::all )] +//! Because c2rust generates per-file type definitions, this layer provides +//! canonical types and thin wrapper functions that cast between structurally +//! identical `#[repr(C)]` types. + +mod c_allocator; + #[cfg(not(all(target_family = "wasm", target_os = "unknown")))] use libc::size_t; #[cfg(not(all(target_family = "wasm", target_os = "unknown")))] @@ -25,6 +28,7 @@ use std::os::raw::{c_char, c_int, c_uchar, c_uint, c_void}; type wasm_size_t = usize; #[cfg(all(target_family = "wasm", target_os = "unknown"))] use self::wasm_size_t as size_t; +use crate::c_allocator::{normalize_c_allocator, normalize_c_stream_allocator}; /************************ * Canonical type aliases @@ -69,7 +73,7 @@ pub struct lzma_options_bcj { #[repr(C)] pub struct lzma_options_delta { - pub type_: lzma_delta_type, + pub r#type: lzma_delta_type, pub dist: u32, pub reserved_int1: u32, pub reserved_int2: u32, @@ -93,9 +97,9 @@ pub const LZMA_VERSION: u32 = xz::common::common::LZMA_VERSION as u32; /* `lzma/base.h`: return codes */ pub use xz::types::{ - LZMA_BUF_ERROR, LZMA_DATA_ERROR, LZMA_FORMAT_ERROR, LZMA_GET_CHECK, LZMA_MEMLIMIT_ERROR, - LZMA_MEM_ERROR, LZMA_NO_CHECK, LZMA_OK, LZMA_OPTIONS_ERROR, LZMA_PROG_ERROR, LZMA_SEEK_NEEDED, - LZMA_STREAM_END, LZMA_UNSUPPORTED_CHECK, + LZMA_BUF_ERROR, LZMA_DATA_ERROR, LZMA_FORMAT_ERROR, LZMA_GET_CHECK, LZMA_MEM_ERROR, + LZMA_MEMLIMIT_ERROR, LZMA_NO_CHECK, LZMA_OK, LZMA_OPTIONS_ERROR, LZMA_PROG_ERROR, + LZMA_SEEK_NEEDED, LZMA_STREAM_END, LZMA_UNSUPPORTED_CHECK, }; /* `lzma/base.h`: actions */ @@ -185,82 +189,89 @@ pub const LZMA_BLOCK_HEADER_SIZE_MAX: u32 = xz::types::LZMA_BLOCK_HEADER_SIZE_MA /* `lzma/version.h` */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_version_number() -> u32 { xz::common::common::lzma_version_number() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_version_string() -> *const c_char { xz::common::common::lzma_version_string() } /* `lzma/base.h`: common stream API */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_code(strm: *mut lzma_stream, action: lzma_action) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::common::lzma_code(strm.cast(), action) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_end(strm: *mut lzma_stream) { + normalize_c_stream_allocator(strm); xz::common::common::lzma_end(strm.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_memlimit_get(strm: *const lzma_stream) -> u64 { + normalize_c_stream_allocator(strm as *mut lzma_stream); xz::common::common::lzma_memlimit_get(strm.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_memlimit_set(strm: *mut lzma_stream, new_memlimit: u64) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::common::lzma_memlimit_set(strm.cast(), new_memlimit) } /* `lzma/base.h`: allocation helpers */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_alloc(size: size_t, allocator: *const lzma_allocator) -> *mut c_void { - xz::common::common::lzma_alloc(size, allocator) + xz::common::common::lzma_alloc(size, normalize_c_allocator(allocator)) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_alloc_zero( size: size_t, allocator: *const lzma_allocator, ) -> *mut c_void { - xz::common::common::lzma_alloc_zero(size, allocator) + xz::common::common::lzma_alloc_zero(size, normalize_c_allocator(allocator)) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_free(ptr: *mut c_void, allocator: *const lzma_allocator) { - xz::common::common::lzma_free(ptr, allocator) + xz::common::common::lzma_free(ptr, normalize_c_allocator(allocator)) } /* `lzma/base.h`: progress / memusage */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_get_progress( strm: *mut lzma_stream, progress_in: *mut u64, progress_out: *mut u64, ) { + normalize_c_stream_allocator(strm); xz::common::common::lzma_get_progress(strm.cast(), progress_in, progress_out) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_memusage(strm: *const lzma_stream) -> u64 { + normalize_c_stream_allocator(strm as *mut lzma_stream); xz::common::common::lzma_memusage(strm.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_get_check(strm: *const lzma_stream) -> lzma_check { + normalize_c_stream_allocator(strm as *mut lzma_stream); xz::common::common::lzma_get_check(strm.cast()) } /* `lzma/vli.h` */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_vli_encode( vli: lzma_vli, vli_pos: *mut size_t, @@ -271,7 +282,7 @@ pub unsafe extern "C" fn lzma_vli_encode( xz::common::vli_encoder::lzma_vli_encode(vli, vli_pos, out, out_pos, out_size) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_vli_decode( vli: *mut lzma_vli, vli_pos: *mut size_t, @@ -282,29 +293,29 @@ pub unsafe extern "C" fn lzma_vli_decode( xz::common::vli_decoder::lzma_vli_decode(vli, vli_pos, input, in_pos, in_size) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_vli_size(vli: lzma_vli) -> u32 { xz::common::vli_size::lzma_vli_size(vli) } /* `lzma/check.h` */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_check_is_supported(check: lzma_check) -> lzma_bool { xz::check::check::lzma_check_is_supported(check) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_check_size(check: lzma_check) -> u32 { xz::check::check::lzma_check_size(check) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_crc32(buf: *const u8, size: size_t, crc: u32) -> u32 { xz::check::crc32_fast::lzma_crc32(buf, size, crc) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_crc64(buf: *const u8, size: size_t, crc: u64) -> u64 { xz::check::crc64_fast::lzma_crc64(buf, size, crc) } @@ -315,26 +326,27 @@ pub unsafe extern "C" fn lzma_crc64(buf: *const u8, size: size_t, crc: u64) -> u /* `lzma/container.h`: easy encoder */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_easy_encoder_memusage(preset: u32) -> u64 { xz::common::easy_encoder_memusage::lzma_easy_encoder_memusage(preset) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_easy_decoder_memusage(preset: u32) -> u64 { xz::common::easy_decoder_memusage::lzma_easy_decoder_memusage(preset) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_easy_encoder( strm: *mut lzma_stream, preset: u32, check: lzma_check, ) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::easy_encoder::lzma_easy_encoder(strm.cast(), preset, check) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_easy_buffer_encode( preset: u32, check: lzma_check, @@ -348,7 +360,7 @@ pub unsafe extern "C" fn lzma_easy_buffer_encode( xz::common::easy_buffer_encoder::lzma_easy_buffer_encode( preset, check, - allocator.cast(), + normalize_c_allocator(allocator).cast(), input, in_size, out, @@ -359,67 +371,75 @@ pub unsafe extern "C" fn lzma_easy_buffer_encode( /* `lzma/container.h`: stream encoder / decoder */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_stream_encoder( strm: *mut lzma_stream, filters: *const lzma_filter, check: lzma_check, ) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::stream_encoder::lzma_stream_encoder(strm.cast(), filters.cast(), check) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_stream_decoder( strm: *mut lzma_stream, memlimit: u64, flags: u32, ) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::stream_decoder::lzma_stream_decoder(strm.cast(), memlimit, flags) } +/* `lzma/container.h`: auto decoder */ + +#[unsafe(no_mangle)] +pub unsafe extern "C" fn lzma_auto_decoder( + strm: *mut lzma_stream, + memlimit: u64, + flags: u32, +) -> lzma_ret { + normalize_c_stream_allocator(strm); + xz::common::auto_decoder::lzma_auto_decoder(strm.cast(), memlimit, flags) +} + /* `lzma/container.h`: alone encoder / decoder */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_alone_encoder( strm: *mut lzma_stream, options: *const lzma_options_lzma, ) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::alone_encoder::lzma_alone_encoder(strm.cast(), options.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_alone_decoder(strm: *mut lzma_stream, memlimit: u64) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::alone_decoder::lzma_alone_decoder(strm.cast(), memlimit) } -/* `lzma/container.h`: auto / lzip decoder */ +/* `lzma/container.h`: lzip decoder */ -#[no_mangle] -pub unsafe extern "C" fn lzma_auto_decoder( - strm: *mut lzma_stream, - memlimit: u64, - flags: u32, -) -> lzma_ret { - xz::common::auto_decoder::lzma_auto_decoder(strm.cast(), memlimit, flags) -} - -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_lzip_decoder( strm: *mut lzma_stream, memlimit: u64, flags: u32, ) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::lzip_decoder::lzma_lzip_decoder(strm.cast(), memlimit, flags) } /* `lzma/container.h`: stream buffer */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_stream_buffer_bound(uncompressed_size: size_t) -> size_t { xz::common::stream_buffer_encoder::lzma_stream_buffer_bound(uncompressed_size) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_stream_buffer_encode( filters: *mut lzma_filter, check: lzma_check, @@ -433,7 +453,7 @@ pub unsafe extern "C" fn lzma_stream_buffer_encode( xz::common::stream_buffer_encoder::lzma_stream_buffer_encode( filters.cast(), check, - allocator.cast(), + normalize_c_allocator(allocator).cast(), input, in_size, out, @@ -442,7 +462,7 @@ pub unsafe extern "C" fn lzma_stream_buffer_encode( ) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_stream_buffer_decode( memlimit: *mut u64, flags: u32, @@ -457,7 +477,7 @@ pub unsafe extern "C" fn lzma_stream_buffer_decode( xz::common::stream_buffer_decoder::lzma_stream_buffer_decode( memlimit, flags, - allocator.cast(), + normalize_c_allocator(allocator).cast(), input, in_pos, in_size, @@ -473,62 +493,69 @@ pub unsafe extern "C" fn lzma_stream_buffer_decode( /* `lzma/filter.h` */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_filter_encoder_is_supported(id: lzma_vli) -> lzma_bool { xz::common::filter_encoder::lzma_filter_encoder_is_supported(id) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_filter_decoder_is_supported(id: lzma_vli) -> lzma_bool { xz::common::filter_decoder::lzma_filter_decoder_is_supported(id) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_filters_copy( src: *const lzma_filter, dest: *mut lzma_filter, allocator: *const lzma_allocator, ) -> lzma_ret { - xz::common::filter_common::lzma_filters_copy(src.cast(), dest.cast(), allocator.cast()) + xz::common::filter_common::lzma_filters_copy( + src.cast(), + dest.cast(), + normalize_c_allocator(allocator).cast(), + ) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_raw_encoder_memusage(filters: *const lzma_filter) -> u64 { xz::common::filter_encoder::lzma_raw_encoder_memusage(filters.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_raw_decoder_memusage(filters: *const lzma_filter) -> u64 { xz::common::filter_decoder::lzma_raw_decoder_memusage(filters.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_raw_encoder( strm: *mut lzma_stream, filters: *const lzma_filter, ) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::filter_encoder::lzma_raw_encoder(strm.cast(), filters.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_raw_decoder( strm: *mut lzma_stream, filters: *const lzma_filter, ) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::filter_decoder::lzma_raw_decoder(strm.cast(), filters.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_filters_update( strm: *mut lzma_stream, filters: *const lzma_filter, ) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::filter_encoder::lzma_filters_update(strm.cast(), filters.cast()) } /* `lzma/filter.h`: raw buffer */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_raw_buffer_encode( filters: *const lzma_filter, allocator: *const lzma_allocator, @@ -540,7 +567,7 @@ pub unsafe extern "C" fn lzma_raw_buffer_encode( ) -> lzma_ret { xz::common::filter_buffer_encoder::lzma_raw_buffer_encode( filters.cast(), - allocator.cast(), + normalize_c_allocator(allocator).cast(), input, in_size, out, @@ -549,7 +576,7 @@ pub unsafe extern "C" fn lzma_raw_buffer_encode( ) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_raw_buffer_decode( filters: *const lzma_filter, allocator: *const lzma_allocator, @@ -562,7 +589,7 @@ pub unsafe extern "C" fn lzma_raw_buffer_decode( ) -> lzma_ret { xz::common::filter_buffer_decoder::lzma_raw_buffer_decode( filters.cast(), - allocator.cast(), + normalize_c_allocator(allocator).cast(), input, in_pos, in_size, @@ -574,7 +601,7 @@ pub unsafe extern "C" fn lzma_raw_buffer_decode( /* `lzma/filter.h`: properties */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_properties_size( size: *mut u32, filter: *const lzma_filter, @@ -582,7 +609,7 @@ pub unsafe extern "C" fn lzma_properties_size( xz::common::filter_encoder::lzma_properties_size(size, filter.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_properties_encode( filter: *const lzma_filter, props: *mut u8, @@ -590,7 +617,7 @@ pub unsafe extern "C" fn lzma_properties_encode( xz::common::filter_encoder::lzma_properties_encode(filter.cast(), props) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_properties_decode( filter: *mut lzma_filter, allocator: *const lzma_allocator, @@ -599,20 +626,20 @@ pub unsafe extern "C" fn lzma_properties_decode( ) -> lzma_ret { xz::common::filter_decoder::lzma_properties_decode( filter.cast(), - allocator.cast(), + normalize_c_allocator(allocator).cast(), props, props_size, ) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_mt_block_size(filters: *const lzma_filter) -> u64 { xz::common::filter_encoder::lzma_mt_block_size(filters.cast()) } /* `lzma/lzma12.h` */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_lzma_preset( options: *mut lzma_options_lzma, preset: u32, @@ -620,19 +647,19 @@ pub unsafe extern "C" fn lzma_lzma_preset( xz::lzma::lzma_encoder_presets::lzma_lzma_preset(options.cast(), preset) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_mf_is_supported(mf: lzma_match_finder) -> lzma_bool { xz::lz::lz_encoder::lzma_mf_is_supported(mf) } -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn lzma_mode_is_supported(mode: lzma_mode) -> lzma_bool { xz::lzma::lzma_encoder::lzma_mode_is_supported(mode) } /* `lzma/bcj.h` */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_bcj_arm64_encode( start_offset: u32, buf: *mut u8, @@ -641,7 +668,7 @@ pub unsafe extern "C" fn lzma_bcj_arm64_encode( xz::simple::arm64::lzma_bcj_arm64_encode(start_offset, buf, size) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_bcj_arm64_decode( start_offset: u32, buf: *mut u8, @@ -650,7 +677,7 @@ pub unsafe extern "C" fn lzma_bcj_arm64_decode( xz::simple::arm64::lzma_bcj_arm64_decode(start_offset, buf, size) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_bcj_riscv_encode( start_offset: u32, buf: *mut u8, @@ -659,7 +686,7 @@ pub unsafe extern "C" fn lzma_bcj_riscv_encode( xz::simple::riscv::lzma_bcj_riscv_encode(start_offset, buf, size) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_bcj_riscv_decode( start_offset: u32, buf: *mut u8, @@ -668,7 +695,7 @@ pub unsafe extern "C" fn lzma_bcj_riscv_decode( xz::simple::riscv::lzma_bcj_riscv_decode(start_offset, buf, size) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_bcj_x86_encode( start_offset: u32, buf: *mut u8, @@ -677,7 +704,7 @@ pub unsafe extern "C" fn lzma_bcj_x86_encode( xz::simple::x86::lzma_bcj_x86_encode(start_offset, buf, size) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_bcj_x86_decode( start_offset: u32, buf: *mut u8, @@ -692,7 +719,7 @@ pub unsafe extern "C" fn lzma_bcj_x86_decode( /* `lzma/stream_flags.h` */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_stream_header_encode( options: *const lzma_stream_flags, out: *mut u8, @@ -700,7 +727,7 @@ pub unsafe extern "C" fn lzma_stream_header_encode( xz::common::stream_flags_encoder::lzma_stream_header_encode(options.cast(), out) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_stream_footer_encode( options: *const lzma_stream_flags, out: *mut u8, @@ -708,7 +735,7 @@ pub unsafe extern "C" fn lzma_stream_footer_encode( xz::common::stream_flags_encoder::lzma_stream_footer_encode(options.cast(), out) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_stream_header_decode( options: *mut lzma_stream_flags, input: *const u8, @@ -716,7 +743,7 @@ pub unsafe extern "C" fn lzma_stream_header_decode( xz::common::stream_flags_decoder::lzma_stream_header_decode(options.cast(), input) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_stream_footer_decode( options: *mut lzma_stream_flags, input: *const u8, @@ -724,7 +751,7 @@ pub unsafe extern "C" fn lzma_stream_footer_decode( xz::common::stream_flags_decoder::lzma_stream_footer_decode(options.cast(), input) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_stream_flags_compare( a: *const lzma_stream_flags, b: *const lzma_stream_flags, @@ -734,19 +761,19 @@ pub unsafe extern "C" fn lzma_stream_flags_compare( /* `lzma/hardware.h` */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_physmem() -> u64 { xz::common::hardware_physmem::lzma_physmem() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_cputhreads() -> u32 { xz::common::hardware_cputhreads::lzma_cputhreads() } /* `lzma/index.h`: core index operations */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_buffer_decode( i: *mut *mut lzma_index, memlimit: *mut u64, @@ -758,31 +785,31 @@ pub unsafe extern "C" fn lzma_index_buffer_decode( xz::common::index_decoder::lzma_index_buffer_decode( i.cast(), memlimit, - allocator.cast(), + normalize_c_allocator(allocator).cast(), input, in_pos, in_size, ) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_uncompressed_size(i: *const lzma_index) -> lzma_vli { xz::common::index::lzma_index_uncompressed_size(i.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_end(i: *mut lzma_index, allocator: *const lzma_allocator) { - xz::common::index::lzma_index_end(i.cast(), allocator.cast()) + xz::common::index::lzma_index_end(i.cast(), normalize_c_allocator(allocator).cast()) } /* `lzma/block.h` */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_block_header_size(block: *mut lzma_block) -> lzma_ret { xz::common::block_header_encoder::lzma_block_header_size(block.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_block_header_encode( block: *const lzma_block, out: *mut u8, @@ -790,7 +817,7 @@ pub unsafe extern "C" fn lzma_block_header_encode( xz::common::block_header_encoder::lzma_block_header_encode(block.cast(), out) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_block_header_decode( block: *mut lzma_block, allocator: *const lzma_allocator, @@ -798,12 +825,12 @@ pub unsafe extern "C" fn lzma_block_header_decode( ) -> lzma_ret { xz::common::block_header_decoder::lzma_block_header_decode( block.cast(), - allocator.cast(), + normalize_c_allocator(allocator).cast(), input, ) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_block_compressed_size( block: *mut lzma_block, unpadded_size: lzma_vli, @@ -811,38 +838,40 @@ pub unsafe extern "C" fn lzma_block_compressed_size( xz::common::block_util::lzma_block_compressed_size(block.cast(), unpadded_size) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_block_unpadded_size(block: *const lzma_block) -> lzma_vli { xz::common::block_util::lzma_block_unpadded_size(block.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_block_total_size(block: *const lzma_block) -> lzma_vli { xz::common::block_util::lzma_block_total_size(block.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_block_encoder( strm: *mut lzma_stream, block: *mut lzma_block, ) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::block_encoder::lzma_block_encoder(strm.cast(), block.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_block_decoder( strm: *mut lzma_stream, block: *mut lzma_block, ) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::block_decoder::lzma_block_decoder(strm.cast(), block.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn lzma_block_buffer_bound(uncompressed_size: size_t) -> size_t { xz::common::block_buffer_encoder::lzma_block_buffer_bound(uncompressed_size) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_block_buffer_encode( block: *mut lzma_block, allocator: *const lzma_allocator, @@ -854,7 +883,7 @@ pub unsafe extern "C" fn lzma_block_buffer_encode( ) -> lzma_ret { xz::common::block_buffer_encoder::lzma_block_buffer_encode( block.cast(), - allocator.cast(), + normalize_c_allocator(allocator).cast(), input, in_size, out, @@ -863,7 +892,7 @@ pub unsafe extern "C" fn lzma_block_buffer_encode( ) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_block_uncomp_encode( block: *mut lzma_block, input: *const u8, @@ -882,7 +911,7 @@ pub unsafe extern "C" fn lzma_block_uncomp_encode( ) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_block_buffer_decode( block: *mut lzma_block, allocator: *const lzma_allocator, @@ -895,7 +924,7 @@ pub unsafe extern "C" fn lzma_block_buffer_decode( ) -> lzma_ret { xz::common::block_buffer_decoder::lzma_block_buffer_decode( block.cast(), - allocator.cast(), + normalize_c_allocator(allocator).cast(), input, in_pos, in_size, @@ -907,22 +936,22 @@ pub unsafe extern "C" fn lzma_block_buffer_decode( /* `lzma/index.h`: extended index operations */ -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn lzma_index_memusage(streams: lzma_vli, blocks: lzma_vli) -> u64 { xz::common::index::lzma_index_memusage(streams, blocks) } -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn lzma_index_memused(i: *const lzma_index) -> u64 { xz::common::index::lzma_index_memused(i.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_init(allocator: *const lzma_allocator) -> *mut lzma_index { - xz::common::index::lzma_index_init(allocator.cast()).cast() + xz::common::index::lzma_index_init(normalize_c_allocator(allocator).cast()).cast() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_append( i: *mut lzma_index, allocator: *const lzma_allocator, @@ -931,13 +960,13 @@ pub unsafe extern "C" fn lzma_index_append( ) -> lzma_ret { xz::common::index::lzma_index_append( i.cast(), - allocator.cast(), + normalize_c_allocator(allocator).cast(), unpadded_size, uncompressed_size, ) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_stream_flags( i: *mut lzma_index, stream_flags: *const lzma_stream_flags, @@ -945,12 +974,12 @@ pub unsafe extern "C" fn lzma_index_stream_flags( xz::common::index::lzma_index_stream_flags(i.cast(), stream_flags.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_checks(i: *const lzma_index) -> u32 { xz::common::index::lzma_index_checks(i.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_stream_padding( i: *mut lzma_index, stream_padding: lzma_vli, @@ -958,47 +987,47 @@ pub unsafe extern "C" fn lzma_index_stream_padding( xz::common::index::lzma_index_stream_padding(i.cast(), stream_padding) } -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn lzma_index_stream_count(i: *const lzma_index) -> lzma_vli { xz::common::index::lzma_index_stream_count(i.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn lzma_index_block_count(i: *const lzma_index) -> lzma_vli { xz::common::index::lzma_index_block_count(i.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn lzma_index_size(i: *const lzma_index) -> lzma_vli { xz::common::index::lzma_index_size(i.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn lzma_index_stream_size(i: *const lzma_index) -> lzma_vli { xz::common::index::lzma_index_stream_size(i.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn lzma_index_total_size(i: *const lzma_index) -> lzma_vli { xz::common::index::lzma_index_total_size(i.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_file_size(i: *const lzma_index) -> lzma_vli { xz::common::index::lzma_index_file_size(i.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_iter_init(iter: *mut lzma_index_iter, i: *const lzma_index) { xz::common::index::lzma_index_iter_init(iter.cast(), i.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_iter_rewind(iter: *mut lzma_index_iter) { xz::common::index::lzma_index_iter_rewind(iter.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_iter_next( iter: *mut lzma_index_iter, mode: lzma_index_iter_mode, @@ -1006,7 +1035,7 @@ pub unsafe extern "C" fn lzma_index_iter_next( xz::common::index::lzma_index_iter_next(iter.cast(), mode) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_iter_locate( iter: *mut lzma_index_iter, target: lzma_vli, @@ -1014,41 +1043,47 @@ pub unsafe extern "C" fn lzma_index_iter_locate( xz::common::index::lzma_index_iter_locate(iter.cast(), target) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_cat( dest: *mut lzma_index, src: *mut lzma_index, allocator: *const lzma_allocator, ) -> lzma_ret { - xz::common::index::lzma_index_cat(dest.cast(), src.cast(), allocator.cast()) + xz::common::index::lzma_index_cat( + dest.cast(), + src.cast(), + normalize_c_allocator(allocator).cast(), + ) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_dup( i: *const lzma_index, allocator: *const lzma_allocator, ) -> *mut lzma_index { - xz::common::index::lzma_index_dup(i.cast(), allocator.cast()).cast() + xz::common::index::lzma_index_dup(i.cast(), normalize_c_allocator(allocator).cast()).cast() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_encoder( strm: *mut lzma_stream, i: *const lzma_index, ) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::index_encoder::lzma_index_encoder(strm.cast(), i.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_decoder( strm: *mut lzma_stream, i: *mut *mut lzma_index, memlimit: u64, ) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::index_decoder::lzma_index_decoder(strm.cast(), i.cast(), memlimit) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_buffer_encode( i: *const lzma_index, out: *mut u8, @@ -1060,23 +1095,30 @@ pub unsafe extern "C" fn lzma_index_buffer_encode( /* `lzma/index_hash.h` */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_hash_init( index_hash: *mut lzma_index_hash, allocator: *const lzma_allocator, ) -> *mut lzma_index_hash { - xz::common::index_hash::lzma_index_hash_init(index_hash.cast(), allocator.cast()).cast() + xz::common::index_hash::lzma_index_hash_init( + index_hash.cast(), + normalize_c_allocator(allocator).cast(), + ) + .cast() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_hash_end( index_hash: *mut lzma_index_hash, allocator: *const lzma_allocator, ) { - xz::common::index_hash::lzma_index_hash_end(index_hash.cast(), allocator.cast()) + xz::common::index_hash::lzma_index_hash_end( + index_hash.cast(), + normalize_c_allocator(allocator).cast(), + ) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_hash_append( index_hash: *mut lzma_index_hash, unpadded_size: lzma_vli, @@ -1089,7 +1131,7 @@ pub unsafe extern "C" fn lzma_index_hash_append( ) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_index_hash_decode( index_hash: *mut lzma_index_hash, input: *const u8, @@ -1099,14 +1141,14 @@ pub unsafe extern "C" fn lzma_index_hash_decode( xz::common::index_hash::lzma_index_hash_decode(index_hash.cast(), input, in_pos, in_size) } -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn lzma_index_hash_size(index_hash: *const lzma_index_hash) -> lzma_vli { xz::common::index_hash::lzma_index_hash_size(index_hash.cast()) } /* `lzma/filter.h`: filter flags / string conversion */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_filter_flags_size( size: *mut u32, filter: *const lzma_filter, @@ -1114,7 +1156,7 @@ pub unsafe extern "C" fn lzma_filter_flags_size( xz::common::filter_flags_encoder::lzma_filter_flags_size(size, filter.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_filter_flags_encode( filter: *const lzma_filter, out: *mut u8, @@ -1129,7 +1171,7 @@ pub unsafe extern "C" fn lzma_filter_flags_encode( ) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_filter_flags_decode( filter: *mut lzma_filter, allocator: *const lzma_allocator, @@ -1139,22 +1181,25 @@ pub unsafe extern "C" fn lzma_filter_flags_decode( ) -> lzma_ret { xz::common::filter_flags_decoder::lzma_filter_flags_decode( filter.cast(), - allocator.cast(), + normalize_c_allocator(allocator).cast(), input, in_pos, in_size, ) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_filters_free( filters: *mut lzma_filter, allocator: *const lzma_allocator, ) { - xz::common::filter_common::lzma_filters_free(filters.cast(), allocator.cast()) + xz::common::filter_common::lzma_filters_free( + filters.cast(), + normalize_c_allocator(allocator).cast(), + ) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_str_to_filters( str_: *const c_char, error_pos: *mut c_int, @@ -1167,11 +1212,11 @@ pub unsafe extern "C" fn lzma_str_to_filters( error_pos, filters.cast(), flags, - allocator.cast(), + normalize_c_allocator(allocator).cast(), ) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_str_from_filters( str_: *mut *mut c_char, filters: *const lzma_filter, @@ -1182,31 +1227,37 @@ pub unsafe extern "C" fn lzma_str_from_filters( str_, filters.cast(), flags, - allocator.cast(), + normalize_c_allocator(allocator).cast(), ) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_str_list_filters( str_: *mut *mut c_char, filter_id: lzma_vli, flags: u32, allocator: *const lzma_allocator, ) -> lzma_ret { - xz::common::string_conversion::lzma_str_list_filters(str_, filter_id, flags, allocator.cast()) + xz::common::string_conversion::lzma_str_list_filters( + str_, + filter_id, + flags, + normalize_c_allocator(allocator).cast(), + ) } /* `lzma/container.h`: additional container helpers */ -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_microlzma_encoder( strm: *mut lzma_stream, options: *const lzma_options_lzma, ) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::microlzma_encoder::lzma_microlzma_encoder(strm.cast(), options.cast()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_microlzma_decoder( strm: *mut lzma_stream, comp_size: u64, @@ -1214,6 +1265,7 @@ pub unsafe extern "C" fn lzma_microlzma_decoder( uncomp_size_is_exact: lzma_bool, dict_size: u32, ) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::microlzma_decoder::lzma_microlzma_decoder( strm.cast(), comp_size, @@ -1223,13 +1275,14 @@ pub unsafe extern "C" fn lzma_microlzma_decoder( ) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_file_info_decoder( strm: *mut lzma_stream, i: *mut *mut lzma_index, memlimit: u64, file_size: u64, ) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::file_info::lzma_file_info_decoder(strm.cast(), i.cast(), memlimit, file_size) } @@ -1240,25 +1293,27 @@ pub unsafe extern "C" fn lzma_file_info_decoder( /* `lzma/container.h`: multithreaded API */ #[cfg(feature = "parallel")] -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_stream_encoder_mt( strm: *mut lzma_stream, options: *const lzma_mt, ) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::stream_mt::lzma_stream_encoder_mt(strm.cast(), options.cast()) } #[cfg(feature = "parallel")] -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_stream_decoder_mt( strm: *mut lzma_stream, options: *const lzma_mt, ) -> lzma_ret { + normalize_c_stream_allocator(strm); xz::common::stream_mt::lzma_stream_decoder_mt(strm.cast(), options.cast()) } #[cfg(feature = "parallel")] -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn lzma_stream_encoder_mt_memusage(options: *const lzma_mt) -> u64 { xz::common::stream_mt::lzma_stream_encoder_mt_memusage(options.cast()) } diff --git a/xz/Cargo.toml b/xz/Cargo.toml index 8495c3fd..8a13865b 100644 --- a/xz/Cargo.toml +++ b/xz/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "xz" version = "0.1.0" -edition = "2021" +edition = "2024" license = "MIT OR Apache-2.0" description = "Pure Rust implementation of liblzma (transpiled from C via c2rust)" diff --git a/xz/src/alloc.rs b/xz/src/alloc.rs index 1b73e3a3..6e955431 100644 --- a/xz/src/alloc.rs +++ b/xz/src/alloc.rs @@ -1,5 +1,5 @@ use crate::types::*; -use std::alloc::{alloc, alloc_zeroed, dealloc, Layout}; +use std::alloc::{Layout, alloc, alloc_zeroed, dealloc}; const RUST_ALLOC_ALIGN: usize = 16; @@ -15,39 +15,41 @@ const fn round_up(value: usize, align: usize) -> usize { (value + (align - 1)) & !(align - 1) } -unsafe fn rust_alloc_impl(size: usize, align: usize, zeroed: bool) -> *mut c_void { +fn rust_alloc_impl(size: usize, align: usize, zeroed: bool) -> *mut c_void { let size = if size == 0 { 1 } else { size }; let align = align .max(RUST_ALLOC_ALIGN) .max(core::mem::align_of::()); let header_size = core::mem::size_of::(); - let offset = match header_size.checked_add(align - 1) { - Some(value) => round_up(value, align), - None => return core::ptr::null_mut(), + let Some(value) = header_size.checked_add(align - 1) else { + return core::ptr::null_mut(); }; - let total_size = match offset.checked_add(size) { - Some(total_size) => total_size, - None => return core::ptr::null_mut(), + let offset = round_up(value, align); + let Some(total_size) = offset.checked_add(size) else { + return core::ptr::null_mut(); }; - let layout = match Layout::from_size_align(total_size, align) { - Ok(layout) => layout, - Err(_) => return core::ptr::null_mut(), + let Ok(layout) = Layout::from_size_align(total_size, align) else { + return core::ptr::null_mut(); }; - let base = if zeroed { - alloc_zeroed(layout) - } else { - alloc(layout) + let base = unsafe { + if zeroed { + alloc_zeroed(layout) + } else { + alloc(layout) + } }; if base.is_null() { return core::ptr::null_mut(); } - let user_ptr = base.add(offset); - let header_ptr = user_ptr.sub(header_size) as *mut RustAllocHeader; - header_ptr.write(RustAllocHeader { - total_size, - align, - offset, - }); + let user_ptr = unsafe { base.add(offset) }; + let header_ptr = unsafe { user_ptr.sub(header_size) as *mut RustAllocHeader }; + unsafe { + header_ptr.write(RustAllocHeader { + total_size, + align, + offset, + }); + } user_ptr as *mut c_void } @@ -64,27 +66,45 @@ unsafe fn rust_free_impl(ptr: *mut c_void) { dealloc(base, layout); } -pub(crate) unsafe extern "C" fn lzma_rust_alloc( +pub(crate) unsafe fn lzma_rust_alloc( _opaque: *mut c_void, nmemb: size_t, size: size_t, ) -> *mut c_void { - let size = match (nmemb as usize).checked_mul(size as usize) { - Some(size) => size, - None => return core::ptr::null_mut(), + let Some(size) = (nmemb as usize).checked_mul(size as usize) else { + return core::ptr::null_mut(); }; rust_alloc_impl(size, RUST_ALLOC_ALIGN, false) } -pub(crate) unsafe extern "C" fn lzma_rust_free(_opaque: *mut c_void, ptr: *mut c_void) { +pub(crate) unsafe fn lzma_rust_free(_opaque: *mut c_void, ptr: *mut c_void) { rust_free_impl(ptr); } +#[repr(transparent)] +struct StaticAllocator(lzma_allocator); + +unsafe impl Sync for StaticAllocator {} + +static RUST_ALLOCATOR: StaticAllocator = StaticAllocator(lzma_allocator { + alloc: Some(lzma_rust_alloc), + free: Some(lzma_rust_free), + opaque: core::ptr::null_mut(), +}); + pub fn rust_allocator() -> lzma_allocator { - lzma_allocator { - alloc: Some(lzma_rust_alloc), - free: Some(lzma_rust_free), - opaque: core::ptr::null_mut(), + RUST_ALLOCATOR.0 +} + +pub(crate) fn rust_allocator_ptr() -> *const lzma_allocator { + &raw const RUST_ALLOCATOR.0 +} + +pub(crate) fn allocator_or_rust(allocator: *const lzma_allocator) -> *const lzma_allocator { + if allocator.is_null() { + rust_allocator_ptr() + } else { + allocator } } @@ -93,11 +113,11 @@ pub(crate) unsafe fn internal_alloc_bytes( allocator: *const lzma_allocator, ) -> *mut c_void { let size = if size == 0 { 1 } else { size }; - if !allocator.is_null() && (*allocator).alloc.is_some() { - (*allocator).alloc.unwrap()((*allocator).opaque, 1, size) - } else { - rust_alloc_impl(size as usize, RUST_ALLOC_ALIGN, false) + let allocator = allocator_or_rust(allocator); + if let Some(alloc) = unsafe { (*allocator).alloc } { + return unsafe { alloc((*allocator).opaque, 1, size) }; } + rust_alloc_impl(size as usize, RUST_ALLOC_ALIGN, false) } pub(crate) unsafe fn internal_alloc_zeroed_bytes( @@ -105,24 +125,24 @@ pub(crate) unsafe fn internal_alloc_zeroed_bytes( allocator: *const lzma_allocator, ) -> *mut c_void { let size = if size == 0 { 1 } else { size }; - if !allocator.is_null() && (*allocator).alloc.is_some() { - let ptr = (*allocator).alloc.unwrap()((*allocator).opaque, 1, size); + let allocator = allocator_or_rust(allocator); + if let Some(alloc) = unsafe { (*allocator).alloc } { + let ptr = unsafe { alloc((*allocator).opaque, 1, size) }; if !ptr.is_null() { - core::ptr::write_bytes(ptr as *mut u8, 0, size); + unsafe { core::ptr::write_bytes(ptr as *mut u8, 0, size) }; } - ptr - } else { - rust_alloc_impl(size as usize, RUST_ALLOC_ALIGN, true) + return ptr; } + rust_alloc_impl(size as usize, RUST_ALLOC_ALIGN, true) } pub(crate) unsafe fn internal_alloc_object(allocator: *const lzma_allocator) -> *mut T { - if !allocator.is_null() && (*allocator).alloc.is_some() { - return (*allocator).alloc.unwrap()( - (*allocator).opaque, - 1, - core::mem::size_of::() as size_t, - ) as *mut T; + if !allocator.is_null() + && let Some(alloc) = unsafe { (*allocator).alloc } + { + return unsafe { + alloc((*allocator).opaque, 1, core::mem::size_of::() as size_t) as *mut T + }; } rust_alloc_impl(core::mem::size_of::(), core::mem::align_of::(), false) as *mut T } @@ -131,12 +151,13 @@ pub(crate) unsafe fn internal_alloc_array( count: size_t, allocator: *const lzma_allocator, ) -> *mut T { - let size = match (count as usize).checked_mul(core::mem::size_of::()) { - Some(size) => size, - None => return core::ptr::null_mut(), + let Some(size) = (count as usize).checked_mul(core::mem::size_of::()) else { + return core::ptr::null_mut(); }; - if !allocator.is_null() && (*allocator).alloc.is_some() { - return (*allocator).alloc.unwrap()((*allocator).opaque, 1, size as size_t) as *mut T; + if !allocator.is_null() + && let Some(alloc) = unsafe { (*allocator).alloc } + { + return unsafe { alloc((*allocator).opaque, 1, size as size_t) as *mut T }; } rust_alloc_impl(size, core::mem::align_of::(), false) as *mut T } @@ -145,14 +166,15 @@ pub(crate) unsafe fn internal_alloc_zeroed_array( count: size_t, allocator: *const lzma_allocator, ) -> *mut T { - let size = match (count as usize).checked_mul(core::mem::size_of::()) { - Some(size) => size, - None => return core::ptr::null_mut(), + let Some(size) = (count as usize).checked_mul(core::mem::size_of::()) else { + return core::ptr::null_mut(); }; - if !allocator.is_null() && (*allocator).alloc.is_some() { - let ptr = (*allocator).alloc.unwrap()((*allocator).opaque, 1, size as size_t) as *mut T; + if !allocator.is_null() + && let Some(alloc) = unsafe { (*allocator).alloc } + { + let ptr = unsafe { alloc((*allocator).opaque, 1, size as size_t) as *mut T }; if !ptr.is_null() { - core::ptr::write_bytes(ptr as *mut u8, 0, size); + unsafe { core::ptr::write_bytes(ptr as *mut u8, 0, size) }; } return ptr; } @@ -160,11 +182,13 @@ pub(crate) unsafe fn internal_alloc_zeroed_array( } pub(crate) unsafe fn internal_free(ptr: *mut c_void, allocator: *const lzma_allocator) { - if !allocator.is_null() && (*allocator).free.is_some() { - (*allocator).free.unwrap()((*allocator).opaque, ptr); - } else { - rust_free_impl(ptr); + if !allocator.is_null() + && let Some(free) = unsafe { (*allocator).free } + { + unsafe { free((*allocator).opaque, ptr) }; + return; } + rust_free_impl(ptr); } #[cfg(test)] @@ -174,10 +198,9 @@ mod tests { #[test] fn rust_allocator_round_trip() { unsafe { - let allocator = rust_allocator(); - let ptr = allocator.alloc.unwrap()(allocator.opaque, 4, 8); + let ptr = lzma_rust_alloc(core::ptr::null_mut(), 4, 8); assert!(!ptr.is_null()); - allocator.free.unwrap()(allocator.opaque, ptr); + lzma_rust_free(core::ptr::null_mut(), ptr); } } diff --git a/xz/src/check/check.rs b/xz/src/check/check.rs index 7197ebb4..62bc0ac0 100644 --- a/xz/src/check/check.rs +++ b/xz/src/check/check.rs @@ -1,8 +1,8 @@ use crate::check::crc64_fast::lzma_crc64; use crate::check::sha256::{lzma_sha256_finish, lzma_sha256_init, lzma_sha256_update}; use crate::types::*; -pub fn lzma_check_is_supported(type_0: lzma_check) -> lzma_bool { - if type_0 > LZMA_CHECK_ID_MAX { +pub fn lzma_check_is_supported(check_type: lzma_check) -> lzma_bool { + if check_type > LZMA_CHECK_ID_MAX { return false as lzma_bool; } static available_checks: [lzma_bool; 16] = [ @@ -23,24 +23,24 @@ pub fn lzma_check_is_supported(type_0: lzma_check) -> lzma_bool { false as lzma_bool, false as lzma_bool, ]; - available_checks[type_0 as usize] + available_checks[check_type as usize] } -pub fn lzma_check_size(type_0: lzma_check) -> u32 { - if type_0 > LZMA_CHECK_ID_MAX { +pub fn lzma_check_size(check_type: lzma_check) -> u32 { + if check_type > LZMA_CHECK_ID_MAX { return UINT32_MAX; } static check_sizes: [u8; 16] = [ 0, 4 as u8, 4 as u8, 4 as u8, 8 as u8, 8 as u8, 8 as u8, 16 as u8, 16 as u8, 16 as u8, 32 as u8, 32 as u8, 32 as u8, 64 as u8, 64 as u8, 64 as u8, ]; - check_sizes[type_0 as usize] as u32 + check_sizes[check_type as usize] as u32 } -pub unsafe fn lzma_check_init(check: *mut lzma_check_state, type_0: lzma_check) { +pub unsafe fn lzma_check_init(check: *mut lzma_check_state, check_type: lzma_check) { if check.is_null() { return; } - match type_0 { + match check_type { LZMA_CHECK_CRC32 => { (*check).state.crc32 = 0; } @@ -55,7 +55,7 @@ pub unsafe fn lzma_check_init(check: *mut lzma_check_state, type_0: lzma_check) } pub unsafe fn lzma_check_update( check: *mut lzma_check_state, - type_0: lzma_check, + check_type: lzma_check, buf: *const u8, size: size_t, ) { @@ -63,7 +63,7 @@ pub unsafe fn lzma_check_update( return; } - match type_0 { + match check_type { LZMA_CHECK_CRC32 => { (*check).state.crc32 = lzma_crc32(buf, size, (*check).state.crc32); } @@ -76,12 +76,12 @@ pub unsafe fn lzma_check_update( _ => {} }; } -pub unsafe fn lzma_check_finish(check: *mut lzma_check_state, type_0: lzma_check) { +pub unsafe fn lzma_check_finish(check: *mut lzma_check_state, check_type: lzma_check) { if check.is_null() { return; } - match type_0 { + match check_type { LZMA_CHECK_CRC32 => { (*check).buffer.u32_0[0] = (*check).state.crc32; } diff --git a/xz/src/check/crc32_fast.rs b/xz/src/check/crc32_fast.rs index 4ed3553f..9470c924 100644 --- a/xz/src/check/crc32_fast.rs +++ b/xz/src/check/crc32_fast.rs @@ -316,40 +316,33 @@ pub static lzma_crc32_table: [[u32; 256]; 8] = [ 0x646e019b, 0xeae10678, 0x264b06e6, ], ]; -unsafe fn lzma_crc32_generic(mut buf: *const u8, mut size: size_t, mut crc: u32) -> u32 { +fn lzma_crc32_generic(mut buf: &[u8], mut crc: u32) -> u32 { crc = !crc; - if size > 8 { - while buf as uintptr_t & 7 as uintptr_t != 0 { - crc = lzma_crc32_table[0][(*buf as u32 ^ crc & 0xff) as usize] ^ crc >> 8; - buf = buf.offset(1); - size -= 1; + if buf.len() > 8 { + while buf.as_ptr() as uintptr_t & 7 as uintptr_t != 0 { + crc = lzma_crc32_table[0][(buf[0] as u32 ^ crc & 0xff) as usize] ^ crc >> 8; + buf = &buf[1..]; } - let limit: *const u8 = buf.offset((size & !(7)) as isize); - size &= 7; - while buf < limit { - crc ^= aligned_read32ne(buf); - buf = buf.offset(4); + let (mut bulk, tail) = buf.split_at(buf.len() & !7); + while !bulk.is_empty() { + crc ^= unsafe { aligned_read32ne(bulk.as_ptr()) }; + bulk = &bulk[4..]; crc = lzma_crc32_table[7][(crc & 0xff) as usize] ^ lzma_crc32_table[6][(crc >> 8 & 0xff) as usize] ^ lzma_crc32_table[5][(crc >> 16 & 0xff) as usize] ^ lzma_crc32_table[4][(crc >> 24) as usize]; - let tmp: u32 = aligned_read32ne(buf) as u32; - buf = buf.offset(4); + let tmp: u32 = unsafe { aligned_read32ne(bulk.as_ptr()) } as u32; + bulk = &bulk[4..]; crc = lzma_crc32_table[3][(tmp & 0xff) as usize] ^ lzma_crc32_table[2][(tmp >> 8 & 0xff) as usize] ^ crc ^ lzma_crc32_table[1][(tmp >> 16 & 0xff) as usize] ^ lzma_crc32_table[0][(tmp >> 24) as usize]; } + buf = tail; } - loop { - let old_size = size; - size = size.wrapping_sub(1); - if old_size == 0 { - break; - } - crc = lzma_crc32_table[0][(*buf as u32 ^ crc & 0xff) as usize] ^ crc >> 8; - buf = buf.offset(1); + for &byte in buf { + crc = lzma_crc32_table[0][(byte as u32 ^ crc & 0xff) as usize] ^ crc >> 8; } !crc } @@ -374,62 +367,69 @@ unsafe fn aligned_read64le(buf: *const u8) -> u64 { #[cfg(target_arch = "aarch64")] #[target_feature(enable = "crc")] -unsafe fn lzma_crc32_arm64(mut buf: *const u8, mut size: size_t, mut crc: u32) -> u32 { +unsafe fn lzma_crc32_arm64(buf: &[u8], mut crc: u32) -> u32 { + let mut ptr = buf.as_ptr(); + let mut size = buf.len(); crc = !crc; if size >= 8 { - let align = (0usize.wrapping_sub(buf as usize)) & 7; + let align = (0usize.wrapping_sub(ptr as usize)) & 7; if align & 1 != 0 { - crc = __crc32b(crc, *buf); - buf = buf.add(1); + crc = __crc32b(crc, *ptr); + ptr = ptr.add(1); } if align & 2 != 0 { - crc = __crc32h(crc, aligned_read16le(buf)); - buf = buf.add(2); + crc = __crc32h(crc, aligned_read16le(ptr)); + ptr = ptr.add(2); } if align & 4 != 0 { - crc = __crc32w(crc, aligned_read32le(buf)); - buf = buf.add(4); + crc = __crc32w(crc, aligned_read32le(ptr)); + ptr = ptr.add(4); } size -= align; - let limit = buf.add(size & !7); - while buf < limit { - crc = __crc32d(crc, aligned_read64le(buf)); - buf = buf.add(8); + let limit = ptr.add(size & !7); + while ptr < limit { + crc = __crc32d(crc, aligned_read64le(ptr)); + ptr = ptr.add(8); } size &= 7; } if size & 4 != 0 { - crc = __crc32w(crc, aligned_read32le(buf)); - buf = buf.add(4); + crc = __crc32w(crc, aligned_read32le(ptr)); + ptr = ptr.add(4); } if size & 2 != 0 { - crc = __crc32h(crc, aligned_read16le(buf)); - buf = buf.add(2); + crc = __crc32h(crc, aligned_read16le(ptr)); + ptr = ptr.add(2); } if size & 1 != 0 { - crc = __crc32b(crc, *buf); + crc = __crc32b(crc, *ptr); } !crc } pub unsafe fn lzma_crc32(buf: *const u8, size: size_t, crc: u32) -> u32 { + let buf = if size == 0 { + &[][..] + } else { + core::slice::from_raw_parts(buf, size) + }; #[cfg(target_arch = "aarch64")] { if std::arch::is_aarch64_feature_detected!("crc") { - return lzma_crc32_arm64(buf, size, crc); + return lzma_crc32_arm64(buf, crc); } } - lzma_crc32_generic(buf, size, crc) + lzma_crc32_generic(buf, crc) } diff --git a/xz/src/check/crc64_fast.rs b/xz/src/check/crc64_fast.rs index 791d5684..704db941 100644 --- a/xz/src/check/crc64_fast.rs +++ b/xz/src/check/crc64_fast.rs @@ -1050,6 +1050,8 @@ unsafe fn crc64_step4( ^ *table0.add((tmp >> 24) as usize) } +// Keep this loop pointer-based: the slice version left slice_index_fail +// slow paths in optimized code for the large contiguous-buffer workload. #[inline(always)] unsafe fn lzma_crc64_generic(mut buf: *const u8, mut size: size_t, mut crc: u64) -> u64 { let table0 = lzma_crc64_table[0].as_ptr(); @@ -1063,7 +1065,7 @@ unsafe fn lzma_crc64_generic(mut buf: *const u8, mut size: size_t, mut crc: u64) buf = buf.offset(1); size -= 1; } - let limit8 = buf.offset((size & !(7)) as isize); + let limit8 = buf.offset((size & !7) as isize); while buf < limit8 { crc = crc64_step4(table0, table1, table2, table3, buf, crc); buf = buf.offset(4); diff --git a/xz/src/check/sha256.rs b/xz/src/check/sha256.rs index 27a825ae..bc04db01 100644 --- a/xz/src/check/sha256.rs +++ b/xz/src/check/sha256.rs @@ -3,7 +3,7 @@ use crate::types::*; fn rotr_32(num: u32, amount: c_uint) -> u32 { num >> amount | num << 32u32.wrapping_sub(amount) } -static mut SHA256_K: [u32; 64] = [ +static SHA256_K: [u32; 64] = [ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0xfc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, @@ -532,7 +532,7 @@ unsafe fn process(check: *mut lzma_check_state) { ); } pub unsafe fn lzma_sha256_init(check: *mut lzma_check_state) { - static mut s: [u32; 8] = [ + let s: [u32; 8] = [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, ]; diff --git a/xz/src/common/alone_decoder.rs b/xz/src/common/alone_decoder.rs index 894c0d6f..afd4e09f 100644 --- a/xz/src/common/alone_decoder.rs +++ b/xz/src/common/alone_decoder.rs @@ -17,10 +17,10 @@ pub const SEQ_CODER_INIT: alone_decoder_seq = 3; pub const SEQ_UNCOMPRESSED_SIZE: alone_decoder_seq = 2; pub const SEQ_DICTIONARY_SIZE: alone_decoder_seq = 1; pub const SEQ_PROPERTIES: alone_decoder_seq = 0; -unsafe extern "C" fn alone_decode( +unsafe fn alone_decode( coder_ptr: *mut c_void, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, @@ -30,22 +30,20 @@ unsafe extern "C" fn alone_decode( ) -> lzma_ret { let coder: *mut lzma_alone_coder = coder_ptr as *mut lzma_alone_coder; while *out_pos < out_size && ((*coder).sequence == SEQ_CODE || *in_pos < in_size) { - let current_block_42: u64; match (*coder).sequence { 0 => { if lzma_lzma_lclppb_decode( ::core::ptr::addr_of_mut!((*coder).options), - *in_0.offset(*in_pos as isize), + *input.offset(*in_pos as isize), ) { return LZMA_FORMAT_ERROR; } (*coder).sequence = SEQ_DICTIONARY_SIZE; *in_pos += 1; - current_block_42 = 11048769245176032998; } 1 => { (*coder).options.dict_size = ((*coder).options.dict_size as size_t - | (*in_0.offset(*in_pos as isize) as size_t) << ((*coder).pos * 8)) + | (*input.offset(*in_pos as isize) as size_t) << ((*coder).pos * 8)) as u32; (*coder).pos += 1; if (*coder).pos == 4 { @@ -65,16 +63,13 @@ unsafe extern "C" fn alone_decode( (*coder).sequence = SEQ_UNCOMPRESSED_SIZE; } *in_pos += 1; - current_block_42 = 11048769245176032998; } 2 => { (*coder).uncompressed_size |= - (*in_0.offset(*in_pos as isize) as lzma_vli) << ((*coder).pos * 8); + (*input.offset(*in_pos as isize) as lzma_vli) << ((*coder).pos * 8); *in_pos += 1; (*coder).pos += 1; - if (*coder).pos < 8 { - current_block_42 = 11048769245176032998; - } else { + if (*coder).pos >= 8 { if (*coder).picky && (*coder).uncompressed_size != LZMA_VLI_UNKNOWN && (*coder).uncompressed_size >= 1 << 38 @@ -90,17 +85,16 @@ unsafe extern "C" fn alone_decode( .wrapping_add(LZMA_MEMUSAGE_BASE); (*coder).pos = 0; (*coder).sequence = SEQ_CODER_INIT; - current_block_42 = 14763689060501151050; } } - 3 => { - current_block_42 = 14763689060501151050; - } + 3 => {} 4 => { - return (*coder).next.code.unwrap()( + debug_assert!((*coder).next.code.is_some()); + let code = (*coder).next.code.unwrap_unchecked(); + return code( (*coder).next.coder, allocator, - in_0, + input, in_pos, in_size, out, @@ -111,52 +105,48 @@ unsafe extern "C" fn alone_decode( } _ => return LZMA_PROG_ERROR, } - match current_block_42 { - 14763689060501151050 => { - if (*coder).memusage > (*coder).memlimit { - return LZMA_MEMLIMIT_ERROR; - } - let mut filters: [lzma_filter_info; 2] = [ - lzma_filter_info_s { - id: LZMA_FILTER_LZMA1EXT, - init: Some( - lzma_lzma_decoder_init - as unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *const lzma_filter_info, - ) - -> lzma_ret, - ), - options: ::core::ptr::addr_of_mut!((*coder).options) as *mut c_void, - }, - lzma_filter_info_s { - id: 0, - init: None, - options: core::ptr::null_mut(), - }, - ]; - let ret_: lzma_ret = lzma_next_filter_init( - ::core::ptr::addr_of_mut!((*coder).next), - allocator, - ::core::ptr::addr_of_mut!(filters) as *mut lzma_filter_info, - ); - if ret_ != LZMA_OK { - return ret_; - } - (*coder).sequence = SEQ_CODE; + if (*coder).sequence == SEQ_CODER_INIT { + if (*coder).memusage > (*coder).memlimit { + return LZMA_MEMLIMIT_ERROR; + } + let mut filters: [lzma_filter_info; 2] = [ + lzma_filter_info_s { + id: LZMA_FILTER_LZMA1EXT, + init: Some( + lzma_lzma_decoder_init + as unsafe fn( + *mut lzma_next_coder, + *const lzma_allocator, + *const lzma_filter_info, + ) -> lzma_ret, + ), + options: ::core::ptr::addr_of_mut!((*coder).options) as *mut c_void, + }, + lzma_filter_info_s { + id: 0, + init: None, + options: core::ptr::null_mut(), + }, + ]; + let ret_: lzma_ret = lzma_next_filter_init( + ::core::ptr::addr_of_mut!((*coder).next), + allocator, + ::core::ptr::addr_of_mut!(filters) as *mut lzma_filter_info, + ); + if ret_ != LZMA_OK { + return ret_; } - _ => {} + (*coder).sequence = SEQ_CODE; } } LZMA_OK } -unsafe extern "C" fn alone_decoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { +unsafe fn alone_decoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_alone_coder = coder_ptr as *mut lzma_alone_coder; lzma_next_end(::core::ptr::addr_of_mut!((*coder).next), allocator); crate::alloc::internal_free(coder as *mut c_void, allocator); } -unsafe extern "C" fn alone_decoder_memconfig( +unsafe fn alone_decoder_memconfig( coder_ptr: *mut c_void, memusage: *mut u64, old_memlimit: *mut u64, @@ -173,52 +163,28 @@ unsafe extern "C" fn alone_decoder_memconfig( } LZMA_OK } -pub(crate) unsafe extern "C" fn lzma_alone_decoder_init( +pub(crate) unsafe fn lzma_alone_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, memlimit: u64, picky: bool, ) -> lzma_ret { if core::mem::transmute::< - Option< - unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - u64, - bool, - ) -> lzma_ret, - >, + Option lzma_ret>, uintptr_t, >(Some( lzma_alone_decoder_init - as unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - u64, - bool, - ) -> lzma_ret, + as unsafe fn(*mut lzma_next_coder, *const lzma_allocator, u64, bool) -> lzma_ret, )) != (*next).init { lzma_next_end(next, allocator); } (*next).init = core::mem::transmute::< - Option< - unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - u64, - bool, - ) -> lzma_ret, - >, + Option lzma_ret>, uintptr_t, >(Some( lzma_alone_decoder_init - as unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - u64, - bool, - ) -> lzma_ret, + as unsafe fn(*mut lzma_next_coder, *const lzma_allocator, u64, bool) -> lzma_ret, )); let mut coder: *mut lzma_alone_coder = (*next).coder as *mut lzma_alone_coder; if coder.is_null() { @@ -229,7 +195,7 @@ pub(crate) unsafe extern "C" fn lzma_alone_decoder_init( (*next).coder = coder as *mut c_void; (*next).code = Some( alone_decode - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -241,12 +207,10 @@ pub(crate) unsafe extern "C" fn lzma_alone_decoder_init( lzma_action, ) -> lzma_ret, ); - (*next).end = Some( - alone_decoder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> (), - ); + (*next).end = + Some(alone_decoder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); (*next).memconfig = Some( - alone_decoder_memconfig - as unsafe extern "C" fn(*mut c_void, *mut u64, *mut u64, u64) -> lzma_ret, + alone_decoder_memconfig as unsafe fn(*mut c_void, *mut u64, *mut u64, u64) -> lzma_ret, ); (*coder).next = lzma_next_coder_s { coder: core::ptr::null_mut(), diff --git a/xz/src/common/alone_encoder.rs b/xz/src/common/alone_encoder.rs index 921236a8..81e0f5a8 100644 --- a/xz/src/common/alone_encoder.rs +++ b/xz/src/common/alone_encoder.rs @@ -11,10 +11,10 @@ pub type alone_encoder_seq = c_uint; pub const SEQ_CODE: alone_encoder_seq = 1; pub const SEQ_HEADER: alone_encoder_seq = 0; pub const ALONE_HEADER_SIZE: u32 = 1 + 4 + 8; -unsafe extern "C" fn alone_encode( +unsafe fn alone_encode( coder_ptr: *mut c_void, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, @@ -40,10 +40,12 @@ unsafe extern "C" fn alone_encode( (*coder).sequence = SEQ_CODE; } 1 => { - return (*coder).next.code.unwrap()( + debug_assert!((*coder).next.code.is_some()); + let code = (*coder).next.code.unwrap_unchecked(); + return code( (*coder).next.coder, allocator, - in_0, + input, in_pos, in_size, out, @@ -57,19 +59,19 @@ unsafe extern "C" fn alone_encode( } LZMA_OK } -unsafe extern "C" fn alone_encoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { +unsafe fn alone_encoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_alone_coder = coder_ptr as *mut lzma_alone_coder; lzma_next_end(::core::ptr::addr_of_mut!((*coder).next), allocator); crate::alloc::internal_free(coder as *mut c_void, allocator); } -unsafe extern "C" fn alone_encoder_init( +unsafe fn alone_encoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, options: *const lzma_options_lzma, ) -> lzma_ret { if core::mem::transmute::< Option< - unsafe extern "C" fn( + unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_options_lzma, @@ -78,7 +80,7 @@ unsafe extern "C" fn alone_encoder_init( uintptr_t, >(Some( alone_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_options_lzma, @@ -89,7 +91,7 @@ unsafe extern "C" fn alone_encoder_init( } (*next).init = core::mem::transmute::< Option< - unsafe extern "C" fn( + unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_options_lzma, @@ -98,7 +100,7 @@ unsafe extern "C" fn alone_encoder_init( uintptr_t, >(Some( alone_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_options_lzma, @@ -113,7 +115,7 @@ unsafe extern "C" fn alone_encoder_init( (*next).coder = coder as *mut c_void; (*next).code = Some( alone_encode - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -125,9 +127,8 @@ unsafe extern "C" fn alone_encoder_init( lzma_action, ) -> lzma_ret, ); - (*next).end = Some( - alone_encoder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> (), - ); + (*next).end = + Some(alone_encoder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); (*coder).next = lzma_next_coder_s { coder: core::ptr::null_mut(), id: LZMA_VLI_UNKNOWN, @@ -162,7 +163,9 @@ unsafe extern "C" fn alone_encoder_init( d += 1; } write32le( - (::core::ptr::addr_of_mut!((*coder).header) as *mut u8).offset(1), + &mut *((::core::ptr::addr_of_mut!((*coder).header) as *mut u8) + .add(1) + .cast::<[u8; 4]>()), d, ); core::ptr::write_bytes( @@ -177,7 +180,7 @@ unsafe extern "C" fn alone_encoder_init( id: LZMA_FILTER_LZMA1, init: Some( lzma_lzma_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -201,18 +204,18 @@ pub unsafe fn lzma_alone_encoder( strm: *mut lzma_stream, options: *const lzma_options_lzma, ) -> lzma_ret { - let ret_: lzma_ret = lzma_strm_init(strm); - if ret_ != LZMA_OK { - return ret_; + let ret: lzma_ret = lzma_strm_init(strm); + if ret != LZMA_OK { + return ret; } - let ret__0: lzma_ret = alone_encoder_init( + let ret: lzma_ret = alone_encoder_init( ::core::ptr::addr_of_mut!((*(*strm).internal).next), (*strm).allocator, options, ); - if ret__0 != LZMA_OK { + if ret != LZMA_OK { lzma_end(strm); - return ret__0; + return ret; } (*(*strm).internal).supported_actions[LZMA_RUN as usize] = true; (*(*strm).internal).supported_actions[LZMA_FINISH as usize] = true; diff --git a/xz/src/common/auto_decoder.rs b/xz/src/common/auto_decoder.rs index a46b44f0..6cc30ff5 100644 --- a/xz/src/common/auto_decoder.rs +++ b/xz/src/common/auto_decoder.rs @@ -12,10 +12,10 @@ pub struct lzma_auto_coder { pub flags: u32, pub sequence: auto_decoder_seq, } -unsafe extern "C" fn auto_decode( +unsafe fn auto_decode( coder_ptr: *mut c_void, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, @@ -24,32 +24,31 @@ unsafe extern "C" fn auto_decode( action: lzma_action, ) -> lzma_ret { let coder: *mut lzma_auto_coder = coder_ptr as *mut lzma_auto_coder; - let current_block_28: u64; match (*coder).sequence { 0 => { if *in_pos >= in_size { return LZMA_OK; } (*coder).sequence = SEQ_CODE; - if *in_0.offset(*in_pos as isize) == 0xfd { - let ret_: lzma_ret = lzma_stream_decoder_init( + if *input.offset(*in_pos as isize) == 0xfd { + let ret: lzma_ret = lzma_stream_decoder_init( ::core::ptr::addr_of_mut!((*coder).next), allocator, (*coder).memlimit, (*coder).flags, ); - if ret_ != LZMA_OK { - return ret_; + if ret != LZMA_OK { + return ret; } } else { - let ret__0: lzma_ret = lzma_alone_decoder_init( + let ret: lzma_ret = lzma_alone_decoder_init( ::core::ptr::addr_of_mut!((*coder).next), allocator, (*coder).memlimit, true, ); - if ret__0 != LZMA_OK { - return ret__0; + if ret != LZMA_OK { + return ret; } if (*coder).flags & LZMA_TELL_NO_CHECK as u32 != 0 { return LZMA_NO_CHECK; @@ -58,40 +57,28 @@ unsafe extern "C" fn auto_decode( return LZMA_GET_CHECK; } } - current_block_28 = 13935781298497728377; - } - 1 => { - current_block_28 = 13935781298497728377; - } - 2 => { - current_block_28 = 4647193646042868866; } + 1 | 2 => {} _ => return LZMA_PROG_ERROR, } - match current_block_28 { - 13935781298497728377 => { - let code = if let Some(code) = (*coder).next.code { - code - } else { - return LZMA_PROG_ERROR; - }; - let ret: lzma_ret = code( - (*coder).next.coder, - allocator, - in_0, - in_pos, - in_size, - out, - out_pos, - out_size, - action, - ); - if ret != LZMA_STREAM_END || (*coder).flags & LZMA_CONCATENATED as u32 == 0 { - return ret; - } - (*coder).sequence = SEQ_FINISH; + if (*coder).sequence != SEQ_FINISH { + debug_assert!((*coder).next.code.is_some()); + let code = (*coder).next.code.unwrap_unchecked(); + let ret: lzma_ret = code( + (*coder).next.coder, + allocator, + input, + in_pos, + in_size, + out, + out_pos, + out_size, + action, + ); + if ret != LZMA_STREAM_END || (*coder).flags & LZMA_CONCATENATED as u32 == 0 { + return ret; } - _ => {} + (*coder).sequence = SEQ_FINISH; } if *in_pos < in_size { return LZMA_DATA_ERROR; @@ -102,22 +89,38 @@ unsafe extern "C" fn auto_decode( LZMA_OK } } -unsafe extern "C" fn auto_decoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { +unsafe fn auto_decoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_auto_coder = coder_ptr as *mut lzma_auto_coder; lzma_next_end(::core::ptr::addr_of_mut!((*coder).next), allocator); crate::alloc::internal_free(coder as *mut c_void, allocator); } -extern "C" fn auto_decoder_get_check(coder_ptr: *const c_void) -> lzma_check { - return unsafe { - let coder: *const lzma_auto_coder = coder_ptr as *const lzma_auto_coder; - (if (*coder).next.get_check.is_none() { - LZMA_CHECK_NONE - } else { - (*coder).next.get_check.unwrap()((*coder).next.coder) - }) as lzma_check - }; +unsafe fn auto_decoder_get_check(coder_ptr: *const c_void) -> lzma_check { + let coder: *const lzma_auto_coder = coder_ptr as *const lzma_auto_coder; + match (*coder).next.get_check { + Some(get_check) => get_check((*coder).next.coder), + None => LZMA_CHECK_NONE, + } +} +pub unsafe fn lzma_auto_decoder(strm: *mut lzma_stream, memlimit: u64, flags: u32) -> lzma_ret { + let ret: lzma_ret = lzma_strm_init(strm); + if ret != LZMA_OK { + return ret; + } + let ret: lzma_ret = auto_decoder_init( + ::core::ptr::addr_of_mut!((*(*strm).internal).next), + (*strm).allocator, + memlimit, + flags, + ); + if ret != LZMA_OK { + lzma_end(strm); + return ret; + } + (*(*strm).internal).supported_actions[LZMA_RUN as usize] = true; + (*(*strm).internal).supported_actions[LZMA_FINISH as usize] = true; + LZMA_OK } -unsafe extern "C" fn auto_decoder_memconfig( +unsafe fn auto_decoder_memconfig( coder_ptr: *mut c_void, memusage: *mut u64, old_memlimit: *mut u64, @@ -125,13 +128,8 @@ unsafe extern "C" fn auto_decoder_memconfig( ) -> lzma_ret { let coder: *mut lzma_auto_coder = coder_ptr as *mut lzma_auto_coder; let mut ret: lzma_ret = LZMA_OK; - if (*coder).next.memconfig.is_some() { - ret = (*coder).next.memconfig.unwrap()( - (*coder).next.coder, - memusage, - old_memlimit, - new_memlimit, - ); + if let Some(memconfig) = (*coder).next.memconfig { + ret = memconfig((*coder).next.coder, memusage, old_memlimit, new_memlimit); } else { *memusage = LZMA_MEMUSAGE_BASE; *old_memlimit = (*coder).memlimit; @@ -145,42 +143,28 @@ unsafe extern "C" fn auto_decoder_memconfig( } ret } -unsafe extern "C" fn auto_decoder_init( +unsafe fn auto_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, memlimit: u64, flags: u32, ) -> lzma_ret { if core::mem::transmute::< - Option< - unsafe extern "C" fn(*mut lzma_next_coder, *const lzma_allocator, u64, u32) -> lzma_ret, - >, + Option lzma_ret>, uintptr_t, >(Some( auto_decoder_init - as unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - u64, - u32, - ) -> lzma_ret, + as unsafe fn(*mut lzma_next_coder, *const lzma_allocator, u64, u32) -> lzma_ret, )) != (*next).init { lzma_next_end(next, allocator); } (*next).init = core::mem::transmute::< - Option< - unsafe extern "C" fn(*mut lzma_next_coder, *const lzma_allocator, u64, u32) -> lzma_ret, - >, + Option lzma_ret>, uintptr_t, >(Some( auto_decoder_init - as unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - u64, - u32, - ) -> lzma_ret, + as unsafe fn(*mut lzma_next_coder, *const lzma_allocator, u64, u32) -> lzma_ret, )); if flags & !(LZMA_SUPPORTED_FLAGS as u32) != 0 { return LZMA_OPTIONS_ERROR; @@ -194,7 +178,7 @@ unsafe extern "C" fn auto_decoder_init( (*next).coder = coder as *mut c_void; (*next).code = Some( auto_decode - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -206,14 +190,10 @@ unsafe extern "C" fn auto_decoder_init( lzma_action, ) -> lzma_ret, ); - (*next).end = Some( - auto_decoder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> (), - ); - (*next).get_check = - Some(auto_decoder_get_check as unsafe extern "C" fn(*const c_void) -> lzma_check); + (*next).end = Some(auto_decoder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); + (*next).get_check = Some(auto_decoder_get_check as unsafe fn(*const c_void) -> lzma_check); (*next).memconfig = Some( - auto_decoder_memconfig - as unsafe extern "C" fn(*mut c_void, *mut u64, *mut u64, u64) -> lzma_ret, + auto_decoder_memconfig as unsafe fn(*mut c_void, *mut u64, *mut u64, u64) -> lzma_ret, ); (*coder).next = lzma_next_coder_s { coder: core::ptr::null_mut(), @@ -233,22 +213,3 @@ unsafe extern "C" fn auto_decoder_init( (*coder).sequence = SEQ_INIT; LZMA_OK } -pub unsafe fn lzma_auto_decoder(strm: *mut lzma_stream, memlimit: u64, flags: u32) -> lzma_ret { - let ret_: lzma_ret = lzma_strm_init(strm); - if ret_ != LZMA_OK { - return ret_; - } - let ret__0: lzma_ret = auto_decoder_init( - ::core::ptr::addr_of_mut!((*(*strm).internal).next), - (*strm).allocator, - memlimit, - flags, - ); - if ret__0 != LZMA_OK { - lzma_end(strm); - return ret__0; - } - (*(*strm).internal).supported_actions[LZMA_RUN as usize] = true; - (*(*strm).internal).supported_actions[LZMA_FINISH as usize] = true; - LZMA_OK -} diff --git a/xz/src/common/block_buffer_decoder.rs b/xz/src/common/block_buffer_decoder.rs index e992d72c..772ef699 100644 --- a/xz/src/common/block_buffer_decoder.rs +++ b/xz/src/common/block_buffer_decoder.rs @@ -2,7 +2,7 @@ use crate::types::*; pub unsafe fn lzma_block_buffer_decode( block: *mut lzma_block, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, @@ -10,7 +10,7 @@ pub unsafe fn lzma_block_buffer_decode( out_size: size_t, ) -> lzma_ret { if in_pos.is_null() - || in_0.is_null() && *in_pos != in_size + || input.is_null() && *in_pos != in_size || *in_pos > in_size || out_pos.is_null() || out.is_null() && *out_pos != out_size @@ -33,18 +33,14 @@ pub unsafe fn lzma_block_buffer_decode( let mut ret: lzma_ret = lzma_block_decoder_init(::core::ptr::addr_of_mut!(block_decoder), allocator, block); if ret == LZMA_OK { - let code = if let Some(code) = block_decoder.code { - code - } else { - lzma_next_end(::core::ptr::addr_of_mut!(block_decoder), allocator); - return LZMA_PROG_ERROR; - }; + debug_assert!(block_decoder.code.is_some()); + let code = block_decoder.code.unwrap_unchecked(); let in_start: size_t = *in_pos; let out_start: size_t = *out_pos; ret = code( block_decoder.coder, allocator, - in_0, + input, in_pos, in_size, out, diff --git a/xz/src/common/block_buffer_encoder.rs b/xz/src/common/block_buffer_encoder.rs index db4bb997..2d763eab 100644 --- a/xz/src/common/block_buffer_encoder.rs +++ b/xz/src/common/block_buffer_encoder.rs @@ -30,7 +30,7 @@ pub fn lzma_block_buffer_bound(uncompressed_size: size_t) -> size_t { } unsafe fn block_encode_uncompressed( block: *mut lzma_block, - in_0: *const u8, + input: *const u8, in_size: size_t, out: *mut u8, out_pos: *mut size_t, @@ -103,7 +103,7 @@ unsafe fn block_encode_uncompressed( *out.offset(*out_pos as isize) = ((copy_size - 1) & 0xff) as u8; *out_pos += 1; core::ptr::copy_nonoverlapping( - in_0.offset(in_pos as isize) as *const u8, + input.offset(in_pos as isize) as *const u8, out.offset(*out_pos as isize) as *mut u8, copy_size, ); @@ -117,7 +117,7 @@ unsafe fn block_encode_uncompressed( unsafe fn block_encode_normal( block: *mut lzma_block, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_size: size_t, out: *mut u8, out_pos: *mut size_t, @@ -154,17 +154,20 @@ unsafe fn block_encode_normal( ); if ret == LZMA_OK { let mut in_pos: size_t = 0; - ret = raw_encoder.code.unwrap()( - raw_encoder.coder, - allocator, - in_0, - ::core::ptr::addr_of_mut!(in_pos), - in_size, - out, - out_pos, - out_size, - LZMA_FINISH, - ); + ret = match raw_encoder.code { + Some(code) => code( + raw_encoder.coder, + allocator, + input, + ::core::ptr::addr_of_mut!(in_pos), + in_size, + out, + out_pos, + out_size, + LZMA_FINISH, + ), + None => LZMA_PROG_ERROR, + }; } lzma_next_end(::core::ptr::addr_of_mut!(raw_encoder), allocator); if ret == LZMA_STREAM_END { @@ -185,7 +188,7 @@ unsafe fn block_encode_normal( unsafe fn block_buffer_encode( block: *mut lzma_block, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_size: size_t, out: *mut u8, out_pos: *mut size_t, @@ -193,7 +196,7 @@ unsafe fn block_buffer_encode( try_to_compress: bool, ) -> lzma_ret { if block.is_null() - || in_0.is_null() && in_size != 0 + || input.is_null() && in_size != 0 || out.is_null() || out_pos.is_null() || *out_pos > out_size @@ -222,14 +225,14 @@ unsafe fn block_buffer_encode( } let mut ret: lzma_ret = LZMA_BUF_ERROR; if try_to_compress { - ret = block_encode_normal(block, allocator, in_0, in_size, out, out_pos, out_size); + ret = block_encode_normal(block, allocator, input, in_size, out, out_pos, out_size); } if ret != LZMA_OK { if ret != LZMA_BUF_ERROR { return ret; } let ret_: lzma_ret = - block_encode_uncompressed(block, in_0, in_size, out, out_pos, out_size); + block_encode_uncompressed(block, input, in_size, out, out_pos, out_size); if ret_ != LZMA_OK { return ret_; } @@ -249,7 +252,7 @@ unsafe fn block_buffer_encode( lzma_check_update( ::core::ptr::addr_of_mut!(check), (*block).check, - in_0, + input, in_size, ); lzma_check_finish(::core::ptr::addr_of_mut!(check), (*block).check); @@ -270,19 +273,19 @@ unsafe fn block_buffer_encode( pub unsafe fn lzma_block_buffer_encode( block: *mut lzma_block, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_size: size_t, out: *mut u8, out_pos: *mut size_t, out_size: size_t, ) -> lzma_ret { block_buffer_encode( - block, allocator, in_0, in_size, out, out_pos, out_size, true, + block, allocator, input, in_size, out, out_pos, out_size, true, ) } pub unsafe fn lzma_block_uncomp_encode( block: *mut lzma_block, - in_0: *const u8, + input: *const u8, in_size: size_t, out: *mut u8, out_pos: *mut size_t, @@ -291,7 +294,7 @@ pub unsafe fn lzma_block_uncomp_encode( block_buffer_encode( block, core::ptr::null(), - in_0, + input, in_size, out, out_pos, diff --git a/xz/src/common/block_decoder.rs b/xz/src/common/block_decoder.rs index 61f1c331..bebd5548 100644 --- a/xz/src/common/block_decoder.rs +++ b/xz/src/common/block_decoder.rs @@ -21,10 +21,10 @@ pub const SEQ_CODE: block_decoder_seq = 0; fn is_size_valid(size: lzma_vli, reference: lzma_vli) -> bool { reference == LZMA_VLI_UNKNOWN || reference == size } -unsafe extern "C" fn block_decode( +unsafe fn block_decode( coder_ptr: *mut c_void, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, @@ -33,7 +33,7 @@ unsafe extern "C" fn block_decode( action: lzma_action, ) -> lzma_ret { let coder: *mut lzma_block_coder = coder_ptr as *mut lzma_block_coder; - let current_block_40: u64 = match (*coder).sequence { + match (*coder).sequence { 0 => { let in_start: size_t = *in_pos; let out_start: size_t = *out_pos; @@ -53,10 +53,12 @@ unsafe extern "C" fn block_decode( } else { (*coder).uncompressed_limit - (*coder).uncompressed_size }) as size_t; - let ret: lzma_ret = (*coder).next.code.unwrap()( + debug_assert!((*coder).next.code.is_some()); + let code = (*coder).next.code.unwrap_unchecked(); + let ret: lzma_ret = code( (*coder).next.coder, allocator, - in_0, + input, in_pos, in_stop, out, @@ -104,19 +106,17 @@ unsafe extern "C" fn block_decode( (*(*coder).block).compressed_size = (*coder).compressed_size; (*(*coder).block).uncompressed_size = (*coder).uncompressed_size; (*coder).sequence = SEQ_PADDING; - 17473121293339793080 } - 1 => 17473121293339793080, - 2 => 9393557385011460022, + 1 | 2 => {} _ => return LZMA_PROG_ERROR, - }; - if current_block_40 == 17473121293339793080 { + } + if (*coder).sequence != SEQ_CHECK { while (*coder).compressed_size & 3 != 0 { if *in_pos >= in_size { return LZMA_OK; } (*coder).compressed_size += 1; - let byte = *in_0.offset(*in_pos as isize); + let byte = *input.offset(*in_pos as isize); *in_pos += 1; if byte != 0 { return LZMA_DATA_ERROR; @@ -135,7 +135,7 @@ unsafe extern "C" fn block_decode( } let check_size: size_t = lzma_check_size((*(*coder).block).check) as size_t; lzma_bufcpy( - in_0, + input, in_pos, in_size, ::core::ptr::addr_of_mut!((*(*coder).block).raw_check) as *mut u8, @@ -157,52 +157,32 @@ unsafe extern "C" fn block_decode( } LZMA_STREAM_END } -unsafe extern "C" fn block_decoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { +unsafe fn block_decoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_block_coder = coder_ptr as *mut lzma_block_coder; lzma_next_end(::core::ptr::addr_of_mut!((*coder).next), allocator); crate::alloc::internal_free(coder as *mut c_void, allocator); } -pub(crate) unsafe extern "C" fn lzma_block_decoder_init( +pub(crate) unsafe fn lzma_block_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, block: *mut lzma_block, ) -> lzma_ret { if core::mem::transmute::< - Option< - unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *mut lzma_block, - ) -> lzma_ret, - >, + Option lzma_ret>, uintptr_t, >(Some( lzma_block_decoder_init - as unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *mut lzma_block, - ) -> lzma_ret, + as unsafe fn(*mut lzma_next_coder, *const lzma_allocator, *mut lzma_block) -> lzma_ret, )) != (*next).init { lzma_next_end(next, allocator); } (*next).init = core::mem::transmute::< - Option< - unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *mut lzma_block, - ) -> lzma_ret, - >, + Option lzma_ret>, uintptr_t, >(Some( lzma_block_decoder_init - as unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *mut lzma_block, - ) -> lzma_ret, + as unsafe fn(*mut lzma_next_coder, *const lzma_allocator, *mut lzma_block) -> lzma_ret, )); if lzma_block_unpadded_size(block) == 0 || !((*block).uncompressed_size <= LZMA_VLI_MAX @@ -219,7 +199,7 @@ pub(crate) unsafe extern "C" fn lzma_block_decoder_init( (*next).coder = coder as *mut c_void; (*next).code = Some( block_decode - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -231,9 +211,8 @@ pub(crate) unsafe extern "C" fn lzma_block_decoder_init( lzma_action, ) -> lzma_ret, ); - (*next).end = Some( - block_decoder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> (), - ); + (*next).end = + Some(block_decoder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); (*coder).next = lzma_next_coder_s { coder: core::ptr::null_mut(), id: LZMA_VLI_UNKNOWN, diff --git a/xz/src/common/block_encoder.rs b/xz/src/common/block_encoder.rs index 2d4d068a..e72403d0 100644 --- a/xz/src/common/block_encoder.rs +++ b/xz/src/common/block_encoder.rs @@ -14,10 +14,10 @@ pub type block_encoder_seq = c_uint; pub const SEQ_CHECK: block_encoder_seq = 2; pub const SEQ_PADDING: block_encoder_seq = 1; pub const SEQ_CODE: block_encoder_seq = 0; -unsafe extern "C" fn block_encode( +unsafe fn block_encode( coder_ptr: *mut c_void, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, @@ -31,14 +31,16 @@ unsafe extern "C" fn block_encode( { return LZMA_DATA_ERROR; } - let current_block_34: u64 = match (*coder).sequence { + match (*coder).sequence { 0 => { let in_start: size_t = *in_pos; let out_start: size_t = *out_pos; - let ret: lzma_ret = (*coder).next.code.unwrap()( + debug_assert!((*coder).next.code.is_some()); + let code = (*coder).next.code.unwrap_unchecked(); + let ret: lzma_ret = code( (*coder).next.coder, allocator, - in_0, + input, in_pos, in_size, out, @@ -57,7 +59,7 @@ unsafe extern "C" fn block_encode( lzma_check_update( ::core::ptr::addr_of_mut!((*coder).check), (*(*coder).block).check, - in_0.offset(in_start as isize), + input.offset(in_start as isize), in_used, ); } @@ -67,13 +69,11 @@ unsafe extern "C" fn block_encode( (*(*coder).block).compressed_size = (*coder).compressed_size; (*(*coder).block).uncompressed_size = (*coder).uncompressed_size; (*coder).sequence = SEQ_PADDING; - 6470892831169497455 } - 1 => 6470892831169497455, - 2 => 47327340716975230, + 1 | 2 => {} _ => return LZMA_PROG_ERROR, - }; - if current_block_34 == 6470892831169497455 { + } + if (*coder).sequence != SEQ_CHECK { while (*coder).compressed_size & 3 != 0 { if *out_pos >= out_size { return LZMA_OK; @@ -110,12 +110,12 @@ unsafe extern "C" fn block_encode( ); LZMA_STREAM_END } -unsafe extern "C" fn block_encoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { +unsafe fn block_encoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_block_coder = coder_ptr as *mut lzma_block_coder; lzma_next_end(::core::ptr::addr_of_mut!((*coder).next), allocator); crate::alloc::internal_free(coder as *mut c_void, allocator); } -unsafe extern "C" fn block_encoder_update( +unsafe fn block_encoder_update( coder_ptr: *mut c_void, allocator: *const lzma_allocator, _filters: *const lzma_filter, @@ -131,47 +131,27 @@ unsafe extern "C" fn block_encoder_update( reversed_filters, ) } -pub(crate) unsafe extern "C" fn lzma_block_encoder_init( +pub(crate) unsafe fn lzma_block_encoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, block: *mut lzma_block, ) -> lzma_ret { if core::mem::transmute::< - Option< - unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *mut lzma_block, - ) -> lzma_ret, - >, + Option lzma_ret>, uintptr_t, >(Some( lzma_block_encoder_init - as unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *mut lzma_block, - ) -> lzma_ret, + as unsafe fn(*mut lzma_next_coder, *const lzma_allocator, *mut lzma_block) -> lzma_ret, )) != (*next).init { lzma_next_end(next, allocator); } (*next).init = core::mem::transmute::< - Option< - unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *mut lzma_block, - ) -> lzma_ret, - >, + Option lzma_ret>, uintptr_t, >(Some( lzma_block_encoder_init - as unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *mut lzma_block, - ) -> lzma_ret, + as unsafe fn(*mut lzma_next_coder, *const lzma_allocator, *mut lzma_block) -> lzma_ret, )); if block.is_null() { return LZMA_PROG_ERROR; @@ -194,7 +174,7 @@ pub(crate) unsafe extern "C" fn lzma_block_encoder_init( (*next).coder = coder as *mut c_void; (*next).code = Some( block_encode - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -206,12 +186,11 @@ pub(crate) unsafe extern "C" fn lzma_block_encoder_init( lzma_action, ) -> lzma_ret, ); - (*next).end = Some( - block_encoder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> (), - ); + (*next).end = + Some(block_encoder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); (*next).update = Some( block_encoder_update - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const lzma_filter, diff --git a/xz/src/common/block_header_decoder.rs b/xz/src/common/block_header_decoder.rs index 11f63431..f1b4c596 100644 --- a/xz/src/common/block_header_decoder.rs +++ b/xz/src/common/block_header_decoder.rs @@ -3,9 +3,9 @@ use crate::types::*; pub unsafe fn lzma_block_header_decode( block: *mut lzma_block, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, ) -> lzma_ret { - if block.is_null() || (*block).filters.is_null() || in_0.is_null() { + if block.is_null() || (*block).filters.is_null() || input.is_null() { return LZMA_PROG_ERROR; } let mut i: size_t = 0; @@ -18,27 +18,27 @@ pub unsafe fn lzma_block_header_decode( (*block).version = 1; } (*block).ignore_check = false as lzma_bool; - if (*in_0 as u32 + 1) * 4 != (*block).header_size || (*block).check > LZMA_CHECK_ID_MAX { + if (*input as u32 + 1) * 4 != (*block).header_size || (*block).check > LZMA_CHECK_ID_MAX { return LZMA_PROG_ERROR; } let in_size: size_t = ((*block).header_size - 4) as size_t; - if lzma_crc32(in_0, in_size, 0) != read32le(in_0.offset(in_size as isize)) { + if lzma_crc32(input, in_size, 0) != read32le(&*input.add(in_size).cast::<[u8; 4]>()) { return LZMA_DATA_ERROR; } - if *in_0.offset(1) & 0x3c != 0 { + if *input.offset(1) & 0x3c != 0 { return LZMA_OPTIONS_ERROR; } let mut in_pos: size_t = 2; - if *in_0.offset(1) & 0x40 != 0 { - let ret_: lzma_ret = lzma_vli_decode( + if *input.offset(1) & 0x40 != 0 { + let ret: lzma_ret = lzma_vli_decode( ::core::ptr::addr_of_mut!((*block).compressed_size), core::ptr::null_mut(), - in_0, + input, ::core::ptr::addr_of_mut!(in_pos), in_size, ); - if ret_ != LZMA_OK { - return ret_; + if ret != LZMA_OK { + return ret; } if lzma_block_unpadded_size(block) == 0 { return LZMA_DATA_ERROR; @@ -46,27 +46,27 @@ pub unsafe fn lzma_block_header_decode( } else { (*block).compressed_size = LZMA_VLI_UNKNOWN; } - if *in_0.offset(1) & 0x80 != 0 { - let ret__0: lzma_ret = lzma_vli_decode( + if *input.offset(1) & 0x80 != 0 { + let ret: lzma_ret = lzma_vli_decode( ::core::ptr::addr_of_mut!((*block).uncompressed_size), core::ptr::null_mut(), - in_0, + input, ::core::ptr::addr_of_mut!(in_pos), in_size, ); - if ret__0 != LZMA_OK { - return ret__0; + if ret != LZMA_OK { + return ret; } } else { (*block).uncompressed_size = LZMA_VLI_UNKNOWN; } - let filter_count: size_t = ((u32::from(*in_0.offset(1)) & 3) + 1) as size_t; + let filter_count: size_t = ((u32::from(*input.offset(1)) & 3) + 1) as size_t; let mut i_0: size_t = 0; while i_0 < filter_count { let ret: lzma_ret = lzma_filter_flags_decode( (*block).filters.offset(i_0 as isize) as *mut lzma_filter, allocator, - in_0, + input, ::core::ptr::addr_of_mut!(in_pos), in_size, ); @@ -77,7 +77,7 @@ pub unsafe fn lzma_block_header_decode( i_0 += 1; } while in_pos < in_size { - if *in_0.offset(in_pos as isize) != 0 { + if *input.offset(in_pos as isize) != 0 { lzma_filters_free((*block).filters, allocator); return LZMA_OPTIONS_ERROR; } diff --git a/xz/src/common/block_header_encoder.rs b/xz/src/common/block_header_encoder.rs index f7ff43e4..301b887f 100644 --- a/xz/src/common/block_header_encoder.rs +++ b/xz/src/common/block_header_encoder.rs @@ -106,6 +106,9 @@ pub unsafe fn lzma_block_header_encode(block: *const lzma_block, out: *mut u8) - 0 as u8, out_size - out_pos, ); - write32le(out.offset(out_size as isize), lzma_crc32(out, out_size, 0)); + write32le( + &mut *out.add(out_size).cast::<[u8; 4]>(), + lzma_crc32(out, out_size, 0), + ); LZMA_OK } diff --git a/xz/src/common/common.rs b/xz/src/common/common.rs index ca6108f6..446769dc 100644 --- a/xz/src/common/common.rs +++ b/xz/src/common/common.rs @@ -1,9 +1,10 @@ +use crate::alloc::allocator_or_rust; use crate::types::*; #[cfg(all(target_family = "wasm", target_os = "unknown"))] -use std::alloc::{alloc, alloc_zeroed, dealloc, Layout}; +use std::alloc::{Layout, alloc, alloc_zeroed, dealloc}; #[cfg(not(all(target_family = "wasm", target_os = "unknown")))] -extern "C" { +unsafe extern "C" { fn malloc(__size: size_t) -> *mut c_void; fn calloc(__count: size_t, __size: size_t) -> *mut c_void; fn free(_: *mut c_void); @@ -15,41 +16,42 @@ const LZMA_ALLOC_ALIGN: usize = 16; const LZMA_ALLOC_HEADER_SIZE: usize = 16; #[cfg(all(target_family = "wasm", target_os = "unknown"))] -unsafe fn rust_alloc_layout(size: usize) -> Option { +fn rust_alloc_layout(size: usize) -> Option { let total_size = size.checked_add(LZMA_ALLOC_HEADER_SIZE)?; Layout::from_size_align(total_size, LZMA_ALLOC_ALIGN).ok() } #[cfg(all(target_family = "wasm", target_os = "unknown"))] -unsafe fn malloc(size: size_t) -> *mut c_void { - let layout = match rust_alloc_layout(size as usize) { - Some(layout) => layout, - None => return core::ptr::null_mut(), +fn malloc(size: size_t) -> *mut c_void { + let Some(layout) = rust_alloc_layout(size as usize) else { + return core::ptr::null_mut(); }; - let base = alloc(layout); + let base = unsafe { alloc(layout) }; if base.is_null() { return core::ptr::null_mut(); } - *(base as *mut usize) = layout.size(); - base.add(LZMA_ALLOC_HEADER_SIZE) as *mut c_void + unsafe { + *(base as *mut usize) = layout.size(); + base.add(LZMA_ALLOC_HEADER_SIZE) as *mut c_void + } } #[cfg(all(target_family = "wasm", target_os = "unknown"))] -unsafe fn calloc(count: size_t, size: size_t) -> *mut c_void { - let size = match (count as usize).checked_mul(size as usize) { - Some(size) => size, - None => return core::ptr::null_mut(), +fn calloc(count: size_t, size: size_t) -> *mut c_void { + let Some(size) = (count as usize).checked_mul(size as usize) else { + return core::ptr::null_mut(); }; - let layout = match rust_alloc_layout(size) { - Some(layout) => layout, - None => return core::ptr::null_mut(), + let Some(layout) = rust_alloc_layout(size) else { + return core::ptr::null_mut(); }; - let base = alloc_zeroed(layout); + let base = unsafe { alloc_zeroed(layout) }; if base.is_null() { return core::ptr::null_mut(); } - *(base as *mut usize) = layout.size(); - base.add(LZMA_ALLOC_HEADER_SIZE) as *mut c_void + unsafe { + *(base as *mut usize) = layout.size(); + base.add(LZMA_ALLOC_HEADER_SIZE) as *mut c_void + } } #[cfg(all(target_family = "wasm", target_os = "unknown"))] @@ -57,14 +59,14 @@ unsafe fn free(ptr: *mut c_void) { if ptr.is_null() { return; } - let base = (ptr as *mut u8).sub(LZMA_ALLOC_HEADER_SIZE); - let total_size = *(base as *const usize); - let layout = Layout::from_size_align_unchecked(total_size, LZMA_ALLOC_ALIGN); - dealloc(base, layout); + let base = unsafe { (ptr as *mut u8).sub(LZMA_ALLOC_HEADER_SIZE) }; + let total_size = unsafe { *(base as *const usize) }; + let layout = unsafe { Layout::from_size_align_unchecked(total_size, LZMA_ALLOC_ALIGN) }; + unsafe { dealloc(base, layout) }; } pub const LZMA_VERSION_MAJOR: u32 = 5; pub const LZMA_VERSION_MINOR: u32 = 8; -pub const LZMA_VERSION_PATCH: u32 = 2; +pub const LZMA_VERSION_PATCH: u32 = 3; pub const LZMA_VERSION_STABILITY: u32 = LZMA_VERSION_STABILITY_STABLE; pub const LZMA_VERSION_STABILITY_STABLE: u32 = 2; pub const LZMA_VERSION: c_uint = LZMA_VERSION_MAJOR * 10000000 @@ -76,51 +78,62 @@ pub fn lzma_version_number() -> u32 { LZMA_VERSION as u32 } pub fn lzma_version_string() -> *const c_char { - crate::c_str!("5.8.2") + crate::c_str!("5.8.3") } pub unsafe fn lzma_alloc(mut size: size_t, allocator: *const lzma_allocator) -> *mut c_void { if size == 0 { size = 1; } - let mut ptr: *mut c_void = core::ptr::null_mut(); - if !allocator.is_null() && (*allocator).alloc.is_some() { - ptr = (*allocator).alloc.unwrap()((*allocator).opaque, 1, size); + let allocator = allocator_or_rust(allocator); + if let Some(alloc) = (*allocator).alloc { + alloc((*allocator).opaque, 1, size) } else { - ptr = malloc(size); + malloc(size) } - ptr } pub unsafe fn lzma_alloc_zero(mut size: size_t, allocator: *const lzma_allocator) -> *mut c_void { if size == 0 { size = 1; } - let mut ptr: *mut c_void = core::ptr::null_mut(); - if !allocator.is_null() && (*allocator).alloc.is_some() { - ptr = (*allocator).alloc.unwrap()((*allocator).opaque, 1, size); - if !ptr.is_null() { - core::ptr::write_bytes(ptr as *mut u8, 0, size); - } + let allocator = allocator_or_rust(allocator); + let ptr = if let Some(alloc) = (*allocator).alloc { + alloc((*allocator).opaque, 1, size) } else { - ptr = calloc(1, size); + calloc(1, size) + }; + if !ptr.is_null() { + core::ptr::write_bytes(ptr as *mut u8, 0, size); } ptr } -pub unsafe extern "C" fn lzma_free(ptr: *mut c_void, allocator: *const lzma_allocator) { - if !allocator.is_null() && (*allocator).free.is_some() { - (*allocator).free.unwrap()((*allocator).opaque, ptr); +pub unsafe fn lzma_free(ptr: *mut c_void, allocator: *const lzma_allocator) { + let allocator = allocator_or_rust(allocator); + if let Some(free_func) = (*allocator).free { + free_func((*allocator).opaque, ptr); } else { free(ptr); }; } +#[inline] +pub unsafe fn lzma_alloc_object(allocator: *const lzma_allocator) -> *mut T { + debug_assert!(core::mem::align_of::() <= 16); + lzma_alloc(core::mem::size_of::() as size_t, allocator) as *mut T +} pub unsafe fn lzma_bufcpy( - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, out_pos: *mut size_t, out_size: size_t, ) -> size_t { - debug_assert!(!in_0.is_null() || *in_pos == in_size); + if *in_pos > in_size || *out_pos > out_size { + return 0; + } + if (input.is_null() && *in_pos != in_size) || (out.is_null() && *out_pos != out_size) { + return 0; + } + debug_assert!(!input.is_null() || *in_pos == in_size); debug_assert!(!out.is_null() || *out_pos == out_size); debug_assert!(*in_pos <= in_size); debug_assert!(*out_pos <= out_size); @@ -134,7 +147,7 @@ pub unsafe fn lzma_bufcpy( }; if copy_size > 0 { core::ptr::copy_nonoverlapping( - in_0.offset(*in_pos as isize) as *const u8, + input.offset(*in_pos as isize) as *const u8, out.offset(*out_pos as isize) as *mut u8, copy_size, ); @@ -153,10 +166,10 @@ pub unsafe fn lzma_next_filter_init( } (*next).init = core::mem::transmute::((*filters).init); (*next).id = (*filters).id; - if (*filters).init.is_none() { - LZMA_OK + if let Some(init) = (*filters).init { + init(next, allocator, filters) } else { - (*filters).init.unwrap()(next, allocator, filters) + LZMA_OK } } pub unsafe fn lzma_next_filter_update( @@ -170,11 +183,8 @@ pub unsafe fn lzma_next_filter_update( if (*reversed_filters).id == LZMA_VLI_UNKNOWN { return LZMA_OK; } - let update = if let Some(update) = (*next).update { - update - } else { - return LZMA_PROG_ERROR; - }; + debug_assert!((*next).update.is_some()); + let update = (*next).update.unwrap_unchecked(); update( (*next).coder, allocator, @@ -183,32 +193,33 @@ pub unsafe fn lzma_next_filter_update( ) } pub unsafe fn lzma_next_end(next: *mut lzma_next_coder, allocator: *const lzma_allocator) { - if (*next).init != 0 { - if (*next).end.is_some() { - (*next).end.unwrap()((*next).coder, allocator); - } else { - lzma_free((*next).coder, allocator); - } - *next = lzma_next_coder_s { - coder: core::ptr::null_mut(), - id: LZMA_VLI_UNKNOWN, - init: 0, - code: None, - end: None, - get_progress: None, - get_check: None, - memconfig: None, - update: None, - set_out_limit: None, - }; + if (*next).init == 0 { + return; + } + if let Some(end) = (*next).end { + end((*next).coder, allocator); + } else { + lzma_free((*next).coder, allocator); } + *next = lzma_next_coder_s { + coder: core::ptr::null_mut(), + id: LZMA_VLI_UNKNOWN, + init: 0, + code: None, + end: None, + get_progress: None, + get_check: None, + memconfig: None, + update: None, + set_out_limit: None, + }; } pub unsafe fn lzma_strm_init(strm: *mut lzma_stream) -> lzma_ret { if strm.is_null() { return LZMA_PROG_ERROR; } if (*strm).internal.is_null() { - (*strm).internal = crate::alloc::internal_alloc_object::((*strm).allocator); + (*strm).internal = lzma_alloc_object::((*strm).allocator); if (*strm).internal.is_null() { return LZMA_MEM_ERROR; } @@ -300,7 +311,9 @@ pub unsafe fn lzma_code(strm: *mut lzma_stream, action: lzma_action) -> lzma_ret } let mut in_pos: size_t = 0; let mut out_pos: size_t = 0; - let mut ret: lzma_ret = (*(*strm).internal).next.code.unwrap()( + debug_assert!((*(*strm).internal).next.code.is_some()); + let code = (*(*strm).internal).next.code.unwrap_unchecked(); + let mut ret: lzma_ret = code( (*(*strm).internal).next.coder, (*strm).allocator, (*strm).next_in, @@ -322,7 +335,6 @@ pub unsafe fn lzma_code(strm: *mut lzma_stream, action: lzma_action) -> lzma_ret (*strm).total_out = (*strm).total_out.wrapping_add(out_pos as u64); } (*(*strm).internal).avail_in = (*strm).avail_in; - let current_block_49: u64; match ret { 0 => { if out_pos == 0 && in_pos == 0 { @@ -334,19 +346,16 @@ pub unsafe fn lzma_code(strm: *mut lzma_stream, action: lzma_action) -> lzma_ret } else { (*(*strm).internal).allow_buf_error = false; } - current_block_49 = 12556861819962772176; } 101 => { (*(*strm).internal).allow_buf_error = false; ret = LZMA_OK; - current_block_49 = 12556861819962772176; } 12 => { (*(*strm).internal).allow_buf_error = false; if (*(*strm).internal).sequence == ISEQ_FINISH { (*(*strm).internal).sequence = ISEQ_RUN; } - current_block_49 = 12556861819962772176; } 1 => { if (*(*strm).internal).sequence == ISEQ_SYNC_FLUSH @@ -357,21 +366,14 @@ pub unsafe fn lzma_code(strm: *mut lzma_stream, action: lzma_action) -> lzma_ret } else { (*(*strm).internal).sequence = ISEQ_END; } - current_block_49 = 16143107162343188004; + (*(*strm).internal).allow_buf_error = false; } 2 | 3 | 4 | 6 => { - current_block_49 = 16143107162343188004; + (*(*strm).internal).allow_buf_error = false; } _ => { (*(*strm).internal).sequence = ISEQ_ERROR; - current_block_49 = 12556861819962772176; - } - } - match current_block_49 { - 16143107162343188004 => { - (*(*strm).internal).allow_buf_error = false; } - _ => {} } ret } @@ -381,7 +383,7 @@ pub unsafe fn lzma_end(strm: *mut lzma_stream) { ::core::ptr::addr_of_mut!((*(*strm).internal).next), (*strm).allocator, ); - crate::alloc::internal_free((*strm).internal as *mut c_void, (*strm).allocator); + lzma_free((*strm).internal as *mut c_void, (*strm).allocator); (*strm).internal = core::ptr::null_mut(); } } @@ -400,77 +402,82 @@ pub unsafe fn lzma_get_progress( return; } - if (*(*strm).internal).next.get_progress.is_some() { - (*(*strm).internal).next.get_progress.unwrap()( - (*(*strm).internal).next.coder, - progress_in, - progress_out, - ); + if let Some(get_progress) = (*(*strm).internal).next.get_progress { + get_progress((*(*strm).internal).next.coder, progress_in, progress_out); } else { *progress_in = (*strm).total_in; *progress_out = (*strm).total_out; }; } pub fn lzma_get_check(strm: *const lzma_stream) -> lzma_check { - return unsafe { + unsafe { if strm.is_null() || (*strm).internal.is_null() { return LZMA_CHECK_NONE; } - if (*(*strm).internal).next.get_check.is_none() { - return LZMA_CHECK_NONE; + if let Some(get_check) = (*(*strm).internal).next.get_check { + get_check((*(*strm).internal).next.coder) + } else { + LZMA_CHECK_NONE } - (*(*strm).internal).next.get_check.unwrap()((*(*strm).internal).next.coder) - }; + } } pub fn lzma_memusage(strm: *const lzma_stream) -> u64 { - return unsafe { + unsafe { let mut memusage: u64 = 0; let mut old_memlimit: u64 = 0; - if strm.is_null() - || (*strm).internal.is_null() - || (*(*strm).internal).next.memconfig.is_none() - || (*(*strm).internal).next.memconfig.unwrap()( - (*(*strm).internal).next.coder, - ::core::ptr::addr_of_mut!(memusage), - ::core::ptr::addr_of_mut!(old_memlimit), - 0, - ) != LZMA_OK + if strm.is_null() || (*strm).internal.is_null() { + return 0; + } + let Some(memconfig) = (*(*strm).internal).next.memconfig else { + return 0; + }; + if memconfig( + (*(*strm).internal).next.coder, + ::core::ptr::addr_of_mut!(memusage), + ::core::ptr::addr_of_mut!(old_memlimit), + 0, + ) != LZMA_OK { return 0; } memusage - }; + } } pub fn lzma_memlimit_get(strm: *const lzma_stream) -> u64 { - return unsafe { + unsafe { let mut old_memlimit: u64 = 0; let mut memusage: u64 = 0; - if strm.is_null() - || (*strm).internal.is_null() - || (*(*strm).internal).next.memconfig.is_none() - || (*(*strm).internal).next.memconfig.unwrap()( - (*(*strm).internal).next.coder, - ::core::ptr::addr_of_mut!(memusage), - ::core::ptr::addr_of_mut!(old_memlimit), - 0, - ) != LZMA_OK + if strm.is_null() || (*strm).internal.is_null() { + return 0; + } + let Some(memconfig) = (*(*strm).internal).next.memconfig else { + return 0; + }; + if memconfig( + (*(*strm).internal).next.coder, + ::core::ptr::addr_of_mut!(memusage), + ::core::ptr::addr_of_mut!(old_memlimit), + 0, + ) != LZMA_OK { return 0; } old_memlimit - }; + } } pub unsafe fn lzma_memlimit_set(strm: *mut lzma_stream, mut new_memlimit: u64) -> lzma_ret { let mut old_memlimit: u64 = 0; let mut memusage: u64 = 0; - if strm.is_null() || (*strm).internal.is_null() || (*(*strm).internal).next.memconfig.is_none() - { + if strm.is_null() || (*strm).internal.is_null() { return LZMA_PROG_ERROR; } + let Some(memconfig) = (*(*strm).internal).next.memconfig else { + return LZMA_PROG_ERROR; + }; if new_memlimit == 0 { new_memlimit = 1; } - (*(*strm).internal).next.memconfig.unwrap()( + memconfig( (*(*strm).internal).next.coder, ::core::ptr::addr_of_mut!(memusage), ::core::ptr::addr_of_mut!(old_memlimit), diff --git a/xz/src/common/common_types.rs b/xz/src/common/common_types.rs new file mode 100644 index 00000000..e6a4e9aa --- /dev/null +++ b/xz/src/common/common_types.rs @@ -0,0 +1,87 @@ +use crate::types::{ + c_uint, c_void, lzma_action, lzma_allocator, lzma_check, lzma_filter, lzma_ret, lzma_vli, + size_t, uintptr_t, +}; + +pub type lzma_end_function = Option ()>; +pub type lzma_code_function = Option< + unsafe fn( + *mut c_void, + *const lzma_allocator, + *const u8, + *mut size_t, + size_t, + *mut u8, + *mut size_t, + size_t, + lzma_action, + ) -> lzma_ret, +>; + +pub type lzma_next_coder = lzma_next_coder_s; + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct lzma_next_coder_s { + pub coder: *mut c_void, + pub id: lzma_vli, + pub init: uintptr_t, + pub code: lzma_code_function, + pub end: lzma_end_function, + pub get_progress: Option ()>, + pub get_check: Option lzma_check>, + pub memconfig: Option lzma_ret>, + pub update: Option< + unsafe fn( + *mut c_void, + *const lzma_allocator, + *const lzma_filter, + *const lzma_filter, + ) -> lzma_ret, + >, + pub set_out_limit: Option lzma_ret>, +} + +pub type lzma_init_function = Option< + unsafe fn(*mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info) -> lzma_ret, +>; + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct lzma_filter_info_s { + pub id: lzma_vli, + pub init: lzma_init_function, + pub options: *mut c_void, +} + +pub type lzma_filter_info = lzma_filter_info_s; + +pub type lzma_internal_sequence = c_uint; +pub const ISEQ_RUN: lzma_internal_sequence = 0; +pub const ISEQ_SYNC_FLUSH: lzma_internal_sequence = 1; +pub const ISEQ_FULL_FLUSH: lzma_internal_sequence = 2; +pub const ISEQ_FINISH: lzma_internal_sequence = 3; +pub const ISEQ_FULL_BARRIER: lzma_internal_sequence = 4; +pub const ISEQ_END: lzma_internal_sequence = 5; +pub const ISEQ_ERROR: lzma_internal_sequence = 6; + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct lzma_internal_s { + pub next: lzma_next_coder, + pub sequence: lzma_internal_sequence, + pub avail_in: size_t, + pub supported_actions: [bool; 5], + pub allow_buf_error: bool, +} + +pub type lzma_internal = lzma_internal_s; + +pub const LZMA_MEMUSAGE_BASE: u64 = 1 << 15; +pub const LZMA_SUPPORTED_FLAGS: c_uint = crate::types::LZMA_TELL_NO_CHECK + | crate::types::LZMA_TELL_UNSUPPORTED_CHECK + | crate::types::LZMA_TELL_ANY_CHECK + | crate::types::LZMA_IGNORE_CHECK + | crate::types::LZMA_CONCATENATED + | crate::types::LZMA_FAIL_FAST; +pub const LZMA_THREADS_MAX: u32 = 16384; diff --git a/xz/src/common/easy_buffer_encoder.rs b/xz/src/common/easy_buffer_encoder.rs index a77ccfd4..551dac1c 100644 --- a/xz/src/common/easy_buffer_encoder.rs +++ b/xz/src/common/easy_buffer_encoder.rs @@ -5,7 +5,7 @@ pub unsafe fn lzma_easy_buffer_encode( preset: u32, check: lzma_check, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_size: size_t, out: *mut u8, out_pos: *mut size_t, @@ -20,7 +20,7 @@ pub unsafe fn lzma_easy_buffer_encode( ::core::ptr::addr_of_mut!(opt_easy.filters) as *mut lzma_filter, check, allocator, - in_0, + input, in_size, out, out_pos, diff --git a/xz/src/common/file_info.rs b/xz/src/common/file_info.rs index 4b88e4d8..312566be 100644 --- a/xz/src/common/file_info.rs +++ b/xz/src/common/file_info.rs @@ -37,12 +37,12 @@ pub const SEQ_PADDING_SEEK: file_info_seq = 1; pub const SEQ_MAGIC_BYTES: file_info_seq = 0; unsafe fn fill_temp( coder: *mut lzma_file_info_coder, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, ) -> bool { (*coder).file_cur_pos += lzma_bufcpy( - in_0, + input, in_pos, in_size, ::core::ptr::addr_of_mut!((*coder).temp) as *mut u8, @@ -104,7 +104,7 @@ unsafe fn get_padding_size(buf: *const u8, mut buf_size: size_t) -> size_t { let mut padding: size_t = 0; while buf_size > 0 && { buf_size -= 1; - *buf.offset(buf_size as isize) == 0 + *buf.add(buf_size) == 0 } { padding += 1; } @@ -119,16 +119,19 @@ fn hide_format_error(mut ret: lzma_ret) -> lzma_ret { unsafe fn decode_index( coder: *mut lzma_file_info_coder, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, update_file_cur_pos: bool, ) -> lzma_ret { let in_start: size_t = *in_pos; - let ret: lzma_ret = (*coder).index_decoder.code.unwrap()( + let Some(code) = (*coder).index_decoder.code else { + return LZMA_PROG_ERROR; + }; + let ret: lzma_ret = code( (*coder).index_decoder.coder, allocator, - in_0, + input, in_pos, in_size, core::ptr::null_mut(), @@ -142,10 +145,10 @@ unsafe fn decode_index( } ret } -unsafe extern "C" fn file_info_decode( +unsafe fn file_info_decode( coder_ptr: *mut c_void, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, mut in_size: size_t, _out: *mut u8, @@ -164,7 +167,7 @@ unsafe extern "C" fn file_info_decode( if (*coder).file_size < LZMA_STREAM_HEADER_SIZE as u64 { return LZMA_FORMAT_ERROR; } - if fill_temp(coder, in_0, in_pos, in_size) { + if fill_temp(coder, input, in_pos, in_size) { return LZMA_OK; } let ret: lzma_ret = lzma_stream_header_decode( @@ -188,13 +191,11 @@ unsafe extern "C" fn file_info_decode( } } SEQ_PADDING_DECODE => { - if fill_temp(coder, in_0, in_pos, in_size) { + if fill_temp(coder, input, in_pos, in_size) { return LZMA_OK; } - let new_padding: size_t = get_padding_size( - ::core::ptr::addr_of_mut!((*coder).temp) as *mut u8, - (*coder).temp_size, - ) as size_t; + let new_padding: size_t = + get_padding_size((*coder).temp.as_ptr(), (*coder).temp_size); (*coder).stream_padding += new_padding as lzma_vli; (*coder).file_target_pos -= new_padding as u64; if new_padding == (*coder).temp_size { @@ -215,7 +216,7 @@ unsafe extern "C" fn file_info_decode( } } SEQ_FOOTER => { - if fill_temp(coder, in_0, in_pos, in_size) { + if fill_temp(coder, input, in_pos, in_size) { return LZMA_OK; } (*coder).file_target_pos -= LZMA_STREAM_HEADER_SIZE as u64; @@ -282,7 +283,7 @@ unsafe extern "C" fn file_info_decode( if (in_size - *in_pos) as lzma_vli > (*coder).index_remaining { in_stop = *in_pos + (*coder).index_remaining as size_t; } - decode_index(coder, allocator, in_0, in_pos, in_stop, true) + decode_index(coder, allocator, input, in_pos, in_stop, true) }; match ret { LZMA_OK => { @@ -329,7 +330,7 @@ unsafe extern "C" fn file_info_decode( } } SEQ_HEADER_DECODE => { - if fill_temp(coder, in_0, in_pos, in_size) { + if fill_temp(coder, input, in_pos, in_size) { return LZMA_OK; } (*coder).file_target_pos -= LZMA_STREAM_HEADER_SIZE as u64; @@ -391,7 +392,7 @@ unsafe extern "C" fn file_info_decode( } } } -unsafe extern "C" fn file_info_decoder_memconfig( +unsafe fn file_info_decoder_memconfig( coder_ptr: *mut c_void, memusage: *mut u64, old_memlimit: *mut u64, @@ -407,7 +408,10 @@ unsafe extern "C" fn file_info_decoder_memconfig( this_index_memusage = lzma_index_memused((*coder).this_index); } else if (*coder).sequence == SEQ_INDEX_DECODE { let mut dummy: u64 = 0; - if (*coder).index_decoder.memconfig.unwrap()( + let Some(memconfig) = (*coder).index_decoder.memconfig else { + return LZMA_PROG_ERROR; + }; + if memconfig( (*coder).index_decoder.coder, ::core::ptr::addr_of_mut!(this_index_memusage), ::core::ptr::addr_of_mut!(dummy), @@ -430,7 +434,10 @@ unsafe extern "C" fn file_info_decoder_memconfig( let idec_new_memlimit: u64 = new_memlimit - combined_index_memusage; let mut dummy1: u64 = 0; let mut dummy2: u64 = 0; - if (*coder).index_decoder.memconfig.unwrap()( + let Some(memconfig) = (*coder).index_decoder.memconfig else { + return LZMA_PROG_ERROR; + }; + if memconfig( (*coder).index_decoder.coder, ::core::ptr::addr_of_mut!(dummy1), ::core::ptr::addr_of_mut!(dummy2), @@ -444,17 +451,14 @@ unsafe extern "C" fn file_info_decoder_memconfig( } LZMA_OK } -unsafe extern "C" fn file_info_decoder_end( - coder_ptr: *mut c_void, - allocator: *const lzma_allocator, -) { +unsafe fn file_info_decoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_file_info_coder = coder_ptr as *mut lzma_file_info_coder; lzma_next_end(::core::ptr::addr_of_mut!((*coder).index_decoder), allocator); lzma_index_end((*coder).this_index, allocator); lzma_index_end((*coder).combined_index, allocator); crate::alloc::internal_free(coder as *mut c_void, allocator); } -unsafe extern "C" fn lzma_file_info_decoder_init( +unsafe fn lzma_file_info_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, seek_pos: *mut u64, @@ -464,7 +468,7 @@ unsafe extern "C" fn lzma_file_info_decoder_init( ) -> lzma_ret { if core::mem::transmute::< Option< - unsafe extern "C" fn( + unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *mut u64, @@ -476,7 +480,7 @@ unsafe extern "C" fn lzma_file_info_decoder_init( uintptr_t, >(Some( lzma_file_info_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *mut u64, @@ -490,7 +494,7 @@ unsafe extern "C" fn lzma_file_info_decoder_init( } (*next).init = core::mem::transmute::< Option< - unsafe extern "C" fn( + unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *mut u64, @@ -502,7 +506,7 @@ unsafe extern "C" fn lzma_file_info_decoder_init( uintptr_t, >(Some( lzma_file_info_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *mut u64, @@ -523,7 +527,7 @@ unsafe extern "C" fn lzma_file_info_decoder_init( (*next).coder = coder as *mut c_void; (*next).code = Some( file_info_decode - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -535,12 +539,11 @@ unsafe extern "C" fn lzma_file_info_decoder_init( lzma_action, ) -> lzma_ret, ); - (*next).end = Some( - file_info_decoder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> (), - ); + (*next).end = + Some(file_info_decoder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); (*next).memconfig = Some( file_info_decoder_memconfig - as unsafe extern "C" fn(*mut c_void, *mut u64, *mut u64, u64) -> lzma_ret, + as unsafe fn(*mut c_void, *mut u64, *mut u64, u64) -> lzma_ret, ); (*coder).index_decoder = lzma_next_coder_s { coder: core::ptr::null_mut(), diff --git a/xz/src/common/filter_buffer_decoder.rs b/xz/src/common/filter_buffer_decoder.rs index ebaf7e50..b7ede862 100644 --- a/xz/src/common/filter_buffer_decoder.rs +++ b/xz/src/common/filter_buffer_decoder.rs @@ -2,14 +2,14 @@ use crate::types::*; pub unsafe fn lzma_raw_buffer_decode( filters: *const lzma_filter, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, out_pos: *mut size_t, out_size: size_t, ) -> lzma_ret { - if in_0.is_null() + if input.is_null() || in_pos.is_null() || *in_pos > in_size || out.is_null() @@ -30,22 +30,18 @@ pub unsafe fn lzma_raw_buffer_decode( update: None, set_out_limit: None, }; - let ret_: lzma_ret = lzma_raw_decoder_init(::core::ptr::addr_of_mut!(next), allocator, filters); - if ret_ != LZMA_OK { - return ret_; + let ret: lzma_ret = lzma_raw_decoder_init(::core::ptr::addr_of_mut!(next), allocator, filters); + if ret != LZMA_OK { + return ret; } - let code = if let Some(code) = next.code { - code - } else { - lzma_next_end(::core::ptr::addr_of_mut!(next), allocator); - return LZMA_PROG_ERROR; - }; + debug_assert!(next.code.is_some()); + let code = next.code.unwrap_unchecked(); let in_start: size_t = *in_pos; let out_start: size_t = *out_pos; let mut ret: lzma_ret = code( next.coder, allocator, - in_0, + input, in_pos, in_size, out, @@ -67,7 +63,7 @@ pub unsafe fn lzma_raw_buffer_decode( code( next.coder, allocator, - in_0, + input, in_pos, in_size, ::core::ptr::addr_of_mut!(tmp) as *mut u8, diff --git a/xz/src/common/filter_buffer_encoder.rs b/xz/src/common/filter_buffer_encoder.rs index f088a2ee..df8764ed 100644 --- a/xz/src/common/filter_buffer_encoder.rs +++ b/xz/src/common/filter_buffer_encoder.rs @@ -2,13 +2,14 @@ use crate::types::*; pub unsafe fn lzma_raw_buffer_encode( filters: *const lzma_filter, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_size: size_t, out: *mut u8, out_pos: *mut size_t, out_size: size_t, ) -> lzma_ret { - if in_0.is_null() && in_size != 0 || out.is_null() || out_pos.is_null() || *out_pos > out_size { + if input.is_null() && in_size != 0 || out.is_null() || out_pos.is_null() || *out_pos > out_size + { return LZMA_PROG_ERROR; } let mut next: lzma_next_coder = lzma_next_coder_s { @@ -23,22 +24,18 @@ pub unsafe fn lzma_raw_buffer_encode( update: None, set_out_limit: None, }; - let ret_: lzma_ret = lzma_raw_encoder_init(::core::ptr::addr_of_mut!(next), allocator, filters); - if ret_ != LZMA_OK { - return ret_; + let ret: lzma_ret = lzma_raw_encoder_init(::core::ptr::addr_of_mut!(next), allocator, filters); + if ret != LZMA_OK { + return ret; } - let code = if let Some(code) = next.code { - code - } else { - lzma_next_end(::core::ptr::addr_of_mut!(next), allocator); - return LZMA_PROG_ERROR; - }; + debug_assert!(next.code.is_some()); + let code = next.code.unwrap_unchecked(); let out_start: size_t = *out_pos; let mut in_pos: size_t = 0; let mut ret: lzma_ret = code( next.coder, allocator, - in_0, + input, ::core::ptr::addr_of_mut!(in_pos), in_size, out, diff --git a/xz/src/common/filter_common.rs b/xz/src/common/filter_common.rs index 2c39b69f..e6eb6279 100644 --- a/xz/src/common/filter_common.rs +++ b/xz/src/common/filter_common.rs @@ -106,7 +106,6 @@ pub unsafe fn lzma_filters_copy( real_dest: *mut lzma_filter, allocator: *const lzma_allocator, ) -> lzma_ret { - let current_block: u64; if src.is_null() || real_dest.is_null() { return LZMA_PROG_ERROR; } @@ -119,13 +118,11 @@ pub unsafe fn lzma_filters_copy( i = 0; 's_15: loop { if (*src.offset(i as isize)).id == LZMA_VLI_UNKNOWN { - current_block = 7175849428784450219; break; } if i == LZMA_FILTERS_MAX as size_t { ret = LZMA_OPTIONS_ERROR; - current_block = 6392083060350426025; - break; + break 's_15; } else { dest[i as usize].id = (*src.offset(i as isize)).id; if (*src.offset(i as isize)).options.is_null() { @@ -136,7 +133,6 @@ pub unsafe fn lzma_filters_copy( while (*src.offset(i as isize)).id != FEATURES[j as usize].id { if FEATURES[j as usize].id == LZMA_VLI_UNKNOWN { ret = LZMA_OPTIONS_ERROR; - current_block = 6392083060350426025; break 's_15; } else { j += 1; @@ -145,8 +141,7 @@ pub unsafe fn lzma_filters_copy( dest[i as usize].options = lzma_alloc(FEATURES[j as usize].options_size, allocator); if dest[i as usize].options.is_null() { ret = LZMA_MEM_ERROR; - current_block = 6392083060350426025; - break; + break 's_15; } else { core::ptr::copy_nonoverlapping( (*src.offset(i as isize)).options as *const u8, @@ -158,25 +153,21 @@ pub unsafe fn lzma_filters_copy( i += 1; } } - match current_block { - 6392083060350426025 => { - while i > 0 { - i -= 1; - lzma_free(dest[i as usize].options, allocator); - } - return ret; - } - _ => { - dest[i as usize].id = LZMA_VLI_UNKNOWN; - dest[i as usize].options = core::ptr::null_mut(); - core::ptr::copy_nonoverlapping( - ::core::ptr::addr_of_mut!(dest) as *const u8, - real_dest as *mut u8, - (i + 1) * core::mem::size_of::(), - ); - return LZMA_OK; + if ret != LZMA_OK { + while i > 0 { + i -= 1; + lzma_free(dest[i as usize].options, allocator); } - }; + return ret; + } + dest[i as usize].id = LZMA_VLI_UNKNOWN; + dest[i as usize].options = core::ptr::null_mut(); + core::ptr::copy_nonoverlapping( + ::core::ptr::addr_of_mut!(dest) as *const u8, + real_dest as *mut u8, + (i + 1) * core::mem::size_of::(), + ); + LZMA_OK } pub unsafe fn lzma_filters_free(filters: *mut lzma_filter, allocator: *const lzma_allocator) { if filters.is_null() { @@ -231,13 +222,13 @@ pub unsafe fn lzma_raw_coder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, options: *const lzma_filter, - coder_find: lzma_filter_find, + coder_find: unsafe fn(lzma_vli) -> *const lzma_filter_coder, is_encoder: bool, ) -> lzma_ret { let mut count: size_t = 0; - let ret_: lzma_ret = lzma_validate_chain(options, ::core::ptr::addr_of_mut!(count)); - if ret_ != LZMA_OK { - return ret_; + let ret: lzma_ret = lzma_validate_chain(options, ::core::ptr::addr_of_mut!(count)); + if ret != LZMA_OK { + return ret; } let mut filters: [lzma_filter_info; 5] = [lzma_filter_info_s { id: 0, @@ -249,7 +240,7 @@ pub unsafe fn lzma_raw_coder_init( while i < count { let j: size_t = count - i - 1; let fc: *const lzma_filter_coder = - coder_find.unwrap()((*options.offset(i as isize)).id) as *const lzma_filter_coder; + coder_find((*options.offset(i as isize)).id) as *const lzma_filter_coder; if fc.is_null() || (*fc).init.is_none() { return LZMA_OPTIONS_ERROR; } @@ -262,7 +253,7 @@ pub unsafe fn lzma_raw_coder_init( let mut i_0: size_t = 0; while i_0 < count { let fc_0: *const lzma_filter_coder = - coder_find.unwrap()((*options.offset(i_0 as isize)).id) as *const lzma_filter_coder; + coder_find((*options.offset(i_0 as isize)).id) as *const lzma_filter_coder; if fc_0.is_null() || (*fc_0).init.is_none() { return LZMA_OPTIONS_ERROR; } @@ -285,7 +276,7 @@ pub unsafe fn lzma_raw_coder_init( ret } pub unsafe fn lzma_raw_coder_memusage( - coder_find: lzma_filter_find, + coder_find: unsafe fn(lzma_vli) -> *const lzma_filter_coder, filters: *const lzma_filter, ) -> u64 { let mut tmp: size_t = 0; @@ -296,18 +287,18 @@ pub unsafe fn lzma_raw_coder_memusage( let mut i: size_t = 0; loop { let fc: *const lzma_filter_coder = - coder_find.unwrap()((*filters.offset(i as isize)).id) as *const lzma_filter_coder; + coder_find((*filters.offset(i as isize)).id) as *const lzma_filter_coder; if fc.is_null() { return UINT64_MAX; } - if (*fc).memusage.is_none() { - total += 1024; - } else { - let usage: u64 = (*fc).memusage.unwrap()((*filters.offset(i as isize)).options) as u64; + if let Some(memusage) = (*fc).memusage { + let usage: u64 = memusage((*filters.offset(i as isize)).options) as u64; if usage == UINT64_MAX { return UINT64_MAX; } total += usage; + } else { + total += 1024; } i += 1; if (*filters.offset(i as isize)).id == LZMA_VLI_UNKNOWN { diff --git a/xz/src/common/filter_decoder.rs b/xz/src/common/filter_decoder.rs index 2135574c..e8cdcebd 100644 --- a/xz/src/common/filter_decoder.rs +++ b/xz/src/common/filter_decoder.rs @@ -1,8 +1,8 @@ use crate::delta::delta_decoder::{lzma_delta_decoder_init, lzma_delta_props_decode}; +use crate::lzma::lzma_decoder::{lzma_lzma_decoder_memusage, lzma_lzma_props_decode}; use crate::lzma::lzma2_decoder::{ lzma_lzma2_decoder_init, lzma_lzma2_decoder_memusage, lzma_lzma2_props_decode, }; -use crate::lzma::lzma_decoder::{lzma_lzma_decoder_memusage, lzma_lzma_props_decode}; use crate::simple::arm::lzma_simple_arm_decoder_init; use crate::simple::arm64::lzma_simple_arm64_decoder_init; use crate::simple::armthumb::lzma_simple_armthumb_decoder_init; @@ -18,31 +18,25 @@ use crate::types::*; pub struct lzma_filter_decoder { pub id: lzma_vli, pub init: lzma_init_function, - pub memusage: Option u64>, - pub props_decode: Option< - unsafe extern "C" fn( - *mut *mut c_void, - *const lzma_allocator, - *const u8, - size_t, - ) -> lzma_ret, - >, + pub memusage: Option u64>, + pub props_decode: + Option lzma_ret>, } static decoders: [lzma_filter_decoder; 12] = [ lzma_filter_decoder { id: LZMA_FILTER_LZMA1, init: Some( lzma_lzma_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, ) -> lzma_ret, ), - memusage: Some(lzma_lzma_decoder_memusage as unsafe extern "C" fn(*const c_void) -> u64), + memusage: Some(lzma_lzma_decoder_memusage as unsafe fn(*const c_void) -> u64), props_decode: Some( lzma_lzma_props_decode - as unsafe extern "C" fn( + as unsafe fn( *mut *mut c_void, *const lzma_allocator, *const u8, @@ -54,16 +48,16 @@ static decoders: [lzma_filter_decoder; 12] = [ id: LZMA_FILTER_LZMA1EXT, init: Some( lzma_lzma_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, ) -> lzma_ret, ), - memusage: Some(lzma_lzma_decoder_memusage as unsafe extern "C" fn(*const c_void) -> u64), + memusage: Some(lzma_lzma_decoder_memusage as unsafe fn(*const c_void) -> u64), props_decode: Some( lzma_lzma_props_decode - as unsafe extern "C" fn( + as unsafe fn( *mut *mut c_void, *const lzma_allocator, *const u8, @@ -75,16 +69,16 @@ static decoders: [lzma_filter_decoder; 12] = [ id: LZMA_FILTER_LZMA2, init: Some( lzma_lzma2_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, ) -> lzma_ret, ), - memusage: Some(lzma_lzma2_decoder_memusage as unsafe extern "C" fn(*const c_void) -> u64), + memusage: Some(lzma_lzma2_decoder_memusage as unsafe fn(*const c_void) -> u64), props_decode: Some( lzma_lzma2_props_decode - as unsafe extern "C" fn( + as unsafe fn( *mut *mut c_void, *const lzma_allocator, *const u8, @@ -96,7 +90,7 @@ static decoders: [lzma_filter_decoder; 12] = [ id: LZMA_FILTER_X86, init: Some( lzma_simple_x86_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -105,7 +99,7 @@ static decoders: [lzma_filter_decoder; 12] = [ memusage: None, props_decode: Some( lzma_simple_props_decode - as unsafe extern "C" fn( + as unsafe fn( *mut *mut c_void, *const lzma_allocator, *const u8, @@ -117,7 +111,7 @@ static decoders: [lzma_filter_decoder; 12] = [ id: LZMA_FILTER_POWERPC, init: Some( lzma_simple_powerpc_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -126,7 +120,7 @@ static decoders: [lzma_filter_decoder; 12] = [ memusage: None, props_decode: Some( lzma_simple_props_decode - as unsafe extern "C" fn( + as unsafe fn( *mut *mut c_void, *const lzma_allocator, *const u8, @@ -138,7 +132,7 @@ static decoders: [lzma_filter_decoder; 12] = [ id: LZMA_FILTER_IA64, init: Some( lzma_simple_ia64_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -147,7 +141,7 @@ static decoders: [lzma_filter_decoder; 12] = [ memusage: None, props_decode: Some( lzma_simple_props_decode - as unsafe extern "C" fn( + as unsafe fn( *mut *mut c_void, *const lzma_allocator, *const u8, @@ -159,7 +153,7 @@ static decoders: [lzma_filter_decoder; 12] = [ id: LZMA_FILTER_ARM, init: Some( lzma_simple_arm_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -168,7 +162,7 @@ static decoders: [lzma_filter_decoder; 12] = [ memusage: None, props_decode: Some( lzma_simple_props_decode - as unsafe extern "C" fn( + as unsafe fn( *mut *mut c_void, *const lzma_allocator, *const u8, @@ -180,7 +174,7 @@ static decoders: [lzma_filter_decoder; 12] = [ id: LZMA_FILTER_ARMTHUMB, init: Some( lzma_simple_armthumb_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -189,7 +183,7 @@ static decoders: [lzma_filter_decoder; 12] = [ memusage: None, props_decode: Some( lzma_simple_props_decode - as unsafe extern "C" fn( + as unsafe fn( *mut *mut c_void, *const lzma_allocator, *const u8, @@ -201,7 +195,7 @@ static decoders: [lzma_filter_decoder; 12] = [ id: LZMA_FILTER_ARM64, init: Some( lzma_simple_arm64_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -210,7 +204,7 @@ static decoders: [lzma_filter_decoder; 12] = [ memusage: None, props_decode: Some( lzma_simple_props_decode - as unsafe extern "C" fn( + as unsafe fn( *mut *mut c_void, *const lzma_allocator, *const u8, @@ -222,7 +216,7 @@ static decoders: [lzma_filter_decoder; 12] = [ id: LZMA_FILTER_SPARC, init: Some( lzma_simple_sparc_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -231,7 +225,7 @@ static decoders: [lzma_filter_decoder; 12] = [ memusage: None, props_decode: Some( lzma_simple_props_decode - as unsafe extern "C" fn( + as unsafe fn( *mut *mut c_void, *const lzma_allocator, *const u8, @@ -243,7 +237,7 @@ static decoders: [lzma_filter_decoder; 12] = [ id: LZMA_FILTER_RISCV, init: Some( lzma_simple_riscv_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -252,7 +246,7 @@ static decoders: [lzma_filter_decoder; 12] = [ memusage: None, props_decode: Some( lzma_simple_props_decode - as unsafe extern "C" fn( + as unsafe fn( *mut *mut c_void, *const lzma_allocator, *const u8, @@ -264,16 +258,16 @@ static decoders: [lzma_filter_decoder; 12] = [ id: LZMA_FILTER_DELTA, init: Some( lzma_delta_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, ) -> lzma_ret, ), - memusage: Some(lzma_delta_coder_memusage as unsafe extern "C" fn(*const c_void) -> u64), + memusage: Some(lzma_delta_coder_memusage as unsafe fn(*const c_void) -> u64), props_decode: Some( lzma_delta_props_decode - as unsafe extern "C" fn( + as unsafe fn( *mut *mut c_void, *const lzma_allocator, *const u8, @@ -294,7 +288,7 @@ fn decoder_find(id: lzma_vli) -> *const lzma_filter_decoder { } core::ptr::null() } -unsafe extern "C" fn coder_find(id: lzma_vli) -> *const lzma_filter_coder { +unsafe fn coder_find(id: lzma_vli) -> *const lzma_filter_coder { decoder_find(id) as *const lzma_filter_coder } pub fn lzma_filter_decoder_is_supported(id: lzma_vli) -> lzma_bool { @@ -309,7 +303,7 @@ pub unsafe fn lzma_raw_decoder_init( next, allocator, options, - Some(coder_find as unsafe extern "C" fn(lzma_vli) -> *const lzma_filter_coder), + coder_find as unsafe fn(lzma_vli) -> *const lzma_filter_coder, false, ) } @@ -333,7 +327,7 @@ pub unsafe fn lzma_raw_decoder(strm: *mut lzma_stream, options: *const lzma_filt } pub unsafe fn lzma_raw_decoder_memusage(filters: *const lzma_filter) -> u64 { lzma_raw_coder_memusage( - Some(coder_find as unsafe extern "C" fn(lzma_vli) -> *const lzma_filter_coder), + coder_find as unsafe fn(lzma_vli) -> *const lzma_filter_coder, filters, ) } @@ -348,17 +342,16 @@ pub unsafe fn lzma_properties_decode( if fd.is_null() { return LZMA_OPTIONS_ERROR; } - if (*fd).props_decode.is_none() { - return if props_size == 0 { - LZMA_OK - } else { - LZMA_OPTIONS_ERROR - }; + if let Some(props_decode) = (*fd).props_decode { + props_decode( + ::core::ptr::addr_of_mut!((*filter).options), + allocator, + props, + props_size, + ) + } else if props_size == 0 { + LZMA_OK + } else { + LZMA_OPTIONS_ERROR } - (*fd).props_decode.unwrap()( - ::core::ptr::addr_of_mut!((*filter).options), - allocator, - props, - props_size, - ) } diff --git a/xz/src/common/filter_encoder.rs b/xz/src/common/filter_encoder.rs index dcbd5dbd..f8796392 100644 --- a/xz/src/common/filter_encoder.rs +++ b/xz/src/common/filter_encoder.rs @@ -1,9 +1,9 @@ use crate::delta::delta_encoder::{lzma_delta_encoder_init, lzma_delta_props_encode}; +use crate::lzma::lzma_encoder::lzma_lzma_props_encode; use crate::lzma::lzma2_encoder::{ lzma_lzma2_block_size, lzma_lzma2_encoder_init, lzma_lzma2_encoder_memusage, lzma_lzma2_props_encode, }; -use crate::lzma::lzma_encoder::lzma_lzma_props_encode; use crate::simple::arm::lzma_simple_arm_encoder_init; use crate::simple::arm64::lzma_simple_arm64_encoder_init; use crate::simple::armthumb::lzma_simple_armthumb_encoder_init; @@ -19,72 +19,68 @@ use crate::types::*; pub struct lzma_filter_encoder { pub id: lzma_vli, pub init: lzma_init_function, - pub memusage: Option u64>, - pub block_size: Option u64>, - pub props_size_get: Option lzma_ret>, + pub memusage: Option u64>, + pub block_size: Option u64>, + pub props_size_get: Option lzma_ret>, pub props_size_fixed: u32, - pub props_encode: Option lzma_ret>, + pub props_encode: Option lzma_ret>, } static encoders: [lzma_filter_encoder; 12] = [ lzma_filter_encoder { id: LZMA_FILTER_LZMA1, init: Some( lzma_lzma_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, ) -> lzma_ret, ), - memusage: Some(lzma_lzma_encoder_memusage as unsafe extern "C" fn(*const c_void) -> u64), + memusage: Some(lzma_lzma_encoder_memusage as unsafe fn(*const c_void) -> u64), block_size: None, props_size_get: None, props_size_fixed: 5, - props_encode: Some( - lzma_lzma_props_encode as unsafe extern "C" fn(*const c_void, *mut u8) -> lzma_ret, - ), + props_encode: Some(lzma_lzma_props_encode as unsafe fn(*const c_void, *mut u8) -> lzma_ret), }, lzma_filter_encoder { id: LZMA_FILTER_LZMA1EXT, init: Some( lzma_lzma_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, ) -> lzma_ret, ), - memusage: Some(lzma_lzma_encoder_memusage as unsafe extern "C" fn(*const c_void) -> u64), + memusage: Some(lzma_lzma_encoder_memusage as unsafe fn(*const c_void) -> u64), block_size: None, props_size_get: None, props_size_fixed: 5, - props_encode: Some( - lzma_lzma_props_encode as unsafe extern "C" fn(*const c_void, *mut u8) -> lzma_ret, - ), + props_encode: Some(lzma_lzma_props_encode as unsafe fn(*const c_void, *mut u8) -> lzma_ret), }, lzma_filter_encoder { id: LZMA_FILTER_LZMA2, init: Some( lzma_lzma2_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, ) -> lzma_ret, ), - memusage: Some(lzma_lzma2_encoder_memusage as unsafe extern "C" fn(*const c_void) -> u64), - block_size: Some(lzma_lzma2_block_size as unsafe extern "C" fn(*const c_void) -> u64), + memusage: Some(lzma_lzma2_encoder_memusage as unsafe fn(*const c_void) -> u64), + block_size: Some(lzma_lzma2_block_size as unsafe fn(*const c_void) -> u64), props_size_get: None, props_size_fixed: 1, props_encode: Some( - lzma_lzma2_props_encode as unsafe extern "C" fn(*const c_void, *mut u8) -> lzma_ret, + lzma_lzma2_props_encode as unsafe fn(*const c_void, *mut u8) -> lzma_ret, ), }, lzma_filter_encoder { id: LZMA_FILTER_X86, init: Some( lzma_simple_x86_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -93,18 +89,18 @@ static encoders: [lzma_filter_encoder; 12] = [ memusage: None, block_size: None, props_size_get: Some( - lzma_simple_props_size as unsafe extern "C" fn(*mut u32, *const c_void) -> lzma_ret, + lzma_simple_props_size as unsafe fn(*mut u32, *const c_void) -> lzma_ret, ), props_size_fixed: 0, props_encode: Some( - lzma_simple_props_encode as unsafe extern "C" fn(*const c_void, *mut u8) -> lzma_ret, + lzma_simple_props_encode as unsafe fn(*const c_void, *mut u8) -> lzma_ret, ), }, lzma_filter_encoder { id: LZMA_FILTER_POWERPC, init: Some( lzma_simple_powerpc_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -113,18 +109,18 @@ static encoders: [lzma_filter_encoder; 12] = [ memusage: None, block_size: None, props_size_get: Some( - lzma_simple_props_size as unsafe extern "C" fn(*mut u32, *const c_void) -> lzma_ret, + lzma_simple_props_size as unsafe fn(*mut u32, *const c_void) -> lzma_ret, ), props_size_fixed: 0, props_encode: Some( - lzma_simple_props_encode as unsafe extern "C" fn(*const c_void, *mut u8) -> lzma_ret, + lzma_simple_props_encode as unsafe fn(*const c_void, *mut u8) -> lzma_ret, ), }, lzma_filter_encoder { id: LZMA_FILTER_IA64, init: Some( lzma_simple_ia64_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -133,18 +129,18 @@ static encoders: [lzma_filter_encoder; 12] = [ memusage: None, block_size: None, props_size_get: Some( - lzma_simple_props_size as unsafe extern "C" fn(*mut u32, *const c_void) -> lzma_ret, + lzma_simple_props_size as unsafe fn(*mut u32, *const c_void) -> lzma_ret, ), props_size_fixed: 0, props_encode: Some( - lzma_simple_props_encode as unsafe extern "C" fn(*const c_void, *mut u8) -> lzma_ret, + lzma_simple_props_encode as unsafe fn(*const c_void, *mut u8) -> lzma_ret, ), }, lzma_filter_encoder { id: LZMA_FILTER_ARM, init: Some( lzma_simple_arm_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -153,18 +149,18 @@ static encoders: [lzma_filter_encoder; 12] = [ memusage: None, block_size: None, props_size_get: Some( - lzma_simple_props_size as unsafe extern "C" fn(*mut u32, *const c_void) -> lzma_ret, + lzma_simple_props_size as unsafe fn(*mut u32, *const c_void) -> lzma_ret, ), props_size_fixed: 0, props_encode: Some( - lzma_simple_props_encode as unsafe extern "C" fn(*const c_void, *mut u8) -> lzma_ret, + lzma_simple_props_encode as unsafe fn(*const c_void, *mut u8) -> lzma_ret, ), }, lzma_filter_encoder { id: LZMA_FILTER_ARMTHUMB, init: Some( lzma_simple_armthumb_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -173,18 +169,18 @@ static encoders: [lzma_filter_encoder; 12] = [ memusage: None, block_size: None, props_size_get: Some( - lzma_simple_props_size as unsafe extern "C" fn(*mut u32, *const c_void) -> lzma_ret, + lzma_simple_props_size as unsafe fn(*mut u32, *const c_void) -> lzma_ret, ), props_size_fixed: 0, props_encode: Some( - lzma_simple_props_encode as unsafe extern "C" fn(*const c_void, *mut u8) -> lzma_ret, + lzma_simple_props_encode as unsafe fn(*const c_void, *mut u8) -> lzma_ret, ), }, lzma_filter_encoder { id: LZMA_FILTER_ARM64, init: Some( lzma_simple_arm64_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -193,18 +189,18 @@ static encoders: [lzma_filter_encoder; 12] = [ memusage: None, block_size: None, props_size_get: Some( - lzma_simple_props_size as unsafe extern "C" fn(*mut u32, *const c_void) -> lzma_ret, + lzma_simple_props_size as unsafe fn(*mut u32, *const c_void) -> lzma_ret, ), props_size_fixed: 0, props_encode: Some( - lzma_simple_props_encode as unsafe extern "C" fn(*const c_void, *mut u8) -> lzma_ret, + lzma_simple_props_encode as unsafe fn(*const c_void, *mut u8) -> lzma_ret, ), }, lzma_filter_encoder { id: LZMA_FILTER_SPARC, init: Some( lzma_simple_sparc_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -213,18 +209,18 @@ static encoders: [lzma_filter_encoder; 12] = [ memusage: None, block_size: None, props_size_get: Some( - lzma_simple_props_size as unsafe extern "C" fn(*mut u32, *const c_void) -> lzma_ret, + lzma_simple_props_size as unsafe fn(*mut u32, *const c_void) -> lzma_ret, ), props_size_fixed: 0, props_encode: Some( - lzma_simple_props_encode as unsafe extern "C" fn(*const c_void, *mut u8) -> lzma_ret, + lzma_simple_props_encode as unsafe fn(*const c_void, *mut u8) -> lzma_ret, ), }, lzma_filter_encoder { id: LZMA_FILTER_RISCV, init: Some( lzma_simple_riscv_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -233,45 +229,57 @@ static encoders: [lzma_filter_encoder; 12] = [ memusage: None, block_size: None, props_size_get: Some( - lzma_simple_props_size as unsafe extern "C" fn(*mut u32, *const c_void) -> lzma_ret, + lzma_simple_props_size as unsafe fn(*mut u32, *const c_void) -> lzma_ret, ), props_size_fixed: 0, props_encode: Some( - lzma_simple_props_encode as unsafe extern "C" fn(*const c_void, *mut u8) -> lzma_ret, + lzma_simple_props_encode as unsafe fn(*const c_void, *mut u8) -> lzma_ret, ), }, lzma_filter_encoder { id: LZMA_FILTER_DELTA, init: Some( lzma_delta_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, ) -> lzma_ret, ), - memusage: Some(lzma_delta_coder_memusage as unsafe extern "C" fn(*const c_void) -> u64), + memusage: Some(lzma_delta_coder_memusage as unsafe fn(*const c_void) -> u64), block_size: None, props_size_get: None, props_size_fixed: 1, props_encode: Some( - lzma_delta_props_encode as unsafe extern "C" fn(*const c_void, *mut u8) -> lzma_ret, + lzma_delta_props_encode as unsafe fn(*const c_void, *mut u8) -> lzma_ret, ), }, ]; +#[inline(always)] +unsafe fn reversed_filter_slot(filters: *mut [lzma_filter; 5], index: usize) -> *mut lzma_filter { + debug_assert!(index < 5); + (filters as *mut lzma_filter).add(index) +} +#[inline(always)] +unsafe fn supported_action_slot(actions: *mut bool, index: u32) -> *mut bool { + debug_assert!((index as usize) < 5); + actions.add(index as usize) +} fn encoder_find(id: lzma_vli) -> *const lzma_filter_encoder { + let encoders_ptr = encoders.as_ptr(); let mut i: size_t = 0; while i < core::mem::size_of::<[lzma_filter_encoder; 12]>() / core::mem::size_of::() { - if encoders[i as usize].id == id { - return &encoders[i as usize]; + let encoder = unsafe { encoders_ptr.add(i as usize) }; + if unsafe { (*encoder).id } == id { + return encoder; } i += 1; } core::ptr::null() } -unsafe extern "C" fn coder_find(id: lzma_vli) -> *const lzma_filter_coder { +unsafe fn coder_find(id: lzma_vli) -> *const lzma_filter_coder { encoder_find(id) as *const lzma_filter_coder } pub fn lzma_filter_encoder_is_supported(id: lzma_vli) -> lzma_bool { @@ -294,11 +302,17 @@ pub unsafe fn lzma_filters_update(strm: *mut lzma_stream, filters: *const lzma_f }; 5]; let mut i: size_t = 0; while i < count { - reversed_filters[count - i - 1] = *filters.offset(i as isize); + *reversed_filter_slot( + ::core::ptr::addr_of_mut!(reversed_filters), + (count - i - 1) as usize, + ) = *filters.offset(i as isize); i += 1; } - reversed_filters[count as usize].id = LZMA_VLI_UNKNOWN; - (*(*strm).internal).next.update.unwrap()( + (*reversed_filter_slot(::core::ptr::addr_of_mut!(reversed_filters), count as usize)).id = + LZMA_VLI_UNKNOWN; + debug_assert!((*(*strm).internal).next.update.is_some()); + let update = (*(*strm).internal).next.update.unwrap_unchecked(); + update( (*(*strm).internal).next.coder, (*strm).allocator, filters, @@ -314,34 +328,43 @@ pub unsafe fn lzma_raw_encoder_init( next, allocator, filters, - Some(coder_find as unsafe extern "C" fn(lzma_vli) -> *const lzma_filter_coder), + coder_find as unsafe fn(lzma_vli) -> *const lzma_filter_coder, true, ) } pub unsafe fn lzma_raw_encoder(strm: *mut lzma_stream, filters: *const lzma_filter) -> lzma_ret { - let ret_: lzma_ret = lzma_strm_init(strm); - if ret_ != LZMA_OK { - return ret_; + let ret: lzma_ret = lzma_strm_init(strm); + if ret != LZMA_OK { + return ret; } - let ret__0: lzma_ret = lzma_raw_coder_init( + let ret: lzma_ret = lzma_raw_coder_init( ::core::ptr::addr_of_mut!((*(*strm).internal).next), (*strm).allocator, filters, - Some(coder_find as unsafe extern "C" fn(lzma_vli) -> *const lzma_filter_coder), + coder_find as unsafe fn(lzma_vli) -> *const lzma_filter_coder, true, ); - if ret__0 != LZMA_OK { + if ret != LZMA_OK { lzma_end(strm); - return ret__0; + return ret; } - (*(*strm).internal).supported_actions[LZMA_RUN as usize] = true; - (*(*strm).internal).supported_actions[LZMA_SYNC_FLUSH as usize] = true; - (*(*strm).internal).supported_actions[LZMA_FINISH as usize] = true; + *supported_action_slot( + ::core::ptr::addr_of_mut!((*(*strm).internal).supported_actions) as *mut bool, + LZMA_RUN, + ) = true; + *supported_action_slot( + ::core::ptr::addr_of_mut!((*(*strm).internal).supported_actions) as *mut bool, + LZMA_SYNC_FLUSH, + ) = true; + *supported_action_slot( + ::core::ptr::addr_of_mut!((*(*strm).internal).supported_actions) as *mut bool, + LZMA_FINISH, + ) = true; LZMA_OK } pub unsafe fn lzma_raw_encoder_memusage(filters: *const lzma_filter) -> u64 { lzma_raw_coder_memusage( - Some(coder_find as unsafe extern "C" fn(lzma_vli) -> *const lzma_filter_coder), + coder_find as unsafe fn(lzma_vli) -> *const lzma_filter_coder, filters, ) } @@ -357,19 +380,15 @@ pub unsafe fn lzma_mt_block_size(filters: *const lzma_filter) -> u64 { if fe.is_null() { return UINT64_MAX; } - if (*fe).block_size.is_some() { - let size: u64 = (*fe).block_size.unwrap()((*filters.offset(i as isize)).options) as u64; + if let Some(block_size) = (*fe).block_size { + let size: u64 = block_size((*filters.offset(i as isize)).options) as u64; if size > max { max = size; } } i += 1; } - if max == 0 { - UINT64_MAX - } else { - max - } + if max == 0 { UINT64_MAX } else { max } } pub unsafe fn lzma_properties_size(size: *mut u32, filter: *const lzma_filter) -> lzma_ret { let fe: *const lzma_filter_encoder = encoder_find((*filter).id) as *const lzma_filter_encoder; @@ -380,19 +399,21 @@ pub unsafe fn lzma_properties_size(size: *mut u32, filter: *const lzma_filter) - LZMA_PROG_ERROR }; } - if (*fe).props_size_get.is_none() { + if let Some(props_size_get) = (*fe).props_size_get { + props_size_get(size, (*filter).options) + } else { *size = (*fe).props_size_fixed; - return LZMA_OK; + LZMA_OK } - (*fe).props_size_get.unwrap()(size, (*filter).options) } pub unsafe fn lzma_properties_encode(filter: *const lzma_filter, props: *mut u8) -> lzma_ret { let fe: *const lzma_filter_encoder = encoder_find((*filter).id) as *const lzma_filter_encoder; if fe.is_null() { return LZMA_PROG_ERROR; } - if (*fe).props_encode.is_none() { - return LZMA_OK; + if let Some(props_encode) = (*fe).props_encode { + props_encode((*filter).options, props) + } else { + LZMA_OK } - (*fe).props_encode.unwrap()((*filter).options, props) } diff --git a/xz/src/common/filter_flags_decoder.rs b/xz/src/common/filter_flags_decoder.rs index 926b5345..2ef19475 100644 --- a/xz/src/common/filter_flags_decoder.rs +++ b/xz/src/common/filter_flags_decoder.rs @@ -3,34 +3,34 @@ use crate::types::*; pub unsafe fn lzma_filter_flags_decode( filter: *mut lzma_filter, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, ) -> lzma_ret { (*filter).options = core::ptr::null_mut(); - let ret_: lzma_ret = lzma_vli_decode( + let ret: lzma_ret = lzma_vli_decode( ::core::ptr::addr_of_mut!((*filter).id), core::ptr::null_mut(), - in_0, + input, in_pos, in_size, ); - if ret_ != LZMA_OK { - return ret_; + if ret != LZMA_OK { + return ret; } if (*filter).id >= LZMA_FILTER_RESERVED_START { return LZMA_DATA_ERROR; } let mut props_size: lzma_vli = 0; - let ret__0: lzma_ret = lzma_vli_decode( + let ret: lzma_ret = lzma_vli_decode( ::core::ptr::addr_of_mut!(props_size), core::ptr::null_mut(), - in_0, + input, in_pos, in_size, ); - if ret__0 != LZMA_OK { - return ret__0; + if ret != LZMA_OK { + return ret; } if ((in_size - *in_pos) as lzma_vli) < props_size { return LZMA_DATA_ERROR; @@ -38,7 +38,7 @@ pub unsafe fn lzma_filter_flags_decode( let ret: lzma_ret = lzma_properties_decode( filter, allocator, - in_0.offset(*in_pos as isize), + input.offset(*in_pos as isize), props_size as size_t, ); *in_pos = (*in_pos as lzma_vli + props_size) as size_t; diff --git a/xz/src/common/index.rs b/xz/src/common/index.rs index ec1d6643..de912b30 100644 --- a/xz/src/common/index.rs +++ b/xz/src/common/index.rs @@ -96,7 +96,7 @@ unsafe fn index_tree_init(tree: *mut index_tree) { unsafe fn index_tree_node_end( node: *mut index_tree_node, allocator: *const lzma_allocator, - free_func: Option ()>, + free_func: unsafe fn(*mut c_void, *const lzma_allocator) -> (), ) { if !(*node).left.is_null() { index_tree_node_end((*node).left, allocator, free_func); @@ -104,12 +104,12 @@ unsafe fn index_tree_node_end( if !(*node).right.is_null() { index_tree_node_end((*node).right, allocator, free_func); } - free_func.unwrap()(node as *mut c_void, allocator); + free_func(node as *mut c_void, allocator); } unsafe fn index_tree_end( tree: *mut index_tree, allocator: *const lzma_allocator, - free_func: Option ()>, + free_func: unsafe fn(*mut c_void, *const lzma_allocator) -> (), ) { if !(*tree).root.is_null() { index_tree_node_end((*tree).root, allocator, free_func); @@ -213,7 +213,7 @@ unsafe fn index_stream_end(node: *mut c_void, allocator: *const lzma_allocator) index_tree_end( ::core::ptr::addr_of_mut!((*s).groups), allocator, - Some(index_node_free as unsafe fn(*mut c_void, *const lzma_allocator) -> ()), + index_node_free as unsafe fn(*mut c_void, *const lzma_allocator) -> (), ); lzma_free(s as *mut c_void, allocator); } @@ -252,7 +252,7 @@ pub unsafe fn lzma_index_end(i: *mut lzma_index, allocator: *const lzma_allocato index_tree_end( ::core::ptr::addr_of_mut!((*i).streams), allocator, - Some(index_stream_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()), + index_stream_end as unsafe fn(*mut c_void, *const lzma_allocator) -> (), ); lzma_free(i as *mut c_void, allocator); } @@ -261,6 +261,28 @@ pub unsafe fn lzma_index_prealloc(i: *mut lzma_index, mut records: lzma_vli) { if records > PREALLOC_MAX as lzma_vli { records = PREALLOC_MAX as lzma_vli; } + + // If index_decoder.c calls us with records == 0, it's decoding + // an Index that has no Records. In that case the decoder won't call + // lzma_index_append() at all, and i->prealloc isn't used during + // the Index decoding either. + // + // Normally the first lzma_index_append() call from the Index decoder + // would reset i->prealloc to INDEX_GROUP_SIZE. With no Records, + // lzma_index_append() isn't called and the resetting of prealloc + // won't occur either. Thus, if records == 0, use the default value + // INDEX_GROUP_SIZE instead. + // + // NOTE: lzma_index_append() assumes i->prealloc > 0. liblzma <= 5.8.2 + // didn't have this check and could set i->prealloc = 0, which would + // result in a buffer overflow if the application called + // lzma_index_append() after decoding an empty Index. Appending + // Records after decoding an Index is a rare thing to do, but + // it is supposed to work. + if records == 0 { + records = INDEX_GROUP_SIZE as lzma_vli; + } + (*i).prealloc = records as size_t; } pub fn lzma_index_memusage(streams: lzma_vli, blocks: lzma_vli) -> u64 { @@ -376,9 +398,9 @@ pub unsafe fn lzma_index_stream_flags( if i.is_null() || stream_flags.is_null() { return LZMA_PROG_ERROR; } - let ret_: lzma_ret = lzma_stream_flags_compare(stream_flags, stream_flags); - if ret_ != LZMA_OK { - return ret_; + let ret: lzma_ret = lzma_stream_flags_compare(stream_flags, stream_flags); + if ret != LZMA_OK { + return ret; } let s: *mut index_stream = (*i).streams.rightmost as *mut index_stream; (*s).stream_flags = *stream_flags; @@ -458,6 +480,7 @@ pub unsafe fn lzma_index_append( if !g.is_null() && (*g).last + 1 < (*g).allocated { (*g).last += 1; } else { + debug_assert!((*i).prealloc > 0); g = lzma_alloc( core::mem::size_of::() + (*i).prealloc * core::mem::size_of::(), @@ -489,6 +512,28 @@ pub unsafe fn lzma_index_append( (*i).index_list_size += index_list_size_add as lzma_vli; LZMA_OK } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn index_prealloc_zero_keeps_append_usable() { + unsafe { + let allocator = core::ptr::null(); + let index = lzma_index_init(allocator); + assert!(!index.is_null()); + + lzma_index_prealloc(index, 0); + assert_eq!((*index).prealloc, INDEX_GROUP_SIZE as size_t); + + let ret = lzma_index_append(index, allocator, UNPADDED_SIZE_MIN, 0); + assert_eq!(ret, LZMA_OK); + + lzma_index_end(index, allocator); + } + } +} unsafe fn index_cat_helper(info: *const index_cat_info, this: *mut index_stream) { let left: *mut index_stream = (*this).node.left as *mut index_stream; let right: *mut index_stream = (*this).node.right as *mut index_stream; diff --git a/xz/src/common/index_decoder.rs b/xz/src/common/index_decoder.rs index 69d824c5..2a18162b 100644 --- a/xz/src/common/index_decoder.rs +++ b/xz/src/common/index_decoder.rs @@ -22,10 +22,10 @@ pub const SEQ_UNPADDED: index_decoder_seq = 3; pub const SEQ_MEMUSAGE: index_decoder_seq = 2; pub const SEQ_COUNT: index_decoder_seq = 1; pub const SEQ_INDICATOR: index_decoder_seq = 0; -unsafe extern "C" fn index_decode( +unsafe fn index_decode( coder_ptr: *mut c_void, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, _out: *mut u8, @@ -33,162 +33,184 @@ unsafe extern "C" fn index_decode( _out_size: size_t, _action: lzma_action, ) -> lzma_ret { - let mut current_block: u64; let coder: *mut lzma_index_coder = coder_ptr as *mut lzma_index_coder; let in_start: size_t = *in_pos; let mut ret: lzma_ret = LZMA_OK; + while *in_pos < in_size { - match (*coder).sequence { - 0 => { - let byte = *in_0.offset(*in_pos as isize); - *in_pos += 1; - if byte != INDEX_INDICATOR { - return LZMA_DATA_ERROR; - } - (*coder).sequence = SEQ_COUNT; - continue; - } - 1 => { - ret = lzma_vli_decode( - ::core::ptr::addr_of_mut!((*coder).count), - ::core::ptr::addr_of_mut!((*coder).pos), - in_0, - in_pos, - in_size, - ); - if ret != LZMA_STREAM_END { - break; - } - (*coder).pos = 0; - (*coder).sequence = SEQ_MEMUSAGE; - current_block = 7642845755631126846; - } - 2 => { - current_block = 7642845755631126846; - } - 3 | 4 => { - let size: *mut lzma_vli = if (*coder).sequence == SEQ_UNPADDED { - ::core::ptr::addr_of_mut!((*coder).unpadded_size) - } else { - ::core::ptr::addr_of_mut!((*coder).uncompressed_size) - }; - ret = lzma_vli_decode( - size, - ::core::ptr::addr_of_mut!((*coder).pos), - in_0, - in_pos, - in_size, - ); - if ret != LZMA_STREAM_END { - break; - } - ret = LZMA_OK; - (*coder).pos = 0; - if (*coder).sequence == SEQ_UNPADDED { - if (*coder).unpadded_size < UNPADDED_SIZE_MIN - || (*coder).unpadded_size > UNPADDED_SIZE_MAX - { + loop { + match (*coder).sequence { + SEQ_INDICATOR => { + let byte = *input.add(*in_pos); + *in_pos += 1; + if byte != INDEX_INDICATOR { return LZMA_DATA_ERROR; } - (*coder).sequence = SEQ_UNCOMPRESSED; - } else { - let ret_: lzma_ret = lzma_index_append( - (*coder).index, - allocator, - (*coder).unpadded_size, - (*coder).uncompressed_size, - ); - if ret_ != LZMA_OK { - return ret_; - } - (*coder).count -= 1; - (*coder).sequence = (if (*coder).count == 0 { - SEQ_PADDING_INIT - } else { - SEQ_UNPADDED - }) as index_decoder_seq; + + (*coder).sequence = SEQ_COUNT; + break; } - continue; - } - 5 => { - (*coder).pos = lzma_index_padding_size((*coder).index) as size_t; - (*coder).sequence = SEQ_PADDING; - current_block = 8340016495055110192; - } - 6 => { - current_block = 8340016495055110192; - } - 7 => { - current_block = 9471676622948044094; - } - _ => return LZMA_PROG_ERROR, - } - match current_block { - 8340016495055110192 => { - if (*coder).pos > 0 { - (*coder).pos -= 1; - let byte = *in_0.offset(*in_pos as isize); - *in_pos += 1; - if byte != 0 { - return LZMA_DATA_ERROR; + + SEQ_COUNT => { + ret = lzma_vli_decode( + ::core::ptr::addr_of_mut!((*coder).count), + ::core::ptr::addr_of_mut!((*coder).pos), + input, + in_pos, + in_size, + ); + if ret != LZMA_STREAM_END { + return goto_out(coder, input, in_start, in_pos, ret); } + + (*coder).pos = 0; + (*coder).sequence = SEQ_MEMUSAGE; continue; - } else { - (*coder).crc32 = lzma_crc32( - in_0.offset(in_start as isize), - *in_pos - in_start, - (*coder).crc32, - ); - (*coder).sequence = SEQ_CRC32; } - } - 7642845755631126846 => { - if lzma_index_memusage(1, (*coder).count) > (*coder).memlimit { - ret = LZMA_MEMLIMIT_ERROR; - break; - } else { + + SEQ_MEMUSAGE => { + if lzma_index_memusage(1, (*coder).count) > (*coder).memlimit { + ret = LZMA_MEMLIMIT_ERROR; + return goto_out(coder, input, in_start, in_pos, ret); + } + lzma_index_prealloc((*coder).index, (*coder).count); ret = LZMA_OK; - (*coder).sequence = (if (*coder).count == 0 { + (*coder).sequence = if (*coder).count == 0 { SEQ_PADDING_INIT } else { SEQ_UNPADDED - }) as index_decoder_seq; + }; + break; + } + + SEQ_UNPADDED | SEQ_UNCOMPRESSED => { + let size = if (*coder).sequence == SEQ_UNPADDED { + ::core::ptr::addr_of_mut!((*coder).unpadded_size) + } else { + ::core::ptr::addr_of_mut!((*coder).uncompressed_size) + }; + + ret = lzma_vli_decode( + size, + ::core::ptr::addr_of_mut!((*coder).pos), + input, + in_pos, + in_size, + ); + if ret != LZMA_STREAM_END { + return goto_out(coder, input, in_start, in_pos, ret); + } + + ret = LZMA_OK; + (*coder).pos = 0; + + if (*coder).sequence == SEQ_UNPADDED { + if (*coder).unpadded_size < UNPADDED_SIZE_MIN + || (*coder).unpadded_size > UNPADDED_SIZE_MAX + { + return LZMA_DATA_ERROR; + } + + (*coder).sequence = SEQ_UNCOMPRESSED; + } else { + let ret_ = lzma_index_append( + (*coder).index, + allocator, + (*coder).unpadded_size, + (*coder).uncompressed_size, + ); + if ret_ != LZMA_OK { + return ret_; + } + + (*coder).count -= 1; + (*coder).sequence = if (*coder).count == 0 { + SEQ_PADDING_INIT + } else { + SEQ_UNPADDED + }; + } + + break; + } + + SEQ_PADDING_INIT => { + (*coder).pos = lzma_index_padding_size((*coder).index) as size_t; + (*coder).sequence = SEQ_PADDING; continue; } - } - _ => {} - } - loop { - if *in_pos == in_size { - return LZMA_OK; - } - let val = *in_0.offset(*in_pos as isize); - *in_pos += 1; - if (*coder).crc32 >> ((*coder).pos * 8) & 0xff != val as u32 { - return LZMA_DATA_ERROR; - } - (*coder).pos += 1; - if (*coder).pos >= 4 { - break; + + SEQ_PADDING => { + if (*coder).pos > 0 { + (*coder).pos -= 1; + let byte = *input.add(*in_pos); + *in_pos += 1; + if byte != 0x00 { + return LZMA_DATA_ERROR; + } + + break; + } + + (*coder).crc32 = + lzma_crc32(input.add(in_start), *in_pos - in_start, (*coder).crc32); + (*coder).sequence = SEQ_CRC32; + continue; + } + + SEQ_CRC32 => { + loop { + if *in_pos == in_size { + return LZMA_OK; + } + + let byte = *input.add(*in_pos); + *in_pos += 1; + if ((*coder).crc32 >> ((*coder).pos * 8)) & 0xff != byte as u32 { + return LZMA_DATA_ERROR; + } + + (*coder).pos += 1; + if (*coder).pos >= 4 { + break; + } + } + + *(*coder).index_ptr = (*coder).index; + (*coder).index = core::ptr::null_mut(); + return LZMA_STREAM_END; + } + + _ => return LZMA_PROG_ERROR, } } - *(*coder).index_ptr = (*coder).index; - (*coder).index = core::ptr::null_mut(); - return LZMA_STREAM_END; } - let in_used: size_t = *in_pos - in_start; + + goto_out(coder, input, in_start, in_pos, ret) +} + +#[inline(always)] +unsafe fn goto_out( + coder: *mut lzma_index_coder, + input: *const u8, + in_start: size_t, + in_pos: *mut size_t, + ret: lzma_ret, +) -> lzma_ret { + let in_used = *in_pos - in_start; if in_used > 0 { - (*coder).crc32 = lzma_crc32(in_0.offset(in_start as isize), in_used, (*coder).crc32); + (*coder).crc32 = lzma_crc32(input.add(in_start), in_used, (*coder).crc32); } + ret } -unsafe extern "C" fn index_decoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { +unsafe fn index_decoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_index_coder = coder_ptr as *mut lzma_index_coder; lzma_index_end((*coder).index, allocator); crate::alloc::internal_free(coder as *mut c_void, allocator); } -unsafe extern "C" fn index_decoder_memconfig( +unsafe fn index_decoder_memconfig( coder_ptr: *mut c_void, memusage: *mut u64, old_memlimit: *mut u64, @@ -224,7 +246,7 @@ unsafe fn index_decoder_reset( (*coder).crc32 = 0; LZMA_OK } -pub(crate) unsafe extern "C" fn lzma_index_decoder_init( +pub(crate) unsafe fn lzma_index_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, i: *mut *mut lzma_index, @@ -232,7 +254,7 @@ pub(crate) unsafe extern "C" fn lzma_index_decoder_init( ) -> lzma_ret { if core::mem::transmute::< Option< - unsafe extern "C" fn( + unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *mut *mut lzma_index, @@ -242,7 +264,7 @@ pub(crate) unsafe extern "C" fn lzma_index_decoder_init( uintptr_t, >(Some( lzma_index_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *mut *mut lzma_index, @@ -254,7 +276,7 @@ pub(crate) unsafe extern "C" fn lzma_index_decoder_init( } (*next).init = core::mem::transmute::< Option< - unsafe extern "C" fn( + unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *mut *mut lzma_index, @@ -264,7 +286,7 @@ pub(crate) unsafe extern "C" fn lzma_index_decoder_init( uintptr_t, >(Some( lzma_index_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *mut *mut lzma_index, @@ -283,7 +305,7 @@ pub(crate) unsafe extern "C" fn lzma_index_decoder_init( (*next).coder = coder as *mut c_void; (*next).code = Some( index_decode - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -295,12 +317,10 @@ pub(crate) unsafe extern "C" fn lzma_index_decoder_init( lzma_action, ) -> lzma_ret, ); - (*next).end = Some( - index_decoder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> (), - ); + (*next).end = + Some(index_decoder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); (*next).memconfig = Some( - index_decoder_memconfig - as unsafe extern "C" fn(*mut c_void, *mut u64, *mut u64, u64) -> lzma_ret, + index_decoder_memconfig as unsafe fn(*mut c_void, *mut u64, *mut u64, u64) -> lzma_ret, ); (*coder).index = core::ptr::null_mut(); } else { @@ -338,14 +358,14 @@ pub unsafe fn lzma_index_buffer_decode( i: *mut *mut lzma_index, memlimit: *mut u64, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, ) -> lzma_ret { if !i.is_null() { *i = core::ptr::null_mut(); } - if i.is_null() || memlimit.is_null() || in_0.is_null() || in_pos.is_null() || *in_pos > in_size + if i.is_null() || memlimit.is_null() || input.is_null() || in_pos.is_null() || *in_pos > in_size { return LZMA_PROG_ERROR; } @@ -369,7 +389,7 @@ pub unsafe fn lzma_index_buffer_decode( let mut ret: lzma_ret = index_decode( ::core::ptr::addr_of_mut!(coder) as *mut c_void, allocator, - in_0, + input, in_pos, in_size, core::ptr::null_mut(), diff --git a/xz/src/common/index_encoder.rs b/xz/src/common/index_encoder.rs index 8b27986a..386b3a88 100644 --- a/xz/src/common/index_encoder.rs +++ b/xz/src/common/index_encoder.rs @@ -17,7 +17,7 @@ pub const SEQ_UNCOMPRESSED: index_encoder_seq = 3; pub const SEQ_UNPADDED: index_encoder_seq = 2; pub const SEQ_COUNT: index_encoder_seq = 1; pub const SEQ_INDICATOR: index_encoder_seq = 0; -unsafe extern "C" fn index_encode( +unsafe fn index_encode( coder_ptr: *mut c_void, _allocator: *const lzma_allocator, _in_0: *const u8, @@ -28,7 +28,6 @@ unsafe extern "C" fn index_encode( out_size: size_t, _action: lzma_action, ) -> lzma_ret { - let mut current_block: u64; let coder: *mut lzma_index_coder = coder_ptr as *mut lzma_index_coder; let out_start: size_t = *out_pos; let mut ret: lzma_ret = LZMA_OK; @@ -69,11 +68,8 @@ unsafe extern "C" fn index_encode( } else { (*coder).sequence = SEQ_UNPADDED; } - current_block = 10048703153582371463; - } - 2 | 3 => { - current_block = 10048703153582371463; } + 2 | 3 => {} 5 => { if (*coder).pos > 0 { (*coder).pos -= 1; @@ -88,49 +84,43 @@ unsafe extern "C" fn index_encode( ); (*coder).sequence = SEQ_CRC32; } - current_block = 10175200006830010844; - } - 6 => { - current_block = 10175200006830010844; } + 6 => {} _ => return LZMA_PROG_ERROR, } - match current_block { - 10048703153582371463 => { - let size: lzma_vli = if (*coder).sequence == SEQ_UNPADDED { - (*coder).iter.block.unpadded_size - } else { - (*coder).iter.block.uncompressed_size - }; - ret = lzma_vli_encode( - size, - ::core::ptr::addr_of_mut!((*coder).pos), - out, - out_pos, - out_size, - ); - if ret != LZMA_STREAM_END { - break; - } - ret = LZMA_OK; - (*coder).pos = 0; - (*coder).sequence += 1; + if (*coder).sequence == SEQ_UNPADDED || (*coder).sequence == SEQ_UNCOMPRESSED { + let size: lzma_vli = if (*coder).sequence == SEQ_UNPADDED { + (*coder).iter.block.unpadded_size + } else { + (*coder).iter.block.uncompressed_size + }; + ret = lzma_vli_encode( + size, + ::core::ptr::addr_of_mut!((*coder).pos), + out, + out_pos, + out_size, + ); + if ret != LZMA_STREAM_END { + break; } - _ => { - loop { - if *out_pos == out_size { - return LZMA_OK; - } - *out.offset(*out_pos as isize) = - ((*coder).crc32 >> ((*coder).pos * 8) & 0xff) as u8; - *out_pos += 1; - (*coder).pos += 1; - if (*coder).pos >= 4 { - break; - } + ret = LZMA_OK; + (*coder).pos = 0; + (*coder).sequence += 1; + } else { + loop { + if *out_pos == out_size { + return LZMA_OK; + } + *out.offset(*out_pos as isize) = + ((*coder).crc32 >> ((*coder).pos * 8) & 0xff) as u8; + *out_pos += 1; + (*coder).pos += 1; + if (*coder).pos >= 4 { + break; } - return LZMA_STREAM_END; } + return LZMA_STREAM_END; } } let out_used: size_t = *out_pos - out_start; @@ -139,7 +129,7 @@ unsafe extern "C" fn index_encode( } ret } -unsafe extern "C" fn index_encoder_end(coder: *mut c_void, allocator: *const lzma_allocator) { +unsafe fn index_encoder_end(coder: *mut c_void, allocator: *const lzma_allocator) { crate::alloc::internal_free(coder, allocator); } unsafe fn index_encoder_reset(coder: *mut lzma_index_coder, i: *const lzma_index) { @@ -149,23 +139,19 @@ unsafe fn index_encoder_reset(coder: *mut lzma_index_coder, i: *const lzma_index (*coder).pos = 0; (*coder).crc32 = 0; } -pub(crate) unsafe extern "C" fn lzma_index_encoder_init( +pub(crate) unsafe fn lzma_index_encoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, i: *const lzma_index, ) -> lzma_ret { if core::mem::transmute::< Option< - unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *const lzma_index, - ) -> lzma_ret, + unsafe fn(*mut lzma_next_coder, *const lzma_allocator, *const lzma_index) -> lzma_ret, >, uintptr_t, >(Some( lzma_index_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_index, @@ -176,16 +162,12 @@ pub(crate) unsafe extern "C" fn lzma_index_encoder_init( } (*next).init = core::mem::transmute::< Option< - unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *const lzma_index, - ) -> lzma_ret, + unsafe fn(*mut lzma_next_coder, *const lzma_allocator, *const lzma_index) -> lzma_ret, >, uintptr_t, >(Some( lzma_index_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_index, @@ -202,7 +184,7 @@ pub(crate) unsafe extern "C" fn lzma_index_encoder_init( } (*next).code = Some( index_encode - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -214,26 +196,25 @@ pub(crate) unsafe extern "C" fn lzma_index_encoder_init( lzma_action, ) -> lzma_ret, ); - (*next).end = Some( - index_encoder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> (), - ); + (*next).end = + Some(index_encoder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); } index_encoder_reset((*next).coder as *mut lzma_index_coder, i); LZMA_OK } pub unsafe fn lzma_index_encoder(strm: *mut lzma_stream, i: *const lzma_index) -> lzma_ret { - let ret_: lzma_ret = lzma_strm_init(strm); - if ret_ != LZMA_OK { - return ret_; + let ret: lzma_ret = lzma_strm_init(strm); + if ret != LZMA_OK { + return ret; } - let ret__0: lzma_ret = lzma_index_encoder_init( + let ret: lzma_ret = lzma_index_encoder_init( ::core::ptr::addr_of_mut!((*(*strm).internal).next), (*strm).allocator, i, ); - if ret__0 != LZMA_OK { + if ret != LZMA_OK { lzma_end(strm); - return ret__0; + return ret; } (*(*strm).internal).supported_actions[LZMA_RUN as usize] = true; (*(*strm).internal).supported_actions[LZMA_FINISH as usize] = true; diff --git a/xz/src/common/index_hash.rs b/xz/src/common/index_hash.rs index 1f0ceaf5..29965817 100644 --- a/xz/src/common/index_hash.rs +++ b/xz/src/common/index_hash.rs @@ -143,11 +143,10 @@ pub unsafe fn lzma_index_hash_append( } pub unsafe fn lzma_index_hash_decode( index_hash: *mut lzma_index_hash, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, ) -> lzma_ret { - let mut current_block: u64; if *in_pos >= in_size { return LZMA_BUF_ERROR; } @@ -156,7 +155,7 @@ pub unsafe fn lzma_index_hash_decode( while *in_pos < in_size { match (*index_hash).sequence { 0 => { - let byte = *in_0.offset(*in_pos as isize); + let byte = *input.offset(*in_pos as isize); *in_pos += 1; if byte != INDEX_INDICATOR { return LZMA_DATA_ERROR; @@ -168,7 +167,7 @@ pub unsafe fn lzma_index_hash_decode( ret = lzma_vli_decode( ::core::ptr::addr_of_mut!((*index_hash).remaining), ::core::ptr::addr_of_mut!((*index_hash).pos), - in_0, + input, in_pos, in_size, ); @@ -196,7 +195,7 @@ pub unsafe fn lzma_index_hash_decode( ret = lzma_vli_decode( size, ::core::ptr::addr_of_mut!((*index_hash).pos), - in_0, + input, in_pos, in_size, ); @@ -241,68 +240,54 @@ pub unsafe fn lzma_index_hash_decode( (*index_hash).records.index_list_size, )) & 3) as size_t; (*index_hash).sequence = SEQ_PADDING; - current_block = 12753679906265593574; - } - 5 => { - current_block = 12753679906265593574; - } - 6 => { - current_block = 88292361528268742; + continue; } + 5 => {} + 6 => break, _ => return LZMA_PROG_ERROR, } - match current_block { - 12753679906265593574 => { - if (*index_hash).pos > 0 { - (*index_hash).pos -= 1; - let byte = *in_0.offset(*in_pos as isize); - *in_pos += 1; - if byte != 0 { - return LZMA_DATA_ERROR; - } - continue; - } else { - if (*index_hash).blocks.blocks_size != (*index_hash).records.blocks_size - || (*index_hash).blocks.uncompressed_size - != (*index_hash).records.uncompressed_size - || (*index_hash).blocks.index_list_size - != (*index_hash).records.index_list_size - { - return LZMA_DATA_ERROR; - } - lzma_check_finish( - ::core::ptr::addr_of_mut!((*index_hash).blocks.check), - LZMA_CHECK_SHA256, - ); - lzma_check_finish( - ::core::ptr::addr_of_mut!((*index_hash).records.check), - LZMA_CHECK_SHA256, - ); - if memcmp( - ::core::ptr::addr_of_mut!((*index_hash).blocks.check.buffer.u8_0) - as *const c_void, - ::core::ptr::addr_of_mut!((*index_hash).records.check.buffer.u8_0) - as *const c_void, - lzma_check_size(LZMA_CHECK_SHA256) as size_t, - ) != 0 - { - return LZMA_DATA_ERROR; - } - (*index_hash).crc32 = lzma_crc32( - in_0.offset(in_start as isize), - *in_pos - in_start, - (*index_hash).crc32, - ); - (*index_hash).sequence = SEQ_CRC32; - } + if (*index_hash).pos > 0 { + (*index_hash).pos -= 1; + let byte = *input.offset(*in_pos as isize); + *in_pos += 1; + if byte != 0 { + return LZMA_DATA_ERROR; } - _ => {} + continue; + } + if (*index_hash).blocks.blocks_size != (*index_hash).records.blocks_size + || (*index_hash).blocks.uncompressed_size != (*index_hash).records.uncompressed_size + || (*index_hash).blocks.index_list_size != (*index_hash).records.index_list_size + { + return LZMA_DATA_ERROR; + } + lzma_check_finish( + ::core::ptr::addr_of_mut!((*index_hash).blocks.check), + LZMA_CHECK_SHA256, + ); + lzma_check_finish( + ::core::ptr::addr_of_mut!((*index_hash).records.check), + LZMA_CHECK_SHA256, + ); + if memcmp( + ::core::ptr::addr_of_mut!((*index_hash).blocks.check.buffer.u8_0) as *const c_void, + ::core::ptr::addr_of_mut!((*index_hash).records.check.buffer.u8_0) as *const c_void, + lzma_check_size(LZMA_CHECK_SHA256) as size_t, + ) != 0 + { + return LZMA_DATA_ERROR; } + (*index_hash).crc32 = lzma_crc32( + input.offset(in_start as isize), + *in_pos - in_start, + (*index_hash).crc32, + ); + (*index_hash).sequence = SEQ_CRC32; loop { if *in_pos == in_size { return LZMA_OK; } - let val = *in_0.offset(*in_pos as isize); + let val = *input.offset(*in_pos as isize); *in_pos += 1; if (*index_hash).crc32 >> ((*index_hash).pos * 8) & 0xff != val as u32 { return LZMA_DATA_ERROR; @@ -316,8 +301,11 @@ pub unsafe fn lzma_index_hash_decode( } let in_used: size_t = *in_pos - in_start; if in_used > 0 { - (*index_hash).crc32 = - lzma_crc32(in_0.offset(in_start as isize), in_used, (*index_hash).crc32); + (*index_hash).crc32 = lzma_crc32( + input.offset(in_start as isize), + in_used, + (*index_hash).crc32, + ); } ret } diff --git a/xz/src/common/lzip_decoder.rs b/xz/src/common/lzip_decoder.rs index 9e8c95ee..358c8799 100644 --- a/xz/src/common/lzip_decoder.rs +++ b/xz/src/common/lzip_decoder.rs @@ -26,28 +26,32 @@ pub const SEQ_DICT_SIZE: lzip_decoder_seq = 2; pub const SEQ_VERSION: lzip_decoder_seq = 1; pub const SEQ_ID_STRING: lzip_decoder_seq = 0; #[inline] -fn read64le(buf: *const u8) -> u64 { - return unsafe { - let mut num: u64 = *buf as u64; - num |= (*buf.offset(1) as u64) << 8; - num |= (*buf.offset(2) as u64) << 16; - num |= (*buf.offset(3) as u64) << 24; - num |= (*buf.offset(4) as u64) << 32; - num |= (*buf.offset(5) as u64) << 40; - num |= (*buf.offset(6) as u64) << 48; - num |= (*buf.offset(7) as u64) << 56; - num - }; +fn read64le(buf: &[u8; 8]) -> u64 { + let mut num: u64 = buf[0] as u64; + num |= (buf[1] as u64) << 8; + num |= (buf[2] as u64) << 16; + num |= (buf[3] as u64) << 24; + num |= (buf[4] as u64) << 32; + num |= (buf[5] as u64) << 40; + num |= (buf[6] as u64) << 48; + num |= (buf[7] as u64) << 56; + num } pub const LZIP_V0_FOOTER_SIZE: u32 = 12; pub const LZIP_V1_FOOTER_SIZE: u32 = 20; pub const LZIP_LC: u32 = 3; pub const LZIP_LP: u32 = 0; pub const LZIP_PB: u32 = 2; -unsafe extern "C" fn lzip_decode( +type LzipBlockState = u8; +const LZIP_BLOCK_VERSION: LzipBlockState = 0; +const LZIP_BLOCK_DICT_SIZE: LzipBlockState = 1; +const LZIP_BLOCK_CODER_INIT: LzipBlockState = 2; +const LZIP_BLOCK_LZMA_STREAM: LzipBlockState = 3; +const LZIP_BLOCK_MEMBER_FOOTER: LzipBlockState = 4; +unsafe fn lzip_decode( coder_ptr: *mut c_void, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, @@ -57,7 +61,7 @@ unsafe extern "C" fn lzip_decode( ) -> lzma_ret { let coder: *mut lzma_lzip_coder = coder_ptr as *mut lzma_lzip_coder; loop { - let mut current_block_80: u64; + let mut block_state: LzipBlockState; match (*coder).sequence { 0 => { let lzip_id_string: [u8; 4] = [0x4c as u8, 0x5a as u8, 0x49 as u8, 0x50 as u8]; @@ -69,7 +73,7 @@ unsafe extern "C" fn lzip_decode( LZMA_OK }; } - if *in_0.offset(*in_pos as isize) != lzip_id_string[(*coder).pos as usize] { + if *input.offset(*in_pos as isize) != lzip_id_string[(*coder).pos as usize] { return if !(*coder).first_member { LZMA_STREAM_END } else { @@ -84,31 +88,31 @@ unsafe extern "C" fn lzip_decode( (*coder).uncompressed_size = 0; (*coder).member_size = core::mem::size_of::<[u8; 4]>() as u64; (*coder).sequence = SEQ_VERSION; - current_block_80 = 11220331375136032509; + block_state = LZIP_BLOCK_VERSION; } 1 => { - current_block_80 = 11220331375136032509; + block_state = LZIP_BLOCK_VERSION; } 2 => { - current_block_80 = 2770508642018830579; + block_state = LZIP_BLOCK_DICT_SIZE; } 3 => { - current_block_80 = 15476230294461844687; + block_state = LZIP_BLOCK_CODER_INIT; } 4 => { - current_block_80 = 13394712405657322686; + block_state = LZIP_BLOCK_LZMA_STREAM; } 5 => { - current_block_80 = 13619784596304402172; + block_state = LZIP_BLOCK_MEMBER_FOOTER; } _ => return LZMA_PROG_ERROR, } - match current_block_80 { - 11220331375136032509 => { + match block_state { + LZIP_BLOCK_VERSION => { if *in_pos >= in_size { return LZMA_OK; } - (*coder).version = *in_0.offset(*in_pos as isize) as u32; + (*coder).version = *input.offset(*in_pos as isize) as u32; *in_pos += 1; if (*coder).version > 1 { return LZMA_OPTIONS_ERROR; @@ -118,16 +122,16 @@ unsafe extern "C" fn lzip_decode( if (*coder).tell_any_check { return LZMA_GET_CHECK; } - current_block_80 = 2770508642018830579; + block_state = LZIP_BLOCK_DICT_SIZE; } _ => {} } - match current_block_80 { - 2770508642018830579 => { + match block_state { + LZIP_BLOCK_DICT_SIZE => { if *in_pos >= in_size { return LZMA_OK; } - let ds: u32 = *in_0.offset(*in_pos as isize) as u32; + let ds: u32 = *input.offset(*in_pos as isize) as u32; *in_pos += 1; (*coder).member_size += 1; let b2log: u32 = ds & 0x1f; @@ -146,12 +150,12 @@ unsafe extern "C" fn lzip_decode( as *const c_void) .wrapping_add(LZMA_MEMUSAGE_BASE); (*coder).sequence = SEQ_CODER_INIT; - current_block_80 = 15476230294461844687; + block_state = LZIP_BLOCK_CODER_INIT; } _ => {} } - match current_block_80 { - 15476230294461844687 => { + match block_state { + LZIP_BLOCK_CODER_INIT => { if (*coder).memusage > (*coder).memlimit { return LZMA_MEMLIMIT_ERROR; } @@ -160,12 +164,11 @@ unsafe extern "C" fn lzip_decode( id: LZMA_FILTER_LZMA1, init: Some( lzma_lzma_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, - ) - -> lzma_ret, + ) -> lzma_ret, ), options: ::core::ptr::addr_of_mut!((*coder).options) as *mut c_void, }, @@ -185,18 +188,21 @@ unsafe extern "C" fn lzip_decode( } (*coder).crc32 = 0; (*coder).sequence = SEQ_LZMA_STREAM; - current_block_80 = 13394712405657322686; + block_state = LZIP_BLOCK_LZMA_STREAM; } _ => {} } - match current_block_80 { - 13394712405657322686 => { + match block_state { + LZIP_BLOCK_LZMA_STREAM => { let in_start: size_t = *in_pos; let out_start: size_t = *out_pos; - let ret: lzma_ret = (*coder).lzma_decoder.code.unwrap()( + let Some(code) = (*coder).lzma_decoder.code else { + return LZMA_PROG_ERROR; + }; + let ret: lzma_ret = code( (*coder).lzma_decoder.coder, allocator, - in_0, + input, in_pos, in_size, out, @@ -224,7 +230,7 @@ unsafe extern "C" fn lzip_decode( LZIP_V1_FOOTER_SIZE }) as size_t; lzma_bufcpy( - in_0, + input, in_pos, in_size, ::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8, @@ -236,25 +242,15 @@ unsafe extern "C" fn lzip_decode( } (*coder).pos = 0; (*coder).member_size += footer_size as u64; - if !(*coder).ignore_check - && (*coder).crc32 - != read32le((::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8) as *mut u8) - { + let footer = ::core::ptr::addr_of!((*coder).buffer) as *const u8; + if !(*coder).ignore_check && (*coder).crc32 != read32le(&*footer.cast::<[u8; 4]>()) { return LZMA_DATA_ERROR; } - if (*coder).uncompressed_size - != read64le( - (::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8).offset(4) as *mut u8, - ) - { + if (*coder).uncompressed_size != read64le(&*footer.add(4).cast::<[u8; 8]>()) { return LZMA_DATA_ERROR; } if (*coder).version > 0 { - if (*coder).member_size - != read64le( - (::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8).offset(12) as *mut u8, - ) - { + if (*coder).member_size != read64le(&*footer.add(12).cast::<[u8; 8]>()) { return LZMA_DATA_ERROR; } } @@ -265,15 +261,15 @@ unsafe extern "C" fn lzip_decode( (*coder).sequence = SEQ_ID_STRING; } } -unsafe extern "C" fn lzip_decoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { +unsafe fn lzip_decoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_lzip_coder = coder_ptr as *mut lzma_lzip_coder; lzma_next_end(::core::ptr::addr_of_mut!((*coder).lzma_decoder), allocator); crate::alloc::internal_free(coder as *mut c_void, allocator); } -extern "C" fn lzip_decoder_get_check(_coder_ptr: *const c_void) -> lzma_check { +unsafe fn lzip_decoder_get_check(_coder_ptr: *const c_void) -> lzma_check { LZMA_CHECK_CRC32 } -unsafe extern "C" fn lzip_decoder_memconfig( +unsafe fn lzip_decoder_memconfig( coder_ptr: *mut c_void, memusage: *mut u64, old_memlimit: *mut u64, @@ -290,42 +286,28 @@ unsafe extern "C" fn lzip_decoder_memconfig( } LZMA_OK } -pub(crate) unsafe extern "C" fn lzma_lzip_decoder_init( +pub(crate) unsafe fn lzma_lzip_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, memlimit: u64, flags: u32, ) -> lzma_ret { if core::mem::transmute::< - Option< - unsafe extern "C" fn(*mut lzma_next_coder, *const lzma_allocator, u64, u32) -> lzma_ret, - >, + Option lzma_ret>, uintptr_t, >(Some( lzma_lzip_decoder_init - as unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - u64, - u32, - ) -> lzma_ret, + as unsafe fn(*mut lzma_next_coder, *const lzma_allocator, u64, u32) -> lzma_ret, )) != (*next).init { lzma_next_end(next, allocator); } (*next).init = core::mem::transmute::< - Option< - unsafe extern "C" fn(*mut lzma_next_coder, *const lzma_allocator, u64, u32) -> lzma_ret, - >, + Option lzma_ret>, uintptr_t, >(Some( lzma_lzip_decoder_init - as unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - u64, - u32, - ) -> lzma_ret, + as unsafe fn(*mut lzma_next_coder, *const lzma_allocator, u64, u32) -> lzma_ret, )); if flags & !(LZMA_SUPPORTED_FLAGS as u32) != 0 { return LZMA_OPTIONS_ERROR; @@ -339,7 +321,7 @@ pub(crate) unsafe extern "C" fn lzma_lzip_decoder_init( (*next).coder = coder as *mut c_void; (*next).code = Some( lzip_decode - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -351,14 +333,10 @@ pub(crate) unsafe extern "C" fn lzma_lzip_decoder_init( lzma_action, ) -> lzma_ret, ); - (*next).end = Some( - lzip_decoder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> (), - ); - (*next).get_check = - Some(lzip_decoder_get_check as unsafe extern "C" fn(*const c_void) -> lzma_check); + (*next).end = Some(lzip_decoder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); + (*next).get_check = Some(lzip_decoder_get_check as unsafe fn(*const c_void) -> lzma_check); (*next).memconfig = Some( - lzip_decoder_memconfig - as unsafe extern "C" fn(*mut c_void, *mut u64, *mut u64, u64) -> lzma_ret, + lzip_decoder_memconfig as unsafe fn(*mut c_void, *mut u64, *mut u64, u64) -> lzma_ret, ); (*coder).lzma_decoder = lzma_next_coder_s { coder: core::ptr::null_mut(), diff --git a/xz/src/common/microlzma_decoder.rs b/xz/src/common/microlzma_decoder.rs index a29d2c9b..d483e5a0 100644 --- a/xz/src/common/microlzma_decoder.rs +++ b/xz/src/common/microlzma_decoder.rs @@ -9,10 +9,10 @@ pub struct lzma_microlzma_coder { pub uncomp_size_is_exact: bool, pub props_decoded: bool, } -unsafe extern "C" fn microlzma_decode( +unsafe fn microlzma_decode( coder_ptr: *mut c_void, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, mut in_size: size_t, out: *mut u8, @@ -65,7 +65,7 @@ unsafe extern "C" fn microlzma_decode( } if lzma_lzma_lclppb_decode( ::core::ptr::addr_of_mut!(options), - !*in_0.offset(*in_pos as isize), + !*input.offset(*in_pos as isize), ) { return LZMA_OPTIONS_ERROR; } @@ -75,7 +75,7 @@ unsafe extern "C" fn microlzma_decode( id: LZMA_FILTER_LZMA1EXT, init: Some( lzma_lzma_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -89,17 +89,20 @@ unsafe extern "C" fn microlzma_decode( options: core::ptr::null_mut(), }, ]; - let ret_: lzma_ret = lzma_next_filter_init( + let ret: lzma_ret = lzma_next_filter_init( ::core::ptr::addr_of_mut!((*coder).lzma), allocator, ::core::ptr::addr_of_mut!(filters) as *mut lzma_filter_info, ); - if ret_ != LZMA_OK { - return ret_; + if ret != LZMA_OK { + return ret; } let dummy_in: u8 = 0; let mut dummy_in_pos: size_t = 0; - if (*coder).lzma.code.unwrap()( + let Some(code) = (*coder).lzma.code else { + return LZMA_PROG_ERROR; + }; + if code( (*coder).lzma.coder, allocator, ::core::ptr::addr_of!(dummy_in), @@ -115,10 +118,13 @@ unsafe extern "C" fn microlzma_decode( } (*coder).props_decoded = true; } - let mut ret: lzma_ret = (*coder).lzma.code.unwrap()( + let Some(code) = (*coder).lzma.code else { + return LZMA_PROG_ERROR; + }; + let mut ret: lzma_ret = code( (*coder).lzma.coder, allocator, - in_0, + input, in_pos, in_size, out, @@ -141,15 +147,12 @@ unsafe extern "C" fn microlzma_decode( } ret } -unsafe extern "C" fn microlzma_decoder_end( - coder_ptr: *mut c_void, - allocator: *const lzma_allocator, -) { +unsafe fn microlzma_decoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_microlzma_coder = coder_ptr as *mut lzma_microlzma_coder; lzma_next_end(::core::ptr::addr_of_mut!((*coder).lzma), allocator); crate::alloc::internal_free(coder as *mut c_void, allocator); } -unsafe extern "C" fn microlzma_decoder_init( +unsafe fn microlzma_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, comp_size: u64, @@ -159,19 +162,12 @@ unsafe extern "C" fn microlzma_decoder_init( ) -> lzma_ret { if core::mem::transmute::< Option< - unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - u64, - u64, - bool, - u32, - ) -> lzma_ret, + unsafe fn(*mut lzma_next_coder, *const lzma_allocator, u64, u64, bool, u32) -> lzma_ret, >, uintptr_t, >(Some( microlzma_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, u64, @@ -185,19 +181,12 @@ unsafe extern "C" fn microlzma_decoder_init( } (*next).init = core::mem::transmute::< Option< - unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - u64, - u64, - bool, - u32, - ) -> lzma_ret, + unsafe fn(*mut lzma_next_coder, *const lzma_allocator, u64, u64, bool, u32) -> lzma_ret, >, uintptr_t, >(Some( microlzma_decoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, u64, @@ -215,7 +204,7 @@ unsafe extern "C" fn microlzma_decoder_init( (*next).coder = coder as *mut c_void; (*next).code = Some( microlzma_decode - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -227,9 +216,8 @@ unsafe extern "C" fn microlzma_decoder_init( lzma_action, ) -> lzma_ret, ); - (*next).end = Some( - microlzma_decoder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> (), - ); + (*next).end = + Some(microlzma_decoder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); (*coder).lzma = lzma_next_coder_s { coder: core::ptr::null_mut(), id: LZMA_VLI_UNKNOWN, @@ -260,11 +248,11 @@ pub unsafe fn lzma_microlzma_decoder( uncomp_size_is_exact: lzma_bool, dict_size: u32, ) -> lzma_ret { - let ret_: lzma_ret = lzma_strm_init(strm); - if ret_ != LZMA_OK { - return ret_; + let ret: lzma_ret = lzma_strm_init(strm); + if ret != LZMA_OK { + return ret; } - let ret__0: lzma_ret = microlzma_decoder_init( + let ret: lzma_ret = microlzma_decoder_init( ::core::ptr::addr_of_mut!((*(*strm).internal).next), (*strm).allocator, comp_size, @@ -272,9 +260,9 @@ pub unsafe fn lzma_microlzma_decoder( uncomp_size_is_exact != 0, dict_size, ); - if ret__0 != LZMA_OK { + if ret != LZMA_OK { lzma_end(strm); - return ret__0; + return ret; } (*(*strm).internal).supported_actions[LZMA_RUN as usize] = true; (*(*strm).internal).supported_actions[LZMA_FINISH as usize] = true; diff --git a/xz/src/common/microlzma_encoder.rs b/xz/src/common/microlzma_encoder.rs index 207b9fde..be4f28b8 100644 --- a/xz/src/common/microlzma_encoder.rs +++ b/xz/src/common/microlzma_encoder.rs @@ -5,10 +5,10 @@ pub struct lzma_microlzma_coder { pub lzma: lzma_next_coder, pub props: u8, } -unsafe extern "C" fn microlzma_encode( +unsafe fn microlzma_encode( coder_ptr: *mut c_void, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, @@ -20,16 +20,10 @@ unsafe extern "C" fn microlzma_encode( let out_start: size_t = *out_pos; let in_start: size_t = *in_pos; let mut uncomp_size: u64 = 0; - let set_out_limit = if let Some(set_out_limit) = (*coder).lzma.set_out_limit { - set_out_limit - } else { - return LZMA_PROG_ERROR; - }; - let code = if let Some(code) = (*coder).lzma.code { - code - } else { - return LZMA_PROG_ERROR; - }; + debug_assert!((*coder).lzma.set_out_limit.is_some()); + let set_out_limit = (*coder).lzma.set_out_limit.unwrap_unchecked(); + debug_assert!((*coder).lzma.code.is_some()); + let code = (*coder).lzma.code.unwrap_unchecked(); if set_out_limit( (*coder).lzma.coder, ::core::ptr::addr_of_mut!(uncomp_size), @@ -41,7 +35,7 @@ unsafe extern "C" fn microlzma_encode( let ret: lzma_ret = code( (*coder).lzma.coder, allocator, - in_0, + input, in_pos, in_size, out, @@ -59,22 +53,19 @@ unsafe extern "C" fn microlzma_encode( *in_pos = in_start + uncomp_size as size_t; ret } -unsafe extern "C" fn microlzma_encoder_end( - coder_ptr: *mut c_void, - allocator: *const lzma_allocator, -) { +unsafe fn microlzma_encoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_microlzma_coder = coder_ptr as *mut lzma_microlzma_coder; lzma_next_end(::core::ptr::addr_of_mut!((*coder).lzma), allocator); crate::alloc::internal_free(coder as *mut c_void, allocator); } -unsafe extern "C" fn microlzma_encoder_init( +unsafe fn microlzma_encoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, options: *const lzma_options_lzma, ) -> lzma_ret { if core::mem::transmute::< Option< - unsafe extern "C" fn( + unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_options_lzma, @@ -83,7 +74,7 @@ unsafe extern "C" fn microlzma_encoder_init( uintptr_t, >(Some( microlzma_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_options_lzma, @@ -94,7 +85,7 @@ unsafe extern "C" fn microlzma_encoder_init( } (*next).init = core::mem::transmute::< Option< - unsafe extern "C" fn( + unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_options_lzma, @@ -103,7 +94,7 @@ unsafe extern "C" fn microlzma_encoder_init( uintptr_t, >(Some( microlzma_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_options_lzma, @@ -118,7 +109,7 @@ unsafe extern "C" fn microlzma_encoder_init( (*next).coder = coder as *mut c_void; (*next).code = Some( microlzma_encode - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -130,9 +121,8 @@ unsafe extern "C" fn microlzma_encoder_init( lzma_action, ) -> lzma_ret, ); - (*next).end = Some( - microlzma_encoder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> (), - ); + (*next).end = + Some(microlzma_encoder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); (*coder).lzma = lzma_next_coder_s { coder: core::ptr::null_mut(), id: LZMA_VLI_UNKNOWN, @@ -154,7 +144,7 @@ unsafe extern "C" fn microlzma_encoder_init( id: LZMA_FILTER_LZMA1, init: Some( lzma_lzma_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter_info, @@ -178,18 +168,18 @@ pub unsafe fn lzma_microlzma_encoder( strm: *mut lzma_stream, options: *const lzma_options_lzma, ) -> lzma_ret { - let ret_: lzma_ret = lzma_strm_init(strm); - if ret_ != LZMA_OK { - return ret_; + let ret: lzma_ret = lzma_strm_init(strm); + if ret != LZMA_OK { + return ret; } - let ret__0: lzma_ret = microlzma_encoder_init( + let ret: lzma_ret = microlzma_encoder_init( ::core::ptr::addr_of_mut!((*(*strm).internal).next), (*strm).allocator, options, ); - if ret__0 != LZMA_OK { + if ret != LZMA_OK { lzma_end(strm); - return ret__0; + return ret; } (*(*strm).internal).supported_actions[LZMA_FINISH as usize] = true; LZMA_OK diff --git a/xz/src/common/mod.rs b/xz/src/common/mod.rs index eec2ca1a..eca2ca25 100644 --- a/xz/src/common/mod.rs +++ b/xz/src/common/mod.rs @@ -9,6 +9,7 @@ pub mod block_header_decoder; pub mod block_header_encoder; pub mod block_util; pub mod common; +pub mod common_types; pub mod easy_buffer_encoder; pub mod easy_decoder_memusage; pub mod easy_encoder; @@ -43,6 +44,7 @@ pub mod stream_flags_decoder; pub mod stream_flags_encoder; pub mod stream_mt; pub mod string_conversion; +pub mod threading; pub mod vli_decoder; pub mod vli_encoder; pub mod vli_size; diff --git a/xz/src/common/outqueue.rs b/xz/src/common/outqueue.rs index 7f1c3e15..25c67ec7 100644 --- a/xz/src/common/outqueue.rs +++ b/xz/src/common/outqueue.rs @@ -159,14 +159,8 @@ pub unsafe fn lzma_outq_read( } pub unsafe fn lzma_outq_enable_partial_output( outq: *mut lzma_outq, - enable_partial_output: Option ()>, + enable_partial_output: unsafe fn(*mut c_void) -> (), ) { - let enable_partial_output = if let Some(enable_partial_output) = enable_partial_output { - enable_partial_output - } else { - return; - }; - if !(*outq).head.is_null() && !(*(*outq).head).finished && !(*(*outq).head).worker.is_null() { enable_partial_output((*(*outq).head).worker); (*(*outq).head).worker = core::ptr::null_mut(); diff --git a/xz/src/common/stream_buffer_decoder.rs b/xz/src/common/stream_buffer_decoder.rs index 36053c0d..3e1af85a 100644 --- a/xz/src/common/stream_buffer_decoder.rs +++ b/xz/src/common/stream_buffer_decoder.rs @@ -3,7 +3,7 @@ pub unsafe fn lzma_stream_buffer_decode( memlimit: *mut u64, flags: u32, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, @@ -11,7 +11,7 @@ pub unsafe fn lzma_stream_buffer_decode( out_size: size_t, ) -> lzma_ret { if in_pos.is_null() - || in_0.is_null() && *in_pos != in_size + || input.is_null() && *in_pos != in_size || *in_pos > in_size || out_pos.is_null() || out.is_null() && *out_pos != out_size @@ -41,18 +41,14 @@ pub unsafe fn lzma_stream_buffer_decode( flags, ); if ret == LZMA_OK { - let code = if let Some(code) = stream_decoder.code { - code - } else { - lzma_next_end(::core::ptr::addr_of_mut!(stream_decoder), allocator); - return LZMA_PROG_ERROR; - }; + debug_assert!(stream_decoder.code.is_some()); + let code = stream_decoder.code.unwrap_unchecked(); let in_start: size_t = *in_pos; let out_start: size_t = *out_pos; ret = code( stream_decoder.coder, allocator, - in_0, + input, in_pos, in_size, out, @@ -73,12 +69,8 @@ pub unsafe fn lzma_stream_buffer_decode( } } else if ret == LZMA_MEMLIMIT_ERROR { let mut memusage: u64 = 0; - let memconfig = if let Some(memconfig) = stream_decoder.memconfig { - memconfig - } else { - lzma_next_end(::core::ptr::addr_of_mut!(stream_decoder), allocator); - return LZMA_PROG_ERROR; - }; + debug_assert!(stream_decoder.memconfig.is_some()); + let memconfig = stream_decoder.memconfig.unwrap_unchecked(); memconfig( stream_decoder.coder, memlimit, diff --git a/xz/src/common/stream_buffer_encoder.rs b/xz/src/common/stream_buffer_encoder.rs index b89031b8..f3fe4bdf 100644 --- a/xz/src/common/stream_buffer_encoder.rs +++ b/xz/src/common/stream_buffer_encoder.rs @@ -18,7 +18,7 @@ pub unsafe fn lzma_stream_buffer_encode( filters: *mut lzma_filter, check: lzma_check, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_size: size_t, out: *mut u8, out_pos_ptr: *mut size_t, @@ -26,7 +26,7 @@ pub unsafe fn lzma_stream_buffer_encode( ) -> lzma_ret { if filters.is_null() || check > LZMA_CHECK_ID_MAX - || in_0.is_null() && in_size != 0 + || input.is_null() && in_size != 0 || out.is_null() || out_pos_ptr.is_null() || *out_pos_ptr > out_size @@ -101,17 +101,17 @@ pub unsafe fn lzma_stream_buffer_encode( reserved_bool8: 0, }; if in_size > 0 { - let ret_: lzma_ret = lzma_block_buffer_encode( + let ret: lzma_ret = lzma_block_buffer_encode( ::core::ptr::addr_of_mut!(block), allocator, - in_0, + input, in_size, out, ::core::ptr::addr_of_mut!(out_pos), out_size, ); - if ret_ != LZMA_OK { - return ret_; + if ret != LZMA_OK { + return ret; } } let i: *mut lzma_index = lzma_index_init(allocator); diff --git a/xz/src/common/stream_decoder.rs b/xz/src/common/stream_decoder.rs index 70071fcc..91a823b9 100644 --- a/xz/src/common/stream_decoder.rs +++ b/xz/src/common/stream_decoder.rs @@ -39,10 +39,10 @@ unsafe fn stream_decoder_reset( (*coder).pos = 0; LZMA_OK } -unsafe extern "C" fn stream_decode( +unsafe fn stream_decode( coder_ptr: *mut c_void, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, @@ -52,11 +52,10 @@ unsafe extern "C" fn stream_decode( ) -> lzma_ret { let coder: *mut lzma_stream_coder = coder_ptr as *mut lzma_stream_coder; loop { - let mut current_block_100: u64; match (*coder).sequence { - 0 => { + SEQ_STREAM_HEADER => { lzma_bufcpy( - in_0, + input, in_pos, in_size, ::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8, @@ -92,77 +91,124 @@ unsafe extern "C" fn stream_decode( if (*coder).tell_any_check { return LZMA_GET_CHECK; } - current_block_100 = 4166486009154926805; + continue; } - 1 => { - current_block_100 = 4166486009154926805; - } - 2 => { - current_block_100 = 3500765272169221397; - } - 3 => { - current_block_100 = 721385680381463314; - } - 4 => { + SEQ_BLOCK_HEADER => { if *in_pos >= in_size { return LZMA_OK; } - let ret_2: lzma_ret = - lzma_index_hash_decode((*coder).index_hash, in_0, in_pos, in_size); - if ret_2 != LZMA_STREAM_END { - return ret_2; + if (*coder).pos == 0 { + if *input.offset(*in_pos as isize) == INDEX_INDICATOR { + (*coder).sequence = SEQ_INDEX; + continue; + } + (*coder).block_options.header_size = + ((*input.offset(*in_pos as isize) as u32) + 1) * 4; } - (*coder).sequence = SEQ_STREAM_FOOTER; - current_block_100 = 17861496924281778896; + + lzma_bufcpy( + input, + in_pos, + in_size, + ::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8, + ::core::ptr::addr_of_mut!((*coder).pos), + (*coder).block_options.header_size as size_t, + ); + if (*coder).pos < (*coder).block_options.header_size as size_t { + return LZMA_OK; + } + + (*coder).pos = 0; + (*coder).sequence = SEQ_BLOCK_INIT; + continue; } - 5 => { - current_block_100 = 17861496924281778896; + SEQ_BLOCK_INIT => { + (*coder).block_options.version = 1; + let mut filters = MaybeUninit::<[lzma_filter; 5]>::uninit(); + let filters_ptr = filters.as_mut_ptr() as *mut lzma_filter; + (*coder).block_options.filters = filters_ptr; + let ret: lzma_ret = lzma_block_header_decode( + ::core::ptr::addr_of_mut!((*coder).block_options), + allocator, + ::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8, + ); + if ret != LZMA_OK { + lzma_filters_free(filters_ptr, allocator); + (*coder).block_options.filters = core::ptr::null_mut(); + return ret; + } + + (*coder).block_options.ignore_check = (*coder).ignore_check as lzma_bool; + + let memusage: u64 = lzma_raw_decoder_memusage(filters_ptr) as u64; + let ret = if memusage == UINT64_MAX { + LZMA_OPTIONS_ERROR + } else { + (*coder).memusage = memusage; + if memusage > (*coder).memlimit { + LZMA_MEMLIMIT_ERROR + } else { + lzma_block_decoder_init( + ::core::ptr::addr_of_mut!((*coder).block_decoder), + allocator, + ::core::ptr::addr_of_mut!((*coder).block_options), + ) + } + }; + + lzma_filters_free(filters_ptr, allocator); + (*coder).block_options.filters = core::ptr::null_mut(); + if ret != LZMA_OK { + return ret; + } + + (*coder).sequence = SEQ_BLOCK_RUN; + continue; } - 6 => { - current_block_100 = 15462640364611497761; + SEQ_BLOCK_RUN => { + debug_assert!((*coder).block_decoder.code.is_some()); + let code = (*coder).block_decoder.code.unwrap_unchecked(); + let ret: lzma_ret = code( + (*coder).block_decoder.coder, + allocator, + input, + in_pos, + in_size, + out, + out_pos, + out_size, + action, + ); + if ret != LZMA_STREAM_END { + return ret; + } + + let ret: lzma_ret = lzma_index_hash_append( + (*coder).index_hash, + lzma_block_unpadded_size(::core::ptr::addr_of_mut!((*coder).block_options)), + (*coder).block_options.uncompressed_size, + ); + if ret != LZMA_OK { + return ret; + } + + (*coder).sequence = SEQ_BLOCK_HEADER; } - _ => return LZMA_PROG_ERROR, - } - match current_block_100 { - 4166486009154926805 => { + SEQ_INDEX => { if *in_pos >= in_size { return LZMA_OK; } - if (*coder).pos == 0 { - if *in_0.offset(*in_pos as isize) == INDEX_INDICATOR { - (*coder).sequence = SEQ_INDEX; - current_block_100 = 16789764818708874114; - } else { - (*coder).block_options.header_size = - ((*in_0.offset(*in_pos as isize) as u32) + 1) * 4; - current_block_100 = 13242334135786603907; - } - } else { - current_block_100 = 13242334135786603907; - } - match current_block_100 { - 16789764818708874114 => {} - _ => { - lzma_bufcpy( - in_0, - in_pos, - in_size, - ::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8, - ::core::ptr::addr_of_mut!((*coder).pos), - (*coder).block_options.header_size as size_t, - ); - if (*coder).pos < (*coder).block_options.header_size as size_t { - return LZMA_OK; - } - (*coder).pos = 0; - (*coder).sequence = SEQ_BLOCK_INIT; - current_block_100 = 3500765272169221397; - } + let ret: lzma_ret = + lzma_index_hash_decode((*coder).index_hash, input, in_pos, in_size); + if ret != LZMA_STREAM_END { + return ret; } + (*coder).sequence = SEQ_STREAM_FOOTER; + continue; } - 17861496924281778896 => { + SEQ_STREAM_FOOTER => { lzma_bufcpy( - in_0, + input, in_pos, in_size, ::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8, @@ -172,40 +218,38 @@ unsafe extern "C" fn stream_decode( if (*coder).pos < LZMA_STREAM_HEADER_SIZE as size_t { return LZMA_OK; } + (*coder).pos = 0; - let mut footer_flags = MaybeUninit::::uninit(); - let ret_3: lzma_ret = lzma_stream_footer_decode( + let mut footer_flags = MaybeUninit::::zeroed(); + let ret: lzma_ret = lzma_stream_footer_decode( footer_flags.as_mut_ptr(), ::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8, ); - if ret_3 != LZMA_OK { - return if ret_3 == LZMA_FORMAT_ERROR { + if ret != LZMA_OK { + return if ret == LZMA_FORMAT_ERROR { LZMA_DATA_ERROR } else { - ret_3 + ret }; } let mut footer_flags = footer_flags.assume_init(); if lzma_index_hash_size((*coder).index_hash) != footer_flags.backward_size { return LZMA_DATA_ERROR; } - let ret__1: lzma_ret = lzma_stream_flags_compare( + let ret: lzma_ret = lzma_stream_flags_compare( ::core::ptr::addr_of_mut!((*coder).stream_flags), ::core::ptr::addr_of_mut!(footer_flags), ); - if ret__1 != LZMA_OK { - return ret__1; + if ret != LZMA_OK { + return ret; } if !(*coder).concatenated { return LZMA_STREAM_END; } (*coder).sequence = SEQ_STREAM_PADDING; - current_block_100 = 15462640364611497761; + continue; } - _ => {} - } - match current_block_100 { - 15462640364611497761 => { + SEQ_STREAM_PADDING => { loop { if *in_pos >= in_size { if action != LZMA_FINISH { @@ -217,105 +261,38 @@ unsafe extern "C" fn stream_decode( LZMA_DATA_ERROR }; } - if *in_0.offset(*in_pos as isize) != 0 { + if *input.offset(*in_pos as isize) != 0 { break; } *in_pos += 1; (*coder).pos = ((*coder).pos + 1) & 3; } + if (*coder).pos != 0 { *in_pos += 1; return LZMA_DATA_ERROR; } - let ret__2: lzma_ret = stream_decoder_reset(coder, allocator); - if ret__2 != LZMA_OK { - return ret__2; - } - current_block_100 = 16789764818708874114; - } - 3500765272169221397 => { - (*coder).block_options.version = 1; - let mut filters = MaybeUninit::<[lzma_filter; 5]>::uninit(); - let filters_ptr = filters.as_mut_ptr() as *mut lzma_filter; - (*coder).block_options.filters = filters_ptr; - let ret_: lzma_ret = lzma_block_header_decode( - ::core::ptr::addr_of_mut!((*coder).block_options), - allocator, - ::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8, - ); - if ret_ != LZMA_OK { - return ret_; - } - (*coder).block_options.ignore_check = (*coder).ignore_check as lzma_bool; - let memusage: u64 = lzma_raw_decoder_memusage(filters_ptr) as u64; - let mut ret_0: lzma_ret = LZMA_OK; - if memusage == UINT64_MAX { - ret_0 = LZMA_OPTIONS_ERROR; - } else { - (*coder).memusage = memusage; - if memusage > (*coder).memlimit { - ret_0 = LZMA_MEMLIMIT_ERROR; - } else { - ret_0 = lzma_block_decoder_init( - ::core::ptr::addr_of_mut!((*coder).block_decoder), - allocator, - ::core::ptr::addr_of_mut!((*coder).block_options), - ); - } - } - lzma_filters_free(filters_ptr, allocator); - (*coder).block_options.filters = core::ptr::null_mut(); - if ret_0 != LZMA_OK { - return ret_0; - } - (*coder).sequence = SEQ_BLOCK_RUN; - current_block_100 = 721385680381463314; - } - _ => {} - } - match current_block_100 { - 721385680381463314 => { - let ret_1: lzma_ret = (*coder).block_decoder.code.unwrap()( - (*coder).block_decoder.coder, - allocator, - in_0, - in_pos, - in_size, - out, - out_pos, - out_size, - action, - ); - if ret_1 != LZMA_STREAM_END { - return ret_1; - } - let ret__0: lzma_ret = lzma_index_hash_append( - (*coder).index_hash, - lzma_block_unpadded_size(::core::ptr::addr_of_mut!((*coder).block_options)), - (*coder).block_options.uncompressed_size, - ); - if ret__0 != LZMA_OK { - return ret__0; + + let ret: lzma_ret = stream_decoder_reset(coder, allocator); + if ret != LZMA_OK { + return ret; } - (*coder).sequence = SEQ_BLOCK_HEADER; } - _ => {} + _ => return LZMA_PROG_ERROR, } } } -unsafe extern "C" fn stream_decoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { +unsafe fn stream_decoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_stream_coder = coder_ptr as *mut lzma_stream_coder; lzma_next_end(::core::ptr::addr_of_mut!((*coder).block_decoder), allocator); lzma_index_hash_end((*coder).index_hash, allocator); - crate::alloc::internal_free(coder as *mut c_void, allocator); + crate::common::common::lzma_free(coder as *mut c_void, allocator); } -extern "C" fn stream_decoder_get_check(coder_ptr: *const c_void) -> lzma_check { - return unsafe { - let coder: *const lzma_stream_coder = coder_ptr as *const lzma_stream_coder; - (*coder).stream_flags.check - }; +unsafe fn stream_decoder_get_check(coder_ptr: *const c_void) -> lzma_check { + let coder: *const lzma_stream_coder = coder_ptr as *const lzma_stream_coder; + (*coder).stream_flags.check } -unsafe extern "C" fn stream_decoder_memconfig( +unsafe fn stream_decoder_memconfig( coder_ptr: *mut c_void, memusage: *mut u64, old_memlimit: *mut u64, @@ -332,56 +309,42 @@ unsafe extern "C" fn stream_decoder_memconfig( } LZMA_OK } -pub(crate) unsafe extern "C" fn lzma_stream_decoder_init( +pub(crate) unsafe fn lzma_stream_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, memlimit: u64, flags: u32, ) -> lzma_ret { if core::mem::transmute::< - Option< - unsafe extern "C" fn(*mut lzma_next_coder, *const lzma_allocator, u64, u32) -> lzma_ret, - >, + Option lzma_ret>, uintptr_t, >(Some( lzma_stream_decoder_init - as unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - u64, - u32, - ) -> lzma_ret, + as unsafe fn(*mut lzma_next_coder, *const lzma_allocator, u64, u32) -> lzma_ret, )) != (*next).init { lzma_next_end(next, allocator); } (*next).init = core::mem::transmute::< - Option< - unsafe extern "C" fn(*mut lzma_next_coder, *const lzma_allocator, u64, u32) -> lzma_ret, - >, + Option lzma_ret>, uintptr_t, >(Some( lzma_stream_decoder_init - as unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - u64, - u32, - ) -> lzma_ret, + as unsafe fn(*mut lzma_next_coder, *const lzma_allocator, u64, u32) -> lzma_ret, )); if flags & !(LZMA_SUPPORTED_FLAGS as u32) != 0 { return LZMA_OPTIONS_ERROR; } let mut coder: *mut lzma_stream_coder = (*next).coder as *mut lzma_stream_coder; if coder.is_null() { - coder = crate::alloc::internal_alloc_object::(allocator); + coder = crate::common::common::lzma_alloc_object::(allocator); if coder.is_null() { return LZMA_MEM_ERROR; } (*next).coder = coder as *mut c_void; (*next).code = Some( stream_decode - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -393,14 +356,12 @@ pub(crate) unsafe extern "C" fn lzma_stream_decoder_init( lzma_action, ) -> lzma_ret, ); - (*next).end = Some( - stream_decoder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> (), - ); + (*next).end = + Some(stream_decoder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); (*next).get_check = - Some(stream_decoder_get_check as unsafe extern "C" fn(*const c_void) -> lzma_check); + Some(stream_decoder_get_check as unsafe fn(*const c_void) -> lzma_check); (*next).memconfig = Some( - stream_decoder_memconfig - as unsafe extern "C" fn(*mut c_void, *mut u64, *mut u64, u64) -> lzma_ret, + stream_decoder_memconfig as unsafe fn(*mut c_void, *mut u64, *mut u64, u64) -> lzma_ret, ); (*coder).block_decoder = lzma_next_coder_s { coder: core::ptr::null_mut(), diff --git a/xz/src/common/stream_decoder_mt.rs b/xz/src/common/stream_decoder_mt.rs index 75cbfa8c..f7881aa7 100644 --- a/xz/src/common/stream_decoder_mt.rs +++ b/xz/src/common/stream_decoder_mt.rs @@ -78,10 +78,20 @@ pub const SEQ_BLOCK_DIRECT_RUN: stream_decoder_mt_seq = 6; pub const SEQ_BLOCK_DIRECT_INIT: stream_decoder_mt_seq = 5; pub const SEQ_BLOCK_THR_RUN: stream_decoder_mt_seq = 4; pub const SEQ_BLOCK_THR_INIT: stream_decoder_mt_seq = 3; +type StreamMtBlockState = u8; +const STREAM_MT_BLOCK_HEADER: StreamMtBlockState = 0; +const STREAM_MT_BLOCK_INIT: StreamMtBlockState = 1; +const STREAM_MT_BLOCK_THR_INIT: StreamMtBlockState = 2; +const STREAM_MT_BLOCK_THR_RUN: StreamMtBlockState = 3; +const STREAM_MT_BLOCK_DIRECT_RUN: StreamMtBlockState = 4; +const STREAM_MT_INDEX_DECODE: StreamMtBlockState = 5; +const STREAM_MT_STREAM_FOOTER: StreamMtBlockState = 6; +const STREAM_MT_STREAM_PADDING: StreamMtBlockState = 7; +const STREAM_MT_RESTART_LOOP: StreamMtBlockState = 8; pub const SEQ_BLOCK_INIT: stream_decoder_mt_seq = 2; pub const SEQ_BLOCK_HEADER: stream_decoder_mt_seq = 1; pub const SEQ_STREAM_HEADER: stream_decoder_mt_seq = 0; -unsafe extern "C" fn worker_enable_partial_update(thr_ptr: *mut c_void) { +unsafe fn worker_enable_partial_update(thr_ptr: *mut c_void) { let thr: *mut worker_thread = thr_ptr as *mut worker_thread; let mut mythread_i_325: c_uint = 0; while if mythread_i_325 != 0 { @@ -146,17 +156,20 @@ unsafe extern "C" fn worker_decoder(thr_ptr: *mut c_void) -> *mut c_void { if in_filled - (*thr).in_pos > chunk_size { in_filled = (*thr).in_pos + chunk_size; } - ret = (*thr).block_decoder.code.unwrap()( - (*thr).block_decoder.coder, - (*thr).allocator, - (*thr).in_0, - ::core::ptr::addr_of_mut!((*thr).in_pos), - in_filled, - ::core::ptr::addr_of_mut!((*(*thr).outbuf).buf) as *mut u8, - ::core::ptr::addr_of_mut!((*thr).out_pos), - (*(*thr).outbuf).allocated, - LZMA_RUN, - ); + ret = match (*thr).block_decoder.code { + Some(code) => code( + (*thr).block_decoder.coder, + (*thr).allocator, + (*thr).in_0, + ::core::ptr::addr_of_mut!((*thr).in_pos), + in_filled, + ::core::ptr::addr_of_mut!((*(*thr).outbuf).buf) as *mut u8, + ::core::ptr::addr_of_mut!((*thr).out_pos), + (*(*thr).outbuf).allocated, + LZMA_RUN, + ), + None => LZMA_PROG_ERROR, + }; if ret == LZMA_OK { if partial_update_enabled { (*thr).partial_update_started = true; @@ -349,7 +362,7 @@ unsafe fn initialize_new_thread( (*thr).mem_filters = 0; if mythread_create( ::core::ptr::addr_of_mut!((*thr).thread_id), - Some(worker_decoder as unsafe extern "C" fn(*mut c_void) -> *mut c_void), + worker_decoder as unsafe extern "C" fn(*mut c_void) -> *mut c_void, thr as *mut c_void, ) != 0 { @@ -438,10 +451,7 @@ unsafe fn read_output_and_wait( if ret == LZMA_STREAM_END { lzma_outq_enable_partial_output( ::core::ptr::addr_of_mut!((*coder).outq), - Some( - worker_enable_partial_update - as unsafe extern "C" fn(*mut c_void) -> (), - ), + worker_enable_partial_update as unsafe fn(*mut c_void) -> (), ); } if ret != LZMA_STREAM_END { @@ -814,7 +824,7 @@ unsafe fn stream_decode_mt_thread_init( while mythread_j_1486 == 0 { lzma_outq_enable_partial_output( ::core::ptr::addr_of_mut!((*coder).outq), - Some(worker_enable_partial_update as unsafe extern "C" fn(*mut c_void) -> ()), + worker_enable_partial_update as unsafe fn(*mut c_void) -> (), ); mythread_j_1486 = 1; } @@ -824,7 +834,7 @@ unsafe fn stream_decode_mt_thread_init( None } -unsafe extern "C" fn stream_decode_mt( +unsafe fn stream_decode_mt( coder_ptr: *mut c_void, allocator: *const lzma_allocator, in_0: *const u8, @@ -851,7 +861,7 @@ unsafe extern "C" fn stream_decode_mt( action == LZMA_FINISH || *in_pos == in_size && !(*coder).out_was_filled; (*coder).out_was_filled = false; loop { - let mut current_block_239: u64; + let mut block_state: StreamMtBlockState; match (*coder).sequence { 0 => { let in_old: size_t = *in_pos; @@ -893,19 +903,19 @@ unsafe extern "C" fn stream_decode_mt( if (*coder).tell_any_check { return LZMA_GET_CHECK; } - current_block_239 = 7149356873433890176; + block_state = STREAM_MT_BLOCK_HEADER; } 1 => { - current_block_239 = 7149356873433890176; + block_state = STREAM_MT_BLOCK_HEADER; } 2 => { - current_block_239 = 3123434771885419771; + block_state = STREAM_MT_BLOCK_INIT; } 3 => { - current_block_239 = 11441799814184323368; + block_state = STREAM_MT_BLOCK_THR_INIT; } 4 => { - current_block_239 = 7728257318064351663; + block_state = STREAM_MT_BLOCK_THR_RUN; } 5 => { let ret__3: lzma_ret = read_output_and_wait( @@ -942,10 +952,10 @@ unsafe extern "C" fn stream_decode_mt( } (*coder).mem_direct_mode = (*coder).mem_next_filters; (*coder).sequence = SEQ_BLOCK_DIRECT_RUN; - current_block_239 = 7173345243791314703; + block_state = STREAM_MT_BLOCK_DIRECT_RUN; } 6 => { - current_block_239 = 7173345243791314703; + block_state = STREAM_MT_BLOCK_DIRECT_RUN; } 7 => { let ret__5: lzma_ret = read_output_and_wait( @@ -966,16 +976,16 @@ unsafe extern "C" fn stream_decode_mt( return LZMA_OK; } (*coder).sequence = SEQ_INDEX_DECODE; - current_block_239 = 13812071707085482240; + block_state = STREAM_MT_INDEX_DECODE; } 8 => { - current_block_239 = 13812071707085482240; + block_state = STREAM_MT_INDEX_DECODE; } 9 => { - current_block_239 = 15174413556390356007; + block_state = STREAM_MT_STREAM_FOOTER; } 10 => { - current_block_239 = 17073193239823527980; + block_state = STREAM_MT_STREAM_PADDING; } 11 => { if !(*coder).fail_fast { @@ -1001,11 +1011,14 @@ unsafe extern "C" fn stream_decode_mt( } _ => return LZMA_PROG_ERROR, } - match current_block_239 { - 7173345243791314703 => { + match block_state { + STREAM_MT_BLOCK_DIRECT_RUN => { let in_old_1: size_t = *in_pos; let out_old: size_t = *out_pos; - let ret_4: lzma_ret = (*coder).block_decoder.code.unwrap()( + let Some(code) = (*coder).block_decoder.code else { + return LZMA_PROG_ERROR; + }; + let ret_4: lzma_ret = code( (*coder).block_decoder.coder, allocator, in_0, @@ -1030,9 +1043,9 @@ unsafe extern "C" fn stream_decode_mt( return ret__4; } (*coder).sequence = SEQ_BLOCK_HEADER; - current_block_239 = 11639917216603986996; + block_state = STREAM_MT_RESTART_LOOP; } - 7149356873433890176 => { + STREAM_MT_BLOCK_HEADER => { let in_old_0: size_t = *in_pos; let ret_0: lzma_ret = decode_block_header(coder, allocator, in_0, in_pos, in_size); (*coder).progress_in += (*in_pos - in_old_0) as u64; @@ -1060,14 +1073,14 @@ unsafe extern "C" fn stream_decode_mt( } else { return LZMA_OK; } - current_block_239 = 11639917216603986996; + block_state = STREAM_MT_RESTART_LOOP; } else if ret_0 == LZMA_RET_INTERNAL2 { (*coder).sequence = SEQ_INDEX_WAIT_OUTPUT; - current_block_239 = 11639917216603986996; + block_state = STREAM_MT_RESTART_LOOP; } else if ret_0 != LZMA_STREAM_END { (*coder).pending_error = ret_0; (*coder).sequence = SEQ_ERROR; - current_block_239 = 11639917216603986996; + block_state = STREAM_MT_RESTART_LOOP; } else { (*coder).mem_next_filters = lzma_raw_decoder_memusage( ::core::ptr::addr_of_mut!((*coder).filters) as *mut lzma_filter, @@ -1075,14 +1088,14 @@ unsafe extern "C" fn stream_decode_mt( if (*coder).mem_next_filters == UINT64_MAX { (*coder).pending_error = LZMA_OPTIONS_ERROR; (*coder).sequence = SEQ_ERROR; - current_block_239 = 11639917216603986996; + block_state = STREAM_MT_RESTART_LOOP; } else { (*coder).sequence = SEQ_BLOCK_INIT; - current_block_239 = 3123434771885419771; + block_state = STREAM_MT_BLOCK_INIT; } } } - 13812071707085482240 => { + STREAM_MT_INDEX_DECODE => { if *in_pos >= in_size { return LZMA_OK; } @@ -1094,12 +1107,12 @@ unsafe extern "C" fn stream_decode_mt( return ret_5; } (*coder).sequence = SEQ_STREAM_FOOTER; - current_block_239 = 15174413556390356007; + block_state = STREAM_MT_STREAM_FOOTER; } _ => {} } - match current_block_239 { - 3123434771885419771 => { + match block_state { + STREAM_MT_BLOCK_INIT => { if let Some(ret) = stream_decode_mt_block_init( coder, allocator, @@ -1113,7 +1126,7 @@ unsafe extern "C" fn stream_decode_mt( } continue; } - 15174413556390356007 => { + STREAM_MT_STREAM_FOOTER => { let in_old_3: size_t = *in_pos; lzma_bufcpy( in_0, @@ -1172,12 +1185,12 @@ unsafe extern "C" fn stream_decode_mt( return LZMA_STREAM_END; } (*coder).sequence = SEQ_STREAM_PADDING; - current_block_239 = 17073193239823527980; + block_state = STREAM_MT_STREAM_PADDING; } _ => {} } - match current_block_239 { - 17073193239823527980 => { + match block_state { + STREAM_MT_STREAM_PADDING => { loop { if *in_pos >= in_size { if action != LZMA_FINISH { @@ -1205,9 +1218,9 @@ unsafe extern "C" fn stream_decode_mt( if ret__7 != LZMA_OK { return ret__7; } - current_block_239 = 11639917216603986996; + block_state = STREAM_MT_RESTART_LOOP; } - 11441799814184323368 => { + STREAM_MT_BLOCK_THR_INIT => { if let Some(ret) = stream_decode_mt_thread_init( coder, allocator, @@ -1223,8 +1236,8 @@ unsafe extern "C" fn stream_decode_mt( } _ => {} } - match current_block_239 { - 7728257318064351663 => { + match block_state { + STREAM_MT_BLOCK_THR_RUN => { if action == LZMA_FINISH && (*coder).fail_fast { let in_avail: size_t = in_size - *in_pos; let in_needed: size_t = (*(*coder).thr).in_size - (*(*coder).thr).in_filled; @@ -1287,10 +1300,7 @@ unsafe extern "C" fn stream_decode_mt( } } } -unsafe extern "C" fn stream_decoder_mt_end( - coder_ptr: *mut c_void, - allocator: *const lzma_allocator, -) { +unsafe fn stream_decoder_mt_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_stream_coder = coder_ptr as *mut lzma_stream_coder; threads_end(coder, allocator); lzma_outq_end(::core::ptr::addr_of_mut!((*coder).outq), allocator); @@ -1302,13 +1312,13 @@ unsafe extern "C" fn stream_decoder_mt_end( lzma_index_hash_end((*coder).index_hash, allocator); crate::alloc::internal_free(coder as *mut c_void, allocator); } -extern "C" fn stream_decoder_mt_get_check(coder_ptr: *const c_void) -> lzma_check { +unsafe fn stream_decoder_mt_get_check(coder_ptr: *const c_void) -> lzma_check { return unsafe { let coder: *const lzma_stream_coder = coder_ptr as *const lzma_stream_coder; (*coder).stream_flags.check }; } -unsafe extern "C" fn stream_decoder_mt_memconfig( +unsafe fn stream_decoder_mt_memconfig( coder_ptr: *mut c_void, memusage: *mut u64, old_memlimit: *mut u64, @@ -1346,7 +1356,7 @@ unsafe extern "C" fn stream_decoder_mt_memconfig( } LZMA_OK } -unsafe extern "C" fn stream_decoder_mt_get_progress( +unsafe fn stream_decoder_mt_get_progress( coder_ptr: *mut c_void, progress_in: *mut u64, progress_out: *mut u64, @@ -1395,7 +1405,7 @@ unsafe extern "C" fn stream_decoder_mt_get_progress( mythread_i_1862 = 1; } } -unsafe extern "C" fn stream_decoder_mt_init( +unsafe fn stream_decoder_mt_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, options: *const lzma_mt, @@ -1408,41 +1418,21 @@ unsafe extern "C" fn stream_decoder_mt_init( return LZMA_OPTIONS_ERROR; } if core::mem::transmute::< - Option< - unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *const lzma_mt, - ) -> lzma_ret, - >, + Option lzma_ret>, uintptr_t, >(Some( stream_decoder_mt_init - as unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *const lzma_mt, - ) -> lzma_ret, + as unsafe fn(*mut lzma_next_coder, *const lzma_allocator, *const lzma_mt) -> lzma_ret, )) != (*next).init { lzma_next_end(next, allocator); } (*next).init = core::mem::transmute::< - Option< - unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *const lzma_mt, - ) -> lzma_ret, - >, + Option lzma_ret>, uintptr_t, >(Some( stream_decoder_mt_init - as unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *const lzma_mt, - ) -> lzma_ret, + as unsafe fn(*mut lzma_next_coder, *const lzma_allocator, *const lzma_mt) -> lzma_ret, )); coder = (*next).coder as *mut lzma_stream_coder; if coder.is_null() { @@ -1462,7 +1452,7 @@ unsafe extern "C" fn stream_decoder_mt_init( } (*next).code = Some( stream_decode_mt - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -1474,18 +1464,16 @@ unsafe extern "C" fn stream_decoder_mt_init( lzma_action, ) -> lzma_ret, ); - (*next).end = Some( - stream_decoder_mt_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> (), - ); + (*next).end = + Some(stream_decoder_mt_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); (*next).get_check = - Some(stream_decoder_mt_get_check as unsafe extern "C" fn(*const c_void) -> lzma_check); + Some(stream_decoder_mt_get_check as unsafe fn(*const c_void) -> lzma_check); (*next).memconfig = Some( stream_decoder_mt_memconfig - as unsafe extern "C" fn(*mut c_void, *mut u64, *mut u64, u64) -> lzma_ret, + as unsafe fn(*mut c_void, *mut u64, *mut u64, u64) -> lzma_ret, ); (*next).get_progress = Some( - stream_decoder_mt_get_progress - as unsafe extern "C" fn(*mut c_void, *mut u64, *mut u64) -> (), + stream_decoder_mt_get_progress as unsafe fn(*mut c_void, *mut u64, *mut u64) -> (), ); (*coder).filters[0].id = LZMA_VLI_UNKNOWN; core::ptr::write_bytes( diff --git a/xz/src/common/stream_encoder.rs b/xz/src/common/stream_encoder.rs index b5c443ab..49485364 100644 --- a/xz/src/common/stream_encoder.rs +++ b/xz/src/common/stream_encoder.rs @@ -14,6 +14,11 @@ pub struct lzma_stream_coder { pub buffer_size: size_t, pub buffer: [u8; LZMA_BLOCK_HEADER_SIZE_MAX as usize], } +#[inline(always)] +unsafe fn supported_action_slot(actions: *mut bool, index: u32) -> *mut bool { + debug_assert!((index as usize) < 5); + actions.add(index as usize) +} pub type stream_encoder_seq = c_uint; pub const SEQ_STREAM_FOOTER: stream_encoder_seq = 5; pub const SEQ_INDEX_ENCODE: stream_encoder_seq = 4; @@ -37,10 +42,10 @@ unsafe fn block_encoder_init( ::core::ptr::addr_of_mut!((*coder).block_options), ) } -unsafe extern "C" fn stream_encode( +unsafe fn stream_encode( coder_ptr: *mut c_void, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, @@ -48,6 +53,18 @@ unsafe extern "C" fn stream_encode( out_size: size_t, action: lzma_action, ) -> lzma_ret { + #[inline(always)] + unsafe fn convert_action(action: lzma_action) -> lzma_action { + static CONVERT: [lzma_action; 5] = [ + LZMA_RUN, + LZMA_SYNC_FLUSH, + LZMA_FINISH, + LZMA_FINISH, + LZMA_FINISH, + ]; + debug_assert!((action as usize) < CONVERT.len()); + *CONVERT.as_ptr().add(action as usize) + } let coder: *mut lzma_stream_coder = coder_ptr as *mut lzma_stream_coder; while *out_pos < out_size { match (*coder).sequence { @@ -107,23 +124,18 @@ unsafe extern "C" fn stream_encode( } } 3 => { - static CONVERT: [lzma_action; 5] = [ - LZMA_RUN, - LZMA_SYNC_FLUSH, - LZMA_FINISH, - LZMA_FINISH, - LZMA_FINISH, - ]; - let ret: lzma_ret = (*coder).block_encoder.code.unwrap()( + debug_assert!((*coder).block_encoder.code.is_some()); + let code = (*coder).block_encoder.code.unwrap_unchecked(); + let ret: lzma_ret = code( (*coder).block_encoder.coder, allocator, - in_0, + input, in_pos, in_size, out, out_pos, out_size, - CONVERT[action as usize], + convert_action(action), ); if ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH { return ret; @@ -143,7 +155,9 @@ unsafe extern "C" fn stream_encode( (*coder).sequence = SEQ_BLOCK_INIT; } 4 => { - let ret_0: lzma_ret = (*coder).index_encoder.code.unwrap()( + debug_assert!((*coder).index_encoder.code.is_some()); + let code = (*coder).index_encoder.code.unwrap_unchecked(); + let ret_0: lzma_ret = code( (*coder).index_encoder.coder, allocator, core::ptr::null(), @@ -177,7 +191,7 @@ unsafe extern "C" fn stream_encode( } LZMA_OK } -unsafe extern "C" fn stream_encoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { +unsafe fn stream_encoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_stream_coder = coder_ptr as *mut lzma_stream_coder; lzma_next_end(::core::ptr::addr_of_mut!((*coder).block_encoder), allocator); lzma_next_end(::core::ptr::addr_of_mut!((*coder).index_encoder), allocator); @@ -186,9 +200,9 @@ unsafe extern "C" fn stream_encoder_end(coder_ptr: *mut c_void, allocator: *cons ::core::ptr::addr_of_mut!((*coder).filters) as *mut lzma_filter, allocator, ); - crate::alloc::internal_free(coder as *mut c_void, allocator); + crate::common::common::lzma_free(coder as *mut c_void, allocator); } -#[inline(never)] +#[inline(always)] unsafe fn stream_encoder_update_before_block( coder: *mut lzma_stream_coder, allocator: *const lzma_allocator, @@ -206,14 +220,16 @@ unsafe fn stream_encoder_update_before_block( LZMA_OK } -#[inline(never)] +#[inline(always)] unsafe fn stream_encoder_update_mid_block( coder: *mut lzma_stream_coder, allocator: *const lzma_allocator, filters: *const lzma_filter, reversed_filters: *const lzma_filter, ) -> lzma_ret { - (*coder).block_encoder.update.unwrap()( + debug_assert!((*coder).block_encoder.update.is_some()); + let update = (*coder).block_encoder.update.unwrap_unchecked(); + update( (*coder).block_encoder.coder, allocator, filters, @@ -221,7 +237,7 @@ unsafe fn stream_encoder_update_mid_block( ) } -unsafe extern "C" fn stream_encoder_update( +unsafe fn stream_encoder_update( coder_ptr: *mut c_void, allocator: *const lzma_allocator, filters: *const lzma_filter, @@ -254,7 +270,7 @@ unsafe extern "C" fn stream_encoder_update( (*coder).filters = temp; LZMA_OK } -unsafe extern "C" fn stream_encoder_init( +unsafe fn stream_encoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter, @@ -262,7 +278,7 @@ unsafe extern "C" fn stream_encoder_init( ) -> lzma_ret { if core::mem::transmute::< Option< - unsafe extern "C" fn( + unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter, @@ -272,7 +288,7 @@ unsafe extern "C" fn stream_encoder_init( uintptr_t, >(Some( stream_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter, @@ -284,7 +300,7 @@ unsafe extern "C" fn stream_encoder_init( } (*next).init = core::mem::transmute::< Option< - unsafe extern "C" fn( + unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter, @@ -294,7 +310,7 @@ unsafe extern "C" fn stream_encoder_init( uintptr_t, >(Some( stream_encoder_init - as unsafe extern "C" fn( + as unsafe fn( *mut lzma_next_coder, *const lzma_allocator, *const lzma_filter, @@ -306,14 +322,14 @@ unsafe extern "C" fn stream_encoder_init( } let mut coder: *mut lzma_stream_coder = (*next).coder as *mut lzma_stream_coder; if coder.is_null() { - coder = crate::alloc::internal_alloc_object::(allocator); + coder = crate::common::common::lzma_alloc_object::(allocator); if coder.is_null() { return LZMA_MEM_ERROR; } (*next).coder = coder as *mut c_void; (*next).code = Some( stream_encode - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -325,12 +341,11 @@ unsafe extern "C" fn stream_encoder_init( lzma_action, ) -> lzma_ret, ); - (*next).end = Some( - stream_encoder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> (), - ); + (*next).end = + Some(stream_encoder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); (*next).update = Some( stream_encoder_update - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const lzma_filter, @@ -406,10 +421,25 @@ pub unsafe fn lzma_stream_encoder( lzma_end(strm); return ret__0; } - (*(*strm).internal).supported_actions[LZMA_RUN as usize] = true; - (*(*strm).internal).supported_actions[LZMA_SYNC_FLUSH as usize] = true; - (*(*strm).internal).supported_actions[LZMA_FULL_FLUSH as usize] = true; - (*(*strm).internal).supported_actions[LZMA_FULL_BARRIER as usize] = true; - (*(*strm).internal).supported_actions[LZMA_FINISH as usize] = true; + *supported_action_slot( + ::core::ptr::addr_of_mut!((*(*strm).internal).supported_actions) as *mut bool, + LZMA_RUN, + ) = true; + *supported_action_slot( + ::core::ptr::addr_of_mut!((*(*strm).internal).supported_actions) as *mut bool, + LZMA_SYNC_FLUSH, + ) = true; + *supported_action_slot( + ::core::ptr::addr_of_mut!((*(*strm).internal).supported_actions) as *mut bool, + LZMA_FULL_FLUSH, + ) = true; + *supported_action_slot( + ::core::ptr::addr_of_mut!((*(*strm).internal).supported_actions) as *mut bool, + LZMA_FULL_BARRIER, + ) = true; + *supported_action_slot( + ::core::ptr::addr_of_mut!((*(*strm).internal).supported_actions) as *mut bool, + LZMA_FINISH, + ) = true; LZMA_OK } diff --git a/xz/src/common/stream_encoder_mt.rs b/xz/src/common/stream_encoder_mt.rs index a1d63acf..6aaac693 100644 --- a/xz/src/common/stream_encoder_mt.rs +++ b/xz/src/common/stream_encoder_mt.rs @@ -168,13 +168,20 @@ unsafe fn worker_encode( } else { LZMA_RUN }) as lzma_action; - static mut in_chunk_max: size_t = 16384; + const IN_CHUNK_MAX: size_t = 16384; let mut in_limit: size_t = in_size; - if in_size - in_pos > in_chunk_max { - in_limit = in_pos + in_chunk_max; + if in_size - in_pos > IN_CHUNK_MAX { + in_limit = in_pos + IN_CHUNK_MAX; action = LZMA_RUN; } - ret = (*thr).block_encoder.code.unwrap()( + let code = match (*thr).block_encoder.code { + Some(code) => code, + None => { + worker_error(thr, LZMA_PROG_ERROR); + return THR_STOP; + } + }; + ret = code( (*thr).block_encoder.coder, (*thr).allocator, (*thr).in_0, @@ -480,7 +487,7 @@ unsafe fn initialize_new_thread( (*thr).filters[0].id = LZMA_VLI_UNKNOWN; if mythread_create( ::core::ptr::addr_of_mut!((*thr).thread_id), - Some(worker_start as unsafe extern "C" fn(*mut c_void) -> *mut c_void), + worker_start as unsafe extern "C" fn(*mut c_void) -> *mut c_void, thr as *mut c_void, ) != 0 { @@ -834,7 +841,10 @@ unsafe fn stream_encode_mt_index( out_pos: *mut size_t, out_size: size_t, ) -> lzma_ret { - let ret: lzma_ret = (*coder).index_encoder.code.unwrap()( + let Some(code) = (*coder).index_encoder.code else { + return LZMA_PROG_ERROR; + }; + let ret: lzma_ret = code( (*coder).index_encoder.coder, allocator, core::ptr::null(), @@ -860,7 +870,7 @@ unsafe fn stream_encode_mt_index( LZMA_STREAM_END } -unsafe extern "C" fn stream_encode_mt( +unsafe fn stream_encode_mt( coder_ptr: *mut c_void, allocator: *const lzma_allocator, in_0: *const u8, @@ -919,10 +929,7 @@ unsafe extern "C" fn stream_encode_mt( LZMA_STREAM_END } } -unsafe extern "C" fn stream_encoder_mt_end( - coder_ptr: *mut c_void, - allocator: *const lzma_allocator, -) { +unsafe fn stream_encoder_mt_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_stream_coder = coder_ptr as *mut lzma_stream_coder; threads_end(coder, allocator); lzma_outq_end(::core::ptr::addr_of_mut!((*coder).outq), allocator); @@ -940,7 +947,7 @@ unsafe extern "C" fn stream_encoder_mt_end( mythread_mutex_destroy(::core::ptr::addr_of_mut!((*coder).mutex)); crate::alloc::internal_free(coder as *mut c_void, allocator); } -unsafe extern "C" fn stream_encoder_mt_update( +unsafe fn stream_encoder_mt_update( coder_ptr: *mut c_void, allocator: *const lzma_allocator, filters: *const lzma_filter, @@ -1018,11 +1025,7 @@ unsafe fn get_options( } LZMA_OK } -unsafe extern "C" fn get_progress( - coder_ptr: *mut c_void, - progress_in: *mut u64, - progress_out: *mut u64, -) { +unsafe fn get_progress(coder_ptr: *mut c_void, progress_in: *mut u64, progress_out: *mut u64) { let coder: *mut lzma_stream_coder = coder_ptr as *mut lzma_stream_coder; let mut mythread_i_1010: c_uint = 0; while if mythread_i_1010 != 0 { @@ -1090,7 +1093,7 @@ unsafe fn stream_encoder_mt_create_coder( } (*next).code = Some( stream_encode_mt - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -1102,13 +1105,11 @@ unsafe fn stream_encoder_mt_create_coder( lzma_action, ) -> lzma_ret, ); - (*next).end = - Some(stream_encoder_mt_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator)); - (*next).get_progress = - Some(get_progress as unsafe extern "C" fn(*mut c_void, *mut u64, *mut u64) -> ()); + (*next).end = Some(stream_encoder_mt_end as unsafe fn(*mut c_void, *const lzma_allocator)); + (*next).get_progress = Some(get_progress as unsafe fn(*mut c_void, *mut u64, *mut u64) -> ()); (*next).update = Some( stream_encoder_mt_update - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const lzma_filter, @@ -1165,47 +1166,27 @@ unsafe fn stream_encoder_mt_prepare_threads( LZMA_OK } -unsafe extern "C" fn stream_encoder_mt_init( +unsafe fn stream_encoder_mt_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, options: *const lzma_mt, ) -> lzma_ret { if core::mem::transmute::< - Option< - unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *const lzma_mt, - ) -> lzma_ret, - >, + Option lzma_ret>, uintptr_t, >(Some( stream_encoder_mt_init - as unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *const lzma_mt, - ) -> lzma_ret, + as unsafe fn(*mut lzma_next_coder, *const lzma_allocator, *const lzma_mt) -> lzma_ret, )) != (*next).init { lzma_next_end(next, allocator); } (*next).init = core::mem::transmute::< - Option< - unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *const lzma_mt, - ) -> lzma_ret, - >, + Option lzma_ret>, uintptr_t, >(Some( stream_encoder_mt_init - as unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *const lzma_mt, - ) -> lzma_ret, + as unsafe fn(*mut lzma_next_coder, *const lzma_allocator, *const lzma_mt) -> lzma_ret, )); let mut easy: lzma_options_easy = lzma_options_easy { filters: [lzma_filter { diff --git a/xz/src/common/stream_flags_common.rs b/xz/src/common/stream_flags_common.rs index be89bd00..6e76d8f9 100644 --- a/xz/src/common/stream_flags_common.rs +++ b/xz/src/common/stream_flags_common.rs @@ -1,8 +1,8 @@ use crate::types::*; -pub static mut lzma_header_magic: [u8; 6] = [ +pub static lzma_header_magic: [u8; 6] = [ 0xfd as u8, 0x37 as u8, 0x7a as u8, 0x58 as u8, 0x5a as u8, 0, ]; -pub static mut lzma_footer_magic: [u8; 2] = [0x59 as u8, 0x5a as u8]; +pub static lzma_footer_magic: [u8; 2] = [0x59 as u8, 0x5a as u8]; pub unsafe fn lzma_stream_flags_compare( a: *const lzma_stream_flags, b: *const lzma_stream_flags, diff --git a/xz/src/common/stream_flags_decoder.rs b/xz/src/common/stream_flags_decoder.rs index 6a334de0..7a57b7e0 100644 --- a/xz/src/common/stream_flags_decoder.rs +++ b/xz/src/common/stream_flags_decoder.rs @@ -1,44 +1,39 @@ use crate::common::stream_flags_common::{lzma_footer_magic, lzma_header_magic}; use crate::types::*; -fn stream_flags_decode(options: *mut lzma_stream_flags, in_0: *const u8) -> bool { - return unsafe { - if *in_0 != 0 || *in_0.offset(1) & 0xf0 != 0 { +fn stream_flags_decode(options: *mut lzma_stream_flags, input: &[u8; 2]) -> bool { + unsafe { + if input[0] != 0 || input[1] & 0xf0 != 0 { return true; } (*options).version = 0; - (*options).check = (*in_0.offset(1) & 0xf) as lzma_check; + (*options).check = (input[1] & 0xf) as lzma_check; false - }; + } } pub unsafe fn lzma_stream_header_decode( options: *mut lzma_stream_flags, - in_0: *const u8, + input: *const u8, ) -> lzma_ret { if memcmp( - in_0 as *const c_void, + input as *const c_void, ::core::ptr::addr_of!(lzma_header_magic) as *const c_void, core::mem::size_of::<[u8; 6]>(), ) != 0 { return LZMA_FORMAT_ERROR; } - let crc: u32 = lzma_crc32( - in_0.offset(core::mem::size_of::<[u8; 6]>() as isize), - LZMA_STREAM_FLAGS_SIZE as size_t, - 0, - ) as u32; + let flags_offset = core::mem::size_of::<[u8; 6]>(); + let crc: u32 = lzma_crc32(input.add(flags_offset), LZMA_STREAM_FLAGS_SIZE as size_t, 0) as u32; if crc != read32le( - in_0.offset(core::mem::size_of::<[u8; 6]>() as isize) - .offset(LZMA_STREAM_FLAGS_SIZE as isize), + &*input + .add(flags_offset + LZMA_STREAM_FLAGS_SIZE as usize) + .cast::<[u8; 4]>(), ) { return LZMA_DATA_ERROR; } - if stream_flags_decode( - options, - in_0.offset(core::mem::size_of::<[u8; 6]>() as isize), - ) { + if stream_flags_decode(options, &*input.add(flags_offset).cast::<[u8; 2]>()) { return LZMA_OPTIONS_ERROR; } (*options).backward_size = LZMA_VLI_UNKNOWN; @@ -46,10 +41,11 @@ pub unsafe fn lzma_stream_header_decode( } pub unsafe fn lzma_stream_footer_decode( options: *mut lzma_stream_flags, - in_0: *const u8, + input: *const u8, ) -> lzma_ret { if memcmp( - in_0.offset((core::mem::size_of::() * 2) as isize) + input + .offset((core::mem::size_of::() * 2) as isize) .offset(LZMA_STREAM_FLAGS_SIZE as isize) as *const c_void, ::core::ptr::addr_of!(lzma_footer_magic) as *const c_void, core::mem::size_of::<[u8; 2]>(), @@ -57,22 +53,20 @@ pub unsafe fn lzma_stream_footer_decode( { return LZMA_FORMAT_ERROR; } + let flags_offset = 2 * core::mem::size_of::(); let crc: u32 = lzma_crc32( - in_0.offset(core::mem::size_of::() as isize), + input.add(core::mem::size_of::()), core::mem::size_of::() + LZMA_STREAM_FLAGS_SIZE as size_t, 0, ) as u32; - if crc != read32le(in_0) { + if crc != read32le(&*input.cast::<[u8; 4]>()) { return LZMA_DATA_ERROR; } - if stream_flags_decode( - options, - in_0.offset((core::mem::size_of::() * 2) as isize), - ) { + if stream_flags_decode(options, &*input.add(flags_offset).cast::<[u8; 2]>()) { return LZMA_OPTIONS_ERROR; } (*options).backward_size = - read32le(in_0.offset(core::mem::size_of::() as isize)) as lzma_vli; + read32le(&*input.add(core::mem::size_of::()).cast::<[u8; 4]>()) as lzma_vli; (*options).backward_size = ((*options).backward_size + 1) * 4; LZMA_OK } diff --git a/xz/src/common/stream_flags_encoder.rs b/xz/src/common/stream_flags_encoder.rs index f5655d8f..9c69d6ec 100644 --- a/xz/src/common/stream_flags_encoder.rs +++ b/xz/src/common/stream_flags_encoder.rs @@ -1,14 +1,14 @@ use crate::common::stream_flags_common::{lzma_footer_magic, lzma_header_magic}; use crate::types::*; -fn stream_flags_encode(options: *const lzma_stream_flags, out: *mut u8) -> bool { - return unsafe { +fn stream_flags_encode(options: *const lzma_stream_flags, out: &mut [u8; 2]) -> bool { + unsafe { if (*options).check > LZMA_CHECK_ID_MAX { return true; } - *out = 0; - *out.offset(1) = (*options).check as u8; + out[0] = 0; + out[1] = (*options).check as u8; false - }; + } } pub unsafe fn lzma_stream_header_encode( options: *const lzma_stream_flags, @@ -22,20 +22,15 @@ pub unsafe fn lzma_stream_header_encode( out as *mut u8, core::mem::size_of::<[u8; 6]>(), ); - if stream_flags_encode( - options, - out.offset(core::mem::size_of::<[u8; 6]>() as isize), - ) { + let flags_offset = core::mem::size_of::<[u8; 6]>(); + if stream_flags_encode(options, &mut *out.add(flags_offset).cast::<[u8; 2]>()) { return LZMA_PROG_ERROR; } - let crc: u32 = lzma_crc32( - out.offset(core::mem::size_of::<[u8; 6]>() as isize), - LZMA_STREAM_FLAGS_SIZE as size_t, - 0, - ) as u32; + let crc: u32 = lzma_crc32(out.add(flags_offset), LZMA_STREAM_FLAGS_SIZE as size_t, 0) as u32; write32le( - out.offset(core::mem::size_of::<[u8; 6]>() as isize) - .offset(LZMA_STREAM_FLAGS_SIZE as isize), + &mut *out + .add(flags_offset + LZMA_STREAM_FLAGS_SIZE as usize) + .cast::<[u8; 4]>(), crc, ); LZMA_OK @@ -51,18 +46,18 @@ pub unsafe fn lzma_stream_footer_encode( return LZMA_PROG_ERROR; } write32le( - out.offset(4), + &mut *out.add(4).cast::<[u8; 4]>(), (*options).backward_size.wrapping_div(4).wrapping_sub(1) as u32, ); - if stream_flags_encode(options, out.offset((2 * 4) as isize)) { + let flags_offset = 2 * core::mem::size_of::(); + if stream_flags_encode(options, &mut *out.add(flags_offset).cast::<[u8; 2]>()) { return LZMA_PROG_ERROR; } - let crc: u32 = lzma_crc32(out.offset(4), (4 + LZMA_STREAM_FLAGS_SIZE) as size_t, 0) as u32; - write32le(out, crc); + let crc: u32 = lzma_crc32(out.add(4), (4 + LZMA_STREAM_FLAGS_SIZE) as size_t, 0) as u32; + write32le(&mut *out.cast::<[u8; 4]>(), crc); core::ptr::copy_nonoverlapping( ::core::ptr::addr_of!(lzma_footer_magic) as *const u8, - out.offset((2 * 4) as isize) - .offset(LZMA_STREAM_FLAGS_SIZE as isize) as *mut u8, + out.add(flags_offset + LZMA_STREAM_FLAGS_SIZE as usize), core::mem::size_of::<[u8; 2]>(), ); LZMA_OK diff --git a/xz/src/common/string_conversion.rs b/xz/src/common/string_conversion.rs index c4897584..df738a79 100644 --- a/xz/src/common/string_conversion.rs +++ b/xz/src/common/string_conversion.rs @@ -6,9 +6,7 @@ pub struct filter_codec_def { pub name: [c_char; 12], pub opts_size: u32, pub id: lzma_vli, - pub parse: Option< - unsafe extern "C" fn(*mut *const c_char, *const c_char, *mut c_void) -> *const c_char, - >, + pub parse: Option *const c_char>, pub optmap: *const option_map, pub strfy_encoder: u8, pub strfy_decoder: u8, @@ -18,7 +16,7 @@ pub struct filter_codec_def { #[repr(C)] pub struct option_map { pub name: [c_char; 12], - pub type_0: u8, + pub option_type: u8, pub flags: u8, pub offset: u16, pub u: option_value, @@ -41,6 +39,20 @@ pub struct name_value_map { pub name: [c_char; 12], pub value: u32, } +#[inline] +const fn array_size(_: *const [T; N]) -> usize { + N +} +#[inline] +const fn c_chars(bytes: [u8; N]) -> [c_char; N] { + let mut out = [0; N]; + let mut i = 0; + while i < N { + out[i] = bytes[i] as c_char; + i += 1; + } + out +} pub const OPTMAP_TYPE_LZMA_MATCH_FINDER: option_type = 2; pub const OPTMAP_TYPE_LZMA_MODE: option_type = 1; pub const OPTMAP_TYPE_LZMA_PRESET: option_type = 3; @@ -107,27 +119,20 @@ unsafe fn str_append_u32(str: *mut lzma_str, mut v: u32, use_byte_suffix: bool) if v == 0 { str_append_str(str, crate::c_str!("0")); } else { - static SUFFIXES: [[c_char; 4]; 4] = unsafe { - [ - core::mem::transmute::<[u8; 4], [c_char; 4]>(*b"\0\0\0\0"), - core::mem::transmute::<[u8; 4], [c_char; 4]>(*b"KiB\0"), - core::mem::transmute::<[u8; 4], [c_char; 4]>(*b"MiB\0"), - core::mem::transmute::<[u8; 4], [c_char; 4]>(*b"GiB\0"), - ] - }; + static SUFFIXES: [[c_char; 4]; 4] = [ + c_chars(*b"\0\0\0\0"), + c_chars(*b"KiB\0"), + c_chars(*b"MiB\0"), + c_chars(*b"GiB\0"), + ]; let mut suf: size_t = 0; if use_byte_suffix { - while v & 1023 == 0 - && suf - < core::mem::size_of::<[[c_char; 4]; 4]>() / core::mem::size_of::<[c_char; 4]>() - - 1 - { + while v & 1023 == 0 && suf < array_size(::core::ptr::addr_of!(SUFFIXES)) - 1 { v >>= 10; suf += 1; } } - let mut buf: [c_char; 16] = - core::mem::transmute::<[u8; 16], [c_char; 16]>(*b"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); + let mut buf: [c_char; 16] = [0; 16]; let mut pos: size_t = core::mem::size_of::<[c_char; 16]>() - 1; loop { pos -= 1; @@ -148,70 +153,64 @@ pub const NAME_LEN_MAX: u32 = 11; pub const OPTMAP_USE_NAME_VALUE_MAP: u8 = 0x1; pub const OPTMAP_USE_BYTE_SUFFIX: u8 = 0x2; pub const OPTMAP_NO_STRFY_ZERO: u8 = 0x4; -static mut bcj_optmap: [option_map; 1] = unsafe { - [option_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"start\0\0\0\0\0\0\0"), - type_0: 0, - flags: (OPTMAP_NO_STRFY_ZERO | OPTMAP_USE_BYTE_SUFFIX) as u8, - offset: 0, - u: option_value { - range: option_value_range { - min: 0, - max: UINT32_MAX, - }, +static mut bcj_optmap: [option_map; 1] = [option_map { + name: c_chars(*b"start\0\0\0\0\0\0\0"), + option_type: 0, + flags: (OPTMAP_NO_STRFY_ZERO | OPTMAP_USE_BYTE_SUFFIX) as u8, + offset: 0, + u: option_value { + range: option_value_range { + min: 0, + max: UINT32_MAX, }, - }] -}; -extern "C" fn parse_bcj( + }, +}]; +fn parse_bcj( str: *mut *const c_char, str_end: *const c_char, filter_options: *mut c_void, ) -> *const c_char { - return unsafe { + unsafe { parse_options( str, str_end, filter_options, ::core::ptr::addr_of!(bcj_optmap) as *const option_map, - core::mem::size_of::<[option_map; 1]>() / core::mem::size_of::(), + array_size(::core::ptr::addr_of!(bcj_optmap)), ) - }; + } } -static mut delta_optmap: [option_map; 1] = unsafe { - [option_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"dist\0\0\0\0\0\0\0\0"), - type_0: 0, - flags: 0, - offset: 4, - u: option_value { - range: option_value_range { - min: LZMA_DELTA_DIST_MIN, - max: LZMA_DELTA_DIST_MAX, - }, +static mut delta_optmap: [option_map; 1] = [option_map { + name: c_chars(*b"dist\0\0\0\0\0\0\0\0"), + option_type: 0, + flags: 0, + offset: 4, + u: option_value { + range: option_value_range { + min: LZMA_DELTA_DIST_MIN, + max: LZMA_DELTA_DIST_MAX, }, - }] -}; -extern "C" fn parse_delta( + }, +}]; +fn parse_delta( str: *mut *const c_char, str_end: *const c_char, filter_options: *mut c_void, ) -> *const c_char { - return unsafe { + unsafe { let opts: *mut lzma_options_delta = filter_options as *mut lzma_options_delta; - (*opts).type_0 = LZMA_DELTA_TYPE_BYTE; + (*opts).type_ = LZMA_DELTA_TYPE_BYTE; (*opts).dist = LZMA_DELTA_DIST_MIN; parse_options( str, str_end, filter_options, ::core::ptr::addr_of!(delta_optmap) as *const option_map, - (core::mem::size_of::<[option_map; 1]>()) - .wrapping_div(core::mem::size_of::()), + array_size(::core::ptr::addr_of!(delta_optmap)), ) - }; + } } -pub const LZMA12_PRESET_STR: [c_char; 7] = - unsafe { core::mem::transmute::<[u8; 7], [c_char; 7]>(*b"0-9[e]\0") }; +pub const LZMA12_PRESET_STR: [c_char; 7] = c_chars(*b"0-9[e]\0"); unsafe fn parse_lzma12_preset( str: *mut *const c_char, str_end: *const c_char, @@ -254,60 +253,56 @@ unsafe fn set_lzma12_preset( } core::ptr::null() } -static lzma12_mode_map: [name_value_map; 3] = unsafe { - [ - name_value_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"fast\0\0\0\0\0\0\0\0"), - value: LZMA_MODE_FAST as u32, - }, - name_value_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"normal\0\0\0\0\0\0"), - value: LZMA_MODE_NORMAL as u32, - }, - name_value_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"\0\0\0\0\0\0\0\0\0\0\0\0"), - value: 0, - }, - ] -}; -static lzma12_mf_map: [name_value_map; 6] = unsafe { - [ - name_value_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"hc3\0\0\0\0\0\0\0\0\0"), - value: LZMA_MF_HC3 as u32, - }, - name_value_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"hc4\0\0\0\0\0\0\0\0\0"), - value: LZMA_MF_HC4 as u32, - }, - name_value_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"bt2\0\0\0\0\0\0\0\0\0"), - value: LZMA_MF_BT2 as u32, - }, - name_value_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"bt3\0\0\0\0\0\0\0\0\0"), - value: LZMA_MF_BT3 as u32, - }, - name_value_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"bt4\0\0\0\0\0\0\0\0\0"), - value: LZMA_MF_BT4 as u32, - }, - name_value_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"\0\0\0\0\0\0\0\0\0\0\0\0"), - value: 0, - }, - ] -}; +static lzma12_mode_map: [name_value_map; 3] = [ + name_value_map { + name: c_chars(*b"fast\0\0\0\0\0\0\0\0"), + value: LZMA_MODE_FAST as u32, + }, + name_value_map { + name: c_chars(*b"normal\0\0\0\0\0\0"), + value: LZMA_MODE_NORMAL as u32, + }, + name_value_map { + name: c_chars(*b"\0\0\0\0\0\0\0\0\0\0\0\0"), + value: 0, + }, +]; +static lzma12_mf_map: [name_value_map; 6] = [ + name_value_map { + name: c_chars(*b"hc3\0\0\0\0\0\0\0\0\0"), + value: LZMA_MF_HC3 as u32, + }, + name_value_map { + name: c_chars(*b"hc4\0\0\0\0\0\0\0\0\0"), + value: LZMA_MF_HC4 as u32, + }, + name_value_map { + name: c_chars(*b"bt2\0\0\0\0\0\0\0\0\0"), + value: LZMA_MF_BT2 as u32, + }, + name_value_map { + name: c_chars(*b"bt3\0\0\0\0\0\0\0\0\0"), + value: LZMA_MF_BT3 as u32, + }, + name_value_map { + name: c_chars(*b"bt4\0\0\0\0\0\0\0\0\0"), + value: LZMA_MF_BT4 as u32, + }, + name_value_map { + name: c_chars(*b"\0\0\0\0\0\0\0\0\0\0\0\0"), + value: 0, + }, +]; static mut lzma12_optmap: [option_map; 9] = [option_map { name: [0; 12], - type_0: 0, + option_type: 0, flags: 0, offset: 0, u: option_value { map: core::ptr::null(), }, }; 9]; -unsafe extern "C" fn parse_lzma12( +unsafe fn parse_lzma12( str: *mut *const c_char, str_end: *const c_char, filter_options: *mut c_void, @@ -319,7 +314,7 @@ unsafe extern "C" fn parse_lzma12( str_end, filter_options, ::core::ptr::addr_of!(lzma12_optmap) as *const option_map, - core::mem::size_of::<[option_map; 9]>() / core::mem::size_of::(), + array_size(::core::ptr::addr_of!(lzma12_optmap)), ); if !errmsg.is_null() { return errmsg; @@ -329,197 +324,143 @@ unsafe extern "C" fn parse_lzma12( } core::ptr::null() } -static mut filter_name_map: [filter_codec_def; 11] = unsafe { - [ - filter_codec_def { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"lzma1\0\0\0\0\0\0\0"), - opts_size: core::mem::size_of::() as u32, - id: LZMA_FILTER_LZMA1, - parse: Some( - parse_lzma12 - as unsafe extern "C" fn( - *mut *const c_char, - *const c_char, - *mut c_void, - ) -> *const c_char, - ), - optmap: ::core::ptr::addr_of!(lzma12_optmap) as *const option_map, - strfy_encoder: 9, - strfy_decoder: 5, - allow_null: false, - }, - filter_codec_def { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"lzma2\0\0\0\0\0\0\0"), - opts_size: core::mem::size_of::() as u32, - id: LZMA_FILTER_LZMA2, - parse: Some( - parse_lzma12 - as unsafe extern "C" fn( - *mut *const c_char, - *const c_char, - *mut c_void, - ) -> *const c_char, - ), - optmap: ::core::ptr::addr_of!(lzma12_optmap) as *const option_map, - strfy_encoder: 9, - strfy_decoder: 2, - allow_null: false, - }, - filter_codec_def { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"x86\0\0\0\0\0\0\0\0\0"), - opts_size: core::mem::size_of::() as u32, - id: LZMA_FILTER_X86, - parse: Some( - parse_bcj - as unsafe extern "C" fn( - *mut *const c_char, - *const c_char, - *mut c_void, - ) -> *const c_char, - ), - optmap: ::core::ptr::addr_of!(bcj_optmap) as *const option_map, - strfy_encoder: 1, - strfy_decoder: 1, - allow_null: true, - }, - filter_codec_def { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"arm\0\0\0\0\0\0\0\0\0"), - opts_size: core::mem::size_of::() as u32, - id: LZMA_FILTER_ARM, - parse: Some( - parse_bcj - as unsafe extern "C" fn( - *mut *const c_char, - *const c_char, - *mut c_void, - ) -> *const c_char, - ), - optmap: ::core::ptr::addr_of!(bcj_optmap) as *const option_map, - strfy_encoder: 1, - strfy_decoder: 1, - allow_null: true, - }, - filter_codec_def { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"armthumb\0\0\0\0"), - opts_size: core::mem::size_of::() as u32, - id: LZMA_FILTER_ARMTHUMB, - parse: Some( - parse_bcj - as unsafe extern "C" fn( - *mut *const c_char, - *const c_char, - *mut c_void, - ) -> *const c_char, - ), - optmap: ::core::ptr::addr_of!(bcj_optmap) as *const option_map, - strfy_encoder: 1, - strfy_decoder: 1, - allow_null: true, - }, - filter_codec_def { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"arm64\0\0\0\0\0\0\0"), - opts_size: core::mem::size_of::() as u32, - id: LZMA_FILTER_ARM64, - parse: Some( - parse_bcj - as unsafe extern "C" fn( - *mut *const c_char, - *const c_char, - *mut c_void, - ) -> *const c_char, - ), - optmap: ::core::ptr::addr_of!(bcj_optmap) as *const option_map, - strfy_encoder: 1, - strfy_decoder: 1, - allow_null: true, - }, - filter_codec_def { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"riscv\0\0\0\0\0\0\0"), - opts_size: core::mem::size_of::() as u32, - id: LZMA_FILTER_RISCV, - parse: Some( - parse_bcj - as unsafe extern "C" fn( - *mut *const c_char, - *const c_char, - *mut c_void, - ) -> *const c_char, - ), - optmap: ::core::ptr::addr_of!(bcj_optmap) as *const option_map, - strfy_encoder: 1, - strfy_decoder: 1, - allow_null: true, - }, - filter_codec_def { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"powerpc\0\0\0\0\0"), - opts_size: core::mem::size_of::() as u32, - id: LZMA_FILTER_POWERPC, - parse: Some( - parse_bcj - as unsafe extern "C" fn( - *mut *const c_char, - *const c_char, - *mut c_void, - ) -> *const c_char, - ), - optmap: ::core::ptr::addr_of!(bcj_optmap) as *const option_map, - strfy_encoder: 1, - strfy_decoder: 1, - allow_null: true, - }, - filter_codec_def { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"ia64\0\0\0\0\0\0\0\0"), - opts_size: core::mem::size_of::() as u32, - id: LZMA_FILTER_IA64, - parse: Some( - parse_bcj - as unsafe extern "C" fn( - *mut *const c_char, - *const c_char, - *mut c_void, - ) -> *const c_char, - ), - optmap: ::core::ptr::addr_of!(bcj_optmap) as *const option_map, - strfy_encoder: 1, - strfy_decoder: 1, - allow_null: true, - }, - filter_codec_def { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"sparc\0\0\0\0\0\0\0"), - opts_size: core::mem::size_of::() as u32, - id: LZMA_FILTER_SPARC, - parse: Some( - parse_bcj - as unsafe extern "C" fn( - *mut *const c_char, - *const c_char, - *mut c_void, - ) -> *const c_char, - ), - optmap: ::core::ptr::addr_of!(bcj_optmap) as *const option_map, - strfy_encoder: 1, - strfy_decoder: 1, - allow_null: true, - }, - filter_codec_def { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"delta\0\0\0\0\0\0\0"), - opts_size: core::mem::size_of::() as u32, - id: LZMA_FILTER_DELTA, - parse: Some( - parse_delta - as unsafe extern "C" fn( - *mut *const c_char, - *const c_char, - *mut c_void, - ) -> *const c_char, - ), - optmap: ::core::ptr::addr_of!(delta_optmap) as *const option_map, - strfy_encoder: 1, - strfy_decoder: 1, - allow_null: false, - }, - ] -}; +static mut filter_name_map: [filter_codec_def; 11] = [ + filter_codec_def { + name: c_chars(*b"lzma1\0\0\0\0\0\0\0"), + opts_size: core::mem::size_of::() as u32, + id: LZMA_FILTER_LZMA1, + parse: Some( + parse_lzma12 + as unsafe fn(*mut *const c_char, *const c_char, *mut c_void) -> *const c_char, + ), + optmap: ::core::ptr::addr_of!(lzma12_optmap) as *const option_map, + strfy_encoder: 9, + strfy_decoder: 5, + allow_null: false, + }, + filter_codec_def { + name: c_chars(*b"lzma2\0\0\0\0\0\0\0"), + opts_size: core::mem::size_of::() as u32, + id: LZMA_FILTER_LZMA2, + parse: Some( + parse_lzma12 + as unsafe fn(*mut *const c_char, *const c_char, *mut c_void) -> *const c_char, + ), + optmap: ::core::ptr::addr_of!(lzma12_optmap) as *const option_map, + strfy_encoder: 9, + strfy_decoder: 2, + allow_null: false, + }, + filter_codec_def { + name: c_chars(*b"x86\0\0\0\0\0\0\0\0\0"), + opts_size: core::mem::size_of::() as u32, + id: LZMA_FILTER_X86, + parse: Some( + parse_bcj as unsafe fn(*mut *const c_char, *const c_char, *mut c_void) -> *const c_char, + ), + optmap: ::core::ptr::addr_of!(bcj_optmap) as *const option_map, + strfy_encoder: 1, + strfy_decoder: 1, + allow_null: true, + }, + filter_codec_def { + name: c_chars(*b"arm\0\0\0\0\0\0\0\0\0"), + opts_size: core::mem::size_of::() as u32, + id: LZMA_FILTER_ARM, + parse: Some( + parse_bcj as unsafe fn(*mut *const c_char, *const c_char, *mut c_void) -> *const c_char, + ), + optmap: ::core::ptr::addr_of!(bcj_optmap) as *const option_map, + strfy_encoder: 1, + strfy_decoder: 1, + allow_null: true, + }, + filter_codec_def { + name: c_chars(*b"armthumb\0\0\0\0"), + opts_size: core::mem::size_of::() as u32, + id: LZMA_FILTER_ARMTHUMB, + parse: Some( + parse_bcj as unsafe fn(*mut *const c_char, *const c_char, *mut c_void) -> *const c_char, + ), + optmap: ::core::ptr::addr_of!(bcj_optmap) as *const option_map, + strfy_encoder: 1, + strfy_decoder: 1, + allow_null: true, + }, + filter_codec_def { + name: c_chars(*b"arm64\0\0\0\0\0\0\0"), + opts_size: core::mem::size_of::() as u32, + id: LZMA_FILTER_ARM64, + parse: Some( + parse_bcj as unsafe fn(*mut *const c_char, *const c_char, *mut c_void) -> *const c_char, + ), + optmap: ::core::ptr::addr_of!(bcj_optmap) as *const option_map, + strfy_encoder: 1, + strfy_decoder: 1, + allow_null: true, + }, + filter_codec_def { + name: c_chars(*b"riscv\0\0\0\0\0\0\0"), + opts_size: core::mem::size_of::() as u32, + id: LZMA_FILTER_RISCV, + parse: Some( + parse_bcj as unsafe fn(*mut *const c_char, *const c_char, *mut c_void) -> *const c_char, + ), + optmap: ::core::ptr::addr_of!(bcj_optmap) as *const option_map, + strfy_encoder: 1, + strfy_decoder: 1, + allow_null: true, + }, + filter_codec_def { + name: c_chars(*b"powerpc\0\0\0\0\0"), + opts_size: core::mem::size_of::() as u32, + id: LZMA_FILTER_POWERPC, + parse: Some( + parse_bcj as unsafe fn(*mut *const c_char, *const c_char, *mut c_void) -> *const c_char, + ), + optmap: ::core::ptr::addr_of!(bcj_optmap) as *const option_map, + strfy_encoder: 1, + strfy_decoder: 1, + allow_null: true, + }, + filter_codec_def { + name: c_chars(*b"ia64\0\0\0\0\0\0\0\0"), + opts_size: core::mem::size_of::() as u32, + id: LZMA_FILTER_IA64, + parse: Some( + parse_bcj as unsafe fn(*mut *const c_char, *const c_char, *mut c_void) -> *const c_char, + ), + optmap: ::core::ptr::addr_of!(bcj_optmap) as *const option_map, + strfy_encoder: 1, + strfy_decoder: 1, + allow_null: true, + }, + filter_codec_def { + name: c_chars(*b"sparc\0\0\0\0\0\0\0"), + opts_size: core::mem::size_of::() as u32, + id: LZMA_FILTER_SPARC, + parse: Some( + parse_bcj as unsafe fn(*mut *const c_char, *const c_char, *mut c_void) -> *const c_char, + ), + optmap: ::core::ptr::addr_of!(bcj_optmap) as *const option_map, + strfy_encoder: 1, + strfy_decoder: 1, + allow_null: true, + }, + filter_codec_def { + name: c_chars(*b"delta\0\0\0\0\0\0\0"), + opts_size: core::mem::size_of::() as u32, + id: LZMA_FILTER_DELTA, + parse: Some( + parse_delta + as unsafe fn(*mut *const c_char, *const c_char, *mut c_void) -> *const c_char, + ), + optmap: ::core::ptr::addr_of!(delta_optmap) as *const option_map, + strfy_encoder: 1, + strfy_decoder: 1, + allow_null: false, + }, +]; unsafe fn parse_options( str: *mut *const c_char, str_end: *const c_char, @@ -571,7 +512,7 @@ unsafe fn parse_options( if value_len == 0 { return crate::c_str!("Option value cannot be empty"); } - if (*optmap.offset(i as isize)).type_0 == OPTMAP_TYPE_LZMA_PRESET { + if (*optmap.offset(i as isize)).option_type == OPTMAP_TYPE_LZMA_PRESET { let errmsg: *const c_char = set_lzma12_preset(str, name_eq_value_end, filter_options); if !errmsg.is_null() { @@ -674,7 +615,7 @@ unsafe fn parse_options( let ptr: *mut c_void = (filter_options as *mut c_char) .offset((*optmap.offset(i as isize)).offset as isize) as *mut c_void; - match (*optmap.offset(i as isize)).type_0 { + match (*optmap.offset(i as isize)).option_type { 1 => { *(ptr as *mut lzma_mode) = v as lzma_mode; } @@ -739,8 +680,9 @@ unsafe fn parse_filter( return crate::c_str!("Memory allocation failed"); } *str = opts_start; - let errmsg: *const c_char = - filter_name_map[i as usize].parse.unwrap()(str, str_end, options); + debug_assert!(filter_name_map[i as usize].parse.is_some()); + let parse = filter_name_map[i as usize].parse.unwrap_unchecked(); + let errmsg: *const c_char = parse(str, str_end, options); if !errmsg.is_null() { lzma_free(options, allocator); return errmsg; @@ -759,7 +701,6 @@ unsafe fn str_to_filters( flags: u32, allocator: *const lzma_allocator, ) -> *const c_char { - let mut current_block: u64; let mut errmsg: *const c_char = core::ptr::null(); while **str as u8 == b' ' { *str = (*str).offset(1); @@ -819,7 +760,6 @@ unsafe fn str_to_filters( loop { if i_0 == LZMA_FILTERS_MAX as size_t { errmsg = crate::c_str!("The maximum number of filters is four"); - current_block = 6100283484465977373; break; } else { if *(*str) as u8 == b'-' && *(*str).offset(1) as u8 == b'-' { @@ -836,7 +776,6 @@ unsafe fn str_to_filters( } if filter_end == *str { errmsg = crate::c_str!("Filter name is missing"); - current_block = 6100283484465977373; break; } else { errmsg = parse_filter( @@ -848,7 +787,6 @@ unsafe fn str_to_filters( only_xz, ); if !errmsg.is_null() { - current_block = 6100283484465977373; break; } while **str as u8 == b' ' { @@ -856,45 +794,39 @@ unsafe fn str_to_filters( } i_0 += 1; if **str == 0 { - current_block = 15090052786889560393; break; } } } } - match current_block { - 15090052786889560393 => { - temp_filters[i_0 as usize].id = LZMA_VLI_UNKNOWN; - temp_filters[i_0 as usize].options = core::ptr::null_mut(); - if flags & LZMA_STR_NO_VALIDATION as u32 == 0 { - let mut dummy: size_t = 0; - let ret: lzma_ret = lzma_validate_chain( - ::core::ptr::addr_of_mut!(temp_filters) as *mut lzma_filter, - ::core::ptr::addr_of_mut!(dummy), - ); - if ret != LZMA_OK { - errmsg = b"Invalid filter chain ('lzma2' missing at the end?)\0" as *const u8 - as *const c_char; - current_block = 6100283484465977373; - } else { - current_block = 12381812505308290051; - } + if errmsg.is_null() && **str == 0 { + temp_filters[i_0 as usize].id = LZMA_VLI_UNKNOWN; + temp_filters[i_0 as usize].options = core::ptr::null_mut(); + if flags & LZMA_STR_NO_VALIDATION as u32 == 0 { + let mut dummy: size_t = 0; + let ret: lzma_ret = lzma_validate_chain( + ::core::ptr::addr_of_mut!(temp_filters) as *mut lzma_filter, + ::core::ptr::addr_of_mut!(dummy), + ); + if ret != LZMA_OK { + errmsg = b"Invalid filter chain ('lzma2' missing at the end?)\0" as *const u8 + as *const c_char; } else { - current_block = 12381812505308290051; - } - match current_block { - 6100283484465977373 => {} - _ => { - core::ptr::copy_nonoverlapping( - ::core::ptr::addr_of_mut!(temp_filters) as *const u8, - filters as *mut u8, - (i_0 + 1) * core::mem::size_of::(), - ); - return core::ptr::null(); - } + core::ptr::copy_nonoverlapping( + ::core::ptr::addr_of_mut!(temp_filters) as *const u8, + filters as *mut u8, + (i_0 + 1) * core::mem::size_of::(), + ); + return core::ptr::null(); } + } else { + core::ptr::copy_nonoverlapping( + ::core::ptr::addr_of_mut!(temp_filters) as *const u8, + filters as *mut u8, + (i_0 + 1) * core::mem::size_of::(), + ); + return core::ptr::null(); } - _ => {} } while i_0 > 0 { i_0 -= 1; @@ -942,12 +874,12 @@ unsafe fn strfy_filter( ) { let mut i: size_t = 0; while i < optmap_count { - if (*optmap.offset(i as isize)).type_0 != OPTMAP_TYPE_LZMA_PRESET { + if (*optmap.offset(i as isize)).option_type != OPTMAP_TYPE_LZMA_PRESET { let mut v: u32 = 0; let ptr: *const c_void = (filter_options as *const c_char) .offset((*optmap.offset(i as isize)).offset as isize) as *const c_void; - match (*optmap.offset(i as isize)).type_0 { + match (*optmap.offset(i as isize)).option_type { 1 => { v = *(ptr as *const lzma_mode) as u32; } @@ -1023,9 +955,9 @@ pub unsafe fn lzma_str_from_filters( buf: core::ptr::null_mut(), pos: 0, }; - let ret_: lzma_ret = str_init(::core::ptr::addr_of_mut!(dest), allocator); - if ret_ != LZMA_OK { - return ret_; + let ret: lzma_ret = str_init(::core::ptr::addr_of_mut!(dest), allocator); + if ret != LZMA_OK { + return ret; } let show_opts: bool = flags & (LZMA_STR_ENCODER as u32 | LZMA_STR_DECODER as u32) != 0; let opt_delim: *const c_char = if flags & LZMA_STR_GETOPT_LONG as u32 != 0 { @@ -1117,9 +1049,9 @@ pub unsafe fn lzma_str_list_filters( buf: core::ptr::null_mut(), pos: 0, }; - let ret_: lzma_ret = str_init(::core::ptr::addr_of_mut!(dest), allocator); - if ret_ != LZMA_OK { - return ret_; + let ret: lzma_ret = str_init(::core::ptr::addr_of_mut!(dest), allocator); + if ret != LZMA_OK { + return ret; } let show_opts: bool = flags & (LZMA_STR_ENCODER as u32 | LZMA_STR_DECODER as u32) != 0; let filter_delim: *const c_char = if show_opts { @@ -1176,7 +1108,7 @@ pub unsafe fn lzma_str_list_filters( as *const c_char, ); str_append_str(::core::ptr::addr_of_mut!(dest), crate::c_str!("=<")); - if (*optmap.offset(j as isize)).type_0 == OPTMAP_TYPE_LZMA_PRESET { + if (*optmap.offset(j as isize)).option_type == OPTMAP_TYPE_LZMA_PRESET { str_append_str( ::core::ptr::addr_of_mut!(dest), LZMA12_PRESET_STR.as_ptr(), @@ -1232,8 +1164,8 @@ pub unsafe fn lzma_str_list_filters( unsafe extern "C" fn run_static_initializers() { lzma12_optmap = [ option_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"preset\0\0\0\0\0\0"), - type_0: OPTMAP_TYPE_LZMA_PRESET, + name: c_chars(*b"preset\0\0\0\0\0\0"), + option_type: OPTMAP_TYPE_LZMA_PRESET, flags: 0, offset: 0, u: option_value { @@ -1241,8 +1173,8 @@ unsafe extern "C" fn run_static_initializers() { }, }, option_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"dict\0\0\0\0\0\0\0\0"), - type_0: 0, + name: c_chars(*b"dict\0\0\0\0\0\0\0\0"), + option_type: 0, flags: OPTMAP_USE_BYTE_SUFFIX, offset: 0, u: option_value { @@ -1253,8 +1185,8 @@ unsafe extern "C" fn run_static_initializers() { }, }, option_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"lc\0\0\0\0\0\0\0\0\0\0"), - type_0: 0, + name: c_chars(*b"lc\0\0\0\0\0\0\0\0\0\0"), + option_type: 0, flags: 0, offset: 20, u: option_value { @@ -1265,8 +1197,8 @@ unsafe extern "C" fn run_static_initializers() { }, }, option_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"lp\0\0\0\0\0\0\0\0\0\0"), - type_0: 0, + name: c_chars(*b"lp\0\0\0\0\0\0\0\0\0\0"), + option_type: 0, flags: 0, offset: 24, u: option_value { @@ -1277,8 +1209,8 @@ unsafe extern "C" fn run_static_initializers() { }, }, option_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"pb\0\0\0\0\0\0\0\0\0\0"), - type_0: 0, + name: c_chars(*b"pb\0\0\0\0\0\0\0\0\0\0"), + option_type: 0, flags: 0, offset: 28, u: option_value { @@ -1289,8 +1221,8 @@ unsafe extern "C" fn run_static_initializers() { }, }, option_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"mode\0\0\0\0\0\0\0\0"), - type_0: OPTMAP_TYPE_LZMA_MODE, + name: c_chars(*b"mode\0\0\0\0\0\0\0\0"), + option_type: OPTMAP_TYPE_LZMA_MODE, flags: OPTMAP_USE_NAME_VALUE_MAP, offset: 32, u: option_value { @@ -1298,8 +1230,8 @@ unsafe extern "C" fn run_static_initializers() { }, }, option_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"nice\0\0\0\0\0\0\0\0"), - type_0: 0, + name: c_chars(*b"nice\0\0\0\0\0\0\0\0"), + option_type: 0, flags: 0, offset: 36, u: option_value { @@ -1307,8 +1239,8 @@ unsafe extern "C" fn run_static_initializers() { }, }, option_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"mf\0\0\0\0\0\0\0\0\0\0"), - type_0: OPTMAP_TYPE_LZMA_MATCH_FINDER, + name: c_chars(*b"mf\0\0\0\0\0\0\0\0\0\0"), + option_type: OPTMAP_TYPE_LZMA_MATCH_FINDER, flags: OPTMAP_USE_NAME_VALUE_MAP, offset: 40, u: option_value { @@ -1316,8 +1248,8 @@ unsafe extern "C" fn run_static_initializers() { }, }, option_map { - name: core::mem::transmute::<[u8; 12], [c_char; 12]>(*b"depth\0\0\0\0\0\0\0"), - type_0: 0, + name: c_chars(*b"depth\0\0\0\0\0\0\0"), + option_type: 0, flags: 0, offset: 44, u: option_value { @@ -1330,7 +1262,7 @@ unsafe extern "C" fn run_static_initializers() { ]; } #[used] -#[cfg_attr(target_os = "linux", link_section = ".init_array")] -#[cfg_attr(target_os = "windows", link_section = ".CRT$XIB")] -#[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")] +#[cfg_attr(target_os = "linux", unsafe(link_section = ".init_array"))] +#[cfg_attr(target_os = "windows", unsafe(link_section = ".CRT$XIB"))] +#[cfg_attr(target_os = "macos", unsafe(link_section = "__DATA,__mod_init_func"))] static INIT_ARRAY: [unsafe extern "C" fn(); 1] = [run_static_initializers]; diff --git a/xz/src/common/threading.rs b/xz/src/common/threading.rs new file mode 100644 index 00000000..86f7f21e --- /dev/null +++ b/xz/src/common/threading.rs @@ -0,0 +1,500 @@ +use crate::types::{c_char, c_int, c_long, c_uint, c_void}; + +#[cfg(windows)] +use windows_sys::Win32::Foundation::{CloseHandle, HANDLE, WAIT_OBJECT_0}; +#[cfg(windows)] +use windows_sys::Win32::System::SystemInformation::GetTickCount; +#[cfg(windows)] +use windows_sys::Win32::System::Threading::{ + CONDITION_VARIABLE, CRITICAL_SECTION, DeleteCriticalSection, EnterCriticalSection, INFINITE, + InitializeConditionVariable, InitializeCriticalSection, LeaveCriticalSection, + SleepConditionVariableCS, WaitForSingleObject, WakeConditionVariable, +}; + +#[cfg(windows)] +unsafe extern "C" { + fn _beginthreadex( + security: *mut c_void, + stack_size: c_uint, + start_address: Option u32>, + arglist: *mut c_void, + initflag: c_uint, + thrdaddr: *mut c_uint, + ) -> usize; +} + +pub const MYTHREAD_RET_VALUE: *mut c_void = core::ptr::null_mut(); +pub const SIG_SETMASK: c_int = 3; + +pub type __uint32_t = u32; +pub type __darwin_time_t = c_long; +pub type __darwin_sigset_t = __uint32_t; + +#[cfg(not(windows))] +#[derive(Copy, Clone)] +#[repr(C)] +pub struct __darwin_pthread_handler_rec { + pub __routine: Option ()>, + pub __arg: *mut c_void, + pub __next: *mut __darwin_pthread_handler_rec, +} + +#[cfg(not(windows))] +#[derive(Copy, Clone)] +#[repr(C)] +pub struct _opaque_pthread_attr_t { + pub __sig: c_long, + pub __opaque: [c_char; 56], +} + +#[cfg(not(windows))] +#[derive(Copy, Clone)] +#[repr(C)] +pub struct _opaque_pthread_cond_t { + pub __sig: c_long, + pub __opaque: [c_char; 40], +} + +#[cfg(not(windows))] +#[derive(Copy, Clone)] +#[repr(C)] +pub struct _opaque_pthread_condattr_t { + pub __sig: c_long, + pub __opaque: [c_char; 8], +} + +#[cfg(not(windows))] +#[derive(Copy, Clone)] +#[repr(C)] +pub struct _opaque_pthread_mutex_t { + pub __sig: c_long, + pub __opaque: [c_char; 56], +} + +#[cfg(not(windows))] +#[derive(Copy, Clone)] +#[repr(C)] +pub struct _opaque_pthread_mutexattr_t { + pub __sig: c_long, + pub __opaque: [c_char; 8], +} + +#[cfg(not(windows))] +#[derive(Copy, Clone)] +#[repr(C)] +pub struct _opaque_pthread_t { + pub __sig: c_long, + pub __cleanup_stack: *mut __darwin_pthread_handler_rec, + pub __opaque: [c_char; 8176], +} + +#[cfg(not(windows))] +pub type __darwin_pthread_attr_t = _opaque_pthread_attr_t; +#[cfg(not(windows))] +pub type __darwin_pthread_cond_t = _opaque_pthread_cond_t; +#[cfg(not(windows))] +pub type __darwin_pthread_condattr_t = _opaque_pthread_condattr_t; +#[cfg(not(windows))] +pub type __darwin_pthread_mutex_t = _opaque_pthread_mutex_t; +#[cfg(not(windows))] +pub type __darwin_pthread_mutexattr_t = _opaque_pthread_mutexattr_t; +#[cfg(not(windows))] +pub type __darwin_pthread_t = *mut _opaque_pthread_t; + +#[cfg(not(windows))] +pub type pthread_attr_t = __darwin_pthread_attr_t; +pub type sigset_t = __darwin_sigset_t; +pub type time_t = __darwin_time_t; + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct timespec { + pub tv_sec: __darwin_time_t, + pub tv_nsec: c_long, +} + +pub type clockid_t = c_uint; +pub const _CLOCK_THREAD_CPUTIME_ID: clockid_t = 16; +pub const _CLOCK_PROCESS_CPUTIME_ID: clockid_t = 12; +pub const _CLOCK_UPTIME_RAW_APPROX: clockid_t = 9; +pub const _CLOCK_UPTIME_RAW: clockid_t = 8; +pub const _CLOCK_MONOTONIC_RAW_APPROX: clockid_t = 5; +pub const _CLOCK_MONOTONIC_RAW: clockid_t = 4; +pub const _CLOCK_MONOTONIC: clockid_t = 6; +pub const _CLOCK_REALTIME: clockid_t = 0; + +#[cfg(not(windows))] +pub type pthread_cond_t = __darwin_pthread_cond_t; +#[cfg(not(windows))] +pub type pthread_condattr_t = __darwin_pthread_condattr_t; +#[cfg(not(windows))] +pub type pthread_mutex_t = __darwin_pthread_mutex_t; +#[cfg(not(windows))] +pub type pthread_mutexattr_t = __darwin_pthread_mutexattr_t; +#[cfg(not(windows))] +pub type pthread_t = __darwin_pthread_t; +#[cfg(windows)] +pub type pthread_attr_t = HANDLE; +#[cfg(windows)] +pub type pthread_cond_t = CONDITION_VARIABLE; +#[cfg(windows)] +pub type pthread_condattr_t = HANDLE; +#[cfg(windows)] +pub type pthread_mutex_t = CRITICAL_SECTION; +#[cfg(windows)] +pub type pthread_mutexattr_t = HANDLE; +#[cfg(windows)] +pub type pthread_t = HANDLE; + +#[cfg(not(windows))] +pub type mythread = pthread_t; +#[cfg(windows)] +pub type mythread = HANDLE; +#[cfg(not(windows))] +pub type mythread_mutex = pthread_mutex_t; +#[cfg(windows)] +pub type mythread_mutex = CRITICAL_SECTION; + +#[cfg(not(windows))] +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mythread_cond { + pub cond: pthread_cond_t, + pub clk_id: clockid_t, +} + +#[cfg(windows)] +pub type mythread_cond = CONDITION_VARIABLE; +#[cfg(not(windows))] +pub type mythread_condtime = timespec; + +#[cfg(windows)] +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mythread_condtime { + pub start: u32, + pub timeout: u32, +} + +#[cfg(not(windows))] +unsafe extern "C" { + pub fn clock_gettime(__clock_id: clockid_t, __tp: *mut timespec) -> c_int; + pub fn pthread_cond_destroy(_: *mut pthread_cond_t) -> c_int; + pub fn pthread_cond_init(_: *mut pthread_cond_t, _: *const pthread_condattr_t) -> c_int; + pub fn pthread_cond_signal(_: *mut pthread_cond_t) -> c_int; + pub fn pthread_cond_timedwait( + _: *mut pthread_cond_t, + _: *mut pthread_mutex_t, + _: *const timespec, + ) -> c_int; + pub fn pthread_cond_wait(_: *mut pthread_cond_t, _: *mut pthread_mutex_t) -> c_int; + pub fn pthread_create( + _: *mut pthread_t, + _: *const pthread_attr_t, + _: Option *mut c_void>, + _: *mut c_void, + ) -> c_int; + pub fn pthread_join(_: pthread_t, _: *mut *mut c_void) -> c_int; + pub fn pthread_mutex_destroy(_: *mut pthread_mutex_t) -> c_int; + pub fn pthread_mutex_init(_: *mut pthread_mutex_t, _: *const pthread_mutexattr_t) -> c_int; + pub fn pthread_mutex_lock(_: *mut pthread_mutex_t) -> c_int; + pub fn pthread_mutex_unlock(_: *mut pthread_mutex_t) -> c_int; + pub fn pthread_sigmask(_: c_int, _: *const sigset_t, _: *mut sigset_t) -> c_int; +} + +#[cfg(not(windows))] +#[inline] +pub fn mythread_sigmask(how: c_int, set: *const sigset_t, oset: *mut sigset_t) { + let _ret: c_int = + unsafe { pthread_sigmask(how, set as *const sigset_t, oset as *mut sigset_t) }; +} + +#[cfg(windows)] +#[inline] +pub fn mythread_sigmask(_how: c_int, _set: *const sigset_t, _oset: *mut sigset_t) {} + +#[cfg(windows)] +struct mythread_start_info { + func: unsafe extern "C" fn(*mut c_void) -> *mut c_void, + arg: *mut c_void, +} + +#[cfg(windows)] +unsafe extern "system" fn mythread_start(param: *mut c_void) -> u32 { + let info = Box::from_raw(param.cast::()); + let _ = (info.func)(info.arg); + 0 +} + +#[cfg(not(windows))] +#[inline] +pub fn mythread_create( + thread: *mut mythread, + func: unsafe extern "C" fn(*mut c_void) -> *mut c_void, + arg: *mut c_void, +) -> c_int { + let mut old: sigset_t = 0; + let mut all: sigset_t = 0; + all = !(0 as sigset_t); + mythread_sigmask( + SIG_SETMASK, + ::core::ptr::addr_of_mut!(all), + ::core::ptr::addr_of_mut!(old), + ); + let ret: c_int = unsafe { + pthread_create( + thread as *mut pthread_t, + core::ptr::null(), + Some(func), + arg as *mut c_void, + ) + }; + mythread_sigmask( + SIG_SETMASK, + ::core::ptr::addr_of_mut!(old), + core::ptr::null_mut(), + ); + ret +} + +#[cfg(windows)] +#[inline] +pub fn mythread_create( + thread: *mut mythread, + func: unsafe extern "C" fn(*mut c_void) -> *mut c_void, + arg: *mut c_void, +) -> c_int { + let info = Box::into_raw(Box::new(mythread_start_info { func, arg })); + let ret = unsafe { + _beginthreadex( + core::ptr::null_mut(), + 0, + Some(mythread_start), + info.cast::(), + 0, + core::ptr::null_mut(), + ) + }; + if ret == 0 { + unsafe { + let _ = Box::from_raw(info); + } + -1 + } else { + unsafe { + *thread = ret as HANDLE; + } + 0 + } +} + +#[cfg(not(windows))] +#[inline] +pub fn mythread_join(thread: mythread) -> c_int { + unsafe { pthread_join(thread as pthread_t, core::ptr::null_mut()) } +} + +#[cfg(windows)] +#[inline] +pub fn mythread_join(thread: mythread) -> c_int { + let mut ret = 0; + unsafe { + if WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0 { + ret = -1; + } + if CloseHandle(thread) == 0 { + ret = -1; + } + } + ret +} + +#[cfg(not(windows))] +#[inline] +pub fn mythread_mutex_init(mutex: *mut mythread_mutex) -> c_int { + unsafe { pthread_mutex_init(mutex as *mut pthread_mutex_t, core::ptr::null()) } +} + +#[cfg(windows)] +#[inline] +pub fn mythread_mutex_init(mutex: *mut mythread_mutex) -> c_int { + unsafe { + InitializeCriticalSection(mutex); + } + 0 +} + +#[cfg(not(windows))] +#[inline] +pub fn mythread_mutex_destroy(mutex: *mut mythread_mutex) { + let _ret: c_int = unsafe { pthread_mutex_destroy(mutex as *mut pthread_mutex_t) }; +} + +#[cfg(windows)] +#[inline] +pub fn mythread_mutex_destroy(mutex: *mut mythread_mutex) { + unsafe { + DeleteCriticalSection(mutex); + } +} + +#[cfg(not(windows))] +#[inline] +pub fn mythread_mutex_lock(mutex: *mut mythread_mutex) { + let _ret: c_int = unsafe { pthread_mutex_lock(mutex as *mut pthread_mutex_t) }; +} + +#[cfg(windows)] +#[inline] +pub fn mythread_mutex_lock(mutex: *mut mythread_mutex) { + unsafe { + EnterCriticalSection(mutex); + } +} + +#[cfg(not(windows))] +#[inline] +pub fn mythread_mutex_unlock(mutex: *mut mythread_mutex) { + let _ret: c_int = unsafe { pthread_mutex_unlock(mutex as *mut pthread_mutex_t) }; +} + +#[cfg(windows)] +#[inline] +pub fn mythread_mutex_unlock(mutex: *mut mythread_mutex) { + unsafe { + LeaveCriticalSection(mutex); + } +} + +#[cfg(not(windows))] +#[inline] +pub fn mythread_cond_init(mycond: *mut mythread_cond) -> c_int { + unsafe { + (*mycond).clk_id = _CLOCK_REALTIME; + pthread_cond_init(::core::ptr::addr_of_mut!((*mycond).cond), core::ptr::null()) + } +} + +#[cfg(windows)] +#[inline] +pub fn mythread_cond_init(cond: *mut mythread_cond) -> c_int { + unsafe { + InitializeConditionVariable(cond); + } + 0 +} + +#[cfg(not(windows))] +#[inline] +pub fn mythread_cond_destroy(cond: *mut mythread_cond) { + let _ret: c_int = unsafe { pthread_cond_destroy(::core::ptr::addr_of_mut!((*cond).cond)) }; +} + +#[cfg(windows)] +#[inline] +pub fn mythread_cond_destroy(_cond: *mut mythread_cond) {} + +#[cfg(not(windows))] +#[inline] +pub fn mythread_cond_signal(cond: *mut mythread_cond) { + let _ret: c_int = unsafe { pthread_cond_signal(::core::ptr::addr_of_mut!((*cond).cond)) }; +} + +#[cfg(windows)] +#[inline] +pub fn mythread_cond_signal(cond: *mut mythread_cond) { + unsafe { + WakeConditionVariable(cond); + } +} + +#[cfg(not(windows))] +#[inline] +pub fn mythread_cond_wait(cond: *mut mythread_cond, mutex: *mut mythread_mutex) { + let _ret: c_int = unsafe { + pthread_cond_wait( + ::core::ptr::addr_of_mut!((*cond).cond), + mutex as *mut pthread_mutex_t, + ) + }; +} + +#[cfg(windows)] +#[inline] +pub fn mythread_cond_wait(cond: *mut mythread_cond, mutex: *mut mythread_mutex) { + unsafe { + let _ = SleepConditionVariableCS(cond, mutex, INFINITE); + } +} + +#[cfg(not(windows))] +#[inline] +pub fn mythread_cond_timedwait( + cond: *mut mythread_cond, + mutex: *mut mythread_mutex, + condtime: *const mythread_condtime, +) -> c_int { + let ret: c_int = unsafe { + pthread_cond_timedwait( + ::core::ptr::addr_of_mut!((*cond).cond), + mutex as *mut pthread_mutex_t, + condtime as *const timespec, + ) + }; + ret +} + +#[cfg(windows)] +#[inline] +pub fn mythread_cond_timedwait( + cond: *mut mythread_cond, + mutex: *mut mythread_mutex, + condtime: *const mythread_condtime, +) -> c_int { + let (start, timeout_ms) = unsafe { ((*condtime).start, (*condtime).timeout) }; + let elapsed = unsafe { GetTickCount().wrapping_sub(start) }; + let timeout = if elapsed >= timeout_ms { + 0 + } else { + timeout_ms - elapsed + }; + let ret = unsafe { SleepConditionVariableCS(cond, mutex, timeout) }; + i32::from(ret == 0) +} + +#[cfg(not(windows))] +#[inline] +pub fn mythread_condtime_set( + condtime: *mut mythread_condtime, + cond: *const mythread_cond, + timeout_ms: u32, +) { + unsafe { + (*condtime).tv_sec = timeout_ms.wrapping_div(1000) as time_t as __darwin_time_t; + (*condtime).tv_nsec = timeout_ms.wrapping_rem(1000).wrapping_mul(1_000_000) as c_long; + let mut now: timespec = timespec { + tv_sec: 0, + tv_nsec: 0, + }; + let _ret: c_int = clock_gettime((*cond).clk_id, ::core::ptr::addr_of_mut!(now)); + (*condtime).tv_sec += now.tv_sec; + (*condtime).tv_nsec += now.tv_nsec; + if (*condtime).tv_nsec >= 1_000_000_000 { + (*condtime).tv_nsec -= 1_000_000_000; + (*condtime).tv_sec += 1; + } + } +} + +#[cfg(windows)] +#[inline] +pub fn mythread_condtime_set( + condtime: *mut mythread_condtime, + _cond: *const mythread_cond, + timeout_ms: u32, +) { + unsafe { + (*condtime).start = GetTickCount(); + (*condtime).timeout = timeout_ms; + } +} diff --git a/xz/src/common/vli_decoder.rs b/xz/src/common/vli_decoder.rs index 96fa2606..25e5aab3 100644 --- a/xz/src/common/vli_decoder.rs +++ b/xz/src/common/vli_decoder.rs @@ -2,7 +2,7 @@ use crate::types::*; pub unsafe fn lzma_vli_decode( vli: *mut lzma_vli, mut vli_pos: *mut size_t, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, ) -> lzma_ret { @@ -25,7 +25,7 @@ pub unsafe fn lzma_vli_decode( } } loop { - let byte: u8 = *in_0.offset(*in_pos as isize); + let byte: u8 = *input.offset(*in_pos as isize); *in_pos += 1; *vli += ((byte & 0x7f) as lzma_vli) << (*vli_pos * 7); *vli_pos += 1; diff --git a/xz/src/delta/delta_common.rs b/xz/src/delta/delta_common.rs index 5c919724..59103301 100644 --- a/xz/src/delta/delta_common.rs +++ b/xz/src/delta/delta_common.rs @@ -1,5 +1,5 @@ use crate::types::*; -unsafe extern "C" fn delta_coder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { +unsafe fn delta_coder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_delta_coder = coder_ptr as *mut lzma_delta_coder; lzma_next_end(::core::ptr::addr_of_mut!((*coder).next), allocator); crate::alloc::internal_free(coder as *mut c_void, allocator); @@ -16,8 +16,7 @@ pub unsafe fn lzma_delta_coder_init( return LZMA_MEM_ERROR; } (*next).coder = coder as *mut c_void; - (*next).end = - Some(delta_coder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> ()); + (*next).end = Some(delta_coder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); (*coder).next = lzma_next_coder_s { coder: core::ptr::null_mut(), id: LZMA_VLI_UNKNOWN, @@ -48,10 +47,10 @@ pub unsafe fn lzma_delta_coder_init( filters.offset(1), ) } -pub(crate) unsafe extern "C" fn lzma_delta_coder_memusage(options: *const c_void) -> u64 { +pub(crate) unsafe fn lzma_delta_coder_memusage(options: *const c_void) -> u64 { let opt: *const lzma_options_delta = options as *const lzma_options_delta; if opt.is_null() - || (*opt).type_0 != LZMA_DELTA_TYPE_BYTE + || (*opt).type_ != LZMA_DELTA_TYPE_BYTE || (*opt).dist < LZMA_DELTA_DIST_MIN || (*opt).dist > LZMA_DELTA_DIST_MAX { diff --git a/xz/src/delta/delta_decoder.rs b/xz/src/delta/delta_decoder.rs index c990de8e..efd7c0bb 100644 --- a/xz/src/delta/delta_decoder.rs +++ b/xz/src/delta/delta_decoder.rs @@ -1,24 +1,26 @@ use crate::types::*; -unsafe fn decode_buffer(coder: *mut lzma_delta_coder, buffer: *mut u8, size: size_t) { - let distance: size_t = (*coder).distance; - let history = (*coder).history.as_mut_ptr(); - let mut pos = (*coder).pos; - let mut i: size_t = 0; - while i < size { - let byte_ptr = buffer.add(i); - let byte = (*byte_ptr) - .wrapping_add(*history.add((distance.wrapping_add(pos as size_t) & 0xff) as usize)); - *byte_ptr = byte; - *history.add((pos & 0xff) as usize) = byte; +fn decode_buffer(coder: &mut lzma_delta_coder, buffer: &mut [u8]) { + let distance: size_t = coder.distance; + let history = coder.history.as_mut_ptr(); + let mut pos = coder.pos; + let mut i: usize = 0; + while i < buffer.len() { + let byte = buffer[i].wrapping_add(unsafe { + *history.add((distance.wrapping_add(pos as size_t) & 0xff) as usize) + }); + buffer[i] = byte; + unsafe { + *history.add((pos & 0xff) as usize) = byte; + } pos = pos.wrapping_sub(1); i += 1; } - (*coder).pos = pos; + coder.pos = pos; } -unsafe extern "C" fn delta_decode( +unsafe fn delta_decode( coder_ptr: *mut c_void, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, @@ -26,12 +28,14 @@ unsafe extern "C" fn delta_decode( out_size: size_t, action: lzma_action, ) -> lzma_ret { - let coder: *mut lzma_delta_coder = coder_ptr as *mut lzma_delta_coder; + let coder: &mut lzma_delta_coder = &mut *(coder_ptr as *mut lzma_delta_coder); let out_start: size_t = *out_pos; - let ret: lzma_ret = (*coder).next.code.unwrap()( - (*coder).next.coder, + debug_assert!(coder.next.code.is_some()); + let code = coder.next.code.unwrap_unchecked(); + let ret: lzma_ret = code( + coder.next.coder, allocator, - in_0, + input, in_pos, in_size, out, @@ -42,18 +46,21 @@ unsafe extern "C" fn delta_decode( debug_assert!(*out_pos >= out_start); let size: size_t = *out_pos - out_start; if size > 0 { - decode_buffer(coder, out.offset(out_start as isize), size); + decode_buffer( + coder, + core::slice::from_raw_parts_mut(out.add(out_start), size), + ); } ret } -pub(crate) unsafe extern "C" fn lzma_delta_decoder_init( +pub(crate) unsafe fn lzma_delta_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, ) -> lzma_ret { (*next).code = Some( delta_decode - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -67,7 +74,7 @@ pub(crate) unsafe extern "C" fn lzma_delta_decoder_init( ); lzma_delta_coder_init(next, allocator, filters) } -pub(crate) unsafe extern "C" fn lzma_delta_props_decode( +pub(crate) unsafe fn lzma_delta_props_decode( options: *mut *mut c_void, allocator: *const lzma_allocator, props: *const u8, @@ -82,7 +89,7 @@ pub(crate) unsafe extern "C" fn lzma_delta_props_decode( if opt.is_null() { return LZMA_MEM_ERROR; } - (*opt).type_0 = LZMA_DELTA_TYPE_BYTE; + (*opt).type_ = LZMA_DELTA_TYPE_BYTE; (*opt).dist = u32::from(*props) + 1; *options = opt as *mut c_void; LZMA_OK diff --git a/xz/src/delta/delta_encoder.rs b/xz/src/delta/delta_encoder.rs index 786f5411..a2df96e8 100644 --- a/xz/src/delta/delta_encoder.rs +++ b/xz/src/delta/delta_encoder.rs @@ -1,37 +1,33 @@ use crate::types::*; -unsafe fn copy_and_encode( - coder: *mut lzma_delta_coder, - in_0: *const u8, - out: *mut u8, - size: size_t, -) { - let distance: size_t = (*coder).distance; - let mut i: size_t = 0; - while i < size { - let tmp: u8 = - (*coder).history[(distance.wrapping_add((*coder).pos as size_t) & 0xff) as usize]; - (*coder).history[((*coder).pos & 0xff) as usize] = *in_0.offset(i as isize); - (*coder).pos = (*coder).pos.wrapping_sub(1); - *out.offset(i as isize) = (*in_0.offset(i as isize)).wrapping_sub(tmp); +fn copy_and_encode(coder: &mut lzma_delta_coder, input: &[u8], output: &mut [u8]) { + let distance: size_t = coder.distance; + debug_assert_eq!(input.len(), output.len()); + let mut i: usize = 0; + while i < input.len() { + let tmp: u8 = coder.history[(distance.wrapping_add(coder.pos as size_t) & 0xff) as usize]; + let byte = input[i]; + coder.history[(coder.pos & 0xff) as usize] = byte; + coder.pos = coder.pos.wrapping_sub(1); + output[i] = byte.wrapping_sub(tmp); i += 1; } } -unsafe fn encode_in_place(coder: *mut lzma_delta_coder, buffer: *mut u8, size: size_t) { - let distance: size_t = (*coder).distance; - let mut i: size_t = 0; - while i < size { - let tmp: u8 = - (*coder).history[(distance.wrapping_add((*coder).pos as size_t) & 0xff) as usize]; - (*coder).history[((*coder).pos & 0xff) as usize] = *buffer.offset(i as isize); - (*coder).pos = (*coder).pos.wrapping_sub(1); - *buffer.offset(i as isize) = (*buffer.offset(i as isize)).wrapping_sub(tmp); +fn encode_in_place(coder: &mut lzma_delta_coder, buffer: &mut [u8]) { + let distance: size_t = coder.distance; + let mut i: usize = 0; + while i < buffer.len() { + let tmp: u8 = coder.history[(distance.wrapping_add(coder.pos as size_t) & 0xff) as usize]; + let byte = buffer[i]; + coder.history[(coder.pos & 0xff) as usize] = byte; + coder.pos = coder.pos.wrapping_sub(1); + buffer[i] = byte.wrapping_sub(tmp); i += 1; } } -unsafe extern "C" fn delta_encode( +unsafe fn delta_encode( coder_ptr: *mut c_void, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, @@ -39,55 +35,59 @@ unsafe extern "C" fn delta_encode( out_size: size_t, action: lzma_action, ) -> lzma_ret { - let coder: *mut lzma_delta_coder = coder_ptr as *mut lzma_delta_coder; - let mut ret: lzma_ret = LZMA_OK; - if (*coder).next.code.is_none() { - debug_assert!(in_size >= *in_pos); - debug_assert!(out_size >= *out_pos); - let in_avail: size_t = in_size - *in_pos; - let out_avail: size_t = out_size - *out_pos; - let size: size_t = if in_avail < out_avail { - in_avail - } else { - out_avail - }; - if size > 0 { - copy_and_encode( - coder, - in_0.offset(*in_pos as isize), - out.offset(*out_pos as isize), - size, - ); + let coder: &mut lzma_delta_coder = &mut *(coder_ptr as *mut lzma_delta_coder); + match coder.next.code { + None => { + debug_assert!(in_size >= *in_pos); + debug_assert!(out_size >= *out_pos); + let in_avail: size_t = in_size - *in_pos; + let out_avail: size_t = out_size - *out_pos; + let size: size_t = if in_avail < out_avail { + in_avail + } else { + out_avail + }; + if size > 0 { + copy_and_encode( + coder, + core::slice::from_raw_parts(input.add(*in_pos), size), + core::slice::from_raw_parts_mut(out.add(*out_pos), size), + ); + } + *in_pos += size; + *out_pos += size; + if action != LZMA_RUN && *in_pos == in_size { + LZMA_STREAM_END + } else { + LZMA_OK + } } - *in_pos += size; - *out_pos += size; - ret = if action != LZMA_RUN && *in_pos == in_size { - LZMA_STREAM_END - } else { - LZMA_OK - }; - } else { - let out_start: size_t = *out_pos; - ret = (*coder).next.code.unwrap()( - (*coder).next.coder, - allocator, - in_0, - in_pos, - in_size, - out, - out_pos, - out_size, - action, - ); - debug_assert!(*out_pos >= out_start); - let size_0: size_t = *out_pos - out_start; - if size_0 > 0 { - encode_in_place(coder, out.offset(out_start as isize), size_0); + Some(code) => { + let out_start: size_t = *out_pos; + let ret = code( + coder.next.coder, + allocator, + input, + in_pos, + in_size, + out, + out_pos, + out_size, + action, + ); + debug_assert!(*out_pos >= out_start); + let size_0: size_t = *out_pos - out_start; + if size_0 > 0 { + encode_in_place( + coder, + core::slice::from_raw_parts_mut(out.add(out_start), size_0), + ); + } + ret } } - ret } -unsafe extern "C" fn delta_encoder_update( +unsafe fn delta_encoder_update( coder_ptr: *mut c_void, allocator: *const lzma_allocator, _filters_null: *const lzma_filter, @@ -100,14 +100,14 @@ unsafe extern "C" fn delta_encoder_update( reversed_filters.offset(1), ) } -pub(crate) unsafe extern "C" fn lzma_delta_encoder_init( +pub(crate) unsafe fn lzma_delta_encoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, ) -> lzma_ret { (*next).code = Some( delta_encode - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -121,7 +121,7 @@ pub(crate) unsafe extern "C" fn lzma_delta_encoder_init( ); (*next).update = Some( delta_encoder_update - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const lzma_filter, @@ -130,10 +130,7 @@ pub(crate) unsafe extern "C" fn lzma_delta_encoder_init( ); lzma_delta_coder_init(next, allocator, filters) } -pub(crate) unsafe extern "C" fn lzma_delta_props_encode( - options: *const c_void, - out: *mut u8, -) -> lzma_ret { +pub(crate) unsafe fn lzma_delta_props_encode(options: *const c_void, out: *mut u8) -> lzma_ret { if lzma_delta_coder_memusage(options) == UINT64_MAX { return LZMA_PROG_ERROR; } diff --git a/xz/src/lib.rs b/xz/src/lib.rs index 5f304142..72878252 100644 --- a/xz/src/lib.rs +++ b/xz/src/lib.rs @@ -4,6 +4,7 @@ non_camel_case_types, non_snake_case, non_upper_case_globals, + unsafe_op_in_unsafe_fn, unused_assignments, clippy::all )] diff --git a/xz/src/lz/lz_decoder.rs b/xz/src/lz/lz_decoder.rs index 1a4ede50..10818c11 100644 --- a/xz/src/lz/lz_decoder.rs +++ b/xz/src/lz/lz_decoder.rs @@ -27,7 +27,7 @@ pub const LZMA_BUFFER_SIZE: u32 = 4096; pub const LZ_DICT_EXTRA: u32 = 0; pub const LZMA_LZ_DECODER_INIT: lzma_lz_decoder = lzma_lz_decoder { coder: core::ptr::null_mut(), - code: None, + code: lzma_lz_decoder_code_uninitialized, reset: None, set_uncompressed: None, end: None, @@ -41,7 +41,7 @@ unsafe fn lz_decoder_reset(coder: *mut lzma_coder) { } unsafe fn decode_buffer( coder: *mut lzma_coder, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, @@ -71,10 +71,10 @@ unsafe fn decode_buffer( (*coder).dict.size.wrapping_sub((*coder).dict.pos) }, ); - let ret: lzma_ret = (*coder).lz.code.unwrap()( + let ret: lzma_ret = ((*coder).lz.code)( (*coder).lz.coder, ::core::ptr::addr_of_mut!((*coder).dict), - in_0, + input, in_pos, in_size, ); @@ -97,10 +97,10 @@ unsafe fn decode_buffer( } } } -unsafe extern "C" fn lz_decode( +unsafe fn lz_decode( coder_ptr: *mut c_void, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, @@ -110,16 +110,18 @@ unsafe extern "C" fn lz_decode( ) -> lzma_ret { let coder: *mut lzma_coder = coder_ptr as *mut lzma_coder; if (*coder).next.code.is_none() { - return decode_buffer(coder, in_0, in_pos, in_size, out, out_pos, out_size); + return decode_buffer(coder, input, in_pos, in_size, out, out_pos, out_size); } while *out_pos < out_size { if !(*coder).next_finished && (*coder).temp.pos == (*coder).temp.size { (*coder).temp.pos = 0; (*coder).temp.size = 0; - let ret: lzma_ret = (*coder).next.code.unwrap()( + debug_assert!((*coder).next.code.is_some()); + let next_code = (*coder).next.code.unwrap_unchecked(); + let ret: lzma_ret = next_code( (*coder).next.coder, allocator, - in_0, + input, in_pos, in_size, ::core::ptr::addr_of_mut!((*coder).temp.buffer) as *mut u8, @@ -142,7 +144,7 @@ unsafe extern "C" fn lz_decode( } return LZMA_OK; } - let ret_0: lzma_ret = decode_buffer( + let ret: lzma_ret = decode_buffer( coder, ::core::ptr::addr_of_mut!((*coder).temp.buffer) as *mut u8, ::core::ptr::addr_of_mut!((*coder).temp.pos), @@ -151,22 +153,22 @@ unsafe extern "C" fn lz_decode( out_pos, out_size, ); - if ret_0 == LZMA_STREAM_END { + if ret == LZMA_STREAM_END { (*coder).this_finished = true; - } else if ret_0 != LZMA_OK { - return ret_0; + } else if ret != LZMA_OK { + return ret; } else if (*coder).next_finished && *out_pos < out_size { return LZMA_DATA_ERROR; } } LZMA_OK } -unsafe extern "C" fn lz_decoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { +unsafe fn lz_decoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_coder = coder_ptr as *mut lzma_coder; lzma_next_end(::core::ptr::addr_of_mut!((*coder).next), allocator); crate::alloc::internal_free((*coder).dict.buf as *mut c_void, allocator); - if (*coder).lz.end.is_some() { - (*coder).lz.end.unwrap()((*coder).lz.coder, allocator); + if let Some(end) = (*coder).lz.end { + end((*coder).lz.coder, allocator); } else { crate::alloc::internal_free((*coder).lz.coder, allocator); } @@ -176,15 +178,13 @@ pub unsafe fn lzma_lz_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, - lz_init: Option< - unsafe extern "C" fn( - *mut lzma_lz_decoder, - *const lzma_allocator, - lzma_vli, - *const c_void, - *mut lzma_lz_options, - ) -> lzma_ret, - >, + lz_init: unsafe fn( + *mut lzma_lz_decoder, + *const lzma_allocator, + lzma_vli, + *const c_void, + *mut lzma_lz_options, + ) -> lzma_ret, ) -> lzma_ret { let mut coder: *mut lzma_coder = (*next).coder as *mut lzma_coder; if coder.is_null() { @@ -195,7 +195,7 @@ pub unsafe fn lzma_lz_decoder_init( (*next).coder = coder as *mut c_void; (*next).code = Some( lz_decode - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -207,8 +207,7 @@ pub unsafe fn lzma_lz_decoder_init( lzma_action, ) -> lzma_ret, ); - (*next).end = - Some(lz_decoder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> ()); + (*next).end = Some(lz_decoder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); (*coder).dict.buf = core::ptr::null_mut(); (*coder).dict.size = 0; (*coder).lz = LZMA_LZ_DECODER_INIT; @@ -230,22 +229,17 @@ pub unsafe fn lzma_lz_decoder_init( preset_dict: core::ptr::null(), preset_dict_size: 0, }; - let lz_init = if let Some(lz_init) = lz_init { - lz_init - } else { - return LZMA_PROG_ERROR; - }; - let ret_: lzma_ret = lz_init( + let ret: lzma_ret = lz_init( ::core::ptr::addr_of_mut!((*coder).lz), allocator, (*filters).id, (*filters).options, ::core::ptr::addr_of_mut!(lz_options), ); - if ret_ != LZMA_OK { - return ret_; + if ret != LZMA_OK { + return ret; } - if (*coder).lz.code.is_none() { + if (*coder).lz.coder.is_null() { return LZMA_PROG_ERROR; } if lz_options.dict_size < 4096 { diff --git a/xz/src/lz/lz_encoder.rs b/xz/src/lz/lz_encoder.rs index 7148d3ef..998cd16b 100644 --- a/xz/src/lz/lz_encoder.rs +++ b/xz/src/lz/lz_encoder.rs @@ -3,6 +3,7 @@ use crate::lz::lz_encoder_mf::{ lzma_mf_bt4_skip, lzma_mf_hc3_find, lzma_mf_hc3_skip, lzma_mf_hc4_find, lzma_mf_hc4_skip, }; use crate::types::*; + #[derive(Copy, Clone)] #[repr(C)] pub struct lzma_lz_options { @@ -43,7 +44,7 @@ unsafe fn move_window(mf: *mut lzma_mf) { unsafe fn fill_window( coder: *mut lzma_coder, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, action: lzma_action, @@ -53,9 +54,21 @@ unsafe fn fill_window( move_window(::core::ptr::addr_of_mut!((*coder).mf)); } let mut write_pos: size_t = (*coder).mf.write_pos as size_t; - let mut ret = if (*coder).next.code.is_none() { + let mut ret = if let Some(code) = (*coder).next.code { + code( + (*coder).next.coder, + allocator, + input, + in_pos, + in_size, + (*coder).mf.buffer, + ::core::ptr::addr_of_mut!(write_pos), + (*coder).mf.size as size_t, + action, + ) + } else { lzma_bufcpy( - in_0, + input, in_pos, in_size, (*coder).mf.buffer, @@ -67,18 +80,6 @@ unsafe fn fill_window( } else { LZMA_OK } - } else { - (*coder).next.code.unwrap()( - (*coder).next.coder, - allocator, - in_0, - in_pos, - in_size, - (*coder).mf.buffer, - ::core::ptr::addr_of_mut!(write_pos), - (*coder).mf.size as size_t, - action, - ) }; (*coder).mf.write_pos = write_pos as u32; core::ptr::write_bytes( @@ -98,14 +99,14 @@ unsafe fn fill_window( (*coder).mf.pending = 0; debug_assert!((*coder).mf.read_pos >= pending); (*coder).mf.read_pos -= pending; - (*coder).mf.skip.unwrap()(::core::ptr::addr_of_mut!((*coder).mf), pending); + ((*coder).mf.skip)(::core::ptr::addr_of_mut!((*coder).mf), pending); } ret } -unsafe extern "C" fn lz_encode( +unsafe fn lz_encode( coder_ptr: *mut c_void, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, @@ -116,12 +117,12 @@ unsafe extern "C" fn lz_encode( let coder: *mut lzma_coder = coder_ptr as *mut lzma_coder; while *out_pos < out_size && (*in_pos < in_size || action != LZMA_RUN) { if (*coder).mf.action == LZMA_RUN && (*coder).mf.read_pos >= (*coder).mf.read_limit { - let ret_: lzma_ret = fill_window(coder, allocator, in_0, in_pos, in_size, action); + let ret_: lzma_ret = fill_window(coder, allocator, input, in_pos, in_size, action); if ret_ != LZMA_OK { return ret_; } } - let ret: lzma_ret = (*coder).lz.code.unwrap()( + let ret: lzma_ret = ((*coder).lz.code)( (*coder).lz.coder, ::core::ptr::addr_of_mut!((*coder).mf), out, @@ -166,34 +167,24 @@ unsafe fn lz_encoder_prepare( (*mf).cyclic_size = (*lz_options).dict_size as u32 + 1; match (*lz_options).match_finder { 3 => { - (*mf).find = Some( - lzma_mf_hc3_find as unsafe extern "C" fn(*mut lzma_mf, *mut lzma_match) -> u32, - ); - (*mf).skip = Some(lzma_mf_hc3_skip as unsafe extern "C" fn(*mut lzma_mf, u32) -> ()); + (*mf).find = lzma_mf_hc3_find as lzma_mf_find_function; + (*mf).skip = lzma_mf_hc3_skip as lzma_mf_skip_function; } 4 => { - (*mf).find = Some( - lzma_mf_hc4_find as unsafe extern "C" fn(*mut lzma_mf, *mut lzma_match) -> u32, - ); - (*mf).skip = Some(lzma_mf_hc4_skip as unsafe extern "C" fn(*mut lzma_mf, u32) -> ()); + (*mf).find = lzma_mf_hc4_find as lzma_mf_find_function; + (*mf).skip = lzma_mf_hc4_skip as lzma_mf_skip_function; } 18 => { - (*mf).find = Some( - lzma_mf_bt2_find as unsafe extern "C" fn(*mut lzma_mf, *mut lzma_match) -> u32, - ); - (*mf).skip = Some(lzma_mf_bt2_skip as unsafe extern "C" fn(*mut lzma_mf, u32) -> ()); + (*mf).find = lzma_mf_bt2_find as lzma_mf_find_function; + (*mf).skip = lzma_mf_bt2_skip as lzma_mf_skip_function; } 19 => { - (*mf).find = Some( - lzma_mf_bt3_find as unsafe extern "C" fn(*mut lzma_mf, *mut lzma_match) -> u32, - ); - (*mf).skip = Some(lzma_mf_bt3_skip as unsafe extern "C" fn(*mut lzma_mf, u32) -> ()); + (*mf).find = lzma_mf_bt3_find as lzma_mf_find_function; + (*mf).skip = lzma_mf_bt3_skip as lzma_mf_skip_function; } 20 => { - (*mf).find = Some( - lzma_mf_bt4_find as unsafe extern "C" fn(*mut lzma_mf, *mut lzma_match) -> u32, - ); - (*mf).skip = Some(lzma_mf_bt4_skip as unsafe extern "C" fn(*mut lzma_mf, u32) -> ()); + (*mf).find = lzma_mf_bt4_find as lzma_mf_find_function; + (*mf).skip = lzma_mf_bt4_skip as lzma_mf_skip_function; } _ => return true, } @@ -309,7 +300,7 @@ unsafe fn lz_encoder_init( (*mf).write_pos as size_t, ); (*mf).action = LZMA_SYNC_FLUSH; - (*mf).skip.unwrap()(mf, (*mf).write_pos); + ((*mf).skip)(mf, (*mf).write_pos); } (*mf).action = LZMA_RUN; false @@ -326,8 +317,8 @@ pub fn lzma_lz_encoder_memusage(lz_options: *const lzma_lz_options) -> u64 { read_limit: 0, write_pos: 0, pending: 0, - find: None, - skip: None, + find: lzma_mf_find_uninitialized, + skip: lzma_mf_skip_uninitialized, hash: core::ptr::null_mut(), son: core::ptr::null_mut(), cyclic_pos: 0, @@ -347,30 +338,30 @@ pub fn lzma_lz_encoder_memusage(lz_options: *const lzma_lz_options) -> u64 { + mf.size as u64 + core::mem::size_of::() as u64 } -unsafe extern "C" fn lz_encoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { +unsafe fn lz_encoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_coder = coder_ptr as *mut lzma_coder; lzma_next_end(::core::ptr::addr_of_mut!((*coder).next), allocator); crate::alloc::internal_free((*coder).mf.son as *mut c_void, allocator); crate::alloc::internal_free((*coder).mf.hash as *mut c_void, allocator); crate::alloc::internal_free((*coder).mf.buffer as *mut c_void, allocator); - if (*coder).lz.end.is_some() { - (*coder).lz.end.unwrap()((*coder).lz.coder, allocator); + if let Some(end) = (*coder).lz.end { + end((*coder).lz.coder, allocator); } else { crate::alloc::internal_free((*coder).lz.coder, allocator); } crate::alloc::internal_free(coder as *mut c_void, allocator); } -unsafe extern "C" fn lz_encoder_update( +unsafe fn lz_encoder_update( coder_ptr: *mut c_void, allocator: *const lzma_allocator, _filters_null: *const lzma_filter, reversed_filters: *const lzma_filter, ) -> lzma_ret { let coder: *mut lzma_coder = coder_ptr as *mut lzma_coder; - if (*coder).lz.options_update.is_none() { + let Some(options_update) = (*coder).lz.options_update else { return LZMA_PROG_ERROR; - } - let ret_: lzma_ret = (*coder).lz.options_update.unwrap()((*coder).lz.coder, reversed_filters); + }; + let ret_: lzma_ret = options_update((*coder).lz.coder, reversed_filters); if ret_ != LZMA_OK { return ret_; } @@ -380,14 +371,16 @@ unsafe extern "C" fn lz_encoder_update( reversed_filters.offset(1), ) } -unsafe extern "C" fn lz_encoder_set_out_limit( +unsafe fn lz_encoder_set_out_limit( coder_ptr: *mut c_void, uncomp_size: *mut u64, out_limit: u64, ) -> lzma_ret { let coder: *mut lzma_coder = coder_ptr as *mut lzma_coder; - if (*coder).next.code.is_none() && (*coder).lz.set_out_limit.is_some() { - return (*coder).lz.set_out_limit.unwrap()((*coder).lz.coder, uncomp_size, out_limit); + if (*coder).next.code.is_none() { + if let Some(set_out_limit) = (*coder).lz.set_out_limit { + return set_out_limit((*coder).lz.coder, uncomp_size, out_limit); + } } LZMA_OPTIONS_ERROR } @@ -395,15 +388,13 @@ pub unsafe fn lzma_lz_encoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, - lz_init: Option< - unsafe extern "C" fn( - *mut lzma_lz_encoder, - *const lzma_allocator, - lzma_vli, - *const c_void, - *mut lzma_lz_options, - ) -> lzma_ret, - >, + lz_init: unsafe fn( + *mut lzma_lz_encoder, + *const lzma_allocator, + lzma_vli, + *const c_void, + *mut lzma_lz_options, + ) -> lzma_ret, ) -> lzma_ret { let mut coder: *mut lzma_coder = (*next).coder as *mut lzma_coder; if coder.is_null() { @@ -414,7 +405,7 @@ pub unsafe fn lzma_lz_encoder_init( (*next).coder = coder as *mut c_void; (*next).code = Some( lz_encode - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -426,23 +417,20 @@ pub unsafe fn lzma_lz_encoder_init( lzma_action, ) -> lzma_ret, ); - (*next).end = - Some(lz_encoder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> ()); + (*next).end = Some(lz_encoder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); (*next).update = Some( lz_encoder_update - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const lzma_filter, *const lzma_filter, ) -> lzma_ret, ); - (*next).set_out_limit = Some( - lz_encoder_set_out_limit - as unsafe extern "C" fn(*mut c_void, *mut u64, u64) -> lzma_ret, - ); + (*next).set_out_limit = + Some(lz_encoder_set_out_limit as unsafe fn(*mut c_void, *mut u64, u64) -> lzma_ret); (*coder).lz.coder = core::ptr::null_mut(); - (*coder).lz.code = None; + (*coder).lz.code = lzma_lz_encoder_code_uninitialized; (*coder).lz.end = None; (*coder).lz.options_update = None; (*coder).lz.set_out_limit = None; @@ -476,11 +464,6 @@ pub unsafe fn lzma_lz_encoder_init( preset_dict: core::ptr::null(), preset_dict_size: 0, }; - let lz_init = if let Some(lz_init) = lz_init { - lz_init - } else { - return LZMA_PROG_ERROR; - }; let ret_: lzma_ret = lz_init( ::core::ptr::addr_of_mut!((*coder).lz), allocator, @@ -491,7 +474,7 @@ pub unsafe fn lzma_lz_encoder_init( if ret_ != LZMA_OK { return ret_; } - if (*coder).lz.code.is_none() { + if (*coder).lz.coder.is_null() { return LZMA_PROG_ERROR; } if lz_encoder_prepare( diff --git a/xz/src/lz/lz_encoder_mf.rs b/xz/src/lz/lz_encoder_mf.rs index 3e1e6e84..c44d775d 100644 --- a/xz/src/lz/lz_encoder_mf.rs +++ b/xz/src/lz/lz_encoder_mf.rs @@ -6,7 +6,7 @@ pub const FIX_3_HASH_SIZE: c_uint = 1u32 << 10; pub const FIX_4_HASH_SIZE: c_uint = HASH_2_SIZE.wrapping_add(HASH_3_SIZE); #[inline] pub unsafe fn lzma_mf_find(mf: *mut lzma_mf, count_ptr: *mut u32, matches: *mut lzma_match) -> u32 { - lzma_mf_find_raw(mf, count_ptr, matches, (*mf).find.unwrap()) + lzma_mf_find_raw(mf, count_ptr, matches, (*mf).find) } #[inline(always)] @@ -14,7 +14,7 @@ pub unsafe fn lzma_mf_find_raw( mf: *mut lzma_mf, count_ptr: *mut u32, matches: *mut lzma_match, - find: unsafe extern "C" fn(*mut lzma_mf, *mut lzma_match) -> u32, + find: unsafe fn(*mut lzma_mf, *mut lzma_match) -> u32, ) -> u32 { let count: u32 = find(mf, matches) as u32; let mut len_best: u32 = 0; @@ -123,10 +123,7 @@ unsafe fn hc_find_func( } } #[inline] -pub(crate) unsafe extern "C" fn lzma_mf_hc3_find( - mf: *mut lzma_mf, - matches: *mut lzma_match, -) -> u32 { +pub(crate) unsafe fn lzma_mf_hc3_find(mf: *mut lzma_mf, matches: *mut lzma_match) -> u32 { let mut len_limit: u32 = mf_avail(mf); if (*mf).nice_len <= len_limit { len_limit = (*mf).nice_len; @@ -177,7 +174,7 @@ pub(crate) unsafe extern "C" fn lzma_mf_hc3_find( matches_count } #[inline] -pub(crate) unsafe extern "C" fn lzma_mf_hc3_skip(mf: *mut lzma_mf, mut amount: u32) { +pub(crate) unsafe fn lzma_mf_hc3_skip(mf: *mut lzma_mf, mut amount: u32) { loop { if mf_avail(mf) < 3 { move_pending(mf); @@ -204,10 +201,7 @@ pub(crate) unsafe extern "C" fn lzma_mf_hc3_skip(mf: *mut lzma_mf, mut amount: u } } #[inline] -pub(crate) unsafe extern "C" fn lzma_mf_hc4_find( - mf: *mut lzma_mf, - matches: *mut lzma_match, -) -> u32 { +pub(crate) unsafe fn lzma_mf_hc4_find(mf: *mut lzma_mf, matches: *mut lzma_match) -> u32 { let mut len_limit: u32 = mf_avail(mf); if (*mf).nice_len <= len_limit { len_limit = (*mf).nice_len; @@ -279,7 +273,7 @@ pub(crate) unsafe extern "C" fn lzma_mf_hc4_find( matches_count } #[inline] -pub(crate) unsafe extern "C" fn lzma_mf_hc4_skip(mf: *mut lzma_mf, mut amount: u32) { +pub(crate) unsafe fn lzma_mf_hc4_skip(mf: *mut lzma_mf, mut amount: u32) { loop { if mf_avail(mf) < 4 { move_pending(mf); @@ -422,10 +416,7 @@ unsafe fn bt_skip_func( } } #[inline] -pub(crate) unsafe extern "C" fn lzma_mf_bt2_find( - mf: *mut lzma_mf, - matches: *mut lzma_match, -) -> u32 { +pub(crate) unsafe fn lzma_mf_bt2_find(mf: *mut lzma_mf, matches: *mut lzma_match) -> u32 { let mut len_limit: u32 = mf_avail(mf); if (*mf).nice_len <= len_limit { len_limit = (*mf).nice_len; @@ -456,39 +447,30 @@ pub(crate) unsafe extern "C" fn lzma_mf_bt2_find( matches_count } #[inline] -pub(crate) unsafe extern "C" fn lzma_mf_bt2_skip(mf: *mut lzma_mf, mut amount: u32) { - let mut current_block_8: u64; +pub(crate) unsafe fn lzma_mf_bt2_skip(mf: *mut lzma_mf, mut amount: u32) { loop { let mut len_limit: u32 = mf_avail(mf); if (*mf).nice_len <= len_limit { len_limit = (*mf).nice_len; - current_block_8 = 11875828834189669668; } else if len_limit < 2 || true && (*mf).action == LZMA_SYNC_FLUSH { move_pending(mf); - current_block_8 = 18088007599891946824; } else { - current_block_8 = 11875828834189669668; - } - match current_block_8 { - 11875828834189669668 => { - let cur: *const u8 = mf_ptr(mf); - let pos: u32 = (*mf).read_pos.wrapping_add((*mf).offset); - let hash_value: u32 = *cur as u32 | (*cur.offset(1) as u32) << 8; - let cur_match: u32 = *(*mf).hash.offset(hash_value as isize); - *(*mf).hash.offset(hash_value as isize) = pos; - bt_skip_func( - len_limit, - pos, - cur, - cur_match, - (*mf).depth, - (*mf).son, - (*mf).cyclic_pos, - (*mf).cyclic_size, - ); - move_pos(mf); - } - _ => {} + let cur: *const u8 = mf_ptr(mf); + let pos: u32 = (*mf).read_pos.wrapping_add((*mf).offset); + let hash_value: u32 = *cur as u32 | (*cur.offset(1) as u32) << 8; + let cur_match: u32 = *(*mf).hash.offset(hash_value as isize); + *(*mf).hash.offset(hash_value as isize) = pos; + bt_skip_func( + len_limit, + pos, + cur, + cur_match, + (*mf).depth, + (*mf).son, + (*mf).cyclic_pos, + (*mf).cyclic_size, + ); + move_pos(mf); } amount -= 1; if amount == 0 { @@ -497,10 +479,7 @@ pub(crate) unsafe extern "C" fn lzma_mf_bt2_skip(mf: *mut lzma_mf, mut amount: u } } #[inline] -pub(crate) unsafe extern "C" fn lzma_mf_bt3_find( - mf: *mut lzma_mf, - matches: *mut lzma_match, -) -> u32 { +pub(crate) unsafe fn lzma_mf_bt3_find(mf: *mut lzma_mf, matches: *mut lzma_match) -> u32 { let mut len_limit: u32 = mf_avail(mf); if (*mf).nice_len <= len_limit { len_limit = (*mf).nice_len; @@ -560,46 +539,37 @@ pub(crate) unsafe extern "C" fn lzma_mf_bt3_find( matches_count } #[inline] -pub(crate) unsafe extern "C" fn lzma_mf_bt3_skip(mf: *mut lzma_mf, mut amount: u32) { - let mut current_block_9: u64; +pub(crate) unsafe fn lzma_mf_bt3_skip(mf: *mut lzma_mf, mut amount: u32) { loop { let mut len_limit: u32 = mf_avail(mf); if (*mf).nice_len <= len_limit { len_limit = (*mf).nice_len; - current_block_9 = 11875828834189669668; } else if len_limit < 3 || true && (*mf).action == LZMA_SYNC_FLUSH { move_pending(mf); - current_block_9 = 18088007599891946824; } else { - current_block_9 = 11875828834189669668; - } - match current_block_9 { - 11875828834189669668 => { - let cur: *const u8 = mf_ptr(mf); - let pos: u32 = (*mf).read_pos.wrapping_add((*mf).offset); - let temp: u32 = lzma_crc32_table[0][*cur as usize] ^ *cur.offset(1) as u32; - let hash_2_value: u32 = temp & HASH_2_MASK as u32; - let hash_value: u32 = (temp ^ (*cur.offset(2) as u32) << 8) & (*mf).hash_mask; - let cur_match: u32 = *(*mf) - .hash - .offset((FIX_3_HASH_SIZE as u32 + hash_value) as isize); - *(*mf).hash.offset(hash_2_value as isize) = pos; - *(*mf) - .hash - .offset((FIX_3_HASH_SIZE as u32 + hash_value) as isize) = pos; - bt_skip_func( - len_limit, - pos, - cur, - cur_match, - (*mf).depth, - (*mf).son, - (*mf).cyclic_pos, - (*mf).cyclic_size, - ); - move_pos(mf); - } - _ => {} + let cur: *const u8 = mf_ptr(mf); + let pos: u32 = (*mf).read_pos.wrapping_add((*mf).offset); + let temp: u32 = lzma_crc32_table[0][*cur as usize] ^ *cur.offset(1) as u32; + let hash_2_value: u32 = temp & HASH_2_MASK as u32; + let hash_value: u32 = (temp ^ (*cur.offset(2) as u32) << 8) & (*mf).hash_mask; + let cur_match: u32 = *(*mf) + .hash + .offset((FIX_3_HASH_SIZE as u32 + hash_value) as isize); + *(*mf).hash.offset(hash_2_value as isize) = pos; + *(*mf) + .hash + .offset((FIX_3_HASH_SIZE as u32 + hash_value) as isize) = pos; + bt_skip_func( + len_limit, + pos, + cur, + cur_match, + (*mf).depth, + (*mf).son, + (*mf).cyclic_pos, + (*mf).cyclic_size, + ); + move_pos(mf); } amount -= 1; if amount == 0 { @@ -608,10 +578,7 @@ pub(crate) unsafe extern "C" fn lzma_mf_bt3_skip(mf: *mut lzma_mf, mut amount: u } } #[inline] -pub(crate) unsafe extern "C" fn lzma_mf_bt4_find( - mf: *mut lzma_mf, - matches: *mut lzma_match, -) -> u32 { +pub(crate) unsafe fn lzma_mf_bt4_find(mf: *mut lzma_mf, matches: *mut lzma_match) -> u32 { let mut len_limit: u32 = mf_avail(mf); if (*mf).nice_len <= len_limit { len_limit = (*mf).nice_len; @@ -692,7 +659,7 @@ pub(crate) unsafe extern "C" fn lzma_mf_bt4_find( matches_count } #[inline] -pub(crate) unsafe extern "C" fn lzma_mf_bt4_skip(mf: *mut lzma_mf, mut amount: u32) { +pub(crate) unsafe fn lzma_mf_bt4_skip(mf: *mut lzma_mf, mut amount: u32) { debug_assert!(amount > 0); loop { let mut len_limit: u32 = mf_avail(mf); diff --git a/xz/src/lzma/fastpos_table.rs b/xz/src/lzma/fastpos_table.rs index db50e825..2f71a484 100644 --- a/xz/src/lzma/fastpos_table.rs +++ b/xz/src/lzma/fastpos_table.rs @@ -1,4 +1,4 @@ -pub static mut lzma_fastpos: [u8; 8192] = [ +pub static lzma_fastpos: [u8; 8192] = [ 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, diff --git a/xz/src/lzma/lzma2_decoder.rs b/xz/src/lzma/lzma2_decoder.rs index 50f2ac80..ed016b0d 100644 --- a/xz/src/lzma/lzma2_decoder.rs +++ b/xz/src/lzma/lzma2_decoder.rs @@ -24,7 +24,7 @@ pub const SEQ_UNCOMPRESSED_1: sequence = 1; pub const SEQ_CONTROL: sequence = 0; pub const LZMA_LZ_DECODER_INIT: lzma_lz_decoder = lzma_lz_decoder { coder: core::ptr::null_mut(), - code: None, + code: lzma_lz_decoder_code_uninitialized, reset: None, set_uncompressed: None, end: None, @@ -32,7 +32,7 @@ pub const LZMA_LZ_DECODER_INIT: lzma_lz_decoder = lzma_lz_decoder { #[inline] unsafe fn dict_write( dict: *mut lzma_dict, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, mut in_size: size_t, left: *mut size_t, @@ -41,7 +41,7 @@ unsafe fn dict_write( in_size = (*in_pos).wrapping_add(*left); } *left = (*left).wrapping_sub(lzma_bufcpy( - in_0, + input, in_pos, in_size, (*dict).buf, @@ -56,10 +56,10 @@ unsafe fn dict_write( unsafe fn dict_reset(dict: *mut lzma_dict) { (*dict).need_reset = true; } -unsafe extern "C" fn lzma2_decode( +unsafe fn lzma2_decode( coder_ptr: *mut c_void, dict: *mut lzma_dict, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, ) -> lzma_ret { @@ -67,7 +67,7 @@ unsafe extern "C" fn lzma2_decode( while *in_pos < in_size || (*coder).sequence == SEQ_LZMA { match (*coder).sequence { 0 => { - let control: u32 = *in_0.offset(*in_pos as isize) as u32; + let control: u32 = *input.offset(*in_pos as isize) as u32; *in_pos = (*in_pos).wrapping_add(1); if control == 0 { return LZMA_STREAM_END; @@ -89,7 +89,9 @@ unsafe extern "C" fn lzma2_decode( } else { (*coder).next_sequence = SEQ_LZMA; if control >= 0xa0 { - (*coder).lzma.reset.unwrap()( + debug_assert!((*coder).lzma.reset.is_some()); + let reset = (*coder).lzma.reset.unwrap_unchecked(); + reset( (*coder).lzma.coder, ::core::ptr::addr_of_mut!((*coder).options) as *const c_void, ); @@ -111,41 +113,46 @@ unsafe extern "C" fn lzma2_decode( 1 => { (*coder).uncompressed_size = (*coder) .uncompressed_size - .wrapping_add(((*in_0.offset(*in_pos as isize) as u32) << 8) as size_t); + .wrapping_add(((*input.offset(*in_pos as isize) as u32) << 8) as size_t); *in_pos += 1; (*coder).sequence = SEQ_UNCOMPRESSED_2; } 2 => { (*coder).uncompressed_size = (*coder).uncompressed_size.wrapping_add( - u32::from(*in_0.offset(*in_pos as isize)).wrapping_add(1) as size_t, + u32::from(*input.offset(*in_pos as isize)).wrapping_add(1) as size_t, ); *in_pos += 1; (*coder).sequence = SEQ_COMPRESSED_0; - (*coder).lzma.set_uncompressed.unwrap()( + debug_assert!((*coder).lzma.set_uncompressed.is_some()); + let set_uncompressed = (*coder).lzma.set_uncompressed.unwrap_unchecked(); + set_uncompressed( (*coder).lzma.coder, (*coder).uncompressed_size as lzma_vli, false, ); } 3 => { - (*coder).compressed_size = ((*in_0.offset(*in_pos as isize) as u32) << 8) as size_t; + (*coder).compressed_size = + ((*input.offset(*in_pos as isize) as u32) << 8) as size_t; *in_pos += 1; (*coder).sequence = SEQ_COMPRESSED_1; } 4 => { (*coder).compressed_size = (*coder).compressed_size.wrapping_add( - u32::from(*in_0.offset(*in_pos as isize)).wrapping_add(1) as size_t, + u32::from(*input.offset(*in_pos as isize)).wrapping_add(1) as size_t, ); *in_pos += 1; (*coder).sequence = (*coder).next_sequence as sequence; } 5 => { - let prop_byte = *in_0.offset(*in_pos as isize); + let prop_byte = *input.offset(*in_pos as isize); *in_pos += 1; if lzma_lzma_lclppb_decode(::core::ptr::addr_of_mut!((*coder).options), prop_byte) { return LZMA_DATA_ERROR; } - (*coder).lzma.reset.unwrap()( + debug_assert!((*coder).lzma.reset.is_some()); + let reset = (*coder).lzma.reset.unwrap_unchecked(); + reset( (*coder).lzma.coder, ::core::ptr::addr_of_mut!((*coder).options) as *const c_void, ); @@ -154,7 +161,7 @@ unsafe extern "C" fn lzma2_decode( 6 => { let in_start: size_t = *in_pos; let ret: lzma_ret = - (*coder).lzma.code.unwrap()((*coder).lzma.coder, dict, in_0, in_pos, in_size); + ((*coder).lzma.code)((*coder).lzma.coder, dict, input, in_pos, in_size); let in_used: size_t = (*in_pos).wrapping_sub(in_start); if in_used > (*coder).compressed_size { return LZMA_DATA_ERROR; @@ -171,7 +178,7 @@ unsafe extern "C" fn lzma2_decode( 7 => { dict_write( dict, - in_0, + input, in_pos, in_size, ::core::ptr::addr_of_mut!((*coder).compressed_size), @@ -186,12 +193,12 @@ unsafe extern "C" fn lzma2_decode( } LZMA_OK } -unsafe extern "C" fn lzma2_decoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { +unsafe fn lzma2_decoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_lzma2_coder = coder_ptr as *mut lzma_lzma2_coder; crate::alloc::internal_free((*coder).lzma.coder, allocator); crate::alloc::internal_free(coder as *mut c_void, allocator); } -unsafe extern "C" fn lzma2_decoder_init( +unsafe fn lzma2_decoder_init( lz: *mut lzma_lz_decoder, allocator: *const lzma_allocator, _id: lzma_vli, @@ -205,19 +212,8 @@ unsafe extern "C" fn lzma2_decoder_init( return LZMA_MEM_ERROR; } (*lz).coder = coder as *mut c_void; - (*lz).code = Some( - lzma2_decode - as unsafe extern "C" fn( - *mut c_void, - *mut lzma_dict, - *const u8, - *mut size_t, - size_t, - ) -> lzma_ret, - ); - (*lz).end = Some( - lzma2_decoder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> (), - ); + (*lz).code = lzma2_decode as lzma_lz_decoder_code_function; + (*lz).end = Some(lzma2_decoder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); (*coder).lzma = LZMA_LZ_DECODER_INIT; } let options: *const lzma_options_lzma = opt as *const lzma_options_lzma; @@ -232,7 +228,7 @@ unsafe extern "C" fn lzma2_decoder_init( lz_options, ) } -pub(crate) unsafe extern "C" fn lzma_lzma2_decoder_init( +pub(crate) unsafe fn lzma_lzma2_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, @@ -241,23 +237,21 @@ pub(crate) unsafe extern "C" fn lzma_lzma2_decoder_init( next, allocator, filters, - Some( - lzma2_decoder_init - as unsafe extern "C" fn( - *mut lzma_lz_decoder, - *const lzma_allocator, - lzma_vli, - *const c_void, - *mut lzma_lz_options, - ) -> lzma_ret, - ), + lzma2_decoder_init + as unsafe fn( + *mut lzma_lz_decoder, + *const lzma_allocator, + lzma_vli, + *const c_void, + *mut lzma_lz_options, + ) -> lzma_ret, ) } -pub(crate) extern "C" fn lzma_lzma2_decoder_memusage(options: *const c_void) -> u64 { +pub(crate) unsafe fn lzma_lzma2_decoder_memusage(options: *const c_void) -> u64 { (core::mem::size_of::() as u64) .wrapping_add(lzma_lzma_decoder_memusage_nocheck(options)) } -pub(crate) unsafe extern "C" fn lzma_lzma2_props_decode( +pub(crate) unsafe fn lzma_lzma2_props_decode( options: *mut *mut c_void, allocator: *const lzma_allocator, props: *const u8, diff --git a/xz/src/lzma/lzma2_encoder.rs b/xz/src/lzma/lzma2_encoder.rs index 54b0ac3f..104cbe17 100644 --- a/xz/src/lzma/lzma2_encoder.rs +++ b/xz/src/lzma/lzma2_encoder.rs @@ -102,7 +102,7 @@ unsafe fn lzma2_header_uncompressed(coder: *mut lzma_lzma2_coder) { (*coder).buf[2] = ((*coder).uncompressed_size - 1 & 0xff) as u8; (*coder).buf_pos = 0; } -unsafe extern "C" fn lzma2_encode( +unsafe fn lzma2_encode( coder_ptr: *mut c_void, mf: *mut lzma_mf, out: *mut u8, @@ -111,7 +111,6 @@ unsafe extern "C" fn lzma2_encode( ) -> lzma_ret { let coder: *mut lzma_lzma2_coder = coder_ptr as *mut lzma_lzma2_coder; while *out_pos < out_size { - let mut current_block_45: u64; match (*coder).sequence { 0 => { if mf_unencoded(mf) == 0 { @@ -137,14 +136,8 @@ unsafe extern "C" fn lzma2_encode( (*coder).uncompressed_size = 0; (*coder).compressed_size = 0; (*coder).sequence = SEQ_LZMA_ENCODE; - current_block_45 = 2979737022853876585; - } - 1 => { - current_block_45 = 2979737022853876585; - } - 2 => { - current_block_45 = 13410404938545238636; } + 1 | 2 => {} 3 => { lzma_bufcpy( ::core::ptr::addr_of_mut!((*coder).buf) as *mut u8, @@ -158,95 +151,81 @@ unsafe extern "C" fn lzma2_encode( return LZMA_OK; } (*coder).sequence = SEQ_UNCOMPRESSED_COPY; - current_block_45 = 10903800704467975402; } - 4 => { - current_block_45 = 10903800704467975402; - } - _ => { - current_block_45 = 11743904203796629665; + 4 | 5 => {} + _ => return LZMA_PROG_ERROR, + } + if (*coder).sequence == SEQ_UNCOMPRESSED_COPY { + mf_read( + mf, + out, + out_pos, + out_size, + ::core::ptr::addr_of_mut!((*coder).uncompressed_size), + ); + if (*coder).uncompressed_size != 0 { + return LZMA_OK; } + (*coder).sequence = SEQ_INIT; + continue; } - match current_block_45 { - 10903800704467975402 => { - mf_read( - mf, - out, - out_pos, - out_size, - ::core::ptr::addr_of_mut!((*coder).uncompressed_size), - ); - if (*coder).uncompressed_size != 0 { - return LZMA_OK; - } - (*coder).sequence = SEQ_INIT; - current_block_45 = 11743904203796629665; + if (*coder).sequence == SEQ_LZMA_ENCODE { + let left: u32 = + ((LZMA2_UNCOMPRESSED_MAX as size_t) - (*coder).uncompressed_size) as u32; + let limit: u32 = if left < (*mf).match_len_max { + 0 + } else { + (*mf).read_pos - (*mf).read_ahead + left - (*mf).match_len_max + }; + let read_start: u32 = (*mf).read_pos - (*mf).read_ahead; + let ret: lzma_ret = lzma_lzma_encode( + (*coder).lzma as *mut lzma_lzma1_encoder, + mf, + (::core::ptr::addr_of_mut!((*coder).buf) as *mut u8) + .offset(LZMA2_HEADER_MAX as isize), + ::core::ptr::addr_of_mut!((*coder).compressed_size), + LZMA2_CHUNK_MAX as size_t, + limit, + ); + (*coder).uncompressed_size += + ((*mf).read_pos - (*mf).read_ahead - read_start) as size_t; + if ret != LZMA_STREAM_END { + return LZMA_OK; } - 2979737022853876585 => { - let left: u32 = - ((LZMA2_UNCOMPRESSED_MAX as size_t) - (*coder).uncompressed_size) as u32; - let mut limit: u32 = 0; - if left < (*mf).match_len_max { - limit = 0; - } else { - limit = (*mf).read_pos - (*mf).read_ahead + left - (*mf).match_len_max; - } - let read_start: u32 = (*mf).read_pos - (*mf).read_ahead; - let ret: lzma_ret = lzma_lzma_encode( - (*coder).lzma as *mut lzma_lzma1_encoder, - mf, - (::core::ptr::addr_of_mut!((*coder).buf) as *mut u8) - .offset(LZMA2_HEADER_MAX as isize), - ::core::ptr::addr_of_mut!((*coder).compressed_size), - LZMA2_CHUNK_MAX as size_t, - limit, - ); - (*coder).uncompressed_size += - ((*mf).read_pos - (*mf).read_ahead - read_start) as size_t; - if ret != LZMA_STREAM_END { - return LZMA_OK; - } - if (*coder).compressed_size >= (*coder).uncompressed_size { - (*coder).uncompressed_size += (*mf).read_ahead as size_t; - (*mf).read_ahead = 0; - lzma2_header_uncompressed(coder); - (*coder).need_state_reset = true; - (*coder).sequence = SEQ_UNCOMPRESSED_HEADER; - current_block_45 = 11743904203796629665; - } else { - lzma2_header_lzma(coder); - (*coder).sequence = SEQ_LZMA_COPY; - current_block_45 = 13410404938545238636; - } + if (*coder).compressed_size >= (*coder).uncompressed_size { + (*coder).uncompressed_size += (*mf).read_ahead as size_t; + (*mf).read_ahead = 0; + lzma2_header_uncompressed(coder); + (*coder).need_state_reset = true; + (*coder).sequence = SEQ_UNCOMPRESSED_HEADER; + continue; } - _ => {} + lzma2_header_lzma(coder); + (*coder).sequence = SEQ_LZMA_COPY; } - match current_block_45 { - 13410404938545238636 => { - lzma_bufcpy( - ::core::ptr::addr_of_mut!((*coder).buf) as *mut u8, - ::core::ptr::addr_of_mut!((*coder).buf_pos), - (*coder).compressed_size, - out, - out_pos, - out_size, - ); - if (*coder).buf_pos != (*coder).compressed_size { - return LZMA_OK; - } - (*coder).sequence = SEQ_INIT; + if (*coder).sequence == SEQ_LZMA_COPY { + lzma_bufcpy( + ::core::ptr::addr_of_mut!((*coder).buf) as *mut u8, + ::core::ptr::addr_of_mut!((*coder).buf_pos), + (*coder).compressed_size, + out, + out_pos, + out_size, + ); + if (*coder).buf_pos != (*coder).compressed_size { + return LZMA_OK; } - _ => {} + (*coder).sequence = SEQ_INIT; } } LZMA_OK } -unsafe extern "C" fn lzma2_encoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { +unsafe fn lzma2_encoder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_lzma2_coder = coder_ptr as *mut lzma_lzma2_coder; crate::alloc::internal_free((*coder).lzma, allocator); crate::alloc::internal_free(coder as *mut c_void, allocator); } -unsafe extern "C" fn lzma2_encoder_options_update( +unsafe fn lzma2_encoder_options_update( coder_ptr: *mut c_void, filter: *const lzma_filter, ) -> lzma_ret { @@ -274,7 +253,7 @@ unsafe extern "C" fn lzma2_encoder_options_update( } LZMA_OK } -unsafe extern "C" fn lzma2_encoder_init( +unsafe fn lzma2_encoder_init( lz: *mut lzma_lz_encoder, allocator: *const lzma_allocator, _id: lzma_vli, @@ -291,22 +270,10 @@ unsafe extern "C" fn lzma2_encoder_init( return LZMA_MEM_ERROR; } (*lz).coder = coder as *mut c_void; - (*lz).code = Some( - lzma2_encode - as unsafe extern "C" fn( - *mut c_void, - *mut lzma_mf, - *mut u8, - *mut size_t, - size_t, - ) -> lzma_ret, - ); - (*lz).end = Some( - lzma2_encoder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> (), - ); + (*lz).code = lzma2_encode as lzma_lz_encoder_code_function; + (*lz).end = Some(lzma2_encoder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); (*lz).options_update = Some( - lzma2_encoder_options_update - as unsafe extern "C" fn(*mut c_void, *const lzma_filter) -> lzma_ret, + lzma2_encoder_options_update as unsafe fn(*mut c_void, *const lzma_filter) -> lzma_ret, ); (*coder).lzma = core::ptr::null_mut(); } @@ -331,7 +298,7 @@ unsafe extern "C" fn lzma2_encoder_init( } LZMA_OK } -pub(crate) unsafe extern "C" fn lzma_lzma2_encoder_init( +pub(crate) unsafe fn lzma_lzma2_encoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, @@ -340,29 +307,24 @@ pub(crate) unsafe extern "C" fn lzma_lzma2_encoder_init( next, allocator, filters, - Some( - lzma2_encoder_init - as unsafe extern "C" fn( - *mut lzma_lz_encoder, - *const lzma_allocator, - lzma_vli, - *const c_void, - *mut lzma_lz_options, - ) -> lzma_ret, - ), + lzma2_encoder_init + as unsafe fn( + *mut lzma_lz_encoder, + *const lzma_allocator, + lzma_vli, + *const c_void, + *mut lzma_lz_options, + ) -> lzma_ret, ) } -pub(crate) extern "C" fn lzma_lzma2_encoder_memusage(options: *const c_void) -> u64 { +pub(crate) unsafe fn lzma_lzma2_encoder_memusage(options: *const c_void) -> u64 { let lzma_mem: u64 = lzma_lzma_encoder_memusage(options) as u64; if lzma_mem == UINT64_MAX { return UINT64_MAX; } (core::mem::size_of::() as u64) + lzma_mem } -pub(crate) unsafe extern "C" fn lzma_lzma2_props_encode( - options: *const c_void, - out: *mut u8, -) -> lzma_ret { +pub(crate) unsafe fn lzma_lzma2_props_encode(options: *const c_void, out: *mut u8) -> lzma_ret { if options.is_null() { return LZMA_PROG_ERROR; } @@ -385,7 +347,7 @@ pub(crate) unsafe extern "C" fn lzma_lzma2_props_encode( } LZMA_OK } -pub(crate) unsafe extern "C" fn lzma_lzma2_block_size(options: *const c_void) -> u64 { +pub(crate) unsafe fn lzma_lzma2_block_size(options: *const c_void) -> u64 { let opt: *const lzma_options_lzma = options as *const lzma_options_lzma; if (*opt).dict_size < LZMA_DICT_SIZE_MIN as u32 || (*opt).dict_size > (1u32 << 30) + (1 << 29) { return UINT64_MAX; diff --git a/xz/src/lzma/lzma_decoder.rs b/xz/src/lzma/lzma_decoder.rs index 58b72a4c..d826b4a8 100644 --- a/xz/src/lzma/lzma_decoder.rs +++ b/xz/src/lzma/lzma_decoder.rs @@ -57,6 +57,36 @@ pub const SEQ_LITERAL_MATCHED: lzma_decoder_seq = 3; pub const SEQ_LITERAL: lzma_decoder_seq = 2; pub const SEQ_IS_MATCH: lzma_decoder_seq = 1; pub const SEQ_NORMALIZE: lzma_decoder_seq = 0; +type DecoderBlockState = u8; +const BLOCK_NORMALIZE_OR_IS_MATCH: DecoderBlockState = 0; +const BLOCK_LITERAL: DecoderBlockState = 1; +const BLOCK_LITERAL_MATCHED: DecoderBlockState = 2; +const BLOCK_LITERAL_WRITE: DecoderBlockState = 3; +const BLOCK_IS_REP: DecoderBlockState = 4; +const BLOCK_MATCH_LEN_CHOICE: DecoderBlockState = 5; +const BLOCK_MATCH_LEN_CHOICE2: DecoderBlockState = 6; +const BLOCK_MATCH_LEN_BITTREE: DecoderBlockState = 7; +const BLOCK_DIST_SLOT: DecoderBlockState = 8; +const BLOCK_DIST_MODEL: DecoderBlockState = 9; +const BLOCK_DIRECT: DecoderBlockState = 10; +const BLOCK_ALIGN: DecoderBlockState = 11; +const BLOCK_EOPM: DecoderBlockState = 12; +const BLOCK_IS_REP0: DecoderBlockState = 13; +const BLOCK_IS_REP0_LONG: DecoderBlockState = 14; +const BLOCK_SHORTREP: DecoderBlockState = 15; +const BLOCK_IS_REP1: DecoderBlockState = 16; +const BLOCK_IS_REP2: DecoderBlockState = 17; +const BLOCK_REP_LEN_CHOICE: DecoderBlockState = 18; +const BLOCK_REP_LEN_CHOICE2: DecoderBlockState = 19; +const BLOCK_REP_LEN_BITTREE: DecoderBlockState = 20; +const BLOCK_COPY: DecoderBlockState = 21; +const BLOCK_RETURN: DecoderBlockState = 22; +const BLOCK_LEN_BITTREE_INIT: DecoderBlockState = 23; +const BLOCK_REP_LEN_PREPARE: DecoderBlockState = 24; +const BLOCK_DIST_SLOT_INIT: DecoderBlockState = 25; +const BLOCK_EOPM_IF_VALID: DecoderBlockState = 26; +const BLOCK_VALIDATE_DISTANCE: DecoderBlockState = 27; +const BLOCK_MAIN_LOOP: DecoderBlockState = 28; #[derive(Copy, Clone)] #[repr(C)] pub struct lzma_range_decoder { @@ -151,7 +181,7 @@ pub const DIST_SLOTS: u32 = 1 << DIST_SLOT_BITS; #[inline] unsafe fn rc_read_init( rc: *mut lzma_range_decoder, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, ) -> lzma_ret { @@ -159,66 +189,336 @@ unsafe fn rc_read_init( if *in_pos == in_size { return LZMA_OK; } - if (*rc).init_bytes_left == 5 && *in_0.offset(*in_pos as isize) != 0 { + if (*rc).init_bytes_left == 5 && *input.offset(*in_pos as isize) != 0 { return LZMA_DATA_ERROR; } - (*rc).code = (*rc).code << 8 | *in_0.offset(*in_pos as isize) as u32; + (*rc).code = (*rc).code << 8 | *input.offset(*in_pos as isize) as u32; *in_pos = (*in_pos).wrapping_add(1); (*rc).init_bytes_left = (*rc).init_bytes_left.wrapping_sub(1); } LZMA_STREAM_END } #[inline(never)] -fn resume_block_for_sequence(sequence: lzma_decoder_seq) -> u64 { +fn resume_block_for_sequence(sequence: lzma_decoder_seq) -> DecoderBlockState { match sequence { - 0 | 1 => 5979571030476392895, - 2 => 13844743919235296534, - 3 => 18125716024132132232, - 4 => 10535798129821001304, - 5 => 3469750012682708893, - 6 => 1138292997408115650, - 7 => 13912927785247575907, - 8 => 592696588731961849, - 9 => 4174862988780014241, - 10 => 617447976488552541, - 11 => 15418612220330286504, - 12 => 10510472849010538284, - 13 => 7073645523065812117, - 14 => 4420799852307653083, - 16 => 1698084742280242340, - 15 => 5341942013764523046, - 17 => 11808118301119257848, - 18 => 3996983927318648760, - 19 => 12043352250568755004, - 20 => 6834592846991627977, - 21 => 2467942631393454738, - 22 => 17340485688450593529, - _ => 4609795085482299213, + 0 | 1 => BLOCK_NORMALIZE_OR_IS_MATCH, + 2 => BLOCK_LITERAL, + 3 => BLOCK_LITERAL_MATCHED, + 4 => BLOCK_LITERAL_WRITE, + 5 => BLOCK_IS_REP, + 6 => BLOCK_MATCH_LEN_CHOICE, + 7 => BLOCK_MATCH_LEN_CHOICE2, + 8 => BLOCK_MATCH_LEN_BITTREE, + 9 => BLOCK_DIST_SLOT, + 10 => BLOCK_DIST_MODEL, + 11 => BLOCK_DIRECT, + 12 => BLOCK_ALIGN, + 13 => BLOCK_EOPM, + 14 => BLOCK_IS_REP0, + 16 => BLOCK_IS_REP0_LONG, + 15 => BLOCK_SHORTREP, + 17 => BLOCK_IS_REP1, + 18 => BLOCK_IS_REP2, + 19 => BLOCK_REP_LEN_CHOICE, + 20 => BLOCK_REP_LEN_CHOICE2, + 21 => BLOCK_REP_LEN_BITTREE, + 22 => BLOCK_COPY, + _ => BLOCK_RETURN, } } -unsafe extern "C" fn lzma_decode( +#[inline(always)] +unsafe fn decoder_is_match_row(coder: *mut lzma_lzma1_decoder, state: u32) -> *mut probability { + debug_assert!((state as usize) < STATES as usize); + ::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*coder).is_match) as *mut [probability; 16]) + .add(state as usize) + ) as *mut probability +} +#[inline(always)] +unsafe fn decoder_is_rep_prob(coder: *mut lzma_lzma1_decoder, state: u32) -> *mut probability { + debug_assert!((state as usize) < STATES as usize); + (::core::ptr::addr_of_mut!((*coder).is_rep) as *mut probability).add(state as usize) +} +#[inline(always)] +unsafe fn decoder_is_rep0_prob(coder: *mut lzma_lzma1_decoder, state: u32) -> *mut probability { + debug_assert!((state as usize) < STATES as usize); + (::core::ptr::addr_of_mut!((*coder).is_rep0) as *mut probability).add(state as usize) +} +#[inline(always)] +unsafe fn decoder_is_rep1_prob(coder: *mut lzma_lzma1_decoder, state: u32) -> *mut probability { + debug_assert!((state as usize) < STATES as usize); + (::core::ptr::addr_of_mut!((*coder).is_rep1) as *mut probability).add(state as usize) +} +#[inline(always)] +unsafe fn decoder_is_rep2_prob(coder: *mut lzma_lzma1_decoder, state: u32) -> *mut probability { + debug_assert!((state as usize) < STATES as usize); + (::core::ptr::addr_of_mut!((*coder).is_rep2) as *mut probability).add(state as usize) +} +#[inline(always)] +unsafe fn decoder_is_rep0_long_row(coder: *mut lzma_lzma1_decoder, state: u32) -> *mut probability { + debug_assert!((state as usize) < STATES as usize); + ::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*coder).is_rep0_long) as *mut [probability; 16]) + .add(state as usize) + ) as *mut probability +} +#[inline(always)] +unsafe fn decoder_dist_slot_row( + coder: *mut lzma_lzma1_decoder, + dist_state: u32, +) -> *mut probability { + debug_assert!((dist_state as usize) < DIST_STATES as usize); + ::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*coder).dist_slot) as *mut [probability; 64]) + .add(dist_state as usize) + ) as *mut probability +} +#[inline(always)] +unsafe fn decoder_pos_align_prob(coder: *mut lzma_lzma1_decoder, index: u32) -> *mut probability { + debug_assert!((index as usize) < ALIGN_SIZE as usize); + (::core::ptr::addr_of_mut!((*coder).pos_align) as *mut probability).add(index as usize) +} +#[inline(always)] +unsafe fn length_low_row( + len_decoder: *mut lzma_length_decoder, + pos_state: u32, +) -> *mut probability { + debug_assert!((pos_state as usize) < (1 << LZMA_PB_MAX) as usize); + ::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*len_decoder).low) as *mut [probability; 8]) + .add(pos_state as usize) + ) as *mut probability +} +#[inline(always)] +unsafe fn length_mid_row( + len_decoder: *mut lzma_length_decoder, + pos_state: u32, +) -> *mut probability { + debug_assert!((pos_state as usize) < (1 << LZMA_PB_MAX) as usize); + ::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*len_decoder).mid) as *mut [probability; 8]) + .add(pos_state as usize) + ) as *mut probability +} +#[inline(always)] +unsafe fn length_high_probs(len_decoder: *mut lzma_length_decoder) -> *mut probability { + ::core::ptr::addr_of_mut!((*len_decoder).high) as *mut probability +} +#[inline(always)] +unsafe fn prob_update_0(prob: *mut probability) { + *prob = (*prob as u32) + .wrapping_add(RC_BIT_MODEL_TOTAL.wrapping_sub(*prob as u32) >> RC_MOVE_BITS) + as probability; +} +#[inline(always)] +unsafe fn prob_update_1(prob: *mut probability) { + *prob = *prob - (*prob >> RC_MOVE_BITS); +} + +macro_rules! rc_normalize { + ($rc:ident, $rc_in_ptr:ident) => { + if $rc.range < RC_TOP_VALUE as u32 { + $rc.range <<= RC_SHIFT_BITS; + $rc.code = $rc.code << RC_SHIFT_BITS | *$rc_in_ptr as u32; + $rc_in_ptr = $rc_in_ptr.offset(1); + } + }; +} + +macro_rules! rc_bittree_step { + ($rc:ident, $rc_bound:ident, $prob:expr, $symbol:ident) => {{ + let prob = $prob; + $rc_bound = ($rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul(*prob as u32); + if $rc.code < $rc_bound { + $rc.range = $rc_bound; + prob_update_0(prob); + $symbol <<= 1; + } else { + $rc.range = $rc.range.wrapping_sub($rc_bound); + $rc.code = $rc.code.wrapping_sub($rc_bound); + prob_update_1(prob); + $symbol = ($symbol << 1).wrapping_add(1); + } + }}; +} + +macro_rules! rc_bittree8 { + ($rc:ident, $rc_in_ptr:ident, $rc_bound:ident, $probs_base:expr, $symbol:ident) => {{ + let probs_base = $probs_base; + $symbol = 1; + rc_normalize!($rc, $rc_in_ptr); + rc_bittree_step!($rc, $rc_bound, probs_base.add($symbol as usize), $symbol); + rc_normalize!($rc, $rc_in_ptr); + rc_bittree_step!($rc, $rc_bound, probs_base.add($symbol as usize), $symbol); + rc_normalize!($rc, $rc_in_ptr); + rc_bittree_step!($rc, $rc_bound, probs_base.add($symbol as usize), $symbol); + rc_normalize!($rc, $rc_in_ptr); + rc_bittree_step!($rc, $rc_bound, probs_base.add($symbol as usize), $symbol); + rc_normalize!($rc, $rc_in_ptr); + rc_bittree_step!($rc, $rc_bound, probs_base.add($symbol as usize), $symbol); + rc_normalize!($rc, $rc_in_ptr); + rc_bittree_step!($rc, $rc_bound, probs_base.add($symbol as usize), $symbol); + rc_normalize!($rc, $rc_in_ptr); + rc_bittree_step!($rc, $rc_bound, probs_base.add($symbol as usize), $symbol); + rc_normalize!($rc, $rc_in_ptr); + rc_bittree_step!($rc, $rc_bound, probs_base.add($symbol as usize), $symbol); + }}; +} + +macro_rules! rc_matched_literal_step { + ( + $rc:ident, + $rc_in_ptr:ident, + $rc_bound:ident, + $probs_base:ident, + $t_match_byte:ident, + $t_match_bit:ident, + $t_subcoder_index:ident, + $t_offset:ident, + $symbol:ident + ) => {{ + $t_match_byte <<= 1; + $t_match_bit = $t_match_byte & $t_offset; + $t_subcoder_index = $t_offset.wrapping_add($t_match_bit).wrapping_add($symbol); + rc_normalize!($rc, $rc_in_ptr); + let prob = $probs_base.add($t_subcoder_index as usize); + $rc_bound = ($rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul(*prob as u32); + if $rc.code < $rc_bound { + $rc.range = $rc_bound; + prob_update_0(prob); + $symbol <<= 1; + $t_offset &= !$t_match_bit; + } else { + $rc.range = $rc.range.wrapping_sub($rc_bound); + $rc.code = $rc.code.wrapping_sub($rc_bound); + prob_update_1(prob); + $symbol = ($symbol << 1).wrapping_add(1); + $t_offset &= $t_match_bit; + } + }}; +} + +macro_rules! rc_matched_literal { + ($rc:ident, $rc_in_ptr:ident, $rc_bound:ident, $probs_base:expr, $match_byte:expr, $symbol:ident) => {{ + let probs_base = $probs_base; + let mut t_match_byte = ($match_byte) as u32; + let mut t_match_bit: u32; + let mut t_subcoder_index: u32; + let mut t_offset: u32 = 0x100; + $symbol = 1; + rc_matched_literal_step!( + $rc, + $rc_in_ptr, + $rc_bound, + probs_base, + t_match_byte, + t_match_bit, + t_subcoder_index, + t_offset, + $symbol + ); + rc_matched_literal_step!( + $rc, + $rc_in_ptr, + $rc_bound, + probs_base, + t_match_byte, + t_match_bit, + t_subcoder_index, + t_offset, + $symbol + ); + rc_matched_literal_step!( + $rc, + $rc_in_ptr, + $rc_bound, + probs_base, + t_match_byte, + t_match_bit, + t_subcoder_index, + t_offset, + $symbol + ); + rc_matched_literal_step!( + $rc, + $rc_in_ptr, + $rc_bound, + probs_base, + t_match_byte, + t_match_bit, + t_subcoder_index, + t_offset, + $symbol + ); + rc_matched_literal_step!( + $rc, + $rc_in_ptr, + $rc_bound, + probs_base, + t_match_byte, + t_match_bit, + t_subcoder_index, + t_offset, + $symbol + ); + rc_matched_literal_step!( + $rc, + $rc_in_ptr, + $rc_bound, + probs_base, + t_match_byte, + t_match_bit, + t_subcoder_index, + t_offset, + $symbol + ); + rc_matched_literal_step!( + $rc, + $rc_in_ptr, + $rc_bound, + probs_base, + t_match_byte, + t_match_bit, + t_subcoder_index, + t_offset, + $symbol + ); + rc_matched_literal_step!( + $rc, + $rc_in_ptr, + $rc_bound, + probs_base, + t_match_byte, + t_match_bit, + t_subcoder_index, + t_offset, + $symbol + ); + }}; +} +unsafe fn lzma_decode( coder_ptr: *mut c_void, dictptr: *mut lzma_dict, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, ) -> lzma_ret { - let mut current_block: u64; + let mut block_state: DecoderBlockState; let coder: *mut lzma_lzma1_decoder = coder_ptr as *mut lzma_lzma1_decoder; - let ret: lzma_ret = rc_read_init( + let init_ret: lzma_ret = rc_read_init( ::core::ptr::addr_of_mut!((*coder).rc), - in_0, + input, in_pos, in_size, ); - if ret != LZMA_STREAM_END { - return ret; + if init_ret != LZMA_STREAM_END { + return init_ret; } let mut dict: lzma_dict = *dictptr; let dict_start: size_t = dict.pos; let mut rc: lzma_range_decoder = (*coder).rc; - let mut rc_in_ptr: *const u8 = in_0.offset(*in_pos as isize); - let rc_in_end: *const u8 = in_0.offset(in_size as isize); + let mut rc_in_ptr: *const u8 = input.offset(*in_pos as isize); + let rc_in_end: *const u8 = input.offset(in_size as isize); let rc_in_fast_end: *const u8 = if rc_in_end.offset_from(rc_in_ptr) <= 20 { rc_in_ptr } else { @@ -238,12 +538,14 @@ unsafe extern "C" fn lzma_decode( let mut len: u32 = (*coder).len; let literal_probs: *mut probability = ::core::ptr::addr_of_mut!((*coder).literal) as *mut probability; - let is_match_probs: *mut [probability; 16] = - ::core::ptr::addr_of_mut!((*coder).is_match) as *mut [probability; 16]; + let match_len_decoder: *mut lzma_length_decoder = + ::core::ptr::addr_of_mut!((*coder).match_len_decoder); + let rep_len_decoder: *mut lzma_length_decoder = + ::core::ptr::addr_of_mut!((*coder).rep_len_decoder); let literal_mask: u32 = (*coder).literal_mask; let literal_context_bits: u32 = (*coder).literal_context_bits; let mut pos_state: u32 = (dict.pos & pos_mask as size_t) as u32; - let mut ret_0: lzma_ret = LZMA_OK; + let mut ret: lzma_ret = LZMA_OK; let mut eopm_is_valid: bool = (*coder).uncompressed_size == LZMA_VLI_UNKNOWN; let mut might_finish_without_eopm: bool = false; if (*coder).uncompressed_size != LZMA_VLI_UNKNOWN @@ -252,18 +554,18 @@ unsafe extern "C" fn lzma_decode( dict.limit = dict.pos.wrapping_add((*coder).uncompressed_size as size_t); might_finish_without_eopm = true; } - current_block = resume_block_for_sequence((*coder).sequence); + block_state = resume_block_for_sequence((*coder).sequence); 'c_9380: loop { - match current_block { - 4609795085482299213 => { + match block_state { + BLOCK_RETURN => { (*dictptr).pos = dict.pos; break; } - 12043352250568755004 => { + BLOCK_REP_LEN_CHOICE => { if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_REP_LEN_CHOICE; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -280,12 +582,11 @@ unsafe extern "C" fn lzma_decode( RC_BIT_MODEL_TOTAL.wrapping_sub((*coder).rep_len_decoder.choice as u32) >> RC_MOVE_BITS, ) as probability; - probs = ::core::ptr::addr_of_mut!(*(::core::ptr::addr_of_mut!( - (*coder).rep_len_decoder.low - ) - as *mut [probability; 8]) - .offset(pos_state as isize)) - as *mut probability; + probs = ::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*coder).rep_len_decoder.low) + as *mut [probability; 8]) + .offset(pos_state as isize) + ) as *mut probability; limit = LEN_LOW_SYMBOLS; len = MATCH_LEN_MIN; } else { @@ -293,16 +594,16 @@ unsafe extern "C" fn lzma_decode( rc.code = rc.code.wrapping_sub(rc_bound); (*coder).rep_len_decoder.choice = (*coder).rep_len_decoder.choice - ((*coder).rep_len_decoder.choice >> RC_MOVE_BITS); - current_block = 6834592846991627977; + block_state = BLOCK_REP_LEN_CHOICE2; continue; } - current_block = 16690975975023747857; + block_state = BLOCK_LEN_BITTREE_INIT; } - 3996983927318648760 => { + BLOCK_IS_REP2 => { if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_IS_REP2; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -310,16 +611,13 @@ unsafe extern "C" fn lzma_decode( rc_in_ptr = rc_in_ptr.offset(1); } } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul((*coder).is_rep2[state as usize] as u32); + let is_rep2_prob = decoder_is_rep2_prob(coder, state); + rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul(*is_rep2_prob as u32); if rc.code < rc_bound { rc.range = rc_bound; - (*coder).is_rep2[state as usize] = ((*coder).is_rep2[state as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL - .wrapping_sub((*coder).is_rep2[state as usize] as u32) - >> RC_MOVE_BITS, - ) as probability; + *is_rep2_prob = (*is_rep2_prob as u32).wrapping_add( + RC_BIT_MODEL_TOTAL.wrapping_sub(*is_rep2_prob as u32) >> RC_MOVE_BITS, + ) as probability; let distance_3: u32 = rep2; rep2 = rep1; rep1 = rep0; @@ -327,21 +625,20 @@ unsafe extern "C" fn lzma_decode( } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).is_rep2[state as usize] = (*coder).is_rep2[state as usize] - - ((*coder).is_rep2[state as usize] >> RC_MOVE_BITS); + *is_rep2_prob = *is_rep2_prob - (*is_rep2_prob >> RC_MOVE_BITS); let distance_4: u32 = rep3; rep3 = rep2; rep2 = rep1; rep1 = rep0; rep0 = distance_4; } - current_block = 15498320742470848828; + block_state = BLOCK_REP_LEN_PREPARE; } - 11808118301119257848 => { + BLOCK_IS_REP1 => { if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_IS_REP1; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -349,46 +646,42 @@ unsafe extern "C" fn lzma_decode( rc_in_ptr = rc_in_ptr.offset(1); } } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul((*coder).is_rep1[state as usize] as u32); + let is_rep1_prob = decoder_is_rep1_prob(coder, state); + rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul(*is_rep1_prob as u32); if rc.code < rc_bound { rc.range = rc_bound; - (*coder).is_rep1[state as usize] = ((*coder).is_rep1[state as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL - .wrapping_sub((*coder).is_rep1[state as usize] as u32) - >> RC_MOVE_BITS, - ) as probability; + *is_rep1_prob = (*is_rep1_prob as u32).wrapping_add( + RC_BIT_MODEL_TOTAL.wrapping_sub(*is_rep1_prob as u32) >> RC_MOVE_BITS, + ) as probability; let distance_2: u32 = rep1; rep1 = rep0; rep0 = distance_2; } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).is_rep1[state as usize] = (*coder).is_rep1[state as usize] - - ((*coder).is_rep1[state as usize] >> RC_MOVE_BITS); - current_block = 3996983927318648760; + *is_rep1_prob = *is_rep1_prob - (*is_rep1_prob >> RC_MOVE_BITS); + block_state = BLOCK_IS_REP2; continue; } - current_block = 15498320742470848828; + block_state = BLOCK_REP_LEN_PREPARE; } - 5341942013764523046 => { + BLOCK_SHORTREP => { if dict_put_safe( ::core::ptr::addr_of_mut!(dict), dict_get(::core::ptr::addr_of_mut!(dict), rep0), ) { (*coder).sequence = SEQ_SHORTREP; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { - current_block = 4956146061682418353; + block_state = BLOCK_MAIN_LOOP; } } - 1698084742280242340 => { + BLOCK_IS_REP0_LONG => { if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_IS_REP0_LONG; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -396,40 +689,34 @@ unsafe extern "C" fn lzma_decode( rc_in_ptr = rc_in_ptr.offset(1); } } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul((*coder).is_rep0_long[state as usize][pos_state as usize] as u32); + let is_rep0_long_prob = + decoder_is_rep0_long_row(coder, state).add(pos_state as usize); + rc_bound = + (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul(*is_rep0_long_prob as u32); if rc.code < rc_bound { rc.range = rc_bound; - (*coder).is_rep0_long[state as usize][pos_state as usize] = - ((*coder).is_rep0_long[state as usize][pos_state as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).is_rep0_long[state as usize][pos_state as usize] - as u32, - ) >> RC_MOVE_BITS, - ) as probability; + *is_rep0_long_prob = (*is_rep0_long_prob as u32).wrapping_add( + RC_BIT_MODEL_TOTAL.wrapping_sub(*is_rep0_long_prob as u32) >> RC_MOVE_BITS, + ) as probability; state = (if state < LIT_STATES { STATE_LIT_SHORTREP } else { STATE_NONLIT_REP }) as u32; - current_block = 5341942013764523046; + block_state = BLOCK_SHORTREP; continue; } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).is_rep0_long[state as usize][pos_state as usize] = (*coder) - .is_rep0_long[state as usize][pos_state as usize] - - ((*coder).is_rep0_long[state as usize][pos_state as usize] - >> RC_MOVE_BITS); + *is_rep0_long_prob = *is_rep0_long_prob - (*is_rep0_long_prob >> RC_MOVE_BITS); } - current_block = 15498320742470848828; + block_state = BLOCK_REP_LEN_PREPARE; } - 4420799852307653083 => { + BLOCK_IS_REP0 => { if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_IS_REP0; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -437,32 +724,28 @@ unsafe extern "C" fn lzma_decode( rc_in_ptr = rc_in_ptr.offset(1); } } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul((*coder).is_rep0[state as usize] as u32); + let is_rep0_prob = decoder_is_rep0_prob(coder, state); + rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul(*is_rep0_prob as u32); if rc.code < rc_bound { rc.range = rc_bound; - (*coder).is_rep0[state as usize] = ((*coder).is_rep0[state as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL - .wrapping_sub((*coder).is_rep0[state as usize] as u32) - >> RC_MOVE_BITS, - ) as probability; - current_block = 1698084742280242340; + *is_rep0_prob = (*is_rep0_prob as u32).wrapping_add( + RC_BIT_MODEL_TOTAL.wrapping_sub(*is_rep0_prob as u32) >> RC_MOVE_BITS, + ) as probability; + block_state = BLOCK_IS_REP0_LONG; continue; } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).is_rep0[state as usize] = (*coder).is_rep0[state as usize] - - ((*coder).is_rep0[state as usize] >> RC_MOVE_BITS); - current_block = 11808118301119257848; + *is_rep0_prob = *is_rep0_prob - (*is_rep0_prob >> RC_MOVE_BITS); + block_state = BLOCK_IS_REP1; continue; } } - 3469750012682708893 => { + BLOCK_IS_REP => { if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_IS_REP; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -470,15 +753,13 @@ unsafe extern "C" fn lzma_decode( rc_in_ptr = rc_in_ptr.offset(1); } } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul((*coder).is_rep[state as usize] as u32); + let is_rep_prob = decoder_is_rep_prob(coder, state); + rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul(*is_rep_prob as u32); if rc.code < rc_bound { rc.range = rc_bound; - (*coder).is_rep[state as usize] = ((*coder).is_rep[state as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub((*coder).is_rep[state as usize] as u32) - >> RC_MOVE_BITS, - ) as probability; + *is_rep_prob = (*is_rep_prob as u32).wrapping_add( + RC_BIT_MODEL_TOTAL.wrapping_sub(*is_rep_prob as u32) >> RC_MOVE_BITS, + ) as probability; state = (if state < LIT_STATES { STATE_LIT_MATCH } else { @@ -487,27 +768,26 @@ unsafe extern "C" fn lzma_decode( rep3 = rep2; rep2 = rep1; rep1 = rep0; - current_block = 1138292997408115650; + block_state = BLOCK_MATCH_LEN_CHOICE; continue; } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).is_rep[state as usize] = (*coder).is_rep[state as usize] - - ((*coder).is_rep[state as usize] >> RC_MOVE_BITS); + *is_rep_prob = *is_rep_prob - (*is_rep_prob >> RC_MOVE_BITS); if dict_is_distance_valid(::core::ptr::addr_of_mut!(dict), 0) { - current_block = 4420799852307653083; + block_state = BLOCK_IS_REP0; continue; } - ret_0 = LZMA_DATA_ERROR; - current_block = 4609795085482299213; + ret = LZMA_DATA_ERROR; + block_state = BLOCK_RETURN; continue; } } - 7073645523065812117 => { + BLOCK_EOPM => { if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_EOPM; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -515,19 +795,19 @@ unsafe extern "C" fn lzma_decode( rc_in_ptr = rc_in_ptr.offset(1); } } - ret_0 = if rc.code == 0 { + ret = if rc.code == 0 { LZMA_STREAM_END } else { LZMA_DATA_ERROR }; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } - 10510472849010538284 => { + BLOCK_ALIGN => { if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_ALIGN; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -535,43 +815,37 @@ unsafe extern "C" fn lzma_decode( rc_in_ptr = rc_in_ptr.offset(1); } } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul((*coder).pos_align[offset.wrapping_add(symbol) as usize] as u32); + let pos_align_prob = decoder_pos_align_prob(coder, offset.wrapping_add(symbol)); + rc_bound = + (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul(*pos_align_prob as u32); if rc.code < rc_bound { rc.range = rc_bound; - (*coder).pos_align[offset.wrapping_add(symbol) as usize] = - ((*coder).pos_align[offset.wrapping_add(symbol) as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).pos_align[offset.wrapping_add(symbol) as usize] as u32, - ) >> RC_MOVE_BITS, - ) as probability; + *pos_align_prob = (*pos_align_prob as u32).wrapping_add( + RC_BIT_MODEL_TOTAL.wrapping_sub(*pos_align_prob as u32) >> RC_MOVE_BITS, + ) as probability; } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).pos_align[offset.wrapping_add(symbol) as usize] = (*coder).pos_align - [offset.wrapping_add(symbol) as usize] - - ((*coder).pos_align[offset.wrapping_add(symbol) as usize] - >> RC_MOVE_BITS); + *pos_align_prob = *pos_align_prob - (*pos_align_prob >> RC_MOVE_BITS); symbol = symbol.wrapping_add(offset); } offset <<= 1; if offset < ALIGN_SIZE { - current_block = 10510472849010538284; + block_state = BLOCK_ALIGN; continue; } rep0 = rep0.wrapping_add(symbol); if rep0 == UINT32_MAX { - current_block = 12043253436139097694; + block_state = BLOCK_EOPM_IF_VALID; } else { - current_block = 13383302701878543647; + block_state = BLOCK_VALIDATE_DISTANCE; } } - 15418612220330286504 => { + BLOCK_DIRECT => { if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_DIRECT; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -586,20 +860,20 @@ unsafe extern "C" fn lzma_decode( rep0 = (rep0 << 1).wrapping_add(rc_bound.wrapping_add(1)); limit -= 1; if limit > 0 { - current_block = 15418612220330286504; + block_state = BLOCK_DIRECT; continue; } rep0 <<= ALIGN_BITS; symbol = 0; offset = 1; - current_block = 10510472849010538284; + block_state = BLOCK_ALIGN; continue; } - 617447976488552541 => { + BLOCK_DIST_MODEL => { if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_DIST_MODEL; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -627,16 +901,16 @@ unsafe extern "C" fn lzma_decode( } offset += 1; if offset < limit { - current_block = 617447976488552541; + block_state = BLOCK_DIST_MODEL; continue; } - current_block = 13383302701878543647; + block_state = BLOCK_VALIDATE_DISTANCE; } - 4174862988780014241 => { + BLOCK_DIST_SLOT => { if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_DIST_SLOT; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -662,7 +936,7 @@ unsafe extern "C" fn lzma_decode( symbol = (symbol << 1).wrapping_add(1); } if symbol < DIST_SLOTS { - current_block = 4174862988780014241; + block_state = BLOCK_DIST_SLOT; continue; } symbol = symbol.wrapping_sub(DIST_SLOTS); @@ -680,21 +954,21 @@ unsafe extern "C" fn lzma_decode( .offset(-1); symbol = 1; offset = 0; - current_block = 617447976488552541; + block_state = BLOCK_DIST_MODEL; continue; } else { limit = limit.wrapping_sub(ALIGN_BITS); - current_block = 15418612220330286504; + block_state = BLOCK_DIRECT; continue; } } - current_block = 13383302701878543647; + block_state = BLOCK_VALIDATE_DISTANCE; } - 592696588731961849 => { + BLOCK_MATCH_LEN_BITTREE => { if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_MATCH_LEN_BITTREE; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -720,28 +994,29 @@ unsafe extern "C" fn lzma_decode( symbol = (symbol << 1).wrapping_add(1); } if symbol < limit { - current_block = 592696588731961849; + block_state = BLOCK_MATCH_LEN_BITTREE; continue; } len = len.wrapping_add(symbol.wrapping_sub(limit)); - probs = ::core::ptr::addr_of_mut!(*(::core::ptr::addr_of_mut!((*coder).dist_slot) - as *mut [probability; 64]) - .offset( - (if len < (DIST_STATES + MATCH_LEN_MIN) as u32 { - len.wrapping_sub(MATCH_LEN_MIN) - } else { - (DIST_STATES - 1) as u32 - }) as isize, - )) as *mut probability; + probs = ::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*coder).dist_slot) as *mut [probability; 64]) + .offset( + (if len < (DIST_STATES + MATCH_LEN_MIN) as u32 { + len.wrapping_sub(MATCH_LEN_MIN) + } else { + (DIST_STATES - 1) as u32 + }) as isize, + ) + ) as *mut probability; symbol = 1; - current_block = 4174862988780014241; + block_state = BLOCK_DIST_SLOT; continue; } - 13912927785247575907 => { + BLOCK_MATCH_LEN_CHOICE2 => { if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_MATCH_LEN_CHOICE2; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -759,12 +1034,11 @@ unsafe extern "C" fn lzma_decode( .wrapping_sub((*coder).match_len_decoder.choice2 as u32) >> RC_MOVE_BITS, ) as probability; - probs = ::core::ptr::addr_of_mut!(*(::core::ptr::addr_of_mut!( - (*coder).match_len_decoder.mid - ) - as *mut [probability; 8]) - .offset(pos_state as isize)) - as *mut probability; + probs = ::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*coder).match_len_decoder.mid) + as *mut [probability; 8]) + .offset(pos_state as isize) + ) as *mut probability; limit = LEN_MID_SYMBOLS; len = (MATCH_LEN_MIN + LEN_LOW_SYMBOLS) as u32; } else { @@ -777,13 +1051,13 @@ unsafe extern "C" fn lzma_decode( limit = LEN_HIGH_SYMBOLS; len = (MATCH_LEN_MIN + LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS) as u32; } - current_block = 8485842003490715114; + block_state = BLOCK_DIST_SLOT_INIT; } - 1138292997408115650 => { + BLOCK_MATCH_LEN_CHOICE => { if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_MATCH_LEN_CHOICE; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -801,12 +1075,11 @@ unsafe extern "C" fn lzma_decode( .wrapping_sub((*coder).match_len_decoder.choice as u32) >> RC_MOVE_BITS, ) as probability; - probs = ::core::ptr::addr_of_mut!(*(::core::ptr::addr_of_mut!( - (*coder).match_len_decoder.low - ) - as *mut [probability; 8]) - .offset(pos_state as isize)) - as *mut probability; + probs = ::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*coder).match_len_decoder.low) + as *mut [probability; 8]) + .offset(pos_state as isize) + ) as *mut probability; limit = LEN_LOW_SYMBOLS; len = MATCH_LEN_MIN; } else { @@ -814,27 +1087,27 @@ unsafe extern "C" fn lzma_decode( rc.code = rc.code.wrapping_sub(rc_bound); (*coder).match_len_decoder.choice = (*coder).match_len_decoder.choice - ((*coder).match_len_decoder.choice >> RC_MOVE_BITS); - current_block = 13912927785247575907; + block_state = BLOCK_MATCH_LEN_CHOICE2; continue; } - current_block = 8485842003490715114; + block_state = BLOCK_DIST_SLOT_INIT; } - 10535798129821001304 => { + BLOCK_LITERAL_WRITE => { if dict_put_safe(::core::ptr::addr_of_mut!(dict), symbol as u8) { (*coder).sequence = SEQ_LITERAL_WRITE; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { - current_block = 4956146061682418353; + block_state = BLOCK_MAIN_LOOP; } } - 18125716024132132232 => { + BLOCK_LITERAL_MATCHED => { let match_bit: u32 = len & offset; let subcoder_index: u32 = offset.wrapping_add(match_bit).wrapping_add(symbol); if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_LITERAL_MATCHED; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -864,19 +1137,19 @@ unsafe extern "C" fn lzma_decode( } len <<= 1; if symbol < (1 << 8) as u32 { - current_block = 18125716024132132232; + block_state = BLOCK_LITERAL_MATCHED; continue; } else { - current_block = 10535798129821001304; + block_state = BLOCK_LITERAL_WRITE; continue; } } - 5979571030476392895 => { + BLOCK_NORMALIZE_OR_IS_MATCH => { if might_finish_without_eopm && dict.pos == dict.limit { if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_NORMALIZE; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -885,12 +1158,12 @@ unsafe extern "C" fn lzma_decode( } } if rc.code == 0 { - ret_0 = LZMA_STREAM_END; - current_block = 4609795085482299213; + ret = LZMA_STREAM_END; + block_state = BLOCK_RETURN; continue; } else if !(*coder).allow_eopm { - ret_0 = LZMA_DATA_ERROR; - current_block = 4609795085482299213; + ret = LZMA_DATA_ERROR; + block_state = BLOCK_RETURN; continue; } else { eopm_is_valid = true; @@ -899,7 +1172,7 @@ unsafe extern "C" fn lzma_decode( if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_IS_MATCH; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -907,9 +1180,7 @@ unsafe extern "C" fn lzma_decode( rc_in_ptr = rc_in_ptr.offset(1); } } - let is_match_prob = ::core::ptr::addr_of_mut!( - (*is_match_probs.add(state as usize))[pos_state as usize] - ); + let is_match_prob = decoder_is_match_row(coder, state).add(pos_state as usize); rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul(*is_match_prob as u32); if rc.code < rc_bound { @@ -932,7 +1203,7 @@ unsafe extern "C" fn lzma_decode( } else { state.wrapping_sub(3) }; - current_block = 13844743919235296534; + block_state = BLOCK_LITERAL; continue; } else { state = if state <= STATE_LIT_SHORTREP { @@ -942,22 +1213,22 @@ unsafe extern "C" fn lzma_decode( }; len = (dict_get(::core::ptr::addr_of_mut!(dict), rep0) as u32) << 1; offset = 0x100; - current_block = 18125716024132132232; + block_state = BLOCK_LITERAL_MATCHED; continue; } } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); *is_match_prob = *is_match_prob - (*is_match_prob >> RC_MOVE_BITS); - current_block = 3469750012682708893; + block_state = BLOCK_IS_REP; continue; } } - 13844743919235296534 => { + BLOCK_LITERAL => { if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_LITERAL; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -983,18 +1254,18 @@ unsafe extern "C" fn lzma_decode( symbol = (symbol << 1).wrapping_add(1); } if symbol < (1 << 8) as u32 { - current_block = 13844743919235296534; + block_state = BLOCK_LITERAL; continue; } else { - current_block = 10535798129821001304; + block_state = BLOCK_LITERAL_WRITE; continue; } } - 2467942631393454738 => { + BLOCK_REP_LEN_BITTREE => { if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_REP_LEN_BITTREE; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -1020,31 +1291,31 @@ unsafe extern "C" fn lzma_decode( symbol = (symbol << 1).wrapping_add(1); } if symbol < limit { - current_block = 2467942631393454738; + block_state = BLOCK_REP_LEN_BITTREE; continue; } len = len.wrapping_add(symbol.wrapping_sub(limit)); - current_block = 17340485688450593529; + block_state = BLOCK_COPY; continue; } - 17340485688450593529 => { + BLOCK_COPY => { if dict_repeat( ::core::ptr::addr_of_mut!(dict), rep0, ::core::ptr::addr_of_mut!(len), ) { (*coder).sequence = SEQ_COPY; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { - current_block = 4956146061682418353; + block_state = BLOCK_MAIN_LOOP; } } _ => { if rc.range < RC_TOP_VALUE as u32 { if rc_in_ptr == rc_in_end { (*coder).sequence = SEQ_REP_LEN_CHOICE2; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue; } else { rc.range <<= RC_SHIFT_BITS; @@ -1062,12 +1333,11 @@ unsafe extern "C" fn lzma_decode( .wrapping_sub((*coder).rep_len_decoder.choice2 as u32) >> RC_MOVE_BITS, ) as probability; - probs = ::core::ptr::addr_of_mut!(*(::core::ptr::addr_of_mut!( - (*coder).rep_len_decoder.mid - ) - as *mut [probability; 8]) - .offset(pos_state as isize)) - as *mut probability; + probs = ::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*coder).rep_len_decoder.mid) + as *mut [probability; 8]) + .offset(pos_state as isize) + ) as *mut probability; limit = LEN_MID_SYMBOLS; len = (MATCH_LEN_MIN + LEN_LOW_SYMBOLS) as u32; } else { @@ -1080,23 +1350,23 @@ unsafe extern "C" fn lzma_decode( limit = LEN_HIGH_SYMBOLS; len = (MATCH_LEN_MIN + LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS) as u32; } - current_block = 16690975975023747857; + block_state = BLOCK_LEN_BITTREE_INIT; } } - match current_block { - 13383302701878543647 => { + match block_state { + BLOCK_VALIDATE_DISTANCE => { if dict_is_distance_valid(::core::ptr::addr_of_mut!(dict), rep0 as size_t) { - current_block = 17340485688450593529; + block_state = BLOCK_COPY; continue; } - ret_0 = LZMA_DATA_ERROR; - current_block = 4609795085482299213; + ret = LZMA_DATA_ERROR; + block_state = BLOCK_RETURN; continue; } - 4956146061682418353 => loop { + BLOCK_MAIN_LOOP => loop { pos_state = (dict.pos & pos_mask as size_t) as u32; if rc_in_ptr >= rc_in_fast_end || dict.pos == dict.limit { - current_block = 5979571030476392895; + block_state = BLOCK_NORMALIZE_OR_IS_MATCH; continue 'c_9380; } if rc.range < RC_TOP_VALUE as u32 { @@ -1104,9 +1374,7 @@ unsafe extern "C" fn lzma_decode( rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; rc_in_ptr = rc_in_ptr.offset(1); } - let is_match_prob = ::core::ptr::addr_of_mut!( - (*is_match_probs.add(state as usize))[pos_state as usize] - ); + let is_match_prob = decoder_is_match_row(coder, state).add(pos_state as usize); rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul(*is_match_prob as u32); if rc.code < rc_bound { @@ -1128,53 +1396,202 @@ unsafe extern "C" fn lzma_decode( } else { state.wrapping_sub(3) }; - symbol = 1; - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul(*probs.offset(symbol as isize) as u32); - if rc.code < rc_bound { - rc.range = rc_bound; - *probs.offset(symbol as isize) = - (*probs.offset(symbol as isize) as u32).wrapping_add( - RC_BIT_MODEL_TOTAL - .wrapping_sub(*probs.offset(symbol as isize) as u32) - >> RC_MOVE_BITS, - ) as probability; - symbol <<= 1; + rc_bittree8!(rc, rc_in_ptr, rc_bound, probs, symbol); + } else { + state = if state <= STATE_LIT_SHORTREP { + state.wrapping_sub(3) } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - *probs.offset(symbol as isize) -= - *probs.offset(symbol as isize) >> RC_MOVE_BITS; - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } + state.wrapping_sub(6) + }; + rc_matched_literal!( + rc, + rc_in_ptr, + rc_bound, + probs, + dict_get(::core::ptr::addr_of_mut!(dict), rep0), + symbol + ); + } + dict_put(::core::ptr::addr_of_mut!(dict), symbol as u8); + } else { + rc.range = rc.range.wrapping_sub(rc_bound); + rc.code = rc.code.wrapping_sub(rc_bound); + *is_match_prob = *is_match_prob - (*is_match_prob >> RC_MOVE_BITS); + if rc.range < RC_TOP_VALUE as u32 { + rc.range <<= RC_SHIFT_BITS; + rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; + rc_in_ptr = rc_in_ptr.offset(1); + } + let is_rep_prob = decoder_is_rep_prob(coder, state); + rc_bound = + (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul(*is_rep_prob as u32); + if rc.code < rc_bound { + rc.range = rc_bound; + *is_rep_prob = (*is_rep_prob as u32).wrapping_add( + RC_BIT_MODEL_TOTAL.wrapping_sub(*is_rep_prob as u32) >> RC_MOVE_BITS, + ) as probability; + state = (if state < LIT_STATES { + STATE_LIT_MATCH + } else { + STATE_NONLIT_MATCH + }) as u32; + rep3 = rep2; + rep2 = rep1; + rep1 = rep0; + symbol = 1; + rc_normalize!(rc, rc_in_ptr); rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul(*probs.offset(symbol as isize) as u32); + .wrapping_mul((*coder).match_len_decoder.choice as u32); if rc.code < rc_bound { rc.range = rc_bound; - *probs.offset(symbol as isize) = - (*probs.offset(symbol as isize) as u32).wrapping_add( - RC_BIT_MODEL_TOTAL - .wrapping_sub(*probs.offset(symbol as isize) as u32) - >> RC_MOVE_BITS, - ) as probability; - symbol <<= 1; + prob_update_0(::core::ptr::addr_of_mut!( + (*coder).match_len_decoder.choice + )); + symbol = 1; + let match_len_low = length_low_row(match_len_decoder, pos_state); + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + match_len_low.add(symbol as usize), + symbol + ); + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + match_len_low.add(symbol as usize), + symbol + ); + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + match_len_low.add(symbol as usize), + symbol + ); + symbol = symbol.wrapping_add((-(1_i32 << 3) + 2) as u32); + len = symbol; } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); - *probs.offset(symbol as isize) -= - *probs.offset(symbol as isize) >> RC_MOVE_BITS; - symbol = (symbol << 1).wrapping_add(1); + prob_update_1(::core::ptr::addr_of_mut!( + (*coder).match_len_decoder.choice + )); + rc_normalize!(rc, rc_in_ptr); + rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) + .wrapping_mul((*coder).match_len_decoder.choice2 as u32); + if rc.code < rc_bound { + rc.range = rc_bound; + prob_update_0(::core::ptr::addr_of_mut!( + (*coder).match_len_decoder.choice2 + )); + symbol = 1; + let match_len_mid = length_mid_row(match_len_decoder, pos_state); + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + match_len_mid.add(symbol as usize), + symbol + ); + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + match_len_mid.add(symbol as usize), + symbol + ); + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + match_len_mid.add(symbol as usize), + symbol + ); + symbol = symbol.wrapping_add((-(1_i32 << 3) + 2 + (1 << 3)) as u32); + len = symbol; + } else { + rc.range = rc.range.wrapping_sub(rc_bound); + rc.code = rc.code.wrapping_sub(rc_bound); + prob_update_1(::core::ptr::addr_of_mut!( + (*coder).match_len_decoder.choice2 + )); + symbol = 1; + let match_len_high = length_high_probs(match_len_decoder); + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + match_len_high.add(symbol as usize), + symbol + ); + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + match_len_high.add(symbol as usize), + symbol + ); + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + match_len_high.add(symbol as usize), + symbol + ); + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + match_len_high.add(symbol as usize), + symbol + ); + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + match_len_high.add(symbol as usize), + symbol + ); + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + match_len_high.add(symbol as usize), + symbol + ); + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + match_len_high.add(symbol as usize), + symbol + ); + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + match_len_high.add(symbol as usize), + symbol + ); + symbol = symbol + .wrapping_add((-(1_i32 << 8) + 2 + (1 << 3) + (1 << 3)) as u32); + len = symbol; + } } + probs = ::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*coder).dist_slot) + as *mut [probability; 64]) + .offset( + (if len < (DIST_STATES + MATCH_LEN_MIN) as u32 { + len.wrapping_sub(MATCH_LEN_MIN) + } else { + (DIST_STATES - 1) as u32 + }) as isize, + ) + ) as *mut probability; + symbol = 1; if rc.range < RC_TOP_VALUE as u32 { rc.range <<= RC_SHIFT_BITS; rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; @@ -1313,936 +1730,9 @@ unsafe extern "C" fn lzma_decode( *probs.offset(symbol as isize) >> RC_MOVE_BITS; symbol = (symbol << 1).wrapping_add(1); } - } else { - state = if state <= STATE_LIT_SHORTREP { - state.wrapping_sub(3) - } else { - state.wrapping_sub(6) - }; - let mut t_match_byte: u32 = - dict_get(::core::ptr::addr_of_mut!(dict), rep0) as u32; - let mut t_match_bit: u32 = 0; - let mut t_subcoder_index: u32 = 0; - let mut t_offset: u32 = 0x100; - symbol = 1; - t_match_byte <<= 1; - t_match_bit = t_match_byte & t_offset; - t_subcoder_index = t_offset.wrapping_add(t_match_bit).wrapping_add(symbol); - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul(*probs.offset(t_subcoder_index as isize) as u32); - if rc.code < rc_bound { - rc.range = rc_bound; - *probs.offset(t_subcoder_index as isize) = - (*probs.offset(t_subcoder_index as isize) as u32).wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - *probs.offset(t_subcoder_index as isize) as u32, - ) >> RC_MOVE_BITS, - ) as probability; - symbol <<= 1; - t_offset &= !t_match_bit; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - *probs.offset(t_subcoder_index as isize) -= - *probs.offset(t_subcoder_index as isize) >> RC_MOVE_BITS; - symbol = (symbol << 1).wrapping_add(1); - t_offset &= t_match_bit; - } - t_match_byte <<= 1; - t_match_bit = t_match_byte & t_offset; - t_subcoder_index = t_offset.wrapping_add(t_match_bit).wrapping_add(symbol); - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul(*probs.offset(t_subcoder_index as isize) as u32); - if rc.code < rc_bound { - rc.range = rc_bound; - *probs.offset(t_subcoder_index as isize) = - (*probs.offset(t_subcoder_index as isize) as u32).wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - *probs.offset(t_subcoder_index as isize) as u32, - ) >> RC_MOVE_BITS, - ) as probability; - symbol <<= 1; - t_offset &= !t_match_bit; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - *probs.offset(t_subcoder_index as isize) -= - *probs.offset(t_subcoder_index as isize) >> RC_MOVE_BITS; - symbol = (symbol << 1).wrapping_add(1); - t_offset &= t_match_bit; - } - t_match_byte <<= 1; - t_match_bit = t_match_byte & t_offset; - t_subcoder_index = t_offset.wrapping_add(t_match_bit).wrapping_add(symbol); - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul(*probs.offset(t_subcoder_index as isize) as u32); - if rc.code < rc_bound { - rc.range = rc_bound; - *probs.offset(t_subcoder_index as isize) = - (*probs.offset(t_subcoder_index as isize) as u32).wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - *probs.offset(t_subcoder_index as isize) as u32, - ) >> RC_MOVE_BITS, - ) as probability; - symbol <<= 1; - t_offset &= !t_match_bit; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - *probs.offset(t_subcoder_index as isize) -= - *probs.offset(t_subcoder_index as isize) >> RC_MOVE_BITS; - symbol = (symbol << 1).wrapping_add(1); - t_offset &= t_match_bit; - } - t_match_byte <<= 1; - t_match_bit = t_match_byte & t_offset; - t_subcoder_index = t_offset.wrapping_add(t_match_bit).wrapping_add(symbol); - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul(*probs.offset(t_subcoder_index as isize) as u32); - if rc.code < rc_bound { - rc.range = rc_bound; - *probs.offset(t_subcoder_index as isize) = - (*probs.offset(t_subcoder_index as isize) as u32).wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - *probs.offset(t_subcoder_index as isize) as u32, - ) >> RC_MOVE_BITS, - ) as probability; - symbol <<= 1; - t_offset &= !t_match_bit; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - *probs.offset(t_subcoder_index as isize) -= - *probs.offset(t_subcoder_index as isize) >> RC_MOVE_BITS; - symbol = (symbol << 1).wrapping_add(1); - t_offset &= t_match_bit; - } - t_match_byte <<= 1; - t_match_bit = t_match_byte & t_offset; - t_subcoder_index = t_offset.wrapping_add(t_match_bit).wrapping_add(symbol); - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul(*probs.offset(t_subcoder_index as isize) as u32); - if rc.code < rc_bound { - rc.range = rc_bound; - *probs.offset(t_subcoder_index as isize) = - (*probs.offset(t_subcoder_index as isize) as u32).wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - *probs.offset(t_subcoder_index as isize) as u32, - ) >> RC_MOVE_BITS, - ) as probability; - symbol <<= 1; - t_offset &= !t_match_bit; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - *probs.offset(t_subcoder_index as isize) -= - *probs.offset(t_subcoder_index as isize) >> RC_MOVE_BITS; - symbol = (symbol << 1).wrapping_add(1); - t_offset &= t_match_bit; - } - t_match_byte <<= 1; - t_match_bit = t_match_byte & t_offset; - t_subcoder_index = t_offset.wrapping_add(t_match_bit).wrapping_add(symbol); - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul(*probs.offset(t_subcoder_index as isize) as u32); - if rc.code < rc_bound { - rc.range = rc_bound; - *probs.offset(t_subcoder_index as isize) = - (*probs.offset(t_subcoder_index as isize) as u32).wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - *probs.offset(t_subcoder_index as isize) as u32, - ) >> RC_MOVE_BITS, - ) as probability; - symbol <<= 1; - t_offset &= !t_match_bit; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - *probs.offset(t_subcoder_index as isize) -= - *probs.offset(t_subcoder_index as isize) >> RC_MOVE_BITS; - symbol = (symbol << 1).wrapping_add(1); - t_offset &= t_match_bit; - } - t_match_byte <<= 1; - t_match_bit = t_match_byte & t_offset; - t_subcoder_index = t_offset.wrapping_add(t_match_bit).wrapping_add(symbol); - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul(*probs.offset(t_subcoder_index as isize) as u32); - if rc.code < rc_bound { - rc.range = rc_bound; - *probs.offset(t_subcoder_index as isize) = - (*probs.offset(t_subcoder_index as isize) as u32).wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - *probs.offset(t_subcoder_index as isize) as u32, - ) >> RC_MOVE_BITS, - ) as probability; - symbol <<= 1; - t_offset &= !t_match_bit; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - *probs.offset(t_subcoder_index as isize) -= - *probs.offset(t_subcoder_index as isize) >> RC_MOVE_BITS; - symbol = (symbol << 1).wrapping_add(1); - t_offset &= t_match_bit; - } - t_match_byte <<= 1; - t_match_bit = t_match_byte & t_offset; - t_subcoder_index = t_offset.wrapping_add(t_match_bit).wrapping_add(symbol); - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul(*probs.offset(t_subcoder_index as isize) as u32); - if rc.code < rc_bound { - rc.range = rc_bound; - *probs.offset(t_subcoder_index as isize) = - (*probs.offset(t_subcoder_index as isize) as u32).wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - *probs.offset(t_subcoder_index as isize) as u32, - ) >> RC_MOVE_BITS, - ) as probability; - symbol <<= 1; - t_offset &= !t_match_bit; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - *probs.offset(t_subcoder_index as isize) -= - *probs.offset(t_subcoder_index as isize) >> RC_MOVE_BITS; - symbol = (symbol << 1).wrapping_add(1); - t_offset &= t_match_bit; - } - } - dict_put(::core::ptr::addr_of_mut!(dict), symbol as u8); - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - *is_match_prob = *is_match_prob - (*is_match_prob >> RC_MOVE_BITS); - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul((*coder).is_rep[state as usize] as u32); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).is_rep[state as usize] = - ((*coder).is_rep[state as usize] as u32).wrapping_add( - RC_BIT_MODEL_TOTAL - .wrapping_sub((*coder).is_rep[state as usize] as u32) - >> RC_MOVE_BITS, - ) as probability; - state = (if state < LIT_STATES { - STATE_LIT_MATCH - } else { - STATE_NONLIT_MATCH - }) as u32; - rep3 = rep2; - rep2 = rep1; - rep1 = rep0; - symbol = 1; - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul((*coder).match_len_decoder.choice as u32); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).match_len_decoder.choice = - ((*coder).match_len_decoder.choice as u32).wrapping_add( - RC_BIT_MODEL_TOTAL - .wrapping_sub((*coder).match_len_decoder.choice as u32) - >> RC_MOVE_BITS, - ) as probability; - symbol = 1; - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).match_len_decoder.low[pos_state as usize][symbol as usize] - as u32, - ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).match_len_decoder.low[pos_state as usize] - [symbol as usize] = ((*coder).match_len_decoder.low - [pos_state as usize][symbol as usize] - as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).match_len_decoder.low[pos_state as usize] - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).match_len_decoder.low[pos_state as usize] - [symbol as usize] = (*coder).match_len_decoder.low - [pos_state as usize][symbol as usize] - - ((*coder).match_len_decoder.low[pos_state as usize] - [symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).match_len_decoder.low[pos_state as usize][symbol as usize] - as u32, - ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).match_len_decoder.low[pos_state as usize] - [symbol as usize] = ((*coder).match_len_decoder.low - [pos_state as usize][symbol as usize] - as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).match_len_decoder.low[pos_state as usize] - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).match_len_decoder.low[pos_state as usize] - [symbol as usize] = (*coder).match_len_decoder.low - [pos_state as usize][symbol as usize] - - ((*coder).match_len_decoder.low[pos_state as usize] - [symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).match_len_decoder.low[pos_state as usize][symbol as usize] - as u32, - ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).match_len_decoder.low[pos_state as usize] - [symbol as usize] = ((*coder).match_len_decoder.low - [pos_state as usize][symbol as usize] - as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).match_len_decoder.low[pos_state as usize] - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).match_len_decoder.low[pos_state as usize] - [symbol as usize] = (*coder).match_len_decoder.low - [pos_state as usize][symbol as usize] - - ((*coder).match_len_decoder.low[pos_state as usize] - [symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - symbol = symbol.wrapping_add((-(1_i32 << 3) + 2) as u32); - len = symbol; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).match_len_decoder.choice = (*coder).match_len_decoder.choice - - ((*coder).match_len_decoder.choice >> RC_MOVE_BITS); - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul((*coder).match_len_decoder.choice2 as u32); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).match_len_decoder.choice2 = - ((*coder).match_len_decoder.choice2 as u32).wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).match_len_decoder.choice2 as u32, - ) >> RC_MOVE_BITS, - ) as probability - as probability; - symbol = 1; - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).match_len_decoder.mid[pos_state as usize] - [symbol as usize] - as u32, - ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).match_len_decoder.mid[pos_state as usize] - [symbol as usize] = ((*coder).match_len_decoder.mid - [pos_state as usize] - [symbol as usize] - as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).match_len_decoder.mid[pos_state as usize] - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).match_len_decoder.mid[pos_state as usize] - [symbol as usize] = (*coder).match_len_decoder.mid - [pos_state as usize] - [symbol as usize] - - ((*coder).match_len_decoder.mid[pos_state as usize] - [symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).match_len_decoder.mid[pos_state as usize] - [symbol as usize] - as u32, - ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).match_len_decoder.mid[pos_state as usize] - [symbol as usize] = ((*coder).match_len_decoder.mid - [pos_state as usize] - [symbol as usize] - as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).match_len_decoder.mid[pos_state as usize] - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).match_len_decoder.mid[pos_state as usize] - [symbol as usize] = (*coder).match_len_decoder.mid - [pos_state as usize] - [symbol as usize] - - ((*coder).match_len_decoder.mid[pos_state as usize] - [symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).match_len_decoder.mid[pos_state as usize] - [symbol as usize] - as u32, - ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).match_len_decoder.mid[pos_state as usize] - [symbol as usize] = ((*coder).match_len_decoder.mid - [pos_state as usize] - [symbol as usize] - as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).match_len_decoder.mid[pos_state as usize] - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).match_len_decoder.mid[pos_state as usize] - [symbol as usize] = (*coder).match_len_decoder.mid - [pos_state as usize] - [symbol as usize] - - ((*coder).match_len_decoder.mid[pos_state as usize] - [symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - symbol = symbol.wrapping_add((-(1_i32 << 3) + 2 + (1 << 3)) as u32); - len = symbol; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).match_len_decoder.choice2 = - (*coder).match_len_decoder.choice2 - - ((*coder).match_len_decoder.choice2 >> RC_MOVE_BITS); - symbol = 1; - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).match_len_decoder.high[symbol as usize] as u32, - ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).match_len_decoder.high[symbol as usize] = - ((*coder).match_len_decoder.high[symbol as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).match_len_decoder.high[symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).match_len_decoder.high[symbol as usize] = - (*coder).match_len_decoder.high[symbol as usize] - - ((*coder).match_len_decoder.high[symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).match_len_decoder.high[symbol as usize] as u32, - ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).match_len_decoder.high[symbol as usize] = - ((*coder).match_len_decoder.high[symbol as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).match_len_decoder.high[symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).match_len_decoder.high[symbol as usize] = - (*coder).match_len_decoder.high[symbol as usize] - - ((*coder).match_len_decoder.high[symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).match_len_decoder.high[symbol as usize] as u32, - ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).match_len_decoder.high[symbol as usize] = - ((*coder).match_len_decoder.high[symbol as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).match_len_decoder.high[symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).match_len_decoder.high[symbol as usize] = - (*coder).match_len_decoder.high[symbol as usize] - - ((*coder).match_len_decoder.high[symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).match_len_decoder.high[symbol as usize] as u32, - ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).match_len_decoder.high[symbol as usize] = - ((*coder).match_len_decoder.high[symbol as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).match_len_decoder.high[symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).match_len_decoder.high[symbol as usize] = - (*coder).match_len_decoder.high[symbol as usize] - - ((*coder).match_len_decoder.high[symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).match_len_decoder.high[symbol as usize] as u32, - ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).match_len_decoder.high[symbol as usize] = - ((*coder).match_len_decoder.high[symbol as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).match_len_decoder.high[symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).match_len_decoder.high[symbol as usize] = - (*coder).match_len_decoder.high[symbol as usize] - - ((*coder).match_len_decoder.high[symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).match_len_decoder.high[symbol as usize] as u32, - ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).match_len_decoder.high[symbol as usize] = - ((*coder).match_len_decoder.high[symbol as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).match_len_decoder.high[symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).match_len_decoder.high[symbol as usize] = - (*coder).match_len_decoder.high[symbol as usize] - - ((*coder).match_len_decoder.high[symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).match_len_decoder.high[symbol as usize] as u32, - ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).match_len_decoder.high[symbol as usize] = - ((*coder).match_len_decoder.high[symbol as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).match_len_decoder.high[symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).match_len_decoder.high[symbol as usize] = - (*coder).match_len_decoder.high[symbol as usize] - - ((*coder).match_len_decoder.high[symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).match_len_decoder.high[symbol as usize] as u32, - ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).match_len_decoder.high[symbol as usize] = - ((*coder).match_len_decoder.high[symbol as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).match_len_decoder.high[symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).match_len_decoder.high[symbol as usize] = - (*coder).match_len_decoder.high[symbol as usize] - - ((*coder).match_len_decoder.high[symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - symbol = symbol - .wrapping_add((-(1_i32 << 8) + 2 + (1 << 3) + (1 << 3)) as u32); - len = symbol; - } - } - probs = ::core::ptr::addr_of_mut!(*(::core::ptr::addr_of_mut!( - (*coder).dist_slot - ) - as *mut [probability; 64]) - .offset( - (if len < (DIST_STATES + MATCH_LEN_MIN) as u32 { - len.wrapping_sub(MATCH_LEN_MIN) - } else { - (DIST_STATES - 1) as u32 - }) as isize, - )) as *mut probability; - symbol = 1; - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul(*probs.offset(symbol as isize) as u32); - if rc.code < rc_bound { - rc.range = rc_bound; - *probs.offset(symbol as isize) = - (*probs.offset(symbol as isize) as u32).wrapping_add( - RC_BIT_MODEL_TOTAL - .wrapping_sub(*probs.offset(symbol as isize) as u32) - >> RC_MOVE_BITS, - ) as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - *probs.offset(symbol as isize) -= - *probs.offset(symbol as isize) >> RC_MOVE_BITS; - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul(*probs.offset(symbol as isize) as u32); - if rc.code < rc_bound { - rc.range = rc_bound; - *probs.offset(symbol as isize) = - (*probs.offset(symbol as isize) as u32).wrapping_add( - RC_BIT_MODEL_TOTAL - .wrapping_sub(*probs.offset(symbol as isize) as u32) - >> RC_MOVE_BITS, - ) as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - *probs.offset(symbol as isize) -= - *probs.offset(symbol as isize) >> RC_MOVE_BITS; - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul(*probs.offset(symbol as isize) as u32); - if rc.code < rc_bound { - rc.range = rc_bound; - *probs.offset(symbol as isize) = - (*probs.offset(symbol as isize) as u32).wrapping_add( - RC_BIT_MODEL_TOTAL - .wrapping_sub(*probs.offset(symbol as isize) as u32) - >> RC_MOVE_BITS, - ) as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - *probs.offset(symbol as isize) -= - *probs.offset(symbol as isize) >> RC_MOVE_BITS; - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul(*probs.offset(symbol as isize) as u32); - if rc.code < rc_bound { - rc.range = rc_bound; - *probs.offset(symbol as isize) = - (*probs.offset(symbol as isize) as u32).wrapping_add( - RC_BIT_MODEL_TOTAL - .wrapping_sub(*probs.offset(symbol as isize) as u32) - >> RC_MOVE_BITS, - ) as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - *probs.offset(symbol as isize) -= - *probs.offset(symbol as isize) >> RC_MOVE_BITS; - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul(*probs.offset(symbol as isize) as u32); - if rc.code < rc_bound { - rc.range = rc_bound; - *probs.offset(symbol as isize) = - (*probs.offset(symbol as isize) as u32).wrapping_add( - RC_BIT_MODEL_TOTAL - .wrapping_sub(*probs.offset(symbol as isize) as u32) - >> RC_MOVE_BITS, - ) as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - *probs.offset(symbol as isize) -= - *probs.offset(symbol as isize) >> RC_MOVE_BITS; - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul(*probs.offset(symbol as isize) as u32); - if rc.code < rc_bound { - rc.range = rc_bound; - *probs.offset(symbol as isize) = - (*probs.offset(symbol as isize) as u32).wrapping_add( - RC_BIT_MODEL_TOTAL - .wrapping_sub(*probs.offset(symbol as isize) as u32) - >> RC_MOVE_BITS, - ) as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - *probs.offset(symbol as isize) -= - *probs.offset(symbol as isize) >> RC_MOVE_BITS; - symbol = (symbol << 1).wrapping_add(1); - } - symbol = symbol.wrapping_add(-(1_i32 << 6) as u32); - if symbol < DIST_MODEL_START { - rep0 = symbol; + symbol = symbol.wrapping_add(-(1_i32 << 6) as u32); + if symbol < DIST_MODEL_START { + rep0 = symbol; } else { limit = (symbol >> 1).wrapping_sub(1); rep0 = (2u32).wrapping_add(symbol & 1); @@ -2313,30 +1803,22 @@ unsafe extern "C" fn lzma_decode( rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; rc_in_ptr = rc_in_ptr.offset(1); } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).pos_align[symbol.wrapping_add(1) as usize] as u32, - ); + let pos_align_prob = + decoder_pos_align_prob(coder, symbol.wrapping_add(1)); + rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) + .wrapping_mul(*pos_align_prob as u32); if rc.code < rc_bound { rc.range = rc_bound; - (*coder).pos_align[symbol.wrapping_add(1) as usize] = ((*coder) - .pos_align - [symbol.wrapping_add(1) as usize] - as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).pos_align[symbol.wrapping_add(1) as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability + *pos_align_prob = (*pos_align_prob as u32).wrapping_add( + RC_BIT_MODEL_TOTAL.wrapping_sub(*pos_align_prob as u32) + >> RC_MOVE_BITS, + ) as probability; } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).pos_align[symbol.wrapping_add(1) as usize] = - (*coder).pos_align[symbol.wrapping_add(1) as usize] - - ((*coder).pos_align[symbol.wrapping_add(1) as usize] - >> RC_MOVE_BITS); + *pos_align_prob = + *pos_align_prob - (*pos_align_prob >> RC_MOVE_BITS); symbol += 1; } if rc.range < RC_TOP_VALUE as u32 { @@ -2344,30 +1826,22 @@ unsafe extern "C" fn lzma_decode( rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; rc_in_ptr = rc_in_ptr.offset(1); } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).pos_align[symbol.wrapping_add(2) as usize] as u32, - ); + let pos_align_prob = + decoder_pos_align_prob(coder, symbol.wrapping_add(2)); + rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) + .wrapping_mul(*pos_align_prob as u32); if rc.code < rc_bound { rc.range = rc_bound; - (*coder).pos_align[symbol.wrapping_add(2) as usize] = ((*coder) - .pos_align - [symbol.wrapping_add(2) as usize] - as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).pos_align[symbol.wrapping_add(2) as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability + *pos_align_prob = (*pos_align_prob as u32).wrapping_add( + RC_BIT_MODEL_TOTAL.wrapping_sub(*pos_align_prob as u32) + >> RC_MOVE_BITS, + ) as probability; } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).pos_align[symbol.wrapping_add(2) as usize] = - (*coder).pos_align[symbol.wrapping_add(2) as usize] - - ((*coder).pos_align[symbol.wrapping_add(2) as usize] - >> RC_MOVE_BITS); + *pos_align_prob = + *pos_align_prob - (*pos_align_prob >> RC_MOVE_BITS); symbol = symbol.wrapping_add(2); } if rc.range < RC_TOP_VALUE as u32 { @@ -2375,30 +1849,22 @@ unsafe extern "C" fn lzma_decode( rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; rc_in_ptr = rc_in_ptr.offset(1); } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).pos_align[symbol.wrapping_add(4) as usize] as u32, - ); + let pos_align_prob = + decoder_pos_align_prob(coder, symbol.wrapping_add(4)); + rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) + .wrapping_mul(*pos_align_prob as u32); if rc.code < rc_bound { rc.range = rc_bound; - (*coder).pos_align[symbol.wrapping_add(4) as usize] = ((*coder) - .pos_align - [symbol.wrapping_add(4) as usize] - as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).pos_align[symbol.wrapping_add(4) as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability + *pos_align_prob = (*pos_align_prob as u32).wrapping_add( + RC_BIT_MODEL_TOTAL.wrapping_sub(*pos_align_prob as u32) + >> RC_MOVE_BITS, + ) as probability; } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).pos_align[symbol.wrapping_add(4) as usize] = - (*coder).pos_align[symbol.wrapping_add(4) as usize] - - ((*coder).pos_align[symbol.wrapping_add(4) as usize] - >> RC_MOVE_BITS); + *pos_align_prob = + *pos_align_prob - (*pos_align_prob >> RC_MOVE_BITS); symbol = symbol.wrapping_add(4); } if rc.range < RC_TOP_VALUE as u32 { @@ -2406,30 +1872,22 @@ unsafe extern "C" fn lzma_decode( rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; rc_in_ptr = rc_in_ptr.offset(1); } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).pos_align[symbol.wrapping_add(8) as usize] as u32, - ); + let pos_align_prob = + decoder_pos_align_prob(coder, symbol.wrapping_add(8)); + rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) + .wrapping_mul(*pos_align_prob as u32); if rc.code < rc_bound { rc.range = rc_bound; - (*coder).pos_align[symbol.wrapping_add(8) as usize] = ((*coder) - .pos_align - [symbol.wrapping_add(8) as usize] - as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).pos_align[symbol.wrapping_add(8) as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability + *pos_align_prob = (*pos_align_prob as u32).wrapping_add( + RC_BIT_MODEL_TOTAL.wrapping_sub(*pos_align_prob as u32) + >> RC_MOVE_BITS, + ) as probability; } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).pos_align[symbol.wrapping_add(8) as usize] = - (*coder).pos_align[symbol.wrapping_add(8) as usize] - - ((*coder).pos_align[symbol.wrapping_add(8) as usize] - >> RC_MOVE_BITS); + *pos_align_prob = + *pos_align_prob - (*pos_align_prob >> RC_MOVE_BITS); symbol = symbol.wrapping_add(8); } rep0 = rep0.wrapping_add(symbol); @@ -2440,18 +1898,18 @@ unsafe extern "C" fn lzma_decode( } if !dict_is_distance_valid(::core::ptr::addr_of_mut!(dict), rep0 as size_t) { - ret_0 = LZMA_DATA_ERROR; - current_block = 4609795085482299213; + ret = LZMA_DATA_ERROR; + block_state = BLOCK_RETURN; continue 'c_9380; } } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).is_rep[state as usize] = (*coder).is_rep[state as usize] - - ((*coder).is_rep[state as usize] >> RC_MOVE_BITS); + let is_rep_prob = decoder_is_rep_prob(coder, state); + *is_rep_prob = *is_rep_prob - (*is_rep_prob >> RC_MOVE_BITS); if !dict_is_distance_valid(::core::ptr::addr_of_mut!(dict), 0) { - ret_0 = LZMA_DATA_ERROR; - current_block = 4609795085482299213; + ret = LZMA_DATA_ERROR; + block_state = BLOCK_RETURN; continue 'c_9380; } else { if rc.range < RC_TOP_VALUE as u32 { @@ -2459,39 +1917,31 @@ unsafe extern "C" fn lzma_decode( rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; rc_in_ptr = rc_in_ptr.offset(1); } + let is_rep0_prob = decoder_is_rep0_prob(coder, state); rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul((*coder).is_rep0[state as usize] as u32); + .wrapping_mul(*is_rep0_prob as u32); if rc.code < rc_bound { rc.range = rc_bound; - (*coder).is_rep0[state as usize] = - ((*coder).is_rep0[state as usize] as u32).wrapping_add( - RC_BIT_MODEL_TOTAL - .wrapping_sub((*coder).is_rep0[state as usize] as u32) - >> RC_MOVE_BITS, - ) as probability - as probability; + *is_rep0_prob = (*is_rep0_prob as u32).wrapping_add( + RC_BIT_MODEL_TOTAL.wrapping_sub(*is_rep0_prob as u32) + >> RC_MOVE_BITS, + ) as probability; if rc.range < RC_TOP_VALUE as u32 { rc.range <<= RC_SHIFT_BITS; rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; rc_in_ptr = rc_in_ptr.offset(1); } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).is_rep0_long[state as usize][pos_state as usize] - as u32, - ); + let is_rep0_long_prob = + decoder_is_rep0_long_row(coder, state).add(pos_state as usize); + rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) + .wrapping_mul(*is_rep0_long_prob as u32); if rc.code < rc_bound { rc.range = rc_bound; - (*coder).is_rep0_long[state as usize][pos_state as usize] = - ((*coder).is_rep0_long[state as usize][pos_state as usize] - as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).is_rep0_long[state as usize] - [pos_state as usize] - as u32, - ) >> RC_MOVE_BITS, - ) as probability - as probability; + *is_rep0_long_prob = (*is_rep0_long_prob as u32).wrapping_add( + RC_BIT_MODEL_TOTAL.wrapping_sub(*is_rep0_long_prob as u32) + >> RC_MOVE_BITS, + ) + as probability; state = (if state < LIT_STATES { STATE_LIT_SHORTREP } else { @@ -2505,59 +1955,50 @@ unsafe extern "C" fn lzma_decode( } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).is_rep0_long[state as usize][pos_state as usize] = - (*coder).is_rep0_long[state as usize][pos_state as usize] - - ((*coder).is_rep0_long[state as usize] - [pos_state as usize] - >> RC_MOVE_BITS); + *is_rep0_long_prob = + *is_rep0_long_prob - (*is_rep0_long_prob >> RC_MOVE_BITS); } } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).is_rep0[state as usize] = (*coder).is_rep0[state as usize] - - ((*coder).is_rep0[state as usize] >> RC_MOVE_BITS); + *is_rep0_prob = *is_rep0_prob - (*is_rep0_prob >> RC_MOVE_BITS); if rc.range < RC_TOP_VALUE as u32 { rc.range <<= RC_SHIFT_BITS; rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; rc_in_ptr = rc_in_ptr.offset(1); } + let is_rep1_prob = decoder_is_rep1_prob(coder, state); rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul((*coder).is_rep1[state as usize] as u32); + .wrapping_mul(*is_rep1_prob as u32); if rc.code < rc_bound { rc.range = rc_bound; - (*coder).is_rep1[state as usize] = - ((*coder).is_rep1[state as usize] as u32).wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).is_rep1[state as usize] as u32, - ) >> RC_MOVE_BITS, - ) as probability - as probability; + *is_rep1_prob = (*is_rep1_prob as u32).wrapping_add( + RC_BIT_MODEL_TOTAL.wrapping_sub(*is_rep1_prob as u32) + >> RC_MOVE_BITS, + ) + as probability; let distance: u32 = rep1; rep1 = rep0; rep0 = distance; } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).is_rep1[state as usize] = (*coder).is_rep1 - [state as usize] - - ((*coder).is_rep1[state as usize] >> RC_MOVE_BITS); + *is_rep1_prob = *is_rep1_prob - (*is_rep1_prob >> RC_MOVE_BITS); if rc.range < RC_TOP_VALUE as u32 { rc.range <<= RC_SHIFT_BITS; rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; rc_in_ptr = rc_in_ptr.offset(1); } + let is_rep2_prob = decoder_is_rep2_prob(coder, state); rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul((*coder).is_rep2[state as usize] as u32); + .wrapping_mul(*is_rep2_prob as u32); if rc.code < rc_bound { rc.range = rc_bound; - (*coder).is_rep2[state as usize] = - ((*coder).is_rep2[state as usize] as u32).wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).is_rep2[state as usize] as u32, - ) >> RC_MOVE_BITS, - ) - as probability - as probability; + *is_rep2_prob = (*is_rep2_prob as u32).wrapping_add( + RC_BIT_MODEL_TOTAL.wrapping_sub(*is_rep2_prob as u32) + >> RC_MOVE_BITS, + ) + as probability; let distance_0: u32 = rep2; rep2 = rep1; rep1 = rep0; @@ -2565,9 +2006,8 @@ unsafe extern "C" fn lzma_decode( } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).is_rep2[state as usize] = (*coder).is_rep2 - [state as usize] - - ((*coder).is_rep2[state as usize] >> RC_MOVE_BITS); + *is_rep2_prob = + *is_rep2_prob - (*is_rep2_prob >> RC_MOVE_BITS); let distance_1: u32 = rep3; rep3 = rep2; rep2 = rep1; @@ -2588,530 +2028,137 @@ unsafe extern "C" fn lzma_decode( rc_in_ptr = rc_in_ptr.offset(1); } rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul((*coder).rep_len_decoder.choice as u32); + .wrapping_mul((*rep_len_decoder).choice as u32); if rc.code < rc_bound { rc.range = rc_bound; - (*coder).rep_len_decoder.choice = - ((*coder).rep_len_decoder.choice as u32).wrapping_add( - RC_BIT_MODEL_TOTAL - .wrapping_sub((*coder).rep_len_decoder.choice as u32) - >> RC_MOVE_BITS, - ) as probability - as probability; + prob_update_0(::core::ptr::addr_of_mut!((*rep_len_decoder).choice)); symbol = 1; - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).rep_len_decoder.low[pos_state as usize] - [symbol as usize] - as u32, + let rep_len_low = length_low_row(rep_len_decoder, pos_state); + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + rep_len_low.add(symbol as usize), + symbol ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).rep_len_decoder.low[pos_state as usize] - [symbol as usize] = - ((*coder).rep_len_decoder.low[pos_state as usize] - [symbol as usize] - as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).rep_len_decoder.low[pos_state as usize] - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).rep_len_decoder.low[pos_state as usize] - [symbol as usize] = (*coder).rep_len_decoder.low - [pos_state as usize] - [symbol as usize] - - ((*coder).rep_len_decoder.low[pos_state as usize] - [symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).rep_len_decoder.low[pos_state as usize] - [symbol as usize] - as u32, + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + rep_len_low.add(symbol as usize), + symbol ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).rep_len_decoder.low[pos_state as usize] - [symbol as usize] = - ((*coder).rep_len_decoder.low[pos_state as usize] - [symbol as usize] - as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).rep_len_decoder.low[pos_state as usize] - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).rep_len_decoder.low[pos_state as usize] - [symbol as usize] = (*coder).rep_len_decoder.low - [pos_state as usize] - [symbol as usize] - - ((*coder).rep_len_decoder.low[pos_state as usize] - [symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).rep_len_decoder.low[pos_state as usize] - [symbol as usize] - as u32, + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + rep_len_low.add(symbol as usize), + symbol ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).rep_len_decoder.low[pos_state as usize] - [symbol as usize] = - ((*coder).rep_len_decoder.low[pos_state as usize] - [symbol as usize] - as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).rep_len_decoder.low[pos_state as usize] - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).rep_len_decoder.low[pos_state as usize] - [symbol as usize] = (*coder).rep_len_decoder.low - [pos_state as usize] - [symbol as usize] - - ((*coder).rep_len_decoder.low[pos_state as usize] - [symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } symbol = symbol.wrapping_add((-(1_i32 << 3) + 2) as u32); len = symbol; } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).rep_len_decoder.choice = (*coder).rep_len_decoder.choice - - ((*coder).rep_len_decoder.choice >> RC_MOVE_BITS); - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } + prob_update_1(::core::ptr::addr_of_mut!((*rep_len_decoder).choice)); + rc_normalize!(rc, rc_in_ptr); rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) - .wrapping_mul((*coder).rep_len_decoder.choice2 as u32); + .wrapping_mul((*rep_len_decoder).choice2 as u32); if rc.code < rc_bound { rc.range = rc_bound; - (*coder).rep_len_decoder.choice2 = - ((*coder).rep_len_decoder.choice2 as u32).wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).rep_len_decoder.choice2 as u32, - ) >> RC_MOVE_BITS, - ) as probability - as probability; + prob_update_0(::core::ptr::addr_of_mut!( + (*rep_len_decoder).choice2 + )); symbol = 1; - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).rep_len_decoder.mid[pos_state as usize] - [symbol as usize] - as u32, + let rep_len_mid = length_mid_row(rep_len_decoder, pos_state); + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + rep_len_mid.add(symbol as usize), + symbol ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).rep_len_decoder.mid[pos_state as usize] - [symbol as usize] = ((*coder).rep_len_decoder.mid - [pos_state as usize] - [symbol as usize] - as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).rep_len_decoder.mid[pos_state as usize] - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).rep_len_decoder.mid[pos_state as usize] - [symbol as usize] = (*coder).rep_len_decoder.mid - [pos_state as usize] - [symbol as usize] - - ((*coder).rep_len_decoder.mid[pos_state as usize] - [symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).rep_len_decoder.mid[pos_state as usize] - [symbol as usize] - as u32, + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + rep_len_mid.add(symbol as usize), + symbol ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).rep_len_decoder.mid[pos_state as usize] - [symbol as usize] = ((*coder).rep_len_decoder.mid - [pos_state as usize] - [symbol as usize] - as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).rep_len_decoder.mid[pos_state as usize] - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).rep_len_decoder.mid[pos_state as usize] - [symbol as usize] = (*coder).rep_len_decoder.mid - [pos_state as usize] - [symbol as usize] - - ((*coder).rep_len_decoder.mid[pos_state as usize] - [symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).rep_len_decoder.mid[pos_state as usize] - [symbol as usize] - as u32, + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + rep_len_mid.add(symbol as usize), + symbol ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).rep_len_decoder.mid[pos_state as usize] - [symbol as usize] = ((*coder).rep_len_decoder.mid - [pos_state as usize] - [symbol as usize] - as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).rep_len_decoder.mid[pos_state as usize] - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).rep_len_decoder.mid[pos_state as usize] - [symbol as usize] = (*coder).rep_len_decoder.mid - [pos_state as usize] - [symbol as usize] - - ((*coder).rep_len_decoder.mid[pos_state as usize] - [symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } symbol = symbol.wrapping_add((-(1_i32 << 3) + 2 + (1 << 3)) as u32); len = symbol; } else { rc.range = rc.range.wrapping_sub(rc_bound); rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).rep_len_decoder.choice2 = - (*coder).rep_len_decoder.choice2 - - ((*coder).rep_len_decoder.choice2 >> RC_MOVE_BITS); + prob_update_1(::core::ptr::addr_of_mut!( + (*rep_len_decoder).choice2 + )); symbol = 1; - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).rep_len_decoder.high[symbol as usize] as u32, + let rep_len_high = length_high_probs(rep_len_decoder); + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + rep_len_high.add(symbol as usize), + symbol ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).rep_len_decoder.high[symbol as usize] = - ((*coder).rep_len_decoder.high[symbol as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).rep_len_decoder.high - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).rep_len_decoder.high[symbol as usize] = - (*coder).rep_len_decoder.high[symbol as usize] - - ((*coder).rep_len_decoder.high[symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).rep_len_decoder.high[symbol as usize] as u32, + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + rep_len_high.add(symbol as usize), + symbol ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).rep_len_decoder.high[symbol as usize] = - ((*coder).rep_len_decoder.high[symbol as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).rep_len_decoder.high - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).rep_len_decoder.high[symbol as usize] = - (*coder).rep_len_decoder.high[symbol as usize] - - ((*coder).rep_len_decoder.high[symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).rep_len_decoder.high[symbol as usize] as u32, + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + rep_len_high.add(symbol as usize), + symbol ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).rep_len_decoder.high[symbol as usize] = - ((*coder).rep_len_decoder.high[symbol as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).rep_len_decoder.high - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).rep_len_decoder.high[symbol as usize] = - (*coder).rep_len_decoder.high[symbol as usize] - - ((*coder).rep_len_decoder.high[symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).rep_len_decoder.high[symbol as usize] as u32, + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + rep_len_high.add(symbol as usize), + symbol ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).rep_len_decoder.high[symbol as usize] = - ((*coder).rep_len_decoder.high[symbol as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).rep_len_decoder.high - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).rep_len_decoder.high[symbol as usize] = - (*coder).rep_len_decoder.high[symbol as usize] - - ((*coder).rep_len_decoder.high[symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).rep_len_decoder.high[symbol as usize] as u32, + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + rep_len_high.add(symbol as usize), + symbol ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).rep_len_decoder.high[symbol as usize] = - ((*coder).rep_len_decoder.high[symbol as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).rep_len_decoder.high - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).rep_len_decoder.high[symbol as usize] = - (*coder).rep_len_decoder.high[symbol as usize] - - ((*coder).rep_len_decoder.high[symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).rep_len_decoder.high[symbol as usize] as u32, + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + rep_len_high.add(symbol as usize), + symbol ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).rep_len_decoder.high[symbol as usize] = - ((*coder).rep_len_decoder.high[symbol as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).rep_len_decoder.high - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).rep_len_decoder.high[symbol as usize] = - (*coder).rep_len_decoder.high[symbol as usize] - - ((*coder).rep_len_decoder.high[symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).rep_len_decoder.high[symbol as usize] as u32, + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + rep_len_high.add(symbol as usize), + symbol ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).rep_len_decoder.high[symbol as usize] = - ((*coder).rep_len_decoder.high[symbol as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).rep_len_decoder.high - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).rep_len_decoder.high[symbol as usize] = - (*coder).rep_len_decoder.high[symbol as usize] - - ((*coder).rep_len_decoder.high[symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } - if rc.range < RC_TOP_VALUE as u32 { - rc.range <<= RC_SHIFT_BITS; - rc.code = rc.code << RC_SHIFT_BITS | *rc_in_ptr as u32; - rc_in_ptr = rc_in_ptr.offset(1); - } - rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul( - (*coder).rep_len_decoder.high[symbol as usize] as u32, + rc_normalize!(rc, rc_in_ptr); + rc_bittree_step!( + rc, + rc_bound, + rep_len_high.add(symbol as usize), + symbol ); - if rc.code < rc_bound { - rc.range = rc_bound; - (*coder).rep_len_decoder.high[symbol as usize] = - ((*coder).rep_len_decoder.high[symbol as usize] as u32) - .wrapping_add( - RC_BIT_MODEL_TOTAL.wrapping_sub( - (*coder).rep_len_decoder.high - [symbol as usize] - as u32, - ) >> RC_MOVE_BITS, - ) - as probability - as probability; - symbol <<= 1; - } else { - rc.range = rc.range.wrapping_sub(rc_bound); - rc.code = rc.code.wrapping_sub(rc_bound); - (*coder).rep_len_decoder.high[symbol as usize] = - (*coder).rep_len_decoder.high[symbol as usize] - - ((*coder).rep_len_decoder.high[symbol as usize] - >> RC_MOVE_BITS); - symbol = (symbol << 1).wrapping_add(1); - } symbol = symbol.wrapping_add( (-(1_i32 << 8) + 2 + (1 << 3) + (1 << 3)) as u32, ); @@ -3128,41 +2175,41 @@ unsafe extern "C" fn lzma_decode( continue; } (*coder).sequence = SEQ_COPY; - current_block = 4609795085482299213; + block_state = BLOCK_RETURN; continue 'c_9380; } }, - 16690975975023747857 => { + BLOCK_LEN_BITTREE_INIT => { symbol = 1; - current_block = 2467942631393454738; + block_state = BLOCK_REP_LEN_BITTREE; continue; } - 15498320742470848828 => { + BLOCK_REP_LEN_PREPARE => { state = (if state < LIT_STATES { STATE_LIT_LONGREP } else { STATE_NONLIT_REP }) as u32; - current_block = 12043352250568755004; + block_state = BLOCK_REP_LEN_CHOICE; continue; } - 8485842003490715114 => { + BLOCK_DIST_SLOT_INIT => { symbol = 1; - current_block = 592696588731961849; + block_state = BLOCK_MATCH_LEN_BITTREE; continue; } _ => {} } if eopm_is_valid { - current_block = 7073645523065812117; + block_state = BLOCK_EOPM; continue; } - ret_0 = LZMA_DATA_ERROR; - current_block = 4609795085482299213; + ret = LZMA_DATA_ERROR; + block_state = BLOCK_RETURN; } (*dictptr).full = dict.full; (*coder).rc = rc; - *in_pos = rc_in_ptr.offset_from(in_0) as size_t; + *in_pos = rc_in_ptr.offset_from(input) as size_t; (*coder).state = state as lzma_lzma_state; (*coder).rep0 = rep0; (*coder).rep1 = rep1; @@ -3178,23 +2225,23 @@ unsafe extern "C" fn lzma_decode( .uncompressed_size .wrapping_sub(dict.pos.wrapping_sub(dict_start) as lzma_vli); if (*coder).uncompressed_size == 0 - && ret_0 == LZMA_OK + && ret == LZMA_OK && ((*coder).sequence == SEQ_LITERAL_WRITE || (*coder).sequence == SEQ_SHORTREP || (*coder).sequence == SEQ_COPY) { - ret_0 = LZMA_DATA_ERROR; + ret = LZMA_DATA_ERROR; } } - if ret_0 == LZMA_STREAM_END { + if ret == LZMA_STREAM_END { (*coder).rc.range = UINT32_MAX; (*coder).rc.code = 0; (*coder).rc.init_bytes_left = 5; (*coder).sequence = SEQ_IS_MATCH; } - ret_0 + ret } -unsafe extern "C" fn lzma_decoder_uncompressed( +unsafe fn lzma_decoder_uncompressed( coder_ptr: *mut c_void, uncompressed_size: lzma_vli, allow_eopm: bool, @@ -3203,7 +2250,7 @@ unsafe extern "C" fn lzma_decoder_uncompressed( (*coder).uncompressed_size = uncompressed_size; (*coder).allow_eopm = allow_eopm; } -unsafe extern "C" fn lzma_decoder_reset(coder_ptr: *mut c_void, opt: *const c_void) { +unsafe fn lzma_decoder_reset(coder_ptr: *mut c_void, opt: *const c_void) { let coder: *mut lzma_lzma1_decoder = coder_ptr as *mut lzma_lzma1_decoder; let options: *const lzma_options_lzma = opt as *const lzma_options_lzma; (*coder).pos_mask = (1u32 << (*options).pb).wrapping_sub(1) as u32; @@ -3219,43 +2266,47 @@ unsafe extern "C" fn lzma_decoder_reset(coder_ptr: *mut c_void, opt: *const c_vo (*coder).rep1 = 0; (*coder).rep2 = 0; (*coder).rep3 = 0; - (*coder).pos_mask = (1u32 << (*options).pb).wrapping_sub(1) as u32; (*coder).rc.range = UINT32_MAX; (*coder).rc.code = 0; (*coder).rc.init_bytes_left = 5; + let match_len_decoder: *mut lzma_length_decoder = + ::core::ptr::addr_of_mut!((*coder).match_len_decoder); + let rep_len_decoder: *mut lzma_length_decoder = + ::core::ptr::addr_of_mut!((*coder).rep_len_decoder); let mut i: u32 = 0; while i < STATES { + let is_match = decoder_is_match_row(coder, i); + let is_rep0_long = decoder_is_rep0_long_row(coder, i); let mut j: u32 = 0; while j <= (*coder).pos_mask { - (*coder).is_match[i as usize][j as usize] = (RC_BIT_MODEL_TOTAL >> 1) as probability; - (*coder).is_rep0_long[i as usize][j as usize] = - (RC_BIT_MODEL_TOTAL >> 1) as probability; + *is_match.add(j as usize) = (RC_BIT_MODEL_TOTAL >> 1) as probability; + *is_rep0_long.add(j as usize) = (RC_BIT_MODEL_TOTAL >> 1) as probability; j += 1; } - (*coder).is_rep[i as usize] = (RC_BIT_MODEL_TOTAL >> 1) as probability; - (*coder).is_rep0[i as usize] = (RC_BIT_MODEL_TOTAL >> 1) as probability; - (*coder).is_rep1[i as usize] = (RC_BIT_MODEL_TOTAL >> 1) as probability; - (*coder).is_rep2[i as usize] = (RC_BIT_MODEL_TOTAL >> 1) as probability; + *decoder_is_rep_prob(coder, i) = (RC_BIT_MODEL_TOTAL >> 1) as probability; + *decoder_is_rep0_prob(coder, i) = (RC_BIT_MODEL_TOTAL >> 1) as probability; + *decoder_is_rep1_prob(coder, i) = (RC_BIT_MODEL_TOTAL >> 1) as probability; + *decoder_is_rep2_prob(coder, i) = (RC_BIT_MODEL_TOTAL >> 1) as probability; i += 1; } - let mut i_0: u32 = 0; - while i_0 < DIST_STATES { + let mut dist_state: u32 = 0; + while dist_state < DIST_STATES { + let dist_slot = decoder_dist_slot_row(coder, dist_state); let mut bt_i: u32 = 0; while bt_i < (1 << 6) as u32 { - (*coder).dist_slot[i_0 as usize][bt_i as usize] = - (RC_BIT_MODEL_TOTAL >> 1) as probability; + *dist_slot.add(bt_i as usize) = (RC_BIT_MODEL_TOTAL >> 1) as probability; bt_i += 1; } - i_0 += 1; + dist_state += 1; } - let mut i_1: u32 = 0; - while i_1 < (FULL_DISTANCES - DIST_MODEL_END) as u32 { - (*coder).pos_special[i_1 as usize] = (RC_BIT_MODEL_TOTAL >> 1) as probability; - i_1 += 1; + let mut special_distance: u32 = 0; + while special_distance < (FULL_DISTANCES - DIST_MODEL_END) as u32 { + (*coder).pos_special[special_distance as usize] = (RC_BIT_MODEL_TOTAL >> 1) as probability; + special_distance += 1; } let mut bt_i_0: u32 = 0; while bt_i_0 < (1 << 4) as u32 { - (*coder).pos_align[bt_i_0 as usize] = (RC_BIT_MODEL_TOTAL >> 1) as probability; + *decoder_pos_align_prob(coder, bt_i_0) = (RC_BIT_MODEL_TOTAL >> 1) as probability; bt_i_0 += 1; } let num_pos_states: u32 = 1 << (*options).pb; @@ -3265,40 +2316,42 @@ unsafe extern "C" fn lzma_decoder_reset(coder_ptr: *mut c_void, opt: *const c_vo (*coder).rep_len_decoder.choice2 = (RC_BIT_MODEL_TOTAL >> 1) as probability; let mut pos_state: u32 = 0; while pos_state < num_pos_states { + let match_len_low = length_low_row(match_len_decoder, pos_state); + let match_len_mid = length_mid_row(match_len_decoder, pos_state); + let rep_len_low = length_low_row(rep_len_decoder, pos_state); + let rep_len_mid = length_mid_row(rep_len_decoder, pos_state); let mut bt_i_1: u32 = 0; while bt_i_1 < (1 << 3) as u32 { - (*coder).match_len_decoder.low[pos_state as usize][bt_i_1 as usize] = - (RC_BIT_MODEL_TOTAL >> 1) as probability; + *match_len_low.add(bt_i_1 as usize) = (RC_BIT_MODEL_TOTAL >> 1) as probability; bt_i_1 += 1; } let mut bt_i_2: u32 = 0; while bt_i_2 < (1 << 3) as u32 { - (*coder).match_len_decoder.mid[pos_state as usize][bt_i_2 as usize] = - (RC_BIT_MODEL_TOTAL >> 1) as probability; + *match_len_mid.add(bt_i_2 as usize) = (RC_BIT_MODEL_TOTAL >> 1) as probability; bt_i_2 += 1; } let mut bt_i_3: u32 = 0; while bt_i_3 < (1 << 3) as u32 { - (*coder).rep_len_decoder.low[pos_state as usize][bt_i_3 as usize] = - (RC_BIT_MODEL_TOTAL >> 1) as probability; + *rep_len_low.add(bt_i_3 as usize) = (RC_BIT_MODEL_TOTAL >> 1) as probability; bt_i_3 += 1; } let mut bt_i_4: u32 = 0; while bt_i_4 < (1 << 3) as u32 { - (*coder).rep_len_decoder.mid[pos_state as usize][bt_i_4 as usize] = - (RC_BIT_MODEL_TOTAL >> 1) as probability; + *rep_len_mid.add(bt_i_4 as usize) = (RC_BIT_MODEL_TOTAL >> 1) as probability; bt_i_4 += 1; } pos_state += 1; } + let match_len_high = length_high_probs(match_len_decoder); let mut bt_i_5: u32 = 0; while bt_i_5 < (1 << 8) as u32 { - (*coder).match_len_decoder.high[bt_i_5 as usize] = (RC_BIT_MODEL_TOTAL >> 1) as probability; + *match_len_high.add(bt_i_5 as usize) = (RC_BIT_MODEL_TOTAL >> 1) as probability; bt_i_5 += 1; } + let rep_len_high = length_high_probs(rep_len_decoder); let mut bt_i_6: u32 = 0; while bt_i_6 < (1 << 8) as u32 { - (*coder).rep_len_decoder.high[bt_i_6 as usize] = (RC_BIT_MODEL_TOTAL >> 1) as probability; + *rep_len_high.add(bt_i_6 as usize) = (RC_BIT_MODEL_TOTAL >> 1) as probability; bt_i_6 += 1; } (*coder).sequence = SEQ_IS_MATCH; @@ -3320,28 +2373,17 @@ pub unsafe fn lzma_lzma_decoder_create( if (*lz).coder.is_null() { return LZMA_MEM_ERROR; } - (*lz).code = Some( - lzma_decode - as unsafe extern "C" fn( - *mut c_void, - *mut lzma_dict, - *const u8, - *mut size_t, - size_t, - ) -> lzma_ret, - ); - (*lz).reset = - Some(lzma_decoder_reset as unsafe extern "C" fn(*mut c_void, *const c_void) -> ()); - (*lz).set_uncompressed = Some( - lzma_decoder_uncompressed as unsafe extern "C" fn(*mut c_void, lzma_vli, bool) -> (), - ); + (*lz).code = lzma_decode as lzma_lz_decoder_code_function; + (*lz).reset = Some(lzma_decoder_reset as unsafe fn(*mut c_void, *const c_void) -> ()); + (*lz).set_uncompressed = + Some(lzma_decoder_uncompressed as unsafe fn(*mut c_void, lzma_vli, bool) -> ()); } (*lz_options).dict_size = (*options).dict_size as size_t; (*lz_options).preset_dict = (*options).preset_dict; (*lz_options).preset_dict_size = (*options).preset_dict_size as size_t; LZMA_OK } -unsafe extern "C" fn lzma_decoder_init( +unsafe fn lzma_decoder_init( lz: *mut lzma_lz_decoder, allocator: *const lzma_allocator, id: lzma_vli, @@ -3363,20 +2405,20 @@ unsafe extern "C" fn lzma_decoder_init( allow_eopm = (*opt).ext_flags & LZMA_LZMA1EXT_ALLOW_EOPM as u32 != 0 || uncomp_size == LZMA_VLI_UNKNOWN; } - let ret_: lzma_ret = lzma_lzma_decoder_create( + let ret: lzma_ret = lzma_lzma_decoder_create( lz, allocator, options as *const lzma_options_lzma, lz_options, ); - if ret_ != LZMA_OK { - return ret_; + if ret != LZMA_OK { + return ret; } lzma_decoder_reset((*lz).coder, options); lzma_decoder_uncompressed((*lz).coder, uncomp_size, allow_eopm); LZMA_OK } -pub(crate) unsafe extern "C" fn lzma_lzma_decoder_init( +pub(crate) unsafe fn lzma_lzma_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, @@ -3385,16 +2427,14 @@ pub(crate) unsafe extern "C" fn lzma_lzma_decoder_init( next, allocator, filters, - Some( - lzma_decoder_init - as unsafe extern "C" fn( - *mut lzma_lz_decoder, - *const lzma_allocator, - lzma_vli, - *const c_void, - *mut lzma_lz_options, - ) -> lzma_ret, - ), + lzma_decoder_init + as unsafe fn( + *mut lzma_lz_decoder, + *const lzma_allocator, + lzma_vli, + *const c_void, + *mut lzma_lz_options, + ) -> lzma_ret, ) } pub unsafe fn lzma_lzma_lclppb_decode(options: *mut lzma_options_lzma, mut byte: u8) -> bool { @@ -3407,20 +2447,18 @@ pub unsafe fn lzma_lzma_lclppb_decode(options: *mut lzma_options_lzma, mut byte: (*options).lc = (byte as u32).wrapping_sub((*options).lp.wrapping_mul(9)); (*options).lc.wrapping_add((*options).lp) > LZMA_LCLP_MAX } -pub(crate) extern "C" fn lzma_lzma_decoder_memusage_nocheck(options: *const c_void) -> u64 { - return unsafe { - let opt: *const lzma_options_lzma = options as *const lzma_options_lzma; - (core::mem::size_of::() as u64) - .wrapping_add(lzma_lz_decoder_memusage((*opt).dict_size as size_t)) - }; +pub(crate) unsafe fn lzma_lzma_decoder_memusage_nocheck(options: *const c_void) -> u64 { + let opt: *const lzma_options_lzma = options as *const lzma_options_lzma; + (core::mem::size_of::() as u64) + .wrapping_add(lzma_lz_decoder_memusage((*opt).dict_size as size_t)) } -pub(crate) extern "C" fn lzma_lzma_decoder_memusage(options: *const c_void) -> u64 { - if !unsafe { is_lclppb_valid(options as *const lzma_options_lzma) } { +pub(crate) unsafe fn lzma_lzma_decoder_memusage(options: *const c_void) -> u64 { + if !is_lclppb_valid(options as *const lzma_options_lzma) { return UINT64_MAX; } lzma_lzma_decoder_memusage_nocheck(options) } -pub(crate) unsafe extern "C" fn lzma_lzma_props_decode( +pub(crate) unsafe fn lzma_lzma_props_decode( options: *mut *mut c_void, allocator: *const lzma_allocator, props: *const u8, @@ -3438,7 +2476,7 @@ pub(crate) unsafe extern "C" fn lzma_lzma_props_decode( lzma_free(opt as *mut c_void, allocator); return LZMA_OPTIONS_ERROR; } else { - (*opt).dict_size = read32le(props.offset(1)); + (*opt).dict_size = read32le(&*props.add(1).cast::<[u8; 4]>()); (*opt).preset_dict = core::ptr::null(); (*opt).preset_dict_size = 0; *options = opt as *mut c_void; diff --git a/xz/src/lzma/lzma_encoder.rs b/xz/src/lzma/lzma_encoder.rs index dfa96664..75d474f2 100644 --- a/xz/src/lzma/lzma_encoder.rs +++ b/xz/src/lzma/lzma_encoder.rs @@ -41,6 +41,51 @@ unsafe fn coder_rep_slot_mut(coder: *mut lzma_lzma1_encoder, index: usize) -> *m (::core::ptr::addr_of_mut!((*coder).reps) as *mut u32).add(index) } #[inline(always)] +unsafe fn encoder_is_match_row(coder: *mut lzma_lzma1_encoder, state: u32) -> *mut probability { + debug_assert!((state as usize) < STATES as usize); + ::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*coder).is_match) as *mut [probability; 16]) + .add(state as usize) + ) as *mut probability +} +#[inline(always)] +unsafe fn encoder_is_rep0_long_row(coder: *mut lzma_lzma1_encoder, state: u32) -> *mut probability { + debug_assert!((state as usize) < STATES as usize); + ::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*coder).is_rep0_long) as *mut [probability; 16]) + .add(state as usize) + ) as *mut probability +} +#[inline(always)] +unsafe fn encoder_is_rep_slot( + probs: *mut [probability; STATES as usize], + state: u32, +) -> *mut probability { + debug_assert!((state as usize) < STATES as usize); + (probs as *mut probability).add(state as usize) +} +#[inline(always)] +unsafe fn encoder_dist_special_prob( + coder: *mut lzma_lzma1_encoder, + index: usize, +) -> *mut probability { + debug_assert!(index < (FULL_DISTANCES - DIST_MODEL_END) as usize); + (::core::ptr::addr_of_mut!((*coder).dist_special) as *mut probability).add(index) +} +#[inline(always)] +unsafe fn encoder_dist_slot_row(coder: *mut lzma_lzma1_encoder, state: u32) -> *mut probability { + debug_assert!((state as usize) < DIST_STATES as usize); + ::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*coder).dist_slot) as *mut [probability; 64]) + .add(state as usize) + ) as *mut probability +} +#[inline(always)] +unsafe fn encoder_dist_align_prob(coder: *mut lzma_lzma1_encoder, index: u32) -> *mut probability { + debug_assert!((index as usize) < ALIGN_SIZE as usize); + (::core::ptr::addr_of_mut!((*coder).dist_align) as *mut probability).add(index as usize) +} +#[inline(always)] unsafe fn rc_bittree( rc: *mut lzma_range_encoder, probs: *mut probability, @@ -269,6 +314,8 @@ unsafe fn rc_encode( } #[inline] unsafe fn rc_encode_dummy(rc: *const lzma_range_encoder, out_limit: u64) -> bool { + let symbols = ::core::ptr::addr_of!((*rc).symbols) as *const rc_symbol; + let probs = ::core::ptr::addr_of!((*rc).probs) as *const *mut probability; let mut low: u64 = (*rc).low; let mut cache_size: u64 = (*rc).cache_size; let mut range: u32 = (*rc).range; @@ -291,13 +338,13 @@ unsafe fn rc_encode_dummy(rc: *const lzma_range_encoder, out_limit: u64) -> bool if pos == (*rc).count { break; } - match (*rc).symbols[pos as usize] { + match *symbols.add(pos) { 0 => { - let prob: probability = *(*rc).probs[pos as usize]; + let prob: probability = **probs.add(pos); range = (range >> RC_BIT_MODEL_TOTAL_BITS).wrapping_mul(prob as u32); } 1 => { - let prob_0: probability = *(*rc).probs[pos as usize]; + let prob_0: probability = **probs.add(pos); let bound: u32 = (prob_0 as u32).wrapping_mul(range >> RC_BIT_MODEL_TOTAL_BITS); low = low.wrapping_add(bound as u64); range = range.wrapping_sub(bound); @@ -424,15 +471,20 @@ unsafe fn length_mid_probs(lc: *mut lzma_length_encoder, pos_state: u32) -> *mut ) as *mut probability } #[inline(always)] +unsafe fn length_high_probs(lc: *mut lzma_length_encoder) -> *mut probability { + ::core::ptr::addr_of_mut!((*lc).high) as *mut probability +} +#[inline(always)] unsafe fn length_counter(lc: *mut lzma_length_encoder, pos_state: u32) -> *mut u32 { debug_assert!((pos_state as usize) < (*lc).counters.len()); (::core::ptr::addr_of_mut!((*lc).counters) as *mut u32).add(pos_state as usize) } #[inline(always)] unsafe fn is_match_prob(coder: *mut lzma_lzma1_encoder, pos_state: u32) -> *mut probability { - (::core::ptr::addr_of_mut!(*(::core::ptr::addr_of_mut!((*coder).is_match) - as *mut [probability; 16]) - .add((*coder).state as usize)) as *mut probability) + (::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*coder).is_match) as *mut [probability; 16]) + .add((*coder).state as usize) + ) as *mut probability) .add(pos_state as usize) } #[inline(always)] @@ -495,8 +547,9 @@ unsafe fn length( } } if !fast_mode { - (*lc).counters[pos_state as usize] -= 1; - if (*lc).counters[pos_state as usize] == 0 { + let counter = length_counter(lc, pos_state); + *counter -= 1; + if *counter == 0 { length_update_prices(lc, pos_state); } } @@ -523,9 +576,10 @@ unsafe fn match_0(coder: *mut lzma_lzma1_encoder, pos_state: u32, distance: u32, }; rc_bittree( ::core::ptr::addr_of_mut!((*coder).rc), - ::core::ptr::addr_of_mut!(*(::core::ptr::addr_of_mut!((*coder).dist_slot) - as *mut [probability; 64]) - .offset(dist_state as isize)) as *mut probability, + ::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*coder).dist_slot) as *mut [probability; 64]) + .offset(dist_state as isize) + ) as *mut probability, DIST_SLOT_BITS, dist_slot, ); @@ -575,9 +629,10 @@ unsafe fn rep_match(coder: *mut lzma_lzma1_encoder, pos_state: u32, rep: u32, le ); rc_bit( ::core::ptr::addr_of_mut!((*coder).rc), - (::core::ptr::addr_of_mut!(*(::core::ptr::addr_of_mut!((*coder).is_rep0_long) - as *mut [probability; 16]) - .offset((*coder).state as isize)) as *mut probability) + (::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*coder).is_rep0_long) as *mut [probability; 16]) + .offset((*coder).state as isize) + ) as *mut probability) .offset(pos_state as isize) as *mut probability, (len != 1) as u32, ); @@ -739,16 +794,6 @@ unsafe fn finish_lzma_stream( } LZMA_STREAM_END } -#[cold] -#[inline(never)] -unsafe fn out_limit_reached(coder: *mut lzma_lzma1_encoder) -> bool { - let rc = ::core::ptr::addr_of_mut!((*coder).rc); - if (*coder).out_limit == 0 || !rc_encode_dummy(rc, (*coder).out_limit) { - return false; - } - rc_forget(rc); - true -} pub unsafe fn lzma_lzma_encode( coder: *mut lzma_lzma1_encoder, mf: *mut lzma_mf, @@ -801,7 +846,8 @@ pub unsafe fn lzma_lzma_encode( ); } encode_symbol(coder, mf, back, len, (*coder).uncomp_size as u32); - if out_limit_reached(coder) { + if (*coder).out_limit != 0 && rc_encode_dummy(rc, (*coder).out_limit) { + rc_forget(rc); break; } (*coder).uncomp_size += len as u64; @@ -811,7 +857,7 @@ pub unsafe fn lzma_lzma_encode( } finish_lzma_stream(coder, out, out_pos, out_size) } -unsafe extern "C" fn lzma_encode( +unsafe fn lzma_encode( coder: *mut c_void, mf: *mut lzma_mf, out: *mut u8, @@ -830,7 +876,7 @@ unsafe extern "C" fn lzma_encode( UINT32_MAX, ) } -unsafe extern "C" fn lzma_lzma_set_out_limit( +unsafe fn lzma_lzma_set_out_limit( coder_ptr: *mut c_void, uncomp_size: *mut u64, out_limit: u64, @@ -878,23 +924,24 @@ unsafe fn length_encoder_reset( (*lencoder).choice2 = (RC_BIT_MODEL_TOTAL >> 1) as probability; let mut pos_state: size_t = 0; while pos_state < num_pos_states as size_t { + let low = length_low_probs(lencoder, pos_state as u32); + let mid = length_mid_probs(lencoder, pos_state as u32); let mut bt_i: u32 = 0; while bt_i < (1 << 3) as u32 { - (*lencoder).low[pos_state as usize][bt_i as usize] = - (RC_BIT_MODEL_TOTAL >> 1) as probability; + *low.add(bt_i as usize) = (RC_BIT_MODEL_TOTAL >> 1) as probability; bt_i += 1; } let mut bt_i_0: u32 = 0; while bt_i_0 < (1 << 3) as u32 { - (*lencoder).mid[pos_state as usize][bt_i_0 as usize] = - (RC_BIT_MODEL_TOTAL >> 1) as probability; + *mid.add(bt_i_0 as usize) = (RC_BIT_MODEL_TOTAL >> 1) as probability; bt_i_0 += 1; } pos_state += 1; } + let high = length_high_probs(lencoder); let mut bt_i_1: u32 = 0; while bt_i_1 < (1 << 8) as u32 { - (*lencoder).high[bt_i_1 as usize] = (RC_BIT_MODEL_TOTAL >> 1) as probability; + *high.add(bt_i_1 as usize) = (RC_BIT_MODEL_TOTAL >> 1) as probability; bt_i_1 += 1; } if !fast_mode { @@ -919,7 +966,7 @@ pub unsafe fn lzma_lzma_encoder_reset( (*coder).state = STATE_LIT_LIT; let mut i: size_t = 0; while i < REPS as size_t { - (*coder).reps[i as usize] = 0; + *coder_rep_slot_mut(coder, i as usize) = 0; i += 1; } literal_init( @@ -929,37 +976,42 @@ pub unsafe fn lzma_lzma_encoder_reset( ); let mut i_0: size_t = 0; while i_0 < STATES as size_t { + let is_match = encoder_is_match_row(coder, i_0 as u32); + let is_rep0_long = encoder_is_rep0_long_row(coder, i_0 as u32); let mut j: size_t = 0; while j <= (*coder).pos_mask as size_t { - (*coder).is_match[i_0 as usize][j as usize] = (RC_BIT_MODEL_TOTAL >> 1) as probability; - (*coder).is_rep0_long[i_0 as usize][j as usize] = - (RC_BIT_MODEL_TOTAL >> 1) as probability; + *is_match.add(j as usize) = (RC_BIT_MODEL_TOTAL >> 1) as probability; + *is_rep0_long.add(j as usize) = (RC_BIT_MODEL_TOTAL >> 1) as probability; j += 1; } - (*coder).is_rep[i_0 as usize] = (RC_BIT_MODEL_TOTAL >> 1) as probability; - (*coder).is_rep0[i_0 as usize] = (RC_BIT_MODEL_TOTAL >> 1) as probability; - (*coder).is_rep1[i_0 as usize] = (RC_BIT_MODEL_TOTAL >> 1) as probability; - (*coder).is_rep2[i_0 as usize] = (RC_BIT_MODEL_TOTAL >> 1) as probability; + *encoder_is_rep_slot(::core::ptr::addr_of_mut!((*coder).is_rep), i_0 as u32) = + (RC_BIT_MODEL_TOTAL >> 1) as probability; + *encoder_is_rep_slot(::core::ptr::addr_of_mut!((*coder).is_rep0), i_0 as u32) = + (RC_BIT_MODEL_TOTAL >> 1) as probability; + *encoder_is_rep_slot(::core::ptr::addr_of_mut!((*coder).is_rep1), i_0 as u32) = + (RC_BIT_MODEL_TOTAL >> 1) as probability; + *encoder_is_rep_slot(::core::ptr::addr_of_mut!((*coder).is_rep2), i_0 as u32) = + (RC_BIT_MODEL_TOTAL >> 1) as probability; i_0 += 1; } let mut i_1: size_t = 0; while i_1 < (FULL_DISTANCES - DIST_MODEL_END) as size_t { - (*coder).dist_special[i_1 as usize] = (RC_BIT_MODEL_TOTAL >> 1) as probability; + *encoder_dist_special_prob(coder, i_1 as usize) = (RC_BIT_MODEL_TOTAL >> 1) as probability; i_1 += 1; } let mut i_2: size_t = 0; while i_2 < DIST_STATES as size_t { + let dist_slot = encoder_dist_slot_row(coder, i_2 as u32); let mut bt_i: u32 = 0; while bt_i < (1 << 6) as u32 { - (*coder).dist_slot[i_2 as usize][bt_i as usize] = - (RC_BIT_MODEL_TOTAL >> 1) as probability; + *dist_slot.add(bt_i as usize) = (RC_BIT_MODEL_TOTAL >> 1) as probability; bt_i += 1; } i_2 += 1; } let mut bt_i_0: u32 = 0; while bt_i_0 < (1 << 4) as u32 { - (*coder).dist_align[bt_i_0 as usize] = (RC_BIT_MODEL_TOTAL >> 1) as probability; + *encoder_dist_align_prob(coder, bt_i_0) = (RC_BIT_MODEL_TOTAL >> 1) as probability; bt_i_0 += 1; } length_encoder_reset( @@ -1032,7 +1084,7 @@ pub unsafe fn lzma_lzma_encoder_create( set_lz_options(lz_options, options); lzma_lzma_encoder_reset(coder, options) } -unsafe extern "C" fn lzma_encoder_init( +unsafe fn lzma_encoder_init( lz: *mut lzma_lz_encoder, allocator: *const lzma_allocator, id: lzma_vli, @@ -1042,19 +1094,9 @@ unsafe extern "C" fn lzma_encoder_init( if options.is_null() { return LZMA_PROG_ERROR; } - (*lz).code = Some( - lzma_encode - as unsafe extern "C" fn( - *mut c_void, - *mut lzma_mf, - *mut u8, - *mut size_t, - size_t, - ) -> lzma_ret, - ); - (*lz).set_out_limit = Some( - lzma_lzma_set_out_limit as unsafe extern "C" fn(*mut c_void, *mut u64, u64) -> lzma_ret, - ); + (*lz).code = lzma_encode as lzma_lz_encoder_code_function; + (*lz).set_out_limit = + Some(lzma_lzma_set_out_limit as unsafe fn(*mut c_void, *mut u64, u64) -> lzma_ret); lzma_lzma_encoder_create( ::core::ptr::addr_of_mut!((*lz).coder), allocator, @@ -1063,7 +1105,7 @@ unsafe extern "C" fn lzma_encoder_init( lz_options, ) } -pub(crate) unsafe extern "C" fn lzma_lzma_encoder_init( +pub(crate) unsafe fn lzma_lzma_encoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, @@ -1072,19 +1114,17 @@ pub(crate) unsafe extern "C" fn lzma_lzma_encoder_init( next, allocator, filters, - Some( - lzma_encoder_init - as unsafe extern "C" fn( - *mut lzma_lz_encoder, - *const lzma_allocator, - lzma_vli, - *const c_void, - *mut lzma_lz_options, - ) -> lzma_ret, - ), + lzma_encoder_init + as unsafe fn( + *mut lzma_lz_encoder, + *const lzma_allocator, + lzma_vli, + *const c_void, + *mut lzma_lz_options, + ) -> lzma_ret, ) } -pub(crate) extern "C" fn lzma_lzma_encoder_memusage(options: *const c_void) -> u64 { +pub(crate) unsafe fn lzma_lzma_encoder_memusage(options: *const c_void) -> u64 { if !is_options_valid(options as *const lzma_options_lzma) { return UINT64_MAX; } @@ -1116,10 +1156,7 @@ pub unsafe fn lzma_lzma_lclppb_encode(options: *const lzma_options_lzma, byte: * *byte = (((*options).pb * 5 + (*options).lp) * 9 + (*options).lc) as u8; false } -pub(crate) unsafe extern "C" fn lzma_lzma_props_encode( - options: *const c_void, - out: *mut u8, -) -> lzma_ret { +pub(crate) unsafe fn lzma_lzma_props_encode(options: *const c_void, out: *mut u8) -> lzma_ret { if options.is_null() { return LZMA_PROG_ERROR; } @@ -1127,7 +1164,7 @@ pub(crate) unsafe extern "C" fn lzma_lzma_props_encode( if lzma_lzma_lclppb_encode(opt, out) { return LZMA_PROG_ERROR; } - write32le(out.offset(1), (*opt).dict_size); + write32le(&mut *out.add(1).cast::<[u8; 4]>(), (*opt).dict_size); LZMA_OK } pub fn lzma_mode_is_supported(mode: lzma_mode) -> lzma_bool { diff --git a/xz/src/lzma/lzma_encoder_optimum_fast.rs b/xz/src/lzma/lzma_encoder_optimum_fast.rs index d1baa1b1..f52d7cd3 100644 --- a/xz/src/lzma/lzma_encoder_optimum_fast.rs +++ b/xz/src/lzma/lzma_encoder_optimum_fast.rs @@ -16,25 +16,10 @@ unsafe fn coder_match(coder: *const lzma_lzma1_encoder, index: u32) -> *const lz debug_assert!((index as usize) < (*coder).matches.len()); (::core::ptr::addr_of!((*coder).matches) as *const lzma_match).add(index as usize) } - -#[inline(always)] -unsafe fn mf_find_callback( - mf: *const lzma_mf, -) -> unsafe extern "C" fn(*mut lzma_mf, *mut lzma_match) -> u32 { - debug_assert!((*mf).find.is_some()); - match (*mf).find { - Some(find) => find, - None => core::hint::unreachable_unchecked(), - } -} - #[inline(always)] -unsafe fn mf_skip_callback(mf: *const lzma_mf) -> unsafe extern "C" fn(*mut lzma_mf, u32) -> () { - debug_assert!((*mf).skip.is_some()); - match (*mf).skip { - Some(skip) => skip, - None => core::hint::unreachable_unchecked(), - } +unsafe fn coder_rep(coder: *const lzma_lzma1_encoder, index: u32) -> u32 { + debug_assert!((index as usize) < REPS as usize); + *((::core::ptr::addr_of!((*coder).reps) as *const u32).add(index as usize)) } pub unsafe fn lzma_lzma_optimum_fast( @@ -43,8 +28,8 @@ pub unsafe fn lzma_lzma_optimum_fast( back_res: *mut u32, len_res: *mut u32, ) { - let mf_find = mf_find_callback(mf); - let mf_skip = mf_skip_callback(mf); + let mf_find = (*mf).find; + let mf_skip = (*mf).skip; let nice_len: u32 = (*mf).nice_len; let mut len_main: u32 = 0; let mut matches_count: u32 = 0; @@ -71,7 +56,7 @@ pub unsafe fn lzma_lzma_optimum_fast( let mut rep_index: u32 = 0; let mut i: u32 = 0; while i < REPS { - let buf_back: *const u8 = buf.offset(-((*coder).reps[i as usize] as isize)).offset(-1); + let buf_back: *const u8 = buf.offset(-(coder_rep(coder, i) as isize)).offset(-1); if not_equal_16(buf, buf_back) { i += 1; continue; @@ -152,8 +137,7 @@ pub unsafe fn lzma_lzma_optimum_fast( while i_0 < REPS { if memcmp( buf as *const c_void, - buf.offset(-((*coder).reps[i_0 as usize] as isize)) - .offset(-1) as *const c_void, + buf.offset(-(coder_rep(coder, i_0) as isize)).offset(-1) as *const c_void, limit as size_t, ) == 0 { diff --git a/xz/src/lzma/lzma_encoder_optimum_normal.rs b/xz/src/lzma/lzma_encoder_optimum_normal.rs index a00b49f1..e0e0c7c0 100644 --- a/xz/src/lzma/lzma_encoder_optimum_normal.rs +++ b/xz/src/lzma/lzma_encoder_optimum_normal.rs @@ -2,6 +2,11 @@ use crate::lzma::lzma_encoder::MATCH_LEN_MAX; use crate::types::*; pub const RC_BIT_PRICE_SHIFT_BITS: u32 = 4; pub const RC_INFINITY_PRICE: c_uint = 1u32 << 30; +#[inline(always)] +unsafe fn fastpos_at(index: usize) -> u8 { + debug_assert!(index < lzma_fastpos.len()); + *lzma_fastpos.as_ptr().add(index) +} #[inline] unsafe fn rc_bittree_reverse_price( probs: *const probability, @@ -29,16 +34,21 @@ fn rc_direct_price(bits: u32) -> u32 { #[inline] unsafe fn get_dist_slot_2(dist: u32) -> u32 { if dist < 1 << FASTPOS_BITS + (14 / 2 - 1 + 0 * (FASTPOS_BITS - 1)) { - return (lzma_fastpos[(dist >> 14 / 2 - 1 + 0 * (FASTPOS_BITS - 1)) as usize] as u32) + return (fastpos_at((dist >> 14 / 2 - 1 + 0 * (FASTPOS_BITS - 1)) as usize) as u32) + (2 * (14 / 2 - 1 + 0 * (FASTPOS_BITS - 1))) as u32; } if dist < 1 << FASTPOS_BITS + (14 / 2 - 1 + 1 * (FASTPOS_BITS - 1)) { - return (lzma_fastpos[(dist >> 14 / 2 - 1 + 1 * (FASTPOS_BITS - 1)) as usize] as u32) + return (fastpos_at((dist >> 14 / 2 - 1 + 1 * (FASTPOS_BITS - 1)) as usize) as u32) + (2 * (14 / 2 - 1 + 1 * (FASTPOS_BITS - 1))) as u32; } - (lzma_fastpos[(dist >> 14 / 2 - 1 + 2 * (FASTPOS_BITS - 1)) as usize] as u32) + (fastpos_at((dist >> 14 / 2 - 1 + 2 * (FASTPOS_BITS - 1)) as usize) as u32) + (2 * (14 / 2 - 1 + 2 * (FASTPOS_BITS - 1))) as u32 } +#[inline(always)] +unsafe fn coder_rep(coder: *const lzma_lzma1_encoder, index: u32) -> u32 { + debug_assert!((index as usize) < REPS as usize); + *((::core::ptr::addr_of!((*coder).reps) as *const u32).add(index as usize)) +} unsafe fn get_literal_price( coder: *const lzma_lzma1_encoder, pos: u32, @@ -100,7 +110,7 @@ fn get_pure_rep_price( state: lzma_lzma_state, pos_state: u32, ) -> u32 { - return unsafe { + unsafe { let mut price: u32 = 0; if rep_index == 0 { price = rc_bit_0_price(rep0_prob(coder, state)); @@ -116,7 +126,7 @@ fn get_pure_rep_price( } } price - }; + } } #[inline] fn get_rep_price( @@ -141,7 +151,7 @@ fn get_dist_len_price( len: u32, pos_state: u32, ) -> u32 { - return unsafe { + unsafe { let dist_state: u32 = if len < (DIST_STATES + MATCH_LEN_MIN) as u32 { debug_assert!(len >= MATCH_LEN_MIN); len - MATCH_LEN_MIN @@ -162,7 +172,7 @@ fn get_dist_len_price( pos_state, ); price - }; + } } #[inline] fn is_literal_state(state: lzma_lzma_state) -> bool { @@ -209,16 +219,17 @@ unsafe fn not_equal_16(a: *const u8, b: *const u8) -> bool { unsafe fn fill_dist_prices(coder: *mut lzma_lzma1_encoder) { let mut dist_state: u32 = 0; while dist_state < DIST_STATES { - let dist_slot_prices: *mut u32 = - ::core::ptr::addr_of_mut!(*(::core::ptr::addr_of_mut!((*coder).dist_slot_prices) - as *mut [u32; 64]) - .offset(dist_state as isize)) as *mut u32; + let dist_slot_prices: *mut u32 = ::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*coder).dist_slot_prices) as *mut [u32; 64]) + .offset(dist_state as isize) + ) as *mut u32; let mut dist_slot: u32 = 0; while dist_slot < (*coder).dist_table_size { *dist_slot_prices.offset(dist_slot as isize) = rc_bittree_price( - ::core::ptr::addr_of_mut!(*(::core::ptr::addr_of_mut!((*coder).dist_slot) - as *mut [probability; 64]) - .offset(dist_state as isize)) as *mut probability, + ::core::ptr::addr_of_mut!( + *(::core::ptr::addr_of_mut!((*coder).dist_slot) as *mut [probability; 64]) + .offset(dist_state as isize) + ) as *mut probability, DIST_SLOT_BITS, dist_slot, ); @@ -231,9 +242,11 @@ unsafe fn fill_dist_prices(coder: *mut lzma_lzma1_encoder) { dist_slot_0 += 1; } let mut i: u32 = 0; + let dist_prices: *mut u32 = (::core::ptr::addr_of_mut!((*coder).dist_prices) + as *mut [u32; 128]) + .add(dist_state as usize) as *mut u32; while i < DIST_MODEL_START { - (*coder).dist_prices[dist_state as usize][i as usize] = - *dist_slot_prices.offset(i as isize); + *dist_prices.add(i as usize) = *dist_slot_prices.offset(i as isize); i += 1; } dist_state += 1; @@ -253,8 +266,13 @@ unsafe fn fill_dist_prices(coder: *mut lzma_lzma1_encoder) { ) as u32; let mut dist_state_0: u32 = 0; while dist_state_0 < DIST_STATES { - (*coder).dist_prices[dist_state_0 as usize][i_0 as usize] = - price + (*coder).dist_slot_prices[dist_state_0 as usize][dist_slot_1 as usize]; + let dist_prices: *mut u32 = (::core::ptr::addr_of_mut!((*coder).dist_prices) + as *mut [u32; 128]) + .add(dist_state_0 as usize) as *mut u32; + let dist_slot_prices: *mut u32 = + (::core::ptr::addr_of_mut!((*coder).dist_slot_prices) as *mut [u32; 64]) + .add(dist_state_0 as usize) as *mut u32; + *dist_prices.add(i_0 as usize) = price + *dist_slot_prices.add(dist_slot_1 as usize); dist_state_0 += 1; } i_0 += 1; @@ -263,8 +281,9 @@ unsafe fn fill_dist_prices(coder: *mut lzma_lzma1_encoder) { } unsafe fn fill_align_prices(coder: *mut lzma_lzma1_encoder) { let mut i: u32 = 0; + let align_prices = ::core::ptr::addr_of_mut!((*coder).align_prices) as *mut u32; while i < ALIGN_SIZE { - (*coder).align_prices[i as usize] = rc_bittree_reverse_price( + *align_prices.add(i as usize) = rc_bittree_reverse_price( ::core::ptr::addr_of_mut!((*coder).dist_align) as *mut probability, ALIGN_BITS, i, @@ -351,6 +370,10 @@ unsafe fn make_short_rep(optimal: *mut lzma_optimal) { (*optimal).back_prev = 0; (*optimal).prev_1_is_literal = false; } +#[inline] +unsafe fn is_short_rep(optimal: *const lzma_optimal) -> bool { + (*optimal).back_prev == 0 +} unsafe fn backward( coder: *mut lzma_lzma1_encoder, len_res: *mut u32, @@ -358,21 +381,21 @@ unsafe fn backward( mut cur: u32, ) { let opts = opts_ptr(coder); + let opt0 = opts; (*coder).opts_end_index = cur; let mut pos_mem: u32 = (*opts.add(cur as usize)).pos_prev; let mut back_mem: u32 = (*opts.add(cur as usize)).back_prev; loop { - if (*opts.add(cur as usize)).prev_1_is_literal { + let opt_cur = opts.add(cur as usize); + if (*opt_cur).prev_1_is_literal { make_literal(opts.add(pos_mem as usize)); debug_assert!(pos_mem > 0); - let literal_pos_prev = pos_mem - 1; - (*opts.add(pos_mem as usize)).pos_prev = literal_pos_prev; - if (*opts.add(cur as usize)).prev_2 { - (*opts.add(literal_pos_prev as usize)).prev_1_is_literal = false; - (*opts.add(literal_pos_prev as usize)).pos_prev = - (*opts.add(cur as usize)).pos_prev_2; - (*opts.add(literal_pos_prev as usize)).back_prev = - (*opts.add(cur as usize)).back_prev_2; + (*opts.add(pos_mem as usize)).pos_prev = pos_mem - 1; + if (*opt_cur).prev_2 { + let opt_prev = opts.add((pos_mem - 1) as usize); + (*opt_prev).prev_1_is_literal = false; + (*opt_prev).pos_prev = (*opt_cur).pos_prev_2; + (*opt_prev).back_prev = (*opt_cur).back_prev_2; } } let pos_prev: u32 = pos_mem; @@ -386,9 +409,9 @@ unsafe fn backward( break; } } - (*coder).opts_current_index = (*opts).pos_prev; - *len_res = (*opts).pos_prev; - *back_res = (*opts).back_prev; + (*coder).opts_current_index = (*opt0).pos_prev; + *len_res = (*opt0).pos_prev; + *back_res = (*opt0).back_prev; } #[inline] unsafe fn helper1( @@ -400,6 +423,8 @@ unsafe fn helper1( ) -> u32 { let opts = opts_ptr(coder); let matches = matches_ptr(coder); + let opt0 = opts; + let opt1 = opts.add(1); let nice_len: u32 = (*mf).nice_len; let mut len_main: u32 = 0; let mut matches_count: u32 = 0; @@ -418,24 +443,23 @@ unsafe fn helper1( } let buf: *const u8 = mf_ptr(mf).offset(-1); let mut rep_lens: [u32; 4] = [0; 4]; - let rep_lens_ptr = rep_lens.as_mut_ptr(); let mut rep_max_index: u32 = 0; let mut i: u32 = 0; while i < REPS { - let buf_back: *const u8 = buf.offset(-((*coder).reps[i as usize] as isize)).offset(-1); + let buf_back: *const u8 = buf.offset(-(coder_rep(coder, i) as isize)).offset(-1); if not_equal_16(buf, buf_back) { - *rep_lens_ptr.add(i as usize) = 0; + rep_lens[i as usize] = 0; } else { - *rep_lens_ptr.add(i as usize) = lzma_memcmplen(buf, buf_back, 2, buf_avail); - if *rep_lens_ptr.add(i as usize) > *rep_lens_ptr.add(rep_max_index as usize) { + rep_lens[i as usize] = lzma_memcmplen(buf, buf_back, 2, buf_avail); + if rep_lens[i as usize] > rep_lens[rep_max_index as usize] { rep_max_index = i; } } i += 1; } - if *rep_lens_ptr.add(rep_max_index as usize) >= nice_len { + if rep_lens[rep_max_index as usize] >= nice_len { *back_res = rep_max_index; - *len_res = *rep_lens_ptr.add(rep_max_index as usize); + *len_res = rep_lens[rep_max_index as usize]; mf_skip(mf, *len_res - 1); return UINT32_MAX; } @@ -447,15 +471,15 @@ unsafe fn helper1( return UINT32_MAX; } let current_byte: u8 = *buf; - let match_byte: u8 = *buf.offset(-((*coder).reps[0] as isize)).offset(-1); - if len_main < 2 && current_byte != match_byte && *rep_lens_ptr.add(rep_max_index as usize) < 2 { + let match_byte: u8 = *buf.offset(-(coder_rep(coder, 0) as isize)).offset(-1); + if len_main < 2 && current_byte != match_byte && rep_lens[rep_max_index as usize] < 2 { *back_res = UINT32_MAX; *len_res = 1; return UINT32_MAX; } - (*opts).state = (*coder).state; + (*opt0).state = (*coder).state; let pos_state: u32 = position & (*coder).pos_mask; - (*opts.add(1)).price = rc_bit_0_price(match_prob(coder, (*coder).state, pos_state)) + (*opt1).price = rc_bit_0_price(match_prob(coder, (*coder).state, pos_state)) + get_literal_price( coder, position, @@ -464,28 +488,29 @@ unsafe fn helper1( match_byte as u32, current_byte as u32, ); - make_literal(opts.add(1)); + make_literal(opt1); let match_price: u32 = rc_bit_1_price(match_prob(coder, (*coder).state, pos_state)) as u32; let rep_match_price: u32 = match_price + rc_bit_1_price(is_rep_prob(coder, (*coder).state)) as u32; if match_byte == current_byte { let short_rep_price: u32 = rep_match_price + get_short_rep_price(coder, (*coder).state, pos_state) as u32; - if short_rep_price < (*opts.add(1)).price { - (*opts.add(1)).price = short_rep_price; - make_short_rep(opts.add(1)); + if short_rep_price < (*opt1).price { + (*opt1).price = short_rep_price; + make_short_rep(opt1); } } - let len_end: u32 = core::cmp::max(len_main, *rep_lens_ptr.add(rep_max_index as usize)); + let len_end: u32 = core::cmp::max(len_main, rep_lens[rep_max_index as usize]); if len_end < 2 { - *back_res = (*opts.add(1)).back_prev; + *back_res = (*opt1).back_prev; *len_res = 1; return UINT32_MAX; } - (*opts.add(1)).pos_prev = 0; + (*opt1).pos_prev = 0; let mut i_0: u32 = 0; + let backs = optimal_backs_ptr(opt0); while i_0 < REPS { - (*opts).backs[i_0 as usize] = (*coder).reps[i_0 as usize]; + *backs.add(i_0 as usize) = coder_rep(coder, i_0); i_0 += 1; } let mut len: u32 = len_end; @@ -498,7 +523,7 @@ unsafe fn helper1( } let mut i_1: u32 = 0; while i_1 < REPS { - let mut rep_len: u32 = *rep_lens_ptr.add(i_1 as usize); + let mut rep_len: u32 = rep_lens[i_1 as usize]; if rep_len >= 2 { let price: u32 = rep_match_price + get_pure_rep_price(coder, i_1, (*coder).state, pos_state) as u32; @@ -525,11 +550,7 @@ unsafe fn helper1( } let normal_match_price: u32 = match_price + rc_bit_0_price(is_rep_prob(coder, (*coder).state)) as u32; - len = if *rep_lens_ptr >= 2 { - *rep_lens_ptr + 1 - } else { - 2 - }; + len = if rep_lens[0] >= 2 { rep_lens[0] + 1 } else { 2 }; if len <= len_main { let mut i_2: u32 = 0; while len > (*matches.add(i_2 as usize)).len { @@ -569,15 +590,17 @@ unsafe fn helper2( ) -> u32 { let opts = opts_ptr(coder); let matches = matches_ptr(coder); + let opt_cur = opts.add(cur as usize); + let opt_next = opts.add((cur + 1) as usize); let mut matches_count: u32 = (*coder).matches_count; let mut new_len: u32 = (*coder).longest_match_length; - let mut pos_prev: u32 = (*opts.add(cur as usize)).pos_prev; + let mut pos_prev: u32 = (*opt_cur).pos_prev; let mut state: lzma_lzma_state = STATE_LIT_LIT; - if (*opts.add(cur as usize)).prev_1_is_literal { + if (*opt_cur).prev_1_is_literal { pos_prev -= 1; - if (*opts.add(cur as usize)).prev_2 { - state = (*opts.add((*opts.add(cur as usize)).pos_prev_2 as usize)).state; - if (*opts.add(cur as usize)).back_prev_2 < REPS { + if (*opt_cur).prev_2 { + state = (*opts.add((*opt_cur).pos_prev_2 as usize)).state; + if (*opt_cur).back_prev_2 < REPS { state = update_long_rep_state(state); } else { state = update_match_state(state); @@ -590,19 +613,19 @@ unsafe fn helper2( state = (*opts.add(pos_prev as usize)).state; } if pos_prev == cur - 1 { - if (*opts.add(cur as usize)).back_prev == 0 { + if is_short_rep(opt_cur) { state = update_short_rep_state(state); } else { state = update_literal_state(state); } } else { let mut pos: u32 = 0; - if (*opts.add(cur as usize)).prev_1_is_literal && (*opts.add(cur as usize)).prev_2 { - pos_prev = (*opts.add(cur as usize)).pos_prev_2; - pos = (*opts.add(cur as usize)).back_prev_2; + if (*opt_cur).prev_1_is_literal && (*opt_cur).prev_2 { + pos_prev = (*opt_cur).pos_prev_2; + pos = (*opt_cur).back_prev_2; state = update_long_rep_state(state); } else { - pos = (*opts.add(cur as usize)).back_prev; + pos = (*opt_cur).back_prev; if pos < REPS { state = update_long_rep_state(state); } else { @@ -632,14 +655,14 @@ unsafe fn helper2( } } } - (*opts.add(cur as usize)).state = state; - let cur_backs = optimal_backs_ptr(opts.add(cur as usize)); + (*opt_cur).state = state; + let cur_backs = optimal_backs_ptr(opt_cur); let mut i_1: u32 = 0; while i_1 < REPS { *cur_backs.add(i_1 as usize) = *reps.offset(i_1 as isize); i_1 += 1; } - let cur_price: u32 = (*opts.add(cur as usize)).price; + let cur_price: u32 = (*opt_cur).price; let current_byte: u8 = *buf; let match_byte: u8 = *buf.offset(-(*reps as isize)).offset(-1); let pos_state: u32 = position & (*coder).pos_mask; @@ -653,23 +676,22 @@ unsafe fn helper2( match_byte as u32, current_byte as u32, ) as u32; - let next_opt = &mut *opts.add((cur + 1) as usize); let mut next_is_literal: bool = false; - if cur_and_1_price < next_opt.price { - next_opt.price = cur_and_1_price; - next_opt.pos_prev = cur; - make_literal(next_opt); + if cur_and_1_price < (*opt_next).price { + (*opt_next).price = cur_and_1_price; + (*opt_next).pos_prev = cur; + make_literal(opt_next); next_is_literal = true; } let match_price: u32 = cur_price + rc_bit_1_price(match_prob(coder, state, pos_state)) as u32; let rep_match_price: u32 = match_price + rc_bit_1_price(is_rep_prob(coder, state)) as u32; - if match_byte == current_byte && !(next_opt.pos_prev < cur && next_opt.back_prev == 0) { + if match_byte == current_byte && !((*opt_next).pos_prev < cur && (*opt_next).back_prev == 0) { let short_rep_price: u32 = rep_match_price + get_short_rep_price(coder, state, pos_state) as u32; - if short_rep_price <= next_opt.price { - next_opt.price = short_rep_price; - next_opt.pos_prev = cur; - make_short_rep(next_opt); + if short_rep_price <= (*opt_next).price { + (*opt_next).price = short_rep_price; + (*opt_next).pos_prev = cur; + make_short_rep(opt_next); next_is_literal = true; } } diff --git a/xz/src/lzma/lzma_encoder_presets.rs b/xz/src/lzma/lzma_encoder_presets.rs index 098a8363..1282a7df 100644 --- a/xz/src/lzma/lzma_encoder_presets.rs +++ b/xz/src/lzma/lzma_encoder_presets.rs @@ -15,14 +15,14 @@ pub unsafe fn lzma_lzma_preset(options: *mut lzma_options_lzma, preset: u32) -> (*options).lc = LZMA_LC_DEFAULT; (*options).lp = LZMA_LP_DEFAULT; (*options).pb = LZMA_PB_DEFAULT; - static mut dict_pow2: [u8; 10] = [18, 20, 21, 22, 22, 23, 23, 24, 25, 26]; - (*options).dict_size = 1u32 << dict_pow2[level as usize]; + const DICT_POW2: [u8; 10] = [18, 20, 21, 22, 22, 23, 23, 24, 25, 26]; + (*options).dict_size = 1u32 << DICT_POW2[level as usize]; if level <= 3 { (*options).mode = LZMA_MODE_FAST; (*options).mf = (if level == 0 { LZMA_MF_HC3 } else { LZMA_MF_HC4 }) as lzma_match_finder; (*options).nice_len = (if level <= 1 { 128 } else { 273 }) as u32; - static mut depths: [u8; 4] = [4, 8, 24, 48]; - (*options).depth = depths[level as usize] as u32; + const DEPTHS: [u8; 4] = [4, 8, 24, 48]; + (*options).depth = DEPTHS[level as usize] as u32; } else { (*options).mode = LZMA_MODE_NORMAL; (*options).mf = LZMA_MF_BT4; diff --git a/xz/src/rangecoder/price_table.rs b/xz/src/rangecoder/price_table.rs index 10ce0f46..da1937ec 100644 --- a/xz/src/rangecoder/price_table.rs +++ b/xz/src/rangecoder/price_table.rs @@ -1,4 +1,4 @@ -pub static mut lzma_rc_prices: [u8; 128] = [ +pub static lzma_rc_prices: [u8; 128] = [ 128, 103, 91, 84, 78, 73, 69, 66, 63, 61, 58, 56, 54, 52, 51, 49, 48, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 34, 33, 32, 31, 31, 30, 29, 29, 28, 28, 27, 26, 26, 25, 25, 24, 24, 23, 23, 22, 22, 22, 21, 21, 20, 20, 19, 19, 19, 18, 18, 17, 17, 17, 16, 16, 16, 15, 15, 15, 14, diff --git a/xz/src/simple/arm.rs b/xz/src/simple/arm.rs index a63faab5..a6aff8a1 100644 --- a/xz/src/simple/arm.rs +++ b/xz/src/simple/arm.rs @@ -1,11 +1,13 @@ use crate::types::*; fn arm_code_impl(now_pos: u32, is_encoder: bool, buffer: &mut [u8]) -> size_t { let size = buffer.len() & !3; + let ptr = buffer.as_mut_ptr(); let mut i: size_t = 0; while i < size { - if buffer[i + 3] == 0xeb { + let cur = unsafe { ptr.add(i) }; + if unsafe { *cur.add(3) } == 0xeb { let mut src: u32 = - (buffer[i + 2] as u32) << 16 | (buffer[i + 1] as u32) << 8 | buffer[i] as u32; + unsafe { (*cur.add(2) as u32) << 16 | (*cur.add(1) as u32) << 8 | *cur as u32 }; src <<= 2; let dest = if is_encoder { now_pos @@ -16,15 +18,17 @@ fn arm_code_impl(now_pos: u32, is_encoder: bool, buffer: &mut [u8]) -> size_t { src.wrapping_sub(now_pos.wrapping_add(i as u32).wrapping_add(8)) }; let dest = dest >> 2; - buffer[i + 2] = (dest >> 16) as u8; - buffer[i + 1] = (dest >> 8) as u8; - buffer[i] = dest as u8; + unsafe { + *cur.add(2) = (dest >> 16) as u8; + *cur.add(1) = (dest >> 8) as u8; + *cur = dest as u8; + } } i += 4; } i } -unsafe extern "C" fn arm_code( +unsafe fn arm_code( _simple: *mut c_void, now_pos: u32, is_encoder: bool, @@ -50,21 +54,21 @@ unsafe fn arm_coder_init( next, allocator, filters, - Some(arm_code as unsafe extern "C" fn(*mut c_void, u32, bool, *mut u8, size_t) -> size_t), + arm_code as lzma_simple_filter_function, 0, 4, 4, is_encoder, ) } -pub(crate) unsafe extern "C" fn lzma_simple_arm_encoder_init( +pub(crate) unsafe fn lzma_simple_arm_encoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, ) -> lzma_ret { arm_coder_init(next, allocator, filters, true) } -pub(crate) unsafe extern "C" fn lzma_simple_arm_decoder_init( +pub(crate) unsafe fn lzma_simple_arm_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, diff --git a/xz/src/simple/arm64.rs b/xz/src/simple/arm64.rs index 56db80b0..897213e1 100644 --- a/xz/src/simple/arm64.rs +++ b/xz/src/simple/arm64.rs @@ -4,7 +4,8 @@ fn arm64_code_impl(now_pos: u32, is_encoder: bool, buffer: &mut [u8]) -> size_t let mut i: size_t = 0; while i < size { let mut pc: u32 = now_pos.wrapping_add(i as u32); - let mut instr: u32 = read32le(buffer.as_mut_ptr().wrapping_add(i)); + let word: &mut [u8; 4] = unsafe { &mut *buffer.as_mut_ptr().add(i).cast::<[u8; 4]>() }; + let mut instr: u32 = read32le(word); if instr >> 26 == 0x25 { let src: u32 = instr; instr = 0x94000000; @@ -13,7 +14,7 @@ fn arm64_code_impl(now_pos: u32, is_encoder: bool, buffer: &mut [u8]) -> size_t pc = 0u32.wrapping_sub(pc); } instr |= src.wrapping_add(pc) & 0x3ffffff; - write32le(buffer.as_mut_ptr().wrapping_add(i), instr); + write32le(word, instr); } else if instr & 0x9f000000 == 0x90000000 { let src: u32 = instr >> 29 & 3 | instr >> 3 & 0x1ffffc; if src.wrapping_add(0x20000) & 0x1c0000 == 0 { @@ -26,14 +27,14 @@ fn arm64_code_impl(now_pos: u32, is_encoder: bool, buffer: &mut [u8]) -> size_t instr |= (dest & 3) << 29; instr |= (dest & 0x3fffc) << 3; instr |= 0u32.wrapping_sub(dest & 0x20000) & 0xe00000; - write32le(buffer.as_mut_ptr().wrapping_add(i), instr); + write32le(word, instr); } } i += 4; } i } -unsafe extern "C" fn arm64_code( +unsafe fn arm64_code( _simple: *mut c_void, now_pos: u32, is_encoder: bool, @@ -59,14 +60,14 @@ unsafe fn arm64_coder_init( next, allocator, filters, - Some(arm64_code as unsafe extern "C" fn(*mut c_void, u32, bool, *mut u8, size_t) -> size_t), + arm64_code as lzma_simple_filter_function, 0, 4, 4, is_encoder, ) } -pub(crate) unsafe extern "C" fn lzma_simple_arm64_encoder_init( +pub(crate) unsafe fn lzma_simple_arm64_encoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, @@ -84,7 +85,7 @@ pub unsafe fn lzma_bcj_arm64_encode(mut start_offset: u32, buf: *mut u8, size: s core::slice::from_raw_parts_mut(buf, size), ) } -pub(crate) unsafe extern "C" fn lzma_simple_arm64_decoder_init( +pub(crate) unsafe fn lzma_simple_arm64_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, diff --git a/xz/src/simple/armthumb.rs b/xz/src/simple/armthumb.rs index 01b01696..04582a35 100644 --- a/xz/src/simple/armthumb.rs +++ b/xz/src/simple/armthumb.rs @@ -4,13 +4,19 @@ fn armthumb_code_impl(now_pos: u32, is_encoder: bool, buffer: &mut [u8]) -> size return 0; } let size = buffer.len() - 4; + let ptr = buffer.as_mut_ptr(); let mut i: size_t = 0; while i <= size { - if buffer[i + 1] & 0xf8 == 0xf0 && buffer[i + 3] & 0xf8 == 0xf8 { - let mut src: u32 = (buffer[i + 1] as u32 & 7) << 19 - | (buffer[i] as u32) << 11 - | (buffer[i + 3] as u32 & 7) << 8 - | buffer[i + 2] as u32; + let cur = unsafe { ptr.add(i) }; + let b1 = unsafe { *cur.add(1) }; + let b3 = unsafe { *cur.add(3) }; + if b1 & 0xf8 == 0xf0 && b3 & 0xf8 == 0xf8 { + let mut src: u32 = unsafe { + (b1 as u32 & 7) << 19 + | (*cur as u32) << 11 + | (b3 as u32 & 7) << 8 + | *cur.add(2) as u32 + }; src <<= 1; let dest = if is_encoder { now_pos @@ -21,17 +27,19 @@ fn armthumb_code_impl(now_pos: u32, is_encoder: bool, buffer: &mut [u8]) -> size src.wrapping_sub(now_pos.wrapping_add(i as u32).wrapping_add(4)) }; let dest = dest >> 1; - buffer[i + 1] = (0xf0 | dest >> 19 & 0x7) as u8; - buffer[i] = (dest >> 11) as u8; - buffer[i + 3] = (0xf8 | dest >> 8 & 0x7) as u8; - buffer[i + 2] = dest as u8; + unsafe { + *cur.add(1) = (0xf0 | dest >> 19 & 0x7) as u8; + *cur = (dest >> 11) as u8; + *cur.add(3) = (0xf8 | dest >> 8 & 0x7) as u8; + *cur.add(2) = dest as u8; + } i += 2; } i += 2; } i } -unsafe extern "C" fn armthumb_code( +unsafe fn armthumb_code( _simple: *mut c_void, now_pos: u32, is_encoder: bool, @@ -57,24 +65,21 @@ unsafe fn armthumb_coder_init( next, allocator, filters, - Some( - armthumb_code - as unsafe extern "C" fn(*mut c_void, u32, bool, *mut u8, size_t) -> size_t, - ), + armthumb_code as lzma_simple_filter_function, 0, 4, 2, is_encoder, ) } -pub(crate) unsafe extern "C" fn lzma_simple_armthumb_encoder_init( +pub(crate) unsafe fn lzma_simple_armthumb_encoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, ) -> lzma_ret { armthumb_coder_init(next, allocator, filters, true) } -pub(crate) unsafe extern "C" fn lzma_simple_armthumb_decoder_init( +pub(crate) unsafe fn lzma_simple_armthumb_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, diff --git a/xz/src/simple/ia64.rs b/xz/src/simple/ia64.rs index 90a4e723..98d03cc4 100644 --- a/xz/src/simple/ia64.rs +++ b/xz/src/simple/ia64.rs @@ -5,10 +5,12 @@ fn ia64_code_impl(now_pos: u32, is_encoder: bool, buffer: &mut [u8]) -> size_t { 0, 0, ]; let size = buffer.len() & !15; + let ptr = buffer.as_mut_ptr(); let mut i: size_t = 0; while i < size { - let instr_template: u32 = (buffer[i] & 0x1f) as u32; - let mask: u32 = BRANCH_TABLE[instr_template as usize]; + let cur = unsafe { ptr.add(i) }; + let instr_template: u32 = unsafe { (*cur & 0x1f) as u32 }; + let mask: u32 = unsafe { *BRANCH_TABLE.as_ptr().add(instr_template as usize) }; let mut bit_pos: u32 = 5; let mut slot: size_t = 0; while slot < 3 { @@ -18,7 +20,7 @@ fn ia64_code_impl(now_pos: u32, is_encoder: bool, buffer: &mut [u8]) -> size_t { let mut instruction: u64 = 0; let mut j: size_t = 0; while j < 6 { - instruction += (buffer[i + j + byte_pos] as u64) << (8 * j); + instruction += unsafe { (*cur.add(j + byte_pos) as u64) << (8 * j) }; j += 1; } let mut inst_norm: u64 = instruction >> bit_res; @@ -39,7 +41,9 @@ fn ia64_code_impl(now_pos: u32, is_encoder: bool, buffer: &mut [u8]) -> size_t { instruction |= inst_norm << bit_res; let mut j: size_t = 0; while j < 6 { - buffer[i + j + byte_pos] = (instruction >> (8 * j)) as u8; + unsafe { + *cur.add(j + byte_pos) = (instruction >> (8 * j)) as u8; + } j += 1; } } @@ -51,7 +55,7 @@ fn ia64_code_impl(now_pos: u32, is_encoder: bool, buffer: &mut [u8]) -> size_t { } i } -unsafe extern "C" fn ia64_code( +unsafe fn ia64_code( _simple: *mut c_void, now_pos: u32, is_encoder: bool, @@ -77,21 +81,21 @@ unsafe fn ia64_coder_init( next, allocator, filters, - Some(ia64_code as unsafe extern "C" fn(*mut c_void, u32, bool, *mut u8, size_t) -> size_t), + ia64_code as lzma_simple_filter_function, 0, 16, 16, is_encoder, ) } -pub(crate) unsafe extern "C" fn lzma_simple_ia64_encoder_init( +pub(crate) unsafe fn lzma_simple_ia64_encoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, ) -> lzma_ret { ia64_coder_init(next, allocator, filters, true) } -pub(crate) unsafe extern "C" fn lzma_simple_ia64_decoder_init( +pub(crate) unsafe fn lzma_simple_ia64_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, diff --git a/xz/src/simple/powerpc.rs b/xz/src/simple/powerpc.rs index 31820413..6bb08134 100644 --- a/xz/src/simple/powerpc.rs +++ b/xz/src/simple/powerpc.rs @@ -1,29 +1,36 @@ use crate::types::*; fn powerpc_code_impl(now_pos: u32, is_encoder: bool, buffer: &mut [u8]) -> size_t { let size = buffer.len() & !3; + let ptr = buffer.as_mut_ptr(); let mut i: size_t = 0; while i < size { - if buffer[i] >> 2 == 0x12 && buffer[i + 3] & 3 == 1 { - let src: u32 = (buffer[i] as u32 & 3) << 24 - | (buffer[i + 1] as u32) << 16 - | (buffer[i + 2] as u32) << 8 - | (buffer[i + 3] as u32 & !3); + let cur = unsafe { ptr.add(i) }; + let b0 = unsafe { *cur }; + let b3 = unsafe { *cur.add(3) }; + if b0 >> 2 == 0x12 && b3 & 3 == 1 { + let src: u32 = unsafe { + (b0 as u32 & 3) << 24 + | (*cur.add(1) as u32) << 16 + | (*cur.add(2) as u32) << 8 + | (b3 as u32 & !3) + }; let dest = if is_encoder { now_pos.wrapping_add(i as u32).wrapping_add(src) } else { src.wrapping_sub(now_pos.wrapping_add(i as u32)) }; - buffer[i] = (0x48 | dest >> 24 & 0x3) as u8; - buffer[i + 1] = (dest >> 16) as u8; - buffer[i + 2] = (dest >> 8) as u8; - buffer[i + 3] &= 0x3; - buffer[i + 3] = (buffer[i + 3] as u32 | dest) as u8; + unsafe { + *cur = (0x48 | dest >> 24 & 0x3) as u8; + *cur.add(1) = (dest >> 16) as u8; + *cur.add(2) = (dest >> 8) as u8; + *cur.add(3) = ((b3 & 0x3) as u32 | dest) as u8; + } } i += 4; } i } -unsafe extern "C" fn powerpc_code( +unsafe fn powerpc_code( _simple: *mut c_void, now_pos: u32, is_encoder: bool, @@ -49,23 +56,21 @@ unsafe fn powerpc_coder_init( next, allocator, filters, - Some( - powerpc_code as unsafe extern "C" fn(*mut c_void, u32, bool, *mut u8, size_t) -> size_t, - ), + powerpc_code as lzma_simple_filter_function, 0, 4, 4, is_encoder, ) } -pub(crate) unsafe extern "C" fn lzma_simple_powerpc_encoder_init( +pub(crate) unsafe fn lzma_simple_powerpc_encoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, ) -> lzma_ret { powerpc_coder_init(next, allocator, filters, true) } -pub(crate) unsafe extern "C" fn lzma_simple_powerpc_decoder_init( +pub(crate) unsafe fn lzma_simple_powerpc_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, diff --git a/xz/src/simple/riscv.rs b/xz/src/simple/riscv.rs index 2ae98818..ff78c5b1 100644 --- a/xz/src/simple/riscv.rs +++ b/xz/src/simple/riscv.rs @@ -1,44 +1,55 @@ use crate::types::*; #[inline] -fn read32be(buf: *const u8) -> u32 { - return unsafe { - let mut num: u32 = (*buf as u32) << 24; - num |= (*buf.offset(1) as u32) << 16; - num |= (*buf.offset(2) as u32) << 8; - num |= *buf.offset(3) as u32; - num - }; +fn read32be(buf: &[u8; 4]) -> u32 { + let mut num: u32 = (buf[0] as u32) << 24; + num |= (buf[1] as u32) << 16; + num |= (buf[2] as u32) << 8; + num |= buf[3] as u32; + num } #[inline] -fn write32be(buf: *mut u8, num: u32) { - unsafe { - *buf = (num >> 24) as u8; - *buf.offset(1) = (num >> 16) as u8; - *buf.offset(2) = (num >> 8) as u8; - *buf.offset(3) = num as u8; - } +fn write32be(buf: &mut [u8; 4], num: u32) { + buf[0] = (num >> 24) as u8; + buf[1] = (num >> 16) as u8; + buf[2] = (num >> 8) as u8; + buf[3] = num as u8; } -unsafe extern "C" fn riscv_encode( - _simple: *mut c_void, - now_pos: u32, - _is_encoder: bool, - buffer: *mut u8, - mut size: size_t, -) -> size_t { - if size < 8 { +#[inline] +unsafe fn read32le_at(buffer: &[u8], offset: usize) -> u32 { + read32le(&*buffer.as_ptr().add(offset).cast::<[u8; 4]>()) +} +#[inline] +unsafe fn read32be_at(buffer: &[u8], offset: usize) -> u32 { + read32be(&*buffer.as_ptr().add(offset).cast::<[u8; 4]>()) +} +#[inline] +unsafe fn write32le_at(buffer: &mut [u8], offset: usize, value: u32) { + write32le( + &mut *buffer.as_mut_ptr().add(offset).cast::<[u8; 4]>(), + value, + ); +} +#[inline] +unsafe fn write32be_at(buffer: &mut [u8], offset: usize, value: u32) { + write32be( + &mut *buffer.as_mut_ptr().add(offset).cast::<[u8; 4]>(), + value, + ); +} +fn riscv_encode_impl(now_pos: u32, buffer: &mut [u8]) -> size_t { + if buffer.len() < 8 { return 0; } - size -= 8; + let size = buffer.len() - 8; + let ptr = buffer.as_mut_ptr(); let mut i: size_t = 0; - let mut current_block_22: u64; - i = 0; while i <= size { - let mut inst: u32 = *buffer.offset(i as isize) as u32; + let mut inst: u32 = unsafe { *ptr.add(i) as u32 }; if inst == 0xef { - let b1: u32 = *buffer.offset((i + 1) as isize) as u32; + let b1: u32 = unsafe { *ptr.add(i + 1) as u32 }; if b1 & 0xd == 0 { - let b2: u32 = *buffer.offset((i + 2) as isize) as u32; - let b3: u32 = *buffer.offset((i + 3) as isize) as u32; + let b2: u32 = unsafe { *ptr.add(i + 2) as u32 }; + let b3: u32 = unsafe { *ptr.add(i + 3) as u32 }; let pc: u32 = now_pos.wrapping_add(i as u32); let mut addr: u32 = (b1 & 0xf0) << 8 | (b2 & 0xf) << 16 @@ -47,46 +58,44 @@ unsafe extern "C" fn riscv_encode( | (b3 & 0x7f) << 4 | (b3 & 0x80) << 13; addr = addr.wrapping_add(pc); - *buffer.offset((i + 1) as isize) = (b1 & 0xf | addr >> 13 & 0xf0) as u8; - *buffer.offset((i + 2) as isize) = (addr >> 9) as u8; - *buffer.offset((i + 3) as isize) = (addr >> 1) as u8; + unsafe { + *ptr.add(i + 1) = (b1 & 0xf | addr >> 13 & 0xf0) as u8; + *ptr.add(i + 2) = (addr >> 9) as u8; + *ptr.add(i + 3) = (addr >> 1) as u8; + } i += 4 - 2; } } else if inst & 0x7f == 0x17 { - inst |= (*buffer.offset((i + 1) as isize) as u32) << 8; - inst |= (*buffer.offset((i + 2) as isize) as u32) << 16; - inst |= (*buffer.offset((i + 3) as isize) as u32) << 24; + inst |= unsafe { (*ptr.add(i + 1) as u32) << 8 }; + inst |= unsafe { (*ptr.add(i + 2) as u32) << 16 }; + inst |= unsafe { (*ptr.add(i + 3) as u32) << 24 }; if inst & 0xe80 != 0 { - let inst2: u32 = read32le(buffer.offset(i as isize).offset(4)); + let inst2: u32 = unsafe { read32le_at(buffer, i + 4) }; if (inst << 8 ^ inst2.wrapping_sub(3)) & 0xf8003 != 0 { i += 6 - 2; - current_block_22 = 12517898123489920830; } else { let mut addr_0: u32 = inst & 0xfffff000; addr_0 = addr_0.wrapping_add((inst2 >> 20).wrapping_sub(inst2 >> 19 & 0x1000)); addr_0 = addr_0.wrapping_add(now_pos.wrapping_add(i as u32)); inst = (0x17 | (2) << 7) as u32 | inst2 << 12; - write32le(buffer.offset(i as isize), inst); - write32be(buffer.offset(i as isize).offset(4), addr_0); - current_block_22 = 15125582407903384992; + unsafe { + write32le_at(buffer, i, inst); + write32be_at(buffer, i + 4, addr_0); + } + i += 8 - 2; } } else { let fake_rs1: u32 = inst >> 27; if inst.wrapping_sub(0x3117) << 18 >= fake_rs1 & 0x1d { i += 4 - 2; - current_block_22 = 12517898123489920830; } else { - let fake_addr: u32 = read32le(buffer.offset(i as isize).offset(4)) as u32; + let fake_addr: u32 = unsafe { read32le_at(buffer, i + 4) }; let fake_inst2: u32 = inst >> 12 | fake_addr << 20; inst = 0x17 | fake_rs1 << 7 | fake_addr & 0xfffff000; - write32le(buffer.offset(i as isize), inst); - write32le(buffer.offset(i as isize).offset(4), fake_inst2); - current_block_22 = 15125582407903384992; - } - } - match current_block_22 { - 12517898123489920830 => {} - _ => { + unsafe { + write32le_at(buffer, i, inst); + write32le_at(buffer, i + 4, fake_inst2); + } i += 8 - 2; } } @@ -95,7 +104,19 @@ unsafe extern "C" fn riscv_encode( } i } -pub(crate) unsafe extern "C" fn lzma_simple_riscv_encoder_init( +unsafe fn riscv_encode( + _simple: *mut c_void, + now_pos: u32, + _is_encoder: bool, + buffer: *mut u8, + size: size_t, +) -> size_t { + if size == 0 { + return 0; + } + riscv_encode_impl(now_pos, core::slice::from_raw_parts_mut(buffer, size)) +} +pub(crate) unsafe fn lzma_simple_riscv_encoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, @@ -104,9 +125,7 @@ pub(crate) unsafe extern "C" fn lzma_simple_riscv_encoder_init( next, allocator, filters, - Some( - riscv_encode as unsafe extern "C" fn(*mut c_void, u32, bool, *mut u8, size_t) -> size_t, - ), + riscv_encode as lzma_simple_filter_function, 0, 8, 2, @@ -115,73 +134,69 @@ pub(crate) unsafe extern "C" fn lzma_simple_riscv_encoder_init( } pub unsafe fn lzma_bcj_riscv_encode(mut start_offset: u32, buf: *mut u8, size: size_t) -> size_t { start_offset = (start_offset & !1u32) as u32; - riscv_encode(core::ptr::null_mut(), start_offset, true, buf, size) + if size == 0 { + return 0; + } + riscv_encode_impl(start_offset, core::slice::from_raw_parts_mut(buf, size)) } -unsafe extern "C" fn riscv_decode( - _simple: *mut c_void, - now_pos: u32, - _is_encoder: bool, - buffer: *mut u8, - mut size: size_t, -) -> size_t { - if size < 8 { +fn riscv_decode_impl(now_pos: u32, buffer: &mut [u8]) -> size_t { + if buffer.len() < 8 { return 0; } - size -= 8; + let size = buffer.len() - 8; + let ptr = buffer.as_mut_ptr(); let mut i: size_t = 0; - let mut current_block_23: u64; - i = 0; while i <= size { - let mut inst: u32 = *buffer.offset(i as isize) as u32; + let mut inst: u32 = unsafe { *ptr.add(i) as u32 }; if inst == 0xef { - let b1: u32 = *buffer.offset((i + 1) as isize) as u32; + let b1: u32 = unsafe { *ptr.add(i + 1) as u32 }; if b1 & 0xd == 0 { - let b2: u32 = *buffer.offset((i + 2) as isize) as u32; - let b3: u32 = *buffer.offset((i + 3) as isize) as u32; + let b2: u32 = unsafe { *ptr.add(i + 2) as u32 }; + let b3: u32 = unsafe { *ptr.add(i + 3) as u32 }; let pc: u32 = now_pos.wrapping_add(i as u32); let mut addr: u32 = (b1 & 0xf0) << 13 | b2 << 9 | b3 << 1; addr = addr.wrapping_sub(pc); - *buffer.offset((i + 1) as isize) = (b1 & 0xf | addr >> 8 & 0xf0) as u8; - *buffer.offset((i + 2) as isize) = - (addr >> 16 & 0xf | addr >> 7 & 0x10 | addr << 4 & 0xe0) as u8; - *buffer.offset((i + 3) as isize) = (addr >> 4 & 0x7f | addr >> 13 & 0x80) as u8; + unsafe { + *ptr.add(i + 1) = (b1 & 0xf | addr >> 8 & 0xf0) as u8; + *ptr.add(i + 2) = + (addr >> 16 & 0xf | addr >> 7 & 0x10 | addr << 4 & 0xe0) as u8; + *ptr.add(i + 3) = (addr >> 4 & 0x7f | addr >> 13 & 0x80) as u8; + } i += 4 - 2; } } else if inst & 0x7f == 0x17 { let mut inst2: u32 = 0; - inst |= (*buffer.offset((i + 1) as isize) as u32) << 8; - inst |= (*buffer.offset((i + 2) as isize) as u32) << 16; - inst |= (*buffer.offset((i + 3) as isize) as u32) << 24; + inst |= unsafe { (*ptr.add(i + 1) as u32) << 8 }; + inst |= unsafe { (*ptr.add(i + 2) as u32) << 16 }; + inst |= unsafe { (*ptr.add(i + 3) as u32) << 24 }; if inst & 0xe80 != 0 { - inst2 = read32le(buffer.offset(i as isize).offset(4)); + inst2 = unsafe { read32le_at(buffer, i + 4) }; if (inst << 8 ^ inst2.wrapping_sub(3)) & 0xf8003 != 0 { i += 6 - 2; - current_block_23 = 12517898123489920830; } else { let mut addr_0: u32 = inst & 0xfffff000; addr_0 = addr_0.wrapping_add(inst2 >> 20); inst = (0x17 | (2) << 7) as u32 | inst2 << 12; inst2 = addr_0; - current_block_23 = 6669252993407410313; + unsafe { + write32le_at(buffer, i, inst); + write32le_at(buffer, i + 4, inst2); + } + i += 8 - 2; } } else { let inst2_rs1: u32 = inst >> 27; if inst.wrapping_sub(0x3117) << 18 >= inst2_rs1 & 0x1d { i += 4 - 2; - current_block_23 = 12517898123489920830; } else { - let mut addr_1: u32 = read32be(buffer.offset(i as isize).offset(4)); + let mut addr_1: u32 = unsafe { read32be_at(buffer, i + 4) }; addr_1 = addr_1.wrapping_sub(now_pos.wrapping_add(i as u32)); inst2 = inst >> 12 | addr_1 << 20; inst = 0x17 | inst2_rs1 << 7 | addr_1.wrapping_add(0x800) & 0xfffff000; - current_block_23 = 6669252993407410313; - } - } - match current_block_23 { - 12517898123489920830 => {} - _ => { - write32le(buffer.offset(i as isize), inst); - write32le(buffer.offset(i as isize).offset(4), inst2); + unsafe { + write32le_at(buffer, i, inst); + write32le_at(buffer, i + 4, inst2); + } i += 8 - 2; } } @@ -190,7 +205,19 @@ unsafe extern "C" fn riscv_decode( } i } -pub(crate) unsafe extern "C" fn lzma_simple_riscv_decoder_init( +unsafe fn riscv_decode( + _simple: *mut c_void, + now_pos: u32, + _is_encoder: bool, + buffer: *mut u8, + size: size_t, +) -> size_t { + if size == 0 { + return 0; + } + riscv_decode_impl(now_pos, core::slice::from_raw_parts_mut(buffer, size)) +} +pub(crate) unsafe fn lzma_simple_riscv_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, @@ -199,9 +226,7 @@ pub(crate) unsafe extern "C" fn lzma_simple_riscv_decoder_init( next, allocator, filters, - Some( - riscv_decode as unsafe extern "C" fn(*mut c_void, u32, bool, *mut u8, size_t) -> size_t, - ), + riscv_decode as lzma_simple_filter_function, 0, 8, 2, @@ -210,5 +235,8 @@ pub(crate) unsafe extern "C" fn lzma_simple_riscv_decoder_init( } pub unsafe fn lzma_bcj_riscv_decode(mut start_offset: u32, buf: *mut u8, size: size_t) -> size_t { start_offset = (start_offset & !1u32) as u32; - riscv_decode(core::ptr::null_mut(), start_offset, false, buf, size) + if size == 0 { + return 0; + } + riscv_decode_impl(start_offset, core::slice::from_raw_parts_mut(buf, size)) } diff --git a/xz/src/simple/simple_coder.rs b/xz/src/simple/simple_coder.rs index 267ec3c7..dff34656 100644 --- a/xz/src/simple/simple_coder.rs +++ b/xz/src/simple/simple_coder.rs @@ -3,7 +3,7 @@ use crate::types::*; unsafe fn copy_or_code( coder: *mut lzma_simple_coder, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, @@ -11,46 +11,49 @@ unsafe fn copy_or_code( out_size: size_t, action: lzma_action, ) -> lzma_ret { - if (*coder).next.code.is_none() { - lzma_bufcpy(in_0, in_pos, in_size, out, out_pos, out_size); - if (*coder).is_encoder && action == LZMA_FINISH && *in_pos == in_size { - (*coder).end_was_reached = true; + match (*coder).next.code { + None => { + lzma_bufcpy(input, in_pos, in_size, out, out_pos, out_size); + if (*coder).is_encoder && action == LZMA_FINISH && *in_pos == in_size { + (*coder).end_was_reached = true; + } } - } else { - let ret: lzma_ret = (*coder).next.code.unwrap()( - (*coder).next.coder, - allocator, - in_0, - in_pos, - in_size, - out, - out_pos, - out_size, - action, - ); - if ret == LZMA_STREAM_END { - (*coder).end_was_reached = true; - } else if ret != LZMA_OK { - return ret; + Some(code) => { + let ret: lzma_ret = code( + (*coder).next.coder, + allocator, + input, + in_pos, + in_size, + out, + out_pos, + out_size, + action, + ); + if ret == LZMA_STREAM_END { + (*coder).end_was_reached = true; + } else if ret != LZMA_OK { + return ret; + } } } LZMA_OK } -unsafe fn call_filter(coder: *mut lzma_simple_coder, buffer: *mut u8, size: size_t) -> size_t { - let filtered: size_t = (*coder).filter.unwrap()( +unsafe fn call_filter(coder: *mut lzma_simple_coder, buffer: &mut [u8]) -> size_t { + let filtered: size_t = ((*coder).filter)( (*coder).simple, (*coder).now_pos, (*coder).is_encoder, - buffer, - size, + buffer.as_mut_ptr(), + buffer.len(), ) as size_t; (*coder).now_pos = (*coder).now_pos.wrapping_add(filtered as u32); filtered } -unsafe extern "C" fn simple_code( +unsafe fn simple_code( coder_ptr: *mut c_void, allocator: *const lzma_allocator, - in_0: *const u8, + input: *const u8, in_pos: *mut size_t, in_size: size_t, out: *mut u8, @@ -58,42 +61,42 @@ unsafe extern "C" fn simple_code( out_size: size_t, action: lzma_action, ) -> lzma_ret { - let coder: *mut lzma_simple_coder = coder_ptr as *mut lzma_simple_coder; + let coder: &mut lzma_simple_coder = &mut *(coder_ptr as *mut lzma_simple_coder); if action == LZMA_SYNC_FLUSH { return LZMA_OPTIONS_ERROR; } - if (*coder).pos < (*coder).filtered { + if coder.pos < coder.filtered { lzma_bufcpy( - ::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8, - ::core::ptr::addr_of_mut!((*coder).pos), - (*coder).filtered, + ::core::ptr::addr_of_mut!(coder.buffer) as *mut u8, + ::core::ptr::addr_of_mut!(coder.pos), + coder.filtered, out, out_pos, out_size, ); - if (*coder).pos < (*coder).filtered { + if coder.pos < coder.filtered { return LZMA_OK; } - if (*coder).end_was_reached { + if coder.end_was_reached { return LZMA_STREAM_END; } } - (*coder).filtered = 0; + coder.filtered = 0; let out_avail: size_t = out_size - *out_pos; - let buf_avail: size_t = (*coder).size - (*coder).pos; + let buf_avail: size_t = coder.size - coder.pos; if out_avail > buf_avail || buf_avail == 0 { let out_start: size_t = *out_pos; if buf_avail > 0 { core::ptr::copy_nonoverlapping( - (::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8) - .offset((*coder).pos as isize) as *const u8, + (::core::ptr::addr_of_mut!(coder.buffer) as *mut u8).offset(coder.pos as isize) + as *const u8, out.offset(*out_pos as isize) as *mut u8, buf_avail, ); } *out_pos += buf_avail; let ret: lzma_ret = copy_or_code( - coder, allocator, in_0, in_pos, in_size, out, out_pos, out_size, action, + coder, allocator, input, in_pos, in_size, out, out_pos, out_size, action, ); if ret != LZMA_OK { return ret; @@ -102,75 +105,80 @@ unsafe extern "C" fn simple_code( let filtered: size_t = if size == 0 { 0 } else { - call_filter(coder, out.offset(out_start as isize), size) as size_t + call_filter( + coder as *mut lzma_simple_coder, + core::slice::from_raw_parts_mut(out.add(out_start), size), + ) as size_t }; let unfiltered: size_t = size - filtered; - (*coder).pos = 0; - (*coder).size = unfiltered; - if (*coder).end_was_reached { - (*coder).size = 0; + coder.pos = 0; + coder.size = unfiltered; + if coder.end_was_reached { + coder.size = 0; } else if unfiltered > 0 { *out_pos -= unfiltered; core::ptr::copy_nonoverlapping( out.offset(*out_pos as isize) as *const u8, - ::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8, + ::core::ptr::addr_of_mut!(coder.buffer) as *mut u8, unfiltered, ); } - } else if (*coder).pos > 0 { + } else if coder.pos > 0 { core::ptr::copy( - (::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8).offset((*coder).pos as isize) + (::core::ptr::addr_of_mut!(coder.buffer) as *mut u8).offset(coder.pos as isize) as *const u8, - ::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8, + ::core::ptr::addr_of_mut!(coder.buffer) as *mut u8, buf_avail, ); - (*coder).size -= (*coder).pos; - (*coder).pos = 0; + coder.size -= coder.pos; + coder.pos = 0; } - if (*coder).size > 0 { - let ret_0: lzma_ret = copy_or_code( + if coder.size > 0 { + let ret: lzma_ret = copy_or_code( coder, allocator, - in_0, + input, in_pos, in_size, - ::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8, - ::core::ptr::addr_of_mut!((*coder).size), - (*coder).allocated, + ::core::ptr::addr_of_mut!(coder.buffer) as *mut u8, + ::core::ptr::addr_of_mut!(coder.size), + coder.allocated, action, ); - if ret_0 != LZMA_OK { - return ret_0; + if ret != LZMA_OK { + return ret; } - (*coder).filtered = call_filter( - coder, - ::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8, - (*coder).size, + coder.filtered = call_filter( + coder as *mut lzma_simple_coder, + core::slice::from_raw_parts_mut( + ::core::ptr::addr_of_mut!(coder.buffer) as *mut u8, + coder.size, + ), ); - if (*coder).end_was_reached { - (*coder).filtered = (*coder).size; + if coder.end_was_reached { + coder.filtered = coder.size; } lzma_bufcpy( - ::core::ptr::addr_of_mut!((*coder).buffer) as *mut u8, - ::core::ptr::addr_of_mut!((*coder).pos), - (*coder).filtered, + ::core::ptr::addr_of_mut!(coder.buffer) as *mut u8, + ::core::ptr::addr_of_mut!(coder.pos), + coder.filtered, out, out_pos, out_size, ); } - if (*coder).end_was_reached && (*coder).pos == (*coder).size { + if coder.end_was_reached && coder.pos == coder.size { return LZMA_STREAM_END; } LZMA_OK } -unsafe extern "C" fn simple_coder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { +unsafe fn simple_coder_end(coder_ptr: *mut c_void, allocator: *const lzma_allocator) { let coder: *mut lzma_simple_coder = coder_ptr as *mut lzma_simple_coder; lzma_next_end(::core::ptr::addr_of_mut!((*coder).next), allocator); crate::alloc::internal_free((*coder).simple, allocator); crate::alloc::internal_free(coder as *mut c_void, allocator); } -unsafe extern "C" fn simple_coder_update( +unsafe fn simple_coder_update( coder_ptr: *mut c_void, allocator: *const lzma_allocator, _filters_null: *const lzma_filter, @@ -183,11 +191,11 @@ unsafe extern "C" fn simple_coder_update( reversed_filters.offset(1), ) } -pub unsafe fn lzma_simple_coder_init( +pub(crate) unsafe fn lzma_simple_coder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, - filter: Option size_t>, + filter: lzma_simple_filter_function, simple_size: size_t, unfiltered_max: size_t, alignment: u32, @@ -205,7 +213,7 @@ pub unsafe fn lzma_simple_coder_init( (*next).coder = coder as *mut c_void; (*next).code = Some( simple_code - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const u8, @@ -217,12 +225,10 @@ pub unsafe fn lzma_simple_coder_init( lzma_action, ) -> lzma_ret, ); - (*next).end = Some( - simple_coder_end as unsafe extern "C" fn(*mut c_void, *const lzma_allocator) -> (), - ); + (*next).end = Some(simple_coder_end as unsafe fn(*mut c_void, *const lzma_allocator) -> ()); (*next).update = Some( simple_coder_update - as unsafe extern "C" fn( + as unsafe fn( *mut c_void, *const lzma_allocator, *const lzma_filter, diff --git a/xz/src/simple/simple_decoder.rs b/xz/src/simple/simple_decoder.rs index d606346a..72cf3b96 100644 --- a/xz/src/simple/simple_decoder.rs +++ b/xz/src/simple/simple_decoder.rs @@ -1,5 +1,5 @@ use crate::types::*; -pub(crate) unsafe extern "C" fn lzma_simple_props_decode( +pub(crate) unsafe fn lzma_simple_props_decode( options: *mut *mut c_void, allocator: *const lzma_allocator, props: *const u8, @@ -16,7 +16,7 @@ pub(crate) unsafe extern "C" fn lzma_simple_props_decode( if opt.is_null() { return LZMA_MEM_ERROR; } - (*opt).start_offset = read32le(props); + (*opt).start_offset = read32le(&*props.cast::<[u8; 4]>()); if (*opt).start_offset == 0 { lzma_free(opt as *mut c_void, allocator); } else { diff --git a/xz/src/simple/simple_encoder.rs b/xz/src/simple/simple_encoder.rs index 2919eef1..1cea2a93 100644 --- a/xz/src/simple/simple_encoder.rs +++ b/xz/src/simple/simple_encoder.rs @@ -1,8 +1,5 @@ use crate::types::*; -pub(crate) unsafe extern "C" fn lzma_simple_props_size( - size: *mut u32, - options: *const c_void, -) -> lzma_ret { +pub(crate) unsafe fn lzma_simple_props_size(size: *mut u32, options: *const c_void) -> lzma_ret { let opt: *const lzma_options_bcj = options as *const lzma_options_bcj; *size = (if opt.is_null() || (*opt).start_offset == 0 { 0 @@ -11,14 +8,11 @@ pub(crate) unsafe extern "C" fn lzma_simple_props_size( }) as u32; LZMA_OK } -pub(crate) unsafe extern "C" fn lzma_simple_props_encode( - options: *const c_void, - out: *mut u8, -) -> lzma_ret { +pub(crate) unsafe fn lzma_simple_props_encode(options: *const c_void, out: *mut u8) -> lzma_ret { let opt: *const lzma_options_bcj = options as *const lzma_options_bcj; if opt.is_null() || (*opt).start_offset == 0 { return LZMA_OK; } - write32le(out, (*opt).start_offset); + write32le(&mut *out.cast::<[u8; 4]>(), (*opt).start_offset); LZMA_OK } diff --git a/xz/src/simple/sparc.rs b/xz/src/simple/sparc.rs index 45fb6e40..c9f5e4e2 100644 --- a/xz/src/simple/sparc.rs +++ b/xz/src/simple/sparc.rs @@ -1,15 +1,19 @@ use crate::types::*; fn sparc_code_impl(now_pos: u32, is_encoder: bool, buffer: &mut [u8]) -> size_t { let size = buffer.len() & !3; + let ptr = buffer.as_mut_ptr(); let mut i: size_t = 0; while i < size { - if buffer[i] == 0x40 && buffer[i + 1] & 0xc0 == 0 - || buffer[i] == 0x7f && buffer[i + 1] & 0xc0 == 0xc0 - { - let mut src: u32 = (buffer[i] as u32) << 24 - | (buffer[i + 1] as u32) << 16 - | (buffer[i + 2] as u32) << 8 - | buffer[i + 3] as u32; + let cur = unsafe { ptr.add(i) }; + let b0 = unsafe { *cur }; + let b1 = unsafe { *cur.add(1) }; + if b0 == 0x40 && b1 & 0xc0 == 0 || b0 == 0x7f && b1 & 0xc0 == 0xc0 { + let mut src: u32 = unsafe { + (b0 as u32) << 24 + | (b1 as u32) << 16 + | (*cur.add(2) as u32) << 8 + | *cur.add(3) as u32 + }; src <<= 2; let dest = if is_encoder { now_pos.wrapping_add(i as u32).wrapping_add(src) @@ -19,16 +23,18 @@ fn sparc_code_impl(now_pos: u32, is_encoder: bool, buffer: &mut [u8]) -> size_t let mut dest = dest >> 2; dest = 0u32.wrapping_sub(dest >> 22 & 1) << 22 & 0x3fffffff | dest & 0x3fffff | 0x40000000; - buffer[i] = (dest >> 24) as u8; - buffer[i + 1] = (dest >> 16) as u8; - buffer[i + 2] = (dest >> 8) as u8; - buffer[i + 3] = dest as u8; + unsafe { + *cur = (dest >> 24) as u8; + *cur.add(1) = (dest >> 16) as u8; + *cur.add(2) = (dest >> 8) as u8; + *cur.add(3) = dest as u8; + } } i += 4; } i } -unsafe extern "C" fn sparc_code( +unsafe fn sparc_code( _simple: *mut c_void, now_pos: u32, is_encoder: bool, @@ -54,21 +60,21 @@ unsafe fn sparc_coder_init( next, allocator, filters, - Some(sparc_code as unsafe extern "C" fn(*mut c_void, u32, bool, *mut u8, size_t) -> size_t), + sparc_code as lzma_simple_filter_function, 0, 4, 4, is_encoder, ) } -pub(crate) unsafe extern "C" fn lzma_simple_sparc_encoder_init( +pub(crate) unsafe fn lzma_simple_sparc_encoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, ) -> lzma_ret { sparc_coder_init(next, allocator, filters, true) } -pub(crate) unsafe extern "C" fn lzma_simple_sparc_decoder_init( +pub(crate) unsafe fn lzma_simple_sparc_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, diff --git a/xz/src/simple/x86.rs b/xz/src/simple/x86.rs index ab383e2a..6dbf0579 100644 --- a/xz/src/simple/x86.rs +++ b/xz/src/simple/x86.rs @@ -5,27 +5,27 @@ pub struct lzma_simple_x86 { pub prev_mask: u32, pub prev_pos: u32, } -unsafe extern "C" fn x86_code( - simple_ptr: *mut c_void, +fn x86_code_impl( + simple: &mut lzma_simple_x86, now_pos: u32, is_encoder: bool, - buffer: *mut u8, - size: size_t, + buffer: &mut [u8], ) -> size_t { - static mut MASK_TO_BIT_NUMBER: [u32; 5] = [0, 1, 2, 2, 3]; - let simple: *mut lzma_simple_x86 = simple_ptr as *mut lzma_simple_x86; - let mut prev_mask: u32 = (*simple).prev_mask; - let mut prev_pos: u32 = (*simple).prev_pos; - if size < 5 { + const MASK_TO_BIT_NUMBER: [u32; 5] = [0, 1, 2, 2, 3]; + let mut prev_mask: u32 = simple.prev_mask; + let mut prev_pos: u32 = simple.prev_pos; + if buffer.len() < 5 { return 0; } if now_pos.wrapping_sub(prev_pos) > 5 { prev_pos = now_pos.wrapping_sub(5); } - let limit: size_t = size - 5; + let limit: size_t = buffer.len() - 5; + let ptr = buffer.as_mut_ptr(); let mut buffer_pos: size_t = 0; while buffer_pos <= limit { - let mut b: u8 = *buffer.offset(buffer_pos as isize); + let cur = unsafe { ptr.add(buffer_pos) }; + let mut b: u8 = unsafe { *cur }; if b != 0xe8 && b != 0xe9 { buffer_pos += 1; } else { @@ -43,36 +43,38 @@ unsafe extern "C" fn x86_code( i += 1; } } - b = *buffer.offset((buffer_pos + 4) as isize); + b = unsafe { *cur.add(4) }; if (b == 0 || b == 0xff) && prev_mask >> 1 <= 4 && prev_mask >> 1 != 3 { - let mut src: u32 = (b as u32) << 24 - | (*buffer.offset((buffer_pos + 3) as isize) as u32) << 16 - | (*buffer.offset((buffer_pos + 2) as isize) as u32) << 8 - | *buffer.offset((buffer_pos + 1) as isize) as u32; - let mut dest: u32 = 0; - loop { - if is_encoder { - dest = src - .wrapping_add(now_pos.wrapping_add(buffer_pos as u32).wrapping_add(5)); + let mut src: u32 = unsafe { + (b as u32) << 24 + | (*cur.add(3) as u32) << 16 + | (*cur.add(2) as u32) << 8 + | *cur.add(1) as u32 + }; + let dest: u32 = loop { + let dest = if is_encoder { + src.wrapping_add(now_pos.wrapping_add(buffer_pos as u32).wrapping_add(5)) } else { - dest = src - .wrapping_sub(now_pos.wrapping_add(buffer_pos as u32).wrapping_add(5)); - } + src.wrapping_sub(now_pos.wrapping_add(buffer_pos as u32).wrapping_add(5)) + }; if prev_mask == 0 { - break; + break dest; } - let i_0: u32 = MASK_TO_BIT_NUMBER[(prev_mask >> 1) as usize]; + let i_0: u32 = + unsafe { *MASK_TO_BIT_NUMBER.as_ptr().add((prev_mask >> 1) as usize) }; b = (dest >> (24u32).wrapping_sub(i_0.wrapping_mul(8))) as u8; if b != 0 && b != 0xff { - break; + break dest; } src = dest ^ (1u32 << (32u32).wrapping_sub(i_0.wrapping_mul(8))).wrapping_sub(1); + }; + unsafe { + *cur.add(4) = !(dest >> 24 & 1).wrapping_sub(1) as u8; + *cur.add(3) = (dest >> 16) as u8; + *cur.add(2) = (dest >> 8) as u8; + *cur.add(1) = dest as u8; } - *buffer.offset((buffer_pos + 4) as isize) = !(dest >> 24 & 1).wrapping_sub(1) as u8; - *buffer.offset((buffer_pos + 3) as isize) = (dest >> 16) as u8; - *buffer.offset((buffer_pos + 2) as isize) = (dest >> 8) as u8; - *buffer.offset((buffer_pos + 1) as isize) = dest as u8; buffer_pos += 5; prev_mask = 0; } else { @@ -84,10 +86,27 @@ unsafe extern "C" fn x86_code( } } } - (*simple).prev_mask = prev_mask; - (*simple).prev_pos = prev_pos; + simple.prev_mask = prev_mask; + simple.prev_pos = prev_pos; buffer_pos } +unsafe fn x86_code( + simple_ptr: *mut c_void, + now_pos: u32, + is_encoder: bool, + buffer: *mut u8, + size: size_t, +) -> size_t { + if size == 0 { + return 0; + } + x86_code_impl( + &mut *(simple_ptr as *mut lzma_simple_x86), + now_pos, + is_encoder, + core::slice::from_raw_parts_mut(buffer, size), + ) +} unsafe fn x86_coder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, @@ -98,7 +117,7 @@ unsafe fn x86_coder_init( next, allocator, filters, - Some(x86_code as unsafe extern "C" fn(*mut c_void, u32, bool, *mut u8, size_t) -> size_t), + x86_code as lzma_simple_filter_function, core::mem::size_of::(), 5, 1, @@ -112,7 +131,7 @@ unsafe fn x86_coder_init( } ret } -pub(crate) unsafe extern "C" fn lzma_simple_x86_encoder_init( +pub(crate) unsafe fn lzma_simple_x86_encoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, @@ -124,15 +143,17 @@ pub unsafe fn lzma_bcj_x86_encode(start_offset: u32, buf: *mut u8, size: size_t) prev_mask: 0, prev_pos: (-5_i32) as u32, }; - x86_code( - ::core::ptr::addr_of_mut!(simple) as *mut c_void, + if size == 0 { + return 0; + } + x86_code_impl( + &mut simple, start_offset, true, - buf, - size, + core::slice::from_raw_parts_mut(buf, size), ) } -pub(crate) unsafe extern "C" fn lzma_simple_x86_decoder_init( +pub(crate) unsafe fn lzma_simple_x86_decoder_init( next: *mut lzma_next_coder, allocator: *const lzma_allocator, filters: *const lzma_filter_info, @@ -144,11 +165,13 @@ pub unsafe fn lzma_bcj_x86_decode(start_offset: u32, buf: *mut u8, size: size_t) prev_mask: 0, prev_pos: (-5_i32) as u32, }; - x86_code( - ::core::ptr::addr_of_mut!(simple) as *mut c_void, + if size == 0 { + return 0; + } + x86_code_impl( + &mut simple, start_offset, false, - buf, - size, + core::slice::from_raw_parts_mut(buf, size), ) } diff --git a/xz/src/types.rs b/xz/src/types.rs index 21bf1a8e..49869144 100644 --- a/xz/src/types.rs +++ b/xz/src/types.rs @@ -1,26 +1,4 @@ pub use std::os::raw::{c_char, c_int, c_long, c_uchar, c_uint, c_ulong, c_ulonglong, c_void}; -#[cfg(windows)] -use windows_sys::Win32::Foundation::{CloseHandle, HANDLE, WAIT_OBJECT_0}; -#[cfg(windows)] -use windows_sys::Win32::System::SystemInformation::GetTickCount; -#[cfg(windows)] -use windows_sys::Win32::System::Threading::{ - DeleteCriticalSection, EnterCriticalSection, InitializeConditionVariable, - InitializeCriticalSection, LeaveCriticalSection, SleepConditionVariableCS, WaitForSingleObject, - WakeConditionVariable, CONDITION_VARIABLE, CRITICAL_SECTION, INFINITE, -}; - -#[cfg(windows)] -unsafe extern "C" { - fn _beginthreadex( - security: *mut c_void, - stack_size: c_uint, - start_address: Option u32>, - arglist: *mut c_void, - initflag: c_uint, - thrdaddr: *mut c_uint, - ) -> usize; -} #[cfg(all(target_family = "wasm", target_os = "unknown"))] pub type size_t = usize; #[cfg(not(all(target_family = "wasm", target_os = "unknown")))] @@ -47,8 +25,8 @@ pub type probability = u16; #[derive(Copy, Clone)] #[repr(C)] pub struct lzma_allocator { - pub alloc: Option *mut c_void>, - pub free: Option ()>, + pub alloc: Option *mut c_void>, + pub free: Option ()>, pub opaque: *mut c_void, } #[derive(Copy, Clone)] @@ -57,57 +35,13 @@ pub struct lzma_filter { pub id: lzma_vli, pub options: *mut c_void, } -pub type lzma_end_function = Option ()>; -pub type lzma_code_function = Option< - unsafe extern "C" fn( - *mut c_void, - *const lzma_allocator, - *const u8, - *mut size_t, - size_t, - *mut u8, - *mut size_t, - size_t, - lzma_action, - ) -> lzma_ret, ->; -pub type lzma_next_coder = lzma_next_coder_s; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct lzma_next_coder_s { - pub coder: *mut c_void, - pub id: lzma_vli, - pub init: uintptr_t, - pub code: lzma_code_function, - pub end: lzma_end_function, - pub get_progress: Option ()>, - pub get_check: Option lzma_check>, - pub memconfig: Option lzma_ret>, - pub update: Option< - unsafe extern "C" fn( - *mut c_void, - *const lzma_allocator, - *const lzma_filter, - *const lzma_filter, - ) -> lzma_ret, - >, - pub set_out_limit: Option lzma_ret>, -} -pub type lzma_init_function = Option< - unsafe extern "C" fn( - *mut lzma_next_coder, - *const lzma_allocator, - *const lzma_filter_info, - ) -> lzma_ret, ->; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct lzma_filter_info_s { - pub id: lzma_vli, - pub init: lzma_init_function, - pub options: *mut c_void, -} -pub type lzma_filter_info = lzma_filter_info_s; +pub use crate::common::common_types::{ + ISEQ_END, ISEQ_ERROR, ISEQ_FINISH, ISEQ_FULL_BARRIER, ISEQ_FULL_FLUSH, ISEQ_RUN, + ISEQ_SYNC_FLUSH, LZMA_MEMUSAGE_BASE, LZMA_SUPPORTED_FLAGS, LZMA_THREADS_MAX, + lzma_code_function, lzma_end_function, lzma_filter_info, lzma_filter_info_s, + lzma_init_function, lzma_internal, lzma_internal_s, lzma_internal_sequence, lzma_next_coder, + lzma_next_coder_s, +}; #[derive(Copy, Clone)] #[repr(C)] pub struct lzma_options_lzma { @@ -136,24 +70,6 @@ pub struct lzma_options_lzma { pub reserved_ptr1: *mut c_void, pub reserved_ptr2: *mut c_void, } -pub type lzma_internal_sequence = c_uint; -pub const ISEQ_RUN: lzma_internal_sequence = 0; -pub const ISEQ_SYNC_FLUSH: lzma_internal_sequence = 1; -pub const ISEQ_FULL_FLUSH: lzma_internal_sequence = 2; -pub const ISEQ_FINISH: lzma_internal_sequence = 3; -pub const ISEQ_FULL_BARRIER: lzma_internal_sequence = 4; -pub const ISEQ_END: lzma_internal_sequence = 5; -pub const ISEQ_ERROR: lzma_internal_sequence = 6; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct lzma_internal_s { - pub next: lzma_next_coder, - pub sequence: lzma_internal_sequence, - pub avail_in: size_t, - pub supported_actions: [bool; 5], - pub allow_buf_error: bool, -} -pub type lzma_internal = lzma_internal_s; #[derive(Copy, Clone)] #[repr(C)] pub struct lzma_stream { @@ -241,7 +157,6 @@ pub const LZMA_IGNORE_CHECK: c_uint = 0x10; pub const LZMA_FAIL_FAST: c_uint = 0x20; pub const LZMA_STREAM_HEADER_SIZE: u32 = 12; pub const LZMA_BLOCK_HEADER_SIZE_MAX: u32 = 1024; -pub const LZMA_MEMUSAGE_BASE: u64 = 1 << 15; pub const LZMA_DICT_SIZE_MIN: c_uint = 4096; pub const STATE_LIT_LIT: lzma_lzma_state = 0; pub const STATE_MATCH_LIT_LIT: lzma_lzma_state = 1; @@ -262,18 +177,11 @@ pub const LZMA_PB_MAX: u32 = 4; pub const LZMA_DELTA_DIST_MAX: u32 = 256; pub const LZMA_BACKWARD_SIZE_MIN: u32 = 4; pub const LZMA_BACKWARD_SIZE_MAX: u64 = 1 << 34; -pub const LZMA_SUPPORTED_FLAGS: c_uint = LZMA_TELL_NO_CHECK - | LZMA_TELL_UNSUPPORTED_CHECK - | LZMA_TELL_ANY_CHECK - | LZMA_IGNORE_CHECK - | LZMA_CONCATENATED - | LZMA_FAIL_FAST; pub const UINTPTR_MAX: c_ulong = uintptr_t::MAX as c_ulong; pub const SIZE_MAX: c_ulong = UINTPTR_MAX; pub const INDEX_INDICATOR: u8 = 0; pub const UNPADDED_SIZE_MIN: c_ulonglong = 5; pub const UNPADDED_SIZE_MAX: c_ulonglong = LZMA_VLI_MAX & !3; -pub const LZMA_THREADS_MAX: u32 = 16384; pub const LZMA_DELTA_DIST_MIN: u32 = 1; pub const LZMA_LZMA1EXT_ALLOW_EOPM: c_uint = 0x1; pub const RC_BIT_MODEL_TOTAL_BITS: u32 = 11; @@ -317,8 +225,6 @@ pub const LZMA_PRESET_EXTREME: c_uint = 1u32 << 31; pub const COMPRESSED_SIZE_MAX: c_ulonglong = LZMA_VLI_MAX .wrapping_sub(LZMA_BLOCK_HEADER_SIZE_MAX as u64) .wrapping_sub(LZMA_CHECK_SIZE_MAX as u64); -pub const MYTHREAD_RET_VALUE: *mut c_void = core::ptr::null_mut(); -pub const SIG_SETMASK: c_int = 3; pub type worker_state = c_uint; pub type lzma_index_iter_mode = c_uint; pub const THR_IDLE: worker_state = 0; @@ -465,7 +371,7 @@ pub struct lzma_check_state { #[derive(Copy, Clone)] #[repr(C)] pub struct lzma_options_delta { - pub type_0: lzma_delta_type, + pub type_: lzma_delta_type, pub dist: u32, pub reserved_int1: u32, pub reserved_int2: u32, @@ -495,25 +401,44 @@ pub struct lzma_dict { #[repr(C)] pub struct lzma_lz_decoder { pub coder: *mut c_void, - pub code: Option< - unsafe extern "C" fn( - *mut c_void, - *mut lzma_dict, - *const u8, - *mut size_t, - size_t, - ) -> lzma_ret, - >, - pub reset: Option ()>, - pub set_uncompressed: Option ()>, - pub end: Option ()>, + pub code: lzma_lz_decoder_code_function, + pub reset: Option ()>, + pub set_uncompressed: Option ()>, + pub end: Option ()>, } +pub type lzma_lz_decoder_code_function = + unsafe fn(*mut c_void, *mut lzma_dict, *const u8, *mut size_t, size_t) -> lzma_ret; + +#[cold] +pub unsafe fn lzma_lz_decoder_code_uninitialized( + _coder: *mut c_void, + _dict: *mut lzma_dict, + _input: *const u8, + _in_pos: *mut size_t, + _in_size: size_t, +) -> lzma_ret { + panic!("uninitialized LZ decoder callback") +} + #[derive(Copy, Clone)] #[repr(C)] pub struct lzma_match { pub len: u32, pub dist: u32, } +pub type lzma_mf_find_function = unsafe fn(*mut lzma_mf, *mut lzma_match) -> u32; +pub type lzma_mf_skip_function = unsafe fn(*mut lzma_mf, u32) -> (); + +#[cold] +pub unsafe fn lzma_mf_find_uninitialized(_mf: *mut lzma_mf, _matches: *mut lzma_match) -> u32 { + panic!("uninitialized match finder callback") +} + +#[cold] +pub unsafe fn lzma_mf_skip_uninitialized(_mf: *mut lzma_mf, _amount: u32) { + panic!("uninitialized match finder skip callback") +} + #[derive(Copy, Clone)] #[repr(C)] pub struct lzma_mf_s { @@ -527,8 +452,8 @@ pub struct lzma_mf_s { pub read_limit: u32, pub write_pos: u32, pub pending: u32, - pub find: Option u32>, - pub skip: Option ()>, + pub find: lzma_mf_find_function, + pub skip: lzma_mf_skip_function, pub hash: *mut u32, pub son: *mut u32, pub cyclic_pos: u32, @@ -554,12 +479,23 @@ pub struct lzma_delta_coder { #[repr(C)] pub struct lzma_lz_encoder { pub coder: *mut c_void, - pub code: Option< - unsafe extern "C" fn(*mut c_void, *mut lzma_mf, *mut u8, *mut size_t, size_t) -> lzma_ret, - >, - pub end: Option ()>, - pub options_update: Option lzma_ret>, - pub set_out_limit: Option lzma_ret>, + pub code: lzma_lz_encoder_code_function, + pub end: Option ()>, + pub options_update: Option lzma_ret>, + pub set_out_limit: Option lzma_ret>, +} +pub type lzma_lz_encoder_code_function = + unsafe fn(*mut c_void, *mut lzma_mf, *mut u8, *mut size_t, size_t) -> lzma_ret; + +#[cold] +pub unsafe fn lzma_lz_encoder_code_uninitialized( + _coder: *mut c_void, + _mf: *mut lzma_mf, + _out: *mut u8, + _out_pos: *mut size_t, + _out_size: size_t, +) -> lzma_ret { + panic!("uninitialized LZ encoder callback") } #[derive(Copy, Clone)] #[repr(C)] @@ -634,418 +570,12 @@ pub struct lzma_lzma1_encoder_s { } pub type lzma_lzma1_encoder = lzma_lzma1_encoder_s; #[inline] -pub fn read32le(buf: *const u8) -> u32 { - unsafe { core::ptr::read_unaligned(buf.cast::()).to_le() } -} -#[inline] -pub fn write32le(buf: *mut u8, num: u32) { - unsafe { - core::ptr::write_unaligned(buf.cast::(), num.to_le()); - } -} -pub type __uint32_t = u32; -pub type __darwin_time_t = c_long; -pub type __darwin_sigset_t = __uint32_t; -#[cfg(not(windows))] -#[derive(Copy, Clone)] -#[repr(C)] -pub struct __darwin_pthread_handler_rec { - pub __routine: Option ()>, - pub __arg: *mut c_void, - pub __next: *mut __darwin_pthread_handler_rec, -} -#[cfg(not(windows))] -#[derive(Copy, Clone)] -#[repr(C)] -pub struct _opaque_pthread_attr_t { - pub __sig: c_long, - pub __opaque: [c_char; 56], -} -#[cfg(not(windows))] -#[derive(Copy, Clone)] -#[repr(C)] -pub struct _opaque_pthread_cond_t { - pub __sig: c_long, - pub __opaque: [c_char; 40], -} -#[cfg(not(windows))] -#[derive(Copy, Clone)] -#[repr(C)] -pub struct _opaque_pthread_condattr_t { - pub __sig: c_long, - pub __opaque: [c_char; 8], -} -#[cfg(not(windows))] -#[derive(Copy, Clone)] -#[repr(C)] -pub struct _opaque_pthread_mutex_t { - pub __sig: c_long, - pub __opaque: [c_char; 56], -} -#[cfg(not(windows))] -#[derive(Copy, Clone)] -#[repr(C)] -pub struct _opaque_pthread_mutexattr_t { - pub __sig: c_long, - pub __opaque: [c_char; 8], -} -#[cfg(not(windows))] -#[derive(Copy, Clone)] -#[repr(C)] -pub struct _opaque_pthread_t { - pub __sig: c_long, - pub __cleanup_stack: *mut __darwin_pthread_handler_rec, - pub __opaque: [c_char; 8176], -} -#[cfg(not(windows))] -pub type __darwin_pthread_attr_t = _opaque_pthread_attr_t; -#[cfg(not(windows))] -pub type __darwin_pthread_cond_t = _opaque_pthread_cond_t; -#[cfg(not(windows))] -pub type __darwin_pthread_condattr_t = _opaque_pthread_condattr_t; -#[cfg(not(windows))] -pub type __darwin_pthread_mutex_t = _opaque_pthread_mutex_t; -#[cfg(not(windows))] -pub type __darwin_pthread_mutexattr_t = _opaque_pthread_mutexattr_t; -#[cfg(not(windows))] -pub type __darwin_pthread_t = *mut _opaque_pthread_t; -#[cfg(not(windows))] -pub type pthread_attr_t = __darwin_pthread_attr_t; -pub type sigset_t = __darwin_sigset_t; -pub type time_t = __darwin_time_t; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct timespec { - pub tv_sec: __darwin_time_t, - pub tv_nsec: c_long, -} -pub type clockid_t = c_uint; -pub const _CLOCK_THREAD_CPUTIME_ID: clockid_t = 16; -pub const _CLOCK_PROCESS_CPUTIME_ID: clockid_t = 12; -pub const _CLOCK_UPTIME_RAW_APPROX: clockid_t = 9; -pub const _CLOCK_UPTIME_RAW: clockid_t = 8; -pub const _CLOCK_MONOTONIC_RAW_APPROX: clockid_t = 5; -pub const _CLOCK_MONOTONIC_RAW: clockid_t = 4; -pub const _CLOCK_MONOTONIC: clockid_t = 6; -pub const _CLOCK_REALTIME: clockid_t = 0; -#[cfg(not(windows))] -pub type pthread_cond_t = __darwin_pthread_cond_t; -#[cfg(not(windows))] -pub type pthread_condattr_t = __darwin_pthread_condattr_t; -#[cfg(not(windows))] -pub type pthread_mutex_t = __darwin_pthread_mutex_t; -#[cfg(not(windows))] -pub type pthread_mutexattr_t = __darwin_pthread_mutexattr_t; -#[cfg(not(windows))] -pub type pthread_t = __darwin_pthread_t; -#[cfg(windows)] -pub type pthread_attr_t = HANDLE; -#[cfg(windows)] -pub type pthread_cond_t = CONDITION_VARIABLE; -#[cfg(windows)] -pub type pthread_condattr_t = HANDLE; -#[cfg(windows)] -pub type pthread_mutex_t = CRITICAL_SECTION; -#[cfg(windows)] -pub type pthread_mutexattr_t = HANDLE; -#[cfg(windows)] -pub type pthread_t = HANDLE; -#[cfg(not(windows))] -pub type mythread = pthread_t; -#[cfg(windows)] -pub type mythread = HANDLE; -#[cfg(not(windows))] -pub type mythread_mutex = pthread_mutex_t; -#[cfg(windows)] -pub type mythread_mutex = CRITICAL_SECTION; -#[cfg(not(windows))] -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mythread_cond { - pub cond: pthread_cond_t, - pub clk_id: clockid_t, -} -#[cfg(windows)] -pub type mythread_cond = CONDITION_VARIABLE; -#[cfg(not(windows))] -pub type mythread_condtime = timespec; -#[cfg(windows)] -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mythread_condtime { - pub start: u32, - pub timeout: u32, -} -#[cfg(not(windows))] -#[inline] -pub fn mythread_sigmask(how: c_int, set: *const sigset_t, oset: *mut sigset_t) { - let _ret: c_int = - unsafe { pthread_sigmask(how, set as *const sigset_t, oset as *mut sigset_t) }; -} -#[cfg(windows)] -#[inline] -pub fn mythread_sigmask(_how: c_int, _set: *const sigset_t, _oset: *mut sigset_t) {} -#[cfg(windows)] -struct mythread_start_info { - func: Option *mut c_void>, - arg: *mut c_void, -} -#[cfg(windows)] -unsafe extern "system" fn mythread_start(param: *mut c_void) -> u32 { - let info = Box::from_raw(param.cast::()); - if let Some(func) = info.func { - let _ = func(info.arg); - } - 0 -} -#[cfg(not(windows))] -#[inline] -pub fn mythread_create( - thread: *mut mythread, - func: Option *mut c_void>, - arg: *mut c_void, -) -> c_int { - let mut old: sigset_t = 0; - let mut all: sigset_t = 0; - all = !(0 as sigset_t); - mythread_sigmask( - SIG_SETMASK, - ::core::ptr::addr_of_mut!(all), - ::core::ptr::addr_of_mut!(old), - ); - let ret: c_int = unsafe { - pthread_create( - thread as *mut pthread_t, - core::ptr::null(), - func as Option *mut c_void>, - arg as *mut c_void, - ) - }; - mythread_sigmask( - SIG_SETMASK, - ::core::ptr::addr_of_mut!(old), - core::ptr::null_mut(), - ); - ret -} -#[cfg(windows)] -#[inline] -pub fn mythread_create( - thread: *mut mythread, - func: Option *mut c_void>, - arg: *mut c_void, -) -> c_int { - let info = Box::into_raw(Box::new(mythread_start_info { func, arg })); - let ret = unsafe { - _beginthreadex( - core::ptr::null_mut(), - 0, - Some(mythread_start), - info.cast::(), - 0, - core::ptr::null_mut(), - ) - }; - if ret == 0 { - unsafe { - let _ = Box::from_raw(info); - } - -1 - } else { - unsafe { - *thread = ret as HANDLE; - } - 0 - } -} -#[cfg(not(windows))] -#[inline] -pub fn mythread_join(thread: mythread) -> c_int { - unsafe { pthread_join(thread as pthread_t, core::ptr::null_mut()) } +pub fn read32le(buf: &[u8; 4]) -> u32 { + u32::from_le_bytes(*buf) } -#[cfg(windows)] #[inline] -pub fn mythread_join(thread: mythread) -> c_int { - let mut ret = 0; - unsafe { - if WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0 { - ret = -1; - } - if CloseHandle(thread) == 0 { - ret = -1; - } - } - ret -} -#[cfg(not(windows))] -#[inline] -pub fn mythread_mutex_init(mutex: *mut mythread_mutex) -> c_int { - unsafe { pthread_mutex_init(mutex as *mut pthread_mutex_t, core::ptr::null()) } -} -#[cfg(windows)] -#[inline] -pub fn mythread_mutex_init(mutex: *mut mythread_mutex) -> c_int { - unsafe { - InitializeCriticalSection(mutex); - } - 0 -} -#[cfg(not(windows))] -#[inline] -pub fn mythread_mutex_destroy(mutex: *mut mythread_mutex) { - let _ret: c_int = unsafe { pthread_mutex_destroy(mutex as *mut pthread_mutex_t) }; -} -#[cfg(windows)] -#[inline] -pub fn mythread_mutex_destroy(mutex: *mut mythread_mutex) { - unsafe { - DeleteCriticalSection(mutex); - } -} -#[cfg(not(windows))] -#[inline] -pub fn mythread_mutex_lock(mutex: *mut mythread_mutex) { - let _ret: c_int = unsafe { pthread_mutex_lock(mutex as *mut pthread_mutex_t) }; -} -#[cfg(windows)] -#[inline] -pub fn mythread_mutex_lock(mutex: *mut mythread_mutex) { - unsafe { - EnterCriticalSection(mutex); - } -} -#[cfg(not(windows))] -#[inline] -pub fn mythread_mutex_unlock(mutex: *mut mythread_mutex) { - let _ret: c_int = unsafe { pthread_mutex_unlock(mutex as *mut pthread_mutex_t) }; -} -#[cfg(windows)] -#[inline] -pub fn mythread_mutex_unlock(mutex: *mut mythread_mutex) { - unsafe { - LeaveCriticalSection(mutex); - } -} -#[cfg(not(windows))] -#[inline] -pub fn mythread_cond_init(mycond: *mut mythread_cond) -> c_int { - return unsafe { - (*mycond).clk_id = _CLOCK_REALTIME; - pthread_cond_init(::core::ptr::addr_of_mut!((*mycond).cond), core::ptr::null()) - }; -} -#[cfg(windows)] -#[inline] -pub fn mythread_cond_init(cond: *mut mythread_cond) -> c_int { - unsafe { - InitializeConditionVariable(cond); - } - 0 -} -#[cfg(not(windows))] -#[inline] -pub fn mythread_cond_destroy(cond: *mut mythread_cond) { - let _ret: c_int = unsafe { pthread_cond_destroy(::core::ptr::addr_of_mut!((*cond).cond)) }; -} -#[cfg(windows)] -#[inline] -pub fn mythread_cond_destroy(_cond: *mut mythread_cond) {} -#[cfg(not(windows))] -#[inline] -pub fn mythread_cond_signal(cond: *mut mythread_cond) { - let _ret: c_int = unsafe { pthread_cond_signal(::core::ptr::addr_of_mut!((*cond).cond)) }; -} -#[cfg(windows)] -#[inline] -pub fn mythread_cond_signal(cond: *mut mythread_cond) { - unsafe { - WakeConditionVariable(cond); - } -} -#[cfg(not(windows))] -#[inline] -pub fn mythread_cond_wait(cond: *mut mythread_cond, mutex: *mut mythread_mutex) { - let _ret: c_int = unsafe { - pthread_cond_wait( - ::core::ptr::addr_of_mut!((*cond).cond), - mutex as *mut pthread_mutex_t, - ) - }; -} -#[cfg(windows)] -#[inline] -pub fn mythread_cond_wait(cond: *mut mythread_cond, mutex: *mut mythread_mutex) { - unsafe { - let _ = SleepConditionVariableCS(cond, mutex, INFINITE); - } -} -#[cfg(not(windows))] -#[inline] -pub fn mythread_cond_timedwait( - cond: *mut mythread_cond, - mutex: *mut mythread_mutex, - condtime: *const mythread_condtime, -) -> c_int { - let ret: c_int = unsafe { - pthread_cond_timedwait( - ::core::ptr::addr_of_mut!((*cond).cond), - mutex as *mut pthread_mutex_t, - condtime as *const timespec, - ) - }; - ret -} -#[cfg(windows)] -#[inline] -pub fn mythread_cond_timedwait( - cond: *mut mythread_cond, - mutex: *mut mythread_mutex, - condtime: *const mythread_condtime, -) -> c_int { - let (start, timeout_ms) = unsafe { ((*condtime).start, (*condtime).timeout) }; - let elapsed = unsafe { GetTickCount().wrapping_sub(start) }; - let timeout = if elapsed >= timeout_ms { - 0 - } else { - timeout_ms - elapsed - }; - let ret = unsafe { SleepConditionVariableCS(cond, mutex, timeout) }; - i32::from(ret == 0) -} -#[cfg(not(windows))] -#[inline] -pub fn mythread_condtime_set( - condtime: *mut mythread_condtime, - cond: *const mythread_cond, - timeout_ms: u32, -) { - unsafe { - (*condtime).tv_sec = timeout_ms.wrapping_div(1000) as time_t as __darwin_time_t; - (*condtime).tv_nsec = timeout_ms.wrapping_rem(1000).wrapping_mul(1_000_000) as c_long; - let mut now: timespec = timespec { - tv_sec: 0, - tv_nsec: 0, - }; - let _ret: c_int = clock_gettime((*cond).clk_id, ::core::ptr::addr_of_mut!(now)); - (*condtime).tv_sec += now.tv_sec; - (*condtime).tv_nsec += now.tv_nsec; - if (*condtime).tv_nsec >= 1_000_000_000 { - (*condtime).tv_nsec -= 1_000_000_000; - (*condtime).tv_sec += 1; - } - } -} -#[cfg(windows)] -#[inline] -pub fn mythread_condtime_set( - condtime: *mut mythread_condtime, - _cond: *const mythread_cond, - timeout_ms: u32, -) { - unsafe { - (*condtime).start = GetTickCount(); - (*condtime).timeout = timeout_ms; - } +pub fn write32le(buf: &mut [u8; 4], num: u32) { + *buf = num.to_le_bytes(); } #[inline] pub fn vli_ceil4(vli: lzma_vli) -> lzma_vli { @@ -1075,15 +605,11 @@ pub unsafe fn mf_avail(mf: *const lzma_mf) -> u32 { } #[inline] pub unsafe fn mf_skip(mf: *mut lzma_mf, amount: u32) { - mf_skip_raw(mf, amount, (*mf).skip.unwrap()); + mf_skip_raw(mf, amount, (*mf).skip); } #[inline(always)] -pub unsafe fn mf_skip_raw( - mf: *mut lzma_mf, - amount: u32, - skip: unsafe extern "C" fn(*mut lzma_mf, u32) -> (), -) { +pub unsafe fn mf_skip_raw(mf: *mut lzma_mf, amount: u32, skip: unsafe fn(*mut lzma_mf, u32) -> ()) { if amount != 0 { skip(mf, amount); (*mf).read_ahead = (*mf).read_ahead.wrapping_add(amount); @@ -1111,24 +637,19 @@ pub unsafe fn lzma_memcmplen(buf1: *const u8, buf2: *const u8, mut len: u32, lim limit } - #[cfg(not(all( - target_endian = "little", - any(target_arch = "aarch64", target_arch = "x86_64") - )))] - while len < limit && *buf1.offset(len as isize) == *buf2.offset(len as isize) { - len += 1; - } - #[cfg(not(all( target_endian = "little", any(target_arch = "aarch64", target_arch = "x86_64") )))] { + while len < limit && *buf1.add(len as usize) == *buf2.add(len as usize) { + len += 1; + } len } } #[inline] -pub unsafe fn get_dist_slot(dist: u32) -> u32 { +pub fn get_dist_slot(dist: u32) -> u32 { if dist < 1 << FASTPOS_BITS + (0 + 0 * (FASTPOS_BITS - 1)) { return lzma_fastpos[dist as usize] as u32; } @@ -1140,30 +661,26 @@ pub unsafe fn get_dist_slot(dist: u32) -> u32 { .wrapping_add((2 * (0 + 2 * (FASTPOS_BITS - 1))) as u32) } #[inline] -unsafe fn rc_price_at(index: u32) -> u32 { +fn rc_price_at(index: u32) -> u32 { debug_assert!((index as usize) < 128); - *(::core::ptr::addr_of!(lzma_rc_prices) as *const u8).add(index as usize) as u32 + unsafe { *lzma_rc_prices.as_ptr().add(index as usize) as u32 } } #[inline] pub fn rc_bit_price(prob: probability, bit: u32) -> u32 { - unsafe { - rc_price_at( - ((prob as u32 ^ 0u32.wrapping_sub(bit) & (RC_BIT_MODEL_TOTAL as u32).wrapping_sub(1)) - >> RC_MOVE_REDUCING_BITS) as u32, - ) - } + rc_price_at( + ((prob as u32 ^ 0u32.wrapping_sub(bit) & (RC_BIT_MODEL_TOTAL as u32).wrapping_sub(1)) + >> RC_MOVE_REDUCING_BITS) as u32, + ) } #[inline] pub fn rc_bit_0_price(prob: probability) -> u32 { - unsafe { rc_price_at((prob >> RC_MOVE_REDUCING_BITS) as u32) } + rc_price_at((prob >> RC_MOVE_REDUCING_BITS) as u32) } #[inline] pub fn rc_bit_1_price(prob: probability) -> u32 { - unsafe { - rc_price_at( - ((prob as u32 ^ RC_BIT_MODEL_TOTAL.wrapping_sub(1)) >> RC_MOVE_REDUCING_BITS) as u32, - ) - } + rc_price_at( + ((prob as u32 ^ RC_BIT_MODEL_TOTAL.wrapping_sub(1)) >> RC_MOVE_REDUCING_BITS) as u32, + ) } #[inline] pub unsafe fn rc_bittree_price(probs: *const probability, bit_levels: u32, mut symbol: u32) -> u32 { @@ -1297,23 +814,26 @@ pub struct lzma_mt { pub struct lzma_filter_coder { pub id: lzma_vli, pub init: lzma_init_function, - pub memusage: Option u64>, + pub memusage: Option u64>, } -pub type lzma_filter_find = Option *const lzma_filter_coder>; +pub type lzma_filter_find = Option *const lzma_filter_coder>; +pub(crate) type lzma_simple_filter_function = + unsafe fn(*mut c_void, u32, bool, *mut u8, size_t) -> size_t; + #[derive(Copy, Clone)] #[repr(C)] -pub struct lzma_simple_coder { - pub next: lzma_next_coder, - pub end_was_reached: bool, - pub is_encoder: bool, - pub filter: Option size_t>, - pub simple: *mut c_void, - pub now_pos: u32, - pub allocated: size_t, - pub pos: size_t, - pub filtered: size_t, - pub size: size_t, - pub buffer: [u8; 0], +pub(crate) struct lzma_simple_coder { + pub(crate) next: lzma_next_coder, + pub(crate) end_was_reached: bool, + pub(crate) is_encoder: bool, + pub(crate) filter: lzma_simple_filter_function, + pub(crate) simple: *mut c_void, + pub(crate) now_pos: u32, + pub(crate) allocated: size_t, + pub(crate) pos: size_t, + pub(crate) filtered: size_t, + pub(crate) size: size_t, + pub(crate) buffer: [u8; 0], } pub use crate::check::check::{ lzma_check_finish, lzma_check_init, lzma_check_is_supported, lzma_check_size, lzma_check_update, @@ -1355,6 +875,16 @@ pub use crate::common::stream_flags_decoder::{ pub use crate::common::stream_flags_encoder::{ lzma_stream_footer_encode, lzma_stream_header_encode, }; +pub use crate::common::threading::{ + __darwin_time_t, _CLOCK_MONOTONIC, _CLOCK_MONOTONIC_RAW, _CLOCK_MONOTONIC_RAW_APPROX, + _CLOCK_PROCESS_CPUTIME_ID, _CLOCK_REALTIME, _CLOCK_THREAD_CPUTIME_ID, _CLOCK_UPTIME_RAW, + _CLOCK_UPTIME_RAW_APPROX, MYTHREAD_RET_VALUE, SIG_SETMASK, clockid_t, mythread, mythread_cond, + mythread_cond_destroy, mythread_cond_init, mythread_cond_signal, mythread_cond_timedwait, + mythread_cond_wait, mythread_condtime, mythread_condtime_set, mythread_create, mythread_join, + mythread_mutex, mythread_mutex_destroy, mythread_mutex_init, mythread_mutex_lock, + mythread_mutex_unlock, mythread_sigmask, pthread_attr_t, pthread_cond_t, pthread_condattr_t, + pthread_mutex_t, pthread_mutexattr_t, pthread_t, sigset_t, time_t, timespec, +}; pub use crate::common::vli_decoder::lzma_vli_decode; pub use crate::common::vli_encoder::lzma_vli_encode; pub use crate::common::vli_size::lzma_vli_size; @@ -1370,33 +900,8 @@ pub use crate::lzma::lzma_encoder::lzma_lzma_lclppb_encode; pub(crate) use crate::lzma::lzma_encoder::{lzma_lzma_encoder_init, lzma_lzma_encoder_memusage}; pub use crate::lzma::lzma_encoder_presets::lzma_lzma_preset; pub use crate::rangecoder::price_table::lzma_rc_prices; -pub use crate::simple::simple_coder::lzma_simple_coder_init; -#[cfg(not(windows))] -extern "C" { - pub fn clock_gettime(__clock_id: clockid_t, __tp: *mut timespec) -> c_int; - pub fn pthread_cond_destroy(_: *mut pthread_cond_t) -> c_int; - pub fn pthread_cond_init(_: *mut pthread_cond_t, _: *const pthread_condattr_t) -> c_int; - pub fn pthread_cond_signal(_: *mut pthread_cond_t) -> c_int; - pub fn pthread_cond_timedwait( - _: *mut pthread_cond_t, - _: *mut pthread_mutex_t, - _: *const timespec, - ) -> c_int; - pub fn pthread_cond_wait(_: *mut pthread_cond_t, _: *mut pthread_mutex_t) -> c_int; - pub fn pthread_create( - _: *mut pthread_t, - _: *const pthread_attr_t, - _: Option *mut c_void>, - _: *mut c_void, - ) -> c_int; - pub fn pthread_join(_: pthread_t, _: *mut *mut c_void) -> c_int; - pub fn pthread_mutex_destroy(_: *mut pthread_mutex_t) -> c_int; - pub fn pthread_mutex_init(_: *mut pthread_mutex_t, _: *const pthread_mutexattr_t) -> c_int; - pub fn pthread_mutex_lock(_: *mut pthread_mutex_t) -> c_int; - pub fn pthread_mutex_unlock(_: *mut pthread_mutex_t) -> c_int; - pub fn pthread_sigmask(_: c_int, _: *const sigset_t, _: *mut sigset_t) -> c_int; -} -extern "C" { +pub(crate) use crate::simple::simple_coder::lzma_simple_coder_init; +unsafe extern "C" { pub fn memcmp(s1: *const c_void, s2: *const c_void, n: size_t) -> c_int; pub fn strlen(s: *const c_char) -> size_t; }