| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
/// Support for reading a slice of foreign-language-allocated bytes over the FFI. |
| 6 |
/// |
| 7 |
/// Foreign language code can pass a slice of bytes by providing a data pointer |
| 8 |
/// and length, and this struct provides a convenient wrapper for working with |
| 9 |
/// that pair. Naturally, this can be tremendously unsafe! So here are the details: |
| 10 |
/// |
| 11 |
/// * The foreign language code must ensure the provided buffer stays alive |
| 12 |
/// and unchanged for the duration of the call to which the `ForeignBytes` |
| 13 |
/// struct was provided. |
| 14 |
/// |
| 15 |
/// To work with the bytes in Rust code, use `as_slice()` to view the data |
| 16 |
/// as a `&[u8]`. |
| 17 |
/// |
| 18 |
/// Implementation note: all the fields of this struct are private and it has no |
| 19 |
/// constructors, so consuming crates cant create instances of it. If you've |
| 20 |
/// got a `ForeignBytes`, then you received it over the FFI and are assuming that |
| 21 |
/// the foreign language code is upholding the above invariants. |
| 22 |
/// |
| 23 |
/// This struct is based on `ByteBuffer` from the `ffi-support` crate, but modified |
| 24 |
/// to give a read-only view of externally-provided bytes. |
| 25 |
#[repr(C)] |
| 26 |
pub struct ForeignBytes { |
| 27 |
/// The length of the pointed-to data. |
| 28 |
/// We use an `i32` for compatibility with JNA. |
| 29 |
len: i32, |
| 30 |
/// The pointer to the foreign-owned bytes. |
| 31 |
data: *const u8, |
| 32 |
} |
| 33 |
|
| 34 |
impl ForeignBytes { |
| 35 |
/// Creates a `ForeignBytes` from its constituent fields. |
| 36 |
/// |
| 37 |
/// This is intended mainly as an internal convenience function and should not |
| 38 |
/// be used outside of this module. |
| 39 |
/// |
| 40 |
/// # Safety |
| 41 |
/// |
| 42 |
/// You must ensure that the raw parts uphold the documented invariants of this class. |
| 43 |
pub unsafe fn from_raw_parts(data: *const u8, len: i32) -> Self { |
| 44 |
Self { len, data } |
| 45 |
} |
| 46 |
|
| 47 |
/// View the foreign bytes as a `&[u8]`. |
| 48 |
/// |
| 49 |
/// # Panics |
| 50 |
/// |
| 51 |
/// Panics if the provided struct has a null pointer but non-zero length. |
| 52 |
/// Panics if the provided length is negative. |
| 53 |
pub fn as_slice(&self) -> &[u8] { |
| 54 |
if self.data.is_null() { |
| 55 |
assert!(self.len == 0, "null ForeignBytes had non-zero length"); |
| 56 |
&[] |
| 57 |
} else { |
| 58 |
unsafe { std::slice::from_raw_parts(self.data, self.len()) } |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/// Get the length of this slice of bytes. |
| 63 |
/// |
| 64 |
/// # Panics |
| 65 |
/// |
| 66 |
/// Panics if the provided length is negative. |
| 67 |
pub fn len(&self) -> usize { |
| 68 |
self.len |
| 69 |
.try_into() |
| 70 |
.expect("bytes length negative or overflowed") |
| 71 |
} |
| 72 |
|
| 73 |
/// Returns true if the length of this slice of bytes is 0. |
| 74 |
pub fn is_empty(&self) -> bool { |
| 75 |
self.len == 0 |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
#[cfg(test)] |
| 80 |
mod test { |
| 81 |
use super::*; |
| 82 |
#[test] |
| 83 |
fn test_foreignbytes_access() { |
| 84 |
let v = [1u8, 2, 3]; |
| 85 |
let fbuf = unsafe { ForeignBytes::from_raw_parts(v.as_ptr(), 3) }; |
| 86 |
assert_eq!(fbuf.len(), 3); |
| 87 |
assert_eq!(fbuf.as_slice(), &[1u8, 2, 3]); |
| 88 |
} |
| 89 |
|
| 90 |
#[test] |
| 91 |
fn test_foreignbytes_empty() { |
| 92 |
let v = Vec::<u8>::new(); |
| 93 |
let fbuf = unsafe { ForeignBytes::from_raw_parts(v.as_ptr(), 0) }; |
| 94 |
assert_eq!(fbuf.len(), 0); |
| 95 |
assert_eq!(fbuf.as_slice(), &[0u8; 0]); |
| 96 |
} |
| 97 |
|
| 98 |
#[test] |
| 99 |
fn test_foreignbytes_null_means_empty() { |
| 100 |
let fbuf = unsafe { ForeignBytes::from_raw_parts(std::ptr::null_mut(), 0) }; |
| 101 |
assert_eq!(fbuf.as_slice(), &[0u8; 0]); |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
#[should_panic] |
| 106 |
fn test_foreignbytes_null_must_have_zero_length() { |
| 107 |
let fbuf = unsafe { ForeignBytes::from_raw_parts(std::ptr::null_mut(), 12) }; |
| 108 |
fbuf.as_slice(); |
| 109 |
} |
| 110 |
|
| 111 |
#[test] |
| 112 |
#[should_panic] |
| 113 |
fn test_foreignbytes_provided_len_must_be_non_negative() { |
| 114 |
let v = [0u8, 1, 2]; |
| 115 |
let fbuf = unsafe { ForeignBytes::from_raw_parts(v.as_ptr(), -1) }; |
| 116 |
fbuf.as_slice(); |
| 117 |
} |
| 118 |
} |