libxml-rs 0.1.0-alpha.26

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 correlated to upstream commits; full xmllint/xmlcatalog/xsltproc CLIs; differential-court-verified C API closure (xmlXPath*, xmlTextReader*, xmlTextWriter*, catalogs, serialization, data globals, chvalid, encoding, the full libxslt surface) โ€” parity ledger at 0 missing for both libxml2 and libxslt. 1144 tests passing.
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
//! Debug/memory debugging infrastructure (ยง85 Phase 7).
//!
//! UPSTREAM-PARITY: Corresponds to `debugXML.c` / `debugXML.h` in libxml2.
//!
//! libxml2's debug APIs for printing tree structure, XPath expressions, etc.
//! These are used by `xmllint --debug` and other diagnostic tools.

use crate::abi::allocator::xmlFreeImpl;
use crate::abi::structs::{_xmlAttr, _xmlDoc, _xmlNode, _xmlNs};
use core::ffi::{c_char, c_int, c_void};
use core::ptr;

/// Maximum indentation depth for debug output.
const MAX_DEPTH: c_int = 100;

/// Check if a node is an XInclude start node.
///
/// UPSTREAM-PARITY: `xmlDebugIsXInclude()` โ€” internal check used by debug dumper.
fn is_xinclude_node(node: *mut _xmlNode) -> bool {
    if node.is_null() {
        return false;
    }
    unsafe {
        let ns = (*node).ns;
        if ns.is_null() {
            return false;
        }
        let ns_href = (*ns).href;
        let ns_prefix = (*ns).prefix;
        if ns_href.is_null() {
            return false;
        }
        // Check for XInclude namespace
        let href = core::slice::from_raw_parts(ns_href as *const u8, 30);
        let xi_ns = b"http://www.w3.org/2001/XInclude\0";
        let mut matches = true;
        for i in 0..30 {
            if i >= href.len() || href[i] != xi_ns[i] {
                matches = false;
                break;
            }
        }
        if !matches {
            return false;
        }
        // Check for xi:include element
        let name_bytes = if !(*node).name.is_null() {
            core::slice::from_raw_parts((*node).name as *const u8, 8)
        } else {
            return false;
        };
        name_bytes.len() >= 7 && &name_bytes[..7] == b"include"
    }
}

/// Convert a boolean to text.
///
/// UPSTREAM-PARITY: `xmlBoolToText()`
#[no_mangle]
pub unsafe extern "C" fn xmlBoolToText(boolval: c_int) -> *const c_char {
    if boolval != 0 {
        b"true\0".as_ptr() as *const c_char
    } else {
        b"false\0".as_ptr() as *const c_char
    }
}

/// Dump a debug representation of an xmlChar string.
///
/// UPSTREAM-PARITY: `xmlDebugDumpString()` โ€” line breaks, tabs and CRs are
/// rendered as a single space.
#[no_mangle]
pub unsafe extern "C" fn xmlDebugDumpString(output: *mut _IO_FILE, str_val: *const u8) {
    if output.is_null() {
        return;
    }
    if str_val.is_null() {
        unsafe {
            libc::fprintf(output, b"(NULL)\0".as_ptr() as *const c_char);
        }
        return;
    }
    unsafe {
        // UPSTREAM-PARITY: xmlCtxtDumpString prints at most 40 characters;
        // blank characters become spaces, bytes >= 0x80 are printed as
        // `#%X`, and a longer string is truncated with "...".
        let mut i = 0;
        while i < 40 {
            let c = *str_val.add(i);
            if c == 0 {
                return;
            }
            if c == b' ' || c == b'\t' || c == b'\n' || c == b'\r' {
                libc::fprintf(output, b" \0".as_ptr() as *const c_char);
            } else if c >= 0x80 {
                libc::fprintf(output, b"#%X\0".as_ptr() as *const c_char, c as c_int);
            } else {
                libc::fprintf(output, b"%c\0".as_ptr() as *const c_char, c as c_int);
            }
            i += 1;
        }
        libc::fprintf(output, b"...\0".as_ptr() as *const c_char);
    }
}

/// Dump a debug representation of an attribute.
///
/// UPSTREAM-PARITY: `xmlDebugDumpAttr()`
#[no_mangle]
pub unsafe extern "C" fn xmlDebugDumpAttr(
    output: *mut _IO_FILE,
    attr: *mut _xmlAttr,
    depth: c_int,
) {
    if output.is_null() || attr.is_null() {
        return;
    }
    unsafe {
        for _ in 0..depth {
            libc::fprintf(output, b"  \0".as_ptr() as *const c_char);
        }
        libc::fprintf(output, b"ATTRIBUTE \0".as_ptr() as *const c_char);
        xmlDebugDumpString(output, (*attr).name as *const u8);
        libc::fprintf(output, b"\n\0".as_ptr() as *const c_char);
        // The attribute value is dumped as a compact text node child.
        xmlDebugDumpNode(output, (*attr).children, depth + 1);
    }
}

