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,
}
#[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()
}
}
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 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 hits = Vec::new();
while hits.len() < max_hits {
let pick = pick_next(next_ascii, next_le, next_be);
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),
_ => next_be = next_utf16_run(bytes, min, false, &mut pos_be),
}
}
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>) -> Option<(u8, Run)> {
let mut best: Option<(u8, Run)> = None;
for (order, cand) in [(0u8, ascii), (1u8, le), (2u8, be)] {
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
}
};
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)
}
#[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);
}
}