vole-document 0.1.0-alpha.35

Persistent procedural document runtime: byte-exact reconstruction plus a content-addressed procedural seed DAG, queryable observations with provenance, and selective late materialization.
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
//! Bounded, representation-preserving XML adapter (Phase 21.9).
//!
//! XML is the next **structured-tree** Wave-2 format after JSON/YAML/CSV/Markdown.
//! Like them it is *not* a package: there is no OPC/ZIP layer, no `mimetype`, and
//! no relationship graph. The exact leaf is therefore the **whole source** (a
//! `DocumentExact`, a RAW-like authority), and everything this module produces is
//! a bounded, deterministic (`Q_gen`) projection that never sits on the exactness
//! path.
//!
//! ## Why a bespoke, span-preserving tree
//!
//! A conventional XML load (`ElementTree`, a DOM, etc.) keeps *values* and drops
//! the *representation*: it discards comments, processing instructions, CDATA
//! boundaries, namespace-declaration spelling, attribute quoting, and every
//! source offset, and it expands entity references. This adapter does the
//! opposite. For every construct it records the exact **byte span** in the source,
//! so it preserves and can report:
//!
//! * every **element** (its qualified name span, start-tag span, end-tag span, and
//!   full span), in document order;
//! * every **attribute** (its name span, quoted value span, inner value span, and
//!   full span), in source order;
//! * every **text** run, **CDATA** section, **comment**, **processing
//!   instruction**, and **DOCTYPE**;
//! * every **namespace declaration** (`xmlns`/`xmlns:prefix`) as an ordinary
//!   attribute with its exact span, so the in-scope bindings are recoverable;
//! * **entity references are never expanded** — the raw `&name;`/`&#…;` bytes
//!   remain in the reported text/content spans, surfaced literally;
//! * **no DTD internal subset** is ever processed, so neither external entities
//!   (XXE) nor entity expansion (billion-laughs) can occur. A benign `<!DOCTYPE…>`
//!   (bare or with a PUBLIC/SYSTEM identifier) is accepted and ignored, exactly as
//!   in the shared [bounded XML policy](crate::adapter::xml_policy); a declaration
//!   with an internal subset (`[…]`) is **declined typed** (the whole document is
//!   then not detected as XML and stays `Opaque`).
//!
//! ## Bounds
//!
//! The parser is a bounded single-pass scanner. It reuses the shared
//! [`crate::adapter::xml_policy::XmlState`] budget for events, element nodes,
//! nesting depth, and accumulated text bytes, and additionally charges the parser
//! against the source length by [`Limits::max_xml_document_bytes`]. Recursion is
//! capped by [`Limits::max_xml_depth`]; the model's node arena is additionally
//! bounded by [`MAX_MODEL_NODES`] at decode. Untrusted input can only ever yield a
//! typed decline, never a panic or an unbounded allocation. There is no `unwrap`,
//! `expect`, `panic!`, or indexing that is not span-checked.
//!
//! ## Detection
//!
//! [`detect`] is deliberately conservative: the source must begin with `<` (an XML
//! prolog `<?xml` or a root element / comment / DOCTYPE start) **and** the whole
//! source must parse as well-formed XML with exactly one root element, within every
//! cap. A `<-prefixed` byte string that is not well-formed XML (and plain prose)
//! stays [`DocumentFormat::Opaque`](crate::field::document_format::DocumentFormat::Opaque).
//!
//! ## Explicitly declined
//!
//! This adapter does **not** support: typed what it does not: DTD internal subsets
//! (entities), external entity resolution, character-encoding switching beyond
//! UTF-8, XInclude, XSLT, schema validation, XPath (beyond the simple
//! `name[N]` element-path selector this module implements), or XML canonicalization
//! (C14N). Each is either a typed decline or simply out of scope; none is guessed.

use crate::adapter::xml_policy::{XmlState, accept_doctype, doctype_declined, harden_xml};
use crate::error::{Error, Result};
use crate::limits::Limits;

/// Wire version of the canonical derived model.
pub const MODEL_VERSION: u8 = 1;
/// Hard cap on decoded model nodes (defends the decoder against a hostile blob).
pub const MAX_MODEL_NODES: u32 = 1 << 24;
/// Hard cap on decoded model attributes (defends the decoder against a hostile blob).
pub const MAX_MODEL_ATTRS: u32 = 1 << 26;

/// XML node kind: an element (`<name …>…</name>` or `<name …/>`).
pub const K_ELEMENT: u8 = 0;
/// XML node kind: a character-data run.
pub const K_TEXT: u8 = 1;
/// XML node kind: a CDATA section (`<![CDATA[…]]>`).
pub const K_CDATA: u8 = 2;
/// XML node kind: a comment (`<!--…-->`).
pub const K_COMMENT: u8 = 3;
/// XML node kind: a processing instruction or the XML declaration (`<?…?>`).
pub const K_PI: u8 = 4;
/// XML node kind: a document type declaration (`<!DOCTYPE…>`).
pub const K_DOCTYPE: u8 = 5;

/// Stable lower-case kind name for reports and JSON output.
pub const fn kind_name(kind: u8) -> &'static str {
    match kind {
        K_ELEMENT => "element",
        K_TEXT => "text",
        K_CDATA => "cdata",
        K_COMMENT => "comment",
        K_PI => "pi",
        K_DOCTYPE => "doctype",
        _ => "unknown",
    }
}

/// Whether `kind` is character data (text or CDATA).
pub const fn is_chardata(kind: u8) -> bool {
    matches!(kind, K_TEXT | K_CDATA)
}

/// One parsed XML node.
///
/// * For an **element**, `[start, end)` is the whole element (`<a…>…</a>` or the
///   self-closing `<a…/>`); `[name_start, name_end)` is the qualified name;
///   `open_end` is one past the start tag's `>`; `close_start` is the `<` of the
///   end tag (`0` for a self-closing element, where `end == open_end`); `children`
///   are the child node indices in document order; `attrs` index [`XAttr`]s.
/// * For **text/cdata/comment/pi/doctype**, `[start, end)` is the whole construct
///   (delimiters included) and `[name_start, name_end)` is the *content* span
///   (for a PI, the target name; the remainder is derivable from `name_end..end`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct XNode {
    /// One of the `K_*` tags.
    pub kind: u8,
    /// The construct's first source byte.
    pub start: u64,
    /// One past the construct's last source byte.
    pub end: u64,
    /// Element: qualified-name start; others: content start.
    pub name_start: u64,
    /// Element: qualified-name end; others: content end.
    pub name_end: u64,
    /// Element: one past the start tag's `>`; others: `0`.
    pub open_end: u64,
    /// Element: the `<` of the end tag (`0` when self-closing); others: `0`.
    pub close_start: u64,
    /// Element: whether it declares a namespace (`xmlns`/`xmlns:…`).
    pub ns_decl: bool,
    /// Child node indices in document order.
    pub children: Vec<u32>,
    /// Attribute indices into [`XmlModel::attrs`].
    pub attrs: Vec<u32>,
}

