libxml-rs 0.1.0-alpha.22

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
//! C ABI exports for the XInclude family — `xmlXInclude*` (upstream
//! `xinclude.h`, 2.15.3).
//!
//! Implements the context-based XInclude entry points by wrapping the
//! native-Rust XInclude engine (`src/xml/xinclude`), which also powers
//! `xmllint --xinclude`.
//!
//! The context struct (`_xmlXIncludeCtxt`) is opaque at the C boundary — no
//! field is ever dereferenced by the caller. The candidate engine keeps all
//! per-include state (URL stack, circular-reference tracking, fallback
//! handling) in Rust-owned structures that live for the duration of a single
//! process call, so the context only needs to record the document, the
//! processing flags, the error handler and the last error code.
//!
//! # Engine scope
//!
//! The engine's public entry points (`xinclude_process`,
//! `xinclude_process_flags`) always walk the whole document starting at its
//! root element. The tree-based entry points below therefore process the
//! document that owns the given node; for a node that is the document root
//! (the common case) this is exactly the upstream subtree semantics.

#![allow(
    missing_docs,
    non_snake_case,
    non_camel_case_types,
    non_upper_case_globals
)]

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

use crate::abi::allocator::{xmlFreeImpl, xmlMallocZero};
use crate::abi::structs::{_xmlDoc, _xmlNode};
use crate::abi::types::{XML_ERR_ARGUMENT, XML_ERR_INTERNAL_ERROR, XML_ERR_OK};
use crate::xml::xinclude;

/// Generic failure return for the process entry points (upstream `-1`).
const XINCLUDE_ERROR: c_int = -1;

/// XInclude error callback (upstream `xmlXIncludeErrorFunc`, xinclude.h).
///
/// `ctx` is the application data pointer registered with the handler,
/// `code` is the error code, `message` the human-readable message.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// typedef void (*xmlXIncludeErrorFunc) (void *context, int code,
///                                       const char *message);
/// ```
pub type xmlXIncludeErrorFunc =
    unsafe extern "C" fn(ctx: *mut c_void, code: c_int, message: *const c_char);

/// XInclude processing context (upstream `struct _xmlXIncludeCtxt`).
///
/// Opaque to C callers; only the fields the candidate engine needs are kept.
#[repr(C)]
#[derive(Debug)]
pub struct _xmlXIncludeCtxt {
    /// The source document being processed.
    pub doc: *mut _xmlDoc,
    /// Error handling function (never invoked by the candidate engine).
    pub error: Option<xmlXIncludeErrorFunc>,
    /// Application data passed to the error handler.
    pub data: *mut c_void,
    /// Processing flags (e.g. `XML_PARSE_NOXINCNODE`, `XML_PARSE_NONET`).
    pub flags: c_int,
    /// Error code of the last failure during processing.
    pub lastError: c_int,
}

/// XInclude context pointer (upstream `xmlXIncludeCtxtPtr`).
pub type xmlXIncludeCtxtPtr = *mut _xmlXIncludeCtxt;

// ═══════════════════════════════════════════════════════════════════════════════
// Context lifecycle
// ═══════════════════════════════════════════════════════════════════════════════

/// Create a new XInclude processing context.
///
/// Returns the context, or NULL on allocation failure. `doc` may be NULL;
/// processing calls then fail with an argument error.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// xmlXIncludeCtxt *xmlXIncludeNewContext(xmlDoc *doc);
/// ```
///
/// # SAFETY
///
/// `doc` must be NULL or a valid pointer to a parsed `_xmlDoc`.
#[no_mangle]
pub unsafe extern "C" fn xmlXIncludeNewContext(doc: *mut _xmlDoc) -> xmlXIncludeCtxtPtr {
    let ctxt = unsafe { xmlMallocZero(size_of::<_xmlXIncludeCtxt>()) } as *mut _xmlXIncludeCtxt;
    if ctxt.is_null() {
        return ptr::null_mut();
    }
    unsafe {
        (*ctxt).doc = doc;
        (*ctxt).error = None;
        (*ctxt).data = ptr::null_mut();
        (*ctxt).flags = 0;
        (*ctxt).lastError = XML_ERR_OK;
    }
    ctxt
}

/// Free an XInclude processing context.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void xmlXIncludeFreeContext(xmlXIncludeCtxt *ctxt);
/// ```
///
/// # SAFETY
///
/// `ctxt` must be NULL or a pointer previously returned by
/// `xmlXIncludeNewContext` that has not already been freed.
#[no_mangle]
pub unsafe extern "C" fn xmlXIncludeFreeContext(ctxt: xmlXIncludeCtxtPtr) {
    if ctxt.is_null() {
        return;
    }
    // The context owns no heap allocations of its own (the engine keeps its
    // per-include state only for the duration of a process call), so the
    // struct itself is the only thing to release.
    unsafe { xmlFreeImpl(ctxt as *mut c_void) };
}

