vyre 0.4.0

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

use crate::ops::security_detection::detector_support::{spans, ByteSpan, DetectionError};

/// Embedded operation spec formerly stored in metadata/spec.toml.
pub const SPEC_TOML: &str = r#"schema_version = 1
id = "security_detection.detect_ipv4"
archetype = "match-bytes-pattern"
display_name = "Detect IPv4"
summary = "Returns offset-length spans for IPv4 addresses with validated octets."
category = "C"

[intrinsic]
wgsl = "security_detection_detect_ipv4"

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

laws = []
equivalence_classes = ["valid_octets", "octet_overflow", "leading_zero", "t47_cap"]
workgroup_size = [64, 1, 1]
tags = ["security-detection", "ipv4", "ioc", "t47"]
fixtures_dir = "fixtures/"
"#;

/// Embedded reference vectors formerly stored in fixtures/reference-vectors.toml.
pub const REFERENCE_VECTORS_TOML: &str = r#"[[case]]
name = "positive_ipv4"
input = "connect 192.168.1.10:443"
expected_spans = [{ offset = 8, len = 12 }]

[[case]]
name = "negative_octet_overflow"
input = "connect 999.168.1.10"
expected_spans = []
"#;

/// 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 detect_ipv4(@builtin(global_invocation_id) gid: vec3<u32>) {
    let i = gid.x;
    var pos = i;
    var parts = 0u;
    loop {
        let parsed = parse_octet(pos);
        if (!parsed.ok || parsed.value > 255u) { return; }
        pos = parsed.next;
        parts = parts + 1u;
        if (parts == 4u) { break; }
        if (pos >= input_len || input[pos] != 46u) { return; }
        pos = pos + 1u;
    }
    if (token_boundary(i, pos)) { emit_span(i, pos - i); }
}"#
    }
}

/// Return IPv4 spans with decimal octets validated in 0..=255.
///
/// # Errors
///
/// Returns `Fix: ...` when input exceeds 64 MiB.
pub fn detect_ipv4(input: &[u8]) -> Result<Vec<ByteSpan>, DetectionError> {
    spans::ipv4_spans(input)
}

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

    /// 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;
        }
    }
}