libxml-rs 0.1.0-alpha.2

Phase 1: Compatibility skeleton complete. Native-Rust forensic reimplementation of libxml2+libxslt with C ABI drop-in replacement. 62 tests passing, ABI courts verified, C headers compatible, Docker oracle built.
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
//! C ABI callback function type definitions — matching upstream callback signatures (§14, §20).
//!
//! This module defines all function pointer types used in public upstream structures:
//! SAX1 callbacks, SAX2 callbacks, error callbacks, validity callbacks, XPath callbacks,
//! I/O callbacks, resource loader callbacks, encoding callbacks, and the SAX locator.
//!
//! # Phase 1 status
//!
//! Complete — all callback types from upstream headers are defined.
//!
//! # Safety
//!
//! All callback types are `unsafe extern "C"` because they are called across the FFI boundary
//! with C calling conventions. The caller must ensure:
//! - Function pointers are non-null before invocation (unless nullable per upstream contract)
//! - Pointers passed to callbacks remain valid for the callback's duration
//! - Callbacks observe the upstream ownership/lifetime contract
//! - Thread safety matches upstream expectations

#![allow(non_camel_case_types)]

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

use crate::abi::structs::*;

// ═══════════════════════════════════════════════════════════════════════════════
// SAX1 Callbacks (xmlSAXHandler)
// ═══════════════════════════════════════════════════════════════════════════════

/// Callback for internal DTD subset notification.
///
/// # UPSTREAM-PARITY
///
/// Oracle behavior: Called when `<!DOCTYPE ... [ ... ]>` internal subset is parsed.
/// Parameters are the DOCTYPE name, external ID (or NULL), system ID (or NULL).
pub type internalSubsetSAXFunc = unsafe extern "C" fn(
    ctx: *mut c_void,
    name: *const crate::abi::types::xmlChar,
    ExternalID: *const crate::abi::types::xmlChar,
    SystemID: *const crate::abi::types::xmlChar,
);

/// Callback for standalone document declaration.
///
/// Returns 1 if standalone="yes", 0 if standalone="no", -1 if not declared.
pub type isStandaloneSAXFunc = unsafe extern "C" fn(ctx: *mut c_void) -> c_int;

/// Callback: does the document have an internal subset?
pub type hasInternalSubsetSAXFunc = unsafe extern "C" fn(ctx: *mut c_void) -> c_int;

/// Callback: does the document have an external subset?
pub type hasExternalSubsetSAXFunc = unsafe extern "C" fn(ctx: *mut c_void) -> c_int;

/// Callback to resolve an external entity.
///
/// Returns a newly allocated `xmlParserInputPtr` or NULL.
///
/// # UPSTREAM-PARITY
///
/// Ownership: The returned `xmlParserInputPtr` is owned by the parser context.
pub type resolveEntitySAXFunc = unsafe extern "C" fn(
    ctx: *mut c_void,
    publicId: *const crate::abi::types::xmlChar,
    systemId: *const crate::abi::types::xmlChar,
) -> *mut _xmlParserInput;

/// Callback to get an entity.
///
/// Returns a pointer to an entity or NULL.
///
/// # UPSTREAM-PARITY
///
/// Ownership: The returned entity is owned by the document's entity table.
/// The caller must not free it.
pub type getEntitySAXFunc = unsafe extern "C" fn(
    ctx: *mut c_void,
    name: *const crate::abi::types::xmlChar,
) -> *mut _xmlEntity;

/// Callback for entity declaration.
pub type entityDeclSAXFunc = unsafe extern "C" fn(
    ctx: *mut c_void,
    name: *const crate::abi::types::xmlChar,
    type_: c_int,
    publicId: *const crate::abi::types::xmlChar,
    systemId: *const crate::abi::types::xmlChar,
    content: *mut crate::abi::types::xmlChar,
);

/// Callback for notation declaration.
pub type notationDeclSAXFunc = unsafe extern "C" fn(
    ctx: *mut c_void,
    name: *const crate::abi::types::xmlChar,
    publicId: *const crate::abi::types::xmlChar,
    systemId: *const crate::abi::types::xmlChar,
);

