Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ matcher_map!(
"dwg",
matchers::image::is_dwg
),
(
MatcherType::Image,
"image/svg+xml",
"svg",
matchers::image::is_svg
),
// Video
(
MatcherType::Video,
Expand Down
19 changes: 19 additions & 0 deletions src/matchers/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,25 @@ pub fn is_djvu(buf: &[u8]) -> bool {
&& buf[14] == 0x56
}

/// Returns whether a buffer is SVG image data.
#[must_use]
pub fn is_svg(buf: &[u8]) -> bool {
if buf.starts_with(b"<svg") {
return true;
}

// Avoid conflicts with other XML types while detecting SVGs.
if buf.starts_with(b"<?xml") {
return buf
.get(..256)
.unwrap_or(buf)
.windows(4)
.any(|w| w == b"<svg");
}

false
}

/// Returns whether a buffer is an AutoCAD Drawing (DWG).
#[must_use]
pub fn is_dwg(buf: &[u8]) -> bool {
Expand Down
12 changes: 10 additions & 2 deletions src/matchers/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn is_xml(buf: &[u8]) -> bool {
let val: &[u8] = b"<?xml";
let buf = trim_start_whitespaces(buf);
let buf = trim_start_byte_order_marks(buf);
starts_with_ignore_ascii_case(buf, val)
starts_with_ignore_ascii_case(buf, val) && !super::image::is_svg(buf) // try avoid detecting XML-declared SVGs
}

/// Strip whitespaces at the beginning of the buffer.
Expand Down Expand Up @@ -88,7 +88,7 @@ pub fn is_shellscript(buf: &[u8]) -> bool {

#[cfg(test)]
mod tests {
use super::{is_html, is_shellscript, trim_start_whitespaces};
use super::{is_html, is_shellscript, is_xml, trim_start_whitespaces};

#[test]
fn trim_whitespaces() {
Expand All @@ -114,4 +114,12 @@ mod tests {
fn shellscript() {
assert!(!is_shellscript(b"#!"));
}

#[test]
fn xml_excludes_svg() {
assert!(!is_xml(
b"<?xml version=\"1.0\"?>\n<svg xmlns=\"http://www.w3.org/2000/svg\">"
));
assert!(is_xml(b"<?xml version=\"1.0\"?>\n<note/>"));
}
}
3 changes: 3 additions & 0 deletions testdata/sample.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions testdata/sample2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions tests/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ test_format!(Image, "image/vnd.djvu", "djvu", djvu1, "sample_single.djvu");
test_format!(Image, "image/vnd.djvu", "djvu", djvu2, "sample_multi.djvu");

test_format!(Image, "image/vnd.dwg", "dwg", dwg, "sample.dwg");

test_format!(Image, "image/svg+xml", "svg", svg, "sample.svg");

test_format!(Image, "image/svg+xml", "svg", svg2, "sample2.svg");
Loading