rxls 0.1.1

Native Rust spreadsheet library: reads .xls (BIFF8/5/7), .xlsx, .xlsb, .ods and writes .xlsx. Typed cells, formulas, panic-free, no JVM/POI.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
//! `.xlsb` (BIFF12 / Excel binary workbook) reading.
//!
//! A `.xlsb` is a ZIP of **binary** parts — `xl/workbook.bin`, `xl/sharedStrings.bin`,
//! `xl/styles.bin`, `xl/worksheets/sheetN.bin` — the same package shape as `.xlsx`
//! but with BIFF12 record streams instead of XML. A record is
//! `[recordType: var-uint][recordSize: var-uint][payload]`. This module decodes the
//! shared strings, number formats (for date detection), merged ranges, and the
//! common cell records into the shared [`Cell`] model, reusing the `.xlsx` ZIP
//! plumbing and the [`crate::format`] classifier. Panic-free / bounds-checked.
//!
//! Reference: [MS-XLSB]. Formula token decompilation is best-effort (cached values
//! are still read); cell `.bin` records beyond the common value kinds are skipped.

use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::io::Read;

use crate::error::{Error, Result};
use crate::{
    format, rk_to_f64, Cell, CellEntry, Color, Comment, DataValidation, DvKind, DvOp, PageSetup,
    ProtectionOptions, Sheet, SheetType, Table, Workbook,
};

// BIFF12 record type ids ([MS-XLSB] 2.4).
const BRT_ROW_HDR: u32 = 0;
const BRT_CELL_RK: u32 = 2;
const BRT_CELL_ERROR: u32 = 3;
const BRT_CELL_BOOL: u32 = 4;
const BRT_CELL_REAL: u32 = 5;
const BRT_CELL_ST: u32 = 6;
const BRT_CELL_ISST: u32 = 7;
const BRT_FMLA_STRING: u32 = 8;
const BRT_FMLA_NUM: u32 = 9;
const BRT_FMLA_BOOL: u32 = 10;
const BRT_FMLA_ERROR: u32 = 11;
const BRT_SST_ITEM: u32 = 19;
const BRT_NAME: u32 = 39;
const BRT_FMT: u32 = 44;
const BRT_XF: u32 = 47;
const BRT_COL_INFO: u32 = 60;
const BRT_DVAL: u32 = 64;
const BRT_BEGIN_WS_VIEW: u32 = 137;
const BRT_END_WS_VIEW: u32 = 138;
const BRT_WS_PROP: u32 = 147;
const BRT_PANE: u32 = 151;
const BRT_BEGIN_AFILTER: u32 = 161;
const BRT_BUNDLE_SH: u32 = 156;
const BRT_BOOK_VIEW: u32 = 158;
const BRT_WB_PROP: u32 = 153; // 0x99 — workbook properties, carries the 1904 flag
const BRT_MERGE_CELL: u32 = 176;
const BRT_BEGIN_LIST: u32 = 288;
const BRT_BEGIN_LIST_COL: u32 = 291;
const BRT_MARGINS: u32 = 475;
const BRT_PRINT_OPTIONS: u32 = 476;
const BRT_PAGE_SETUP: u32 = 477;
const BRT_BEGIN_HEADER_FOOTER: u32 = 478;
const BRT_HLINK: u32 = 0x01EE;
const BRT_BOOK_PROTECTION: u32 = 534;
const BRT_SHEET_PROTECTION: u32 = 535;
const BRT_LIST_PART: u32 = 550;
const BRT_COMMENT_AUTHOR: u32 = 632;
const BRT_BEGIN_COMMENT: u32 = 635;
const BRT_END_COMMENT: u32 = 636;
const BRT_COMMENT_TEXT: u32 = 637;
const BRT_TABLE_STYLE_CLIENT: u32 = 649;
const BRT_DVAL_LIST: u32 = 681;
const BRT_BEGIN_CELL_XFS: u32 = 0x0269;
const BRT_END_CELL_XFS: u32 = 0x026A;
const MAX_DVAL_RANGES: usize = 8192;
const MAX_TABLE_COLUMNS: usize = 16_384;
const MAX_XLSB_COL_INDEX: u32 = 16_383;
const MAX_XLSB_ROW_INDEX: u32 = 1_048_575;

/// A cursor over one BIFF12 record stream that yields `(record_type, payload)`,
/// bounded by the buffer — a hostile size never reads past the end.
struct RecReader<'a> {
    b: &'a [u8],
    pos: usize,
}

impl<'a> RecReader<'a> {
    fn new(b: &'a [u8]) -> Self {
        RecReader { b, pos: 0 }
    }
    /// Read a variable-width unsigned int of up to `max_bytes` (7 bits each, high
    /// bit = continue).
    fn var(&mut self, max_bytes: usize) -> Option<u32> {
        let mut val: u32 = 0;
        for i in 0..max_bytes {
            let byte = *self.b.get(self.pos)?;
            self.pos += 1;
            val |= u32::from(byte & 0x7F) << (7 * i);
            if byte & 0x80 == 0 {
                break;
            }
        }
        Some(val)
    }
    /// Next record as `(type, payload)`, or `None` at end / on truncation.
    fn next(&mut self) -> Option<(u32, &'a [u8])> {
        if self.pos >= self.b.len() {
            return None;
        }
        let rt = self.var(2)?;
        let sz = self.var(4)? as usize;
        let start = self.pos;
        let end = start.checked_add(sz)?;
        if end > self.b.len() {
            return None;
        }
        self.pos = end;
        Some((rt, &self.b[start..end]))
    }
}

fn u16le(b: &[u8], o: usize) -> Option<u16> {
    b.get(o..o + 2).map(|s| u16::from_le_bytes([s[0], s[1]]))
}
fn u32le(b: &[u8], o: usize) -> Option<u32> {
    b.get(o..o + 4)
        .map(|s| u32::from_le_bytes([s[0], s[1], s[2], s[3]]))
}
fn i32le(b: &[u8], o: usize) -> Option<i32> {
    b.get(o..o + 4)
        .map(|s| i32::from_le_bytes([s[0], s[1], s[2], s[3]]))
}
fn f64le(b: &[u8], o: usize) -> Option<f64> {
    b.get(o..o + 8)
        .and_then(|s| s.try_into().ok())
        .map(f64::from_le_bytes)
}

/// An `XLWideString`: `cch: u32` then `cch` UTF-16LE code units. Returns the
/// string and the byte length consumed.
fn wide_string(b: &[u8], o: usize) -> Option<(String, usize)> {
    let cch = u32le(b, o)? as usize;
    let bytes = cch.checked_mul(2)?;
    let chars = b.get(o + 4..o + 4 + bytes)?;
    let units: Vec<u16> = chars
        .chunks_exact(2)
        .map(|c| u16::from_le_bytes([c[0], c[1]]))
        .collect();
    Some((String::from_utf16_lossy(&units), 4 + bytes))
}

/// Detect `.xlsb` by the presence of `xl/workbook.bin` in the ZIP.
pub(crate) fn is_xlsb(bytes: &[u8]) -> bool {
    zip::ZipArchive::new(std::io::Cursor::new(bytes))
        .map(|mut z| part_index(&mut z, "xl/workbook.bin").is_some())
        .unwrap_or(false)
}

fn part(zip: &mut zip::ZipArchive<std::io::Cursor<&[u8]>>, name: &str) -> Option<Vec<u8>> {
    const MAX_PART: u64 = 256 << 20;
    let idx = part_index(zip, name)?;
    let f = zip.by_index(idx).ok()?;
    let mut v = Vec::new();
    f.take(MAX_PART).read_to_end(&mut v).ok()?;
    Some(v)
}

fn part_index(zip: &mut zip::ZipArchive<std::io::Cursor<&[u8]>>, name: &str) -> Option<usize> {
    if let Some(idx) = zip.index_for_name(name) {
        return Some(idx);
    }
    let wanted = canonical_part_name(name);
    for idx in 0..zip.len() {
        let Ok(file) = zip.by_index(idx) else {
            continue;
        };
        if canonical_part_name(file.name()).eq_ignore_ascii_case(&wanted) {
            return Some(idx);
        }
    }
    None
}

fn canonical_part_name(name: &str) -> String {
    name.replace('\\', "/").trim_start_matches('/').to_string()
}

pub(crate) fn open(bytes: &[u8]) -> Result<Workbook> {
    let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes))
        .map_err(|_| Error::Zip("not a valid .xlsb ZIP container"))?;

    let shared = part(&mut zip, "xl/sharedStrings.bin")
        .map(|b| parse_shared_strings(&b))
        .unwrap_or_default();
    let styles = part(&mut zip, "xl/styles.bin")
        .map(|b| parse_styles(&b))
        .unwrap_or_default();
    let properties = crate::xlsx::parse_doc_properties(
        crate::xlsx::part_str(&mut zip, "docProps/core.xml").as_deref(),
        crate::xlsx::part_str(&mut zip, "docProps/app.xml").as_deref(),
    );
    let workbook_rels_xml =
        crate::xlsx::part_str(&mut zip, "xl/_rels/workbook.bin.rels").unwrap_or_default();
    let rels = crate::xlsx::parse_rels(&workbook_rels_xml);
    let rel_types = crate::xlsx::parse_rel_types(&workbook_rels_xml);
    let (names, date1904, active_sheet, defined_names, protect_structure, sheet_builtin_names) =
        match part(&mut zip, "xl/workbook.bin") {
            Some(b) => parse_workbook(&b),
            None => return Err(Error::MissingWorkbook),
        };

    let mut budget = crate::MAX_TEXT_BYTES;
    let mut sheets = Vec::with_capacity(names.len().min(1 << 16));
    let mut selected_sheet_fallback = None;
    for (sheet_index, (name, rid, hs_state)) in names.into_iter().enumerate() {
        let sheet_type = xlsb_sheet_type(&rid, hs_state, rel_types.get(&rid).map(String::as_str));
        let is_worksheet = sheet_type == SheetType::WorkSheet;
        let target = rels.get(&rid).cloned().unwrap_or_default();
        let path = normalize_target(&target);
        let sheet_rels_xml = if is_worksheet {
            crate::xlsx::part_str(&mut zip, &sheet_rels_path(&path)).unwrap_or_default()
        } else {
            String::new()
        };
        let sheet_rels = if is_worksheet && !sheet_rels_xml.is_empty() {
            crate::xlsx::parse_rels(&sheet_rels_xml)
        } else {
            HashMap::new()
        };
        let comments = if is_worksheet {
            parse_sheet_comments(&mut zip, &path, &sheet_rels, &sheet_rels_xml)
        } else {
            Vec::new()
        };
        let (cells, merges, read_hyperlinks, metadata) = if is_worksheet {
            part(&mut zip, &path)
                .map(|b| parse_sheet(&b, &shared, &styles, date1904, &sheet_rels, &mut budget))
                .unwrap_or_default()
        } else {
            (
                Vec::new(),
                Vec::new(),
                Vec::new(),
                SheetReadMetadata::default(),
            )
        };
        if is_worksheet && metadata.selected && selected_sheet_fallback.is_none() {
            selected_sheet_fallback = Some(sheet_index);
        }
        let tables = if is_worksheet {
            parse_sheet_tables(
                &mut zip,
                &path,
                &sheet_rels,
                &sheet_rels_xml,
                &metadata.table_rel_ids,
            )
        } else {
            Vec::new()
        };
        sheets.push(Sheet {
            name,
            is_worksheet,
            sheet_type: Some(sheet_type),
            cells,
            read_merges: merges,
            read_hyperlinks,
            comments,
            tables,
            freeze: metadata.freeze,
            autofilter: metadata.autofilter,
            data_validations: metadata.data_validations,
            page_setup: metadata.page_setup,
            tab_color: metadata.tab_color,
            print_gridlines: metadata.print_gridlines,
            print_headings: metadata.print_headings,
            hide_gridlines: metadata.hide_gridlines,
            zoom: metadata.zoom,
            show_headers: metadata.show_headers,
            right_to_left: metadata.right_to_left,
            protect: metadata.protect,
            protect_options: metadata.protect_options,
            row_outline: metadata.row_outline,
            col_outline: metadata.col_outline,
            outline_summary_below: metadata.outline_summary_below.unwrap_or(true),
            outline_summary_right: metadata.outline_summary_right.unwrap_or(true),
            collapsed_rows: metadata.collapsed_rows,
            // hsState: 0 = visible, 1 = hidden, 2 = veryHidden ([MS-XLSB] 2.4.301).
            hidden: hs_state == 1,
            very_hidden: hs_state == 2,
            ..Default::default()
        });
    }
    apply_xlsb_sheet_builtin_names(&mut sheets, sheet_builtin_names);
    Ok(Workbook {
        sheets,
        properties,
        defined_names,
        date1904,
        active_sheet: active_sheet.or(selected_sheet_fallback).unwrap_or_default(),
        text_truncated: budget == 0,
        protect_structure,
        ..Default::default()
    })
}

fn xlsb_sheet_type(rid: &str, hs_state: u32, rel_type: Option<&str>) -> SheetType {
    if rid.is_empty() && hs_state == 2 {
        return SheetType::Vba;
    }
    let kind = rel_type
        .and_then(|ty| ty.rsplit('/').next())
        .unwrap_or("worksheet");
    match kind.to_ascii_lowercase().as_str() {
        "chartsheet" => SheetType::ChartSheet,
        "dialogsheet" => SheetType::DialogSheet,
        "macrosheet" | "xlmacrosheet" | "xlintlmacrosheet" => SheetType::MacroSheet,
        _ => SheetType::WorkSheet,
    }
}

fn normalize_target(target: &str) -> String {
    let t = target.trim_start_matches('/');
    if t.starts_with("xl/") {
        t.to_string()
    } else {
        format!("xl/{t}")
    }
}

fn sheet_rels_path(path: &str) -> String {
    match path.rsplit_once('/') {
        Some((dir, file)) => format!("{dir}/_rels/{file}.rels"),
        None => format!("_rels/{path}.rels"),
    }
}

fn normalize_part_target(base: &str, target: &str) -> String {
    if let Some(abs) = target.strip_prefix('/') {
        return abs.to_string();
    }
    let mut dir: Vec<&str> = match base.rfind('/') {
        Some(i) => base[..i].split('/').collect(),
        None => Vec::new(),
    };
    for seg in target.split('/') {
        match seg {
            "" | "." => {}
            ".." => {
                dir.pop();
            }
            other => dir.push(other),
        }
    }
    dir.join("/")
}

/// Number formats from `styles.bin` (for date detection), shared with `.xlsx`.
#[derive(Default)]
struct Styles {
    xf_numfmt: Vec<u16>,
    custom: HashMap<u16, String>,
}

impl Styles {
    fn kind(&self, style_idx: usize) -> format::Kind {
        let id = self.xf_numfmt.get(style_idx).copied().unwrap_or(0);
        format::classify(id, self.custom.get(&id).map(String::as_str))
    }
}

