libxml-rs 0.1.0-alpha.39

Native-Rust forensic reimplementation of libxml2+libxslt with C ABI drop-in replacement. Cross-version oracle matrix (libxml2 2.7.8-2.15.3, libxslt 1.1.26-1.1.45) with semantic epochs; full xmllint/xmlcatalog/xsltproc CLIs; differential-court-verified C API closure; three-DSO ELF packaging (libxml2.so.16 core + libxslt.so.1/libexslt.so.0 facades, upstream NEEDED chain); fail-closed oracle-isolated ABI-FUNCTION-SIGNATURE plane (SOURCE_PROTOTYPE + MACHINE_ABI fingerprints, zero silent omissions); Phase-12 real downstream substitution (binary/static/docker substitution, export-surface disposition, ELF version graphs); Phase-13 hostile audit courts (ABI/ownership/allocator/callbacks/failure/threads/oracle-contamination) byte-identical vs the system oracle incl. the upstream thread-local globals model; Phase-14 downstream custodian validation courts (lxml/Nokogiri/PHP/Debian). Test counts live in atlas/TEST_COUNTS.json, residuals in atlas/RESIDUAL_LEDGER.json (generated evidence).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
//! Entity handling (§24, §85 Phase 6).
//!
//! General entities, parameter entities, external entities, entity
//! substitution, entity references, security limits, recursive entities,
//! expansion limits.
//!
//! # Upstream contract
//!
//! Mirrors upstream entities.c and the entity paths of parser.c
//! (SRC-LIBXML2-2.15.0, oracle tree `oracle/historical/src/libxml2-2.15.0/`):
//! xmlAddEntity, xmlGetEntity, xmlGetPredefinedEntity, xmlNewReference,
//! entity content caching and the expansion limits. Parity target: the system
//! libxml2 2.15.3 oracle.
//!
//! # Conceptual behavior
//!
//! General entities, parameter entities, external entities, entity
//! substitution, entity references, security limits, recursive entities and
//! expansion limits. The entity model parses a referenced entity content once
//! into ent->children (XML_ENT_PARSED / XML_ENT_EXPANDING flags) and reuses
//! it — structural re-expansion is impossible by construction.
//!
//! # Ownership & safety invariants
//!
//! Ownership: entity declarations are owned by the DTD hash tables
//! (entities/pentities); ent->children nodes are owned by the declaration and
//! freed with the DTD; entity-ref nodes share content with the entity
//! (xmlNewReference semantics) and must never be freed separately (R-000164).
//! SAFETY: the expansion guards make entity processing loop-free.
//!
//! # Historical quirks & epochs
//!
//! Security epochs: CVE-2014-3660 (billion laughs) fix be2a7eda and its
//! regression fix 72a46a51 (SEC-0006) bounded expansion; CVE-2013-2877
//! (SEC-0004) added loop detection; the 2015 batch (SEC-0008: 69030714,
//! f1063fdb) fixed entity-boundary bugs; the recursion-depth increments came
//! from commit 8f30bdff (2016, SEC-0009). XML_ENTITY_CONTENT_DEPTH_MAX = 32
//! and XML_ENTITY_CONTENT_EXPANSION_MAX = 1,000,000 follow upstream.
//!
//! # Deliberate oddities
//!
//! Deliberate oddities: the amplification guard fires unconditionally with no
//! XML_PARSE_HUGE bypass; unloadable external entities fail silently
//! (xmlCtxtParseEntity) rather than raising undeclared-entity errors;
//! predefined entities (amp/lt/gt/quot/apos) substitute unconditionally
//! regardless of XML_PARSE_NOENT.
//!
//! # Proving courts
//!
//! PARSER-ENTITY-* court family, SECURITY-LIMITS probe (amplification sweep
//! L4..L9 x 10 matches the oracle on every boundary), TREE-001, CLI-XMLLINT-
//! 0033/0034 and `cargo test --lib`.
//!
//! # Tempting simplifications that would break parity
//!
//! Not caching entity content would re-parse per reference (exponential
//! blowup — the vulnerable pre-CVE behavior); skipping the XML_ENT_EXPANDING
//! re-entry check would loop on a self-referential entity (CVE-2013-2877). Do
//! not drop the silent-failure path for unloadable external entities — the
//! NONET oracle behavior depends on it (SD-004).
//!
//! # Safety
//!
//! - The module-level `unsafe impl Sync for SyncPtr` is only instantiated in
//!   this module as the `static PREDEFINED_ENTITIES` array of `_xmlEntity`
//!   values. That static and the byte-string literals it points to are
//!   immutable after initialization and are only ever read (never mutated or
//!   freed), so sharing references to it across threads cannot cause data
//!   races or use-after-free.

use core::ffi::c_void;
use core::mem::size_of;
use core::ptr;
use std::os::raw::{c_char, c_int, c_uint, c_ulong};

use crate::abi::allocator;
use crate::abi::structs::*;
use crate::abi::types::xmlElementType::*;
use crate::abi::types::xmlEntityType::*;
use crate::abi::types::*;
use crate::xml::hash;
use crate::xml::string;

// ═══════════════════════════════════════════════════════════════════════════════
// Constants
// ═══════════════════════════════════════════════════════════════════════════════

/// Maximum entity recursion depth.
///
/// # UPSTREAM-PARITY
///
/// libxml2 uses XML_ENTITY_CONTENT_DEPTH_MAX (default 32).
pub const XML_ENTITY_CONTENT_DEPTH_MAX: c_int = 32;

/// Maximum entity expansion size (in bytes).
///
/// # UPSTREAM-PARITY
///
/// libxml2 uses XML_ENTITY_CONTENT_EXPANSION_MAX (default 1,000,000).
pub const XML_ENTITY_CONTENT_EXPANSION_MAX: c_uint = 1_000_000;

// ═══════════════════════════════════════════════════════════════════════════════
// Entity Declaration Functions
// ═══════════════════════════════════════════════════════════════════════════════

/// Add an entity declaration to a DTD.
///
/// # UPSTREAM-PARITY
///
/// Add an entity declaration to a DTD's hash table (DTD-level helper).
///
/// # UPSTREAM-PARITY
///
/// This is the historical tree.c `xmlAddEntity` core (dtd-level add) and
/// backs the candidate's `xmlAddDocEntity`/`xmlAddDtdEntity` and the parser
/// entity-declaration path. The exported `xmlAddEntity` (entities.h, 2.15)
/// is the document-level `add_entity_doc` with the int/error-code contract
/// (R-000176).
///
/// Adds an entity to the appropriate hash table in the DTD based on the
/// entity type (general entities go to `entities`, parameter entities to
/// `pentities`).
///
/// If an entity with the same name already exists, the existing entity
/// is returned and no new entity is created.
///
/// # SAFETY
///
/// - `dtd` must be a valid pointer to an _xmlDtd, or NULL.
/// - `name` must be a valid null-terminated string.
/// - `ExternalID`, `SystemID`, `content` may be NULL.
pub unsafe fn add_entity(
    dtd: *mut _xmlDtd,
    name: *const xmlChar,
    etype: c_int,
    ExternalID: *const xmlChar,
    SystemID: *const xmlChar,
    content: *const xmlChar,
) -> *mut _xmlEntity {
    unsafe { add_entity_impl(dtd, name, etype, ExternalID, SystemID, content, ptr::null()) }
}

/// Like `add_entity`, but also records the ORIGINAL declaration text on the
/// entity (`orig`). UPSTREAM-PARITY (parser.c xmlParseEntityDecl): internal
/// entities parsed from a DTD keep the raw source text of their value in
/// `ent->orig`, and xmlsave.c `xmlBufDumpEntityDecl` prints `orig` verbatim
/// (only `"` escaped) when present — falling back to the content path (which
/// additionally escapes `%` to `%`) only for entities without `orig`
/// (php bug67081: `<!ENTITY % attrs "%coreattrs;">` must round-trip with a
/// raw `%`).
///
/// # SAFETY
///
/// - Same as `add_entity`; `orig` may be NULL.
pub unsafe fn add_entity_with_orig(
    dtd: *mut _xmlDtd,
    name: *const xmlChar,
    etype: c_int,
    ExternalID: *const xmlChar,
    SystemID: *const xmlChar,
    content: *const xmlChar,
    orig: *const xmlChar,
) -> *mut _xmlEntity {
    unsafe { add_entity_impl(dtd, name, etype, ExternalID, SystemID, content, orig) }
}

