use crate::host::with_host;
use fusevm::Value;
use super::common::arg;
pub fn dispatch(name: &str, args: &[Value]) -> Option<Result<Value, String>> {
let v = match name {
"is_scalar" => Value::bool(matches!(
arg(args, 0),
Value::Int(_) | Value::Float(_) | Value::Str(_) | Value::Bool(_)
)),
"is_object" => {
let a = arg(args, 0);
with_host(|h| Value::bool(h.is_object_value(&a)))
}
"is_countable" => {
let a = arg(args, 0);
with_host(|h| {
Value::bool(
h.is_array(&a)
|| h.object_class(&a)
.is_some_and(|c| h.class_is_a_pub(&c, "Countable")),
)
})
}
"is_iterable" => {
let a = arg(args, 0);
with_host(|h| {
Value::bool(
h.is_array(&a)
|| h.is_generator_val(&a)
|| h.object_class(&a).is_some_and(|c| {
h.class_is_a_pub(&c, "Traversable")
|| h.class_is_a_pub(&c, "Iterator")
|| h.class_is_a_pub(&c, "IteratorAggregate")
}),
)
})
}
"get_debug_type" => {
let a = arg(args, 0);
let name = with_host(|h| debug_type(h, &a));
Value::str(name)
}
"serialize" => {
let a = arg(args, 0);
let line = crate::host::warn_line();
let answers = crate::host::collect_object_hooks(
std::slice::from_ref(&a),
&["__serialize", "__sleep"],
&|obj, class, method, r| {
if method == "__sleep" {
crate::host::set_warn_line(line);
return Some(with_host(|h| sleep_props(h, obj, class, &r)));
}
if with_host(|h| h.is_array(&r)) {
return Some(r);
}
let msg = format!(
"{}::__serialize() must return an array",
crate::host::display_class(class)
);
let _ = crate::host::throw_from_internal(
"serialize",
std::slice::from_ref(&a),
"TypeError",
&msg,
);
None
},
);
let Some(answers) = answers else {
return Some(Ok(Value::Undef));
};
let out = with_host(|h| {
let outer = std::mem::replace(&mut h.serialize_info, answers);
let denied = unserializable_in(h, &a, &mut crate::host::Visiting::default());
let s = php_serialize(h, &a);
h.serialize_info = outer;
denied.map_or(Ok(s), Err)
});
match out {
Ok(s) => Value::str(s),
Err(class) => {
let _ = crate::host::throw_from_internal(
"serialize",
std::slice::from_ref(&a),
"Exception",
&format!("Serialization of '{class}' is not allowed"),
);
Value::Undef
}
}
}
"unserialize" => {
let s = with_host(|h| h.to_str(&arg(args, 0)));
let raw = crate::bytes::to_bytes(&s);
let bytes: &[u8] = &raw;
let total = bytes.len();
if total == 0 {
return Some(Ok(Value::bool(false)));
}
let mut deferred = Vec::new();
let result = php_unserialize(bytes, &mut deferred);
if let Unser::Denied(class) = &result {
let _ = crate::host::throw_from_internal(
"unserialize",
std::slice::from_ref(&arg(args, 0)),
"Exception",
&format!("Unserialization of '{class}' is not allowed"),
);
return Some(Ok(Value::Undef));
}
if !matches!(result, Unser::Failed(..)) {
for (obj, data) in deferred {
let class = with_host(|h| h.object_class(&obj)).unwrap_or_default();
let _ = match data {
Some(data) => {
crate::host::call_method(&class, "__unserialize", Some(obj), vec![data])
}
None => crate::host::call_method(&class, "__wakeup", Some(obj), Vec::new()),
};
if crate::host::unwinding() {
return Some(Ok(Value::Undef));
}
}
}
match result {
Unser::Value(v) => v,
Unser::Saturated(v) => {
with_host(|h| h.warn("unserialize(): Numerical result out of range"));
v
}
Unser::Extra(v, at) => {
with_host(|h| {
h.warn(format_args!(
"unserialize(): Extra data starting at offset {at} of {total} bytes"
))
});
v
}
Unser::Failed(at, short) => {
with_host(|h| {
if short {
h.warn("unserialize(): Unexpected end of serialized data");
}
h.warn(format_args!(
"unserialize(): Error at offset {at} of {total} bytes"
))
});
Value::bool(false)
}
Unser::Denied(_) => Value::Undef,
}
}
_ => return None,
};
Some(Ok(v))
}
pub fn value_name(h: &crate::host::PhpHost, v: &Value) -> String {
match v {
Value::Bool(true) => "true".to_string(),
Value::Bool(false) => "false".to_string(),
_ => debug_type(h, v),
}
}
pub fn debug_type(h: &crate::host::PhpHost, v: &Value) -> String {
match v {
Value::Undef => "null".to_string(),
Value::Bool(_) => "bool".to_string(),
Value::Int(_) => "int".to_string(),
Value::Float(_) => "float".to_string(),
Value::Str(_) => "string".to_string(),
Value::Obj(_) => {
if h.is_array(v) {
"array".to_string()
} else if let Some((_, closed)) = h.resource_info(v) {
if closed {
"resource (closed)"
} else {
"resource (stream)"
}
.to_string()
} else {
h.instance_class(v).map_or_else(
|| "object".to_string(),
|c| crate::host::display_class(&c).to_string(),
)
}
}
_ => "unknown".to_string(),
}
}
const NOT_SERIALIZABLE: [&str; 2] = ["weakmap", "weakreference"];
fn unserializable_in(
h: &crate::host::PhpHost,
v: &Value,
seen: &mut crate::host::Visiting,
) -> Option<String> {
if h.is_closure(v) {
return Some("Closure".to_string());
}
if h.is_generator_val(v) {
return Some("Generator".to_string());
}
if !(h.is_array(v) || h.is_object(v)) || h.is_stream(v) || !seen.enter(v) {
return None;
}
let children: Vec<Value> = if h.is_array(v) {
h.array_pairs(v)
.unwrap_or_default()
.into_iter()
.map(|(_, val)| val)
.collect()
} else {
let class = h.object_class(v).unwrap_or_default();
if class.contains("@anonymous")
|| NOT_SERIALIZABLE.contains(&class.to_ascii_lowercase().as_str())
{
seen.leave();
return Some(crate::host::display_class(&class).to_string());
}
let answer = match v {
Value::Obj(id) => h.serialize_info.get(id),
_ => None,
};
match answer {
Some(data) => h
.array_pairs(data)
.unwrap_or_default()
.into_iter()
.map(|(_, val)| val)
.collect(),
None if h.enum_case_of(v).is_some() => Vec::new(),
None => h.object_props(v).into_iter().map(|(_, val)| val).collect(),
}
};
let found = children.iter().find_map(|c| unserializable_in(h, c, seen));
seen.leave();
found
}
fn not_unserializable(class: &str) -> Option<String> {
let lower = class.to_ascii_lowercase();
match lower.as_str() {
"closure" => return Some("Closure".to_string()),
"generator" => return Some("Generator".to_string()),
_ => {}
}
with_host(|h| {
let refused = NOT_SERIALIZABLE.contains(&lower.as_str()) || lower.contains("@anonymous");
(refused && h.class_exists(class))
.then(|| crate::host::display_class(&h.class_display_name(&lower)).to_string())
})
}
fn php_serialize(h: &crate::host::PhpHost, v: &Value) -> String {
php_serialize_seen(h, v, &mut crate::host::Visiting::default())
}
fn php_serialize_seen(
h: &crate::host::PhpHost,
v: &Value,
seen: &mut crate::host::Visiting,
) -> String {
if (h.is_array(v) || h.is_object(v)) && !seen.enter(v) {
return "N;".to_string();
}
let out = php_serialize_body(h, v, seen);
if h.is_array(v) || h.is_object(v) {
seen.leave();
}
out
}
fn php_serialize_body(
h: &crate::host::PhpHost,
v: &Value,
seen: &mut crate::host::Visiting,
) -> String {
match v {
Value::Undef => "N;".to_string(),
Value::Bool(b) => format!("b:{};", *b as i32),
Value::Int(n) => format!("i:{n};"),
Value::Float(f) => format!("d:{};", serialize_float(*f)),
Value::Str(s) => format!("s:{}:\"{}\";", crate::bytes::byte_len(s), s),
Value::Obj(_) if h.is_stream(v) => "i:0;".to_string(),
Value::Obj(_) if h.is_array(v) => {
let pairs = h.array_pairs(v).unwrap_or_default();
let mut out = format!("a:{}:{{", pairs.len());
for (k, val) in pairs {
out.push_str(&php_serialize_seen(h, &k, seen));
out.push_str(&php_serialize_seen(h, &val, seen));
}
out.push('}');
out
}
Value::Obj(_) if h.is_object(v) => php_serialize_object(h, v, seen),
_ => "N;".to_string(),
}
}
fn php_serialize_object(
h: &crate::host::PhpHost,
v: &Value,
seen: &mut crate::host::Visiting,
) -> String {
let class = h.object_class(v).unwrap_or_else(|| "stdClass".to_string());
if let Some((case, _)) = h.enum_case_of(v) {
let tag = format!("{class}:{case}");
return format!("E:{}:\"{tag}\";", tag.len());
}
let answer = match v {
Value::Obj(id) => h.serialize_info.get(id),
_ => None,
};
if let Some(data) = answer {
if !h.is_array(data) {
return "N;".to_string();
}
let pairs = h.array_pairs(data).unwrap_or_default();
let mut out = format!("O:{}:\"{class}\":{}:{{", class.len(), pairs.len());
for (k, val) in pairs {
out.push_str(&php_serialize_seen(h, &k, seen));
out.push_str(&php_serialize_seen(h, &val, seen));
}
out.push('}');
return out;
}
let props = h.object_props(v);
let mut out = format!("O:{}:\"{class}\":{}:{{", class.len(), props.len());
for (name, val) in props {
match crate::host::prop_key(&name) {
Value::Int(n) => out.push_str(&format!("i:{n};")),
_ => {
let key = mangled_prop_key(h, &class, &name);
out.push_str(&format!("s:{}:\"{key}\";", crate::bytes::byte_len(&key)));
}
}
out.push_str(&php_serialize_seen(h, &val, seen));
}
out.push('}');
out
}
fn sleep_props(h: &mut crate::host::PhpHost, obj: &Value, class: &str, names: &Value) -> Value {
let class_name = crate::host::display_class(class);
let not_names = format!(
"serialize(): {class_name}::__sleep() should return an array only containing the names of \
instance-variables to serialize"
);
if !h.is_array(names) {
h.warn(not_names);
return Value::Undef;
}
let props: Vec<(String, Value)> = h
.object_props(obj)
.into_iter()
.map(|(name, val)| (mangled_prop_key(h, class, &name), val))
.collect();
let out = h.new_array();
let mut added: Vec<String> = Vec::new();
for (_, name_v) in h.array_pairs(names).unwrap_or_default() {
if !matches!(name_v, Value::Str(_)) {
h.warn(not_names.clone());
}
let name = h.to_str_diag(&name_v);
let declared_key = mangled_prop_key(h, class, &name);
let uninit = h.uninit_read_error(obj, &name).is_some();
let candidates = [
name.clone(),
format!("\0{class_name}\0{name}"),
format!("\0*\0{name}"),
];
let found = candidates.iter().find_map(|key| {
if let Some((_, val)) = props.iter().find(|(k, _)| k == key) {
return Some(Some((key.clone(), val.clone())));
}
(uninit && *key == declared_key).then_some(None)
});
match found {
Some(Some((key, val))) => {
if added.contains(&key) {
h.warn(format!("serialize(): \"{name}\" is returned from __sleep() multiple times"));
} else {
h.arr_set_key(&out, &Value::str(key.clone()), val);
added.push(key);
}
}
Some(None) => {}
None => h.warn(format!(
"serialize(): \"{name}\" returned as member variable from __sleep() but does not exist"
)),
}
}
out
}
fn mangled_prop_key(h: &crate::host::PhpHost, class: &str, name: &str) -> String {
match h.prop_visibility(class, name) {
Some((_, crate::ast::Visibility::Protected)) => format!("\0*\0{name}"),
Some((declaring, crate::ast::Visibility::Private)) => format!("\0{declaring}\0{name}"),
_ => name.to_string(),
}
}
pub fn serialize_float(f: f64) -> String {
if f.is_nan() {
return "NAN".to_string();
}
if f.is_infinite() {
return if f < 0.0 { "-INF" } else { "INF" }.to_string();
}
if f == 0.0 {
return if f.is_sign_negative() { "-0" } else { "0" }.to_string();
}
let neg = f < 0.0;
let a = f.abs();
let sci = format!("{a:e}");
let (mant, exp_s) = sci.split_once('e').expect("`{:e}` always contains 'e'");
let exp: i32 = exp_s.parse().expect("`{:e}` exponent is an integer");
let digits: String = mant.chars().filter(|c| *c != '.').collect();
let decpt = exp + 1;
let body = if !(-3..=17).contains(&decpt) {
let (first, rest) = digits.split_at(1);
let frac = if rest.is_empty() { "0" } else { rest };
let e = decpt - 1;
format!(
"{first}.{frac}E{}{}",
if e < 0 { "-" } else { "+" },
e.abs()
)
} else if decpt <= 0 {
format!("0.{}{}", "0".repeat((-decpt) as usize), digits)
} else if decpt as usize >= digits.len() {
format!("{digits}{}", "0".repeat(decpt as usize - digits.len()))
} else {
let (int_part, frac) = digits.split_at(decpt as usize);
format!("{int_part}.{frac}")
};
if neg {
format!("-{body}")
} else {
body
}
}
enum Unser {
Value(Value),
Extra(Value, usize),
Saturated(Value),
Failed(usize, bool),
Denied(String),
}
fn php_unserialize(b: &[u8], deferred: &mut Vec<(Value, Option<Value>)>) -> Unser {
let mut p = Parser {
b,
deferred,
pos: 0,
fail_at: None,
unexpected_end: false,
saturated: false,
denied: None,
};
let Some(v) = p.value() else {
if let Some(class) = p.denied {
return Unser::Denied(class);
}
return Unser::Failed(p.fail_at.unwrap_or(0), p.unexpected_end);
};
if p.pos != b.len() {
Unser::Extra(v, p.pos)
} else if p.saturated {
Unser::Saturated(v)
} else {
Unser::Value(v)
}
}
struct Parser<'a> {
b: &'a [u8],
deferred: &'a mut Vec<(Value, Option<Value>)>,
pos: usize,
fail_at: Option<usize>,
unexpected_end: bool,
saturated: bool,
denied: Option<String>,
}
impl<'a> Parser<'a> {
fn eat(&mut self, s: &[u8]) -> Option<()> {
if self.b[self.pos..].starts_with(s) {
self.pos += s.len();
Some(())
} else {
None
}
}
fn read_until(&mut self, delim: u8) -> Option<&'a [u8]> {
let start = self.pos;
while self.pos < self.b.len() && self.b[self.pos] != delim {
self.pos += 1;
}
if self.pos >= self.b.len() {
return None;
}
let slice = &self.b[start..self.pos];
self.pos += 1; Some(slice)
}
fn int_until(&mut self, delim: u8) -> Option<i64> {
let s = std::str::from_utf8(self.read_until(delim)?).ok()?;
s.parse().ok()
}
fn int_value(&mut self, delim: u8) -> Option<i64> {
let s = std::str::from_utf8(self.read_until(delim)?).ok()?;
match s.parse::<i64>() {
Ok(n) => Some(n),
Err(_) => {
let (neg, body) = match s.strip_prefix('-') {
Some(rest) => (true, rest),
None => (false, s),
};
if !body.is_empty() && body.bytes().all(|c| c.is_ascii_digit()) {
self.saturated = true;
Some(if neg { i64::MIN } else { i64::MAX })
} else {
None
}
}
}
}
fn fail_at(&mut self, offset: usize) -> Option<Value> {
self.fail_at.get_or_insert(offset);
None
}
fn container_exhausted(&mut self) -> Option<Option<Value>> {
if self.b.get(self.pos) != Some(&b'}') {
return None;
}
self.unexpected_end = true;
Some(self.fail_at(self.pos))
}
fn close_container(&mut self) -> Option<()> {
if self.b.get(self.pos) != Some(&b'}') {
self.fail_at(self.pos);
return None;
}
self.pos += 1;
Some(())
}
fn value(&mut self) -> Option<Value> {
let start = self.pos;
let Some(&tag) = self.b.get(self.pos) else {
return self.fail_at(start);
};
let blame = match tag {
b's' | b'O' | b'E' => start + 2,
_ => start,
};
match self.value_inner(tag) {
Some(v) => Some(v),
None => self.fail_at(blame),
}
}
fn value_inner(&mut self, tag: u8) -> Option<Value> {
match tag {
b'N' => {
self.eat(b"N;")?;
Some(Value::Undef)
}
b'b' => {
self.eat(b"b:")?;
let d = *self.b.get(self.pos)?;
self.pos += 1;
self.eat(b";")?;
match d {
b'0' => Some(Value::bool(false)),
b'1' => Some(Value::bool(true)),
_ => None,
}
}
b'i' => {
self.eat(b"i:")?;
let n = self.int_value(b';')?;
Some(Value::int(n))
}
b'd' => {
self.eat(b"d:")?;
let s = std::str::from_utf8(self.read_until(b';')?).ok()?;
let f = match s {
"NAN" => f64::NAN,
"INF" => f64::INFINITY,
"-INF" => f64::NEG_INFINITY,
_ => s.parse().ok()?,
};
Some(Value::float(f))
}
b's' => {
self.eat(b"s:")?;
let len = self.int_until(b':')? as usize;
self.eat(b"\"")?;
let end = self.pos.checked_add(len)?;
if end > self.b.len() {
return None;
}
let bytes = &self.b[self.pos..end];
self.pos = end;
self.eat(b"\";")?;
Some(Value::str(crate::bytes::from_bytes(bytes)))
}
b'a' => {
self.eat(b"a:")?;
let count = self.int_until(b':')?;
if count < 0 {
return None;
}
self.eat(b"{")?;
let mut pairs: Vec<(Value, Value)> = Vec::new();
for _ in 0..count {
if let Some(fail) = self.container_exhausted() {
return fail;
}
let k = self.value()?;
if let Some(fail) = self.container_exhausted() {
return fail;
}
let v = self.value()?;
pairs.push((k, v));
}
self.close_container()?;
Some(super::common::make_map(pairs))
}
b'O' => {
self.eat(b"O:")?;
let class = self.length_prefixed_string()?;
if let Some(name) = not_unserializable(&class) {
self.denied = Some(name);
return None;
}
if crate::host::class_known(&class).is_err() {
return None;
}
self.eat(b":")?;
let count = self.int_until(b':')?;
if count < 0 {
return None;
}
self.eat(b"{")?;
let custom = with_host(|h| {
h.class_exists(&class) && h.class_has_method(&class, "__unserialize")
});
if custom {
let obj = crate::host::alloc_object(&class).ok()?;
let mut pairs = Vec::with_capacity(count as usize);
for _ in 0..count {
if let Some(fail) = self.container_exhausted() {
return fail;
}
let k = self.value()?;
if let Some(fail) = self.container_exhausted() {
return fail;
}
pairs.push((k, self.value()?));
}
self.close_container()?;
self.deferred
.push((obj.clone(), Some(super::common::make_map(pairs))));
return Some(obj);
}
let mut props: Vec<(String, Value)> = Vec::with_capacity(count as usize);
for _ in 0..count {
if let Some(fail) = self.container_exhausted() {
return fail;
}
let Value::Str(k) = self.value()? else {
return None;
};
if let Some(fail) = self.container_exhausted() {
return fail;
}
props.push((demangle_prop(&k), self.value()?));
}
self.close_container()?;
let known = with_host(|h| h.class_exists(&class));
if !known {
props.insert(
0,
(
"__PHP_Incomplete_Class_Name".to_string(),
Value::str(class.clone()),
),
);
}
let target = if known {
class
} else {
"__PHP_Incomplete_Class".to_string()
};
let wakes = known && with_host(|h| h.class_has_method(&target, "__wakeup"));
let allocated = if known {
crate::host::alloc_object(&target).ok()
} else {
None
};
let obj = match allocated {
Some(obj) => with_host(|h| {
for (name, val) in props {
h.prop_set(&obj, &name, val);
}
obj
}),
None => with_host(|h| h.new_object_bare(&target, props)),
};
if wakes {
self.deferred.push((obj.clone(), None));
}
Some(obj)
}
b'E' => {
self.eat(b"E:")?;
let tag = self.length_prefixed_string()?;
self.eat(b";")?;
let (class, case) = tag.split_once(':')?;
crate::host::class_const_silent(class, case).ok()
}
_ => None,
}
}
fn length_prefixed_string(&mut self) -> Option<String> {
let len = self.int_until(b':')? as usize;
self.eat(b"\"")?;
let end = self.pos.checked_add(len)?;
if end > self.b.len() {
return None;
}
let bytes = &self.b[self.pos..end];
self.pos = end;
self.eat(b"\"")?;
Some(crate::bytes::from_bytes(bytes))
}
}
fn demangle_prop(k: &str) -> String {
match k.strip_prefix('\0') {
Some(rest) => rest
.split_once('\0')
.map(|(_, name)| name.to_string())
.unwrap_or_else(|| k.to_string()),
None => k.to_string(),
}
}