bilingua-model 0.10.0

Bilingua: the extracted-document model shared by every Bilingua file parser
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
//! Safety limits and the XML guard, one place for every parser (D27 step 5).
//!
//! The numbers are Localization OS's (`app/config.py`): a 100 MB input, 50 000 units, 100 000 characters in one
//! unit, twenty lines per allowed unit, a zip of at most 10 000 entries that unpacks to at most 256 MB with no
//! entry expanding more than 200 times. The caller can change any of them through `ExtractOptions::limits`.
//! Each refusal is a [`LimitError`] with a [`LimitKind`] a program can branch on and a short message that is
//! safe to show to the person who uploaded the file.
//!
//! The XML guard ([`check_xml`]) reads no DTD and expands no entity: a document that declares an entity
//! (internal or external, so the billion-laughs and XXE families) is refused before any parser sees it, and the
//! element depth and count are bounded. A plain `<!DOCTYPE x>` or `<!DOCTYPE x SYSTEM "x.dtd">` is allowed because
//! the parsers never fetch or apply it.

use crate::{Document, Error};
use serde::{Deserialize, Serialize};
use std::fmt;

/// Which limit a refusal is about.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum LimitKind {
    /// The file is too large.
    InputBytes,
    /// Too many lines.
    Lines,
    /// Too many units or segments.
    Units,
    /// A unit is too long.
    UnitChars,
    /// Too much key, note and other context text on the units (`Limits::max_context_bytes`).
    Context,
    /// XML nested too deeply.
    XmlDepth,
    /// Nesting deeper than allowed in a format that is not XML (HTML, JSON, YAML, ...). Uses
    /// `Limits::max_xml_depth`, which is the nesting cap for every format.
    Depth,
    /// Too many XML elements.
    XmlElements,
    /// An XML entity declaration (DTD): entity bombs and external entities.
    XmlEntities,
    /// More paragraphs in a word-processing file than allowed (empty ones count).
    Paragraphs,
    /// A spreadsheet over its cell, row, characters-per-cell or total-text limit.
    Spreadsheet,
    /// Too many zip entries.
    ZipEntries,
    /// Too many bytes once unpacked.
    ZipBytes,
    /// A zip entry expands too much (a zip bomb).
    ZipRatio,
    /// A zip whose declared sizes or names are unsafe or do not match its data.
    ZipMalformed,
    /// A merge would write more than `Limits::max_output_bytes`.
    OutputBytes,
    /// A stored document's skeleton is larger, or refers to its units more often (in all, or to any one unit), than
    /// any extraction makes it, or refers to a unit the document does not have. Merge only.
    Skeleton,
}

/// A file refused for being over a limit or unsafe.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct LimitError {
    /// Which limit.
    pub kind: LimitKind,
    /// The message, safe to show the person who uploaded the file.
    pub msg: String,
}

impl fmt::Display for LimitError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.msg)
    }
}

impl LimitError {
    /// A limit error of this kind with this message.
    pub fn new(kind: LimitKind, msg: impl Into<String>) -> LimitError {
        LimitError { kind, msg: msg.into() }
    }
}

impl From<LimitError> for Error {
    fn from(e: LimitError) -> Error {
        Error::Limit(e)
    }
}

/// The limits one extraction runs under. `Default` is Localization OS's configuration.
/// Build them with [`Limits::new`] and the `with_*` methods (`Limits::new().with_max_units(500)`); new limits are
/// added without breaking callers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct Limits {
    /// Largest input file (`MAX_UPLOAD_BYTES`).
    pub max_input_bytes: u64,
    /// Most units (and most segments) in a document (`MAX_DOCUMENT_SEGMENTS`).
    pub max_units: usize,
    /// Most characters in one unit (`MAX_SEGMENT_CHARS`).
    pub max_unit_chars: usize,
    /// Most lines in a text file: twenty per allowed unit, as LocOS's line budget.
    pub max_lines: usize,
    /// Deepest XML element nesting (libxml2's own default).
    pub max_xml_depth: usize,
    /// Most XML elements in one part.
    pub max_xml_elements: usize,
    /// Most entries in a zip (`ZIP_MAX_MEMBERS`).
    pub max_zip_entries: usize,
    /// Most bytes all entries of a zip unpack to (`ZIP_MAX_UNCOMPRESSED_BYTES`).
    pub max_zip_bytes: u64,
    /// Most bytes one zip entry may unpack to. LocOS has no cap of its own beyond the total, so the default is the
    /// same 256 MB; a caller may set it lower (or raise both for desktop use).
    pub max_zip_entry_bytes: u64,
    /// Largest unpacked/packed ratio of one entry (`ZIP_MAX_RATIO`).
    pub max_zip_ratio: u64,
    /// Entries smaller than this are not checked for ratio (`ZIP_RATIO_MIN_BYTES`).
    pub zip_ratio_floor_bytes: u64,
    /// Most paragraphs in a DOCX, empty ones included (`DOCX_MAX_PARAGRAPHS`).
    pub max_paragraphs: usize,
    /// Most cells in an XLSX (`XLSX_MAX_CELLS`).
    pub xlsx_max_cells: usize,
    /// Highest row number in an XLSX (`XLSX_MAX_ROWS`).
    pub xlsx_max_rows: u32,
    /// Most characters in one XLSX cell or shared string (`XLSX_MAX_CELL_CHARS`, Excel's own limit).
    pub xlsx_max_cell_chars: usize,
    /// Most characters of cell text in an XLSX, repeated shared strings counted every time
    /// (`XLSX_MAX_TOTAL_TEXT_CHARS`).
    pub xlsx_max_total_text_chars: usize,
    /// Most bytes of context the units of one document may carry together: keys, notes, headings, locations and
    /// the other strings of `UnitContext`. A string the file holds once and a parser copies into many units counts
    /// once per copy, so one long key or comment over many units cannot multiply the input.
    pub max_context_bytes: u64,
    /// Most bytes one merge may write (1 GB). Only a merge reads it: an ordinary file never comes near it, so it
    /// bounds what a stored Document that was changed by hand can make a merge produce.
    pub max_output_bytes: u64,
}

impl Default for Limits {
    fn default() -> Self {
        Limits {
            max_input_bytes: 100 * 1024 * 1024,
            max_units: 50_000,
            max_unit_chars: 100_000,
            max_lines: 20 * 50_000,
            max_xml_depth: 256,
            max_xml_elements: 4_000_000,
            max_zip_entries: 10_000,
            max_zip_bytes: 256 * 1024 * 1024,
            max_zip_entry_bytes: 256 * 1024 * 1024,
            max_zip_ratio: 200,
            zip_ratio_floor_bytes: 1024 * 1024,
            max_paragraphs: 50_000,
            xlsx_max_cells: 500_000,
            xlsx_max_rows: 100_000,
            xlsx_max_cell_chars: 32_767,
            xlsx_max_total_text_chars: 8 * 1024 * 1024,
            max_context_bytes: 128 * 1024 * 1024,
            max_output_bytes: 1024 * 1024 * 1024,
        }
    }
}

/// The largest count or size the `unbounded` profile allows (and [`Limits::clamped`] keeps any limit to): far beyond any real file, and small enough that the
/// budgets formats derive from a limit (`40 * max_units`, `20 * max_units` lines) cannot overflow.
/// On a 32-bit target (WASM) it is about 16 million, so `200 * max_units` still fits.
const UNBOUNDED_COUNT: usize = if usize::BITS >= 64 { usize::MAX >> 12 } else { usize::MAX >> 8 };
const UNBOUNDED_BYTES: u64 = u64::MAX >> 12;

impl Limits {
    /// Localization OS's limits (the same as `Limits::default()`); change any of them with the `with_*` methods.
    /// These are the limits for a server that takes uploads from people it does not know (`hosted`).
    pub fn new() -> Limits {
        Limits::default()
    }

    /// The limits for a person's own files on their own machine (the CLI, the desktop app, a batch job): large
    /// enough for any ordinary document (a 4 GB input, 20 million units or paragraphs, every row Excel allows,
    /// 200 million cells), so big manuals, spreadsheets and pages are processed at the cost of memory. The
    /// structural guards stay: XML nesting depth, zip entry count, a zip that unpacks to at most 4 GB (2 GB per
    /// part, more than a full million-row sheet needs) and a ratio check that still stops a zip bomb (1000:1
    /// instead of 200:1). It is the CLI's default, so it must stay safe for a downloaded attachment.
    pub fn large() -> Limits {
        Limits {
            max_input_bytes: 4 << 30,
            max_units: 20_000_000,
            max_unit_chars: 10_000_000,
            max_lines: 400_000_000,
            max_xml_depth: 256,
            max_xml_elements: 400_000_000,
            max_zip_entries: 200_000,
            max_zip_bytes: 4 << 30,
            max_zip_entry_bytes: 2 << 30,
            max_zip_ratio: 1000,
            zip_ratio_floor_bytes: 1024 * 1024,
            max_paragraphs: 20_000_000,
            xlsx_max_cells: 200_000_000,
            xlsx_max_rows: 1_048_576,
            xlsx_max_cell_chars: 32_767,
            xlsx_max_total_text_chars: 2_000_000_000,
            max_context_bytes: 16 << 30,
            max_output_bytes: 32 << 30,
        }
    }

    /// No size or count limits at all: only what the machine's memory allows. The XML depth limit stays (it
    /// protects the stack, and no real document nests 256 elements deep). Use it only for trusted files.
    pub fn unbounded() -> Limits {
        Limits {
            max_input_bytes: UNBOUNDED_BYTES,
            max_units: UNBOUNDED_COUNT,
            max_unit_chars: UNBOUNDED_COUNT,
            max_lines: UNBOUNDED_COUNT,
            max_xml_depth: 256,
            max_xml_elements: UNBOUNDED_COUNT,
            max_zip_entries: UNBOUNDED_COUNT,
            max_zip_bytes: UNBOUNDED_BYTES,
            max_zip_entry_bytes: UNBOUNDED_BYTES,
            max_zip_ratio: UNBOUNDED_BYTES,
            zip_ratio_floor_bytes: 1024 * 1024,
            max_paragraphs: UNBOUNDED_COUNT,
            xlsx_max_cells: UNBOUNDED_COUNT,
            xlsx_max_rows: u32::MAX >> 4,
            xlsx_max_cell_chars: UNBOUNDED_COUNT,
            xlsx_max_total_text_chars: UNBOUNDED_COUNT,
            max_context_bytes: UNBOUNDED_BYTES,
            max_output_bytes: UNBOUNDED_BYTES,
        }
    }