/// Dump a debug representation of an attribute list.
///
/// UPSTREAM-PARITY: `xmlDebugDumpAttrList()`
#[no_mangle]
pub unsafe extern "C" fn xmlDebugDumpAttrList(
    output: *mut _IO_FILE,
    attr: *mut _xmlAttr,
    depth: c_int,
) {
    if output.is_null() {
        return;
    }
    let mut cur = attr;
    while !cur.is_null() {
        unsafe {
            xmlDebugDumpAttr(output, cur, depth);
            cur = (*cur).next;
        }
    }
}

/// Dump a single node for debug output.
///
/// UPSTREAM-PARITY: `xmlDebugDumpOneNode()`
#[no_mangle]
pub unsafe extern "C" fn xmlDebugDumpOneNode(
    output: *mut _IO_FILE,
    node: *mut _xmlNode,
    depth: c_int,
) {
    if output.is_null() || node.is_null() {
        return;
    }
    unsafe {
        // Indent
        for _ in 0..depth {
            libc::fprintf(output, b"  \0".as_ptr() as *const c_char);
        }

        // Print node type
        match (*node).type_ {
            1 => {
                // XML_ELEMENT_NODE
                libc::fprintf(output, b"ELEMENT \0".as_ptr() as *const c_char);
                // QName: prefix:name when a namespace prefix is present.
                if !(*node).ns.is_null() && !(*(*node).ns).prefix.is_null() {
                    libc::fprintf(
                        output,
                        b"%s:\0".as_ptr() as *const c_char,
                        (*(*node).ns).prefix,
                    );
                }
                xmlDebugDumpString(output, (*node).name as *const u8);
                libc::fprintf(output, b"\n\0".as_ptr() as *const c_char);

                // Namespace declarations on the element (upstream prints them
                // before attributes).
                if !(*node).nsDef.is_null() {
                    let mut ns = (*node).nsDef;
                    while !ns.is_null() {
                        for _ in 0..(depth + 1) {
                            libc::fprintf(output, b"  \0".as_ptr() as *const c_char);
                        }
                        libc::fprintf(output, b"namespace \0".as_ptr() as *const c_char);
                        if (*ns).prefix.is_null() {
                            libc::fprintf(output, b" \0".as_ptr() as *const c_char);
                        } else {
                            libc::fprintf(output, b"%s\0".as_ptr() as *const c_char, (*ns).prefix);
                        }
                        libc::fprintf(output, b" href=\0".as_ptr() as *const c_char);
                        libc::fprintf(output, b"%s\n\0".as_ptr() as *const c_char, (*ns).href);
                        ns = (*ns).next;
                    }
                }

                // Attributes
                if !(*node).properties.is_null() {
                    xmlDebugDumpAttrList(output, (*node).properties, depth + 1);
                }
            }
            2 => {
                // XML_ATTRIBUTE_NODE
                libc::fprintf(output, b"ATTRIBUTE \0".as_ptr() as *const c_char);
                xmlDebugDumpString(output, (*node).name as *const u8);
                libc::fprintf(output, b"\n\0".as_ptr() as *const c_char);
            }
            3 => {
                // XML_TEXT_NODE
                libc::fprintf(output, b"TEXT\0".as_ptr() as *const c_char);
                // UPSTREAM-PARITY: debugXML.c marks compact text via
                // `node->content == (xmlChar *) &(node->properties)`.
                let inline_addr = std::ptr::addr_of_mut!((*node).properties) as *const c_void;
                if (*node).content as *const c_void == inline_addr {
                    libc::fprintf(output, b" compact\0".as_ptr() as *const c_char);
                }
                libc::fprintf(output, b"\n\0".as_ptr() as *const c_char);
                for _ in 0..(depth + 1) {
                    libc::fprintf(output, b"  \0".as_ptr() as *const c_char);
                }
                libc::fprintf(output, b"content=\0".as_ptr() as *const c_char);
                if !(*node).content.is_null() {
                    xmlDebugDumpString(output, (*node).content as *const u8);
                } else {
                    let c = crate::xml::tree::node_get_content(node);
                    if !c.is_null() {
                        libc::fprintf(output, b"%s\0".as_ptr() as *const c_char, c);
                        crate::abi::allocator::xmlFreeImpl(c as *mut c_void);
                    }
                }
                libc::fprintf(output, b"\n\0".as_ptr() as *const c_char);
            }
            4 => {
                // XML_CDATA_SECTION_NODE
                libc::fprintf(output, b"CDATA_SECTION\n\0".as_ptr() as *const c_char);
                for _ in 0..(depth + 1) {
                    libc::fprintf(output, b"  \0".as_ptr() as *const c_char);
                }
                libc::fprintf(output, b"content=\0".as_ptr() as *const c_char);
                xmlDebugDumpString(output, (*node).content as *const u8);
                libc::fprintf(output, b"\n\0".as_ptr() as *const c_char);
            }
            5 => {
                // XML_ENTITY_REF_NODE
                libc::fprintf(output, b"ENTITY_REF(\0".as_ptr() as *const c_char);
                xmlDebugDumpString(output, (*node).name as *const u8);
                libc::fprintf(output, b")\n\0".as_ptr() as *const c_char);
                // The referenced entity's declaration.
                let doc = (*node).doc;
                let ent = if doc.is_null() {
                    ptr::null_mut()
                } else {
                    crate::xml::entities::get_entity(doc, (*node).name)
                };
                if !ent.is_null() {
                    for _ in 0..(depth + 1) {
                        libc::fprintf(output, b"  \0".as_ptr() as *const c_char);
                    }
                    let etype = (*ent).etype;
                    let etype_name = entity_type_name(etype);
                    libc::fprintf(
                        output,
                        b"%s \0".as_ptr() as *const c_char,
                        etype_name.as_ptr() as *const c_char,
                    );
                    libc::fprintf(output, b"%s\n\0".as_ptr() as *const c_char, (*ent).name);
                    for _ in 0..(depth + 1) {
                        libc::fprintf(output, b"  \0".as_ptr() as *const c_char);
                    }
                    libc::fprintf(output, b"content=\0".as_ptr() as *const c_char);
                    if !(*ent).content.is_null() {
                        xmlDebugDumpString(output, (*ent).content as *const u8);
                    }
                    libc::fprintf(output, b"\n\0".as_ptr() as *const c_char);
                }
            }
            6 => {
                // XML_ENTITY_NODE
                libc::fprintf(output, b"ENTITYDECL(\0".as_ptr() as *const c_char);
                xmlDebugDumpString(output, (*node).name as *const u8);
                libc::fprintf(output, b")\0".as_ptr() as *const c_char);
                if !(*node).content.is_null() {
                    libc::fprintf(output, b", internal\n \0".as_ptr() as *const c_char);
                    libc::fprintf(output, b"content=\0".as_ptr() as *const c_char);
                    xmlDebugDumpString(output, (*node).content as *const u8);
                }
                libc::fprintf(output, b"\n\0".as_ptr() as *const c_char);
            }
            7 => {
                // XML_PI_NODE
                libc::fprintf(output, b"PI \0".as_ptr() as *const c_char);
                xmlDebugDumpString(output, (*node).name as *const u8);
                libc::fprintf(output, b"\n\0".as_ptr() as *const c_char);
                if !(*node).content.is_null() {
                    for _ in 0..(depth + 1) {
                        libc::fprintf(output, b"  \0".as_ptr() as *const c_char);
                    }
                    libc::fprintf(output, b"content=\0".as_ptr() as *const c_char);
                    xmlDebugDumpString(output, (*node).content as *const u8);
                    libc::fprintf(output, b"\n\0".as_ptr() as *const c_char);
                }
            }
            8 => {
                // XML_COMMENT_NODE
                libc::fprintf(output, b"COMMENT\n\0".as_ptr() as *const c_char);
                for _ in 0..(depth + 1) {
                    libc::fprintf(output, b"  \0".as_ptr() as *const c_char);
                }
                libc::fprintf(output, b"content=\0".as_ptr() as *const c_char);
                xmlDebugDumpString(output, (*node).content as *const u8);
                libc::fprintf(output, b"\n\0".as_ptr() as *const c_char);
            }
            9 => {
                // XML_DOCUMENT_NODE
                libc::fprintf(output, b"DOCUMENT\0".as_ptr() as *const c_char);
            }
            10 => {
                // XML_DOCUMENT_TYPE_NODE
                libc::fprintf(output, b"DOCTYPE\0".as_ptr() as *const c_char);
            }
            14 => {
                // XML_DTD_NODE. UPSTREAM-PARITY: xmlCtxtDumpDtdNode prints
                // `DTD(name)`, `, PUBLIC extID` and `, SYSTEM sysID` all on
                // one line.
                libc::fprintf(output, b"DTD(\0".as_ptr() as *const c_char);
                xmlDebugDumpString(output, (*node).name as *const u8);
                libc::fprintf(output, b")\0".as_ptr() as *const c_char);
                let dtd = node as *mut crate::abi::structs::_xmlDtd;
                if !(*dtd).ExternalID.is_null() {
                    libc::fprintf(output, b", PUBLIC \0".as_ptr() as *const c_char);
                    libc::fprintf(output, b"%s\0".as_ptr() as *const c_char, (*dtd).ExternalID);
                }
                if !(*dtd).SystemID.is_null() {
                    libc::fprintf(output, b", SYSTEM \0".as_ptr() as *const c_char);
                    libc::fprintf(output, b"%s\0".as_ptr() as *const c_char, (*dtd).SystemID);
                }
                libc::fprintf(output, b"\n\0".as_ptr() as *const c_char);
                // Element declarations (hash table).
                let dtd = node as *mut crate::abi::structs::_xmlDtd;
                if !(*dtd).elements.is_null() {
                    let ctx = DtdDumpCtx {
                        output,
                        depth: depth + 1,
                    };
                    crate::xml::hash::hash_scan(
                        (*dtd).elements as *mut crate::xml::hash::HashTable,
                        Some(dump_elemscan_cb),
                        &ctx as *const DtdDumpCtx as *mut c_void,
                    );
                }
                // Entity declarations.
                if !(*dtd).entities.is_null() {
                    let ctx = DtdDumpCtx {
                        output,
                        depth: depth + 1,
                    };
                    crate::xml::hash::hash_scan(
                        (*dtd).entities as *mut crate::xml::hash::HashTable,
                        Some(dump_entityscan_cb),
                        &ctx as *const DtdDumpCtx as *mut c_void,
                    );
                }
            }
            13 => {
                // XML_HTML_DOCUMENT_NODE
                libc::fprintf(output, b"HTML DOCUMENT\0".as_ptr() as *const c_char);
            }
            18 => {
                // XML_NAMESPACE_DECL
                libc::fprintf(output, b"NAMESPACE\0".as_ptr() as *const c_char);
                if !(*node).ns.is_null() && !(*(*node).ns).prefix.is_null() {
                    libc::fprintf(
                        output,
                        b" %s=%s\0".as_ptr() as *const c_char,
                        (*(*node).ns).prefix,
                        (*(*node).ns).href,
                    );
                }
            }
            19 => {
                // XML_XINCLUDE_START
                if is_xinclude_node(node) {
                    libc::fprintf(output, b"XINCLUDE\0".as_ptr() as *const c_char);
                } else {
                    libc::fprintf(output, b"XINCLUDE_START\0".as_ptr() as *const c_char);
                }
            }
            20 => {
                // XML_XINCLUDE_END
                libc::fprintf(output, b"XINCLUDE_END\0".as_ptr() as *const c_char);
            }
            _ => {
                libc::fprintf(
                    output,
                    b"UNKNOWN (%d)\0".as_ptr() as *const c_char,
                    (*node).type_ as c_int,
                );
            }
        }
    }
}

