vyre 0.4.0

GPU compute intermediate representation with a standard operation library
Documentation
//! Catalog entry for `file_magic_detect`.

use crate::ops::security_detection::detector_support::{magic, DetectionError, MagicFinding};

/// Embedded operation spec formerly stored in metadata/spec.toml.
pub const SPEC_TOML: &str = r#"schema_version = 1
id = "security_detection.file_magic_detect"
archetype = "match-bytes-pattern"
display_name = "File Magic Detect"
summary = "Returns magic-id and confidence pairs for known file signatures."
category = "C"

[intrinsic]
wgsl = "security_detection_file_magic_detect"

[signature]
inputs = ["Bytes", "Bytes"]
output = "Bytes"

laws = []
equivalence_classes = ["elf", "pe", "archive", "unknown", "t47_cap"]
workgroup_size = [64, 1, 1]
tags = ["security-detection", "file-magic", "t47"]
fixtures_dir = "fixtures/"
"#;

/// Embedded reference vectors formerly stored in fixtures/reference-vectors.toml.
pub const REFERENCE_VECTORS_TOML: &str = r#"[[case]]
name = "positive_elf"
input_hex = "7f454c4602010100"
expected = [{ magic_id = 2, confidence = 100 }]

[[case]]
name = "negative_unknown"
input = "plain text"
expected = []
"#;

/// WGSL lowering source for this detector.
pub mod lowering {
    /// Return the detector-specific WGSL source.
    #[must_use]
    pub const fn source() -> &'static str {
        r#"@compute @workgroup_size(64)
pub fn file_magic_detect() {
    if (starts_with("MZ")) { emit_magic(1u, 80u); }
    if (starts_with("\x7fELF")) { emit_magic(2u, 100u); }
    if (starts_with("\xfe\xed\xfa\xce")) { emit_magic(3u, 100u); }
    if (starts_with("\xfe\xed\xfa\xcf")) { emit_magic(4u, 100u); }
    if (starts_with("\xca\xfe\xba\xbe")) { emit_magic(5u, 100u); }
    if (starts_with("PK\x03\x04")) { emit_magic(6u, 95u); }
    if (starts_with("\x1f\x8b")) { emit_magic(7u, 95u); }
    if (starts_with("\x89PNG\r\n\x1a\n")) { emit_magic(8u, 100u); }
    if (starts_with("%PDF-")) { emit_magic(9u, 100u); }
}"#
    }
}

/// Return known file magic ids and confidence scores.
///
/// # Errors
///
/// Returns `Fix: ...` when input exceeds 64 MiB.
pub fn file_magic_detect(input: &[u8]) -> Result<Vec<MagicFinding>, DetectionError> {
    magic::file_magic_detect(input)
}

/// Compatibility surface for the previous generated implementation module.
pub mod implementation {
    pub use super::file_magic_detect;
    /// Compatibility module for callers that used the generated kernel path.
    pub mod kernel {
        pub use super::super::file_magic_detect;
    }

    /// Compatibility module for callers that used the generated lowering path.
    pub mod lowering {
        /// Compatibility module for callers that used `implementation::lowering::wgsl`.
        pub mod wgsl {
            pub use super::super::super::lowering::source;
        }
    }
}