xberg 1.0.10

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 101 formats and 371 programming languages via tree-sitter code intelligence with async/sync APIs.
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
//! Apple Numbers (.numbers) extractor.

use crate::Result;
use crate::core::config::ExtractionConfig;
use crate::error::XbergError;
use crate::extractors::iwork::{
    IwaExpansionBudget, collect_iwa_paths, extract_metadata_from_zip, read_iwa_file, validate_iwork_zip,
};
use crate::extractors::security::{SecurityBudget, SecurityLimits};
use crate::plugins::{InternalDocumentExtractor, Plugin};
use crate::types::internal::InternalDocument;
use crate::types::internal_builder::InternalDocumentBuilder;
use async_trait::async_trait;
use std::collections::HashMap;

/// Apple Numbers spreadsheet extractor.
///
/// Supports `.numbers` files (modern iWork format, 2013+).
///
/// Extracts table names and cell string values from the IWA container:
/// ZIP → Snappy → typed protobuf table storage. Output preserves the source
/// row and column structure.
#[cfg_attr(alef, alef(skip))]
pub struct NumbersExtractor;

impl NumbersExtractor {
    pub(crate) fn new() -> Self {
        Self
    }
}

impl Default for NumbersExtractor {
    fn default() -> Self {
        Self::new()
    }
}

impl Plugin for NumbersExtractor {
    fn name(&self) -> &str {
        "iwork-numbers-extractor"
    }

    fn version(&self) -> String {
        env!("CARGO_PKG_VERSION").to_string()
    }

    fn initialize(&self) -> Result<()> {
        Ok(())
    }

    fn shutdown(&self) -> Result<()> {
        Ok(())
    }

    fn description(&self) -> &str {
        "Apple Numbers (.numbers) text extraction via IWA container parser"
    }

    fn author(&self) -> &str {
        "Xberg Team"
    }
}

/// Parsed Numbers tables and optional metadata.
struct NumbersData {
    /// Tables from every document sheet, in display order.
    tables: Vec<(String, Vec<Vec<String>>)>,
    /// Metadata extracted from the ZIP archive.
    metadata: crate::types::metadata::Metadata,
}

/// Parse a Numbers ZIP and extract all text from IWA files.
///
/// Numbers stores its content across many IWA files:
/// - `Index/CalculationEngine.iwa` — formula cells and sheet data
/// - `Index/Document.iwa` — document structure and sheet names
/// - `tables/DataStore.iwa` — table cell string values
///
/// The IWA object type identifiers below are stable identifiers embedded in
/// Numbers' archive metadata, not protobuf field numbers. ~keep
const DOCUMENT_ARCHIVE_TYPE: u32 = 1;
const SHEET_ARCHIVE_TYPE: u32 = 2;
const TABLE_INFO_ARCHIVE_TYPE: u32 = 6000;
const TABLE_MODEL_ARCHIVE_TYPE: u32 = 6001;
const TILE_ARCHIVE_TYPE: u32 = 6002;
const TABLE_DATA_LIST_TYPES: &[u32] = &[6005, 6201];
const RICH_TEXT_PAYLOAD_ARCHIVE_TYPE: u32 = 6218;
const TEXT_STORAGE_ARCHIVE_TYPE: u32 = 2001;
const CELL_STORAGE_VERSION: u8 = 5;
const EMPTY_CELL_TYPE: u8 = 0;
const NUMBER_CELL_TYPE: u8 = 2;
const TEXT_CELL_TYPE: u8 = 3;
const DATE_CELL_TYPE: u8 = 5;
const BOOLEAN_CELL_TYPE: u8 = 6;
const DURATION_CELL_TYPE: u8 = 7;
const ERROR_CELL_TYPE: u8 = 8;
const RICH_TEXT_CELL_TYPE: u8 = 9;
const CURRENCY_CELL_TYPE: u8 = 10;
const CELL_HEADER_LENGTH: usize = 12;
const CELL_DECIMAL_FLAG: u32 = 0x1;
const CELL_DOUBLE_FLAG: u32 = 0x2;
const CELL_DATE_FLAG: u32 = 0x4;
const CELL_STRING_FLAG: u32 = 0x8;
const CELL_RICH_TEXT_FLAG: u32 = 0x10;
const DECIMAL_VALUE_LENGTH: usize = 16;
const SCALAR_VALUE_LENGTH: usize = 8;
const STRING_KEY_LENGTH: usize = 4;
const OFFSET_ENTRY_LENGTH: usize = 2;
const DEFAULT_TILE_SIZE: usize = 256;
const WIDE_OFFSET_SCALE: usize = 4;
const DECIMAL128_EXPONENT_BIAS: i32 = 0x1820;
const IWORK_EPOCH_TO_UNIX_SECONDS: i64 = 978_307_200;
const SECONDS_PER_DAY: i64 = 86_400;

const OLD_V1_MAX_VERSION: u8 = 1;
const OLD_V3_MAX_VERSION: u8 = 3;
const OLD_V1_HEADER_LENGTH: usize = 8;
const OLD_CELL_TYPE_V1_V3_OFFSET: usize = 2;
const OLD_CELL_TYPE_V4_OFFSET: usize = 1;
const OLD_FLAGS_OFFSET: usize = 4;
const OLD_STRING_FLAG: u32 = 0x10;
const OLD_DOUBLE_FLAG: u32 = 0x20;
const OLD_DATE_FLAG: u32 = 0x40;
const OLD_RICH_TEXT_FLAG: u32 = 0x200;
const OLD_CELL_STYLE_FLAG: u32 = 0x2;
const OLD_TEXT_STYLE_FLAG: u32 = 0x80;
const OLD_CONDITIONAL_STYLE_FLAG: u32 = 0x400;
const OLD_CONDITIONAL_RULE_FLAG: u32 = 0x800;
const OLD_CURRENT_FORMAT_FLAG: u32 = 0x4;
const OLD_FORMULA_FLAG: u32 = 0x8;
const OLD_FORMULA_ERROR_FLAG: u32 = 0x100;
const OLD_COMMENT_FLAG: u32 = 0x1000;
const OLD_IMPORT_WARNING_FLAG: u32 = 0x2000;
const OLD_NUMBER_FORMAT_FLAG: u32 = 0x10000;
const OLD_CURRENCY_FORMAT_FLAG: u32 = 0x80000;
const OLD_DATE_FORMAT_FLAG: u32 = 0x20000;
const OLD_DURATION_FORMAT_FLAG: u32 = 0x40000;
const OLD_CONTROL_FORMAT_FLAG: u32 = 0x100000;
const OLD_CUSTOM_FORMAT_FLAG: u32 = 0x200000;
const OLD_BASE_FORMAT_FLAG: u32 = 0x400000;
const OLD_CHOICE_FORMAT_FLAG: u32 = 0x800000;