/// Map an entity type to its upstream debug name.
fn entity_type_name(etype: c_int) -> Vec<u8> {
    use crate::abi::types::xmlEntityType::*;
    match etype {
        t if t == XML_INTERNAL_GENERAL_ENTITY as c_int => b"INTERNAL_GENERAL_ENTITY\0".to_vec(),
        t if t == XML_INTERNAL_PARAMETER_ENTITY as c_int => b"INTERNAL_PARAMETER_ENTITY\0".to_vec(),
        t if t == XML_EXTERNAL_GENERAL_PARSED_ENTITY as c_int => {
            b"EXTERNAL_GENERAL_PARSED_ENTITY\0".to_vec()
        }
        t if t == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY as c_int => {
            b"EXTERNAL_GENERAL_UNPARSED_ENTITY\0".to_vec()
        }
        t if t == XML_EXTERNAL_PARAMETER_ENTITY as c_int => b"EXTERNAL_PARAMETER_ENTITY\0".to_vec(),
        t if t == XML_INTERNAL_PREDEFINED_ENTITY as c_int => {
            b"INTERNAL_PREDEFINED_ENTITY\0".to_vec()
        }
        _ => b"UNKNOWN_ENTITY\0".to_vec(),
    }
}

/// Context for DTD hash-scan debug callbacks.
#[repr(C)]
struct DtdDumpCtx {
    output: *mut _IO_FILE,
    depth: c_int,
}

