libxml-rs 0.1.0-alpha.23

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. 1135 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
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
//! Exported C data globals — the classic libxml2/libxslt public variables
//! (11.1-G data-ABI closure, residual R-000135).
//!
//! Downstream C code reads and writes these symbols directly
//! (e.g. `xmlDoValidityCheckingDefaultValue = 1;`), so they must exist with
//! upstream names, types and defaults. The parser-default accessors in
//! `src/xml/globals/mod.rs` read and write the SAME statics (single source
//! of truth): a C write is immediately observable by the candidate parser.
//!
//! Defaults match upstream `globals.c` (libxml2 2.15.3) and `xslt.c`
//! (libxslt 1.1.45).
//!
//! # SAFETY
//!
//! `static mut` globals are unsafe to touch from Rust; every access goes
//! through the accessor functions in `crate::xml::globals` (or directly
//! here with an explicit safety note). C accesses are inherently racy in
//! upstream too — upstream documents these globals as deprecated and
//! not thread-safe.

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

use crate::abi::callbacks::{xmlGenericErrorFunc, xmlStructuredErrorFunc};
use crate::abi::types::xmlChar;

// ═══════════════════════════════════════════════════════════════════════════════
// Parser defaults (upstream globals.c)
// ═══════════════════════════════════════════════════════════════════════════════

/// `int xmlDoValidityCheckingDefaultValue` (default 0)
#[no_mangle]
pub static mut xmlDoValidityCheckingDefaultValue: c_int = 0;

/// `int xmlGetWarningsDefaultValue` (default 1)
#[no_mangle]
pub static mut xmlGetWarningsDefaultValue: c_int = 1;

/// `int xmlLoadExtDtdDefaultValue` (default 0)
#[no_mangle]
pub static mut xmlLoadExtDtdDefaultValue: c_int = 0;

/// `int xmlPedanticParserDefaultValue` (default 0)
#[no_mangle]
pub static mut xmlPedanticParserDefaultValue: c_int = 0;

/// `int xmlLineNumbersDefaultValue` (default 1 — upstream globals.c
/// `xmlLineNumbersDefaultValueThrDef = 1`)
#[no_mangle]
pub static mut xmlLineNumbersDefaultValue: c_int = 1;

/// `int xmlKeepBlanksDefaultValue` (default 1)
#[no_mangle]
pub static mut xmlKeepBlanksDefaultValue: c_int = 1;

/// `int xmlSubstituteEntitiesDefaultValue` (default 0)
#[no_mangle]
pub static mut xmlSubstituteEntitiesDefaultValue: c_int = 0;

/// `int xmlParserDebugEntities` (default 0)
#[no_mangle]
pub static mut xmlParserDebugEntities: c_int = 0;

/// `int xmlIndentTreeOutput` (default 1 — upstream globals.c
/// `xmlIndentTreeOutputThrDef = 1`)
#[no_mangle]
pub static mut xmlIndentTreeOutput: c_int = 1;

/// `const xmlChar *xmlTreeIndentString` (default "  " — upstream globals.c
/// `xmlTreeIndentStringThrDef = "  "`)
#[no_mangle]
pub static mut xmlTreeIndentString: *const xmlChar = {
    static S: [u8; 3] = *b"  \0";
    S.as_ptr()
};

/// `int xmlSaveNoEmptyTags` (default 0)
#[no_mangle]
pub static mut xmlSaveNoEmptyTags: c_int = 0;

/// Upstream `xmlRegisterNodeFunc xmlRegisterNodeDefaultValue` (default NULL)
#[no_mangle]
pub static mut xmlRegisterNodeDefaultValue: Option<
    unsafe extern "C" fn(*mut crate::abi::structs::_xmlNode),
> = None;

/// Upstream static `xmlRegisterCallbacks` (tree.c): the gate that arms the
/// node register/deregister hooks once a callback has been registered.
pub static XML_REGISTER_CALLBACKS: core::sync::atomic::AtomicBool =
    core::sync::atomic::AtomicBool::new(false);

/// Upstream `xmlRegisterNodeCallback(node)` — invoke the registered node
/// hook, gated by `xmlRegisterCallbacks` (tree.c).
///
/// # SAFETY
///
/// - `node` must be a valid, fully-initialised node or NULL.
pub fn register_node_hook(node: *mut crate::abi::structs::_xmlNode) {
    if !XML_REGISTER_CALLBACKS.load(core::sync::atomic::Ordering::Relaxed) {
        return;
    }
    let hook = unsafe { xmlRegisterNodeDefaultValue };
    if let Some(h) = hook {
        if !node.is_null() {
            // SAFETY: the hook is a valid C callback registered by the user.
            unsafe { h(node) };
        }
    }
}

/// Upstream `xmlDeregisterNodeCallback(node)` — invoke the registered node
/// deregister hook, gated by `xmlRegisterCallbacks` (tree.c).
///
/// # SAFETY
///
/// - `node` must be a valid node about to be freed, or NULL.
pub fn deregister_node_hook(node: *mut crate::abi::structs::_xmlNode) {
    if !XML_REGISTER_CALLBACKS.load(core::sync::atomic::Ordering::Relaxed) {
        return;
    }
    let hook = unsafe { xmlDeregisterNodeDefaultValue };
    if let Some(h) = hook {
        if !node.is_null() {
            // SAFETY: the hook is a valid C callback registered by the user.
            unsafe { h(node) };
        }
    }
}

/// `xmlDeregisterNodeFunc xmlDeregisterNodeDefaultValue` (default NULL)
#[no_mangle]
pub static mut xmlDeregisterNodeDefaultValue: Option<
    unsafe extern "C" fn(*mut crate::abi::structs::_xmlNode),
> = None;

/// `const char *xmlParserVersion` — matched to the system oracle build
/// (libxml2 2.15.3 GIT build: LIBXML_VERSION_STRING "21503" plus the
/// upstream version extra "-GITv2.15.3").
///
/// SAFETY: the pointed-to string is a static, immutable, null-terminated
/// literal; `static mut` is used because C raw pointers are not `Sync`.
/// Reads/writes of the pointer itself are racy only if C code mutates it
/// (upstream treats it as a constant).
#[no_mangle]
pub static mut xmlParserVersion: *const c_char = {
    static V: [u8; 17] = *b"21503-GITv2.15.3\0";
    V.as_ptr() as *const c_char
};

