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
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
//! Canonical XML implementation (§28, §85 Phase 7).
//!
//! Inclusive and exclusive canonicalization, comments, namespace propagation,
//! attribute ordering, character escaping, subsets, node sets.
//! Must be byte-exact compared to oracle.
//!
//! References:
//! - XML Canonicalization (C14N) — inclusive: <https://www.w3.org/TR/xml-c14n11/>
//! - Exclusive XML Canonicalization (C14N): <https://www.w3.org/2001/10/xml-exc-c14n>
//!
//! # Safety
//!
//! - The unsafe entry points in this module accept raw pointers that must be
//!   valid, correctly typed, and live for the duration of the call: pointers
//!   to `_xmlDoc`, `_xmlNode`, `_xmlNs`, `_xmlBuffer`, and `xmlOutputBuffer`
//!   objects, and NUL-terminated `xmlChar` strings. NULL is permitted only
//!   where an individual function's contract explicitly allows it.
//! - Callers own the pointed-to objects for the duration of each call and
//!   release them afterwards with the matching free function.

#![allow(
    missing_docs,
    non_snake_case,
    non_camel_case_types,
    non_upper_case_globals,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_ptr_alignment,
    clippy::missing_safety_doc,
    clippy::too_many_arguments,
    clippy::too_many_lines,
    clippy::type_complexity
)]

use core::ffi::c_void;
use core::ptr;
use std::collections::HashSet;
use std::os::raw::{c_char, c_int};

use crate::abi::callbacks::xmlC14NIsVisibleCallback;
use crate::abi::structs::*;
use crate::abi::types::xmlElementType::*;
use crate::abi::types::*;
use crate::xml::io;
use crate::xml::tree;

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

/// The XML namespace prefix string "xml".
const XML_XML_PREFIX: &[xmlChar] = b"xml\0";

/// The XML namespace URI string.
const XML_XML_NS_URI: &[xmlChar] = b"http://www.w3.org/XML/1998/namespace\0";

/// The xmlns namespace URI.
const _XMLNS_NS_URI: &[xmlChar] = b"http://www.w3.org/2000/xmlns/\0";

/// The xmlns prefix string.
const _XMLNS_PREFIX: &[xmlChar] = b"xmlns\0";

// ═══════════════════════════════════════════════════════════════════════════════
// C14N Mode
// ═══════════════════════════════════════════════════════════════════════════════

/// Canonicalization mode flags.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// typedef enum {
///     XML_C14N_1_0 = 0,       /* C14N 1.0 (inclusive) */
///     XML_C14N_EXCLUSIVE_1_0 = 1, /* Exclusive C14N 1.0 */
///     XML_C14N_1_1 = 2,       /* C14N 1.1 */
///     XML_C14N_1_0_WITH_COMMENTS = 3, /* C14N 1.0 with comments */
///     XML_C14N_EXCLUSIVE_1_0_WITH_COMMENTS = 4, /* Exclusive with comments */
///     XML_C14N_1_1_WITH_COMMENTS = 5  /* C14N 1.1 with comments */
/// } xmlC14NMode;
/// ```
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum C14nMode {
    XML_C14N_1_0 = 0,
    XML_C14N_EXCLUSIVE_1_0 = 1,
    XML_C14N_1_1 = 2,
    XML_C14N_1_0_WITH_COMMENTS = 3,
    XML_C14N_EXCLUSIVE_1_0_WITH_COMMENTS = 4,
    XML_C14N_1_1_WITH_COMMENTS = 5,
}

impl C14nMode {
    /// Returns true if this mode includes comments in the output.
    const fn with_comments(self) -> bool {
        matches!(
            self,
            C14nMode::XML_C14N_1_0_WITH_COMMENTS
                | C14nMode::XML_C14N_EXCLUSIVE_1_0_WITH_COMMENTS
                | C14nMode::XML_C14N_1_1_WITH_COMMENTS
        )
    }

    /// Returns true if this mode is exclusive.
    const fn is_exclusive(self) -> bool {
        matches!(
            self,
            C14nMode::XML_C14N_EXCLUSIVE_1_0 | C14nMode::XML_C14N_EXCLUSIVE_1_0_WITH_COMMENTS
        )
    }