mod field {
    pub(super) const ARCHIVE_IDENTIFIER: u32 = 1;
    pub(super) const ARCHIVE_MESSAGE_INFO: u32 = 2;
    pub(super) const ARCHIVE_SHOULD_MERGE: u32 = 3;
    pub(super) const MESSAGE_TYPE: u32 = 1;
    pub(super) const MESSAGE_LENGTH: u32 = 3;
    pub(super) const MESSAGE_BASE_INDEX: u32 = 7;
    pub(super) const DOCUMENT_SHEET: u32 = 1;
    pub(super) const SHEET_DRAWABLE: u32 = 2;
    pub(super) const TABLE_INFO_MODEL: u32 = 2;
    pub(super) const TABLE_DATA_STORE: u32 = 4;
    pub(super) const TABLE_ROWS: u32 = 6;
    pub(super) const TABLE_COLUMNS: u32 = 7;
    pub(super) const TABLE_NAME: u32 = 8;
    pub(super) const DATA_STORE_TILES: u32 = 3;
    pub(super) const DATA_STORE_STRINGS: u32 = 4;
    pub(super) const DATA_STORE_RICH_TEXT: u32 = 17;
    pub(super) const DATA_LIST_ENTRY: u32 = 3;
    pub(super) const DATA_LIST_ENTRY_KEY: u32 = 1;
    pub(super) const DATA_LIST_ENTRY_STRING: u32 = 3;
    pub(super) const DATA_LIST_ENTRY_RICH_TEXT: u32 = 9;
    pub(super) const RICH_TEXT_STORAGE: u32 = 1;
    pub(super) const TEXT_STORAGE_TEXT: u32 = 3;
    pub(super) const TILE_STORAGE_TILE: u32 = 1;
    pub(super) const TILE_STORAGE_SIZE: u32 = 2;
    pub(super) const TILE_INDEX: u32 = 1;
    pub(super) const TILE_REFERENCE: u32 = 2;
    pub(super) const TILE_ROW_INFO: u32 = 5;
    pub(super) const ROW_INDEX: u32 = 1;
    pub(super) const ROW_CELL_STORAGE: u32 = 6;
    pub(super) const ROW_CELL_OFFSETS: u32 = 7;
    pub(super) const ROW_CELL_STORAGE_PRE_BNC: u32 = 3;
    pub(super) const ROW_CELL_OFFSETS_PRE_BNC: u32 = 4;
    pub(super) const ROW_HAS_WIDE_OFFSETS: u32 = 8;
}

mod wire {
    pub(super) const VARINT: u64 = 0;
    pub(super) const FIXED64: u64 = 1;
    pub(super) const LENGTH_DELIMITED: u64 = 2;
    pub(super) const FIXED32: u64 = 5;
    pub(super) const FIELD_NUMBER_SHIFT: u32 = 3;
    pub(super) const TYPE_MASK: u64 = 0x7;
    pub(super) const REFERENCE_TAG: u64 = 8;
    pub(super) const FIXED64_LENGTH: u64 = 8;
    pub(super) const FIXED32_LENGTH: u64 = 4;
    pub(super) const VARINT_VALUE_MASK: u8 = 0x7f;
    pub(super) const VARINT_CONTINUATION_BIT: u8 = 0x80;
    pub(super) const VARINT_BITS_PER_BYTE: u32 = 7;
    pub(super) const VARINT_MAX_BITS: u32 = 64;
}

#[derive(Debug)]
struct IwaObject {
    object_type: u32,
    payload: Vec<u8>,
    is_merge_patch: bool,
}

type IwaObjects = HashMap<u64, Vec<IwaObject>>;

#[derive(Clone, Copy, Debug)]
enum ProtoValue<'a> {
    Varint(u64),
    Bytes(&'a [u8]),
}

#[derive(Clone, Copy, Debug)]
struct ProtoField<'a> {
    number: u32,
    value: ProtoValue<'a>,
}

#[derive(Clone, Copy, Debug)]
struct IwaMessageInfo {
    object_type: u32,
    payload_length: u64,
    base_index: Option<usize>,
}

/// Parse a Numbers ZIP using the small subset of the reverse-engineered schema
/// needed for sheet/table names, string dictionaries, tiles, rows, and cells.
fn parse_numbers(content: &[u8], limits: &SecurityLimits) -> Result<NumbersData> {
    validate_iwork_zip(content, limits)?;
    let structured = {
        let mut budget = SecurityBudget::for_iwork(limits);
        let mut expansion = IwaExpansionBudget::from_limits(limits);
        parse_numbers_structured(content, &mut budget, &mut expansion)
    };
    match structured {
        Ok(data) if !data.tables.is_empty() => Ok(data),
        Ok(_) => parse_numbers_legacy_fresh(content, limits),
        Err(error) if matches!(&error, XbergError::Security { .. }) => Err(error),
        Err(error) => {
            tracing::debug!(%error, "structured Numbers table parsing failed; using legacy text fallback");
            parse_numbers_legacy_fresh(content, limits)
        }
    }
}

fn parse_numbers_legacy_fresh(content: &[u8], limits: &SecurityLimits) -> Result<NumbersData> {
    // Fallback is an independent parse attempt; fresh budgets prevent double-accounting the same member. ~keep
    let mut budget = SecurityBudget::for_iwork(limits);
    let mut expansion = IwaExpansionBudget::from_limits(limits);
    parse_numbers_legacy(content, &mut budget, &mut expansion)
}

fn parse_numbers_structured(
    content: &[u8],
    budget: &mut SecurityBudget,
    expansion: &mut IwaExpansionBudget,
) -> Result<NumbersData> {
    let objects = read_iwa_objects(content, budget, expansion)?;
    let metadata = extract_metadata_from_zip(content);

    let table_ids = document_table_ids(&objects, budget)?;
    let mut tables = Vec::with_capacity(table_ids.len());
    for table_id in table_ids {
        if let Some(table) = parse_table(table_id, &objects, budget)? {
            tables.push(table);
        }
    }

    Ok(NumbersData { tables, metadata })
}

fn parse_numbers_legacy(
    content: &[u8],
    budget: &mut SecurityBudget,
    expansion: &mut IwaExpansionBudget,
) -> Result<NumbersData> {
    let iwa_paths = collect_iwa_paths(content)?;
    let metadata = extract_metadata_from_zip(content);
    let mut table_cells = Vec::new();
    let mut other_cells = Vec::new();
    let mut table_seen = std::collections::HashSet::new();
    let mut other_seen = std::collections::HashSet::new();

    for path in iwa_paths {
        budget.step()?;
        let decompressed = match read_iwa_file(content, &path, expansion) {
            Ok(decompressed) => decompressed,
            Err(error) if matches!(&error, XbergError::Security { .. }) => return Err(error),
            Err(error) => {
                tracing::debug!(member = %path, %error, "skipping unreadable legacy iWork member");
                continue;
            }
        };
        let texts = crate::extractors::iwork::extract_text_from_proto(&decompressed, budget)?;
        if path.contains("Table") || path.contains("DataStore") {
            append_legacy_cells(texts, &mut table_seen, &mut table_cells, budget)?;
        } else {
            append_legacy_cells(texts, &mut other_seen, &mut other_cells, budget)?;
        }
    }

    let mut tables = Vec::new();
    if !table_cells.is_empty() {
        tables.push(("Sheet Data".to_string(), table_cells));
    }
    if !other_cells.is_empty() {
        tables.push(("Document Info".to_string(), other_cells));
    }
    Ok(NumbersData { tables, metadata })
}

fn append_legacy_cells(
    texts: Vec<String>,
    seen: &mut std::collections::HashSet<String>,
    cells: &mut Vec<Vec<String>>,
    budget: &mut SecurityBudget,
) -> Result<()> {
    for text in texts {
        if text.len() < 2 || !text.chars().any(char::is_alphanumeric) || seen.contains(&text) {
            continue;
        }
        // Charge the cell before HashSet/row allocations so the limit prevents growth. ~keep
        budget.add_cells(1)?;
        seen.insert(text.clone());
        cells.push(vec![text]);
    }
    Ok(())
}

fn read_iwa_objects(
    content: &[u8],
    budget: &mut SecurityBudget,
    expansion: &mut IwaExpansionBudget,
) -> Result<IwaObjects> {
    let mut objects: IwaObjects = HashMap::new();
    for path in collect_iwa_paths(content)? {
        budget.step()?;
        let decompressed = match read_iwa_file(content, &path, expansion) {
            Ok(data) => data,
            Err(error) if matches!(&error, XbergError::Security { .. }) => return Err(error),
            Err(error) => {
                tracing::debug!(member = %path, %error, "skipping unreadable iWork archive member");
                continue;
            }
        };
        let mut member_objects = HashMap::new();
        if let Err(error) = parse_iwa_segments(&decompressed, budget, &mut member_objects) {
            if matches!(&error, XbergError::Security { .. }) {
                return Err(error);
            }
            tracing::debug!(member = %path, %error, "skipping malformed iWork archive member");
            continue;
        }
        for (identifier, mut messages) in member_objects {
            objects.entry(identifier).or_default().append(&mut messages);
        }
    }
    reject_required_merge_patches(&objects)?;
    Ok(objects)
}

