//! Venus's wire: little-endian values in 4-byte granules, sizes and //! pointer marks as u64 (VK_EXT_command_serialization). What the generated //! encoders and reply decoders are written in. use alloc::vec::Vec; use core::ffi::c_void; /// What every chained struct begins with: its type and the next. #[repr(C)] pub struct Base { pub s_type: i32, pub p_next: *mut Base, } /// A command stream being written. #[derive(Default)] pub struct Encoder(pub Vec); impl Encoder { pub fn u32(&mut self, v: u32) { self.0.extend_from_slice(&v.to_le_bytes()); } pub fn i32(&mut self, v: i32) { self.0.extend_from_slice(&v.to_le_bytes()); } pub fn u64(&mut self, v: u64) { self.0.extend_from_slice(&v.to_le_bytes()); } pub fn i64(&mut self, v: i64) { self.0.extend_from_slice(&v.to_le_bytes()); } pub fn f32(&mut self, v: f32) { self.0.extend_from_slice(&v.to_le_bytes()); } pub fn f64(&mut self, v: f64) { self.0.extend_from_slice(&v.to_le_bytes()); } /// An array's size, a string's length or a pointer's mark. pub fn array_size(&mut self, n: usize) { self.u64(n as u64); } /// A pointer's mark: whether what it points at follows. pub fn pointer(&mut self, present: bool) { self.u64(u64::from(present)); } /// `len` bytes from `at`, padded to 4. /// /// # Safety /// `at` points at `len` readable bytes (or `len` is 0). pub unsafe fn bytes(&mut self, at: *const c_void, len: usize) { if len > 0 { // SAFETY: as the caller promised. self.0 .extend_from_slice(unsafe { core::slice::from_raw_parts(at as *const u8, len) }); } while !self.0.len().is_multiple_of(4) { self.0.push(0); } } /// A NUL-terminated string: its length with the NUL, then its bytes. /// /// # Safety /// `s` is null or a NUL-terminated string. pub unsafe fn string(&mut self, s: *const core::ffi::c_char) { if s.is_null() { return self.array_size(0); } // SAFETY: as the caller promised. let len = unsafe { core::ffi::CStr::from_ptr(s) }.to_bytes().len() + 1; self.array_size(len); // SAFETY: the string and its NUL. unsafe { self.bytes(s as *const c_void, len) }; } } /// A reply being read: values in the order the command's reply has them. pub struct Decoder<'a> { bytes: &'a [u8], at: usize, /// Something did not fit: the rest is not to be trusted. pub fatal: bool, } impl<'a> Decoder<'a> { pub fn new(bytes: &'a [u8]) -> Decoder<'a> { Decoder { bytes, at: 0, fatal: false, } } /// How many bytes were read. pub fn position(&self) -> usize { self.at } fn take(&mut self) -> [u8; N] { let mut out = [0; N]; match self.bytes.get(self.at..self.at + N) { Some(b) => out.copy_from_slice(b), None => self.fatal = true, } self.at += N; out } pub fn u32(&mut self) -> u32 { u32::from_le_bytes(self.take()) } pub fn i32(&mut self) -> i32 { i32::from_le_bytes(self.take()) } pub fn u64(&mut self) -> u64 { u64::from_le_bytes(self.take()) } pub fn i64(&mut self) -> i64 { i64::from_le_bytes(self.take()) } pub fn f32(&mut self) -> f32 { f32::from_le_bytes(self.take()) } pub fn f64(&mut self) -> f64 { f64::from_le_bytes(self.take()) } /// A pointer's mark: whether what it points at follows. pub fn pointer(&mut self) -> bool { self.u64() != 0 } /// The next array size, not taken. pub fn peek_array_size(&mut self) -> u64 { let at = self.at; let n = self.u64(); self.at = at; n } /// An array's size, which must be `expected`: else the reply is not to /// be trusted, and none of it is read. pub fn array_size(&mut self, expected: usize) -> usize { let n = self.u64(); if n != expected as u64 { self.fatal = true; return 0; } expected } /// `len` bytes into `at`, past their padding to 4. /// /// # Safety /// `at` points at `len` writable bytes (or `len` is 0). pub unsafe fn bytes(&mut self, at: *mut c_void, len: usize) { let padded = len.next_multiple_of(4); match self.bytes.get(self.at..self.at + len) { Some(b) if len > 0 && !at.is_null() => { // SAFETY: as the caller promised. unsafe { core::ptr::copy_nonoverlapping(b.as_ptr(), at as *mut u8, len) } } Some(_) => {} None => self.fatal = true, } self.at += padded; } } /// The chained struct in `chain` whose type is `s_type`, for a reply's /// pNext to be read into. /// /// # Safety /// `chain` is null or a valid pNext chain. pub unsafe fn find_in_chain(chain: *mut c_void, s_type: i32) -> *mut c_void { let mut at = chain as *mut Base; // SAFETY: each link as the caller promised. while !at.is_null() && unsafe { (*at).s_type } != s_type { at = unsafe { (*at).p_next }; } at as *mut c_void }