/// One parsed XML attribute (or namespace declaration).
///
/// `[name_start, name_end)` is the qualified attribute name.
/// `[value_start, value_end)` is the value *without* its quotes.
/// `[span_start, span_end)` is the whole attribute (`name="value"`), so the
/// original quoting is recoverable from the source.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct XAttr {
    /// The qualified attribute name start.
    pub name_start: u64,
    /// The qualified attribute name end.
    pub name_end: u64,
    /// The attribute value start (past the opening quote).
    pub value_start: u64,
    /// The attribute value end (at the closing quote).
    pub value_end: u64,
    /// The whole attribute span start.
    pub span_start: u64,
    /// The whole attribute span end (past the closing quote).
    pub span_end: u64,
    /// `0` = ordinary, `1` = `xmlns`, `2` = `xmlns:prefix`.
    pub is_ns: u8,
}

/// The canonical derived XML model (the materialization of an `XmlModel` node).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct XmlModel {
    /// Index of the root element node.
    pub root: u32,
    /// The observed maximum element nesting depth (root = 1).
    pub max_depth: u32,
    /// The exact source length the spans are relative to.
    pub doc_len: u64,
    /// The node arena, in document order.
    pub nodes: Vec<XNode>,
    /// The attribute arena, in document order.
    pub attrs: Vec<XAttr>,
}

impl XmlModel {
    /// The node at `index`, if present.
    pub fn node(&self, index: u32) -> Option<&XNode> {
        self.nodes.get(index as usize)
    }

    /// The attribute at `index`, if present.
    pub fn attr(&self, index: u32) -> Option<&XAttr> {
        self.attrs.get(index as usize)
    }

    /// Deterministically encode the model (little-endian, length-prefixed).
    pub fn encode(&self) -> Vec<u8> {
        let mut out = Vec::with_capacity(32 + self.nodes.len() * 72 + self.attrs.len() * 49);
        out.extend_from_slice(b"XMLM");
        out.push(MODEL_VERSION);
        out.extend_from_slice(&self.root.to_le_bytes());
        out.extend_from_slice(&self.max_depth.to_le_bytes());
        out.extend_from_slice(&self.doc_len.to_le_bytes());
        out.extend_from_slice(&(self.nodes.len() as u32).to_le_bytes());
        out.extend_from_slice(&(self.attrs.len() as u32).to_le_bytes());
        for n in &self.nodes {
            out.push(n.kind);
            out.push(u8::from(n.ns_decl));
            out.extend_from_slice(&n.start.to_le_bytes());
            out.extend_from_slice(&n.end.to_le_bytes());
            out.extend_from_slice(&n.name_start.to_le_bytes());
            out.extend_from_slice(&n.name_end.to_le_bytes());
            out.extend_from_slice(&n.open_end.to_le_bytes());
            out.extend_from_slice(&n.close_start.to_le_bytes());
            out.extend_from_slice(&(n.children.len() as u32).to_le_bytes());
            for c in &n.children {
                out.extend_from_slice(&c.to_le_bytes());
            }
            out.extend_from_slice(&(n.attrs.len() as u32).to_le_bytes());
            for a in &n.attrs {
                out.extend_from_slice(&a.to_le_bytes());
            }
        }
        for a in &self.attrs {
            out.push(a.is_ns);
            out.extend_from_slice(&a.name_start.to_le_bytes());
            out.extend_from_slice(&a.name_end.to_le_bytes());
            out.extend_from_slice(&a.value_start.to_le_bytes());
            out.extend_from_slice(&a.value_end.to_le_bytes());
            out.extend_from_slice(&a.span_start.to_le_bytes());
            out.extend_from_slice(&a.span_end.to_le_bytes());
        }
        out
    }

    /// Decode a model produced by [`Self::encode`]. Fail-closed on any corruption.
    pub fn decode(bytes: &[u8]) -> Result<XmlModel> {
        let mut r = BinReader::new(bytes);
        if r.bytes(4)? != b"XMLM" {
            return Err(corrupt("bad model magic"));
        }
        if r.u8()? != MODEL_VERSION {
            return Err(corrupt("unsupported model version"));
        }
        let root = r.u32()?;
        let max_depth = r.u32()?;
        let doc_len = r.u64()?;
        let node_count = r.u32()?;
        let attr_count = r.u32()?;
        if node_count > MAX_MODEL_NODES {
            return Err(corrupt("model node count is implausible"));
        }
        if attr_count > MAX_MODEL_ATTRS {
            return Err(corrupt("model attribute count is implausible"));
        }
        let mut nodes = Vec::with_capacity(node_count as usize);
        for _ in 0..node_count {
            let kind = r.u8()?;
            if kind > K_DOCTYPE {
                return Err(corrupt("unknown node kind"));
            }
            let ns_decl = match r.u8()? {
                0 => false,
                1 => true,
                _ => return Err(corrupt("invalid node flag")),
            };
            let start = r.u64()?;
            let end = r.u64()?;
            let name_start = r.u64()?;
            let name_end = r.u64()?;
            let open_end = r.u64()?;
            let close_start = r.u64()?;
            for (lo, hi) in [
                (start, end),
                (name_start, name_end),
                (0, open_end),
                (0, close_start),
            ] {
                if lo > hi || hi > doc_len {
                    return Err(corrupt("node span is outside the document"));
                }
            }
            let cn = r.u32()?;
            if cn as u64 > node_count as u64 {
                return Err(corrupt("node child count is implausible"));
            }
            let mut children = Vec::with_capacity(cn as usize);
            for _ in 0..cn {
                let c = r.u32()?;
                if c >= node_count {
                    return Err(corrupt("child index is out of range"));
                }
                children.push(c);
            }
            let an = r.u32()?;
            if an as u64 > attr_count as u64 {
                return Err(corrupt("node attribute count is implausible"));
            }
            let mut attrs = Vec::with_capacity(an as usize);
            for _ in 0..an {
                let a = r.u32()?;
                if a >= attr_count {
                    return Err(corrupt("attribute index is out of range"));
                }
                attrs.push(a);
            }
            nodes.push(XNode {
                kind,
                start,
                end,
                name_start,
                name_end,
                open_end,
                close_start,
                ns_decl,
                children,
                attrs,
            });
        }
        let mut attrs = Vec::with_capacity(attr_count as usize);
        for _ in 0..attr_count {
            let is_ns = r.u8()?;
            if is_ns > 2 {
                return Err(corrupt("invalid namespace flag"));
            }
            let name_start = r.u64()?;
            let name_end = r.u64()?;
            let value_start = r.u64()?;
            let value_end = r.u64()?;
            let span_start = r.u64()?;
            let span_end = r.u64()?;
            for (lo, hi) in [
                (name_start, name_end),
                (value_start, value_end),
                (span_start, span_end),
            ] {
                if lo > hi || hi > doc_len {
                    return Err(corrupt("attribute span is outside the document"));
                }
            }
            attrs.push(XAttr {
                name_start,
                name_end,
                value_start,
                value_end,
                span_start,
                span_end,
                is_ns,
            });
        }
        if !r.at_end() {
            return Err(corrupt("model has trailing bytes"));
        }
        if root as usize >= nodes.len() {
            return Err(corrupt("root index is out of range"));
        }
        Ok(XmlModel {
            root,
            max_depth,
            doc_len,
            nodes,
            attrs,
        })
    }
}