fn parse_styles(b: &[u8]) -> Styles {
    let mut s = Styles::default();
    let mut r = RecReader::new(b);
    let mut in_cell_xfs = false;
    while let Some((rt, p)) = r.next() {
        match rt {
            BRT_FMT => {
                // ifmt:u16, stFmtCode: XLWideString.
                if let (Some(ifmt), Some((code, _))) = (u16le(p, 0), wide_string(p, 2)) {
                    s.custom.insert(ifmt, code);
                }
            }
            BRT_BEGIN_CELL_XFS => {
                in_cell_xfs = true;
            }
            BRT_END_CELL_XFS => {
                in_cell_xfs = false;
            }
            BRT_XF if in_cell_xfs => {
                // BrtXF: ixfeParent:u16, iFmt:u16, ...
                if let Some(ifmt) = u16le(p, 2) {
                    s.xf_numfmt.push(ifmt);
                }
            }
            _ => {}
        }
    }
    s
}

fn parse_shared_strings(b: &[u8]) -> Vec<String> {
    let mut out = Vec::new();
    let mut r = RecReader::new(b);
    while let Some((rt, p)) = r.next() {
        if rt == BRT_SST_ITEM {
            // flags:u8, then XLWideString (rich-run/phonetic data after is ignored).
            if let Some((s, _)) = wide_string(p, 1) {
                out.push(s);
            } else {
                out.push(String::new());
            }
        }
    }
    out
}

/// Returns the `(sheet name, rel id, hsState)` triples, whether the workbook
/// uses the 1904 date system (`BrtWbProp` bit 0, matching calamine and
/// `[MS-XLSB]`), the active sheet index from `BrtBookView.itabCur`, workbook
/// defined names, workbook structure protection from `BrtBookProtection`, and
/// selected sheet-local built-in names used by sheet metadata facades.
/// `hsState` is the sheet visibility (0 = visible, 1 = hidden, 2 = veryHidden).
fn parse_workbook(
    b: &[u8],
) -> (
    WorkbookSheets,
    bool,
    Option<usize>,
    DefinedNames,
    bool,
    Vec<SheetBuiltinName>,
) {
    let mut out = Vec::new();
    let mut defined_names = Vec::new();
    let mut sheet_builtin_names = Vec::new();
    let mut date1904 = false;
    let mut active_sheet = None;
    let mut protect_structure = false;
    let mut r = RecReader::new(b);
    while let Some((rt, p)) = r.next() {
        if rt == BRT_BUNDLE_SH {
            // hsState:u32, iTabID:u32, strRelID: XLNullableWideString, strName: XLWideString.
            let hs_state = u32le(p, 0).unwrap_or(0);
            let Some((rid, used)) = nullable_wide(p, 8) else {
                continue;
            };
            if let Some((name, _)) = wide_string(p, 8 + used) {
                out.push((name, rid, hs_state));
            }
        } else if rt == BRT_WB_PROP {
            date1904 = p.first().is_some_and(|byte| byte & 0x1 != 0);
        } else if rt == BRT_BOOK_VIEW && active_sheet.is_none() {
            active_sheet = u32le(p, 24).and_then(|index| usize::try_from(index).ok());
        } else if rt == BRT_NAME {
            match parse_brt_name(p) {
                Some(ParsedBrtName::GlobalUser(name)) => defined_names.push(name),
                Some(ParsedBrtName::SheetBuiltin(name)) => sheet_builtin_names.push(name),
                None => {}
            }
        } else if rt == BRT_BOOK_PROTECTION {
            protect_structure |= u16le(p, 4).is_some_and(|flags| flags & 0x0001 != 0);
        }
    }
    (
        out,
        date1904,
        active_sheet,
        defined_names,
        protect_structure,
        sheet_builtin_names,
    )
}

/// Parse a `BrtName` record. Workbook-global user names are surfaced through
/// `Workbook::defined_names`; selected sheet-local built-ins become existing
/// sheet metadata facades.
fn parse_brt_name(p: &[u8]) -> Option<ParsedBrtName> {
    let flags = u32le(p, 0)?;
    let built_in = flags & 0x20 != 0;
    let itab = u32le(p, 5)?;
    let (name, used) = wide_string(p, 9)?;
    let formula_start = 9usize.checked_add(used)?;
    let cce = u32le(p, formula_start)? as usize;
    let rgce_start = formula_start + 4;
    let rgce = p.get(rgce_start..rgce_start.checked_add(cce)?)?;
    if built_in {
        if itab == 0xFFFF_FFFF {
            return None;
        }
        let kind = xlsb_builtin_name(&name)?;
        let sheet_index = usize::try_from(itab).ok()?;
        let ranges = parse_brt_name_ranges(rgce)?;
        Some(ParsedBrtName::SheetBuiltin(SheetBuiltinName {
            sheet_index,
            kind,
            ranges,
        }))
    } else if itab == 0xFFFF_FFFF && !name.is_empty() {
        Some(ParsedBrtName::GlobalUser((
            name,
            crate::ptg::decompile(rgce, true),
        )))
    } else {
        None
    }
}

enum ParsedBrtName {
    GlobalUser((String, String)),
    SheetBuiltin(SheetBuiltinName),
}

#[derive(Clone, Copy)]
enum SheetBuiltinKind {
    PrintArea,
    PrintTitles,
    FilterDatabase,
}

struct SheetBuiltinName {
    sheet_index: usize,
    kind: SheetBuiltinKind,
    ranges: Vec<SheetRange>,
}

fn xlsb_builtin_name(name: &str) -> Option<SheetBuiltinKind> {
    let lower = name.to_ascii_lowercase();
    let name = lower.strip_prefix("_xlnm.").unwrap_or(&lower);
    match name {
        "_filterdatabase" => Some(SheetBuiltinKind::FilterDatabase),
        "print_area" => Some(SheetBuiltinKind::PrintArea),
        "print_titles" => Some(SheetBuiltinKind::PrintTitles),
        _ => None,
    }
}

fn apply_xlsb_sheet_builtin_names(sheets: &mut [Sheet], names: Vec<SheetBuiltinName>) {
    for name in names {
        let Some(sheet) = sheets.get_mut(name.sheet_index) else {
            continue;
        };
        match name.kind {
            SheetBuiltinKind::PrintArea => {
                if let Some(range) = name.ranges.into_iter().next() {
                    sheet
                        .page_setup
                        .get_or_insert_with(PageSetup::default)
                        .print_area = Some(range);
                }
            }
            SheetBuiltinKind::PrintTitles => {
                let setup = sheet.page_setup.get_or_insert_with(PageSetup::default);
                for range in name.ranges {
                    apply_xlsb_print_title_range(setup, range);
                }
            }
            SheetBuiltinKind::FilterDatabase => {
                if let Some(range) = name.ranges.into_iter().next() {
                    sheet.autofilter = Some(range);
                }
            }
        }
    }
}

fn apply_xlsb_print_title_range(setup: &mut PageSetup, range: SheetRange) {
    let (r0, c0, r1, c1) = range;
    if c0 == 0 && u32::from(c1) >= MAX_XLSB_COL_INDEX {
        setup.repeat_rows = Some((r0, r1));
    }
    if r0 == 0 && r1 >= MAX_XLSB_ROW_INDEX {
        setup.repeat_cols = Some((c0, c1));
    }
}

fn parse_brt_name_ranges(rgce: &[u8]) -> Option<Vec<SheetRange>> {
    let mut ranges = Vec::new();
    let mut offset = 0usize;
    while offset < rgce.len() {
        let token = rgce[offset];
        match token {
            0x24 | 0x44 | 0x64 => {
                let (row, col) = parse_brt_name_ref(rgce, offset + 1)?;
                ranges.push((row, col, row, col));
                offset += 7;
            }
            0x1A | 0x3A | 0x5A | 0x7A => {
                let (row, col) = parse_brt_name_ref(rgce, offset + 3)?;
                ranges.push((row, col, row, col));
                offset += 9;
            }
            0x25 | 0x45 | 0x65 => {
                ranges.push(parse_brt_name_area(rgce, offset + 1)?);
                offset += 13;
            }
            0x1B | 0x3B | 0x5B | 0x7B => {
                ranges.push(parse_brt_name_area(rgce, offset + 3)?);
                offset += 15;
            }
            0x10 => offset += 1, // PtgUnion
            _ => return None,
        }
    }
    (!ranges.is_empty()).then_some(ranges)
}

fn parse_brt_name_ref(rgce: &[u8], offset: usize) -> Option<(u32, u16)> {
    let row = u32le(rgce, offset)?;
    let col = u16le(rgce, offset + 4)? & 0x3FFF;
    Some((row, col))
}

fn parse_brt_name_area(rgce: &[u8], offset: usize) -> Option<SheetRange> {
    let r0 = u32le(rgce, offset)?;
    let r1 = u32le(rgce, offset + 4)?;
    let c0 = u16le(rgce, offset + 8)? & 0x3FFF;
    let c1 = u16le(rgce, offset + 10)? & 0x3FFF;
    Some((r0.min(r1), c0.min(c1), r0.max(r1), c0.max(c1)))
}

/// An `XLNullableWideString`: `cch == 0xFFFFFFFF` means null (empty), else a
/// normal `XLWideString`. Returns `(string, bytes_consumed)`.
fn nullable_wide(b: &[u8], o: usize) -> Option<(String, usize)> {
    let cch = u32le(b, o)?;
    if cch == 0xFFFF_FFFF {
        return Some((String::new(), 4));
    }
    wide_string(b, o)
}

type WorkbookSheets = Vec<(String, String, u32)>;
type DefinedNames = Vec<(String, String)>;
type SheetRange = (u32, u16, u32, u16);
type SheetRanges = Vec<SheetRange>;
type Merges = SheetRanges;
type Hyperlinks = Vec<(u32, u16, String)>;
type AutoFilter = Option<SheetRange>;

#[derive(Default)]
struct SheetReadMetadata {
    freeze: Option<(u32, u16)>,
    autofilter: AutoFilter,
    data_validations: Vec<DataValidation>,
    page_setup: Option<PageSetup>,
    tab_color: Option<Color>,
    table_rel_ids: Vec<String>,
    print_gridlines: bool,
    print_headings: bool,
    hide_gridlines: bool,
    zoom: Option<u16>,
    show_headers: Option<bool>,
    right_to_left: bool,
    selected: bool,
    protect: bool,
    protect_options: Option<ProtectionOptions>,
    row_outline: BTreeMap<u32, u8>,
    col_outline: BTreeMap<u16, u8>,
    outline_summary_below: Option<bool>,
    outline_summary_right: Option<bool>,
    collapsed_rows: BTreeSet<u32>,
}

fn parse_sheet_comments(
    zip: &mut zip::ZipArchive<std::io::Cursor<&[u8]>>,
    sheet_path: &str,
    sheet_rels: &HashMap<String, String>,
    sheet_rels_xml: &str,
) -> Vec<Comment> {
    if sheet_rels_xml.is_empty() {
        return Vec::new();
    }
    let rel_types = crate::xlsx::parse_rel_types(sheet_rels_xml);
    let Some(target) = rel_types
        .iter()
        .find(|(_, ty)| ty.rsplit('/').next() == Some("comments"))
        .and_then(|(id, _)| sheet_rels.get(id))
    else {
        return Vec::new();
    };
    let path = normalize_part_target(sheet_path, target);
    part(zip, &path)
        .map(|b| parse_comments(&b))
        .unwrap_or_default()
}

fn parse_comments(b: &[u8]) -> Vec<Comment> {
    struct PendingComment {
        row: u32,
        col: u16,
        text: String,
        author: Option<String>,
    }

    let mut authors = Vec::new();
    let mut out = Vec::new();
    let mut pending: Option<PendingComment> = None;
    let mut r = RecReader::new(b);
    while let Some((rt, p)) = r.next() {
        match rt {
            BRT_COMMENT_AUTHOR => {
                if let Some((author, _)) = wide_string(p, 0) {
                    authors.push(author);
                }
            }
            BRT_BEGIN_COMMENT => {
                let (Some(author_id), Some(row), Some(col)) =
                    (u32le(p, 0), u32le(p, 4), u32le(p, 12))
                else {
                    pending = None;
                    continue;
                };
                let author = authors.get(author_id as usize).cloned();
                pending = Some(PendingComment {
                    row,
                    col: col.min(u32::from(u16::MAX)) as u16,
                    text: String::new(),
                    author,
                });
            }
            BRT_COMMENT_TEXT => {
                if let (Some(comment), Some(text)) = (pending.as_mut(), rich_string_text(p)) {
                    comment.text.push_str(&text);
                }
            }
            BRT_END_COMMENT => {
                if let Some(comment) = pending.take() {
                    out.push(Comment {
                        row: comment.row,
                        col: comment.col,
                        text: comment.text,
                        author: comment.author,
                    });
                }
            }
            _ => {}
        }
    }
    out
}

fn rich_string_text(b: &[u8]) -> Option<String> {
    // RichStr starts with one flag byte followed by the plain XLWideString; the
    // trailing rich-format runs are not needed for the public comment text.
    wide_string(b, 1).map(|(s, _)| s)
}