/// `int xmlParserMaxDepth` (default 256)
#[no_mangle]
pub static mut xmlParserMaxDepth: c_int = 256;

// ═══════════════════════════════════════════════════════════════════════════════
// Buffer globals (upstream tree.h / xmlIO.c)
// ═══════════════════════════════════════════════════════════════════════════════

/// `int xmlDefaultBufferSize` (default 4096)
#[no_mangle]
pub static mut xmlDefaultBufferSize: c_int = 4096;

/// `xmlBufferAllocationScheme xmlBufferAllocScheme` (default XML_BUFFER_ALLOC_EXACT = 1;
/// upstream globals.c `xmlBufferAllocSchemeThrDef = XML_BUFFER_ALLOC_EXACT`)
#[no_mangle]
pub static mut xmlBufferAllocScheme: c_int = 1;

// ═══════════════════════════════════════════════════════════════════════════════
// Error callback globals (upstream xmlerror.h)
// ═══════════════════════════════════════════════════════════════════════════════

/// `xmlGenericErrorFunc xmlGenericError` — the generic error callback.
///
/// Upstream defaults to `xmlGenericErrorDefaultFunc` (a variadic stderr
/// printer, error.c); the candidate's shim below reproduces it, so a
/// freshly-initialized library routes errors to stderr exactly like upstream.
#[cfg(target_arch = "x86_64")]
#[no_mangle]
pub static mut xmlGenericError: Option<xmlGenericErrorFunc> = Some(XML_GENERIC_ERROR_DEFAULT);

#[cfg(not(target_arch = "x86_64"))]
#[no_mangle]
pub static mut xmlGenericError: Option<xmlGenericErrorFunc> = None;

/// `void *xmlGenericErrorContext` — context for the generic error callback.
#[no_mangle]
pub static mut xmlGenericErrorContext: *mut c_void = core::ptr::null_mut();

/// `xmlStructuredErrorFunc xmlStructuredError` — the structured error callback.
#[no_mangle]
pub static mut xmlStructuredError: Option<xmlStructuredErrorFunc> = None;

/// `void *xmlStructuredErrorContext` — context for the structured callback.
#[no_mangle]
pub static mut xmlStructuredErrorContext: *mut c_void = core::ptr::null_mut();

// ═══════════════════════════════════════════════════════════════════════════════
// Variadic default error handlers (upstream error.c / xsltutils.c)
// ═══════════════════════════════════════════════════════════════════════════════
//
// Upstream `xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...)`
// prints the formatted message to `xmlGenericErrorContext` (stderr when
// NULL). Stable Rust cannot define a variadic extern fn body, so the ABI
// entry is an x86_64 SysV inline-asm shim that materialises the caller's
// register/stack arguments into a `va_list` and forwards to a non-variadic
// receiver — the same pattern as `xsltTransformError`
// (exports_xslt_util.rs) and the writer's `vfmt_shim!`. Neither default
// function is a dynamic export upstream (both are internal); the exported
// data globals merely point at them.

/// System V AMD64 `__va_list_tag` (24 bytes) — same layout as the writer's
/// shims and `exports_xslt_util.rs`.
#[cfg(target_arch = "x86_64")]
#[repr(C)]
#[derive(Clone, Copy)]
struct VaListTag {
    gp_offset: c_uint,
    fp_offset: c_uint,
    overflow_arg_area: *mut c_void,
    reg_save_area: *mut c_void,
}

#[cfg(target_arch = "x86_64")]
unsafe extern "C" {
    fn vfprintf(stream: *mut c_void, format: *const c_char, ap: *mut VaListTag) -> c_int;
}

/// The `stderr` FILE* — glibc exports the `stderr` data object, an
/// 8-byte pointer variable whose value is `&_IO_2_1_stderr_`. Upstream
/// `xmlGenericErrorDefaultFunc` defaults the error context to `stderr`;
/// using the real stdio object (unbuffered, fd-2 relative) keeps writes
/// byte-exact and honors fd-2 redirection, unlike a private `fdopen(2)`
/// FILE* which is fully buffered and lands at exit on whatever fd 2 then
/// points to.
#[cfg(target_arch = "x86_64")]
unsafe fn stderr_file() -> *mut c_void {
    extern "C" {
        static stderr: *mut c_void;
    }
    unsafe { stderr }
}

/// Variadic receiver for the `xmlGenericErrorDefaultFunc` shim (upstream
/// error.c semantics: default the context to stderr, then `vfprintf`).
#[cfg(target_arch = "x86_64")]
#[no_mangle]
pub unsafe extern "C" fn xmlGenericErrorDefaultFuncV(
    _ctx: *mut c_void,
    msg: *const c_char,
    ap: *mut VaListTag,
) -> c_int {
    unsafe {
        if crate::abi::data_globals::xmlGenericErrorContext.is_null() {
            crate::abi::data_globals::xmlGenericErrorContext = stderr_file();
        }
        let stream = crate::abi::data_globals::xmlGenericErrorContext;
        if msg.is_null() || stream.is_null() {
            return 0;
        }
        vfprintf(stream, msg, ap)
    }
}

/// `xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...)` — the
/// upstream default generic error handler. Not exported dynamically (matches
/// upstream, where the symbol is internal).
///
/// 2 fixed args (ctx=rdi, msg=rsi) → `gp_offset` 16; the va_list pointer is
/// passed as the 3rd arg (rdx) of the receiver.
#[cfg(target_arch = "x86_64")]
pub unsafe extern "C" fn xmlGenericErrorDefaultFunc() -> 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], 16",
            "mov dword ptr [rsp+180], 48",
            "lea rax, [rsp+256]",
            "mov [rsp+184], rax",
            "lea rax, [rsp]",
            "mov [rsp+192], rax",
            "lea rdx, [rsp+176]",
            "call xmlGenericErrorDefaultFuncV",
            "add rsp, 240",
            "add rsp, 8",
            "ret",
            options(noreturn),
        );
    }
}

