Skip to main content

apple_cf/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 apple_cf::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 (via `strdup`) and this function takes
127/// ownership and frees it.
128///
129/// # Arguments
130/// * `ffi_call` - A closure that returns an owned C string pointer (or null)
131///
132/// # Returns
133/// * `Some(String)` if the pointer was non-null and valid UTF-8
134/// * `None` if the pointer was null
135///
136/// # Safety
137/// The caller must ensure the returned pointer was allocated by Swift's `strdup`
138/// or equivalent, and that `sc_free_string` properly frees it. The pointer is
139/// freed via an RAII guard, so a panic in `to_string_lossy` (extremely rare —
140/// only OOM) does not leak the Swift-allocated buffer.
141pub unsafe fn ffi_string_owned<F>(ffi_call: F) -> Option<String>
142where
143    F: FnOnce() -> *mut i8,
144{
145    /// RAII guard: releases the Swift-allocated buffer on drop, including
146    /// during panic unwind. Without this, a panic between `CStr::from_ptr`
147    /// and the explicit `sc_free_string` call (e.g. allocator failure
148    /// inside `to_string_lossy`) would leak the buffer.
149    struct FreeGuard(*mut i8);
150    impl Drop for FreeGuard {
151        fn drop(&mut self) {
152            if !self.0.is_null() {
153                unsafe { crate::ffi::acf_free_string(self.0) };
154            }
155        }
156    }
157
158    let ptr = ffi_call();
159    if ptr.is_null() {
160        return None;
161    }
162    let _guard = FreeGuard(ptr);
163    // `to_string_lossy().to_string()` allocates twice on the valid-UTF-8
164    // path: once for the borrowed Cow, then again for the explicit
165    // `to_string`. `from_utf8_lossy(...).into_owned()` allocates once
166    // and skips the redundant copy. For invalid UTF-8 (extremely rare
167    // for AppKit strings) both paths allocate the replacement-char string.
168    let bytes = CStr::from_ptr(ptr).to_bytes();
169    if bytes.is_empty() {
170        return None;
171    }
172    Some(String::from_utf8_lossy(bytes).into_owned())
173}
174
175/// Same as [`ffi_string_owned`] but returns an empty string on failure.
176///
177/// # Safety
178/// Same requirements as [`ffi_string_owned`].
179pub unsafe fn ffi_string_owned_or_empty<F>(ffi_call: F) -> String
180where
181    F: FnOnce() -> *mut i8,
182{
183    ffi_string_owned(ffi_call).unwrap_or_default()
184}