// ═══════════════════════════════════════════════════════════════════════════════
// Processing
// ═══════════════════════════════════════════════════════════════════════════════

/// Run the internal engine over the context's document, recording the
/// outcome in `lastError`.
///
/// Returns the number of XInclude nodes processed, or -1 on error.
///
/// # SAFETY
///
/// `ctxt` must be a valid, non-null pointer to a context created by
/// `xmlXIncludeNewContext`.
unsafe fn process_ctxt_doc(ctxt: xmlXIncludeCtxtPtr) -> c_int {
    let doc = unsafe { (*ctxt).doc };
    if doc.is_null() {
        unsafe { (*ctxt).lastError = XML_ERR_ARGUMENT };
        return XINCLUDE_ERROR;
    }
    let flags = unsafe { (*ctxt).flags };
    let ret = unsafe { xinclude::xinclude_process_flags(doc, flags) };
    if ret < 0 {
        // The engine reports failure without a fine-grained error code, so
        // record a generic parser error to make xmlXIncludeGetLastError
        // non-zero after a failure.
        unsafe { (*ctxt).lastError = XML_ERR_INTERNAL_ERROR };
    }
    ret
}

/// Process the XInclude nodes in the tree rooted at `tree`, using the
/// context's document and flags.
///
/// Returns the number of XInclude nodes processed, or -1 on error.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xmlXIncludeProcessNode(xmlXIncludeCtxt *ctxt, xmlNode *tree);
/// ```
///
/// # SAFETY
///
/// `ctxt` must be a valid context created by `xmlXIncludeNewContext`; `tree`
/// must be NULL or a valid `_xmlNode`.
#[no_mangle]
pub unsafe extern "C" fn xmlXIncludeProcessNode(
    ctxt: xmlXIncludeCtxtPtr,
    tree: *mut _xmlNode,
) -> c_int {
    if ctxt.is_null() || tree.is_null() {
        return XINCLUDE_ERROR;
    }
    unsafe { process_ctxt_doc(ctxt) }
}

/// Process all XInclude nodes in the document containing `tree`.
///
/// Returns the number of XInclude nodes processed, or -1 on error.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xmlXIncludeProcessTree(xmlNode *tree);
/// ```
///
/// # SAFETY
///
/// `tree` must be NULL or a valid `_xmlNode` attached to a document.
#[no_mangle]
pub unsafe extern "C" fn xmlXIncludeProcessTree(tree: *mut _xmlNode) -> c_int {
    if tree.is_null() {
        return XINCLUDE_ERROR;
    }
    let doc = unsafe { (*tree).doc };
    if doc.is_null() {
        return XINCLUDE_ERROR;
    }
    let ctxt = unsafe { xmlXIncludeNewContext(doc) };
    if ctxt.is_null() {
        return XINCLUDE_ERROR;
    }
    let ret = unsafe { xmlXIncludeProcessNode(ctxt, tree) };
    unsafe { xmlXIncludeFreeContext(ctxt) };
    ret
}

/// Process all XInclude nodes in the document containing `tree`, with flags.
///
/// Returns the number of XInclude nodes processed, or -1 on error.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xmlXIncludeProcessTreeFlags(xmlNode *tree, int flags);
/// ```
///
/// # SAFETY
///
/// `tree` must be NULL or a valid `_xmlNode` attached to a document.
#[no_mangle]
pub unsafe extern "C" fn xmlXIncludeProcessTreeFlags(tree: *mut _xmlNode, flags: c_int) -> c_int {
    if tree.is_null() {
        return XINCLUDE_ERROR;
    }
    let doc = unsafe { (*tree).doc };
    if doc.is_null() {
        return XINCLUDE_ERROR;
    }
    let ctxt = unsafe { xmlXIncludeNewContext(doc) };
    if ctxt.is_null() {
        return XINCLUDE_ERROR;
    }
    unsafe {
        (*ctxt).flags = flags;
    }
    let ret = unsafe { xmlXIncludeProcessNode(ctxt, tree) };
    unsafe { xmlXIncludeFreeContext(ctxt) };
    ret
}