/// Default value of the exported `xmlGenericError` data global.
#[cfg(target_arch = "x86_64")]
const XML_GENERIC_ERROR_DEFAULT: xmlGenericErrorFunc = unsafe {
    // SAFETY: the shim and the function-pointer type have identical ABI
    // (a code pointer); the declared arity is a Rust-side fiction required
    // to store a variadic entry in the non-variadic pointer type.
    core::mem::transmute::<
        unsafe extern "C" fn() -> c_int,
        unsafe extern "C" fn(*mut c_void, *const c_char),
    >(xmlGenericErrorDefaultFunc)
};

/// Variadic receiver for the `xsltGenericErrorDefaultFunc` shim (upstream
/// xsltutils.c semantics: default the context to stderr, then `vfprintf`).
#[cfg(target_arch = "x86_64")]
#[no_mangle]
pub unsafe extern "C" fn xsltGenericErrorDefaultFuncV(
    _ctx: *mut c_void,
    msg: *const c_char,
    ap: *mut VaListTag,
) -> c_int {
    unsafe {
        if crate::abi::data_globals::xsltGenericErrorContext.is_null() {
            crate::abi::data_globals::xsltGenericErrorContext = stderr_file();
        }
        let stream = crate::abi::data_globals::xsltGenericErrorContext;
        if msg.is_null() || stream.is_null() {
            return 0;
        }
        vfprintf(stream, msg, ap)
    }
}

/// `xsltGenericErrorDefaultFunc(void *ctx, const char *msg, ...)` — the
/// upstream default XSLT error handler (xsltutils.c).
#[cfg(target_arch = "x86_64")]
pub unsafe extern "C" fn xsltGenericErrorDefaultFunc() -> 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], 16",
            "mov dword ptr [rsp+180], 48",
            "lea rax, [rsp+256]",
            "mov [rsp+184], rax",
            "lea rax, [rsp]",
            "mov [rsp+192], rax",
            "lea rdx, [rsp+176]",
            "call xsltGenericErrorDefaultFuncV",
            "add rsp, 240",
            "add rsp, 8",
            "ret",
            options(noreturn),
        );
    }
}

/// Default value of the exported `xsltGenericError` data global.
#[cfg(target_arch = "x86_64")]
const XSLT_GENERIC_ERROR_DEFAULT: xmlGenericErrorFunc = unsafe {
    // SAFETY: as above — ABI-identical code pointer.
    core::mem::transmute::<
        unsafe extern "C" fn() -> c_int,
        unsafe extern "C" fn(*mut c_void, *const c_char),
    >(xsltGenericErrorDefaultFunc)
};

