1
0
Fork 0
forked from wry/wry

ei: add support for libei

This commit is contained in:
Julian Orth 2024-07-24 01:38:05 +02:00
parent 084fe50259
commit 40e87f8f91
69 changed files with 4340 additions and 72 deletions

View file

@ -0,0 +1,106 @@
use {
crate::{
ei::ei_object::EiObjectId,
utils::buffd::buf_out::{MsgFds, OutBuffer, OutBufferMeta, OUT_BUF_SIZE},
},
std::{mem, rc::Rc},
uapi::OwnedFd,
};
pub struct EiMsgFormatter<'a> {
buf: &'a mut [u8],
meta: &'a mut OutBufferMeta,
pos: usize,
fds: &'a mut Vec<Rc<OwnedFd>>,
}
impl<'a> EiMsgFormatter<'a> {
pub fn new(buf: &'a mut OutBuffer, fds: &'a mut Vec<Rc<OwnedFd>>) -> Self {
Self {
pos: buf.meta.write_pos,
buf: &mut buf.buf[..],
fds,
meta: &mut buf.meta,
}
}
fn write(&mut self, bytes: &[u8]) {
if bytes.len() > OUT_BUF_SIZE - self.meta.write_pos {
panic!("Out buffer overflow");
}
self.buf[self.meta.write_pos..self.meta.write_pos + bytes.len()].copy_from_slice(bytes);
self.meta.write_pos += bytes.len();
}
pub fn int(&mut self, int: i32) -> &mut Self {
self.write(uapi::as_bytes(&int));
self
}
pub fn uint(&mut self, int: u32) -> &mut Self {
self.write(uapi::as_bytes(&int));
self
}
#[allow(dead_code)]
pub fn long(&mut self, int: i64) -> &mut Self {
self.write(uapi::as_bytes(&int));
self
}
pub fn ulong(&mut self, int: u64) -> &mut Self {
self.write(uapi::as_bytes(&int));
self
}
pub fn float(&mut self, f: f32) -> &mut Self {
self.write(uapi::as_bytes(&f));
self
}
pub fn optstr<S: AsRef<[u8]> + ?Sized>(&mut self, s: Option<&S>) -> &mut Self {
match s {
Some(s) => self.string(s),
_ => self.uint(0),
}
}
pub fn string<S: AsRef<[u8]> + ?Sized>(&mut self, s: &S) -> &mut Self {
let s = s.as_ref();
let len = s.len() + 1;
let cap = (len + 3) & !3;
self.uint(len as u32);
self.write(uapi::as_bytes(s));
let none = [0; 4];
self.write(&none[..cap - len + 1]);
self
}
pub fn fd(&mut self, fd: Rc<OwnedFd>) -> &mut Self {
self.fds.push(fd);
self
}
pub fn object<T: Into<EiObjectId>>(&mut self, obj: T) -> &mut Self {
self.ulong(obj.into().raw())
}
pub fn header<T: Into<EiObjectId>>(&mut self, obj: T, event: u32) -> &mut Self {
self.object(obj).uint(0).uint(event)
}
pub fn write_len(self) {
assert!(self.meta.write_pos - self.pos >= 16);
assert_eq!(self.pos % 4, 0);
unsafe {
let second_ptr = self.buf.as_ptr().add(self.pos + 8) as *mut u32;
*second_ptr = (self.meta.write_pos - self.pos) as u32;
}
if self.fds.len() > 0 {
self.meta.fds.push_back(MsgFds {
pos: self.pos,
fds: mem::take(self.fds),
})
}
}
}

View file

@ -0,0 +1,112 @@
use {
crate::{ei::ei_object::EiObjectId, utils::buffd::BufFdIn},
std::{ptr, rc::Rc},
thiserror::Error,
uapi::OwnedFd,
};
#[derive(Debug, Error)]
pub enum EiMsgParserError {
#[error("The message ended unexpectedly")]
UnexpectedEof,
#[error("The message contained a string of size 0")]
EmptyString,
#[error("Message is missing a required file descriptor")]
MissingFd,
#[error("There is trailing data after the message")]
TrailingData,
#[error("String is not UTF-8")]
NonUtf8,
}
pub struct EiMsgParser<'a, 'b> {
buf: &'a mut BufFdIn,
pos: usize,
data: &'b [u8],
}
impl<'a, 'b> EiMsgParser<'a, 'b> {
pub fn new(buf: &'a mut BufFdIn, data: &'b [u32]) -> Self {
Self {
buf,
pos: 0,
data: uapi::as_bytes(data),
}
}
pub fn int(&mut self) -> Result<i32, EiMsgParserError> {
if self.data.len() - self.pos < 4 {
return Err(EiMsgParserError::UnexpectedEof);
}
let res = unsafe { *(self.data.as_ptr().add(self.pos) as *const i32) };
self.pos += 4;
Ok(res)
}
pub fn uint(&mut self) -> Result<u32, EiMsgParserError> {
self.int().map(|i| i as u32)
}
pub fn long(&mut self) -> Result<i64, EiMsgParserError> {
if self.data.len() - self.pos < 8 {
return Err(EiMsgParserError::UnexpectedEof);
}
let res = unsafe { ptr::read_unaligned(self.data.as_ptr().add(self.pos) as *const i64) };
self.pos += 8;
Ok(res)
}
pub fn ulong(&mut self) -> Result<u64, EiMsgParserError> {
self.long().map(|i| i as u64)
}
pub fn object<T>(&mut self) -> Result<T, EiMsgParserError>
where
EiObjectId: Into<T>,
{
self.ulong().map(|i| EiObjectId::from_raw(i).into())
}
pub fn float(&mut self) -> Result<f32, EiMsgParserError> {
Ok(f32::from_bits(self.uint()?))
}
pub fn optstr(&mut self) -> Result<Option<&'b str>, EiMsgParserError> {
let len = self.uint()? as usize;
if len == 0 {
return Ok(None);
}
let cap = (len + 3) & !3;
if cap > self.data.len() - self.pos {
return Err(EiMsgParserError::UnexpectedEof);
}
let pos = self.pos;
self.pos += cap;
match std::str::from_utf8(&self.data[pos..pos + len - 1]) {
Ok(s) => Ok(Some(s)),
Err(_) => Err(EiMsgParserError::NonUtf8),
}
}
pub fn str(&mut self) -> Result<&'b str, EiMsgParserError> {
match self.optstr()? {
Some(s) => Ok(s),
_ => Err(EiMsgParserError::EmptyString),
}
}
pub fn fd(&mut self) -> Result<Rc<OwnedFd>, EiMsgParserError> {
match self.buf.get_fd() {
Ok(fd) => Ok(fd),
_ => Err(EiMsgParserError::MissingFd),
}
}
pub fn eof(&self) -> Result<(), EiMsgParserError> {
if self.pos == self.data.len() {
Ok(())
} else {
Err(EiMsgParserError::TrailingData)
}
}
}