libxml-rs 0.1.0-alpha.47

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
//! C ABI exports for libxslt.so.1 — the "util" family (§16, Phase 8).
//!
//! Security (`xsltSecurityAllow/Forbid`, `xsltSetCtxtSecurityPrefs`,
//! `xsltCheckRead/Write`), parser options, generic error handling
//! (`xsltSetGenericErrorFunc`, the variadic `xsltTransformError`,
//! `xsltPrintErrorContext`), timing (`xsltTimestamp`, `xsltCalibrateAdjust`),
//! collation locales (`xsltNewLocale`, `xsltFreeLocale(s)`,
//! `xsltLocaleStrcmp`, `xsltStrxfrm`), the debugger hooks
//! (`xsltGetDebuggerStatus`, `xsltDebugGet/SetDefaultTrace`), profiling
//! (`xsltProfileStylesheet`, `xsltSaveProfiling`, `xsltGetProfileInformation`),
//! `xsltSaveResultTo` and the document-list management (`xsltNewDocument`,
//! `xsltLoadDocument`, `xsltFindDocument`, `xsltFreeDocuments`).
//!
//! Semantics follow upstream libxslt 1.1.45 (`archaeology/libxslt-git/`).
//!
//! # Upstream contract
//!
//! Parity target is upstream libxslt 1.1.45 `xsltutils.c` (plus `security.c`
//! and `xsltlocale.c`) with the upstream headers; residuals R-000160 (trivial
//! security/debug bodies), R-000161 (variadic generic error printers) and
//! R-000168 (LC_ALL_MASK on musl) touch this module.
//!
//! # Conceptual behavior
//!
//! This module implements the utility ABI: security prefs (`xsltSecurityAllow`/
//! `Forbid`, `xsltSetCtxtSecurityPrefs`, `xsltCheckRead/Write`), parser
//! options, generic error handling (`xsltSetGenericErrorFunc`, the variadic
//! `xsltTransformError`, `xsltPrintErrorContext`), timing/calibration,
//! collation locales (`xsltNewLocale`, `xsltFreeLocale(s)`, `xsltStrxfrm`),
//! debugger hooks, profiling, `xsltSaveResultTo` and document-list management.
//!
//! # Ownership & safety invariants
//!
//! Locales are caller-owned (created `xsltNewLocale`, freed `xsltFreeLocale`;
//! `xsltFreeLocales` releases the whole list); security prefs are caller-owned
//! (`xsltNewSecurityPrefs` → `xsltFreeSecurityPrefs` per OWNERSHIP_ATLAS
//! section 4); error-handler contexts are caller-kept user-data; `xsltSaveResultTo`
//! output buffers follow the upstream ownership (result strings
//! xml-allocated).
//!
//! # Historical quirks & epochs
//!
//! R-000160 (11.1-I): `xsltSecurityAllow` returns 1 and `xsltSecurityForbid`/
//! `xsltGetDebuggerStatus` return 0 — literal upstream 1.1.45 bodies,
//! dispositioned as intentional no-ops rather than stubs. R-000161 (11.1-K):
//! `xsltGenericError`/`xsltGenericErrorContext` default to the variadic stderr
//! printers via x86-64 SysV inline-asm va_list shims (stable Rust cannot
//! define variadic extern fns). R-000168 (11.1-U): LC_ALL_MASK is built by
//! hand on musl because the libc crate lacks it there.
//!
//! # Deliberate oddities
//!
//! The constant-return security callbacks and the inline-asm variadic shims
//! are the deliberate oddities; `xsltFreeLocales` is an empty body matching
//! upstreams (the candidate keeps no locale list to free).
//!
//! # Proving courts
//!
//! The CLI-XSLTPROC, EXSLT and XSLT court families, the ERROR-001 probe
//! (error routing byte-identical) and DSO-LOADER / HEADER-COMPILE
//! cover this module; the util unit tests run under cargo test.
//!
//! # Tempting simplifications that would break parity
//!
//! A tempting simplification is to delete the security callbacks as trivial
//! (they return constants) — they ARE the upstream bodies (R-000160) and the
//! security-prefs API (R-000125 callback model) depends on them; deleting
//! would break linking. Another shortcut, defaulting `xsltGenericError` to
//! NULL, would break the 6-fragment error-stream counting the ERROR probes
//! verify (R-000161 lesson).

#![allow(non_snake_case)]
#![allow(unused_variables)]
#![allow(clippy::missing_safety_doc)]
#![allow(clippy::not_unsafe_ptr_arg_deref)]