/// The built-in default generic error handler (upstream
/// `xmlGenericErrorDefaultFunc`), for use when a caller resets the handler
/// with NULL. Only available on x86_64 (the variadic shim is SysV-specific);
/// on other targets there is no default (resets leave the handler unset).
pub fn default_generic_error_func() -> Option<xmlGenericErrorFunc> {
    #[cfg(target_arch = "x86_64")]
    {
        Some(XML_GENERIC_ERROR_DEFAULT)
    }
    #[cfg(not(target_arch = "x86_64"))]
    {
        None
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Static strings (upstream xmlstring.h / tree.c)
// ═══════════════════════════════════════════════════════════════════════════════

/// `const xmlChar xmlStringText[]` — "text"
#[no_mangle]
pub static xmlStringText: [xmlChar; 5] = [b't', b'e', b'x', b't', 0];

/// `const xmlChar xmlStringTextNoenc[]` — "textnoenc"
#[no_mangle]
pub static xmlStringTextNoenc: [xmlChar; 9] =
    [b't', b'e', b'x', b't', b'n', b'o', b'e', b'n', b'c'];

/// `const xmlChar xmlStringComment[]` — "comment"
#[no_mangle]
pub static xmlStringComment: [xmlChar; 8] = [b'c', b'o', b'm', b'm', b'e', b'n', b't', 0];

// ═══════════════════════════════════════════════════════════════════════════════
// XPath numeric constants (upstream xpath.c)
// ═══════════════════════════════════════════════════════════════════════════════

/// `double xmlXPathNAN` — NaN
#[no_mangle]
pub static xmlXPathNAN: f64 = f64::NAN;

/// `double xmlXPathPINF` — +infinity
#[no_mangle]
pub static xmlXPathPINF: f64 = f64::INFINITY;

/// `double xmlXPathNINF` — -infinity
#[no_mangle]
pub static xmlXPathNINF: f64 = f64::NEG_INFINITY;

// ═══════════════════════════════════════════════════════════════════════════════
// libxslt globals (upstream xslt.c / xsltutils.c / documents.c / xslt.h)
// ═══════════════════════════════════════════════════════════════════════════════
// xsltMaxDepth / xsltMaxVars are exported from src/xslt/transform/mod.rs
// (they are read by the transform engine).

/// `const int xsltLibxmlVersion` = LIBXML_VERSION (21503) — the libxml2
/// version libxslt was built against (upstream xslt.c).
#[no_mangle]
pub static xsltLibxmlVersion: c_int = 21503;

/// `xmlGenericErrorFunc xsltGenericError` — the libxslt error callback.
/// Upstream defaults to `xsltGenericErrorDefaultFunc` (xsltutils.c, a variadic
/// stderr printer); the candidate's shim below reproduces it (R-000135
/// divergence now closed).
#[cfg(target_arch = "x86_64")]
#[no_mangle]
pub static mut xsltGenericError: Option<xmlGenericErrorFunc> = Some(XSLT_GENERIC_ERROR_DEFAULT);

#[cfg(not(target_arch = "x86_64"))]
#[no_mangle]
pub static mut xsltGenericError: Option<xmlGenericErrorFunc> = None;

/// `void *xsltGenericErrorContext` (default NULL)
#[no_mangle]
pub static mut xsltGenericErrorContext: *mut c_void = core::ptr::null_mut();

/// `void *xsltGenericDebugContext` (default NULL)
#[no_mangle]
pub static mut xsltGenericDebugContext: *mut c_void = core::ptr::null_mut();

/// `const xmlChar xsltExtMarker[]` — empty string used to mark extension
/// nodes (upstream transform.c).
#[no_mangle]
pub static xsltExtMarker: [xmlChar; 1] = [0];

/// `xsltDocLoaderFunc xsltDocDefaultLoader` — the document loader callback.
/// Upstream defaults to `xsltDocDefaultLoaderFunc`; the candidate defaults
/// to NULL and its internal loader path is used — documented safe
/// divergence (residual R-000135).
#[no_mangle]
pub static mut xsltDocDefaultLoader: Option<
    unsafe extern "C" fn(
        *const xmlChar,
        *mut c_void,
        c_int,
        *mut crate::abi::structs::_xsltStylesheet,
        *mut crate::abi::structs::_xsltTransformContext,
    ) -> *mut crate::abi::structs::_xmlDoc,
> = None;

// ═══════════════════════════════════════════════════════════════════════════════
// I/O filename callback globals (upstream xmlIO.h)
// ═══════════════════════════════════════════════════════════════════════════════

/// `xmlParserInputBufferCreateFilenameFunc xmlParserInputBufferCreateFilenameValue`
#[no_mangle]
pub static mut xmlParserInputBufferCreateFilenameValue: Option<
    unsafe extern "C" fn(*const c_char, c_int) -> *mut crate::abi::structs::_xmlParserInputBuffer,
> = None;

/// `xmlOutputBufferCreateFilenameFunc xmlOutputBufferCreateFilenameValue`
#[no_mangle]
pub static mut xmlOutputBufferCreateFilenameValue: Option<
    unsafe extern "C" fn(
        *const c_char,
        crate::abi::structs::xmlCharEncodingHandlerPtr,
        c_int,
    ) -> *mut crate::abi::structs::_xmlOutputBuffer,
> = None;

// ═══════════════════════════════════════════════════════════════════════════════
// Default SAX v1 handler structs + locator (upstream globals.c 2.15.3)
// ═══════════════════════════════════════════════════════════════════════════════
//
// `const xmlSAXHandlerV1 xmlDefaultSAXHandler` and `htmlDefaultSAXHandler`
// (parser.h / HTMLparser.h), plus `const xmlSAXLocator xmlDefaultSAXLocator`.
// The handler instances reproduce the upstream initializer lists exactly
// (globals.c); every referenced xmlSAX2* entry point is a real candidate
// export.

/// `const xmlSAXHandlerV1 xmlDefaultSAXHandler` (globals.c 2.15.3).
#[no_mangle]
pub static xmlDefaultSAXHandler: crate::abi::structs::_xmlSAXHandlerV1 =
    crate::abi::structs::_xmlSAXHandlerV1 {
        internalSubset: Some(crate::abi::exports_xml2::xmlSAX2InternalSubset),
        isStandalone: Some(crate::abi::exports_xml2::xmlSAX2IsStandalone),
        hasInternalSubset: Some(crate::abi::exports_xml2::xmlSAX2HasInternalSubset),
        hasExternalSubset: Some(crate::abi::exports_xml2::xmlSAX2HasExternalSubset),
        resolveEntity: Some(crate::abi::exports_xml2::xmlSAX2ResolveEntity),
        getEntity: Some(crate::abi::exports_xml2::xmlSAX2GetEntity),
        entityDecl: Some(crate::abi::exports_xml2::xmlSAX2EntityDecl),
        notationDecl: Some(crate::abi::exports_xml2::xmlSAX2NotationDecl),
        attributeDecl: Some(crate::abi::exports_xml2::xmlSAX2AttributeDecl),
        elementDecl: Some(crate::abi::exports_xml2::xmlSAX2ElementDecl),
        unparsedEntityDecl: Some(crate::abi::exports_xml2::xmlSAX2UnparsedEntityDecl),
        setDocumentLocator: Some(crate::abi::exports_xml2::xmlSAX2SetDocumentLocator),
        startDocument: Some(crate::abi::exports_xml2::xmlSAX2StartDocument),
        endDocument: Some(crate::abi::exports_xml2::xmlSAX2EndDocument),
        startElement: Some(crate::abi::exports_xml2::xmlSAX2StartElement),
        endElement: Some(crate::abi::exports_xml2::xmlSAX2EndElement),
        reference: Some(crate::abi::exports_xml2::xmlSAX2Reference),
        characters: Some(crate::abi::exports_xml2::xmlSAX2Characters),
        ignorableWhitespace: Some(crate::abi::exports_xml2::xmlSAX2IgnorableWhitespace),
        processingInstruction: Some(crate::abi::exports_xml2::xmlSAX2ProcessingInstruction),
        comment: Some(crate::abi::exports_xml2::xmlSAX2Comment),
        warning: Some(crate::xml::errors::xmlParserWarning),
        error: Some(crate::xml::errors::xmlParserError),
        fatalError: Some(crate::xml::errors::xmlParserError),
        getParameterEntity: Some(crate::abi::exports_xml2::xmlSAX2GetParameterEntity),
        cdataBlock: Some(crate::abi::exports_xml2::xmlSAX2CDataBlock),
        externalSubset: Some(crate::abi::exports_xml2::xmlSAX2ExternalSubset),
        initialized: 1,
    };

/// `const xmlSAXHandlerV1 htmlDefaultSAXHandler` (globals.c 2.15.3).
#[no_mangle]
pub static htmlDefaultSAXHandler: crate::abi::structs::_xmlSAXHandlerV1 =
    crate::abi::structs::_xmlSAXHandlerV1 {
        internalSubset: Some(crate::abi::exports_xml2::xmlSAX2InternalSubset),
        isStandalone: None,
        hasInternalSubset: None,
        hasExternalSubset: None,
        resolveEntity: None,
        getEntity: Some(crate::abi::exports_xml2::xmlSAX2GetEntity),
        entityDecl: None,
        notationDecl: None,
        attributeDecl: None,
        elementDecl: None,
        unparsedEntityDecl: None,
        setDocumentLocator: Some(crate::abi::exports_xml2::xmlSAX2SetDocumentLocator),
        startDocument: Some(crate::abi::exports_xml2::xmlSAX2StartDocument),
        endDocument: Some(crate::abi::exports_xml2::xmlSAX2EndDocument),
        startElement: Some(crate::abi::exports_xml2::xmlSAX2StartElement),
        endElement: Some(crate::abi::exports_xml2::xmlSAX2EndElement),
        reference: None,
        characters: Some(crate::abi::exports_xml2::xmlSAX2Characters),
        ignorableWhitespace: Some(crate::abi::exports_xml2::xmlSAX2IgnorableWhitespace),
        processingInstruction: Some(crate::abi::exports_xml2::xmlSAX2ProcessingInstruction),
        comment: Some(crate::abi::exports_xml2::xmlSAX2Comment),
        warning: Some(crate::xml::errors::xmlParserWarning),
        error: Some(crate::xml::errors::xmlParserError),
        fatalError: Some(crate::xml::errors::xmlParserError),
        getParameterEntity: None,
        cdataBlock: Some(crate::abi::exports_xml2::xmlSAX2CDataBlock),
        externalSubset: None,
        initialized: 1,
    };

/// `const xmlSAXLocator xmlDefaultSAXLocator` (globals.c 2.15.3).
#[no_mangle]
pub static xmlDefaultSAXLocator: crate::abi::callbacks::_xmlSAXLocator =
    crate::abi::callbacks::_xmlSAXLocator {
        getPublicId: Some(crate::abi::exports_xml2::xmlSAX2GetPublicId),
        getSystemId: Some(crate::abi::exports_xml2::xmlSAX2GetSystemId),
        getLineNumber: Some(crate::abi::exports_xml2::xmlSAX2GetLineNumber),
        getColumnNumber: Some(crate::abi::exports_xml2::xmlSAX2GetColumnNumber),
    };

// ═══════════════════════════════════════════════════════════════════════════════
// xmlLastError — the exported C global mirror of the thread-local error state
// ═══════════════════════════════════════════════════════════════════════════════
//
// Upstream `XMLPUBVAR xmlError xmlLastError` (xmlerror.h). The candidate's
// internal error state is thread-local (safe divergence, more correct than
// upstream's racy global); this mirror is deep-copied on every error raise
// and freed on reset, so C consumers observe upstream-equivalent lifetime
// semantics. Residual R-000135.

/// `xmlError xmlLastError` — most recent error (mirror).
#[no_mangle]
pub static mut xmlLastError: crate::abi::structs::_xmlError = crate::abi::structs::_xmlError {
    domain: 0,
    code: 0,
    message: core::ptr::null_mut(),
    level: 0,
    file: core::ptr::null_mut(),
    line: 0,
    str1: core::ptr::null_mut(),
    str2: core::ptr::null_mut(),
    str3: core::ptr::null_mut(),
    int1: 0,
    int2: 0,
    ctxt: core::ptr::null_mut(),
    node: core::ptr::null_mut(),
};

/// Deep-copy `err` into the exported `xmlLastError` global.
///
/// The string fields are copied with `libc::malloc`/`memcpy` so the mirror
/// owns them (previous mirror strings are freed first — upstream
/// xmlResetError semantics).
///
/// # SAFETY
///
/// - `err` must point to a valid `_xmlError` whose string fields are
///   NUL-terminated or NULL.
pub unsafe fn sync_xml_last_error(err: *const crate::abi::structs::_xmlError) {
    if err.is_null() {
        return;
    }
    unsafe {
        reset_xml_last_error();
        let src = &*err;
        let dst = core::ptr::addr_of_mut!(xmlLastError);
        (*dst).domain = src.domain;
        (*dst).code = src.code;
        (*dst).level = src.level;
        (*dst).line = src.line;
        (*dst).int1 = src.int1;
        (*dst).int2 = src.int2;
        (*dst).ctxt = src.ctxt;
        (*dst).node = src.node;
        (*dst).message = dup_cstr(src.message as *const u8);
        (*dst).file = dup_cstr(src.file as *const u8);
        (*dst).str1 = dup_cstr(src.str1 as *const u8);
        (*dst).str2 = dup_cstr(src.str2 as *const u8);
        (*dst).str3 = dup_cstr(src.str3 as *const u8);
    }
}

/// Reset the exported `xmlLastError` global, freeing owned strings
/// (upstream xmlResetError).
///
/// # SAFETY
///
/// Only call while no other thread is reading the global (upstream has the
/// same race; documented).
pub unsafe fn reset_xml_last_error() {
    unsafe {
        let dst = core::ptr::addr_of_mut!(xmlLastError);
        if !(*dst).message.is_null() {
            libc::free((*dst).message as *mut libc::c_void);
        }
        if !(*dst).file.is_null() {
            libc::free((*dst).file as *mut libc::c_void);
        }
        if !(*dst).str1.is_null() {
            libc::free((*dst).str1 as *mut libc::c_void);
        }
        if !(*dst).str2.is_null() {
            libc::free((*dst).str2 as *mut libc::c_void);
        }
        if !(*dst).str3.is_null() {
            libc::free((*dst).str3 as *mut libc::c_void);
        }
        *dst = crate::abi::structs::_xmlError {
            domain: 0,
            code: 0,
            message: core::ptr::null_mut(),
            level: 0,
            file: core::ptr::null_mut(),
            line: 0,
            str1: core::ptr::null_mut(),
            str2: core::ptr::null_mut(),
            str3: core::ptr::null_mut(),
            int1: 0,
            int2: 0,
            ctxt: core::ptr::null_mut(),
            node: core::ptr::null_mut(),
        };
    }
}

/// Heap-copy a NUL-terminated string (NULL-safe).
unsafe fn dup_cstr(s: *const u8) -> *mut c_char {
    if s.is_null() {
        return core::ptr::null_mut();
    }
    unsafe {
        let len = libc::strlen(s as *const libc::c_char) as usize;
        let p = libc::malloc(len + 1) as *mut u8;
        if p.is_null() {
            return core::ptr::null_mut();
        }
        libc::memcpy(p as *mut libc::c_void, s as *const libc::c_void, len + 1);
        p as *mut c_char
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Default accessor functions (upstream parser.h / tree.h / xmlsave.h)
// ═══════════════════════════════════════════════════════════════════════════════
//
// The deprecated `xmlXxxDefault(v)` accessors set the corresponding global
// when `v != 0` and return the (new) value — upstream semantics (they
// predate the plain globals; the modern behavior is conditional-set-and-
// return, see upstream globals.c / parser.c).

/// Upstream `xmlKeepBlanksDefault(int v)`.
#[no_mangle]
pub unsafe extern "C" fn xmlKeepBlanksDefault(v: c_int) -> c_int {
    unsafe {
        if v != 0 {
            xmlKeepBlanksDefaultValue = v;
        }
        xmlKeepBlanksDefaultValue
    }
}

/// Upstream `xmlLineNumbersDefault(int v)`.
#[no_mangle]
pub unsafe extern "C" fn xmlLineNumbersDefault(v: c_int) -> c_int {
    unsafe {
        if v != 0 {
            xmlLineNumbersDefaultValue = v;
        }
        xmlLineNumbersDefaultValue
    }
}

/// Upstream `xmlSubstituteEntitiesDefault(int v)`.
#[no_mangle]
pub unsafe extern "C" fn xmlSubstituteEntitiesDefault(v: c_int) -> c_int {
    unsafe {
        if v != 0 {
            xmlSubstituteEntitiesDefaultValue = v;
        }
        xmlSubstituteEntitiesDefaultValue
    }
}

/// Upstream `xmlPedanticParserDefault(int v)`.
#[no_mangle]
pub unsafe extern "C" fn xmlPedanticParserDefault(v: c_int) -> c_int {
    unsafe {
        if v != 0 {
            xmlPedanticParserDefaultValue = v;
        }
        xmlPedanticParserDefaultValue
    }
}

/// Upstream `xmlDoValidityCheckingDefaultValue` accessor is the global
/// itself; `xmlGetWarningsDefaultValue` likewise (no accessor functions
/// exist for those in upstream 2.15).

/// Upstream `xmlRegisterNodeDefault(xmlRegisterNodeFunc func)`.
#[no_mangle]
pub unsafe extern "C" fn xmlRegisterNodeDefault(
    func: Option<unsafe extern "C" fn(*mut crate::abi::structs::_xmlNode)>,
) -> Option<unsafe extern "C" fn(*mut crate::abi::structs::_xmlNode)> {
    unsafe {
        // UPSTREAM-PARITY (tree.c): registering any callback arms the
        // xmlRegisterCallbacks gate.
        XML_REGISTER_CALLBACKS.store(true, core::sync::atomic::Ordering::Relaxed);
        if func.is_some() {
            xmlRegisterNodeDefaultValue = func;
        }
        xmlRegisterNodeDefaultValue
    }
}

/// Upstream `xmlDeregisterNodeDefault(xmlDeregisterNodeFunc func)`.
#[no_mangle]
pub unsafe extern "C" fn xmlDeregisterNodeDefault(
    func: Option<unsafe extern "C" fn(*mut crate::abi::structs::_xmlNode)>,
) -> Option<unsafe extern "C" fn(*mut crate::abi::structs::_xmlNode)> {
    unsafe {
        // UPSTREAM-PARITY (tree.c): registering any callback arms the
        // xmlRegisterCallbacks gate.
        XML_REGISTER_CALLBACKS.store(true, core::sync::atomic::Ordering::Relaxed);
        if func.is_some() {
            xmlDeregisterNodeDefaultValue = func;
        }
        xmlDeregisterNodeDefaultValue
    }
}

/// Upstream `__xmlIndentTreeOutput(void)` (parser.h) — returns a pointer to
/// the `xmlIndentTreeOutput` global.
#[no_mangle]
pub unsafe extern "C" fn __xmlIndentTreeOutput() -> *mut c_int {
    unsafe { core::ptr::addr_of_mut!(xmlIndentTreeOutput) }
}

/// Upstream `__xmlSaveNoEmptyTags(void)` (parser.h) — returns a pointer to
/// the `xmlSaveNoEmptyTags` global.
#[no_mangle]
pub unsafe extern "C" fn __xmlSaveNoEmptyTags() -> *mut c_int {
    unsafe { core::ptr::addr_of_mut!(xmlSaveNoEmptyTags) }
}

/// Upstream `__xmlTreeIndentString(void)` (parser.h) — returns a pointer to
/// the `xmlTreeIndentString` global.
#[no_mangle]
pub unsafe extern "C" fn __xmlTreeIndentString() -> *mut *const xmlChar {
    unsafe { core::ptr::addr_of_mut!(xmlTreeIndentString) }
}

// ═══════════════════════════════════════════════════════════════════════════════
// xmlThrDef* accessors (upstream threads.c / globals.c)
// ═══════════════════════════════════════════════════════════════════════════════
//
// The deprecated `xmlThrDef*` family reads/writes the public globals with
// the upstream semantics: when `v != 0` the global is set, and the (new)
// value is returned. In upstream these were thread-local definitions before
// the globals became plain variables; the modern behavior is exactly this
// conditional-set-and-return on the global (see upstream globals.c).

/// Upstream `xmlThrDefDoValidityCheckingDefaultValue(int v)`.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefDoValidityCheckingDefaultValue(v: c_int) -> c_int {
    unsafe {
        if v != 0 {
            xmlDoValidityCheckingDefaultValue = v;
        }
        xmlDoValidityCheckingDefaultValue
    }
}

/// Upstream `xmlThrDefGetWarningsDefaultValue(int v)`.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefGetWarningsDefaultValue(v: c_int) -> c_int {
    unsafe {
        if v != 0 {
            xmlGetWarningsDefaultValue = v;
        }
        xmlGetWarningsDefaultValue
    }
}

/// Upstream `xmlThrDefLoadExtDtdDefaultValue(int v)`.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefLoadExtDtdDefaultValue(v: c_int) -> c_int {
    unsafe {
        if v != 0 {
            xmlLoadExtDtdDefaultValue = v;
        }
        xmlLoadExtDtdDefaultValue
    }
}

/// Upstream `xmlThrDefPedanticParserDefaultValue(int v)`.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefPedanticParserDefaultValue(v: c_int) -> c_int {
    unsafe {
        if v != 0 {
            xmlPedanticParserDefaultValue = v;
        }
        xmlPedanticParserDefaultValue
    }
}