/// Dump an element declaration (`ELEMDECL(name), TYPE (model)`).
unsafe extern "C" fn dump_elemscan_cb(payload: *mut c_void, data: *mut c_void, _name: *const u8) {
    if payload.is_null() || data.is_null() {
        return;
    }
    let ctx = unsafe { &*(data as *const DtdDumpCtx) };
    let elem = unsafe { &*(payload as *mut crate::abi::structs::_xmlElement) };
    unsafe {
        for _ in 0..ctx.depth {
            libc::fprintf(ctx.output, b"  \0".as_ptr() as *const c_char);
        }
        libc::fprintf(ctx.output, b"ELEMDECL(\0".as_ptr() as *const c_char);
        if !elem.name.is_null() {
            libc::fprintf(ctx.output, b"%s\0".as_ptr() as *const c_char, elem.name);
        }
        libc::fprintf(ctx.output, b")\0".as_ptr() as *const c_char);
        // UPSTREAM-PARITY: 2.15 prints the MIXED label for every
        // parenthesized content model (even element-only ones) in the debug
        // dump.
        match elem.type_ {
            t if t == crate::abi::types::xmlElementTypeVal::XML_ELEMENT_TYPE_EMPTY as c_int => {
                libc::fprintf(ctx.output, b", EMPTY\n\0".as_ptr() as *const c_char);
            }
            t if t == crate::abi::types::xmlElementTypeVal::XML_ELEMENT_TYPE_ANY as c_int => {
                libc::fprintf(ctx.output, b", ANY\n\0".as_ptr() as *const c_char);
            }
            _ => {
                libc::fprintf(ctx.output, b", MIXED \0".as_ptr() as *const c_char);
                dump_debug_content_model(ctx.output, elem.content);
                libc::fprintf(ctx.output, b"\n\0".as_ptr() as *const c_char);
            }
        }
    }
}

