use crate::toc::Loc;
use crate::{Rd, Result, bad, capacity, latin1, u32_len};
const R: &str = "bundle region";
const M: &str = "bundle manifest";
pub const BUNDLE_SALT: u32 = 0x7065_636E;
pub const BUNDLE_MAGIC: u32 = 0xED1C_EDB8;
pub type FileRef = (Loc, u32, u32);
pub fn read_region(r: &[u8]) -> Result<(Vec<FileRef>, Vec<u8>)> {
let rd = Rd::new(r, R);
let int = |i: usize| rd.i32be(crate::at(0, i, 4));
let (mo, ms, lo, total, dato, ro, co, total2) = (
int(0)?,
int(1)?,
rd.u32be(8)?,
int(3)?,
rd.u32be(16)?,
rd.u32be(20)?,
rd.u32be(24)?,
int(8)?,
);
let (Ok(mo), Ok(ms), Ok(total)) = (usize::try_from(mo), usize::try_from(ms), usize::try_from(total))
else {
return Err(bad(
R,
0,
"header is invalid: expected a metadata offset, metadata size and file count of 0 or more",
));
};
if total2 < 0 || usize::try_from(total2) != Ok(total) || ro != dato || co != dato {
return Err(bad(
R,
0,
"header is invalid: expected the two file counts equal and the three data offsets equal",
));
}
let inline = if ms > 0 {
rd.bytes(mo, ms)?.to_vec()
} else {
Vec::new()
};
let mut files = Vec::with_capacity(capacity(R, total, 9, r.len())?);
let mut pos = dato as usize;
let mut cur: Option<Loc> = None;
for i in 0..total {
let flag = rd.u8(crate::at(lo as usize, i, 1))?;
if flag == 1 {
let enc = rd.u32be(pos)?;
pos = pos.saturating_add(4);
cur = Some(Loc {
patch: enc & 0x00FF_0000 != 0,
chunk: (enc >> 8) & 0xFF,
archive: u16::from(enc.to_le_bytes()[0]),
});
} else if flag != 0 {
let id = rd.u64be(pos)?;
cur = Some(Loc::decode(id).ok_or_else(|| bad(R, pos, crate::toc::unrepresentable(id)))?);
pos = pos.saturating_add(8);
}
let loc = cur.ok_or_else(|| bad(R, pos, "first file has no archive identifier"))?;
files.push((loc, rd.u32be(pos)?, rd.u32be(pos.saturating_add(4))?));
pos = pos.saturating_add(8);
}
Ok((files, inline))
}
pub fn write_region(files: &[FileRef], inline: &[u8]) -> Result<Vec<u8>> {
let mut table = Vec::new();
let mut flags = Vec::with_capacity(files.len());
let mut prev = None;
for &(loc, off, size) in files {
if prev == Some(loc) {
flags.push(0u8);
} else {
flags.push(0x80);
table.extend(loc.encode().to_be_bytes());
prev = Some(loc);
}
table.extend(off.to_be_bytes());
table.extend(size.to_be_bytes());
}
if let Some(f) = flags.first_mut() {
*f |= 0x04;
}
let data_at = 0x24u32;
let too_large = || {
bad(
R,
0,
"the file table and inline manifest do not fit 32-bit offsets",
)
};
let table_end = u32_len(R, table.len())?
.checked_add(data_at)
.ok_or_else(too_large)?;
let inline_len = u32_len(R, inline.len())?;
let meta_at = if inline.is_empty() { 0 } else { table_end };
let loc_at = if inline.is_empty() {
table_end
} else {
table_end.checked_add(inline_len).ok_or_else(too_large)?
};
let n = u32_len(R, files.len())?;
let mut out = Vec::with_capacity(
table
.len()
.saturating_add(inline.len())
.saturating_add(flags.len())
.saturating_add(0x24),
);
for v in [meta_at, inline_len, loc_at, n, data_at, data_at, data_at, 0, n] {
out.extend(v.to_be_bytes());
}
out.extend(table);
out.extend(inline);
out.extend(flags);
Ok(out)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Kind {
Ebx,
Res,
Chunk,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Asset {
pub kind: Kind,
pub name: String,
pub sha1: [u8; 20],
pub size: u32,
pub rtype: u32,
pub rmeta: [u8; 16],
pub rid: u64,
pub guid: [u8; 16],
pub loff: u32,
pub lsize: u32,
}
impl Asset {
pub fn chunk_size(&self) -> u64 {
u64::from(self.loff).saturating_add(u64::from(self.lsize))
}
fn new(kind: Kind) -> Self {
Self {
kind,
name: String::new(),
sha1: [0; 20],
size: 0,
rtype: 0,
rmeta: [0; 16],
rid: 0,
guid: [0; 16],
loff: 0,
lsize: 0,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Manifest {
pub ebx: Vec<Asset>,
pub res: Vec<Asset>,
pub chunks: Vec<Asset>,
pub meta: Vec<u8>,
pub big_endian: bool,
}
impl Manifest {
pub fn count(&self) -> usize {
self.ebx
.len()
.saturating_add(self.res.len())
.saturating_add(self.chunks.len())
}
pub fn res_file(&self, i: usize) -> usize {
self.ebx.len().saturating_add(i).saturating_add(1)
}
pub fn chunk_file(&self, i: usize) -> usize {
self.ebx
.len()
.saturating_add(self.res.len())
.saturating_add(i)
.saturating_add(1)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BundleMetric {
pub resource_type: u32,
pub name: String,
pub value: u32,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ChunkMetaBody {
Empty,
FirstMip(i32),
Container {
container: String,
metrics: Vec<BundleMetric>,
},
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ChunkMeta {
pub h64: u64,
pub meta: ChunkMetaBody,
}
fn meta_err(detail: impl Into<String>) -> crate::Error {
bad(M, 0, detail)
}
fn le_u32(n: &crate::db::Node, what: &str, ty: u8) -> Result<[u8; 4]> {
match (n.ty == ty, n.payload.as_slice()) {
(true, &[a, b, c, d]) => Ok([a, b, c, d]),
_ => Err(meta_err(format!(
"chunkMeta {what}: expected a 4-byte value of type {ty}"
))),
}
}
pub fn read_chunk_meta(meta: &[u8]) -> Result<Vec<ChunkMeta>> {
use crate::db;
if meta.is_empty() {
return Ok(Vec::new());
}
let (root, end) = db::read_node(meta, 0)?;
if end != meta.len() || root.ty != 1 || root.name != "chunkMeta" {
return Err(meta_err(
"chunkMeta: expected one list named chunkMeta that fills the metadata",
));
}
root.children
.iter()
.map(|row| {
let [h64, body] = row.children.as_slice() else {
return Err(meta_err("chunkMeta row: expected the children h64 and meta"));
};
if row.ty != 2 || h64.name != "h64" || body.name != "meta" || body.ty != 2 {
return Err(meta_err("chunkMeta row: expected an object of h64 and meta"));
}
let h64 = match (h64.ty, h64.payload.as_slice()) {
(9, p) => u64::from_le_bytes(
p.try_into()
.map_err(|_| meta_err("chunkMeta h64: expected 8 bytes"))?,
),
_ => return Err(meta_err("chunkMeta h64: expected a value of type 9")),
};
let names: Vec<&str> = body.children.iter().map(|c| c.name.as_str()).collect();
let meta = match names.as_slice() {
[] => ChunkMetaBody::Empty,
["firstMip"] => ChunkMetaBody::FirstMip(i32::from_le_bytes(le_u32(
body.children
.first()
.ok_or_else(|| meta_err("chunkMeta firstMip is missing"))?,
"firstMip",
8,
)?)),
["container", "metrics"] => {
let [container, metrics] = body.children.as_slice() else {
return Err(meta_err("chunkMeta: expected container and metrics"));
};
if container.ty != 7 || metrics.ty != 1 {
return Err(meta_err("chunkMeta: container is a string and metrics a list"));
}
ChunkMetaBody::Container {
container: container.text(),
metrics: metrics.children.iter().map(metric).collect::<Result<_>>()?,
}
}
other => {
return Err(meta_err(format!(
"chunkMeta meta: children {other:?} are no known shape"
)));
}
};
Ok(ChunkMeta { h64, meta })
})
.collect()
}
fn metric(n: &crate::db::Node) -> Result<BundleMetric> {
let [kind, value] = n.children.as_slice() else {
return Err(meta_err("chunkMeta metric: expected resourceType and value"));
};
if n.ty != 2 || kind.name != "resourceType" || value.name != "value" || value.ty != 2 {
return Err(meta_err(
"chunkMeta metric: expected an object of resourceType and value",
));
}
let [number] = value.children.as_slice() else {
return Err(meta_err("chunkMeta metric: expected one number in value"));
};
Ok(BundleMetric {
resource_type: u32::from_le_bytes(le_u32(kind, "resourceType", 8)?),
name: number.name.clone(),
value: u32::from_le_bytes(le_u32(number, "metric value", 8)?),
})
}
fn container(children: Vec<crate::db::Node>) -> crate::db::Node {
crate::db::Node {
ty: 2,
children,
terminated: true,
..crate::db::Node::default()
}
}
fn leaf(name: &str, ty: u8, payload: Vec<u8>) -> crate::db::Node {
crate::db::Node {
ty,
named: true,
name: name.to_string(),
payload,
..crate::db::Node::default()
}
}
pub fn write_chunk_meta(rows: &[ChunkMeta]) -> Result<Vec<u8>> {
use crate::db;
let named = |mut n: db::Node, name: &str| {
n.named = true;
n.name = name.to_string();
n
};
let children = rows
.iter()
.map(|r| {
let body = match &r.meta {
ChunkMetaBody::Empty => Vec::new(),
ChunkMetaBody::FirstMip(m) => vec![leaf("firstMip", 8, m.to_le_bytes().to_vec())],
ChunkMetaBody::Container {
container: c,
metrics,
} => {
let mut text = latin1(c, M)?;
text.push(0);
vec![
leaf("container", 7, text),
named(
db::Node {
ty: 1,
terminated: true,
children: metrics
.iter()
.map(|m| {
container(vec![
leaf("resourceType", 8, m.resource_type.to_le_bytes().to_vec()),
named(
container(vec![leaf(
&m.name,
8,
m.value.to_le_bytes().to_vec(),
)]),
"value",
),
])
})
.collect(),
..db::Node::default()
},
"metrics",
),
]
}
};
Ok(container(vec![
leaf("h64", 9, r.h64.to_le_bytes().to_vec()),
named(container(body), "meta"),
]))
})
.collect::<Result<Vec<_>>>()?;
let root = named(
db::Node {
ty: 1,
terminated: true,
children,
..db::Node::default()
},
"chunkMeta",
);
let mut out = Vec::new();
db::write_node(&root, &mut out)?;
Ok(out)
}
impl Manifest {
pub fn chunk_meta(&self) -> Result<Vec<ChunkMeta>> {
read_chunk_meta(&self.meta)
}
}
pub fn is_manifest(m: &[u8]) -> bool {
let rd = Rd::new(m, M);
let (Ok(declared), Ok(be), Ok(le)) = (rd.u32be(0), rd.u32be(4), rd.u32le(4)) else {
return false;
};
m.len() >= 36
&& usize::try_from(declared).ok().and_then(|d| d.checked_add(4)) == Some(m.len())
&& (be ^ BUNDLE_SALT == BUNDLE_MAGIC || le ^ BUNDLE_SALT == BUNDLE_MAGIC)
}
pub fn read_manifest(m: &[u8]) -> Result<Manifest> {
let rd = Rd::new(m, M);
if m.len() < 36 {
return Err(bad(M, 0, "header is truncated"));
}
let declared = rd.u32be(0)?;
if usize::try_from(declared).ok().and_then(|d| d.checked_add(4)) != Some(m.len()) {
return Err(bad(
M,
0,
format!(
"declared size {declared} does not match: expected {}, the bytes after the size word",
m.len().saturating_sub(4)
),
));
}
let big_endian = rd.u32be(4)? ^ BUNDLE_SALT == BUNDLE_MAGIC;
if !big_endian && rd.u32le(4)? ^ BUNDLE_SALT != BUNDLE_MAGIC {
return Err(bad(M, 4, "magic is unsupported"));
}
let u32e = |at: usize| if big_endian { rd.u32be(at) } else { rd.u32le(at) };
let u64e = |at: usize| if big_endian { rd.u64be(at) } else { rd.u64le(at) };
let size = |at: usize| u32e(at).map(|v| v as usize);
let (total, ne, nr, nc) = (size(8)?, size(12)?, size(16)?, size(20)?);
let (strings, mo, ms) = (size(24)?.saturating_add(4), size(28)?, size(32)?);
if ne.checked_add(nr).and_then(|n| n.checked_add(nc)) != Some(total) {
return Err(bad(
M,
8,
format!(
"asset counts do not add up: {ne} EBX + {nr} resources + {nc} chunks, expected the total {total}"
),
));
}
let mut pos = 36usize;
let mut hashes = Vec::with_capacity(capacity(M, total, 20, m.len().saturating_sub(36))?);
for _ in 0..total {
hashes.push(rd.arr::<20>(pos)?);
pos = pos.saturating_add(20);
}
let mut b = Manifest {
ebx: Vec::new(),
res: Vec::new(),
chunks: Vec::new(),
meta: Vec::new(),
big_endian,
};
for (n, kind) in [(ne, Kind::Ebx), (nr, Kind::Res)] {
for _ in 0..n {
let mut a = Asset::new(kind);
a.name = rd.cstr(strings.saturating_add(size(pos)?))?;
a.size = u32e(pos.saturating_add(4))?;
pos = pos.saturating_add(8);
if kind == Kind::Ebx {
b.ebx.push(a)
} else {
b.res.push(a)
}
}
}
for (a, hash) in b.ebx.iter_mut().chain(b.res.iter_mut()).zip(&hashes) {
a.sha1 = *hash;
}
for a in &mut b.res {
a.rtype = u32e(pos)?;
pos = pos.saturating_add(4);
}
for a in &mut b.res {
a.rmeta = rd.arr(pos)?;
pos = pos.saturating_add(16);
}
for a in &mut b.res {
a.rid = u64e(pos)?;
pos = pos.saturating_add(8);
}
let chunk_hashes = hashes.iter().skip(ne.saturating_add(nr));
for hash in chunk_hashes.take(nc) {
let mut a = Asset::new(Kind::Chunk);
a.guid = rd.arr(pos)?;
if big_endian {
a.guid.reverse();
}
a.loff = u32e(pos.saturating_add(16))?;
a.lsize = u32e(pos.saturating_add(20))?;
pos = pos.saturating_add(24);
a.sha1 = *hash;
a.size = a.lsize;
b.chunks.push(a);
}
if ms > 0 {
b.meta = rd.bytes(mo.saturating_add(4), ms)?.to_vec();
}
Ok(b)
}
pub fn write_manifest(b: &Manifest) -> Result<Vec<u8>> {
if b.big_endian {
return Err(bad(M, 0, "only little-endian manifests are written"));
}
let total = b.count();
let mut strings = Vec::new();
let mut offsets = Vec::with_capacity(b.ebx.len().saturating_add(b.res.len()));
for a in b.ebx.iter().chain(&b.res) {
offsets.push(u32_len(M, strings.len())?);
strings.extend(latin1(&a.name, M)?);
strings.push(0);
}
let assets_with_names = b.ebx.len().saturating_add(b.res.len());
let string_at = 36usize
.saturating_add(crate::at(0, total, 20))
.saturating_add(crate::at(0, assets_with_names, 8))
.saturating_add(crate::at(0, b.res.len(), 28))
.saturating_add(crate::at(0, b.chunks.len(), 24));
let meta_at = string_at.saturating_add(strings.len());
let len = meta_at.saturating_add(b.meta.len());
let after_size = |at: usize| u32_len(M, at.saturating_sub(4));
let mut out = Vec::with_capacity(len);
out.extend(after_size(len)?.to_be_bytes());
out.extend((BUNDLE_MAGIC ^ BUNDLE_SALT).to_le_bytes());
let meta_field = if b.meta.is_empty() {
0
} else {
after_size(meta_at)?
};
for v in [
u32_len(M, total)?,
u32_len(M, b.ebx.len())?,
u32_len(M, b.res.len())?,
u32_len(M, b.chunks.len())?,
after_size(string_at)?,
meta_field,
u32_len(M, b.meta.len())?,
] {
out.extend(v.to_le_bytes());
}
for a in b.ebx.iter().chain(&b.res).chain(&b.chunks) {
out.extend(a.sha1);
}
for (a, off) in b.ebx.iter().chain(&b.res).zip(&offsets) {
out.extend(off.to_le_bytes());
out.extend(a.size.to_le_bytes());
}
for a in &b.res {
out.extend(a.rtype.to_le_bytes());
}
for a in &b.res {
out.extend(a.rmeta);
}
for a in &b.res {
out.extend(a.rid.to_le_bytes());
}
for a in &b.chunks {
out.extend(a.guid);
out.extend(a.loff.to_le_bytes());
out.extend(a.lsize.to_le_bytes());
}
out.extend(strings);
out.extend(&b.meta);
debug_assert_eq!(out.len(), len);
Ok(out)
}