/// Callback for attribute declaration.
pub type attributeDeclSAXFunc = unsafe extern "C" fn(
    ctx: *mut c_void,
    elem: *const crate::abi::types::xmlChar,
    name: *const crate::abi::types::xmlChar,
    type_: c_int,
    def: c_int,
    defaultValue: *const crate::abi::types::xmlChar,
    tree: *mut _xmlEnumeration,
);

/// Callback for element declaration.
pub type elementDeclSAXFunc = unsafe extern "C" fn(
    ctx: *mut c_void,
    name: *const crate::abi::types::xmlChar,
    type_: c_int,
    content: *mut _xmlElementContent,
);

/// Callback for unparsed entity declaration.
pub type unparsedEntityDeclSAXFunc = unsafe extern "C" fn(
    ctx: *mut c_void,
    name: *const crate::abi::types::xmlChar,
    publicId: *const crate::abi::types::xmlChar,
    systemId: *const crate::abi::types::xmlChar,
    notationName: *const crate::abi::types::xmlChar,
);

/// Callback to set the document locator.
///
/// # UPSTREAM-PARITY
///
/// The locator is an opaque structure that provides line/column information.
pub type setDocumentLocatorSAXFunc =
    unsafe extern "C" fn(ctx: *mut c_void, loc: *mut _xmlSAXLocator);

/// Callback for document start.
pub type startDocumentSAXFunc = unsafe extern "C" fn(ctx: *mut c_void);

/// Callback for document end.
pub type endDocumentSAXFunc = unsafe extern "C" fn(ctx: *mut c_void);

/// Callback for element start (SAX1).
///
/// # Parameters
/// - `name`: element name
/// - `atts`: NULL-terminated array of [name, value, name, value, ..., NULL]
pub type startElementSAXFunc = unsafe extern "C" fn(
    ctx: *mut c_void,
    name: *const crate::abi::types::xmlChar,
    atts: *mut *const crate::abi::types::xmlChar,
);

/// Callback for element end (SAX1).
pub type endElementSAXFunc =
    unsafe extern "C" fn(ctx: *mut c_void, name: *const crate::abi::types::xmlChar);

/// Callback for entity reference.
pub type referenceSAXFunc =
    unsafe extern "C" fn(ctx: *mut c_void, name: *const crate::abi::types::xmlChar);

/// Callback for character data.
pub type charactersSAXFunc =
    unsafe extern "C" fn(ctx: *mut c_void, ch: *const crate::abi::types::xmlChar, len: c_int);

/// Callback for ignorable whitespace.
pub type ignorableWhitespaceSAXFunc =
    unsafe extern "C" fn(ctx: *mut c_void, ch: *const crate::abi::types::xmlChar, len: c_int);

/// Callback for processing instructions.
pub type processingInstructionSAXFunc = unsafe extern "C" fn(
    ctx: *mut c_void,
    target: *const crate::abi::types::xmlChar,
    data: *const crate::abi::types::xmlChar,
);

/// Callback for comments.
pub type commentSAXFunc =
    unsafe extern "C" fn(ctx: *mut c_void, value: *const crate::abi::types::xmlChar);

/// Callback for warnings (printf-style, variadic at C call site).
///
/// # UPSTREAM-PARITY
///
/// The `...` is implicit in C; Rust type cannot express variadic extern "C"
/// on stable. The function pointer ABI is identical — C callers pass variadic
/// arguments and the callee uses va_list internally.
pub type warningSAXFunc = unsafe extern "C" fn(ctx: *mut c_void, msg: *const c_char);

/// Callback for errors (printf-style, variadic at C call site).
pub type errorSAXFunc = unsafe extern "C" fn(ctx: *mut c_void, msg: *const c_char);

/// Callback for fatal errors (printf-style, variadic at C call site).
pub type fatalErrorSAXFunc = unsafe extern "C" fn(ctx: *mut c_void, msg: *const c_char);

