mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 08:15:29 +00:00
fix(import): normalize maximum drawing stacking orders without replay
This commit is contained in:
1 parent
37d7236d86
commit
c2068fc90b
11 files changed
+348
-5
No files matched your search
@@ -12,6 +12,9 @@ pub const MAX_PART: u64 = 256 * 1024 * 1024;
|
||||
pub const MAX_TOTAL: u64 = 512 * 1024 * 1024;
|
||||
/// Most zip entries we look at.
|
||||
pub const MAX_ENTRIES: usize = 20_000;
|
||||
// ZIP names have at most u16::MAX raw bytes. CP437/lossy UTF-8 decoding can
|
||||
// expand each byte to three UTF-8 bytes. Bound from_parts lookup copies too.
|
||||
const MAX_SCAN_NAME: usize = 3 * u16::MAX as usize;
|
||||
|
||||
/// Relationship type suffixes (after `.../relationships/`).
|
||||
pub mod rt {
|
||||
@@ -265,6 +268,61 @@ impl Package {
|
||||
}
|
||||
}
|
||||
|
||||
/// Read only XML attributes, once per canonical package part. This never invokes
|
||||
/// an altChunk importer or fetches additional data. Non-XML extensions are valid OPC
|
||||
/// targets too. Errors end a lenient part exactly as the normal XML reader does;
|
||||
/// the subsequent strict main-document parse remains authoritative.
|
||||
pub(crate) fn file_orders(&self) -> Result<craft_wordmodel::para::FileOrders, DocxError> {
|
||||
use craft_wordmodel::para::FileOrderScan;
|
||||
let mut bytes = 0u64;
|
||||
for (i, name) in self.opc.names().enumerate() {
|
||||
if name.len() > MAX_SCAN_NAME {
|
||||
return Err(DocxError::Limit("stacking-order source part name is too long".into()));
|
||||
}
|
||||
if i >= MAX_ENTRIES {
|
||||
return Err(DocxError::Limit("too many stacking-order source parts".into()));
|
||||
}
|
||||
bytes = bytes
|
||||
.checked_add(self.opc.get(name).map_or(0, |b| b.len() as u64))
|
||||
.ok_or_else(|| DocxError::Limit("stacking-order source size overflow".into()))?;
|
||||
if bytes > FileOrderScan::MAX_SOURCE_BYTES {
|
||||
return Err(DocxError::Limit("stacking-order source exceeds 2 GiB".into()));
|
||||
}
|
||||
}
|
||||
let mut scan = FileOrderScan::new(bytes).map_err(DocxError::Limit)?;
|
||||
let limits = craft_xml::Limits {
|
||||
max_input: 2 << 30,
|
||||
max_depth: xml::MAX_DEPTH + 1,
|
||||
max_nodes: 2 * xml::MAX_ELEMENTS,
|
||||
max_elements: xml::MAX_ELEMENTS,
|
||||
max_attrs: xml::MAX_ATTRS,
|
||||
max_attrs_total: 2 * xml::MAX_ELEMENTS,
|
||||
};
|
||||
let opts = craft_xml::Options::default().with_limits(limits);
|
||||
for name in self.opc.names() {
|
||||
let Some(bytes) = self.opc.get(name) else { continue };
|
||||
let text = craft_xml::to_utf8(bytes);
|
||||
// Attribute names cannot contain entity references. This only skips parts
|
||||
// that cannot possibly supply an order, including ordinary binary media.
|
||||
if !text.windows(b"relativeHeight".len()).any(|w| w == b"relativeHeight") {
|
||||
continue;
|
||||
}
|
||||
let mut events = opts.events(&text);
|
||||
while let Ok(Some(ev)) = events.next() {
|
||||
if let craft_xml::Event::Start { attrs, .. } = ev {
|
||||
for (name, value) in attrs {
|
||||
if name.rsplit(':').next() == Some("relativeHeight")
|
||||
&& let Ok(raw) = value.trim().parse::<u32>()
|
||||
{
|
||||
scan.observe(raw);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
scan.finish().map_err(DocxError::Limit)
|
||||
}
|
||||
|
||||
/// Relationships of `part` (from `dir/_rels/name.rels`). Internal targets name the part as
|
||||
/// stored, so a part reached under several spellings is kept, cached and written once.
|
||||
pub fn rels(&self, part: &str) -> Rels {
|
||||
@@ -386,3 +444,39 @@ mod tests {
|
||||
assert!(junk.parts.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod file_order_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn file_order_scan_bounds_decoded_part_names_before_lookup() {
|
||||
let at_limit = "n".repeat(MAX_SCAN_NAME);
|
||||
assert!(Package::from_parts(BTreeMap::from([(at_limit, Vec::new())])).file_orders().is_ok());
|
||||
let too_long = "n".repeat(MAX_SCAN_NAME + 1);
|
||||
assert!(Package::from_parts(BTreeMap::from([(too_long, Vec::new())])).file_orders().is_err());
|
||||
}
|
||||
|
||||
fn scan(xml: &str) -> craft_wordmodel::para::FileOrders {
|
||||
Package::from_parts(BTreeMap::from([("odd.part".into(), xml.as_bytes().to_vec())])).file_orders().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_order_scan_matches_lenient_attributes_entities_and_parser_limits() {
|
||||
let malformed = r#"<r><x alien:relativeHeight="4294967295"></wrong><x relativeHeight="4294967294"/></r>"#;
|
||||
let tree = xml::parse(malformed.as_bytes()).unwrap();
|
||||
assert_eq!(tree.find("?:x").or_else(|| tree.find("x")).unwrap().attr("relativeHeight"), Some("4294967295"));
|
||||
let map = scan(malformed);
|
||||
assert_eq!((map.model_order(Some(u32::MAX - 1)), map.model_order(Some(u32::MAX))), (u32::MAX - 1, u32::MAX));
|
||||
let custom = r#"<!DOCTYPE r [<!ENTITY rank "4294967295">]><r relativeHeight="&rank;"/>"#;
|
||||
assert_ne!(xml::parse(custom.as_bytes()).unwrap().attr("relativeHeight"), Some("4294967295"));
|
||||
assert_eq!(scan(custom).model_order(Some(u32::MAX - 1)), u32::MAX);
|
||||
let too_many =
|
||||
format!(r#"<r {} relativeHeight="4294967295"/>"#, (0..xml::MAX_ATTRS).map(|i| format!(r#"a{i}="x""#)).collect::<Vec<_>>().join(" "));
|
||||
assert!(xml::parse(too_many.as_bytes()).is_err());
|
||||
assert_eq!(scan(&too_many).model_order(Some(u32::MAX - 1)), u32::MAX);
|
||||
let too_deep = format!(r#"{}<r relativeHeight="4294967295"/>{}"#, "<r>".repeat(xml::MAX_DEPTH + 1), "</r>".repeat(xml::MAX_DEPTH + 1));
|
||||
assert!(xml::parse(too_deep.as_bytes()).is_err());
|
||||
assert_eq!(scan(&too_deep).model_order(Some(u32::MAX - 1)), u32::MAX);
|
||||
}
|
||||
}
|
||||
@@ -70,6 +70,7 @@ const MAX_TEMPLATE_PATH: usize = 32 * 1024;
|
||||
|
||||
pub(crate) struct Reader<'p> {
|
||||
pkg: &'p Package,
|
||||
file_orders: craft_wordmodel::para::FileOrders,
|
||||
pub doc: Document,
|
||||
pub pc: PropCtx,
|
||||
revs: HashMap<(u8, String, String, Option<String>), u32>,
|
||||
@@ -167,7 +168,9 @@ fn read_opened_in(pkg: &Package, chunks: ChunkState) -> Result<(Document, usize,
|
||||
let rels = pkg.rels(&main);
|
||||
let mut doc = Document::new();
|
||||
doc.body.clear();
|
||||
let file_orders = pkg.file_orders()?;
|
||||
let mut r = Reader {
|
||||
file_orders,
|
||||
pkg,
|
||||
doc,
|
||||
pc: PropCtx::default(),
|
||||
|
||||
@@ -20,7 +20,7 @@ impl Reader<'_> {
|
||||
let dim = |n: &str| ext.and_then(|e| e.attr(n)).and_then(|v| measure(v, 12_700.0)).unwrap_or(0.0).clamp(0.0, crate::units::MAX_LEN_PT);
|
||||
let (w, h) = (dim("cx"), dim("cy"));
|
||||
let (alt, meta) = nv_alt(c.child("wp:docPr"));
|
||||
let mut float = if anchored { anchor_float(c) } else { Float::default() };
|
||||
let mut float = if anchored { anchor_float(c, self.file_orders) } else { Float::default() };
|
||||
float.alt = meta;
|
||||
float.hidden = c.child("wp:docPr").is_some_and(|p| on_off_attr(p, "hidden"));
|
||||
if let Some(e) = c.child("wp:effectExtent") {
|
||||
@@ -620,11 +620,11 @@ fn nv_alt(p: Option<&El>) -> (String, AltText) {
|
||||
(descr, AltText { text: String::new(), title, decorative })
|
||||
}
|
||||
|
||||
fn anchor_float(c: &El) -> Float {
|
||||
fn anchor_float(c: &El, orders: craft_wordmodel::para::FileOrders) -> Float {
|
||||
// `relativeHeight`: the stacking order Word draws floating objects in (higher on top), which
|
||||
// need not be their order in the file. Kept one more (see `Float::z`): 0, the lowest a file
|
||||
// can give, isn't the 0 of an object without one, which stacks above them all.
|
||||
let z = c.attr("relativeHeight").and_then(|v| v.trim().parse::<u32>().ok()).map_or(0, |z| z.saturating_add(1));
|
||||
let z = orders.model_order(c.attr("relativeHeight").and_then(|v| v.trim().parse::<u32>().ok()));
|
||||
let mut f = Float { wrap: Wrap::Square, z, ..Float::default() };
|
||||
let behind = on_off_attr(c, "behindDoc");
|
||||
// Word wraps a position relative to the page's size (`wp14:pctPosVOffset`) in
|
||||
|
||||
@@ -4912,3 +4912,58 @@ fn vml_horizontal_rules_are_read_and_kept() {
|
||||
let d = read_body(&body);
|
||||
assert_eq!(rules(&d).iter().map(|r| r.0).collect::<Vec<_>>(), [None, None]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extreme_raw_orders_share_one_map_across_parts_without_replaying_chunks() {
|
||||
let shape = |name: &str, z: &str| {
|
||||
anchored_wsp(name, 127000, 127000, r#"<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>"#, "", "<wps:cNvSpPr/>")
|
||||
.replace(r#"relativeHeight="1""#, &format!(r#"relativeHeight="{z}""#))
|
||||
};
|
||||
let doc = format!(
|
||||
r#"<w:document {W_NS}><w:body><w:p>{}{}{}</w:p><w:altChunk r:id="chunk"/><w:sectPr><w:headerReference w:type="default" r:id="header"/></w:sectPr></w:body></w:document>"#,
|
||||
shape("zero", "0"),
|
||||
shape("lower", "4294967292"),
|
||||
shape("high", "4294967294")
|
||||
);
|
||||
let header = format!(
|
||||
r#"<w:hdr {W_NS}><w:p>{}{}</w:p></w:hdr>"#,
|
||||
shape("maximum", "4294967295").replace("relativeHeight", "alien:relativeHeight"),
|
||||
shape("equal", "4294967295")
|
||||
)
|
||||
.replace("xmlns:wp=", "xmlns:position=")
|
||||
.replace("wp:", "position:");
|
||||
let header: Vec<u8> = [vec![0xff, 0xfe], header.encode_utf16().flat_map(u16::to_le_bytes).collect()].concat();
|
||||
let ignored = format!(r#"<w:hdr {W_NS}><w:p>{}</w:p></w:hdr>"#, shape("wrong-case duplicate", "4294967293"));
|
||||
let rel = rels(&[("header", "header", "header.part"), ("chunk", "aFChunk", "chunk.txt")]);
|
||||
let bytes = zip(&[
|
||||
("_rels/.rels", ROOT_RELS.as_bytes()),
|
||||
("word/document.xml", doc.as_bytes()),
|
||||
("word/_rels/document.xml.rels", rel.as_bytes()),
|
||||
("word/header.part", &header),
|
||||
("WORD/HEADER.PART", ignored.as_bytes()),
|
||||
("word/chunk.txt", b"external chunk"),
|
||||
]);
|
||||
let calls = std::cell::Cell::new(0);
|
||||
let importer = |_: &craft_docx::AltChunk| {
|
||||
calls.set(calls.get() + 1);
|
||||
Some(Document::from_text("Imported once"))
|
||||
};
|
||||
let mut d = craft_docx::read_with(&bytes, &craft_docx::ReadOptions { chunks: Some(&importer), ..Default::default() }).unwrap();
|
||||
assert_eq!(calls.get(), 1);
|
||||
let orders = |d: &Document| {
|
||||
let body: Vec<u32> = paras(d).iter().flat_map(|p| &p.objects).filter_map(|o| o.frame().map(|(_, _, f)| f.z)).collect();
|
||||
let header = &d.parts[&d.last_section.headers.default.unwrap()];
|
||||
let header: Vec<u32> =
|
||||
header.blocks.iter().filter_map(|b| b.as_para()).flat_map(|p| &p.objects).filter_map(|o| o.frame().map(|(_, _, f)| f.z)).collect();
|
||||
(body, header)
|
||||
};
|
||||
let expected = (vec![1, u32::MAX - 2, u32::MAX - 1], vec![u32::MAX, u32::MAX]);
|
||||
assert_eq!(orders(&d), expected);
|
||||
for _ in 0..2 {
|
||||
d = craft_docx::read(&craft_docx::write(&d).unwrap()).unwrap();
|
||||
assert_eq!(orders(&d), expected);
|
||||
}
|
||||
assert_eq!(calls.get(), 1);
|
||||
let malformed = doc.replace("</w:body>", "");
|
||||
assert!(craft_docx::read(&zip(&[("_rels/.rels", ROOT_RELS.as_bytes()), ("word/document.xml", malformed.as_bytes())])).is_err());
|
||||
}
|
||||
@@ -35,6 +35,7 @@ type Frame =
|
||||
(Option<f32>, Option<f32>, Option<Vec<u8>>, String, Option<(String, bool)>, Option<craft_wordmodel::chart::ChartSpec>, Option<model::OdfObject>);
|
||||
|
||||
pub(super) struct Body<'a> {
|
||||
pub(super) file_orders: craft_wordmodel::para::FileOrders,
|
||||
styles: &'a Styles,
|
||||
zip: &'a mut dyn FnMut(&str) -> Option<Vec<u8>>,
|
||||
/// The package's entry names and its manifest's media types, for embedded objects.
|
||||
@@ -219,6 +220,7 @@ impl<'a> Body<'a> {
|
||||
pub(super) fn new(styles: &'a Styles, pages: &'a Pages, zip: &'a mut dyn FnMut(&str) -> Option<Vec<u8>>) -> Self {
|
||||
let page = pages.first.clone().unwrap_or_default();
|
||||
Body {
|
||||
file_orders: Default::default(),
|
||||
styles,
|
||||
zip,
|
||||
entries: &[],
|
||||
@@ -1164,6 +1166,25 @@ impl Body<'_> {
|
||||
}
|
||||
|
||||
fn start(&mut self, q: &str, a: &Attrs) {
|
||||
// Keep all existing attribute readers unchanged. Only the occupied upper tail
|
||||
// is adjusted, before any frame/group/note reader can saturate its one-more z.
|
||||
let adjusted = a
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, (name, _))| name == "draw:z-index")
|
||||
.and_then(|(i, (_, value))| value.trim().parse::<u32>().ok().map(|raw| (i, raw)))
|
||||
.and_then(|(i, raw)| {
|
||||
let mapped = self.file_orders.file_order(raw);
|
||||
(mapped != raw).then_some((i, mapped))
|
||||
});
|
||||
let mut normalized;
|
||||
let a = if let Some((i, mapped)) = adjusted {
|
||||
normalized = a.clone();
|
||||
normalized[i].1 = mapped.to_string();
|
||||
&normalized
|
||||
} else {
|
||||
a
|
||||
};
|
||||
self.depth += 1;
|
||||
if q == "office:text" {
|
||||
self.in_text = true;
|
||||
|
||||
@@ -725,3 +725,37 @@ pub(super) fn frame_of(f: &Float, w: f32, h: f32) -> (&'static str, String, Stri
|
||||
}
|
||||
(anchor, props, xy)
|
||||
}
|
||||
|
||||
/// Scan the same already-inflated content/style streams as the body and header readers.
|
||||
/// The existing event parser supplies the same entity, malformed-input and 128-attribute
|
||||
/// truncation behavior; no picture fetch or embedded-object callback is invoked.
|
||||
pub(super) fn read_orders(content: &[u8], styles: Option<&[u8]>) -> Result<craft_wordmodel::para::FileOrders, String> {
|
||||
use craft_wordmodel::para::FileOrderScan;
|
||||
let bytes = (content.len() as u64).checked_add(styles.map_or(0, |s| s.len() as u64)).ok_or("stacking-order source size overflow")?;
|
||||
let mut scan = FileOrderScan::new(bytes)?;
|
||||
for xml in std::iter::once(content).chain(styles) {
|
||||
super::xml::events(xml, |ev| {
|
||||
if let super::xml::Ev::Start(_, attrs) = ev
|
||||
&& let Some(raw) = get(&attrs, "draw:z-index").and_then(|s| s.trim().parse::<u32>().ok())
|
||||
{
|
||||
scan.observe(raw);
|
||||
}
|
||||
});
|
||||
}
|
||||
scan.finish()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod file_order_tests {
|
||||
#[test]
|
||||
fn file_order_scan_uses_the_existing_odf_attribute_and_entity_rules() {
|
||||
let ignored = format!(r#"<r {} draw:z-index="4294967295"/>"#, (0..128).map(|i| format!(r#"a{i}="x""#)).collect::<Vec<_>>().join(" "));
|
||||
let map = super::read_orders(ignored.as_bytes(), None).unwrap();
|
||||
assert_eq!(map.model_order(Some(u32::MAX - 1)), u32::MAX);
|
||||
let custom = br#"<!DOCTYPE r [<!ENTITY rank "4294967295">]><r draw:z-index="&rank;"/>"#;
|
||||
assert_eq!(super::read_orders(custom, None).unwrap().model_order(Some(u32::MAX - 1)), u32::MAX);
|
||||
let numeric = br#"<r draw:z-index="4294967295"><x draw:z-index="4294967294"/></r>"#;
|
||||
let map = super::read_orders(numeric, None).unwrap();
|
||||
assert_eq!((map.model_order(Some(u32::MAX - 1)), map.model_order(Some(u32::MAX))), (u32::MAX - 1, u32::MAX));
|
||||
}
|
||||
}
|
||||
@@ -875,6 +875,7 @@ fn parse_tracked(bytes: &[u8], paper: (f32, f32)) -> Result<Parsed, String> {
|
||||
None if styles_xml.is_some() => EMPTY_CONTENT.as_bytes().to_vec(),
|
||||
None => return Err("ODT: content.xml is missing".into()),
|
||||
};
|
||||
let file_orders = super::frames::read_orders(&content, styles_xml.as_deref())?;
|
||||
let mut styles = Styles::default();
|
||||
let mut pages = Pages::default();
|
||||
if let Some(s) = &styles_xml {
|
||||
@@ -916,10 +917,13 @@ fn parse_tracked(bytes: &[u8], paper: (f32, f32)) -> Result<Parsed, String> {
|
||||
let objects = (entries.as_slice(), &manifest);
|
||||
// One `text:s` budget for the headers and footers and then the body.
|
||||
let mut spaces_left = MAX_EXTRA_SPACES;
|
||||
let mut parts =
|
||||
styles_xml.as_deref().map(|x| page_parts::read(x, &styles, &mut pages, &mut fetch, objects, &mut spaces_left)).unwrap_or_default();
|
||||
let mut parts = styles_xml
|
||||
.as_deref()
|
||||
.map(|x| page_parts::read(x, &styles, &mut pages, &mut fetch, objects, &mut spaces_left, file_orders))
|
||||
.unwrap_or_default();
|
||||
first_pages(&mut pages);
|
||||
let mut body = Body::new(&styles, &pages, &mut fetch);
|
||||
body.file_orders = file_orders;
|
||||
(body.entries, body.manifest) = (objects.0, Some(objects.1));
|
||||
body.raw = &content;
|
||||
body.spaces_left = spaces_left;
|
||||
|
||||
@@ -78,6 +78,7 @@ pub(super) fn read(
|
||||
fetch: &mut dyn FnMut(&str) -> Option<Vec<u8>>,
|
||||
objects: (&[String], &std::collections::HashMap<String, String>),
|
||||
spaces_left: &mut usize,
|
||||
file_orders: craft_wordmodel::para::FileOrders,
|
||||
) -> Parts {
|
||||
let mut found: Vec<(String, Slot, Vec<Ev>)> = Vec::new();
|
||||
let mut master: Option<String> = None;
|
||||
@@ -127,6 +128,7 @@ pub(super) fn read(
|
||||
let mut out = Parts::default();
|
||||
for (name, kind, evs) in found {
|
||||
let mut reader = Body::new(styles, &none, fetch);
|
||||
reader.file_orders = file_orders;
|
||||
reader.in_text = true;
|
||||
(reader.entries, reader.manifest) = (objects.0, Some(objects.1));
|
||||
reader.spaces_left = *spaces_left;
|
||||
|
||||
@@ -572,3 +572,29 @@ fn extreme_stacking_orders_remain_distinct_after_repeated_roundtrips() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extreme_raw_orders_are_normalized_once_across_body_and_headers() {
|
||||
let shape = |z: &str| format!(r#"<draw:rect text:anchor-type="paragraph" svg:width="1in" svg:height="1in" draw:z-index="{z}"/>"#);
|
||||
let content = format!(
|
||||
r#"<office:document-content {NS_T} {NS_D}><office:body><office:text><text:p>{}{}</text:p></office:text></office:body></office:document-content>"#,
|
||||
shape("0"),
|
||||
shape("4294967294")
|
||||
);
|
||||
let styles = format!(
|
||||
r#"<office:document-styles {NS_T} {NS_D}><office:automatic-styles><style:page-layout style:name="pm1"><style:page-layout-properties fo:page-width="21cm" fo:page-height="29.7cm"/></style:page-layout></office:automatic-styles><office:master-styles><style:master-page style:name="Standard" style:page-layout-name="pm1"><style:header><text:p>{}{}</text:p></style:header></style:master-page></office:master-styles></office:document-styles>"#,
|
||||
shape("4294967295"),
|
||||
shape("4294967295")
|
||||
);
|
||||
let mut d = import(&zip_of(&[("content.xml", content.as_bytes()), ("styles.xml", styles.as_bytes())])).unwrap();
|
||||
for _ in 0..3 {
|
||||
let body: Vec<u32> =
|
||||
d.body.iter().filter_map(|b| b.as_para()).flat_map(|p| &p.objects).filter_map(|o| o.frame().map(|(_, _, f)| f.z)).collect();
|
||||
let header = &d.parts[&d.last_section.headers.default.unwrap()];
|
||||
let head: Vec<u32> =
|
||||
header.blocks.iter().filter_map(|b| b.as_para()).flat_map(|p| &p.objects).filter_map(|o| o.frame().map(|(_, _, f)| f.z)).collect();
|
||||
assert_eq!(body, [1, u32::MAX - 1]);
|
||||
assert_eq!(head, [u32::MAX, u32::MAX]);
|
||||
d = import(&crate::text::tests::export_model(&d).unwrap()).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -13,11 +13,13 @@ use crate::props::{CharProps, ParaProps};
|
||||
use crate::section::SectionProps;
|
||||
use crate::{DocError, Result};
|
||||
|
||||
mod file_orders;
|
||||
mod float;
|
||||
mod inline_kinds;
|
||||
mod inline_object;
|
||||
mod run;
|
||||
|
||||
pub use file_orders::{FileOrderScan, FileOrders};
|
||||
pub use float::{AltText, Anchor, Float, FloatAlign, MAX_ALT_TEXT, Wrap, WrapText, cap_alt};
|
||||
pub use inline_kinds::{NoteKind, PTabAlign, PTabBase, RubyAlign, ShapeKind, ruby_offset, ruby_raise};
|
||||
pub use inline_object::{CanvasStyle, GroupChild, InlineObject, MAX_GROUP_CHILDREN};
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
//! Bounded conversion of unsigned file stacking orders to Float's zero-is-unset model.
|
||||
|
||||
/// A file-order mapping. Ordinary files retain their exact one-more encoding.
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct FileOrders {
|
||||
gap: Option<u32>,
|
||||
}
|
||||
|
||||
impl FileOrders {
|
||||
/// A file order adjusted to leave room for its one-more model representation.
|
||||
pub fn file_order(self, raw: u32) -> u32 {
|
||||
raw - u32::from(self.gap.is_some_and(|gap| raw > gap))
|
||||
}
|
||||
|
||||
/// An absent order remains unset; a present file order remains present.
|
||||
pub fn model_order(self, raw: Option<u32>) -> u32 {
|
||||
raw.map_or(0, |raw| self.file_order(raw).saturating_add(1))
|
||||
}
|
||||
}
|
||||
|
||||
/// Collect every raw order before decoding any. No document or callback is replayed.
|
||||
///
|
||||
/// The upper window has more slots than the source can contain distinct ten-digit
|
||||
/// orders. Only this window can be shifted. XML callers must count all source bytes,
|
||||
/// decode attributes exactly as their reader does, and never expand custom entities.
|
||||
pub struct FileOrderScan {
|
||||
seen: Vec<u8>,
|
||||
slots: usize,
|
||||
}
|
||||
|
||||
impl FileOrderScan {
|
||||
/// Most source bytes scanned. This also caps the bitmap below 26 MiB.
|
||||
pub const MAX_SOURCE_BYTES: u64 = 2 * 1024 * 1024 * 1024;
|
||||
|
||||
/// Allocate fallibly; reject an unbounded source rather than truncating the scan.
|
||||
pub fn new(source_bytes: u64) -> Result<Self, String> {
|
||||
if source_bytes > Self::MAX_SOURCE_BYTES {
|
||||
return Err("stacking-order source exceeds 2 GiB".into());
|
||||
}
|
||||
let slots = (source_bytes / 10 + 1) as usize;
|
||||
let size = slots.div_ceil(8);
|
||||
let mut seen = Vec::new();
|
||||
seen.try_reserve_exact(size).map_err(|_| "stacking-order bitmap allocation failed")?;
|
||||
seen.resize(size, 0);
|
||||
Ok(Self { seen, slots })
|
||||
}
|
||||
|
||||
/// Observe a decoded unsigned order. Repeated values cost no extra memory.
|
||||
pub fn observe(&mut self, raw: u32) {
|
||||
let distance = (u32::MAX - raw) as usize;
|
||||
if distance < self.slots
|
||||
&& let Some(byte) = self.seen.get_mut(distance / 8)
|
||||
{
|
||||
*byte |= 1 << (distance % 8);
|
||||
}
|
||||
}
|
||||
|
||||
/// Finish after the complete source traversal, before constructing model floats.
|
||||
pub fn finish(self) -> Result<FileOrders, String> {
|
||||
if self.seen.first().is_none_or(|b| b & 1 == 0) {
|
||||
return Ok(FileOrders::default());
|
||||
}
|
||||
for (i, byte) in self.seen.into_iter().enumerate() {
|
||||
if byte == u8::MAX {
|
||||
continue;
|
||||
}
|
||||
let distance = i * 8 + (!byte).trailing_zeros() as usize;
|
||||
if distance < self.slots {
|
||||
return Ok(FileOrders { gap: Some(u32::MAX - distance as u32) });
|
||||
}
|
||||
}
|
||||
Err("stacking-order scan did not cover its source".into())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn upper_tail_is_injective_and_leaves_ordinary_orders_alone() {
|
||||
let raw = [0, 1, 251658240, u32::MAX - 2, u32::MAX - 1, u32::MAX, u32::MAX];
|
||||
let mut scan = FileOrderScan::new(raw.len() as u64 * 10).unwrap();
|
||||
for raw in raw {
|
||||
scan.observe(raw);
|
||||
}
|
||||
let map = scan.finish().unwrap();
|
||||
assert_eq!(raw.map(|raw| map.model_order(Some(raw))), [1, 2, 251658241, u32::MAX - 2, u32::MAX - 1, u32::MAX, u32::MAX]);
|
||||
assert_eq!(map.model_order(None), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_max_means_no_renumbering_and_source_limits_fail_closed() {
|
||||
let mut scan = FileOrderScan::new(100).unwrap();
|
||||
scan.observe(u32::MAX - 1);
|
||||
assert_eq!(scan.finish().unwrap().model_order(Some(u32::MAX - 1)), u32::MAX);
|
||||
assert!(FileOrderScan::new(FileOrderScan::MAX_SOURCE_BYTES + 1).is_err());
|
||||
let mut incomplete = FileOrderScan::new(0).unwrap();
|
||||
incomplete.observe(u32::MAX);
|
||||
assert!(incomplete.finish().is_err());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user