/// Process all XInclude nodes in the document containing `tree`, with flags
/// and an error-handler data pointer.
///
/// Returns the number of XInclude nodes processed, or -1 on error.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xmlXIncludeProcessTreeFlagsData(xmlNode *tree, int flags, void *data);
/// ```
///
/// # SAFETY
///
/// `tree` must be NULL or a valid `_xmlNode` attached to a document; `data`
/// must be NULL or a valid pointer for the lifetime of the call.
#[no_mangle]
pub unsafe extern "C" fn xmlXIncludeProcessTreeFlagsData(
    tree: *mut _xmlNode,
    flags: c_int,
    data: *mut c_void,
) -> c_int {
    if tree.is_null() {
        return XINCLUDE_ERROR;
    }
    let doc = unsafe { (*tree).doc };
    if doc.is_null() {
        return XINCLUDE_ERROR;
    }
    let ctxt = unsafe { xmlXIncludeNewContext(doc) };
    if ctxt.is_null() {
        return XINCLUDE_ERROR;
    }
    unsafe {
        (*ctxt).flags = flags;
        (*ctxt).data = data;
        // Upstream also clears the handler function when only data is
        // supplied; the candidate engine never invokes the handler, so the
        // data pointer is the only observable part.
        (*ctxt).error = None;
    }
    let ret = unsafe { xmlXIncludeProcessNode(ctxt, tree) };
    unsafe { xmlXIncludeFreeContext(ctxt) };
    ret
}

/// Process all XInclude nodes in `doc`, with flags and an error-handler data
/// pointer.
///
/// Returns the number of XInclude nodes processed, or -1 on error.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xmlXIncludeProcessFlagsData(xmlDoc *doc, int flags, void *data);
/// ```
///
/// # SAFETY
///
/// `doc` must be NULL or a valid pointer to a parsed `_xmlDoc`; `data` must
/// be NULL or a valid pointer for the lifetime of the call.
#[no_mangle]
pub unsafe extern "C" fn xmlXIncludeProcessFlagsData(
    doc: *mut _xmlDoc,
    flags: c_int,
    data: *mut c_void,
) -> c_int {
    if doc.is_null() {
        return XINCLUDE_ERROR;
    }
    let ctxt = unsafe { xmlXIncludeNewContext(doc) };
    if ctxt.is_null() {
        return XINCLUDE_ERROR;
    }
    unsafe {
        (*ctxt).flags = flags;
        (*ctxt).data = data;
    }
    let ret = unsafe { process_ctxt_doc(ctxt) };
    unsafe { xmlXIncludeFreeContext(ctxt) };
    ret
}

// ═══════════════════════════════════════════════════════════════════════════════
// Context configuration & state
// ═══════════════════════════════════════════════════════════════════════════════

/// Set the processing flags on the context.
///
/// Returns the previous set of flags, or -1 if `ctxt` is NULL.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xmlXIncludeSetFlags(xmlXIncludeCtxt *ctxt, int flags);
/// ```
///
/// # SAFETY
///
/// `ctxt` must be NULL or a valid context created by `xmlXIncludeNewContext`.
#[no_mangle]
pub unsafe extern "C" fn xmlXIncludeSetFlags(ctxt: xmlXIncludeCtxtPtr, flags: c_int) -> c_int {
    if ctxt.is_null() {
        return XINCLUDE_ERROR;
    }
    let old_flags = unsafe { (*ctxt).flags };
    unsafe {
        (*ctxt).flags = flags;
    }
    old_flags
}

/// Set the error handler and its data on the context.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void xmlXIncludeSetErrorHandler(xmlXIncludeCtxt *ctxt,
///                                 xmlXIncludeErrorFunc handler,
///                                 void *data);
/// ```
///
/// # SAFETY
///
/// `ctxt` must be NULL or a valid context created by `xmlXIncludeNewContext`;
/// `data` must be NULL or a valid pointer for as long as the handler may be
/// called.
#[no_mangle]
pub unsafe extern "C" fn xmlXIncludeSetErrorHandler(
    ctxt: xmlXIncludeCtxtPtr,
    handler: Option<xmlXIncludeErrorFunc>,
    data: *mut c_void,
) {
    if ctxt.is_null() {
        return;
    }
    unsafe {
        (*ctxt).error = handler;
        (*ctxt).data = data;
    }
}

/// Get the error code of the last failure during XInclude processing on this
/// context.
///
/// Returns the last error code, or -1 if `ctxt` is NULL.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xmlXIncludeGetLastError(xmlXIncludeCtxt *ctxt);
/// ```
///
/// # SAFETY
///
/// `ctxt` must be NULL or a valid context created by `xmlXIncludeNewContext`.
#[no_mangle]
pub unsafe extern "C" fn xmlXIncludeGetLastError(ctxt: xmlXIncludeCtxtPtr) -> c_int {
    if ctxt.is_null() {
        return XINCLUDE_ERROR;
    }
    unsafe { (*ctxt).lastError }
}