|
| 1 | +//! Provides implementations for converting to and from Zend binary strings, commonly returned |
| 2 | +//! from functions such as [`pack`] and [`unpack`]. |
| 3 | +//! |
| 4 | +//! [`pack`]: https://www.php.net/manual/en/function.pack.php |
| 5 | +//! [`unpack`]: https://www.php.net/manual/en/function.unpack.php |
| 6 | +
|
| 7 | +use super::types::string::ZendString; |
| 8 | +use crate::bindings::ext_php_rs_zend_string_init; |
| 9 | + |
| 10 | +/// Used to convert between Zend binary strings and vectors. Useful in conjunction with the |
| 11 | +/// [`pack`] and [`unpack`] functions built-in to PHP. |
| 12 | +/// |
| 13 | +/// # Safety |
| 14 | +/// |
| 15 | +/// The types cannot be ensured between PHP and Rust, as the data is represented as a string when |
| 16 | +/// crossing the language boundary. Exercise caution when using these functions. |
| 17 | +/// |
| 18 | +/// [`pack`]: https://www.php.net/manual/en/function.pack.php |
| 19 | +/// [`unpack`]: https://www.php.net/manual/en/function.unpack.php |
| 20 | +pub unsafe trait Pack: Sized { |
| 21 | + fn pack_into(vec: Vec<Self>) -> *mut ZendString; //{ |
| 22 | + // unsafe { ext_php_rs_zend_string_init(ptr as *mut i8, vec.len() as _, false) } |
| 23 | + // } |
| 24 | + |
| 25 | + /// Unpacks a given Zend binary string into a Rust vector. Can be used to pass data from `pack` |
| 26 | + /// in PHP to Rust without encoding into another format. Note that the data *must* be all one |
| 27 | + /// type, as this implementation only unpacks one type. |
| 28 | + /// |
| 29 | + /// # Safety |
| 30 | + /// |
| 31 | + /// This is an unsafe function. There is no way to tell if the data passed from the PHP |
| 32 | + /// function is indeed the correct format. Exercise caution when using the `unpack` functions. |
| 33 | + /// In fact, even when used correctly, the results can differ depending on the platform and the |
| 34 | + /// size of each type on the platform. Consult the [`pack`](https://www.php.net/manual/en/function.pack.php) |
| 35 | + /// function documentation for more details. |
| 36 | + /// |
| 37 | + /// # Parameters |
| 38 | + /// |
| 39 | + /// * `s` - The Zend string containing the binary data. |
| 40 | + unsafe fn unpack_into(s: &ZendString) -> Vec<Self>; |
| 41 | +} |
| 42 | + |
| 43 | +/// Implements the [`Pack`] trait for a given type. |
| 44 | +/// The first argument is the type and the second argument is the factor of size difference between |
| 45 | +/// the given type and an 8-bit integer e.g. impl Unpack for i32, factor = 4 => 4 * 8 = 32 |
| 46 | +#[macro_use] |
| 47 | +macro_rules! pack_impl { |
| 48 | + ($t: ty, $d: expr) => { |
| 49 | + unsafe impl Pack for $t { |
| 50 | + fn pack_into(vec: Vec<Self>) -> *mut ZendString { |
| 51 | + let len = vec.len() * $d; |
| 52 | + let ptr = Box::into_raw(vec.into_boxed_slice()); |
| 53 | + unsafe { ext_php_rs_zend_string_init(ptr as *mut i8, len as _, false) } |
| 54 | + } |
| 55 | + |
| 56 | + unsafe fn unpack_into(s: &ZendString) -> Vec<Self> { |
| 57 | + let len = s.len / $d; |
| 58 | + let mut result = Vec::with_capacity(len as _); |
| 59 | + let ptr = s.val.as_ptr() as *const $t; |
| 60 | + |
| 61 | + for i in 0..len { |
| 62 | + result.push(*ptr.offset(i as _)); |
| 63 | + } |
| 64 | + |
| 65 | + result |
| 66 | + } |
| 67 | + } |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +pack_impl!(u8, 1); |
| 72 | +pack_impl!(i8, 1); |
| 73 | + |
| 74 | +pack_impl!(u16, 2); |
| 75 | +pack_impl!(i16, 2); |
| 76 | + |
| 77 | +pack_impl!(u32, 4); |
| 78 | +pack_impl!(i32, 4); |
| 79 | + |
| 80 | +pack_impl!(u64, 8); |
| 81 | +pack_impl!(i64, 8); |
| 82 | + |
| 83 | +pack_impl!(f32, 4); |
| 84 | +pack_impl!(f64, 8); |
0 commit comments