/// Render a content model tree in the upstream debug format
/// (`xmlDebugDumpContentModel`, flattened).
unsafe fn dump_debug_content_model(
    output: *mut _IO_FILE,
    content: *mut crate::abi::structs::_xmlElementContent,
) {
    use crate::abi::types::xmlElementContentOccur::*;
    use crate::abi::types::xmlElementContentType::*;
    if content.is_null() {
        return;
    }
    unsafe {
        let c = &*content;
        match c.type_ {
            t if t == XML_ELEMENT_CONTENT_PCDATA as c_int => {
                libc::fprintf(output, b"(#PCDATA)\0".as_ptr() as *const c_char);
            }
            t if t == XML_ELEMENT_CONTENT_ELEMENT as c_int => {
                if !c.prefix.is_null() {
                    libc::fprintf(output, b"%s:\0".as_ptr() as *const c_char, c.prefix);
                }
                libc::fprintf(output, b"%s\0".as_ptr() as *const c_char, c.name);
            }
            _ => {
                let sep = if c.type_ == XML_ELEMENT_CONTENT_SEQ as c_int {
                    b" , \0".as_ptr() as *const c_char
                } else {
                    b" | \0".as_ptr() as *const c_char
                };
                libc::fprintf(output, b"(\0".as_ptr() as *const c_char);
                let mut parts: Vec<*mut crate::abi::structs::_xmlElementContent> = Vec::new();
                flatten_chain(c as *const _ as *mut _, c.type_, &mut parts);
                for (i, &p) in parts.iter().enumerate() {
                    if i > 0 {
                        libc::fprintf(output, b"%s\0".as_ptr() as *const c_char, sep);
                    }
                    let pc = &*p;
                    if pc.type_ == XML_ELEMENT_CONTENT_PCDATA as c_int {
                        libc::fprintf(output, b"#PCDATA\0".as_ptr() as *const c_char);
                    } else if pc.type_ == XML_ELEMENT_CONTENT_ELEMENT as c_int {
                        if !pc.prefix.is_null() {
                            libc::fprintf(output, b"%s:\0".as_ptr() as *const c_char, pc.prefix);
                        }
                        libc::fprintf(output, b"%s\0".as_ptr() as *const c_char, pc.name);
                    } else {
                        dump_debug_content_model(output, p);
                    }
                    dump_debug_occurrence(output, pc.ocur);
                }
                libc::fprintf(output, b")\0".as_ptr() as *const c_char);
            }
        }
        dump_debug_occurrence(output, c.ocur);
    }
}

/// Collect the leaves of a same-type chain (left-leaning trees flatten).
fn flatten_chain(
    node: *mut crate::abi::structs::_xmlElementContent,
    chain_type: c_int,
    parts: &mut Vec<*mut crate::abi::structs::_xmlElementContent>,
) {
    unsafe {
        let c = &*node;
        if c.type_ == chain_type && !c.c1.is_null() {
            flatten_chain(c.c1, chain_type, parts);
            if !c.c2.is_null() {
                flatten_chain(c.c2, chain_type, parts);
            }
        } else {
            parts.push(node);
        }
    }
}

/// Print the occurrence suffix.
unsafe fn dump_debug_occurrence(output: *mut _IO_FILE, ocur: c_int) {
    use crate::abi::types::xmlElementContentOccur::*;
    let s = match ocur {
        t if t == XML_ELEMENT_CONTENT_OPT as c_int => b"?\0".as_ptr() as *const c_char,
        t if t == XML_ELEMENT_CONTENT_MULT as c_int => b"*\0".as_ptr() as *const c_char,
        t if t == XML_ELEMENT_CONTENT_PLUS as c_int => b"+\0".as_ptr() as *const c_char,
        _ => return,
    };
    unsafe {
        libc::fprintf(output, b"%s\0".as_ptr() as *const c_char, s);
    }
}

/// True when a null-terminated string contains markup-significant bytes
/// (`<` or `&`), i.e. it cannot be a single plain text node.
unsafe fn contains_markup(s: *const crate::abi::types::xmlChar) -> bool {
    if s.is_null() {
        return false;
    }
    unsafe {
        let mut i = 0usize;
        while *s.add(i) != 0 {
            let c = *s.add(i);
            if c == b'<' || c == b'&' {
                return true;
            }
            i += 1;
        }
    }
    false
}

