|
| 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: Clone { |
| 21 | + /// Packs a given vector into a Zend binary string. Can be passed to PHP and then unpacked |
| 22 | + /// using the [`unpack`] function. Note you should probably use the [`set_binary`] method on the |
| 23 | + /// [`Zval`] struct instead of this function directly, as there is currently no way to set a |
| 24 | + /// [`ZendString`] on a [`Zval`] directly. |
| 25 | + /// |
| 26 | + /// # Parameters |
| 27 | + /// |
| 28 | + /// * `vec` - The vector to pack into a binary string. |
| 29 | + /// |
| 30 | + /// [`unpack`]: https://www.php.net/manual/en/function.unpack.php |
| 31 | + /// [`Zval`]: crate::php::types::zval::Zval |
| 32 | + /// [`ZendString`]: crate::php::types::string::ZendString |
| 33 | + /// [`set_binary`]: crate::php::types::zval::Zval#method.set_binary |
| 34 | + fn pack_into(vec: Vec<Self>) -> *mut ZendString; |
| 35 | + |
| 36 | + /// Unpacks a given Zend binary string into a Rust vector. Can be used to pass data from `pack` |
| 37 | + /// in PHP to Rust without encoding into another format. Note that the data *must* be all one |
| 38 | + /// type, as this implementation only unpacks one type. |
| 39 | + /// |
| 40 | + /// # Safety |
| 41 | + /// |
| 42 | + /// This is an unsafe function. There is no way to tell if the data passed from the PHP |
| 43 | + /// function is indeed the correct format. Exercise caution when using the `unpack` functions. |
| 44 | + /// In fact, even when used correctly, the results can differ depending on the platform and the |
| 45 | + /// size of each type on the platform. Consult the [`pack`](https://www.php.net/manual/en/function.pack.php) |
| 46 | + /// function documentation for more details. |
| 47 | + /// |
| 48 | + /// # Parameters |
| 49 | + /// |
| 50 | + /// * `s` - The Zend string containing the binary data. |
| 51 | + unsafe fn unpack_into(s: &ZendString) -> Vec<Self>; |
| 52 | +} |
| 53 | + |
| 54 | +/// Implements the [`Pack`] trait for a given type. |
| 55 | +/// The first argument is the type and the second argument is the factor of size difference between |
| 56 | +/// the given type and an 8-bit integer e.g. impl Unpack for i32, factor = 4 => 4 * 8 = 32 |
| 57 | +#[macro_use] |
| 58 | +macro_rules! pack_impl { |
| 59 | + ($t: ty, $d: expr) => { |
| 60 | + unsafe impl Pack for $t { |
| 61 | + fn pack_into(vec: Vec<Self>) -> *mut ZendString { |
| 62 | + let len = vec.len() * $d; |
| 63 | + let ptr = Box::into_raw(vec.into_boxed_slice()); |
| 64 | + unsafe { ext_php_rs_zend_string_init(ptr as *mut i8, len as _, false) } |
| 65 | + } |
| 66 | + |
| 67 | + unsafe fn unpack_into(s: &ZendString) -> Vec<Self> { |
| 68 | + let len = s.len / $d; |
| 69 | + let mut result = Vec::with_capacity(len as _); |
| 70 | + let ptr = s.val.as_ptr() as *const $t; |
| 71 | + |
| 72 | + for i in 0..len { |
| 73 | + result.push(*ptr.offset(i as _)); |
| 74 | + } |
| 75 | + |
| 76 | + result |
| 77 | + } |
| 78 | + } |
| 79 | + }; |
| 80 | +} |
| 81 | + |
| 82 | +pack_impl!(u8, 1); |
| 83 | +pack_impl!(i8, 1); |
| 84 | + |
| 85 | +pack_impl!(u16, 2); |
| 86 | +pack_impl!(i16, 2); |
| 87 | + |
| 88 | +pack_impl!(u32, 4); |
| 89 | +pack_impl!(i32, 4); |
| 90 | + |
| 91 | +pack_impl!(u64, 8); |
| 92 | +pack_impl!(i64, 8); |
| 93 | + |
| 94 | +pack_impl!(f32, 4); |
| 95 | +pack_impl!(f64, 8); |
0 commit comments