use crate::bindings;
use crate::lazy_static;
use std::ptr::null;
use std::sync::Mutex;
lazy_static! {
static ref XMLSEC: Mutex<Option<XmlSecContext>> = Mutex::new(None);
}
pub fn guarantee_xmlsec_init()
{
let mut inner = XMLSEC.lock()
.expect("Unable to lock global xmlsec initalization wrapper");
if inner.is_none() {
*inner = Some(XmlSecContext::new());
}
}
struct XmlSecContext {}
impl XmlSecContext
{
pub fn new() -> Self
{
init_xmlsec();
init_crypto_app();
init_crypto();
Self {}
}
}
impl Drop for XmlSecContext
{
fn drop(&mut self)
{
cleanup_crypto();
cleanup_crypto_app();
cleanup_xmlsec();
}
}
fn init_xmlsec()
{
let rc = unsafe { bindings::xmlSecInit() };
if rc < 0 {
panic!("XmlSec failed initialization");
}
}
fn init_crypto_app()
{
let rc = unsafe { bindings::xmlSecOpenSSLAppInit(null()) };
if rc < 0 {
panic!("XmlSec failed to init crypto backend")
}
}
fn init_crypto()
{
let rc = unsafe { bindings::xmlSecOpenSSLInit() };
if rc < 0 {
panic!("XmlSec failed while loading default crypto backend. \
Make sure that you have it installed and check shread libraries path");
}
}
fn cleanup_crypto()
{
unsafe { bindings::xmlSecOpenSSLShutdown() };
}
fn cleanup_crypto_app()
{
unsafe { bindings::xmlSecOpenSSLAppShutdown() };
}
fn cleanup_xmlsec()
{
unsafe { bindings::xmlSecShutdown() };
}