/// Upstream `xmlThrDefLineNumbersDefaultValue(int v)`.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefLineNumbersDefaultValue(v: c_int) -> c_int {
    unsafe {
        if v != 0 {
            xmlLineNumbersDefaultValue = v;
        }
        xmlLineNumbersDefaultValue
    }
}

/// Upstream `xmlThrDefKeepBlanksDefaultValue(int v)`.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefKeepBlanksDefaultValue(v: c_int) -> c_int {
    unsafe {
        if v != 0 {
            xmlKeepBlanksDefaultValue = v;
        }
        xmlKeepBlanksDefaultValue
    }
}

/// Upstream `xmlThrDefSubstituteEntitiesDefaultValue(int v)`.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefSubstituteEntitiesDefaultValue(v: c_int) -> c_int {
    unsafe {
        if v != 0 {
            xmlSubstituteEntitiesDefaultValue = v;
        }
        xmlSubstituteEntitiesDefaultValue
    }
}

/// Upstream `xmlThrDefParserDebugEntities(int v)`.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefParserDebugEntities(v: c_int) -> c_int {
    unsafe {
        if v != 0 {
            xmlParserDebugEntities = v;
        }
        xmlParserDebugEntities
    }
}

/// Upstream `xmlThrDefIndentTreeOutput(int v)`.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefIndentTreeOutput(v: c_int) -> c_int {
    unsafe {
        if v != 0 {
            xmlIndentTreeOutput = v;
        }
        xmlIndentTreeOutput
    }
}