/// A resolved element-path query: the node index and how many siblings matched
/// the final step's name (so an ambiguous name is reported, never hidden).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Resolved {
    /// The resolved element node index.
    pub index: u32,
    /// The number of child elements with the final step's name.
    pub matches: u32,
}

/// A resolved attribute query: the global attribute index and how many
/// attributes matched the name on the element.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ResolvedAttr {
    /// The resolved global attribute index.
    pub index: u32,
    /// The number of attributes with the requested name on the element.
    pub matches: u32,
}

/// One lexical-match record from [`find`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct XmlMatch {
    /// A canonical-ish element path to the containing node (e.g. `/a/b[2]`).
    pub path: String,
    /// What matched.
    pub role: MatchRole,
    /// The exact source span of the matching construct.
    pub start: u64,
    /// One past the matching construct.
    pub end: u64,
    /// The matched text (raw; entity references unexpanded).
    pub text: String,
}

/// What a lexical match is.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MatchRole {
    /// An element's qualified name.
    Element,
    /// An attribute's qualified name.
    AttrName,
    /// An attribute value.
    AttrValue,
    /// A character-data run.
    Text,
    /// A CDATA section's content.
    Cdata,
}

impl MatchRole {
    /// Stable lower-case name.
    pub const fn name(self) -> &'static str {
        match self {
            MatchRole::Element => "element",
            MatchRole::AttrName => "attr-name",
            MatchRole::AttrValue => "attr-value",
            MatchRole::Text => "text",
            MatchRole::Cdata => "cdata",
        }
    }
}

/// One namespace declaration found in the document.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NsDecl {
    /// The element that carries the declaration (model node index).
    pub element: u32,
    /// The prefix (empty string for a default `xmlns` declaration).
    pub prefix: String,
    /// The declared URI (raw; entity references unexpanded).
    pub uri: String,
    /// The exact span of the whole declaration attribute.
    pub start: u64,
    /// One past the declaration attribute.
    pub end: u64,
}

/// Byte-based, conservative XML detector. See the module docs.
pub fn detect(source: &[u8], limits: Limits) -> bool {
    if source.len() as u64 > limits.max_xml_document_bytes {
        return false;
    }
    // Must begin with `<` (prolog, root element, comment, or DOCTYPE start).
    if source.first() != Some(&b'<') {
        return false;
    }
    parse(source, limits, false).is_ok()
}

/// Parse `source` and return its canonical derived model, encoded (the
/// materialization of an `XmlModel` node).
pub fn build_xml_model(source: &[u8], limits: Limits) -> Result<Vec<u8>> {
    Ok(parse(source, limits, true)?.encode())
}

/// Parse `source` into an [`XmlModel`]. `build` selects whether the node/attribute
/// arenas are populated (detection runs with `build = false` to stay O(1) in extra
/// memory).
pub fn parse(source: &[u8], limits: Limits, build: bool) -> Result<XmlModel> {
    if source.len() as u64 > limits.max_xml_document_bytes {
        return Err(Error::resource_limit(format!(
            "XML source is {} bytes, above the {}-byte document cap",
            source.len(),
            limits.max_xml_document_bytes
        )));
    }
    harden_xml(source, limits)?;
    let mut p = Parser::new(source, limits, build);
    let root = p.parse_document()?;
    Ok(XmlModel {
        root,
        max_depth: p.max_depth_seen,
        doc_len: source.len() as u64,
        nodes: p.nodes,
        attrs: p.attrs,
    })
}

/// The exact source bytes of a node's whole span (`[start, end)`).
pub fn token_bytes<'a>(source: &'a [u8], node: &XNode) -> Result<&'a [u8]> {
    slice(source, node.start, node.end).ok_or_else(|| corrupt("node span is outside the source"))
}

/// The exact source bytes of a node's content span (`[name_start, name_end)`).
pub fn content_bytes<'a>(source: &'a [u8], node: &XNode) -> Result<&'a [u8]> {
    slice(source, node.name_start, node.name_end)
        .ok_or_else(|| corrupt("content span is outside the source"))
}

/// The exact source bytes of an attribute's quoted value (`[value_start, value_end)`).
pub fn attr_value_bytes<'a>(source: &'a [u8], attr: &XAttr) -> Result<&'a [u8]> {
    slice(source, attr.value_start, attr.value_end)
        .ok_or_else(|| corrupt("attribute value span is outside the source"))
}

/// An element's qualified name (raw source spelling, prefix intact).
pub fn element_name<'a>(source: &'a [u8], node: &XNode) -> Result<&'a str> {
    if node.kind != K_ELEMENT {
        return Err(corrupt("node is not an element"));
    }
    let bytes = slice(source, node.name_start, node.name_end)
        .ok_or_else(|| corrupt("element name span is outside the source"))?;
    core::str::from_utf8(bytes).map_err(|_| corrupt("element name is not valid UTF-8"))
}

/// An attribute's qualified name (raw source spelling).
pub fn attr_name<'a>(source: &'a [u8], attr: &XAttr) -> Result<&'a str> {
    let bytes = slice(source, attr.name_start, attr.name_end)
        .ok_or_else(|| corrupt("attribute name span is outside the source"))?;
    core::str::from_utf8(bytes).map_err(|_| corrupt("attribute name is not valid UTF-8"))
}

fn slice(source: &[u8], lo: u64, hi: u64) -> Option<&[u8]> {
    let s = usize::try_from(lo).ok()?;
    let e = usize::try_from(hi).ok()?;
    source.get(s..e)
}

