diff --git a/src/formats/sheet/xls.rs b/src/formats/sheet/xls.rs index 28488fdc..0ed3fa9b 100644 --- a/src/formats/sheet/xls.rs +++ b/src/formats/sheet/xls.rs @@ -821,9 +821,9 @@ fn txo_text(segs: &[&[u8]]) -> Option { Some(String::from_utf16_lossy(&units)) } -/// Record only non-empty cell text, like the xlsx reader. +/// Record only cells with visible text, like the xlsx reader. fn put(out: &mut SheetContent, row: u32, col: u32, text: String) { - if !text.is_empty() { + if !text.trim().is_empty() { out.cells.insert((row, col), text); } } diff --git a/src/formats/sheet/xlsb.rs b/src/formats/sheet/xlsb.rs index 7bc042f0..ce0a1d17 100644 --- a/src/formats/sheet/xlsb.rs +++ b/src/formats/sheet/xlsb.rs @@ -76,10 +76,20 @@ pub(super) fn parse(pkg: &mut Package, wb_part: &str) -> Result = HashMap::with_capacity(sheets.len()); + for (_, part) in &sheets { + *remaining.entry(part.as_str()).or_insert(0) += 1; + } let mut slots = 0u64; for (name, part) in &sheets { let content = pkg.optional_part(part)?.map(|bytes| read_sheet(&bytes, &shared, &xfs, date1904)); + if let Some(left) = remaining.get_mut(part.as_str()) { + *left -= 1; + if *left == 0 { + pkg.evict(part); + } + } let mut content = match content { Some(Ok(c)) => c, Some(Err(e)) if e.is_fatal() => return Err(e), @@ -272,7 +282,7 @@ fn read_sheet( } let fmt = xfs.get(style as usize).unwrap_or(&CellFormat::General); let text = cell_text(id, &mut f, fmt, shared, date1904)?; - if !text.is_empty() { + if !text.trim().is_empty() { out.cells.insert((r, col), text); } } diff --git a/src/formats/sheet/xlsx.rs b/src/formats/sheet/xlsx.rs index e0924562..e5ce2df5 100644 --- a/src/formats/sheet/xlsx.rs +++ b/src/formats/sheet/xlsx.rs @@ -10,10 +10,13 @@ use crate::error::ConvertError; use crate::model::{Block, Cell, Document, GridBuilder, Inline, Table, TableKind}; use crate::package::limits; use crate::package::relationships::{Relationships, read_rels, rel_type, rels_part_for}; -use crate::package::xml::{Element, ns}; +use crate::package::xml::{Element, ns, resolve_entity, to_utf8}; use crate::package::{Package, path}; use crate::shared::header::resolve_header_rows; use crate::shared::text::clean_text; +use quick_xml::NsReader; +use quick_xml::events::{BytesStart, Event}; +use quick_xml::name::ResolveResult; use std::collections::{HashMap, HashSet}; use std::rc::Rc; @@ -74,15 +77,34 @@ pub(super) fn parse(pkg: &mut Package, wb_part: &str) -> Result = HashMap::with_capacity(sheets.len()); + for (_, part) in &sheets { + *remaining.entry(part.as_str()).or_insert(0) += 1; + } let mut slots = 0u64; for (name, part) in &sheets { - let worksheet = pkg.optional_xml_part(part)?; - let Some(worksheet) = worksheet.as_ref().and_then(|r| r.find(ns::SML, "worksheet")) else { - log::warn!("skipping unreadable sheet {name:?}"); - failed += 1; - continue; + let content = + pkg.optional_part(part)?.map(|bytes| read_sheet(&bytes, &shared, &styles, date1904)); + if let Some(left) = remaining.get_mut(part.as_str()) { + *left -= 1; + if *left == 0 { + pkg.evict(part); + } + } + let mut content = match content { + Some(Ok(c)) => c, + Some(Err(e)) if e.is_fatal() => return Err(e), + Some(Err(e)) => { + log::warn!("skipping unreadable sheet {name:?}: {e}"); + failed += 1; + continue; + } + None => { + log::warn!("skipping unreadable sheet {name:?}"); + failed += 1; + continue; + } }; - let mut content = read_sheet(worksheet, &shared, &styles, date1904); content.checkboxes = read_vml_checkboxes(pkg, part)?; let Some(table) = build_table(content, &mut slots)? else { continue; @@ -200,10 +222,8 @@ impl Styles { Styles { xfs } } - /// The format for a cell's `s` attribute, an index into `cellXfs` - /// (default 0). - fn for_cell(&self, s: Option<&str>) -> &CellFormat { - let i = s.and_then(|s| s.parse::().ok()).unwrap_or(0); + /// The format for a cell's `s` index into `cellXfs` (default 0). + fn for_index(&self, i: usize) -> &CellFormat { self.xfs.get(i).unwrap_or(&CellFormat::General) } } @@ -245,106 +265,372 @@ pub(super) struct SheetContent { pub(super) merges: Vec<(u32, u32, u32, u32)>, } +#[derive(Clone, Copy, PartialEq, Eq)] +enum Tag { + Worksheet, + Cols, + SheetData, + Row, + Cell, + Value, + InlineStr, + RichRun, + InlineText, + MergeCells, + Other, +} + +#[derive(Clone, Copy, Default)] +enum CellKind { + #[default] + Number, + SharedStr, + FormulaStr, + InlineStr, + Bool, + Literal, +} + +#[derive(Default)] +struct ActiveCell { + active: bool, + row: u32, + col: u32, + style: usize, + kind: CellKind, + has_v: bool, + has_is: bool, + r_has_t: bool, + v_buf: String, + is_buf: String, +} + +impl ActiveCell { + fn reset(&mut self, row: u32, col: u32, style: usize, kind: CellKind) { + self.active = true; + self.row = row; + self.col = col; + self.style = style; + self.kind = kind; + self.has_v = false; + self.has_is = false; + self.r_has_t = false; + self.v_buf.clear(); + self.is_buf.clear(); + } +} + +/// Transitional or ISO/IEC 29500 Strict SpreadsheetML namespace. +fn is_sml(res: &ResolveResult<'_>) -> bool { + matches!( + res, + ResolveResult::Bound(ns) + if ns.as_ref() == ns::SML.as_bytes() + || ns.as_ref() == b"http://purl.oclc.org/ooxml/spreadsheetml/main" + ) +} + +/// Decode an unqualified attribute value on an opening element. +fn unqualified_attr(e: &BytesStart<'_>, reader: &NsReader<&[u8]>, name: &[u8]) -> Option { + for attr in e.attributes().flatten() { + if attr.key.as_ref() == name { + let val = attr + .decoded_and_normalized_value(quick_xml::XmlVersion::Implicit1_0, reader.decoder()) + .map(|v| v.into_owned()) + .unwrap_or_else(|_| String::from_utf8_lossy(&attr.value).into_owned()); + return Some(val); + } + let (res, local) = reader.resolver().resolve_attribute(attr.key); + if matches!(res, ResolveResult::Unbound) && local.as_ref() == name { + let val = attr + .decoded_and_normalized_value(quick_xml::XmlVersion::Implicit1_0, reader.decoder()) + .map(|v| v.into_owned()) + .unwrap_or_else(|_| String::from_utf8_lossy(&attr.value).into_owned()); + return Some(val); + } + } + None +} + +/// Stream a worksheet part directly rather than building an `Element` DOM: +/// real spreadsheets routinely carry hundreds of thousands of ``/`` +/// tags (including dragged space fills down to row 1,048,576), which would +/// trip the DOM node budget and fragment per-worker heap arenas while needing +/// only row/cell state at a time. fn read_sheet( - worksheet: &Element, + bytes: &[u8], shared: &[String], styles: &Styles, date1904: bool, -) -> SheetContent { +) -> Result { + let utf8 = to_utf8(bytes); + let mut reader = NsReader::from_reader(utf8.as_ref()); + let mut out = SheetContent::default(); - for cols in worksheet.find_all(ns::SML, "cols") { - for col in cols.find_all(ns::SML, "col") { - if !col.attr_unqualified("hidden").is_some_and(bool_attr) { - continue; + let mut stack: Vec = Vec::new(); + let mut saw_worksheet = false; + let mut next_row: u32 = 0; + let mut cur_row: u32 = 0; + let mut next_col: u32 = 0; + let mut cell = ActiveCell::default(); + + let finish_cell = |cell: &mut ActiveCell, out: &mut SheetContent| { + if !cell.active { + return; + } + cell.active = false; + let text = render_cell_value(cell, shared, styles, date1904); + if !text.trim().is_empty() { + out.cells.insert((cell.row, cell.col), text); + } + }; + + loop { + let event = reader + .read_event() + .map_err(|e| ConvertError::malformed(format!("unparseable xml: {e}")))?; + match event { + Event::Start(e) => { + if stack.len() >= limits::MAX_XML_DEPTH { + return Err(ConvertError::ResourceLimit { + limit: "max_xml_depth", + detail: format!("element nesting exceeds {}", limits::MAX_XML_DEPTH), + }); + } + let tag = open_worksheet_tag( + &e, + &reader, + stack.last().copied(), + &mut saw_worksheet, + &mut next_row, + &mut cur_row, + &mut next_col, + &mut cell, + &mut out, + ); + stack.push(tag); } - let bound = |name| { - col.attr_unqualified(name) - .and_then(|v| v.parse::().ok()) - .and_then(|v| v.checked_sub(1)) - }; - if let (Some(min), Some(max)) = (bound("min"), bound("max")) - && min <= max - { - out.hidden_cols.push((min, max.min(MAX_COLS - 1))); + Event::Empty(e) => { + let tag = open_worksheet_tag( + &e, + &reader, + stack.last().copied(), + &mut saw_worksheet, + &mut next_row, + &mut cur_row, + &mut next_col, + &mut cell, + &mut out, + ); + if tag == Tag::Cell { + finish_cell(&mut cell, &mut out); + } + } + Event::End(_) => { + if stack.pop() == Some(Tag::Cell) { + finish_cell(&mut cell, &mut out); + } } + Event::Text(e) => { + if let Some(target) = active_text_buf(stack.last().copied(), &mut cell) { + match e.decode() { + Ok(t) => target.push_str(&t), + Err(_) => target.push_str(&String::from_utf8_lossy(e.as_ref())), + } + } + } + Event::GeneralRef(e) => { + if let Some(target) = active_text_buf(stack.last().copied(), &mut cell) { + let name = String::from_utf8_lossy(e.as_ref()); + match resolve_entity(&name) { + Some(resolved) => target.push_str(&resolved), + None => { + target.push('&'); + target.push_str(&name); + target.push(';'); + } + } + } + } + Event::CData(e) => { + if let Some(target) = active_text_buf(stack.last().copied(), &mut cell) { + target.push_str(&String::from_utf8_lossy(e.as_ref())); + } + } + Event::Eof => break, + _ => {} } } - let mut next_row: u32 = 0; - for row in worksheet.find_all(ns::SML, "sheetData").flat_map(|sd| sd.find_all(ns::SML, "row")) { - let r = row - .attr_unqualified("r") - .and_then(|v| v.parse::().ok()) - .and_then(|v| v.checked_sub(1)) - .unwrap_or(next_row); - if r >= MAX_ROWS { - continue; + while let Some(tag) = stack.pop() { + if tag == Tag::Cell { + finish_cell(&mut cell, &mut out); } - next_row = r + 1; - if row.attr_unqualified("hidden").is_some_and(bool_attr) { - out.hidden_rows.insert(r); + } + if !saw_worksheet { + return Err(ConvertError::malformed("worksheet part has no worksheet root")); + } + Ok(out) +} + +#[allow(clippy::too_many_arguments)] +fn open_worksheet_tag( + e: &BytesStart<'_>, + reader: &NsReader<&[u8]>, + parent: Option, + saw_worksheet: &mut bool, + next_row: &mut u32, + cur_row: &mut u32, + next_col: &mut u32, + cell: &mut ActiveCell, + out: &mut SheetContent, +) -> Tag { + let (res, local) = reader.resolver().resolve_element(e.name()); + let sml = is_sml(&res); + let name = local.as_ref(); + match parent { + None if sml && name == b"worksheet" => { + *saw_worksheet = true; + Tag::Worksheet + } + Some(Tag::Worksheet) if sml => match name { + b"cols" => Tag::Cols, + b"sheetData" => Tag::SheetData, + b"mergeCells" => Tag::MergeCells, + _ => Tag::Other, + }, + Some(Tag::Cols) if sml && name == b"col" => { + if unqualified_attr(e, reader, b"hidden").as_deref().is_some_and(bool_attr) { + let bound = |attr| { + unqualified_attr(e, reader, attr) + .and_then(|v| v.parse::().ok()) + .and_then(|v| v.checked_sub(1)) + }; + if let (Some(min), Some(max)) = (bound(b"min"), bound(b"max")) + && min <= max + { + out.hidden_cols.push((min, max.min(MAX_COLS - 1))); + } + } + Tag::Other + } + Some(Tag::SheetData) if sml && name == b"row" => { + let r = unqualified_attr(e, reader, b"r") + .and_then(|v| v.parse::().ok()) + .and_then(|v| v.checked_sub(1)) + .unwrap_or(*next_row); + if r >= MAX_ROWS { + return Tag::Other; + } + *next_row = r + 1; + *cur_row = r; + *next_col = 0; + if unqualified_attr(e, reader, b"hidden").as_deref().is_some_and(bool_attr) { + out.hidden_rows.insert(r); + } + Tag::Row } - let mut next_col: u32 = 0; - for c in row.find_all(ns::SML, "c") { + Some(Tag::Row) if sml && name == b"c" => { // Position comes from the cell's own reference: a row may skip // cells entirely, so iteration order says nothing. - let (cr, cc) = match c.attr_unqualified("r").map(parse_ref) { + let (cr, cc) = match unqualified_attr(e, reader, b"r").as_deref().map(parse_ref) { Some(Some(rc)) => rc, - Some(None) => continue, - None => (r, next_col), + Some(None) => return Tag::Other, + None => (*cur_row, *next_col), }; - next_col = cc + 1; + *next_col = cc + 1; if cr >= MAX_ROWS || cc >= MAX_COLS { - continue; + return Tag::Other; + } + let style = unqualified_attr(e, reader, b"s") + .and_then(|s| s.parse::().ok()) + .unwrap_or(0); + let kind = match unqualified_attr(e, reader, b"t").as_deref().unwrap_or("n") { + "s" => CellKind::SharedStr, + "str" => CellKind::FormulaStr, + "inlineStr" => CellKind::InlineStr, + "b" => CellKind::Bool, + "e" | "d" => CellKind::Literal, + _ => CellKind::Number, + }; + cell.reset(cr, cc, style, kind); + Tag::Cell + } + Some(Tag::Cell) if sml => match name { + b"v" if !cell.has_v => { + cell.has_v = true; + Tag::Value + } + b"is" if !cell.has_is => { + cell.has_is = true; + Tag::InlineStr + } + _ => Tag::Other, + }, + Some(Tag::Value) => Tag::Value, + Some(Tag::InlineStr) if sml => match name { + b"t" => Tag::InlineText, + b"r" => { + cell.r_has_t = false; + Tag::RichRun } - let text = cell_text(c, shared, styles, date1904); - if !text.is_empty() { - out.cells.insert((cr, cc), text); + _ => Tag::Other, + }, + Some(Tag::RichRun) if sml && name == b"t" && !cell.r_has_t => { + cell.r_has_t = true; + Tag::InlineText + } + Some(Tag::InlineText) => Tag::InlineText, + Some(Tag::MergeCells) if sml && name == b"mergeCell" => { + match unqualified_attr(e, reader, b"ref").as_deref().and_then(parse_region) { + Some((r1, c1, r2, c2)) if r1 != r2 || c1 != c2 => { + out.merges.push((r1, c1, r2, c2)); + } + Some(_) => {} + None => log::debug!("skipping unparseable merge reference"), } + Tag::Other } + _ => Tag::Other, } - for merge in - worksheet.find_all(ns::SML, "mergeCells").flat_map(|mc| mc.find_all(ns::SML, "mergeCell")) - { - let Some(region) = merge.attr_unqualified("ref").and_then(parse_region) else { - log::debug!("skipping unparseable merge reference"); - continue; - }; - let (r1, c1, r2, c2) = region; - if r1 != r2 || c1 != c2 { - out.merges.push(region); - } +} + +fn active_text_buf(tag: Option, cell: &mut ActiveCell) -> Option<&mut String> { + match tag { + Some(Tag::Value) => Some(&mut cell.v_buf), + Some(Tag::InlineText) => Some(&mut cell.is_buf), + _ => None, } - out } /// A cell's rendered text, per its `t` type and resolved number format. -fn cell_text(c: &Element, shared: &[String], styles: &Styles, date1904: bool) -> String { - let fmt = styles.for_cell(c.attr_unqualified("s")); - let value = || c.find(ns::SML, "v").map(|v| v.text()).unwrap_or_default(); - match c.attr_unqualified("t").unwrap_or("n") { - "s" => { - let v = value(); - match v.trim().parse::().ok().and_then(|i| shared.get(i)) { - Some(text) => format_as_text(fmt, text), - None => { - log::debug!("shared string index {v:?} out of range"); - String::new() - } +fn render_cell_value( + cell: &ActiveCell, + shared: &[String], + styles: &Styles, + date1904: bool, +) -> String { + let fmt = styles.for_index(cell.style); + let v = cell.v_buf.as_str(); + match cell.kind { + CellKind::SharedStr => match v.trim().parse::().ok().and_then(|i| shared.get(i)) { + Some(text) => format_as_text(fmt, text), + None => { + log::debug!("shared string index {v:?} out of range"); + String::new() } + }, + CellKind::FormulaStr => format_as_text(fmt, &clean_text(v)), + CellKind::InlineStr => { + let text = if cell.has_is { clean_text(&cell.is_buf) } else { String::new() }; + format_as_text(fmt, &text) } - "str" => format_as_text(fmt, &clean_text(&value())), - "inlineStr" => { - let text = c.find(ns::SML, "is").map(|is| clean_text(&rich_text(is))); - format_as_text(fmt, &text.unwrap_or_default()) - } - "b" => match value().trim() { + CellKind::Bool => match v.trim() { "1" | "true" => "TRUE".to_string(), "0" | "false" => "FALSE".to_string(), _ => String::new(), }, - "e" | "d" => clean_text(&value()), - _ => { - let v = value(); + CellKind::Literal => clean_text(v), + CellKind::Number => { let v = v.trim(); if v.is_empty() { return String::new(); @@ -1078,4 +1364,138 @@ mod tests { let doc = parse(&wb.build()).unwrap(); assert_eq!(texts(first_table(&doc)), vec![vec!["14", "L/R [x] Roof", "[ ]"]]); } + + #[test] + fn whitespace_only_cells_do_not_widen_the_grid_extent() { + // Issue #156: single-space or NBSP cells outside the real table + // render empty and must not inflate the workbook's grid budget, + // while padding around real text stays intact. + let mut sheet = String::from( + r#"id padded "#, + ); + sheet.push_str( + r#"0 "#, + ); + let wb = Wb { + sheets: vec![("A", "", &sheet), ("B", "", &sheet)], + shared: Some(r#" "#), + ..Wb::default() + }; + let doc = parse(&wb.build()).unwrap(); + assert_eq!(texts(first_table(&doc)), vec![vec!["id", " padded "]]); + } + + #[test] + fn space_fill_dragged_past_the_dom_node_cap_streams_without_error() { + // Issue #156 (shape 2): a column of single-space shared-string cells + // filled down 550k rows (~2.2M XML nodes) exceeds MAX_XML_NODES when + // built into a DOM, yet holds only a two-cell table. + let mut sheet = String::with_capacity(28 * 1024 * 1024); + sheet.push_str( + r#"itemqtybolt4"#, + ); + for r in 3..=550_002u32 { + use std::fmt::Write as _; + let _ = write!(sheet, r#"0"#); + } + sheet.push_str(""); + let wb = Wb { + sheets: vec![("Tracker", "", &sheet)], + shared: Some(r#" "#), + ..Wb::default() + }; + let doc = parse(&wb.build()).unwrap(); + assert_eq!(texts(first_table(&doc)), vec![vec!["item", "qty"], vec!["bolt", "4"]]); + } + + #[test] + fn deeply_nested_worksheet_xml_still_trips_max_xml_depth() { + let depth = limits::MAX_XML_DEPTH + 2; + let mut body = String::from( + r#"ok"#, + ); + for _ in 0..depth { + body.push_str(""); + } + for _ in 0..depth { + body.push_str(""); + } + let err = parse(&one_sheet(&body).build()).unwrap_err(); + assert!( + matches!(err, ConvertError::ResourceLimit { limit: "max_xml_depth", .. }), + "got {err:?}" + ); + } + + #[test] + fn inline_phonetic_runs_are_omitted_and_strict_ooxml_worksheets_stream() { + let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let opts = zip::write::SimpleFileOptions::default(); + let mut add = |name: &str, body: &str| { + zip.start_file(name, opts).unwrap(); + zip.write_all(body.as_bytes()).unwrap(); + }; + add( + "xl/workbook.xml", + r#""#, + ); + add( + "xl/_rels/workbook.xml.rels", + &format!( + r#""# + ), + ); + add( + "xl/worksheets/sheet1.xml", + r#"東京とうきょう"#, + ); + let bytes = zip.finish().unwrap().into_inner(); + let doc = parse(&bytes).unwrap(); + assert_eq!(texts(first_table(&doc)), vec![vec!["東京"]]); + } + + #[test] + fn mismatched_closing_tags_are_rejected_and_shared_sheet_parts_evict_once() { + let bad = r#"wrong"#; + let err = parse(&one_sheet(bad).build()).unwrap_err(); + assert!(matches!(err, ConvertError::Malformed { .. }), "got {err:?}"); + + // Two workbook sheet entries pointing at the same worksheet part must + // both succeed before the part is evicted when its reference count hits 0. + let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let opts = zip::write::SimpleFileOptions::default(); + let mut add = |name: &str, body: &str| { + zip.start_file(name, opts).unwrap(); + zip.write_all(body.as_bytes()).unwrap(); + }; + add( + "xl/workbook.xml", + &format!( + r#""# + ), + ); + add( + "xl/_rels/workbook.xml.rels", + &format!( + r#""# + ), + ); + add( + "xl/worksheets/sheet1.xml", + &format!( + r#"shared"# + ), + ); + let bytes = zip.finish().unwrap().into_inner(); + let doc = parse(&bytes).unwrap(); + let tables: Vec<_> = doc + .blocks + .iter() + .filter_map(|b| match b { + Block::Table(t) => Some(texts(t)), + _ => None, + }) + .collect(); + assert_eq!(tables, vec![vec![vec!["shared"]], vec![vec!["shared"]]]); + } } diff --git a/src/package/archive.rs b/src/package/archive.rs index ce6637bd..c6a087a3 100644 --- a/src/package/archive.rs +++ b/src/package/archive.rs @@ -89,7 +89,12 @@ impl<'a> Package<'a> { Ok(Some(bytes)) } - /// Read a part that must exist for any meaningful output. + /// Drop a cached part buffer once a caller knows it will not be read + /// again, without rolling back `total_read`. + pub fn evict(&mut self, name: &str) { + self.cache.remove(name.trim_start_matches('/')); + } + /// True when a part exists, without reading (or budget-charging) it. pub fn has_part(&self, name: &str) -> bool { self.zip.index_for_name(name.trim_start_matches('/')).is_some() diff --git a/src/package/xml.rs b/src/package/xml.rs index 32ff65d8..a539a219 100644 --- a/src/package/xml.rs +++ b/src/package/xml.rs @@ -279,7 +279,7 @@ pub fn parse_xml(bytes: &[u8]) -> Result { } /// Transcode an XML part to UTF-8 based on its BOM or encoding declaration. -fn to_utf8(bytes: &[u8]) -> std::borrow::Cow<'_, [u8]> { +pub(crate) fn to_utf8(bytes: &[u8]) -> std::borrow::Cow<'_, [u8]> { use std::borrow::Cow; match bytes { [0xFF, 0xFE, ..] => { @@ -437,7 +437,7 @@ fn push_text(stack: &mut [Element], root: &mut Element, text: String) { } /// Resolve entity references quick-xml surfaces as GeneralRef events. -fn resolve_entity(name: &str) -> Option { +pub(crate) fn resolve_entity(name: &str) -> Option { if let Some(num) = name.strip_prefix('#') { let code = if let Some(hex) = num.strip_prefix('x').or_else(|| num.strip_prefix('X')) { u32::from_str_radix(hex, 16).ok()?