fn reject_required_merge_patches(objects: &IwaObjects) -> Result<()> {
    if objects.values().flatten().any(|object| object.is_merge_patch) {
        // Applying field-path protobuf diffs without the full schema could return stale tables. ~keep
        return Err(numbers_parse_error(
            "Numbers archive uses a merge patch that requires schema-aware reconstruction",
        ));
    }
    Ok(())
}

fn parse_iwa_segments(data: &[u8], budget: &mut SecurityBudget, objects: &mut IwaObjects) -> Result<()> {
    let mut position = 0usize;
    while position < data.len() {
        budget.step()?;
        let (identifier, should_merge, message_infos) = parse_iwa_segment_header(data, &mut position, budget)?;
        store_iwa_messages(data, &mut position, identifier, should_merge, &message_infos, objects)?;
    }
    Ok(())
}

fn parse_iwa_segment_header(
    data: &[u8],
    position: &mut usize,
    budget: &mut SecurityBudget,
) -> Result<(u64, bool, Vec<IwaMessageInfo>)> {
    let (header_length, prefix_length) = read_varint_at(data, *position)?;
    *position = position
        .checked_add(prefix_length)
        .ok_or_else(|| numbers_parse_error("IWA header offset overflow"))?;
    let header_end = checked_end(*position, header_length, data.len(), "IWA archive header")?;
    let header_fields = parse_proto_fields(&data[*position..header_end], budget)?;
    *position = header_end;
    let identifier = field_varint(&header_fields, field::ARCHIVE_IDENTIFIER)
        .ok_or_else(|| numbers_parse_error("IWA ArchiveInfo has no object identifier"))?;
    let should_merge = field_varint(&header_fields, field::ARCHIVE_SHOULD_MERGE).is_some_and(|value| value != 0);
    let message_infos = parse_iwa_message_infos(&header_fields, budget)?;
    Ok((identifier, should_merge, message_infos))
}

