use serde::Serialize;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum DetectedType {
Pdf,
Html,
Xml,
Dossier,
Zip,
Text,
Binary,
}
impl DetectedType {
pub const fn as_str(self) -> &'static str {
match self {
Self::Pdf => "pdf",
Self::Html => "html",
Self::Xml => "xml",
Self::Dossier => "dossier",
Self::Zip => "zip",
Self::Text => "text",
Self::Binary => "binary",
}
}
pub const fn preferred_extension(self) -> Option<&'static str> {
match self {
Self::Pdf => Some("pdf"),
Self::Html => Some("html"),
Self::Xml => Some("xml"),
Self::Dossier => Some("es3"),
Self::Zip => Some("zip"),
Self::Text => Some("txt"),
Self::Binary => None,
}
}
}
const SNIFF_WINDOW: usize = 4096;
const HTML_WINDOW: usize = 1024;
pub fn sniff(bytes: &[u8]) -> DetectedType {
if bytes.starts_with(b"%PDF") {
return DetectedType::Pdf;
}
if bytes.starts_with(b"PK\x03\x04") {
return DetectedType::Zip;
}
let body = bytes.strip_prefix(&[0xEF, 0xBB, 0xBF]).unwrap_or(bytes);
let markup = trim_ascii_start(body);
if markup.starts_with(b"<")
&& let Some(kind) = sniff_markup(markup)
{
return kind;
}
if is_text(bytes) {
DetectedType::Text
} else {
DetectedType::Binary
}
}
fn sniff_markup(input: &[u8]) -> Option<DetectedType> {
let window = &input[..input.len().min(SNIFF_WINDOW)];
let mut saw_declaration = false;
let mut i = 0usize;
while i < window.len() {
if window[i].is_ascii_whitespace() {
i += 1;
continue;
}
if window[i] != b'<' {
break;
}
let rest = &window[i..];
if rest.starts_with(b"<!--") {
match skip_past(rest, 4, b"-->") {
Some(next) => i += next,
None => break,
}
continue;
}
if rest.starts_with(b"<?") {
if starts_with_ignore_case(&rest[2..], b"xml") {
saw_declaration = true;
}
match skip_past(rest, 2, b"?>") {
Some(next) => i += next,
None => break,
}
continue;
}
if rest.starts_with(b"<!") {
if starts_with_ignore_case(&rest[2..], b"doctype")
&& starts_with_ignore_case(trim_ascii_start(&rest[9..]), b"html")
{
return Some(DetectedType::Html);
}
match skip_past(rest, 2, b">") {
Some(next) => i += next,
None => break,
}
continue;
}
return match element_kind(&rest[1..])? {
RootKind::Html if i < HTML_WINDOW => Some(DetectedType::Html),
RootKind::Dossier => Some(DetectedType::Dossier),
RootKind::Html | RootKind::Other => Some(DetectedType::Xml),
};
}
saw_declaration.then_some(DetectedType::Xml)
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum RootKind {
Html,
Dossier,
Other,
}
fn element_kind(bytes: &[u8]) -> Option<RootKind> {
let first = *bytes.first()?;
if !(first.is_ascii_alphabetic() || first == b'_' || first >= 0x80) {
return None;
}
let end = bytes
.iter()
.position(|byte| {
byte.is_ascii_whitespace() || *byte == b'>' || *byte == b'/' || *byte == b'='
})
.unwrap_or(bytes.len());
let name = &bytes[..end];
let local = name
.iter()
.rposition(|byte| *byte == b':')
.map_or(name, |colon| &name[colon + 1..]);
if [b"html".as_slice(), b"head", b"body", b"meta"]
.iter()
.any(|candidate| local.eq_ignore_ascii_case(candidate))
{
return Some(RootKind::Html);
}
if local == b"Dossier" {
return Some(RootKind::Dossier);
}
Some(RootKind::Other)
}
fn skip_past(bytes: &[u8], from: usize, terminator: &[u8]) -> Option<usize> {
if from >= bytes.len() {
return None;
}
bytes[from..]
.windows(terminator.len())
.position(|window| window == terminator)
.map(|position| from + position + terminator.len())
}
fn starts_with_ignore_case(bytes: &[u8], prefix: &[u8]) -> bool {
bytes.len() >= prefix.len() && bytes[..prefix.len()].eq_ignore_ascii_case(prefix)
}
fn trim_ascii_start(bytes: &[u8]) -> &[u8] {
let start = bytes
.iter()
.position(|byte| !byte.is_ascii_whitespace())
.unwrap_or(bytes.len());
&bytes[start..]
}
fn is_text(bytes: &[u8]) -> bool {
let window = &bytes[..bytes.len().min(SNIFF_WINDOW)];
let text = match std::str::from_utf8(window) {
Ok(text) => text,
Err(error) if error.error_len().is_none() && window.len() == SNIFF_WINDOW => {
std::str::from_utf8(&window[..error.valid_up_to()]).unwrap_or("")
}
Err(_) => return false,
};
text.chars()
.all(|character| !character.is_control() || character.is_whitespace())
}