/// Parse an element path (`/a/b[2]/c`; `""` is the root element).
fn parse_path(path: &str) -> Result<Vec<Step>> {
    if path.is_empty() {
        return Ok(Vec::new());
    }
    if !path.starts_with('/') {
        return Err(Error::usage(format!(
            "XML path {path:?} must be empty or start with '/'"
        )));
    }
    let mut out = Vec::new();
    for raw in path.split('/').skip(1) {
        if raw.is_empty() {
            return Err(Error::usage(format!("XML path {path:?} has an empty step")));
        }
        let (name, index) = match raw.split_once('[') {
            Some((name, rest)) => {
                let close = rest.strip_suffix(']').ok_or_else(|| {
                    Error::usage(format!("XML path step {raw:?} has an unclosed '['"))
                })?;
                let n: u32 = close
                    .parse()
                    .map_err(|_| Error::usage(format!("XML path step {raw:?} has a bad index")))?;
                if n == 0 {
                    return Err(Error::usage(format!(
                        "XML path step {raw:?} uses a 0 index (indices are 1-based)"
                    )));
                }
                (name, n)
            }
            None => (raw, 1),
        };
        if name.is_empty() {
            return Err(Error::usage(format!(
                "XML path step {raw:?} has an empty name"
            )));
        }
        out.push(Step {
            name: name.to_string(),
            index,
        });
    }
    Ok(out)
}

struct Step {
    name: String,
    index: u32,
}

/// Resolve an element path against a parsed model. A missing element or an
/// out-of-range positional index is a typed decline (never a silent empty answer).
pub fn resolve_path(model: &XmlModel, source: &[u8], path: &str) -> Result<Resolved> {
    let steps = parse_path(path)?;
    // The empty path is the root element. A non-empty path's first step must name
    // the root element itself (`/a/b` selects `b` inside a root named `a`), mirroring
    // how an absolute XML path is rooted at the document element.
    let root_node = model
        .node(model.root)
        .ok_or_else(|| corrupt("model has no root node"))?;
    if root_node.kind != K_ELEMENT {
        return Err(corrupt("the model root is not an element"));
    }
    if steps.is_empty() {
        return Ok(Resolved {
            index: model.root,
            matches: 1,
        });
    }
    let root_name = element_name(source, root_node)?;
    if steps[0].index != 1 || steps[0].name != root_name {
        return Err(Error::unsupported_feature(format!(
            "XML root element is {root_name:?}, not {:?}[{}]",
            steps[0].name, steps[0].index
        )));
    }
    let mut index = model.root;
    let mut matches = 1u32;
    for step in &steps[1..] {
        let node = model
            .node(index)
            .ok_or_else(|| corrupt("path traversal left the model"))?;
        if node.kind != K_ELEMENT {
            return Err(corrupt("path traversal reached a non-element node"));
        }
        let mut found = 0u32;
        let mut selected = None;
        for &c in &node.children {
            let cn = model
                .node(c)
                .ok_or_else(|| corrupt("child index is out of range"))?;
            if cn.kind != K_ELEMENT {
                continue;
            }
            if element_name(source, cn)? == step.name {
                found = found.saturating_add(1);
                if found == step.index {
                    selected = Some(c);
                }
            }
        }
        index = selected.ok_or_else(|| {
            Error::unsupported_feature(format!(
                "XML has no element {name:?}[{idx}] at this path",
                name = step.name,
                idx = step.index
            ))
        })?;
        matches = found;
    }
    Ok(Resolved { index, matches })
}

/// Resolve an attribute reference `PATH@NAME` (e.g. `/a/b@id`, or `@id` for the
/// root). A missing element or attribute is a typed decline.
pub fn resolve_attr(model: &XmlModel, source: &[u8], spec: &str) -> Result<ResolvedAttr> {
    let (path, name) = spec
        .rsplit_once('@')
        .ok_or_else(|| Error::usage(format!("XML attribute spec {spec:?} must be PATH@NAME")))?;
    if name.is_empty() {
        return Err(Error::usage(format!(
            "XML attribute spec {spec:?} has an empty name"
        )));
    }
    let r = resolve_path(model, source, path)?;
    let node = model
        .node(r.index)
        .ok_or_else(|| corrupt("attribute element resolved out of range"))?;
    let mut found = 0u32;
    let mut first = None;
    for &a in &node.attrs {
        let attr = model
            .attr(a)
            .ok_or_else(|| corrupt("attribute index is out of range"))?;
        if attr_name(source, attr)? == name {
            found = found.saturating_add(1);
            if first.is_none() {
                first = Some(a);
            }
        }
    }
    let index = first.ok_or_else(|| {
        Error::unsupported_feature(format!(
            "XML element at path {path:?} has no attribute {name:?}"
        ))
    })?;
    Ok(ResolvedAttr {
        index,
        matches: found,
    })
}

/// All namespace declarations (`xmlns`/`xmlns:prefix`) in document order.
pub fn namespaces(model: &XmlModel, source: &[u8]) -> Result<Vec<NsDecl>> {
    let mut out = Vec::new();
    for (i, node) in model.nodes.iter().enumerate() {
        if node.kind != K_ELEMENT || !node.ns_decl {
            continue;
        }
        for &a in &node.attrs {
            let attr = model
                .attr(a)
                .ok_or_else(|| corrupt("attribute index is out of range"))?;
            if attr.is_ns == 0 {
                continue;
            }
            let full = attr_name(source, attr)?;
            let prefix = match full.strip_prefix("xmlns:") {
                Some(p) => p.to_string(),
                None if full == "xmlns" => String::new(),
                None => continue,
            };
            let uri = core::str::from_utf8(attr_value_bytes(source, attr)?)
                .map_err(|_| corrupt("namespace URI is not valid UTF-8"))?
                .to_string();
            out.push(NsDecl {
                element: i as u32,
                prefix,
                uri,
                start: attr.span_start,
                end: attr.span_end,
            });
        }
    }
    Ok(out)
}

/// The concatenation of all character data (text + CDATA content) in the subtree
/// rooted at `index`, in document order. Entity references are surfaced
/// **literally** (never expanded).
pub fn subtree_text(model: &XmlModel, source: &[u8], index: u32) -> Result<String> {
    let mut out = String::new();
    collect_text(model, source, index, &mut out)?;
    Ok(out)
}