/// Dump an entity declaration (`ENTITYDECL(name), internal`).
unsafe extern "C" fn dump_entityscan_cb(payload: *mut c_void, data: *mut c_void, _name: *const u8) {
    if payload.is_null() || data.is_null() {
        return;
    }
    let ctx = unsafe { &*(data as *const DtdDumpCtx) };
    let ent = unsafe { &*(payload as *mut crate::abi::structs::_xmlEntity) };
    unsafe {
        for _ in 0..ctx.depth {
            libc::fprintf(ctx.output, b"  \0".as_ptr() as *const c_char);
        }
        libc::fprintf(ctx.output, b"ENTITYDECL(\0".as_ptr() as *const c_char);
        if !ent.name.is_null() {
            libc::fprintf(ctx.output, b"%s\0".as_ptr() as *const c_char, ent.name);
        }
        if ent.etype == crate::abi::types::xmlEntityType::XML_INTERNAL_GENERAL_ENTITY as c_int {
            libc::fprintf(ctx.output, b"), internal\n\0".as_ptr() as *const c_char);
            // UPSTREAM-PARITY: the content line is the ENTITYDECL indent plus
            // one extra leading space.
            for _ in 0..ctx.depth {
                libc::fprintf(ctx.output, b"  \0".as_ptr() as *const c_char);
            }
            libc::fprintf(ctx.output, b" content=\0".as_ptr() as *const c_char);
            if !ent.content.is_null() {
                xmlDebugDumpString(ctx.output, ent.content as *const u8);
            }
            libc::fprintf(ctx.output, b"\n\0".as_ptr() as *const c_char);
            // The entity's parsed content tree (upstream builds children on
            // first reference and dumps them; ours stores the raw content,
            // so a compact text node is synthesized for plain text content
            // only. KNOWN RESIDUAL: entity content containing markup (`<`,
            // `&`) is not parsed into a child tree, so the oracle's
            // element/entity-ref children are not reproduced here.
            if !ent.content.is_null() && !contains_markup(ent.content) && ent.children.is_null() {
                for _ in 0..(ctx.depth + 1) {
                    libc::fprintf(ctx.output, b"  \0".as_ptr() as *const c_char);
                }
                libc::fprintf(ctx.output, b"TEXT compact\n\0".as_ptr() as *const c_char);
                for _ in 0..(ctx.depth + 2) {
                    libc::fprintf(ctx.output, b"  \0".as_ptr() as *const c_char);
                }
                libc::fprintf(ctx.output, b"content=\0".as_ptr() as *const c_char);
                xmlDebugDumpString(ctx.output, ent.content as *const u8);
                libc::fprintf(ctx.output, b"\n\0".as_ptr() as *const c_char);
            }
        } else {
            libc::fprintf(ctx.output, b")\n\0".as_ptr() as *const c_char);
        }
    }
}

/// Dump a node and its subtree.
///
/// UPSTREAM-PARITY: `xmlDebugDumpNode()`
#[no_mangle]
pub unsafe extern "C" fn xmlDebugDumpNode(
    output: *mut _IO_FILE,
    node: *mut _xmlNode,
    depth: c_int,
) {
    if output.is_null() || node.is_null() || depth > MAX_DEPTH {
        return;
    }
    unsafe {
        xmlDebugDumpOneNode(output, node, depth);

        // UPSTREAM-PARITY: only element-like nodes recurse into children;
        // text nodes (including the non-compact merged representation) do not.
        let t = (*node).type_;
        let recurse = t == 1 // XML_ELEMENT_NODE
            || t == 9  // XML_DOCUMENT_NODE
            || t == 13 // XML_HTML_DOCUMENT_NODE
            || t == 11 // XML_DOCUMENT_FRAG_NODE
            || t == 14; // XML_DTD_NODE
        if recurse && !(*node).children.is_null() {
            let mut child = (*node).children;
            while !child.is_null() {
                xmlDebugDumpNode(output, child, depth + 1);
                child = (*child).next;
            }
        }
    }
}

/// Dump a node list.
///
/// UPSTREAM-PARITY: `xmlDebugDumpNodeList()`
#[no_mangle]
pub unsafe extern "C" fn xmlDebugDumpNodeList(
    output: *mut _IO_FILE,
    node: *mut _xmlNode,
    depth: c_int,
) {
    if output.is_null() {
        return;
    }
    let mut cur = node;
    while !cur.is_null() {
        unsafe {
            xmlDebugDumpNode(output, cur, depth);
            cur = (*cur).next;
        }
    }
}

