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
//! 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
use c_void;
use ;
use crate*;
// ═══════════════════════════════════════════════════════════════════════════════
// 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;
/// 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 ;
/// Callback: does the document have an internal subset?
pub type hasInternalSubsetSAXFunc = unsafe extern "C" fn ;
/// Callback: does the document have an external subset?
pub type hasExternalSubsetSAXFunc = unsafe extern "C" fn ;
/// 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 ;
/// 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 ;
/// Callback for entity declaration.
pub type entityDeclSAXFunc = unsafe extern "C" fn;
/// Callback for notation declaration.
pub type notationDeclSAXFunc = unsafe extern "C" fn;
/// Callback for attribute declaration.
pub type attributeDeclSAXFunc = unsafe extern "C" fn;
/// Callback for element declaration.
pub type elementDeclSAXFunc = unsafe extern "C" fn;
/// Callback for unparsed entity declaration.
pub type unparsedEntityDeclSAXFunc = unsafe extern "C" fn;
/// 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;
/// Callback for document start.
pub type startDocumentSAXFunc = unsafe extern "C" fn;
/// Callback for document end.
pub type endDocumentSAXFunc = unsafe extern "C" fn;
/// 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;
/// Callback for element end (SAX1).
pub type endElementSAXFunc =
unsafe extern "C" fn;
/// Callback for entity reference.
pub type referenceSAXFunc =
unsafe extern "C" fn;
/// Callback for character data.
pub type charactersSAXFunc =
unsafe extern "C" fn;
/// Callback for ignorable whitespace.
pub type ignorableWhitespaceSAXFunc =
unsafe extern "C" fn;
/// Callback for processing instructions.
pub type processingInstructionSAXFunc = unsafe extern "C" fn;
/// Callback for comments.
pub type commentSAXFunc =
unsafe extern "C" fn;
/// 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;
/// Callback for errors (printf-style, variadic at C call site).
pub type errorSAXFunc = unsafe extern "C" fn;
/// Callback for fatal errors (printf-style, variadic at C call site).
pub type fatalErrorSAXFunc = unsafe extern "C" fn;
/// Callback to get a parameter entity.
///
/// Returns a pointer to a parameter entity or NULL.
pub type getParameterEntitySAXFunc = unsafe extern "C" fn ;
/// Callback for CDATA block.
pub type cdataBlockSAXFunc =
unsafe extern "C" fn;
/// Callback for external subset notification.
pub type externalSubsetSAXFunc = unsafe extern "C" fn;
/// 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;
// ═══════════════════════════════════════════════════════════════════════════════
// 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;
/// Callback for element end (SAX2/namespaced).
pub type endElementNsSAX2Func = unsafe extern "C" fn;
// ═══════════════════════════════════════════════════════════════════════════════
// 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);
/// };
/// ```
/// 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;
/// 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;
// ═══════════════════════════════════════════════════════════════════════════════
// Validity Callbacks
// ═══════════════════════════════════════════════════════════════════════════════
/// Validity error handler callback (printf-style, variadic at C call site).
pub type xmlValidityErrorFunc = unsafe extern "C" fn;
/// Validity warning handler callback (printf-style, variadic at C call site).
pub type xmlValidityWarningFunc = unsafe extern "C" fn;
// ═══════════════════════════════════════════════════════════════════════════════
// 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 ;
/// Close callback for custom input.
///
/// Returns 0 on success, -1 on error.
pub type xmlInputCloseCallback = unsafe extern "C" fn ;
/// 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 ;
/// Close callback for custom output.
///
/// Returns 0 on success, -1 on error.
pub type xmlOutputCloseCallback = unsafe extern "C" fn ;
// ═══════════════════════════════════════════════════════════════════════════════
// 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 ;
/// Function lookup function for XPath extensions.
///
/// Returns a function pointer or NULL.
pub type xmlXPathFuncLookupFunc = unsafe extern "C" fn ;
// ═══════════════════════════════════════════════════════════════════════════════
// Resource Loader Callbacks
// ═══════════════════════════════════════════════════════════════════════════════
/// Resource loader callback.
///
/// Loads a resource identified by `url` and returns a parser input.
///
/// # UPSTREAM-PARITY
///
/// Declared in upstream `parser.h` (2.15+):
///
/// ```c
/// typedef xmlParserErrors
/// (*xmlResourceLoader)(void *ctxt, const char *url, const char *publicId,
/// xmlResourceType type, xmlParserInputFlags flags,
/// xmlParserInput **out);
/// ```
///
/// Note: a different, older `void *(*)(const char*, const char*, int, void*)`
/// signature appears in very old libxml2 headers; the 2.15 contract wins.
pub type xmlResourceLoader = unsafe extern "C" fn ; // xmlParserErrors
// ═══════════════════════════════════════════════════════════════════════════════
// Encoding Callbacks
// ═══════════════════════════════════════════════════════════════════════════════
/// Character encoding input conversion function.
///
/// Converts from the handler's input encoding to UTF-8.
/// `in` and `inlen` describe input; `out` and `outlen` describe output buffer.
/// Returns the number of bytes written, or -1 on error.
pub type xmlCharEncodingInputFunc = unsafe extern "C" fn ;
/// Modern character encoding conversion function (upstream encoding.h).
///
/// ```c
/// typedef xmlCharEncError
/// (*xmlCharEncConvFunc)(void *vctxt, unsigned char *out, int *outlen,
/// const unsigned char *in, int *inlen, int flush);
/// ```
pub type xmlCharEncConvFunc = unsafe extern "C" fn ; // xmlCharEncError
/// Conversion-context destructor (upstream encoding.h).
///
/// ```c
/// typedef void (*xmlCharEncConvCtxtDtor)(void *vctxt);
/// ```
pub type xmlCharEncConvCtxtDtor = unsafe extern "C" fn;
/// Character encoding output conversion function.
///
/// Converts from UTF-8 to the handler's output encoding.
/// `in` and `inlen` describe input; `out` and `outlen` describe output buffer.
/// Returns the number of bytes written, or -1 on error.
pub type xmlCharEncodingOutputFunc = unsafe extern "C" fn ;
/// Character encoding conversion implementation.
///
/// # UPSTREAM-PARITY
///
/// Declared in upstream `encoding.h` (2.15+):
///
/// ```c
/// typedef xmlParserErrors
/// (*xmlCharEncConvImpl)(void *vctxt, const char *name, xmlCharEncFlags flags,
/// xmlCharEncodingHandler **out);
/// ```
///
/// Returns an xmlParserErrors code; `out` receives a new handler on success.
pub type xmlCharEncConvImpl = unsafe extern "C" fn ; // xmlParserErrors
// ═══════════════════════════════════════════════════════════════════════════════
// Catalog Callbacks
// ═══════════════════════════════════════════════════════════════════════════════
/// Catalog preference callback.
///
/// Returns 1 if the system prefers XML catalogs, 0 otherwise.
pub type xmlCatalogPreferFunc = unsafe extern "C" fn ;
// ═══════════════════════════════════════════════════════════════════════════════
// Allocator Callback Types
// ═══════════════════════════════════════════════════════════════════════════════
/// Free function type for allocator hooks.
pub type xmlFreeFunc = unsafe extern "C" fn;
/// Malloc function type for allocator hooks.
pub type xmlMallocFunc = unsafe extern "C" fn ;
/// Realloc function type for allocator hooks.
pub type xmlReallocFunc = unsafe extern "C" fn ;
/// Strdup function type for allocator hooks.
pub type xmlStrdupFunc = unsafe extern "C" fn ;
// ═══════════════════════════════════════════════════════════════════════════════
// Module Callbacks
// ═══════════════════════════════════════════════════════════════════════════════
/// Module register/unregister callback.
pub type xmlModuleRegisterFunc = unsafe extern "C" fn ;
// ═══════════════════════════════════════════════════════════════════════════════
// 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;
/// 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 ;
/// Scanner function for hash table entries.
///
/// Called for each entry during xmlHashScan.
pub type xmlHashScanner = unsafe extern "C" fn;
/// 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;