fn collect_text(model: &XmlModel, source: &[u8], index: u32, out: &mut String) -> Result<()> {
    let node = model
        .node(index)
        .ok_or_else(|| corrupt("text walk hit an out-of-range node"))?;
    if is_chardata(node.kind) {
        out.push_str(
            core::str::from_utf8(content_bytes(source, node)?)
                .map_err(|_| corrupt("character data is not valid UTF-8"))?,
        );
        return Ok(());
    }
    for &c in &node.children {
        collect_text(model, source, c, out)?;
    }
    Ok(())
}

/// The whole document's character data: every text and CDATA content span in
/// document order (including prolog/epilog text is impossible, but comments/PIs
/// carry none). Entity references are surfaced literally.
pub fn canonical_text(model: &XmlModel, source: &[u8]) -> Result<String> {
    let mut out = String::new();
    for node in &model.nodes {
        if is_chardata(node.kind) {
            out.push_str(
                core::str::from_utf8(content_bytes(source, node)?)
                    .map_err(|_| corrupt("character data is not valid UTF-8"))?,
            );
        }
    }
    Ok(out)
}

/// The index of a node's parent, if any (the root and top-level nodes have none).
pub fn find_parent(model: &XmlModel, index: u32) -> Option<u32> {
    model
        .nodes
        .iter()
        .position(|n| n.children.contains(&index))
        .map(|i| i as u32)
}

/// A bounded, case-sensitive lexical search over element names, attribute names
/// and values, and character data. Returns matches in document order.
pub fn find(
    model: &XmlModel,
    source: &[u8],
    pattern: &str,
    limits: Limits,
) -> Result<Vec<XmlMatch>> {
    let mut out = Vec::new();
    let mut path: Vec<String> = Vec::new();
    walk_find(
        model, source, model.root, &mut path, pattern, &mut out, limits, 0,
    )?;
    Ok(out)
}

#[allow(clippy::too_many_arguments)]
fn walk_find(
    model: &XmlModel,
    source: &[u8],
    index: u32,
    path: &mut Vec<String>,
    pattern: &str,
    out: &mut Vec<XmlMatch>,
    limits: Limits,
    depth: u32,
) -> Result<()> {
    if depth > limits.max_xml_depth {
        return Err(Error::resource_limit(
            "XML find exceeded the nesting-depth cap",
        ));
    }
    let node = model
        .node(index)
        .ok_or_else(|| corrupt("find hit an out-of-range node"))?;
    let here = pointer_of(path);
    if node.kind != K_ELEMENT {
        return Ok(());
    }
    let name = element_name(source, node)?;
    if name.contains(pattern) {
        out.push(XmlMatch {
            path: here.clone(),
            role: MatchRole::Element,
            start: node.name_start,
            end: node.name_end,
            text: name.to_string(),
        });
    }
    for &a in &node.attrs {
        let attr = model
            .attr(a)
            .ok_or_else(|| corrupt("attribute index is out of range"))?;
        let an = attr_name(source, attr)?;
        if an.contains(pattern) {
            out.push(XmlMatch {
                path: format!("{here}@{an}"),
                role: MatchRole::AttrName,
                start: attr.name_start,
                end: attr.name_end,
                text: an.to_string(),
            });
        }
        let av = core::str::from_utf8(attr_value_bytes(source, attr)?)
            .map_err(|_| corrupt("attribute value is not valid UTF-8"))?;
        if av.contains(pattern) {
            out.push(XmlMatch {
                path: format!("{here}@{an}"),
                role: MatchRole::AttrValue,
                start: attr.value_start,
                end: attr.value_end,
                text: av.to_string(),
            });
        }
    }
    for &c in &node.children {
        let cn = model
            .node(c)
            .ok_or_else(|| corrupt("child index is out of range"))?;
        if cn.kind == K_ELEMENT {
            let cname = element_name(source, cn)?.to_string();
            // 1-based ordinal among preceding element siblings with this name.
            let mut ordinal = 1u32;
            for &x in node.children.iter() {
                if x == c {
                    break;
                }
                let xn = model
                    .node(x)
                    .ok_or_else(|| corrupt("child index is out of range"))?;
                if xn.kind == K_ELEMENT && element_name(source, xn)? == cname {
                    ordinal += 1;
                }
            }
            path.push(format!("{cname}[{ordinal}]"));
            walk_find(model, source, c, path, pattern, out, limits, depth + 1)?;
            path.pop();
        } else if is_chardata(cn.kind) {
            let content = core::str::from_utf8(content_bytes(source, cn)?)
                .map_err(|_| corrupt("character data is not valid UTF-8"))?;
            if content.contains(pattern) {
                out.push(XmlMatch {
                    path: here.clone(),
                    role: if cn.kind == K_CDATA {
                        MatchRole::Cdata
                    } else {
                        MatchRole::Text
                    },
                    start: cn.name_start,
                    end: cn.name_end,
                    text: content.to_string(),
                });
            }
        }
    }
    Ok(())
}

fn pointer_of(path: &[String]) -> String {
    let mut s = String::new();
    for seg in path {
        s.push('/');
        s.push_str(seg);
    }
    if s.is_empty() {
        s.push('/');
    }
    s
}

// ---------------------------------------------------------------------------
// The bounded scanner
// ---------------------------------------------------------------------------

fn corrupt(msg: &str) -> Error {
    Error::invalid_xml_structure(format!("malformed XML: {msg}"))
}

fn is_ws(c: u8) -> bool {
    matches!(c, b' ' | b'\t' | b'\r' | b'\n')
}

fn is_name_start(c: u8) -> bool {
    c.is_ascii_alphabetic() || c == b'_' || c == b':' || c >= 0x80
}

fn is_name_char(c: u8) -> bool {
    is_name_start(c) || c.is_ascii_digit() || c == b'-' || c == b'.'
}

struct Parser<'a> {
    b: &'a [u8],
    at: usize,
    limits: Limits,
    build: bool,
    st: XmlState,
    nodes: Vec<XNode>,
    attrs: Vec<XAttr>,
    max_depth_seen: u32,
}

impl<'a> Parser<'a> {
    fn new(b: &'a [u8], limits: Limits, build: bool) -> Self {
        Parser {
            b,
            at: 0,
            limits,
            build,
            st: XmlState::new(),
            nodes: Vec::new(),
            attrs: Vec::new(),
            max_depth_seen: 0,
        }
    }

    fn byte(&self) -> Option<u8> {
        self.b.get(self.at).copied()
    }

    fn starts_with(&self, pat: &[u8]) -> bool {
        self.b.get(self.at..).is_some_and(|s| s.starts_with(pat))
    }

    fn skip_ws(&mut self) {
        while let Some(c) = self.byte() {
            if is_ws(c) {
                self.at += 1;
            } else {
                break;
            }
        }
    }