/// Upstream `xmlThrDefTreeIndentString(const char *v)` — sets the indent
/// string when non-NULL and returns the current pointer.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefTreeIndentString(v: *const c_char) -> *const c_char {
    unsafe {
        if !v.is_null() {
            xmlTreeIndentString = v as *const xmlChar;
        }
        xmlTreeIndentString as *const c_char
    }
}

/// Upstream `xmlThrDefSaveNoEmptyTags(int v)`.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefSaveNoEmptyTags(v: c_int) -> c_int {
    unsafe {
        if v != 0 {
            xmlSaveNoEmptyTags = v;
        }
        xmlSaveNoEmptyTags
    }
}

/// Upstream `xmlThrDefRegisterNodeDefault(xmlRegisterNodeFunc func)`.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefRegisterNodeDefault(
    func: Option<unsafe extern "C" fn(*mut crate::abi::structs::_xmlNode)>,
) -> Option<unsafe extern "C" fn(*mut crate::abi::structs::_xmlNode)> {
    unsafe {
        if func.is_some() {
            xmlRegisterNodeDefaultValue = func;
        }
        xmlRegisterNodeDefaultValue
    }
}

/// Upstream `xmlThrDefDeregisterNodeDefault(xmlDeregisterNodeFunc func)`.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefDeregisterNodeDefault(
    func: Option<unsafe extern "C" fn(*mut crate::abi::structs::_xmlNode)>,
) -> Option<unsafe extern "C" fn(*mut crate::abi::structs::_xmlNode)> {
    unsafe {
        if func.is_some() {
            xmlDeregisterNodeDefaultValue = func;
        }
        xmlDeregisterNodeDefaultValue
    }
}