/// Callback to get a parameter entity.
///
/// Returns a pointer to a parameter entity or NULL.
pub type getParameterEntitySAXFunc = unsafe extern "C" fn(
    ctx: *mut c_void,
    name: *const crate::abi::types::xmlChar,
) -> *mut _xmlEntity;

/// Callback for CDATA block.
pub type cdataBlockSAXFunc =
    unsafe extern "C" fn(ctx: *mut c_void, value: *const crate::abi::types::xmlChar, len: c_int);

/// Callback for external subset notification.
pub type externalSubsetSAXFunc = unsafe extern "C" fn(
    ctx: *mut c_void,
    name: *const crate::abi::types::xmlChar,
    ExternalID: *const crate::abi::types::xmlChar,
    SystemID: *const crate::abi::types::xmlChar,
);

/// Callback for initializing the SAX handler.
///
/// # UPSTREAM-PARITY
///
/// This is a libxml2-internal callback used to set SAX2 callbacks when SAX1 callbacks
/// are not provided. Not typically set by downstream users.
pub type initSAXFunc = unsafe extern "C" fn(ctx: *mut c_void, handler: *mut _xmlSAXHandler);

// ═══════════════════════════════════════════════════════════════════════════════
// SAX2 Callbacks
// ═══════════════════════════════════════════════════════════════════════════════

/// Callback for element start (SAX2/namespaced).
///
/// # Parameters
/// - `localname`: element local name
/// - `prefix`: element namespace prefix (or NULL)
/// - `URI`: element namespace URI (or NULL)
/// - `nb_namespaces`: number of namespace declarations
/// - `namespaces`: array of [prefix, URI, prefix, URI, ...] (size 2*nb_namespaces)
/// - `nb_attributes`: total number of attributes
/// - `nb_defaulted`: number of defaulted attributes (from DTD)
/// - `attributes`: array of [localname, prefix, URI, value, value_end, ...]
///   each attribute is 5 entries, value_end is pointer past last char of value
pub type startElementNsSAX2Func = unsafe extern "C" fn(
    ctx: *mut c_void,
    localname: *const crate::abi::types::xmlChar,
    prefix: *const crate::abi::types::xmlChar,
    URI: *const crate::abi::types::xmlChar,
    nb_namespaces: c_int,
    namespaces: *mut *const crate::abi::types::xmlChar,
    nb_attributes: c_int,
    nb_defaulted: c_int,
    attributes: *mut *const crate::abi::types::xmlChar,
);

/// Callback for element end (SAX2/namespaced).
pub type endElementNsSAX2Func = unsafe extern "C" fn(
    ctx: *mut c_void,
    localname: *const crate::abi::types::xmlChar,
    prefix: *const crate::abi::types::xmlChar,
    URI: *const crate::abi::types::xmlChar,
);

// ═══════════════════════════════════════════════════════════════════════════════
// SAX Locator
// ═══════════════════════════════════════════════════════════════════════════════

/// The SAX locator structure providing line/column information.
///
/// # UPSTREAM-PARITY
///
/// This is an opaque structure from the perspective of SAX handlers.
/// Upstream defines it as:
/// ```c
/// typedef struct _xmlSAXLocator xmlSAXLocator;
/// typedef xmlSAXLocator *xmlSAXLocatorPtr;
/// struct _xmlSAXLocator {
///     xmlChar *(*getPublicId)(void *ctx);
///     xmlChar *(*getSystemId)(void *ctx);
///     int      (*getLineNumber)(void *ctx);
///     int      (*getColumnNumber)(void *ctx);
/// };
/// ```
#[repr(C)]
pub struct _xmlSAXLocator {
    /// Get the public ID of the current document position.
    pub getPublicId:
        Option<unsafe extern "C" fn(ctx: *mut c_void) -> *const crate::abi::types::xmlChar>,
    /// Get the system ID of the current document position.
    pub getSystemId:
        Option<unsafe extern "C" fn(ctx: *mut c_void) -> *const crate::abi::types::xmlChar>,
    /// Get the line number of the current document position.
    pub getLineNumber: Option<unsafe extern "C" fn(ctx: *mut c_void) -> c_int>,
    /// Get the column number of the current document position.
    pub getColumnNumber: Option<unsafe extern "C" fn(ctx: *mut c_void) -> c_int>,
}