    fn find(&self, from: usize, pat: &[u8]) -> Option<usize> {
        if pat.is_empty() {
            return Some(from);
        }
        let hay = self.b.get(from..)?;
        if pat.len() > hay.len() {
            return None;
        }
        for i in 0..=(hay.len() - pat.len()) {
            if &hay[i..i + pat.len()] == pat {
                return Some(from + i);
            }
        }
        None
    }

    fn parse_name(&mut self) -> Result<()> {
        let start = self.at;
        match self.byte() {
            Some(c) if is_name_start(c) => self.at += 1,
            _ => return Err(corrupt("expected an XML name")),
        }
        while let Some(c) = self.byte() {
            if is_name_char(c) {
                self.at += 1;
            } else {
                break;
            }
        }
        if self.at == start {
            return Err(corrupt("empty XML name"));
        }
        Ok(())
    }

    fn push_node(&mut self, node: XNode) -> Result<u32> {
        if !self.build {
            return Ok(0);
        }
        if self.nodes.len() as u64 >= u64::from(MAX_MODEL_NODES) {
            return Err(Error::resource_limit("XML model node arena is full"));
        }
        let idx = self.nodes.len() as u32;
        self.nodes.push(node);
        Ok(idx)
    }

    fn push_attr(&mut self, attr: XAttr) -> Result<u32> {
        if !self.build {
            return Ok(0);
        }
        if self.attrs.len() as u64 >= u64::from(MAX_MODEL_ATTRS) {
            return Err(Error::resource_limit("XML model attribute arena is full"));
        }
        let idx = self.attrs.len() as u32;
        self.attrs.push(attr);
        Ok(idx)
    }

    fn parse_document(&mut self) -> Result<u32> {
        let mut root: Option<u32> = None;
        loop {
            self.skip_ws();
            if self.at >= self.b.len() {
                break;
            }
            if self.byte() != Some(b'<') {
                return Err(corrupt("character data outside the root element"));
            }
            if self.starts_with(b"<?") {
                self.parse_pi()?;
            } else if self.starts_with(b"<!--") {
                self.parse_comment()?;
            } else if self.starts_with(b"<!DOCTYPE") {
                self.parse_doctype()?;
            } else if self.byte() == Some(b'<') && self.b.get(self.at + 1) == Some(&b'!') {
                return Err(corrupt("unexpected declaration at the document level"));
            } else {
                if root.is_some() {
                    return Err(corrupt("multiple root elements"));
                }
                root = Some(self.parse_element(1)?);
            }
        }
        root.ok_or_else(|| corrupt("no root element"))
    }

    fn parse_element(&mut self, depth: u32) -> Result<u32> {
        if depth > self.limits.max_xml_depth {
            return Err(Error::resource_limit(format!(
                "XML nesting exceeds the {}-level cap",
                self.limits.max_xml_depth
            )));
        }
        self.max_depth_seen = self.max_depth_seen.max(depth);
        self.st.event(self.limits)?;
        let start = self.at as u64;
        self.at += 1; // '<'
        let name_start = self.at as u64;
        self.parse_name()?;
        let name_end = self.at as u64;
        let mut attr_entries: Vec<XAttr> = Vec::new();
        let self_closing;
        let open_end;
        loop {
            self.skip_ws();
            match self.byte() {
                Some(b'>') => {
                    self.at += 1;
                    open_end = self.at as u64;
                    self.st.open(self.limits)?;
                    self_closing = false;
                    break;
                }
                Some(b'/') => {
                    if self.b.get(self.at + 1) != Some(&b'>') {
                        return Err(corrupt("expected '>' after '/' in a start tag"));
                    }
                    self.at += 2;
                    open_end = self.at as u64;
                    self.st.leaf(self.limits)?;
                    self_closing = true;
                    break;
                }
                Some(_) => {
                    if attr_entries.len() as u64 >= u64::from(self.limits.max_xml_attrs_per_element)
                    {
                        return Err(Error::resource_limit("XML element has too many attributes"));
                    }
                    attr_entries.push(self.parse_attribute()?);
                }
                None => return Err(corrupt("unexpected end of input inside a start tag")),
            }
        }
        let ns_decl = attr_entries.iter().any(|a| a.is_ns != 0);
        let mut attrs = Vec::with_capacity(attr_entries.len());
        for a in attr_entries {
            attrs.push(self.push_attr(a)?);
        }
        let idx = self.push_node(XNode {
            kind: K_ELEMENT,
            start,
            end: open_end,
            name_start,
            name_end,
            open_end,
            close_start: 0,
            ns_decl,
            children: Vec::new(),
            attrs,
        })?;
        if self_closing {
            return Ok(idx);
        }
        let mut children: Vec<u32> = Vec::new();
        loop {
            if self.at >= self.b.len() {
                return Err(corrupt("unclosed element"));
            }
            if self.byte() == Some(b'<') {
                if self.starts_with(b"</") {
                    let close_start = self.at as u64;
                    self.st.event(self.limits)?;
                    self.at += 2;
                    let en_start = self.at;
                    self.parse_name()?;
                    let en_end = self.at;
                    if self.b.get(name_start as usize..name_end as usize)
                        != self.b.get(en_start..en_end)
                    {
                        return Err(corrupt("mismatched end tag"));
                    }
                    self.skip_ws();
                    if self.byte() != Some(b'>') {
                        return Err(corrupt("expected '>' at the end of an end tag"));
                    }
                    self.at += 1;
                    self.st.close();
                    if self.build
                        && let Some(n) = self.nodes.get_mut(idx as usize)
                    {
                        n.children = children;
                        n.close_start = close_start;
                        n.end = self.at as u64;
                    }
                    return Ok(idx);
                } else if self.starts_with(b"<!--") {
                    children.push(self.parse_comment()?);
                } else if self.starts_with(b"<![CDATA[") {
                    children.push(self.parse_cdata()?);
                } else if self.starts_with(b"<?") {
                    children.push(self.parse_pi()?);
                } else if self.b.get(self.at + 1) == Some(&b'!') {
                    return Err(corrupt("declaration inside element content"));
                } else {
                    children.push(self.parse_element(depth + 1)?);
                }
            } else {
                children.push(self.parse_text()?);
            }
        }
    }

