Skip to main content

deaddrop_sdk/
ffi.rs

1//! C ABI. Python/Go/Swift/Kotlin bindings should link this, not crate internals.
2
3use std::ffi::{CStr, CString};
4use std::os::raw::{c_char, c_int};
5
6#[unsafe(no_mangle)]
7pub extern "C" fn dd_version() -> *mut c_char {
8    CString::new(deaddrop_core::PROTOCOL_LABEL)
9        .unwrap()
10        .into_raw()
11}
12
13/// # Safety
14/// `s` must be a malloc'd string from this ABI or null.
15#[unsafe(no_mangle)]
16pub unsafe extern "C" fn dd_string_free(s: *mut c_char) {
17    if !s.is_null() {
18        unsafe {
19            drop(CString::from_raw(s));
20        }
21    }
22}
23
24#[unsafe(no_mangle)]
25pub extern "C" fn dd_protocol_version() -> c_int {
26    deaddrop_core::PROTOCOL_VERSION as c_int
27}
28
29/// # Safety
30/// `path` is a UTF-8 C string.
31#[unsafe(no_mangle)]
32pub unsafe extern "C" fn dd_node_open(path: *const c_char) -> c_int {
33    if path.is_null() {
34        return -1;
35    }
36    let p = unsafe { CStr::from_ptr(path) }.to_string_lossy();
37    match crate::DeadDrop::open_dir(std::path::Path::new(p.as_ref())) {
38        Ok(_) => 0,
39        Err(_) => -1,
40    }
41}