    /// Returns true if this mode is C14N 1.0 (inclusive).
    const fn is_1_0(self) -> bool {
        matches!(
            self,
            C14nMode::XML_C14N_1_0 | C14nMode::XML_C14N_1_0_WITH_COMMENTS
        )
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// C14N Context
// ═══════════════════════════════════════════════════════════════════════════════

/// Namespace entry for the context stack.
#[allow(dead_code)]
#[derive(Debug, Clone)]
struct NsEntry {
    /// The prefix (NULL for default namespace).
    prefix: *const xmlChar,
    /// The namespace URI (NULL for undeclaration).
    href: *const xmlChar,
    /// Whether this namespace was rendered in the output.
    rendered: bool,
}

/// C14N serialization context.
///
/// Tracks namespace propagation state, visited prefixes, and
/// document-level state during canonicalization.
#[derive(Debug)]
pub struct C14nContext {
    /// The canonicalization mode.
    pub mode: C14nMode,
    /// Stack of in-scope namespace declarations per depth.
    ns_stack: Vec<Vec<NsEntry>>,
    /// Set of inclusive namespace prefixes (for exclusive C14N).
    #[allow(dead_code)]
    inclusive_ns_prefixes: Option<HashSet<String>>,
    /// The document being canonicalized.
    #[allow(dead_code)]
    doc: *mut _xmlDoc,
    /// Per-subtree stack of already-rendered (prefix, href) namespace pairs
    /// (upstream c14n.c `ns_rendered` visible-ns stack). A namespace
    /// binding is rendered at the topmost element where it is in effect;
    /// descendants inherit it without re-declaring, and a fresh subtree
    /// re-renders it (R-000166).
    rendered_stack: Vec<Vec<(Vec<u8>, Vec<u8>)>>,
    /// Position relative to the document element (upstream `ctx->pos`):
    /// 0 = XMLC14N_BEFORE_DOCUMENT_ELEMENT, 1 = INSIDE, 2 = AFTER. PIs and
    /// comments at the document level gain a trailing/leading newline
    /// depending on this position (c14n.c xmlC14NProcessNode).
    pos: u8,
    /// Upstream `ctx->parent_is_doc`: whether the element being processed
    /// is a direct child of the document (the document element).
    parent_is_doc: bool,
    /// Node-set for subset canonicalization (upstream's visibility
    /// callback): when present, only nodes whose pointer is in this set are
    /// visible. NULL (None) means the whole document is visible.
    visible_set: Option<HashSet<*mut c_void>>,
    /// Upstream `xmlC14NExecute`'s visibility callback (c14n.c
    /// `xmlC14NIsVisible`): when present it decides node visibility directly
    /// (R-000176 — the candidate previously mis-implemented xmlC14NExecute's
    /// whole register layout; the callback now lives here).
    visibility_callback: Option<(xmlC14NIsVisibleCallback, *mut c_void)>,
    /// Set when a node that canonical XML cannot process is encountered
    /// (upstream c14n.c xmlC14NErrInvalidNode: XML_ENTITY_REF_NODE /
    /// XML_ENTITY_NODE / XML_NAMESPACE_DECL return -1 "processing node"
    /// — 11.1-Z.1, R-000175). The dump functions then return -1 like the
    /// oracle instead of serializing the reference.
    pub invalid_node: bool,
}

impl C14nContext {
    /// Create a new C14N context.
    ///
    /// # SAFETY
    ///
    /// - `doc` must be a valid pointer to an `_xmlDoc` or NULL.
    pub unsafe fn new(
        doc: *mut _xmlDoc,
        mode: C14nMode,
        inclusive_ns_prefixes: Option<HashSet<String>>,
    ) -> Self {
        Self::with_visible_set(doc, mode, inclusive_ns_prefixes, None)
    }

    /// Create a new C14N context with an explicit visibility set.
    ///
    /// # SAFETY
    ///
    /// - `doc` must be a valid pointer to an `_xmlDoc` or NULL.
    /// - `visible_set` holds node pointers; None means the whole document is
    ///   visible (upstream `is_visible_callback == NULL`).
    pub unsafe fn with_visible_set(
        doc: *mut _xmlDoc,
        mode: C14nMode,
        inclusive_ns_prefixes: Option<HashSet<String>>,
        visible_set: Option<HashSet<*mut c_void>>,
    ) -> Self {
        let mut ctx = C14nContext {
            mode,
            ns_stack: Vec::new(),
            inclusive_ns_prefixes,
            doc,
            rendered_stack: Vec::new(),
            // Upstream xmlC14NNewCtx initialises the walk at the document
            // level, before the document element (c14n.c lines 1773-1774).
            pos: 0, // XMLC14N_BEFORE_DOCUMENT_ELEMENT
            parent_is_doc: true,
            visible_set,
            visibility_callback: None,
            invalid_node: false,
        };
        // Push the initial (document-level) namespace scope, which contains
        // the implicit `xml` namespace.
        let xml_prefix = XML_XML_PREFIX.as_ptr() as *const xmlChar;
        let xml_href = XML_XML_NS_URI.as_ptr() as *const xmlChar;
        ctx.ns_stack.push(vec![NsEntry {
            prefix: xml_prefix,
            href: xml_href,
            rendered: false,
        }]);
        ctx
    }

    /// Create a C14N context driven by an `xmlC14NExecute` visibility
    /// callback (upstream `ctx->isVisibleCallback` / `ctx->userData`).
    ///
    /// # SAFETY
    ///
    /// - `doc` must be a valid pointer to an `_xmlDoc` or NULL.
    /// - `callback`/`user_data` come from the C caller and must stay valid
    ///   for the walk.
    pub unsafe fn with_visibility_callback(
        doc: *mut _xmlDoc,
        mode: C14nMode,
        inclusive_ns_prefixes: Option<HashSet<String>>,
        callback: xmlC14NIsVisibleCallback,
        user_data: *mut c_void,
    ) -> Self {
        let mut ctx = C14nContext::with_visible_set(doc, mode, inclusive_ns_prefixes, None);
        ctx.visibility_callback = Some((callback, user_data));
        ctx
    }

    /// Upstream `xmlC14NIsVisible(ctx, node, parent)`: whether a node is in
    /// the visible node-set. Without a subset every node is visible. When an
    /// `xmlC14NExecute` visibility callback is installed it is consulted
    /// first (upstream `xmlC14NIsVisible` calls `ctx->isVisibleCallback`).
    fn is_visible_node<T>(&self, node: *mut T) -> bool {
        if let Some((cb, user_data)) = self.visibility_callback {
            let parent = if node.is_null() {
                ptr::null_mut()
            } else {
                unsafe { (*(node as *mut _xmlNode)).parent }
            };
            // SAFETY: the C caller provided the callback and data; per the
            // C contract both stay valid for the walk.
            return unsafe { cb(user_data, node as *mut _xmlNode, parent) } != 0;
        }
        match &self.visible_set {
            None => true,
            Some(set) => !node.is_null() && set.contains(&(node as *mut c_void)),
        }
    }

    /// Upstream `xmlC14NIsVisible(ctx, ns, cur)` with the caller's real
    /// namespace declaration: whether the namespace is part of the visible
    /// subset. c14n.c `xmlC14NIsNodeInNodeset` makes a stack copy of the ns,
    /// links `next` to the owning element, and defers to
    /// `xmlXPathNodeSetContains`, whose NAMESPACE_DECL arm matches a
    /// node-set entry by owner element + prefix (xpath.c). The synthesized
    /// namespace-axis nodes in the node-set carry `next` = owner element, so
    /// the equivalent test is: some set entry is a NAMESPACE_DECL whose
    /// `next` is `owner` with an equal (empty-tolerant) prefix.
    ///
    /// With the whole document visible every namespace is visible; with an
    /// `xmlC14NExecute` visibility callback the callback is consulted with
    /// the ns node and the owning element as parent (upstream behaviour).
    fn ns_visible(&self, ns: *const _xmlNs, owner: *mut _xmlNode) -> bool {
        if let Some((cb, user_data)) = self.visibility_callback {
            return unsafe { cb(user_data, ns as *mut _xmlNode, owner) } != 0;
        }
        match &self.visible_set {
            None => true,
            Some(set) => {
                if ns.is_null() || owner.is_null() {
                    return false;
                }
                let prefix = unsafe { (*ns).prefix };
                set.iter().any(|&e| {
                    // Node-set namespace entries are synthesized `_xmlNs`
                    // structs (xmlXPathNodeSetDupNs): type lives at the
                    // xmlNs offset, not the xmlNode offset.
                    let ns2 = e as *const _xmlNs;
                    unsafe {
                        (*ns2).type_ == XML_NAMESPACE_DECL as c_int
                            && (*ns2).next as *mut _xmlNode == owner
                            && c14n_prefix_eq((*ns2).prefix, prefix)
                    }
                })
            }
        }
    }

    /// Enter a new rendered-namespace scope (element open).
    fn push_rendered_scope(&mut self) {
        self.rendered_stack.push(Vec::new());
    }

    /// Exit the current rendered-namespace scope (element close).
    fn pop_rendered_scope(&mut self) {
        self.rendered_stack.pop();
    }

    /// Upstream c14n.c `xmlC14NVisibleNsStackFind` / `xmlExcC14NVisibleNsStackFind`
    /// (R-000166): whether the binding `(prefix, href)` counts as already
    /// rendered. Entries are searched most-recent-first; the first entry whose
    /// prefix matches decides by href equality (not exact-pair matching — a
    /// re-declaration of the same prefix to a different URI is NOT rendered, a
    /// re-declaration back to an older URI IS). `parent_frame_only` restricts
    /// the search to the parent element's rendered frame (upstream's
    /// `nsPrevStart` window, used by the inclusive axis and the exclusive
    /// InclusiveNamespaces list); otherwise the whole stack is searched
    /// (upstream `start = 0`, used by exclusive node/attribute namespaces).
    ///
    /// When no entry has the prefix at all, upstream returns `has_empty_ns`:
    /// the empty namespace counts as already rendered (so `xmlns=""` is only
    /// emitted when a non-empty default was rendered), everything else as not
    /// rendered.
    fn already_rendered(&self, prefix: &[u8], href: &[u8], parent_frame_only: bool) -> bool {
        let len = self.rendered_stack.len();
        let mut idx = len;
        let min = if parent_frame_only {
            len.saturating_sub(2)
        } else {
            0
        };
        while idx > min {
            idx -= 1;
            for (p, h) in self.rendered_stack[idx].iter().rev() {
                if p == prefix {
                    return h == href;
                }
            }
        }
        prefix.is_empty() && href.is_empty()
    }

    /// Mark the (prefix, href) pair in the current scope. Upstream adds every
    /// processed in-scope namespace to `ns_rendered` regardless of whether it
    /// was rendered, so this is called unconditionally for processed bindings.
    fn mark_rendered_pair(&mut self, prefix: &[u8], href: &[u8]) {
        if let Some(top) = self.rendered_stack.last_mut() {
            top.push((prefix.to_vec(), href.to_vec()));
        }
    }

    /// Enter a new namespace scope (depth + 1).
    #[allow(dead_code)]
    fn push_scope(&mut self) {
        // Clone the current top scope as the base for the new scope
        let base = if let Some(top) = self.ns_stack.last() {
            top.clone()
        } else {
            Vec::new()
        };
        self.ns_stack.push(base);
    }

    /// Exit the current namespace scope (depth - 1).
    #[allow(dead_code)]
    fn pop_scope(&mut self) {
        self.ns_stack.pop();
    }

    /// Add a namespace declaration to the current scope.
    ///
    /// # Safety
    ///
    /// - `prefix` must be NULL or a valid pointer to a NUL-terminated `xmlChar`
    ///   string; it is compared against existing entries with `xmlStrEqual`,
    ///   which reads it as a C string.
    /// - `href` is stored in the scope entry without being dereferenced here;
    ///   it must remain valid (or NULL, for an undeclaration) for as long as
    ///   the context keeps the entry.
    #[allow(dead_code)]
    fn add_namespace(&mut self, prefix: *const xmlChar, href: *const xmlChar) {
        if let Some(top) = self.ns_stack.last_mut() {
            // Check if this prefix already exists in the current scope
            if !top
                .iter()
                .any(|e| unsafe { crate::abi::exports_xml2::xmlStrEqual(e.prefix, prefix) != 0 })
            {
                top.push(NsEntry {
                    prefix,
                    href,
                    rendered: false,
                });
            } else {
                // Update the existing entry
                if let Some(existing) = top.iter_mut().find(|e| unsafe {
                    crate::abi::exports_xml2::xmlStrEqual(e.prefix, prefix) != 0
                }) {
                    existing.href = href;
                    existing.rendered = false;
                }
            }
        }
    }

    /// Check if a prefix is in scope.
    ///
    /// # Safety
    ///
    /// - `prefix` must be NULL or a valid pointer to a NUL-terminated `xmlChar`
    ///   string; it is compared with `xmlStrEqual`, which reads it as a C
    ///   string.
    #[allow(dead_code)]
    fn is_prefix_in_scope(&self, prefix: *const xmlChar) -> bool {
        self.ns_stack.iter().rev().any(|scope| {
            scope
                .iter()
                .any(|e| unsafe { crate::abi::exports_xml2::xmlStrEqual(e.prefix, prefix) != 0 })
        })
    }

    /// Get the href for a prefix from the current scope.
    ///
    /// # Safety
    ///
    /// - `prefix` must be NULL or a valid pointer to a NUL-terminated `xmlChar`
    ///   string; it is compared with `xmlStrEqual`, which reads it as a C
    ///   string.
    /// - The returned pointer is the matching scope entry's stored `href`; it
    ///   is valid only while that entry (and the string it points to) remains
    ///   alive in the context.
    #[allow(dead_code)]
    fn get_href_for_prefix(&self, prefix: *const xmlChar) -> *const xmlChar {
        for scope in self.ns_stack.iter().rev() {
            for entry in scope.iter() {
                if unsafe { crate::abi::exports_xml2::xmlStrEqual(entry.prefix, prefix) != 0 } {
                    return entry.href;
                }
            }
        }
        ptr::null()
    }

    /// Check if a prefix is in the inclusive namespace prefixes list.
    ///
    /// # Safety
    ///
    /// - A non-NULL `prefix` must be a valid pointer to a NUL-terminated
    ///   `xmlChar` string; it is converted with `CStr::from_ptr`. A NULL
    ///   `prefix` matches the empty-string entry in the set.
    #[allow(dead_code)]
    fn is_inclusive_prefix(&self, prefix: *const xmlChar) -> bool {
        if let Some(ref set) = self.inclusive_ns_prefixes {
            if prefix.is_null() {
                return set.contains("");
            }
            let prefix_str = unsafe {
                let c_str = core::ffi::CStr::from_ptr(prefix as *const c_char);
                match c_str.to_str() {
                    Ok(s) => s.to_string(),
                    Err(_) => return false,
                }
            };
            set.contains(&prefix_str)
        } else {
            false
        }
    }

    /// Mark a namespace as rendered.
    ///
    /// # Safety
    ///
    /// - `prefix` must be NULL or a valid pointer to a NUL-terminated `xmlChar`
    ///   string; it is compared with `xmlStrEqual`, which reads it as a C
    ///   string.
    #[allow(dead_code)]
    fn mark_rendered(&mut self, prefix: *const xmlChar) {
        for scope in self.ns_stack.iter_mut().rev() {
            for entry in scope.iter_mut() {
                if unsafe { crate::abi::exports_xml2::xmlStrEqual(entry.prefix, prefix) != 0 } {
                    entry.rendered = true;
                    return;
                }
            }
        }
    }

    /// Check if a namespace is already rendered.
    ///
    /// # Safety
    ///
    /// - `prefix` must be NULL or a valid pointer to a NUL-terminated `xmlChar`
    ///   string; it is compared with `xmlStrEqual`, which reads it as a C
    ///   string.
    #[allow(dead_code)]
    fn is_rendered(&self, prefix: *const xmlChar) -> bool {
        for scope in self.ns_stack.iter().rev() {
            for entry in scope.iter() {
                if unsafe { crate::abi::exports_xml2::xmlStrEqual(entry.prefix, prefix) != 0 } {
                    return entry.rendered;
                }
            }
        }
        false
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// C14N Escaping
// ═══════════════════════════════════════════════════════════════════════════════

/// Escape text content per C14N rules.
///
/// Canonical XML requires:
/// - `<` → `&lt;`
/// - `>` → `&gt;`
/// - `&` → `&amp;`
/// - Carriage return `\r` (0x0D) → `&#xD;`
/// - `]]>` → `]]&gt;`
///
/// # SAFETY
///
/// - `buf` must be a valid pointer to a mutable `_xmlBuffer`.
/// - `text` must be a valid pointer to `len` bytes of xmlChar data, or NULL.
unsafe fn c14n_escape_text(buf: *mut _xmlBuffer, text: *const xmlChar, len: c_int) {
    if buf.is_null() || text.is_null() || len <= 0 {
        return;
    }

    let mut i: c_int = 0;
    while i < len {
        let ch = unsafe { *text.add(i as usize) };

        // Check for `]]>` sequence
        if ch == b']'
            && i + 2 < len
            && unsafe { *text.add(i as usize + 1) == b']' }
            && unsafe { *text.add(i as usize + 2) == b'>' }
        {
            // Write `]]&gt;` — escape the `>` that ends `]]>`
            io::buf_add(buf, b"]]" as *const u8, 2); // write `]]`
            io::buf_add(buf, b"&gt;" as *const u8, 4);
            i += 3;
            continue;
        }

        match ch {
            b'<' => {
                io::buf_add(buf, b"&lt;" as *const u8, 4);
            }
            b'>' => {
                io::buf_add(buf, b"&gt;" as *const u8, 4);
            }
            b'&' => {
                io::buf_add(buf, b"&amp;" as *const u8, 5);
            }
            0x0D => {
                // Carriage return: &#xD;
                io::buf_add(buf, b"&#xD;" as *const u8, 5);
            }
            _ => {
                io::buf_add(buf, &ch as *const u8, 1);
            }
        }
        i += 1;
    }
}

/// Escape attribute values per C14N rules.
///
/// Canonical XML requires:
/// - `<` → `&lt;`
/// - `&` → `&amp;`
/// - `"` → `&quot;`
/// - Tab (0x09) → `&#x9;`
/// - Newline (0x0A) → `&#xA;`
/// - Carriage return (0x0D) → `&#xD;`
/// - `]]>` → `]]&gt;`
///
/// # SAFETY
///
/// - `buf` must be a valid pointer to a mutable `_xmlBuffer`.
/// - `text` must be a valid pointer to a null-terminated xmlChar string, or NULL.
unsafe fn c14n_escape_attr(buf: *mut _xmlBuffer, text: *const xmlChar) {
    if buf.is_null() || text.is_null() {
        return;
    }

    let len = tree::xml_strlen(text);
    let mut i: c_int = 0;
    while i < len {
        let ch = unsafe { *text.add(i as usize) };

        // Check for `]]>` sequence
        if ch == b']'
            && i + 2 < len
            && unsafe { *text.add(i as usize + 1) == b']' }
            && unsafe { *text.add(i as usize + 2) == b'>' }
        {
            // Write `]]&gt;`
            io::buf_add(buf, b"]]" as *const u8, 2); // write `]]`
            io::buf_add(buf, b"&gt;" as *const u8, 4);
            i += 3;
            continue;
        }

        match ch {
            b'<' => {
                io::buf_add(buf, b"&lt;" as *const u8, 4);
            }
            b'&' => {
                io::buf_add(buf, b"&amp;" as *const u8, 5);
            }
            b'"' => {
                io::buf_add(buf, b"&quot;" as *const u8, 6);
            }
            0x09 => {
                // Tab
                io::buf_add(buf, b"&#x9;" as *const u8, 5);
            }
            0x0A => {
                // Newline
                io::buf_add(buf, b"&#xA;" as *const u8, 5);
            }
            0x0D => {
                // Carriage return
                io::buf_add(buf, b"&#xD;" as *const u8, 5);
            }
            _ => {
                io::buf_add(buf, &ch as *const u8, 1);
            }
        }
        i += 1;
    }
}

/// Escape comment content per C14N rules: carriage returns are rendered as
/// `&#xD;`, everything else is copied (upstream `xmlC11NNormalizeComment`).
///
/// # SAFETY
///
/// - `buf` must be a valid pointer to a mutable `_xmlBuffer`.
/// - `text` must be a valid pointer to a null-terminated xmlChar string.
unsafe fn c14n_escape_comment(buf: *mut _xmlBuffer, text: *const xmlChar) {
    if buf.is_null() || text.is_null() {
        return;
    }
    let len = tree::xml_strlen(text);
    let mut i: c_int = 0;
    while i < len {
        let ch = unsafe { *text.add(i as usize) };
        if ch == 0x0D {
            io::buf_add(buf, b"&#xD;" as *const u8, 5);
        } else {
            io::buf_add(buf, &ch as *const u8, 1);
        }
        i += 1;
    }
}

/// Escape PI content per C14N rules: carriage returns are rendered as
/// `&#xD;`, everything else is copied (upstream `xmlC11NNormalizePI`).
///
/// # SAFETY
///
/// - `buf` must be a valid pointer to a mutable `_xmlBuffer`.
/// - `text` must be a valid pointer to a null-terminated xmlChar string.
unsafe fn c14n_escape_pi(buf: *mut _xmlBuffer, text: *const xmlChar) {
    if buf.is_null() || text.is_null() {
        return;
    }
    let len = tree::xml_strlen(text);
    let mut i: c_int = 0;
    while i < len {
        let ch = unsafe { *text.add(i as usize) };
        if ch == 0x0D {
            io::buf_add(buf, b"&#xD;" as *const u8, 5);
        } else {
            io::buf_add(buf, &ch as *const u8, 1);
        }
        i += 1;
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Namespace Collection
// ═══════════════════════════════════════════════════════════════════════════════

/// Represents a collected namespace declaration for output.
#[derive(Debug, Clone)]
struct CollectedNs {
    /// The prefix (NULL for default namespace).
    prefix: *const xmlChar,
    /// The namespace URI.
    href: *const xmlChar,
}

/// Whether a namespace is the implicit xml namespace (prefix `xml`, href
/// `http://www.w3.org/XML/1998/namespace`) — upstream c14n.c
/// `xmlC14NIsXmlNs`. Such namespaces are never rendered.
///
/// # SAFETY
///
/// - `ns` must be a valid reference to an `_xmlNs`.
unsafe fn is_xml_ns_ref(ns: &_xmlNs) -> bool {
    !ns.prefix.is_null()
        && !ns.href.is_null()
        && crate::abi::exports_xml2::xmlStrEqual(
            ns.prefix,
            XML_XML_PREFIX.as_ptr() as *const xmlChar,
        ) != 0
        && crate::abi::exports_xml2::xmlStrEqual(ns.href, XML_XML_NS_URI.as_ptr() as *const xmlChar)
            != 0
}

/// Upstream c14n.c `xmlC14NStrEqual`: NULL and the empty string are
/// interchangeable (missing prefixes/hrefs compare as empty).
///
/// # SAFETY
///
/// - Non-NULL arguments must be valid NUL-terminated `xmlChar` strings.
unsafe fn c14n_prefix_eq(a: *const xmlChar, b: *const xmlChar) -> bool {
    if a == b {
        return true;
    }
    let empty = |p: *const xmlChar| p.is_null() || unsafe { *p == 0 };
    if empty(a) || empty(b) {
        return empty(a) && empty(b);
    }
    unsafe { crate::abi::exports_xml2::xmlStrEqual(a, b) != 0 }
}

/// Copy a NUL-terminated xmlChar string into a Vec (empty for NULL).
///
/// # SAFETY
///
/// - `p` must be NULL or a valid NUL-terminated xmlChar string.
unsafe fn cstr_bytes(p: *const xmlChar) -> Vec<u8> {
    if p.is_null() {
        return Vec::new();
    }
    let mut l = 0usize;
    while *p.add(l) != 0 {
        l += 1;
    }
    core::slice::from_raw_parts(p, l).to_vec()
}

/// Upstream `xmlExcC14NProcessNamespacesAxis` per-namespace handling: skip
/// the xml namespace and non-visible namespaces, resolve already-rendered
/// against the visible-ns stack (adding to it when the element is visible,
/// as upstream does), and collect when not already rendered.
/// `parent_frame_only` selects the plain `xmlC14NVisibleNsStackFind` window
/// (upstream `nsPrevStart`) versus the exclusive `start = 0` window.
///
/// # SAFETY
///
/// - `ns` must be NULL or a valid pointer to an `_xmlNs`.
unsafe fn exc_push_ns(
    ctx: &mut C14nContext,
    collected: &mut Vec<CollectedNs>,
    ns: *mut _xmlNs,
    has_empty_ns: &mut bool,
    parent_frame_only: bool,
    mark: bool,
    insert: bool,
    default_seen: bool,
) {
    if ns.is_null() {
        return;
    }
    let ns_ref = unsafe { &*ns };
    if is_xml_ns_ref(ns_ref) {
        return;
    }
    let prefix_bytes = unsafe { cstr_bytes(ns_ref.prefix) };
    let href_bytes = unsafe { cstr_bytes(ns_ref.href) };
    if insert {
        let already = ctx.already_rendered(&prefix_bytes, &href_bytes, parent_frame_only);
        if !already {
            collected.push(CollectedNs {
                prefix: ns_ref.prefix,
                href: ns_ref.href,
            });
        }
    }
    if mark {
        ctx.mark_rendered_pair(&prefix_bytes, &href_bytes);
    }
    if default_seen && ns_ref.prefix.is_null() {
        *has_empty_ns = true;
    }
}

/// Collect visible namespace declarations for a node per inclusive/exclusive rules.
///
/// For **inclusive** C14N:
/// - All effective namespaces in scope for the node are included.
/// - Bindings the parent element already rendered are not re-rendered.
/// - `xmlns=""` undeclarations are emitted when declared.
///
/// For **exclusive** C14N:
/// - Only namespaces visibly utilized by the node and its attributes are
///   included, plus any prefixes in the InclusiveNamespaces PrefixList.
/// - Bindings already rendered by an ancestor are not re-rendered.
///
/// This is a faithful port of upstream c14n.c `xmlC14NProcessNamespacesAxis`
/// (inclusive) and `xmlExcC14NProcessNamespacesAxis` (exclusive), including
/// the `ns_rendered` visible-ns stack semantics and the visibility callback
/// gating for subset canonicalization (R-000166).
///
/// # SAFETY
///
/// - `node` must be a valid pointer to an `_xmlNode` or NULL.
/// - `ctx` must be a valid pointer to a `C14nContext`.
unsafe fn c14n_collect_namespaces(
    node: *mut _xmlNode,
    ctx: &mut C14nContext,
    visible: bool,
) -> Vec<CollectedNs> {
    if node.is_null() {
        return Vec::new();
    }

    let n = unsafe { &*node };
    if n.type_ != XML_ELEMENT_NODE as c_int {
        return Vec::new();
    }

    let mut collected: Vec<CollectedNs> = Vec::new();
    let mut seen_prefixes: Vec<*const xmlChar> = Vec::new();

    if ctx.mode.is_exclusive() {
        // ── Exclusive C14N namespace collection ──
        //
        // Port of upstream xmlExcC14NProcessNamespacesAxis:
        //   1. the InclusiveNamespaces PrefixList is processed first under
        //      Canonical-XML rules (plain find, parent-frame window);
        //   2. the element's own namespace, or — when the element has no
        //      binding — the default namespace in scope (this is what
        //      renders `xmlns=""` undeclarations);
        //   3. attribute namespaces (exclusive find, whole-stack window);
        //   4. the `xmlns=""` fallback for a visibly-utilized empty default.
        let mut has_empty_ns = false;
        let mut has_empty_ns_in_inclusive_list = false;
        let mut has_visibly_utilized_empty_ns = false;

        // 1. InclusiveNamespaces PrefixList.
        let inclusive_prefixes: Vec<String> = ctx
            .inclusive_ns_prefixes
            .clone()
            .map(|s| s.into_iter().collect())
            .unwrap_or_default();
        // Keep owned NUL-terminated copies alive across the loop (the
        // `if`-arm temporary would dangle).
        let prefix_cstrs: Vec<Vec<u8>> = inclusive_prefixes
            .iter()
            .filter(|p| !p.is_empty() && p.as_str() != "#default")
            .map(|p| format!("{}\0", p).into_bytes())
            .collect();
        for inc_prefix_str in &inclusive_prefixes {
            let is_default = inc_prefix_str.is_empty() || inc_prefix_str == "#default";
            if is_default {
                has_empty_ns_in_inclusive_list = true;
            }
            let inc_prefix: *const xmlChar = if is_default {
                ptr::null()
            } else {
                let idx = prefix_cstrs
                    .iter()
                    .position(|c| {
                        let s = String::from_utf8_lossy(&c[..c.len() - 1]);
                        s == *inc_prefix_str
                    })
                    .expect("prefix cstr present");
                prefix_cstrs[idx].as_ptr() as *const xmlChar
            };
            let ns = find_ns_declaration(node, inc_prefix);
            // Upstream processes the prefix list only when the namespace is
            // visible in the subset (`xmlC14NIsVisible(ctx, ns, cur)`).
            let ns_visible = !ns.is_null() && ctx.ns_visible(ns, node);
            exc_push_ns(
                ctx,
                &mut collected,
                ns,
                &mut has_empty_ns,
                true,
                visible,
                ns_visible,
                ns_visible,
            );
        }

        // 2. The node's own namespace; when the element has no binding, the
        //    default namespace in scope (upstream xmlSearchNs(cur->doc, cur,
        //    NULL) with has_visibly_utilized_empty_ns = 1).
        let node_ns = if n.ns.is_null() {
            has_visibly_utilized_empty_ns = true;
            find_ns_declaration(node, ptr::null())
        } else {
            n.ns
        };
        let node_ns_visible = !node_ns.is_null() && ctx.ns_visible(node_ns, node);
        // Upstream: the node namespace is inserted only for a visible element
        // whose namespace is visible (`if(visible && xmlC14NIsVisible(...))`),
        // while the ns_rendered stack add happens for any visible element and
        // has_empty_ns is set whenever the binding is a default namespace.
        exc_push_ns(
            ctx,
            &mut collected,
            node_ns,
            &mut has_empty_ns,
            false,
            visible,
            visible && node_ns_visible,
            true,
        );

        // 3. Attribute namespaces: a visible attribute's namespace is added
        //    regardless of the namespace's own subset membership (upstream
        //    gates on `xmlC14NIsVisible(ctx, attr, cur)`).
        let mut attr = n.properties;
        while !attr.is_null() {
            let a = unsafe { &*attr };
            if !a.ns.is_null() {
                let ans = unsafe { &*a.ns };
                let attr_visible = ctx.is_visible_node(attr);
                if !is_xml_ns_ref(ans) && attr_visible {
                    exc_push_ns(
                        ctx,
                        &mut collected,
                        a.ns,
                        &mut has_empty_ns,
                        false,
                        attr_visible,
                        attr_visible,
                        attr_visible,
                    );
                } else if ans.prefix.is_null() && !ans.href.is_null() && *ans.href == 0 {
                    // Upstream: an attribute bound to an empty default
                    // namespace counts as a visibly utilized empty default
                    // (checked outside the visibility gate).
                    has_visibly_utilized_empty_ns = true;
                }
            }
            attr = a.next;
        }

        // 4. Process xmlns="".
        if visible
            && !has_empty_ns
            && (has_visibly_utilized_empty_ns || has_empty_ns_in_inclusive_list)
        {
            let empty_prefix: Vec<u8> = Vec::new();
            let empty_href: Vec<u8> = Vec::new();
            if !ctx.already_rendered(&empty_prefix, &empty_href, false) {
                collected.push(CollectedNs {
                    prefix: ptr::null(),
                    href: ptr::null(),
                });
            }
        }
    } else {
        // ── Inclusive C14N namespace collection ──
        //
        // Port of upstream xmlC14NProcessNamespacesAxis: walk the element
        // and its ancestors, processing every effective (in-scope) namespace
        // declaration (upstream's `xmlSearchNs(...) == ns` tmp check), skip
        // the xml namespace and non-visible namespaces, and skip bindings
        // already rendered by the parent (plain find, parent-frame window).
        let mut has_empty_ns = false;
        let mut cur: *mut _xmlNode = node;
        while !cur.is_null() {
            let cur_node = unsafe { &*cur };
            // Only element ancestors carry namespace declarations. In
            // particular the document node aliases `nsDef` onto its
            // `oldNs` list (identical struct offsets); upstream filters
            // those out with the `xmlSearchNs(...) == ns` effective-binding
            // test, and skipping non-element ancestors reproduces that.
            if cur_node.type_ != XML_ELEMENT_NODE as c_int {
                cur = cur_node.parent;
                continue;
            }
            let mut ns_def = cur_node.nsDef;
            while !ns_def.is_null() {
                let ns = unsafe { &*ns_def };
                let ns_prefix = ns.prefix;

                // The effective binding for this prefix at `node` is the
                // nearest declaration (upstream tmp == ns check).
                if !seen_prefixes.iter().any(|p| {
                    if ns_prefix.is_null() && p.is_null() {
                        return true;
                    }
                    if ns_prefix.is_null() || p.is_null() {
                        return false;
                    }
                    unsafe { crate::abi::exports_xml2::xmlStrEqual(*p, ns_prefix) != 0 }
                }) {
                    seen_prefixes.push(ns_prefix);
                    // Skip the xml namespace (always implicitly in scope)
                    // and non-visible namespaces (upstream tmp == ns &&
                    // !xmlC14NIsXmlNs && xmlC14NIsVisible(ctx, ns, cur)
                    // gates, where `cur` is the axis-owning element).
                    if is_xml_ns_ref(ns) || !ctx.ns_visible(ns_def, node) {
                        ns_def = ns.next;
                        continue;
                    }
                    let prefix_bytes = unsafe { cstr_bytes(ns_prefix) };
                    let href_bytes = unsafe { cstr_bytes(ns.href) };
                    let is_empty_ns = prefix_bytes.is_empty() && href_bytes.is_empty();
                    let already = ctx.already_rendered(&prefix_bytes, &href_bytes, !is_empty_ns);
                    if visible {
                        ctx.mark_rendered_pair(&prefix_bytes, &href_bytes);
                    }
                    if !already {
                        collected.push(CollectedNs {
                            prefix: ns_prefix,
                            href: ns.href,
                        });
                    }
                    if ns_prefix.is_null() {
                        has_empty_ns = true;
                    }
                }
                ns_def = ns.next;
            }
            cur = cur_node.parent;
        }

        // Upstream lines 625-641: emit xmlns="" when this element's axis
        // has no default namespace node but a non-empty default namespace
        // was already rendered (C14N 1.0 namespace-axis rule).
        if visible && !has_empty_ns {
            let empty_prefix: Vec<u8> = Vec::new();
            let empty_href: Vec<u8> = Vec::new();
            if !ctx.already_rendered(&empty_prefix, &empty_href, false) {
                collected.push(CollectedNs {
                    prefix: ptr::null(),
                    href: ptr::null(),
                });
            }
        }
    }

    // Upstream renders namespaces in lexicographic prefix order (default
    // namespace first) via the sorted xmlList (xmlC14NNsCompare).
    collected.sort_by(|a, b| {
        let (ap, bp) = (a.prefix, b.prefix);
        match (ap.is_null(), bp.is_null()) {
            (true, true) => std::cmp::Ordering::Equal,
            (true, false) => std::cmp::Ordering::Less,
            (false, true) => std::cmp::Ordering::Greater,
            (false, false) => unsafe { crate::abi::exports_xml2::xmlStrcmp(ap, bp) }.cmp(&0),
        }
    });

    collected
}

/// Find the namespace declaration for a given prefix by walking up the ancestor chain.
///
/// # SAFETY
///
/// - `node` must be a valid pointer to an `_xmlNode` or NULL.
unsafe fn find_ns_declaration(node: *mut _xmlNode, prefix: *const xmlChar) -> *mut _xmlNs {
    if node.is_null() {
        return ptr::null_mut();
    }

    let mut cur: *mut _xmlNode = node;
    while !cur.is_null() {
        let cur_node = unsafe { &*cur };
        let mut ns_def = cur_node.nsDef;
        while !ns_def.is_null() {
            let ns = unsafe { &*ns_def };
            let match_found = if prefix.is_null() {
                // Default namespace: prefix should be NULL
                ns.prefix.is_null()
            } else if ns.prefix.is_null() {
                false
            } else {
                unsafe { crate::abi::exports_xml2::xmlStrEqual(ns.prefix, prefix) != 0 }
            };
            if match_found {
                return ns_def;
            }
            ns_def = ns.next;
        }
        cur = cur_node.parent;
    }

    ptr::null_mut()
}

// ═══════════════════════════════════════════════════════════════════════════════
// Namespace Serialization
// ═══════════════════════════════════════════════════════════════════════════════

/// Serialize namespace declarations to the output buffer.
///
/// Outputs namespace declarations in canonical order.
///
/// # SAFETY
///
/// - `buf` must be a valid pointer to a mutable `_xmlBuffer`.
/// - `ns_list` contains raw pointers that must be valid.
unsafe fn c14n_serialize_namespaces(buf: *mut _xmlBuffer, ns_list: &[CollectedNs]) {
    if buf.is_null() || ns_list.is_empty() {
        return;
    }

    for ns in ns_list {
        io::buf_add(buf, b" xmlns" as *const u8, 6);
        if !ns.prefix.is_null() {
            io::buf_ccat(buf, b':');
            io::buf_cat(buf, ns.prefix);
        }
        io::buf_add(buf, b"=\"" as *const u8, 2);
        if !ns.href.is_null() {
            c14n_escape_attr(buf, ns.href);
        }
        io::buf_ccat(buf, b'"');
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Attribute Ordering (Canonical)
// ═══════════════════════════════════════════════════════════════════════════════

/// Compare two attribute pointers for canonical ordering — upstream c14n.c
/// `xmlC14NAttrsCompare`: same-ns-pointer first, then attributes in the
/// default namespace, then lexicographic by namespace URI, then by local
/// name.
///
/// Returns negative, zero, or positive.
///
/// # SAFETY
///
/// - `a` and `b` must be valid pointers to `_xmlAttr`.
unsafe fn compare_attrs(a: *const _xmlAttr, b: *const _xmlAttr) -> std::cmp::Ordering {
    if a == b {
        return std::cmp::Ordering::Equal;
    }
    if a.is_null() {
        return std::cmp::Ordering::Less;
    }
    if b.is_null() {
        return std::cmp::Ordering::Greater;
    }
    let attr_a = unsafe { &*a };
    let attr_b = unsafe { &*b };

    if attr_a.ns == attr_b.ns {
        return unsafe { crate::abi::exports_xml2::xmlStrcmp(attr_a.name, attr_b.name) }.cmp(&0);
    }

    // Attributes in the default namespace are first because the default
    // namespace is not applied to unqualified attributes.
    if attr_a.ns.is_null() {
        return std::cmp::Ordering::Less;
    }
    if attr_b.ns.is_null() {
        return std::cmp::Ordering::Greater;
    }
    if unsafe { (*(attr_a.ns)).prefix.is_null() } {
        return std::cmp::Ordering::Less;
    }
    if unsafe { (*(attr_b.ns)).prefix.is_null() } {
        return std::cmp::Ordering::Greater;
    }

    let ret =
        unsafe { crate::abi::exports_xml2::xmlStrcmp((*(attr_a.ns)).href, (*(attr_b.ns)).href) };
    if ret != 0 {
        return ret.cmp(&0);
    }
    unsafe { crate::abi::exports_xml2::xmlStrcmp(attr_a.name, attr_b.name) }.cmp(&0)
}

/// Whether an attribute is in the xml namespace (upstream `xmlC14NIsXmlAttr`).
///
/// # SAFETY
///
/// - `attr` must be a valid reference to an `_xmlAttr`.
unsafe fn is_xml_attr_ref(attr: &_xmlAttr) -> bool {
    !attr.ns.is_null() && unsafe { is_xml_ns_ref(&*attr.ns) }
}

/// Upstream `xmlC14NFindHiddenParentAttr`: walk up from `cur` while the node
/// is NOT in the visible node-set, returning the nearest xml-namespace
/// attribute with the given local name.
///
/// # SAFETY
///
/// - `cur` must be NULL or a valid pointer to an `_xmlNode`.
unsafe fn find_hidden_parent_attr(
    ctx: &C14nContext,
    cur: *mut _xmlNode,
    name: &[u8],
) -> *mut _xmlAttr {
    let mut cur = cur;
    while !cur.is_null() && !ctx.is_visible_node(cur) {
        let name_c = format!("{}\0", String::from_utf8_lossy(name));
        let res = crate::xml::tree::has_ns_prop(
            cur,
            name_c.as_ptr() as *const xmlChar,
            XML_XML_NS_URI.as_ptr() as *const xmlChar,
        );
        if !res.is_null() {
            return res;
        }
        cur = unsafe { (*cur).parent };
    }
    ptr::null_mut()
}

/// Upstream `xmlC14NFixupBaseAttr`: resolve an xml:base value against the
/// xml:base attributes of the hidden ancestors. Returns the resolved value
/// (owned Vec), or None when the result is empty.
///
/// # SAFETY
///
/// - `base_attr` must be a valid pointer to an `_xmlAttr` with a parent.
unsafe fn fixup_base_attr(ctx: &C14nContext, base_attr: *mut _xmlAttr) -> Option<Vec<u8>> {
    let mut res: Vec<u8> = Vec::new();
    let ba = unsafe { &*base_attr };
    if !ba.children.is_null() {
        let child = unsafe { &*ba.children };
        if !child.content.is_null() {
            let mut l = 0usize;
            while *child.content.add(l) != 0 {
                l += 1;
            }
            res = core::slice::from_raw_parts(child.content, l).to_vec();
        }
    }

    let mut cur = if ba.parent.is_null() {
        ptr::null_mut()
    } else {
        unsafe { (*ba.parent).parent }
    };
    while !cur.is_null() && !ctx.is_visible_node(cur) {
        let tmp_c = b"base\0";
        let attr = crate::xml::tree::has_ns_prop(
            cur,
            tmp_c.as_ptr() as *const xmlChar,
            XML_XML_NS_URI.as_ptr() as *const xmlChar,
        );
        if !attr.is_null() {
            let mut tmp_str: Vec<u8> = Vec::new();
            let a = unsafe { &*attr };
            if !a.children.is_null() {
                let child = unsafe { &*a.children };
                if !child.content.is_null() {
                    let mut l = 0usize;
                    while *child.content.add(l) != 0 {
                        l += 1;
                    }
                    tmp_str = core::slice::from_raw_parts(child.content, l).to_vec();
                }
            }
            // Force going "up" when the base ends in '.' or '..' (upstream
            // appends '/').
            let tl = tmp_str.len();
            if tl > 1 && tmp_str[tl - 2] == b'.' {
                tmp_str.push(b'/');
            }
            // Build the resolved URI.
            let uri_c = format!("{}\0", String::from_utf8_lossy(&res));
            let base_c = format!("{}\0", String::from_utf8_lossy(&tmp_str));
            let built = crate::abi::exports_uri::xmlBuildURI(
                uri_c.as_ptr() as *const c_char,
                base_c.as_ptr() as *const c_char,
            );
            if built.is_null() {
                return None;
            }
            let mut l = 0usize;
            while *built.add(l) != 0 {
                l += 1;
            }
            res = core::slice::from_raw_parts(built, l).to_vec();
            crate::abi::allocator::xmlFreeImpl(built as *mut c_void);
        }
        cur = unsafe { (*cur).parent };
    }

    if res.is_empty() {
        None
    } else {
        Some(res)
    }
}

/// One attribute to serialize: the attribute node plus an optional value
/// override (used for the fixed-up xml:base of hidden ancestors).
struct AttrOut {
    attr: *mut _xmlAttr,
    base_value: Option<Vec<u8>>,
}

/// Serialize the attribute axis in canonical order — upstream c14n.c
/// `xmlC14NProcessAttrsAxis`. The `xmlns:*` declarations are NOT included
/// here; they are handled by `c14n_serialize_namespaces`.
///
/// Mode-specific subset rules:
/// - C14N 1.0: visible attributes, plus xml-namespace attributes imported
///   from hidden ancestors of a visible orphan element.
/// - Exclusive: visible attributes only.
/// - C14N 1.1: visible attributes, plus the simple inheritable xml:lang /
///   xml:space from hidden ancestors and a fixed-up xml:base.
///
/// # SAFETY
///
/// - `node` must be a valid pointer to an `_xmlNode` or NULL.
/// - `ctx` must be a valid pointer to a `C14nContext`.
/// - `buf` must be a valid pointer to a mutable `_xmlBuffer`.
unsafe fn c14n_serialize_attributes(
    node: *mut _xmlNode,
    ctx: &mut C14nContext,
    buf: *mut _xmlBuffer,
    element_visible: bool,
) {
    if node.is_null() || buf.is_null() {
        return;
    }

    let n = unsafe { &*node };
    if n.type_ != XML_ELEMENT_NODE as c_int {
        return;
    }

    // Collect attributes into a vector for canonical sorting.
    let mut list: Vec<AttrOut> = Vec::new();
    let mut cur_attr = n.properties;

    if ctx.mode.is_1_0() {
        // C14N 1.0: all visible attributes of the element.
        while !cur_attr.is_null() {
            if ctx.is_visible_node(cur_attr) {
                list.push(AttrOut {
                    attr: cur_attr,
                    base_value: None,
                });
            }
            cur_attr = unsafe { (*cur_attr).next };
        }

        // Orphan handling: a visible element whose parent is not in the
        // node-set imports the xml-namespace attributes of its hidden
        // ancestors (c14n.c lines 1165-1187).
        if element_visible && !n.parent.is_null() && !ctx.is_visible_node(n.parent) {
            let mut tmp = n.parent;
            while !tmp.is_null() && unsafe { (*tmp).type_ == XML_ELEMENT_NODE as c_int } {
                let mut attr = unsafe { (*tmp).properties };
                while !attr.is_null() {
                    let a = unsafe { &*attr };
                    if unsafe { is_xml_attr_ref(a) } {
                        let dup = list.iter().any(|o| unsafe {
                            compare_attrs(o.attr, attr) == std::cmp::Ordering::Equal
                        });
                        if !dup {
                            list.push(AttrOut {
                                attr,
                                base_value: None,
                            });
                        }
                    }
                    attr = unsafe { (*attr).next };
                }
                tmp = unsafe { (*tmp).parent };
            }
        }
    } else if ctx.mode.is_exclusive() {
        // Exclusive: visible attributes only; xml attributes of orphan
        // nodes are not imported.
        while !cur_attr.is_null() {
            if ctx.is_visible_node(cur_attr) {
                list.push(AttrOut {
                    attr: cur_attr,
                    base_value: None,
                });
            }
            cur_attr = unsafe { (*cur_attr).next };
        }
    } else {
        // C14N 1.1: visible attributes, with xml:lang / xml:space /
        // xml:base handled specially (c14n.c lines 1238-1311).
        let mut xml_lang_attr: *mut _xmlAttr = ptr::null_mut();
        let mut xml_space_attr: *mut _xmlAttr = ptr::null_mut();
        let mut xml_base_attr: *mut _xmlAttr = ptr::null_mut();

        while !cur_attr.is_null() {
            let a = unsafe { &*cur_attr };
            if !element_visible || !unsafe { is_xml_attr_ref(a) } {
                if ctx.is_visible_node(cur_attr) {
                    list.push(AttrOut {
                        attr: cur_attr,
                        base_value: None,
                    });
                }
            } else {
                // Simple inheritable attributes and xml:base of the element
                // itself are collected (visible or not); other xml-namespace
                // attributes are ordinary visible attributes.
                let mut matched = false;
                if xml_lang_attr.is_null()
                    && !a.name.is_null()
                    && unsafe {
                        crate::abi::exports_xml2::xmlStrEqual(
                            a.name,
                            c"lang".as_ptr() as *const xmlChar,
                        ) != 0
                    }
                {
                    xml_lang_attr = cur_attr;
                    matched = true;
                }
                if !matched
                    && xml_space_attr.is_null()
                    && !a.name.is_null()
                    && unsafe {
                        crate::abi::exports_xml2::xmlStrEqual(
                            a.name,
                            c"space".as_ptr() as *const xmlChar,
                        ) != 0
                    }
                {
                    xml_space_attr = cur_attr;
                    matched = true;
                }
                if !matched
                    && xml_base_attr.is_null()
                    && !a.name.is_null()
                    && unsafe {
                        crate::abi::exports_xml2::xmlStrEqual(
                            a.name,
                            c"base".as_ptr() as *const xmlChar,
                        ) != 0
                    }
                {
                    xml_base_attr = cur_attr;
                    matched = true;
                }
                if !matched && ctx.is_visible_node(cur_attr) {
                    list.push(AttrOut {
                        attr: cur_attr,
                        base_value: None,
                    });
                }
            }
            cur_attr = unsafe { (*cur_attr).next };
        }

        if element_visible {
            // Simple inheritable attributes — nearest hidden ancestor.
            if xml_lang_attr.is_null() {
                xml_lang_attr = find_hidden_parent_attr(ctx, n.parent, b"lang");
            }
            if !xml_lang_attr.is_null() {
                list.push(AttrOut {
                    attr: xml_lang_attr,
                    base_value: None,
                });
            }
            if xml_space_attr.is_null() {
                xml_space_attr = find_hidden_parent_attr(ctx, n.parent, b"space");
            }
            if !xml_space_attr.is_null() {
                list.push(AttrOut {
                    attr: xml_space_attr,
                    base_value: None,
                });
            }
            // xml:base — resolved against the hidden ancestors.
            if xml_base_attr.is_null() {
                xml_base_attr = find_hidden_parent_attr(ctx, n.parent, b"base");
            }
            if !xml_base_attr.is_null() {
                if let Some(resolved) = fixup_base_attr(ctx, xml_base_attr) {
                    list.push(AttrOut {
                        attr: xml_base_attr,
                        base_value: Some(resolved),
                    });
                }
            }
        }
    }

    // Sort attributes canonically
    list.sort_by(|a, b| unsafe { compare_attrs(a.attr, b.attr) });

    // Serialize each attribute
    for out in &list {
        let a = unsafe { &*out.attr };

        io::buf_ccat(buf, b' ');

        // Write attribute name with optional namespace prefix
        if !a.ns.is_null() {
            let ans = unsafe { &*a.ns };
            if !ans.prefix.is_null() {
                io::buf_cat(buf, ans.prefix);
                io::buf_ccat(buf, b':');
            }
        }
        if !a.name.is_null() {
            io::buf_cat(buf, a.name);
        }

        io::buf_add(buf, b"=\"" as *const u8, 2);

        // Attribute value: the fixed-up base value, or the child text node.
        if let Some(ref value) = out.base_value {
            if !value.is_empty() {
                let value_c = format!("{}\0", String::from_utf8_lossy(value));
                c14n_escape_attr(buf, value_c.as_ptr() as *const xmlChar);
            }
        } else if !a.children.is_null() {
            let child = unsafe { &*a.children };
            if child.type_ == XML_TEXT_NODE as c_int && !child.content.is_null() {
                c14n_escape_attr(buf, child.content);
            }
        }

        io::buf_ccat(buf, b'"');
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Node Serialization (Canonical)
// ═══════════════════════════════════════════════════════════════════════════════

/// Serialize a single node in canonical form.
///
/// This is the core recursive canonical serialization function.
///
/// # SAFETY
///
/// - `node` must be a valid pointer to an `_xmlNode` or NULL.
/// - `ctx` must be a valid pointer to a `C14nContext`.
/// - `buf` must be a valid pointer to a mutable `_xmlBuffer`.
unsafe fn c14n_serialize_node(node: *mut _xmlNode, ctx: &mut C14nContext, buf: *mut _xmlBuffer) {
    if node.is_null() || buf.is_null() {
        return;
    }

    let n = unsafe { &*node };

    match n.type_ {
        t if t == XML_ELEMENT_NODE as c_int => {
            c14n_serialize_element(node, ctx, buf);
        }
        t if t == XML_TEXT_NODE as c_int => {
            // UPSTREAM-PARITY (c14n.c xmlC14NProcessNode): text nodes are
            // rendered only when they are in the visible node-set.
            if ctx.is_visible_node(node) && !n.content.is_null() {
                c14n_escape_text(buf, n.content, tree::xml_strlen(n.content));
            }
        }
        t if t == XML_CDATA_SECTION_NODE as c_int => {
            // C14N converts CDATA sections to text
            if ctx.is_visible_node(node) && !n.content.is_null() {
                c14n_escape_text(buf, n.content, tree::xml_strlen(n.content));
            }
        }
        t if t == XML_COMMENT_NODE as c_int => {
            if ctx.is_visible_node(node) && ctx.mode.with_comments() {
                // UPSTREAM-PARITY (c14n.c xmlC14NProcessNode): comment
                // children of the root node get a leading newline when they
                // follow the document element and a trailing newline when
                // they precede it.
                if ctx.pos == 2 {
                    io::buf_ccat(buf, b'\n');
                }
                io::buf_add(buf, b"<!--" as *const u8, 4);
                if !n.content.is_null() {
                    c14n_escape_comment(buf, n.content);
                }
                io::buf_add(buf, b"-->" as *const u8, 3);
                if ctx.pos == 0 {
                    io::buf_ccat(buf, b'\n');
                }
            }
        }
        t if t == XML_PI_NODE as c_int => {
            // UPSTREAM-PARITY (c14n.c xmlC14NProcessNode): PI children of
            // the root node get a leading newline when they follow the
            // document element and a trailing newline when they precede it.
            if ctx.is_visible_node(node) {
                if ctx.pos == 2 {
                    io::buf_ccat(buf, b'\n');
                }
                io::buf_add(buf, b"<?" as *const u8, 2);
                if !n.name.is_null() {
                    io::buf_cat(buf, n.name);
                }
                if !n.content.is_null() && unsafe { *n.content != 0 } {
                    io::buf_ccat(buf, b' ');
                    c14n_escape_pi(buf, n.content);
                }
                io::buf_add(buf, b"?>" as *const u8, 2);
                if ctx.pos == 0 {
                    io::buf_ccat(buf, b'\n');
                }
            }
        }
        t if t == XML_DOCUMENT_NODE as c_int || t == XML_HTML_DOCUMENT_NODE as c_int => {
            // Serialize children of the document node (skip XML declaration)
            ctx.pos = 0; // XMLC14N_BEFORE_DOCUMENT_ELEMENT
            ctx.parent_is_doc = true;
            let mut child = n.children;
            while !child.is_null() {
                c14n_serialize_node(child, ctx, buf);
                child = unsafe { (*child).next };
            }
        }
        t if t == XML_DTD_NODE as c_int || t == XML_DOCUMENT_TYPE_NODE as c_int => {
            // Skip DTD nodes in C14N output
        }
        t if t == XML_ENTITY_REF_NODE as c_int => {
            // UPSTREAM-PARITY (c14n.c xmlC14NProcessNode): an unexpanded
            // entity reference is NOT serialized — upstream fails the
            // canonicalization with xmlC14NErrInvalidNode ("processing
            // node", return -1). The candidate mirrors the -1 (R-000175).
            ctx.invalid_node = true;
        }
        _ => {
            // For unknown types, write content if present
            if !n.content.is_null() {
                c14n_escape_text(buf, n.content, tree::xml_strlen(n.content));
            }
        }
    }
}

/// Check whether any element in the document declares a relative
/// (scheme-less) namespace URI (upstream c14n.c
/// `xmlC14NCheckForRelativeNamespaces`).
///
/// # SAFETY
///
/// - `doc` must be a valid pointer to an `_xmlDoc`.
unsafe fn doc_has_relative_ns(doc: *mut _xmlDoc) -> bool {
    unsafe {
        let mut stack: Vec<*mut _xmlNode> = Vec::new();
        if !(*doc).children.is_null() {
            stack.push((*doc).children);
        }
        while let Some(node) = stack.pop() {
            let mut cur = node;
            while !cur.is_null() {
                let t = (*cur).type_;
                if t == crate::abi::types::xmlElementType::XML_ELEMENT_NODE as c_int {
                    let mut ns_def = (*cur).nsDef;
                    while !ns_def.is_null() {
                        let href = (*ns_def).href;
                        if !href.is_null() && *href != 0 && !has_uri_scheme_bytes(href) {
                            return true;
                        }
                        ns_def = (*ns_def).next;
                    }
                    if !(*cur).children.is_null() {
                        stack.push((*cur).children);
                    }
                }
                cur = (*cur).next;
            }
        }
        false
    }
}

/// Whether a NUL-terminated URI has a scheme (`scheme:` prefix) — upstream
/// xmlParseURISafe's `scheme == NULL` check.
///
/// # Safety
///
/// - `uri` must be a valid pointer to a NUL-terminated `xmlChar` buffer; the
///   scan reads one byte at a time until the terminating NUL and never past it.
const unsafe fn has_uri_scheme_bytes(uri: *const xmlChar) -> bool {
    unsafe {
        let mut i: usize = 0;
        while *uri.add(i) != 0 {
            let c = *uri.add(i);
            if c == b':' {
                return i > 0;
            }
            if i == 0 {
                if !c.is_ascii_alphabetic() {
                    return false;
                }
            } else if !(c.is_ascii_alphanumeric() || c == b'+' || c == b'-' || c == b'.') {
                return false;
            }
            i += 1;
        }
        false
    }
}

/// Serialize an element node in canonical form.
///
/// This handles namespace collection, attribute ordering, and recursive
/// child serialization.
///
/// # SAFETY
///
/// - `node` must be a valid pointer to an `_xmlNode` or NULL.
/// - `ctx` must be a valid pointer to a `C14nContext`.
/// - `buf` must be a valid pointer to a mutable `_xmlBuffer`.
unsafe fn c14n_serialize_element(node: *mut _xmlNode, ctx: &mut C14nContext, buf: *mut _xmlBuffer) {
    if node.is_null() || buf.is_null() {
        return;
    }

    let n = unsafe { &*node };
    let visible = ctx.is_visible_node(node);

    // UPSTREAM-PARITY (c14n.c xmlC14NProcessElementNode): the document
    // element moves the position state into the element; it is restored to
    // AFTER_DOCUMENT_ELEMENT when the element closes (c14n.c lines
    // 1420-1426 and 1470-1474). Both transitions happen only when the
    // element is visible.
    let parent_is_doc = ctx.parent_is_doc;
    if visible && parent_is_doc {
        ctx.parent_is_doc = false;
        ctx.pos = 1; // XMLC14N_INSIDE_DOCUMENT_ELEMENT
    }

    // Push a new namespace scope for this element
    ctx.push_scope();
    // Push a rendered-namespace scope (upstream ns_rendered stack).
    ctx.push_rendered_scope();

    // Collect namespaces for this element
    let ns_list = c14n_collect_namespaces(node, ctx, visible);

    if visible {
        // Open element: `<`
        io::buf_ccat(buf, b'<');

        // Write element name with optional namespace prefix
        if !n.ns.is_null() {
            let ns = unsafe { &*n.ns };
            if !ns.prefix.is_null() {
                io::buf_cat(buf, ns.prefix);
                io::buf_ccat(buf, b':');
            }
        }
        if !n.name.is_null() {
            io::buf_cat(buf, n.name);
        }

        // Write namespace declarations
        c14n_serialize_namespaces(buf, &ns_list);

        // Write attributes in canonical order
        c14n_serialize_attributes(node, ctx, buf, visible);

        if n.children.is_null() {
            // UPSTREAM-PARITY (c14n.c xmlC14NProcessElementNode): canonical
            // form expands empty elements (`<name></name>`, never `<name/>`).
            io::buf_ccat(buf, b'>');
            io::buf_add(buf, b"</" as *const u8, 2);
            if !n.ns.is_null() {
                let ns = unsafe { &*n.ns };
                if !ns.prefix.is_null() {
                    io::buf_cat(buf, ns.prefix);
                    io::buf_ccat(buf, b':');
                }
            }
            if !n.name.is_null() {
                io::buf_cat(buf, n.name);
            }
            io::buf_ccat(buf, b'>');
        } else {
            io::buf_ccat(buf, b'>');

            // Serialize children
            let mut child = n.children;
            while !child.is_null() {
                c14n_serialize_node(child, ctx, buf);
                child = unsafe { (*child).next };
            }

            // Close element: `</name>`
            io::buf_add(buf, b"</" as *const u8, 2);
            if !n.ns.is_null() {
                let ns = unsafe { &*n.ns };
                if !ns.prefix.is_null() {
                    io::buf_cat(buf, ns.prefix);
                    io::buf_ccat(buf, b':');
                }
            }
            if !n.name.is_null() {
                io::buf_cat(buf, n.name);
            }
            io::buf_ccat(buf, b'>');
        }
    } else {
        // Not in the node-set: still process the namespace and attribute
        // axes (for the ns_rendered stack semantics) and the children, but
        // write nothing (c14n.c xmlC14NProcessElementNode).
        let mut child = n.children;
        while !child.is_null() {
            c14n_serialize_node(child, ctx, buf);
            child = unsafe { (*child).next };
        }
    }

    // Pop namespace scope
    ctx.pop_rendered_scope();
    ctx.pop_scope();

    // UPSTREAM-PARITY: after the document element closes, following root
    // children are in the AFTER_DOCUMENT_ELEMENT position.
    if visible && parent_is_doc {
        ctx.parent_is_doc = true;
        ctx.pos = 2; // XMLC14N_AFTER_DOCUMENT_ELEMENT
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Public API — Document-level canonicalization
// ═══════════════════════════════════════════════════════════════════════════════

/// Canonicalize a document (or a subset of nodes) to an xmlBuffer.
///
/// If `nodes` is non-NULL, it is a NULL-terminated array of node pointers
/// that form the subset to canonicalize. If NULL, the entire document is
/// canonicalized.
///
/// # SAFETY
///
/// - `doc` must be a valid pointer to an `_xmlDoc` or NULL.
/// - `nodes` may be NULL (meaning the entire document) or a NULL-terminated
///   array of `_xmlNode` pointers.
/// - `buf` must be a valid pointer to a mutable `_xmlBuffer`.
/// - `inclusive_ns_prefixes` is a comma-separated list of prefixes or NULL.
pub unsafe fn c14n_doc_dump_memory(
    doc: *mut _xmlDoc,
    nodes: *mut *mut _xmlNode,
    mode: C14nMode,
    inclusive_ns_prefixes: *const xmlChar,
    with_comments: c_int,
    result: *mut *mut xmlChar,
) -> c_int {
    if doc.is_null() || result.is_null() {
        return -1;
    }

    // Determine the effective mode, considering with_comments flag
    let effective_mode = if with_comments != 0 {
        match mode {
            C14nMode::XML_C14N_1_0 => C14nMode::XML_C14N_1_0_WITH_COMMENTS,
            C14nMode::XML_C14N_EXCLUSIVE_1_0 => C14nMode::XML_C14N_EXCLUSIVE_1_0_WITH_COMMENTS,
            C14nMode::XML_C14N_1_1 => C14nMode::XML_C14N_1_1_WITH_COMMENTS,
            _ => mode,
        }
    } else {
        mode
    };

    // Parse inclusive namespace prefixes
    let inclusive_set = parse_inclusive_prefixes(inclusive_ns_prefixes);

    // Upstream xmlC14NDocDumpMemory passes an xmlNodeSet as the visibility
    // callback data: nodes in the set are visible, everything else is
    // processed but not rendered (c14n.c xmlC14NIsNodeInNodeset).
    let visible_set = build_visible_set(nodes);

    let mut ctx = C14nContext::with_visible_set(doc, effective_mode, inclusive_set, visible_set);

    // UPSTREAM-PARITY (c14n.c xmlC14NCheckForRelativeNamespaces, called from
    // xmlC14NProcessElementNode in BOTH inclusive and exclusive modes):
    // canonicalization refuses documents that declare relative
    // (scheme-less) namespace URIs — "Failed to canonicalize" and a
    // negative return (R-000166).
    if doc_has_relative_ns(doc) {
        return -1;
    }

    // Create output buffer
    let buf = io::buf_create(-1);
    if buf.is_null() {
        return -1;
    }

    // Walk the document; visibility decides what is rendered (upstream
    // xmlC14NProcessNodeList over doc->children).
    c14n_walk_document(doc, &mut ctx, buf);

    // UPSTREAM-PARITY (c14n.c xmlC14NProcessNode): an unexpanded entity
    // reference (or an entity/namespace-decl node) fails the whole
    // canonicalization with -1 (R-000175).
    if ctx.invalid_node {
        io::buf_free(buf);
        return -1;
    }

    // Extract result string
    let content = io::buf_content(buf);
    let len = io::buf_length(buf);
    if content.is_null() || len < 0 {
        io::buf_free(buf);
        return -1;
    }

    // Duplicate the content for the caller
    let result_str = crate::abi::exports_xml2::xmlStrdup(content);
    io::buf_free(buf);

    if result_str.is_null() {
        return -1;
    }

    unsafe {
        *result = result_str;
    }

    len
}

/// Canonicalize a document to an output buffer via callback.
///
/// # SAFETY
///
/// - `doc` must be a valid pointer to an `_xmlDoc` or NULL.
/// - `callback` must be a valid function pointer.
/// - `inclusive_ns_prefixes` is a comma-separated list of prefixes or NULL.
pub unsafe fn c14n_execute(
    doc: *mut _xmlDoc,
    mode: C14nMode,
    inclusive_ns_prefixes: *const xmlChar,
    with_comments: c_int,
    callback: Option<
        unsafe extern "C" fn(ctx: *mut c_void, data: *const c_char, len: c_int) -> c_int,
    >,
    callback_data: *mut c_void,
) -> c_int {
    if doc.is_null() || callback.is_none() {
        return -1;
    }

    let callback = callback.unwrap();

    // Determine effective mode
    let effective_mode = if with_comments != 0 {
        match mode {
            C14nMode::XML_C14N_1_0 => C14nMode::XML_C14N_1_0_WITH_COMMENTS,
            C14nMode::XML_C14N_EXCLUSIVE_1_0 => C14nMode::XML_C14N_EXCLUSIVE_1_0_WITH_COMMENTS,
            C14nMode::XML_C14N_1_1 => C14nMode::XML_C14N_1_1_WITH_COMMENTS,
            _ => mode,
        }
    } else {
        mode
    };

    // Parse inclusive namespace prefixes
    let inclusive_set = parse_inclusive_prefixes(inclusive_ns_prefixes);

    // UPSTREAM-PARITY: xmlC14NExecute carries the visibility callback
    // provided by the caller; the candidate's built-in wrappers always
    // canonicalize the whole document (visible_set = None).
    let mut ctx = C14nContext::new(doc, effective_mode, inclusive_set);

    // UPSTREAM-PARITY (c14n.c xmlC14NCheckForRelativeNamespaces): the
    // relative-namespace-URI check applies in every mode, inclusive and
    // exclusive (R-000166).
    if doc_has_relative_ns(doc) {
        return -1;
    }

    // Create output buffer
    let buf = io::buf_create(-1);
    if buf.is_null() {
        return -1;
    }

    // Walk the document (upstream xmlC14NProcessNodeList).
    c14n_walk_document(doc, &mut ctx, buf);

    // UPSTREAM-PARITY (c14n.c xmlC14NProcessNode): an unexpanded entity
    // reference fails the canonicalization with -1 (R-000175).
    if ctx.invalid_node {
        io::buf_free(buf);
        return -1;
    }

    // Call the callback with the result
    let content = io::buf_content(buf);
    let len = io::buf_length(buf);
    if content.is_null() || len < 0 {
        io::buf_free(buf);
        return -1;
    }

    let ret = unsafe { callback(callback_data, content as *const c_char, len) };

    io::buf_free(buf);
    ret
}

/// Canonicalize a document to an `xmlOutputBuffer` with an optional
/// `xmlC14NExecute` visibility callback (upstream c14n.c `xmlC14NExecute`:
/// `is_visible_callback == NULL` → the whole document is visible;
/// otherwise the callback decides node inclusion; the output goes to
/// `buf` via `xmlOutputBufferWrite`).
///
/// # SAFETY
///
/// - `doc`, `output` must be valid pointers (or NULL where the upstream C
///   contract allows), obtained from the matching constructor/owner and not
///   yet freed.
/// - `callback`/`user_data` must stay valid for the walk when non-NULL.
pub unsafe fn c14n_execute_visibility(
    doc: *mut _xmlDoc,
    mode: C14nMode,
    inclusive_ns_prefixes: *const xmlChar,
    with_comments: c_int,
    is_visible_callback: Option<crate::abi::callbacks::xmlC14NIsVisibleCallback>,
    user_data: *mut c_void,
    output: *mut _xmlOutputBuffer,
) -> c_int {
    if doc.is_null() || output.is_null() {
        return -1;
    }

    // Determine effective mode
    let effective_mode = if with_comments != 0 {
        match mode {
            C14nMode::XML_C14N_1_0 => C14nMode::XML_C14N_1_0_WITH_COMMENTS,
            C14nMode::XML_C14N_EXCLUSIVE_1_0 => C14nMode::XML_C14N_EXCLUSIVE_1_0_WITH_COMMENTS,
            C14nMode::XML_C14N_1_1 => C14nMode::XML_C14N_1_1_WITH_COMMENTS,
            _ => mode,
        }
    } else {
        mode
    };

    // Parse inclusive namespace prefixes
    let inclusive_set = parse_inclusive_prefixes(inclusive_ns_prefixes);

    let mut ctx = match is_visible_callback {
        Some(cb) => {
            C14nContext::with_visibility_callback(doc, effective_mode, inclusive_set, cb, user_data)
        }
        None => C14nContext::new(doc, effective_mode, inclusive_set),
    };

    // UPSTREAM-PARITY (c14n.c xmlC14NCheckForRelativeNamespaces): the
    // relative-namespace-URI check applies in every mode, inclusive and
    // exclusive (R-000166).
    if doc_has_relative_ns(doc) {
        return -1;
    }

    // Create output buffer
    let buf = io::buf_create(-1);
    if buf.is_null() {
        return -1;
    }

    // Walk the document (upstream xmlC14NProcessNodeList).
    c14n_walk_document(doc, &mut ctx, buf);

    // UPSTREAM-PARITY (c14n.c xmlC14NProcessNode): an unexpanded entity
    // reference fails the canonicalization with -1 (R-000175).
    if ctx.invalid_node {
        io::buf_free(buf);
        return -1;
    }

    let content = io::buf_content(buf);
    let len = io::buf_length(buf);
    if content.is_null() || len < 0 {
        io::buf_free(buf);
        return -1;
    }

    // Write the canonical output to the caller's xmlOutputBuffer (upstream
    // xmlC14NProcessNode writes directly via xmlOutputBufferWrite).
    let ret = crate::abi::exports_xml2::xmlOutputBufferWrite(output, len, content as *const c_char);

    io::buf_free(buf);
    ret
}

/// Canonicalize a document and save to an output buffer.
///
/// # SAFETY
///
/// - `doc` must be a valid pointer to an `_xmlDoc` or NULL.
/// - `output` must be a valid pointer to an `_xmlOutputBuffer` or NULL.
/// - `inclusive_ns_prefixes` is a comma-separated list of prefixes or NULL.
pub unsafe fn c14n_doc_save_to(
    doc: *mut _xmlDoc,
    nodes: *mut *mut _xmlNode,
    mode: C14nMode,
    inclusive_ns_prefixes: *const xmlChar,
    with_comments: c_int,
    output: *mut _xmlOutputBuffer,
) -> c_int {
    if doc.is_null() || output.is_null() {
        return -1;
    }

    // Determine effective mode
    let effective_mode = if with_comments != 0 {
        match mode {
            C14nMode::XML_C14N_1_0 => C14nMode::XML_C14N_1_0_WITH_COMMENTS,
            C14nMode::XML_C14N_EXCLUSIVE_1_0 => C14nMode::XML_C14N_EXCLUSIVE_1_0_WITH_COMMENTS,
            C14nMode::XML_C14N_1_1 => C14nMode::XML_C14N_1_1_WITH_COMMENTS,
            _ => mode,
        }
    } else {
        mode
    };

    // Parse inclusive namespace prefixes
    let inclusive_set = parse_inclusive_prefixes(inclusive_ns_prefixes);

    // Upstream xmlC14NDocSaveTo passes the xmlNodeSet as the visibility
    // callback data (c14n.c xmlC14NIsNodeInNodeset).
    let visible_set = build_visible_set(nodes);

    let mut ctx = C14nContext::with_visible_set(doc, effective_mode, inclusive_set, visible_set);

    // UPSTREAM-PARITY (c14n.c xmlC14NCheckForRelativeNamespaces): the
    // relative-namespace-URI check applies in every mode, inclusive and
    // exclusive (R-000166).
    if doc_has_relative_ns(doc) {
        return -1;
    }

    // Create buffer for serialization
    let buf = io::buf_create(-1);
    if buf.is_null() {
        return -1;
    }

    // Walk the document; visibility decides what is rendered (upstream
    // xmlC14NProcessNodeList).
    c14n_walk_document(doc, &mut ctx, buf);

    // UPSTREAM-PARITY (c14n.c xmlC14NProcessNode): an unexpanded entity
    // reference fails the canonicalization with -1 (R-000175).
    if ctx.invalid_node {
        io::buf_free(buf);
        return -1;
    }

    // Write to output buffer
    let content = io::buf_content(buf);
    let len = io::buf_length(buf);
    if content.is_null() || len < 0 {
        io::buf_free(buf);
        return -1;
    }

    let written = io::output_buffer_write(output, len, content as *const c_char);
    io::buf_free(buf);

    // Flush the output buffer to ensure data reaches the underlying target
    let flush_ret = io::output_buffer_flush(output);
    if flush_ret < 0 {
        return written;
    }

    written
}

// ═══════════════════════════════════════════════════════════════════════════════
// Helper Functions
// ═══════════════════════════════════════════════════════════════════════════════

/// Parse a comma-separated list of inclusive namespace prefixes.
///
/// Returns `None` if the input is NULL (meaning no inclusive prefixes).
///
/// # Safety
///
/// - `input` must be NULL or a valid pointer to a NUL-terminated `xmlChar`
///   string; a non-NULL input is read as a C string with `CStr::from_ptr`.
fn parse_inclusive_prefixes(input: *const xmlChar) -> Option<HashSet<String>> {
    if input.is_null() {
        return None;
    }

    let input_str = unsafe {
        let c_str = core::ffi::CStr::from_ptr(input as *const c_char);
        match c_str.to_str() {
            Ok(s) => s.to_string(),
            Err(_) => return None,
        }
    };

    if input_str.is_empty() {
        return None;
    }

    let mut set = HashSet::new();
    for prefix in input_str.split(',') {
        let trimmed = prefix.trim();
        if !trimmed.is_empty() {
            set.insert(trimmed.to_string());
        }
    }

    if set.is_empty() {
        None
    } else {
        Some(set)
    }
}

/// Compare two nodes in document order.
///
/// Returns negative if `a` comes before `b`, positive if `a` comes after `b`.
///
/// Used by the unit tests to verify node-set ordering.
///
/// # SAFETY
///
/// - `a` and `b` must be valid pointers to `_xmlNode`.
#[cfg(test)]
unsafe fn cmp_document_order(a: *mut _xmlNode, b: *mut _xmlNode) -> std::cmp::Ordering {
    if a == b {
        return std::cmp::Ordering::Equal;
    }

    // Build ancestor chains
    let mut ancestors_a: Vec<*mut _xmlNode> = Vec::new();
    let mut cur = a;
    while !cur.is_null() {
        ancestors_a.push(cur);
        cur = unsafe { (*cur).parent };
    }

    let mut ancestors_b: Vec<*mut _xmlNode> = Vec::new();
    let mut cur = b;
    while !cur.is_null() {
        ancestors_b.push(cur);
        cur = unsafe { (*cur).parent };
    }

    // Find the lowest common ancestor
    let mut i = ancestors_a.len();
    let mut j = ancestors_b.len();

    while i > 0 && j > 0 && ancestors_a[i - 1] == ancestors_b[j - 1] {
        i -= 1;
        j -= 1;
    }

    if i == 0 || j == 0 {
        // One is an ancestor of the other
        if i == 0 {
            return std::cmp::Ordering::Less;
        }
        return std::cmp::Ordering::Greater;
    }

    // The nodes at i-1 and j-1 are siblings under the common ancestor.
    // Determine their order by walking the sibling chain.
    let sibling_a = ancestors_a[i - 1];
    let sibling_b = ancestors_b[j - 1];

    // Walk forward from sibling_a to see if we find sibling_b
    let mut walk = sibling_a;
    while !walk.is_null() {
        if walk == sibling_b {
            return std::cmp::Ordering::Less;
        }
        walk = unsafe { (*walk).next };
    }

    // sibling_b must be before sibling_a
    std::cmp::Ordering::Greater
}

// ═══════════════════════════════════════════════════════════════════════════════
// C ABI Exports
// ═══════════════════════════════════════════════════════════════════════════════

/// Serialize canonical XML to a memory buffer.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xmlC14NDocDumpMemory(
///     xmlDocPtr doc,
///     xmlNodeSetPtr nodes,
///     int mode,
///     xmlChar **inclusive_ns_prefixes,
///     int with_comments,
///     xmlChar **result
/// );
/// ```
///
/// Serializes `doc` (or a subset of `nodes`) to canonical XML.
/// The result is allocated with `xmlMalloc` and must be freed by the caller
/// with `xmlFree`.
///
/// Returns the length of the result string in bytes, or -1 on error.
///
/// # SAFETY
///
/// - `doc` must be a valid pointer to an `_xmlDoc` or NULL.
/// - `nodes` may be NULL (entire document) or a valid `xmlNodeSet*` whose
///   nodes form the subset to canonicalize (upstream signature).
/// - `result` must be a valid pointer to a `xmlChar*` that will receive the result.
#[no_mangle]
pub unsafe extern "C" fn xmlC14NDocDumpMemory(
    doc: *mut _xmlDoc,
    nodes: *mut _xmlNodeSet,
    mode: c_int,
    inclusive_ns_prefixes: *mut *mut xmlChar,
    with_comments: c_int,
    result: *mut *mut xmlChar,
) -> c_int {
    // SAFETY: Delegates to the safe internal implementation.
    // The caller must provide valid pointers or NULL as documented.

    let c14n_mode = match mode {
        0 => C14nMode::XML_C14N_1_0,
        1 => C14nMode::XML_C14N_EXCLUSIVE_1_0,
        2 => C14nMode::XML_C14N_1_1,
        3 => C14nMode::XML_C14N_1_0_WITH_COMMENTS,
        4 => C14nMode::XML_C14N_EXCLUSIVE_1_0_WITH_COMMENTS,
        5 => C14nMode::XML_C14N_1_1_WITH_COMMENTS,
        _ => return -1,
    };

    // The inclusive_ns_prefixes parameter in the upstream API is a
    // NULL-terminated array of xmlChar* strings. We join them with commas
    // for our internal parser.
    let joined_prefixes: Option<Vec<u8>> = if !inclusive_ns_prefixes.is_null() {
        let mut parts: Vec<*mut xmlChar> = Vec::new();
        let mut i = 0;
        loop {
            let p = unsafe { *inclusive_ns_prefixes.add(i) };
            if p.is_null() {
                break;
            }
            parts.push(p);
            i += 1;
        }

        if parts.is_empty() {
            None
        } else {
            // Build comma-separated string (upstream passes a NULL-terminated
            // array; keep the Vec alive across the delegated call).
            let mut result_str = Vec::<u8>::new();
            for (idx, &part) in parts.iter().enumerate() {
                if idx > 0 {
                    result_str.push(b',');
                }
                let len = tree::xml_strlen(part);
                let part_slice = unsafe { core::slice::from_raw_parts(part, len as usize) };
                result_str.extend_from_slice(part_slice);
            }
            result_str.push(0); // null-terminate
            Some(result_str)
        }
    } else {
        None
    };
    let joined_ptr = joined_prefixes
        .as_ref()
        .map(|v| v.as_ptr() as *const xmlChar)
        .unwrap_or(ptr::null());

    // The node-set conversion is kept alive for the duration of the call
    // and dropped afterwards (no leak).
    let nodes_array = node_set_to_array(nodes);
    let nodes_ptr = if nodes_array.is_empty() {
        ptr::null_mut()
    } else {
        nodes_array.as_ptr() as *mut *mut _xmlNode
    };
    let ret = unsafe {
        c14n_doc_dump_memory(doc, nodes_ptr, c14n_mode, joined_ptr, with_comments, result)
    };
    drop(nodes_array);
    ret
}

/// Build the visibility set from a NULL-terminated node array (subset
/// argument of `xmlC14NDocDumpMemory` / `xmlC14NDocSaveTo`). None means the
/// whole document is visible (upstream `nodes == NULL`).
///
/// # SAFETY
///
/// - `nodes` must be NULL or a valid NULL-terminated array of `_xmlNode`
///   pointers.
unsafe fn build_visible_set(nodes: *mut *mut _xmlNode) -> Option<HashSet<*mut c_void>> {
    if nodes.is_null() {
        return None;
    }
    let mut set = HashSet::new();
    let mut i = 0usize;
    loop {
        let n = unsafe { *nodes.add(i) };
        if n.is_null() {
            break;
        }
        set.insert(n as *mut c_void);
        i += 1;
    }
    Some(set)
}

/// Walk the document children with the current context (upstream
/// xmlC14NProcessNodeList over doc->children).
///
/// # SAFETY
///
/// - `doc` must be a valid pointer to an `_xmlDoc`.
/// - `ctx` must be a valid pointer to a `C14nContext`.
/// - `buf` must be a valid pointer to a mutable `_xmlBuffer`.
unsafe fn c14n_walk_document(doc: *mut _xmlDoc, ctx: &mut C14nContext, buf: *mut _xmlBuffer) {
    let doc_node = doc as *mut _xmlNode;
    let d = unsafe { &*doc_node };
    let mut child = d.children;
    while !child.is_null() {
        c14n_serialize_node(child, ctx, buf);
        child = unsafe { (*child).next };
    }
}

/// Convert an upstream `xmlNodeSet*` (subset argument of
/// `xmlC14NDocDumpMemory` / `xmlC14NDocSaveTo`) into a NULL-terminated
/// node array used by the internal canonicalization walk. An empty Vec
/// means NULL (whole document).
///
/// # SAFETY
///
/// - `nodes` must be NULL or a valid pointer to an `_xmlNodeSet`.
unsafe fn node_set_to_array(nodes: *mut _xmlNodeSet) -> Vec<*mut _xmlNode> {
    if nodes.is_null() {
        return Vec::new();
    }
    let ns = unsafe { &*nodes };
    let nr = if ns.nodeNr > 0 { ns.nodeNr as usize } else { 0 };
    let mut vec: Vec<*mut _xmlNode> = Vec::with_capacity(nr + 1);
    for i in 0..nr {
        vec.push(unsafe { *ns.nodeTab.add(i) });
    }
    vec.push(ptr::null_mut());
    vec
}

/// Canonicalize XML with a callback for output.
///
/// # UPSTREAM-PARITY
///
/// Canonicalize a document with a caller-supplied visibility callback
/// (upstream c14n.h / c14n.c `xmlC14NExecute`).
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xmlC14NExecute(xmlDoc *doc,
///                    xmlC14NIsVisibleCallback is_visible_callback,
///                    void *user_data,
///                    int mode, /* a xmlC14NMode */
///                    xmlChar **inclusive_ns_prefixes,
///                    int with_comments,
///                    xmlOutputBuffer *buf);
/// ```
///
/// R-000176: the pre-11.1-Z.2 candidate exported a 6-argument form with a
/// completely different register layout (doc, mode, prefixes, with_comments,
/// write-callback, data) that misread every argument a C caller passes.
/// This now mirrors the oracle exactly: `is_visible_callback` decides node
/// visibility (NULL means the whole document is visible, upstream
/// `xmlC14NIsVisible`), and the canonical output is written to `buf` via
/// `xmlOutputBufferWrite`. Returns 0 on success, -1 on error.
///
/// # SAFETY
///
/// - `doc` must be a valid pointer to an `_xmlDoc` or NULL.
/// - `is_visible_callback` may be NULL; when non-NULL it and `user_data`
///   must stay valid for the walk.
/// - `buf` must be a valid pointer to an `_xmlOutputBuffer` or NULL.
#[no_mangle]
pub unsafe extern "C" fn xmlC14NExecute(
    doc: *mut _xmlDoc,
    is_visible_callback: Option<crate::abi::callbacks::xmlC14NIsVisibleCallback>,
    user_data: *mut c_void,
    mode: c_int,
    inclusive_ns_prefixes: *mut *mut xmlChar,
    with_comments: c_int,
    output: *mut _xmlOutputBuffer,
) -> c_int {
    // SAFETY: Delegates to the safe internal implementation.
    if doc.is_null() || output.is_null() {
        return -1;
    }

    let c14n_mode = match mode {
        0 => C14nMode::XML_C14N_1_0,
        1 => C14nMode::XML_C14N_EXCLUSIVE_1_0,
        2 => C14nMode::XML_C14N_1_1,
        3 => C14nMode::XML_C14N_1_0_WITH_COMMENTS,
        4 => C14nMode::XML_C14N_EXCLUSIVE_1_0_WITH_COMMENTS,
        5 => C14nMode::XML_C14N_1_1_WITH_COMMENTS,
        _ => return -1,
    };

    let joined_prefixes: Option<Vec<u8>> = if !inclusive_ns_prefixes.is_null() {
        let mut parts: Vec<*mut xmlChar> = Vec::new();
        let mut i = 0;
        loop {
            let p = unsafe { *inclusive_ns_prefixes.add(i) };
            if p.is_null() {
                break;
            }
            parts.push(p);
            i += 1;
        }

        if parts.is_empty() {
            None
        } else {
            // Build comma-separated string; the Vec must stay alive across
            // the delegated call (dangling-pointer fix).
            let mut result_str = Vec::<u8>::new();
            for (idx, &part) in parts.iter().enumerate() {
                if idx > 0 {
                    result_str.push(b',');
                }
                let len = tree::xml_strlen(part);
                let part_slice = unsafe { core::slice::from_raw_parts(part, len as usize) };
                result_str.extend_from_slice(part_slice);
            }
            result_str.push(0);
            Some(result_str)
        }
    } else {
        None
    };
    let joined_ptr = joined_prefixes
        .as_ref()
        .map(|v| v.as_ptr() as *const xmlChar)
        .unwrap_or(ptr::null());

    unsafe {
        c14n_execute_visibility(
            doc,
            c14n_mode,
            joined_ptr,
            with_comments,
            is_visible_callback,
            user_data,
            output,
        )
    }
}

/// Save canonical XML to an output buffer.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xmlC14NDocSaveTo(
///     xmlDocPtr doc,
///     xmlNodeSetPtr nodes,
///     int mode,
///     xmlChar **inclusive_ns_prefixes,
///     int with_comments,
///     xmlOutputBufferPtr output
/// );
/// ```
///
/// # SAFETY
///
/// - `doc` must be a valid pointer to an `_xmlDoc` or NULL.
/// - `output` must be a valid pointer to an `_xmlOutputBuffer` or NULL.
#[no_mangle]
pub unsafe extern "C" fn xmlC14NDocSaveTo(
    doc: *mut _xmlDoc,
    nodes: *mut _xmlNodeSet,
    mode: c_int,
    inclusive_ns_prefixes: *mut *mut xmlChar,
    with_comments: c_int,
    output: *mut _xmlOutputBuffer,
) -> c_int {
    // SAFETY: Delegates to the safe internal implementation.

    let c14n_mode = match mode {
        0 => C14nMode::XML_C14N_1_0,
        1 => C14nMode::XML_C14N_EXCLUSIVE_1_0,
        2 => C14nMode::XML_C14N_1_1,
        3 => C14nMode::XML_C14N_1_0_WITH_COMMENTS,
        4 => C14nMode::XML_C14N_EXCLUSIVE_1_0_WITH_COMMENTS,
        5 => C14nMode::XML_C14N_1_1_WITH_COMMENTS,
        _ => return -1,
    };

    let joined_prefixes: Option<Vec<u8>> = if !inclusive_ns_prefixes.is_null() {
        let mut parts: Vec<*mut xmlChar> = Vec::new();
        let mut i = 0;
        loop {
            let p = unsafe { *inclusive_ns_prefixes.add(i) };
            if p.is_null() {
                break;
            }
            parts.push(p);
            i += 1;
        }

        if parts.is_empty() {
            None
        } else {
            let mut result_str = Vec::<u8>::new();
            for (idx, &part) in parts.iter().enumerate() {
                if idx > 0 {
                    result_str.push(b',');
                }
                let len = tree::xml_strlen(part);
                let part_slice = unsafe { core::slice::from_raw_parts(part, len as usize) };
                result_str.extend_from_slice(part_slice);
            }
            result_str.push(0);
            Some(result_str)
        }
    } else {
        None
    };

    let joined_ptr = joined_prefixes
        .as_ref()
        .map(|v| v.as_ptr() as *const xmlChar)
        .unwrap_or(ptr::null());

    let nodes_array = node_set_to_array(nodes);
    let nodes_ptr = if nodes_array.is_empty() {
        ptr::null_mut()
    } else {
        nodes_array.as_ptr() as *mut *mut _xmlNode
    };
    let ret =
        unsafe { c14n_doc_save_to(doc, nodes_ptr, c14n_mode, joined_ptr, with_comments, output) };
    drop(nodes_array);
    ret
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::abi::allocator::xmlFreeImpl;
    use crate::xml::io;
    use crate::xml::tree;
    use core::ptr;
    use std::os::raw::c_int;

    /// Helper: create a simple document for testing.
    ///
    /// Creates: `<root><child attr="value">text</child></root>`
    unsafe fn create_simple_doc() -> *mut _xmlDoc {
        let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
        assert!(!doc.is_null());

        let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
        assert!(!root.is_null());
        tree::doc_set_root_element(doc, root);

        let child = tree::new_node(ptr::null_mut(), b"child\0" as *const u8 as *const xmlChar);
        assert!(!child.is_null());
        tree::add_child(root, child);

        // Set attribute
        tree::set_prop(
            child,
            b"attr\0" as *const u8 as *const xmlChar,
            b"value\0" as *const u8 as *const xmlChar,
        );

        // Set text content
        let text = tree::new_text(b"text\0" as *const u8 as *const xmlChar);
        assert!(!text.is_null());
        tree::add_child(child, text);

        doc
    }

    /// Helper: canonicalize a document and return the result as a String.
    unsafe fn canonicalize_doc(doc: *mut _xmlDoc, mode: C14nMode, with_comments: c_int) -> String {
        let mut result: *mut xmlChar = ptr::null_mut();
        let len = c14n_doc_dump_memory(
            doc,
            ptr::null_mut(),
            mode,
            ptr::null(),
            with_comments,
            &mut result as *mut *mut xmlChar,
        );
        assert!(len >= 0);
        assert!(!result.is_null());

        let s = {
            let slice = core::slice::from_raw_parts(result, len as usize);
            String::from_utf8_lossy(slice).to_string()
        };
        xmlFreeImpl(result as *mut c_void);
        s
    }

    // ── Basic document canonicalization ──

    /// Verify that an unexpanded entity reference makes C14N fail with -1,
    /// matching the upstream oracle.
    ///
    /// # Safety
    ///
    /// - The `xmlReadMemory` input must be a byte buffer readable for
    ///   `len - 1` bytes and NUL-terminated at the end.
    /// - The returned `doc` is asserted non-NULL and freed with `xmlFreeDoc`;
    ///   the `result` out-parameter is left NULL on the -1 failure path, as
    ///   asserted.
    #[test]
    fn test_c14n_entity_ref_node_fails_like_upstream() {
        // R-000175: an unexpanded entity reference is an invalid node for
        // canonical XML (upstream c14n.c xmlC14NProcessNode returns -1 via
        // xmlC14NErrInvalidNode). The candidate used to serialize the
        // reference (`&foo;`); it must fail with -1 exactly like the oracle.
        unsafe {
            let xml = b"<?xml version='1.0'?><!DOCTYPE r [<!ELEMENT r (#PCDATA)><!ENTITY foo \"FOO\">]><r>a &foo; b</r>\0";
            let doc = crate::abi::exports_xml2::xmlReadMemory(
                xml.as_ptr() as *const c_char,
                (xml.len() - 1) as c_int,
                b"t.xml\0" as *const u8 as *const c_char,
                ptr::null(),
                0,
            );
            assert!(!doc.is_null(), "doc must parse (NOENT unset keeps the ref)");
            let mut result: *mut xmlChar = ptr::null_mut();
            let len = c14n_doc_dump_memory(
                doc,
                ptr::null_mut(),
                C14nMode::XML_C14N_1_0,
                ptr::null(),
                0,
                &mut result as *mut *mut xmlChar,
            );
            assert_eq!(len, -1, "entity-ref node must fail canonicalization");
            assert!(result.is_null());
            crate::abi::exports_xml2::xmlFreeDoc(doc);
        }
    }

    /// Verify that canonicalizing a simple document emits the expected
    /// element markup.
    ///
    /// # Safety
    ///
    /// - `doc` returned by `create_simple_doc` must be a valid `_xmlDoc`
    ///   pointer, alive until `canonicalize_doc` returns; the test frees it
    ///   with `tree::free_doc`.
    #[test]
    fn test_c14n_basic_document() {
        unsafe {
            let doc = create_simple_doc();
            let result = canonicalize_doc(doc, C14nMode::XML_C14N_1_0, 0);
            assert!(
                result.contains("<root>"),
                "Result should contain <root>, got: {}",
                result
            );
            assert!(
                result.contains("<child"),
                "Result should contain <child>, got: {}",
                result
            );
            assert!(
                result.contains("attr=\"value\""),
                "Result should contain attr=\"value\", got: {}",
                result
            );
            assert!(
                result.contains("text"),
                "Result should contain text, got: {}",
                result
            );
            assert!(
                result.contains("</child>"),
                "Result should contain </child>, got: {}",
                result
            );
            assert!(
                result.contains("</root>"),
                "Result should contain </root>, got: {}",
                result
            );
            tree::free_doc(doc);
        }
    }

    /// Verify that an empty element is canonicalized in expanded form.
    ///
    /// # Safety
    ///
    /// - `doc` and `root` created by the tree helpers must be valid and linked
    ///   before `canonicalize_doc` reads them; the doc is freed with
    ///   `tree::free_doc`.
    #[test]
    fn test_c14n_basic_empty_element() {
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            assert!(!doc.is_null());
            let root = tree::new_node(ptr::null_mut(), b"empty\0" as *const u8 as *const xmlChar);
            assert!(!root.is_null());
            tree::doc_set_root_element(doc, root);

            let result = canonicalize_doc(doc, C14nMode::XML_C14N_1_0, 0);
            // UPSTREAM-PARITY (C14N 1.0 §2.4): empty elements are rendered
            // expanded (`<empty></empty>`), never self-closing. R-000166.
            assert!(
                result.contains("<empty></empty>"),
                "Empty element should be expanded, got: {}",
                result
            );
            tree::free_doc(doc);
        }
    }

    // ── Namespace propagation ──

    /// Verify that namespace declarations propagate through canonicalization.
    ///
    /// # Safety
    ///
    /// - `doc`, `ns`, and `root` created by the tree helpers must be valid,
    ///   live pointers while `canonicalize_doc` walks the document; the doc is
    ///   freed with `tree::free_doc`.
    #[test]
    fn test_c14n_namespace_propagation() {
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            assert!(!doc.is_null());

            let ns = tree::new_ns(
                ptr::null_mut(),
                b"http://example.com/ns\0" as *const u8 as *const xmlChar,
                b"ex\0" as *const u8 as *const xmlChar,
            );
            assert!(!ns.is_null());

            let root = tree::new_node(ns, b"root\0" as *const u8 as *const xmlChar);
            assert!(!root.is_null());
            tree::doc_set_root_element(doc, root);

            // Re-attach the namespace to the root node
            tree::new_ns(
                root,
                b"http://example.com/ns\0" as *const u8 as *const xmlChar,
                b"ex\0" as *const u8 as *const xmlChar,
            );

            let result = canonicalize_doc(doc, C14nMode::XML_C14N_1_0, 0);
            assert!(
                result.contains("xmlns:ex=\"http://example.com/ns\""),
                "Result should contain namespace declaration, got: {}",
                result
            );
            assert!(
                result.contains("<ex:root"),
                "Result should contain <ex:root, got: {}",
                result
            );
            tree::free_doc(doc);
        }
    }

    // ── Attribute ordering ──

    /// Verify that attributes are canonicalized in lexicographic order.
    ///
    /// # Safety
    ///
    /// - `doc` and `root` created by the tree helpers must be valid and linked
    ///   before `canonicalize_doc` reads them; the doc is freed with
    ///   `tree::free_doc`.
    #[test]
    fn test_c14n_attribute_ordering() {
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            assert!(!doc.is_null());

            let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
            assert!(!root.is_null());
            tree::doc_set_root_element(doc, root);

            // Set attributes in reverse alphabetical order
            tree::set_prop(
                root,
                b"zeta\0" as *const u8 as *const xmlChar,
                b"1\0" as *const u8 as *const xmlChar,
            );
            tree::set_prop(
                root,
                b"alpha\0" as *const u8 as *const xmlChar,
                b"2\0" as *const u8 as *const xmlChar,
            );
            tree::set_prop(
                root,
                b"beta\0" as *const u8 as *const xmlChar,
                b"3\0" as *const u8 as *const xmlChar,
            );

            let result = canonicalize_doc(doc, C14nMode::XML_C14N_1_0, 0);

            // Find the position of each attribute in the output
            let alpha_pos = result.find("alpha=\"2\"");
            let beta_pos = result.find("beta=\"3\"");
            let zeta_pos = result.find("zeta=\"1\"");

            assert!(alpha_pos.is_some(), "alpha attribute should be present");
            assert!(beta_pos.is_some(), "beta attribute should be present");
            assert!(zeta_pos.is_some(), "zeta attribute should be present");

            // alpha should come before beta, beta before zeta
            assert!(
                alpha_pos.unwrap() < beta_pos.unwrap(),
                "alpha should come before beta"
            );
            assert!(
                beta_pos.unwrap() < zeta_pos.unwrap(),
                "beta should come before zeta"
            );

            tree::free_doc(doc);
        }
    }

    // ── Character escaping ──

    /// Verify that text content escapes the less-than, ampersand,
    /// greater-than, and carriage-return characters.
    ///
    /// # Safety
    ///
    /// - The text bytes must be NUL-terminated (`tree::new_text` reads them as
    ///   a C string); `doc` and `root` must be valid while `canonicalize_doc`
    ///   runs, and the doc is freed with `tree::free_doc`.
    #[test]
    fn test_c14n_character_escaping_text() {
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            assert!(!doc.is_null());

            let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
            assert!(!root.is_null());
            tree::doc_set_root_element(doc, root);

            // Text with special characters
            let text = tree::new_text(b"a < b & c > d\r\0" as *const u8 as *const xmlChar);
            assert!(!text.is_null());
            tree::add_child(root, text);

            let result = canonicalize_doc(doc, C14nMode::XML_C14N_1_0, 0);
            assert!(result.contains("&lt;"), "Should escape <, got: {}", result);
            assert!(result.contains("&amp;"), "Should escape &, got: {}", result);
            assert!(result.contains("&gt;"), "Should escape >, got: {}", result);
            assert!(
                result.contains("&#xD;"),
                "Should escape CR, got: {}",
                result
            );

            tree::free_doc(doc);
        }
    }

    /// Verify that attribute values escape the less-than, ampersand, quote,
    /// tab, newline, and carriage-return characters.
    ///
    /// # Safety
    ///
    /// - The attribute name and value byte buffers must be NUL-terminated
    ///   (`tree::set_prop` reads them as C strings); `doc` and `root` must be
    ///   valid while `canonicalize_doc` runs, and the doc is freed with
    ///   `tree::free_doc`.
    #[test]
    fn test_c14n_character_escaping_attr() {
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            assert!(!doc.is_null());

            let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
            assert!(!root.is_null());
            tree::doc_set_root_element(doc, root);

            // Attribute value with special characters including tab, newline, CR
            tree::set_prop(
                root,
                b"test\0" as *const u8 as *const xmlChar,
                b"a < b & c \" d\t\n\r\0" as *const u8 as *const xmlChar,
            );

            let result = canonicalize_doc(doc, C14nMode::XML_C14N_1_0, 0);
            assert!(
                result.contains("&lt;"),
                "Should escape < in attr, got: {}",
                result
            );
            assert!(
                result.contains("&amp;"),
                "Should escape & in attr, got: {}",
                result
            );
            assert!(
                result.contains("&quot;"),
                "Should escape \" in attr, got: {}",
                result
            );
            assert!(
                result.contains("&#x9;"),
                "Should escape tab in attr, got: {}",
                result
            );
            assert!(
                result.contains("&#xA;"),
                "Should escape newline in attr, got: {}",
                result
            );
            assert!(
                result.contains("&#xD;"),
                "Should escape CR in attr, got: {}",
                result
            );

            tree::free_doc(doc);
        }
    }

    // ── With comments ──

    /// Verify that comments are omitted or included depending on the mode.
    ///
    /// # Safety
    ///
    /// - `doc`, `root`, and `comment` created by the tree helpers must be
    ///   valid, live pointers while `canonicalize_doc` runs; the doc is freed
    ///   with `tree::free_doc`.
    #[test]
    fn test_c14n_with_comments() {
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            assert!(!doc.is_null());

            let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
            assert!(!root.is_null());
            tree::doc_set_root_element(doc, root);

            let comment = tree::new_comment(b" a comment \0" as *const u8 as *const xmlChar);
            assert!(!comment.is_null());
            tree::add_child(root, comment);

            // Without comments
            let result_no_comments = canonicalize_doc(doc, C14nMode::XML_C14N_1_0, 0);
            assert!(
                !result_no_comments.contains("<!--"),
                "Without comments: should not contain comments, got: {}",
                result_no_comments
            );

            // With comments
            let result_with_comments =
                canonicalize_doc(doc, C14nMode::XML_C14N_1_0_WITH_COMMENTS, 0);
            assert!(
                result_with_comments.contains("<!--"),
                "With comments: should contain comments, got: {}",
                result_with_comments
            );

            tree::free_doc(doc);
        }
    }

    // ── Exclusive vs inclusive ──

    /// Verify the namespace rendering difference between exclusive and
    /// inclusive C14N.
    ///
    /// # Safety
    ///
    /// - `doc`, `root`, and `child` created by the tree helpers must be valid,
    ///   live pointers while `canonicalize_doc` runs; the doc is freed with
    ///   `tree::free_doc`.
    #[test]
    fn test_c14n_exclusive_vs_inclusive() {
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            assert!(!doc.is_null());

            // Create root with a namespace
            tree::new_ns(
                ptr::null_mut(),
                b"http://example.com/ns1\0" as *const u8 as *const xmlChar,
                b"ns1\0" as *const u8 as *const xmlChar,
            );
            let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
            assert!(!root.is_null());
            tree::doc_set_root_element(doc, root);
            tree::new_ns(
                root,
                b"http://example.com/ns1\0" as *const u8 as *const xmlChar,
                b"ns1\0" as *const u8 as *const xmlChar,
            );

            // Child without any namespace usage
            let child = tree::new_node(ptr::null_mut(), b"child\0" as *const u8 as *const xmlChar);
            assert!(!child.is_null());
            tree::add_child(root, child);

            // Inclusive C14N should include the ns1 namespace on child
            let result_inclusive = canonicalize_doc(doc, C14nMode::XML_C14N_1_0, 0);
            // The inclusive output will include namespace declarations from ancestors
            // on each element.

            // Exclusive C14N should NOT include the ns1 namespace on child
            // since the child doesn't use it
            let _result_exclusive = canonicalize_doc(doc, C14nMode::XML_C14N_EXCLUSIVE_1_0, 0);

            // In inclusive mode, the namespace should be visible
            // In exclusive mode, it should NOT be on the child (which doesn't use ns1)
            // The root element in exclusive mode still has ns1 declared on it

            // Both should contain the ns1 namespace somewhere
            assert!(
                result_inclusive.contains("ns1"),
                "Inclusive should have ns1, got: {}",
                result_inclusive
            );

            tree::free_doc(doc);
        }
    }

    // ── XML declaration handling ──

    /// Verify that C14N output omits the XML declaration.
    ///
    /// # Safety
    ///
    /// - `doc` returned by `create_simple_doc` must be a valid `_xmlDoc`
    ///   pointer, alive until `canonicalize_doc` returns; the test frees it
    ///   with `tree::free_doc`.
    #[test]
    fn test_c14n_no_xml_declaration() {
        unsafe {
            let doc = create_simple_doc();
            let result = canonicalize_doc(doc, C14nMode::XML_C14N_1_0, 0);
            // C14N output should NOT include the XML declaration
            assert!(
                !result.contains("<?xml"),
                "C14N output should not contain XML declaration, got: {}",
                result
            );
            tree::free_doc(doc);
        }
    }

    // ── Empty document ──

    /// Verify that an empty document canonicalizes to empty output.
    ///
    /// # Safety
    ///
    /// - `doc` created by `tree::new_doc` must be a valid `_xmlDoc` pointer
    ///   while `canonicalize_doc` runs; the doc is freed with `tree::free_doc`.
    #[test]
    fn test_c14n_empty_document() {
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            assert!(!doc.is_null());

            let result = canonicalize_doc(doc, C14nMode::XML_C14N_1_0, 0);
            assert!(
                result.is_empty(),
                "Empty document should produce empty output, got: {}",
                result
            );

            tree::free_doc(doc);
        }
    }

    // ── Edge cases ──

    /// Verify that canonicalizing a NULL document returns -1.
    ///
    /// # Safety
    ///
    /// - Passing a NULL `doc` is allowed and must not be dereferenced; the
    ///   `result` out-parameter receives no allocation on the failure path.
    #[test]
    fn test_c14n_null_doc() {
        unsafe {
            let mut result: *mut xmlChar = ptr::null_mut();
            let len = c14n_doc_dump_memory(
                ptr::null_mut(),
                ptr::null_mut(),
                C14nMode::XML_C14N_1_0,
                ptr::null(),
                0,
                &mut result as *mut *mut xmlChar,
            );
            assert_eq!(len, -1, "Null doc should return -1");
        }
    }

    /// Verify that text node content survives canonicalization.
    ///
    /// # Safety
    ///
    /// - `doc`, `root`, and `text` created by the tree helpers must be valid,
    ///   live pointers while `canonicalize_doc` runs; the doc is freed with
    ///   `tree::free_doc`.
    #[test]
    fn test_c14n_text_node() {
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            assert!(!doc.is_null());

            let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
            assert!(!root.is_null());
            tree::doc_set_root_element(doc, root);

            // Text node with various content
            let text = tree::new_text(b"Hello World\0" as *const u8 as *const xmlChar);
            assert!(!text.is_null());
            tree::add_child(root, text);

            let result = canonicalize_doc(doc, C14nMode::XML_C14N_1_0, 0);
            assert!(
                result.contains("Hello World"),
                "Should contain text content, got: {}",
                result
            );

            tree::free_doc(doc);
        }
    }

    /// Verify that CDATA content is canonicalized as escaped text.
    ///
    /// # Safety
    ///
    /// - `doc`, `root`, and the text node created by the tree helpers must be
    ///   valid, live pointers while `canonicalize_doc` runs; the doc is freed
    ///   with `tree::free_doc`.
    #[test]
    fn test_c14n_cdata_section() {
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            assert!(!doc.is_null());

            let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
            assert!(!root.is_null());
            tree::doc_set_root_element(doc, root);

            // CDATA section content is represented as a text node in the tree
            // For C14N, CDATA sections are converted to text
            let cdata =
                tree::new_text(b"<greeting>Hello</greeting>\0" as *const u8 as *const xmlChar);
            assert!(!cdata.is_null());
            tree::add_child(root, cdata);

            let result = canonicalize_doc(doc, C14nMode::XML_C14N_1_0, 0);
            assert!(
                result.contains("&lt;greeting&gt;"),
                "CDATA should be converted to escaped text, got: {}",
                result
            );

            tree::free_doc(doc);
        }
    }

    /// Verify that processing instructions are canonicalized.
    ///
    /// # Safety
    ///
    /// - `doc`, `root`, and `pi` created by the tree helpers must be valid,
    ///   live pointers while `canonicalize_doc` runs; the doc is freed with
    ///   `tree::free_doc`.
    #[test]
    fn test_c14n_pi_node() {
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            assert!(!doc.is_null());

            let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
            assert!(!root.is_null());
            tree::doc_set_root_element(doc, root);

            let pi = tree::new_pi(
                b"xml-model\0" as *const u8 as *const xmlChar,
                b"href=\"schema.xsd\"\0" as *const u8 as *const xmlChar,
            );
            assert!(!pi.is_null());
            tree::add_child(root, pi);

            let result = canonicalize_doc(doc, C14nMode::XML_C14N_1_0, 0);
            assert!(result.contains("<?"), "Should contain PI, got: {}", result);

            tree::free_doc(doc);
        }
    }

    /// Verify that the with-comments flag includes comments in the output.
    ///
    /// # Safety
    ///
    /// - `doc`, `root`, and `comment` created by the tree helpers must be
    ///   valid, live pointers while `c14n_doc_dump_memory` runs.
    /// - `result` receives a buffer owned by the callee; it is asserted
    ///   non-NULL, read as `len` bytes, and freed with `xmlFreeImpl`.
    #[test]
    fn test_c14n_with_comments_flag() {
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            assert!(!doc.is_null());

            let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
            assert!(!root.is_null());
            tree::doc_set_root_element(doc, root);

            let comment = tree::new_comment(b"test\0" as *const u8 as *const xmlChar);
            assert!(!comment.is_null());
            tree::add_child(root, comment);

            // Using the with_comments flag with the base mode
            let mut result: *mut xmlChar = ptr::null_mut();
            let len = c14n_doc_dump_memory(
                doc,
                ptr::null_mut(),
                C14nMode::XML_C14N_1_0,
                ptr::null(),
                1, // with_comments = true
                &mut result as *mut *mut xmlChar,
            );
            assert!(len >= 0);
            let s = {
                let slice = core::slice::from_raw_parts(result, len as usize);
                String::from_utf8_lossy(slice).to_string()
            };
            xmlFreeImpl(result as *mut c_void);

            assert!(
                s.contains("<!--"),
                "With comments flag should include comments, got: {}",
                s
            );

            tree::free_doc(doc);
        }
    }

    #[test]
    fn test_c14n_mode_enum_values() {
        // Verify mode enum values match upstream constants
        assert_eq!(C14nMode::XML_C14N_1_0 as c_int, 0);
        assert_eq!(C14nMode::XML_C14N_EXCLUSIVE_1_0 as c_int, 1);
        assert_eq!(C14nMode::XML_C14N_1_1 as c_int, 2);
        assert_eq!(C14nMode::XML_C14N_1_0_WITH_COMMENTS as c_int, 3);
        assert_eq!(C14nMode::XML_C14N_EXCLUSIVE_1_0_WITH_COMMENTS as c_int, 4);
        assert_eq!(C14nMode::XML_C14N_1_1_WITH_COMMENTS as c_int, 5);
    }

    #[test]
    fn test_c14n_with_comments_property() {
        assert!(C14nMode::XML_C14N_1_0_WITH_COMMENTS.with_comments());
        assert!(C14nMode::XML_C14N_EXCLUSIVE_1_0_WITH_COMMENTS.with_comments());
        assert!(C14nMode::XML_C14N_1_1_WITH_COMMENTS.with_comments());
        assert!(!C14nMode::XML_C14N_1_0.with_comments());
        assert!(!C14nMode::XML_C14N_EXCLUSIVE_1_0.with_comments());
        assert!(!C14nMode::XML_C14N_1_1.with_comments());
    }

    #[test]
    fn test_c14n_is_exclusive_property() {
        assert!(C14nMode::XML_C14N_EXCLUSIVE_1_0.is_exclusive());
        assert!(C14nMode::XML_C14N_EXCLUSIVE_1_0_WITH_COMMENTS.is_exclusive());
        assert!(!C14nMode::XML_C14N_1_0.is_exclusive());
        assert!(!C14nMode::XML_C14N_1_0_WITH_COMMENTS.is_exclusive());
        assert!(!C14nMode::XML_C14N_1_1.is_exclusive());
    }

    /// Verify that `c14n_escape_text` escapes carriage returns as `&#xD;`.
    ///
    /// # Safety
    ///
    /// - `buf` from `io::buf_create` must be a valid `_xmlBuffer` pointer for
    ///   the whole call; it is freed with `io::buf_free`.
    /// - `text` must point to a buffer with at least `len` readable bytes
    ///   (the NUL terminator is not needed because `len` bounds the read).
    #[test]
    fn test_c14n_escape_text_cr() {
        unsafe {
            let buf = io::buf_create(-1);
            assert!(!buf.is_null());

            let text = b"line1\r\nline2\r\0" as *const u8 as *const xmlChar;
            c14n_escape_text(buf, text, 13);

            let content = io::buf_content(buf);
            let len = io::buf_length(buf);
            let s = {
                let slice = core::slice::from_raw_parts(content, len as usize);
                String::from_utf8_lossy(slice).to_string()
            };
            assert!(
                s.contains("&#xD;"),
                "CR should be escaped as &#xD;, got: {}",
                s
            );
            assert!(s.contains("\n"), "LF should remain as-is, got: {}", s);

            io::buf_free(buf);
        }
    }

    /// Verify that `c14n_escape_attr` escapes tab, newline, and carriage
    /// return characters.
    ///
    /// # Safety
    ///
    /// - `buf` from `io::buf_create` must be a valid `_xmlBuffer` pointer for
    ///   the whole call; it is freed with `io::buf_free`.
    /// - `text` must be a valid NUL-terminated `xmlChar` string, read as a C
    ///   string by `c14n_escape_attr`.
    #[test]
    fn test_c14n_escape_attr_tab_nl_cr() {
        unsafe {
            let buf = io::buf_create(-1);
            assert!(!buf.is_null());

            let text = b"a\tb\nc\rd\0" as *const u8 as *const xmlChar;
            c14n_escape_attr(buf, text);

            let content = io::buf_content(buf);
            let len = io::buf_length(buf);
            let s = {
                let slice = core::slice::from_raw_parts(content, len as usize);
                String::from_utf8_lossy(slice).to_string()
            };
            assert!(
                s.contains("&#x9;"),
                "Tab should be escaped as &#x9;, got: {}",
                s
            );
            assert!(
                s.contains("&#xA;"),
                "NL should be escaped as &#xA;, got: {}",
                s
            );
            assert!(
                s.contains("&#xD;"),
                "CR should be escaped as &#xD;, got: {}",
                s
            );

            io::buf_free(buf);
        }
    }

    /// Verify `parse_inclusive_prefixes` on NULL, empty, and comma-separated
    /// inputs.
    ///
    /// # Safety
    ///
    /// - The static byte-string pointers passed in must be NUL-terminated;
    ///   NULL is allowed and yields `None`.
    #[test]
    fn test_c14n_parse_inclusive_prefixes() {
        // Test NULL input
        assert!(parse_inclusive_prefixes(ptr::null()).is_none());

        // Test empty string
        let empty = b"\0" as *const u8 as *const xmlChar;
        assert!(parse_inclusive_prefixes(empty).is_none());

        // Test single prefix
        let single = b"foo\0" as *const u8 as *const xmlChar;
        let result = parse_inclusive_prefixes(single);
        assert!(result.is_some());
        assert!(result.unwrap().contains("foo"));

        // Test multiple prefixes
        let multi = b"foo,bar,baz\0" as *const u8 as *const xmlChar;
        let result = parse_inclusive_prefixes(multi);
        assert!(result.is_some());
        let set = result.unwrap();
        assert!(set.contains("foo"));
        assert!(set.contains("bar"));
        assert!(set.contains("baz"));
        assert_eq!(set.len(), 3);
    }

    /// Verify `cmp_document_order` against siblings in document order.
    ///
    /// # Safety
    ///
    /// - `doc`, `root`, `child1`, and `child2` created by the tree helpers
    ///   must be valid, live pointers while `cmp_document_order` walks the
    ///   ancestor chains; the doc is freed with `tree::free_doc`.
    #[test]
    fn test_c14n_document_order() {
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            assert!(!doc.is_null());

            let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
            assert!(!root.is_null());
            tree::doc_set_root_element(doc, root);

            let child1 =
                tree::new_node(ptr::null_mut(), b"child1\0" as *const u8 as *const xmlChar);
            assert!(!child1.is_null());
            tree::add_child(root, child1);

            let child2 =
                tree::new_node(ptr::null_mut(), b"child2\0" as *const u8 as *const xmlChar);
            assert!(!child2.is_null());
            tree::add_child(root, child2);

            // child1 should come before child2
            assert_eq!(
                cmp_document_order(child1, child2),
                std::cmp::Ordering::Less,
                "child1 should be before child2"
            );
            assert_eq!(
                cmp_document_order(child2, child1),
                std::cmp::Ordering::Greater,
                "child2 should be after child1"
            );
            assert_eq!(
                cmp_document_order(child1, child1),
                std::cmp::Ordering::Equal,
                "Same node should be equal"
            );

            tree::free_doc(doc);
        }
    }

