exml 0.7.2

Pure Rust XML library based on libxml2
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
//! Provide methods and data structures for handling I/O actions.
//!
//! This module is based on `libxml/xmlIO.h`, `xmlIO.c`, and so on in `libxml2-v2.11.8`.  
//! Please refer to original libxml2 documents also.

// Copyright of the original code is the following.
// --------
// Summary: interface for the I/O interfaces used by the parser
// Description: interface for the I/O interfaces used by the parser
//
// Copy: See Copyright for the status of this software.
//
// Author: Daniel Veillard
// --------
// xmlIO.c : implementation of the I/O interfaces used by the parser
//
// See Copyright for the status of this software.
//
// daniel@veillard.com
//
// 14 Nov 2000 ht - for VMS, truncated name of long functions to under 32 char

mod input;
#[cfg(feature = "libxml_output")]
mod output;

#[cfg(feature = "libxml_output")]
use std::io::Write;
use std::{
    borrow::Cow,
    env::current_dir,
    fs::{File, metadata},
    io::{self, ErrorKind, Read, stdin},
    path::{Path, PathBuf},
};

use libc::{
    __errno_location, EACCES, EADDRINUSE, EAFNOSUPPORT, EAGAIN, EALREADY, EBADF, EBADMSG, EBUSY,
    ECANCELED, ECHILD, ECONNREFUSED, EDEADLK, EDOM, EEXIST, EFAULT, EFBIG, EINPROGRESS, EINTR,
    EINVAL, EIO, EISCONN, EISDIR, EMFILE, EMLINK, EMSGSIZE, ENAMETOOLONG, ENFILE, ENODEV, ENOENT,
    ENOEXEC, ENOLCK, ENOMEM, ENOSPC, ENOSYS, ENOTDIR, ENOTEMPTY, ENOTSOCK, ENOTSUP, ENOTTY, ENXIO,
    EPERM, EPIPE, ERANGE, EROFS, ESPIPE, ESRCH, ETIMEDOUT, EXDEV,
};

use crate::{
    encoding::find_encoding_handler,
    error::{__xml_simple_error, XmlErrorDomain, XmlParserErrors},
    nanohttp::XmlNanoHTTPCtxt,
    parser::{__xml_err_encoding, XmlParserCtxt, XmlParserCtxtPtr, XmlParserInput},
    uri::{canonic_path, unescape_url},
};

pub use input::*;
#[cfg(feature = "libxml_output")]
pub use output::*;

const IOERR: &[&str] = &[
    "Unknown IO error",                    /* UNKNOWN */
    "Permission denied",                   /* EACCES */
    "Resource temporarily unavailable",    /* EAGAIN */
    "Bad file descriptor",                 /* EBADF */
    "Bad message",                         /* EBADMSG */
    "Resource busy",                       /* EBUSY */
    "Operation canceled",                  /* ECANCELED */
    "No child processes",                  /* ECHILD */
    "Resource deadlock avoided",           /* EDEADLK */
    "Domain error",                        /* EDOM */
    "File exists",                         /* EEXIST */
    "Bad address",                         /* EFAULT */
    "File too large",                      /* EFBIG */
    "Operation in progress",               /* EINPROGRESS */
    "Interrupted function call",           /* EINTR */
    "Invalid argument",                    /* EINVAL */
    "Input/output error",                  /* EIO */
    "Is a directory",                      /* EISDIR */
    "Too many open files",                 /* EMFILE */
    "Too many links",                      /* EMLINK */
    "Inappropriate message buffer length", /* EMSGSIZE */
    "Filename too long",                   /* ENAMETOOLONG */
    "Too many open files in system",       /* ENFILE */
    "No such device",                      /* ENODEV */
    "No such file or directory",           /* ENOENT */
    "Exec format error",                   /* ENOEXEC */
    "No locks available",                  /* ENOLCK */
    "Not enough space",                    /* ENOMEM */
    "No space left on device",             /* ENOSPC */
    "Function not implemented",            /* ENOSYS */
    "Not a directory",                     /* ENOTDIR */
    "Directory not empty",                 /* ENOTEMPTY */
    "Not supported",                       /* ENOTSUP */
    "Inappropriate I/O control operation", /* ENOTTY */
    "No such device or address",           /* ENXIO */
    "Operation not permitted",             /* EPERM */
    "Broken pipe",                         /* EPIPE */
    "Result too large",                    /* ERANGE */
    "Read-only file system",               /* EROFS */
    "Invalid seek",                        /* ESPIPE */
    "No such process",                     /* ESRCH */
    "Operation timed out",                 /* ETIMEDOUT */
    "Improper link",                       /* EXDEV */
    "Attempt to load network entity %s",   /* XML_IO_NETWORK_ATTEMPT */
    "encoder error",                       /* XML_IO_ENCODER */
    "flush error",
    "write error",
    "no input",
    "buffer full",
    "loading error",
    "not a socket",           /* ENOTSOCK */
    "already connected",      /* EISCONN */
    "connection refused",     /* ECONNREFUSED */
    "unreachable network",    /* ENETUNREACH */
    "address in use",         /* EADDRINUSE */
    "already in use",         /* EALREADY */
    "unknown address family", /* EAFNOSUPPORT */
];

