use std::collections::BTreeMap;
use super::{
ArrayRecord, BoxedRef, BoxedValueRecord, Document, FieldDescriptor, FieldType, FieldValue,
ImportReference, Instance, Object, Pointer, ReflectionGroup, TypeDescriptor, TypeRef, Value,
};
use crate::{Result, bad};
use primase_foundations::bytes::Endian;
const RIFF: u32 = 0x4646_4952; const EBX: u32 = 0x4542_5800;
const EBXS: u32 = 0x4542_5853;
const EBXD: u32 = 0x4542_5844;
const EFIX: u32 = 0x4546_4958;
const EBXX: u32 = 0x4542_5858;
const REFL: u32 = 0x5245_464C;
const RFL2: u32 = 0x5246_4C32;
const MAX_DEPTH: u32 = 128;
pub(crate) fn align_up(v: usize, a: usize) -> usize {
if a <= 1 {
v
} else {
v.div_ceil(a).saturating_mul(a)
}
}
fn data_relative(position: usize, displacement: i64, data_start: usize) -> i64 {
i64::try_from(position)
.unwrap_or(i64::MAX)
.saturating_add(displacement)
.saturating_sub(i64::try_from(data_start).unwrap_or(0))
}
struct Cursor<'a>(primase_foundations::bytes::Cursor<'a>);
impl<'a> std::ops::Deref for Cursor<'a> {
type Target = primase_foundations::bytes::Cursor<'a>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for Cursor<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<'a> Cursor<'a> {
fn new(d: &'a [u8]) -> Self {
Cursor(primase_foundations::bytes::Cursor::new(d))
}
fn nonnegative(&mut self, what: &'static str) -> Result<usize> {
let at = self.position();
let v = self.i32(Endian::Little)?;
usize::try_from(v).map_err(|_| bad(what, at, "count cannot be negative"))
}
fn ensure(&self, end: usize, count: usize, stride: usize, what: &'static str) -> Result<()> {
let room = end
.checked_sub(self.position())
.and_then(|room| room.checked_div(stride));
if room.is_none_or(|room| count > room) {
return Err(bad(
what,
self.position(),
format!("{count} records exceed their chunk"),
));
}
Ok(())
}
fn c_string(&mut self) -> Result<Vec<u8>> {
let rest = self.bytes().get(self.position()..).unwrap_or_default();
let mut parts = rest.splitn(2, |&b| b == 0);
let (Some(text), Some(_)) = (parts.next(), parts.next()) else {
return Err(bad(
"string",
self.position(),
"unterminated, expected a zero byte",
));
};
let s = text.to_vec();
self.take(s.len().saturating_add(1))?;
Ok(s)
}
}
#[derive(Clone, Copy)]
struct Chunk {
id: u32,
begin: usize,
end: usize,
}
fn read_chunks(r: &mut Cursor) -> Result<Vec<Chunk>> {
if r.u32(Endian::Little)? != RIFF {
return Err(bad("RIFF", 0, "not a RIFF document"));
}
let riff_end = (r.u32(Endian::Little)? as usize).saturating_add(8);
if riff_end != r.bytes().len() {
return Err(bad(
"RIFF",
4,
format!("size {riff_end} does not match the stream ({})", r.bytes().len()),
));
}
let form = r.u32(Endian::Big)?;
if form != EBX && form != EBXS {
return Err(bad("RIFF", 8, format!("form {form:#x} is not EBX or EBXS")));
}
let mut chunks = Vec::new();
while r.position() < riff_end {
if r.bytes().len().saturating_sub(r.position()) < 8 {
return Err(bad("RIFF", r.position(), "truncated chunk header"));
}
let id = r.u32(Endian::Big)?;
let size = r.u32(Endian::Little)? as usize;
let begin = r.position();
if riff_end.checked_sub(begin).is_none_or(|room| size > room) {
return Err(bad("RIFF", begin, "chunk exceeds its document"));
}
let end = begin.saturating_add(size);
chunks.push(Chunk { id, begin, end });
let next = end.saturating_add(size & 1);
if next > riff_end {
return Err(bad("RIFF", end, "chunk padding is truncated"));
}
r.seek(next)?;
}
Ok(chunks)
}
fn one_chunk(chunks: &[Chunk], id: u32, name: &'static str, required: bool) -> Result<Option<Chunk>> {
let mut found = None;
for c in chunks.iter().filter(|c| c.id == id) {
if found.is_some() {
return Err(bad(name, c.begin, "duplicate chunk"));
}
found = Some(*c);
}
if required && found.is_none() {
return Err(bad(name, 0, "missing chunk"));
}
Ok(found)
}
fn validate_data_offset(doc: &Document, offset: u32, required: usize, what: &'static str) -> Result<()> {
let size = doc.data_end.saturating_sub(doc.data_start);
let o = offset as usize;
if o > size || required > size.saturating_sub(o) {
return Err(bad(
what,
doc.data_start.saturating_add(o),
format!("offset exceeds EBXD: expected {required} bytes at most {size} bytes into the data"),
));
}
Ok(())
}
fn read_fixup(r: &mut Cursor, c: Chunk, doc: &mut Document) -> Result<()> {
r.seek(c.begin)?;
doc.file_guid = r.array()?;
let types = r.nonnegative("EFIX type")?;
r.ensure(c.end, types, 16, "EFIX type")?;
for _ in 0..types {
doc.fixup_type_guids.push(r.array()?);
}
let sigs = r.nonnegative("EFIX signature")?;
if sigs > types {
return Err(bad("EFIX signature", r.position(), "more signatures than types"));
}
r.ensure(c.end, sigs, 4, "EFIX signature")?;
for _ in 0..sigs {
doc.fixup_type_signatures.push(r.u32(Endian::Little)?);
}
let exported = r.nonnegative("EFIX exported instance")?;
let instances = r.nonnegative("EFIX instance")?;
if exported > instances {
return Err(bad(
"EFIX instance",
r.position(),
"exported count exceeds the instances",
));
}
r.ensure(c.end, instances, 4, "EFIX instance")?;
for i in 0..instances {
let offset = r.u32(Endian::Little)?;
validate_data_offset(doc, offset, 2, "EFIX instance")?;
let at = doc.data_start.saturating_add(offset as usize);
let class_ref = r
.bytes()
.get(at..)
.and_then(|b| b.first_chunk::<2>())
.map(|b| u16::from_le_bytes(*b))
.ok_or_else(|| {
bad(
"EFIX instance",
at,
"instance type slot past the end: expected 2 bytes",
)
})?;
if usize::from(class_ref) >= doc.fixup_type_guids.len() {
return Err(bad(
"EFIX instance",
at,
format!("type slot {class_ref} is invalid"),
));
}
doc.instances.push(Instance {
fixup_type: class_ref,
descriptor: None,
data_offset: offset,
exported: i < exported,
guid: [0; 16],
object: None,
raw_image: Vec::new(),
});
}
let offsets = |r: &mut Cursor, doc: &Document, what: &'static str, width: usize| -> Result<Vec<u32>> {
let n = r.nonnegative(what)?;
r.ensure(c.end, n, 4, what)?;
let mut out = Vec::with_capacity(n);
for _ in 0..n {
let o = r.u32(Endian::Little)?;
validate_data_offset(doc, o, width, what)?;
out.push(o);
}
Ok(out)
};
doc.pointer_offsets = offsets(r, doc, "EFIX pointer", 8)?;
doc.resource_ref_offsets = offsets(r, doc, "EFIX resource reference", 8)?;
let imports = r.nonnegative("EFIX import")?;
r.ensure(c.end, imports, 32, "EFIX import")?;
for _ in 0..imports {
doc.imports.push(ImportReference {
file_guid: r.array()?,
class_guid: r.array()?,
});
}
doc.import_offsets = offsets(r, doc, "EFIX import relocation", 8)?;
doc.type_info_offsets = offsets(r, doc, "EFIX type-info reference", 8)?;
if c.end.saturating_sub(r.position()) < 12 {
return Err(bad("EFIX", r.position(), "truncated section offsets"));
}
doc.arrays_offset = r.u32(Endian::Little)?;
doc.boxed_values_offset = r.u32(Endian::Little)?;
doc.strings_offset = r.u32(Endian::Little)?;
validate_data_offset(doc, doc.arrays_offset, 0, "EFIX array section")?;
validate_data_offset(doc, doc.boxed_values_offset, 0, "EFIX boxed-value section")?;
validate_data_offset(doc, doc.strings_offset, 0, "EFIX string section")?;
if r.position() < c.end {
doc.boxed_reference_offsets = offsets(r, doc, "EFIX boxed reference", 16)?;
}
if r.position() != c.end {
return Err(bad("EFIX", r.position(), "unexpected bytes at its end"));
}
Ok(())
}
fn read_extra(r: &mut Cursor, c: Chunk, doc: &mut Document) -> Result<()> {
r.seek(c.begin)?;
let arrays = r.nonnegative("EBXX array")?;
let boxed = r.nonnegative("EBXX boxed value")?;
r.ensure(c.end, arrays.saturating_add(boxed), 16, "EBXX")?;
let record = |r: &mut Cursor, what: &'static str| -> Result<(u32, u32, u32, u16, u16)> {
let offset = r.u32(Endian::Little)?;
let at = r.position();
let count = u32::try_from(r.i32(Endian::Little)?).map_err(|_| bad(what, at, "negative count"))?;
let v = (
offset,
count,
r.u32(Endian::Little)?,
r.u16(Endian::Little)?,
r.u16(Endian::Little)?,
);
validate_data_offset(doc, offset, 0, what)?;
Ok(v)
};
let mut array_records = Vec::with_capacity(arrays);
for _ in 0..arrays {
let (offset, count, hash, encoded_type, class_ref) = record(r, "EBXX array")?;
array_records.push(ArrayRecord {
offset,
count,
hash,
encoded_type,
class_ref,
});
}
let mut boxed_records = Vec::with_capacity(boxed);
for _ in 0..boxed {
let (offset, count, hash, encoded_type, class_ref) = record(r, "EBXX boxed value")?;
boxed_records.push(BoxedValueRecord {
offset,
count,
hash,
encoded_type,
class_ref,
raw: Vec::new(),
});
}
if r.position() != c.end {
return Err(bad("EBXX", r.position(), "unexpected bytes at its end"));
}
doc.arrays = array_records;
doc.boxed_values = boxed_records;
Ok(())
}
fn string_at(d: &[u8], offset: usize, end: usize) -> Result<String> {
let s = d.get(offset..end).filter(|s| !s.is_empty()).ok_or_else(|| {
bad(
"reflection string",
offset,
"offset is invalid: expected a string inside the chunk",
)
})?;
let mut parts = s.splitn(2, |&b| b == 0);
let (Some(text), Some(_)) = (parts.next(), parts.next()) else {
return Err(bad(
"reflection string",
offset,
"unterminated, expected a zero byte",
));
};
String::from_utf8(text.to_vec()).map_err(|_| bad("reflection string", offset, "is not UTF-8"))
}
fn read_reflection(
r: &mut Cursor,
c: Chunk,
hashed: bool,
doc: &mut Document,
) -> Result<Vec<([u8; 16], u32)>> {
r.seek(c.begin)?;
let nsig = r.nonnegative("reflection signature")?;
r.ensure(c.end, nsig, 20, "reflection signature")?;
let mut ids = Vec::with_capacity(nsig);
for _ in 0..nsig {
ids.push((r.array::<16>()?, r.u32(Endian::Little)?));
}
let ntypes = r.nonnegative("reflection type")?;
r.ensure(c.end, ntypes, 16, "reflection type")?;
for i in 0..ntypes {
let at = r.position();
let name_hash = r.u32(Endian::Little)?;
let field_index = r.i32(Endian::Little)?;
let field_count = r.u16(Endian::Little)?;
let encoded_type = r.u16(Endian::Little)? >> 1;
let size = r.u16(Endian::Little)?;
let alignment = r.u16(Endian::Little)?;
if alignment > 255 {
return Err(bad("reflection type", at, "alignment is unsupported"));
}
let (guid, signature) = ids.get(i).copied().unwrap_or(([0; 16], 0));
doc.types.push(TypeDescriptor {
name: String::new(),
name_hash,
field_index,
field_count,
encoded_type,
size,
alignment,
guid,
signature,
});
}
let nfields = r.nonnegative("reflection field")?;
r.ensure(c.end, nfields, 12, "reflection field")?;
for _ in 0..nfields {
let name_hash = r.u32(Endian::Little)?;
let data_offset = r.u32(Endian::Little)?;
let encoded_type = r.u16(Endian::Little)? >> 1;
let class_ref = r.u16(Endian::Little)?;
doc.fields.push(FieldDescriptor {
name: String::new(),
name_hash,
encoded_type,
class_ref,
data_offset,
});
}
for t in &doc.types {
let ok = usize::try_from(t.field_index).is_ok_and(|fi| {
doc.fields
.len()
.checked_sub(fi)
.is_some_and(|room| usize::from(t.field_count) <= room)
});
if !ok {
return Err(bad(
"reflection type",
c.begin,
format!("{:#x}: field range is invalid", t.name_hash),
));
}
}
let ngroups = r.nonnegative("reflection group")?;
r.ensure(c.end, ngroups, 12, "reflection group")?;
for _ in 0..ngroups {
doc.reflection_groups.push(ReflectionGroup {
hash: r.u32(Endian::Little)?,
count: r.u32(Endian::Little)?,
first: r.u32(Endian::Little)?,
});
}
let nmap = r.nonnegative("reflection mapping")?;
r.ensure(c.end, nmap, 8, "reflection mapping")?;
r.take(crate::at(0, nmap, 8))?;
if hashed {
let nnames = r.nonnegative("reflection name")?;
r.ensure(c.end, nnames, 8, "reflection name")?;
let mut names = BTreeMap::new();
for _ in 0..nnames {
let h = r.u32(Endian::Little)?;
names.insert(h, r.u32(Endian::Little)? as usize);
}
let strings = r.position();
let lookup = |h: u32| -> Result<String> {
let o = names.get(&h).ok_or_else(|| {
bad(
"RFL2 name",
c.begin,
format!("{h:#x} is missing from {} names", names.len()),
)
})?;
string_at(r.bytes(), strings.saturating_add(*o), c.end)
};
for t in &mut doc.types {
t.name = lookup(t.name_hash)?;
}
for f in &mut doc.fields {
f.name = lookup(f.name_hash)?;
}
} else {
let strings = r.position();
for t in &mut doc.types {
t.name = string_at(r.bytes(), strings.saturating_add(t.name_hash as usize), c.end)?;
}
for f in &mut doc.fields {
f.name = string_at(r.bytes(), strings.saturating_add(f.name_hash as usize), c.end)?;
}
}
Ok(ids)
}
struct Values<'a> {
d: &'a [u8],
r: Cursor<'a>,
doc: &'a Document,
array_hashes: BTreeMap<i64, u32>,
instance_at: BTreeMap<i64, usize>,
depth: u32,
boxed_slots: Vec<u32>,
}
impl<'a> Values<'a> {
fn object(&mut self, descriptor: usize, start: usize) -> Result<Object> {
self.nest(start)?;
let t = self
.doc
.types
.get(descriptor)
.ok_or_else(|| bad("EBX object", start, "type reference is invalid"))?;
if start < self.doc.data_start || usize::from(t.size) > self.doc.data_end.saturating_sub(start) {
return Err(bad("EBX object", start, "exceeds EBXD"));
}
let mut o = Object {
descriptor,
fields: Vec::new(),
};
self.fields(&mut o, descriptor, start)?;
self.depth = self.depth.saturating_sub(1);
Ok(o)
}
fn nest(&mut self, at: usize) -> Result<()> {
if self.depth >= MAX_DEPTH {
return Err(bad("EBX type", at, "types nest too deeply"));
}
self.depth = self.depth.saturating_add(1);
Ok(())
}
fn fields(&mut self, out: &mut Object, type_index: usize, start: usize) -> Result<()> {
let t = self
.doc
.types
.get(type_index)
.ok_or_else(|| bad("EBX type", start, "type reference is invalid"))?;
let first = usize::try_from(t.field_index).unwrap_or(0);
for k in 0..usize::from(t.field_count) {
let di = first.saturating_add(k);
let f = self.doc.fields.get(di).ok_or_else(|| {
bad(
"EBX field",
start,
format!("field {di} of {} is invalid", self.doc.fields.len()),
)
})?;
if f.ty() == FieldType::Inherited {
let base = usize::from(f.class_ref);
if base >= self.doc.types.len() {
return Err(bad("EBX base type", start, "is invalid"));
}
self.nest(start)?;
self.fields(out, base, start)?;
self.depth = self.depth.saturating_sub(1);
continue;
}
let position = start.saturating_add(f.data_offset as usize);
if position > self.doc.data_end {
return Err(bad("EBX field", position, "offset exceeds EBXD"));
}
let mut value = self.field(f, position)?;
if f.is_array()
&& let Value::Array(items, hash) = &mut value
&& !items.is_empty()
{
let disp = self
.d
.get(position..)
.and_then(|b| b.first_chunk::<4>())
.map(|b| i32::from_le_bytes(*b))
.ok_or_else(|| {
bad(
"EBX array",
position,
"array displacement past the end: expected 4 bytes",
)
})?;
let target = data_relative(position, i64::from(disp), self.doc.data_start);
*hash = self.array_hashes.get(&target).copied();
}
out.fields.push(FieldValue {
descriptor: di,
name: f.name.clone(),
value,
});
}
Ok(())
}
fn field(&mut self, f: &FieldDescriptor, position: usize) -> Result<Value> {
if f.is_array() {
self.r.seek(position)?;
let disp = self.r.i32(Endian::Little)?;
let target = data_relative(position, i64::from(disp), 0);
let low = i64::try_from(self.doc.data_start.saturating_add(4)).unwrap_or(i64::MAX);
let high = i64::try_from(self.doc.data_end).unwrap_or(i64::MAX);
let target = match usize::try_from(target) {
Ok(t) if (low..=high).contains(&target) => t,
_ => return Err(bad("EBX array", position, "displacement exceeds EBXD")),
};
let count_at = target.saturating_sub(4);
self.r.seek(count_at)?;
let count = usize::try_from(self.r.i32(Endian::Little)?)
.map_err(|_| bad("EBX array", count_at, "negative count"))?;
if count > self.doc.data_end.saturating_sub(target) {
return Err(bad("EBX array", target, format!("{count} elements exceed EBXD")));
}
self.r.seek(target)?;
let mut items = Vec::with_capacity(count);
for _ in 0..count {
let element = self.r.position();
items.push(self.value(f.ty(), f.class_ref, element)?);
match f.ty() {
FieldType::Structure => {
let t = self
.doc
.types
.get(usize::from(f.class_ref))
.ok_or_else(|| bad("EBX array", element, "struct type is invalid"))?;
self.r.seek(element.saturating_add(usize::from(t.size)))?;
}
FieldType::Pointer | FieldType::CString => {
let p = align_up(self.r.position(), 8);
self.r.seek(p)?;
}
_ => {}
}
}
return Ok(Value::Array(items, None));
}
self.r.seek(position)?;
self.value(f.ty(), f.class_ref, position)
}
fn value(&mut self, ty: FieldType, class_ref: u16, position: usize) -> Result<Value> {
let r = &mut self.r;
Ok(match ty {
FieldType::Boolean => Value::Bool(r.u8()? != 0),
FieldType::Int8 => Value::Int(i64::from(r.u8()?.cast_signed())),
FieldType::UInt8 => Value::UInt(u64::from(r.u8()?)),
FieldType::Int16 => Value::Int(i64::from(r.u16(Endian::Little)?.cast_signed())),
FieldType::UInt16 => Value::UInt(u64::from(r.u16(Endian::Little)?)),
FieldType::Int32 | FieldType::Enumeration => Value::Int(i64::from(r.i32(Endian::Little)?)),
FieldType::UInt32 => Value::UInt(u64::from(r.u32(Endian::Little)?)),
FieldType::Int64 => Value::Int(r.i64(Endian::Little)?),
FieldType::UInt64 => Value::UInt(r.u64(Endian::Little)?),
FieldType::Float32 => Value::Float(f64::from(f32::from_le_bytes(r.array()?))),
FieldType::Float64 => Value::Float(f64::from_le_bytes(r.array()?)),
FieldType::Guid => Value::Guid(r.array()?),
FieldType::Sha1 => Value::Sha1(r.array()?),
FieldType::ResourceRef => Value::Resource(r.u64(Endian::Little)?),
FieldType::String => {
let raw = r.take(32)?;
Value::Str(raw.split(|&b| b == 0).next().unwrap_or_default().to_vec())
}
FieldType::CString => {
let disp = r.u32(Endian::Little)?;
Value::Str(self.relative_string(position, disp)?)
}
FieldType::FileRef => {
let disp = r.u32(Endian::Little)?;
r.take(4)?;
Value::Str(self.relative_string(position, disp)?)
}
FieldType::Pointer => {
let encoded = r.i32(Endian::Little)?;
if encoded == 0 {
Value::Pointer(Pointer::Null)
} else if encoded & 1 != 0 {
let index = usize::try_from(encoded >> 1)
.ok()
.filter(|&i| i < self.doc.imports.len())
.ok_or_else(|| bad("EBX pointer", position, "import is invalid"))?;
Value::Pointer(Pointer::External(index))
} else {
let target = data_relative(position, i64::from(encoded), self.doc.data_start);
let i = *self
.instance_at
.get(&target)
.ok_or_else(|| bad("EBX pointer", position, "internal target is invalid"))?;
Value::Pointer(Pointer::Internal(i))
}
}
FieldType::TypeRef | FieldType::Delegate => {
let encoded = r.u32(Endian::Little)?;
r.take(4)?;
if encoded == 0 {
Value::TypeRef(TypeRef {
primitive: false,
primitive_type: None,
descriptor: None,
encoded: 0,
})
} else if encoded & 0x8000_0000 != 0 {
let bits = ((encoded & !0x8000_0000) >> 5) & 0x1F;
Value::TypeRef(TypeRef {
primitive: true,
primitive_type: Some(FieldType::from_bits(u16::try_from(bits).unwrap_or_default())),
descriptor: None,
encoded,
})
} else {
Value::TypeRef(TypeRef {
primitive: false,
primitive_type: None,
descriptor: Some((encoded >> 2) as usize),
encoded,
})
}
}
FieldType::BoxedValueRef => {
let encoded_type = r.u32(Endian::Little)?;
r.take(4)?;
let disp = r.i64(Endian::Little)?;
let target = data_relative(position.saturating_add(8), disp, self.doc.data_start);
let slot = u32::try_from(position.saturating_sub(self.doc.data_start))
.map_err(|_| bad("EBX boxed value", position, "offset does not fit 32 bits"))?;
self.boxed_slots.push(slot);
Value::Boxed(BoxedRef {
encoded_type,
data_offset: target,
relative_offset: disp,
})
}
FieldType::Structure => Value::Object(Box::new(self.object(usize::from(class_ref), position)?)),
_ => Value::Nothing,
})
}
fn relative_string(&mut self, base: usize, disp: u32) -> Result<Vec<u8>> {
if disp == u32::MAX {
return Ok(Vec::new());
}
let target = base.saturating_add(disp as usize);
if !(self.doc.data_start..self.doc.data_end).contains(&target) {
return Err(bad("EBX string", base, "displacement exceeds EBXD"));
}
let saved = self.r.position();
self.r.seek(target)?;
let s = self.r.c_string()?;
if self.r.position() > self.doc.data_end {
return Err(bad("EBX string", target, "exceeds EBXD"));
}
self.r.seek(saved)?;
Ok(s)
}
}
pub fn read(bytes: &[u8]) -> Result<Document> {
let mut r = Cursor::new(bytes);
let chunks = read_chunks(&mut r)?;
let data = one_chunk(&chunks, EBXD, "EBXD", true)?.ok_or_else(|| bad("EBXD", 0, "missing"))?;
let fixup = one_chunk(&chunks, EFIX, "EFIX", true)?.ok_or_else(|| bad("EFIX", 0, "missing"))?;
let extra = one_chunk(&chunks, EBXX, "EBXX", true)?.ok_or_else(|| bad("EBXX", 0, "missing"))?;
let refl = one_chunk(&chunks, REFL, "REFL", false)?;
let rfl2 = one_chunk(&chunks, RFL2, "RFL2", false)?;
let (reflection, hashed) = match (refl, rfl2) {
(Some(c), None) => (c, false),
(None, Some(c)) => (c, true),
_ => return Err(bad("RIFF", 0, "must hold exactly one REFL or RFL2 chunk")),
};
let data_start = align_up(data.begin, 16);
if data_start > data.end {
return Err(bad("EBXD", data.begin, "alignment exceeds the chunk"));
}
r.seek(8)?;
let serialized_layout = r.u32(Endian::Big)? == EBXS;
let mut doc = Document {
file_guid: [0; 16],
root_type: String::new(),
data_start,
data_end: data.end,
arrays_offset: 0,
boxed_values_offset: 0,
strings_offset: 0,
serialized_layout,
hashed_reflection_names: hashed,
reflection_data: bytes
.get(reflection.begin..reflection.end)
.ok_or_else(|| bad("RIFF", reflection.begin, "reflection chunk past the end"))?
.to_vec(),
fixup_type_guids: Vec::new(),
fixup_type_signatures: Vec::new(),
boxed_reference_offsets: Vec::new(),
types: Vec::new(),
fields: Vec::new(),
instances: Vec::new(),
imports: Vec::new(),
arrays: Vec::new(),
boxed_values: Vec::new(),
pointer_offsets: Vec::new(),
resource_ref_offsets: Vec::new(),
import_offsets: Vec::new(),
type_info_offsets: Vec::new(),
boxed_value_slots: Vec::new(),
payload: bytes
.get(data_start..data.end)
.ok_or_else(|| bad("EBXD", data_start, "data chunk past the end"))?
.to_vec(),
reflection_groups: Vec::new(),
};
read_fixup(&mut r, fixup, &mut doc)?;
read_extra(&mut r, extra, &mut doc)?;
let ids = read_reflection(&mut r, reflection, hashed, &mut doc)?;
for inst in &mut doc.instances {
let slot = usize::from(inst.fixup_type);
let (Some(guid), Some(signature)) = (
doc.fixup_type_guids.get(slot),
doc.fixup_type_signatures.get(slot),
) else {
continue;
};
inst.descriptor = ids.iter().position(|&(g, s)| g == *guid && s == *signature);
}
if let Some(d) = doc.instances.first().and_then(|i| i.descriptor)
&& let Some(t) = doc.types.get(d)
{
doc.root_type = t.name.clone();
}
let mut instances = std::mem::take(&mut doc.instances);
{
let mut array_hashes = BTreeMap::new();
for a in &doc.arrays {
array_hashes.entry(i64::from(a.offset)).or_insert(a.hash);
}
let mut instance_at = BTreeMap::new();
for (i, inst) in instances.iter().enumerate() {
instance_at.entry(i64::from(inst.data_offset)).or_insert(i);
}
let mut v = Values {
d: bytes,
r: Cursor::new(bytes),
doc: &doc,
array_hashes,
instance_at,
depth: 0,
boxed_slots: Vec::new(),
};
for inst in &mut instances {
let Some(d) = inst.descriptor else { continue };
let Some(t) = doc.types.get(d) else { continue };
let start = doc.data_start.saturating_add(inst.data_offset as usize);
if inst.exported {
let guid = start
.checked_sub(16)
.filter(|&at| at >= doc.data_start)
.and_then(|at| bytes.get(at..)?.first_chunk::<16>())
.ok_or_else(|| bad("EBX instance", start, "exported instance has no GUID"))?;
inst.guid = *guid;
}
let size = usize::from(t.size);
if size > doc.data_end.saturating_sub(start) {
return Err(bad("EBX instance", start, "exceeds EBXD"));
}
inst.raw_image = bytes
.get(start..start.saturating_add(size))
.ok_or_else(|| bad("EBX instance", start, "exceeds the document"))?
.to_vec();
inst.object = Some(v.object(d, start)?);
}
let slots = std::mem::take(&mut v.boxed_slots);
doc.boxed_value_slots = slots;
}
doc.instances = instances;
doc.boxed_values.sort_by_key(|b| b.offset);
let mut boxed = std::mem::take(&mut doc.boxed_values);
let ends: Vec<u32> = (boxed.iter().skip(1).map(|b| b.offset))
.chain(std::iter::once(doc.strings_offset))
.collect();
for (b, &end) in boxed.iter_mut().zip(&ends) {
let start = b.offset;
let Some(len) = end.checked_sub(start) else {
return Err(bad("EBXX boxed value", start as usize, "records are not ordered"));
};
validate_data_offset(&doc, start, len as usize, "EBXX boxed value bytes")?;
b.raw = doc
.payload
.get(start as usize..end as usize)
.ok_or_else(|| bad("EBXX boxed value", start as usize, "bytes exceed EBXD"))?
.to_vec();
}
doc.boxed_values = boxed;
Ok(doc)
}
pub fn read_file_guid(bytes: &[u8]) -> Result<[u8; 16]> {
let mut r = Cursor::new(bytes);
let chunks = read_chunks(&mut r)?;
let c = one_chunk(&chunks, EFIX, "EFIX", true)?.ok_or_else(|| bad("EFIX", 0, "missing"))?;
r.seek(c.begin)?;
Ok(r.array()?)
}