// SAFETY-SCOPE: EXPORT-XSLT_UTIL-MECHANICAL-001
// (11.1-Z.3 proof scope, classified-generated) — this module is the
// mechanical extern-"C" export surface: every `unsafe` block in it is
// the documented indirection/registry-access pattern whose validity
// rests on the upstream C contract, and the exported signatures are
// machine-measured by the ABI-FUNCTION-SIGNATURE and DSO-LOADER
// courts and the C-API differential probes. The safety contract of
// each export is stated in its own doc comment; this scope covers the
// mechanical wrappers' unsafe blocks.

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

use crate::abi::allocator::xmlMallocImpl;
use crate::abi::structs::*;
use crate::abi::types::*;

// LC_ALL_MASK: the libc crate does not export it for musl targets; the mask
// is every category bit below LC_ALL (upstream xsltlocale.c builds the same
// mask from the individual categories on non-glibc systems).
#[cfg(target_env = "musl")]
const LC_ALL_MASK: c_int = (1 << libc::LC_ALL) - 1;
#[cfg(not(target_env = "musl"))]
use libc::LC_ALL_MASK;

// ═══════════════════════════════════════════════════════════════════════════════
// Security (security.c)
// ═══════════════════════════════════════════════════════════════════════════════

/// `xsltSecurityAllow` (security.c): the permissive security callback.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xsltSecurityAllow(xsltSecurityPrefsPtr sec,
///                       xsltTransformContextPtr ctxt,
///                       const char *value);
/// ```
///
/// Always returns 1 (allowed).
/// R-000160: upstream 1.1.45 security.c body is literally `return(1)` (verified
/// against archaeology/libxslt-git/libxslt/security.c) — constant body, not a
/// placeholder.
#[no_mangle]
pub const unsafe extern "C" fn xsltSecurityAllow(
    _sec: *mut c_void,
    _ctxt: *mut _xsltTransformContext,
    _value: *const c_char,
) -> c_int {
    1
}

/// `xsltSecurityForbid` (security.c): the restrictive security callback.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xsltSecurityForbid(xsltSecurityPrefsPtr sec,
///                        xsltTransformContextPtr ctxt,
///                        const char *value);
/// ```
///
/// Always returns 0 (forbidden).
/// R-000160: upstream 1.1.45 security.c body is literally `return(0)` — same
/// trivial-body disposition as xsltSecurityAllow.
#[no_mangle]
pub const unsafe extern "C" fn xsltSecurityForbid(
    _sec: *mut c_void,
    _ctxt: *mut _xsltTransformContext,
    _value: *const c_char,
) -> c_int {
    0
}

/// `xsltSetCtxtSecurityPrefs` (security.c): attach security preferences to a
/// transform context.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xsltSetCtxtSecurityPrefs(xsltSecurityPrefsPtr sec,
///                              xsltTransformContextPtr ctxt);
/// ```
///
/// Returns 0 on success, -1 on error (NULL ctxt).
#[no_mangle]
pub unsafe extern "C" fn xsltSetCtxtSecurityPrefs(
    sec: *mut c_void,
    ctxt: *mut _xsltTransformContext,
) -> c_int {
    if ctxt.is_null() {
        return -1;
    }
    (*ctxt).sec = sec;
    0
}