/// Handle an I/O error
#[doc(alias = "__xmlIOErr")]
pub(crate) fn __xml_ioerr(domain: XmlErrorDomain, mut code: XmlParserErrors, extra: Option<&str>) {
    let mut idx: u32;
    let errno = unsafe { *__errno_location() };

    if code == XmlParserErrors::XmlErrOK {
        if errno == 0 {
            code = XmlParserErrors::XmlErrOK;
        } else if errno == EACCES {
            code = XmlParserErrors::XmlIOEACCES;
        } else if errno == EAGAIN {
            code = XmlParserErrors::XmlIOEAGAIN;
        } else if errno == EBADF {
            code = XmlParserErrors::XmlIOEBADF;
        } else if errno == EBADMSG {
            code = XmlParserErrors::XmlIOEBADMSG;
        } else if errno == EBUSY {
            code = XmlParserErrors::XmlIOEBUSY;
        } else if errno == ECANCELED {
            code = XmlParserErrors::XmlIOECANCELED;
        } else if errno == ECHILD {
            code = XmlParserErrors::XmlIOECHILD;
        } else if errno == EDEADLK {
            code = XmlParserErrors::XmlIOEDEADLK;
        } else if errno == EDOM {
            code = XmlParserErrors::XmlIOEDOM;
        } else if errno == EEXIST {
            code = XmlParserErrors::XmlIOEEXIST;
        } else if errno == EFAULT {
            code = XmlParserErrors::XmlIOEFAULT;
        } else if errno == EFBIG {
            code = XmlParserErrors::XmlIOEFBIG;
        } else if errno == EINPROGRESS {
            code = XmlParserErrors::XmlIOEINPROGRESS;
        } else if errno == EINTR {
            code = XmlParserErrors::XmlIOEINTR;
        } else if errno == EINVAL {
            code = XmlParserErrors::XmlIOEINVAL;
        } else if errno == EIO {
            code = XmlParserErrors::XmlIOEIO;
        } else if errno == EISDIR {
            code = XmlParserErrors::XmlIOEISDIR;
        } else if errno == EMFILE {
            code = XmlParserErrors::XmlIOEMFILE;
        } else if errno == EMLINK {
            code = XmlParserErrors::XmlIOEMLINK;
        } else if errno == EMSGSIZE {
            code = XmlParserErrors::XmlIOEMSGSIZE;
        } else if errno == ENAMETOOLONG {
            code = XmlParserErrors::XmlIOENAMETOOLONG;
        } else if errno == ENFILE {
            code = XmlParserErrors::XmlIOENFILE;
        } else if errno == ENODEV {
            code = XmlParserErrors::XmlIOENODEV;
        } else if errno == ENOENT {
            code = XmlParserErrors::XmlIOENOENT;
        } else if errno == ENOEXEC {
            code = XmlParserErrors::XmlIOENOEXEC;
        } else if errno == ENOLCK {
            code = XmlParserErrors::XmlIOENOLCK;
        } else if errno == ENOMEM {
            code = XmlParserErrors::XmlIOENOMEM;
        } else if errno == ENOSPC {
            code = XmlParserErrors::XmlIOENOSPC;
        } else if errno == ENOSYS {
            code = XmlParserErrors::XmlIOENOSYS;
        } else if errno == ENOTDIR {
            code = XmlParserErrors::XmlIOENOTDIR;
        } else if errno == ENOTEMPTY {
            code = XmlParserErrors::XmlIOENOTEMPTY;
        } else if errno == ENOTSUP {
            code = XmlParserErrors::XmlIOENOTSUP;
        } else if errno == ENOTTY {
            code = XmlParserErrors::XmlIOENOTTY;
        } else if errno == ENXIO {
            code = XmlParserErrors::XmlIOENXIO;
        } else if errno == EPERM {
            code = XmlParserErrors::XmlIOEPERM;
        } else if errno == EPIPE {
            code = XmlParserErrors::XmlIOEPIPE;
        } else if errno == ERANGE {
            code = XmlParserErrors::XmlIOERANGE;
        } else if errno == EROFS {
            code = XmlParserErrors::XmlIOEROFS;
        } else if errno == ESPIPE {
            code = XmlParserErrors::XmlIOESPIPE;
        } else if errno == ESRCH {
            code = XmlParserErrors::XmlIOESRCH;
        } else if errno == ETIMEDOUT {
            code = XmlParserErrors::XmlIOETIMEOUT;
        } else if errno == EXDEV {
            code = XmlParserErrors::XmlIOEXDEV;
        } else if errno == ENOTSOCK {
            code = XmlParserErrors::XmlIOENOTSOCK;
        } else if errno == EISCONN {
            code = XmlParserErrors::XmlIOEISCONN;
        } else if errno == ECONNREFUSED {
            code = XmlParserErrors::XmlIOECONNREFUSED;
        } else if errno == EADDRINUSE {
            code = XmlParserErrors::XmlIOEADDRINUSE;
        } else if errno == EALREADY {
            code = XmlParserErrors::XmlIOEALREADY;
        } else if errno == EAFNOSUPPORT {
            code = XmlParserErrors::XmlIOEAFNOSUPPORT;
        } else {
            code = XmlParserErrors::XmlIOUnknown;
        }
    }
    idx = 0;
    if code as i32 >= XmlParserErrors::XmlIOUnknown as i32 {
        idx = code as u32 - XmlParserErrors::XmlIOUnknown as u32;
    }
    if idx >= IOERR.len() as u32 {
        idx = 0;
    }

    let msg: Cow<'static, str> = match idx {
        43 => format!(
            "Attempt to load network entity {}",
            extra.expect("Internal Error")
        )
        .into(), /* XML_IO_NETWORK_ATTEMPT */
        index => IOERR[index as usize].into(),
    };

    __xml_simple_error!(domain, code, None, &msg);
}

