Skip to content

Commit

Permalink
no need to have the filedescriptors of the UnmarshalContext public
Browse files Browse the repository at this point in the history
  • Loading branch information
KillingSpark committed Mar 1, 2024
1 parent 56d23ac commit e49e5b7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 37 deletions.
9 changes: 2 additions & 7 deletions rustbus/src/wire/unmarshal/param/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,8 @@ pub fn unmarshal_base(
Ok(params::Base::Uint32(val))
}
signature::Base::UnixFd => {
let idx = ctx.read_u32()?;
if ctx.fds.len() <= idx as usize {
Err(UnmarshalError::BadFdIndex(idx as usize))
} else {
let val = &ctx.fds[idx as usize];
Ok(params::Base::UnixFd(val.clone()))
}
let val = ctx.read_unixfd()?;
Ok(params::Base::UnixFd(val))
}
signature::Base::Int32 => {
let val = ctx.read_i32()?;
Expand Down
6 changes: 2 additions & 4 deletions rustbus/src/wire/unmarshal/param/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ pub fn unmarshal_container(
ctx.align_to(elem_sig.get_alignment())?;

let mut elements = Vec::new();
let raw_elements = ctx.read_raw(bytes_in_array)?;
let mut ctx = UnmarshalContext::new(ctx.fds, ctx.byteorder, raw_elements, 0);
let mut ctx = ctx.sub_context(bytes_in_array)?;
while !ctx.remainder().is_empty() {
let element = unmarshal_with_sig(elem_sig, &mut ctx)?;
elements.push(element);
Expand All @@ -69,8 +68,7 @@ pub fn unmarshal_container(
ctx.align_to(8)?;

let mut elements = std::collections::HashMap::new();
let raw_elements = ctx.read_raw(bytes_in_dict)?;
let mut ctx = UnmarshalContext::new(ctx.fds, ctx.byteorder, raw_elements, 0);
let mut ctx = ctx.sub_context(bytes_in_dict)?;
while !ctx.remainder().is_empty() {
ctx.align_to(8)?;

Expand Down
21 changes: 5 additions & 16 deletions rustbus/src/wire/unmarshal/traits/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::wire::errors::UnmarshalError;
use crate::wire::marshal::traits::SignatureBuffer;
use crate::wire::unmarshal;
use crate::wire::unmarshal_context::UnmarshalContext;
use crate::ByteOrder;
use crate::Signature;
use crate::Unmarshal;
use std::borrow::Cow;
Expand Down Expand Up @@ -185,8 +184,7 @@ impl<'buf, 'fds, E: Unmarshal<'buf, 'fds>> Unmarshal<'buf, 'fds> for Vec<E> {
ctx.align_to(E::alignment())?;

let mut elements = Vec::new();
let raw_elements = ctx.read_raw(bytes_in_array)?;
let mut ctx = UnmarshalContext::new(ctx.fds, ctx.byteorder, raw_elements, 0);
let mut ctx = ctx.sub_context(bytes_in_array)?;
while !ctx.remainder().is_empty() {
ctx.align_to(E::alignment())?;
let element = E::unmarshal(&mut ctx)?;
Expand All @@ -208,8 +206,7 @@ impl<'buf, 'fds, K: Unmarshal<'buf, 'fds> + std::hash::Hash + Eq, V: Unmarshal<'
ctx.align_to(8)?;

let mut map = std::collections::HashMap::new();
let raw_elements = ctx.read_raw(bytes_in_array)?;
let mut ctx = UnmarshalContext::new(ctx.fds, ctx.byteorder, raw_elements, 0);
let mut ctx = ctx.sub_context(bytes_in_array)?;
while !ctx.remainder().is_empty() {
// Always align to 8
ctx.align_to(8)?;
Expand All @@ -229,10 +226,7 @@ impl<'buf, 'fds, K: Unmarshal<'buf, 'fds> + std::hash::Hash + Eq, V: Unmarshal<'
#[derive(Debug)]
pub struct Variant<'fds, 'buf> {
pub(crate) sig: signature::Type,
pub(crate) byteorder: ByteOrder,
pub(crate) offset: usize,
pub(crate) buf: &'buf [u8],
pub(crate) fds: &'fds [crate::wire::UnixFd],
pub(crate) sub_ctx: UnmarshalContext<'fds, 'buf>,
}

impl<'buf, 'fds> Variant<'fds, 'buf> {
Expand All @@ -250,7 +244,7 @@ impl<'buf, 'fds> Variant<'fds, 'buf> {
if self.sig != T::signature() {
return Err(UnmarshalError::WrongSignature);
}
let mut ctx = UnmarshalContext::new(self.fds, self.byteorder, self.buf, self.offset);
let mut ctx = self.sub_ctx;
T::unmarshal(&mut ctx)
}
}
Expand Down Expand Up @@ -290,14 +284,9 @@ impl<'buf, 'fds> Unmarshal<'buf, 'fds> for Variant<'fds, 'buf> {
crate::wire::validate_raw::validate_marshalled(ctx.byteorder, 0, ctx.remainder(), &sig)
.map_err(|e| e.1)?;

let raw_content = ctx.read_raw(val_bytes)?;

Ok(Variant {
sig,
buf: raw_content,
offset: 0,
byteorder: ctx.byteorder,
fds: ctx.fds,
sub_ctx: ctx.sub_context(val_bytes)?,
})
}
}
Expand Down
20 changes: 19 additions & 1 deletion rustbus/src/wire/unmarshal_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ use super::{
errors::UnmarshalError,
unmarshal::UnmarshalResult,
util::{parse_u16, parse_u32, parse_u64, unmarshal_signature, unmarshal_str},
UnixFd,
};

#[derive(Debug, Clone, Copy)]
pub struct UnmarshalContext<'fds, 'buf> {
pub fds: &'fds [crate::wire::UnixFd],
pub byteorder: ByteOrder,
fds: &'fds [crate::wire::UnixFd],
cursor: Cursor<'buf>,
}

Expand All @@ -26,6 +28,11 @@ impl<'fds, 'buf> UnmarshalContext<'fds, 'buf> {
}
}

pub fn sub_context(&mut self, length: usize) -> UnmarshalResult<UnmarshalContext<'fds, 'buf>> {
let region = self.read_raw(length)?;
Ok(UnmarshalContext::new(self.fds, self.byteorder, region, 0))
}

pub fn align_to(&mut self, alignment: usize) -> Result<usize, UnmarshalError> {
self.cursor.align_to(alignment)
}
Expand Down Expand Up @@ -54,6 +61,16 @@ impl<'fds, 'buf> UnmarshalContext<'fds, 'buf> {
self.cursor.read_u32(self.byteorder)
}

pub fn read_unixfd(&mut self) -> UnmarshalResult<UnixFd> {
let idx = self.cursor.read_u32(self.byteorder)?;
if self.fds.len() <= idx as usize {
Err(UnmarshalError::BadFdIndex(idx as usize))
} else {
let val = &self.fds[idx as usize];
Ok(val.clone())
}
}

pub fn read_i64(&mut self) -> UnmarshalResult<i64> {
self.cursor.read_i64(self.byteorder)
}
Expand Down Expand Up @@ -83,6 +100,7 @@ impl<'fds, 'buf> UnmarshalContext<'fds, 'buf> {
}
}

#[derive(Debug, Clone, Copy)]
pub struct Cursor<'a> {
buf: &'a [u8],
offset: usize,
Expand Down
11 changes: 2 additions & 9 deletions rustbus/src/wire/wrapper_types/unixfd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::wire::errors::{MarshalError, UnmarshalError};
use crate::wire::errors::MarshalError;
use crate::wire::marshal::traits::SignatureBuffer;
use crate::wire::marshal::MarshalContext;
use crate::wire::unmarshal_context::UnmarshalContext;
Expand Down Expand Up @@ -191,14 +191,7 @@ impl<'buf, 'fds> Unmarshal<'buf, 'fds> for UnixFd {
fn unmarshal(
ctx: &mut UnmarshalContext<'fds, 'buf>,
) -> crate::wire::unmarshal::UnmarshalResult<Self> {
let idx = u32::unmarshal(ctx)?;

if ctx.fds.len() <= idx as usize {
Err(UnmarshalError::BadFdIndex(idx as usize))
} else {
let val = &ctx.fds[idx as usize];
Ok(val.clone())
}
ctx.read_unixfd()
}
}

Expand Down

0 comments on commit e49e5b7

Please sign in to comment.