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
#[cfg(unix)] mod internal_impl {
    // Platform specific things
    use std::os::raw;
    use std::ffi::OsStr;
    use super::cstr_cow_from_bytes;

    extern { // syscalls
        fn dlopen(filename: *const raw::c_char, flags: raw::c_int) -> *mut raw::c_void;
        fn dlsym(handle: *mut raw::c_void, symbol: *const raw::c_char) -> *mut raw::c_void;
        fn dlclose(handle: *mut raw::c_void) -> raw::c_int;
    }

    #[cfg(not(target_os="android"))]
    const RTLD_NOW: raw::c_int = 2;
    #[cfg(target_os="android")]
    const RTLD_NOW: raw::c_int = 0;

    /// A platform-specific equivalent of the cross-platform `Library`.
    pub struct Library(*mut raw::c_void);

    unsafe impl Send for Library {}
    unsafe impl Sync for Library {}

    impl Library {
        pub fn new<P: AsRef<OsStr>>(path: P) -> Option<Self> {
            use std::os::unix::ffi::OsStrExt;
            let file_path = cstr_cow_from_bytes(path.as_ref().as_bytes())?;
            let result = unsafe { dlopen(file_path.as_ptr(), RTLD_NOW) };
            if result.is_null() { None } else { Some(Library(result)) }
        }

        pub fn get(&self, symbol: &[u8]) -> Option<*mut raw::c_void> {
            let symbol = cstr_cow_from_bytes(symbol)?;
            let symbol = unsafe { dlsym(self.0, symbol.as_ptr()) };
            if symbol.is_null() { None } else { Some(symbol) }
        }
    }

    impl Drop for Library {
        fn drop(&mut self) {
            unsafe { dlclose(self.0) };
        }
    }
}

#[cfg(windows)] mod internal_impl {

    extern crate winapi;

    use self::winapi::shared::minwindef::{HMODULE, FARPROC};
    use self::winapi::um::libloaderapi;
    use super::cstr_cow_from_bytes;
    use std::ffi::OsStr;

    pub struct Library(HMODULE);

    unsafe impl Send for Library {}
    unsafe impl Sync for Library {}

    impl Library {
        pub fn new<P: AsRef<OsStr>>(path: P) -> Option<Self> {
            use std::os::windows::ffi::OsStrExt;
            let wide_filename: Vec<u16> = path.as_ref().encode_wide().chain(Some(0)).collect();
            let handle = unsafe { libloaderapi::LoadLibraryExW(wide_filename.as_ptr(), std::ptr::null_mut(), 0) };
            if handle.is_null()  { None } else { Some(Library(handle)) }
        }
        pub fn get(&self, symbol: &[u8]) -> Option<FARPROC> {
            let symbol = cstr_cow_from_bytes(symbol)?;
            let symbol = unsafe { libloaderapi::GetProcAddress(self.0, symbol.as_ptr()) };
            if symbol.is_null() { None } else { Some(symbol) }
        }
    }

    impl Drop for Library {
        fn drop(&mut self) {
            unsafe { libloaderapi::FreeLibrary(self.0); }
        }
    }
}

use std::ffi::{CStr, CString};
use std::borrow::Cow;
use std::os::raw;

pub(crate) fn cstr_cow_from_bytes<'a>(slice: &'a [u8]) -> Option<Cow<'a, CStr>> {
    static ZERO: raw::c_char = 0;
    Some(match slice.last() {
        // Slice out of 0 elements
        None => unsafe { Cow::Borrowed(CStr::from_ptr(&ZERO)) },
        // Slice with trailing 0
        Some(&0) => {
            Cow::Borrowed(CStr::from_bytes_with_nul(slice).ok()?)
        },
        // Slice with no trailing 0
        Some(_) => Cow::Owned(CString::new(slice).ok()?),
    })
}

pub use internal_impl::Library;