/// Handle an I/O error
#[doc(alias = "xmlIOErr")]
pub(crate) fn xml_ioerr(code: XmlParserErrors, extra: Option<&str>) {
    __xml_ioerr(XmlErrorDomain::XmlFromIO, code, extra);
}

#[cfg(feature = "libxml_output")]
pub(crate) fn write_quoted<'a>(out: &mut (impl Write + 'a), s: &str) -> std::io::Result<()> {
    if s.contains('"') {
        if s.contains('\'') {
            // If `s` contains both single and double-quote, quote with double-quote
            // and escape inner double-quotes
            write!(out, "\"")?;
            let mut split = s.split('"');
            write!(out, "{}", split.next().unwrap())?;
            for chunk in split {
                write!(out, "&quot;{chunk}")?;
            }
            write!(out, "\"")?;
        } else {
            // If `s` contains only double-quote, quote with single-quote
            write!(out, "'{s}'")?;
        }
    } else {
        // If `s` does not contain double-quotes, quote with double-quote
        write!(out, "\"{s}\"")?;
    }
    Ok(())
}

const MINLEN: usize = 4000;

/// lookup the directory for that file
///
/// Returns a new allocated string containing the directory, or NULL.
#[doc(alias = "xmlParserGetDirectory")]
pub fn xml_parser_get_directory(filename: impl AsRef<Path>) -> Option<PathBuf> {
    fn _xml_parser_get_directory(filename: &Path) -> Option<PathBuf> {
        filename
            .parent()
            .map(|p| p.to_path_buf())
            .or_else(|| current_dir().ok())
    }
    _xml_parser_get_directory(filename.as_ref())
}

