diff --git a/crates/xls/src/lib.rs b/crates/xls/src/lib.rs index adec09780..df29cd3ef 100644 --- a/crates/xls/src/lib.rs +++ b/crates/xls/src/lib.rs @@ -24,8 +24,8 @@ //! caps, formula nesting is bounded, and nothing indexes by a number from the file. //! //! ``` -//! # fn main() -> Result<(), craft_xls::Error> { -//! # let bytes = craft_xls::testing::workbook(&[("Sheet1", vec![vec!["a".into(), "2.5".into()]])]); +//! # fn main() -> Result<(), Box> { +//! # let bytes = craft_xls::testing::workbook(&[("Sheet1", vec![vec!["a".into(), "2.5".into()]])])?; //! let read = craft_xls::read(&bytes)?; //! let sheet = &read.workbook.sheets[0]; //! assert_eq!(sheet.name, "Sheet1"); diff --git a/crates/xls/src/testing.rs b/crates/xls/src/testing.rs index 16667d4a9..1aaaf347e 100644 --- a/crates/xls/src/testing.rs +++ b/crates/xls/src/testing.rs @@ -4,6 +4,10 @@ const BOF: u16 = 0x0809; const EOF: u16 = 0x000A; /// Characters kept from one string: 4,000 UTF-16 units and the 3-byte header fit one record. const MAX_STRING: usize = 4000; +/// The last zero-based row of the BIFF8 grid ([MS-XLS] 2.2.1). +const MAX_ROW: usize = 65_535; +/// The last zero-based column of the BIFF8 grid ([MS-XLS] 2.2.1). +const MAX_COLUMN: usize = 255; fn rec(out: &mut Vec, kind: u16, data: &[u8]) { out.extend_from_slice(&kind.to_le_bytes()); @@ -30,20 +34,57 @@ fn bof(dt: u16) -> Vec { d } -/// A compound file whose root holds one stream. -pub fn compound(stream: &str, data: &[u8]) -> Vec { - craft_cfb::write_streams(craft_cfb::Version::V3, &[(stream, data)]).unwrap_or_default() +/// A compound file whose root holds one stream. A stream the file cannot hold, such as one with +/// an empty name or a name longer than 31 characters, returns the writer's error. +pub fn compound(stream: &str, data: &[u8]) -> Result, craft_cfb::Error> { + craft_cfb::write_streams(craft_cfb::Version::V3, &[(stream, data)]) +} + +/// The XF index and value of a cell written as a Number record, or `None` for a cell written as +/// text: a cell `date:` takes the date XF when `` is a number, and any other cell +/// that is a number takes the general XF. +fn number(cell: &str) -> Option<(u16, f64)> { + match cell.strip_prefix("date:") { + Some(serial) => serial.parse().ok().map(|x| (1, x)), + None => cell.parse().ok().map(|x| (0, x)), + } } /// A minimal BIFF8 workbook: one worksheet per `(name, rows)`. Cells that read as numbers are /// Number records, a cell `date:` is a Number with the date format `m/d/yyyy` (built-in -/// format 14), an empty cell is left out, and everything else goes through the shared-string -/// table (as UTF-16 when it isn't Latin-1). +/// format 14), an empty cell is left out, and everything else, including a `date:` cell whose +/// serial is not a number, goes through the shared-string table (as UTF-16 when it isn't +/// Latin-1). A nonempty cell past row 65,535 or column 255 (zero-based) returns +/// [`craft_cfb::Error::Invalid`] naming its sheet, row and column, and a compound file that cannot +/// be written returns the writer's error. /// /// shortcut: characters outside the Basic Multilingual Plane are dropped, sheet names are cut at /// 255 characters and strings at 4,000 (so each fits one record); lift when a fixture needs more. -pub fn workbook(sheets: &[(&str, Vec>)]) -> Vec { +pub fn workbook(sheets: &[(&str, Vec>)]) -> Result, craft_cfb::Error> { let mut sst: Vec<&str> = Vec::new(); + let mut references = 0usize; + for (name, rows) in sheets { + for (r, row) in rows.iter().enumerate() { + for (c, v) in row.iter().enumerate() { + if v.is_empty() { + continue; + } + if r > MAX_ROW || c > MAX_COLUMN { + return Err(craft_cfb::Error::Invalid(format!( + "fixture cell on sheet {name:?} at zero-based row {r}, column {c} is outside the BIFF8 grid" + ))); + } + if number(v).is_none() { + references += 1; + if !sst.contains(&v.as_str()) { + sst.push(v); + } + } + } + } + } + let total = i32::try_from(references) + .map_err(|_| craft_cfb::Error::Invalid(format!("{references} shared-string references do not fit the SST record")))?; let mut out = Vec::new(); rec(&mut out, BOF, &bof(0x0005)); for ifmt in [0u16, 14] { @@ -61,13 +102,10 @@ pub fn workbook(sheets: &[(&str, Vec>)]) -> Vec { chars(&mut d, &name); rec(&mut out, 0x0085, &d); } - for c in sheets.iter().flat_map(|(_, rows)| rows.iter().flatten()) { - if c.parse::().is_err() && !c.starts_with("date:") && !sst.contains(&c.as_str()) { - sst.push(c); - } - } + // cstTotal is the number of LabelSst cells and cstUnique is the number of strings in the table + // ([MS-XLS] 2.4.265). let mut d = Vec::new(); - d.extend_from_slice(&(sst.len() as u32).to_le_bytes()); + d.extend_from_slice(&total.to_le_bytes()); d.extend_from_slice(&(sst.len() as u32).to_le_bytes()); let mut sst_records = vec![d]; for s in &sst { @@ -92,18 +130,17 @@ pub fn workbook(sheets: &[(&str, Vec>)]) -> Vec { let mut d = Vec::new(); d.extend_from_slice(&(r as u16).to_le_bytes()); d.extend_from_slice(&(c as u16).to_le_bytes()); - if let Some(x) = v.strip_prefix("date:").and_then(|x| x.parse::().ok()) { - d.extend_from_slice(&1u16.to_le_bytes()); - d.extend_from_slice(&x.to_le_bytes()); - rec(&mut out, 0x0203, &d); - } else if let Ok(x) = v.parse::() { - d.extend_from_slice(&0u16.to_le_bytes()); + if let Some((xf, x)) = number(v) { + d.extend_from_slice(&xf.to_le_bytes()); d.extend_from_slice(&x.to_le_bytes()); rec(&mut out, 0x0203, &d); } else { + let i = sst + .iter() + .position(|s| s == v) + .ok_or_else(|| craft_cfb::Error::Invalid(format!("the cell text {v:?} is not in the shared-string table")))?; d.extend_from_slice(&0u16.to_le_bytes()); - let i = sst.iter().position(|s| s == v).unwrap_or(0) as u32; - d.extend_from_slice(&i.to_le_bytes()); + d.extend_from_slice(&(i as u32).to_le_bytes()); rec(&mut out, 0x00FD, &d); } } diff --git a/crates/xls/tests/read.rs b/crates/xls/tests/read.rs index f7ea37fb0..f4360b5e0 100644 --- a/crates/xls/tests/read.rs +++ b/crates/xls/tests/read.rs @@ -621,7 +621,7 @@ fn the_fixture_writer_reads_back() { let long = "é".repeat(3000); let rows = vec![vec!["Name".to_string(), "Żółw".into(), String::new(), "2.5".into()], vec!["date:45000".into(), long.clone(), long.clone() + "x"]]; - let bytes = craft_xls::testing::workbook(&[("Erste", rows), ("Second", vec![vec!["Name".into()]])]); + let bytes = craft_xls::testing::workbook(&[("Erste", rows), ("Second", vec![vec!["Name".into()]])]).unwrap(); assert!(is_xls(&bytes)); let read = craft_xls::read(&bytes).unwrap(); assert_eq!(read.warnings, []); @@ -634,6 +634,86 @@ fn the_fixture_writer_reads_back() { assert_eq!(v(&wb, 1, "A1"), text("Name")); } +#[test] +fn a_fixture_that_cannot_be_written_returns_the_writer_error() { + assert_eq!(craft_xls::testing::compound("", b"x"), Err(craft_cfb::Error::BadName(String::new()))); + let long = "S".repeat(32); + assert_eq!(craft_xls::testing::compound(&long, b"x"), Err(craft_cfb::Error::BadName(long.clone()))); +} + +/// A nonempty cell outside rows 0 to 65,535 or columns 0 to 255 is an error naming the cell, and +/// the last row and column are written where they were asked for. +#[test] +fn the_fixture_writer_rejects_a_cell_outside_the_grid() { + let mut rows = vec![Vec::new(); 65_537]; + rows[0] = vec!["first".to_string()]; + rows[65_536] = vec!["wrapped".into()]; + match craft_xls::testing::workbook(&[("Tall", rows)]) { + Err(craft_cfb::Error::Invalid(m)) => assert!(m.contains("\"Tall\"") && m.contains("row 65536, column 0"), "{m}"), + other => panic!("{:?}", other.map(|b| b.len())), + } + let mut wide = vec![String::new(); 257]; + wide[0] = "first".into(); + wide[256] = "dropped".into(); + match craft_xls::testing::workbook(&[("Wide", vec![wide])]) { + Err(craft_cfb::Error::Invalid(m)) => assert!(m.contains("\"Wide\"") && m.contains("row 0, column 256"), "{m}"), + other => panic!("{:?}", other.map(|b| b.len())), + } + // An empty cell past the grid is not written, so it is not an error. + let mut rows = vec![Vec::new(); 65_537]; + rows[0] = vec!["first".to_string(), String::new()]; + rows[65_535] = vec![String::new(); 257]; + rows[65_535][255] = "last".into(); + rows[65_536] = vec![String::new()]; + let read = craft_xls::read(&craft_xls::testing::workbook(&[("Full", rows)]).unwrap()).unwrap(); + assert_eq!(read.warnings, []); + assert_eq!((v(&read.workbook, 0, "A1"), v(&read.workbook, 0, "IV65536")), (text("first"), text("last"))); +} + +/// The SST record's cstTotal and cstUnique fields ([MS-XLS] 2.4.265) in a fixture's Workbook stream. +fn sst_counts(file: &[u8]) -> (u32, u32) { + let stream = craft_cfb::CompoundFile::open(file).unwrap().read_path("Workbook").unwrap(); + let mut at = 0; + while let Some(head) = stream.get(at..at + 4) { + let kind = u16::from_le_bytes([head[0], head[1]]); + let len = usize::from(u16::from_le_bytes([head[2], head[3]])); + if kind == 0x00FC { + let d = &stream[at + 4..at + 12]; + return (u32::from_le_bytes([d[0], d[1], d[2], d[3]]), u32::from_le_bytes([d[4], d[5], d[6], d[7]])); + } + at += 4 + len; + } + panic!("no SST record"); +} + +/// cstTotal is the number of cells that refer to the shared-string table, and cstUnique is the +/// number of strings in it. +#[test] +fn the_fixture_writer_counts_every_shared_string_reference() { + let row = |cells: &[&str]| vec![cells.iter().map(|s| s.to_string()).collect::>()]; + let one = craft_xls::testing::workbook(&[("S", row(&["same", "same"]))]).unwrap(); + assert_eq!(sst_counts(&one), (2, 1)); + let two = craft_xls::testing::workbook(&[("S", row(&["same"])), ("T", row(&["same"]))]).unwrap(); + assert_eq!(sst_counts(&two), (2, 1)); + let none = craft_xls::testing::workbook(&[("S", row(&["1", "", "date:45000"]))]).unwrap(); + assert_eq!(sst_counts(&none), (0, 0)); + let wb = craft_xls::read(&two).unwrap().workbook; + assert_eq!((v(&wb, 0, "A1"), v(&wb, 1, "A1")), (text("same"), text("same"))); +} + +/// A `date:` cell whose serial is not a number is text, whether the shared-string table holds +/// other text before it, an empty cell, or nothing else. +#[test] +fn the_fixture_writer_keeps_a_date_cell_without_a_serial_as_text() { + for (first, value) in [("first", text("first")), ("", Value::Empty), ("1", Value::Number(1.0))] { + let rows = vec![vec![first.to_string(), "date:abc".into(), "date:".into()]]; + let read = craft_xls::read(&craft_xls::testing::workbook(&[("S", rows)]).unwrap()).unwrap(); + assert_eq!(read.warnings, [], "{first:?}"); + let wb = read.workbook; + assert_eq!((v(&wb, 0, "A1"), v(&wb, 0, "B1"), v(&wb, 0, "C1")), (value, text("date:abc"), text("date:")), "{first:?}"); + } +} + #[test] fn cell_sheet_and_merge_caps_are_enforced() { let number = |r: u16| rec(0x0203, &cat(&[&cell(r, 0, 0), &1.0f64.to_le_bytes()]));