pub mod carry;
mod decode;
mod encode;
pub mod remap;
pub use decode::{read, read_file_guid};
pub use encode::write;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum FieldType {
Inherited = 0x00,
DbObject = 0x01,
Structure = 0x02,
Pointer = 0x03,
Array = 0x04,
FixedArray = 0x05,
String = 0x06,
CString = 0x07,
Enumeration = 0x08,
FileRef = 0x09,
Boolean = 0x0A,
Int8 = 0x0B,
UInt8 = 0x0C,
Int16 = 0x0D,
UInt16 = 0x0E,
Int32 = 0x0F,
UInt32 = 0x10,
Int64 = 0x11,
UInt64 = 0x12,
Float32 = 0x13,
Float64 = 0x14,
Guid = 0x15,
Sha1 = 0x16,
ResourceRef = 0x17,
Function = 0x18,
TypeRef = 0x19,
BoxedValueRef = 0x1A,
Interface = 0x1B,
Delegate = 0x1C,
Unknown = 0x1F,
}
impl FieldType {
pub fn from_bits(v: u16) -> FieldType {
use FieldType::*;
const ALL: [FieldType; 29] = [
Inherited,
DbObject,
Structure,
Pointer,
Array,
FixedArray,
String,
CString,
Enumeration,
FileRef,
Boolean,
Int8,
UInt8,
Int16,
UInt16,
Int32,
UInt32,
Int64,
UInt64,
Float32,
Float64,
Guid,
Sha1,
ResourceRef,
Function,
TypeRef,
BoxedValueRef,
Interface,
Delegate,
];
ALL.get(usize::from(v)).copied().unwrap_or(Unknown)
}
}
const CATEGORY_ARRAY: u16 = 4;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FieldDescriptor {
pub name: String,
pub name_hash: u32,
pub encoded_type: u16,
pub class_ref: u16,
pub data_offset: u32,
}
impl FieldDescriptor {
pub fn ty(&self) -> FieldType {
FieldType::from_bits((self.encoded_type >> 4) & 0x1F)
}
pub fn is_array(&self) -> bool {
self.encoded_type & 0x0F == CATEGORY_ARRAY
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TypeDescriptor {
pub name: String,
pub name_hash: u32,
pub field_index: i32,
pub field_count: u16,
pub encoded_type: u16,
pub size: u16,
pub alignment: u16,
pub guid: [u8; 16],
pub signature: u32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ImportReference {
pub file_guid: [u8; 16],
pub class_guid: [u8; 16],
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ArrayRecord {
pub offset: u32,
pub count: u32,
pub hash: u32,
pub encoded_type: u16,
pub class_ref: u16,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ReflectionGroup {
pub hash: u32,
pub count: u32,
pub first: u32,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BoxedValueRecord {
pub offset: u32,
pub count: u32,
pub hash: u32,
pub encoded_type: u16,
pub class_ref: u16,
pub raw: Vec<u8>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Instance {
pub fixup_type: u16,
pub descriptor: Option<usize>,
pub data_offset: u32,
pub exported: bool,
pub guid: [u8; 16],
pub object: Option<Object>,
pub raw_image: Vec<u8>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Pointer {
Null,
Internal(usize),
External(usize),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TypeRef {
pub primitive: bool,
pub primitive_type: Option<FieldType>,
pub fixup_type: Option<usize>,
pub encoded: u32,
}
impl TypeRef {
pub fn with_fixup_type(self, slot: usize) -> Option<TypeRef> {
if self.primitive || self.fixup_type.is_none() {
return None;
}
let shifted = u32::try_from(slot).ok().filter(|&s| s < 1 << 29)? << 2;
Some(TypeRef {
fixup_type: Some(slot),
encoded: shifted | (self.encoded & 3),
..self
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BoxedRef {
pub encoded_type: u32,
pub data_offset: i64,
pub relative_offset: i64,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
Nothing,
Bool(bool),
Int(i64),
UInt(u64),
Float(f64),
Str(Vec<u8>),
Guid([u8; 16]),
Sha1([u8; 20]),
Resource(u64),
Pointer(Pointer),
TypeRef(TypeRef),
Boxed(BoxedRef),
Object(Box<Object>),
Array(Vec<Value>, Option<u32>),
}
#[derive(Clone, Debug, PartialEq)]
pub struct FieldValue {
pub descriptor: usize,
pub name: String,
pub value: Value,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Object {
pub descriptor: usize,
pub fields: Vec<FieldValue>,
}
impl Object {
pub fn field(&self, name: &str) -> Option<&Value> {
self.fields
.iter()
.find(|f| f.name.eq_ignore_ascii_case(name))
.map(|f| &f.value)
}
pub fn field_mut(&mut self, name: &str) -> Option<&mut Value> {
self.fields
.iter_mut()
.find(|f| f.name.eq_ignore_ascii_case(name))
.map(|f| &mut f.value)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Document {
pub file_guid: [u8; 16],
pub root_type: String,
pub data_start: usize,
pub data_end: usize,
pub arrays_offset: u32,
pub boxed_values_offset: u32,
pub strings_offset: u32,
pub serialized_layout: bool,
pub hashed_reflection_names: bool,
pub reflection_data: Vec<u8>,
pub fixup_type_guids: Vec<[u8; 16]>,
pub fixup_type_signatures: Vec<u32>,
pub boxed_reference_offsets: Vec<u32>,
pub types: Vec<TypeDescriptor>,
pub fields: Vec<FieldDescriptor>,
pub instances: Vec<Instance>,
pub imports: Vec<ImportReference>,
pub arrays: Vec<ArrayRecord>,
pub boxed_values: Vec<BoxedValueRecord>,
pub pointer_offsets: Vec<u32>,
pub resource_ref_offsets: Vec<u32>,
pub import_offsets: Vec<u32>,
pub type_info_offsets: Vec<u32>,
pub boxed_value_slots: Vec<u32>,
pub payload: Vec<u8>,
pub reflection_groups: Vec<ReflectionGroup>,
}
impl Document {
pub fn fixup_type_target(&self, slot: usize) -> Option<usize> {
let guid = self.fixup_type_guids.get(slot)?;
let signature = self.fixup_type_signatures.get(slot);
self.types
.iter()
.position(|t| t.guid == *guid && signature.is_none_or(|s| t.signature == *s))
}
pub fn type_ref_target(&self, t: &TypeRef) -> Option<usize> {
self.fixup_type_target(t.fixup_type?)
}
pub fn enum_members(&self, field: usize) -> impl Iterator<Item = (&str, i64)> + '_ {
let members = self
.fields
.get(field)
.filter(|f| f.ty() == FieldType::Enumeration)
.and_then(|f| self.types.get(usize::from(f.class_ref)))
.and_then(|t| {
let first = usize::try_from(t.field_index).ok()?;
self.fields
.get(first..first.checked_add(usize::from(t.field_count))?)
})
.unwrap_or_default();
members
.iter()
.map(|m| (m.name.as_str(), i64::from(m.data_offset.cast_signed())))
}
pub fn enum_member(&self, field: usize, value: i64) -> Option<&str> {
self.enum_members(field)
.find(|&(_, v)| v == value)
.map(|(n, _)| n)
}
pub fn array_data_offset(&self, slot: u32) -> Option<u32> {
let at = usize::try_from(slot).ok()?;
let disp = i32::from_le_bytes(*self.payload.get(at..)?.first_chunk::<4>()?);
let target = i64::from(slot).checked_add(i64::from(disp))?;
let target = u32::try_from(target).ok()?;
(usize::try_from(target).ok()? <= self.payload.len()).then_some(target)
}
pub fn type_name(&self, i: usize) -> Option<&str> {
let d = self.instances.get(i)?.descriptor?;
self.types.get(d).map(|t| t.name.as_str())
}
}