fn parse_iwa_message_infos(fields: &[ProtoField<'_>], budget: &mut SecurityBudget) -> Result<Vec<IwaMessageInfo>> {
    field_bytes(fields, field::ARCHIVE_MESSAGE_INFO)
        .map(|message| {
            let message_fields = parse_proto_fields(message, budget)?;
            let object_type = field_varint(&message_fields, field::MESSAGE_TYPE)
                .and_then(|value| u32::try_from(value).ok())
                .ok_or_else(|| numbers_parse_error("IWA MessageInfo has no valid object type"))?;
            let payload_length = field_varint(&message_fields, field::MESSAGE_LENGTH)
                .ok_or_else(|| numbers_parse_error("IWA MessageInfo has no payload length"))?;
            let base_index = field_usize(&message_fields, field::MESSAGE_BASE_INDEX);
            Ok(IwaMessageInfo {
                object_type,
                payload_length,
                base_index,
            })
        })
        .collect()
}

fn store_iwa_messages(
    data: &[u8],
    position: &mut usize,
    identifier: u64,
    should_merge: bool,
    message_infos: &[IwaMessageInfo],
    objects: &mut IwaObjects,
) -> Result<()> {
    for message in message_infos {
        let payload_end = checked_end(*position, message.payload_length, data.len(), "IWA object payload")?;
        let is_merge_patch = should_merge && message.object_type == 0;
        let effective_type = effective_iwa_object_type(message, message_infos, is_merge_patch);
        if is_required_object_type(effective_type) {
            let payload = data[*position..payload_end].to_vec();
            // numbers-parser defines ordered bodies and base_message_index; retain all for fallback. ~keep
            objects.entry(identifier).or_default().push(IwaObject {
                object_type: effective_type,
                payload,
                is_merge_patch,
            });
        }
        *position = payload_end;
    }
    Ok(())
}

fn effective_iwa_object_type(message: &IwaMessageInfo, message_infos: &[IwaMessageInfo], is_merge_patch: bool) -> u32 {
    if !is_merge_patch {
        return message.object_type;
    }
    message
        .base_index
        .and_then(|index| message_infos.get(index))
        .map_or(0, |base| base.object_type)
}

fn is_required_object_type(object_type: u32) -> bool {
    matches!(
        object_type,
        DOCUMENT_ARCHIVE_TYPE
            | SHEET_ARCHIVE_TYPE
            | TABLE_INFO_ARCHIVE_TYPE
            | TABLE_MODEL_ARCHIVE_TYPE
            | TILE_ARCHIVE_TYPE
            | RICH_TEXT_PAYLOAD_ARCHIVE_TYPE
            | TEXT_STORAGE_ARCHIVE_TYPE
    ) || TABLE_DATA_LIST_TYPES.contains(&object_type)
}

fn object_for_type(objects: &IwaObjects, identifier: u64, object_type: u32) -> Option<&IwaObject> {
    objects
        .get(&identifier)?
        .iter()
        .rev()
        .find(|object| object.object_type == object_type && !object.is_merge_patch)
}

fn document_table_ids(objects: &IwaObjects, budget: &mut SecurityBudget) -> Result<Vec<u64>> {
    let document = objects
        .values()
        .find_map(|messages| {
            messages
                .iter()
                .rev()
                .find(|object| object.object_type == DOCUMENT_ARCHIVE_TYPE && !object.is_merge_patch)
        })
        .ok_or_else(|| numbers_parse_error("Numbers document archive is missing"))?;
    let document_fields = parse_proto_fields(&document.payload, budget)?;
    let mut table_ids = Vec::new();
    for sheet_reference in field_bytes(&document_fields, field::DOCUMENT_SHEET) {
        let Some(sheet_id) = reference_identifier(sheet_reference) else {
            continue;
        };
        let Some(sheet) = object_for_type(objects, sheet_id, SHEET_ARCHIVE_TYPE) else {
            continue;
        };
        let sheet_fields = parse_proto_fields(&sheet.payload, budget)?;
        for drawable in field_bytes(&sheet_fields, field::SHEET_DRAWABLE) {
            let Some(table_info_id) = reference_identifier(drawable) else {
                continue;
            };
            let Some(table_info) = object_for_type(objects, table_info_id, TABLE_INFO_ARCHIVE_TYPE) else {
                continue;
            };
            let fields = parse_proto_fields(&table_info.payload, budget)?;
            if let Some(table_id) = field_bytes(&fields, field::TABLE_INFO_MODEL)
                .next()
                .and_then(reference_identifier)
            {
                table_ids.push(table_id);
            }
        }
    }
    if table_ids.is_empty() {
        return Err(numbers_parse_error("Numbers document has no readable table references"));
    }
    Ok(table_ids)
}

fn parse_table(
    table_id: u64,
    objects: &IwaObjects,
    budget: &mut SecurityBudget,
) -> Result<Option<(String, Vec<Vec<String>>)>> {
    let Some(model) = object_for_type(objects, table_id, TABLE_MODEL_ARCHIVE_TYPE) else {
        return Ok(None);
    };
    let fields = parse_proto_fields(&model.payload, budget)?;
    let name = parse_table_name(&fields, budget)?;
    let (rows, columns) = parse_table_dimensions(&fields, budget)?;
    if rows == 0 || columns == 0 {
        return Ok(None);
    }

    let Some(data_store) = field_bytes(&fields, field::TABLE_DATA_STORE).next() else {
        return Ok(None);
    };
    let data_store_fields = parse_proto_fields(data_store, budget)?;
    let (strings, rich_strings) = parse_table_dictionaries(&data_store_fields, objects, budget)?;

    let mut cells = vec![vec![String::new(); columns]; rows];
    if let Some(tile_storage) = field_bytes(&data_store_fields, field::DATA_STORE_TILES).next() {
        fill_table_tiles(tile_storage, objects, &strings, &rich_strings, &mut cells, budget)?;
    }
    Ok(Some((name, cells)))
}

fn parse_table_name(fields: &[ProtoField<'_>], budget: &mut SecurityBudget) -> Result<String> {
    let name = field_bytes(fields, field::TABLE_NAME)
        .next()
        .and_then(|value| std::str::from_utf8(value).ok())
        .unwrap_or("Table")
        .to_string();
    budget.check_entity(&name)?;
    budget.account_text(name.len())?;
    Ok(name)
}

fn parse_table_dimensions(fields: &[ProtoField<'_>], budget: &mut SecurityBudget) -> Result<(usize, usize)> {
    let rows = field_usize(fields, field::TABLE_ROWS).unwrap_or(0);
    let columns = field_usize(fields, field::TABLE_COLUMNS).unwrap_or(0);
    let cell_count = rows
        .checked_mul(columns)
        .ok_or_else(|| numbers_parse_error("Numbers table dimensions overflow"))?;
    budget.add_cells(cell_count)?;
    Ok((rows, columns))
}

fn parse_table_dictionaries(
    fields: &[ProtoField<'_>],
    objects: &IwaObjects,
    budget: &mut SecurityBudget,
) -> Result<(HashMap<i32, String>, HashMap<i32, String>)> {
    let strings = field_bytes(fields, field::DATA_STORE_STRINGS)
        .next()
        .and_then(reference_identifier)
        .and_then(|identifier| object_for_table_data_list(objects, identifier))
        .map(|object| parse_string_table(&object.payload, budget))
        .transpose()?
        .unwrap_or_default();
    let rich_strings = field_bytes(fields, field::DATA_STORE_RICH_TEXT)
        .next()
        .and_then(reference_identifier)
        .and_then(|identifier| object_for_table_data_list(objects, identifier))
        .map(|object| parse_rich_text_table(&object.payload, objects, budget))
        .transpose()?
        .unwrap_or_default();
    Ok((strings, rich_strings))
}

fn object_for_table_data_list(objects: &IwaObjects, identifier: u64) -> Option<&IwaObject> {
    TABLE_DATA_LIST_TYPES
        .iter()
        .find_map(|object_type| object_for_type(objects, identifier, *object_type))
}

fn parse_string_table(payload: &[u8], budget: &mut SecurityBudget) -> Result<HashMap<i32, String>> {
    let fields = parse_proto_fields(payload, budget)?;
    let mut strings = HashMap::new();
    for entry in field_bytes(&fields, field::DATA_LIST_ENTRY) {
        let entry_fields = parse_proto_fields(entry, budget)?;
        let Some(key) =
            field_varint(&entry_fields, field::DATA_LIST_ENTRY_KEY).and_then(|value| i32::try_from(value).ok())
        else {
            continue;
        };
        let Some(value) = field_bytes(&entry_fields, field::DATA_LIST_ENTRY_STRING)
            .next()
            .and_then(|bytes| std::str::from_utf8(bytes).ok())
        else {
            continue;
        };
        budget.check_entity(value)?;
        budget.account_text(value.len())?;
        strings.insert(key, value.to_string());
    }
    Ok(strings)
}

fn parse_rich_text_table(
    payload: &[u8],
    objects: &IwaObjects,
    budget: &mut SecurityBudget,
) -> Result<HashMap<i32, String>> {
    let fields = parse_proto_fields(payload, budget)?;
    let mut strings = HashMap::new();
    for entry in field_bytes(&fields, field::DATA_LIST_ENTRY) {
        let entry_fields = parse_proto_fields(entry, budget)?;
        let Some(key) =
            field_varint(&entry_fields, field::DATA_LIST_ENTRY_KEY).and_then(|value| i32::try_from(value).ok())
        else {
            continue;
        };
        let Some(payload_id) = field_bytes(&entry_fields, field::DATA_LIST_ENTRY_RICH_TEXT)
            .next()
            .and_then(reference_identifier)
        else {
            continue;
        };
        let Some(rich_payload) = object_for_type(objects, payload_id, RICH_TEXT_PAYLOAD_ARCHIVE_TYPE) else {
            continue;
        };
        let rich_fields = parse_proto_fields(&rich_payload.payload, budget)?;
        let Some(storage_id) = field_bytes(&rich_fields, field::RICH_TEXT_STORAGE)
            .next()
            .and_then(reference_identifier)
        else {
            continue;
        };
        let Some(storage) = object_for_type(objects, storage_id, TEXT_STORAGE_ARCHIVE_TYPE) else {
            continue;
        };
        let storage_fields = parse_proto_fields(&storage.payload, budget)?;
        let value = field_bytes(&storage_fields, field::TEXT_STORAGE_TEXT)
            .filter_map(|bytes| std::str::from_utf8(bytes).ok())
            .collect::<String>();
        if value.is_empty() {
            continue;
        }
        budget.check_entity(&value)?;
        budget.account_text(value.len())?;
        strings.insert(key, value);
    }
    Ok(strings)
}

fn fill_table_tiles(
    tile_storage: &[u8],
    objects: &IwaObjects,
    strings: &HashMap<i32, String>,
    rich_strings: &HashMap<i32, String>,
    cells: &mut [Vec<String>],
    budget: &mut SecurityBudget,
) -> Result<()> {
    let storage_fields = parse_proto_fields(tile_storage, budget)?;
    let tile_size = field_usize(&storage_fields, field::TILE_STORAGE_SIZE).unwrap_or(DEFAULT_TILE_SIZE);
    for tile_entry in field_bytes(&storage_fields, field::TILE_STORAGE_TILE) {
        let tile_fields = parse_proto_fields(tile_entry, budget)?;
        let tile_index = field_usize(&tile_fields, field::TILE_INDEX).unwrap_or(0);
        let Some(tile_id) = field_bytes(&tile_fields, field::TILE_REFERENCE)
            .next()
            .and_then(reference_identifier)
        else {
            continue;
        };
        let Some(tile) = object_for_type(objects, tile_id, TILE_ARCHIVE_TYPE) else {
            continue;
        };
        let row_offset = tile_index
            .checked_mul(tile_size)
            .ok_or_else(|| numbers_parse_error("Numbers tile row offset overflow"))?;
        fill_tile(&tile.payload, row_offset, strings, rich_strings, cells, budget)?;
    }
    Ok(())
}

fn fill_tile(
    payload: &[u8],
    row_offset: usize,
    strings: &HashMap<i32, String>,
    rich_strings: &HashMap<i32, String>,
    cells: &mut [Vec<String>],
    budget: &mut SecurityBudget,
) -> Result<()> {
    let fields = parse_proto_fields(payload, budget)?;
    for row_info in field_bytes(&fields, field::TILE_ROW_INFO) {
        let row_fields = parse_proto_fields(row_info, budget)?;
        let Some(row_index) =
            field_usize(&row_fields, field::ROW_INDEX).and_then(|index| row_offset.checked_add(index))
        else {
            continue;
        };
        let Some(row) = cells.get_mut(row_index) else {
            continue;
        };
        let modern_storage = field_bytes(&row_fields, field::ROW_CELL_STORAGE).next();
        let modern_offsets = field_bytes(&row_fields, field::ROW_CELL_OFFSETS).next();
        let (storage, offsets, wide_offsets) = match (modern_storage, modern_offsets) {
            (Some(storage), Some(offsets)) if !storage.is_empty() || !offsets.is_empty() => (
                storage,
                offsets,
                field_varint(&row_fields, field::ROW_HAS_WIDE_OFFSETS).is_some_and(|value| value != 0),
            ),
            _ => (
                field_bytes(&row_fields, field::ROW_CELL_STORAGE_PRE_BNC)
                    .next()
                    .unwrap_or_default(),
                field_bytes(&row_fields, field::ROW_CELL_OFFSETS_PRE_BNC)
                    .next()
                    .unwrap_or_default(),
                false,
            ),
        };
        fill_row(row, storage, offsets, wide_offsets, strings, rich_strings, budget)?;
    }
    Ok(())
}

fn fill_row(
    row: &mut [String],
    storage: &[u8],
    offsets: &[u8],
    wide_offsets: bool,
    strings: &HashMap<i32, String>,
    rich_strings: &HashMap<i32, String>,
    budget: &mut SecurityBudget,
) -> Result<()> {
    let parsed_offsets = parse_cell_offsets(offsets, row.len(), wide_offsets)?;

    for (column, start) in parsed_offsets.iter().copied().enumerate() {
        let Some(start) = start else {
            continue;
        };
        let end = parsed_offsets[column + 1..]
            .iter()
            .flatten()
            .copied()
            .next()
            .unwrap_or(storage.len());
        if start > end || end > storage.len() {
            return Err(numbers_parse_error("Numbers cell storage offset is out of bounds"));
        }
        if let Some(value) = parse_cell_value(&storage[start..end], strings, rich_strings)? {
            budget.account_text(value.len())?;
            row[column] = value;
        }
    }
    Ok(())
}

fn parse_cell_offsets(offsets: &[u8], column_count: usize, wide_offsets: bool) -> Result<Vec<Option<usize>>> {
    offsets
        .chunks_exact(OFFSET_ENTRY_LENGTH)
        .take(column_count)
        .map(|bytes| {
            let offset = u16::from_le_bytes([bytes[0], bytes[1]]);
            if offset == u16::MAX {
                Ok(None)
            } else {
                let scale = if wide_offsets { WIDE_OFFSET_SCALE } else { 1 };
                let expanded = usize::from(offset)
                    .checked_mul(scale)
                    .ok_or_else(|| numbers_parse_error("Numbers cell offset overflow"))?;
                Ok(Some(expanded))
            }
        })
        .collect()
}

fn parse_cell_value(
    storage: &[u8],
    strings: &HashMap<i32, String>,
    rich_strings: &HashMap<i32, String>,
) -> Result<Option<String>> {
    let Some(version) = storage.first().copied() else {
        return Err(numbers_parse_error("Numbers cell storage is truncated"));
    };
    if version == CELL_STORAGE_VERSION {
        parse_v5_cell(storage, strings, rich_strings)
    } else if version <= 4 {
        parse_old_cell(storage, strings, rich_strings)
    } else {
        tracing::debug!(version, "skipping unsupported Numbers cell storage version");
        Ok(None)
    }
}

fn parse_v5_cell(
    storage: &[u8],
    strings: &HashMap<i32, String>,
    rich_strings: &HashMap<i32, String>,
) -> Result<Option<String>> {
    if storage.len() < CELL_HEADER_LENGTH {
        return Err(numbers_parse_error("Numbers v5 cell storage is truncated"));
    }
    let cell_type = storage[1];
    let flags = u32::from_le_bytes([storage[8], storage[9], storage[10], storage[11]]);
    let mut cursor = CELL_HEADER_LENGTH;
    let decimal =
        take_flagged(storage, &mut cursor, flags, CELL_DECIMAL_FLAG, DECIMAL_VALUE_LENGTH)?.map(decode_decimal128);
    let double = take_flagged(storage, &mut cursor, flags, CELL_DOUBLE_FLAG, SCALAR_VALUE_LENGTH)?.map(decode_f64);
    let seconds = take_flagged(storage, &mut cursor, flags, CELL_DATE_FLAG, SCALAR_VALUE_LENGTH)?.map(decode_f64);
    let string_key = take_flagged(storage, &mut cursor, flags, CELL_STRING_FLAG, STRING_KEY_LENGTH)?.map(decode_i32);
    let rich_key = take_flagged(storage, &mut cursor, flags, CELL_RICH_TEXT_FLAG, STRING_KEY_LENGTH)?.map(decode_i32);

    // Source: numbers-parser cell.py::_from_storage defines the v5 layout and type IDs. ~keep
    Ok(match cell_type {
        EMPTY_CELL_TYPE | ERROR_CELL_TYPE => None,
        NUMBER_CELL_TYPE | CURRENCY_CELL_TYPE => decimal.or(double).map(format_scalar),
        TEXT_CELL_TYPE => string_key.and_then(|key| strings.get(&key).cloned()),
        DATE_CELL_TYPE => seconds.map(format_iwork_date),
        BOOLEAN_CELL_TYPE => double.map(|value| (value > 0.0).to_string()),
        DURATION_CELL_TYPE => double.map(|value| format!("{}s", format_scalar(value))),
        RICH_TEXT_CELL_TYPE => rich_key.and_then(|key| rich_strings.get(&key).cloned()),
        _ => {
            tracing::debug!(cell_type, "skipping unsupported Numbers v5 cell type");
            None
        }
    })
}

fn parse_old_cell(
    storage: &[u8],
    strings: &HashMap<i32, String>,
    rich_strings: &HashMap<i32, String>,
) -> Result<Option<String>> {
    let version = storage[0];
    let header_length = if version <= OLD_V1_MAX_VERSION {
        OLD_V1_HEADER_LENGTH
    } else {
        CELL_HEADER_LENGTH
    };
    if storage.len() < header_length {
        return Err(numbers_parse_error("Numbers legacy cell storage is truncated"));
    }
    let cell_type_offset = if version <= OLD_V3_MAX_VERSION {
        OLD_CELL_TYPE_V1_V3_OFFSET
    } else {
        OLD_CELL_TYPE_V4_OFFSET
    };
    let cell_type = storage[cell_type_offset];
    let flags = if version <= OLD_V1_MAX_VERSION {
        u32::from(u16::from_le_bytes([
            storage[OLD_FLAGS_OFFSET],
            storage[OLD_FLAGS_OFFSET + 1],
        ]))
    } else {
        let mut bytes = [0_u8; 4];
        bytes.copy_from_slice(&storage[OLD_FLAGS_OFFSET..OLD_FLAGS_OFFSET + 4]);
        u32::from_le_bytes(bytes)
    };
    let fields = parse_old_cell_fields(storage, header_length, flags)?;

    // Source: https://oss.sheetjs.com/notes/iwa/ documents pre-BNC fields 3/4 and masks. ~keep
    Ok(match cell_type {
        EMPTY_CELL_TYPE | ERROR_CELL_TYPE => None,
        NUMBER_CELL_TYPE | CURRENCY_CELL_TYPE => fields.double.map(format_scalar),
        TEXT_CELL_TYPE => fields.string_key.and_then(|key| strings.get(&key).cloned()),
        DATE_CELL_TYPE => fields.seconds.map(format_iwork_date),
        BOOLEAN_CELL_TYPE => fields.double.map(|value| (value > 0.0).to_string()),
        DURATION_CELL_TYPE => fields.double.map(|value| format!("{}s", format_scalar(value))),
        RICH_TEXT_CELL_TYPE => fields.rich_key.and_then(|key| rich_strings.get(&key).cloned()),
        _ => None,
    })
}

#[derive(Default)]
struct OldCellFields {
    string_key: Option<i32>,
    rich_key: Option<i32>,
    double: Option<f64>,
    seconds: Option<f64>,
}

fn parse_old_cell_fields(storage: &[u8], mut cursor: usize, flags: u32) -> Result<OldCellFields> {
    const FIELD_LAYOUT: &[(u32, usize)] = &[
        (OLD_CELL_STYLE_FLAG, STRING_KEY_LENGTH),
        (OLD_TEXT_STYLE_FLAG, STRING_KEY_LENGTH),
        (OLD_CONDITIONAL_STYLE_FLAG, STRING_KEY_LENGTH),
        (OLD_CONDITIONAL_RULE_FLAG, STRING_KEY_LENGTH),
        (OLD_CURRENT_FORMAT_FLAG, STRING_KEY_LENGTH),
        (OLD_FORMULA_FLAG, STRING_KEY_LENGTH),
        (OLD_FORMULA_ERROR_FLAG, STRING_KEY_LENGTH),
        (OLD_RICH_TEXT_FLAG, STRING_KEY_LENGTH),
        (OLD_COMMENT_FLAG, STRING_KEY_LENGTH),
        (OLD_IMPORT_WARNING_FLAG, STRING_KEY_LENGTH),
        (OLD_STRING_FLAG, STRING_KEY_LENGTH),
        (OLD_DOUBLE_FLAG, SCALAR_VALUE_LENGTH),
        (OLD_DATE_FLAG, SCALAR_VALUE_LENGTH),
        (OLD_NUMBER_FORMAT_FLAG, STRING_KEY_LENGTH),
        (OLD_CURRENCY_FORMAT_FLAG, STRING_KEY_LENGTH),
        (OLD_DATE_FORMAT_FLAG, STRING_KEY_LENGTH),
        (OLD_DURATION_FORMAT_FLAG, STRING_KEY_LENGTH),
        (OLD_CONTROL_FORMAT_FLAG, STRING_KEY_LENGTH),
        (OLD_CUSTOM_FORMAT_FLAG, STRING_KEY_LENGTH),
        (OLD_BASE_FORMAT_FLAG, STRING_KEY_LENGTH),
        (OLD_CHOICE_FORMAT_FLAG, STRING_KEY_LENGTH),
    ];
    let mut fields = OldCellFields::default();
    for &(flag, length) in FIELD_LAYOUT {
        let Some(bytes) = take_flagged(storage, &mut cursor, flags, flag, length)? else {
            continue;
        };
        match flag {
            OLD_STRING_FLAG => fields.string_key = Some(decode_i32(bytes)),
            OLD_RICH_TEXT_FLAG => fields.rich_key = Some(decode_i32(bytes)),
            OLD_DOUBLE_FLAG => fields.double = Some(decode_f64(bytes)),
            OLD_DATE_FLAG => fields.seconds = Some(decode_f64(bytes)),
            _ => {}
        }
    }
    Ok(fields)
}

fn take_flagged<'a>(
    storage: &'a [u8],
    cursor: &mut usize,
    flags: u32,
    flag: u32,
    length: usize,
) -> Result<Option<&'a [u8]>> {
    if flags & flag == 0 {
        return Ok(None);
    }
    let end = cursor
        .checked_add(length)
        .ok_or_else(|| numbers_parse_error("Numbers cell field offset overflow"))?;
    let value = storage
        .get(*cursor..end)
        .ok_or_else(|| numbers_parse_error("Numbers cell field is truncated"))?;
    *cursor = end;
    Ok(Some(value))
}

fn decode_i32(bytes: &[u8]) -> i32 {
    let mut value = [0_u8; 4];
    value.copy_from_slice(bytes);
    i32::from_le_bytes(value)
}

fn decode_f64(bytes: &[u8]) -> f64 {
    let mut value = [0_u8; 8];
    value.copy_from_slice(bytes);
    f64::from_le_bytes(value)
}

fn decode_decimal128(bytes: &[u8]) -> f64 {
    let mut mantissa_bytes = [0_u8; 16];
    mantissa_bytes[..14].copy_from_slice(&bytes[..14]);
    mantissa_bytes[14] = bytes[14] & 1;
    let mantissa = u128::from_le_bytes(mantissa_bytes) as f64;
    let exponent = (i32::from(bytes[15] & 0x7f) << 7) | i32::from(bytes[14] >> 1);
    let sign = if bytes[15] & 0x80 == 0 { 1.0 } else { -1.0 };
    sign * mantissa * 10_f64.powi(exponent - DECIMAL128_EXPONENT_BIAS)
}

fn format_scalar(value: f64) -> String {
    if value.is_finite() && value.fract() == 0.0 {
        format!("{value:.0}")
    } else {
        value.to_string()
    }
}

fn format_iwork_date(seconds: f64) -> String {
    if !seconds.is_finite() || seconds < i64::MIN as f64 || seconds > i64::MAX as f64 {
        return format_scalar(seconds);
    }
    let unix_seconds = (seconds as i64).saturating_add(IWORK_EPOCH_TO_UNIX_SECONDS);
    let days = unix_seconds.div_euclid(SECONDS_PER_DAY);
    let day_seconds = unix_seconds.rem_euclid(SECONDS_PER_DAY);
    let (year, month, day) = civil_date_from_unix_days(days);
    let hour = day_seconds / 3_600;
    let minute = day_seconds % 3_600 / 60;
    let second = day_seconds % 60;
    format!("{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{second:02}Z")
}

fn civil_date_from_unix_days(days: i64) -> (i64, i64, i64) {
    const CIVIL_EPOCH_OFFSET_DAYS: i64 = 719_468;
    let adjusted = days + CIVIL_EPOCH_OFFSET_DAYS;
    let era = adjusted.div_euclid(146_097);
    let day_of_era = adjusted - era * 146_097;
    let year_of_era = (day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
    let mut year = year_of_era + era * 400;
    let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
    let month_prime = (5 * day_of_year + 2) / 153;
    let day = day_of_year - (153 * month_prime + 2) / 5 + 1;
    let month = month_prime + if month_prime < 10 { 3 } else { -9 };
    year += i64::from(month <= 2);
    (year, month, day)
}

fn parse_proto_fields<'a>(data: &'a [u8], budget: &mut SecurityBudget) -> Result<Vec<ProtoField<'a>>> {
    budget.enter()?;
    let result = parse_proto_fields_inner(data, budget);
    budget.leave();
    result
}

fn parse_proto_fields_inner<'a>(data: &'a [u8], budget: &mut SecurityBudget) -> Result<Vec<ProtoField<'a>>> {
    let mut fields = Vec::new();
    let mut position = 0usize;
    while position < data.len() {
        budget.step()?;
        let (tag, tag_length) = read_varint_at(data, position)?;
        position = position
            .checked_add(tag_length)
            .ok_or_else(|| numbers_parse_error("protobuf tag offset overflow"))?;
        let number = u32::try_from(tag >> wire::FIELD_NUMBER_SHIFT)
            .map_err(|_| numbers_parse_error("protobuf field number overflow"))?;
        if let Some(value) = parse_proto_value(data, &mut position, tag & wire::TYPE_MASK)? {
            fields.push(ProtoField { number, value });
        }
    }
    Ok(fields)
}

