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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use crate::{Error, Result};
use libc::{c_char, c_void, free, malloc, memset};
use std::{
    ffi::{CStr, CString},
    mem,
    ptr::NonNull,
};

/// A helper to read C string
pub struct StringReader {
    buf: Vec<i8>,
}

impl StringReader {
    /// Create a new reader
    ///
    /// # Arguments
    ///
    /// * max_length - Maximum length of string
    pub fn new(max_length: usize) -> Self {
        Self {
            buf: Vec::with_capacity(max_length),
        }
    }

    /// Get a pointer to read to
    pub fn get_target(&mut self) -> *mut c_char {
        self.buf.as_mut_ptr()
    }

    /// Get a result string
    pub fn into_string(self) -> Result<String> {
        self.into_string_opt()?.ok_or_else(|| Error::Null)
    }

    /// Get a result string or None if pointer is NULL
    pub fn into_string_opt(mut self) -> Result<Option<String>> {
        let ptr = self.buf.as_mut_ptr();
        mem::forget(self.buf);
        if ptr.is_null() {
            Ok(None)
        } else {
            Ok(Some(
                unsafe { CStr::from_ptr(ptr) }
                    .to_str()
                    .map(|x| x.to_string())?,
            ))
        }
    }
}

/// A wrapper for null-terminated C string array
pub struct StringArray {
    ptr: NonNull<*const c_char>,
    should_drop: bool,
    has_dropped: bool,
}

impl StringArray {
    /// Creates a new string array
    ///
    /// # Panics
    ///
    /// Panics if memory allocation failed
    ///
    /// # Arguments
    ///
    /// * items - Items to copy
    pub fn new<T, I>(items: T) -> Result<Self>
    where
        T: IntoIterator<Item = I>,
        I: AsRef<str>,
    {
        let items: Vec<I> = items.into_iter().collect();
        let array_size = mem::size_of::<*const c_char>() * (items.len() + 1);
        let array_ptr = unsafe {
            let ptr = malloc(array_size);
            assert!(!ptr.is_null());
            memset(ptr, 0, array_size);
            ptr as *mut *const c_char
        };
        for (item_idx, item_data) in items.iter().enumerate() {
            let item_idx = item_idx as isize;
            let item_data = item_data.as_ref().as_bytes();
            unsafe {
                let item_ptr = array_ptr.offset(item_idx);
                *item_ptr = expose_string(item_data)?;
            }
        }
        Ok(Self {
            ptr: unsafe { NonNull::new_unchecked(array_ptr) },
            should_drop: true,
            has_dropped: false,
        })
    }

    /// Returns a raw pointer to string array
    ///
    /// You MUST be sure that string array is deallocated
    ///
    /// Use `from_raw` method with `sould_drop=true`,
    /// or make sure that C code deallocates a returned data.
    pub fn into_raw(mut self) -> *mut *const c_char {
        self.should_drop = false;
        self.ptr.as_ptr()
    }

    /// Constructs a string array from raw pointer
    ///
    /// # Safety
    ///
    /// Improper use may lead to memory problems.
    /// For example, a double-free may occur
    /// if the function is called twice on the same raw pointer.
    ///
    /// # Panics
    ///
    /// Pointer must be not NULL
    ///
    /// # Arguments
    ///
    /// * ptr - A pointer to C string array
    /// * should_drop - Should data be deallocated when `drop()` is called
    pub unsafe fn from_raw(ptr: *mut *const c_char, should_drop: bool) -> Self {
        Self {
            ptr: NonNull::new(ptr).expect("Pointer must be not NULL"),
            should_drop,
            has_dropped: false,
        }
    }

    fn free(&mut self) {
        if self.should_drop && !self.has_dropped {
            unsafe { free(self.ptr.as_ptr().cast()) }
            self.has_dropped = true;
        }
    }
}

impl Drop for StringArray {
    fn drop(&mut self) {
        self.free()
    }
}

impl IntoIterator for StringArray {
    type Item = Result<String>;
    type IntoIter = StringArrayIter;

    fn into_iter(self) -> Self::IntoIter {
        StringArrayIter::new(self)
    }
}

/// Iterator over StringArray
pub struct StringArrayIter {
    array: StringArray,
    current_index: isize,
}

impl StringArrayIter {
    fn new(array: StringArray) -> Self {
        Self {
            array,
            current_index: 0,
        }
    }
}

impl Iterator for StringArrayIter {
    type Item = Result<String>;

    fn next(&mut self) -> Option<Self::Item> {
        let item_ptr = unsafe { *self.array.ptr.as_ptr().offset(self.current_index) };
        if item_ptr.is_null() {
            None
        } else {
            self.current_index += 1;
            Some(
                unsafe { CStr::from_ptr(item_ptr) }
                    .to_str()
                    .map(|x| x.to_string())
                    .map_err(Error::from),
            )
        }
    }
}

/// Copies a rust string to a newly allocated C String
///
/// Use this function if you are unable to deallocate string in Rust code.
/// You MUST be sure that string is deallocated.
pub fn expose_string<T: Into<Vec<u8>>>(input: T) -> Result<*const c_char> {
    let input = input.into();
    let size = input.len() + 1;
    let input = CString::new(input)?;
    let src = input.as_ptr().cast::<c_void>();
    let dest = unsafe {
        let p = malloc(size);
        assert!(!p.is_null());
        src.copy_to_nonoverlapping(p, size);
        p.cast::<i8>()
    };
    Ok(dest)
}

#[cfg(test)]
mod tests {
    use super::*;
    use libc::strcpy;
    use std::ptr::null_mut;

    #[test]
    fn test_read_and_write_string() {
        let input = "test";
        let ptr = expose_string(input).unwrap();
        let mut reader = StringReader::new(input.len());
        let copy_ptr = reader.get_target();
        unsafe { strcpy(copy_ptr, ptr) };
        let result = reader.into_string().unwrap();
        assert_eq!(result, input);
        unsafe { libc::free(ptr as *mut c_void) };
    }

    #[test]
    fn test_read_and_write_string_array() {
        let array = StringArray::new(&["a", "b", "c"]).unwrap();
        let ptr = array.into_raw();
        let array = unsafe { StringArray::from_raw(ptr, true) };
        let items: Vec<String> = array.into_iter().map(|x| x.unwrap()).collect();
        assert_eq!(items, vec!["a", "b", "c"]);
    }

    #[test]
    fn test_drop_string_array() {
        let array = StringArray::new(&["a", "b", "c"]).unwrap();
        let ptr = array.into_raw();
        let mut array = unsafe { StringArray::from_raw(ptr, false) };
        array.free();
        assert!(!array.has_dropped);
        let mut array = unsafe { StringArray::from_raw(ptr, true) };
        array.free();
        assert!(array.has_dropped);
    }

    #[test]
    #[should_panic]
    fn test_string_array_from_raw_null() {
        let _ = unsafe { StringArray::from_raw(null_mut(), true) };
    }
}