    /// Verify that `c14n_escape_text` escapes a greater-than sign as `&gt;`.
    ///
    /// # Safety
    ///
    /// - `buf` from `io::buf_create` must be a valid `_xmlBuffer` pointer for
    ///   the whole call; it is freed with `io::buf_free`.
    /// - `text` must point to a buffer with at least `len` readable bytes.
    #[test]
    fn test_c14n_escape_text_gt() {
        unsafe {
            let buf = io::buf_create(-1);
            assert!(!buf.is_null());

            let text = b"a > b\0" as *const u8 as *const xmlChar;
            c14n_escape_text(buf, text, 5);

            let content = io::buf_content(buf);
            let len = io::buf_length(buf);
            let s = {
                let slice = core::slice::from_raw_parts(content, len as usize);
                String::from_utf8_lossy(slice).to_string()
            };
            assert!(
                s.contains("&gt;"),
                "> should be escaped as &gt;, got: {}",
                s
            );

            io::buf_free(buf);
        }
    }

    /// Verify that `c14n_escape_text` escapes the CDATA-end sequence.
    ///
    /// # Safety
    ///
    /// - `buf` from `io::buf_create` must be a valid `_xmlBuffer` pointer for
    ///   the whole call; it is freed with `io::buf_free`.
    /// - `text` must point to a buffer with at least `len` readable bytes.
    #[test]
    fn test_c14n_escape_text_cdata_end() {
        unsafe {
            let buf = io::buf_create(-1);
            assert!(!buf.is_null());

            let text = b"a]]>b\0" as *const u8 as *const xmlChar;
            c14n_escape_text(buf, text, 5);

            let content = io::buf_content(buf);
            let len = io::buf_length(buf);
            let s = {
                let slice = core::slice::from_raw_parts(content, len as usize);
                String::from_utf8_lossy(slice).to_string()
            };
            // The `]]>` sequence should become `]]&gt;`
            assert!(
                s.contains("]]&gt;"),
                "]]> should be escaped as ]]&gt;, got: {}",
                s
            );

            io::buf_free(buf);
        }
    }

