use crate::stdlib::common::*;
use base64::engine::general_purpose::STANDARD;
use base64::Engine;
use fusevm::Value;
fn bytes_to_str(bytes: &[u8]) -> String {
bytes.iter().map(|&b| b as char).collect()
}
fn base64_reverse(b: u8) -> i16 {
match b {
b'A'..=b'Z' => (b - b'A') as i16,
b'a'..=b'z' => (b - b'a') as i16 + 26,
b'0'..=b'9' => (b - b'0') as i16 + 52,
b'+' => 62,
b'/' => 63,
b'\t' | b'\n' | b'\r' | b' ' => -1,
_ => -2,
}
}
fn base64_decode_ex(input: &[u8], strict: bool) -> Option<Vec<u8>> {
let mut result = Vec::with_capacity(input.len());
let mut i = 0i32; let mut padding = 0i32;
let mut cur = 0u8;
for &raw in input {
if raw == b'=' {
padding += 1;
continue;
}
let ch = base64_reverse(raw);
if !strict {
if ch < 0 {
continue;
}
} else {
if ch == -1 {
continue;
}
if ch == -2 || padding != 0 {
return None;
}
}
let ch = ch as u8;
match i % 4 {
0 => cur = ch << 2,
1 => {
result.push(cur | (ch >> 4));
cur = (ch & 0x0f) << 4;
}
2 => {
result.push(cur | (ch >> 2));
cur = (ch & 0x03) << 6;
}
3 => result.push(cur | ch),
_ => unreachable!(),
}
i += 1;
}
if strict {
if i % 4 == 1 {
return None;
}
if padding != 0 && (padding > 2 || (i + padding) % 4 != 0) {
return None;
}
}
Some(result)
}
fn qp_encode(input: &[u8]) -> Vec<u8> {
const HEX: &[u8; 16] = b"0123456789ABCDEF";
const MAXL: i32 = 75;
let mut out = Vec::with_capacity(input.len());
let mut lp = 0i32;
let n = input.len();
let mut i = 0;
while i < n {
let c = input[i];
if c == 0x0d && i + 1 < n && input[i + 1] == 0x0a {
out.push(0x0d);
out.push(0x0a);
lp = 0;
i += 2;
continue;
}
let next = if i + 1 < n { input[i + 1] } else { 0 };
let is_cntrl = c < 0x20 || c == 0x7f;
if is_cntrl || (c & 0x80) != 0 || c == b'=' || (c == b' ' && next == 0x0d) {
lp += 3;
if lp > MAXL && c <= 0x7f {
out.push(b'=');
out.push(0x0d);
out.push(0x0a);
lp = 3;
}
out.push(b'=');
out.push(HEX[(c >> 4) as usize]);
out.push(HEX[(c & 0x0f) as usize]);
} else {
lp += 1;
if lp > MAXL {
out.push(b'=');
out.push(0x0d);
out.push(0x0a);
lp = 1;
}
out.push(c);
}
i += 1;
}
out
}
fn hex_val(b: u8) -> Option<u8> {
match b {
b'0'..=b'9' => Some(b - b'0'),
b'a'..=b'f' => Some(b - b'a' + 10),
b'A'..=b'F' => Some(b - b'A' + 10),
_ => None,
}
}
fn qp_decode(input: &[u8]) -> Vec<u8> {
let mut out = Vec::with_capacity(input.len());
let n = input.len();
let mut i = 0;
while i < n {
let c = input[i];
if c == b'=' && i + 1 >= n {
break;
}
if c == b'=' && i + 1 < n {
let n1 = input[i + 1];
if n1 == b'\r' {
i += 2;
if i < n && input[i] == b'\n' {
i += 1;
}
continue;
} else if n1 == b'\n' {
i += 2;
continue;
} else if i + 2 < n {
if let (Some(h), Some(l)) = (hex_val(n1), hex_val(input[i + 2])) {
out.push((h << 4) | l);
i += 3;
continue;
}
}
}
out.push(c);
i += 1;
}
out
}
fn uu_enc(c: i32) -> u8 {
let v = c & 0x3f;
if v != 0 {
(v + 0x20) as u8
} else {
b'`'
}
}
fn uuencode(src: &[u8]) -> Vec<u8> {
let n = src.len();
let mut out = Vec::new();
let mut i = 0;
while i < n {
let line_len = std::cmp::min(45, n - i);
out.push(uu_enc(line_len as i32));
let mut j = 0;
while j < line_len {
let b0 = src[i + j] as i32;
let b1 = if j + 1 < line_len {
src[i + j + 1] as i32
} else {
0
};
let b2 = if j + 2 < line_len {
src[i + j + 2] as i32
} else {
0
};
out.push(uu_enc(b0 >> 2));
out.push(uu_enc(((b0 << 4) & 0x30) | ((b1 >> 4) & 0x0f)));
out.push(uu_enc(((b1 << 2) & 0x3c) | ((b2 >> 6) & 0x03)));
out.push(uu_enc(b2 & 0x3f));
j += 3;
}
out.push(b'\n');
i += line_len;
}
out.push(b'`');
out.push(b'\n');
out
}
fn uu_dec(c: u8) -> i32 {
(c as i32 - 0x20) & 0x3f
}
fn uudecode(src: &[u8]) -> Option<Vec<u8>> {
let src_len = src.len();
if src_len == 0 {
return None;
}
let n = src_len;
let mut out = Vec::new();
let mut total_len: usize = 0;
let mut i = 0;
while i < n {
let line_len = uu_dec(src[i]) as usize;
i += 1;
if line_len == 0 {
break;
}
if line_len > src_len {
return None;
}
total_len += line_len;
let span = if line_len == 45 {
60
} else {
(line_len as f64 * 1.37).floor() as usize
};
if i + span > n {
return None;
}
let mut remaining = line_len;
while remaining > 0 {
let d0 = uu_dec(*src.get(i).unwrap_or(&0));
let d1 = uu_dec(*src.get(i + 1).unwrap_or(&0));
let d2 = uu_dec(*src.get(i + 2).unwrap_or(&0));
let d3 = uu_dec(*src.get(i + 3).unwrap_or(&0));
if remaining > 0 {
out.push(((d0 << 2) | (d1 >> 4)) as u8);
remaining -= 1;
}
if remaining > 0 {
out.push(((d1 << 4) | (d2 >> 2)) as u8);
remaining -= 1;
}
if remaining > 0 {
out.push(((d2 << 6) | d3) as u8);
remaining -= 1;
}
i += 4;
}
if i < n && (src[i] == b'\r' || src[i] == b'\n') {
i += 1;
}
}
if out.is_empty() {
return None;
}
if out.len() > total_len {
out.truncate(total_len);
}
Some(out)
}
pub fn dispatch(name: &str, args: &[Value]) -> Option<Result<Value, String>> {
let strict = || args.len() > 1 && crate::host::with_host(|h| h.is_truthy(&arg(args, 1)));
let v = match name {
"base64_encode" => Value::str(STANDARD.encode(str_arg(args, 0).as_bytes())),
"base64_decode" => match base64_decode_ex(str_arg(args, 0).as_bytes(), strict()) {
Some(bytes) => Value::str(bytes_to_str(&bytes)),
None => Value::bool(false),
},
"hex2bin" => {
let s = str_arg(args, 0);
match hex::decode(s.as_bytes()) {
Ok(bytes) => Value::str(bytes_to_str(&bytes)),
Err(_) => Value::bool(false),
}
}
"quoted_printable_encode" => {
Value::str(bytes_to_str(&qp_encode(str_arg(args, 0).as_bytes())))
}
"quoted_printable_decode" => {
Value::str(bytes_to_str(&qp_decode(str_arg(args, 0).as_bytes())))
}
"convert_uuencode" => Value::str(bytes_to_str(&uuencode(str_arg(args, 0).as_bytes()))),
"convert_uudecode" => match uudecode(str_arg(args, 0).as_bytes()) {
Some(bytes) => Value::str(bytes_to_str(&bytes)),
None => Value::bool(false),
},
"utf8_encode" => Value::str(
str_arg(args, 0)
.as_bytes()
.iter()
.map(|&b| b as char)
.collect::<String>(),
),
"utf8_decode" => {
let s = str_arg(args, 0);
let bytes: Vec<u8> = s
.chars()
.map(|c| {
let cp = c as u32;
if cp <= 0xff {
cp as u8
} else {
b'?'
}
})
.collect();
Value::str(bytes_to_str(&bytes))
}
_ => return None,
};
Some(Ok(v))
}