/// Pointer to a SAX locator.
pub type xmlSAXLocatorPtr = *mut _xmlSAXLocator;

// ═══════════════════════════════════════════════════════════════════════════════
// Error Callbacks
// ═══════════════════════════════════════════════════════════════════════════════

/// Structured error handler callback.
///
/// Called with a pointer to the error structure. The error structure is valid
/// only during the callback invocation.
pub type xmlStructuredErrorFunc = unsafe extern "C" fn(ctx: *mut c_void, error: *const _xmlError);

/// Generic error handler callback (printf-style, variadic at C call site).
///
/// # UPSTREAM-PARITY
///
/// This is the older error reporting mechanism. New code should use the structured
/// error handler (`xmlStructuredErrorFunc`) instead.
pub type xmlGenericErrorFunc = unsafe extern "C" fn(ctx: *mut c_void, msg: *const c_char);

// ═══════════════════════════════════════════════════════════════════════════════
// Validity Callbacks
// ═══════════════════════════════════════════════════════════════════════════════

/// Validity error handler callback (printf-style, variadic at C call site).
pub type xmlValidityErrorFunc = unsafe extern "C" fn(ctx: *mut c_void, msg: *const c_char);

/// Validity warning handler callback (printf-style, variadic at C call site).
pub type xmlValidityWarningFunc = unsafe extern "C" fn(ctx: *mut c_void, msg: *const c_char);

// ═══════════════════════════════════════════════════════════════════════════════
// I/O Callbacks
// ═══════════════════════════════════════════════════════════════════════════════

/// Read callback for custom input.
///
/// Should fill `buffer` with up to `len` bytes.
/// Returns the number of bytes read, 0 on EOF, or -1 on error.
pub type xmlInputReadCallback =
    unsafe extern "C" fn(context: *mut c_void, buffer: *mut c_char, len: c_int) -> c_int;

/// Close callback for custom input.
///
/// Returns 0 on success, -1 on error.
pub type xmlInputCloseCallback = unsafe extern "C" fn(context: *mut c_void) -> c_int;

/// Write callback for custom output.
///
/// Should write up to `len` bytes from `buffer`.
/// Returns the number of bytes written, or -1 on error.
pub type xmlOutputWriteCallback =
    unsafe extern "C" fn(context: *mut c_void, buffer: *const c_char, len: c_int) -> c_int;

/// Close callback for custom output.
///
/// Returns 0 on success, -1 on error.
pub type xmlOutputCloseCallback = unsafe extern "C" fn(context: *mut c_void) -> c_int;

// ═══════════════════════════════════════════════════════════════════════════════
// XPath Callbacks
// ═══════════════════════════════════════════════════════════════════════════════

/// Variable lookup function for XPath.
///
/// Returns an `xmlXPathObjectPtr` representing the variable's value, or NULL.
///
/// # UPSTREAM-PARITY
///
/// Ownership: The returned object is owned by the caller (must be freed).
pub type xmlXPathVariableLookupFunc = unsafe extern "C" fn(
    ctxt: *mut c_void,
    name: *const crate::abi::types::xmlChar,
    ns_uri: *const crate::abi::types::xmlChar,
) -> *mut _xmlXPathObject;

/// Function lookup function for XPath extensions.
///
/// Returns a function pointer or NULL.
pub type xmlXPathFuncLookupFunc = unsafe extern "C" fn(
    ctxt: *mut c_void,
    name: *const crate::abi::types::xmlChar,
    ns_uri: *const crate::abi::types::xmlChar,
) -> *mut c_void;

// ═══════════════════════════════════════════════════════════════════════════════
// Resource Loader Callbacks
// ═══════════════════════════════════════════════════════════════════════════════

