use serde::{Deserialize, Serialize};
use crate::error::{Error, Result};
pub const DEFAULT_MIN_LEN: usize = 4;
pub const DEFAULT_MAX_HITS: usize = 10_000;
pub const MAX_PATTERN_LEN: usize = 1024;
pub const REGEX_PREFIX: &str = "regex:";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Encoding {
Ascii,
Utf16Le,
Utf16Be,
Utf8,
Utf16LeBmp,
Base64,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct StringHit {
pub offset: usize,
pub vaddr: Option<u64>,
pub encoding: Encoding,
pub value: String,
#[serde(default)]
pub section: Option<String>,
#[serde(default)]
pub executable: bool,
}
impl Serialize for StringHit {
fn serialize<S: serde::Serializer>(
&self,
serializer: S,
) -> std::result::Result<S::Ok, S::Error> {
use serde::ser::SerializeStruct;
let mut st = serializer.serialize_struct("StringHit", 7)?;
st.serialize_field("offset", &self.offset)?;
st.serialize_field("vaddr", &self.vaddr)?;
st.serialize_field("vaddr_hex", &self.vaddr.map(crate::addr_hex))?;
st.serialize_field("encoding", &self.encoding)?;
st.serialize_field("value", &self.value)?;
st.serialize_field("section", &self.section)?;
st.serialize_field("executable", &self.executable)?;
st.end()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DecodeSpec {
Xor(u8),
XorMulti(Vec<u8>),
Add(u8),
Rot(u8),
Base64,
}
pub const MAX_XOR_KEY_LEN: usize = 32;
pub const MAX_BASE64_RUN_BYTES: usize = 64 * 1024;
impl DecodeSpec {
#[must_use]
pub fn apply(&self, bytes: &[u8]) -> Vec<u8> {
match self {
Self::Xor(key) => bytes.iter().map(|b| b ^ key).collect(),
Self::XorMulti(key) => {
if key.is_empty() {
return bytes.to_vec();
}
bytes
.iter()
.enumerate()
.map(|(i, b)| b ^ key[i % key.len()])
.collect()
}
Self::Add(key) => bytes.iter().map(|b| b.wrapping_add(*key)).collect(),
Self::Rot(n) => bytes.iter().map(|b| b.rotate_left(u32::from(*n))).collect(),
Self::Base64 => bytes.to_vec(),
}
}
}
pub fn parse_decode_spec(s: &str) -> Result<DecodeSpec> {
const HELP: &str = "expected one of xor:<byte>, xor:0x<HEXKEY> (4+ hex digits, rolling key, max 32 bytes), add:<byte>, rot:<0-7>, or base64 (byte is 0x-hex or decimal)";
if s.eq_ignore_ascii_case("base64") {
return Ok(DecodeSpec::Base64);
}
let (op, val) = s
.split_once(':')
.ok_or_else(|| Error::Invalid(format!("bad decode spec {s:?}: {HELP}")))?;
let bad = || Error::Invalid(format!("bad decode spec {s:?}: {HELP}"));
match op.to_lowercase().as_str() {
"xor" => parse_xor_spec(val).ok_or_else(bad),
"add" => {
let byte = parse_decode_byte(val).ok_or_else(bad)?;
Ok(DecodeSpec::Add(byte))
}
"rot" => {
let byte = parse_decode_byte(val).ok_or_else(bad)?;
if byte > 7 {
return Err(bad());
}
Ok(DecodeSpec::Rot(byte))
}
_ => Err(bad()),
}
}
fn parse_xor_spec(val: &str) -> Option<DecodeSpec> {
let t = val.trim();
if let Some(hex) = t.strip_prefix("0x").or_else(|| t.strip_prefix("0X")) {
if hex.is_empty() || hex.len() > MAX_XOR_KEY_LEN * 2 {
return None;
}
if !hex.bytes().all(|b| b.is_ascii_hexdigit()) {
return None;
}
if let Ok(v) = u64::from_str_radix(hex, 16)
&& let Ok(byte) = u8::try_from(v)
{
return Some(DecodeSpec::Xor(byte));
}
if hex.len() < 4 || hex.len() % 2 != 0 {
return None;
}
let mut key = Vec::with_capacity(hex.len() / 2);
for i in (0..hex.len()).step_by(2) {
key.push(u8::from_str_radix(&hex[i..i + 2], 16).ok()?);
}
return Some(DecodeSpec::XorMulti(key));
}
parse_decode_byte(t).map(DecodeSpec::Xor)
}
fn parse_decode_byte(s: &str) -> Option<u8> {
let s = s.trim();
let v = if let Some(hex) = s.strip_prefix("0x").or_else(|| s.strip_prefix("0X")) {
u64::from_str_radix(hex, 16).ok()?
} else {
s.parse::<u64>().ok()?
};
u8::try_from(v).ok()
}
pub fn matches_pattern(value: &str, pattern: &str) -> Result<bool> {
Ok(PatternMatcher::compile(pattern)?.is_match(value))
}
#[derive(Debug, Clone)]
pub enum PatternMatcher<'a> {
All,
Substring(&'a str),
Regex(regex::Regex),
}
impl<'a> PatternMatcher<'a> {
pub fn compile(pattern: &'a str) -> Result<Self> {
if pattern.len() > MAX_PATTERN_LEN {
return Err(Error::Invalid(format!(
"pattern exceeds {MAX_PATTERN_LEN} bytes"
)));
}
if pattern.is_empty() {
return Ok(Self::All);
}
match pattern.strip_prefix(REGEX_PREFIX) {
Some(expr) => regex::Regex::new(expr)
.map(Self::Regex)
.map_err(|e| Error::Invalid(format!("bad regex pattern {pattern:?}: {e}"))),
None => Ok(Self::Substring(pattern)),
}
}
#[must_use]
pub fn is_match(&self, value: &str) -> bool {
match self {
Self::All => true,
Self::Substring(needle) => value.contains(needle),
Self::Regex(re) => re.is_match(value),
}
}
}
#[must_use]
pub fn extract(
bytes: &[u8],
min_len: usize,
max_hits: usize,
base_va: Option<u64>,
) -> Vec<StringHit> {
if max_hits == 0 {
return Vec::new();
}
let min = min_len.max(1);
let mut pos_ascii = 0usize;
let mut pos_le = 0usize;
let mut pos_be = 0usize;
let mut pos_utf8 = 0usize;
let mut pos_bmp = 0usize;
let mut next_ascii = next_ascii_run(bytes, min, &mut pos_ascii);
let mut next_le = next_utf16_run(bytes, min, true, &mut pos_le);
let mut next_be = next_utf16_run(bytes, min, false, &mut pos_be);
let mut next_utf8 = next_utf8_run(bytes, min, &mut pos_utf8);
let mut next_bmp = next_utf16_bmp_run(bytes, min, &mut pos_bmp);
let mut hits = Vec::new();
while hits.len() < max_hits {
let pick = pick_next(next_ascii, next_le, next_be, next_utf8, next_bmp);
let Some((order, run)) = pick else {
break;
};
hits.push(decode_run(bytes, run, base_va));
match order {
0 => next_ascii = next_ascii_run(bytes, min, &mut pos_ascii),
1 => next_le = next_utf16_run(bytes, min, true, &mut pos_le),
2 => next_be = next_utf16_run(bytes, min, false, &mut pos_be),
3 => next_utf8 = next_utf8_run(bytes, min, &mut pos_utf8),
_ => next_bmp = next_utf16_bmp_run(bytes, min, &mut pos_bmp),
}
}
suppress_bmp_shadows(bytes, &mut hits);
hits
}
#[must_use]
pub fn extract_page(
bytes: &[u8],
min_len: usize,
max_hits: usize,
base_va: Option<u64>,
offset: usize,
limit: usize,
) -> Vec<StringHit> {
extract(bytes, min_len, max_hits, base_va)
.into_iter()
.skip(offset)
.take(limit)
.collect()
}
fn is_print(b: u8) -> bool {
matches!(b, 0x20..=0x7E)
}
fn va(base_va: Option<u64>, offset: usize) -> Option<u64> {
base_va.map(|b| b.wrapping_add(offset as u64))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Run {
offset: usize,
end: usize,
encoding: Encoding,
}
fn next_ascii_run(bytes: &[u8], min: usize, pos: &mut usize) -> Option<Run> {
while *pos < bytes.len() {
if is_print(bytes[*pos]) {
let start = *pos;
while *pos < bytes.len() && is_print(bytes[*pos]) {
*pos += 1;
}
if *pos - start >= min {
return Some(Run {
offset: start,
end: *pos,
encoding: Encoding::Ascii,
});
}
} else {
*pos += 1;
}
}
None
}
fn next_utf16_run(bytes: &[u8], min: usize, little: bool, pos: &mut usize) -> Option<Run> {
let encoding = if little {
Encoding::Utf16Le
} else {
Encoding::Utf16Be
};
while *pos + 1 < bytes.len() {
if unit_at(bytes, *pos, little).is_some() {
let start = *pos;
let mut count = 0usize;
while unit_at(bytes, *pos, little).is_some() {
count += 1;
*pos += 2;
}
if count >= min {
return Some(Run {
offset: start,
end: *pos,
encoding,
});
}
} else {
*pos += 1;
}
}
None
}
fn pick_next(
ascii: Option<Run>,
le: Option<Run>,
be: Option<Run>,
utf8: Option<Run>,
bmp: Option<Run>,
) -> Option<(u8, Run)> {
let mut best: Option<(u8, Run)> = None;
for (order, cand) in [(0u8, ascii), (1u8, le), (2u8, be), (3u8, utf8), (4u8, bmp)] {
let Some(run) = cand else {
continue;
};
let replace = match best {
None => true,
Some((best_order, best_run)) => (run.offset, order) < (best_run.offset, best_order),
};
if replace {
best = Some((order, run));
}
}
best
}
fn decode_run(bytes: &[u8], run: Run, base_va: Option<u64>) -> StringHit {
let value = match run.encoding {
Encoding::Ascii => String::from_utf8_lossy(&bytes[run.offset..run.end]).into_owned(),
Encoding::Utf16Le => {
let mut value = String::with_capacity((run.end - run.offset) / 2);
let mut i = run.offset;
while i < run.end {
if let Some(c) = unit_at(bytes, i, true) {
value.push(c as char);
i += 2;
} else {
break;
}
}
value
}
Encoding::Utf16Be => {
let mut value = String::with_capacity((run.end - run.offset) / 2);
let mut i = run.offset;
while i < run.end {
if let Some(c) = unit_at(bytes, i, false) {
value.push(c as char);
i += 2;
} else {
break;
}
}
value
}
Encoding::Utf8 => std::str::from_utf8(&bytes[run.offset..run.end])
.expect("utf8 runs hold validated bytes")
.to_owned(),
Encoding::Utf16LeBmp => {
let mut value = String::new();
let mut i = run.offset;
while i < run.end {
if let Some((c, _)) = bmp_unit_at(bytes, i) {
value.push(c);
i += 2;
} else {
break;
}
}
value
}
Encoding::Base64 => String::from_utf8_lossy(&bytes[run.offset..run.end]).into_owned(),
};
StringHit {
offset: run.offset,
vaddr: va(base_va, run.offset),
encoding: run.encoding,
value,
section: None,
executable: false,
}
}
fn unit_at(bytes: &[u8], i: usize, little: bool) -> Option<u8> {
let pair = bytes.get(i..i + 2)?;
let (c, z) = if little {
(pair[0], pair[1])
} else {
(pair[1], pair[0])
};
(is_print(c) && z == 0).then_some(c)
}
fn bmp_unit_at(bytes: &[u8], i: usize) -> Option<(char, u8)> {
let pair = bytes.get(i..i + 2)?;
let u = u16::from_le_bytes([pair[0], pair[1]]);
if u < 0x80 {
return None;
}
let script = bmp_letter_script(u)?;
let c = char::from_u32(u32::from(u))?;
c.is_alphabetic().then_some((c, script))
}
#[must_use]
pub fn bmp_letter_script(u: u16) -> Option<u8> {
Some(match u {
0x00C0..=0x024F | 0x1E00..=0x1EFF | 0xFF21..=0xFF5A | 0xFF41..=0xFF7A => 1,
0x0370..=0x03FF => 2,
0x0400..=0x052F => 3,
0x0531..=0x0586 => 4,
0x0590..=0x05FF => 5,
0x0600..=0x06FF => 6,
0x0900..=0x097F => 7,
0x0E00..=0x0E7F => 8,
0x10A0..=0x10FF => 9,
0x1100..=0x11FF | 0xAC00..=0xD7AF => 10,
0x3040..=0x309F => 11,
0x30A0..=0x30FF => 12,
0x3100..=0x312F => 13,
0x3400..=0x4DBF | 0x4E00..=0x9FFF | 0xF900..=0xFAFF => 14,
_ => return None,
})
}
pub const MAX_BMP_RUN_CHARS: usize = 1024;
pub const BMP_DIVERSITY_LEN: usize = 64;
pub const BMP_DIVERSITY_MIN: usize = 8;
pub fn suppress_bmp_shadows(bytes: &[u8], hits: &mut Vec<StringHit>) {
if !hits.iter().any(|h| h.encoding == Encoding::Utf16LeBmp) {
return;
}
let ascii: Vec<(usize, usize)> = hits
.iter()
.filter(|h| h.encoding == Encoding::Ascii)
.map(|h| (h.offset, h.offset + h.value.len()))
.collect();
if ascii.is_empty() {
return;
}
hits.retain(|h| {
if h.encoding != Encoding::Utf16LeBmp {
return true;
}
let span = h.value.chars().count() * 2;
!(0..span).all(|d| {
let o = h.offset + d;
!bytes.get(o).is_some_and(|b| is_print(*b))
|| ascii.iter().any(|&(a, b)| a <= o && o < b)
})
});
}
#[must_use]
pub fn bmp_distinct_units(bytes: &[u8], start: usize, end: usize) -> usize {
let mut seen: [u16; BMP_DIVERSITY_MIN] = [0; BMP_DIVERSITY_MIN];
let mut count = 0usize;
let mut i = start;
while i + 1 < end {
let u = u16::from_le_bytes([bytes[i], bytes[i + 1]]);
if !seen[..count].contains(&u) {
if count >= BMP_DIVERSITY_MIN {
break;
}
seen[count] = u;
count += 1;
}
i += 2;
}
count
}
fn next_utf16_bmp_run(bytes: &[u8], min: usize, pos: &mut usize) -> Option<Run> {
while *pos + 1 < bytes.len() {
if let Some((_, script)) = bmp_unit_at(bytes, *pos) {
let start = *pos;
let mut count = 0usize;
while count < MAX_BMP_RUN_CHARS
&& let Some((_, s)) = bmp_unit_at(bytes, *pos)
{
if s != script {
break;
}
count += 1;
*pos += 2;
}
let degenerate = count > BMP_DIVERSITY_LEN
&& bmp_distinct_units(bytes, start, *pos) < BMP_DIVERSITY_MIN;
if count >= min && !degenerate {
return Some(Run {
offset: start,
end: *pos,
encoding: Encoding::Utf16LeBmp,
});
}
} else {
*pos += 1;
}
}
None
}
fn utf8_char_at(bytes: &[u8], i: usize) -> Option<(char, usize)> {
let b = *bytes.get(i)?;
if is_print(b) {
return Some((b as char, 1));
}
if b < 0x80 {
return None;
}
for len in 2..=4usize {
let Some(sl) = bytes.get(i..i + len) else {
continue;
};
let Ok(s) = std::str::from_utf8(sl) else {
continue;
};
let mut chars = s.chars();
match (chars.next(), chars.next()) {
(Some(c), None) if !c.is_control() && c != '\u{FEFF}' => return Some((c, len)),
(Some(_), None) => return None,
_ => continue,
}
}
None
}
fn next_utf8_run(bytes: &[u8], min: usize, pos: &mut usize) -> Option<Run> {
while *pos < bytes.len() {
if utf8_char_at(bytes, *pos).is_some() {
let start = *pos;
let mut count = 0usize;
let mut multi = false;
while let Some((_, len)) = utf8_char_at(bytes, *pos) {
count += 1;
multi |= len > 1;
*pos += len;
}
if count >= min && multi {
return Some(Run {
offset: start,
end: *pos,
encoding: Encoding::Utf8,
});
}
} else {
*pos += 1;
}
}
None
}
#[must_use]
pub fn is_base64_candidate(s: &str) -> bool {
if s.len() < 8 || !s.len().is_multiple_of(4) {
return false;
}
let pad = s.len() - s.trim_end_matches('=').len();
if pad > 2 {
return false;
}
let body = &s[..s.len() - pad];
if body.is_empty() {
return false;
}
body.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'+' || b == b'/')
}
#[must_use]
pub fn decode_base64(s: &str) -> Option<Vec<u8>> {
fn val(b: u8) -> Option<u32> {
match b {
b'A'..=b'Z' => Some(u32::from(b - b'A')),
b'a'..=b'z' => Some(u32::from(b - b'a' + 26)),
b'0'..=b'9' => Some(u32::from(b - b'0' + 52)),
b'+' => Some(62),
b'/' => Some(63),
_ => None,
}
}
if !is_base64_candidate(s) {
return None;
}
let raw = s.as_bytes();
let pad = raw.len() - s.trim_end_matches('=').len();
let mut out = Vec::with_capacity(raw.len() / 4 * 3);
let chunks = raw.len() / 4;
for (qi, q) in raw.chunks(4).enumerate() {
let q: [u8; 4] = q.try_into().ok()?;
let last = qi + 1 == chunks;
let n = q
.iter()
.enumerate()
.map(|(i, &b)| {
if b == b'=' {
(last && i >= 4 - pad).then_some(0)
} else {
val(b)
}
})
.collect::<Option<Vec<_>>>()?;
let v = (n[0] << 18) | (n[1] << 12) | (n[2] << 6) | n[3];
out.push((v >> 16) as u8);
if !(last && pad >= 2) {
out.push((v >> 8) as u8);
}
if !(last && pad >= 1) {
out.push(v as u8);
}
}
Some(out)
}
fn is_decoded_printable(text: &str) -> bool {
text.chars().all(|c| {
if c.is_ascii() {
is_print(c as u8)
} else {
!c.is_control() && c != '\u{FEFF}'
}
})
}
#[must_use]
pub fn extract_base64(
bytes: &[u8],
min_len: usize,
max_hits: usize,
base_va: Option<u64>,
) -> Vec<StringHit> {
if max_hits == 0 {
return Vec::new();
}
let min = min_len.max(1);
let mut hits = Vec::new();
let mut pos = 0usize;
while pos < bytes.len() && hits.len() < max_hits {
if !is_print(bytes[pos]) {
pos += 1;
continue;
}
let start = pos;
while pos < bytes.len() && is_print(bytes[pos]) {
pos += 1;
}
let run = &bytes[start..pos];
if run.len() < 8 || run.len() > MAX_BASE64_RUN_BYTES {
continue;
}
let Ok(ascii) = std::str::from_utf8(run) else {
continue;
};
if !is_base64_candidate(ascii) {
continue;
}
let Some(decoded) = decode_base64(ascii) else {
continue;
};
let Ok(text) = String::from_utf8(decoded) else {
continue;
};
if text.chars().count() < min || !is_decoded_printable(&text) {
continue;
}
hits.push(StringHit {
offset: start,
vaddr: va(base_va, start),
encoding: Encoding::Base64,
value: text,
section: None,
executable: false,
});
}
hits
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ascii_runs_need_min_len() {
let hits = extract(b"\x00hi\x00hello\x00", 4, 100, None);
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].value, "hello");
assert_eq!(hits[0].offset, 4);
assert_eq!(hits[0].encoding, Encoding::Ascii);
assert_eq!(hits[0].vaddr, None);
}
#[test]
fn utf16_both_endians_detected() {
let bytes = [0x41, 0x00, 0x42, 0x00, 0xFF, 0xFF, 0x00, 0x43, 0x00, 0x44];
let hits = extract(&bytes, 2, 100, Some(0x1000));
assert_eq!(
hits,
[
StringHit {
offset: 0,
vaddr: Some(0x1000),
encoding: Encoding::Utf16Le,
value: "AB".to_string(),
section: None,
executable: false,
},
StringHit {
offset: 6,
vaddr: Some(0x1006),
encoding: Encoding::Utf16Be,
value: "CD".to_string(),
section: None,
executable: false,
},
]
);
}
#[test]
fn paging_slices_hits() {
let mut bytes = Vec::new();
for i in 0..10 {
bytes.extend_from_slice(format!("s{i:03}\x00").as_bytes());
}
let page = extract_page(&bytes, 4, 100, None, 3, 4);
assert_eq!(
page.iter().map(|h| h.value.as_str()).collect::<Vec<_>>(),
["s003", "s004", "s005", "s006"]
);
let capped = extract(&bytes, 4, 3, None);
assert_eq!(capped.len(), 3);
}
#[test]
fn pattern_matches_substring_by_default() {
assert!(matches_pattern("SAVE%d.GAM is invalid", "SAVE").unwrap());
assert!(matches_pattern("SAVE%d.GAM is invalid", "GAM is").unwrap());
assert!(!matches_pattern("SAVE%d.GAM is invalid", "save").unwrap());
assert!(!matches_pattern("SAVE%d.GAM is invalid", "zzz").unwrap());
assert!(matches_pattern("anything", "").unwrap());
assert!(matches_pattern("regex:SAVE", "SAVE").unwrap());
}
#[test]
fn pattern_regex_prefix_opts_in() {
assert!(matches_pattern("SAVE%d.GAM is invalid", "regex:SAVE.*invalid").unwrap());
assert!(matches_pattern("SAVE%d.GAM is invalid", "regex:(?i)save").unwrap());
assert!(!matches_pattern("SAVE%d.GAM is invalid", "regex:^invalid").unwrap());
}
#[test]
fn pattern_bad_regex_and_overlong_are_errors() {
assert!(matches_pattern("x", "regex:([").is_err());
assert!(matches_pattern("x", &"y".repeat(MAX_PATTERN_LEN + 1)).is_err());
assert!(matches_pattern("x", &"y".repeat(MAX_PATTERN_LEN)).is_ok());
}
#[test]
fn matcher_agrees_with_matches_pattern() {
let values = [
"SAVE%d.GAM is invalid",
"save game",
"",
"regex:SAVE",
"zzz",
];
let patterns = [
"SAVE",
"save",
"zzz",
"",
"regex:SAVE.*invalid",
"regex:(?i)save",
"regex:^invalid",
"regex:([",
];
for pattern in patterns {
let single: Vec<Result<bool>> =
values.iter().map(|v| matches_pattern(v, pattern)).collect();
let reused = PatternMatcher::compile(pattern);
match (single, reused) {
(results, Ok(matcher)) => {
for (value, expected) in values.iter().zip(results) {
assert_eq!(
matcher.is_match(value),
expected.expect("compiled pattern must match per-row"),
"value={value:?} pattern={pattern:?}"
);
}
}
(results, Err(_)) => {
assert!(
results.iter().all(|r| r.is_err()),
"failed compile must fail every row: {pattern:?}"
);
}
}
}
let overlong = "y".repeat(MAX_PATTERN_LEN + 1);
assert!(PatternMatcher::compile(&overlong).is_err());
}
#[test]
fn matcher_modes_match_everything_or_substring() {
assert!(
PatternMatcher::compile("")
.expect("empty compiles")
.is_match("anything")
);
let sub = PatternMatcher::compile("SAVE").expect("substring compiles");
assert!(sub.is_match("SAVE%d.GAM is invalid"));
assert!(!sub.is_match("save game"));
let re = PatternMatcher::compile("regex:SAVE.*invalid").expect("regex compiles");
assert!(re.is_match("SAVE%d.GAM is invalid"));
assert!(!re.is_match("save game"));
}
#[test]
fn old_rows_decode_without_new_fields() {
let hit: StringHit =
serde_json::from_str(r#"{"offset":1,"vaddr":4096,"encoding":"Ascii","value":"hi"}"#)
.expect("old rows must still decode");
assert_eq!(hit.section, None);
assert!(!hit.executable);
}
#[test]
fn decode_specs_parse_hex_and_decimal() {
assert_eq!(
parse_decode_spec("xor:0x41").expect("hex parses"),
DecodeSpec::Xor(0x41)
);
assert_eq!(
parse_decode_spec("XOR:65").expect("op is case-insensitive"),
DecodeSpec::Xor(65)
);
assert_eq!(
parse_decode_spec("xor:0x0041").expect("zero-padded stays single"),
DecodeSpec::Xor(0x41)
);
assert_eq!(
parse_decode_spec("xor:0x4142").expect("rolling key"),
DecodeSpec::XorMulti(vec![0x41, 0x42])
);
assert_eq!(
parse_decode_spec("add:7").expect("add parses"),
DecodeSpec::Add(7)
);
assert_eq!(
parse_decode_spec("rot:3").expect("rot parses"),
DecodeSpec::Rot(3)
);
assert_eq!(
parse_decode_spec("base64").expect("bare base64"),
DecodeSpec::Base64
);
assert_eq!(
parse_decode_spec("BASE64").expect("base64 case-insensitive"),
DecodeSpec::Base64
);
}
#[test]
fn decode_specs_reject_everything_off_allowlist() {
for bad in [
"",
"xor",
"aes:0x41",
"xor:",
"xor:0x",
"xor:0x100",
"xor:0x123",
"xor:0x414",
"xor:0xZZZZ",
"xor:256",
"xor:-1",
"xor:zz",
"add:999",
"rot:8",
"rot:0x41",
"rotl:3",
"xor:0x41:extra",
"base64:",
"base64:x",
] {
assert!(
parse_decode_spec(bad).is_err(),
"{bad:?} must be rejected, never a silent fallback"
);
}
let huge = format!("xor:0x{}", "AB".repeat(MAX_XOR_KEY_LEN + 1));
assert!(parse_decode_spec(&huge).is_err());
let maxed = format!("xor:0x{}", "AB".repeat(MAX_XOR_KEY_LEN));
assert!(matches!(
parse_decode_spec(&maxed).expect("max key parses"),
DecodeSpec::XorMulti(k) if k.len() == MAX_XOR_KEY_LEN
));
}
#[test]
fn xor_multi_rolls_the_key() {
let spec = DecodeSpec::XorMulti(vec![0x41, 0x42]);
assert_eq!(spec.apply(b"...."), b"olol");
assert_eq!(spec.apply(b".."), b"ol");
assert_eq!(spec.apply(b"....."), b"ololo");
assert_eq!(
DecodeSpec::Xor(0x41).apply(b"...."),
DecodeSpec::XorMulti(vec![0x41]).apply(b"....")
);
}
#[test]
fn base64_candidate_and_decode_vectors() {
for (blob, plain) in [
("TWFuTWFu", "ManMan"),
("cGxlYXN1cmUu", "pleasure."),
("aGVsbG8td29ybGQ=", "hello-world"),
] {
assert!(is_base64_candidate(blob), "{blob:?} must be a candidate");
assert_eq!(
decode_base64(blob).expect("decodes"),
plain.as_bytes(),
"{blob:?}"
);
}
for bad in [
"",
"abc",
"AAAAAAAAA",
"AB=CDEFGH",
"A=======",
"========采采采采",
"a b c d e f g h",
"xy",
] {
assert!(!is_base64_candidate(bad), "{bad:?} must not be a candidate");
assert_eq!(decode_base64(bad), None);
}
assert_eq!(decode_base64("AAAAAAAA").expect("decodes"), vec![0u8; 6]);
}
#[test]
fn utf8_runs_need_multibyte_printable_text() {
let mut bytes = b"\x00hello ".to_vec();
bytes.extend_from_slice("世界".as_bytes());
bytes.push(0x00);
let hits = extract(&bytes, 4, 100, None);
let utf8: Vec<&StringHit> = hits
.iter()
.filter(|h| h.encoding == Encoding::Utf8)
.collect();
assert_eq!(utf8.len(), 1);
assert_eq!(utf8[0].value, "hello 世界");
assert_eq!(utf8[0].offset, 1);
let hits = extract(b"\x00hello world\x00", 4, 100, None);
assert!(hits.iter().all(|h| h.encoding == Encoding::Ascii));
for bad in [
vec![0x00, 0x80, 0x80, 0x80, 0x80, 0x00],
vec![0x00, 0xC0, 0xAF, 0xC0, 0xAF, 0xC0, 0xAF, 0xC0, 0xAF, 0x00],
vec![0x00, 0xC2, 0x80, 0xC2, 0x80, 0xC2, 0x80, 0xC2, 0x80, 0x00],
vec![0x00, 0xED, 0xA0, 0x80, 0xED, 0xA0, 0x80, 0x00],
] {
assert!(
extract(&bad, 2, 100, None)
.iter()
.all(|h| h.encoding != Encoding::Utf8),
"no UTF-8 hit expected in {bad:02x?}"
);
}
}
#[test]
fn bmp_letter_script_table_spot_checks() {
assert_eq!(bmp_letter_script(0x0430), Some(3));
assert_eq!(bmp_letter_script(0x4E16), Some(14));
assert_eq!(bmp_letter_script(0xAC00), Some(10));
assert_eq!(bmp_letter_script(0x3042), Some(11));
assert_eq!(bmp_letter_script(0x2D65), None);
assert_eq!(bmp_letter_script(0xD800), None);
assert_eq!(bmp_letter_script(0x0041), None);
assert_eq!(bmp_letter_script(0xFFFF), None);
}
#[test]
fn bmp_runs_are_script_consistent() {
let cyrillic: Vec<u8> = vec![
0x1F, 0x04, 0x40, 0x04, 0x38, 0x04, 0x32, 0x04, 0x35, 0x04, 0x42, 0x04,
];
let hits = extract(&cyrillic, 4, 100, None);
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].encoding, Encoding::Utf16LeBmp);
assert_eq!(hits[0].value, "Привет");
let mut mixed = Vec::new();
for _ in 0..6 {
mixed.extend_from_slice(&[0x16, 0x4E, 0x65, 0x2D]);
}
assert!(
extract(&mixed, 2, 100, None)
.iter()
.all(|h| h.encoding != Encoding::Utf16LeBmp),
"mixed-script units must not chain"
);
}
#[test]
fn bmp_long_runs_chunk_and_degenerate_rejects() {
let mut units: Vec<u8> = Vec::new();
for i in 0..1100usize {
let u: u16 = 0x4E00 + (i % 10) as u16;
units.extend_from_slice(&u.to_le_bytes());
}
let hits = extract(&units, 4, 100, None);
assert_eq!(hits.len(), 2);
assert_eq!(hits[0].value.chars().count(), MAX_BMP_RUN_CHARS);
assert_eq!(hits[0].offset, 0);
assert_eq!(hits[1].offset, MAX_BMP_RUN_CHARS * 2);
assert_eq!(hits[1].value.chars().count(), 76);
let mut fill = Vec::new();
for i in 0..300usize {
let u: u16 = if i % 2 == 0 { 0x4E16 } else { 0x754C };
fill.extend_from_slice(&u.to_le_bytes());
}
assert!(
extract(&fill, 4, 100, None)
.iter()
.all(|h| h.encoding != Encoding::Utf16LeBmp),
"degenerate periodic runs must not report"
);
}
#[test]
fn decode_round_trips_reveal_printable_runs() {
fn ascii_values(hits: &[StringHit]) -> Vec<&str> {
hits.iter()
.filter(|h| h.encoding == Encoding::Ascii)
.map(|h| h.value.as_str())
.collect()
}
let plain = b"hidden-marker";
let xor: Vec<u8> = plain.iter().map(|b| b ^ 0x41).collect();
let decoded = DecodeSpec::Xor(0x41).apply(&xor);
assert_eq!(decoded, plain);
let hits = extract(&decoded, 4, 100, Some(0x1000));
assert_eq!(ascii_values(&hits), ["hidden-marker"]);
let marker = hits
.iter()
.find(|h| h.encoding == Encoding::Ascii)
.expect("ascii marker");
assert_eq!(marker.offset, 0);
assert_eq!(marker.vaddr, Some(0x1000));
let added: Vec<u8> = plain.iter().map(|b| b.wrapping_sub(7)).collect();
let hits = extract(&DecodeSpec::Add(7).apply(&added), 4, 100, None);
assert_eq!(ascii_values(&hits), ["hidden-marker"]);
let rord: Vec<u8> = plain.iter().map(|b| b.rotate_right(3)).collect();
let hits = extract(&DecodeSpec::Rot(3).apply(&rord), 4, 100, None);
assert_eq!(ascii_values(&hits), ["hidden-marker"]);
}
}