pub mod boost;
mod dict;
pub mod emit;
mod fields;
pub mod python;
mod read;
pub use fields::FieldProgress;
pub(crate) use read::read_with_fields;
pub use read::{read_typetree, read_typetree_with_reader};
mod work;
pub mod write;
pub use dict::DICT_FIRST;
pub(crate) use dict::steps;
pub(crate) use work::{OpenWork, Work, limit_refused};
pub use work::{STEP_WORK, VALUE_WORK, WORK_CHUNK};
#[cfg(test)]
mod dict_tests;
#[cfg(test)]
mod linear_tests;
#[cfg(test)]
mod prepass_tests;
use std::sync::Arc;
use crate::error::{LimitError, LimitKind};
use crate::files::SerializedType;
use crate::limits::{ChargeError, Limits, Meter, MeteredVec};
use crate::provider::{NoTree, ProviderError};
use crate::raw::RawStr;
use crate::stream::{Endian, StreamRefusal};
use crate::typetree::{NodeId, TreeError};
use crate::value::{Built, Parsed, Value};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum FloatMode {
#[default]
UnityPy,
Lossless,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum WriteRules {
#[default]
UnityPy,
MirrorReader,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ReadOptions {
pub floats: FloatMode,
pub limits: Limits,
}
#[derive(Clone, Copy, Debug)]
pub enum RefTypes<'a> {
NoFile,
Absent,
List(&'a [SerializedType]),
}
#[derive(Clone, Copy, Debug)]
pub struct ReadCtx<'a> {
pub endian: Endian,
pub ref_types: RefTypes<'a>,
pub options: &'a ReadOptions,
pub meter: &'a Arc<Meter>,
}
#[derive(Clone, Copy, Debug)]
pub struct WriteCtx<'a> {
pub endian: Endian,
pub ref_types: RefTypes<'a>,
pub rules: WriteRules,
pub limits: &'a Limits,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum L2Kind {
NoTypetree,
CheckRead,
OutOfBounds,
NegativeLength,
RefType,
Encode,
Other,
}
impl L2Kind {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::NoTypetree => "no_typetree",
Self::CheckRead => "check_read",
Self::OutOfBounds => "out_of_bounds",
Self::NegativeLength => "negative_length",
Self::RefType => "ref_type",
Self::Encode => "encode",
Self::Other => "other",
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("{kind:?} at byte {at}: {detail}")]
pub struct ReadError {
pub kind: ReadErrorKind,
pub at: u64,
pub detail: ReadDetail,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ReadErrorKind {
OutOfBounds,
NegativeLength,
RefType,
Other,
}
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ReadDetail {
#[error("needs {need} bytes past the end")]
Eof {
need: u64,
exception: &'static str,
},
#[error("{method} out of bounds (needs {need} bytes)")]
BoostEof {
method: &'static str,
need: u64,
exception: &'static str,
},
#[error("negative length {0}")]
NegativeLength(i64),
#[error("Array node must have 2 children")]
ArrayChildren,
#[error("Pair node must have 2 children")]
PairChildren,
#[error("no SerializedFile.ref_types")]
NoRefTypes {
reader: crate::value::ReadBy,
absent: bool,
},
#[error("Failed to get ref type node")]
RefTypeNotFound,
#[error("'NoneType' object has no attribute 'm_MetaFlag'")]
RefTypeNoTree,
#[error("python reader: {0}")]
Python(&'static str),
#[error("{}: {}", .0.exception(), .0)]
Stream(StreamRefusal),
#[error(transparent)]
Limit(LimitError),
#[error(transparent)]
Charge(ChargeError),
#[error(transparent)]
Tree(TreeError),
}
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ParseError {
#[error("no typetree for class {class_id}")]
NoTypeTree {
class_id: i32,
why: Option<NoTree>,
},
#[error(transparent)]
Read(#[from] ReadError),
#[error("Expected to read {expected} bytes, but only read {} bytes", .parsed.bytes_read)]
CheckRead {
expected: u32,
parsed: Arc<Parsed>,
},
#[error(transparent)]
Tree(#[from] TreeError),
#[error(transparent)]
Provider(#[from] ProviderError),
#[error("m_Script: {0}")]
Deref(crate::env::DerefError),
}
impl From<crate::budget::AllocError> for ReadDetail {
fn from(error: crate::budget::AllocError) -> Self {
Self::Charge(error.into())
}
}
impl From<crate::budget::AllocError> for ParseError {
fn from(error: crate::budget::AllocError) -> Self {
Self::Tree(error.into())
}
}
impl crate::once::CellError for ParseError {
fn is_transient(&self) -> bool {
match self {
Self::Read(r) => match &r.detail {
ReadDetail::Charge(c) => c.is_transient(),
ReadDetail::Tree(t) => t.is_transient(),
_ => false,
},
Self::Tree(t) => t.is_transient(),
Self::Provider(_) => true,
Self::Deref(d) => d.is_transient(),
Self::NoTypeTree { .. } | Self::CheckRead { .. } => false,
}
}
fn is_meter_refusal(&self) -> bool {
match self {
Self::Read(r) => match &r.detail {
ReadDetail::Charge(c) => c.is_meter_refusal(),
ReadDetail::Tree(t) => t.is_meter_refusal(),
_ => false,
},
Self::Tree(t) => t.is_meter_refusal(),
Self::Deref(d) => d.is_meter_refusal(),
Self::Provider(_) | Self::NoTypeTree { .. } | Self::CheckRead { .. } => false,
}
}
fn heap_bytes(&self) -> u64 {
match self {
Self::CheckRead { .. } => crate::error::heap_of(std::mem::size_of::<Parsed>() + 16),
Self::Provider(p) => crate::error::heap_of(p.detail.len()),
Self::Deref(d) => d.heap_bytes(),
_ => 0,
}
}
fn work_refused(limit: LimitError) -> Self {
Self::Read(ReadError {
kind: ReadErrorKind::Other,
at: 0,
detail: ReadDetail::Limit(limit),
})
}
}
impl ParseError {
#[must_use]
pub fn l2_kind(&self) -> L2Kind {
match self {
Self::NoTypeTree { .. } => L2Kind::NoTypetree,
Self::CheckRead { parsed, .. } if parsed.bytes_read < 0 => L2Kind::Other,
Self::CheckRead { .. } => L2Kind::CheckRead,
Self::Read(e) => match e.kind {
ReadErrorKind::OutOfBounds => L2Kind::OutOfBounds,
ReadErrorKind::NegativeLength => L2Kind::NegativeLength,
ReadErrorKind::RefType => L2Kind::RefType,
ReadErrorKind::Other => L2Kind::Other,
},
Self::Tree(_) | Self::Provider(_) | Self::Deref(_) => L2Kind::Other,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum WriteError {
#[error("missing key {0:?}")]
MissingKey(RawStr),
#[error("{node:?}: expected {expected}")]
TypeMismatch {
node: RawStr,
expected: &'static str,
},
#[error("{node:?}: value out of range")]
OutOfRange {
node: RawStr,
},
#[error("SerializedFile has no ref_types")]
NoRefTypes,
#[error("'SerializedFile' object has no attribute 'ref_types'")]
NoRefTypesAttribute,
#[error("Referenced type not found")]
RefTypeNotFound,
#[error("malformed node {0:?}")]
Malformed(RawStr),
#[error("no typetree for class {class_id}")]
NoTypeTree {
class_id: i32,
why: Option<NoTree>,
},
#[error("typetree: {0}")]
Tree(Box<ParseError>),
#[error(transparent)]
Limit(LimitError),
#[error(transparent)]
Charge(ChargeError),
}
impl WriteError {
#[must_use]
pub fn exceptions(&self) -> &'static [&'static str] {
match self {
Self::MissingKey(_) => &["KeyError"],
Self::TypeMismatch { expected, .. } => match *expected {
"int" | "float" => &["error"],
"str"
| "an object with this attribute"
| "a ref type with a tree"
| "a dict or an object with class, ns and asm" => &["AttributeError"],
"bytes" | "a sized iterable" | "dict" => &["TypeError"],
"a sequence of two" => &["TypeError", "KeyError", "IndexError"],
_ => &[],
},
Self::OutOfRange { .. } => &["error", "OverflowError"],
Self::NoRefTypes | Self::RefTypeNotFound => &["ValueError"],
Self::NoRefTypesAttribute => &["AttributeError"],
Self::Malformed(_) => &["IndexError"],
Self::NoTypeTree { why, .. } => match why {
Some(NoTree::UnknownClass) => &["KeyError"],
Some(NoTree::NoEntry) => &["ValueError"],
Some(NoTree::NoReleaseRoot) => &["AssertionError"],
Some(NoTree::NoVersion) => &["NotImplementedError"],
None => &[],
},
Self::Tree(_) | Self::Limit(_) | Self::Charge(_) => &[],
}
}
}
pub(crate) fn stream_refused(at: u64, e: StreamRefusal) -> ReadError {
ReadError {
kind: ReadErrorKind::Other,
at,
detail: ReadDetail::Stream(e),
}
}
#[derive(Debug)]
pub(crate) enum Out {
Plain(Value),
Built(Built),
}
impl Out {
#[inline]
pub(crate) fn push_to(self, items: &mut MeteredVec<Value>) -> Result<(), ChargeError> {
match self {
Self::Plain(v) => items.push(v),
Self::Built(b) => items.push_built(b),
}
}
#[inline]
pub(crate) fn push_entry(
self,
entries: &mut MeteredVec<(NodeId, Value)>,
key: NodeId,
) -> Result<(), ChargeError> {
match self {
Self::Plain(v) => entries.push((key, v)),
Self::Built(b) => entries.push_entry(key, b),
}
}
pub(crate) fn set_entry(self, entries: &mut MeteredVec<(NodeId, Value)>, pos: usize) {
match self {
Self::Plain(v) => {
if let Some(e) = entries.as_mut_slice().get_mut(pos) {
e.1 = v;
}
}
Self::Built(b) => {
entries.set_entry(pos, b);
}
}
}
pub(crate) fn into_built(
self,
builder: &crate::value::ValueBuilder,
at: u64,
) -> Result<Built, ReadError> {
match self {
Self::Built(b) => Ok(b),
Self::Plain(v) => builder.scalar(v).map_err(|_| ReadError {
kind: ReadErrorKind::Other,
at,
detail: ReadDetail::Python("a plain value owned heap"),
}),
}
}
}
pub(crate) fn f32_bits(bits: u32, mode: FloatMode) -> u32 {
let nan = bits & 0x7f80_0000 == 0x7f80_0000 && bits & 0x007f_ffff != 0;
if nan && mode == FloatMode::UnityPy {
bits | 0x0040_0000
} else {
bits
}
}
pub(crate) fn int(v: i64) -> Value {
Value::Int(v)
}
pub(crate) fn uint(v: u64) -> Value {
i64::try_from(v).map_or(Value::UInt(v), Value::Int)
}
pub(crate) fn py_size_field_is_zero(v: &Value) -> bool {
match v {
Value::Str(s) => s.is_empty(),
Value::Int(i) => *i == 0,
Value::UInt(_) => false,
Value::Bool(b) => !*b,
Value::F32(b) => f64::from(f32::from_bits(*b)).to_bits() == 0,
Value::F64(b) => *b == 0,
Value::Bytes(b) => b.is_empty(),
Value::Prims(p) => p.is_empty(),
Value::List(l) => l.is_empty(),
Value::Tuple(t) => t.is_empty(),
Value::Dict(m) => m.is_empty(),
}
}
pub(crate) fn limit_error(at: u64, kind: LimitKind, value: u64, max: u64) -> ReadError {
ReadError {
kind: ReadErrorKind::Other,
at,
detail: ReadDetail::Limit(LimitError::refused(kind, value, max)),
}
}
pub(crate) fn charge_error(at: u64, e: ChargeError) -> ReadError {
ReadError {
kind: ReadErrorKind::Other,
at,
detail: ReadDetail::Charge(e),
}
}
pub(crate) fn first_reserve(len: usize, remaining: usize, min_size: u32, room: usize) -> usize {
let backed = if min_size == 0 {
usize::MAX
} else {
remaining / (min_size as usize)
};
len.min(backed).min(room)
}
pub(crate) fn grow_step<T>(items: &mut MeteredVec<T>, len: usize) -> Result<(), ChargeError> {
if items.len() < items.capacity() {
return Ok(());
}
items.reserve_within(1, len)
}