android-res: bound retained values and report UTF-8 recovery omissions

This commit is contained in:
FredApps committed 2026-11-23 09:39:20 +01:00
1 parent eeb5b26198
commit 042dccd075
2 files changed
+326 -39

No files matched your search

+176 -20
View File
@@ -3,7 +3,7 @@
//! orientation, SDK level…). Simple and complex (bag) entries, sparse and 16-bit offset tables.
use crate::Error;
use crate::axml::{ResValue, StringPool, chunk};
use crate::axml::{MAX_RETAINED_VALUE_BYTES, ResValue, StringPool, ValueBudget, chunk, report_partial_string_pool};
use crate::bytes::Reader;
use serde::Serialize;
@@ -21,14 +21,18 @@ const MAX_BAG: usize = 4096;
pub struct Limits {
/// Most entries decoded across all types and configurations.
pub max_entries: usize,
/// Most bytes retained in formatted simple-entry and bag values.
pub max_value_bytes: usize,
}
impl Default for Limits {
fn default() -> Self {
Limits { max_entries: 2_000_000 }
Limits { max_entries: 2_000_000, max_value_bytes: MAX_RETAINED_VALUE_BYTES }
}
}
const VALUE_BUDGET_WARNING: &str = "resource value budget reached; remaining values were omitted";
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct TypeInfo {
pub id: u8,
@@ -300,6 +304,7 @@ pub fn parse(data: &[u8], limits: Limits) -> Result<Table, Error> {
let r = Reader::new(data);
let mut table = Table::default();
let mut global = StringPool::default();
let mut value_budget = ValueBudget::new(limits.max_value_bytes);
let mut pos = h;
while pos < size {
let Some((ct, _ch, cs)) = chunk(&r, pos) else {
@@ -310,10 +315,7 @@ pub fn parse(data: &[u8], limits: Limits) -> Result<Table, Error> {
match ct {
crate::axml::RES_STRING_POOL => match StringPool::parse(data, pos) {
Ok(p) => {
if p.strings.len() < p.declared {
table.truncated = true;
table.warnings.push(format!("global string pool was capped at {} of {} strings", p.strings.len(), p.declared));
}
report_partial_string_pool(&p, "global string pool", &mut table.truncated, &mut table.warnings);
table.global_strings = p.declared;
global = p;
}
@@ -327,7 +329,7 @@ pub fn parse(data: &[u8], limits: Limits) -> Result<Table, Error> {
table.truncated = true;
table.warnings.push("too many packages; the rest were skipped".into());
} else {
parse_package(data, pos, cs, &global, &mut table, limits);
parse_package(data, pos, cs, &global, &mut table, limits, &mut value_budget);
}
}
_ => {}
@@ -340,7 +342,7 @@ pub fn parse(data: &[u8], limits: Limits) -> Result<Table, Error> {
Ok(table)
}
fn parse_package(data: &[u8], pos: usize, cs: usize, global: &StringPool, table: &mut Table, limits: Limits) {
fn parse_package(data: &[u8], pos: usize, cs: usize, global: &StringPool, table: &mut Table, limits: Limits, value_budget: &mut ValueBudget) {
let Some(data) = pos.checked_add(cs).and_then(|end| data.get(..end)) else {
table.truncated = true;
table.warnings.push("package runs past its resource table".into());
@@ -377,14 +379,7 @@ fn parse_package(data: &[u8], pos: usize, cs: usize, global: &StringPool, table:
};
match StringPool::parse(data, start) {
Ok(pool) => {
if pool.strings.len() < pool.declared {
table.truncated = true;
table.warnings.push(format!(
"package {name}: {label} string pool was capped at {} of {} strings",
pool.strings.len(),
pool.declared
));
}
report_partial_string_pool(&pool, &format!("package {name}: {label} string pool"), &mut table.truncated, &mut table.warnings);
pool
}
Err(error) => {
@@ -447,7 +442,7 @@ fn parse_package(data: &[u8], pos: usize, cs: usize, global: &StringPool, table:
}
}
}
parse_type(&r, c, (ch, sz), (cfg, locale), &p, tid, global, table, limits);
parse_type(&r, c, (ch, sz), (cfg, locale), &p, tid, global, table, limits, value_budget);
}
(RES_TYPE_SPEC | RES_TYPE, None) if ch >= 16 => {
table.truncated = true;
@@ -494,6 +489,7 @@ fn parse_type(
global: &StringPool,
table: &mut Table,
limits: Limits,
value_budget: &mut ValueBudget,
) {
let tid = r.u8(c + 8).unwrap_or(0);
let flags = r.u8(c + 9).unwrap_or(0);
@@ -563,7 +559,7 @@ fn parse_type(
// Compact entry: key index in the size field, type in the high byte of flags.
entry.key = p.keys.get(u32::from(r.u16_le(e).unwrap_or(0))).unwrap_or("").to_string();
let v = ResValue { data_type: (eflags >> 8) as u8, data: r.u32_le(e + 4).unwrap_or(0) };
entry.value = Some(v.format(global));
entry.value = value_budget.take(v.format(global), &mut table.truncated, &mut table.warnings, VALUE_BUDGET_WARNING);
entry.value_type = Some(v.type_name());
entry.raw = Some(v);
} else {
@@ -596,14 +592,16 @@ fn parse_type(
}
let name = r.u32_le(m).unwrap_or(0);
let v = ResValue { data_type: r.u8(m + 7).unwrap_or(0), data: r.u32_le(m + 8).unwrap_or(0) };
entry.bag.push(BagItem { name: format!("@{name:#010x}"), value: v.format(global), value_type: v.type_name() });
let value =
value_budget.take(v.format(global), &mut table.truncated, &mut table.warnings, VALUE_BUDGET_WARNING).unwrap_or_default();
entry.bag.push(BagItem { name: format!("@{name:#010x}"), value, value_type: v.type_name() });
m += 12;
}
} else {
let v_at = e.saturating_add(esize.max(8));
if v_at.saturating_add(8) <= end {
let v = ResValue { data_type: r.u8(v_at + 3).unwrap_or(0), data: r.u32_le(v_at + 4).unwrap_or(0) };
entry.value = Some(v.format(global));
entry.value = value_budget.take(v.format(global), &mut table.truncated, &mut table.warnings, VALUE_BUDGET_WARNING);
entry.value_type = Some(v.type_name());
entry.raw = Some(v);
} else {
@@ -722,4 +720,162 @@ mod tests {
assert_eq!(q, "es-rMX-land");
assert_eq!(l, "es-rMX");
}
fn le_u16(v: &mut Vec<u8>, x: u16) {
v.extend_from_slice(&x.to_le_bytes());
}
fn le_u32(v: &mut Vec<u8>, x: u32) {
v.extend_from_slice(&x.to_le_bytes());
}
fn type_spec(id: u8, entries: u32) -> Vec<u8> {
let mut c = Vec::new();
le_u16(&mut c, 0x0202);
le_u16(&mut c, 16);
le_u32(&mut c, 16 + 4 * entries);
c.push(id);
c.push(0);
le_u16(&mut c, 0);
le_u32(&mut c, entries);
for _ in 0..entries {
le_u32(&mut c, 0);
}
c
}
fn type_chunk(id: u8, entries: &[Vec<u8>]) -> Vec<u8> {
let mut cfg = vec![0u8; 64];
cfg[0] = 64;
let header = 20 + cfg.len();
let start = header + 4 * entries.len();
let mut body = Vec::new();
let mut offs = Vec::new();
for e in entries {
offs.push(body.len() as u32);
body.extend_from_slice(e);
}
let mut c = Vec::new();
le_u16(&mut c, 0x0201);
le_u16(&mut c, header as u16);
le_u32(&mut c, (start + body.len()) as u32);
c.push(id);
c.push(0);
le_u16(&mut c, 0);
le_u32(&mut c, entries.len() as u32);
le_u32(&mut c, start as u32);
c.extend_from_slice(&cfg);
for o in offs {
le_u32(&mut c, o);
}
c.extend(body);
c
}
fn table_with_entries(pool: &[&str], type_name: &str, keys: &[&str], entries: &[Vec<u8>]) -> Vec<u8> {
let global = fx::string_pool(pool, true);
let types = fx::string_pool(&[type_name], false);
let key_pool = fx::string_pool(keys, false);
let mut chunks = type_spec(1, entries.len() as u32);
chunks.extend(type_chunk(1, entries));
let header = 288usize;
let mut pkg = Vec::new();
le_u16(&mut pkg, 0x0200);
le_u16(&mut pkg, header as u16);
le_u32(&mut pkg, (header + types.len() + key_pool.len() + chunks.len()) as u32);
le_u32(&mut pkg, 0x7f);
pkg.extend(vec![0u8; 256]);
le_u32(&mut pkg, header as u32);
le_u32(&mut pkg, 1);
le_u32(&mut pkg, (header + types.len()) as u32);
le_u32(&mut pkg, keys.len() as u32);
le_u32(&mut pkg, 0);
pkg.extend(types);
pkg.extend(key_pool);
pkg.extend(chunks);
let mut out = Vec::new();
le_u16(&mut out, 0x0002);
le_u16(&mut out, 12);
le_u32(&mut out, (12 + global.len() + pkg.len()) as u32);
le_u32(&mut out, 1);
out.extend(global);
out.extend(pkg);
out
}
fn simple_string_entry(key: u32, pool_index: u32) -> Vec<u8> {
let mut e = Vec::new();
le_u16(&mut e, 8);
le_u16(&mut e, 0);
le_u32(&mut e, key);
le_u16(&mut e, 8);
e.push(0);
e.push(0x03);
le_u32(&mut e, pool_index);
e
}
fn compact_string_entry(key: u16, pool_index: u32) -> Vec<u8> {
let mut e = Vec::new();
le_u16(&mut e, key);
le_u16(&mut e, 0x0308);
le_u32(&mut e, pool_index);
e
}
fn bag_string_entry(key: u32, pool_indices: &[u32]) -> Vec<u8> {
let mut e = Vec::new();
le_u16(&mut e, 16);
le_u16(&mut e, 1);
le_u32(&mut e, key);
le_u32(&mut e, 0);
le_u32(&mut e, pool_indices.len() as u32);
for (i, idx) in pool_indices.iter().enumerate() {
le_u32(&mut e, 0x0101_0000 + i as u32);
le_u16(&mut e, 8);
e.push(0);
e.push(0x03);
le_u32(&mut e, *idx);
}
e
}
#[test]
fn value_budget_omits_long_copies_and_keeps_a_later_short_value() {
let long = "x".repeat(16);
let tight = Limits { max_value_bytes: 20, ..Limits::default() };
let keys = ["a", "b", "c"];
let simple =
table_with_entries(&[&long, "ok"], "string", &keys, &[simple_string_entry(0, 0), simple_string_entry(1, 0), simple_string_entry(2, 1)]);
let complete = parse(&simple, Limits::default()).unwrap();
assert!(!complete.truncated, "{:?}", complete.warnings);
assert_eq!(complete.entries.iter().map(|e| e.value.as_deref()).collect::<Vec<_>>(), [Some(long.as_str()), Some(long.as_str()), Some("ok")]);
let partial = parse(&simple, tight).unwrap();
assert!(partial.truncated);
assert!(partial.warnings.iter().any(|w| w.contains("value budget")), "{:?}", partial.warnings);
assert_eq!(partial.entries.iter().map(|e| e.value.as_deref()).collect::<Vec<_>>(), [Some(long.as_str()), None, Some("ok")]);
let compact = table_with_entries(
&[&long, "ok"],
"string",
&keys,
&[compact_string_entry(0, 0), compact_string_entry(1, 0), compact_string_entry(2, 1)],
);
let partial = parse(&compact, tight).unwrap();
assert!(partial.truncated);
assert!(partial.warnings.iter().any(|w| w.contains("value budget")), "{:?}", partial.warnings);
assert_eq!(partial.entries.iter().map(|e| e.value.as_deref()).collect::<Vec<_>>(), [Some(long.as_str()), None, Some("ok")]);
let bag = table_with_entries(&[&long, "ok"], "style", &["theme"], &[bag_string_entry(0, &[0, 0, 1])]);
let complete = parse(&bag, Limits::default()).unwrap();
assert!(!complete.truncated, "{:?}", complete.warnings);
assert_eq!(complete.entries[0].bag.iter().map(|i| i.value.as_str()).collect::<Vec<_>>(), [long.as_str(), long.as_str(), "ok"]);
let partial = parse(&bag, tight).unwrap();
assert!(partial.truncated);
assert!(partial.warnings.iter().any(|w| w.contains("value budget")), "{:?}", partial.warnings);
assert_eq!(partial.entries[0].bag.iter().map(|i| i.value.as_str()).collect::<Vec<_>>(), [long.as_str(), "", "ok"]);
}
}
+150 -19
View File
@@ -20,8 +20,10 @@ const XML_RESOURCE_MAP: u16 = 0x0180;
const MAX_STRINGS: usize = 1_000_000;
/// Longest string kept (characters).
const MAX_STRING: usize = 64 * 1024;
/// Default budget for copied resource and XML values, and the base string-pool byte cap.
pub(crate) const MAX_RETAINED_VALUE_BYTES: usize = 64 << 20;
/// Base budget for decoded string bytes; larger chunks allow at most three times their size.
const BASE_POOL_BYTES: usize = 64 << 20;
const BASE_POOL_BYTES: usize = MAX_RETAINED_VALUE_BYTES;
/// Most extra UTF-8 length candidates examined across one string pool.
const MAX_RECOVERY_PROBES: usize = 65_536;
/// Most XML nodes decoded.
@@ -54,6 +56,8 @@ pub struct StringPool {
pub utf8: bool,
/// Strings the pool declared (may exceed `strings.len()` when capped).
pub declared: usize,
/// Entries left empty because the UTF-8 recovery probe budget ran out.
pub recovery_omitted: usize,
}
impl StringPool {
@@ -104,12 +108,13 @@ impl StringPool {
let strings_reader = Reader::new(string_bytes);
let mut strings = Vec::with_capacity(count.min(MAX_STRINGS).min(65_536));
let mut bytes_left = BASE_POOL_BYTES.max(size.saturating_mul(3));
let mut recovery_omitted = 0;
for i in 0..count.min(MAX_STRINGS) {
let at = r.u32_le(hdr + i * 4).ok_or_else(|| Error::Truncated("string offsets".into()))? as usize;
let s = if at >= string_bytes.len() {
None
} else if utf8 {
utf8_string(&strings_reader, at, string_bytes.len(), recovery_left)
utf8_string(&strings_reader, at, string_bytes.len(), recovery_left, &mut recovery_omitted)
} else {
utf16_string(&strings_reader, at, string_bytes.len())
};
@@ -120,7 +125,7 @@ impl StringPool {
bytes_left -= s.len();
strings.push(s);
}
Ok(StringPool { strings, utf8, declared: count })
Ok(StringPool { strings, utf8, declared: count, recovery_omitted })
}
}
@@ -134,7 +139,7 @@ fn utf8_len(r: &Reader<'_>, at: usize) -> Option<(usize, usize)> {
}
}
fn utf8_string(r: &Reader<'_>, at: usize, end: usize, recovery_left: &mut usize) -> Option<String> {
fn utf8_string(r: &Reader<'_>, at: usize, end: usize, recovery_left: &mut usize, recovery_omitted: &mut usize) -> Option<String> {
let (_chars, n1) = utf8_len(r, at)?;
let (mut bytes, n2) = utf8_len(r, at.checked_add(n1)?)?;
let start = at.checked_add(n1 + n2)?;
@@ -146,7 +151,11 @@ fn utf8_string(r: &Reader<'_>, at: usize, end: usize, recovery_left: &mut usize)
}
// The declared candidate costs one read per entry, already bounded by MAX_STRINGS.
if bytes >= 0x8000 {
*recovery_left = recovery_left.checked_sub(1)?;
if *recovery_left == 0 {
*recovery_omitted += 1;
return None;
}
*recovery_left -= 1;
}
if r.u8(terminator)? == 0 {
break;
@@ -173,6 +182,56 @@ fn utf16_string(r: &Reader<'_>, at: usize, end: usize) -> Option<String> {
Some(String::from_utf16_lossy(&units))
}
/// Record that a string pool was capped or omitted recovery entries.
pub(crate) fn report_partial_string_pool(pool: &StringPool, prefix: &str, truncated: &mut bool, warnings: &mut Vec<String>) {
if pool.strings.len() < pool.declared {
*truncated = true;
warnings.push(format!("{prefix} was capped at {} of {} strings", pool.strings.len(), pool.declared));
}
if pool.recovery_omitted > 0 {
*truncated = true;
warnings.push(format!("{prefix} omitted {} strings after the UTF-8 recovery budget was exhausted", pool.recovery_omitted));
}
}
/// Charge `value` against a retained-byte budget. When it does not fit, report truncation once
/// and return `None`, so a later shorter value can still be kept.
pub(crate) fn retain_value(
left: &mut usize,
value: String,
truncated: &mut bool,
warned: &mut bool,
warnings: &mut Vec<String>,
warning: &str,
) -> Option<String> {
if value.len() > *left {
if !*warned {
warnings.push(warning.to_string());
*warned = true;
}
*truncated = true;
None
} else {
*left -= value.len();
Some(value)
}
}
pub(crate) struct ValueBudget {
left: usize,
warned: bool,
}
impl ValueBudget {
pub(crate) fn new(max_bytes: usize) -> Self {
Self { left: max_bytes, warned: false }
}
pub(crate) fn take(&mut self, value: String, truncated: &mut bool, warnings: &mut Vec<String>, warning: &str) -> Option<String> {
retain_value(&mut self.left, value, truncated, &mut self.warned, warnings, warning)
}
}
/// A typed value (`Res_value`): data type and 32-bit data.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct ResValue {
@@ -313,10 +372,10 @@ pub fn framework_attr(id: u32) -> Option<&'static str> {
/// Decode a binary XML file.
pub fn parse(data: &[u8]) -> Result<Document, Error> {
parse_with_limits(data, MAX_NODES, MAX_DEPTH)
parse_with_limits(data, MAX_NODES, MAX_DEPTH, MAX_RETAINED_VALUE_BYTES)
}
fn parse_with_limits(data: &[u8], max_nodes: usize, max_depth: usize) -> Result<Document, Error> {
fn parse_with_limits(data: &[u8], max_nodes: usize, max_depth: usize, max_value_bytes: usize) -> Result<Document, Error> {
let r = Reader::new(data);
let (t, h, size) = chunk(&r, 0).ok_or_else(|| Error::Truncated("binary XML header".into()))?;
if t != RES_XML {
@@ -329,6 +388,7 @@ fn parse_with_limits(data: &[u8], max_nodes: usize, max_depth: usize) -> Result<
let mut pos = h;
let mut depth = 0usize;
let mut ns_stack: Vec<(String, String)> = Vec::new();
let mut value_budget = ValueBudget::new(max_value_bytes);
while pos < size {
let Some((ct, ch, cs)) = chunk(&r, pos) else {
doc.truncated = true;
@@ -354,10 +414,7 @@ fn parse_with_limits(data: &[u8], max_nodes: usize, max_depth: usize) -> Result<
match ct {
RES_STRING_POOL => match StringPool::parse(data, pos) {
Ok(p) => {
if p.strings.len() < p.declared {
doc.truncated = true;
doc.warnings.push(format!("string pool was capped at {} of {} strings", p.strings.len(), p.declared));
}
report_partial_string_pool(&p, "string pool", &mut doc.truncated, &mut doc.warnings);
doc.pool = p;
}
Err(e) => doc.warnings.push(format!("string pool: {e}")),
@@ -431,6 +488,9 @@ fn parse_with_limits(data: &[u8], max_nodes: usize, max_depth: usize) -> Result<
} else {
raw.format(&doc.pool)
};
let value = value_budget
.take(value, &mut doc.truncated, &mut doc.warnings, "XML value budget reached; remaining values were omitted")
.unwrap_or_default();
attributes.push(Attribute {
namespace: ans,
name: aname,
@@ -450,6 +510,9 @@ fn parse_with_limits(data: &[u8], max_nodes: usize, max_depth: usize) -> Result<
}
XML_CDATA => {
let text = r.u32_le(pos + ch).and_then(|i| doc.pool.get(i)).unwrap_or("").to_string();
let text = value_budget
.take(text, &mut doc.truncated, &mut doc.warnings, "XML value budget reached; remaining values were omitted")
.unwrap_or_default();
doc.nodes.push(Node::Text { depth, text });
}
_ => {}
@@ -639,6 +702,7 @@ mod tests {
assert_eq!(parsed.get(1), parsed.get(0));
assert_eq!(parsed.get(2), Some(""));
assert_eq!(parsed.get(3), Some("ok"));
assert_eq!(parsed.recovery_omitted, 1);
}
#[test]
@@ -655,6 +719,7 @@ mod tests {
assert_eq!(parsed.get(1).map(str::len), Some(0x8000));
assert_eq!(parsed.get(2), Some(""));
assert_eq!(parsed.get(3), Some("ok"));
assert_eq!(parsed.recovery_omitted, 1);
}
#[test]
@@ -670,6 +735,7 @@ mod tests {
assert_eq!(left, remaining);
assert_eq!(parsed.get(0).map(str::len), Some(length));
assert_eq!(parsed.get(1), Some("ok"));
assert_eq!(parsed.recovery_omitted, usize::from(length == 0));
}
}
@@ -681,10 +747,10 @@ mod tests {
assert_eq!(left, 7);
assert_eq!(parsed.get(0), Some(""));
assert_eq!(parsed.get(1), Some("ok"));
assert_eq!(parsed.recovery_omitted, 0);
}
#[test]
fn default_pool_budget_stops_repeated_recovery_before_later_long_strings() {
fn utf8_recovery_exhausted_pool() -> (Vec<u8>, usize) {
let mut strings = vec![b'x'; 2 * 0x8000 + 16];
strings[..4].copy_from_slice(&[0x80, 0, 0x80, 0]);
let long = append_legacy_ascii(&mut strings, 0x8000);
@@ -694,13 +760,78 @@ mod tests {
let repeated = MAX_RECOVERY_PROBES / 3 + 1;
let mut offsets = vec![0; repeated];
offsets.extend([long, short]);
let data = pool_with_offsets(&offsets, &strings);
(pool_with_offsets(&offsets, &strings), repeated)
}
#[test]
fn default_pool_budget_stops_repeated_recovery_before_later_long_strings() {
let (data, repeated) = utf8_recovery_exhausted_pool();
let parsed = StringPool::parse(&data, 0).unwrap();
assert_eq!(parsed.declared, repeated + 2);
assert_eq!(parsed.strings.len(), repeated + 2);
assert!(parsed.strings[..repeated].iter().all(String::is_empty));
assert_eq!(parsed.get(repeated as u32), Some(""));
assert_eq!(parsed.get((repeated + 1) as u32), Some("ok"));
assert!(parsed.recovery_omitted > 0);
}
#[test]
fn xml_and_resource_tables_disclose_recovery_omissions() {
let (pool, repeated) = utf8_recovery_exhausted_pool();
let short_index = (repeated + 1) as u32;
{
let doc = parse(&pool_container(RES_XML, 8, &pool)).unwrap();
assert!(doc.truncated);
assert!(doc.warnings.iter().any(|w| w.contains("string pool") && w.contains("UTF-8 recovery budget")));
assert_eq!(doc.pool.get(short_index), Some("ok"));
assert!(doc.pool.recovery_omitted > 0);
}
let table = crate::arsc::parse(&pool_container(2, 12, &pool), crate::arsc::Limits::default()).unwrap();
assert!(table.truncated);
assert!(table.warnings.iter().any(|w| w.contains("global string pool") && w.contains("UTF-8 recovery budget")));
for (field, label) in [(268, "type"), (276, "key")] {
let mut package = pool_container(0x0200, 288, &pool);
package[8..12].copy_from_slice(&0x7fu32.to_le_bytes());
package[field..field + 4].copy_from_slice(&288u32.to_le_bytes());
let table = crate::arsc::parse(&pool_container(2, 12, &package), crate::arsc::Limits::default()).unwrap();
assert!(table.truncated, "{label}");
assert!(
table.warnings.iter().any(|w| w.contains(&format!("{label} string pool")) && w.contains("UTF-8 recovery budget")),
"{label}: {:?}",
table.warnings
);
}
}
#[test]
fn xml_value_budget_omits_long_copies_and_keeps_a_later_short_value() {
let long = "x".repeat(16);
let attrs = vec![
fx::AxAttr { name: "a", res_id: None, val: fx::AxVal::Str(&long) },
fx::AxAttr { name: "b", res_id: None, val: fx::AxVal::Str(&long) },
fx::AxAttr { name: "c", res_id: None, val: fx::AxVal::Str("ok") },
];
let bytes = fx::axml(&[fx::AxNode::Start("root", attrs), fx::AxNode::End("root")]);
let complete = parse(&bytes).unwrap();
assert!(!complete.truncated);
let Node::Start { attributes, .. } = complete.nodes.iter().find(|n| matches!(n, Node::Start { name, .. } if name == "root")).unwrap() else {
panic!("expected the root start");
};
assert_eq!(
attributes.iter().map(|a| (a.name.as_str(), a.value.as_str())).collect::<Vec<_>>(),
[("a", long.as_str()), ("b", long.as_str()), ("c", "ok")]
);
let partial = parse_with_limits(&bytes, MAX_NODES, MAX_DEPTH, 20).unwrap();
assert!(partial.truncated);
assert!(partial.warnings.iter().any(|w| w.contains("value budget")));
let Node::Start { attributes, .. } = partial.nodes.iter().find(|n| matches!(n, Node::Start { name, .. } if name == "root")).unwrap() else {
panic!("expected the root start");
};
assert_eq!(
attributes.iter().map(|a| (a.name.as_str(), a.value.as_str())).collect::<Vec<_>>(),
[("a", long.as_str()), ("b", ""), ("c", "ok")]
);
}
fn chunk_at(data: &[u8], kind: u16) -> usize {
@@ -757,11 +888,11 @@ mod tests {
#[test]
fn dropping_a_close_event_marks_the_document_partial() {
let bytes = fx::axml(&[fx::AxNode::Start("root", vec![]), fx::AxNode::End("root")]);
let complete = parse_with_limits(&bytes, 3, 1).unwrap();
let complete = parse_with_limits(&bytes, 3, 1, MAX_RETAINED_VALUE_BYTES).unwrap();
assert!(!complete.truncated);
assert!(matches!(complete.nodes.last(), Some(Node::End { depth: 0, .. })));
let partial = parse_with_limits(&bytes, 2, 1).unwrap();
let partial = parse_with_limits(&bytes, 2, 1, MAX_RETAINED_VALUE_BYTES).unwrap();
assert!(partial.truncated);
assert_eq!(partial.nodes, complete.nodes[..2]);
}
@@ -770,19 +901,19 @@ mod tests {
fn nesting_limit_keeps_an_exact_prefix_instead_of_saturating_depth() {
let bytes =
fx::axml(&[fx::AxNode::Start("root", vec![]), fx::AxNode::Start("child", vec![]), fx::AxNode::End("child"), fx::AxNode::End("root")]);
let complete = parse_with_limits(&bytes, 5, 2).unwrap();
let complete = parse_with_limits(&bytes, 5, 2, MAX_RETAINED_VALUE_BYTES).unwrap();
assert!(!complete.truncated);
assert!(matches!(complete.nodes[3], Node::End { depth: 1, .. }));
assert!(matches!(complete.nodes[4], Node::End { depth: 0, .. }));
let partial = parse_with_limits(&bytes, 5, 1).unwrap();
let partial = parse_with_limits(&bytes, 5, 1, MAX_RETAINED_VALUE_BYTES).unwrap();
assert!(partial.truncated);
assert_eq!(partial.nodes, complete.nodes[..2]);
}
#[test]
fn values_format() {
let p = StringPool { strings: vec!["x".into()], utf8: false, declared: 1 };
let p = StringPool { strings: vec!["x".into()], utf8: false, declared: 1, recovery_omitted: 0 };
assert_eq!(ResValue { data_type: 0x05, data: (16 << 8) | 1 }.format(&p), "16dp");
assert_eq!(ResValue { data_type: 0x12, data: 0xffff_ffff }.format(&p), "true");
assert_eq!(ResValue { data_type: 0x1c, data: 0xff00_ff00 }.format(&p), "#ff00ff00");