use crate::err;
use crate::error::Result;
pub const SIGNATURE: &[u8; 8] = b"XISF0100";
pub const PREAMBLE_LEN: usize = 16;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Form {
Monolithic,
HeaderFile,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Layout {
pub header: (usize, usize),
pub form: Form,
}
impl Layout {
pub fn data_start(&self) -> usize {
self.header.1
}
}
pub fn scan(bytes: &[u8]) -> Result<Layout> {
if !bytes.starts_with(SIGNATURE) && looks_like_xml(bytes) {
return Ok(Layout { header: (0, bytes.len()), form: Form::HeaderFile });
}
if bytes.len() < PREAMBLE_LEN {
return Err(err!(
Truncated,
"a monolithic XISF file needs at least {PREAMBLE_LEN} bytes, this has {}",
bytes.len()
));
}
if &bytes[..8] != SIGNATURE {
return Err(err!(
NotXisf,
"expected the signature {:?}, found {:?}",
String::from_utf8_lossy(SIGNATURE),
String::from_utf8_lossy(&bytes[..8])
));
}
let header_len = u32::from_le_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]) as usize;
if bytes[12..16] != [0, 0, 0, 0] {
return Err(err!(
BadHeader,
"the reserved field must be zero, found {:02x?}",
&bytes[12..16]
));
}
let end = PREAMBLE_LEN.checked_add(header_len).ok_or_else(|| {
err!(Truncated, "the declared header length {header_len} overflows this platform")
})?;
if end > bytes.len() {
return Err(err!(
Truncated,
"the header claims {header_len} bytes, which runs {} past the end of the file",
end - bytes.len()
));
}
Ok(Layout { header: (PREAMBLE_LEN, end), form: Form::Monolithic })
}
pub fn header_str<'a>(bytes: &'a [u8], layout: &Layout) -> Result<&'a str> {
let raw = &bytes[layout.header.0..layout.header.1];
core::str::from_utf8(raw).map_err(|e| err!(BadHeader, "the header is not valid UTF-8: {e}"))
}
fn looks_like_xml(bytes: &[u8]) -> bool {
let bytes = bytes.strip_prefix(&[0xEF, 0xBB, 0xBF]).unwrap_or(bytes);
let start = bytes.iter().position(|b| !b.is_ascii_whitespace()).unwrap_or(bytes.len());
bytes[start..].starts_with(b"<")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ErrorKind;
fn file_with(header: &str) -> Vec<u8> {
let mut out = Vec::new();
out.extend_from_slice(SIGNATURE);
out.extend_from_slice(&(header.len() as u32).to_le_bytes());
out.extend_from_slice(&[0; 4]);
out.extend_from_slice(header.as_bytes());
out
}
#[test]
fn scans_a_well_formed_preamble() {
let bytes = file_with("<xisf/>");
let layout = scan(&bytes).unwrap();
assert_eq!(layout.header, (16, 23));
assert_eq!(layout.form, Form::Monolithic);
assert_eq!(header_str(&bytes, &layout).unwrap(), "<xisf/>");
assert_eq!(layout.data_start(), 23);
}
#[test]
fn rejects_a_file_that_is_not_xisf() {
assert_eq!(scan(b"not an xisf file at all").unwrap_err().kind(), ErrorKind::NotXisf);
}
#[test]
fn rejects_a_header_running_past_the_end() {
let mut bytes = file_with("<xisf/>");
bytes[8] = 0xff;
assert_eq!(scan(&bytes).unwrap_err().kind(), ErrorKind::Truncated);
}
#[test]
fn rejects_a_non_zero_reserved_field() {
let mut bytes = file_with("<xisf/>");
bytes[12] = 1;
assert_eq!(scan(&bytes).unwrap_err().kind(), ErrorKind::BadHeader);
}
#[test]
fn truncation_at_every_length_is_an_error_not_a_panic() {
let bytes = file_with("<xisf version=\"1.0\"/>");
for n in 0..bytes.len() {
let _ = scan(&bytes[..n]);
}
}
}