use crate::host::with_host;
use crate::stdlib::common::*;
use fusevm::Value;
pub fn dispatch(name: &str, args: &[Value]) -> Option<Result<Value, String>> {
let r = match name {
"fprintf" => fprintf(args),
"vfprintf" => vfprintf(args),
"fscanf" => fscanf(args),
"array_change_key_case" => Ok(array_change_key_case(args)),
"get_html_translation_table" => Ok(get_html_translation_table(args)),
_ => return None,
};
Some(r)
}
fn fprintf(args: &[Value]) -> Result<Value, String> {
let res = arg(args, 0);
let mut call_args = vec![arg(args, 1)];
if args.len() > 2 {
call_args.extend_from_slice(&args[2..]);
}
write_formatted("fprintf", &res, call_args)
}
fn vfprintf(args: &[Value]) -> Result<Value, String> {
let res = arg(args, 0);
let mut call_args = vec![arg(args, 1)];
let pairs = with_host(|h| h.array_pairs(&arg(args, 2)).unwrap_or_default());
for (_, v) in pairs {
call_args.push(v);
}
write_formatted("vfprintf", &res, call_args)
}
fn write_formatted(fname: &str, res: &Value, call_args: Vec<Value>) -> Result<Value, String> {
let s = crate::builtins::call_library("sprintf", &call_args)?;
let bytes = crate::bytes::to_bytes(&with_host(|h| h.to_str(&s))).into_owned();
let written = crate::stdlib::fileres::write_bytes(fname, res, &bytes);
Ok(match written {
Some(n) => Value::int(n as i64),
None => Value::bool(false),
})
}
fn fscanf(args: &[Value]) -> Result<Value, String> {
let res = arg(args, 0);
let fmt = arg(args, 1);
let line = match crate::stdlib::fileres::read_line("fscanf", &res) {
Some(l) => l,
None => return Ok(Value::bool(false)),
};
crate::builtins::call_library("sscanf", &[Value::str(line), fmt])
}
fn array_change_key_case(args: &[Value]) -> Value {
let upper = resolve_case(&arg(args, 1)) == 1;
let pairs = with_host(|h| h.array_pairs(&arg(args, 0)).unwrap_or_default());
let mapped = pairs
.into_iter()
.map(|(k, v)| {
let k = match &k {
Value::Str(s) if upper => Value::str(s.to_ascii_uppercase()),
Value::Str(s) => Value::str(s.to_ascii_lowercase()),
_ => k,
};
(k, v)
})
.collect();
make_map(mapped)
}
fn resolve_case(v: &Value) -> i64 {
if let Value::Str(name) = v {
match name.as_str() {
"CASE_LOWER" => return 0,
"CASE_UPPER" => return 1,
_ => {}
}
}
v.to_int()
}
fn get_html_translation_table(args: &[Value]) -> Value {
let table = resolve_table(&arg(args, 0));
let flags = if args.len() > 1 {
int_arg(args, 1)
} else {
ENT_DEFAULT
};
let (double_q, single_q) = quote_flags(flags);
let mut pairs: Vec<(Value, Value)> = Vec::new();
let mut push =
|ch: char, ent: &str| pairs.push((Value::str(ch.to_string()), Value::str(ent.to_string())));
if double_q {
push('"', """);
}
push('&', "&");
if single_q {
push('\'', "'");
}
push('<', "<");
push('>', ">");
if table == 1 {
for (cp, name) in HTML401_ENTITIES {
let ch = char::from_u32(*cp).unwrap();
pairs.push((Value::str(ch.to_string()), Value::str(format!("&{name};"))));
}
}
make_map(pairs)
}
fn resolve_table(v: &Value) -> i64 {
if let Value::Str(name) = v {
match name.as_str() {
"HTML_SPECIALCHARS" => return 0,
"HTML_ENTITIES" => return 1,
_ => {}
}
}
v.to_int()
}
pub(crate) const HTML401_ENTITIES: &[(u32, &str)] = &[
(0x00A0, "nbsp"),
(0x00A1, "iexcl"),
(0x00A2, "cent"),
(0x00A3, "pound"),
(0x00A4, "curren"),
(0x00A5, "yen"),
(0x00A6, "brvbar"),
(0x00A7, "sect"),
(0x00A8, "uml"),
(0x00A9, "copy"),
(0x00AA, "ordf"),
(0x00AB, "laquo"),
(0x00AC, "not"),
(0x00AD, "shy"),
(0x00AE, "reg"),
(0x00AF, "macr"),
(0x00B0, "deg"),
(0x00B1, "plusmn"),
(0x00B2, "sup2"),
(0x00B3, "sup3"),
(0x00B4, "acute"),
(0x00B5, "micro"),
(0x00B6, "para"),
(0x00B7, "middot"),
(0x00B8, "cedil"),
(0x00B9, "sup1"),
(0x00BA, "ordm"),
(0x00BB, "raquo"),
(0x00BC, "frac14"),
(0x00BD, "frac12"),
(0x00BE, "frac34"),
(0x00BF, "iquest"),
(0x00C0, "Agrave"),
(0x00C1, "Aacute"),
(0x00C2, "Acirc"),
(0x00C3, "Atilde"),
(0x00C4, "Auml"),
(0x00C5, "Aring"),
(0x00C6, "AElig"),
(0x00C7, "Ccedil"),
(0x00C8, "Egrave"),
(0x00C9, "Eacute"),
(0x00CA, "Ecirc"),
(0x00CB, "Euml"),
(0x00CC, "Igrave"),
(0x00CD, "Iacute"),
(0x00CE, "Icirc"),
(0x00CF, "Iuml"),
(0x00D0, "ETH"),
(0x00D1, "Ntilde"),
(0x00D2, "Ograve"),
(0x00D3, "Oacute"),
(0x00D4, "Ocirc"),
(0x00D5, "Otilde"),
(0x00D6, "Ouml"),
(0x00D7, "times"),
(0x00D8, "Oslash"),
(0x00D9, "Ugrave"),
(0x00DA, "Uacute"),
(0x00DB, "Ucirc"),
(0x00DC, "Uuml"),
(0x00DD, "Yacute"),
(0x00DE, "THORN"),
(0x00DF, "szlig"),
(0x00E0, "agrave"),
(0x00E1, "aacute"),
(0x00E2, "acirc"),
(0x00E3, "atilde"),
(0x00E4, "auml"),
(0x00E5, "aring"),
(0x00E6, "aelig"),
(0x00E7, "ccedil"),
(0x00E8, "egrave"),
(0x00E9, "eacute"),
(0x00EA, "ecirc"),
(0x00EB, "euml"),
(0x00EC, "igrave"),
(0x00ED, "iacute"),
(0x00EE, "icirc"),
(0x00EF, "iuml"),
(0x00F0, "eth"),
(0x00F1, "ntilde"),
(0x00F2, "ograve"),
(0x00F3, "oacute"),
(0x00F4, "ocirc"),
(0x00F5, "otilde"),
(0x00F6, "ouml"),
(0x00F7, "divide"),
(0x00F8, "oslash"),
(0x00F9, "ugrave"),
(0x00FA, "uacute"),
(0x00FB, "ucirc"),
(0x00FC, "uuml"),
(0x00FD, "yacute"),
(0x00FE, "thorn"),
(0x00FF, "yuml"),
(0x0152, "OElig"),
(0x0153, "oelig"),
(0x0160, "Scaron"),
(0x0161, "scaron"),
(0x0178, "Yuml"),
(0x0192, "fnof"),
(0x02C6, "circ"),
(0x02DC, "tilde"),
(0x0391, "Alpha"),
(0x0392, "Beta"),
(0x0393, "Gamma"),
(0x0394, "Delta"),
(0x0395, "Epsilon"),
(0x0396, "Zeta"),
(0x0397, "Eta"),
(0x0398, "Theta"),
(0x0399, "Iota"),
(0x039A, "Kappa"),
(0x039B, "Lambda"),
(0x039C, "Mu"),
(0x039D, "Nu"),
(0x039E, "Xi"),
(0x039F, "Omicron"),
(0x03A0, "Pi"),
(0x03A1, "Rho"),
(0x03A3, "Sigma"),
(0x03A4, "Tau"),
(0x03A5, "Upsilon"),
(0x03A6, "Phi"),
(0x03A7, "Chi"),
(0x03A8, "Psi"),
(0x03A9, "Omega"),
(0x03B1, "alpha"),
(0x03B2, "beta"),
(0x03B3, "gamma"),
(0x03B4, "delta"),
(0x03B5, "epsilon"),
(0x03B6, "zeta"),
(0x03B7, "eta"),
(0x03B8, "theta"),
(0x03B9, "iota"),
(0x03BA, "kappa"),
(0x03BB, "lambda"),
(0x03BC, "mu"),
(0x03BD, "nu"),
(0x03BE, "xi"),
(0x03BF, "omicron"),
(0x03C0, "pi"),
(0x03C1, "rho"),
(0x03C2, "sigmaf"),
(0x03C3, "sigma"),
(0x03C4, "tau"),
(0x03C5, "upsilon"),
(0x03C6, "phi"),
(0x03C7, "chi"),
(0x03C8, "psi"),
(0x03C9, "omega"),
(0x03D1, "thetasym"),
(0x03D2, "upsih"),
(0x03D6, "piv"),
(0x2002, "ensp"),
(0x2003, "emsp"),
(0x2009, "thinsp"),
(0x200C, "zwnj"),
(0x200D, "zwj"),
(0x200E, "lrm"),
(0x200F, "rlm"),
(0x2013, "ndash"),
(0x2014, "mdash"),
(0x2018, "lsquo"),
(0x2019, "rsquo"),
(0x201A, "sbquo"),
(0x201C, "ldquo"),
(0x201D, "rdquo"),
(0x201E, "bdquo"),
(0x2020, "dagger"),
(0x2021, "Dagger"),
(0x2022, "bull"),
(0x2026, "hellip"),
(0x2030, "permil"),
(0x2032, "prime"),
(0x2033, "Prime"),
(0x2039, "lsaquo"),
(0x203A, "rsaquo"),
(0x203E, "oline"),
(0x2044, "frasl"),
(0x20AC, "euro"),
(0x2111, "image"),
(0x2118, "weierp"),
(0x211C, "real"),
(0x2122, "trade"),
(0x2135, "alefsym"),
(0x2190, "larr"),
(0x2191, "uarr"),
(0x2192, "rarr"),
(0x2193, "darr"),
(0x2194, "harr"),
(0x21B5, "crarr"),
(0x21D0, "lArr"),
(0x21D1, "uArr"),
(0x21D2, "rArr"),
(0x21D3, "dArr"),
(0x21D4, "hArr"),
(0x2200, "forall"),
(0x2202, "part"),
(0x2203, "exist"),
(0x2205, "empty"),
(0x2207, "nabla"),
(0x2208, "isin"),
(0x2209, "notin"),
(0x220B, "ni"),
(0x220F, "prod"),
(0x2211, "sum"),
(0x2212, "minus"),
(0x2217, "lowast"),
(0x221A, "radic"),
(0x221D, "prop"),
(0x221E, "infin"),
(0x2220, "ang"),
(0x2227, "and"),
(0x2228, "or"),
(0x2229, "cap"),
(0x222A, "cup"),
(0x222B, "int"),
(0x2234, "there4"),
(0x223C, "sim"),
(0x2245, "cong"),
(0x2248, "asymp"),
(0x2260, "ne"),
(0x2261, "equiv"),
(0x2264, "le"),
(0x2265, "ge"),
(0x2282, "sub"),
(0x2283, "sup"),
(0x2284, "nsub"),
(0x2286, "sube"),
(0x2287, "supe"),
(0x2295, "oplus"),
(0x2297, "otimes"),
(0x22A5, "perp"),
(0x22C5, "sdot"),
(0x2308, "lceil"),
(0x2309, "rceil"),
(0x230A, "lfloor"),
(0x230B, "rfloor"),
(0x2329, "lang"),
(0x232A, "rang"),
(0x25CA, "loz"),
(0x2660, "spades"),
(0x2663, "clubs"),
(0x2665, "hearts"),
(0x2666, "diams"),
];
pub(crate) fn quote_flags(flags: i64) -> (bool, bool) {
(flags & 2 != 0, flags & 1 != 0)
}
pub(crate) const ENT_DEFAULT: i64 = 3 | 8;
pub(crate) fn html_encode(s: &str, flags: i64, named: bool) -> String {
let (dq, sq) = quote_flags(flags);
let mut out = String::with_capacity(s.len());
for ch in s.chars() {
match ch {
'&' => out.push_str("&"),
'<' => out.push_str("<"),
'>' => out.push_str(">"),
'"' if dq => out.push_str("""),
'\'' if sq => out.push_str("'"),
_ => match named.then(|| entity_name(ch as u32)).flatten() {
Some(name) => {
out.push('&');
out.push_str(name);
out.push(';');
}
None => out.push(ch),
},
}
}
out
}
fn entity_name(cp: u32) -> Option<&'static str> {
HTML401_ENTITIES
.binary_search_by_key(&cp, |(c, _)| *c)
.ok()
.map(|i| HTML401_ENTITIES[i].1)
}
fn entity_cp(name: &str) -> Option<u32> {
HTML401_ENTITIES
.iter()
.find(|(_, n)| *n == name)
.map(|(c, _)| *c)
}
pub(crate) fn html_decode(s: &str, flags: i64, named: bool) -> String {
let (dq, sq) = quote_flags(flags);
let b = s.as_bytes();
let mut out = String::with_capacity(s.len());
let mut i = 0usize;
while i < b.len() {
if b[i] != b'&' {
let start = i;
while i < b.len() && b[i] != b'&' {
i += 1;
}
out.push_str(&s[start..i]);
continue;
}
let Some(semi) = b[i + 1..]
.iter()
.position(|&c| c == b';')
.map(|p| i + 1 + p)
.filter(|semi| semi - i <= 10)
else {
out.push('&');
i += 1;
continue;
};
let body = &s[i + 1..semi];
let decoded = if let Some(digits) = body.strip_prefix('#') {
let cp = match digits.strip_prefix(['x', 'X']) {
Some(hex) => u32::from_str_radix(hex, 16).ok(),
None => digits.parse::<u32>().ok().filter(|_| !digits.is_empty()),
};
match cp.and_then(char::from_u32) {
Some('\'') => sq.then_some('\''),
Some('"') => dq.then_some('"'),
Some(_) if !named => None,
other => other,
}
} else {
match body {
"lt" => Some('<'),
"gt" => Some('>'),
"amp" => Some('&'),
"quot" => dq.then_some('"'),
_ if named => entity_cp(body).and_then(char::from_u32),
_ => None,
}
};
match decoded {
Some(ch) => {
out.push_str(&crate::bytes::from_text(ch.encode_utf8(&mut [0u8; 4])));
i = semi + 1;
}
None => {
out.push('&');
i += 1;
}
}
}
out
}
const HTML_CHARSETS: &[&str] = &[
"ISO-8859-1",
"ISO8859-1",
"ISO-8859-15",
"ISO8859-15",
"utf-8",
"cp1252",
"Windows-1252",
"1252",
"BIG5",
"950",
"GB2312",
"936",
"Big5-HKSCS",
"Shift_JIS",
"SJIS",
"932",
"SJIS-win",
"CP932",
"EUCJP",
"EUC-JP",
"eucJP-win",
"KOI8-R",
"koi8-ru",
"koi8r",
"cp1251",
"Windows-1251",
"win-1251",
"iso8859-5",
"iso-8859-5",
"cp866",
"866",
"ibm866",
"MacRoman",
];
pub fn check_html_charset(h: &mut crate::host::PhpHost, func: &str, encoding: Option<&Value>) {
let Some(v) = encoding.filter(|v| !matches!(v, Value::Undef)) else {
return;
};
let name = h.to_str(v);
if name.is_empty() || HTML_CHARSETS.iter().any(|c| c.eq_ignore_ascii_case(&name)) {
return;
}
h.warn(format_args!(
"{func}(): Charset \"{name}\" is not supported, assuming UTF-8"
));
}