    /// Verify that `c14n_execute` streams canonicalized output through a C
    /// callback.
    ///
    /// # Safety
    ///
    /// - `doc` must be a valid `_xmlDoc` pointer, alive for the call.
    /// - `output_vec` must be the `Box::into_raw` pointer of a byte vector
    ///   and stay exclusively owned until it is reclaimed with `Box::from_raw`.
    #[test]
    fn test_c14n_execute_callback() {
        unsafe {
            let doc = create_simple_doc();

            // Use a heap-allocated Vec passed through the callback context
            let output_vec = Box::into_raw(Box::new(Vec::<u8>::new()));

            /// C callback that appends canonicalized output to a byte vector.
            ///
            /// # Safety
            ///
            /// - `ctx` must be the `Box::into_raw` pointer of a byte vector
            ///   that is exclusively owned for the duration of the call.
            /// - `data` must point to `len` readable bytes of output; `len` is
            ///   cast to `usize` for the slice.
            unsafe extern "C" fn test_callback(
                ctx: *mut c_void,
                data: *const c_char,
                len: c_int,
            ) -> c_int {
                let slice = unsafe { core::slice::from_raw_parts(data as *const u8, len as usize) };
                let output = unsafe { &mut *(ctx as *mut Vec<u8>) };
                output.extend_from_slice(slice);
                len
            }

            let ret = c14n_execute(
                doc,
                C14nMode::XML_C14N_1_0,
                ptr::null(),
                0,
                Some(
                    test_callback
                        as unsafe extern "C" fn(*mut c_void, *const c_char, c_int) -> c_int,
                ),
                output_vec as *mut c_void,
            );

            assert!(ret >= 0, "c14n_execute should succeed");
            let output = Box::from_raw(output_vec);
            let output_str = String::from_utf8_lossy(&output);
            assert!(
                output_str.contains("<root>"),
                "Callback output should contain <root>, got: {}",
                output_str
            );

            tree::free_doc(doc);
        }
    }