    /// A profile by name, any case: `hosted` (the default, [`Limits::new`]; also `default`, `server`), `large`
    /// ([`Limits::large`]; also `desktop`, `local`) or `unbounded` ([`Limits::unbounded`]; also `none`). `None`
    /// for any other name.
    pub fn profile(name: &str) -> Option<Limits> {
        match name.trim().to_ascii_lowercase().as_str() {
            "hosted" | "default" | "server" => Some(Limits::new()),
            "large" | "desktop" | "local" => Some(Limits::large()),
            "unbounded" | "none" => Some(Limits::unbounded()),
            _ => None,
        }
    }

    /// The same limits with every count and size kept to what [`Limits::unbounded`] allows, so no budget a
    /// format derives from a limit (`200 * max_units`, `10 * max_units as i64`) can overflow. Extract and merge
    /// apply it to the caller's limits; it changes nothing under `hosted` or `large`.
    pub fn clamped(self) -> Limits {
        let u = Limits::unbounded();
        Limits {
            max_input_bytes: self.max_input_bytes.min(u.max_input_bytes),
            max_units: self.max_units.min(u.max_units),
            max_unit_chars: self.max_unit_chars.min(u.max_unit_chars),
            max_lines: self.max_lines.min(u.max_lines),
            max_xml_depth: self.max_xml_depth,
            max_xml_elements: self.max_xml_elements.min(u.max_xml_elements),
            max_zip_entries: self.max_zip_entries.min(u.max_zip_entries),
            max_zip_bytes: self.max_zip_bytes.min(u.max_zip_bytes),
            max_zip_entry_bytes: self.max_zip_entry_bytes.min(u.max_zip_entry_bytes),
            max_zip_ratio: self.max_zip_ratio.min(u.max_zip_ratio),
            zip_ratio_floor_bytes: self.zip_ratio_floor_bytes,
            max_paragraphs: self.max_paragraphs.min(u.max_paragraphs),
            xlsx_max_cells: self.xlsx_max_cells.min(u.xlsx_max_cells),
            xlsx_max_rows: self.xlsx_max_rows.min(u.xlsx_max_rows),
            xlsx_max_cell_chars: self.xlsx_max_cell_chars.min(u.xlsx_max_cell_chars),
            xlsx_max_total_text_chars: self.xlsx_max_total_text_chars.min(u.xlsx_max_total_text_chars),
            max_context_bytes: self.max_context_bytes.min(u.max_context_bytes),
            max_output_bytes: self.max_output_bytes.min(u.max_output_bytes),
        }
    }
    /// Sets `max_input_bytes`.
    pub fn with_max_input_bytes(mut self, v: u64) -> Limits {
        self.max_input_bytes = v;
        self
    }
    /// Sets `max_units`.
    pub fn with_max_units(mut self, v: usize) -> Limits {
        self.max_units = v;
        self
    }
    /// Sets `max_unit_chars`.
    pub fn with_max_unit_chars(mut self, v: usize) -> Limits {
        self.max_unit_chars = v;
        self
    }
    /// Sets `max_lines`.
    pub fn with_max_lines(mut self, v: usize) -> Limits {
        self.max_lines = v;
        self
    }
    /// Sets `max_xml_depth`.
    pub fn with_max_xml_depth(mut self, v: usize) -> Limits {
        self.max_xml_depth = v;
        self
    }
    /// Sets `max_xml_elements`.
    pub fn with_max_xml_elements(mut self, v: usize) -> Limits {
        self.max_xml_elements = v;
        self
    }
    /// Sets `max_zip_entries`.
    pub fn with_max_zip_entries(mut self, v: usize) -> Limits {
        self.max_zip_entries = v;
        self
    }
    /// Sets `max_zip_bytes`.
    pub fn with_max_zip_bytes(mut self, v: u64) -> Limits {
        self.max_zip_bytes = v;
        self
    }
    /// Sets `max_zip_entry_bytes`.
    pub fn with_max_zip_entry_bytes(mut self, v: u64) -> Limits {
        self.max_zip_entry_bytes = v;
        self
    }
    /// Sets `max_zip_ratio`.
    pub fn with_max_zip_ratio(mut self, v: u64) -> Limits {
        self.max_zip_ratio = v;
        self
    }
    /// Sets `zip_ratio_floor_bytes`.
    pub fn with_zip_ratio_floor_bytes(mut self, v: u64) -> Limits {
        self.zip_ratio_floor_bytes = v;
        self
    }
    /// Sets `max_paragraphs`.
    pub fn with_max_paragraphs(mut self, v: usize) -> Limits {
        self.max_paragraphs = v;
        self
    }
    /// Sets `xlsx_max_cells`.
    pub fn with_xlsx_max_cells(mut self, v: usize) -> Limits {
        self.xlsx_max_cells = v;
        self
    }
    /// Sets `xlsx_max_rows`.
    pub fn with_xlsx_max_rows(mut self, v: u32) -> Limits {
        self.xlsx_max_rows = v;
        self
    }
    /// Sets `xlsx_max_cell_chars`.
    pub fn with_xlsx_max_cell_chars(mut self, v: usize) -> Limits {
        self.xlsx_max_cell_chars = v;
        self
    }
    /// Sets `xlsx_max_total_text_chars`.
    pub fn with_xlsx_max_total_text_chars(mut self, v: usize) -> Limits {
        self.xlsx_max_total_text_chars = v;
        self
    }
    /// Sets `max_context_bytes`.
    pub fn with_max_context_bytes(mut self, v: u64) -> Limits {
        self.max_context_bytes = v;
        self
    }
    /// Sets `max_output_bytes`.
    pub fn with_max_output_bytes(mut self, v: u64) -> Limits {
        self.max_output_bytes = v;
        self
    }
}

/// A format crate's own cap under the caller's limit for the same thing. While the caller's number is at or below
/// the hosted one (`hosted`, the field of [`Limits::new`]) the cap is the smaller of the crate's own and the
/// caller's, so a server gets exactly the crate's numbers it always had; once the caller raised it above the
/// hosted number (`Limits::large`, `Limits::unbounded`, a desktop's own choice) the caller's number decides, so a
/// raised profile raises every format.
pub fn own_cap<T: Ord + Copy>(own: T, hosted: T, caller: T) -> T {
    if caller > hosted {
        caller
    } else {
        own.min(caller)
    }
}

/// [`own_cap`] for a byte cap kept as `usize` against a `u64` limit.
pub fn own_cap_bytes(own: usize, hosted: u64, caller: u64) -> usize {
    let c = usize::try_from(caller).unwrap_or(usize::MAX);
    if caller > hosted {
        c
    } else {
        own.min(c)
    }
}

fn mb(n: u64) -> String {
    if n >= 1024 * 1024 {
        format!("{} MB", n / (1024 * 1024))
    } else {
        format!("{n} bytes")
    }
}

/// Refuses an input over `max_input_bytes`. Call before any other work on the bytes.
pub fn check_input(bytes: &[u8], l: &Limits) -> Result<(), Error> {
    if bytes.len() as u64 > l.max_input_bytes {
        return Err(LimitError::new(
            LimitKind::InputBytes,
            format!(
                "This file is {} long; the limit is {}. Split it into smaller files.",
                mb(bytes.len() as u64),
                mb(l.max_input_bytes)
            ),
        )
        .into());
    }
    Ok(())
}

/// Refuses a text with more lines than the line budget, before any per-line work (a file of 100 MB of newlines
/// would otherwise build a list of 100 million lines).
pub fn check_lines(text: &str, l: &Limits) -> Result<(), Error> {
    let n = text.bytes().filter(|b| *b == b'\n' || *b == b'\r').count();
    if n > l.max_lines {
        return Err(LimitError::new(
            LimitKind::Lines,
            format!("This file has more than {} lines. Split it into smaller files.", l.max_lines),
        )
        .into());
    }
    Ok(())
}

/// The running-total check for a parser that counts units as it goes: refuse as soon as `count` passes the cap,
/// so a hostile file stops early.
pub fn check_unit_count(count: usize, l: &Limits) -> Result<(), Error> {
    if count > l.max_units {
        return Err(LimitError::new(
            LimitKind::Units,
            format!("This file has more than {} segments. Split it into smaller files.", l.max_units),
        )
        .into());
    }
    Ok(())
}

/// LocOS's `office_comments.COMMENT_BASE`: the Office formats (ODF, PPTX, VSDX, XLSX) number comment units from
/// here, above every body paragraph, cell or text box, so a file read without comments keeps its numbers.
pub const COMMENT_BASE: usize = 1_000_000;

/// BILI-126: refuses a file whose body numbers reach [`COMMENT_BASE`] while it has comment units. `body_end` is one
/// past the highest body number, counted the way the format numbers its body (empty slots too where they count).
/// No body gets there under the hosted limits' unit cap; under `Limits::large()` one can, and a comment would then
/// share its number with a body unit, so a merge could write a comment's translation into the body or the reverse.
/// Refusing keeps LocOS's one numbering for every file below the line rather than a second scheme for the few above
/// it. Call it before the first comment unit is made; a merge calls [`check_comment_base_merge`].
pub fn check_comment_base(body_end: usize) -> Result<(), Error> {
    if body_end > COMMENT_BASE {
        return Err(LimitError::new(
            LimitKind::Units,
            "This file has more than 1,000,000 paragraphs, cells or text boxes and also has comments to translate, \
             which is more than Bilingua can number apart. Turn off comment translation or split the file.",
        )
        .into());
    }
    Ok(())
}

/// The merge-time refusal of BILI-126: the document's comments and body share numbers. An extraction never makes
/// such a document, so it was changed by hand or put together from another file.
fn comment_numbers_shared() -> Error {
    LimitError::new(
        LimitKind::Units,
        "This document's comments and body text share numbers, so it cannot be merged safely. Extract the file again without comments.",
    )
    .into()
}

/// [`check_comment_base`] for a merge that writes comments by number: a document with comment units over a body
/// that reaches [`COMMENT_BASE`] is refused.
pub fn check_comment_base_merge(body_end: usize) -> Result<(), Error> {
    if body_end > COMMENT_BASE {
        return Err(comment_numbers_shared());
    }
    Ok(())
}

/// BILI-126: refuses a merge whose units carry one number twice (`numbers`: every unit's LocOS number), whatever
/// their kind or hidden fields, so a hand edit that strips a comment's marks cannot slip a comment into the body.
pub fn check_distinct_numbers(numbers: impl IntoIterator<Item = i64>) -> Result<(), Error> {
    let mut seen = std::collections::HashSet::new();
    if numbers.into_iter().any(|n| !seen.insert(n)) {
        return Err(comment_numbers_shared());
    }
    Ok(())
}