/// Take a block of UTF-8 chars in and escape them.
/// Returns 0 if success, or -1 otherwise
///
/// The value of `inlen` after return is the number of octets consumed
/// if the return value is positive, else unpredictable.
/// The value of `outlen` after return is the number of octets consumed.
#[doc(alias = "xmlEscapeContent")]
#[cfg(feature = "libxml_output")]
fn xml_escape_content(input: &str, output: &mut String) -> i32 {
    for input in input.chars() {
        match input {
            '<' => output.push_str("&lt;"),
            '>' => output.push_str("&gt;"),
            '&' => output.push_str("&amp;"),
            '\r' => output.push_str("&#13;"),
            c => output.push(c),
        }
    }
    0
}

/// Handle a resource access error
#[doc(alias = "__xmlLoaderErr")]
macro_rules! __xml_loader_err {
    ($ctx:expr, $msg:literal) => {
        $crate::io::__xml_loader_err!(@inner $ctx, $msg, None);
    };
    ($ctx:expr, $msg:literal, $filename:expr) => {
        let msg = format!($msg, $filename);
        $crate::io::__xml_loader_err!(@inner $ctx, &msg, Some($filename.to_owned().into()));
    };
    (@inner $ctx:expr, $msg:expr, $filename:expr) => {
        use $crate::{
            error::XmlErrorLevel,
            globals::{GenericError, StructuredError},
            parser::XML_SAX2_MAGIC,
        };
        let mut schannel: Option<StructuredError> = None;
        let mut channel: Option<GenericError> = None;
        let mut data = None;
        let mut level = XmlErrorLevel::XmlErrError;

        if !$ctx.disable_sax
            || !matches!($ctx.instate, $crate::parser::XmlParserInputState::XmlParserEOF)
        {
            if let Some(sax) = $ctx.sax.as_deref_mut() {
                if $ctx.validate {
                    channel = sax.error;
                    level = XmlErrorLevel::XmlErrError;
                } else {
                    channel = sax.warning;
                    level = XmlErrorLevel::XmlErrWarning;
                }
                if sax.initialized == XML_SAX2_MAGIC as u32 {
                    schannel = sax.serror;
                }
                data = $ctx.user_data.clone();
            }
            $crate::error::__xml_raise_error!(
                schannel,
                channel,
                data,
                $ctx as XmlParserCtxtPtr as _,
                None,
                $crate::error::XmlErrorDomain::XmlFromIO,
                $crate::error::XmlParserErrors::XmlIOLoadError,
                level,
                None,
                0,
                $filename,
                None,
                None,
                0,
                0,
                Some($msg),
            );
        }
    };
}
pub(crate) use __xml_loader_err;