fn parse_sheet(
    b: &[u8],
    shared: &[String],
    styles: &Styles,
    date1904: bool,
    sheet_rels: &HashMap<String, String>,
    budget: &mut usize,
) -> (Vec<CellEntry>, Merges, Hyperlinks, SheetReadMetadata) {
    let mut cells = Vec::new();
    let mut merges = Vec::new();
    let mut hyperlinks = Vec::new();
    let mut metadata = SheetReadMetadata::default();
    let mut selected_view_rank = 0u8;
    let mut in_selected_view = false;
    let mut pending_dval_list: Option<String> = None;
    let mut row: u32 = 0;
    let mut r = RecReader::new(b);
    while let Some((rt, p)) = r.next() {
        match rt {
            BRT_DVAL_LIST => {
                pending_dval_list = wide_string(p, 0).map(|(formula, _)| formula);
            }
            BRT_DVAL => {
                metadata
                    .data_validations
                    .extend(parse_dval(p, pending_dval_list.take()));
            }
            BRT_SHEET_PROTECTION => {
                apply_sheet_protection(p, &mut metadata);
            }
            BRT_WS_PROP => {
                apply_ws_prop_metadata(p, &mut metadata);
            }
            BRT_MARGINS => {
                if let Some(margins) = parse_page_margins(p) {
                    page_setup_mut(&mut metadata).margins = Some(margins);
                }
            }
            BRT_PRINT_OPTIONS => {
                parse_print_options(p, &mut metadata);
            }
            BRT_PAGE_SETUP => {
                parse_page_setup(p, &mut metadata);
            }
            BRT_BEGIN_HEADER_FOOTER => {
                parse_header_footer(p, &mut metadata);
            }
            BRT_BEGIN_WS_VIEW => {
                let Some(rank) = parse_sheet_view(p, &mut metadata, selected_view_rank) else {
                    in_selected_view = false;
                    continue;
                };
                selected_view_rank = rank;
                in_selected_view = true;
            }
            BRT_END_WS_VIEW => {
                in_selected_view = false;
            }
            BRT_PANE if in_selected_view => {
                if let Some(freeze) = parse_pane_freeze(p) {
                    metadata.freeze = Some(freeze);
                }
            }
            BRT_BEGIN_AFILTER => {
                metadata.autofilter = parse_unchecked_rfx(p);
            }
            BRT_COL_INFO => {
                apply_col_outline(p, &mut metadata);
            }
            BRT_ROW_HDR => {
                if let Some(rr) = u32le(p, 0) {
                    row = rr;
                }
                apply_row_outline(p, &mut metadata);
            }
            BRT_MERGE_CELL => {
                // UncheckedRfX: rwFirst:u32, rwLast:u32, colFirst:u32, colLast:u32.
                if let (Some(rf), Some(rl), Some(cf), Some(cl)) =
                    (u32le(p, 0), u32le(p, 4), u32le(p, 8), u32le(p, 12))
                {
                    merges.push((
                        rf,
                        cf.min(u32::from(u16::MAX)) as u16,
                        rl,
                        cl.min(u32::from(u16::MAX)) as u16,
                    ));
                }
            }
            BRT_HLINK => {
                hyperlinks.extend(parse_hlink(p, sheet_rels));
            }
            BRT_LIST_PART => {
                if let Some(rel_id) = parse_list_part_rel_id(p) {
                    metadata.table_rel_ids.push(rel_id);
                }
            }
            BRT_CELL_RK | BRT_CELL_REAL | BRT_CELL_ISST | BRT_CELL_ST | BRT_CELL_BOOL
            | BRT_CELL_ERROR | BRT_FMLA_NUM | BRT_FMLA_STRING | BRT_FMLA_BOOL | BRT_FMLA_ERROR => {
                if *budget == 0 {
                    continue;
                }
                // Cell: col:u32 (0..4), iStyleRef:u24 + flags (4..8). Value at 8.
                let Some(col_u) = u32le(p, 0) else { continue };
                let col = col_u.min(u32::from(u16::MAX)) as u16;
                let style_idx = (u32::from(*p.get(4).unwrap_or(&0))
                    | u32::from(*p.get(5).unwrap_or(&0)) << 8
                    | u32::from(*p.get(6).unwrap_or(&0)) << 16)
                    as usize;
                decode_cell(
                    rt, p, col, style_idx, row, shared, styles, date1904, &mut cells, budget,
                );
            }
            _ => {}
        }
    }
    (cells, merges, hyperlinks, metadata)
}

fn parse_list_part_rel_id(p: &[u8]) -> Option<String> {
    let (rel_id, _) = wide_string(p, 0)?;
    (!rel_id.is_empty()).then_some(rel_id)
}

fn parse_ws_prop_tab_color(p: &[u8]) -> Option<Color> {
    // BrtWsProp starts with 2 bytes of worksheet property flags plus one byte of
    // filter/conditional-format flags, followed by the 8-byte BrtColor tab color.
    parse_brt_color(p.get(3..11)?)
}

fn apply_sheet_protection(p: &[u8], metadata: &mut SheetReadMetadata) {
    let Some(locked) = u32le(p, 2) else {
        return;
    };
    metadata.protect = locked != 0;
    if !metadata.protect {
        metadata.protect_options = None;
        return;
    }

    let options = ProtectionOptions {
        format_cells: u32le(p, 14).unwrap_or(0) != 0,
        format_columns: u32le(p, 18).unwrap_or(0) != 0,
        format_rows: u32le(p, 22).unwrap_or(0) != 0,
        insert_columns: u32le(p, 26).unwrap_or(0) != 0,
        insert_rows: u32le(p, 30).unwrap_or(0) != 0,
        insert_hyperlinks: u32le(p, 34).unwrap_or(0) != 0,
        delete_columns: u32le(p, 38).unwrap_or(0) != 0,
        delete_rows: u32le(p, 42).unwrap_or(0) != 0,
        sort: u32le(p, 50).unwrap_or(0) != 0,
        auto_filter: u32le(p, 54).unwrap_or(0) != 0,
        pivot_tables: u32le(p, 58).unwrap_or(0) != 0,
    };
    metadata.protect_options = (options != ProtectionOptions::default()).then_some(options);
}

fn apply_ws_prop_metadata(p: &[u8], metadata: &mut SheetReadMetadata) {
    metadata.tab_color = parse_ws_prop_tab_color(p);
    if let Some(flags) = u16le(p, 0) {
        metadata.outline_summary_below = Some(flags & 0x0040 != 0);
        metadata.outline_summary_right = Some(flags & 0x0080 != 0);
    }
}

fn apply_row_outline(p: &[u8], metadata: &mut SheetReadMetadata) {
    let (Some(row), Some(flags)) = (u32le(p, 0), u16le(p, 10)) else {
        return;
    };
    let level = ((flags >> 8) & 0x07) as u8;
    if level > 0 {
        metadata.row_outline.insert(row, level);
    }
    if flags & (1 << 11) != 0 {
        metadata.collapsed_rows.insert(row);
    }
}

fn apply_col_outline(p: &[u8], metadata: &mut SheetReadMetadata) {
    let (Some(first), Some(last), Some(flags)) = (u32le(p, 0), u32le(p, 4), u16le(p, 16)) else {
        return;
    };
    let level = ((flags >> 8) & 0x07) as u8;
    if level == 0 || first > last || first > MAX_XLSB_COL_INDEX {
        return;
    }
    for col in first..=last.min(MAX_XLSB_COL_INDEX) {
        metadata.col_outline.insert(col as u16, level);
    }
}

fn parse_brt_color(p: &[u8]) -> Option<Color> {
    let flags = *p.first()?;
    let valid_rgb = flags & 0x01 != 0;
    let color_type = flags >> 1;
    if !valid_rgb || color_type != 0x02 {
        return None;
    }
    Some(Color::rgb(*p.get(4)?, *p.get(5)?, *p.get(6)?))
}

fn parse_sheet_tables(
    zip: &mut zip::ZipArchive<std::io::Cursor<&[u8]>>,
    sheet_path: &str,
    sheet_rels: &HashMap<String, String>,
    sheet_rels_xml: &str,
    table_rel_ids: &[String],
) -> Vec<Table> {
    if sheet_rels_xml.is_empty() {
        return Vec::new();
    }

    let rel_types = crate::xlsx::parse_rel_types(sheet_rels_xml);
    let mut rel_ids: Vec<String> = table_rel_ids
        .iter()
        .filter(|id| {
            rel_types
                .get(*id)
                .is_some_and(|ty| ty.rsplit('/').next() == Some("table"))
        })
        .cloned()
        .collect();
    if rel_ids.is_empty() {
        rel_ids.extend(
            rel_types
                .iter()
                .filter(|(_, ty)| ty.rsplit('/').next() == Some("table"))
                .map(|(id, _)| id.clone()),
        );
    }

    let mut seen = HashSet::new();
    rel_ids
        .into_iter()
        .filter(|id| seen.insert(id.clone()))
        .filter_map(|id| sheet_rels.get(&id).cloned())
        .map(|target| normalize_part_target(sheet_path, &target))
        .filter_map(|path| part(zip, &path))
        .filter_map(|bytes| parse_table(&bytes))
        .collect()
}

fn parse_table(b: &[u8]) -> Option<Table> {
    let mut range: Option<SheetRange> = None;
    let mut name: Option<String> = None;
    let mut display_name: Option<String> = None;
    let mut columns = Vec::new();
    let mut style: Option<String> = None;

    let mut r = RecReader::new(b);
    while let Some((rt, p)) = r.next() {
        match rt {
            BRT_BEGIN_LIST => {
                if let Some((parsed_range, parsed_name, parsed_display_name)) =
                    parse_table_begin_list(p)
                {
                    range = Some(parsed_range);
                    name = parsed_name;
                    display_name = parsed_display_name;
                }
            }
            BRT_BEGIN_LIST_COL if columns.len() < MAX_TABLE_COLUMNS => {
                if let Some(column) = parse_table_column(p) {
                    columns.push(column);
                }
            }
            BRT_TABLE_STYLE_CLIENT => {
                style = parse_table_style_client(p);
            }
            _ => {}
        }
    }

    Some(Table {
        range: range?,
        name: display_name.or(name).unwrap_or_default(),
        columns,
        style,
    })
}

type XlsbTableHeader = (SheetRange, Option<String>, Option<String>);

fn parse_table_begin_list(p: &[u8]) -> Option<XlsbTableHeader> {
    let range = parse_unchecked_rfx(p.get(0..16)?)?;
    let mut offset = 64usize;
    let (name, used) = nullable_wide_opt(p, offset)?;
    offset = offset.checked_add(used)?;
    let (display_name, _) = nullable_wide_opt(p, offset)?;
    Some((
        range,
        name.filter(|s| !s.is_empty()),
        display_name.filter(|s| !s.is_empty()),
    ))
}

fn parse_table_column(p: &[u8]) -> Option<String> {
    let mut offset = 24usize;
    let (name, used) = nullable_wide_opt(p, offset)?;
    offset = offset.checked_add(used)?;
    let (caption, _) = nullable_wide_opt(p, offset)?;
    caption.or(name).and_then(|s| (!s.is_empty()).then_some(s))
}

fn parse_table_style_client(p: &[u8]) -> Option<String> {
    let (style, _) = wide_string(p, 2)?;
    (!style.is_empty()).then_some(style)
}

fn page_setup_mut(metadata: &mut SheetReadMetadata) -> &mut PageSetup {
    metadata.page_setup.get_or_insert_with(PageSetup::default)
}

fn parse_page_margins(p: &[u8]) -> Option<(f64, f64, f64, f64, f64, f64)> {
    Some((
        xlsb_margin_at(p, 0)?,
        xlsb_margin_at(p, 8)?,
        xlsb_margin_at(p, 16)?,
        xlsb_margin_at(p, 24)?,
        xlsb_margin_at(p, 32)?,
        xlsb_margin_at(p, 40)?,
    ))
}

fn xlsb_margin_at(p: &[u8], offset: usize) -> Option<f64> {
    f64le(p, offset).filter(|value| value.is_finite() && *value >= 0.0 && *value < 49.0)
}

fn parse_print_options(p: &[u8], metadata: &mut SheetReadMetadata) {
    let Some(flags) = u16le(p, 0) else { return };
    if flags & 0x0001 != 0 {
        page_setup_mut(metadata).center_horizontally = true;
    }
    if flags & 0x0002 != 0 {
        page_setup_mut(metadata).center_vertically = true;
    }
    metadata.print_headings = flags & 0x0004 != 0;
    metadata.print_gridlines = flags & 0x0008 != 0;
}

fn parse_page_setup(p: &[u8], metadata: &mut SheetReadMetadata) {
    let (Some(page_start), Some(flags)) = (i32le(p, 20), u16le(p, 32)) else {
        return;
    };

    let ps = page_setup_mut(metadata);
    ps.paper_size = nonzero_u32_as_u16(p, 0);
    ps.scale = nonzero_u32_as_u16(p, 4);
    ps.fit_to_width = nonzero_u32_as_u16(p, 24);
    ps.fit_to_height = nonzero_u32_as_u16(p, 28);
    if flags & 0x0040 == 0 {
        ps.landscape = flags & 0x0002 != 0;
    }
    if flags & 0x0080 != 0 && page_start > 0 {
        ps.first_page_number = u16::try_from(page_start).ok();
    }
}

fn nonzero_u32_as_u16(p: &[u8], offset: usize) -> Option<u16> {
    let value = u32le(p, offset)?;
    if value == 0 {
        return None;
    }
    u16::try_from(value).ok()
}

fn parse_header_footer(p: &[u8], metadata: &mut SheetReadMetadata) {
    let Some((header, footer)) = parse_header_footer_pair(p) else {
        return;
    };
    if !header.is_empty() {
        page_setup_mut(metadata).header = Some(header);
    }
    if !footer.is_empty() {
        page_setup_mut(metadata).footer = Some(footer);
    }
}

fn parse_header_footer_pair(p: &[u8]) -> Option<(String, String)> {
    let mut offset = 2usize;
    let (header, used) = nullable_wide(p, offset)?;
    offset = offset.checked_add(used)?;
    let (footer, _) = nullable_wide(p, offset)?;
    Some((header, footer))
}

fn parse_dval(p: &[u8], list_formula: Option<String>) -> Vec<DataValidation> {
    let Some(flags) = u32le(p, 0) else {
        return Vec::new();
    };
    let Some(kind) = parse_dval_kind(flags & 0x0F) else {
        return Vec::new();
    };
    let operator = parse_dval_op((flags >> 20) & 0x0F).unwrap_or(DvOp::Between);
    let allow_blank = flags & (1 << 8) != 0;
    let show_input_message = flags & (1 << 18) != 0;
    let show_error_message = flags & (1 << 19) != 0;

    let Some((ranges, ranges_len)) = parse_unchecked_sq_rfx(p, 4) else {
        return Vec::new();
    };
    let strings_offset = 4 + ranges_len;
    let Some((error, prompt, strings_len)) = parse_dval_strings(p, strings_offset) else {
        return Vec::new();
    };
    let formula1_offset = strings_offset + strings_len;
    let Some((parsed_formula1, formula1_len)) = parse_dv_formula(p, formula1_offset) else {
        return Vec::new();
    };
    let formula2_offset = formula1_offset + formula1_len;
    let Some((parsed_formula2, _formula2_len)) = parse_dv_formula(p, formula2_offset) else {
        return Vec::new();
    };

    let formula1 = list_formula.unwrap_or(parsed_formula1);
    if formula1.is_empty() {
        return Vec::new();
    }
    let formula2 = (!parsed_formula2.is_empty()).then_some(parsed_formula2);
    let Some((&sqref, rest)) = ranges.split_first() else {
        return Vec::new();
    };

    let base = DataValidation {
        sqref,
        kind,
        operator,
        formula1,
        formula2,
        allow_blank,
        show_input_message,
        show_error_message,
        prompt,
        error,
    };
    let mut out = Vec::with_capacity(ranges.len().min(MAX_DVAL_RANGES));
    out.push(base.clone());
    for sqref in rest.iter().take(MAX_DVAL_RANGES - 1) {
        let mut clone = base.clone();
        clone.sqref = *sqref;
        out.push(clone);
    }
    out
}

fn parse_dval_kind(value: u32) -> Option<DvKind> {
    match value {
        1 => Some(DvKind::Whole),
        2 => Some(DvKind::Decimal),
        3 => Some(DvKind::List),
        4 => Some(DvKind::Date),
        5 => Some(DvKind::Time),
        6 => Some(DvKind::TextLength),
        7 => Some(DvKind::Custom),
        _ => None,
    }
}