    fn parse_attribute(&mut self) -> Result<XAttr> {
        let span_start = self.at as u64;
        let name_start = self.at as u64;
        self.parse_name()?;
        let name_end = self.at as u64;
        self.skip_ws();
        if self.byte() != Some(b'=') {
            return Err(corrupt("expected '=' after an attribute name"));
        }
        self.at += 1;
        self.skip_ws();
        let quote = match self.byte() {
            Some(q @ (b'"' | b'\'')) => q,
            _ => return Err(corrupt("attribute value must be quoted")),
        };
        self.at += 1;
        let value_start = self.at as u64;
        loop {
            match self.byte() {
                None => return Err(corrupt("unterminated attribute value")),
                Some(c) if c == quote => {
                    let value_end = self.at as u64;
                    self.at += 1;
                    let span_end = self.at as u64;
                    let name_bytes = self
                        .b
                        .get(name_start as usize..name_end as usize)
                        .ok_or_else(|| corrupt("attribute name span overflow"))?;
                    let is_ns = if name_bytes == b"xmlns" {
                        1
                    } else if name_bytes.starts_with(b"xmlns:") {
                        2
                    } else {
                        0
                    };
                    return Ok(XAttr {
                        name_start,
                        name_end,
                        value_start,
                        value_end,
                        span_start,
                        span_end,
                        is_ns,
                    });
                }
                Some(b'<') => return Err(corrupt("'<' is not allowed in an attribute value")),
                Some(_) => self.at += 1,
            }
        }
    }

    fn parse_text(&mut self) -> Result<u32> {
        let start = self.at;
        while let Some(c) = self.byte() {
            if c == b'<' {
                break;
            }
            self.at += 1;
        }
        let end = self.at;
        let raw = self
            .b
            .get(start..end)
            .ok_or_else(|| corrupt("text span overflow"))?;
        if raw.windows(3).any(|w| w == b"]]>") {
            return Err(corrupt("']]>' is not allowed in character data"));
        }
        self.st.event(self.limits)?;
        self.st.text(end - start, self.limits)?;
        self.push_node(XNode {
            kind: K_TEXT,
            start: start as u64,
            end: end as u64,
            name_start: start as u64,
            name_end: end as u64,
            open_end: 0,
            close_start: 0,
            ns_decl: false,
            children: Vec::new(),
            attrs: Vec::new(),
        })
    }

    fn parse_cdata(&mut self) -> Result<u32> {
        self.st.event(self.limits)?;
        let start = self.at;
        self.at += 9; // <![CDATA[
        let content_start = self.at;
        let close = self
            .find(self.at, b"]]>")
            .ok_or_else(|| corrupt("unterminated CDATA section"))?;
        let content_end = close;
        self.at = close + 3;
        let end = self.at;
        self.st.text(content_end - content_start, self.limits)?;
        self.push_node(XNode {
            kind: K_CDATA,
            start: start as u64,
            end: end as u64,
            name_start: content_start as u64,
            name_end: content_end as u64,
            open_end: 0,
            close_start: 0,
            ns_decl: false,
            children: Vec::new(),
            attrs: Vec::new(),
        })
    }

    fn parse_comment(&mut self) -> Result<u32> {
        self.st.event(self.limits)?;
        let start = self.at;
        self.at += 4; // <!--
        let content_start = self.at;
        let close = self
            .find(self.at, b"-->")
            .ok_or_else(|| corrupt("unterminated comment"))?;
        let content_end = close;
        // XML forbids '--' inside a comment.
        if self
            .b
            .get(content_start..content_end)
            .is_some_and(|c| c.windows(2).any(|w| w == b"--"))
        {
            return Err(corrupt("'--' is not allowed inside a comment"));
        }
        self.at = close + 3;
        let end = self.at;
        self.push_node(XNode {
            kind: K_COMMENT,
            start: start as u64,
            end: end as u64,
            name_start: content_start as u64,
            name_end: content_end as u64,
            open_end: 0,
            close_start: 0,
            ns_decl: false,
            children: Vec::new(),
            attrs: Vec::new(),
        })
    }

    fn parse_pi(&mut self) -> Result<u32> {
        self.st.event(self.limits)?;
        let start = self.at;
        self.at += 2; // <?
        let name_start = self.at;
        self.parse_name()?;
        let name_end = self.at;
        let close = self
            .find(self.at, b"?>")
            .ok_or_else(|| corrupt("unterminated processing instruction"))?;
        self.at = close + 2;
        let end = self.at;
        self.push_node(XNode {
            kind: K_PI,
            start: start as u64,
            end: end as u64,
            name_start: name_start as u64,
            name_end: name_end as u64,
            open_end: 0,
            close_start: 0,
            ns_decl: false,
            children: Vec::new(),
            attrs: Vec::new(),
        })
    }

    fn parse_doctype(&mut self) -> Result<u32> {
        self.st.event(self.limits)?;
        let start = self.at;
        self.at += 9; // <!DOCTYPE
        loop {
            match self.byte() {
                None => return Err(corrupt("unterminated DOCTYPE")),
                // An internal subset declares entities: refuse it (no XXE, no
                // billion-laughs). This mirrors the shared bounded-XML policy.
                Some(b'[') => return Err(doctype_declined()),
                Some(b'>') => {
                    self.at += 1;
                    break;
                }
                Some(_) => self.at += 1,
            }
        }
        let end = self.at;
        let raw = self
            .b
            .get(start..end)
            .ok_or_else(|| corrupt("DOCTYPE span overflow"))?;
        let text = core::str::from_utf8(raw).map_err(|_| corrupt("DOCTYPE is not valid UTF-8"))?;
        accept_doctype(text)?;
        self.push_node(XNode {
            kind: K_DOCTYPE,
            start: start as u64,
            end: end as u64,
            name_start: (start + 9).min(end) as u64,
            name_end: end as u64,
            open_end: 0,
            close_start: 0,
            ns_decl: false,
            children: Vec::new(),
            attrs: Vec::new(),
        })
    }
}

struct BinReader<'a> {
    b: &'a [u8],
    at: usize,
}

impl<'a> BinReader<'a> {
    fn new(b: &'a [u8]) -> Self {
        BinReader { b, at: 0 }
    }