fn parse_proto_value<'a>(data: &'a [u8], position: &mut usize, wire_type: u64) -> Result<Option<ProtoValue<'a>>> {
    match wire_type {
        wire::VARINT => {
            let (value, length) = read_varint_at(data, *position)?;
            *position = position
                .checked_add(length)
                .ok_or_else(|| numbers_parse_error("protobuf varint offset overflow"))?;
            Ok(Some(ProtoValue::Varint(value)))
        }
        wire::FIXED64 => {
            *position = checked_end(*position, wire::FIXED64_LENGTH, data.len(), "protobuf fixed64")?;
            Ok(None)
        }
        wire::LENGTH_DELIMITED => parse_length_delimited_value(data, position).map(Some),
        wire::FIXED32 => {
            *position = checked_end(*position, wire::FIXED32_LENGTH, data.len(), "protobuf fixed32")?;
            Ok(None)
        }
        _ => Err(numbers_parse_error(&format!(
            "unsupported protobuf wire type {wire_type}"
        ))),
    }
}

fn parse_length_delimited_value<'a>(data: &'a [u8], position: &mut usize) -> Result<ProtoValue<'a>> {
    let (length, prefix_length) = read_varint_at(data, *position)?;
    *position = position
        .checked_add(prefix_length)
        .ok_or_else(|| numbers_parse_error("protobuf length offset overflow"))?;
    let end = checked_end(*position, length, data.len(), "protobuf length-delimited field")?;
    let value = ProtoValue::Bytes(&data[*position..end]);
    *position = end;
    Ok(value)
}