/// Check an input in case it was created from an HTTP stream,
/// in that case it will handle encoding and update of the base URL in case of redirection.  
/// It also checks for HTTP errors in which case the input is cleanly freed up
/// and an appropriate error is raised in context
///
/// Returns the input or NULL in case of HTTP error.
#[doc(alias = "xmlCheckHTTPInput")]
pub fn xml_check_http_input<'a>(
    ctxt: &mut XmlParserCtxt,
    mut ret: Option<XmlParserInput<'a>>,
) -> Option<XmlParserInput<'a>> {
    #[cfg(feature = "http")]
    {
        if ret.is_some()
            && ret.as_ref().unwrap().buf.is_some()
            && ret
                .as_mut()
                .unwrap()
                .buf
                .as_mut()
                .unwrap()
                .nanohttp_context()
                .is_some()
        {
            let buf = ret.as_mut().unwrap().buf.as_mut().unwrap();
            let context = buf.nanohttp_context().unwrap();
            let code = context.return_code();
            if code >= 400 {
                // fatal error
                if let Some(filename) = ret.as_ref().unwrap().filename.as_deref() {
                    __xml_loader_err!(ctxt, "failed to load HTTP resource \"{}\"\n", filename);
                } else {
                    __xml_loader_err!(ctxt, "failed to load HTTP resource\n");
                }

                return None;
            }
            let is_mime_xml = context
                .mime_type()
                .filter(|mime| mime.contains("/xml") || mime.contains("+xml"))
                .is_some();
            let encoding = context.encoding().map(|encoding| encoding.to_owned());
            let redirection = context
                .redirection()
                .map(|redirection| redirection.to_owned());
            if is_mime_xml {
                if let Some(encoding) = encoding {
                    if let Some(handler) = find_encoding_handler(&encoding) {
                        ctxt.switch_input_encoding(ret.as_mut().unwrap(), handler);
                    } else {
                        __xml_err_encoding!(
                            &mut *ctxt,
                            XmlParserErrors::XmlErrUnknownEncoding,
                            "Unknown encoding {}",
                            encoding
                        );
                    }
                    if ret.as_mut().unwrap().encoding.is_none() {
                        ret.as_mut().unwrap().encoding = Some(encoding);
                    }
                }
            }
            if let Some(redir) = redirection {
                ret.as_mut().unwrap().directory = None;
                ret.as_mut().unwrap().filename = Some(redir);
            }
        }
    }
    ret
}

/// This function is obsolete.
/// Please see `xmlURIFromPath` in uri.c for a better solution.
///
/// Returns a canonicalized version of the path
#[doc(alias = "xmlNormalizeWindowsPath")]
pub fn xml_normalize_windows_path(path: &str) -> Cow<'_, str> {
    canonic_path(path)
}

/// function checks to see if `path` is a valid source (file, socket...) for XML.
///
/// if stat is not available on the target machine, returns 1.  
/// if stat fails, returns 0 (if calling stat on the filename fails, it can't be right).  
/// if stat succeeds and the file is a directory, returns 2.  
/// otherwise returns 1.
#[doc(alias = "xmlCheckFilename")]
pub fn xml_check_filename(path: impl AsRef<Path>) -> i32 {
    fn check_filename(path: &Path) -> i32 {
        match metadata(path) {
            Ok(meta) => {
                if meta.is_dir() {
                    2
                } else {
                    1
                }
            }
            _ => 0,
        }
    }
    check_filename(path.as_ref())
}

#[derive(Debug, Clone, Copy)]
pub struct DefaultFileIOCallbacks;

impl XmlInputCallback for DefaultFileIOCallbacks {
    fn is_match(&self, _filename: &str) -> bool {
        true
    }

    fn open(&mut self, filename: &str) -> io::Result<Box<dyn Read>> {
        if filename == "-" {
            return Ok(Box::new(stdin()));
        }

        #[cfg(target_os = "windows")]
        let path = filename
            .strip_prefix("file://localhost/")
            .or_else(|| filename.strip_prefix("file:///"))
            .or_else(|| filename.strip_prefix("file:/"))
            .unwrap_or(filename);
        #[cfg(not(target_os = "windows"))]
        let path = filename
            .starts_with("file://localhost/")
            .then(|| &filename[16..])
            .or_else(|| filename.starts_with("file:///").then(|| &filename[7..]))
            .or_else(|| filename.starts_with("file:/").then(|| &filename[5..]))
            .unwrap_or(filename);

        if xml_check_filename(path) == 0 {
            return match unescape_url(filename) {
                Ok(unescaped) if filename != unescaped => XmlInputCallback::open(self, &unescaped),
                _ => Err(io::Error::new(
                    ErrorKind::NotFound,
                    format!("{} is not found", path),
                )),
            };
        }

        File::open(path)
            .inspect_err(|_| xml_ioerr(XmlParserErrors::XmlErrOK, Some(path)))
            .map(|file| Box::new(file) as Box<dyn Read>)
    }
}