    fn bytes(&mut self, n: usize) -> Result<&'a [u8]> {
        let end = self
            .at
            .checked_add(n)
            .ok_or_else(|| corrupt("reader overflow"))?;
        let s = self
            .b
            .get(self.at..end)
            .ok_or_else(|| corrupt("reader underrun"))?;
        self.at = end;
        Ok(s)
    }

    fn u8(&mut self) -> Result<u8> {
        Ok(self.bytes(1)?[0])
    }

    fn u32(&mut self) -> Result<u32> {
        let b = self.bytes(4)?;
        Ok(u32::from_le_bytes([b[0], b[1], b[2], b[3]]))
    }

    fn u64(&mut self) -> Result<u64> {
        let b = self.bytes(8)?;
        Ok(u64::from_le_bytes([
            b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
        ]))
    }

    fn at_end(&self) -> bool {
        self.at == self.b.len()
    }
}

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

    fn model(src: &[u8]) -> XmlModel {
        parse(src, Limits::DEFAULT, true).unwrap()
    }

    #[test]
    fn detects_xml_and_rejects_junk() {
        assert!(detect(b"<?xml version=\"1.0\"?><a/>", Limits::DEFAULT));
        assert!(detect(b"<a><b>x</b></a>", Limits::DEFAULT));
        assert!(detect(b"<!-- c --><a/>", Limits::DEFAULT));
        assert!(detect(b"<!DOCTYPE a><a/>", Limits::DEFAULT));
        assert!(!detect(b"", Limits::DEFAULT));
        assert!(!detect(b"plain prose", Limits::DEFAULT));
        assert!(!detect(b"<<<", Limits::DEFAULT));
        assert!(!detect(b"<3", Limits::DEFAULT));
        assert!(!detect(b"<a>", Limits::DEFAULT));
        assert!(!detect(b"<a></b>", Limits::DEFAULT));
        // An internal-subset DOCTYPE (billion-laughs) is refused -> Opaque.
        assert!(!detect(
            b"<!DOCTYPE a [<!ENTITY x \"y\">]><a/>",
            Limits::DEFAULT
        ));
    }

    #[test]
    fn preserves_spans_comments_cdata_pi_and_namespaces() {
        let src: &[u8] =
            b"<?xml version=\"1.0\"?><!-- hi --><a xmlns:p=\"u\" id='7'>t<![CDATA[x<y]]><p:b/></a>";
        let m = model(src);
        // The root element's name and start tag.
        let root = m.node(m.root).unwrap();
        assert_eq!(element_name(src, root).unwrap(), "a");
        assert_eq!(&src[root.start as usize..root.name_end as usize], b"<a");
        // Namespace declaration is recorded.
        let ns = namespaces(&m, src).unwrap();
        assert_eq!(ns.len(), 1);
        assert_eq!(ns[0].prefix, "p");
        assert_eq!(ns[0].uri, "u");
        assert_eq!(
            &src[ns[0].start as usize..ns[0].end as usize],
            b"xmlns:p=\"u\""
        );
        // The comment and CDATA keep their exact spans.
        let comment = m.nodes.iter().find(|n| n.kind == K_COMMENT).unwrap();
        assert_eq!(token_bytes(src, comment).unwrap(), b"<!-- hi -->");
        assert_eq!(content_bytes(src, comment).unwrap(), b" hi ");
        let cdata = m.nodes.iter().find(|n| n.kind == K_CDATA).unwrap();
        assert_eq!(token_bytes(src, cdata).unwrap(), b"<![CDATA[x<y]]>");
        assert_eq!(content_bytes(src, cdata).unwrap(), b"x<y");
        // The element text is the raw character data (entity refs unexpanded).
        assert_eq!(subtree_text(&m, src, m.root).unwrap(), "tx<y");
        // Attribute exact span preserves the single quotes.
        let ra = resolve_attr(&m, src, "/a@id").unwrap();
        let attr = m.attr(ra.index).unwrap();
        assert_eq!(
            &src[attr.span_start as usize..attr.span_end as usize],
            b"id='7'"
        );
        assert_eq!(attr_value_bytes(src, attr).unwrap(), b"7");
    }

    #[test]
    fn path_resolution_handles_positions_and_errors() {
        let src: &[u8] = b"<r><a>1</a><a>2</a><b><a>3</a></b></r>";
        let m = model(src);
        let r = resolve_path(&m, src, "/r/a[2]").unwrap();
        assert_eq!(subtree_text(&m, src, r.index).unwrap(), "2");
        let r = resolve_path(&m, src, "/r/b/a").unwrap();
        assert_eq!(subtree_text(&m, src, r.index).unwrap(), "3");
        assert!(resolve_path(&m, src, "/r/nope").is_err());
        assert!(resolve_path(&m, src, "/r/a[3]").is_err());
        assert!(resolve_path(&m, src, "no-slash").is_err());
        assert!(resolve_path(&m, src, "/r/a[0]").is_err());
        // A path whose first step is not the root element is a typed decline.
        assert!(resolve_path(&m, src, "/a").is_err());
    }

    #[test]
    fn entity_references_are_surfaced_literally() {
        // A predefined entity is NOT expanded in the reported text span.
        let src: &[u8] = b"<a>a&amp;b</a>";
        let m = model(src);
        assert_eq!(subtree_text(&m, src, m.root).unwrap(), "a&amp;b");
        // The whole-source exactness is independent of the derived projection.
        assert!(!build_xml_model(src, Limits::DEFAULT).unwrap().is_empty());
    }

    #[test]
    fn billion_laughs_declines_typed() {
        let src: &[u8] = b"<!DOCTYPE lolz [<!ENTITY lol \"lol\">]><lolz>&lol;</lolz>";
        assert!(!detect(src, Limits::DEFAULT));
        let e = parse(src, Limits::DEFAULT, true).unwrap_err();
        assert_eq!(e.class(), crate::error::ErrorClass::InvalidXmlStructure);
    }

    #[test]
    fn deep_nesting_declines_typed() {
        let mut src = Vec::new();
        for _ in 0..200 {
            src.extend_from_slice(b"<a>");
        }
        src.extend_from_slice(b"x");
        for _ in 0..200 {
            src.extend_from_slice(b"</a>");
        }
        assert!(!detect(&src, Limits::STRICT));
        let e = parse(&src, Limits::STRICT, true).unwrap_err();
        assert_eq!(e.class(), crate::error::ErrorClass::ResourceLimit);
    }

    #[test]
    fn model_roundtrips() {
        let src: &[u8] = b"<r a=\"1\" b='2'><c/>t</r>";
        let m = model(src);
        let bytes = m.encode();
        assert_eq!(XmlModel::decode(&bytes).unwrap(), m);
        for cut in 0..bytes.len() {
            let _ = XmlModel::decode(&bytes[..cut]);
        }
    }

    #[test]
    fn random_bytes_never_panic() {
        let mut x: u64 = 0x9E37_79B9_7F4A_7C15;
        for _ in 0..512 {
            let mut buf = vec![0u8; 256];
            for b in buf.iter_mut() {
                x ^= x << 13;
                x ^= x >> 7;
                x ^= x << 17;
                *b = (x & 0xFF) as u8;
            }
            let _ = detect(&buf, Limits::STRICT);
            if let Ok(m) = parse(&buf, Limits::STRICT, true) {
                let _ = m.encode();
                let _ = canonical_text(&m, &buf);
                let _ = find(&m, &buf, "a", Limits::STRICT);
            }
        }
    }
}