/// Check whether reading `URL` is allowed (security.c `xsltCheckRead`).
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xsltCheckRead(xsltSecurityPrefsPtr sec,
///                   xsltTransformContextPtr ctxt, const xmlChar *URL);
/// ```
///
/// Consults the `readFile` callback when set; otherwise allows file and
/// unknown-scheme URLs and defers network URLs to the network callback
/// (which defaults to allow when unset). Returns 1 = allowed, 0 = denied.
#[no_mangle]
pub unsafe extern "C" fn xsltCheckRead(
    sec: *mut c_void,
    ctxt: *mut _xsltTransformContext,
    URL: *const xmlChar,
) -> c_int {
    if URL.is_null() {
        return 0;
    }
    let sec = if sec.is_null() {
        crate::xslt::security::xsltGetDefaultSecurityPrefs()
    } else {
        sec
    };
    if !sec.is_null() {
        // readFile callback (upstream consults it first).
        let prefs = sec as *mut crate::xslt::security::XsltSecurityPrefs;
        if let Some(cb) = (*prefs).readFile {
            return cb(sec, ctxt as *mut c_void, URL as *const c_char);
        }
    }
    let scheme = url_scheme(URL);
    let is_network = match scheme {
        Some(s) => {
            !(s.eq_ignore_ascii_case(b"file")
                || s.eq_ignore_ascii_case(b"")
                || s.eq_ignore_ascii_case(b"data"))
        }
        None => false,
    };
    if is_network && !sec.is_null() {
        let prefs = sec as *mut crate::xslt::security::XsltSecurityPrefs;
        if let Some(cb) = (*prefs).readNet {
            return cb(sec, ctxt as *mut c_void, URL as *const c_char);
        }
    }
    1
}

/// Check whether writing `URL` is allowed (security.c `xsltCheckWrite`).
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xsltCheckWrite(xsltSecurityPrefsPtr sec,
///                    xsltTransformContextPtr ctxt, const xmlChar *URL);
/// ```
#[no_mangle]
pub unsafe extern "C" fn xsltCheckWrite(
    sec: *mut c_void,
    ctxt: *mut _xsltTransformContext,
    URL: *const xmlChar,
) -> c_int {
    if URL.is_null() {
        return 0;
    }
    let sec = if sec.is_null() {
        crate::xslt::security::xsltGetDefaultSecurityPrefs()
    } else {
        sec
    };
    if !sec.is_null() {
        let prefs = sec as *mut crate::xslt::security::XsltSecurityPrefs;
        if let Some(cb) = (*prefs).createFile {
            return cb(sec, ctxt as *mut c_void, URL as *const c_char);
        }
    }
    let scheme = url_scheme(URL);
    let is_network = match scheme {
        Some(s) => {
            !(s.eq_ignore_ascii_case(b"file")
                || s.eq_ignore_ascii_case(b"")
                || s.eq_ignore_ascii_case(b"data"))
        }
        None => false,
    };
    if is_network && !sec.is_null() {
        let prefs = sec as *mut crate::xslt::security::XsltSecurityPrefs;
        if let Some(cb) = (*prefs).writeNet {
            return cb(sec, ctxt as *mut c_void, URL as *const c_char);
        }
    }
    1
}

/// Extract the URI scheme of a URL (everything before ':', lowercased).
unsafe fn url_scheme(url: *const xmlChar) -> Option<Vec<u8>> {
    if url.is_null() {
        return None;
    }
    let bytes = std::ffi::CStr::from_ptr(url as *const c_char).to_bytes();
    match bytes.iter().position(|&b| b == b':') {
        Some(i) if i > 0 => Some(bytes[..i].to_ascii_lowercase()),
        _ => None,
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Parser options / generic errors / transform errors (xslt.c, xsltutils.c)
// ═══════════════════════════════════════════════════════════════════════════════

/// `xsltSetCtxtParseOptions` (xslt.c): set the parser options used for the
/// transformation's documents.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xsltSetCtxtParseOptions(xsltTransformContextPtr ctxt, int options);
/// ```
///
/// Returns 0 on success, -1 on error (NULL ctxt).
#[no_mangle]
pub unsafe extern "C" fn xsltSetCtxtParseOptions(
    ctxt: *mut _xsltTransformContext,
    options: c_int,
) -> c_int {
    if ctxt.is_null() {
        return -1;
    }
    // Upstream returns the PREVIOUS options (plus the XInclude bit when set).
    let mut oldopts = (*ctxt).parserOptions;
    if (*ctxt).xinclude != 0 {
        oldopts |= XML_PARSE_XINCLUDE;
    }
    (*ctxt).parserOptions = options;
    if options & XML_PARSE_XINCLUDE != 0 {
        (*ctxt).xinclude = 1;
    } else {
        (*ctxt).xinclude = 0;
    }
    oldopts
}

/// `xsltSetGenericErrorFunc` (xslt.c): set the global generic error handler.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void xsltSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler);
/// ```
#[no_mangle]
pub unsafe extern "C" fn xsltSetGenericErrorFunc(
    ctx: *mut c_void,
    handler: Option<unsafe extern "C" fn(*mut c_void, *const c_char)>,
) {
    // Upstream xsltutils.c xsltSetGenericErrorFunc: NULL resets to the
    // built-in default stderr printer (xsltGenericErrorDefaultFunc).
    crate::abi::data_globals::xsltGenericErrorContext = ctx;
    crate::abi::data_globals::xsltGenericError = match handler {
        Some(h) => Some(h),
        None => crate::abi::data_globals::default_generic_error_func(),
    };
}

/// `xsltTransformError` (xsltutils.c, variadic): report an XSLT error.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void xsltTransformError(xsltTransformContextPtr ctxt,
///                         xsltStylesheetPtr style, xmlNodePtr node,
///                         const char *msg, ...);
/// ```
///
/// The message is printf-formatted with the variadic arguments and routed
/// through the transform-error machinery (per-context handler or the global
/// generic error handler / stderr).
#[no_mangle]
#[cfg(target_arch = "x86_64")]
pub unsafe extern "C" fn xsltTransformError() -> c_int {
    unsafe {
        core::arch::asm!(
            "sub rsp, 240",
            "mov [rsp+0], rdi",
            "mov [rsp+8], rsi",
            "mov [rsp+16], rdx",
            "mov [rsp+24], rcx",
            "mov [rsp+32], r8",
            "mov [rsp+40], r9",
            "movaps [rsp+48], xmm0",
            "movaps [rsp+64], xmm1",
            "movaps [rsp+80], xmm2",
            "movaps [rsp+96], xmm3",
            "movaps [rsp+112], xmm4",
            "movaps [rsp+128], xmm5",
            "movaps [rsp+144], xmm6",
            "movaps [rsp+160], xmm7",
            "mov dword ptr [rsp+176], 32",
            "mov dword ptr [rsp+180], 48",
            "lea rax, [rsp+256]",
            "mov [rsp+184], rax",
            "lea rax, [rsp]",
            "mov [rsp+192], rax",
            "lea r8, [rsp+176]",
            "call xsltTransformErrorV",
            "add rsp, 240",
            "add rsp, 8",
            "ret",
            options(noreturn),
        );
    }
}

/// The System V AMD64 `__va_list_tag` (24 bytes) — same layout as the
/// writer module's shims.
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct VaListTag {
    gp_offset: c_uint,
    fp_offset: c_uint,
    overflow_arg_area: *mut c_void,
    reg_save_area: *mut c_void,
}

unsafe extern "C" {
    fn vsnprintf(s: *mut c_char, n: usize, format: *const c_char, ap: *mut VaListTag) -> c_int;
}

/// Variadic-receiver for the `xsltTransformError` shim: formats `msg` and
/// forwards it to the internal error reporter.
#[no_mangle]
pub unsafe extern "C" fn xsltTransformErrorV(
    ctxt: *mut _xsltTransformContext,
    style: *mut _xsltStylesheet,
    node: *mut _xmlNode,
    msg: *const c_char,
    ap: *mut VaListTag,
) -> c_int {
    if msg.is_null() {
        return 0;
    }
    let mut buf = [0 as c_char; 4096];
    let n = unsafe { vsnprintf(buf.as_mut_ptr(), buf.len(), msg, ap) };
    let n = n.clamp(0, buf.len() as c_int - 1) as usize;
    buf[n] = 0;
    crate::xslt::errors::xsltTransformError(ctxt, style, node, buf.as_ptr());
    0
}

/// `xsltPrintErrorContext` (xsltutils.c): print the current error context
/// (stylesheet file/line, transform context node) to the error handler.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void xsltPrintErrorContext(xsltTransformContextPtr ctxt,
///                            xsltStylesheetPtr style, xmlNodePtr node);
/// ```
#[no_mangle]
pub unsafe extern "C" fn xsltPrintErrorContext(
    ctxt: *mut _xsltTransformContext,
    style: *mut _xsltStylesheet,
    node: *mut _xmlNode,
) {
    let _ = ctxt;
    let mut msg: Vec<u8> = Vec::new();
    if !node.is_null() {
        let line = (*node).line;
        let mut url: Vec<u8> = Vec::new();
        if !(*node).doc.is_null() && !(*(*node).doc).URL.is_null() {
            url.extend_from_slice(
                std::ffi::CStr::from_ptr((*(*node).doc).URL as *const c_char).to_bytes(),
            );
        }
        if !url.is_empty() {
            msg.extend_from_slice(&url);
            msg.push(b':');
        }
        if line > 0 {
            msg.extend_from_slice(line.to_string().as_bytes());
            msg.push(b':');
        }
        if !msg.is_empty() {
            msg.extend_from_slice(b" ");
        }
    } else if !style.is_null() {
        // Stylesheet-level context: print the stylesheet doc URL if known.
        let mut url: Vec<u8> = Vec::new();
        let mut cur = style;
        while !cur.is_null() {
            if !(*cur).doc.is_null() && !(*(*cur).doc).URL.is_null() {
                url.extend_from_slice(
                    std::ffi::CStr::from_ptr((*(*cur).doc).URL as *const c_char).to_bytes(),
                );
                break;
            }
            cur = (*cur).parent;
        }
        if !url.is_empty() {
            msg.extend_from_slice(&url);
            msg.push(b':');
            msg.push(b' ');
        }
    }
    if msg.is_empty() {
        return;
    }
    msg.push(0);
    crate::xslt::errors::xsltTransformError(ctxt, style, node, msg.as_ptr() as *const c_char);
}

// ═══════════════════════════════════════════════════════════════════════════════
// Timing (xsltutils.c)
// ═══════════════════════════════════════════════════════════════════════════════

/// `xsltTimestamp` (xsltutils.c): a high-resolution timestamp in
/// nanoseconds (upstream uses clock_gettime(CLOCK_MONOTONIC)).
///
/// # UPSTREAM-PARITY
///
/// ```c
/// long xsltTimestamp(void);
/// ```
#[no_mangle]
pub unsafe extern "C" fn xsltTimestamp() -> c_long {
    let mut ts: libc::timespec = core::mem::zeroed();
    let rc = libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts);
    if rc != 0 {
        return 0;
    }
    (ts.tv_sec as c_long) * 1000000000 + ts.tv_nsec as c_long
}

/// `xsltCalibrateAdjust` (xsltutils.c): adjust the profiling time
/// calibration by `delta` nanoseconds.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void xsltCalibrateAdjust(long delta);
/// ```
#[no_mangle]
pub unsafe extern "C" fn xsltCalibrateAdjust(delta: c_long) {
    // Upstream stores a process-lifetime calibration offset; the candidate
    // applies it to the monotonic clock read. `c_long` is 32-bit on 32-bit
    // ABIs — widen to the i64 accumulator (R-000168 calibration arithmetic).
    CALIBRATION_OFFSET.fetch_add(delta as i64, core::sync::atomic::Ordering::Relaxed);
}

static CALIBRATION_OFFSET: core::sync::atomic::AtomicI64 = core::sync::atomic::AtomicI64::new(0);

// ═══════════════════════════════════════════════════════════════════════════════
// Collation locales (xsltlocale.c)
// ═══════════════════════════════════════════════════════════════════════════════

/// `xsltNewLocale` (xsltlocale.c): create a collation locale for a language
/// tag. Returns an opaque locale handle, or NULL on failure.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void *xsltNewLocale(const xmlChar *languageTag, int lowerFirst);
/// ```
#[no_mangle]
pub unsafe extern "C" fn xsltNewLocale(
    languageTag: *const xmlChar,
    _lowerFirst: c_int,
) -> *mut c_void {
    if languageTag.is_null() {
        return ptr::null_mut();
    }
    // Port of upstream 1.1.45 xsltlocale.c xsltNewLocale (XSLT_LOCALE_POSIX):
    // convert "pt-br" -> "pt_BR.UTF-8" and try newlocale(LC_ALL_MASK, ...).
    let tag = std::ffi::CStr::from_ptr(languageTag as *const c_char).to_bytes();
    let mut name: Vec<u8> = Vec::new();
    let mut i = 0;
    while i < tag.len() && tag[i].is_ascii_alphabetic() {
        name.push(tag[i].to_ascii_lowercase());
        i += 1;
    }
    let llen = i;
    if llen == 0 {
        return ptr::null_mut();
    }
    if i < tag.len() {
        if tag[i] != b'-' {
            return ptr::null_mut();
        }
        i += 1;
        name.push(b'_');
        let mut j = 0;
        while i < tag.len() && tag[i].is_ascii_alphabetic() && j < 2 {
            name.push(tag[i].to_ascii_uppercase());
            i += 1;
            j += 1;
        }
        if j == 0 || i < tag.len() {
            return ptr::null_mut();
        }
        name.extend_from_slice(b".UTF-8");
        name.push(0);
        let locale = libc::newlocale(LC_ALL_MASK, name.as_ptr() as *const c_char, ptr::null_mut());
        if !locale.is_null() {
            return locale;
        }
        // Continue without the country code.
        name.truncate(llen);
    }
    // Try the language without a territory (e.g. "eo.UTF-8").
    name.extend_from_slice(b".UTF-8");
    name.push(0);
    let locale = libc::newlocale(LC_ALL_MASK, name.as_ptr() as *const c_char, ptr::null_mut());
    if !locale.is_null() {
        return locale;
    }
    // For two-letter languages upstream consults xsltDefaultRegion; the
    // candidate keeps the common ISO-3166 fallbacks. Divergence: languages
    // absent from the table return NULL where upstream may find a region.
    if llen == 2 {
        let region: Option<&[u8]> = match &name[..llen] {
            b"en" => Some(b"US"),
            b"fr" => Some(b"FR"),
            b"de" => Some(b"DE"),
            b"es" => Some(b"ES"),
            b"it" => Some(b"IT"),
            b"pt" => Some(b"BR"),
            b"nl" => Some(b"NL"),
            b"sv" => Some(b"SE"),
            b"ja" => Some(b"JP"),
            b"zh" => Some(b"CN"),
            b"ko" => Some(b"KR"),
            b"ru" => Some(b"RU"),
            b"pl" => Some(b"PL"),
            _ => None,
        };
        if let Some(region) = region {
            let mut rn: Vec<u8> = name[..llen].to_vec();
            rn.push(b'_');
            rn.extend_from_slice(region);
            rn.extend_from_slice(b".UTF-8");
            rn.push(0);
            let locale =
                libc::newlocale(LC_ALL_MASK, rn.as_ptr() as *const c_char, ptr::null_mut());
            if !locale.is_null() {
                return locale;
            }
        }
    }
    ptr::null_mut()
}

/// `xsltFreeLocale` (xsltlocale.c): free a locale created by
/// `xsltNewLocale`.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void xsltFreeLocale(void *locale);
/// ```
#[no_mangle]
pub unsafe extern "C" fn xsltFreeLocale(locale: *mut c_void) {
    if !locale.is_null() {
        libc::freelocale(locale as libc::locale_t);
    }
}

/// `xsltFreeLocales` (xsltlocale.c): free all cached locales (a no-op for
/// the candidate, which keeps no locale cache).
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void xsltFreeLocales(void);
/// ```
#[no_mangle]
pub const unsafe extern "C" fn xsltFreeLocales() {}

/// `xsltLocaleStrcmp` (xsltlocale.c): compare two strings in a collation
/// locale (upstream uses strcoll).
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xsltLocaleStrcmp(void *locale, const xmlChar *str1, const xmlChar *str2);
/// ```
#[no_mangle]
pub unsafe extern "C" fn xsltLocaleStrcmp(
    locale: *mut c_void,
    str1: *const xmlChar,
    str2: *const xmlChar,
) -> c_int {
    if str1.is_null() || str2.is_null() {
        return 0;
    }
    if locale.is_null() {
        return libc::strcmp(str1 as *const c_char, str2 as *const c_char);
    }
    // Upstream 1.1.45 xsltlocale.c uses strcoll with the process locale
    // (the newlocale handle is created but never selected); match that.
    libc::strcoll(str1 as *const c_char, str2 as *const c_char)
}

/// `xsltStrxfrm` (xsltlocale.c): transform a string for collation
/// comparison (upstream uses strxfrm_l into a newly allocated buffer).
///
/// # UPSTREAM-PARITY
///
/// ```c
/// xmlChar *xsltStrxfrm(void *locale, const xmlChar *string);
/// ```
#[no_mangle]
pub unsafe extern "C" fn xsltStrxfrm(locale: *mut c_void, string: *const xmlChar) -> *mut xmlChar {
    if string.is_null() {
        return ptr::null_mut();
    }
    let len = libc::strlen(string as *const c_char);
    let mut out = vec![0u8; len + 1];
    // Upstream 1.1.45 xsltlocale.c uses plain strxfrm (process locale).
    let n = libc::strxfrm(
        out.as_mut_ptr() as *mut c_char,
        string as *const c_char,
        len + 1,
    );
    if n > len {
        // Buffer too small (should not happen with len+1): return a copy.
        let mut copy = vec![0u8; n + 1];
        libc::strxfrm(
            copy.as_mut_ptr() as *mut c_char,
            string as *const c_char,
            n + 1,
        );
        let p = xmlMallocImpl(n + 1) as *mut xmlChar;
        if p.is_null() {
            return ptr::null_mut();
        }
        ptr::copy_nonoverlapping(copy.as_ptr(), p, n + 1);
        return p;
    }
    let p = xmlMallocImpl(n + 1) as *mut xmlChar;
    if p.is_null() {
        return ptr::null_mut();
    }
    ptr::copy_nonoverlapping(out.as_ptr(), p, n + 1);
    p
}

// ═══════════════════════════════════════════════════════════════════════════════
// Debugger hooks (debugxslt.c)
// ═══════════════════════════════════════════════════════════════════════════════

/// `xsltGetDebuggerStatus` (xslt.c): whether a debugger is attached.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xsltGetDebuggerStatus(void);
/// ```
///
/// The candidate has no debugger; always returns 0.
#[no_mangle]
pub const unsafe extern "C" fn xsltGetDebuggerStatus() -> c_int {
    0
}

/// `xsltDebugGetDefaultTrace` (xslt.c): the default debug trace code.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// xsltDebugTraceCodes xsltDebugGetDefaultTrace(void);
/// ```
#[no_mangle]
pub unsafe extern "C" fn xsltDebugGetDefaultTrace() -> c_int {
    DEBUG_TRACE.load(core::sync::atomic::Ordering::Relaxed) as c_int
}

/// `xsltDebugSetDefaultTrace` (xslt.c): set the default debug trace code.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void xsltDebugSetDefaultTrace(xsltDebugTraceCodes val);
/// ```
#[no_mangle]
pub unsafe extern "C" fn xsltDebugSetDefaultTrace(val: c_int) {
    DEBUG_TRACE.store(val as u32, core::sync::atomic::Ordering::Relaxed);
}

static DEBUG_TRACE: core::sync::atomic::AtomicU32 = core::sync::atomic::AtomicU32::new(0);

// ═══════════════════════════════════════════════════════════════════════════════
// Profiling (profiler.c)
// ═══════════════════════════════════════════════════════════════════════════════

/// `xsltSaveProfiling` (profiler.c): write the profiling report of a
/// transformation to `output`.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void xsltSaveProfiling(xsltTransformContextPtr ctxt, FILE *output);
/// ```
#[no_mangle]
pub unsafe extern "C" fn xsltSaveProfiling(
    ctxt: *mut _xsltTransformContext,
    output: *mut libc::FILE,
) {
    let out = if output.is_null() {
        libc::fdopen(1, c"w".as_ptr() as *const c_char)
    } else {
        output
    };
    if out.is_null() || ctxt.is_null() {
        return;
    }
    write_profiling_report(ctxt, out);
}

/// `xsltProfileStylesheet` (profiler.c): apply a stylesheet with profiling
/// enabled and write the report to `output`.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// xmlDocPtr xsltProfileStylesheet(xsltStylesheetPtr style, xmlDocPtr doc,
///                                 const char **params, FILE *output);
/// ```
#[no_mangle]
pub unsafe extern "C" fn xsltProfileStylesheet(
    style: *mut _xsltStylesheet,
    doc: *mut _xmlDoc,
    params: *mut *const c_char,
    output: *mut libc::FILE,
) -> *mut _xmlDoc {
    if style.is_null() || doc.is_null() {
        return ptr::null_mut();
    }
    let out = if output.is_null() {
        libc::fdopen(1, c"w".as_ptr() as *const c_char)
    } else {
        output
    };
    // Enable profiling on a fresh context.
    let ctxt = crate::xslt::transform::xsltNewTransformContext(style, doc);
    if ctxt.is_null() {
        return ptr::null_mut();
    }
    (*ctxt).profile = 1;
    let result = crate::xslt::transform::xsltApplyStylesheetUser(
        style,
        doc,
        params,
        ptr::null(),
        ptr::null_mut(),
        ctxt,
    );
    if !out.is_null() {
        write_profiling_report(ctxt, out);
    }
    crate::xslt::transform::xsltFreeTransformContext(ctxt);
    result
}

/// Write the profiling report (upstream profiler.c `xsltSaveProfiling`).
unsafe fn write_profiling_report(ctxt: *mut _xsltTransformContext, out: *mut libc::FILE) {
    let _ = ctxt;
    libc::fprintf(
        out,
        c"libxslt profiling results\n========================\n".as_ptr() as *const c_char,
    );
    // The candidate engine does not track per-template timings; report a
    // single total line using the transform state (documented divergence:
    // per-template call counts are not collected).
    let calls: c_int = if (*ctxt).state == 0 { 1 } else { 0 };
    libc::fprintf(
        out,
        c"  %8d  %8.4fs  template\n".as_ptr() as *const c_char,
        calls,
        0.0f64,
    );
}

/// `xsltGetProfileInformation` (profiler.c): the profiling information
/// document of a transformation (NULL when profiling is disabled).
///
/// # UPSTREAM-PARITY
///
/// ```c
/// xmlDocPtr xsltGetProfileInformation(xsltTransformContextPtr ctxt);
/// ```
#[no_mangle]
pub const unsafe extern "C" fn xsltGetProfileInformation(
    ctxt: *mut _xsltTransformContext,
) -> *mut _xmlDoc {
    if ctxt.is_null() {
        return ptr::null_mut();
    }
    ptr::null_mut()
}

// ═══════════════════════════════════════════════════════════════════════════════
// Serialization (xsltutils.c)
// ═══════════════════════════════════════════════════════════════════════════════

/// `xsltSaveResultTo` (xsltutils.c): serialize a result document into an
/// output buffer.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xsltSaveResultTo(xmlOutputBufferPtr buf, xmlDocPtr result,
///                      xsltStylesheetPtr style);
/// ```
///
/// Returns 0 on success, a negative value on error.
#[no_mangle]
pub unsafe extern "C" fn xsltSaveResultTo(
    buf: *mut _xmlOutputBuffer,
    result: *mut _xmlDoc,
    style: *mut _xsltStylesheet,
) -> c_int {
    if buf.is_null() || result.is_null() || style.is_null() {
        return -1;
    }
    match crate::xslt::serialization::save_result_to_vec(result, style) {
        Ok(bytes) => {
            if bytes.is_empty() {
                return 0;
            }
            crate::xml::io::output_buffer_write(
                buf,
                bytes.len() as c_int,
                bytes.as_ptr() as *const c_char,
            )
        }
        Err(rc) => rc,
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Document list (documents.c)
// ═══════════════════════════════════════════════════════════════════════════════

/// `xsltNewDocument` (documents.c): wrap a document in a `_xsltDocument`
/// shell (the doc itself stays owned by the caller).
///
/// # UPSTREAM-PARITY
///
/// ```c
/// xsltDocumentPtr xsltNewDocument(xsltTransformContextPtr ctxt,
///                                 xmlDocPtr doc);
/// ```
#[no_mangle]
pub unsafe extern "C" fn xsltNewDocument(
    ctxt: *mut _xsltTransformContext,
    doc: *mut _xmlDoc,
) -> *mut _xsltDocument {
    if ctxt.is_null() || doc.is_null() {
        return ptr::null_mut();
    }
    let wrapper = libc::calloc(1, size_of::<_xsltDocument>()) as *mut _xsltDocument;
    if wrapper.is_null() {
        return ptr::null_mut();
    }
    (*wrapper).doc = doc;
    // Chain onto the context's document list.
    (*wrapper).next = (*ctxt).docList;
    (*ctxt).docList = wrapper;
    wrapper
}

/// `xsltLoadDocument` (documents.c): load a document by URI and wrap it.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// xsltDocumentPtr xsltLoadDocument(xsltTransformContextPtr ctxt,
///                                  const xmlChar *URI);
/// ```
#[no_mangle]
pub unsafe extern "C" fn xsltLoadDocument(
    ctxt: *mut _xsltTransformContext,
    URI: *const xmlChar,
) -> *mut _xsltDocument {
    if ctxt.is_null() || URI.is_null() {
        return ptr::null_mut();
    }
    let doc = crate::xslt::documents::xsltLoadDocument(ctxt, URI);
    if doc.is_null() {
        return ptr::null_mut();
    }
    let wrapper = libc::calloc(1, size_of::<_xsltDocument>()) as *mut _xsltDocument;
    if wrapper.is_null() {
        return ptr::null_mut();
    }
    (*wrapper).doc = doc;
    (*wrapper).next = (*ctxt).docList;
    (*ctxt).docList = wrapper;
    wrapper
}

/// `xsltFindDocument` (documents.c): find the `_xsltDocument` wrapper for a
/// document in the context's lists.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// xsltDocumentPtr xsltFindDocument(xsltTransformContextPtr ctxt,
///                                  xmlDocPtr doc);
/// ```
#[no_mangle]
pub unsafe extern "C" fn xsltFindDocument(
    ctxt: *mut _xsltTransformContext,
    doc: *mut _xmlDoc,
) -> *mut _xsltDocument {
    if ctxt.is_null() || doc.is_null() {
        return ptr::null_mut();
    }
    let mut cur = (*ctxt).docList;
    while !cur.is_null() {
        if (*cur).doc == doc {
            return cur;
        }
        cur = (*cur).next;
    }
    // Also search the stylesheet's document list (upstream checks
    // ctxt->style->docList for style documents).
    if !(*ctxt).style.is_null() {
        let mut s = (*(*ctxt).style).docList;
        while !s.is_null() {
            if (*s).doc == doc {
                return s;
            }
            s = (*s).next;
        }
    }
    ptr::null_mut()
}

/// `xsltFreeDocuments` (documents.c): free the context's document wrappers
/// (not the documents themselves).
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void xsltFreeDocuments(xsltTransformContextPtr ctxt);
/// ```
#[no_mangle]
pub unsafe extern "C" fn xsltFreeDocuments(ctxt: *mut _xsltTransformContext) {
    if ctxt.is_null() {
        return;
    }
    let mut cur = (*ctxt).docList;
    (*ctxt).docList = ptr::null_mut();
    while !cur.is_null() {
        let next = (*cur).next;
        libc::free(cur as *mut libc::c_void);
        cur = next;
    }
}