    /// Verify that `c14n_doc_save_to` writes into an output buffer.
    ///
    /// # Safety
    ///
    /// - `doc` must be a valid `_xmlDoc` pointer, alive for the call.
    /// - `buf` and the `output` buffer created from it must be valid pointers
    ///   until `io::output_buffer_close`; `buf` is freed with `io::buf_free`
    ///   and the doc with `tree::free_doc`.
    #[test]
    fn test_c14n_save_to_output_buffer() {
        unsafe {
            let doc = create_simple_doc();

            // Create an output buffer
            let buf = io::buf_create(-1);
            assert!(!buf.is_null());

            let output = io::output_buffer_create_buffer(buf, ptr::null_mut());
            assert!(!output.is_null());

            let ret = c14n_doc_save_to(
                doc,
                ptr::null_mut(),
                C14nMode::XML_C14N_1_0,
                ptr::null(),
                0,
                output,
            );

            assert!(ret >= 0, "c14n_doc_save_to should succeed");

            let content = io::buf_content(buf);
            let len = io::buf_length(buf);
            let s = {
                let slice = core::slice::from_raw_parts(content, len as usize);
                String::from_utf8_lossy(slice).to_string()
            };
            assert!(
                s.contains("<root>"),
                "Output buffer should contain <root>, got: {}",
                s
            );

            io::output_buffer_close(output);
            io::buf_free(buf);
            tree::free_doc(doc);
        }
    }