/// Localization OS's pre-parse budget (#571 and its gate): a text with more than `per_unit` x `max_units` of some
/// marker (`count`: JSON quotes, or commas and brackets; PO backslashes) is refused before the parser builds
/// anything, since each marker costs work or memory whether or not it ends in a unit (100 MB of `{},` holds no
/// text but millions of values). `what` names the file and the marker ("This JSON file has far more values").
pub fn check_budget(count: usize, per_unit: usize, what: &str, l: &Limits) -> Result<(), Error> {
    if count > per_unit.saturating_mul(l.max_units) {
        return Err(LimitError::new(
            LimitKind::Units,
            format!("{what} than a file of {} segments. Split it into smaller files.", l.max_units),
        )
        .into());
    }
    Ok(())
}

/// Refuses a unit text longer than `max_unit_chars` characters.
pub fn check_unit_chars(chars: usize, l: &Limits) -> Result<(), Error> {
    if chars > l.max_unit_chars {
        return Err(LimitError::new(
            LimitKind::UnitChars,
            format!(
                "This file has a line or paragraph longer than {} characters. Break it into shorter lines.",
                l.max_unit_chars
            ),
        )
        .into());
    }
    Ok(())
}

/// Refuses a running total of context bytes over `max_context_bytes`.
pub fn check_context_bytes(total: u64, l: &Limits) -> Result<(), Error> {
    if total > l.max_context_bytes {
        return Err(LimitError::new(
            LimitKind::Context,
            format!(
                "This file carries more key, comment and note text than the limit of {} allows. Split it into smaller files.",
                mb(l.max_context_bytes)
            ),
        )
        .into());
    }
    Ok(())
}

/// The bytes of context one unit carries: every string of its `UnitContext`.
pub fn unit_context_bytes(c: &crate::UnitContext) -> u64 {
    let opt = |s: &Option<String>| s.as_ref().map_or(0, |v| v.len() as u64);
    opt(&c.kind)
        + opt(&c.style)
        + opt(&c.key)
        + opt(&c.heading)
        + opt(&c.location)
        + c.notes.iter().map(|n| n.len() as u64).sum::<u64>()
        + c.extra.iter().map(|(k, v)| (k.len() + v.len()) as u64).sum::<u64>()
}

/// The running context budget for a parser that copies a shared string (a key path, a section name, a comment)
/// into many units: charge every copy before it is made, so a hostile file is refused before the memory is
/// taken. `check_document` applies the same cap to the finished document as the safety net.
#[derive(Debug, Clone, Copy, Default)]
pub struct ContextBudget {
    used: u64,
}

impl ContextBudget {
    /// A budget with nothing charged.
    pub fn new() -> ContextBudget {
        ContextBudget::default()
    }
    /// Charges `bytes` more of context and refuses once the total passes `max_context_bytes`.
    pub fn charge(&mut self, bytes: usize, l: &Limits) -> Result<(), Error> {
        self.used = self.used.saturating_add(bytes as u64);
        check_context_bytes(self.used, l)
    }
    /// The bytes charged so far.
    pub fn used(&self) -> u64 {
        self.used
    }
}

/// The check for a finished extraction: unit and segment counts, the length of every unit, and the context
/// bytes of all units together.
pub fn check_document(doc: &Document, l: &Limits) -> Result<(), Error> {
    check_unit_count(doc.units.len(), l)?;
    check_unit_count(doc.segment_count(), l)?;
    let mut context: u64 = 0;
    for u in &doc.units {
        context = context.saturating_add(unit_context_bytes(&u.context));
        check_context_bytes(context, l)?;
        let chars: usize = u
            .segments
            .iter()
            .flat_map(|s| s.source.iter())
            .map(|i| match i {
                crate::Inline::Text(t) => t.chars().count(),
                crate::Inline::Tag { .. } => 1,
            })
            .sum();
        check_unit_chars(chars, l)?;
    }
    Ok(())
}

/// How often a skeleton may refer to units: in all, per allowed unit (`max_units`), and to any one unit.
/// Extraction writes each unit into the skeleton once or a few times; a stored Document that refers to one unit
/// many more times would make a merge write that unit over and over (a small Document, a large output).
pub const MERGE_REFS_PER_UNIT: usize = 4;

/// How long a stored target may be, as a multiple of `max_unit_chars` (a source over that one is refused at
/// extract; a translation may be longer than its source, but not without bound).
pub const MERGE_TARGET_CHARS_FACTOR: usize = 4;

/// The checks a merge runs on a stored Document before writing it (BILI-105). A Document is the caller's data, and
/// a host should keep a stored one where only it can write it, but it is plain JSON between extract and merge, so
/// these bound what one that was edited by hand can make a merge do, under the caller's merge limits
/// (`MergeOptions::limits`):
/// * the checks of an extraction ([`check_document`]): units and segments within `max_units`, every unit's source
///   within `max_unit_chars`, the context within `max_context_bytes`; and every unit's target within
///   [`MERGE_TARGET_CHARS_FACTOR`] x `max_unit_chars`;
/// * no more zip entries than `max_zip_entries`, and every zip entry name one the zip reader gives
///   ([`check_zip_name`]), none listed twice (case and `\` against `/` ignored, as the reader compares them);
/// * at most [`MERGE_REFS_PER_UNIT`] x `max_units` unit slots in all, at most [`MERGE_REFS_PER_UNIT`] to any one
///   unit, none to a unit the document does not have, and `max_lines` + the slot cap skeleton parts
///   ([`LimitKind::Skeleton`]);
/// * an estimate of the output (the skeleton's own bytes, each unit slot counted at its unit's size, and the units
///   once more for formats that rebuild from them) within `max_output_bytes`.
///
/// An ordinary extraction is far inside every one of these.
pub fn check_merge(doc: &Document, l: &Limits) -> Result<(), Error> {
    check_document(doc, l)?;
    check_target_chars(doc, l)?;
    let max_refs = MERGE_REFS_PER_UNIT.saturating_mul(l.max_units);
    let max_parts = l.max_lines.saturating_add(max_refs);
    let sizes: Vec<u64> = doc.units.iter().map(unit_output_bytes).collect();
    let mut walk = SkeletonWalk { parts: 0, refs: 0, per_unit: vec![0; sizes.len()], estimate: sizes.iter().fold(0u64, |a, b| a.saturating_add(*b)) };
    check_output_estimate(walk.estimate, l)?;
    match &doc.skeleton {
        crate::Skeleton::Text { parts, .. } => walk.parts(parts, &sizes, max_parts, max_refs, l)?,
        crate::Skeleton::Zip { entries, .. } => {
            if entries.len() > l.max_zip_entries {
                return Err(LimitError::new(
                    LimitKind::ZipEntries,
                    format!("This document lists more than {} zip entries. It was not written.", l.max_zip_entries),
                )
                .into());
            }
            let mut seen = std::collections::HashSet::with_capacity(entries.len());
            for e in entries {
                // a format's own records (`\0bilingua-docx-meta`, ...) are never written as entries: the zip
                // writers refuse a name with a control character, so one changed by hand cannot reach the output
                if e.name.starts_with(INTERNAL_ENTRY_PREFIX) {
                    continue;
                }
                check_zip_name(&e.name)?;
                if !seen.insert(e.name.to_ascii_lowercase()) {
                    return Err(zip_name_error(&e.name, "is listed twice"));
                }
            }
            for e in entries {
                match &e.data {
                    crate::ZipData::Raw(b) => {
                        walk.estimate = walk.estimate.saturating_add(b.len() as u64);
                        check_output_estimate(walk.estimate, l)?;
                    }
                    crate::ZipData::Parts(parts) => walk.parts(parts, &sizes, max_parts, max_refs, l)?,
                }
            }
        }
        crate::Skeleton::Binary { data, .. } => {
            walk.estimate = walk.estimate.saturating_add(data.len() as u64);
            check_output_estimate(walk.estimate, l)?;
        }
    }
    Ok(())
}

/// Refuses a stored target longer than [`MERGE_TARGET_CHARS_FACTOR`] x `max_unit_chars` (codes count one each,
/// as in [`check_document`]).
fn check_target_chars(doc: &Document, l: &Limits) -> Result<(), Error> {
    let max = l.max_unit_chars.saturating_mul(MERGE_TARGET_CHARS_FACTOR);
    for u in &doc.units {
        let chars: usize = u
            .segments
            .iter()
            .flat_map(|s| s.target.iter())
            .map(|i| match i {
                crate::Inline::Text(t) => t.chars().count(),
                crate::Inline::Tag { .. } => 1,
            })
            .fold(0usize, usize::saturating_add);
        if chars > max {
            return Err(LimitError::new(
                LimitKind::UnitChars,
                format!("This document has a translation longer than {max} characters. It was not written."),
            )
            .into());
        }
    }
    Ok(())
}

/// The prefix of the zip skeleton entries a format keeps for itself (its metadata, the original archive), which no
/// writer puts in the output archive.
pub const INTERNAL_ENTRY_PREFIX: &str = "\0bilingua-";

fn zip_name_error(raw: &str, what: &str) -> Error {
    // the name is the caller's own data; shown escaped and cut short
    let shown: String = raw.chars().take(80).flat_map(char::escape_debug).collect();
    LimitError::new(LimitKind::ZipMalformed, format!("This document has a zip entry name that {what} (\"{shown}\"). It was not written.")).into()
}

/// Refuses a zip entry name the zip reader never gives (BILI-105): a stored Document's names are written into the
/// output archive, so one that was changed by hand must not reach it. A name the reader gives is relative, uses
/// `/`, and has no empty, `.` or `..` component, no component made only of dots and spaces, no colon and no
/// control character; a folder ends in `/`. Duplicates are checked over the whole list by [`check_merge`].
pub fn check_zip_name(raw: &str) -> Result<(), Error> {
    if raw.is_empty() {
        return Err(zip_name_error(raw, "is empty"));
    }
    if raw.chars().any(char::is_control) {
        return Err(zip_name_error(raw, "holds a control character"));
    }
    if raw.starts_with('/') || raw.contains('\\') {
        return Err(zip_name_error(raw, "is an absolute path or uses a backslash"));
    }
    if raw.contains(':') {
        return Err(zip_name_error(raw, "contains a colon"));
    }
    let body = raw.strip_suffix('/').unwrap_or(raw);
    for part in body.split('/') {
        if part == ".." {
            return Err(zip_name_error(raw, "climbs out of the archive (..)"));
        }
        if part.is_empty() || part.trim_end_matches(['.', ' ']).is_empty() {
            return Err(zip_name_error(raw, "has an empty component or one made of dots or spaces"));
        }
    }
    Ok(())
}

/// The running totals of [`check_merge`] over every part list of a skeleton.
struct SkeletonWalk {
    parts: usize,
    refs: usize,
    /// References so far to each unit.
    per_unit: Vec<usize>,
    estimate: u64,
}