fn read_varint_at(data: &[u8], position: usize) -> Result<(u64, usize)> {
    let mut value = 0u64;
    let mut shift = 0u32;
    let mut cursor = position;
    loop {
        let byte = *data
            .get(cursor)
            .ok_or_else(|| numbers_parse_error("truncated protobuf varint"))?;
        cursor += 1;
        if shift == wire::VARINT_MAX_BITS - 1 && byte > 1 {
            return Err(numbers_parse_error("protobuf varint exceeds 64 bits"));
        }
        value |= u64::from(byte & wire::VARINT_VALUE_MASK) << shift;
        if byte & wire::VARINT_CONTINUATION_BIT == 0 {
            return Ok((value, cursor - position));
        }
        shift += wire::VARINT_BITS_PER_BYTE;
        if shift >= wire::VARINT_MAX_BITS {
            return Err(numbers_parse_error("protobuf varint exceeds 64 bits"));
        }
    }
}

fn checked_end(position: usize, length: u64, total: usize, description: &str) -> Result<usize> {
    let length = usize::try_from(length).map_err(|_| numbers_parse_error(&format!("{description} length overflow")))?;
    let end = position
        .checked_add(length)
        .ok_or_else(|| numbers_parse_error(&format!("{description} offset overflow")))?;
    if end > total {
        return Err(numbers_parse_error(&format!("{description} is truncated")));
    }
    Ok(end)
}

