From a9efd722b49924588448fba5c59f36d3aee3f4c6 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 17:56:38 -0500 Subject: [PATCH] Expose bounded BMP header recognition for consumer cutovers (#1929) * Expose supported BMP header recognition Port the BMP routing check from LightCraft commit 304180a500d1b3f87c8c841f269d305161d6c8b9, preserving minimum length and DIB allowlist. * Test BMP routing boundaries and unsupported DIB sizes --------- Co-authored-by: Brandon Thomas --- crates/image-codecs/src/bmp_probe.rs | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/crates/image-codecs/src/bmp_probe.rs b/crates/image-codecs/src/bmp_probe.rs index 59c78ca1e..5b47721bc 100644 --- a/crates/image-codecs/src/bmp_probe.rs +++ b/crates/image-codecs/src/bmp_probe.rs @@ -1,5 +1,18 @@ //! Header-only BMP dimensions and pixel-data coverage, without allocating a canvas. +/// Recognize a BMP file header with a supported DIB header kind. +/// +/// This is a format-routing check, not full validation: dimensions and pixel data may still +/// be invalid. Unlike the broad signature detector, it requires the minimum 26-byte core +/// header and a known DIB size, preserving callers that reject arbitrary `BM` prefixes. +pub fn is_supported_header(b: &[u8]) -> bool { + if b.len() >= 26 && b.starts_with(b"BM") { + let dib = u32::from_le_bytes([b[14], b[15], b[16], b[17]]); + return matches!(dib, 12 | 16 | 40 | 52 | 56 | 64 | 108 | 124); + } + false +} + /// Coverage of the declared pixel array. Row byte counts include row padding; RLE coverage /// counts the pixels drawn by commands and may be smaller than the canvas. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -142,4 +155,23 @@ mod tests { let nibble = bmp(2, &[0, 3, 0x12, 0x30, 0, 1]); assert_eq!(coverage(&nibble, 4, 2).pixels, 3); } + #[test] + fn supported_header_recognition_preserves_length_signature_and_dib_rules() { + let mut b = [0_u8; 26]; + b[..2].copy_from_slice(b"BM"); + for dib in [12_u32, 16, 40, 52, 56, 64, 108, 124] { + b[14..18].copy_from_slice(&dib.to_le_bytes()); + assert!(is_supported_header(&b), "DIB {dib}"); + for n in 0..26 { + assert!(!is_supported_header(&b[..n]), "prefix {n}"); + } + } + for dib in [0_u32, 13, 39, 41, 125, u32::MAX] { + b[14..18].copy_from_slice(&dib.to_le_bytes()); + assert!(!is_supported_header(&b), "DIB {dib}"); + } + b[14..18].copy_from_slice(&40_u32.to_le_bytes()); + b[0] = b'X'; + assert!(!is_supported_header(&b)); + } }