/// Upstream `xmlThrDefSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc func)`.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefSetGenericErrorFunc(
    ctx: *mut c_void,
    func: Option<xmlGenericErrorFunc>,
) {
    unsafe {
        xmlGenericErrorContext = ctx;
        xmlGenericError = func;
    }
}

/// Upstream `xmlThrDefSetStructuredErrorFunc(void *ctx, xmlStructuredErrorFunc func)`.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefSetStructuredErrorFunc(
    ctx: *mut c_void,
    func: Option<xmlStructuredErrorFunc>,
) {
    unsafe {
        xmlStructuredErrorContext = ctx;
        xmlStructuredError = func;
    }
}

/// Upstream `xmlThrDefDefaultBufferSize(int v)`.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefDefaultBufferSize(v: c_int) -> c_int {
    unsafe {
        if v != 0 {
            xmlDefaultBufferSize = v;
        }
        xmlDefaultBufferSize
    }
}

/// Upstream `xmlThrDefBufferAllocScheme(xmlBufferAllocationScheme v)`.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefBufferAllocScheme(v: c_int) -> c_int {
    unsafe {
        if v != 0 {
            xmlBufferAllocScheme = v;
        }
        xmlBufferAllocScheme
    }
}

/// Upstream `xmlThrDefParserInputBufferCreateFilenameDefault(...)`.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefParserInputBufferCreateFilenameDefault(
    func: Option<
        unsafe extern "C" fn(
            *const c_char,
            c_int,
        ) -> *mut crate::abi::structs::_xmlParserInputBuffer,
    >,
) -> Option<
    unsafe extern "C" fn(*const c_char, c_int) -> *mut crate::abi::structs::_xmlParserInputBuffer,
> {
    unsafe {
        if func.is_some() {
            xmlParserInputBufferCreateFilenameValue = func;
        }
        xmlParserInputBufferCreateFilenameValue
    }
}

/// Upstream `xmlThrDefOutputBufferCreateFilenameDefault(...)`.
#[no_mangle]
pub unsafe extern "C" fn xmlThrDefOutputBufferCreateFilenameDefault(
    func: Option<
        unsafe extern "C" fn(
            *const c_char,
            crate::abi::structs::xmlCharEncodingHandlerPtr,
            c_int,
        ) -> *mut crate::abi::structs::_xmlOutputBuffer,
    >,
) -> Option<
    unsafe extern "C" fn(
        *const c_char,
        crate::abi::structs::xmlCharEncodingHandlerPtr,
        c_int,
    ) -> *mut crate::abi::structs::_xmlOutputBuffer,
> {
    unsafe {
        if func.is_some() {
            xmlOutputBufferCreateFilenameValue = func;
        }
        xmlOutputBufferCreateFilenameValue
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// __xmlXxx() pointer accessors (upstream threads.c / globals.c)
// ═══════════════════════════════════════════════════════════════════════════════
// The deprecated thread-local API exports one `__xmlXxx(void)` accessor per
// global; each returns a pointer to the global so callers can read/write it.

/// Upstream `__xmlBufferAllocScheme(void)` — returns a pointer to `xmlBufferAllocScheme`.
#[no_mangle]
pub unsafe extern "C" fn __xmlBufferAllocScheme() -> *mut c_int {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlBufferAllocScheme) }
}