fn field_varint(fields: &[ProtoField<'_>], number: u32) -> Option<u64> {
    fields.iter().find_map(|field| match field {
        ProtoField {
            number: field_number,
            value: ProtoValue::Varint(value),
        } if *field_number == number => Some(*value),
        _ => None,
    })
}

fn field_usize(fields: &[ProtoField<'_>], number: u32) -> Option<usize> {
    field_varint(fields, number).and_then(|value| usize::try_from(value).ok())
}

fn field_bytes<'data, 'fields>(
    fields: &'fields [ProtoField<'data>],
    number: u32,
) -> impl Iterator<Item = &'data [u8]> + 'fields
where
    'data: 'fields,
{
    fields.iter().filter_map(move |field| match field {
        ProtoField {
            number: field_number,
            value: ProtoValue::Bytes(value),
        } if *field_number == number => Some(*value),
        _ => None,
    })
}

fn reference_identifier(data: &[u8]) -> Option<u64> {
    let (tag, tag_length) = read_varint_at(data, 0).ok()?;
    if tag != wire::REFERENCE_TAG {
        return None;
    }
    read_varint_at(data, tag_length).ok().map(|(value, _)| value)
}

fn numbers_parse_error(message: &str) -> XbergError {
    XbergError::parsing(format!("Failed to parse Numbers table data: {message}"))
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl InternalDocumentExtractor for NumbersExtractor {
    async fn extract_content(
        &self,
        content: &[u8],
        mime_type: &str,
        config: &ExtractionConfig,
    ) -> Result<InternalDocument> {
        let data = {
            #[cfg(feature = "tokio-runtime")]
            if crate::core::batch_mode::is_batch_mode() {
                if config.cancel_token.as_ref().map(|t| t.is_cancelled()).unwrap_or(false) {
                    return Err(crate::error::XbergError::Cancelled);
                }
                let content_owned = content.to_vec();
                let limits = config.security_limits.clone().unwrap_or_default();
                let span = tracing::Span::current();
                tokio::task::spawn_blocking(move || {
                    let _guard = span.entered();
                    parse_numbers(&content_owned, &limits)
                })
                .await
                .map_err(|e| crate::error::XbergError::parsing(format!("Numbers extraction task failed: {e}")))??
            } else {
                let limits = config.security_limits.clone().unwrap_or_default();
                parse_numbers(content, &limits)?
            }

            #[cfg(not(feature = "tokio-runtime"))]
            {
                if config.cancel_token.as_ref().map(|t| t.is_cancelled()).unwrap_or(false) {
                    return Err(crate::error::XbergError::Cancelled);
                }
                let limits = config.security_limits.clone().unwrap_or_default();
                parse_numbers(content, &limits)?
            }
        };

        let mut doc = build_numbers_internal_document(&data);
        doc.mime_type = mime_type.to_string();
        Ok(doc)
    }

    fn supported_mime_types(&self) -> &[&str] {
        &["application/x-iwork-numbers-sffnumbers"]
    }

    fn priority(&self) -> i32 {
        50
    }
}

/// Build an `InternalDocument` from extracted Numbers data.
///
/// Outputs cell values as structured tables rather than flat paragraphs,
/// reflecting the spreadsheet nature of the .numbers format.
fn build_numbers_internal_document(data: &NumbersData) -> InternalDocument {
    let mut builder = InternalDocumentBuilder::new("numbers");

    if data.metadata.title.is_some() || data.metadata.authors.is_some() {
        builder.set_metadata(data.metadata.clone());
    }

    for (table_name, cells) in &data.tables {
        if cells.is_empty() {
            continue;
        }

        builder.push_heading(1, table_name, None, None);
        builder.push_table_from_cells(cells, None, None);
    }

    builder.build()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_numbers_extractor_plugin_interface() {
        let extractor = NumbersExtractor::new();
        assert_eq!(extractor.name(), "iwork-numbers-extractor");
        assert!(extractor.initialize().is_ok());
        assert!(extractor.shutdown().is_ok());
    }

    #[test]
    fn test_numbers_extractor_supported_mime_types() {
        let extractor = NumbersExtractor::new();
        let types = extractor.supported_mime_types();
        assert!(types.contains(&"application/x-iwork-numbers-sffnumbers"));
    }

    #[test]
    fn should_reject_varint_that_overflows_u64() {
        let overflowing = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x02];

        assert!(read_varint_at(&overflowing, 0).is_err());
    }

    #[test]
    fn should_accept_maximum_u64_varint() {
        let maximum = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01];

        assert_eq!(read_varint_at(&maximum, 0).unwrap(), (u64::MAX, maximum.len()));
    }

    #[test]
    fn should_treat_high_bit_cell_offsets_as_valid_unsigned_values() {
        let offsets = [0x00, 0x80, 0xff, 0xff];

        assert_eq!(
            parse_cell_offsets(&offsets, 2, false).unwrap(),
            vec![Some(32_768), None]
        );
        assert_eq!(
            parse_cell_offsets(&offsets, 2, true).unwrap(),
            vec![Some(131_072), None]
        );
    }

    #[test]
    fn should_account_for_repeated_dictionary_string_expansion() {
        let value = "amplify".to_string();
        let strings = HashMap::from([(7, value)]);
        let mut cell = vec![0; CELL_HEADER_LENGTH + STRING_KEY_LENGTH];
        cell[0] = CELL_STORAGE_VERSION;
        cell[1] = TEXT_CELL_TYPE;
        cell[8..12].copy_from_slice(&CELL_STRING_FLAG.to_le_bytes());
        cell[CELL_HEADER_LENGTH..].copy_from_slice(&7_i32.to_le_bytes());
        let storage = [cell.as_slice(), cell.as_slice()].concat();
        let second_offset = u16::try_from(cell.len()).unwrap();
        let offsets = [0_u16.to_le_bytes(), second_offset.to_le_bytes()].concat();
        let limits = SecurityLimits {
            max_content_size: 10,
            ..SecurityLimits::default()
        };
        let mut budget = SecurityBudget::from_limits(&limits);
        let mut row = vec![String::new(), String::new()];
        let rich_strings = HashMap::new();

        assert!(matches!(
            fill_row(
                &mut row,
                &storage,
                &offsets,
                false,
                &strings,
                &rich_strings,
                &mut budget
            ),
            Err(XbergError::Security { .. })
        ));
    }

    #[test]
    fn should_skip_unknown_cell_without_downgrading_the_table() {
        let strings = HashMap::new();
        let mut cell = vec![0; CELL_HEADER_LENGTH];
        cell[0] = CELL_STORAGE_VERSION;
        cell[1] = u8::MAX;

        assert_eq!(parse_cell_value(&cell, &strings, &HashMap::new()).unwrap(), None);
    }

    #[test]
    fn should_decode_mixed_v5_and_legacy_cell_values() {
        let strings = HashMap::from([(7, "plain".to_string())]);
        let rich_strings = HashMap::from([(9, "rich text".to_string())]);
        let decimal = decimal128_integer(123);
        let cases = [
            (v5_cell(NUMBER_CELL_TYPE, CELL_DECIMAL_FLAG, &decimal), Some("123")),
            (
                v5_cell(BOOLEAN_CELL_TYPE, CELL_DOUBLE_FLAG, &1_f64.to_le_bytes()),
                Some("true"),
            ),
            (
                v5_cell(DATE_CELL_TYPE, CELL_DATE_FLAG, &0_f64.to_le_bytes()),
                Some("2001-01-01T00:00:00Z"),
            ),
            (
                v5_cell(TEXT_CELL_TYPE, CELL_STRING_FLAG, &7_i32.to_le_bytes()),
                Some("plain"),
            ),
            (
                v5_cell(RICH_TEXT_CELL_TYPE, CELL_RICH_TEXT_FLAG, &9_i32.to_le_bytes()),
                Some("rich text"),
            ),
            (
                legacy_v4_cell(NUMBER_CELL_TYPE, OLD_DOUBLE_FLAG, &12.5_f64.to_le_bytes()),
                Some("12.5"),
            ),
        ];

        for (cell, expected) in cases {
            assert_eq!(
                parse_cell_value(&cell, &strings, &rich_strings).unwrap().as_deref(),
                expected
            );
        }
    }

    #[test]
    fn should_read_legacy_pre_bnc_row_fields_three_and_four() {
        let strings = HashMap::from([(7, "legacy text".to_string())]);
        let cell = legacy_v4_cell(TEXT_CELL_TYPE, OLD_STRING_FLAG, &7_i32.to_le_bytes());
        let mut row_info = vec![8, 0, 26];
        row_info.extend(encode_varint(cell.len() as u64));
        row_info.extend(cell);
        row_info.extend([34, 2, 0, 0]);
        let mut tile = vec![42];
        tile.extend(encode_varint(row_info.len() as u64));
        tile.extend(row_info);
        let mut cells = vec![vec![String::new()]];
        let mut budget = SecurityBudget::for_iwork(&SecurityLimits::default());

        fill_tile(&tile, 0, &strings, &HashMap::new(), &mut cells, &mut budget).unwrap();

        assert_eq!(cells, vec![vec!["legacy text".to_string()]]);
    }

    #[test]
    fn should_reject_required_merge_patch_instead_of_returning_stale_object() {
        let objects = HashMap::from([(
            1,
            vec![
                IwaObject {
                    object_type: DOCUMENT_ARCHIVE_TYPE,
                    payload: vec![1],
                    is_merge_patch: false,
                },
                IwaObject {
                    object_type: DOCUMENT_ARCHIVE_TYPE,
                    payload: vec![2],
                    is_merge_patch: true,
                },
            ],
        )]);

        assert_eq!(objects[&1].len(), 2);
        assert!(reject_required_merge_patches(&objects).is_err());
    }

    #[test]
    fn should_preserve_multiple_message_info_payloads_for_one_identifier() {
        let first = [8, 1];
        let second = [8, 2];
        let mut header = vec![8, 42];
        append_message_info(&mut header, TABLE_MODEL_ARCHIVE_TYPE, first.len());
        append_message_info(&mut header, TABLE_MODEL_ARCHIVE_TYPE, second.len());
        let mut segment = encode_varint(header.len() as u64);
        segment.extend_from_slice(&header);
        segment.extend_from_slice(&first);
        segment.extend_from_slice(&second);
        let mut objects = HashMap::new();
        let mut budget = SecurityBudget::for_iwork(&SecurityLimits::default());

        parse_iwa_segments(&segment, &mut budget, &mut objects).unwrap();

        assert_eq!(objects[&42].len(), 2);
        assert_eq!(objects[&42][0].payload, first);
        assert_eq!(objects[&42][1].payload, second);
    }

    #[test]
    fn should_limit_legacy_cells_before_allocating_all_rows() {
        let limits = SecurityLimits {
            max_table_cells: 1,
            ..SecurityLimits::default()
        };
        let mut budget = SecurityBudget::for_iwork(&limits);
        let mut seen = std::collections::HashSet::new();
        let mut cells = Vec::new();

        assert!(matches!(
            append_legacy_cells(
                vec!["first".to_string(), "second".to_string()],
                &mut seen,
                &mut cells,
                &mut budget
            ),
            Err(XbergError::Security { .. })
        ));
        assert_eq!(cells, vec![vec!["first".to_string()]]);
    }

    #[test]
    fn should_use_fresh_expansion_budget_for_parse_numbers_fallback() {
        let text = b"fresh fallback";
        let mut protobuf = vec![0x1a, text.len() as u8];
        protobuf.extend_from_slice(text);
        let mut iwa = vec![1, protobuf.len() as u8, 0, 0];
        iwa.extend_from_slice(&protobuf);
        let archive = numbers_zip_with_table_iwa(&iwa);
        let limits = SecurityLimits {
            // One attempt fits exactly; reusing its expansion budget would reject the fallback. ~keep
            max_content_size: iwa.len(),
            ..SecurityLimits::default()
        };

        let data = parse_numbers(&archive, &limits).unwrap();

        assert_eq!(data.tables.len(), 1);
        assert_eq!(data.tables[0].0, "Sheet Data");
        assert_eq!(data.tables[0].1, vec![vec!["fresh fallback".to_string()]]);
    }

    fn v5_cell(cell_type: u8, flag: u32, value: &[u8]) -> Vec<u8> {
        let mut cell = vec![0; CELL_HEADER_LENGTH];
        cell[0] = CELL_STORAGE_VERSION;
        cell[1] = cell_type;
        cell[8..12].copy_from_slice(&flag.to_le_bytes());
        cell.extend_from_slice(value);
        cell
    }

    fn legacy_v4_cell(cell_type: u8, flag: u32, value: &[u8]) -> Vec<u8> {
        let mut cell = vec![0; CELL_HEADER_LENGTH];
        cell[0] = 4;
        cell[1] = cell_type;
        cell[4..8].copy_from_slice(&flag.to_le_bytes());
        cell.extend_from_slice(value);
        cell
    }

    fn decimal128_integer(value: u8) -> [u8; DECIMAL_VALUE_LENGTH] {
        let mut decimal = [0_u8; DECIMAL_VALUE_LENGTH];
        decimal[0] = value;
        decimal[14] = ((DECIMAL128_EXPONENT_BIAS & 0x7f) << 1) as u8;
        decimal[15] = (DECIMAL128_EXPONENT_BIAS >> 7) as u8;
        decimal
    }

    fn append_message_info(header: &mut Vec<u8>, object_type: u32, payload_length: usize) {
        let mut message = vec![8];
        message.extend(encode_varint(u64::from(object_type)));
        message.push(24);
        message.extend(encode_varint(payload_length as u64));
        header.push(18);
        header.extend(encode_varint(message.len() as u64));
        header.extend(message);
    }

    fn numbers_zip_with_table_iwa(iwa: &[u8]) -> Vec<u8> {
        use std::io::Write;

        let mut archive = Vec::new();
        {
            let cursor = std::io::Cursor::new(&mut archive);
            let mut zip = zip::ZipWriter::new(cursor);
            let options = zip::write::FileOptions::<()>::default().compression_method(zip::CompressionMethod::Stored);
            zip.start_file("Index/Table-1.iwa", options).unwrap();
            zip.write_all(iwa).unwrap();
            zip.finish().unwrap();
        }
        archive
    }

    fn encode_varint(mut value: u64) -> Vec<u8> {
        let mut encoded = Vec::new();
        loop {
            let byte = (value & 0x7f) as u8;
            value >>= 7;
            encoded.push(if value == 0 { byte } else { byte | 0x80 });
            if value == 0 {
                return encoded;
            }
        }
    }
}