fix(hle): read manifest fields past inline XML comments
elements() took field text up to the next '<', so a generator comment before the value made TitleId, StoreId or SizeMB empty. Skip comments while gathering the text. Add a regression with comments inside all three fields. (cherry picked from commit ca362a42473bb0f8f9a83377091287a621e94527)
This commit is contained in:
1 parent
7a4f0018b5
commit
d23f0b83e1
1 file changed
+41
-2
@@ -125,6 +125,32 @@ impl Title {
|
||||
}
|
||||
}
|
||||
|
||||
/// The trimmed text of one element: comments inside it are skipped, not
|
||||
/// treated as the end of its value. A manifest generator can write
|
||||
/// `<TitleId><!-- generated -->639AA8DD</TitleId>`, which must still give
|
||||
/// the title's id rather than an empty field.
|
||||
fn field_text(rest: &str) -> String {
|
||||
let mut out = String::new();
|
||||
let mut rest = rest;
|
||||
loop {
|
||||
let Some(open) = rest.find('<') else {
|
||||
out.push_str(rest);
|
||||
break;
|
||||
};
|
||||
out.push_str(&rest[..open]);
|
||||
if rest[open..].starts_with("<!--") {
|
||||
let Some(end) = rest[open..].find("-->") else {
|
||||
break;
|
||||
};
|
||||
rest = &rest[open + end + 3..];
|
||||
} else {
|
||||
// A child element, a closing tag or anything else ends the text.
|
||||
break;
|
||||
}
|
||||
}
|
||||
out.trim().to_owned()
|
||||
}
|
||||
|
||||
/// Each element of an XML text as its path from the root (`Game/TitleId`)
|
||||
/// and its trimmed text up to the first child, the predefined entities
|
||||
/// decoded; comments, CDATA sections, declarations and attributes are
|
||||
@@ -161,10 +187,10 @@ fn elements(text: &str) -> Vec<(String, String)> {
|
||||
.next()
|
||||
.unwrap_or("");
|
||||
path.push(name);
|
||||
let body = rest[..rest.find('<').unwrap_or(rest.len())].trim();
|
||||
let body = field_text(rest);
|
||||
let body = [("<", "<"), (">", ">"), (""", "\""), ("'", "'"), ("&", "&")]
|
||||
.iter()
|
||||
.fold(body.to_owned(), |b, (entity, c)| b.replace(entity, c));
|
||||
.fold(body, |b, (entity, c)| b.replace(entity, c));
|
||||
out.push((path.join("/"), body));
|
||||
if closed {
|
||||
path.pop();
|
||||
@@ -260,4 +286,17 @@ mod tests {
|
||||
assert_eq!(t.store_id, "9&C");
|
||||
assert_eq!(Title::from_manifest("").storage_bytes, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_comments_do_not_truncate_a_field() {
|
||||
let text = "<Game>\
|
||||
<TitleId><!-- generated -->639AA8DD</TitleId>\
|
||||
<StoreId>9&<!-- keep -->C</StoreId>\
|
||||
<PersistentLocalStorage><SizeMB><!-- n -->1024</SizeMB></PersistentLocalStorage>\
|
||||
</Game>";
|
||||
let t = Title::from_manifest(text);
|
||||
assert_eq!(t.id, 0x639A_A8DD);
|
||||
assert_eq!(t.store_id, "9&C");
|
||||
assert_eq!(t.storage_bytes, 1024 << 20);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user