Skip to main content

doom_fish_utils/
ffi_string.rs

1//! FFI string utilities
2//!
3//! Helper functions for retrieving strings from C/Objective-C APIs
4//! that use buffer-based string retrieval patterns.
5
6use std::ffi::CStr;
7
8/// Default buffer size for FFI string retrieval
9pub const DEFAULT_BUFFER_SIZE: usize = 1024;
10
11/// Smaller buffer size for short strings (e.g., device IDs, stream names)
12pub const SMALL_BUFFER_SIZE: usize = 256;
13
14/// Stack-allocate up to this many bytes — anything bigger falls back to a
15/// heap `Vec`. 256 bytes covers every real call site (`SMALL_BUFFER_SIZE`,
16/// audio device IDs, stream names, microphone IDs); the 1 KiB callers are
17/// rare and currently absent from the codebase, so the heap fallback path
18/// is essentially dead code today but kept for forward-compat with
19/// future longer-string APIs.
20const STACK_BUFFER_BYTES: usize = 256;
21
22/// Retrieves a string from an FFI function that writes to a buffer.
23///
24/// This is a common pattern in Objective-C FFI where a function:
25/// 1. Takes a buffer pointer and length
26/// 2. Writes a null-terminated string to the buffer
27/// 3. Returns a boolean indicating success
28///
29/// # Arguments
30/// * `buffer_size` - Size of the buffer to allocate
31/// * `ffi_call` - A closure that takes (`buffer_ptr`, `buffer_len`) and returns success bool
32///
33/// # Returns
34/// * `Some(String)` if the FFI call succeeded and the string was valid UTF-8
35/// * `None` if the FFI call failed or returned an empty string
36///
37/// # Safety
38/// The caller must ensure the `ffi_call` closure does not write beyond the
39/// provided `buffer_len`. This function defends against the closure writing
40/// a non-NUL-terminated string by scanning the buffer up to its declared
41/// length and treating the absence of a terminator as failure (returns
42/// `None`) rather than reading past the buffer with `CStr::from_ptr`.
43///
44/// # Example
45/// ```
46/// use doom_fish_utils::ffi_string::ffi_string_from_buffer;
47///
48/// let result = unsafe {
49///     ffi_string_from_buffer(64, |buf, len| {
50///         // Simulate FFI call that writes "hello" to buffer
51///         let src = b"hello\0";
52///         if len >= src.len() as isize {
53///             std::ptr::copy_nonoverlapping(src.as_ptr(), buf as *mut u8, src.len());
54///             true
55///         } else {
56///             false
57///         }
58///     })
59/// };
60/// assert_eq!(result, Some("hello".to_string()));
61/// ```
62#[allow(clippy::cast_possible_wrap)]
63pub unsafe fn ffi_string_from_buffer<F>(buffer_size: usize, ffi_call: F) -> Option<String>
64where
65    F: FnOnce(*mut i8, isize) -> bool,
66{
67    // Fast path: the typical small-getter case (audio device IDs, stream
68    // names, microphone IDs) fits comfortably in 256 bytes and is called
69    // often enough that the per-call `vec![0i8; 256]` heap allocation
70    // adds up. Use a stack buffer for those and only fall back to a Vec
71    // for unusually-large requests.
72    if buffer_size <= STACK_BUFFER_BYTES {
73        let mut buffer = [0i8; STACK_BUFFER_BYTES];
74        let success = ffi_call(buffer.as_mut_ptr(), buffer_size as isize);
75        if !success {
76            return None;
77        }
78        return parse_buffer(&buffer[..buffer_size]);
79    }
80
81    let mut buffer = vec![0i8; buffer_size];
82    let success = ffi_call(buffer.as_mut_ptr(), buffer.len() as isize);
83    if !success {
84        return None;
85    }
86    parse_buffer(&buffer)
87}
88
89/// Scan for the NUL terminator and decode the string portion.
90/// Defensive: do NOT use `CStr::from_ptr` here. If the FFI closure
91/// returned `true` but failed to write a NUL terminator, `CStr::from_ptr`
92/// would read past the buffer until it found a zero byte — UB and a
93/// potential information leak. Instead, scan only the buffer we
94/// allocated and treat a missing terminator as failure.
95fn parse_buffer(buffer: &[i8]) -> Option<String> {
96    // SAFETY: `i8` and `u8` have identical layout; the cast is purely a
97    // signed/unsigned reinterpretation.
98    let bytes = unsafe { std::slice::from_raw_parts(buffer.as_ptr().cast::<u8>(), buffer.len()) };
99    let nul_pos = bytes.iter().position(|&b| b == 0)?;
100    let s = String::from_utf8_lossy(&bytes[..nul_pos]).into_owned();
101    if s.is_empty() {
102        None
103    } else {
104        Some(s)
105    }
106}
107
108/// Same as [`ffi_string_from_buffer`] but returns an empty string on failure
109/// instead of `None`.
110///
111/// Useful when the API should always return a string, even if empty.
112///
113/// # Safety
114/// The caller must ensure that the FFI call writes valid UTF-8 data to the buffer.
115#[allow(clippy::cast_possible_wrap)]
116pub unsafe fn ffi_string_from_buffer_or_empty<F>(buffer_size: usize, ffi_call: F) -> String
117where
118    F: FnOnce(*mut i8, isize) -> bool,
119{
120    ffi_string_from_buffer(buffer_size, ffi_call).unwrap_or_default()
121}
122
123/// Retrieves a string from an FFI function that returns an owned C string pointer.
124///
125/// This is more efficient than buffer-based retrieval as it avoids pre-allocation.
126/// The FFI function allocates the string (typically via Swift's `strdup`) and
127/// this helper takes ownership and frees it using the caller-supplied
128/// `free_fn`.
129///
130/// # Arguments
131/// * `ffi_call` - A closure that returns an owned C string pointer (or null)
132/// * `free_fn`  - A function that frees the pointer returned by `ffi_call`.
133///   Each bridging crate passes its own `_free_string` `extern "C"` here
134///   (e.g. `apple_cf::ffi::acf_free_string`,
135///   `screencapturekit::ffi::sc_free_string`).
136///
137/// # Returns
138/// * `Some(String)` if the pointer was non-null and the string was non-empty
139/// * `None` if the pointer was null or the string was empty
140///
141/// # Safety
142/// The caller must ensure the returned pointer was allocated by the Swift /
143/// Objective-C side using a strategy that `free_fn` correctly releases.
144/// The pointer is freed via an RAII guard, so a panic in `from_utf8_lossy`
145/// (extremely rare — only OOM) does not leak the bridge-allocated buffer.
146pub unsafe fn ffi_string_owned<F, Free>(ffi_call: F, free_fn: Free) -> Option<String>
147where
148    F: FnOnce() -> *mut i8,
149    Free: FnOnce(*mut i8),
150{
151    /// RAII guard: releases the bridge-allocated buffer on drop, including
152    /// during panic unwind. Without this, a panic between `CStr::from_ptr`
153    /// and the explicit free call (e.g. allocator failure inside
154    /// `from_utf8_lossy`) would leak the buffer.
155    struct FreeGuard<F: FnOnce(*mut i8)> {
156        ptr: *mut i8,
157        free_fn: Option<F>,
158    }
159    impl<F: FnOnce(*mut i8)> Drop for FreeGuard<F> {
160        fn drop(&mut self) {
161            if !self.ptr.is_null() {
162                if let Some(free) = self.free_fn.take() {
163                    free(self.ptr);
164                }
165            }
166        }
167    }
168
169    let ptr = ffi_call();
170    if ptr.is_null() {
171        return None;
172    }
173    let _guard = FreeGuard {
174        ptr,
175        free_fn: Some(free_fn),
176    };
177    // `to_string_lossy().to_string()` allocates twice on the valid-UTF-8
178    // path: once for the borrowed Cow, then again for the explicit
179    // `to_string`. `from_utf8_lossy(...).into_owned()` allocates once
180    // and skips the redundant copy. For invalid UTF-8 (extremely rare
181    // for AppKit strings) both paths allocate the replacement-char string.
182    let bytes = CStr::from_ptr(ptr).to_bytes();
183    if bytes.is_empty() {
184        return None;
185    }
186    Some(String::from_utf8_lossy(bytes).into_owned())
187}
188
189/// Same as [`ffi_string_owned`] but returns an empty string on failure.
190///
191/// # Safety
192/// Same requirements as [`ffi_string_owned`].
193pub unsafe fn ffi_string_owned_or_empty<F, Free>(ffi_call: F, free_fn: Free) -> String
194where
195    F: FnOnce() -> *mut i8,
196    Free: FnOnce(*mut i8),
197{
198    ffi_string_owned(ffi_call, free_fn).unwrap_or_default()
199}