/// Dump an entire document.
///
/// UPSTREAM-PARITY: `xmlDebugDumpDocument()`
#[no_mangle]
pub unsafe extern "C" fn xmlDebugDumpDocument(output: *mut _IO_FILE, doc: *mut _xmlDoc) {
    if output.is_null() || doc.is_null() {
        return;
    }
    unsafe {
        // UPSTREAM-PARITY: xmlCtxtDumpDocHead prints "HTML DOCUMENT" for
        // HTML documents and "DOCUMENT" otherwise (debugXML.c).
        if (*doc).type_ == crate::abi::types::xmlElementType::XML_HTML_DOCUMENT_NODE as c_int {
            libc::fprintf(output, b"HTML DOCUMENT\n\0".as_ptr() as *const c_char);
        } else {
            libc::fprintf(output, b"DOCUMENT\n\0".as_ptr() as *const c_char);
        }
        if !(*doc).version.is_null() {
            libc::fprintf(output, b"version=\0".as_ptr() as *const c_char);
            xmlDebugDumpString(output, (*doc).version as *const u8);
            libc::fprintf(output, b"\n\0".as_ptr() as *const c_char);
        }
        if !(*doc).URL.is_null() {
            libc::fprintf(output, b"URL=\0".as_ptr() as *const c_char);
            // UPSTREAM-PARITY: xmlCtxtDumpDocHead prints the URL through
            // xmlCtxtDumpString, so it is truncated at 40 characters.
            xmlDebugDumpString(output, (*doc).URL as *const u8);
            libc::fprintf(output, b"\n\0".as_ptr() as *const c_char);
        }
        // UPSTREAM-PARITY: the standalone flag is tri-state; the debug dump
        // prints "standalone=true" whenever it is not 0 (unset defaults to
        // true in the parser).
        if (*doc).standalone != 0 {
            libc::fprintf(output, b"standalone=true\n\0".as_ptr() as *const c_char);
        }

        // Document-level namespace declarations (upstream keeps the xml
        // namespace here; other prefixes live on the root element's nsDef).
        if !(*doc).oldNs.is_null() {
            let mut ns = (*doc).oldNs;
            while !ns.is_null() {
                libc::fprintf(output, b"namespace \0".as_ptr() as *const c_char);
                if (*ns).prefix.is_null() {
                    libc::fprintf(output, b" \0".as_ptr() as *const c_char);
                } else {
                    libc::fprintf(output, b"%s\0".as_ptr() as *const c_char, (*ns).prefix);
                }
                libc::fprintf(
                    output,
                    b" href=%s\n\0".as_ptr() as *const c_char,
                    (*ns).href,
                );
                ns = (*ns).next;
            }
        }

        // Dump the internal subset (upstream keeps the DTD as the document's
        // first child; ours is stored on doc->intSubset).
        if !(*doc).intSubset.is_null() {
            xmlDebugDumpNode(output, (*doc).intSubset as *mut _xmlNode, 1);
        }

        // Dump children of doc
        if !(*doc).children.is_null() {
            let mut child = (*doc).children;
            while !child.is_null() {
                xmlDebugDumpNode(output, child, 1);
                child = (*child).next;
            }
        }
    }
}

/// Dump the document head (first few nodes).
///
/// UPSTREAM-PARITY: `xmlDebugDumpDocumentHead()`
#[no_mangle]
pub unsafe extern "C" fn xmlDebugDumpDocumentHead(output: *mut _IO_FILE, doc: *mut _xmlDoc) {
    if output.is_null() || doc.is_null() {
        return;
    }
    unsafe {
        xmlDebugDumpDocument(output, doc);
    }
}

/// Count the number of nodes in a list reachable via next pointers.
///
/// UPSTREAM-PARITY: `xmlLsCountNode()`
#[no_mangle]
pub unsafe extern "C" fn xmlLsCountNode(node: *mut _xmlNode) -> c_int {
    if node.is_null() {
        return 0;
    }
    let mut count: c_int = 0;
    let mut cur = node;
    while !cur.is_null() {
        count += 1;
        unsafe {
            cur = (*cur).next;
        }
    }
    count
}

/// Dump a single node summary (like `ls -l` for nodes).
///
/// UPSTREAM-PARITY: `xmlLsOneNode()`
#[no_mangle]
pub unsafe extern "C" fn xmlLsOneNode(output: *mut _IO_FILE, node: *mut _xmlNode) {
    if output.is_null() || node.is_null() {
        return;
    }
    unsafe {
        match (*node).type_ {
            1 => {
                // XML_ELEMENT_NODE
                libc::fprintf(output, b"E \0".as_ptr() as *const c_char);
                if !(*node).ns.is_null() && !(*(*node).ns).prefix.is_null() {
                    libc::fprintf(
                        output,
                        b"%s:\0".as_ptr() as *const c_char,
                        (*(*node).ns).prefix,
                    );
                }
                xmlDebugDumpString(output, (*node).name as *const u8);
            }
            2 => {
                libc::fprintf(output, b"A \0".as_ptr() as *const c_char);
                xmlDebugDumpString(output, (*node).name as *const u8);
            }
            3 => {
                libc::fprintf(output, b"T \0".as_ptr() as *const c_char);
                if !(*node).content.is_null() {
                    xmlDebugDumpString(output, (*node).content as *const u8);
                }
            }
            4 => {
                libc::fprintf(output, b"C \0".as_ptr() as *const c_char);
            }
            5 => {
                libc::fprintf(output, b"E \0".as_ptr() as *const c_char);
                xmlDebugDumpString(output, (*node).name as *const u8);
            }
            6 => {
                libc::fprintf(output, b"E \0".as_ptr() as *const c_char);
                xmlDebugDumpString(output, (*node).name as *const u8);
            }
            7 => {
                libc::fprintf(output, b"PI \0".as_ptr() as *const c_char);
                xmlDebugDumpString(output, (*node).name as *const u8);
            }
            8 => {
                libc::fprintf(output, b"C \0".as_ptr() as *const c_char);
            }
            9 => {
                libc::fprintf(output, b"D \0".as_ptr() as *const c_char);
            }
            10 => {
                libc::fprintf(output, b"DTD \0".as_ptr() as *const c_char);
            }
            14 => {
                libc::fprintf(output, b"X \0".as_ptr() as *const c_char);
            }
            _ => {
                libc::fprintf(
                    output,
                    b"? (%d)\0".as_ptr() as *const c_char,
                    (*node).type_ as c_int,
                );
            }
        }
        libc::fprintf(output, b"\n\0".as_ptr() as *const c_char);
    }
}