/// Upstream `__xmlDefaultBufferSize(void)` — returns a pointer to `xmlDefaultBufferSize`.
#[no_mangle]
pub unsafe extern "C" fn __xmlDefaultBufferSize() -> *mut c_int {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlDefaultBufferSize) }
}

/// Upstream `__xmlDeregisterNodeDefaultValue(void)` — returns a pointer to `xmlDeregisterNodeDefaultValue`.
#[no_mangle]
pub unsafe extern "C" fn __xmlDeregisterNodeDefaultValue(
) -> *mut Option<unsafe extern "C" fn(*mut crate::abi::structs::_xmlNode)> {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlDeregisterNodeDefaultValue) }
}

/// Upstream `__xmlDoValidityCheckingDefaultValue(void)` — returns a pointer to `xmlDoValidityCheckingDefaultValue`.
#[no_mangle]
pub unsafe extern "C" fn __xmlDoValidityCheckingDefaultValue() -> *mut c_int {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlDoValidityCheckingDefaultValue) }
}

/// Upstream `__xmlGenericError(void)` — returns a pointer to `xmlGenericError`.
#[no_mangle]
pub unsafe extern "C" fn __xmlGenericError() -> *mut Option<xmlGenericErrorFunc> {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlGenericError) }
}

/// Upstream `__xmlGenericErrorContext(void)` — returns a pointer to `xmlGenericErrorContext`.
#[no_mangle]
pub unsafe extern "C" fn __xmlGenericErrorContext() -> *mut *mut c_void {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlGenericErrorContext) }
}

/// Upstream `__xmlGetWarningsDefaultValue(void)` — returns a pointer to `xmlGetWarningsDefaultValue`.
#[no_mangle]
pub unsafe extern "C" fn __xmlGetWarningsDefaultValue() -> *mut c_int {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlGetWarningsDefaultValue) }
}

/// Upstream `__xmlKeepBlanksDefaultValue(void)` — returns a pointer to `xmlKeepBlanksDefaultValue`.
#[no_mangle]
pub unsafe extern "C" fn __xmlKeepBlanksDefaultValue() -> *mut c_int {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlKeepBlanksDefaultValue) }
}

/// Upstream `__xmlLineNumbersDefaultValue(void)` — returns a pointer to `xmlLineNumbersDefaultValue`.
#[no_mangle]
pub unsafe extern "C" fn __xmlLineNumbersDefaultValue() -> *mut c_int {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlLineNumbersDefaultValue) }
}

/// Upstream `__xmlLoadExtDtdDefaultValue(void)` — returns a pointer to `xmlLoadExtDtdDefaultValue`.
#[no_mangle]
pub unsafe extern "C" fn __xmlLoadExtDtdDefaultValue() -> *mut c_int {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlLoadExtDtdDefaultValue) }
}

/// Upstream `__xmlOutputBufferCreateFilenameValue(void)` — returns a pointer to `xmlOutputBufferCreateFilenameValue`.
#[no_mangle]
pub unsafe extern "C" fn __xmlOutputBufferCreateFilenameValue() -> *mut Option<
    unsafe extern "C" fn(
        *const c_char,
        crate::abi::structs::xmlCharEncodingHandlerPtr,
        c_int,
    ) -> *mut crate::abi::structs::_xmlOutputBuffer,
> {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlOutputBufferCreateFilenameValue) }
}

/// Upstream `__xmlParserDebugEntities(void)` — returns a pointer to `xmlParserDebugEntities`.
#[no_mangle]
pub unsafe extern "C" fn __xmlParserDebugEntities() -> *mut c_int {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlParserDebugEntities) }
}

/// Upstream `__xmlParserInputBufferCreateFilenameValue(void)` — returns a pointer to `xmlParserInputBufferCreateFilenameValue`.
#[no_mangle]
pub unsafe extern "C" fn __xmlParserInputBufferCreateFilenameValue() -> *mut Option<
    unsafe extern "C" fn(*const c_char, c_int) -> *mut crate::abi::structs::_xmlParserInputBuffer,
> {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlParserInputBufferCreateFilenameValue) }
}

/// Upstream `__xmlParserVersion(void)` — returns a pointer to `xmlParserVersion`.
#[no_mangle]
pub unsafe extern "C" fn __xmlParserVersion() -> *mut *const c_char {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlParserVersion) }
}

/// Upstream `__xmlPedanticParserDefaultValue(void)` — returns a pointer to `xmlPedanticParserDefaultValue`.
#[no_mangle]
pub unsafe extern "C" fn __xmlPedanticParserDefaultValue() -> *mut c_int {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlPedanticParserDefaultValue) }
}

/// Upstream `__xmlRegisterNodeDefaultValue(void)` — returns a pointer to `xmlRegisterNodeDefaultValue`.
#[no_mangle]
pub unsafe extern "C" fn __xmlRegisterNodeDefaultValue(
) -> *mut Option<unsafe extern "C" fn(*mut crate::abi::structs::_xmlNode)> {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlRegisterNodeDefaultValue) }
}

/// Upstream `__xmlStructuredError(void)` — returns a pointer to `xmlStructuredError`.
#[no_mangle]
pub unsafe extern "C" fn __xmlStructuredError() -> *mut Option<xmlStructuredErrorFunc> {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlStructuredError) }
}

/// Upstream `__xmlStructuredErrorContext(void)` — returns a pointer to `xmlStructuredErrorContext`.
#[no_mangle]
pub unsafe extern "C" fn __xmlStructuredErrorContext() -> *mut *mut c_void {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlStructuredErrorContext) }
}

/// Upstream `__xmlSubstituteEntitiesDefaultValue(void)` — returns a pointer to `xmlSubstituteEntitiesDefaultValue`.
#[no_mangle]
pub unsafe extern "C" fn __xmlSubstituteEntitiesDefaultValue() -> *mut c_int {
    // SAFETY: returning a pointer to an exported static; the caller may
    // read/write it exactly as with upstream's deprecated accessor.
    unsafe { core::ptr::addr_of_mut!(xmlSubstituteEntitiesDefaultValue) }
}