refactor(decompress): move legacy threshold policy to Rust
Keep the target-specific ZSTD_LEGACY_SUPPORT macro in the C translation unit, but pass its scalar value through a Rust policy bridge before legacy dispatch. Rust now accepts only the historical supported range 1 through 7 and normalizes all other values, including a missing projection, to disabled. ABI assertions and focused tests preserve the compile-time configuration contract. Test Plan: Not run in this atomic commit; the capped serial Rust, native, smoke, and original test-suite verification follows.
This commit is contained in:
@@ -299,6 +299,17 @@ struct ZSTD_rustDctxView {
|
||||
dctx_size: usize,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct ZSTD_rustLegacySupportProjection {
|
||||
pub configured_level: c_uint,
|
||||
}
|
||||
|
||||
const _: () = {
|
||||
assert!(offset_of!(ZSTD_rustLegacySupportProjection, configured_level) == 0);
|
||||
assert!(size_of::<ZSTD_rustLegacySupportProjection>() == size_of::<c_uint>());
|
||||
};
|
||||
|
||||
const _: () = {
|
||||
assert!(offset_of!(ZSTD_rustDctxView, dctx) == 0);
|
||||
assert!(offset_of!(ZSTD_rustDctxView, static_size) == 29 * size_of::<usize>());
|
||||
@@ -645,6 +656,50 @@ unsafe fn configured_legacy_support() -> u32 {
|
||||
}
|
||||
}
|
||||
|
||||
/// Normalize the C build's legacy-support threshold before dispatch.
|
||||
///
|
||||
/// C supplies the active compile-time value because test builds intentionally
|
||||
/// compile every legacy module while varying `ZSTD_LEGACY_SUPPORT`. Rust owns
|
||||
/// the public policy that accepts levels 1 through 7 and treats every other
|
||||
/// value as legacy-disabled.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn ZSTD_rust_legacy_support_policy(
|
||||
projection: *const ZSTD_rustLegacySupportProjection,
|
||||
) -> c_uint {
|
||||
let Some(projection) = (unsafe { projection.as_ref() }) else {
|
||||
return 0;
|
||||
};
|
||||
let configured = projection.configured_level;
|
||||
if (1..=7).contains(&configured) {
|
||||
configured
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod legacy_support_policy_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn preserves_supported_thresholds_and_disables_other_values() {
|
||||
for (configured, expected) in [(0, 0), (1, 1), (4, 4), (7, 7), (8, 0), (c_uint::MAX, 0)] {
|
||||
let projection = ZSTD_rustLegacySupportProjection {
|
||||
configured_level: configured,
|
||||
};
|
||||
assert_eq!(
|
||||
unsafe { ZSTD_rust_legacy_support_policy(&projection) },
|
||||
expected
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_missing_configuration_projection() {
|
||||
assert_eq!(unsafe { ZSTD_rust_legacy_support_policy(ptr::null()) }, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/* This is the Rust equivalent of ZSTD_isLegacy(). The v0.1 magic is the one
|
||||
* historical exception: its bytes are checked as the little-endian constant
|
||||
* 0x1EB52FFD, while v0.2 through v0.7 use the usual 0xFD2FB52N values. */
|
||||
|
||||
Reference in New Issue
Block a user