use crate::db::{self, Node};
use crate::{Result, bad};
const W: &str = "layout.toc";
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InstallChunk {
pub id: Option<[u8; 16]>,
pub name: String,
pub install_bundle: Option<String>,
pub persistent_index: Option<u32>,
pub superbundles: Vec<String>,
pub required_chunks: Vec<[u8; 16]>,
pub always_installed: Option<bool>,
}
impl InstallChunk {
pub fn directory(&self) -> String {
if !self.name.is_empty() {
return self.name.clone();
}
self.install_bundle
.as_deref()
.map(|b| b.strip_prefix("Win32/").unwrap_or(b).to_string())
.unwrap_or_default()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InstallGroup {
pub name: String,
pub chunks: Vec<[u8; 16]>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Layout {
pub envelope: Vec<u8>,
pub root: Node,
pub trailer: Vec<u8>,
pub super_bundles: Vec<String>,
pub fs: Vec<String>,
pub head: Option<u32>,
pub platform: Option<String>,
pub install_chunks: Vec<InstallChunk>,
pub install_groups: Vec<InstallGroup>,
pub excluded_superbundles: Vec<String>,
pub bundle_format: Option<u64>,
}
fn err(detail: impl Into<String>) -> crate::Error {
bad(W, 0, detail)
}
fn kind(n: &Node, ty: u8, key: &str) -> Result<()> {
if n.ty == ty {
Ok(())
} else {
Err(err(format!("{key}: expected type {ty}, found {}", n.ty)))
}
}
fn text(parent: &Node, key: &str) -> Result<Option<String>> {
parent
.field(key)
.map(|n| kind(n, 7, key).map(|()| n.text()))
.transpose()
}
fn guid(n: &Node, key: &str) -> Result<[u8; 16]> {
kind(n, 15, key)?;
n.payload
.as_slice()
.try_into()
.map_err(|_| err(format!("{key}: expected 16 bytes")))
}
fn strings(parent: &Node, key: &str) -> Result<Vec<String>> {
let Some(list) = parent.field(key) else {
return Ok(Vec::new());
};
kind(list, 1, key)?;
list.children
.iter()
.map(|c| kind(c, 7, key).map(|()| c.text()))
.collect()
}
fn guids(parent: &Node, key: &str) -> Result<Vec<[u8; 16]>> {
let Some(list) = parent.field(key) else {
return Ok(Vec::new());
};
kind(list, 1, key)?;
list.children.iter().map(|c| guid(c, key)).collect()
}
pub fn read(data: &[u8]) -> Result<Layout> {
let (envelope, root, trailer) = db::read_file(data)?;
let super_bundles = match root.field("superBundles") {
None => Vec::new(),
Some(list) => {
kind(list, 1, "superBundles")?;
list.children
.iter()
.map(|e| text(e, "name")?.ok_or_else(|| err("superBundles: an entry without a name")))
.collect::<Result<_>>()?
}
};
let head = match root.field("head") {
None => None,
Some(n) => Some(n.u32().ok_or_else(|| err("head: expected a 32-bit value"))?),
};
let manifest = root.field("installManifest");
let mut install_chunks = Vec::new();
let mut install_groups = Vec::new();
let mut excluded_superbundles = Vec::new();
let mut bundle_format = None;
if let Some(m) = manifest {
if let Some(list) = m.field("installChunks") {
kind(list, 1, "installChunks")?;
for e in &list.children {
install_chunks.push(InstallChunk {
id: e.field("id").map(|n| guid(n, "id")).transpose()?,
name: text(e, "name")?.unwrap_or_default(),
install_bundle: text(e, "installBundle")?,
persistent_index: e
.field("persistentIndex")
.map(|n| {
n.u32()
.ok_or_else(|| err("persistentIndex: expected a 32-bit value"))
})
.transpose()?,
superbundles: strings(e, "superbundles")?,
required_chunks: guids(e, "requiredChunks")?,
always_installed: e
.field("alwaysInstalled")
.map(|n| match (n.ty, n.payload.as_slice()) {
(6, &[b]) => Ok(b != 0),
_ => Err(err("alwaysInstalled: expected a boolean")),
})
.transpose()?,
});
}
}
if let Some(list) = m.field("installGroups") {
kind(list, 1, "installGroups")?;
for e in &list.children {
install_groups.push(InstallGroup {
name: text(e, "name")?.unwrap_or_default(),
chunks: guids(e, "chunks")?,
});
}
}
excluded_superbundles = strings(m, "excludedSuperbundles")?;
if let Some(b) = m.field("settings").and_then(|s| s.field("bundleFormat")) {
bundle_format = Some(match (b.ty, b.payload.as_slice()) {
(9, &[a, b, c, d, e, f, g, h]) => u64::from_le_bytes([a, b, c, d, e, f, g, h]),
_ => return Err(err("bundleFormat: expected a 64-bit value")),
});
}
}
Ok(Layout {
envelope,
super_bundles,
fs: strings(&root, "fs")?,
head,
platform: text(&root, "platform")?,
install_chunks,
install_groups,
excluded_superbundles,
bundle_format,
root,
trailer,
})
}
impl Layout {
pub fn write(&self) -> Result<Vec<u8>> {
db::write_file(&self.envelope, &self.root, &self.trailer)
}
}