use super::*;
use base64::Engine;
#[derive(Debug, Clone, Default)]
pub struct LogicalMetadata {
pub path: Vec<u8>,
pub parent: Option<String>,
pub created: Option<i128>,
pub modified: Option<i128>,
pub accessed: Option<i128>,
pub changed: Option<i128>,
pub mode: Option<u32>,
}
#[derive(Debug, Clone, Default)]
pub struct CaseMetadata {
pub case_number: String,
pub evidence_number: String,
pub examiner: String,
pub notes: String,
}
#[derive(Debug, Clone, Copy)]
pub enum SubstreamKind {
AlternateDataStream,
ExtendedAttribute,
}
fn literal(value: &str) -> String {
oxrdf::Literal::new_simple_literal(value).to_string()
}
fn normalized(bytes: &[u8]) -> (String, Option<String>) {
if let Ok(value) = std::str::from_utf8(bytes)
&& !value.chars().any(char::is_control)
{
return (value.into(), None);
}
let mut text = String::new();
for byte in bytes {
if *byte < 32 || *byte >= 127 || *byte == b'%' {
text.push_str(&format!("%{byte:02X}"));
} else {
text.push(*byte as char);
}
}
(
text,
Some(base64::engine::general_purpose::STANDARD.encode(bytes)),
)
}
impl LogicalMetadata {
fn properties(&self) -> Result<(String, String)> {
if self.path.is_empty() {
return Err(malformed("empty original path"));
}
let name = {
self.path
.rsplit(|b| *b == b'/' || *b == b'\\')
.next()
.unwrap()
};
let (path, raw_path) = normalized(&self.path);
let (_, raw_name) = normalized(name);
let mut properties = format!("l:originalPathName {};", literal(&path));
for (key, value) in [("originalPathNameRaw", raw_path), ("fileNameRaw", raw_name)] {
if let Some(value) = value {
properties.push_str(&format!(
" l:{key} {}^^<http://www.w3.org/2001/XMLSchema#base64Binary>;",
literal(&value)
));
}
}
for (key, value) in [
("birthTime", self.created),
("lastWritten", self.modified),
("lastAccessed", self.accessed),
("recordChanged", self.changed),
] {
if let Some(value) = value {
let time = time::OffsetDateTime::from_unix_timestamp_nanos(value)
.map_err(|_| malformed("timestamp outside supported calendar"))?
.format(&time::format_description::well_known::Rfc3339)
.map_err(|_| malformed("timestamp cannot be represented as RFC3339"))?;
properties.push_str(&format!(
" a:{key} {}^^<http://www.w3.org/2001/XMLSchema#dateTime>;",
literal(&time)
));
}
}
if let Some(mode) = self.mode {
properties.push_str(&format!(" l:fileMode {mode};"));
}
Ok((path, properties))
}
}
impl Writer {
pub(super) fn logical_parent(&self, parent: Option<&str>) -> Result<()> {
self.healthy()?;
if self.profile != Profile::Logical {
return Err(Error::Unsupported(
"logical metadata in physical profile".into(),
));
}
if parent.is_some_and(|p| !self.folders.contains_key(p)) {
return Err(malformed("parent is not a writer-owned folder"));
}
Ok(())
}
fn register_child(&mut self, parent: Option<&str>, id: &str) {
if let Some(parent) = parent {
self.folders.get_mut(parent).unwrap().1.push(id.into());
} else {
self.roots.push(id.into());
}
}
pub fn add_folder(&mut self, metadata: &LogicalMetadata) -> Result<String> {
self.logical_parent(metadata.parent.as_deref())?;
let (path, properties) = metadata.properties()?;
let id = identifier();
let name = {
metadata
.path
.rsplit(|b| *b == b'/' || *b == b'\\')
.next()
.unwrap()
};
let (name, _) = normalized(name);
let properties = format!(
"a a:Folder, a:FolderImage, a:Image; a:fileName {}; a:originalFileName {}; {properties} a:stored <{}>",
literal(&name),
literal(&path),
self.volume
);
self.register_child(metadata.parent.as_deref(), &id);
self.folders.insert(id.clone(), (properties, Vec::new()));
Ok(id)
}
pub fn add_file_with_metadata(
&mut self,
metadata: &LogicalMetadata,
size: u64,
input: &mut impl Read,
progress: impl FnMut(u64, u64) -> ControlFlow<()>,
) -> Result<String> {
self.logical_parent(metadata.parent.as_deref())?;
let (path, properties) = metadata.properties()?;
let id = self.add_file_impl(&path, size, input, progress, &properties)?;
self.register_child(metadata.parent.as_deref(), &id);
Ok(id)
}
pub fn add_case_metadata(&mut self, metadata: &CaseMetadata) -> Result<String> {
self.healthy()?;
let id = identifier();
self.metadata.push_str(&format!("<{id}> a a:CaseDetails; a:target <{}>; a:caseNumber {}; a:evidenceNumber {}; a:examiner {}; a:notes {} .\n", self.volume, literal(&metadata.case_number), literal(&metadata.evidence_number), literal(&metadata.examiner), literal(&metadata.notes)));
Ok(id)
}
pub fn add_substream(
&mut self,
parent: &str,
name: &[u8],
kind: SubstreamKind,
bytes: &[u8],
) -> Result<String> {
self.healthy()?;
if !self.streams.iter().any(|s| s.id == parent) && !self.folders.contains_key(parent) {
return Err(malformed("substream parent is not writer-owned"));
}
if bytes.len() > 1024 {
return Err(Error::Unsupported(
"inline substreams are limited to 1 KiB; use a file stream for larger attributes"
.into(),
));
}
let id = identifier();
let (name, raw) = normalized(name);
let raw = raw
.map(|s| {
format!(
"l:fileNameRaw {}^^<http://www.w3.org/2001/XMLSchema#base64Binary>;",
literal(&s)
)
})
.unwrap_or_default();
let content = base64::engine::general_purpose::STANDARD.encode(bytes);
self.metadata.push_str(&format!("<{id}> a a:FileSubStream, a:Image; a:target <{parent}>; a:name {}; {raw} a:size {}; l:dataStream {}^^<http://www.w3.org/2001/XMLSchema#base64Binary>; a:hash {}^^a:SHA256 .\n", literal(&name), bytes.len(), literal(&content), literal(&hex(&Sha256::digest(bytes)))));
let property = match kind {
SubstreamKind::AlternateDataStream => "alternateDataStream",
SubstreamKind::ExtendedAttribute => "extendedAttribute",
};
self.metadata
.push_str(&format!("<{parent}> l:{property} <{id}> .\n"));
Ok(id)
}
pub fn set_path_separator(&mut self, separator: char) -> Result<()> {
self.healthy()?;
if !matches!(separator, '/' | '\\') {
return Err(malformed("unsupported path separator"));
}
self.path_separator = separator;
Ok(())
}
pub(super) fn finish_logical_metadata(&mut self) {
for (id, (properties, children)) in &self.folders {
self.metadata.push_str(&format!("<{id}> {properties}"));
for child in children {
self.metadata.push_str(&format!("; a:child <{child}>"));
}
self.metadata.push_str(" .\n");
}
if !self.roots.is_empty() {
self.metadata.push_str(&format!(
"<{}> a a:LogicalAcquisitionTask; l:pathSeparator {}",
identifier(),
literal(&self.path_separator.to_string())
));
for root in &self.roots {
self.metadata
.push_str(&format!("; a:filesystemRoot <{root}>"));
}
self.metadata.push_str(" .\n");
}
}
}