    /// Verify the `xmlC14NDocDumpMemory` C ABI export.
    ///
    /// # Safety
    ///
    /// - `doc` must be a valid `_xmlDoc` pointer, alive for the call.
    /// - `result` receives a callee-owned buffer, asserted non-NULL, read as
    ///   `len` bytes, and freed with `xmlFreeImpl`; the doc is freed with
    ///   `tree::free_doc`.
    #[test]
    fn test_c14n_c_abi_doc_dump_memory() {
        unsafe {
            let doc = create_simple_doc();

            let mut result: *mut xmlChar = ptr::null_mut();
            let len = xmlC14NDocDumpMemory(
                doc,
                ptr::null_mut(),
                0, // XML_C14N_1_0
                ptr::null_mut(),
                0, // no comments
                &mut result as *mut *mut xmlChar,
            );

            assert!(len >= 0, "xmlC14NDocDumpMemory should succeed");
            assert!(!result.is_null());

            let s = {
                let slice = core::slice::from_raw_parts(result, len as usize);
                String::from_utf8_lossy(slice).to_string()
            };
            assert!(
                s.contains("<root>"),
                "C ABI export should produce canonical output, got: {}",
                s
            );

            xmlFreeImpl(result as *mut c_void);
            tree::free_doc(doc);
        }
    }