/// Re-export _IO_FILE type for C ABI compatibility.
///
/// This is typically `FILE` in C. On Linux with libc, `_IO_FILE` is the struct
/// behind `FILE *`. We use `*mut _IO_FILE` to match the upstream signature.
pub type _IO_FILE = libc::FILE;

// โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
// Tests
// โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

#[cfg(test)]
mod tests {
    use super::*;
    use crate::abi::allocator::xmlMallocImpl;
    use crate::abi::structs::*;
    use crate::abi::types::xmlChar;
    use crate::xml::tree::*;

    /// Helper: create a simple document for testing.
    unsafe fn create_test_doc() -> *mut _xmlDoc {
        let doc = new_doc(b"1.0\0".as_ptr() as *const xmlChar);
        let root = new_node(ptr::null_mut(), b"root\0".as_ptr() as *const xmlChar);
        doc_set_root_element(doc, root);
        let child = new_child(root, ptr::null_mut(), b"child\0".as_ptr() as *const xmlChar);
        // Set a property using set_prop
        set_prop(
            child,
            b"attr1\0".as_ptr() as *const xmlChar,
            b"value1\0".as_ptr() as *const xmlChar,
        );
        doc
    }

    #[test]
    fn test_xml_bool_to_text() {
        unsafe {
            let t = xmlBoolToText(1);
            assert!(!t.is_null());
            let f = xmlBoolToText(0);
            assert!(!f.is_null());
            // Check that the strings are correct by comparing first byte
            assert_eq!(*t as u8, b't');
            assert_eq!(*f as u8, b'f');
        }
    }

    #[test]
    fn test_debug_dump_string_null() {
        unsafe {
            // Should not crash
            xmlDebugDumpString(ptr::null_mut(), ptr::null());
            // Should print "(null)"
            let f = libc::fmemopen(ptr::null_mut(), 0, b"w\0".as_ptr() as *const c_char);
            if !f.is_null() {
                xmlDebugDumpString(f, ptr::null());
                libc::fclose(f);
            }
        }
    }

    #[test]
    fn test_debug_dump_document_null() {
        unsafe {
            xmlDebugDumpDocument(ptr::null_mut(), ptr::null_mut());
            let f = libc::fmemopen(ptr::null_mut(), 0, b"w\0".as_ptr() as *const c_char);
            if !f.is_null() {
                xmlDebugDumpDocument(f, ptr::null_mut());
                libc::fclose(f);
            }
        }
    }

    #[test]
    fn test_debug_dump_node_null() {
        unsafe {
            xmlDebugDumpNode(ptr::null_mut(), ptr::null_mut(), 0);
            let f = libc::fmemopen(ptr::null_mut(), 0, b"w\0".as_ptr() as *const c_char);
            if !f.is_null() {
                xmlDebugDumpNode(f, ptr::null_mut(), 0);
                libc::fclose(f);
            }
        }
    }

    #[test]
    fn test_ls_count_node() {
        unsafe {
            let node = new_node(ptr::null_mut(), b"test\0".as_ptr() as *const xmlChar);
            assert!(!node.is_null());
            let count = xmlLsCountNode(node);
            assert_eq!(count, 1);

            // Add a sibling
            let sibling = new_node(ptr::null_mut(), b"sibling\0".as_ptr() as *const xmlChar);
            add_sibling(node, sibling);
            let count = xmlLsCountNode(node);
            assert_eq!(count, 2);

            free_node(node);
        }
    }

    #[test]
    fn test_debug_dump_attr_null() {
        unsafe {
            xmlDebugDumpAttr(ptr::null_mut(), ptr::null_mut(), 0);
            let f = libc::fmemopen(ptr::null_mut(), 0, b"w\0".as_ptr() as *const c_char);
            if !f.is_null() {
                xmlDebugDumpAttr(f, ptr::null_mut(), 0);
                libc::fclose(f);
            }
        }
    }

    #[test]
    fn test_debug_dump_attr_list_null() {
        unsafe {
            xmlDebugDumpAttrList(ptr::null_mut(), ptr::null_mut(), 0);
        }
    }

    #[test]
    fn test_debug_dump_node_list_null() {
        unsafe {
            xmlDebugDumpNodeList(ptr::null_mut(), ptr::null_mut(), 0);
        }
    }

    #[test]
    fn test_ls_one_node_null() {
        unsafe {
            xmlLsOneNode(ptr::null_mut(), ptr::null_mut());
            let f = libc::fmemopen(ptr::null_mut(), 0, b"w\0".as_ptr() as *const c_char);
            if !f.is_null() {
                xmlLsOneNode(f, ptr::null_mut());
                libc::fclose(f);
            }
        }
    }

    #[test]
    fn test_dump_document_head_null() {
        unsafe {
            xmlDebugDumpDocumentHead(ptr::null_mut(), ptr::null_mut());
        }
    }

    #[test]
    fn test_ls_count_node_null() {
        assert_eq!(unsafe { xmlLsCountNode(ptr::null_mut()) }, 0);
    }
}