impl SkeletonWalk {
    fn parts(&mut self, parts: &[crate::SkelPart], sizes: &[u64], max_parts: usize, max_refs: usize, l: &Limits) -> Result<(), Error> {
        self.parts = self.parts.saturating_add(parts.len());
        if self.parts > max_parts {
            return Err(over_skeleton(l));
        }
        for p in parts {
            match p {
                crate::SkelPart::Raw(s) => self.estimate = self.estimate.saturating_add(s.len() as u64),
                crate::SkelPart::Unit(i) => {
                    self.refs += 1;
                    if self.refs > max_refs {
                        return Err(over_skeleton(l));
                    }
                    let Some(n) = self.per_unit.get_mut(*i) else {
                        return Err(LimitError::new(
                            LimitKind::Skeleton,
                            format!("This document's skeleton refers to unit {i}, which the document does not have. It was not written."),
                        )
                        .into());
                    };
                    *n += 1;
                    if *n > MERGE_REFS_PER_UNIT {
                        return Err(LimitError::new(
                            LimitKind::Skeleton,
                            format!(
                                "This document's skeleton writes one unit more than {MERGE_REFS_PER_UNIT} times, more than any extraction makes it. It was not written."
                            ),
                        )
                        .into());
                    }
                    self.estimate = self.estimate.saturating_add(sizes[*i]);
                }
            }
        }
        check_output_estimate(self.estimate, l)
    }
}

/// Refuses merge output over `max_output_bytes` (the facade calls it on what the writer returned).
pub fn check_output(len: usize, l: &Limits) -> Result<(), Error> {
    check_output_estimate(len as u64, l)
}

fn check_output_estimate(n: u64, l: &Limits) -> Result<(), Error> {
    if n > l.max_output_bytes {
        return Err(LimitError::new(
            LimitKind::OutputBytes,
            format!("This document would write more than {}, the output limit. It was not written.", mb(l.max_output_bytes)),
        )
        .into());
    }
    Ok(())
}

fn over_skeleton(l: &Limits) -> Error {
    LimitError::new(
        LimitKind::Skeleton,
        format!(
            "This document's skeleton is larger, or refers to its units more often, than a file of {} segments. It was not written.",
            l.max_units
        ),
    )
    .into()
}

/// What writing one unit costs at least: per segment the larger of its source and target, each code counted at
/// its stored markup.
fn unit_output_bytes(u: &crate::Unit) -> u64 {
    let inlines = |v: &[crate::Inline]| -> u64 {
        v.iter()
            .map(|i| match i {
                crate::Inline::Text(t) => t.len() as u64,
                crate::Inline::Tag { id, kind, display, .. } => {
                    let code = u.codes.get(id).and_then(|c| match kind {
                        crate::TagKind::Open => c.open.as_ref(),
                        crate::TagKind::Close => c.close.as_ref(),
                        crate::TagKind::Standalone => c.standalone.as_ref(),
                    });
                    code.map_or(display.len(), String::len) as u64
                }
            })
            .fold(0u64, u64::saturating_add)
    };
    u.segments.iter().map(|s| inlines(&s.source).max(inlines(&s.target))).fold(0u64, u64::saturating_add)
}

/// Checks every stored target's tags as a merge reads them (BILI-105): a tag must be one of its segment's source
/// tags (same id and kind) or have its markup in the unit's `codes`. With `counts`, a paired tag may also appear no more often than in the source, the
/// rule of [`Document::set_target_repeating`](crate::Document::set_target_repeating); without it repeats pass.
pub fn check_targets(doc: &Document, counts: bool) -> Result<(), Error> {
    for u in &doc.units {
        for s in &u.segments {
            if !s.target.is_empty() {
                crate::check_target_tags(&s.source, &s.target, &u.codes, counts)?;
            }
        }
    }
    Ok(())
}

/// What every parser ends with: the document passes `check_document`, and the warnings of the text decode go on it.
pub fn finish(mut doc: Document, warnings: Vec<crate::Warning>, l: &Limits) -> Result<Document, Error> {
    check_document(&doc, l)?;
    doc.warnings.extend(warnings);
    Ok(doc)
}

fn unsafe_xml(msg: impl Into<String>) -> Error {
    LimitError::new(LimitKind::XmlEntities, msg).into()
}

fn find(hay: &[u8], from: usize, needle: &[u8]) -> Option<usize> {
    if from >= hay.len() {
        return None;
    }
    hay[from..].windows(needle.len()).position(|w| w == needle).map(|p| p + from)
}

fn contains_ci(hay: &[u8], needle: &[u8]) -> bool {
    hay.windows(needle.len()).any(|w| w.eq_ignore_ascii_case(needle))
}

/// Reads XML as bytes, transcoding UTF-16 (BOM or `<` in 16-bit form) so the scan sees ASCII markup.
fn markup_bytes(bytes: &[u8]) -> std::borrow::Cow<'_, [u8]> {
    let le = bytes.starts_with(&[0xFF, 0xFE]) || bytes.starts_with(&[b'<', 0]);
    let be = bytes.starts_with(&[0xFE, 0xFF]) || bytes.starts_with(&[0, b'<']);
    if le && !bytes.starts_with(&[0xFF, 0xFE, 0, 0]) {
        let (s, _) = encoding_rs::UTF_16LE.decode_without_bom_handling(bytes);
        return std::borrow::Cow::Owned(s.into_owned().into_bytes());
    }
    if be {
        let (s, _) = encoding_rs::UTF_16BE.decode_without_bom_handling(bytes);
        return std::borrow::Cow::Owned(s.into_owned().into_bytes());
    }
    std::borrow::Cow::Borrowed(bytes)
}

/// The XML guard: scans the markup of one XML part once and refuses
/// * any `<!ENTITY` declaration (an entity bomb or an external entity; `&amp; &lt; &gt; &quot; &apos;` and
///   character references are all a document needs),
/// * nesting deeper than `max_xml_depth`,
/// * more than `max_xml_elements` elements.
///
/// Comments, CDATA sections and processing instructions are skipped, so markup inside them is not counted.
///
/// The count is only as good as the reader's agreement on where those regions end, so a part is also refused when
/// the readers would not agree: a DOCTYPE that never ends, has a `<`, `>` or bracket in a quoted string, a bracket
/// or angle bracket in a comment of its internal subset (a quote there is fine, but then no `<!ENTITY` may follow
/// the DOCTYPE anywhere), a quote in a processing instruction of the subset, or a `<`/`>` that `quick-xml` pairs
/// differently; a comment that
/// starts with `>` or `->`; `<?>`; a `<!` declaration outside the DOCTYPE. Every reading a reader may give the
/// bytes is scanned: UTF-16 and UTF-8 for `<\0` without a byte order mark, and the double-byte code pages for
/// bytes that are not UTF-8.
pub fn check_xml(bytes: &[u8], l: &Limits) -> Result<(), Error> {
    check_xml_with(bytes, l, false, false)
}

/// Like [`check_xml`], but a DOCTYPE may declare plain internal entities: `<!ENTITY name "literal">` with a literal
/// that holds no `&` (so no entity expands into another), at most 1024 bytes long, and at most 64 of them. Adobe Illustrator, Inkscape and
/// Sodipodi SVGs declare a few of these for namespace URIs. External (`SYSTEM`, `PUBLIC`) and parameter (`%`)
/// entities are refused as before, and every other check of [`check_xml`] still applies. For a reader that never
/// expands an entity (the SVG filter keeps every `&name;` as typed).
pub fn check_xml_internal_entities(bytes: &[u8], l: &Limits) -> Result<(), Error> {
    check_xml_with(bytes, l, false, true)
}

/// True when every `<!ENTITY` declaration in a DOCTYPE is `<!ENTITY name "literal">` (or single quotes) and the
/// literal has no `&`; false for an external or parameter entity, a malformed one, or more than 64.
fn only_plain_internal_entities(doctype: &[u8]) -> bool {
    let mut n = 0usize;
    let mut from = 0usize;
    while let Some(p) = doctype[from..].windows(8).position(|w| w.eq_ignore_ascii_case(b"<!ENTITY")) {
        n += 1;
        if n > 64 {
            return false;
        }
        let mut j = from + p + 8;
        let ws = |j: &mut usize| {
            let s = *j;
            while *j < doctype.len() && matches!(doctype[*j], b' ' | b'\t' | b'\r' | b'\n') {
                *j += 1;
            }
            *j > s
        };
        if !ws(&mut j) {
            return false;
        }
        let name_start = j;
        while j < doctype.len() && (doctype[j].is_ascii_alphanumeric() || matches!(doctype[j], b'_' | b'-' | b'.' | b':')) {
            j += 1;
        }
        if j == name_start || !ws(&mut j) || j >= doctype.len() {
            return false;
        }
        let q = doctype[j];
        if q != b'"' && q != b'\'' {
            return false;
        }
        let Some(len) = doctype[j + 1..].iter().position(|c| *c == q) else { return false };
        // no `&`: nothing expands into anything; at most 1024 bytes: a few references cannot blow up a reader
        // that does expand them (the file is written back as it is, for browsers and editors to read)
        if len > 1024 || doctype[j + 1..j + 1 + len].contains(&b'&') {
            return false;
        }
        from = j + 1 + len + 1;
    }
    true
}

/// Like [`check_xml`] for document formats where a DTD with entity declarations is normal (DocBook, DITA, plain
/// XML): the declarations are allowed, and the depth and element limits still apply. This is safe only for a reader
/// that never expands or fetches an entity, as the XML filter family does: it keeps every `&name;` as an opaque
/// code and writes the DOCTYPE back byte for byte.
pub fn check_xml_keeping_dtd(bytes: &[u8], l: &Limits) -> Result<(), Error> {
    check_xml_with(bytes, l, true, false)
}

/// End of a `<!DOCTYPE ...>` that starts at `i`: the index of its closing `>` (or `b.len()`). Quoted strings, the
/// `[ ... ]` internal subset and the comments and processing instructions inside it are skipped.
fn doctype_end(b: &[u8], i: usize) -> usize {
    let (mut j, mut sq, mut subset) = (i + 9, 0u8, 0usize);
    while j < b.len() {
        let c = b[j];
        if sq != 0 {
            if c == sq {
                sq = 0;
            }
        } else if c == b'"' || c == b'\'' {
            sq = c;
        } else if subset > 0 && b[j..].starts_with(b"<!--") {
            match find(b, j + 4, b"-->") {
                Some(e) => j = e + 2,
                None => return b.len(),
            }
        } else if subset > 0 && b[j..].starts_with(b"<?") {
            match find(b, j + 2, b"?>") {
                Some(e) => j = e + 1,
                None => return b.len(),
            }
        } else if c == b'[' {
            subset += 1;
        } else if c == b']' {
            subset = subset.saturating_sub(1);
        } else if c == b'>' && subset == 0 {
            break;
        }
        j += 1;
    }
    j.min(b.len())
}