    /// Verify the `xmlC14NExecute` C ABI export with no visibility callback.
    ///
    /// # Safety
    ///
    /// - `doc` must be a valid `_xmlDoc` pointer, alive for the call.
    /// - `buf` and the `output` buffer created from it must be valid until
    ///   `io::output_buffer_close`; the doc is freed with `tree::free_doc`.
    #[test]
    fn test_c14n_c_abi_execute() {
        unsafe {
            let doc = create_simple_doc();

            // The oracle 7-argument contract (R-000176): no visibility
            // callback → whole document; output goes to an xmlOutputBuffer.
            let buf = io::buf_create(-1);
            assert!(!buf.is_null());
            let output = io::output_buffer_create_buffer(buf, ptr::null_mut());
            assert!(!output.is_null());

            let ret = xmlC14NExecute(
                doc,
                None,            // is_visible_callback
                ptr::null_mut(), // user_data
                0,               // XML_C14N_1_0
                ptr::null_mut(), // inclusive_ns_prefixes
                0,               // with_comments
                output,
            );

            assert!(ret >= 0, "xmlC14NExecute should succeed");
            // The write lands in the output buffer's internal buffer and is
            // flushed to the target buffer on close (upstream flush model).
            io::output_buffer_close(output);
            let content = io::buf_content(buf);
            let len = io::buf_length(buf);
            let s = {
                let slice = core::slice::from_raw_parts(content, len as usize);
                String::from_utf8_lossy(slice).to_string()
            };
            assert!(
                s.contains("<root>"),
                "C ABI execute should produce canonical output, got: {}",
                s
            );

            tree::free_doc(doc);
        }
    }