fn parse_dval_op(value: u32) -> Option<DvOp> {
    match value {
        0 => Some(DvOp::Between),
        1 => Some(DvOp::NotBetween),
        2 => Some(DvOp::Equal),
        3 => Some(DvOp::NotEqual),
        4 => Some(DvOp::GreaterThan),
        5 => Some(DvOp::LessThan),
        6 => Some(DvOp::GreaterThanOrEqual),
        7 => Some(DvOp::LessThanOrEqual),
        _ => None,
    }
}

fn parse_unchecked_sq_rfx(p: &[u8], offset: usize) -> Option<(SheetRanges, usize)> {
    let crfx = i32::from_le_bytes(p.get(offset..offset + 4)?.try_into().ok()?);
    if crfx <= 0 {
        return None;
    }
    let count = usize::try_from(crfx).ok()?;
    let start = offset.checked_add(4)?;
    let ranges_len = count.checked_mul(16)?;
    let end = start.checked_add(ranges_len)?;
    p.get(start..end)?;

    let retained_count = count.min(MAX_DVAL_RANGES);
    let mut ranges = Vec::with_capacity(retained_count);
    for i in 0..retained_count {
        let pos = start + i * 16;
        let range = parse_unchecked_rfx(p.get(pos..pos + 16)?)?;
        ranges.push(range);
    }
    Some((ranges, end - offset))
}

type DvalStrings = (Option<(String, String)>, Option<(String, String)>, usize);

fn parse_dval_strings(p: &[u8], offset: usize) -> Option<DvalStrings> {
    let (error_title, used1) = nullable_wide_opt(p, offset)?;
    let (error_message, used2) = nullable_wide_opt(p, offset + used1)?;
    let (prompt_title, used3) = nullable_wide_opt(p, offset + used1 + used2)?;
    let (prompt_message, used4) = nullable_wide_opt(p, offset + used1 + used2 + used3)?;
    let error = match (error_title, error_message) {
        (None, None) => None,
        (title, message) => Some((title.unwrap_or_default(), message.unwrap_or_default())),
    };
    let prompt = match (prompt_title, prompt_message) {
        (None, None) => None,
        (title, message) => Some((title.unwrap_or_default(), message.unwrap_or_default())),
    };
    Some((error, prompt, used1 + used2 + used3 + used4))
}

fn nullable_wide_opt(b: &[u8], o: usize) -> Option<(Option<String>, usize)> {
    let cch = u32le(b, o)?;
    if cch == 0xFFFF_FFFF {
        return Some((None, 4));
    }
    wide_string(b, o).map(|(s, used)| (Some(s), used))
}

fn parse_dv_formula(p: &[u8], offset: usize) -> Option<(String, usize)> {
    let cce = u32le(p, offset)? as usize;
    let rgce_start = offset + 4;
    let rgce_end = rgce_start.checked_add(cce)?;
    let rgce = p.get(rgce_start..rgce_end)?;
    let cb = u32le(p, rgce_end)? as usize;
    let end = rgce_end.checked_add(4)?.checked_add(cb)?;
    p.get(rgce_end + 4..end)?;
    Some((crate::ptg::decompile(rgce, true), end - offset))
}

fn parse_sheet_view(p: &[u8], metadata: &mut SheetReadMetadata, current_rank: u8) -> Option<u8> {
    let flags = u16le(p, 0)?;
    let i_wbk_view = u32le(p, 26).unwrap_or(0);
    let rank = if i_wbk_view == 0 { 2 } else { 1 };
    if rank <= current_rank {
        return None;
    }

    metadata.freeze = None;
    metadata.hide_gridlines = flags & (1 << 2) == 0;
    metadata.show_headers = Some(flags & (1 << 3) != 0);
    metadata.right_to_left = flags & (1 << 5) != 0;
    metadata.selected = flags & (1 << 6) != 0;
    metadata.zoom = u16le(p, 18).filter(|&zoom| zoom != 0);
    Some(rank)
}

fn parse_pane_freeze(p: &[u8]) -> Option<(u32, u16)> {
    let flags = *p.get(28)?;
    if flags & 0x03 == 0 {
        return None;
    }
    let rows = f64le(p, 0)?.max(0.0).floor() as u32;
    let cols_u = f64le(p, 8)?.max(0.0).floor() as u32;
    let cols = cols_u.min(u32::from(u16::MAX)) as u16;
    if rows == 0 && cols == 0 {
        None
    } else {
        Some((rows, cols))
    }
}

fn parse_unchecked_rfx(p: &[u8]) -> AutoFilter {
    let (Some(rf), Some(rl), Some(cf), Some(cl)) =
        (u32le(p, 0), u32le(p, 4), u32le(p, 8), u32le(p, 12))
    else {
        return None;
    };
    if rf > rl || cf > cl {
        return None;
    }
    Some((
        rf,
        cf.min(u32::from(u16::MAX)) as u16,
        rl,
        cl.min(u32::from(u16::MAX)) as u16,
    ))
}

fn parse_hlink(p: &[u8], sheet_rels: &HashMap<String, String>) -> Hyperlinks {
    const MAX_HYPERLINK_CELLS: usize = 1 << 16;
    let (Some(rf), Some(rl), Some(cf), Some(cl)) =
        (u32le(p, 0), u32le(p, 4), u32le(p, 8), u32le(p, 12))
    else {
        return Vec::new();
    };
    let Some((rel_id, rel_len)) = nullable_wide(p, 16) else {
        return Vec::new();
    };
    let location_offset = 16 + rel_len;
    let Some((location, _location_len)) = wide_string(p, location_offset) else {
        return Vec::new();
    };

    let target = match (sheet_rels.get(&rel_id), location.is_empty()) {
        (Some(url), true) => url.clone(),
        (Some(url), false) => format!("{url}#{location}"),
        (None, false) if rel_id.is_empty() => format!("#{location}"),
        _ => return Vec::new(),
    };

    let c0 = cf.min(u32::from(u16::MAX)) as u16;
    let c1 = cl.min(u32::from(u16::MAX)) as u16;
    if rf > rl || c0 > c1 {
        return Vec::new();
    }

    let mut hyperlinks = Vec::new();
    'links: for row in rf..=rl {
        for col in c0..=c1 {
            if hyperlinks.len() >= MAX_HYPERLINK_CELLS {
                break 'links;
            }
            hyperlinks.push((row, col, target.clone()));
        }
    }
    hyperlinks
}

#[allow(clippy::too_many_arguments)]
fn decode_cell(
    rt: u32,
    p: &[u8],
    col: u16,
    style_idx: usize,
    row: u32,
    shared: &[String],
    styles: &Styles,
    date1904: bool,
    cells: &mut Vec<CellEntry>,
    budget: &mut usize,
) {
    let push = |cells: &mut Vec<CellEntry>, budget: &mut usize, value: Cell, text: String| {
        if text.len() > *budget {
            *budget = 0;
            return;
        }
        *budget -= text.len();
        cells.push(CellEntry {
            row,
            col,
            value,
            text,
            style: None,
            hyperlink: None,
        });
    };
    let number = |f: f64, cells: &mut Vec<CellEntry>, budget: &mut usize| {
        let kind = styles.kind(style_idx);
        let display = format::render_value(f, kind, date1904);
        let cell = if kind.is_datetime() {
            Cell::Date(f)
        } else {
            Cell::Number(f)
        };
        push(cells, budget, cell, display);
    };
    match rt {
        BRT_CELL_REAL => {
            if let Some(s) = p.get(8..16) {
                number(
                    f64::from_le_bytes(s.try_into().unwrap_or([0; 8])),
                    cells,
                    budget,
                );
            }
        }
        BRT_FMLA_NUM => {
            if let Some(s) = p.get(8..16) {
                let f = f64::from_le_bytes(s.try_into().unwrap_or([0; 8]));
                let kind = styles.kind(style_idx);
                let display = format::render_value(f, kind, date1904);
                let cached = if kind.is_datetime() {
                    Cell::Date(f)
                } else {
                    Cell::Number(f)
                };
                push(cells, budget, wrap_fmla(p, 16, cached), display);
            }
        }
        BRT_CELL_RK => {
            if let Some(rk) = u32le(p, 8) {
                number(rk_to_f64(rk), cells, budget);
            }
        }
        BRT_CELL_ISST => {
            if let Some(isst) = u32le(p, 8) {
                if let Some(s) = shared.get(isst as usize) {
                    push(cells, budget, Cell::Text(s.clone()), s.clone());
                }
            }
        }
        BRT_CELL_ST => {
            if let Some((s, _)) = wide_string(p, 8) {
                push(cells, budget, Cell::Text(s.clone()), s);
            }
        }
        BRT_FMLA_STRING => {
            if let Some((s, used)) = wide_string(p, 8) {
                push(
                    cells,
                    budget,
                    wrap_fmla(p, 8 + used, Cell::Text(s.clone())),
                    s,
                );
            }
        }
        BRT_CELL_BOOL => {
            let b = p.get(8).copied().unwrap_or(0) != 0;
            push(
                cells,
                budget,
                Cell::Bool(b),
                if b { "TRUE" } else { "FALSE" }.to_string(),
            );
        }
        BRT_FMLA_BOOL => {
            let b = p.get(8).copied().unwrap_or(0) != 0;
            let text = if b { "TRUE" } else { "FALSE" }.to_string();
            push(cells, budget, wrap_fmla(p, 9, Cell::Bool(b)), text);
        }
        BRT_CELL_ERROR => {
            let code = crate::error_code(p.get(8).copied().unwrap_or(0)).to_string();
            push(cells, budget, Cell::Error(code.clone()), code);
        }
        BRT_FMLA_ERROR => {
            let code = crate::error_code(p.get(8).copied().unwrap_or(0)).to_string();
            push(
                cells,
                budget,
                wrap_fmla(p, 9, Cell::Error(code.clone())),
                code,
            );
        }
        _ => {}
    }
}

