From 1bdaa7aff32035ca09a178d296b082a53366b3b7 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Thu, 12 Nov 2026 23:23:02 -0500 Subject: [PATCH 01/14] craft-xlsx: the XLSB function table, with the numbers 0x17C-0x1E4 (port of gridcraft#3810) [MS-XLSB]'s Ftab (2.5.98.10) continues the [MS-XLS] table from 0x17C: the cube functions, the old Analysis ToolPak functions, IFERROR, COUNTIFS, SUMIFS and AVERAGEIF(S). Excel writes these by number in .xlsb (PtgFunc / PtgFuncVar) where .xls calls them by name, so an XLSB formula decoder needs them. Generated from the spec with the same argument-count rule as the existing entries. Ported from storytold/gridcraft#3810 (issue gridcraft#3806). There the entries were appended to GridCraft's .xls table, which is now craft-xls's. Here the table is craft-xlsx's own (xlsb/ftab.rs): craft-xls's 0-379, which match GridCraft's table before the PR, then the PR's entries. The two formats follow separate specifications and develop separately; they can share a table later if both readers settle on one. The PR's table checks come along. Its decoding checks (IFERROR, SUMIFS by number) go with the decoder in the next commit, and its check that every new name is a GridCraft function stays in GridCraft. --- crates/xlsx/src/xlsb.rs | 2 + crates/xlsx/src/xlsb/ftab.rs | 516 +++++++++++++++++++++++++++++++++++ 2 files changed, 518 insertions(+) create mode 100644 crates/xlsx/src/xlsb/ftab.rs diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index dbff61dd7..542fd580c 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -2,6 +2,8 @@ //! their records. Saved values only: formulas are token streams that are not decoded, and //! styles are not read. A record that does not parse fails the read. +mod ftab; + use std::sync::Arc; use craft_opc::{ContentTypes, Rels}; diff --git a/crates/xlsx/src/xlsb/ftab.rs b/crates/xlsx/src/xlsb/ftab.rs new file mode 100644 index 000000000..c6b59092a --- /dev/null +++ b/crates/xlsx/src/xlsb/ftab.rs @@ -0,0 +1,516 @@ +//! The XLSB function table ([MS-XLSB] 2.5.98.10, `Ftab`): the functions a `PtgFunc` or +//! `PtgFuncVar` token calls by number. Up to 379 (0x17B) it is the table of [MS-XLS] 2.5.198.17, +//! which craft-xls also carries. The entries from 380 (0x17C) on are the rest of the XLSB table: +//! XLSB files call these functions by number, where `.xls` files call them by name. Kept apart from +//! craft-xls's copy: the two formats follow separate specifications. + +/// (iftab, name, fixed argument count or -1 when the function takes a variable number of +/// arguments), sorted by number. +const FTAB: &[(u16, &str, i8)] = &[ + (0, "COUNT", -1), + (1, "IF", -1), + (2, "ISNA", 1), + (3, "ISERROR", 1), + (4, "SUM", -1), + (5, "AVERAGE", -1), + (6, "MIN", -1), + (7, "MAX", -1), + (8, "ROW", -1), + (9, "COLUMN", -1), + (10, "NA", 0), + (11, "NPV", -1), + (12, "STDEV", -1), + (13, "DOLLAR", -1), + (14, "FIXED", -1), + (15, "SIN", 1), + (16, "COS", 1), + (17, "TAN", 1), + (18, "ATAN", 1), + (19, "PI", 0), + (20, "SQRT", 1), + (21, "EXP", 1), + (22, "LN", 1), + (23, "LOG10", 1), + (24, "ABS", 1), + (25, "INT", 1), + (26, "SIGN", 1), + (27, "ROUND", 2), + (28, "LOOKUP", -1), + (29, "INDEX", -1), + (30, "REPT", 2), + (31, "MID", 3), + (32, "LEN", 1), + (33, "VALUE", 1), + (34, "TRUE", 0), + (35, "FALSE", 0), + (36, "AND", -1), + (37, "OR", -1), + (38, "NOT", 1), + (39, "MOD", 2), + (40, "DCOUNT", 3), + (41, "DSUM", 3), + (42, "DAVERAGE", 3), + (43, "DMIN", 3), + (44, "DMAX", 3), + (45, "DSTDEV", 3), + (46, "VAR", -1), + (47, "DVAR", 3), + (48, "TEXT", 2), + (49, "LINEST", -1), + (50, "TREND", -1), + (51, "LOGEST", -1), + (52, "GROWTH", -1), + (53, "GOTO", 1), + (54, "HALT", -1), + (55, "RETURN", -1), + (56, "PV", -1), + (57, "FV", -1), + (58, "NPER", -1), + (59, "PMT", -1), + (60, "RATE", -1), + (61, "MIRR", 3), + (62, "IRR", -1), + (63, "RAND", 0), + (64, "MATCH", -1), + (65, "DATE", 3), + (66, "TIME", 3), + (67, "DAY", 1), + (68, "MONTH", 1), + (69, "YEAR", 1), + (70, "WEEKDAY", -1), + (71, "HOUR", 1), + (72, "MINUTE", 1), + (73, "SECOND", 1), + (74, "NOW", 0), + (75, "AREAS", 1), + (76, "ROWS", 1), + (77, "COLUMNS", 1), + (78, "OFFSET", -1), + (79, "ABSREF", 2), + (80, "RELREF", 2), + (81, "ARGUMENT", -1), + (82, "SEARCH", -1), + (83, "TRANSPOSE", 1), + (84, "ERROR", -1), + (85, "STEP", 0), + (86, "TYPE", 1), + (87, "ECHO", -1), + (88, "SET.NAME", -1), + (89, "CALLER", 0), + (90, "DEREF", 1), + (91, "WINDOWS", -1), + (92, "SERIES", -1), + (93, "DOCUMENTS", -1), + (94, "ACTIVE.CELL", 0), + (95, "SELECTION", 0), + (96, "RESULT", -1), + (97, "ATAN2", 2), + (98, "ASIN", 1), + (99, "ACOS", 1), + (100, "CHOOSE", -1), + (101, "HLOOKUP", -1), + (102, "VLOOKUP", -1), + (103, "LINKS", -1), + (104, "INPUT", -1), + (105, "ISREF", 1), + (106, "GET.FORMULA", 1), + (107, "GET.NAME", -1), + (108, "SET.VALUE", 2), + (109, "LOG", -1), + (110, "EXEC", -1), + (111, "CHAR", 1), + (112, "LOWER", 1), + (113, "UPPER", 1), + (114, "PROPER", 1), + (115, "LEFT", -1), + (116, "RIGHT", -1), + (117, "EXACT", 2), + (118, "TRIM", 1), + (119, "REPLACE", 4), + (120, "SUBSTITUTE", -1), + (121, "CODE", 1), + (122, "NAMES", -1), + (123, "DIRECTORY", -1), + (124, "FIND", -1), + (125, "CELL", -1), + (126, "ISERR", 1), + (127, "ISTEXT", 1), + (128, "ISNUMBER", 1), + (129, "ISBLANK", 1), + (130, "T", 1), + (131, "N", 1), + (132, "FOPEN", -1), + (133, "FCLOSE", 1), + (134, "FSIZE", 1), + (135, "FREADLN", 1), + (136, "FREAD", 2), + (137, "FWRITELN", 2), + (138, "FWRITE", 2), + (139, "FPOS", -1), + (140, "DATEVALUE", 1), + (141, "TIMEVALUE", 1), + (142, "SLN", 3), + (143, "SYD", 4), + (144, "DDB", -1), + (145, "GET.DEF", -1), + (146, "REFTEXT", -1), + (147, "TEXTREF", -1), + (148, "INDIRECT", -1), + (149, "REGISTER", -1), + (150, "CALL", -1), + (151, "ADD.BAR", -1), + (152, "ADD.MENU", -1), + (153, "ADD.COMMAND", -1), + (154, "ENABLE.COMMAND", -1), + (155, "CHECK.COMMAND", -1), + (156, "RENAME.COMMAND", -1), + (157, "SHOW.BAR", -1), + (158, "DELETE.MENU", -1), + (159, "DELETE.COMMAND", -1), + (160, "GET.CHART.ITEM", -1), + (161, "DIALOG.BOX", 1), + (162, "CLEAN", 1), + (163, "MDETERM", 1), + (164, "MINVERSE", 1), + (165, "MMULT", 2), + (166, "FILES", -1), + (167, "IPMT", -1), + (168, "PPMT", -1), + (169, "COUNTA", -1), + (170, "CANCEL.KEY", -1), + (171, "FOR", -1), + (172, "WHILE", 1), + (173, "BREAK", 0), + (174, "NEXT", 0), + (175, "INITIATE", 2), + (176, "REQUEST", 2), + (177, "POKE", 3), + (178, "EXECUTE", 2), + (179, "TERMINATE", 1), + (180, "RESTART", -1), + (181, "HELP", -1), + (182, "GET.BAR", -1), + (183, "PRODUCT", -1), + (184, "FACT", 1), + (185, "GET.CELL", -1), + (186, "GET.WORKSPACE", 1), + (187, "GET.WINDOW", -1), + (188, "GET.DOCUMENT", -1), + (189, "DPRODUCT", 3), + (190, "ISNONTEXT", 1), + (191, "GET.NOTE", -1), + (192, "NOTE", -1), + (193, "STDEVP", -1), + (194, "VARP", -1), + (195, "DSTDEVP", 3), + (196, "DVARP", 3), + (197, "TRUNC", -1), + (198, "ISLOGICAL", 1), + (199, "DCOUNTA", 3), + (200, "DELETE.BAR", 1), + (201, "UNREGISTER", 1), + (204, "USDOLLAR", -1), + (205, "FINDB", -1), + (206, "SEARCHB", -1), + (207, "REPLACEB", 4), + (208, "LEFTB", -1), + (209, "RIGHTB", -1), + (210, "MIDB", 3), + (211, "LENB", 1), + (212, "ROUNDUP", 2), + (213, "ROUNDDOWN", 2), + (214, "ASC", 1), + (215, "DBCS", 1), + (216, "RANK", -1), + (219, "ADDRESS", -1), + (220, "DAYS360", -1), + (221, "TODAY", 0), + (222, "VDB", -1), + (223, "ELSE", 0), + (224, "ELSE.IF", 1), + (225, "END.IF", 0), + (226, "FOR.CELL", -1), + (227, "MEDIAN", -1), + (228, "SUMPRODUCT", -1), + (229, "SINH", 1), + (230, "COSH", 1), + (231, "TANH", 1), + (232, "ASINH", 1), + (233, "ACOSH", 1), + (234, "ATANH", 1), + (235, "DGET", 3), + (236, "CREATE.OBJECT", -1), + (237, "VOLATILE", -1), + (238, "LAST.ERROR", 0), + (239, "CUSTOM.UNDO", -1), + (240, "CUSTOM.REPEAT", -1), + (241, "FORMULA.CONVERT", -1), + (242, "GET.LINK.INFO", -1), + (243, "TEXT.BOX", -1), + (244, "INFO", 1), + (245, "GROUP", 0), + (246, "GET.OBJECT", -1), + (247, "DB", -1), + (248, "PAUSE", -1), + (251, "RESUME", -1), + (252, "FREQUENCY", 2), + (253, "ADD.TOOLBAR", -1), + (254, "DELETE.TOOLBAR", 1), + (256, "RESET.TOOLBAR", 1), + (257, "EVALUATE", 1), + (258, "GET.TOOLBAR", -1), + (259, "GET.TOOL", -1), + (260, "SPELLING.CHECK", -1), + (261, "ERROR.TYPE", 1), + (262, "APP.TITLE", -1), + (263, "WINDOW.TITLE", -1), + (264, "SAVE.TOOLBAR", -1), + (265, "ENABLE.TOOL", 3), + (266, "PRESS.TOOL", 3), + (267, "REGISTER.ID", -1), + (268, "GET.WORKBOOK", -1), + (269, "AVEDEV", -1), + (270, "BETADIST", -1), + (271, "GAMMALN", 1), + (272, "BETAINV", -1), + (273, "BINOMDIST", 4), + (274, "CHIDIST", 2), + (275, "CHIINV", 2), + (276, "COMBIN", 2), + (277, "CONFIDENCE", 3), + (278, "CRITBINOM", 3), + (279, "EVEN", 1), + (280, "EXPONDIST", 3), + (281, "FDIST", 3), + (282, "FINV", 3), + (283, "FISHER", 1), + (284, "FISHERINV", 1), + (285, "FLOOR", 2), + (286, "GAMMADIST", 4), + (287, "GAMMAINV", 3), + (288, "CEILING", 2), + (289, "HYPGEOMDIST", 4), + (290, "LOGNORMDIST", 3), + (291, "LOGINV", 3), + (292, "NEGBINOMDIST", 3), + (293, "NORMDIST", 4), + (294, "NORMSDIST", 1), + (295, "NORMINV", 3), + (296, "NORMSINV", 1), + (297, "STANDARDIZE", 3), + (298, "ODD", 1), + (299, "PERMUT", 2), + (300, "POISSON", 3), + (301, "TDIST", 3), + (302, "WEIBULL", 4), + (303, "SUMXMY2", 2), + (304, "SUMX2MY2", 2), + (305, "SUMX2PY2", 2), + (306, "CHITEST", 2), + (307, "CORREL", 2), + (308, "COVAR", 2), + (309, "FORECAST", 3), + (310, "FTEST", 2), + (311, "INTERCEPT", 2), + (312, "PEARSON", 2), + (313, "RSQ", 2), + (314, "STEYX", 2), + (315, "SLOPE", 2), + (316, "TTEST", 4), + (317, "PROB", -1), + (318, "DEVSQ", -1), + (319, "GEOMEAN", -1), + (320, "HARMEAN", -1), + (321, "SUMSQ", -1), + (322, "KURT", -1), + (323, "SKEW", -1), + (324, "ZTEST", -1), + (325, "LARGE", 2), + (326, "SMALL", 2), + (327, "QUARTILE", 2), + (328, "PERCENTILE", 2), + (329, "PERCENTRANK", -1), + (330, "MODE", -1), + (331, "TRIMMEAN", 2), + (332, "TINV", 2), + (334, "MOVIE.COMMAND", -1), + (335, "GET.MOVIE", -1), + (336, "CONCATENATE", -1), + (337, "POWER", 2), + (338, "PIVOT.ADD.DATA", -1), + (339, "GET.PIVOT.TABLE", -1), + (340, "GET.PIVOT.FIELD", -1), + (341, "GET.PIVOT.ITEM", -1), + (342, "RADIANS", 1), + (343, "DEGREES", 1), + (344, "SUBTOTAL", -1), + (345, "SUMIF", -1), + (346, "COUNTIF", 2), + (347, "COUNTBLANK", 1), + (348, "SCENARIO.GET", -1), + (349, "OPTIONS.LISTS.GET", 1), + (350, "ISPMT", 4), + (351, "DATEDIF", 3), + (352, "DATESTRING", 1), + (353, "NUMBERSTRING", 2), + (354, "ROMAN", -1), + (355, "OPEN.DIALOG", -1), + (356, "SAVE.DIALOG", -1), + (357, "VIEW.GET", -1), + (358, "GETPIVOTDATA", -1), + (359, "HYPERLINK", -1), + (360, "PHONETIC", 1), + (361, "AVERAGEA", -1), + (362, "MAXA", -1), + (363, "MINA", -1), + (364, "STDEVPA", -1), + (365, "VARPA", -1), + (366, "STDEVA", -1), + (367, "VARA", -1), + (368, "BAHTTEXT", 1), + (369, "THAIDAYOFWEEK", 1), + (370, "THAIDIGIT", 1), + (371, "THAIMONTHOFYEAR", 1), + (372, "THAINUMSOUND", 1), + (373, "THAINUMSTRING", 1), + (374, "THAISTRINGLENGTH", 1), + (375, "ISTHAIDIGIT", 1), + (376, "ROUNDBAHTDOWN", 1), + (377, "ROUNDBAHTUP", 1), + (378, "THAIYEAR", 1), + (379, "RTD", -1), + (380, "CUBEVALUE", -1), + (381, "CUBEMEMBER", -1), + (382, "CUBEMEMBERPROPERTY", 3), + (383, "CUBERANKEDMEMBER", -1), + (384, "HEX2BIN", -1), + (385, "HEX2DEC", 1), + (386, "HEX2OCT", -1), + (387, "DEC2BIN", -1), + (388, "DEC2HEX", -1), + (389, "DEC2OCT", -1), + (390, "OCT2BIN", -1), + (391, "OCT2HEX", -1), + (392, "OCT2DEC", 1), + (393, "BIN2DEC", 1), + (394, "BIN2OCT", -1), + (395, "BIN2HEX", -1), + (396, "IMSUB", 2), + (397, "IMDIV", 2), + (398, "IMPOWER", 2), + (399, "IMABS", 1), + (400, "IMSQRT", 1), + (401, "IMLN", 1), + (402, "IMLOG2", 1), + (403, "IMLOG10", 1), + (404, "IMSIN", 1), + (405, "IMCOS", 1), + (406, "IMEXP", 1), + (407, "IMARGUMENT", 1), + (408, "IMCONJUGATE", 1), + (409, "IMAGINARY", 1), + (410, "IMREAL", 1), + (411, "COMPLEX", -1), + (412, "IMSUM", -1), + (413, "IMPRODUCT", -1), + (414, "SERIESSUM", 4), + (415, "FACTDOUBLE", 1), + (416, "SQRTPI", 1), + (417, "QUOTIENT", 2), + (418, "DELTA", -1), + (419, "GESTEP", -1), + (420, "ISEVEN", 1), + (421, "ISODD", 1), + (422, "MROUND", 2), + (423, "ERF", -1), + (424, "ERFC", 1), + (425, "BESSELJ", 2), + (426, "BESSELK", 2), + (427, "BESSELY", 2), + (428, "BESSELI", 2), + (429, "XIRR", -1), + (430, "XNPV", 3), + (431, "PRICEMAT", -1), + (432, "YIELDMAT", -1), + (433, "INTRATE", -1), + (434, "RECEIVED", -1), + (435, "DISC", -1), + (436, "PRICEDISC", -1), + (437, "YIELDDISC", -1), + (438, "TBILLEQ", 3), + (439, "TBILLPRICE", 3), + (440, "TBILLYIELD", 3), + (441, "PRICE", -1), + (442, "YIELD", -1), + (443, "DOLLARDE", 2), + (444, "DOLLARFR", 2), + (445, "NOMINAL", 2), + (446, "EFFECT", 2), + (447, "CUMPRINC", 6), + (448, "CUMIPMT", 6), + (449, "EDATE", 2), + (450, "EOMONTH", 2), + (451, "YEARFRAC", -1), + (452, "COUPDAYBS", -1), + (453, "COUPDAYS", -1), + (454, "COUPDAYSNC", -1), + (455, "COUPNCD", -1), + (456, "COUPNUM", -1), + (457, "COUPPCD", -1), + (458, "DURATION", -1), + (459, "MDURATION", -1), + (460, "ODDLPRICE", -1), + (461, "ODDLYIELD", -1), + (462, "ODDFPRICE", -1), + (463, "ODDFYIELD", -1), + (464, "RANDBETWEEN", 2), + (465, "WEEKNUM", -1), + (466, "AMORDEGRC", -1), + (467, "AMORLINC", -1), + (469, "ACCRINT", -1), + (470, "ACCRINTM", -1), + (471, "WORKDAY", -1), + (472, "NETWORKDAYS", -1), + (473, "GCD", -1), + (474, "MULTINOMIAL", -1), + (475, "LCM", -1), + (476, "FVSCHEDULE", 2), + (477, "CUBEKPIMEMBER", -1), + (478, "CUBESET", -1), + (479, "CUBESETCOUNT", 1), + (480, "IFERROR", 2), + (481, "COUNTIFS", -1), + (482, "SUMIFS", -1), + (483, "AVERAGEIF", -1), + (484, "AVERAGEIFS", -1), +]; + +/// The function a `PtgFunc` or `PtgFuncVar` calls: its name and fixed argument count (-1 when it +/// takes a variable number). `None` for a number the table doesn't assign. +// The formula decoder calls it. +#[allow(dead_code)] +pub(super) fn function(iftab: u16) -> Option<(&'static str, i8)> { + FTAB.binary_search_by_key(&iftab, |(i, _, _)| *i).ok().and_then(|i| FTAB.get(i)).map(|(_, n, a)| (*n, *a)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn xlsb_function_numbers() { + // `function` binary-searches the table: sorted, each number once. + assert!(FTAB.windows(2).all(|w| w[0].0 < w[1].0)); + // The [MS-XLS] part, unchanged. + assert_eq!(function(0), Some(("COUNT", -1))); + assert_eq!(function(379), Some(("RTD", -1))); + // [MS-XLSB] 2.5.98.10 continues the table from 0x17C. Excel writes these by number in + // XLSB: PtgFunc for a fixed argument count, PtgFuncVar otherwise. + assert_eq!(function(0x17C), Some(("CUBEVALUE", -1))); + assert_eq!(function(449), Some(("EDATE", 2))); + assert_eq!(function(480), Some(("IFERROR", 2))); + assert_eq!(function(482), Some(("SUMIFS", -1))); + assert_eq!(function(0x1D4), None, "unassigned"); + assert_eq!(function(0x1E5), None); + } +} From 787b3e44f49541b2b34bad86b296edb6f1aa6b5e Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Thu, 12 Nov 2026 23:29:29 -0500 Subject: [PATCH 02/14] craft-xlsx: read cell formulas from .xlsb files (port of gridcraft#3818) A BIFF12 decoder for cell formulas (xlsb/formula.rs), separate from craft-xls's BIFF8 one because nearly every operand differs (32-bit rows, 14-bit columns, UTF-16 strings, its own array-constant codes, newer functions called through hidden names). It reads references, 3-D references, constants, array constants, functions by number (xlsb/ftab.rs) and newer functions through Excel's hidden _xlfn. names. A formula cell's CellFormula holds the text as an .xlsx file holds it (`_xlfn.CONCAT`, `_xlpm.x`, no implicit `@`), and a BrtCellMeta before a cell gives it cell_metadata, as `cm` does. Reverse-Polish tokens group by order alone, so the decoded tree gets the parentheses its text needs from operator precedence; Excel's own text is unchanged. A chain of string constants joined with & prints as one flat chain. What a formula refers to must be readable too, so shared, array and data-table forms, user defined names (XLSB names aren't read yet), table references, external links and PY(index, ...) (code stored elsewhere in the package) keep only their saved results, with a warning saying why. So does a formula past Limits::binary_formula_text (128 MiB of text over the workbook). The workbook's links and names are optional: if they can't be read, the file opens with values only, as before, and a warning says so. Ported from storytold/gridcraft#3818 (issue gridcraft#3806), with its two review fixes (grouping by precedence, the flat & chain). Changed from the GridCraft PR: - The tree and its A1 printer (xlsb/expr.rs) are a copy of craft-xls's expr.rs, with BinOp::precedence added; the decoder's stack and depth guards are copies of craft-xls's. The PR used GridCraft's formula crate and shared these with GridCraft's .xls decoder. - No fmla::from_file, implicit `@`, parse check or Python-cell adoption: those are GridCraft's reading of the text, not the file's. A single-cell dynamic-array formula is read (with its cell metadata) instead of being kept as its value; the PR left dynamic arrays to the next PR. - The links are read with the workbook part and kept by Package for its sheets. Its tests come along in tests/xlsb.rs, with the expected text in the file's spelling. Checked against Excel: of the formulas in a workbook Excel saved both as .xlsb and as .xlsx, every one this reads (26) has the text of the .xlsx; the other 12 are the kinds above. --- crates/xlsx/src/lib.rs | 5 + crates/xlsx/src/package.rs | 15 +- crates/xlsx/src/xlsb.rs | 158 +++++++++- crates/xlsx/src/xlsb/expr.rs | 345 ++++++++++++++++++++ crates/xlsx/src/xlsb/formula.rs | 538 ++++++++++++++++++++++++++++++++ crates/xlsx/src/xlsb/ftab.rs | 2 - crates/xlsx/tests/common/mod.rs | 19 +- crates/xlsx/tests/xlsb.rs | 267 ++++++++++++++++ 8 files changed, 1329 insertions(+), 20 deletions(-) create mode 100644 crates/xlsx/src/xlsb/expr.rs create mode 100644 crates/xlsx/src/xlsb/formula.rs diff --git a/crates/xlsx/src/lib.rs b/crates/xlsx/src/lib.rs index b0771d366..5a64ae0ab 100644 --- a/crates/xlsx/src/lib.rs +++ b/crates/xlsx/src/lib.rs @@ -145,6 +145,10 @@ pub struct Limits { pub binary_cells: usize, /// Bytes of decoded text (shared and inline strings) in a binary workbook, over all its parts. pub binary_text: usize, + /// Bytes of decoded formula text in a binary workbook, over all its sheets. A cell formula + /// may hold 16 KB of tokens, so without a total a million cells of long formulas would cost + /// gigabytes; past it, formulas keep their saved results (the file still opens). + pub binary_formula_text: usize, } impl Default for Limits { @@ -156,6 +160,7 @@ impl Default for Limits { binary_records: 40_000_000, binary_cells: 4_000_000, binary_text: 256 << 20, + binary_formula_text: 128 << 20, } } } diff --git a/crates/xlsx/src/package.rs b/crates/xlsx/src/package.rs index 1c133d0ad..8f2c5efc6 100644 --- a/crates/xlsx/src/package.rs +++ b/crates/xlsx/src/package.rs @@ -29,6 +29,9 @@ pub struct Package<'a> { binary: Option, workbook_rels: Option, budget: xlsb::Budget, + /// What a binary workbook's formulas refer to, once its workbook part is read: `Some(None)` + /// when that couldn't be read (every formula keeps its saved result). + binary_links: Option>, /// Shared strings in a binary workbook, once counted: a cell's index is checked against it. binary_strings: Option, } @@ -67,6 +70,7 @@ impl<'a> Package<'a> { binary, workbook_rels: None, budget: xlsb::Budget::default(), + binary_links: None, binary_strings: None, }) } @@ -145,7 +149,9 @@ impl<'a> Package<'a> { if self.binary.is_some() { let bytes = self.required(&self.workbook_part.clone())?; let rels = self.workbook_rels()?; - return xlsb::workbook(&bytes, &rels, &self.limits, &self.budget, &mut self.warnings); + let (wb, links) = xlsb::workbook(&bytes, &rels, &self.limits, &self.budget, &mut self.warnings)?; + self.binary_links = Some(links); + return Ok(wb); } let options = self.xml_options(); let root = match self.opc.read_xml(&self.workbook_part, &options)? { @@ -234,8 +240,13 @@ impl<'a> Package<'a> { Some(n) => n, None => self.shared_strings()?.len(), }; + // Formulas name sheets and functions through the workbook part. + if self.binary_links.is_none() { + self.workbook()?; + } let bytes = self.required(&part)?; - let mut sheet = xlsb::worksheet(&bytes, strings, &self.limits, &self.budget, &mut self.warnings, &mut each)?; + let links = self.binary_links.as_ref().and_then(Option::as_ref); + let mut sheet = xlsb::worksheet(&bytes, strings, links, &self.limits, &self.budget, &mut self.warnings, &mut each)?; sheet.part = part; return Ok(sheet); } diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index 542fd580c..aedbae5ab 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -1,7 +1,10 @@ //! Binary workbooks ([MS-XLSB]): the workbook part, shared strings and worksheet cells, from -//! their records. Saved values only: formulas are token streams that are not decoded, and -//! styles are not read. A record that does not parse fails the read. +//! their records. Cell formulas are decoded from their tokens ([`formula`]) into the text an +//! `.xlsx` file holds; one that can't be keeps only its saved result, with a warning saying why. +//! Styles are not read. A record that does not parse fails the read. +mod expr; +mod formula; mod ftab; use std::sync::Arc; @@ -9,10 +12,12 @@ use std::sync::Arc; use craft_opc::{ContentTypes, Rels}; use craft_sheet::{CellError, CellRef, MAX_COLS, MAX_ROWS, Range, Visibility}; -use crate::sheet::{Cell, CellValue, Row, SheetPart}; +use crate::sheet::{Cell, CellFormula, CellValue, Row, SheetPart}; use crate::strings::StringItem; use crate::workbook::{SheetEntry, SheetKind, Workbook}; use crate::{Error, Limits, Warnings}; +pub(crate) use formula::Links; +use formula::{Book, Name, Xti}; pub(crate) const MAIN: &str = "application/vnd.ms-excel.main"; const BINARY_MAIN: &str = "application/vnd.ms-excel.sheet.binary.macroEnabled.main"; @@ -22,6 +27,9 @@ pub(crate) const STRINGS: &str = "application/vnd.ms-excel.sharedStrings"; const MAX_SHEETS: usize = 1024; /// Most merged ranges in one worksheet. const MAX_MERGES: usize = 100_000; +/// Defined names (Excel's hidden ones for newer functions included) read at most; past it, the +/// workbook's formulas keep their saved results. +const MAX_NAMES: usize = 65_536; pub(crate) fn invalid(msg: impl Into) -> Error { Error::Invalid(format!("XLSB: {}", msg.into())) @@ -43,9 +51,21 @@ pub(crate) struct Budget { cells: std::cell::Cell, text: std::cell::Cell, records: std::cell::Cell, + formula_text: std::cell::Cell, } impl Budget { + /// Charges a decoded formula's text if it fits [`Limits::binary_formula_text`]; a formula + /// that doesn't keeps its saved result. + fn try_formula_text(&self, len: usize, limits: &Limits) -> bool { + let total = self.formula_text.get().saturating_add(len); + let fits = total <= limits.binary_formula_text; + if fits { + self.formula_text.set(total); + } + fits + } + fn text(&self, len: usize, limits: &Limits) -> Result<(), Error> { self.text.set(self.text.get().saturating_add(len)); if self.text.get() > limits.binary_text { @@ -97,6 +117,9 @@ impl<'a> Bytes<'a> { fn u32(&mut self) -> Result { Ok(u32::from_le_bytes(self.take(4)?.try_into().map_err(|_| invalid("truncated u32"))?)) } + fn i32(&mut self) -> Result { + Ok(i32::from_le_bytes(self.take(4)?.try_into().map_err(|_| invalid("truncated i32"))?)) + } fn f64(&mut self) -> Result { let n = f64::from_le_bytes(self.take(8)?.try_into().map_err(|_| invalid("truncated number"))?); @@ -224,10 +247,19 @@ impl<'a, 'l> Records<'a, 'l> { } /// The workbook part: BrtBeginBook, BrtWbProp, the BrtBundleSh list. -pub(crate) fn workbook(bytes: &[u8], rels: &Rels, limits: &Limits, budget: &Budget, warnings: &mut Warnings) -> Result { +/// The workbook part, and the tables its formulas refer to ([`Links`]): `None` when those can't +/// be read, so that the workbook still opens, with every formula kept as its saved result. +pub(crate) fn workbook( + bytes: &[u8], + rels: &Rels, + limits: &Limits, + budget: &Budget, + warnings: &mut Warnings, +) -> Result<(Workbook, Option), Error> { let mut records = Records::new(bytes, limits, budget); records.first(131, warnings)?; let mut wb = Workbook::default(); + let mut links = Some(Links::default()); let mut bundle = false; let mut bundle_seen = false; while let Some((id, payload)) = records.next(warnings)? { @@ -241,7 +273,17 @@ pub(crate) fn workbook(bytes: &[u8], rels: &Rels, limits: &Limits, budget: &Budg if wb.sheets.is_empty() { return Err(invalid("no supported worksheets")); } - return Ok(wb); + if links.is_none() { + warnings.warn("XLSB: the workbook's sheet links or names could not be read; formulas keep their saved results"); + } + return Ok((wb, links)); + } + 39 | 355 | 357 | 358 | 362 | 667 => { + if let Some(l) = links.as_mut() + && link_record(id, payload, l).is_err() + { + links = None; + } } 143 => { if bundle_seen { @@ -289,6 +331,10 @@ pub(crate) fn workbook(bytes: &[u8], rels: &Rels, limits: &Limits, budget: &Budg return Err(invalid("worksheet relationship is null")); } warnings.warn("XLSB module sheets were skipped"); + // Formulas name sheets by their place in this list, skipped sheets included. + if let Some(l) = links.as_mut() { + l.sheets.push(None); + } continue; }; let rel = rels.by_id(&rel_id).ok_or_else(|| invalid("worksheet relationship is missing"))?; @@ -299,6 +345,10 @@ pub(crate) fn workbook(bytes: &[u8], rels: &Rels, limits: &Limits, budget: &Budg if let SheetKind::Other(_) = kind { return Err(invalid("unknown sheet relationship type")); } + // Only a worksheet has cells a formula can refer to. + if let Some(l) = links.as_mut() { + l.sheets.push((kind == SheetKind::Worksheet).then(|| name.clone())); + } wb.sheets.push(SheetEntry { name, sheet_id, visibility, rel_id, kind, part: Some(rel.target.clone()) }); } _ => {} @@ -307,6 +357,48 @@ pub(crate) fn workbook(bytes: &[u8], rels: &Rels, limits: &Limits, budget: &Budg Err(invalid("missing workbook end record")) } +/// A supporting link (`BrtSupSelf`, `BrtSupSame`, `BrtSupAddin`, `BrtSupBookSrc`), the sheet links +/// (`BrtExternSheet`) or a defined name (`BrtName`), added to `links`. +fn link_record(id: u16, payload: &[u8], links: &mut Links) -> Result<(), Error> { + let mut b = Bytes::new(payload); + match id { + 357 | 358 | 667 => { + b.finish()?; + links.books.push(match id { + 357 => Book::This, + 358 => Book::Same, + _ => Book::Addin, + }); + } + 355 => links.books.push(Book::External), + 362 => { + // [MS-XLSB] 2.4.672: cXti "MUST be less than 65536" (PtgRef3d's ixti is 16 bits). + let count = b.u32()?; + if count >= 65_536 || !links.xti.is_empty() { + return Err(invalid("invalid sheet link list")); + } + for _ in 0..count { + let book = b.u32()?; + let (first, last) = (b.i32()?, b.i32()?); + links.xti.push(Xti { book, first, last }); + } + b.finish()?; + } + _ => { + // BrtName ([MS-XLSB] 2.4.718): flags, chKey, itab, then the name; the formula and the + // rest aren't needed to call a function or tell a name from one. + if links.names.len() >= MAX_NAMES { + return Err(invalid("too many defined names")); + } + let flags = b.u32()?; + b.take(5)?; + let text = b.text(255)?; + links.names.push(Name { text, future: flags & (1 << 17) != 0 }); + } + } + Ok(()) +} + /// A `RichStr`: the text; formatting and phonetic runs are skipped. fn rich_string(b: &mut Bytes<'_>) -> Result { let flags = b.u8()?; @@ -397,17 +489,37 @@ fn boolean(b: &mut Bytes<'_>) -> Result { } } -/// The formula after a cell's saved result: checked for framing, not decoded. -fn skip_formula(b: &mut Bytes<'_>) -> Result<(), Error> { +/// The `grbitFlags` and `CellParsedFormula` after a formula cell's value: its tokens (`rgce`) +/// and their extra data (`rgcb`). The flags' only bit, `fAlwaysCalc`, is the calculator's to know. +fn cell_formula<'a>(b: &mut Bytes<'a>) -> Result<(&'a [u8], &'a [u8]), Error> { b.take(2)?; let cce = b.u32()?; if !(1..=16384).contains(&cce) { return Err(invalid("invalid formula token length")); } - b.take(cce as usize)?; + let rgce = b.take(cce as usize)?; let extra = b.u32()?; - b.take(extra as usize)?; - Ok(()) + let rgcb = b.take(extra as usize)?; + Ok((rgce, rgcb)) +} + +/// A formula's text, or `None` (with a warning saying why) for one that keeps its saved result: +/// the workbook's links couldn't be read, the tokens can't be decoded, or the formula text +/// allowance is spent. +fn decode_formula(rgce: &[u8], rgcb: &[u8], links: Option<&Links>, limits: &Limits, budget: &Budget, warnings: &mut Warnings) -> Option { + let links = links?; + let text = match formula::decode(rgce, rgcb, links) { + Ok(e) => expr::print(&e), + Err(formula::Unsupported(why)) => { + warnings.warn(format!("XLSB: some formulas were kept as their saved results ({why})")); + return None; + } + }; + if !budget.try_formula_text(text.len(), limits) { + warnings.warn("XLSB: some formulas were kept as their saved results (more formula text than the limits allow)"); + return None; + } + Some(text) } fn value(id: u16, b: &mut Bytes<'_>, strings: usize, limits: &Limits, budget: &Budget) -> Result { @@ -444,16 +556,16 @@ fn value(id: u16, b: &mut Bytes<'_>, strings: usize, limits: &Limits, budget: &B } _ => return Err(invalid("unsupported cell record")), }; - if (8..=11).contains(&id) { - skip_formula(b)?; - } Ok(value) } /// A worksheet part: rows (BrtRowHdr) and cells inside BrtBeginSheetData, merged cells after. +/// Formulas refer to the workbook's `links`; with none, each keeps its saved result. +#[allow(clippy::too_many_arguments)] pub(crate) fn worksheet( bytes: &[u8], strings: usize, + links: Option<&Links>, limits: &Limits, budget: &Budget, warnings: &mut Warnings, @@ -466,9 +578,18 @@ pub(crate) fn worksheet( let mut data_seen = false; let mut row: Option = None; let mut col = None; + // BrtCellMeta applies to the cell record that follows it (a dynamic-array formula), as `cm` + // does in `.xlsx`: a one-based index into the metadata part's cell metadata. + let mut meta: Option = None; while let Some((id, payload)) = records.next(warnings)? { let mut b = Bytes::new(payload); + let cell_metadata = meta.take(); match id { + 49 => { + let icmb = b.i32()?; + b.finish()?; + meta = u32::try_from(icmb).ok().filter(|&i| i >= 1); + } 130 => { if data || !data_seen { return Err(invalid("incomplete worksheet data")); @@ -527,7 +648,14 @@ pub(crate) fn worksheet( } let style = b.u32()?; let value = value(id, &mut b, strings, limits, budget)?; - b.finish()?; + let formula = if (8..=11).contains(&id) { + let (rgce, rgcb) = cell_formula(&mut b)?; + b.finish()?; + decode_formula(rgce, rgcb, links, limits, budget, warnings).map(|text| CellFormula { text, ..CellFormula::default() }) + } else { + b.finish()?; + None + }; if value != CellValue::Empty { budget.cell(limits)?; } @@ -536,6 +664,8 @@ pub(crate) fn worksheet( style: style & 0x00FF_FFFF, phonetic: style & 0x0100_0000 != 0, value, + formula, + cell_metadata, ..Cell::default() }); col = Some(c); diff --git a/crates/xlsx/src/xlsb/expr.rs b/crates/xlsx/src/xlsb/expr.rs new file mode 100644 index 000000000..3dd643a6a --- /dev/null +++ b/crates/xlsx/src/xlsb/expr.rs @@ -0,0 +1,345 @@ +//! The expression tree an XLSB formula decodes to, and its A1 formula text (without `=`), in the +//! file's spelling (`_xlfn.` and `_xlpm.` names as written). A copy of craft-xls's: the two +//! readers follow separate specifications, and can share one tree when both settle. + +use std::fmt::Write; +use std::sync::Arc; + +use craft_sheet::{CellError, push_col, quote_sheet, sheet_needs_quotes}; + +/// One end of a reference, with `$` anchors. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct Anchor { + pub row: u32, + pub col: u32, + pub row_abs: bool, + pub col_abs: bool, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) enum RefKind { + Cell(Anchor), + Range(Anchor, Anchor), + /// Whole rows `3:5` as (row, absolute) pairs. + Rows(u32, bool, u32, bool), + /// Whole columns `A:C`. + Cols(u32, bool, u32, bool), +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) enum SheetSel { + /// Unqualified: the formula's own sheet. + Current, + Named(String), + /// 3-D span `Sheet1:Sheet3`. + Span(String, String), +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct Reference { + pub sheet: SheetSel, + pub kind: RefKind, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum UnOp { + Neg, + Plus, + Percent, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum BinOp { + Add, + Sub, + Mul, + Div, + Pow, + Concat, + Eq, + Ne, + Lt, + Gt, + Le, + Ge, + /// `:` between two references. + Range, + /// `,` inside parentheses. + Union, + /// A space between two references. + Intersect, +} + +impl BinOp { + /// How tightly the operator binds, as formula text is read: `:` 90, space 85, `,` 80, `^` 50, + /// `*` `/` 40, `+` `-` 30, `&` 20, comparisons 10. + pub(crate) fn precedence(self) -> u8 { + match self { + BinOp::Range => 90, + BinOp::Intersect => 85, + BinOp::Union => 80, + BinOp::Pow => 50, + BinOp::Mul | BinOp::Div => 40, + BinOp::Add | BinOp::Sub => 30, + BinOp::Concat => 20, + BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Gt | BinOp::Le | BinOp::Ge => 10, + } + } + + fn symbol(self) -> &'static str { + match self { + BinOp::Add => "+", + BinOp::Sub => "-", + BinOp::Mul => "*", + BinOp::Div => "/", + BinOp::Pow => "^", + BinOp::Concat => "&", + BinOp::Eq => "=", + BinOp::Ne => "<>", + BinOp::Lt => "<", + BinOp::Gt => ">", + BinOp::Le => "<=", + BinOp::Ge => ">=", + BinOp::Range => ":", + BinOp::Union => ",", + BinOp::Intersect => " ", + } + } +} + +/// A formula's syntax tree. The decoder bounds its depth, so printing and dropping it recurse +/// safely. +#[derive(Debug, PartialEq)] +pub(crate) enum Expr { + Number(f64), + Text(Arc), + Bool(bool), + Error(CellError), + /// Array constant `{1,2;3,4}` (rows of literals). + Array(Vec>), + Ref(Reference), + /// A defined name. + Name(String), + Unary(UnOp, Box), + Binary(BinOp, Box, Box), + Call(String, Vec), + /// An omitted argument. + Missing, + Paren(Box), +} + +fn push_anchor(s: &mut String, a: &Anchor) { + if a.col_abs { + s.push('$'); + } + push_col(s, a.col); + if a.row_abs { + s.push('$'); + } + let _ = write!(s, "{}", u64::from(a.row) + 1); +} + +fn push_reference(s: &mut String, r: &Reference) { + match &r.sheet { + SheetSel::Current => {} + SheetSel::Named(n) => { + s.push_str("e_sheet(n)); + s.push('!'); + } + SheetSel::Span(a, b) => { + let joined = format!("{a}:{b}"); + if sheet_needs_quotes(a) || sheet_needs_quotes(b) { + let _ = write!(s, "'{}'!", joined.replace('\'', "''")); + } else { + let _ = write!(s, "{joined}!"); + } + } + } + let dollar = |abs: bool| if abs { "$" } else { "" }; + match &r.kind { + RefKind::Cell(a) => push_anchor(s, a), + RefKind::Range(a, b) => { + push_anchor(s, a); + s.push(':'); + push_anchor(s, b); + } + RefKind::Rows(r0, a0, r1, a1) => { + let _ = write!(s, "{}{}:{}{}", dollar(*a0), u64::from(*r0) + 1, dollar(*a1), u64::from(*r1) + 1); + } + RefKind::Cols(c0, a0, c1, a1) => { + s.push_str(dollar(*a0)); + push_col(s, *c0); + s.push(':'); + s.push_str(dollar(*a1)); + push_col(s, *c1); + } + } +} + +/// Removes trailing zeros (and a trailing point) from a decimal string. +fn trim_decimal(s: &str) -> String { + if s.contains('.') { + let t = s.trim_end_matches('0').trim_end_matches('.'); + if t == "-0" { "0".into() } else { t.to_string() } + } else { + s.to_string() + } +} + +/// Scientific text like `1.23456789012346E+15` with up to `sig` significant digits. +fn sci_text(n: f64, sig: usize) -> String { + let s = format!("{:.*e}", sig.saturating_sub(1), n); + let Some((mant, exp)) = s.split_once('e') else { return s }; + let mant = trim_decimal(mant); + let e: i32 = exp.parse().unwrap_or(0); + format!("{}E{}{:02}", mant, if e < 0 { '-' } else { '+' }, e.abs()) +} + +/// A number as a formula spells it: up to 15 significant digits, scientific for very large or +/// very small magnitudes. +pub(crate) fn number_to_text(n: f64) -> String { + if n == 0.0 || !n.is_finite() { + return "0".into(); + } + let a = n.abs(); + if !(1e-9..1e15).contains(&a) && a.round() != a || a >= 1e15 { + return sci_text(n, 15); + } + // A value just below 1e15 can round up to 1e15 at 15 significant digits. + if format!("{a:.14e}").parse::().is_ok_and(|r| r >= 1e15) { + return sci_text(n, 15); + } + let mag = a.log10().floor() as i32; + let decimals = (14 - mag).clamp(0, 30) as usize; + trim_decimal(&format!("{n:.decimals$}")) +} + +fn number(n: f64) -> String { + let text = number_to_text(n); + // 15 significant digits can round past f64::MAX, which no longer parses. + if text.parse::().is_ok_and(f64::is_finite) { + return text; + } + let exact = format!("{n:E}"); + match exact.split_once('E') { + Some((m, e)) if !e.starts_with('-') => format!("{m}E+{e}"), + _ => exact, + } +} + +/// Splits a sheet-qualified name at its last `!` outside brackets. +fn sheet_split(n: &str) -> Option<(&str, &str)> { + let mut depth = 0usize; + let mut escaped = false; + let mut at = None; + for (i, c) in n.char_indices() { + if escaped { + escaped = false; + continue; + } + match c { + '\'' if depth > 0 => escaped = true, + '[' => depth = depth.saturating_add(1), + ']' => depth = depth.saturating_sub(1), + '!' if depth == 0 => at = Some(i), + _ => {} + } + } + at.and_then(|i| Some((n.get(..i)?, n.get(i + 1..)?))) +} + +/// Prints an expression in A1 notation. +pub(crate) fn print(e: &Expr) -> String { + let mut s = String::new(); + write_expr(&mut s, e); + s +} + +fn write_args(s: &mut String, args: &[Expr], sep: char) { + for (i, a) in args.iter().enumerate() { + if i > 0 { + s.push(sep); + } + write_expr(s, a); + } +} + +fn write_expr(s: &mut String, e: &Expr) { + match e { + Expr::Number(n) => s.push_str(&number(*n)), + Expr::Text(t) => { + s.push('"'); + s.push_str(&t.replace('"', "\"\"")); + s.push('"'); + } + Expr::Bool(b) => s.push_str(if *b { "TRUE" } else { "FALSE" }), + Expr::Error(err) => s.push_str(err.as_str()), + Expr::Array(rows) => { + s.push('{'); + for (ri, row) in rows.iter().enumerate() { + if ri > 0 { + s.push(';'); + } + write_args(s, row, ','); + } + s.push('}'); + } + Expr::Ref(r) => push_reference(s, r), + Expr::Name(n) => match sheet_split(n) { + // A sheet-qualified name holds the bare sheet name (`My Sheet!Total`): quote it so it parses back. + Some((sheet, name)) => { + s.push_str("e_sheet(sheet)); + s.push('!'); + s.push_str(name); + } + None => s.push_str(n), + }, + Expr::Unary(UnOp::Neg, x) => { + s.push('-'); + write_expr(s, x); + } + Expr::Unary(UnOp::Plus, x) => { + s.push('+'); + write_expr(s, x); + } + Expr::Unary(UnOp::Percent, x) => { + write_expr(s, x); + s.push('%'); + } + Expr::Binary(op, a, b) => { + write_expr(s, a); + s.push_str(op.symbol()); + write_expr(s, b); + } + Expr::Call(name, args) => { + s.push_str(name); + s.push('('); + write_args(s, args, ','); + s.push(')'); + } + Expr::Missing => {} + Expr::Paren(x) => { + s.push('('); + write_expr(s, x); + s.push(')'); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn numbers_print_with_fifteen_significant_digits() { + assert_eq!(number_to_text(1.0), "1"); + assert_eq!(number_to_text(-2.5), "-2.5"); + assert_eq!(number_to_text(0.1 + 0.2), "0.3"); + assert_eq!(number_to_text(123456789012345.0), "123456789012345"); + assert_eq!(number_to_text(1e15), "1E+15"); + assert_eq!(number_to_text(1234567890123456.0), "1.23456789012346E+15"); + assert_eq!(number_to_text(0.0001), "0.0001"); + assert_eq!(number_to_text(1e-10), "1E-10"); + assert!(number(f64::MAX).parse::().is_ok_and(f64::is_finite)); + } +} diff --git a/crates/xlsx/src/xlsb/formula.rs b/crates/xlsx/src/xlsb/formula.rs new file mode 100644 index 000000000..bed0adcd0 --- /dev/null +++ b/crates/xlsx/src/xlsb/formula.rs @@ -0,0 +1,538 @@ +//! BIFF12 parsed formulas ([MS-XLSB] 2.2.2, 2.5.98): reverse-Polish `Ptg` tokens back to an +//! expression tree, for cell formulas. The tokens are BIFF8's, but nearly every operand differs +//! (32-bit rows, 14-bit columns, UTF-16 strings, its own array-constant codes, newer functions +//! called through hidden names), so this is its own decoder, with its own function table +//! ([`super::ftab`]) and its own copies of craft-xls's depth guards and limits. Anything outside the +//! token set it knows is reported as unsupported, and the caller keeps the cell's cached value. +//! So is a formula that needs something the reader doesn't read: a user's defined name (XLSB +//! names are not read yet) or Python code stored outside the cell. +//! +//! Names keep their file spelling (`_xlfn.CONCAT`, `_xlpm.x`), as `.xlsx` formula text has them. + +use std::sync::Arc; + +use craft_sheet::{CellError, MAX_COLS, MAX_ROWS}; + +use super::expr::{Anchor, BinOp, Expr, RefKind, Reference, SheetSel, UnOp}; +use super::ftab::function; + +/// Formula token bytes decoded at most (a cell formula holds at most 16,384). +pub(super) const MAX_RGCE: usize = 16 * 1024; +/// Values in one array constant at most. +const MAX_ARRAY: usize = 65_536; + +/// Why a formula could not be turned back into text. +#[derive(Debug, PartialEq)] +pub(super) struct Unsupported(pub &'static str); + +/// Expression nesting at most: deeper trees would overflow the stack when printed or dropped (a +/// hostile file can chain thousands of PtgParen tokens). +const MAX_DEPTH: usize = 256; + +/// Operand stack entries: an expression and its nesting depth. +type Stack = Vec<(Expr, usize)>; + +/// Pops an operand, recording its depth in `kids` (the deepest operand this token consumed). +fn pop(stack: &mut Stack, kids: &mut usize) -> Result { + let (e, d) = stack.pop().ok_or(Unsupported("malformed formula (operand missing)"))?; + *kids = (*kids).max(d); + Ok(e) +} + +fn pop_n(stack: &mut Stack, n: usize, kids: &mut usize) -> Result, Unsupported> { + let at = stack.len().checked_sub(n).ok_or(Unsupported("malformed formula (argument missing)"))?; + let popped = stack.split_off(at); + *kids = popped.iter().map(|(_, d)| *d).fold(*kids, usize::max); + Ok(popped.into_iter().map(|(e, _)| e).collect()) +} + +/// Pushes the result of a token one level above the operands it consumed. +fn push(stack: &mut Stack, kids: usize, e: Expr) -> Result<(), Unsupported> { + let depth = kids.saturating_add(1); + if depth > MAX_DEPTH { + return Err(Unsupported("formula nested too deeply")); + } + stack.push((e, depth)); + Ok(()) +} + +/// An error constant's code ([MS-XLSB] 2.5.98.21, `BErr`). +fn error_code(code: u8) -> Option { + Some(match code { + 0x00 => CellError::Null, + 0x07 => CellError::Div0, + 0x0F => CellError::Value, + 0x17 => CellError::Ref, + 0x1D => CellError::Name, + 0x24 => CellError::Num, + 0x2A => CellError::NA, + 0x2B => CellError::GettingData, + _ => return None, + }) +} + +/// A supporting link record ([MS-XLSB] 2.2.7.3), in the order of the workbook's externals block. +#[derive(Clone, Copy, Debug, PartialEq)] +pub(super) enum Book { + /// `BrtSupSelf`: this workbook. + This, + /// `BrtSupSame`: the caller's sheet (macro sheets and names only). + Same, + /// `BrtSupAddin`: add-in functions. + Addin, + /// `BrtSupBookSrc`: another workbook, DDE or OLE. Never followed. + External, +} + +/// Xti ([MS-XLSB] 2.5.173): a supporting link and a range of its sheets. +#[derive(Clone, Copy, Debug)] +pub(super) struct Xti { + pub book: u32, + pub first: i32, + pub last: i32, +} + +/// A `BrtName` as far as cell formulas need it. +#[derive(Clone, Debug)] +pub(super) struct Name { + /// The name as written (`_xlfn.CONCAT`, `_xlpm.x`, `Rate`). + pub text: String, + /// `fFutureFunction`: a hidden name standing for a function newer than the file format. + pub future: bool, +} + +/// Workbook-level tables formulas refer to. +#[derive(Default)] +pub(crate) struct Links { + /// Every sheet's name in `BrtBundleSh` order, `None` for sheets the reader skips or that hold + /// no cells (chart, dialog and macro sheets). + pub(super) sheets: Vec>, + /// Every `BrtName`, in order: `PtgName` indexes them from 1. + pub(super) names: Vec, + pub(super) books: Vec, + pub(super) xti: Vec, +} + +/// Whether a name stands for a LET parameter (`_xlpm.x`), which only the formula itself defines. +fn is_param(name: &str) -> bool { + name.get(..6).is_some_and(|p| p.eq_ignore_ascii_case("_xlpm.")) +} + +impl Links { + /// The sheets a 3-D reference names, or `None` for a deleted sheet (the reference is `#REF!`). + fn sheet_sel(&self, ixti: u16) -> Result, Unsupported> { + let x = self.xti.get(usize::from(ixti)).ok_or(Unsupported("reference to a missing sheet link"))?; + match usize::try_from(x.book).ok().and_then(|b| self.books.get(b)) { + Some(Book::This) => {} + Some(Book::External) => return Err(Unsupported("references to other workbooks (external links)")), + Some(_) => return Err(Unsupported("references through add-in or same-sheet links")), + None => return Err(Unsupported("reference to a missing workbook link")), + } + // -1: "the first sheet in the reference cannot be found" (a deleted sheet). + if x.first == -1 || x.last == -1 { + return Ok(None); + } + // -2 (the whole workbook) is for names, not cell references. + let (Ok(first), Ok(last)) = (usize::try_from(x.first), usize::try_from(x.last)) else { + return Err(Unsupported("reference to a missing sheet")); + }; + // A span takes every sheet between its ends, so a skipped one anywhere in it changes + // what the reference means. + let span = self.sheets.get(first..=last).filter(|s| !s.is_empty()).ok_or(Unsupported("reference to a missing sheet"))?; + if span.iter().any(Option::is_none) { + return Err(Unsupported("references to sheets the reader skips")); + } + let (Some(Some(a)), Some(Some(b))) = (span.first(), span.last()) else { + return Err(Unsupported("reference to a missing sheet")); + }; + Ok(Some(if first == last { SheetSel::Named(a.clone()) } else { SheetSel::Span(a.clone(), b.clone()) })) + } +} + +struct Bytes<'a> { + b: &'a [u8], + pos: usize, +} + +const TRUNC: Unsupported = Unsupported("truncated formula"); + +impl Bytes<'_> { + fn take(&mut self, n: usize) -> Result<&[u8], Unsupported> { + let s = self.b.get(self.pos..self.pos.checked_add(n).ok_or(TRUNC)?).ok_or(TRUNC)?; + self.pos += n; + Ok(s) + } + fn u8(&mut self) -> Result { + self.take(1)?.first().copied().ok_or(TRUNC) + } + fn u16(&mut self) -> Result { + let s: [u8; 2] = self.take(2)?.try_into().map_err(|_| TRUNC)?; + Ok(u16::from_le_bytes(s)) + } + fn u32(&mut self) -> Result { + let s: [u8; 4] = self.take(4)?.try_into().map_err(|_| TRUNC)?; + Ok(u32::from_le_bytes(s)) + } + fn f64(&mut self) -> Result { + let s: [u8; 8] = self.take(8)?.try_into().map_err(|_| TRUNC)?; + let n = f64::from_le_bytes(s); + if !n.is_finite() { + return Err(Unsupported("invalid number constant")); + } + Ok(n) + } + /// `cch` UTF-16 code units; unpaired surrogates are refused, not replaced. + fn chars(&mut self, cch: usize) -> Result { + let units = self.take(cch.checked_mul(2).ok_or(TRUNC)?)?.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes(*c)); + char::decode_utf16(units).collect::>().map_err(|_| Unsupported("invalid text constant")) + } +} + +/// The last row and column of the grid: a range from 0 to these is a whole column or row. +const LAST_ROW: u32 = MAX_ROWS - 1; +const LAST_COL: u32 = MAX_COLS - 1; + +/// RgceLoc ([MS-XLSB] 2.5.98.91): a row and a `ColRelShort` (14-bit column, then the +/// column-relative and row-relative flags). In a cell formula the coordinates are absolute +/// whatever the flags say; the flags only decide where `$` goes. +fn anchor(row: u32, col: u16) -> Result { + let c = u32::from(col & 0x3FFF); + if row > LAST_ROW || c > LAST_COL { + return Err(Unsupported("reference outside the grid")); + } + Ok(Anchor { row, col: c, row_abs: col & 0x8000 == 0, col_abs: col & 0x4000 == 0 }) +} + +/// RgceArea: a range spanning every row (or column) is a whole-column (row) reference. +fn area(r0: u32, r1: u32, c0: u16, c1: u16) -> Result { + let (a, b) = (anchor(r0, c0)?, anchor(r1, c1)?); + Ok(if a.row == 0 && b.row == LAST_ROW { + RefKind::Cols(a.col, a.col_abs, b.col, b.col_abs) + } else if a.col == 0 && b.col == LAST_COL { + RefKind::Rows(a.row, a.row_abs, b.row, b.row_abs) + } else { + RefKind::Range(a, b) + }) +} + +fn reference(sheet: Option, kind: RefKind) -> Expr { + match sheet { + Some(sheet) => Expr::Ref(Reference { sheet, kind }), + None => Expr::Error(CellError::Ref), + } +} + +/// Decodes a cell formula's `rgce` (tokens) with its `rgcb` (extra data for array constants and +/// cached areas). Returns the expression, with names in their file spelling, or why it can't be +/// represented. +pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links) -> Result { + if rgce.len() > MAX_RGCE { + return Err(Unsupported("formula too long")); + } + let mut t = Bytes { b: rgce, pos: 0 }; + let mut extra = Bytes { b: rgcb, pos: 0 }; + let mut stack: Stack = Vec::new(); + while t.pos < rgce.len() { + let ptg = t.u8()?; + let mut kids = 0usize; + // Operand classes (reference / value / array) share one token number. + let id = if ptg & 0x60 != 0 { 0x20 | (ptg & 0x1F) } else { ptg }; + match id { + 0x03..=0x11 => { + let op = match id { + 0x03 => BinOp::Add, + 0x04 => BinOp::Sub, + 0x05 => BinOp::Mul, + 0x06 => BinOp::Div, + 0x07 => BinOp::Pow, + 0x08 => BinOp::Concat, + 0x09 => BinOp::Lt, + 0x0A => BinOp::Le, + 0x0B => BinOp::Eq, + 0x0C => BinOp::Ge, + 0x0D => BinOp::Gt, + 0x0E => BinOp::Ne, + 0x0F => BinOp::Intersect, + 0x10 => BinOp::Union, + _ => BinOp::Range, + }; + let b = pop(&mut stack, &mut kids)?; + let a = pop(&mut stack, &mut kids)?; + push(&mut stack, kids, Expr::Binary(op, Box::new(a), Box::new(b)))?; + } + 0x12..=0x14 => { + let op = match id { + 0x12 => UnOp::Plus, + 0x13 => UnOp::Neg, + _ => UnOp::Percent, + }; + let x = pop(&mut stack, &mut kids)?; + push(&mut stack, kids, Expr::Unary(op, Box::new(x)))?; + } + 0x15 => { + let x = pop(&mut stack, &mut kids)?; + push(&mut stack, kids, Expr::Paren(Box::new(x)))?; + } + 0x16 => push(&mut stack, kids, Expr::Missing)?, + // PtgStr: cch (u16, at most 255) and always UTF-16, unlike BIFF8. + 0x17 => { + let cch = usize::from(t.u16()?); + if cch > 255 { + return Err(Unsupported("text constant too long")); + } + push(&mut stack, kids, Expr::Text(Arc::from(t.chars(cch)?)))?; + } + 0x19 => { + let kind = t.u8()?; + if kind & 0x04 != 0 { + // PtgAttrChoose: jump table of cOffset + 1 entries. + let n = usize::from(t.u16()?) + 1; + t.take(n * 2)?; + } else { + t.take(2)?; + } + if kind & 0x10 != 0 { + // PtgAttrSum: SUM of the single operand. + let x = pop(&mut stack, &mut kids)?; + push(&mut stack, kids, Expr::Call("SUM".into(), vec![x]))?; + } + // Semi (volatile), If, Goto, Space, Baxcel: no effect on the expression. + } + 0x1C => { + let e = t.u8()?; + push(&mut stack, kids, Expr::Error(error_code(e).ok_or(Unsupported("unknown error constant"))?))?; + } + 0x1D => push(&mut stack, kids, Expr::Bool(t.u8()? != 0))?, + 0x1E => push(&mut stack, kids, Expr::Number(f64::from(t.u16()?)))?, + 0x1F => { + let n = t.f64()?; + push(&mut stack, kids, Expr::Number(n))?; + } + 0x20 => { + // PtgArray: 14 unused bytes (Excel leaves junk in them); the values are in rgcb. + t.take(14)?; + push(&mut stack, kids, array_constant(&mut extra)?)?; + } + 0x21 => { + let iftab = t.u16()?; + let (name, args) = function(iftab).ok_or(Unsupported("unknown function"))?; + let n = usize::try_from(args).map_err(|_| Unsupported("unknown function argument count"))?; + let args = pop_n(&mut stack, n, &mut kids)?; + push(&mut stack, kids, Expr::Call(name.to_string(), args))?; + } + 0x22 => { + let n = usize::from(t.u8()?); + let tab = t.u16()?; + if tab & 0x8000 != 0 { + return Err(Unsupported("macro sheet commands")); + } + let mut args = pop_n(&mut stack, n, &mut kids)?; + if tab & 0x7FFF == 0x00FF { + // A function called by name (a newer function, add-in or LAMBDA): the name + // comes first, as a PtgName to a hidden `_xlfn.` name for a newer function. + if args.is_empty() { + return Err(Unsupported("malformed function call")); + } + let name = match args.remove(0) { + Expr::Name(n) => n, + _ => return Err(Unsupported("function call through an expression")), + }; + // `PY(index, …)` runs a script stored elsewhere in the package, which this + // import doesn't read. `PY("code", …)` carries its code and becomes a Python cell. + let py = name.rsplit('.').next().is_some_and(|n| n.eq_ignore_ascii_case("PY")); + if py && !args.first().is_some_and(literal_text) { + return Err(Unsupported("Python code stored outside the cell")); + } + push(&mut stack, kids, Expr::Call(name, args))?; + } else { + let (name, _) = function(tab & 0x7FFF).ok_or(Unsupported("unknown function"))?; + push(&mut stack, kids, Expr::Call(name.to_string(), args))?; + } + } + 0x23 => { + // PtgName: a 1-based index into every BrtName, hidden ones included. + let i = usize::try_from(t.u32()?).ok().and_then(|i| i.checked_sub(1)); + let name = i.and_then(|i| links.names.get(i)).ok_or(Unsupported("reference to a missing defined name"))?; + // Defined names are not imported from XLSB yet: a cell using one would calculate + // to #NAME?, so it keeps its saved result. Newer functions and LET parameters are + // hidden names that the formula text itself defines. + if !name.future && !is_param(&name.text) { + return Err(Unsupported("defined names")); + } + push(&mut stack, kids, Expr::Name(name.text.clone()))?; + } + 0x24 => { + let (r, c) = (t.u32()?, t.u16()?); + push(&mut stack, kids, Expr::Ref(Reference { sheet: SheetSel::Current, kind: RefKind::Cell(anchor(r, c)?) }))?; + } + 0x25 => { + let (r0, r1, c0, c1) = (t.u32()?, t.u32()?, t.u16()?, t.u16()?); + push(&mut stack, kids, Expr::Ref(Reference { sheet: SheetSel::Current, kind: area(r0, r1, c0, c1)? }))?; + } + 0x26 => { + // PtgMemArea: a cache of the sub-expression that follows. Its rgcb entry + // (PtgExtraMem) is a count and 16 bytes per cached area: skip both. + t.take(6)?; + let n = usize::try_from(extra.u32()?).map_err(|_| TRUNC)?; + extra.take(n.checked_mul(16).ok_or(TRUNC)?)?; + } + // PtgMemErr, PtgMemNoMem: 6 bytes; PtgMemFunc: 2. The sub-expression follows them. + 0x27 | 0x28 => { + t.take(6)?; + } + 0x29 => { + t.take(2)?; + } + 0x2A => { + t.take(6)?; + push(&mut stack, kids, Expr::Error(CellError::Ref))?; + } + 0x2B => { + t.take(12)?; + push(&mut stack, kids, Expr::Error(CellError::Ref))?; + } + 0x39 => return Err(Unsupported("names in other workbooks or on other sheets")), + 0x3A => { + let ixti = t.u16()?; + let (r, c) = (t.u32()?, t.u16()?); + let kind = RefKind::Cell(anchor(r, c)?); + push(&mut stack, kids, reference(links.sheet_sel(ixti)?, kind))?; + } + 0x3B => { + let ixti = t.u16()?; + let (r0, r1, c0, c1) = (t.u32()?, t.u32()?, t.u16()?, t.u16()?); + let kind = area(r0, r1, c0, c1)?; + push(&mut stack, kids, reference(links.sheet_sel(ixti)?, kind))?; + } + 0x3C | 0x3D => { + let ixti = t.u16()?; + t.take(if id == 0x3C { 6 } else { 12 })?; + // The sheet link must still be one the reader follows: an external #REF! stays cached. + links.sheet_sel(ixti)?; + push(&mut stack, kids, Expr::Error(CellError::Ref))?; + } + 0x01 => return Err(Unsupported("the shared or array formula form")), + 0x02 => return Err(Unsupported("what-if data tables")), + 0x18 => return Err(Unsupported("structured table references")), + 0x2C | 0x2D => return Err(Unsupported("relative references in a cell formula")), + _ => return Err(Unsupported("unknown formula token")), + } + if stack.len() > MAX_RGCE { + return Err(Unsupported("formula too complex")); + } + } + let (e, _) = stack.pop().ok_or(Unsupported("malformed formula (operand missing)"))?; + if !stack.is_empty() { + return Err(Unsupported("malformed formula (unused operands)")); + } + Ok(grouped(e, false)) +} + +/// Text written out in the formula: a string, or strings joined with `&` (how Python code +/// longer than a string constant is written). +fn literal_text(e: &Expr) -> bool { + match e { + Expr::Text(_) => true, + Expr::Binary(BinOp::Concat, a, b) => literal_text(a) && literal_text(b), + _ => false, + } +} + +/// How tightly an expression binds when printed, as the formula parser reads it: binary +/// operators by [`BinOp::precedence`], prefix `+`/`-` at 60 and `%` at 70 (`-2^2` is `(-2)^2`); +/// anything else is one operand. +fn binding(e: &Expr) -> u8 { + match e { + Expr::Binary(op, ..) => op.precedence(), + Expr::Unary(UnOp::Neg | UnOp::Plus, _) => 60, + Expr::Unary(UnOp::Percent, _) => 70, + _ => u8::MAX, + } +} + +/// The strings of a [`literal_text`] chain, left to right. +fn flatten_texts(e: Expr, out: &mut Vec) { + match e { + Expr::Binary(BinOp::Concat, a, b) => { + flatten_texts(*a, out); + flatten_texts(*b, out); + } + text => out.push(text), + } +} + +fn paren(e: Expr) -> Expr { + Expr::Paren(Box::new(e)) +} + +/// The tree with the parentheses its text needs to mean the same. Tokens are reverse Polish, so +/// grouping lives in their order: Excel adds `PtgParen` wherever its text has parentheses, but +/// `1 2 + 3 *` (another writer's, or a crafted file's) would print as `1+2*3`. Binary operators +/// are left-associative; a union in a function's arguments needs parentheses of its own. +/// Recursion follows the decoded tree, which `push` bounds at `MAX_DEPTH` levels. +fn grouped(e: Expr, in_args: bool) -> Expr { + match e { + // Strings joined with `&` are written as one flat chain, however their tokens nest: `&` + // is associative, and `PY("…"&"…")` is read as Python code only in that form. + e @ Expr::Binary(BinOp::Concat, ..) if literal_text(&e) => { + let mut texts = vec![]; + flatten_texts(e, &mut texts); + let mut texts = texts.into_iter(); + let first = texts.next().unwrap_or(Expr::Missing); + texts.fold(first, |a, b| Expr::Binary(BinOp::Concat, Box::new(a), Box::new(b))) + } + Expr::Binary(op, a, b) => { + let (a, b) = (grouped(*a, in_args), grouped(*b, in_args)); + let a = if binding(&a) < op.precedence() { paren(a) } else { a }; + let b = if binding(&b) <= op.precedence() { paren(b) } else { b }; + let e = Expr::Binary(op, Box::new(a), Box::new(b)); + if in_args && op == BinOp::Union { paren(e) } else { e } + } + Expr::Unary(op, x) => { + let x = grouped(*x, in_args); + // Each of `+`, `-` and `%` takes an operand that binds at least as tightly as a sign. The + // parser reads `-1%` as `-(1%)`, Excel's tokens as `(-1)%`: the same value, so a sign + // under `%` keeps Excel's spelling. + let needs = binding(&x) < 60; + Expr::Unary(op, Box::new(if needs { paren(x) } else { x })) + } + Expr::Paren(x) => paren(grouped(*x, false)), + Expr::Call(name, args) => Expr::Call(name, args.into_iter().map(|a| grouped(a, true)).collect()), + other => other, + } +} + +/// PtgExtraArray ([MS-XLSB] 2.5.98.41): row and column counts, then `SerAr` values in row-major +/// order. The type codes are not BIFF8's: 0x00 is a number here, not a missing value. +fn array_constant(b: &mut Bytes<'_>) -> Result { + let rows = usize::try_from(b.u32()?).map_err(|_| TRUNC)?; + let cols = usize::try_from(b.u32()?).map_err(|_| TRUNC)?; + if rows == 0 || cols == 0 || rows.saturating_mul(cols) > MAX_ARRAY { + return Err(Unsupported("array constant too large or empty")); + } + let mut out = Vec::with_capacity(rows); + for _ in 0..rows { + let mut row = Vec::with_capacity(cols); + for _ in 0..cols { + row.push(match b.u8()? { + 0x00 => Expr::Number(b.f64()?), + 0x01 => { + let cch = usize::from(b.u16()?); + if cch > 255 { + return Err(Unsupported("text constant too long")); + } + Expr::Text(Arc::from(b.chars(cch)?)) + } + 0x02 => Expr::Bool(b.u8()? != 0), + 0x04 => { + let e = b.u8()?; + b.take(3)?; + Expr::Error(error_code(e).ok_or(Unsupported("unknown error constant"))?) + } + _ => return Err(Unsupported("unknown array constant value")), + }); + } + out.push(row); + } + Ok(Expr::Array(out)) +} diff --git a/crates/xlsx/src/xlsb/ftab.rs b/crates/xlsx/src/xlsb/ftab.rs index c6b59092a..87f369519 100644 --- a/crates/xlsx/src/xlsb/ftab.rs +++ b/crates/xlsx/src/xlsb/ftab.rs @@ -487,8 +487,6 @@ const FTAB: &[(u16, &str, i8)] = &[ /// The function a `PtgFunc` or `PtgFuncVar` calls: its name and fixed argument count (-1 when it /// takes a variable number). `None` for a number the table doesn't assign. -// The formula decoder calls it. -#[allow(dead_code)] pub(super) fn function(iftab: u16) -> Option<(&'static str, i8)> { FTAB.binary_search_by_key(&iftab, |(i, _, _)| *i).ok().and_then(|i| FTAB.get(i)).map(|(_, n, a)| (*n, *a)) } diff --git a/crates/xlsx/tests/common/mod.rs b/crates/xlsx/tests/common/mod.rs index 59d3d8372..30703b3cd 100644 --- a/crates/xlsx/tests/common/mod.rs +++ b/crates/xlsx/tests/common/mod.rs @@ -3,12 +3,15 @@ use std::collections::BTreeMap; -use craft_xlsx::{CellRef, Error, Limits, Package, Range, SheetKind, Value, Visibility}; +use craft_xlsx::{CellFormula, CellRef, Error, Limits, Package, Range, SheetKind, Value, Visibility}; pub struct Sheet { pub name: String, pub visibility: Visibility, pub cells: BTreeMap, + /// Every cell's formula, and its cell metadata index (`cm`), where it has one. + pub formulas: BTreeMap, + pub cell_metadata: BTreeMap, pub merges: Vec, } @@ -16,6 +19,11 @@ impl Sheet { pub fn value(&self, at: CellRef) -> Value { self.cells.get(&at).cloned().unwrap_or_default() } + + /// The formula text at `at`, if the cell has a formula. + pub fn formula(&self, at: CellRef) -> Option<&str> { + self.formulas.get(&at).map(|f| f.text.as_str()) + } } pub struct Book { @@ -37,8 +45,15 @@ pub fn read_with(bytes: &[u8], limits: &Limits) -> Result { continue; } let mut cells = BTreeMap::new(); + let (mut formulas, mut cell_metadata) = (BTreeMap::new(), BTreeMap::new()); let part = p.sheet(e, |row| { for c in row.cells { + if let Some(f) = c.formula { + formulas.insert(c.at, f); + } + if let Some(m) = c.cell_metadata { + cell_metadata.insert(c.at, m); + } let v = c.value.resolve(&strings, wb.date_1904); if !v.is_empty() { cells.insert(c.at, v); @@ -46,7 +61,7 @@ pub fn read_with(bytes: &[u8], limits: &Limits) -> Result { } Ok(()) })?; - book.sheets.push(Sheet { name: e.name.clone(), visibility: e.visibility, cells, merges: part.merges }); + book.sheets.push(Sheet { name: e.name.clone(), visibility: e.visibility, cells, formulas, cell_metadata, merges: part.merges }); } book.warnings = p.warnings().to_vec(); Ok(book) diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index cef6d9cbe..42c27158b 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -442,3 +442,270 @@ fn phonetic_runs_use_documented_size_and_tolerate_padding() { } assert!(read(&file(&worksheet(&[row(0), cell(62, 0, &phonetic(2, 12))].concat(), &[]))).is_err()); } + +/// A formula cell (`BrtFmlaNum`, cached 0) with its own tokens and extra data. +fn formula_cell(col: u32, rgce: &[u8], rgcb: &[u8]) -> Vec { + let mut data = col.to_le_bytes().to_vec(); + data.extend([0; 4]); + data.extend(0f64.to_le_bytes()); + data.extend([0; 2]); + data.extend((rgce.len() as u32).to_le_bytes()); + data.extend(rgce); + data.extend((rgcb.len() as u32).to_le_bytes()); + data.extend(rgcb); + record(9, &data) +} + +fn name(flags: u32, text: &str) -> Vec { + let mut data = flags.to_le_bytes().to_vec(); + data.push(0); + data.extend(u32::MAX.to_le_bytes()); // Workbook scope. + data.extend(wide(text)); + data.extend([2, 0, 0, 0, 0x1c, 0x1d, 0, 0, 0, 0]); // Formula `#NAME?`, no extra data. + data.extend(u32::MAX.to_le_bytes()); // No comment. + record(39, &data) +} + +/// `book(false)` with a supporting-link block and defined names, as Excel writes them: this +/// workbook, then another; sheet links Data, Hidden, Data:Hidden, a deleted sheet and the other +/// workbook; hidden names for CONCAT, LET and a LET parameter, then the user name Rate. +fn book_with_links(extern_sheet: &[u8]) -> Vec { + let plain = book(false); + let end = record(132, &[]); + let mut out = plain[..plain.len() - end.len()].to_vec(); + out.extend(record(353, &[])); + out.extend(record(357, &[])); + out.extend(record(355, &wide("rId9"))); + out.extend(record(362, extern_sheet)); + out.extend(record(354, &[])); + const FUTURE: u32 = 0x0002_000b; + out.extend(name(FUTURE, "_xlfn.CONCAT")); + out.extend(name(FUTURE, "_xlfn.LET")); + out.extend(name(0x0002_0019, "_xlpm.x")); + out.extend(name(0, "Rate")); + out.extend(end); + out +} + +fn xti() -> Vec { + let mut out = 5u32.to_le_bytes().to_vec(); + for (book, first, last) in [(0u32, 0i32, 0i32), (0, 1, 1), (0, 0, 1), (0, -1, -1), (1, 0, 0)] { + out.extend(book.to_le_bytes()); + out.extend(first.to_le_bytes()); + out.extend(last.to_le_bytes()); + } + out +} + +fn loc(row: u32, col: u16) -> Vec { + [&row.to_le_bytes()[..], &col.to_le_bytes()].concat() +} + +/// Relative row and column flags of a `ColRelShort`. +const REL: u16 = 0xC000; + +fn warned(wb: &common::Book, why: &str) -> bool { + wb.warnings.iter().any(|w| w.contains("kept as their saved results") && w.contains(why)) +} + +#[test] +fn cell_formulas_are_read_in_their_xlsx_spelling() { + let ref3d = |ixti: u16, row: u32, col: u16| [&[0x5Au8][..], &ixti.to_le_bytes(), &loc(row, col)].concat(); + let cells: Vec<(u32, Vec, Vec)> = vec![ + (1, [&[0x44u8][..], &loc(0, REL), &[0x1E, 2, 0, 0x05]].concat(), vec![]), + (2, [&[0x25u8][..], &0u32.to_le_bytes(), &1u32.to_le_bytes(), &0u16.to_le_bytes(), &REL.to_le_bytes(), &[0x19, 0x10, 0, 0]].concat(), vec![]), + (3, ref3d(1, 1, 1 | REL), vec![]), + ( + 4, + [ + &[0x3Bu8][..], + &2u16.to_le_bytes(), + &0u32.to_le_bytes(), + &0xFFFFFu32.to_le_bytes(), + &REL.to_le_bytes(), + &REL.to_le_bytes(), + &[0x19, 0x10, 0, 0], + ] + .concat(), + vec![], + ), + (5, [&[0x23u8, 1, 0, 0, 0][..], &[0x17, 1, 0, b'a', 0], &[0x17, 1, 0, b'b', 0], &[0x42, 3, 0xFF, 0]].concat(), vec![]), + (6, vec![0x1E, 1, 0, 0x1E, 0, 0, 0x06, 0x1E, 2, 0, 0x41, 0xE0, 0x01], vec![]), + ( + 7, + [&[0x60u8][..], &[0xAB; 14]].concat(), + [&2u32.to_le_bytes()[..], &2u32.to_le_bytes(), &[0], &1f64.to_le_bytes(), &[1, 1, 0, b'x', 0], &[2, 1], &[4, 0x2A, 0, 0, 0]].concat(), + ), + ( + 8, + [&[0x23u8, 2, 0, 0, 0][..], &[0x23, 3, 0, 0, 0, 0x1E, 1, 0], &[0x23, 3, 0, 0, 0, 0x1E, 1, 0, 0x03], &[0x42, 4, 0xFF, 0]].concat(), + vec![], + ), + (9, ref3d(3, 0, 0), vec![]), + (10, [&[0x25u8][..], &0u32.to_le_bytes(), &1u32.to_le_bytes(), &REL.to_le_bytes(), &REL.to_le_bytes(), &[0x41, 20, 0]].concat(), vec![]), + // Kept as their saved results: + (11, vec![0x43, 4, 0, 0, 0, 0x1E, 2, 0, 0x05], vec![]), + (12, ref3d(4, 0, REL), vec![]), + (13, vec![0x01, 0, 0, 0, 0], vec![0, 0, 0, 0]), + ]; + let mut data = row(0); + data.extend(cell(5, 0, &3.0f64.to_le_bytes())); + for (col, rgce, rgcb) in &cells { + data.extend(formula_cell(*col, rgce, rgcb)); + } + data.extend(record(49, &1u32.to_le_bytes())); // BrtCellMeta: the next formula is a dynamic array. + data.extend(formula_cell(14, &[0x1E, 1, 0], &[])); + data.extend(formula_cell(15, &[0x18, 0x19, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0], &[])); // A table reference. + let wb = read(&package(&book_with_links(&xti()), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES)).unwrap(); + let sheet = &wb.sheets[0]; + let got: Vec<_> = (1..=15).map(|c| sheet.formula(CellRef::new(0, c))).collect(); + // As an `.xlsx` file holds them: names in their file spelling (`_xlfn.`, `_xlpm.`), and no + // implicit `@`, which is how a calculator reads a legacy formula, not how the file spells it. + let want = [ + Some("A1*2"), + Some("SUM($A$1:A2)"), + Some("Hidden!B2"), + Some("SUM(Data:Hidden!A:A)"), + Some("_xlfn.CONCAT(\"a\",\"b\")"), + Some("IFERROR(1/0,2)"), + Some("{1,\"x\";TRUE,#N/A}"), + Some("_xlfn.LET(_xlpm.x,1,_xlpm.x+1)"), + Some("#REF!"), + Some("SQRT(A1:A2)"), + None, + None, + None, + // A BrtCellMeta before a cell gives it cell metadata, as `cm` does in .xlsx. + Some("1"), + None, + ]; + assert_eq!(got, want); + assert!(sheet.formulas.values().all(|f| f.kind == craft_xlsx::FormulaKind::Normal)); + assert_eq!(sheet.cell_metadata.iter().collect::>(), [(&CellRef::new(0, 14), &1)]); + // Every formula cell keeps its saved result as its value. + assert!((1..=15).all(|c| sheet.value(CellRef::new(0, c)) == Value::Number(0.0))); + for why in ["defined names", "other workbooks", "the shared or array formula form", "structured table references"] { + assert!(warned(&wb, why), "{why}: {:?}", wb.warnings); + } +} + +#[test] +fn unreadable_links_keep_every_formula_as_its_value() { + // A sheet-link count past the spec's limit, and a truncated list: the file opens as it did + // before formulas were read, with values only. + let mut truncated = xti(); + truncated.truncate(20); + for extern_sheet in [70_000u32.to_le_bytes().to_vec(), truncated] { + let data = [row(0), formula_cell(0, &[0x1E, 1, 0], &[])].concat(); + let wb = read(&package(&book_with_links(&extern_sheet), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES)).unwrap(); + assert_eq!(wb.sheets[0].formula(CellRef::new(0, 0)), None); + assert_eq!(wb.sheets[0].value(CellRef::new(0, 0)), Value::Number(0.0)); + assert!(wb.warnings.iter().any(|w| w.contains("could not be read")), "{:?}", wb.warnings); + } +} + +#[test] +fn formula_grouping_follows_token_order() { + // Reverse-Polish tokens group by their order alone. Excel writes PtgParen wherever its text + // has parentheses, but another writer need not: the text gets the ones it needs to mean the + // same, and no more. + let a = |row: u32| [&[0x24u8][..], &loc(row, REL)].concat(); + let int = |n: u8| vec![0x1E, n, 0]; + let cases: Vec<(Vec, &str)> = vec![ + ([int(1), int(2), vec![0x03], int(3), vec![0x05]].concat(), "(1+2)*3"), + ([int(1), int(2), int(3), vec![0x04, 0x04]].concat(), "1-(2-3)"), + ([int(1), int(2), vec![0x04], int(3), vec![0x04]].concat(), "1-2-3"), + ([int(2), vec![0x13], int(2), vec![0x07]].concat(), "-2^2"), + ([int(2), int(2), vec![0x07, 0x13]].concat(), "-(2^2)"), + // Excel's tokens for its own `-1%`: the parser groups it the other way, to the same value. + ([int(1), vec![0x13, 0x14]].concat(), "-1%"), + ([int(1), int(2), vec![0x03, 0x14]].concat(), "(1+2)%"), + ([a(0), a(1), vec![0x10], vec![0x19, 0x10, 0, 0]].concat(), "SUM((A1,A2))"), + ([int(1), int(2), vec![0x03, 0x15], int(3), vec![0x05]].concat(), "(1+2)*3"), + ]; + let data: Vec = + [row(0)].into_iter().chain(cases.iter().enumerate().map(|(i, (rgce, _))| formula_cell(i as u32, rgce, &[]))).flatten().collect(); + let wb = read(&package(&book_with_links(&xti()), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES)).unwrap(); + for (i, (_, want)) in cases.iter().enumerate() { + assert_eq!(wb.sheets[0].formula(CellRef::new(0, i as u32)), Some(*want)); + } +} + +#[test] +fn functions_past_0x17b_are_called_by_number() { + // [MS-XLSB] 2.5.98.10's numbers past the .xls table: IFERROR (PtgFunc) and SUMIFS (PtgFuncVar). + let sumifs = [&[0x1Eu8, 1, 0][..], &[0x1E, 2, 0], &[0x17, 2, 0, b'>', 0, b'0', 0], &[0x42, 3, 0xE2, 0x01]].concat(); + let data = [row(0), formula_cell(0, &[0x1E, 1, 0, 0x1E, 2, 0, 0x41, 0xE0, 0x01], &[]), formula_cell(1, &sumifs, &[])].concat(); + let wb = read(&package(&book_with_links(&xti()), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES)).unwrap(); + assert_eq!(wb.sheets[0].formula(CellRef::new(0, 0)), Some("IFERROR(1,2)")); + assert_eq!(wb.sheets[0].formula(CellRef::new(0, 1)), Some("SUMIFS(1,2,\">0\")")); +} + +#[test] +fn hostile_formula_tokens_keep_the_saved_result() { + let mut deep = vec![0x1E, 1, 0]; + deep.resize(16384, 0x15); // Thousands of parentheses: refused by the depth guard. + let empty_array = [&[0x60u8][..], &[0; 14]].concat(); + let bad: Vec<(Vec, Vec)> = vec![ + (vec![0x1E, 1], vec![]), // Truncated PtgInt. + (vec![0x17, 1, 0, 0x00, 0xD8], vec![]), // Unpaired surrogate. + (vec![0x17, 0, 1], vec![]), // Text constant over 255. + ([&[0x24u8][..], &loc(1_048_576, 0)].concat(), vec![]), // Row past the grid. + (empty_array.clone(), vec![0; 8]), // Empty array constant. + (empty_array, vec![0xFF; 8]), // Huge one. + (vec![0x1E, 1, 0, 0x1E, 2, 0], vec![]), // Unused operand. + (vec![0x21, 0xD4, 0x01], vec![]), // Unassigned function number. + (vec![0x2C, 0, 0, 0, 0, 0, 0], vec![]), // PtgRefN outside a shared formula. + (vec![0x23, 0xFF, 0xFF, 0xFF, 0xFF], vec![]), // Name index past the list. + ([&[0x5Au8][..], &9u16.to_le_bytes(), &loc(0, 0)].concat(), vec![]), // Missing sheet link. + (deep, vec![]), + ]; + for (rgce, rgcb) in bad { + let data = [row(0), formula_cell(0, &rgce, &rgcb)].concat(); + let wb = read(&package(&book_with_links(&xti()), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES)).unwrap(); + assert_eq!(wb.sheets[0].formula(CellRef::new(0, 0)), None, "{rgce:?}"); + assert_eq!(wb.sheets[0].value(CellRef::new(0, 0)), Value::Number(0.0)); + } +} + +#[test] +fn python_formulas_need_their_code_in_the_cell() { + // `PY("code", 0)` carries its code; `PY(0, 0)` names a script elsewhere in the package, which + // the reader doesn't read, and keeps its saved result. + let mut book = book_with_links(&xti()); + let end = record(132, &[]); + book.truncate(book.len() - end.len()); + book.extend(name(0x0002_000b, "_xlfn._xlws.PY")); + book.extend(end); + let py = |arg: &[u8]| [&[0x23u8, 5, 0, 0, 0][..], arg, &[0x1E, 0, 0, 0x42, 3, 0xFF, 0]].concat(); + // Code longer than one string constant is written as constants joined with `&`. + let joined = [&[0x17u8, 1, 0, b'1', 0][..], &[0x17, 2, 0, b'+', 0, b'1', 0], &[0x08]].concat(); + // The same strings nested to the right, `"1"&("+"&"1")` by token order: still one flat chain. + let nested = [&[0x17u8, 1, 0, b'1', 0][..], &[0x17, 1, 0, b'+', 0], &[0x17, 1, 0, b'1', 0], &[0x08, 0x08]].concat(); + let data = [ + row(0), + formula_cell(0, &py(&[0x17, 1, 0, b'1', 0]), &[]), + formula_cell(1, &py(&[0x1E, 0, 0]), &[]), + formula_cell(2, &py(&joined), &[]), + formula_cell(3, &py(&nested), &[]), + ] + .concat(); + let wb = read(&package(&book, &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES)).unwrap(); + let formula = |c| wb.sheets[0].formula(CellRef::new(0, c)); + assert_eq!(formula(0), Some("_xlfn._xlws.PY(\"1\",0)")); + assert_eq!(formula(1), None); + assert_eq!(formula(2), Some("_xlfn._xlws.PY(\"1\"&\"+1\",0)")); + assert_eq!(formula(3), Some("_xlfn._xlws.PY(\"1\"&\"+\"&\"1\",0)")); + assert!(warned(&wb, "Python code stored outside"), "{:?}", wb.warnings); +} + +#[test] +fn formula_text_past_the_allowance_keeps_the_saved_result() { + // `1` takes the last byte of a one-byte allowance; the next formula doesn't fit. + let data = [row(0), formula_cell(0, &[0x1E, 1, 0], &[]), formula_cell(1, &[0x1E, 1, 0], &[])].concat(); + let limits = Limits { binary_formula_text: 1, ..Limits::default() }; + let wb = read_with(&package(&book_with_links(&xti()), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES), &limits).unwrap(); + assert_eq!(wb.sheets[0].formula(CellRef::new(0, 0)), Some("1")); + assert_eq!(wb.sheets[0].formula(CellRef::new(0, 1)), None); + assert!(warned(&wb, "more formula text than the limits allow"), "{:?}", wb.warnings); +} From 734ade8cd988f95c318c788e810a07b86f3bc880 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Thu, 12 Nov 2026 23:34:15 -0500 Subject: [PATCH 03/14] craft-xlsx: read shared, array and dynamic-array formulas from .xlsb files (port of gridcraft#3821) A cell whose tokens are only PtgExp (row) + PtgExtraCol (column) belongs to the shared or array formula starting there; its first cell is followed by BrtShrFmla or BrtArrFmla. - Shared formulas: each cell's copy is decoded from the shared tokens at its own cell, as [MS-XLSB] defines it (PtgRefN/PtgAreaN offsets, wrapping around the grid). Every cell of the group gets FormulaKind::Shared, its own text and the group's index (`si`, counted per sheet); the first also gets the group's range. Its first cell is the first that uses it, anywhere in its range. Copies are charged to the formula text allowance before they are decoded, so a long formula shared by a million cells stops costing once the allowance is spent. - Ctrl+Shift+Enter and dynamic arrays: the first cell gets FormulaKind::Array with its range; a dynamic one also has its cell metadata (BrtCellMeta, which now also holds across a BrtValueMeta written in between), as `cm` marks it in .xlsx. The other cells of an array keep their cached values and have no formula, as in .xlsx. - A record that can't be read leaves its range as values and never fails the file, with a warning; so does a first cell with no formula record after it. Ported from storytold/gridcraft#3821 (issue gridcraft#3806), with its review fixes (each copy decoded at its own cell; a shared formula may start inside its range). Changed from the GridCraft PR: - In .xlsx a shared formula's other cells have empty text. Here they have their own decoded text (and CellFormula's doc says so), because that is exact where shifting the first cell's text is not, as the PR's review found. - Clearing an array's other cells for recalculation, marking dynamic arrays to spill, and the check against GridCraft's parser's 8,192-character limit are GridCraft's reading, not the file's: the cells keep what the file holds. That test becomes one that a copy may be longer than its first cell. The PR's tests come along in tests/xlsb.rs, with craft-xlsx's model in the expectations. Checked against Excel: in a workbook Excel saved both as .xlsb and as .xlsx, every formula this reads (33, shared, array and dynamic ones included) has the .xlsx text, kind and range; the other 5 use tables, an external link or defined names. --- crates/xlsx/src/sheet.rs | 6 +- crates/xlsx/src/xlsb.rs | 210 ++++++++++++++++++++++++++++++-- crates/xlsx/src/xlsb/formula.rs | 51 +++++++- crates/xlsx/tests/xlsb.rs | 170 +++++++++++++++++++++++++- 4 files changed, 415 insertions(+), 22 deletions(-) diff --git a/crates/xlsx/src/sheet.rs b/crates/xlsx/src/sheet.rs index 243430f8e..2691764d5 100644 --- a/crates/xlsx/src/sheet.rs +++ b/crates/xlsx/src/sheet.rs @@ -113,8 +113,10 @@ pub enum FormulaKind { #[derive(Clone, Debug, Default, PartialEq)] pub struct CellFormula { pub kind: FormulaKind, - /// The formula as the file spells it (`_xlfn.` prefixes and all), trimmed. Empty for a - /// cell that shares the formula of its group's first cell. + /// The formula as the file spells it (`_xlfn.` prefixes and all), trimmed. In `.xlsx`, empty + /// for a cell that shares the formula of its group's first cell. A binary workbook (`.xlsb`) + /// stores a shared formula's tokens once, so every cell of the group gets its own text, + /// decoded at the cell. pub text: String, /// `si`: the shared formula group. pub shared_index: Option, diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index aedbae5ab..dfea974b6 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -7,17 +7,18 @@ mod expr; mod formula; mod ftab; +use std::collections::HashMap; use std::sync::Arc; use craft_opc::{ContentTypes, Rels}; use craft_sheet::{CellError, CellRef, MAX_COLS, MAX_ROWS, Range, Visibility}; -use crate::sheet::{Cell, CellFormula, CellValue, Row, SheetPart}; +use crate::sheet::{Cell, CellFormula, CellValue, FormulaKind, Row, SheetPart}; use crate::strings::StringItem; use crate::workbook::{SheetEntry, SheetKind, Workbook}; use crate::{Error, Limits, Warnings}; pub(crate) use formula::Links; -use formula::{Book, Name, Xti}; +use formula::{Book, Mode, Name, Xti}; pub(crate) const MAIN: &str = "application/vnd.ms-excel.main"; const BINARY_MAIN: &str = "application/vnd.ms-excel.sheet.binary.macroEnabled.main"; @@ -57,6 +58,11 @@ pub(crate) struct Budget { impl Budget { /// Charges a decoded formula's text if it fits [`Limits::binary_formula_text`]; a formula /// that doesn't keeps its saved result. + /// Whether formula text of `len` bytes would still fit, without charging it. + fn formula_text_fits(&self, len: usize, limits: &Limits) -> bool { + self.formula_text.get().saturating_add(len) <= limits.binary_formula_text + } + fn try_formula_text(&self, len: usize, limits: &Limits) -> bool { let total = self.formula_text.get().saturating_add(len); let fits = total <= limits.binary_formula_text; @@ -506,22 +512,178 @@ fn cell_formula<'a>(b: &mut Bytes<'a>) -> Result<(&'a [u8], &'a [u8]), Error> { /// A formula's text, or `None` (with a warning saying why) for one that keeps its saved result: /// the workbook's links couldn't be read, the tokens can't be decoded, or the formula text /// allowance is spent. -fn decode_formula(rgce: &[u8], rgcb: &[u8], links: Option<&Links>, limits: &Limits, budget: &Budget, warnings: &mut Warnings) -> Option { +fn decode_formula( + rgce: &[u8], + rgcb: &[u8], + mode: Mode, + links: Option<&Links>, + limits: &Limits, + budget: &Budget, + warnings: &mut Warnings, +) -> Option { let links = links?; - let text = match formula::decode(rgce, rgcb, links) { + let text = match formula::decode(rgce, rgcb, links, mode) { Ok(e) => expr::print(&e), Err(formula::Unsupported(why)) => { - warnings.warn(format!("XLSB: some formulas were kept as their saved results ({why})")); + kept(warnings, why); return None; } }; if !budget.try_formula_text(text.len(), limits) { - warnings.warn("XLSB: some formulas were kept as their saved results (more formula text than the limits allow)"); + kept(warnings, "more formula text than the limits allow"); return None; } Some(text) } +/// Warns, once per reason, that formulas were kept as their saved results. +fn kept(warnings: &mut Warnings, why: &str) { + warnings.warn(format!("XLSB: some formulas were kept as their saved results ({why})")); +} + +/// A `BrtShrFmla`: the cells it may cover, its tokens, its first cell's text length (what a copy +/// is expected to cost) and the group's index (`si`). +struct SharedFormula { + range: Range, + rgce: Vec, + rgcb: Vec, + len: usize, + si: u32, +} + +/// A formula cell whose tokens are only `PtgExp`: part of a shared or array formula. +enum Part { + /// The first cell: its formula is the `BrtShrFmla` or `BrtArrFmla` record that follows. + First, + /// Another cell of the formula whose first cell is this. + Of(CellRef), +} + +/// `PtgExp` ([MS-XLSB] 2.5.98.40) with its `PtgExtraCol`: the first cell of the shared or array +/// formula this cell belongs to. BIFF12 keeps only the row in the token and the column in rgcb. +fn part_of(rgce: &[u8], rgcb: &[u8], at: CellRef) -> Option> { + let (&ptg, row) = rgce.split_first()?; + if ptg != 0x01 { + return None; + } + let (Ok(row), Ok(col)) = (<[u8; 4]>::try_from(row), <[u8; 4]>::try_from(rgcb)) else { + return Some(Err("malformed shared or array formula references")); + }; + let first = CellRef::new(u32::from_le_bytes(row), u32::from_le_bytes(col)); + Some(Ok(if first == at { Part::First } else { Part::Of(first) })) +} + +/// One sheet's shared and array formulas, by first cell, as the reader meets them. `None` when +/// the formula keeps its values (so its other cells keep theirs). +#[derive(Default)] +struct RangeFormulas { + shared: HashMap>>, + arrays: HashMap>, +} + +impl RangeFormulas { + /// The formula of another cell `at` of the shared or array formula starting at `first`. A + /// shared formula's copy is decoded from the shared tokens at its own cell, as [MS-XLSB] + /// defines it (relative offsets wrap around the grid), and checked like any formula: its text + /// may be longer than the first cell's. That length estimates its cost, and a copy that can't + /// fit isn't decoded at all, so a long formula shared by a million cells stops costing once + /// the allowance is spent. An array formula's other cells have none, as in `.xlsx`. + fn follower( + &self, + at: CellRef, + first: CellRef, + links: Option<&Links>, + limits: &Limits, + budget: &Budget, + warnings: &mut Warnings, + ) -> Option { + if let Some(shared) = self.shared.get(&first) { + let sh = shared.as_ref()?; + if !sh.range.contains(at) { + kept(warnings, "malformed shared or array formula references"); + return None; + } + if !budget.formula_text_fits(sh.len, limits) { + kept(warnings, "more formula text than the limits allow"); + return None; + } + let text = decode_formula(&sh.rgce, &sh.rgcb, Mode::Shared(at), links, limits, budget, warnings)?; + return Some(CellFormula { kind: FormulaKind::Shared, text, shared_index: Some(sh.si), ..CellFormula::default() }); + } + match self.arrays.get(&first) { + Some(Some(range)) if range.contains(at) => {} + Some(Some(_)) => kept(warnings, "malformed shared or array formula references"), + Some(None) => {} + None => kept(warnings, "a shared or array formula that is missing from the file"), + } + None + } + + /// `BrtShrFmla` (427) or `BrtArrFmla` (426) right after the first cell `first` of its range: + /// the formula that cell keeps, recorded for the range's other cells. A record that can't be + /// read leaves the whole range as values; it never fails the file, as these records were + /// skipped before formulas were read. + #[allow(clippy::too_many_arguments)] + fn range_formula( + &mut self, + id: u16, + payload: &[u8], + first: CellRef, + links: Option<&Links>, + limits: &Limits, + budget: &Budget, + warnings: &mut Warnings, + ) -> Option { + let read = |b: &mut Bytes<'_>| -> Result<(Range, Vec, Vec), Error> { + let (r0, r1, c0, c1) = (b.u32()?, b.u32()?, b.u32()?, b.u32()?); + if id == 426 { + b.u8()?; // Only fAlwaysCalc, the calculator's to know. + } + let cce = b.u32()?; + if !(1..=16384).contains(&cce) { + return Err(invalid("invalid formula token length")); + } + let rgce = b.take(cce as usize)?.to_vec(); + let cb = b.u32()?; + let rgcb = b.take(cb as usize)?.to_vec(); + if r0 > r1 || c0 > c1 || r1 >= MAX_ROWS || c1 >= MAX_COLS { + return Err(invalid("invalid formula range")); + } + let range = Range::new(CellRef::new(r0, c0), CellRef::new(r1, c1)); + // An array formula's first cell is its range's top-left one. A shared formula's is the + // first cell that uses it, which need not be: the top-left cell may not share it. + let start_ok = if id == 427 { range.contains(first) } else { range.first == first }; + if !start_ok { + return Err(invalid("invalid formula range")); + } + Ok((range, rgce, rgcb)) + }; + let mut b = Bytes::new(payload); + let Ok((range, rgce, rgcb)) = read(&mut b).and_then(|r| b.finish().map(|()| r)) else { + kept(warnings, "malformed shared or array formulas"); + self.shared.insert(first, None); + self.arrays.insert(first, None); + return None; + }; + if id == 427 { + let text = decode_formula(&rgce, &rgcb, Mode::Shared(first), links, limits, budget, warnings); + let si = u32::try_from(self.shared.len()).unwrap_or(u32::MAX); + let shared = text.as_ref().map(|t| Arc::new(SharedFormula { range, rgce, rgcb, len: t.len(), si })); + self.shared.insert(first, shared); + return text.map(|text| CellFormula { + kind: FormulaKind::Shared, + text, + shared_index: Some(si), + range: Some(range), + ..CellFormula::default() + }); + } + let text = decode_formula(&rgce, &rgcb, Mode::Cell, links, limits, budget, warnings); + self.arrays.insert(first, text.as_ref().map(|_| range)); + text.map(|text| CellFormula { kind: FormulaKind::Array, text, range: Some(range), ..CellFormula::default() }) + } +} + fn value(id: u16, b: &mut Bytes<'_>, strings: usize, limits: &Limits, budget: &Budget) -> Result { let value = match id { 1 => CellValue::Empty, @@ -579,11 +741,25 @@ pub(crate) fn worksheet( let mut row: Option = None; let mut col = None; // BrtCellMeta applies to the cell record that follows it (a dynamic-array formula), as `cm` - // does in `.xlsx`: a one-based index into the metadata part's cell metadata. + // does in `.xlsx`: a one-based index into the metadata part's cell metadata. Excel may write + // the cell's BrtValueMeta (a rich value, such as a spill error) in between. let mut meta: Option = None; + let mut ranges = RangeFormulas::default(); + // The first cell of a shared or array formula, waiting for the record holding the formula. + let mut first: Option = None; while let Some((id, payload)) = records.next(warnings)? { let mut b = Bytes::new(payload); - let cell_metadata = meta.take(); + let cell_metadata = if id == 50 { meta } else { meta.take() }; + if let Some(at) = first.take() { + if matches!(id, 426 | 427) { + let f = ranges.range_formula(id, payload, at, links, limits, budget, warnings); + if let Some(cell) = row.as_mut().and_then(|r| r.cells.last_mut()).filter(|c| c.at == at) { + cell.formula = f; + } + continue; + } + kept(warnings, "a shared or array formula that is missing from the file"); + } match id { 49 => { let icmb = b.i32()?; @@ -648,10 +824,24 @@ pub(crate) fn worksheet( } let style = b.u32()?; let value = value(id, &mut b, strings, limits, budget)?; + let at = CellRef::new(current.index, c); let formula = if (8..=11).contains(&id) { let (rgce, rgcb) = cell_formula(&mut b)?; b.finish()?; - decode_formula(rgce, rgcb, links, limits, budget, warnings).map(|text| CellFormula { text, ..CellFormula::default() }) + match part_of(rgce, rgcb, at) { + None => decode_formula(rgce, rgcb, Mode::Cell, links, limits, budget, warnings) + .map(|text| CellFormula { text, ..CellFormula::default() }), + Some(Ok(Part::First)) => { + first = links.map(|_| at); + None + } + Some(Ok(Part::Of(start))) if links.is_some() => ranges.follower(at, start, links, limits, budget, warnings), + Some(Ok(Part::Of(_))) => None, + Some(Err(why)) => { + kept(warnings, why); + None + } + } } else { b.finish()?; None @@ -660,7 +850,7 @@ pub(crate) fn worksheet( budget.cell(limits)?; } current.cells.push(Cell { - at: CellRef::new(current.index, c), + at, style: style & 0x00FF_FFFF, phonetic: style & 0x0100_0000 != 0, value, diff --git a/crates/xlsx/src/xlsb/formula.rs b/crates/xlsx/src/xlsb/formula.rs index bed0adcd0..53b02f8a1 100644 --- a/crates/xlsx/src/xlsb/formula.rs +++ b/crates/xlsx/src/xlsb/formula.rs @@ -11,7 +11,7 @@ use std::sync::Arc; -use craft_sheet::{CellError, MAX_COLS, MAX_ROWS}; +use craft_sheet::{CellError, CellRef, MAX_COLS, MAX_ROWS}; use super::expr::{Anchor, BinOp, Expr, RefKind, Reference, SheetSel, UnOp}; use super::ftab::function; @@ -192,6 +192,14 @@ impl Bytes<'_> { const LAST_ROW: u32 = MAX_ROWS - 1; const LAST_COL: u32 = MAX_COLS - 1; +/// Where a formula lives: a cell formula's references hold coordinates; a shared formula's +/// relative tokens (`PtgRefN`, `PtgAreaN`) hold offsets from the shared formula's first cell. +#[derive(Clone, Copy, PartialEq)] +pub(super) enum Mode { + Cell, + Shared(CellRef), +} + /// RgceLoc ([MS-XLSB] 2.5.98.91): a row and a `ColRelShort` (14-bit column, then the /// column-relative and row-relative flags). In a cell formula the coordinates are absolute /// whatever the flags say; the flags only decide where `$` goes. @@ -203,9 +211,29 @@ fn anchor(row: u32, col: u16) -> Result { Ok(Anchor { row, col: c, row_abs: col & 0x8000 == 0, col_abs: col & 0x4000 == 0 }) } +/// RgceLocRel ([MS-XLSB] 2.5.98.92), in a shared formula: a relative row or column is an offset +/// from `base`, wrapping around the grid (1,048,576 rows, 16,384 columns), as Excel adds them. +/// An absolute one is a coordinate. +fn relative_anchor(row: u32, col: u16, base: CellRef) -> Result { + let (row_rel, col_rel) = (col & 0x8000 != 0, col & 0x4000 != 0); + let r = if row_rel { base.row.wrapping_add(row) % MAX_ROWS } else { row }; + let c = if col_rel { (base.col + u32::from(col & 0x3FFF)) % MAX_COLS } else { u32::from(col & 0x3FFF) }; + anchor(r, (c as u16) | (col & 0xC000)) +} + +fn loc(row: u32, col: u16, mode: Mode) -> Result { + match mode { + Mode::Cell => anchor(row, col), + Mode::Shared(base) => relative_anchor(row, col, base), + } +} + /// RgceArea: a range spanning every row (or column) is a whole-column (row) reference. fn area(r0: u32, r1: u32, c0: u16, c1: u16) -> Result { - let (a, b) = (anchor(r0, c0)?, anchor(r1, c1)?); + area_of(anchor(r0, c0)?, anchor(r1, c1)?) +} + +fn area_of(a: Anchor, b: Anchor) -> Result { Ok(if a.row == 0 && b.row == LAST_ROW { RefKind::Cols(a.col, a.col_abs, b.col, b.col_abs) } else if a.col == 0 && b.col == LAST_COL { @@ -222,10 +250,11 @@ fn reference(sheet: Option, kind: RefKind) -> Expr { } } -/// Decodes a cell formula's `rgce` (tokens) with its `rgcb` (extra data for array constants and +/// Decodes a formula's `rgce` (tokens) with its `rgcb` (extra data for array constants and /// cached areas). Returns the expression, with names in their file spelling, or why it can't be -/// represented. -pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links) -> Result { +/// represented. A shared formula's tokens are decoded once for each of its cells +/// (`Mode::Shared` with that cell), whose relative references are offsets from it. +pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Result { if rgce.len() > MAX_RGCE { return Err(Unsupported("formula too long")); } @@ -414,7 +443,17 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links) -> Result return Err(Unsupported("the shared or array formula form")), 0x02 => return Err(Unsupported("what-if data tables")), 0x18 => return Err(Unsupported("structured table references")), - 0x2C | 0x2D => return Err(Unsupported("relative references in a cell formula")), + 0x2C => { + let Mode::Shared(_) = mode else { return Err(Unsupported("relative references in a cell formula")) }; + let (r, c) = (t.u32()?, t.u16()?); + push(&mut stack, kids, Expr::Ref(Reference { sheet: SheetSel::Current, kind: RefKind::Cell(loc(r, c, mode)?) }))?; + } + 0x2D => { + let Mode::Shared(_) = mode else { return Err(Unsupported("relative references in a cell formula")) }; + let (r0, r1, c0, c1) = (t.u32()?, t.u32()?, t.u16()?, t.u16()?); + let kind = area_of(loc(r0, c0, mode)?, loc(r1, c1, mode)?)?; + push(&mut stack, kids, Expr::Ref(Reference { sheet: SheetSel::Current, kind }))?; + } _ => return Err(Unsupported("unknown formula token")), } if stack.len() > MAX_RGCE { diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index 42c27158b..d2b03dbbd 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -5,7 +5,7 @@ mod common; use common::{read, read_with}; use craft_xlsx::testing::zip; -use craft_xlsx::{CellError, CellRef, Error, Limits, Range, SheetKind, Value, Visibility, is_xlsb}; +use craft_xlsx::{CellError, CellRef, Error, FormulaKind, Limits, Range, SheetKind, Value, Visibility, is_xlsb}; const ROOT: &str = r#""#; const RELS: &str = r#""#; @@ -546,7 +546,7 @@ fn cell_formulas_are_read_in_their_xlsx_spelling() { // Kept as their saved results: (11, vec![0x43, 4, 0, 0, 0, 0x1E, 2, 0, 0x05], vec![]), (12, ref3d(4, 0, REL), vec![]), - (13, vec![0x01, 0, 0, 0, 0], vec![0, 0, 0, 0]), + (13, vec![0x01, 0, 0, 0, 0], vec![0, 0, 0, 0]), // Part of a shared formula at A1 that isn't one. ]; let mut data = row(0); data.extend(cell(5, 0, &3.0f64.to_le_bytes())); @@ -580,11 +580,11 @@ fn cell_formulas_are_read_in_their_xlsx_spelling() { None, ]; assert_eq!(got, want); - assert!(sheet.formulas.values().all(|f| f.kind == craft_xlsx::FormulaKind::Normal)); + assert!(sheet.formulas.values().all(|f| f.kind == FormulaKind::Normal)); assert_eq!(sheet.cell_metadata.iter().collect::>(), [(&CellRef::new(0, 14), &1)]); // Every formula cell keeps its saved result as its value. assert!((1..=15).all(|c| sheet.value(CellRef::new(0, c)) == Value::Number(0.0))); - for why in ["defined names", "other workbooks", "the shared or array formula form", "structured table references"] { + for why in ["defined names", "other workbooks", "missing from the file", "structured table references"] { assert!(warned(&wb, why), "{why}: {:?}", wb.warnings); } } @@ -709,3 +709,165 @@ fn formula_text_past_the_allowance_keeps_the_saved_result() { assert_eq!(wb.sheets[0].formula(CellRef::new(0, 1)), None); assert!(warned(&wb, "more formula text than the limits allow"), "{:?}", wb.warnings); } + +fn range_formula(id: u16, (r0, c0, r1, c1): (u32, u32, u32, u32), rgce: &[u8]) -> Vec { + let mut data = vec![]; + for n in [r0, r1, c0, c1] { + data.extend(n.to_le_bytes()); + } + if id == 426 { + data.push(0x08); // The flags Excel writes for an ordinary array. + } + data.extend((rgce.len() as u32).to_le_bytes()); + data.extend(rgce); + data.extend(0u32.to_le_bytes()); + record(id, &data) +} + +/// A cell of the shared or array formula whose first cell is `row`, `col` (PtgExp + PtgExtraCol). +fn part(col: u32, (row, first_col): (u32, u32)) -> Vec { + formula_cell(col, &[&[0x01u8][..], &row.to_le_bytes()].concat(), &first_col.to_le_bytes()) +} + +fn ref_n(row: i32, col: u16) -> Vec { + [&[0x4Cu8][..], &row.to_le_bytes(), &col.to_le_bytes()].concat() +} + +fn at(a1: &str) -> CellRef { + CellRef::parse(a1).unwrap() +} + +#[test] +fn shared_array_and_dynamic_formulas_read_as_from_xlsx() { + // As Excel writes them: C1:C3 share `A1*10` (PtgRefN, column offset -2 as 16382); D1:E2 + // share `$A1+A$1`; F1:F2 hold the Ctrl+Shift+Enter array {=A1:A2*2}; G1 spills `=A1:A2`. + let area = [&[0x45u8][..], &0u32.to_le_bytes(), &1u32.to_le_bytes(), &REL.to_le_bytes(), &REL.to_le_bytes()].concat(); + let mut data = row(0); + data.extend(cell(5, 0, &1.0f64.to_le_bytes())); + data.extend(part(2, (0, 2))); + data.extend(range_formula(427, (0, 2, 2, 2), &[ref_n(0, 16382 | REL), vec![0x1E, 10, 0, 0x05]].concat())); + data.extend(part(3, (0, 3))); + data.extend(range_formula(427, (0, 3, 1, 4), &[ref_n(0, 0x8000), ref_n(0, 16381 | 0x4000), vec![0x03]].concat())); + data.extend(part(4, (0, 3))); + data.extend(part(5, (0, 5))); + data.extend(range_formula(426, (0, 5, 1, 5), &[area.clone(), vec![0x1E, 2, 0, 0x05]].concat())); + data.extend(record(49, &1u32.to_le_bytes())); + data.extend(record(50, &1u32.to_le_bytes())); // BrtValueMeta in between, as Excel writes for a spill error. + data.extend(part(6, (0, 6))); + data.extend(range_formula(426, (0, 6, 1, 6), &area)); + data.extend(row(1)); + data.extend(cell(5, 0, &2.0f64.to_le_bytes())); + for (col, first) in [(2, (0, 2)), (3, (0, 3)), (4, (0, 3)), (5, (0, 5)), (6, (0, 6))] { + data.extend(part(col, first)); + } + data.extend(row(2)); + data.extend(part(2, (0, 2))); + let wb = read(&file(&worksheet(&data, &[]))).unwrap(); + let sheet = &wb.sheets[0]; + let formula = |a: &str| sheet.formulas.get(&at(a)); + // Each cell of a shared formula has its own text, decoded at the cell: the first with the + // group's range, all with its index. + for (a, want, si) in [ + ("C1", "A1*10", 0), + ("C2", "A2*10", 0), + ("C3", "A3*10", 0), + ("D1", "$A1+A$1", 1), + ("E1", "$A1+B$1", 1), + ("D2", "$A2+A$1", 1), + ("E2", "$A2+B$1", 1), + ] { + let f = formula(a).unwrap_or_else(|| panic!("{a}")); + assert_eq!((f.kind, f.text.as_str(), f.shared_index), (FormulaKind::Shared, want, Some(si)), "{a}"); + } + assert_eq!(formula("C1").and_then(|f| f.range), Range::parse("C1:C3")); + assert_eq!(formula("D1").and_then(|f| f.range), Range::parse("D1:E2")); + assert_eq!(formula("C2").and_then(|f| f.range), None); + // An array formula's first cell has it, with its range; a dynamic array's has its cell + // metadata too, as `cm` in .xlsx, though Excel wrote a BrtValueMeta in between. + let f1 = formula("F1").unwrap(); + assert_eq!((f1.kind, f1.text.as_str(), f1.range), (FormulaKind::Array, "A1:A2*2", Range::parse("F1:F2"))); + let g1 = formula("G1").unwrap(); + assert_eq!((g1.kind, g1.text.as_str(), g1.range), (FormulaKind::Array, "A1:A2", Range::parse("G1:G2"))); + assert_eq!(sheet.cell_metadata.get(&at("G1")), Some(&1)); + assert_eq!(sheet.cell_metadata.get(&at("F1")), None); + // Their other cells have no formula and keep their saved results, as in .xlsx. + for a in ["F2", "G2"] { + assert!(formula(a).is_none(), "{a}"); + assert_eq!(sheet.value(at(a)), Value::Number(0.0), "{a}"); + } + assert!(!wb.warnings.iter().any(|w| w.contains("kept as their saved results")), "{:?}", wb.warnings); +} + +#[test] +fn unreadable_shared_formulas_keep_their_values() { + // A shared formula with an unsupported token, a range that doesn't start at its first cell, + // a truncated record, and a first cell with no formula record after it: every cell of each + // keeps its saved result, and the file opens. + let unknown = range_formula(427, (0, 0, 1, 0), &[0xFF]); + let wrong_start = range_formula(427, (0, 1, 1, 1), &[0x1E, 1, 0]); + // A well-framed record whose payload stops before rgcb's length. + let short: Vec = [&[0u8; 4][..], &1u32.to_le_bytes(), &[0; 8], &3u32.to_le_bytes(), &[0x1E, 1, 0]].concat(); + let truncated = record(427, &short); + for after_first in [unknown, wrong_start, truncated, cell(5, 1, &1.0f64.to_le_bytes())] { + let data = [row(0), part(0, (0, 0)), after_first, row(1), part(0, (0, 0))].concat(); + let wb = read(&file(&worksheet(&data, &[]))).unwrap(); + for r in [0, 1] { + assert_eq!(wb.sheets[0].formula(CellRef::new(r, 0)), None); + assert_eq!(wb.sheets[0].value(CellRef::new(r, 0)), Value::Number(0.0)); + } + assert!(wb.warnings.iter().any(|w| w.contains("kept as their saved results")), "{:?}", wb.warnings); + } +} + +#[test] +fn shared_formulas_wrap_and_may_start_inside_their_range() { + // `=` the cell to the left (column offset -1, written 16383), shared over A1:B2 from B1: A1 + // isn't part of it. In column A the offset wraps around the grid, as [MS-XLSB] defines it. + let left = ref_n(0, 16383 | REL); + let data = [ + row(0), + cell(5, 0, &1.0f64.to_le_bytes()), + part(1, (0, 1)), + range_formula(427, (0, 0, 1, 1), &left), + row(1), + part(0, (0, 1)), + part(1, (0, 1)), + ] + .concat(); + let wb = read(&file(&worksheet(&data, &[]))).unwrap(); + let text = |a: &str| wb.sheets[0].formula(at(a)); + assert_eq!((text("A1"), text("B1"), text("A2"), text("B2")), (None, Some("A1"), Some("XFD2"), Some("A2"))); + assert!(!wb.warnings.iter().any(|w| w.contains("kept as their saved results")), "{:?}", wb.warnings); +} + +#[test] +fn a_shared_formula_copy_may_be_longer_than_its_first_cell() { + // SUM of nine SUMs of 255 references to the cell on the left: about 6,900 characters at B9, + // over 18,000 at B999999, where each `A9` is `A999999`. Each copy has its own text. + let mut rgce = vec![]; + for _ in 0..9 { + for _ in 0..255 { + rgce.extend(ref_n(0, 16383 | REL)); + } + rgce.extend([0x22, 255, 4, 0]); + } + rgce.extend([0x22, 9, 4, 0]); + let data = [row(8), part(1, (8, 1)), range_formula(427, (8, 1, 999_998, 1), &rgce), row(999_998), part(1, (8, 1))].concat(); + let wb = read(&file(&worksheet(&data, &[]))).unwrap(); + let (b9, last) = (wb.sheets[0].formula(at("B9")).unwrap(), wb.sheets[0].formula(at("B999999")).unwrap()); + assert!(b9.len() < 8192 && last.len() > 18_000, "{} {}", b9.len(), last.len()); + assert!(last.starts_with("SUM(SUM(A999999,A999999"), "{}", &last[..40]); +} + +#[test] +fn shared_formula_copies_are_charged_before_they_are_made() { + // A1:A3 share `1+1` (3 bytes); with 6 bytes allowed, the first cell and one copy fit, and the + // last copy keeps its value without being decoded. + let one_plus_one = [0x1E, 1, 0, 0x1E, 1, 0, 0x03]; + let data = [row(0), part(0, (0, 0)), range_formula(427, (0, 0, 2, 0), &one_plus_one), row(1), part(0, (0, 0)), row(2), part(0, (0, 0))].concat(); + let limits = Limits { binary_formula_text: 6, ..Limits::default() }; + let wb = read_with(&file(&worksheet(&data, &[])), &limits).unwrap(); + let got: Vec<_> = (0..3).map(|r| wb.sheets[0].formula(CellRef::new(r, 0))).collect(); + assert_eq!(got, [Some("1+1"), Some("1+1"), None]); + assert!(warned(&wb, "more formula text than the limits allow"), "{:?}", wb.warnings); +} From 1f5f1f0f5143c312650c972bae48961414906b00 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Thu, 12 Nov 2026 23:36:18 -0500 Subject: [PATCH 04/14] craft-xlsx: a shared formula stops decoding copies once one is kept as its value From gridcraft#3821's review (CodeRabbit, still open there): a shared formula's copies are checked against the formula text allowance with the first cell's length, which a copy can exceed. A copy that was decoded and printed, then kept as its value because it didn't fit, or because it couldn't be decoded, charged nothing, so a file with many cells of one long shared formula could repeat that work for each of them. The PR's case was GridCraft's 8,192-character rejection, which isn't here; the allowance and decode failures are the same path. After one such copy the group keeps every later copy as its value without decoding it, so a group costs at most one wasted decode. A new test shows a later copy that would have fit kept as its value; it fails without this commit. --- crates/xlsx/src/xlsb.rs | 13 ++++++++++--- crates/xlsx/tests/xlsb.rs | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index dfea974b6..d0bf980c9 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -589,7 +589,7 @@ impl RangeFormulas { /// fit isn't decoded at all, so a long formula shared by a million cells stops costing once /// the allowance is spent. An array formula's other cells have none, as in `.xlsx`. fn follower( - &self, + &mut self, at: CellRef, first: CellRef, links: Option<&Links>, @@ -598,7 +598,7 @@ impl RangeFormulas { warnings: &mut Warnings, ) -> Option { if let Some(shared) = self.shared.get(&first) { - let sh = shared.as_ref()?; + let sh = shared.clone()?; if !sh.range.contains(at) { kept(warnings, "malformed shared or array formula references"); return None; @@ -607,7 +607,14 @@ impl RangeFormulas { kept(warnings, "more formula text than the limits allow"); return None; } - let text = decode_formula(&sh.rgce, &sh.rgcb, Mode::Shared(at), links, limits, budget, warnings)?; + let Some(text) = decode_formula(&sh.rgce, &sh.rgcb, Mode::Shared(at), links, limits, budget, warnings) else { + // A copy that was decoded but kept as its value charged nothing: one past the + // allowance (it may be longer than the first cell's estimate) or one that can't be + // decoded. The group's later copies would fail the same way, so they keep their + // values without being decoded: a group costs at most one wasted decode. + self.shared.insert(first, None); + return None; + }; return Some(CellFormula { kind: FormulaKind::Shared, text, shared_index: Some(sh.si), ..CellFormula::default() }); } match self.arrays.get(&first) { diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index d2b03dbbd..3f1c51b7f 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -871,3 +871,17 @@ fn shared_formula_copies_are_charged_before_they_are_made() { assert_eq!(got, [Some("1+1"), Some("1+1"), None]); assert!(warned(&wb, "more formula text than the limits allow"), "{:?}", wb.warnings); } + +#[test] +fn a_shared_formula_stops_decoding_copies_once_one_is_kept_as_its_value() { + // A1:Z2 share `=` the cell to the right. A1 is `B1` (2 bytes), Z1's copy is `AA1` (3): it + // passes the estimate from the first cell but not the allowance, after being decoded. The + // group's later copies keep their values without being decoded, though B2's (`C2`) would fit. + let right = ref_n(0, 1 | REL); + let data = [row(0), part(0, (0, 0)), range_formula(427, (0, 0, 1, 25), &right), part(25, (0, 0)), row(1), part(1, (0, 0))].concat(); + let limits = Limits { binary_formula_text: 4, ..Limits::default() }; + let wb = read_with(&file(&worksheet(&data, &[])), &limits).unwrap(); + let text = |a: &str| wb.sheets[0].formula(at(a)); + assert_eq!((text("A1"), text("Z1"), text("B2")), (Some("B1"), None, None)); + assert!(warned(&wb, "more formula text than the limits allow"), "{:?}", wb.warnings); +} From 20050077adcd794cfef32805f73de6191069962d Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Thu, 12 Nov 2026 23:36:47 -0500 Subject: [PATCH 05/14] craft-xlsx: the README describes formulas in binary workbooks They were "token streams that are not decoded". The README now says what the decoder gives (text in the file's spelling, shared and array kinds and ranges, cell metadata, each shared copy's own text), what keeps its saved result and why, that the function table and printer are this crate's own, and the binary_formula_text limit with the decoder's fixed bounds. --- crates/xlsx/README.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/xlsx/README.md b/crates/xlsx/README.md index 3d3808ad3..1dc30574d 100644 --- a/crates/xlsx/README.md +++ b/crates/xlsx/README.md @@ -83,9 +83,15 @@ for new pictures (`vm = i + 1`) and the `XLDAPR` dynamic-array metadata (`cm = 1 The parts this crate writes use the transitional namespaces; a strict file's kept parts stay as they were. A binary workbook is not written. -A binary workbook gives the same `Workbook`, strings, rows and merged cells, with saved values -only: its formulas are token streams that are not decoded, and its styles are not read (each -cell keeps its format index). +A binary workbook gives the same `Workbook`, strings, rows and merged cells. Its styles are not +read (each cell keeps its format index). Its cell formulas are decoded from their tokens into the +text an `.xlsx` file holds (`_xlfn.` and `_xlpm.` names as written, no implicit `@`), with +`FormulaKind::Shared` or `FormulaKind::Array` and their ranges, and `BrtCellMeta` as the cell +metadata index. Every cell of a shared formula gets its own text, decoded at the cell, since the +file keeps the tokens once. A formula whose decoding needs something the reader does not read +keeps only its saved result, with a warning saying why: user defined names, table references, +other workbooks, Python code stored outside the cell, data tables. The XLSB function table +(`xlsb/ftab.rs`) and the expression printer are this crate's own, apart from `craft-xls`'s. ## Hostile input @@ -94,7 +100,9 @@ cell keeps its format index). string and what is left of a part, not a sheet's length. - `Limits.binary_records`, `binary_cells`, `binary_text`: a binary workbook, over all its parts (40 million records, 4 million cells and as many shared strings, 256 MiB of text, as - `craft-xls` allows). Fixed caps: 1024 sheets, 100,000 merged ranges per sheet, 64 nested + `craft-xls` allows). `binary_formula_text`: decoded formula text over the workbook (128 MiB); + past it, formulas keep their saved results. A formula's tokens are bounded at 16 KB, its + nesting at 256 levels and an array constant at 65,536 values. Fixed caps: 1024 sheets, 100,000 merged ranges per sheet, 64 nested extension blocks. - Cells and rows outside the 1,048,576 × 16,384 grid are dropped and counted. From 79b4fd014497d72305aa2ff7e2802127d42e37fa Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Fri, 13 Nov 2026 00:04:32 -0500 Subject: [PATCH 06/14] craft-xlsx: review fixes: CONVERT, shared formula tokens borrowed, cell metadata with its formula From the first review round: - 468 (0x1D4) is CONVERT, with 3 arguments. [MS-XLSB]'s Ftab table has no row for it, though its grammar lists convert-params among the fixed-argument functions; Excel writes =CONVERT(1,"m","ft") in .xlsb as a PtgFunc calling 0x1D4 (observed in Excel). It was the only unassigned number from 0x17C to 0x1E4. The module doc says it isn't the specification's row. - A BrtShrFmla's tokens and extra data are borrowed from the worksheet part instead of copied: a record may claim up to 256 MiB, and each shared formula is kept for the rest of the sheet, so the copies could cost as much again as the part. BrtArrFmla's are used at once and borrowed too. - A formula record's cell metadata goes with its formula: when the formula is kept as its value (tokens the reader can't decode, an unreadable BrtArrFmla), the cell loses its `cm`, which would otherwise mark a dynamic-array formula that isn't there. A value cell keeps its cell metadata, as the file has it. The CONVERT and cell-metadata tests fail without this commit. The Excel-saved workbook still reads with all 33 formulas as in its .xlsx. --- crates/xlsx/src/xlsb.rs | 46 +++++++++++++++++++++++------------- crates/xlsx/src/xlsb/ftab.rs | 9 ++++++- crates/xlsx/tests/xlsb.rs | 33 ++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 17 deletions(-) diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index d0bf980c9..d46c2ea20 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -542,11 +542,14 @@ fn kept(warnings: &mut Warnings, why: &str) { } /// A `BrtShrFmla`: the cells it may cover, its tokens, its first cell's text length (what a copy -/// is expected to cost) and the group's index (`si`). -struct SharedFormula { +/// is expected to cost) and the group's index (`si`). The tokens are borrowed from the worksheet +/// part, which outlives the sheet's read: a record can be up to 256 MiB, and copying every +/// shared formula's would cost as much again as the part. +#[derive(Clone, Copy)] +struct SharedFormula<'a> { range: Range, - rgce: Vec, - rgcb: Vec, + rgce: &'a [u8], + rgcb: &'a [u8], len: usize, si: u32, } @@ -576,12 +579,12 @@ fn part_of(rgce: &[u8], rgcb: &[u8], at: CellRef) -> Option>>, +struct RangeFormulas<'a> { + shared: HashMap>>, arrays: HashMap>, } -impl RangeFormulas { +impl<'a> RangeFormulas<'a> { /// The formula of another cell `at` of the shared or array formula starting at `first`. A /// shared formula's copy is decoded from the shared tokens at its own cell, as [MS-XLSB] /// defines it (relative offsets wrap around the grid), and checked like any formula: its text @@ -598,7 +601,7 @@ impl RangeFormulas { warnings: &mut Warnings, ) -> Option { if let Some(shared) = self.shared.get(&first) { - let sh = shared.clone()?; + let sh = (*shared)?; if !sh.range.contains(at) { kept(warnings, "malformed shared or array formula references"); return None; @@ -607,7 +610,7 @@ impl RangeFormulas { kept(warnings, "more formula text than the limits allow"); return None; } - let Some(text) = decode_formula(&sh.rgce, &sh.rgcb, Mode::Shared(at), links, limits, budget, warnings) else { + let Some(text) = decode_formula(sh.rgce, sh.rgcb, Mode::Shared(at), links, limits, budget, warnings) else { // A copy that was decoded but kept as its value charged nothing: one past the // allowance (it may be longer than the first cell's estimate) or one that can't be // decoded. The group's later copies would fail the same way, so they keep their @@ -634,14 +637,14 @@ impl RangeFormulas { fn range_formula( &mut self, id: u16, - payload: &[u8], + payload: &'a [u8], first: CellRef, links: Option<&Links>, limits: &Limits, budget: &Budget, warnings: &mut Warnings, ) -> Option { - let read = |b: &mut Bytes<'_>| -> Result<(Range, Vec, Vec), Error> { + let read = |b: &mut Bytes<'a>| -> Result<(Range, &'a [u8], &'a [u8]), Error> { let (r0, r1, c0, c1) = (b.u32()?, b.u32()?, b.u32()?, b.u32()?); if id == 426 { b.u8()?; // Only fAlwaysCalc, the calculator's to know. @@ -650,9 +653,9 @@ impl RangeFormulas { if !(1..=16384).contains(&cce) { return Err(invalid("invalid formula token length")); } - let rgce = b.take(cce as usize)?.to_vec(); + let rgce = b.take(cce as usize)?; let cb = b.u32()?; - let rgcb = b.take(cb as usize)?.to_vec(); + let rgcb = b.take(cb as usize)?; if r0 > r1 || c0 > c1 || r1 >= MAX_ROWS || c1 >= MAX_COLS { return Err(invalid("invalid formula range")); } @@ -673,9 +676,9 @@ impl RangeFormulas { return None; }; if id == 427 { - let text = decode_formula(&rgce, &rgcb, Mode::Shared(first), links, limits, budget, warnings); + let text = decode_formula(rgce, rgcb, Mode::Shared(first), links, limits, budget, warnings); let si = u32::try_from(self.shared.len()).unwrap_or(u32::MAX); - let shared = text.as_ref().map(|t| Arc::new(SharedFormula { range, rgce, rgcb, len: t.len(), si })); + let shared = text.as_ref().map(|t| SharedFormula { range, rgce, rgcb, len: t.len(), si }); self.shared.insert(first, shared); return text.map(|text| CellFormula { kind: FormulaKind::Shared, @@ -685,7 +688,7 @@ impl RangeFormulas { ..CellFormula::default() }); } - let text = decode_formula(&rgce, &rgcb, Mode::Cell, links, limits, budget, warnings); + let text = decode_formula(rgce, rgcb, Mode::Cell, links, limits, budget, warnings); self.arrays.insert(first, text.as_ref().map(|_| range)); text.map(|text| CellFormula { kind: FormulaKind::Array, text, range: Some(range), ..CellFormula::default() }) } @@ -761,6 +764,11 @@ pub(crate) fn worksheet( if matches!(id, 426 | 427) { let f = ranges.range_formula(id, payload, at, links, limits, budget, warnings); if let Some(cell) = row.as_mut().and_then(|r| r.cells.last_mut()).filter(|c| c.at == at) { + // A dynamic array's cell metadata describes its formula: with none kept, the + // cell is a value, and `cm` on it would mark a formula that isn't there. + if f.is_none() { + cell.cell_metadata = None; + } cell.formula = f; } continue; @@ -856,6 +864,12 @@ pub(crate) fn worksheet( if value != CellValue::Empty { budget.cell(limits)?; } + // A formula record's cell metadata describes its formula (a dynamic array), so a + // formula kept as its value drops it; the first cell of a shared or array formula + // keeps it until its formula record is read. Other cells keep theirs as the file + // has it. + let formula_kept = !(8..=11).contains(&id) || formula.is_some() || first == Some(at); + let cell_metadata = cell_metadata.filter(|_| formula_kept); current.cells.push(Cell { at, style: style & 0x00FF_FFFF, diff --git a/crates/xlsx/src/xlsb/ftab.rs b/crates/xlsx/src/xlsb/ftab.rs index 87f369519..efd6e4426 100644 --- a/crates/xlsx/src/xlsb/ftab.rs +++ b/crates/xlsx/src/xlsb/ftab.rs @@ -3,6 +3,10 @@ //! which craft-xls also carries. The entries from 380 (0x17C) on are the rest of the XLSB table: //! XLSB files call these functions by number, where `.xls` files call them by name. Kept apart from //! craft-xls's copy: the two formats follow separate specifications. +//! +//! One entry is not in the specification's table: 468 (0x1D4) is CONVERT. The table has no row +//! for it, but its grammar lists `convert-params` among the fixed-argument functions, and Excel +//! writes `=CONVERT(1,"m","ft")` in `.xlsb` as a `PtgFunc` calling 0x1D4. /// (iftab, name, fixed argument count or -1 when the function takes a variable number of /// arguments), sorted by number. @@ -467,6 +471,7 @@ const FTAB: &[(u16, &str, i8)] = &[ (465, "WEEKNUM", -1), (466, "AMORDEGRC", -1), (467, "AMORLINC", -1), + (468, "CONVERT", 3), (469, "ACCRINT", -1), (470, "ACCRINTM", -1), (471, "WORKDAY", -1), @@ -508,7 +513,9 @@ mod tests { assert_eq!(function(449), Some(("EDATE", 2))); assert_eq!(function(480), Some(("IFERROR", 2))); assert_eq!(function(482), Some(("SUMIFS", -1))); - assert_eq!(function(0x1D4), None, "unassigned"); + // Not in the specification's table, but what Excel writes for CONVERT. + assert_eq!(function(0x1D4), Some(("CONVERT", 3))); + assert_eq!(function(0x17B), Some(("RTD", -1))); assert_eq!(function(0x1E5), None); } } diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index 3f1c51b7f..518ca5214 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -885,3 +885,36 @@ fn a_shared_formula_stops_decoding_copies_once_one_is_kept_as_its_value() { assert_eq!((text("A1"), text("Z1"), text("B2")), (Some("B1"), None, None)); assert!(warned(&wb, "more formula text than the limits allow"), "{:?}", wb.warnings); } + +#[test] +fn convert_is_called_by_its_number_as_excel_writes_it() { + // Excel's tokens for `=CONVERT(1,"m","ft")` in .xlsb: PtgFunc 0x1D4, which the + // specification's Ftab table leaves out. + let rgce = [0x1E, 0x01, 0x00, 0x17, 0x01, 0x00, 0x6D, 0x00, 0x17, 0x02, 0x00, 0x66, 0x00, 0x74, 0x00, 0x41, 0xD4, 0x01]; + let wb = read(&file(&worksheet(&[row(0), formula_cell(0, &rgce, &[])].concat(), &[]))).unwrap(); + assert_eq!(wb.sheets[0].formula(CellRef::new(0, 0)), Some("CONVERT(1,\"m\",\"ft\")")); +} + +#[test] +fn a_dynamic_array_kept_as_its_value_has_no_cell_metadata() { + let meta = record(49, &1u32.to_le_bytes()); + let table_ref = [0x18, 0x19, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]; + let data = [ + row(0), + // A1: a dynamic-array formula the reader can't decode (a table reference). + meta.clone(), + formula_cell(0, &table_ref, &[]), + // B1:B2: a dynamic array whose BrtArrFmla holds an unknown token. + meta.clone(), + part(1, (0, 1)), + range_formula(426, (0, 1, 1, 1), &[0xFF]), + // C1: a value with cell metadata keeps it, as the file has it. + meta, + cell(5, 2, &1.0f64.to_le_bytes()), + ] + .concat(); + let wb = read(&file(&worksheet(&data, &[]))).unwrap(); + let sheet = &wb.sheets[0]; + assert_eq!((sheet.formula(at("A1")), sheet.formula(at("B1"))), (None, None)); + assert_eq!(sheet.cell_metadata.iter().collect::>(), [(&at("C1"), &1)]); +} From fd8a0dcf22b7e2d59393a5852727700d39fdebce Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Fri, 13 Nov 2026 19:12:02 -0500 Subject: [PATCH 07/14] craft-xlsx: review fixes: a long string chain stays within the depth bound; pending array metadata From the second review round (FredApps): - Strings joined with `&` are printed as one flat chain, which nests one level per string. A balanced tree of 4,096 empty strings fits in 16,383 token bytes and is 13 levels deep, but as a chain it was 4,096 levels, and printing and dropping it recurse: on a 256 KiB stack it overflowed. The chain is now made only up to MAX_DEPTH strings, the bound push keeps on the decoded tree (Excel's 8,192 characters hold at most about 33 full-length constants, so Python code split into constants is unaffected). A longer one prints as its tokens nest, with the parentheses that needs. GridCraft's tree walks a left-deep chain in a loop; the printer copied from craft-xls recurses, which is how the port lost the bound. - The first cell of a shared or array formula whose BrtShrFmla/BrtArrFmla never comes (another cell or the next row follows) loses its cell metadata too, as a formula kept as its value does. The new unit test overflowed its stack without this commit; the extended metadata test fails without it. The Excel-saved workbook still reads with all 33 formulas as in its .xlsx. --- crates/xlsx/src/xlsb.rs | 4 +++ crates/xlsx/src/xlsb/formula.rs | 61 +++++++++++++++++++++++++++++++-- crates/xlsx/tests/xlsb.rs | 16 +++++++-- 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index d46c2ea20..2fdde59be 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -773,6 +773,10 @@ pub(crate) fn worksheet( } continue; } + // No formula record: the cell stays a value, so its formula's cell metadata goes too. + if let Some(cell) = row.as_mut().and_then(|r| r.cells.last_mut()).filter(|c| c.at == at) { + cell.cell_metadata = None; + } kept(warnings, "a shared or array formula that is missing from the file"); } match id { diff --git a/crates/xlsx/src/xlsb/formula.rs b/crates/xlsx/src/xlsb/formula.rs index 53b02f8a1..327ffcc25 100644 --- a/crates/xlsx/src/xlsb/formula.rs +++ b/crates/xlsx/src/xlsb/formula.rs @@ -477,6 +477,15 @@ fn literal_text(e: &Expr) -> bool { } } +/// How many strings a [`literal_text`] chain joins, or `None` for anything else. +fn literal_texts(e: &Expr) -> Option { + match e { + Expr::Text(_) => Some(1), + Expr::Binary(BinOp::Concat, a, b) => literal_texts(a)?.checked_add(literal_texts(b)?), + _ => None, + } +} + /// How tightly an expression binds when printed, as the formula parser reads it: binary /// operators by [`BinOp::precedence`], prefix `+`/`-` at 60 and `%` at 70 (`-2^2` is `(-2)^2`); /// anything else is one operand. @@ -512,8 +521,13 @@ fn paren(e: Expr) -> Expr { fn grouped(e: Expr, in_args: bool) -> Expr { match e { // Strings joined with `&` are written as one flat chain, however their tokens nest: `&` - // is associative, and `PY("…"&"…")` is read as Python code only in that form. - e @ Expr::Binary(BinOp::Concat, ..) if literal_text(&e) => { + // is associative, and `PY("…"&"…")` is read as Python code only in that form. The chain + // nests one level per string, so it is made only up to `MAX_DEPTH` strings, the bound + // `push` keeps on the decoded tree (Excel's 8,192 characters hold at most about 33 + // full-length constants). A longer one prints as its tokens nest, with the parentheses + // that needs: a balanced tree of thousands of strings is only a few levels deep, and + // printing or dropping a chain of thousands would recurse past the stack. + e @ Expr::Binary(BinOp::Concat, ..) if literal_texts(&e).is_some_and(|n| n <= MAX_DEPTH) => { let mut texts = vec![]; flatten_texts(e, &mut texts); let mut texts = texts.into_iter(); @@ -575,3 +589,46 @@ fn array_constant(b: &mut Bytes<'_>) -> Result { } Ok(Expr::Array(out)) } + +#[cfg(test)] +mod tests { + use super::*; + + /// Postfix tokens for `n` empty strings joined by `&` as a balanced tree. + fn balanced_strings(n: usize, out: &mut Vec) { + if n == 1 { + out.extend([0x17, 0, 0]); + } else { + balanced_strings(n / 2, out); + balanced_strings(n - n / 2, out); + out.push(0x08); + } + } + + #[test] + fn a_long_chain_of_strings_stays_within_the_depth_bound() { + // 4,096 strings and 4,095 `&` fill 16,383 token bytes, 13 levels deep as written. Made + // into one flat chain they would be 4,096 levels deep, past what printing and dropping + // the tree may recurse; on a small stack that overflows. They print as written instead. + let mut rgce = vec![]; + balanced_strings(4096, &mut rgce); + assert!(rgce.len() <= MAX_RGCE); + let text = std::thread::Builder::new() + .stack_size(256 * 1024) + .spawn(move || { + let e = decode(&rgce, &[], &Links::default(), Mode::Cell).unwrap(); + let text = super::super::expr::print(&e); + drop(e); + text + }) + .unwrap() + .join() + .unwrap(); + assert_eq!(text.matches("\"\"").count(), 4096); + // A short chain, such as Python code split into constants, is still one flat chain. + let mut short = vec![]; + balanced_strings(4, &mut short); + let e = decode(&short, &[], &Links::default(), Mode::Cell).unwrap(); + assert_eq!(super::super::expr::print(&e), "\"\"&\"\"&\"\"&\"\""); + } +} diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index 518ca5214..ee9ac3afd 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -909,12 +909,24 @@ fn a_dynamic_array_kept_as_its_value_has_no_cell_metadata() { part(1, (0, 1)), range_formula(426, (0, 1, 1, 1), &[0xFF]), // C1: a value with cell metadata keeps it, as the file has it. - meta, + meta.clone(), cell(5, 2, &1.0f64.to_le_bytes()), + // D1: the first cell of a dynamic array with no BrtArrFmla after it, then a value, E1. + meta.clone(), + part(3, (0, 3)), + cell(5, 4, &1.0f64.to_le_bytes()), + // F1: the same as the last cell of its row. + meta, + part(5, (0, 5)), + row(1), + cell(5, 0, &1.0f64.to_le_bytes()), ] .concat(); let wb = read(&file(&worksheet(&data, &[]))).unwrap(); let sheet = &wb.sheets[0]; - assert_eq!((sheet.formula(at("A1")), sheet.formula(at("B1"))), (None, None)); + for a in ["A1", "B1", "D1", "F1"] { + assert_eq!(sheet.formula(at(a)), None, "{a}"); + } assert_eq!(sheet.cell_metadata.iter().collect::>(), [(&at("C1"), &1)]); + assert!(warned(&wb, "missing from the file"), "{:?}", wb.warnings); } From 3e957e0fd1a9821221154bb151b4debab5f30955 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Fri, 13 Nov 2026 23:35:18 -0500 Subject: [PATCH 08/14] craft-xlsx: read the styles part of a binary workbook Package::styles() reads styles.bin into the same Styles the XML part gives: number formats, fonts, fills, borders, cell and cell style formats, cell styles, differential formats and indexed colours, spelled as Excel writes them in styles.xml. A damaged part is a warning and no styles; each table holds at most 65,536 entries. Closes #1253. --- crates/xlsx/README.md | 7 +- crates/xlsx/src/document.rs | 4 +- crates/xlsx/src/package.rs | 20 +- crates/xlsx/src/xlsb.rs | 6 +- crates/xlsx/src/xlsb/styles.rs | 471 +++++++++++++++++++++++++++++++++ crates/xlsx/tests/xlsb.rs | 266 ++++++++++++++++++- 6 files changed, 764 insertions(+), 10 deletions(-) create mode 100644 crates/xlsx/src/xlsb/styles.rs diff --git a/crates/xlsx/README.md b/crates/xlsx/README.md index 1dc30574d..b2b581112 100644 --- a/crates/xlsx/README.md +++ b/crates/xlsx/README.md @@ -83,8 +83,11 @@ for new pictures (`vm = i + 1`) and the `XLDAPR` dynamic-array metadata (`cm = 1 The parts this crate writes use the transitional namespaces; a strict file's kept parts stay as they were. A binary workbook is not written. -A binary workbook gives the same `Workbook`, strings, rows and merged cells. Its styles are not -read (each cell keeps its format index). Its cell formulas are decoded from their tokens into the +A binary workbook gives the same `Workbook`, strings, rows and merged cells, and the same +`Styles`: number formats, fonts, fills, borders, cell and cell style formats, cell styles, +differential formats and the indexed colours, as Excel writes them in `styles.xml` (default fill +colours and empty settings left out, a cell style format's `apply*="0"`). Custom table styles and +recent colours are not read; a damaged styles part is a warning and no styles. Its cell formulas are decoded from their tokens into the text an `.xlsx` file holds (`_xlfn.` and `_xlpm.` names as written, no implicit `@`), with `FormulaKind::Shared` or `FormulaKind::Array` and their ranges, and `BrtCellMeta` as the cell metadata index. Every cell of a shared formula gets its own text, decoded at the cell, since the diff --git a/crates/xlsx/src/document.rs b/crates/xlsx/src/document.rs index 26e6a84c6..1be23029e 100644 --- a/crates/xlsx/src/document.rs +++ b/crates/xlsx/src/document.rs @@ -209,8 +209,8 @@ pub struct Document { } impl Document { - /// Reads every part. A binary workbook is refused: its formulas and styles are not read, so - /// it cannot be written back as SpreadsheetML without loss. + /// Reads every part. A binary workbook is refused: only some of its parts are read, so it + /// cannot be written back as SpreadsheetML without loss. pub fn read(bytes: &[u8], limits: &Limits) -> Result { let mut p = Package::open(bytes, limits)?; if p.is_binary() { diff --git a/crates/xlsx/src/package.rs b/crates/xlsx/src/package.rs index 8f2c5efc6..f16b20d57 100644 --- a/crates/xlsx/src/package.rs +++ b/crates/xlsx/src/package.rs @@ -214,16 +214,28 @@ impl<'a> Package<'a> { Ok(items) } - /// The styles part, `None` when the workbook has none (or is binary: its styles are not read). + /// The styles part, `None` when the workbook has none. A damaged one is a warning and `None`; + /// only a limit is an error. pub fn styles(&mut self) -> Result, Error> { - if self.binary.is_some() { - return Ok(None); - } let rels = self.workbook_rels()?; let Some(rel) = rels.first("styles").filter(|r| !r.external) else { self.warn("no styles part"); return Ok(None); }; + if let Some(types) = &self.binary { + let read = xlsb::check_type(types, &rel.target, xlsb::STYLES).and_then(|()| { + let bytes = self.opc.read(&rel.target)?.ok_or_else(|| xlsb::invalid(format!("missing part {}", rel.target)))?; + xlsb::styles(&bytes, &self.limits, &self.budget, &mut self.warnings) + }); + return match read { + Ok(styles) => Ok(Some(styles)), + Err(Error::TooLarge(m)) => Err(Error::TooLarge(m)), + Err(e) => { + self.warn(format!("skipped {}: {e}", rel.target)); + Ok(None) + } + }; + } Ok(self.optional_xml(&rel.target.clone())?.map(Styles::read)) } diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index 2fdde59be..50da6185c 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -1,11 +1,13 @@ //! Binary workbooks ([MS-XLSB]): the workbook part, shared strings and worksheet cells, from //! their records. Cell formulas are decoded from their tokens ([`formula`]) into the text an //! `.xlsx` file holds; one that can't be keeps only its saved result, with a warning saying why. -//! Styles are not read. A record that does not parse fails the read. +//! The styles part is read into the same [`crate::styles::Styles`] ([`styles`]). A record that +//! does not parse fails the read. mod expr; mod formula; mod ftab; +mod styles; use std::collections::HashMap; use std::sync::Arc; @@ -19,11 +21,13 @@ use crate::workbook::{SheetEntry, SheetKind, Workbook}; use crate::{Error, Limits, Warnings}; pub(crate) use formula::Links; use formula::{Book, Mode, Name, Xti}; +pub(crate) use styles::styles; pub(crate) const MAIN: &str = "application/vnd.ms-excel.main"; const BINARY_MAIN: &str = "application/vnd.ms-excel.sheet.binary.macroEnabled.main"; pub(crate) const WORKSHEET: &str = "application/vnd.ms-excel.worksheet"; pub(crate) const STRINGS: &str = "application/vnd.ms-excel.sharedStrings"; +pub(crate) const STYLES: &str = "application/vnd.ms-excel.styles"; /// Most sheets in the workbook. const MAX_SHEETS: usize = 1024; /// Most merged ranges in one worksheet. diff --git a/crates/xlsx/src/xlsb/styles.rs b/crates/xlsx/src/xlsb/styles.rs new file mode 100644 index 000000000..094620a07 --- /dev/null +++ b/crates/xlsx/src/xlsb/styles.rs @@ -0,0 +1,471 @@ +//! The styles part of a binary workbook (`styles.bin`, [MS-XLSB] 2.1.7.50), read into the same +//! [`Styles`] the XML part gives: what Excel writes in `styles.xml` for the same workbook. Table +//! styles, recent colours and future records are not read. + +use craft_sheet::{BorderStyle, HAlign, Pattern, Script, Underline, VAlign}; +use craft_xml::El; + +use super::{Budget, Bytes, Records, invalid}; +use crate::styles::{ + Alignment, Border, BorderEdge, CellStyle, Color, ColorSource, Dxf, Fill, Font, FontScheme, GradientFill, NumFmt, PatternFill, Protection, Styles, + Xf, +}; +use crate::{Error, Limits, Warnings}; + +/// Characters of a font name or number format kept, as the XML side keeps of a font name. +const MAX_NAME_CHARS: usize = 255; +/// Longest string read from the part (Excel's own limits are 31 and 255 characters). +const MAX_STRING_UNITS: u32 = 32_767; +/// Most stops in one gradient fill (Excel writes a handful). +const MAX_STOPS: u32 = 1024; +/// Most entries in one table of the part (Excel allows about 64,000 cell formats). Each entry is +/// an allocation, and the record allowance alone would let a small part hold millions. +const MAX_ENTRIES: usize = 65_536; + +/// `BrtColor` and `XFPropColor`: the same eight bytes. `None` for "not set" (`XFPropColor` type 4). +fn color(b: &mut Bytes<'_>) -> Result, Error> { + let kind = b.u8()? >> 1; + let index = b.u8()?; + let tint = i16::from_le_bytes(<[u8; 2]>::try_from(b.take(2)?).map_err(|_| invalid("truncated colour"))?); + let [r, g, bl, a] = <[u8; 4]>::try_from(b.take(4)?).map_err(|_| invalid("truncated colour"))?; + let source = match kind { + 0 => ColorSource::Auto, + 1 => ColorSource::Indexed(u32::from(index)), + 2 => ColorSource::Rgb(u32::from_be_bytes([a, r, g, bl])), + 3 => ColorSource::Theme(u32::from(index)), + _ => return Ok(None), + }; + Ok(Some(Color { source, tint: (f64::from(tint) / 32767.0).clamp(-1.0, 1.0) })) +} + +fn pattern(fls: u32) -> Result { + const PATTERNS: [Pattern; 19] = [ + Pattern::None, + Pattern::Solid, + Pattern::Gray50, + Pattern::Gray75, + Pattern::Gray25, + Pattern::DarkHorizontal, + Pattern::DarkVertical, + Pattern::DarkDown, + Pattern::DarkUp, + Pattern::DarkGrid, + Pattern::DarkTrellis, + Pattern::LightHorizontal, + Pattern::LightVertical, + Pattern::LightDown, + Pattern::LightUp, + Pattern::LightGrid, + Pattern::LightTrellis, + Pattern::Gray125, + Pattern::Gray0625, + ]; + PATTERNS.get(fls as usize).copied().ok_or_else(|| invalid("unknown fill pattern")) +} + +fn border_style(dg: u16) -> Result { + const STYLES: [BorderStyle; 14] = [ + BorderStyle::None, + BorderStyle::Thin, + BorderStyle::Medium, + BorderStyle::Dashed, + BorderStyle::Dotted, + BorderStyle::Thick, + BorderStyle::Double, + BorderStyle::Hair, + BorderStyle::MediumDashed, + BorderStyle::DashDot, + BorderStyle::MediumDashDot, + BorderStyle::DashDotDot, + BorderStyle::MediumDashDotDot, + BorderStyle::SlantDashDot, + ]; + STYLES.get(usize::from(dg)).copied().ok_or_else(|| invalid("unknown border style")) +} + +fn halign(alc: u32) -> Option { + [HAlign::General, HAlign::Left, HAlign::Center, HAlign::Right, HAlign::Fill, HAlign::Justify, HAlign::CenterAcross, HAlign::Distributed] + .get(alc as usize) + .copied() +} + +fn valign(alcv: u32) -> Option { + [VAlign::Top, VAlign::Center, VAlign::Bottom, VAlign::Justify, VAlign::Distributed].get(alcv as usize).copied() +} + +fn underline(uls: u32) -> Underline { + match uls { + 0 => Underline::None, + 2 => Underline::Double, + 0x21 => Underline::SingleAccounting, + 0x22 => Underline::DoubleAccounting, + _ => Underline::Single, + } +} + +fn script(sss: u32) -> Script { + match sss { + 1 => Script::Superscript, + 2 => Script::Subscript, + _ => Script::Baseline, + } +} + +fn scheme(s: u32) -> Option { + match s { + 0 => Some(FontScheme::None), + 1 => Some(FontScheme::Major), + 2 => Some(FontScheme::Minor), + _ => None, + } +} + +fn name(text: String) -> String { + text.chars().take(MAX_NAME_CHARS).collect() +} + +/// `BrtFont`: only what Excel's `.xlsx` states for the same font (no ``, no scheme +/// `none`, no family or character set 0). +fn font(b: &mut Bytes<'_>) -> Result { + let height = b.u16()?; + let flags = b.u16()?; + let weight = b.u16()?; + let sss = b.u16()?; + let uls = b.u8()?; + let family = b.u8()?; + let charset = b.u8()?; + b.u8()?; + let color = color(b)?; + let font_scheme = b.u8()?; + let font_name = name(b.text(MAX_STRING_UNITS)?); + let on = |bit: u16| (flags & bit != 0).then_some(true); + Ok(Font { + name: Some(font_name), + size: Some(f64::from(height) / 20.0), + bold: (weight >= 0x2BC).then_some(true), + italic: on(0x02), + strike: on(0x08), + outline: on(0x10), + shadow: on(0x20), + condense: on(0x40), + extend: on(0x80), + underline: (uls != 0).then(|| underline(u32::from(uls))), + vert_align: (sss != 0).then(|| script(u32::from(sss))), + color, + family: (family != 0).then_some(u32::from(family)), + charset: (charset != 0).then_some(u32::from(charset)), + scheme: scheme(u32::from(font_scheme)).filter(|s| *s != FontScheme::None), + }) +} + +/// A gradient's angle and inner rectangle, as `BrtFill` and `XFPropGradient` hold them. +fn gradient(b: &mut Bytes<'_>, path: bool) -> Result { + Ok(GradientFill { path, degree: b.f64()?, left: b.f64()?, right: b.f64()?, top: b.f64()?, bottom: b.f64()?, stops: vec![] }) +} + +/// `BrtFill`. Excel's `.xlsx` leaves out the default colours: system foreground (indexed 64) as +/// the pattern colour and system background (indexed 65) as the background. +fn fill(b: &mut Bytes<'_>) -> Result { + let fls = b.u32()?; + let fg = color(b)?; + let bg = color(b)?; + let kind = b.u32()?; + let mut g = gradient(b, kind == 1)?; + let count = b.u32()?; + if fls == 0x28 { + if count > MAX_STOPS { + return Err(invalid("too many gradient stops")); + } + for _ in 0..count { + let c = color(b)?.unwrap_or_default(); + g.stops.push((b.f64()?, c)); + } + return Ok(Fill::Gradient(g)); + } + let system = |c: Color, i: u32| c.source == ColorSource::Indexed(i) && c.tint == 0.0; + Ok(Fill::Pattern(PatternFill { pattern: Some(pattern(fls)?), fg: fg.filter(|c| !system(*c, 64)), bg: bg.filter(|c| !system(*c, 65)) })) +} + +/// `BrtBorder`: the five edges a cell border has, each stated (`` when it has no line). +fn border(b: &mut Bytes<'_>) -> Result { + let flags = b.u8()?; + let mut edge = || -> Result { + let style = border_style(u16::from(b.u8()?))?; + b.u8()?; + let c = color(b)?; + Ok(BorderEdge { style, color: c.filter(|_| style != BorderStyle::None) }) + }; + let (top, bottom, left, right, diagonal) = (edge()?, edge()?, edge()?, edge()?, edge()?); + Ok(Border { + left: Some(left), + right: Some(right), + top: Some(top), + bottom: Some(bottom), + diagonal: Some(diagonal), + vertical: None, + horizontal: None, + diagonal_up: flags & 2 != 0, + diagonal_down: flags & 1 != 0, + outline: true, + }) +} + +/// `BrtXF`. A cell style format (`style`) marks what it leaves out (`apply*="0"`); a cell format +/// marks what it sets apart from its style (`apply*="1"`). +fn xf(b: &mut Bytes<'_>, style: bool) -> Result { + const APPLY: [(u8, &str); 6] = [ + (0x01, "applyNumberFormat"), + (0x02, "applyFont"), + (0x10, "applyFill"), + (0x08, "applyBorder"), + (0x04, "applyAlignment"), + (0x20, "applyProtection"), + ]; + let parent = b.u16()?; + let num_fmt_id = u32::from(b.u16()?); + let font_id = u32::from(b.u16()?); + let fill_id = u32::from(b.u16()?); + let border_id = u32::from(b.u16()?); + let rotation = u32::from(b.u8()?); + let indent = u32::from(b.u8()?); + let flags = u32::from(b.u16()?); + let apply = b.u8()?; + b.u8()?; + let alignment = Alignment { + horizontal: halign(flags & 7).filter(|h| *h != HAlign::General), + vertical: valign((flags >> 3) & 7).filter(|v| *v != VAlign::Bottom), + text_rotation: (rotation != 0).then_some(rotation), + wrap_text: flags & 0x40 != 0, + justify_last_line: flags & 0x80 != 0, + shrink_to_fit: flags & 0x100 != 0, + indent: (indent != 0).then_some(indent), + reading_order: Some((flags >> 10) & 3).filter(|r| *r != 0), + }; + let protection = Protection { locked: (flags & 0x1000 == 0).then_some(false), hidden: (flags & 0x2000 != 0).then_some(true) }; + let mut el = El::new("xf"); + for (bit, attr) in APPLY { + if apply & bit != 0 { + el = el.with_attr(attr, if style { "0" } else { "1" }); + } + } + Ok(Xf { + num_fmt_id, + font_id, + fill_id, + border_id, + xf_id: (!style).then_some(u32::from(parent)), + alignment: (alignment != Alignment::default()).then_some(alignment), + protection: (protection != Protection::default()).then_some(protection), + quote_prefix: flags & 0x8000 != 0, + el, + }) +} + +/// `BrtStyle`. +fn cell_style(b: &mut Bytes<'_>) -> Result { + let xf_id = b.u32()?; + let flags = b.u16()?; + let builtin = b.u8()?; + let level = b.u8()?; + let style_name = b.string(MAX_STRING_UNITS, true)?.map(name).unwrap_or_default(); + let is_builtin = flags & 1 != 0; + Ok(CellStyle { + name: style_name, + xf_id, + builtin_id: is_builtin.then_some(u32::from(builtin)), + level: (is_builtin && matches!(builtin, 1 | 2)).then_some(u32::from(level)), + hidden: flags & 2 != 0, + custom_builtin: flags & 4 != 0, + }) +} + +/// An unsigned integer of the size a property has (1, 2 or 4 bytes). +fn uint(data: &[u8]) -> Result { + match *data { + [a] => Ok(u32::from(a)), + [a, b] => Ok(u32::from(u16::from_le_bytes([a, b]))), + [a, b, c, d] => Ok(u32::from_le_bytes([a, b, c, d])), + _ => Err(invalid("unexpected formatting property size")), + } +} + +/// A string with a 2-byte length (`LPWideString`, and the number format of `XFProp` 0x26). +fn short_string(b: &mut Bytes<'_>) -> Result { + let count = usize::from(b.u16()?); + let mut text = String::new(); + for ch in char::decode_utf16(b.take(count * 2)?.as_chunks::<2>().0.iter().copied().map(u16::from_le_bytes)) { + text.push(ch.map_err(|_| invalid("invalid UTF-16 string"))?); + } + Ok(name(text)) +} + +fn dxf_font(d: &mut Dxf) -> &mut Font { + d.font.get_or_insert_with(Font::default) +} + +/// `BrtDXF`: its `XFProps`, each stating one property. +fn dxf(b: &mut Bytes<'_>) -> Result { + b.u16()?; + b.u16()?; + let count = b.u16()?; + let mut d = Dxf::default(); + let mut gradient_fill: Option = None; + let mut pattern_fill: Option = None; + for _ in 0..count { + let kind = b.u16()?; + let size = usize::from(b.u16()?).checked_sub(4).ok_or_else(|| invalid("formatting property too short"))?; + let data = b.take(size)?; + let mut p = Bytes::new(data); + match kind { + 0x00 => pattern_fill.get_or_insert_with(PatternFill::default).pattern = Some(pattern(uint(data)?)?), + 0x01 => pattern_fill.get_or_insert_with(PatternFill::default).fg = color(&mut p)?, + 0x02 => pattern_fill.get_or_insert_with(PatternFill::default).bg = color(&mut p)?, + 0x03 => { + let path = p.u32()? == 1; + gradient_fill = Some(gradient(&mut p, path)?); + } + 0x04 => { + let g = gradient_fill.as_mut().ok_or_else(|| invalid("gradient stop without a gradient"))?; + if g.stops.len() >= MAX_STOPS as usize { + return Err(invalid("too many gradient stops")); + } + p.u16()?; + let position = p.f64()?; + g.stops.push((position, color(&mut p)?.unwrap_or_default())); + } + 0x05 => dxf_font(&mut d).color = color(&mut p)?, + 0x06..=0x0C => { + let c = color(&mut p)?; + let edge = Some(BorderEdge { style: border_style(p.u16()?)?, color: c }); + let border = d.border.get_or_insert_with(|| Border { outline: true, ..Border::default() }); + match kind { + 0x06 => border.top = edge, + 0x07 => border.bottom = edge, + 0x08 => border.left = edge, + 0x09 => border.right = edge, + 0x0A => border.diagonal = edge, + 0x0B => border.vertical = edge, + _ => border.horizontal = edge, + } + } + 0x0D => d.border.get_or_insert_with(|| Border { outline: true, ..Border::default() }).diagonal_up = uint(data)? != 0, + 0x0E => d.border.get_or_insert_with(|| Border { outline: true, ..Border::default() }).diagonal_down = uint(data)? != 0, + 0x0F..=0x16 => { + let v = uint(data)?; + let a = d.alignment.get_or_insert_with(Alignment::default); + match kind { + 0x0F => a.horizontal = halign(v), + 0x10 => a.vertical = valign(v), + 0x11 => a.text_rotation = Some(v), + 0x12 => a.indent = Some(v), + 0x13 => a.reading_order = Some(v), + 0x14 => a.wrap_text = v != 0, + 0x15 => a.justify_last_line = v != 0, + _ => a.shrink_to_fit = v != 0, + } + } + 0x18 => dxf_font(&mut d).name = Some(short_string(&mut p)?), + 0x19 => dxf_font(&mut d).bold = Some(uint(data)? >= 0x2BC), + 0x1A => dxf_font(&mut d).underline = Some(underline(uint(data)?)), + 0x1B => dxf_font(&mut d).vert_align = Some(script(uint(data)?)), + 0x1C => dxf_font(&mut d).italic = Some(uint(data)? != 0), + 0x1D => dxf_font(&mut d).strike = Some(uint(data)? != 0), + 0x1E => dxf_font(&mut d).outline = Some(uint(data)? != 0), + 0x1F => dxf_font(&mut d).shadow = Some(uint(data)? != 0), + 0x20 => dxf_font(&mut d).condense = Some(uint(data)? != 0), + 0x21 => dxf_font(&mut d).extend = Some(uint(data)? != 0), + 0x22 => dxf_font(&mut d).charset = Some(uint(data)?), + 0x23 => dxf_font(&mut d).family = Some(uint(data)?), + 0x24 => dxf_font(&mut d).size = Some(f64::from(uint(data)?) / 20.0), + 0x25 => dxf_font(&mut d).scheme = scheme(uint(data)?), + 0x26 => d.num_fmt.get_or_insert_with(NumFmt::default).code = Some(short_string(&mut p)?), + 0x29 => d.num_fmt.get_or_insert_with(NumFmt::default).id = Some(uint(data)?), + 0x2B => d.protection.get_or_insert_with(Protection::default).locked = Some(uint(data)? != 0), + 0x2C => d.protection.get_or_insert_with(Protection::default).hidden = Some(uint(data)? != 0), + // Merged cells (0x17) and relative indents (0x2A) have no place in ``. + _ => {} + } + } + d.fill = gradient_fill.map(Fill::Gradient).or(pattern_fill.map(Fill::Pattern)); + Ok(d) +} + +/// The styles part: `BrtBeginStyleSheet` to `BrtEndStyleSheet`. +pub(crate) fn styles(bytes: &[u8], limits: &Limits, budget: &Budget, warnings: &mut Warnings) -> Result { + let mut records = Records::new(bytes, limits, budget); + records.first(278, warnings)?; + let mut s = Styles::default(); + // Inside BrtBeginCellStyleXFs (true) or BrtBeginCellXFs (false). + let mut xfs: Option = None; + let mut palette = false; + while let Some((id, payload)) = records.next(warnings)? { + let mut b = Bytes::new(payload); + let entries = match id { + 44 => s.num_fmts.len(), + 43 => s.fonts.len(), + 45 => s.fills.len(), + 46 => s.borders.len(), + 47 => s.cell_style_xfs.len().max(s.cell_xfs.len()), + 48 => s.cell_styles.len(), + 507 => s.dxfs.len(), + 475 => s.indexed_colors.len(), + _ => 0, + }; + if entries >= MAX_ENTRIES { + return Err(invalid(format!("more than {MAX_ENTRIES} entries in a styles table"))); + } + match id { + 279 => { + records.end(payload, warnings)?; + return Ok(s); + } + 44 => { + let id = u32::from(b.u16()?); + let code = name(b.text(MAX_STRING_UNITS)?); + budget.text(code.len(), limits)?; + s.num_fmts.push((id, code)); + } + 43 => { + let f = font(&mut b)?; + budget.text(f.name.as_ref().map_or(0, String::len), limits)?; + s.fonts.push(f); + } + 45 => s.fills.push(fill(&mut b)?), + 46 => s.borders.push(border(&mut b)?), + 626 => xfs = Some(true), + 617 => xfs = Some(false), + 627 | 618 => xfs = None, + 47 => match xfs { + Some(true) => s.cell_style_xfs.push(xf(&mut b, true)?), + Some(false) => s.cell_xfs.push(xf(&mut b, false)?), + None => return Err(invalid("cell format outside its list")), + }, + 48 => { + let c = cell_style(&mut b)?; + budget.text(c.name.len(), limits)?; + s.cell_styles.push(c); + } + 507 => s.dxfs.push(dxf(&mut b)?), + // BrtBeginIndexedColors, BrtEndIndexedColors. + 565 => palette = true, + 566 => palette = false, + 475 if palette => { + let [r, g, bl, _] = <[u8; 4]>::try_from(b.take(4)?).map_err(|_| invalid("truncated colour"))?; + s.indexed_colors.push(u32::from_be_bytes([0, r, g, bl])); + } + // BrtBeginTableStyles: the count of custom table styles, which are not read. + 508 => { + if b.u32()? > 0 { + warnings.warn("custom table styles in a binary workbook were not read"); + } + continue; + } + _ => continue, + } + // The records read above are read whole; the rest are skipped. + if matches!(id, 44 | 43 | 45 | 46 | 47 | 48 | 507 | 475) { + b.finish()?; + } + } + Err(invalid("missing styles end record")) +} diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index ee9ac3afd..bc1a7f6bd 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -5,7 +5,10 @@ mod common; use common::{read, read_with}; use craft_xlsx::testing::zip; -use craft_xlsx::{CellError, CellRef, Error, FormulaKind, Limits, Range, SheetKind, Value, Visibility, is_xlsb}; +use craft_xlsx::{ + CellError, CellRef, Color, ColorSource, Error, Fill, FormulaKind, Limits, Package, Pattern, PatternFill, Protection, Range, SheetKind, Styles, + Value, Visibility, is_xlsb, +}; const ROOT: &str = r#""#; const RELS: &str = r#""#; @@ -930,3 +933,264 @@ fn a_dynamic_array_kept_as_its_value_has_no_cell_metadata() { assert_eq!(sheet.cell_metadata.iter().collect::>(), [(&at("C1"), &1)]); assert!(warned(&wb, "missing from the file"), "{:?}", wb.warnings); } + +// Styles ([MS-XLSB] 2.1.7.50): a styles part assembled from records, and the `styles.xml` Excel +// writes for the same formats when it saves the workbook as `.xlsx`. + +const STYLES_RELS: &str = r#""#; +const STYLES_TYPES: &str = r#""#; + +fn styled_file(styles: &[u8], types: &str) -> Vec { + let sheet = worksheet(&[row(0), cell(5, 0, &1.0f64.to_le_bytes())].concat(), &[]); + zip(&[ + ("[Content_Types].xml", types.as_bytes()), + ("_rels/.rels", ROOT.as_bytes()), + ("records/book.bin", &book(false)), + ("records/_rels/book.bin.rels", STYLES_RELS.as_bytes()), + ("data/one.bin", &sheet), + ("data/two.bin", &worksheet(&[], &[])), + ("look/styles.bin", styles), + ]) +} + +/// The styles part of a package, and the warnings reading it gave. +fn styles_of(bytes: &[u8]) -> (Option, Vec) { + let mut p = Package::open(bytes, &Limits::default()).unwrap(); + let styles = p.styles().unwrap(); + (styles, p.warnings().to_vec()) +} + +/// `BrtColor`: auto 0, indexed 1, RGB 2, theme 3. +fn color(kind: u8, index: u8, tint: i16, rgba: [u8; 4]) -> Vec { + [&[kind << 1 | 1, index][..], &tint.to_le_bytes(), &rgba].concat() +} +fn indexed(i: u8) -> Vec { + color(1, i, 0, [0, 0, 0, 255]) +} +fn theme(i: u8, tint: i16) -> Vec { + color(3, i, tint, [0, 0, 0, 255]) +} +fn rgb(r: u8, g: u8, b: u8) -> Vec { + color(2, 255, 0, [r, g, b, 255]) +} +#[allow(clippy::too_many_arguments)] +fn font(twips: u16, flags: u16, weight: u16, script: u16, underline: u8, family: u8, charset: u8, c: &[u8], scheme: u8, name: &str) -> Vec { + let head = + [&twips.to_le_bytes()[..], &flags.to_le_bytes(), &weight.to_le_bytes(), &script.to_le_bytes(), &[underline, family, charset, 0]].concat(); + record(43, &[&head[..], c, &[scheme], &wide(name)].concat()) +} +fn pattern_fill(fls: u32, fg: &[u8], bg: &[u8]) -> Vec { + record(45, &[&fls.to_le_bytes()[..], fg, bg, &[0; 48]].concat()) +} +fn edge(dg: u8, c: &[u8]) -> Vec { + [&[dg, 0][..], c].concat() +} +#[allow(clippy::too_many_arguments)] +fn brt_xf(parent: u16, fmt: u16, font: u16, fill: u16, border: u16, rotation: u8, indent: u8, flags: u16, apply: u8) -> Vec { + let ids = [parent, fmt, font, fill, border].map(u16::to_le_bytes).concat(); + record(47, &[&ids[..], &[rotation, indent], &flags.to_le_bytes(), &[apply, 0]].concat()) +} +fn style(xf: u32, flags: u16, builtin: u8, level: u8, name: &str) -> Vec { + record(48, &[&xf.to_le_bytes()[..], &flags.to_le_bytes(), &[builtin, level], &wide(name)].concat()) +} +fn prop(kind: u16, data: &[u8]) -> Vec { + [&kind.to_le_bytes()[..], &(data.len() as u16 + 4).to_le_bytes(), data].concat() +} +fn short(text: &str) -> Vec { + let units: Vec = text.encode_utf16().collect(); + [(units.len() as u16).to_le_bytes().to_vec(), units.iter().flat_map(|u| u.to_le_bytes()).collect()].concat() +} +fn brt_dxf(props: &[Vec]) -> Vec { + record(507, &[&[0, 0x80, 0, 0][..], &(props.len() as u16).to_le_bytes(), &props.concat()].concat()) +} +fn begin(id: u16, count: u32) -> Vec { + record(id, &count.to_le_bytes()) +} + +/// Every kind of record the styles part has, as Excel writes them. +fn styles_bin() -> Vec { + let normal = font(220, 0, 400, 0, 0, 2, 0, &theme(1, 0), 2, "Aptos Narrow"); + // Bold, italic, struck out, superscript, double accounting underline; family and + // character set 0 and scheme "none" are not written in `.xlsx`. + let fancy = font(290, 0x0A, 700, 1, 0x22, 0, 2, &rgb(0, 128, 255), 0, "Wingdings"); + let heading = font(220, 0, 400, 0, 0, 2, 0, &theme(4, 13106), 1, "Aptos Display"); + let gradient = [ + &0x28u32.to_le_bytes()[..], + &indexed(64), + &indexed(65), + &1u32.to_le_bytes(), + &[0.0f64, 0.5, 0.5, 0.5, 0.5].map(f64::to_le_bytes).concat(), + &2u32.to_le_bytes(), + &theme(0, 0), + &0.0f64.to_le_bytes(), + &theme(4, 0), + &1.0f64.to_le_bytes(), + ] + .concat(); + let none = edge(0, &color(0, 0, 0, [0, 0, 0, 0])); + let parts: Vec> = vec![ + record(278, &[]), + begin(615, 1), + record(44, &[&164u16.to_le_bytes()[..], &wide(r"yyyy\-mm\-dd")].concat()), + record(616, &[]), + begin(611, 3), + normal, + fancy, + heading, + record(612, &[]), + begin(603, 5), + pattern_fill(0, &indexed(64), &indexed(65)), + pattern_fill(17, &indexed(64), &indexed(65)), + pattern_fill(1, &rgb(255, 255, 0), &indexed(64)), + pattern_fill(9, &theme(5, -8191), &indexed(65)), + record(45, &gradient), + record(604, &[]), + begin(613, 2), + record(46, &[&[0][..], &none, &none, &none, &none, &none].concat()), + record( + 46, + &[&[3][..], &edge(2, &rgb(200, 0, 0)), &none, &edge(7, &indexed(64)), &edge(13, &color(0, 0, 0, [0, 0, 0, 0])), &edge(1, &theme(4, 0))] + .concat(), + ), + record(614, &[]), + begin(626, 3), + brt_xf(0xFFFF, 0, 0, 0, 0, 0, 0, 0x1010, 0), + brt_xf(0xFFFF, 9, 0, 0, 0, 0, 0, 0x1010, 0x3E), + brt_xf(0xFFFF, 0, 2, 0, 0, 0, 0, 0x1010, 0), + record(627, &[]), + begin(617, 4), + brt_xf(0, 0, 0, 0, 0, 0, 0, 0x1010, 0), + brt_xf(0, 164, 0, 0, 0, 0, 0, 0x1010, 0x01), + // Left, top, wrapped, shrunk, right to left, unlocked, hidden, quote prefix. + brt_xf(0, 0, 0, 0, 0, 180, 2, 0x01 | 0x40 | 0x100 | 0x800 | 0x2000 | 0x8000, 0x24), + brt_xf(1, 0, 1, 4, 1, 0, 0, 0x1010, 0x1A), + record(618, &[]), + begin(619, 3), + style(0, 1, 0, 0, "Normal"), + // Excel wraps each style in alternate content, which is skipped. + record(37, &[1, 0, 2, 0x0e, 0, 0x80]), + record(3072, &[0; 16]), + record(38, &[]), + style(1, 1 | 4, 1, 1, "RowLevel_2"), + style(2, 2, 0, 0, "Probe Style"), + record(620, &[]), + begin(505, 2), + brt_dxf(&[ + prop(0x05, &rgb(0x9C, 0, 6)), + prop(0x19, &700u16.to_le_bytes()), + prop(0x1C, &[0]), + prop(0x1A, &0x21u16.to_le_bytes()), + prop(0x24, &240u32.to_le_bytes()), + prop(0x18, &short("Arial")), + prop(0x29, &170u16.to_le_bytes()), + prop(0x26, &short("0.0")), + prop(0x02, &rgb(0xFF, 0xC7, 0xCE)), + prop(0x0F, &[2]), + prop(0x14, &[1]), + prop(0x2B, &[0]), + prop(0x07, &[&color(0, 0, 0, [0, 0, 0, 0])[..], &1u16.to_le_bytes()].concat()), + ]), + brt_dxf(&[ + prop(0x03, &[&0u32.to_le_bytes()[..], &[90.0f64, 0.0, 0.0, 0.0, 0.0].map(f64::to_le_bytes).concat()].concat()), + prop(0x04, &[&[0, 0][..], &0.0f64.to_le_bytes(), &rgb(255, 255, 255)].concat()), + prop(0x04, &[&[0, 0][..], &1.0f64.to_le_bytes(), &theme(4, 0)].concat()), + ]), + record(506, &[]), + record(508, &[&0u32.to_le_bytes()[..], &wide("TableStyleMedium2"), &wide("PivotStyleLight16")].concat()), + record(509, &[]), + record(473, &[]), + record(565, &[]), + record(475, &[0x11, 0x22, 0x33, 0]), + record(475, &[0xFF, 0xFF, 0xFF, 0]), + record(566, &[]), + record(474, &[]), + record(279, &[]), + ]; + parts.concat() +} + +const STYLES_XML: &str = r#""#; + +/// The same styles from a SpreadsheetML package. +fn xml_styles(xml: &str) -> Styles { + let types = r#""#; + let root = r#""#; + let rels = r#""#; + let bytes = zip(&[ + ("[Content_Types].xml", types.as_bytes()), + ("_rels/.rels", root.as_bytes()), + ("xl/workbook.xml", br#""#), + ("xl/_rels/workbook.xml.rels", rels.as_bytes()), + ("xl/styles.xml", xml.as_bytes()), + ]); + styles_of(&bytes).0.unwrap() +} + +/// What both readers lift out: an ``'s element keeps only its `apply*` flags, and the XML +/// part's own element is dropped. +fn lifted(mut s: Styles) -> Styles { + for xf in s.cell_style_xfs.iter_mut().chain(&mut s.cell_xfs) { + let attrs = xf.el.attrs.iter().filter(|(k, _)| k.starts_with("apply")).cloned().collect(); + xf.el = craft_xml::El::new("xf"); + xf.el.attrs = attrs; + } + s.root = craft_xml::El::default(); + s +} + +#[test] +fn styles_are_read_as_their_xlsx_spelling() { + let (binary, warnings) = styles_of(&styled_file(&styles_bin(), STYLES_TYPES)); + let binary = binary.expect("styles are read"); + assert_eq!(lifted(binary.clone()), lifted(xml_styles(STYLES_XML))); + // A few of the conversions, spelled out. + assert_eq!(binary.fonts[1].size, Some(14.5)); + assert_eq!(binary.fonts[1].family, None); + let solid = Fill::Pattern(PatternFill { + pattern: Some(Pattern::Solid), + fg: Some(Color { source: ColorSource::Rgb(0xFFFF_FF00), tint: 0.0 }), + bg: Some(Color { source: ColorSource::Indexed(64), tint: 0.0 }), + }); + assert_eq!(binary.fills[2], solid); + assert_eq!(binary.cell_xfs[2].protection, Some(Protection { locked: Some(false), hidden: Some(true) })); + assert_eq!(binary.palette()[0], 0x11_2233); + // Written back as SpreadsheetML, a cell style format says what it leaves out, as Excel's does. + let written = String::from_utf8(binary.to_xml()).unwrap(); + assert!(written.contains(r#"applyFont="0" applyFill="0" applyBorder="0" applyAlignment="0" applyProtection="0""#), "{written}"); + assert!(warnings.iter().any(|w| w.contains("alternate-content")), "{warnings:?}"); + assert!(!warnings.iter().any(|w| w.contains("table styles")), "{warnings:?}"); +} + +#[test] +fn damaged_styles_are_a_warning_and_the_workbook_opens() { + let good = styles_bin(); + let end = record(279, &[]); + let body = &good[..good.len() - end.len()]; + let with = |extra: &[u8]| [body, extra, &end].concat(); + let cases: Vec<(&str, Vec, &str)> = vec![ + ("truncated", good[..good.len() - 1].to_vec(), STYLES_TYPES), + ("no end record", body.to_vec(), STYLES_TYPES), + ("unknown fill pattern", with(&pattern_fill(99, &indexed(64), &indexed(65))), STYLES_TYPES), + ("unknown border style", with(&record(46, &[&[0][..], &edge(14, &indexed(64)), &[0; 40]].concat())), STYLES_TYPES), + ("cell format outside its list", with(&brt_xf(0, 0, 0, 0, 0, 0, 0, 0x1010, 0)), STYLES_TYPES), + ("short property", with(&brt_dxf(&[vec![0x19, 0, 2, 0]])), STYLES_TYPES), + ("wrong content type", good.clone(), TYPES), + ]; + for (why, part, types) in cases { + let bytes = styled_file(&part, types); + let (styles, warnings) = styles_of(&bytes); + assert!(styles.is_none(), "{why}"); + assert!(warnings.iter().any(|w| w.contains("look/styles.bin")), "{why}: {warnings:?}"); + assert_eq!(read(&bytes).unwrap().sheets[0].value(CellRef::new(0, 0)), Value::Number(1.0), "{why}"); + } + // A table past 65,536 entries is refused before it is held. + let xfs = brt_xf(0, 0, 0, 0, 0, 0, 0, 0x1010, 0).repeat(65_537); + let (styles, warnings) = styles_of(&styled_file(&with(&[&begin(617, 65_537)[..], &xfs, &record(618, &[])].concat()), STYLES_TYPES)); + assert!(styles.is_none()); + assert!(warnings.iter().any(|w| w.contains("65536 entries")), "{warnings:?}"); + // Custom table styles are not read, and say so. + let table_styles = [body, &record(508, &[&1u32.to_le_bytes()[..], &wide("Probe Table"), &wide("")].concat()), &end].concat(); + let (styles, warnings) = styles_of(&styled_file(&table_styles, STYLES_TYPES)); + assert!(styles.is_some()); + assert!(warnings.iter().any(|w| w.contains("table styles")), "{warnings:?}"); +} From d2b6416db9b8358e337ed7472ec52c262593a27c Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Sat, 14 Nov 2026 11:32:34 -0500 Subject: [PATCH 09/14] craft-xlsx: binary styles: legacy number formats, automatic font colours, tinted fills A broader Excel probe found three differences from the styles.xml Excel writes for the same workbook: - A number format with a newer locale tag ([$-ja-JP-x-gannen]) is only in alternate content: the newer spelling in a block for later versions, and the one .xlsx keeps as formatCode in a block listing version 12 onwards. The styles part now reads the alternate content meant for Excel 2007 (version 12), so the format is no longer lost. - A font whose colour is automatic has no in .xlsx. - A pattern fill whose pattern colour has a tint states the system background (bgColor indexed 65), as Excel's .xlsx does for any pattern. --- crates/xlsx/README.md | 3 ++- crates/xlsx/src/xlsb.rs | 29 ++++++++++++++++++++++------- crates/xlsx/src/xlsb/styles.rs | 12 +++++++++--- crates/xlsx/tests/xlsb.rs | 21 +++++++++++++++++---- 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/crates/xlsx/README.md b/crates/xlsx/README.md index b2b581112..81941fac1 100644 --- a/crates/xlsx/README.md +++ b/crates/xlsx/README.md @@ -86,7 +86,8 @@ they were. A binary workbook is not written. A binary workbook gives the same `Workbook`, strings, rows and merged cells, and the same `Styles`: number formats, fonts, fills, borders, cell and cell style formats, cell styles, differential formats and the indexed colours, as Excel writes them in `styles.xml` (default fill -colours and empty settings left out, a cell style format's `apply*="0"`). Custom table styles and +colours and empty settings left out, a cell style format's `apply*="0"`, a number format with a +newer locale tag in its Excel 2007 spelling, as `formatCode` has it). Custom table styles and recent colours are not read; a damaged styles part is a warning and no styles. Its cell formulas are decoded from their tokens into the text an `.xlsx` file holds (`_xlfn.` and `_xlpm.` names as written, no implicit `@`), with `FormulaKind::Shared` or `FormulaKind::Array` and their ranges, and `BrtCellMeta` as the cell diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index 50da6185c..043b0743b 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -175,12 +175,16 @@ struct Records<'a, 'l> { bytes: Bytes<'a>, limits: &'l Limits, budget: &'l Budget, - blocks: Vec, + /// Each open block's end record, and whether its records are read. + blocks: Vec<(u16, bool)>, + /// Read the alternate content meant for Excel 2007 (version 12), as an `.xlsx` reader sees + /// the main attribute where Excel keeps a newer spelling in an extension. + excel_2007: bool, } impl<'a, 'l> Records<'a, 'l> { fn new(bytes: &'a [u8], limits: &'l Limits, budget: &'l Budget) -> Self { - Self { bytes: Bytes::new(bytes), limits, budget, blocks: vec![] } + Self { bytes: Bytes::new(bytes), limits, budget, blocks: vec![], excel_2007: false } } fn next(&mut self, warnings: &mut Warnings) -> Result, Error> { @@ -209,6 +213,7 @@ impl<'a, 'l> Records<'a, 'l> { // BrtFRTBegin, BrtACBegin. 35 | 37 => { let mut b = Bytes::new(payload); + let mut read = false; if id == 35 { b.take(4)?; } else { @@ -216,22 +221,32 @@ impl<'a, 'l> Records<'a, 'l> { if count == 0 { return Err(invalid("empty alternate-content version list")); } - b.take(usize::from(count) * 4)?; + // ACProductVersion: fileVersion, then fileProduct (15 bits) and + // fileExtension (the version and later). + for _ in 0..count { + let (version, product) = (b.u16()?, b.u16()?); + let later = product & 0x8000 != 0; + read |= self.excel_2007 && product & 0x7FFF == 0 && (version == 12 || later && version <= 12); + } } b.finish()?; if self.blocks.len() >= 64 { return Err(invalid("extension blocks nested too deeply")); } - self.blocks.push(id + 1); - warnings.warn("unsupported XLSB future-record and alternate-content blocks were skipped"); + // Inside a skipped block, everything is skipped. + let read = read && self.blocks.iter().all(|(_, r)| *r); + self.blocks.push((id + 1, read)); + if !read { + warnings.warn("unsupported XLSB future-record and alternate-content blocks were skipped"); + } } // BrtFRTEnd, BrtACEnd. 36 | 38 => { - if self.blocks.pop() != Some(id) || !payload.is_empty() { + if self.blocks.pop().map(|(end, _)| end) != Some(id) || !payload.is_empty() { return Err(invalid("mismatched extension block end")); } } - _ if !self.blocks.is_empty() => {} + _ if self.blocks.iter().any(|(_, read)| !read) => {} _ => return Ok(Some((id, payload))), } } diff --git a/crates/xlsx/src/xlsb/styles.rs b/crates/xlsx/src/xlsb/styles.rs index 094620a07..50673ef28 100644 --- a/crates/xlsx/src/xlsb/styles.rs +++ b/crates/xlsx/src/xlsb/styles.rs @@ -151,10 +151,11 @@ fn font(b: &mut Bytes<'_>) -> Result { extend: on(0x80), underline: (uls != 0).then(|| underline(u32::from(uls))), vert_align: (sss != 0).then(|| script(u32::from(sss))), - color, family: (family != 0).then_some(u32::from(family)), charset: (charset != 0).then_some(u32::from(charset)), scheme: scheme(u32::from(font_scheme)).filter(|s| *s != FontScheme::None), + // An automatic colour is a font with no `` in `.xlsx`. + color: color.filter(|c| c.source != ColorSource::Auto), }) } @@ -164,7 +165,8 @@ fn gradient(b: &mut Bytes<'_>, path: bool) -> Result { } /// `BrtFill`. Excel's `.xlsx` leaves out the default colours: system foreground (indexed 64) as -/// the pattern colour and system background (indexed 65) as the background. +/// the pattern colour and system background (indexed 65) as the background, unless the pattern +/// colour has a tint (as the built-in "20% - Accent1" styles do), when it states the background. fn fill(b: &mut Bytes<'_>) -> Result { let fls = b.u32()?; let fg = color(b)?; @@ -183,7 +185,8 @@ fn fill(b: &mut Bytes<'_>) -> Result { return Ok(Fill::Gradient(g)); } let system = |c: Color, i: u32| c.source == ColorSource::Indexed(i) && c.tint == 0.0; - Ok(Fill::Pattern(PatternFill { pattern: Some(pattern(fls)?), fg: fg.filter(|c| !system(*c, 64)), bg: bg.filter(|c| !system(*c, 65)) })) + let tinted = fg.is_some_and(|c| c.tint != 0.0); + Ok(Fill::Pattern(PatternFill { pattern: Some(pattern(fls)?), fg: fg.filter(|c| !system(*c, 64)), bg: bg.filter(|c| tinted || !system(*c, 65)) })) } /// `BrtBorder`: the five edges a cell border has, each stated (`` when it has no line). @@ -393,6 +396,9 @@ fn dxf(b: &mut Bytes<'_>) -> Result { /// The styles part: `BrtBeginStyleSheet` to `BrtEndStyleSheet`. pub(crate) fn styles(bytes: &[u8], limits: &Limits, budget: &Budget, warnings: &mut Warnings) -> Result { let mut records = Records::new(bytes, limits, budget); + // Excel keeps a number format with a newer locale tag in two alternate-content blocks: the + // newer spelling for itself, and for Excel 2007 the one `.xlsx` has as `formatCode`. + records.excel_2007 = true; records.first(278, warnings)?; let mut s = Styles::default(); // Inside BrtBeginCellStyleXFs (true) or BrtBeginCellXFs (false). diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index bc1a7f6bd..85643b0ee 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -1014,6 +1014,8 @@ fn styles_bin() -> Vec { // character set 0 and scheme "none" are not written in `.xlsx`. let fancy = font(290, 0x0A, 700, 1, 0x22, 0, 2, &rgb(0, 128, 255), 0, "Wingdings"); let heading = font(220, 0, 400, 0, 0, 2, 0, &theme(4, 13106), 1, "Aptos Display"); + // An automatic colour: no `` in `.xlsx`. + let auto = font(220, 0x02, 400, 0, 0, 2, 0, &color(0, 64, 0, [0, 0, 0, 255]), 2, "Aptos Narrow"); let gradient = [ &0x28u32.to_le_bytes()[..], &indexed(64), @@ -1030,20 +1032,31 @@ fn styles_bin() -> Vec { let none = edge(0, &color(0, 0, 0, [0, 0, 0, 0])); let parts: Vec> = vec![ record(278, &[]), - begin(615, 1), + begin(615, 2), record(44, &[&164u16.to_le_bytes()[..], &wide(r"yyyy\-mm\-dd")].concat()), + // A format in two alternate-content blocks: the newer spelling for version 13 and later + // (skipped), and the one for versions 12 and 13, which `.xlsx` has as `formatCode`. + record(37, &[1, 0, 13, 0, 0, 0x80]), + record(44, &[&165u16.to_le_bytes()[..], &wide("[$-ja-JP-x-gannen]ge")].concat()), + record(38, &[]), + record(37, &[2, 0, 12, 0, 0, 0, 13, 0, 0, 0]), + record(44, &[&165u16.to_le_bytes()[..], &wide("[$]ge")].concat()), + record(38, &[]), record(616, &[]), - begin(611, 3), + begin(611, 4), normal, fancy, heading, + auto, record(612, &[]), - begin(603, 5), + begin(603, 6), pattern_fill(0, &indexed(64), &indexed(65)), pattern_fill(17, &indexed(64), &indexed(65)), pattern_fill(1, &rgb(255, 255, 0), &indexed(64)), pattern_fill(9, &theme(5, -8191), &indexed(65)), record(45, &gradient), + // A tinted pattern colour: `.xlsx` states the system background, as for darkGrid above. + pattern_fill(1, &theme(4, 26213), &indexed(65)), record(604, &[]), begin(613, 2), record(46, &[&[0][..], &none, &none, &none, &none, &none].concat()), @@ -1109,7 +1122,7 @@ fn styles_bin() -> Vec { parts.concat() } -const STYLES_XML: &str = r#""#; +const STYLES_XML: &str = r#""#; /// The same styles from a SpreadsheetML package. fn xml_styles(xml: &str) -> Styles { From abceed3f17c3a17de5680c5bd8d14666804e22f7 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Sat, 14 Nov 2026 11:56:51 -0500 Subject: [PATCH 10/14] craft-xlsx: binary formulas keep typed spacing and Excel's number spelling Checked against a wider Excel probe (every worksheet function Excel accepts, operators, references, constants, spacing, shared and array formulas), three differences from the .xlsx text are fixed: - PtgAttrSpace: the spaces and line breaks typed in a formula are kept where Excel writes them in .xlsx (before an operand, an operator's symbol or a call, after a parenthesis's or call's opening and before its closing), and the text is trimmed as the .xlsx reader trims it. - Numbers are written out in full when that takes at most 21 characters (100000000000000000000, 0.0000000000000000001), scientific past that (1E+21, 1.5E-19, 1.23456789012345E-06), as Excel spells them. - ISO.CEILING, ECMA.CEILING, NETWORKDAYS.INTL and WORKDAY.INTL are called through hidden _xlfn. names in .xlsb, but .xlsx names them bare. --- crates/xlsx/README.md | 4 +- crates/xlsx/src/xlsb/expr.rs | 64 +++++++++++---- crates/xlsx/src/xlsb/formula.rs | 133 +++++++++++++++++++++++++++++--- crates/xlsx/tests/xlsb.rs | 77 ++++++++++++++++++ 4 files changed, 249 insertions(+), 29 deletions(-) diff --git a/crates/xlsx/README.md b/crates/xlsx/README.md index 1dc30574d..e03128e81 100644 --- a/crates/xlsx/README.md +++ b/crates/xlsx/README.md @@ -85,7 +85,9 @@ they were. A binary workbook is not written. A binary workbook gives the same `Workbook`, strings, rows and merged cells. Its styles are not read (each cell keeps its format index). Its cell formulas are decoded from their tokens into the -text an `.xlsx` file holds (`_xlfn.` and `_xlpm.` names as written, no implicit `@`), with +text an `.xlsx` file holds (`_xlfn.` and `_xlpm.` names as written, apart from the four +ISO/IEC 29500 functions `.xlsx` names bare, no implicit `@`, the spaces and line breaks typed in +the formula, numbers as Excel spells them), with `FormulaKind::Shared` or `FormulaKind::Array` and their ranges, and `BrtCellMeta` as the cell metadata index. Every cell of a shared formula gets its own text, decoded at the cell, since the file keeps the tokens once. A formula whose decoding needs something the reader does not read diff --git a/crates/xlsx/src/xlsb/expr.rs b/crates/xlsx/src/xlsb/expr.rs index 3dd643a6a..197644a33 100644 --- a/crates/xlsx/src/xlsb/expr.rs +++ b/crates/xlsx/src/xlsb/expr.rs @@ -120,6 +120,9 @@ pub(crate) enum Expr { Ref(Reference), /// A defined name. Name(String), + /// Spaces and line breaks typed before and after an expression (`PtgAttrSpace`), which + /// `.xlsx` formula text keeps. + Spaced(String, Box, String), Unary(UnOp, Box), Binary(BinOp, Box, Box), Call(String, Vec), @@ -195,23 +198,33 @@ fn sci_text(n: f64, sig: usize) -> String { format!("{}E{}{:02}", mant, if e < 0 { '-' } else { '+' }, e.abs()) } -/// A number as a formula spells it: up to 15 significant digits, scientific for very large or -/// very small magnitudes. +/// A number as Excel spells it in a formula: rounded to 15 significant digits, written out in +/// full (`100000000000000000000`, `0.0000000000000000001`) when that takes at most 21 characters +/// without the sign, and scientific (`1E+21`, `1.5E-19`, `1.23456789012345E-06`) when it would +/// take more. pub(crate) fn number_to_text(n: f64) -> String { if n == 0.0 || !n.is_finite() { return "0".into(); } - let a = n.abs(); - if !(1e-9..1e15).contains(&a) && a.round() != a || a >= 1e15 { + let s = format!("{:.14e}", n.abs()); + let Some((mant, exp)) = s.split_once('e') else { return s }; + let Ok(e) = exp.parse::() else { return s }; + let digits: String = mant.chars().filter(char::is_ascii_digit).collect(); + let digits = digits.trim_end_matches('0'); + let digits = if digits.is_empty() { "0" } else { digits }; + let k = digits.len() as i32; + let plain_len = if e >= 0 { (e + 1).max(k + i32::from(k > e + 1)) } else { 1 - e + k }; + if plain_len > 21 { return sci_text(n, 15); } - // A value just below 1e15 can round up to 1e15 at 15 significant digits. - if format!("{a:.14e}").parse::().is_ok_and(|r| r >= 1e15) { - return sci_text(n, 15); - } - let mag = a.log10().floor() as i32; - let decimals = (14 - mag).clamp(0, 30) as usize; - trim_decimal(&format!("{n:.decimals$}")) + let sign = if n < 0.0 { "-" } else { "" }; + let plain = if e >= 0 { + let int = (e + 1) as usize; + if digits.len() <= int { format!("{digits}{}", "0".repeat(int - digits.len())) } else { format!("{}.{}", &digits[..int], &digits[int..]) } + } else { + format!("0.{}{digits}", "0".repeat((-e - 1) as usize)) + }; + format!("{sign}{plain}") } fn number(n: f64) -> String { @@ -248,11 +261,14 @@ fn sheet_split(n: &str) -> Option<(&str, &str)> { at.and_then(|i| Some((n.get(..i)?, n.get(i + 1..)?))) } -/// Prints an expression in A1 notation. +/// Prints an expression in A1 notation, trimmed as the `.xlsx` reader trims formula text. pub(crate) fn print(e: &Expr) -> String { let mut s = String::new(); write_expr(&mut s, e); - s + match s.trim() { + t if t.len() == s.len() => s, + t => t.to_string(), + } } fn write_args(s: &mut String, args: &[Expr], sep: char) { @@ -323,6 +339,11 @@ fn write_expr(s: &mut String, e: &Expr) { write_expr(s, x); s.push(')'); } + Expr::Spaced(before, x, after) => { + s.push_str(before); + write_expr(s, x); + s.push_str(after); + } } } @@ -336,10 +357,21 @@ mod tests { assert_eq!(number_to_text(-2.5), "-2.5"); assert_eq!(number_to_text(0.1 + 0.2), "0.3"); assert_eq!(number_to_text(123456789012345.0), "123456789012345"); - assert_eq!(number_to_text(1e15), "1E+15"); - assert_eq!(number_to_text(1234567890123456.0), "1.23456789012346E+15"); + // Written out in full up to 21 characters, as Excel writes them in .xlsx. + assert_eq!(number_to_text(1e15), "1000000000000000"); + assert_eq!(number_to_text(1234567890123456.0), "1234567890123460"); + assert_eq!(number_to_text(-1e20), "-100000000000000000000"); + assert_eq!(number_to_text(1e21), "1E+21"); + assert_eq!(number_to_text(1.5e21), "1.5E+21"); assert_eq!(number_to_text(0.0001), "0.0001"); - assert_eq!(number_to_text(1e-10), "1E-10"); + assert_eq!(number_to_text(1e-10), "0.0000000001"); + assert_eq!(number_to_text(1e-19), "0.0000000000000000001"); + assert_eq!(number_to_text(1e-20), "1E-20"); + assert_eq!(number_to_text(1.5e-19), "1.5E-19"); + assert_eq!(number_to_text(1.23456789012345e-5), "0.0000123456789012345"); + assert_eq!(number_to_text(1.23456789012345e-6), "1.23456789012345E-06"); + assert_eq!(number_to_text(1234567890.12345), "1234567890.12345"); + assert_eq!(number_to_text(1e100), "1E+100"); assert!(number(f64::MAX).parse::().is_ok_and(f64::is_finite)); } } diff --git a/crates/xlsx/src/xlsb/formula.rs b/crates/xlsx/src/xlsb/formula.rs index 327ffcc25..22355b630 100644 --- a/crates/xlsx/src/xlsb/formula.rs +++ b/crates/xlsx/src/xlsb/formula.rs @@ -46,9 +46,10 @@ fn pop_n(stack: &mut Stack, n: usize, kids: &mut usize) -> Result, Uns Ok(popped.into_iter().map(|(e, _)| e).collect()) } -/// Pushes the result of a token one level above the operands it consumed. +/// Pushes the result of a token one level above the operands it consumed (two for one with the +/// spacing typed around it, which prints as a level of its own). fn push(stack: &mut Stack, kids: usize, e: Expr) -> Result<(), Unsupported> { - let depth = kids.saturating_add(1); + let depth = kids.saturating_add(1).saturating_add(usize::from(matches!(e, Expr::Spaced(..)))); if depth > MAX_DEPTH { return Err(Unsupported("formula nested too deeply")); } @@ -56,6 +57,48 @@ fn push(stack: &mut Stack, kids: usize, e: Expr) -> Result<(), Unsupported> { Ok(()) } +/// Spaces and line breaks typed in the formula (`PtgAttrSpace`, [MS-XLSB] 2.5.98.32), waiting +/// for the token they belong to. Excel writes the attribute just before that token: spaces +/// before an operand, an operator's symbol or a function call go in `before`; those before a +/// parenthesis's `(`, or before the `)` of a parenthesis or a call, in `open` and `close`. +#[derive(Default)] +struct Spacing { + before: String, + open: String, + close: String, +} + +/// `e` with `ws` typed before it. +fn spaced_before(e: Expr, ws: String) -> Expr { + match e { + _ if ws.is_empty() => e, + Expr::Spaced(b, x, a) => Expr::Spaced(ws + &b, x, a), + e => Expr::Spaced(ws, Box::new(e), String::new()), + } +} + +/// `e` with `ws` typed after it. +fn spaced_after(e: Expr, ws: String) -> Expr { + match e { + _ if ws.is_empty() => e, + Expr::Spaced(b, x, a) => Expr::Spaced(b, x, a + &ws), + e => Expr::Spaced(String::new(), Box::new(e), ws), + } +} + +/// A call's arguments with the spacing typed before its `)`: after the last argument, or alone +/// between the parentheses (`PI( )`). +fn close_args(mut args: Vec, ws: String) -> Vec { + if ws.is_empty() { + return args; + } + match args.pop() { + Some(last) => args.push(spaced_after(last, ws)), + None => args.push(Expr::Spaced(String::new(), Box::new(Expr::Missing), ws)), + } + args +} + /// An error constant's code ([MS-XLSB] 2.5.98.21, `BErr`). fn error_code(code: u8) -> Option { Some(match code { @@ -261,11 +304,16 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res let mut t = Bytes { b: rgce, pos: 0 }; let mut extra = Bytes { b: rgcb, pos: 0 }; let mut stack: Stack = Vec::new(); + let mut ws = Spacing::default(); while t.pos < rgce.len() { let ptg = t.u8()?; let mut kids = 0usize; // Operand classes (reference / value / array) share one token number. let id = if ptg & 0x60 != 0 { 0x20 | (ptg & 0x1F) } else { ptg }; + // An operand (a constant, reference or name) takes the spacing typed before it; operators, + // calls and parentheses place theirs below, and attribute and cache tokens take none. + let operand = !matches!(id, 0x03..=0x15 | 0x19 | 0x21 | 0x22 | 0x26..=0x29); + let height = stack.len(); match id { 0x03..=0x11 => { let op = match id { @@ -287,6 +335,10 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res }; let b = pop(&mut stack, &mut kids)?; let a = pop(&mut stack, &mut kids)?; + // Spacing before the operator's symbol follows the left operand. + let before = std::mem::take(&mut ws.before); + kids = kids.saturating_add(usize::from(!before.is_empty())); + let a = spaced_after(a, before); push(&mut stack, kids, Expr::Binary(op, Box::new(a), Box::new(b)))?; } 0x12..=0x14 => { @@ -296,11 +348,23 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res _ => UnOp::Percent, }; let x = pop(&mut stack, &mut kids)?; - push(&mut stack, kids, Expr::Unary(op, Box::new(x)))?; + // A sign's spacing comes before it; `%`'s before its symbol, after the operand. + let before = std::mem::take(&mut ws.before); + let e = if op == UnOp::Percent { + kids = kids.saturating_add(usize::from(!before.is_empty())); + Expr::Unary(op, Box::new(spaced_after(x, before))) + } else { + spaced_before(Expr::Unary(op, Box::new(x)), before) + }; + push(&mut stack, kids, e)?; } 0x15 => { let x = pop(&mut stack, &mut kids)?; - push(&mut stack, kids, Expr::Paren(Box::new(x)))?; + let close = std::mem::take(&mut ws.close); + kids = kids.saturating_add(usize::from(!close.is_empty())); + let x = spaced_after(x, close); + let before = std::mem::take(&mut ws.open) + &std::mem::take(&mut ws.before); + push(&mut stack, kids, spaced_before(Expr::Paren(Box::new(x)), before))?; } 0x16 => push(&mut stack, kids, Expr::Missing)?, // PtgStr: cch (u16, at most 255) and always UTF-16, unlike BIFF8. @@ -318,14 +382,27 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res let n = usize::from(t.u16()?) + 1; t.take(n * 2)?; } else { - t.take(2)?; + let data = t.take(2)?; + if kind & 0x40 != 0 { + // PtgAttrSpace / PtgAttrSpaceSemi: PtgAttrSpaceType's `type` and `cch`. + let (kind, n) = (data.first().copied().unwrap_or(0), data.get(1).copied().unwrap_or(0)); + let text = if matches!(kind, 1 | 3 | 5) { "\n" } else { " " }.repeat(usize::from(n)); + match kind { + 0 | 1 | 6 => ws.before.push_str(&text), + 2 | 3 => ws.open.push_str(&text), + 4 | 5 => ws.close.push_str(&text), + _ => return Err(Unsupported("unknown spacing in a formula")), + } + } } if kind & 0x10 != 0 { // PtgAttrSum: SUM of the single operand. let x = pop(&mut stack, &mut kids)?; - push(&mut stack, kids, Expr::Call("SUM".into(), vec![x]))?; + let args = close_args(vec![x], std::mem::take(&mut ws.close)); + let call = spaced_before(Expr::Call("SUM".into(), args), std::mem::take(&mut ws.before)); + push(&mut stack, kids.saturating_add(1), call)?; } - // Semi (volatile), If, Goto, Space, Baxcel: no effect on the expression. + // Semi (volatile), If, Goto, Baxcel: no effect on the expression. } 0x1C => { let e = t.u8()?; @@ -346,8 +423,9 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res let iftab = t.u16()?; let (name, args) = function(iftab).ok_or(Unsupported("unknown function"))?; let n = usize::try_from(args).map_err(|_| Unsupported("unknown function argument count"))?; - let args = pop_n(&mut stack, n, &mut kids)?; - push(&mut stack, kids, Expr::Call(name.to_string(), args))?; + let args = close_args(pop_n(&mut stack, n, &mut kids)?, std::mem::take(&mut ws.close)); + let call = spaced_before(Expr::Call(name.to_string(), args), std::mem::take(&mut ws.before)); + push(&mut stack, kids.saturating_add(1), call)?; } 0x22 => { let n = usize::from(t.u8()?); @@ -362,20 +440,33 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res if args.is_empty() { return Err(Unsupported("malformed function call")); } - let name = match args.remove(0) { + // Spacing typed after the `(` comes before the name's token: it belongs to the + // first argument. + let (lead, first) = match args.remove(0) { + Expr::Spaced(before, x, _) => (before, *x), + e => (String::new(), e), + }; + let name = match first { Expr::Name(n) => n, _ => return Err(Unsupported("function call through an expression")), }; + if let Some(a) = args.first_mut() { + *a = spaced_before(std::mem::replace(a, Expr::Missing), lead); + } // `PY(index, …)` runs a script stored elsewhere in the package, which this // import doesn't read. `PY("code", …)` carries its code and becomes a Python cell. let py = name.rsplit('.').next().is_some_and(|n| n.eq_ignore_ascii_case("PY")); if py && !args.first().is_some_and(literal_text) { return Err(Unsupported("Python code stored outside the cell")); } - push(&mut stack, kids, Expr::Call(name, args))?; + let args = close_args(args, std::mem::take(&mut ws.close)); + let call = spaced_before(Expr::Call(bare_function(name), args), std::mem::take(&mut ws.before)); + push(&mut stack, kids.saturating_add(1), call)?; } else { let (name, _) = function(tab & 0x7FFF).ok_or(Unsupported("unknown function"))?; - push(&mut stack, kids, Expr::Call(name.to_string(), args))?; + let args = close_args(args, std::mem::take(&mut ws.close)); + let call = spaced_before(Expr::Call(name.to_string(), args), std::mem::take(&mut ws.before)); + push(&mut stack, kids.saturating_add(1), call)?; } } 0x23 => { @@ -456,6 +547,10 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res } _ => return Err(Unsupported("unknown formula token")), } + if operand && !ws.before.is_empty() && stack.len() == height + 1 { + let (e, depth) = stack.pop().ok_or(Unsupported("malformed formula (operand missing)"))?; + push(&mut stack, depth, spaced_before(e, std::mem::take(&mut ws.before)))?; + } if stack.len() > MAX_RGCE { return Err(Unsupported("formula too complex")); } @@ -467,11 +562,23 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res Ok(grouped(e, false)) } +/// The name a function called through a hidden name is written with in `.xlsx`: that name +/// (`_xlfn.CONCAT`), except for the four ISO/IEC 29500 functions, which `.xlsx` names without the +/// prefix though `.xlsb` calls them as it calls newer functions. +fn bare_function(name: String) -> String { + const ISO: [&str; 4] = ["ISO.CEILING", "ECMA.CEILING", "NETWORKDAYS.INTL", "WORKDAY.INTL"]; + match name.get(..6).filter(|p| p.eq_ignore_ascii_case("_xlfn.")).and(name.get(6..)) { + Some(bare) if ISO.iter().any(|f| f.eq_ignore_ascii_case(bare)) => bare.to_string(), + _ => name, + } +} + /// Text written out in the formula: a string, or strings joined with `&` (how Python code /// longer than a string constant is written). fn literal_text(e: &Expr) -> bool { match e { Expr::Text(_) => true, + Expr::Spaced(_, x, _) => literal_text(x), Expr::Binary(BinOp::Concat, a, b) => literal_text(a) && literal_text(b), _ => false, } @@ -494,6 +601,7 @@ fn binding(e: &Expr) -> u8 { Expr::Binary(op, ..) => op.precedence(), Expr::Unary(UnOp::Neg | UnOp::Plus, _) => 60, Expr::Unary(UnOp::Percent, _) => 70, + Expr::Spaced(_, x, _) => binding(x), _ => u8::MAX, } } @@ -550,6 +658,7 @@ fn grouped(e: Expr, in_args: bool) -> Expr { Expr::Unary(op, Box::new(if needs { paren(x) } else { x })) } Expr::Paren(x) => paren(grouped(*x, false)), + Expr::Spaced(before, x, after) => Expr::Spaced(before, Box::new(grouped(*x, in_args)), after), Expr::Call(name, args) => Expr::Call(name, args.into_iter().map(|a| grouped(a, true)).collect()), other => other, } diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index ee9ac3afd..1be653c87 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -930,3 +930,80 @@ fn a_dynamic_array_kept_as_its_value_has_no_cell_metadata() { assert_eq!(sheet.cell_metadata.iter().collect::>(), [(&at("C1"), &1)]); assert!(warned(&wb, "missing from the file"), "{:?}", wb.warnings); } + +/// `PtgAttrSpace` ([MS-XLSB] 2.5.98.31): its type (0 spaces before the next token, 1 line breaks +/// before it, 2 spaces before a `(`, 4 spaces before a `)`) and count. +fn space(kind: u8, n: u8) -> Vec { + vec![0x19, 0x40, kind, n] +} + +#[test] +fn typed_spacing_numbers_and_iso_functions_read_as_in_xlsx() { + let a1 = || [&[0x24u8][..], &loc(0, REL)].concat(); + let b1 = || [&[0x24u8][..], &loc(0, 1 | REL)].concat(); + let num = |n: f64| [&[0x1Fu8][..], &n.to_le_bytes()].concat(); + let area = |c: u16| [&[0x25u8][..], &0u32.to_le_bytes(), &1u32.to_le_bytes(), &(c | REL).to_le_bytes(), &(c | REL).to_le_bytes()].concat(); + let goto = [0x19u8, 0x08, 0, 0]; + let cells: Vec> = vec![ + // Spacing goes before the next token: an operand, or an operator's symbol. + [&[0x1Eu8, 1, 0][..], &space(0, 2), &[0x1E, 2, 0], &space(0, 1), &[0x03]].concat(), + // ... or a call's `)`, and inside a call's parentheses. + [space(0, 1), a1(), space(0, 1), b1(), space(4, 1), vec![0x42, 2, 4, 0]].concat(), + // Line breaks. + [ + a1(), + vec![0x1E, 0, 0, 0x0D, 0x19, 0x02, 0, 0], + space(1, 1), + space(0, 2), + vec![0x1E, 1, 0], + goto.to_vec(), + space(1, 1), + space(0, 2), + vec![0x1E, 2, 0], + goto.to_vec(), + vec![0x42, 3, 1, 0], + ] + .concat(), + // Inside and before parentheses. + [space(0, 1), vec![0x1E, 1, 0], space(0, 1), vec![0x1E, 2, 0], space(0, 1), vec![0x03], space(4, 1), vec![0x15]].concat(), + [vec![0x1E, 1, 0, 0x1E, 2, 0], space(2, 1), vec![0x15, 0x03]].concat(), + // A call with no arguments, and one through PtgAttrSum. + [space(4, 1), vec![0x41, 19, 0]].concat(), + [area(0), area(1), space(0, 1), vec![0x0F, 0x19, 0x10, 0, 0]].concat(), + // A sign, `%`, and leading spacing (trimmed, as `.xlsx` text is read). + [space(0, 1), vec![0x1E, 1, 0, 0x13], space(0, 1), vec![0x14]].concat(), + [space(0, 3), a1()].concat(), + // A newer function: spacing before its name's token is the first argument's. + [vec![0x1E, 1, 0], space(0, 1), vec![0x23, 5, 0, 0, 0], num(1.5), space(0, 1), vec![0x42, 2, 0xFF, 0, 0x03]].concat(), + vec![0x23, 1, 0, 0, 0, 0x17, 1, 0, b'a', 0, 0x42, 2, 0xFF, 0], + // Numbers written out up to 21 characters, scientific past them. + [num(1e20), num(1e21), vec![0x03], num(1e-19), vec![0x03], num(1.5e-19), vec![0x03], num(1234567890123456.0), vec![0x03]].concat(), + ]; + let mut data = row(0); + for (i, rgce) in cells.iter().enumerate() { + data.extend(formula_cell(i as u32 + 1, rgce, &[])); + } + // Name 5: the hidden name ISO.CEILING is called through, as Excel writes it. + let mut book = book_with_links(&xti()); + let end = record(132, &[]); + book.truncate(book.len() - end.len()); + book.extend(name(0x0002_000b, "_xlfn.ISO.CEILING")); + book.extend(end); + let wb = read(&package(&book, &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES)).unwrap(); + let got: Vec<_> = (1..=cells.len() as u32).map(|c| wb.sheets[0].formula(CellRef::new(0, c))).collect(); + let want = [ + "1 + 2", + "SUM( A1, B1 )", + "IF(A1>0,\n 1,\n 2)", + "( 1 + 2 )", + "1+ (2)", + "PI( )", + "SUM(A1:A2 B1:B2)", + "- 1 %", + "A1", + "1+ ISO.CEILING( 1.5)", + "_xlfn.CONCAT(\"a\")", + "100000000000000000000+1E+21+0.0000000000000000001+1.5E-19+1234567890123460", + ]; + assert_eq!(got, want.map(Some)); +} From 5742279729a6a9cd0118ccf4cee4b99314e7bb56 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Sat, 14 Nov 2026 12:01:46 -0500 Subject: [PATCH 11/14] craft-xlsx: binary formulas name macro sheets and span chart sheets An Excel probe showed two references the decoder refused that .xlsx spells: - A reference to an XLM macro sheet (Macro1!$A$1): macro sheets have cells, so the sheet links now name them. - A 3-D span with a chart sheet between its ends ('Data:After Sheet'!$A$1): a span is spelled by its ends, as .xlsx has it. An end with no cells is still refused. --- crates/xlsx/src/xlsb.rs | 5 +++-- crates/xlsx/src/xlsb/formula.rs | 13 +++++-------- crates/xlsx/tests/xlsb.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index 2fdde59be..65aab0eb8 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -351,9 +351,10 @@ pub(crate) fn workbook( if let SheetKind::Other(_) = kind { return Err(invalid("unknown sheet relationship type")); } - // Only a worksheet has cells a formula can refer to. + // Only worksheets and macro sheets have cells a formula can refer to (a macro's + // name refers to its macro sheet: `Macro1!$A$1`). if let Some(l) = links.as_mut() { - l.sheets.push((kind == SheetKind::Worksheet).then(|| name.clone())); + l.sheets.push(matches!(kind, SheetKind::Worksheet | SheetKind::Macrosheet).then(|| name.clone())); } wb.sheets.push(SheetEntry { name, sheet_id, visibility, rel_id, kind, part: Some(rel.target.clone()) }); } diff --git a/crates/xlsx/src/xlsb/formula.rs b/crates/xlsx/src/xlsb/formula.rs index 22355b630..3a6960b39 100644 --- a/crates/xlsx/src/xlsb/formula.rs +++ b/crates/xlsx/src/xlsb/formula.rs @@ -147,8 +147,8 @@ pub(super) struct Name { /// Workbook-level tables formulas refer to. #[derive(Default)] pub(crate) struct Links { - /// Every sheet's name in `BrtBundleSh` order, `None` for sheets the reader skips or that hold - /// no cells (chart, dialog and macro sheets). + /// Every sheet's name in `BrtBundleSh` order, `None` for the sheets that hold no cells (chart, + /// dialog and module sheets). pub(super) sheets: Vec>, /// Every `BrtName`, in order: `PtgName` indexes them from 1. pub(super) names: Vec, @@ -179,14 +179,11 @@ impl Links { let (Ok(first), Ok(last)) = (usize::try_from(x.first), usize::try_from(x.last)) else { return Err(Unsupported("reference to a missing sheet")); }; - // A span takes every sheet between its ends, so a skipped one anywhere in it changes - // what the reference means. + // A span is spelled by its ends. A sheet between them with no cells (a chart sheet) adds + // nothing to it, and `.xlsx` spells the span the same; an end with no cells can't be named. let span = self.sheets.get(first..=last).filter(|s| !s.is_empty()).ok_or(Unsupported("reference to a missing sheet"))?; - if span.iter().any(Option::is_none) { - return Err(Unsupported("references to sheets the reader skips")); - } let (Some(Some(a)), Some(Some(b))) = (span.first(), span.last()) else { - return Err(Unsupported("reference to a missing sheet")); + return Err(Unsupported("references to sheets with no cells")); }; Ok(Some(if first == last { SheetSel::Named(a.clone()) } else { SheetSel::Span(a.clone(), b.clone()) })) } diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index 1be653c87..950dc8847 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -1007,3 +1007,31 @@ fn typed_spacing_numbers_and_iso_functions_read_as_in_xlsx() { ]; assert_eq!(got, want.map(Some)); } + +#[test] +fn references_to_macro_sheets_and_spans_over_chart_sheets_are_named() { + // Sheets Data, a chart sheet, an XLM macro sheet, Hidden; sheet links Macro1, Data:Hidden + // (over the chart sheet) and Chart1 (no cells). + let mut book = record(131, &[]); + book.extend(record(153, &[[0u8; 8].as_slice(), &wide("")].concat())); + book.extend(record(143, &[])); + for (id, rel, name) in [(1u32, "one", "Data"), (3, "c", "Chart1"), (4, "m", "Macro1"), (2, "two", "Hidden")] { + book.extend(record(156, &[&0u32.to_le_bytes()[..], &id.to_le_bytes(), &wide(rel), &wide(name)].concat())); + } + book.extend(record(144, &[])); + let mut xti = 3u32.to_le_bytes().to_vec(); + for (first, last) in [(2i32, 2i32), (0, 3), (1, 1)] { + xti.extend([0u32.to_le_bytes(), first.to_le_bytes(), last.to_le_bytes()].concat()); + } + book.extend([record(353, &[]), record(357, &[]), record(362, &xti), record(354, &[]), record(132, &[])].concat()); + let rels = RELS.replace( + "", + r#""#, + ); + let ref3d = |ixti: u16| [&[0x5Au8][..], &ixti.to_le_bytes(), &loc(0, 0)].concat(); + let data = [row(0), formula_cell(1, &ref3d(0), &[]), formula_cell(2, &ref3d(1), &[]), formula_cell(3, &ref3d(2), &[])].concat(); + let wb = read(&package(&book, &worksheet(&data, &[]), &sst(), ROOT, &rels, TYPES)).unwrap(); + let got: Vec<_> = (1..=3).map(|c| wb.sheets[0].formula(CellRef::new(0, c))).collect(); + assert_eq!(got, [Some("Macro1!$A$1"), Some("Data:Hidden!$A$1"), None]); + assert!(warned(&wb, "references to sheets with no cells"), "{:?}", wb.warnings); +} From fa5457bed643d167816d9d188c3b1fc4ebd68d2e Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Sat, 14 Nov 2026 12:35:54 -0500 Subject: [PATCH 12/14] craft-xlsx: review fixes: array constants bounded per formula, a spent allowance skips decoding - A formula's array constants share one allowance of 65,536 values, each array charged before its rows are allocated. Before, the cap was per array, so a formula of many arrays could hold gigabytes before the formula text allowance was checked. - Once binary_formula_text is spent, a formula is not decoded only to be dropped: it keeps its saved result straight away. --- crates/xlsx/README.md | 5 +++-- crates/xlsx/src/xlsb.rs | 5 +++++ crates/xlsx/src/xlsb/formula.rs | 15 ++++++++++----- crates/xlsx/tests/xlsb.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/crates/xlsx/README.md b/crates/xlsx/README.md index e03128e81..b8669288e 100644 --- a/crates/xlsx/README.md +++ b/crates/xlsx/README.md @@ -104,8 +104,9 @@ other workbooks, Python code stored outside the cell, data tables. The XLSB func (40 million records, 4 million cells and as many shared strings, 256 MiB of text, as `craft-xls` allows). `binary_formula_text`: decoded formula text over the workbook (128 MiB); past it, formulas keep their saved results. A formula's tokens are bounded at 16 KB, its - nesting at 256 levels and an array constant at 65,536 values. Fixed caps: 1024 sheets, 100,000 merged ranges per sheet, 64 nested - extension blocks. + nesting at 256 levels and its array constants at 65,536 values in all, each charged before + it is held; once the formula text allowance is spent, no formula is decoded. Fixed caps: 1024 + sheets, 100,000 merged ranges per sheet, 64 nested extension blocks. - Cells and rows outside the 1,048,576 × 16,384 grid are dropped and counted. SpreadsheetML is read leniently: a damaged optional part is a warning, a damaged worksheet keeps diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index 65aab0eb8..ea37f59ca 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -523,6 +523,11 @@ fn decode_formula( warnings: &mut Warnings, ) -> Option { let links = links?; + // Once the allowance is spent no formula fits it, so none is decoded only to be dropped. + if !budget.formula_text_fits(1, limits) { + kept(warnings, "more formula text than the limits allow"); + return None; + } let text = match formula::decode(rgce, rgcb, links, mode) { Ok(e) => expr::print(&e), Err(formula::Unsupported(why)) => { diff --git a/crates/xlsx/src/xlsb/formula.rs b/crates/xlsx/src/xlsb/formula.rs index 3a6960b39..b62072ad2 100644 --- a/crates/xlsx/src/xlsb/formula.rs +++ b/crates/xlsx/src/xlsb/formula.rs @@ -18,7 +18,8 @@ use super::ftab::function; /// Formula token bytes decoded at most (a cell formula holds at most 16,384). pub(super) const MAX_RGCE: usize = 16 * 1024; -/// Values in one array constant at most. +/// Values in one formula's array constants at most, all of them together: each array is charged +/// before its values are held, so a formula of many arrays can't hold more than this. const MAX_ARRAY: usize = 65_536; /// Why a formula could not be turned back into text. @@ -301,6 +302,7 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res let mut t = Bytes { b: rgce, pos: 0 }; let mut extra = Bytes { b: rgcb, pos: 0 }; let mut stack: Stack = Vec::new(); + let mut array_values = MAX_ARRAY; let mut ws = Spacing::default(); while t.pos < rgce.len() { let ptg = t.u8()?; @@ -414,7 +416,7 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res 0x20 => { // PtgArray: 14 unused bytes (Excel leaves junk in them); the values are in rgcb. t.take(14)?; - push(&mut stack, kids, array_constant(&mut extra)?)?; + push(&mut stack, kids, array_constant(&mut extra, &mut array_values)?)?; } 0x21 => { let iftab = t.u16()?; @@ -663,12 +665,15 @@ fn grouped(e: Expr, in_args: bool) -> Expr { /// PtgExtraArray ([MS-XLSB] 2.5.98.41): row and column counts, then `SerAr` values in row-major /// order. The type codes are not BIFF8's: 0x00 is a number here, not a missing value. -fn array_constant(b: &mut Bytes<'_>) -> Result { +fn array_constant(b: &mut Bytes<'_>, left: &mut usize) -> Result { let rows = usize::try_from(b.u32()?).map_err(|_| TRUNC)?; let cols = usize::try_from(b.u32()?).map_err(|_| TRUNC)?; - if rows == 0 || cols == 0 || rows.saturating_mul(cols) > MAX_ARRAY { - return Err(Unsupported("array constant too large or empty")); + let values = rows.saturating_mul(cols); + if values == 0 { + return Err(Unsupported("empty array constant")); } + // Charged before the rows are allocated, against what the formula's arrays may hold in all. + *left = left.checked_sub(values).ok_or(Unsupported("array constants too large"))?; let mut out = Vec::with_capacity(rows); for _ in 0..rows { let mut row = Vec::with_capacity(cols); diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index 950dc8847..94bb51ba6 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -1035,3 +1035,30 @@ fn references_to_macro_sheets_and_spans_over_chart_sheets_are_named() { assert_eq!(got, [Some("Macro1!$A$1"), Some("Data:Hidden!$A$1"), None]); assert!(warned(&wb, "references to sheets with no cells"), "{:?}", wb.warnings); } + +#[test] +fn a_formulas_array_constants_share_one_allowance() { + // Two arrays of 40,000 Boolean values, each within what one array may hold, added together: + // charged before their values are held, together they pass what a formula's arrays may hold + // (65,536 values), so the cell keeps its saved result. One of them alone decodes. + let array = |n: u32| [&n.to_le_bytes()[..], &1u32.to_le_bytes(), &[2u8, 1].repeat(n as usize)].concat(); + let ptg = [&[0x60u8][..], &[0; 14]].concat(); + let two = [&ptg[..], &ptg, &[0x03]].concat(); + let data = [row(0), formula_cell(0, &two, &[array(40_000), array(40_000)].concat()), formula_cell(1, &ptg, &array(40_000))].concat(); + let wb = read(&package(&book_with_links(&xti()), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES)).unwrap(); + assert_eq!(wb.sheets[0].formula(CellRef::new(0, 0)), None); + assert_eq!(wb.sheets[0].value(CellRef::new(0, 0)), Value::Number(0.0)); + assert!(wb.sheets[0].formula(CellRef::new(0, 1)).is_some_and(|f| f.starts_with("{TRUE;TRUE"))); + assert!(warned(&wb, "array constants too large"), "{:?}", wb.warnings); +} + +#[test] +fn a_spent_formula_allowance_skips_decoding() { + // With no allowance, a formula is not decoded at all: an unknown token is never reached. + let data = [row(0), formula_cell(0, &[0xFF], &[])].concat(); + let limits = Limits { binary_formula_text: 0, ..Limits::default() }; + let wb = read_with(&package(&book_with_links(&xti()), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES), &limits).unwrap(); + assert_eq!(wb.sheets[0].formula(CellRef::new(0, 0)), None); + assert!(warned(&wb, "more formula text than the limits allow"), "{:?}", wb.warnings); + assert!(!warned(&wb, "unknown formula token"), "{:?}", wb.warnings); +} From 88287a6b46e4b7a5e8fab18d07c6ca71791803e9 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Sat, 14 Nov 2026 12:40:55 -0500 Subject: [PATCH 13/14] craft-xlsx: review fixes: styles caps are too large, differential format strings are charged - A styles table past 65,536 entries, or a gradient past 1,024 stops, is past a fixed cap: Error::TooLarge, as the error's contract and the other caps have it, not a damaged part that is skipped with a warning. - A differential format's font name and number format are charged to binary_text, as the other string-bearing style records are. --- crates/xlsx/README.md | 31 ++++++++++++++++--------------- crates/xlsx/src/xlsb/styles.rs | 15 +++++++++++---- crates/xlsx/tests/xlsb.rs | 18 ++++++++++++++---- 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/crates/xlsx/README.md b/crates/xlsx/README.md index 088e82e3e..9d8e1b785 100644 --- a/crates/xlsx/README.md +++ b/crates/xlsx/README.md @@ -83,21 +83,22 @@ for new pictures (`vm = i + 1`) and the `XLDAPR` dynamic-array metadata (`cm = 1 The parts this crate writes use the transitional namespaces; a strict file's kept parts stay as they were. A binary workbook is not written. -A binary workbook gives the same `Workbook`, strings, rows and merged cells, and the same -`Styles`: number formats, fonts, fills, borders, cell and cell style formats, cell styles, -differential formats and the indexed colours, as Excel writes them in `styles.xml` (default fill -colours and empty settings left out, a cell style format's `apply*="0"`, a number format with a -newer locale tag in its Excel 2007 spelling, as `formatCode` has it). Custom table styles and -recent colours are not read; a damaged styles part is a warning and no styles. Its cell formulas -are decoded from their tokens into the text an `.xlsx` file holds (`_xlfn.` and `_xlpm.` names as -written, apart from the four ISO/IEC 29500 functions `.xlsx` names bare, no implicit `@`, the -spaces and line breaks typed in the formula, numbers as Excel spells them), with -`FormulaKind::Shared` or `FormulaKind::Array` and their ranges, and `BrtCellMeta` as the cell -metadata index. Every cell of a shared formula gets its own text, decoded at the cell, since the -file keeps the tokens once. A formula whose decoding needs something the reader does not read -keeps only its saved result, with a warning saying why: user defined names, table references, -other workbooks, Python code stored outside the cell, data tables. The XLSB function table -(`xlsb/ftab.rs`) and the expression printer are this crate's own, apart from `craft-xls`'s. +A binary workbook gives the same `Workbook`, strings, rows and merged cells, and the same `Styles`: +number formats, fonts, fills, borders, cell and cell style formats, cell styles, differential +formats and the indexed colours, as Excel writes them in `styles.xml` (default fill colours and +empty settings left out, a cell style format's `apply*="0"`, a number format with a newer locale tag +in its Excel 2007 spelling, as `formatCode` has it). Custom table styles and recent colours are not +read; a damaged styles part is a warning and no styles, while one past a cap (65,536 entries in a +table, 1,024 gradient stops) or past `binary_text` is too large. Its cell formulas are decoded from +their tokens into the text an `.xlsx` file holds (`_xlfn.` and `_xlpm.` names as written, apart from +the four ISO/IEC 29500 functions `.xlsx` names bare, no implicit `@`, the spaces and line breaks +typed in the formula, numbers as Excel spells them), with `FormulaKind::Shared` or +`FormulaKind::Array` and their ranges, and `BrtCellMeta` as the cell metadata index. Every cell of a +shared formula gets its own text, decoded at the cell, since the file keeps the tokens once. A +formula whose decoding needs something the reader does not read keeps only its saved result, with a +warning saying why: user defined names, table references, other workbooks, Python code stored +outside the cell, data tables. The XLSB function table (`xlsb/ftab.rs`) and the expression printer +are this crate's own, apart from `craft-xls`'s. ## Hostile input diff --git a/crates/xlsx/src/xlsb/styles.rs b/crates/xlsx/src/xlsb/styles.rs index 50673ef28..88c6bee51 100644 --- a/crates/xlsx/src/xlsb/styles.rs +++ b/crates/xlsx/src/xlsb/styles.rs @@ -176,7 +176,7 @@ fn fill(b: &mut Bytes<'_>) -> Result { let count = b.u32()?; if fls == 0x28 { if count > MAX_STOPS { - return Err(invalid("too many gradient stops")); + return Err(Error::TooLarge(format!("more than {MAX_STOPS} gradient stops"))); } for _ in 0..count { let c = color(b)?.unwrap_or_default(); @@ -330,7 +330,7 @@ fn dxf(b: &mut Bytes<'_>) -> Result { 0x04 => { let g = gradient_fill.as_mut().ok_or_else(|| invalid("gradient stop without a gradient"))?; if g.stops.len() >= MAX_STOPS as usize { - return Err(invalid("too many gradient stops")); + return Err(Error::TooLarge(format!("more than {MAX_STOPS} gradient stops"))); } p.u16()?; let position = p.f64()?; @@ -418,7 +418,7 @@ pub(crate) fn styles(bytes: &[u8], limits: &Limits, budget: &Budget, warnings: & _ => 0, }; if entries >= MAX_ENTRIES { - return Err(invalid(format!("more than {MAX_ENTRIES} entries in a styles table"))); + return Err(Error::TooLarge(format!("more than {MAX_ENTRIES} entries in a styles table"))); } match id { 279 => { @@ -451,7 +451,14 @@ pub(crate) fn styles(bytes: &[u8], limits: &Limits, budget: &Budget, warnings: & budget.text(c.name.len(), limits)?; s.cell_styles.push(c); } - 507 => s.dxfs.push(dxf(&mut b)?), + 507 => { + let d = dxf(&mut b)?; + // What it keeps of its strings, after a later property has replaced an earlier. + let font = d.font.as_ref().and_then(|f| f.name.as_ref()).map_or(0, String::len); + let format = d.num_fmt.as_ref().and_then(|n| n.code.as_ref()).map_or(0, String::len); + budget.text(font + format, limits)?; + s.dxfs.push(d); + } // BrtBeginIndexedColors, BrtEndIndexedColors. 565 => palette = true, 566 => palette = false, diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index bd8b36176..b72705b9f 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -1196,11 +1196,11 @@ fn damaged_styles_are_a_warning_and_the_workbook_opens() { assert!(warnings.iter().any(|w| w.contains("look/styles.bin")), "{why}: {warnings:?}"); assert_eq!(read(&bytes).unwrap().sheets[0].value(CellRef::new(0, 0)), Value::Number(1.0), "{why}"); } - // A table past 65,536 entries is refused before it is held. + // A table past 65,536 entries is past a fixed cap: refused before it is held, as too large. let xfs = brt_xf(0, 0, 0, 0, 0, 0, 0, 0x1010, 0).repeat(65_537); - let (styles, warnings) = styles_of(&styled_file(&with(&[&begin(617, 65_537)[..], &xfs, &record(618, &[])].concat()), STYLES_TYPES)); - assert!(styles.is_none()); - assert!(warnings.iter().any(|w| w.contains("65536 entries")), "{warnings:?}"); + let bytes = styled_file(&with(&[&begin(617, 65_537)[..], &xfs, &record(618, &[])].concat()), STYLES_TYPES); + let mut p = Package::open(&bytes, &Limits::default()).unwrap(); + assert!(matches!(p.styles(), Err(Error::TooLarge(m)) if m.contains("65536 entries"))); // Custom table styles are not read, and say so. let table_styles = [body, &record(508, &[&1u32.to_le_bytes()[..], &wide("Probe Table"), &wide("")].concat()), &end].concat(); let (styles, warnings) = styles_of(&styled_file(&table_styles, STYLES_TYPES)); @@ -1339,3 +1339,13 @@ fn a_spent_formula_allowance_skips_decoding() { assert!(warned(&wb, "more formula text than the limits allow"), "{:?}", wb.warnings); assert!(!warned(&wb, "unknown formula token"), "{:?}", wb.warnings); } + +#[test] +fn differential_format_strings_are_charged_to_the_text_allowance() { + // A DXF's font name and number format are text the styles part keeps, charged like a font's. + let dxf = |name: &str| [record(278, &[]), begin(505, 1), brt_dxf(&[prop(0x18, &short(name))]), record(506, &[]), record(279, &[])].concat(); + let limits = Limits { binary_text: 100, ..Limits::default() }; + let open = |part: &[u8]| Package::open(&styled_file(part, STYLES_TYPES), &limits).unwrap().styles(); + assert!(open(&dxf("Arial")).is_ok_and(|s| s.is_some_and(|s| s.dxfs.len() == 1))); + assert!(matches!(open(&dxf(&"x".repeat(200))), Err(Error::TooLarge(_)))); +} From 24e626611985746bebbbdd9c252b8ee705701efd Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Sat, 14 Nov 2026 13:27:25 -0500 Subject: [PATCH 14/14] craft-xlsx: review fixes: spaced string chains flatten, module sheets and supporting links are capped - literal_texts now accepts a spaced operand as literal_text does, and the chain flattens with the spacing kept on its strings, so a chain the Python check accepts is written flat ("x"& "="&"1", not "x"&( "="&"1")). - Every BrtBundleSh counts toward the 1,024-sheet cap, a skipped module sheet too, since each keeps a place in the sheet links. - Supporting links are capped at 65,536; past it the links aren't read and formulas keep their saved results. --- crates/xlsx/src/xlsb.rs | 12 +++++++++++- crates/xlsx/src/xlsb/formula.rs | 30 ++++++++++++++++++++++++++++-- crates/xlsx/tests/xlsb.rs | 18 ++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index ea37f59ca..b3f94b5fa 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -26,6 +26,9 @@ pub(crate) const WORKSHEET: &str = "application/vnd.ms-excel.worksheet"; pub(crate) const STRINGS: &str = "application/vnd.ms-excel.sharedStrings"; /// Most sheets in the workbook. const MAX_SHEETS: usize = 1024; +/// Supporting links (this workbook, add-ins, other workbooks) read at most; past it, formulas +/// keep their saved results. +const MAX_BOOKS: usize = 65_536; /// Most merged ranges in one worksheet. const MAX_MERGES: usize = 100_000; /// Defined names (Excel's hidden ones for newer functions included) read at most; past it, the @@ -268,6 +271,9 @@ pub(crate) fn workbook( let mut links = Some(Links::default()); let mut bundle = false; let mut bundle_seen = false; + // Every BrtBundleSh counts toward the cap, a skipped module sheet too: each keeps a place + // in the sheet links. + let mut bundle_count = 0usize; while let Some((id, payload)) = records.next(warnings)? { let mut b = Bytes::new(payload); match id { @@ -316,9 +322,10 @@ pub(crate) fn workbook( if !bundle { return Err(invalid("worksheet outside the worksheet list")); } - if wb.sheets.len() >= MAX_SHEETS { + if bundle_count >= MAX_SHEETS { return Err(Error::TooLarge(format!("XLSB exceeds {MAX_SHEETS} sheets"))); } + bundle_count += 1; let visibility = match b.u32()? { 0 => Visibility::Visible, 1 => Visibility::Hidden, @@ -368,6 +375,9 @@ pub(crate) fn workbook( /// (`BrtExternSheet`) or a defined name (`BrtName`), added to `links`. fn link_record(id: u16, payload: &[u8], links: &mut Links) -> Result<(), Error> { let mut b = Bytes::new(payload); + if matches!(id, 355 | 357 | 358 | 667) && links.books.len() >= MAX_BOOKS { + return Err(invalid("too many supporting links")); + } match id { 357 | 358 | 667 => { b.finish()?; diff --git a/crates/xlsx/src/xlsb/formula.rs b/crates/xlsx/src/xlsb/formula.rs index b62072ad2..a7293c0df 100644 --- a/crates/xlsx/src/xlsb/formula.rs +++ b/crates/xlsx/src/xlsb/formula.rs @@ -583,10 +583,13 @@ fn literal_text(e: &Expr) -> bool { } } -/// How many strings a [`literal_text`] chain joins, or `None` for anything else. +/// How many strings a [`literal_text`] chain joins, or `None` for anything else. It accepts +/// what [`literal_text`] does, typed spacing included, so a chain the Python check accepts is +/// one that is written flat. fn literal_texts(e: &Expr) -> Option { match e { Expr::Text(_) => Some(1), + Expr::Spaced(_, x, _) => literal_texts(x), Expr::Binary(BinOp::Concat, a, b) => literal_texts(a)?.checked_add(literal_texts(b)?), _ => None, } @@ -605,13 +608,24 @@ fn binding(e: &Expr) -> u8 { } } -/// The strings of a [`literal_text`] chain, left to right. +/// The strings of a [`literal_text`] chain, left to right. Spacing typed around a part of the +/// chain stays with that part's first and last strings, where it is written. fn flatten_texts(e: Expr, out: &mut Vec) { match e { Expr::Binary(BinOp::Concat, a, b) => { flatten_texts(*a, out); flatten_texts(*b, out); } + Expr::Spaced(before, x, after) if matches!(*x, Expr::Binary(BinOp::Concat, ..)) => { + let start = out.len(); + flatten_texts(*x, out); + if let Some(first) = out.get_mut(start) { + *first = spaced_before(std::mem::replace(first, Expr::Missing), before); + } + if let Some(last) = out.last_mut() { + *last = spaced_after(std::mem::replace(last, Expr::Missing), after); + } + } text => out.push(text), } } @@ -716,6 +730,18 @@ mod tests { } } + #[test] + fn a_spaced_part_of_a_string_chain_is_written_flat() { + // `"x"`, then `"="` typed with a space before it joined to `"1"`, then joined: the tokens + // nest to the right, and the space is on the inner chain. Written flat, as Python code + // must be, with the space where it was typed. + let s = |c: u8| [0x17, 1, 0, c, 0]; + let rgce = [&s(b'x')[..], &[0x19, 0x40, 0, 1], &s(b'=')[..], &s(b'1')[..], &[0x08, 0x08]].concat(); + let e = decode(&rgce, &[], &Links::default(), Mode::Cell).unwrap(); + assert_eq!(super::super::expr::print(&e), "\"x\"& \"=\"&\"1\""); + assert!(literal_text(&e) && literal_texts(&e) == Some(3)); + } + #[test] fn a_long_chain_of_strings_stays_within_the_depth_bound() { // 4,096 strings and 4,095 `&` fill 16,383 token bytes, 13 levels deep as written. Made diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index 94bb51ba6..748d1b86d 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -1062,3 +1062,21 @@ fn a_spent_formula_allowance_skips_decoding() { assert!(warned(&wb, "more formula text than the limits allow"), "{:?}", wb.warnings); assert!(!warned(&wb, "unknown formula token"), "{:?}", wb.warnings); } + +#[test] +fn module_sheets_and_supporting_links_are_bounded() { + // 1,025 very hidden module sheets (no relationship) before the worksheets: each keeps a place + // in the sheet links, so together they are past the 1,024-sheet cap. + let plain = book(false); + let at = plain.windows(3).position(|w| w == record(143, &[]).as_slice()).unwrap() + 3; + let module = record(156, &[&2u32.to_le_bytes()[..], &9u32.to_le_bytes(), &u32::MAX.to_le_bytes(), &wide("Module")].concat()); + let modules = [&plain[..at], &module.repeat(1025), &plain[at..]].concat(); + assert!(matches!(read(&package(&modules, &worksheet(&[], &[]), &sst(), ROOT, RELS, TYPES)), Err(Error::TooLarge(_)))); + // 65,537 supporting links: past the cap the links aren't read, and formulas keep their values. + let end = record(132, &[]); + let selves = [&plain[..plain.len() - end.len()], &record(353, &[]), &record(357, &[]).repeat(65_537), &record(354, &[]), &end].concat(); + let data = [row(0), formula_cell(0, &[0x1E, 1, 0], &[])].concat(); + let wb = read(&package(&selves, &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES)).unwrap(); + assert_eq!(wb.sheets[0].formula(CellRef::new(0, 0)), None); + assert!(wb.warnings.iter().any(|w| w.contains("sheet links or names could not be read")), "{:?}", wb.warnings); +}