Skip to content

Commit

Permalink
Make marshall and unmarshall generic
Browse files Browse the repository at this point in the history
Since the `marshall` and `unmarshall` methods can now fall back on the
more generic `_offset` methods, we can rely on default implementations
directly.

Signed-off-by: Ionut Mihalcea <ionut.mihalcea@arm.com>
  • Loading branch information
ionut-arm committed Aug 6, 2023
1 parent f0f2906 commit 0e27599
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 89 deletions.
22 changes: 0 additions & 22 deletions tss-esapi/src/constants/command_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,23 +158,6 @@ impl From<CommandCode> for TPM2_CC {
impl Marshall for CommandCode {
const BUFFER_SIZE: usize = std::mem::size_of::<TPM2_CC>();

/// Produce a marshalled [TPM2_CC]
fn marshall(&self) -> Result<Vec<u8>> {
let mut buffer = vec![0; Self::BUFFER_SIZE];
let mut offset = 0;

self.marshall_offset(&mut buffer, &mut offset)?;

let checked_offset = usize::try_from(offset).map_err(|e| {
error!("Failed to parse offset as usize: {}", e);
Error::local_error(WrapperErrorKind::InvalidParam)
})?;

buffer.truncate(checked_offset);

Ok(buffer)
}

fn marshall_offset(
&self,
marshalled_data: &mut [u8],
Expand All @@ -201,11 +184,6 @@ impl Marshall for CommandCode {
}

impl UnMarshall for CommandCode {
/// Unmarshall the structure from [`TPM2_CC`]
fn unmarshall(marshalled_data: &[u8]) -> Result<Self> {
CommandCode::unmarshall_offset(marshalled_data, &mut 0)
}

fn unmarshall_offset(
marshalled_data: &[u8],
offset: &mut std::os::raw::c_ulong,
Expand Down
44 changes: 0 additions & 44 deletions tss-esapi/src/interface_types/structure_tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,6 @@ impl TryFrom<TPMI_ST_ATTEST> for AttestationType {
impl Marshall for AttestationType {
const BUFFER_SIZE: usize = std::mem::size_of::<TPMI_ST_ATTEST>();

/// Produce a marshalled [`TPMI_ST_ATTEST`]
fn marshall(&self) -> Result<Vec<u8>> {
let mut buffer = vec![0; Self::BUFFER_SIZE];
let mut offset = 0;

self.marshall_offset(&mut buffer, &mut offset)?;

let checked_offset = usize::try_from(offset).map_err(|e| {
error!("Failed to parse offset as usize: {}", e);
Error::local_error(WrapperErrorKind::InvalidParam)
})?;

buffer.truncate(checked_offset);

Ok(buffer)
}

fn marshall_offset(
&self,
marshalled_data: &mut [u8],
Expand Down Expand Up @@ -122,11 +105,6 @@ impl Marshall for AttestationType {
}

impl UnMarshall for AttestationType {
/// Unmarshall the structure from [`TPMI_ST_ATTEST`]
fn unmarshall(marshalled_data: &[u8]) -> Result<Self> {
AttestationType::unmarshall_offset(marshalled_data, &mut 0)
}

fn unmarshall_offset(
marshalled_data: &[u8],
offset: &mut std::os::raw::c_ulong,
Expand Down Expand Up @@ -200,23 +178,6 @@ impl TryFrom<TPMI_ST_COMMAND_TAG> for CommandTag {
impl Marshall for CommandTag {
const BUFFER_SIZE: usize = std::mem::size_of::<TPMI_ST_COMMAND_TAG>();

/// Produce a marshalled [`TPMI_ST_COMMAND_TAG`]
fn marshall(&self) -> Result<Vec<u8>> {
let mut buffer = vec![0; Self::BUFFER_SIZE];
let mut offset = 0;

self.marshall_offset(&mut buffer, &mut offset)?;

let checked_offset = usize::try_from(offset).map_err(|e| {
error!("Failed to parse offset as usize: {}", e);
Error::local_error(WrapperErrorKind::InvalidParam)
})?;

buffer.truncate(checked_offset);

Ok(buffer)
}

fn marshall_offset(
&self,
marshalled_data: &mut [u8],
Expand Down Expand Up @@ -244,11 +205,6 @@ impl Marshall for CommandTag {
}

impl UnMarshall for CommandTag {
/// Unmarshall the structure from [`TPMI_ST_COMMAND_TAG`]
fn unmarshall(marshalled_data: &[u8]) -> Result<Self> {
CommandTag::unmarshall_offset(marshalled_data, &mut 0)
}

fn unmarshall_offset(
marshalled_data: &[u8],
offset: &mut std::os::raw::c_ulong,
Expand Down
41 changes: 18 additions & 23 deletions tss-esapi/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,21 @@ use crate::{Error, Result, ReturnCode, WrapperErrorKind};
pub trait Marshall: Sized {
const BUFFER_SIZE: usize;
/// Returns the type in the form of marshalled data
fn marshall(&self) -> Result<Vec<u8>>;
fn marshall(&self) -> Result<Vec<u8>> {
let mut buffer = vec![0; Self::BUFFER_SIZE];
let mut offset = 0;

self.marshall_offset(&mut buffer, &mut offset)?;

let checked_offset = usize::try_from(offset).map_err(|e| {
error!("Failed to parse offset as usize: {}", e);
Error::local_error(WrapperErrorKind::InvalidParam)
})?;

buffer.truncate(checked_offset);

Ok(buffer)
}

/// Writes the type in the form of marshalled data to `marshalled_data`,
/// and modifies the `offset` to point to the first byte in the buffer
Expand All @@ -30,7 +44,9 @@ pub trait Marshall: Sized {
/// TPM marshalled data.
pub trait UnMarshall: Sized {
/// Creates the type from marshalled data.
fn unmarshall(marshalled_data: &[u8]) -> Result<Self>;
fn unmarshall(marshalled_data: &[u8]) -> Result<Self> {
Self::unmarshall_offset(marshalled_data, &mut 0)
}

/// Creates the type from the marshalled data, and modifies
/// the `offset` to point to the first byte in the `marshalled_data`
Expand All @@ -46,23 +62,6 @@ pub trait UnMarshall: Sized {
impl Marshall for u32 {
const BUFFER_SIZE: usize = std::mem::size_of::<UINT32>();

/// Produce a marshalled [UINT32]
fn marshall(&self) -> Result<Vec<u8>> {
let mut buffer = vec![0; Self::BUFFER_SIZE];
let mut offset = 0;

self.marshall_offset(&mut buffer, &mut offset)?;

let checked_offset = usize::try_from(offset).map_err(|e| {
error!("Failed to parse offset as usize: {}", e);
Error::local_error(WrapperErrorKind::InvalidParam)
})?;

buffer.truncate(checked_offset);

Ok(buffer)
}

fn marshall_offset(
&self,
marshalled_data: &mut [u8],
Expand Down Expand Up @@ -90,10 +89,6 @@ impl Marshall for u32 {
}

impl UnMarshall for u32 {
fn unmarshall(marshalled_data: &[u8]) -> Result<Self> {
u32::unmarshall_offset(marshalled_data, &mut 0)
}

fn unmarshall_offset(
marshalled_data: &[u8],
offset: &mut std::os::raw::c_ulong,
Expand Down

0 comments on commit 0e27599

Please sign in to comment.