/// Wrap a cached value as `Cell::Formula` by decoding the `BrtFmla*` formula that
/// follows it. Layout after the cached value: `grbitFlags:u16`, then a
/// `CellParsedFormula` (`cce:u32`, `rgce[cce]`, …). `value_end` is the byte offset
/// just past the cached value. Falls back to the bare cached value if the rgce is
/// absent or decompiles to nothing.
fn wrap_fmla(p: &[u8], value_end: usize, cached: Cell) -> Cell {
    let Some(cce) = u32le(p, value_end + 2) else {
        return cached;
    };
    let start = value_end + 6;
    let Some(rgce) = p.get(start..start.saturating_add(cce as usize)) else {
        return cached;
    };
    let f = crate::ptg::decompile(rgce, true);
    if f.is_empty() {
        cached
    } else {
        Cell::Formula {
            formula: f,
            cached: Box::new(cached),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Color, SheetMetadata, SheetType, SheetVisible};
    use std::io::Write;
    use zip::write::SimpleFileOptions;

    /// `XLWideString`: `cch:u32` + UTF-16LE chars.
    fn wstr(s: &str) -> Vec<u8> {
        let units: Vec<u16> = s.encode_utf16().collect();
        let mut v = (units.len() as u32).to_le_bytes().to_vec();
        for u in units {
            v.extend_from_slice(&u.to_le_bytes());
        }
        v
    }

    fn null_wstr() -> Vec<u8> {
        0xFFFF_FFFFu32.to_le_bytes().to_vec()
    }

    /// Frame a BIFF12 record: var-uint type, var-uint size, payload.
    fn rec(rt: u32, payload: &[u8]) -> Vec<u8> {
        let mut v = Vec::new();
        if rt < 0x80 {
            v.push(rt as u8);
        } else {
            v.push((rt & 0x7F) as u8 | 0x80);
            v.push((rt >> 7) as u8 & 0x7F);
        }
        let mut sz = payload.len();
        loop {
            let mut b = (sz & 0x7F) as u8;
            sz >>= 7;
            if sz > 0 {
                b |= 0x80;
            }
            v.push(b);
            if sz == 0 {
                break;
            }
        }
        v.extend_from_slice(payload);
        v
    }

    fn xf(numfmt: u16) -> Vec<u8> {
        let mut v = vec![0u8; 16];
        v[2..4].copy_from_slice(&numfmt.to_le_bytes());
        v
    }

    #[test]
    fn record_framing_var_uints() {
        // A 2-byte record type (156 = BrtBundleSh) and small size round-trip.
        let r = rec(BRT_BUNDLE_SH, &[1, 2, 3]);
        let mut rr = RecReader::new(&r);
        let (rt, p) = rr.next().unwrap();
        assert_eq!(rt, BRT_BUNDLE_SH);
        assert_eq!(p, &[1, 2, 3]);
        assert!(rr.next().is_none());
    }

    #[test]
    fn reads_a_synthetic_xlsb() {
        // workbook.bin: one BrtBundleSh(rId1, "시트1").
        let mut wb_bin = vec![0u8; 8]; // hsState + iTabID
        wb_bin.extend_from_slice(&wstr("rId1")); // strRelID (non-null)
        wb_bin.extend_from_slice(&wstr("시트1")); // strName
        let wb_bin = rec(BRT_BUNDLE_SH, &wb_bin);

        // sharedStrings.bin: BrtSSTItem("품목").
        let mut item = vec![0u8]; // flags
        item.extend_from_slice(&wstr("품목"));
        let sst = rec(BRT_SST_ITEM, &item);

        // sheet1.bin: RowHdr(0), CellIsst(0,0 → isst 0), CellReal(0,1 → 42.0).
        let mut sh = rec(BRT_ROW_HDR, &[0, 0, 0, 0]);
        let mut isst = vec![0u8; 8]; // col=0, styleRef/flags
        isst.extend_from_slice(&0u32.to_le_bytes()); // isst = 0
        sh.extend_from_slice(&rec(BRT_CELL_ISST, &isst));
        let mut real = vec![1, 0, 0, 0, 0, 0, 0, 0]; // col=1, styleRef
        real.extend_from_slice(&42.0f64.to_le_bytes());
        sh.extend_from_slice(&rec(BRT_CELL_REAL, &real));

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="worksheets/sheet1.bin"/></Relationships>"#;

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (name, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/sharedStrings.bin", sst.as_slice()),
            ("xl/worksheets/sheet1.bin", sh.as_slice()),
        ] {
            zw.start_file(name, opt).unwrap();
            zw.write_all(body).unwrap();
        }
        let bytes = zw.finish().unwrap().into_inner();

        let wb = Workbook::open(&bytes).unwrap();
        assert_eq!(wb.sheets.len(), 1);
        assert_eq!(wb.sheets[0].name, "시트1");
        assert_eq!(
            wb.sheets[0].cell(0, 0),
            Some(&Cell::Text("품목".to_string()))
        );
        assert_eq!(wb.sheets[0].cell(0, 1), Some(&Cell::Number(42.0)));
    }

    #[test]
    fn xlsb_book_view_active_tab_surfaces_workbook_metadata() {
        let mut wb_bin = Vec::new();
        for (rid, name) in [("rId1", "Data"), ("rId2", "Summary")] {
            let mut bundle = vec![0u8; 8]; // hsState + iTabID
            bundle.extend_from_slice(&wstr(rid));
            bundle.extend_from_slice(&wstr(name));
            wb_bin.extend_from_slice(&rec(BRT_BUNDLE_SH, &bundle));
        }
        let mut book_view = Vec::new();
        book_view.extend_from_slice(&0i32.to_le_bytes()); // xWn
        book_view.extend_from_slice(&0i32.to_le_bytes()); // yWn
        book_view.extend_from_slice(&0u32.to_le_bytes()); // dxWn
        book_view.extend_from_slice(&0u32.to_le_bytes()); // dyWn
        book_view.extend_from_slice(&600u32.to_le_bytes()); // iTabRatio
        book_view.extend_from_slice(&0u32.to_le_bytes()); // itabFirst
        book_view.extend_from_slice(&1u32.to_le_bytes()); // itabCur
        wb_bin.extend_from_slice(&rec(158, &book_view)); // BrtBookView

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="worksheets/sheet1.bin"/><Relationship Id="rId2" Target="worksheets/sheet2.bin"/></Relationships>"#;

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (path, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", [].as_slice()),
            ("xl/worksheets/sheet2.bin", [].as_slice()),
        ] {
            zw.start_file(path, opt).unwrap();
            zw.write_all(body).unwrap();
        }

        let wb = Workbook::open(&zw.finish().unwrap().into_inner()).unwrap();
        let metadata = wb.metadata();

        assert_eq!(wb.active_sheet_index(), Some(1));
        assert_eq!(wb.active_sheet_name(), Some("Summary"));
        assert_eq!(metadata.active_sheet, Some(1));
        assert_eq!(metadata.active_sheet_name, Some("Summary"));
        assert_eq!(
            <Workbook as crate::Reader>::metadata(&wb).active_sheet_name,
            Some("Summary")
        );
    }

    #[test]
    fn xlsb_selected_sheet_view_falls_back_to_active_sheet_metadata() {
        const BRT_BEGIN_WS_VIEWS: u32 = 133;
        const BRT_END_WS_VIEWS: u32 = 134;

        let mut wb_bin = Vec::new();
        for (rid, name) in [("rId1", "Data"), ("rId2", "Summary")] {
            let mut bundle = vec![0u8; 8]; // hsState + iTabID
            bundle.extend_from_slice(&wstr(rid));
            bundle.extend_from_slice(&wstr(name));
            wb_bin.extend_from_slice(&rec(BRT_BUNDLE_SH, &bundle));
        }

        let mut ws_view = Vec::new();
        ws_view.extend_from_slice(&(1u16 << 6).to_le_bytes()); // fSelected
        ws_view.extend_from_slice(&0u32.to_le_bytes()); // xlView normal
        ws_view.extend_from_slice(&0u32.to_le_bytes()); // rwTop
        ws_view.extend_from_slice(&0u32.to_le_bytes()); // colLeft
        ws_view.push(0x40); // icvHdr
        ws_view.push(0); // reserved2
        ws_view.extend_from_slice(&0u16.to_le_bytes()); // reserved3
        ws_view.extend_from_slice(&100u16.to_le_bytes()); // wScale
        ws_view.extend_from_slice(&0u16.to_le_bytes()); // wScaleNormal
        ws_view.extend_from_slice(&0u16.to_le_bytes()); // wScaleSLV
        ws_view.extend_from_slice(&0u16.to_le_bytes()); // wScalePLV
        ws_view.extend_from_slice(&0u32.to_le_bytes()); // iWbkView

        let mut selected_sheet = rec(BRT_BEGIN_WS_VIEWS, &[]);
        selected_sheet.extend_from_slice(&rec(BRT_BEGIN_WS_VIEW, &ws_view));
        selected_sheet.extend_from_slice(&rec(BRT_END_WS_VIEW, &[]));
        selected_sheet.extend_from_slice(&rec(BRT_END_WS_VIEWS, &[]));

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="worksheets/sheet1.bin"/><Relationship Id="rId2" Target="worksheets/sheet2.bin"/></Relationships>"#;

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (path, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", [].as_slice()),
            ("xl/worksheets/sheet2.bin", selected_sheet.as_slice()),
        ] {
            zw.start_file(path, opt).unwrap();
            zw.write_all(body).unwrap();
        }

        let wb = Workbook::open(&zw.finish().unwrap().into_inner()).unwrap();
        let metadata = wb.metadata();

        assert_eq!(wb.active_sheet_index(), Some(1));
        assert_eq!(wb.active_sheet_name(), Some("Summary"));
        assert_eq!(metadata.active_sheet, Some(1));
        assert_eq!(metadata.active_sheet_name, Some("Summary"));
        assert_eq!(
            <Workbook as crate::Reader>::metadata(&wb).active_sheet_name,
            Some("Summary")
        );
    }

    #[test]
    fn xlsb_shared_strings_part_lookup_is_case_insensitive() {
        // calamine/tests/issue_419.xlsb stores the shared-string part as
        // `xl/SharedStrings.bin`; the cell stream still references it through
        // BrtCellIsst.
        let mut wb_bin = vec![0u8; 8];
        wb_bin.extend_from_slice(&wstr("rId1"));
        wb_bin.extend_from_slice(&wstr("Sheet1"));
        let wb_bin = rec(BRT_BUNDLE_SH, &wb_bin);

        let mut item = vec![0u8];
        item.extend_from_slice(&wstr("Hello"));
        let sst = rec(BRT_SST_ITEM, &item);

        let mut sh = rec(BRT_ROW_HDR, &[0, 0, 0, 0]);
        let mut isst = vec![0u8; 8];
        isst.extend_from_slice(&0u32.to_le_bytes());
        sh.extend_from_slice(&rec(BRT_CELL_ISST, &isst));

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="worksheets/sheet1.bin"/></Relationships>"#;

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (name, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/SharedStrings.bin", sst.as_slice()),
            ("xl/worksheets/sheet1.bin", sh.as_slice()),
        ] {
            zw.start_file(name, opt).unwrap();
            zw.write_all(body).unwrap();
        }
        let bytes = zw.finish().unwrap().into_inner();

        let wb = Workbook::open(&bytes).unwrap();
        assert_eq!(
            wb.sheets[0].cell(0, 0),
            Some(&Cell::Text("Hello".to_string()))
        );
    }

    #[test]
    fn xlsb_styles_use_only_cell_xfs_for_cell_style_indexes() {
        // Real styles.bin parts can contain a non-cell XF group before
        // BrtBeginCellXFs. Cell records index only the XF records inside
        // BrtBeginCellXFs; collecting earlier BrtXF records shifts style
        // indexes and can turn plain numeric cells into dates.
        let mut wb_bin = vec![0u8; 8];
        wb_bin.extend_from_slice(&wstr("rId1"));
        wb_bin.extend_from_slice(&wstr("Sheet1"));
        let wb_bin = rec(BRT_BUNDLE_SH, &wb_bin);

        let mut fmt = 164u16.to_le_bytes().to_vec();
        fmt.extend_from_slice(&wstr("yyyy-mm-dd"));
        let mut styles = rec(BRT_FMT, &fmt);
        styles.extend_from_slice(&rec(0x0272, &1u32.to_le_bytes()));
        styles.extend_from_slice(&rec(BRT_XF, &xf(0)));
        styles.extend_from_slice(&rec(0x0273, &[]));
        styles.extend_from_slice(&rec(BRT_BEGIN_CELL_XFS, &2u32.to_le_bytes()));
        styles.extend_from_slice(&rec(BRT_XF, &xf(164)));
        styles.extend_from_slice(&rec(BRT_XF, &xf(0)));
        styles.extend_from_slice(&rec(BRT_END_CELL_XFS, &[]));

        let mut sh = rec(BRT_ROW_HDR, &[0, 0, 0, 0]);
        let mut date = vec![0u8; 8];
        date.extend_from_slice(&44_197.0f64.to_le_bytes());
        sh.extend_from_slice(&rec(BRT_CELL_REAL, &date));
        let mut number = vec![1, 0, 0, 0, 1, 0, 0, 0];
        number.extend_from_slice(&15.0f64.to_le_bytes());
        sh.extend_from_slice(&rec(BRT_CELL_REAL, &number));

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="worksheets/sheet1.bin"/></Relationships>"#;

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (name, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/styles.bin", styles.as_slice()),
            ("xl/worksheets/sheet1.bin", sh.as_slice()),
        ] {
            zw.start_file(name, opt).unwrap();
            zw.write_all(body).unwrap();
        }
        let bytes = zw.finish().unwrap().into_inner();

        let wb = Workbook::open(&bytes).unwrap();
        assert_eq!(wb.sheets[0].cell(0, 0), Some(&Cell::Date(44_197.0)));
        assert_eq!(wb.sheets[0].cell(0, 1), Some(&Cell::Number(15.0)));
    }

    #[test]
    fn xlsb_hyperlinks_surface_public_metadata() {
        let mut wb_bin = vec![0u8; 8]; // hsState + iTabID
        wb_bin.extend_from_slice(&wstr("rId1"));
        wb_bin.extend_from_slice(&wstr("Links"));
        let wb_bin = rec(BRT_BUNDLE_SH, &wb_bin);

        let url = "https://example.com/xlsb";
        let mut hlink = Vec::new();
        for value in [1u32, 2, 1, 1] {
            hlink.extend_from_slice(&value.to_le_bytes());
        }
        hlink.extend_from_slice(&wstr("rId2"));
        hlink.extend_from_slice(&wstr(""));
        hlink.extend_from_slice(&wstr("Open bid"));
        hlink.extend_from_slice(&wstr(""));
        let sheet = rec(0x01EE, &hlink);

        let wb_rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.bin"/></Relationships>"#;
        let sheet_rels = format!(
            r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="{url}" TargetMode="External"/></Relationships>"#
        );

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (path, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", wb_rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", sheet.as_slice()),
            ("xl/worksheets/_rels/sheet1.bin.rels", sheet_rels.as_bytes()),
        ] {
            zw.start_file(path, opt).unwrap();
            zw.write_all(body).unwrap();
        }

        let wb = Workbook::open(&zw.finish().unwrap().into_inner()).unwrap();

        assert_eq!(
            wb.sheets[0].hyperlinks(),
            &[(1, 1, url.to_string()), (2, 1, url.to_string())]
        );
    }

    #[test]
    fn xlsb_comments_surface_public_metadata() {
        const BRT_BEGIN_COMMENTS: u32 = 628;
        const BRT_END_COMMENTS: u32 = 629;
        const BRT_BEGIN_COMMENT_AUTHORS: u32 = 630;
        const BRT_END_COMMENT_AUTHORS: u32 = 631;
        const BRT_COMMENT_AUTHOR: u32 = 632;
        const BRT_BEGIN_COMMENT_LIST: u32 = 633;
        const BRT_END_COMMENT_LIST: u32 = 634;
        const BRT_BEGIN_COMMENT: u32 = 635;
        const BRT_END_COMMENT: u32 = 636;
        const BRT_COMMENT_TEXT: u32 = 637;

        let mut wb_bin = vec![0u8; 8]; // hsState + iTabID
        wb_bin.extend_from_slice(&wstr("rId1"));
        wb_bin.extend_from_slice(&wstr("Notes"));
        let wb_bin = rec(BRT_BUNDLE_SH, &wb_bin);

        let mut comment = Vec::new();
        comment.extend_from_slice(&0u32.to_le_bytes()); // iauthor
        for value in [2u32, 2, 3, 3] {
            comment.extend_from_slice(&value.to_le_bytes()); // UncheckedRfX: D3
        }
        comment.extend_from_slice(&[0u8; 16]); // guid

        let mut rich_text = vec![0x01]; // RichStr flags: fRichStr=1, fExtStr=0
        rich_text.extend_from_slice(&wstr("검토 필요"));
        rich_text.extend_from_slice(&0u32.to_le_bytes()); // zero StrRun entries

        let mut comments = rec(BRT_BEGIN_COMMENTS, &[]);
        comments.extend_from_slice(&rec(BRT_BEGIN_COMMENT_AUTHORS, &[]));
        comments.extend_from_slice(&rec(BRT_COMMENT_AUTHOR, &wstr("auditor")));
        comments.extend_from_slice(&rec(BRT_END_COMMENT_AUTHORS, &[]));
        comments.extend_from_slice(&rec(BRT_BEGIN_COMMENT_LIST, &[]));
        comments.extend_from_slice(&rec(BRT_BEGIN_COMMENT, &comment));
        comments.extend_from_slice(&rec(BRT_COMMENT_TEXT, &rich_text));
        comments.extend_from_slice(&rec(BRT_END_COMMENT, &[]));
        comments.extend_from_slice(&rec(BRT_END_COMMENT_LIST, &[]));
        comments.extend_from_slice(&rec(BRT_END_COMMENTS, &[]));

        let wb_rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.bin"/></Relationships>"#;
        let sheet_rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="../comments1.bin"/></Relationships>"#;

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (path, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", wb_rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", [].as_slice()),
            ("xl/worksheets/_rels/sheet1.bin.rels", sheet_rels.as_bytes()),
            ("xl/comments1.bin", comments.as_slice()),
        ] {
            zw.start_file(path, opt).unwrap();
            zw.write_all(body).unwrap();
        }

        let wb = Workbook::open(&zw.finish().unwrap().into_inner()).unwrap();
        let comments = wb.sheets[0].comments();

        assert_eq!(comments.len(), 1);
        assert_eq!(comments[0].row, 2);
        assert_eq!(comments[0].col, 3);
        assert_eq!(comments[0].text, "검토 필요");
        assert_eq!(comments[0].author.as_deref(), Some("auditor"));
    }

    #[test]
    fn xlsb_tables_surface_public_metadata() {
        const BRT_BEGIN_LIST: u32 = 288;
        const BRT_BEGIN_LIST_COL: u32 = 291;
        const BRT_BEGIN_LIST_COLS: u32 = 293;
        const BRT_LIST_PART: u32 = 550;
        const BRT_TABLE_STYLE_CLIENT: u32 = 649;

        let mut wb_bin = vec![0u8; 8]; // hsState + iTabID
        wb_bin.extend_from_slice(&wstr("rId1"));
        wb_bin.extend_from_slice(&wstr("Tables"));
        let wb_bin = rec(BRT_BUNDLE_SH, &wb_bin);

        let sheet = rec(BRT_LIST_PART, &wstr("rId2"));

        let mut begin_list = Vec::new();
        for value in [0u32, 2, 0, 2] {
            begin_list.extend_from_slice(&value.to_le_bytes()); // A1:C3
        }
        begin_list.extend_from_slice(&0u32.to_le_bytes()); // lt = LTRANGE
        begin_list.extend_from_slice(&1u32.to_le_bytes()); // idList
        begin_list.extend_from_slice(&1u32.to_le_bytes()); // crwHeader
        begin_list.extend_from_slice(&0u32.to_le_bytes()); // crwTotals
        begin_list.extend_from_slice(&0u32.to_le_bytes()); // table flags
        for _ in 0..6 {
            begin_list.extend_from_slice(&0xFFFF_FFFFu32.to_le_bytes()); // DXF ids
        }
        begin_list.extend_from_slice(&0u32.to_le_bytes()); // dwConnID
        begin_list.extend_from_slice(&null_wstr()); // stName
        begin_list.extend_from_slice(&wstr("SalesTable")); // stDisplayName
        begin_list.extend_from_slice(&wstr("")); // stComment
        begin_list.extend_from_slice(&null_wstr()); // stStyleHeader
        begin_list.extend_from_slice(&null_wstr()); // stStyleData
        begin_list.extend_from_slice(&null_wstr()); // stStyleAgg

        let mut table = rec(BRT_BEGIN_LIST, &begin_list);
        table.extend_from_slice(&rec(BRT_BEGIN_LIST_COLS, &3u32.to_le_bytes()));
        for (idx, caption) in ["Item", "Qty", "Total"].iter().enumerate() {
            let mut column = Vec::new();
            column.extend_from_slice(&((idx + 1) as u32).to_le_bytes()); // idField
            column.extend_from_slice(&0u32.to_le_bytes()); // ilta
            column.extend_from_slice(&0xFFFF_FFFFu32.to_le_bytes()); // nDxfHdr
            column.extend_from_slice(&0xFFFF_FFFFu32.to_le_bytes()); // nDxfInsertRow
            column.extend_from_slice(&0xFFFF_FFFFu32.to_le_bytes()); // nDxfAgg
            column.extend_from_slice(&0u32.to_le_bytes()); // idqsif
            column.extend_from_slice(&null_wstr()); // stName
            column.extend_from_slice(&wstr(caption)); // stCaption
            column.extend_from_slice(&null_wstr()); // stTotal
            column.extend_from_slice(&null_wstr()); // stStyleHeader
            column.extend_from_slice(&null_wstr()); // stStyleInsertRow
            column.extend_from_slice(&null_wstr()); // stStyleAgg
            table.extend_from_slice(&rec(BRT_BEGIN_LIST_COL, &column));
        }
        let mut style = 0b100u16.to_le_bytes().to_vec(); // fRowStripes
        style.extend_from_slice(&wstr("TableStyleMedium9"));
        table.extend_from_slice(&rec(BRT_TABLE_STYLE_CLIENT, &style));

        let wb_rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.bin"/></Relationships>"#;
        let sheet_rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/table" Target="../tables/table1.bin"/></Relationships>"#;

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (path, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", wb_rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", sheet.as_slice()),
            ("xl/worksheets/_rels/sheet1.bin.rels", sheet_rels.as_bytes()),
            ("xl/tables/table1.bin", table.as_slice()),
        ] {
            zw.start_file(path, opt).unwrap();
            zw.write_all(body).unwrap();
        }

        let wb = Workbook::open(&zw.finish().unwrap().into_inner()).unwrap();
        let tables = wb.sheets[0].tables();

        assert_eq!(tables.len(), 1);
        assert_eq!(tables[0].name, "SalesTable");
        assert_eq!(tables[0].range, (0, 0, 2, 2));
        assert_eq!(tables[0].columns, vec!["Item", "Qty", "Total"]);
        assert_eq!(tables[0].style.as_deref(), Some("TableStyleMedium9"));
    }

    #[test]
    fn xlsb_sheet_view_and_autofilter_surface_public_metadata() {
        const BRT_BEGIN_WS_VIEWS: u32 = 133;
        const BRT_END_WS_VIEWS: u32 = 134;
        const BRT_BEGIN_WS_VIEW: u32 = 137;
        const BRT_END_WS_VIEW: u32 = 138;
        const BRT_BEGIN_AFILTER: u32 = 161;
        const BRT_END_AFILTER: u32 = 162;
        const BRT_PANE: u32 = 151;

        let mut wb_bin = vec![0u8; 8]; // hsState + iTabID
        wb_bin.extend_from_slice(&wstr("rId1"));
        wb_bin.extend_from_slice(&wstr("View"));
        let wb_bin = rec(BRT_BUNDLE_SH, &wb_bin);

        let mut ws_view = Vec::new();
        let flags = (1u16 << 4) // fDspZeros
            | (1u16 << 5) // fRightToLeft
            | (1u16 << 6) // fSelected
            | (1u16 << 7) // fDspRuler
            | (1u16 << 8) // fDspGuts
            | (1u16 << 9); // fDefaultHdr
        ws_view.extend_from_slice(&flags.to_le_bytes());
        ws_view.extend_from_slice(&0u32.to_le_bytes()); // xlView normal
        ws_view.extend_from_slice(&0u32.to_le_bytes()); // rwTop
        ws_view.extend_from_slice(&0u32.to_le_bytes()); // colLeft
        ws_view.push(0x40); // icvHdr
        ws_view.push(0); // reserved2
        ws_view.extend_from_slice(&0u16.to_le_bytes()); // reserved3
        ws_view.extend_from_slice(&125u16.to_le_bytes()); // wScale
        ws_view.extend_from_slice(&0u16.to_le_bytes()); // wScaleNormal
        ws_view.extend_from_slice(&0u16.to_le_bytes()); // wScaleSLV
        ws_view.extend_from_slice(&0u16.to_le_bytes()); // wScalePLV
        ws_view.extend_from_slice(&0u32.to_le_bytes()); // iWbkView

        let mut pane = Vec::new();
        pane.extend_from_slice(&1.0f64.to_le_bytes()); // frozen rows
        pane.extend_from_slice(&2.0f64.to_le_bytes()); // frozen columns
        pane.extend_from_slice(&1u32.to_le_bytes()); // rwTop
        pane.extend_from_slice(&2u32.to_le_bytes()); // colLeft
        pane.extend_from_slice(&0u32.to_le_bytes()); // pnnAct
        pane.push(0x01); // fFrozen

        let mut autofilter = Vec::new();
        for value in [0u32, 9, 0, 3] {
            autofilter.extend_from_slice(&value.to_le_bytes());
        }

        let mut sheet = rec(BRT_BEGIN_WS_VIEWS, &[]);
        sheet.extend_from_slice(&rec(BRT_BEGIN_WS_VIEW, &ws_view));
        sheet.extend_from_slice(&rec(BRT_PANE, &pane));
        sheet.extend_from_slice(&rec(BRT_END_WS_VIEW, &[]));
        sheet.extend_from_slice(&rec(BRT_END_WS_VIEWS, &[]));
        sheet.extend_from_slice(&rec(BRT_BEGIN_AFILTER, &autofilter));
        sheet.extend_from_slice(&rec(BRT_END_AFILTER, &[]));

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="worksheets/sheet1.bin"/></Relationships>"#;

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (path, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", sheet.as_slice()),
        ] {
            zw.start_file(path, opt).unwrap();
            zw.write_all(body).unwrap();
        }

        let wb = Workbook::open(&zw.finish().unwrap().into_inner()).unwrap();
        let sheet = &wb.sheets[0];

        assert_eq!(
            sheet.sheet_view(),
            crate::SheetView {
                freeze: Some((1, 2)),
                hide_gridlines: true,
                zoom: Some(125),
                show_headers: Some(false),
                right_to_left: true,
            }
        );
        assert_eq!(sheet.autofilter_range(), Some((0, 0, 9, 3)));
    }

    #[test]
    fn xlsb_sheet_view_explicit_visible_headers_are_preserved() {
        const BRT_BEGIN_WS_VIEWS: u32 = 133;
        const BRT_END_WS_VIEWS: u32 = 134;
        const BRT_BEGIN_WS_VIEW: u32 = 137;
        const BRT_END_WS_VIEW: u32 = 138;

        let mut wb_bin = vec![0u8; 8]; // hsState + iTabID
        wb_bin.extend_from_slice(&wstr("rId1"));
        wb_bin.extend_from_slice(&wstr("View"));
        let wb_bin = rec(BRT_BUNDLE_SH, &wb_bin);

        let mut ws_view = Vec::new();
        let flags = (1u16 << 2) // display gridlines
            | (1u16 << 3); // display row/column headings
        ws_view.extend_from_slice(&flags.to_le_bytes());
        ws_view.extend_from_slice(&0u32.to_le_bytes()); // xlView normal
        ws_view.extend_from_slice(&0u32.to_le_bytes()); // rwTop
        ws_view.extend_from_slice(&0u32.to_le_bytes()); // colLeft
        ws_view.push(0x40); // icvHdr
        ws_view.push(0); // reserved2
        ws_view.extend_from_slice(&0u16.to_le_bytes()); // reserved3
        ws_view.extend_from_slice(&0u16.to_le_bytes()); // wScale
        ws_view.extend_from_slice(&0u16.to_le_bytes()); // wScaleNormal
        ws_view.extend_from_slice(&0u16.to_le_bytes()); // wScaleSLV
        ws_view.extend_from_slice(&0u16.to_le_bytes()); // wScalePLV
        ws_view.extend_from_slice(&0u32.to_le_bytes()); // iWbkView

        let mut sheet = rec(BRT_BEGIN_WS_VIEWS, &[]);
        sheet.extend_from_slice(&rec(BRT_BEGIN_WS_VIEW, &ws_view));
        sheet.extend_from_slice(&rec(BRT_END_WS_VIEW, &[]));
        sheet.extend_from_slice(&rec(BRT_END_WS_VIEWS, &[]));

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="worksheets/sheet1.bin"/></Relationships>"#;

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (path, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", sheet.as_slice()),
        ] {
            zw.start_file(path, opt).unwrap();
            zw.write_all(body).unwrap();
        }

        let wb = Workbook::open(&zw.finish().unwrap().into_inner()).unwrap();

        assert_eq!(wb.sheets[0].sheet_view().show_headers, Some(true));
    }

    #[test]
    fn xlsb_ws_prop_tab_color_surfaces_public_metadata() {
        const BRT_WS_PROP: u32 = 147;

        let mut wb_bin = vec![0u8; 8]; // hsState + iTabID
        wb_bin.extend_from_slice(&wstr("rId1"));
        wb_bin.extend_from_slice(&wstr("Color"));
        let wb_bin = rec(BRT_BUNDLE_SH, &wb_bin);

        let mut color = Vec::new();
        color.push(1 | (2 << 1)); // fValidRGB + xColorType=ARGB
        color.push(0); // index, ignored for ARGB
        color.extend_from_slice(&0i16.to_le_bytes()); // nTintAndShade
        color.extend_from_slice(&[0x12, 0x34, 0x56, 0xFF]); // RGB + alpha

        let mut ws_prop = Vec::new();
        ws_prop.extend_from_slice(&0u16.to_le_bytes()); // worksheet property flags
        ws_prop.push(0); // filter/conditional-format flags
        ws_prop.extend_from_slice(&color);
        ws_prop.extend_from_slice(&0xFFFF_FFFFu32.to_le_bytes()); // rwSync ignored
        ws_prop.extend_from_slice(&0xFFFF_FFFFu32.to_le_bytes()); // colSync ignored
        ws_prop.extend_from_slice(&wstr("")); // code name

        let sheet = rec(BRT_WS_PROP, &ws_prop);

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="worksheets/sheet1.bin"/></Relationships>"#;

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (path, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", sheet.as_slice()),
        ] {
            zw.start_file(path, opt).unwrap();
            zw.write_all(body).unwrap();
        }

        let wb = Workbook::open(&zw.finish().unwrap().into_inner()).unwrap();

        assert_eq!(wb.sheets[0].tab_color(), Some(Color::rgb(0x12, 0x34, 0x56)));
    }

    #[test]
    fn xlsb_sheet_protection_surfaces_public_metadata() {
        fn sheet_protection() -> Vec<u8> {
            let mut out = Vec::new();
            out.extend_from_slice(&0u16.to_le_bytes()); // protpwd
            for allowed in [
                1u32, // fLocked
                0,    // fObjects (not modeled)
                0,    // fScenarios (not modeled)
                1,    // fFormatCells
                0,    // fFormatColumns
                1,    // fFormatRows
                0,    // fInsertColumns
                1,    // fInsertRows
                1,    // fInsertHyperlinks
                0,    // fDeleteColumns
                1,    // fDeleteRows
                1,    // fSelLockedCells (not modeled)
                1,    // fSort
                1,    // fAutoFilter
                0,    // fPivotTables
                1,    // fSelUnlockedCells (not modeled)
            ] {
                out.extend_from_slice(&allowed.to_le_bytes());
            }
            out
        }

        let mut wb_bin = vec![0u8; 8]; // hsState + iTabID
        wb_bin.extend_from_slice(&wstr("rId1"));
        wb_bin.extend_from_slice(&wstr("Protected"));
        let wb_bin = rec(BRT_BUNDLE_SH, &wb_bin);

        let sheet = rec(BRT_SHEET_PROTECTION, &sheet_protection());

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="worksheets/sheet1.bin"/></Relationships>"#;

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (path, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", sheet.as_slice()),
        ] {
            zw.start_file(path, opt).unwrap();
            zw.write_all(body).unwrap();
        }

        let wb = Workbook::open(&zw.finish().unwrap().into_inner()).unwrap();
        let sheet = &wb.sheets[0];

        assert!(sheet.is_protected());
        assert_eq!(
            sheet.protection_options(),
            Some(crate::ProtectionOptions {
                sort: true,
                auto_filter: true,
                format_cells: true,
                format_rows: true,
                insert_rows: true,
                insert_hyperlinks: true,
                delete_rows: true,
                ..Default::default()
            })
        );

        let metadata = sheet.metadata();
        assert!(metadata.protected);
        assert_eq!(metadata.protection_options, sheet.protection_options());
    }

    #[test]
    fn xlsb_book_protection_surfaces_workbook_metadata() {
        const BRT_BOOK_PROTECTION: u32 = 534;

        fn book_protection(flags: u16) -> Vec<u8> {
            let mut out = Vec::new();
            out.extend_from_slice(&0u16.to_le_bytes()); // protpwdBook
            out.extend_from_slice(&0u16.to_le_bytes()); // protpwdRev
            out.extend_from_slice(&flags.to_le_bytes()); // wFlags
            out
        }

        let mut wb_bin = rec(BRT_BOOK_PROTECTION, &book_protection(0x0001));
        let mut sheet_ref = vec![0u8; 8]; // hsState + iTabID
        sheet_ref.extend_from_slice(&wstr("rId1"));
        sheet_ref.extend_from_slice(&wstr("LockedBook"));
        wb_bin.extend_from_slice(&rec(BRT_BUNDLE_SH, &sheet_ref));

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="worksheets/sheet1.bin"/></Relationships>"#;

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (path, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", [].as_slice()),
        ] {
            zw.start_file(path, opt).unwrap();
            zw.write_all(body).unwrap();
        }

        let wb = Workbook::open(&zw.finish().unwrap().into_inner()).unwrap();

        assert!(wb.is_structure_protected());
        assert!(wb.metadata().structure_protected);
    }

    #[test]
    fn xlsb_outline_records_surface_public_metadata() {
        fn row_hdr(row: u32, level: u8, collapsed: bool) -> Vec<u8> {
            let mut out = Vec::new();
            out.extend_from_slice(&row.to_le_bytes());
            out.extend_from_slice(&0u32.to_le_bytes()); // ixfe
            out.extend_from_slice(&0u16.to_le_bytes()); // miyRw
            let mut flags = u16::from(level) << 8;
            if collapsed {
                flags |= 1 << 11;
            }
            out.extend_from_slice(&flags.to_le_bytes());
            out
        }

        fn col_info(first: u32, last: u32, level: u8) -> Vec<u8> {
            let mut out = Vec::new();
            out.extend_from_slice(&first.to_le_bytes());
            out.extend_from_slice(&last.to_le_bytes());
            out.extend_from_slice(&0x08FFu32.to_le_bytes()); // default width
            out.extend_from_slice(&0u32.to_le_bytes()); // ixfe
            out.extend_from_slice(&(u16::from(level) << 8).to_le_bytes());
            out
        }

        let mut wb_bin = vec![0u8; 8]; // hsState + iTabID
        wb_bin.extend_from_slice(&wstr("rId1"));
        wb_bin.extend_from_slice(&wstr("Outline"));
        let wb_bin = rec(BRT_BUNDLE_SH, &wb_bin);

        let mut ws_prop = Vec::new();
        ws_prop.extend_from_slice(&0u16.to_le_bytes()); // summaries above/left
        ws_prop.push(0); // filter/conditional-format flags
        ws_prop.extend_from_slice(&[0u8; 8]); // no tab color
        ws_prop.extend_from_slice(&0xFFFF_FFFFu32.to_le_bytes()); // rwSync ignored
        ws_prop.extend_from_slice(&0xFFFF_FFFFu32.to_le_bytes()); // colSync ignored
        ws_prop.extend_from_slice(&wstr("")); // code name

        let mut sheet = rec(BRT_WS_PROP, &ws_prop);
        sheet.extend_from_slice(&rec(BRT_COL_INFO, &col_info(1, 3, 3)));
        sheet.extend_from_slice(&rec(BRT_ROW_HDR, &row_hdr(2, 2, true)));
        sheet.extend_from_slice(&rec(BRT_ROW_HDR, &row_hdr(3, 2, false)));

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="worksheets/sheet1.bin"/></Relationships>"#;

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (path, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", sheet.as_slice()),
        ] {
            zw.start_file(path, opt).unwrap();
            zw.write_all(body).unwrap();
        }

        let wb = Workbook::open(&zw.finish().unwrap().into_inner()).unwrap();
        let sheet = &wb.sheets[0];

        assert_eq!(sheet.row_outline_levels().get(&2), Some(&2));
        assert_eq!(sheet.row_outline_levels().get(&3), Some(&2));
        assert!(sheet.collapsed_rows().contains(&2));
        assert_eq!(sheet.col_outline_levels().get(&1), Some(&3));
        assert_eq!(sheet.col_outline_levels().get(&3), Some(&3));
        assert!(!sheet.outline_summary_below());
        assert!(!sheet.outline_summary_right());

        let metadata = sheet.metadata();
        assert_eq!(metadata.row_outline_levels.get(&2), Some(&2));
        assert_eq!(metadata.col_outline_levels.get(&1), Some(&3));
        assert!(metadata.collapsed_rows.contains(&2));
        assert!(!metadata.outline_summary_below);
        assert!(!metadata.outline_summary_right);
    }

    #[test]
    fn xlsb_page_setup_records_surface_public_metadata() {
        const BRT_MARGINS: u32 = 475;
        const BRT_PRINT_OPTIONS: u32 = 476;
        const BRT_PAGE_SETUP: u32 = 477;
        const BRT_BEGIN_HEADER_FOOTER: u32 = 478;
        const BRT_END_HEADER_FOOTER: u32 = 479;

        let mut wb_bin = vec![0u8; 8]; // hsState + iTabID
        wb_bin.extend_from_slice(&wstr("rId1"));
        wb_bin.extend_from_slice(&wstr("Print"));
        let wb_bin = rec(BRT_BUNDLE_SH, &wb_bin);

        let mut margins = Vec::new();
        for margin in [0.7, 0.8, 0.9, 1.0, 0.3, 0.4] {
            margins.extend_from_slice(&f64::to_le_bytes(margin));
        }

        let mut page_setup = Vec::new();
        page_setup.extend_from_slice(&9u32.to_le_bytes()); // iPaperSize: A4
        page_setup.extend_from_slice(&80u32.to_le_bytes()); // iScale
        page_setup.extend_from_slice(&600u32.to_le_bytes()); // iRes
        page_setup.extend_from_slice(&600u32.to_le_bytes()); // iVRes
        page_setup.extend_from_slice(&1u32.to_le_bytes()); // iCopies
        page_setup.extend_from_slice(&3i32.to_le_bytes()); // iPageStart
        page_setup.extend_from_slice(&2u32.to_le_bytes()); // iFitWidth
        page_setup.extend_from_slice(&1u32.to_le_bytes()); // iFitHeight
        page_setup.extend_from_slice(&((1u16 << 1) | (1u16 << 7)).to_le_bytes());
        page_setup.extend_from_slice(&0xFFFF_FFFFu32.to_le_bytes()); // szRelID null

        let mut header_footer = Vec::new();
        header_footer.extend_from_slice(&0u16.to_le_bytes());
        header_footer.extend_from_slice(&wstr("&CQuarterly"));
        header_footer.extend_from_slice(&wstr("&RPage &P"));
        for _ in 0..4 {
            header_footer.extend_from_slice(&0xFFFF_FFFFu32.to_le_bytes());
        }

        let mut sheet = rec(BRT_MARGINS, &margins);
        sheet.extend_from_slice(&rec(BRT_PRINT_OPTIONS, &0b1111u16.to_le_bytes()));
        sheet.extend_from_slice(&rec(BRT_PAGE_SETUP, &page_setup));
        sheet.extend_from_slice(&rec(BRT_BEGIN_HEADER_FOOTER, &header_footer));
        sheet.extend_from_slice(&rec(BRT_END_HEADER_FOOTER, &[]));

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="worksheets/sheet1.bin"/></Relationships>"#;

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (path, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", sheet.as_slice()),
        ] {
            zw.start_file(path, opt).unwrap();
            zw.write_all(body).unwrap();
        }

        let wb = Workbook::open(&zw.finish().unwrap().into_inner()).unwrap();
        let ps = wb.sheets[0].page_setup().expect("page setup");

        assert!(ps.landscape);
        assert_eq!(ps.paper_size, Some(9));
        assert_eq!(ps.scale, Some(80));
        assert_eq!(ps.fit_to_width, Some(2));
        assert_eq!(ps.fit_to_height, Some(1));
        assert_eq!(ps.first_page_number, Some(3));
        assert_eq!(ps.margins, Some((0.7, 0.8, 0.9, 1.0, 0.3, 0.4)));
        assert!(ps.center_horizontally);
        assert!(ps.center_vertically);
        assert!(wb.sheets[0].print_headings());
        assert!(wb.sheets[0].print_gridlines());
        assert_eq!(ps.header.as_deref(), Some("&CQuarterly"));
        assert_eq!(ps.footer.as_deref(), Some("&RPage &P"));
    }

    #[test]
    fn xlsb_data_validations_surface_public_metadata() {
        const BRT_DVAL: u32 = 64;
        const BRT_BEGIN_DVALS: u32 = 573;
        const BRT_END_DVALS: u32 = 574;
        const BRT_DVAL_LIST: u32 = 681;

        let mut wb_bin = vec![0u8; 8]; // hsState + iTabID
        wb_bin.extend_from_slice(&wstr("rId1"));
        wb_bin.extend_from_slice(&wstr("Validation"));
        let wb_bin = rec(BRT_BUNDLE_SH, &wb_bin);

        let mut dvals = Vec::new();
        dvals.extend_from_slice(&0u16.to_le_bytes()); // DVals flags
        dvals.extend_from_slice(&0u32.to_le_bytes()); // xLeft
        dvals.extend_from_slice(&0u32.to_le_bytes()); // yTop
        dvals.extend_from_slice(&0u32.to_le_bytes()); // unused
        dvals.extend_from_slice(&1u32.to_le_bytes()); // idvMac

        let mut dval = Vec::new();
        let flags = 3u32 // valType=list
            | (1u32 << 8) // fAllowBlank
            | (1u32 << 18); // fShowInputMsg
        dval.extend_from_slice(&flags.to_le_bytes());
        dval.extend_from_slice(&2i32.to_le_bytes()); // two UncheckedRfX ranges
        for value in [0u32, 0, 0, 0, 2, 3, 0, 0] {
            dval.extend_from_slice(&value.to_le_bytes()); // A1, A3:A4
        }
        dval.extend_from_slice(&0xFFFF_FFFFu32.to_le_bytes()); // strErrorTitle null
        dval.extend_from_slice(&0xFFFF_FFFFu32.to_le_bytes()); // strError null
        dval.extend_from_slice(&wstr("Pick")); // strPromptTitle
        dval.extend_from_slice(&wstr("Choose one")); // strPrompt
        dval.extend_from_slice(&0u32.to_le_bytes()); // formula1.cce
        dval.extend_from_slice(&0u32.to_le_bytes()); // formula1.cb
        dval.extend_from_slice(&0u32.to_le_bytes()); // formula2.cce
        dval.extend_from_slice(&0u32.to_le_bytes()); // formula2.cb

        let mut sheet = rec(BRT_BEGIN_DVALS, &dvals);
        sheet.extend_from_slice(&rec(BRT_DVAL_LIST, &wstr("\"Yes,No\"")));
        sheet.extend_from_slice(&rec(BRT_DVAL, &dval));
        sheet.extend_from_slice(&rec(BRT_END_DVALS, &[]));

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="worksheets/sheet1.bin"/></Relationships>"#;

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (path, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", sheet.as_slice()),
        ] {
            zw.start_file(path, opt).unwrap();
            zw.write_all(body).unwrap();
        }

        let wb = Workbook::open(&zw.finish().unwrap().into_inner()).unwrap();
        let validations = wb.sheets[0].data_validations();

        assert_eq!(validations.len(), 2);
        assert_eq!(validations[0].sqref, (0, 0, 0, 0));
        assert_eq!(validations[1].sqref, (2, 0, 3, 0));
        assert_eq!(validations[0].kind, crate::DvKind::List);
        assert_eq!(validations[0].operator, crate::DvOp::Between);
        assert_eq!(validations[0].formula1, "\"Yes,No\"");
        assert!(validations[0].allow_blank);
        assert!(validations[0].show_input_message);
        assert!(!validations[0].show_error_message);
        assert_eq!(
            validations[0].prompt.as_ref(),
            Some(&("Pick".to_string(), "Choose one".to_string()))
        );
    }

    #[test]
    fn xlsb_data_validations_consume_ranges_beyond_retained_cap() {
        let mut dval = Vec::new();
        dval.extend_from_slice(&3u32.to_le_bytes()); // valType=list
        dval.extend_from_slice(&((MAX_DVAL_RANGES + 1) as i32).to_le_bytes());
        for _ in 0..=MAX_DVAL_RANGES {
            for value in [0u32, 0, 0, 0] {
                dval.extend_from_slice(&value.to_le_bytes());
            }
        }
        for _ in 0..4 {
            dval.extend_from_slice(&0xFFFF_FFFFu32.to_le_bytes());
        }
        dval.extend_from_slice(&0u32.to_le_bytes()); // formula1.cce
        dval.extend_from_slice(&0u32.to_le_bytes()); // formula1.cb
        dval.extend_from_slice(&0u32.to_le_bytes()); // formula2.cce
        dval.extend_from_slice(&0u32.to_le_bytes()); // formula2.cb

        let validations = parse_dval(&dval, Some("\"A\"".to_string()));

        assert_eq!(validations.len(), MAX_DVAL_RANGES);
        assert_eq!(validations[0].kind, crate::DvKind::List);
        assert_eq!(validations[0].formula1, "\"A\"");
    }

    #[test]
    fn xlsb_defined_name_is_read_from_brt_name_record() {
        let mut name = Vec::new();
        name.extend_from_slice(&0u32.to_le_bytes()); // flags: visible, non-built-in
        name.push(0); // chKey
        name.extend_from_slice(&0xFFFF_FFFFu32.to_le_bytes()); // workbook scope
        name.extend_from_slice(&wstr("Answer"));
        name.extend_from_slice(&3u32.to_le_bytes()); // cce
        name.extend_from_slice(&[0x1E, 42, 0]); // PtgInt(42)
        name.extend_from_slice(&0u32.to_le_bytes()); // formula cb
        name.extend_from_slice(&0xFFFF_FFFFu32.to_le_bytes()); // null comment

        let mut wb_bin = rec(39, &name);
        let mut bundle = vec![0u8; 8]; // hsState + iTabID
        bundle.extend_from_slice(&wstr("rId1"));
        bundle.extend_from_slice(&wstr("S1"));
        wb_bin.extend_from_slice(&rec(BRT_BUNDLE_SH, &bundle));

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="worksheets/sheet1.bin"/></Relationships>"#;
        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (path, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", [].as_slice()),
        ] {
            zw.start_file(path, opt).unwrap();
            zw.write_all(body).unwrap();
        }

        let wb = Workbook::open(&zw.finish().unwrap().into_inner()).unwrap();
        assert_eq!(
            wb.defined_names(),
            &[("Answer".to_string(), "42".to_string())]
        );
    }

    #[test]
    fn xlsb_sheet_local_builtin_names_surface_page_setup() {
        fn name_builtin(name_text: &str, sheet_index: u32, rgce: &[u8]) -> Vec<u8> {
            let mut name = Vec::new();
            name.extend_from_slice(&0x20u32.to_le_bytes()); // flags: built-in
            name.push(0); // chKey: no macro shortcut
            name.extend_from_slice(&sheet_index.to_le_bytes());
            name.extend_from_slice(&wstr(name_text));
            name.extend_from_slice(&(rgce.len() as u32).to_le_bytes());
            name.extend_from_slice(rgce);
            name.extend_from_slice(&0u32.to_le_bytes()); // formula cb
            name.extend_from_slice(&0xFFFF_FFFFu32.to_le_bytes()); // null comment
            name
        }

        fn ptg_area(r0: u32, c0: u16, r1: u32, c1: u16) -> Vec<u8> {
            let mut rgce = vec![0x25]; // PtgArea with BIFF12 row widths
            rgce.extend_from_slice(&r0.to_le_bytes());
            rgce.extend_from_slice(&r1.to_le_bytes());
            rgce.extend_from_slice(&c0.to_le_bytes());
            rgce.extend_from_slice(&c1.to_le_bytes());
            rgce
        }

        let print_area = ptg_area(1, 1, 5, 3);
        let mut print_titles = ptg_area(0, 0, 1, MAX_XLSB_COL_INDEX as u16);
        print_titles.extend_from_slice(&ptg_area(0, 0, 1_048_575, 2));
        print_titles.push(0x10); // PtgUnion

        let mut wb_bin = rec(BRT_NAME, &name_builtin("_xlnm.Print_Area", 0, &print_area));
        wb_bin.extend_from_slice(&rec(
            BRT_NAME,
            &name_builtin("_xlnm.Print_Titles", 0, &print_titles),
        ));
        let mut bundle = vec![0u8; 8]; // hsState + iTabID
        bundle.extend_from_slice(&wstr("rId1"));
        bundle.extend_from_slice(&wstr("S1"));
        wb_bin.extend_from_slice(&rec(BRT_BUNDLE_SH, &bundle));

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="worksheets/sheet1.bin"/></Relationships>"#;
        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (path, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", [].as_slice()),
        ] {
            zw.start_file(path, opt).unwrap();
            zw.write_all(body).unwrap();
        }

        let wb = Workbook::open(&zw.finish().unwrap().into_inner()).unwrap();

        assert!(wb.defined_names().is_empty());
        let ps = wb.sheets[0].page_setup().expect("page setup");
        assert_eq!(ps.print_area, Some((1, 1, 5, 3)));
        assert_eq!(ps.repeat_rows, Some((0, 1)));
        assert_eq!(ps.repeat_cols, Some((0, 2)));
    }

    #[test]
    fn xlsb_sheet_local_filter_database_name_surfaces_autofilter() {
        fn name_builtin(name_text: &str, sheet_index: u32, rgce: &[u8]) -> Vec<u8> {
            let mut name = Vec::new();
            name.extend_from_slice(&0x20u32.to_le_bytes()); // flags: built-in
            name.push(0); // chKey: no macro shortcut
            name.extend_from_slice(&sheet_index.to_le_bytes());
            name.extend_from_slice(&wstr(name_text));
            name.extend_from_slice(&(rgce.len() as u32).to_le_bytes());
            name.extend_from_slice(rgce);
            name.extend_from_slice(&0u32.to_le_bytes()); // formula cb
            name.extend_from_slice(&0xFFFF_FFFFu32.to_le_bytes()); // null comment
            name
        }

        let mut filter_area = vec![0x25]; // PtgArea with BIFF12 row widths
        filter_area.extend_from_slice(&2u32.to_le_bytes());
        filter_area.extend_from_slice(&9u32.to_le_bytes());
        filter_area.extend_from_slice(&1u16.to_le_bytes());
        filter_area.extend_from_slice(&4u16.to_le_bytes());

        let mut wb_bin = rec(
            BRT_NAME,
            &name_builtin("_xlnm._FilterDatabase", 0, &filter_area),
        );
        let mut bundle = vec![0u8; 8]; // hsState + iTabID
        bundle.extend_from_slice(&wstr("rId1"));
        bundle.extend_from_slice(&wstr("S1"));
        wb_bin.extend_from_slice(&rec(BRT_BUNDLE_SH, &bundle));

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="worksheets/sheet1.bin"/></Relationships>"#;
        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (path, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", [].as_slice()),
        ] {
            zw.start_file(path, opt).unwrap();
            zw.write_all(body).unwrap();
        }

        let wb = Workbook::open(&zw.finish().unwrap().into_inner()).unwrap();

        assert!(wb.defined_names().is_empty());
        assert_eq!(wb.sheets[0].autofilter_range(), Some((2, 1, 9, 4)));
        assert_eq!(wb.sheets[0].page_setup(), None);
    }

    #[test]
    fn xlsb_doc_properties_surface_through_workbook_metadata() {
        let mut wb_bin = vec![0u8; 8]; // hsState + iTabID
        wb_bin.extend_from_slice(&wstr("rId1"));
        wb_bin.extend_from_slice(&wstr("S1"));
        let wb_bin = rec(BRT_BUNDLE_SH, &wb_bin);

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="worksheets/sheet1.bin"/></Relationships>"#;
        let core = r#"<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/"><dc:title>Binary Report</dc:title><dc:subject>Procurement</dc:subject><dc:creator>rxls xlsb</dc:creator><cp:keywords>bid,binary</cp:keywords><dc:description>XLSB public metadata</dc:description><cp:lastModifiedBy>reviewer</cp:lastModifiedBy><dcterms:created>2026-06-24T01:02:03Z</dcterms:created></cp:coreProperties>"#;
        let app = r#"<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"><Application>Excel</Application><Company>ACME XLSB</Company></Properties>"#;

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (path, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", [].as_slice()),
            ("docProps/core.xml", core.as_bytes()),
            ("docProps/app.xml", app.as_bytes()),
        ] {
            zw.start_file(path, opt).unwrap();
            zw.write_all(body).unwrap();
        }

        let wb = Workbook::open(&zw.finish().unwrap().into_inner()).unwrap();
        let metadata = wb.metadata();
        assert_eq!(metadata.properties.title.as_deref(), Some("Binary Report"));
        assert_eq!(metadata.properties.subject.as_deref(), Some("Procurement"));
        assert_eq!(metadata.properties.creator.as_deref(), Some("rxls xlsb"));
        assert_eq!(metadata.properties.keywords.as_deref(), Some("bid,binary"));
        assert_eq!(
            metadata.properties.description.as_deref(),
            Some("XLSB public metadata")
        );
        assert_eq!(
            metadata.properties.last_modified_by.as_deref(),
            Some("reviewer")
        );
        assert_eq!(
            metadata.properties.created.as_deref(),
            Some("2026-06-24T01:02:03Z")
        );
        assert_eq!(metadata.properties.company.as_deref(), Some("ACME XLSB"));
    }

    #[test]
    fn xlsb_formula_is_decoded() {
        // BrtFmlaNum: cell(8) + xnum 30.0 + grbitFlags(2) + cce(4) + rgce. The rgce
        // is BIFF12 SUM(A1:A2): PtgArea (u32 rows) + PtgFuncVar(SUM, 1 arg).
        let mut p = vec![0u8; 8]; // col + style
        p.extend_from_slice(&30.0f64.to_le_bytes()); // xnum
        p.extend_from_slice(&[0, 0]); // grbitFlags
        let rgce: Vec<u8> = vec![
            0x25, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, // PtgArea A1:A2 (r0=0,r1=1,c0=0,c1=0)
            0x22, 0x01, 0x04, 0x00, // PtgFuncVar SUM(1 arg)
        ];
        p.extend_from_slice(&(rgce.len() as u32).to_le_bytes()); // cce
        p.extend_from_slice(&rgce);
        let mut cells = Vec::new();
        let mut budget = crate::MAX_TEXT_BYTES;
        decode_cell(
            BRT_FMLA_NUM,
            &p,
            0,
            0,
            0,
            &[],
            &Styles::default(),
            false,
            &mut cells,
            &mut budget,
        );
        assert_eq!(cells.len(), 1);
        match &cells[0].value {
            Cell::Formula { formula, cached } => {
                assert_eq!(formula, "SUM(A1:A2)");
                assert_eq!(**cached, Cell::Number(30.0));
            }
            o => panic!("expected a formula cell, got {o:?}"),
        }
    }

    #[test]
    fn xlsb_formula_string_is_decoded() {
        // BrtFmlaString stores its cached string inside the formula cell record
        // (`XLWideString`), unlike BIFF8 `.xls` which uses a following STRING
        // record for string-result formulas.
        let mut p = vec![0u8; 8]; // col + style
        p.extend_from_slice(&wstr("cached"));
        p.extend_from_slice(&[0, 0]); // grbitFlags
        let rgce = vec![0x17, 1, 0, b'x']; // PtgStr("x")
        p.extend_from_slice(&(rgce.len() as u32).to_le_bytes());
        p.extend_from_slice(&rgce);

        let mut cells = Vec::new();
        let mut budget = crate::MAX_TEXT_BYTES;
        decode_cell(
            BRT_FMLA_STRING,
            &p,
            0,
            0,
            0,
            &[],
            &Styles::default(),
            false,
            &mut cells,
            &mut budget,
        );

        assert_eq!(cells.len(), 1);
        assert_eq!(cells[0].text, "cached");
        match &cells[0].value {
            Cell::Formula { formula, cached } => {
                assert_eq!(formula, "\"x\"");
                assert_eq!(**cached, Cell::Text("cached".to_string()));
            }
            other => panic!("expected a string-result formula cell, got {other:?}"),
        }
    }

    #[test]
    fn xlsb_formula_string_budget_exhaustion_sets_partial_signal() {
        // BrtFmlaString: cell(8) + XLWideString cached value + grbitFlags(2) +
        // CellParsedFormula. If the cached display text cannot fit in the shared
        // workbook text budget, parsing must leave a partial-extraction signal.
        let mut p = vec![0u8; 8]; // col + style
        p.extend_from_slice(&wstr("toolong"));
        p.extend_from_slice(&[0, 0]); // grbitFlags
        let rgce = vec![0x17, 1, 0, b'x']; // PtgStr("x")
        p.extend_from_slice(&(rgce.len() as u32).to_le_bytes());
        p.extend_from_slice(&rgce);

        let sh = rec(BRT_FMLA_STRING, &p);
        let mut budget = "toolong".len() - 1;
        let (cells, _merges, _hyperlinks, _metadata) = parse_sheet(
            &sh,
            &[],
            &Styles::default(),
            false,
            &HashMap::new(),
            &mut budget,
        );

        assert!(cells.is_empty());
        assert_eq!(budget, 0);
    }

    #[test]
    fn bundle_sh_hsstate_visibility() {
        // BrtBundleSh: hsState:u32 (0 visible / 1 hidden / 2 veryHidden), iTabID:u32,
        // strRelID, strName. Build one with hsState=1 and assert it parses as hidden.
        let bundle = |hs_state: u32| {
            let mut p = hs_state.to_le_bytes().to_vec(); // hsState
            p.extend_from_slice(&0u32.to_le_bytes()); // iTabID
            p.extend_from_slice(&wstr("rId1")); // strRelID
            p.extend_from_slice(&wstr("S1")); // strName
            let (names, _, _, _, _, _) = parse_workbook(&rec(BRT_BUNDLE_SH, &p));
            names
        };
        assert_eq!(bundle(0), vec![("S1".to_string(), "rId1".to_string(), 0)]);
        assert_eq!(bundle(1), vec![("S1".to_string(), "rId1".to_string(), 1)]);
        assert_eq!(bundle(2), vec![("S1".to_string(), "rId1".to_string(), 2)]);
    }

    #[test]
    fn xlsb_hidden_sheet_end_to_end() {
        // workbook.bin: one BrtBundleSh with hsState=1 (hidden) for "Secret".
        let mut wb_bin = 1u32.to_le_bytes().to_vec(); // hsState = 1 (hidden)
        wb_bin.extend_from_slice(&0u32.to_le_bytes()); // iTabID
        wb_bin.extend_from_slice(&wstr("rId1")); // strRelID
        wb_bin.extend_from_slice(&wstr("Secret")); // strName
        let wb_bin = rec(BRT_BUNDLE_SH, &wb_bin);

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Target="worksheets/sheet1.bin"/></Relationships>"#;

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (name, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", [].as_slice()),
        ] {
            zw.start_file(name, opt).unwrap();
            zw.write_all(body).unwrap();
        }
        let bytes = zw.finish().unwrap().into_inner();

        let wb = Workbook::open(&bytes).unwrap();
        assert_eq!(wb.sheets.len(), 1);
        assert_eq!(wb.sheets[0].name, "Secret");
        assert!(wb.sheets[0].is_hidden(), "hsState=1 => hidden");
        assert!(!wb.sheets[0].is_very_hidden());
    }

    #[test]
    fn xlsb_bundle_sheet_preserves_sheet_types_end_to_end() {
        let bundle = |name: &str, rid: Option<&str>, hs_state: u32, tab_id: u32| {
            let mut p = hs_state.to_le_bytes().to_vec();
            p.extend_from_slice(&tab_id.to_le_bytes());
            if let Some(rid) = rid {
                p.extend_from_slice(&wstr(rid));
            } else {
                p.extend_from_slice(&0xFFFF_FFFFu32.to_le_bytes());
            }
            p.extend_from_slice(&wstr(name));
            rec(BRT_BUNDLE_SH, &p)
        };

        let mut wb_bin = bundle("Data", Some("rId1"), 0, 1);
        wb_bin.extend_from_slice(&bundle("Chart", Some("rId2"), 0, 2));
        wb_bin.extend_from_slice(&bundle("Macro", Some("rId3"), 1, 3));
        wb_bin.extend_from_slice(&bundle("Dialog", Some("rId4"), 2, 4));
        wb_bin.extend_from_slice(&bundle("Module", None, 2, 5));

        let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
            <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.bin"/>
            <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet" Target="chartsheets/sheet1.bin"/>
            <Relationship Id="rId3" Type="http://schemas.microsoft.com/office/2006/relationships/xlMacrosheet" Target="macrosheets/sheet1.bin"/>
            <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/dialogsheet" Target="dialogsheets/sheet1.bin"/>
        </Relationships>"#;

        let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let opt = SimpleFileOptions::default();
        for (name, body) in [
            ("xl/workbook.bin", wb_bin.as_slice()),
            ("xl/_rels/workbook.bin.rels", rels.as_bytes()),
            ("xl/worksheets/sheet1.bin", [].as_slice()),
        ] {
            zw.start_file(name, opt).unwrap();
            zw.write_all(body).unwrap();
        }
        let bytes = zw.finish().unwrap().into_inner();

        let wb = Workbook::open(&bytes).unwrap();
        assert_eq!(
            wb.sheets_metadata(),
            vec![
                SheetMetadata {
                    name: "Data".to_string(),
                    typ: SheetType::WorkSheet,
                    visible: SheetVisible::Visible,
                },
                SheetMetadata {
                    name: "Chart".to_string(),
                    typ: SheetType::ChartSheet,
                    visible: SheetVisible::Visible,
                },
                SheetMetadata {
                    name: "Macro".to_string(),
                    typ: SheetType::MacroSheet,
                    visible: SheetVisible::Hidden,
                },
                SheetMetadata {
                    name: "Dialog".to_string(),
                    typ: SheetType::DialogSheet,
                    visible: SheetVisible::VeryHidden,
                },
                SheetMetadata {
                    name: "Module".to_string(),
                    typ: SheetType::Vba,
                    visible: SheetVisible::VeryHidden,
                },
            ]
        );
        assert_eq!(
            wb.worksheets()
                .into_iter()
                .map(|(name, _)| name)
                .collect::<Vec<_>>(),
            vec!["Data".to_string()]
        );
    }

    #[test]
    fn parses_date1904_flag() {
        // BrtWbProp with bit 0 set => 1904 date system (matches calamine/MS-XLSB).
        let on = rec(BRT_WB_PROP, &[0x01, 0, 0, 0]);
        assert!(parse_workbook(&on).1, "bit 0 set => 1904");
        let off = rec(BRT_WB_PROP, &[0x00, 0, 0, 0]);
        assert!(!parse_workbook(&off).1, "bit 0 clear => 1900");
    }
}