#[cfg(feature = "http")]
#[derive(Debug, Clone, Copy)]
pub struct DefaultHTTPIOCallbacks {
    write_method: &'static str,
}

#[cfg(feature = "http")]
impl XmlInputCallback for DefaultHTTPIOCallbacks {
    fn is_match(&self, filename: &str) -> bool {
        filename.starts_with("http://")
    }

    fn open(&mut self, filename: &str) -> io::Result<Box<dyn Read>> {
        XmlNanoHTTPCtxt::open(filename, &mut None).map(|ctxt| Box::new(ctxt) as Box<dyn Read>)
    }
}

#[cfg(test)]
mod tests {
    use crate::{globals::reset_last_error, libxml::xmlmemory::xml_mem_blocks};

    use super::*;

    #[test]
    fn test_xml_cleanup_input_callbacks() {
        unsafe {
            let mut leaks = 0;
            let mem_base = xml_mem_blocks();

            cleanup_input_callbacks();
            reset_last_error();
            if mem_base != xml_mem_blocks() {
                leaks += 1;
                eprintln!(
                    "Leak of {} blocks found in xmlCleanupInputCallbacks",
                    xml_mem_blocks() - mem_base
                );
                assert!(
                    leaks == 0,
                    "{leaks} Leaks are found in xmlCleanupInputCallbacks()"
                );
            }
        }
    }

    #[test]
    fn test_xml_cleanup_output_callbacks() {
        #[cfg(feature = "libxml_output")]
        unsafe {
            let mut leaks = 0;

            let mem_base = xml_mem_blocks();

            cleanup_output_callbacks();
            reset_last_error();
            if mem_base != xml_mem_blocks() {
                leaks += 1;
                eprintln!(
                    "Leak of {} blocks found in xmlCleanupOutputCallbacks",
                    xml_mem_blocks() - mem_base
                );
                assert!(
                    leaks == 0,
                    "{leaks} Leaks are found in xmlCleanupOutputCallbacks()"
                );
            }
        }
    }

    #[test]
    fn test_xml_register_default_input_callbacks() {
        unsafe {
            let mut leaks = 0;
            let mem_base = xml_mem_blocks();

            register_default_input_callbacks();
            reset_last_error();
            if mem_base != xml_mem_blocks() {
                leaks += 1;
                eprintln!(
                    "Leak of {} blocks found in xmlRegisterDefaultInputCallbacks",
                    xml_mem_blocks() - mem_base
                );
                assert!(
                    leaks == 0,
                    "{leaks} Leaks are found in xmlRegisterDefaultInputCallbacks()"
                );
            }
        }
    }

    #[test]
    fn test_xml_register_default_output_callbacks() {
        #[cfg(feature = "libxml_output")]
        unsafe {
            let mut leaks = 0;

            let mem_base = xml_mem_blocks();

            register_default_output_callbacks();
            reset_last_error();
            if mem_base != xml_mem_blocks() {
                leaks += 1;
                eprintln!(
                    "Leak of {} blocks found in xmlRegisterDefaultOutputCallbacks",
                    xml_mem_blocks() - mem_base
                );
                assert!(
                    leaks == 0,
                    "{leaks} Leaks are found in xmlRegisterDefaultOutputCallbacks()"
                );
            }
        }
    }

    #[test]
    fn test_xml_register_httppost_callbacks() {
        #[cfg(all(feature = "libxml_output", feature = "http"))]
        unsafe {
            let mut leaks = 0;

            let mem_base = xml_mem_blocks();

            xml_register_http_post_callbacks();
            reset_last_error();
            if mem_base != xml_mem_blocks() {
                leaks += 1;
                eprintln!(
                    "Leak of {} blocks found in xmlRegisterHTTPPostCallbacks",
                    xml_mem_blocks() - mem_base
                );
                assert!(
                    leaks == 0,
                    "{leaks} Leaks are found in xmlRegisterHTTPPostCallbacks()"
                );
            }
        }
    }
}