#[allow(clippy::too_many_arguments)]
unsafe fn add_entity_impl(
    dtd: *mut _xmlDtd,
    name: *const xmlChar,
    etype: c_int,
    ExternalID: *const xmlChar,
    SystemID: *const xmlChar,
    content: *const xmlChar,
    orig: *const xmlChar,
) -> *mut _xmlEntity {
    if dtd.is_null() || name.is_null() {
        return ptr::null_mut();
    }

    unsafe {
        let d = &*dtd;

        // Determine which hash table to use
        let is_param = is_parameter_entity(etype);
        // UPSTREAM-PARITY (entities.c xmlAddEntity): the table is created
        // lazily on first use.
        if is_param {
            if (*dtd).pentities.is_null() {
                (*dtd).pentities = hash::hash_create(8) as *mut c_void;
            }
        } else if (*dtd).entities.is_null() {
            (*dtd).entities = hash::hash_create(8) as *mut c_void;
        }
        let hash_table = if is_param {
            d.pentities as *mut hash::HashTable
        } else {
            d.entities as *mut hash::HashTable
        };

        // Check if entity already exists
        let existing = hash::hash_lookup(hash_table, name);
        if !existing.is_null() {
            return existing as *mut _xmlEntity;
        }

        // SAFETY: Allocate zero-initialized memory for the entity.
        let entity = allocator::xmlMallocZero(size_of::<_xmlEntity>() as usize) as *mut _xmlEntity;
        if entity.is_null() {
            return ptr::null_mut();
        }

        (*entity).type_ = XML_ENTITY_DECL as c_int;
        (*entity).name = string::xml_strdup(name);
        (*entity).etype = etype;
        (*entity).ExternalID = string::xml_strdup(ExternalID);
        (*entity).SystemID = string::xml_strdup(SystemID);
        (*entity).content = string::xml_strdup(content);
        (*entity).orig = if orig.is_null() {
            ptr::null_mut()
        } else {
            string::xml_strdup(orig)
        };
        (*entity).length = if content.is_null() {
            0
        } else {
            string::xml_strlen(content) as c_int
        };
        (*entity).flags = 0;
        (*entity).expandedSize = 0;
        (*entity).URI = ptr::null_mut();
        (*entity).parent = dtd;
        (*entity).doc = d.doc;
        (*entity).nexte = ptr::null_mut();
        (*entity).owner = 0;

        // Add to hash table
        let ret = hash::hash_add_entry(hash_table, name, entity as *mut c_void);
        if ret != 0 {
            // Failed to add
            free_entity_internal(entity, false);
            return ptr::null_mut();
        }

        // UPSTREAM-PARITY (entities.c xmlAddDocEntity/xmlAddDtdEntity
        // "Link it to the DTD"): the entity decl is a child node of the DTD.
        if (*dtd).last.is_null() {
            (*dtd).children = entity as *mut _xmlNode;
            (*dtd).last = entity as *mut _xmlNode;
        } else {
            (*(*dtd).last).next = entity as *mut _xmlNode;
            (*entity).prev = (*dtd).last;
            (*dtd).last = entity as *mut _xmlNode;
        }

        entity
    }
}