    /// Verify the `xmlC14NDocSaveTo` C ABI export.
    ///
    /// # Safety
    ///
    /// - `doc` must be a valid `_xmlDoc` pointer, alive for the call.
    /// - `buf` and the `output` buffer created from it must be valid until
    ///   `io::output_buffer_close`; `buf` is freed with `io::buf_free` and the
    ///   doc with `tree::free_doc`.
    #[test]
    fn test_c14n_c_abi_save_to() {
        unsafe {
            let doc = create_simple_doc();

            let buf = io::buf_create(-1);
            assert!(!buf.is_null());

            let output = io::output_buffer_create_buffer(buf, ptr::null_mut());
            assert!(!output.is_null());

            let ret = xmlC14NDocSaveTo(
                doc,
                ptr::null_mut(),
                0, // XML_C14N_1_0
                ptr::null_mut(),
                0,
                output,
            );

            assert!(ret >= 0, "xmlC14NDocSaveTo should succeed");

            let content = io::buf_content(buf);
            let len = io::buf_length(buf);
            let s = {
                let slice = core::slice::from_raw_parts(content, len as usize);
                String::from_utf8_lossy(slice).to_string()
            };
            assert!(
                s.contains("<root>"),
                "C ABI save_to should produce canonical output, got: {}",
                s
            );

            io::output_buffer_close(output);
            io::buf_free(buf);
            tree::free_doc(doc);
        }
    }

    // ── R-000166 regression tests (11.1-X C14N closure) ──

    /// Build `<root xmlns:p="http://u/p"><p:one><p:two p:a="1"/></p:one></root>`.
    unsafe fn build_nested_ns_doc() -> *mut _xmlDoc {
        let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
        let ns = tree::new_ns(
            ptr::null_mut(),
            b"http://u/p\0" as *const u8 as *const xmlChar,
            b"p\0" as *const u8 as *const xmlChar,
        );
        let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
        tree::doc_set_root_element(doc, root);
        tree::new_ns(
            root,
            b"http://u/p\0" as *const u8 as *const xmlChar,
            b"p\0" as *const u8 as *const xmlChar,
        );
        let one = tree::new_node(ns, b"one\0" as *const u8 as *const xmlChar);
        tree::add_child(root, one);
        let two = tree::new_node(ns, b"two\0" as *const u8 as *const xmlChar);
        tree::add_child(one, two);
        tree::set_prop(
            two,
            b"a\0" as *const u8 as *const xmlChar,
            b"1\0" as *const u8 as *const xmlChar,
        );
        // Bind the attribute to the p namespace.
        let attr = (*two).properties;
        (*attr).ns = ns;
        doc
    }

    /// Verify that exclusive C14N does not re-declare a namespace already
    /// rendered by an ancestor.
    ///
    /// # Safety
    ///
    /// - `doc` from `build_nested_ns_doc` must be a valid `_xmlDoc` with valid
    ///   node, namespace, and attribute pointers while `canonicalize_doc` runs;
    ///   the doc is freed with `tree::free_doc`.
    #[test]
    fn test_c14n_exclusive_skips_ancestor_rendered_ns() {
        // R-000166: exclusive C14N must not re-declare a namespace already
        // rendered by an ancestor (`xmlExcC14NProcessNamespacesAxis` with the
        // ns_rendered stack).
        unsafe {
            let doc = build_nested_ns_doc();
            let out = canonicalize_doc(doc, C14nMode::XML_C14N_EXCLUSIVE_1_0, 0);
            assert_eq!(
                out,
                "<root><p:one xmlns:p=\"http://u/p\"><p:two p:a=\"1\"></p:two></p:one></root>"
            );
            tree::free_doc(doc);
        }
    }

    /// Verify that namespaces render in lexicographic prefix order.
    ///
    /// # Safety
    ///
    /// - `doc`, `root`, and the namespace declarations created by the tree
    ///   helpers must be valid, live pointers while `canonicalize_doc` runs;
    ///   the doc is freed with `tree::free_doc`.
    #[test]
    fn test_c14n_namespace_sorting() {
        // R-000166: namespaces render in lexicographic prefix order (upstream
        // xmlC14NNsCompare) regardless of declaration order.
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
            tree::doc_set_root_element(doc, root);
            tree::new_ns(
                root,
                b"http://u/z\0" as *const u8 as *const xmlChar,
                b"z\0" as *const u8 as *const xmlChar,
            );
            tree::new_ns(
                root,
                b"http://u/a\0" as *const u8 as *const xmlChar,
                b"a\0" as *const u8 as *const xmlChar,
            );
            let out = canonicalize_doc(doc, C14nMode::XML_C14N_1_0, 0);
            assert_eq!(
                out,
                "<root xmlns:a=\"http://u/a\" xmlns:z=\"http://u/z\"></root>"
            );
            tree::free_doc(doc);
        }
    }

    /// Verify that the implicit `xml` namespace is never rendered.
    ///
    /// # Safety
    ///
    /// - `doc`, `root`, and `xml_ns` created by the tree helpers must be valid,
    ///   live pointers; `xml_ns` is stored in `doc.oldNs` and must stay alive
    ///   until the doc is freed with `tree::free_doc`.
    #[test]
    fn test_c14n_xml_ns_never_rendered() {
        // R-000166: the xml namespace is never rendered as xmlns:xml in
        // exclusive C14N (`xmlC14NIsXmlNs`).
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
            tree::doc_set_root_element(doc, root);
            // Materialize the xml namespace like the parser does.
            let xml_ns = tree::new_ns(
                ptr::null_mut(),
                b"http://www.w3.org/XML/1998/namespace\0" as *const u8 as *const xmlChar,
                b"xml\0" as *const u8 as *const xmlChar,
            );
            (*doc).oldNs = xml_ns;
            let out = canonicalize_doc(doc, C14nMode::XML_C14N_EXCLUSIVE_1_0, 0);
            assert_eq!(out, "<root></root>");
            assert!(!out.contains("xmlns:xml"));
            tree::free_doc(doc);
        }
    }

    /// Verify that an empty default namespace undeclaration is rendered.
    ///
    /// # Safety
    ///
    /// - `doc`, `root`, `d_ns`, `a`, and the namespace declarations created
    ///   by the tree helpers must be valid, live pointers while
    ///   `canonicalize_doc` runs; the doc is freed with `tree::free_doc`.
    #[test]
    fn test_c14n_empty_default_undeclaration() {
        // R-000166: `<a xmlns="">` renders the empty default undeclaration
        // in exclusive C14N (element with no binding resolves the default
        // namespace in scope).
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
            tree::doc_set_root_element(doc, root);
            let d_ns = tree::new_ns(
                ptr::null_mut(),
                b"http://u/d\0" as *const u8 as *const xmlChar,
                ptr::null(),
            );
            tree::new_ns(
                root,
                b"http://u/d\0" as *const u8 as *const xmlChar,
                ptr::null(),
            );
            let a = tree::new_node(d_ns, b"a\0" as *const u8 as *const xmlChar);
            tree::add_child(root, a);
            // `a` carries xmlns="". Like the parser, an empty default
            // namespace is not bound to the element (a.ns stays NULL), so
            // exclusive C14N resolves it via the in-scope default search.
            tree::new_ns(a, b"\0" as *const u8 as *const xmlChar, ptr::null());
            (*a).ns = ptr::null_mut();
            let out = canonicalize_doc(doc, C14nMode::XML_C14N_EXCLUSIVE_1_0, 0);
            assert_eq!(out, "<root xmlns=\"http://u/d\"><a xmlns=\"\"></a></root>");
            tree::free_doc(doc);
        }
    }

    /// Verify that exclusive C14N rejects relative namespace URIs.
    ///
    /// # Safety
    ///
    /// - `doc` and `root` created by the tree helpers must be valid, live
    ///   pointers while `c14n_doc_dump_memory` runs; the doc is freed with
    ///   `tree::free_doc`.
    #[test]
    fn test_c14n_relative_ns_rejected_exclusive() {
        // R-000166: the relative-namespace-URI rejection applies in exclusive
        // mode too (xmlC14NCheckForRelativeNamespaces runs in
        // xmlC14NProcessElementNode for every mode).
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
            tree::doc_set_root_element(doc, root);
            tree::new_ns(
                root,
                b"u\0" as *const u8 as *const xmlChar,
                b"p\0" as *const u8 as *const xmlChar,
            );
            let mut result: *mut xmlChar = ptr::null_mut();
            let len = c14n_doc_dump_memory(
                doc,
                ptr::null_mut(),
                C14nMode::XML_C14N_EXCLUSIVE_1_0,
                ptr::null(),
                0,
                &mut result as *mut *mut xmlChar,
            );
            assert!(
                len < 0,
                "exclusive C14N must reject relative namespace URIs"
            );
            tree::free_doc(doc);
        }
    }

    /// Verify newline placement for document-level PIs.
    ///
    /// # Safety
    ///
    /// - `doc`, `pi1`, `root`, and `pi2` created by the tree helpers must be
    ///   valid, live pointers; the PIs are children of the doc and must stay
    ///   alive until the doc is freed with `tree::free_doc`.
    #[test]
    fn test_c14n_pi_document_level_newlines() {
        // R-000166: PIs before the document element get a trailing newline,
        // PIs after it a leading newline.
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            let pi1 = tree::new_pi(b"one\0" as *const u8 as *const xmlChar, ptr::null());
            tree::add_child(doc as *mut _xmlNode, pi1);
            let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
            tree::add_child(doc as *mut _xmlNode, root);
            let pi2 = tree::new_pi(b"three\0" as *const u8 as *const xmlChar, ptr::null());
            tree::add_child(doc as *mut _xmlNode, pi2);
            let out = canonicalize_doc(doc, C14nMode::XML_C14N_1_0, 0);
            assert_eq!(out, "<?one?>\n<root></root>\n<?three?>");
            tree::free_doc(doc);
        }
    }

    /// Verify that subset canonicalization renders only nodes in the set.
    ///
    /// # Safety
    ///
    /// - `doc`, `root`, and `child` created by the tree helpers must be valid,
    ///   live pointers while `c14n_doc_dump_memory` runs; the nodeset array
    ///   must be NULL-terminated and live for the call.
    /// - `result` receives a callee-owned buffer, read as `len` bytes, and
    ///   freed with `xmlFreeImpl`; the doc is freed with `tree::free_doc`.
    #[test]
    fn test_c14n_subset_visibility() {
        // R-000166: subset canonicalization renders only nodes in the set;
        // the document element is still visited so invisible siblings are
        // skipped.
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
            tree::doc_set_root_element(doc, root);
            let child = tree::new_node(ptr::null_mut(), b"child\0" as *const u8 as *const xmlChar);
            tree::add_child(root, child);

            let mut nodes: Vec<*mut _xmlNode> = vec![root, ptr::null_mut()];
            let mut result: *mut xmlChar = ptr::null_mut();
            let len = c14n_doc_dump_memory(
                doc,
                nodes.as_mut_ptr(),
                C14nMode::XML_C14N_1_0,
                ptr::null(),
                0,
                &mut result as *mut *mut xmlChar,
            );
            assert!(len >= 0);
            let s = {
                let slice = core::slice::from_raw_parts(result, len as usize);
                String::from_utf8_lossy(slice).to_string()
            };
            xmlFreeImpl(result as *mut c_void);
            // Only `root` is in the set; `child` is processed but not rendered.
            assert_eq!(s, "<root></root>");
            tree::free_doc(doc);
        }
    }

    /// Verify that C14N 1.0 imports xml-namespace attributes from hidden
    /// ancestors of a visible orphan element.
    ///
    /// # Safety
    ///
    /// - `doc`, `xml_ns`, `root`, `lang_attr`, and `child` created by the tree
    ///   helpers must be valid, live pointers while `c14n_doc_dump_memory`
    ///   runs; the nodeset array must be NULL-terminated and live for the call.
    /// - `result` receives a callee-owned buffer, read as `len` bytes, and
    ///   freed with `xmlFreeImpl`; the doc is freed with `tree::free_doc`.
    #[test]
    fn test_c14n_subset_hidden_parent_xml_lang() {
        // R-000166: C14N 1.0 imports xml-namespace attributes from hidden
        // ancestors of a visible orphan element.
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            let xml_ns = tree::new_ns(
                ptr::null_mut(),
                b"http://www.w3.org/XML/1998/namespace\0" as *const u8 as *const xmlChar,
                b"xml\0" as *const u8 as *const xmlChar,
            );
            (*doc).oldNs = xml_ns;
            let root = tree::new_node(ptr::null_mut(), b"root\0" as *const u8 as *const xmlChar);
            tree::doc_set_root_element(doc, root);
            tree::set_prop(
                root,
                b"lang\0" as *const u8 as *const xmlChar,
                b"en\0" as *const u8 as *const xmlChar,
            );
            let lang_attr = (*root).properties;
            (*lang_attr).ns = xml_ns;

            let child = tree::new_node(ptr::null_mut(), b"a\0" as *const u8 as *const xmlChar);
            tree::add_child(root, child);

            let mut nodes: Vec<*mut _xmlNode> = vec![child, ptr::null_mut()];
            let mut result: *mut xmlChar = ptr::null_mut();
            let len = c14n_doc_dump_memory(
                doc,
                nodes.as_mut_ptr(),
                C14nMode::XML_C14N_1_0,
                ptr::null(),
                0,
                &mut result as *mut *mut xmlChar,
            );
            assert!(len >= 0);
            let s = {
                let slice = core::slice::from_raw_parts(result, len as usize);
                String::from_utf8_lossy(slice).to_string()
            };
            xmlFreeImpl(result as *mut c_void);
            assert_eq!(s, "<a xml:lang=\"en\"></a>");
            tree::free_doc(doc);
        }
    }

    /// Verify that a prefix rebound away and back to an older URI is
    /// re-rendered.
    ///
    /// # Safety
    ///
    /// - `doc`, `root`, `b`, `c`, and the namespace declarations created by
    ///   the tree helpers must be valid, live pointers while `canonicalize_doc`
    ///   runs; the doc is freed with `tree::free_doc`.
    #[test]
    fn test_c14n_rebinding_chain_rere_declares() {
        // R-000166: a prefix rebound away and back to an older URI IS
        // re-rendered (prefix-scoped last-wins find semantics).
        unsafe {
            let doc = tree::new_doc(b"1.0\0" as *const u8 as *const xmlChar);
            let ns1 = tree::new_ns(
                ptr::null_mut(),
                b"http://u/1\0" as *const u8 as *const xmlChar,
                b"p\0" as *const u8 as *const xmlChar,
            );
            let root = tree::new_node(ptr::null_mut(), b"a\0" as *const u8 as *const xmlChar);
            tree::doc_set_root_element(doc, root);
            tree::new_ns(
                root,
                b"http://u/1\0" as *const u8 as *const xmlChar,
                b"p\0" as *const u8 as *const xmlChar,
            );
            let b = tree::new_node(ns1, b"b\0" as *const u8 as *const xmlChar);
            tree::add_child(root, b);
            let ns2 = tree::new_ns(
                ptr::null_mut(),
                b"http://u/2\0" as *const u8 as *const xmlChar,
                b"p\0" as *const u8 as *const xmlChar,
            );
            tree::new_ns(
                b,
                b"http://u/2\0" as *const u8 as *const xmlChar,
                b"p\0" as *const u8 as *const xmlChar,
            );
            (*b).ns = ns2;
            let c = tree::new_node(ns1, b"c\0" as *const u8 as *const xmlChar);
            tree::add_child(b, c);
            tree::new_ns(
                c,
                b"http://u/1\0" as *const u8 as *const xmlChar,
                b"p\0" as *const u8 as *const xmlChar,
            );

            let out = canonicalize_doc(doc, C14nMode::XML_C14N_1_0, 0);
            assert_eq!(
                out,
                "<a xmlns:p=\"http://u/1\"><p:b xmlns:p=\"http://u/2\"><p:c xmlns:p=\"http://u/1\"></p:c></p:b></a>"
            );
            tree::free_doc(doc);
        }
    }
}