pub mod hash;
pub mod pek;
pub mod sam;
pub mod uac;
use std::io::Read;
use std::path::Path;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct NtdsFile {
format_version: u32,
page_size: u32,
file_type: u32,
}
impl NtdsFile {
pub fn open(path: impl AsRef<Path>) -> Result<Self, Error> {
let mut f = std::fs::File::open(path).map_err(Error::Io)?;
let mut buf = [0u8; 668];
f.read_exact(&mut buf).map_err(Error::Io)?;
Self::parse_header(&buf)
}
pub fn parse_header(hdr: &[u8]) -> Result<Self, Error> {
if hdr.len() < 668 {
return Err(Error::Format("header too short (< 668 bytes)".into()));
}
let magic = u32::from_le_bytes([hdr[4], hdr[5], hdr[6], hdr[7]]);
if magic != 0x89AB_CDEF {
return Err(Error::Format(format!(
"bad ESE magic: got {magic:#010x}, want 0x89abcdef"
)));
}
let format_version = u32::from_le_bytes([hdr[8], hdr[9], hdr[10], hdr[11]]);
let file_type = u32::from_le_bytes([hdr[12], hdr[13], hdr[14], hdr[15]]);
let page_size = u32::from_le_bytes([hdr[236], hdr[237], hdr[238], hdr[239]]);
if ![2048, 4096, 8192, 16384, 32768].contains(&page_size) {
return Err(Error::Format(format!("odd page size {page_size}")));
}
Ok(NtdsFile {
format_version,
page_size,
file_type,
})
}
pub fn format_version(&self) -> u32 {
self.format_version
}
pub fn page_size(&self) -> u32 {
self.page_size
}
pub fn file_type(&self) -> u32 {
self.file_type
}
}
pub trait EseBackend {
type Row;
type Error;
fn iter_table(
&self,
table: &str,
) -> Result<Box<dyn Iterator<Item = Self::Row> + '_>, Self::Error>;
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Io(std::io::Error),
Format(String),
Crypto(String),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Io(e) => write!(f, "io: {e}"),
Error::Format(s) => write!(f, "format: {s}"),
Error::Crypto(s) => write!(f, "crypto: {s}"),
}
}
}
impl std::error::Error for Error {}
#[cfg(test)]
mod tests {
use super::*;
fn synthetic_header(page_size: u32) -> Vec<u8> {
let mut hdr = vec![0u8; 668];
hdr[4..8].copy_from_slice(&0x89AB_CDEFu32.to_le_bytes());
hdr[8..12].copy_from_slice(&620u32.to_le_bytes());
hdr[12..16].copy_from_slice(&0u32.to_le_bytes());
hdr[236..240].copy_from_slice(&page_size.to_le_bytes());
hdr
}
#[test]
fn accepts_valid_synthetic_header() {
let hdr = synthetic_header(8192);
let f = NtdsFile::parse_header(&hdr).unwrap();
assert_eq!(f.page_size(), 8192);
assert_eq!(f.format_version(), 620);
assert_eq!(f.file_type(), 0);
}
#[test]
fn rejects_wrong_magic() {
let mut hdr = synthetic_header(8192);
hdr[4..8].copy_from_slice(&0xdeadbeefu32.to_le_bytes());
let err = NtdsFile::parse_header(&hdr).unwrap_err();
assert!(matches!(err, Error::Format(_)));
}
#[test]
fn rejects_short_header() {
let hdr = vec![0u8; 100];
assert!(matches!(
NtdsFile::parse_header(&hdr).unwrap_err(),
Error::Format(_)
));
}
#[test]
fn rejects_bad_page_size() {
let hdr = synthetic_header(1234);
assert!(matches!(
NtdsFile::parse_header(&hdr).unwrap_err(),
Error::Format(_)
));
}
}