/// Where `quick-xml` ends the `<!DOCTYPE` that starts at `i`: the first `>` outside a `<` ... `>` pair, quotes,
/// brackets and comments not looked at (the index of that `>`, or `None` when there is none).
fn reader_doctype_end(b: &[u8], i: usize) -> Option<usize> {
    let mut balance = 0usize;
    for (j, c) in b.iter().enumerate().skip(i + 2) {
        match c {
            b'<' => balance += 1,
            b'>' if balance == 0 => return Some(j),
            b'>' => balance -= 1,
            _ => {}
        }
    }
    None
}

/// `Some` when every XML reader in the workspace ends the DOCTYPE at `i..=end` (`end` from [`doctype_end`]) at
/// the same `>`. The readers differ in what they skip: `quick-xml` only pairs `<` with `>` (and skips the comments
/// of the subset whole); the hand-written scanners skip quoted strings, some also the `[ ... ]` subset, some the
/// comments or processing instructions inside it. They all agree when no quoted string holds a `<`, `>`, `[` or
/// `]`, no comment or processing instruction of the subset holds a bracket or an angle bracket of its own, no
/// processing instruction a quote, and `quick-xml` stops at the same place. Real files meet that easily
/// (`<!DOCTYPE plist PUBLIC "..." "...">`, an XMB subset of `<!ELEMENT>` and `<!ATTLIST>` declarations).
///
/// A quote in a comment of the subset (`<!-- don't edit -->`, BILI-60) ends the DOCTYPE in the same place for
/// every reader that skips the comment; `Some(true)` says the DOCTYPE has one, so the caller can make sure a
/// scanner that pairs that quote instead could not find an entity declaration in what follows.
fn doctype_is_unambiguous(b: &[u8], i: usize, end: usize) -> Option<bool> {
    if end >= b.len() || reader_doctype_end(b, i) != Some(end) {
        return None;
    }
    let mut quoted_comment = false;
    let special = |s: &[u8], also_quotes: bool| s.iter().any(|c| matches!(c, b'<' | b'>' | b'[' | b']') || (also_quotes && matches!(c, b'"' | b'\'')));
    let (mut j, mut subset) = (i + 9, 0usize);
    while j < end {
        let c = b[j];
        if c == b'"' || c == b'\'' {
            let q = b[j + 1..end].iter().position(|x| *x == c)?;
            if special(&b[j + 1..j + 1 + q], false) {
                return None;
            }
            j += q + 2;
            continue;
        }
        if subset > 0 && b[j..].starts_with(b"<!--") {
            let e = find(b, j + 4, b"-->").filter(|e| *e < end)?;
            let body = &b[j + 4..e];
            if special(body, false) {
                return None;
            }
            quoted_comment |= body.iter().any(|c| matches!(c, b'"' | b'\''));
            j = e + 3;
            continue;
        }
        if subset > 0 && b[j..].starts_with(b"<?") {
            let e = find(b, j + 2, b"?>").filter(|e| *e < end)?;
            if special(&b[j + 2..e], true) {
                return None;
            }
            j = e + 2;
            continue;
        }
        if c == b'[' {
            subset += 1;
        } else if c == b']' {
            subset = subset.saturating_sub(1);
        }
        j += 1;
    }
    Some(quoted_comment)
}

fn ambiguous_markup(what: &str) -> Error {
    LimitError::new(
        LimitKind::XmlElements,
        format!("This file has {what}, which XML readers do not agree on, so its elements could not be counted safely. It was not processed."),
    )
    .into()
}

/// Every way a reader in the workspace may read `bytes` as markup. A UTF-16 byte order mark leaves one reading.
/// Without one, `<\0` or `\0<` is UTF-16 for the readers that sniff it (EPUB, IDML) but UTF-8 with a NUL for the
/// ones that decode with [`crate::enc::decode_text`], so both are scanned. Bytes that are not UTF-8 may be read
/// in a double-byte code page whose second byte can be `[` or `]` (Shift_JIS, GBK/GB18030, Big5: a CDATA section
/// then ends at another `]]>` than in the bytes), so each of those readings is scanned too.
fn markup_views(bytes: &[u8]) -> Vec<std::borrow::Cow<'_, [u8]>> {
    use std::borrow::Cow;
    let decode = |e: &'static encoding_rs::Encoding| Cow::Owned(e.decode_without_bom_handling(bytes).0.into_owned().into_bytes());
    if (bytes.starts_with(&[0xFF, 0xFE]) && !bytes.starts_with(&[0xFF, 0xFE, 0, 0])) || bytes.starts_with(&[0xFE, 0xFF]) {
        return vec![markup_bytes(bytes)];
    }
    let mut v = vec![Cow::Borrowed(bytes)];
    if bytes.starts_with(&[b'<', 0]) || bytes.starts_with(&[0, b'<']) {
        v.push(markup_bytes(bytes));
    } else if std::str::from_utf8(bytes.strip_prefix(&[0xEF, 0xBB, 0xBF]).unwrap_or(bytes)).is_err() {
        for e in [encoding_rs::SHIFT_JIS, encoding_rs::GB18030, encoding_rs::BIG5] {
            v.push(decode(e));
        }
    }
    v
}

fn check_xml_with(bytes: &[u8], l: &Limits, keep_dtd: bool, internal_ok: bool) -> Result<(), Error> {
    for view in markup_views(bytes) {
        scan_xml(&view, l, keep_dtd, internal_ok)?;
    }
    Ok(())
}

fn scan_xml(b: &[u8], l: &Limits, keep_dtd: bool, internal_ok: bool) -> Result<(), Error> {
    let strict = !keep_dtd;
    let (mut i, mut depth, mut elements) = (0usize, 0usize, 0usize);
    while i < b.len() {
        let Some(p) = b[i..].iter().position(|c| *c == b'<') else { break };
        i += p;
        let rest = &b[i..];
        if rest.starts_with(b"<!--") {
            // `<!-->` and `<!--->` open a comment for XML, but a scanner that looks for `-->` from the `<` reads them
            // as a whole comment and goes on with what XML still calls comment text
            if strict && (rest[4..].starts_with(b">") || rest[4..].starts_with(b"->")) {
                return Err(ambiguous_markup("a comment that starts with '>' or '->'"));
            }
            match find(b, i + 4, b"-->") {
                Some(e) => i = e + 3,
                None => break,
            }
        } else if rest.starts_with(b"<![CDATA[") {
            match find(b, i + 9, b"]]>") {
                Some(e) => i = e + 3,
                None => break,
            }
        } else if rest.starts_with(b"<?") {
            // `<?>` is no processing instruction; a scanner that looks for `?>` from the `<` reads it as one
            if strict && rest[2..].starts_with(b">") {
                return Err(ambiguous_markup("a processing instruction with no name ('<?>')"));
            }
            match find(b, i + 2, b"?>") {
                Some(e) => i = e + 2,
                None => break,
            }
        } else if rest.len() >= 9 && rest[..9].eq_ignore_ascii_case(b"<!DOCTYPE") {
            let end = doctype_end(b, i);
            if !keep_dtd && contains_ci(&b[i..end], b"<!ENTITY") && !(internal_ok && only_plain_internal_entities(&b[i..end])) {
                return Err(unsafe_xml(
                    "This file declares XML entities (a DTD), which could expand without limit or read other files. It was not processed.",
                ));
            }
            // the readers must all end the DOCTYPE where this scan does, or elements (or an ENTITY) could hide in
            // what one of them calls the DOCTYPE. The keep-DTD readers (the XML filter family) check that themselves.
            if strict && end >= b.len() {
                return Err(unsafe_xml("This file has a DOCTYPE that is not closed. It was not processed."));
            }
            if strict {
                match doctype_is_unambiguous(b, i, end) {
                    None => {
                        return Err(unsafe_xml(
                            "This file has a DOCTYPE with a quoted '<', '>' or bracket, or a comment in its DTD with brackets, which XML readers do not agree on. It was not processed.",
                        ))
                    }
                    // a scanner that pairs the comment's quote with a later one would still be in the subset
                    // after the DOCTYPE: no `<!ENTITY` may follow anywhere, even in an attribute, comment or CDATA
                    Some(true) if contains_ci(&b[end..], b"<!ENTITY") => {
                        return Err(unsafe_xml(
                            "This file declares XML entities, which are not allowed. It was not processed.",
                        ))
                    }
                    Some(_) => {}
                }
            }
            i = end + 1;
        } else if rest.starts_with(b"<!") {
            // another declaration outside a DOCTYPE
            if rest.len() >= 8 && rest[..8].eq_ignore_ascii_case(b"<!ENTITY") {
                return Err(unsafe_xml("This file declares XML entities, which are not allowed. It was not processed."));
            }
            // `quick-xml` reads `<![` to `]]>` and `<!-` to `-->`; XML has no other declaration outside a DOCTYPE
            if strict {
                return Err(ambiguous_markup("a markup declaration outside the DOCTYPE"));
            }
            match find(b, i + 2, b">") {
                Some(e) => i = e + 1,
                None => break,
            }
        } else if rest.starts_with(b"</") {
            depth = depth.saturating_sub(1);
            match find(b, i + 2, b">") {
                Some(e) => i = e + 1,
                None => break,
            }
        } else {
            // a start tag: find '>' outside quotes
            let (mut j, mut sq) = (i + 1, 0u8);
            let mut end = None;
            while j < b.len() {
                let c = b[j];
                if sq != 0 {
                    if c == sq {
                        sq = 0;
                    }
                } else if c == b'"' || c == b'\'' {
                    sq = c;
                } else if c == b'>' {
                    end = Some(j);
                    break;
                }
                j += 1;
            }
            let Some(e) = end else { break };
            elements += 1;
            if elements > l.max_xml_elements {
                return Err(LimitError::new(
                    LimitKind::XmlElements,
                    format!("This file has more than {} XML elements in one part. It was not processed.", l.max_xml_elements),
                )
                .into());
            }
            if b[e - 1] != b'/' {
                depth += 1;
                if depth > l.max_xml_depth {
                    return Err(LimitError::new(
                        LimitKind::XmlDepth,
                        format!("This file nests XML elements more than {} deep. It was not processed.", l.max_xml_depth),
                    )
                    .into());
                }
            }
            i = e + 1;
        }
    }
    Ok(())
}