/// Add an entity to a document's DTD (upstream entities.c `xmlAddEntity`, 2.15).
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xmlAddEntity(xmlDoc *doc, int extSubset, const xmlChar *name, int type,
///                  const xmlChar *publicId, const xmlChar *systemId,
///                  const xmlChar *content, xmlEntity **out);
/// ```
///
/// Mirrors the upstream control flow exactly (entities.c 2.15.0):
///
/// - `*out` is set to NULL on entry (upstream line: `if (out != NULL)
///   *out = NULL;`);
/// - NULL `doc`/`name` → `XML_ERR_ARGUMENT`;
/// - missing DTD (extSubset selects the external subset, else internal) →
///   `XML_DTD_NO_DTD`;
/// - a predefined entity (lt/gt/amp/apos/quot) may only be redeclared with
///   the exact replacement-content form upstream accepts (XML 1.0 §4.6),
///   otherwise → `XML_ERR_REDECL_PREDEF_ENTITY`;
/// - an unknown entity type → `XML_ERR_ARGUMENT`;
/// - allocation failure → `XML_ERR_NO_MEMORY`;
/// - a name already present in the selected table → `XML_WAR_ENTITY_REDEFINED`
///   (the freshly created entity is freed);
/// - success → `*out = entity` and returns 0.
///
/// The entity is created without going through the exported hooks, exactly
/// as upstream `xmlCreateEntity` allocates directly with `xmlMalloc`.
///
/// # SAFETY
///
/// - `doc` must be a valid `_xmlDoc` pointer (or NULL), `name` a valid
///   NUL-terminated string, `publicId`/`systemId`/`content` NUL-terminated
///   or NULL, and `out` a writable `xmlEntity*` slot or NULL.
#[allow(clippy::too_many_arguments)]
pub unsafe fn add_entity_doc(
    doc: *mut _xmlDoc,
    ext_subset: c_int,
    name: *const xmlChar,
    etype: c_int,
    public_id: *const xmlChar,
    system_id: *const xmlChar,
    content: *const xmlChar,
    out: *mut *mut _xmlEntity,
) -> c_int {
    use crate::abi::types::{
        XML_DTD_NO_DTD, XML_ERR_ARGUMENT, XML_ERR_NO_MEMORY, XML_ERR_REDECL_PREDEF_ENTITY,
        XML_WAR_ENTITY_REDEFINED,
    };
    unsafe {
        if !out.is_null() {
            *out = ptr::null_mut();
        }
        if doc.is_null() || name.is_null() {
            return XML_ERR_ARGUMENT;
        }
        let dtd = if ext_subset != 0 {
            (*doc).extSubset
        } else {
            (*doc).intSubset
        };
        if dtd.is_null() {
            return XML_DTD_NO_DTD;
        }

        // Select the table by entity type (upstream switch), creating it
        // lazily with the candidate's dictionary-backed hash (hash_create).
        let general = etype == XML_INTERNAL_GENERAL_ENTITY as c_int
            || etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY as c_int
            || etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY as c_int;
        let parameter = etype == XML_INTERNAL_PARAMETER_ENTITY as c_int
            || etype == XML_EXTERNAL_PARAMETER_ENTITY as c_int;
        let table_field: *mut hash::HashTable = if general {
            // XML 1.0 §4.6 predefined-entity redeclaration check.
            let predef = lookup_predefined_entity(name);
            if !predef.is_null() {
                let mut valid = 0;
                if etype == XML_INTERNAL_GENERAL_ENTITY as c_int && !content.is_null() {
                    let c = *(*predef).content;
                    if (*content == c)
                        && (*(content.add(1)) == 0)
                        && (c == b'>' || c == b'\'' || c == b'"')
                    {
                        valid = 1;
                    } else if (*content == b'&') && (*(content.add(1)) == b'#') {
                        if *(content.add(2)) == b'x' as xmlChar {
                            let hex = b"0123456789ABCDEF";
                            let mut ref_: [u8; 3] = [0, 0, b';'];
                            ref_[0] = hex[(c / 16 % 16) as usize];
                            ref_[1] = hex[(c % 16) as usize];
                            if libc::strcasecmp(
                                content.add(3) as *const c_char,
                                ref_.as_ptr() as *const c_char,
                            ) == 0
                            {
                                valid = 1;
                            }
                        } else {
                            let mut ref_: [u8; 3] = [0, 0, b';'];
                            ref_[0] = b'0' + c / 10 % 10;
                            ref_[1] = b'0' + c % 10;
                            if libc::strcmp(
                                content.add(2) as *const c_char,
                                ref_.as_ptr() as *const c_char,
                            ) == 0
                            {
                                valid = 1;
                            }
                        }
                    }
                }
                if valid == 0 {
                    return XML_ERR_REDECL_PREDEF_ENTITY;
                }
            }
            if (*dtd).entities.is_null() {
                (*dtd).entities = hash::hash_create(8) as *mut c_void;
            }
            (*dtd).entities as *mut hash::HashTable
        } else if parameter {
            if (*dtd).pentities.is_null() {
                (*dtd).pentities = hash::hash_create(8) as *mut c_void;
            }
            (*dtd).pentities as *mut hash::HashTable
        } else {
            return XML_ERR_ARGUMENT;
        };

        // Upstream xmlCreateEntity: allocate zeroed, fill fields, strdup.
        let entity = allocator::xmlMallocZero(size_of::<_xmlEntity>() as usize) as *mut _xmlEntity;
        if entity.is_null() {
            return XML_ERR_NO_MEMORY;
        }
        (*entity).doc = doc;
        (*entity).type_ = XML_ENTITY_DECL as c_int;
        (*entity).etype = etype;
        (*entity).name = string::xml_strdup(name);
        if (*entity).name.is_null() {
            free_entity_internal(entity, false);
            return XML_ERR_NO_MEMORY;
        }
        if !public_id.is_null() {
            (*entity).ExternalID = string::xml_strdup(public_id);
            if (*entity).ExternalID.is_null() {
                free_entity_internal(entity, false);
                return XML_ERR_NO_MEMORY;
            }
        }
        if !system_id.is_null() {
            (*entity).SystemID = string::xml_strdup(system_id);
            if (*entity).SystemID.is_null() {
                free_entity_internal(entity, false);
                return XML_ERR_NO_MEMORY;
            }
        }
        if !content.is_null() {
            (*entity).length = string::xml_strlen(content) as c_int;
            (*entity).content = string::xml_strdup(content);
            if (*entity).content.is_null() {
                free_entity_internal(entity, false);
                return XML_ERR_NO_MEMORY;
            }
        } else {
            (*entity).length = 0;
            (*entity).content = ptr::null_mut();
        }
        (*entity).URI = ptr::null();
        (*entity).orig = ptr::null_mut();
        (*entity).flags = 0;
        (*entity).expandedSize = 0;
        (*entity).nexte = ptr::null_mut();
        (*entity).owner = 0;

        // Upstream xmlHashAdd on the selected table: a name already present
        // frees the fresh entity and reports the redefinition warning; any
        // other add failure is treated as OOM (upstream res < 0).
        let existing = hash::hash_lookup(table_field, name);
        if !existing.is_null() {
            free_entity_internal(entity, false);
            return XML_WAR_ENTITY_REDEFINED;
        }
        let res = hash::hash_add_entry(table_field, name, entity as *mut c_void);
        if res != 0 {
            free_entity_internal(entity, false);
            return XML_ERR_NO_MEMORY;
        }

        // Link it to the DTD (upstream "Link it to the DTD" block).
        (*entity).parent = dtd;
        (*entity).doc = (*dtd).doc;
        if (*dtd).last.is_null() {
            (*dtd).children = entity as *mut _xmlNode;
            (*dtd).last = entity as *mut _xmlNode;
        } else {
            (*(*dtd).last).next = entity as *mut _xmlNode;
            (*entity).prev = (*dtd).last;
            (*dtd).last = entity as *mut _xmlNode;
        }

        if !out.is_null() {
            *out = entity;
        }
        0
    }
}

/// Get a general entity by name.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// xmlEntityPtr xmlGetEntity(xmlDocPtr doc, const xmlChar *name);
/// ```
///
/// Searches the document's DTD for a general entity with the given name.
/// Checks both internal and external subsets.
///
/// # SAFETY
///
/// - `doc` must be a valid pointer to an _xmlDoc, or NULL.
/// - `name` must be a valid null-terminated string.
pub unsafe fn get_entity(doc: *mut _xmlDoc, name: *const xmlChar) -> *mut _xmlEntity {
    // UPSTREAM-PARITY (entities.c xmlGetDocEntity): a NULL document resolves
    // through the PREDEFINED entities only — a doc-less
    // `new DOMEntityReference("amp")` (xmlNewReference with doc NULL) still
    // binds the predefined declaration as its children/last/content, and
    // php's `dom_entity_reference_fetch_and_sync_declaration` re-syncs the
    // same way via xmlGetDocEntity(NULL, ...).
    if doc.is_null() {
        return unsafe { crate::abi::exports_misc::xmlGetPredefinedEntity(name) };
    }
    if name.is_null() {
        return ptr::null_mut();
    }

    unsafe {
        let d = &*doc;

        // Check internal subset
        if !d.intSubset.is_null() {
            let entities = (*d.intSubset).entities as *mut hash::HashTable;
            let found = hash::hash_lookup(entities, name);
            if !found.is_null() {
                return found as *mut _xmlEntity;
            }
        }

        // Check external subset
        if !d.extSubset.is_null() {
            let entities = (*d.extSubset).entities as *mut hash::HashTable;
            let found = hash::hash_lookup(entities, name);
            if !found.is_null() {
                return found as *mut _xmlEntity;
            }
        }

        // UPSTREAM-PARITY (entities.c xmlGetDocEntity): an unregistered name
        // still resolves to the predefined entity (amp/lt/gt/quot/apos).
        unsafe { crate::abi::exports_misc::xmlGetPredefinedEntity(name) }
    }
}

/// Look up an entity in a DTD's entity hash table (upstream
/// `xmlGetDtdEntity` core).
///
/// # SAFETY
///
/// - `dtd` must be a valid DTD pointer or NULL.
/// - `name` must be a valid null-terminated string.
pub unsafe fn get_entity_from_dtd(dtd: *mut _xmlDtd, name: *const xmlChar) -> *mut _xmlEntity {
    if dtd.is_null() || name.is_null() {
        return ptr::null_mut();
    }
    unsafe {
        let entities = (*dtd).entities as *mut hash::HashTable;
        let found = hash::hash_lookup(entities, name);
        if found.is_null() {
            ptr::null_mut()
        } else {
            found as *mut _xmlEntity
        }
    }
}

/// Get a parameter entity by name.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// xmlEntityPtr xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *name);
/// ```
///
/// Searches the document's DTD for a parameter entity with the given name.
///
/// # SAFETY
///
/// - `doc` must be a valid pointer to an _xmlDoc, or NULL.
/// - `name` must be a valid null-terminated string.
pub unsafe fn get_parameter_entity(doc: *mut _xmlDoc, name: *const xmlChar) -> *mut _xmlEntity {
    if doc.is_null() || name.is_null() {
        return ptr::null_mut();
    }

    unsafe {
        let d = &*doc;

        // Check internal subset
        if !d.intSubset.is_null() {
            let pentities = (*d.intSubset).pentities as *mut hash::HashTable;
            let found = hash::hash_lookup(pentities, name);
            if !found.is_null() {
                return found as *mut _xmlEntity;
            }
        }

        // Check external subset
        if !d.extSubset.is_null() {
            let pentities = (*d.extSubset).pentities as *mut hash::HashTable;
            let found = hash::hash_lookup(pentities, name);
            if !found.is_null() {
                return found as *mut _xmlEntity;
            }
        }

        ptr::null_mut()
    }
}

/// Deep copy an entity declaration.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// xmlEntityPtr xmlCopyEntity(xmlEntityPtr entity);
/// ```
///
/// # SAFETY
///
/// - `entity` must be a valid pointer to an _xmlEntity, or NULL.
pub unsafe fn copy_entity(entity: *mut _xmlEntity) -> *mut _xmlEntity {
    if entity.is_null() {
        return ptr::null_mut();
    }

    unsafe {
        let e = &*entity;

        // SAFETY: Allocate zero-initialized memory for the copy.
        let copy = allocator::xmlMallocZero(size_of::<_xmlEntity>() as usize) as *mut _xmlEntity;
        if copy.is_null() {
            return ptr::null_mut();
        }

        (*copy).type_ = XML_ENTITY_DECL as c_int;
        (*copy).name = string::xml_strdup(e.name);
        (*copy).etype = e.etype;
        (*copy).ExternalID = string::xml_strdup(e.ExternalID);
        (*copy).SystemID = string::xml_strdup(e.SystemID);
        (*copy).content = string::xml_strdup(e.content);
        (*copy).orig = string::xml_strdup(e.orig);
        (*copy).length = e.length;
        (*copy).flags = e.flags;
        (*copy).expandedSize = e.expandedSize;
        (*copy).URI = string::xml_strdup(e.URI);
        (*copy).parent = e.parent;
        (*copy).doc = e.doc;
        (*copy).nexte = ptr::null_mut();
        (*copy).owner = e.owner;

        copy
    }
}

/// Free an entity declaration.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void xmlFreeEntity(xmlEntityPtr entity);
/// ```
///
/// # SAFETY
///
/// - `entity` must be a valid pointer to an _xmlEntity, or NULL.
pub unsafe fn free_entity(entity: *mut _xmlEntity) {
    free_entity_internal(entity, true);
}

/// Internal entity free function.
///
/// If `free_children` is true, also frees the entity's children tree nodes.
/// This is separated because the hash deallocator should not free children
/// (they are owned by the document tree), but `xmlFreeEntity` from user code
/// should free everything.
///
/// # Safety
///
/// - `entity` must be NULL or a pointer to a heap-allocated `_xmlEntity` whose
///   `name`, `content`, `orig`, `ExternalID`, `SystemID`, and `URI` fields are
///   each either NULL or pointers to allocator-owned allocations. The call
///   frees every non-NULL field, then frees the entity itself, so afterwards
///   neither the entity nor any of its fields may be dereferenced or freed
///   again by the caller.
unsafe fn free_entity_internal(entity: *mut _xmlEntity, free_children: bool) {
    if entity.is_null() {
        return;
    }

    unsafe {
        let e = &*entity;

        if !e.name.is_null() {
            allocator::xmlFreeImpl(e.name as *mut c_void);
        }
        if !e.content.is_null() {
            allocator::xmlFreeImpl(e.content as *mut c_void);
        }
        if !e.orig.is_null() {
            allocator::xmlFreeImpl(e.orig as *mut c_void);
        }
        if !e.ExternalID.is_null() {
            allocator::xmlFreeImpl(e.ExternalID as *mut c_void);
        }
        if !e.SystemID.is_null() {
            allocator::xmlFreeImpl(e.SystemID as *mut c_void);
        }
        if !e.URI.is_null() {
            allocator::xmlFreeImpl(e.URI as *mut c_void);
        }

        // Free children tree nodes if requested. The materialized replacement
        // tree (upstream xmlNodeParseAttValue fills `ent->children` from
        // `ent->content` on first reference) is owned by the declaration and
        // freed with it (text/cdata nodes and entity-REF nodes; the REFs' own
        // `children` point at declarations, which free_node never descends
        // into).
        if free_children && !e.children.is_null() {
            crate::xml::tree::free_node_list(e.children);
        }

        allocator::xmlFreeImpl(entity as *mut c_void);
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Entity Type Helpers
// ═══════════════════════════════════════════════════════════════════════════════

/// Check if an entity type is a parameter entity.
#[inline]
pub const fn is_parameter_entity(etype: c_int) -> bool {
    etype == XML_INTERNAL_PARAMETER_ENTITY as c_int
        || etype == XML_EXTERNAL_PARAMETER_ENTITY as c_int
}

/// Check if an entity type is an external entity.
#[inline]
pub const fn is_external_entity(etype: c_int) -> bool {
    etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY as c_int
        || etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY as c_int
        || etype == XML_EXTERNAL_PARAMETER_ENTITY as c_int
}

/// Check if an entity type is a predefined entity.
#[inline]
pub const fn is_predefined_entity(etype: c_int) -> bool {
    etype == XML_INTERNAL_PREDEFINED_ENTITY as c_int
}

// ═══════════════════════════════════════════════════════════════════════════════
// Entity Substitution & Encoding
// ═══════════════════════════════════════════════════════════════════════════════

/// Encode special XML characters in a string for output.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// xmlChar *xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input);
/// ```
///
/// Encodes `<`, `>`, `&`, `"`, `'` as their corresponding XML entities.
/// The returned string must be freed with `xmlFree`.
///
/// # SAFETY
///
/// - `input` must be a valid null-terminated xmlChar string, or NULL.
pub unsafe fn encode_entities_reentrant(_doc: *mut _xmlDoc, input: *const xmlChar) -> *mut xmlChar {
    if input.is_null() {
        return ptr::null_mut();
    }

    unsafe {
        // First pass: calculate output size
        let len = string::xml_strlen(input);
        let mut out_len: usize = 0;

        for i in 0..len {
            match *input.add(i) {
                b'<' => out_len += 4,  // &lt;
                b'>' => out_len += 4,  // &gt;
                b'&' => out_len += 5,  // &amp;
                b'"' => out_len += 6,  // &quot;
                b'\'' => out_len += 6, // &apos;
                _c => out_len += 1,
            }
        }

        // Allocate output buffer
        let output = allocator::xmlMallocImpl(out_len + 1) as *mut xmlChar;
        if output.is_null() {
            return ptr::null_mut();
        }

        // Second pass: encode
        let mut j: usize = 0;
        for i in 0..len {
            let c = *input.add(i);
            match c {
                b'<' => {
                    ptr::copy_nonoverlapping(b"&lt;" as *const u8, output.add(j), 4);
                    j += 4;
                }
                b'>' => {
                    ptr::copy_nonoverlapping(b"&gt;" as *const u8, output.add(j), 4);
                    j += 4;
                }
                b'&' => {
                    ptr::copy_nonoverlapping(b"&amp;" as *const u8, output.add(j), 5);
                    j += 5;
                }
                b'"' => {
                    ptr::copy_nonoverlapping(b"&quot;" as *const u8, output.add(j), 6);
                    j += 6;
                }
                b'\'' => {
                    ptr::copy_nonoverlapping(b"&apos;" as *const u8, output.add(j), 6);
                    j += 6;
                }
                _ => {
                    *output.add(j) = c;
                    j += 1;
                }
            }
        }

        *output.add(out_len) = 0; // null-terminate
        output
    }
}

/// Decode entity references in a string.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// xmlChar *xmlStringDecodeEntities(xmlDocPtr doc, const xmlChar *input,
///                                  int what, int end, int end2, int end3);
/// ```
///
/// Replaces entity references (`&name;`) with their content from the
/// document's entity declarations. Also handles numeric character
/// references (`&#NNN;` and `&#xHHH;`).
///
/// The `what` parameter specifies which entities to substitute:
/// - 0: substitute all
/// - 1: substitute only predefined
/// - 2: substitute only general
///
/// `end`, `end2`, `end3` specify terminating characters (0 if none).
///
/// Returns the decoded string (must be freed with `xmlFree`), or NULL on error.
///
/// # SAFETY
///
/// - `doc` may be NULL (no entity lookup, only numeric refs).
/// - `input` must be a valid null-terminated xmlChar string, or NULL.
pub unsafe fn string_decode_entities(
    doc: *mut _xmlDoc,
    input: *const xmlChar,
    what: c_int,
    end: xmlChar,
    end2: xmlChar,
    end3: xmlChar,
) -> *mut xmlChar {
    if input.is_null() {
        return ptr::null_mut();
    }

    unsafe {
        let len = string::xml_strlen(input);
        if len == 0 {
            // Return empty string
            let empty = allocator::xmlMallocImpl(1) as *mut xmlChar;
            if !empty.is_null() {
                *empty = 0;
            }
            return empty;
        }

        // Allocate a generous output buffer (input length + expansion)
        let max_out = len * 4 + 1; // Allow for some expansion
        let output = allocator::xmlMallocImpl(max_out) as *mut xmlChar;
        if output.is_null() {
            return ptr::null_mut();
        }

        let mut out_pos: usize = 0;
        let mut i: usize = 0;

        while i < len {
            let c = *input.add(i);

            // Check for terminating characters
            if (end != 0 && c == end) || (end2 != 0 && c == end2) || (end3 != 0 && c == end3) {
                break;
            }

            if c == b'&' {
                // Entity reference
                i += 1;

                // Check for numeric character reference: &#NNN; or &#xHHH;
                if i < len && *input.add(i) == b'#' {
                    i += 1;
                    let (decoded_char, _consumed) = decode_numeric_ref(input, &mut i, len);
                    if decoded_char != 0 {
                        // Encode the decoded character as UTF-8
                        if decoded_char < 0x80 {
                            if out_pos < max_out - 1 {
                                *output.add(out_pos) = decoded_char as u8;
                                out_pos += 1;
                            }
                        } else if decoded_char < 0x800 {
                            if out_pos < max_out - 2 {
                                *output.add(out_pos) = 0xC0 | ((decoded_char >> 6) as u8);
                                *output.add(out_pos + 1) = 0x80 | ((decoded_char & 0x3F) as u8);
                                out_pos += 2;
                            }
                        } else {
                            if out_pos < max_out - 3 {
                                *output.add(out_pos) = 0xE0 | ((decoded_char >> 12) as u8);
                                *output.add(out_pos + 1) =
                                    0x80 | (((decoded_char >> 6) & 0x3F) as u8);
                                *output.add(out_pos + 2) = 0x80 | ((decoded_char & 0x3F) as u8);
                                out_pos += 3;
                            }
                        }
                    }
                    // decode_numeric_ref already advanced i past ';'
                    // The continue skips the i += 1 at the bottom of the loop
                    continue;
                }

                // General entity reference: &name;
                let entity_name_start = i;
                while i < len
                    && *input.add(i) != b';'
                    && *input.add(i) != b'&'
                    && *input.add(i) != 0
                {
                    i += 1;
                }

                if i < len && *input.add(i) == b';' {
                    // We have a complete entity reference
                    let name_len = i - entity_name_start;
                    if name_len > 0 {
                        // Create a null-terminated name
                        let name_buf = allocator::xmlMallocImpl(name_len + 1) as *mut xmlChar;
                        if !name_buf.is_null() {
                            ptr::copy_nonoverlapping(
                                input.add(entity_name_start),
                                name_buf,
                                name_len,
                            );
                            *name_buf.add(name_len) = 0;

                            // Try to find the entity
                            let mut entity: *mut _xmlEntity = ptr::null_mut();
                            if what != 1 {
                                // Not just predefined
                                if !doc.is_null() {
                                    entity = get_entity(doc, name_buf as *const xmlChar);
                                }
                            }

                            if entity.is_null() {
                                // Try predefined entities
                                entity = lookup_predefined_entity(name_buf as *const xmlChar);
                            }

                            if !entity.is_null() && !(*entity).content.is_null() {
                                // Copy entity content to output
                                let content = (*entity).content;
                                let content_len = string::xml_strlen(content);
                                for j in 0..content_len {
                                    if out_pos < max_out - 1 {
                                        *output.add(out_pos) = *content.add(j);
                                        out_pos += 1;
                                    }
                                }
                            } else {
                                // Entity not found — output the reference as-is
                                if out_pos < max_out - 2 {
                                    *output.add(out_pos) = b'&';
                                    out_pos += 1;
                                }
                                for j in 0..name_len {
                                    if out_pos < max_out - 2 {
                                        *output.add(out_pos) = *input.add(entity_name_start + j);
                                        out_pos += 1;
                                    }
                                }
                                if out_pos < max_out - 1 {
                                    *output.add(out_pos) = b';';
                                    out_pos += 1;
                                }
                            }

                            allocator::xmlFreeImpl(name_buf as *mut c_void);
                        }
                    }
                    // Skip past the semicolon
                    // i is already at the semicolon, loop increment will skip it
                } else {
                    // Malformed reference — output as-is
                    if out_pos < max_out - 1 {
                        *output.add(out_pos) = b'&';
                        out_pos += 1;
                    }
                    // Back up to include all characters we scanned
                    // The loop increment will advance i
                }
            } else {
                // Regular character
                if out_pos < max_out - 1 {
                    *output.add(out_pos) = c;
                    out_pos += 1;
                }
            }

            i += 1;
        }

        *output.add(out_pos) = 0; // null-terminate
        output
    }
}

/// Decode a numeric character reference (`&#NNN;` or `&#xHHH;`).
///
/// Returns the decoded character and the number of additional characters consumed.
///
/// # Safety
///
/// - `input` must be a valid pointer to a buffer of at least `len` readable
///   bytes; `pos` must be a valid `&mut usize` in `0..=len` — the read at
///   `input[pos]` is bounds-checked against `len` before every access.
const unsafe fn decode_numeric_ref(
    input: *const xmlChar,
    pos: &mut usize,
    len: usize,
) -> (u32, usize) {
    unsafe {
        let mut consumed: usize = 0;

        if *pos >= len {
            return (0, 0);
        }

        if *input.add(*pos) == b'x' || *input.add(*pos) == b'X' {
            // Hexadecimal: &#xHHH;
            *pos += 1;
            consumed += 1;

            let mut value: u32 = 0;
            while *pos < len {
                let c = *input.add(*pos);
                if c == b';' {
                    *pos += 1;
                    consumed += 1;
                    return (value, consumed);
                }
                let digit = match c {
                    b'0'..=b'9' => c - b'0',
                    b'a'..=b'f' => c - b'a' + 10,
                    b'A'..=b'F' => c - b'A' + 10,
                    _ => break,
                };
                value = value.wrapping_mul(16).wrapping_add(digit as u32);
                *pos += 1;
                consumed += 1;
            }
            (value, consumed)
        } else {
            // Decimal: &#NNN;
            let mut value: u32 = 0;
            while *pos < len {
                let c = *input.add(*pos);
                if c == b';' {
                    *pos += 1;
                    consumed += 1;
                    return (value, consumed);
                }
                if !c.is_ascii_digit() {
                    break;
                }
                value = value.wrapping_mul(10).wrapping_add((c - b'0') as u32);
                *pos += 1;
                consumed += 1;
            }
            (value, consumed)
        }
    }
}

/// A wrapper for static data that implements Sync for raw pointer types.
struct SyncPtr<T>(pub T);
unsafe impl<T> Sync for SyncPtr<T> {}

/// Look up a predefined XML entity by name.
///
/// Returns the entity pointer (to a static entity) or NULL.
///
/// # Safety
///
/// - `name` must be NULL or a pointer to a NUL-terminated `xmlChar` string
///   that stays readable for the duration of the call: the first byte is read
///   via `name.add(0)` and the full string is compared with
///   `string::xml_strcmp`. The returned pointer aliases the immutable static
///   `PREDEFINED_ENTITIES` array, lives for the whole program, and must not be
///   freed or mutated by the caller.
unsafe fn lookup_predefined_entity(name: *const xmlChar) -> *mut _xmlEntity {
    if name.is_null() {
        return ptr::null_mut();
    }

    // UPSTREAM-PARITY: Predefined entities are: lt, gt, amp, quot, apos
    static PREDEFINED_ENTITIES: SyncPtr<[_xmlEntity; 5]> = SyncPtr([
        _xmlEntity {
            _private: ptr::null_mut(),
            type_: XML_ENTITY_DECL as c_int,
            name: b"lt\0" as *const u8 as *const xmlChar,
            children: ptr::null_mut(),
            last: ptr::null_mut(),
            parent: ptr::null_mut(),
            next: ptr::null_mut(),
            prev: ptr::null_mut(),
            doc: ptr::null_mut(),
            orig: ptr::null_mut(),
            content: b"<\0" as *const u8 as *mut xmlChar,
            length: 1,
            etype: XML_INTERNAL_PREDEFINED_ENTITY as c_int,
            ExternalID: ptr::null(),
            SystemID: ptr::null(),
            nexte: ptr::null_mut(),
            URI: ptr::null(),
            owner: 0,
            flags: 0,
            expandedSize: 0,
        },
        _xmlEntity {
            _private: ptr::null_mut(),
            type_: XML_ENTITY_DECL as c_int,
            name: b"gt\0" as *const u8 as *const xmlChar,
            children: ptr::null_mut(),
            last: ptr::null_mut(),
            parent: ptr::null_mut(),
            next: ptr::null_mut(),
            prev: ptr::null_mut(),
            doc: ptr::null_mut(),
            orig: ptr::null_mut(),
            content: b">\0" as *const u8 as *mut xmlChar,
            length: 1,
            etype: XML_INTERNAL_PREDEFINED_ENTITY as c_int,
            ExternalID: ptr::null(),
            SystemID: ptr::null(),
            nexte: ptr::null_mut(),
            URI: ptr::null(),
            owner: 0,
            flags: 0,
            expandedSize: 0,
        },
        _xmlEntity {
            _private: ptr::null_mut(),
            type_: XML_ENTITY_DECL as c_int,
            name: b"amp\0" as *const u8 as *const xmlChar,
            children: ptr::null_mut(),
            last: ptr::null_mut(),
            parent: ptr::null_mut(),
            next: ptr::null_mut(),
            prev: ptr::null_mut(),
            doc: ptr::null_mut(),
            orig: ptr::null_mut(),
            content: b"&\0" as *const u8 as *mut xmlChar,
            length: 1,
            etype: XML_INTERNAL_PREDEFINED_ENTITY as c_int,
            ExternalID: ptr::null(),
            SystemID: ptr::null(),
            nexte: ptr::null_mut(),
            URI: ptr::null(),
            owner: 0,
            flags: 0,
            expandedSize: 0,
        },
        _xmlEntity {
            _private: ptr::null_mut(),
            type_: XML_ENTITY_DECL as c_int,
            name: b"quot\0" as *const u8 as *const xmlChar,
            children: ptr::null_mut(),
            last: ptr::null_mut(),
            parent: ptr::null_mut(),
            next: ptr::null_mut(),
            prev: ptr::null_mut(),
            doc: ptr::null_mut(),
            orig: ptr::null_mut(),
            content: b"\"\0" as *const u8 as *mut xmlChar,
            length: 1,
            etype: XML_INTERNAL_PREDEFINED_ENTITY as c_int,
            ExternalID: ptr::null(),
            SystemID: ptr::null(),
            nexte: ptr::null_mut(),
            URI: ptr::null(),
            owner: 0,
            flags: 0,
            expandedSize: 0,
        },
        _xmlEntity {
            _private: ptr::null_mut(),
            type_: XML_ENTITY_DECL as c_int,
            name: b"apos\0" as *const u8 as *const xmlChar,
            children: ptr::null_mut(),
            last: ptr::null_mut(),
            parent: ptr::null_mut(),
            next: ptr::null_mut(),
            prev: ptr::null_mut(),
            doc: ptr::null_mut(),
            orig: ptr::null_mut(),
            content: b"'\0" as *const u8 as *mut xmlChar,
            length: 1,
            etype: XML_INTERNAL_PREDEFINED_ENTITY as c_int,
            ExternalID: ptr::null(),
            SystemID: ptr::null(),
            nexte: ptr::null_mut(),
            URI: ptr::null(),
            owner: 0,
            flags: 0,
            expandedSize: 0,
        },
    ]);

    unsafe {
        // Match which entity
        let idx = match *name.add(0) as char {
            'l' => {
                if string::xml_strcmp(name, b"lt\0" as *const u8 as *const xmlChar) == 0 {
                    0
                } else {
                    return ptr::null_mut();
                }
            }
            'g' => {
                if string::xml_strcmp(name, b"gt\0" as *const u8 as *const xmlChar) == 0 {
                    1
                } else {
                    return ptr::null_mut();
                }
            }
            'a' => {
                if string::xml_strcmp(name, b"amp\0" as *const u8 as *const xmlChar) == 0 {
                    2
                } else if string::xml_strcmp(name, b"apos\0" as *const u8 as *const xmlChar) == 0 {
                    4
                } else {
                    return ptr::null_mut();
                }
            }
            'q' if string::xml_strcmp(name, b"quot\0" as *const u8 as *const xmlChar) == 0 => 3,
            _ => return ptr::null_mut(),
        };

        &PREDEFINED_ENTITIES.0[idx] as *const _xmlEntity as *mut _xmlEntity
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Entity Content Retrieval
// ═══════════════════════════════════════════════════════════════════════════════

/// Get the content of an entity as a string.
///
/// # SAFETY
///
/// - `entity` must be a valid pointer to an _xmlEntity, or NULL.
pub unsafe fn get_entity_content(entity: *mut _xmlEntity) -> *mut xmlChar {
    if entity.is_null() {
        return ptr::null_mut();
    }

    unsafe {
        let e = &*entity;
        if e.content.is_null() {
            return ptr::null_mut();
        }
        string::xml_strdup(e.content)
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Security/Limit Functions
// ═══════════════════════════════════════════════════════════════════════════════

/// Check if entity expansion exceeds limits.
///
/// Returns 0 if within limits, -1 if exceeded.
pub const fn check_entity_expansion_limit(expanded_size: c_ulong) -> c_int {
    if expanded_size > XML_ENTITY_CONTENT_EXPANSION_MAX as c_ulong {
        -1
    } else {
        0
    }
}

/// Check if entity recursion depth exceeds limits.
///
/// Returns 0 if within limits, -1 if exceeded.
pub const fn check_entity_recursion_depth(depth: c_int) -> c_int {
    if depth > XML_ENTITY_CONTENT_DEPTH_MAX {
        -1
    } else {
        0
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Tests
// ═══════════════════════════════════════════════════════════════════════════════

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

    use core::ffi::c_void;
    use core::ptr;

    unsafe fn c_str(s: &[u8]) -> *const xmlChar {
        let len = s.len();
        let buf = allocator::xmlMallocImpl(len + 1) as *mut xmlChar;
        assert!(!buf.is_null());
        ptr::copy_nonoverlapping(s.as_ptr(), buf, len);
        *buf.add(len) = 0;
        buf as *const xmlChar
    }

    unsafe fn make_doc_and_dtd() -> (*mut _xmlDoc, *mut _xmlDtd) {
        let doc = allocator::xmlMallocZero(size_of::<_xmlDoc>() as usize) as *mut _xmlDoc;
        assert!(!doc.is_null());
        (*doc).type_ = XML_DOCUMENT_NODE as c_int;
        (*doc).doc = doc;
        let dtd = allocator::xmlMallocZero(size_of::<_xmlDtd>() as usize) as *mut _xmlDtd;
        assert!(!dtd.is_null());
        (*dtd).type_ = XML_DTD_NODE as c_int;
        (*dtd).parent = doc;
        (*dtd).doc = doc;
        (*dtd).entities = hash::hash_create(8) as *mut c_void;
        (*dtd).pentities = hash::hash_create(8) as *mut c_void;
        (*doc).intSubset = dtd;
        (doc, dtd)
    }

    // ── Entity Declaration Tests ────────────────────────────────────────

    #[test]
    /// Tests that a general entity can be added to a DTD and looked back up.
    ///
    /// # Safety
    ///
    /// - The test dereferences only raw pointers it allocates itself: `doc`
    ///   and `dtd` are zero-initialized by `make_doc_and_dtd` and stay alive
    ///   until the final `xmlFreeImpl` calls; `name` and `content` are
    ///   NUL-terminated buffers from `c_str`; `entity` is asserted non-NULL
    ///   before `(*entity)` is read. The hash tables are freed with
    ///   `entity_deallocator` before `dtd` and `doc` are freed, so no access
    ///   touches freed memory.
    fn test_add_entity_general() {
        unsafe {
            let (doc, dtd) = make_doc_and_dtd();
            let name = c_str(b"myEntity");
            let content = c_str(b"Hello, World!");

            let entity = add_entity(
                dtd,
                name,
                XML_INTERNAL_GENERAL_ENTITY as c_int,
                ptr::null(),
                ptr::null(),
                content,
            );
            assert!(!entity.is_null());
            assert_eq!((*entity).etype, XML_INTERNAL_GENERAL_ENTITY as c_int);
            assert_eq!((*entity).length, 13);

            // Lookup
            let found = get_entity(doc, name);
            assert_eq!(found, entity);

            // Cleanup
            // We need to free the hash tables manually since we don't have free_dtd available
            hash::hash_free(
                (*dtd).entities as *mut hash::HashTable,
                Some(entity_deallocator),
            );
            hash::hash_free(
                (*dtd).pentities as *mut hash::HashTable,
                Some(entity_deallocator),
            );
            allocator::xmlFreeImpl(dtd as *mut c_void);
            allocator::xmlFreeImpl(doc as *mut c_void);
        }
    }

    /// Deallocator passed to `hash::hash_free` to release entity payloads.
    ///
    /// # Safety
    ///
    /// - `payload` must be NULL or a pointer to a heap-allocated `_xmlEntity`
    ///   that has not yet been freed, because it is forwarded to
    ///   `free_entity`; `_name` is ignored by this function.
    unsafe extern "C" fn entity_deallocator(payload: *mut c_void, _name: *mut u8) {
        if !payload.is_null() {
            free_entity(payload as *mut _xmlEntity);
        }
    }

    #[test]
    /// Tests that a parameter entity is stored in the `pentities` table.
    ///
    /// # Safety
    ///
    /// - `doc` and `dtd` are zero-initialized by `make_doc_and_dtd` and remain
    ///   live until the trailing frees; `name` and `content` are NUL-terminated
    ///   `c_str` allocations valid for the whole test; the `entity` pointer is
    ///   asserted non-NULL before `(*entity)` is read, and the hash tables are
    ///   freed with `entity_deallocator` before `dtd` and `doc` are freed.
    fn test_add_entity_parameter() {
        unsafe {
            let (doc, dtd) = make_doc_and_dtd();
            let name = c_str(b"myParam");
            let content = c_str(b"parameter content");

            let entity = add_entity(
                dtd,
                name,
                XML_INTERNAL_PARAMETER_ENTITY as c_int,
                ptr::null(),
                ptr::null(),
                content,
            );
            assert!(!entity.is_null());
            assert_eq!((*entity).etype, XML_INTERNAL_PARAMETER_ENTITY as c_int);

            // Parameter entity should be in pentities
            let found = hash::hash_lookup((*dtd).pentities as *mut hash::HashTable, name);
            assert_eq!(found, entity as *mut c_void);

            // General entity lookup should NOT find it
            let not_found = hash::hash_lookup((*dtd).entities as *mut hash::HashTable, name);
            assert!(not_found.is_null());

            hash::hash_free(
                (*dtd).entities as *mut hash::HashTable,
                Some(entity_deallocator),
            );
            hash::hash_free(
                (*dtd).pentities as *mut hash::HashTable,
                Some(entity_deallocator),
            );
            allocator::xmlFreeImpl(dtd as *mut c_void);
            allocator::xmlFreeImpl(doc as *mut c_void);
        }
    }

    #[test]
    /// Tests that `add_entity` returns NULL when the DTD is NULL.
    ///
    /// # Safety
    ///
    /// - `add_entity` checks `dtd` for NULL and returns early without
    ///   dereferencing it, and the `name` argument is a NUL-terminated `c_str`
    ///   allocation that stays alive for the duration of the call.
    fn test_add_entity_null_dtd() {
        unsafe {
            let entity = add_entity(
                ptr::null_mut(),
                c_str(b"test"),
                XML_INTERNAL_GENERAL_ENTITY as c_int,
                ptr::null(),
                ptr::null(),
                ptr::null(),
            );
            assert!(entity.is_null());
        }
    }

    #[test]
    /// Tests that re-adding an entity with the same name reuses the existing
    /// declaration.
    ///
    /// # Safety
    ///
    /// - `doc` and `dtd` are zero-initialized by `make_doc_and_dtd` and outlive
    ///   every dereference; `name` and `content` are NUL-terminated `c_str`
    ///   allocations; `e1` is asserted non-NULL before `(*e1)` is read, and
    ///   the hash tables are freed with `entity_deallocator` before `dtd` and
    ///   `doc` are freed.
    fn test_add_entity_duplicate() {
        unsafe {
            let (doc, dtd) = make_doc_and_dtd();
            let name = c_str(b"dup");
            let content = c_str(b"original");

            let e1 = add_entity(
                dtd,
                name,
                XML_INTERNAL_GENERAL_ENTITY as c_int,
                ptr::null(),
                ptr::null(),
                content,
            );
            assert!(!e1.is_null());

            let e2 = add_entity(
                dtd,
                name,
                XML_INTERNAL_GENERAL_ENTITY as c_int,
                ptr::null(),
                ptr::null(),
                c_str(b"replacement"),
            );
            assert_eq!(e1, e2); // Same pointer

            hash::hash_free(
                (*dtd).entities as *mut hash::HashTable,
                Some(entity_deallocator),
            );
            hash::hash_free(
                (*dtd).pentities as *mut hash::HashTable,
                Some(entity_deallocator),
            );
            allocator::xmlFreeImpl(dtd as *mut c_void);
            allocator::xmlFreeImpl(doc as *mut c_void);
        }
    }

    #[test]
    /// Tests that an external parsed entity keeps its SystemID.
    ///
    /// # Safety
    ///
    /// - `doc` and `dtd` come from `make_doc_and_dtd` and stay live until the
    ///   trailing frees; `name` and `sysid` are NUL-terminated `c_str`
    ///   allocations; `entity` is asserted non-NULL before `(*entity).SystemID`
    ///   is passed to `string::xml_strcmp`, and the hash tables are freed with
    ///   `entity_deallocator` before `dtd` and `doc` are freed.
    fn test_add_entity_external() {
        unsafe {
            let (doc, dtd) = make_doc_and_dtd();
            let name = c_str(b"extEntity");
            let sysid = c_str(b"http://example.com/entity.xml");

            let entity = add_entity(
                dtd,
                name,
                XML_EXTERNAL_GENERAL_PARSED_ENTITY as c_int,
                ptr::null(),
                sysid,
                ptr::null(),
            );
            assert!(!entity.is_null());
            assert_eq!((*entity).etype, XML_EXTERNAL_GENERAL_PARSED_ENTITY as c_int);
            assert_eq!(string::xml_strcmp((*entity).SystemID, sysid), 0);

            hash::hash_free(
                (*dtd).entities as *mut hash::HashTable,
                Some(entity_deallocator),
            );
            hash::hash_free(
                (*dtd).pentities as *mut hash::HashTable,
                Some(entity_deallocator),
            );
            allocator::xmlFreeImpl(dtd as *mut c_void);
            allocator::xmlFreeImpl(doc as *mut c_void);
        }
    }

    #[test]
    /// Tests that `get_entity` returns NULL when the document is NULL.
    ///
    /// # Safety
    ///
    /// - `get_entity` checks `doc` for NULL before dereferencing it, and the
    ///   `name` argument is a NUL-terminated `c_str` allocation that stays
    ///   alive for the duration of the call.
    fn test_get_entity_null_doc() {
        unsafe {
            let found = get_entity(ptr::null_mut(), c_str(b"test"));
            assert!(found.is_null());
        }
    }

    #[test]
    /// Tests that `get_entity` returns NULL for an undeclared name.
    ///
    /// # Safety
    ///
    /// - `doc` and `dtd` are zero-initialized by `make_doc_and_dtd` and stay
    ///   live until the trailing frees; the lookup name is a NUL-terminated
    ///   `c_str` allocation; the hash tables are freed with
    ///   `entity_deallocator` before `dtd` and `doc` are freed.
    fn test_get_entity_not_found() {
        unsafe {
            let (doc, dtd) = make_doc_and_dtd();
            let found = get_entity(doc, c_str(b"nonexistent"));
            assert!(found.is_null());

            hash::hash_free(
                (*dtd).entities as *mut hash::HashTable,
                Some(entity_deallocator),
            );
            hash::hash_free(
                (*dtd).pentities as *mut hash::HashTable,
                Some(entity_deallocator),
            );
            allocator::xmlFreeImpl(dtd as *mut c_void);
            allocator::xmlFreeImpl(doc as *mut c_void);
        }
    }

    #[test]
    /// Tests that `get_parameter_entity` finds a stored parameter entity.
    ///
    /// # Safety
    ///
    /// - `doc` and `dtd` come from `make_doc_and_dtd` and stay live until the
    ///   trailing frees; `name` and `content` are NUL-terminated `c_str`
    ///   allocations; `entity` is asserted non-NULL before the equality check,
    ///   and the hash tables are freed with `entity_deallocator` before `dtd`
    ///   and `doc` are freed.
    fn test_get_parameter_entity() {
        unsafe {
            let (doc, dtd) = make_doc_and_dtd();
            let name = c_str(b"param1");
            let content = c_str(b"param content");

            let entity = add_entity(
                dtd,
                name,
                XML_INTERNAL_PARAMETER_ENTITY as c_int,
                ptr::null(),
                ptr::null(),
                content,
            );
            assert!(!entity.is_null());

            let found = get_parameter_entity(doc, name);
            assert_eq!(found, entity);

            hash::hash_free(
                (*dtd).entities as *mut hash::HashTable,
                Some(entity_deallocator),
            );
            hash::hash_free(
                (*dtd).pentities as *mut hash::HashTable,
                Some(entity_deallocator),
            );
            allocator::xmlFreeImpl(dtd as *mut c_void);
            allocator::xmlFreeImpl(doc as *mut c_void);
        }
    }

    #[test]
    /// Tests that `copy_entity` produces an independent deep copy.
    ///
    /// # Safety
    ///
    /// - `entity` is a zero-initialized `xmlMallocZero` allocation whose
    ///   `name` and `content` fields are fresh `xml_strdup` allocations; the
    ///   original and the copy are both freed with `free_entity` before the
    ///   test returns, and every dereference of these raw pointers happens
    ///   while the allocations are still live.
    fn test_copy_entity() {
        unsafe {
            let name = c_str(b"srcEntity");
            let content = c_str(b"source content");

            // Create an entity without a DTD
            let entity =
                allocator::xmlMallocZero(size_of::<_xmlEntity>() as usize) as *mut _xmlEntity;
            assert!(!entity.is_null());
            (*entity).type_ = XML_ENTITY_DECL as c_int;
            (*entity).name = string::xml_strdup(name);
            (*entity).content = string::xml_strdup(content);
            (*entity).length = 14;
            (*entity).etype = XML_INTERNAL_GENERAL_ENTITY as c_int;

            let copy = copy_entity(entity);
            assert!(!copy.is_null());
            assert_ne!(copy, entity);
            assert_eq!((*copy).etype, XML_INTERNAL_GENERAL_ENTITY as c_int);
            assert_eq!((*copy).length, 14);
            assert_eq!(string::xml_strcmp((*copy).name, name), 0);
            assert_eq!(string::xml_strcmp((*copy).content, content), 0);

            free_entity(copy);
            free_entity(entity);
        }
    }

    #[test]
    /// Tests that `copy_entity` returns NULL for a NULL input.
    ///
    /// # Safety
    ///
    /// - `copy_entity` checks its argument for NULL and returns early without
    ///   dereferencing it, so passing `ptr::null_mut()` is sound.
    fn test_copy_entity_null() {
        unsafe {
            assert!(copy_entity(ptr::null_mut()).is_null());
        }
    }

    #[test]
    /// Tests that `free_entity` tolerates a NULL pointer without crashing.
    ///
    /// # Safety
    ///
    /// - `free_entity` returns early when the pointer is NULL and never
    ///   dereferences it, so the call is sound.
    fn test_free_entity_null() {
        unsafe {
            free_entity(ptr::null_mut()); // Should not crash
        }
    }

    // ── Entity Type Tests ───────────────────────────────────────────────

    #[test]
    fn test_is_parameter_entity() {
        assert!(is_parameter_entity(XML_INTERNAL_PARAMETER_ENTITY as c_int));
        assert!(is_parameter_entity(XML_EXTERNAL_PARAMETER_ENTITY as c_int));
        assert!(!is_parameter_entity(XML_INTERNAL_GENERAL_ENTITY as c_int));
        assert!(!is_parameter_entity(
            XML_EXTERNAL_GENERAL_PARSED_ENTITY as c_int
        ));
    }

    #[test]
    fn test_is_external_entity() {
        assert!(is_external_entity(
            XML_EXTERNAL_GENERAL_PARSED_ENTITY as c_int
        ));
        assert!(is_external_entity(
            XML_EXTERNAL_GENERAL_UNPARSED_ENTITY as c_int
        ));
        assert!(is_external_entity(XML_EXTERNAL_PARAMETER_ENTITY as c_int));
        assert!(!is_external_entity(XML_INTERNAL_GENERAL_ENTITY as c_int));
        assert!(!is_external_entity(XML_INTERNAL_PARAMETER_ENTITY as c_int));
    }

    #[test]
    fn test_is_predefined_entity() {
        assert!(is_predefined_entity(
            XML_INTERNAL_PREDEFINED_ENTITY as c_int
        ));
        assert!(!is_predefined_entity(XML_INTERNAL_GENERAL_ENTITY as c_int));
    }

    // ── Entity Encoding Tests ───────────────────────────────────────────

    #[test]
    /// Tests that `encode_entities_reentrant` returns NULL for NULL input.
    ///
    /// # Safety
    ///
    /// - `encode_entities_reentrant` checks `input` for NULL and returns
    ///   without dereferencing it, so passing null pointers is sound.
    fn test_encode_entities_reentrant_null() {
        unsafe {
            let result = encode_entities_reentrant(ptr::null_mut(), ptr::null());
            assert!(result.is_null());
        }
    }

    #[test]
    /// Tests that text without special characters is encoded unchanged.
    ///
    /// # Safety
    ///
    /// - `input` is a NUL-terminated `c_str` allocation that stays alive until
    ///   the comparison; `result` is the freshly allocated output of
    ///   `encode_entities_reentrant`, asserted non-NULL, and is freed with
    ///   `xmlFreeImpl` after the comparison.
    fn test_encode_entities_reentrant_no_special() {
        unsafe {
            let input = c_str(b"Hello, World!");
            let result = encode_entities_reentrant(ptr::null_mut(), input);
            assert!(!result.is_null());
            assert_eq!(string::xml_strcmp(result, input), 0);
            allocator::xmlFreeImpl(result as *mut c_void);
        }
    }

    #[test]
    /// Tests that the `lt` and `gt` characters are encoded to entities.
    ///
    /// # Safety
    ///
    /// - `input` and `expected` are NUL-terminated `c_str` allocations that
    ///   stay alive until the comparison; `result` is the freshly allocated
    ///   output of `encode_entities_reentrant`, asserted non-NULL, and is
    ///   freed with `xmlFreeImpl` after the comparison.
    fn test_encode_entities_reentrant_lt_gt() {
        unsafe {
            let input = c_str(b"a < b > c");
            let result = encode_entities_reentrant(ptr::null_mut(), input);
            assert!(!result.is_null());
            let expected = c_str(b"a &lt; b &gt; c");
            assert_eq!(string::xml_strcmp(result, expected), 0);
            allocator::xmlFreeImpl(result as *mut c_void);
        }
    }

    #[test]
    /// Tests that the `amp` character is encoded to an entity.
    ///
    /// # Safety
    ///
    /// - `input` and `expected` are NUL-terminated `c_str` allocations that
    ///   stay alive until the comparison; `result` is the freshly allocated
    ///   output of `encode_entities_reentrant`, asserted non-NULL, and is
    ///   freed with `xmlFreeImpl` after the comparison.
    fn test_encode_entities_reentrant_amp() {
        unsafe {
            let input = c_str(b"a & b");
            let result = encode_entities_reentrant(ptr::null_mut(), input);
            assert!(!result.is_null());
            let expected = c_str(b"a &amp; b");
            assert_eq!(string::xml_strcmp(result, expected), 0);
            allocator::xmlFreeImpl(result as *mut c_void);
        }
    }

    #[test]
    /// Tests that quotes and apostrophes are encoded to entities.
    ///
    /// # Safety
    ///
    /// - `input` and `expected` are NUL-terminated `c_str` allocations that
    ///   stay alive until the comparison; `result` is the freshly allocated
    ///   output of `encode_entities_reentrant`, asserted non-NULL, and is
    ///   freed with `xmlFreeImpl` after the comparison.
    fn test_encode_entities_reentrant_quotes() {
        unsafe {
            let input = c_str(b"\"hello\" 'world'");
            let result = encode_entities_reentrant(ptr::null_mut(), input);
            assert!(!result.is_null());
            let expected = c_str(b"&quot;hello&quot; &apos;world&apos;");
            assert_eq!(string::xml_strcmp(result, expected), 0);
            allocator::xmlFreeImpl(result as *mut c_void);
        }
    }

    #[test]
    /// Tests that a mixed string is fully encoded in one pass.
    ///
    /// # Safety
    ///
    /// - `input` and `expected` are NUL-terminated `c_str` allocations that
    ///   stay alive until the comparison; `result` is the freshly allocated
    ///   output of `encode_entities_reentrant`, asserted non-NULL, and is
    ///   freed with `xmlFreeImpl` after the comparison.
    fn test_encode_entities_reentrant_all() {
        unsafe {
            let input = c_str(b"<tag attr=\"value\">&'more'</tag>");
            let result = encode_entities_reentrant(ptr::null_mut(), input);
            assert!(!result.is_null());
            let expected =
                c_str(b"&lt;tag attr=&quot;value&quot;&gt;&amp;&apos;more&apos;&lt;/tag&gt;");
            assert_eq!(string::xml_strcmp(result, expected), 0);
            allocator::xmlFreeImpl(result as *mut c_void);
        }
    }

    // ── Entity Decoding Tests ───────────────────────────────────────────

    #[test]
    /// Tests that `string_decode_entities` returns NULL for a NULL input.
    ///
    /// # Safety
    ///
    /// - `string_decode_entities` returns early when `input` is NULL, and the
    ///   `doc` argument is only dereferenced for entity lookup during
    ///   decoding, so passing null pointers here is sound.
    fn test_decode_entities_null_input() {
        unsafe {
            let result = string_decode_entities(ptr::null_mut(), ptr::null(), 0, 0, 0, 0);
            assert!(result.is_null());
        }
    }

    #[test]
    /// Tests that decoding an empty string yields an empty NUL-terminated
    /// result.
    ///
    /// # Safety
    ///
    /// - `input` is a NUL-terminated `c_str` allocation valid for the call;
    ///   `result` is asserted non-NULL before the `*result` dereference and is
    ///   freed with `xmlFreeImpl` before the test ends.
    fn test_decode_entities_empty() {
        unsafe {
            let input = c_str(b"");
            let result = string_decode_entities(ptr::null_mut(), input, 0, 0, 0, 0);
            assert!(!result.is_null());
            assert_eq!(*result, 0);
            allocator::xmlFreeImpl(result as *mut c_void);
        }
    }

    #[test]
    /// Tests that input without references is decoded unchanged.
    ///
    /// # Safety
    ///
    /// - `input` is a NUL-terminated `c_str` allocation that stays alive until
    ///   the comparison; `result` is asserted non-NULL before being passed to
    ///   `string::xml_strcmp` and is freed with `xmlFreeImpl` afterwards.
    fn test_decode_entities_no_refs() {
        unsafe {
            let input = c_str(b"Hello, World!");
            let result = string_decode_entities(ptr::null_mut(), input, 0, 0, 0, 0);
            assert!(!result.is_null());
            assert_eq!(string::xml_strcmp(result, input), 0);
            allocator::xmlFreeImpl(result as *mut c_void);
        }
    }

    #[test]
    /// Tests that a decimal numeric character reference is decoded.
    ///
    /// # Safety
    ///
    /// - `input` and `expected` are NUL-terminated `c_str` allocations that
    ///   stay alive until the comparison; `result` is asserted non-NULL before
    ///   being passed to `string::xml_strcmp` and is freed with `xmlFreeImpl`
    ///   afterwards.
    fn test_decode_numeric_decimal() {
        unsafe {
            // &#65; = 'A'
            let input = c_str(b"&#65;");
            let result = string_decode_entities(ptr::null_mut(), input, 0, 0, 0, 0);
            assert!(!result.is_null());
            let expected = c_str(b"A");
            assert_eq!(string::xml_strcmp(result, expected), 0);
            allocator::xmlFreeImpl(result as *mut c_void);
        }
    }

    #[test]
    /// Tests that a hexadecimal numeric character reference is decoded.
    ///
    /// # Safety
    ///
    /// - `input` and `expected` are NUL-terminated `c_str` allocations that
    ///   stay alive until the comparison; `result` is asserted non-NULL before
    ///   being passed to `string::xml_strcmp` and is freed with `xmlFreeImpl`
    ///   afterwards.
    fn test_decode_numeric_hex() {
        unsafe {
            // &#x41; = 'A'
            let input = c_str(b"&#x41;");
            let result = string_decode_entities(ptr::null_mut(), input, 0, 0, 0, 0);
            assert!(!result.is_null());
            let expected = c_str(b"A");
            assert_eq!(string::xml_strcmp(result, expected), 0);
            allocator::xmlFreeImpl(result as *mut c_void);
        }
    }

    #[test]
    /// Tests that mixed decimal and hexadecimal references are decoded.
    ///
    /// # Safety
    ///
    /// - `input` and `expected` are NUL-terminated `c_str` allocations that
    ///   stay alive until the comparison; `result` is asserted non-NULL before
    ///   being passed to `string::xml_strcmp` and is freed with `xmlFreeImpl`
    ///   afterwards.
    fn test_decode_numeric_mixed() {
        unsafe {
            let input = c_str(b"Hello &#x57;&#111;rld!"); // Hello World!
            let result = string_decode_entities(ptr::null_mut(), input, 0, 0, 0, 0);
            assert!(!result.is_null());
            let expected = c_str(b"Hello World!");
            assert_eq!(string::xml_strcmp(result, expected), 0);
            allocator::xmlFreeImpl(result as *mut c_void);
        }
    }

    #[test]
    /// Tests that predefined general entities in the DTD are substituted.
    ///
    /// # Safety
    ///
    /// - `doc` and `dtd` are zero-initialized by `make_doc_and_dtd` and stay
    ///   live until the trailing frees; the `input` and `expected` buffers are
    ///   NUL-terminated `c_str` allocations; `result` is asserted non-NULL
    ///   before the comparison and freed with `xmlFreeImpl`; the hash tables
    ///   are freed with `entity_deallocator` before `dtd` and `doc` are freed.
    fn test_decode_predefined_entities() {
        unsafe {
            let (doc, dtd) = make_doc_and_dtd();
            let input = c_str(b"a &lt; b &gt; c &amp; d");

            // Add the entities to the DTD so get_entity can find them
            add_entity(
                dtd,
                c_str(b"lt"),
                XML_INTERNAL_GENERAL_ENTITY as c_int,
                ptr::null(),
                ptr::null(),
                c_str(b"<"),
            );
            add_entity(
                dtd,
                c_str(b"gt"),
                XML_INTERNAL_GENERAL_ENTITY as c_int,
                ptr::null(),
                ptr::null(),
                c_str(b">"),
            );
            add_entity(
                dtd,
                c_str(b"amp"),
                XML_INTERNAL_GENERAL_ENTITY as c_int,
                ptr::null(),
                ptr::null(),
                c_str(b"&"),
            );

            let result = string_decode_entities(doc, input, 0, 0, 0, 0);
            assert!(!result.is_null());
            let expected = c_str(b"a < b > c & d");
            assert_eq!(string::xml_strcmp(result, expected), 0);

            allocator::xmlFreeImpl(result as *mut c_void);

            hash::hash_free(
                (*dtd).entities as *mut hash::HashTable,
                Some(entity_deallocator),
            );
            hash::hash_free(
                (*dtd).pentities as *mut hash::HashTable,
                Some(entity_deallocator),
            );
            allocator::xmlFreeImpl(dtd as *mut c_void);
            allocator::xmlFreeImpl(doc as *mut c_void);
        }
    }

    // ── Security/Limit Tests ────────────────────────────────────────────

    #[test]
    fn test_check_entity_expansion_limit() {
        assert_eq!(check_entity_expansion_limit(100), 0);
        assert_eq!(check_entity_expansion_limit(1_000_000), 0);
        assert_eq!(check_entity_expansion_limit(1_000_001), -1);
    }

    #[test]
    fn test_check_entity_recursion_depth() {
        assert_eq!(check_entity_recursion_depth(10), 0);
        assert_eq!(check_entity_recursion_depth(32), 0);
        assert_eq!(check_entity_recursion_depth(33), -1);
    }

    // ── Entity Content Tests ────────────────────────────────────────────

    #[test]
    /// Tests that `get_entity_content` returns a copy of the entity content.
    ///
    /// # Safety
    ///
    /// - `entity` is a zero-initialized `xmlMallocZero` allocation whose
    ///   `content` field is a fresh `xml_strdup` allocation; `retrieved` is
    ///   asserted non-NULL before the comparison and freed with `xmlFreeImpl`,
    ///   and `entity` is freed with `free_entity` before the test returns.
    fn test_get_entity_content() {
        unsafe {
            let content = c_str(b"entity content");
            let entity =
                allocator::xmlMallocZero(size_of::<_xmlEntity>() as usize) as *mut _xmlEntity;
            assert!(!entity.is_null());
            (*entity).content = string::xml_strdup(content);

            let retrieved = get_entity_content(entity);
            assert!(!retrieved.is_null());
            assert_eq!(string::xml_strcmp(retrieved, content), 0);

            allocator::xmlFreeImpl(retrieved as *mut c_void);
            free_entity(entity);
        }
    }

    #[test]
    /// Tests that `get_entity_content` returns NULL for a NULL entity.
    ///
    /// # Safety
    ///
    /// - `get_entity_content` checks its argument for NULL and returns early
    ///   without dereferencing it, so passing `ptr::null_mut()` is sound.
    fn test_get_entity_content_null() {
        unsafe {
            assert!(get_entity_content(ptr::null_mut()).is_null());
        }
    }
}