/// Resource loader callback.
///
/// Loads a resource identified by `url` and returns a parser input.
///
/// # UPSTREAM-PARITY
///
/// - `type_`: The type of resource being loaded
///   (1 = parser entity, 2 = stylesheet include, 3 = stylesheet import, 4 = document)
pub type xmlResourceLoader = unsafe extern "C" fn(
    ctxt: *mut c_void,
    url: *const c_char,
    options: c_int,
    type_: c_int,
) -> *mut _xmlParserInput;

// ═══════════════════════════════════════════════════════════════════════════════
// Encoding Callbacks
// ═══════════════════════════════════════════════════════════════════════════════

/// Character encoding conversion implementation.
///
/// # UPSTREAM-PARITY
///
/// This is used to plug custom encoding converters into the I/O subsystem.
/// Returns the number of bytes written, or -1 on error.
pub type xmlCharEncConvImpl = unsafe extern "C" fn(
    name: *mut *const crate::abi::types::xmlChar,
    out: *mut *mut crate::abi::types::xmlChar,
    outlen: *mut c_int,
    in_: *const crate::abi::types::xmlChar,
    inlen: *mut c_int,
) -> c_int;

// ═══════════════════════════════════════════════════════════════════════════════
// Catalog Callbacks
// ═══════════════════════════════════════════════════════════════════════════════

/// Catalog preference callback.
///
/// Returns 1 if the system prefers XML catalogs, 0 otherwise.
pub type xmlCatalogPreferFunc = unsafe extern "C" fn() -> c_int;

// ═══════════════════════════════════════════════════════════════════════════════
// Allocator Callback Types
// ═══════════════════════════════════════════════════════════════════════════════

/// Free function type for allocator hooks.
pub type xmlFreeFunc = unsafe extern "C" fn(ptr: *mut c_void);

/// Malloc function type for allocator hooks.
pub type xmlMallocFunc = unsafe extern "C" fn(size: usize) -> *mut c_void;

/// Realloc function type for allocator hooks.
pub type xmlReallocFunc = unsafe extern "C" fn(ptr: *mut c_void, size: usize) -> *mut c_void;

/// Strdup function type for allocator hooks.
pub type xmlStrdupFunc = unsafe extern "C" fn(str: *const c_char) -> *mut c_void;

// ═══════════════════════════════════════════════════════════════════════════════
// Module Callbacks
// ═══════════════════════════════════════════════════════════════════════════════

/// Module register/unregister callback.
pub type xmlModuleRegisterFunc = unsafe extern "C" fn(module: *mut c_void) -> c_int;

// ═══════════════════════════════════════════════════════════════════════════════
// Pattern Callbacks
// ═══════════════════════════════════════════════════════════════════════════════

/// Stream callback for pattern matching.
pub type xmlStreamCtxtPtr = *mut c_void;

// ═══════════════════════════════════════════════════════════════════════════════
// Hash Table Callbacks
// ═══════════════════════════════════════════════════════════════════════════════

/// Deallocator function for hash table entries.
///
/// Called when removing an entry from a hash table.
/// The function receives the payload and the name (key) of the entry.
pub type xmlHashDeallocator =
    unsafe extern "C" fn(payload: *mut c_void, name: *mut crate::abi::types::xmlChar);

/// Copier function for hash table entries.
///
/// Called when copying a hash table. Returns a copy of the payload.
pub type xmlHashCopier = unsafe extern "C" fn(
    payload: *mut c_void,
    name: *const crate::abi::types::xmlChar,
) -> *mut c_void;

/// Scanner function for hash table entries.
///
/// Called for each entry during xmlHashScan.
pub type xmlHashScanner = unsafe extern "C" fn(
    payload: *mut c_void,
    data: *mut c_void,
    name: *const crate::abi::types::xmlChar,
);

/// Full scanner function for hash table entries.
///
/// Called for each entry during xmlHashScanFull. Includes all three key parts.
pub type xmlHashScannerFull = unsafe extern "C" fn(
    payload: *mut c_void,
    data: *mut c_void,
    name: *const crate::abi::types::xmlChar,
    name2: *const crate::abi::types::xmlChar,
    name3: *const crate::abi::types::xmlChar,
);