/// Counts the start tags whose local name is `local` (`p` for a Word paragraph), comments, CDATA and processing
/// instructions skipped. Cheap enough to run over every part before a parser builds a tree of it.
pub fn count_elements(bytes: &[u8], local: &str) -> usize {
    let data = markup_bytes(bytes);
    let b: &[u8] = &data;
    let (mut i, mut n) = (0usize, 0usize);
    while let Some(p) = b.get(i..).and_then(|r| r.iter().position(|c| *c == b'<')) {
        i += p;
        let rest = &b[i + 1..];
        if rest.starts_with(b"!--") {
            i = find(b, i + 4, b"-->").map_or(b.len(), |e| e + 3);
        } else if rest.starts_with(b"![CDATA[") {
            i = find(b, i + 9, b"]]>").map_or(b.len(), |e| e + 3);
        } else if rest.starts_with(b"?") {
            i = find(b, i + 2, b"?>").map_or(b.len(), |e| e + 2);
        } else {
            let name_end = rest.iter().position(|c| c.is_ascii_whitespace() || *c == b'>' || *c == b'/').unwrap_or(rest.len());
            let name = &rest[..name_end];
            let lname = name.rsplit(|c| *c == b':').next().unwrap_or(name);
            if !name.starts_with(b"/") && !name.starts_with(b"!") && lname == local.as_bytes() {
                n += 1;
            }
            i += 1 + name_end;
        }
    }
    n
}

/// [`count_elements`] fed in pieces, as a part inflates out of a zip (BILI-115): after the last piece and
/// [`ElementCounter::finish`], [`ElementCounter::count`] is exactly what `count_elements` gives for the whole part,
/// and after any piece it is at most that (a start tag is counted only once its name has ended, a region is skipped
/// only once its opening is known), so a running total over several parts can refuse a file before the rest of it
/// is read without refusing one the whole-part count accepts. Memory is a few bytes whatever the input.
///
/// A part that starts as UTF-16 (`count_elements` transcodes those) is not counted: [`ElementCounter::exact`] is
/// then false, `count` stays 0 (below the real count, so still safe for an early refusal) and the caller counts
/// the whole part with `count_elements` instead.
#[derive(Debug, Clone)]
pub struct ElementCounter {
    local: Vec<u8>,
    state: CountState,
    count: usize,
    /// The first byte, held until the second shows whether the part is UTF-16.
    first: Option<u8>,
    started: bool,
    utf16: bool,
}

#[derive(Debug, Clone, Copy)]
enum CountState {
    /// Character data: looking for the next `<`.
    Text,
    /// Just after a `<`.
    Open,
    /// After `<!`: `n` bytes (from the `!`) that are still a prefix of `!--` (`comment`) or of `![CDATA[` (`cdata`).
    Bang { n: usize, comment: bool, cdata: bool },
    /// In a tag name: `pos` bytes since its last `:`, `ok` while those equal the start of the local name; `never`
    /// for a name that starts with `!` (not an element).
    Name { pos: usize, ok: bool, never: bool },
    /// In a comment, CDATA section or processing instruction: `k` bytes of its terminator matched.
    Skip { end: &'static [u8], k: usize },
}

const CDATA_OPEN: &[u8] = b"![CDATA[";

/// The bytes of `end` matched after one more byte `c`, when `k` were matched before (the longest prefix of `end`
/// that ends the text read so far; the terminators are at most three bytes long).
fn skip_step(end: &[u8], k: usize, c: u8) -> usize {
    let mut j = (k + 1).min(end.len());
    while j > 0 {
        if end[j - 1] == c && end[..j - 1] == end[k + 1 - j..k] {
            return j;
        }
        j -= 1;
    }
    0
}

/// A byte that ends a tag name for `count_elements`.
fn ends_name(c: u8) -> bool {
    c.is_ascii_whitespace() || c == b'>' || c == b'/'
}

impl ElementCounter {
    /// A counter of the start tags whose local name is `local`.
    pub fn new(local: &str) -> ElementCounter {
        ElementCounter { local: local.as_bytes().to_vec(), state: CountState::Text, count: 0, first: None, started: false, utf16: false }
    }

    /// The elements counted so far.
    pub fn count(&self) -> usize {
        self.count
    }

    /// False when the part is UTF-16, which this counter leaves to [`count_elements`].
    pub fn exact(&self) -> bool {
        !self.utf16
    }

    /// The next bytes of the part.
    pub fn feed(&mut self, bytes: &[u8]) {
        if bytes.is_empty() || self.utf16 {
            return;
        }
        if !self.started {
            // `markup_bytes` decides on the first two bytes
            let held = self.first;
            let Some(f) = held.or(bytes.first().copied()) else { return };
            let second = if held.is_some() { bytes.first() } else { bytes.get(1) };
            let Some(&s) = second else {
                self.first = Some(f);
                return;
            };
            self.started = true;
            if matches!((f, s), (0xFF, 0xFE) | (b'<', 0) | (0xFE, 0xFF) | (0, b'<')) {
                self.utf16 = true;
                self.first = None;
                return;
            }
            if let Some(f) = self.first.take() {
                self.run(&[f]);
            }
        }
        self.run(bytes);
    }

    /// The end of the part: a tag name still open at the end is counted as `count_elements` counts it.
    pub fn finish(&mut self) {
        if let Some(f) = self.first.take() {
            self.started = true;
            self.run(&[f]);
        }
        if let CountState::Name { pos, ok, never } = self.state {
            if !never && ok && pos == self.local.len() {
                self.count += 1;
            }
        }
        self.state = CountState::Text;
    }

    fn run(&mut self, b: &[u8]) {
        let mut i = 0;
        while i < b.len() {
            match self.state {
                CountState::Text => match b[i..].iter().position(|c| *c == b'<') {
                    Some(p) => {
                        i += p + 1;
                        self.state = CountState::Open;
                    }
                    None => return,
                },
                CountState::Open => match b[i] {
                    b'!' => {
                        self.state = CountState::Bang { n: 1, comment: true, cdata: true };
                        i += 1;
                    }
                    b'?' => {
                        self.state = CountState::Skip { end: b"?>", k: 0 };
                        i += 1;
                    }
                    // the byte is the name's first (or ends an empty name): the name state reads it
                    _ => self.state = CountState::Name { pos: 0, ok: true, never: false },
                },
                CountState::Bang { n, comment, cdata } => {
                    let c = b[i];
                    let n = n + 1;
                    // the whole prefix must still match: `<![-` is no comment
                    let comment = comment && n <= 3 && b"!--"[n - 1] == c;
                    let cdata = cdata && n <= CDATA_OPEN.len() && CDATA_OPEN[n - 1] == c;
                    i += 1;
                    self.state = if comment && n == 3 {
                        CountState::Skip { end: b"-->", k: 0 }
                    } else if cdata && n == CDATA_OPEN.len() {
                        CountState::Skip { end: b"]]>", k: 0 }
                    } else if comment || cdata {
                        CountState::Bang { n, comment, cdata }
                    } else if ends_name(c) {
                        // a name such as `!x` ended here: never an element (the byte is not a `<`, so the text
                        // scan loses nothing by not seeing it)
                        CountState::Text
                    } else {
                        CountState::Name { pos: 0, ok: false, never: true }
                    };
                }
                CountState::Name { mut pos, mut ok, never } => {
                    while i < b.len() && !ends_name(b[i]) {
                        let c = b[i];
                        if c == b':' {
                            pos = 0;
                            ok = true;
                        } else {
                            ok = ok && self.local.get(pos) == Some(&c);
                            pos += 1;
                        }
                        i += 1;
                    }
                    if i == b.len() {
                        self.state = CountState::Name { pos, ok, never };
                        return;
                    }
                    // the byte that ends the name is left to the text scan, where `count_elements` resumes
                    if !never && ok && pos == self.local.len() {
                        self.count += 1;
                    }
                    self.state = CountState::Text;
                }
                CountState::Skip { end, mut k } => {
                    while i < b.len() && k < end.len() {
                        k = skip_step(end, k, b[i]);
                        i += 1;
                    }
                    self.state = if k == end.len() { CountState::Text } else { CountState::Skip { end, k } };
                }
            }
        }
    }
}

/// The guard for markup that is not strict XML (HTML): no entity declarations, and the element count cap. Depth
/// of HTML is the parser's own business (unclosed tags make it unknowable from the text).
pub fn check_markup(text: &str, l: &Limits) -> Result<(), Error> {
    let b = text.as_bytes();
    if contains_ci(b, b"<!ENTITY") {
        return Err(unsafe_xml("This file declares markup entities, which are not allowed. It was not processed."));
    }
    let elements = b.windows(2).filter(|w| w[0] == b'<' && (w[1].is_ascii_alphabetic() || w[1] == b'/')).count();
    if elements > l.max_xml_elements {
        return Err(LimitError::new(
            LimitKind::XmlElements,
            format!("This file has more than {} markup elements. It was not processed.", l.max_xml_elements),
        )
        .into());
    }
    Ok(())
}

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

    fn kind(r: Result<(), Error>) -> Option<LimitKind> {
        match r {
            Err(Error::Limit(e)) => Some(e.kind),
            _ => None,
        }
    }

    #[test]
    fn defaults_are_locos_numbers() {
        let l = Limits::default();
        assert_eq!((l.max_input_bytes, l.max_units, l.max_unit_chars), (100 << 20, 50_000, 100_000));
        assert_eq!((l.max_zip_entries, l.max_zip_bytes, l.max_zip_ratio), (10_000, 256 << 20, 200));
    }

    #[test]
    fn pre_parse_budget_scales_with_the_unit_limit() {
        let l = Limits { max_units: 10, ..Limits::default() };
        assert!(check_budget(200, 20, "This JSON file has far more values", &l).is_ok());
        let e = check_budget(201, 20, "This JSON file has far more values", &l).unwrap_err();
        assert!(e.to_string().contains("far more values than a file of 10 segments. Split"), "{e}");
        assert_eq!(kind(Err(e)), Some(LimitKind::Units));
        assert!(check_budget(usize::MAX, usize::MAX, "x", &Limits { max_units: usize::MAX, ..Limits::default() }).is_ok());
    }

    #[test]
    fn input_size_is_checked_against_the_callers_limit() {
        let l = Limits { max_input_bytes: 10, ..Limits::default() };
        assert!(check_input(&[0; 10], &l).is_ok());
        assert_eq!(kind(check_input(&[0; 11], &l)), Some(LimitKind::InputBytes));
    }

    #[test]
    fn xml_entity_bomb_and_xxe_are_refused() {
        let bomb = br#"<?xml version="1.0"?><!DOCTYPE lolz [<!ENTITY lol "lol"><!ENTITY lol2 "&lol;&lol;&lol;">]><lolz>&lol2;</lolz>"#;
        assert_eq!(kind(check_xml(bomb, &Limits::default())), Some(LimitKind::XmlEntities));
        let xxe = br#"<!DOCTYPE r [ <!ENTITY x SYSTEM "file:///etc/passwd"> ]><r>&x;</r>"#;
        assert_eq!(kind(check_xml(xxe, &Limits::default())), Some(LimitKind::XmlEntities));
        let param = br#"<!doctype r [ <!entity % p SYSTEM "http://example.invalid/x.dtd"> %p; ]><r/>"#;
        assert_eq!(kind(check_xml(param, &Limits::default())), Some(LimitKind::XmlEntities));
    }

    #[test]
    fn internal_entities_mode_allows_only_plain_literals() {
        let l = Limits::default();
        let ok = br#"<?xml version="1.0"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [ <!ENTITY ns_a "http://ns.example.invalid/a/1.0/"> <!ENTITY b 'x'> ]><svg xmlns:a="&ns_a;"/>"#;
        assert!(check_xml_internal_entities(ok, &l).is_ok());
        assert_eq!(kind(check_xml(ok, &l)), Some(LimitKind::XmlEntities), "the strict check still refuses it");
        for bad in [
            r#"<!DOCTYPE r [<!ENTITY lol "lol"><!ENTITY lol2 "&lol;&lol;">]><r/>"#,
            r#"<!DOCTYPE r [<!ENTITY x SYSTEM "file:///etc/hosts">]><r/>"#,
            r#"<!DOCTYPE r [<!ENTITY x PUBLIC "a" "b">]><r/>"#,
            r#"<!DOCTYPE r [<!ENTITY % p "x">]><r/>"#,
            r#"<!DOCTYPE r [<!ENTITY x>]><r/>"#,
            r#"<!DOCTYPE r [<!ENTITY x "unclosed>]><r/>"#,
            r#"<!ENTITY x "y"><r/>"#,
        ] {
            assert_eq!(kind(check_xml_internal_entities(bad.as_bytes(), &l)), Some(LimitKind::XmlEntities), "{bad}");
        }
        let long = format!("<!DOCTYPE r [<!ENTITY x \"{}\">]><r/>", "a".repeat(1025));
        assert_eq!(kind(check_xml_internal_entities(long.as_bytes(), &l)), Some(LimitKind::XmlEntities));
        // a literal never carries markup: a quoted '<' or '>' in the DOCTYPE is refused
        for bad in [r#"<!DOCTYPE r [<!ENTITY x "<g/>">]><r/>"#, r#"<!DOCTYPE r [<!ENTITY x "a>b">]><r/>"#] {
            assert!(check_xml_internal_entities(bad.as_bytes(), &l).is_err(), "{bad}");
        }
        // declared entities do not lift the depth and element limits
        let tight = Limits { max_xml_depth: 2, max_xml_elements: 3, ..Limits::default() };
        let deep = r#"<!DOCTYPE r [<!ENTITY x "y">]><r><a><b>&x;</b></a></r>"#;
        assert!(check_xml_internal_entities(deep.as_bytes(), &tight).is_err());
        let many: String = (0..65).map(|k| format!("<!ENTITY e{k} \"v\">")).collect();
        assert!(check_xml_internal_entities(format!("<!DOCTYPE r [{many}]><r/>").as_bytes(), &l).is_err());
    }

    #[test]
    fn keeping_dtd_allows_declarations_but_not_more_elements_or_depth() {
        let l = Limits { max_xml_depth: 8, max_xml_elements: 20, ..Limits::default() };
        let bomb = br#"<!DOCTYPE lolz [<!ENTITY lol "lol"><!ENTITY lol2 "&lol;&lol;&lol;"><!ENTITY % p SYSTEM "http://example.invalid/x.dtd"> %p;]><lolz>&lol2;</lolz>"#;
        assert!(check_xml_keeping_dtd(bomb, &l).is_ok());
        assert_eq!(kind(check_xml(bomb, &l)), Some(LimitKind::XmlEntities), "the strict check still refuses it");
        // a quote, a '>' or a ']' in a comment or a PI of the subset does not end the DOCTYPE early or late
        for dt in [r#"<!DOCTYPE r [ <!-- ' ]> --> <!ENTITY a "x"> ]>"#, r#"<!DOCTYPE r [ <?pi ]> " ?> <!ENTITY a "]>"> ]>"#] {
            let deep = format!("{dt}{}{}", "<a>".repeat(9), "</a>".repeat(9));
            assert_eq!(kind(check_xml_keeping_dtd(deep.as_bytes(), &l)), Some(LimitKind::XmlDepth), "{dt}");
            let wide = format!("{dt}<r>{}</r>", "<b/>".repeat(30));
            assert_eq!(kind(check_xml_keeping_dtd(wide.as_bytes(), &l)), Some(LimitKind::XmlElements), "{dt}");
            assert_eq!(kind(check_xml(deep.as_bytes(), &l)), Some(LimitKind::XmlEntities), "{dt}");
        }
        // an ENTITY declaration outside a DOCTYPE is refused in both modes
        assert_eq!(kind(check_xml_keeping_dtd(br#"<!ENTITY a "x"><r/>"#, &l)), Some(LimitKind::XmlEntities));
    }

    #[test]
    fn a_doctype_the_readers_end_elsewhere_is_refused() {
        let l = Limits { max_xml_depth: 2, max_xml_elements: 3, ..Limits::default() };
        let body = "<r><a><b><c/></b></a></r>";
        assert_eq!(kind(check_xml(body.as_bytes(), &l)), Some(LimitKind::XmlDepth));
        for pre in [
            "<!doctype r SYSTEM '>",
            "<!DOCTYPE r SYSTEM \">",
            "<!DoCtYpE r PUBLIC 'a>b' 'c'>",
            "<!DOCTYPE r [>",
            "<!DOCTYPE r [<!-- > -->",
            "<!DOCTYPE r [<!-- ] --> <x><y>",
            "<!DOCTYPE r [<!-- ' --> <?p ' ?> ]>",
            "<!DOCTYPE r SYSTEM 'a[b'>",
            "<!DOCTYPE r < >",
            "<!DOCTYPE r [ > ]>",
            "<!DOCTYPE r",
        ] {
            let k = kind(check_xml(format!("{pre}{body}").as_bytes(), &l));
            assert!(matches!(k, Some(LimitKind::XmlEntities | LimitKind::XmlDepth)), "{pre}: {k:?}");
            assert!(kind(check_xml(format!("{pre}{body}").as_bytes(), &Limits::default())).is_some(), "{pre} refused even with room");
        }
        // what real files carry still passes
        for ok in [
            "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">",
            "<!doctype html>",
            "<!DOCTYPE r []>",
            "<!DOCTYPE mb [ <!ELEMENT mb (msg)*> <!ATTLIST msg id CDATA #IMPLIED d CDATA 'x'> <!-- a note --> <?pi data?> ]>",
            "<!DOCTYPE r [ <!---->]>",
        ] {
            assert!(check_xml(format!("{ok}<r/>").as_bytes(), &Limits::default()).is_ok(), "{ok}");
        }
        // the keep-DTD check leaves this to its reader (the XML filter family refuses a mismatch itself)
        assert!(check_xml_keeping_dtd(b"<!DOCTYPE r [<!-- > --> ]><r/>", &Limits::default()).is_ok());
    }

    #[test]
    fn a_quote_in_a_comment_of_the_subset_passes_but_hides_no_entity() {
        // BILI-60: every reader skips a comment of the subset whole (quick-xml, the guard, the XML family), so an
        // apostrophe in it changes no reading
        let l = Limits::default();
        for ok in [
            "<!DOCTYPE r [ <!-- don't edit this file --> <!ELEMENT r ANY> ]><r/>",
            "<!DOCTYPE r [ <!-- the \"r\" element --> <!ATTLIST r id CDATA #IMPLIED> <!-- it's 'r' --> ]><r id='a'>x</r>",
            "<!DOCTYPE r [<!--'-->]><r/>",
        ] {
            assert!(check_xml(ok.as_bytes(), &l).is_ok(), "{ok}");
            assert!(check_xml_internal_entities(ok.as_bytes(), &l).is_ok(), "{ok}");
        }
        let svg = "<!DOCTYPE svg [ <!-- Adobe's namespaces --> <!ENTITY ns_svg \"http://www.w3.org/2000/svg\"> ]><svg/>";
        assert!(check_xml_internal_entities(svg.as_bytes(), &l).is_ok());
        assert_eq!(kind(check_xml(svg.as_bytes(), &l)), Some(LimitKind::XmlEntities));
        // an entity declaration next to, inside or after such a comment is still refused
        for bad in [
            "<!DOCTYPE r [ <!-- don't --> <!ENTITY a \"x\"> ]><r>&a;</r>",
            "<!DOCTYPE r [ <!ENTITY a \"x\"> <!-- don't --> ]><r>&a;</r>",
            "<!DOCTYPE r [ <!-- ' <!ENTITY a SYSTEM \"file:///etc/passwd\"> ' --> ]><r/>",
            "<!DOCTYPE r [ <!-- ' --><!ENTITY % p SYSTEM \"http://example.invalid/x.dtd\"> %p; ]><r/>",
            "<!DOCTYPE r [ <!-- ' --> ]><!ENTITY a \"x\"><r/>",
        ] {
            assert_eq!(kind(check_xml(bad.as_bytes(), &l)), Some(LimitKind::XmlEntities), "{bad}");
        }
        for bad in [
            "<!DOCTYPE svg [ <!-- ' --> <!ENTITY a \"&b;&b;\"> ]><svg/>",
            "<!DOCTYPE svg [ <!-- ' <!ENTITY a SYSTEM \"file:///etc/passwd\"> --> ]><svg/>",
            "<!DOCTYPE svg [ <!-- ' --> <!ENTITY % p \"x\"> ]><svg/>",
        ] {
            assert_eq!(kind(check_xml_internal_entities(bad.as_bytes(), &l)), Some(LimitKind::XmlEntities), "{bad}");
        }
        // a reader that pairs quotes but does not skip comments would read the subset on to the next quote: an
        // ENTITY anywhere after such a DOCTYPE (an attribute, a comment, CDATA) could be a declaration to it
        for bad in [
            "<!DOCTYPE r [ <!-- ' --> ]><r a='<!ENTITY b \"x\">'/>",
            "<!DOCTYPE r [ <!-- \" --> ]><r><!-- <!entity b 'x'> --></r>",
            "<!DOCTYPE r [ <!-- ' --> ]><r><![CDATA[<!ENTITY b 'x'>]]></r>",
        ] {
            assert_eq!(kind(check_xml(bad.as_bytes(), &l)), Some(LimitKind::XmlEntities), "{bad}");
            assert_eq!(kind(check_xml_internal_entities(bad.as_bytes(), &l)), Some(LimitKind::XmlEntities), "{bad}");
        }
        // a bracket or an angle bracket in the comment, or a quote in a processing instruction, is still refused
        for bad in [
            "<!DOCTYPE r [ <!-- ' ] --> ]><r/>",
            "<!DOCTYPE r [ <!-- \" > --> ]><r/>",
            "<!DOCTYPE r [ <!-- ' < --> ]><r/>",
            "<!DOCTYPE r [ <!-- ' [ --> ]><r/>",
            "<!DOCTYPE r [ <!-- ok --> <?pi don't ?> ]><r/>",
            "<!DOCTYPE r [ <!-- ' --> ",
        ] {
            assert_eq!(kind(check_xml(bad.as_bytes(), &l)), Some(LimitKind::XmlEntities), "{bad}");
        }
        // and the elements after it are counted as before
        let tight = Limits { max_xml_depth: 2, max_xml_elements: 3, ..Limits::default() };
        let deep = "<!DOCTYPE r [ <!-- don't --> ]><r><a><b><c/></b></a></r>";
        assert_eq!(kind(check_xml(deep.as_bytes(), &tight)), Some(LimitKind::XmlDepth));
        let wide = "<!DOCTYPE r [ <!-- don't --> ]><r><b/><b/><b/><b/></r>";
        assert_eq!(kind(check_xml(wide.as_bytes(), &tight)), Some(LimitKind::XmlElements));
    }

    #[test]
    fn every_reading_of_the_bytes_is_scanned() {
        let l = Limits { max_xml_depth: 2, max_xml_elements: 3, ..Limits::default() };
        let body = "<a><b><c/></b></a>";
        // 0x81 0x5D is one Shift_JIS / GBK character: the bytes end the CDATA at `]]>`, the decoded text later
        let sjis = format!("<?xml version=\"1.0\" encoding=\"Shift_JIS\"?><r><![CDATA[\u{1}]]><!--]]>{body}--></r>");
        let mut bytes = sjis.into_bytes();
        let p = bytes.iter().position(|c| *c == 1).unwrap();
        bytes[p] = 0x81;
        assert!(check_xml(&bytes, &l).is_err());
        // a valid double-byte file still passes
        let ok = b"<?xml version=\"1.0\" encoding=\"Shift_JIS\"?><r><![CDATA[\x93\xfa]]>\x96\x7b</r>";
        assert!(check_xml(ok, &Limits::default()).is_ok());
        // a NUL first: UTF-16BE for one reader, UTF-8 for another
        let nul = format!("\0<r>{body}</r>");
        assert!(check_xml(nul.as_bytes(), &l).is_err());
        let mut le = Vec::new();
        for u in format!("<r>{body}</r>").encode_utf16() {
            le.extend(u.to_le_bytes());
        }
        assert!(check_xml(&le, &l).is_err());
        assert!(check_xml(&le, &Limits::default()).is_ok());
    }

    #[test]
    fn a_comment_or_pi_a_scanner_ends_early_is_refused() {
        let l = Limits::default();
        for bad in ["<!--><r><a/></r><!-- -->", "<!---><r/><!-- -->", "<?><r/><?x?>", "<r><![IGNORE[ <a/> ]]></r>", "<!ELEMENT r ANY><r/>"] {
            assert_eq!(kind(check_xml(bad.as_bytes(), &l)), Some(LimitKind::XmlElements), "{bad}");
        }
        for ok in ["<r><!----></r>", "<r><!-- - --></r>", "<r><??></r>", "<r><?pi x?></r>"] {
            assert!(check_xml(ok.as_bytes(), &l).is_ok(), "{ok}");
        }
    }

    #[test]
    fn a_plain_doctype_and_predefined_entities_pass() {
        let ok = br#"<?xml version="1.0"?><!DOCTYPE xliff PUBLIC "-//OASIS//DTD XLIFF 1.2//EN" "xliff.dtd"><a>x &amp; y &#233;</a>"#;
        assert!(check_xml(ok, &Limits::default()).is_ok());
        // an ENTITY inside a comment or CDATA is only text
        assert!(check_xml(b"<a><!-- <!ENTITY x 'y'> --><![CDATA[<!ENTITY z 'w'>]]></a>", &Limits::default()).is_ok());
    }

    #[test]
    fn xml_depth_and_element_count_are_bounded() {
        let l = Limits { max_xml_depth: 8, max_xml_elements: 20, ..Limits::default() };
        let deep = format!("{}{}", "<a>".repeat(9), "</a>".repeat(9));
        assert_eq!(kind(check_xml(deep.as_bytes(), &l)), Some(LimitKind::XmlDepth));
        let ok = format!("{}{}", "<a>".repeat(8), "</a>".repeat(8));
        assert!(check_xml(ok.as_bytes(), &l).is_ok());
        let wide = format!("<r>{}</r>", "<b/>".repeat(30));
        assert_eq!(kind(check_xml(wide.as_bytes(), &l)), Some(LimitKind::XmlElements));
    }

    #[test]
    fn utf16_xml_is_scanned_too() {
        let src = "<!DOCTYPE r [<!ENTITY a 'b'>]><r/>";
        let mut v = vec![0xFF, 0xFE];
        v.extend(src.encode_utf16().flat_map(|u| u.to_le_bytes()));
        assert_eq!(kind(check_xml(&v, &Limits::default())), Some(LimitKind::XmlEntities));
    }

    #[test]
    fn counts_elements_by_local_name() {
        let x = b"<w:body><w:p/><w:p w:x='1'>a</w:p><!-- <w:p/> --><w:pPr/><p>x</p></w:body>";
        assert_eq!(count_elements(x, "p"), 3);
    }

    /// BILI-115: the streaming counter gives `count_elements`'s number for the whole part whatever the pieces it is
    /// fed in, and never more than that number after any piece.
    #[test]
    fn element_counter_agrees_with_count_elements() {
        let mut seed: u64 = 0x5eed_0115;
        let mut next = move |n: usize| {
            seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
            ((seed >> 33) as usize) % n
        };
        let pieces: &[&[u8]] = &[
            b"<", b">", b"/", b"!", b"-", b"--", b"[", b"]", b"]]", b"?", b":", b" ", b"\n", b"p", b"w:p", b"<w:p>", b"<w:p/>",
            b"<w:pPr>", b"</w:p>", b"<!--", b"-->", b"<![CDATA[", b"]]>", b"<?", b"?>", b"<!", b"<!-", b"<![CD", b"<![-", b"<!-[", b"a:p", b"pp",
            b"<p", b":p", b"x", b"<!DOCTYPE x>", b"\"", b"'",
        ];
        for round in 0..3000 {
            let mut doc = Vec::new();
            for _ in 0..next(60) {
                doc.extend_from_slice(pieces[next(pieces.len())]);
            }
            for local in ["p", "pPr"] {
                let want = count_elements(&doc, local);
                // whole, one byte at a time, and random pieces
                for mode in 0..3 {
                    let mut c = ElementCounter::new(local);
                    let mut at = 0;
                    while at < doc.len() {
                        let len = match mode {
                            0 => doc.len(),
                            1 => 1,
                            _ => 1 + next(7),
                        };
                        let end = (at + len).min(doc.len());
                        c.feed(&doc[at..end]);
                        at = end;
                        assert!(c.count() <= want, "round {round}: {} over {want} after a piece of {:?}", c.count(), String::from_utf8_lossy(&doc));
                    }
                    c.finish();
                    assert!(c.exact());
                    assert_eq!(c.count(), want, "round {round} mode {mode}: {:?}", String::from_utf8_lossy(&doc));
                }
            }
        }
        let x = b"<w:body><w:p/><w:p w:x='1'>a</w:p><!-- <w:p/> --><w:pPr/><p>x</p><![CDATA[<w:p>]]><?pi <w:p>?></w:body>";
        let mut c = ElementCounter::new("p");
        c.feed(x);
        c.finish();
        assert_eq!((c.count(), count_elements(x, "p")), (3, 3));
        // a UTF-16 part is left to `count_elements`
        let mut v = vec![0xFF, 0xFE];
        v.extend("<w:p/>".encode_utf16().flat_map(|u| u.to_le_bytes()));
        let mut c = ElementCounter::new("p");
        c.feed(&v[..1]);
        c.feed(&v[1..]);
        c.finish();
        assert!(!c.exact());
        assert_eq!(c.count(), 0);
        assert_eq!(count_elements(&v, "p"), 1);
    }

    #[test]
    fn own_caps_follow_a_raised_limit_only() {
        let h = Limits::new();
        // hosted and lowered: the smaller of the crate's own and the caller's, as before
        assert_eq!(own_cap(500_000, h.max_units, h.max_units), 50_000);
        assert_eq!(own_cap(20_000, h.max_units, h.max_units), 20_000);
        assert_eq!(own_cap(500_000, h.max_units, 10), 10);
        assert_eq!(own_cap_bytes(64 << 20, h.max_input_bytes, h.max_input_bytes), 64 << 20);
        assert_eq!(own_cap_bytes(256 << 20, h.max_input_bytes, h.max_input_bytes), 100 << 20);
        // raised: the caller's
        let l = Limits::large();
        assert_eq!(own_cap(500_000, h.max_units, l.max_units), l.max_units);
        assert_eq!(own_cap_bytes(64 << 20, h.max_input_bytes, l.max_input_bytes) as u64, l.max_input_bytes);
    }

    #[test]
    fn comment_base_refuses_a_body_that_reaches_it() {
        // BILI-126: body numbers 0..=999_999 leave the comment numbers free; one more and the first comment collides
        assert!(check_comment_base(0).is_ok());
        assert!(check_comment_base(COMMENT_BASE).is_ok());
        match check_comment_base(COMMENT_BASE + 1) {
            Err(Error::Limit(e)) => {
                assert_eq!(e.kind, LimitKind::Units);
                assert!(e.msg.contains("more than Bilingua can number apart"), "{e}");
                assert!(!e.msg.contains('\u{2014}'), "no em dash: {e}");
            }
            other => panic!("not a limit error: {other:?}"),
        }
        // the merge side: the same line, and one number on two units, each with the merge's own message
        assert!(check_comment_base_merge(COMMENT_BASE).is_ok());
        assert!(check_distinct_numbers([0, 1, 1_000_000]).is_ok());
        for r in [check_comment_base_merge(COMMENT_BASE + 1), check_distinct_numbers([5, 1_000_000, 5])] {
            match r {
                Err(Error::Limit(e)) => {
                    assert_eq!(e.kind, LimitKind::Units);
                    assert!(e.msg.contains("comments and body text share numbers"), "{e}");
                    assert!(!e.msg.contains('\u{2014}'), "no em dash: {e}");
                }
                other => panic!("not a limit error: {other:?}"),
            }
        }
    }

    #[test]
    fn line_budget() {
        let l = Limits { max_lines: 5, ..Limits::default() };
        assert!(check_lines("a\nb\nc\n", &l).is_ok());
        assert_eq!(kind(check_lines(&"\n".repeat(6), &l)), Some(LimitKind::Lines));
    }
}